blob: 33685b9d7c28dd299ba49ae47f852c464599380a [file] [log] [blame]
Guido van Rossumdce2e3d1991-06-04 19:42:30 +00001
2/* Write Python objects to files and read them back.
3 This is intended for writing and reading compiled Python code only;
4 a true persistent storage facility would be much harder, since
5 it would have to take circular links and sharing into account. */
6
Thomas Wouters695934a2006-03-01 23:49:13 +00007#define PY_SSIZE_T_CLEAN
8
Guido van Rossum79f25d91997-04-29 20:08:16 +00009#include "Python.h"
Guido van Rossumdce2e3d1991-06-04 19:42:30 +000010#include "longintrepr.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000011#include "code.h"
Guido van Rossumdce2e3d1991-06-04 19:42:30 +000012#include "marshal.h"
13
Mark Dickinsonefc82f72009-03-20 15:51:55 +000014#define ABS(x) ((x) < 0 ? -(x) : (x))
15
Fred Drake6da0b912000-06-28 18:47:56 +000016/* High water mark to determine when the marshalled object is dangerously deep
17 * and risks coring the interpreter. When the object stack gets this deep,
18 * raise an exception instead of continuing.
19 */
Neal Norwitzf6b0e4d2007-05-17 07:04:46 +000020#define MAX_MARSHAL_STACK_DEPTH 2000
Fred Drake6da0b912000-06-28 18:47:56 +000021
Antoine Pitrouc83ea132010-05-09 14:46:46 +000022#define TYPE_NULL '0'
23#define TYPE_NONE 'N'
24#define TYPE_FALSE 'F'
25#define TYPE_TRUE 'T'
26#define TYPE_STOPITER 'S'
27#define TYPE_ELLIPSIS '.'
28#define TYPE_INT 'i'
29#define TYPE_INT64 'I'
30#define TYPE_FLOAT 'f'
31#define TYPE_BINARY_FLOAT 'g'
32#define TYPE_COMPLEX 'x'
33#define TYPE_BINARY_COMPLEX 'y'
34#define TYPE_LONG 'l'
35#define TYPE_STRING 's'
36#define TYPE_INTERNED 't'
37#define TYPE_STRINGREF 'R'
38#define TYPE_TUPLE '('
39#define TYPE_LIST '['
40#define TYPE_DICT '{'
41#define TYPE_CODE 'c'
42#define TYPE_UNICODE 'u'
43#define TYPE_UNKNOWN '?'
44#define TYPE_SET '<'
45#define TYPE_FROZENSET '>'
Guido van Rossumdce2e3d1991-06-04 19:42:30 +000046
Eric Smith15669272009-10-19 00:34:12 +000047#define WFERR_OK 0
48#define WFERR_UNMARSHALLABLE 1
49#define WFERR_NESTEDTOODEEP 2
50#define WFERR_NOMEMORY 3
51
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000052typedef struct {
Antoine Pitrouc83ea132010-05-09 14:46:46 +000053 FILE *fp;
54 int error; /* see WFERR_* values */
55 int depth;
56 /* If fp == NULL, the following are valid: */
57 PyObject *str;
58 char *ptr;
59 char *end;
60 PyObject *strings; /* dict on marshal, list on unmarshal */
61 int version;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000062} WFILE;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +000063
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000064#define w_byte(c, p) if (((p)->fp)) putc((c), (p)->fp); \
Antoine Pitrouc83ea132010-05-09 14:46:46 +000065 else if ((p)->ptr != (p)->end) *(p)->ptr++ = (c); \
66 else w_more(c, p)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000067
68static void
Fredrik Lundh11534382000-07-23 18:24:06 +000069w_more(int c, WFILE *p)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000070{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000071 Py_ssize_t size, newsize;
72 if (p->str == NULL)
73 return; /* An error already occurred */
74 size = PyString_Size(p->str);
75 newsize = size + size + 1024;
76 if (newsize > 32*1024*1024) {
77 newsize = size + (size >> 3); /* 12.5% overallocation */
78 }
79 if (_PyString_Resize(&p->str, newsize) != 0) {
80 p->ptr = p->end = NULL;
81 }
82 else {
83 p->ptr = PyString_AS_STRING((PyStringObject *)p->str) + size;
84 p->end =
85 PyString_AS_STRING((PyStringObject *)p->str) + newsize;
86 *p->ptr++ = Py_SAFE_DOWNCAST(c, int, char);
87 }
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000088}
89
90static void
Serhiy Storchaka34fe1b72013-02-13 12:07:43 +020091w_string(char *s, Py_ssize_t n, WFILE *p)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000092{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000093 if (p->fp != NULL) {
94 fwrite(s, 1, n, p->fp);
95 }
96 else {
97 while (--n >= 0) {
98 w_byte(*s, p);
99 s++;
100 }
101 }
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000102}
103
104static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000105w_short(int x, WFILE *p)
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000106{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000107 w_byte((char)( x & 0xff), p);
108 w_byte((char)((x>> 8) & 0xff), p);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000109}
110
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000111static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000112w_long(long x, WFILE *p)
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000113{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000114 w_byte((char)( x & 0xff), p);
115 w_byte((char)((x>> 8) & 0xff), p);
116 w_byte((char)((x>>16) & 0xff), p);
117 w_byte((char)((x>>24) & 0xff), p);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000118}
119
Guido van Rossumc1547d91996-12-10 15:39:04 +0000120#if SIZEOF_LONG > 4
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000121static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000122w_long64(long x, WFILE *p)
Guido van Rossumb0c168c1996-12-05 23:15:02 +0000123{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000124 w_long(x, p);
125 w_long(x>>32, p);
Guido van Rossumb0c168c1996-12-05 23:15:02 +0000126}
Guido van Rossumc1547d91996-12-10 15:39:04 +0000127#endif
Guido van Rossumb0c168c1996-12-05 23:15:02 +0000128
Serhiy Storchaka34fe1b72013-02-13 12:07:43 +0200129#define SIZE32_MAX 0x7FFFFFFF
130
131#if SIZEOF_SIZE_T > 4
132# define W_SIZE(n, p) do { \
133 if ((n) > SIZE32_MAX) { \
134 (p)->depth--; \
135 (p)->error = WFERR_UNMARSHALLABLE; \
136 return; \
137 } \
138 w_long((long)(n), p); \
139 } while(0)
140#else
141# define W_SIZE w_long
142#endif
143
Mark Dickinsonefc82f72009-03-20 15:51:55 +0000144/* We assume that Python longs are stored internally in base some power of
145 2**15; for the sake of portability we'll always read and write them in base
146 exactly 2**15. */
147
148#define PyLong_MARSHAL_SHIFT 15
149#define PyLong_MARSHAL_BASE ((short)1 << PyLong_MARSHAL_SHIFT)
150#define PyLong_MARSHAL_MASK (PyLong_MARSHAL_BASE - 1)
151#if PyLong_SHIFT % PyLong_MARSHAL_SHIFT != 0
152#error "PyLong_SHIFT must be a multiple of PyLong_MARSHAL_SHIFT"
153#endif
154#define PyLong_MARSHAL_RATIO (PyLong_SHIFT / PyLong_MARSHAL_SHIFT)
155
156static void
157w_PyLong(const PyLongObject *ob, WFILE *p)
158{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000159 Py_ssize_t i, j, n, l;
160 digit d;
Mark Dickinsonefc82f72009-03-20 15:51:55 +0000161
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000162 w_byte(TYPE_LONG, p);
163 if (Py_SIZE(ob) == 0) {
164 w_long((long)0, p);
165 return;
166 }
Mark Dickinsonefc82f72009-03-20 15:51:55 +0000167
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000168 /* set l to number of base PyLong_MARSHAL_BASE digits */
169 n = ABS(Py_SIZE(ob));
170 l = (n-1) * PyLong_MARSHAL_RATIO;
171 d = ob->ob_digit[n-1];
172 assert(d != 0); /* a PyLong is always normalized */
173 do {
174 d >>= PyLong_MARSHAL_SHIFT;
175 l++;
176 } while (d != 0);
Serhiy Storchaka34fe1b72013-02-13 12:07:43 +0200177 if (l > SIZE32_MAX) {
178 p->depth--;
179 p->error = WFERR_UNMARSHALLABLE;
180 return;
181 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000182 w_long((long)(Py_SIZE(ob) > 0 ? l : -l), p);
Mark Dickinsonefc82f72009-03-20 15:51:55 +0000183
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000184 for (i=0; i < n-1; i++) {
185 d = ob->ob_digit[i];
186 for (j=0; j < PyLong_MARSHAL_RATIO; j++) {
187 w_short(d & PyLong_MARSHAL_MASK, p);
188 d >>= PyLong_MARSHAL_SHIFT;
189 }
190 assert (d == 0);
191 }
192 d = ob->ob_digit[n-1];
193 do {
194 w_short(d & PyLong_MARSHAL_MASK, p);
195 d >>= PyLong_MARSHAL_SHIFT;
196 } while (d != 0);
Mark Dickinsonefc82f72009-03-20 15:51:55 +0000197}
198
Guido van Rossumb0c168c1996-12-05 23:15:02 +0000199static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000200w_object(PyObject *v, WFILE *p)
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000201{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000202 Py_ssize_t i, n;
Fred Drake6da0b912000-06-28 18:47:56 +0000203
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000204 p->depth++;
Tim Petersd9b9ac82001-01-28 00:27:39 +0000205
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000206 if (p->depth > MAX_MARSHAL_STACK_DEPTH) {
207 p->error = WFERR_NESTEDTOODEEP;
208 }
209 else if (v == NULL) {
210 w_byte(TYPE_NULL, p);
211 }
212 else if (v == Py_None) {
213 w_byte(TYPE_NONE, p);
214 }
215 else if (v == PyExc_StopIteration) {
216 w_byte(TYPE_STOPITER, p);
217 }
218 else if (v == Py_Ellipsis) {
219 w_byte(TYPE_ELLIPSIS, p);
220 }
221 else if (v == Py_False) {
222 w_byte(TYPE_FALSE, p);
223 }
224 else if (v == Py_True) {
225 w_byte(TYPE_TRUE, p);
226 }
227 else if (PyInt_CheckExact(v)) {
228 long x = PyInt_AS_LONG((PyIntObject *)v);
Guido van Rossumc1547d91996-12-10 15:39:04 +0000229#if SIZEOF_LONG > 4
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000230 long y = Py_ARITHMETIC_RIGHT_SHIFT(long, x, 31);
231 if (y && y != -1) {
232 w_byte(TYPE_INT64, p);
233 w_long64(x, p);
234 }
235 else
Guido van Rossumc1547d91996-12-10 15:39:04 +0000236#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000237 {
238 w_byte(TYPE_INT, p);
239 w_long(x, p);
240 }
241 }
242 else if (PyLong_CheckExact(v)) {
243 PyLongObject *ob = (PyLongObject *)v;
244 w_PyLong(ob, p);
245 }
246 else if (PyFloat_CheckExact(v)) {
247 if (p->version > 1) {
248 unsigned char buf[8];
249 if (_PyFloat_Pack8(PyFloat_AsDouble(v),
250 buf, 1) < 0) {
251 p->error = WFERR_UNMARSHALLABLE;
252 return;
253 }
254 w_byte(TYPE_BINARY_FLOAT, p);
255 w_string((char*)buf, 8, p);
256 }
257 else {
258 char *buf = PyOS_double_to_string(PyFloat_AS_DOUBLE(v),
259 'g', 17, 0, NULL);
260 if (!buf) {
261 p->error = WFERR_NOMEMORY;
262 return;
263 }
264 n = strlen(buf);
265 w_byte(TYPE_FLOAT, p);
266 w_byte((int)n, p);
Serhiy Storchaka34fe1b72013-02-13 12:07:43 +0200267 w_string(buf, n, p);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000268 PyMem_Free(buf);
269 }
270 }
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000271#ifndef WITHOUT_COMPLEX
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000272 else if (PyComplex_CheckExact(v)) {
273 if (p->version > 1) {
274 unsigned char buf[8];
275 if (_PyFloat_Pack8(PyComplex_RealAsDouble(v),
276 buf, 1) < 0) {
277 p->error = WFERR_UNMARSHALLABLE;
278 return;
279 }
280 w_byte(TYPE_BINARY_COMPLEX, p);
281 w_string((char*)buf, 8, p);
282 if (_PyFloat_Pack8(PyComplex_ImagAsDouble(v),
283 buf, 1) < 0) {
284 p->error = WFERR_UNMARSHALLABLE;
285 return;
286 }
287 w_string((char*)buf, 8, p);
288 }
289 else {
290 char *buf;
291 w_byte(TYPE_COMPLEX, p);
292 buf = PyOS_double_to_string(PyComplex_RealAsDouble(v),
293 'g', 17, 0, NULL);
294 if (!buf) {
295 p->error = WFERR_NOMEMORY;
296 return;
297 }
298 n = strlen(buf);
299 w_byte((int)n, p);
Serhiy Storchaka34fe1b72013-02-13 12:07:43 +0200300 w_string(buf, n, p);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000301 PyMem_Free(buf);
302 buf = PyOS_double_to_string(PyComplex_ImagAsDouble(v),
303 'g', 17, 0, NULL);
304 if (!buf) {
305 p->error = WFERR_NOMEMORY;
306 return;
307 }
308 n = strlen(buf);
309 w_byte((int)n, p);
Serhiy Storchaka34fe1b72013-02-13 12:07:43 +0200310 w_string(buf, n, p);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000311 PyMem_Free(buf);
312 }
313 }
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000314#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000315 else if (PyString_CheckExact(v)) {
316 if (p->strings && PyString_CHECK_INTERNED(v)) {
317 PyObject *o = PyDict_GetItem(p->strings, v);
318 if (o) {
319 long w = PyInt_AsLong(o);
320 w_byte(TYPE_STRINGREF, p);
321 w_long(w, p);
322 goto exit;
323 }
324 else {
325 int ok;
326 o = PyInt_FromSsize_t(PyDict_Size(p->strings));
327 ok = o &&
328 PyDict_SetItem(p->strings, v, o) >= 0;
329 Py_XDECREF(o);
330 if (!ok) {
331 p->depth--;
332 p->error = WFERR_UNMARSHALLABLE;
333 return;
334 }
335 w_byte(TYPE_INTERNED, p);
336 }
337 }
338 else {
339 w_byte(TYPE_STRING, p);
340 }
341 n = PyString_GET_SIZE(v);
Serhiy Storchaka34fe1b72013-02-13 12:07:43 +0200342 W_SIZE(n, p);
343 w_string(PyString_AS_STRING(v), n, p);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000344 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000345#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000346 else if (PyUnicode_CheckExact(v)) {
347 PyObject *utf8;
348 utf8 = PyUnicode_AsUTF8String(v);
349 if (utf8 == NULL) {
350 p->depth--;
351 p->error = WFERR_UNMARSHALLABLE;
352 return;
353 }
354 w_byte(TYPE_UNICODE, p);
355 n = PyString_GET_SIZE(utf8);
Serhiy Storchaka34fe1b72013-02-13 12:07:43 +0200356 W_SIZE(n, p);
357 w_string(PyString_AS_STRING(utf8), n, p);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000358 Py_DECREF(utf8);
359 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000360#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000361 else if (PyTuple_CheckExact(v)) {
362 w_byte(TYPE_TUPLE, p);
363 n = PyTuple_Size(v);
Serhiy Storchaka34fe1b72013-02-13 12:07:43 +0200364 W_SIZE(n, p);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000365 for (i = 0; i < n; i++) {
366 w_object(PyTuple_GET_ITEM(v, i), p);
367 }
368 }
369 else if (PyList_CheckExact(v)) {
370 w_byte(TYPE_LIST, p);
371 n = PyList_GET_SIZE(v);
Serhiy Storchaka34fe1b72013-02-13 12:07:43 +0200372 W_SIZE(n, p);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000373 for (i = 0; i < n; i++) {
374 w_object(PyList_GET_ITEM(v, i), p);
375 }
376 }
377 else if (PyDict_CheckExact(v)) {
378 Py_ssize_t pos;
379 PyObject *key, *value;
380 w_byte(TYPE_DICT, p);
381 /* This one is NULL object terminated! */
382 pos = 0;
383 while (PyDict_Next(v, &pos, &key, &value)) {
384 w_object(key, p);
385 w_object(value, p);
386 }
387 w_object((PyObject *)NULL, p);
388 }
389 else if (PyAnySet_CheckExact(v)) {
390 PyObject *value, *it;
Raymond Hettingera422c342005-01-11 03:03:27 +0000391
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000392 if (PyObject_TypeCheck(v, &PySet_Type))
393 w_byte(TYPE_SET, p);
394 else
395 w_byte(TYPE_FROZENSET, p);
396 n = PyObject_Size(v);
397 if (n == -1) {
398 p->depth--;
399 p->error = WFERR_UNMARSHALLABLE;
400 return;
401 }
Serhiy Storchaka34fe1b72013-02-13 12:07:43 +0200402 W_SIZE(n, p);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000403 it = PyObject_GetIter(v);
404 if (it == NULL) {
405 p->depth--;
406 p->error = WFERR_UNMARSHALLABLE;
407 return;
408 }
409 while ((value = PyIter_Next(it)) != NULL) {
410 w_object(value, p);
411 Py_DECREF(value);
412 }
413 Py_DECREF(it);
414 if (PyErr_Occurred()) {
415 p->depth--;
416 p->error = WFERR_UNMARSHALLABLE;
417 return;
418 }
419 }
420 else if (PyCode_Check(v)) {
421 PyCodeObject *co = (PyCodeObject *)v;
422 w_byte(TYPE_CODE, p);
423 w_long(co->co_argcount, p);
424 w_long(co->co_nlocals, p);
425 w_long(co->co_stacksize, p);
426 w_long(co->co_flags, p);
427 w_object(co->co_code, p);
428 w_object(co->co_consts, p);
429 w_object(co->co_names, p);
430 w_object(co->co_varnames, p);
431 w_object(co->co_freevars, p);
432 w_object(co->co_cellvars, p);
433 w_object(co->co_filename, p);
434 w_object(co->co_name, p);
435 w_long(co->co_firstlineno, p);
436 w_object(co->co_lnotab, p);
437 }
438 else if (PyObject_CheckReadBuffer(v)) {
439 /* Write unknown buffer-style objects as a string */
440 char *s;
441 PyBufferProcs *pb = v->ob_type->tp_as_buffer;
442 w_byte(TYPE_STRING, p);
443 n = (*pb->bf_getreadbuffer)(v, 0, (void **)&s);
Serhiy Storchaka34fe1b72013-02-13 12:07:43 +0200444 W_SIZE(n, p);
445 w_string(s, n, p);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000446 }
447 else {
448 w_byte(TYPE_UNKNOWN, p);
449 p->error = WFERR_UNMARSHALLABLE;
450 }
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000451 exit:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000452 p->depth--;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000453}
454
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000455/* version currently has no effect for writing longs. */
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000456void
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000457PyMarshal_WriteLongToFile(long x, FILE *fp, int version)
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000458{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000459 WFILE wf;
460 wf.fp = fp;
461 wf.error = WFERR_OK;
462 wf.depth = 0;
463 wf.strings = NULL;
464 wf.version = version;
465 w_long(x, &wf);
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000466}
467
468void
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000469PyMarshal_WriteObjectToFile(PyObject *x, FILE *fp, int version)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000470{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000471 WFILE wf;
472 wf.fp = fp;
473 wf.error = WFERR_OK;
474 wf.depth = 0;
475 wf.strings = (version > 0) ? PyDict_New() : NULL;
476 wf.version = version;
477 w_object(x, &wf);
478 Py_XDECREF(wf.strings);
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000479}
480
481typedef WFILE RFILE; /* Same struct with different invariants */
482
Neal Norwitzb1a9b372007-05-16 20:05:11 +0000483#define rs_byte(p) (((p)->ptr < (p)->end) ? (unsigned char)*(p)->ptr++ : EOF)
Guido van Rossum8d617a61995-03-09 12:12:11 +0000484
485#define r_byte(p) ((p)->fp ? getc((p)->fp) : rs_byte(p))
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000486
Serhiy Storchaka34fe1b72013-02-13 12:07:43 +0200487static Py_ssize_t
488r_string(char *s, Py_ssize_t n, RFILE *p)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000489{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000490 if (p->fp != NULL)
491 /* The result fits into int because it must be <=n. */
Serhiy Storchaka34fe1b72013-02-13 12:07:43 +0200492 return fread(s, 1, n, p->fp);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000493 if (p->end - p->ptr < n)
Serhiy Storchaka34fe1b72013-02-13 12:07:43 +0200494 n = p->end - p->ptr;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000495 memcpy(s, p->ptr, n);
496 p->ptr += n;
497 return n;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000498}
499
500static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000501r_short(RFILE *p)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000502{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000503 register short x;
504 x = r_byte(p);
505 x |= r_byte(p) << 8;
506 /* Sign-extension, in case short greater than 16 bits */
507 x |= -(x & 0x8000);
508 return x;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000509}
510
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000511static long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000512r_long(RFILE *p)
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000513{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000514 register long x;
515 register FILE *fp = p->fp;
516 if (fp) {
517 x = getc(fp);
518 x |= (long)getc(fp) << 8;
519 x |= (long)getc(fp) << 16;
520 x |= (long)getc(fp) << 24;
521 }
522 else {
523 x = rs_byte(p);
524 x |= (long)rs_byte(p) << 8;
525 x |= (long)rs_byte(p) << 16;
526 x |= (long)rs_byte(p) << 24;
527 }
Guido van Rossumc1547d91996-12-10 15:39:04 +0000528#if SIZEOF_LONG > 4
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000529 /* Sign extension for 64-bit machines */
530 x |= -(x & 0x80000000L);
Guido van Rossumc1547d91996-12-10 15:39:04 +0000531#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000532 return x;
Guido van Rossumb0c168c1996-12-05 23:15:02 +0000533}
534
Tim Peters82112372001-08-29 02:28:42 +0000535/* r_long64 deals with the TYPE_INT64 code. On a machine with
536 sizeof(long) > 4, it returns a Python int object, else a Python long
537 object. Note that w_long64 writes out TYPE_INT if 32 bits is enough,
538 so there's no inefficiency here in returning a PyLong on 32-bit boxes
539 for everything written via TYPE_INT64 (i.e., if an int is written via
540 TYPE_INT64, it *needs* more than 32 bits).
541*/
542static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000543r_long64(RFILE *p)
Guido van Rossumb0c168c1996-12-05 23:15:02 +0000544{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000545 long lo4 = r_long(p);
546 long hi4 = r_long(p);
Guido van Rossumc1547d91996-12-10 15:39:04 +0000547#if SIZEOF_LONG > 4
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000548 long x = (hi4 << 32) | (lo4 & 0xFFFFFFFFL);
549 return PyInt_FromLong(x);
Guido van Rossumc1547d91996-12-10 15:39:04 +0000550#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000551 unsigned char buf[8];
552 int one = 1;
553 int is_little_endian = (int)*(char*)&one;
554 if (is_little_endian) {
555 memcpy(buf, &lo4, 4);
556 memcpy(buf+4, &hi4, 4);
557 }
558 else {
559 memcpy(buf, &hi4, 4);
560 memcpy(buf+4, &lo4, 4);
561 }
562 return _PyLong_FromByteArray(buf, 8, is_little_endian, 1);
Guido van Rossumc1547d91996-12-10 15:39:04 +0000563#endif
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000564}
565
Guido van Rossum79f25d91997-04-29 20:08:16 +0000566static PyObject *
Mark Dickinsonefc82f72009-03-20 15:51:55 +0000567r_PyLong(RFILE *p)
568{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000569 PyLongObject *ob;
Serhiy Storchaka34fe1b72013-02-13 12:07:43 +0200570 long n, size, i;
571 int j, md, shorts_in_top_digit;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000572 digit d;
Mark Dickinsonefc82f72009-03-20 15:51:55 +0000573
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000574 n = r_long(p);
575 if (n == 0)
576 return (PyObject *)_PyLong_New(0);
Serhiy Storchaka34fe1b72013-02-13 12:07:43 +0200577 if (n < -SIZE32_MAX || n > SIZE32_MAX) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000578 PyErr_SetString(PyExc_ValueError,
579 "bad marshal data (long size out of range)");
580 return NULL;
581 }
Mark Dickinsonefc82f72009-03-20 15:51:55 +0000582
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000583 size = 1 + (ABS(n) - 1) / PyLong_MARSHAL_RATIO;
584 shorts_in_top_digit = 1 + (ABS(n) - 1) % PyLong_MARSHAL_RATIO;
585 ob = _PyLong_New(size);
586 if (ob == NULL)
587 return NULL;
588 Py_SIZE(ob) = n > 0 ? size : -size;
Mark Dickinsonefc82f72009-03-20 15:51:55 +0000589
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000590 for (i = 0; i < size-1; i++) {
591 d = 0;
592 for (j=0; j < PyLong_MARSHAL_RATIO; j++) {
593 md = r_short(p);
594 if (md < 0 || md > PyLong_MARSHAL_BASE)
595 goto bad_digit;
596 d += (digit)md << j*PyLong_MARSHAL_SHIFT;
597 }
598 ob->ob_digit[i] = d;
599 }
600 d = 0;
601 for (j=0; j < shorts_in_top_digit; j++) {
602 md = r_short(p);
603 if (md < 0 || md > PyLong_MARSHAL_BASE)
604 goto bad_digit;
605 /* topmost marshal digit should be nonzero */
606 if (md == 0 && j == shorts_in_top_digit - 1) {
607 Py_DECREF(ob);
608 PyErr_SetString(PyExc_ValueError,
609 "bad marshal data (unnormalized long data)");
610 return NULL;
611 }
612 d += (digit)md << j*PyLong_MARSHAL_SHIFT;
613 }
614 /* top digit should be nonzero, else the resulting PyLong won't be
615 normalized */
616 ob->ob_digit[size-1] = d;
617 return (PyObject *)ob;
Mark Dickinsonefc82f72009-03-20 15:51:55 +0000618 bad_digit:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000619 Py_DECREF(ob);
620 PyErr_SetString(PyExc_ValueError,
621 "bad marshal data (digit out of range in long)");
622 return NULL;
Mark Dickinsonefc82f72009-03-20 15:51:55 +0000623}
624
625
626static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000627r_object(RFILE *p)
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000628{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000629 /* NULL is a valid return value, it does not necessarily means that
630 an exception is set. */
631 PyObject *v, *v2;
632 long i, n;
633 int type = r_byte(p);
634 PyObject *retval;
Neal Norwitzb1a9b372007-05-16 20:05:11 +0000635
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000636 p->depth++;
Neal Norwitzb1a9b372007-05-16 20:05:11 +0000637
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000638 if (p->depth > MAX_MARSHAL_STACK_DEPTH) {
639 p->depth--;
640 PyErr_SetString(PyExc_ValueError, "recursion limit exceeded");
641 return NULL;
642 }
Tim Petersd9b9ac82001-01-28 00:27:39 +0000643
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000644 switch (type) {
Tim Petersd9b9ac82001-01-28 00:27:39 +0000645
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000646 case EOF:
647 PyErr_SetString(PyExc_EOFError,
648 "EOF read where object expected");
649 retval = NULL;
650 break;
Tim Petersd9b9ac82001-01-28 00:27:39 +0000651
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000652 case TYPE_NULL:
653 retval = NULL;
654 break;
Tim Petersd9b9ac82001-01-28 00:27:39 +0000655
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000656 case TYPE_NONE:
657 Py_INCREF(Py_None);
658 retval = Py_None;
659 break;
Tim Petersd9b9ac82001-01-28 00:27:39 +0000660
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000661 case TYPE_STOPITER:
662 Py_INCREF(PyExc_StopIteration);
663 retval = PyExc_StopIteration;
664 break;
Tim Peters5ca576e2001-06-18 22:08:13 +0000665
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000666 case TYPE_ELLIPSIS:
667 Py_INCREF(Py_Ellipsis);
668 retval = Py_Ellipsis;
669 break;
Tim Petersd9b9ac82001-01-28 00:27:39 +0000670
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000671 case TYPE_FALSE:
672 Py_INCREF(Py_False);
673 retval = Py_False;
674 break;
Guido van Rossum77f6a652002-04-03 22:41:51 +0000675
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000676 case TYPE_TRUE:
677 Py_INCREF(Py_True);
678 retval = Py_True;
679 break;
Guido van Rossum77f6a652002-04-03 22:41:51 +0000680
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000681 case TYPE_INT:
682 retval = PyInt_FromLong(r_long(p));
683 break;
Tim Petersd9b9ac82001-01-28 00:27:39 +0000684
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000685 case TYPE_INT64:
686 retval = r_long64(p);
687 break;
Tim Petersd9b9ac82001-01-28 00:27:39 +0000688
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000689 case TYPE_LONG:
690 retval = r_PyLong(p);
691 break;
Tim Petersd9b9ac82001-01-28 00:27:39 +0000692
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000693 case TYPE_FLOAT:
694 {
695 char buf[256];
696 double dx;
697 n = r_byte(p);
Serhiy Storchaka34fe1b72013-02-13 12:07:43 +0200698 if (n == EOF || r_string(buf, n, p) != n) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000699 PyErr_SetString(PyExc_EOFError,
700 "EOF read where object expected");
701 retval = NULL;
702 break;
703 }
704 buf[n] = '\0';
705 dx = PyOS_string_to_double(buf, NULL, NULL);
706 if (dx == -1.0 && PyErr_Occurred()) {
707 retval = NULL;
708 break;
709 }
710 retval = PyFloat_FromDouble(dx);
711 break;
712 }
Tim Petersd9b9ac82001-01-28 00:27:39 +0000713
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000714 case TYPE_BINARY_FLOAT:
715 {
716 unsigned char buf[8];
717 double x;
718 if (r_string((char*)buf, 8, p) != 8) {
719 PyErr_SetString(PyExc_EOFError,
720 "EOF read where object expected");
721 retval = NULL;
722 break;
723 }
724 x = _PyFloat_Unpack8(buf, 1);
725 if (x == -1.0 && PyErr_Occurred()) {
726 retval = NULL;
727 break;
728 }
729 retval = PyFloat_FromDouble(x);
730 break;
731 }
Michael W. Hudsondf888462005-06-03 14:41:55 +0000732
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000733#ifndef WITHOUT_COMPLEX
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000734 case TYPE_COMPLEX:
735 {
736 char buf[256];
737 Py_complex c;
738 n = r_byte(p);
Serhiy Storchaka34fe1b72013-02-13 12:07:43 +0200739 if (n == EOF || r_string(buf, n, p) != n) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000740 PyErr_SetString(PyExc_EOFError,
741 "EOF read where object expected");
742 retval = NULL;
743 break;
744 }
745 buf[n] = '\0';
746 c.real = PyOS_string_to_double(buf, NULL, NULL);
747 if (c.real == -1.0 && PyErr_Occurred()) {
748 retval = NULL;
749 break;
750 }
751 n = r_byte(p);
Serhiy Storchaka34fe1b72013-02-13 12:07:43 +0200752 if (n == EOF || r_string(buf, n, p) != n) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000753 PyErr_SetString(PyExc_EOFError,
754 "EOF read where object expected");
755 retval = NULL;
756 break;
757 }
758 buf[n] = '\0';
759 c.imag = PyOS_string_to_double(buf, NULL, NULL);
760 if (c.imag == -1.0 && PyErr_Occurred()) {
761 retval = NULL;
762 break;
763 }
764 retval = PyComplex_FromCComplex(c);
765 break;
766 }
Michael W. Hudsondf888462005-06-03 14:41:55 +0000767
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000768 case TYPE_BINARY_COMPLEX:
769 {
770 unsigned char buf[8];
771 Py_complex c;
772 if (r_string((char*)buf, 8, p) != 8) {
773 PyErr_SetString(PyExc_EOFError,
774 "EOF read where object expected");
775 retval = NULL;
776 break;
777 }
778 c.real = _PyFloat_Unpack8(buf, 1);
779 if (c.real == -1.0 && PyErr_Occurred()) {
780 retval = NULL;
781 break;
782 }
783 if (r_string((char*)buf, 8, p) != 8) {
784 PyErr_SetString(PyExc_EOFError,
785 "EOF read where object expected");
786 retval = NULL;
787 break;
788 }
789 c.imag = _PyFloat_Unpack8(buf, 1);
790 if (c.imag == -1.0 && PyErr_Occurred()) {
791 retval = NULL;
792 break;
793 }
794 retval = PyComplex_FromCComplex(c);
795 break;
796 }
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000797#endif
Tim Petersd9b9ac82001-01-28 00:27:39 +0000798
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000799 case TYPE_INTERNED:
800 case TYPE_STRING:
801 n = r_long(p);
Serhiy Storchaka34fe1b72013-02-13 12:07:43 +0200802 if (n < 0 || n > SIZE32_MAX) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000803 PyErr_SetString(PyExc_ValueError, "bad marshal data (string size out of range)");
804 retval = NULL;
805 break;
806 }
807 v = PyString_FromStringAndSize((char *)NULL, n);
808 if (v == NULL) {
809 retval = NULL;
810 break;
811 }
Serhiy Storchaka34fe1b72013-02-13 12:07:43 +0200812 if (r_string(PyString_AS_STRING(v), n, p) != n) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000813 Py_DECREF(v);
814 PyErr_SetString(PyExc_EOFError,
815 "EOF read where object expected");
816 retval = NULL;
817 break;
818 }
819 if (type == TYPE_INTERNED) {
820 PyString_InternInPlace(&v);
821 if (PyList_Append(p->strings, v) < 0) {
822 retval = NULL;
823 break;
824 }
825 }
826 retval = v;
827 break;
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000828
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000829 case TYPE_STRINGREF:
830 n = r_long(p);
831 if (n < 0 || n >= PyList_GET_SIZE(p->strings)) {
832 PyErr_SetString(PyExc_ValueError, "bad marshal data (string ref out of range)");
833 retval = NULL;
834 break;
835 }
836 v = PyList_GET_ITEM(p->strings, n);
837 Py_INCREF(v);
838 retval = v;
839 break;
Tim Petersd9b9ac82001-01-28 00:27:39 +0000840
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000841#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000842 case TYPE_UNICODE:
843 {
844 char *buffer;
Guido van Rossumc279b532000-03-10 23:03:02 +0000845
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000846 n = r_long(p);
Serhiy Storchaka34fe1b72013-02-13 12:07:43 +0200847 if (n < 0 || n > SIZE32_MAX) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000848 PyErr_SetString(PyExc_ValueError, "bad marshal data (unicode size out of range)");
849 retval = NULL;
850 break;
851 }
852 buffer = PyMem_NEW(char, n);
853 if (buffer == NULL) {
854 retval = PyErr_NoMemory();
855 break;
856 }
Serhiy Storchaka34fe1b72013-02-13 12:07:43 +0200857 if (r_string(buffer, n, p) != n) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000858 PyMem_DEL(buffer);
859 PyErr_SetString(PyExc_EOFError,
860 "EOF read where object expected");
861 retval = NULL;
862 break;
863 }
864 v = PyUnicode_DecodeUTF8(buffer, n, NULL);
865 PyMem_DEL(buffer);
866 retval = v;
867 break;
868 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000869#endif
Tim Petersd9b9ac82001-01-28 00:27:39 +0000870
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000871 case TYPE_TUPLE:
872 n = r_long(p);
Serhiy Storchaka34fe1b72013-02-13 12:07:43 +0200873 if (n < 0 || n > SIZE32_MAX) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000874 PyErr_SetString(PyExc_ValueError, "bad marshal data (tuple size out of range)");
875 retval = NULL;
876 break;
877 }
Serhiy Storchaka34fe1b72013-02-13 12:07:43 +0200878 v = PyTuple_New(n);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000879 if (v == NULL) {
880 retval = NULL;
881 break;
882 }
883 for (i = 0; i < n; i++) {
884 v2 = r_object(p);
885 if ( v2 == NULL ) {
886 if (!PyErr_Occurred())
887 PyErr_SetString(PyExc_TypeError,
888 "NULL object in marshal data for tuple");
889 Py_DECREF(v);
890 v = NULL;
891 break;
892 }
Serhiy Storchaka34fe1b72013-02-13 12:07:43 +0200893 PyTuple_SET_ITEM(v, i, v2);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000894 }
895 retval = v;
896 break;
Tim Petersd9b9ac82001-01-28 00:27:39 +0000897
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000898 case TYPE_LIST:
899 n = r_long(p);
Serhiy Storchaka34fe1b72013-02-13 12:07:43 +0200900 if (n < 0 || n > SIZE32_MAX) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000901 PyErr_SetString(PyExc_ValueError, "bad marshal data (list size out of range)");
902 retval = NULL;
903 break;
904 }
Serhiy Storchaka34fe1b72013-02-13 12:07:43 +0200905 v = PyList_New(n);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000906 if (v == NULL) {
907 retval = NULL;
908 break;
909 }
910 for (i = 0; i < n; i++) {
911 v2 = r_object(p);
912 if ( v2 == NULL ) {
913 if (!PyErr_Occurred())
914 PyErr_SetString(PyExc_TypeError,
915 "NULL object in marshal data for list");
916 Py_DECREF(v);
917 v = NULL;
918 break;
919 }
Serhiy Storchaka34fe1b72013-02-13 12:07:43 +0200920 PyList_SET_ITEM(v, i, v2);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000921 }
922 retval = v;
923 break;
Tim Petersd9b9ac82001-01-28 00:27:39 +0000924
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000925 case TYPE_DICT:
926 v = PyDict_New();
927 if (v == NULL) {
928 retval = NULL;
929 break;
930 }
931 for (;;) {
932 PyObject *key, *val;
933 key = r_object(p);
934 if (key == NULL)
935 break;
936 val = r_object(p);
937 if (val != NULL)
938 PyDict_SetItem(v, key, val);
939 Py_DECREF(key);
940 Py_XDECREF(val);
941 }
942 if (PyErr_Occurred()) {
943 Py_DECREF(v);
944 v = NULL;
945 }
946 retval = v;
947 break;
Tim Petersd9b9ac82001-01-28 00:27:39 +0000948
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000949 case TYPE_SET:
950 case TYPE_FROZENSET:
951 n = r_long(p);
Serhiy Storchaka34fe1b72013-02-13 12:07:43 +0200952 if (n < 0 || n > SIZE32_MAX) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000953 PyErr_SetString(PyExc_ValueError, "bad marshal data (set size out of range)");
954 retval = NULL;
955 break;
956 }
957 v = (type == TYPE_SET) ? PySet_New(NULL) : PyFrozenSet_New(NULL);
958 if (v == NULL) {
959 retval = NULL;
960 break;
961 }
962 for (i = 0; i < n; i++) {
963 v2 = r_object(p);
964 if ( v2 == NULL ) {
965 if (!PyErr_Occurred())
966 PyErr_SetString(PyExc_TypeError,
967 "NULL object in marshal data for set");
968 Py_DECREF(v);
969 v = NULL;
970 break;
971 }
972 if (PySet_Add(v, v2) == -1) {
973 Py_DECREF(v);
974 Py_DECREF(v2);
975 v = NULL;
976 break;
977 }
978 Py_DECREF(v2);
979 }
980 retval = v;
981 break;
Raymond Hettingera422c342005-01-11 03:03:27 +0000982
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000983 case TYPE_CODE:
984 if (PyEval_GetRestricted()) {
985 PyErr_SetString(PyExc_RuntimeError,
986 "cannot unmarshal code objects in "
987 "restricted execution mode");
988 retval = NULL;
989 break;
990 }
991 else {
992 int argcount;
993 int nlocals;
994 int stacksize;
995 int flags;
996 PyObject *code = NULL;
997 PyObject *consts = NULL;
998 PyObject *names = NULL;
999 PyObject *varnames = NULL;
1000 PyObject *freevars = NULL;
1001 PyObject *cellvars = NULL;
1002 PyObject *filename = NULL;
1003 PyObject *name = NULL;
1004 int firstlineno;
1005 PyObject *lnotab = NULL;
Tim Petersd9b9ac82001-01-28 00:27:39 +00001006
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001007 v = NULL;
Michael W. Hudsondf888462005-06-03 14:41:55 +00001008
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001009 /* XXX ignore long->int overflows for now */
1010 argcount = (int)r_long(p);
1011 nlocals = (int)r_long(p);
1012 stacksize = (int)r_long(p);
1013 flags = (int)r_long(p);
1014 code = r_object(p);
1015 if (code == NULL)
1016 goto code_error;
1017 consts = r_object(p);
1018 if (consts == NULL)
1019 goto code_error;
1020 names = r_object(p);
1021 if (names == NULL)
1022 goto code_error;
1023 varnames = r_object(p);
1024 if (varnames == NULL)
1025 goto code_error;
1026 freevars = r_object(p);
1027 if (freevars == NULL)
1028 goto code_error;
1029 cellvars = r_object(p);
1030 if (cellvars == NULL)
1031 goto code_error;
1032 filename = r_object(p);
1033 if (filename == NULL)
1034 goto code_error;
1035 name = r_object(p);
1036 if (name == NULL)
1037 goto code_error;
1038 firstlineno = (int)r_long(p);
1039 lnotab = r_object(p);
1040 if (lnotab == NULL)
1041 goto code_error;
Michael W. Hudsondf888462005-06-03 14:41:55 +00001042
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001043 v = (PyObject *) PyCode_New(
1044 argcount, nlocals, stacksize, flags,
1045 code, consts, names, varnames,
1046 freevars, cellvars, filename, name,
1047 firstlineno, lnotab);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +00001048
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001049 code_error:
1050 Py_XDECREF(code);
1051 Py_XDECREF(consts);
1052 Py_XDECREF(names);
1053 Py_XDECREF(varnames);
1054 Py_XDECREF(freevars);
1055 Py_XDECREF(cellvars);
1056 Py_XDECREF(filename);
1057 Py_XDECREF(name);
1058 Py_XDECREF(lnotab);
Tim Petersd9b9ac82001-01-28 00:27:39 +00001059
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001060 }
1061 retval = v;
1062 break;
Tim Petersd9b9ac82001-01-28 00:27:39 +00001063
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001064 default:
1065 /* Bogus data got written, which isn't ideal.
1066 This will let you keep working and recover. */
1067 PyErr_SetString(PyExc_ValueError, "bad marshal data (unknown type code)");
1068 retval = NULL;
1069 break;
1070
1071 }
1072 p->depth--;
1073 return retval;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +00001074}
1075
Neal Norwitzd85c4522004-06-13 20:31:49 +00001076static PyObject *
Armin Rigo01ab2792004-03-26 15:09:27 +00001077read_object(RFILE *p)
1078{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001079 PyObject *v;
1080 if (PyErr_Occurred()) {
1081 fprintf(stderr, "XXX readobject called with exception set\n");
1082 return NULL;
1083 }
1084 v = r_object(p);
1085 if (v == NULL && !PyErr_Occurred())
1086 PyErr_SetString(PyExc_TypeError, "NULL object in marshal data for object");
1087 return v;
Armin Rigo01ab2792004-03-26 15:09:27 +00001088}
1089
Guido van Rossumb8cf3e62001-10-19 01:46:21 +00001090int
1091PyMarshal_ReadShortFromFile(FILE *fp)
1092{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001093 RFILE rf;
1094 assert(fp);
1095 rf.fp = fp;
1096 rf.strings = NULL;
1097 rf.end = rf.ptr = NULL;
1098 return r_short(&rf);
Guido van Rossumb8cf3e62001-10-19 01:46:21 +00001099}
1100
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00001101long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001102PyMarshal_ReadLongFromFile(FILE *fp)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00001103{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001104 RFILE rf;
1105 rf.fp = fp;
1106 rf.strings = NULL;
1107 rf.ptr = rf.end = NULL;
1108 return r_long(&rf);
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00001109}
1110
Tim Peters691e0e92001-01-18 04:39:16 +00001111#ifdef HAVE_FSTAT
1112/* Return size of file in bytes; < 0 if unknown. */
1113static off_t
1114getfilesize(FILE *fp)
1115{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001116 struct stat st;
1117 if (fstat(fileno(fp), &st) != 0)
1118 return -1;
1119 else
1120 return st.st_size;
Tim Peters691e0e92001-01-18 04:39:16 +00001121}
1122#endif
Tim Petersd9b9ac82001-01-28 00:27:39 +00001123
Tim Peters691e0e92001-01-18 04:39:16 +00001124/* If we can get the size of the file up-front, and it's reasonably small,
1125 * read it in one gulp and delegate to ...FromString() instead. Much quicker
1126 * than reading a byte at a time from file; speeds .pyc imports.
Tim Petersd9b9ac82001-01-28 00:27:39 +00001127 * CAUTION: since this may read the entire remainder of the file, don't
1128 * call it unless you know you're done with the file.
Tim Peters691e0e92001-01-18 04:39:16 +00001129 */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001130PyObject *
Tim Petersd9b9ac82001-01-28 00:27:39 +00001131PyMarshal_ReadLastObjectFromFile(FILE *fp)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00001132{
Antoine Pitrou18e63fb2010-04-21 22:53:29 +00001133/* REASONABLE_FILE_LIMIT is by defn something big enough for Tkinter.pyc. */
Tim Peters691e0e92001-01-18 04:39:16 +00001134#define REASONABLE_FILE_LIMIT (1L << 18)
Tim Peters691e0e92001-01-18 04:39:16 +00001135#ifdef HAVE_FSTAT
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001136 off_t filesize;
1137 filesize = getfilesize(fp);
1138 if (filesize > 0 && filesize <= REASONABLE_FILE_LIMIT) {
1139 char* pBuf = (char *)PyMem_MALLOC(filesize);
1140 if (pBuf != NULL) {
Serhiy Storchaka34fe1b72013-02-13 12:07:43 +02001141 size_t n = fread(pBuf, 1, (size_t)filesize, fp);
1142 PyObject* v = PyMarshal_ReadObjectFromString(pBuf, n);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001143 PyMem_FREE(pBuf);
1144 return v;
1145 }
Tim Petersd9b9ac82001-01-28 00:27:39 +00001146
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001147 }
Tim Peters691e0e92001-01-18 04:39:16 +00001148#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001149 /* We don't have fstat, or we do but the file is larger than
1150 * REASONABLE_FILE_LIMIT or malloc failed -- read a byte at a time.
1151 */
1152 return PyMarshal_ReadObjectFromFile(fp);
Tim Petersd9b9ac82001-01-28 00:27:39 +00001153
Tim Peters691e0e92001-01-18 04:39:16 +00001154#undef REASONABLE_FILE_LIMIT
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00001155}
1156
Guido van Rossum79f25d91997-04-29 20:08:16 +00001157PyObject *
Tim Petersd9b9ac82001-01-28 00:27:39 +00001158PyMarshal_ReadObjectFromFile(FILE *fp)
1159{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001160 RFILE rf;
1161 PyObject *result;
1162 rf.fp = fp;
1163 rf.strings = PyList_New(0);
1164 rf.depth = 0;
1165 rf.ptr = rf.end = NULL;
1166 result = r_object(&rf);
1167 Py_DECREF(rf.strings);
1168 return result;
Tim Petersd9b9ac82001-01-28 00:27:39 +00001169}
1170
1171PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001172PyMarshal_ReadObjectFromString(char *str, Py_ssize_t len)
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001173{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001174 RFILE rf;
1175 PyObject *result;
1176 rf.fp = NULL;
1177 rf.ptr = str;
1178 rf.end = str + len;
1179 rf.strings = PyList_New(0);
1180 rf.depth = 0;
1181 result = r_object(&rf);
1182 Py_DECREF(rf.strings);
1183 return result;
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001184}
1185
Eric Smith15669272009-10-19 00:34:12 +00001186static void
1187set_error(int error)
1188{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001189 switch (error) {
1190 case WFERR_NOMEMORY:
1191 PyErr_NoMemory();
1192 break;
1193 case WFERR_UNMARSHALLABLE:
1194 PyErr_SetString(PyExc_ValueError, "unmarshallable object");
1195 break;
1196 case WFERR_NESTEDTOODEEP:
1197 default:
1198 PyErr_SetString(PyExc_ValueError,
1199 "object too deeply nested to marshal");
1200 break;
1201 }
Eric Smith15669272009-10-19 00:34:12 +00001202}
1203
Guido van Rossum79f25d91997-04-29 20:08:16 +00001204PyObject *
Martin v. Löwisef82d2f2004-06-27 16:51:46 +00001205PyMarshal_WriteObjectToString(PyObject *x, int version)
Guido van Rossum3f3bb3d1996-08-19 22:07:17 +00001206{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001207 WFILE wf;
1208 wf.fp = NULL;
1209 wf.str = PyString_FromStringAndSize((char *)NULL, 50);
1210 if (wf.str == NULL)
1211 return NULL;
1212 wf.ptr = PyString_AS_STRING((PyStringObject *)wf.str);
1213 wf.end = wf.ptr + PyString_Size(wf.str);
1214 wf.error = WFERR_OK;
1215 wf.depth = 0;
1216 wf.version = version;
1217 wf.strings = (version > 0) ? PyDict_New() : NULL;
1218 w_object(x, &wf);
1219 Py_XDECREF(wf.strings);
1220 if (wf.str != NULL) {
1221 char *base = PyString_AS_STRING((PyStringObject *)wf.str);
1222 if (wf.ptr - base > PY_SSIZE_T_MAX) {
1223 Py_DECREF(wf.str);
1224 PyErr_SetString(PyExc_OverflowError,
1225 "too much marshall data for a string");
1226 return NULL;
1227 }
1228 if (_PyString_Resize(&wf.str, (Py_ssize_t)(wf.ptr - base)))
1229 return NULL;
1230 }
1231 if (wf.error != WFERR_OK) {
1232 Py_XDECREF(wf.str);
1233 set_error(wf.error);
1234 return NULL;
1235 }
1236 return wf.str;
Guido van Rossum3f3bb3d1996-08-19 22:07:17 +00001237}
1238
Guido van Rossum64b45521991-06-07 13:58:22 +00001239/* And an interface for Python programs... */
Guido van Rossumdce2e3d1991-06-04 19:42:30 +00001240
Guido van Rossum79f25d91997-04-29 20:08:16 +00001241static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001242marshal_dump(PyObject *self, PyObject *args)
Guido van Rossumdce2e3d1991-06-04 19:42:30 +00001243{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001244 WFILE wf;
1245 PyObject *x;
1246 PyObject *f;
1247 int version = Py_MARSHAL_VERSION;
1248 if (!PyArg_ParseTuple(args, "OO|i:dump", &x, &f, &version))
1249 return NULL;
1250 if (!PyFile_Check(f)) {
1251 PyErr_SetString(PyExc_TypeError,
1252 "marshal.dump() 2nd arg must be file");
1253 return NULL;
1254 }
1255 wf.fp = PyFile_AsFile(f);
1256 wf.str = NULL;
1257 wf.ptr = wf.end = NULL;
1258 wf.error = WFERR_OK;
1259 wf.depth = 0;
1260 wf.strings = (version > 0) ? PyDict_New() : 0;
1261 wf.version = version;
1262 w_object(x, &wf);
1263 Py_XDECREF(wf.strings);
1264 if (wf.error != WFERR_OK) {
1265 set_error(wf.error);
1266 return NULL;
1267 }
1268 Py_INCREF(Py_None);
1269 return Py_None;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +00001270}
1271
R. David Murraya3ec6972009-05-13 00:30:29 +00001272PyDoc_STRVAR(dump_doc,
1273"dump(value, file[, version])\n\
1274\n\
1275Write the value on the open file. The value must be a supported type.\n\
1276The file must be an open file object such as sys.stdout or returned by\n\
1277open() or os.popen(). It must be opened in binary mode ('wb' or 'w+b').\n\
1278\n\
1279If the value has (or contains an object that has) an unsupported type, a\n\
1280ValueError exception is raised but garbage data will also be written\n\
1281to the file. The object will not be properly read back by load()\n\
1282\n\
1283New in version 2.4: The version argument indicates the data format that\n\
1284dump should use.");
1285
Guido van Rossum79f25d91997-04-29 20:08:16 +00001286static PyObject *
Georg Brandlbf92f462006-05-29 21:58:42 +00001287marshal_load(PyObject *self, PyObject *f)
Guido van Rossumdce2e3d1991-06-04 19:42:30 +00001288{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001289 RFILE rf;
1290 PyObject *result;
1291 if (!PyFile_Check(f)) {
1292 PyErr_SetString(PyExc_TypeError,
1293 "marshal.load() arg must be file");
1294 return NULL;
1295 }
1296 rf.fp = PyFile_AsFile(f);
1297 rf.strings = PyList_New(0);
1298 rf.depth = 0;
1299 result = read_object(&rf);
1300 Py_DECREF(rf.strings);
1301 return result;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00001302}
1303
R. David Murraya3ec6972009-05-13 00:30:29 +00001304PyDoc_STRVAR(load_doc,
1305"load(file)\n\
1306\n\
1307Read one value from the open file and return it. If no valid value is\n\
1308read (e.g. because the data has a different Python versions\n\
1309incompatible marshal format), raise EOFError, ValueError or TypeError.\n\
1310The file must be an open file object opened in binary mode ('rb' or\n\
1311'r+b').\n\
1312\n\
1313Note: If an object containing an unsupported type was marshalled with\n\
1314dump(), load() will substitute None for the unmarshallable type.");
1315
1316
Guido van Rossum79f25d91997-04-29 20:08:16 +00001317static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001318marshal_dumps(PyObject *self, PyObject *args)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00001319{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001320 PyObject *x;
1321 int version = Py_MARSHAL_VERSION;
1322 if (!PyArg_ParseTuple(args, "O|i:dumps", &x, &version))
1323 return NULL;
1324 return PyMarshal_WriteObjectToString(x, version);
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00001325}
1326
R. David Murraya3ec6972009-05-13 00:30:29 +00001327PyDoc_STRVAR(dumps_doc,
1328"dumps(value[, version])\n\
1329\n\
1330Return the string that would be written to a file by dump(value, file).\n\
1331The value must be a supported type. Raise a ValueError exception if\n\
1332value has (or contains an object that has) an unsupported type.\n\
1333\n\
1334New in version 2.4: The version argument indicates the data format that\n\
R. David Murray525cffc2009-05-13 13:07:14 +00001335dumps should use.");
R. David Murraya3ec6972009-05-13 00:30:29 +00001336
1337
Guido van Rossum79f25d91997-04-29 20:08:16 +00001338static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001339marshal_loads(PyObject *self, PyObject *args)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00001340{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001341 RFILE rf;
1342 char *s;
1343 Py_ssize_t n;
1344 PyObject* result;
1345 if (!PyArg_ParseTuple(args, "s#:loads", &s, &n))
1346 return NULL;
1347 rf.fp = NULL;
1348 rf.ptr = s;
1349 rf.end = s + n;
1350 rf.strings = PyList_New(0);
1351 rf.depth = 0;
1352 result = read_object(&rf);
1353 Py_DECREF(rf.strings);
1354 return result;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +00001355}
1356
R. David Murraya3ec6972009-05-13 00:30:29 +00001357PyDoc_STRVAR(loads_doc,
1358"loads(string)\n\
1359\n\
1360Convert the string to a value. If no valid value is found, raise\n\
1361EOFError, ValueError or TypeError. Extra characters in the string are\n\
1362ignored.");
1363
Guido van Rossum79f25d91997-04-29 20:08:16 +00001364static PyMethodDef marshal_methods[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001365 {"dump", marshal_dump, METH_VARARGS, dump_doc},
1366 {"load", marshal_load, METH_O, load_doc},
1367 {"dumps", marshal_dumps, METH_VARARGS, dumps_doc},
1368 {"loads", marshal_loads, METH_VARARGS, loads_doc},
1369 {NULL, NULL} /* sentinel */
Guido van Rossumdce2e3d1991-06-04 19:42:30 +00001370};
1371
R. David Murraya3ec6972009-05-13 00:30:29 +00001372PyDoc_STRVAR(marshal_doc,
1373"This module contains functions that can read and write Python values in\n\
1374a binary format. The format is specific to Python, but independent of\n\
1375machine architecture issues.\n\
1376\n\
1377Not all Python object types are supported; in general, only objects\n\
1378whose value is independent from a particular invocation of Python can be\n\
1379written and read by this module. The following types are supported:\n\
1380None, integers, long integers, floating point numbers, strings, Unicode\n\
1381objects, tuples, lists, sets, dictionaries, and code objects, where it\n\
1382should be understood that tuples, lists and dictionaries are only\n\
1383supported as long as the values contained therein are themselves\n\
1384supported; and recursive lists and dictionaries should not be written\n\
1385(they will cause infinite loops).\n\
1386\n\
1387Variables:\n\
1388\n\
1389version -- indicates the format that the module uses. Version 0 is the\n\
1390 historical format, version 1 (added in Python 2.4) shares interned\n\
1391 strings and version 2 (added in Python 2.5) uses a binary format for\n\
1392 floating point numbers. (New in version 2.4)\n\
1393\n\
1394Functions:\n\
1395\n\
1396dump() -- write value to a file\n\
1397load() -- read value from a file\n\
1398dumps() -- write value to a string\n\
1399loads() -- read value from a string");
1400
1401
Jason Tishler6bc06ec2003-09-04 11:59:50 +00001402PyMODINIT_FUNC
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001403PyMarshal_Init(void)
Guido van Rossumdce2e3d1991-06-04 19:42:30 +00001404{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001405 PyObject *mod = Py_InitModule3("marshal", marshal_methods,
1406 marshal_doc);
1407 if (mod == NULL)
1408 return;
1409 PyModule_AddIntConstant(mod, "version", Py_MARSHAL_VERSION);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +00001410}