More configure fixes: avoid sh 'integer argument expected' error when 'long long...
[python.git] / Modules / _struct.c
blob27f8881fc0cd385dc92562f13be2f1adea556cd7
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_FLOAT_COERCE is defined, the struct module will allow float
21 arguments for integer formats with a warning for backwards
22 compatibility. */
24 #define PY_STRUCT_FLOAT_COERCE 1
26 #ifdef PY_STRUCT_FLOAT_COERCE
27 #define FLOAT_COERCE "integer argument expected, got float"
28 #endif
31 /* The translation function for each format character is table driven */
32 typedef struct _formatdef {
33 char format;
34 Py_ssize_t size;
35 Py_ssize_t alignment;
36 PyObject* (*unpack)(const char *,
37 const struct _formatdef *);
38 int (*pack)(char *, PyObject *,
39 const struct _formatdef *);
40 } formatdef;
42 typedef struct _formatcode {
43 const struct _formatdef *fmtdef;
44 Py_ssize_t offset;
45 Py_ssize_t size;
46 } formatcode;
48 /* Struct object interface */
50 typedef struct {
51 PyObject_HEAD
52 Py_ssize_t s_size;
53 Py_ssize_t s_len;
54 formatcode *s_codes;
55 PyObject *s_format;
56 PyObject *weakreflist; /* List of weak references */
57 } PyStructObject;
60 #define PyStruct_Check(op) PyObject_TypeCheck(op, &PyStructType)
61 #define PyStruct_CheckExact(op) (Py_TYPE(op) == &PyStructType)
64 /* Exception */
66 static PyObject *StructError;
69 /* Define various structs to figure out the alignments of types */
72 typedef struct { char c; short x; } st_short;
73 typedef struct { char c; int x; } st_int;
74 typedef struct { char c; long x; } st_long;
75 typedef struct { char c; float x; } st_float;
76 typedef struct { char c; double x; } st_double;
77 typedef struct { char c; void *x; } st_void_p;
79 #define SHORT_ALIGN (sizeof(st_short) - sizeof(short))
80 #define INT_ALIGN (sizeof(st_int) - sizeof(int))
81 #define LONG_ALIGN (sizeof(st_long) - sizeof(long))
82 #define FLOAT_ALIGN (sizeof(st_float) - sizeof(float))
83 #define DOUBLE_ALIGN (sizeof(st_double) - sizeof(double))
84 #define VOID_P_ALIGN (sizeof(st_void_p) - sizeof(void *))
86 /* We can't support q and Q in native mode unless the compiler does;
87 in std mode, they're 8 bytes on all platforms. */
88 #ifdef HAVE_LONG_LONG
89 typedef struct { char c; PY_LONG_LONG x; } s_long_long;
90 #define LONG_LONG_ALIGN (sizeof(s_long_long) - sizeof(PY_LONG_LONG))
91 #endif
93 #ifdef HAVE_C99_BOOL
94 #define BOOL_TYPE _Bool
95 typedef struct { char c; _Bool x; } s_bool;
96 #define BOOL_ALIGN (sizeof(s_bool) - sizeof(BOOL_TYPE))
97 #else
98 #define BOOL_TYPE char
99 #define BOOL_ALIGN 0
100 #endif
102 #define STRINGIFY(x) #x
104 #ifdef __powerc
105 #pragma options align=reset
106 #endif
108 static char *integer_codes = "bBhHiIlLqQ";
110 /* Helper to get a PyLongObject by hook or by crook. Caller should decref. */
112 static PyObject *
113 get_pylong(PyObject *v)
115 assert(v != NULL);
116 if (PyInt_Check(v))
117 return PyLong_FromLong(PyInt_AS_LONG(v));
118 if (PyLong_Check(v)) {
119 Py_INCREF(v);
120 return v;
122 #ifdef PY_STRUCT_FLOAT_COERCE
123 if (PyFloat_Check(v)) {
124 if (PyErr_WarnEx(PyExc_DeprecationWarning, FLOAT_COERCE, 2)<0)
125 return NULL;
126 return PyNumber_Long(v);
128 #endif
129 PyErr_SetString(StructError,
130 "cannot convert argument to long");
131 return NULL;
134 /* Helper to convert a Python object to a C long. Sets an exception
135 (struct.error for an inconvertible type, OverflowError for
136 out-of-range values) and returns -1 on error. */
138 static int
139 get_long(PyObject *v, long *p)
141 long x;
143 v = get_pylong(v);
144 if (v == NULL)
145 return -1;
146 assert(PyLong_Check(v));
147 x = PyLong_AsLong(v);
148 Py_DECREF(v);
149 if (x == (long)-1 && PyErr_Occurred())
150 return -1;
151 *p = x;
152 return 0;
155 /* Same, but handling unsigned long */
157 static int
158 get_ulong(PyObject *v, unsigned long *p)
160 unsigned long x;
162 v = get_pylong(v);
163 if (v == NULL)
164 return -1;
165 assert(PyLong_Check(v));
166 x = PyLong_AsUnsignedLong(v);
167 Py_DECREF(v);
168 if (x == (unsigned long)-1 && PyErr_Occurred())
169 return -1;
170 *p = x;
171 return 0;
174 #ifdef HAVE_LONG_LONG
176 /* Same, but handling native long long. */
178 static int
179 get_longlong(PyObject *v, PY_LONG_LONG *p)
181 PY_LONG_LONG x;
183 v = get_pylong(v);
184 if (v == NULL)
185 return -1;
186 assert(PyLong_Check(v));
187 x = PyLong_AsLongLong(v);
188 Py_DECREF(v);
189 if (x == (PY_LONG_LONG)-1 && PyErr_Occurred())
190 return -1;
191 *p = x;
192 return 0;
195 /* Same, but handling native unsigned long long. */
197 static int
198 get_ulonglong(PyObject *v, unsigned PY_LONG_LONG *p)
200 unsigned PY_LONG_LONG x;
202 v = get_pylong(v);
203 if (v == NULL)
204 return -1;
205 assert(PyLong_Check(v));
206 x = PyLong_AsUnsignedLongLong(v);
207 Py_DECREF(v);
208 if (x == (unsigned PY_LONG_LONG)-1 && PyErr_Occurred())
209 return -1;
210 *p = x;
211 return 0;
214 #endif
216 /* Floating point helpers */
218 static PyObject *
219 unpack_float(const char *p, /* start of 4-byte string */
220 int le) /* true for little-endian, false for big-endian */
222 double x;
224 x = _PyFloat_Unpack4((unsigned char *)p, le);
225 if (x == -1.0 && PyErr_Occurred())
226 return NULL;
227 return PyFloat_FromDouble(x);
230 static PyObject *
231 unpack_double(const char *p, /* start of 8-byte string */
232 int le) /* true for little-endian, false for big-endian */
234 double x;
236 x = _PyFloat_Unpack8((unsigned char *)p, le);
237 if (x == -1.0 && PyErr_Occurred())
238 return NULL;
239 return PyFloat_FromDouble(x);
242 /* Helper to format the range error exceptions */
243 static int
244 _range_error(const formatdef *f, int is_unsigned)
246 /* ulargest is the largest unsigned value with f->size bytes.
247 * Note that the simpler:
248 * ((size_t)1 << (f->size * 8)) - 1
249 * doesn't work when f->size == sizeof(size_t) because C doesn't
250 * define what happens when a left shift count is >= the number of
251 * bits in the integer being shifted; e.g., on some boxes it doesn't
252 * shift at all when they're equal.
254 const size_t ulargest = (size_t)-1 >> ((SIZEOF_SIZE_T - f->size)*8);
255 assert(f->size >= 1 && f->size <= SIZEOF_SIZE_T);
256 if (is_unsigned)
257 PyErr_Format(StructError,
258 "'%c' format requires 0 <= number <= %zu",
259 f->format,
260 ulargest);
261 else {
262 const Py_ssize_t largest = (Py_ssize_t)(ulargest >> 1);
263 PyErr_Format(StructError,
264 "'%c' format requires %zd <= number <= %zd",
265 f->format,
266 ~ largest,
267 largest);
269 return -1;
274 /* A large number of small routines follow, with names of the form
276 [bln][up]_TYPE
278 [bln] distiguishes among big-endian, little-endian and native.
279 [pu] distiguishes between pack (to struct) and unpack (from struct).
280 TYPE is one of char, byte, ubyte, etc.
283 /* Native mode routines. ****************************************************/
284 /* NOTE:
285 In all n[up]_<type> routines handling types larger than 1 byte, there is
286 *no* guarantee that the p pointer is properly aligned for each type,
287 therefore memcpy is called. An intermediate variable is used to
288 compensate for big-endian architectures.
289 Normally both the intermediate variable and the memcpy call will be
290 skipped by C optimisation in little-endian architectures (gcc >= 2.91
291 does this). */
293 static PyObject *
294 nu_char(const char *p, const formatdef *f)
296 return PyString_FromStringAndSize(p, 1);
299 static PyObject *
300 nu_byte(const char *p, const formatdef *f)
302 return PyInt_FromLong((long) *(signed char *)p);
305 static PyObject *
306 nu_ubyte(const char *p, const formatdef *f)
308 return PyInt_FromLong((long) *(unsigned char *)p);
311 static PyObject *
312 nu_short(const char *p, const formatdef *f)
314 short x;
315 memcpy((char *)&x, p, sizeof x);
316 return PyInt_FromLong((long)x);
319 static PyObject *
320 nu_ushort(const char *p, const formatdef *f)
322 unsigned short x;
323 memcpy((char *)&x, p, sizeof x);
324 return PyInt_FromLong((long)x);
327 static PyObject *
328 nu_int(const char *p, const formatdef *f)
330 int x;
331 memcpy((char *)&x, p, sizeof x);
332 return PyInt_FromLong((long)x);
335 static PyObject *
336 nu_uint(const char *p, const formatdef *f)
338 unsigned int x;
339 memcpy((char *)&x, p, sizeof x);
340 #if (SIZEOF_LONG > SIZEOF_INT)
341 return PyInt_FromLong((long)x);
342 #else
343 if (x <= ((unsigned int)LONG_MAX))
344 return PyInt_FromLong((long)x);
345 return PyLong_FromUnsignedLong((unsigned long)x);
346 #endif
349 static PyObject *
350 nu_long(const char *p, const formatdef *f)
352 long x;
353 memcpy((char *)&x, p, sizeof x);
354 return PyInt_FromLong(x);
357 static PyObject *
358 nu_ulong(const char *p, const formatdef *f)
360 unsigned long x;
361 memcpy((char *)&x, p, sizeof x);
362 if (x <= LONG_MAX)
363 return PyInt_FromLong((long)x);
364 return PyLong_FromUnsignedLong(x);
367 /* Native mode doesn't support q or Q unless the platform C supports
368 long long (or, on Windows, __int64). */
370 #ifdef HAVE_LONG_LONG
372 static PyObject *
373 nu_longlong(const char *p, const formatdef *f)
375 PY_LONG_LONG x;
376 memcpy((char *)&x, p, sizeof x);
377 if (x >= LONG_MIN && x <= LONG_MAX)
378 return PyInt_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
379 return PyLong_FromLongLong(x);
382 static PyObject *
383 nu_ulonglong(const char *p, const formatdef *f)
385 unsigned PY_LONG_LONG x;
386 memcpy((char *)&x, p, sizeof x);
387 if (x <= LONG_MAX)
388 return PyInt_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
389 return PyLong_FromUnsignedLongLong(x);
392 #endif
394 static PyObject *
395 nu_bool(const char *p, const formatdef *f)
397 BOOL_TYPE x;
398 memcpy((char *)&x, p, sizeof x);
399 return PyBool_FromLong(x != 0);
403 static PyObject *
404 nu_float(const char *p, const formatdef *f)
406 float x;
407 memcpy((char *)&x, p, sizeof x);
408 return PyFloat_FromDouble((double)x);
411 static PyObject *
412 nu_double(const char *p, const formatdef *f)
414 double x;
415 memcpy((char *)&x, p, sizeof x);
416 return PyFloat_FromDouble(x);
419 static PyObject *
420 nu_void_p(const char *p, const formatdef *f)
422 void *x;
423 memcpy((char *)&x, p, sizeof x);
424 return PyLong_FromVoidPtr(x);
427 static int
428 np_byte(char *p, PyObject *v, const formatdef *f)
430 long x;
431 if (get_long(v, &x) < 0)
432 return -1;
433 if (x < -128 || x > 127){
434 PyErr_SetString(StructError,
435 "byte format requires -128 <= number <= 127");
436 return -1;
438 *p = (char)x;
439 return 0;
442 static int
443 np_ubyte(char *p, PyObject *v, const formatdef *f)
445 long x;
446 if (get_long(v, &x) < 0)
447 return -1;
448 if (x < 0 || x > 255){
449 PyErr_SetString(StructError,
450 "ubyte format requires 0 <= number <= 255");
451 return -1;
453 *p = (char)x;
454 return 0;
457 static int
458 np_char(char *p, PyObject *v, const formatdef *f)
460 if (!PyString_Check(v) || PyString_Size(v) != 1) {
461 PyErr_SetString(StructError,
462 "char format require string of length 1");
463 return -1;
465 *p = *PyString_AsString(v);
466 return 0;
469 static int
470 np_short(char *p, PyObject *v, const formatdef *f)
472 long x;
473 short y;
474 if (get_long(v, &x) < 0)
475 return -1;
476 if (x < SHRT_MIN || x > SHRT_MAX){
477 PyErr_SetString(StructError,
478 "short format requires " STRINGIFY(SHRT_MIN)
479 " <= number <= " STRINGIFY(SHRT_MAX));
480 return -1;
482 y = (short)x;
483 memcpy(p, (char *)&y, sizeof y);
484 return 0;
487 static int
488 np_ushort(char *p, PyObject *v, const formatdef *f)
490 long x;
491 unsigned short y;
492 if (get_long(v, &x) < 0)
493 return -1;
494 if (x < 0 || x > USHRT_MAX){
495 PyErr_SetString(StructError,
496 "ushort format requires 0 <= number <= " STRINGIFY(USHRT_MAX));
497 return -1;
499 y = (unsigned short)x;
500 memcpy(p, (char *)&y, sizeof y);
501 return 0;
504 static int
505 np_int(char *p, PyObject *v, const formatdef *f)
507 long x;
508 int y;
509 if (get_long(v, &x) < 0)
510 return -1;
511 #if (SIZEOF_LONG > SIZEOF_INT)
512 if ((x < ((long)INT_MIN)) || (x > ((long)INT_MAX)))
513 return _range_error(f, 0);
514 #endif
515 y = (int)x;
516 memcpy(p, (char *)&y, sizeof y);
517 return 0;
520 static int
521 np_uint(char *p, PyObject *v, const formatdef *f)
523 unsigned long x;
524 unsigned int y;
525 if (get_ulong(v, &x) < 0)
526 return -1;
527 y = (unsigned int)x;
528 #if (SIZEOF_LONG > SIZEOF_INT)
529 if (x > ((unsigned long)UINT_MAX))
530 return _range_error(f, 1);
531 #endif
532 memcpy(p, (char *)&y, sizeof y);
533 return 0;
536 static int
537 np_long(char *p, PyObject *v, const formatdef *f)
539 long x;
540 if (get_long(v, &x) < 0)
541 return -1;
542 memcpy(p, (char *)&x, sizeof x);
543 return 0;
546 static int
547 np_ulong(char *p, PyObject *v, const formatdef *f)
549 unsigned long x;
550 if (get_ulong(v, &x) < 0)
551 return -1;
552 memcpy(p, (char *)&x, sizeof x);
553 return 0;
556 #ifdef HAVE_LONG_LONG
558 static int
559 np_longlong(char *p, PyObject *v, const formatdef *f)
561 PY_LONG_LONG x;
562 if (get_longlong(v, &x) < 0)
563 return -1;
564 memcpy(p, (char *)&x, sizeof x);
565 return 0;
568 static int
569 np_ulonglong(char *p, PyObject *v, const formatdef *f)
571 unsigned PY_LONG_LONG x;
572 if (get_ulonglong(v, &x) < 0)
573 return -1;
574 memcpy(p, (char *)&x, sizeof x);
575 return 0;
577 #endif
580 static int
581 np_bool(char *p, PyObject *v, const formatdef *f)
583 BOOL_TYPE y;
584 y = PyObject_IsTrue(v);
585 memcpy(p, (char *)&y, sizeof y);
586 return 0;
589 static int
590 np_float(char *p, PyObject *v, const formatdef *f)
592 float x = (float)PyFloat_AsDouble(v);
593 if (x == -1 && PyErr_Occurred()) {
594 PyErr_SetString(StructError,
595 "required argument is not a float");
596 return -1;
598 memcpy(p, (char *)&x, sizeof x);
599 return 0;
602 static int
603 np_double(char *p, PyObject *v, const formatdef *f)
605 double x = PyFloat_AsDouble(v);
606 if (x == -1 && PyErr_Occurred()) {
607 PyErr_SetString(StructError,
608 "required argument is not a float");
609 return -1;
611 memcpy(p, (char *)&x, sizeof(double));
612 return 0;
615 static int
616 np_void_p(char *p, PyObject *v, const formatdef *f)
618 void *x;
620 v = get_pylong(v);
621 if (v == NULL)
622 return -1;
623 assert(PyLong_Check(v));
624 x = PyLong_AsVoidPtr(v);
625 Py_DECREF(v);
626 if (x == NULL && PyErr_Occurred())
627 return -1;
628 memcpy(p, (char *)&x, sizeof x);
629 return 0;
632 static formatdef native_table[] = {
633 {'x', sizeof(char), 0, NULL},
634 {'b', sizeof(char), 0, nu_byte, np_byte},
635 {'B', sizeof(char), 0, nu_ubyte, np_ubyte},
636 {'c', sizeof(char), 0, nu_char, np_char},
637 {'s', sizeof(char), 0, NULL},
638 {'p', sizeof(char), 0, NULL},
639 {'h', sizeof(short), SHORT_ALIGN, nu_short, np_short},
640 {'H', sizeof(short), SHORT_ALIGN, nu_ushort, np_ushort},
641 {'i', sizeof(int), INT_ALIGN, nu_int, np_int},
642 {'I', sizeof(int), INT_ALIGN, nu_uint, np_uint},
643 {'l', sizeof(long), LONG_ALIGN, nu_long, np_long},
644 {'L', sizeof(long), LONG_ALIGN, nu_ulong, np_ulong},
645 #ifdef HAVE_LONG_LONG
646 {'q', sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_longlong, np_longlong},
647 {'Q', sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_ulonglong,np_ulonglong},
648 #endif
649 {'?', sizeof(BOOL_TYPE), BOOL_ALIGN, nu_bool, np_bool},
650 {'f', sizeof(float), FLOAT_ALIGN, nu_float, np_float},
651 {'d', sizeof(double), DOUBLE_ALIGN, nu_double, np_double},
652 {'P', sizeof(void *), VOID_P_ALIGN, nu_void_p, np_void_p},
656 /* Big-endian routines. *****************************************************/
658 static PyObject *
659 bu_int(const char *p, const formatdef *f)
661 long x = 0;
662 Py_ssize_t i = f->size;
663 const unsigned char *bytes = (const unsigned char *)p;
664 do {
665 x = (x<<8) | *bytes++;
666 } while (--i > 0);
667 /* Extend the sign bit. */
668 if (SIZEOF_LONG > f->size)
669 x |= -(x & (1L << ((8 * f->size) - 1)));
670 return PyInt_FromLong(x);
673 static PyObject *
674 bu_uint(const char *p, const formatdef *f)
676 unsigned long x = 0;
677 Py_ssize_t i = f->size;
678 const unsigned char *bytes = (const unsigned char *)p;
679 do {
680 x = (x<<8) | *bytes++;
681 } while (--i > 0);
682 if (x <= LONG_MAX)
683 return PyInt_FromLong((long)x);
684 return PyLong_FromUnsignedLong(x);
687 static PyObject *
688 bu_longlong(const char *p, const formatdef *f)
690 #ifdef HAVE_LONG_LONG
691 PY_LONG_LONG x = 0;
692 Py_ssize_t i = f->size;
693 const unsigned char *bytes = (const unsigned char *)p;
694 do {
695 x = (x<<8) | *bytes++;
696 } while (--i > 0);
697 /* Extend the sign bit. */
698 if (SIZEOF_LONG_LONG > f->size)
699 x |= -(x & ((PY_LONG_LONG)1 << ((8 * f->size) - 1)));
700 if (x >= LONG_MIN && x <= LONG_MAX)
701 return PyInt_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
702 return PyLong_FromLongLong(x);
703 #else
704 return _PyLong_FromByteArray((const unsigned char *)p,
706 0, /* little-endian */
707 1 /* signed */);
708 #endif
711 static PyObject *
712 bu_ulonglong(const char *p, const formatdef *f)
714 #ifdef HAVE_LONG_LONG
715 unsigned PY_LONG_LONG x = 0;
716 Py_ssize_t i = f->size;
717 const unsigned char *bytes = (const unsigned char *)p;
718 do {
719 x = (x<<8) | *bytes++;
720 } while (--i > 0);
721 if (x <= LONG_MAX)
722 return PyInt_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
723 return PyLong_FromUnsignedLongLong(x);
724 #else
725 return _PyLong_FromByteArray((const unsigned char *)p,
727 0, /* little-endian */
728 0 /* signed */);
729 #endif
732 static PyObject *
733 bu_float(const char *p, const formatdef *f)
735 return unpack_float(p, 0);
738 static PyObject *
739 bu_double(const char *p, const formatdef *f)
741 return unpack_double(p, 0);
744 static PyObject *
745 bu_bool(const char *p, const formatdef *f)
747 char x;
748 memcpy((char *)&x, p, sizeof x);
749 return PyBool_FromLong(x != 0);
752 static int
753 bp_int(char *p, PyObject *v, const formatdef *f)
755 long x;
756 Py_ssize_t i;
757 if (get_long(v, &x) < 0)
758 return -1;
759 i = f->size;
760 if (i != SIZEOF_LONG) {
761 if ((i == 2) && (x < -32768 || x > 32767))
762 return _range_error(f, 0);
763 #if (SIZEOF_LONG != 4)
764 else if ((i == 4) && (x < -2147483648L || x > 2147483647L))
765 return _range_error(f, 0);
766 #endif
768 do {
769 p[--i] = (char)x;
770 x >>= 8;
771 } while (i > 0);
772 return 0;
775 static int
776 bp_uint(char *p, PyObject *v, const formatdef *f)
778 unsigned long x;
779 Py_ssize_t i;
780 if (get_ulong(v, &x) < 0)
781 return -1;
782 i = f->size;
783 if (i != SIZEOF_LONG) {
784 unsigned long maxint = 1;
785 maxint <<= (unsigned long)(i * 8);
786 if (x >= maxint)
787 return _range_error(f, 1);
789 do {
790 p[--i] = (char)x;
791 x >>= 8;
792 } while (i > 0);
793 return 0;
796 static int
797 bp_longlong(char *p, PyObject *v, const formatdef *f)
799 int res;
800 v = get_pylong(v);
801 if (v == NULL)
802 return -1;
803 res = _PyLong_AsByteArray((PyLongObject *)v,
804 (unsigned char *)p,
806 0, /* little_endian */
807 1 /* signed */);
808 Py_DECREF(v);
809 return res;
812 static int
813 bp_ulonglong(char *p, PyObject *v, const formatdef *f)
815 int res;
816 v = get_pylong(v);
817 if (v == NULL)
818 return -1;
819 res = _PyLong_AsByteArray((PyLongObject *)v,
820 (unsigned char *)p,
822 0, /* little_endian */
823 0 /* signed */);
824 Py_DECREF(v);
825 return res;
828 static int
829 bp_float(char *p, PyObject *v, const formatdef *f)
831 double x = PyFloat_AsDouble(v);
832 if (x == -1 && PyErr_Occurred()) {
833 PyErr_SetString(StructError,
834 "required argument is not a float");
835 return -1;
837 return _PyFloat_Pack4(x, (unsigned char *)p, 0);
840 static int
841 bp_double(char *p, PyObject *v, const formatdef *f)
843 double x = PyFloat_AsDouble(v);
844 if (x == -1 && PyErr_Occurred()) {
845 PyErr_SetString(StructError,
846 "required argument is not a float");
847 return -1;
849 return _PyFloat_Pack8(x, (unsigned char *)p, 0);
852 static int
853 bp_bool(char *p, PyObject *v, const formatdef *f)
855 char y;
856 y = PyObject_IsTrue(v);
857 memcpy(p, (char *)&y, sizeof y);
858 return 0;
861 static formatdef bigendian_table[] = {
862 {'x', 1, 0, NULL},
863 {'b', 1, 0, nu_byte, np_byte},
864 {'B', 1, 0, nu_ubyte, np_ubyte},
865 {'c', 1, 0, nu_char, np_char},
866 {'s', 1, 0, NULL},
867 {'p', 1, 0, NULL},
868 {'h', 2, 0, bu_int, bp_int},
869 {'H', 2, 0, bu_uint, bp_uint},
870 {'i', 4, 0, bu_int, bp_int},
871 {'I', 4, 0, bu_uint, bp_uint},
872 {'l', 4, 0, bu_int, bp_int},
873 {'L', 4, 0, bu_uint, bp_uint},
874 {'q', 8, 0, bu_longlong, bp_longlong},
875 {'Q', 8, 0, bu_ulonglong, bp_ulonglong},
876 {'?', 1, 0, bu_bool, bp_bool},
877 {'f', 4, 0, bu_float, bp_float},
878 {'d', 8, 0, bu_double, bp_double},
882 /* Little-endian routines. *****************************************************/
884 static PyObject *
885 lu_int(const char *p, const formatdef *f)
887 long x = 0;
888 Py_ssize_t i = f->size;
889 const unsigned char *bytes = (const unsigned char *)p;
890 do {
891 x = (x<<8) | bytes[--i];
892 } while (i > 0);
893 /* Extend the sign bit. */
894 if (SIZEOF_LONG > f->size)
895 x |= -(x & (1L << ((8 * f->size) - 1)));
896 return PyInt_FromLong(x);
899 static PyObject *
900 lu_uint(const char *p, const formatdef *f)
902 unsigned long x = 0;
903 Py_ssize_t i = f->size;
904 const unsigned char *bytes = (const unsigned char *)p;
905 do {
906 x = (x<<8) | bytes[--i];
907 } while (i > 0);
908 if (x <= LONG_MAX)
909 return PyInt_FromLong((long)x);
910 return PyLong_FromUnsignedLong((long)x);
913 static PyObject *
914 lu_longlong(const char *p, const formatdef *f)
916 #ifdef HAVE_LONG_LONG
917 PY_LONG_LONG x = 0;
918 Py_ssize_t i = f->size;
919 const unsigned char *bytes = (const unsigned char *)p;
920 do {
921 x = (x<<8) | bytes[--i];
922 } while (i > 0);
923 /* Extend the sign bit. */
924 if (SIZEOF_LONG_LONG > f->size)
925 x |= -(x & ((PY_LONG_LONG)1 << ((8 * f->size) - 1)));
926 if (x >= LONG_MIN && x <= LONG_MAX)
927 return PyInt_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
928 return PyLong_FromLongLong(x);
929 #else
930 return _PyLong_FromByteArray((const unsigned char *)p,
932 1, /* little-endian */
933 1 /* signed */);
934 #endif
937 static PyObject *
938 lu_ulonglong(const char *p, const formatdef *f)
940 #ifdef HAVE_LONG_LONG
941 unsigned PY_LONG_LONG x = 0;
942 Py_ssize_t i = f->size;
943 const unsigned char *bytes = (const unsigned char *)p;
944 do {
945 x = (x<<8) | bytes[--i];
946 } while (i > 0);
947 if (x <= LONG_MAX)
948 return PyInt_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
949 return PyLong_FromUnsignedLongLong(x);
950 #else
951 return _PyLong_FromByteArray((const unsigned char *)p,
953 1, /* little-endian */
954 0 /* signed */);
955 #endif
958 static PyObject *
959 lu_float(const char *p, const formatdef *f)
961 return unpack_float(p, 1);
964 static PyObject *
965 lu_double(const char *p, const formatdef *f)
967 return unpack_double(p, 1);
970 static int
971 lp_int(char *p, PyObject *v, const formatdef *f)
973 long x;
974 Py_ssize_t i;
975 if (get_long(v, &x) < 0)
976 return -1;
977 i = f->size;
978 if (i != SIZEOF_LONG) {
979 if ((i == 2) && (x < -32768 || x > 32767))
980 return _range_error(f, 0);
981 #if (SIZEOF_LONG != 4)
982 else if ((i == 4) && (x < -2147483648L || x > 2147483647L))
983 return _range_error(f, 0);
984 #endif
986 do {
987 *p++ = (char)x;
988 x >>= 8;
989 } while (--i > 0);
990 return 0;
993 static int
994 lp_uint(char *p, PyObject *v, const formatdef *f)
996 unsigned long x;
997 Py_ssize_t i;
998 if (get_ulong(v, &x) < 0)
999 return -1;
1000 i = f->size;
1001 if (i != SIZEOF_LONG) {
1002 unsigned long maxint = 1;
1003 maxint <<= (unsigned long)(i * 8);
1004 if (x >= maxint)
1005 return _range_error(f, 1);
1007 do {
1008 *p++ = (char)x;
1009 x >>= 8;
1010 } while (--i > 0);
1011 return 0;
1014 static int
1015 lp_longlong(char *p, PyObject *v, const formatdef *f)
1017 int res;
1018 v = get_pylong(v);
1019 if (v == NULL)
1020 return -1;
1021 res = _PyLong_AsByteArray((PyLongObject*)v,
1022 (unsigned char *)p,
1024 1, /* little_endian */
1025 1 /* signed */);
1026 Py_DECREF(v);
1027 return res;
1030 static int
1031 lp_ulonglong(char *p, PyObject *v, const formatdef *f)
1033 int res;
1034 v = get_pylong(v);
1035 if (v == NULL)
1036 return -1;
1037 res = _PyLong_AsByteArray((PyLongObject*)v,
1038 (unsigned char *)p,
1040 1, /* little_endian */
1041 0 /* signed */);
1042 Py_DECREF(v);
1043 return res;
1046 static int
1047 lp_float(char *p, PyObject *v, const formatdef *f)
1049 double x = PyFloat_AsDouble(v);
1050 if (x == -1 && PyErr_Occurred()) {
1051 PyErr_SetString(StructError,
1052 "required argument is not a float");
1053 return -1;
1055 return _PyFloat_Pack4(x, (unsigned char *)p, 1);
1058 static int
1059 lp_double(char *p, PyObject *v, const formatdef *f)
1061 double x = PyFloat_AsDouble(v);
1062 if (x == -1 && PyErr_Occurred()) {
1063 PyErr_SetString(StructError,
1064 "required argument is not a float");
1065 return -1;
1067 return _PyFloat_Pack8(x, (unsigned char *)p, 1);
1070 static formatdef lilendian_table[] = {
1071 {'x', 1, 0, NULL},
1072 {'b', 1, 0, nu_byte, np_byte},
1073 {'B', 1, 0, nu_ubyte, np_ubyte},
1074 {'c', 1, 0, nu_char, np_char},
1075 {'s', 1, 0, NULL},
1076 {'p', 1, 0, NULL},
1077 {'h', 2, 0, lu_int, lp_int},
1078 {'H', 2, 0, lu_uint, lp_uint},
1079 {'i', 4, 0, lu_int, lp_int},
1080 {'I', 4, 0, lu_uint, lp_uint},
1081 {'l', 4, 0, lu_int, lp_int},
1082 {'L', 4, 0, lu_uint, lp_uint},
1083 {'q', 8, 0, lu_longlong, lp_longlong},
1084 {'Q', 8, 0, lu_ulonglong, lp_ulonglong},
1085 {'?', 1, 0, bu_bool, bp_bool}, /* Std rep not endian dep,
1086 but potentially different from native rep -- reuse bx_bool funcs. */
1087 {'f', 4, 0, lu_float, lp_float},
1088 {'d', 8, 0, lu_double, lp_double},
1093 static const formatdef *
1094 whichtable(char **pfmt)
1096 const char *fmt = (*pfmt)++; /* May be backed out of later */
1097 switch (*fmt) {
1098 case '<':
1099 return lilendian_table;
1100 case '>':
1101 case '!': /* Network byte order is big-endian */
1102 return bigendian_table;
1103 case '=': { /* Host byte order -- different from native in aligment! */
1104 int n = 1;
1105 char *p = (char *) &n;
1106 if (*p == 1)
1107 return lilendian_table;
1108 else
1109 return bigendian_table;
1111 default:
1112 --*pfmt; /* Back out of pointer increment */
1113 /* Fall through */
1114 case '@':
1115 return native_table;
1120 /* Get the table entry for a format code */
1122 static const formatdef *
1123 getentry(int c, const formatdef *f)
1125 for (; f->format != '\0'; f++) {
1126 if (f->format == c) {
1127 return f;
1130 PyErr_SetString(StructError, "bad char in struct format");
1131 return NULL;
1135 /* Align a size according to a format code */
1137 static int
1138 align(Py_ssize_t size, char c, const formatdef *e)
1140 if (e->format == c) {
1141 if (e->alignment) {
1142 size = ((size + e->alignment - 1)
1143 / e->alignment)
1144 * e->alignment;
1147 return size;
1151 /* calculate the size of a format string */
1153 static int
1154 prepare_s(PyStructObject *self)
1156 const formatdef *f;
1157 const formatdef *e;
1158 formatcode *codes;
1160 const char *s;
1161 const char *fmt;
1162 char c;
1163 Py_ssize_t size, len, num, itemsize, x;
1165 fmt = PyString_AS_STRING(self->s_format);
1167 f = whichtable((char **)&fmt);
1169 s = fmt;
1170 size = 0;
1171 len = 0;
1172 while ((c = *s++) != '\0') {
1173 if (isspace(Py_CHARMASK(c)))
1174 continue;
1175 if ('0' <= c && c <= '9') {
1176 num = c - '0';
1177 while ('0' <= (c = *s++) && c <= '9') {
1178 x = num*10 + (c - '0');
1179 if (x/10 != num) {
1180 PyErr_SetString(
1181 StructError,
1182 "overflow in item count");
1183 return -1;
1185 num = x;
1187 if (c == '\0')
1188 break;
1190 else
1191 num = 1;
1193 e = getentry(c, f);
1194 if (e == NULL)
1195 return -1;
1197 switch (c) {
1198 case 's': /* fall through */
1199 case 'p': len++; break;
1200 case 'x': break;
1201 default: len += num; break;
1204 itemsize = e->size;
1205 size = align(size, c, e);
1206 x = num * itemsize;
1207 size += x;
1208 if (x/itemsize != num || size < 0) {
1209 PyErr_SetString(StructError,
1210 "total struct size too long");
1211 return -1;
1215 /* check for overflow */
1216 if ((len + 1) > (PY_SSIZE_T_MAX / sizeof(formatcode))) {
1217 PyErr_NoMemory();
1218 return -1;
1221 self->s_size = size;
1222 self->s_len = len;
1223 codes = PyMem_MALLOC((len + 1) * sizeof(formatcode));
1224 if (codes == NULL) {
1225 PyErr_NoMemory();
1226 return -1;
1228 self->s_codes = codes;
1230 s = fmt;
1231 size = 0;
1232 while ((c = *s++) != '\0') {
1233 if (isspace(Py_CHARMASK(c)))
1234 continue;
1235 if ('0' <= c && c <= '9') {
1236 num = c - '0';
1237 while ('0' <= (c = *s++) && c <= '9')
1238 num = num*10 + (c - '0');
1239 if (c == '\0')
1240 break;
1242 else
1243 num = 1;
1245 e = getentry(c, f);
1247 size = align(size, c, e);
1248 if (c == 's' || c == 'p') {
1249 codes->offset = size;
1250 codes->size = num;
1251 codes->fmtdef = e;
1252 codes++;
1253 size += num;
1254 } else if (c == 'x') {
1255 size += num;
1256 } else {
1257 while (--num >= 0) {
1258 codes->offset = size;
1259 codes->size = e->size;
1260 codes->fmtdef = e;
1261 codes++;
1262 size += e->size;
1266 codes->fmtdef = NULL;
1267 codes->offset = size;
1268 codes->size = 0;
1270 return 0;
1273 static PyObject *
1274 s_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1276 PyObject *self;
1278 assert(type != NULL && type->tp_alloc != NULL);
1280 self = type->tp_alloc(type, 0);
1281 if (self != NULL) {
1282 PyStructObject *s = (PyStructObject*)self;
1283 Py_INCREF(Py_None);
1284 s->s_format = Py_None;
1285 s->s_codes = NULL;
1286 s->s_size = -1;
1287 s->s_len = -1;
1289 return self;
1292 static int
1293 s_init(PyObject *self, PyObject *args, PyObject *kwds)
1295 PyStructObject *soself = (PyStructObject *)self;
1296 PyObject *o_format = NULL;
1297 int ret = 0;
1298 static char *kwlist[] = {"format", 0};
1300 assert(PyStruct_Check(self));
1302 if (!PyArg_ParseTupleAndKeywords(args, kwds, "S:Struct", kwlist,
1303 &o_format))
1304 return -1;
1306 Py_INCREF(o_format);
1307 Py_CLEAR(soself->s_format);
1308 soself->s_format = o_format;
1310 ret = prepare_s(soself);
1311 return ret;
1314 static void
1315 s_dealloc(PyStructObject *s)
1317 if (s->weakreflist != NULL)
1318 PyObject_ClearWeakRefs((PyObject *)s);
1319 if (s->s_codes != NULL) {
1320 PyMem_FREE(s->s_codes);
1322 Py_XDECREF(s->s_format);
1323 Py_TYPE(s)->tp_free((PyObject *)s);
1326 static PyObject *
1327 s_unpack_internal(PyStructObject *soself, char *startfrom) {
1328 formatcode *code;
1329 Py_ssize_t i = 0;
1330 PyObject *result = PyTuple_New(soself->s_len);
1331 if (result == NULL)
1332 return NULL;
1334 for (code = soself->s_codes; code->fmtdef != NULL; code++) {
1335 PyObject *v;
1336 const formatdef *e = code->fmtdef;
1337 const char *res = startfrom + code->offset;
1338 if (e->format == 's') {
1339 v = PyString_FromStringAndSize(res, code->size);
1340 } else if (e->format == 'p') {
1341 Py_ssize_t n = *(unsigned char*)res;
1342 if (n >= code->size)
1343 n = code->size - 1;
1344 v = PyString_FromStringAndSize(res + 1, n);
1345 } else {
1346 v = e->unpack(res, e);
1348 if (v == NULL)
1349 goto fail;
1350 PyTuple_SET_ITEM(result, i++, v);
1353 return result;
1354 fail:
1355 Py_DECREF(result);
1356 return NULL;
1360 PyDoc_STRVAR(s_unpack__doc__,
1361 "S.unpack(str) -> (v1, v2, ...)\n\
1363 Return tuple containing values unpacked according to this Struct's format.\n\
1364 Requires len(str) == self.size. See struct.__doc__ for more on format\n\
1365 strings.");
1367 static PyObject *
1368 s_unpack(PyObject *self, PyObject *inputstr)
1370 char *start;
1371 Py_ssize_t len;
1372 PyObject *args=NULL, *result;
1373 PyStructObject *soself = (PyStructObject *)self;
1374 assert(PyStruct_Check(self));
1375 assert(soself->s_codes != NULL);
1376 if (inputstr == NULL)
1377 goto fail;
1378 if (PyString_Check(inputstr) &&
1379 PyString_GET_SIZE(inputstr) == soself->s_size) {
1380 return s_unpack_internal(soself, PyString_AS_STRING(inputstr));
1382 args = PyTuple_Pack(1, inputstr);
1383 if (args == NULL)
1384 return NULL;
1385 if (!PyArg_ParseTuple(args, "s#:unpack", &start, &len))
1386 goto fail;
1387 if (soself->s_size != len)
1388 goto fail;
1389 result = s_unpack_internal(soself, start);
1390 Py_DECREF(args);
1391 return result;
1393 fail:
1394 Py_XDECREF(args);
1395 PyErr_Format(StructError,
1396 "unpack requires a string argument of length %zd",
1397 soself->s_size);
1398 return NULL;
1401 PyDoc_STRVAR(s_unpack_from__doc__,
1402 "S.unpack_from(buffer[, offset]) -> (v1, v2, ...)\n\
1404 Return tuple containing values unpacked according to this Struct's format.\n\
1405 Unlike unpack, unpack_from can unpack values from any object supporting\n\
1406 the buffer API, not just str. Requires len(buffer[offset:]) >= self.size.\n\
1407 See struct.__doc__ for more on format strings.");
1409 static PyObject *
1410 s_unpack_from(PyObject *self, PyObject *args, PyObject *kwds)
1412 static char *kwlist[] = {"buffer", "offset", 0};
1413 #if (PY_VERSION_HEX < 0x02050000)
1414 static char *fmt = "z#|i:unpack_from";
1415 #else
1416 static char *fmt = "z#|n:unpack_from";
1417 #endif
1418 Py_ssize_t buffer_len = 0, offset = 0;
1419 char *buffer = NULL;
1420 PyStructObject *soself = (PyStructObject *)self;
1421 assert(PyStruct_Check(self));
1422 assert(soself->s_codes != NULL);
1424 if (!PyArg_ParseTupleAndKeywords(args, kwds, fmt, kwlist,
1425 &buffer, &buffer_len, &offset))
1426 return NULL;
1428 if (buffer == NULL) {
1429 PyErr_Format(StructError,
1430 "unpack_from requires a buffer argument");
1431 return NULL;
1434 if (offset < 0)
1435 offset += buffer_len;
1437 if (offset < 0 || (buffer_len - offset) < soself->s_size) {
1438 PyErr_Format(StructError,
1439 "unpack_from requires a buffer of at least %zd bytes",
1440 soself->s_size);
1441 return NULL;
1443 return s_unpack_internal(soself, buffer + offset);
1448 * Guts of the pack function.
1450 * Takes a struct object, a tuple of arguments, and offset in that tuple of
1451 * argument for where to start processing the arguments for packing, and a
1452 * character buffer for writing the packed string. The caller must insure
1453 * that the buffer may contain the required length for packing the arguments.
1454 * 0 is returned on success, 1 is returned if there is an error.
1457 static int
1458 s_pack_internal(PyStructObject *soself, PyObject *args, int offset, char* buf)
1460 formatcode *code;
1461 /* XXX(nnorwitz): why does i need to be a local? can we use
1462 the offset parameter or do we need the wider width? */
1463 Py_ssize_t i;
1465 memset(buf, '\0', soself->s_size);
1466 i = offset;
1467 for (code = soself->s_codes; code->fmtdef != NULL; code++) {
1468 Py_ssize_t n;
1469 PyObject *v = PyTuple_GET_ITEM(args, i++);
1470 const formatdef *e = code->fmtdef;
1471 char *res = buf + code->offset;
1472 if (e->format == 's') {
1473 if (!PyString_Check(v)) {
1474 PyErr_SetString(StructError,
1475 "argument for 's' must "
1476 "be a string");
1477 return -1;
1479 n = PyString_GET_SIZE(v);
1480 if (n > code->size)
1481 n = code->size;
1482 if (n > 0)
1483 memcpy(res, PyString_AS_STRING(v), n);
1484 } else if (e->format == 'p') {
1485 if (!PyString_Check(v)) {
1486 PyErr_SetString(StructError,
1487 "argument for 'p' must "
1488 "be a string");
1489 return -1;
1491 n = PyString_GET_SIZE(v);
1492 if (n > (code->size - 1))
1493 n = code->size - 1;
1494 if (n > 0)
1495 memcpy(res + 1, PyString_AS_STRING(v), n);
1496 if (n > 255)
1497 n = 255;
1498 *res = Py_SAFE_DOWNCAST(n, Py_ssize_t, unsigned char);
1499 } else if (e->pack(res, v, e) < 0) {
1500 if (strchr(integer_codes, e->format) != NULL &&
1501 PyErr_ExceptionMatches(PyExc_OverflowError))
1502 PyErr_Format(StructError,
1503 "integer out of range for "
1504 "'%c' format code",
1505 e->format);
1506 return -1;
1510 /* Success */
1511 return 0;
1515 PyDoc_STRVAR(s_pack__doc__,
1516 "S.pack(v1, v2, ...) -> string\n\
1518 Return a string containing values v1, v2, ... packed according to this\n\
1519 Struct's format. See struct.__doc__ for more on format strings.");
1521 static PyObject *
1522 s_pack(PyObject *self, PyObject *args)
1524 PyStructObject *soself;
1525 PyObject *result;
1527 /* Validate arguments. */
1528 soself = (PyStructObject *)self;
1529 assert(PyStruct_Check(self));
1530 assert(soself->s_codes != NULL);
1531 if (PyTuple_GET_SIZE(args) != soself->s_len)
1533 PyErr_Format(StructError,
1534 "pack requires exactly %zd arguments", soself->s_len);
1535 return NULL;
1538 /* Allocate a new string */
1539 result = PyString_FromStringAndSize((char *)NULL, soself->s_size);
1540 if (result == NULL)
1541 return NULL;
1543 /* Call the guts */
1544 if ( s_pack_internal(soself, args, 0, PyString_AS_STRING(result)) != 0 ) {
1545 Py_DECREF(result);
1546 return NULL;
1549 return result;
1552 PyDoc_STRVAR(s_pack_into__doc__,
1553 "S.pack_into(buffer, offset, v1, v2, ...)\n\
1555 Pack the values v1, v2, ... according to this Struct's format, write \n\
1556 the packed bytes into the writable buffer buf starting at offset. Note\n\
1557 that the offset is not an optional argument. See struct.__doc__ for \n\
1558 more on format strings.");
1560 static PyObject *
1561 s_pack_into(PyObject *self, PyObject *args)
1563 PyStructObject *soself;
1564 char *buffer;
1565 Py_ssize_t buffer_len, offset;
1567 /* Validate arguments. +1 is for the first arg as buffer. */
1568 soself = (PyStructObject *)self;
1569 assert(PyStruct_Check(self));
1570 assert(soself->s_codes != NULL);
1571 if (PyTuple_GET_SIZE(args) != (soself->s_len + 2))
1573 PyErr_Format(StructError,
1574 "pack_into requires exactly %zd arguments",
1575 (soself->s_len + 2));
1576 return NULL;
1579 /* Extract a writable memory buffer from the first argument */
1580 if ( PyObject_AsWriteBuffer(PyTuple_GET_ITEM(args, 0),
1581 (void**)&buffer, &buffer_len) == -1 ) {
1582 return NULL;
1584 assert( buffer_len >= 0 );
1586 /* Extract the offset from the first argument */
1587 offset = PyInt_AsSsize_t(PyTuple_GET_ITEM(args, 1));
1588 if (offset == -1 && PyErr_Occurred())
1589 return NULL;
1591 /* Support negative offsets. */
1592 if (offset < 0)
1593 offset += buffer_len;
1595 /* Check boundaries */
1596 if (offset < 0 || (buffer_len - offset) < soself->s_size) {
1597 PyErr_Format(StructError,
1598 "pack_into requires a buffer of at least %zd bytes",
1599 soself->s_size);
1600 return NULL;
1603 /* Call the guts */
1604 if ( s_pack_internal(soself, args, 2, buffer + offset) != 0 ) {
1605 return NULL;
1608 Py_RETURN_NONE;
1611 static PyObject *
1612 s_get_format(PyStructObject *self, void *unused)
1614 Py_INCREF(self->s_format);
1615 return self->s_format;
1618 static PyObject *
1619 s_get_size(PyStructObject *self, void *unused)
1621 return PyInt_FromSsize_t(self->s_size);
1624 /* List of functions */
1626 static struct PyMethodDef s_methods[] = {
1627 {"pack", s_pack, METH_VARARGS, s_pack__doc__},
1628 {"pack_into", s_pack_into, METH_VARARGS, s_pack_into__doc__},
1629 {"unpack", s_unpack, METH_O, s_unpack__doc__},
1630 {"unpack_from", (PyCFunction)s_unpack_from, METH_VARARGS|METH_KEYWORDS,
1631 s_unpack_from__doc__},
1632 {NULL, NULL} /* sentinel */
1635 PyDoc_STRVAR(s__doc__, "Compiled struct object");
1637 #define OFF(x) offsetof(PyStructObject, x)
1639 static PyGetSetDef s_getsetlist[] = {
1640 {"format", (getter)s_get_format, (setter)NULL, "struct format string", NULL},
1641 {"size", (getter)s_get_size, (setter)NULL, "struct size in bytes", NULL},
1642 {NULL} /* sentinel */
1645 static
1646 PyTypeObject PyStructType = {
1647 PyVarObject_HEAD_INIT(NULL, 0)
1648 "Struct",
1649 sizeof(PyStructObject),
1651 (destructor)s_dealloc, /* tp_dealloc */
1652 0, /* tp_print */
1653 0, /* tp_getattr */
1654 0, /* tp_setattr */
1655 0, /* tp_compare */
1656 0, /* tp_repr */
1657 0, /* tp_as_number */
1658 0, /* tp_as_sequence */
1659 0, /* tp_as_mapping */
1660 0, /* tp_hash */
1661 0, /* tp_call */
1662 0, /* tp_str */
1663 PyObject_GenericGetAttr, /* tp_getattro */
1664 PyObject_GenericSetAttr, /* tp_setattro */
1665 0, /* tp_as_buffer */
1666 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_WEAKREFS,/* tp_flags */
1667 s__doc__, /* tp_doc */
1668 0, /* tp_traverse */
1669 0, /* tp_clear */
1670 0, /* tp_richcompare */
1671 offsetof(PyStructObject, weakreflist), /* tp_weaklistoffset */
1672 0, /* tp_iter */
1673 0, /* tp_iternext */
1674 s_methods, /* tp_methods */
1675 NULL, /* tp_members */
1676 s_getsetlist, /* tp_getset */
1677 0, /* tp_base */
1678 0, /* tp_dict */
1679 0, /* tp_descr_get */
1680 0, /* tp_descr_set */
1681 0, /* tp_dictoffset */
1682 s_init, /* tp_init */
1683 PyType_GenericAlloc,/* tp_alloc */
1684 s_new, /* tp_new */
1685 PyObject_Del, /* tp_free */
1689 /* ---- Standalone functions ---- */
1691 #define MAXCACHE 100
1692 static PyObject *cache = NULL;
1694 static PyObject *
1695 cache_struct(PyObject *fmt)
1697 PyObject * s_object;
1699 if (cache == NULL) {
1700 cache = PyDict_New();
1701 if (cache == NULL)
1702 return NULL;
1705 s_object = PyDict_GetItem(cache, fmt);
1706 if (s_object != NULL) {
1707 Py_INCREF(s_object);
1708 return s_object;
1711 s_object = PyObject_CallFunctionObjArgs((PyObject *)(&PyStructType), fmt, NULL);
1712 if (s_object != NULL) {
1713 if (PyDict_Size(cache) >= MAXCACHE)
1714 PyDict_Clear(cache);
1715 /* Attempt to cache the result */
1716 if (PyDict_SetItem(cache, fmt, s_object) == -1)
1717 PyErr_Clear();
1719 return s_object;
1722 PyDoc_STRVAR(clearcache_doc,
1723 "Clear the internal cache.");
1725 static PyObject *
1726 clearcache(PyObject *self)
1728 Py_CLEAR(cache);
1729 Py_RETURN_NONE;
1732 PyDoc_STRVAR(calcsize_doc,
1733 "Return size of C struct described by format string fmt.");
1735 static PyObject *
1736 calcsize(PyObject *self, PyObject *fmt)
1738 Py_ssize_t n;
1739 PyObject *s_object = cache_struct(fmt);
1740 if (s_object == NULL)
1741 return NULL;
1742 n = ((PyStructObject *)s_object)->s_size;
1743 Py_DECREF(s_object);
1744 return PyInt_FromSsize_t(n);
1747 PyDoc_STRVAR(pack_doc,
1748 "Return string containing values v1, v2, ... packed according to fmt.");
1750 static PyObject *
1751 pack(PyObject *self, PyObject *args)
1753 PyObject *s_object, *fmt, *newargs, *result;
1754 Py_ssize_t n = PyTuple_GET_SIZE(args);
1756 if (n == 0) {
1757 PyErr_SetString(PyExc_TypeError, "missing format argument");
1758 return NULL;
1760 fmt = PyTuple_GET_ITEM(args, 0);
1761 newargs = PyTuple_GetSlice(args, 1, n);
1762 if (newargs == NULL)
1763 return NULL;
1765 s_object = cache_struct(fmt);
1766 if (s_object == NULL) {
1767 Py_DECREF(newargs);
1768 return NULL;
1770 result = s_pack(s_object, newargs);
1771 Py_DECREF(newargs);
1772 Py_DECREF(s_object);
1773 return result;
1776 PyDoc_STRVAR(pack_into_doc,
1777 "Pack the values v1, v2, ... according to fmt.\n\
1778 Write the packed bytes into the writable buffer buf starting at offset.");
1780 static PyObject *
1781 pack_into(PyObject *self, PyObject *args)
1783 PyObject *s_object, *fmt, *newargs, *result;
1784 Py_ssize_t n = PyTuple_GET_SIZE(args);
1786 if (n == 0) {
1787 PyErr_SetString(PyExc_TypeError, "missing format argument");
1788 return NULL;
1790 fmt = PyTuple_GET_ITEM(args, 0);
1791 newargs = PyTuple_GetSlice(args, 1, n);
1792 if (newargs == NULL)
1793 return NULL;
1795 s_object = cache_struct(fmt);
1796 if (s_object == NULL) {
1797 Py_DECREF(newargs);
1798 return NULL;
1800 result = s_pack_into(s_object, newargs);
1801 Py_DECREF(newargs);
1802 Py_DECREF(s_object);
1803 return result;
1806 PyDoc_STRVAR(unpack_doc,
1807 "Unpack the string containing packed C structure data, according to fmt.\n\
1808 Requires len(string) == calcsize(fmt).");
1810 static PyObject *
1811 unpack(PyObject *self, PyObject *args)
1813 PyObject *s_object, *fmt, *inputstr, *result;
1815 if (!PyArg_UnpackTuple(args, "unpack", 2, 2, &fmt, &inputstr))
1816 return NULL;
1818 s_object = cache_struct(fmt);
1819 if (s_object == NULL)
1820 return NULL;
1821 result = s_unpack(s_object, inputstr);
1822 Py_DECREF(s_object);
1823 return result;
1826 PyDoc_STRVAR(unpack_from_doc,
1827 "Unpack the buffer, containing packed C structure data, according to\n\
1828 fmt, starting at offset. Requires len(buffer[offset:]) >= calcsize(fmt).");
1830 static PyObject *
1831 unpack_from(PyObject *self, PyObject *args, PyObject *kwds)
1833 PyObject *s_object, *fmt, *newargs, *result;
1834 Py_ssize_t n = PyTuple_GET_SIZE(args);
1836 if (n == 0) {
1837 PyErr_SetString(PyExc_TypeError, "missing format argument");
1838 return NULL;
1840 fmt = PyTuple_GET_ITEM(args, 0);
1841 newargs = PyTuple_GetSlice(args, 1, n);
1842 if (newargs == NULL)
1843 return NULL;
1845 s_object = cache_struct(fmt);
1846 if (s_object == NULL) {
1847 Py_DECREF(newargs);
1848 return NULL;
1850 result = s_unpack_from(s_object, newargs, kwds);
1851 Py_DECREF(newargs);
1852 Py_DECREF(s_object);
1853 return result;
1856 static struct PyMethodDef module_functions[] = {
1857 {"_clearcache", (PyCFunction)clearcache, METH_NOARGS, clearcache_doc},
1858 {"calcsize", calcsize, METH_O, calcsize_doc},
1859 {"pack", pack, METH_VARARGS, pack_doc},
1860 {"pack_into", pack_into, METH_VARARGS, pack_into_doc},
1861 {"unpack", unpack, METH_VARARGS, unpack_doc},
1862 {"unpack_from", (PyCFunction)unpack_from,
1863 METH_VARARGS|METH_KEYWORDS, unpack_from_doc},
1864 {NULL, NULL} /* sentinel */
1868 /* Module initialization */
1870 PyDoc_STRVAR(module_doc,
1871 "Functions to convert between Python values and C structs represented\n\
1872 as Python strings. It uses format strings (explained below) as compact\n\
1873 descriptions of the lay-out of the C structs and the intended conversion\n\
1874 to/from Python values.\n\
1876 The optional first format char indicates byte order, size and alignment:\n\
1877 @: native order, size & alignment (default)\n\
1878 =: native order, std. size & alignment\n\
1879 <: little-endian, std. size & alignment\n\
1880 >: big-endian, std. size & alignment\n\
1881 !: same as >\n\
1883 The remaining chars indicate types of args and must match exactly;\n\
1884 these can be preceded by a decimal repeat count:\n\
1885 x: pad byte (no data); c:char; b:signed byte; B:unsigned byte;\n\
1886 ?: _Bool (requires C99; if not available, char is used instead)\n\
1887 h:short; H:unsigned short; i:int; I:unsigned int;\n\
1888 l:long; L:unsigned long; f:float; d:double.\n\
1889 Special cases (preceding decimal count indicates length):\n\
1890 s:string (array of char); p: pascal string (with count byte).\n\
1891 Special case (only available in native format):\n\
1892 P:an integer type that is wide enough to hold a pointer.\n\
1893 Special case (not in native mode unless 'long long' in platform C):\n\
1894 q:long long; Q:unsigned long long\n\
1895 Whitespace between formats is ignored.\n\
1897 The variable struct.error is an exception raised on errors.\n");
1899 PyMODINIT_FUNC
1900 init_struct(void)
1902 PyObject *ver, *m;
1904 ver = PyString_FromString("0.2");
1905 if (ver == NULL)
1906 return;
1908 m = Py_InitModule3("_struct", module_functions, module_doc);
1909 if (m == NULL)
1910 return;
1912 Py_TYPE(&PyStructType) = &PyType_Type;
1913 if (PyType_Ready(&PyStructType) < 0)
1914 return;
1916 /* This speed trick can't be used until overflow masking goes
1917 away, because native endian always raises exceptions
1918 instead of overflow masking. */
1920 /* Check endian and swap in faster functions */
1922 int one = 1;
1923 formatdef *native = native_table;
1924 formatdef *other, *ptr;
1925 if ((int)*(unsigned char*)&one)
1926 other = lilendian_table;
1927 else
1928 other = bigendian_table;
1929 /* Scan through the native table, find a matching
1930 entry in the endian table and swap in the
1931 native implementations whenever possible
1932 (64-bit platforms may not have "standard" sizes) */
1933 while (native->format != '\0' && other->format != '\0') {
1934 ptr = other;
1935 while (ptr->format != '\0') {
1936 if (ptr->format == native->format) {
1937 /* Match faster when formats are
1938 listed in the same order */
1939 if (ptr == other)
1940 other++;
1941 /* Only use the trick if the
1942 size matches */
1943 if (ptr->size != native->size)
1944 break;
1945 /* Skip float and double, could be
1946 "unknown" float format */
1947 if (ptr->format == 'd' || ptr->format == 'f')
1948 break;
1949 ptr->pack = native->pack;
1950 ptr->unpack = native->unpack;
1951 break;
1953 ptr++;
1955 native++;
1959 /* Add some symbolic constants to the module */
1960 if (StructError == NULL) {
1961 StructError = PyErr_NewException("struct.error", NULL, NULL);
1962 if (StructError == NULL)
1963 return;
1966 Py_INCREF(StructError);
1967 PyModule_AddObject(m, "error", StructError);
1969 Py_INCREF((PyObject*)&PyStructType);
1970 PyModule_AddObject(m, "Struct", (PyObject*)&PyStructType);
1972 PyModule_AddObject(m, "__version__", ver);
1974 PyModule_AddIntConstant(m, "_PY_STRUCT_RANGE_CHECKING", 1);
1975 #ifdef PY_STRUCT_FLOAT_COERCE
1976 PyModule_AddIntConstant(m, "_PY_STRUCT_FLOAT_COERCE", 1);
1977 #endif