CodedInputData_OSX.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Tencent is pleased to support the open source community by making
  3. * MMKV available.
  4. *
  5. * Copyright (C) 2019 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 "CodedInputData.h"
  21. #ifdef MMKV_APPLE
  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 *CodedInputData::readString() {
  30. int32_t size = this->readRawVarint32();
  31. if (size < 0) {
  32. throw length_error("InvalidProtocolBuffer negativeSize");
  33. }
  34. auto s_size = static_cast<size_t>(size);
  35. if (s_size <= m_size - m_position) {
  36. auto ptr = m_ptr + m_position;
  37. NSString *result = [[NSString alloc] initWithBytes:ptr length:s_size encoding:NSUTF8StringEncoding];
  38. m_position += s_size;
  39. return [result autorelease];
  40. } else {
  41. throw out_of_range("InvalidProtocolBuffer truncatedMessage");
  42. }
  43. }
  44. NSString *CodedInputData::readString(KeyValueHolder &kvHolder) {
  45. kvHolder.offset = static_cast<uint32_t>(m_position);
  46. int32_t size = this->readRawVarint32();
  47. if (size < 0) {
  48. throw length_error("InvalidProtocolBuffer negativeSize");
  49. }
  50. auto s_size = static_cast<size_t>(size);
  51. if (s_size <= m_size - m_position) {
  52. kvHolder.keySize = static_cast<uint16_t>(s_size);
  53. auto ptr = m_ptr + m_position;
  54. NSString *result = [[NSString alloc] initWithBytes:ptr length:s_size encoding:NSUTF8StringEncoding];
  55. m_position += s_size;
  56. return [result autorelease];
  57. } else {
  58. throw out_of_range("InvalidProtocolBuffer truncatedMessage");
  59. }
  60. }
  61. NSData *CodedInputData::readNSData() {
  62. int32_t size = this->readRawVarint32();
  63. if (size < 0) {
  64. throw length_error("InvalidProtocolBuffer negativeSize");
  65. }
  66. auto s_size = static_cast<size_t>(size);
  67. if (s_size <= m_size - m_position) {
  68. NSData *result = [NSData dataWithBytes:(m_ptr + m_position) length:s_size];
  69. m_position += s_size;
  70. return result;
  71. } else {
  72. throw out_of_range("InvalidProtocolBuffer truncatedMessage");
  73. }
  74. }
  75. } // namespace mmkv
  76. #endif // MMKV_APPLE