MiniPBCoder.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 "MMBuffer.h"
  25. #include <cstdint>
  26. #ifdef MMKV_HAS_CPP20
  27. # include <span>
  28. # define MMKV_STRING_CONTAINER std::span<const std::string>
  29. #else
  30. # define MMKV_STRING_CONTAINER std::vector<std::string>
  31. #endif
  32. namespace mmkv {
  33. class CodedInputData;
  34. class CodedOutputData;
  35. class AESCrypt;
  36. class CodedInputDataCrypt;
  37. struct PBEncodeItem;
  38. class MMKV_EXPORT MiniPBCoder {
  39. const MMBuffer *m_inputBuffer = nullptr;
  40. CodedInputData *m_inputData = nullptr;
  41. CodedInputDataCrypt *m_inputDataDecrpt = nullptr;
  42. MMBuffer *m_outputBuffer = nullptr;
  43. CodedOutputData *m_outputData = nullptr;
  44. std::vector<PBEncodeItem> *m_encodeItems = nullptr;
  45. MiniPBCoder();
  46. explicit MiniPBCoder(const MMBuffer *inputBuffer, AESCrypt *crypter = nullptr);
  47. ~MiniPBCoder();
  48. void writeRootObject();
  49. size_t prepareObjectForEncode(const MMKVVector &vec);
  50. size_t prepareObjectForEncode(const MMBuffer &buffer);
  51. template <typename T>
  52. MMBuffer getEncodeData(const T &obj) {
  53. size_t index = prepareObjectForEncode(obj);
  54. return writePreparedItems(index);
  55. }
  56. MMBuffer writePreparedItems(size_t index);
  57. void decodeOneMap(MMKVMap &dic, size_t position, bool greedy);
  58. #ifndef MMKV_DISABLE_CRYPT
  59. void decodeOneMap(MMKVMapCrypt &dic, size_t position, bool greedy);
  60. #endif
  61. size_t prepareObjectForEncode(const std::string &str);
  62. size_t prepareObjectForEncode(const MMKV_STRING_CONTAINER &vector);
  63. std::vector<std::string> decodeOneVector();
  64. #ifdef MMKV_HAS_CPP20
  65. size_t prepareObjectForEncode(const std::span<const int32_t> &vec);
  66. size_t prepareObjectForEncode(const std::span<const uint32_t> &vec);
  67. size_t prepareObjectForEncode(const std::span<const int64_t> &vec);
  68. size_t prepareObjectForEncode(const std::span<const uint64_t> &vec);
  69. bool decodeOneVector(std::vector<bool> &result);
  70. bool decodeOneVector(std::vector<int32_t> &result);
  71. bool decodeOneVector(std::vector<uint32_t> &result);
  72. bool decodeOneVector(std::vector<int64_t> &result);
  73. bool decodeOneVector(std::vector<uint64_t> &result);
  74. bool decodeOneVector(std::vector<float> &result);
  75. bool decodeOneVector(std::vector<double> &result);
  76. // special case for fixed size types
  77. MMBuffer getEncodeData(const std::vector<bool> &obj);
  78. MMBuffer getEncodeData(const std::span<const float> &obj);
  79. MMBuffer getEncodeData(const std::span<const double> &obj);
  80. #endif // MMKV_HAS_CPP20
  81. #if defined(MMKV_APPLE) && defined(__OBJC__)
  82. // NSString, NSData, NSDate
  83. size_t prepareObjectForEncode(__unsafe_unretained NSObject *obj);
  84. #endif
  85. public:
  86. template <typename T>
  87. static MMBuffer encodeDataWithObject(const T &obj) {
  88. MiniPBCoder pbCoder;
  89. return pbCoder.getEncodeData(obj);
  90. }
  91. // opt encoding a single MMBuffer
  92. static MMBuffer encodeDataWithObject(const MMBuffer &obj);
  93. // return empty result if there's any error
  94. static void decodeMap(MMKVMap &dic, const MMBuffer &oData, size_t position = 0);
  95. // decode as much data as possible before any error happens
  96. static void greedyDecodeMap(MMKVMap &dic, const MMBuffer &oData, size_t position = 0);
  97. #ifndef MMKV_DISABLE_CRYPT
  98. // return empty result if there's any error
  99. static void decodeMap(MMKVMapCrypt &dic, const MMBuffer &oData, AESCrypt *crypter, size_t position = 0);
  100. // decode as much data as possible before any error happens
  101. static void greedyDecodeMap(MMKVMapCrypt &dic, const MMBuffer &oData, AESCrypt *crypter, size_t position = 0);
  102. #endif // MMKV_DISABLE_CRYPT
  103. static std::vector<std::string> decodeVector(const MMBuffer &oData);
  104. template <typename T>
  105. static bool decodeVector(const MMBuffer &oData, std::vector<T> &result) {
  106. MiniPBCoder oCoder(&oData);
  107. return oCoder.decodeOneVector(result);
  108. }
  109. #if defined(MMKV_APPLE) && defined(__OBJC__)
  110. // NSString, NSData, NSDate
  111. static NSObject *decodeObject(const MMBuffer &oData, Class cls);
  112. static bool isCompatibleClass(Class cls);
  113. #endif
  114. // just forbid it for possibly misuse
  115. explicit MiniPBCoder(const MiniPBCoder &other) = delete;
  116. MiniPBCoder &operator=(const MiniPBCoder &other) = delete;
  117. };
  118. } // namespace mmkv
  119. #endif
  120. #endif //MMKV_MINIPBCODER_H