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