blob: ae89f2a655884544d5aa64557511a9d050e7a557 [file] [log] [blame]
Nobuaki Sukegawa6525f6a2016-02-11 13:58:39 +09001/*
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
Ali-Akber Saifeeb7247872021-12-11 08:39:24 -080020#define PY_SSIZE_T_CLEAN
Nobuaki Sukegawa6525f6a2016-02-11 13:58:39 +090021#include "ext/compact.h"
22
23namespace apache {
24namespace thrift {
25namespace py {
26
27const uint8_t CompactProtocol::TTypeToCType[] = {
28 CT_STOP, // T_STOP
29 0, // unused
30 CT_BOOLEAN_TRUE, // T_BOOL
31 CT_BYTE, // T_BYTE
32 CT_DOUBLE, // T_DOUBLE
33 0, // unused
34 CT_I16, // T_I16
35 0, // unused
36 CT_I32, // T_I32
37 0, // unused
38 CT_I64, // T_I64
39 CT_BINARY, // T_STRING
40 CT_STRUCT, // T_STRUCT
41 CT_MAP, // T_MAP
42 CT_SET, // T_SET
43 CT_LIST, // T_LIST
44};
45
46bool CompactProtocol::readFieldBegin(TType& type, int16_t& tag) {
47 uint8_t b;
48 if (!readByte(b)) {
49 return false;
50 }
51 uint8_t ctype = b & 0xf;
52 type = getTType(ctype);
53 if (type == -1) {
54 return false;
55 } else if (type == T_STOP) {
56 tag = 0;
57 return true;
58 }
59 uint8_t diff = (b & 0xf0) >> 4;
60 if (diff) {
61 tag = readTags_.top() + diff;
62 } else if (!readI16(tag)) {
63 readTags_.top() = -1;
64 return false;
65 }
66 if (ctype == CT_BOOLEAN_FALSE || ctype == CT_BOOLEAN_TRUE) {
67 readBool_.exists = true;
68 readBool_.value = ctype == CT_BOOLEAN_TRUE;
69 }
70 readTags_.top() = tag;
71 return true;
72}
73
74TType CompactProtocol::getTType(uint8_t type) {
75 switch (type) {
76 case T_STOP:
77 return T_STOP;
78 case CT_BOOLEAN_FALSE:
79 case CT_BOOLEAN_TRUE:
80 return T_BOOL;
81 case CT_BYTE:
82 return T_BYTE;
83 case CT_I16:
84 return T_I16;
85 case CT_I32:
86 return T_I32;
87 case CT_I64:
88 return T_I64;
89 case CT_DOUBLE:
90 return T_DOUBLE;
91 case CT_BINARY:
92 return T_STRING;
93 case CT_LIST:
94 return T_LIST;
95 case CT_SET:
96 return T_SET;
97 case CT_MAP:
98 return T_MAP;
99 case CT_STRUCT:
100 return T_STRUCT;
101 default:
102 PyErr_Format(PyExc_TypeError, "don't know what type: %d", type);
103 return static_cast<TType>(-1);
104 }
105}
106}
107}
108}