| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 19 | #ifndef _THRIFT_PROTOCOL_TCOMPACTPROTOCOL_TCC_ |
| 20 | #define _THRIFT_PROTOCOL_TCOMPACTPROTOCOL_TCC_ 1 |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 21 | |
| Bryan Duxbury | 141eab4 | 2009-04-03 15:05:28 +0000 | [diff] [blame] | 22 | #include <limits> |
| Mario Emmenlauer | 04aabcb | 2018-07-05 14:09:04 +0200 | [diff] [blame] | 23 | #include <cstdlib> |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 24 | |
| Antonio Di Monaco | 796667b | 2016-01-04 23:05:19 +0100 | [diff] [blame] | 25 | #include "thrift/config.h" |
| 26 | |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 27 | /* |
| 28 | * TCompactProtocol::i*ToZigzag depend on the fact that the right shift |
| 29 | * operator on a signed integer is an arithmetic (sign-extending) shift. |
| 30 | * If this is not the case, the current implementation will not work. |
| 31 | * If anyone encounters this error, we can try to figure out the best |
| 32 | * way to implement an arithmetic right shift on their platform. |
| 33 | */ |
| 34 | #if !defined(SIGNED_RIGHT_SHIFT_IS) || !defined(ARITHMETIC_RIGHT_SHIFT) |
| 35 | # error "Unable to determine the behavior of a signed right shift" |
| 36 | #endif |
| 37 | #if SIGNED_RIGHT_SHIFT_IS != ARITHMETIC_RIGHT_SHIFT |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 38 | # error "TCompactProtocol currently only works if a signed right shift is arithmetic" |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 39 | #endif |
| 40 | |
| 41 | #ifdef __GNUC__ |
| 42 | #define UNLIKELY(val) (__builtin_expect((val), 0)) |
| 43 | #else |
| 44 | #define UNLIKELY(val) (val) |
| 45 | #endif |
| 46 | |
| 47 | namespace apache { namespace thrift { namespace protocol { |
| 48 | |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 49 | namespace detail { namespace compact { |
| 50 | |
| 51 | enum Types { |
| 52 | CT_STOP = 0x00, |
| 53 | CT_BOOLEAN_TRUE = 0x01, |
| 54 | CT_BOOLEAN_FALSE = 0x02, |
| 55 | CT_BYTE = 0x03, |
| 56 | CT_I16 = 0x04, |
| 57 | CT_I32 = 0x05, |
| 58 | CT_I64 = 0x06, |
| 59 | CT_DOUBLE = 0x07, |
| 60 | CT_BINARY = 0x08, |
| 61 | CT_LIST = 0x09, |
| 62 | CT_SET = 0x0A, |
| 63 | CT_MAP = 0x0B, |
| Carel Combrink | 786764b | 2025-05-15 12:22:37 +0000 | [diff] [blame] | 64 | CT_STRUCT = 0x0C, |
| 65 | CT_UUID = 0x0D |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 66 | }; |
| 67 | |
| Carel Combrink | 786764b | 2025-05-15 12:22:37 +0000 | [diff] [blame] | 68 | const int8_t TTypeToCType[17] = { |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 69 | CT_STOP, // T_STOP |
| 70 | 0, // unused |
| 71 | CT_BOOLEAN_TRUE, // T_BOOL |
| 72 | CT_BYTE, // T_BYTE |
| 73 | CT_DOUBLE, // T_DOUBLE |
| 74 | 0, // unused |
| 75 | CT_I16, // T_I16 |
| 76 | 0, // unused |
| 77 | CT_I32, // T_I32 |
| 78 | 0, // unused |
| 79 | CT_I64, // T_I64 |
| 80 | CT_BINARY, // T_STRING |
| 81 | CT_STRUCT, // T_STRUCT |
| 82 | CT_MAP, // T_MAP |
| 83 | CT_SET, // T_SET |
| 84 | CT_LIST, // T_LIST |
| Carel Combrink | 786764b | 2025-05-15 12:22:37 +0000 | [diff] [blame] | 85 | CT_UUID, // T_UUID |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | }} // end detail::compact namespace |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 89 | |
| 90 | |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 91 | template <class Transport_> |
| 92 | uint32_t TCompactProtocolT<Transport_>::writeMessageBegin( |
| 93 | const std::string& name, |
| 94 | const TMessageType messageType, |
| 95 | const int32_t seqid) { |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 96 | uint32_t wsize = 0; |
| 97 | wsize += writeByte(PROTOCOL_ID); |
| Maximilian Bandle | 7c94335 | 2025-02-07 10:58:32 +0100 | [diff] [blame] | 98 | wsize += writeByte((VERSION_N & VERSION_MASK) | ((static_cast<int32_t>(messageType) << TYPE_SHIFT_AMOUNT) & TYPE_MASK)); |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 99 | wsize += writeVarint32(seqid); |
| 100 | wsize += writeString(name); |
| 101 | return wsize; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Write a field header containing the field id and field type. If the |
| 106 | * difference between the current field id and the last one is small (< 15), |
| 107 | * then the field id will be encoded in the 4 MSB as a delta. Otherwise, the |
| 108 | * field id will follow the type header as a zigzag varint. |
| 109 | */ |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 110 | template <class Transport_> |
| 111 | uint32_t TCompactProtocolT<Transport_>::writeFieldBegin(const char* name, |
| 112 | const TType fieldType, |
| 113 | const int16_t fieldId) { |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 114 | if (fieldType == T_BOOL) { |
| 115 | booleanField_.name = name; |
| 116 | booleanField_.fieldType = fieldType; |
| 117 | booleanField_.fieldId = fieldId; |
| 118 | } else { |
| 119 | return writeFieldBeginInternal(name, fieldType, fieldId, -1); |
| 120 | } |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Write the STOP symbol so we know there are no more fields in this struct. |
| 126 | */ |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 127 | template <class Transport_> |
| 128 | uint32_t TCompactProtocolT<Transport_>::writeFieldStop() { |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 129 | return writeByte(T_STOP); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Write a struct begin. This doesn't actually put anything on the wire. We |
| 134 | * use it as an opportunity to put special placeholder markers on the field |
| 135 | * stack so we can get the field id deltas correct. |
| 136 | */ |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 137 | template <class Transport_> |
| 138 | uint32_t TCompactProtocolT<Transport_>::writeStructBegin(const char* name) { |
| Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 139 | (void) name; |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 140 | lastField_.push(lastFieldId_); |
| 141 | lastFieldId_ = 0; |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Write a struct end. This doesn't actually put anything on the wire. We use |
| 147 | * this as an opportunity to pop the last field from the current struct off |
| 148 | * of the field stack. |
| 149 | */ |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 150 | template <class Transport_> |
| 151 | uint32_t TCompactProtocolT<Transport_>::writeStructEnd() { |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 152 | lastFieldId_ = lastField_.top(); |
| 153 | lastField_.pop(); |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Write a List header. |
| 159 | */ |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 160 | template <class Transport_> |
| 161 | uint32_t TCompactProtocolT<Transport_>::writeListBegin(const TType elemType, |
| 162 | const uint32_t size) { |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 163 | return writeCollectionBegin(elemType, size); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Write a set header. |
| 168 | */ |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 169 | template <class Transport_> |
| 170 | uint32_t TCompactProtocolT<Transport_>::writeSetBegin(const TType elemType, |
| 171 | const uint32_t size) { |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 172 | return writeCollectionBegin(elemType, size); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Write a map header. If the map is empty, omit the key and value type |
| 177 | * headers, as we don't need any additional information to skip it. |
| 178 | */ |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 179 | template <class Transport_> |
| 180 | uint32_t TCompactProtocolT<Transport_>::writeMapBegin(const TType keyType, |
| 181 | const TType valType, |
| 182 | const uint32_t size) { |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 183 | uint32_t wsize = 0; |
| 184 | |
| 185 | if (size == 0) { |
| 186 | wsize += writeByte(0); |
| 187 | } else { |
| 188 | wsize += writeVarint32(size); |
| 189 | wsize += writeByte(getCompactType(keyType) << 4 | getCompactType(valType)); |
| 190 | } |
| 191 | return wsize; |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Write a boolean value. Potentially, this could be a boolean field, in |
| 196 | * which case the field header info isn't written yet. If so, decide what the |
| 197 | * right type header is for the value and then write the field header. |
| 198 | * Otherwise, write a single byte. |
| 199 | */ |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 200 | template <class Transport_> |
| 201 | uint32_t TCompactProtocolT<Transport_>::writeBool(const bool value) { |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 202 | uint32_t wsize = 0; |
| 203 | |
| Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 204 | if (booleanField_.name != nullptr) { |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 205 | // we haven't written the field header yet |
| Roger Meier | 64a799d | 2013-06-04 20:59:01 +0200 | [diff] [blame] | 206 | wsize |
| 207 | += writeFieldBeginInternal(booleanField_.name, |
| 208 | booleanField_.fieldType, |
| 209 | booleanField_.fieldId, |
| 210 | static_cast<int8_t>(value |
| 211 | ? detail::compact::CT_BOOLEAN_TRUE |
| 212 | : detail::compact::CT_BOOLEAN_FALSE)); |
| Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 213 | booleanField_.name = nullptr; |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 214 | } else { |
| 215 | // we're not part of a field, so just write the value |
| Roger Meier | 64a799d | 2013-06-04 20:59:01 +0200 | [diff] [blame] | 216 | wsize |
| 217 | += writeByte(static_cast<int8_t>(value |
| 218 | ? detail::compact::CT_BOOLEAN_TRUE |
| 219 | : detail::compact::CT_BOOLEAN_FALSE)); |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 220 | } |
| 221 | return wsize; |
| 222 | } |
| 223 | |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 224 | template <class Transport_> |
| 225 | uint32_t TCompactProtocolT<Transport_>::writeByte(const int8_t byte) { |
| Maximilian Bandle | 7c94335 | 2025-02-07 10:58:32 +0100 | [diff] [blame] | 226 | trans_->write(reinterpret_cast<const uint8_t*>(&byte), 1); |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 227 | return 1; |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Write an i16 as a zigzag varint. |
| 232 | */ |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 233 | template <class Transport_> |
| 234 | uint32_t TCompactProtocolT<Transport_>::writeI16(const int16_t i16) { |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 235 | return writeVarint32(i32ToZigzag(i16)); |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Write an i32 as a zigzag varint. |
| 240 | */ |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 241 | template <class Transport_> |
| 242 | uint32_t TCompactProtocolT<Transport_>::writeI32(const int32_t i32) { |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 243 | return writeVarint32(i32ToZigzag(i32)); |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Write an i64 as a zigzag varint. |
| 248 | */ |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 249 | template <class Transport_> |
| 250 | uint32_t TCompactProtocolT<Transport_>::writeI64(const int64_t i64) { |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 251 | return writeVarint64(i64ToZigzag(i64)); |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Write a double to the wire as 8 bytes. |
| 256 | */ |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 257 | template <class Transport_> |
| 258 | uint32_t TCompactProtocolT<Transport_>::writeDouble(const double dub) { |
| cyy | 863262d | 2019-01-06 10:40:58 +0800 | [diff] [blame] | 259 | static_assert(sizeof(double) == sizeof(uint64_t), "sizeof(double) == sizeof(uint64_t)"); |
| 260 | static_assert(std::numeric_limits<double>::is_iec559, "std::numeric_limits<double>::is_iec559"); |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 261 | |
| Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 262 | auto bits = bitwise_cast<uint64_t>(dub); |
| jfarrell | ad3a955 | 2015-09-24 23:27:34 -0400 | [diff] [blame] | 263 | bits = THRIFT_htolell(bits); |
| Maximilian Bandle | 7c94335 | 2025-02-07 10:58:32 +0100 | [diff] [blame] | 264 | trans_->write(reinterpret_cast<const uint8_t*>(&bits), 8); |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 265 | return 8; |
| 266 | } |
| 267 | |
| 268 | /** |
| Konrad Grochowski | 3b5dacb | 2014-11-24 10:55:31 +0100 | [diff] [blame] | 269 | * Write a string to the wire with a varint size preceding. |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 270 | */ |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 271 | template <class Transport_> |
| 272 | uint32_t TCompactProtocolT<Transport_>::writeString(const std::string& str) { |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 273 | return writeBinary(str); |
| 274 | } |
| 275 | |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 276 | template <class Transport_> |
| 277 | uint32_t TCompactProtocolT<Transport_>::writeBinary(const std::string& str) { |
| Ben Craig | 7f10de7 | 2013-10-14 20:27:18 -0500 | [diff] [blame] | 278 | if(str.size() > (std::numeric_limits<uint32_t>::max)()) |
| 279 | throw TProtocolException(TProtocolException::SIZE_LIMIT); |
| Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 280 | auto ssize = static_cast<uint32_t>(str.size()); |
| Ben Craig | 7f10de7 | 2013-10-14 20:27:18 -0500 | [diff] [blame] | 281 | uint32_t wsize = writeVarint32(ssize) ; |
| 282 | // checking ssize + wsize > uint_max, but we don't want to overflow while checking for overflows. |
| 283 | // transforming the check to ssize > uint_max - wsize |
| 284 | if(ssize > (std::numeric_limits<uint32_t>::max)() - wsize) |
| 285 | throw TProtocolException(TProtocolException::SIZE_LIMIT); |
| 286 | wsize += ssize; |
| Maximilian Bandle | 7c94335 | 2025-02-07 10:58:32 +0100 | [diff] [blame] | 287 | trans_->write(reinterpret_cast<const uint8_t*>(str.data()), ssize); |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 288 | return wsize; |
| 289 | } |
| 290 | |
| Carel Combrink | 786764b | 2025-05-15 12:22:37 +0000 | [diff] [blame] | 291 | /** |
| 292 | * Write a TUuid to the wire |
| 293 | */ |
| 294 | template <class Transport_> |
| 295 | uint32_t TCompactProtocolT<Transport_>::writeUUID(const TUuid& uuid) { |
| 296 | trans_->write(uuid.data(), uuid.size()); |
| 297 | return uuid.size(); |
| 298 | } |
| 299 | |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 300 | // |
| 301 | // Internal Writing methods |
| 302 | // |
| 303 | |
| 304 | /** |
| 305 | * The workhorse of writeFieldBegin. It has the option of doing a |
| 306 | * 'type override' of the type header. This is used specifically in the |
| 307 | * boolean field case. |
| 308 | */ |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 309 | template <class Transport_> |
| 310 | int32_t TCompactProtocolT<Transport_>::writeFieldBeginInternal( |
| 311 | const char* name, |
| 312 | const TType fieldType, |
| 313 | const int16_t fieldId, |
| 314 | int8_t typeOverride) { |
| Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 315 | (void) name; |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 316 | uint32_t wsize = 0; |
| 317 | |
| 318 | // if there's a type override, use that. |
| 319 | int8_t typeToWrite = (typeOverride == -1 ? getCompactType(fieldType) : typeOverride); |
| 320 | |
| 321 | // check if we can use delta encoding for the field id |
| 322 | if (fieldId > lastFieldId_ && fieldId - lastFieldId_ <= 15) { |
| 323 | // write them together |
| Roger Meier | 64a799d | 2013-06-04 20:59:01 +0200 | [diff] [blame] | 324 | wsize += writeByte(static_cast<int8_t>((fieldId - lastFieldId_) |
| 325 | << 4 | typeToWrite)); |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 326 | } else { |
| 327 | // write them separate |
| 328 | wsize += writeByte(typeToWrite); |
| 329 | wsize += writeI16(fieldId); |
| 330 | } |
| 331 | |
| 332 | lastFieldId_ = fieldId; |
| 333 | return wsize; |
| 334 | } |
| 335 | |
| 336 | /** |
| 337 | * Abstract method for writing the start of lists and sets. List and sets on |
| 338 | * the wire differ only by the type indicator. |
| 339 | */ |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 340 | template <class Transport_> |
| Roger Meier | 64a799d | 2013-06-04 20:59:01 +0200 | [diff] [blame] | 341 | uint32_t TCompactProtocolT<Transport_>::writeCollectionBegin(const TType elemType, |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 342 | int32_t size) { |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 343 | uint32_t wsize = 0; |
| 344 | if (size <= 14) { |
| Roger Meier | 64a799d | 2013-06-04 20:59:01 +0200 | [diff] [blame] | 345 | wsize += writeByte(static_cast<int8_t>(size |
| 346 | << 4 | getCompactType(elemType))); |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 347 | } else { |
| 348 | wsize += writeByte(0xf0 | getCompactType(elemType)); |
| 349 | wsize += writeVarint32(size); |
| 350 | } |
| 351 | return wsize; |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * Write an i32 as a varint. Results in 1-5 bytes on the wire. |
| 356 | */ |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 357 | template <class Transport_> |
| 358 | uint32_t TCompactProtocolT<Transport_>::writeVarint32(uint32_t n) { |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 359 | uint8_t buf[5]; |
| 360 | uint32_t wsize = 0; |
| 361 | |
| 362 | while (true) { |
| 363 | if ((n & ~0x7F) == 0) { |
| Maximilian Bandle | 7c94335 | 2025-02-07 10:58:32 +0100 | [diff] [blame] | 364 | buf[wsize++] = static_cast<int8_t>(n); |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 365 | break; |
| 366 | } else { |
| Maximilian Bandle | 7c94335 | 2025-02-07 10:58:32 +0100 | [diff] [blame] | 367 | buf[wsize++] = static_cast<int8_t>((n & 0x7F) | 0x80); |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 368 | n >>= 7; |
| 369 | } |
| 370 | } |
| 371 | trans_->write(buf, wsize); |
| 372 | return wsize; |
| 373 | } |
| 374 | |
| 375 | /** |
| 376 | * Write an i64 as a varint. Results in 1-10 bytes on the wire. |
| 377 | */ |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 378 | template <class Transport_> |
| 379 | uint32_t TCompactProtocolT<Transport_>::writeVarint64(uint64_t n) { |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 380 | uint8_t buf[10]; |
| 381 | uint32_t wsize = 0; |
| 382 | |
| 383 | while (true) { |
| 384 | if ((n & ~0x7FL) == 0) { |
| Maximilian Bandle | 7c94335 | 2025-02-07 10:58:32 +0100 | [diff] [blame] | 385 | buf[wsize++] = static_cast<int8_t>(n); |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 386 | break; |
| 387 | } else { |
| Maximilian Bandle | 7c94335 | 2025-02-07 10:58:32 +0100 | [diff] [blame] | 388 | buf[wsize++] = static_cast<int8_t>((n & 0x7F) | 0x80); |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 389 | n >>= 7; |
| 390 | } |
| 391 | } |
| 392 | trans_->write(buf, wsize); |
| 393 | return wsize; |
| 394 | } |
| 395 | |
| 396 | /** |
| 397 | * Convert l into a zigzag long. This allows negative numbers to be |
| 398 | * represented compactly as a varint. |
| 399 | */ |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 400 | template <class Transport_> |
| 401 | uint64_t TCompactProtocolT<Transport_>::i64ToZigzag(const int64_t l) { |
| Jim Apple | 147c284 | 2017-03-18 12:56:50 -0700 | [diff] [blame] | 402 | return (static_cast<uint64_t>(l) << 1) ^ (l >> 63); |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | /** |
| 406 | * Convert n into a zigzag int. This allows negative numbers to be |
| 407 | * represented compactly as a varint. |
| 408 | */ |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 409 | template <class Transport_> |
| 410 | uint32_t TCompactProtocolT<Transport_>::i32ToZigzag(const int32_t n) { |
| Jim Apple | 147c284 | 2017-03-18 12:56:50 -0700 | [diff] [blame] | 411 | return (static_cast<uint32_t>(n) << 1) ^ (n >> 31); |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | /** |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 415 | * Given a TType value, find the appropriate detail::compact::Types value |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 416 | */ |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 417 | template <class Transport_> |
| Roger Meier | 64a799d | 2013-06-04 20:59:01 +0200 | [diff] [blame] | 418 | int8_t TCompactProtocolT<Transport_>::getCompactType(const TType ttype) { |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 419 | return detail::compact::TTypeToCType[ttype]; |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | // |
| 423 | // Reading Methods |
| 424 | // |
| 425 | |
| 426 | /** |
| 427 | * Read a message header. |
| 428 | */ |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 429 | template <class Transport_> |
| 430 | uint32_t TCompactProtocolT<Transport_>::readMessageBegin( |
| 431 | std::string& name, |
| 432 | TMessageType& messageType, |
| 433 | int32_t& seqid) { |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 434 | uint32_t rsize = 0; |
| 435 | int8_t protocolId; |
| 436 | int8_t versionAndType; |
| 437 | int8_t version; |
| 438 | |
| 439 | rsize += readByte(protocolId); |
| 440 | if (protocolId != PROTOCOL_ID) { |
| 441 | throw TProtocolException(TProtocolException::BAD_VERSION, "Bad protocol identifier"); |
| 442 | } |
| 443 | |
| 444 | rsize += readByte(versionAndType); |
| Maximilian Bandle | 7c94335 | 2025-02-07 10:58:32 +0100 | [diff] [blame] | 445 | version = static_cast<int8_t>(versionAndType & VERSION_MASK); |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 446 | if (version != VERSION_N) { |
| 447 | throw TProtocolException(TProtocolException::BAD_VERSION, "Bad protocol version"); |
| 448 | } |
| 449 | |
| Maximilian Bandle | 7c94335 | 2025-02-07 10:58:32 +0100 | [diff] [blame] | 450 | messageType = static_cast<TMessageType>((versionAndType >> TYPE_SHIFT_AMOUNT) & TYPE_BITS); |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 451 | rsize += readVarint32(seqid); |
| 452 | rsize += readString(name); |
| 453 | |
| 454 | return rsize; |
| 455 | } |
| 456 | |
| 457 | /** |
| 458 | * Read a struct begin. There's nothing on the wire for this, but it is our |
| 459 | * opportunity to push a new struct begin marker on the field stack. |
| 460 | */ |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 461 | template <class Transport_> |
| 462 | uint32_t TCompactProtocolT<Transport_>::readStructBegin(std::string& name) { |
| Ben Gemmill | 7089a3a | 2025-11-06 10:11:12 -0500 | [diff] [blame] | 463 | name.clear(); |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 464 | lastField_.push(lastFieldId_); |
| 465 | lastFieldId_ = 0; |
| 466 | return 0; |
| 467 | } |
| 468 | |
| 469 | /** |
| 470 | * Doesn't actually consume any wire data, just removes the last field for |
| 471 | * this struct from the field stack. |
| 472 | */ |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 473 | template <class Transport_> |
| 474 | uint32_t TCompactProtocolT<Transport_>::readStructEnd() { |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 475 | lastFieldId_ = lastField_.top(); |
| 476 | lastField_.pop(); |
| 477 | return 0; |
| 478 | } |
| 479 | |
| 480 | /** |
| 481 | * Read a field header off the wire. |
| 482 | */ |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 483 | template <class Transport_> |
| 484 | uint32_t TCompactProtocolT<Transport_>::readFieldBegin(std::string& name, |
| 485 | TType& fieldType, |
| 486 | int16_t& fieldId) { |
| Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 487 | (void) name; |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 488 | uint32_t rsize = 0; |
| 489 | int8_t byte; |
| 490 | int8_t type; |
| 491 | |
| 492 | rsize += readByte(byte); |
| 493 | type = (byte & 0x0f); |
| 494 | |
| 495 | // if it's a stop, then we can return immediately, as the struct is over. |
| 496 | if (type == T_STOP) { |
| 497 | fieldType = T_STOP; |
| 498 | fieldId = 0; |
| 499 | return rsize; |
| 500 | } |
| 501 | |
| 502 | // mask off the 4 MSB of the type header. it could contain a field id delta. |
| Maximilian Bandle | 7c94335 | 2025-02-07 10:58:32 +0100 | [diff] [blame] | 503 | auto modifier = static_cast<int16_t>(static_cast<uint8_t>(byte & 0xf0) >> 4); |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 504 | if (modifier == 0) { |
| 505 | // not a delta, look ahead for the zigzag varint field id. |
| 506 | rsize += readI16(fieldId); |
| 507 | } else { |
| Maximilian Bandle | 7c94335 | 2025-02-07 10:58:32 +0100 | [diff] [blame] | 508 | fieldId = static_cast<int16_t>(lastFieldId_ + modifier); |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 509 | } |
| 510 | fieldType = getTType(type); |
| 511 | |
| 512 | // if this happens to be a boolean field, the value is encoded in the type |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 513 | if (type == detail::compact::CT_BOOLEAN_TRUE || |
| 514 | type == detail::compact::CT_BOOLEAN_FALSE) { |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 515 | // save the boolean value in a special instance variable. |
| 516 | boolValue_.hasBoolValue = true; |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 517 | boolValue_.boolValue = |
| 518 | (type == detail::compact::CT_BOOLEAN_TRUE ? true : false); |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | // push the new field onto the field stack so we can keep the deltas going. |
| 522 | lastFieldId_ = fieldId; |
| 523 | return rsize; |
| 524 | } |
| 525 | |
| 526 | /** |
| 527 | * Read a map header off the wire. If the size is zero, skip reading the key |
| 528 | * and value type. This means that 0-length maps will yield TMaps without the |
| 529 | * "correct" types. |
| 530 | */ |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 531 | template <class Transport_> |
| 532 | uint32_t TCompactProtocolT<Transport_>::readMapBegin(TType& keyType, |
| 533 | TType& valType, |
| 534 | uint32_t& size) { |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 535 | uint32_t rsize = 0; |
| 536 | int8_t kvType = 0; |
| 537 | int32_t msize = 0; |
| 538 | |
| 539 | rsize += readVarint32(msize); |
| 540 | if (msize != 0) |
| 541 | rsize += readByte(kvType); |
| 542 | |
| 543 | if (msize < 0) { |
| 544 | throw TProtocolException(TProtocolException::NEGATIVE_SIZE); |
| 545 | } else if (container_limit_ && msize > container_limit_) { |
| 546 | throw TProtocolException(TProtocolException::SIZE_LIMIT); |
| 547 | } |
| 548 | |
| Maximilian Bandle | 7c94335 | 2025-02-07 10:58:32 +0100 | [diff] [blame] | 549 | keyType = getTType(static_cast<int8_t>(static_cast<uint8_t>(kvType) >> 4)); |
| 550 | valType = getTType(static_cast<int8_t>(static_cast<uint8_t>(kvType) & 0xf)); |
| 551 | size = static_cast<uint32_t>(msize); |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 552 | |
| zeshuai007 | 86352b4 | 2020-06-15 17:00:33 +0800 | [diff] [blame] | 553 | TMap map(keyType, valType, size); |
| 554 | checkReadBytesAvailable(map); |
| 555 | |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 556 | return rsize; |
| 557 | } |
| 558 | |
| 559 | /** |
| 560 | * Read a list header off the wire. If the list size is 0-14, the size will |
| 561 | * be packed into the element type header. If it's a longer list, the 4 MSB |
| 562 | * of the element type header will be 0xF, and a varint will follow with the |
| 563 | * true size. |
| 564 | */ |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 565 | template <class Transport_> |
| 566 | uint32_t TCompactProtocolT<Transport_>::readListBegin(TType& elemType, |
| 567 | uint32_t& size) { |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 568 | int8_t size_and_type; |
| 569 | uint32_t rsize = 0; |
| 570 | int32_t lsize; |
| 571 | |
| 572 | rsize += readByte(size_and_type); |
| 573 | |
| Maximilian Bandle | 7c94335 | 2025-02-07 10:58:32 +0100 | [diff] [blame] | 574 | lsize = (static_cast<uint8_t>(size_and_type) >> 4) & 0x0f; |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 575 | if (lsize == 15) { |
| 576 | rsize += readVarint32(lsize); |
| 577 | } |
| 578 | |
| 579 | if (lsize < 0) { |
| 580 | throw TProtocolException(TProtocolException::NEGATIVE_SIZE); |
| 581 | } else if (container_limit_ && lsize > container_limit_) { |
| 582 | throw TProtocolException(TProtocolException::SIZE_LIMIT); |
| 583 | } |
| 584 | |
| Maximilian Bandle | 7c94335 | 2025-02-07 10:58:32 +0100 | [diff] [blame] | 585 | elemType = getTType(static_cast<int8_t>(size_and_type & 0x0f)); |
| 586 | size = static_cast<uint32_t>(lsize); |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 587 | |
| zeshuai007 | 86352b4 | 2020-06-15 17:00:33 +0800 | [diff] [blame] | 588 | TList list(elemType, size); |
| 589 | checkReadBytesAvailable(list); |
| 590 | |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 591 | return rsize; |
| 592 | } |
| 593 | |
| 594 | /** |
| 595 | * Read a set header off the wire. If the set size is 0-14, the size will |
| 596 | * be packed into the element type header. If it's a longer set, the 4 MSB |
| 597 | * of the element type header will be 0xF, and a varint will follow with the |
| 598 | * true size. |
| 599 | */ |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 600 | template <class Transport_> |
| 601 | uint32_t TCompactProtocolT<Transport_>::readSetBegin(TType& elemType, |
| 602 | uint32_t& size) { |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 603 | return readListBegin(elemType, size); |
| 604 | } |
| 605 | |
| 606 | /** |
| 607 | * Read a boolean off the wire. If this is a boolean field, the value should |
| 608 | * already have been read during readFieldBegin, so we'll just consume the |
| 609 | * pre-stored value. Otherwise, read a byte. |
| 610 | */ |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 611 | template <class Transport_> |
| 612 | uint32_t TCompactProtocolT<Transport_>::readBool(bool& value) { |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 613 | if (boolValue_.hasBoolValue == true) { |
| 614 | value = boolValue_.boolValue; |
| 615 | boolValue_.hasBoolValue = false; |
| 616 | return 0; |
| 617 | } else { |
| 618 | int8_t val; |
| 619 | readByte(val); |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 620 | value = (val == detail::compact::CT_BOOLEAN_TRUE); |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 621 | return 1; |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | /** |
| 626 | * Read a single byte off the wire. Nothing interesting here. |
| 627 | */ |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 628 | template <class Transport_> |
| 629 | uint32_t TCompactProtocolT<Transport_>::readByte(int8_t& byte) { |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 630 | uint8_t b[1]; |
| 631 | trans_->readAll(b, 1); |
| Maximilian Bandle | 7c94335 | 2025-02-07 10:58:32 +0100 | [diff] [blame] | 632 | byte = static_cast<int8_t>(b[0]); |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 633 | return 1; |
| 634 | } |
| 635 | |
| 636 | /** |
| 637 | * Read an i16 from the wire as a zigzag varint. |
| 638 | */ |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 639 | template <class Transport_> |
| 640 | uint32_t TCompactProtocolT<Transport_>::readI16(int16_t& i16) { |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 641 | int32_t value; |
| 642 | uint32_t rsize = readVarint32(value); |
| Maximilian Bandle | 7c94335 | 2025-02-07 10:58:32 +0100 | [diff] [blame] | 643 | i16 = static_cast<int16_t>(zigzagToI32(value)); |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 644 | return rsize; |
| 645 | } |
| 646 | |
| 647 | /** |
| 648 | * Read an i32 from the wire as a zigzag varint. |
| 649 | */ |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 650 | template <class Transport_> |
| 651 | uint32_t TCompactProtocolT<Transport_>::readI32(int32_t& i32) { |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 652 | int32_t value; |
| 653 | uint32_t rsize = readVarint32(value); |
| 654 | i32 = zigzagToI32(value); |
| 655 | return rsize; |
| 656 | } |
| 657 | |
| 658 | /** |
| 659 | * Read an i64 from the wire as a zigzag varint. |
| 660 | */ |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 661 | template <class Transport_> |
| 662 | uint32_t TCompactProtocolT<Transport_>::readI64(int64_t& i64) { |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 663 | int64_t value; |
| 664 | uint32_t rsize = readVarint64(value); |
| 665 | i64 = zigzagToI64(value); |
| 666 | return rsize; |
| 667 | } |
| 668 | |
| 669 | /** |
| 670 | * No magic here - just read a double off the wire. |
| 671 | */ |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 672 | template <class Transport_> |
| 673 | uint32_t TCompactProtocolT<Transport_>::readDouble(double& dub) { |
| cyy | 863262d | 2019-01-06 10:40:58 +0800 | [diff] [blame] | 674 | static_assert(sizeof(double) == sizeof(uint64_t), "sizeof(double) == sizeof(uint64_t)"); |
| 675 | static_assert(std::numeric_limits<double>::is_iec559, "std::numeric_limits<double>::is_iec559"); |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 676 | |
| Carl Yeksigian | 3e93711 | 2013-06-03 13:46:51 -0400 | [diff] [blame] | 677 | union { |
| 678 | uint64_t bits; |
| 679 | uint8_t b[8]; |
| 680 | } u; |
| 681 | trans_->readAll(u.b, 8); |
| jfarrell | ad3a955 | 2015-09-24 23:27:34 -0400 | [diff] [blame] | 682 | u.bits = THRIFT_letohll(u.bits); |
| Carl Yeksigian | 3e93711 | 2013-06-03 13:46:51 -0400 | [diff] [blame] | 683 | dub = bitwise_cast<double>(u.bits); |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 684 | return 8; |
| 685 | } |
| 686 | |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 687 | template <class Transport_> |
| 688 | uint32_t TCompactProtocolT<Transport_>::readString(std::string& str) { |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 689 | return readBinary(str); |
| 690 | } |
| 691 | |
| 692 | /** |
| 693 | * Read a byte[] from the wire. |
| 694 | */ |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 695 | template <class Transport_> |
| 696 | uint32_t TCompactProtocolT<Transport_>::readBinary(std::string& str) { |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 697 | int32_t rsize = 0; |
| 698 | int32_t size; |
| 699 | |
| 700 | rsize += readVarint32(size); |
| 701 | // Catch empty string case |
| 702 | if (size == 0) { |
| Ben Gemmill | 7089a3a | 2025-11-06 10:11:12 -0500 | [diff] [blame] | 703 | str.clear(); |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 704 | return rsize; |
| 705 | } |
| 706 | |
| 707 | // Catch error cases |
| 708 | if (size < 0) { |
| 709 | throw TProtocolException(TProtocolException::NEGATIVE_SIZE); |
| 710 | } |
| 711 | if (string_limit_ > 0 && size > string_limit_) { |
| 712 | throw TProtocolException(TProtocolException::SIZE_LIMIT); |
| 713 | } |
| 714 | |
| Maximilian Bandle | 5c08893 | 2025-02-07 10:00:56 +0100 | [diff] [blame] | 715 | // Check against MaxMessageSize before alloc |
| Maximilian Bandle | 7c94335 | 2025-02-07 10:58:32 +0100 | [diff] [blame] | 716 | trans_->checkReadBytesAvailable(static_cast<uint32_t>(size)); |
| Maximilian Bandle | 5c08893 | 2025-02-07 10:00:56 +0100 | [diff] [blame] | 717 | |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 718 | // Use the heap here to prevent stack overflow for v. large strings |
| Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 719 | if (size > string_buf_size_ || string_buf_ == nullptr) { |
| Maximilian Bandle | 7c94335 | 2025-02-07 10:58:32 +0100 | [diff] [blame] | 720 | void* new_string_buf = std::realloc(string_buf_, static_cast<uint32_t>(size)); |
| Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 721 | if (new_string_buf == nullptr) { |
| David Reiss | f673509 | 2010-10-06 17:10:49 +0000 | [diff] [blame] | 722 | throw std::bad_alloc(); |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 723 | } |
| Maximilian Bandle | 7c94335 | 2025-02-07 10:58:32 +0100 | [diff] [blame] | 724 | string_buf_ = static_cast<uint8_t*>(new_string_buf); |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 725 | string_buf_size_ = size; |
| 726 | } |
| 727 | trans_->readAll(string_buf_, size); |
| Maximilian Bandle | 7c94335 | 2025-02-07 10:58:32 +0100 | [diff] [blame] | 728 | str.assign(reinterpret_cast<char*>(string_buf_), size); |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 729 | |
| Maximilian Bandle | 7c94335 | 2025-02-07 10:58:32 +0100 | [diff] [blame] | 730 | return rsize + static_cast<uint32_t>(size); |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 731 | } |
| 732 | |
| Carel Combrink | 786764b | 2025-05-15 12:22:37 +0000 | [diff] [blame] | 733 | |
| 734 | /** |
| 735 | * Read a TUuid from the wire. |
| 736 | */ |
| 737 | template <class Transport_> |
| 738 | uint32_t TCompactProtocolT<Transport_>::readUUID(TUuid& uuid) { |
| 739 | return trans_->readAll(uuid.begin(), uuid.size()); |
| 740 | } |
| 741 | |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 742 | /** |
| 743 | * Read an i32 from the wire as a varint. The MSB of each byte is set |
| 744 | * if there is another byte to follow. This can read up to 5 bytes. |
| 745 | */ |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 746 | template <class Transport_> |
| 747 | uint32_t TCompactProtocolT<Transport_>::readVarint32(int32_t& i32) { |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 748 | int64_t val; |
| 749 | uint32_t rsize = readVarint64(val); |
| Maximilian Bandle | 7c94335 | 2025-02-07 10:58:32 +0100 | [diff] [blame] | 750 | i32 = static_cast<int32_t>(val); |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 751 | return rsize; |
| 752 | } |
| 753 | |
| 754 | /** |
| 755 | * Read an i64 from the wire as a proper varint. The MSB of each byte is set |
| 756 | * if there is another byte to follow. This can read up to 10 bytes. |
| 757 | */ |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 758 | template <class Transport_> |
| 759 | uint32_t TCompactProtocolT<Transport_>::readVarint64(int64_t& i64) { |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 760 | uint32_t rsize = 0; |
| 761 | uint64_t val = 0; |
| 762 | int shift = 0; |
| 763 | uint8_t buf[10]; // 64 bits / (7 bits/byte) = 10 bytes. |
| 764 | uint32_t buf_size = sizeof(buf); |
| 765 | const uint8_t* borrowed = trans_->borrow(buf, &buf_size); |
| 766 | |
| 767 | // Fast path. |
| Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 768 | if (borrowed != nullptr) { |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 769 | while (true) { |
| 770 | uint8_t byte = borrowed[rsize]; |
| 771 | rsize++; |
| Maximilian Bandle | 7c94335 | 2025-02-07 10:58:32 +0100 | [diff] [blame] | 772 | val |= static_cast<uint64_t>(byte & 0x7f) << shift; |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 773 | shift += 7; |
| 774 | if (!(byte & 0x80)) { |
| 775 | i64 = val; |
| 776 | trans_->consume(rsize); |
| 777 | return rsize; |
| 778 | } |
| 779 | // Have to check for invalid data so we don't crash. |
| 780 | if (UNLIKELY(rsize == sizeof(buf))) { |
| 781 | throw TProtocolException(TProtocolException::INVALID_DATA, "Variable-length int over 10 bytes."); |
| 782 | } |
| 783 | } |
| 784 | } |
| 785 | |
| 786 | // Slow path. |
| 787 | else { |
| 788 | while (true) { |
| 789 | uint8_t byte; |
| 790 | rsize += trans_->readAll(&byte, 1); |
| Maximilian Bandle | 7c94335 | 2025-02-07 10:58:32 +0100 | [diff] [blame] | 791 | val |= static_cast<uint64_t>(byte & 0x7f) << shift; |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 792 | shift += 7; |
| 793 | if (!(byte & 0x80)) { |
| 794 | i64 = val; |
| 795 | return rsize; |
| 796 | } |
| 797 | // Might as well check for invalid data on the slow path too. |
| 798 | if (UNLIKELY(rsize >= sizeof(buf))) { |
| 799 | throw TProtocolException(TProtocolException::INVALID_DATA, "Variable-length int over 10 bytes."); |
| 800 | } |
| 801 | } |
| 802 | } |
| 803 | } |
| 804 | |
| 805 | /** |
| 806 | * Convert from zigzag int to int. |
| 807 | */ |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 808 | template <class Transport_> |
| 809 | int32_t TCompactProtocolT<Transport_>::zigzagToI32(uint32_t n) { |
| Ben Craig | 7f10de7 | 2013-10-14 20:27:18 -0500 | [diff] [blame] | 810 | return (n >> 1) ^ static_cast<uint32_t>(-static_cast<int32_t>(n & 1)); |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | /** |
| 814 | * Convert from zigzag long to long. |
| 815 | */ |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 816 | template <class Transport_> |
| 817 | int64_t TCompactProtocolT<Transport_>::zigzagToI64(uint64_t n) { |
| Ben Craig | 7f10de7 | 2013-10-14 20:27:18 -0500 | [diff] [blame] | 818 | return (n >> 1) ^ static_cast<uint64_t>(-static_cast<int64_t>(n & 1)); |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 819 | } |
| 820 | |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 821 | template <class Transport_> |
| 822 | TType TCompactProtocolT<Transport_>::getTType(int8_t type) { |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 823 | switch (type) { |
| 824 | case T_STOP: |
| 825 | return T_STOP; |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 826 | case detail::compact::CT_BOOLEAN_FALSE: |
| 827 | case detail::compact::CT_BOOLEAN_TRUE: |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 828 | return T_BOOL; |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 829 | case detail::compact::CT_BYTE: |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 830 | return T_BYTE; |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 831 | case detail::compact::CT_I16: |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 832 | return T_I16; |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 833 | case detail::compact::CT_I32: |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 834 | return T_I32; |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 835 | case detail::compact::CT_I64: |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 836 | return T_I64; |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 837 | case detail::compact::CT_DOUBLE: |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 838 | return T_DOUBLE; |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 839 | case detail::compact::CT_BINARY: |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 840 | return T_STRING; |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 841 | case detail::compact::CT_LIST: |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 842 | return T_LIST; |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 843 | case detail::compact::CT_SET: |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 844 | return T_SET; |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 845 | case detail::compact::CT_MAP: |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 846 | return T_MAP; |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 847 | case detail::compact::CT_STRUCT: |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 848 | return T_STRUCT; |
| Carel Combrink | 786764b | 2025-05-15 12:22:37 +0000 | [diff] [blame] | 849 | case detail::compact::CT_UUID: |
| 850 | return T_UUID; |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 851 | default: |
| Maximilian Bandle | 7c94335 | 2025-02-07 10:58:32 +0100 | [diff] [blame] | 852 | throw TException(std::string("don't know what type: ") + static_cast<char>(type)); |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 853 | } |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 854 | } |
| 855 | |
| zeshuai007 | 86352b4 | 2020-06-15 17:00:33 +0800 | [diff] [blame] | 856 | // Return the minimum number of bytes a type will consume on the wire |
| 857 | template <class Transport_> |
| 858 | int TCompactProtocolT<Transport_>::getMinSerializedSize(TType type) |
| 859 | { |
| 860 | switch (type) |
| 861 | { |
| Hasnain Lakhani | 845a87a | 2025-05-27 22:31:42 -0700 | [diff] [blame] | 862 | case T_STOP: return 1; // T_STOP needs to count itself |
| 863 | case T_VOID: return 1; // T_VOID needs to count itself |
| zeshuai007 | 86352b4 | 2020-06-15 17:00:33 +0800 | [diff] [blame] | 864 | case T_BOOL: return sizeof(int8_t); |
| 865 | case T_DOUBLE: return 8; // uses fixedLongToBytes() which always writes 8 bytes |
| 866 | case T_BYTE: return sizeof(int8_t); |
| 867 | case T_I16: return sizeof(int8_t); // zigzag |
| 868 | case T_I32: return sizeof(int8_t); // zigzag |
| 869 | case T_I64: return sizeof(int8_t); // zigzag |
| 870 | case T_STRING: return sizeof(int8_t); // string length |
| Hasnain Lakhani | 845a87a | 2025-05-27 22:31:42 -0700 | [diff] [blame] | 871 | case T_STRUCT: return 1; // empty struct needs at least 1 byte for the T_STOP |
| zeshuai007 | 86352b4 | 2020-06-15 17:00:33 +0800 | [diff] [blame] | 872 | case T_MAP: return sizeof(int8_t); // element count |
| 873 | case T_SET: return sizeof(int8_t); // element count |
| 874 | case T_LIST: return sizeof(int8_t); // element count |
| Carel Combrink | 786764b | 2025-05-15 12:22:37 +0000 | [diff] [blame] | 875 | case T_UUID: return 16; // 16 bytes |
| zeshuai007 | 86352b4 | 2020-06-15 17:00:33 +0800 | [diff] [blame] | 876 | default: throw TProtocolException(TProtocolException::UNKNOWN, "unrecognized type code"); |
| 877 | } |
| 878 | } |
| 879 | |
| 880 | |
| David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 881 | }}} // apache::thrift::protocol |
| David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 882 | |
| 883 | #endif // _THRIFT_PROTOCOL_TCOMPACTPROTOCOL_TCC_ |