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 */
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\
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\
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\
34 The variable struct.error is an exception raised on errors.");
39 static PyObject
*StructError
;
42 /* Define various structs to figure out the alignments of types */
45 typedef struct { char c
; short x
; } st_short
;
46 typedef struct { char c
; int x
; } st_int
;
47 typedef struct { char c
; long x
; } st_long
;
48 typedef struct { char c
; float x
; } st_float
;
49 typedef struct { char c
; double x
; } st_double
;
50 typedef struct { char c
; void *x
; } st_void_p
;
52 #define SHORT_ALIGN (sizeof(st_short) - sizeof(short))
53 #define INT_ALIGN (sizeof(st_int) - sizeof(int))
54 #define LONG_ALIGN (sizeof(st_long) - sizeof(long))
55 #define FLOAT_ALIGN (sizeof(st_float) - sizeof(float))
56 #define DOUBLE_ALIGN (sizeof(st_double) - sizeof(double))
57 #define VOID_P_ALIGN (sizeof(st_void_p) - sizeof(void *))
59 /* We can't support q and Q in native mode unless the compiler does;
60 in std mode, they're 8 bytes on all platforms. */
62 typedef struct { char c
; PY_LONG_LONG x
; } s_long_long
;
63 #define LONG_LONG_ALIGN (sizeof(s_long_long) - sizeof(PY_LONG_LONG))
66 #define STRINGIFY(x) #x
69 #pragma options align=reset
72 /* Helper to get a PyLongObject by hook or by crook. Caller should decref. */
75 get_pylong(PyObject
*v
)
81 return PyLong_FromLong(PyInt_AS_LONG(v
));
82 if (PyLong_Check(v
)) {
86 m
= v
->ob_type
->tp_as_number
;
87 if (m
!= NULL
&& m
->nb_long
!= NULL
) {
95 PyErr_SetString(StructError
,
96 "cannot convert argument to long");
100 /* Helper routine to get a Python integer and raise the appropriate error
104 get_long(PyObject
*v
, long *p
)
106 long x
= PyInt_AsLong(v
);
107 if (x
== -1 && PyErr_Occurred()) {
108 if (PyErr_ExceptionMatches(PyExc_TypeError
))
109 PyErr_SetString(StructError
,
110 "required argument is not an integer");
118 /* Same, but handling unsigned long */
121 get_ulong(PyObject
*v
, unsigned long *p
)
123 if (PyLong_Check(v
)) {
124 unsigned long x
= PyLong_AsUnsignedLong(v
);
125 if (x
== (unsigned long)(-1) && PyErr_Occurred())
131 return get_long(v
, (long *)p
);
135 #ifdef HAVE_LONG_LONG
137 /* Same, but handling native long long. */
140 get_longlong(PyObject
*v
, PY_LONG_LONG
*p
)
147 assert(PyLong_Check(v
));
148 x
= PyLong_AsLongLong(v
);
150 if (x
== (PY_LONG_LONG
)-1 && PyErr_Occurred())
156 /* Same, but handling native unsigned long long. */
159 get_ulonglong(PyObject
*v
, unsigned PY_LONG_LONG
*p
)
161 unsigned PY_LONG_LONG x
;
166 assert(PyLong_Check(v
));
167 x
= PyLong_AsUnsignedLongLong(v
);
169 if (x
== (unsigned PY_LONG_LONG
)-1 && PyErr_Occurred())
177 /* Floating point helpers */
180 unpack_float(const char *p
, /* start of 4-byte string */
181 int le
) /* true for little-endian, false for big-endian */
185 x
= _PyFloat_Unpack4((unsigned char *)p
, le
);
186 if (x
== -1.0 && PyErr_Occurred())
188 return PyFloat_FromDouble(x
);
192 unpack_double(const char *p
, /* start of 8-byte string */
193 int le
) /* true for little-endian, false for big-endian */
197 x
= _PyFloat_Unpack8((unsigned char *)p
, le
);
198 if (x
== -1.0 && PyErr_Occurred())
200 return PyFloat_FromDouble(x
);
204 /* The translation function for each format character is table driven */
206 typedef struct _formatdef
{
210 PyObject
* (*unpack
)(const char *,
211 const struct _formatdef
*);
212 int (*pack
)(char *, PyObject
*,
213 const struct _formatdef
*);
216 /* A large number of small routines follow, with names of the form
220 [bln] distiguishes among big-endian, little-endian and native.
221 [pu] distiguishes between pack (to struct) and unpack (from struct).
222 TYPE is one of char, byte, ubyte, etc.
225 /* Native mode routines. ****************************************************/
227 In all n[up]_<type> routines handling types larger than 1 byte, there is
228 *no* guarantee that the p pointer is properly aligned for each type,
229 therefore memcpy is called. An intermediate variable is used to
230 compensate for big-endian architectures.
231 Normally both the intermediate variable and the memcpy call will be
232 skipped by C optimisation in little-endian architectures (gcc >= 2.91
236 nu_char(const char *p
, const formatdef
*f
)
238 return PyString_FromStringAndSize(p
, 1);
242 nu_byte(const char *p
, const formatdef
*f
)
244 return PyInt_FromLong((long) *(signed char *)p
);
248 nu_ubyte(const char *p
, const formatdef
*f
)
250 return PyInt_FromLong((long) *(unsigned char *)p
);
254 nu_short(const char *p
, const formatdef
*f
)
257 memcpy((char *)&x
, p
, sizeof x
);
258 return PyInt_FromLong((long)x
);
262 nu_ushort(const char *p
, const formatdef
*f
)
265 memcpy((char *)&x
, p
, sizeof x
);
266 return PyInt_FromLong((long)x
);
270 nu_int(const char *p
, const formatdef
*f
)
273 memcpy((char *)&x
, p
, sizeof x
);
274 return PyInt_FromLong((long)x
);
278 nu_uint(const char *p
, const formatdef
*f
)
281 memcpy((char *)&x
, p
, sizeof x
);
282 return PyLong_FromUnsignedLong((unsigned long)x
);
286 nu_long(const char *p
, const formatdef
*f
)
289 memcpy((char *)&x
, p
, sizeof x
);
290 return PyInt_FromLong(x
);
294 nu_ulong(const char *p
, const formatdef
*f
)
297 memcpy((char *)&x
, p
, sizeof x
);
298 return PyLong_FromUnsignedLong(x
);
301 /* Native mode doesn't support q or Q unless the platform C supports
302 long long (or, on Windows, __int64). */
304 #ifdef HAVE_LONG_LONG
307 nu_longlong(const char *p
, const formatdef
*f
)
310 memcpy((char *)&x
, p
, sizeof x
);
311 return PyLong_FromLongLong(x
);
315 nu_ulonglong(const char *p
, const formatdef
*f
)
317 unsigned PY_LONG_LONG x
;
318 memcpy((char *)&x
, p
, sizeof x
);
319 return PyLong_FromUnsignedLongLong(x
);
325 nu_float(const char *p
, const formatdef
*f
)
328 memcpy((char *)&x
, p
, sizeof x
);
329 return PyFloat_FromDouble((double)x
);
333 nu_double(const char *p
, const formatdef
*f
)
336 memcpy((char *)&x
, p
, sizeof x
);
337 return PyFloat_FromDouble(x
);
341 nu_void_p(const char *p
, const formatdef
*f
)
344 memcpy((char *)&x
, p
, sizeof x
);
345 return PyLong_FromVoidPtr(x
);
349 np_byte(char *p
, PyObject
*v
, const formatdef
*f
)
352 if (get_long(v
, &x
) < 0)
354 if (x
< -128 || x
> 127){
355 PyErr_SetString(StructError
,
356 "byte format requires -128<=number<=127");
364 np_ubyte(char *p
, PyObject
*v
, const formatdef
*f
)
367 if (get_long(v
, &x
) < 0)
369 if (x
< 0 || x
> 255){
370 PyErr_SetString(StructError
,
371 "ubyte format requires 0<=number<=255");
379 np_char(char *p
, PyObject
*v
, const formatdef
*f
)
381 if (!PyString_Check(v
) || PyString_Size(v
) != 1) {
382 PyErr_SetString(StructError
,
383 "char format require string of length 1");
386 *p
= *PyString_AsString(v
);
391 np_short(char *p
, PyObject
*v
, const formatdef
*f
)
395 if (get_long(v
, &x
) < 0)
397 if (x
< SHRT_MIN
|| x
> SHRT_MAX
){
398 PyErr_SetString(StructError
,
399 "short format requires " STRINGIFY(SHRT_MIN
)
400 "<=number<=" STRINGIFY(SHRT_MAX
));
404 memcpy(p
, (char *)&y
, sizeof y
);
409 np_ushort(char *p
, PyObject
*v
, const formatdef
*f
)
413 if (get_long(v
, &x
) < 0)
415 if (x
< 0 || x
> USHRT_MAX
){
416 PyErr_SetString(StructError
,
417 "short format requires 0<=number<=" STRINGIFY(USHRT_MAX
));
420 y
= (unsigned short)x
;
421 memcpy(p
, (char *)&y
, sizeof y
);
426 np_int(char *p
, PyObject
*v
, const formatdef
*f
)
430 if (get_long(v
, &x
) < 0)
433 memcpy(p
, (char *)&y
, sizeof y
);
438 np_uint(char *p
, PyObject
*v
, const formatdef
*f
)
442 if (get_ulong(v
, &x
) < 0)
445 memcpy(p
, (char *)&y
, sizeof y
);
450 np_long(char *p
, PyObject
*v
, const formatdef
*f
)
453 if (get_long(v
, &x
) < 0)
455 memcpy(p
, (char *)&x
, sizeof x
);
460 np_ulong(char *p
, PyObject
*v
, const formatdef
*f
)
463 if (get_ulong(v
, &x
) < 0)
465 memcpy(p
, (char *)&x
, sizeof x
);
469 #ifdef HAVE_LONG_LONG
472 np_longlong(char *p
, PyObject
*v
, const formatdef
*f
)
475 if (get_longlong(v
, &x
) < 0)
477 memcpy(p
, (char *)&x
, sizeof x
);
482 np_ulonglong(char *p
, PyObject
*v
, const formatdef
*f
)
484 unsigned PY_LONG_LONG x
;
485 if (get_ulonglong(v
, &x
) < 0)
487 memcpy(p
, (char *)&x
, sizeof x
);
493 np_float(char *p
, PyObject
*v
, const formatdef
*f
)
495 float x
= (float)PyFloat_AsDouble(v
);
496 if (x
== -1 && PyErr_Occurred()) {
497 PyErr_SetString(StructError
,
498 "required argument is not a float");
501 memcpy(p
, (char *)&x
, sizeof x
);
506 np_double(char *p
, PyObject
*v
, const formatdef
*f
)
508 double x
= PyFloat_AsDouble(v
);
509 if (x
== -1 && PyErr_Occurred()) {
510 PyErr_SetString(StructError
,
511 "required argument is not a float");
514 memcpy(p
, (char *)&x
, sizeof(double));
519 np_void_p(char *p
, PyObject
*v
, const formatdef
*f
)
521 void *x
= PyLong_AsVoidPtr(v
);
522 if (x
== NULL
&& PyErr_Occurred()) {
523 /* ### hrm. PyLong_AsVoidPtr raises SystemError */
524 if (PyErr_ExceptionMatches(PyExc_TypeError
))
525 PyErr_SetString(StructError
,
526 "required argument is not an integer");
529 memcpy(p
, (char *)&x
, sizeof x
);
533 static formatdef native_table
[] = {
534 {'x', sizeof(char), 0, NULL
},
535 {'b', sizeof(char), 0, nu_byte
, np_byte
},
536 {'B', sizeof(char), 0, nu_ubyte
, np_ubyte
},
537 {'c', sizeof(char), 0, nu_char
, np_char
},
538 {'s', sizeof(char), 0, NULL
},
539 {'p', sizeof(char), 0, NULL
},
540 {'h', sizeof(short), SHORT_ALIGN
, nu_short
, np_short
},
541 {'H', sizeof(short), SHORT_ALIGN
, nu_ushort
, np_ushort
},
542 {'i', sizeof(int), INT_ALIGN
, nu_int
, np_int
},
543 {'I', sizeof(int), INT_ALIGN
, nu_uint
, np_uint
},
544 {'l', sizeof(long), LONG_ALIGN
, nu_long
, np_long
},
545 {'L', sizeof(long), LONG_ALIGN
, nu_ulong
, np_ulong
},
546 {'f', sizeof(float), FLOAT_ALIGN
, nu_float
, np_float
},
547 {'d', sizeof(double), DOUBLE_ALIGN
, nu_double
, np_double
},
548 {'P', sizeof(void *), VOID_P_ALIGN
, nu_void_p
, np_void_p
},
549 #ifdef HAVE_LONG_LONG
550 {'q', sizeof(PY_LONG_LONG
), LONG_LONG_ALIGN
, nu_longlong
, np_longlong
},
551 {'Q', sizeof(PY_LONG_LONG
), LONG_LONG_ALIGN
, nu_ulonglong
,np_ulonglong
},
556 /* Big-endian routines. *****************************************************/
559 bu_int(const char *p
, const formatdef
*f
)
564 x
= (x
<<8) | (*p
++ & 0xFF);
566 /* Extend the sign bit. */
567 if (SIZEOF_LONG
> f
->size
)
568 x
|= -(x
& (1L << (8*f
->size
- 1)));
569 return PyInt_FromLong(x
);
573 bu_uint(const char *p
, const formatdef
*f
)
578 x
= (x
<<8) | (*p
++ & 0xFF);
581 return PyLong_FromUnsignedLong(x
);
583 return PyInt_FromLong((long)x
);
587 bu_longlong(const char *p
, const formatdef
*f
)
589 return _PyLong_FromByteArray((const unsigned char *)p
,
591 0, /* little-endian */
596 bu_ulonglong(const char *p
, const formatdef
*f
)
598 return _PyLong_FromByteArray((const unsigned char *)p
,
600 0, /* little-endian */
605 bu_float(const char *p
, const formatdef
*f
)
607 return unpack_float(p
, 0);
611 bu_double(const char *p
, const formatdef
*f
)
613 return unpack_double(p
, 0);
617 bp_int(char *p
, PyObject
*v
, const formatdef
*f
)
621 if (get_long(v
, &x
) < 0)
632 bp_uint(char *p
, PyObject
*v
, const formatdef
*f
)
636 if (get_ulong(v
, &x
) < 0)
647 bp_longlong(char *p
, PyObject
*v
, const formatdef
*f
)
653 res
= _PyLong_AsByteArray((PyLongObject
*)v
,
656 0, /* little_endian */
663 bp_ulonglong(char *p
, PyObject
*v
, const formatdef
*f
)
669 res
= _PyLong_AsByteArray((PyLongObject
*)v
,
672 0, /* little_endian */
679 bp_float(char *p
, PyObject
*v
, const formatdef
*f
)
681 double x
= PyFloat_AsDouble(v
);
682 if (x
== -1 && PyErr_Occurred()) {
683 PyErr_SetString(StructError
,
684 "required argument is not a float");
687 return _PyFloat_Pack4(x
, (unsigned char *)p
, 0);
691 bp_double(char *p
, PyObject
*v
, const formatdef
*f
)
693 double x
= PyFloat_AsDouble(v
);
694 if (x
== -1 && PyErr_Occurred()) {
695 PyErr_SetString(StructError
,
696 "required argument is not a float");
699 return _PyFloat_Pack8(x
, (unsigned char *)p
, 0);
702 static formatdef bigendian_table
[] = {
704 {'b', 1, 0, bu_int
, bp_int
},
705 {'B', 1, 0, bu_uint
, bp_int
},
706 {'c', 1, 0, nu_char
, np_char
},
709 {'h', 2, 0, bu_int
, bp_int
},
710 {'H', 2, 0, bu_uint
, bp_uint
},
711 {'i', 4, 0, bu_int
, bp_int
},
712 {'I', 4, 0, bu_uint
, bp_uint
},
713 {'l', 4, 0, bu_int
, bp_int
},
714 {'L', 4, 0, bu_uint
, bp_uint
},
715 {'q', 8, 0, bu_longlong
, bp_longlong
},
716 {'Q', 8, 0, bu_ulonglong
, bp_ulonglong
},
717 {'f', 4, 0, bu_float
, bp_float
},
718 {'d', 8, 0, bu_double
, bp_double
},
722 /* Little-endian routines. *****************************************************/
725 lu_int(const char *p
, const formatdef
*f
)
730 x
= (x
<<8) | (p
[--i
] & 0xFF);
732 /* Extend the sign bit. */
733 if (SIZEOF_LONG
> f
->size
)
734 x
|= -(x
& (1L << (8*f
->size
- 1)));
735 return PyInt_FromLong(x
);
739 lu_uint(const char *p
, const formatdef
*f
)
744 x
= (x
<<8) | (p
[--i
] & 0xFF);
747 return PyLong_FromUnsignedLong(x
);
749 return PyInt_FromLong((long)x
);
753 lu_longlong(const char *p
, const formatdef
*f
)
755 return _PyLong_FromByteArray((const unsigned char *)p
,
757 1, /* little-endian */
762 lu_ulonglong(const char *p
, const formatdef
*f
)
764 return _PyLong_FromByteArray((const unsigned char *)p
,
766 1, /* little-endian */
771 lu_float(const char *p
, const formatdef
*f
)
773 return unpack_float(p
, 1);
777 lu_double(const char *p
, const formatdef
*f
)
779 return unpack_double(p
, 1);
783 lp_int(char *p
, PyObject
*v
, const formatdef
*f
)
787 if (get_long(v
, &x
) < 0)
798 lp_uint(char *p
, PyObject
*v
, const formatdef
*f
)
802 if (get_ulong(v
, &x
) < 0)
813 lp_longlong(char *p
, PyObject
*v
, const formatdef
*f
)
819 res
= _PyLong_AsByteArray((PyLongObject
*)v
,
822 1, /* little_endian */
829 lp_ulonglong(char *p
, PyObject
*v
, const formatdef
*f
)
835 res
= _PyLong_AsByteArray((PyLongObject
*)v
,
838 1, /* little_endian */
845 lp_float(char *p
, PyObject
*v
, const formatdef
*f
)
847 double x
= PyFloat_AsDouble(v
);
848 if (x
== -1 && PyErr_Occurred()) {
849 PyErr_SetString(StructError
,
850 "required argument is not a float");
853 return _PyFloat_Pack4(x
, (unsigned char *)p
, 1);
857 lp_double(char *p
, PyObject
*v
, const formatdef
*f
)
859 double x
= PyFloat_AsDouble(v
);
860 if (x
== -1 && PyErr_Occurred()) {
861 PyErr_SetString(StructError
,
862 "required argument is not a float");
865 return _PyFloat_Pack8(x
, (unsigned char *)p
, 1);
868 static formatdef lilendian_table
[] = {
870 {'b', 1, 0, lu_int
, lp_int
},
871 {'B', 1, 0, lu_uint
, lp_int
},
872 {'c', 1, 0, nu_char
, np_char
},
875 {'h', 2, 0, lu_int
, lp_int
},
876 {'H', 2, 0, lu_uint
, lp_uint
},
877 {'i', 4, 0, lu_int
, lp_int
},
878 {'I', 4, 0, lu_uint
, lp_uint
},
879 {'l', 4, 0, lu_int
, lp_int
},
880 {'L', 4, 0, lu_uint
, lp_uint
},
881 {'q', 8, 0, lu_longlong
, lp_longlong
},
882 {'Q', 8, 0, lu_ulonglong
, lp_ulonglong
},
883 {'f', 4, 0, lu_float
, lp_float
},
884 {'d', 8, 0, lu_double
, lp_double
},
889 static const formatdef
*
890 whichtable(char **pfmt
)
892 const char *fmt
= (*pfmt
)++; /* May be backed out of later */
895 return lilendian_table
;
897 case '!': /* Network byte order is big-endian */
898 return bigendian_table
;
899 case '=': { /* Host byte order -- different from native in aligment! */
901 char *p
= (char *) &n
;
903 return lilendian_table
;
905 return bigendian_table
;
908 --*pfmt
; /* Back out of pointer increment */
916 /* Get the table entry for a format code */
918 static const formatdef
*
919 getentry(int c
, const formatdef
*f
)
921 for (; f
->format
!= '\0'; f
++) {
922 if (f
->format
== c
) {
926 PyErr_SetString(StructError
, "bad char in struct format");
931 /* Align a size according to a format code */
934 align(int size
, int c
, const formatdef
*e
)
936 if (e
->format
== c
) {
938 size
= ((size
+ e
->alignment
- 1)
947 /* calculate the size of a format string */
950 calcsize(const char *fmt
, const formatdef
*f
)
955 int size
, num
, itemsize
, x
;
959 while ((c
= *s
++) != '\0') {
962 if ('0' <= c
&& c
<= '9') {
964 while ('0' <= (c
= *s
++) && c
<= '9') {
965 x
= num
*10 + (c
- '0');
969 "overflow in item count");
984 size
= align(size
, c
, e
);
987 if (x
/itemsize
!= num
|| size
< 0) {
988 PyErr_SetString(StructError
,
989 "total struct size too long");
998 PyDoc_STRVAR(calcsize__doc__
,
999 "calcsize(fmt) -> int\n\
1000 Return size of C struct described by format string fmt.\n\
1001 See struct.__doc__ for more on format strings.");
1004 struct_calcsize(PyObject
*self
, PyObject
*args
)
1010 if (!PyArg_ParseTuple(args
, "s:calcsize", &fmt
))
1012 f
= whichtable(&fmt
);
1013 size
= calcsize(fmt
, f
);
1016 return PyInt_FromLong((long)size
);
1020 PyDoc_STRVAR(pack__doc__
,
1021 "pack(fmt, v1, v2, ...) -> string\n\
1022 Return string containing values v1, v2, ... packed according to fmt.\n\
1023 See struct.__doc__ for more on format strings.");
1026 struct_pack(PyObject
*self
, PyObject
*args
)
1028 const formatdef
*f
, *e
;
1029 PyObject
*format
, *result
, *v
;
1033 char *s
, *res
, *restart
, *nres
;
1036 if (args
== NULL
|| !PyTuple_Check(args
) ||
1037 (n
= PyTuple_Size(args
)) < 1)
1039 PyErr_SetString(PyExc_TypeError
,
1040 "struct.pack requires at least one argument");
1043 format
= PyTuple_GetItem(args
, 0);
1044 fmt
= PyString_AsString(format
);
1047 f
= whichtable(&fmt
);
1048 size
= calcsize(fmt
, f
);
1051 result
= PyString_FromStringAndSize((char *)NULL
, size
);
1057 res
= restart
= PyString_AsString(result
);
1059 while ((c
= *s
++) != '\0') {
1060 if (isspace((int)c
))
1062 if ('0' <= c
&& c
<= '9') {
1064 while ('0' <= (c
= *s
++) && c
<= '9')
1065 num
= num
*10 + (c
- '0');
1075 nres
= restart
+ align((int)(res
-restart
), c
, e
);
1076 /* Fill padd bytes with zeros */
1079 if (num
== 0 && c
!= 's')
1083 /* doesn't consume arguments */
1084 memset(res
, '\0', num
);
1089 PyErr_SetString(StructError
,
1090 "insufficient arguments to pack");
1093 v
= PyTuple_GetItem(args
, i
++);
1097 /* num is string size, not repeat count */
1099 if (!PyString_Check(v
)) {
1100 PyErr_SetString(StructError
,
1101 "argument for 's' must be a string");
1104 n
= PyString_Size(v
);
1108 memcpy(res
, PyString_AsString(v
), n
);
1110 memset(res
+n
, '\0', num
-n
);
1114 else if (c
== 'p') {
1115 /* num is string size + 1,
1116 to fit in the count byte */
1118 num
--; /* now num is max string size */
1119 if (!PyString_Check(v
)) {
1120 PyErr_SetString(StructError
,
1121 "argument for 'p' must be a string");
1124 n
= PyString_Size(v
);
1128 memcpy(res
+1, PyString_AsString(v
), n
);
1130 /* no real need, just to be nice */
1131 memset(res
+1+n
, '\0', num
-n
);
1134 *res
++ = n
; /* store the length byte */
1139 if (e
->pack(res
, v
, e
) < 0)
1143 } while (--num
> 0);
1147 PyErr_SetString(StructError
,
1148 "too many arguments for pack format");
1160 PyDoc_STRVAR(unpack__doc__
,
1161 "unpack(fmt, string) -> (v1, v2, ...)\n\
1162 Unpack the string, containing packed C structure data, according\n\
1163 to fmt. Requires len(string)==calcsize(fmt).\n\
1164 See struct.__doc__ for more on format strings.");
1167 struct_unpack(PyObject
*self
, PyObject
*args
)
1169 const formatdef
*f
, *e
;
1170 char *str
, *start
, *fmt
, *s
;
1175 if (!PyArg_ParseTuple(args
, "ss#:unpack", &fmt
, &start
, &len
))
1177 f
= whichtable(&fmt
);
1178 size
= calcsize(fmt
, f
);
1182 PyErr_SetString(StructError
,
1183 "unpack str size does not match format");
1186 res
= PyList_New(0);
1191 while ((c
= *s
++) != '\0') {
1192 if (isspace((int)c
))
1194 if ('0' <= c
&& c
<= '9') {
1196 while ('0' <= (c
= *s
++) && c
<= '9')
1197 num
= num
*10 + (c
- '0');
1207 str
= start
+ align((int)(str
-start
), c
, e
);
1208 if (num
== 0 && c
!= 's')
1217 /* num is string size, not repeat count */
1218 v
= PyString_FromStringAndSize(str
, num
);
1224 else if (c
== 'p') {
1225 /* num is string buffer size,
1227 int n
= *(unsigned char*)str
;
1228 /* first byte (unsigned) is string size */
1231 v
= PyString_FromStringAndSize(str
+1, n
);
1238 v
= e
->unpack(str
, e
);
1243 if (v
== NULL
|| PyList_Append(res
, v
) < 0)
1246 } while (--num
> 0);
1249 v
= PyList_AsTuple(res
);
1259 /* List of functions */
1261 static PyMethodDef struct_methods
[] = {
1262 {"calcsize", struct_calcsize
, METH_VARARGS
, calcsize__doc__
},
1263 {"pack", struct_pack
, METH_VARARGS
, pack__doc__
},
1264 {"unpack", struct_unpack
, METH_VARARGS
, unpack__doc__
},
1265 {NULL
, NULL
} /* sentinel */
1269 /* Module initialization */
1276 /* Create the module and add the functions */
1277 m
= Py_InitModule4("struct", struct_methods
, struct__doc__
,
1278 (PyObject
*)NULL
, PYTHON_API_VERSION
);
1280 /* Add some symbolic constants to the module */
1281 if (StructError
== NULL
) {
1282 StructError
= PyErr_NewException("struct.error", NULL
, NULL
);
1283 if (StructError
== NULL
)
1286 Py_INCREF(StructError
);
1287 PyModule_AddObject(m
, "error", StructError
);