|
0 |
#include "Python.h"
|
|
1 |
|
|
2 |
#include "cbor.h"
|
|
3 |
|
|
4 |
#include <math.h>
|
|
5 |
#include <stdint.h>
|
|
6 |
|
|
7 |
//#include <stdio.h>
|
|
8 |
#include <arpa/inet.h>
|
|
9 |
|
|
10 |
|
|
11 |
#ifndef DEBUG_LOGGING
|
|
12 |
// causes things to be written to stderr
|
|
13 |
#define DEBUG_LOGGING 0
|
|
14 |
//#define DEBUG_LOGGING 1
|
|
15 |
#endif
|
|
16 |
|
|
17 |
|
|
18 |
#ifdef Py_InitModule
|
|
19 |
// Python 2.7
|
|
20 |
|
|
21 |
#define HAS_FILE_READER 1
|
|
22 |
#define IS_PY3 0
|
|
23 |
|
|
24 |
#else
|
|
25 |
|
|
26 |
#define HAS_FILE_READER 0
|
|
27 |
#define IS_PY3 1
|
|
28 |
|
|
29 |
#endif
|
|
30 |
|
|
31 |
// Hey Look! It's a polymorphic object structure in C!
|
|
32 |
|
|
33 |
// read(, len): read len bytes and return in buffer, or NULL on error
|
|
34 |
// read1(, uint8_t*): read one byte and return 0 on success
|
|
35 |
// return_buffer(, *): release result of read(, len)
|
|
36 |
// delete(): destructor. free thiz and contents.
|
|
37 |
#define READER_FUNCTIONS \
|
|
38 |
void* (*read)(void* self, Py_ssize_t len); \
|
|
39 |
int (*read1)(void* self, uint8_t* oneByte); \
|
|
40 |
void (*return_buffer)(void* self, void* buffer); \
|
|
41 |
void (*delete)(void* self);
|
|
42 |
|
|
43 |
#define SET_READER_FUNCTIONS(thiz, clazz) (thiz)->read = clazz##_read;\
|
|
44 |
(thiz)->read1 = clazz##_read1;\
|
|
45 |
(thiz)->return_buffer = clazz##_return_buffer;\
|
|
46 |
(thiz)->delete = clazz##_delete;
|
|
47 |
|
|
48 |
typedef struct _Reader {
|
|
49 |
READER_FUNCTIONS;
|
|
50 |
} Reader;
|
|
51 |
|
|
52 |
static Reader* NewBufferReader(PyObject* ob);
|
|
53 |
static Reader* NewObjectReader(PyObject* ob);
|
|
54 |
#if HAS_FILE_READER
|
|
55 |
static Reader* NewFileReader(PyObject* ob);
|
|
56 |
#endif
|
|
57 |
|
|
58 |
|
|
59 |
static PyObject* loads_tag(Reader* rin, uint64_t aux);
|
|
60 |
static int loads_kv(PyObject* out, Reader* rin);
|
|
61 |
|
|
62 |
typedef struct VarBufferPart {
|
|
63 |
void* start;
|
|
64 |
uint64_t len;
|
|
65 |
struct VarBufferPart* next;
|
|
66 |
} VarBufferPart;
|
|
67 |
|
|
68 |
|
|
69 |
static int logprintf(const char* fmt, ...) {
|
|
70 |
va_list ap;
|
|
71 |
int ret;
|
|
72 |
va_start(ap, fmt);
|
|
73 |
#if DEBUG_LOGGING
|
|
74 |
ret = vfprintf(stderr, fmt, ap);
|
|
75 |
#else
|
|
76 |
ret = 0;
|
|
77 |
#endif
|
|
78 |
va_end(ap);
|
|
79 |
return ret;
|
|
80 |
}
|
|
81 |
|
|
82 |
// TODO: portably work this out at compile time
|
|
83 |
static int _is_big_endian = 0;
|
|
84 |
|
|
85 |
static int is_big_endian(void) {
|
|
86 |
uint32_t val = 1234;
|
|
87 |
_is_big_endian = val == htonl(val);
|
|
88 |
//logprintf("is_big_endian=%d\n", _is_big_endian);
|
|
89 |
return _is_big_endian;
|
|
90 |
}
|
|
91 |
|
|
92 |
|
|
93 |
PyObject* decodeFloat16(Reader* rin) {
|
|
94 |
// float16 parsing adapted from example code in spec
|
|
95 |
uint8_t hibyte, lobyte;// = raw[pos];
|
|
96 |
int err;
|
|
97 |
int exp;
|
|
98 |
int mant;
|
|
99 |
double val;
|
|
100 |
|
|
101 |
err = rin->read1(rin, &hibyte);
|
|
102 |
if (err) { logprintf("fail in float16[0]\n"); return NULL; }
|
|
103 |
err = rin->read1(rin, &lobyte);
|
|
104 |
if (err) { logprintf("fail in float16[1]\n"); return NULL; }
|
|
105 |
|
|
106 |
exp = (hibyte >> 2) & 0x1f;
|
|
107 |
mant = ((hibyte & 0x3) << 8) | lobyte;
|
|
108 |
if (exp == 0) {
|
|
109 |
val = ldexp(mant, -24);
|
|
110 |
} else if (exp != 31) {
|
|
111 |
val = ldexp(mant + 1024, exp - 25);
|
|
112 |
} else {
|
|
113 |
val = mant == 0 ? INFINITY : NAN;
|
|
114 |
}
|
|
115 |
if (hibyte & 0x80) {
|
|
116 |
val = -val;
|
|
117 |
}
|
|
118 |
return PyFloat_FromDouble(val);
|
|
119 |
}
|
|
120 |
PyObject* decodeFloat32(Reader* rin) {
|
|
121 |
float val;
|
|
122 |
uint8_t* raw = rin->read(rin, 4);
|
|
123 |
if (!raw) { logprintf("fail in float32\n"); return NULL; }
|
|
124 |
if (_is_big_endian) {
|
|
125 |
// easy!
|
|
126 |
val = *((float*)raw);
|
|
127 |
} else {
|
|
128 |
uint8_t* dest = (uint8_t*)(&val);
|
|
129 |
dest[3] = raw[0];
|
|
130 |
dest[2] = raw[1];
|
|
131 |
dest[1] = raw[2];
|
|
132 |
dest[0] = raw[3];
|
|
133 |
}
|
|
134 |
rin->return_buffer(rin, raw);
|
|
135 |
return PyFloat_FromDouble(val);
|
|
136 |
}
|
|
137 |
PyObject* decodeFloat64(Reader* rin) {
|
|
138 |
int si;
|
|
139 |
uint64_t aux = 0;
|
|
140 |
uint8_t* raw = rin->read(rin, 8);
|
|
141 |
if (!raw) { logprintf("fail in float64\n"); return NULL; }
|
|
142 |
for (si = 0; si < 8; si++) {
|
|
143 |
aux = aux << 8;
|
|
144 |
aux |= raw[si];
|
|
145 |
}
|
|
146 |
rin->return_buffer(rin, raw);
|
|
147 |
return PyFloat_FromDouble(*((double*)(&aux)));
|
|
148 |
}
|
|
149 |
|
|
150 |
// parse following int value into *auxP
|
|
151 |
// return 0 on success, -1 on fail
|
|
152 |
static int handle_info_bits(Reader* rin, uint8_t cbor_info, uint64_t* auxP) {
|
|
153 |
uint64_t aux;
|
|
154 |
|
|
155 |
if (cbor_info <= 23) {
|
|
156 |
// literal value <=23
|
|
157 |
aux = cbor_info;
|
|
158 |
} else if (cbor_info == CBOR_UINT8_FOLLOWS) {
|
|
159 |
uint8_t taux;
|
|
160 |
if (rin->read1(rin, &taux)) { logprintf("fail in uint8\n"); return -1; }
|
|
161 |
aux = taux;
|
|
162 |
} else if (cbor_info == CBOR_UINT16_FOLLOWS) {
|
|
163 |
uint8_t hibyte, lobyte;
|
|
164 |
if (rin->read1(rin, &hibyte)) { logprintf("fail in uint16[0]\n"); return -1; }
|
|
165 |
if (rin->read1(rin, &lobyte)) { logprintf("fail in uint16[1]\n"); return -1; }
|
|
166 |
aux = (hibyte << 8) | lobyte;
|
|
167 |
} else if (cbor_info == CBOR_UINT32_FOLLOWS) {
|
|
168 |
uint8_t* raw = (uint8_t*)rin->read(rin, 4);
|
|
169 |
if (!raw) { logprintf("fail in uint32[1]\n"); return -1; }
|
|
170 |
aux =
|
|
171 |
(((uint64_t)raw[0]) << 24) |
|
|
172 |
(((uint64_t)raw[1]) << 16) |
|
|
173 |
(((uint64_t)raw[2]) << 8) |
|
|
174 |
((uint64_t)raw[3]);
|
|
175 |
rin->return_buffer(rin, raw);
|
|
176 |
} else if (cbor_info == CBOR_UINT64_FOLLOWS) {
|
|
177 |
int si;
|
|
178 |
uint8_t* raw = (uint8_t*)rin->read(rin, 8);
|
|
179 |
if (!raw) { logprintf("fail in uint64[1]\n"); return -1; }
|
|
180 |
aux = 0;
|
|
181 |
for (si = 0; si < 8; si++) {
|
|
182 |
aux = aux << 8;
|
|
183 |
aux |= raw[si];
|
|
184 |
}
|
|
185 |
rin->return_buffer(rin, raw);
|
|
186 |
} else {
|
|
187 |
aux = 0;
|
|
188 |
}
|
|
189 |
*auxP = aux;
|
|
190 |
return 0;
|
|
191 |
}
|
|
192 |
|
|
193 |
static PyObject* inner_loads_c(Reader* rin, uint8_t c);
|
|
194 |
|
|
195 |
static PyObject* inner_loads(Reader* rin) {
|
|
196 |
uint8_t c;
|
|
197 |
int err;
|
|
198 |
|
|
199 |
err = rin->read1(rin, &c);
|
|
200 |
if (err) { logprintf("fail in loads tag\n"); return NULL; }
|
|
201 |
return inner_loads_c(rin, c);
|
|
202 |
}
|
|
203 |
|
|
204 |
PyObject* inner_loads_c(Reader* rin, uint8_t c) {
|
|
205 |
uint8_t cbor_type;
|
|
206 |
uint8_t cbor_info;
|
|
207 |
uint64_t aux;
|
|
208 |
|
|
209 |
cbor_type = c & CBOR_TYPE_MASK;
|
|
210 |
cbor_info = c & CBOR_INFO_BITS;
|
|
211 |
|
|
212 |
#if 0
|
|
213 |
if (pos > len) {
|
|
214 |
PyErr_SetString(PyExc_ValueError, "misparse, token went longer than buffer");
|
|
215 |
return NULL;
|
|
216 |
}
|
|
217 |
|
|
218 |
pos += 1;
|
|
219 |
#endif
|
|
220 |
|
|
221 |
if (cbor_type == CBOR_7) {
|
|
222 |
if (cbor_info == CBOR_UINT16_FOLLOWS) { // float16
|
|
223 |
return decodeFloat16(rin);
|
|
224 |
} else if (cbor_info == CBOR_UINT32_FOLLOWS) { // float32
|
|
225 |
return decodeFloat32(rin);
|
|
226 |
} else if (cbor_info == CBOR_UINT64_FOLLOWS) { // float64
|
|
227 |
return decodeFloat64(rin);
|
|
228 |
}
|
|
229 |
// not a float, fall through to other CBOR_7 interpretations
|
|
230 |
}
|
|
231 |
if (handle_info_bits(rin, cbor_info, &aux)) { logprintf("info bits failed\n"); return NULL; }
|
|
232 |
|
|
233 |
PyObject* out = NULL;
|
|
234 |
switch (cbor_type) {
|
|
235 |
case CBOR_UINT:
|
|
236 |
out = PyLong_FromUnsignedLongLong(aux);
|
|
237 |
if (out == NULL) {
|
|
238 |
PyErr_SetString(PyExc_RuntimeError, "unknown error decoding UINT");
|
|
239 |
}
|
|
240 |
return out;
|
|
241 |
case CBOR_NEGINT:
|
|
242 |
if (aux > 0x7fffffffffffffff) {
|
|
243 |
PyObject* bignum = PyLong_FromUnsignedLongLong(aux);
|
|
244 |
PyObject* minusOne = PyLong_FromLong(-1);
|
|
245 |
out = PyNumber_Subtract(minusOne, bignum);
|
|
246 |
Py_DECREF(minusOne);
|
|
247 |
Py_DECREF(bignum);
|
|
248 |
} else {
|
|
249 |
out = PyLong_FromLongLong((long long)(((long long)-1) - aux));
|
|
250 |
}
|
|
251 |
if (out == NULL) {
|
|
252 |
PyErr_SetString(PyExc_RuntimeError, "unknown error decoding NEGINT");
|
|
253 |
}
|
|
254 |
return out;
|
|
255 |
case CBOR_BYTES:
|
|
256 |
if (cbor_info == CBOR_VAR_FOLLOWS) {
|
|
257 |
size_t total = 0;
|
|
258 |
VarBufferPart* parts = NULL;
|
|
259 |
VarBufferPart* parts_tail = NULL;
|
|
260 |
uint8_t sc;
|
|
261 |
if (rin->read1(rin, &sc)) { logprintf("r1 fail in var bytes tag\n"); return NULL; }
|
|
262 |
while (sc != CBOR_BREAK) {
|
|
263 |
uint8_t scbor_type = sc & CBOR_TYPE_MASK;
|
|
264 |
uint8_t scbor_info = sc & CBOR_INFO_BITS;
|
|
265 |
uint64_t saux;
|
|
266 |
void* blob;
|
|
267 |
|
|
268 |
if (scbor_type != CBOR_BYTES) {
|
|
269 |
PyErr_Format(PyExc_ValueError, "expected subordinate BYTES block under VAR BYTES, but got %x", scbor_type);
|
|
270 |
return NULL;
|
|
271 |
}
|
|
272 |
if(handle_info_bits(rin, scbor_info, &saux)) { logprintf("var bytes sub infobits failed\n"); return NULL; }
|
|
273 |
blob = rin->read(rin, saux);
|
|
274 |
if (!blob) { logprintf("var bytes sub bytes read failed\n"); return NULL; }
|
|
275 |
if (parts_tail == NULL) {
|
|
276 |
parts = parts_tail = (VarBufferPart*)PyMem_Malloc(sizeof(VarBufferPart) + saux);
|
|
277 |
} else {
|
|
278 |
parts_tail->next = (VarBufferPart*)PyMem_Malloc(sizeof(VarBufferPart) + saux);
|
|
279 |
parts_tail = parts_tail->next;
|
|
280 |
}
|
|
281 |
parts_tail->start = (void*)(parts_tail + 1);
|
|
282 |
memcpy(parts_tail->start, blob, saux);
|
|
283 |
rin->return_buffer(rin, blob);
|
|
284 |
parts_tail->len = saux;
|
|
285 |
parts_tail->next = NULL;
|
|
286 |
total += saux;
|
|
287 |
if (rin->read1(rin, &sc)) { logprintf("r1 fail in var bytes tag\n"); return NULL; }
|
|
288 |
}
|
|
289 |
// Done
|
|
290 |
{
|
|
291 |
uint8_t* allbytes = (uint8_t*)PyMem_Malloc(total);
|
|
292 |
uintptr_t op = 0;
|
|
293 |
while (parts != NULL) {
|
|
294 |
VarBufferPart* next;
|
|
295 |
memcpy(allbytes + op, parts->start, parts->len);
|
|
296 |
op += parts->len;
|
|
297 |
next = parts->next;
|
|
298 |
PyMem_Free(parts);
|
|
299 |
parts = next;
|
|
300 |
}
|
|
301 |
out = PyBytes_FromStringAndSize((char*)allbytes, total);
|
|
302 |
PyMem_Free(allbytes);
|
|
303 |
}
|
|
304 |
if (out == NULL) {
|
|
305 |
PyErr_SetString(PyExc_RuntimeError, "unknown error decoding VAR BYTES");
|
|
306 |
}
|
|
307 |
} else {
|
|
308 |
void* raw;
|
|
309 |
if (aux == 0) {
|
|
310 |
static void* empty_string = "";
|
|
311 |
raw = empty_string;
|
|
312 |
} else {
|
|
313 |
raw = rin->read(rin, aux);
|
|
314 |
if (!raw) { logprintf("bytes read failed\n"); return NULL; }
|
|
315 |
}
|
|
316 |
out = PyBytes_FromStringAndSize(raw, (Py_ssize_t)aux);
|
|
317 |
if (out == NULL) {
|
|
318 |
PyErr_SetString(PyExc_RuntimeError, "unknown error decoding BYTES");
|
|
319 |
}
|
|
320 |
if (aux != 0) {
|
|
321 |
rin->return_buffer(rin, raw);
|
|
322 |
}
|
|
323 |
}
|
|
324 |
return out;
|
|
325 |
case CBOR_TEXT:
|
|
326 |
if (cbor_info == CBOR_VAR_FOLLOWS) {
|
|
327 |
PyObject* parts = PyList_New(0);
|
|
328 |
PyObject* joiner = PyUnicode_FromString("");
|
|
329 |
uint8_t sc;
|
|
330 |
if (rin->read1(rin, &sc)) { logprintf("r1 fail in var text tag\n"); return NULL; }
|
|
331 |
while (sc != CBOR_BREAK) {
|
|
332 |
PyObject* subitem = inner_loads_c(rin, sc);
|
|
333 |
if (subitem == NULL) { logprintf("fail in var text subitem\n"); return NULL; }
|
|
334 |
PyList_Append(parts, subitem);
|
|
335 |
Py_DECREF(subitem);
|
|
336 |
if (rin->read1(rin, &sc)) { logprintf("r1 fail in var text tag\n"); return NULL; }
|
|
337 |
}
|
|
338 |
// Done
|
|
339 |
out = PyUnicode_Join(joiner, parts);
|
|
340 |
Py_DECREF(joiner);
|
|
341 |
Py_DECREF(parts);
|
|
342 |
if (out == NULL) {
|
|
343 |
PyErr_SetString(PyExc_RuntimeError, "unknown error decoding VAR TEXT");
|
|
344 |
}
|
|
345 |
} else {
|
|
346 |
void* raw;
|
|
347 |
if (aux == 0) {
|
|
348 |
static void* empty_string = "";
|
|
349 |
raw = empty_string;
|
|
350 |
} else {
|
|
351 |
raw = rin->read(rin, aux);
|
|
352 |
if (!raw) { logprintf("read text failed\n"); return NULL; }
|
|
353 |
}
|
|
354 |
out = PyUnicode_FromStringAndSize((char*)raw, (Py_ssize_t)aux);
|
|
355 |
if (out == NULL) {
|
|
356 |
PyErr_SetString(PyExc_RuntimeError, "unknown error decoding TEXT");
|
|
357 |
}
|
|
358 |
if (aux != 0) {
|
|
359 |
rin->return_buffer(rin, raw);
|
|
360 |
}
|
|
361 |
}
|
|
362 |
return out;
|
|
363 |
case CBOR_ARRAY:
|
|
364 |
if (cbor_info == CBOR_VAR_FOLLOWS) {
|
|
365 |
uint8_t sc;
|
|
366 |
out = PyList_New(0);
|
|
367 |
if (rin->read1(rin, &sc)) { logprintf("r1 fail in var array tag\n"); return NULL; }
|
|
368 |
while (sc != CBOR_BREAK) {
|
|
369 |
PyObject* subitem = inner_loads_c(rin, sc);
|
|
370 |
if (subitem == NULL) { logprintf("fail in var array subitem\n"); return NULL; }
|
|
371 |
PyList_Append(out, subitem);
|
|
372 |
Py_DECREF(subitem);
|
|
373 |
if (rin->read1(rin, &sc)) { logprintf("r1 fail in var array tag\n"); return NULL; }
|
|
374 |
}
|
|
375 |
// Done
|
|
376 |
if (out == NULL) {
|
|
377 |
PyErr_SetString(PyExc_RuntimeError, "unknown error decoding VAR ARRAY");
|
|
378 |
}
|
|
379 |
} else {
|
|
380 |
unsigned int i;
|
|
381 |
out = PyList_New((Py_ssize_t)aux);
|
|
382 |
for (i = 0; i < aux; i++) {
|
|
383 |
PyObject* subitem = inner_loads(rin);
|
|
384 |
if (subitem == NULL) { logprintf("array subitem[%d] (of %d) failed\n", i, aux); return NULL; }
|
|
385 |
PyList_SetItem(out, (Py_ssize_t)i, subitem);
|
|
386 |
// PyList_SetItem became the owner of the reference count of subitem, we don't need to DECREF it
|
|
387 |
}
|
|
388 |
if (out == NULL) {
|
|
389 |
PyErr_SetString(PyExc_RuntimeError, "unknown error decoding ARRAY");
|
|
390 |
}
|
|
391 |
}
|
|
392 |
return out;
|
|
393 |
case CBOR_MAP:
|
|
394 |
out = PyDict_New();
|
|
395 |
if (cbor_info == CBOR_VAR_FOLLOWS) {
|
|
396 |
uint8_t sc;
|
|
397 |
if (rin->read1(rin, &sc)) { logprintf("r1 fail in var map tag\n"); return NULL; }
|
|
398 |
while (sc != CBOR_BREAK) {
|
|
399 |
PyObject* key = inner_loads_c(rin, sc);
|
|
400 |
PyObject* value;
|
|
401 |
if (key == NULL) { logprintf("var map key fail\n"); return NULL; }
|
|
402 |
value = inner_loads(rin);
|
|
403 |
if (value == NULL) { logprintf("var map val vail\n"); return NULL; }
|
|
404 |
PyDict_SetItem(out, key, value);
|
|
405 |
Py_DECREF(key);
|
|
406 |
Py_DECREF(value);
|
|
407 |
|
|
408 |
if (rin->read1(rin, &sc)) { logprintf("r1 fail in var map tag\n"); return NULL; }
|
|
409 |
}
|
|
410 |
if (out == NULL) {
|
|
411 |
PyErr_SetString(PyExc_RuntimeError, "unknown error decoding VAR MAP");
|
|
412 |
}
|
|
413 |
} else {
|
|
414 |
unsigned int i;
|
|
415 |
for (i = 0; i < aux; i++) {
|
|
416 |
if (loads_kv(out, rin) != 0) {
|
|
417 |
logprintf("map kv[%d] failed\n", i);
|
|
418 |
return NULL;
|
|
419 |
}
|
|
420 |
}
|
|
421 |
if (out == NULL) {
|
|
422 |
PyErr_SetString(PyExc_RuntimeError, "unknown error decoding MAP");
|
|
423 |
}
|
|
424 |
}
|
|
425 |
return out;
|
|
426 |
case CBOR_TAG:
|
|
427 |
return loads_tag(rin, aux);
|
|
428 |
case CBOR_7:
|
|
429 |
if (aux == 20) {
|
|
430 |
out = Py_False;
|
|
431 |
Py_INCREF(out);
|
|
432 |
} else if (aux == 21) {
|
|
433 |
out = Py_True;
|
|
434 |
Py_INCREF(out);
|
|
435 |
} else if (aux == 22) {
|
|
436 |
out = Py_None;
|
|
437 |
Py_INCREF(out);
|
|
438 |
} else if (aux == 23) {
|
|
439 |
// js `undefined`, closest is py None
|
|
440 |
out = Py_None;
|
|
441 |
Py_INCREF(out);
|
|
442 |
}
|
|
443 |
if (out == NULL) {
|
|
444 |
PyErr_Format(PyExc_ValueError, "unknown section 7 marker %02x, aux=%llu", c, aux);
|
|
445 |
}
|
|
446 |
return out;
|
|
447 |
default:
|
|
448 |
PyErr_Format(PyExc_RuntimeError, "unknown cbor marker %02x", c);
|
|
449 |
return NULL;
|
|
450 |
}
|
|
451 |
PyErr_SetString(PyExc_RuntimeError, "cbor library internal error moof!");
|
|
452 |
return NULL;
|
|
453 |
}
|
|
454 |
|
|
455 |
static int loads_kv(PyObject* out, Reader* rin) {
|
|
456 |
PyObject* key = inner_loads(rin);
|
|
457 |
PyObject* value;
|
|
458 |
if (key == NULL) { logprintf("map key fail\n"); return -1; }
|
|
459 |
value = inner_loads(rin);
|
|
460 |
if (value == NULL) { logprintf("map val fail\n"); return -1; }
|
|
461 |
PyDict_SetItem(out, key, value);
|
|
462 |
Py_DECREF(key);
|
|
463 |
Py_DECREF(value);
|
|
464 |
return 0;
|
|
465 |
}
|
|
466 |
|
|
467 |
static PyObject* loads_bignum(Reader* rin, uint8_t c) {
|
|
468 |
PyObject* out = NULL;
|
|
469 |
|
|
470 |
uint8_t bytes_info = c & CBOR_INFO_BITS;
|
|
471 |
if (bytes_info < 24) {
|
|
472 |
int i;
|
|
473 |
PyObject* eight = PyLong_FromLong(8);
|
|
474 |
out = PyLong_FromLong(0);
|
|
475 |
for (i = 0; i < bytes_info; i++) {
|
|
476 |
// TODO: is this leaking like crazy?
|
|
477 |
PyObject* curbyte;
|
|
478 |
PyObject* tout = PyNumber_Lshift(out, eight);
|
|
479 |
Py_DECREF(out);
|
|
480 |
out = tout;
|
|
481 |
uint8_t cb;
|
|
482 |
if (rin->read1(rin, &cb)) {
|
|
483 |
logprintf("r1 fail in bignum %d/%d\n", i, bytes_info);
|
|
484 |
Py_DECREF(eight);
|
|
485 |
Py_DECREF(out);
|
|
486 |
return NULL;
|
|
487 |
}
|
|
488 |
curbyte = PyLong_FromLong(cb);
|
|
489 |
tout = PyNumber_Or(out, curbyte);
|
|
490 |
Py_DECREF(curbyte);
|
|
491 |
Py_DECREF(out);
|
|
492 |
out = tout;
|
|
493 |
}
|
|
494 |
Py_DECREF(eight);
|
|
495 |
return out;
|
|
496 |
} else {
|
|
497 |
PyErr_Format(PyExc_NotImplementedError, "TODO: TAG BIGNUM for bigger bignum bytes_info=%d, len(ull)=%lu\n", bytes_info, sizeof(unsigned long long));
|
|
498 |
return NULL;
|
|
499 |
}
|
|
500 |
}
|
|
501 |
|
|
502 |
|
|
503 |
// returns a PyObject for cbor.cbor.Tag
|
|
504 |
// Returned PyObject* is a BORROWED reference from the module dict
|
|
505 |
static PyObject* getCborTagClass(void) {
|
|
506 |
PyObject* cbor_module = PyImport_ImportModule("cbor.cbor");
|
|
507 |
PyObject* moddict = PyModule_GetDict(cbor_module);
|
|
508 |
PyObject* tag_class = PyDict_GetItemString(moddict, "Tag");
|
|
509 |
// moddict and tag_class are 'borrowed reference'
|
|
510 |
Py_DECREF(cbor_module);
|
|
511 |
|
|
512 |
return tag_class;
|
|
513 |
}
|
|
514 |
|
|
515 |
|
|
516 |
static PyObject* loads_tag(Reader* rin, uint64_t aux) {
|
|
517 |
PyObject* out = NULL;
|
|
518 |
// return an object CBORTag(tagnum, nextob)
|
|
519 |
if (aux == CBOR_TAG_BIGNUM) {
|
|
520 |
// If the next object is bytes, interpret it here without making a PyObject for it.
|
|
521 |
uint8_t sc;
|
|
522 |
if (rin->read1(rin, &sc)) { logprintf("r1 fail in bignum tag\n"); return NULL; }
|
|
523 |
if ((sc & CBOR_TYPE_MASK) == CBOR_BYTES) {
|
|
524 |
return loads_bignum(rin, sc);
|
|
525 |
} else {
|
|
526 |
PyErr_Format(PyExc_ValueError, "TAG BIGNUM not followed by bytes but %02x", sc);
|
|
527 |
return NULL;
|
|
528 |
}
|
|
529 |
PyErr_Format(PyExc_ValueError, "TODO: WRITEME CBOR TAG BIGNUM %02x ...\n", sc);
|
|
530 |
return NULL;
|
|
531 |
} else if (aux == CBOR_TAG_NEGBIGNUM) {
|
|
532 |
// If the next object is bytes, interpret it here without making a PyObject for it.
|
|
533 |
uint8_t sc;
|
|
534 |
if (rin->read1(rin, &sc)) { logprintf("r1 fail in negbignum tag\n"); return NULL; }
|
|
535 |
if ((sc & CBOR_TYPE_MASK) == CBOR_BYTES) {
|
|
536 |
out = loads_bignum(rin, sc);
|
|
537 |
if (out == NULL) { logprintf("loads_bignum fail inside TAG_NEGBIGNUM\n"); return NULL; }
|
|
538 |
PyObject* minusOne = PyLong_FromLong(-1);
|
|
539 |
PyObject* tout = PyNumber_Subtract(minusOne, out);
|
|
540 |
Py_DECREF(minusOne);
|
|
541 |
Py_DECREF(out);
|
|
542 |
out = tout;
|
|
543 |
return out;
|
|
544 |
} else {
|
|
545 |
PyErr_Format(PyExc_ValueError, "TAG NEGBIGNUM not followed by bytes but %02x", sc);
|
|
546 |
return NULL;
|
|
547 |
}
|
|
548 |
PyErr_Format(PyExc_ValueError, "TODO: WRITEME CBOR TAG NEGBIGNUM %02x ...\n", sc);
|
|
549 |
return NULL;
|
|
550 |
}
|
|
551 |
out = inner_loads(rin);
|
|
552 |
if (out == NULL) { return NULL; }
|
|
553 |
{
|
|
554 |
PyObject* tag_class = getCborTagClass();
|
|
555 |
PyObject* args = Py_BuildValue("(K,O)", aux, out);
|
|
556 |
PyObject* tout = PyObject_CallObject(tag_class, args);
|
|
557 |
Py_DECREF(args);
|
|
558 |
Py_DECREF(out);
|
|
559 |
// tag_class was just a borrowed reference
|
|
560 |
out = tout;
|
|
561 |
}
|
|
562 |
return out;
|
|
563 |
}
|
|
564 |
|
|
565 |
|
|
566 |
static PyObject*
|
|
567 |
cbor_loads(PyObject* noself, PyObject* args) {
|
|
568 |
PyObject* ob;
|
|
569 |
is_big_endian();
|
|
570 |
if (PyType_IsSubtype(Py_TYPE(args), &PyList_Type)) {
|
|
571 |
ob = PyList_GetItem(args, 0);
|
|
572 |
} else if (PyType_IsSubtype(Py_TYPE(args), &PyTuple_Type)) {
|
|
573 |
ob = PyTuple_GetItem(args, 0);
|
|
574 |
} else {
|
|
575 |
PyErr_Format(PyExc_ValueError, "args not list or tuple: %R\n", args);
|
|
576 |
return NULL;
|
|
577 |
}
|
|
578 |
|
|
579 |
if (ob == Py_None) {
|
|
580 |
PyErr_SetString(PyExc_ValueError, "got None for buffer to decode in loads");
|
|
581 |
return NULL;
|
|
582 |
}
|
|
583 |
|
|
584 |
{
|
|
585 |
PyObject* out = NULL;
|
|
586 |
Reader* r = NewBufferReader(ob);
|
|
587 |
if (!r) {
|
|
588 |
return NULL;
|
|
589 |
}
|
|
590 |
out = inner_loads(r);
|
|
591 |
r->delete(r);
|
|
592 |
return out;
|
|
593 |
}
|
|
594 |
}
|
|
595 |
|
|
596 |
|
|
597 |
#if HAS_FILE_READER
|
|
598 |
|
|
599 |
typedef struct _FileReader {
|
|
600 |
READER_FUNCTIONS;
|
|
601 |
FILE* fin;
|
|
602 |
void* dst;
|
|
603 |
Py_ssize_t dst_size;
|
|
604 |
Py_ssize_t read_count;
|
|
605 |
} FileReader;
|
|
606 |
|
|
607 |
// read from a python builtin file which contains a C FILE*
|
|
608 |
static void* FileReader_read(void* self, Py_ssize_t len) {
|
|
609 |
FileReader* thiz = (FileReader*)self;
|
|
610 |
Py_ssize_t rtotal = 0;
|
|
611 |
uintptr_t opos;
|
|
612 |
//logprintf("file read %d\n", len);
|
|
613 |
if (len > thiz->dst_size) {
|
|
614 |
thiz->dst = PyMem_Realloc(thiz->dst, len);
|
|
615 |
thiz->dst_size = len;
|
|
616 |
} else if ((thiz->dst_size > (128 * 1024)) && (len < 4096)) {
|
|
617 |
PyMem_Free(thiz->dst);
|
|
618 |
thiz->dst = PyMem_Malloc(len);
|
|
619 |
thiz->dst_size = len;
|
|
620 |
}
|
|
621 |
opos = (uintptr_t)(thiz->dst);
|
|
622 |
while (1) {
|
|
623 |
size_t rlen = fread((void*)opos, 1, len, thiz->fin);
|
|
624 |
if (rlen == 0) {
|
|
625 |
// file isn't going to give any more
|
|
626 |
PyErr_Format(PyExc_ValueError, "only got %zd bytes with %zd stil to read from file", rtotal, len);
|
|
627 |
PyMem_Free(thiz->dst);
|
|
628 |
thiz->dst = NULL;
|
|
629 |
thiz->dst_size = 0;
|
|
630 |
return NULL;
|
|
631 |
}
|
|
632 |
thiz->read_count += rlen;
|
|
633 |
rtotal += rlen;
|
|
634 |
opos += rlen;
|
|
635 |
len -= rlen;
|
|
636 |
if (rtotal >= len) {
|
|
637 |
if (thiz->dst == NULL) {
|
|
638 |
PyErr_SetString(PyExc_RuntimeError, "known error in file reader, NULL dst");
|
|
639 |
return NULL;
|
|
640 |
}
|
|
641 |
return thiz->dst;
|
|
642 |
}
|
|
643 |
}
|
|
644 |
}
|
|
645 |
static int FileReader_read1(void* self, uint8_t* oneByte) {
|
|
646 |
FileReader* thiz = (FileReader*)self;
|
|
647 |
size_t didread = fread((void*)oneByte, 1, 1, thiz->fin);
|
|
648 |
if (didread == 0) {
|
|
649 |
logprintf("failed to read 1 from file\n");
|
|
650 |
PyErr_SetString(PyExc_ValueError, "got nothing reading 1 from file");
|
|
651 |
return -1;
|
|
652 |
}
|
|
653 |
thiz->read_count++;
|
|
654 |
return 0;
|
|
655 |
}
|
|
656 |
static void FileReader_return_buffer(void* self, void* buffer) {
|
|
657 |
// Nothing to do, we hold onto the buffer and maybe reuse it for next read
|
|
658 |
}
|
|
659 |
static void FileReader_delete(void* self) {
|
|
660 |
FileReader* thiz = (FileReader*)self;
|
|
661 |
if (thiz->dst) {
|
|
662 |
PyMem_Free(thiz->dst);
|
|
663 |
}
|
|
664 |
PyMem_Free(thiz);
|
|
665 |
}
|
|
666 |
static Reader* NewFileReader(PyObject* ob) {
|
|
667 |
FileReader* fr = (FileReader*)PyMem_Malloc(sizeof(FileReader));
|
|
668 |
if (fr == NULL) {
|
|
669 |
PyErr_SetString(PyExc_MemoryError, "failed to allocate FileReader");
|
|
670 |
return NULL;
|
|
671 |
}
|
|
672 |
fr->fin = PyFile_AsFile(ob);
|
|
673 |
if (fr->fin == NULL) {
|
|
674 |
PyErr_SetString(PyExc_RuntimeError, "PyFile_AsFile NULL");
|
|
675 |
PyMem_Free(fr);
|
|
676 |
return NULL;
|
|
677 |
}
|
|
678 |
fr->dst = NULL;
|
|
679 |
fr->dst_size = 0;
|
|
680 |
fr->read_count = 0;
|
|
681 |
SET_READER_FUNCTIONS(fr, FileReader);
|
|
682 |
return (Reader*)fr;
|
|
683 |
}
|
|
684 |
|
|
685 |
#endif /* Python 2.7 FileReader */
|
|
686 |
|
|
687 |
|
|
688 |
typedef struct _ObjectReader {
|
|
689 |
READER_FUNCTIONS;
|
|
690 |
PyObject* ob;
|
|
691 |
|
|
692 |
// We got one object with all the bytes neccessary, and need to
|
|
693 |
// DECREF it later.
|
|
694 |
PyObject* retval;
|
|
695 |
void* bytes;
|
|
696 |
|
|
697 |
// OR, we got several objects, we DECREFed them as we went, and
|
|
698 |
// need to Free() this buffer at the end.
|
|
699 |
void* dst;
|
|
700 |
|
|
701 |
Py_ssize_t read_count;
|
|
702 |
int exception_is_external;
|
|
703 |
} ObjectReader;
|
|
704 |
|
|
705 |
// read from a python file-like object which has a .read(n) method
|
|
706 |
static void* ObjectReader_read(void* context, Py_ssize_t len) {
|
|
707 |
ObjectReader* thiz = (ObjectReader*)context;
|
|
708 |
Py_ssize_t rtotal = 0;
|
|
709 |
uintptr_t opos = 0;
|
|
710 |
//logprintf("ob read %d\n", len);
|
|
711 |
assert(!thiz->dst);
|
|
712 |
assert(!thiz->bytes);
|
|
713 |
while (rtotal < len) {
|
|
714 |
PyObject* retval = PyObject_CallMethod(thiz->ob, "read", "n", len - rtotal, NULL);
|
|
715 |
Py_ssize_t rlen;
|
|
716 |
if (retval == NULL) {
|
|
717 |
thiz->exception_is_external = 1;
|
|
718 |
logprintf("exception in object.read()\n");
|
|
719 |
return NULL;
|
|
720 |
}
|
|
721 |
if (!PyBytes_Check(retval)) {
|
|
722 |
logprintf("object.read() is not bytes\n");
|
|
723 |
PyErr_SetString(PyExc_ValueError, "expected ob.read() to return a bytes object\n");
|
|
724 |
Py_DECREF(retval);
|
|
725 |
return NULL;
|
|
726 |
}
|
|
727 |
rlen = PyBytes_Size(retval);
|
|
728 |
thiz->read_count += rlen;
|
|
729 |
if (rlen > len - rtotal) {
|
|
730 |
logprintf("object.read() is too much!\n");
|
|
731 |
PyErr_Format(PyExc_ValueError, "ob.read() returned %ld bytes but only wanted %lu\n", rlen, len - rtotal);
|
|
732 |
Py_DECREF(retval);
|
|
733 |
return NULL;
|
|
734 |
}
|
|
735 |
if (rlen == len) {
|
|
736 |
// best case! All in one call to read()
|
|
737 |
// We _keep_ a reference to retval until later.
|
|
738 |
thiz->retval = retval;
|
|
739 |
thiz->bytes = PyBytes_AsString(retval);
|
|
740 |
assert(thiz->bytes);
|
|
741 |
thiz->dst = NULL;
|
|
742 |
opos = 0;
|
|
743 |
return thiz->bytes;
|
|
744 |
}
|
|
745 |
if (thiz->dst == NULL) {
|
|
746 |
thiz->dst = PyMem_Malloc(len);
|
|
747 |
opos = (uintptr_t)thiz->dst;
|
|
748 |
}
|
|
749 |
// else, not enough all in one go
|
|
750 |
memcpy((void*)opos, PyBytes_AsString(retval), rlen);
|
|
751 |
Py_DECREF(retval);
|
|
752 |
opos += rlen;
|
|
753 |
rtotal += rlen;
|
|
754 |
}
|
|
755 |
assert(thiz->dst);
|
|
756 |
return thiz->dst;
|
|
757 |
}
|
|
758 |
static int ObjectReader_read1(void* self, uint8_t* oneByte) {
|
|
759 |
ObjectReader* thiz = (ObjectReader*)self;
|
|
760 |
PyObject* retval = PyObject_CallMethod(thiz->ob, "read", "i", 1, NULL);
|
|
761 |
Py_ssize_t rlen;
|
|
762 |
if (retval == NULL) {
|
|
763 |
thiz->exception_is_external = 1;
|
|
764 |
//logprintf("call ob read(1) failed\n");
|
|
765 |
return -1;
|
|
766 |
}
|
|
767 |
if (!PyBytes_Check(retval)) {
|
|
768 |
PyErr_SetString(PyExc_ValueError, "expected ob.read() to return a bytes object\n");
|
|
769 |
return -1;
|
|
770 |
}
|
|
771 |
rlen = PyBytes_Size(retval);
|
|
772 |
thiz->read_count += rlen;
|
|
773 |
if (rlen > 1) {
|
|
774 |
PyErr_Format(PyExc_ValueError, "TODO: raise exception: WAT ob.read() returned %ld bytes but only wanted 1\n", rlen);
|
|
775 |
return -1;
|
|
776 |
}
|
|
777 |
if (rlen == 1) {
|
|
778 |
*oneByte = PyBytes_AsString(retval)[0];
|
|
779 |
Py_DECREF(retval);
|
|
780 |
return 0;
|
|
781 |
}
|
|
782 |
PyErr_SetString(PyExc_ValueError, "got nothing reading 1");
|
|
783 |
return -1;
|
|
784 |
}
|
|
785 |
static void ObjectReader_return_buffer(void* context, void* buffer) {
|
|
786 |
ObjectReader* thiz = (ObjectReader*)context;
|
|
787 |
if (buffer == thiz->bytes) {
|
|
788 |
Py_DECREF(thiz->retval);
|
|
789 |
thiz->retval = NULL;
|
|
790 |
thiz->bytes = NULL;
|
|
791 |
} else if (buffer == thiz->dst) {
|
|
792 |
PyMem_Free(thiz->dst);
|
|
793 |
thiz->dst = NULL;
|
|
794 |
} else {
|
|
795 |
logprintf("TODO: raise exception, could not release buffer %p, wanted dst=%p or bytes=%p\n", buffer, thiz->dst, thiz->bytes);
|
|
796 |
}
|
|
797 |
}
|
|
798 |
static void ObjectReader_delete(void* context) {
|
|
799 |
ObjectReader* thiz = (ObjectReader*)context;
|
|
800 |
if (thiz->retval != NULL) {
|
|
801 |
Py_DECREF(thiz->retval);
|
|
802 |
}
|
|
803 |
if (thiz->dst != NULL) {
|
|
804 |
PyMem_Free(thiz->dst);
|
|
805 |
}
|
|
806 |
PyMem_Free(thiz);
|
|
807 |
}
|
|
808 |
static Reader* NewObjectReader(PyObject* ob) {
|
|
809 |
ObjectReader* r = (ObjectReader*)PyMem_Malloc(sizeof(ObjectReader));
|
|
810 |
r->ob = ob;
|
|
811 |
r->retval = NULL;
|
|
812 |
r->bytes = NULL;
|
|
813 |
r->dst = NULL;
|
|
814 |
r->read_count = 0;
|
|
815 |
r->exception_is_external = 0;
|
|
816 |
SET_READER_FUNCTIONS(r, ObjectReader);
|
|
817 |
return (Reader*)r;
|
|
818 |
}
|
|
819 |
|
|
820 |
typedef struct _BufferReader {
|
|
821 |
READER_FUNCTIONS;
|
|
822 |
uint8_t* raw;
|
|
823 |
Py_ssize_t len;
|
|
824 |
uintptr_t pos;
|
|
825 |
} BufferReader;
|
|
826 |
|
|
827 |
// read from a buffer, aka loads()
|
|
828 |
static void* BufferReader_read(void* context, Py_ssize_t len) {
|
|
829 |
BufferReader* thiz = (BufferReader*)context;
|
|
830 |
//logprintf("br %p %d (%d)\n", thiz, len, thiz->len);
|
|
831 |
if (len <= thiz->len) {
|
|
832 |
void* out = (void*)thiz->pos;
|
|
833 |
thiz->pos += len;
|
|
834 |
thiz->len -= len;
|
|
835 |
assert(out);
|
|
836 |
return out;
|
|
837 |
}
|
|
838 |
PyErr_Format(PyExc_ValueError, "buffer read for %zd but only have %zd\n", len, thiz->len);
|
|
839 |
return NULL;
|
|
840 |
}
|
|
841 |
static int BufferReader_read1(void* self, uint8_t* oneByte) {
|
|
842 |
BufferReader* thiz = (BufferReader*)self;
|
|
843 |
//logprintf("br %p _1_ (%d)\n", thiz, thiz->len);
|
|
844 |
if (thiz->len <= 0) {
|
|
845 |
PyErr_SetString(PyExc_LookupError, "buffer exhausted");
|
|
846 |
return -1;
|
|
847 |
}
|
|
848 |
*oneByte = *((uint8_t*)thiz->pos);
|
|
849 |
thiz->pos += 1;
|
|
850 |
thiz->len -= 1;
|
|
851 |
return 0;
|
|
852 |
}
|
|
853 |
static void BufferReader_return_buffer(void* context, void* buffer) {
|
|
854 |
// nothing to do
|
|
855 |
}
|
|
856 |
static void BufferReader_delete(void* context) {
|
|
857 |
BufferReader* thiz = (BufferReader*)context;
|
|
858 |
PyMem_Free(thiz);
|
|
859 |
}
|
|
860 |
static Reader* NewBufferReader(PyObject* ob) {
|
|
861 |
BufferReader* r = (BufferReader*)PyMem_Malloc(sizeof(BufferReader));
|
|
862 |
SET_READER_FUNCTIONS(r, BufferReader);
|
|
863 |
if (PyByteArray_Check(ob)) {
|
|
864 |
r->raw = (uint8_t*)PyByteArray_AsString(ob);
|
|
865 |
r->len = PyByteArray_Size(ob);
|
|
866 |
} else if (PyBytes_Check(ob)) {
|
|
867 |
r->raw = (uint8_t*)PyBytes_AsString(ob);
|
|
868 |
r->len = PyBytes_Size(ob);
|
|
869 |
} else {
|
|
870 |
PyErr_SetString(PyExc_ValueError, "input of unknown type not bytes or bytearray");
|
|
871 |
return NULL;
|
|
872 |
}
|
|
873 |
r->pos = (uintptr_t)r->raw;
|
|
874 |
if (r->len == 0) {
|
|
875 |
PyErr_SetString(PyExc_ValueError, "got zero length string in loads");
|
|
876 |
return NULL;
|
|
877 |
}
|
|
878 |
if (r->raw == NULL) {
|
|
879 |
PyErr_SetString(PyExc_ValueError, "got NULL buffer for string");
|
|
880 |
return NULL;
|
|
881 |
}
|
|
882 |
//logprintf("NBR(%llu, %ld)\n", r->pos, r->len);
|
|
883 |
return (Reader*)r;
|
|
884 |
}
|
|
885 |
|
|
886 |
|
|
887 |
static PyObject*
|
|
888 |
cbor_load(PyObject* noself, PyObject* args) {
|
|
889 |
PyObject* ob;
|
|
890 |
Reader* reader;
|
|
891 |
is_big_endian();
|
|
892 |
if (PyType_IsSubtype(Py_TYPE(args), &PyList_Type)) {
|
|
893 |
ob = PyList_GetItem(args, 0);
|
|
894 |
} else if (PyType_IsSubtype(Py_TYPE(args), &PyTuple_Type)) {
|
|
895 |
ob = PyTuple_GetItem(args, 0);
|
|
896 |
} else {
|
|
897 |
PyErr_Format(PyExc_ValueError, "args not list or tuple: %R\n", args);
|
|
898 |
return NULL;
|
|
899 |
}
|
|
900 |
|
|
901 |
if (ob == Py_None) {
|
|
902 |
PyErr_SetString(PyExc_ValueError, "got None for buffer to decode in loads");
|
|
903 |
return NULL;
|
|
904 |
}
|
|
905 |
PyObject* retval;
|
|
906 |
#if HAS_FILE_READER
|
|
907 |
if (PyFile_Check(ob)) {
|
|
908 |
reader = NewFileReader(ob);
|
|
909 |
if (reader == NULL) { return NULL; }
|
|
910 |
retval = inner_loads(reader);
|
|
911 |
if ((retval == NULL) &&
|
|
912 |
(((FileReader*)reader)->read_count == 0) &&
|
|
913 |
(feof(((FileReader*)reader)->fin) != 0)) {
|
|
914 |
// never got anything, started at EOF
|
|
915 |
PyErr_Clear();
|
|
916 |
PyErr_SetString(PyExc_EOFError, "read nothing, apparent EOF");
|
|
917 |
}
|
|
918 |
reader->delete(reader);
|
|
919 |
} else
|
|
920 |
#endif
|
|
921 |
{
|
|
922 |
reader = NewObjectReader(ob);
|
|
923 |
retval = inner_loads(reader);
|
|
924 |
if ((retval == NULL) &&
|
|
925 |
(!((ObjectReader*)reader)->exception_is_external) &&
|
|
926 |
((ObjectReader*)reader)->read_count == 0) {
|
|
927 |
// never got anything, assume EOF
|
|
928 |
PyErr_Clear();
|
|
929 |
PyErr_SetString(PyExc_EOFError, "read nothing, apparent EOF");
|
|
930 |
}
|
|
931 |
reader->delete(reader);
|
|
932 |
}
|
|
933 |
return retval;
|
|
934 |
}
|
|
935 |
|
|
936 |
|
|
937 |
static void tag_u64_out(uint8_t cbor_type, uint64_t aux, uint8_t* out, uintptr_t* posp) {
|
|
938 |
uintptr_t pos = *posp;
|
|
939 |
if (out != NULL) {
|
|
940 |
out[pos] = cbor_type | CBOR_UINT64_FOLLOWS;
|
|
941 |
out[pos+1] = (aux >> 56) & 0x0ff;
|
|
942 |
out[pos+2] = (aux >> 48) & 0x0ff;
|
|
943 |
out[pos+3] = (aux >> 40) & 0x0ff;
|
|
944 |
out[pos+4] = (aux >> 32) & 0x0ff;
|
|
945 |
out[pos+5] = (aux >> 24) & 0x0ff;
|
|
946 |
out[pos+6] = (aux >> 16) & 0x0ff;
|
|
947 |
out[pos+7] = (aux >> 8) & 0x0ff;
|
|
948 |
out[pos+8] = aux & 0x0ff;
|
|
949 |
}
|
|
950 |
pos += 9;
|
|
951 |
*posp = pos;
|
|
952 |
}
|
|
953 |
|
|
954 |
|
|
955 |
static void tag_aux_out(uint8_t cbor_type, uint64_t aux, uint8_t* out, uintptr_t* posp) {
|
|
956 |
uintptr_t pos = *posp;
|
|
957 |
if (aux <= 23) {
|
|
958 |
// tiny literal
|
|
959 |
if (out != NULL) {
|
|
960 |
out[pos] = cbor_type | aux;
|
|
961 |
}
|
|
962 |
pos += 1;
|
|
963 |
} else if (aux <= 0x0ff) {
|
|
964 |
// one byte value
|
|
965 |
if (out != NULL) {
|
|
966 |
out[pos] = cbor_type | CBOR_UINT8_FOLLOWS;
|
|
967 |
out[pos+1] = aux;
|
|
968 |
}
|
|
969 |
pos += 2;
|
|
970 |
} else if (aux <= 0x0ffff) {
|
|
971 |
// two byte value
|
|
972 |
if (out != NULL) {
|
|
973 |
out[pos] = cbor_type | CBOR_UINT16_FOLLOWS;
|
|
974 |
out[pos+1] = (aux >> 8) & 0x0ff;
|
|
975 |
out[pos+2] = aux & 0x0ff;
|
|
976 |
}
|
|
977 |
pos += 3;
|
|
978 |
} else if (aux <= 0x0ffffffffL) {
|
|
979 |
// four byte value
|
|
980 |
if (out != NULL) {
|
|
981 |
out[pos] = cbor_type | CBOR_UINT32_FOLLOWS;
|
|
982 |
out[pos+1] = (aux >> 24) & 0x0ff;
|
|
983 |
out[pos+2] = (aux >> 16) & 0x0ff;
|
|
984 |
out[pos+3] = (aux >> 8) & 0x0ff;
|
|
985 |
out[pos+4] = aux & 0x0ff;
|
|
986 |
}
|
|
987 |
pos += 5;
|
|
988 |
} else {
|
|
989 |
// eight byte value
|
|
990 |
tag_u64_out(cbor_type, aux, out, posp);
|
|
991 |
return;
|
|
992 |
}
|
|
993 |
*posp = pos;
|
|
994 |
return;
|
|
995 |
}
|
|
996 |
|
|
997 |
static int inner_dumps(PyObject* ob, uint8_t* out, uintptr_t* posp);
|
|
998 |
|
|
999 |
static int dumps_dict(PyObject* ob, uint8_t* out, uintptr_t* posp) {
|
|
1000 |
uintptr_t pos = *posp;
|
|
1001 |
Py_ssize_t dictiter = 0;
|
|
1002 |
PyObject* key;
|
|
1003 |
PyObject* val;
|
|
1004 |
Py_ssize_t dictlen = PyDict_Size(ob);
|
|
1005 |
int err;
|
|
1006 |
tag_aux_out(CBOR_MAP, dictlen, out, &pos);
|
|
1007 |
while (PyDict_Next(ob, &dictiter, &key, &val)) {
|
|
1008 |
err = inner_dumps(key, out, &pos);
|
|
1009 |
if (err != 0) { return err; }
|
|
1010 |
err = inner_dumps(val, out, &pos);
|
|
1011 |
if (err != 0) { return err; }
|
|
1012 |
}
|
|
1013 |
*posp = pos;
|
|
1014 |
return 0;
|
|
1015 |
}
|
|
1016 |
|
|
1017 |
|
|
1018 |
static void dumps_bignum(uint8_t tag, PyObject* val, uint8_t* out, uintptr_t* posp) {
|
|
1019 |
uintptr_t pos = (posp != NULL) ? *posp : 0;
|
|
1020 |
PyObject* eight = PyLong_FromLong(8);
|
|
1021 |
PyObject* bytemask = NULL;
|
|
1022 |
PyObject* nval = NULL;
|
|
1023 |
uint8_t* revbytes = NULL;
|
|
1024 |
int revbytepos = 0;
|
|
1025 |
int val_is_orig = 1;
|
|
1026 |
if (out != NULL) {
|
|
1027 |
bytemask = PyLong_FromLongLong(0x0ff);
|
|
1028 |
revbytes = PyMem_Malloc(23);
|
|
1029 |
}
|
|
1030 |
while (PyObject_IsTrue(val) && (revbytepos < 23)) {
|
|
1031 |
if (revbytes != NULL) {
|
|
1032 |
PyObject* tbyte = PyNumber_And(val, bytemask);
|
|
1033 |
revbytes[revbytepos] = PyLong_AsLong(tbyte);
|
|
1034 |
Py_DECREF(tbyte);
|
|
1035 |
}
|
|
1036 |
revbytepos++;
|
|
1037 |
nval = PyNumber_InPlaceRshift(val, eight);
|
|
1038 |
if (val_is_orig) {
|
|
1039 |
val_is_orig = 0;
|
|
1040 |
} else {
|
|
1041 |
Py_DECREF(val);
|
|
1042 |
}
|
|
1043 |
val = nval;
|
|
1044 |
}
|
|
1045 |
if (revbytes != NULL) {
|
|
1046 |
out[pos] = CBOR_TAG | tag;
|
|
1047 |
pos++;
|
|
1048 |
out[pos] = CBOR_BYTES | revbytepos;
|
|
1049 |
pos++;
|
|
1050 |
revbytepos--;
|
|
1051 |
while (revbytepos >= 0) {
|
|
1052 |
out[pos] = revbytes[revbytepos];
|
|
1053 |
pos++;
|
|
1054 |
revbytepos--;
|
|
1055 |
}
|
|
1056 |
PyMem_Free(revbytes);
|
|
1057 |
Py_DECREF(bytemask);
|
|
1058 |
} else {
|
|
1059 |
pos += 2 + revbytepos;
|
|
1060 |
}
|
|
1061 |
if (!val_is_orig) {
|
|
1062 |
Py_DECREF(val);
|
|
1063 |
}
|
|
1064 |
Py_DECREF(eight);
|
|
1065 |
*posp = pos;
|
|
1066 |
}
|
|
1067 |
|
|
1068 |
static int dumps_tag(PyObject* ob, uint8_t* out, uintptr_t* posp) {
|
|
1069 |
uintptr_t pos = (posp != NULL) ? *posp : 0;
|
|
1070 |
int err = 0;
|
|
1071 |
|
|
1072 |
|
|
1073 |
PyObject* tag_num;
|
|
1074 |
PyObject* tag_value;
|
|
1075 |
err = 0;
|
|
1076 |
|
|
1077 |
tag_num = PyObject_GetAttrString(ob, "tag");
|
|
1078 |
if (tag_num != NULL) {
|
|
1079 |
tag_value = PyObject_GetAttrString(ob, "value");
|
|
1080 |
if (tag_value != NULL) {
|
|
1081 |
#ifdef Py_INTOBJECT_H
|
|
1082 |
if (PyInt_Check(tag_num)) {
|
|
1083 |
long val = PyInt_AsLong(tag_num);
|
|
1084 |
if (val > 0) {
|
|
1085 |
tag_aux_out(CBOR_TAG, val, out, &pos);
|
|
1086 |
err = inner_dumps(tag_value, out, &pos);
|
|
1087 |
} else {
|
|
1088 |
PyErr_Format(PyExc_ValueError, "tag cannot be a negative int: %ld", val);
|
|
1089 |
err = -1;
|
|
1090 |
}
|
|
1091 |
} else
|
|
1092 |
#endif
|
|
1093 |
if (PyLong_Check(tag_num)) {
|
|
1094 |
int overflow = -1;
|
|
1095 |
long long val = PyLong_AsLongLongAndOverflow(tag_num, &overflow);
|
|
1096 |
if (overflow == 0) {
|
|
1097 |
if (val >= 0) {
|
|
1098 |
tag_aux_out(CBOR_TAG, val, out, &pos);
|
|
1099 |
err = inner_dumps(tag_value, out, &pos);
|
|
1100 |
} else {
|
|
1101 |
PyErr_Format(PyExc_ValueError, "tag cannot be a negative long: %lld", val);
|
|
1102 |
err = -1;
|
|
1103 |
}
|
|
1104 |
} else {
|
|
1105 |
PyErr_SetString(PyExc_ValueError, "tag number too large");
|
|
1106 |
err = -1;
|
|
1107 |
}
|
|
1108 |
}
|
|
1109 |
Py_DECREF(tag_value);
|
|
1110 |
} else {
|
|
1111 |
PyErr_SetString(PyExc_ValueError, "broken Tag object has .tag but not .value");
|
|
1112 |
err = -1;
|
|
1113 |
}
|
|
1114 |
Py_DECREF(tag_num);
|
|
1115 |
} else {
|
|
1116 |
PyErr_SetString(PyExc_ValueError, "broken Tag object with no .tag");
|
|
1117 |
err = -1;
|
|
1118 |
}
|
|
1119 |
if (err != 0) { return err; }
|
|
1120 |
|
|
1121 |
*posp = pos;
|
|
1122 |
return err;
|
|
1123 |
}
|
|
1124 |
|
|
1125 |
|
|
1126 |
// With out=NULL it just counts the length.
|
|
1127 |
// return err, 0=OK
|
|
1128 |
static int inner_dumps(PyObject* ob, uint8_t* out, uintptr_t* posp) {
|
|
1129 |
uintptr_t pos = (posp != NULL) ? *posp : 0;
|
|
1130 |
|
|
1131 |
if (PyBool_Check(ob)) {
|
|
1132 |
if (out != NULL) {
|
|
1133 |
if (PyObject_IsTrue(ob)) {
|
|
1134 |
out[pos] = CBOR_TRUE;
|
|
1135 |
} else {
|
|
1136 |
out[pos] = CBOR_FALSE;
|
|
1137 |
}
|
|
1138 |
}
|
|
1139 |
pos += 1;
|
|
1140 |
} else if (ob == Py_None) {
|
|
1141 |
if (out != NULL) {
|
|
1142 |
out[pos] = CBOR_NULL;
|
|
1143 |
}
|
|
1144 |
pos += 1;
|
|
1145 |
} else if (PyDict_Check(ob)) {
|
|
1146 |
int err = dumps_dict(ob, out, &pos);
|
|
1147 |
if (err != 0) { return err; }
|
|
1148 |
} else if (PyList_Check(ob)) {
|
|
1149 |
Py_ssize_t i;
|
|
1150 |
Py_ssize_t listlen = PyList_Size(ob);
|
|
1151 |
tag_aux_out(CBOR_ARRAY, listlen, out, &pos);
|
|
1152 |
for (i = 0; i < listlen; i++) {
|
|
1153 |
int err = inner_dumps(PyList_GetItem(ob, i), out, &pos);
|
|
1154 |
if (err != 0) { return err; }
|
|
1155 |
}
|
|
1156 |
} else if (PyTuple_Check(ob)) {
|
|
1157 |
Py_ssize_t i;
|
|
1158 |
Py_ssize_t listlen = PyTuple_Size(ob);
|
|
1159 |
tag_aux_out(CBOR_ARRAY, listlen, out, &pos);
|
|
1160 |
for (i = 0; i < listlen; i++) {
|
|
1161 |
int err = inner_dumps(PyTuple_GetItem(ob, i), out, &pos);
|
|
1162 |
if (err != 0) { return err; }
|
|
1163 |
}
|
|
1164 |
// TODO: accept other enumerables and emit a variable length array
|
|
1165 |
#ifdef Py_INTOBJECT_H
|
|
1166 |
// PyInt exists in Python 2 but not 3
|
|
1167 |
} else if (PyInt_Check(ob)) {
|
|
1168 |
long val = PyInt_AsLong(ob);
|
|
1169 |
if (val >= 0) {
|
|
1170 |
tag_aux_out(CBOR_UINT, val, out, &pos);
|
|
1171 |
} else {
|
|
1172 |
tag_aux_out(CBOR_NEGINT, -1 - val, out, &pos);
|
|
1173 |
}
|
|
1174 |
#endif
|
|
1175 |
} else if (PyLong_Check(ob)) {
|
|
1176 |
int overflow = 0;
|
|
1177 |
long long val = PyLong_AsLongLongAndOverflow(ob, &overflow);
|
|
1178 |
if (overflow == 0) {
|
|
1179 |
if (val >= 0) {
|
|
1180 |
tag_aux_out(CBOR_UINT, val, out, &pos);
|
|
1181 |
} else {
|
|
1182 |
tag_aux_out(CBOR_NEGINT, -1L - val, out, &pos);
|
|
1183 |
}
|
|
1184 |
} else {
|
|
1185 |
if (overflow < 0) {
|
|
1186 |
// BIG NEGINT
|
|
1187 |
PyObject* minusone = PyLong_FromLongLong(-1L);
|
|
1188 |
PyObject* val = PyNumber_Subtract(minusone, ob);
|
|
1189 |
Py_DECREF(minusone);
|
|
1190 |
dumps_bignum(CBOR_TAG_NEGBIGNUM, val, out, &pos);
|
|
1191 |
Py_DECREF(val);
|
|
1192 |
} else {
|
|
1193 |
// BIG INT
|
|
1194 |
dumps_bignum(CBOR_TAG_BIGNUM, ob, out, &pos);
|
|
1195 |
}
|
|
1196 |
}
|
|
1197 |
} else if (PyFloat_Check(ob)) {
|
|
1198 |
double val = PyFloat_AsDouble(ob);
|
|
1199 |
tag_u64_out(CBOR_7, *((uint64_t*)(&val)), out, &pos);
|
|
1200 |
} else if (PyBytes_Check(ob)) {
|
|
1201 |
Py_ssize_t len = PyBytes_Size(ob);
|
|
1202 |
tag_aux_out(CBOR_BYTES, len, out, &pos);
|
|
1203 |
if (out != NULL) {
|
|
1204 |
memcpy(out + pos, PyBytes_AsString(ob), len);
|
|
1205 |
}
|
|
1206 |
pos += len;
|
|
1207 |
} else if (PyUnicode_Check(ob)) {
|
|
1208 |
PyObject* utf8 = PyUnicode_AsUTF8String(ob);
|
|
1209 |
Py_ssize_t len = PyBytes_Size(utf8);
|
|
1210 |
tag_aux_out(CBOR_TEXT, len, out, &pos);
|
|
1211 |
if (out != NULL) {
|
|
1212 |
memcpy(out + pos, PyBytes_AsString(utf8), len);
|
|
1213 |
}
|
|
1214 |
pos += len;
|
|
1215 |
Py_DECREF(utf8);
|
|
1216 |
} else {
|
|
1217 |
int handled = 0;
|
|
1218 |
{
|
|
1219 |
PyObject* tag_class = getCborTagClass();
|
|
1220 |
if (PyObject_IsInstance(ob, tag_class)) {
|
|
1221 |
int err = dumps_tag(ob, out, &pos);
|
|
1222 |
if (err != 0) { return err; }
|
|
1223 |
handled = 1;
|
|
1224 |
}
|
|
1225 |
// tag_class was just a borrowed reference
|
|
1226 |
}
|
|
1227 |
|
|
1228 |
// TODO: other special object serializations here
|
|
1229 |
|
|
1230 |
if (!handled) {
|
|
1231 |
#if IS_PY3
|
|
1232 |
PyErr_Format(PyExc_ValueError, "cannot serialize unknown object: %R", ob);
|
|
1233 |
#else
|
|
1234 |
PyObject* badtype = PyObject_Type(ob);
|
|
1235 |
PyObject* badtypename = PyObject_Str(badtype);
|
|
1236 |
PyErr_Format(PyExc_ValueError, "cannot serialize unknown object of type %s", PyString_AsString(badtypename));
|
|
1237 |
Py_DECREF(badtypename);
|
|
1238 |
Py_DECREF(badtype);
|
|
1239 |
#endif
|
|
1240 |
return -1;
|
|
1241 |
}
|
|
1242 |
}
|
|
1243 |
if (posp != NULL) {
|
|
1244 |
*posp = pos;
|
|
1245 |
}
|
|
1246 |
return 0;
|
|
1247 |
}
|
|
1248 |
|
|
1249 |
static PyObject*
|
|
1250 |
cbor_dumps(PyObject* noself, PyObject* args) {
|
|
1251 |
PyObject* ob;
|
|
1252 |
is_big_endian();
|
|
1253 |
if (PyType_IsSubtype(Py_TYPE(args), &PyList_Type)) {
|
|
1254 |
ob = PyList_GetItem(args, 0);
|
|
1255 |
} else if (PyType_IsSubtype(Py_TYPE(args), &PyTuple_Type)) {
|
|
1256 |
ob = PyTuple_GetItem(args, 0);
|
|
1257 |
} else {
|
|
1258 |
PyErr_Format(PyExc_ValueError, "args not list or tuple: %R\n", args);
|
|
1259 |
return NULL;
|
|
1260 |
}
|
|
1261 |
|
|
1262 |
{
|
|
1263 |
Py_ssize_t outlen = 0;
|
|
1264 |
uintptr_t pos = 0;
|
|
1265 |
void* out = NULL;
|
|
1266 |
PyObject* obout = NULL;
|
|
1267 |
int err;
|
|
1268 |
|
|
1269 |
// first pass just to count length
|
|
1270 |
err = inner_dumps(ob, NULL, &pos);
|
|
1271 |
if (err != 0) {
|
|
1272 |
return NULL;
|
|
1273 |
}
|
|
1274 |
|
|
1275 |
outlen = pos;
|
|
1276 |
|
|
1277 |
out = PyMem_Malloc(outlen);
|
|
1278 |
if (out == NULL) {
|
|
1279 |
PyErr_NoMemory();
|
|
1280 |
return NULL;
|
|
1281 |
}
|
|
1282 |
|
|
1283 |
err = inner_dumps(ob, out, NULL);
|
|
1284 |
if (err != 0) {
|
|
1285 |
PyMem_Free(out);
|
|
1286 |
return NULL;
|
|
1287 |
}
|
|
1288 |
|
|
1289 |
// TODO: I wish there was a way to do this without this copy.
|
|
1290 |
obout = PyBytes_FromStringAndSize(out, outlen);
|
|
1291 |
PyMem_Free(out);
|
|
1292 |
return obout;
|
|
1293 |
}
|
|
1294 |
}
|
|
1295 |
|
|
1296 |
static PyObject*
|
|
1297 |
cbor_dump(PyObject* noself, PyObject* args) {
|
|
1298 |
// args should be (obj, fp)
|
|
1299 |
PyObject* ob;
|
|
1300 |
PyObject* fp;
|
|
1301 |
|
|
1302 |
is_big_endian();
|
|
1303 |
if (PyType_IsSubtype(Py_TYPE(args), &PyList_Type)) {
|
|
1304 |
ob = PyList_GetItem(args, 0);
|
|
1305 |
fp = PyList_GetItem(args, 1);
|
|
1306 |
} else if (PyType_IsSubtype(Py_TYPE(args), &PyTuple_Type)) {
|
|
1307 |
ob = PyTuple_GetItem(args, 0);
|
|
1308 |
fp = PyTuple_GetItem(args, 1);
|
|
1309 |
} else {
|
|
1310 |
PyErr_Format(PyExc_ValueError, "args not list or tuple: %R\n", args);
|
|
1311 |
return NULL;
|
|
1312 |
}
|
|
1313 |
|
|
1314 |
{
|
|
1315 |
// TODO: make this smarter, right now it is justt fp.write(dumps(ob))
|
|
1316 |
Py_ssize_t outlen = 0;
|
|
1317 |
uintptr_t pos = 0;
|
|
1318 |
void* out = NULL;
|
|
1319 |
int err;
|
|
1320 |
|
|
1321 |
// first pass just to count length
|
|
1322 |
err = inner_dumps(ob, NULL, &pos);
|
|
1323 |
if (err != 0) {
|
|
1324 |
return NULL;
|
|
1325 |
}
|
|
1326 |
|
|
1327 |
outlen = pos;
|
|
1328 |
|
|
1329 |
out = PyMem_Malloc(outlen);
|
|
1330 |
if (out == NULL) {
|
|
1331 |
PyErr_NoMemory();
|
|
1332 |
return NULL;
|
|
1333 |
}
|
|
1334 |
|
|
1335 |
err = inner_dumps(ob, out, NULL);
|
|
1336 |
if (err != 0) {
|
|
1337 |
PyMem_Free(out);
|
|
1338 |
return NULL;
|
|
1339 |
}
|
|
1340 |
|
|
1341 |
#if HAS_FILE_READER
|
|
1342 |
if (PyFile_Check(fp)) {
|
|
1343 |
FILE* fout = PyFile_AsFile(fp);
|
|
1344 |
fwrite(out, 1, outlen, fout);
|
|
1345 |
} else
|
|
1346 |
#endif
|
|
1347 |
{
|
|
1348 |
PyObject* ret;
|
|
1349 |
PyObject* obout = NULL;
|
|
1350 |
#if IS_PY3
|
|
1351 |
PyObject* writeStr = PyUnicode_FromString("write");
|
|
1352 |
#else
|
|
1353 |
PyObject* writeStr = PyString_FromString("write");
|
|
1354 |
#endif
|
|
1355 |
obout = PyBytes_FromStringAndSize(out, outlen);
|
|
1356 |
//logprintf("write %zd bytes to %p.write() as %p\n", outlen, fp, obout);
|
|
1357 |
ret = PyObject_CallMethodObjArgs(fp, writeStr, obout, NULL);
|
|
1358 |
Py_DECREF(writeStr);
|
|
1359 |
Py_DECREF(obout);
|
|
1360 |
if (ret != NULL) {
|
|
1361 |
Py_DECREF(ret);
|
|
1362 |
} else {
|
|
1363 |
// exception in fp.write()
|
|
1364 |
PyMem_Free(out);
|
|
1365 |
return NULL;
|
|
1366 |
}
|
|
1367 |
//logprintf("wrote %zd bytes to %p.write() as %p\n", outlen, fp, obout);
|
|
1368 |
}
|
|
1369 |
PyMem_Free(out);
|
|
1370 |
}
|
|
1371 |
|
|
1372 |
Py_RETURN_NONE;
|
|
1373 |
}
|
|
1374 |
|
|
1375 |
|
|
1376 |
static PyMethodDef CborMethods[] = {
|
|
1377 |
{"loads", cbor_loads, METH_VARARGS,
|
|
1378 |
"parse cbor from data buffer to objects"},
|
|
1379 |
{"dumps", cbor_dumps, METH_VARARGS,
|
|
1380 |
"serialize python object to bytes"},
|
|
1381 |
{"load", cbor_load, METH_VARARGS,
|
|
1382 |
"Parse cbor from data buffer to objects.\n"
|
|
1383 |
"Takes a file-like object capable of .read(N)\n"},
|
|
1384 |
{"dump", cbor_dump, METH_VARARGS,
|
|
1385 |
"Serialize python object to bytes.\n"
|
|
1386 |
"dump(obj, fp)\n"
|
|
1387 |
"obj: object to output; fp: file-like object to .write() to\n"},
|
|
1388 |
{NULL, NULL, 0, NULL} /* Sentinel */
|
|
1389 |
};
|
|
1390 |
|
|
1391 |
#ifdef Py_InitModule
|
|
1392 |
// Python 2.7
|
|
1393 |
PyMODINIT_FUNC
|
|
1394 |
init_cbor(void)
|
|
1395 |
{
|
|
1396 |
(void) Py_InitModule("cbor._cbor", CborMethods);
|
|
1397 |
}
|
|
1398 |
#else
|
|
1399 |
// Python 3
|
|
1400 |
PyMODINIT_FUNC
|
|
1401 |
PyInit__cbor(void)
|
|
1402 |
{
|
|
1403 |
static PyModuleDef modef = {
|
|
1404 |
PyModuleDef_HEAD_INIT,
|
|
1405 |
};
|
|
1406 |
//modef.m_base = PyModuleDef_HEAD_INIT;
|
|
1407 |
modef.m_name = "cbor._cbor";
|
|
1408 |
modef.m_doc = NULL;
|
|
1409 |
modef.m_size = 0;
|
|
1410 |
modef.m_methods = CborMethods;
|
|
1411 |
modef.m_reload = NULL;
|
|
1412 |
modef.m_traverse = NULL;
|
|
1413 |
modef.m_clear = NULL;
|
|
1414 |
modef.m_free = NULL;
|
|
1415 |
return PyModule_Create(&modef);
|
|
1416 |
}
|
|
1417 |
#endif
|
|
1418 |
|