Debian Codegraph @

third_party/upb/python/unknown_fields.c

raw
1 /*
2 * Copyright (c) 2009-2021, Google LLC
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of Google LLC nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT,
20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include "python/unknown_fields.h"
29
30 #include "python/message.h"
31 #include "python/protobuf.h"
32
33 static const char* PyUpb_DecodeVarint(const char* ptr, const char* end,
34 uint64_t* val) {
35 *val = 0;
36 for (int i = 0; ptr < end && i < 10; i++, ptr++) {
37 uint64_t byte = (uint8_t)*ptr;
38 *val |= (byte & 0x7f) << (i * 7);
39 if ((byte & 0x80) == 0) {
40 return ptr + 1;
41 }
42 }
43 return NULL;
44 }
45
46 // -----------------------------------------------------------------------------
47 // UnknownFieldSet
48 // -----------------------------------------------------------------------------
49
50 typedef struct {
51 PyObject_HEAD;
52 PyObject* fields;
53 } PyUpb_UnknownFieldSet;
54
55 static void PyUpb_UnknownFieldSet_Dealloc(PyObject* _self) {
56 PyUpb_UnknownFieldSet* self = (PyUpb_UnknownFieldSet*)_self;
57 Py_XDECREF(self->fields);
58 PyUpb_Dealloc(self);
59 }
60
61 PyUpb_UnknownFieldSet* PyUpb_UnknownFieldSet_NewBare(void) {
62 PyUpb_ModuleState* s = PyUpb_ModuleState_Get();
63 PyUpb_UnknownFieldSet* self =
64 (void*)PyType_GenericAlloc(s->unknown_fields_type, 0);
65 return self;
66 }
67
68 // Generic functions to skip a value or group.
69
70 static const char* PyUpb_UnknownFieldSet_SkipGroup(const char* ptr,
71 const char* end,
72 int group_number);
73
74 static const char* PyUpb_UnknownFieldSet_SkipField(const char* ptr,
75 const char* end,
76 uint32_t tag) {
77 int field_number = tag >> 3;
78 int wire_type = tag & 7;
79 switch (wire_type) {
80 case kUpb_WireType_Varint: {
81 uint64_t val;
82 return PyUpb_DecodeVarint(ptr, end, &val);
83 }
84 case kUpb_WireType_64Bit:
85 if (end - ptr < 8) return NULL;
86 return ptr + 8;
87 case kUpb_WireType_32Bit:
88 if (end - ptr < 4) return NULL;
89 return ptr + 4;
90 case kUpb_WireType_Delimited: {
91 uint64_t size;
92 ptr = PyUpb_DecodeVarint(ptr, end, &size);
93 if (!ptr || end - ptr < size) return NULL;
94 return ptr + size;
95 }
96 case kUpb_WireType_StartGroup:
97 return PyUpb_UnknownFieldSet_SkipGroup(ptr, end, field_number);
98 case kUpb_WireType_EndGroup:
99 return NULL;
100 default:
101 assert(0);
102 return NULL;
103 }
104 }
105
106 static const char* PyUpb_UnknownFieldSet_SkipGroup(const char* ptr,
107 const char* end,
108 int group_number) {
109 uint32_t end_tag = (group_number << 3) | kUpb_WireType_EndGroup;
110 while (true) {
111 if (ptr == end) return NULL;
112 uint64_t tag;
113 ptr = PyUpb_DecodeVarint(ptr, end, &tag);
114 if (!ptr) return NULL;
115 if (tag == end_tag) return ptr;
116 ptr = PyUpb_UnknownFieldSet_SkipField(ptr, end, tag);
117 if (!ptr) return NULL;
118 }
119 return ptr;
120 }
121
122 // For MessageSet the established behavior is for UnknownFieldSet to interpret
123 // the MessageSet wire format:
124 // message MessageSet {
125 // repeated group Item = 1 {
126 // required int32 type_id = 2;
127 // required bytes message = 3;
128 // }
129 // }
130 //
131 // And create unknown fields like:
132 // UnknownField(type_id, WIRE_TYPE_DELIMITED, message)
133 //
134 // For any unknown fields that are unexpected per the wire format defined above,
135 // we drop them on the floor.
136
137 enum {
138 kUpb_MessageSet_StartItemTag = (1 << 3) | kUpb_WireType_StartGroup,
139 kUpb_MessageSet_EndItemTag = (1 << 3) | kUpb_WireType_EndGroup,
140 kUpb_MessageSet_TypeIdTag = (2 << 3) | kUpb_WireType_Varint,
141 kUpb_MessageSet_MessageTag = (3 << 3) | kUpb_WireType_Delimited,
142 };
143
144 static const char* PyUpb_UnknownFieldSet_BuildMessageSetItem(
145 PyUpb_UnknownFieldSet* self, const char* ptr, const char* end) {
146 PyUpb_ModuleState* s = PyUpb_ModuleState_Get();
147 int type_id = 0;
148 PyObject* msg = NULL;
149 while (true) {
150 if (ptr == end) goto err;
151 uint64_t tag;
152 ptr = PyUpb_DecodeVarint(ptr, end, &tag);
153 if (!ptr) goto err;
154 switch (tag) {
155 case kUpb_MessageSet_EndItemTag:
156 goto done;
157 case kUpb_MessageSet_TypeIdTag: {
158 uint64_t tmp;
159 ptr = PyUpb_DecodeVarint(ptr, end, &tmp);
160 if (!ptr) goto err;
161 if (!type_id) type_id = tmp;
162 break;
163 }
164 case kUpb_MessageSet_MessageTag: {
165 uint64_t size;
166 ptr = PyUpb_DecodeVarint(ptr, end, &size);
167 if (!ptr || end - ptr < size) goto err;
168 if (!msg) {
169 msg = PyBytes_FromStringAndSize(ptr, size);
170 if (!msg) goto err;
171 } else {
172 // already saw a message here so deliberately skipping the duplicate
173 }
174 ptr += size;
175 break;
176 }
177 default:
178 ptr = PyUpb_UnknownFieldSet_SkipField(ptr, end, tag);
179 if (!ptr) goto err;
180 }
181 }
182
183 done:
184 if (type_id && msg) {
185 PyObject* field = PyObject_CallFunction(
186 s->unknown_field_type, "iiO", type_id, kUpb_WireType_Delimited, msg);
187 if (!field) goto err;
188 PyList_Append(self->fields, field);
189 Py_DECREF(field);
190 }
191 Py_XDECREF(msg);
192 return ptr;
193
194 err:
195 Py_XDECREF(msg);
196 return NULL;
197 }
198
199 static const char* PyUpb_UnknownFieldSet_BuildMessageSet(
200 PyUpb_UnknownFieldSet* self, const char* ptr, const char* end) {
201 self->fields = PyList_New(0);
202 while (ptr < end) {
203 uint64_t tag;
204 ptr = PyUpb_DecodeVarint(ptr, end, &tag);
205 if (!ptr) goto err;
206 if (tag == kUpb_MessageSet_StartItemTag) {
207 ptr = PyUpb_UnknownFieldSet_BuildMessageSetItem(self, ptr, end);
208 } else {
209 ptr = PyUpb_UnknownFieldSet_SkipField(ptr, end, tag);
210 }
211 if (!ptr) goto err;
212 }
213 return ptr;
214
215 err:
216 Py_DECREF(self->fields);
217 self->fields = NULL;
218 return NULL;
219 }
220
221 static const char* PyUpb_UnknownFieldSet_Build(PyUpb_UnknownFieldSet* self,
222 const char* ptr, const char* end,
223 int group_number);
224
225 static const char* PyUpb_UnknownFieldSet_BuildValue(
226 PyUpb_UnknownFieldSet* self, const char* ptr, const char* end,
227 int field_number, int wire_type, int group_number, PyObject** data) {
228 switch (wire_type) {
229 case kUpb_WireType_Varint: {
230 uint64_t val;
231 ptr = PyUpb_DecodeVarint(ptr, end, &val);
232 if (!ptr) return NULL;
233 *data = PyLong_FromUnsignedLongLong(val);
234 return ptr;
235 }
236 case kUpb_WireType_64Bit: {
237 if (end - ptr < 8) return NULL;
238 uint64_t val;
239 memcpy(&val, ptr, 8);
240 *data = PyLong_FromUnsignedLongLong(val);
241 return ptr + 8;
242 }
243 case kUpb_WireType_32Bit: {
244 if (end - ptr < 4) return NULL;
245 uint32_t val;
246 memcpy(&val, ptr, 4);
247 *data = PyLong_FromUnsignedLongLong(val);
248 return ptr + 4;
249 }
250 case kUpb_WireType_Delimited: {
251 uint64_t size;
252 ptr = PyUpb_DecodeVarint(ptr, end, &size);
253 if (!ptr || end - ptr < size) return NULL;
254 *data = PyBytes_FromStringAndSize(ptr, size);
255 return ptr + size;
256 }
257 case kUpb_WireType_StartGroup: {
258 PyUpb_UnknownFieldSet* sub = PyUpb_UnknownFieldSet_NewBare();
259 if (!sub) return NULL;
260 *data = &sub->ob_base;
261 return PyUpb_UnknownFieldSet_Build(sub, ptr, end, field_number);
262 }
263 default:
264 assert(0);
265 *data = NULL;
266 return NULL;
267 }
268 }
269
270 // For non-MessageSet we just build the unknown fields exactly as they exist on
271 // the wire.
272 static const char* PyUpb_UnknownFieldSet_Build(PyUpb_UnknownFieldSet* self,
273 const char* ptr, const char* end,
274 int group_number) {
275 PyUpb_ModuleState* s = PyUpb_ModuleState_Get();
276 self->fields = PyList_New(0);
277 while (ptr < end) {
278 uint64_t tag;
279 ptr = PyUpb_DecodeVarint(ptr, end, &tag);
280 if (!ptr) goto err;
281 PyObject* data = NULL;
282 int field_number = tag >> 3;
283 int wire_type = tag & 7;
284 if (wire_type == kUpb_WireType_EndGroup) {
285 if (field_number != group_number) return NULL;
286 return ptr;
287 }
288 ptr = PyUpb_UnknownFieldSet_BuildValue(self, ptr, end, field_number,
289 wire_type, group_number, &data);
290 if (!ptr) {
291 Py_XDECREF(data);
292 goto err;
293 }
294 assert(data);
295 PyObject* field = PyObject_CallFunction(s->unknown_field_type, "iiN",
296 field_number, wire_type, data);
297 PyList_Append(self->fields, field);
298 Py_DECREF(field);
299 }
300 return ptr;
301
302 err:
303 Py_DECREF(self->fields);
304 self->fields = NULL;
305 return NULL;
306 }
307
308 static PyObject* PyUpb_UnknownFieldSet_New(PyTypeObject* type, PyObject* args,
309 PyObject* kwargs) {
310 char* kwlist[] = {"message", 0};
311 PyObject* py_msg = NULL;
312
313 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O", kwlist, &py_msg)) {
314 return NULL;
315 }
316
317 if (!PyUpb_Message_Verify(py_msg)) return NULL;
318 PyUpb_UnknownFieldSet* self = PyUpb_UnknownFieldSet_NewBare();
319 upb_Message* msg = PyUpb_Message_GetIfReified(py_msg);
320 if (!msg) return &self->ob_base;
321
322 size_t size;
323 const char* ptr = upb_Message_GetUnknown(msg, &size);
324 if (size == 0) return &self->ob_base;
325
326 const char* end = ptr + size;
327 const upb_MessageDef* msgdef = PyUpb_Message_GetMsgdef(py_msg);
328
329 bool ok;
330 if (upb_MessageDef_IsMessageSet(msgdef)) {
331 ok = PyUpb_UnknownFieldSet_BuildMessageSet(self, ptr, end) == end;
332 } else {
333 ok = PyUpb_UnknownFieldSet_Build(self, ptr, end, -1) == end;
334 }
335
336 if (!ok) {
337 Py_DECREF(&self->ob_base);
338 return NULL;
339 }
340
341 return &self->ob_base;
342 }
343
344 static Py_ssize_t PyUpb_UnknownFieldSet_Length(PyObject* _self) {
345 PyUpb_UnknownFieldSet* self = (PyUpb_UnknownFieldSet*)_self;
346 return self->fields ? PyObject_Length(self->fields) : 0;
347 }
348
349 static PyObject* PyUpb_UnknownFieldSet_GetItem(PyObject* _self,
350 Py_ssize_t index) {
351 PyUpb_UnknownFieldSet* self = (PyUpb_UnknownFieldSet*)_self;
352 if (!self->fields) {
353 PyErr_Format(PyExc_IndexError, "list index (%zd) out of range", index);
354 return NULL;
355 }
356 PyObject* ret = PyList_GetItem(self->fields, index);
357 if (ret) Py_INCREF(ret);
358 return ret;
359 }
360
361 static PyType_Slot PyUpb_UnknownFieldSet_Slots[] = {
362 {Py_tp_new, &PyUpb_UnknownFieldSet_New},
363 {Py_tp_dealloc, &PyUpb_UnknownFieldSet_Dealloc},
364 {Py_sq_length, PyUpb_UnknownFieldSet_Length},
365 {Py_sq_item, PyUpb_UnknownFieldSet_GetItem},
366 {Py_tp_hash, PyObject_HashNotImplemented},
367 {0, NULL},
368 };
369
370 static PyType_Spec PyUpb_UnknownFieldSet_Spec = {
371 PYUPB_MODULE_NAME ".UnknownFieldSet", // tp_name
372 sizeof(PyUpb_UnknownFieldSet), // tp_basicsize
373 0, // tp_itemsize
374 Py_TPFLAGS_DEFAULT, // tp_flags
375 PyUpb_UnknownFieldSet_Slots,
376 };
377
378 // -----------------------------------------------------------------------------
379 // Top Level
380 // -----------------------------------------------------------------------------
381
382 PyObject* PyUpb_UnknownFieldSet_CreateNamedTuple(void) {
383 PyObject* mod = NULL;
384 PyObject* namedtuple = NULL;
385 PyObject* ret = NULL;
386
387 mod = PyImport_ImportModule("collections");
388 if (!mod) goto done;
389 namedtuple = PyObject_GetAttrString(mod, "namedtuple");
390 if (!namedtuple) goto done;
391 ret = PyObject_CallFunction(namedtuple, "s[sss]", "PyUnknownField",
392 "field_number", "wire_type", "data");
393
394 done:
395 Py_XDECREF(mod);
396 Py_XDECREF(namedtuple);
397 return ret;
398 }
399
400 bool PyUpb_UnknownFields_Init(PyObject* m) {
401 PyUpb_ModuleState* s = PyUpb_ModuleState_GetFromModule(m);
402
403 s->unknown_fields_type = PyUpb_AddClass(m, &PyUpb_UnknownFieldSet_Spec);
404 s->unknown_field_type = PyUpb_UnknownFieldSet_CreateNamedTuple();
405
406 return s->unknown_fields_type && s->unknown_field_type;
407 }