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 */
19 #ifndef _THRIFT_PROTOCOL_TCOMPACTPROTOCOL_TCC_
20 #define _THRIFT_PROTOCOL_TCOMPACTPROTOCOL_TCC_ 1
21
22 #include <limits>
23 #include <cstdlib>
24
25 #include "thrift/config.h"
26
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
38 # error "TCompactProtocol currently only works if a signed right shift is arithmetic"
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
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,
64 CT_STRUCT = 0x0C
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
87
88
89 template <class Transport_>
writeMessageBegin(const std::string & name,const TMessageType messageType,const int32_t seqid)90 uint32_t TCompactProtocolT<Transport_>::writeMessageBegin(
91 const std::string& name,
92 const TMessageType messageType,
93 const int32_t seqid) {
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 */
108 template <class Transport_>
writeFieldBegin(const char * name,const TType fieldType,const int16_t fieldId)109 uint32_t TCompactProtocolT<Transport_>::writeFieldBegin(const char* name,
110 const TType fieldType,
111 const int16_t fieldId) {
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 */
125 template <class Transport_>
writeFieldStop()126 uint32_t TCompactProtocolT<Transport_>::writeFieldStop() {
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 */
135 template <class Transport_>
writeStructBegin(const char * name)136 uint32_t TCompactProtocolT<Transport_>::writeStructBegin(const char* name) {
137 (void) name;
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 */
148 template <class Transport_>
writeStructEnd()149 uint32_t TCompactProtocolT<Transport_>::writeStructEnd() {
150 lastFieldId_ = lastField_.top();
151 lastField_.pop();
152 return 0;
153 }
154
155 /**
156 * Write a List header.
157 */
158 template <class Transport_>
writeListBegin(const TType elemType,const uint32_t size)159 uint32_t TCompactProtocolT<Transport_>::writeListBegin(const TType elemType,
160 const uint32_t size) {
161 return writeCollectionBegin(elemType, size);
162 }
163
164 /**
165 * Write a set header.
166 */
167 template <class Transport_>
writeSetBegin(const TType elemType,const uint32_t size)168 uint32_t TCompactProtocolT<Transport_>::writeSetBegin(const TType elemType,
169 const uint32_t size) {
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 */
177 template <class Transport_>
writeMapBegin(const TType keyType,const TType valType,const uint32_t size)178 uint32_t TCompactProtocolT<Transport_>::writeMapBegin(const TType keyType,
179 const TType valType,
180 const uint32_t size) {
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 */
198 template <class Transport_>
writeBool(const bool value)199 uint32_t TCompactProtocolT<Transport_>::writeBool(const bool value) {
200 uint32_t wsize = 0;
201
202 if (booleanField_.name != nullptr) {
203 // we haven't written the field header yet
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));
211 booleanField_.name = nullptr;
212 } else {
213 // we're not part of a field, so just write the value
214 wsize
215 += writeByte(static_cast<int8_t>(value
216 ? detail::compact::CT_BOOLEAN_TRUE
217 : detail::compact::CT_BOOLEAN_FALSE));
218 }
219 return wsize;
220 }
221
222 template <class Transport_>
writeByte(const int8_t byte)223 uint32_t TCompactProtocolT<Transport_>::writeByte(const int8_t byte) {
224 trans_->write((uint8_t*)&byte, 1);
225 return 1;
226 }
227
228 /**
229 * Write an i16 as a zigzag varint.
230 */
231 template <class Transport_>
writeI16(const int16_t i16)232 uint32_t TCompactProtocolT<Transport_>::writeI16(const int16_t i16) {
233 return writeVarint32(i32ToZigzag(i16));
234 }
235
236 /**
237 * Write an i32 as a zigzag varint.
238 */
239 template <class Transport_>
writeI32(const int32_t i32)240 uint32_t TCompactProtocolT<Transport_>::writeI32(const int32_t i32) {
241 return writeVarint32(i32ToZigzag(i32));
242 }
243
244 /**
245 * Write an i64 as a zigzag varint.
246 */
247 template <class Transport_>
writeI64(const int64_t i64)248 uint32_t TCompactProtocolT<Transport_>::writeI64(const int64_t i64) {
249 return writeVarint64(i64ToZigzag(i64));
250 }
251
252 /**
253 * Write a double to the wire as 8 bytes.
254 */
255 template <class Transport_>
writeDouble(const double dub)256 uint32_t TCompactProtocolT<Transport_>::writeDouble(const double dub) {
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");
259
260 auto bits = bitwise_cast<uint64_t>(dub);
261 bits = THRIFT_htolell(bits);
262 trans_->write((uint8_t*)&bits, 8);
263 return 8;
264 }
265
266 /**
267 * Write a string to the wire with a varint size preceding.
268 */
269 template <class Transport_>
writeString(const std::string & str)270 uint32_t TCompactProtocolT<Transport_>::writeString(const std::string& str) {
271 return writeBinary(str);
272 }
273
274 template <class Transport_>
writeBinary(const std::string & str)275 uint32_t TCompactProtocolT<Transport_>::writeBinary(const std::string& str) {
276 if(str.size() > (std::numeric_limits<uint32_t>::max)())
277 throw TProtocolException(TProtocolException::SIZE_LIMIT);
278 auto ssize = static_cast<uint32_t>(str.size());
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;
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 */
298 template <class Transport_>
writeFieldBeginInternal(const char * name,const TType fieldType,const int16_t fieldId,int8_t typeOverride)299 int32_t TCompactProtocolT<Transport_>::writeFieldBeginInternal(
300 const char* name,
301 const TType fieldType,
302 const int16_t fieldId,
303 int8_t typeOverride) {
304 (void) name;
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
313 wsize += writeByte(static_cast<int8_t>((fieldId - lastFieldId_)
314 << 4 | typeToWrite));
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 */
329 template <class Transport_>
writeCollectionBegin(const TType elemType,int32_t size)330 uint32_t TCompactProtocolT<Transport_>::writeCollectionBegin(const TType elemType,
331 int32_t size) {
332 uint32_t wsize = 0;
333 if (size <= 14) {
334 wsize += writeByte(static_cast<int8_t>(size
335 << 4 | getCompactType(elemType)));
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 */
346 template <class Transport_>
writeVarint32(uint32_t n)347 uint32_t TCompactProtocolT<Transport_>::writeVarint32(uint32_t n) {
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 */
367 template <class Transport_>
writeVarint64(uint64_t n)368 uint32_t TCompactProtocolT<Transport_>::writeVarint64(uint64_t n) {
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 */
389 template <class Transport_>
i64ToZigzag(const int64_t l)390 uint64_t TCompactProtocolT<Transport_>::i64ToZigzag(const int64_t l) {
391 return (static_cast<uint64_t>(l) << 1) ^ (l >> 63);
392 }
393
394 /**
395 * Convert n into a zigzag int. This allows negative numbers to be
396 * represented compactly as a varint.
397 */
398 template <class Transport_>
i32ToZigzag(const int32_t n)399 uint32_t TCompactProtocolT<Transport_>::i32ToZigzag(const int32_t n) {
400 return (static_cast<uint32_t>(n) << 1) ^ (n >> 31);
401 }
402
403 /**
404 * Given a TType value, find the appropriate detail::compact::Types value
405 */
406 template <class Transport_>
getCompactType(const TType ttype)407 int8_t TCompactProtocolT<Transport_>::getCompactType(const TType ttype) {
408 return detail::compact::TTypeToCType[ttype];
409 }
410
411 //
412 // Reading Methods
413 //
414
415 /**
416 * Read a message header.
417 */
418 template <class Transport_>
readMessageBegin(std::string & name,TMessageType & messageType,int32_t & seqid)419 uint32_t TCompactProtocolT<Transport_>::readMessageBegin(
420 std::string& name,
421 TMessageType& messageType,
422 int32_t& seqid) {
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
439 messageType = (TMessageType)((versionAndType >> TYPE_SHIFT_AMOUNT) & TYPE_BITS);
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 */
450 template <class Transport_>
readStructBegin(std::string & name)451 uint32_t TCompactProtocolT<Transport_>::readStructBegin(std::string& name) {
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 */
462 template <class Transport_>
readStructEnd()463 uint32_t TCompactProtocolT<Transport_>::readStructEnd() {
464 lastFieldId_ = lastField_.top();
465 lastField_.pop();
466 return 0;
467 }
468
469 /**
470 * Read a field header off the wire.
471 */
472 template <class Transport_>
readFieldBegin(std::string & name,TType & fieldType,int16_t & fieldId)473 uint32_t TCompactProtocolT<Transport_>::readFieldBegin(std::string& name,
474 TType& fieldType,
475 int16_t& fieldId) {
476 (void) name;
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.
492 auto modifier = (int16_t)(((uint8_t)byte & 0xf0) >> 4);
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
502 if (type == detail::compact::CT_BOOLEAN_TRUE ||
503 type == detail::compact::CT_BOOLEAN_FALSE) {
504 // save the boolean value in a special instance variable.
505 boolValue_.hasBoolValue = true;
506 boolValue_.boolValue =
507 (type == detail::compact::CT_BOOLEAN_TRUE ? true : false);
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 */
520 template <class Transport_>
readMapBegin(TType & keyType,TType & valType,uint32_t & size)521 uint32_t TCompactProtocolT<Transport_>::readMapBegin(TType& keyType,
522 TType& valType,
523 uint32_t& size) {
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
542 TMap map(keyType, valType, size);
543 checkReadBytesAvailable(map);
544
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 */
554 template <class Transport_>
readListBegin(TType & elemType,uint32_t & size)555 uint32_t TCompactProtocolT<Transport_>::readListBegin(TType& elemType,
556 uint32_t& size) {
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
577 TList list(elemType, size);
578 checkReadBytesAvailable(list);
579
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 */
589 template <class Transport_>
readSetBegin(TType & elemType,uint32_t & size)590 uint32_t TCompactProtocolT<Transport_>::readSetBegin(TType& elemType,
591 uint32_t& size) {
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 */
600 template <class Transport_>
readBool(bool & value)601 uint32_t TCompactProtocolT<Transport_>::readBool(bool& value) {
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);
609 value = (val == detail::compact::CT_BOOLEAN_TRUE);
610 return 1;
611 }
612 }
613
614 /**
615 * Read a single byte off the wire. Nothing interesting here.
616 */
617 template <class Transport_>
readByte(int8_t & byte)618 uint32_t TCompactProtocolT<Transport_>::readByte(int8_t& byte) {
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 */
628 template <class Transport_>
readI16(int16_t & i16)629 uint32_t TCompactProtocolT<Transport_>::readI16(int16_t& i16) {
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 */
639 template <class Transport_>
readI32(int32_t & i32)640 uint32_t TCompactProtocolT<Transport_>::readI32(int32_t& i32) {
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 */
650 template <class Transport_>
readI64(int64_t & i64)651 uint32_t TCompactProtocolT<Transport_>::readI64(int64_t& i64) {
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 */
661 template <class Transport_>
readDouble(double & dub)662 uint32_t TCompactProtocolT<Transport_>::readDouble(double& dub) {
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");
665
666 union {
667 uint64_t bits;
668 uint8_t b[8];
669 } u;
670 trans_->readAll(u.b, 8);
671 u.bits = THRIFT_letohll(u.bits);
672 dub = bitwise_cast<double>(u.bits);
673 return 8;
674 }
675
676 template <class Transport_>
readString(std::string & str)677 uint32_t TCompactProtocolT<Transport_>::readString(std::string& str) {
678 return readBinary(str);
679 }
680
681 /**
682 * Read a byte[] from the wire.
683 */
684 template <class Transport_>
readBinary(std::string & str)685 uint32_t TCompactProtocolT<Transport_>::readBinary(std::string& str) {
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
705 if (size > string_buf_size_ || string_buf_ == nullptr) {
706 void* new_string_buf = std::realloc(string_buf_, (uint32_t)size);
707 if (new_string_buf == nullptr) {
708 throw std::bad_alloc();
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
716 trans_->checkReadBytesAvailable(rsize + (uint32_t)size);
717
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 */
725 template <class Transport_>
readVarint32(int32_t & i32)726 uint32_t TCompactProtocolT<Transport_>::readVarint32(int32_t& i32) {
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 */
737 template <class Transport_>
readVarint64(int64_t & i64)738 uint32_t TCompactProtocolT<Transport_>::readVarint64(int64_t& i64) {
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.
747 if (borrowed != nullptr) {
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 */
787 template <class Transport_>
zigzagToI32(uint32_t n)788 int32_t TCompactProtocolT<Transport_>::zigzagToI32(uint32_t n) {
789 return (n >> 1) ^ static_cast<uint32_t>(-static_cast<int32_t>(n & 1));
790 }
791
792 /**
793 * Convert from zigzag long to long.
794 */
795 template <class Transport_>
zigzagToI64(uint64_t n)796 int64_t TCompactProtocolT<Transport_>::zigzagToI64(uint64_t n) {
797 return (n >> 1) ^ static_cast<uint64_t>(-static_cast<int64_t>(n & 1));
798 }
799
800 template <class Transport_>
getTType(int8_t type)801 TType TCompactProtocolT<Transport_>::getTType(int8_t type) {
802 switch (type) {
803 case T_STOP:
804 return T_STOP;
805 case detail::compact::CT_BOOLEAN_FALSE:
806 case detail::compact::CT_BOOLEAN_TRUE:
807 return T_BOOL;
808 case detail::compact::CT_BYTE:
809 return T_BYTE;
810 case detail::compact::CT_I16:
811 return T_I16;
812 case detail::compact::CT_I32:
813 return T_I32;
814 case detail::compact::CT_I64:
815 return T_I64;
816 case detail::compact::CT_DOUBLE:
817 return T_DOUBLE;
818 case detail::compact::CT_BINARY:
819 return T_STRING;
820 case detail::compact::CT_LIST:
821 return T_LIST;
822 case detail::compact::CT_SET:
823 return T_SET;
824 case detail::compact::CT_MAP:
825 return T_MAP;
826 case detail::compact::CT_STRUCT:
827 return T_STRUCT;
828 default:
829 throw TException(std::string("don't know what type: ") + (char)type);
830 }
831 }
832
833 // Return the minimum number of bytes a type will consume on the wire
834 template <class Transport_>
getMinSerializedSize(TType type)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
857 }}} // apache::thrift::protocol
858
859 #endif // _THRIFT_PROTOCOL_TCOMPACTPROTOCOL_TCC_
860