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