blob: 9270ab899023327a9c053f0267072e31e73d00e2 [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>
Mario Emmenlauer04aabcb2018-07-05 14:09:04 +020023#include <cstdlib>
David Reisse4d4ea02009-04-02 21:37:17 +000024
Antonio Di Monaco796667b2016-01-04 23:05:19 +010025#include "thrift/config.h"
26
David Reisse4d4ea02009-04-02 21:37:17 +000027/*
28 * TCompactProtocol::i*ToZigzag depend on the fact that the right shift
29 * operator on a signed integer is an arithmetic (sign-extending) shift.
30 * If this is not the case, the current implementation will not work.
31 * If anyone encounters this error, we can try to figure out the best
32 * way to implement an arithmetic right shift on their platform.
33 */
34#if !defined(SIGNED_RIGHT_SHIFT_IS) || !defined(ARITHMETIC_RIGHT_SHIFT)
35# error "Unable to determine the behavior of a signed right shift"
36#endif
37#if SIGNED_RIGHT_SHIFT_IS != ARITHMETIC_RIGHT_SHIFT
David Reisse71115b2010-10-06 17:09:56 +000038# error "TCompactProtocol currently only works if a signed right shift is arithmetic"
David Reisse4d4ea02009-04-02 21:37:17 +000039#endif
40
41#ifdef __GNUC__
42#define UNLIKELY(val) (__builtin_expect((val), 0))
43#else
44#define UNLIKELY(val) (val)
45#endif
46
47namespace apache { namespace thrift { namespace protocol {
48
David Reisse71115b2010-10-06 17:09:56 +000049namespace detail { namespace compact {
50
51enum Types {
52 CT_STOP = 0x00,
53 CT_BOOLEAN_TRUE = 0x01,
54 CT_BOOLEAN_FALSE = 0x02,
55 CT_BYTE = 0x03,
56 CT_I16 = 0x04,
57 CT_I32 = 0x05,
58 CT_I64 = 0x06,
59 CT_DOUBLE = 0x07,
60 CT_BINARY = 0x08,
61 CT_LIST = 0x09,
62 CT_SET = 0x0A,
63 CT_MAP = 0x0B,
Roger Meier0069cc42010-10-13 18:10:18 +000064 CT_STRUCT = 0x0C
David Reisse71115b2010-10-06 17:09:56 +000065};
66
67const int8_t TTypeToCType[16] = {
68 CT_STOP, // T_STOP
69 0, // unused
70 CT_BOOLEAN_TRUE, // T_BOOL
71 CT_BYTE, // T_BYTE
72 CT_DOUBLE, // T_DOUBLE
73 0, // unused
74 CT_I16, // T_I16
75 0, // unused
76 CT_I32, // T_I32
77 0, // unused
78 CT_I64, // T_I64
79 CT_BINARY, // T_STRING
80 CT_STRUCT, // T_STRUCT
81 CT_MAP, // T_MAP
82 CT_SET, // T_SET
83 CT_LIST, // T_LIST
84};
85
86}} // end detail::compact namespace
David Reisse4d4ea02009-04-02 21:37:17 +000087
88
David Reisse71115b2010-10-06 17:09:56 +000089template <class Transport_>
90uint32_t TCompactProtocolT<Transport_>::writeMessageBegin(
91 const std::string& name,
92 const TMessageType messageType,
93 const int32_t seqid) {
David Reisse4d4ea02009-04-02 21:37:17 +000094 uint32_t wsize = 0;
95 wsize += writeByte(PROTOCOL_ID);
96 wsize += writeByte((VERSION_N & VERSION_MASK) | (((int32_t)messageType << TYPE_SHIFT_AMOUNT) & TYPE_MASK));
97 wsize += writeVarint32(seqid);
98 wsize += writeString(name);
99 return wsize;
100}
101
102/**
103 * Write a field header containing the field id and field type. If the
104 * difference between the current field id and the last one is small (< 15),
105 * then the field id will be encoded in the 4 MSB as a delta. Otherwise, the
106 * field id will follow the type header as a zigzag varint.
107 */
David Reisse71115b2010-10-06 17:09:56 +0000108template <class Transport_>
109uint32_t TCompactProtocolT<Transport_>::writeFieldBegin(const char* name,
110 const TType fieldType,
111 const int16_t fieldId) {
David Reisse4d4ea02009-04-02 21:37:17 +0000112 if (fieldType == T_BOOL) {
113 booleanField_.name = name;
114 booleanField_.fieldType = fieldType;
115 booleanField_.fieldId = fieldId;
116 } else {
117 return writeFieldBeginInternal(name, fieldType, fieldId, -1);
118 }
119 return 0;
120}
121
122/**
123 * Write the STOP symbol so we know there are no more fields in this struct.
124 */
David Reisse71115b2010-10-06 17:09:56 +0000125template <class Transport_>
126uint32_t TCompactProtocolT<Transport_>::writeFieldStop() {
David Reisse4d4ea02009-04-02 21:37:17 +0000127 return writeByte(T_STOP);
128}
129
130/**
131 * Write a struct begin. This doesn't actually put anything on the wire. We
132 * use it as an opportunity to put special placeholder markers on the field
133 * stack so we can get the field id deltas correct.
134 */
David Reisse71115b2010-10-06 17:09:56 +0000135template <class Transport_>
136uint32_t TCompactProtocolT<Transport_>::writeStructBegin(const char* name) {
Roger Meier3b771a12010-11-17 22:11:26 +0000137 (void) name;
David Reisse4d4ea02009-04-02 21:37:17 +0000138 lastField_.push(lastFieldId_);
139 lastFieldId_ = 0;
140 return 0;
141}
142
143/**
144 * Write a struct end. This doesn't actually put anything on the wire. We use
145 * this as an opportunity to pop the last field from the current struct off
146 * of the field stack.
147 */
David Reisse71115b2010-10-06 17:09:56 +0000148template <class Transport_>
149uint32_t TCompactProtocolT<Transport_>::writeStructEnd() {
David Reisse4d4ea02009-04-02 21:37:17 +0000150 lastFieldId_ = lastField_.top();
151 lastField_.pop();
152 return 0;
153}
154
155/**
156 * Write a List header.
157 */
David Reisse71115b2010-10-06 17:09:56 +0000158template <class Transport_>
159uint32_t TCompactProtocolT<Transport_>::writeListBegin(const TType elemType,
160 const uint32_t size) {
David Reisse4d4ea02009-04-02 21:37:17 +0000161 return writeCollectionBegin(elemType, size);
162}
163
164/**
165 * Write a set header.
166 */
David Reisse71115b2010-10-06 17:09:56 +0000167template <class Transport_>
168uint32_t TCompactProtocolT<Transport_>::writeSetBegin(const TType elemType,
169 const uint32_t size) {
David Reisse4d4ea02009-04-02 21:37:17 +0000170 return writeCollectionBegin(elemType, size);
171}
172
173/**
174 * Write a map header. If the map is empty, omit the key and value type
175 * headers, as we don't need any additional information to skip it.
176 */
David Reisse71115b2010-10-06 17:09:56 +0000177template <class Transport_>
178uint32_t TCompactProtocolT<Transport_>::writeMapBegin(const TType keyType,
179 const TType valType,
180 const uint32_t size) {
David Reisse4d4ea02009-04-02 21:37:17 +0000181 uint32_t wsize = 0;
182
183 if (size == 0) {
184 wsize += writeByte(0);
185 } else {
186 wsize += writeVarint32(size);
187 wsize += writeByte(getCompactType(keyType) << 4 | getCompactType(valType));
188 }
189 return wsize;
190}
191
192/**
193 * Write a boolean value. Potentially, this could be a boolean field, in
194 * which case the field header info isn't written yet. If so, decide what the
195 * right type header is for the value and then write the field header.
196 * Otherwise, write a single byte.
197 */
David Reisse71115b2010-10-06 17:09:56 +0000198template <class Transport_>
199uint32_t TCompactProtocolT<Transport_>::writeBool(const bool value) {
David Reisse4d4ea02009-04-02 21:37:17 +0000200 uint32_t wsize = 0;
201
Sebastian Zenker042580f2019-01-29 15:48:12 +0100202 if (booleanField_.name != nullptr) {
David Reisse4d4ea02009-04-02 21:37:17 +0000203 // we haven't written the field header yet
Roger Meier64a799d2013-06-04 20:59:01 +0200204 wsize
205 += writeFieldBeginInternal(booleanField_.name,
206 booleanField_.fieldType,
207 booleanField_.fieldId,
208 static_cast<int8_t>(value
209 ? detail::compact::CT_BOOLEAN_TRUE
210 : detail::compact::CT_BOOLEAN_FALSE));
Sebastian Zenker042580f2019-01-29 15:48:12 +0100211 booleanField_.name = nullptr;
David Reisse4d4ea02009-04-02 21:37:17 +0000212 } else {
213 // we're not part of a field, so just write the value
Roger Meier64a799d2013-06-04 20:59:01 +0200214 wsize
215 += writeByte(static_cast<int8_t>(value
216 ? detail::compact::CT_BOOLEAN_TRUE
217 : detail::compact::CT_BOOLEAN_FALSE));
David Reisse4d4ea02009-04-02 21:37:17 +0000218 }
219 return wsize;
220}
221
David Reisse71115b2010-10-06 17:09:56 +0000222template <class Transport_>
223uint32_t TCompactProtocolT<Transport_>::writeByte(const int8_t byte) {
David Reisse4d4ea02009-04-02 21:37:17 +0000224 trans_->write((uint8_t*)&byte, 1);
225 return 1;
226}
227
228/**
229 * Write an i16 as a zigzag varint.
230 */
David Reisse71115b2010-10-06 17:09:56 +0000231template <class Transport_>
232uint32_t TCompactProtocolT<Transport_>::writeI16(const int16_t i16) {
David Reisse4d4ea02009-04-02 21:37:17 +0000233 return writeVarint32(i32ToZigzag(i16));
234}
235
236/**
237 * Write an i32 as a zigzag varint.
238 */
David Reisse71115b2010-10-06 17:09:56 +0000239template <class Transport_>
240uint32_t TCompactProtocolT<Transport_>::writeI32(const int32_t i32) {
David Reisse4d4ea02009-04-02 21:37:17 +0000241 return writeVarint32(i32ToZigzag(i32));
242}
243
244/**
245 * Write an i64 as a zigzag varint.
246 */
David Reisse71115b2010-10-06 17:09:56 +0000247template <class Transport_>
248uint32_t TCompactProtocolT<Transport_>::writeI64(const int64_t i64) {
David Reisse4d4ea02009-04-02 21:37:17 +0000249 return writeVarint64(i64ToZigzag(i64));
250}
251
252/**
253 * Write a double to the wire as 8 bytes.
254 */
David Reisse71115b2010-10-06 17:09:56 +0000255template <class Transport_>
256uint32_t TCompactProtocolT<Transport_>::writeDouble(const double dub) {
cyy863262d2019-01-06 10:40:58 +0800257 static_assert(sizeof(double) == sizeof(uint64_t), "sizeof(double) == sizeof(uint64_t)");
258 static_assert(std::numeric_limits<double>::is_iec559, "std::numeric_limits<double>::is_iec559");
David Reisse4d4ea02009-04-02 21:37:17 +0000259
Sebastian Zenker042580f2019-01-29 15:48:12 +0100260 auto bits = bitwise_cast<uint64_t>(dub);
jfarrellad3a9552015-09-24 23:27:34 -0400261 bits = THRIFT_htolell(bits);
David Reisse4d4ea02009-04-02 21:37:17 +0000262 trans_->write((uint8_t*)&bits, 8);
263 return 8;
264}
265
266/**
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +0100267 * Write a string to the wire with a varint size preceding.
David Reisse4d4ea02009-04-02 21:37:17 +0000268 */
David Reisse71115b2010-10-06 17:09:56 +0000269template <class Transport_>
270uint32_t TCompactProtocolT<Transport_>::writeString(const std::string& str) {
David Reisse4d4ea02009-04-02 21:37:17 +0000271 return writeBinary(str);
272}
273
David Reisse71115b2010-10-06 17:09:56 +0000274template <class Transport_>
275uint32_t TCompactProtocolT<Transport_>::writeBinary(const std::string& str) {
Ben Craig7f10de72013-10-14 20:27:18 -0500276 if(str.size() > (std::numeric_limits<uint32_t>::max)())
277 throw TProtocolException(TProtocolException::SIZE_LIMIT);
Sebastian Zenker042580f2019-01-29 15:48:12 +0100278 auto ssize = static_cast<uint32_t>(str.size());
Ben Craig7f10de72013-10-14 20:27:18 -0500279 uint32_t wsize = writeVarint32(ssize) ;
280 // checking ssize + wsize > uint_max, but we don't want to overflow while checking for overflows.
281 // transforming the check to ssize > uint_max - wsize
282 if(ssize > (std::numeric_limits<uint32_t>::max)() - wsize)
283 throw TProtocolException(TProtocolException::SIZE_LIMIT);
284 wsize += ssize;
David Reisse4d4ea02009-04-02 21:37:17 +0000285 trans_->write((uint8_t*)str.data(), ssize);
286 return wsize;
287}
288
289//
290// Internal Writing methods
291//
292
293/**
294 * The workhorse of writeFieldBegin. It has the option of doing a
295 * 'type override' of the type header. This is used specifically in the
296 * boolean field case.
297 */
David Reisse71115b2010-10-06 17:09:56 +0000298template <class Transport_>
299int32_t TCompactProtocolT<Transport_>::writeFieldBeginInternal(
300 const char* name,
301 const TType fieldType,
302 const int16_t fieldId,
303 int8_t typeOverride) {
Roger Meier3b771a12010-11-17 22:11:26 +0000304 (void) name;
David Reisse4d4ea02009-04-02 21:37:17 +0000305 uint32_t wsize = 0;
306
307 // if there's a type override, use that.
308 int8_t typeToWrite = (typeOverride == -1 ? getCompactType(fieldType) : typeOverride);
309
310 // check if we can use delta encoding for the field id
311 if (fieldId > lastFieldId_ && fieldId - lastFieldId_ <= 15) {
312 // write them together
Roger Meier64a799d2013-06-04 20:59:01 +0200313 wsize += writeByte(static_cast<int8_t>((fieldId - lastFieldId_)
314 << 4 | typeToWrite));
David Reisse4d4ea02009-04-02 21:37:17 +0000315 } else {
316 // write them separate
317 wsize += writeByte(typeToWrite);
318 wsize += writeI16(fieldId);
319 }
320
321 lastFieldId_ = fieldId;
322 return wsize;
323}
324
325/**
326 * Abstract method for writing the start of lists and sets. List and sets on
327 * the wire differ only by the type indicator.
328 */
David Reisse71115b2010-10-06 17:09:56 +0000329template <class Transport_>
Roger Meier64a799d2013-06-04 20:59:01 +0200330uint32_t TCompactProtocolT<Transport_>::writeCollectionBegin(const TType elemType,
David Reisse71115b2010-10-06 17:09:56 +0000331 int32_t size) {
David Reisse4d4ea02009-04-02 21:37:17 +0000332 uint32_t wsize = 0;
333 if (size <= 14) {
Roger Meier64a799d2013-06-04 20:59:01 +0200334 wsize += writeByte(static_cast<int8_t>(size
335 << 4 | getCompactType(elemType)));
David Reisse4d4ea02009-04-02 21:37:17 +0000336 } else {
337 wsize += writeByte(0xf0 | getCompactType(elemType));
338 wsize += writeVarint32(size);
339 }
340 return wsize;
341}
342
343/**
344 * Write an i32 as a varint. Results in 1-5 bytes on the wire.
345 */
David Reisse71115b2010-10-06 17:09:56 +0000346template <class Transport_>
347uint32_t TCompactProtocolT<Transport_>::writeVarint32(uint32_t n) {
David Reisse4d4ea02009-04-02 21:37:17 +0000348 uint8_t buf[5];
349 uint32_t wsize = 0;
350
351 while (true) {
352 if ((n & ~0x7F) == 0) {
353 buf[wsize++] = (int8_t)n;
354 break;
355 } else {
356 buf[wsize++] = (int8_t)((n & 0x7F) | 0x80);
357 n >>= 7;
358 }
359 }
360 trans_->write(buf, wsize);
361 return wsize;
362}
363
364/**
365 * Write an i64 as a varint. Results in 1-10 bytes on the wire.
366 */
David Reisse71115b2010-10-06 17:09:56 +0000367template <class Transport_>
368uint32_t TCompactProtocolT<Transport_>::writeVarint64(uint64_t n) {
David Reisse4d4ea02009-04-02 21:37:17 +0000369 uint8_t buf[10];
370 uint32_t wsize = 0;
371
372 while (true) {
373 if ((n & ~0x7FL) == 0) {
374 buf[wsize++] = (int8_t)n;
375 break;
376 } else {
377 buf[wsize++] = (int8_t)((n & 0x7F) | 0x80);
378 n >>= 7;
379 }
380 }
381 trans_->write(buf, wsize);
382 return wsize;
383}
384
385/**
386 * Convert l into a zigzag long. This allows negative numbers to be
387 * represented compactly as a varint.
388 */
David Reisse71115b2010-10-06 17:09:56 +0000389template <class Transport_>
390uint64_t TCompactProtocolT<Transport_>::i64ToZigzag(const int64_t l) {
Jim Apple147c2842017-03-18 12:56:50 -0700391 return (static_cast<uint64_t>(l) << 1) ^ (l >> 63);
David Reisse4d4ea02009-04-02 21:37:17 +0000392}
393
394/**
395 * Convert n into a zigzag int. This allows negative numbers to be
396 * represented compactly as a varint.
397 */
David Reisse71115b2010-10-06 17:09:56 +0000398template <class Transport_>
399uint32_t TCompactProtocolT<Transport_>::i32ToZigzag(const int32_t n) {
Jim Apple147c2842017-03-18 12:56:50 -0700400 return (static_cast<uint32_t>(n) << 1) ^ (n >> 31);
David Reisse4d4ea02009-04-02 21:37:17 +0000401}
402
403/**
David Reisse71115b2010-10-06 17:09:56 +0000404 * Given a TType value, find the appropriate detail::compact::Types value
David Reisse4d4ea02009-04-02 21:37:17 +0000405 */
David Reisse71115b2010-10-06 17:09:56 +0000406template <class Transport_>
Roger Meier64a799d2013-06-04 20:59:01 +0200407int8_t TCompactProtocolT<Transport_>::getCompactType(const TType ttype) {
David Reisse71115b2010-10-06 17:09:56 +0000408 return detail::compact::TTypeToCType[ttype];
David Reisse4d4ea02009-04-02 21:37:17 +0000409}
410
411//
412// Reading Methods
413//
414
415/**
416 * Read a message header.
417 */
David Reisse71115b2010-10-06 17:09:56 +0000418template <class Transport_>
419uint32_t TCompactProtocolT<Transport_>::readMessageBegin(
420 std::string& name,
421 TMessageType& messageType,
422 int32_t& seqid) {
David Reisse4d4ea02009-04-02 21:37:17 +0000423 uint32_t rsize = 0;
424 int8_t protocolId;
425 int8_t versionAndType;
426 int8_t version;
427
428 rsize += readByte(protocolId);
429 if (protocolId != PROTOCOL_ID) {
430 throw TProtocolException(TProtocolException::BAD_VERSION, "Bad protocol identifier");
431 }
432
433 rsize += readByte(versionAndType);
434 version = (int8_t)(versionAndType & VERSION_MASK);
435 if (version != VERSION_N) {
436 throw TProtocolException(TProtocolException::BAD_VERSION, "Bad protocol version");
437 }
438
Jens Geyera86886e2014-09-17 22:25:48 +0200439 messageType = (TMessageType)((versionAndType >> TYPE_SHIFT_AMOUNT) & TYPE_BITS);
David Reisse4d4ea02009-04-02 21:37:17 +0000440 rsize += readVarint32(seqid);
441 rsize += readString(name);
442
443 return rsize;
444}
445
446/**
447 * Read a struct begin. There's nothing on the wire for this, but it is our
448 * opportunity to push a new struct begin marker on the field stack.
449 */
David Reisse71115b2010-10-06 17:09:56 +0000450template <class Transport_>
451uint32_t TCompactProtocolT<Transport_>::readStructBegin(std::string& name) {
David Reisse4d4ea02009-04-02 21:37:17 +0000452 name = "";
453 lastField_.push(lastFieldId_);
454 lastFieldId_ = 0;
455 return 0;
456}
457
458/**
459 * Doesn't actually consume any wire data, just removes the last field for
460 * this struct from the field stack.
461 */
David Reisse71115b2010-10-06 17:09:56 +0000462template <class Transport_>
463uint32_t TCompactProtocolT<Transport_>::readStructEnd() {
David Reisse4d4ea02009-04-02 21:37:17 +0000464 lastFieldId_ = lastField_.top();
465 lastField_.pop();
466 return 0;
467}
468
469/**
470 * Read a field header off the wire.
471 */
David Reisse71115b2010-10-06 17:09:56 +0000472template <class Transport_>
473uint32_t TCompactProtocolT<Transport_>::readFieldBegin(std::string& name,
474 TType& fieldType,
475 int16_t& fieldId) {
Roger Meier3b771a12010-11-17 22:11:26 +0000476 (void) name;
David Reisse4d4ea02009-04-02 21:37:17 +0000477 uint32_t rsize = 0;
478 int8_t byte;
479 int8_t type;
480
481 rsize += readByte(byte);
482 type = (byte & 0x0f);
483
484 // if it's a stop, then we can return immediately, as the struct is over.
485 if (type == T_STOP) {
486 fieldType = T_STOP;
487 fieldId = 0;
488 return rsize;
489 }
490
491 // mask off the 4 MSB of the type header. it could contain a field id delta.
Sebastian Zenker042580f2019-01-29 15:48:12 +0100492 auto modifier = (int16_t)(((uint8_t)byte & 0xf0) >> 4);
David Reisse4d4ea02009-04-02 21:37:17 +0000493 if (modifier == 0) {
494 // not a delta, look ahead for the zigzag varint field id.
495 rsize += readI16(fieldId);
496 } else {
497 fieldId = (int16_t)(lastFieldId_ + modifier);
498 }
499 fieldType = getTType(type);
500
501 // if this happens to be a boolean field, the value is encoded in the type
David Reisse71115b2010-10-06 17:09:56 +0000502 if (type == detail::compact::CT_BOOLEAN_TRUE ||
503 type == detail::compact::CT_BOOLEAN_FALSE) {
David Reisse4d4ea02009-04-02 21:37:17 +0000504 // save the boolean value in a special instance variable.
505 boolValue_.hasBoolValue = true;
David Reisse71115b2010-10-06 17:09:56 +0000506 boolValue_.boolValue =
507 (type == detail::compact::CT_BOOLEAN_TRUE ? true : false);
David Reisse4d4ea02009-04-02 21:37:17 +0000508 }
509
510 // push the new field onto the field stack so we can keep the deltas going.
511 lastFieldId_ = fieldId;
512 return rsize;
513}
514
515/**
516 * Read a map header off the wire. If the size is zero, skip reading the key
517 * and value type. This means that 0-length maps will yield TMaps without the
518 * "correct" types.
519 */
David Reisse71115b2010-10-06 17:09:56 +0000520template <class Transport_>
521uint32_t TCompactProtocolT<Transport_>::readMapBegin(TType& keyType,
522 TType& valType,
523 uint32_t& size) {
David Reisse4d4ea02009-04-02 21:37:17 +0000524 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
zeshuai00786352b42020-06-15 17:00:33 +0800542 TMap map(keyType, valType, size);
543 checkReadBytesAvailable(map);
544
David Reisse4d4ea02009-04-02 21:37:17 +0000545 return rsize;
546}
547
548/**
549 * Read a list header off the wire. If the list size is 0-14, the size will
550 * be packed into the element type header. If it's a longer list, the 4 MSB
551 * of the element type header will be 0xF, and a varint will follow with the
552 * true size.
553 */
David Reisse71115b2010-10-06 17:09:56 +0000554template <class Transport_>
555uint32_t TCompactProtocolT<Transport_>::readListBegin(TType& elemType,
556 uint32_t& size) {
David Reisse4d4ea02009-04-02 21:37:17 +0000557 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
zeshuai00786352b42020-06-15 17:00:33 +0800577 TList list(elemType, size);
578 checkReadBytesAvailable(list);
579
David Reisse4d4ea02009-04-02 21:37:17 +0000580 return rsize;
581}
582
583/**
584 * Read a set header off the wire. If the set size is 0-14, the size will
585 * be packed into the element type header. If it's a longer set, the 4 MSB
586 * of the element type header will be 0xF, and a varint will follow with the
587 * true size.
588 */
David Reisse71115b2010-10-06 17:09:56 +0000589template <class Transport_>
590uint32_t TCompactProtocolT<Transport_>::readSetBegin(TType& elemType,
591 uint32_t& size) {
David Reisse4d4ea02009-04-02 21:37:17 +0000592 return readListBegin(elemType, size);
593}
594
595/**
596 * Read a boolean off the wire. If this is a boolean field, the value should
597 * already have been read during readFieldBegin, so we'll just consume the
598 * pre-stored value. Otherwise, read a byte.
599 */
David Reisse71115b2010-10-06 17:09:56 +0000600template <class Transport_>
601uint32_t TCompactProtocolT<Transport_>::readBool(bool& value) {
David Reisse4d4ea02009-04-02 21:37:17 +0000602 if (boolValue_.hasBoolValue == true) {
603 value = boolValue_.boolValue;
604 boolValue_.hasBoolValue = false;
605 return 0;
606 } else {
607 int8_t val;
608 readByte(val);
David Reisse71115b2010-10-06 17:09:56 +0000609 value = (val == detail::compact::CT_BOOLEAN_TRUE);
David Reisse4d4ea02009-04-02 21:37:17 +0000610 return 1;
611 }
612}
613
614/**
615 * Read a single byte off the wire. Nothing interesting here.
616 */
David Reisse71115b2010-10-06 17:09:56 +0000617template <class Transport_>
618uint32_t TCompactProtocolT<Transport_>::readByte(int8_t& byte) {
David Reisse4d4ea02009-04-02 21:37:17 +0000619 uint8_t b[1];
620 trans_->readAll(b, 1);
621 byte = *(int8_t*)b;
622 return 1;
623}
624
625/**
626 * Read an i16 from the wire as a zigzag varint.
627 */
David Reisse71115b2010-10-06 17:09:56 +0000628template <class Transport_>
629uint32_t TCompactProtocolT<Transport_>::readI16(int16_t& i16) {
David Reisse4d4ea02009-04-02 21:37:17 +0000630 int32_t value;
631 uint32_t rsize = readVarint32(value);
632 i16 = (int16_t)zigzagToI32(value);
633 return rsize;
634}
635
636/**
637 * Read an i32 from the wire as a zigzag varint.
638 */
David Reisse71115b2010-10-06 17:09:56 +0000639template <class Transport_>
640uint32_t TCompactProtocolT<Transport_>::readI32(int32_t& i32) {
David Reisse4d4ea02009-04-02 21:37:17 +0000641 int32_t value;
642 uint32_t rsize = readVarint32(value);
643 i32 = zigzagToI32(value);
644 return rsize;
645}
646
647/**
648 * Read an i64 from the wire as a zigzag varint.
649 */
David Reisse71115b2010-10-06 17:09:56 +0000650template <class Transport_>
651uint32_t TCompactProtocolT<Transport_>::readI64(int64_t& i64) {
David Reisse4d4ea02009-04-02 21:37:17 +0000652 int64_t value;
653 uint32_t rsize = readVarint64(value);
654 i64 = zigzagToI64(value);
655 return rsize;
656}
657
658/**
659 * No magic here - just read a double off the wire.
660 */
David Reisse71115b2010-10-06 17:09:56 +0000661template <class Transport_>
662uint32_t TCompactProtocolT<Transport_>::readDouble(double& dub) {
cyy863262d2019-01-06 10:40:58 +0800663 static_assert(sizeof(double) == sizeof(uint64_t), "sizeof(double) == sizeof(uint64_t)");
664 static_assert(std::numeric_limits<double>::is_iec559, "std::numeric_limits<double>::is_iec559");
David Reisse4d4ea02009-04-02 21:37:17 +0000665
Carl Yeksigian3e937112013-06-03 13:46:51 -0400666 union {
667 uint64_t bits;
668 uint8_t b[8];
669 } u;
670 trans_->readAll(u.b, 8);
jfarrellad3a9552015-09-24 23:27:34 -0400671 u.bits = THRIFT_letohll(u.bits);
Carl Yeksigian3e937112013-06-03 13:46:51 -0400672 dub = bitwise_cast<double>(u.bits);
David Reisse4d4ea02009-04-02 21:37:17 +0000673 return 8;
674}
675
David Reisse71115b2010-10-06 17:09:56 +0000676template <class Transport_>
677uint32_t TCompactProtocolT<Transport_>::readString(std::string& str) {
David Reisse4d4ea02009-04-02 21:37:17 +0000678 return readBinary(str);
679}
680
681/**
682 * Read a byte[] from the wire.
683 */
David Reisse71115b2010-10-06 17:09:56 +0000684template <class Transport_>
685uint32_t TCompactProtocolT<Transport_>::readBinary(std::string& str) {
David Reisse4d4ea02009-04-02 21:37:17 +0000686 int32_t rsize = 0;
687 int32_t size;
688
689 rsize += readVarint32(size);
690 // Catch empty string case
691 if (size == 0) {
692 str = "";
693 return rsize;
694 }
695
696 // Catch error cases
697 if (size < 0) {
698 throw TProtocolException(TProtocolException::NEGATIVE_SIZE);
699 }
700 if (string_limit_ > 0 && size > string_limit_) {
701 throw TProtocolException(TProtocolException::SIZE_LIMIT);
702 }
703
704 // Use the heap here to prevent stack overflow for v. large strings
Sebastian Zenker042580f2019-01-29 15:48:12 +0100705 if (size > string_buf_size_ || string_buf_ == nullptr) {
David Reisse4d4ea02009-04-02 21:37:17 +0000706 void* new_string_buf = std::realloc(string_buf_, (uint32_t)size);
Sebastian Zenker042580f2019-01-29 15:48:12 +0100707 if (new_string_buf == nullptr) {
David Reissf6735092010-10-06 17:10:49 +0000708 throw std::bad_alloc();
David Reisse4d4ea02009-04-02 21:37:17 +0000709 }
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
zeshuai00786352b42020-06-15 17:00:33 +0800716 trans_->checkReadBytesAvailable(rsize + (uint32_t)size);
717
David Reisse4d4ea02009-04-02 21:37:17 +0000718 return rsize + (uint32_t)size;
719}
720
721/**
722 * Read an i32 from the wire as a varint. The MSB of each byte is set
723 * if there is another byte to follow. This can read up to 5 bytes.
724 */
David Reisse71115b2010-10-06 17:09:56 +0000725template <class Transport_>
726uint32_t TCompactProtocolT<Transport_>::readVarint32(int32_t& i32) {
David Reisse4d4ea02009-04-02 21:37:17 +0000727 int64_t val;
728 uint32_t rsize = readVarint64(val);
729 i32 = (int32_t)val;
730 return rsize;
731}
732
733/**
734 * Read an i64 from the wire as a proper varint. The MSB of each byte is set
735 * if there is another byte to follow. This can read up to 10 bytes.
736 */
David Reisse71115b2010-10-06 17:09:56 +0000737template <class Transport_>
738uint32_t TCompactProtocolT<Transport_>::readVarint64(int64_t& i64) {
David Reisse4d4ea02009-04-02 21:37:17 +0000739 uint32_t rsize = 0;
740 uint64_t val = 0;
741 int shift = 0;
742 uint8_t buf[10]; // 64 bits / (7 bits/byte) = 10 bytes.
743 uint32_t buf_size = sizeof(buf);
744 const uint8_t* borrowed = trans_->borrow(buf, &buf_size);
745
746 // Fast path.
Sebastian Zenker042580f2019-01-29 15:48:12 +0100747 if (borrowed != nullptr) {
David Reisse4d4ea02009-04-02 21:37:17 +0000748 while (true) {
749 uint8_t byte = borrowed[rsize];
750 rsize++;
751 val |= (uint64_t)(byte & 0x7f) << shift;
752 shift += 7;
753 if (!(byte & 0x80)) {
754 i64 = val;
755 trans_->consume(rsize);
756 return rsize;
757 }
758 // Have to check for invalid data so we don't crash.
759 if (UNLIKELY(rsize == sizeof(buf))) {
760 throw TProtocolException(TProtocolException::INVALID_DATA, "Variable-length int over 10 bytes.");
761 }
762 }
763 }
764
765 // Slow path.
766 else {
767 while (true) {
768 uint8_t byte;
769 rsize += trans_->readAll(&byte, 1);
770 val |= (uint64_t)(byte & 0x7f) << shift;
771 shift += 7;
772 if (!(byte & 0x80)) {
773 i64 = val;
774 return rsize;
775 }
776 // Might as well check for invalid data on the slow path too.
777 if (UNLIKELY(rsize >= sizeof(buf))) {
778 throw TProtocolException(TProtocolException::INVALID_DATA, "Variable-length int over 10 bytes.");
779 }
780 }
781 }
782}
783
784/**
785 * Convert from zigzag int to int.
786 */
David Reisse71115b2010-10-06 17:09:56 +0000787template <class Transport_>
788int32_t TCompactProtocolT<Transport_>::zigzagToI32(uint32_t n) {
Ben Craig7f10de72013-10-14 20:27:18 -0500789 return (n >> 1) ^ static_cast<uint32_t>(-static_cast<int32_t>(n & 1));
David Reisse4d4ea02009-04-02 21:37:17 +0000790}
791
792/**
793 * Convert from zigzag long to long.
794 */
David Reisse71115b2010-10-06 17:09:56 +0000795template <class Transport_>
796int64_t TCompactProtocolT<Transport_>::zigzagToI64(uint64_t n) {
Ben Craig7f10de72013-10-14 20:27:18 -0500797 return (n >> 1) ^ static_cast<uint64_t>(-static_cast<int64_t>(n & 1));
David Reisse4d4ea02009-04-02 21:37:17 +0000798}
799
David Reisse71115b2010-10-06 17:09:56 +0000800template <class Transport_>
801TType TCompactProtocolT<Transport_>::getTType(int8_t type) {
David Reisse4d4ea02009-04-02 21:37:17 +0000802 switch (type) {
803 case T_STOP:
804 return T_STOP;
David Reisse71115b2010-10-06 17:09:56 +0000805 case detail::compact::CT_BOOLEAN_FALSE:
806 case detail::compact::CT_BOOLEAN_TRUE:
David Reisse4d4ea02009-04-02 21:37:17 +0000807 return T_BOOL;
David Reisse71115b2010-10-06 17:09:56 +0000808 case detail::compact::CT_BYTE:
David Reisse4d4ea02009-04-02 21:37:17 +0000809 return T_BYTE;
David Reisse71115b2010-10-06 17:09:56 +0000810 case detail::compact::CT_I16:
David Reisse4d4ea02009-04-02 21:37:17 +0000811 return T_I16;
David Reisse71115b2010-10-06 17:09:56 +0000812 case detail::compact::CT_I32:
David Reisse4d4ea02009-04-02 21:37:17 +0000813 return T_I32;
David Reisse71115b2010-10-06 17:09:56 +0000814 case detail::compact::CT_I64:
David Reisse4d4ea02009-04-02 21:37:17 +0000815 return T_I64;
David Reisse71115b2010-10-06 17:09:56 +0000816 case detail::compact::CT_DOUBLE:
David Reisse4d4ea02009-04-02 21:37:17 +0000817 return T_DOUBLE;
David Reisse71115b2010-10-06 17:09:56 +0000818 case detail::compact::CT_BINARY:
David Reisse4d4ea02009-04-02 21:37:17 +0000819 return T_STRING;
David Reisse71115b2010-10-06 17:09:56 +0000820 case detail::compact::CT_LIST:
David Reisse4d4ea02009-04-02 21:37:17 +0000821 return T_LIST;
David Reisse71115b2010-10-06 17:09:56 +0000822 case detail::compact::CT_SET:
David Reisse4d4ea02009-04-02 21:37:17 +0000823 return T_SET;
David Reisse71115b2010-10-06 17:09:56 +0000824 case detail::compact::CT_MAP:
David Reisse4d4ea02009-04-02 21:37:17 +0000825 return T_MAP;
David Reisse71115b2010-10-06 17:09:56 +0000826 case detail::compact::CT_STRUCT:
David Reisse4d4ea02009-04-02 21:37:17 +0000827 return T_STRUCT;
828 default:
Roger Meiera7ab94d2013-03-22 22:34:16 +0100829 throw TException(std::string("don't know what type: ") + (char)type);
David Reisse4d4ea02009-04-02 21:37:17 +0000830 }
David Reisse4d4ea02009-04-02 21:37:17 +0000831}
832
zeshuai00786352b42020-06-15 17:00:33 +0800833// Return the minimum number of bytes a type will consume on the wire
834template <class Transport_>
835int TCompactProtocolT<Transport_>::getMinSerializedSize(TType type)
836{
837 switch (type)
838 {
839 case T_STOP: return 0;
840 case T_VOID: return 0;
841 case T_BOOL: return sizeof(int8_t);
842 case T_DOUBLE: return 8; // uses fixedLongToBytes() which always writes 8 bytes
843 case T_BYTE: return sizeof(int8_t);
844 case T_I16: return sizeof(int8_t); // zigzag
845 case T_I32: return sizeof(int8_t); // zigzag
846 case T_I64: return sizeof(int8_t); // zigzag
847 case T_STRING: return sizeof(int8_t); // string length
848 case T_STRUCT: return 0; // empty struct
849 case T_MAP: return sizeof(int8_t); // element count
850 case T_SET: return sizeof(int8_t); // element count
851 case T_LIST: return sizeof(int8_t); // element count
852 default: throw TProtocolException(TProtocolException::UNKNOWN, "unrecognized type code");
853 }
854}
855
856
David Reisse4d4ea02009-04-02 21:37:17 +0000857}}} // apache::thrift::protocol
David Reisse71115b2010-10-06 17:09:56 +0000858
859#endif // _THRIFT_PROTOCOL_TCOMPACTPROTOCOL_TCC_