David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | |
| 20 | #ifndef _THRIFT_PROTOCOL_TBINARYPROTOCOL_TCC_ |
| 21 | #define _THRIFT_PROTOCOL_TBINARYPROTOCOL_TCC_ 1 |
| 22 | |
Roger Meier | 4285ba2 | 2013-06-10 21:17:23 +0200 | [diff] [blame] | 23 | #include <thrift/protocol/TBinaryProtocol.h> |
David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 24 | |
| 25 | #include <limits> |
| 26 | |
| 27 | |
| 28 | namespace apache { namespace thrift { namespace protocol { |
| 29 | |
| 30 | template <class Transport_> |
| 31 | uint32_t TBinaryProtocolT<Transport_>::writeMessageBegin(const std::string& name, |
| 32 | const TMessageType messageType, |
| 33 | const int32_t seqid) { |
| 34 | if (this->strict_write_) { |
| 35 | int32_t version = (VERSION_1) | ((int32_t)messageType); |
| 36 | uint32_t wsize = 0; |
| 37 | wsize += writeI32(version); |
| 38 | wsize += writeString(name); |
| 39 | wsize += writeI32(seqid); |
| 40 | return wsize; |
| 41 | } else { |
| 42 | uint32_t wsize = 0; |
| 43 | wsize += writeString(name); |
| 44 | wsize += writeByte((int8_t)messageType); |
| 45 | wsize += writeI32(seqid); |
| 46 | return wsize; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | template <class Transport_> |
| 51 | uint32_t TBinaryProtocolT<Transport_>::writeMessageEnd() { |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | template <class Transport_> |
| 56 | uint32_t TBinaryProtocolT<Transport_>::writeStructBegin(const char* name) { |
Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 57 | (void) name; |
David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | template <class Transport_> |
| 62 | uint32_t TBinaryProtocolT<Transport_>::writeStructEnd() { |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | template <class Transport_> |
| 67 | uint32_t TBinaryProtocolT<Transport_>::writeFieldBegin(const char* name, |
| 68 | const TType fieldType, |
| 69 | const int16_t fieldId) { |
Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 70 | (void) name; |
David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 71 | uint32_t wsize = 0; |
| 72 | wsize += writeByte((int8_t)fieldType); |
| 73 | wsize += writeI16(fieldId); |
| 74 | return wsize; |
| 75 | } |
| 76 | |
| 77 | template <class Transport_> |
| 78 | uint32_t TBinaryProtocolT<Transport_>::writeFieldEnd() { |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | template <class Transport_> |
| 83 | uint32_t TBinaryProtocolT<Transport_>::writeFieldStop() { |
| 84 | return |
| 85 | writeByte((int8_t)T_STOP); |
| 86 | } |
| 87 | |
| 88 | template <class Transport_> |
| 89 | uint32_t TBinaryProtocolT<Transport_>::writeMapBegin(const TType keyType, |
| 90 | const TType valType, |
| 91 | const uint32_t size) { |
| 92 | uint32_t wsize = 0; |
| 93 | wsize += writeByte((int8_t)keyType); |
| 94 | wsize += writeByte((int8_t)valType); |
| 95 | wsize += writeI32((int32_t)size); |
| 96 | return wsize; |
| 97 | } |
| 98 | |
| 99 | template <class Transport_> |
| 100 | uint32_t TBinaryProtocolT<Transport_>::writeMapEnd() { |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | template <class Transport_> |
| 105 | uint32_t TBinaryProtocolT<Transport_>::writeListBegin(const TType elemType, |
| 106 | const uint32_t size) { |
| 107 | uint32_t wsize = 0; |
| 108 | wsize += writeByte((int8_t) elemType); |
| 109 | wsize += writeI32((int32_t)size); |
| 110 | return wsize; |
| 111 | } |
| 112 | |
| 113 | template <class Transport_> |
| 114 | uint32_t TBinaryProtocolT<Transport_>::writeListEnd() { |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | template <class Transport_> |
| 119 | uint32_t TBinaryProtocolT<Transport_>::writeSetBegin(const TType elemType, |
| 120 | const uint32_t size) { |
| 121 | uint32_t wsize = 0; |
| 122 | wsize += writeByte((int8_t)elemType); |
| 123 | wsize += writeI32((int32_t)size); |
| 124 | return wsize; |
| 125 | } |
| 126 | |
| 127 | template <class Transport_> |
| 128 | uint32_t TBinaryProtocolT<Transport_>::writeSetEnd() { |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | template <class Transport_> |
| 133 | uint32_t TBinaryProtocolT<Transport_>::writeBool(const bool value) { |
| 134 | uint8_t tmp = value ? 1 : 0; |
| 135 | this->trans_->write(&tmp, 1); |
| 136 | return 1; |
| 137 | } |
| 138 | |
| 139 | template <class Transport_> |
| 140 | uint32_t TBinaryProtocolT<Transport_>::writeByte(const int8_t byte) { |
| 141 | this->trans_->write((uint8_t*)&byte, 1); |
| 142 | return 1; |
| 143 | } |
| 144 | |
| 145 | template <class Transport_> |
| 146 | uint32_t TBinaryProtocolT<Transport_>::writeI16(const int16_t i16) { |
| 147 | int16_t net = (int16_t)htons(i16); |
| 148 | this->trans_->write((uint8_t*)&net, 2); |
| 149 | return 2; |
| 150 | } |
| 151 | |
| 152 | template <class Transport_> |
| 153 | uint32_t TBinaryProtocolT<Transport_>::writeI32(const int32_t i32) { |
| 154 | int32_t net = (int32_t)htonl(i32); |
| 155 | this->trans_->write((uint8_t*)&net, 4); |
| 156 | return 4; |
| 157 | } |
| 158 | |
| 159 | template <class Transport_> |
| 160 | uint32_t TBinaryProtocolT<Transport_>::writeI64(const int64_t i64) { |
| 161 | int64_t net = (int64_t)htonll(i64); |
| 162 | this->trans_->write((uint8_t*)&net, 8); |
| 163 | return 8; |
| 164 | } |
| 165 | |
| 166 | template <class Transport_> |
| 167 | uint32_t TBinaryProtocolT<Transport_>::writeDouble(const double dub) { |
| 168 | BOOST_STATIC_ASSERT(sizeof(double) == sizeof(uint64_t)); |
| 169 | BOOST_STATIC_ASSERT(std::numeric_limits<double>::is_iec559); |
| 170 | |
| 171 | uint64_t bits = bitwise_cast<uint64_t>(dub); |
| 172 | bits = htonll(bits); |
| 173 | this->trans_->write((uint8_t*)&bits, 8); |
| 174 | return 8; |
| 175 | } |
| 176 | |
| 177 | |
| 178 | template <class Transport_> |
Jake Farrell | f42ae01 | 2012-06-22 03:22:53 +0000 | [diff] [blame] | 179 | template<typename StrType> |
| 180 | uint32_t TBinaryProtocolT<Transport_>::writeString(const StrType& str) { |
Roger Meier | b69d24d | 2012-10-04 18:02:15 +0000 | [diff] [blame] | 181 | if(str.size() > static_cast<size_t>((std::numeric_limits<int32_t>::max)())) |
| 182 | throw TProtocolException(TProtocolException::SIZE_LIMIT); |
| 183 | uint32_t size = static_cast<uint32_t>(str.size()); |
David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 184 | uint32_t result = writeI32((int32_t)size); |
| 185 | if (size > 0) { |
| 186 | this->trans_->write((uint8_t*)str.data(), size); |
| 187 | } |
| 188 | return result + size; |
| 189 | } |
| 190 | |
| 191 | template <class Transport_> |
| 192 | uint32_t TBinaryProtocolT<Transport_>::writeBinary(const std::string& str) { |
| 193 | return TBinaryProtocolT<Transport_>::writeString(str); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Reading functions |
| 198 | */ |
| 199 | |
| 200 | template <class Transport_> |
| 201 | uint32_t TBinaryProtocolT<Transport_>::readMessageBegin(std::string& name, |
| 202 | TMessageType& messageType, |
| 203 | int32_t& seqid) { |
| 204 | uint32_t result = 0; |
| 205 | int32_t sz; |
| 206 | result += readI32(sz); |
| 207 | |
| 208 | if (sz < 0) { |
| 209 | // Check for correct version number |
| 210 | int32_t version = sz & VERSION_MASK; |
| 211 | if (version != VERSION_1) { |
| 212 | throw TProtocolException(TProtocolException::BAD_VERSION, "Bad version identifier"); |
| 213 | } |
| 214 | messageType = (TMessageType)(sz & 0x000000ff); |
| 215 | result += readString(name); |
| 216 | result += readI32(seqid); |
| 217 | } else { |
| 218 | if (this->strict_read_) { |
| 219 | throw TProtocolException(TProtocolException::BAD_VERSION, "No version identifier... old protocol client in strict mode?"); |
| 220 | } else { |
| 221 | // Handle pre-versioned input |
| 222 | int8_t type; |
| 223 | result += readStringBody(name, sz); |
| 224 | result += readByte(type); |
| 225 | messageType = (TMessageType)type; |
| 226 | result += readI32(seqid); |
| 227 | } |
| 228 | } |
| 229 | return result; |
| 230 | } |
| 231 | |
| 232 | template <class Transport_> |
| 233 | uint32_t TBinaryProtocolT<Transport_>::readMessageEnd() { |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | template <class Transport_> |
| 238 | uint32_t TBinaryProtocolT<Transport_>::readStructBegin(std::string& name) { |
| 239 | name = ""; |
| 240 | return 0; |
| 241 | } |
| 242 | |
| 243 | template <class Transport_> |
| 244 | uint32_t TBinaryProtocolT<Transport_>::readStructEnd() { |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | template <class Transport_> |
| 249 | uint32_t TBinaryProtocolT<Transport_>::readFieldBegin(std::string& name, |
| 250 | TType& fieldType, |
| 251 | int16_t& fieldId) { |
Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 252 | (void) name; |
David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 253 | uint32_t result = 0; |
| 254 | int8_t type; |
| 255 | result += readByte(type); |
| 256 | fieldType = (TType)type; |
| 257 | if (fieldType == T_STOP) { |
| 258 | fieldId = 0; |
| 259 | return result; |
| 260 | } |
| 261 | result += readI16(fieldId); |
| 262 | return result; |
| 263 | } |
| 264 | |
| 265 | template <class Transport_> |
| 266 | uint32_t TBinaryProtocolT<Transport_>::readFieldEnd() { |
| 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | template <class Transport_> |
| 271 | uint32_t TBinaryProtocolT<Transport_>::readMapBegin(TType& keyType, |
| 272 | TType& valType, |
| 273 | uint32_t& size) { |
| 274 | int8_t k, v; |
| 275 | uint32_t result = 0; |
| 276 | int32_t sizei; |
| 277 | result += readByte(k); |
| 278 | keyType = (TType)k; |
| 279 | result += readByte(v); |
| 280 | valType = (TType)v; |
| 281 | result += readI32(sizei); |
| 282 | if (sizei < 0) { |
| 283 | throw TProtocolException(TProtocolException::NEGATIVE_SIZE); |
| 284 | } else if (this->container_limit_ && sizei > this->container_limit_) { |
| 285 | throw TProtocolException(TProtocolException::SIZE_LIMIT); |
| 286 | } |
| 287 | size = (uint32_t)sizei; |
| 288 | return result; |
| 289 | } |
| 290 | |
| 291 | template <class Transport_> |
| 292 | uint32_t TBinaryProtocolT<Transport_>::readMapEnd() { |
| 293 | return 0; |
| 294 | } |
| 295 | |
| 296 | template <class Transport_> |
| 297 | uint32_t TBinaryProtocolT<Transport_>::readListBegin(TType& elemType, |
| 298 | uint32_t& size) { |
| 299 | int8_t e; |
| 300 | uint32_t result = 0; |
| 301 | int32_t sizei; |
| 302 | result += readByte(e); |
| 303 | elemType = (TType)e; |
| 304 | result += readI32(sizei); |
| 305 | if (sizei < 0) { |
| 306 | throw TProtocolException(TProtocolException::NEGATIVE_SIZE); |
| 307 | } else if (this->container_limit_ && sizei > this->container_limit_) { |
| 308 | throw TProtocolException(TProtocolException::SIZE_LIMIT); |
| 309 | } |
| 310 | size = (uint32_t)sizei; |
| 311 | return result; |
| 312 | } |
| 313 | |
| 314 | template <class Transport_> |
| 315 | uint32_t TBinaryProtocolT<Transport_>::readListEnd() { |
| 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | template <class Transport_> |
| 320 | uint32_t TBinaryProtocolT<Transport_>::readSetBegin(TType& elemType, |
| 321 | uint32_t& size) { |
| 322 | int8_t e; |
| 323 | uint32_t result = 0; |
| 324 | int32_t sizei; |
| 325 | result += readByte(e); |
| 326 | elemType = (TType)e; |
| 327 | result += readI32(sizei); |
| 328 | if (sizei < 0) { |
| 329 | throw TProtocolException(TProtocolException::NEGATIVE_SIZE); |
| 330 | } else if (this->container_limit_ && sizei > this->container_limit_) { |
| 331 | throw TProtocolException(TProtocolException::SIZE_LIMIT); |
| 332 | } |
| 333 | size = (uint32_t)sizei; |
| 334 | return result; |
| 335 | } |
| 336 | |
| 337 | template <class Transport_> |
| 338 | uint32_t TBinaryProtocolT<Transport_>::readSetEnd() { |
| 339 | return 0; |
| 340 | } |
| 341 | |
| 342 | template <class Transport_> |
| 343 | uint32_t TBinaryProtocolT<Transport_>::readBool(bool& value) { |
| 344 | uint8_t b[1]; |
| 345 | this->trans_->readAll(b, 1); |
| 346 | value = *(int8_t*)b != 0; |
| 347 | return 1; |
| 348 | } |
| 349 | |
| 350 | template <class Transport_> |
| 351 | uint32_t TBinaryProtocolT<Transport_>::readByte(int8_t& byte) { |
| 352 | uint8_t b[1]; |
| 353 | this->trans_->readAll(b, 1); |
| 354 | byte = *(int8_t*)b; |
| 355 | return 1; |
| 356 | } |
| 357 | |
| 358 | template <class Transport_> |
| 359 | uint32_t TBinaryProtocolT<Transport_>::readI16(int16_t& i16) { |
Christian Lavoie | 2bbc328 | 2011-02-08 23:05:47 +0000 | [diff] [blame] | 360 | union bytes { |
| 361 | uint8_t b[2]; |
| 362 | int16_t all; |
| 363 | } theBytes; |
| 364 | this->trans_->readAll(theBytes.b, 2); |
| 365 | i16 = (int16_t)ntohs(theBytes.all); |
David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 366 | return 2; |
| 367 | } |
| 368 | |
| 369 | template <class Transport_> |
| 370 | uint32_t TBinaryProtocolT<Transport_>::readI32(int32_t& i32) { |
Christian Lavoie | 2bbc328 | 2011-02-08 23:05:47 +0000 | [diff] [blame] | 371 | union bytes { |
| 372 | uint8_t b[4]; |
| 373 | int32_t all; |
| 374 | } theBytes; |
| 375 | this->trans_->readAll(theBytes.b, 4); |
| 376 | i32 = (int32_t)ntohl(theBytes.all); |
David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 377 | return 4; |
| 378 | } |
| 379 | |
| 380 | template <class Transport_> |
| 381 | uint32_t TBinaryProtocolT<Transport_>::readI64(int64_t& i64) { |
Christian Lavoie | 2bbc328 | 2011-02-08 23:05:47 +0000 | [diff] [blame] | 382 | union bytes { |
| 383 | uint8_t b[8]; |
| 384 | int64_t all; |
| 385 | } theBytes; |
| 386 | this->trans_->readAll(theBytes.b, 8); |
| 387 | i64 = (int64_t)ntohll(theBytes.all); |
David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 388 | return 8; |
| 389 | } |
| 390 | |
| 391 | template <class Transport_> |
| 392 | uint32_t TBinaryProtocolT<Transport_>::readDouble(double& dub) { |
| 393 | BOOST_STATIC_ASSERT(sizeof(double) == sizeof(uint64_t)); |
| 394 | BOOST_STATIC_ASSERT(std::numeric_limits<double>::is_iec559); |
| 395 | |
Christian Lavoie | 2bbc328 | 2011-02-08 23:05:47 +0000 | [diff] [blame] | 396 | union bytes { |
| 397 | uint8_t b[8]; |
| 398 | uint64_t all; |
| 399 | } theBytes; |
| 400 | this->trans_->readAll(theBytes.b, 8); |
| 401 | theBytes.all = ntohll(theBytes.all); |
| 402 | dub = bitwise_cast<double>(theBytes.all); |
David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 403 | return 8; |
| 404 | } |
| 405 | |
| 406 | template <class Transport_> |
Jake Farrell | f42ae01 | 2012-06-22 03:22:53 +0000 | [diff] [blame] | 407 | template<typename StrType> |
| 408 | uint32_t TBinaryProtocolT<Transport_>::readString(StrType& str) { |
David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 409 | uint32_t result; |
| 410 | int32_t size; |
| 411 | result = readI32(size); |
| 412 | return result + readStringBody(str, size); |
| 413 | } |
| 414 | |
| 415 | template <class Transport_> |
| 416 | uint32_t TBinaryProtocolT<Transport_>::readBinary(std::string& str) { |
| 417 | return TBinaryProtocolT<Transport_>::readString(str); |
| 418 | } |
| 419 | |
| 420 | template <class Transport_> |
Jake Farrell | f42ae01 | 2012-06-22 03:22:53 +0000 | [diff] [blame] | 421 | template<typename StrType> |
| 422 | uint32_t TBinaryProtocolT<Transport_>::readStringBody(StrType& str, |
David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 423 | int32_t size) { |
| 424 | uint32_t result = 0; |
| 425 | |
| 426 | // Catch error cases |
| 427 | if (size < 0) { |
| 428 | throw TProtocolException(TProtocolException::NEGATIVE_SIZE); |
| 429 | } |
| 430 | if (this->string_limit_ > 0 && size > this->string_limit_) { |
| 431 | throw TProtocolException(TProtocolException::SIZE_LIMIT); |
| 432 | } |
| 433 | |
| 434 | // Catch empty string case |
| 435 | if (size == 0) { |
Roger Meier | 92a90ff | 2012-04-13 14:50:32 +0000 | [diff] [blame] | 436 | str.clear(); |
David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 437 | return result; |
| 438 | } |
| 439 | |
| 440 | // Try to borrow first |
| 441 | const uint8_t* borrow_buf; |
| 442 | uint32_t got = size; |
| 443 | if ((borrow_buf = this->trans_->borrow(NULL, &got))) { |
| 444 | str.assign((const char*)borrow_buf, size); |
| 445 | this->trans_->consume(size); |
| 446 | return size; |
| 447 | } |
| 448 | |
Ben Craig | fd64c15 | 2013-10-09 15:26:05 -0500 | [diff] [blame] | 449 | str.resize(size); |
| 450 | this->trans_->readAll(reinterpret_cast<uint8_t *>(&str[0]), size); |
David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 451 | return (uint32_t)size; |
| 452 | } |
| 453 | |
| 454 | }}} // apache::thrift::protocol |
| 455 | |
| 456 | #endif // #ifndef _THRIFT_PROTOCOL_TBINARYPROTOCOL_TCC_ |