Issue 6340: update by Gregor Lingl of his tdemo_chaos demo program.
[python.git] / Modules / _struct.c
bloba7fce105a0b946a3bf52a8cd5e05c5c482ae7af3
1 /* struct module -- pack values into and (out of) strings */
3 /* New version supporting byte order, alignment and size options,
4 character strings, and unsigned numbers */
6 #define PY_SSIZE_T_CLEAN
8 #include "Python.h"
9 #include "structseq.h"
10 #include "structmember.h"
11 #include <ctype.h>
13 static PyTypeObject PyStructType;
15 /* compatibility macros */
16 #if (PY_VERSION_HEX < 0x02050000)
17 typedef int Py_ssize_t;
18 #endif
20 /* If PY_STRUCT_OVERFLOW_MASKING is defined, the struct module will wrap all input
21 numbers for explicit endians such that they fit in the given type, much
22 like explicit casting in C. A warning will be raised if the number did
23 not originally fit within the range of the requested type. If it is
24 not defined, then all range errors and overflow will be struct.error
25 exceptions. */
27 #define PY_STRUCT_OVERFLOW_MASKING 1
29 #ifdef PY_STRUCT_OVERFLOW_MASKING
30 static PyObject *pylong_ulong_mask = NULL;
31 static PyObject *pyint_zero = NULL;
32 #endif
34 /* If PY_STRUCT_FLOAT_COERCE is defined, the struct module will allow float
35 arguments for integer formats with a warning for backwards
36 compatibility. */
38 #define PY_STRUCT_FLOAT_COERCE 1
40 #ifdef PY_STRUCT_FLOAT_COERCE
41 #define FLOAT_COERCE "integer argument expected, got float"
42 #endif
45 /* The translation function for each format character is table driven */
46 typedef struct _formatdef {
47 char format;
48 Py_ssize_t size;
49 Py_ssize_t alignment;
50 PyObject* (*unpack)(const char *,
51 const struct _formatdef *);
52 int (*pack)(char *, PyObject *,
53 const struct _formatdef *);
54 } formatdef;
56 typedef struct _formatcode {
57 const struct _formatdef *fmtdef;
58 Py_ssize_t offset;
59 Py_ssize_t size;
60 } formatcode;
62 /* Struct object interface */
64 typedef struct {
65 PyObject_HEAD
66 Py_ssize_t s_size;
67 Py_ssize_t s_len;
68 formatcode *s_codes;
69 PyObject *s_format;
70 PyObject *weakreflist; /* List of weak references */
71 } PyStructObject;
74 #define PyStruct_Check(op) PyObject_TypeCheck(op, &PyStructType)
75 #define PyStruct_CheckExact(op) (Py_TYPE(op) == &PyStructType)
78 /* Exception */
80 static PyObject *StructError;
83 /* Define various structs to figure out the alignments of types */
86 typedef struct { char c; short x; } st_short;
87 typedef struct { char c; int x; } st_int;
88 typedef struct { char c; long x; } st_long;
89 typedef struct { char c; float x; } st_float;
90 typedef struct { char c; double x; } st_double;
91 typedef struct { char c; void *x; } st_void_p;
93 #define SHORT_ALIGN (sizeof(st_short) - sizeof(short))
94 #define INT_ALIGN (sizeof(st_int) - sizeof(int))
95 #define LONG_ALIGN (sizeof(st_long) - sizeof(long))
96 #define FLOAT_ALIGN (sizeof(st_float) - sizeof(float))
97 #define DOUBLE_ALIGN (sizeof(st_double) - sizeof(double))
98 #define VOID_P_ALIGN (sizeof(st_void_p) - sizeof(void *))
100 /* We can't support q and Q in native mode unless the compiler does;
101 in std mode, they're 8 bytes on all platforms. */
102 #ifdef HAVE_LONG_LONG
103 typedef struct { char c; PY_LONG_LONG x; } s_long_long;
104 #define LONG_LONG_ALIGN (sizeof(s_long_long) - sizeof(PY_LONG_LONG))
105 #endif
107 #ifdef HAVE_C99_BOOL
108 #define BOOL_TYPE _Bool
109 typedef struct { char c; _Bool x; } s_bool;
110 #define BOOL_ALIGN (sizeof(s_bool) - sizeof(BOOL_TYPE))
111 #else
112 #define BOOL_TYPE char
113 #define BOOL_ALIGN 0
114 #endif
116 #define STRINGIFY(x) #x
118 #ifdef __powerc
119 #pragma options align=reset
120 #endif
122 /* Helper to get a PyLongObject by hook or by crook. Caller should decref. */
124 static PyObject *
125 get_pylong(PyObject *v)
127 PyNumberMethods *m;
129 assert(v != NULL);
130 if (PyInt_Check(v))
131 return PyLong_FromLong(PyInt_AS_LONG(v));
132 if (PyLong_Check(v)) {
133 Py_INCREF(v);
134 return v;
136 m = Py_TYPE(v)->tp_as_number;
137 if (m != NULL && m->nb_long != NULL) {
138 v = m->nb_long(v);
139 if (v == NULL)
140 return NULL;
141 if (PyLong_Check(v))
142 return v;
143 Py_DECREF(v);
145 PyErr_SetString(StructError,
146 "cannot convert argument to long");
147 return NULL;
150 /* Helper routine to get a Python integer and raise the appropriate error
151 if it isn't one */
153 static int
154 get_long(PyObject *v, long *p)
156 long x = PyInt_AsLong(v);
157 if (x == -1 && PyErr_Occurred()) {
158 #ifdef PY_STRUCT_FLOAT_COERCE
159 if (PyFloat_Check(v)) {
160 PyObject *o;
161 int res;
162 PyErr_Clear();
163 if (PyErr_WarnEx(PyExc_DeprecationWarning, FLOAT_COERCE, 2) < 0)
164 return -1;
165 o = PyNumber_Int(v);
166 if (o == NULL)
167 return -1;
168 res = get_long(o, p);
169 Py_DECREF(o);
170 return res;
172 #endif
173 if (PyErr_ExceptionMatches(PyExc_TypeError))
174 PyErr_SetString(StructError,
175 "required argument is not an integer");
176 return -1;
178 *p = x;
179 return 0;
183 /* Same, but handling unsigned long */
185 #ifndef PY_STRUCT_OVERFLOW_MASKING
186 static int
187 get_ulong(PyObject *v, unsigned long *p)
189 if (PyLong_Check(v)) {
190 unsigned long x = PyLong_AsUnsignedLong(v);
191 if (x == (unsigned long)(-1) && PyErr_Occurred())
192 return -1;
193 *p = x;
194 return 0;
196 if (get_long(v, (long *)p) < 0)
197 return -1;
198 if (((long)*p) < 0) {
199 PyErr_SetString(StructError,
200 "unsigned argument is < 0");
201 return -1;
203 return 0;
205 #endif /* PY_STRUCT_OVERFLOW_MASKING */
207 #ifdef HAVE_LONG_LONG
209 /* Same, but handling native long long. */
211 static int
212 get_longlong(PyObject *v, PY_LONG_LONG *p)
214 PY_LONG_LONG x;
216 v = get_pylong(v);
217 if (v == NULL)
218 return -1;
219 assert(PyLong_Check(v));
220 x = PyLong_AsLongLong(v);
221 Py_DECREF(v);
222 if (x == (PY_LONG_LONG)-1 && PyErr_Occurred())
223 return -1;
224 *p = x;
225 return 0;
228 /* Same, but handling native unsigned long long. */
230 static int
231 get_ulonglong(PyObject *v, unsigned PY_LONG_LONG *p)
233 unsigned PY_LONG_LONG x;
235 v = get_pylong(v);
236 if (v == NULL)
237 return -1;
238 assert(PyLong_Check(v));
239 x = PyLong_AsUnsignedLongLong(v);
240 Py_DECREF(v);
241 if (x == (unsigned PY_LONG_LONG)-1 && PyErr_Occurred())
242 return -1;
243 *p = x;
244 return 0;
247 #endif
249 #ifdef PY_STRUCT_OVERFLOW_MASKING
251 /* Helper routine to get a Python integer and raise the appropriate error
252 if it isn't one */
254 #define INT_OVERFLOW "struct integer overflow masking is deprecated"
256 static int
257 get_wrapped_long(PyObject *v, long *p)
259 if (get_long(v, p) < 0) {
260 if (PyLong_Check(v) &&
261 PyErr_ExceptionMatches(PyExc_OverflowError)) {
262 PyObject *wrapped;
263 long x;
264 PyErr_Clear();
265 #ifdef PY_STRUCT_FLOAT_COERCE
266 if (PyFloat_Check(v)) {
267 PyObject *o;
268 int res;
269 PyErr_Clear();
270 if (PyErr_WarnEx(PyExc_DeprecationWarning, FLOAT_COERCE, 2) < 0)
271 return -1;
272 o = PyNumber_Int(v);
273 if (o == NULL)
274 return -1;
275 res = get_wrapped_long(o, p);
276 Py_DECREF(o);
277 return res;
279 #endif
280 if (PyErr_WarnEx(PyExc_DeprecationWarning, INT_OVERFLOW, 2) < 0)
281 return -1;
282 wrapped = PyNumber_And(v, pylong_ulong_mask);
283 if (wrapped == NULL)
284 return -1;
285 x = (long)PyLong_AsUnsignedLong(wrapped);
286 Py_DECREF(wrapped);
287 if (x == -1 && PyErr_Occurred())
288 return -1;
289 *p = x;
290 } else {
291 return -1;
294 return 0;
297 static int
298 get_wrapped_ulong(PyObject *v, unsigned long *p)
300 long x = (long)PyLong_AsUnsignedLong(v);
301 if (x == -1 && PyErr_Occurred()) {
302 PyObject *wrapped;
303 PyErr_Clear();
304 #ifdef PY_STRUCT_FLOAT_COERCE
305 if (PyFloat_Check(v)) {
306 PyObject *o;
307 int res;
308 PyErr_Clear();
309 if (PyErr_WarnEx(PyExc_DeprecationWarning, FLOAT_COERCE, 2) < 0)
310 return -1;
311 o = PyNumber_Int(v);
312 if (o == NULL)
313 return -1;
314 res = get_wrapped_ulong(o, p);
315 Py_DECREF(o);
316 return res;
318 #endif
319 wrapped = PyNumber_And(v, pylong_ulong_mask);
320 if (wrapped == NULL)
321 return -1;
322 if (PyErr_WarnEx(PyExc_DeprecationWarning, INT_OVERFLOW, 2) < 0) {
323 Py_DECREF(wrapped);
324 return -1;
326 x = (long)PyLong_AsUnsignedLong(wrapped);
327 Py_DECREF(wrapped);
328 if (x == -1 && PyErr_Occurred())
329 return -1;
331 *p = (unsigned long)x;
332 return 0;
335 #define RANGE_ERROR(x, f, flag, mask) \
336 do { \
337 if (_range_error(f, flag) < 0) \
338 return -1; \
339 else \
340 (x) &= (mask); \
341 } while (0)
343 #else
345 #define get_wrapped_long get_long
346 #define get_wrapped_ulong get_ulong
347 #define RANGE_ERROR(x, f, flag, mask) return _range_error(f, flag)
349 #endif
351 /* Floating point helpers */
353 static PyObject *
354 unpack_float(const char *p, /* start of 4-byte string */
355 int le) /* true for little-endian, false for big-endian */
357 double x;
359 x = _PyFloat_Unpack4((unsigned char *)p, le);
360 if (x == -1.0 && PyErr_Occurred())
361 return NULL;
362 return PyFloat_FromDouble(x);
365 static PyObject *
366 unpack_double(const char *p, /* start of 8-byte string */
367 int le) /* true for little-endian, false for big-endian */
369 double x;
371 x = _PyFloat_Unpack8((unsigned char *)p, le);
372 if (x == -1.0 && PyErr_Occurred())
373 return NULL;
374 return PyFloat_FromDouble(x);
377 /* Helper to format the range error exceptions */
378 static int
379 _range_error(const formatdef *f, int is_unsigned)
381 /* ulargest is the largest unsigned value with f->size bytes.
382 * Note that the simpler:
383 * ((size_t)1 << (f->size * 8)) - 1
384 * doesn't work when f->size == sizeof(size_t) because C doesn't
385 * define what happens when a left shift count is >= the number of
386 * bits in the integer being shifted; e.g., on some boxes it doesn't
387 * shift at all when they're equal.
389 const size_t ulargest = (size_t)-1 >> ((SIZEOF_SIZE_T - f->size)*8);
390 assert(f->size >= 1 && f->size <= SIZEOF_SIZE_T);
391 if (is_unsigned)
392 PyErr_Format(StructError,
393 "'%c' format requires 0 <= number <= %zu",
394 f->format,
395 ulargest);
396 else {
397 const Py_ssize_t largest = (Py_ssize_t)(ulargest >> 1);
398 PyErr_Format(StructError,
399 "'%c' format requires %zd <= number <= %zd",
400 f->format,
401 ~ largest,
402 largest);
404 #ifdef PY_STRUCT_OVERFLOW_MASKING
406 PyObject *ptype, *pvalue, *ptraceback;
407 PyObject *msg;
408 int rval;
409 PyErr_Fetch(&ptype, &pvalue, &ptraceback);
410 assert(pvalue != NULL);
411 msg = PyObject_Str(pvalue);
412 Py_XDECREF(ptype);
413 Py_XDECREF(pvalue);
414 Py_XDECREF(ptraceback);
415 if (msg == NULL)
416 return -1;
417 rval = PyErr_WarnEx(PyExc_DeprecationWarning,
418 PyString_AS_STRING(msg), 2);
419 Py_DECREF(msg);
420 if (rval == 0)
421 return 0;
423 #endif
424 return -1;
429 /* A large number of small routines follow, with names of the form
431 [bln][up]_TYPE
433 [bln] distiguishes among big-endian, little-endian and native.
434 [pu] distiguishes between pack (to struct) and unpack (from struct).
435 TYPE is one of char, byte, ubyte, etc.
438 /* Native mode routines. ****************************************************/
439 /* NOTE:
440 In all n[up]_<type> routines handling types larger than 1 byte, there is
441 *no* guarantee that the p pointer is properly aligned for each type,
442 therefore memcpy is called. An intermediate variable is used to
443 compensate for big-endian architectures.
444 Normally both the intermediate variable and the memcpy call will be
445 skipped by C optimisation in little-endian architectures (gcc >= 2.91
446 does this). */
448 static PyObject *
449 nu_char(const char *p, const formatdef *f)
451 return PyString_FromStringAndSize(p, 1);
454 static PyObject *
455 nu_byte(const char *p, const formatdef *f)
457 return PyInt_FromLong((long) *(signed char *)p);
460 static PyObject *
461 nu_ubyte(const char *p, const formatdef *f)
463 return PyInt_FromLong((long) *(unsigned char *)p);
466 static PyObject *
467 nu_short(const char *p, const formatdef *f)
469 short x;
470 memcpy((char *)&x, p, sizeof x);
471 return PyInt_FromLong((long)x);
474 static PyObject *
475 nu_ushort(const char *p, const formatdef *f)
477 unsigned short x;
478 memcpy((char *)&x, p, sizeof x);
479 return PyInt_FromLong((long)x);
482 static PyObject *
483 nu_int(const char *p, const formatdef *f)
485 int x;
486 memcpy((char *)&x, p, sizeof x);
487 return PyInt_FromLong((long)x);
490 static PyObject *
491 nu_uint(const char *p, const formatdef *f)
493 unsigned int x;
494 memcpy((char *)&x, p, sizeof x);
495 #if (SIZEOF_LONG > SIZEOF_INT)
496 return PyInt_FromLong((long)x);
497 #else
498 if (x <= ((unsigned int)LONG_MAX))
499 return PyInt_FromLong((long)x);
500 return PyLong_FromUnsignedLong((unsigned long)x);
501 #endif
504 static PyObject *
505 nu_long(const char *p, const formatdef *f)
507 long x;
508 memcpy((char *)&x, p, sizeof x);
509 return PyInt_FromLong(x);
512 static PyObject *
513 nu_ulong(const char *p, const formatdef *f)
515 unsigned long x;
516 memcpy((char *)&x, p, sizeof x);
517 if (x <= LONG_MAX)
518 return PyInt_FromLong((long)x);
519 return PyLong_FromUnsignedLong(x);
522 /* Native mode doesn't support q or Q unless the platform C supports
523 long long (or, on Windows, __int64). */
525 #ifdef HAVE_LONG_LONG
527 static PyObject *
528 nu_longlong(const char *p, const formatdef *f)
530 PY_LONG_LONG x;
531 memcpy((char *)&x, p, sizeof x);
532 if (x >= LONG_MIN && x <= LONG_MAX)
533 return PyInt_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
534 return PyLong_FromLongLong(x);
537 static PyObject *
538 nu_ulonglong(const char *p, const formatdef *f)
540 unsigned PY_LONG_LONG x;
541 memcpy((char *)&x, p, sizeof x);
542 if (x <= LONG_MAX)
543 return PyInt_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
544 return PyLong_FromUnsignedLongLong(x);
547 #endif
549 static PyObject *
550 nu_bool(const char *p, const formatdef *f)
552 BOOL_TYPE x;
553 memcpy((char *)&x, p, sizeof x);
554 return PyBool_FromLong(x != 0);
558 static PyObject *
559 nu_float(const char *p, const formatdef *f)
561 float x;
562 memcpy((char *)&x, p, sizeof x);
563 return PyFloat_FromDouble((double)x);
566 static PyObject *
567 nu_double(const char *p, const formatdef *f)
569 double x;
570 memcpy((char *)&x, p, sizeof x);
571 return PyFloat_FromDouble(x);
574 static PyObject *
575 nu_void_p(const char *p, const formatdef *f)
577 void *x;
578 memcpy((char *)&x, p, sizeof x);
579 return PyLong_FromVoidPtr(x);
582 static int
583 np_byte(char *p, PyObject *v, const formatdef *f)
585 long x;
586 if (get_long(v, &x) < 0)
587 return -1;
588 if (x < -128 || x > 127){
589 PyErr_SetString(StructError,
590 "byte format requires -128 <= number <= 127");
591 return -1;
593 *p = (char)x;
594 return 0;
597 static int
598 np_ubyte(char *p, PyObject *v, const formatdef *f)
600 long x;
601 if (get_long(v, &x) < 0)
602 return -1;
603 if (x < 0 || x > 255){
604 PyErr_SetString(StructError,
605 "ubyte format requires 0 <= number <= 255");
606 return -1;
608 *p = (char)x;
609 return 0;
612 static int
613 np_char(char *p, PyObject *v, const formatdef *f)
615 if (!PyString_Check(v) || PyString_Size(v) != 1) {
616 PyErr_SetString(StructError,
617 "char format require string of length 1");
618 return -1;
620 *p = *PyString_AsString(v);
621 return 0;
624 static int
625 np_short(char *p, PyObject *v, const formatdef *f)
627 long x;
628 short y;
629 if (get_long(v, &x) < 0)
630 return -1;
631 if (x < SHRT_MIN || x > SHRT_MAX){
632 PyErr_SetString(StructError,
633 "short format requires " STRINGIFY(SHRT_MIN)
634 " <= number <= " STRINGIFY(SHRT_MAX));
635 return -1;
637 y = (short)x;
638 memcpy(p, (char *)&y, sizeof y);
639 return 0;
642 static int
643 np_ushort(char *p, PyObject *v, const formatdef *f)
645 long x;
646 unsigned short y;
647 if (get_long(v, &x) < 0)
648 return -1;
649 if (x < 0 || x > USHRT_MAX){
650 PyErr_SetString(StructError,
651 "short format requires 0 <= number <= " STRINGIFY(USHRT_MAX));
652 return -1;
654 y = (unsigned short)x;
655 memcpy(p, (char *)&y, sizeof y);
656 return 0;
659 static int
660 np_int(char *p, PyObject *v, const formatdef *f)
662 long x;
663 int y;
664 if (get_long(v, &x) < 0)
665 return -1;
666 #if (SIZEOF_LONG > SIZEOF_INT)
667 if ((x < ((long)INT_MIN)) || (x > ((long)INT_MAX)))
668 RANGE_ERROR(x, f, 0, -1);
669 #endif
670 y = (int)x;
671 memcpy(p, (char *)&y, sizeof y);
672 return 0;
675 static int
676 np_uint(char *p, PyObject *v, const formatdef *f)
678 unsigned long x;
679 unsigned int y;
680 if (get_wrapped_ulong(v, &x) < 0)
681 return -1;
682 y = (unsigned int)x;
683 #if (SIZEOF_LONG > SIZEOF_INT)
684 if (x > ((unsigned long)UINT_MAX))
685 RANGE_ERROR(y, f, 1, -1);
686 #endif
687 memcpy(p, (char *)&y, sizeof y);
688 return 0;
691 static int
692 np_long(char *p, PyObject *v, const formatdef *f)
694 long x;
695 if (get_long(v, &x) < 0)
696 return -1;
697 memcpy(p, (char *)&x, sizeof x);
698 return 0;
701 static int
702 np_ulong(char *p, PyObject *v, const formatdef *f)
704 unsigned long x;
705 if (get_wrapped_ulong(v, &x) < 0)
706 return -1;
707 memcpy(p, (char *)&x, sizeof x);
708 return 0;
711 #ifdef HAVE_LONG_LONG
713 static int
714 np_longlong(char *p, PyObject *v, const formatdef *f)
716 PY_LONG_LONG x;
717 if (get_longlong(v, &x) < 0)
718 return -1;
719 memcpy(p, (char *)&x, sizeof x);
720 return 0;
723 static int
724 np_ulonglong(char *p, PyObject *v, const formatdef *f)
726 unsigned PY_LONG_LONG x;
727 if (get_ulonglong(v, &x) < 0)
728 return -1;
729 memcpy(p, (char *)&x, sizeof x);
730 return 0;
732 #endif
735 static int
736 np_bool(char *p, PyObject *v, const formatdef *f)
738 BOOL_TYPE y;
739 y = PyObject_IsTrue(v);
740 memcpy(p, (char *)&y, sizeof y);
741 return 0;
744 static int
745 np_float(char *p, PyObject *v, const formatdef *f)
747 float x = (float)PyFloat_AsDouble(v);
748 if (x == -1 && PyErr_Occurred()) {
749 PyErr_SetString(StructError,
750 "required argument is not a float");
751 return -1;
753 memcpy(p, (char *)&x, sizeof x);
754 return 0;
757 static int
758 np_double(char *p, PyObject *v, const formatdef *f)
760 double x = PyFloat_AsDouble(v);
761 if (x == -1 && PyErr_Occurred()) {
762 PyErr_SetString(StructError,
763 "required argument is not a float");
764 return -1;
766 memcpy(p, (char *)&x, sizeof(double));
767 return 0;
770 static int
771 np_void_p(char *p, PyObject *v, const formatdef *f)
773 void *x;
775 v = get_pylong(v);
776 if (v == NULL)
777 return -1;
778 assert(PyLong_Check(v));
779 x = PyLong_AsVoidPtr(v);
780 Py_DECREF(v);
781 if (x == NULL && PyErr_Occurred())
782 return -1;
783 memcpy(p, (char *)&x, sizeof x);
784 return 0;
787 static formatdef native_table[] = {
788 {'x', sizeof(char), 0, NULL},
789 {'b', sizeof(char), 0, nu_byte, np_byte},
790 {'B', sizeof(char), 0, nu_ubyte, np_ubyte},
791 {'c', sizeof(char), 0, nu_char, np_char},
792 {'s', sizeof(char), 0, NULL},
793 {'p', sizeof(char), 0, NULL},
794 {'h', sizeof(short), SHORT_ALIGN, nu_short, np_short},
795 {'H', sizeof(short), SHORT_ALIGN, nu_ushort, np_ushort},
796 {'i', sizeof(int), INT_ALIGN, nu_int, np_int},
797 {'I', sizeof(int), INT_ALIGN, nu_uint, np_uint},
798 {'l', sizeof(long), LONG_ALIGN, nu_long, np_long},
799 {'L', sizeof(long), LONG_ALIGN, nu_ulong, np_ulong},
800 #ifdef HAVE_LONG_LONG
801 {'q', sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_longlong, np_longlong},
802 {'Q', sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_ulonglong,np_ulonglong},
803 #endif
804 {'?', sizeof(BOOL_TYPE), BOOL_ALIGN, nu_bool, np_bool},
805 {'f', sizeof(float), FLOAT_ALIGN, nu_float, np_float},
806 {'d', sizeof(double), DOUBLE_ALIGN, nu_double, np_double},
807 {'P', sizeof(void *), VOID_P_ALIGN, nu_void_p, np_void_p},
811 /* Big-endian routines. *****************************************************/
813 static PyObject *
814 bu_int(const char *p, const formatdef *f)
816 long x = 0;
817 Py_ssize_t i = f->size;
818 const unsigned char *bytes = (const unsigned char *)p;
819 do {
820 x = (x<<8) | *bytes++;
821 } while (--i > 0);
822 /* Extend the sign bit. */
823 if (SIZEOF_LONG > f->size)
824 x |= -(x & (1L << ((8 * f->size) - 1)));
825 return PyInt_FromLong(x);
828 static PyObject *
829 bu_uint(const char *p, const formatdef *f)
831 unsigned long x = 0;
832 Py_ssize_t i = f->size;
833 const unsigned char *bytes = (const unsigned char *)p;
834 do {
835 x = (x<<8) | *bytes++;
836 } while (--i > 0);
837 if (x <= LONG_MAX)
838 return PyInt_FromLong((long)x);
839 return PyLong_FromUnsignedLong(x);
842 static PyObject *
843 bu_longlong(const char *p, const formatdef *f)
845 #ifdef HAVE_LONG_LONG
846 PY_LONG_LONG x = 0;
847 Py_ssize_t i = f->size;
848 const unsigned char *bytes = (const unsigned char *)p;
849 do {
850 x = (x<<8) | *bytes++;
851 } while (--i > 0);
852 /* Extend the sign bit. */
853 if (SIZEOF_LONG_LONG > f->size)
854 x |= -(x & ((PY_LONG_LONG)1 << ((8 * f->size) - 1)));
855 if (x >= LONG_MIN && x <= LONG_MAX)
856 return PyInt_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
857 return PyLong_FromLongLong(x);
858 #else
859 return _PyLong_FromByteArray((const unsigned char *)p,
861 0, /* little-endian */
862 1 /* signed */);
863 #endif
866 static PyObject *
867 bu_ulonglong(const char *p, const formatdef *f)
869 #ifdef HAVE_LONG_LONG
870 unsigned PY_LONG_LONG x = 0;
871 Py_ssize_t i = f->size;
872 const unsigned char *bytes = (const unsigned char *)p;
873 do {
874 x = (x<<8) | *bytes++;
875 } while (--i > 0);
876 if (x <= LONG_MAX)
877 return PyInt_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
878 return PyLong_FromUnsignedLongLong(x);
879 #else
880 return _PyLong_FromByteArray((const unsigned char *)p,
882 0, /* little-endian */
883 0 /* signed */);
884 #endif
887 static PyObject *
888 bu_float(const char *p, const formatdef *f)
890 return unpack_float(p, 0);
893 static PyObject *
894 bu_double(const char *p, const formatdef *f)
896 return unpack_double(p, 0);
899 static PyObject *
900 bu_bool(const char *p, const formatdef *f)
902 char x;
903 memcpy((char *)&x, p, sizeof x);
904 return PyBool_FromLong(x != 0);
907 static int
908 bp_int(char *p, PyObject *v, const formatdef *f)
910 long x;
911 Py_ssize_t i;
912 if (get_wrapped_long(v, &x) < 0)
913 return -1;
914 i = f->size;
915 if (i != SIZEOF_LONG) {
916 if ((i == 2) && (x < -32768 || x > 32767))
917 RANGE_ERROR(x, f, 0, 0xffffL);
918 #if (SIZEOF_LONG != 4)
919 else if ((i == 4) && (x < -2147483648L || x > 2147483647L))
920 RANGE_ERROR(x, f, 0, 0xffffffffL);
921 #endif
922 #ifdef PY_STRUCT_OVERFLOW_MASKING
923 else if ((i == 1) && (x < -128 || x > 127))
924 RANGE_ERROR(x, f, 0, 0xffL);
925 #endif
927 do {
928 p[--i] = (char)x;
929 x >>= 8;
930 } while (i > 0);
931 return 0;
934 static int
935 bp_uint(char *p, PyObject *v, const formatdef *f)
937 unsigned long x;
938 Py_ssize_t i;
939 if (get_wrapped_ulong(v, &x) < 0)
940 return -1;
941 i = f->size;
942 if (i != SIZEOF_LONG) {
943 unsigned long maxint = 1;
944 maxint <<= (unsigned long)(i * 8);
945 if (x >= maxint)
946 RANGE_ERROR(x, f, 1, maxint - 1);
948 do {
949 p[--i] = (char)x;
950 x >>= 8;
951 } while (i > 0);
952 return 0;
955 static int
956 bp_longlong(char *p, PyObject *v, const formatdef *f)
958 int res;
959 v = get_pylong(v);
960 if (v == NULL)
961 return -1;
962 res = _PyLong_AsByteArray((PyLongObject *)v,
963 (unsigned char *)p,
965 0, /* little_endian */
966 1 /* signed */);
967 Py_DECREF(v);
968 return res;
971 static int
972 bp_ulonglong(char *p, PyObject *v, const formatdef *f)
974 int res;
975 v = get_pylong(v);
976 if (v == NULL)
977 return -1;
978 res = _PyLong_AsByteArray((PyLongObject *)v,
979 (unsigned char *)p,
981 0, /* little_endian */
982 0 /* signed */);
983 Py_DECREF(v);
984 return res;
987 static int
988 bp_float(char *p, PyObject *v, const formatdef *f)
990 double x = PyFloat_AsDouble(v);
991 if (x == -1 && PyErr_Occurred()) {
992 PyErr_SetString(StructError,
993 "required argument is not a float");
994 return -1;
996 return _PyFloat_Pack4(x, (unsigned char *)p, 0);
999 static int
1000 bp_double(char *p, PyObject *v, const formatdef *f)
1002 double x = PyFloat_AsDouble(v);
1003 if (x == -1 && PyErr_Occurred()) {
1004 PyErr_SetString(StructError,
1005 "required argument is not a float");
1006 return -1;
1008 return _PyFloat_Pack8(x, (unsigned char *)p, 0);
1011 static int
1012 bp_bool(char *p, PyObject *v, const formatdef *f)
1014 char y;
1015 y = PyObject_IsTrue(v);
1016 memcpy(p, (char *)&y, sizeof y);
1017 return 0;
1020 static formatdef bigendian_table[] = {
1021 {'x', 1, 0, NULL},
1022 #ifdef PY_STRUCT_OVERFLOW_MASKING
1023 /* Native packers do range checking without overflow masking. */
1024 {'b', 1, 0, nu_byte, bp_int},
1025 {'B', 1, 0, nu_ubyte, bp_uint},
1026 #else
1027 {'b', 1, 0, nu_byte, np_byte},
1028 {'B', 1, 0, nu_ubyte, np_ubyte},
1029 #endif
1030 {'c', 1, 0, nu_char, np_char},
1031 {'s', 1, 0, NULL},
1032 {'p', 1, 0, NULL},
1033 {'h', 2, 0, bu_int, bp_int},
1034 {'H', 2, 0, bu_uint, bp_uint},
1035 {'i', 4, 0, bu_int, bp_int},
1036 {'I', 4, 0, bu_uint, bp_uint},
1037 {'l', 4, 0, bu_int, bp_int},
1038 {'L', 4, 0, bu_uint, bp_uint},
1039 {'q', 8, 0, bu_longlong, bp_longlong},
1040 {'Q', 8, 0, bu_ulonglong, bp_ulonglong},
1041 {'?', 1, 0, bu_bool, bp_bool},
1042 {'f', 4, 0, bu_float, bp_float},
1043 {'d', 8, 0, bu_double, bp_double},
1047 /* Little-endian routines. *****************************************************/
1049 static PyObject *
1050 lu_int(const char *p, const formatdef *f)
1052 long x = 0;
1053 Py_ssize_t i = f->size;
1054 const unsigned char *bytes = (const unsigned char *)p;
1055 do {
1056 x = (x<<8) | bytes[--i];
1057 } while (i > 0);
1058 /* Extend the sign bit. */
1059 if (SIZEOF_LONG > f->size)
1060 x |= -(x & (1L << ((8 * f->size) - 1)));
1061 return PyInt_FromLong(x);
1064 static PyObject *
1065 lu_uint(const char *p, const formatdef *f)
1067 unsigned long x = 0;
1068 Py_ssize_t i = f->size;
1069 const unsigned char *bytes = (const unsigned char *)p;
1070 do {
1071 x = (x<<8) | bytes[--i];
1072 } while (i > 0);
1073 if (x <= LONG_MAX)
1074 return PyInt_FromLong((long)x);
1075 return PyLong_FromUnsignedLong((long)x);
1078 static PyObject *
1079 lu_longlong(const char *p, const formatdef *f)
1081 #ifdef HAVE_LONG_LONG
1082 PY_LONG_LONG x = 0;
1083 Py_ssize_t i = f->size;
1084 const unsigned char *bytes = (const unsigned char *)p;
1085 do {
1086 x = (x<<8) | bytes[--i];
1087 } while (i > 0);
1088 /* Extend the sign bit. */
1089 if (SIZEOF_LONG_LONG > f->size)
1090 x |= -(x & ((PY_LONG_LONG)1 << ((8 * f->size) - 1)));
1091 if (x >= LONG_MIN && x <= LONG_MAX)
1092 return PyInt_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
1093 return PyLong_FromLongLong(x);
1094 #else
1095 return _PyLong_FromByteArray((const unsigned char *)p,
1097 1, /* little-endian */
1098 1 /* signed */);
1099 #endif
1102 static PyObject *
1103 lu_ulonglong(const char *p, const formatdef *f)
1105 #ifdef HAVE_LONG_LONG
1106 unsigned PY_LONG_LONG x = 0;
1107 Py_ssize_t i = f->size;
1108 const unsigned char *bytes = (const unsigned char *)p;
1109 do {
1110 x = (x<<8) | bytes[--i];
1111 } while (i > 0);
1112 if (x <= LONG_MAX)
1113 return PyInt_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
1114 return PyLong_FromUnsignedLongLong(x);
1115 #else
1116 return _PyLong_FromByteArray((const unsigned char *)p,
1118 1, /* little-endian */
1119 0 /* signed */);
1120 #endif
1123 static PyObject *
1124 lu_float(const char *p, const formatdef *f)
1126 return unpack_float(p, 1);
1129 static PyObject *
1130 lu_double(const char *p, const formatdef *f)
1132 return unpack_double(p, 1);
1135 static int
1136 lp_int(char *p, PyObject *v, const formatdef *f)
1138 long x;
1139 Py_ssize_t i;
1140 if (get_wrapped_long(v, &x) < 0)
1141 return -1;
1142 i = f->size;
1143 if (i != SIZEOF_LONG) {
1144 if ((i == 2) && (x < -32768 || x > 32767))
1145 RANGE_ERROR(x, f, 0, 0xffffL);
1146 #if (SIZEOF_LONG != 4)
1147 else if ((i == 4) && (x < -2147483648L || x > 2147483647L))
1148 RANGE_ERROR(x, f, 0, 0xffffffffL);
1149 #endif
1150 #ifdef PY_STRUCT_OVERFLOW_MASKING
1151 else if ((i == 1) && (x < -128 || x > 127))
1152 RANGE_ERROR(x, f, 0, 0xffL);
1153 #endif
1155 do {
1156 *p++ = (char)x;
1157 x >>= 8;
1158 } while (--i > 0);
1159 return 0;
1162 static int
1163 lp_uint(char *p, PyObject *v, const formatdef *f)
1165 unsigned long x;
1166 Py_ssize_t i;
1167 if (get_wrapped_ulong(v, &x) < 0)
1168 return -1;
1169 i = f->size;
1170 if (i != SIZEOF_LONG) {
1171 unsigned long maxint = 1;
1172 maxint <<= (unsigned long)(i * 8);
1173 if (x >= maxint)
1174 RANGE_ERROR(x, f, 1, maxint - 1);
1176 do {
1177 *p++ = (char)x;
1178 x >>= 8;
1179 } while (--i > 0);
1180 return 0;
1183 static int
1184 lp_longlong(char *p, PyObject *v, const formatdef *f)
1186 int res;
1187 v = get_pylong(v);
1188 if (v == NULL)
1189 return -1;
1190 res = _PyLong_AsByteArray((PyLongObject*)v,
1191 (unsigned char *)p,
1193 1, /* little_endian */
1194 1 /* signed */);
1195 Py_DECREF(v);
1196 return res;
1199 static int
1200 lp_ulonglong(char *p, PyObject *v, const formatdef *f)
1202 int res;
1203 v = get_pylong(v);
1204 if (v == NULL)
1205 return -1;
1206 res = _PyLong_AsByteArray((PyLongObject*)v,
1207 (unsigned char *)p,
1209 1, /* little_endian */
1210 0 /* signed */);
1211 Py_DECREF(v);
1212 return res;
1215 static int
1216 lp_float(char *p, PyObject *v, const formatdef *f)
1218 double x = PyFloat_AsDouble(v);
1219 if (x == -1 && PyErr_Occurred()) {
1220 PyErr_SetString(StructError,
1221 "required argument is not a float");
1222 return -1;
1224 return _PyFloat_Pack4(x, (unsigned char *)p, 1);
1227 static int
1228 lp_double(char *p, PyObject *v, const formatdef *f)
1230 double x = PyFloat_AsDouble(v);
1231 if (x == -1 && PyErr_Occurred()) {
1232 PyErr_SetString(StructError,
1233 "required argument is not a float");
1234 return -1;
1236 return _PyFloat_Pack8(x, (unsigned char *)p, 1);
1239 static formatdef lilendian_table[] = {
1240 {'x', 1, 0, NULL},
1241 #ifdef PY_STRUCT_OVERFLOW_MASKING
1242 /* Native packers do range checking without overflow masking. */
1243 {'b', 1, 0, nu_byte, lp_int},
1244 {'B', 1, 0, nu_ubyte, lp_uint},
1245 #else
1246 {'b', 1, 0, nu_byte, np_byte},
1247 {'B', 1, 0, nu_ubyte, np_ubyte},
1248 #endif
1249 {'c', 1, 0, nu_char, np_char},
1250 {'s', 1, 0, NULL},
1251 {'p', 1, 0, NULL},
1252 {'h', 2, 0, lu_int, lp_int},
1253 {'H', 2, 0, lu_uint, lp_uint},
1254 {'i', 4, 0, lu_int, lp_int},
1255 {'I', 4, 0, lu_uint, lp_uint},
1256 {'l', 4, 0, lu_int, lp_int},
1257 {'L', 4, 0, lu_uint, lp_uint},
1258 {'q', 8, 0, lu_longlong, lp_longlong},
1259 {'Q', 8, 0, lu_ulonglong, lp_ulonglong},
1260 {'?', 1, 0, bu_bool, bp_bool}, /* Std rep not endian dep,
1261 but potentially different from native rep -- reuse bx_bool funcs. */
1262 {'f', 4, 0, lu_float, lp_float},
1263 {'d', 8, 0, lu_double, lp_double},
1268 static const formatdef *
1269 whichtable(char **pfmt)
1271 const char *fmt = (*pfmt)++; /* May be backed out of later */
1272 switch (*fmt) {
1273 case '<':
1274 return lilendian_table;
1275 case '>':
1276 case '!': /* Network byte order is big-endian */
1277 return bigendian_table;
1278 case '=': { /* Host byte order -- different from native in aligment! */
1279 int n = 1;
1280 char *p = (char *) &n;
1281 if (*p == 1)
1282 return lilendian_table;
1283 else
1284 return bigendian_table;
1286 default:
1287 --*pfmt; /* Back out of pointer increment */
1288 /* Fall through */
1289 case '@':
1290 return native_table;
1295 /* Get the table entry for a format code */
1297 static const formatdef *
1298 getentry(int c, const formatdef *f)
1300 for (; f->format != '\0'; f++) {
1301 if (f->format == c) {
1302 return f;
1305 PyErr_SetString(StructError, "bad char in struct format");
1306 return NULL;
1310 /* Align a size according to a format code */
1312 static int
1313 align(Py_ssize_t size, char c, const formatdef *e)
1315 if (e->format == c) {
1316 if (e->alignment) {
1317 size = ((size + e->alignment - 1)
1318 / e->alignment)
1319 * e->alignment;
1322 return size;
1326 /* calculate the size of a format string */
1328 static int
1329 prepare_s(PyStructObject *self)
1331 const formatdef *f;
1332 const formatdef *e;
1333 formatcode *codes;
1335 const char *s;
1336 const char *fmt;
1337 char c;
1338 Py_ssize_t size, len, num, itemsize, x;
1340 fmt = PyString_AS_STRING(self->s_format);
1342 f = whichtable((char **)&fmt);
1344 s = fmt;
1345 size = 0;
1346 len = 0;
1347 while ((c = *s++) != '\0') {
1348 if (isspace(Py_CHARMASK(c)))
1349 continue;
1350 if ('0' <= c && c <= '9') {
1351 num = c - '0';
1352 while ('0' <= (c = *s++) && c <= '9') {
1353 x = num*10 + (c - '0');
1354 if (x/10 != num) {
1355 PyErr_SetString(
1356 StructError,
1357 "overflow in item count");
1358 return -1;
1360 num = x;
1362 if (c == '\0')
1363 break;
1365 else
1366 num = 1;
1368 e = getentry(c, f);
1369 if (e == NULL)
1370 return -1;
1372 switch (c) {
1373 case 's': /* fall through */
1374 case 'p': len++; break;
1375 case 'x': break;
1376 default: len += num; break;
1379 itemsize = e->size;
1380 size = align(size, c, e);
1381 x = num * itemsize;
1382 size += x;
1383 if (x/itemsize != num || size < 0) {
1384 PyErr_SetString(StructError,
1385 "total struct size too long");
1386 return -1;
1390 /* check for overflow */
1391 if ((len + 1) > (PY_SSIZE_T_MAX / sizeof(formatcode))) {
1392 PyErr_NoMemory();
1393 return -1;
1396 self->s_size = size;
1397 self->s_len = len;
1398 codes = PyMem_MALLOC((len + 1) * sizeof(formatcode));
1399 if (codes == NULL) {
1400 PyErr_NoMemory();
1401 return -1;
1403 self->s_codes = codes;
1405 s = fmt;
1406 size = 0;
1407 while ((c = *s++) != '\0') {
1408 if (isspace(Py_CHARMASK(c)))
1409 continue;
1410 if ('0' <= c && c <= '9') {
1411 num = c - '0';
1412 while ('0' <= (c = *s++) && c <= '9')
1413 num = num*10 + (c - '0');
1414 if (c == '\0')
1415 break;
1417 else
1418 num = 1;
1420 e = getentry(c, f);
1422 size = align(size, c, e);
1423 if (c == 's' || c == 'p') {
1424 codes->offset = size;
1425 codes->size = num;
1426 codes->fmtdef = e;
1427 codes++;
1428 size += num;
1429 } else if (c == 'x') {
1430 size += num;
1431 } else {
1432 while (--num >= 0) {
1433 codes->offset = size;
1434 codes->size = e->size;
1435 codes->fmtdef = e;
1436 codes++;
1437 size += e->size;
1441 codes->fmtdef = NULL;
1442 codes->offset = size;
1443 codes->size = 0;
1445 return 0;
1448 static PyObject *
1449 s_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1451 PyObject *self;
1453 assert(type != NULL && type->tp_alloc != NULL);
1455 self = type->tp_alloc(type, 0);
1456 if (self != NULL) {
1457 PyStructObject *s = (PyStructObject*)self;
1458 Py_INCREF(Py_None);
1459 s->s_format = Py_None;
1460 s->s_codes = NULL;
1461 s->s_size = -1;
1462 s->s_len = -1;
1464 return self;
1467 static int
1468 s_init(PyObject *self, PyObject *args, PyObject *kwds)
1470 PyStructObject *soself = (PyStructObject *)self;
1471 PyObject *o_format = NULL;
1472 int ret = 0;
1473 static char *kwlist[] = {"format", 0};
1475 assert(PyStruct_Check(self));
1477 if (!PyArg_ParseTupleAndKeywords(args, kwds, "S:Struct", kwlist,
1478 &o_format))
1479 return -1;
1481 Py_INCREF(o_format);
1482 Py_CLEAR(soself->s_format);
1483 soself->s_format = o_format;
1485 ret = prepare_s(soself);
1486 return ret;
1489 static void
1490 s_dealloc(PyStructObject *s)
1492 if (s->weakreflist != NULL)
1493 PyObject_ClearWeakRefs((PyObject *)s);
1494 if (s->s_codes != NULL) {
1495 PyMem_FREE(s->s_codes);
1497 Py_XDECREF(s->s_format);
1498 Py_TYPE(s)->tp_free((PyObject *)s);
1501 static PyObject *
1502 s_unpack_internal(PyStructObject *soself, char *startfrom) {
1503 formatcode *code;
1504 Py_ssize_t i = 0;
1505 PyObject *result = PyTuple_New(soself->s_len);
1506 if (result == NULL)
1507 return NULL;
1509 for (code = soself->s_codes; code->fmtdef != NULL; code++) {
1510 PyObject *v;
1511 const formatdef *e = code->fmtdef;
1512 const char *res = startfrom + code->offset;
1513 if (e->format == 's') {
1514 v = PyString_FromStringAndSize(res, code->size);
1515 } else if (e->format == 'p') {
1516 Py_ssize_t n = *(unsigned char*)res;
1517 if (n >= code->size)
1518 n = code->size - 1;
1519 v = PyString_FromStringAndSize(res + 1, n);
1520 } else {
1521 v = e->unpack(res, e);
1523 if (v == NULL)
1524 goto fail;
1525 PyTuple_SET_ITEM(result, i++, v);
1528 return result;
1529 fail:
1530 Py_DECREF(result);
1531 return NULL;
1535 PyDoc_STRVAR(s_unpack__doc__,
1536 "S.unpack(str) -> (v1, v2, ...)\n\
1538 Return tuple containing values unpacked according to this Struct's format.\n\
1539 Requires len(str) == self.size. See struct.__doc__ for more on format\n\
1540 strings.");
1542 static PyObject *
1543 s_unpack(PyObject *self, PyObject *inputstr)
1545 char *start;
1546 Py_ssize_t len;
1547 PyObject *args=NULL, *result;
1548 PyStructObject *soself = (PyStructObject *)self;
1549 assert(PyStruct_Check(self));
1550 assert(soself->s_codes != NULL);
1551 if (inputstr == NULL)
1552 goto fail;
1553 if (PyString_Check(inputstr) &&
1554 PyString_GET_SIZE(inputstr) == soself->s_size) {
1555 return s_unpack_internal(soself, PyString_AS_STRING(inputstr));
1557 args = PyTuple_Pack(1, inputstr);
1558 if (args == NULL)
1559 return NULL;
1560 if (!PyArg_ParseTuple(args, "s#:unpack", &start, &len))
1561 goto fail;
1562 if (soself->s_size != len)
1563 goto fail;
1564 result = s_unpack_internal(soself, start);
1565 Py_DECREF(args);
1566 return result;
1568 fail:
1569 Py_XDECREF(args);
1570 PyErr_Format(StructError,
1571 "unpack requires a string argument of length %zd",
1572 soself->s_size);
1573 return NULL;
1576 PyDoc_STRVAR(s_unpack_from__doc__,
1577 "S.unpack_from(buffer[, offset]) -> (v1, v2, ...)\n\
1579 Return tuple containing values unpacked according to this Struct's format.\n\
1580 Unlike unpack, unpack_from can unpack values from any object supporting\n\
1581 the buffer API, not just str. Requires len(buffer[offset:]) >= self.size.\n\
1582 See struct.__doc__ for more on format strings.");
1584 static PyObject *
1585 s_unpack_from(PyObject *self, PyObject *args, PyObject *kwds)
1587 static char *kwlist[] = {"buffer", "offset", 0};
1588 #if (PY_VERSION_HEX < 0x02050000)
1589 static char *fmt = "z#|i:unpack_from";
1590 #else
1591 static char *fmt = "z#|n:unpack_from";
1592 #endif
1593 Py_ssize_t buffer_len = 0, offset = 0;
1594 char *buffer = NULL;
1595 PyStructObject *soself = (PyStructObject *)self;
1596 assert(PyStruct_Check(self));
1597 assert(soself->s_codes != NULL);
1599 if (!PyArg_ParseTupleAndKeywords(args, kwds, fmt, kwlist,
1600 &buffer, &buffer_len, &offset))
1601 return NULL;
1603 if (buffer == NULL) {
1604 PyErr_Format(StructError,
1605 "unpack_from requires a buffer argument");
1606 return NULL;
1609 if (offset < 0)
1610 offset += buffer_len;
1612 if (offset < 0 || (buffer_len - offset) < soself->s_size) {
1613 PyErr_Format(StructError,
1614 "unpack_from requires a buffer of at least %zd bytes",
1615 soself->s_size);
1616 return NULL;
1618 return s_unpack_internal(soself, buffer + offset);
1623 * Guts of the pack function.
1625 * Takes a struct object, a tuple of arguments, and offset in that tuple of
1626 * argument for where to start processing the arguments for packing, and a
1627 * character buffer for writing the packed string. The caller must insure
1628 * that the buffer may contain the required length for packing the arguments.
1629 * 0 is returned on success, 1 is returned if there is an error.
1632 static int
1633 s_pack_internal(PyStructObject *soself, PyObject *args, int offset, char* buf)
1635 formatcode *code;
1636 /* XXX(nnorwitz): why does i need to be a local? can we use
1637 the offset parameter or do we need the wider width? */
1638 Py_ssize_t i;
1640 memset(buf, '\0', soself->s_size);
1641 i = offset;
1642 for (code = soself->s_codes; code->fmtdef != NULL; code++) {
1643 Py_ssize_t n;
1644 PyObject *v = PyTuple_GET_ITEM(args, i++);
1645 const formatdef *e = code->fmtdef;
1646 char *res = buf + code->offset;
1647 if (e->format == 's') {
1648 if (!PyString_Check(v)) {
1649 PyErr_SetString(StructError,
1650 "argument for 's' must be a string");
1651 return -1;
1653 n = PyString_GET_SIZE(v);
1654 if (n > code->size)
1655 n = code->size;
1656 if (n > 0)
1657 memcpy(res, PyString_AS_STRING(v), n);
1658 } else if (e->format == 'p') {
1659 if (!PyString_Check(v)) {
1660 PyErr_SetString(StructError,
1661 "argument for 'p' must be a string");
1662 return -1;
1664 n = PyString_GET_SIZE(v);
1665 if (n > (code->size - 1))
1666 n = code->size - 1;
1667 if (n > 0)
1668 memcpy(res + 1, PyString_AS_STRING(v), n);
1669 if (n > 255)
1670 n = 255;
1671 *res = Py_SAFE_DOWNCAST(n, Py_ssize_t, unsigned char);
1672 } else {
1673 if (e->pack(res, v, e) < 0) {
1674 if (PyLong_Check(v) && PyErr_ExceptionMatches(PyExc_OverflowError))
1675 PyErr_SetString(StructError,
1676 "long too large to convert to int");
1677 return -1;
1682 /* Success */
1683 return 0;
1687 PyDoc_STRVAR(s_pack__doc__,
1688 "S.pack(v1, v2, ...) -> string\n\
1690 Return a string containing values v1, v2, ... packed according to this\n\
1691 Struct's format. See struct.__doc__ for more on format strings.");
1693 static PyObject *
1694 s_pack(PyObject *self, PyObject *args)
1696 PyStructObject *soself;
1697 PyObject *result;
1699 /* Validate arguments. */
1700 soself = (PyStructObject *)self;
1701 assert(PyStruct_Check(self));
1702 assert(soself->s_codes != NULL);
1703 if (PyTuple_GET_SIZE(args) != soself->s_len)
1705 PyErr_Format(StructError,
1706 "pack requires exactly %zd arguments", soself->s_len);
1707 return NULL;
1710 /* Allocate a new string */
1711 result = PyString_FromStringAndSize((char *)NULL, soself->s_size);
1712 if (result == NULL)
1713 return NULL;
1715 /* Call the guts */
1716 if ( s_pack_internal(soself, args, 0, PyString_AS_STRING(result)) != 0 ) {
1717 Py_DECREF(result);
1718 return NULL;
1721 return result;
1724 PyDoc_STRVAR(s_pack_into__doc__,
1725 "S.pack_into(buffer, offset, v1, v2, ...)\n\
1727 Pack the values v1, v2, ... according to this Struct's format, write \n\
1728 the packed bytes into the writable buffer buf starting at offset. Note\n\
1729 that the offset is not an optional argument. See struct.__doc__ for \n\
1730 more on format strings.");
1732 static PyObject *
1733 s_pack_into(PyObject *self, PyObject *args)
1735 PyStructObject *soself;
1736 char *buffer;
1737 Py_ssize_t buffer_len, offset;
1739 /* Validate arguments. +1 is for the first arg as buffer. */
1740 soself = (PyStructObject *)self;
1741 assert(PyStruct_Check(self));
1742 assert(soself->s_codes != NULL);
1743 if (PyTuple_GET_SIZE(args) != (soself->s_len + 2))
1745 PyErr_Format(StructError,
1746 "pack_into requires exactly %zd arguments",
1747 (soself->s_len + 2));
1748 return NULL;
1751 /* Extract a writable memory buffer from the first argument */
1752 if ( PyObject_AsWriteBuffer(PyTuple_GET_ITEM(args, 0),
1753 (void**)&buffer, &buffer_len) == -1 ) {
1754 return NULL;
1756 assert( buffer_len >= 0 );
1758 /* Extract the offset from the first argument */
1759 offset = PyInt_AsSsize_t(PyTuple_GET_ITEM(args, 1));
1760 if (offset == -1 && PyErr_Occurred())
1761 return NULL;
1763 /* Support negative offsets. */
1764 if (offset < 0)
1765 offset += buffer_len;
1767 /* Check boundaries */
1768 if (offset < 0 || (buffer_len - offset) < soself->s_size) {
1769 PyErr_Format(StructError,
1770 "pack_into requires a buffer of at least %zd bytes",
1771 soself->s_size);
1772 return NULL;
1775 /* Call the guts */
1776 if ( s_pack_internal(soself, args, 2, buffer + offset) != 0 ) {
1777 return NULL;
1780 Py_RETURN_NONE;
1783 static PyObject *
1784 s_get_format(PyStructObject *self, void *unused)
1786 Py_INCREF(self->s_format);
1787 return self->s_format;
1790 static PyObject *
1791 s_get_size(PyStructObject *self, void *unused)
1793 return PyInt_FromSsize_t(self->s_size);
1796 /* List of functions */
1798 static struct PyMethodDef s_methods[] = {
1799 {"pack", s_pack, METH_VARARGS, s_pack__doc__},
1800 {"pack_into", s_pack_into, METH_VARARGS, s_pack_into__doc__},
1801 {"unpack", s_unpack, METH_O, s_unpack__doc__},
1802 {"unpack_from", (PyCFunction)s_unpack_from, METH_VARARGS|METH_KEYWORDS,
1803 s_unpack_from__doc__},
1804 {NULL, NULL} /* sentinel */
1807 PyDoc_STRVAR(s__doc__, "Compiled struct object");
1809 #define OFF(x) offsetof(PyStructObject, x)
1811 static PyGetSetDef s_getsetlist[] = {
1812 {"format", (getter)s_get_format, (setter)NULL, "struct format string", NULL},
1813 {"size", (getter)s_get_size, (setter)NULL, "struct size in bytes", NULL},
1814 {NULL} /* sentinel */
1817 static
1818 PyTypeObject PyStructType = {
1819 PyVarObject_HEAD_INIT(NULL, 0)
1820 "Struct",
1821 sizeof(PyStructObject),
1823 (destructor)s_dealloc, /* tp_dealloc */
1824 0, /* tp_print */
1825 0, /* tp_getattr */
1826 0, /* tp_setattr */
1827 0, /* tp_compare */
1828 0, /* tp_repr */
1829 0, /* tp_as_number */
1830 0, /* tp_as_sequence */
1831 0, /* tp_as_mapping */
1832 0, /* tp_hash */
1833 0, /* tp_call */
1834 0, /* tp_str */
1835 PyObject_GenericGetAttr, /* tp_getattro */
1836 PyObject_GenericSetAttr, /* tp_setattro */
1837 0, /* tp_as_buffer */
1838 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_WEAKREFS,/* tp_flags */
1839 s__doc__, /* tp_doc */
1840 0, /* tp_traverse */
1841 0, /* tp_clear */
1842 0, /* tp_richcompare */
1843 offsetof(PyStructObject, weakreflist), /* tp_weaklistoffset */
1844 0, /* tp_iter */
1845 0, /* tp_iternext */
1846 s_methods, /* tp_methods */
1847 NULL, /* tp_members */
1848 s_getsetlist, /* tp_getset */
1849 0, /* tp_base */
1850 0, /* tp_dict */
1851 0, /* tp_descr_get */
1852 0, /* tp_descr_set */
1853 0, /* tp_dictoffset */
1854 s_init, /* tp_init */
1855 PyType_GenericAlloc,/* tp_alloc */
1856 s_new, /* tp_new */
1857 PyObject_Del, /* tp_free */
1861 /* ---- Standalone functions ---- */
1863 #define MAXCACHE 100
1864 static PyObject *cache = NULL;
1866 static PyObject *
1867 cache_struct(PyObject *fmt)
1869 PyObject * s_object;
1871 if (cache == NULL) {
1872 cache = PyDict_New();
1873 if (cache == NULL)
1874 return NULL;
1877 s_object = PyDict_GetItem(cache, fmt);
1878 if (s_object != NULL) {
1879 Py_INCREF(s_object);
1880 return s_object;
1883 s_object = PyObject_CallFunctionObjArgs((PyObject *)(&PyStructType), fmt, NULL);
1884 if (s_object != NULL) {
1885 if (PyDict_Size(cache) >= MAXCACHE)
1886 PyDict_Clear(cache);
1887 /* Attempt to cache the result */
1888 if (PyDict_SetItem(cache, fmt, s_object) == -1)
1889 PyErr_Clear();
1891 return s_object;
1894 PyDoc_STRVAR(clearcache_doc,
1895 "Clear the internal cache.");
1897 static PyObject *
1898 clearcache(PyObject *self)
1900 Py_CLEAR(cache);
1901 Py_RETURN_NONE;
1904 PyDoc_STRVAR(calcsize_doc,
1905 "Return size of C struct described by format string fmt.");
1907 static PyObject *
1908 calcsize(PyObject *self, PyObject *fmt)
1910 Py_ssize_t n;
1911 PyObject *s_object = cache_struct(fmt);
1912 if (s_object == NULL)
1913 return NULL;
1914 n = ((PyStructObject *)s_object)->s_size;
1915 Py_DECREF(s_object);
1916 return PyInt_FromSsize_t(n);
1919 PyDoc_STRVAR(pack_doc,
1920 "Return string containing values v1, v2, ... packed according to fmt.");
1922 static PyObject *
1923 pack(PyObject *self, PyObject *args)
1925 PyObject *s_object, *fmt, *newargs, *result;
1926 Py_ssize_t n = PyTuple_GET_SIZE(args);
1928 if (n == 0) {
1929 PyErr_SetString(PyExc_TypeError, "missing format argument");
1930 return NULL;
1932 fmt = PyTuple_GET_ITEM(args, 0);
1933 newargs = PyTuple_GetSlice(args, 1, n);
1934 if (newargs == NULL)
1935 return NULL;
1937 s_object = cache_struct(fmt);
1938 if (s_object == NULL) {
1939 Py_DECREF(newargs);
1940 return NULL;
1942 result = s_pack(s_object, newargs);
1943 Py_DECREF(newargs);
1944 Py_DECREF(s_object);
1945 return result;
1948 PyDoc_STRVAR(pack_into_doc,
1949 "Pack the values v1, v2, ... according to fmt.\n\
1950 Write the packed bytes into the writable buffer buf starting at offset.");
1952 static PyObject *
1953 pack_into(PyObject *self, PyObject *args)
1955 PyObject *s_object, *fmt, *newargs, *result;
1956 Py_ssize_t n = PyTuple_GET_SIZE(args);
1958 if (n == 0) {
1959 PyErr_SetString(PyExc_TypeError, "missing format argument");
1960 return NULL;
1962 fmt = PyTuple_GET_ITEM(args, 0);
1963 newargs = PyTuple_GetSlice(args, 1, n);
1964 if (newargs == NULL)
1965 return NULL;
1967 s_object = cache_struct(fmt);
1968 if (s_object == NULL) {
1969 Py_DECREF(newargs);
1970 return NULL;
1972 result = s_pack_into(s_object, newargs);
1973 Py_DECREF(newargs);
1974 Py_DECREF(s_object);
1975 return result;
1978 PyDoc_STRVAR(unpack_doc,
1979 "Unpack the string containing packed C structure data, according to fmt.\n\
1980 Requires len(string) == calcsize(fmt).");
1982 static PyObject *
1983 unpack(PyObject *self, PyObject *args)
1985 PyObject *s_object, *fmt, *inputstr, *result;
1987 if (!PyArg_UnpackTuple(args, "unpack", 2, 2, &fmt, &inputstr))
1988 return NULL;
1990 s_object = cache_struct(fmt);
1991 if (s_object == NULL)
1992 return NULL;
1993 result = s_unpack(s_object, inputstr);
1994 Py_DECREF(s_object);
1995 return result;
1998 PyDoc_STRVAR(unpack_from_doc,
1999 "Unpack the buffer, containing packed C structure data, according to\n\
2000 fmt, starting at offset. Requires len(buffer[offset:]) >= calcsize(fmt).");
2002 static PyObject *
2003 unpack_from(PyObject *self, PyObject *args, PyObject *kwds)
2005 PyObject *s_object, *fmt, *newargs, *result;
2006 Py_ssize_t n = PyTuple_GET_SIZE(args);
2008 if (n == 0) {
2009 PyErr_SetString(PyExc_TypeError, "missing format argument");
2010 return NULL;
2012 fmt = PyTuple_GET_ITEM(args, 0);
2013 newargs = PyTuple_GetSlice(args, 1, n);
2014 if (newargs == NULL)
2015 return NULL;
2017 s_object = cache_struct(fmt);
2018 if (s_object == NULL) {
2019 Py_DECREF(newargs);
2020 return NULL;
2022 result = s_unpack_from(s_object, newargs, kwds);
2023 Py_DECREF(newargs);
2024 Py_DECREF(s_object);
2025 return result;
2028 static struct PyMethodDef module_functions[] = {
2029 {"_clearcache", (PyCFunction)clearcache, METH_NOARGS, clearcache_doc},
2030 {"calcsize", calcsize, METH_O, calcsize_doc},
2031 {"pack", pack, METH_VARARGS, pack_doc},
2032 {"pack_into", pack_into, METH_VARARGS, pack_into_doc},
2033 {"unpack", unpack, METH_VARARGS, unpack_doc},
2034 {"unpack_from", (PyCFunction)unpack_from,
2035 METH_VARARGS|METH_KEYWORDS, unpack_from_doc},
2036 {NULL, NULL} /* sentinel */
2040 /* Module initialization */
2042 PyDoc_STRVAR(module_doc,
2043 "Functions to convert between Python values and C structs.\n\
2044 Python strings are used to hold the data representing the C struct\n\
2045 and also as format strings to describe the layout of data in the C struct.\n\
2047 The optional first format char indicates byte order, size and alignment:\n\
2048 @: native order, size & alignment (default)\n\
2049 =: native order, std. size & alignment\n\
2050 <: little-endian, std. size & alignment\n\
2051 >: big-endian, std. size & alignment\n\
2052 !: same as >\n\
2054 The remaining chars indicate types of args and must match exactly;\n\
2055 these can be preceded by a decimal repeat count:\n\
2056 x: pad byte (no data); c:char; b:signed byte; B:unsigned byte;\n\
2057 h:short; H:unsigned short; i:int; I:unsigned int;\n\
2058 l:long; L:unsigned long; f:float; d:double.\n\
2059 Special cases (preceding decimal count indicates length):\n\
2060 s:string (array of char); p: pascal string (with count byte).\n\
2061 Special case (only available in native format):\n\
2062 P:an integer type that is wide enough to hold a pointer.\n\
2063 Special case (not in native mode unless 'long long' in platform C):\n\
2064 q:long long; Q:unsigned long long\n\
2065 Whitespace between formats is ignored.\n\
2067 The variable struct.error is an exception raised on errors.\n");
2069 PyMODINIT_FUNC
2070 init_struct(void)
2072 PyObject *ver, *m;
2074 ver = PyString_FromString("0.2");
2075 if (ver == NULL)
2076 return;
2078 m = Py_InitModule3("_struct", module_functions, module_doc);
2079 if (m == NULL)
2080 return;
2082 Py_TYPE(&PyStructType) = &PyType_Type;
2083 if (PyType_Ready(&PyStructType) < 0)
2084 return;
2086 #ifdef PY_STRUCT_OVERFLOW_MASKING
2087 if (pyint_zero == NULL) {
2088 pyint_zero = PyInt_FromLong(0);
2089 if (pyint_zero == NULL)
2090 return;
2092 if (pylong_ulong_mask == NULL) {
2093 #if (SIZEOF_LONG == 4)
2094 pylong_ulong_mask = PyLong_FromString("FFFFFFFF", NULL, 16);
2095 #else
2096 pylong_ulong_mask = PyLong_FromString("FFFFFFFFFFFFFFFF", NULL, 16);
2097 #endif
2098 if (pylong_ulong_mask == NULL)
2099 return;
2102 #else
2103 /* This speed trick can't be used until overflow masking goes away, because
2104 native endian always raises exceptions instead of overflow masking. */
2106 /* Check endian and swap in faster functions */
2108 int one = 1;
2109 formatdef *native = native_table;
2110 formatdef *other, *ptr;
2111 if ((int)*(unsigned char*)&one)
2112 other = lilendian_table;
2113 else
2114 other = bigendian_table;
2115 /* Scan through the native table, find a matching
2116 entry in the endian table and swap in the
2117 native implementations whenever possible
2118 (64-bit platforms may not have "standard" sizes) */
2119 while (native->format != '\0' && other->format != '\0') {
2120 ptr = other;
2121 while (ptr->format != '\0') {
2122 if (ptr->format == native->format) {
2123 /* Match faster when formats are
2124 listed in the same order */
2125 if (ptr == other)
2126 other++;
2127 /* Only use the trick if the
2128 size matches */
2129 if (ptr->size != native->size)
2130 break;
2131 /* Skip float and double, could be
2132 "unknown" float format */
2133 if (ptr->format == 'd' || ptr->format == 'f')
2134 break;
2135 ptr->pack = native->pack;
2136 ptr->unpack = native->unpack;
2137 break;
2139 ptr++;
2141 native++;
2144 #endif
2146 /* Add some symbolic constants to the module */
2147 if (StructError == NULL) {
2148 StructError = PyErr_NewException("struct.error", NULL, NULL);
2149 if (StructError == NULL)
2150 return;
2153 Py_INCREF(StructError);
2154 PyModule_AddObject(m, "error", StructError);
2156 Py_INCREF((PyObject*)&PyStructType);
2157 PyModule_AddObject(m, "Struct", (PyObject*)&PyStructType);
2159 PyModule_AddObject(m, "__version__", ver);
2161 PyModule_AddIntConstant(m, "_PY_STRUCT_RANGE_CHECKING", 1);
2162 #ifdef PY_STRUCT_OVERFLOW_MASKING
2163 PyModule_AddIntConstant(m, "_PY_STRUCT_OVERFLOW_MASKING", 1);
2164 #endif
2165 #ifdef PY_STRUCT_FLOAT_COERCE
2166 PyModule_AddIntConstant(m, "_PY_STRUCT_FLOAT_COERCE", 1);
2167 #endif