blob: 627ac5052bad0ca9c3626c255da42dabb738d933 [file] [log] [blame]
Bob Ippolito232f3c92006-05-23 19:12:41 +00001/* struct module -- pack values into and (out of) strings */
2
3/* New version supporting byte order, alignment and size options,
4 character strings, and unsigned numbers */
5
6#include "Python.h"
7#include "structseq.h"
8#include "structmember.h"
9#include <ctype.h>
10
Bob Ippolitod3611eb2006-05-23 19:31:23 +000011static PyTypeObject PyStructType;
Bob Ippolito232f3c92006-05-23 19:12:41 +000012
13/* compatibility macros */
14#if (PY_VERSION_HEX < 0x02050000)
15typedef int Py_ssize_t;
16#endif
17
18
19
20/* The translation function for each format character is table driven */
21
22typedef struct _formatdef {
23 char format;
24 int size;
25 int alignment;
26 PyObject* (*unpack)(const char *,
27 const struct _formatdef *);
28 int (*pack)(char *, PyObject *,
29 const struct _formatdef *);
30} formatdef;
31
32typedef struct _formatcode {
33 const struct _formatdef *fmtdef;
34 int offset;
Bob Ippolitoeb621272006-05-24 15:32:06 +000035 int size;
Bob Ippolito232f3c92006-05-23 19:12:41 +000036} formatcode;
37
38/* Struct object interface */
39
40typedef struct {
41 PyObject_HEAD
42 int s_size;
43 int s_len;
44 formatcode *s_codes;
45 PyObject *s_format;
46 PyObject *weakreflist; /* List of weak references */
47} PyStructObject;
48
Bob Ippolitoeb621272006-05-24 15:32:06 +000049
Bob Ippolito07c023b2006-05-23 19:32:25 +000050#define PyStruct_Check(op) PyObject_TypeCheck(op, &PyStructType)
51#define PyStruct_CheckExact(op) ((op)->ob_type == &PyStructType)
Bob Ippolito232f3c92006-05-23 19:12:41 +000052
53
54/* Exception */
55
56static PyObject *StructError;
57
58
59/* Define various structs to figure out the alignments of types */
60
61
62typedef struct { char c; short x; } st_short;
63typedef struct { char c; int x; } st_int;
64typedef struct { char c; long x; } st_long;
65typedef struct { char c; float x; } st_float;
66typedef struct { char c; double x; } st_double;
67typedef struct { char c; void *x; } st_void_p;
68
69#define SHORT_ALIGN (sizeof(st_short) - sizeof(short))
70#define INT_ALIGN (sizeof(st_int) - sizeof(int))
71#define LONG_ALIGN (sizeof(st_long) - sizeof(long))
72#define FLOAT_ALIGN (sizeof(st_float) - sizeof(float))
73#define DOUBLE_ALIGN (sizeof(st_double) - sizeof(double))
74#define VOID_P_ALIGN (sizeof(st_void_p) - sizeof(void *))
75
76/* We can't support q and Q in native mode unless the compiler does;
77 in std mode, they're 8 bytes on all platforms. */
78#ifdef HAVE_LONG_LONG
79typedef struct { char c; PY_LONG_LONG x; } s_long_long;
80#define LONG_LONG_ALIGN (sizeof(s_long_long) - sizeof(PY_LONG_LONG))
81#endif
82
83#define STRINGIFY(x) #x
84
85#ifdef __powerc
86#pragma options align=reset
87#endif
88
89/* Helper to get a PyLongObject by hook or by crook. Caller should decref. */
90
91static PyObject *
92get_pylong(PyObject *v)
93{
94 PyNumberMethods *m;
95
96 assert(v != NULL);
97 if (PyInt_Check(v))
98 return PyLong_FromLong(PyInt_AS_LONG(v));
99 if (PyLong_Check(v)) {
100 Py_INCREF(v);
101 return v;
102 }
103 m = v->ob_type->tp_as_number;
104 if (m != NULL && m->nb_long != NULL) {
105 v = m->nb_long(v);
106 if (v == NULL)
107 return NULL;
108 if (PyLong_Check(v))
109 return v;
110 Py_DECREF(v);
111 }
112 PyErr_SetString(StructError,
113 "cannot convert argument to long");
114 return NULL;
115}
116
117/* Helper routine to get a Python integer and raise the appropriate error
118 if it isn't one */
119
120static int
121get_long(PyObject *v, long *p)
122{
123 long x = PyInt_AsLong(v);
124 if (x == -1 && PyErr_Occurred()) {
125 if (PyErr_ExceptionMatches(PyExc_TypeError))
126 PyErr_SetString(StructError,
127 "required argument is not an integer");
128 return -1;
129 }
130 *p = x;
131 return 0;
132}
133
134
135/* Same, but handling unsigned long */
136
137static int
138get_ulong(PyObject *v, unsigned long *p)
139{
140 if (PyLong_Check(v)) {
141 unsigned long x = PyLong_AsUnsignedLong(v);
142 if (x == (unsigned long)(-1) && PyErr_Occurred())
143 return -1;
144 *p = x;
145 return 0;
146 }
147 else {
148 return get_long(v, (long *)p);
149 }
150}
151
152#ifdef HAVE_LONG_LONG
153
154/* Same, but handling native long long. */
155
156static int
157get_longlong(PyObject *v, PY_LONG_LONG *p)
158{
159 PY_LONG_LONG x;
160
161 v = get_pylong(v);
162 if (v == NULL)
163 return -1;
164 assert(PyLong_Check(v));
165 x = PyLong_AsLongLong(v);
166 Py_DECREF(v);
167 if (x == (PY_LONG_LONG)-1 && PyErr_Occurred())
168 return -1;
169 *p = x;
170 return 0;
171}
172
173/* Same, but handling native unsigned long long. */
174
175static int
176get_ulonglong(PyObject *v, unsigned PY_LONG_LONG *p)
177{
178 unsigned PY_LONG_LONG x;
179
180 v = get_pylong(v);
181 if (v == NULL)
182 return -1;
183 assert(PyLong_Check(v));
184 x = PyLong_AsUnsignedLongLong(v);
185 Py_DECREF(v);
186 if (x == (unsigned PY_LONG_LONG)-1 && PyErr_Occurred())
187 return -1;
188 *p = x;
189 return 0;
190}
191
192#endif
193
194/* Floating point helpers */
195
196static PyObject *
197unpack_float(const char *p, /* start of 4-byte string */
198 int le) /* true for little-endian, false for big-endian */
199{
200 double x;
201
202 x = _PyFloat_Unpack4((unsigned char *)p, le);
203 if (x == -1.0 && PyErr_Occurred())
204 return NULL;
205 return PyFloat_FromDouble(x);
206}
207
208static PyObject *
209unpack_double(const char *p, /* start of 8-byte string */
210 int le) /* true for little-endian, false for big-endian */
211{
212 double x;
213
214 x = _PyFloat_Unpack8((unsigned char *)p, le);
215 if (x == -1.0 && PyErr_Occurred())
216 return NULL;
217 return PyFloat_FromDouble(x);
218}
219
220
221/* A large number of small routines follow, with names of the form
222
223 [bln][up]_TYPE
224
225 [bln] distiguishes among big-endian, little-endian and native.
226 [pu] distiguishes between pack (to struct) and unpack (from struct).
227 TYPE is one of char, byte, ubyte, etc.
228*/
229
230/* Native mode routines. ****************************************************/
231/* NOTE:
232 In all n[up]_<type> routines handling types larger than 1 byte, there is
233 *no* guarantee that the p pointer is properly aligned for each type,
234 therefore memcpy is called. An intermediate variable is used to
235 compensate for big-endian architectures.
236 Normally both the intermediate variable and the memcpy call will be
237 skipped by C optimisation in little-endian architectures (gcc >= 2.91
238 does this). */
239
240static PyObject *
241nu_char(const char *p, const formatdef *f)
242{
243 return PyString_FromStringAndSize(p, 1);
244}
245
246static PyObject *
247nu_byte(const char *p, const formatdef *f)
248{
249 return PyInt_FromLong((long) *(signed char *)p);
250}
251
252static PyObject *
253nu_ubyte(const char *p, const formatdef *f)
254{
255 return PyInt_FromLong((long) *(unsigned char *)p);
256}
257
258static PyObject *
259nu_short(const char *p, const formatdef *f)
260{
261 short x;
262 memcpy((char *)&x, p, sizeof x);
263 return PyInt_FromLong((long)x);
264}
265
266static PyObject *
267nu_ushort(const char *p, const formatdef *f)
268{
269 unsigned short x;
270 memcpy((char *)&x, p, sizeof x);
271 return PyInt_FromLong((long)x);
272}
273
274static PyObject *
275nu_int(const char *p, const formatdef *f)
276{
277 int x;
278 memcpy((char *)&x, p, sizeof x);
279 return PyInt_FromLong((long)x);
280}
281
282static PyObject *
283nu_uint(const char *p, const formatdef *f)
284{
285 unsigned int x;
286 memcpy((char *)&x, p, sizeof x);
287 return PyLong_FromUnsignedLong((unsigned long)x);
288}
289
290static PyObject *
291nu_long(const char *p, const formatdef *f)
292{
293 long x;
294 memcpy((char *)&x, p, sizeof x);
295 return PyInt_FromLong(x);
296}
297
298static PyObject *
299nu_ulong(const char *p, const formatdef *f)
300{
301 unsigned long x;
302 memcpy((char *)&x, p, sizeof x);
303 return PyLong_FromUnsignedLong(x);
304}
305
306/* Native mode doesn't support q or Q unless the platform C supports
307 long long (or, on Windows, __int64). */
308
309#ifdef HAVE_LONG_LONG
310
311static PyObject *
312nu_longlong(const char *p, const formatdef *f)
313{
314 PY_LONG_LONG x;
315 memcpy((char *)&x, p, sizeof x);
316 return PyLong_FromLongLong(x);
317}
318
319static PyObject *
320nu_ulonglong(const char *p, const formatdef *f)
321{
322 unsigned PY_LONG_LONG x;
323 memcpy((char *)&x, p, sizeof x);
324 return PyLong_FromUnsignedLongLong(x);
325}
326
327#endif
328
329static PyObject *
330nu_float(const char *p, const formatdef *f)
331{
332 float x;
333 memcpy((char *)&x, p, sizeof x);
334 return PyFloat_FromDouble((double)x);
335}
336
337static PyObject *
338nu_double(const char *p, const formatdef *f)
339{
340 double x;
341 memcpy((char *)&x, p, sizeof x);
342 return PyFloat_FromDouble(x);
343}
344
345static PyObject *
346nu_void_p(const char *p, const formatdef *f)
347{
348 void *x;
349 memcpy((char *)&x, p, sizeof x);
350 return PyLong_FromVoidPtr(x);
351}
352
353static int
354np_byte(char *p, PyObject *v, const formatdef *f)
355{
356 long x;
357 if (get_long(v, &x) < 0)
358 return -1;
359 if (x < -128 || x > 127){
360 PyErr_SetString(StructError,
361 "byte format requires -128<=number<=127");
362 return -1;
363 }
364 *p = (char)x;
365 return 0;
366}
367
368static int
369np_ubyte(char *p, PyObject *v, const formatdef *f)
370{
371 long x;
372 if (get_long(v, &x) < 0)
373 return -1;
374 if (x < 0 || x > 255){
375 PyErr_SetString(StructError,
376 "ubyte format requires 0<=number<=255");
377 return -1;
378 }
379 *p = (char)x;
380 return 0;
381}
382
383static int
384np_char(char *p, PyObject *v, const formatdef *f)
385{
386 if (!PyString_Check(v) || PyString_Size(v) != 1) {
387 PyErr_SetString(StructError,
388 "char format require string of length 1");
389 return -1;
390 }
391 *p = *PyString_AsString(v);
392 return 0;
393}
394
395static int
396np_short(char *p, PyObject *v, const formatdef *f)
397{
398 long x;
399 short y;
400 if (get_long(v, &x) < 0)
401 return -1;
402 if (x < SHRT_MIN || x > SHRT_MAX){
403 PyErr_SetString(StructError,
404 "short format requires " STRINGIFY(SHRT_MIN)
405 "<=number<=" STRINGIFY(SHRT_MAX));
406 return -1;
407 }
408 y = (short)x;
409 memcpy(p, (char *)&y, sizeof y);
410 return 0;
411}
412
413static int
414np_ushort(char *p, PyObject *v, const formatdef *f)
415{
416 long x;
417 unsigned short y;
418 if (get_long(v, &x) < 0)
419 return -1;
420 if (x < 0 || x > USHRT_MAX){
421 PyErr_SetString(StructError,
422 "short format requires 0<=number<=" STRINGIFY(USHRT_MAX));
423 return -1;
424 }
425 y = (unsigned short)x;
426 memcpy(p, (char *)&y, sizeof y);
427 return 0;
428}
429
430static int
431np_int(char *p, PyObject *v, const formatdef *f)
432{
433 long x;
434 int y;
435 if (get_long(v, &x) < 0)
436 return -1;
437 y = (int)x;
438 memcpy(p, (char *)&y, sizeof y);
439 return 0;
440}
441
442static int
443np_uint(char *p, PyObject *v, const formatdef *f)
444{
445 unsigned long x;
446 unsigned int y;
447 if (get_ulong(v, &x) < 0)
448 return -1;
449 y = (unsigned int)x;
450 memcpy(p, (char *)&y, sizeof y);
451 return 0;
452}
453
454static int
455np_long(char *p, PyObject *v, const formatdef *f)
456{
457 long x;
458 if (get_long(v, &x) < 0)
459 return -1;
460 memcpy(p, (char *)&x, sizeof x);
461 return 0;
462}
463
464static int
465np_ulong(char *p, PyObject *v, const formatdef *f)
466{
467 unsigned long x;
468 if (get_ulong(v, &x) < 0)
469 return -1;
470 memcpy(p, (char *)&x, sizeof x);
471 return 0;
472}
473
474#ifdef HAVE_LONG_LONG
475
476static int
477np_longlong(char *p, PyObject *v, const formatdef *f)
478{
479 PY_LONG_LONG x;
480 if (get_longlong(v, &x) < 0)
481 return -1;
482 memcpy(p, (char *)&x, sizeof x);
483 return 0;
484}
485
486static int
487np_ulonglong(char *p, PyObject *v, const formatdef *f)
488{
489 unsigned PY_LONG_LONG x;
490 if (get_ulonglong(v, &x) < 0)
491 return -1;
492 memcpy(p, (char *)&x, sizeof x);
493 return 0;
494}
495#endif
496
497static int
498np_float(char *p, PyObject *v, const formatdef *f)
499{
500 float x = (float)PyFloat_AsDouble(v);
501 if (x == -1 && PyErr_Occurred()) {
502 PyErr_SetString(StructError,
503 "required argument is not a float");
504 return -1;
505 }
506 memcpy(p, (char *)&x, sizeof x);
507 return 0;
508}
509
510static int
511np_double(char *p, PyObject *v, const formatdef *f)
512{
513 double x = PyFloat_AsDouble(v);
514 if (x == -1 && PyErr_Occurred()) {
515 PyErr_SetString(StructError,
516 "required argument is not a float");
517 return -1;
518 }
519 memcpy(p, (char *)&x, sizeof(double));
520 return 0;
521}
522
523static int
524np_void_p(char *p, PyObject *v, const formatdef *f)
525{
526 void *x;
527
528 v = get_pylong(v);
529 if (v == NULL)
530 return -1;
531 assert(PyLong_Check(v));
532 x = PyLong_AsVoidPtr(v);
533 Py_DECREF(v);
534 if (x == NULL && PyErr_Occurred())
535 return -1;
536 memcpy(p, (char *)&x, sizeof x);
537 return 0;
538}
539
540static formatdef native_table[] = {
541 {'x', sizeof(char), 0, NULL},
542 {'b', sizeof(char), 0, nu_byte, np_byte},
543 {'B', sizeof(char), 0, nu_ubyte, np_ubyte},
544 {'c', sizeof(char), 0, nu_char, np_char},
545 {'s', sizeof(char), 0, NULL},
546 {'p', sizeof(char), 0, NULL},
547 {'h', sizeof(short), SHORT_ALIGN, nu_short, np_short},
548 {'H', sizeof(short), SHORT_ALIGN, nu_ushort, np_ushort},
549 {'i', sizeof(int), INT_ALIGN, nu_int, np_int},
550 {'I', sizeof(int), INT_ALIGN, nu_uint, np_uint},
551 {'l', sizeof(long), LONG_ALIGN, nu_long, np_long},
552 {'L', sizeof(long), LONG_ALIGN, nu_ulong, np_ulong},
553 {'f', sizeof(float), FLOAT_ALIGN, nu_float, np_float},
554 {'d', sizeof(double), DOUBLE_ALIGN, nu_double, np_double},
555 {'P', sizeof(void *), VOID_P_ALIGN, nu_void_p, np_void_p},
556#ifdef HAVE_LONG_LONG
557 {'q', sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_longlong, np_longlong},
558 {'Q', sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_ulonglong,np_ulonglong},
559#endif
560 {0}
561};
562
563/* Big-endian routines. *****************************************************/
564
565static PyObject *
566bu_int(const char *p, const formatdef *f)
567{
568 long x = 0;
569 int i = f->size;
570 do {
571 x = (x<<8) | (*p++ & 0xFF);
572 } while (--i > 0);
573 /* Extend the sign bit. */
574 if (SIZEOF_LONG > f->size)
575 x |= -(x & (1L << (8*f->size - 1)));
576 return PyInt_FromLong(x);
577}
578
579static PyObject *
580bu_uint(const char *p, const formatdef *f)
581{
582 unsigned long x = 0;
583 int i = f->size;
584 do {
585 x = (x<<8) | (*p++ & 0xFF);
586 } while (--i > 0);
587 if (f->size >= 4)
588 return PyLong_FromUnsignedLong(x);
589 else
590 return PyInt_FromLong((long)x);
591}
592
593static PyObject *
594bu_longlong(const char *p, const formatdef *f)
595{
596 return _PyLong_FromByteArray((const unsigned char *)p,
597 8,
598 0, /* little-endian */
599 1 /* signed */);
600}
601
602static PyObject *
603bu_ulonglong(const char *p, const formatdef *f)
604{
605 return _PyLong_FromByteArray((const unsigned char *)p,
606 8,
607 0, /* little-endian */
608 0 /* signed */);
609}
610
611static PyObject *
612bu_float(const char *p, const formatdef *f)
613{
614 return unpack_float(p, 0);
615}
616
617static PyObject *
618bu_double(const char *p, const formatdef *f)
619{
620 return unpack_double(p, 0);
621}
622
623static int
624bp_int(char *p, PyObject *v, const formatdef *f)
625{
626 long x;
627 int i;
628 if (get_long(v, &x) < 0)
629 return -1;
630 i = f->size;
631 do {
632 p[--i] = (char)x;
633 x >>= 8;
634 } while (i > 0);
635 return 0;
636}
637
638static int
639bp_uint(char *p, PyObject *v, const formatdef *f)
640{
641 unsigned long x;
642 int i;
643 if (get_ulong(v, &x) < 0)
644 return -1;
645 i = f->size;
646 do {
647 p[--i] = (char)x;
648 x >>= 8;
649 } while (i > 0);
650 return 0;
651}
652
653static int
654bp_longlong(char *p, PyObject *v, const formatdef *f)
655{
656 int res;
657 v = get_pylong(v);
658 if (v == NULL)
659 return -1;
660 res = _PyLong_AsByteArray((PyLongObject *)v,
661 (unsigned char *)p,
662 8,
663 0, /* little_endian */
664 1 /* signed */);
665 Py_DECREF(v);
666 return res;
667}
668
669static int
670bp_ulonglong(char *p, PyObject *v, const formatdef *f)
671{
672 int res;
673 v = get_pylong(v);
674 if (v == NULL)
675 return -1;
676 res = _PyLong_AsByteArray((PyLongObject *)v,
677 (unsigned char *)p,
678 8,
679 0, /* little_endian */
680 0 /* signed */);
681 Py_DECREF(v);
682 return res;
683}
684
685static int
686bp_float(char *p, PyObject *v, const formatdef *f)
687{
688 double x = PyFloat_AsDouble(v);
689 if (x == -1 && PyErr_Occurred()) {
690 PyErr_SetString(StructError,
691 "required argument is not a float");
692 return -1;
693 }
694 return _PyFloat_Pack4(x, (unsigned char *)p, 0);
695}
696
697static int
698bp_double(char *p, PyObject *v, const formatdef *f)
699{
700 double x = PyFloat_AsDouble(v);
701 if (x == -1 && PyErr_Occurred()) {
702 PyErr_SetString(StructError,
703 "required argument is not a float");
704 return -1;
705 }
706 return _PyFloat_Pack8(x, (unsigned char *)p, 0);
707}
708
709static formatdef bigendian_table[] = {
710 {'x', 1, 0, NULL},
711 {'b', 1, 0, bu_int, bp_int},
712 {'B', 1, 0, bu_uint, bp_int},
713 {'c', 1, 0, nu_char, np_char},
714 {'s', 1, 0, NULL},
715 {'p', 1, 0, NULL},
716 {'h', 2, 0, bu_int, bp_int},
717 {'H', 2, 0, bu_uint, bp_uint},
718 {'i', 4, 0, bu_int, bp_int},
719 {'I', 4, 0, bu_uint, bp_uint},
720 {'l', 4, 0, bu_int, bp_int},
721 {'L', 4, 0, bu_uint, bp_uint},
722 {'q', 8, 0, bu_longlong, bp_longlong},
723 {'Q', 8, 0, bu_ulonglong, bp_ulonglong},
724 {'f', 4, 0, bu_float, bp_float},
725 {'d', 8, 0, bu_double, bp_double},
726 {0}
727};
728
729/* Little-endian routines. *****************************************************/
730
731static PyObject *
732lu_int(const char *p, const formatdef *f)
733{
734 long x = 0;
735 int i = f->size;
736 do {
737 x = (x<<8) | (p[--i] & 0xFF);
738 } while (i > 0);
739 /* Extend the sign bit. */
740 if (SIZEOF_LONG > f->size)
741 x |= -(x & (1L << (8*f->size - 1)));
742 return PyInt_FromLong(x);
743}
744
745static PyObject *
746lu_uint(const char *p, const formatdef *f)
747{
748 unsigned long x = 0;
749 int i = f->size;
750 do {
751 x = (x<<8) | (p[--i] & 0xFF);
752 } while (i > 0);
753 if (f->size >= 4)
754 return PyLong_FromUnsignedLong(x);
755 else
756 return PyInt_FromLong((long)x);
757}
758
759static PyObject *
760lu_longlong(const char *p, const formatdef *f)
761{
762 return _PyLong_FromByteArray((const unsigned char *)p,
763 8,
764 1, /* little-endian */
765 1 /* signed */);
766}
767
768static PyObject *
769lu_ulonglong(const char *p, const formatdef *f)
770{
771 return _PyLong_FromByteArray((const unsigned char *)p,
772 8,
773 1, /* little-endian */
774 0 /* signed */);
775}
776
777static PyObject *
778lu_float(const char *p, const formatdef *f)
779{
780 return unpack_float(p, 1);
781}
782
783static PyObject *
784lu_double(const char *p, const formatdef *f)
785{
786 return unpack_double(p, 1);
787}
788
789static int
790lp_int(char *p, PyObject *v, const formatdef *f)
791{
792 long x;
793 int i;
794 if (get_long(v, &x) < 0)
795 return -1;
796 i = f->size;
797 do {
798 *p++ = (char)x;
799 x >>= 8;
800 } while (--i > 0);
801 return 0;
802}
803
804static int
805lp_uint(char *p, PyObject *v, const formatdef *f)
806{
807 unsigned long x;
808 int i;
809 if (get_ulong(v, &x) < 0)
810 return -1;
811 i = f->size;
812 do {
813 *p++ = (char)x;
814 x >>= 8;
815 } while (--i > 0);
816 return 0;
817}
818
819static int
820lp_longlong(char *p, PyObject *v, const formatdef *f)
821{
822 int res;
823 v = get_pylong(v);
824 if (v == NULL)
825 return -1;
826 res = _PyLong_AsByteArray((PyLongObject*)v,
827 (unsigned char *)p,
828 8,
829 1, /* little_endian */
830 1 /* signed */);
831 Py_DECREF(v);
832 return res;
833}
834
835static int
836lp_ulonglong(char *p, PyObject *v, const formatdef *f)
837{
838 int res;
839 v = get_pylong(v);
840 if (v == NULL)
841 return -1;
842 res = _PyLong_AsByteArray((PyLongObject*)v,
843 (unsigned char *)p,
844 8,
845 1, /* little_endian */
846 0 /* signed */);
847 Py_DECREF(v);
848 return res;
849}
850
851static int
852lp_float(char *p, PyObject *v, const formatdef *f)
853{
854 double x = PyFloat_AsDouble(v);
855 if (x == -1 && PyErr_Occurred()) {
856 PyErr_SetString(StructError,
857 "required argument is not a float");
858 return -1;
859 }
860 return _PyFloat_Pack4(x, (unsigned char *)p, 1);
861}
862
863static int
864lp_double(char *p, PyObject *v, const formatdef *f)
865{
866 double x = PyFloat_AsDouble(v);
867 if (x == -1 && PyErr_Occurred()) {
868 PyErr_SetString(StructError,
869 "required argument is not a float");
870 return -1;
871 }
872 return _PyFloat_Pack8(x, (unsigned char *)p, 1);
873}
874
875static formatdef lilendian_table[] = {
876 {'x', 1, 0, NULL},
877 {'b', 1, 0, lu_int, lp_int},
878 {'B', 1, 0, lu_uint, lp_int},
879 {'c', 1, 0, nu_char, np_char},
880 {'s', 1, 0, NULL},
881 {'p', 1, 0, NULL},
882 {'h', 2, 0, lu_int, lp_int},
883 {'H', 2, 0, lu_uint, lp_uint},
884 {'i', 4, 0, lu_int, lp_int},
885 {'I', 4, 0, lu_uint, lp_uint},
886 {'l', 4, 0, lu_int, lp_int},
887 {'L', 4, 0, lu_uint, lp_uint},
888 {'q', 8, 0, lu_longlong, lp_longlong},
889 {'Q', 8, 0, lu_ulonglong, lp_ulonglong},
890 {'f', 4, 0, lu_float, lp_float},
891 {'d', 8, 0, lu_double, lp_double},
892 {0}
893};
894
895
896static const formatdef *
897whichtable(char **pfmt)
898{
899 const char *fmt = (*pfmt)++; /* May be backed out of later */
900 switch (*fmt) {
901 case '<':
902 return lilendian_table;
903 case '>':
904 case '!': /* Network byte order is big-endian */
905 return bigendian_table;
906 case '=': { /* Host byte order -- different from native in aligment! */
907 int n = 1;
908 char *p = (char *) &n;
909 if (*p == 1)
910 return lilendian_table;
911 else
912 return bigendian_table;
913 }
914 default:
915 --*pfmt; /* Back out of pointer increment */
916 /* Fall through */
917 case '@':
918 return native_table;
919 }
920}
921
922
923/* Get the table entry for a format code */
924
925static const formatdef *
926getentry(int c, const formatdef *f)
927{
928 for (; f->format != '\0'; f++) {
929 if (f->format == c) {
930 return f;
931 }
932 }
933 PyErr_SetString(StructError, "bad char in struct format");
934 return NULL;
935}
936
937
938/* Align a size according to a format code */
939
940static int
941align(int size, int c, const formatdef *e)
942{
943 if (e->format == c) {
944 if (e->alignment) {
945 size = ((size + e->alignment - 1)
946 / e->alignment)
947 * e->alignment;
948 }
949 }
950 return size;
951}
952
953
954/* calculate the size of a format string */
955
956static int
957prepare_s(PyStructObject *self)
958{
959 const formatdef *f;
960 const formatdef *e;
961 formatcode *codes;
962
963 const char *s;
964 const char *fmt;
965 char c;
Bob Ippolitoeb621272006-05-24 15:32:06 +0000966 int size, len, num, itemsize, x;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000967
968 fmt = PyString_AS_STRING(self->s_format);
969
970 f = whichtable((char **)&fmt);
971
972 s = fmt;
973 size = 0;
974 len = 0;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000975 while ((c = *s++) != '\0') {
976 if (isspace(Py_CHARMASK(c)))
977 continue;
978 if ('0' <= c && c <= '9') {
979 num = c - '0';
980 while ('0' <= (c = *s++) && c <= '9') {
981 x = num*10 + (c - '0');
982 if (x/10 != num) {
983 PyErr_SetString(
984 StructError,
985 "overflow in item count");
986 return -1;
987 }
988 num = x;
989 }
990 if (c == '\0')
991 break;
992 }
993 else
994 num = 1;
995
996 e = getentry(c, f);
997 if (e == NULL)
998 return -1;
999
1000 switch (c) {
1001 case 's': /* fall through */
1002 case 'p': len++; break;
1003 case 'x': break;
1004 default: len += num; break;
1005 }
Bob Ippolito232f3c92006-05-23 19:12:41 +00001006
1007 itemsize = e->size;
1008 size = align(size, c, e);
1009 x = num * itemsize;
1010 size += x;
1011 if (x/itemsize != num || size < 0) {
1012 PyErr_SetString(StructError,
1013 "total struct size too long");
1014 return -1;
1015 }
1016 }
1017
1018 self->s_size = size;
1019 self->s_len = len;
Bob Ippolitoeb621272006-05-24 15:32:06 +00001020 codes = PyMem_MALLOC((len + 1) * sizeof(formatcode));
Bob Ippolito232f3c92006-05-23 19:12:41 +00001021 if (codes == NULL) {
1022 PyErr_NoMemory();
1023 return -1;
1024 }
1025 self->s_codes = codes;
1026
1027 s = fmt;
1028 size = 0;
1029 while ((c = *s++) != '\0') {
1030 if (isspace(Py_CHARMASK(c)))
1031 continue;
1032 if ('0' <= c && c <= '9') {
1033 num = c - '0';
1034 while ('0' <= (c = *s++) && c <= '9')
1035 num = num*10 + (c - '0');
1036 if (c == '\0')
1037 break;
1038 }
1039 else
1040 num = 1;
1041
1042 e = getentry(c, f);
1043
1044 size = align(size, c, e);
Bob Ippolitoeb621272006-05-24 15:32:06 +00001045 if (c == 's' || c == 'p') {
Bob Ippolito232f3c92006-05-23 19:12:41 +00001046 codes->offset = size;
Bob Ippolitoeb621272006-05-24 15:32:06 +00001047 codes->size = num;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001048 codes->fmtdef = e;
1049 codes++;
Bob Ippolitoeb621272006-05-24 15:32:06 +00001050 size += num;
1051 } else if (c == 'x') {
1052 size += num;
1053 } else {
1054 while (--num >= 0) {
1055 codes->offset = size;
1056 codes->size = e->size;
1057 codes->fmtdef = e;
1058 codes++;
1059 size += e->size;
1060 }
Bob Ippolito232f3c92006-05-23 19:12:41 +00001061 }
Bob Ippolito232f3c92006-05-23 19:12:41 +00001062 }
1063 codes->fmtdef = NULL;
Bob Ippolitoeb621272006-05-24 15:32:06 +00001064 codes->offset = size;
1065 codes->size = 0;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001066
1067 return 0;
1068}
1069
1070static PyObject *
1071s_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1072{
1073 PyObject *self;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001074
1075 assert(type != NULL && type->tp_alloc != NULL);
1076
1077 self = type->tp_alloc(type, 0);
1078 if (self != NULL) {
1079 PyStructObject *s = (PyStructObject*)self;
1080 Py_INCREF(Py_None);
1081 s->s_format = Py_None;
1082 s->s_codes = NULL;
1083 s->s_size = -1;
1084 s->s_len = -1;
1085 }
1086 return self;
1087}
1088
1089static int
1090s_init(PyObject *self, PyObject *args, PyObject *kwds)
1091{
1092 PyStructObject *soself = (PyStructObject *)self;
1093 PyObject *o_format = NULL;
1094 int ret = 0;
1095 static char *kwlist[] = {"format", 0};
1096
1097 assert(PyStruct_Check(self));
1098
1099 if (!PyArg_ParseTupleAndKeywords(args, kwds, "S:Struct", kwlist,
1100 &o_format))
1101 return -1;
1102
1103 Py_INCREF(o_format);
1104 Py_XDECREF(soself->s_format);
1105 soself->s_format = o_format;
1106
1107 ret = prepare_s(soself);
1108 return ret;
1109}
1110
1111static void
1112s_dealloc(PyStructObject *s)
1113{
Bob Ippolito232f3c92006-05-23 19:12:41 +00001114 if (s->weakreflist != NULL)
1115 PyObject_ClearWeakRefs((PyObject *)s);
1116 if (s->s_codes != NULL) {
1117 PyMem_FREE(s->s_codes);
1118 }
1119 Py_XDECREF(s->s_format);
1120 s->ob_type->tp_free((PyObject *)s);
1121}
1122
Bob Ippolitoeb621272006-05-24 15:32:06 +00001123static PyObject *
1124s_unpack_internal(PyStructObject *soself, char *startfrom) {
1125 formatcode *code;
1126 Py_ssize_t i = 0;
1127 PyObject *result = PyTuple_New(soself->s_len);
1128 if (result == NULL)
1129 return NULL;
1130
1131 for (code = soself->s_codes; code->fmtdef != NULL; code++) {
1132 PyObject *v;
1133 const formatdef *e = code->fmtdef;
1134 const char *res = startfrom + code->offset;
1135 if (e->format == 's') {
1136 v = PyString_FromStringAndSize(res, code->size);
1137 if (v == NULL)
1138 goto fail;
1139 PyTuple_SET_ITEM(result, i++, v);
1140 } else if (e->format == 'p') {
1141 Py_ssize_t n = *(unsigned char*)res;
1142 if (n >= code->size)
1143 n = code->size - 1;
1144 v = PyString_FromStringAndSize(res + 1, n);
1145 if (v == NULL)
1146 goto fail;
1147 PyTuple_SET_ITEM(result, i++, v);
1148 } else {
1149 v = e->unpack(res, e);
1150 if (v == NULL)
1151 goto fail;
1152 PyTuple_SET_ITEM(result, i++, v);
1153 }
1154 }
1155
1156 return result;
1157fail:
1158 Py_DECREF(result);
1159 return NULL;
1160};
1161
1162
Bob Ippolito232f3c92006-05-23 19:12:41 +00001163PyDoc_STRVAR(s_unpack__doc__,
1164"unpack(str) -> (v1, v2, ...)\n\
1165\n\
1166Return tuple containing values unpacked according to this Struct's format.\n\
1167Requires len(str) == self.size. See struct.__doc__ for more on format\n\
1168strings.");
1169
1170static PyObject *
1171s_unpack(PyObject *self, PyObject *inputstr)
1172{
Bob Ippolitoeb621272006-05-24 15:32:06 +00001173 PyStructObject *soself = (PyStructObject *)self;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001174 assert(PyStruct_Check(self));
1175 assert(soself->s_codes != NULL);
1176 if (inputstr == NULL || !PyString_Check(inputstr) ||
Bob Ippolitoeb621272006-05-24 15:32:06 +00001177 PyString_GET_SIZE(inputstr) != soself->s_size) {
Bob Ippolito232f3c92006-05-23 19:12:41 +00001178 PyErr_Format(StructError,
1179 "unpack requires a string argument of length %d", soself->s_size);
1180 return NULL;
1181 }
Bob Ippolitoeb621272006-05-24 15:32:06 +00001182 return s_unpack_internal(soself, PyString_AS_STRING(inputstr));
1183}
1184
1185PyDoc_STRVAR(s_unpack_from__doc__,
1186"unpack_from(buffer[, offset]) -> (v1, v2, ...)\n\
1187\n\
1188Return tuple containing values unpacked according to this Struct's format.\n\
1189Unlike unpack, unpack_from can unpack values from any object supporting\n\
1190the buffer API, not just str. Requires len(buffer[offset:]) >= self.size.\n\
1191See struct.__doc__ for more on format strings.");
1192
1193static PyObject *
1194s_unpack_from(PyObject *self, PyObject *args, PyObject *kwds)
1195{
1196 static char *kwlist[] = {"buffer", "offset", 0};
1197#if (PY_VERSION_HEX < 0x02050000)
1198 static char *fmt = "z#|i:unpack_from";
1199#else
1200 static char *fmt = "z#|n:unpack_from";
1201#endif
1202 Py_ssize_t buffer_len = 0, offset = 0;
1203 char *buffer = NULL;
1204 PyStructObject *soself = (PyStructObject *)self;
1205 assert(PyStruct_Check(self));
1206 assert(soself->s_codes != NULL);
1207
1208 if (!PyArg_ParseTupleAndKeywords(args, kwds, fmt, kwlist,
1209 &buffer, &buffer_len, &offset))
1210 return NULL;
1211
1212 if (buffer == NULL) {
1213 PyErr_Format(StructError,
1214 "unpack_from requires a buffer argument");
Bob Ippolito232f3c92006-05-23 19:12:41 +00001215 return NULL;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001216 }
Bob Ippolitoeb621272006-05-24 15:32:06 +00001217
1218 if (offset < 0)
1219 offset += buffer_len;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001220
Bob Ippolitoeb621272006-05-24 15:32:06 +00001221 if (offset < 0 || (buffer_len - offset) < soself->s_size) {
1222 PyErr_Format(StructError,
1223 "unpack_from requires a buffer of at least %d bytes",
1224 soself->s_size);
1225 return NULL;
1226 }
1227 return s_unpack_internal(soself, buffer + offset);
1228}
Bob Ippolito232f3c92006-05-23 19:12:41 +00001229
1230PyDoc_STRVAR(s_pack__doc__,
1231"pack(v1, v2, ...) -> string\n\
1232\n\
1233Return a string containing values v1, v2, ... packed according to this\n\
1234Struct's format. See struct.__doc__ for more on format strings.");
1235
1236static PyObject *
1237s_pack(PyObject *self, PyObject *args)
1238{
1239 PyStructObject *soself;
1240 PyObject *result;
1241 char *restart;
1242 formatcode *code;
1243 Py_ssize_t i;
1244
1245 soself = (PyStructObject *)self;
1246 assert(PyStruct_Check(self));
1247 assert(soself->s_codes != NULL);
1248 if (args == NULL || !PyTuple_Check(args) ||
1249 PyTuple_GET_SIZE(args) != soself->s_len)
1250 {
1251 PyErr_Format(StructError,
1252 "pack requires exactly %d arguments", soself->s_len);
1253 return NULL;
1254 }
1255
1256 result = PyString_FromStringAndSize((char *)NULL, soself->s_size);
1257 if (result == NULL)
1258 return NULL;
1259
1260 restart = PyString_AS_STRING(result);
1261 memset(restart, '\0', soself->s_size);
1262 i = 0;
1263 for (code = soself->s_codes; code->fmtdef != NULL; code++) {
1264 Py_ssize_t n;
1265 PyObject *v;
1266 const formatdef *e = code->fmtdef;
1267 char *res = restart + code->offset;
1268 if (e->format == 's') {
1269 v = PyTuple_GET_ITEM(args, i++);
1270 if (!PyString_Check(v)) {
1271 PyErr_SetString(StructError,
1272 "argument for 's' must be a string");
1273 goto fail;
1274 }
1275 n = PyString_GET_SIZE(v);
Bob Ippolitoeb621272006-05-24 15:32:06 +00001276 if (n > code->size)
1277 n = code->size;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001278 if (n > 0)
1279 memcpy(res, PyString_AS_STRING(v), n);
1280 } else if (e->format == 'p') {
1281 v = PyTuple_GET_ITEM(args, i++);
1282 if (!PyString_Check(v)) {
1283 PyErr_SetString(StructError,
1284 "argument for 'p' must be a string");
1285 goto fail;
1286 }
1287 n = PyString_GET_SIZE(v);
Bob Ippolitoeb621272006-05-24 15:32:06 +00001288 if (n > (code->size - 1))
1289 n = code->size - 1;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001290 if (n > 0)
1291 memcpy(res + 1, PyString_AS_STRING(v), n);
1292 if (n > 255)
1293 n = 255;
1294 *res = Py_SAFE_DOWNCAST(n, Py_ssize_t, unsigned char);
1295 } else {
Bob Ippolitoeb621272006-05-24 15:32:06 +00001296 v = PyTuple_GET_ITEM(args, i++);
1297 if (e->pack(res, v, e) < 0)
1298 goto fail;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001299 }
1300 }
1301
1302 return result;
1303
1304fail:
1305 Py_DECREF(result);
1306 return NULL;
1307
1308}
1309
1310
1311/* List of functions */
1312
1313static struct PyMethodDef s_methods[] = {
Bob Ippolitoeb621272006-05-24 15:32:06 +00001314 {"pack", (PyCFunction)s_pack, METH_VARARGS, s_pack__doc__},
1315 {"unpack", (PyCFunction)s_unpack, METH_O, s_unpack__doc__},
1316 {"unpack_from", (PyCFunction)s_unpack_from, METH_KEYWORDS, s_unpack_from__doc__},
Bob Ippolito232f3c92006-05-23 19:12:41 +00001317 {NULL, NULL} /* sentinel */
1318};
1319
1320PyDoc_STRVAR(s__doc__, "Compiled struct object");
1321
1322#define OFF(x) offsetof(PyStructObject, x)
1323
1324static PyMemberDef s_memberlist[] = {
1325 {"format", T_OBJECT, OFF(s_format), RO,
1326 "struct format string"},
1327 {"size", T_INT, OFF(s_size), RO,
1328 "struct size in bytes"},
1329 {"_len", T_INT, OFF(s_len), RO,
1330 "number of items expected in tuple"},
1331 {NULL} /* Sentinel */
1332};
1333
1334
1335static
1336PyTypeObject PyStructType = {
1337 PyObject_HEAD_INIT(&PyType_Type)
1338 0,
1339 "Struct",
1340 sizeof(PyStructObject),
1341 0,
1342 (destructor)s_dealloc, /* tp_dealloc */
1343 0, /* tp_print */
1344 0, /* tp_getattr */
1345 0, /* tp_setattr */
1346 0, /* tp_compare */
1347 0, /* tp_repr */
1348 0, /* tp_as_number */
1349 0, /* tp_as_sequence */
1350 0, /* tp_as_mapping */
1351 0, /* tp_hash */
1352 0, /* tp_call */
1353 0, /* tp_str */
1354 PyObject_GenericGetAttr, /* tp_getattro */
1355 PyObject_GenericSetAttr, /* tp_setattro */
1356 0, /* tp_as_buffer */
1357 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */
1358 s__doc__, /* tp_doc */
1359 0, /* tp_traverse */
1360 0, /* tp_clear */
1361 0, /* tp_richcompare */
1362 offsetof(PyStructObject, weakreflist), /* tp_weaklistoffset */
1363 0, /* tp_iter */
1364 0, /* tp_iternext */
1365 s_methods, /* tp_methods */
1366 s_memberlist, /* tp_members */
1367 0, /* tp_getset */
1368 0, /* tp_base */
1369 0, /* tp_dict */
1370 0, /* tp_descr_get */
1371 0, /* tp_descr_set */
1372 0, /* tp_dictoffset */
1373 s_init, /* tp_init */
1374 PyType_GenericAlloc, /* tp_alloc */
1375 s_new, /* tp_new */
1376 PyObject_Del, /* tp_free */
1377};
1378
1379/* Module initialization */
1380
1381PyMODINIT_FUNC
1382init_struct(void)
1383{
1384 PyObject *m = Py_InitModule("_struct", NULL);
1385 if (m == NULL)
1386 return;
1387
1388 /* Add some symbolic constants to the module */
1389 if (StructError == NULL) {
1390 StructError = PyErr_NewException("struct.error", NULL, NULL);
1391 if (StructError == NULL)
1392 return;
1393 }
1394 Py_INCREF(StructError);
1395 PyModule_AddObject(m, "error", StructError);
1396 Py_INCREF((PyObject*)&PyStructType);
1397 PyModule_AddObject(m, "Struct", (PyObject*)&PyStructType);
1398}