blob: 9fb2ec3679e640f00d4df023f0606141e00aea5e [file] [log] [blame]
Marc Slemkod42a2c22006-08-10 03:30:18 +00001#include "TBinaryProtocol.h"
2
Mark Slee8d7e1f62006-06-07 06:48:56 +00003using std::string;
Mark Sleee8540632006-05-30 09:24:40 +00004
Marc Slemko6f038a72006-08-03 18:58:09 +00005namespace facebook { namespace thrift { namespace protocol {
6
Marc Slemko16698852006-08-04 03:16:10 +00007uint32_t TBinaryProtocol::writeMessageBegin(shared_ptr<TTransport> out,
Marc Slemko91f67482006-08-11 23:58:57 +00008 const std::string name,
Marc Slemko16698852006-08-04 03:16:10 +00009 const TMessageType messageType,
Mark Sleecfc01932006-09-01 22:18:16 +000010 const int32_t seqid) const {
Marc Slemko16698852006-08-04 03:16:10 +000011 return
Marc Slemko91f67482006-08-11 23:58:57 +000012 writeString(out, name) +
Mark Sleecfc01932006-09-01 22:18:16 +000013 writeByte(out, (int8_t)messageType) +
14 writeI32(out, seqid);
Marc Slemko16698852006-08-04 03:16:10 +000015}
16
17uint32_t TBinaryProtocol::writeMessageEnd(shared_ptr<TTransport> out) const {
18 return 0;
19}
20
21uint32_t TBinaryProtocol::writeStructBegin(shared_ptr<TTransport> out,
Mark Slee8d7e1f62006-06-07 06:48:56 +000022 const string& name) const {
23 return 0;
Mark Sleee8540632006-05-30 09:24:40 +000024}
25
Marc Slemko16698852006-08-04 03:16:10 +000026uint32_t TBinaryProtocol::writeStructEnd(shared_ptr<TTransport> out) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +000027 return 0;
Mark Sleee8540632006-05-30 09:24:40 +000028}
29
Marc Slemko16698852006-08-04 03:16:10 +000030uint32_t TBinaryProtocol::writeFieldBegin(shared_ptr<TTransport> out,
Mark Slee8d7e1f62006-06-07 06:48:56 +000031 const string& name,
32 const TType fieldType,
Marc Slemko0b4ffa92006-08-11 02:49:29 +000033 const int16_t fieldId) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +000034 return
Mark Sleecfc01932006-09-01 22:18:16 +000035 writeByte(out, (int8_t)fieldType) +
Marc Slemko0b4ffa92006-08-11 02:49:29 +000036 writeI16(out, fieldId);
Mark Slee8d7e1f62006-06-07 06:48:56 +000037}
38
Marc Slemko16698852006-08-04 03:16:10 +000039uint32_t TBinaryProtocol::writeFieldEnd(shared_ptr<TTransport> out) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +000040 return 0;
41}
42
Marc Slemko16698852006-08-04 03:16:10 +000043uint32_t TBinaryProtocol::writeFieldStop(shared_ptr<TTransport> out) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +000044 return
Mark Sleecfc01932006-09-01 22:18:16 +000045 writeByte(out, (int8_t)T_STOP);
Mark Slee8d7e1f62006-06-07 06:48:56 +000046}
47
Marc Slemko16698852006-08-04 03:16:10 +000048uint32_t TBinaryProtocol::writeMapBegin(shared_ptr<TTransport> out,
Mark Slee8d7e1f62006-06-07 06:48:56 +000049 const TType keyType,
50 const TType valType,
Marc Slemkob09f5882006-08-23 22:03:34 +000051 const uint32_t size) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +000052 return
Mark Sleecfc01932006-09-01 22:18:16 +000053 writeByte(out, (int8_t)keyType) +
54 writeByte(out, (int8_t)valType) +
55 writeI32(out, (int32_t)size);
Mark Slee8d7e1f62006-06-07 06:48:56 +000056}
57
Marc Slemko16698852006-08-04 03:16:10 +000058uint32_t TBinaryProtocol::writeMapEnd(shared_ptr<TTransport> out) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +000059 return 0;
60}
61
Marc Slemko16698852006-08-04 03:16:10 +000062uint32_t TBinaryProtocol::writeListBegin(shared_ptr<TTransport> out,
Mark Slee8d7e1f62006-06-07 06:48:56 +000063 const TType elemType,
Marc Slemkob09f5882006-08-23 22:03:34 +000064 const uint32_t size) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +000065 return
Mark Sleecfc01932006-09-01 22:18:16 +000066 writeByte(out, (int8_t) elemType) +
67 writeI32(out, (int32_t)size);
Mark Slee8d7e1f62006-06-07 06:48:56 +000068}
69
Marc Slemko16698852006-08-04 03:16:10 +000070uint32_t TBinaryProtocol::writeListEnd(shared_ptr<TTransport> out) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +000071 return 0;
72}
73
Marc Slemko16698852006-08-04 03:16:10 +000074uint32_t TBinaryProtocol::writeSetBegin(shared_ptr<TTransport> out,
Mark Slee8d7e1f62006-06-07 06:48:56 +000075 const TType elemType,
Marc Slemkob09f5882006-08-23 22:03:34 +000076 const uint32_t size) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +000077 return
Mark Sleecfc01932006-09-01 22:18:16 +000078 writeByte(out, (int8_t)elemType) +
79 writeI32(out, (int32_t)size);
Mark Slee8d7e1f62006-06-07 06:48:56 +000080}
81
Marc Slemko16698852006-08-04 03:16:10 +000082uint32_t TBinaryProtocol::writeSetEnd(shared_ptr<TTransport> out) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +000083 return 0;
84}
85
Marc Slemko0b4ffa92006-08-11 02:49:29 +000086uint32_t TBinaryProtocol::writeBool(shared_ptr<TTransport> out,
87 const bool value) const {
88 uint8_t tmp = value ? 1 : 0;
89 out->write(&tmp, 1);
90 return 1;
91}
92
Marc Slemko16698852006-08-04 03:16:10 +000093uint32_t TBinaryProtocol::writeByte(shared_ptr<TTransport> out,
Mark Sleecfc01932006-09-01 22:18:16 +000094 const int8_t byte) const {
95 out->write((uint8_t*)&byte, 1);
Mark Slee8d7e1f62006-06-07 06:48:56 +000096 return 1;
97}
98
Marc Slemko0b4ffa92006-08-11 02:49:29 +000099uint32_t TBinaryProtocol::writeI16(shared_ptr<TTransport> out,
100 const int16_t i16) const {
101 int16_t net = (int16_t)htons(i16);
102 out->write((uint8_t*)&net, 2);
103 return 2;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000104}
105
Marc Slemkoe6889de2006-08-12 00:32:53 +0000106uint32_t TBinaryProtocol::writeI32(shared_ptr<TTransport> out,
107 const int32_t i32) const {
108 int32_t net = (int32_t)htonl(i32);
109 out->write((uint8_t*)&net, 4);
110 return 4;
111}
112
Marc Slemko16698852006-08-04 03:16:10 +0000113uint32_t TBinaryProtocol::writeI64(shared_ptr<TTransport> out,
Mark Slee8d7e1f62006-06-07 06:48:56 +0000114 const int64_t i64) const {
115 int64_t net = (int64_t)htonll(i64);
116 out->write((uint8_t*)&net, 8);
117 return 8;
118}
Mark Sleec98d0502006-09-06 02:42:25 +0000119
120uint32_t TBinaryProtocol::writeDouble(shared_ptr<TTransport> out,
121 const double dub) const {
122 uint8_t b[8];
123 uint8_t* d = (uint8_t*)&dub;
124 b[0] = d[7];
125 b[1] = d[6];
126 b[2] = d[5];
127 b[3] = d[4];
128 b[4] = d[3];
129 b[5] = d[2];
130 b[6] = d[1];
131 b[7] = d[0];
132 out->write((uint8_t*)b, 8);
133 return 8;
134}
Mark Slee8d7e1f62006-06-07 06:48:56 +0000135
Mark Sleec98d0502006-09-06 02:42:25 +0000136
Marc Slemko16698852006-08-04 03:16:10 +0000137uint32_t TBinaryProtocol::writeString(shared_ptr<TTransport> out,
Mark Slee8d7e1f62006-06-07 06:48:56 +0000138 const string& str) const {
Mark Sleef3c322b2006-06-26 23:52:22 +0000139 uint32_t result = writeI32(out, str.size());
Mark Slee8d7e1f62006-06-07 06:48:56 +0000140 out->write((uint8_t*)str.data(), str.size());
141 return result + str.size();
142}
143
144/**
145 * Reading functions
146 */
147
Marc Slemkoe6889de2006-08-12 00:32:53 +0000148uint32_t TBinaryProtocol::readMessageBegin(shared_ptr<TTransport> in,
149 std::string& name,
150 TMessageType& messageType,
Mark Sleecfc01932006-09-01 22:18:16 +0000151 int32_t& seqid) const {
Marc Slemko16698852006-08-04 03:16:10 +0000152
153 uint32_t result = 0;
Mark Sleecfc01932006-09-01 22:18:16 +0000154 int8_t type;
Marc Slemko91f67482006-08-11 23:58:57 +0000155 result+= readString(in, name);
Marc Slemko16698852006-08-04 03:16:10 +0000156 result+= readByte(in, type);
157 messageType = (TMessageType)type;
Mark Sleecfc01932006-09-01 22:18:16 +0000158 result+= readI32(in, seqid);
Marc Slemko16698852006-08-04 03:16:10 +0000159 return result;
160}
161
162uint32_t TBinaryProtocol::readMessageEnd(shared_ptr<TTransport> in) const{
163 return 0;
164}
165
166uint32_t TBinaryProtocol::readStructBegin(shared_ptr<TTransport> in,
Mark Slee8d7e1f62006-06-07 06:48:56 +0000167 string& name) const {
168 name = "";
169 return 0;
170}
171
Marc Slemko16698852006-08-04 03:16:10 +0000172uint32_t TBinaryProtocol::readStructEnd(shared_ptr<TTransport> in) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000173 return 0;
174}
175
Marc Slemko16698852006-08-04 03:16:10 +0000176uint32_t TBinaryProtocol::readFieldBegin(shared_ptr<TTransport> in,
Mark Slee8d7e1f62006-06-07 06:48:56 +0000177 string& name,
178 TType& fieldType,
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000179 int16_t& fieldId) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000180 uint32_t result = 0;
Mark Sleecfc01932006-09-01 22:18:16 +0000181 int8_t type;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000182 result += readByte(in, type);
183 fieldType = (TType)type;
184 if (fieldType == T_STOP) {
185 fieldId = 0;
186 return result;
187 }
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000188 result += readI16(in, fieldId);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000189 return result;
190}
Mark Sleee8540632006-05-30 09:24:40 +0000191
Marc Slemko16698852006-08-04 03:16:10 +0000192uint32_t TBinaryProtocol::readFieldEnd(shared_ptr<TTransport> in) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000193 return 0;
Mark Sleee8540632006-05-30 09:24:40 +0000194}
Mark Slee8d7e1f62006-06-07 06:48:56 +0000195
Marc Slemko16698852006-08-04 03:16:10 +0000196uint32_t TBinaryProtocol::readMapBegin(shared_ptr<TTransport> in,
Mark Slee8d7e1f62006-06-07 06:48:56 +0000197 TType& keyType,
198 TType& valType,
Marc Slemkob09f5882006-08-23 22:03:34 +0000199 uint32_t& size) const {
Mark Sleecfc01932006-09-01 22:18:16 +0000200 int8_t k, v;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000201 uint32_t result = 0;
Mark Sleecfc01932006-09-01 22:18:16 +0000202 int32_t sizei;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000203 result += readByte(in, k);
204 keyType = (TType)k;
205 result += readByte(in, v);
206 valType = (TType)v;
Mark Sleecfc01932006-09-01 22:18:16 +0000207 result += readI32(in, sizei);
208 // TODO(mcslee): check for negative size
209 size = (uint32_t)sizei;
Mark Sleee8540632006-05-30 09:24:40 +0000210 return result;
211}
212
Marc Slemko16698852006-08-04 03:16:10 +0000213uint32_t TBinaryProtocol::readMapEnd(shared_ptr<TTransport> in) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000214 return 0;
215}
216
Marc Slemko16698852006-08-04 03:16:10 +0000217uint32_t TBinaryProtocol::readListBegin(shared_ptr<TTransport> in,
Mark Slee8d7e1f62006-06-07 06:48:56 +0000218 TType& elemType,
Marc Slemkob09f5882006-08-23 22:03:34 +0000219 uint32_t& size) const {
Mark Sleecfc01932006-09-01 22:18:16 +0000220 int8_t e;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000221 uint32_t result = 0;
Mark Sleecfc01932006-09-01 22:18:16 +0000222 int32_t sizei;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000223 result += readByte(in, e);
224 elemType = (TType)e;
Mark Sleecfc01932006-09-01 22:18:16 +0000225 result += readI32(in, sizei);
226 // TODO(mcslee): check for negative size
227 size = (uint32_t)sizei;
Mark Sleee8540632006-05-30 09:24:40 +0000228 return result;
229}
230
Marc Slemko16698852006-08-04 03:16:10 +0000231uint32_t TBinaryProtocol::readListEnd(shared_ptr<TTransport> in) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000232 return 0;
233}
234
Marc Slemko16698852006-08-04 03:16:10 +0000235uint32_t TBinaryProtocol::readSetBegin(shared_ptr<TTransport> in,
Mark Slee8d7e1f62006-06-07 06:48:56 +0000236 TType& elemType,
Marc Slemkob09f5882006-08-23 22:03:34 +0000237 uint32_t& size) const {
Mark Sleecfc01932006-09-01 22:18:16 +0000238 int8_t e;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000239 uint32_t result = 0;
Mark Sleecfc01932006-09-01 22:18:16 +0000240 int32_t sizei;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000241 result += readByte(in, e);
242 elemType = (TType)e;
Mark Sleecfc01932006-09-01 22:18:16 +0000243 result += readI32(in, sizei);
244 // TODO(mcslee): check for negative size
245 size = (uint32_t)sizei;
Mark Sleee8540632006-05-30 09:24:40 +0000246 return result;
247}
248
Marc Slemko16698852006-08-04 03:16:10 +0000249uint32_t TBinaryProtocol::readSetEnd(shared_ptr<TTransport> in) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000250 return 0;
Mark Sleee8540632006-05-30 09:24:40 +0000251}
252
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000253uint32_t TBinaryProtocol::readBool(shared_ptr<TTransport> in,
254 bool& value) const {
255 uint8_t b[1];
256 in->readAll(b, 1);
Mark Sleecfc01932006-09-01 22:18:16 +0000257 value = *(int8_t*)b != 0;
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000258 return 1;
259}
260
Marc Slemko16698852006-08-04 03:16:10 +0000261uint32_t TBinaryProtocol::readByte(shared_ptr<TTransport> in,
Mark Sleecfc01932006-09-01 22:18:16 +0000262 int8_t& byte) const {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000263 uint8_t b[1];
264 in->readAll(b, 1);
Mark Sleecfc01932006-09-01 22:18:16 +0000265 byte = *(int8_t*)b;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000266 return 1;
Mark Sleee8540632006-05-30 09:24:40 +0000267}
268
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000269uint32_t TBinaryProtocol::readI16(shared_ptr<TTransport> in,
270 int16_t& i16) const {
271 uint8_t b[2];
272 in->readAll(b, 2);
273 i16 = *(int16_t*)b;
274 i16 = (int16_t)ntohs(i16);
275 return 2;
276}
277
Marc Slemko16698852006-08-04 03:16:10 +0000278uint32_t TBinaryProtocol::readI32(shared_ptr<TTransport> in,
Mark Slee8d7e1f62006-06-07 06:48:56 +0000279 int32_t& i32) const {
280 uint8_t b[4];
281 in->readAll(b, 4);
282 i32 = *(int32_t*)b;
283 i32 = (int32_t)ntohl(i32);
284 return 4;
Mark Sleee8540632006-05-30 09:24:40 +0000285}
286
Marc Slemko16698852006-08-04 03:16:10 +0000287uint32_t TBinaryProtocol::readI64(shared_ptr<TTransport> in,
Mark Slee8d7e1f62006-06-07 06:48:56 +0000288 int64_t& i64) const {
289 uint8_t b[8];
290 in->readAll(b, 8);
291 i64 = *(int64_t*)b;
292 i64 = (int64_t)ntohll(i64);
293 return 8;
Mark Sleee8540632006-05-30 09:24:40 +0000294}
295
Mark Sleec98d0502006-09-06 02:42:25 +0000296uint32_t TBinaryProtocol::readDouble(shared_ptr<TTransport> in,
297 double& dub) const {
298 uint8_t b[8];
299 uint8_t d[8];
300 in->readAll(b, 8);
301 d[0] = b[7];
302 d[1] = b[6];
303 d[2] = b[5];
304 d[3] = b[4];
305 d[4] = b[3];
306 d[5] = b[2];
307 d[6] = b[1];
308 d[7] = b[0];
309 dub = *(double*)d;
310 return 8;
311}
312
Marc Slemko16698852006-08-04 03:16:10 +0000313uint32_t TBinaryProtocol::readString(shared_ptr<TTransport> in,
Mark Slee8d7e1f62006-06-07 06:48:56 +0000314 string& str) const {
Mark Sleef3c322b2006-06-26 23:52:22 +0000315 uint32_t result;
Mark Sleecfc01932006-09-01 22:18:16 +0000316 int32_t size;
317 result = readI32(in, size);
318
319 // TODO(mcslee): check for negative size
Mark Slee6e536442006-06-30 18:28:50 +0000320
321 // Use the heap here to prevent stack overflow for v. large strings
322 uint8_t *b = new uint8_t[size];
Mark Slee8d7e1f62006-06-07 06:48:56 +0000323 in->readAll(b, size);
324 str = string((char*)b, size);
Mark Slee6e536442006-06-30 18:28:50 +0000325 delete [] b;
326
Mark Sleef3c322b2006-06-26 23:52:22 +0000327 return result + (uint32_t)size;
Mark Sleee8540632006-05-30 09:24:40 +0000328}
Mark Sleecfc01932006-09-01 22:18:16 +0000329
Marc Slemko6f038a72006-08-03 18:58:09 +0000330}}} // facebook::thrift::protocol