CodedInputDataCrypt_OSX.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. #include "CodedInputDataCrypt.h"
  21. #if defined(MMKV_APPLE) && !defined(MMKV_DISABLE_CRYPT)
  22. # include "PBUtility.h"
  23. # include <stdexcept>
  24. # if __has_feature(objc_arc)
  25. # error This file must be compiled with MRC. Use -fno-objc-arc flag.
  26. # endif
  27. using namespace std;
  28. namespace mmkv {
  29. NSString *CodedInputDataCrypt::readString(KeyValueHolderCrypt &kvHolder) {
  30. kvHolder.offset = static_cast<uint32_t>(m_position);
  31. int32_t size = this->readRawVarint32(true);
  32. if (size < 0) {
  33. throw length_error("InvalidProtocolBuffer negativeSize");
  34. }
  35. auto s_size = static_cast<size_t>(size);
  36. if (s_size <= m_size - m_position) {
  37. consumeBytes(s_size);
  38. kvHolder.keySize = static_cast<uint16_t>(s_size);
  39. auto ptr = m_decryptBuffer + m_decryptBufferPosition;
  40. NSString *result = [[NSString alloc] initWithBytes:ptr length:s_size encoding:NSUTF8StringEncoding];
  41. m_position += s_size;
  42. m_decryptBufferPosition += s_size;
  43. return [result autorelease];
  44. } else {
  45. throw out_of_range("InvalidProtocolBuffer truncatedMessage");
  46. }
  47. }
  48. } // namespace mmkv
  49. #endif // MMKV_APPLE && !MMKV_DISABLE_CRYPT