MiniPBCoder.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. /*
  2. * Tencent is pleased to support the open source community by making
  3. * MMKV available.
  4. *
  5. * Copyright (C) 2018 THL A29 Limited, a Tencent company.
  6. * All rights reserved.
  7. *
  8. * Licensed under the BSD 3-Clause License (the "License"); you may not use
  9. * this file except in compliance with the License. You may obtain a copy of
  10. * the License at
  11. *
  12. * https://opensource.org/licenses/BSD-3-Clause
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. #include "MiniPBCoder.h"
  21. #include "CodedInputData.h"
  22. #include "CodedInputDataCrypt.h"
  23. #include "CodedOutputData.h"
  24. #include "PBEncodeItem.hpp"
  25. #include "PBUtility.h"
  26. #include "MMKVLog.h"
  27. #ifdef MMKV_APPLE
  28. # if __has_feature(objc_arc)
  29. # error This file must be compiled with MRC. Use -fno-objc-arc flag.
  30. # endif
  31. #endif // MMKV_APPLE
  32. using namespace std;
  33. namespace mmkv {
  34. MiniPBCoder::MiniPBCoder() : m_encodeItems(new std::vector<PBEncodeItem>()) {
  35. }
  36. MiniPBCoder::MiniPBCoder(const MMBuffer *inputBuffer, AESCrypt *crypter) : MiniPBCoder() {
  37. m_inputBuffer = inputBuffer;
  38. #ifndef MMKV_DISABLE_CRYPT
  39. if (crypter) {
  40. m_inputDataDecrpt = new CodedInputDataCrypt(m_inputBuffer->getPtr(), m_inputBuffer->length(), *crypter);
  41. } else {
  42. m_inputData = new CodedInputData(m_inputBuffer->getPtr(), m_inputBuffer->length());
  43. }
  44. #else
  45. m_inputData = new CodedInputData(m_inputBuffer->getPtr(), m_inputBuffer->length());
  46. #endif // MMKV_DISABLE_CRYPT
  47. }
  48. MiniPBCoder::~MiniPBCoder() {
  49. delete m_inputData;
  50. #ifndef MMKV_DISABLE_CRYPT
  51. delete m_inputDataDecrpt;
  52. #endif
  53. delete m_outputBuffer;
  54. delete m_outputData;
  55. delete m_encodeItems;
  56. }
  57. // encode
  58. // write object using prepared m_encodeItems[]
  59. void MiniPBCoder::writeRootObject() {
  60. for (size_t index = 0, total = m_encodeItems->size(); index < total; index++) {
  61. PBEncodeItem *encodeItem = &(*m_encodeItems)[index];
  62. switch (encodeItem->type) {
  63. case PBEncodeItemType_Data: {
  64. m_outputData->writeData(*(encodeItem->value.bufferValue));
  65. break;
  66. }
  67. case PBEncodeItemType_Container: {
  68. m_outputData->writeUInt32(encodeItem->valueSize);
  69. break;
  70. }
  71. #ifdef MMKV_HAS_CPP20
  72. case PBEncodeItemType_Int32: {
  73. m_outputData->writeInt32(encodeItem->value.int32Value);
  74. break;
  75. }
  76. case PBEncodeItemType_UInt32: {
  77. m_outputData->writeUInt32(encodeItem->value.uint32Value);
  78. break;
  79. }
  80. case PBEncodeItemType_Int64: {
  81. m_outputData->writeInt64(encodeItem->value.int64Value);
  82. break;
  83. }
  84. case PBEncodeItemType_UInt64: {
  85. m_outputData->writeUInt64(encodeItem->value.uint64Value);
  86. break;
  87. }
  88. #endif // MMKV_HAS_CPP20
  89. case PBEncodeItemType_String: {
  90. m_outputData->writeString(*(encodeItem->value.strValue));
  91. break;
  92. }
  93. #ifdef MMKV_APPLE
  94. case PBEncodeItemType_NSString: {
  95. m_outputData->writeUInt32(encodeItem->valueSize);
  96. if (encodeItem->valueSize > 0 && encodeItem->value.tmpObjectValue != nullptr) {
  97. auto obj = (__bridge NSData *) encodeItem->value.tmpObjectValue;
  98. MMBuffer buffer(obj, MMBufferNoCopy);
  99. m_outputData->writeRawData(buffer);
  100. }
  101. break;
  102. }
  103. case PBEncodeItemType_NSData: {
  104. m_outputData->writeUInt32(encodeItem->valueSize);
  105. if (encodeItem->valueSize > 0 && encodeItem->value.objectValue != nullptr) {
  106. auto obj = (__bridge NSData *) encodeItem->value.objectValue;
  107. MMBuffer buffer(obj, MMBufferNoCopy);
  108. m_outputData->writeRawData(buffer);
  109. }
  110. break;
  111. }
  112. case PBEncodeItemType_NSDate: {
  113. NSDate *oDate = (__bridge NSDate *) encodeItem->value.objectValue;
  114. m_outputData->writeDouble(oDate.timeIntervalSince1970);
  115. break;
  116. }
  117. #endif // MMKV_APPLE
  118. case PBEncodeItemType_None: {
  119. MMKVError("%d", encodeItem->type);
  120. break;
  121. }
  122. }
  123. }
  124. }
  125. size_t MiniPBCoder::prepareObjectForEncode(const MMBuffer &buffer) {
  126. m_encodeItems->push_back(PBEncodeItem());
  127. PBEncodeItem *encodeItem = &(m_encodeItems->back());
  128. size_t index = m_encodeItems->size() - 1;
  129. {
  130. encodeItem->type = PBEncodeItemType_Data;
  131. encodeItem->value.bufferValue = &buffer;
  132. encodeItem->valueSize = static_cast<uint32_t>(buffer.length());
  133. }
  134. encodeItem->compiledSize = pbRawVarint32Size(encodeItem->valueSize) + encodeItem->valueSize;
  135. return index;
  136. }
  137. size_t MiniPBCoder::prepareObjectForEncode(const MMKVVector &vec) {
  138. m_encodeItems->push_back(PBEncodeItem());
  139. PBEncodeItem *encodeItem = &(m_encodeItems->back());
  140. size_t index = m_encodeItems->size() - 1;
  141. {
  142. encodeItem->type = PBEncodeItemType_Container;
  143. encodeItem->value.bufferValue = nullptr;
  144. for (const auto &itr : vec) {
  145. const auto &key = itr.first;
  146. const auto &value = itr.second;
  147. # ifdef MMKV_APPLE
  148. if (key.length <= 0) {
  149. # else
  150. if (key.length() <= 0) {
  151. # endif
  152. continue;
  153. }
  154. size_t keyIndex = prepareObjectForEncode(key);
  155. if (keyIndex < m_encodeItems->size()) {
  156. size_t valueIndex = prepareObjectForEncode(value);
  157. if (valueIndex < m_encodeItems->size()) {
  158. (*m_encodeItems)[index].valueSize += (*m_encodeItems)[keyIndex].compiledSize;
  159. (*m_encodeItems)[index].valueSize += (*m_encodeItems)[valueIndex].compiledSize;
  160. } else {
  161. m_encodeItems->pop_back(); // pop key
  162. }
  163. }
  164. }
  165. encodeItem = &(*m_encodeItems)[index];
  166. }
  167. encodeItem->compiledSize = pbRawVarint32Size(encodeItem->valueSize) + encodeItem->valueSize;
  168. return index;
  169. }
  170. MMBuffer MiniPBCoder::writePreparedItems(size_t index) {
  171. try {
  172. PBEncodeItem *oItem = (index < m_encodeItems->size()) ? &(*m_encodeItems)[index] : nullptr;
  173. if (oItem && oItem->compiledSize > 0) {
  174. m_outputBuffer = new MMBuffer(oItem->compiledSize);
  175. m_outputData = new CodedOutputData(m_outputBuffer->getPtr(), m_outputBuffer->length());
  176. writeRootObject();
  177. }
  178. return std::move(*m_outputBuffer);
  179. } catch (const std::exception &exception) {
  180. MMKVError("%s", exception.what());
  181. return MMBuffer();
  182. } catch (...) {
  183. MMKVError("encode fail");
  184. return MMBuffer();
  185. }
  186. }
  187. MMBuffer MiniPBCoder::encodeDataWithObject(const MMBuffer &obj) {
  188. try {
  189. auto valueSize = static_cast<uint32_t>(obj.length());
  190. auto compiledSize = pbRawVarint32Size(valueSize) + valueSize;
  191. MMBuffer result(compiledSize);
  192. CodedOutputData output(result.getPtr(), result.length());
  193. output.writeData(obj);
  194. return result;
  195. } catch (const std::exception &exception) {
  196. MMKVError("%s", exception.what());
  197. return MMBuffer();
  198. } catch (...) {
  199. MMKVError("prepare encode fail");
  200. return MMBuffer();
  201. }
  202. }
  203. size_t MiniPBCoder::prepareObjectForEncode(const string &str) {
  204. m_encodeItems->push_back(PBEncodeItem());
  205. PBEncodeItem *encodeItem = &(m_encodeItems->back());
  206. size_t index = m_encodeItems->size() - 1;
  207. {
  208. encodeItem->type = PBEncodeItemType_String;
  209. encodeItem->value.strValue = &str;
  210. encodeItem->valueSize = static_cast<uint32_t>(str.size());
  211. }
  212. encodeItem->compiledSize = pbRawVarint32Size(encodeItem->valueSize) + encodeItem->valueSize;
  213. return index;
  214. }
  215. size_t MiniPBCoder::prepareObjectForEncode(const MMKV_STRING_CONTAINER &v) {
  216. m_encodeItems->push_back(PBEncodeItem());
  217. PBEncodeItem *encodeItem = &(m_encodeItems->back());
  218. size_t index = m_encodeItems->size() - 1;
  219. {
  220. encodeItem->type = PBEncodeItemType_Container;
  221. encodeItem->value.bufferValue = nullptr;
  222. for (const auto &str : v) {
  223. size_t itemIndex = prepareObjectForEncode(str);
  224. if (itemIndex < m_encodeItems->size()) {
  225. (*m_encodeItems)[index].valueSize += (*m_encodeItems)[itemIndex].compiledSize;
  226. }
  227. }
  228. encodeItem = &(*m_encodeItems)[index];
  229. }
  230. encodeItem->compiledSize = pbRawVarint32Size(encodeItem->valueSize) + encodeItem->valueSize;
  231. return index;
  232. }
  233. #ifdef MMKV_HAS_CPP20
  234. size_t MiniPBCoder::prepareObjectForEncode(const std::span<const int32_t> &vec) {
  235. m_encodeItems->push_back(PBEncodeItem());
  236. PBEncodeItem *encodeItem = &(m_encodeItems->back());
  237. size_t index = m_encodeItems->size() - 1;
  238. {
  239. encodeItem->type = PBEncodeItemType_Container;
  240. encodeItem->value.bufferValue = nullptr;
  241. for (const auto value : vec) {
  242. PBEncodeItem item;
  243. item.type = PBEncodeItemType_Int32;
  244. item.value.int32Value = value;
  245. item.compiledSize = pbInt32Size(value);
  246. (*m_encodeItems)[index].valueSize += item.compiledSize;
  247. m_encodeItems->push_back(std::move(item));
  248. }
  249. encodeItem = &(*m_encodeItems)[index];
  250. }
  251. encodeItem->compiledSize = pbRawVarint32Size(encodeItem->valueSize) + encodeItem->valueSize;
  252. return index;
  253. }
  254. size_t MiniPBCoder::prepareObjectForEncode(const std::span<const uint32_t> &vec) {
  255. m_encodeItems->push_back(PBEncodeItem());
  256. PBEncodeItem *encodeItem = &(m_encodeItems->back());
  257. size_t index = m_encodeItems->size() - 1;
  258. {
  259. encodeItem->type = PBEncodeItemType_Container;
  260. encodeItem->value.bufferValue = nullptr;
  261. for (const auto value : vec) {
  262. PBEncodeItem item;
  263. item.type = PBEncodeItemType_UInt32;
  264. item.value.uint32Value = value;
  265. item.compiledSize = pbUInt32Size(value);
  266. (*m_encodeItems)[index].valueSize += item.compiledSize;
  267. m_encodeItems->push_back(std::move(item));
  268. }
  269. encodeItem = &(*m_encodeItems)[index];
  270. }
  271. encodeItem->compiledSize = pbRawVarint32Size(encodeItem->valueSize) + encodeItem->valueSize;
  272. return index;
  273. }
  274. size_t MiniPBCoder::prepareObjectForEncode(const std::span<const int64_t> &vec) {
  275. m_encodeItems->push_back(PBEncodeItem());
  276. PBEncodeItem *encodeItem = &(m_encodeItems->back());
  277. size_t index = m_encodeItems->size() - 1;
  278. {
  279. encodeItem->type = PBEncodeItemType_Container;
  280. encodeItem->value.bufferValue = nullptr;
  281. for (const auto value : vec) {
  282. PBEncodeItem item;
  283. item.type = PBEncodeItemType_Int64;
  284. item.value.int64Value = value;
  285. item.compiledSize = pbInt64Size(value);
  286. (*m_encodeItems)[index].valueSize += item.compiledSize;
  287. m_encodeItems->push_back(std::move(item));
  288. }
  289. encodeItem = &(*m_encodeItems)[index];
  290. }
  291. encodeItem->compiledSize = pbRawVarint32Size(encodeItem->valueSize) + encodeItem->valueSize;
  292. return index;
  293. }
  294. size_t MiniPBCoder::prepareObjectForEncode(const std::span<const uint64_t> &vec) {
  295. m_encodeItems->push_back(PBEncodeItem());
  296. PBEncodeItem *encodeItem = &(m_encodeItems->back());
  297. size_t index = m_encodeItems->size() - 1;
  298. {
  299. encodeItem->type = PBEncodeItemType_Container;
  300. encodeItem->value.bufferValue = nullptr;
  301. for (const auto value : vec) {
  302. PBEncodeItem item;
  303. item.type = PBEncodeItemType_UInt64;
  304. item.value.uint64Value = value;
  305. item.compiledSize = pbUInt64Size(value);
  306. (*m_encodeItems)[index].valueSize += item.compiledSize;
  307. m_encodeItems->push_back(std::move(item));
  308. }
  309. encodeItem = &(*m_encodeItems)[index];
  310. }
  311. encodeItem->compiledSize = pbRawVarint32Size(encodeItem->valueSize) + encodeItem->valueSize;
  312. return index;
  313. }
  314. #endif // MMKV_HAS_CPP20
  315. vector<string> MiniPBCoder::decodeOneVector() {
  316. vector<string> v;
  317. m_inputData->readInt32();
  318. while (!m_inputData->isAtEnd()) {
  319. auto value = m_inputData->readString();
  320. v.push_back(std::move(value));
  321. }
  322. return v;
  323. }
  324. #ifdef MMKV_HAS_CPP20
  325. bool MiniPBCoder::decodeOneVector(std::vector<bool> &result) {
  326. try {
  327. auto size = m_inputData->readUInt32();
  328. result.reserve(size / pbBoolSize());
  329. while (!m_inputData->isAtEnd()) {
  330. auto value = m_inputData->readBool();
  331. result.push_back(value);
  332. }
  333. return true;
  334. } catch (std::exception &exception) {
  335. MMKVError("%s", exception.what());
  336. } catch (...) {
  337. MMKVError("decode fail");
  338. }
  339. return false;
  340. }
  341. bool MiniPBCoder::decodeOneVector(std::vector<int32_t> &result) {
  342. try {
  343. m_inputData->readInt32();
  344. while (!m_inputData->isAtEnd()) {
  345. auto value = m_inputData->readInt32();
  346. result.push_back(value);
  347. }
  348. return true;
  349. } catch (std::exception &exception) {
  350. MMKVError("%s", exception.what());
  351. } catch (...) {
  352. MMKVError("decode fail");
  353. }
  354. return false;
  355. }
  356. bool MiniPBCoder::decodeOneVector(std::vector<uint32_t> &result) {
  357. try {
  358. m_inputData->readInt32();
  359. while (!m_inputData->isAtEnd()) {
  360. auto value = m_inputData->readUInt32();
  361. result.push_back(value);
  362. }
  363. return true;
  364. } catch (std::exception &exception) {
  365. MMKVError("%s", exception.what());
  366. } catch (...) {
  367. MMKVError("decode fail");
  368. }
  369. return false;
  370. }
  371. bool MiniPBCoder::decodeOneVector(std::vector<int64_t> &result) {
  372. try {
  373. m_inputData->readInt32();
  374. while (!m_inputData->isAtEnd()) {
  375. auto value = m_inputData->readInt64();
  376. result.push_back(value);
  377. }
  378. return true;
  379. } catch (std::exception &exception) {
  380. MMKVError("%s", exception.what());
  381. } catch (...) {
  382. MMKVError("decode fail");
  383. }
  384. return false;
  385. }
  386. bool MiniPBCoder::decodeOneVector(std::vector<uint64_t> &result) {
  387. try {
  388. m_inputData->readInt32();
  389. while (!m_inputData->isAtEnd()) {
  390. auto value = m_inputData->readUInt64();
  391. result.push_back(value);
  392. }
  393. return true;
  394. } catch (std::exception &exception) {
  395. MMKVError("%s", exception.what());
  396. } catch (...) {
  397. MMKVError("decode fail");
  398. }
  399. return false;
  400. }
  401. bool MiniPBCoder::decodeOneVector(std::vector<float> &result) {
  402. try {
  403. auto size = m_inputData->readUInt32();
  404. result.reserve(size / pbFloatSize());
  405. while (!m_inputData->isAtEnd()) {
  406. auto value = m_inputData->readFloat();
  407. result.push_back(value);
  408. }
  409. return true;
  410. } catch (std::exception &exception) {
  411. MMKVError("%s", exception.what());
  412. } catch (...) {
  413. MMKVError("decode fail");
  414. }
  415. return false;
  416. }
  417. bool MiniPBCoder::decodeOneVector(std::vector<double> &result) {
  418. try {
  419. auto size = m_inputData->readUInt32();
  420. result.reserve(size / pbDoubleSize());
  421. while (!m_inputData->isAtEnd()) {
  422. auto value = m_inputData->readDouble();
  423. result.push_back(value);
  424. }
  425. return true;
  426. } catch (std::exception &exception) {
  427. MMKVError("%s", exception.what());
  428. } catch (...) {
  429. MMKVError("decode fail");
  430. }
  431. return false;
  432. }
  433. #endif // MMKV_HAS_CPP20
  434. #ifndef MMKV_APPLE
  435. void MiniPBCoder::decodeOneMap(MMKVMap &dic, size_t position, bool greedy) {
  436. auto block = [position, this](MMKVMap &dictionary) {
  437. if (position) {
  438. m_inputData->seek(position);
  439. } else {
  440. m_inputData->readInt32();
  441. }
  442. while (!m_inputData->isAtEnd()) {
  443. KeyValueHolder kvHolder;
  444. const auto &key = m_inputData->readString(kvHolder);
  445. if (key.length() > 0) {
  446. m_inputData->readData(kvHolder);
  447. if (kvHolder.valueSize > 0) {
  448. dictionary[key] = std::move(kvHolder);
  449. } else {
  450. auto itr = dictionary.find(key);
  451. if (itr != dictionary.end()) {
  452. dictionary.erase(itr);
  453. }
  454. }
  455. }
  456. }
  457. };
  458. if (greedy) {
  459. try {
  460. block(dic);
  461. } catch (std::exception &exception) {
  462. MMKVError("%s", exception.what());
  463. } catch (...) {
  464. MMKVError("prepare encode fail");
  465. }
  466. } else {
  467. try {
  468. MMKVMap tmpDic;
  469. block(tmpDic);
  470. dic.swap(tmpDic);
  471. } catch (std::exception &exception) {
  472. MMKVError("%s", exception.what());
  473. } catch (...) {
  474. MMKVError("prepare encode fail");
  475. }
  476. }
  477. }
  478. # ifndef MMKV_DISABLE_CRYPT
  479. void MiniPBCoder::decodeOneMap(MMKVMapCrypt &dic, size_t position, bool greedy) {
  480. auto block = [position, this](MMKVMapCrypt &dictionary) {
  481. if (position) {
  482. m_inputDataDecrpt->seek(position);
  483. } else {
  484. m_inputDataDecrpt->readInt32();
  485. }
  486. while (!m_inputDataDecrpt->isAtEnd()) {
  487. KeyValueHolderCrypt kvHolder;
  488. const auto &key = m_inputDataDecrpt->readString(kvHolder);
  489. if (key.length() > 0) {
  490. m_inputDataDecrpt->readData(kvHolder);
  491. if (kvHolder.realValueSize() > 0) {
  492. dictionary[key] = std::move(kvHolder);
  493. } else {
  494. auto itr = dictionary.find(key);
  495. if (itr != dictionary.end()) {
  496. dictionary.erase(itr);
  497. }
  498. }
  499. }
  500. }
  501. };
  502. if (greedy) {
  503. try {
  504. block(dic);
  505. } catch (std::exception &exception) {
  506. MMKVError("%s", exception.what());
  507. } catch (...) {
  508. MMKVError("prepare encode fail");
  509. }
  510. } else {
  511. try {
  512. MMKVMapCrypt tmpDic;
  513. block(tmpDic);
  514. dic.swap(tmpDic);
  515. } catch (std::exception &exception) {
  516. MMKVError("%s", exception.what());
  517. } catch (...) {
  518. MMKVError("prepare encode fail");
  519. }
  520. }
  521. }
  522. #endif // !MMKV_APPLE
  523. # endif // MMKV_DISABLE_CRYPT
  524. vector<string> MiniPBCoder::decodeVector(const MMBuffer &oData) {
  525. MiniPBCoder oCoder(&oData);
  526. return oCoder.decodeOneVector();
  527. }
  528. #ifdef MMKV_HAS_CPP20
  529. MMBuffer MiniPBCoder::getEncodeData(const std::vector<bool> &value) {
  530. auto valueLength = static_cast<uint32_t>(value.size() * pbBoolSize());
  531. auto size = pbRawVarint32Size(valueLength) + valueLength;
  532. auto buffer = MMBuffer(size);
  533. CodedOutputData output(buffer.getPtr(), size);
  534. output.writeUInt32(valueLength);
  535. for (auto single : value) {
  536. output.writeBool(single);
  537. }
  538. return buffer;
  539. }
  540. MMBuffer MiniPBCoder::getEncodeData(const std::span<const float> &value) {
  541. auto valueLength = static_cast<uint32_t>(value.size() * pbFloatSize());
  542. auto size = pbRawVarint32Size(valueLength) + valueLength;
  543. auto buffer = MMBuffer(size);
  544. CodedOutputData output(buffer.getPtr(), size);
  545. output.writeUInt32(valueLength);
  546. for (auto single : value) {
  547. output.writeFloat(single);
  548. }
  549. return buffer;
  550. }
  551. MMBuffer MiniPBCoder::getEncodeData(const std::span<const double> &value) {
  552. auto valueLength = static_cast<uint32_t>(value.size() * pbDoubleSize());
  553. auto size = pbRawVarint32Size(valueLength) + valueLength;
  554. auto buffer = MMBuffer(size);
  555. CodedOutputData output(buffer.getPtr(), size);
  556. output.writeUInt32(valueLength);
  557. for (auto single : value) {
  558. output.writeDouble(single);
  559. }
  560. return buffer;
  561. }
  562. #endif // MMKV_HAS_CPP20
  563. void MiniPBCoder::decodeMap(MMKVMap &dic, const MMBuffer &oData, size_t position) {
  564. MiniPBCoder oCoder(&oData);
  565. oCoder.decodeOneMap(dic, position, false);
  566. }
  567. void MiniPBCoder::greedyDecodeMap(MMKVMap &dic, const MMBuffer &oData, size_t position) {
  568. MiniPBCoder oCoder(&oData);
  569. oCoder.decodeOneMap(dic, position, true);
  570. }
  571. #ifndef MMKV_DISABLE_CRYPT
  572. void MiniPBCoder::decodeMap(MMKVMapCrypt &dic, const MMBuffer &oData, AESCrypt *crypter, size_t position) {
  573. MiniPBCoder oCoder(&oData, crypter);
  574. oCoder.decodeOneMap(dic, position, false);
  575. }
  576. void MiniPBCoder::greedyDecodeMap(MMKVMapCrypt &dic, const MMBuffer &oData, AESCrypt *crypter, size_t position) {
  577. MiniPBCoder oCoder(&oData, crypter);
  578. oCoder.decodeOneMap(dic, position, true);
  579. }
  580. #endif
  581. } // namespace mmkv