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