| 961 | } |
| 962 | |
| 963 | KVHolderRet_t MMKV::doOverrideDataWithKey(const MMBuffer &data, |
| 964 | const MMBuffer &keyData, |
| 965 | bool isDataHolder, |
| 966 | uint32_t originKeyLength) { |
| 967 | auto isKeyEncoded = (originKeyLength < keyData.length()); |
| 968 | auto keyLength = static_cast<uint32_t>(keyData.length()); |
| 969 | auto valueLength = static_cast<uint32_t>(data.length()); |
| 970 | if (isDataHolder) { |
| 971 | valueLength += pbRawVarint32Size(valueLength); |
| 972 | } |
| 973 | // size needed to encode the key |
| 974 | size_t size = isKeyEncoded ? keyLength : (keyLength + pbRawVarint32Size(keyLength)); |
| 975 | // size needed to encode the value |
| 976 | size += valueLength + pbRawVarint32Size(valueLength); |
| 977 | |
| 978 | if (!checkSizeLimit(size, keyData, originKeyLength)) { |
| 979 | return make_pair(false, KeyValueHolder()); |
| 980 | } |
| 981 | |
| 982 | if (!checkSizeForOverride(size)) { |
| 983 | return doAppendDataWithKey(data, keyData, isDataHolder, originKeyLength); |
| 984 | } |
| 985 | |
| 986 | // we don't not support override in multi-process mode |
| 987 | // SCOPED_LOCK(m_exclusiveProcessLock); |
| 988 | |
| 989 | #ifndef MMKV_DISABLE_CRYPT |
| 990 | if (m_crypter) { |
| 991 | if (m_metaInfo->m_version >= MMKVVersionRandomIV) { |
| 992 | m_crypter->resetIV(m_metaInfo->m_vector, sizeof(m_metaInfo->m_vector)); |
| 993 | } else { |
| 994 | m_crypter->resetIV(); |
| 995 | } |
| 996 | } |
| 997 | #endif |
| 998 | try { |
| 999 | // write ItemSizeHolder |
| 1000 | m_output->setPosition(0); |
| 1001 | m_output->writeUInt32(AESCrypt::randomItemSizeHolder(ItemSizeHolderSize)); |
| 1002 | m_actualSize = ItemSizeHolderSize; |
| 1003 | #ifndef MMKV_DISABLE_CRYPT |
| 1004 | if (m_crypter) { |
| 1005 | auto ptr = (uint8_t *) m_file->getMemory() + Fixed32Size; |
| 1006 | m_crypter->encrypt(ptr, ptr, m_actualSize); |
| 1007 | if (KeyValueHolderCrypt::isValueStoredAsOffset(valueLength)) { |
| 1008 | m_crypter->getCurStatus(t_status); |
| 1009 | } |
| 1010 | } |
| 1011 | #endif |
| 1012 | if (isKeyEncoded) { |
| 1013 | m_output->writeRawData(keyData); |
| 1014 | } else { |
| 1015 | m_output->writeData(keyData); |
| 1016 | } |
| 1017 | if (isDataHolder) { |
| 1018 | m_output->writeRawVarint32((int32_t) valueLength); |
| 1019 | } |
| 1020 | m_output->writeData(data); // note: write size of data |
nothing calls this directly
no test coverage detected