CodedInputDataCrypt.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Tencent is pleased to support the open source community by making
  3. * MMKV available.
  4. *
  5. * Copyright (C) 2020 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 CodedInputDataCrypt_h
  21. #define CodedInputDataCrypt_h
  22. #ifdef __cplusplus
  23. #include "MMKVPredef.h"
  24. #include "KeyValueHolder.h"
  25. #include "MMBuffer.h"
  26. #include "aes/AESCrypt.h"
  27. #include <cstdint>
  28. #ifdef MMKV_DISABLE_CRYPT
  29. namespace mmkv {
  30. class CodedInputDataCrypt;
  31. }
  32. #else
  33. namespace mmkv {
  34. class CodedInputDataCrypt {
  35. uint8_t *const m_ptr;
  36. size_t m_size;
  37. size_t m_position;
  38. size_t m_decryptPosition; // position of text that has beed decrypted
  39. AESCrypt &m_decrypter;
  40. uint8_t *m_decryptBuffer; // internal decrypt buffer, grows by (n * AES_KEY_LEN) bytes
  41. size_t m_decryptBufferSize;
  42. size_t m_decryptBufferPosition; // reader position in the buffer, synced with m_position
  43. size_t m_decryptBufferDecryptLength; // length of the buffer that has been used
  44. size_t m_decryptBufferDiscardPosition; // recycle position, any data before that can be discarded
  45. void consumeBytes(size_t length, bool discardPreData = false);
  46. void skipBytes(size_t length);
  47. void statusBeforeDecrypt(size_t rollbackSize, AESCryptStatus &status);
  48. int8_t readRawByte();
  49. int32_t readRawVarint32(bool discardPreData = false);
  50. public:
  51. CodedInputDataCrypt(const void *oData, size_t length, AESCrypt &crypt);
  52. ~CodedInputDataCrypt();
  53. bool isAtEnd() { return m_position == m_size; };
  54. void seek(size_t addedSize);
  55. int32_t readInt32();
  56. void readData(KeyValueHolderCrypt &kvHolder);
  57. #ifndef MMKV_APPLE
  58. std::string readString(KeyValueHolderCrypt &kvHolder);
  59. #else
  60. NSString *readString(KeyValueHolderCrypt &kvHolder);
  61. #endif
  62. };
  63. } // namespace mmkv
  64. #endif // MMKV_DISABLE_CRYPT
  65. #endif // __cplusplus
  66. #endif /* CodedInputDataCrypt_h */