OSDN Git Service

three.jsをThirdPartyに追加
[webglgame/webgl_framework.git] / webglFramework / Thirdparty / three.js-master / utils / exporters / blender / modules / msgpack / unpack.h
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     int use_list;
24     PyObject *object_hook;
25     bool has_pairs_hook;
26     PyObject *list_hook;
27     PyObject *ext_hook;
28     const char *encoding;
29     const char *unicode_errors;
30 } unpack_user;
31
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);
36
37 static inline msgpack_unpack_object unpack_callback_root(unpack_user* u)
38 {
39     return NULL;
40 }
41
42 static inline int unpack_callback_uint16(unpack_user* u, uint16_t d, msgpack_unpack_object* o)
43 {
44     PyObject *p = PyInt_FromLong((long)d);
45     if (!p)
46         return -1;
47     *o = p;
48     return 0;
49 }
50 static inline int unpack_callback_uint8(unpack_user* u, uint8_t d, msgpack_unpack_object* o)
51 {
52     return unpack_callback_uint16(u, d, o);
53 }
54
55
56 static inline int unpack_callback_uint32(unpack_user* u, uint32_t d, msgpack_unpack_object* o)
57 {
58     PyObject *p;
59 #if UINT32_MAX > LONG_MAX
60     if (d > LONG_MAX) {
61         p = PyLong_FromUnsignedLong((unsigned long)d);
62     } else
63 #endif
64     {
65         p = PyInt_FromLong((long)d);
66     }
67     if (!p)
68         return -1;
69     *o = p;
70     return 0;
71 }
72
73 static inline int unpack_callback_uint64(unpack_user* u, uint64_t d, msgpack_unpack_object* o)
74 {
75     PyObject *p;
76     if (d > LONG_MAX) {
77         p = PyLong_FromUnsignedLongLong((unsigned PY_LONG_LONG)d);
78     } else {
79         p = PyInt_FromLong((long)d);
80     }
81     if (!p)
82         return -1;
83     *o = p;
84     return 0;
85 }
86
87 static inline int unpack_callback_int32(unpack_user* u, int32_t d, msgpack_unpack_object* o)
88 {
89     PyObject *p = PyInt_FromLong(d);
90     if (!p)
91         return -1;
92     *o = p;
93     return 0;
94 }
95
96 static inline int unpack_callback_int16(unpack_user* u, int16_t d, msgpack_unpack_object* o)
97 {
98     return unpack_callback_int32(u, d, o);
99 }
100
101 static inline int unpack_callback_int8(unpack_user* u, int8_t d, msgpack_unpack_object* o)
102 {
103     return unpack_callback_int32(u, d, o);
104 }
105
106 static inline int unpack_callback_int64(unpack_user* u, int64_t d, msgpack_unpack_object* o)
107 {
108     PyObject *p;
109     if (d > LONG_MAX || d < LONG_MIN) {
110         p = PyLong_FromLongLong((unsigned PY_LONG_LONG)d);
111     } else {
112         p = PyInt_FromLong((long)d);
113     }
114     *o = p;
115     return 0;
116 }
117
118 static inline int unpack_callback_double(unpack_user* u, double d, msgpack_unpack_object* o)
119 {
120     PyObject *p = PyFloat_FromDouble(d);
121     if (!p)
122         return -1;
123     *o = p;
124     return 0;
125 }
126
127 static inline int unpack_callback_float(unpack_user* u, float d, msgpack_unpack_object* o)
128 {
129     return unpack_callback_double(u, d, o);
130 }
131
132 static inline int unpack_callback_nil(unpack_user* u, msgpack_unpack_object* o)
133 { Py_INCREF(Py_None); *o = Py_None; return 0; }
134
135 static inline int unpack_callback_true(unpack_user* u, msgpack_unpack_object* o)
136 { Py_INCREF(Py_True); *o = Py_True; return 0; }
137
138 static inline int unpack_callback_false(unpack_user* u, msgpack_unpack_object* o)
139 { Py_INCREF(Py_False); *o = Py_False; return 0; }
140
141 static inline int unpack_callback_array(unpack_user* u, unsigned int n, msgpack_unpack_object* o)
142 {
143     PyObject *p = u->use_list ? PyList_New(n) : PyTuple_New(n);
144
145     if (!p)
146         return -1;
147     *o = p;
148     return 0;
149 }
150
151 static inline int unpack_callback_array_item(unpack_user* u, unsigned int current, msgpack_unpack_object* c, msgpack_unpack_object o)
152 {
153     if (u->use_list)
154         PyList_SET_ITEM(*c, current, o);
155     else
156         PyTuple_SET_ITEM(*c, current, o);
157     return 0;
158 }
159
160 static inline int unpack_callback_array_end(unpack_user* u, msgpack_unpack_object* c)
161 {
162     if (u->list_hook) {
163         PyObject *new_c = PyObject_CallFunctionObjArgs(u->list_hook, *c, NULL);
164         if (!new_c)
165             return -1;
166         Py_DECREF(*c);
167         *c = new_c;
168     }
169     return 0;
170 }
171
172 static inline int unpack_callback_map(unpack_user* u, unsigned int n, msgpack_unpack_object* o)
173 {
174     PyObject *p;
175     if (u->has_pairs_hook) {
176         p = PyList_New(n); // Or use tuple?
177     }
178     else {
179         p = PyDict_New();
180     }
181     if (!p)
182         return -1;
183     *o = p;
184     return 0;
185 }
186
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)
188 {
189     if (u->has_pairs_hook) {
190         msgpack_unpack_object item = PyTuple_Pack(2, k, v);
191         if (!item)
192             return -1;
193         Py_DECREF(k);
194         Py_DECREF(v);
195         PyList_SET_ITEM(*c, current, item);
196         return 0;
197     }
198     else if (PyDict_SetItem(*c, k, v) == 0) {
199         Py_DECREF(k);
200         Py_DECREF(v);
201         return 0;
202     }
203     return -1;
204 }
205
206 static inline int unpack_callback_map_end(unpack_user* u, msgpack_unpack_object* c)
207 {
208     if (u->object_hook) {
209         PyObject *new_c = PyObject_CallFunctionObjArgs(u->object_hook, *c, NULL);
210         if (!new_c)
211             return -1;
212
213         Py_DECREF(*c);
214         *c = new_c;
215     }
216     return 0;
217 }
218
219 static inline int unpack_callback_raw(unpack_user* u, const char* b, const char* p, unsigned int l, msgpack_unpack_object* o)
220 {
221     PyObject *py;
222     if(u->encoding) {
223         py = PyUnicode_Decode(p, l, u->encoding, u->unicode_errors);
224     } else {
225         py = PyBytes_FromStringAndSize(p, l);
226     }
227     if (!py)
228         return -1;
229     *o = py;
230     return 0;
231 }
232
233 static inline int unpack_callback_bin(unpack_user* u, const char* b, const char* p, unsigned int l, msgpack_unpack_object* o)
234 {
235     PyObject *py = PyBytes_FromStringAndSize(p, l);
236     if (!py)
237         return -1;
238     *o = py;
239     return 0;
240 }
241
242 static inline int unpack_callback_ext(unpack_user* u, const char* base, const char* pos,
243                                       unsigned int lenght, msgpack_unpack_object* o)
244 {
245     PyObject *py;
246     int8_t typecode = (int8_t)*pos++;
247     if (!u->ext_hook) {
248         PyErr_SetString(PyExc_AssertionError, "u->ext_hook cannot be NULL");
249         return -1;
250     }
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);
254 #else
255     py = PyObject_CallFunction(u->ext_hook, "(iy#)", typecode, pos, lenght-1);
256 #endif
257     if (!py)
258         return -1;
259     *o = py;
260     return 0;
261 }
262
263 #include "unpack_template.h"