void MifareStorageCardService::erase(std::shared_ptr<Location> location, std::shared_ptr<AccessInfo> aiToUse)
    {
        std::shared_ptr<MifareLocation> mLocation = std::dynamic_pointer_cast<MifareLocation>(location);
        if (!mLocation)
        {
            return;
        }

        unsigned int zeroblock_size = getMifareChip()->getMifareCommands()->getNbBlocks(mLocation->sector) * 16;

        std::shared_ptr<MifareAccessInfo> _aiToWrite;
        _aiToWrite.reset(new MifareAccessInfo());

        if (mLocation->sector == 0)
        {
            if (mLocation->block == 0)
            {
                mLocation->block = 1;
            }
        }
        else if (mLocation->useMAD)
        {
            std::shared_ptr<MifareLocation> madLocation(new MifareLocation());
            madLocation->sector = 0;
            madLocation->block = 1;

            std::shared_ptr<MifareAccessInfo> madAi(new MifareAccessInfo());
            if (aiToUse)
            {
                std::shared_ptr<MifareAccessInfo> mAiToUse = std::dynamic_pointer_cast<MifareAccessInfo>(aiToUse);
                if (mAiToUse->useMAD)
                {
                    madAi->keyA = mAiToUse->madKeyA;
                    madAi->keyB = mAiToUse->madKeyB;
                }
            }

            if (madAi->keyB && !madAi->keyB->isEmpty())
            {
                madAi->sab.setAReadBWriteConfiguration();
            }
			std::vector<unsigned char> zeroblock(32, 0x00);
            writeData(madLocation, madAi, _aiToWrite, zeroblock,  CB_DEFAULT);
        }

        bool tmpuseMAD = mLocation->useMAD;
        mLocation->useMAD = false;
		std::vector<unsigned char> zeroblock(zeroblock_size - (mLocation->block * 16), 0x00);
        writeData(location, aiToUse, _aiToWrite, zeroblock, CB_DEFAULT);
        mLocation->useMAD = tmpuseMAD;
    }
    void MifareUltralightStorageCardService::erase()
    {
		std::vector<unsigned char> zeroblock(4, 0x00);
        for (unsigned int i = 4; i < 16; ++i)
        {
            getMifareUltralightChip()->getMifareUltralightCommands()->writePage(i, zeroblock);
        }
    }
    void MifareStorageCardService::erase()
    {
        std::vector<unsigned char> zeroblock(16, 0x00);
		std::vector<unsigned char> trailerblock(16, 0xFF);

        MifareAccessInfo::SectorAccessBits sab;
        sab.setTransportConfiguration();
        trailerblock[9] = 0x00;

        if (!sab.toArray(&trailerblock[6], 3))
        {
            THROW_EXCEPTION_WITH_LOG(std::invalid_argument, "Bad sector access bits configuration.");
        }

        for (unsigned int i = 0; i < getMifareChip()->getNbSectors(); ++i)
        {
            bool erased = true;
            bool used = false;
            unsigned int firstBlock = (i == 0) ? 1 : 0; // Don't write the first block in sector 0

            std::shared_ptr<MifareLocation> location(new MifareLocation());
            location->sector = i;
            if (getMifareChip()->getMifareProfile()->getKeyUsage(i, KT_KEY_B))
            {
                used = true;

                std::shared_ptr<MifareKey> key = getMifareChip()->getMifareProfile()->getKey(i, KT_KEY_B);

                getMifareChip()->getMifareCommands()->loadKey(location, key, KT_KEY_B);
                getMifareChip()->getMifareCommands()->authenticate(static_cast<unsigned char>(getMifareChip()->getMifareCommands()->getSectorStartBlock(i)), key->getKeyStorage(), KT_KEY_B);

                for (unsigned int j = firstBlock; j < getMifareChip()->getMifareCommands()->getNbBlocks(i); ++j)
                {
                    getMifareChip()->getMifareCommands()->updateBinary(static_cast<unsigned char>(getMifareChip()->getMifareCommands()->getSectorStartBlock(i) + j), zeroblock);
                }

                getMifareChip()->getMifareCommands()->updateBinary(static_cast<unsigned char>(getMifareChip()->getMifareCommands()->getSectorStartBlock(i) + getMifareChip()->getMifareCommands()->getNbBlocks(i)), trailerblock);
            }

            if ((!erased || !used) && getMifareChip()->getMifareProfile()->getKeyUsage(i, KT_KEY_A))
            {
                used = true;

                std::shared_ptr<MifareKey> key = getMifareChip()->getMifareProfile()->getKey(i, KT_KEY_A);

                getMifareChip()->getMifareCommands()->loadKey(location, key, KT_KEY_A);
                getMifareChip()->getMifareCommands()->authenticate(static_cast<unsigned char>(getMifareChip()->getMifareCommands()->getSectorStartBlock(i)), key->getKeyStorage(), KT_KEY_A);

                for (unsigned int j = firstBlock; j < getMifareChip()->getMifareCommands()->getNbBlocks(i); ++j)
                {
                    getMifareChip()->getMifareCommands()->updateBinary(static_cast<unsigned char>(getMifareChip()->getMifareCommands()->getSectorStartBlock(i) + j), zeroblock);
                }

                getMifareChip()->getMifareCommands()->updateBinary(static_cast<unsigned char>(getMifareChip()->getMifareCommands()->getSectorStartBlock(i) + getMifareChip()->getMifareCommands()->getNbBlocks(i)), trailerblock);
            }
        }
    }
    void MifareUltralightStorageCardService::erase(std::shared_ptr<Location> location, std::shared_ptr<AccessInfo> aiToUse)
    {
        std::shared_ptr<MifareUltralightLocation> mLocation = std::dynamic_pointer_cast<MifareUltralightLocation>(location);
        if (!mLocation)
        {
            return;
        }

        std::vector<unsigned char> zeroblock(4, 0x00);

        writeData(location, aiToUse, std::shared_ptr<AccessInfo>(), zeroblock, CB_DEFAULT);
    }