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