blob: 792a2d89e3b7904f99df83733d52e174b520a933 [file] [log] [blame]
David Reisse4d4ea02009-04-02 21:37:17 +00001/*
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 */
19
20#ifndef _THRIFT_PROTOCOL_TCOMPACTPROTOCOL_H_
21#define _THRIFT_PROTOCOL_TCOMPACTPROTOCOL_H_ 1
22
Roger Meier4285ba22013-06-10 21:17:23 +020023#include <thrift/protocol/TVirtualProtocol.h>
David Reisse4d4ea02009-04-02 21:37:17 +000024
25#include <stack>
cyy316723a2019-01-05 16:35:14 +080026#include <memory>
David Reisse4d4ea02009-04-02 21:37:17 +000027
Konrad Grochowski16a23a62014-11-13 15:33:38 +010028namespace apache {
29namespace thrift {
30namespace protocol {
David Reisse4d4ea02009-04-02 21:37:17 +000031
32/**
33 * C++ Implementation of the Compact Protocol as described in THRIFT-110
34 */
David Reisse71115b2010-10-06 17:09:56 +000035template <class Transport_>
Konrad Grochowski16a23a62014-11-13 15:33:38 +010036class TCompactProtocolT : public TVirtualProtocol<TCompactProtocolT<Transport_> > {
Nobuaki Sukegawa95c628e2016-01-24 01:03:28 +090037public:
Konrad Grochowski16a23a62014-11-13 15:33:38 +010038 static const int8_t PROTOCOL_ID = (int8_t)0x82u;
39 static const int8_t VERSION_N = 1;
40 static const int8_t VERSION_MASK = 0x1f; // 0001 1111
Nobuaki Sukegawa95c628e2016-01-24 01:03:28 +090041
42protected:
Konrad Grochowski16a23a62014-11-13 15:33:38 +010043 static const int8_t TYPE_MASK = (int8_t)0xE0u; // 1110 0000
44 static const int8_t TYPE_BITS = 0x07; // 0000 0111
David Reisse4d4ea02009-04-02 21:37:17 +000045 static const int32_t TYPE_SHIFT_AMOUNT = 5;
46
David Reisse71115b2010-10-06 17:09:56 +000047 Transport_* trans_;
48
David Reisse4d4ea02009-04-02 21:37:17 +000049 /**
50 * (Writing) If we encounter a boolean field begin, save the TField here
51 * so it can have the value incorporated.
52 */
53 struct {
54 const char* name;
55 TType fieldType;
56 int16_t fieldId;
57 } booleanField_;
58
59 /**
60 * (Reading) If we read a field header, and it's a boolean field, save
61 * the boolean value here so that readBool can use it.
62 */
63 struct {
64 bool hasBoolValue;
65 bool boolValue;
66 } boolValue_;
67
68 /**
69 * Used to keep track of the last field for the current and previous structs,
70 * so we can do the delta stuff.
71 */
72
73 std::stack<int16_t> lastField_;
74 int16_t lastFieldId_;
75
Konrad Grochowski16a23a62014-11-13 15:33:38 +010076public:
cyy316723a2019-01-05 16:35:14 +080077 TCompactProtocolT(std::shared_ptr<Transport_> trans)
Konrad Grochowski16a23a62014-11-13 15:33:38 +010078 : TVirtualProtocol<TCompactProtocolT<Transport_> >(trans),
79 trans_(trans.get()),
80 lastFieldId_(0),
81 string_limit_(0),
Sebastian Zenker042580f2019-01-29 15:48:12 +010082 string_buf_(nullptr),
Konrad Grochowski16a23a62014-11-13 15:33:38 +010083 string_buf_size_(0),
84 container_limit_(0) {
Sebastian Zenker042580f2019-01-29 15:48:12 +010085 booleanField_.name = nullptr;
David Reisse4d4ea02009-04-02 21:37:17 +000086 boolValue_.hasBoolValue = false;
87 }
88
cyy316723a2019-01-05 16:35:14 +080089 TCompactProtocolT(std::shared_ptr<Transport_> trans,
David Reisse71115b2010-10-06 17:09:56 +000090 int32_t string_limit,
Konrad Grochowski16a23a62014-11-13 15:33:38 +010091 int32_t container_limit)
92 : TVirtualProtocol<TCompactProtocolT<Transport_> >(trans),
93 trans_(trans.get()),
94 lastFieldId_(0),
95 string_limit_(string_limit),
Sebastian Zenker042580f2019-01-29 15:48:12 +010096 string_buf_(nullptr),
Konrad Grochowski16a23a62014-11-13 15:33:38 +010097 string_buf_size_(0),
98 container_limit_(container_limit) {
Sebastian Zenker042580f2019-01-29 15:48:12 +010099 booleanField_.name = nullptr;
David Reisse4d4ea02009-04-02 21:37:17 +0000100 boolValue_.hasBoolValue = false;
101 }
102
Sebastian Zenker042580f2019-01-29 15:48:12 +0100103 ~TCompactProtocolT() override { free(string_buf_); }
David Reisse4d4ea02009-04-02 21:37:17 +0000104
105 /**
106 * Writing functions
107 */
108
109 virtual uint32_t writeMessageBegin(const std::string& name,
110 const TMessageType messageType,
111 const int32_t seqid);
112
113 uint32_t writeStructBegin(const char* name);
114
115 uint32_t writeStructEnd();
116
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100117 uint32_t writeFieldBegin(const char* name, const TType fieldType, const int16_t fieldId);
David Reisse4d4ea02009-04-02 21:37:17 +0000118
119 uint32_t writeFieldStop();
120
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100121 uint32_t writeListBegin(const TType elemType, const uint32_t size);
David Reisse4d4ea02009-04-02 21:37:17 +0000122
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100123 uint32_t writeSetBegin(const TType elemType, const uint32_t size);
David Reisse4d4ea02009-04-02 21:37:17 +0000124
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100125 virtual uint32_t writeMapBegin(const TType keyType, const TType valType, const uint32_t size);
David Reisse4d4ea02009-04-02 21:37:17 +0000126
127 uint32_t writeBool(const bool value);
128
129 uint32_t writeByte(const int8_t byte);
130
131 uint32_t writeI16(const int16_t i16);
132
133 uint32_t writeI32(const int32_t i32);
134
135 uint32_t writeI64(const int64_t i64);
136
137 uint32_t writeDouble(const double dub);
138
139 uint32_t writeString(const std::string& str);
140
141 uint32_t writeBinary(const std::string& str);
142
Christopher Friedtcea55592022-10-01 09:01:45 -0400143 int getMinSerializedSize(TType type) override;
zeshuai00786352b42020-06-15 17:00:33 +0800144
Christopher Friedtcea55592022-10-01 09:01:45 -0400145 void checkReadBytesAvailable(TSet& set) override
zeshuai00786352b42020-06-15 17:00:33 +0800146 {
147 trans_->checkReadBytesAvailable(set.size_ * getMinSerializedSize(set.elemType_));
148 }
149
Christopher Friedtcea55592022-10-01 09:01:45 -0400150 void checkReadBytesAvailable(TList& list) override
zeshuai00786352b42020-06-15 17:00:33 +0800151 {
152 trans_->checkReadBytesAvailable(list.size_ * getMinSerializedSize(list.elemType_));
153 }
154
Christopher Friedtcea55592022-10-01 09:01:45 -0400155 void checkReadBytesAvailable(TMap& map) override
zeshuai00786352b42020-06-15 17:00:33 +0800156 {
157 int elmSize = getMinSerializedSize(map.keyType_) + getMinSerializedSize(map.valueType_);
158 trans_->checkReadBytesAvailable(map.size_ * elmSize);
159 }
160
David Reisse4d4ea02009-04-02 21:37:17 +0000161 /**
162 * These methods are called by structs, but don't actually have any wired
163 * output or purpose
164 */
165 virtual uint32_t writeMessageEnd() { return 0; }
166 uint32_t writeMapEnd() { return 0; }
167 uint32_t writeListEnd() { return 0; }
168 uint32_t writeSetEnd() { return 0; }
169 uint32_t writeFieldEnd() { return 0; }
170
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100171protected:
David Reisse4d4ea02009-04-02 21:37:17 +0000172 int32_t writeFieldBeginInternal(const char* name,
173 const TType fieldType,
174 const int16_t fieldId,
175 int8_t typeOverride);
Roger Meier64a799d2013-06-04 20:59:01 +0200176 uint32_t writeCollectionBegin(const TType elemType, int32_t size);
David Reisse4d4ea02009-04-02 21:37:17 +0000177 uint32_t writeVarint32(uint32_t n);
178 uint32_t writeVarint64(uint64_t n);
179 uint64_t i64ToZigzag(const int64_t l);
180 uint32_t i32ToZigzag(const int32_t n);
Roger Meier64a799d2013-06-04 20:59:01 +0200181 inline int8_t getCompactType(const TType ttype);
David Reisse4d4ea02009-04-02 21:37:17 +0000182
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100183public:
184 uint32_t readMessageBegin(std::string& name, TMessageType& messageType, int32_t& seqid);
David Reisse4d4ea02009-04-02 21:37:17 +0000185
186 uint32_t readStructBegin(std::string& name);
187
188 uint32_t readStructEnd();
189
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100190 uint32_t readFieldBegin(std::string& name, TType& fieldType, int16_t& fieldId);
David Reisse4d4ea02009-04-02 21:37:17 +0000191
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100192 uint32_t readMapBegin(TType& keyType, TType& valType, uint32_t& size);
David Reisse4d4ea02009-04-02 21:37:17 +0000193
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100194 uint32_t readListBegin(TType& elemType, uint32_t& size);
David Reisse4d4ea02009-04-02 21:37:17 +0000195
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100196 uint32_t readSetBegin(TType& elemType, uint32_t& size);
David Reisse4d4ea02009-04-02 21:37:17 +0000197
198 uint32_t readBool(bool& value);
David Reiss8dfc7322010-10-06 17:09:58 +0000199 // Provide the default readBool() implementation for std::vector<bool>
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100200 using TVirtualProtocol<TCompactProtocolT<Transport_> >::readBool;
David Reisse4d4ea02009-04-02 21:37:17 +0000201
202 uint32_t readByte(int8_t& byte);
203
204 uint32_t readI16(int16_t& i16);
205
206 uint32_t readI32(int32_t& i32);
207
208 uint32_t readI64(int64_t& i64);
209
210 uint32_t readDouble(double& dub);
211
212 uint32_t readString(std::string& str);
213
214 uint32_t readBinary(std::string& str);
215
216 /*
217 *These methods are here for the struct to call, but don't have any wire
218 * encoding.
219 */
220 uint32_t readMessageEnd() { return 0; }
221 uint32_t readFieldEnd() { return 0; }
222 uint32_t readMapEnd() { return 0; }
223 uint32_t readListEnd() { return 0; }
224 uint32_t readSetEnd() { return 0; }
225
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100226protected:
David Reisse4d4ea02009-04-02 21:37:17 +0000227 uint32_t readVarint32(int32_t& i32);
228 uint32_t readVarint64(int64_t& i64);
229 int32_t zigzagToI32(uint32_t n);
230 int64_t zigzagToI64(uint64_t n);
231 TType getTType(int8_t type);
232
233 // Buffer for reading strings, save for the lifetime of the protocol to
234 // avoid memory churn allocating memory on every string read
235 int32_t string_limit_;
236 uint8_t* string_buf_;
237 int32_t string_buf_size_;
238 int32_t container_limit_;
239};
240
David Reisse71115b2010-10-06 17:09:56 +0000241typedef TCompactProtocolT<TTransport> TCompactProtocol;
242
David Reisse4d4ea02009-04-02 21:37:17 +0000243/**
244 * Constructs compact protocol handlers
245 */
David Reisse71115b2010-10-06 17:09:56 +0000246template <class Transport_>
247class TCompactProtocolFactoryT : public TProtocolFactory {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100248public:
249 TCompactProtocolFactoryT() : string_limit_(0), container_limit_(0) {}
David Reisse4d4ea02009-04-02 21:37:17 +0000250
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100251 TCompactProtocolFactoryT(int32_t string_limit, int32_t container_limit)
252 : string_limit_(string_limit), container_limit_(container_limit) {}
David Reisse4d4ea02009-04-02 21:37:17 +0000253
Sebastian Zenker042580f2019-01-29 15:48:12 +0100254 ~TCompactProtocolFactoryT() override = default;
David Reisse4d4ea02009-04-02 21:37:17 +0000255
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100256 void setStringSizeLimit(int32_t string_limit) { string_limit_ = string_limit; }
David Reisse4d4ea02009-04-02 21:37:17 +0000257
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100258 void setContainerSizeLimit(int32_t container_limit) { container_limit_ = container_limit; }
David Reisse4d4ea02009-04-02 21:37:17 +0000259
Sebastian Zenker042580f2019-01-29 15:48:12 +0100260 std::shared_ptr<TProtocol> getProtocol(std::shared_ptr<TTransport> trans) override {
cyy316723a2019-01-05 16:35:14 +0800261 std::shared_ptr<Transport_> specific_trans = std::dynamic_pointer_cast<Transport_>(trans);
David Reisse71115b2010-10-06 17:09:56 +0000262 TProtocol* prot;
263 if (specific_trans) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100264 prot = new TCompactProtocolT<Transport_>(specific_trans, string_limit_, container_limit_);
David Reisse71115b2010-10-06 17:09:56 +0000265 } else {
266 prot = new TCompactProtocol(trans, string_limit_, container_limit_);
267 }
268
cyy316723a2019-01-05 16:35:14 +0800269 return std::shared_ptr<TProtocol>(prot);
David Reisse4d4ea02009-04-02 21:37:17 +0000270 }
271
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100272private:
David Reisse4d4ea02009-04-02 21:37:17 +0000273 int32_t string_limit_;
274 int32_t container_limit_;
David Reisse4d4ea02009-04-02 21:37:17 +0000275};
276
David Reisse71115b2010-10-06 17:09:56 +0000277typedef TCompactProtocolFactoryT<TTransport> TCompactProtocolFactory;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100278}
279}
280} // apache::thrift::protocol
David Reisse4d4ea02009-04-02 21:37:17 +0000281
Roger Meier4285ba22013-06-10 21:17:23 +0200282#include <thrift/protocol/TCompactProtocol.tcc>
David Reisse71115b2010-10-06 17:09:56 +0000283
David Reisse4d4ea02009-04-02 21:37:17 +0000284#endif