This commit was manufactured by cvs2svn to create tag 'r23a1-fork'.
[python/dscho.git] / Modules / structmodule.c
blobd4f8d861c275724aa4597c74444823c9243dc734
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 #include "Python.h"
7 #include <ctype.h>
9 PyDoc_STRVAR(struct__doc__,
10 "Functions to convert between Python values and C structs.\n\
11 Python strings are used to hold the data representing the C struct\n\
12 and also as format strings to describe the layout of data in the C struct.\n\
13 \n\
14 The optional first format char indicates byte order, size and alignment:\n\
15 @: native order, size & alignment (default)\n\
16 =: native order, std. size & alignment\n\
17 <: little-endian, std. size & alignment\n\
18 >: big-endian, std. size & alignment\n\
19 !: same as >\n\
20 \n\
21 The remaining chars indicate types of args and must match exactly;\n\
22 these can be preceded by a decimal repeat count:\n\
23 x: pad byte (no data); c:char; b:signed byte; B:unsigned byte;\n\
24 h:short; H:unsigned short; i:int; I:unsigned int;\n\
25 l:long; L:unsigned long; f:float; d:double.\n\
26 Special cases (preceding decimal count indicates length):\n\
27 s:string (array of char); p: pascal string (with count byte).\n\
28 Special case (only available in native format):\n\
29 P:an integer type that is wide enough to hold a pointer.\n\
30 Special case (not in native mode unless 'long long' in platform C):\n\
31 q:long long; Q:unsigned long long\n\
32 Whitespace between formats is ignored.\n\
33 \n\
34 The variable struct.error is an exception raised on errors.");
37 /* Exception */
39 static PyObject *StructError;
42 /* Define various structs to figure out the alignments of types */
44 #ifdef __MWERKS__
46 ** XXXX We have a problem here. There are no unique alignment rules
47 ** on the PowerPC mac.
49 #ifdef __powerc
50 #pragma options align=mac68k
51 #endif
52 #endif /* __MWERKS__ */
54 typedef struct { char c; short x; } st_short;
55 typedef struct { char c; int x; } st_int;
56 typedef struct { char c; long x; } st_long;
57 typedef struct { char c; float x; } st_float;
58 typedef struct { char c; double x; } st_double;
59 typedef struct { char c; void *x; } st_void_p;
61 #define SHORT_ALIGN (sizeof(st_short) - sizeof(short))
62 #define INT_ALIGN (sizeof(st_int) - sizeof(int))
63 #define LONG_ALIGN (sizeof(st_long) - sizeof(long))
64 #define FLOAT_ALIGN (sizeof(st_float) - sizeof(float))
65 #define DOUBLE_ALIGN (sizeof(st_double) - sizeof(double))
66 #define VOID_P_ALIGN (sizeof(st_void_p) - sizeof(void *))
68 /* We can't support q and Q in native mode unless the compiler does;
69 in std mode, they're 8 bytes on all platforms. */
70 #ifdef HAVE_LONG_LONG
71 typedef struct { char c; LONG_LONG x; } s_long_long;
72 #define LONG_LONG_ALIGN (sizeof(s_long_long) - sizeof(LONG_LONG))
73 #endif
75 #define STRINGIFY(x) #x
77 #ifdef __powerc
78 #pragma options align=reset
79 #endif
81 /* Helper to get a PyLongObject by hook or by crook. Caller should decref. */
83 static PyObject *
84 get_pylong(PyObject *v)
86 PyNumberMethods *m;
88 assert(v != NULL);
89 if (PyInt_Check(v))
90 return PyLong_FromLong(PyInt_AS_LONG(v));
91 if (PyLong_Check(v)) {
92 Py_INCREF(v);
93 return v;
95 m = v->ob_type->tp_as_number;
96 if (m != NULL && m->nb_long != NULL) {
97 v = m->nb_long(v);
98 if (v == NULL)
99 return NULL;
100 if (PyLong_Check(v))
101 return v;
102 Py_DECREF(v);
104 PyErr_SetString(StructError,
105 "cannot convert argument to long");
106 return NULL;
109 /* Helper routine to get a Python integer and raise the appropriate error
110 if it isn't one */
112 static int
113 get_long(PyObject *v, long *p)
115 long x = PyInt_AsLong(v);
116 if (x == -1 && PyErr_Occurred()) {
117 if (PyErr_ExceptionMatches(PyExc_TypeError))
118 PyErr_SetString(StructError,
119 "required argument is not an integer");
120 return -1;
122 *p = x;
123 return 0;
127 /* Same, but handling unsigned long */
129 static int
130 get_ulong(PyObject *v, unsigned long *p)
132 if (PyLong_Check(v)) {
133 unsigned long x = PyLong_AsUnsignedLong(v);
134 if (x == (unsigned long)(-1) && PyErr_Occurred())
135 return -1;
136 *p = x;
137 return 0;
139 else {
140 return get_long(v, (long *)p);
144 #ifdef HAVE_LONG_LONG
146 /* Same, but handling native long long. */
148 static int
149 get_longlong(PyObject *v, LONG_LONG *p)
151 LONG_LONG x;
153 v = get_pylong(v);
154 if (v == NULL)
155 return -1;
156 assert(PyLong_Check(v));
157 x = PyLong_AsLongLong(v);
158 Py_DECREF(v);
159 if (x == (LONG_LONG)-1 && PyErr_Occurred())
160 return -1;
161 *p = x;
162 return 0;
165 /* Same, but handling native unsigned long long. */
167 static int
168 get_ulonglong(PyObject *v, unsigned LONG_LONG *p)
170 unsigned LONG_LONG x;
172 v = get_pylong(v);
173 if (v == NULL)
174 return -1;
175 assert(PyLong_Check(v));
176 x = PyLong_AsUnsignedLongLong(v);
177 Py_DECREF(v);
178 if (x == (unsigned LONG_LONG)-1 && PyErr_Occurred())
179 return -1;
180 *p = x;
181 return 0;
184 #endif
186 /* Floating point helpers */
188 /* These use ANSI/IEEE Standard 754-1985 (Standard for Binary Floating
189 Point Arithmetic). See the following URL:
190 http://www.psc.edu/general/software/packages/ieee/ieee.html */
192 /* XXX Inf/NaN are not handled quite right (but underflow is!) */
194 static int
195 pack_float(double x, /* The number to pack */
196 char *p, /* Where to pack the high order byte */
197 int incr) /* 1 for big-endian; -1 for little-endian */
199 int s;
200 int e;
201 double f;
202 long fbits;
204 if (x < 0) {
205 s = 1;
206 x = -x;
208 else
209 s = 0;
211 f = frexp(x, &e);
213 /* Normalize f to be in the range [1.0, 2.0) */
214 if (0.5 <= f && f < 1.0) {
215 f *= 2.0;
216 e--;
218 else if (f == 0.0) {
219 e = 0;
221 else {
222 PyErr_SetString(PyExc_SystemError,
223 "frexp() result out of range");
224 return -1;
227 if (e >= 128) {
228 /* XXX 128 itself is reserved for Inf/NaN */
229 PyErr_SetString(PyExc_OverflowError,
230 "float too large to pack with f format");
231 return -1;
233 else if (e < -126) {
234 /* Gradual underflow */
235 f = ldexp(f, 126 + e);
236 e = 0;
238 else if (!(e == 0 && f == 0.0)) {
239 e += 127;
240 f -= 1.0; /* Get rid of leading 1 */
243 f *= 8388608.0; /* 2**23 */
244 fbits = (long) floor(f + 0.5); /* Round */
246 /* First byte */
247 *p = (s<<7) | (e>>1);
248 p += incr;
250 /* Second byte */
251 *p = (char) (((e&1)<<7) | (fbits>>16));
252 p += incr;
254 /* Third byte */
255 *p = (fbits>>8) & 0xFF;
256 p += incr;
258 /* Fourth byte */
259 *p = fbits&0xFF;
261 /* Done */
262 return 0;
265 static int
266 pack_double(double x, /* The number to pack */
267 char *p, /* Where to pack the high order byte */
268 int incr) /* 1 for big-endian; -1 for little-endian */
270 int s;
271 int e;
272 double f;
273 long fhi, flo;
275 if (x < 0) {
276 s = 1;
277 x = -x;
279 else
280 s = 0;
282 f = frexp(x, &e);
284 /* Normalize f to be in the range [1.0, 2.0) */
285 if (0.5 <= f && f < 1.0) {
286 f *= 2.0;
287 e--;
289 else if (f == 0.0) {
290 e = 0;
292 else {
293 PyErr_SetString(PyExc_SystemError,
294 "frexp() result out of range");
295 return -1;
298 if (e >= 1024) {
299 /* XXX 1024 itself is reserved for Inf/NaN */
300 PyErr_SetString(PyExc_OverflowError,
301 "float too large to pack with d format");
302 return -1;
304 else if (e < -1022) {
305 /* Gradual underflow */
306 f = ldexp(f, 1022 + e);
307 e = 0;
309 else if (!(e == 0 && f == 0.0)) {
310 e += 1023;
311 f -= 1.0; /* Get rid of leading 1 */
314 /* fhi receives the high 28 bits; flo the low 24 bits (== 52 bits) */
315 f *= 268435456.0; /* 2**28 */
316 fhi = (long) floor(f); /* Truncate */
317 f -= (double)fhi;
318 f *= 16777216.0; /* 2**24 */
319 flo = (long) floor(f + 0.5); /* Round */
321 /* First byte */
322 *p = (s<<7) | (e>>4);
323 p += incr;
325 /* Second byte */
326 *p = (char) (((e&0xF)<<4) | (fhi>>24));
327 p += incr;
329 /* Third byte */
330 *p = (fhi>>16) & 0xFF;
331 p += incr;
333 /* Fourth byte */
334 *p = (fhi>>8) & 0xFF;
335 p += incr;
337 /* Fifth byte */
338 *p = fhi & 0xFF;
339 p += incr;
341 /* Sixth byte */
342 *p = (flo>>16) & 0xFF;
343 p += incr;
345 /* Seventh byte */
346 *p = (flo>>8) & 0xFF;
347 p += incr;
349 /* Eighth byte */
350 *p = flo & 0xFF;
351 p += incr;
353 /* Done */
354 return 0;
357 static PyObject *
358 unpack_float(const char *p, /* Where the high order byte is */
359 int incr) /* 1 for big-endian; -1 for little-endian */
361 int s;
362 int e;
363 long f;
364 double x;
366 /* First byte */
367 s = (*p>>7) & 1;
368 e = (*p & 0x7F) << 1;
369 p += incr;
371 /* Second byte */
372 e |= (*p>>7) & 1;
373 f = (*p & 0x7F) << 16;
374 p += incr;
376 /* Third byte */
377 f |= (*p & 0xFF) << 8;
378 p += incr;
380 /* Fourth byte */
381 f |= *p & 0xFF;
383 x = (double)f / 8388608.0;
385 /* XXX This sadly ignores Inf/NaN issues */
386 if (e == 0)
387 e = -126;
388 else {
389 x += 1.0;
390 e -= 127;
392 x = ldexp(x, e);
394 if (s)
395 x = -x;
397 return PyFloat_FromDouble(x);
400 static PyObject *
401 unpack_double(const char *p, /* Where the high order byte is */
402 int incr) /* 1 for big-endian; -1 for little-endian */
404 int s;
405 int e;
406 long fhi, flo;
407 double x;
409 /* First byte */
410 s = (*p>>7) & 1;
411 e = (*p & 0x7F) << 4;
412 p += incr;
414 /* Second byte */
415 e |= (*p>>4) & 0xF;
416 fhi = (*p & 0xF) << 24;
417 p += incr;
419 /* Third byte */
420 fhi |= (*p & 0xFF) << 16;
421 p += incr;
423 /* Fourth byte */
424 fhi |= (*p & 0xFF) << 8;
425 p += incr;
427 /* Fifth byte */
428 fhi |= *p & 0xFF;
429 p += incr;
431 /* Sixth byte */
432 flo = (*p & 0xFF) << 16;
433 p += incr;
435 /* Seventh byte */
436 flo |= (*p & 0xFF) << 8;
437 p += incr;
439 /* Eighth byte */
440 flo |= *p & 0xFF;
441 p += incr;
443 x = (double)fhi + (double)flo / 16777216.0; /* 2**24 */
444 x /= 268435456.0; /* 2**28 */
446 /* XXX This sadly ignores Inf/NaN */
447 if (e == 0)
448 e = -1022;
449 else {
450 x += 1.0;
451 e -= 1023;
453 x = ldexp(x, e);
455 if (s)
456 x = -x;
458 return PyFloat_FromDouble(x);
462 /* The translation function for each format character is table driven */
464 typedef struct _formatdef {
465 char format;
466 int size;
467 int alignment;
468 PyObject* (*unpack)(const char *,
469 const struct _formatdef *);
470 int (*pack)(char *, PyObject *,
471 const struct _formatdef *);
472 } formatdef;
474 /* A large number of small routines follow, with names of the form
476 [bln][up]_TYPE
478 [bln] distiguishes among big-endian, little-endian and native.
479 [pu] distiguishes between pack (to struct) and unpack (from struct).
480 TYPE is one of char, byte, ubyte, etc.
483 /* Native mode routines. ****************************************************/
484 /* NOTE:
485 In all n[up]_<type> routines handling types larger than 1 byte, there is
486 *no* guarantee that the p pointer is properly aligned for each type,
487 therefore memcpy is called. An intermediate variable is used to
488 compensate for big-endian architectures.
489 Normally both the intermediate variable and the memcpy call will be
490 skipped by C optimisation in little-endian architectures (gcc >= 2.91
491 does this). */
493 static PyObject *
494 nu_char(const char *p, const formatdef *f)
496 return PyString_FromStringAndSize(p, 1);
499 static PyObject *
500 nu_byte(const char *p, const formatdef *f)
502 return PyInt_FromLong((long) *(signed char *)p);
505 static PyObject *
506 nu_ubyte(const char *p, const formatdef *f)
508 return PyInt_FromLong((long) *(unsigned char *)p);
511 static PyObject *
512 nu_short(const char *p, const formatdef *f)
514 short x;
515 memcpy((char *)&x, p, sizeof x);
516 return PyInt_FromLong((long)x);
519 static PyObject *
520 nu_ushort(const char *p, const formatdef *f)
522 unsigned short x;
523 memcpy((char *)&x, p, sizeof x);
524 return PyInt_FromLong((long)x);
527 static PyObject *
528 nu_int(const char *p, const formatdef *f)
530 int x;
531 memcpy((char *)&x, p, sizeof x);
532 return PyInt_FromLong((long)x);
535 static PyObject *
536 nu_uint(const char *p, const formatdef *f)
538 unsigned int x;
539 memcpy((char *)&x, p, sizeof x);
540 return PyLong_FromUnsignedLong((unsigned long)x);
543 static PyObject *
544 nu_long(const char *p, const formatdef *f)
546 long x;
547 memcpy((char *)&x, p, sizeof x);
548 return PyInt_FromLong(x);
551 static PyObject *
552 nu_ulong(const char *p, const formatdef *f)
554 unsigned long x;
555 memcpy((char *)&x, p, sizeof x);
556 return PyLong_FromUnsignedLong(x);
559 /* Native mode doesn't support q or Q unless the platform C supports
560 long long (or, on Windows, __int64). */
562 #ifdef HAVE_LONG_LONG
564 static PyObject *
565 nu_longlong(const char *p, const formatdef *f)
567 LONG_LONG x;
568 memcpy((char *)&x, p, sizeof x);
569 return PyLong_FromLongLong(x);
572 static PyObject *
573 nu_ulonglong(const char *p, const formatdef *f)
575 unsigned LONG_LONG x;
576 memcpy((char *)&x, p, sizeof x);
577 return PyLong_FromUnsignedLongLong(x);
580 #endif
582 static PyObject *
583 nu_float(const char *p, const formatdef *f)
585 float x;
586 memcpy((char *)&x, p, sizeof x);
587 return PyFloat_FromDouble((double)x);
590 static PyObject *
591 nu_double(const char *p, const formatdef *f)
593 double x;
594 memcpy((char *)&x, p, sizeof x);
595 return PyFloat_FromDouble(x);
598 static PyObject *
599 nu_void_p(const char *p, const formatdef *f)
601 void *x;
602 memcpy((char *)&x, p, sizeof x);
603 return PyLong_FromVoidPtr(x);
606 static int
607 np_byte(char *p, PyObject *v, const formatdef *f)
609 long x;
610 if (get_long(v, &x) < 0)
611 return -1;
612 if (x < -128 || x > 127){
613 PyErr_SetString(StructError,
614 "byte format requires -128<=number<=127");
615 return -1;
617 *p = (char)x;
618 return 0;
621 static int
622 np_ubyte(char *p, PyObject *v, const formatdef *f)
624 long x;
625 if (get_long(v, &x) < 0)
626 return -1;
627 if (x < 0 || x > 255){
628 PyErr_SetString(StructError,
629 "ubyte format requires 0<=number<=255");
630 return -1;
632 *p = (char)x;
633 return 0;
636 static int
637 np_char(char *p, PyObject *v, const formatdef *f)
639 if (!PyString_Check(v) || PyString_Size(v) != 1) {
640 PyErr_SetString(StructError,
641 "char format require string of length 1");
642 return -1;
644 *p = *PyString_AsString(v);
645 return 0;
648 static int
649 np_short(char *p, PyObject *v, const formatdef *f)
651 long x;
652 short y;
653 if (get_long(v, &x) < 0)
654 return -1;
655 if (x < SHRT_MIN || x > SHRT_MAX){
656 PyErr_SetString(StructError,
657 "short format requires " STRINGIFY(SHRT_MIN)
658 "<=number<=" STRINGIFY(SHRT_MAX));
659 return -1;
661 y = (short)x;
662 memcpy(p, (char *)&y, sizeof y);
663 return 0;
666 static int
667 np_ushort(char *p, PyObject *v, const formatdef *f)
669 long x;
670 unsigned short y;
671 if (get_long(v, &x) < 0)
672 return -1;
673 if (x < 0 || x > USHRT_MAX){
674 PyErr_SetString(StructError,
675 "short format requires 0<=number<=" STRINGIFY(USHRT_MAX));
676 return -1;
678 y = (unsigned short)x;
679 memcpy(p, (char *)&y, sizeof y);
680 return 0;
683 static int
684 np_int(char *p, PyObject *v, const formatdef *f)
686 long x;
687 int y;
688 if (get_long(v, &x) < 0)
689 return -1;
690 y = (int)x;
691 memcpy(p, (char *)&y, sizeof y);
692 return 0;
695 static int
696 np_uint(char *p, PyObject *v, const formatdef *f)
698 unsigned long x;
699 unsigned int y;
700 if (get_ulong(v, &x) < 0)
701 return -1;
702 y = (unsigned int)x;
703 memcpy(p, (char *)&y, sizeof y);
704 return 0;
707 static int
708 np_long(char *p, PyObject *v, const formatdef *f)
710 long x;
711 if (get_long(v, &x) < 0)
712 return -1;
713 memcpy(p, (char *)&x, sizeof x);
714 return 0;
717 static int
718 np_ulong(char *p, PyObject *v, const formatdef *f)
720 unsigned long x;
721 if (get_ulong(v, &x) < 0)
722 return -1;
723 memcpy(p, (char *)&x, sizeof x);
724 return 0;
727 #ifdef HAVE_LONG_LONG
729 static int
730 np_longlong(char *p, PyObject *v, const formatdef *f)
732 LONG_LONG x;
733 if (get_longlong(v, &x) < 0)
734 return -1;
735 memcpy(p, (char *)&x, sizeof x);
736 return 0;
739 static int
740 np_ulonglong(char *p, PyObject *v, const formatdef *f)
742 unsigned LONG_LONG x;
743 if (get_ulonglong(v, &x) < 0)
744 return -1;
745 memcpy(p, (char *)&x, sizeof x);
746 return 0;
748 #endif
750 static int
751 np_float(char *p, PyObject *v, const formatdef *f)
753 float x = (float)PyFloat_AsDouble(v);
754 if (x == -1 && PyErr_Occurred()) {
755 PyErr_SetString(StructError,
756 "required argument is not a float");
757 return -1;
759 memcpy(p, (char *)&x, sizeof x);
760 return 0;
763 static int
764 np_double(char *p, PyObject *v, const formatdef *f)
766 double x = PyFloat_AsDouble(v);
767 if (x == -1 && PyErr_Occurred()) {
768 PyErr_SetString(StructError,
769 "required argument is not a float");
770 return -1;
772 memcpy(p, (char *)&x, sizeof(double));
773 return 0;
776 static int
777 np_void_p(char *p, PyObject *v, const formatdef *f)
779 void *x = PyLong_AsVoidPtr(v);
780 if (x == NULL && PyErr_Occurred()) {
781 /* ### hrm. PyLong_AsVoidPtr raises SystemError */
782 if (PyErr_ExceptionMatches(PyExc_TypeError))
783 PyErr_SetString(StructError,
784 "required argument is not an integer");
785 return -1;
787 memcpy(p, (char *)&x, sizeof x);
788 return 0;
791 static formatdef native_table[] = {
792 {'x', sizeof(char), 0, NULL},
793 {'b', sizeof(char), 0, nu_byte, np_byte},
794 {'B', sizeof(char), 0, nu_ubyte, np_ubyte},
795 {'c', sizeof(char), 0, nu_char, np_char},
796 {'s', sizeof(char), 0, NULL},
797 {'p', sizeof(char), 0, NULL},
798 {'h', sizeof(short), SHORT_ALIGN, nu_short, np_short},
799 {'H', sizeof(short), SHORT_ALIGN, nu_ushort, np_ushort},
800 {'i', sizeof(int), INT_ALIGN, nu_int, np_int},
801 {'I', sizeof(int), INT_ALIGN, nu_uint, np_uint},
802 {'l', sizeof(long), LONG_ALIGN, nu_long, np_long},
803 {'L', sizeof(long), LONG_ALIGN, nu_ulong, np_ulong},
804 {'f', sizeof(float), FLOAT_ALIGN, nu_float, np_float},
805 {'d', sizeof(double), DOUBLE_ALIGN, nu_double, np_double},
806 {'P', sizeof(void *), VOID_P_ALIGN, nu_void_p, np_void_p},
807 #ifdef HAVE_LONG_LONG
808 {'q', sizeof(LONG_LONG), LONG_LONG_ALIGN, nu_longlong, np_longlong},
809 {'Q', sizeof(LONG_LONG), LONG_LONG_ALIGN, nu_ulonglong,np_ulonglong},
810 #endif
814 /* Big-endian routines. *****************************************************/
816 static PyObject *
817 bu_int(const char *p, const formatdef *f)
819 long x = 0;
820 int i = f->size;
821 do {
822 x = (x<<8) | (*p++ & 0xFF);
823 } while (--i > 0);
824 /* Extend the sign bit. */
825 if (SIZEOF_LONG > f->size)
826 x |= -(x & (1L << (8*f->size - 1)));
827 return PyInt_FromLong(x);
830 static PyObject *
831 bu_uint(const char *p, const formatdef *f)
833 unsigned long x = 0;
834 int i = f->size;
835 do {
836 x = (x<<8) | (*p++ & 0xFF);
837 } while (--i > 0);
838 if (f->size >= 4)
839 return PyLong_FromUnsignedLong(x);
840 else
841 return PyInt_FromLong((long)x);
844 static PyObject *
845 bu_longlong(const char *p, const formatdef *f)
847 return _PyLong_FromByteArray((const unsigned char *)p,
849 0, /* little-endian */
850 1 /* signed */);
853 static PyObject *
854 bu_ulonglong(const char *p, const formatdef *f)
856 return _PyLong_FromByteArray((const unsigned char *)p,
858 0, /* little-endian */
859 0 /* signed */);
862 static PyObject *
863 bu_float(const char *p, const formatdef *f)
865 return unpack_float(p, 1);
868 static PyObject *
869 bu_double(const char *p, const formatdef *f)
871 return unpack_double(p, 1);
874 static int
875 bp_int(char *p, PyObject *v, const formatdef *f)
877 long x;
878 int i;
879 if (get_long(v, &x) < 0)
880 return -1;
881 i = f->size;
882 do {
883 p[--i] = (char)x;
884 x >>= 8;
885 } while (i > 0);
886 return 0;
889 static int
890 bp_uint(char *p, PyObject *v, const formatdef *f)
892 unsigned long x;
893 int i;
894 if (get_ulong(v, &x) < 0)
895 return -1;
896 i = f->size;
897 do {
898 p[--i] = (char)x;
899 x >>= 8;
900 } while (i > 0);
901 return 0;
904 static int
905 bp_longlong(char *p, PyObject *v, const formatdef *f)
907 int res;
908 v = get_pylong(v);
909 if (v == NULL)
910 return -1;
911 res = _PyLong_AsByteArray((PyLongObject *)v,
912 (unsigned char *)p,
914 0, /* little_endian */
915 1 /* signed */);
916 Py_DECREF(v);
917 return res;
920 static int
921 bp_ulonglong(char *p, PyObject *v, const formatdef *f)
923 int res;
924 v = get_pylong(v);
925 if (v == NULL)
926 return -1;
927 res = _PyLong_AsByteArray((PyLongObject *)v,
928 (unsigned char *)p,
930 0, /* little_endian */
931 0 /* signed */);
932 Py_DECREF(v);
933 return res;
936 static int
937 bp_float(char *p, PyObject *v, const formatdef *f)
939 double x = PyFloat_AsDouble(v);
940 if (x == -1 && PyErr_Occurred()) {
941 PyErr_SetString(StructError,
942 "required argument is not a float");
943 return -1;
945 return pack_float(x, p, 1);
948 static int
949 bp_double(char *p, PyObject *v, const formatdef *f)
951 double x = PyFloat_AsDouble(v);
952 if (x == -1 && PyErr_Occurred()) {
953 PyErr_SetString(StructError,
954 "required argument is not a float");
955 return -1;
957 return pack_double(x, p, 1);
960 static formatdef bigendian_table[] = {
961 {'x', 1, 0, NULL},
962 {'b', 1, 0, bu_int, bp_int},
963 {'B', 1, 0, bu_uint, bp_int},
964 {'c', 1, 0, nu_char, np_char},
965 {'s', 1, 0, NULL},
966 {'p', 1, 0, NULL},
967 {'h', 2, 0, bu_int, bp_int},
968 {'H', 2, 0, bu_uint, bp_uint},
969 {'i', 4, 0, bu_int, bp_int},
970 {'I', 4, 0, bu_uint, bp_uint},
971 {'l', 4, 0, bu_int, bp_int},
972 {'L', 4, 0, bu_uint, bp_uint},
973 {'q', 8, 0, bu_longlong, bp_longlong},
974 {'Q', 8, 0, bu_ulonglong, bp_ulonglong},
975 {'f', 4, 0, bu_float, bp_float},
976 {'d', 8, 0, bu_double, bp_double},
980 /* Little-endian routines. *****************************************************/
982 static PyObject *
983 lu_int(const char *p, const formatdef *f)
985 long x = 0;
986 int i = f->size;
987 do {
988 x = (x<<8) | (p[--i] & 0xFF);
989 } while (i > 0);
990 /* Extend the sign bit. */
991 if (SIZEOF_LONG > f->size)
992 x |= -(x & (1L << (8*f->size - 1)));
993 return PyInt_FromLong(x);
996 static PyObject *
997 lu_uint(const char *p, const formatdef *f)
999 unsigned long x = 0;
1000 int i = f->size;
1001 do {
1002 x = (x<<8) | (p[--i] & 0xFF);
1003 } while (i > 0);
1004 if (f->size >= 4)
1005 return PyLong_FromUnsignedLong(x);
1006 else
1007 return PyInt_FromLong((long)x);
1010 static PyObject *
1011 lu_longlong(const char *p, const formatdef *f)
1013 return _PyLong_FromByteArray((const unsigned char *)p,
1015 1, /* little-endian */
1016 1 /* signed */);
1019 static PyObject *
1020 lu_ulonglong(const char *p, const formatdef *f)
1022 return _PyLong_FromByteArray((const unsigned char *)p,
1024 1, /* little-endian */
1025 0 /* signed */);
1028 static PyObject *
1029 lu_float(const char *p, const formatdef *f)
1031 return unpack_float(p+3, -1);
1034 static PyObject *
1035 lu_double(const char *p, const formatdef *f)
1037 return unpack_double(p+7, -1);
1040 static int
1041 lp_int(char *p, PyObject *v, const formatdef *f)
1043 long x;
1044 int i;
1045 if (get_long(v, &x) < 0)
1046 return -1;
1047 i = f->size;
1048 do {
1049 *p++ = (char)x;
1050 x >>= 8;
1051 } while (--i > 0);
1052 return 0;
1055 static int
1056 lp_uint(char *p, PyObject *v, const formatdef *f)
1058 unsigned long x;
1059 int i;
1060 if (get_ulong(v, &x) < 0)
1061 return -1;
1062 i = f->size;
1063 do {
1064 *p++ = (char)x;
1065 x >>= 8;
1066 } while (--i > 0);
1067 return 0;
1070 static int
1071 lp_longlong(char *p, PyObject *v, const formatdef *f)
1073 int res;
1074 v = get_pylong(v);
1075 if (v == NULL)
1076 return -1;
1077 res = _PyLong_AsByteArray((PyLongObject*)v,
1078 (unsigned char *)p,
1080 1, /* little_endian */
1081 1 /* signed */);
1082 Py_DECREF(v);
1083 return res;
1086 static int
1087 lp_ulonglong(char *p, PyObject *v, const formatdef *f)
1089 int res;
1090 v = get_pylong(v);
1091 if (v == NULL)
1092 return -1;
1093 res = _PyLong_AsByteArray((PyLongObject*)v,
1094 (unsigned char *)p,
1096 1, /* little_endian */
1097 0 /* signed */);
1098 Py_DECREF(v);
1099 return res;
1102 static int
1103 lp_float(char *p, PyObject *v, const formatdef *f)
1105 double x = PyFloat_AsDouble(v);
1106 if (x == -1 && PyErr_Occurred()) {
1107 PyErr_SetString(StructError,
1108 "required argument is not a float");
1109 return -1;
1111 return pack_float(x, p+3, -1);
1114 static int
1115 lp_double(char *p, PyObject *v, const formatdef *f)
1117 double x = PyFloat_AsDouble(v);
1118 if (x == -1 && PyErr_Occurred()) {
1119 PyErr_SetString(StructError,
1120 "required argument is not a float");
1121 return -1;
1123 return pack_double(x, p+7, -1);
1126 static formatdef lilendian_table[] = {
1127 {'x', 1, 0, NULL},
1128 {'b', 1, 0, lu_int, lp_int},
1129 {'B', 1, 0, lu_uint, lp_int},
1130 {'c', 1, 0, nu_char, np_char},
1131 {'s', 1, 0, NULL},
1132 {'p', 1, 0, NULL},
1133 {'h', 2, 0, lu_int, lp_int},
1134 {'H', 2, 0, lu_uint, lp_uint},
1135 {'i', 4, 0, lu_int, lp_int},
1136 {'I', 4, 0, lu_uint, lp_uint},
1137 {'l', 4, 0, lu_int, lp_int},
1138 {'L', 4, 0, lu_uint, lp_uint},
1139 {'q', 8, 0, lu_longlong, lp_longlong},
1140 {'Q', 8, 0, lu_ulonglong, lp_ulonglong},
1141 {'f', 4, 0, lu_float, lp_float},
1142 {'d', 8, 0, lu_double, lp_double},
1147 static const formatdef *
1148 whichtable(char **pfmt)
1150 const char *fmt = (*pfmt)++; /* May be backed out of later */
1151 switch (*fmt) {
1152 case '<':
1153 return lilendian_table;
1154 case '>':
1155 case '!': /* Network byte order is big-endian */
1156 return bigendian_table;
1157 case '=': { /* Host byte order -- different from native in aligment! */
1158 int n = 1;
1159 char *p = (char *) &n;
1160 if (*p == 1)
1161 return lilendian_table;
1162 else
1163 return bigendian_table;
1165 default:
1166 --*pfmt; /* Back out of pointer increment */
1167 /* Fall through */
1168 case '@':
1169 return native_table;
1174 /* Get the table entry for a format code */
1176 static const formatdef *
1177 getentry(int c, const formatdef *f)
1179 for (; f->format != '\0'; f++) {
1180 if (f->format == c) {
1181 return f;
1184 PyErr_SetString(StructError, "bad char in struct format");
1185 return NULL;
1189 /* Align a size according to a format code */
1191 static int
1192 align(int size, int c, const formatdef *e)
1194 if (e->format == c) {
1195 if (e->alignment) {
1196 size = ((size + e->alignment - 1)
1197 / e->alignment)
1198 * e->alignment;
1201 return size;
1205 /* calculate the size of a format string */
1207 static int
1208 calcsize(const char *fmt, const formatdef *f)
1210 const formatdef *e;
1211 const char *s;
1212 char c;
1213 int size, num, itemsize, x;
1215 s = fmt;
1216 size = 0;
1217 while ((c = *s++) != '\0') {
1218 if (isspace((int)c))
1219 continue;
1220 if ('0' <= c && c <= '9') {
1221 num = c - '0';
1222 while ('0' <= (c = *s++) && c <= '9') {
1223 x = num*10 + (c - '0');
1224 if (x/10 != num) {
1225 PyErr_SetString(
1226 StructError,
1227 "overflow in item count");
1228 return -1;
1230 num = x;
1232 if (c == '\0')
1233 break;
1235 else
1236 num = 1;
1238 e = getentry(c, f);
1239 if (e == NULL)
1240 return -1;
1241 itemsize = e->size;
1242 size = align(size, c, e);
1243 x = num * itemsize;
1244 size += x;
1245 if (x/itemsize != num || size < 0) {
1246 PyErr_SetString(StructError,
1247 "total struct size too long");
1248 return -1;
1252 return size;
1256 PyDoc_STRVAR(calcsize__doc__,
1257 "calcsize(fmt) -> int\n\
1258 Return size of C struct described by format string fmt.\n\
1259 See struct.__doc__ for more on format strings.");
1261 static PyObject *
1262 struct_calcsize(PyObject *self, PyObject *args)
1264 char *fmt;
1265 const formatdef *f;
1266 int size;
1268 if (!PyArg_ParseTuple(args, "s:calcsize", &fmt))
1269 return NULL;
1270 f = whichtable(&fmt);
1271 size = calcsize(fmt, f);
1272 if (size < 0)
1273 return NULL;
1274 return PyInt_FromLong((long)size);
1278 PyDoc_STRVAR(pack__doc__,
1279 "pack(fmt, v1, v2, ...) -> string\n\
1280 Return string containing values v1, v2, ... packed according to fmt.\n\
1281 See struct.__doc__ for more on format strings.");
1283 static PyObject *
1284 struct_pack(PyObject *self, PyObject *args)
1286 const formatdef *f, *e;
1287 PyObject *format, *result, *v;
1288 char *fmt;
1289 int size, num;
1290 int i, n;
1291 char *s, *res, *restart, *nres;
1292 char c;
1294 if (args == NULL || !PyTuple_Check(args) ||
1295 (n = PyTuple_Size(args)) < 1)
1297 PyErr_SetString(PyExc_TypeError,
1298 "struct.pack requires at least one argument");
1299 return NULL;
1301 format = PyTuple_GetItem(args, 0);
1302 fmt = PyString_AsString(format);
1303 if (!fmt)
1304 return NULL;
1305 f = whichtable(&fmt);
1306 size = calcsize(fmt, f);
1307 if (size < 0)
1308 return NULL;
1309 result = PyString_FromStringAndSize((char *)NULL, size);
1310 if (result == NULL)
1311 return NULL;
1313 s = fmt;
1314 i = 1;
1315 res = restart = PyString_AsString(result);
1317 while ((c = *s++) != '\0') {
1318 if (isspace((int)c))
1319 continue;
1320 if ('0' <= c && c <= '9') {
1321 num = c - '0';
1322 while ('0' <= (c = *s++) && c <= '9')
1323 num = num*10 + (c - '0');
1324 if (c == '\0')
1325 break;
1327 else
1328 num = 1;
1330 e = getentry(c, f);
1331 if (e == NULL)
1332 goto fail;
1333 nres = restart + align((int)(res-restart), c, e);
1334 /* Fill padd bytes with zeros */
1335 while (res < nres)
1336 *res++ = '\0';
1337 if (num == 0 && c != 's')
1338 continue;
1339 do {
1340 if (c == 'x') {
1341 /* doesn't consume arguments */
1342 memset(res, '\0', num);
1343 res += num;
1344 break;
1346 if (i >= n) {
1347 PyErr_SetString(StructError,
1348 "insufficient arguments to pack");
1349 goto fail;
1351 v = PyTuple_GetItem(args, i++);
1352 if (v == NULL)
1353 goto fail;
1354 if (c == 's') {
1355 /* num is string size, not repeat count */
1356 int n;
1357 if (!PyString_Check(v)) {
1358 PyErr_SetString(StructError,
1359 "argument for 's' must be a string");
1360 goto fail;
1362 n = PyString_Size(v);
1363 if (n > num)
1364 n = num;
1365 if (n > 0)
1366 memcpy(res, PyString_AsString(v), n);
1367 if (n < num)
1368 memset(res+n, '\0', num-n);
1369 res += num;
1370 break;
1372 else if (c == 'p') {
1373 /* num is string size + 1,
1374 to fit in the count byte */
1375 int n;
1376 num--; /* now num is max string size */
1377 if (!PyString_Check(v)) {
1378 PyErr_SetString(StructError,
1379 "argument for 'p' must be a string");
1380 goto fail;
1382 n = PyString_Size(v);
1383 if (n > num)
1384 n = num;
1385 if (n > 0)
1386 memcpy(res+1, PyString_AsString(v), n);
1387 if (n < num)
1388 /* no real need, just to be nice */
1389 memset(res+1+n, '\0', num-n);
1390 if (n > 255)
1391 n = 255;
1392 *res++ = n; /* store the length byte */
1393 res += num;
1394 break;
1396 else {
1397 if (e->pack(res, v, e) < 0)
1398 goto fail;
1399 res += e->size;
1401 } while (--num > 0);
1404 if (i < n) {
1405 PyErr_SetString(StructError,
1406 "too many arguments for pack format");
1407 goto fail;
1410 return result;
1412 fail:
1413 Py_DECREF(result);
1414 return NULL;
1418 PyDoc_STRVAR(unpack__doc__,
1419 "unpack(fmt, string) -> (v1, v2, ...)\n\
1420 Unpack the string, containing packed C structure data, according\n\
1421 to fmt. Requires len(string)==calcsize(fmt).\n\
1422 See struct.__doc__ for more on format strings.");
1424 static PyObject *
1425 struct_unpack(PyObject *self, PyObject *args)
1427 const formatdef *f, *e;
1428 char *str, *start, *fmt, *s;
1429 char c;
1430 int len, size, num;
1431 PyObject *res, *v;
1433 if (!PyArg_ParseTuple(args, "ss#:unpack", &fmt, &start, &len))
1434 return NULL;
1435 f = whichtable(&fmt);
1436 size = calcsize(fmt, f);
1437 if (size < 0)
1438 return NULL;
1439 if (size != len) {
1440 PyErr_SetString(StructError,
1441 "unpack str size does not match format");
1442 return NULL;
1444 res = PyList_New(0);
1445 if (res == NULL)
1446 return NULL;
1447 str = start;
1448 s = fmt;
1449 while ((c = *s++) != '\0') {
1450 if (isspace((int)c))
1451 continue;
1452 if ('0' <= c && c <= '9') {
1453 num = c - '0';
1454 while ('0' <= (c = *s++) && c <= '9')
1455 num = num*10 + (c - '0');
1456 if (c == '\0')
1457 break;
1459 else
1460 num = 1;
1462 e = getentry(c, f);
1463 if (e == NULL)
1464 goto fail;
1465 str = start + align((int)(str-start), c, e);
1466 if (num == 0 && c != 's')
1467 continue;
1469 do {
1470 if (c == 'x') {
1471 str += num;
1472 break;
1474 if (c == 's') {
1475 /* num is string size, not repeat count */
1476 v = PyString_FromStringAndSize(str, num);
1477 if (v == NULL)
1478 goto fail;
1479 str += num;
1480 num = 0;
1482 else if (c == 'p') {
1483 /* num is string buffer size,
1484 not repeat count */
1485 int n = *(unsigned char*)str;
1486 /* first byte (unsigned) is string size */
1487 if (n >= num)
1488 n = num-1;
1489 v = PyString_FromStringAndSize(str+1, n);
1490 if (v == NULL)
1491 goto fail;
1492 str += num;
1493 num = 0;
1495 else {
1496 v = e->unpack(str, e);
1497 if (v == NULL)
1498 goto fail;
1499 str += e->size;
1501 if (v == NULL || PyList_Append(res, v) < 0)
1502 goto fail;
1503 Py_DECREF(v);
1504 } while (--num > 0);
1507 v = PyList_AsTuple(res);
1508 Py_DECREF(res);
1509 return v;
1511 fail:
1512 Py_DECREF(res);
1513 return NULL;
1517 /* List of functions */
1519 static PyMethodDef struct_methods[] = {
1520 {"calcsize", struct_calcsize, METH_VARARGS, calcsize__doc__},
1521 {"pack", struct_pack, METH_VARARGS, pack__doc__},
1522 {"unpack", struct_unpack, METH_VARARGS, unpack__doc__},
1523 {NULL, NULL} /* sentinel */
1527 /* Module initialization */
1529 PyMODINIT_FUNC
1530 initstruct(void)
1532 PyObject *m;
1534 /* Create the module and add the functions */
1535 m = Py_InitModule4("struct", struct_methods, struct__doc__,
1536 (PyObject*)NULL, PYTHON_API_VERSION);
1538 /* Add some symbolic constants to the module */
1539 if (StructError == NULL) {
1540 StructError = PyErr_NewException("struct.error", NULL, NULL);
1541 if (StructError == NULL)
1542 return;
1544 Py_INCREF(StructError);
1545 PyModule_AddObject(m, "error", StructError);