CodedInputData.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. #include "CodedInputData.h"
  21. #include "PBUtility.h"
  22. #include <stdexcept>
  23. #ifdef MMKV_APPLE
  24. # if __has_feature(objc_arc)
  25. # error This file must be compiled with MRC. Use -fno-objc-arc flag.
  26. # endif
  27. #endif // MMKV_APPLE
  28. using namespace std;
  29. namespace mmkv {
  30. CodedInputData::CodedInputData(const void *oData, size_t length)
  31. : m_ptr((uint8_t *) oData), m_size(length), m_position(0) {
  32. MMKV_ASSERT(m_ptr);
  33. }
  34. void CodedInputData::seek(size_t addedSize) {
  35. if (m_position + addedSize > m_size) {
  36. throw out_of_range("OutOfSpace");
  37. }
  38. m_position += addedSize;
  39. }
  40. double CodedInputData::readDouble() {
  41. return Int64ToFloat64(this->readRawLittleEndian64());
  42. }
  43. float CodedInputData::readFloat() {
  44. return Int32ToFloat32(this->readRawLittleEndian32());
  45. }
  46. int64_t CodedInputData::readInt64() {
  47. int32_t shift = 0;
  48. int64_t result = 0;
  49. while (shift < 64) {
  50. int8_t b = this->readRawByte();
  51. result |= (int64_t)(b & 0x7f) << shift;
  52. if ((b & 0x80) == 0) {
  53. return result;
  54. }
  55. shift += 7;
  56. }
  57. throw invalid_argument("InvalidProtocolBuffer malformedInt64");
  58. }
  59. uint64_t CodedInputData::readUInt64() {
  60. return static_cast<uint64_t>(readInt64());
  61. }
  62. int32_t CodedInputData::readInt32() {
  63. return this->readRawVarint32();
  64. }
  65. uint32_t CodedInputData::readUInt32() {
  66. return static_cast<uint32_t>(readRawVarint32());
  67. }
  68. bool CodedInputData::readBool() {
  69. return this->readRawVarint32() != 0;
  70. }
  71. #ifndef MMKV_APPLE
  72. string CodedInputData::readString() {
  73. int32_t size = readRawVarint32();
  74. if (size < 0) {
  75. throw length_error("InvalidProtocolBuffer negativeSize");
  76. }
  77. auto s_size = static_cast<size_t>(size);
  78. if (s_size <= m_size - m_position) {
  79. string result((char *) (m_ptr + m_position), s_size);
  80. m_position += s_size;
  81. return result;
  82. } else {
  83. throw out_of_range("InvalidProtocolBuffer truncatedMessage");
  84. }
  85. }
  86. string CodedInputData::readString(KeyValueHolder &kvHolder) {
  87. kvHolder.offset = static_cast<uint32_t>(m_position);
  88. int32_t size = this->readRawVarint32();
  89. if (size < 0) {
  90. throw length_error("InvalidProtocolBuffer negativeSize");
  91. }
  92. auto s_size = static_cast<size_t>(size);
  93. if (s_size <= m_size - m_position) {
  94. kvHolder.keySize = static_cast<uint16_t>(s_size);
  95. auto ptr = m_ptr + m_position;
  96. string result((char *) (m_ptr + m_position), s_size);
  97. m_position += s_size;
  98. return result;
  99. } else {
  100. throw out_of_range("InvalidProtocolBuffer truncatedMessage");
  101. }
  102. }
  103. #endif
  104. MMBuffer CodedInputData::readData() {
  105. int32_t size = this->readRawVarint32();
  106. if (size < 0) {
  107. throw length_error("InvalidProtocolBuffer negativeSize");
  108. }
  109. auto s_size = static_cast<size_t>(size);
  110. if (s_size <= m_size - m_position) {
  111. MMBuffer data(((int8_t *) m_ptr) + m_position, s_size);
  112. m_position += s_size;
  113. return data;
  114. } else {
  115. throw out_of_range("InvalidProtocolBuffer truncatedMessage");
  116. }
  117. }
  118. void CodedInputData::readData(KeyValueHolder &kvHolder) {
  119. int32_t size = this->readRawVarint32();
  120. if (size < 0) {
  121. throw length_error("InvalidProtocolBuffer negativeSize");
  122. }
  123. auto s_size = static_cast<size_t>(size);
  124. if (s_size <= m_size - m_position) {
  125. kvHolder.computedKVSize = static_cast<uint16_t>(m_position - kvHolder.offset);
  126. kvHolder.valueSize = static_cast<uint32_t>(s_size);
  127. m_position += s_size;
  128. } else {
  129. throw out_of_range("InvalidProtocolBuffer truncatedMessage");
  130. }
  131. }
  132. int32_t CodedInputData::readRawVarint32() {
  133. int8_t tmp = this->readRawByte();
  134. if (tmp >= 0) {
  135. return tmp;
  136. }
  137. int32_t result = tmp & 0x7f;
  138. if ((tmp = this->readRawByte()) >= 0) {
  139. result |= tmp << 7;
  140. } else {
  141. result |= (tmp & 0x7f) << 7;
  142. if ((tmp = this->readRawByte()) >= 0) {
  143. result |= tmp << 14;
  144. } else {
  145. result |= (tmp & 0x7f) << 14;
  146. if ((tmp = this->readRawByte()) >= 0) {
  147. result |= tmp << 21;
  148. } else {
  149. result |= (tmp & 0x7f) << 21;
  150. result |= (tmp = this->readRawByte()) << 28;
  151. if (tmp < 0) {
  152. // discard upper 32 bits
  153. for (int i = 0; i < 5; i++) {
  154. if (this->readRawByte() >= 0) {
  155. return result;
  156. }
  157. }
  158. throw invalid_argument("InvalidProtocolBuffer malformed varint32");
  159. }
  160. }
  161. }
  162. }
  163. return result;
  164. }
  165. int32_t CodedInputData::readRawLittleEndian32() {
  166. int8_t b1 = this->readRawByte();
  167. int8_t b2 = this->readRawByte();
  168. int8_t b3 = this->readRawByte();
  169. int8_t b4 = this->readRawByte();
  170. return (((int32_t) b1 & 0xff)) | (((int32_t) b2 & 0xff) << 8) | (((int32_t) b3 & 0xff) << 16) |
  171. (((int32_t) b4 & 0xff) << 24);
  172. }
  173. int64_t CodedInputData::readRawLittleEndian64() {
  174. int8_t b1 = this->readRawByte();
  175. int8_t b2 = this->readRawByte();
  176. int8_t b3 = this->readRawByte();
  177. int8_t b4 = this->readRawByte();
  178. int8_t b5 = this->readRawByte();
  179. int8_t b6 = this->readRawByte();
  180. int8_t b7 = this->readRawByte();
  181. int8_t b8 = this->readRawByte();
  182. return (((int64_t) b1 & 0xff)) | (((int64_t) b2 & 0xff) << 8) | (((int64_t) b3 & 0xff) << 16) |
  183. (((int64_t) b4 & 0xff) << 24) | (((int64_t) b5 & 0xff) << 32) | (((int64_t) b6 & 0xff) << 40) |
  184. (((int64_t) b7 & 0xff) << 48) | (((int64_t) b8 & 0xff) << 56);
  185. }
  186. int8_t CodedInputData::readRawByte() {
  187. if (m_position == m_size) {
  188. auto msg = "reach end, m_position: " + to_string(m_position) + ", m_size: " + to_string(m_size);
  189. throw out_of_range(msg);
  190. }
  191. auto *bytes = (int8_t *) m_ptr;
  192. return bytes[m_position++];
  193. }
  194. #ifdef MMKV_APPLE
  195. #endif // MMKV_APPLE
  196. } // namespace mmkv