David Reiss | ea2cba8 | 2009-03-30 21:35:00 +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 | */ |
Mark Slee | 9f0c651 | 2007-02-28 23:58:26 +0000 | [diff] [blame] | 19 | |
Marc Slemko | d42a2c2 | 2006-08-10 03:30:18 +0000 | [diff] [blame] | 20 | #include "TBinaryProtocol.h" |
| 21 | |
David Reiss | 32e95f8 | 2008-06-11 01:18:36 +0000 | [diff] [blame] | 22 | #include <limits> |
Mark Slee | 4f261c5 | 2007-04-13 00:33:24 +0000 | [diff] [blame] | 23 | #include <boost/static_assert.hpp> |
| 24 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 25 | using std::string; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 26 | |
Mark Slee | 4f261c5 | 2007-04-13 00:33:24 +0000 | [diff] [blame] | 27 | // Use this to get around strict aliasing rules. |
| 28 | // For example, uint64_t i = bitwise_cast<uint64_t>(returns_double()); |
| 29 | // The most obvious implementation is to just cast a pointer, |
| 30 | // but that doesn't work. |
| 31 | // For a pretty in-depth explanation of the problem, see |
| 32 | // http://www.cellperformance.com/mike_acton/2006/06/ (...) |
| 33 | // understanding_strict_aliasing.html |
| 34 | template <typename To, typename From> |
| 35 | static inline To bitwise_cast(From from) { |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 36 | BOOST_STATIC_ASSERT(sizeof(From) == sizeof(To)); |
Mark Slee | 4f261c5 | 2007-04-13 00:33:24 +0000 | [diff] [blame] | 37 | |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 38 | // BAD!!! These are all broken with -O2. |
| 39 | //return *reinterpret_cast<To*>(&from); // BAD!!! |
| 40 | //return *static_cast<To*>(static_cast<void*>(&from)); // BAD!!! |
| 41 | //return *(To*)(void*)&from; // BAD!!! |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 42 | |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 43 | // Super clean and paritally blessed by section 3.9 of the standard. |
| 44 | //unsigned char c[sizeof(from)]; |
| 45 | //memcpy(c, &from, sizeof(from)); |
| 46 | //To to; |
| 47 | //memcpy(&to, c, sizeof(c)); |
| 48 | //return to; |
Mark Slee | 4f261c5 | 2007-04-13 00:33:24 +0000 | [diff] [blame] | 49 | |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 50 | // Slightly more questionable. |
| 51 | // Same code emitted by GCC. |
| 52 | //To to; |
| 53 | //memcpy(&to, &from, sizeof(from)); |
| 54 | //return to; |
Mark Slee | 4f261c5 | 2007-04-13 00:33:24 +0000 | [diff] [blame] | 55 | |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 56 | // Technically undefined, but almost universally supported, |
| 57 | // and the most efficient implementation. |
| 58 | union { |
| 59 | From f; |
| 60 | To t; |
| 61 | } u; |
| 62 | u.f = from; |
| 63 | return u.t; |
Mark Slee | 4f261c5 | 2007-04-13 00:33:24 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 67 | namespace apache { namespace thrift { namespace protocol { |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 68 | |
Mark Slee | 82a6c0f | 2007-04-04 21:08:21 +0000 | [diff] [blame] | 69 | uint32_t TBinaryProtocol::writeMessageBegin(const std::string& name, |
| 70 | const TMessageType messageType, |
| 71 | const int32_t seqid) { |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 72 | if (strict_write_) { |
| 73 | int32_t version = (VERSION_1) | ((int32_t)messageType); |
David Reiss | f79031e | 2007-07-06 21:43:48 +0000 | [diff] [blame] | 74 | uint32_t wsize = 0; |
| 75 | wsize += writeI32(version); |
| 76 | wsize += writeString(name); |
| 77 | wsize += writeI32(seqid); |
| 78 | return wsize; |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 79 | } else { |
David Reiss | f79031e | 2007-07-06 21:43:48 +0000 | [diff] [blame] | 80 | uint32_t wsize = 0; |
| 81 | wsize += writeString(name); |
| 82 | wsize += writeByte((int8_t)messageType); |
| 83 | wsize += writeI32(seqid); |
| 84 | return wsize; |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 85 | } |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 88 | uint32_t TBinaryProtocol::writeMessageEnd() { |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 89 | return 0; |
| 90 | } |
| 91 | |
David Reiss | 6412000 | 2008-04-29 23:12:24 +0000 | [diff] [blame] | 92 | uint32_t TBinaryProtocol::writeStructBegin(const char* name) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 93 | return 0; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 96 | uint32_t TBinaryProtocol::writeStructEnd() { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 97 | return 0; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 98 | } |
| 99 | |
David Reiss | 6412000 | 2008-04-29 23:12:24 +0000 | [diff] [blame] | 100 | uint32_t TBinaryProtocol::writeFieldBegin(const char* name, |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 101 | const TType fieldType, |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 102 | const int16_t fieldId) { |
David Reiss | f79031e | 2007-07-06 21:43:48 +0000 | [diff] [blame] | 103 | uint32_t wsize = 0; |
| 104 | wsize += writeByte((int8_t)fieldType); |
| 105 | wsize += writeI16(fieldId); |
| 106 | return wsize; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 109 | uint32_t TBinaryProtocol::writeFieldEnd() { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 110 | return 0; |
| 111 | } |
| 112 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 113 | uint32_t TBinaryProtocol::writeFieldStop() { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 114 | return |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 115 | writeByte((int8_t)T_STOP); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 118 | uint32_t TBinaryProtocol::writeMapBegin(const TType keyType, |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 119 | const TType valType, |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 120 | const uint32_t size) { |
David Reiss | f79031e | 2007-07-06 21:43:48 +0000 | [diff] [blame] | 121 | uint32_t wsize = 0; |
| 122 | wsize += writeByte((int8_t)keyType); |
| 123 | wsize += writeByte((int8_t)valType); |
| 124 | wsize += writeI32((int32_t)size); |
| 125 | return wsize; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 128 | uint32_t TBinaryProtocol::writeMapEnd() { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 129 | return 0; |
| 130 | } |
| 131 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 132 | uint32_t TBinaryProtocol::writeListBegin(const TType elemType, |
| 133 | const uint32_t size) { |
David Reiss | f79031e | 2007-07-06 21:43:48 +0000 | [diff] [blame] | 134 | uint32_t wsize = 0; |
| 135 | wsize += writeByte((int8_t) elemType); |
| 136 | wsize += writeI32((int32_t)size); |
| 137 | return wsize; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 140 | uint32_t TBinaryProtocol::writeListEnd() { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 141 | return 0; |
| 142 | } |
| 143 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 144 | uint32_t TBinaryProtocol::writeSetBegin(const TType elemType, |
| 145 | const uint32_t size) { |
David Reiss | f79031e | 2007-07-06 21:43:48 +0000 | [diff] [blame] | 146 | uint32_t wsize = 0; |
David Reiss | f79031e | 2007-07-06 21:43:48 +0000 | [diff] [blame] | 147 | wsize += writeByte((int8_t)elemType); |
| 148 | wsize += writeI32((int32_t)size); |
| 149 | return wsize; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 152 | uint32_t TBinaryProtocol::writeSetEnd() { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 153 | return 0; |
| 154 | } |
| 155 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 156 | uint32_t TBinaryProtocol::writeBool(const bool value) { |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 157 | uint8_t tmp = value ? 1 : 0; |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 158 | trans_->write(&tmp, 1); |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 159 | return 1; |
| 160 | } |
| 161 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 162 | uint32_t TBinaryProtocol::writeByte(const int8_t byte) { |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 163 | trans_->write((uint8_t*)&byte, 1); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 164 | return 1; |
| 165 | } |
| 166 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 167 | uint32_t TBinaryProtocol::writeI16(const int16_t i16) { |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 168 | int16_t net = (int16_t)htons(i16); |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 169 | trans_->write((uint8_t*)&net, 2); |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 170 | return 2; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 173 | uint32_t TBinaryProtocol::writeI32(const int32_t i32) { |
Marc Slemko | e6889de | 2006-08-12 00:32:53 +0000 | [diff] [blame] | 174 | int32_t net = (int32_t)htonl(i32); |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 175 | trans_->write((uint8_t*)&net, 4); |
Marc Slemko | e6889de | 2006-08-12 00:32:53 +0000 | [diff] [blame] | 176 | return 4; |
| 177 | } |
| 178 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 179 | uint32_t TBinaryProtocol::writeI64(const int64_t i64) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 180 | int64_t net = (int64_t)htonll(i64); |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 181 | trans_->write((uint8_t*)&net, 8); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 182 | return 8; |
| 183 | } |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 184 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 185 | uint32_t TBinaryProtocol::writeDouble(const double dub) { |
Mark Slee | 4f261c5 | 2007-04-13 00:33:24 +0000 | [diff] [blame] | 186 | BOOST_STATIC_ASSERT(sizeof(double) == sizeof(uint64_t)); |
| 187 | BOOST_STATIC_ASSERT(std::numeric_limits<double>::is_iec559); |
| 188 | |
| 189 | uint64_t bits = bitwise_cast<uint64_t>(dub); |
| 190 | bits = htonll(bits); |
| 191 | trans_->write((uint8_t*)&bits, 8); |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 192 | return 8; |
| 193 | } |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 194 | |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 195 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 196 | uint32_t TBinaryProtocol::writeString(const string& str) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 197 | uint32_t size = str.size(); |
| 198 | uint32_t result = writeI32((int32_t)size); |
| 199 | if (size > 0) { |
| 200 | trans_->write((uint8_t*)str.data(), size); |
| 201 | } |
| 202 | return result + size; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 203 | } |
| 204 | |
David Reiss | c005b1b | 2008-02-15 01:38:18 +0000 | [diff] [blame] | 205 | uint32_t TBinaryProtocol::writeBinary(const string& str) { |
| 206 | return TBinaryProtocol::writeString(str); |
| 207 | } |
| 208 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 209 | /** |
| 210 | * Reading functions |
| 211 | */ |
| 212 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 213 | uint32_t TBinaryProtocol::readMessageBegin(std::string& name, |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 214 | TMessageType& messageType, |
| 215 | int32_t& seqid) { |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 216 | uint32_t result = 0; |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 217 | int32_t sz; |
| 218 | result += readI32(sz); |
| 219 | |
| 220 | if (sz < 0) { |
| 221 | // Check for correct version number |
| 222 | int32_t version = sz & VERSION_MASK; |
| 223 | if (version != VERSION_1) { |
| 224 | throw TProtocolException(TProtocolException::BAD_VERSION, "Bad version identifier"); |
| 225 | } |
| 226 | messageType = (TMessageType)(sz & 0x000000ff); |
| 227 | result += readString(name); |
| 228 | result += readI32(seqid); |
| 229 | } else { |
| 230 | if (strict_read_) { |
| 231 | throw TProtocolException(TProtocolException::BAD_VERSION, "No version identifier... old protocol client in strict mode?"); |
| 232 | } else { |
| 233 | // Handle pre-versioned input |
| 234 | int8_t type; |
| 235 | result += readStringBody(name, sz); |
| 236 | result += readByte(type); |
| 237 | messageType = (TMessageType)type; |
| 238 | result += readI32(seqid); |
| 239 | } |
| 240 | } |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 241 | return result; |
| 242 | } |
| 243 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 244 | uint32_t TBinaryProtocol::readMessageEnd() { |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 245 | return 0; |
| 246 | } |
| 247 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 248 | uint32_t TBinaryProtocol::readStructBegin(string& name) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 249 | name = ""; |
| 250 | return 0; |
| 251 | } |
| 252 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 253 | uint32_t TBinaryProtocol::readStructEnd() { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 254 | return 0; |
| 255 | } |
| 256 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 257 | uint32_t TBinaryProtocol::readFieldBegin(string& name, |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 258 | TType& fieldType, |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 259 | int16_t& fieldId) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 260 | uint32_t result = 0; |
Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 261 | int8_t type; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 262 | result += readByte(type); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 263 | fieldType = (TType)type; |
| 264 | if (fieldType == T_STOP) { |
| 265 | fieldId = 0; |
| 266 | return result; |
| 267 | } |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 268 | result += readI16(fieldId); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 269 | return result; |
| 270 | } |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 271 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 272 | uint32_t TBinaryProtocol::readFieldEnd() { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 273 | return 0; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 274 | } |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 275 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 276 | uint32_t TBinaryProtocol::readMapBegin(TType& keyType, |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 277 | TType& valType, |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 278 | uint32_t& size) { |
Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 279 | int8_t k, v; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 280 | uint32_t result = 0; |
Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 281 | int32_t sizei; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 282 | result += readByte(k); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 283 | keyType = (TType)k; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 284 | result += readByte(v); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 285 | valType = (TType)v; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 286 | result += readI32(sizei); |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 287 | if (sizei < 0) { |
| 288 | throw TProtocolException(TProtocolException::NEGATIVE_SIZE); |
| 289 | } else if (container_limit_ && sizei > container_limit_) { |
| 290 | throw TProtocolException(TProtocolException::SIZE_LIMIT); |
| 291 | } |
Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 292 | size = (uint32_t)sizei; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 293 | return result; |
| 294 | } |
| 295 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 296 | uint32_t TBinaryProtocol::readMapEnd() { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 297 | return 0; |
| 298 | } |
| 299 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 300 | uint32_t TBinaryProtocol::readListBegin(TType& elemType, |
| 301 | uint32_t& size) { |
Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 302 | int8_t e; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 303 | uint32_t result = 0; |
Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 304 | int32_t sizei; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 305 | result += readByte(e); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 306 | elemType = (TType)e; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 307 | result += readI32(sizei); |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 308 | if (sizei < 0) { |
| 309 | throw TProtocolException(TProtocolException::NEGATIVE_SIZE); |
| 310 | } else if (container_limit_ && sizei > container_limit_) { |
| 311 | throw TProtocolException(TProtocolException::SIZE_LIMIT); |
| 312 | } |
Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 313 | size = (uint32_t)sizei; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 314 | return result; |
| 315 | } |
| 316 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 317 | uint32_t TBinaryProtocol::readListEnd() { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 318 | return 0; |
| 319 | } |
| 320 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 321 | uint32_t TBinaryProtocol::readSetBegin(TType& elemType, |
| 322 | uint32_t& size) { |
Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 323 | int8_t e; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 324 | uint32_t result = 0; |
Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 325 | int32_t sizei; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 326 | result += readByte(e); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 327 | elemType = (TType)e; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 328 | result += readI32(sizei); |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 329 | if (sizei < 0) { |
| 330 | throw TProtocolException(TProtocolException::NEGATIVE_SIZE); |
| 331 | } else if (container_limit_ && sizei > container_limit_) { |
| 332 | throw TProtocolException(TProtocolException::SIZE_LIMIT); |
| 333 | } |
Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 334 | size = (uint32_t)sizei; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 335 | return result; |
| 336 | } |
| 337 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 338 | uint32_t TBinaryProtocol::readSetEnd() { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 339 | return 0; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 340 | } |
| 341 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 342 | uint32_t TBinaryProtocol::readBool(bool& value) { |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 343 | uint8_t b[1]; |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 344 | trans_->readAll(b, 1); |
Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 345 | value = *(int8_t*)b != 0; |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 346 | return 1; |
| 347 | } |
| 348 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 349 | uint32_t TBinaryProtocol::readByte(int8_t& byte) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 350 | uint8_t b[1]; |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 351 | trans_->readAll(b, 1); |
Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 352 | byte = *(int8_t*)b; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 353 | return 1; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 356 | uint32_t TBinaryProtocol::readI16(int16_t& i16) { |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 357 | uint8_t b[2]; |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 358 | trans_->readAll(b, 2); |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 359 | i16 = *(int16_t*)b; |
| 360 | i16 = (int16_t)ntohs(i16); |
| 361 | return 2; |
| 362 | } |
| 363 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 364 | uint32_t TBinaryProtocol::readI32(int32_t& i32) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 365 | uint8_t b[4]; |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 366 | trans_->readAll(b, 4); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 367 | i32 = *(int32_t*)b; |
| 368 | i32 = (int32_t)ntohl(i32); |
| 369 | return 4; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 370 | } |
| 371 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 372 | uint32_t TBinaryProtocol::readI64(int64_t& i64) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 373 | uint8_t b[8]; |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 374 | trans_->readAll(b, 8); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 375 | i64 = *(int64_t*)b; |
| 376 | i64 = (int64_t)ntohll(i64); |
| 377 | return 8; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 380 | uint32_t TBinaryProtocol::readDouble(double& dub) { |
Mark Slee | 4f261c5 | 2007-04-13 00:33:24 +0000 | [diff] [blame] | 381 | BOOST_STATIC_ASSERT(sizeof(double) == sizeof(uint64_t)); |
| 382 | BOOST_STATIC_ASSERT(std::numeric_limits<double>::is_iec559); |
| 383 | |
| 384 | uint64_t bits; |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 385 | uint8_t b[8]; |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 386 | trans_->readAll(b, 8); |
Mark Slee | 4f261c5 | 2007-04-13 00:33:24 +0000 | [diff] [blame] | 387 | bits = *(uint64_t*)b; |
| 388 | bits = ntohll(bits); |
| 389 | dub = bitwise_cast<double>(bits); |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 390 | return 8; |
| 391 | } |
| 392 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 393 | uint32_t TBinaryProtocol::readString(string& str) { |
Mark Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame] | 394 | uint32_t result; |
Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 395 | int32_t size; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 396 | result = readI32(size); |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 397 | return result + readStringBody(str, size); |
| 398 | } |
| 399 | |
David Reiss | c005b1b | 2008-02-15 01:38:18 +0000 | [diff] [blame] | 400 | uint32_t TBinaryProtocol::readBinary(string& str) { |
| 401 | return TBinaryProtocol::readString(str); |
| 402 | } |
| 403 | |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 404 | uint32_t TBinaryProtocol::readStringBody(string& str, int32_t size) { |
| 405 | uint32_t result = 0; |
Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 406 | |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 407 | // Catch error cases |
| 408 | if (size < 0) { |
| 409 | throw TProtocolException(TProtocolException::NEGATIVE_SIZE); |
| 410 | } |
| 411 | if (string_limit_ > 0 && size > string_limit_) { |
| 412 | throw TProtocolException(TProtocolException::SIZE_LIMIT); |
| 413 | } |
| 414 | |
| 415 | // Catch empty string case |
| 416 | if (size == 0) { |
| 417 | str = ""; |
| 418 | return result; |
| 419 | } |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 420 | |
| 421 | // Use the heap here to prevent stack overflow for v. large strings |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 422 | if (size > string_buf_size_ || string_buf_ == NULL) { |
David Reiss | 58b4fa7 | 2008-04-01 04:17:58 +0000 | [diff] [blame] | 423 | void* new_string_buf = std::realloc(string_buf_, (uint32_t)size); |
| 424 | if (new_string_buf == NULL) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 425 | throw TProtocolException(TProtocolException::UNKNOWN, "Out of memory in TBinaryProtocol::readString"); |
| 426 | } |
David Reiss | 58b4fa7 | 2008-04-01 04:17:58 +0000 | [diff] [blame] | 427 | string_buf_ = (uint8_t*)new_string_buf; |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 428 | string_buf_size_ = size; |
| 429 | } |
| 430 | trans_->readAll(string_buf_, size); |
| 431 | str = string((char*)string_buf_, size); |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 432 | return (uint32_t)size; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 433 | } |
Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 434 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 435 | }}} // apache::thrift::protocol |