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