blob: 64162ccb0b0c7a058bfd52aac1ddeb24223f4f8b [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,
Carel Combrink786764b2025-05-15 12:22:37 +000064 CT_STRUCT = 0x0C,
65 CT_UUID = 0x0D
David Reisse71115b2010-10-06 17:09:56 +000066};
67
Carel Combrink786764b2025-05-15 12:22:37 +000068const int8_t TTypeToCType[17] = {
David Reisse71115b2010-10-06 17:09:56 +000069 CT_STOP, // T_STOP
70 0, // unused
71 CT_BOOLEAN_TRUE, // T_BOOL
72 CT_BYTE, // T_BYTE
73 CT_DOUBLE, // T_DOUBLE
74 0, // unused
75 CT_I16, // T_I16
76 0, // unused
77 CT_I32, // T_I32
78 0, // unused
79 CT_I64, // T_I64
80 CT_BINARY, // T_STRING
81 CT_STRUCT, // T_STRUCT
82 CT_MAP, // T_MAP
83 CT_SET, // T_SET
84 CT_LIST, // T_LIST
Carel Combrink786764b2025-05-15 12:22:37 +000085 CT_UUID, // T_UUID
David Reisse71115b2010-10-06 17:09:56 +000086};
87
88}} // end detail::compact namespace
David Reisse4d4ea02009-04-02 21:37:17 +000089
90
David Reisse71115b2010-10-06 17:09:56 +000091template <class Transport_>
92uint32_t TCompactProtocolT<Transport_>::writeMessageBegin(
93 const std::string& name,
94 const TMessageType messageType,
95 const int32_t seqid) {
David Reisse4d4ea02009-04-02 21:37:17 +000096 uint32_t wsize = 0;
97 wsize += writeByte(PROTOCOL_ID);
Maximilian Bandle7c943352025-02-07 10:58:32 +010098 wsize += writeByte((VERSION_N & VERSION_MASK) | ((static_cast<int32_t>(messageType) << TYPE_SHIFT_AMOUNT) & TYPE_MASK));
David Reisse4d4ea02009-04-02 21:37:17 +000099 wsize += writeVarint32(seqid);
100 wsize += writeString(name);
101 return wsize;
102}
103
104/**
105 * Write a field header containing the field id and field type. If the
106 * difference between the current field id and the last one is small (< 15),
107 * then the field id will be encoded in the 4 MSB as a delta. Otherwise, the
108 * field id will follow the type header as a zigzag varint.
109 */
David Reisse71115b2010-10-06 17:09:56 +0000110template <class Transport_>
111uint32_t TCompactProtocolT<Transport_>::writeFieldBegin(const char* name,
112 const TType fieldType,
113 const int16_t fieldId) {
David Reisse4d4ea02009-04-02 21:37:17 +0000114 if (fieldType == T_BOOL) {
115 booleanField_.name = name;
116 booleanField_.fieldType = fieldType;
117 booleanField_.fieldId = fieldId;
118 } else {
119 return writeFieldBeginInternal(name, fieldType, fieldId, -1);
120 }
121 return 0;
122}
123
124/**
125 * Write the STOP symbol so we know there are no more fields in this struct.
126 */
David Reisse71115b2010-10-06 17:09:56 +0000127template <class Transport_>
128uint32_t TCompactProtocolT<Transport_>::writeFieldStop() {
David Reisse4d4ea02009-04-02 21:37:17 +0000129 return writeByte(T_STOP);
130}
131
132/**
133 * Write a struct begin. This doesn't actually put anything on the wire. We
134 * use it as an opportunity to put special placeholder markers on the field
135 * stack so we can get the field id deltas correct.
136 */
David Reisse71115b2010-10-06 17:09:56 +0000137template <class Transport_>
138uint32_t TCompactProtocolT<Transport_>::writeStructBegin(const char* name) {
Roger Meier3b771a12010-11-17 22:11:26 +0000139 (void) name;
David Reisse4d4ea02009-04-02 21:37:17 +0000140 lastField_.push(lastFieldId_);
141 lastFieldId_ = 0;
142 return 0;
143}
144
145/**
146 * Write a struct end. This doesn't actually put anything on the wire. We use
147 * this as an opportunity to pop the last field from the current struct off
148 * of the field stack.
149 */
David Reisse71115b2010-10-06 17:09:56 +0000150template <class Transport_>
151uint32_t TCompactProtocolT<Transport_>::writeStructEnd() {
David Reisse4d4ea02009-04-02 21:37:17 +0000152 lastFieldId_ = lastField_.top();
153 lastField_.pop();
154 return 0;
155}
156
157/**
158 * Write a List header.
159 */
David Reisse71115b2010-10-06 17:09:56 +0000160template <class Transport_>
161uint32_t TCompactProtocolT<Transport_>::writeListBegin(const TType elemType,
162 const uint32_t size) {
David Reisse4d4ea02009-04-02 21:37:17 +0000163 return writeCollectionBegin(elemType, size);
164}
165
166/**
167 * Write a set header.
168 */
David Reisse71115b2010-10-06 17:09:56 +0000169template <class Transport_>
170uint32_t TCompactProtocolT<Transport_>::writeSetBegin(const TType elemType,
171 const uint32_t size) {
David Reisse4d4ea02009-04-02 21:37:17 +0000172 return writeCollectionBegin(elemType, size);
173}
174
175/**
176 * Write a map header. If the map is empty, omit the key and value type
177 * headers, as we don't need any additional information to skip it.
178 */
David Reisse71115b2010-10-06 17:09:56 +0000179template <class Transport_>
180uint32_t TCompactProtocolT<Transport_>::writeMapBegin(const TType keyType,
181 const TType valType,
182 const uint32_t size) {
David Reisse4d4ea02009-04-02 21:37:17 +0000183 uint32_t wsize = 0;
184
185 if (size == 0) {
186 wsize += writeByte(0);
187 } else {
188 wsize += writeVarint32(size);
189 wsize += writeByte(getCompactType(keyType) << 4 | getCompactType(valType));
190 }
191 return wsize;
192}
193
194/**
195 * Write a boolean value. Potentially, this could be a boolean field, in
196 * which case the field header info isn't written yet. If so, decide what the
197 * right type header is for the value and then write the field header.
198 * Otherwise, write a single byte.
199 */
David Reisse71115b2010-10-06 17:09:56 +0000200template <class Transport_>
201uint32_t TCompactProtocolT<Transport_>::writeBool(const bool value) {
David Reisse4d4ea02009-04-02 21:37:17 +0000202 uint32_t wsize = 0;
203
Sebastian Zenker042580f2019-01-29 15:48:12 +0100204 if (booleanField_.name != nullptr) {
David Reisse4d4ea02009-04-02 21:37:17 +0000205 // we haven't written the field header yet
Roger Meier64a799d2013-06-04 20:59:01 +0200206 wsize
207 += writeFieldBeginInternal(booleanField_.name,
208 booleanField_.fieldType,
209 booleanField_.fieldId,
210 static_cast<int8_t>(value
211 ? detail::compact::CT_BOOLEAN_TRUE
212 : detail::compact::CT_BOOLEAN_FALSE));
Sebastian Zenker042580f2019-01-29 15:48:12 +0100213 booleanField_.name = nullptr;
David Reisse4d4ea02009-04-02 21:37:17 +0000214 } else {
215 // we're not part of a field, so just write the value
Roger Meier64a799d2013-06-04 20:59:01 +0200216 wsize
217 += writeByte(static_cast<int8_t>(value
218 ? detail::compact::CT_BOOLEAN_TRUE
219 : detail::compact::CT_BOOLEAN_FALSE));
David Reisse4d4ea02009-04-02 21:37:17 +0000220 }
221 return wsize;
222}
223
David Reisse71115b2010-10-06 17:09:56 +0000224template <class Transport_>
225uint32_t TCompactProtocolT<Transport_>::writeByte(const int8_t byte) {
Maximilian Bandle7c943352025-02-07 10:58:32 +0100226 trans_->write(reinterpret_cast<const uint8_t*>(&byte), 1);
David Reisse4d4ea02009-04-02 21:37:17 +0000227 return 1;
228}
229
230/**
231 * Write an i16 as a zigzag varint.
232 */
David Reisse71115b2010-10-06 17:09:56 +0000233template <class Transport_>
234uint32_t TCompactProtocolT<Transport_>::writeI16(const int16_t i16) {
David Reisse4d4ea02009-04-02 21:37:17 +0000235 return writeVarint32(i32ToZigzag(i16));
236}
237
238/**
239 * Write an i32 as a zigzag varint.
240 */
David Reisse71115b2010-10-06 17:09:56 +0000241template <class Transport_>
242uint32_t TCompactProtocolT<Transport_>::writeI32(const int32_t i32) {
David Reisse4d4ea02009-04-02 21:37:17 +0000243 return writeVarint32(i32ToZigzag(i32));
244}
245
246/**
247 * Write an i64 as a zigzag varint.
248 */
David Reisse71115b2010-10-06 17:09:56 +0000249template <class Transport_>
250uint32_t TCompactProtocolT<Transport_>::writeI64(const int64_t i64) {
David Reisse4d4ea02009-04-02 21:37:17 +0000251 return writeVarint64(i64ToZigzag(i64));
252}
253
254/**
255 * Write a double to the wire as 8 bytes.
256 */
David Reisse71115b2010-10-06 17:09:56 +0000257template <class Transport_>
258uint32_t TCompactProtocolT<Transport_>::writeDouble(const double dub) {
cyy863262d2019-01-06 10:40:58 +0800259 static_assert(sizeof(double) == sizeof(uint64_t), "sizeof(double) == sizeof(uint64_t)");
260 static_assert(std::numeric_limits<double>::is_iec559, "std::numeric_limits<double>::is_iec559");
David Reisse4d4ea02009-04-02 21:37:17 +0000261
Sebastian Zenker042580f2019-01-29 15:48:12 +0100262 auto bits = bitwise_cast<uint64_t>(dub);
jfarrellad3a9552015-09-24 23:27:34 -0400263 bits = THRIFT_htolell(bits);
Maximilian Bandle7c943352025-02-07 10:58:32 +0100264 trans_->write(reinterpret_cast<const uint8_t*>(&bits), 8);
David Reisse4d4ea02009-04-02 21:37:17 +0000265 return 8;
266}
267
268/**
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +0100269 * Write a string to the wire with a varint size preceding.
David Reisse4d4ea02009-04-02 21:37:17 +0000270 */
David Reisse71115b2010-10-06 17:09:56 +0000271template <class Transport_>
272uint32_t TCompactProtocolT<Transport_>::writeString(const std::string& str) {
David Reisse4d4ea02009-04-02 21:37:17 +0000273 return writeBinary(str);
274}
275
David Reisse71115b2010-10-06 17:09:56 +0000276template <class Transport_>
277uint32_t TCompactProtocolT<Transport_>::writeBinary(const std::string& str) {
Ben Craig7f10de72013-10-14 20:27:18 -0500278 if(str.size() > (std::numeric_limits<uint32_t>::max)())
279 throw TProtocolException(TProtocolException::SIZE_LIMIT);
Sebastian Zenker042580f2019-01-29 15:48:12 +0100280 auto ssize = static_cast<uint32_t>(str.size());
Ben Craig7f10de72013-10-14 20:27:18 -0500281 uint32_t wsize = writeVarint32(ssize) ;
282 // checking ssize + wsize > uint_max, but we don't want to overflow while checking for overflows.
283 // transforming the check to ssize > uint_max - wsize
284 if(ssize > (std::numeric_limits<uint32_t>::max)() - wsize)
285 throw TProtocolException(TProtocolException::SIZE_LIMIT);
286 wsize += ssize;
Maximilian Bandle7c943352025-02-07 10:58:32 +0100287 trans_->write(reinterpret_cast<const uint8_t*>(str.data()), ssize);
David Reisse4d4ea02009-04-02 21:37:17 +0000288 return wsize;
289}
290
Carel Combrink786764b2025-05-15 12:22:37 +0000291/**
292 * Write a TUuid to the wire
293 */
294template <class Transport_>
295uint32_t TCompactProtocolT<Transport_>::writeUUID(const TUuid& uuid) {
296 trans_->write(uuid.data(), uuid.size());
297 return uuid.size();
298}
299
David Reisse4d4ea02009-04-02 21:37:17 +0000300//
301// Internal Writing methods
302//
303
304/**
305 * The workhorse of writeFieldBegin. It has the option of doing a
306 * 'type override' of the type header. This is used specifically in the
307 * boolean field case.
308 */
David Reisse71115b2010-10-06 17:09:56 +0000309template <class Transport_>
310int32_t TCompactProtocolT<Transport_>::writeFieldBeginInternal(
311 const char* name,
312 const TType fieldType,
313 const int16_t fieldId,
314 int8_t typeOverride) {
Roger Meier3b771a12010-11-17 22:11:26 +0000315 (void) name;
David Reisse4d4ea02009-04-02 21:37:17 +0000316 uint32_t wsize = 0;
317
318 // if there's a type override, use that.
319 int8_t typeToWrite = (typeOverride == -1 ? getCompactType(fieldType) : typeOverride);
320
321 // check if we can use delta encoding for the field id
322 if (fieldId > lastFieldId_ && fieldId - lastFieldId_ <= 15) {
323 // write them together
Roger Meier64a799d2013-06-04 20:59:01 +0200324 wsize += writeByte(static_cast<int8_t>((fieldId - lastFieldId_)
325 << 4 | typeToWrite));
David Reisse4d4ea02009-04-02 21:37:17 +0000326 } else {
327 // write them separate
328 wsize += writeByte(typeToWrite);
329 wsize += writeI16(fieldId);
330 }
331
332 lastFieldId_ = fieldId;
333 return wsize;
334}
335
336/**
337 * Abstract method for writing the start of lists and sets. List and sets on
338 * the wire differ only by the type indicator.
339 */
David Reisse71115b2010-10-06 17:09:56 +0000340template <class Transport_>
Roger Meier64a799d2013-06-04 20:59:01 +0200341uint32_t TCompactProtocolT<Transport_>::writeCollectionBegin(const TType elemType,
David Reisse71115b2010-10-06 17:09:56 +0000342 int32_t size) {
David Reisse4d4ea02009-04-02 21:37:17 +0000343 uint32_t wsize = 0;
344 if (size <= 14) {
Roger Meier64a799d2013-06-04 20:59:01 +0200345 wsize += writeByte(static_cast<int8_t>(size
346 << 4 | getCompactType(elemType)));
David Reisse4d4ea02009-04-02 21:37:17 +0000347 } else {
348 wsize += writeByte(0xf0 | getCompactType(elemType));
349 wsize += writeVarint32(size);
350 }
351 return wsize;
352}
353
354/**
355 * Write an i32 as a varint. Results in 1-5 bytes on the wire.
356 */
David Reisse71115b2010-10-06 17:09:56 +0000357template <class Transport_>
358uint32_t TCompactProtocolT<Transport_>::writeVarint32(uint32_t n) {
David Reisse4d4ea02009-04-02 21:37:17 +0000359 uint8_t buf[5];
360 uint32_t wsize = 0;
361
362 while (true) {
363 if ((n & ~0x7F) == 0) {
Maximilian Bandle7c943352025-02-07 10:58:32 +0100364 buf[wsize++] = static_cast<int8_t>(n);
David Reisse4d4ea02009-04-02 21:37:17 +0000365 break;
366 } else {
Maximilian Bandle7c943352025-02-07 10:58:32 +0100367 buf[wsize++] = static_cast<int8_t>((n & 0x7F) | 0x80);
David Reisse4d4ea02009-04-02 21:37:17 +0000368 n >>= 7;
369 }
370 }
371 trans_->write(buf, wsize);
372 return wsize;
373}
374
375/**
376 * Write an i64 as a varint. Results in 1-10 bytes on the wire.
377 */
David Reisse71115b2010-10-06 17:09:56 +0000378template <class Transport_>
379uint32_t TCompactProtocolT<Transport_>::writeVarint64(uint64_t n) {
David Reisse4d4ea02009-04-02 21:37:17 +0000380 uint8_t buf[10];
381 uint32_t wsize = 0;
382
383 while (true) {
384 if ((n & ~0x7FL) == 0) {
Maximilian Bandle7c943352025-02-07 10:58:32 +0100385 buf[wsize++] = static_cast<int8_t>(n);
David Reisse4d4ea02009-04-02 21:37:17 +0000386 break;
387 } else {
Maximilian Bandle7c943352025-02-07 10:58:32 +0100388 buf[wsize++] = static_cast<int8_t>((n & 0x7F) | 0x80);
David Reisse4d4ea02009-04-02 21:37:17 +0000389 n >>= 7;
390 }
391 }
392 trans_->write(buf, wsize);
393 return wsize;
394}
395
396/**
397 * Convert l into a zigzag long. This allows negative numbers to be
398 * represented compactly as a varint.
399 */
David Reisse71115b2010-10-06 17:09:56 +0000400template <class Transport_>
401uint64_t TCompactProtocolT<Transport_>::i64ToZigzag(const int64_t l) {
Jim Apple147c2842017-03-18 12:56:50 -0700402 return (static_cast<uint64_t>(l) << 1) ^ (l >> 63);
David Reisse4d4ea02009-04-02 21:37:17 +0000403}
404
405/**
406 * Convert n into a zigzag int. This allows negative numbers to be
407 * represented compactly as a varint.
408 */
David Reisse71115b2010-10-06 17:09:56 +0000409template <class Transport_>
410uint32_t TCompactProtocolT<Transport_>::i32ToZigzag(const int32_t n) {
Jim Apple147c2842017-03-18 12:56:50 -0700411 return (static_cast<uint32_t>(n) << 1) ^ (n >> 31);
David Reisse4d4ea02009-04-02 21:37:17 +0000412}
413
414/**
David Reisse71115b2010-10-06 17:09:56 +0000415 * Given a TType value, find the appropriate detail::compact::Types value
David Reisse4d4ea02009-04-02 21:37:17 +0000416 */
David Reisse71115b2010-10-06 17:09:56 +0000417template <class Transport_>
Roger Meier64a799d2013-06-04 20:59:01 +0200418int8_t TCompactProtocolT<Transport_>::getCompactType(const TType ttype) {
David Reisse71115b2010-10-06 17:09:56 +0000419 return detail::compact::TTypeToCType[ttype];
David Reisse4d4ea02009-04-02 21:37:17 +0000420}
421
422//
423// Reading Methods
424//
425
426/**
427 * Read a message header.
428 */
David Reisse71115b2010-10-06 17:09:56 +0000429template <class Transport_>
430uint32_t TCompactProtocolT<Transport_>::readMessageBegin(
431 std::string& name,
432 TMessageType& messageType,
433 int32_t& seqid) {
David Reisse4d4ea02009-04-02 21:37:17 +0000434 uint32_t rsize = 0;
435 int8_t protocolId;
436 int8_t versionAndType;
437 int8_t version;
438
439 rsize += readByte(protocolId);
440 if (protocolId != PROTOCOL_ID) {
441 throw TProtocolException(TProtocolException::BAD_VERSION, "Bad protocol identifier");
442 }
443
444 rsize += readByte(versionAndType);
Maximilian Bandle7c943352025-02-07 10:58:32 +0100445 version = static_cast<int8_t>(versionAndType & VERSION_MASK);
David Reisse4d4ea02009-04-02 21:37:17 +0000446 if (version != VERSION_N) {
447 throw TProtocolException(TProtocolException::BAD_VERSION, "Bad protocol version");
448 }
449
Maximilian Bandle7c943352025-02-07 10:58:32 +0100450 messageType = static_cast<TMessageType>((versionAndType >> TYPE_SHIFT_AMOUNT) & TYPE_BITS);
David Reisse4d4ea02009-04-02 21:37:17 +0000451 rsize += readVarint32(seqid);
452 rsize += readString(name);
453
454 return rsize;
455}
456
457/**
458 * Read a struct begin. There's nothing on the wire for this, but it is our
459 * opportunity to push a new struct begin marker on the field stack.
460 */
David Reisse71115b2010-10-06 17:09:56 +0000461template <class Transport_>
462uint32_t TCompactProtocolT<Transport_>::readStructBegin(std::string& name) {
Ben Gemmill7089a3a2025-11-06 10:11:12 -0500463 name.clear();
David Reisse4d4ea02009-04-02 21:37:17 +0000464 lastField_.push(lastFieldId_);
465 lastFieldId_ = 0;
466 return 0;
467}
468
469/**
470 * Doesn't actually consume any wire data, just removes the last field for
471 * this struct from the field stack.
472 */
David Reisse71115b2010-10-06 17:09:56 +0000473template <class Transport_>
474uint32_t TCompactProtocolT<Transport_>::readStructEnd() {
David Reisse4d4ea02009-04-02 21:37:17 +0000475 lastFieldId_ = lastField_.top();
476 lastField_.pop();
477 return 0;
478}
479
480/**
481 * Read a field header off the wire.
482 */
David Reisse71115b2010-10-06 17:09:56 +0000483template <class Transport_>
484uint32_t TCompactProtocolT<Transport_>::readFieldBegin(std::string& name,
485 TType& fieldType,
486 int16_t& fieldId) {
Roger Meier3b771a12010-11-17 22:11:26 +0000487 (void) name;
David Reisse4d4ea02009-04-02 21:37:17 +0000488 uint32_t rsize = 0;
489 int8_t byte;
490 int8_t type;
491
492 rsize += readByte(byte);
493 type = (byte & 0x0f);
494
495 // if it's a stop, then we can return immediately, as the struct is over.
496 if (type == T_STOP) {
497 fieldType = T_STOP;
498 fieldId = 0;
499 return rsize;
500 }
501
502 // mask off the 4 MSB of the type header. it could contain a field id delta.
Maximilian Bandle7c943352025-02-07 10:58:32 +0100503 auto modifier = static_cast<int16_t>(static_cast<uint8_t>(byte & 0xf0) >> 4);
David Reisse4d4ea02009-04-02 21:37:17 +0000504 if (modifier == 0) {
505 // not a delta, look ahead for the zigzag varint field id.
506 rsize += readI16(fieldId);
507 } else {
Maximilian Bandle7c943352025-02-07 10:58:32 +0100508 fieldId = static_cast<int16_t>(lastFieldId_ + modifier);
David Reisse4d4ea02009-04-02 21:37:17 +0000509 }
510 fieldType = getTType(type);
511
512 // if this happens to be a boolean field, the value is encoded in the type
David Reisse71115b2010-10-06 17:09:56 +0000513 if (type == detail::compact::CT_BOOLEAN_TRUE ||
514 type == detail::compact::CT_BOOLEAN_FALSE) {
David Reisse4d4ea02009-04-02 21:37:17 +0000515 // save the boolean value in a special instance variable.
516 boolValue_.hasBoolValue = true;
David Reisse71115b2010-10-06 17:09:56 +0000517 boolValue_.boolValue =
518 (type == detail::compact::CT_BOOLEAN_TRUE ? true : false);
David Reisse4d4ea02009-04-02 21:37:17 +0000519 }
520
521 // push the new field onto the field stack so we can keep the deltas going.
522 lastFieldId_ = fieldId;
523 return rsize;
524}
525
526/**
527 * Read a map header off the wire. If the size is zero, skip reading the key
528 * and value type. This means that 0-length maps will yield TMaps without the
529 * "correct" types.
530 */
David Reisse71115b2010-10-06 17:09:56 +0000531template <class Transport_>
532uint32_t TCompactProtocolT<Transport_>::readMapBegin(TType& keyType,
533 TType& valType,
534 uint32_t& size) {
David Reisse4d4ea02009-04-02 21:37:17 +0000535 uint32_t rsize = 0;
536 int8_t kvType = 0;
537 int32_t msize = 0;
538
539 rsize += readVarint32(msize);
540 if (msize != 0)
541 rsize += readByte(kvType);
542
543 if (msize < 0) {
544 throw TProtocolException(TProtocolException::NEGATIVE_SIZE);
545 } else if (container_limit_ && msize > container_limit_) {
546 throw TProtocolException(TProtocolException::SIZE_LIMIT);
547 }
548
Maximilian Bandle7c943352025-02-07 10:58:32 +0100549 keyType = getTType(static_cast<int8_t>(static_cast<uint8_t>(kvType) >> 4));
550 valType = getTType(static_cast<int8_t>(static_cast<uint8_t>(kvType) & 0xf));
551 size = static_cast<uint32_t>(msize);
David Reisse4d4ea02009-04-02 21:37:17 +0000552
zeshuai00786352b42020-06-15 17:00:33 +0800553 TMap map(keyType, valType, size);
554 checkReadBytesAvailable(map);
555
David Reisse4d4ea02009-04-02 21:37:17 +0000556 return rsize;
557}
558
559/**
560 * Read a list header off the wire. If the list size is 0-14, the size will
561 * be packed into the element type header. If it's a longer list, the 4 MSB
562 * of the element type header will be 0xF, and a varint will follow with the
563 * true size.
564 */
David Reisse71115b2010-10-06 17:09:56 +0000565template <class Transport_>
566uint32_t TCompactProtocolT<Transport_>::readListBegin(TType& elemType,
567 uint32_t& size) {
David Reisse4d4ea02009-04-02 21:37:17 +0000568 int8_t size_and_type;
569 uint32_t rsize = 0;
570 int32_t lsize;
571
572 rsize += readByte(size_and_type);
573
Maximilian Bandle7c943352025-02-07 10:58:32 +0100574 lsize = (static_cast<uint8_t>(size_and_type) >> 4) & 0x0f;
David Reisse4d4ea02009-04-02 21:37:17 +0000575 if (lsize == 15) {
576 rsize += readVarint32(lsize);
577 }
578
579 if (lsize < 0) {
580 throw TProtocolException(TProtocolException::NEGATIVE_SIZE);
581 } else if (container_limit_ && lsize > container_limit_) {
582 throw TProtocolException(TProtocolException::SIZE_LIMIT);
583 }
584
Maximilian Bandle7c943352025-02-07 10:58:32 +0100585 elemType = getTType(static_cast<int8_t>(size_and_type & 0x0f));
586 size = static_cast<uint32_t>(lsize);
David Reisse4d4ea02009-04-02 21:37:17 +0000587
zeshuai00786352b42020-06-15 17:00:33 +0800588 TList list(elemType, size);
589 checkReadBytesAvailable(list);
590
David Reisse4d4ea02009-04-02 21:37:17 +0000591 return rsize;
592}
593
594/**
595 * Read a set header off the wire. If the set size is 0-14, the size will
596 * be packed into the element type header. If it's a longer set, the 4 MSB
597 * of the element type header will be 0xF, and a varint will follow with the
598 * true size.
599 */
David Reisse71115b2010-10-06 17:09:56 +0000600template <class Transport_>
601uint32_t TCompactProtocolT<Transport_>::readSetBegin(TType& elemType,
602 uint32_t& size) {
David Reisse4d4ea02009-04-02 21:37:17 +0000603 return readListBegin(elemType, size);
604}
605
606/**
607 * Read a boolean off the wire. If this is a boolean field, the value should
608 * already have been read during readFieldBegin, so we'll just consume the
609 * pre-stored value. Otherwise, read a byte.
610 */
David Reisse71115b2010-10-06 17:09:56 +0000611template <class Transport_>
612uint32_t TCompactProtocolT<Transport_>::readBool(bool& value) {
David Reisse4d4ea02009-04-02 21:37:17 +0000613 if (boolValue_.hasBoolValue == true) {
614 value = boolValue_.boolValue;
615 boolValue_.hasBoolValue = false;
616 return 0;
617 } else {
618 int8_t val;
619 readByte(val);
David Reisse71115b2010-10-06 17:09:56 +0000620 value = (val == detail::compact::CT_BOOLEAN_TRUE);
David Reisse4d4ea02009-04-02 21:37:17 +0000621 return 1;
622 }
623}
624
625/**
626 * Read a single byte off the wire. Nothing interesting here.
627 */
David Reisse71115b2010-10-06 17:09:56 +0000628template <class Transport_>
629uint32_t TCompactProtocolT<Transport_>::readByte(int8_t& byte) {
David Reisse4d4ea02009-04-02 21:37:17 +0000630 uint8_t b[1];
631 trans_->readAll(b, 1);
Maximilian Bandle7c943352025-02-07 10:58:32 +0100632 byte = static_cast<int8_t>(b[0]);
David Reisse4d4ea02009-04-02 21:37:17 +0000633 return 1;
634}
635
636/**
637 * Read an i16 from the wire as a zigzag varint.
638 */
David Reisse71115b2010-10-06 17:09:56 +0000639template <class Transport_>
640uint32_t TCompactProtocolT<Transport_>::readI16(int16_t& i16) {
David Reisse4d4ea02009-04-02 21:37:17 +0000641 int32_t value;
642 uint32_t rsize = readVarint32(value);
Maximilian Bandle7c943352025-02-07 10:58:32 +0100643 i16 = static_cast<int16_t>(zigzagToI32(value));
David Reisse4d4ea02009-04-02 21:37:17 +0000644 return rsize;
645}
646
647/**
648 * Read an i32 from the wire as a zigzag varint.
649 */
David Reisse71115b2010-10-06 17:09:56 +0000650template <class Transport_>
651uint32_t TCompactProtocolT<Transport_>::readI32(int32_t& i32) {
David Reisse4d4ea02009-04-02 21:37:17 +0000652 int32_t value;
653 uint32_t rsize = readVarint32(value);
654 i32 = zigzagToI32(value);
655 return rsize;
656}
657
658/**
659 * Read an i64 from the wire as a zigzag varint.
660 */
David Reisse71115b2010-10-06 17:09:56 +0000661template <class Transport_>
662uint32_t TCompactProtocolT<Transport_>::readI64(int64_t& i64) {
David Reisse4d4ea02009-04-02 21:37:17 +0000663 int64_t value;
664 uint32_t rsize = readVarint64(value);
665 i64 = zigzagToI64(value);
666 return rsize;
667}
668
669/**
670 * No magic here - just read a double off the wire.
671 */
David Reisse71115b2010-10-06 17:09:56 +0000672template <class Transport_>
673uint32_t TCompactProtocolT<Transport_>::readDouble(double& dub) {
cyy863262d2019-01-06 10:40:58 +0800674 static_assert(sizeof(double) == sizeof(uint64_t), "sizeof(double) == sizeof(uint64_t)");
675 static_assert(std::numeric_limits<double>::is_iec559, "std::numeric_limits<double>::is_iec559");
David Reisse4d4ea02009-04-02 21:37:17 +0000676
Carl Yeksigian3e937112013-06-03 13:46:51 -0400677 union {
678 uint64_t bits;
679 uint8_t b[8];
680 } u;
681 trans_->readAll(u.b, 8);
jfarrellad3a9552015-09-24 23:27:34 -0400682 u.bits = THRIFT_letohll(u.bits);
Carl Yeksigian3e937112013-06-03 13:46:51 -0400683 dub = bitwise_cast<double>(u.bits);
David Reisse4d4ea02009-04-02 21:37:17 +0000684 return 8;
685}
686
David Reisse71115b2010-10-06 17:09:56 +0000687template <class Transport_>
688uint32_t TCompactProtocolT<Transport_>::readString(std::string& str) {
David Reisse4d4ea02009-04-02 21:37:17 +0000689 return readBinary(str);
690}
691
692/**
693 * Read a byte[] from the wire.
694 */
David Reisse71115b2010-10-06 17:09:56 +0000695template <class Transport_>
696uint32_t TCompactProtocolT<Transport_>::readBinary(std::string& str) {
David Reisse4d4ea02009-04-02 21:37:17 +0000697 int32_t rsize = 0;
698 int32_t size;
699
700 rsize += readVarint32(size);
701 // Catch empty string case
702 if (size == 0) {
Ben Gemmill7089a3a2025-11-06 10:11:12 -0500703 str.clear();
David Reisse4d4ea02009-04-02 21:37:17 +0000704 return rsize;
705 }
706
707 // Catch error cases
708 if (size < 0) {
709 throw TProtocolException(TProtocolException::NEGATIVE_SIZE);
710 }
711 if (string_limit_ > 0 && size > string_limit_) {
712 throw TProtocolException(TProtocolException::SIZE_LIMIT);
713 }
714
Maximilian Bandle5c088932025-02-07 10:00:56 +0100715 // Check against MaxMessageSize before alloc
Maximilian Bandle7c943352025-02-07 10:58:32 +0100716 trans_->checkReadBytesAvailable(static_cast<uint32_t>(size));
Maximilian Bandle5c088932025-02-07 10:00:56 +0100717
David Reisse4d4ea02009-04-02 21:37:17 +0000718 // Use the heap here to prevent stack overflow for v. large strings
Sebastian Zenker042580f2019-01-29 15:48:12 +0100719 if (size > string_buf_size_ || string_buf_ == nullptr) {
Maximilian Bandle7c943352025-02-07 10:58:32 +0100720 void* new_string_buf = std::realloc(string_buf_, static_cast<uint32_t>(size));
Sebastian Zenker042580f2019-01-29 15:48:12 +0100721 if (new_string_buf == nullptr) {
David Reissf6735092010-10-06 17:10:49 +0000722 throw std::bad_alloc();
David Reisse4d4ea02009-04-02 21:37:17 +0000723 }
Maximilian Bandle7c943352025-02-07 10:58:32 +0100724 string_buf_ = static_cast<uint8_t*>(new_string_buf);
David Reisse4d4ea02009-04-02 21:37:17 +0000725 string_buf_size_ = size;
726 }
727 trans_->readAll(string_buf_, size);
Maximilian Bandle7c943352025-02-07 10:58:32 +0100728 str.assign(reinterpret_cast<char*>(string_buf_), size);
David Reisse4d4ea02009-04-02 21:37:17 +0000729
Maximilian Bandle7c943352025-02-07 10:58:32 +0100730 return rsize + static_cast<uint32_t>(size);
David Reisse4d4ea02009-04-02 21:37:17 +0000731}
732
Carel Combrink786764b2025-05-15 12:22:37 +0000733
734/**
735 * Read a TUuid from the wire.
736 */
737template <class Transport_>
738uint32_t TCompactProtocolT<Transport_>::readUUID(TUuid& uuid) {
739 return trans_->readAll(uuid.begin(), uuid.size());
740}
741
David Reisse4d4ea02009-04-02 21:37:17 +0000742/**
743 * Read an i32 from the wire as a varint. The MSB of each byte is set
744 * if there is another byte to follow. This can read up to 5 bytes.
745 */
David Reisse71115b2010-10-06 17:09:56 +0000746template <class Transport_>
747uint32_t TCompactProtocolT<Transport_>::readVarint32(int32_t& i32) {
David Reisse4d4ea02009-04-02 21:37:17 +0000748 int64_t val;
749 uint32_t rsize = readVarint64(val);
Maximilian Bandle7c943352025-02-07 10:58:32 +0100750 i32 = static_cast<int32_t>(val);
David Reisse4d4ea02009-04-02 21:37:17 +0000751 return rsize;
752}
753
754/**
755 * Read an i64 from the wire as a proper varint. The MSB of each byte is set
756 * if there is another byte to follow. This can read up to 10 bytes.
757 */
David Reisse71115b2010-10-06 17:09:56 +0000758template <class Transport_>
759uint32_t TCompactProtocolT<Transport_>::readVarint64(int64_t& i64) {
David Reisse4d4ea02009-04-02 21:37:17 +0000760 uint32_t rsize = 0;
761 uint64_t val = 0;
762 int shift = 0;
763 uint8_t buf[10]; // 64 bits / (7 bits/byte) = 10 bytes.
764 uint32_t buf_size = sizeof(buf);
765 const uint8_t* borrowed = trans_->borrow(buf, &buf_size);
766
767 // Fast path.
Sebastian Zenker042580f2019-01-29 15:48:12 +0100768 if (borrowed != nullptr) {
David Reisse4d4ea02009-04-02 21:37:17 +0000769 while (true) {
770 uint8_t byte = borrowed[rsize];
771 rsize++;
Maximilian Bandle7c943352025-02-07 10:58:32 +0100772 val |= static_cast<uint64_t>(byte & 0x7f) << shift;
David Reisse4d4ea02009-04-02 21:37:17 +0000773 shift += 7;
774 if (!(byte & 0x80)) {
775 i64 = val;
776 trans_->consume(rsize);
777 return rsize;
778 }
779 // Have to check for invalid data so we don't crash.
780 if (UNLIKELY(rsize == sizeof(buf))) {
781 throw TProtocolException(TProtocolException::INVALID_DATA, "Variable-length int over 10 bytes.");
782 }
783 }
784 }
785
786 // Slow path.
787 else {
788 while (true) {
789 uint8_t byte;
790 rsize += trans_->readAll(&byte, 1);
Maximilian Bandle7c943352025-02-07 10:58:32 +0100791 val |= static_cast<uint64_t>(byte & 0x7f) << shift;
David Reisse4d4ea02009-04-02 21:37:17 +0000792 shift += 7;
793 if (!(byte & 0x80)) {
794 i64 = val;
795 return rsize;
796 }
797 // Might as well check for invalid data on the slow path too.
798 if (UNLIKELY(rsize >= sizeof(buf))) {
799 throw TProtocolException(TProtocolException::INVALID_DATA, "Variable-length int over 10 bytes.");
800 }
801 }
802 }
803}
804
805/**
806 * Convert from zigzag int to int.
807 */
David Reisse71115b2010-10-06 17:09:56 +0000808template <class Transport_>
809int32_t TCompactProtocolT<Transport_>::zigzagToI32(uint32_t n) {
Ben Craig7f10de72013-10-14 20:27:18 -0500810 return (n >> 1) ^ static_cast<uint32_t>(-static_cast<int32_t>(n & 1));
David Reisse4d4ea02009-04-02 21:37:17 +0000811}
812
813/**
814 * Convert from zigzag long to long.
815 */
David Reisse71115b2010-10-06 17:09:56 +0000816template <class Transport_>
817int64_t TCompactProtocolT<Transport_>::zigzagToI64(uint64_t n) {
Ben Craig7f10de72013-10-14 20:27:18 -0500818 return (n >> 1) ^ static_cast<uint64_t>(-static_cast<int64_t>(n & 1));
David Reisse4d4ea02009-04-02 21:37:17 +0000819}
820
David Reisse71115b2010-10-06 17:09:56 +0000821template <class Transport_>
822TType TCompactProtocolT<Transport_>::getTType(int8_t type) {
David Reisse4d4ea02009-04-02 21:37:17 +0000823 switch (type) {
824 case T_STOP:
825 return T_STOP;
David Reisse71115b2010-10-06 17:09:56 +0000826 case detail::compact::CT_BOOLEAN_FALSE:
827 case detail::compact::CT_BOOLEAN_TRUE:
David Reisse4d4ea02009-04-02 21:37:17 +0000828 return T_BOOL;
David Reisse71115b2010-10-06 17:09:56 +0000829 case detail::compact::CT_BYTE:
David Reisse4d4ea02009-04-02 21:37:17 +0000830 return T_BYTE;
David Reisse71115b2010-10-06 17:09:56 +0000831 case detail::compact::CT_I16:
David Reisse4d4ea02009-04-02 21:37:17 +0000832 return T_I16;
David Reisse71115b2010-10-06 17:09:56 +0000833 case detail::compact::CT_I32:
David Reisse4d4ea02009-04-02 21:37:17 +0000834 return T_I32;
David Reisse71115b2010-10-06 17:09:56 +0000835 case detail::compact::CT_I64:
David Reisse4d4ea02009-04-02 21:37:17 +0000836 return T_I64;
David Reisse71115b2010-10-06 17:09:56 +0000837 case detail::compact::CT_DOUBLE:
David Reisse4d4ea02009-04-02 21:37:17 +0000838 return T_DOUBLE;
David Reisse71115b2010-10-06 17:09:56 +0000839 case detail::compact::CT_BINARY:
David Reisse4d4ea02009-04-02 21:37:17 +0000840 return T_STRING;
David Reisse71115b2010-10-06 17:09:56 +0000841 case detail::compact::CT_LIST:
David Reisse4d4ea02009-04-02 21:37:17 +0000842 return T_LIST;
David Reisse71115b2010-10-06 17:09:56 +0000843 case detail::compact::CT_SET:
David Reisse4d4ea02009-04-02 21:37:17 +0000844 return T_SET;
David Reisse71115b2010-10-06 17:09:56 +0000845 case detail::compact::CT_MAP:
David Reisse4d4ea02009-04-02 21:37:17 +0000846 return T_MAP;
David Reisse71115b2010-10-06 17:09:56 +0000847 case detail::compact::CT_STRUCT:
David Reisse4d4ea02009-04-02 21:37:17 +0000848 return T_STRUCT;
Carel Combrink786764b2025-05-15 12:22:37 +0000849 case detail::compact::CT_UUID:
850 return T_UUID;
David Reisse4d4ea02009-04-02 21:37:17 +0000851 default:
Maximilian Bandle7c943352025-02-07 10:58:32 +0100852 throw TException(std::string("don't know what type: ") + static_cast<char>(type));
David Reisse4d4ea02009-04-02 21:37:17 +0000853 }
David Reisse4d4ea02009-04-02 21:37:17 +0000854}
855
zeshuai00786352b42020-06-15 17:00:33 +0800856// Return the minimum number of bytes a type will consume on the wire
857template <class Transport_>
858int TCompactProtocolT<Transport_>::getMinSerializedSize(TType type)
859{
860 switch (type)
861 {
Hasnain Lakhani845a87a2025-05-27 22:31:42 -0700862 case T_STOP: return 1; // T_STOP needs to count itself
863 case T_VOID: return 1; // T_VOID needs to count itself
zeshuai00786352b42020-06-15 17:00:33 +0800864 case T_BOOL: return sizeof(int8_t);
865 case T_DOUBLE: return 8; // uses fixedLongToBytes() which always writes 8 bytes
866 case T_BYTE: return sizeof(int8_t);
867 case T_I16: return sizeof(int8_t); // zigzag
868 case T_I32: return sizeof(int8_t); // zigzag
869 case T_I64: return sizeof(int8_t); // zigzag
870 case T_STRING: return sizeof(int8_t); // string length
Hasnain Lakhani845a87a2025-05-27 22:31:42 -0700871 case T_STRUCT: return 1; // empty struct needs at least 1 byte for the T_STOP
zeshuai00786352b42020-06-15 17:00:33 +0800872 case T_MAP: return sizeof(int8_t); // element count
873 case T_SET: return sizeof(int8_t); // element count
874 case T_LIST: return sizeof(int8_t); // element count
Carel Combrink786764b2025-05-15 12:22:37 +0000875 case T_UUID: return 16; // 16 bytes
zeshuai00786352b42020-06-15 17:00:33 +0800876 default: throw TProtocolException(TProtocolException::UNKNOWN, "unrecognized type code");
877 }
878}
879
880
David Reisse4d4ea02009-04-02 21:37:17 +0000881}}} // apache::thrift::protocol
David Reisse71115b2010-10-06 17:09:56 +0000882
883#endif // _THRIFT_PROTOCOL_TCOMPACTPROTOCOL_TCC_