David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +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 | */ |
| 19 | |
| 20 | #ifndef _THRIFT_PROTOCOL_TCOMPACTPROTOCOL_H_ |
| 21 | #define _THRIFT_PROTOCOL_TCOMPACTPROTOCOL_H_ 1 |
| 22 | |
Roger Meier | 4285ba2 | 2013-06-10 21:17:23 +0200 | [diff] [blame] | 23 | #include <thrift/protocol/TVirtualProtocol.h> |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 24 | |
| 25 | #include <stack> |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 26 | #include <memory> |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 27 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 28 | namespace apache { |
| 29 | namespace thrift { |
| 30 | namespace protocol { |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 31 | |
| 32 | /** |
| 33 | * C++ Implementation of the Compact Protocol as described in THRIFT-110 |
| 34 | */ |
David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 35 | template <class Transport_> |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 36 | class TCompactProtocolT : public TVirtualProtocol<TCompactProtocolT<Transport_> > { |
Nobuaki Sukegawa | 95c628e | 2016-01-24 01:03:28 +0900 | [diff] [blame] | 37 | public: |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 38 | 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 Sukegawa | 95c628e | 2016-01-24 01:03:28 +0900 | [diff] [blame] | 41 | |
| 42 | protected: |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 43 | static const int8_t TYPE_MASK = (int8_t)0xE0u; // 1110 0000 |
| 44 | static const int8_t TYPE_BITS = 0x07; // 0000 0111 |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 45 | static const int32_t TYPE_SHIFT_AMOUNT = 5; |
| 46 | |
David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 47 | Transport_* trans_; |
| 48 | |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 49 | /** |
| 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 Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 76 | public: |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 77 | TCompactProtocolT(std::shared_ptr<Transport_> trans) |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 78 | : TVirtualProtocol<TCompactProtocolT<Transport_> >(trans), |
| 79 | trans_(trans.get()), |
| 80 | lastFieldId_(0), |
| 81 | string_limit_(0), |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 82 | string_buf_(nullptr), |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 83 | string_buf_size_(0), |
| 84 | container_limit_(0) { |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 85 | booleanField_.name = nullptr; |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 86 | boolValue_.hasBoolValue = false; |
| 87 | } |
| 88 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 89 | TCompactProtocolT(std::shared_ptr<Transport_> trans, |
David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 90 | int32_t string_limit, |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 91 | int32_t container_limit) |
| 92 | : TVirtualProtocol<TCompactProtocolT<Transport_> >(trans), |
| 93 | trans_(trans.get()), |
| 94 | lastFieldId_(0), |
| 95 | string_limit_(string_limit), |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 96 | string_buf_(nullptr), |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 97 | string_buf_size_(0), |
| 98 | container_limit_(container_limit) { |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 99 | booleanField_.name = nullptr; |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 100 | boolValue_.hasBoolValue = false; |
| 101 | } |
| 102 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 103 | ~TCompactProtocolT() override { free(string_buf_); } |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 104 | |
| 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 Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 117 | uint32_t writeFieldBegin(const char* name, const TType fieldType, const int16_t fieldId); |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 118 | |
| 119 | uint32_t writeFieldStop(); |
| 120 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 121 | uint32_t writeListBegin(const TType elemType, const uint32_t size); |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 122 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 123 | uint32_t writeSetBegin(const TType elemType, const uint32_t size); |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 124 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 125 | virtual uint32_t writeMapBegin(const TType keyType, const TType valType, const uint32_t size); |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 126 | |
| 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 Friedt | cea5559 | 2022-10-01 09:01:45 -0400 | [diff] [blame] | 143 | int getMinSerializedSize(TType type) override; |
zeshuai007 | 86352b4 | 2020-06-15 17:00:33 +0800 | [diff] [blame] | 144 | |
Christopher Friedt | cea5559 | 2022-10-01 09:01:45 -0400 | [diff] [blame] | 145 | void checkReadBytesAvailable(TSet& set) override |
zeshuai007 | 86352b4 | 2020-06-15 17:00:33 +0800 | [diff] [blame] | 146 | { |
| 147 | trans_->checkReadBytesAvailable(set.size_ * getMinSerializedSize(set.elemType_)); |
| 148 | } |
| 149 | |
Christopher Friedt | cea5559 | 2022-10-01 09:01:45 -0400 | [diff] [blame] | 150 | void checkReadBytesAvailable(TList& list) override |
zeshuai007 | 86352b4 | 2020-06-15 17:00:33 +0800 | [diff] [blame] | 151 | { |
| 152 | trans_->checkReadBytesAvailable(list.size_ * getMinSerializedSize(list.elemType_)); |
| 153 | } |
| 154 | |
Christopher Friedt | cea5559 | 2022-10-01 09:01:45 -0400 | [diff] [blame] | 155 | void checkReadBytesAvailable(TMap& map) override |
zeshuai007 | 86352b4 | 2020-06-15 17:00:33 +0800 | [diff] [blame] | 156 | { |
| 157 | int elmSize = getMinSerializedSize(map.keyType_) + getMinSerializedSize(map.valueType_); |
| 158 | trans_->checkReadBytesAvailable(map.size_ * elmSize); |
| 159 | } |
| 160 | |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 161 | /** |
| 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 Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 171 | protected: |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 172 | int32_t writeFieldBeginInternal(const char* name, |
| 173 | const TType fieldType, |
| 174 | const int16_t fieldId, |
| 175 | int8_t typeOverride); |
Roger Meier | 64a799d | 2013-06-04 20:59:01 +0200 | [diff] [blame] | 176 | uint32_t writeCollectionBegin(const TType elemType, int32_t size); |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 177 | 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 Meier | 64a799d | 2013-06-04 20:59:01 +0200 | [diff] [blame] | 181 | inline int8_t getCompactType(const TType ttype); |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 182 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 183 | public: |
| 184 | uint32_t readMessageBegin(std::string& name, TMessageType& messageType, int32_t& seqid); |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 185 | |
| 186 | uint32_t readStructBegin(std::string& name); |
| 187 | |
| 188 | uint32_t readStructEnd(); |
| 189 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 190 | uint32_t readFieldBegin(std::string& name, TType& fieldType, int16_t& fieldId); |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 191 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 192 | uint32_t readMapBegin(TType& keyType, TType& valType, uint32_t& size); |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 193 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 194 | uint32_t readListBegin(TType& elemType, uint32_t& size); |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 195 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 196 | uint32_t readSetBegin(TType& elemType, uint32_t& size); |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 197 | |
| 198 | uint32_t readBool(bool& value); |
David Reiss | 8dfc732 | 2010-10-06 17:09:58 +0000 | [diff] [blame] | 199 | // Provide the default readBool() implementation for std::vector<bool> |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 200 | using TVirtualProtocol<TCompactProtocolT<Transport_> >::readBool; |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 201 | |
| 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 Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 226 | protected: |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 227 | 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 Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 241 | typedef TCompactProtocolT<TTransport> TCompactProtocol; |
| 242 | |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 243 | /** |
| 244 | * Constructs compact protocol handlers |
| 245 | */ |
David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 246 | template <class Transport_> |
| 247 | class TCompactProtocolFactoryT : public TProtocolFactory { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 248 | public: |
| 249 | TCompactProtocolFactoryT() : string_limit_(0), container_limit_(0) {} |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 250 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 251 | TCompactProtocolFactoryT(int32_t string_limit, int32_t container_limit) |
| 252 | : string_limit_(string_limit), container_limit_(container_limit) {} |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 253 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 254 | ~TCompactProtocolFactoryT() override = default; |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 255 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 256 | void setStringSizeLimit(int32_t string_limit) { string_limit_ = string_limit; } |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 257 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 258 | void setContainerSizeLimit(int32_t container_limit) { container_limit_ = container_limit; } |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 259 | |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 260 | std::shared_ptr<TProtocol> getProtocol(std::shared_ptr<TTransport> trans) override { |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 261 | std::shared_ptr<Transport_> specific_trans = std::dynamic_pointer_cast<Transport_>(trans); |
David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 262 | TProtocol* prot; |
| 263 | if (specific_trans) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 264 | prot = new TCompactProtocolT<Transport_>(specific_trans, string_limit_, container_limit_); |
David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 265 | } else { |
| 266 | prot = new TCompactProtocol(trans, string_limit_, container_limit_); |
| 267 | } |
| 268 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 269 | return std::shared_ptr<TProtocol>(prot); |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 270 | } |
| 271 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 272 | private: |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 273 | int32_t string_limit_; |
| 274 | int32_t container_limit_; |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 275 | }; |
| 276 | |
David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 277 | typedef TCompactProtocolFactoryT<TTransport> TCompactProtocolFactory; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | } // apache::thrift::protocol |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 281 | |
Roger Meier | 4285ba2 | 2013-06-10 21:17:23 +0200 | [diff] [blame] | 282 | #include <thrift/protocol/TCompactProtocol.tcc> |
David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 283 | |
David Reiss | e4d4ea0 | 2009-04-02 21:37:17 +0000 | [diff] [blame] | 284 | #endif |