blob: 4d660e8a5014a1290ea707b0aa55fb7bc2caa4eb [file] [log] [blame]
David Reisse4d4ea02009-04-02 21:37:17 +00001/*
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 Reisse71115b2010-10-06 17:09:56 +000019#ifndef _THRIFT_PROTOCOL_TCOMPACTPROTOCOL_TCC_
20#define _THRIFT_PROTOCOL_TCOMPACTPROTOCOL_TCC_ 1
David Reisse4d4ea02009-04-02 21:37:17 +000021
Bryan Duxbury141eab42009-04-03 15:05:28 +000022#include <limits>
David Reisse4d4ea02009-04-02 21:37:17 +000023
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 Reisse71115b2010-10-06 17:09:56 +000035# error "TCompactProtocol currently only works if a signed right shift is arithmetic"
David Reisse4d4ea02009-04-02 21:37:17 +000036#endif
37
38#ifdef __GNUC__
39#define UNLIKELY(val) (__builtin_expect((val), 0))
40#else
41#define UNLIKELY(val) (val)
42#endif
43
44namespace apache { namespace thrift { namespace protocol {
45
David Reisse71115b2010-10-06 17:09:56 +000046namespace detail { namespace compact {
47
48enum 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 Meier0069cc42010-10-13 18:10:18 +000061 CT_STRUCT = 0x0C
David Reisse71115b2010-10-06 17:09:56 +000062};
63
64const 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 Reisse4d4ea02009-04-02 21:37:17 +000084
85
David Reisse71115b2010-10-06 17:09:56 +000086template <class Transport_>
87uint32_t TCompactProtocolT<Transport_>::writeMessageBegin(
88 const std::string& name,
89 const TMessageType messageType,
90 const int32_t seqid) {
David Reisse4d4ea02009-04-02 21:37:17 +000091 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 Reisse71115b2010-10-06 17:09:56 +0000105template <class Transport_>
106uint32_t TCompactProtocolT<Transport_>::writeFieldBegin(const char* name,
107 const TType fieldType,
108 const int16_t fieldId) {
David Reisse4d4ea02009-04-02 21:37:17 +0000109 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 Reisse71115b2010-10-06 17:09:56 +0000122template <class Transport_>
123uint32_t TCompactProtocolT<Transport_>::writeFieldStop() {
David Reisse4d4ea02009-04-02 21:37:17 +0000124 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 Reisse71115b2010-10-06 17:09:56 +0000132template <class Transport_>
133uint32_t TCompactProtocolT<Transport_>::writeStructBegin(const char* name) {
Roger Meier3b771a12010-11-17 22:11:26 +0000134 (void) name;
David Reisse4d4ea02009-04-02 21:37:17 +0000135 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 Reisse71115b2010-10-06 17:09:56 +0000145template <class Transport_>
146uint32_t TCompactProtocolT<Transport_>::writeStructEnd() {
David Reisse4d4ea02009-04-02 21:37:17 +0000147 lastFieldId_ = lastField_.top();
148 lastField_.pop();
149 return 0;
150}
151
152/**
153 * Write a List header.
154 */
David Reisse71115b2010-10-06 17:09:56 +0000155template <class Transport_>
156uint32_t TCompactProtocolT<Transport_>::writeListBegin(const TType elemType,
157 const uint32_t size) {
David Reisse4d4ea02009-04-02 21:37:17 +0000158 return writeCollectionBegin(elemType, size);
159}
160
161/**
162 * Write a set header.
163 */
David Reisse71115b2010-10-06 17:09:56 +0000164template <class Transport_>
165uint32_t TCompactProtocolT<Transport_>::writeSetBegin(const TType elemType,
166 const uint32_t size) {
David Reisse4d4ea02009-04-02 21:37:17 +0000167 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 Reisse71115b2010-10-06 17:09:56 +0000174template <class Transport_>
175uint32_t TCompactProtocolT<Transport_>::writeMapBegin(const TType keyType,
176 const TType valType,
177 const uint32_t size) {
David Reisse4d4ea02009-04-02 21:37:17 +0000178 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 Reisse71115b2010-10-06 17:09:56 +0000195template <class Transport_>
196uint32_t TCompactProtocolT<Transport_>::writeBool(const bool value) {
David Reisse4d4ea02009-04-02 21:37:17 +0000197 uint32_t wsize = 0;
198
199 if (booleanField_.name != NULL) {
200 // we haven't written the field header yet
Roger Meier64a799d2013-06-04 20:59:01 +0200201 wsize
202 += writeFieldBeginInternal(booleanField_.name,
203 booleanField_.fieldType,
204 booleanField_.fieldId,
205 static_cast<int8_t>(value
206 ? detail::compact::CT_BOOLEAN_TRUE
207 : detail::compact::CT_BOOLEAN_FALSE));
David Reisse4d4ea02009-04-02 21:37:17 +0000208 booleanField_.name = NULL;
209 } else {
210 // we're not part of a field, so just write the value
Roger Meier64a799d2013-06-04 20:59:01 +0200211 wsize
212 += writeByte(static_cast<int8_t>(value
213 ? detail::compact::CT_BOOLEAN_TRUE
214 : detail::compact::CT_BOOLEAN_FALSE));
David Reisse4d4ea02009-04-02 21:37:17 +0000215 }
216 return wsize;
217}
218
David Reisse71115b2010-10-06 17:09:56 +0000219template <class Transport_>
220uint32_t TCompactProtocolT<Transport_>::writeByte(const int8_t byte) {
David Reisse4d4ea02009-04-02 21:37:17 +0000221 trans_->write((uint8_t*)&byte, 1);
222 return 1;
223}
224
225/**
226 * Write an i16 as a zigzag varint.
227 */
David Reisse71115b2010-10-06 17:09:56 +0000228template <class Transport_>
229uint32_t TCompactProtocolT<Transport_>::writeI16(const int16_t i16) {
David Reisse4d4ea02009-04-02 21:37:17 +0000230 return writeVarint32(i32ToZigzag(i16));
231}
232
233/**
234 * Write an i32 as a zigzag varint.
235 */
David Reisse71115b2010-10-06 17:09:56 +0000236template <class Transport_>
237uint32_t TCompactProtocolT<Transport_>::writeI32(const int32_t i32) {
David Reisse4d4ea02009-04-02 21:37:17 +0000238 return writeVarint32(i32ToZigzag(i32));
239}
240
241/**
242 * Write an i64 as a zigzag varint.
243 */
David Reisse71115b2010-10-06 17:09:56 +0000244template <class Transport_>
245uint32_t TCompactProtocolT<Transport_>::writeI64(const int64_t i64) {
David Reisse4d4ea02009-04-02 21:37:17 +0000246 return writeVarint64(i64ToZigzag(i64));
247}
248
249/**
250 * Write a double to the wire as 8 bytes.
251 */
David Reisse71115b2010-10-06 17:09:56 +0000252template <class Transport_>
253uint32_t TCompactProtocolT<Transport_>::writeDouble(const double dub) {
David Reisse4d4ea02009-04-02 21:37:17 +0000254 BOOST_STATIC_ASSERT(sizeof(double) == sizeof(uint64_t));
255 BOOST_STATIC_ASSERT(std::numeric_limits<double>::is_iec559);
256
257 uint64_t bits = bitwise_cast<uint64_t>(dub);
jfarrellad3a9552015-09-24 23:27:34 -0400258 bits = THRIFT_htolell(bits);
David Reisse4d4ea02009-04-02 21:37:17 +0000259 trans_->write((uint8_t*)&bits, 8);
260 return 8;
261}
262
263/**
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +0100264 * Write a string to the wire with a varint size preceding.
David Reisse4d4ea02009-04-02 21:37:17 +0000265 */
David Reisse71115b2010-10-06 17:09:56 +0000266template <class Transport_>
267uint32_t TCompactProtocolT<Transport_>::writeString(const std::string& str) {
David Reisse4d4ea02009-04-02 21:37:17 +0000268 return writeBinary(str);
269}
270
David Reisse71115b2010-10-06 17:09:56 +0000271template <class Transport_>
272uint32_t TCompactProtocolT<Transport_>::writeBinary(const std::string& str) {
Ben Craig7f10de72013-10-14 20:27:18 -0500273 if(str.size() > (std::numeric_limits<uint32_t>::max)())
274 throw TProtocolException(TProtocolException::SIZE_LIMIT);
275 uint32_t ssize = static_cast<uint32_t>(str.size());
276 uint32_t wsize = writeVarint32(ssize) ;
277 // checking ssize + wsize > uint_max, but we don't want to overflow while checking for overflows.
278 // transforming the check to ssize > uint_max - wsize
279 if(ssize > (std::numeric_limits<uint32_t>::max)() - wsize)
280 throw TProtocolException(TProtocolException::SIZE_LIMIT);
281 wsize += ssize;
David Reisse4d4ea02009-04-02 21:37:17 +0000282 trans_->write((uint8_t*)str.data(), ssize);
283 return wsize;
284}
285
286//
287// Internal Writing methods
288//
289
290/**
291 * The workhorse of writeFieldBegin. It has the option of doing a
292 * 'type override' of the type header. This is used specifically in the
293 * boolean field case.
294 */
David Reisse71115b2010-10-06 17:09:56 +0000295template <class Transport_>
296int32_t TCompactProtocolT<Transport_>::writeFieldBeginInternal(
297 const char* name,
298 const TType fieldType,
299 const int16_t fieldId,
300 int8_t typeOverride) {
Roger Meier3b771a12010-11-17 22:11:26 +0000301 (void) name;
David Reisse4d4ea02009-04-02 21:37:17 +0000302 uint32_t wsize = 0;
303
304 // if there's a type override, use that.
305 int8_t typeToWrite = (typeOverride == -1 ? getCompactType(fieldType) : typeOverride);
306
307 // check if we can use delta encoding for the field id
308 if (fieldId > lastFieldId_ && fieldId - lastFieldId_ <= 15) {
309 // write them together
Roger Meier64a799d2013-06-04 20:59:01 +0200310 wsize += writeByte(static_cast<int8_t>((fieldId - lastFieldId_)
311 << 4 | typeToWrite));
David Reisse4d4ea02009-04-02 21:37:17 +0000312 } else {
313 // write them separate
314 wsize += writeByte(typeToWrite);
315 wsize += writeI16(fieldId);
316 }
317
318 lastFieldId_ = fieldId;
319 return wsize;
320}
321
322/**
323 * Abstract method for writing the start of lists and sets. List and sets on
324 * the wire differ only by the type indicator.
325 */
David Reisse71115b2010-10-06 17:09:56 +0000326template <class Transport_>
Roger Meier64a799d2013-06-04 20:59:01 +0200327uint32_t TCompactProtocolT<Transport_>::writeCollectionBegin(const TType elemType,
David Reisse71115b2010-10-06 17:09:56 +0000328 int32_t size) {
David Reisse4d4ea02009-04-02 21:37:17 +0000329 uint32_t wsize = 0;
330 if (size <= 14) {
Roger Meier64a799d2013-06-04 20:59:01 +0200331 wsize += writeByte(static_cast<int8_t>(size
332 << 4 | getCompactType(elemType)));
David Reisse4d4ea02009-04-02 21:37:17 +0000333 } else {
334 wsize += writeByte(0xf0 | getCompactType(elemType));
335 wsize += writeVarint32(size);
336 }
337 return wsize;
338}
339
340/**
341 * Write an i32 as a varint. Results in 1-5 bytes on the wire.
342 */
David Reisse71115b2010-10-06 17:09:56 +0000343template <class Transport_>
344uint32_t TCompactProtocolT<Transport_>::writeVarint32(uint32_t n) {
David Reisse4d4ea02009-04-02 21:37:17 +0000345 uint8_t buf[5];
346 uint32_t wsize = 0;
347
348 while (true) {
349 if ((n & ~0x7F) == 0) {
350 buf[wsize++] = (int8_t)n;
351 break;
352 } else {
353 buf[wsize++] = (int8_t)((n & 0x7F) | 0x80);
354 n >>= 7;
355 }
356 }
357 trans_->write(buf, wsize);
358 return wsize;
359}
360
361/**
362 * Write an i64 as a varint. Results in 1-10 bytes on the wire.
363 */
David Reisse71115b2010-10-06 17:09:56 +0000364template <class Transport_>
365uint32_t TCompactProtocolT<Transport_>::writeVarint64(uint64_t n) {
David Reisse4d4ea02009-04-02 21:37:17 +0000366 uint8_t buf[10];
367 uint32_t wsize = 0;
368
369 while (true) {
370 if ((n & ~0x7FL) == 0) {
371 buf[wsize++] = (int8_t)n;
372 break;
373 } else {
374 buf[wsize++] = (int8_t)((n & 0x7F) | 0x80);
375 n >>= 7;
376 }
377 }
378 trans_->write(buf, wsize);
379 return wsize;
380}
381
382/**
383 * Convert l into a zigzag long. This allows negative numbers to be
384 * represented compactly as a varint.
385 */
David Reisse71115b2010-10-06 17:09:56 +0000386template <class Transport_>
387uint64_t TCompactProtocolT<Transport_>::i64ToZigzag(const int64_t l) {
David Reisse4d4ea02009-04-02 21:37:17 +0000388 return (l << 1) ^ (l >> 63);
389}
390
391/**
392 * Convert n into a zigzag int. This allows negative numbers to be
393 * represented compactly as a varint.
394 */
David Reisse71115b2010-10-06 17:09:56 +0000395template <class Transport_>
396uint32_t TCompactProtocolT<Transport_>::i32ToZigzag(const int32_t n) {
David Reisse4d4ea02009-04-02 21:37:17 +0000397 return (n << 1) ^ (n >> 31);
398}
399
400/**
David Reisse71115b2010-10-06 17:09:56 +0000401 * Given a TType value, find the appropriate detail::compact::Types value
David Reisse4d4ea02009-04-02 21:37:17 +0000402 */
David Reisse71115b2010-10-06 17:09:56 +0000403template <class Transport_>
Roger Meier64a799d2013-06-04 20:59:01 +0200404int8_t TCompactProtocolT<Transport_>::getCompactType(const TType ttype) {
David Reisse71115b2010-10-06 17:09:56 +0000405 return detail::compact::TTypeToCType[ttype];
David Reisse4d4ea02009-04-02 21:37:17 +0000406}
407
408//
409// Reading Methods
410//
411
412/**
413 * Read a message header.
414 */
David Reisse71115b2010-10-06 17:09:56 +0000415template <class Transport_>
416uint32_t TCompactProtocolT<Transport_>::readMessageBegin(
417 std::string& name,
418 TMessageType& messageType,
419 int32_t& seqid) {
David Reisse4d4ea02009-04-02 21:37:17 +0000420 uint32_t rsize = 0;
421 int8_t protocolId;
422 int8_t versionAndType;
423 int8_t version;
424
425 rsize += readByte(protocolId);
426 if (protocolId != PROTOCOL_ID) {
427 throw TProtocolException(TProtocolException::BAD_VERSION, "Bad protocol identifier");
428 }
429
430 rsize += readByte(versionAndType);
431 version = (int8_t)(versionAndType & VERSION_MASK);
432 if (version != VERSION_N) {
433 throw TProtocolException(TProtocolException::BAD_VERSION, "Bad protocol version");
434 }
435
Jens Geyera86886e2014-09-17 22:25:48 +0200436 messageType = (TMessageType)((versionAndType >> TYPE_SHIFT_AMOUNT) & TYPE_BITS);
David Reisse4d4ea02009-04-02 21:37:17 +0000437 rsize += readVarint32(seqid);
438 rsize += readString(name);
439
440 return rsize;
441}
442
443/**
444 * Read a struct begin. There's nothing on the wire for this, but it is our
445 * opportunity to push a new struct begin marker on the field stack.
446 */
David Reisse71115b2010-10-06 17:09:56 +0000447template <class Transport_>
448uint32_t TCompactProtocolT<Transport_>::readStructBegin(std::string& name) {
David Reisse4d4ea02009-04-02 21:37:17 +0000449 name = "";
450 lastField_.push(lastFieldId_);
451 lastFieldId_ = 0;
452 return 0;
453}
454
455/**
456 * Doesn't actually consume any wire data, just removes the last field for
457 * this struct from the field stack.
458 */
David Reisse71115b2010-10-06 17:09:56 +0000459template <class Transport_>
460uint32_t TCompactProtocolT<Transport_>::readStructEnd() {
David Reisse4d4ea02009-04-02 21:37:17 +0000461 lastFieldId_ = lastField_.top();
462 lastField_.pop();
463 return 0;
464}
465
466/**
467 * Read a field header off the wire.
468 */
David Reisse71115b2010-10-06 17:09:56 +0000469template <class Transport_>
470uint32_t TCompactProtocolT<Transport_>::readFieldBegin(std::string& name,
471 TType& fieldType,
472 int16_t& fieldId) {
Roger Meier3b771a12010-11-17 22:11:26 +0000473 (void) name;
David Reisse4d4ea02009-04-02 21:37:17 +0000474 uint32_t rsize = 0;
475 int8_t byte;
476 int8_t type;
477
478 rsize += readByte(byte);
479 type = (byte & 0x0f);
480
481 // if it's a stop, then we can return immediately, as the struct is over.
482 if (type == T_STOP) {
483 fieldType = T_STOP;
484 fieldId = 0;
485 return rsize;
486 }
487
488 // mask off the 4 MSB of the type header. it could contain a field id delta.
489 int16_t modifier = (int16_t)(((uint8_t)byte & 0xf0) >> 4);
490 if (modifier == 0) {
491 // not a delta, look ahead for the zigzag varint field id.
492 rsize += readI16(fieldId);
493 } else {
494 fieldId = (int16_t)(lastFieldId_ + modifier);
495 }
496 fieldType = getTType(type);
497
498 // if this happens to be a boolean field, the value is encoded in the type
David Reisse71115b2010-10-06 17:09:56 +0000499 if (type == detail::compact::CT_BOOLEAN_TRUE ||
500 type == detail::compact::CT_BOOLEAN_FALSE) {
David Reisse4d4ea02009-04-02 21:37:17 +0000501 // save the boolean value in a special instance variable.
502 boolValue_.hasBoolValue = true;
David Reisse71115b2010-10-06 17:09:56 +0000503 boolValue_.boolValue =
504 (type == detail::compact::CT_BOOLEAN_TRUE ? true : false);
David Reisse4d4ea02009-04-02 21:37:17 +0000505 }
506
507 // push the new field onto the field stack so we can keep the deltas going.
508 lastFieldId_ = fieldId;
509 return rsize;
510}
511
512/**
513 * Read a map header off the wire. If the size is zero, skip reading the key
514 * and value type. This means that 0-length maps will yield TMaps without the
515 * "correct" types.
516 */
David Reisse71115b2010-10-06 17:09:56 +0000517template <class Transport_>
518uint32_t TCompactProtocolT<Transport_>::readMapBegin(TType& keyType,
519 TType& valType,
520 uint32_t& size) {
David Reisse4d4ea02009-04-02 21:37:17 +0000521 uint32_t rsize = 0;
522 int8_t kvType = 0;
523 int32_t msize = 0;
524
525 rsize += readVarint32(msize);
526 if (msize != 0)
527 rsize += readByte(kvType);
528
529 if (msize < 0) {
530 throw TProtocolException(TProtocolException::NEGATIVE_SIZE);
531 } else if (container_limit_ && msize > container_limit_) {
532 throw TProtocolException(TProtocolException::SIZE_LIMIT);
533 }
534
535 keyType = getTType((int8_t)((uint8_t)kvType >> 4));
536 valType = getTType((int8_t)((uint8_t)kvType & 0xf));
537 size = (uint32_t)msize;
538
539 return rsize;
540}
541
542/**
543 * Read a list header off the wire. If the list size is 0-14, the size will
544 * be packed into the element type header. If it's a longer list, the 4 MSB
545 * of the element type header will be 0xF, and a varint will follow with the
546 * true size.
547 */
David Reisse71115b2010-10-06 17:09:56 +0000548template <class Transport_>
549uint32_t TCompactProtocolT<Transport_>::readListBegin(TType& elemType,
550 uint32_t& size) {
David Reisse4d4ea02009-04-02 21:37:17 +0000551 int8_t size_and_type;
552 uint32_t rsize = 0;
553 int32_t lsize;
554
555 rsize += readByte(size_and_type);
556
557 lsize = ((uint8_t)size_and_type >> 4) & 0x0f;
558 if (lsize == 15) {
559 rsize += readVarint32(lsize);
560 }
561
562 if (lsize < 0) {
563 throw TProtocolException(TProtocolException::NEGATIVE_SIZE);
564 } else if (container_limit_ && lsize > container_limit_) {
565 throw TProtocolException(TProtocolException::SIZE_LIMIT);
566 }
567
568 elemType = getTType((int8_t)(size_and_type & 0x0f));
569 size = (uint32_t)lsize;
570
571 return rsize;
572}
573
574/**
575 * Read a set header off the wire. If the set size is 0-14, the size will
576 * be packed into the element type header. If it's a longer set, the 4 MSB
577 * of the element type header will be 0xF, and a varint will follow with the
578 * true size.
579 */
David Reisse71115b2010-10-06 17:09:56 +0000580template <class Transport_>
581uint32_t TCompactProtocolT<Transport_>::readSetBegin(TType& elemType,
582 uint32_t& size) {
David Reisse4d4ea02009-04-02 21:37:17 +0000583 return readListBegin(elemType, size);
584}
585
586/**
587 * Read a boolean off the wire. If this is a boolean field, the value should
588 * already have been read during readFieldBegin, so we'll just consume the
589 * pre-stored value. Otherwise, read a byte.
590 */
David Reisse71115b2010-10-06 17:09:56 +0000591template <class Transport_>
592uint32_t TCompactProtocolT<Transport_>::readBool(bool& value) {
David Reisse4d4ea02009-04-02 21:37:17 +0000593 if (boolValue_.hasBoolValue == true) {
594 value = boolValue_.boolValue;
595 boolValue_.hasBoolValue = false;
596 return 0;
597 } else {
598 int8_t val;
599 readByte(val);
David Reisse71115b2010-10-06 17:09:56 +0000600 value = (val == detail::compact::CT_BOOLEAN_TRUE);
David Reisse4d4ea02009-04-02 21:37:17 +0000601 return 1;
602 }
603}
604
605/**
606 * Read a single byte off the wire. Nothing interesting here.
607 */
David Reisse71115b2010-10-06 17:09:56 +0000608template <class Transport_>
609uint32_t TCompactProtocolT<Transport_>::readByte(int8_t& byte) {
David Reisse4d4ea02009-04-02 21:37:17 +0000610 uint8_t b[1];
611 trans_->readAll(b, 1);
612 byte = *(int8_t*)b;
613 return 1;
614}
615
616/**
617 * Read an i16 from the wire as a zigzag varint.
618 */
David Reisse71115b2010-10-06 17:09:56 +0000619template <class Transport_>
620uint32_t TCompactProtocolT<Transport_>::readI16(int16_t& i16) {
David Reisse4d4ea02009-04-02 21:37:17 +0000621 int32_t value;
622 uint32_t rsize = readVarint32(value);
623 i16 = (int16_t)zigzagToI32(value);
624 return rsize;
625}
626
627/**
628 * Read an i32 from the wire as a zigzag varint.
629 */
David Reisse71115b2010-10-06 17:09:56 +0000630template <class Transport_>
631uint32_t TCompactProtocolT<Transport_>::readI32(int32_t& i32) {
David Reisse4d4ea02009-04-02 21:37:17 +0000632 int32_t value;
633 uint32_t rsize = readVarint32(value);
634 i32 = zigzagToI32(value);
635 return rsize;
636}
637
638/**
639 * Read an i64 from the wire as a zigzag varint.
640 */
David Reisse71115b2010-10-06 17:09:56 +0000641template <class Transport_>
642uint32_t TCompactProtocolT<Transport_>::readI64(int64_t& i64) {
David Reisse4d4ea02009-04-02 21:37:17 +0000643 int64_t value;
644 uint32_t rsize = readVarint64(value);
645 i64 = zigzagToI64(value);
646 return rsize;
647}
648
649/**
650 * No magic here - just read a double off the wire.
651 */
David Reisse71115b2010-10-06 17:09:56 +0000652template <class Transport_>
653uint32_t TCompactProtocolT<Transport_>::readDouble(double& dub) {
David Reisse4d4ea02009-04-02 21:37:17 +0000654 BOOST_STATIC_ASSERT(sizeof(double) == sizeof(uint64_t));
655 BOOST_STATIC_ASSERT(std::numeric_limits<double>::is_iec559);
656
Carl Yeksigian3e937112013-06-03 13:46:51 -0400657 union {
658 uint64_t bits;
659 uint8_t b[8];
660 } u;
661 trans_->readAll(u.b, 8);
jfarrellad3a9552015-09-24 23:27:34 -0400662 u.bits = THRIFT_letohll(u.bits);
Carl Yeksigian3e937112013-06-03 13:46:51 -0400663 dub = bitwise_cast<double>(u.bits);
David Reisse4d4ea02009-04-02 21:37:17 +0000664 return 8;
665}
666
David Reisse71115b2010-10-06 17:09:56 +0000667template <class Transport_>
668uint32_t TCompactProtocolT<Transport_>::readString(std::string& str) {
David Reisse4d4ea02009-04-02 21:37:17 +0000669 return readBinary(str);
670}
671
672/**
673 * Read a byte[] from the wire.
674 */
David Reisse71115b2010-10-06 17:09:56 +0000675template <class Transport_>
676uint32_t TCompactProtocolT<Transport_>::readBinary(std::string& str) {
David Reisse4d4ea02009-04-02 21:37:17 +0000677 int32_t rsize = 0;
678 int32_t size;
679
680 rsize += readVarint32(size);
681 // Catch empty string case
682 if (size == 0) {
683 str = "";
684 return rsize;
685 }
686
687 // Catch error cases
688 if (size < 0) {
689 throw TProtocolException(TProtocolException::NEGATIVE_SIZE);
690 }
691 if (string_limit_ > 0 && size > string_limit_) {
692 throw TProtocolException(TProtocolException::SIZE_LIMIT);
693 }
694
695 // Use the heap here to prevent stack overflow for v. large strings
696 if (size > string_buf_size_ || string_buf_ == NULL) {
697 void* new_string_buf = std::realloc(string_buf_, (uint32_t)size);
698 if (new_string_buf == NULL) {
David Reissf6735092010-10-06 17:10:49 +0000699 throw std::bad_alloc();
David Reisse4d4ea02009-04-02 21:37:17 +0000700 }
701 string_buf_ = (uint8_t*)new_string_buf;
702 string_buf_size_ = size;
703 }
704 trans_->readAll(string_buf_, size);
705 str.assign((char*)string_buf_, size);
706
707 return rsize + (uint32_t)size;
708}
709
710/**
711 * Read an i32 from the wire as a varint. The MSB of each byte is set
712 * if there is another byte to follow. This can read up to 5 bytes.
713 */
David Reisse71115b2010-10-06 17:09:56 +0000714template <class Transport_>
715uint32_t TCompactProtocolT<Transport_>::readVarint32(int32_t& i32) {
David Reisse4d4ea02009-04-02 21:37:17 +0000716 int64_t val;
717 uint32_t rsize = readVarint64(val);
718 i32 = (int32_t)val;
719 return rsize;
720}
721
722/**
723 * Read an i64 from the wire as a proper varint. The MSB of each byte is set
724 * if there is another byte to follow. This can read up to 10 bytes.
725 */
David Reisse71115b2010-10-06 17:09:56 +0000726template <class Transport_>
727uint32_t TCompactProtocolT<Transport_>::readVarint64(int64_t& i64) {
David Reisse4d4ea02009-04-02 21:37:17 +0000728 uint32_t rsize = 0;
729 uint64_t val = 0;
730 int shift = 0;
731 uint8_t buf[10]; // 64 bits / (7 bits/byte) = 10 bytes.
732 uint32_t buf_size = sizeof(buf);
733 const uint8_t* borrowed = trans_->borrow(buf, &buf_size);
734
735 // Fast path.
736 if (borrowed != NULL) {
737 while (true) {
738 uint8_t byte = borrowed[rsize];
739 rsize++;
740 val |= (uint64_t)(byte & 0x7f) << shift;
741 shift += 7;
742 if (!(byte & 0x80)) {
743 i64 = val;
744 trans_->consume(rsize);
745 return rsize;
746 }
747 // Have to check for invalid data so we don't crash.
748 if (UNLIKELY(rsize == sizeof(buf))) {
749 throw TProtocolException(TProtocolException::INVALID_DATA, "Variable-length int over 10 bytes.");
750 }
751 }
752 }
753
754 // Slow path.
755 else {
756 while (true) {
757 uint8_t byte;
758 rsize += trans_->readAll(&byte, 1);
759 val |= (uint64_t)(byte & 0x7f) << shift;
760 shift += 7;
761 if (!(byte & 0x80)) {
762 i64 = val;
763 return rsize;
764 }
765 // Might as well check for invalid data on the slow path too.
766 if (UNLIKELY(rsize >= sizeof(buf))) {
767 throw TProtocolException(TProtocolException::INVALID_DATA, "Variable-length int over 10 bytes.");
768 }
769 }
770 }
771}
772
773/**
774 * Convert from zigzag int to int.
775 */
David Reisse71115b2010-10-06 17:09:56 +0000776template <class Transport_>
777int32_t TCompactProtocolT<Transport_>::zigzagToI32(uint32_t n) {
Ben Craig7f10de72013-10-14 20:27:18 -0500778 return (n >> 1) ^ static_cast<uint32_t>(-static_cast<int32_t>(n & 1));
David Reisse4d4ea02009-04-02 21:37:17 +0000779}
780
781/**
782 * Convert from zigzag long to long.
783 */
David Reisse71115b2010-10-06 17:09:56 +0000784template <class Transport_>
785int64_t TCompactProtocolT<Transport_>::zigzagToI64(uint64_t n) {
Ben Craig7f10de72013-10-14 20:27:18 -0500786 return (n >> 1) ^ static_cast<uint64_t>(-static_cast<int64_t>(n & 1));
David Reisse4d4ea02009-04-02 21:37:17 +0000787}
788
David Reisse71115b2010-10-06 17:09:56 +0000789template <class Transport_>
790TType TCompactProtocolT<Transport_>::getTType(int8_t type) {
David Reisse4d4ea02009-04-02 21:37:17 +0000791 switch (type) {
792 case T_STOP:
793 return T_STOP;
David Reisse71115b2010-10-06 17:09:56 +0000794 case detail::compact::CT_BOOLEAN_FALSE:
795 case detail::compact::CT_BOOLEAN_TRUE:
David Reisse4d4ea02009-04-02 21:37:17 +0000796 return T_BOOL;
David Reisse71115b2010-10-06 17:09:56 +0000797 case detail::compact::CT_BYTE:
David Reisse4d4ea02009-04-02 21:37:17 +0000798 return T_BYTE;
David Reisse71115b2010-10-06 17:09:56 +0000799 case detail::compact::CT_I16:
David Reisse4d4ea02009-04-02 21:37:17 +0000800 return T_I16;
David Reisse71115b2010-10-06 17:09:56 +0000801 case detail::compact::CT_I32:
David Reisse4d4ea02009-04-02 21:37:17 +0000802 return T_I32;
David Reisse71115b2010-10-06 17:09:56 +0000803 case detail::compact::CT_I64:
David Reisse4d4ea02009-04-02 21:37:17 +0000804 return T_I64;
David Reisse71115b2010-10-06 17:09:56 +0000805 case detail::compact::CT_DOUBLE:
David Reisse4d4ea02009-04-02 21:37:17 +0000806 return T_DOUBLE;
David Reisse71115b2010-10-06 17:09:56 +0000807 case detail::compact::CT_BINARY:
David Reisse4d4ea02009-04-02 21:37:17 +0000808 return T_STRING;
David Reisse71115b2010-10-06 17:09:56 +0000809 case detail::compact::CT_LIST:
David Reisse4d4ea02009-04-02 21:37:17 +0000810 return T_LIST;
David Reisse71115b2010-10-06 17:09:56 +0000811 case detail::compact::CT_SET:
David Reisse4d4ea02009-04-02 21:37:17 +0000812 return T_SET;
David Reisse71115b2010-10-06 17:09:56 +0000813 case detail::compact::CT_MAP:
David Reisse4d4ea02009-04-02 21:37:17 +0000814 return T_MAP;
David Reisse71115b2010-10-06 17:09:56 +0000815 case detail::compact::CT_STRUCT:
David Reisse4d4ea02009-04-02 21:37:17 +0000816 return T_STRUCT;
817 default:
Roger Meiera7ab94d2013-03-22 22:34:16 +0100818 throw TException(std::string("don't know what type: ") + (char)type);
David Reisse4d4ea02009-04-02 21:37:17 +0000819 }
David Reisse4d4ea02009-04-02 21:37:17 +0000820}
821
822}}} // apache::thrift::protocol
David Reisse71115b2010-10-06 17:09:56 +0000823
824#endif // _THRIFT_PROTOCOL_TCOMPACTPROTOCOL_TCC_