blob: 066759a86cf3f7d6af74f229b741aaa5765fc68f [file] [log] [blame]
Benjamin Peterson4ae19462008-07-31 15:03:40 +00001/* struct module -- pack values into and (out of) bytes objects */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002
3/* New version supporting byte order, alignment and size options,
4 character strings, and unsigned numbers */
5
6#define PY_SSIZE_T_CLEAN
7
8#include "Python.h"
9#include "structseq.h"
10#include "structmember.h"
11#include <ctype.h>
12
13static PyTypeObject PyStructType;
14
Thomas Wouters0e3f5912006-08-11 14:57:12 +000015/* If PY_STRUCT_FLOAT_COERCE is defined, the struct module will allow float
16 arguments for integer formats with a warning for backwards
17 compatibility. */
18
19#define PY_STRUCT_FLOAT_COERCE 1
20
21#ifdef PY_STRUCT_FLOAT_COERCE
22#define FLOAT_COERCE "integer argument expected, got float"
23#endif
24
25
Thomas Wouters477c8d52006-05-27 19:21:47 +000026/* The translation function for each format character is table driven */
27typedef struct _formatdef {
28 char format;
29 Py_ssize_t size;
30 Py_ssize_t alignment;
31 PyObject* (*unpack)(const char *,
32 const struct _formatdef *);
33 int (*pack)(char *, PyObject *,
34 const struct _formatdef *);
35} formatdef;
36
37typedef struct _formatcode {
38 const struct _formatdef *fmtdef;
39 Py_ssize_t offset;
40 Py_ssize_t size;
41} formatcode;
42
43/* Struct object interface */
44
45typedef struct {
46 PyObject_HEAD
47 Py_ssize_t s_size;
48 Py_ssize_t s_len;
49 formatcode *s_codes;
50 PyObject *s_format;
51 PyObject *weakreflist; /* List of weak references */
52} PyStructObject;
53
54
55#define PyStruct_Check(op) PyObject_TypeCheck(op, &PyStructType)
Christian Heimes90aa7642007-12-19 02:45:37 +000056#define PyStruct_CheckExact(op) (Py_TYPE(op) == &PyStructType)
Thomas Wouters477c8d52006-05-27 19:21:47 +000057
58
59/* Exception */
60
61static PyObject *StructError;
62
63
64/* Define various structs to figure out the alignments of types */
65
66
67typedef struct { char c; short x; } st_short;
68typedef struct { char c; int x; } st_int;
69typedef struct { char c; long x; } st_long;
70typedef struct { char c; float x; } st_float;
71typedef struct { char c; double x; } st_double;
72typedef struct { char c; void *x; } st_void_p;
73
74#define SHORT_ALIGN (sizeof(st_short) - sizeof(short))
75#define INT_ALIGN (sizeof(st_int) - sizeof(int))
76#define LONG_ALIGN (sizeof(st_long) - sizeof(long))
77#define FLOAT_ALIGN (sizeof(st_float) - sizeof(float))
78#define DOUBLE_ALIGN (sizeof(st_double) - sizeof(double))
79#define VOID_P_ALIGN (sizeof(st_void_p) - sizeof(void *))
80
81/* We can't support q and Q in native mode unless the compiler does;
82 in std mode, they're 8 bytes on all platforms. */
83#ifdef HAVE_LONG_LONG
84typedef struct { char c; PY_LONG_LONG x; } s_long_long;
85#define LONG_LONG_ALIGN (sizeof(s_long_long) - sizeof(PY_LONG_LONG))
86#endif
87
Thomas Woutersb2137042007-02-01 18:02:27 +000088#ifdef HAVE_C99_BOOL
89#define BOOL_TYPE _Bool
90typedef struct { char c; _Bool x; } s_bool;
91#define BOOL_ALIGN (sizeof(s_bool) - sizeof(BOOL_TYPE))
92#else
93#define BOOL_TYPE char
94#define BOOL_ALIGN 0
95#endif
96
Thomas Wouters477c8d52006-05-27 19:21:47 +000097#define STRINGIFY(x) #x
98
99#ifdef __powerc
100#pragma options align=reset
101#endif
102
103/* Helper to get a PyLongObject by hook or by crook. Caller should decref. */
104
105static PyObject *
106get_pylong(PyObject *v)
107{
108 PyNumberMethods *m;
109
110 assert(v != NULL);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000111 if (PyLong_Check(v)) {
112 Py_INCREF(v);
113 return v;
114 }
Christian Heimes90aa7642007-12-19 02:45:37 +0000115 m = Py_TYPE(v)->tp_as_number;
Mark Dickinsoncce2f212009-01-15 19:32:23 +0000116 if (m != NULL && m->nb_int != NULL) {
117 v = m->nb_int(v);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000118 if (v == NULL)
119 return NULL;
120 if (PyLong_Check(v))
121 return v;
122 Py_DECREF(v);
123 }
124 PyErr_SetString(StructError,
125 "cannot convert argument to long");
126 return NULL;
127}
128
129/* Helper routine to get a Python integer and raise the appropriate error
130 if it isn't one */
131
132static int
133get_long(PyObject *v, long *p)
134{
Christian Heimes217cfd12007-12-02 14:31:20 +0000135 long x = PyLong_AsLong(v);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000136 if (x == -1 && PyErr_Occurred()) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000137#ifdef PY_STRUCT_FLOAT_COERCE
138 if (PyFloat_Check(v)) {
139 PyObject *o;
140 int res;
141 PyErr_Clear();
142 if (PyErr_WarnEx(PyExc_DeprecationWarning, FLOAT_COERCE, 2) < 0)
143 return -1;
Mark Dickinson17c7cd82009-01-17 21:57:11 +0000144 o = PyNumber_Long(v);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000145 if (o == NULL)
146 return -1;
147 res = get_long(o, p);
148 Py_DECREF(o);
149 return res;
150 }
151#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +0000152 if (PyErr_ExceptionMatches(PyExc_TypeError))
153 PyErr_SetString(StructError,
154 "required argument is not an integer");
155 return -1;
156 }
157 *p = x;
158 return 0;
159}
160
161
162/* Same, but handling unsigned long */
163
164static int
165get_ulong(PyObject *v, unsigned long *p)
166{
167 if (PyLong_Check(v)) {
168 unsigned long x = PyLong_AsUnsignedLong(v);
169 if (x == (unsigned long)(-1) && PyErr_Occurred())
170 return -1;
171 *p = x;
172 return 0;
173 }
174 if (get_long(v, (long *)p) < 0)
175 return -1;
176 if (((long)*p) < 0) {
177 PyErr_SetString(StructError,
178 "unsigned argument is < 0");
179 return -1;
180 }
181 return 0;
182}
183
184#ifdef HAVE_LONG_LONG
185
186/* Same, but handling native long long. */
187
188static int
189get_longlong(PyObject *v, PY_LONG_LONG *p)
190{
191 PY_LONG_LONG x;
192
193 v = get_pylong(v);
194 if (v == NULL)
195 return -1;
196 assert(PyLong_Check(v));
197 x = PyLong_AsLongLong(v);
198 Py_DECREF(v);
199 if (x == (PY_LONG_LONG)-1 && PyErr_Occurred())
200 return -1;
201 *p = x;
202 return 0;
203}
204
205/* Same, but handling native unsigned long long. */
206
207static int
208get_ulonglong(PyObject *v, unsigned PY_LONG_LONG *p)
209{
210 unsigned PY_LONG_LONG x;
211
212 v = get_pylong(v);
213 if (v == NULL)
214 return -1;
215 assert(PyLong_Check(v));
216 x = PyLong_AsUnsignedLongLong(v);
217 Py_DECREF(v);
218 if (x == (unsigned PY_LONG_LONG)-1 && PyErr_Occurred())
219 return -1;
220 *p = x;
221 return 0;
222}
223
224#endif
225
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000226
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000227#define RANGE_ERROR(x, f, flag, mask) return _range_error(f, flag)
228
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000229
Thomas Wouters477c8d52006-05-27 19:21:47 +0000230/* Floating point helpers */
231
232static PyObject *
233unpack_float(const char *p, /* start of 4-byte string */
234 int le) /* true for little-endian, false for big-endian */
235{
236 double x;
237
238 x = _PyFloat_Unpack4((unsigned char *)p, le);
239 if (x == -1.0 && PyErr_Occurred())
240 return NULL;
241 return PyFloat_FromDouble(x);
242}
243
244static PyObject *
245unpack_double(const char *p, /* start of 8-byte string */
246 int le) /* true for little-endian, false for big-endian */
247{
248 double x;
249
250 x = _PyFloat_Unpack8((unsigned char *)p, le);
251 if (x == -1.0 && PyErr_Occurred())
252 return NULL;
253 return PyFloat_FromDouble(x);
254}
255
256/* Helper to format the range error exceptions */
257static int
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000258_range_error(const formatdef *f, int is_unsigned)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000259{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000260 /* ulargest is the largest unsigned value with f->size bytes.
261 * Note that the simpler:
262 * ((size_t)1 << (f->size * 8)) - 1
263 * doesn't work when f->size == sizeof(size_t) because C doesn't
264 * define what happens when a left shift count is >= the number of
265 * bits in the integer being shifted; e.g., on some boxes it doesn't
266 * shift at all when they're equal.
267 */
268 const size_t ulargest = (size_t)-1 >> ((SIZEOF_SIZE_T - f->size)*8);
269 assert(f->size >= 1 && f->size <= SIZEOF_SIZE_T);
270 if (is_unsigned)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000271 PyErr_Format(StructError,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000272 "'%c' format requires 0 <= number <= %zu",
273 f->format,
274 ulargest);
275 else {
276 const Py_ssize_t largest = (Py_ssize_t)(ulargest >> 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000277 PyErr_Format(StructError,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000278 "'%c' format requires %zd <= number <= %zd",
279 f->format,
280 ~ largest,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000281 largest);
282 }
Mark Dickinsonae681df2009-03-21 10:26:31 +0000283
Thomas Wouters477c8d52006-05-27 19:21:47 +0000284 return -1;
285}
286
287
288
289/* A large number of small routines follow, with names of the form
290
291 [bln][up]_TYPE
292
293 [bln] distiguishes among big-endian, little-endian and native.
294 [pu] distiguishes between pack (to struct) and unpack (from struct).
295 TYPE is one of char, byte, ubyte, etc.
296*/
297
298/* Native mode routines. ****************************************************/
299/* NOTE:
300 In all n[up]_<type> routines handling types larger than 1 byte, there is
301 *no* guarantee that the p pointer is properly aligned for each type,
302 therefore memcpy is called. An intermediate variable is used to
303 compensate for big-endian architectures.
304 Normally both the intermediate variable and the memcpy call will be
305 skipped by C optimisation in little-endian architectures (gcc >= 2.91
306 does this). */
307
308static PyObject *
309nu_char(const char *p, const formatdef *f)
310{
Christian Heimes72b710a2008-05-26 13:28:38 +0000311 return PyBytes_FromStringAndSize(p, 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000312}
313
314static PyObject *
315nu_byte(const char *p, const formatdef *f)
316{
Christian Heimes217cfd12007-12-02 14:31:20 +0000317 return PyLong_FromLong((long) *(signed char *)p);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000318}
319
320static PyObject *
321nu_ubyte(const char *p, const formatdef *f)
322{
Christian Heimes217cfd12007-12-02 14:31:20 +0000323 return PyLong_FromLong((long) *(unsigned char *)p);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000324}
325
326static PyObject *
327nu_short(const char *p, const formatdef *f)
328{
329 short x;
330 memcpy((char *)&x, p, sizeof x);
Christian Heimes217cfd12007-12-02 14:31:20 +0000331 return PyLong_FromLong((long)x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000332}
333
334static PyObject *
335nu_ushort(const char *p, const formatdef *f)
336{
337 unsigned short x;
338 memcpy((char *)&x, p, sizeof x);
Christian Heimes217cfd12007-12-02 14:31:20 +0000339 return PyLong_FromLong((long)x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000340}
341
342static PyObject *
343nu_int(const char *p, const formatdef *f)
344{
345 int x;
346 memcpy((char *)&x, p, sizeof x);
Christian Heimes217cfd12007-12-02 14:31:20 +0000347 return PyLong_FromLong((long)x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000348}
349
350static PyObject *
351nu_uint(const char *p, const formatdef *f)
352{
353 unsigned int x;
354 memcpy((char *)&x, p, sizeof x);
355#if (SIZEOF_LONG > SIZEOF_INT)
Christian Heimes217cfd12007-12-02 14:31:20 +0000356 return PyLong_FromLong((long)x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000357#else
358 if (x <= ((unsigned int)LONG_MAX))
Christian Heimes217cfd12007-12-02 14:31:20 +0000359 return PyLong_FromLong((long)x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000360 return PyLong_FromUnsignedLong((unsigned long)x);
361#endif
362}
363
364static PyObject *
365nu_long(const char *p, const formatdef *f)
366{
367 long x;
368 memcpy((char *)&x, p, sizeof x);
Christian Heimes217cfd12007-12-02 14:31:20 +0000369 return PyLong_FromLong(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000370}
371
372static PyObject *
373nu_ulong(const char *p, const formatdef *f)
374{
375 unsigned long x;
376 memcpy((char *)&x, p, sizeof x);
377 if (x <= LONG_MAX)
Christian Heimes217cfd12007-12-02 14:31:20 +0000378 return PyLong_FromLong((long)x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000379 return PyLong_FromUnsignedLong(x);
380}
381
382/* Native mode doesn't support q or Q unless the platform C supports
383 long long (or, on Windows, __int64). */
384
385#ifdef HAVE_LONG_LONG
386
387static PyObject *
388nu_longlong(const char *p, const formatdef *f)
389{
390 PY_LONG_LONG x;
391 memcpy((char *)&x, p, sizeof x);
392 if (x >= LONG_MIN && x <= LONG_MAX)
Christian Heimes217cfd12007-12-02 14:31:20 +0000393 return PyLong_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
Thomas Wouters477c8d52006-05-27 19:21:47 +0000394 return PyLong_FromLongLong(x);
395}
396
397static PyObject *
398nu_ulonglong(const char *p, const formatdef *f)
399{
400 unsigned PY_LONG_LONG x;
401 memcpy((char *)&x, p, sizeof x);
402 if (x <= LONG_MAX)
Christian Heimes217cfd12007-12-02 14:31:20 +0000403 return PyLong_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
Thomas Wouters477c8d52006-05-27 19:21:47 +0000404 return PyLong_FromUnsignedLongLong(x);
405}
406
407#endif
408
409static PyObject *
Thomas Woutersb2137042007-02-01 18:02:27 +0000410nu_bool(const char *p, const formatdef *f)
411{
412 BOOL_TYPE x;
413 memcpy((char *)&x, p, sizeof x);
414 return PyBool_FromLong(x != 0);
415}
416
417
418static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +0000419nu_float(const char *p, const formatdef *f)
420{
421 float x;
422 memcpy((char *)&x, p, sizeof x);
423 return PyFloat_FromDouble((double)x);
424}
425
426static PyObject *
427nu_double(const char *p, const formatdef *f)
428{
429 double x;
430 memcpy((char *)&x, p, sizeof x);
431 return PyFloat_FromDouble(x);
432}
433
434static PyObject *
435nu_void_p(const char *p, const formatdef *f)
436{
437 void *x;
438 memcpy((char *)&x, p, sizeof x);
439 return PyLong_FromVoidPtr(x);
440}
441
442static int
443np_byte(char *p, PyObject *v, const formatdef *f)
444{
445 long x;
446 if (get_long(v, &x) < 0)
447 return -1;
448 if (x < -128 || x > 127){
449 PyErr_SetString(StructError,
450 "byte format requires -128 <= number <= 127");
451 return -1;
452 }
453 *p = (char)x;
454 return 0;
455}
456
457static int
458np_ubyte(char *p, PyObject *v, const formatdef *f)
459{
460 long x;
461 if (get_long(v, &x) < 0)
462 return -1;
463 if (x < 0 || x > 255){
464 PyErr_SetString(StructError,
465 "ubyte format requires 0 <= number <= 255");
466 return -1;
467 }
468 *p = (char)x;
469 return 0;
470}
471
472static int
473np_char(char *p, PyObject *v, const formatdef *f)
474{
Guido van Rossume625fd52007-05-27 09:19:04 +0000475 if (PyUnicode_Check(v)) {
476 v = _PyUnicode_AsDefaultEncodedString(v, NULL);
477 if (v == NULL)
478 return -1;
479 }
Christian Heimes72b710a2008-05-26 13:28:38 +0000480 if (!PyBytes_Check(v) || PyBytes_Size(v) != 1) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000481 PyErr_SetString(StructError,
Benjamin Peterson4ae19462008-07-31 15:03:40 +0000482 "char format requires bytes or string of length 1");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000483 return -1;
484 }
Christian Heimes72b710a2008-05-26 13:28:38 +0000485 *p = *PyBytes_AsString(v);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000486 return 0;
487}
488
489static int
490np_short(char *p, PyObject *v, const formatdef *f)
491{
492 long x;
493 short y;
494 if (get_long(v, &x) < 0)
495 return -1;
496 if (x < SHRT_MIN || x > SHRT_MAX){
497 PyErr_SetString(StructError,
498 "short format requires " STRINGIFY(SHRT_MIN)
499 " <= number <= " STRINGIFY(SHRT_MAX));
500 return -1;
501 }
502 y = (short)x;
503 memcpy(p, (char *)&y, sizeof y);
504 return 0;
505}
506
507static int
508np_ushort(char *p, PyObject *v, const formatdef *f)
509{
510 long x;
511 unsigned short y;
512 if (get_long(v, &x) < 0)
513 return -1;
514 if (x < 0 || x > USHRT_MAX){
515 PyErr_SetString(StructError,
516 "short format requires 0 <= number <= " STRINGIFY(USHRT_MAX));
517 return -1;
518 }
519 y = (unsigned short)x;
520 memcpy(p, (char *)&y, sizeof y);
521 return 0;
522}
523
524static int
525np_int(char *p, PyObject *v, const formatdef *f)
526{
527 long x;
528 int y;
529 if (get_long(v, &x) < 0)
530 return -1;
531#if (SIZEOF_LONG > SIZEOF_INT)
532 if ((x < ((long)INT_MIN)) || (x > ((long)INT_MAX)))
Georg Brandlb1441c72009-01-03 22:33:39 +0000533 RANGE_ERROR(x, f, 0, -1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000534#endif
535 y = (int)x;
536 memcpy(p, (char *)&y, sizeof y);
537 return 0;
538}
539
540static int
541np_uint(char *p, PyObject *v, const formatdef *f)
542{
543 unsigned long x;
544 unsigned int y;
Mark Dickinsonae681df2009-03-21 10:26:31 +0000545 if (get_ulong(v, &x) < 0)
Georg Brandlb1441c72009-01-03 22:33:39 +0000546 return -1;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000547 y = (unsigned int)x;
548#if (SIZEOF_LONG > SIZEOF_INT)
549 if (x > ((unsigned long)UINT_MAX))
Georg Brandlb1441c72009-01-03 22:33:39 +0000550 RANGE_ERROR(y, f, 1, -1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000551#endif
552 memcpy(p, (char *)&y, sizeof y);
553 return 0;
554}
555
556static int
557np_long(char *p, PyObject *v, const formatdef *f)
558{
559 long x;
560 if (get_long(v, &x) < 0)
561 return -1;
562 memcpy(p, (char *)&x, sizeof x);
563 return 0;
564}
565
566static int
567np_ulong(char *p, PyObject *v, const formatdef *f)
568{
569 unsigned long x;
Mark Dickinsonae681df2009-03-21 10:26:31 +0000570 if (get_ulong(v, &x) < 0)
Georg Brandlb1441c72009-01-03 22:33:39 +0000571 return -1;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000572 memcpy(p, (char *)&x, sizeof x);
573 return 0;
574}
575
576#ifdef HAVE_LONG_LONG
577
578static int
579np_longlong(char *p, PyObject *v, const formatdef *f)
580{
581 PY_LONG_LONG x;
582 if (get_longlong(v, &x) < 0)
583 return -1;
584 memcpy(p, (char *)&x, sizeof x);
585 return 0;
586}
587
588static int
589np_ulonglong(char *p, PyObject *v, const formatdef *f)
590{
591 unsigned PY_LONG_LONG x;
592 if (get_ulonglong(v, &x) < 0)
593 return -1;
594 memcpy(p, (char *)&x, sizeof x);
595 return 0;
596}
597#endif
598
Thomas Woutersb2137042007-02-01 18:02:27 +0000599
600static int
601np_bool(char *p, PyObject *v, const formatdef *f)
602{
603 BOOL_TYPE y;
604 y = PyObject_IsTrue(v);
605 memcpy(p, (char *)&y, sizeof y);
606 return 0;
607}
608
Thomas Wouters477c8d52006-05-27 19:21:47 +0000609static int
610np_float(char *p, PyObject *v, const formatdef *f)
611{
612 float x = (float)PyFloat_AsDouble(v);
613 if (x == -1 && PyErr_Occurred()) {
614 PyErr_SetString(StructError,
615 "required argument is not a float");
616 return -1;
617 }
618 memcpy(p, (char *)&x, sizeof x);
619 return 0;
620}
621
622static int
623np_double(char *p, PyObject *v, const formatdef *f)
624{
625 double x = PyFloat_AsDouble(v);
626 if (x == -1 && PyErr_Occurred()) {
627 PyErr_SetString(StructError,
628 "required argument is not a float");
629 return -1;
630 }
631 memcpy(p, (char *)&x, sizeof(double));
632 return 0;
633}
634
635static int
636np_void_p(char *p, PyObject *v, const formatdef *f)
637{
638 void *x;
639
640 v = get_pylong(v);
641 if (v == NULL)
642 return -1;
643 assert(PyLong_Check(v));
644 x = PyLong_AsVoidPtr(v);
645 Py_DECREF(v);
646 if (x == NULL && PyErr_Occurred())
647 return -1;
648 memcpy(p, (char *)&x, sizeof x);
649 return 0;
650}
651
652static formatdef native_table[] = {
653 {'x', sizeof(char), 0, NULL},
654 {'b', sizeof(char), 0, nu_byte, np_byte},
655 {'B', sizeof(char), 0, nu_ubyte, np_ubyte},
656 {'c', sizeof(char), 0, nu_char, np_char},
657 {'s', sizeof(char), 0, NULL},
658 {'p', sizeof(char), 0, NULL},
659 {'h', sizeof(short), SHORT_ALIGN, nu_short, np_short},
660 {'H', sizeof(short), SHORT_ALIGN, nu_ushort, np_ushort},
661 {'i', sizeof(int), INT_ALIGN, nu_int, np_int},
662 {'I', sizeof(int), INT_ALIGN, nu_uint, np_uint},
663 {'l', sizeof(long), LONG_ALIGN, nu_long, np_long},
664 {'L', sizeof(long), LONG_ALIGN, nu_ulong, np_ulong},
665#ifdef HAVE_LONG_LONG
666 {'q', sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_longlong, np_longlong},
667 {'Q', sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_ulonglong,np_ulonglong},
668#endif
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000669 {'?', sizeof(BOOL_TYPE), BOOL_ALIGN, nu_bool, np_bool},
Thomas Wouters477c8d52006-05-27 19:21:47 +0000670 {'f', sizeof(float), FLOAT_ALIGN, nu_float, np_float},
671 {'d', sizeof(double), DOUBLE_ALIGN, nu_double, np_double},
672 {'P', sizeof(void *), VOID_P_ALIGN, nu_void_p, np_void_p},
673 {0}
674};
675
676/* Big-endian routines. *****************************************************/
677
678static PyObject *
679bu_int(const char *p, const formatdef *f)
680{
681 long x = 0;
682 Py_ssize_t i = f->size;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000683 const unsigned char *bytes = (const unsigned char *)p;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000684 do {
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000685 x = (x<<8) | *bytes++;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000686 } while (--i > 0);
687 /* Extend the sign bit. */
688 if (SIZEOF_LONG > f->size)
689 x |= -(x & (1L << ((8 * f->size) - 1)));
Christian Heimes217cfd12007-12-02 14:31:20 +0000690 return PyLong_FromLong(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000691}
692
693static PyObject *
694bu_uint(const char *p, const formatdef *f)
695{
696 unsigned long x = 0;
697 Py_ssize_t i = f->size;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000698 const unsigned char *bytes = (const unsigned char *)p;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000699 do {
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000700 x = (x<<8) | *bytes++;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000701 } while (--i > 0);
702 if (x <= LONG_MAX)
Christian Heimes217cfd12007-12-02 14:31:20 +0000703 return PyLong_FromLong((long)x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000704 return PyLong_FromUnsignedLong(x);
705}
706
707static PyObject *
708bu_longlong(const char *p, const formatdef *f)
709{
710#ifdef HAVE_LONG_LONG
711 PY_LONG_LONG x = 0;
712 Py_ssize_t i = f->size;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000713 const unsigned char *bytes = (const unsigned char *)p;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000714 do {
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000715 x = (x<<8) | *bytes++;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000716 } while (--i > 0);
717 /* Extend the sign bit. */
718 if (SIZEOF_LONG_LONG > f->size)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000719 x |= -(x & ((PY_LONG_LONG)1 << ((8 * f->size) - 1)));
Thomas Wouters477c8d52006-05-27 19:21:47 +0000720 if (x >= LONG_MIN && x <= LONG_MAX)
Christian Heimes217cfd12007-12-02 14:31:20 +0000721 return PyLong_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
Thomas Wouters477c8d52006-05-27 19:21:47 +0000722 return PyLong_FromLongLong(x);
723#else
724 return _PyLong_FromByteArray((const unsigned char *)p,
725 8,
726 0, /* little-endian */
727 1 /* signed */);
728#endif
729}
730
731static PyObject *
732bu_ulonglong(const char *p, const formatdef *f)
733{
734#ifdef HAVE_LONG_LONG
735 unsigned PY_LONG_LONG x = 0;
736 Py_ssize_t i = f->size;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000737 const unsigned char *bytes = (const unsigned char *)p;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000738 do {
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000739 x = (x<<8) | *bytes++;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000740 } while (--i > 0);
741 if (x <= LONG_MAX)
Christian Heimes217cfd12007-12-02 14:31:20 +0000742 return PyLong_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
Thomas Wouters477c8d52006-05-27 19:21:47 +0000743 return PyLong_FromUnsignedLongLong(x);
744#else
745 return _PyLong_FromByteArray((const unsigned char *)p,
746 8,
747 0, /* little-endian */
748 0 /* signed */);
749#endif
750}
751
752static PyObject *
753bu_float(const char *p, const formatdef *f)
754{
755 return unpack_float(p, 0);
756}
757
758static PyObject *
759bu_double(const char *p, const formatdef *f)
760{
761 return unpack_double(p, 0);
762}
763
Thomas Woutersb2137042007-02-01 18:02:27 +0000764static PyObject *
765bu_bool(const char *p, const formatdef *f)
766{
767 char x;
768 memcpy((char *)&x, p, sizeof x);
769 return PyBool_FromLong(x != 0);
770}
771
Thomas Wouters477c8d52006-05-27 19:21:47 +0000772static int
773bp_int(char *p, PyObject *v, const formatdef *f)
774{
775 long x;
776 Py_ssize_t i;
Mark Dickinsonae681df2009-03-21 10:26:31 +0000777 if (get_long(v, &x) < 0)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000778 return -1;
779 i = f->size;
780 if (i != SIZEOF_LONG) {
781 if ((i == 2) && (x < -32768 || x > 32767))
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000782 RANGE_ERROR(x, f, 0, 0xffffL);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000783#if (SIZEOF_LONG != 4)
784 else if ((i == 4) && (x < -2147483648L || x > 2147483647L))
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000785 RANGE_ERROR(x, f, 0, 0xffffffffL);
786#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +0000787 }
788 do {
789 p[--i] = (char)x;
790 x >>= 8;
791 } while (i > 0);
792 return 0;
793}
794
795static int
796bp_uint(char *p, PyObject *v, const formatdef *f)
797{
798 unsigned long x;
799 Py_ssize_t i;
Mark Dickinsonae681df2009-03-21 10:26:31 +0000800 if (get_ulong(v, &x) < 0)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000801 return -1;
802 i = f->size;
803 if (i != SIZEOF_LONG) {
804 unsigned long maxint = 1;
805 maxint <<= (unsigned long)(i * 8);
806 if (x >= maxint)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000807 RANGE_ERROR(x, f, 1, maxint - 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000808 }
809 do {
810 p[--i] = (char)x;
811 x >>= 8;
812 } while (i > 0);
813 return 0;
814}
815
816static int
817bp_longlong(char *p, PyObject *v, const formatdef *f)
818{
819 int res;
820 v = get_pylong(v);
821 if (v == NULL)
822 return -1;
823 res = _PyLong_AsByteArray((PyLongObject *)v,
824 (unsigned char *)p,
825 8,
826 0, /* little_endian */
827 1 /* signed */);
828 Py_DECREF(v);
829 return res;
830}
831
832static int
833bp_ulonglong(char *p, PyObject *v, const formatdef *f)
834{
835 int res;
836 v = get_pylong(v);
837 if (v == NULL)
838 return -1;
839 res = _PyLong_AsByteArray((PyLongObject *)v,
840 (unsigned char *)p,
841 8,
842 0, /* little_endian */
843 0 /* signed */);
844 Py_DECREF(v);
845 return res;
846}
847
848static int
849bp_float(char *p, PyObject *v, const formatdef *f)
850{
851 double x = PyFloat_AsDouble(v);
852 if (x == -1 && PyErr_Occurred()) {
853 PyErr_SetString(StructError,
854 "required argument is not a float");
855 return -1;
856 }
857 return _PyFloat_Pack4(x, (unsigned char *)p, 0);
858}
859
860static int
861bp_double(char *p, PyObject *v, const formatdef *f)
862{
863 double x = PyFloat_AsDouble(v);
864 if (x == -1 && PyErr_Occurred()) {
865 PyErr_SetString(StructError,
866 "required argument is not a float");
867 return -1;
868 }
869 return _PyFloat_Pack8(x, (unsigned char *)p, 0);
870}
871
Thomas Woutersb2137042007-02-01 18:02:27 +0000872static int
873bp_bool(char *p, PyObject *v, const formatdef *f)
874{
875 char y;
876 y = PyObject_IsTrue(v);
877 memcpy(p, (char *)&y, sizeof y);
878 return 0;
879}
880
Thomas Wouters477c8d52006-05-27 19:21:47 +0000881static formatdef bigendian_table[] = {
882 {'x', 1, 0, NULL},
883 {'b', 1, 0, nu_byte, np_byte},
884 {'B', 1, 0, nu_ubyte, np_ubyte},
885 {'c', 1, 0, nu_char, np_char},
886 {'s', 1, 0, NULL},
887 {'p', 1, 0, NULL},
888 {'h', 2, 0, bu_int, bp_int},
889 {'H', 2, 0, bu_uint, bp_uint},
890 {'i', 4, 0, bu_int, bp_int},
891 {'I', 4, 0, bu_uint, bp_uint},
892 {'l', 4, 0, bu_int, bp_int},
893 {'L', 4, 0, bu_uint, bp_uint},
894 {'q', 8, 0, bu_longlong, bp_longlong},
895 {'Q', 8, 0, bu_ulonglong, bp_ulonglong},
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000896 {'?', 1, 0, bu_bool, bp_bool},
Thomas Wouters477c8d52006-05-27 19:21:47 +0000897 {'f', 4, 0, bu_float, bp_float},
898 {'d', 8, 0, bu_double, bp_double},
899 {0}
900};
901
902/* Little-endian routines. *****************************************************/
903
904static PyObject *
905lu_int(const char *p, const formatdef *f)
906{
907 long x = 0;
908 Py_ssize_t i = f->size;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000909 const unsigned char *bytes = (const unsigned char *)p;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000910 do {
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000911 x = (x<<8) | bytes[--i];
Thomas Wouters477c8d52006-05-27 19:21:47 +0000912 } while (i > 0);
913 /* Extend the sign bit. */
914 if (SIZEOF_LONG > f->size)
915 x |= -(x & (1L << ((8 * f->size) - 1)));
Christian Heimes217cfd12007-12-02 14:31:20 +0000916 return PyLong_FromLong(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000917}
918
919static PyObject *
920lu_uint(const char *p, const formatdef *f)
921{
922 unsigned long x = 0;
923 Py_ssize_t i = f->size;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000924 const unsigned char *bytes = (const unsigned char *)p;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000925 do {
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000926 x = (x<<8) | bytes[--i];
Thomas Wouters477c8d52006-05-27 19:21:47 +0000927 } while (i > 0);
928 if (x <= LONG_MAX)
Christian Heimes217cfd12007-12-02 14:31:20 +0000929 return PyLong_FromLong((long)x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000930 return PyLong_FromUnsignedLong((long)x);
931}
932
933static PyObject *
934lu_longlong(const char *p, const formatdef *f)
935{
936#ifdef HAVE_LONG_LONG
937 PY_LONG_LONG x = 0;
938 Py_ssize_t i = f->size;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000939 const unsigned char *bytes = (const unsigned char *)p;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000940 do {
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000941 x = (x<<8) | bytes[--i];
Thomas Wouters477c8d52006-05-27 19:21:47 +0000942 } while (i > 0);
943 /* Extend the sign bit. */
944 if (SIZEOF_LONG_LONG > f->size)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000945 x |= -(x & ((PY_LONG_LONG)1 << ((8 * f->size) - 1)));
Thomas Wouters477c8d52006-05-27 19:21:47 +0000946 if (x >= LONG_MIN && x <= LONG_MAX)
Christian Heimes217cfd12007-12-02 14:31:20 +0000947 return PyLong_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
Thomas Wouters477c8d52006-05-27 19:21:47 +0000948 return PyLong_FromLongLong(x);
949#else
950 return _PyLong_FromByteArray((const unsigned char *)p,
951 8,
952 1, /* little-endian */
953 1 /* signed */);
954#endif
955}
956
957static PyObject *
958lu_ulonglong(const char *p, const formatdef *f)
959{
960#ifdef HAVE_LONG_LONG
961 unsigned PY_LONG_LONG x = 0;
962 Py_ssize_t i = f->size;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000963 const unsigned char *bytes = (const unsigned char *)p;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000964 do {
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000965 x = (x<<8) | bytes[--i];
Thomas Wouters477c8d52006-05-27 19:21:47 +0000966 } while (i > 0);
967 if (x <= LONG_MAX)
Christian Heimes217cfd12007-12-02 14:31:20 +0000968 return PyLong_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
Thomas Wouters477c8d52006-05-27 19:21:47 +0000969 return PyLong_FromUnsignedLongLong(x);
970#else
971 return _PyLong_FromByteArray((const unsigned char *)p,
972 8,
973 1, /* little-endian */
974 0 /* signed */);
975#endif
976}
977
978static PyObject *
979lu_float(const char *p, const formatdef *f)
980{
981 return unpack_float(p, 1);
982}
983
984static PyObject *
985lu_double(const char *p, const formatdef *f)
986{
987 return unpack_double(p, 1);
988}
989
990static int
991lp_int(char *p, PyObject *v, const formatdef *f)
992{
993 long x;
994 Py_ssize_t i;
Mark Dickinsonae681df2009-03-21 10:26:31 +0000995 if (get_long(v, &x) < 0)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000996 return -1;
997 i = f->size;
998 if (i != SIZEOF_LONG) {
999 if ((i == 2) && (x < -32768 || x > 32767))
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001000 RANGE_ERROR(x, f, 0, 0xffffL);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001001#if (SIZEOF_LONG != 4)
1002 else if ((i == 4) && (x < -2147483648L || x > 2147483647L))
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001003 RANGE_ERROR(x, f, 0, 0xffffffffL);
1004#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00001005 }
1006 do {
1007 *p++ = (char)x;
1008 x >>= 8;
1009 } while (--i > 0);
1010 return 0;
1011}
1012
1013static int
1014lp_uint(char *p, PyObject *v, const formatdef *f)
1015{
1016 unsigned long x;
1017 Py_ssize_t i;
Mark Dickinsonae681df2009-03-21 10:26:31 +00001018 if (get_ulong(v, &x) < 0)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001019 return -1;
1020 i = f->size;
1021 if (i != SIZEOF_LONG) {
1022 unsigned long maxint = 1;
1023 maxint <<= (unsigned long)(i * 8);
1024 if (x >= maxint)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001025 RANGE_ERROR(x, f, 1, maxint - 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001026 }
1027 do {
1028 *p++ = (char)x;
1029 x >>= 8;
1030 } while (--i > 0);
1031 return 0;
1032}
1033
1034static int
1035lp_longlong(char *p, PyObject *v, const formatdef *f)
1036{
1037 int res;
1038 v = get_pylong(v);
1039 if (v == NULL)
1040 return -1;
1041 res = _PyLong_AsByteArray((PyLongObject*)v,
1042 (unsigned char *)p,
1043 8,
1044 1, /* little_endian */
1045 1 /* signed */);
1046 Py_DECREF(v);
1047 return res;
1048}
1049
1050static int
1051lp_ulonglong(char *p, PyObject *v, const formatdef *f)
1052{
1053 int res;
1054 v = get_pylong(v);
1055 if (v == NULL)
1056 return -1;
1057 res = _PyLong_AsByteArray((PyLongObject*)v,
1058 (unsigned char *)p,
1059 8,
1060 1, /* little_endian */
1061 0 /* signed */);
1062 Py_DECREF(v);
1063 return res;
1064}
1065
1066static int
1067lp_float(char *p, PyObject *v, const formatdef *f)
1068{
1069 double x = PyFloat_AsDouble(v);
1070 if (x == -1 && PyErr_Occurred()) {
1071 PyErr_SetString(StructError,
1072 "required argument is not a float");
1073 return -1;
1074 }
1075 return _PyFloat_Pack4(x, (unsigned char *)p, 1);
1076}
1077
1078static int
1079lp_double(char *p, PyObject *v, const formatdef *f)
1080{
1081 double x = PyFloat_AsDouble(v);
1082 if (x == -1 && PyErr_Occurred()) {
1083 PyErr_SetString(StructError,
1084 "required argument is not a float");
1085 return -1;
1086 }
1087 return _PyFloat_Pack8(x, (unsigned char *)p, 1);
1088}
1089
1090static formatdef lilendian_table[] = {
1091 {'x', 1, 0, NULL},
1092 {'b', 1, 0, nu_byte, np_byte},
1093 {'B', 1, 0, nu_ubyte, np_ubyte},
1094 {'c', 1, 0, nu_char, np_char},
1095 {'s', 1, 0, NULL},
1096 {'p', 1, 0, NULL},
1097 {'h', 2, 0, lu_int, lp_int},
1098 {'H', 2, 0, lu_uint, lp_uint},
1099 {'i', 4, 0, lu_int, lp_int},
1100 {'I', 4, 0, lu_uint, lp_uint},
1101 {'l', 4, 0, lu_int, lp_int},
1102 {'L', 4, 0, lu_uint, lp_uint},
1103 {'q', 8, 0, lu_longlong, lp_longlong},
1104 {'Q', 8, 0, lu_ulonglong, lp_ulonglong},
Christian Heimesdd15f6c2008-03-16 00:07:10 +00001105 {'?', 1, 0, bu_bool, bp_bool}, /* Std rep not endian dep,
Thomas Woutersb2137042007-02-01 18:02:27 +00001106 but potentially different from native rep -- reuse bx_bool funcs. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001107 {'f', 4, 0, lu_float, lp_float},
1108 {'d', 8, 0, lu_double, lp_double},
1109 {0}
1110};
1111
1112
1113static const formatdef *
1114whichtable(char **pfmt)
1115{
1116 const char *fmt = (*pfmt)++; /* May be backed out of later */
1117 switch (*fmt) {
1118 case '<':
1119 return lilendian_table;
1120 case '>':
1121 case '!': /* Network byte order is big-endian */
1122 return bigendian_table;
1123 case '=': { /* Host byte order -- different from native in aligment! */
1124 int n = 1;
1125 char *p = (char *) &n;
1126 if (*p == 1)
1127 return lilendian_table;
1128 else
1129 return bigendian_table;
1130 }
1131 default:
1132 --*pfmt; /* Back out of pointer increment */
1133 /* Fall through */
1134 case '@':
1135 return native_table;
1136 }
1137}
1138
1139
1140/* Get the table entry for a format code */
1141
1142static const formatdef *
1143getentry(int c, const formatdef *f)
1144{
1145 for (; f->format != '\0'; f++) {
1146 if (f->format == c) {
1147 return f;
1148 }
1149 }
1150 PyErr_SetString(StructError, "bad char in struct format");
1151 return NULL;
1152}
1153
1154
1155/* Align a size according to a format code */
1156
1157static int
1158align(Py_ssize_t size, char c, const formatdef *e)
1159{
1160 if (e->format == c) {
1161 if (e->alignment) {
1162 size = ((size + e->alignment - 1)
1163 / e->alignment)
1164 * e->alignment;
1165 }
1166 }
1167 return size;
1168}
1169
1170
1171/* calculate the size of a format string */
1172
1173static int
1174prepare_s(PyStructObject *self)
1175{
1176 const formatdef *f;
1177 const formatdef *e;
1178 formatcode *codes;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001179
Thomas Wouters477c8d52006-05-27 19:21:47 +00001180 const char *s;
1181 const char *fmt;
1182 char c;
1183 Py_ssize_t size, len, num, itemsize, x;
1184
Christian Heimes72b710a2008-05-26 13:28:38 +00001185 fmt = PyBytes_AS_STRING(self->s_format);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001186
1187 f = whichtable((char **)&fmt);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001188
Thomas Wouters477c8d52006-05-27 19:21:47 +00001189 s = fmt;
1190 size = 0;
1191 len = 0;
1192 while ((c = *s++) != '\0') {
1193 if (isspace(Py_CHARMASK(c)))
1194 continue;
1195 if ('0' <= c && c <= '9') {
1196 num = c - '0';
1197 while ('0' <= (c = *s++) && c <= '9') {
1198 x = num*10 + (c - '0');
1199 if (x/10 != num) {
1200 PyErr_SetString(
1201 StructError,
1202 "overflow in item count");
1203 return -1;
1204 }
1205 num = x;
1206 }
1207 if (c == '\0')
1208 break;
1209 }
1210 else
1211 num = 1;
1212
1213 e = getentry(c, f);
1214 if (e == NULL)
1215 return -1;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001216
Thomas Wouters477c8d52006-05-27 19:21:47 +00001217 switch (c) {
1218 case 's': /* fall through */
1219 case 'p': len++; break;
1220 case 'x': break;
1221 default: len += num; break;
1222 }
1223
1224 itemsize = e->size;
1225 size = align(size, c, e);
1226 x = num * itemsize;
1227 size += x;
1228 if (x/itemsize != num || size < 0) {
1229 PyErr_SetString(StructError,
1230 "total struct size too long");
1231 return -1;
1232 }
1233 }
1234
Amaury Forgeot d'Arc35c86582008-06-17 21:11:29 +00001235 /* check for overflow */
1236 if ((len + 1) > (PY_SSIZE_T_MAX / sizeof(formatcode))) {
1237 PyErr_NoMemory();
1238 return -1;
1239 }
1240
Thomas Wouters477c8d52006-05-27 19:21:47 +00001241 self->s_size = size;
1242 self->s_len = len;
1243 codes = PyMem_MALLOC((len + 1) * sizeof(formatcode));
1244 if (codes == NULL) {
1245 PyErr_NoMemory();
1246 return -1;
1247 }
1248 self->s_codes = codes;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001249
Thomas Wouters477c8d52006-05-27 19:21:47 +00001250 s = fmt;
1251 size = 0;
1252 while ((c = *s++) != '\0') {
1253 if (isspace(Py_CHARMASK(c)))
1254 continue;
1255 if ('0' <= c && c <= '9') {
1256 num = c - '0';
1257 while ('0' <= (c = *s++) && c <= '9')
1258 num = num*10 + (c - '0');
1259 if (c == '\0')
1260 break;
1261 }
1262 else
1263 num = 1;
1264
1265 e = getentry(c, f);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001266
Thomas Wouters477c8d52006-05-27 19:21:47 +00001267 size = align(size, c, e);
1268 if (c == 's' || c == 'p') {
1269 codes->offset = size;
1270 codes->size = num;
1271 codes->fmtdef = e;
1272 codes++;
1273 size += num;
1274 } else if (c == 'x') {
1275 size += num;
1276 } else {
1277 while (--num >= 0) {
1278 codes->offset = size;
1279 codes->size = e->size;
1280 codes->fmtdef = e;
1281 codes++;
1282 size += e->size;
1283 }
1284 }
1285 }
1286 codes->fmtdef = NULL;
1287 codes->offset = size;
1288 codes->size = 0;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001289
Thomas Wouters477c8d52006-05-27 19:21:47 +00001290 return 0;
1291}
1292
1293static PyObject *
1294s_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1295{
1296 PyObject *self;
1297
1298 assert(type != NULL && type->tp_alloc != NULL);
1299
1300 self = type->tp_alloc(type, 0);
1301 if (self != NULL) {
1302 PyStructObject *s = (PyStructObject*)self;
1303 Py_INCREF(Py_None);
1304 s->s_format = Py_None;
1305 s->s_codes = NULL;
1306 s->s_size = -1;
1307 s->s_len = -1;
1308 }
1309 return self;
1310}
1311
1312static int
1313s_init(PyObject *self, PyObject *args, PyObject *kwds)
1314{
1315 PyStructObject *soself = (PyStructObject *)self;
1316 PyObject *o_format = NULL;
1317 int ret = 0;
1318 static char *kwlist[] = {"format", 0};
1319
1320 assert(PyStruct_Check(self));
1321
Christian Heimesa34706f2008-01-04 03:06:10 +00001322 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:Struct", kwlist,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001323 &o_format))
1324 return -1;
1325
Christian Heimesa34706f2008-01-04 03:06:10 +00001326 if (PyUnicode_Check(o_format)) {
1327 o_format = PyUnicode_AsASCIIString(o_format);
1328 if (o_format == NULL)
1329 return -1;
1330 }
1331 /* XXX support buffer interface, too */
1332 else {
1333 Py_INCREF(o_format);
1334 }
1335
Christian Heimes72b710a2008-05-26 13:28:38 +00001336 if (!PyBytes_Check(o_format)) {
Christian Heimesa34706f2008-01-04 03:06:10 +00001337 Py_DECREF(o_format);
1338 PyErr_Format(PyExc_TypeError,
1339 "Struct() argument 1 must be bytes, not %.200s",
1340 Py_TYPE(o_format)->tp_name);
1341 return -1;
1342 }
1343
Christian Heimes18c66892008-02-17 13:31:39 +00001344 Py_CLEAR(soself->s_format);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001345 soself->s_format = o_format;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001346
Thomas Wouters477c8d52006-05-27 19:21:47 +00001347 ret = prepare_s(soself);
1348 return ret;
1349}
1350
1351static void
1352s_dealloc(PyStructObject *s)
1353{
1354 if (s->weakreflist != NULL)
1355 PyObject_ClearWeakRefs((PyObject *)s);
1356 if (s->s_codes != NULL) {
1357 PyMem_FREE(s->s_codes);
1358 }
1359 Py_XDECREF(s->s_format);
Christian Heimes90aa7642007-12-19 02:45:37 +00001360 Py_TYPE(s)->tp_free((PyObject *)s);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001361}
1362
1363static PyObject *
1364s_unpack_internal(PyStructObject *soself, char *startfrom) {
1365 formatcode *code;
1366 Py_ssize_t i = 0;
1367 PyObject *result = PyTuple_New(soself->s_len);
1368 if (result == NULL)
1369 return NULL;
1370
1371 for (code = soself->s_codes; code->fmtdef != NULL; code++) {
1372 PyObject *v;
1373 const formatdef *e = code->fmtdef;
1374 const char *res = startfrom + code->offset;
1375 if (e->format == 's') {
Christian Heimes72b710a2008-05-26 13:28:38 +00001376 v = PyBytes_FromStringAndSize(res, code->size);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001377 } else if (e->format == 'p') {
1378 Py_ssize_t n = *(unsigned char*)res;
1379 if (n >= code->size)
1380 n = code->size - 1;
Christian Heimes72b710a2008-05-26 13:28:38 +00001381 v = PyBytes_FromStringAndSize(res + 1, n);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001382 } else {
1383 v = e->unpack(res, e);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001384 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001385 if (v == NULL)
1386 goto fail;
1387 PyTuple_SET_ITEM(result, i++, v);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001388 }
1389
1390 return result;
1391fail:
1392 Py_DECREF(result);
1393 return NULL;
1394}
1395
1396
1397PyDoc_STRVAR(s_unpack__doc__,
Guido van Rossum913dd0b2007-04-13 03:33:53 +00001398"S.unpack(buffer) -> (v1, v2, ...)\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +00001399\n\
1400Return tuple containing values unpacked according to this Struct's format.\n\
Guido van Rossum913dd0b2007-04-13 03:33:53 +00001401Requires len(buffer) == self.size. See struct.__doc__ for more on format\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +00001402strings.");
1403
1404static PyObject *
Guido van Rossum98297ee2007-11-06 21:34:58 +00001405s_unpack(PyObject *self, PyObject *input)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001406{
Guido van Rossum98297ee2007-11-06 21:34:58 +00001407 Py_buffer vbuf;
1408 PyObject *result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001409 PyStructObject *soself = (PyStructObject *)self;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001410
Thomas Wouters477c8d52006-05-27 19:21:47 +00001411 assert(PyStruct_Check(self));
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001412 assert(soself->s_codes != NULL);
Guido van Rossum98297ee2007-11-06 21:34:58 +00001413 if (PyObject_GetBuffer(input, &vbuf, PyBUF_SIMPLE) < 0)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001414 return NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001415 if (vbuf.len != soself->s_size) {
1416 PyErr_Format(StructError,
1417 "unpack requires a bytes argument of length %zd",
1418 soself->s_size);
Martin v. Löwis423be952008-08-13 15:53:07 +00001419 PyBuffer_Release(&vbuf);
Guido van Rossum98297ee2007-11-06 21:34:58 +00001420 return NULL;
1421 }
1422 result = s_unpack_internal(soself, vbuf.buf);
Martin v. Löwis423be952008-08-13 15:53:07 +00001423 PyBuffer_Release(&vbuf);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001424 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001425}
1426
1427PyDoc_STRVAR(s_unpack_from__doc__,
1428"S.unpack_from(buffer[, offset]) -> (v1, v2, ...)\n\
1429\n\
1430Return tuple containing values unpacked according to this Struct's format.\n\
1431Unlike unpack, unpack_from can unpack values from any object supporting\n\
1432the buffer API, not just str. Requires len(buffer[offset:]) >= self.size.\n\
1433See struct.__doc__ for more on format strings.");
1434
1435static PyObject *
1436s_unpack_from(PyObject *self, PyObject *args, PyObject *kwds)
1437{
1438 static char *kwlist[] = {"buffer", "offset", 0};
Guido van Rossum98297ee2007-11-06 21:34:58 +00001439
1440 PyObject *input;
1441 Py_ssize_t offset = 0;
1442 Py_buffer vbuf;
1443 PyObject *result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001444 PyStructObject *soself = (PyStructObject *)self;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001445
Thomas Wouters477c8d52006-05-27 19:21:47 +00001446 assert(PyStruct_Check(self));
1447 assert(soself->s_codes != NULL);
1448
Guido van Rossum98297ee2007-11-06 21:34:58 +00001449 if (!PyArg_ParseTupleAndKeywords(args, kwds,
1450 "O|n:unpack_from", kwlist,
1451 &input, &offset))
Thomas Wouters477c8d52006-05-27 19:21:47 +00001452 return NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001453 if (PyObject_GetBuffer(input, &vbuf, PyBUF_SIMPLE) < 0)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001454 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001455 if (offset < 0)
Guido van Rossum98297ee2007-11-06 21:34:58 +00001456 offset += vbuf.len;
1457 if (offset < 0 || vbuf.len - offset < soself->s_size) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001458 PyErr_Format(StructError,
1459 "unpack_from requires a buffer of at least %zd bytes",
1460 soself->s_size);
Martin v. Löwis423be952008-08-13 15:53:07 +00001461 PyBuffer_Release(&vbuf);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001462 return NULL;
1463 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001464 result = s_unpack_internal(soself, (char*)vbuf.buf + offset);
Martin v. Löwis423be952008-08-13 15:53:07 +00001465 PyBuffer_Release(&vbuf);
Guido van Rossum98297ee2007-11-06 21:34:58 +00001466 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001467}
1468
1469
1470/*
1471 * Guts of the pack function.
1472 *
1473 * Takes a struct object, a tuple of arguments, and offset in that tuple of
1474 * argument for where to start processing the arguments for packing, and a
1475 * character buffer for writing the packed string. The caller must insure
1476 * that the buffer may contain the required length for packing the arguments.
1477 * 0 is returned on success, 1 is returned if there is an error.
1478 *
1479 */
1480static int
1481s_pack_internal(PyStructObject *soself, PyObject *args, int offset, char* buf)
1482{
1483 formatcode *code;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001484 /* XXX(nnorwitz): why does i need to be a local? can we use
1485 the offset parameter or do we need the wider width? */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001486 Py_ssize_t i;
1487
1488 memset(buf, '\0', soself->s_size);
1489 i = offset;
1490 for (code = soself->s_codes; code->fmtdef != NULL; code++) {
1491 Py_ssize_t n;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001492 PyObject *v = PyTuple_GET_ITEM(args, i++);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001493 const formatdef *e = code->fmtdef;
1494 char *res = buf + code->offset;
1495 if (e->format == 's') {
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001496 int isstring;
1497 void *p;
1498 if (PyUnicode_Check(v)) {
1499 v = _PyUnicode_AsDefaultEncodedString(v, NULL);
1500 if (v == NULL)
1501 return -1;
1502 }
Christian Heimes72b710a2008-05-26 13:28:38 +00001503 isstring = PyBytes_Check(v);
Christian Heimes9c4756e2008-05-26 13:22:05 +00001504 if (!isstring && !PyByteArray_Check(v)) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001505 PyErr_SetString(StructError,
Benjamin Peterson4ae19462008-07-31 15:03:40 +00001506 "argument for 's' must be a bytes or string");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001507 return -1;
1508 }
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001509 if (isstring) {
Christian Heimes72b710a2008-05-26 13:28:38 +00001510 n = PyBytes_GET_SIZE(v);
1511 p = PyBytes_AS_STRING(v);
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001512 }
1513 else {
Christian Heimes9c4756e2008-05-26 13:22:05 +00001514 n = PyByteArray_GET_SIZE(v);
1515 p = PyByteArray_AS_STRING(v);
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001516 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001517 if (n > code->size)
1518 n = code->size;
1519 if (n > 0)
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001520 memcpy(res, p, n);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001521 } else if (e->format == 'p') {
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001522 int isstring;
1523 void *p;
1524 if (PyUnicode_Check(v)) {
1525 v = _PyUnicode_AsDefaultEncodedString(v, NULL);
1526 if (v == NULL)
1527 return -1;
1528 }
Christian Heimes72b710a2008-05-26 13:28:38 +00001529 isstring = PyBytes_Check(v);
Christian Heimes9c4756e2008-05-26 13:22:05 +00001530 if (!isstring && !PyByteArray_Check(v)) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001531 PyErr_SetString(StructError,
Benjamin Peterson4ae19462008-07-31 15:03:40 +00001532 "argument for 'p' must be a bytes or string");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001533 return -1;
1534 }
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001535 if (isstring) {
Christian Heimes72b710a2008-05-26 13:28:38 +00001536 n = PyBytes_GET_SIZE(v);
1537 p = PyBytes_AS_STRING(v);
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001538 }
1539 else {
Christian Heimes9c4756e2008-05-26 13:22:05 +00001540 n = PyByteArray_GET_SIZE(v);
1541 p = PyByteArray_AS_STRING(v);
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001542 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001543 if (n > (code->size - 1))
1544 n = code->size - 1;
1545 if (n > 0)
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001546 memcpy(res + 1, p, n);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001547 if (n > 255)
1548 n = 255;
1549 *res = Py_SAFE_DOWNCAST(n, Py_ssize_t, unsigned char);
1550 } else {
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001551 if (e->pack(res, v, e) < 0) {
1552 if (PyLong_Check(v) && PyErr_ExceptionMatches(PyExc_OverflowError))
1553 PyErr_SetString(StructError,
1554 "long too large to convert to int");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001555 return -1;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001556 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001557 }
1558 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001559
Thomas Wouters477c8d52006-05-27 19:21:47 +00001560 /* Success */
1561 return 0;
1562}
1563
1564
1565PyDoc_STRVAR(s_pack__doc__,
Benjamin Peterson4ae19462008-07-31 15:03:40 +00001566"S.pack(v1, v2, ...) -> bytes\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +00001567\n\
Benjamin Peterson4ae19462008-07-31 15:03:40 +00001568Return a bytes containing values v1, v2, ... packed according to this\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +00001569Struct's format. See struct.__doc__ for more on format strings.");
1570
1571static PyObject *
1572s_pack(PyObject *self, PyObject *args)
1573{
1574 PyStructObject *soself;
1575 PyObject *result;
1576
1577 /* Validate arguments. */
1578 soself = (PyStructObject *)self;
1579 assert(PyStruct_Check(self));
1580 assert(soself->s_codes != NULL);
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001581 if (PyTuple_GET_SIZE(args) != soself->s_len)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001582 {
1583 PyErr_Format(StructError,
1584 "pack requires exactly %zd arguments", soself->s_len);
1585 return NULL;
1586 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001587
Thomas Wouters477c8d52006-05-27 19:21:47 +00001588 /* Allocate a new string */
Christian Heimes72b710a2008-05-26 13:28:38 +00001589 result = PyBytes_FromStringAndSize((char *)NULL, soself->s_size);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001590 if (result == NULL)
1591 return NULL;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001592
Thomas Wouters477c8d52006-05-27 19:21:47 +00001593 /* Call the guts */
Christian Heimes72b710a2008-05-26 13:28:38 +00001594 if ( s_pack_internal(soself, args, 0, PyBytes_AS_STRING(result)) != 0 ) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001595 Py_DECREF(result);
1596 return NULL;
1597 }
1598
1599 return result;
1600}
1601
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001602PyDoc_STRVAR(s_pack_into__doc__,
1603"S.pack_into(buffer, offset, v1, v2, ...)\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +00001604\n\
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001605Pack the values v1, v2, ... according to this Struct's format, write \n\
Thomas Wouters477c8d52006-05-27 19:21:47 +00001606the packed bytes into the writable buffer buf starting at offset. Note\n\
1607that the offset is not an optional argument. See struct.__doc__ for \n\
1608more on format strings.");
1609
1610static PyObject *
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001611s_pack_into(PyObject *self, PyObject *args)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001612{
1613 PyStructObject *soself;
1614 char *buffer;
1615 Py_ssize_t buffer_len, offset;
1616
1617 /* Validate arguments. +1 is for the first arg as buffer. */
1618 soself = (PyStructObject *)self;
1619 assert(PyStruct_Check(self));
1620 assert(soself->s_codes != NULL);
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001621 if (PyTuple_GET_SIZE(args) != (soself->s_len + 2))
Thomas Wouters477c8d52006-05-27 19:21:47 +00001622 {
1623 PyErr_Format(StructError,
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001624 "pack_into requires exactly %zd arguments",
Thomas Wouters477c8d52006-05-27 19:21:47 +00001625 (soself->s_len + 2));
1626 return NULL;
1627 }
1628
1629 /* Extract a writable memory buffer from the first argument */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001630 if ( PyObject_AsWriteBuffer(PyTuple_GET_ITEM(args, 0),
1631 (void**)&buffer, &buffer_len) == -1 ) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001632 return NULL;
1633 }
1634 assert( buffer_len >= 0 );
1635
1636 /* Extract the offset from the first argument */
Georg Brandl75c3d6f2009-02-13 11:01:07 +00001637 offset = PyNumber_AsSsize_t(PyTuple_GET_ITEM(args, 1), PyExc_IndexError);
Benjamin Petersona8a93042008-09-30 02:18:09 +00001638 if (offset == -1 && PyErr_Occurred())
1639 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001640
1641 /* Support negative offsets. */
1642 if (offset < 0)
1643 offset += buffer_len;
1644
1645 /* Check boundaries */
1646 if (offset < 0 || (buffer_len - offset) < soself->s_size) {
1647 PyErr_Format(StructError,
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001648 "pack_into requires a buffer of at least %zd bytes",
Thomas Wouters477c8d52006-05-27 19:21:47 +00001649 soself->s_size);
1650 return NULL;
1651 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001652
Thomas Wouters477c8d52006-05-27 19:21:47 +00001653 /* Call the guts */
1654 if ( s_pack_internal(soself, args, 2, buffer + offset) != 0 ) {
1655 return NULL;
1656 }
1657
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001658 Py_RETURN_NONE;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001659}
1660
1661static PyObject *
1662s_get_format(PyStructObject *self, void *unused)
1663{
1664 Py_INCREF(self->s_format);
1665 return self->s_format;
1666}
1667
1668static PyObject *
1669s_get_size(PyStructObject *self, void *unused)
1670{
Christian Heimes217cfd12007-12-02 14:31:20 +00001671 return PyLong_FromSsize_t(self->s_size);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001672}
1673
1674/* List of functions */
1675
1676static struct PyMethodDef s_methods[] = {
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001677 {"pack", s_pack, METH_VARARGS, s_pack__doc__},
1678 {"pack_into", s_pack_into, METH_VARARGS, s_pack_into__doc__},
1679 {"unpack", s_unpack, METH_O, s_unpack__doc__},
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001680 {"unpack_from", (PyCFunction)s_unpack_from, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001681 s_unpack_from__doc__},
Thomas Wouters477c8d52006-05-27 19:21:47 +00001682 {NULL, NULL} /* sentinel */
1683};
1684
1685PyDoc_STRVAR(s__doc__, "Compiled struct object");
1686
1687#define OFF(x) offsetof(PyStructObject, x)
1688
1689static PyGetSetDef s_getsetlist[] = {
1690 {"format", (getter)s_get_format, (setter)NULL, "struct format string", NULL},
1691 {"size", (getter)s_get_size, (setter)NULL, "struct size in bytes", NULL},
1692 {NULL} /* sentinel */
1693};
1694
1695static
1696PyTypeObject PyStructType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001697 PyVarObject_HEAD_INIT(NULL, 0)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001698 "Struct",
1699 sizeof(PyStructObject),
1700 0,
1701 (destructor)s_dealloc, /* tp_dealloc */
1702 0, /* tp_print */
1703 0, /* tp_getattr */
1704 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00001705 0, /* tp_reserved */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001706 0, /* tp_repr */
1707 0, /* tp_as_number */
1708 0, /* tp_as_sequence */
1709 0, /* tp_as_mapping */
1710 0, /* tp_hash */
1711 0, /* tp_call */
1712 0, /* tp_str */
1713 PyObject_GenericGetAttr, /* tp_getattro */
1714 PyObject_GenericSetAttr, /* tp_setattro */
1715 0, /* tp_as_buffer */
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00001716 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001717 s__doc__, /* tp_doc */
1718 0, /* tp_traverse */
1719 0, /* tp_clear */
1720 0, /* tp_richcompare */
1721 offsetof(PyStructObject, weakreflist), /* tp_weaklistoffset */
1722 0, /* tp_iter */
1723 0, /* tp_iternext */
1724 s_methods, /* tp_methods */
1725 NULL, /* tp_members */
1726 s_getsetlist, /* tp_getset */
1727 0, /* tp_base */
1728 0, /* tp_dict */
1729 0, /* tp_descr_get */
1730 0, /* tp_descr_set */
1731 0, /* tp_dictoffset */
1732 s_init, /* tp_init */
1733 PyType_GenericAlloc,/* tp_alloc */
1734 s_new, /* tp_new */
1735 PyObject_Del, /* tp_free */
1736};
1737
Christian Heimesa34706f2008-01-04 03:06:10 +00001738
1739/* ---- Standalone functions ---- */
1740
1741#define MAXCACHE 100
1742static PyObject *cache = NULL;
1743
1744static PyObject *
1745cache_struct(PyObject *fmt)
1746{
1747 PyObject * s_object;
1748
1749 if (cache == NULL) {
1750 cache = PyDict_New();
1751 if (cache == NULL)
1752 return NULL;
1753 }
1754
1755 s_object = PyDict_GetItem(cache, fmt);
1756 if (s_object != NULL) {
1757 Py_INCREF(s_object);
1758 return s_object;
1759 }
1760
1761 s_object = PyObject_CallFunctionObjArgs((PyObject *)(&PyStructType), fmt, NULL);
1762 if (s_object != NULL) {
1763 if (PyDict_Size(cache) >= MAXCACHE)
1764 PyDict_Clear(cache);
1765 /* Attempt to cache the result */
1766 if (PyDict_SetItem(cache, fmt, s_object) == -1)
1767 PyErr_Clear();
1768 }
1769 return s_object;
1770}
1771
1772PyDoc_STRVAR(clearcache_doc,
1773"Clear the internal cache.");
1774
1775static PyObject *
1776clearcache(PyObject *self)
1777{
Christian Heimes679db4a2008-01-18 09:56:22 +00001778 Py_CLEAR(cache);
Christian Heimesa34706f2008-01-04 03:06:10 +00001779 Py_RETURN_NONE;
1780}
1781
1782PyDoc_STRVAR(calcsize_doc,
1783"Return size of C struct described by format string fmt.");
1784
1785static PyObject *
1786calcsize(PyObject *self, PyObject *fmt)
1787{
1788 Py_ssize_t n;
1789 PyObject *s_object = cache_struct(fmt);
1790 if (s_object == NULL)
1791 return NULL;
1792 n = ((PyStructObject *)s_object)->s_size;
1793 Py_DECREF(s_object);
1794 return PyLong_FromSsize_t(n);
1795}
1796
1797PyDoc_STRVAR(pack_doc,
Benjamin Peterson4ae19462008-07-31 15:03:40 +00001798"Return bytes containing values v1, v2, ... packed according to fmt.");
Christian Heimesa34706f2008-01-04 03:06:10 +00001799
1800static PyObject *
1801pack(PyObject *self, PyObject *args)
1802{
1803 PyObject *s_object, *fmt, *newargs, *result;
1804 Py_ssize_t n = PyTuple_GET_SIZE(args);
1805
1806 if (n == 0) {
1807 PyErr_SetString(PyExc_TypeError, "missing format argument");
1808 return NULL;
1809 }
1810 fmt = PyTuple_GET_ITEM(args, 0);
1811 newargs = PyTuple_GetSlice(args, 1, n);
1812 if (newargs == NULL)
1813 return NULL;
1814
1815 s_object = cache_struct(fmt);
1816 if (s_object == NULL) {
1817 Py_DECREF(newargs);
1818 return NULL;
1819 }
1820 result = s_pack(s_object, newargs);
1821 Py_DECREF(newargs);
1822 Py_DECREF(s_object);
1823 return result;
1824}
1825
1826PyDoc_STRVAR(pack_into_doc,
1827"Pack the values v1, v2, ... according to fmt.\n\
1828Write the packed bytes into the writable buffer buf starting at offset.");
1829
1830static PyObject *
1831pack_into(PyObject *self, PyObject *args)
1832{
1833 PyObject *s_object, *fmt, *newargs, *result;
1834 Py_ssize_t n = PyTuple_GET_SIZE(args);
1835
1836 if (n == 0) {
1837 PyErr_SetString(PyExc_TypeError, "missing format argument");
1838 return NULL;
1839 }
1840 fmt = PyTuple_GET_ITEM(args, 0);
1841 newargs = PyTuple_GetSlice(args, 1, n);
1842 if (newargs == NULL)
1843 return NULL;
1844
1845 s_object = cache_struct(fmt);
1846 if (s_object == NULL) {
1847 Py_DECREF(newargs);
1848 return NULL;
1849 }
1850 result = s_pack_into(s_object, newargs);
1851 Py_DECREF(newargs);
1852 Py_DECREF(s_object);
1853 return result;
1854}
1855
1856PyDoc_STRVAR(unpack_doc,
Benjamin Peterson4ae19462008-07-31 15:03:40 +00001857"Unpack the bytes containing packed C structure data, according to fmt.\n\
1858Requires len(bytes) == calcsize(fmt).");
Christian Heimesa34706f2008-01-04 03:06:10 +00001859
1860static PyObject *
1861unpack(PyObject *self, PyObject *args)
1862{
1863 PyObject *s_object, *fmt, *inputstr, *result;
1864
1865 if (!PyArg_UnpackTuple(args, "unpack", 2, 2, &fmt, &inputstr))
1866 return NULL;
1867
1868 s_object = cache_struct(fmt);
1869 if (s_object == NULL)
1870 return NULL;
1871 result = s_unpack(s_object, inputstr);
1872 Py_DECREF(s_object);
1873 return result;
1874}
1875
1876PyDoc_STRVAR(unpack_from_doc,
1877"Unpack the buffer, containing packed C structure data, according to\n\
1878fmt, starting at offset. Requires len(buffer[offset:]) >= calcsize(fmt).");
1879
1880static PyObject *
1881unpack_from(PyObject *self, PyObject *args, PyObject *kwds)
1882{
1883 PyObject *s_object, *fmt, *newargs, *result;
1884 Py_ssize_t n = PyTuple_GET_SIZE(args);
1885
1886 if (n == 0) {
1887 PyErr_SetString(PyExc_TypeError, "missing format argument");
1888 return NULL;
1889 }
1890 fmt = PyTuple_GET_ITEM(args, 0);
1891 newargs = PyTuple_GetSlice(args, 1, n);
1892 if (newargs == NULL)
1893 return NULL;
1894
1895 s_object = cache_struct(fmt);
1896 if (s_object == NULL) {
1897 Py_DECREF(newargs);
1898 return NULL;
1899 }
1900 result = s_unpack_from(s_object, newargs, kwds);
1901 Py_DECREF(newargs);
1902 Py_DECREF(s_object);
1903 return result;
1904}
1905
1906static struct PyMethodDef module_functions[] = {
1907 {"_clearcache", (PyCFunction)clearcache, METH_NOARGS, clearcache_doc},
1908 {"calcsize", calcsize, METH_O, calcsize_doc},
1909 {"pack", pack, METH_VARARGS, pack_doc},
1910 {"pack_into", pack_into, METH_VARARGS, pack_into_doc},
1911 {"unpack", unpack, METH_VARARGS, unpack_doc},
1912 {"unpack_from", (PyCFunction)unpack_from,
1913 METH_VARARGS|METH_KEYWORDS, unpack_from_doc},
1914 {NULL, NULL} /* sentinel */
1915};
1916
1917
Thomas Wouters477c8d52006-05-27 19:21:47 +00001918/* Module initialization */
1919
Christian Heimesa34706f2008-01-04 03:06:10 +00001920PyDoc_STRVAR(module_doc,
1921"Functions to convert between Python values and C structs.\n\
Benjamin Peterson4ae19462008-07-31 15:03:40 +00001922Python bytes objects are used to hold the data representing the C struct\n\
Christian Heimesa34706f2008-01-04 03:06:10 +00001923and also as format strings to describe the layout of data in the C struct.\n\
1924\n\
1925The optional first format char indicates byte order, size and alignment:\n\
1926 @: native order, size & alignment (default)\n\
1927 =: native order, std. size & alignment\n\
1928 <: little-endian, std. size & alignment\n\
1929 >: big-endian, std. size & alignment\n\
1930 !: same as >\n\
1931\n\
1932The remaining chars indicate types of args and must match exactly;\n\
1933these can be preceded by a decimal repeat count:\n\
1934 x: pad byte (no data); c:char; b:signed byte; B:unsigned byte;\n\
1935 h:short; H:unsigned short; i:int; I:unsigned int;\n\
1936 l:long; L:unsigned long; f:float; d:double.\n\
1937Special cases (preceding decimal count indicates length):\n\
1938 s:string (array of char); p: pascal string (with count byte).\n\
1939Special case (only available in native format):\n\
1940 P:an integer type that is wide enough to hold a pointer.\n\
1941Special case (not in native mode unless 'long long' in platform C):\n\
1942 q:long long; Q:unsigned long long\n\
1943Whitespace between formats is ignored.\n\
1944\n\
1945The variable struct.error is an exception raised on errors.\n");
1946
Martin v. Löwis1a214512008-06-11 05:26:20 +00001947
1948static struct PyModuleDef _structmodule = {
1949 PyModuleDef_HEAD_INIT,
1950 "_struct",
1951 module_doc,
1952 -1,
1953 module_functions,
1954 NULL,
1955 NULL,
1956 NULL,
1957 NULL
1958};
1959
Thomas Wouters477c8d52006-05-27 19:21:47 +00001960PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001961PyInit__struct(void)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001962{
Christian Heimesa34706f2008-01-04 03:06:10 +00001963 PyObject *ver, *m;
1964
Christian Heimes72b710a2008-05-26 13:28:38 +00001965 ver = PyBytes_FromString("0.2");
Christian Heimesa34706f2008-01-04 03:06:10 +00001966 if (ver == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001967 return NULL;
Christian Heimesa34706f2008-01-04 03:06:10 +00001968
Martin v. Löwis1a214512008-06-11 05:26:20 +00001969 m = PyModule_Create(&_structmodule);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001970 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001971 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001972
Christian Heimes90aa7642007-12-19 02:45:37 +00001973 Py_TYPE(&PyStructType) = &PyType_Type;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001974 if (PyType_Ready(&PyStructType) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001975 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001976
1977 /* Check endian and swap in faster functions */
1978 {
1979 int one = 1;
1980 formatdef *native = native_table;
1981 formatdef *other, *ptr;
1982 if ((int)*(unsigned char*)&one)
1983 other = lilendian_table;
1984 else
1985 other = bigendian_table;
1986 /* Scan through the native table, find a matching
1987 entry in the endian table and swap in the
1988 native implementations whenever possible
1989 (64-bit platforms may not have "standard" sizes) */
1990 while (native->format != '\0' && other->format != '\0') {
1991 ptr = other;
1992 while (ptr->format != '\0') {
1993 if (ptr->format == native->format) {
1994 /* Match faster when formats are
1995 listed in the same order */
1996 if (ptr == other)
1997 other++;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001998 /* Only use the trick if the
Thomas Wouters477c8d52006-05-27 19:21:47 +00001999 size matches */
2000 if (ptr->size != native->size)
2001 break;
2002 /* Skip float and double, could be
2003 "unknown" float format */
2004 if (ptr->format == 'd' || ptr->format == 'f')
2005 break;
2006 ptr->pack = native->pack;
2007 ptr->unpack = native->unpack;
2008 break;
2009 }
2010 ptr++;
2011 }
2012 native++;
2013 }
2014 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002015
Thomas Wouters477c8d52006-05-27 19:21:47 +00002016 /* Add some symbolic constants to the module */
2017 if (StructError == NULL) {
2018 StructError = PyErr_NewException("struct.error", NULL, NULL);
2019 if (StructError == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00002020 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002021 }
2022
2023 Py_INCREF(StructError);
2024 PyModule_AddObject(m, "error", StructError);
2025
2026 Py_INCREF((PyObject*)&PyStructType);
2027 PyModule_AddObject(m, "Struct", (PyObject*)&PyStructType);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002028
Christian Heimesa34706f2008-01-04 03:06:10 +00002029 PyModule_AddObject(m, "__version__", ver);
2030
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002031#ifdef PY_STRUCT_FLOAT_COERCE
2032 PyModule_AddIntConstant(m, "_PY_STRUCT_FLOAT_COERCE", 1);
2033#endif
Martin v. Löwis1a214512008-06-11 05:26:20 +00002034 return m;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002035
Thomas Wouters477c8d52006-05-27 19:21:47 +00002036}