2 * MessagePack for Python unpacking routine
4 * Copyright (C) 2009 Naoki INADA
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #define MSGPACK_EMBED_STACK_SIZE (1024)
20 #include "unpack_define.h"
22 typedef struct unpack_user {
24 PyObject *object_hook;
29 const char *unicode_errors;
32 typedef PyObject* msgpack_unpack_object;
33 struct unpack_context;
34 typedef struct unpack_context unpack_context;
35 typedef int (*execute_fn)(unpack_context *ctx, const char* data, size_t len, size_t* off);
37 static inline msgpack_unpack_object unpack_callback_root(unpack_user* u)
42 static inline int unpack_callback_uint16(unpack_user* u, uint16_t d, msgpack_unpack_object* o)
44 PyObject *p = PyInt_FromLong((long)d);
50 static inline int unpack_callback_uint8(unpack_user* u, uint8_t d, msgpack_unpack_object* o)
52 return unpack_callback_uint16(u, d, o);
56 static inline int unpack_callback_uint32(unpack_user* u, uint32_t d, msgpack_unpack_object* o)
59 #if UINT32_MAX > LONG_MAX
61 p = PyLong_FromUnsignedLong((unsigned long)d);
65 p = PyInt_FromLong((long)d);
73 static inline int unpack_callback_uint64(unpack_user* u, uint64_t d, msgpack_unpack_object* o)
77 p = PyLong_FromUnsignedLongLong((unsigned PY_LONG_LONG)d);
79 p = PyInt_FromLong((long)d);
87 static inline int unpack_callback_int32(unpack_user* u, int32_t d, msgpack_unpack_object* o)
89 PyObject *p = PyInt_FromLong(d);
96 static inline int unpack_callback_int16(unpack_user* u, int16_t d, msgpack_unpack_object* o)
98 return unpack_callback_int32(u, d, o);
101 static inline int unpack_callback_int8(unpack_user* u, int8_t d, msgpack_unpack_object* o)
103 return unpack_callback_int32(u, d, o);
106 static inline int unpack_callback_int64(unpack_user* u, int64_t d, msgpack_unpack_object* o)
109 if (d > LONG_MAX || d < LONG_MIN) {
110 p = PyLong_FromLongLong((unsigned PY_LONG_LONG)d);
112 p = PyInt_FromLong((long)d);
118 static inline int unpack_callback_double(unpack_user* u, double d, msgpack_unpack_object* o)
120 PyObject *p = PyFloat_FromDouble(d);
127 static inline int unpack_callback_float(unpack_user* u, float d, msgpack_unpack_object* o)
129 return unpack_callback_double(u, d, o);
132 static inline int unpack_callback_nil(unpack_user* u, msgpack_unpack_object* o)
133 { Py_INCREF(Py_None); *o = Py_None; return 0; }
135 static inline int unpack_callback_true(unpack_user* u, msgpack_unpack_object* o)
136 { Py_INCREF(Py_True); *o = Py_True; return 0; }
138 static inline int unpack_callback_false(unpack_user* u, msgpack_unpack_object* o)
139 { Py_INCREF(Py_False); *o = Py_False; return 0; }
141 static inline int unpack_callback_array(unpack_user* u, unsigned int n, msgpack_unpack_object* o)
143 PyObject *p = u->use_list ? PyList_New(n) : PyTuple_New(n);
151 static inline int unpack_callback_array_item(unpack_user* u, unsigned int current, msgpack_unpack_object* c, msgpack_unpack_object o)
154 PyList_SET_ITEM(*c, current, o);
156 PyTuple_SET_ITEM(*c, current, o);
160 static inline int unpack_callback_array_end(unpack_user* u, msgpack_unpack_object* c)
163 PyObject *new_c = PyObject_CallFunctionObjArgs(u->list_hook, *c, NULL);
172 static inline int unpack_callback_map(unpack_user* u, unsigned int n, msgpack_unpack_object* o)
175 if (u->has_pairs_hook) {
176 p = PyList_New(n); // Or use tuple?
187 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)
189 if (u->has_pairs_hook) {
190 msgpack_unpack_object item = PyTuple_Pack(2, k, v);
195 PyList_SET_ITEM(*c, current, item);
198 else if (PyDict_SetItem(*c, k, v) == 0) {
206 static inline int unpack_callback_map_end(unpack_user* u, msgpack_unpack_object* c)
208 if (u->object_hook) {
209 PyObject *new_c = PyObject_CallFunctionObjArgs(u->object_hook, *c, NULL);
219 static inline int unpack_callback_raw(unpack_user* u, const char* b, const char* p, unsigned int l, msgpack_unpack_object* o)
223 py = PyUnicode_Decode(p, l, u->encoding, u->unicode_errors);
225 py = PyBytes_FromStringAndSize(p, l);
233 static inline int unpack_callback_bin(unpack_user* u, const char* b, const char* p, unsigned int l, msgpack_unpack_object* o)
235 PyObject *py = PyBytes_FromStringAndSize(p, l);
242 static inline int unpack_callback_ext(unpack_user* u, const char* base, const char* pos,
243 unsigned int lenght, msgpack_unpack_object* o)
246 int8_t typecode = (int8_t)*pos++;
248 PyErr_SetString(PyExc_AssertionError, "u->ext_hook cannot be NULL");
251 // length also includes the typecode, so the actual data is lenght-1
252 #if PY_MAJOR_VERSION == 2
253 py = PyObject_CallFunction(u->ext_hook, "(is#)", typecode, pos, lenght-1);
255 py = PyObject_CallFunction(u->ext_hook, "(iy#)", typecode, pos, lenght-1);
263 #include "unpack_template.h"