summaryrefslogtreecommitdiff
path: root/msgpack/unpack.h
blob: 34212bcd7a179f2ca2af684f0974c783ac6658b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/*
 * MessagePack for Python unpacking routine
 *
 * Copyright (C) 2009 Naoki INADA
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

#define MSGPACK_EMBED_STACK_SIZE  (1024)
#include "unpack_define.h"

typedef struct unpack_user {
    bool use_list;
    bool raw;
    bool has_pairs_hook;
    bool strict_map_key;
    int timestamp;
    PyObject *object_hook;
    PyObject *list_hook;
    PyObject *ext_hook;
    PyObject *timestamp_t;
    PyObject *giga;
    PyObject *utc;
    const char *unicode_errors;
    Py_ssize_t max_str_len, max_bin_len, max_array_len, max_map_len, max_ext_len;
} unpack_user;

typedef PyObject* msgpack_unpack_object;
struct unpack_context;
typedef struct unpack_context unpack_context;
typedef int (*execute_fn)(unpack_context *ctx, const char* data, Py_ssize_t len, Py_ssize_t* off);

static inline msgpack_unpack_object unpack_callback_root(unpack_user* u)
{
    return NULL;
}

static inline int unpack_callback_uint16(unpack_user* u, uint16_t d, msgpack_unpack_object* o)
{
    PyObject *p = PyInt_FromLong((long)d);
    if (!p)
        return -1;
    *o = p;
    return 0;
}
static inline int unpack_callback_uint8(unpack_user* u, uint8_t d, msgpack_unpack_object* o)
{
    return unpack_callback_uint16(u, d, o);
}


static inline int unpack_callback_uint32(unpack_user* u, uint32_t d, msgpack_unpack_object* o)
{
    PyObject *p = PyInt_FromSize_t((size_t)d);
    if (!p)
        return -1;
    *o = p;
    return 0;
}

static inline int unpack_callback_uint64(unpack_user* u, uint64_t d, msgpack_unpack_object* o)
{
    PyObject *p;
    if (d > LONG_MAX) {
        p = PyLong_FromUnsignedLongLong((unsigned PY_LONG_LONG)d);
    } else {
        p = PyInt_FromLong((long)d);
    }
    if (!p)
        return -1;
    *o = p;
    return 0;
}

static inline int unpack_callback_int32(unpack_user* u, int32_t d, msgpack_unpack_object* o)
{
    PyObject *p = PyInt_FromLong(d);
    if (!p)
        return -1;
    *o = p;
    return 0;
}

static inline int unpack_callback_int16(unpack_user* u, int16_t d, msgpack_unpack_object* o)
{
    return unpack_callback_int32(u, d, o);
}

static inline int unpack_callback_int8(unpack_user* u, int8_t d, msgpack_unpack_object* o)
{
    return unpack_callback_int32(u, d, o);
}

static inline int unpack_callback_int64(unpack_user* u, int64_t d, msgpack_unpack_object* o)
{
    PyObject *p;
    if (d > LONG_MAX || d < LONG_MIN) {
        p = PyLong_FromLongLong((PY_LONG_LONG)d);
    } else {
        p = PyInt_FromLong((long)d);
    }
    *o = p;
    return 0;
}

static inline int unpack_callback_double(unpack_user* u, double d, msgpack_unpack_object* o)
{
    PyObject *p = PyFloat_FromDouble(d);
    if (!p)
        return -1;
    *o = p;
    return 0;
}

static inline int unpack_callback_float(unpack_user* u, float d, msgpack_unpack_object* o)
{
    return unpack_callback_double(u, d, o);
}

static inline int unpack_callback_nil(unpack_user* u, msgpack_unpack_object* o)
{ Py_INCREF(Py_None); *o = Py_None; return 0; }

static inline int unpack_callback_true(unpack_user* u, msgpack_unpack_object* o)
{ Py_INCREF(Py_True); *o = Py_True; return 0; }

static inline int unpack_callback_false(unpack_user* u, msgpack_unpack_object* o)
{ Py_INCREF(Py_False); *o = Py_False; return 0; }

static inline int unpack_callback_array(unpack_user* u, unsigned int n, msgpack_unpack_object* o)
{
    if (n > u->max_array_len) {
        PyErr_Format(PyExc_ValueError, "%u exceeds max_array_len(%zd)", n, u->max_array_len);
        return -1;
    }
    PyObject *p = u->use_list ? PyList_New(n) : PyTuple_New(n);

    if (!p)
        return -1;
    *o = p;
    return 0;
}

static inline int unpack_callback_array_item(unpack_user* u, unsigned int current, msgpack_unpack_object* c, msgpack_unpack_object o)
{
    if (u->use_list)
        PyList_SET_ITEM(*c, current, o);
    else
        PyTuple_SET_ITEM(*c, current, o);
    return 0;
}

static inline int unpack_callback_array_end(unpack_user* u, msgpack_unpack_object* c)
{
    if (u->list_hook) {
        PyObject *new_c = PyObject_CallFunctionObjArgs(u->list_hook, *c, NULL);
        if (!new_c)
            return -1;
        Py_DECREF(*c);
        *c = new_c;
    }
    return 0;
}

static inline int unpack_callback_map(unpack_user* u, unsigned int n, msgpack_unpack_object* o)
{
    if (n > u->max_map_len) {
        PyErr_Format(PyExc_ValueError, "%u exceeds max_map_len(%zd)", n, u->max_map_len);
        return -1;
    }
    PyObject *p;
    if (u->has_pairs_hook) {
        p = PyList_New(n); // Or use tuple?
    }
    else {
        p = PyDict_New();
    }
    if (!p)
        return -1;
    *o = p;
    return 0;
}

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)
{
    if (u->strict_map_key && !PyUnicode_CheckExact(k) && !PyBytes_CheckExact(k)) {
        PyErr_Format(PyExc_ValueError, "%.100s is not allowed for map key", Py_TYPE(k)->tp_name);
        return -1;
    }
    if (PyUnicode_CheckExact(k)) {
        PyUnicode_InternInPlace(&k);
    }
    if (u->has_pairs_hook) {
        msgpack_unpack_object item = PyTuple_Pack(2, k, v);
        if (!item)
            return -1;
        Py_DECREF(k);
        Py_DECREF(v);
        PyList_SET_ITEM(*c, current, item);
        return 0;
    }
    else if (PyDict_SetItem(*c, k, v) == 0) {
        Py_DECREF(k);
        Py_DECREF(v);
        return 0;
    }
    return -1;
}

static inline int unpack_callback_map_end(unpack_user* u, msgpack_unpack_object* c)
{
    if (u->object_hook) {
        PyObject *new_c = PyObject_CallFunctionObjArgs(u->object_hook, *c, NULL);
        if (!new_c)
            return -1;

        Py_DECREF(*c);
        *c = new_c;
    }
    return 0;
}

static inline int unpack_callback_raw(unpack_user* u, const char* b, const char* p, unsigned int l, msgpack_unpack_object* o)
{
    if (l > u->max_str_len) {
        PyErr_Format(PyExc_ValueError, "%u exceeds max_str_len(%zd)", l, u->max_str_len);
        return -1;
    }

    PyObject *py;

    if (u->raw) {
        py = PyBytes_FromStringAndSize(p, l);
    } else {
        py = PyUnicode_DecodeUTF8(p, l, u->unicode_errors);
    }
    if (!py)
        return -1;
    *o = py;
    return 0;
}

static inline int unpack_callback_bin(unpack_user* u, const char* b, const char* p, unsigned int l, msgpack_unpack_object* o)
{
    if (l > u->max_bin_len) {
        PyErr_Format(PyExc_ValueError, "%u exceeds max_bin_len(%zd)", l, u->max_bin_len);
        return -1;
    }

    PyObject *py = PyBytes_FromStringAndSize(p, l);
    if (!py)
        return -1;
    *o = py;
    return 0;
}

typedef struct msgpack_timestamp {
    int64_t tv_sec;
    uint32_t tv_nsec;
} msgpack_timestamp;

/*
 * Unpack ext buffer to a timestamp. Pulled from msgpack-c timestamp.h.
 */
