Blocked revisions 73580-73582 via svnmerge
[python/dscho.git] / Modules / _struct.c
blob83f5685dd056b3821b9d83813c3099927c902f58
1 /* struct module -- pack values into and (out of) bytes objects */
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 /* The translation function for each format character is table driven */
16 typedef struct _formatdef {
17 char format;
18 Py_ssize_t size;
19 Py_ssize_t alignment;
20 PyObject* (*unpack)(const char *,
21 const struct _formatdef *);
22 int (*pack)(char *, PyObject *,
23 const struct _formatdef *);
24 } formatdef;
26 typedef struct _formatcode {
27 const struct _formatdef *fmtdef;
28 Py_ssize_t offset;
29 Py_ssize_t size;
30 } formatcode;
32 /* Struct object interface */
34 typedef struct {
35 PyObject_HEAD
36 Py_ssize_t s_size;
37 Py_ssize_t s_len;
38 formatcode *s_codes;
39 PyObject *s_format;
40 PyObject *weakreflist; /* List of weak references */
41 } PyStructObject;
44 #define PyStruct_Check(op) PyObject_TypeCheck(op, &PyStructType)
45 #define PyStruct_CheckExact(op) (Py_TYPE(op) == &PyStructType)
48 /* Exception */
50 static PyObject *StructError;
53 /* Define various structs to figure out the alignments of types */
56 typedef struct { char c; short x; } st_short;
57 typedef struct { char c; int x; } st_int;
58 typedef struct { char c; long x; } st_long;
59 typedef struct { char c; float x; } st_float;
60 typedef struct { char c; double x; } st_double;
61 typedef struct { char c; void *x; } st_void_p;
63 #define SHORT_ALIGN (sizeof(st_short) - sizeof(short))
64 #define INT_ALIGN (sizeof(st_int) - sizeof(int))
65 #define LONG_ALIGN (sizeof(st_long) - sizeof(long))
66 #define FLOAT_ALIGN (sizeof(st_float) - sizeof(float))
67 #define DOUBLE_ALIGN (sizeof(st_double) - sizeof(double))
68 #define VOID_P_ALIGN (sizeof(st_void_p) - sizeof(void *))
70 /* We can't support q and Q in native mode unless the compiler does;
71 in std mode, they're 8 bytes on all platforms. */
72 #ifdef HAVE_LONG_LONG
73 typedef struct { char c; PY_LONG_LONG x; } s_long_long;
74 #define LONG_LONG_ALIGN (sizeof(s_long_long) - sizeof(PY_LONG_LONG))
75 #endif
77 #ifdef HAVE_C99_BOOL
78 #define BOOL_TYPE _Bool
79 typedef struct { char c; _Bool x; } s_bool;
80 #define BOOL_ALIGN (sizeof(s_bool) - sizeof(BOOL_TYPE))
81 #else
82 #define BOOL_TYPE char
83 #define BOOL_ALIGN 0
84 #endif
86 #define STRINGIFY(x) #x
88 #ifdef __powerc
89 #pragma options align=reset
90 #endif
92 /* Helper to get a PyLongObject. Caller should decref. */
94 static PyObject *
95 get_pylong(PyObject *v)
97 assert(v != NULL);
98 if (!PyLong_Check(v)) {
99 PyErr_SetString(StructError,
100 "required argument is not an integer");
101 return NULL;
104 Py_INCREF(v);
105 return v;
108 /* Helper routine to get a C long and raise the appropriate error if it isn't
109 one */
111 static int
112 get_long(PyObject *v, long *p)
114 long x;
116 if (!PyLong_Check(v)) {
117 PyErr_SetString(StructError,
118 "required argument is not an integer");
119 return -1;
121 x = PyLong_AsLong(v);
122 if (x == -1 && PyErr_Occurred()) {
123 if (PyErr_ExceptionMatches(PyExc_OverflowError))
124 PyErr_SetString(StructError,
125 "argument out of range");
126 return -1;
128 *p = x;
129 return 0;
133 /* Same, but handling unsigned long */
135 static int
136 get_ulong(PyObject *v, unsigned long *p)
138 unsigned long x;
140 if (!PyLong_Check(v)) {
141 PyErr_SetString(StructError,
142 "required argument is not an integer");
143 return -1;
145 x = PyLong_AsUnsignedLong(v);
146 if (x == (unsigned long)-1 && PyErr_Occurred()) {
147 if (PyErr_ExceptionMatches(PyExc_OverflowError))
148 PyErr_SetString(StructError,
149 "argument out of range");
150 return -1;
152 *p = x;
153 return 0;
156 #ifdef HAVE_LONG_LONG
158 /* Same, but handling native long long. */
160 static int
161 get_longlong(PyObject *v, PY_LONG_LONG *p)
163 PY_LONG_LONG x;
164 if (!PyLong_Check(v)) {
165 PyErr_SetString(StructError,
166 "required argument is not an integer");
167 return -1;
169 x = PyLong_AsLongLong(v);
170 if (x == -1 && PyErr_Occurred()) {
171 if (PyErr_ExceptionMatches(PyExc_OverflowError))
172 PyErr_SetString(StructError,
173 "argument out of range");
174 return -1;
176 *p = x;
177 return 0;
180 /* Same, but handling native unsigned long long. */
182 static int
183 get_ulonglong(PyObject *v, unsigned PY_LONG_LONG *p)
185 unsigned PY_LONG_LONG x;
186 if (!PyLong_Check(v)) {
187 PyErr_SetString(StructError,
188 "required argument is not an integer");
189 return -1;
191 x = PyLong_AsUnsignedLongLong(v);
192 if (x == -1 && PyErr_Occurred()) {
193 if (PyErr_ExceptionMatches(PyExc_OverflowError))
194 PyErr_SetString(StructError,
195 "argument out of range");
196 return -1;
198 *p = x;
199 return 0;
202 #endif
205 #define RANGE_ERROR(x, f, flag, mask) return _range_error(f, flag)
208 /* Floating point helpers */
210 static PyObject *
211 unpack_float(const char *p, /* start of 4-byte string */
212 int le) /* true for little-endian, false for big-endian */
214 double x;
216 x = _PyFloat_Unpack4((unsigned char *)p, le);
217 if (x == -1.0 && PyErr_Occurred())
218 return NULL;
219 return PyFloat_FromDouble(x);
222 static PyObject *
223 unpack_double(const char *p, /* start of 8-byte string */
224 int le) /* true for little-endian, false for big-endian */
226 double x;
228 x = _PyFloat_Unpack8((unsigned char *)p, le);
229 if (x == -1.0 && PyErr_Occurred())
230 return NULL;
231 return PyFloat_FromDouble(x);
234 /* Helper to format the range error exceptions */
235 static int
236 _range_error(const formatdef *f, int is_unsigned)
238 /* ulargest is the largest unsigned value with f->size bytes.
239 * Note that the simpler:
240 * ((size_t)1 << (f->size * 8)) - 1
241 * doesn't work when f->size == sizeof(size_t) because C doesn't
242 * define what happens when a left shift count is >= the number of
243 * bits in the integer being shifted; e.g., on some boxes it doesn't
244 * shift at all when they're equal.
246 const size_t ulargest = (size_t)-1 >> ((SIZEOF_SIZE_T - f->size)*8);
247 assert(f->size >= 1 && f->size <= SIZEOF_SIZE_T);
248 if (is_unsigned)
249 PyErr_Format(StructError,
250 "'%c' format requires 0 <= number <= %zu",
251 f->format,
252 ulargest);
253 else {
254 const Py_ssize_t largest = (Py_ssize_t)(ulargest >> 1);
255 PyErr_Format(StructError,
256 "'%c' format requires %zd <= number <= %zd",
257 f->format,
258 ~ largest,
259 largest);
262 return -1;
267 /* A large number of small routines follow, with names of the form
269 [bln][up]_TYPE
271 [bln] distiguishes among big-endian, little-endian and native.
272 [pu] distiguishes between pack (to struct) and unpack (from struct).
273 TYPE is one of char, byte, ubyte, etc.
276 /* Native mode routines. ****************************************************/
277 /* NOTE:
278 In all n[up]_<type> routines handling types larger than 1 byte, there is
279 *no* guarantee that the p pointer is properly aligned for each type,
280 therefore memcpy is called. An intermediate variable is used to
281 compensate for big-endian architectures.
282 Normally both the intermediate variable and the memcpy call will be
283 skipped by C optimisation in little-endian architectures (gcc >= 2.91
284 does this). */
286 static PyObject *
287 nu_char(const char *p, const formatdef *f)
289 return PyBytes_FromStringAndSize(p, 1);
292 static PyObject *
293 nu_byte(const char *p, const formatdef *f)
295 return PyLong_FromLong((long) *(signed char *)p);
298 static PyObject *
299 nu_ubyte(const char *p, const formatdef *f)
301 return PyLong_FromLong((long) *(unsigned char *)p);
304 static PyObject *
305 nu_short(const char *p, const formatdef *f)
307 short x;
308 memcpy((char *)&x, p, sizeof x);
309 return PyLong_FromLong((long)x);
312 static PyObject *
313 nu_ushort(const char *p, const formatdef *f)
315 unsigned short x;
316 memcpy((char *)&x, p, sizeof x);
317 return PyLong_FromLong((long)x);
320 static PyObject *
321 nu_int(const char *p, const formatdef *f)
323 int x;
324 memcpy((char *)&x, p, sizeof x);
325 return PyLong_FromLong((long)x);
328 static PyObject *
329 nu_uint(const char *p, const formatdef *f)
331 unsigned int x;
332 memcpy((char *)&x, p, sizeof x);
333 #if (SIZEOF_LONG > SIZEOF_INT)
334 return PyLong_FromLong((long)x);
335 #else
336 if (x <= ((unsigned int)LONG_MAX))
337 return PyLong_FromLong((long)x);
338 return PyLong_FromUnsignedLong((unsigned long)x);
339 #endif
342 static PyObject *
343 nu_long(const char *p, const formatdef *f)
345 long x;
346 memcpy((char *)&x, p, sizeof x);
347 return PyLong_FromLong(x);
350 static PyObject *
351 nu_ulong(const char *p, const formatdef *f)
353 unsigned long x;
354 memcpy((char *)&x, p, sizeof x);
355 if (x <= LONG_MAX)
356 return PyLong_FromLong((long)x);
357 return PyLong_FromUnsignedLong(x);
360 /* Native mode doesn't support q or Q unless the platform C supports
361 long long (or, on Windows, __int64). */
363 #ifdef HAVE_LONG_LONG
365 static PyObject *
366 nu_longlong(const char *p, const formatdef *f)
368 PY_LONG_LONG x;
369 memcpy((char *)&x, p, sizeof x);
370 if (x >= LONG_MIN && x <= LONG_MAX)
371 return PyLong_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
372 return PyLong_FromLongLong(x);
375 static PyObject *
376 nu_ulonglong(const char *p, const formatdef *f)
378 unsigned PY_LONG_LONG x;
379 memcpy((char *)&x, p, sizeof x);
380 if (x <= LONG_MAX)
381 return PyLong_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
382 return PyLong_FromUnsignedLongLong(x);
385 #endif
387 static PyObject *
388 nu_bool(const char *p, const formatdef *f)
390 BOOL_TYPE x;
391 memcpy((char *)&x, p, sizeof x);
392 return PyBool_FromLong(x != 0);
396 static PyObject *
397 nu_float(const char *p, const formatdef *f)
399 float x;
400 memcpy((char *)&x, p, sizeof x);
401 return PyFloat_FromDouble((double)x);
404 static PyObject *
405 nu_double(const char *p, const formatdef *f)
407 double x;
408 memcpy((char *)&x, p, sizeof x);
409 return PyFloat_FromDouble(x);
412 static PyObject *
413 nu_void_p(const char *p, const formatdef *f)
415 void *x;
416 memcpy((char *)&x, p, sizeof x);
417 return PyLong_FromVoidPtr(x);
420 static int
421 np_byte(char *p, PyObject *v, const formatdef *f)
423 long x;
424 if (get_long(v, &x) < 0)
425 return -1;
426 if (x < -128 || x > 127){
427 PyErr_SetString(StructError,
428 "byte format requires -128 <= number <= 127");
429 return -1;
431 *p = (char)x;
432 return 0;
435 static int
436 np_ubyte(char *p, PyObject *v, const formatdef *f)
438 long x;
439 if (get_long(v, &x) < 0)
440 return -1;
441 if (x < 0 || x > 255){
442 PyErr_SetString(StructError,
443 "ubyte format requires 0 <= number <= 255");
444 return -1;
446 *p = (char)x;
447 return 0;
450 static int
451 np_char(char *p, PyObject *v, const formatdef *f)
453 if (PyUnicode_Check(v)) {
454 v = _PyUnicode_AsDefaultEncodedString(v, NULL);
455 if (v == NULL)
456 return -1;
458 if (!PyBytes_Check(v) || PyBytes_Size(v) != 1) {
459 PyErr_SetString(StructError,
460 "char format requires bytes or string of length 1");
461 return -1;
463 *p = *PyBytes_AsString(v);
464 return 0;
467 static int
468 np_short(char *p, PyObject *v, const formatdef *f)
470 long x;
471 short y;
472 if (get_long(v, &x) < 0)
473 return -1;
474 if (x < SHRT_MIN || x > SHRT_MAX){
475 PyErr_SetString(StructError,
476 "short format requires " STRINGIFY(SHRT_MIN)
477 " <= number <= " STRINGIFY(SHRT_MAX));
478 return -1;
480 y = (short)x;
481 memcpy(p, (char *)&y, sizeof y);
482 return 0;
485 static int
486 np_ushort(char *p, PyObject *v, const formatdef *f)
488 long x;
489 unsigned short y;
490 if (get_long(v, &x) < 0)
491 return -1;
492 if (x < 0 || x > USHRT_MAX){
493 PyErr_SetString(StructError,
494 "short format requires 0 <= number <= " STRINGIFY(USHRT_MAX));
495 return -1;
497 y = (unsigned short)x;
498 memcpy(p, (char *)&y, sizeof y);
499 return 0;
502 static int
503 np_int(char *p, PyObject *v, const formatdef *f)
505 long x;
506 int y;
507 if (get_long(v, &x) < 0)
508 return -1;
509 #if (SIZEOF_LONG > SIZEOF_INT)
510 if ((x < ((long)INT_MIN)) || (x > ((long)INT_MAX)))
511 RANGE_ERROR(x, f, 0, -1);
512 #endif
513 y = (int)x;
514 memcpy(p, (char *)&y, sizeof y);
515 return 0;
518 static int
519 np_uint(char *p, PyObject *v, const formatdef *f)
521 unsigned long x;
522 unsigned int y;
523 if (get_ulong(v, &x) < 0)
524 return -1;
525 y = (unsigned int)x;
526 #if (SIZEOF_LONG > SIZEOF_INT)
527 if (x > ((unsigned long)UINT_MAX))
528 RANGE_ERROR(y, f, 1, -1);
529 #endif
530 memcpy(p, (char *)&y, sizeof y);
531 return 0;
534 static int
535 np_long(char *p, PyObject *v, const formatdef *f)
537 long x;
538 if (get_long(v, &x) < 0)
539 return -1;
540 memcpy(p, (char *)&x, sizeof x);
541 return 0;
544 static int
545 np_ulong(char *p, PyObject *v, const formatdef *f)
547 unsigned long x;
548 if (get_ulong(v, &x) < 0)
549 return -1;
550 memcpy(p, (char *)&x, sizeof x);
551 return 0;
554 #ifdef HAVE_LONG_LONG
556 static int
557 np_longlong(char *p, PyObject *v, const formatdef *f)
559 PY_LONG_LONG x;
560 if (get_longlong(v, &x) < 0)
561 return -1;
562 memcpy(p, (char *)&x, sizeof x);
563 return 0;
566 static int
567 np_ulonglong(char *p, PyObject *v, const formatdef *f)
569 unsigned PY_LONG_LONG x;
570 if (get_ulonglong(v, &x) < 0)
571 return -1;
572 memcpy(p, (char *)&x, sizeof x);
573 return 0;
575 #endif
578 static int
579 np_bool(char *p, PyObject *v, const formatdef *f)
581 BOOL_TYPE y;
582 y = PyObject_IsTrue(v);
583 memcpy(p, (char *)&y, sizeof y);
584 return 0;
587 static int
588 np_float(char *p, PyObject *v, const formatdef *f)
590 float x = (float)PyFloat_AsDouble(v);
591 if (x == -1 && PyErr_Occurred()) {
592 PyErr_SetString(StructError,
593 "required argument is not a float");
594 return -1;
596 memcpy(p, (char *)&x, sizeof x);
597 return 0;
600 static int
601 np_double(char *p, PyObject *v, const formatdef *f)
603 double x = PyFloat_AsDouble(v);
604 if (x == -1 && PyErr_Occurred()) {
605 PyErr_SetString(StructError,
606 "required argument is not a float");
607 return -1;
609 memcpy(p, (char *)&x, sizeof(double));
610 return 0;
613 static int
614 np_void_p(char *p, PyObject *v, const formatdef *f)
616 void *x;
618 v = get_pylong(v);
619 if (v == NULL)
620 return -1;
621 assert(PyLong_Check(v));
622 x = PyLong_AsVoidPtr(v);
623 Py_DECREF(v);
624 if (x == NULL && PyErr_Occurred())
625 return -1;
626 memcpy(p, (char *)&x, sizeof x);
627 return 0;
630 static formatdef native_table[] = {
631 {'x', sizeof(char), 0, NULL},
632 {'b', sizeof(char), 0, nu_byte, np_byte},
633 {'B', sizeof(char), 0, nu_ubyte, np_ubyte},
634 {'c', sizeof(char), 0, nu_char, np_char},
635 {'s', sizeof(char), 0, NULL},
636 {'p', sizeof(char), 0, NULL},
637 {'h', sizeof(short), SHORT_ALIGN, nu_short, np_short},
638 {'H', sizeof(short), SHORT_ALIGN, nu_ushort, np_ushort},
639 {'i', sizeof(int), INT_ALIGN, nu_int, np_int},
640 {'I', sizeof(int), INT_ALIGN, nu_uint, np_uint},
641 {'l', sizeof(long), LONG_ALIGN, nu_long, np_long},
642 {'L', sizeof(long), LONG_ALIGN, nu_ulong, np_ulong},
643 #ifdef HAVE_LONG_LONG
644 {'q', sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_longlong, np_longlong},
645 {'Q', sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_ulonglong,np_ulonglong},
646 #endif
647 {'?', sizeof(BOOL_TYPE), BOOL_ALIGN, nu_bool, np_bool},
648 {'f', sizeof(float), FLOAT_ALIGN, nu_float, np_float},
649 {'d', sizeof(double), DOUBLE_ALIGN, nu_double, np_double},
650 {'P', sizeof(void *), VOID_P_ALIGN, nu_void_p, np_void_p},
654 /* Big-endian routines. *****************************************************/
656 static PyObject *
657 bu_int(const char *p, const formatdef *f)
659 long x = 0;
660 Py_ssize_t i = f->size;
661 const unsigned char *bytes = (const unsigned char *)p;
662 do {
663 x = (x<<8) | *bytes++;
664 } while (--i > 0);
665 /* Extend the sign bit. */
666 if (SIZEOF_LONG > f->size)
667 x |= -(x & (1L << ((8 * f->size) - 1)));
668 return PyLong_FromLong(x);
671 static PyObject *
672 bu_uint(const char *p, const formatdef *f)
674 unsigned long x = 0;
675 Py_ssize_t i = f->size;
676 const unsigned char *bytes = (const unsigned char *)p;
677 do {
678 x = (x<<8) | *bytes++;
679 } while (--i > 0);
680 if (x <= LONG_MAX)
681 return PyLong_FromLong((long)x);
682 return PyLong_FromUnsignedLong(x);
685 static PyObject *
686 bu_longlong(const char *p, const formatdef *f)
688 #ifdef HAVE_LONG_LONG
689 PY_LONG_LONG x = 0;
690 Py_ssize_t i = f->size;
691 const unsigned char *bytes = (const unsigned char *)p;
692 do {
693 x = (x<<8) | *bytes++;
694 } while (--i > 0);
695 /* Extend the sign bit. */
696 if (SIZEOF_LONG_LONG > f->size)
697 x |= -(x & ((PY_LONG_LONG)1 << ((8 * f->size) - 1)));
698 if (x >= LONG_MIN && x <= LONG_MAX)
699 return PyLong_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
700 return PyLong_FromLongLong(x);
701 #else
702 return _PyLong_FromByteArray((const unsigned char *)p,
704 0, /* little-endian */
705 1 /* signed */);
706 #endif
709 static PyObject *
710 bu_ulonglong(const char *p, const formatdef *f)
712 #ifdef HAVE_LONG_LONG
713 unsigned PY_LONG_LONG x = 0;
714 Py_ssize_t i = f->size;
715 const unsigned char *bytes = (const unsigned char *)p;
716 do {
717 x = (x<<8) | *bytes++;
718 } while (--i > 0);
719 if (x <= LONG_MAX)
720 return PyLong_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
721 return PyLong_FromUnsignedLongLong(x);
722 #else
723 return _PyLong_FromByteArray((const unsigned char *)p,
725 0, /* little-endian */
726 0 /* signed */);
727 #endif
730 static PyObject *
731 bu_float(const char *p, const formatdef *f)
733 return unpack_float(p, 0);
736 static PyObject *
737 bu_double(const char *p, const formatdef *f)
739 return unpack_double(p, 0);
742 static PyObject *
743 bu_bool(const char *p, const formatdef *f)
745 char x;
746 memcpy((char *)&x, p, sizeof x);
747 return PyBool_FromLong(x != 0);
750 static int
751 bp_int(char *p, PyObject *v, const formatdef *f)
753 long x;
754 Py_ssize_t i;
755 if (get_long(v, &x) < 0)
756 return -1;
757 i = f->size;
758 if (i != SIZEOF_LONG) {
759 if ((i == 2) && (x < -32768 || x > 32767))
760 RANGE_ERROR(x, f, 0, 0xffffL);
761 #if (SIZEOF_LONG != 4)
762 else if ((i == 4) && (x < -2147483648L || x > 2147483647L))
763 RANGE_ERROR(x, f, 0, 0xffffffffL);
764 #endif
766 do {
767 p[--i] = (char)x;
768 x >>= 8;
769 } while (i > 0);
770 return 0;
773 static int
774 bp_uint(char *p, PyObject *v, const formatdef *f)
776 unsigned long x;
777 Py_ssize_t i;
778 if (get_ulong(v, &x) < 0)
779 return -1;
780 i = f->size;
781 if (i != SIZEOF_LONG) {
782 unsigned long maxint = 1;
783 maxint <<= (unsigned long)(i * 8);
784 if (x >= maxint)
785 RANGE_ERROR(x, f, 1, maxint - 1);
787 do {
788 p[--i] = (char)x;
789 x >>= 8;
790 } while (i > 0);
791 return 0;
794 static int
795 bp_longlong(char *p, PyObject *v, const formatdef *f)
797 int res;
798 v = get_pylong(v);
799 if (v == NULL)
800 return -1;
801 res = _PyLong_AsByteArray((PyLongObject *)v,
802 (unsigned char *)p,
804 0, /* little_endian */
805 1 /* signed */);
806 Py_DECREF(v);
807 return res;
810 static int
811 bp_ulonglong(char *p, PyObject *v, const formatdef *f)
813 int res;
814 v = get_pylong(v);
815 if (v == NULL)
816 return -1;
817 res = _PyLong_AsByteArray((PyLongObject *)v,
818 (unsigned char *)p,
820 0, /* little_endian */
821 0 /* signed */);
822 Py_DECREF(v);
823 return res;
826 static int
827 bp_float(char *p, PyObject *v, const formatdef *f)
829 double x = PyFloat_AsDouble(v);
830 if (x == -1 && PyErr_Occurred()) {
831 PyErr_SetString(StructError,
832 "required argument is not a float");
833 return -1;
835 return _PyFloat_Pack4(x, (unsigned char *)p, 0);
838 static int
839 bp_double(char *p, PyObject *v, const formatdef *f)
841 double x = PyFloat_AsDouble(v);
842 if (x == -1 && PyErr_Occurred()) {
843 PyErr_SetString(StructError,
844 "required argument is not a float");
845 return -1;
847 return _PyFloat_Pack8(x, (unsigned char *)p, 0);
850 static int
851 bp_bool(char *p, PyObject *v, const formatdef *f)
853 char y;
854 y = PyObject_IsTrue(v);
855 memcpy(p, (char *)&y, sizeof y);
856 return 0;
859 static formatdef bigendian_table[] = {
860 {'x', 1, 0, NULL},
861 {'b', 1, 0, nu_byte, np_byte},
862 {'B', 1, 0, nu_ubyte, np_ubyte},
863 {'c', 1, 0, nu_char, np_char},
864 {'s', 1, 0, NULL},
865 {'p', 1, 0, NULL},
866 {'h', 2, 0, bu_int, bp_int},
867 {'H', 2, 0, bu_uint, bp_uint},
868 {'i', 4, 0, bu_int, bp_int},
869 {'I', 4, 0, bu_uint, bp_uint},
870 {'l', 4, 0, bu_int, bp_int},
871 {'L', 4, 0, bu_uint, bp_uint},
872 {'q', 8, 0, bu_longlong, bp_longlong},
873 {'Q', 8, 0, bu_ulonglong, bp_ulonglong},
874 {'?', 1, 0, bu_bool, bp_bool},
875 {'f', 4, 0, bu_float, bp_float},
876 {'d', 8, 0, bu_double, bp_double},
880 /* Little-endian routines. *****************************************************/
882 static PyObject *
883 lu_int(const char *p, const formatdef *f)
885 long x = 0;
886 Py_ssize_t i = f->size;
887 const unsigned char *bytes = (const unsigned char *)p;
888 do {
889 x = (x<<8) | bytes[--i];
890 } while (i > 0);
891 /* Extend the sign bit. */
892 if (SIZEOF_LONG > f->size)
893 x |= -(x & (1L << ((8 * f->size) - 1)));
894 return PyLong_FromLong(x);
897 static PyObject *
898 lu_uint(const char *p, const formatdef *f)
900 unsigned long x = 0;
901 Py_ssize_t i = f->size;
902 const unsigned char *bytes = (const unsigned char *)p;
903 do {
904 x = (x<<8) | bytes[--i];
905 } while (i > 0);
906 if (x <= LONG_MAX)
907 return PyLong_FromLong((long)x);
908 return PyLong_FromUnsignedLong((long)x);
911 static PyObject *
912 lu_longlong(const char *p, const formatdef *f)
914 #ifdef HAVE_LONG_LONG
915 PY_LONG_LONG x = 0;
916 Py_ssize_t i = f->size;
917 const unsigned char *bytes = (const unsigned char *)p;
918 do {
919 x = (x<<8) | bytes[--i];
920 } while (i > 0);
921 /* Extend the sign bit. */
922 if (SIZEOF_LONG_LONG > f->size)
923 x |= -(x & ((PY_LONG_LONG)1 << ((8 * f->size) - 1)));
924 if (x >= LONG_MIN && x <= LONG_MAX)
925 return PyLong_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
926 return PyLong_FromLongLong(x);
927 #else
928 return _PyLong_FromByteArray((const unsigned char *)p,
930 1, /* little-endian */
931 1 /* signed */);
932 #endif
935 static PyObject *
936 lu_ulonglong(const char *p, const formatdef *f)
938 #ifdef HAVE_LONG_LONG
939 unsigned PY_LONG_LONG x = 0;
940 Py_ssize_t i = f->size;
941 const unsigned char *bytes = (const unsigned char *)p;
942 do {
943 x = (x<<8) | bytes[--i];
944 } while (i > 0);
945 if (x <= LONG_MAX)
946 return PyLong_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
947 return PyLong_FromUnsignedLongLong(x);
948 #else
949 return _PyLong_FromByteArray((const unsigned char *)p,
951 1, /* little-endian */
952 0 /* signed */);
953 #endif
956 static PyObject *
957 lu_float(const char *p, const formatdef *f)
959 return unpack_float(p, 1);
962 static PyObject *
963 lu_double(const char *p, const formatdef *f)
965 return unpack_double(p, 1);
968 static int
969 lp_int(char *p, PyObject *v, const formatdef *f)
971 long x;
972 Py_ssize_t i;
973 if (get_long(v, &x) < 0)
974 return -1;
975 i = f->size;
976 if (i != SIZEOF_LONG) {
977 if ((i == 2) && (x < -32768 || x > 32767))
978 RANGE_ERROR(x, f, 0, 0xffffL);
979 #if (SIZEOF_LONG != 4)
980 else if ((i == 4) && (x < -2147483648L || x > 2147483647L))
981 RANGE_ERROR(x, f, 0, 0xffffffffL);
982 #endif
984 do {
985 *p++ = (char)x;
986 x >>= 8;
987 } while (--i > 0);
988 return 0;
991 static int
992 lp_uint(char *p, PyObject *v, const formatdef *f)
994 unsigned long x;
995 Py_ssize_t i;
996 if (get_ulong(v, &x) < 0)
997 return -1;
998 i = f->size;
999 if (i != SIZEOF_LONG) {
1000 unsigned long maxint = 1;
1001 maxint <<= (unsigned long)(i * 8);
1002 if (x >= maxint)
1003 RANGE_ERROR(x, f, 1, maxint - 1);
1005 do {
1006 *p++ = (char)x;
1007 x >>= 8;
1008 } while (--i > 0);
1009 return 0;
1012 static int
1013 lp_longlong(char *p, PyObject *v, const formatdef *f)
1015 int res;
1016 v = get_pylong(v);
1017 if (v == NULL)
1018 return -1;
1019 res = _PyLong_AsByteArray((PyLongObject*)v,
1020 (unsigned char *)p,
1022 1, /* little_endian */
1023 1 /* signed */);
1024 Py_DECREF(v);
1025 return res;
1028 static int
1029 lp_ulonglong(char *p, PyObject *v, const formatdef *f)
1031 int res;
1032 v = get_pylong(v);
1033 if (v == NULL)
1034 return -1;
1035 res = _PyLong_AsByteArray((PyLongObject*)v,
1036 (unsigned char *)p,
1038 1, /* little_endian */
1039 0 /* signed */);
1040 Py_DECREF(v);
1041 return res;
1044 static int
1045 lp_float(char *p, PyObject *v, const formatdef *f)
1047 double x = PyFloat_AsDouble(v);
1048 if (x == -1 && PyErr_Occurred()) {
1049 PyErr_SetString(StructError,
1050 "required argument is not a float");
1051 return -1;
1053 return _PyFloat_Pack4(x, (unsigned char *)p, 1);
1056 static int
1057 lp_double(char *p, PyObject *v, const formatdef *f)
1059 double x = PyFloat_AsDouble(v);
1060 if (x == -1 && PyErr_Occurred()) {
1061 PyErr_SetString(StructError,
1062 "required argument is not a float");
1063 return -1;
1065 return _PyFloat_Pack8(x, (unsigned char *)p, 1);
1068 static formatdef lilendian_table[] = {
1069 {'x', 1, 0, NULL},
1070 {'b', 1, 0, nu_byte, np_byte},
1071 {'B', 1, 0, nu_ubyte, np_ubyte},
1072 {'c', 1, 0, nu_char, np_char},
1073 {'s', 1, 0, NULL},
1074 {'p', 1, 0, NULL},
1075 {'h', 2, 0, lu_int, lp_int},
1076 {'H', 2, 0, lu_uint, lp_uint},
1077 {'i', 4, 0, lu_int, lp_int},
1078 {'I', 4, 0, lu_uint, lp_uint},
1079 {'l', 4, 0, lu_int, lp_int},
1080 {'L', 4, 0, lu_uint, lp_uint},
1081 {'q', 8, 0, lu_longlong, lp_longlong},
1082 {'Q', 8, 0, lu_ulonglong, lp_ulonglong},
1083 {'?', 1, 0, bu_bool, bp_bool}, /* Std rep not endian dep,
1084 but potentially different from native rep -- reuse bx_bool funcs. */
1085 {'f', 4, 0, lu_float, lp_float},
1086 {'d', 8, 0, lu_double, lp_double},
1091 static const formatdef *
1092 whichtable(char **pfmt)
1094 const char *fmt = (*pfmt)++; /* May be backed out of later */
1095 switch (*fmt) {
1096 case '<':
1097 return lilendian_table;
1098 case '>':
1099 case '!': /* Network byte order is big-endian */
1100 return bigendian_table;
1101 case '=': { /* Host byte order -- different from native in aligment! */
1102 int n = 1;
1103 char *p = (char *) &n;
1104 if (*p == 1)
1105 return lilendian_table;
1106 else
1107 return bigendian_table;
1109 default:
1110 --*pfmt; /* Back out of pointer increment */
1111 /* Fall through */
1112 case '@':
1113 return native_table;
1118 /* Get the table entry for a format code */
1120 static const formatdef *
1121 getentry(int c, const formatdef *f)
1123 for (; f->format != '\0'; f++) {
1124 if (f->format == c) {
1125 return f;
1128 PyErr_SetString(StructError, "bad char in struct format");
1129 return NULL;
1133 /* Align a size according to a format code */
1135 static int
1136 align(Py_ssize_t size, char c, const formatdef *e)
1138 if (e->format == c) {
1139 if (e->alignment) {
1140 size = ((size + e->alignment - 1)
1141 / e->alignment)
1142 * e->alignment;
1145 return size;
1149 /* calculate the size of a format string */
1151 static int
1152 prepare_s(PyStructObject *self)
1154 const formatdef *f;
1155 const formatdef *e;
1156 formatcode *codes;
1158 const char *s;
1159 const char *fmt;
1160 char c;
1161 Py_ssize_t size, len, num, itemsize, x;
1163 fmt = PyBytes_AS_STRING(self->s_format);
1165 f = whichtable((char **)&fmt);
1167 s = fmt;
1168 size = 0;
1169 len = 0;
1170 while ((c = *s++) != '\0') {
1171 if (isspace(Py_CHARMASK(c)))
1172 continue;
1173 if ('0' <= c && c <= '9') {
1174 num = c - '0';
1175 while ('0' <= (c = *s++) && c <= '9') {
1176 x = num*10 + (c - '0');
1177 if (x/10 != num) {
1178 PyErr_SetString(
1179 StructError,
1180 "overflow in item count");
1181 return -1;
1183 num = x;
1185 if (c == '\0')
1186 break;
1188 else
1189 num = 1;
1191 e = getentry(c, f);
1192 if (e == NULL)
1193 return -1;
1195 switch (c) {
1196 case 's': /* fall through */
1197 case 'p': len++; break;
1198 case 'x': break;
1199 default: len += num; break;
1202 itemsize = e->size;
1203 size = align(size, c, e);
1204 x = num * itemsize;
1205 size += x;
1206 if (x/itemsize != num || size < 0) {
1207 PyErr_SetString(StructError,
1208 "total struct size too long");
1209 return -1;
1213 /* check for overflow */
1214 if ((len + 1) > (PY_SSIZE_T_MAX / sizeof(formatcode))) {
1215 PyErr_NoMemory();
1216 return -1;
1219 self->s_size = size;
1220 self->s_len = len;
1221 codes = PyMem_MALLOC((len + 1) * sizeof(formatcode));
1222 if (codes == NULL) {
1223 PyErr_NoMemory();
1224 return -1;
1226 self->s_codes = codes;
1228 s = fmt;
1229 size = 0;
1230 while ((c = *s++) != '\0') {
1231 if (isspace(Py_CHARMASK(c)))
1232 continue;
1233 if ('0' <= c && c <= '9') {
1234 num = c - '0';
1235 while ('0' <= (c = *s++) && c <= '9')
1236 num = num*10 + (c - '0');
1237 if (c == '\0')
1238 break;
1240 else
1241 num = 1;
1243 e = getentry(c, f);
1245 size = align(size, c, e);
1246 if (c == 's' || c == 'p') {
1247 codes->offset = size;
1248 codes->size = num;
1249 codes->fmtdef = e;
1250 codes++;
1251 size += num;
1252 } else if (c == 'x') {
1253 size += num;
1254 } else {
1255 while (--num >= 0) {
1256 codes->offset = size;
1257 codes->size = e->size;
1258 codes->fmtdef = e;
1259 codes++;
1260 size += e->size;
1264 codes->fmtdef = NULL;
1265 codes->offset = size;
1266 codes->size = 0;
1268 return 0;
1271 static PyObject *
1272 s_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1274 PyObject *self;
1276 assert(type != NULL && type->tp_alloc != NULL);
1278 self = type->tp_alloc(type, 0);
1279 if (self != NULL) {
1280 PyStructObject *s = (PyStructObject*)self;
1281 Py_INCREF(Py_None);
1282 s->s_format = Py_None;
1283 s->s_codes = NULL;
1284 s->s_size = -1;
1285 s->s_len = -1;
1287 return self;
1290 static int
1291 s_init(PyObject *self, PyObject *args, PyObject *kwds)
1293 PyStructObject *soself = (PyStructObject *)self;
1294 PyObject *o_format = NULL;
1295 int ret = 0;
1296 static char *kwlist[] = {"format", 0};
1298 assert(PyStruct_Check(self));
1300 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:Struct", kwlist,
1301 &o_format))
1302 return -1;
1304 if (PyUnicode_Check(o_format)) {
1305 o_format = PyUnicode_AsASCIIString(o_format);
1306 if (o_format == NULL)
1307 return -1;
1309 /* XXX support buffer interface, too */
1310 else {
1311 Py_INCREF(o_format);
1314 if (!PyBytes_Check(o_format)) {
1315 Py_DECREF(o_format);
1316 PyErr_Format(PyExc_TypeError,
1317 "Struct() argument 1 must be bytes, not %.200s",
1318 Py_TYPE(o_format)->tp_name);
1319 return -1;
1322 Py_CLEAR(soself->s_format);
1323 soself->s_format = o_format;
1325 ret = prepare_s(soself);
1326 return ret;
1329 static void
1330 s_dealloc(PyStructObject *s)
1332 if (s->weakreflist != NULL)
1333 PyObject_ClearWeakRefs((PyObject *)s);
1334 if (s->s_codes != NULL) {
1335 PyMem_FREE(s->s_codes);
1337 Py_XDECREF(s->s_format);
1338 Py_TYPE(s)->tp_free((PyObject *)s);
1341 static PyObject *
1342 s_unpack_internal(PyStructObject *soself, char *startfrom) {
1343 formatcode *code;
1344 Py_ssize_t i = 0;
1345 PyObject *result = PyTuple_New(soself->s_len);
1346 if (result == NULL)
1347 return NULL;
1349 for (code = soself->s_codes; code->fmtdef != NULL; code++) {
1350 PyObject *v;
1351 const formatdef *e = code->fmtdef;
1352 const char *res = startfrom + code->offset;
1353 if (e->format == 's') {
1354 v = PyBytes_FromStringAndSize(res, code->size);
1355 } else if (e->format == 'p') {
1356 Py_ssize_t n = *(unsigned char*)res;
1357 if (n >= code->size)
1358 n = code->size - 1;
1359 v = PyBytes_FromStringAndSize(res + 1, n);
1360 } else {
1361 v = e->unpack(res, e);
1363 if (v == NULL)
1364 goto fail;
1365 PyTuple_SET_ITEM(result, i++, v);
1368 return result;
1369 fail:
1370 Py_DECREF(result);
1371 return NULL;
1375 PyDoc_STRVAR(s_unpack__doc__,
1376 "S.unpack(buffer) -> (v1, v2, ...)\n\
1378 Return tuple containing values unpacked according to this Struct's format.\n\
1379 Requires len(buffer) == self.size. See struct.__doc__ for more on format\n\
1380 strings.");
1382 static PyObject *
1383 s_unpack(PyObject *self, PyObject *input)
1385 Py_buffer vbuf;
1386 PyObject *result;
1387 PyStructObject *soself = (PyStructObject *)self;
1389 assert(PyStruct_Check(self));
1390 assert(soself->s_codes != NULL);
1391 if (PyObject_GetBuffer(input, &vbuf, PyBUF_SIMPLE) < 0)
1392 return NULL;
1393 if (vbuf.len != soself->s_size) {
1394 PyErr_Format(StructError,
1395 "unpack requires a bytes argument of length %zd",
1396 soself->s_size);
1397 PyBuffer_Release(&vbuf);
1398 return NULL;
1400 result = s_unpack_internal(soself, vbuf.buf);
1401 PyBuffer_Release(&vbuf);
1402 return result;
1405 PyDoc_STRVAR(s_unpack_from__doc__,
1406 "S.unpack_from(buffer[, offset]) -> (v1, v2, ...)\n\
1408 Return tuple containing values unpacked according to this Struct's format.\n\
1409 Unlike unpack, unpack_from can unpack values from any object supporting\n\
1410 the buffer API, not just str. Requires len(buffer[offset:]) >= self.size.\n\
1411 See struct.__doc__ for more on format strings.");
1413 static PyObject *
1414 s_unpack_from(PyObject *self, PyObject *args, PyObject *kwds)
1416 static char *kwlist[] = {"buffer", "offset", 0};
1418 PyObject *input;
1419 Py_ssize_t offset = 0;
1420 Py_buffer vbuf;
1421 PyObject *result;
1422 PyStructObject *soself = (PyStructObject *)self;
1424 assert(PyStruct_Check(self));
1425 assert(soself->s_codes != NULL);
1427 if (!PyArg_ParseTupleAndKeywords(args, kwds,
1428 "O|n:unpack_from", kwlist,
1429 &input, &offset))
1430 return NULL;
1431 if (PyObject_GetBuffer(input, &vbuf, PyBUF_SIMPLE) < 0)
1432 return NULL;
1433 if (offset < 0)
1434 offset += vbuf.len;
1435 if (offset < 0 || vbuf.len - offset < soself->s_size) {
1436 PyErr_Format(StructError,
1437 "unpack_from requires a buffer of at least %zd bytes",
1438 soself->s_size);
1439 PyBuffer_Release(&vbuf);
1440 return NULL;
1442 result = s_unpack_internal(soself, (char*)vbuf.buf + offset);
1443 PyBuffer_Release(&vbuf);
1444 return result;
1449 * Guts of the pack function.
1451 * Takes a struct object, a tuple of arguments, and offset in that tuple of
1452 * argument for where to start processing the arguments for packing, and a
1453 * character buffer for writing the packed string. The caller must insure
1454 * that the buffer may contain the required length for packing the arguments.
1455 * 0 is returned on success, 1 is returned if there is an error.
1458 static int
1459 s_pack_internal(PyStructObject *soself, PyObject *args, int offset, char* buf)
1461 formatcode *code;
1462 /* XXX(nnorwitz): why does i need to be a local? can we use
1463 the offset parameter or do we need the wider width? */
1464 Py_ssize_t i;
1466 memset(buf, '\0', soself->s_size);
1467 i = offset;
1468 for (code = soself->s_codes; code->fmtdef != NULL; code++) {
1469 Py_ssize_t n;
1470 PyObject *v = PyTuple_GET_ITEM(args, i++);
1471 const formatdef *e = code->fmtdef;
1472 char *res = buf + code->offset;
1473 if (e->format == 's') {
1474 int isstring;
1475 void *p;
1476 if (PyUnicode_Check(v)) {
1477 v = _PyUnicode_AsDefaultEncodedString(v, NULL);
1478 if (v == NULL)
1479 return -1;
1481 isstring = PyBytes_Check(v);
1482 if (!isstring && !PyByteArray_Check(v)) {
1483 PyErr_SetString(StructError,
1484 "argument for 's' must be a bytes or string");
1485 return -1;
1487 if (isstring) {
1488 n = PyBytes_GET_SIZE(v);
1489 p = PyBytes_AS_STRING(v);
1491 else {
1492 n = PyByteArray_GET_SIZE(v);
1493 p = PyByteArray_AS_STRING(v);
1495 if (n > code->size)
1496 n = code->size;
1497 if (n > 0)
1498 memcpy(res, p, n);
1499 } else if (e->format == 'p') {
1500 int isstring;
1501 void *p;
1502 if (PyUnicode_Check(v)) {
1503 v = _PyUnicode_AsDefaultEncodedString(v, NULL);
1504 if (v == NULL)
1505 return -1;
1507 isstring = PyBytes_Check(v);
1508 if (!isstring && !PyByteArray_Check(v)) {
1509 PyErr_SetString(StructError,
1510 "argument for 'p' must be a bytes or string");
1511 return -1;
1513 if (isstring) {
1514 n = PyBytes_GET_SIZE(v);
1515 p = PyBytes_AS_STRING(v);
1517 else {
1518 n = PyByteArray_GET_SIZE(v);
1519 p = PyByteArray_AS_STRING(v);
1521 if (n > (code->size - 1))
1522 n = code->size - 1;
1523 if (n > 0)
1524 memcpy(res + 1, p, n);
1525 if (n > 255)
1526 n = 255;
1527 *res = Py_SAFE_DOWNCAST(n, Py_ssize_t, unsigned char);
1528 } else {
1529 if (e->pack(res, v, e) < 0) {
1530 if (PyLong_Check(v) && PyErr_ExceptionMatches(PyExc_OverflowError))
1531 PyErr_SetString(StructError,
1532 "long too large to convert to int");
1533 return -1;
1538 /* Success */
1539 return 0;
1543 PyDoc_STRVAR(s_pack__doc__,
1544 "S.pack(v1, v2, ...) -> bytes\n\
1546 Return a bytes containing values v1, v2, ... packed according to this\n\
1547 Struct's format. See struct.__doc__ for more on format strings.");
1549 static PyObject *
1550 s_pack(PyObject *self, PyObject *args)
1552 PyStructObject *soself;
1553 PyObject *result;
1555 /* Validate arguments. */
1556 soself = (PyStructObject *)self;
1557 assert(PyStruct_Check(self));
1558 assert(soself->s_codes != NULL);
1559 if (PyTuple_GET_SIZE(args) != soself->s_len)
1561 PyErr_Format(StructError,
1562 "pack requires exactly %zd arguments", soself->s_len);
1563 return NULL;
1566 /* Allocate a new string */
1567 result = PyBytes_FromStringAndSize((char *)NULL, soself->s_size);
1568 if (result == NULL)
1569 return NULL;
1571 /* Call the guts */
1572 if ( s_pack_internal(soself, args, 0, PyBytes_AS_STRING(result)) != 0 ) {
1573 Py_DECREF(result);
1574 return NULL;
1577 return result;
1580 PyDoc_STRVAR(s_pack_into__doc__,
1581 "S.pack_into(buffer, offset, v1, v2, ...)\n\
1583 Pack the values v1, v2, ... according to this Struct's format, write \n\
1584 the packed bytes into the writable buffer buf starting at offset. Note\n\
1585 that the offset is not an optional argument. See struct.__doc__ for \n\
1586 more on format strings.");
1588 static PyObject *
1589 s_pack_into(PyObject *self, PyObject *args)
1591 PyStructObject *soself;
1592 char *buffer;
1593 Py_ssize_t buffer_len, offset;
1595 /* Validate arguments. +1 is for the first arg as buffer. */
1596 soself = (PyStructObject *)self;
1597 assert(PyStruct_Check(self));
1598 assert(soself->s_codes != NULL);
1599 if (PyTuple_GET_SIZE(args) != (soself->s_len + 2))
1601 PyErr_Format(StructError,
1602 "pack_into requires exactly %zd arguments",
1603 (soself->s_len + 2));
1604 return NULL;
1607 /* Extract a writable memory buffer from the first argument */
1608 if ( PyObject_AsWriteBuffer(PyTuple_GET_ITEM(args, 0),
1609 (void**)&buffer, &buffer_len) == -1 ) {
1610 return NULL;
1612 assert( buffer_len >= 0 );
1614 /* Extract the offset from the first argument */
1615 offset = PyNumber_AsSsize_t(PyTuple_GET_ITEM(args, 1), PyExc_IndexError);
1616 if (offset == -1 && PyErr_Occurred())
1617 return NULL;
1619 /* Support negative offsets. */
1620 if (offset < 0)
1621 offset += buffer_len;
1623 /* Check boundaries */
1624 if (offset < 0 || (buffer_len - offset) < soself->s_size) {
1625 PyErr_Format(StructError,
1626 "pack_into requires a buffer of at least %zd bytes",
1627 soself->s_size);
1628 return NULL;
1631 /* Call the guts */
1632 if ( s_pack_internal(soself, args, 2, buffer + offset) != 0 ) {
1633 return NULL;
1636 Py_RETURN_NONE;
1639 static PyObject *
1640 s_get_format(PyStructObject *self, void *unused)
1642 Py_INCREF(self->s_format);
1643 return self->s_format;
1646 static PyObject *
1647 s_get_size(PyStructObject *self, void *unused)
1649 return PyLong_FromSsize_t(self->s_size);
1652 /* List of functions */
1654 static struct PyMethodDef s_methods[] = {
1655 {"pack", s_pack, METH_VARARGS, s_pack__doc__},
1656 {"pack_into", s_pack_into, METH_VARARGS, s_pack_into__doc__},
1657 {"unpack", s_unpack, METH_O, s_unpack__doc__},
1658 {"unpack_from", (PyCFunction)s_unpack_from, METH_VARARGS|METH_KEYWORDS,
1659 s_unpack_from__doc__},
1660 {NULL, NULL} /* sentinel */
1663 PyDoc_STRVAR(s__doc__, "Compiled struct object");
1665 #define OFF(x) offsetof(PyStructObject, x)
1667 static PyGetSetDef s_getsetlist[] = {
1668 {"format", (getter)s_get_format, (setter)NULL, "struct format string", NULL},
1669 {"size", (getter)s_get_size, (setter)NULL, "struct size in bytes", NULL},
1670 {NULL} /* sentinel */
1673 static
1674 PyTypeObject PyStructType = {
1675 PyVarObject_HEAD_INIT(NULL, 0)
1676 "Struct",
1677 sizeof(PyStructObject),
1679 (destructor)s_dealloc, /* tp_dealloc */
1680 0, /* tp_print */
1681 0, /* tp_getattr */
1682 0, /* tp_setattr */
1683 0, /* tp_reserved */
1684 0, /* tp_repr */
1685 0, /* tp_as_number */
1686 0, /* tp_as_sequence */
1687 0, /* tp_as_mapping */
1688 0, /* tp_hash */
1689 0, /* tp_call */
1690 0, /* tp_str */
1691 PyObject_GenericGetAttr, /* tp_getattro */
1692 PyObject_GenericSetAttr, /* tp_setattro */
1693 0, /* tp_as_buffer */
1694 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
1695 s__doc__, /* tp_doc */
1696 0, /* tp_traverse */
1697 0, /* tp_clear */
1698 0, /* tp_richcompare */
1699 offsetof(PyStructObject, weakreflist), /* tp_weaklistoffset */
1700 0, /* tp_iter */
1701 0, /* tp_iternext */
1702 s_methods, /* tp_methods */
1703 NULL, /* tp_members */
1704 s_getsetlist, /* tp_getset */
1705 0, /* tp_base */
1706 0, /* tp_dict */
1707 0, /* tp_descr_get */
1708 0, /* tp_descr_set */
1709 0, /* tp_dictoffset */
1710 s_init, /* tp_init */
1711 PyType_GenericAlloc,/* tp_alloc */
1712 s_new, /* tp_new */
1713 PyObject_Del, /* tp_free */
1717 /* ---- Standalone functions ---- */
1719 #define MAXCACHE 100
1720 static PyObject *cache = NULL;
1722 static PyObject *
1723 cache_struct(PyObject *fmt)
1725 PyObject * s_object;
1727 if (cache == NULL) {
1728 cache = PyDict_New();
1729 if (cache == NULL)
1730 return NULL;
1733 s_object = PyDict_GetItem(cache, fmt);
1734 if (s_object != NULL) {
1735 Py_INCREF(s_object);
1736 return s_object;
1739 s_object = PyObject_CallFunctionObjArgs((PyObject *)(&PyStructType), fmt, NULL);
1740 if (s_object != NULL) {
1741 if (PyDict_Size(cache) >= MAXCACHE)
1742 PyDict_Clear(cache);
1743 /* Attempt to cache the result */
1744 if (PyDict_SetItem(cache, fmt, s_object) == -1)
1745 PyErr_Clear();
1747 return s_object;
1750 PyDoc_STRVAR(clearcache_doc,
1751 "Clear the internal cache.");
1753 static PyObject *
1754 clearcache(PyObject *self)
1756 Py_CLEAR(cache);
1757 Py_RETURN_NONE;
1760 PyDoc_STRVAR(calcsize_doc,
1761 "Return size of C struct described by format string fmt.");
1763 static PyObject *
1764 calcsize(PyObject *self, PyObject *fmt)
1766 Py_ssize_t n;
1767 PyObject *s_object = cache_struct(fmt);
1768 if (s_object == NULL)
1769 return NULL;
1770 n = ((PyStructObject *)s_object)->s_size;
1771 Py_DECREF(s_object);
1772 return PyLong_FromSsize_t(n);
1775 PyDoc_STRVAR(pack_doc,
1776 "Return bytes containing values v1, v2, ... packed according to fmt.");
1778 static PyObject *
1779 pack(PyObject *self, PyObject *args)
1781 PyObject *s_object, *fmt, *newargs, *result;
1782 Py_ssize_t n = PyTuple_GET_SIZE(args);
1784 if (n == 0) {
1785 PyErr_SetString(PyExc_TypeError, "missing format argument");
1786 return NULL;
1788 fmt = PyTuple_GET_ITEM(args, 0);
1789 newargs = PyTuple_GetSlice(args, 1, n);
1790 if (newargs == NULL)
1791 return NULL;
1793 s_object = cache_struct(fmt);
1794 if (s_object == NULL) {
1795 Py_DECREF(newargs);
1796 return NULL;
1798 result = s_pack(s_object, newargs);
1799 Py_DECREF(newargs);
1800 Py_DECREF(s_object);
1801 return result;
1804 PyDoc_STRVAR(pack_into_doc,
1805 "Pack the values v1, v2, ... according to fmt.\n\
1806 Write the packed bytes into the writable buffer buf starting at offset.");
1808 static PyObject *
1809 pack_into(PyObject *self, PyObject *args)
1811 PyObject *s_object, *fmt, *newargs, *result;
1812 Py_ssize_t n = PyTuple_GET_SIZE(args);
1814 if (n == 0) {
1815 PyErr_SetString(PyExc_TypeError, "missing format argument");
1816 return NULL;
1818 fmt = PyTuple_GET_ITEM(args, 0);
1819 newargs = PyTuple_GetSlice(args, 1, n);
1820 if (newargs == NULL)
1821 return NULL;
1823 s_object = cache_struct(fmt);
1824 if (s_object == NULL) {
1825 Py_DECREF(newargs);
1826 return NULL;
1828 result = s_pack_into(s_object, newargs);
1829 Py_DECREF(newargs);
1830 Py_DECREF(s_object);
1831 return result;
1834 PyDoc_STRVAR(unpack_doc,
1835 "Unpack the bytes containing packed C structure data, according to fmt.\n\
1836 Requires len(bytes) == calcsize(fmt).");
1838 static PyObject *
1839 unpack(PyObject *self, PyObject *args)
1841 PyObject *s_object, *fmt, *inputstr, *result;
1843 if (!PyArg_UnpackTuple(args, "unpack", 2, 2, &fmt, &inputstr))
1844 return NULL;
1846 s_object = cache_struct(fmt);
1847 if (s_object == NULL)
1848 return NULL;
1849 result = s_unpack(s_object, inputstr);
1850 Py_DECREF(s_object);
1851 return result;
1854 PyDoc_STRVAR(unpack_from_doc,
1855 "Unpack the buffer, containing packed C structure data, according to\n\
1856 fmt, starting at offset. Requires len(buffer[offset:]) >= calcsize(fmt).");
1858 static PyObject *
1859 unpack_from(PyObject *self, PyObject *args, PyObject *kwds)
1861 PyObject *s_object, *fmt, *newargs, *result;
1862 Py_ssize_t n = PyTuple_GET_SIZE(args);
1864 if (n == 0) {
1865 PyErr_SetString(PyExc_TypeError, "missing format argument");
1866 return NULL;
1868 fmt = PyTuple_GET_ITEM(args, 0);
1869 newargs = PyTuple_GetSlice(args, 1, n);
1870 if (newargs == NULL)
1871 return NULL;
1873 s_object = cache_struct(fmt);
1874 if (s_object == NULL) {
1875 Py_DECREF(newargs);
1876 return NULL;
1878 result = s_unpack_from(s_object, newargs, kwds);
1879 Py_DECREF(newargs);
1880 Py_DECREF(s_object);
1881 return result;
1884 static struct PyMethodDef module_functions[] = {
1885 {"_clearcache", (PyCFunction)clearcache, METH_NOARGS, clearcache_doc},
1886 {"calcsize", calcsize, METH_O, calcsize_doc},
1887 {"pack", pack, METH_VARARGS, pack_doc},
1888 {"pack_into", pack_into, METH_VARARGS, pack_into_doc},
1889 {"unpack", unpack, METH_VARARGS, unpack_doc},
1890 {"unpack_from", (PyCFunction)unpack_from,
1891 METH_VARARGS|METH_KEYWORDS, unpack_from_doc},
1892 {NULL, NULL} /* sentinel */
1896 /* Module initialization */
1898 PyDoc_STRVAR(module_doc,
1899 "Functions to convert between Python values and C structs.\n\
1900 Python bytes objects are used to hold the data representing the C struct\n\
1901 and also as format strings to describe the layout of data in the C struct.\n\
1903 The optional first format char indicates byte order, size and alignment:\n\
1904 @: native order, size & alignment (default)\n\
1905 =: native order, std. size & alignment\n\
1906 <: little-endian, std. size & alignment\n\
1907 >: big-endian, std. size & alignment\n\
1908 !: same as >\n\
1910 The remaining chars indicate types of args and must match exactly;\n\
1911 these can be preceded by a decimal repeat count:\n\
1912 x: pad byte (no data); c:char; b:signed byte; B:unsigned byte;\n\
1913 h:short; H:unsigned short; i:int; I:unsigned int;\n\
1914 l:long; L:unsigned long; f:float; d:double.\n\
1915 Special cases (preceding decimal count indicates length):\n\
1916 s:string (array of char); p: pascal string (with count byte).\n\
1917 Special case (only available in native format):\n\
1918 P:an integer type that is wide enough to hold a pointer.\n\
1919 Special case (not in native mode unless 'long long' in platform C):\n\
1920 q:long long; Q:unsigned long long\n\
1921 Whitespace between formats is ignored.\n\
1923 The variable struct.error is an exception raised on errors.\n");
1926 static struct PyModuleDef _structmodule = {
1927 PyModuleDef_HEAD_INIT,
1928 "_struct",
1929 module_doc,
1931 module_functions,
1932 NULL,
1933 NULL,
1934 NULL,
1935 NULL
1938 PyMODINIT_FUNC
1939 PyInit__struct(void)
1941 PyObject *ver, *m;
1943 ver = PyBytes_FromString("0.3");
1944 if (ver == NULL)
1945 return NULL;
1947 m = PyModule_Create(&_structmodule);
1948 if (m == NULL)
1949 return NULL;
1951 Py_TYPE(&PyStructType) = &PyType_Type;
1952 if (PyType_Ready(&PyStructType) < 0)
1953 return NULL;
1955 /* Check endian and swap in faster functions */
1957 int one = 1;
1958 formatdef *native = native_table;
1959 formatdef *other, *ptr;
1960 if ((int)*(unsigned char*)&one)
1961 other = lilendian_table;
1962 else
1963 other = bigendian_table;
1964 /* Scan through the native table, find a matching
1965 entry in the endian table and swap in the
1966 native implementations whenever possible
1967 (64-bit platforms may not have "standard" sizes) */
1968 while (native->format != '\0' && other->format != '\0') {
1969 ptr = other;
1970 while (ptr->format != '\0') {
1971 if (ptr->format == native->format) {
1972 /* Match faster when formats are
1973 listed in the same order */
1974 if (ptr == other)
1975 other++;
1976 /* Only use the trick if the
1977 size matches */
1978 if (ptr->size != native->size)
1979 break;
1980 /* Skip float and double, could be
1981 "unknown" float format */
1982 if (ptr->format == 'd' || ptr->format == 'f')
1983 break;
1984 ptr->pack = native->pack;
1985 ptr->unpack = native->unpack;
1986 break;
1988 ptr++;
1990 native++;
1994 /* Add some symbolic constants to the module */
1995 if (StructError == NULL) {
1996 StructError = PyErr_NewException("struct.error", NULL, NULL);
1997 if (StructError == NULL)
1998 return NULL;
2001 Py_INCREF(StructError);
2002 PyModule_AddObject(m, "error", StructError);
2004 Py_INCREF((PyObject*)&PyStructType);
2005 PyModule_AddObject(m, "Struct", (PyObject*)&PyStructType);
2007 PyModule_AddObject(m, "__version__", ver);
2009 return m;