blob: 55b784f22ac7cbd986124a6bc223684872761132 [file] [log] [blame]
David Reisse4d4ea02009-04-02 21:37:17 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
David Reisse71115b2010-10-06 17:09:56 +000019#ifndef _THRIFT_PROTOCOL_TCOMPACTPROTOCOL_TCC_
20#define _THRIFT_PROTOCOL_TCOMPACTPROTOCOL_TCC_ 1
David Reisse4d4ea02009-04-02 21:37:17 +000021
Bryan Duxbury141eab42009-04-03 15:05:28 +000022#include <limits>
David Reisse4d4ea02009-04-02 21:37:17 +000023
24/*
25 * TCompactProtocol::i*ToZigzag depend on the fact that the right shift
26 * operator on a signed integer is an arithmetic (sign-extending) shift.
27 * If this is not the case, the current implementation will not work.
28 * If anyone encounters this error, we can try to figure out the best
29 * way to implement an arithmetic right shift on their platform.
30 */
31#if !defined(SIGNED_RIGHT_SHIFT_IS) || !defined(ARITHMETIC_RIGHT_SHIFT)
32# error "Unable to determine the behavior of a signed right shift"
33#endif
34#if SIGNED_RIGHT_SHIFT_IS != ARITHMETIC_RIGHT_SHIFT
David Reisse71115b2010-10-06 17:09:56 +000035# error "TCompactProtocol currently only works if a signed right shift is arithmetic"
David Reisse4d4ea02009-04-02 21:37:17 +000036#endif
37
38#ifdef __GNUC__
39#define UNLIKELY(val) (__builtin_expect((val), 0))
40#else
41#define UNLIKELY(val) (val)
42#endif
43
44namespace apache { namespace thrift { namespace protocol {
45
David Reisse71115b2010-10-06 17:09:56 +000046namespace detail { namespace compact {
47
48enum Types {
49 CT_STOP = 0x00,
50 CT_BOOLEAN_TRUE = 0x01,
51 CT_BOOLEAN_FALSE = 0x02,
52 CT_BYTE = 0x03,
53 CT_I16 = 0x04,
54 CT_I32 = 0x05,
55 CT_I64 = 0x06,
56 CT_DOUBLE = 0x07,
57 CT_BINARY = 0x08,
58 CT_LIST = 0x09,
59 CT_SET = 0x0A,
60 CT_MAP = 0x0B,
Roger Meier0069cc42010-10-13 18:10:18 +000061 CT_STRUCT = 0x0C
David Reisse71115b2010-10-06 17:09:56 +000062};
63
64const int8_t TTypeToCType[16] = {
65 CT_STOP, // T_STOP
66 0, // unused
67 CT_BOOLEAN_TRUE, // T_BOOL
68 CT_BYTE, // T_BYTE
69 CT_DOUBLE, // T_DOUBLE
70 0, // unused
71 CT_I16, // T_I16
72 0, // unused
73 CT_I32, // T_I32
74 0, // unused
75 CT_I64, // T_I64
76 CT_BINARY, // T_STRING
77 CT_STRUCT, // T_STRUCT
78 CT_MAP, // T_MAP
79 CT_SET, // T_SET
80 CT_LIST, // T_LIST
81};
82
83}} // end detail::compact namespace
David Reisse4d4ea02009-04-02 21:37:17 +000084
85
David Reisse71115b2010-10-06 17:09:56 +000086template <class Transport_>
87uint32_t TCompactProtocolT<Transport_>::writeMessageBegin(
88 const std::string& name,
89 const TMessageType messageType,
90 const int32_t seqid) {
David Reisse4d4ea02009-04-02 21:37:17 +000091 uint32_t wsize = 0;
92 wsize += writeByte(PROTOCOL_ID);
93 wsize += writeByte((VERSION_N & VERSION_MASK) | (((int32_t)messageType << TYPE_SHIFT_AMOUNT) & TYPE_MASK));
94 wsize += writeVarint32(seqid);
95 wsize += writeString(name);
96 return wsize;
97}
98
99/**
100 * Write a field header containing the field id and field type. If the
101 * difference between the current field id and the last one is small (< 15),
102 * then the field id will be encoded in the 4 MSB as a delta. Otherwise, the
103 * field id will follow the type header as a zigzag varint.
104 */
David Reisse71115b2010-10-06 17:09:56 +0000105template <class Transport_>
106uint32_t TCompactProtocolT<Transport_>::writeFieldBegin(const char* name,
107 const TType fieldType,
108 const int16_t fieldId) {
David Reisse4d4ea02009-04-02 21:37:17 +0000109 if (fieldType == T_BOOL) {
110 booleanField_.name = name;
111 booleanField_.fieldType = fieldType;
112 booleanField_.fieldId = fieldId;
113 } else {
114 return writeFieldBeginInternal(name, fieldType, fieldId, -1);
115 }
116 return 0;
117}
118
119/**
120 * Write the STOP symbol so we know there are no more fields in this struct.
121 */
David Reisse71115b2010-10-06 17:09:56 +0000122template <class Transport_>
123uint32_t TCompactProtocolT<Transport_>::writeFieldStop() {
David Reisse4d4ea02009-04-02 21:37:17 +0000124 return writeByte(T_STOP);
125}
126
127/**
128 * Write a struct begin. This doesn't actually put anything on the wire. We
129 * use it as an opportunity to put special placeholder markers on the field
130 * stack so we can get the field id deltas correct.
131 */
David Reisse71115b2010-10-06 17:09:56 +0000132template <class Transport_>
133uint32_t TCompactProtocolT<Transport_>::writeStructBegin(const char* name) {
Roger Meier3b771a12010-11-17 22:11:26 +0000134 (void) name;
David Reisse4d4ea02009-04-02 21:37:17 +0000135 lastField_.push(lastFieldId_);
136 lastFieldId_ = 0;
137 return 0;
138}
139
140/**
141 * Write a struct end. This doesn't actually put anything on the wire. We use
142 * this as an opportunity to pop the last field from the current struct off
143 * of the field stack.
144 */
David Reisse71115b2010-10-06 17:09:56 +0000145template <class Transport_>
146uint32_t TCompactProtocolT<Transport_>::writeStructEnd() {
David Reisse4d4ea02009-04-02 21:37:17 +0000147 lastFieldId_ = lastField_.top();
148 lastField_.pop();
149 return 0;
150}
151
152/**
153 * Write a List header.
154 */
David Reisse71115b2010-10-06 17:09:56 +0000155template <class Transport_>
156uint32_t TCompactProtocolT<Transport_>::writeListBegin(const TType elemType,
157 const uint32_t size) {
David Reisse4d4ea02009-04-02 21:37:17 +0000158 return writeCollectionBegin(elemType, size);
159}
160
161/**
162 * Write a set header.
163 */
David Reisse71115b2010-10-06 17:09:56 +0000164template <class Transport_>
165uint32_t TCompactProtocolT<Transport_>::writeSetBegin(const TType elemType,
166 const uint32_t size) {
David Reisse4d4ea02009-04-02 21:37:17 +0000167 return writeCollectionBegin(elemType, size);
168}
169
170/**
171 * Write a map header. If the map is empty, omit the key and value type
172 * headers, as we don't need any additional information to skip it.
173 */
David Reisse71115b2010-10-06 17:09:56 +0000174template <class Transport_>
175uint32_t TCompactProtocolT<Transport_>::writeMapBegin(const TType keyType,
176 const TType valType,
177 const uint32_t size) {
David Reisse4d4ea02009-04-02 21:37:17 +0000178 uint32_t wsize = 0;
179
180 if (size == 0) {
181 wsize += writeByte(0);
182 } else {
183 wsize += writeVarint32(size);
184 wsize += writeByte(getCompactType(keyType) << 4 | getCompactType(valType));
185 }
186 return wsize;
187}
188
189/**
190 * Write a boolean value. Potentially, this could be a boolean field, in
191 * which case the field header info isn't written yet. If so, decide what the
192 * right type header is for the value and then write the field header.
193 * Otherwise, write a single byte.
194 */
David Reisse71115b2010-10-06 17:09:56 +0000195template <class Transport_>
196uint32_t TCompactProtocolT<Transport_>::writeBool(const bool value) {
David Reisse4d4ea02009-04-02 21:37:17 +0000197 uint32_t wsize = 0;
198
199 if (booleanField_.name != NULL) {
200 // we haven't written the field header yet
201 wsize += writeFieldBeginInternal(booleanField_.name,
202 booleanField_.fieldType,
203 booleanField_.fieldId,
David Reisse71115b2010-10-06 17:09:56 +0000204 value ? detail::compact::CT_BOOLEAN_TRUE :
205 detail::compact::CT_BOOLEAN_FALSE);
David Reisse4d4ea02009-04-02 21:37:17 +0000206 booleanField_.name = NULL;
207 } else {
208 // we're not part of a field, so just write the value
David Reisse71115b2010-10-06 17:09:56 +0000209 wsize += writeByte(value ? detail::compact::CT_BOOLEAN_TRUE :
210 detail::compact::CT_BOOLEAN_FALSE);
David Reisse4d4ea02009-04-02 21:37:17 +0000211 }
212 return wsize;
213}
214
David Reisse71115b2010-10-06 17:09:56 +0000215template <class Transport_>
216uint32_t TCompactProtocolT<Transport_>::writeByte(const int8_t byte) {
David Reisse4d4ea02009-04-02 21:37:17 +0000217 trans_->write((uint8_t*)&byte, 1);
218 return 1;
219}
220
221/**
222 * Write an i16 as a zigzag varint.
223 */
David Reisse71115b2010-10-06 17:09:56 +0000224template <class Transport_>
225uint32_t TCompactProtocolT<Transport_>::writeI16(const int16_t i16) {
David Reisse4d4ea02009-04-02 21:37:17 +0000226 return writeVarint32(i32ToZigzag(i16));
227}
228
229/**
230 * Write an i32 as a zigzag varint.
231 */
David Reisse71115b2010-10-06 17:09:56 +0000232template <class Transport_>
233uint32_t TCompactProtocolT<Transport_>::writeI32(const int32_t i32) {
David Reisse4d4ea02009-04-02 21:37:17 +0000234 return writeVarint32(i32ToZigzag(i32));
235}
236
237/**
238 * Write an i64 as a zigzag varint.
239 */
David Reisse71115b2010-10-06 17:09:56 +0000240template <class Transport_>
241uint32_t TCompactProtocolT<Transport_>::writeI64(const int64_t i64) {
David Reisse4d4ea02009-04-02 21:37:17 +0000242 return writeVarint64(i64ToZigzag(i64));
243}
244
245/**
246 * Write a double to the wire as 8 bytes.
247 */
David Reisse71115b2010-10-06 17:09:56 +0000248template <class Transport_>
249uint32_t TCompactProtocolT<Transport_>::writeDouble(const double dub) {
David Reisse4d4ea02009-04-02 21:37:17 +0000250 BOOST_STATIC_ASSERT(sizeof(double) == sizeof(uint64_t));
251 BOOST_STATIC_ASSERT(std::numeric_limits<double>::is_iec559);
252
253 uint64_t bits = bitwise_cast<uint64_t>(dub);
254 bits = htolell(bits);
255 trans_->write((uint8_t*)&bits, 8);
256 return 8;
257}
258
259/**
260 * Write a string to the wire with a varint size preceeding.
261 */
David Reisse71115b2010-10-06 17:09:56 +0000262template <class Transport_>
263uint32_t TCompactProtocolT<Transport_>::writeString(const std::string& str) {
David Reisse4d4ea02009-04-02 21:37:17 +0000264 return writeBinary(str);
265}
266
David Reisse71115b2010-10-06 17:09:56 +0000267template <class Transport_>
268uint32_t TCompactProtocolT<Transport_>::writeBinary(const std::string& str) {
David Reisse4d4ea02009-04-02 21:37:17 +0000269 uint32_t ssize = str.size();
270 uint32_t wsize = writeVarint32(ssize) + ssize;
271 trans_->write((uint8_t*)str.data(), ssize);
272 return wsize;
273}
274
275//
276// Internal Writing methods
277//
278
279/**
280 * The workhorse of writeFieldBegin. It has the option of doing a
281 * 'type override' of the type header. This is used specifically in the
282 * boolean field case.
283 */
David Reisse71115b2010-10-06 17:09:56 +0000284template <class Transport_>
285int32_t TCompactProtocolT<Transport_>::writeFieldBeginInternal(
286 const char* name,
287 const TType fieldType,
288 const int16_t fieldId,
289 int8_t typeOverride) {
Roger Meier3b771a12010-11-17 22:11:26 +0000290 (void) name;
David Reisse4d4ea02009-04-02 21:37:17 +0000291 uint32_t wsize = 0;
292
293 // if there's a type override, use that.
294 int8_t typeToWrite = (typeOverride == -1 ? getCompactType(fieldType) : typeOverride);
295
296 // check if we can use delta encoding for the field id
297 if (fieldId > lastFieldId_ && fieldId - lastFieldId_ <= 15) {
298 // write them together
299 wsize += writeByte((fieldId - lastFieldId_) << 4 | typeToWrite);
300 } else {
301 // write them separate
302 wsize += writeByte(typeToWrite);
303 wsize += writeI16(fieldId);
304 }
305
306 lastFieldId_ = fieldId;
307 return wsize;
308}
309
310/**
311 * Abstract method for writing the start of lists and sets. List and sets on
312 * the wire differ only by the type indicator.
313 */
David Reisse71115b2010-10-06 17:09:56 +0000314template <class Transport_>
315uint32_t TCompactProtocolT<Transport_>::writeCollectionBegin(int8_t elemType,
316 int32_t size) {
David Reisse4d4ea02009-04-02 21:37:17 +0000317 uint32_t wsize = 0;
318 if (size <= 14) {
319 wsize += writeByte(size << 4 | getCompactType(elemType));
320 } else {
321 wsize += writeByte(0xf0 | getCompactType(elemType));
322 wsize += writeVarint32(size);
323 }
324 return wsize;
325}
326
327/**
328 * Write an i32 as a varint. Results in 1-5 bytes on the wire.
329 */
David Reisse71115b2010-10-06 17:09:56 +0000330template <class Transport_>
331uint32_t TCompactProtocolT<Transport_>::writeVarint32(uint32_t n) {
David Reisse4d4ea02009-04-02 21:37:17 +0000332 uint8_t buf[5];
333 uint32_t wsize = 0;
334
335 while (true) {
336 if ((n & ~0x7F) == 0) {
337 buf[wsize++] = (int8_t)n;
338 break;
339 } else {
340 buf[wsize++] = (int8_t)((n & 0x7F) | 0x80);
341 n >>= 7;
342 }
343 }
344 trans_->write(buf, wsize);
345 return wsize;
346}
347
348/**
349 * Write an i64 as a varint. Results in 1-10 bytes on the wire.
350 */
David Reisse71115b2010-10-06 17:09:56 +0000351template <class Transport_>
352uint32_t TCompactProtocolT<Transport_>::writeVarint64(uint64_t n) {
David Reisse4d4ea02009-04-02 21:37:17 +0000353 uint8_t buf[10];
354 uint32_t wsize = 0;
355
356 while (true) {
357 if ((n & ~0x7FL) == 0) {
358 buf[wsize++] = (int8_t)n;
359 break;
360 } else {
361 buf[wsize++] = (int8_t)((n & 0x7F) | 0x80);
362 n >>= 7;
363 }
364 }
365 trans_->write(buf, wsize);
366 return wsize;
367}
368
369/**
370 * Convert l into a zigzag long. This allows negative numbers to be
371 * represented compactly as a varint.
372 */
David Reisse71115b2010-10-06 17:09:56 +0000373template <class Transport_>
374uint64_t TCompactProtocolT<Transport_>::i64ToZigzag(const int64_t l) {
David Reisse4d4ea02009-04-02 21:37:17 +0000375 return (l << 1) ^ (l >> 63);
376}
377
378/**
379 * Convert n into a zigzag int. This allows negative numbers to be
380 * represented compactly as a varint.
381 */
David Reisse71115b2010-10-06 17:09:56 +0000382template <class Transport_>
383uint32_t TCompactProtocolT<Transport_>::i32ToZigzag(const int32_t n) {
David Reisse4d4ea02009-04-02 21:37:17 +0000384 return (n << 1) ^ (n >> 31);
385}
386
387/**
David Reisse71115b2010-10-06 17:09:56 +0000388 * Given a TType value, find the appropriate detail::compact::Types value
David Reisse4d4ea02009-04-02 21:37:17 +0000389 */
David Reisse71115b2010-10-06 17:09:56 +0000390template <class Transport_>
391int8_t TCompactProtocolT<Transport_>::getCompactType(int8_t ttype) {
392 return detail::compact::TTypeToCType[ttype];
David Reisse4d4ea02009-04-02 21:37:17 +0000393}
394
395//
396// Reading Methods
397//
398
399/**
400 * Read a message header.
401 */
David Reisse71115b2010-10-06 17:09:56 +0000402template <class Transport_>
403uint32_t TCompactProtocolT<Transport_>::readMessageBegin(
404 std::string& name,
405 TMessageType& messageType,
406 int32_t& seqid) {
David Reisse4d4ea02009-04-02 21:37:17 +0000407 uint32_t rsize = 0;
408 int8_t protocolId;
409 int8_t versionAndType;
410 int8_t version;
411
412 rsize += readByte(protocolId);
413 if (protocolId != PROTOCOL_ID) {
414 throw TProtocolException(TProtocolException::BAD_VERSION, "Bad protocol identifier");
415 }
416
417 rsize += readByte(versionAndType);
418 version = (int8_t)(versionAndType & VERSION_MASK);
419 if (version != VERSION_N) {
420 throw TProtocolException(TProtocolException::BAD_VERSION, "Bad protocol version");
421 }
422
423 messageType = (TMessageType)((versionAndType >> TYPE_SHIFT_AMOUNT) & 0x03);
424 rsize += readVarint32(seqid);
425 rsize += readString(name);
426
427 return rsize;
428}
429
430/**
431 * Read a struct begin. There's nothing on the wire for this, but it is our
432 * opportunity to push a new struct begin marker on the field stack.
433 */
David Reisse71115b2010-10-06 17:09:56 +0000434template <class Transport_>
435uint32_t TCompactProtocolT<Transport_>::readStructBegin(std::string& name) {
David Reisse4d4ea02009-04-02 21:37:17 +0000436 name = "";
437 lastField_.push(lastFieldId_);
438 lastFieldId_ = 0;
439 return 0;
440}
441
442/**
443 * Doesn't actually consume any wire data, just removes the last field for
444 * this struct from the field stack.
445 */
David Reisse71115b2010-10-06 17:09:56 +0000446template <class Transport_>
447uint32_t TCompactProtocolT<Transport_>::readStructEnd() {
David Reisse4d4ea02009-04-02 21:37:17 +0000448 lastFieldId_ = lastField_.top();
449 lastField_.pop();
450 return 0;
451}
452
453/**
454 * Read a field header off the wire.
455 */
David Reisse71115b2010-10-06 17:09:56 +0000456template <class Transport_>
457uint32_t TCompactProtocolT<Transport_>::readFieldBegin(std::string& name,
458 TType& fieldType,
459 int16_t& fieldId) {
Roger Meier3b771a12010-11-17 22:11:26 +0000460 (void) name;
David Reisse4d4ea02009-04-02 21:37:17 +0000461 uint32_t rsize = 0;
462 int8_t byte;
463 int8_t type;
464
465 rsize += readByte(byte);
466 type = (byte & 0x0f);
467
468 // if it's a stop, then we can return immediately, as the struct is over.
469 if (type == T_STOP) {
470 fieldType = T_STOP;
471 fieldId = 0;
472 return rsize;
473 }
474
475 // mask off the 4 MSB of the type header. it could contain a field id delta.
476 int16_t modifier = (int16_t)(((uint8_t)byte & 0xf0) >> 4);
477 if (modifier == 0) {
478 // not a delta, look ahead for the zigzag varint field id.
479 rsize += readI16(fieldId);
480 } else {
481 fieldId = (int16_t)(lastFieldId_ + modifier);
482 }
483 fieldType = getTType(type);
484
485 // if this happens to be a boolean field, the value is encoded in the type
David Reisse71115b2010-10-06 17:09:56 +0000486 if (type == detail::compact::CT_BOOLEAN_TRUE ||
487 type == detail::compact::CT_BOOLEAN_FALSE) {
David Reisse4d4ea02009-04-02 21:37:17 +0000488 // save the boolean value in a special instance variable.
489 boolValue_.hasBoolValue = true;
David Reisse71115b2010-10-06 17:09:56 +0000490 boolValue_.boolValue =
491 (type == detail::compact::CT_BOOLEAN_TRUE ? true : false);
David Reisse4d4ea02009-04-02 21:37:17 +0000492 }
493
494 // push the new field onto the field stack so we can keep the deltas going.
495 lastFieldId_ = fieldId;
496 return rsize;
497}
498
499/**
500 * Read a map header off the wire. If the size is zero, skip reading the key
501 * and value type. This means that 0-length maps will yield TMaps without the
502 * "correct" types.
503 */
David Reisse71115b2010-10-06 17:09:56 +0000504template <class Transport_>
505uint32_t TCompactProtocolT<Transport_>::readMapBegin(TType& keyType,
506 TType& valType,
507 uint32_t& size) {
David Reisse4d4ea02009-04-02 21:37:17 +0000508 uint32_t rsize = 0;
509 int8_t kvType = 0;
510 int32_t msize = 0;
511
512 rsize += readVarint32(msize);
513 if (msize != 0)
514 rsize += readByte(kvType);
515
516 if (msize < 0) {
517 throw TProtocolException(TProtocolException::NEGATIVE_SIZE);
518 } else if (container_limit_ && msize > container_limit_) {
519 throw TProtocolException(TProtocolException::SIZE_LIMIT);
520 }
521
522 keyType = getTType((int8_t)((uint8_t)kvType >> 4));
523 valType = getTType((int8_t)((uint8_t)kvType & 0xf));
524 size = (uint32_t)msize;
525
526 return rsize;
527}
528
529/**
530 * Read a list header off the wire. If the list size is 0-14, the size will
531 * be packed into the element type header. If it's a longer list, the 4 MSB
532 * of the element type header will be 0xF, and a varint will follow with the
533 * true size.
534 */
David Reisse71115b2010-10-06 17:09:56 +0000535template <class Transport_>
536uint32_t TCompactProtocolT<Transport_>::readListBegin(TType& elemType,
537 uint32_t& size) {
David Reisse4d4ea02009-04-02 21:37:17 +0000538 int8_t size_and_type;
539 uint32_t rsize = 0;
540 int32_t lsize;
541
542 rsize += readByte(size_and_type);
543
544 lsize = ((uint8_t)size_and_type >> 4) & 0x0f;
545 if (lsize == 15) {
546 rsize += readVarint32(lsize);
547 }
548
549 if (lsize < 0) {
550 throw TProtocolException(TProtocolException::NEGATIVE_SIZE);
551 } else if (container_limit_ && lsize > container_limit_) {
552 throw TProtocolException(TProtocolException::SIZE_LIMIT);
553 }
554
555 elemType = getTType((int8_t)(size_and_type & 0x0f));
556 size = (uint32_t)lsize;
557
558 return rsize;
559}
560
561/**
562 * Read a set header off the wire. If the set size is 0-14, the size will
563 * be packed into the element type header. If it's a longer set, the 4 MSB
564 * of the element type header will be 0xF, and a varint will follow with the
565 * true size.
566 */
David Reisse71115b2010-10-06 17:09:56 +0000567template <class Transport_>
568uint32_t TCompactProtocolT<Transport_>::readSetBegin(TType& elemType,
569 uint32_t& size) {
David Reisse4d4ea02009-04-02 21:37:17 +0000570 return readListBegin(elemType, size);
571}
572
573/**
574 * Read a boolean off the wire. If this is a boolean field, the value should
575 * already have been read during readFieldBegin, so we'll just consume the
576 * pre-stored value. Otherwise, read a byte.
577 */
David Reisse71115b2010-10-06 17:09:56 +0000578template <class Transport_>
579uint32_t TCompactProtocolT<Transport_>::readBool(bool& value) {
David Reisse4d4ea02009-04-02 21:37:17 +0000580 if (boolValue_.hasBoolValue == true) {
581 value = boolValue_.boolValue;
582 boolValue_.hasBoolValue = false;
583 return 0;
584 } else {
585 int8_t val;
586 readByte(val);
David Reisse71115b2010-10-06 17:09:56 +0000587 value = (val == detail::compact::CT_BOOLEAN_TRUE);
David Reisse4d4ea02009-04-02 21:37:17 +0000588 return 1;
589 }
590}
591
592/**
593 * Read a single byte off the wire. Nothing interesting here.
594 */
David Reisse71115b2010-10-06 17:09:56 +0000595template <class Transport_>
596uint32_t TCompactProtocolT<Transport_>::readByte(int8_t& byte) {
David Reisse4d4ea02009-04-02 21:37:17 +0000597 uint8_t b[1];
598 trans_->readAll(b, 1);
599 byte = *(int8_t*)b;
600 return 1;
601}
602
603/**
604 * Read an i16 from the wire as a zigzag varint.
605 */
David Reisse71115b2010-10-06 17:09:56 +0000606template <class Transport_>
607uint32_t TCompactProtocolT<Transport_>::readI16(int16_t& i16) {
David Reisse4d4ea02009-04-02 21:37:17 +0000608 int32_t value;
609 uint32_t rsize = readVarint32(value);
610 i16 = (int16_t)zigzagToI32(value);
611 return rsize;
612}
613
614/**
615 * Read an i32 from the wire as a zigzag varint.
616 */
David Reisse71115b2010-10-06 17:09:56 +0000617template <class Transport_>
618uint32_t TCompactProtocolT<Transport_>::readI32(int32_t& i32) {
David Reisse4d4ea02009-04-02 21:37:17 +0000619 int32_t value;
620 uint32_t rsize = readVarint32(value);
621 i32 = zigzagToI32(value);
622 return rsize;
623}
624
625/**
626 * Read an i64 from the wire as a zigzag varint.
627 */
David Reisse71115b2010-10-06 17:09:56 +0000628template <class Transport_>
629uint32_t TCompactProtocolT<Transport_>::readI64(int64_t& i64) {
David Reisse4d4ea02009-04-02 21:37:17 +0000630 int64_t value;
631 uint32_t rsize = readVarint64(value);
632 i64 = zigzagToI64(value);
633 return rsize;
634}
635
636/**
637 * No magic here - just read a double off the wire.
638 */
David Reisse71115b2010-10-06 17:09:56 +0000639template <class Transport_>
640uint32_t TCompactProtocolT<Transport_>::readDouble(double& dub) {
David Reisse4d4ea02009-04-02 21:37:17 +0000641 BOOST_STATIC_ASSERT(sizeof(double) == sizeof(uint64_t));
642 BOOST_STATIC_ASSERT(std::numeric_limits<double>::is_iec559);
643
644 uint64_t bits;
645 uint8_t b[8];
646 trans_->readAll(b, 8);
647 bits = *(uint64_t*)b;
648 bits = letohll(bits);
649 dub = bitwise_cast<double>(bits);
650 return 8;
651}
652
David Reisse71115b2010-10-06 17:09:56 +0000653template <class Transport_>
654uint32_t TCompactProtocolT<Transport_>::readString(std::string& str) {
David Reisse4d4ea02009-04-02 21:37:17 +0000655 return readBinary(str);
656}
657
658/**
659 * Read a byte[] from the wire.
660 */
David Reisse71115b2010-10-06 17:09:56 +0000661template <class Transport_>
662uint32_t TCompactProtocolT<Transport_>::readBinary(std::string& str) {
David Reisse4d4ea02009-04-02 21:37:17 +0000663 int32_t rsize = 0;
664 int32_t size;
665
666 rsize += readVarint32(size);
667 // Catch empty string case
668 if (size == 0) {
669 str = "";
670 return rsize;
671 }
672
673 // Catch error cases
674 if (size < 0) {
675 throw TProtocolException(TProtocolException::NEGATIVE_SIZE);
676 }
677 if (string_limit_ > 0 && size > string_limit_) {
678 throw TProtocolException(TProtocolException::SIZE_LIMIT);
679 }
680
681 // Use the heap here to prevent stack overflow for v. large strings
682 if (size > string_buf_size_ || string_buf_ == NULL) {
683 void* new_string_buf = std::realloc(string_buf_, (uint32_t)size);
684 if (new_string_buf == NULL) {
David Reissf6735092010-10-06 17:10:49 +0000685 throw std::bad_alloc();
David Reisse4d4ea02009-04-02 21:37:17 +0000686 }
687 string_buf_ = (uint8_t*)new_string_buf;
688 string_buf_size_ = size;
689 }
690 trans_->readAll(string_buf_, size);
691 str.assign((char*)string_buf_, size);
692
693 return rsize + (uint32_t)size;
694}
695
696/**
697 * Read an i32 from the wire as a varint. The MSB of each byte is set
698 * if there is another byte to follow. This can read up to 5 bytes.
699 */
David Reisse71115b2010-10-06 17:09:56 +0000700template <class Transport_>
701uint32_t TCompactProtocolT<Transport_>::readVarint32(int32_t& i32) {
David Reisse4d4ea02009-04-02 21:37:17 +0000702 int64_t val;
703 uint32_t rsize = readVarint64(val);
704 i32 = (int32_t)val;
705 return rsize;
706}
707
708/**
709 * Read an i64 from the wire as a proper varint. The MSB of each byte is set
710 * if there is another byte to follow. This can read up to 10 bytes.
711 */
David Reisse71115b2010-10-06 17:09:56 +0000712template <class Transport_>
713uint32_t TCompactProtocolT<Transport_>::readVarint64(int64_t& i64) {
David Reisse4d4ea02009-04-02 21:37:17 +0000714 uint32_t rsize = 0;
715 uint64_t val = 0;
716 int shift = 0;
717 uint8_t buf[10]; // 64 bits / (7 bits/byte) = 10 bytes.
718 uint32_t buf_size = sizeof(buf);
719 const uint8_t* borrowed = trans_->borrow(buf, &buf_size);
720
721 // Fast path.
722 if (borrowed != NULL) {
723 while (true) {
724 uint8_t byte = borrowed[rsize];
725 rsize++;
726 val |= (uint64_t)(byte & 0x7f) << shift;
727 shift += 7;
728 if (!(byte & 0x80)) {
729 i64 = val;
730 trans_->consume(rsize);
731 return rsize;
732 }
733 // Have to check for invalid data so we don't crash.
734 if (UNLIKELY(rsize == sizeof(buf))) {
735 throw TProtocolException(TProtocolException::INVALID_DATA, "Variable-length int over 10 bytes.");
736 }
737 }
738 }
739
740 // Slow path.
741 else {
742 while (true) {
743 uint8_t byte;
744 rsize += trans_->readAll(&byte, 1);
745 val |= (uint64_t)(byte & 0x7f) << shift;
746 shift += 7;
747 if (!(byte & 0x80)) {
748 i64 = val;
749 return rsize;
750 }
751 // Might as well check for invalid data on the slow path too.
752 if (UNLIKELY(rsize >= sizeof(buf))) {
753 throw TProtocolException(TProtocolException::INVALID_DATA, "Variable-length int over 10 bytes.");
754 }
755 }
756 }
757}
758
759/**
760 * Convert from zigzag int to int.
761 */
David Reisse71115b2010-10-06 17:09:56 +0000762template <class Transport_>
763int32_t TCompactProtocolT<Transport_>::zigzagToI32(uint32_t n) {
David Reisse4d4ea02009-04-02 21:37:17 +0000764 return (n >> 1) ^ -(n & 1);
765}
766
767/**
768 * Convert from zigzag long to long.
769 */
David Reisse71115b2010-10-06 17:09:56 +0000770template <class Transport_>
771int64_t TCompactProtocolT<Transport_>::zigzagToI64(uint64_t n) {
David Reisse4d4ea02009-04-02 21:37:17 +0000772 return (n >> 1) ^ -(n & 1);
773}
774
David Reisse71115b2010-10-06 17:09:56 +0000775template <class Transport_>
776TType TCompactProtocolT<Transport_>::getTType(int8_t type) {
David Reisse4d4ea02009-04-02 21:37:17 +0000777 switch (type) {
778 case T_STOP:
779 return T_STOP;
David Reisse71115b2010-10-06 17:09:56 +0000780 case detail::compact::CT_BOOLEAN_FALSE:
781 case detail::compact::CT_BOOLEAN_TRUE:
David Reisse4d4ea02009-04-02 21:37:17 +0000782 return T_BOOL;
David Reisse71115b2010-10-06 17:09:56 +0000783 case detail::compact::CT_BYTE:
David Reisse4d4ea02009-04-02 21:37:17 +0000784 return T_BYTE;
David Reisse71115b2010-10-06 17:09:56 +0000785 case detail::compact::CT_I16:
David Reisse4d4ea02009-04-02 21:37:17 +0000786 return T_I16;
David Reisse71115b2010-10-06 17:09:56 +0000787 case detail::compact::CT_I32:
David Reisse4d4ea02009-04-02 21:37:17 +0000788 return T_I32;
David Reisse71115b2010-10-06 17:09:56 +0000789 case detail::compact::CT_I64:
David Reisse4d4ea02009-04-02 21:37:17 +0000790 return T_I64;
David Reisse71115b2010-10-06 17:09:56 +0000791 case detail::compact::CT_DOUBLE:
David Reisse4d4ea02009-04-02 21:37:17 +0000792 return T_DOUBLE;
David Reisse71115b2010-10-06 17:09:56 +0000793 case detail::compact::CT_BINARY:
David Reisse4d4ea02009-04-02 21:37:17 +0000794 return T_STRING;
David Reisse71115b2010-10-06 17:09:56 +0000795 case detail::compact::CT_LIST:
David Reisse4d4ea02009-04-02 21:37:17 +0000796 return T_LIST;
David Reisse71115b2010-10-06 17:09:56 +0000797 case detail::compact::CT_SET:
David Reisse4d4ea02009-04-02 21:37:17 +0000798 return T_SET;
David Reisse71115b2010-10-06 17:09:56 +0000799 case detail::compact::CT_MAP:
David Reisse4d4ea02009-04-02 21:37:17 +0000800 return T_MAP;
David Reisse71115b2010-10-06 17:09:56 +0000801 case detail::compact::CT_STRUCT:
David Reisse4d4ea02009-04-02 21:37:17 +0000802 return T_STRUCT;
803 default:
804 throw TException("don't know what type: " + type);
805 }
806 return T_STOP;
807}
808
809}}} // apache::thrift::protocol
David Reisse71115b2010-10-06 17:09:56 +0000810
811#endif // _THRIFT_PROTOCOL_TCOMPACTPROTOCOL_TCC_