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