blob: 89958ec54feee586b63b637af8754b841b1a8fdf [file] [log] [blame]
Mark Slee9f0c6512007-02-28 23:58:26 +00001// 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
Mark Sleef5f2be42006-09-05 21:05:31 +00007#ifndef _THRIFT_PROTOCOL_TBINARYPROTOCOL_H_
8#define _THRIFT_PROTOCOL_TBINARYPROTOCOL_H_ 1
Mark Sleee8540632006-05-30 09:24:40 +00009
Marc Slemkod42a2c22006-08-10 03:30:18 +000010#include "TProtocol.h"
Marc Slemko16698852006-08-04 03:16:10 +000011
12#include <boost/shared_ptr.hpp>
Mark Sleee8540632006-05-30 09:24:40 +000013
Marc Slemko6f038a72006-08-03 18:58:09 +000014namespace facebook { namespace thrift { namespace protocol {
15
Mark Sleee8540632006-05-30 09:24:40 +000016/**
17 * The default binary protocol for thrift. Writes all data in a very basic
18 * binary format, essentially just spitting out the raw bytes.
19 *
20 * @author Mark Slee <mcslee@facebook.com>
21 */
Mark Sleef9831082007-02-20 20:59:21 +000022class TBinaryProtocol : public TProtocol {
Mark Slee808454e2007-06-20 21:51:57 +000023 protected:
24 static const int32_t VERSION_MASK = 0xffff0000;
25 static const int32_t VERSION_1 = 0x80010000;
David Reiss4e7530d2007-09-04 21:49:53 +000026 // VERSION_2 (0x80020000) is taken by TDenseProtocol.
Mark Slee808454e2007-06-20 21:51:57 +000027
Marc Slemko0b4ffa92006-08-11 02:49:29 +000028 public:
Mark Slee5ea15f92007-03-05 22:55:59 +000029 TBinaryProtocol(boost::shared_ptr<TTransport> trans) :
Mark Sleef9831082007-02-20 20:59:21 +000030 TProtocol(trans),
31 string_limit_(0),
32 container_limit_(0),
Mark Slee808454e2007-06-20 21:51:57 +000033 strict_read_(false),
34 strict_write_(true),
Mark Sleef9831082007-02-20 20:59:21 +000035 string_buf_(NULL),
36 string_buf_size_(0) {}
Mark Slee4af6ed72006-10-25 19:02:49 +000037
Mark Slee5ea15f92007-03-05 22:55:59 +000038 TBinaryProtocol(boost::shared_ptr<TTransport> trans,
Mark Sleef9831082007-02-20 20:59:21 +000039 int32_t string_limit,
Mark Slee808454e2007-06-20 21:51:57 +000040 int32_t container_limit,
41 bool strict_read,
42 bool strict_write) :
Mark Sleef9831082007-02-20 20:59:21 +000043 TProtocol(trans),
44 string_limit_(string_limit),
45 container_limit_(container_limit),
Mark Slee808454e2007-06-20 21:51:57 +000046 strict_read_(strict_read),
47 strict_write_(strict_write),
Mark Sleef9831082007-02-20 20:59:21 +000048 string_buf_(NULL),
49 string_buf_size_(0) {}
50
51 ~TBinaryProtocol() {
52 if (string_buf_ != NULL) {
53 free(string_buf_);
54 string_buf_size_ = 0;
55 }
56 }
57
58 void setStringSizeLimit(int32_t string_limit) {
59 string_limit_ = string_limit;
60 }
61
62 void setContainerSizeLimit(int32_t container_limit) {
63 container_limit_ = container_limit;
64 }
Mark Sleee8540632006-05-30 09:24:40 +000065
Mark Slee808454e2007-06-20 21:51:57 +000066 void setStrict(bool strict_read, bool strict_write) {
67 strict_read_ = strict_read;
68 strict_write_ = strict_write;
69 }
70
Mark Slee8d7e1f62006-06-07 06:48:56 +000071 /**
72 * Writing functions.
73 */
Mark Sleee8540632006-05-30 09:24:40 +000074
Mark Slee82a6c0f2007-04-04 21:08:21 +000075 virtual uint32_t writeMessageBegin(const std::string& name,
76 const TMessageType messageType,
77 const int32_t seqid);
Marc Slemko16698852006-08-04 03:16:10 +000078
Mark Slee4af6ed72006-10-25 19:02:49 +000079 virtual uint32_t writeMessageEnd();
Marc Slemko16698852006-08-04 03:16:10 +000080
81
Mark Slee4af6ed72006-10-25 19:02:49 +000082 uint32_t writeStructBegin(const std::string& name);
Mark Sleee8540632006-05-30 09:24:40 +000083
Mark Slee4af6ed72006-10-25 19:02:49 +000084 uint32_t writeStructEnd();
Mark Sleee8540632006-05-30 09:24:40 +000085
Mark Slee4af6ed72006-10-25 19:02:49 +000086 uint32_t writeFieldBegin(const std::string& name,
87 const TType fieldType,
88 const int16_t fieldId);
Mark Slee8d7e1f62006-06-07 06:48:56 +000089
Mark Slee4af6ed72006-10-25 19:02:49 +000090 uint32_t writeFieldEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +000091
Mark Slee4af6ed72006-10-25 19:02:49 +000092 uint32_t writeFieldStop();
Mark Slee8d7e1f62006-06-07 06:48:56 +000093
Mark Slee4af6ed72006-10-25 19:02:49 +000094 uint32_t writeMapBegin(const TType keyType,
95 const TType valType,
96 const uint32_t size);
Mark Slee8d7e1f62006-06-07 06:48:56 +000097
Mark Slee4af6ed72006-10-25 19:02:49 +000098 uint32_t writeMapEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +000099
Mark Slee4af6ed72006-10-25 19:02:49 +0000100 uint32_t writeListBegin(const TType elemType,
101 const uint32_t size);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000102
Mark Slee4af6ed72006-10-25 19:02:49 +0000103 uint32_t writeListEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000104
Mark Slee4af6ed72006-10-25 19:02:49 +0000105 uint32_t writeSetBegin(const TType elemType,
106 const uint32_t size);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000107
Mark Slee4af6ed72006-10-25 19:02:49 +0000108 uint32_t writeSetEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000109
Mark Slee4af6ed72006-10-25 19:02:49 +0000110 uint32_t writeBool(const bool value);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000111
Mark Slee4af6ed72006-10-25 19:02:49 +0000112 uint32_t writeByte(const int8_t byte);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000113
Mark Slee4af6ed72006-10-25 19:02:49 +0000114 uint32_t writeI16(const int16_t i16);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000115
Mark Slee4af6ed72006-10-25 19:02:49 +0000116 uint32_t writeI32(const int32_t i32);
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000117
Mark Slee4af6ed72006-10-25 19:02:49 +0000118 uint32_t writeI64(const int64_t i64);
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000119
Mark Slee4af6ed72006-10-25 19:02:49 +0000120 uint32_t writeDouble(const double dub);
Mark Sleec98d0502006-09-06 02:42:25 +0000121
122
Mark Slee4af6ed72006-10-25 19:02:49 +0000123 uint32_t writeString(const std::string& str);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000124
125 /**
126 * Reading functions
127 */
128
Marc Slemko16698852006-08-04 03:16:10 +0000129
Mark Slee4af6ed72006-10-25 19:02:49 +0000130 uint32_t readMessageBegin(std::string& name,
David Reiss96d23882007-07-26 21:10:32 +0000131 TMessageType& messageType,
132 int32_t& seqid);
Marc Slemko16698852006-08-04 03:16:10 +0000133
Mark Slee4af6ed72006-10-25 19:02:49 +0000134 uint32_t readMessageEnd();
Marc Slemko16698852006-08-04 03:16:10 +0000135
Mark Slee4af6ed72006-10-25 19:02:49 +0000136 uint32_t readStructBegin(std::string& name);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000137
Mark Slee4af6ed72006-10-25 19:02:49 +0000138 uint32_t readStructEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000139
Mark Slee4af6ed72006-10-25 19:02:49 +0000140 uint32_t readFieldBegin(std::string& name,
David Reiss96d23882007-07-26 21:10:32 +0000141 TType& fieldType,
142 int16_t& fieldId);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000143
Mark Slee4af6ed72006-10-25 19:02:49 +0000144 uint32_t readFieldEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000145
Mark Slee4af6ed72006-10-25 19:02:49 +0000146 uint32_t readMapBegin(TType& keyType,
David Reiss96d23882007-07-26 21:10:32 +0000147 TType& valType,
148 uint32_t& size);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000149
Mark Slee4af6ed72006-10-25 19:02:49 +0000150 uint32_t readMapEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000151
Mark Slee4af6ed72006-10-25 19:02:49 +0000152 uint32_t readListBegin(TType& elemType,
153 uint32_t& size);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000154
Mark Slee4af6ed72006-10-25 19:02:49 +0000155 uint32_t readListEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000156
Mark Slee4af6ed72006-10-25 19:02:49 +0000157 uint32_t readSetBegin(TType& elemType,
David Reiss96d23882007-07-26 21:10:32 +0000158 uint32_t& size);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000159
Mark Slee4af6ed72006-10-25 19:02:49 +0000160 uint32_t readSetEnd();
Mark Slee8d7e1f62006-06-07 06:48:56 +0000161
Mark Slee4af6ed72006-10-25 19:02:49 +0000162 uint32_t readBool(bool& value);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000163
Mark Slee4af6ed72006-10-25 19:02:49 +0000164 uint32_t readByte(int8_t& byte);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000165
Mark Slee4af6ed72006-10-25 19:02:49 +0000166 uint32_t readI16(int16_t& i16);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000167
Mark Slee4af6ed72006-10-25 19:02:49 +0000168 uint32_t readI32(int32_t& i32);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000169
Mark Slee4af6ed72006-10-25 19:02:49 +0000170 uint32_t readI64(int64_t& i64);
Marc Slemko0b4ffa92006-08-11 02:49:29 +0000171
Mark Slee4af6ed72006-10-25 19:02:49 +0000172 uint32_t readDouble(double& dub);
Mark Sleec98d0502006-09-06 02:42:25 +0000173
Mark Slee4af6ed72006-10-25 19:02:49 +0000174 uint32_t readString(std::string& str);
Mark Sleef9831082007-02-20 20:59:21 +0000175
Mark Slee808454e2007-06-20 21:51:57 +0000176 protected:
177 uint32_t readStringBody(std::string& str, int32_t sz);
178
Mark Sleef9831082007-02-20 20:59:21 +0000179 int32_t string_limit_;
180 int32_t container_limit_;
181
Mark Slee808454e2007-06-20 21:51:57 +0000182 // Enforce presence of version identifier
183 bool strict_read_;
184 bool strict_write_;
185
Mark Sleef9831082007-02-20 20:59:21 +0000186 // Buffer for reading strings, save for the lifetime of the protocol to
187 // avoid memory churn allocating memory on every string read
188 uint8_t* string_buf_;
189 int32_t string_buf_size_;
190
Mark Slee4af6ed72006-10-25 19:02:49 +0000191};
192
193/**
194 * Constructs binary protocol handlers
195 */
196class TBinaryProtocolFactory : public TProtocolFactory {
197 public:
Mark Sleef9831082007-02-20 20:59:21 +0000198 TBinaryProtocolFactory() :
199 string_limit_(0),
Mark Slee808454e2007-06-20 21:51:57 +0000200 container_limit_(0),
201 strict_read_(false),
202 strict_write_(true) {}
Mark Sleef9831082007-02-20 20:59:21 +0000203
Mark Slee808454e2007-06-20 21:51:57 +0000204 TBinaryProtocolFactory(int32_t string_limit, int32_t container_limit, bool strict_read, bool strict_write) :
Mark Sleef9831082007-02-20 20:59:21 +0000205 string_limit_(string_limit),
Mark Slee808454e2007-06-20 21:51:57 +0000206 container_limit_(container_limit),
207 strict_read_(strict_read),
208 strict_write_(strict_write) {}
Mark Slee4af6ed72006-10-25 19:02:49 +0000209
210 virtual ~TBinaryProtocolFactory() {}
211
Mark Sleef9831082007-02-20 20:59:21 +0000212 void setStringSizeLimit(int32_t string_limit) {
213 string_limit_ = string_limit;
Mark Slee4af6ed72006-10-25 19:02:49 +0000214 }
Mark Sleef9831082007-02-20 20:59:21 +0000215
216 void setContainerSizeLimit(int32_t container_limit) {
217 container_limit_ = container_limit;
218 }
219
Mark Slee808454e2007-06-20 21:51:57 +0000220 void setStrict(bool strict_read, bool strict_write) {
221 strict_read_ = strict_read;
222 strict_write_ = strict_write;
223 }
224
Mark Sleef9831082007-02-20 20:59:21 +0000225 boost::shared_ptr<TProtocol> getProtocol(boost::shared_ptr<TTransport> trans) {
Mark Slee808454e2007-06-20 21:51:57 +0000226 return boost::shared_ptr<TProtocol>(new TBinaryProtocol(trans, string_limit_, container_limit_, strict_read_, strict_write_));
Mark Sleef9831082007-02-20 20:59:21 +0000227 }
228
229 private:
230 int32_t string_limit_;
231 int32_t container_limit_;
Mark Slee808454e2007-06-20 21:51:57 +0000232 bool strict_read_;
233 bool strict_write_;
Mark Sleef9831082007-02-20 20:59:21 +0000234
Mark Sleee8540632006-05-30 09:24:40 +0000235};
236
Marc Slemko6f038a72006-08-03 18:58:09 +0000237}}} // facebook::thrift::protocol
238
Mark Sleef5f2be42006-09-05 21:05:31 +0000239#endif // #ifndef _THRIFT_PROTOCOL_TBINARYPROTOCOL_H_