Debian Codegraph @

msgpack/unpack.h

raw
1 /*
2 * MessagePack for Python unpacking routine
3 *
4 * Copyright (C) 2009 Naoki INADA
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * 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, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 #define MSGPACK_EMBED_STACK_SIZE (1024)
20 #include "unpack_define.h"
21
22 typedef struct unpack_user {
23 bool use_list;
24 bool raw;
25 bool has_pairs_hook;
26 bool strict_map_key;
27 int timestamp;
28 PyObject *object_hook;
29 PyObject *list_hook;
30 PyObject *ext_hook;
31 PyObject *timestamp_t;
32 PyObject *giga;
33 PyObject *utc;
34 const char *unicode_errors;
35 Py_ssize_t max_str_len, max_bin_len, max_array_len, max_map_len, max_ext_len;
36 } unpack_user;
37
38 typedef PyObject* msgpack_unpack_object;
39 struct unpack_context;
40 typedef struct unpack_context unpack_context;
41 typedef int (*execute_fn)(unpack_context *ctx, const char* data, Py_ssize_t len, Py_ssize_t* off);
42
43 static inline msgpack_unpack_object unpack_callback_root(unpack_user* u)
44 {
45 return NULL;
46 }
47
48 static inline int unpack_callback_uint16(unpack_user* u, uint16_t d, msgpack_unpack_object* o)
49 {
50 PyObject *p = PyLong_FromLong((long)d);
51 if (!p)
52 return -1;
53 *o = p;
54 return 0;
55 }
56 static inline int unpack_callback_uint8(unpack_user* u, uint8_t d, msgpack_unpack_object* o)
57 {
58 return unpack_callback_uint16(u, d, o);
59 }
60
61
62 static inline int unpack_callback_uint32(unpack_user* u, uint32_t d, msgpack_unpack_object* o)
63 {
64 PyObject *p = PyLong_FromSize_t((size_t)d);
65 if (!p)
66 return -1;
67 *o = p;
68 return 0;
69 }
70
71 static inline int unpack_callback_uint64(unpack_user* u, uint64_t d, msgpack_unpack_object* o)
72 {
73 PyObject *p = PyLong_FromUnsignedLongLong((unsigned PY_LONG_LONG)d);
74 if (!p)
75 return -1;
76 *o = p;
77 return 0;
78 }
79
80 static inline int unpack_callback_int32(unpack_user* u, int32_t d, msgpack_unpack_object* o)
81 {
82 PyObject *p = PyLong_FromLong(d);
83 if (!p)
84 return -1;
85 *o = p;
86 return 0;
87 }
88
89 static inline int unpack_callback_int16(unpack_user* u, int16_t d, msgpack_unpack_object* o)
90 {
91 return unpack_callback_int32(u, d, o);
92 }
93
94 static inline int unpack_callback_int8(unpack_user* u, int8_t d, msgpack_unpack_object* o)
95 {
96 return unpack_callback_int32(u, d, o);
97 }
98
99 static inline int unpack_callback_int64(unpack_user* u, int64_t d, msgpack_unpack_object* o)
100 {
101 PyObject *p = PyLong_FromLongLong((PY_LONG_LONG)d);
102 if (!p)
103 return -1;
104 *o = p;
105 return 0;
106 }
107
108 static inline int unpack_callback_double(unpack_user* u, double d, msgpack_unpack_object* o)
109 {
110 PyObject *p = PyFloat_FromDouble(d);
111 if (!p)
112 return -1;
113 *o = p;
114 return 0;
115 }
116
117 static inline int unpack_callback_float(unpack_user* u, float d, msgpack_unpack_object* o)
118 {
119 return unpack_callback_double(u, d, o);
120 }
121
122 static inline int unpack_callback_nil(unpack_user* u, msgpack_unpack_object* o)
123 { Py_INCREF(Py_None); *o = Py_None; return 0; }
124
125 static inline int unpack_callback_true(unpack_user* u, msgpack_unpack_object* o)
126 { Py_INCREF(Py_True); *o = Py_True; return 0; }
127
128 static inline int unpack_callback_false(unpack_user* u, msgpack_unpack_object* o)
129 { Py_INCREF(Py_False); *o = Py_False; return 0; }
130
131 static inline int unpack_callback_array(unpack_user* u, unsigned int n, msgpack_unpack_object* o)
132 {
133 if (n > u->max_array_len) {
134 PyErr_Format(PyExc_ValueError, "%u exceeds max_array_len(%zd)", n, u->max_array_len);
135 return -1;
136 }
137 PyObject *p = u->use_list ? PyList_New(n) : PyTuple_New(n);
138
139 if (!p)
140 return -1;
141 *o = p;
142 return 0;
143 }
144
145 static inline int unpack_callback_array_item(unpack_user* u, unsigned int current, msgpack_unpack_object* c, msgpack_unpack_object o)
146 {
147 if (u->use_list)
148 PyList_SET_ITEM(*c, current, o);
149 else
150 PyTuple_SET_ITEM(*c, current, o);
151 return 0;
152 }
153
154 static inline int unpack_callback_array_end(unpack_user* u, msgpack_unpack_object* c)
155 {
156 if (u->list_hook) {
157 PyObject *new_c = PyObject_CallFunctionObjArgs(u->list_hook, *c, NULL);
158 if (!new_c)
159 return -1;
160 Py_DECREF(*c);
161 *c = new_c;
162 }
163 return 0;
164 }
165
166 static inline int unpack_callback_map(unpack_user* u, unsigned int n, msgpack_unpack_object* o)
167 {
168 if (n > u->max_map_len) {
169 PyErr_Format(PyExc_ValueError, "%u exceeds max_map_len(%zd)", n, u->max_map_len);
170 return -1;
171 }
172 PyObject *p;
173 if (u->has_pairs_hook) {
174 p = PyList_New(n); // Or use tuple?
175 }
176 else {
177 p = PyDict_New();
178 }
179 if (!p)
180 return -1;
181 *o = p;
182 return 0;
183 }
184
185 static inline int unpack_callback_map_item(unpack_user* u, unsigned int current, msgpack_unpack_object* c, msgpack_unpack_object k, msgpack_unpack_object v)
186 {
187 if (u->strict_map_key && !PyUnicode_CheckExact(k) && !PyBytes_CheckExact(k)) {
188 PyErr_Format(PyExc_ValueError, "%.100s is not allowed for map key when strict_map_key=True", Py_TYPE(k)->tp_name);
189 return -1;
190 }
191 if (PyUnicode_CheckExact(k)) {
192 PyUnicode_InternInPlace(&k);
193 }
194 if (u->has_pairs_hook) {
195 msgpack_unpack_object item = PyTuple_Pack(2, k, v);
196 if (!item)
197 return -1;
198 Py_DECREF(k);
199 Py_DECREF(v);
200 PyList_SET_ITEM(*c, current, item);
201 return 0;
202 }
203 else if (PyDict_SetItem(*c, k, v) == 0) {
204 Py_DECREF(k);
205 Py_DECREF(v);
206 return 0;
207 }
208 return -1;
209 }
210
211 static inline int unpack_callback_map_end(unpack_user* u, msgpack_unpack_object* c)
212 {
213 if (u->object_hook) {
214 PyObject *new_c = PyObject_CallFunctionObjArgs(u->object_hook, *c, NULL);
215 if (!new_c)
216 return -1;
217
218 Py_DECREF(*c);
219 *c = new_c;
220 }
221 return 0;
222 }
223
224 static inline int unpack_callback_raw(unpack_user* u, const char* b, const char* p, unsigned int l, msgpack_unpack_object* o)
225 {
226 if (l > u->max_str_len) {
227 PyErr_Format(PyExc_ValueError, "%u exceeds max_str_len(%zd)", l, u->max_str_len);
228 return -1;
229 }
230
231 PyObject *py;
232
233 if (u->raw) {
234 py = PyBytes_FromStringAndSize(p, l);
235 } else {
236 py = PyUnicode_DecodeUTF8(p, l, u->unicode_errors);
237 }
238 if (!py)
239 return -1;
240 *o = py;
241 return 0;
242 }
243
244 static inline int unpack_callback_bin(unpack_user* u, const char* b, const char* p, unsigned int l, msgpack_unpack_object* o)
245 {
246 if (l > u->max_bin_len) {
247 PyErr_Format(PyExc_ValueError, "%u exceeds max_bin_len(%zd)", l, u->max_bin_len);
248 return -1;
249 }
250
251 PyObject *py = PyBytes_FromStringAndSize(p, l);
252 if (!py)
253 return -1;
254 *o = py;
255 return 0;
256 }
257
258 typedef struct msgpack_timestamp {
259 int64_t tv_sec;
260 uint32_t tv_nsec;
261 } msgpack_timestamp;
262
263 /*
264 * Unpack ext buffer to a timestamp. Pulled from msgpack-c timestamp.h.
265 */
266 static int unpack_timestamp(const char* buf, unsigned int buflen, msgpack_timestamp* ts) {
267 switch (buflen) {
268 case 4:
269 ts->tv_nsec = 0;
270 {
271 uint32_t v = _msgpack_load32(uint32_t, buf);
272 ts->tv_sec = (int64_t)v;
273 }
274 return 0;
275 case 8: {
276 uint64_t value =_msgpack_load64(uint64_t, buf);
277 ts->tv_nsec = (uint32_t)(value >> 34);
278 ts->tv_sec = value & 0x00000003ffffffffLL;
279 return 0;
280 }
281 case 12:
282 ts->tv_nsec = _msgpack_load32(uint32_t, buf);
283 ts->tv_sec = _msgpack_load64(int64_t, buf + 4);
284 return 0;
285 default:
286 return -1;
287 }
288 }
289
290 #include "datetime.h"
291
292 static int unpack_callback_ext(unpack_user* u, const char* base, const char* pos,
293 unsigned int length, msgpack_unpack_object* o)
294 {
295 int8_t typecode = (int8_t)*pos++;
296 if (!u->ext_hook) {
297 PyErr_SetString(PyExc_AssertionError, "u->ext_hook cannot be NULL");
298 return -1;
299 }
300 if (length-1 > u->max_ext_len) {
301 PyErr_Format(PyExc_ValueError, "%u exceeds max_ext_len(%zd)", length, u->max_ext_len);
302 return -1;
303 }
304
305 PyObject *py = NULL;
306 // length also includes the typecode, so the actual data is length-1
307 if (typecode == -1) {
308 msgpack_timestamp ts;
309 if (unpack_timestamp(pos, length-1, &ts) < 0) {
310 return -1;
311 }
312
313 if (u->timestamp == 2) { // int
314 PyObject *a = PyLong_FromLongLong(ts.tv_sec);
315 if (a == NULL) return -1;
316
317 PyObject *c = PyNumber_Multiply(a, u->giga);
318 Py_DECREF(a);
319 if (c == NULL) {
320 return -1;
321 }
322
323 PyObject *b = PyLong_FromUnsignedLong(ts.tv_nsec);
324 if (b == NULL) {
325 Py_DECREF(c);
326 return -1;
327 }
328
329 py = PyNumber_Add(c, b);
330 Py_DECREF(c);
331 Py_DECREF(b);
332 }
333 else if (u->timestamp == 0) { // Timestamp
334 py = PyObject_CallFunction(u->timestamp_t, "(Lk)", ts.tv_sec, ts.tv_nsec);
335 }
336 else if (u->timestamp == 3) { // datetime
337 // Calculate datetime using epoch + delta
338 // due to limitations PyDateTime_FromTimestamp on Windows with negative timestamps
339 PyObject *epoch = PyDateTimeAPI->DateTime_FromDateAndTime(1970, 1, 1, 0, 0, 0, 0, u->utc, PyDateTimeAPI->DateTimeType);
340 if (epoch == NULL) {
341 return -1;
342 }
343
344 PyObject* d = PyDelta_FromDSU(ts.tv_sec/(24*3600), ts.tv_sec%(24*3600), ts.tv_nsec / 1000);
345 if (d == NULL) {
346 Py_DECREF(epoch);
347 return -1;
348 }
349
350 py = PyNumber_Add(epoch, d);
351
352 Py_DECREF(epoch);
353 Py_DECREF(d);
354 }
355 else { // float
356 PyObject *a = PyFloat_FromDouble((double)ts.tv_nsec);
357 if (a == NULL) return -1;
358
359 PyObject *b = PyNumber_TrueDivide(a, u->giga);
360 Py_DECREF(a);
361 if (b == NULL) return -1;
362
363 PyObject *c = PyLong_FromLongLong(ts.tv_sec);
364 if (c == NULL) {
365 Py_DECREF(b);
366 return -1;
367 }
368
369 a = PyNumber_Add(b, c);
370 Py_DECREF(b);
371 Py_DECREF(c);
372 py = a;
373 }
374 } else {
375 py = PyObject_CallFunction(u->ext_hook, "(iy#)", (int)typecode, pos, (Py_ssize_t)length-1);
376 }
377 if (!py)
378 return -1;
379 *o = py;
380 return 0;
381 }
382
383 #include "unpack_template.h"