blob: 83f5685dd056b3821b9d83813c3099927c902f58 [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 Wouters477c8d52006-05-27 19:21:47 +000015/* The translation function for each format character is table driven */
16typedef struct _formatdef {
17 char format;
18 Py_ssize_t size;
19 Py_ssize_t alignment;
20 PyObject* (*unpack)(const char *,
21 const struct _formatdef *);
22 int (*pack)(char *, PyObject *,
23 const struct _formatdef *);
24} formatdef;
25
26typedef struct _formatcode {
27 const struct _formatdef *fmtdef;
28 Py_ssize_t offset;
29 Py_ssize_t size;
30} formatcode;
31
32/* Struct object interface */
33
34typedef struct {
35 PyObject_HEAD
36 Py_ssize_t s_size;
37 Py_ssize_t s_len;
38 formatcode *s_codes;
39 PyObject *s_format;
40 PyObject *weakreflist; /* List of weak references */
41} PyStructObject;
42
43
44#define PyStruct_Check(op) PyObject_TypeCheck(op, &PyStructType)
Christian Heimes90aa7642007-12-19 02:45:37 +000045#define PyStruct_CheckExact(op) (Py_TYPE(op) == &PyStructType)
Thomas Wouters477c8d52006-05-27 19:21:47 +000046
47
48/* Exception */
49
50static PyObject *StructError;
51
52
53/* Define various structs to figure out the alignments of types */
54
55
56typedef struct { char c; short x; } st_short;
57typedef struct { char c; int x; } st_int;
58typedef struct { char c; long x; } st_long;
59typedef struct { char c; float x; } st_float;
60typedef struct { char c; double x; } st_double;
61typedef struct { char c; void *x; } st_void_p;
62
63#define SHORT_ALIGN (sizeof(st_short) - sizeof(short))
64#define INT_ALIGN (sizeof(st_int) - sizeof(int))
65#define LONG_ALIGN (sizeof(st_long) - sizeof(long))
66#define FLOAT_ALIGN (sizeof(st_float) - sizeof(float))
67#define DOUBLE_ALIGN (sizeof(st_double) - sizeof(double))
68#define VOID_P_ALIGN (sizeof(st_void_p) - sizeof(void *))
69
70/* We can't support q and Q in native mode unless the compiler does;
71 in std mode, they're 8 bytes on all platforms. */
72#ifdef HAVE_LONG_LONG
73typedef struct { char c; PY_LONG_LONG x; } s_long_long;
74#define LONG_LONG_ALIGN (sizeof(s_long_long) - sizeof(PY_LONG_LONG))
75#endif
76
Thomas Woutersb2137042007-02-01 18:02:27 +000077#ifdef HAVE_C99_BOOL
78#define BOOL_TYPE _Bool
79typedef struct { char c; _Bool x; } s_bool;
80#define BOOL_ALIGN (sizeof(s_bool) - sizeof(BOOL_TYPE))
81#else
82#define BOOL_TYPE char
83#define BOOL_ALIGN 0
84#endif
85
Thomas Wouters477c8d52006-05-27 19:21:47 +000086#define STRINGIFY(x) #x
87
88#ifdef __powerc
89#pragma options align=reset
90#endif
91
Mark Dickinsonea835e72009-04-19 20:40:33 +000092/* Helper to get a PyLongObject. Caller should decref. */
Thomas Wouters477c8d52006-05-27 19:21:47 +000093
94static PyObject *
95get_pylong(PyObject *v)
96{
Thomas Wouters477c8d52006-05-27 19:21:47 +000097 assert(v != NULL);
Mark Dickinsonea835e72009-04-19 20:40:33 +000098 if (!PyLong_Check(v)) {
99 PyErr_SetString(StructError,
100 "required argument is not an integer");
101 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000102 }
Mark Dickinsonea835e72009-04-19 20:40:33 +0000103
104 Py_INCREF(v);
105 return v;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000106}
107
Mark Dickinsonea835e72009-04-19 20:40:33 +0000108/* Helper routine to get a C long and raise the appropriate error if it isn't
109 one */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000110
111static int
112get_long(PyObject *v, long *p)
113{
Mark Dickinsonea835e72009-04-19 20:40:33 +0000114 long x;
115
116 if (!PyLong_Check(v)) {
117 PyErr_SetString(StructError,
118 "required argument is not an integer");
119 return -1;
120 }
121 x = PyLong_AsLong(v);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000122 if (x == -1 && PyErr_Occurred()) {
Mark Dickinsonea835e72009-04-19 20:40:33 +0000123 if (PyErr_ExceptionMatches(PyExc_OverflowError))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000124 PyErr_SetString(StructError,
Mark Dickinsonea835e72009-04-19 20:40:33 +0000125 "argument out of range");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000126 return -1;
127 }
128 *p = x;
129 return 0;
130}
131
132
133/* Same, but handling unsigned long */
134
135static int
136get_ulong(PyObject *v, unsigned long *p)
137{
Mark Dickinsonea835e72009-04-19 20:40:33 +0000138 unsigned long x;
139
140 if (!PyLong_Check(v)) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000141 PyErr_SetString(StructError,
Mark Dickinsonea835e72009-04-19 20:40:33 +0000142 "required argument is not an integer");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000143 return -1;
144 }
Mark Dickinsonea835e72009-04-19 20:40:33 +0000145 x = PyLong_AsUnsignedLong(v);
146 if (x == (unsigned long)-1 && PyErr_Occurred()) {
147 if (PyErr_ExceptionMatches(PyExc_OverflowError))
148 PyErr_SetString(StructError,
149 "argument out of range");
150 return -1;
151 }
152 *p = x;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000153 return 0;
154}
155
156#ifdef HAVE_LONG_LONG
157
158/* Same, but handling native long long. */
159
160static int
161get_longlong(PyObject *v, PY_LONG_LONG *p)
162{
163 PY_LONG_LONG x;
Mark Dickinsonea835e72009-04-19 20:40:33 +0000164 if (!PyLong_Check(v)) {
165 PyErr_SetString(StructError,
166 "required argument is not an integer");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000167 return -1;
Mark Dickinsonea835e72009-04-19 20:40:33 +0000168 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000169 x = PyLong_AsLongLong(v);
Mark Dickinsonea835e72009-04-19 20:40:33 +0000170 if (x == -1 && PyErr_Occurred()) {
171 if (PyErr_ExceptionMatches(PyExc_OverflowError))
172 PyErr_SetString(StructError,
173 "argument out of range");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000174 return -1;
Mark Dickinsonea835e72009-04-19 20:40:33 +0000175 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000176 *p = x;
177 return 0;
178}
179
180/* Same, but handling native unsigned long long. */
181
182static int
183get_ulonglong(PyObject *v, unsigned PY_LONG_LONG *p)
184{
185 unsigned PY_LONG_LONG x;
Mark Dickinsonea835e72009-04-19 20:40:33 +0000186 if (!PyLong_Check(v)) {
187 PyErr_SetString(StructError,
188 "required argument is not an integer");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000189 return -1;
Mark Dickinsonea835e72009-04-19 20:40:33 +0000190 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000191 x = PyLong_AsUnsignedLongLong(v);
Mark Dickinsonea835e72009-04-19 20:40:33 +0000192 if (x == -1 && PyErr_Occurred()) {
193 if (PyErr_ExceptionMatches(PyExc_OverflowError))
194 PyErr_SetString(StructError,
195 "argument out of range");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000196 return -1;
Mark Dickinsonea835e72009-04-19 20:40:33 +0000197 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000198 *p = x;
199 return 0;
200}
201
202#endif
203
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000204
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000205#define RANGE_ERROR(x, f, flag, mask) return _range_error(f, flag)
206
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000207
Thomas Wouters477c8d52006-05-27 19:21:47 +0000208/* Floating point helpers */
209
210static PyObject *
211unpack_float(const char *p, /* start of 4-byte string */
212 int le) /* true for little-endian, false for big-endian */
213{
214 double x;
215
216 x = _PyFloat_Unpack4((unsigned char *)p, le);
217 if (x == -1.0 && PyErr_Occurred())
218 return NULL;
219 return PyFloat_FromDouble(x);
220}
221
222static PyObject *
223unpack_double(const char *p, /* start of 8-byte string */
224 int le) /* true for little-endian, false for big-endian */
225{
226 double x;
227
228 x = _PyFloat_Unpack8((unsigned char *)p, le);
229 if (x == -1.0 && PyErr_Occurred())
230 return NULL;
231 return PyFloat_FromDouble(x);
232}
233
234/* Helper to format the range error exceptions */
235static int
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000236_range_error(const formatdef *f, int is_unsigned)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000237{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000238 /* ulargest is the largest unsigned value with f->size bytes.
239 * Note that the simpler:
240 * ((size_t)1 << (f->size * 8)) - 1
241 * doesn't work when f->size == sizeof(size_t) because C doesn't
242 * define what happens when a left shift count is >= the number of
243 * bits in the integer being shifted; e.g., on some boxes it doesn't
244 * shift at all when they're equal.
245 */
246 const size_t ulargest = (size_t)-1 >> ((SIZEOF_SIZE_T - f->size)*8);
247 assert(f->size >= 1 && f->size <= SIZEOF_SIZE_T);
248 if (is_unsigned)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000249 PyErr_Format(StructError,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000250 "'%c' format requires 0 <= number <= %zu",
251 f->format,
252 ulargest);
253 else {
254 const Py_ssize_t largest = (Py_ssize_t)(ulargest >> 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000255 PyErr_Format(StructError,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000256 "'%c' format requires %zd <= number <= %zd",
257 f->format,
258 ~ largest,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000259 largest);
260 }
Mark Dickinsonae681df2009-03-21 10:26:31 +0000261
Thomas Wouters477c8d52006-05-27 19:21:47 +0000262 return -1;
263}
264
265
266
267/* A large number of small routines follow, with names of the form
268
269 [bln][up]_TYPE
270
271 [bln] distiguishes among big-endian, little-endian and native.
272 [pu] distiguishes between pack (to struct) and unpack (from struct).
273 TYPE is one of char, byte, ubyte, etc.
274*/
275
276/* Native mode routines. ****************************************************/
277/* NOTE:
278 In all n[up]_<type> routines handling types larger than 1 byte, there is
279 *no* guarantee that the p pointer is properly aligned for each type,
280 therefore memcpy is called. An intermediate variable is used to
281 compensate for big-endian architectures.
282 Normally both the intermediate variable and the memcpy call will be
283 skipped by C optimisation in little-endian architectures (gcc >= 2.91
284 does this). */
285
286static PyObject *
287nu_char(const char *p, const formatdef *f)
288{
Christian Heimes72b710a2008-05-26 13:28:38 +0000289 return PyBytes_FromStringAndSize(p, 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000290}
291
292static PyObject *
293nu_byte(const char *p, const formatdef *f)
294{
Christian Heimes217cfd12007-12-02 14:31:20 +0000295 return PyLong_FromLong((long) *(signed char *)p);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000296}
297
298static PyObject *
299nu_ubyte(const char *p, const formatdef *f)
300{
Christian Heimes217cfd12007-12-02 14:31:20 +0000301 return PyLong_FromLong((long) *(unsigned char *)p);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000302}
303
304static PyObject *
305nu_short(const char *p, const formatdef *f)
306{
307 short x;
308 memcpy((char *)&x, p, sizeof x);
Christian Heimes217cfd12007-12-02 14:31:20 +0000309 return PyLong_FromLong((long)x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000310}
311
312static PyObject *
313nu_ushort(const char *p, const formatdef *f)
314{
315 unsigned short x;
316 memcpy((char *)&x, p, sizeof x);
Christian Heimes217cfd12007-12-02 14:31:20 +0000317 return PyLong_FromLong((long)x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000318}
319
320static PyObject *
321nu_int(const char *p, const formatdef *f)
322{
323 int x;
324 memcpy((char *)&x, p, sizeof x);
Christian Heimes217cfd12007-12-02 14:31:20 +0000325 return PyLong_FromLong((long)x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000326}
327
328static PyObject *
329nu_uint(const char *p, const formatdef *f)
330{
331 unsigned int x;
332 memcpy((char *)&x, p, sizeof x);
333#if (SIZEOF_LONG > SIZEOF_INT)
Christian Heimes217cfd12007-12-02 14:31:20 +0000334 return PyLong_FromLong((long)x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000335#else
336 if (x <= ((unsigned int)LONG_MAX))
Christian Heimes217cfd12007-12-02 14:31:20 +0000337 return PyLong_FromLong((long)x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000338 return PyLong_FromUnsignedLong((unsigned long)x);
339#endif
340}
341
342static PyObject *
343nu_long(const char *p, const formatdef *f)
344{
345 long x;
346 memcpy((char *)&x, p, sizeof x);
Christian Heimes217cfd12007-12-02 14:31:20 +0000347 return PyLong_FromLong(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000348}
349
350static PyObject *
351nu_ulong(const char *p, const formatdef *f)
352{
353 unsigned long x;
354 memcpy((char *)&x, p, sizeof x);
355 if (x <= LONG_MAX)
Christian Heimes217cfd12007-12-02 14:31:20 +0000356 return PyLong_FromLong((long)x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000357 return PyLong_FromUnsignedLong(x);
358}
359
360/* Native mode doesn't support q or Q unless the platform C supports
361 long long (or, on Windows, __int64). */
362
363#ifdef HAVE_LONG_LONG
364
365static PyObject *
366nu_longlong(const char *p, const formatdef *f)
367{
368 PY_LONG_LONG x;
369 memcpy((char *)&x, p, sizeof x);
370 if (x >= LONG_MIN && x <= LONG_MAX)
Christian Heimes217cfd12007-12-02 14:31:20 +0000371 return PyLong_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
Thomas Wouters477c8d52006-05-27 19:21:47 +0000372 return PyLong_FromLongLong(x);
373}
374
375static PyObject *
376nu_ulonglong(const char *p, const formatdef *f)
377{
378 unsigned PY_LONG_LONG x;
379 memcpy((char *)&x, p, sizeof x);
380 if (x <= LONG_MAX)
Christian Heimes217cfd12007-12-02 14:31:20 +0000381 return PyLong_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
Thomas Wouters477c8d52006-05-27 19:21:47 +0000382 return PyLong_FromUnsignedLongLong(x);
383}
384
385#endif
386
387static PyObject *
Thomas Woutersb2137042007-02-01 18:02:27 +0000388nu_bool(const char *p, const formatdef *f)
389{
390 BOOL_TYPE x;
391 memcpy((char *)&x, p, sizeof x);
392 return PyBool_FromLong(x != 0);
393}
394
395
396static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +0000397nu_float(const char *p, const formatdef *f)
398{
399 float x;
400 memcpy((char *)&x, p, sizeof x);
401 return PyFloat_FromDouble((double)x);
402}
403
404static PyObject *
405nu_double(const char *p, const formatdef *f)
406{
407 double x;
408 memcpy((char *)&x, p, sizeof x);
409 return PyFloat_FromDouble(x);
410}
411
412static PyObject *
413nu_void_p(const char *p, const formatdef *f)
414{
415 void *x;
416 memcpy((char *)&x, p, sizeof x);
417 return PyLong_FromVoidPtr(x);
418}
419
420static int
421np_byte(char *p, PyObject *v, const formatdef *f)
422{
423 long x;
424 if (get_long(v, &x) < 0)
425 return -1;
426 if (x < -128 || x > 127){
427 PyErr_SetString(StructError,
428 "byte format requires -128 <= number <= 127");
429 return -1;
430 }
431 *p = (char)x;
432 return 0;
433}
434
435static int
436np_ubyte(char *p, PyObject *v, const formatdef *f)
437{
438 long x;
439 if (get_long(v, &x) < 0)
440 return -1;
441 if (x < 0 || x > 255){
442 PyErr_SetString(StructError,
443 "ubyte format requires 0 <= number <= 255");
444 return -1;
445 }
446 *p = (char)x;
447 return 0;
448}
449
450static int
451np_char(char *p, PyObject *v, const formatdef *f)
452{
Guido van Rossume625fd52007-05-27 09:19:04 +0000453 if (PyUnicode_Check(v)) {
454 v = _PyUnicode_AsDefaultEncodedString(v, NULL);
455 if (v == NULL)
456 return -1;
457 }
Christian Heimes72b710a2008-05-26 13:28:38 +0000458 if (!PyBytes_Check(v) || PyBytes_Size(v) != 1) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000459 PyErr_SetString(StructError,
Benjamin Peterson4ae19462008-07-31 15:03:40 +0000460 "char format requires bytes or string of length 1");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000461 return -1;
462 }
Christian Heimes72b710a2008-05-26 13:28:38 +0000463 *p = *PyBytes_AsString(v);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000464 return 0;
465}
466
467static int
468np_short(char *p, PyObject *v, const formatdef *f)
469{
470 long x;
471 short y;
472 if (get_long(v, &x) < 0)
473 return -1;
474 if (x < SHRT_MIN || x > SHRT_MAX){
475 PyErr_SetString(StructError,
476 "short format requires " STRINGIFY(SHRT_MIN)
477 " <= number <= " STRINGIFY(SHRT_MAX));
478 return -1;
479 }
480 y = (short)x;
481 memcpy(p, (char *)&y, sizeof y);
482 return 0;
483}
484
485static int
486np_ushort(char *p, PyObject *v, const formatdef *f)
487{
488 long x;
489 unsigned short y;
490 if (get_long(v, &x) < 0)
491 return -1;
492 if (x < 0 || x > USHRT_MAX){
493 PyErr_SetString(StructError,
494 "short format requires 0 <= number <= " STRINGIFY(USHRT_MAX));
495 return -1;
496 }
497 y = (unsigned short)x;
498 memcpy(p, (char *)&y, sizeof y);
499 return 0;
500}
501
502static int
503np_int(char *p, PyObject *v, const formatdef *f)
504{
505 long x;
506 int y;
507 if (get_long(v, &x) < 0)
508 return -1;
509#if (SIZEOF_LONG > SIZEOF_INT)
510 if ((x < ((long)INT_MIN)) || (x > ((long)INT_MAX)))
Georg Brandlb1441c72009-01-03 22:33:39 +0000511 RANGE_ERROR(x, f, 0, -1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000512#endif
513 y = (int)x;
514 memcpy(p, (char *)&y, sizeof y);
515 return 0;
516}
517
518static int
519np_uint(char *p, PyObject *v, const formatdef *f)
520{
521 unsigned long x;
522 unsigned int y;
Mark Dickinsonae681df2009-03-21 10:26:31 +0000523 if (get_ulong(v, &x) < 0)
Georg Brandlb1441c72009-01-03 22:33:39 +0000524 return -1;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000525 y = (unsigned int)x;
526#if (SIZEOF_LONG > SIZEOF_INT)
527 if (x > ((unsigned long)UINT_MAX))
Georg Brandlb1441c72009-01-03 22:33:39 +0000528 RANGE_ERROR(y, f, 1, -1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000529#endif
530 memcpy(p, (char *)&y, sizeof y);
531 return 0;
532}
533
534static int
535np_long(char *p, PyObject *v, const formatdef *f)
536{
537 long x;
538 if (get_long(v, &x) < 0)
539 return -1;
540 memcpy(p, (char *)&x, sizeof x);
541 return 0;
542}
543
544static int
545np_ulong(char *p, PyObject *v, const formatdef *f)
546{
547 unsigned long x;
Mark Dickinsonae681df2009-03-21 10:26:31 +0000548 if (get_ulong(v, &x) < 0)
Georg Brandlb1441c72009-01-03 22:33:39 +0000549 return -1;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000550 memcpy(p, (char *)&x, sizeof x);
551 return 0;
552}
553
554#ifdef HAVE_LONG_LONG
555
556static int
557np_longlong(char *p, PyObject *v, const formatdef *f)
558{
559 PY_LONG_LONG x;
560 if (get_longlong(v, &x) < 0)
561 return -1;
562 memcpy(p, (char *)&x, sizeof x);
563 return 0;
564}
565
566static int
567np_ulonglong(char *p, PyObject *v, const formatdef *f)
568{
569 unsigned PY_LONG_LONG x;
570 if (get_ulonglong(v, &x) < 0)
571 return -1;
572 memcpy(p, (char *)&x, sizeof x);
573 return 0;
574}
575#endif
576
Thomas Woutersb2137042007-02-01 18:02:27 +0000577
578static int
579np_bool(char *p, PyObject *v, const formatdef *f)
580{
581 BOOL_TYPE y;
582 y = PyObject_IsTrue(v);
583 memcpy(p, (char *)&y, sizeof y);
584 return 0;
585}
586
Thomas Wouters477c8d52006-05-27 19:21:47 +0000587static int
588np_float(char *p, PyObject *v, const formatdef *f)
589{
590 float x = (float)PyFloat_AsDouble(v);
591 if (x == -1 && PyErr_Occurred()) {
592 PyErr_SetString(StructError,
593 "required argument is not a float");
594 return -1;
595 }
596 memcpy(p, (char *)&x, sizeof x);
597 return 0;
598}
599
600static int
601np_double(char *p, PyObject *v, const formatdef *f)
602{
603 double x = PyFloat_AsDouble(v);
604 if (x == -1 && PyErr_Occurred()) {
605 PyErr_SetString(StructError,
606 "required argument is not a float");
607 return -1;
608 }
609 memcpy(p, (char *)&x, sizeof(double));
610 return 0;
611}
612
613static int
614np_void_p(char *p, PyObject *v, const formatdef *f)
615{
616 void *x;
617
618 v = get_pylong(v);
619 if (v == NULL)
620 return -1;
621 assert(PyLong_Check(v));
622 x = PyLong_AsVoidPtr(v);
623 Py_DECREF(v);
624 if (x == NULL && PyErr_Occurred())
625 return -1;
626 memcpy(p, (char *)&x, sizeof x);
627 return 0;
628}
629
630static formatdef native_table[] = {
631 {'x', sizeof(char), 0, NULL},
632 {'b', sizeof(char), 0, nu_byte, np_byte},
633 {'B', sizeof(char), 0, nu_ubyte, np_ubyte},
634 {'c', sizeof(char), 0, nu_char, np_char},
635 {'s', sizeof(char), 0, NULL},
636 {'p', sizeof(char), 0, NULL},
637 {'h', sizeof(short), SHORT_ALIGN, nu_short, np_short},
638 {'H', sizeof(short), SHORT_ALIGN, nu_ushort, np_ushort},
639 {'i', sizeof(int), INT_ALIGN, nu_int, np_int},
640 {'I', sizeof(int), INT_ALIGN, nu_uint, np_uint},
641 {'l', sizeof(long), LONG_ALIGN, nu_long, np_long},
642 {'L', sizeof(long), LONG_ALIGN, nu_ulong, np_ulong},
643#ifdef HAVE_LONG_LONG
644 {'q', sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_longlong, np_longlong},
645 {'Q', sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_ulonglong,np_ulonglong},
646#endif
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000647 {'?', sizeof(BOOL_TYPE), BOOL_ALIGN, nu_bool, np_bool},
Thomas Wouters477c8d52006-05-27 19:21:47 +0000648 {'f', sizeof(float), FLOAT_ALIGN, nu_float, np_float},
649 {'d', sizeof(double), DOUBLE_ALIGN, nu_double, np_double},
650 {'P', sizeof(void *), VOID_P_ALIGN, nu_void_p, np_void_p},
651 {0}
652};
653
654/* Big-endian routines. *****************************************************/
655
656static PyObject *
657bu_int(const char *p, const formatdef *f)
658{
659 long x = 0;
660 Py_ssize_t i = f->size;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000661 const unsigned char *bytes = (const unsigned char *)p;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000662 do {
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000663 x = (x<<8) | *bytes++;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000664 } while (--i > 0);
665 /* Extend the sign bit. */
666 if (SIZEOF_LONG > f->size)
667 x |= -(x & (1L << ((8 * f->size) - 1)));
Christian Heimes217cfd12007-12-02 14:31:20 +0000668 return PyLong_FromLong(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000669}
670
671static PyObject *
672bu_uint(const char *p, const formatdef *f)
673{
674 unsigned long x = 0;
675 Py_ssize_t i = f->size;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000676 const unsigned char *bytes = (const unsigned char *)p;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000677 do {
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000678 x = (x<<8) | *bytes++;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000679 } while (--i > 0);
680 if (x <= LONG_MAX)
Christian Heimes217cfd12007-12-02 14:31:20 +0000681 return PyLong_FromLong((long)x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000682 return PyLong_FromUnsignedLong(x);
683}
684
685static PyObject *
686bu_longlong(const char *p, const formatdef *f)
687{
688#ifdef HAVE_LONG_LONG
689 PY_LONG_LONG x = 0;
690 Py_ssize_t i = f->size;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000691 const unsigned char *bytes = (const unsigned char *)p;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000692 do {
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000693 x = (x<<8) | *bytes++;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000694 } while (--i > 0);
695 /* Extend the sign bit. */
696 if (SIZEOF_LONG_LONG > f->size)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000697 x |= -(x & ((PY_LONG_LONG)1 << ((8 * f->size) - 1)));
Thomas Wouters477c8d52006-05-27 19:21:47 +0000698 if (x >= LONG_MIN && x <= LONG_MAX)
Christian Heimes217cfd12007-12-02 14:31:20 +0000699 return PyLong_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
Thomas Wouters477c8d52006-05-27 19:21:47 +0000700 return PyLong_FromLongLong(x);
701#else
702 return _PyLong_FromByteArray((const unsigned char *)p,
703 8,
704 0, /* little-endian */
705 1 /* signed */);
706#endif
707}
708
709static PyObject *
710bu_ulonglong(const char *p, const formatdef *f)
711{
712#ifdef HAVE_LONG_LONG
713 unsigned PY_LONG_LONG x = 0;
714 Py_ssize_t i = f->size;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000715 const unsigned char *bytes = (const unsigned char *)p;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000716 do {
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000717 x = (x<<8) | *bytes++;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000718 } while (--i > 0);
719 if (x <= LONG_MAX)
Christian Heimes217cfd12007-12-02 14:31:20 +0000720 return PyLong_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
Thomas Wouters477c8d52006-05-27 19:21:47 +0000721 return PyLong_FromUnsignedLongLong(x);
722#else
723 return _PyLong_FromByteArray((const unsigned char *)p,
724 8,
725 0, /* little-endian */
726 0 /* signed */);
727#endif
728}
729
730static PyObject *
731bu_float(const char *p, const formatdef *f)
732{
733 return unpack_float(p, 0);
734}
735
736static PyObject *
737bu_double(const char *p, const formatdef *f)
738{
739 return unpack_double(p, 0);
740}
741
Thomas Woutersb2137042007-02-01 18:02:27 +0000742static PyObject *
743bu_bool(const char *p, const formatdef *f)
744{
745 char x;
746 memcpy((char *)&x, p, sizeof x);
747 return PyBool_FromLong(x != 0);
748}
749
Thomas Wouters477c8d52006-05-27 19:21:47 +0000750static int
751bp_int(char *p, PyObject *v, const formatdef *f)
752{
753 long x;
754 Py_ssize_t i;
Mark Dickinsonae681df2009-03-21 10:26:31 +0000755 if (get_long(v, &x) < 0)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000756 return -1;
757 i = f->size;
758 if (i != SIZEOF_LONG) {
759 if ((i == 2) && (x < -32768 || x > 32767))
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000760 RANGE_ERROR(x, f, 0, 0xffffL);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000761#if (SIZEOF_LONG != 4)
762 else if ((i == 4) && (x < -2147483648L || x > 2147483647L))
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000763 RANGE_ERROR(x, f, 0, 0xffffffffL);
764#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +0000765 }
766 do {
767 p[--i] = (char)x;
768 x >>= 8;
769 } while (i > 0);
770 return 0;
771}
772
773static int
774bp_uint(char *p, PyObject *v, const formatdef *f)
775{
776 unsigned long x;
777 Py_ssize_t i;
Mark Dickinsonae681df2009-03-21 10:26:31 +0000778 if (get_ulong(v, &x) < 0)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000779 return -1;
780 i = f->size;
781 if (i != SIZEOF_LONG) {
782 unsigned long maxint = 1;
783 maxint <<= (unsigned long)(i * 8);
784 if (x >= maxint)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000785 RANGE_ERROR(x, f, 1, maxint - 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000786 }
787 do {
788 p[--i] = (char)x;
789 x >>= 8;
790 } while (i > 0);
791 return 0;
792}
793
794static int
795bp_longlong(char *p, PyObject *v, const formatdef *f)
796{
797 int res;
798 v = get_pylong(v);
799 if (v == NULL)
800 return -1;
801 res = _PyLong_AsByteArray((PyLongObject *)v,
802 (unsigned char *)p,
803 8,
804 0, /* little_endian */
805 1 /* signed */);
806 Py_DECREF(v);
807 return res;
808}
809
810static int
811bp_ulonglong(char *p, PyObject *v, const formatdef *f)
812{
813 int res;
814 v = get_pylong(v);
815 if (v == NULL)
816 return -1;
817 res = _PyLong_AsByteArray((PyLongObject *)v,
818 (unsigned char *)p,
819 8,
820 0, /* little_endian */
821 0 /* signed */);
822 Py_DECREF(v);
823 return res;
824}
825
826static int
827bp_float(char *p, PyObject *v, const formatdef *f)
828{
829 double x = PyFloat_AsDouble(v);
830 if (x == -1 && PyErr_Occurred()) {
831 PyErr_SetString(StructError,
832 "required argument is not a float");
833 return -1;
834 }
835 return _PyFloat_Pack4(x, (unsigned char *)p, 0);
836}
837
838static int
839bp_double(char *p, PyObject *v, const formatdef *f)
840{
841 double x = PyFloat_AsDouble(v);
842 if (x == -1 && PyErr_Occurred()) {
843 PyErr_SetString(StructError,
844 "required argument is not a float");
845 return -1;
846 }
847 return _PyFloat_Pack8(x, (unsigned char *)p, 0);
848}
849
Thomas Woutersb2137042007-02-01 18:02:27 +0000850static int
851bp_bool(char *p, PyObject *v, const formatdef *f)
852{
853 char y;
854 y = PyObject_IsTrue(v);
855 memcpy(p, (char *)&y, sizeof y);
856 return 0;
857}
858
Thomas Wouters477c8d52006-05-27 19:21:47 +0000859static formatdef bigendian_table[] = {
860 {'x', 1, 0, NULL},
861 {'b', 1, 0, nu_byte, np_byte},
862 {'B', 1, 0, nu_ubyte, np_ubyte},
863 {'c', 1, 0, nu_char, np_char},
864 {'s', 1, 0, NULL},
865 {'p', 1, 0, NULL},
866 {'h', 2, 0, bu_int, bp_int},
867 {'H', 2, 0, bu_uint, bp_uint},
868 {'i', 4, 0, bu_int, bp_int},
869 {'I', 4, 0, bu_uint, bp_uint},
870 {'l', 4, 0, bu_int, bp_int},
871 {'L', 4, 0, bu_uint, bp_uint},
872 {'q', 8, 0, bu_longlong, bp_longlong},
873 {'Q', 8, 0, bu_ulonglong, bp_ulonglong},
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000874 {'?', 1, 0, bu_bool, bp_bool},
Thomas Wouters477c8d52006-05-27 19:21:47 +0000875 {'f', 4, 0, bu_float, bp_float},
876 {'d', 8, 0, bu_double, bp_double},
877 {0}
878};
879
880/* Little-endian routines. *****************************************************/
881
882static PyObject *
883lu_int(const char *p, const formatdef *f)
884{
885 long x = 0;
886 Py_ssize_t i = f->size;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000887 const unsigned char *bytes = (const unsigned char *)p;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000888 do {
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000889 x = (x<<8) | bytes[--i];
Thomas Wouters477c8d52006-05-27 19:21:47 +0000890 } while (i > 0);
891 /* Extend the sign bit. */
892 if (SIZEOF_LONG > f->size)
893 x |= -(x & (1L << ((8 * f->size) - 1)));
Christian Heimes217cfd12007-12-02 14:31:20 +0000894 return PyLong_FromLong(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000895}
896
897static PyObject *
898lu_uint(const char *p, const formatdef *f)
899{
900 unsigned long x = 0;
901 Py_ssize_t i = f->size;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000902 const unsigned char *bytes = (const unsigned char *)p;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000903 do {
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000904 x = (x<<8) | bytes[--i];
Thomas Wouters477c8d52006-05-27 19:21:47 +0000905 } while (i > 0);
906 if (x <= LONG_MAX)
Christian Heimes217cfd12007-12-02 14:31:20 +0000907 return PyLong_FromLong((long)x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000908 return PyLong_FromUnsignedLong((long)x);
909}
910
911static PyObject *
912lu_longlong(const char *p, const formatdef *f)
913{
914#ifdef HAVE_LONG_LONG
915 PY_LONG_LONG x = 0;
916 Py_ssize_t i = f->size;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000917 const unsigned char *bytes = (const unsigned char *)p;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000918 do {
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000919 x = (x<<8) | bytes[--i];
Thomas Wouters477c8d52006-05-27 19:21:47 +0000920 } while (i > 0);
921 /* Extend the sign bit. */
922 if (SIZEOF_LONG_LONG > f->size)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000923 x |= -(x & ((PY_LONG_LONG)1 << ((8 * f->size) - 1)));
Thomas Wouters477c8d52006-05-27 19:21:47 +0000924 if (x >= LONG_MIN && x <= LONG_MAX)
Christian Heimes217cfd12007-12-02 14:31:20 +0000925 return PyLong_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
Thomas Wouters477c8d52006-05-27 19:21:47 +0000926 return PyLong_FromLongLong(x);
927#else
928 return _PyLong_FromByteArray((const unsigned char *)p,
929 8,
930 1, /* little-endian */
931 1 /* signed */);
932#endif
933}
934
935static PyObject *
936lu_ulonglong(const char *p, const formatdef *f)
937{
938#ifdef HAVE_LONG_LONG
939 unsigned PY_LONG_LONG x = 0;
940 Py_ssize_t i = f->size;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000941 const unsigned char *bytes = (const unsigned char *)p;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000942 do {
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000943 x = (x<<8) | bytes[--i];
Thomas Wouters477c8d52006-05-27 19:21:47 +0000944 } while (i > 0);
945 if (x <= LONG_MAX)
Christian Heimes217cfd12007-12-02 14:31:20 +0000946 return PyLong_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
Thomas Wouters477c8d52006-05-27 19:21:47 +0000947 return PyLong_FromUnsignedLongLong(x);
948#else
949 return _PyLong_FromByteArray((const unsigned char *)p,
950 8,
951 1, /* little-endian */
952 0 /* signed */);
953#endif
954}
955
956static PyObject *
957lu_float(const char *p, const formatdef *f)
958{
959 return unpack_float(p, 1);
960}
961
962static PyObject *
963lu_double(const char *p, const formatdef *f)
964{
965 return unpack_double(p, 1);
966}
967
968static int
969lp_int(char *p, PyObject *v, const formatdef *f)
970{
971 long x;
972 Py_ssize_t i;
Mark Dickinsonae681df2009-03-21 10:26:31 +0000973 if (get_long(v, &x) < 0)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000974 return -1;
975 i = f->size;
976 if (i != SIZEOF_LONG) {
977 if ((i == 2) && (x < -32768 || x > 32767))
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000978 RANGE_ERROR(x, f, 0, 0xffffL);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000979#if (SIZEOF_LONG != 4)
980 else if ((i == 4) && (x < -2147483648L || x > 2147483647L))
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000981 RANGE_ERROR(x, f, 0, 0xffffffffL);
982#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +0000983 }
984 do {
985 *p++ = (char)x;
986 x >>= 8;
987 } while (--i > 0);
988 return 0;
989}
990
991static int
992lp_uint(char *p, PyObject *v, const formatdef *f)
993{
994 unsigned long x;
995 Py_ssize_t i;
Mark Dickinsonae681df2009-03-21 10:26:31 +0000996 if (get_ulong(v, &x) < 0)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000997 return -1;
998 i = f->size;
999 if (i != SIZEOF_LONG) {
1000 unsigned long maxint = 1;
1001 maxint <<= (unsigned long)(i * 8);
1002 if (x >= maxint)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001003 RANGE_ERROR(x, f, 1, maxint - 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001004 }
1005 do {
1006 *p++ = (char)x;
1007 x >>= 8;
1008 } while (--i > 0);
1009 return 0;
1010}
1011
1012static int
1013lp_longlong(char *p, PyObject *v, const formatdef *f)
1014{
1015 int res;
1016 v = get_pylong(v);
1017 if (v == NULL)
1018 return -1;
1019 res = _PyLong_AsByteArray((PyLongObject*)v,
1020 (unsigned char *)p,
1021 8,
1022 1, /* little_endian */
1023 1 /* signed */);
1024 Py_DECREF(v);
1025 return res;
1026}
1027
1028static int
1029lp_ulonglong(char *p, PyObject *v, const formatdef *f)
1030{
1031 int res;
1032 v = get_pylong(v);
1033 if (v == NULL)
1034 return -1;
1035 res = _PyLong_AsByteArray((PyLongObject*)v,
1036 (unsigned char *)p,
1037 8,
1038 1, /* little_endian */
1039 0 /* signed */);
1040 Py_DECREF(v);
1041 return res;
1042}
1043
1044static int
1045lp_float(char *p, PyObject *v, const formatdef *f)
1046{
1047 double x = PyFloat_AsDouble(v);
1048 if (x == -1 && PyErr_Occurred()) {
1049 PyErr_SetString(StructError,
1050 "required argument is not a float");
1051 return -1;
1052 }
1053 return _PyFloat_Pack4(x, (unsigned char *)p, 1);
1054}
1055
1056static int
1057lp_double(char *p, PyObject *v, const formatdef *f)
1058{
1059 double x = PyFloat_AsDouble(v);
1060 if (x == -1 && PyErr_Occurred()) {
1061 PyErr_SetString(StructError,
1062 "required argument is not a float");
1063 return -1;
1064 }
1065 return _PyFloat_Pack8(x, (unsigned char *)p, 1);
1066}
1067
1068static formatdef lilendian_table[] = {
1069 {'x', 1, 0, NULL},
1070 {'b', 1, 0, nu_byte, np_byte},
1071 {'B', 1, 0, nu_ubyte, np_ubyte},
1072 {'c', 1, 0, nu_char, np_char},
1073 {'s', 1, 0, NULL},
1074 {'p', 1, 0, NULL},
1075 {'h', 2, 0, lu_int, lp_int},
1076 {'H', 2, 0, lu_uint, lp_uint},
1077 {'i', 4, 0, lu_int, lp_int},
1078 {'I', 4, 0, lu_uint, lp_uint},
1079 {'l', 4, 0, lu_int, lp_int},
1080 {'L', 4, 0, lu_uint, lp_uint},
1081 {'q', 8, 0, lu_longlong, lp_longlong},
1082 {'Q', 8, 0, lu_ulonglong, lp_ulonglong},
Christian Heimesdd15f6c2008-03-16 00:07:10 +00001083 {'?', 1, 0, bu_bool, bp_bool}, /* Std rep not endian dep,
Thomas Woutersb2137042007-02-01 18:02:27 +00001084 but potentially different from native rep -- reuse bx_bool funcs. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001085 {'f', 4, 0, lu_float, lp_float},
1086 {'d', 8, 0, lu_double, lp_double},
1087 {0}
1088};
1089
1090
1091static const formatdef *
1092whichtable(char **pfmt)
1093{
1094 const char *fmt = (*pfmt)++; /* May be backed out of later */
1095 switch (*fmt) {
1096 case '<':
1097 return lilendian_table;
1098 case '>':
1099 case '!': /* Network byte order is big-endian */
1100 return bigendian_table;
1101 case '=': { /* Host byte order -- different from native in aligment! */
1102 int n = 1;
1103 char *p = (char *) &n;
1104 if (*p == 1)
1105 return lilendian_table;
1106 else
1107 return bigendian_table;
1108 }
1109 default:
1110 --*pfmt; /* Back out of pointer increment */
1111 /* Fall through */
1112 case '@':
1113 return native_table;
1114 }
1115}
1116
1117
1118/* Get the table entry for a format code */
1119
1120static const formatdef *
1121getentry(int c, const formatdef *f)
1122{
1123 for (; f->format != '\0'; f++) {
1124 if (f->format == c) {
1125 return f;
1126 }
1127 }
1128 PyErr_SetString(StructError, "bad char in struct format");
1129 return NULL;
1130}
1131
1132
1133/* Align a size according to a format code */
1134
1135static int
1136align(Py_ssize_t size, char c, const formatdef *e)
1137{
1138 if (e->format == c) {
1139 if (e->alignment) {
1140 size = ((size + e->alignment - 1)
1141 / e->alignment)
1142 * e->alignment;
1143 }
1144 }
1145 return size;
1146}
1147
1148
1149/* calculate the size of a format string */
1150
1151static int
1152prepare_s(PyStructObject *self)
1153{
1154 const formatdef *f;
1155 const formatdef *e;
1156 formatcode *codes;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001157
Thomas Wouters477c8d52006-05-27 19:21:47 +00001158 const char *s;
1159 const char *fmt;
1160 char c;
1161 Py_ssize_t size, len, num, itemsize, x;
1162
Christian Heimes72b710a2008-05-26 13:28:38 +00001163 fmt = PyBytes_AS_STRING(self->s_format);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001164
1165 f = whichtable((char **)&fmt);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001166
Thomas Wouters477c8d52006-05-27 19:21:47 +00001167 s = fmt;
1168 size = 0;
1169 len = 0;
1170 while ((c = *s++) != '\0') {
1171 if (isspace(Py_CHARMASK(c)))
1172 continue;
1173 if ('0' <= c && c <= '9') {
1174 num = c - '0';
1175 while ('0' <= (c = *s++) && c <= '9') {
1176 x = num*10 + (c - '0');
1177 if (x/10 != num) {
1178 PyErr_SetString(
1179 StructError,
1180 "overflow in item count");
1181 return -1;
1182 }
1183 num = x;
1184 }
1185 if (c == '\0')
1186 break;
1187 }
1188 else
1189 num = 1;
1190
1191 e = getentry(c, f);
1192 if (e == NULL)
1193 return -1;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001194
Thomas Wouters477c8d52006-05-27 19:21:47 +00001195 switch (c) {
1196 case 's': /* fall through */
1197 case 'p': len++; break;
1198 case 'x': break;
1199 default: len += num; break;
1200 }
1201
1202 itemsize = e->size;
1203 size = align(size, c, e);
1204 x = num * itemsize;
1205 size += x;
1206 if (x/itemsize != num || size < 0) {
1207 PyErr_SetString(StructError,
1208 "total struct size too long");
1209 return -1;
1210 }
1211 }
1212
Amaury Forgeot d'Arc35c86582008-06-17 21:11:29 +00001213 /* check for overflow */
1214 if ((len + 1) > (PY_SSIZE_T_MAX / sizeof(formatcode))) {
1215 PyErr_NoMemory();
1216 return -1;
1217 }
1218
Thomas Wouters477c8d52006-05-27 19:21:47 +00001219 self->s_size = size;
1220 self->s_len = len;
1221 codes = PyMem_MALLOC((len + 1) * sizeof(formatcode));
1222 if (codes == NULL) {
1223 PyErr_NoMemory();
1224 return -1;
1225 }
1226 self->s_codes = codes;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001227
Thomas Wouters477c8d52006-05-27 19:21:47 +00001228 s = fmt;
1229 size = 0;
1230 while ((c = *s++) != '\0') {
1231 if (isspace(Py_CHARMASK(c)))
1232 continue;
1233 if ('0' <= c && c <= '9') {
1234 num = c - '0';
1235 while ('0' <= (c = *s++) && c <= '9')
1236 num = num*10 + (c - '0');
1237 if (c == '\0')
1238 break;
1239 }
1240 else
1241 num = 1;
1242
1243 e = getentry(c, f);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001244
Thomas Wouters477c8d52006-05-27 19:21:47 +00001245 size = align(size, c, e);
1246 if (c == 's' || c == 'p') {
1247 codes->offset = size;
1248 codes->size = num;
1249 codes->fmtdef = e;
1250 codes++;
1251 size += num;
1252 } else if (c == 'x') {
1253 size += num;
1254 } else {
1255 while (--num >= 0) {
1256 codes->offset = size;
1257 codes->size = e->size;
1258 codes->fmtdef = e;
1259 codes++;
1260 size += e->size;
1261 }
1262 }
1263 }
1264 codes->fmtdef = NULL;
1265 codes->offset = size;
1266 codes->size = 0;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001267
Thomas Wouters477c8d52006-05-27 19:21:47 +00001268 return 0;
1269}
1270
1271static PyObject *
1272s_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1273{
1274 PyObject *self;
1275
1276 assert(type != NULL && type->tp_alloc != NULL);
1277
1278 self = type->tp_alloc(type, 0);
1279 if (self != NULL) {
1280 PyStructObject *s = (PyStructObject*)self;
1281 Py_INCREF(Py_None);
1282 s->s_format = Py_None;
1283 s->s_codes = NULL;
1284 s->s_size = -1;
1285 s->s_len = -1;
1286 }
1287 return self;
1288}
1289
1290static int
1291s_init(PyObject *self, PyObject *args, PyObject *kwds)
1292{
1293 PyStructObject *soself = (PyStructObject *)self;
1294 PyObject *o_format = NULL;
1295 int ret = 0;
1296 static char *kwlist[] = {"format", 0};
1297
1298 assert(PyStruct_Check(self));
1299
Christian Heimesa34706f2008-01-04 03:06:10 +00001300 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:Struct", kwlist,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001301 &o_format))
1302 return -1;
1303
Christian Heimesa34706f2008-01-04 03:06:10 +00001304 if (PyUnicode_Check(o_format)) {
1305 o_format = PyUnicode_AsASCIIString(o_format);
1306 if (o_format == NULL)
1307 return -1;
1308 }
1309 /* XXX support buffer interface, too */
1310 else {
1311 Py_INCREF(o_format);
1312 }
1313
Christian Heimes72b710a2008-05-26 13:28:38 +00001314 if (!PyBytes_Check(o_format)) {
Christian Heimesa34706f2008-01-04 03:06:10 +00001315 Py_DECREF(o_format);
1316 PyErr_Format(PyExc_TypeError,
1317 "Struct() argument 1 must be bytes, not %.200s",
1318 Py_TYPE(o_format)->tp_name);
1319 return -1;
1320 }
1321
Christian Heimes18c66892008-02-17 13:31:39 +00001322 Py_CLEAR(soself->s_format);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001323 soself->s_format = o_format;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001324
Thomas Wouters477c8d52006-05-27 19:21:47 +00001325 ret = prepare_s(soself);
1326 return ret;
1327}
1328
1329static void
1330s_dealloc(PyStructObject *s)
1331{
1332 if (s->weakreflist != NULL)
1333 PyObject_ClearWeakRefs((PyObject *)s);
1334 if (s->s_codes != NULL) {
1335 PyMem_FREE(s->s_codes);
1336 }
1337 Py_XDECREF(s->s_format);
Christian Heimes90aa7642007-12-19 02:45:37 +00001338 Py_TYPE(s)->tp_free((PyObject *)s);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001339}
1340
1341static PyObject *
1342s_unpack_internal(PyStructObject *soself, char *startfrom) {
1343 formatcode *code;
1344 Py_ssize_t i = 0;
1345 PyObject *result = PyTuple_New(soself->s_len);
1346 if (result == NULL)
1347 return NULL;
1348
1349 for (code = soself->s_codes; code->fmtdef != NULL; code++) {
1350 PyObject *v;
1351 const formatdef *e = code->fmtdef;
1352 const char *res = startfrom + code->offset;
1353 if (e->format == 's') {
Christian Heimes72b710a2008-05-26 13:28:38 +00001354 v = PyBytes_FromStringAndSize(res, code->size);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001355 } else if (e->format == 'p') {
1356 Py_ssize_t n = *(unsigned char*)res;
1357 if (n >= code->size)
1358 n = code->size - 1;
Christian Heimes72b710a2008-05-26 13:28:38 +00001359 v = PyBytes_FromStringAndSize(res + 1, n);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001360 } else {
1361 v = e->unpack(res, e);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001362 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001363 if (v == NULL)
1364 goto fail;
1365 PyTuple_SET_ITEM(result, i++, v);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001366 }
1367
1368 return result;
1369fail:
1370 Py_DECREF(result);
1371 return NULL;
1372}
1373
1374
1375PyDoc_STRVAR(s_unpack__doc__,
Guido van Rossum913dd0b2007-04-13 03:33:53 +00001376"S.unpack(buffer) -> (v1, v2, ...)\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +00001377\n\
1378Return tuple containing values unpacked according to this Struct's format.\n\
Guido van Rossum913dd0b2007-04-13 03:33:53 +00001379Requires len(buffer) == self.size. See struct.__doc__ for more on format\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +00001380strings.");
1381
1382static PyObject *
Guido van Rossum98297ee2007-11-06 21:34:58 +00001383s_unpack(PyObject *self, PyObject *input)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001384{
Guido van Rossum98297ee2007-11-06 21:34:58 +00001385 Py_buffer vbuf;
1386 PyObject *result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001387 PyStructObject *soself = (PyStructObject *)self;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001388
Thomas Wouters477c8d52006-05-27 19:21:47 +00001389 assert(PyStruct_Check(self));
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001390 assert(soself->s_codes != NULL);
Guido van Rossum98297ee2007-11-06 21:34:58 +00001391 if (PyObject_GetBuffer(input, &vbuf, PyBUF_SIMPLE) < 0)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001392 return NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001393 if (vbuf.len != soself->s_size) {
1394 PyErr_Format(StructError,
1395 "unpack requires a bytes argument of length %zd",
1396 soself->s_size);
Martin v. Löwis423be952008-08-13 15:53:07 +00001397 PyBuffer_Release(&vbuf);
Guido van Rossum98297ee2007-11-06 21:34:58 +00001398 return NULL;
1399 }
1400 result = s_unpack_internal(soself, vbuf.buf);
Martin v. Löwis423be952008-08-13 15:53:07 +00001401 PyBuffer_Release(&vbuf);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001402 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001403}
1404
1405PyDoc_STRVAR(s_unpack_from__doc__,
1406"S.unpack_from(buffer[, offset]) -> (v1, v2, ...)\n\
1407\n\
1408Return tuple containing values unpacked according to this Struct's format.\n\
1409Unlike unpack, unpack_from can unpack values from any object supporting\n\
1410the buffer API, not just str. Requires len(buffer[offset:]) >= self.size.\n\
1411See struct.__doc__ for more on format strings.");
1412
1413static PyObject *
1414s_unpack_from(PyObject *self, PyObject *args, PyObject *kwds)
1415{
1416 static char *kwlist[] = {"buffer", "offset", 0};
Guido van Rossum98297ee2007-11-06 21:34:58 +00001417
1418 PyObject *input;
1419 Py_ssize_t offset = 0;
1420 Py_buffer vbuf;
1421 PyObject *result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001422 PyStructObject *soself = (PyStructObject *)self;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001423
Thomas Wouters477c8d52006-05-27 19:21:47 +00001424 assert(PyStruct_Check(self));
1425 assert(soself->s_codes != NULL);
1426
Guido van Rossum98297ee2007-11-06 21:34:58 +00001427 if (!PyArg_ParseTupleAndKeywords(args, kwds,
1428 "O|n:unpack_from", kwlist,
1429 &input, &offset))
Thomas Wouters477c8d52006-05-27 19:21:47 +00001430 return NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001431 if (PyObject_GetBuffer(input, &vbuf, PyBUF_SIMPLE) < 0)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001432 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001433 if (offset < 0)
Guido van Rossum98297ee2007-11-06 21:34:58 +00001434 offset += vbuf.len;
1435 if (offset < 0 || vbuf.len - offset < soself->s_size) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001436 PyErr_Format(StructError,
1437 "unpack_from requires a buffer of at least %zd bytes",
1438 soself->s_size);
Martin v. Löwis423be952008-08-13 15:53:07 +00001439 PyBuffer_Release(&vbuf);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001440 return NULL;
1441 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001442 result = s_unpack_internal(soself, (char*)vbuf.buf + offset);
Martin v. Löwis423be952008-08-13 15:53:07 +00001443 PyBuffer_Release(&vbuf);
Guido van Rossum98297ee2007-11-06 21:34:58 +00001444 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001445}
1446
1447
1448/*
1449 * Guts of the pack function.
1450 *
1451 * Takes a struct object, a tuple of arguments, and offset in that tuple of
1452 * argument for where to start processing the arguments for packing, and a
1453 * character buffer for writing the packed string. The caller must insure
1454 * that the buffer may contain the required length for packing the arguments.
1455 * 0 is returned on success, 1 is returned if there is an error.
1456 *
1457 */
1458static int
1459s_pack_internal(PyStructObject *soself, PyObject *args, int offset, char* buf)
1460{
1461 formatcode *code;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001462 /* XXX(nnorwitz): why does i need to be a local? can we use
1463 the offset parameter or do we need the wider width? */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001464 Py_ssize_t i;
1465
1466 memset(buf, '\0', soself->s_size);
1467 i = offset;
1468 for (code = soself->s_codes; code->fmtdef != NULL; code++) {
1469 Py_ssize_t n;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001470 PyObject *v = PyTuple_GET_ITEM(args, i++);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001471 const formatdef *e = code->fmtdef;
1472 char *res = buf + code->offset;
1473 if (e->format == 's') {
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001474 int isstring;
1475 void *p;
1476 if (PyUnicode_Check(v)) {
1477 v = _PyUnicode_AsDefaultEncodedString(v, NULL);
1478 if (v == NULL)
1479 return -1;
1480 }
Christian Heimes72b710a2008-05-26 13:28:38 +00001481 isstring = PyBytes_Check(v);
Christian Heimes9c4756e2008-05-26 13:22:05 +00001482 if (!isstring && !PyByteArray_Check(v)) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001483 PyErr_SetString(StructError,
Benjamin Peterson4ae19462008-07-31 15:03:40 +00001484 "argument for 's' must be a bytes or string");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001485 return -1;
1486 }
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001487 if (isstring) {
Christian Heimes72b710a2008-05-26 13:28:38 +00001488 n = PyBytes_GET_SIZE(v);
1489 p = PyBytes_AS_STRING(v);
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001490 }
1491 else {
Christian Heimes9c4756e2008-05-26 13:22:05 +00001492 n = PyByteArray_GET_SIZE(v);
1493 p = PyByteArray_AS_STRING(v);
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001494 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001495 if (n > code->size)
1496 n = code->size;
1497 if (n > 0)
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001498 memcpy(res, p, n);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001499 } else if (e->format == 'p') {
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001500 int isstring;
1501 void *p;
1502 if (PyUnicode_Check(v)) {
1503 v = _PyUnicode_AsDefaultEncodedString(v, NULL);
1504 if (v == NULL)
1505 return -1;
1506 }
Christian Heimes72b710a2008-05-26 13:28:38 +00001507 isstring = PyBytes_Check(v);
Christian Heimes9c4756e2008-05-26 13:22:05 +00001508 if (!isstring && !PyByteArray_Check(v)) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001509 PyErr_SetString(StructError,
Benjamin Peterson4ae19462008-07-31 15:03:40 +00001510 "argument for 'p' must be a bytes or string");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001511 return -1;
1512 }
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001513 if (isstring) {
Christian Heimes72b710a2008-05-26 13:28:38 +00001514 n = PyBytes_GET_SIZE(v);
1515 p = PyBytes_AS_STRING(v);
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001516 }
1517 else {
Christian Heimes9c4756e2008-05-26 13:22:05 +00001518 n = PyByteArray_GET_SIZE(v);
1519 p = PyByteArray_AS_STRING(v);
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001520 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001521 if (n > (code->size - 1))
1522 n = code->size - 1;
1523 if (n > 0)
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001524 memcpy(res + 1, p, n);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001525 if (n > 255)
1526 n = 255;
1527 *res = Py_SAFE_DOWNCAST(n, Py_ssize_t, unsigned char);
1528 } else {
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001529 if (e->pack(res, v, e) < 0) {
1530 if (PyLong_Check(v) && PyErr_ExceptionMatches(PyExc_OverflowError))
1531 PyErr_SetString(StructError,
1532 "long too large to convert to int");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001533 return -1;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001534 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001535 }
1536 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001537
Thomas Wouters477c8d52006-05-27 19:21:47 +00001538 /* Success */
1539 return 0;
1540}
1541
1542
1543PyDoc_STRVAR(s_pack__doc__,
Benjamin Peterson4ae19462008-07-31 15:03:40 +00001544"S.pack(v1, v2, ...) -> bytes\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +00001545\n\
Benjamin Peterson4ae19462008-07-31 15:03:40 +00001546Return a bytes containing values v1, v2, ... packed according to this\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +00001547Struct's format. See struct.__doc__ for more on format strings.");
1548
1549static PyObject *
1550s_pack(PyObject *self, PyObject *args)
1551{
1552 PyStructObject *soself;
1553 PyObject *result;
1554
1555 /* Validate arguments. */
1556 soself = (PyStructObject *)self;
1557 assert(PyStruct_Check(self));
1558 assert(soself->s_codes != NULL);
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001559 if (PyTuple_GET_SIZE(args) != soself->s_len)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001560 {
1561 PyErr_Format(StructError,
1562 "pack requires exactly %zd arguments", soself->s_len);
1563 return NULL;
1564 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001565
Thomas Wouters477c8d52006-05-27 19:21:47 +00001566 /* Allocate a new string */
Christian Heimes72b710a2008-05-26 13:28:38 +00001567 result = PyBytes_FromStringAndSize((char *)NULL, soself->s_size);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001568 if (result == NULL)
1569 return NULL;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001570
Thomas Wouters477c8d52006-05-27 19:21:47 +00001571 /* Call the guts */
Christian Heimes72b710a2008-05-26 13:28:38 +00001572 if ( s_pack_internal(soself, args, 0, PyBytes_AS_STRING(result)) != 0 ) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001573 Py_DECREF(result);
1574 return NULL;
1575 }
1576
1577 return result;
1578}
1579
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001580PyDoc_STRVAR(s_pack_into__doc__,
1581"S.pack_into(buffer, offset, v1, v2, ...)\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +00001582\n\
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001583Pack the values v1, v2, ... according to this Struct's format, write \n\
Thomas Wouters477c8d52006-05-27 19:21:47 +00001584the packed bytes into the writable buffer buf starting at offset. Note\n\
1585that the offset is not an optional argument. See struct.__doc__ for \n\
1586more on format strings.");
1587
1588static PyObject *
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001589s_pack_into(PyObject *self, PyObject *args)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001590{
1591 PyStructObject *soself;
1592 char *buffer;
1593 Py_ssize_t buffer_len, offset;
1594
1595 /* Validate arguments. +1 is for the first arg as buffer. */
1596 soself = (PyStructObject *)self;
1597 assert(PyStruct_Check(self));
1598 assert(soself->s_codes != NULL);
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001599 if (PyTuple_GET_SIZE(args) != (soself->s_len + 2))
Thomas Wouters477c8d52006-05-27 19:21:47 +00001600 {
1601 PyErr_Format(StructError,
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001602 "pack_into requires exactly %zd arguments",
Thomas Wouters477c8d52006-05-27 19:21:47 +00001603 (soself->s_len + 2));
1604 return NULL;
1605 }
1606
1607 /* Extract a writable memory buffer from the first argument */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001608 if ( PyObject_AsWriteBuffer(PyTuple_GET_ITEM(args, 0),
1609 (void**)&buffer, &buffer_len) == -1 ) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001610 return NULL;
1611 }
1612 assert( buffer_len >= 0 );
1613
1614 /* Extract the offset from the first argument */
Georg Brandl75c3d6f2009-02-13 11:01:07 +00001615 offset = PyNumber_AsSsize_t(PyTuple_GET_ITEM(args, 1), PyExc_IndexError);
Benjamin Petersona8a93042008-09-30 02:18:09 +00001616 if (offset == -1 && PyErr_Occurred())
1617 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001618
1619 /* Support negative offsets. */
1620 if (offset < 0)
1621 offset += buffer_len;
1622
1623 /* Check boundaries */
1624 if (offset < 0 || (buffer_len - offset) < soself->s_size) {
1625 PyErr_Format(StructError,
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001626 "pack_into requires a buffer of at least %zd bytes",
Thomas Wouters477c8d52006-05-27 19:21:47 +00001627 soself->s_size);
1628 return NULL;
1629 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001630
Thomas Wouters477c8d52006-05-27 19:21:47 +00001631 /* Call the guts */
1632 if ( s_pack_internal(soself, args, 2, buffer + offset) != 0 ) {
1633 return NULL;
1634 }
1635
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001636 Py_RETURN_NONE;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001637}
1638
1639static PyObject *
1640s_get_format(PyStructObject *self, void *unused)
1641{
1642 Py_INCREF(self->s_format);
1643 return self->s_format;
1644}
1645
1646static PyObject *
1647s_get_size(PyStructObject *self, void *unused)
1648{
Christian Heimes217cfd12007-12-02 14:31:20 +00001649 return PyLong_FromSsize_t(self->s_size);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001650}
1651
1652/* List of functions */
1653
1654static struct PyMethodDef s_methods[] = {
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001655 {"pack", s_pack, METH_VARARGS, s_pack__doc__},
1656 {"pack_into", s_pack_into, METH_VARARGS, s_pack_into__doc__},
1657 {"unpack", s_unpack, METH_O, s_unpack__doc__},
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001658 {"unpack_from", (PyCFunction)s_unpack_from, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001659 s_unpack_from__doc__},
Thomas Wouters477c8d52006-05-27 19:21:47 +00001660 {NULL, NULL} /* sentinel */
1661};
1662
1663PyDoc_STRVAR(s__doc__, "Compiled struct object");
1664
1665#define OFF(x) offsetof(PyStructObject, x)
1666
1667static PyGetSetDef s_getsetlist[] = {
1668 {"format", (getter)s_get_format, (setter)NULL, "struct format string", NULL},
1669 {"size", (getter)s_get_size, (setter)NULL, "struct size in bytes", NULL},
1670 {NULL} /* sentinel */
1671};
1672
1673static
1674PyTypeObject PyStructType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001675 PyVarObject_HEAD_INIT(NULL, 0)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001676 "Struct",
1677 sizeof(PyStructObject),
1678 0,
1679 (destructor)s_dealloc, /* tp_dealloc */
1680 0, /* tp_print */
1681 0, /* tp_getattr */
1682 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00001683 0, /* tp_reserved */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001684 0, /* tp_repr */
1685 0, /* tp_as_number */
1686 0, /* tp_as_sequence */
1687 0, /* tp_as_mapping */
1688 0, /* tp_hash */
1689 0, /* tp_call */
1690 0, /* tp_str */
1691 PyObject_GenericGetAttr, /* tp_getattro */
1692 PyObject_GenericSetAttr, /* tp_setattro */
1693 0, /* tp_as_buffer */
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00001694 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001695 s__doc__, /* tp_doc */
1696 0, /* tp_traverse */
1697 0, /* tp_clear */
1698 0, /* tp_richcompare */
1699 offsetof(PyStructObject, weakreflist), /* tp_weaklistoffset */
1700 0, /* tp_iter */
1701 0, /* tp_iternext */
1702 s_methods, /* tp_methods */
1703 NULL, /* tp_members */
1704 s_getsetlist, /* tp_getset */
1705 0, /* tp_base */
1706 0, /* tp_dict */
1707 0, /* tp_descr_get */
1708 0, /* tp_descr_set */
1709 0, /* tp_dictoffset */
1710 s_init, /* tp_init */
1711 PyType_GenericAlloc,/* tp_alloc */
1712 s_new, /* tp_new */
1713 PyObject_Del, /* tp_free */
1714};
1715
Christian Heimesa34706f2008-01-04 03:06:10 +00001716
1717/* ---- Standalone functions ---- */
1718
1719#define MAXCACHE 100
1720static PyObject *cache = NULL;
1721
1722static PyObject *
1723cache_struct(PyObject *fmt)
1724{
1725 PyObject * s_object;
1726
1727 if (cache == NULL) {
1728 cache = PyDict_New();
1729 if (cache == NULL)
1730 return NULL;
1731 }
1732
1733 s_object = PyDict_GetItem(cache, fmt);
1734 if (s_object != NULL) {
1735 Py_INCREF(s_object);
1736 return s_object;
1737 }
1738
1739 s_object = PyObject_CallFunctionObjArgs((PyObject *)(&PyStructType), fmt, NULL);
1740 if (s_object != NULL) {
1741 if (PyDict_Size(cache) >= MAXCACHE)
1742 PyDict_Clear(cache);
1743 /* Attempt to cache the result */
1744 if (PyDict_SetItem(cache, fmt, s_object) == -1)
1745 PyErr_Clear();
1746 }
1747 return s_object;
1748}
1749
1750PyDoc_STRVAR(clearcache_doc,
1751"Clear the internal cache.");
1752
1753static PyObject *
1754clearcache(PyObject *self)
1755{
Christian Heimes679db4a2008-01-18 09:56:22 +00001756 Py_CLEAR(cache);
Christian Heimesa34706f2008-01-04 03:06:10 +00001757 Py_RETURN_NONE;
1758}
1759
1760PyDoc_STRVAR(calcsize_doc,
1761"Return size of C struct described by format string fmt.");
1762
1763static PyObject *
1764calcsize(PyObject *self, PyObject *fmt)
1765{
1766 Py_ssize_t n;
1767 PyObject *s_object = cache_struct(fmt);
1768 if (s_object == NULL)
1769 return NULL;
1770 n = ((PyStructObject *)s_object)->s_size;
1771 Py_DECREF(s_object);
1772 return PyLong_FromSsize_t(n);
1773}
1774
1775PyDoc_STRVAR(pack_doc,
Benjamin Peterson4ae19462008-07-31 15:03:40 +00001776"Return bytes containing values v1, v2, ... packed according to fmt.");
Christian Heimesa34706f2008-01-04 03:06:10 +00001777
1778static PyObject *
1779pack(PyObject *self, PyObject *args)
1780{
1781 PyObject *s_object, *fmt, *newargs, *result;
1782 Py_ssize_t n = PyTuple_GET_SIZE(args);
1783
1784 if (n == 0) {
1785 PyErr_SetString(PyExc_TypeError, "missing format argument");
1786 return NULL;
1787 }
1788 fmt = PyTuple_GET_ITEM(args, 0);
1789 newargs = PyTuple_GetSlice(args, 1, n);
1790 if (newargs == NULL)
1791 return NULL;
1792
1793 s_object = cache_struct(fmt);
1794 if (s_object == NULL) {
1795 Py_DECREF(newargs);
1796 return NULL;
1797 }
1798 result = s_pack(s_object, newargs);
1799 Py_DECREF(newargs);
1800 Py_DECREF(s_object);
1801 return result;
1802}
1803
1804PyDoc_STRVAR(pack_into_doc,
1805"Pack the values v1, v2, ... according to fmt.\n\
1806Write the packed bytes into the writable buffer buf starting at offset.");
1807
1808static PyObject *
1809pack_into(PyObject *self, PyObject *args)
1810{
1811 PyObject *s_object, *fmt, *newargs, *result;
1812 Py_ssize_t n = PyTuple_GET_SIZE(args);
1813
1814 if (n == 0) {
1815 PyErr_SetString(PyExc_TypeError, "missing format argument");
1816 return NULL;
1817 }
1818 fmt = PyTuple_GET_ITEM(args, 0);
1819 newargs = PyTuple_GetSlice(args, 1, n);
1820 if (newargs == NULL)
1821 return NULL;
1822
1823 s_object = cache_struct(fmt);
1824 if (s_object == NULL) {
1825 Py_DECREF(newargs);
1826 return NULL;
1827 }
1828 result = s_pack_into(s_object, newargs);
1829 Py_DECREF(newargs);
1830 Py_DECREF(s_object);
1831 return result;
1832}
1833
1834PyDoc_STRVAR(unpack_doc,
Benjamin Peterson4ae19462008-07-31 15:03:40 +00001835"Unpack the bytes containing packed C structure data, according to fmt.\n\
1836Requires len(bytes) == calcsize(fmt).");
Christian Heimesa34706f2008-01-04 03:06:10 +00001837
1838static PyObject *
1839unpack(PyObject *self, PyObject *args)
1840{
1841 PyObject *s_object, *fmt, *inputstr, *result;
1842
1843 if (!PyArg_UnpackTuple(args, "unpack", 2, 2, &fmt, &inputstr))
1844 return NULL;
1845
1846 s_object = cache_struct(fmt);
1847 if (s_object == NULL)
1848 return NULL;
1849 result = s_unpack(s_object, inputstr);
1850 Py_DECREF(s_object);
1851 return result;
1852}
1853
1854PyDoc_STRVAR(unpack_from_doc,
1855"Unpack the buffer, containing packed C structure data, according to\n\
1856fmt, starting at offset. Requires len(buffer[offset:]) >= calcsize(fmt).");
1857
1858static PyObject *
1859unpack_from(PyObject *self, PyObject *args, PyObject *kwds)
1860{
1861 PyObject *s_object, *fmt, *newargs, *result;
1862 Py_ssize_t n = PyTuple_GET_SIZE(args);
1863
1864 if (n == 0) {
1865 PyErr_SetString(PyExc_TypeError, "missing format argument");
1866 return NULL;
1867 }
1868 fmt = PyTuple_GET_ITEM(args, 0);
1869 newargs = PyTuple_GetSlice(args, 1, n);
1870 if (newargs == NULL)
1871 return NULL;
1872
1873 s_object = cache_struct(fmt);
1874 if (s_object == NULL) {
1875 Py_DECREF(newargs);
1876 return NULL;
1877 }
1878 result = s_unpack_from(s_object, newargs, kwds);
1879 Py_DECREF(newargs);
1880 Py_DECREF(s_object);
1881 return result;
1882}
1883
1884static struct PyMethodDef module_functions[] = {
1885 {"_clearcache", (PyCFunction)clearcache, METH_NOARGS, clearcache_doc},
1886 {"calcsize", calcsize, METH_O, calcsize_doc},
1887 {"pack", pack, METH_VARARGS, pack_doc},
1888 {"pack_into", pack_into, METH_VARARGS, pack_into_doc},
1889 {"unpack", unpack, METH_VARARGS, unpack_doc},
1890 {"unpack_from", (PyCFunction)unpack_from,
1891 METH_VARARGS|METH_KEYWORDS, unpack_from_doc},
1892 {NULL, NULL} /* sentinel */
1893};
1894
1895
Thomas Wouters477c8d52006-05-27 19:21:47 +00001896/* Module initialization */
1897
Christian Heimesa34706f2008-01-04 03:06:10 +00001898PyDoc_STRVAR(module_doc,
1899"Functions to convert between Python values and C structs.\n\
Benjamin Peterson4ae19462008-07-31 15:03:40 +00001900Python bytes objects are used to hold the data representing the C struct\n\
Christian Heimesa34706f2008-01-04 03:06:10 +00001901and also as format strings to describe the layout of data in the C struct.\n\
1902\n\
1903The optional first format char indicates byte order, size and alignment:\n\
1904 @: native order, size & alignment (default)\n\
1905 =: native order, std. size & alignment\n\
1906 <: little-endian, std. size & alignment\n\
1907 >: big-endian, std. size & alignment\n\
1908 !: same as >\n\
1909\n\
1910The remaining chars indicate types of args and must match exactly;\n\
1911these can be preceded by a decimal repeat count:\n\
1912 x: pad byte (no data); c:char; b:signed byte; B:unsigned byte;\n\
1913 h:short; H:unsigned short; i:int; I:unsigned int;\n\
1914 l:long; L:unsigned long; f:float; d:double.\n\
1915Special cases (preceding decimal count indicates length):\n\
1916 s:string (array of char); p: pascal string (with count byte).\n\
1917Special case (only available in native format):\n\
1918 P:an integer type that is wide enough to hold a pointer.\n\
1919Special case (not in native mode unless 'long long' in platform C):\n\
1920 q:long long; Q:unsigned long long\n\
1921Whitespace between formats is ignored.\n\
1922\n\
1923The variable struct.error is an exception raised on errors.\n");
1924
Martin v. Löwis1a214512008-06-11 05:26:20 +00001925
1926static struct PyModuleDef _structmodule = {
1927 PyModuleDef_HEAD_INIT,
1928 "_struct",
1929 module_doc,
1930 -1,
1931 module_functions,
1932 NULL,
1933 NULL,
1934 NULL,
1935 NULL
1936};
1937
Thomas Wouters477c8d52006-05-27 19:21:47 +00001938PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001939PyInit__struct(void)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001940{
Christian Heimesa34706f2008-01-04 03:06:10 +00001941 PyObject *ver, *m;
1942
Mark Dickinsonea835e72009-04-19 20:40:33 +00001943 ver = PyBytes_FromString("0.3");
Christian Heimesa34706f2008-01-04 03:06:10 +00001944 if (ver == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001945 return NULL;
Christian Heimesa34706f2008-01-04 03:06:10 +00001946
Martin v. Löwis1a214512008-06-11 05:26:20 +00001947 m = PyModule_Create(&_structmodule);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001948 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001949 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001950
Christian Heimes90aa7642007-12-19 02:45:37 +00001951 Py_TYPE(&PyStructType) = &PyType_Type;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001952 if (PyType_Ready(&PyStructType) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001953 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001954
1955 /* Check endian and swap in faster functions */
1956 {
1957 int one = 1;
1958 formatdef *native = native_table;
1959 formatdef *other, *ptr;
1960 if ((int)*(unsigned char*)&one)
1961 other = lilendian_table;
1962 else
1963 other = bigendian_table;
1964 /* Scan through the native table, find a matching
1965 entry in the endian table and swap in the
1966 native implementations whenever possible
1967 (64-bit platforms may not have "standard" sizes) */
1968 while (native->format != '\0' && other->format != '\0') {
1969 ptr = other;
1970 while (ptr->format != '\0') {
1971 if (ptr->format == native->format) {
1972 /* Match faster when formats are
1973 listed in the same order */
1974 if (ptr == other)
1975 other++;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001976 /* Only use the trick if the
Thomas Wouters477c8d52006-05-27 19:21:47 +00001977 size matches */
1978 if (ptr->size != native->size)
1979 break;
1980 /* Skip float and double, could be
1981 "unknown" float format */
1982 if (ptr->format == 'd' || ptr->format == 'f')
1983 break;
1984 ptr->pack = native->pack;
1985 ptr->unpack = native->unpack;
1986 break;
1987 }
1988 ptr++;
1989 }
1990 native++;
1991 }
1992 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001993
Thomas Wouters477c8d52006-05-27 19:21:47 +00001994 /* Add some symbolic constants to the module */
1995 if (StructError == NULL) {
1996 StructError = PyErr_NewException("struct.error", NULL, NULL);
1997 if (StructError == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001998 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001999 }
2000
2001 Py_INCREF(StructError);
2002 PyModule_AddObject(m, "error", StructError);
2003
2004 Py_INCREF((PyObject*)&PyStructType);
2005 PyModule_AddObject(m, "Struct", (PyObject*)&PyStructType);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002006
Christian Heimesa34706f2008-01-04 03:06:10 +00002007 PyModule_AddObject(m, "__version__", ver);
2008
Martin v. Löwis1a214512008-06-11 05:26:20 +00002009 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002010}