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) { |
| 22 | BOOST_STATIC_ASSERT(sizeof(From) == sizeof(To)); |
| 23 | |
| 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; |
| 35 | |
| 36 | // Slightly more questionable. |
| 37 | // Same code emitted by GCC. |
| 38 | //To to; |
| 39 | //memcpy(&to, &from, sizeof(from)); |
| 40 | //return to; |
| 41 | |
| 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; |
| 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) { |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 58 | return |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 59 | writeString(name) + |
| 60 | writeByte((int8_t)messageType) + |
| 61 | writeI32(seqid); |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 64 | uint32_t TBinaryProtocol::writeMessageEnd() { |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 65 | return 0; |
| 66 | } |
| 67 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 68 | uint32_t TBinaryProtocol::writeStructBegin(const string& name) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 69 | return 0; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 72 | uint32_t TBinaryProtocol::writeStructEnd() { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 73 | return 0; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 76 | uint32_t TBinaryProtocol::writeFieldBegin(const string& name, |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 77 | const TType fieldType, |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 78 | const int16_t fieldId) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 79 | return |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 80 | writeByte((int8_t)fieldType) + |
| 81 | writeI16(fieldId); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 84 | uint32_t TBinaryProtocol::writeFieldEnd() { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 85 | return 0; |
| 86 | } |
| 87 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 88 | uint32_t TBinaryProtocol::writeFieldStop() { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 89 | return |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 90 | writeByte((int8_t)T_STOP); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 93 | uint32_t TBinaryProtocol::writeMapBegin(const TType keyType, |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 94 | const TType valType, |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 95 | const uint32_t size) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 96 | return |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 97 | writeByte((int8_t)keyType) + |
| 98 | writeByte((int8_t)valType) + |
| 99 | writeI32((int32_t)size); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 102 | uint32_t TBinaryProtocol::writeMapEnd() { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 103 | return 0; |
| 104 | } |
| 105 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 106 | uint32_t TBinaryProtocol::writeListBegin(const TType elemType, |
| 107 | const uint32_t size) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 108 | return |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 109 | writeByte((int8_t) elemType) + |
| 110 | writeI32((int32_t)size); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 113 | uint32_t TBinaryProtocol::writeListEnd() { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 114 | return 0; |
| 115 | } |
| 116 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 117 | uint32_t TBinaryProtocol::writeSetBegin(const TType elemType, |
| 118 | const uint32_t size) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 119 | return |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 120 | writeByte((int8_t)elemType) + |
| 121 | writeI32((int32_t)size); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 124 | uint32_t TBinaryProtocol::writeSetEnd() { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 125 | return 0; |
| 126 | } |
| 127 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 128 | uint32_t TBinaryProtocol::writeBool(const bool value) { |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 129 | uint8_t tmp = value ? 1 : 0; |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 130 | trans_->write(&tmp, 1); |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 131 | return 1; |
| 132 | } |
| 133 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 134 | uint32_t TBinaryProtocol::writeByte(const int8_t byte) { |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 135 | trans_->write((uint8_t*)&byte, 1); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 136 | return 1; |
| 137 | } |
| 138 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 139 | uint32_t TBinaryProtocol::writeI16(const int16_t i16) { |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 140 | int16_t net = (int16_t)htons(i16); |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 141 | trans_->write((uint8_t*)&net, 2); |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 142 | return 2; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 145 | uint32_t TBinaryProtocol::writeI32(const int32_t i32) { |
Marc Slemko | e6889de | 2006-08-12 00:32:53 +0000 | [diff] [blame] | 146 | int32_t net = (int32_t)htonl(i32); |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 147 | trans_->write((uint8_t*)&net, 4); |
Marc Slemko | e6889de | 2006-08-12 00:32:53 +0000 | [diff] [blame] | 148 | return 4; |
| 149 | } |
| 150 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 151 | uint32_t TBinaryProtocol::writeI64(const int64_t i64) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 152 | int64_t net = (int64_t)htonll(i64); |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 153 | trans_->write((uint8_t*)&net, 8); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 154 | return 8; |
| 155 | } |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 156 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 157 | uint32_t TBinaryProtocol::writeDouble(const double dub) { |
Mark Slee | 4f261c5 | 2007-04-13 00:33:24 +0000 | [diff] [blame^] | 158 | BOOST_STATIC_ASSERT(sizeof(double) == sizeof(uint64_t)); |
| 159 | BOOST_STATIC_ASSERT(std::numeric_limits<double>::is_iec559); |
| 160 | |
| 161 | uint64_t bits = bitwise_cast<uint64_t>(dub); |
| 162 | bits = htonll(bits); |
| 163 | trans_->write((uint8_t*)&bits, 8); |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 164 | return 8; |
| 165 | } |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 166 | |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 167 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 168 | uint32_t TBinaryProtocol::writeString(const string& str) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 169 | uint32_t size = str.size(); |
| 170 | uint32_t result = writeI32((int32_t)size); |
| 171 | if (size > 0) { |
| 172 | trans_->write((uint8_t*)str.data(), size); |
| 173 | } |
| 174 | return result + size; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Reading functions |
| 179 | */ |
| 180 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 181 | uint32_t TBinaryProtocol::readMessageBegin(std::string& name, |
Marc Slemko | e6889de | 2006-08-12 00:32:53 +0000 | [diff] [blame] | 182 | TMessageType& messageType, |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 183 | int32_t& seqid) { |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 184 | |
| 185 | uint32_t result = 0; |
Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 186 | int8_t type; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 187 | result+= readString(name); |
| 188 | result+= readByte(type); |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 189 | messageType = (TMessageType)type; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 190 | result+= readI32(seqid); |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 191 | return result; |
| 192 | } |
| 193 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 194 | uint32_t TBinaryProtocol::readMessageEnd() { |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 195 | return 0; |
| 196 | } |
| 197 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 198 | uint32_t TBinaryProtocol::readStructBegin(string& name) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 199 | name = ""; |
| 200 | return 0; |
| 201 | } |
| 202 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 203 | uint32_t TBinaryProtocol::readStructEnd() { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 204 | return 0; |
| 205 | } |
| 206 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 207 | uint32_t TBinaryProtocol::readFieldBegin(string& name, |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 208 | TType& fieldType, |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 209 | int16_t& fieldId) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 210 | uint32_t result = 0; |
Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 211 | int8_t type; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 212 | result += readByte(type); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 213 | fieldType = (TType)type; |
| 214 | if (fieldType == T_STOP) { |
| 215 | fieldId = 0; |
| 216 | return result; |
| 217 | } |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 218 | result += readI16(fieldId); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 219 | return result; |
| 220 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 221 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 222 | uint32_t TBinaryProtocol::readFieldEnd() { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 223 | return 0; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 224 | } |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 225 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 226 | uint32_t TBinaryProtocol::readMapBegin(TType& keyType, |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 227 | TType& valType, |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 228 | uint32_t& size) { |
Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 229 | int8_t k, v; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 230 | uint32_t result = 0; |
Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 231 | int32_t sizei; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 232 | result += readByte(k); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 233 | keyType = (TType)k; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 234 | result += readByte(v); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 235 | valType = (TType)v; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 236 | result += readI32(sizei); |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 237 | if (sizei < 0) { |
| 238 | throw TProtocolException(TProtocolException::NEGATIVE_SIZE); |
| 239 | } else if (container_limit_ && sizei > container_limit_) { |
| 240 | throw TProtocolException(TProtocolException::SIZE_LIMIT); |
| 241 | } |
Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 242 | size = (uint32_t)sizei; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 243 | return result; |
| 244 | } |
| 245 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 246 | uint32_t TBinaryProtocol::readMapEnd() { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 247 | return 0; |
| 248 | } |
| 249 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 250 | uint32_t TBinaryProtocol::readListBegin(TType& elemType, |
| 251 | uint32_t& size) { |
Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 252 | int8_t e; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 253 | uint32_t result = 0; |
Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 254 | int32_t sizei; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 255 | result += readByte(e); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 256 | elemType = (TType)e; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 257 | result += readI32(sizei); |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 258 | if (sizei < 0) { |
| 259 | throw TProtocolException(TProtocolException::NEGATIVE_SIZE); |
| 260 | } else if (container_limit_ && sizei > container_limit_) { |
| 261 | throw TProtocolException(TProtocolException::SIZE_LIMIT); |
| 262 | } |
Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 263 | size = (uint32_t)sizei; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 264 | return result; |
| 265 | } |
| 266 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 267 | uint32_t TBinaryProtocol::readListEnd() { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 268 | return 0; |
| 269 | } |
| 270 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 271 | uint32_t TBinaryProtocol::readSetBegin(TType& elemType, |
| 272 | uint32_t& size) { |
Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 273 | int8_t e; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 274 | uint32_t result = 0; |
Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 275 | int32_t sizei; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 276 | result += readByte(e); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 277 | elemType = (TType)e; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 278 | result += readI32(sizei); |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 279 | if (sizei < 0) { |
| 280 | throw TProtocolException(TProtocolException::NEGATIVE_SIZE); |
| 281 | } else if (container_limit_ && sizei > container_limit_) { |
| 282 | throw TProtocolException(TProtocolException::SIZE_LIMIT); |
| 283 | } |
Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 284 | size = (uint32_t)sizei; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 285 | return result; |
| 286 | } |
| 287 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 288 | uint32_t TBinaryProtocol::readSetEnd() { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 289 | return 0; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 292 | uint32_t TBinaryProtocol::readBool(bool& value) { |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 293 | uint8_t b[1]; |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 294 | trans_->readAll(b, 1); |
Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 295 | value = *(int8_t*)b != 0; |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 296 | return 1; |
| 297 | } |
| 298 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 299 | uint32_t TBinaryProtocol::readByte(int8_t& byte) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 300 | uint8_t b[1]; |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 301 | trans_->readAll(b, 1); |
Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 302 | byte = *(int8_t*)b; |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 303 | return 1; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 306 | uint32_t TBinaryProtocol::readI16(int16_t& i16) { |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 307 | uint8_t b[2]; |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 308 | trans_->readAll(b, 2); |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 309 | i16 = *(int16_t*)b; |
| 310 | i16 = (int16_t)ntohs(i16); |
| 311 | return 2; |
| 312 | } |
| 313 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 314 | uint32_t TBinaryProtocol::readI32(int32_t& i32) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 315 | uint8_t b[4]; |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 316 | trans_->readAll(b, 4); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 317 | i32 = *(int32_t*)b; |
| 318 | i32 = (int32_t)ntohl(i32); |
| 319 | return 4; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 322 | uint32_t TBinaryProtocol::readI64(int64_t& i64) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 323 | uint8_t b[8]; |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 324 | trans_->readAll(b, 8); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 325 | i64 = *(int64_t*)b; |
| 326 | i64 = (int64_t)ntohll(i64); |
| 327 | return 8; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 330 | uint32_t TBinaryProtocol::readDouble(double& dub) { |
Mark Slee | 4f261c5 | 2007-04-13 00:33:24 +0000 | [diff] [blame^] | 331 | BOOST_STATIC_ASSERT(sizeof(double) == sizeof(uint64_t)); |
| 332 | BOOST_STATIC_ASSERT(std::numeric_limits<double>::is_iec559); |
| 333 | |
| 334 | uint64_t bits; |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 335 | uint8_t b[8]; |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 336 | trans_->readAll(b, 8); |
Mark Slee | 4f261c5 | 2007-04-13 00:33:24 +0000 | [diff] [blame^] | 337 | bits = *(uint64_t*)b; |
| 338 | bits = ntohll(bits); |
| 339 | dub = bitwise_cast<double>(bits); |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 340 | return 8; |
| 341 | } |
| 342 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 343 | uint32_t TBinaryProtocol::readString(string& str) { |
Mark Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame] | 344 | uint32_t result; |
Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 345 | int32_t size; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 346 | result = readI32(size); |
Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 347 | |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 348 | // Catch error cases |
| 349 | if (size < 0) { |
| 350 | throw TProtocolException(TProtocolException::NEGATIVE_SIZE); |
| 351 | } |
| 352 | if (string_limit_ > 0 && size > string_limit_) { |
| 353 | throw TProtocolException(TProtocolException::SIZE_LIMIT); |
| 354 | } |
| 355 | |
| 356 | // Catch empty string case |
| 357 | if (size == 0) { |
| 358 | str = ""; |
| 359 | return result; |
| 360 | } |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 361 | |
| 362 | // Use the heap here to prevent stack overflow for v. large strings |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 363 | if (size > string_buf_size_ || string_buf_ == NULL) { |
| 364 | string_buf_ = (uint8_t*)realloc(string_buf_, (uint32_t)size); |
| 365 | if (string_buf_ == NULL) { |
| 366 | string_buf_size_ = 0; |
| 367 | throw TProtocolException(TProtocolException::UNKNOWN, "Out of memory in TBinaryProtocol::readString"); |
| 368 | } |
| 369 | string_buf_size_ = size; |
| 370 | } |
| 371 | trans_->readAll(string_buf_, size); |
| 372 | str = string((char*)string_buf_, size); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 373 | |
Mark Slee | f3c322b | 2006-06-26 23:52:22 +0000 | [diff] [blame] | 374 | return result + (uint32_t)size; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 375 | } |
Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 376 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 377 | }}} // facebook::thrift::protocol |