blob: 8d13d3d196fb15b5a9a084c32d6cb20101494ded [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
Carel Combrinka715bdf2025-10-30 07:44:21 +010027/** Mapping of Compact type to Thrift Type according.
28 * This list must match the TType enum in TEnum.h */
Nobuaki Sukegawa6525f6a2016-02-11 13:58:39 +090029const uint8_t CompactProtocol::TTypeToCType[] = {
Carel Combrinka715bdf2025-10-30 07:44:21 +010030/* 0 */ CT_STOP, // T_STOP
31/* 1 */ 0, // unused
32/* 2 */ CT_BOOLEAN_TRUE, // T_BOOL
33/* 3 */ CT_BYTE, // T_BYTE
34/* 4 */ CT_DOUBLE, // T_DOUBLE
35/* 5 */ 0, // unused
36/* 6 */ CT_I16, // T_I16
37/* 7 */ 0, // unused
38/* 8 */ CT_I32, // T_I32
39/* 9 */ 0, // unused
40/* 10 */ CT_I64, // T_I64
41/* 11 */ CT_BINARY, // T_STRING
42/* 12 */ CT_STRUCT, // T_STRUCT
43/* 13 */ CT_MAP, // T_MAP
44/* 14 */ CT_SET, // T_SET
45/* 15 */ CT_LIST, // T_LIST
46/* 16 */ CT_UUID, // T_UUID
Nobuaki Sukegawa6525f6a2016-02-11 13:58:39 +090047};
48
49bool CompactProtocol::readFieldBegin(TType& type, int16_t& tag) {
50 uint8_t b;
51 if (!readByte(b)) {
52 return false;
53 }
54 uint8_t ctype = b & 0xf;
55 type = getTType(ctype);
56 if (type == -1) {
57 return false;
58 } else if (type == T_STOP) {
59 tag = 0;
60 return true;
61 }
62 uint8_t diff = (b & 0xf0) >> 4;
63 if (diff) {
64 tag = readTags_.top() + diff;
65 } else if (!readI16(tag)) {
66 readTags_.top() = -1;
67 return false;
68 }
69 if (ctype == CT_BOOLEAN_FALSE || ctype == CT_BOOLEAN_TRUE) {
70 readBool_.exists = true;
71 readBool_.value = ctype == CT_BOOLEAN_TRUE;
72 }
73 readTags_.top() = tag;
74 return true;
75}
76
77TType CompactProtocol::getTType(uint8_t type) {
78 switch (type) {
79 case T_STOP:
80 return T_STOP;
81 case CT_BOOLEAN_FALSE:
82 case CT_BOOLEAN_TRUE:
83 return T_BOOL;
84 case CT_BYTE:
85 return T_BYTE;
86 case CT_I16:
87 return T_I16;
88 case CT_I32:
89 return T_I32;
90 case CT_I64:
91 return T_I64;
92 case CT_DOUBLE:
93 return T_DOUBLE;
94 case CT_BINARY:
95 return T_STRING;
96 case CT_LIST:
97 return T_LIST;
98 case CT_SET:
99 return T_SET;
100 case CT_MAP:
101 return T_MAP;
102 case CT_STRUCT:
103 return T_STRUCT;
Carel Combrinka715bdf2025-10-30 07:44:21 +0100104 case CT_UUID:
105 return T_UUID;
Nobuaki Sukegawa6525f6a2016-02-11 13:58:39 +0900106 default:
107 PyErr_Format(PyExc_TypeError, "don't know what type: %d", type);
108 return static_cast<TType>(-1);
109 }
110}
111}
112}
113}