MiniPBCoder.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. #ifndef MMKV_MINIPBCODER_H
  21. #define MMKV_MINIPBCODER_H
  22. #ifdef __cplusplus
  23. #include "MMKVPredef.h"
  24. #include "KeyValueHolder.h"
  25. #include "MMBuffer.h"
  26. #include "MMBuffer.h"
  27. #include "MMKVLog.h"
  28. #include "PBUtility.h"
  29. #include <cstdint>
  30. namespace mmkv {
  31. class CodedInputData;
  32. class CodedOutputData;
  33. class AESCrypt;
  34. class CodedInputDataCrypt;
  35. struct PBEncodeItem;
  36. class MiniPBCoder {
  37. const MMBuffer *m_inputBuffer = nullptr;
  38. CodedInputData *m_inputData = nullptr;
  39. CodedInputDataCrypt *m_inputDataDecrpt = nullptr;
  40. MMBuffer *m_outputBuffer = nullptr;
  41. CodedOutputData *m_outputData = nullptr;
  42. std::vector<PBEncodeItem> *m_encodeItems = nullptr;
  43. MiniPBCoder();
  44. explicit MiniPBCoder(const MMBuffer *inputBuffer, AESCrypt *crypter = nullptr);
  45. ~MiniPBCoder();
  46. void writeRootObject();
  47. size_t prepareObjectForEncode(const MMKVVector &vec);
  48. size_t prepareObjectForEncode(const MMBuffer &buffer);
  49. template <typename T>
  50. MMBuffer getEncodeData(const T &obj) {
  51. size_t index = prepareObjectForEncode(obj);
  52. return writePreparedItems(index);
  53. }
  54. MMBuffer writePreparedItems(size_t index);
  55. void decodeOneMap(MMKVMap &dic, size_t position, bool greedy);
  56. #ifndef MMKV_DISABLE_CRYPT
  57. void decodeOneMap(MMKVMapCrypt &dic, size_t position, bool greedy);
  58. #endif
  59. #ifndef MMKV_APPLE
  60. size_t prepareObjectForEncode(const std::string &str);
  61. size_t prepareObjectForEncode(const std::vector<std::string> &vector);
  62. std::vector<std::string> decodeOneVector();
  63. #else
  64. // NSString, NSData, NSDate
  65. size_t prepareObjectForEncode(__unsafe_unretained NSObject *obj);
  66. #endif
  67. public:
  68. template <typename T>
  69. static MMBuffer encodeDataWithObject(const T &obj) {
  70. try {
  71. MiniPBCoder pbcoder;
  72. return pbcoder.getEncodeData(obj);
  73. } catch (const std::exception &exception) {
  74. MMKVError("%s", exception.what());
  75. return MMBuffer();
  76. }
  77. }
  78. // opt encoding a single MMBuffer
  79. static MMBuffer encodeDataWithObject(const MMBuffer &obj);
  80. // return empty result if there's any error
  81. static void decodeMap(MMKVMap &dic, const MMBuffer &oData, size_t position = 0);
  82. // decode as much data as possible before any error happens
  83. static void greedyDecodeMap(MMKVMap &dic, const MMBuffer &oData, size_t position = 0);
  84. #ifndef MMKV_DISABLE_CRYPT
  85. // return empty result if there's any error
  86. static void decodeMap(MMKVMapCrypt &dic, const MMBuffer &oData, AESCrypt *crypter, size_t position = 0);
  87. // decode as much data as possible before any error happens
  88. static void greedyDecodeMap(MMKVMapCrypt &dic, const MMBuffer &oData, AESCrypt *crypter, size_t position = 0);
  89. #endif // MMKV_DISABLE_CRYPT
  90. #ifndef MMKV_APPLE
  91. static std::vector<std::string> decodeVector(const MMBuffer &oData);
  92. #else
  93. // NSString, NSData, NSDate
  94. static NSObject *decodeObject(const MMBuffer &oData, Class cls);
  95. static bool isCompatibleClass(Class cls);
  96. #endif
  97. // just forbid it for possibly misuse
  98. explicit MiniPBCoder(const MiniPBCoder &other) = delete;
  99. MiniPBCoder &operator=(const MiniPBCoder &other) = delete;
  100. };
  101. } // namespace mmkv
  102. #endif
  103. #endif //MMKV_MINIPBCODER_H