static int unpack_timestamp(const char* buf, unsigned int buflen, msgpack_timestamp* ts) {
    switch (buflen) {
    case 4:
        ts->tv_nsec = 0;
        {
            uint32_t v = _msgpack_load32(uint32_t, buf);
            ts->tv_sec = (int64_t)v;
        }
        return 0;
    case 8: {
        uint64_t value =_msgpack_load64(uint64_t, buf);
        ts->tv_nsec = (uint32_t)(value >> 34);
        ts->tv_sec = value & 0x00000003ffffffffLL;
        return 0;
    }
    case 12:
        ts->tv_nsec = _msgpack_load32(uint32_t, buf);
        ts->tv_sec = _msgpack_load64(int64_t, buf + 4);
        return 0;
    default:
        return -1;
    }
}

#include "datetime.h"

static int unpack_callback_ext(unpack_user* u, const char* base, const char* pos,
                               unsigned int length, msgpack_unpack_object* o)
{
    int8_t typecode = (int8_t)*pos++;
    if (!u->ext_hook) {
        PyErr_SetString(PyExc_AssertionError, "u->ext_hook cannot be NULL");
        return -1;
    }
    if (length-1 > u->max_ext_len) {
        PyErr_Format(PyExc_ValueError, "%u exceeds max_ext_len(%zd)", length, u->max_ext_len);
        return -1;
    }

    PyObject *py = NULL;
    // length also includes the typecode, so the actual data is length-1
    if (typecode == -1) {
        msgpack_timestamp ts;
        if (unpack_timestamp(pos, length-1, &ts) < 0) {
            return -1;
        }

        if (u->timestamp == 2) {  // int
            PyObject *a = PyLong_FromLongLong(ts.tv_sec);
            if (a == NULL) return -1;

            PyObject *c = PyNumber_Multiply(a, u->giga);
            Py_DECREF(a);
            if (c == NULL) {
                return -1;
            }

            PyObject *b = PyLong_FromUnsignedLong(ts.tv_nsec);
            if (b == NULL) {
                Py_DECREF(c);
                return -1;
            }

            py = PyNumber_Add(c, b);
            Py_DECREF(c);
            Py_DECREF(b);
        }
        else if (u->timestamp == 0) {  // Timestamp
            py = PyObject_CallFunction(u->timestamp_t, "(Lk)", ts.tv_sec, ts.tv_nsec);
        }
        else if (u->timestamp == 3) {  // datetime
            // Calculate datetime using epoch + delta
            // due to limitations PyDateTime_FromTimestamp on Windows with negative timestamps
            PyObject *epoch = PyDateTimeAPI->DateTime_FromDateAndTime(1970, 1, 1, 0, 0, 0, 0, u->utc, PyDateTimeAPI->DateTimeType);
            if (epoch == NULL) {
                return -1;
            }

            PyObject* d = PyDelta_FromDSU(ts.tv_sec/(24*3600), ts.tv_sec%(24*3600), ts.tv_nsec / 1000);
            if (d == NULL) {
                Py_DECREF(epoch);
                return -1;
            }

            py = PyNumber_Add(epoch, d);

            Py_DECREF(epoch);
            Py_DECREF(d);
        }
        else { // float
            PyObject *a = PyFloat_FromDouble((double)ts.tv_nsec);
            if (a == NULL) return -1;

            PyObject *b = PyNumber_TrueDivide(a, u->giga);
            Py_DECREF(a);
            if (b == NULL) return -1;

            PyObject *c = PyLong_FromLongLong(ts.tv_sec);
            if (c == NULL) {
                Py_DECREF(b);
                return -1;
            }

            a = PyNumber_Add(b, c);
            Py_DECREF(b);
            Py_DECREF(c);
            py = a;
        }
    } else {
        py = PyObject_CallFunction(u->ext_hook, "(iy#)", (int)typecode, pos, (Py_ssize_t)length-1);
    }
    if (!py)
        return -1;
    *o = py;
    return 0;
}

#include "unpack_template.h"