CodedInputData.cpp 7.4 KB

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