blob: f85d5622d7b5009af96f979976fdc9fd30d6d06c [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 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000017 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 *);
Thomas Wouters477c8d52006-05-27 19:21:47 +000024} formatdef;
25
26typedef struct _formatcode {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000027 const struct _formatdef *fmtdef;
28 Py_ssize_t offset;
29 Py_ssize_t size;
Thomas Wouters477c8d52006-05-27 19:21:47 +000030} formatcode;
31
32/* Struct object interface */
33
34typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000035 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 */
Thomas Wouters477c8d52006-05-27 19:21:47 +000041} 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 Dickinson055a3fb2010-04-03 15:26:31 +000092/* Helper for integer format codes: converts an arbitrary Python object to a
93 PyLongObject if possible, otherwise fails. Caller should decref. */
Thomas Wouters477c8d52006-05-27 19:21:47 +000094
95static PyObject *
96get_pylong(PyObject *v)
97{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000098 assert(v != NULL);
99 if (!PyLong_Check(v)) {
100 /* Not an integer; try to use __index__ to convert. */
101 if (PyIndex_Check(v)) {
102 v = PyNumber_Index(v);
103 if (v == NULL)
104 return NULL;
105 }
106 else {
107 PyErr_SetString(StructError,
108 "required argument is not an integer");
109 return NULL;
110 }
111 }
112 else
113 Py_INCREF(v);
Mark Dickinsonea835e72009-04-19 20:40:33 +0000114
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 assert(PyLong_Check(v));
116 return v;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000117}
118
Mark Dickinsonea835e72009-04-19 20:40:33 +0000119/* Helper routine to get a C long and raise the appropriate error if it isn't
120 one */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000121
122static int
123get_long(PyObject *v, long *p)
124{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000125 long x;
Mark Dickinsonea835e72009-04-19 20:40:33 +0000126
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 v = get_pylong(v);
128 if (v == NULL)
129 return -1;
130 assert(PyLong_Check(v));
131 x = PyLong_AsLong(v);
132 Py_DECREF(v);
133 if (x == (long)-1 && PyErr_Occurred()) {
134 if (PyErr_ExceptionMatches(PyExc_OverflowError))
135 PyErr_SetString(StructError,
136 "argument out of range");
137 return -1;
138 }
139 *p = x;
140 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000141}
142
143
144/* Same, but handling unsigned long */
145
146static int
147get_ulong(PyObject *v, unsigned long *p)
148{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000149 unsigned long x;
Mark Dickinsonea835e72009-04-19 20:40:33 +0000150
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000151 v = get_pylong(v);
152 if (v == NULL)
153 return -1;
154 assert(PyLong_Check(v));
155 x = PyLong_AsUnsignedLong(v);
156 Py_DECREF(v);
157 if (x == (unsigned long)-1 && PyErr_Occurred()) {
158 if (PyErr_ExceptionMatches(PyExc_OverflowError))
159 PyErr_SetString(StructError,
160 "argument out of range");
161 return -1;
162 }
163 *p = x;
164 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000165}
166
167#ifdef HAVE_LONG_LONG
168
169/* Same, but handling native long long. */
170
171static int
172get_longlong(PyObject *v, PY_LONG_LONG *p)
173{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000174 PY_LONG_LONG x;
Mark Dickinson055a3fb2010-04-03 15:26:31 +0000175
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000176 v = get_pylong(v);
177 if (v == NULL)
178 return -1;
179 assert(PyLong_Check(v));
180 x = PyLong_AsLongLong(v);
181 Py_DECREF(v);
182 if (x == (PY_LONG_LONG)-1 && PyErr_Occurred()) {
183 if (PyErr_ExceptionMatches(PyExc_OverflowError))
184 PyErr_SetString(StructError,
185 "argument out of range");
186 return -1;
187 }
188 *p = x;
189 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000190}
191
192/* Same, but handling native unsigned long long. */
193
194static int
195get_ulonglong(PyObject *v, unsigned PY_LONG_LONG *p)
196{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000197 unsigned PY_LONG_LONG x;
Mark Dickinson055a3fb2010-04-03 15:26:31 +0000198
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000199 v = get_pylong(v);
200 if (v == NULL)
201 return -1;
202 assert(PyLong_Check(v));
203 x = PyLong_AsUnsignedLongLong(v);
204 Py_DECREF(v);
205 if (x == (unsigned PY_LONG_LONG)-1 && PyErr_Occurred()) {
206 if (PyErr_ExceptionMatches(PyExc_OverflowError))
207 PyErr_SetString(StructError,
208 "argument out of range");
209 return -1;
210 }
211 *p = x;
212 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000213}
214
215#endif
216
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000217
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000218#define RANGE_ERROR(x, f, flag, mask) return _range_error(f, flag)
219
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000220
Thomas Wouters477c8d52006-05-27 19:21:47 +0000221/* Floating point helpers */
222
223static PyObject *
224unpack_float(const char *p, /* start of 4-byte string */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000225 int le) /* true for little-endian, false for big-endian */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000226{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000227 double x;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000228
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000229 x = _PyFloat_Unpack4((unsigned char *)p, le);
230 if (x == -1.0 && PyErr_Occurred())
231 return NULL;
232 return PyFloat_FromDouble(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000233}
234
235static PyObject *
236unpack_double(const char *p, /* start of 8-byte string */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000237 int le) /* true for little-endian, false for big-endian */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000238{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000239 double x;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000240
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000241 x = _PyFloat_Unpack8((unsigned char *)p, le);
242 if (x == -1.0 && PyErr_Occurred())
243 return NULL;
244 return PyFloat_FromDouble(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000245}
246
247/* Helper to format the range error exceptions */
248static int
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000249_range_error(const formatdef *f, int is_unsigned)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000250{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000251 /* ulargest is the largest unsigned value with f->size bytes.
252 * Note that the simpler:
253 * ((size_t)1 << (f->size * 8)) - 1
254 * doesn't work when f->size == sizeof(size_t) because C doesn't
255 * define what happens when a left shift count is >= the number of
256 * bits in the integer being shifted; e.g., on some boxes it doesn't
257 * shift at all when they're equal.
258 */
259 const size_t ulargest = (size_t)-1 >> ((SIZEOF_SIZE_T - f->size)*8);
260 assert(f->size >= 1 && f->size <= SIZEOF_SIZE_T);
261 if (is_unsigned)
262 PyErr_Format(StructError,
263 "'%c' format requires 0 <= number <= %zu",
264 f->format,
265 ulargest);
266 else {
267 const Py_ssize_t largest = (Py_ssize_t)(ulargest >> 1);
268 PyErr_Format(StructError,
269 "'%c' format requires %zd <= number <= %zd",
270 f->format,
271 ~ largest,
272 largest);
273 }
Mark Dickinsonae681df2009-03-21 10:26:31 +0000274
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000275 return -1;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000276}
277
278
279
280/* A large number of small routines follow, with names of the form
281
282 [bln][up]_TYPE
283
284 [bln] distiguishes among big-endian, little-endian and native.
285 [pu] distiguishes between pack (to struct) and unpack (from struct).
286 TYPE is one of char, byte, ubyte, etc.
287*/
288
289/* Native mode routines. ****************************************************/
290/* NOTE:
291 In all n[up]_<type> routines handling types larger than 1 byte, there is
292 *no* guarantee that the p pointer is properly aligned for each type,
293 therefore memcpy is called. An intermediate variable is used to
294 compensate for big-endian architectures.
295 Normally both the intermediate variable and the memcpy call will be
296 skipped by C optimisation in little-endian architectures (gcc >= 2.91
297 does this). */
298
299static PyObject *
300nu_char(const char *p, const formatdef *f)
301{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 return PyBytes_FromStringAndSize(p, 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000303}
304
305static PyObject *
306nu_byte(const char *p, const formatdef *f)
307{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000308 return PyLong_FromLong((long) *(signed char *)p);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000309}
310
311static PyObject *
312nu_ubyte(const char *p, const formatdef *f)
313{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000314 return PyLong_FromLong((long) *(unsigned char *)p);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000315}
316
317static PyObject *
318nu_short(const char *p, const formatdef *f)
319{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000320 short x;
321 memcpy((char *)&x, p, sizeof x);
322 return PyLong_FromLong((long)x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000323}
324
325static PyObject *
326nu_ushort(const char *p, const formatdef *f)
327{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000328 unsigned short x;
329 memcpy((char *)&x, p, sizeof x);
330 return PyLong_FromLong((long)x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000331}
332
333static PyObject *
334nu_int(const char *p, const formatdef *f)
335{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000336 int x;
337 memcpy((char *)&x, p, sizeof x);
338 return PyLong_FromLong((long)x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000339}
340
341static PyObject *
342nu_uint(const char *p, const formatdef *f)
343{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000344 unsigned int x;
345 memcpy((char *)&x, p, sizeof x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000346#if (SIZEOF_LONG > SIZEOF_INT)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347 return PyLong_FromLong((long)x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000348#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000349 if (x <= ((unsigned int)LONG_MAX))
350 return PyLong_FromLong((long)x);
351 return PyLong_FromUnsignedLong((unsigned long)x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000352#endif
353}
354
355static PyObject *
356nu_long(const char *p, const formatdef *f)
357{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000358 long x;
359 memcpy((char *)&x, p, sizeof x);
360 return PyLong_FromLong(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000361}
362
363static PyObject *
364nu_ulong(const char *p, const formatdef *f)
365{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 unsigned long x;
367 memcpy((char *)&x, p, sizeof x);
368 if (x <= LONG_MAX)
369 return PyLong_FromLong((long)x);
370 return PyLong_FromUnsignedLong(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000371}
372
373/* Native mode doesn't support q or Q unless the platform C supports
374 long long (or, on Windows, __int64). */
375
376#ifdef HAVE_LONG_LONG
377
378static PyObject *
379nu_longlong(const char *p, const formatdef *f)
380{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000381 PY_LONG_LONG x;
382 memcpy((char *)&x, p, sizeof x);
383 if (x >= LONG_MIN && x <= LONG_MAX)
384 return PyLong_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
385 return PyLong_FromLongLong(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000386}
387
388static PyObject *
389nu_ulonglong(const char *p, const formatdef *f)
390{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000391 unsigned PY_LONG_LONG x;
392 memcpy((char *)&x, p, sizeof x);
393 if (x <= LONG_MAX)
394 return PyLong_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
395 return PyLong_FromUnsignedLongLong(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000396}
397
398#endif
399
400static PyObject *
Thomas Woutersb2137042007-02-01 18:02:27 +0000401nu_bool(const char *p, const formatdef *f)
402{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000403 BOOL_TYPE x;
404 memcpy((char *)&x, p, sizeof x);
405 return PyBool_FromLong(x != 0);
Thomas Woutersb2137042007-02-01 18:02:27 +0000406}
407
408
409static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +0000410nu_float(const char *p, const formatdef *f)
411{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000412 float x;
413 memcpy((char *)&x, p, sizeof x);
414 return PyFloat_FromDouble((double)x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000415}
416
417static PyObject *
418nu_double(const char *p, const formatdef *f)
419{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000420 double x;
421 memcpy((char *)&x, p, sizeof x);
422 return PyFloat_FromDouble(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000423}
424
425static PyObject *
426nu_void_p(const char *p, const formatdef *f)
427{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000428 void *x;
429 memcpy((char *)&x, p, sizeof x);
430 return PyLong_FromVoidPtr(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000431}
432
433static int
434np_byte(char *p, PyObject *v, const formatdef *f)
435{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000436 long x;
437 if (get_long(v, &x) < 0)
438 return -1;
439 if (x < -128 || x > 127){
440 PyErr_SetString(StructError,
441 "byte format requires -128 <= number <= 127");
442 return -1;
443 }
444 *p = (char)x;
445 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000446}
447
448static int
449np_ubyte(char *p, PyObject *v, const formatdef *f)
450{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 long x;
452 if (get_long(v, &x) < 0)
453 return -1;
454 if (x < 0 || x > 255){
455 PyErr_SetString(StructError,
456 "ubyte format requires 0 <= number <= 255");
457 return -1;
458 }
459 *p = (char)x;
460 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000461}
462
463static int
464np_char(char *p, PyObject *v, const formatdef *f)
465{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000466 if (PyUnicode_Check(v)) {
467 v = _PyUnicode_AsDefaultEncodedString(v, NULL);
468 if (v == NULL)
469 return -1;
470 }
471 if (!PyBytes_Check(v) || PyBytes_Size(v) != 1) {
472 PyErr_SetString(StructError,
473 "char format requires bytes or string of length 1");
474 return -1;
475 }
476 *p = *PyBytes_AsString(v);
477 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000478}
479
480static int
481np_short(char *p, PyObject *v, const formatdef *f)
482{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000483 long x;
484 short y;
485 if (get_long(v, &x) < 0)
486 return -1;
487 if (x < SHRT_MIN || x > SHRT_MAX){
488 PyErr_SetString(StructError,
489 "short format requires " STRINGIFY(SHRT_MIN)
490 " <= number <= " STRINGIFY(SHRT_MAX));
491 return -1;
492 }
493 y = (short)x;
494 memcpy(p, (char *)&y, sizeof y);
495 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000496}
497
498static int
499np_ushort(char *p, PyObject *v, const formatdef *f)
500{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 long x;
502 unsigned short y;
503 if (get_long(v, &x) < 0)
504 return -1;
505 if (x < 0 || x > USHRT_MAX){
506 PyErr_SetString(StructError,
507 "ushort format requires 0 <= number <= " STRINGIFY(USHRT_MAX));
508 return -1;
509 }
510 y = (unsigned short)x;
511 memcpy(p, (char *)&y, sizeof y);
512 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000513}
514
515static int
516np_int(char *p, PyObject *v, const formatdef *f)
517{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000518 long x;
519 int y;
520 if (get_long(v, &x) < 0)
521 return -1;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000522#if (SIZEOF_LONG > SIZEOF_INT)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 if ((x < ((long)INT_MIN)) || (x > ((long)INT_MAX)))
524 RANGE_ERROR(x, f, 0, -1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000525#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000526 y = (int)x;
527 memcpy(p, (char *)&y, sizeof y);
528 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000529}
530
531static int
532np_uint(char *p, PyObject *v, const formatdef *f)
533{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000534 unsigned long x;
535 unsigned int y;
536 if (get_ulong(v, &x) < 0)
537 return -1;
538 y = (unsigned int)x;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000539#if (SIZEOF_LONG > SIZEOF_INT)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 if (x > ((unsigned long)UINT_MAX))
541 RANGE_ERROR(y, f, 1, -1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000542#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000543 memcpy(p, (char *)&y, sizeof y);
544 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000545}
546
547static int
548np_long(char *p, PyObject *v, const formatdef *f)
549{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000550 long x;
551 if (get_long(v, &x) < 0)
552 return -1;
553 memcpy(p, (char *)&x, sizeof x);
554 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000555}
556
557static int
558np_ulong(char *p, PyObject *v, const formatdef *f)
559{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 unsigned long x;
561 if (get_ulong(v, &x) < 0)
562 return -1;
563 memcpy(p, (char *)&x, sizeof x);
564 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000565}
566
567#ifdef HAVE_LONG_LONG
568
569static int
570np_longlong(char *p, PyObject *v, const formatdef *f)
571{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000572 PY_LONG_LONG x;
573 if (get_longlong(v, &x) < 0)
574 return -1;
575 memcpy(p, (char *)&x, sizeof x);
576 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000577}
578
579static int
580np_ulonglong(char *p, PyObject *v, const formatdef *f)
581{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000582 unsigned PY_LONG_LONG x;
583 if (get_ulonglong(v, &x) < 0)
584 return -1;
585 memcpy(p, (char *)&x, sizeof x);
586 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000587}
588#endif
589
Thomas Woutersb2137042007-02-01 18:02:27 +0000590
591static int
592np_bool(char *p, PyObject *v, const formatdef *f)
593{
Benjamin Petersonde73c452010-07-07 18:54:59 +0000594 int y;
595 BOOL_TYPE x;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000596 y = PyObject_IsTrue(v);
Benjamin Petersonde73c452010-07-07 18:54:59 +0000597 if (y < 0)
598 return -1;
599 x = y;
600 memcpy(p, (char *)&x, sizeof x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000601 return 0;
Thomas Woutersb2137042007-02-01 18:02:27 +0000602}
603
Thomas Wouters477c8d52006-05-27 19:21:47 +0000604static int
605np_float(char *p, PyObject *v, const formatdef *f)
606{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000607 float x = (float)PyFloat_AsDouble(v);
608 if (x == -1 && PyErr_Occurred()) {
609 PyErr_SetString(StructError,
610 "required argument is not a float");
611 return -1;
612 }
613 memcpy(p, (char *)&x, sizeof x);
614 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000615}
616
617static int
618np_double(char *p, PyObject *v, const formatdef *f)
619{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000620 double x = PyFloat_AsDouble(v);
621 if (x == -1 && PyErr_Occurred()) {
622 PyErr_SetString(StructError,
623 "required argument is not a float");
624 return -1;
625 }
626 memcpy(p, (char *)&x, sizeof(double));
627 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000628}
629
630static int
631np_void_p(char *p, PyObject *v, const formatdef *f)
632{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000633 void *x;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000634
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000635 v = get_pylong(v);
636 if (v == NULL)
637 return -1;
638 assert(PyLong_Check(v));
639 x = PyLong_AsVoidPtr(v);
640 Py_DECREF(v);
641 if (x == NULL && PyErr_Occurred())
642 return -1;
643 memcpy(p, (char *)&x, sizeof x);
644 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000645}
646
647static formatdef native_table[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000648 {'x', sizeof(char), 0, NULL},
649 {'b', sizeof(char), 0, nu_byte, np_byte},
650 {'B', sizeof(char), 0, nu_ubyte, np_ubyte},
651 {'c', sizeof(char), 0, nu_char, np_char},
652 {'s', sizeof(char), 0, NULL},
653 {'p', sizeof(char), 0, NULL},
654 {'h', sizeof(short), SHORT_ALIGN, nu_short, np_short},
655 {'H', sizeof(short), SHORT_ALIGN, nu_ushort, np_ushort},
656 {'i', sizeof(int), INT_ALIGN, nu_int, np_int},
657 {'I', sizeof(int), INT_ALIGN, nu_uint, np_uint},
658 {'l', sizeof(long), LONG_ALIGN, nu_long, np_long},
659 {'L', sizeof(long), LONG_ALIGN, nu_ulong, np_ulong},
Thomas Wouters477c8d52006-05-27 19:21:47 +0000660#ifdef HAVE_LONG_LONG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000661 {'q', sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_longlong, np_longlong},
662 {'Q', sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_ulonglong,np_ulonglong},
Thomas Wouters477c8d52006-05-27 19:21:47 +0000663#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000664 {'?', sizeof(BOOL_TYPE), BOOL_ALIGN, nu_bool, np_bool},
665 {'f', sizeof(float), FLOAT_ALIGN, nu_float, np_float},
666 {'d', sizeof(double), DOUBLE_ALIGN, nu_double, np_double},
667 {'P', sizeof(void *), VOID_P_ALIGN, nu_void_p, np_void_p},
668 {0}
Thomas Wouters477c8d52006-05-27 19:21:47 +0000669};
670
671/* Big-endian routines. *****************************************************/
672
673static PyObject *
674bu_int(const char *p, const formatdef *f)
675{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000676 long x = 0;
677 Py_ssize_t i = f->size;
678 const unsigned char *bytes = (const unsigned char *)p;
679 do {
680 x = (x<<8) | *bytes++;
681 } while (--i > 0);
682 /* Extend the sign bit. */
683 if (SIZEOF_LONG > f->size)
684 x |= -(x & (1L << ((8 * f->size) - 1)));
685 return PyLong_FromLong(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000686}
687
688static PyObject *
689bu_uint(const char *p, const formatdef *f)
690{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000691 unsigned long x = 0;
692 Py_ssize_t i = f->size;
693 const unsigned char *bytes = (const unsigned char *)p;
694 do {
695 x = (x<<8) | *bytes++;
696 } while (--i > 0);
697 if (x <= LONG_MAX)
698 return PyLong_FromLong((long)x);
699 return PyLong_FromUnsignedLong(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000700}
701
702static PyObject *
703bu_longlong(const char *p, const formatdef *f)
704{
705#ifdef HAVE_LONG_LONG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 PY_LONG_LONG x = 0;
707 Py_ssize_t i = f->size;
708 const unsigned char *bytes = (const unsigned char *)p;
709 do {
710 x = (x<<8) | *bytes++;
711 } while (--i > 0);
712 /* Extend the sign bit. */
713 if (SIZEOF_LONG_LONG > f->size)
714 x |= -(x & ((PY_LONG_LONG)1 << ((8 * f->size) - 1)));
715 if (x >= LONG_MIN && x <= LONG_MAX)
716 return PyLong_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
717 return PyLong_FromLongLong(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000718#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 return _PyLong_FromByteArray((const unsigned char *)p,
720 8,
721 0, /* little-endian */
722 1 /* signed */);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000723#endif
724}
725
726static PyObject *
727bu_ulonglong(const char *p, const formatdef *f)
728{
729#ifdef HAVE_LONG_LONG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000730 unsigned PY_LONG_LONG x = 0;
731 Py_ssize_t i = f->size;
732 const unsigned char *bytes = (const unsigned char *)p;
733 do {
734 x = (x<<8) | *bytes++;
735 } while (--i > 0);
736 if (x <= LONG_MAX)
737 return PyLong_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
738 return PyLong_FromUnsignedLongLong(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000739#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000740 return _PyLong_FromByteArray((const unsigned char *)p,
741 8,
742 0, /* little-endian */
743 0 /* signed */);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000744#endif
745}
746
747static PyObject *
748bu_float(const char *p, const formatdef *f)
749{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000750 return unpack_float(p, 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000751}
752
753static PyObject *
754bu_double(const char *p, const formatdef *f)
755{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000756 return unpack_double(p, 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000757}
758
Thomas Woutersb2137042007-02-01 18:02:27 +0000759static PyObject *
760bu_bool(const char *p, const formatdef *f)
761{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000762 char x;
763 memcpy((char *)&x, p, sizeof x);
764 return PyBool_FromLong(x != 0);
Thomas Woutersb2137042007-02-01 18:02:27 +0000765}
766
Thomas Wouters477c8d52006-05-27 19:21:47 +0000767static int
768bp_int(char *p, PyObject *v, const formatdef *f)
769{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000770 long x;
771 Py_ssize_t i;
772 if (get_long(v, &x) < 0)
773 return -1;
774 i = f->size;
775 if (i != SIZEOF_LONG) {
776 if ((i == 2) && (x < -32768 || x > 32767))
777 RANGE_ERROR(x, f, 0, 0xffffL);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000778#if (SIZEOF_LONG != 4)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000779 else if ((i == 4) && (x < -2147483648L || x > 2147483647L))
780 RANGE_ERROR(x, f, 0, 0xffffffffL);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000781#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000782 }
783 do {
784 p[--i] = (char)x;
785 x >>= 8;
786 } while (i > 0);
787 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000788}
789
790static int
791bp_uint(char *p, PyObject *v, const formatdef *f)
792{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793 unsigned long x;
794 Py_ssize_t i;
795 if (get_ulong(v, &x) < 0)
796 return -1;
797 i = f->size;
798 if (i != SIZEOF_LONG) {
799 unsigned long maxint = 1;
800 maxint <<= (unsigned long)(i * 8);
801 if (x >= maxint)
802 RANGE_ERROR(x, f, 1, maxint - 1);
803 }
804 do {
805 p[--i] = (char)x;
806 x >>= 8;
807 } while (i > 0);
808 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000809}
810
811static int
812bp_longlong(char *p, PyObject *v, const formatdef *f)
813{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 int res;
815 v = get_pylong(v);
816 if (v == NULL)
817 return -1;
818 res = _PyLong_AsByteArray((PyLongObject *)v,
819 (unsigned char *)p,
820 8,
821 0, /* little_endian */
822 1 /* signed */);
823 Py_DECREF(v);
824 return res;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000825}
826
827static int
828bp_ulonglong(char *p, PyObject *v, const formatdef *f)
829{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000830 int res;
831 v = get_pylong(v);
832 if (v == NULL)
833 return -1;
834 res = _PyLong_AsByteArray((PyLongObject *)v,
835 (unsigned char *)p,
836 8,
837 0, /* little_endian */
838 0 /* signed */);
839 Py_DECREF(v);
840 return res;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000841}
842
843static int
844bp_float(char *p, PyObject *v, const formatdef *f)
845{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 double x = PyFloat_AsDouble(v);
847 if (x == -1 && PyErr_Occurred()) {
848 PyErr_SetString(StructError,
849 "required argument is not a float");
850 return -1;
851 }
852 return _PyFloat_Pack4(x, (unsigned char *)p, 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000853}
854
855static int
856bp_double(char *p, PyObject *v, const formatdef *f)
857{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000858 double x = PyFloat_AsDouble(v);
859 if (x == -1 && PyErr_Occurred()) {
860 PyErr_SetString(StructError,
861 "required argument is not a float");
862 return -1;
863 }
864 return _PyFloat_Pack8(x, (unsigned char *)p, 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000865}
866
Thomas Woutersb2137042007-02-01 18:02:27 +0000867static int
868bp_bool(char *p, PyObject *v, const formatdef *f)
869{
Mark Dickinsoneff5d852010-07-18 07:29:02 +0000870 int y;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000871 y = PyObject_IsTrue(v);
Benjamin Petersonde73c452010-07-07 18:54:59 +0000872 if (y < 0)
873 return -1;
Mark Dickinsoneff5d852010-07-18 07:29:02 +0000874 *p = (char)y;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000875 return 0;
Thomas Woutersb2137042007-02-01 18:02:27 +0000876}
877
Thomas Wouters477c8d52006-05-27 19:21:47 +0000878static formatdef bigendian_table[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000879 {'x', 1, 0, NULL},
880 {'b', 1, 0, nu_byte, np_byte},
881 {'B', 1, 0, nu_ubyte, np_ubyte},
882 {'c', 1, 0, nu_char, np_char},
883 {'s', 1, 0, NULL},
884 {'p', 1, 0, NULL},
885 {'h', 2, 0, bu_int, bp_int},
886 {'H', 2, 0, bu_uint, bp_uint},
887 {'i', 4, 0, bu_int, bp_int},
888 {'I', 4, 0, bu_uint, bp_uint},
889 {'l', 4, 0, bu_int, bp_int},
890 {'L', 4, 0, bu_uint, bp_uint},
891 {'q', 8, 0, bu_longlong, bp_longlong},
892 {'Q', 8, 0, bu_ulonglong, bp_ulonglong},
893 {'?', 1, 0, bu_bool, bp_bool},
894 {'f', 4, 0, bu_float, bp_float},
895 {'d', 8, 0, bu_double, bp_double},
896 {0}
Thomas Wouters477c8d52006-05-27 19:21:47 +0000897};
898
899/* Little-endian routines. *****************************************************/
900
901static PyObject *
902lu_int(const char *p, const formatdef *f)
903{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000904 long x = 0;
905 Py_ssize_t i = f->size;
906 const unsigned char *bytes = (const unsigned char *)p;
907 do {
908 x = (x<<8) | bytes[--i];
909 } while (i > 0);
910 /* Extend the sign bit. */
911 if (SIZEOF_LONG > f->size)
912 x |= -(x & (1L << ((8 * f->size) - 1)));
913 return PyLong_FromLong(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000914}
915
916static PyObject *
917lu_uint(const char *p, const formatdef *f)
918{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000919 unsigned long x = 0;
920 Py_ssize_t i = f->size;
921 const unsigned char *bytes = (const unsigned char *)p;
922 do {
923 x = (x<<8) | bytes[--i];
924 } while (i > 0);
925 if (x <= LONG_MAX)
926 return PyLong_FromLong((long)x);
927 return PyLong_FromUnsignedLong((long)x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000928}
929
930static PyObject *
931lu_longlong(const char *p, const formatdef *f)
932{
933#ifdef HAVE_LONG_LONG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000934 PY_LONG_LONG x = 0;
935 Py_ssize_t i = f->size;
936 const unsigned char *bytes = (const unsigned char *)p;
937 do {
938 x = (x<<8) | bytes[--i];
939 } while (i > 0);
940 /* Extend the sign bit. */
941 if (SIZEOF_LONG_LONG > f->size)
942 x |= -(x & ((PY_LONG_LONG)1 << ((8 * f->size) - 1)));
943 if (x >= LONG_MIN && x <= LONG_MAX)
944 return PyLong_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
945 return PyLong_FromLongLong(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000946#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000947 return _PyLong_FromByteArray((const unsigned char *)p,
948 8,
949 1, /* little-endian */
950 1 /* signed */);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000951#endif
952}
953
954static PyObject *
955lu_ulonglong(const char *p, const formatdef *f)
956{
957#ifdef HAVE_LONG_LONG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 unsigned PY_LONG_LONG x = 0;
959 Py_ssize_t i = f->size;
960 const unsigned char *bytes = (const unsigned char *)p;
961 do {
962 x = (x<<8) | bytes[--i];
963 } while (i > 0);
964 if (x <= LONG_MAX)
965 return PyLong_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
966 return PyLong_FromUnsignedLongLong(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000967#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000968 return _PyLong_FromByteArray((const unsigned char *)p,
969 8,
970 1, /* little-endian */
971 0 /* signed */);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000972#endif
973}
974
975static PyObject *
976lu_float(const char *p, const formatdef *f)
977{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000978 return unpack_float(p, 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000979}
980
981static PyObject *
982lu_double(const char *p, const formatdef *f)
983{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000984 return unpack_double(p, 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000985}
986
987static int
988lp_int(char *p, PyObject *v, const formatdef *f)
989{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990 long x;
991 Py_ssize_t i;
992 if (get_long(v, &x) < 0)
993 return -1;
994 i = f->size;
995 if (i != SIZEOF_LONG) {
996 if ((i == 2) && (x < -32768 || x > 32767))
997 RANGE_ERROR(x, f, 0, 0xffffL);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000998#if (SIZEOF_LONG != 4)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 else if ((i == 4) && (x < -2147483648L || x > 2147483647L))
1000 RANGE_ERROR(x, f, 0, 0xffffffffL);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001001#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 }
1003 do {
1004 *p++ = (char)x;
1005 x >>= 8;
1006 } while (--i > 0);
1007 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001008}
1009
1010static int
1011lp_uint(char *p, PyObject *v, const formatdef *f)
1012{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001013 unsigned long x;
1014 Py_ssize_t i;
1015 if (get_ulong(v, &x) < 0)
1016 return -1;
1017 i = f->size;
1018 if (i != SIZEOF_LONG) {
1019 unsigned long maxint = 1;
1020 maxint <<= (unsigned long)(i * 8);
1021 if (x >= maxint)
1022 RANGE_ERROR(x, f, 1, maxint - 1);
1023 }
1024 do {
1025 *p++ = (char)x;
1026 x >>= 8;
1027 } while (--i > 0);
1028 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001029}
1030
1031static int
1032lp_longlong(char *p, PyObject *v, const formatdef *f)
1033{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001034 int res;
1035 v = get_pylong(v);
1036 if (v == NULL)
1037 return -1;
1038 res = _PyLong_AsByteArray((PyLongObject*)v,
1039 (unsigned char *)p,
1040 8,
1041 1, /* little_endian */
1042 1 /* signed */);
1043 Py_DECREF(v);
1044 return res;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001045}
1046
1047static int
1048lp_ulonglong(char *p, PyObject *v, const formatdef *f)
1049{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001050 int res;
1051 v = get_pylong(v);
1052 if (v == NULL)
1053 return -1;
1054 res = _PyLong_AsByteArray((PyLongObject*)v,
1055 (unsigned char *)p,
1056 8,
1057 1, /* little_endian */
1058 0 /* signed */);
1059 Py_DECREF(v);
1060 return res;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001061}
1062
1063static int
1064lp_float(char *p, PyObject *v, const formatdef *f)
1065{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066 double x = PyFloat_AsDouble(v);
1067 if (x == -1 && PyErr_Occurred()) {
1068 PyErr_SetString(StructError,
1069 "required argument is not a float");
1070 return -1;
1071 }
1072 return _PyFloat_Pack4(x, (unsigned char *)p, 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001073}
1074
1075static int
1076lp_double(char *p, PyObject *v, const formatdef *f)
1077{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001078 double x = PyFloat_AsDouble(v);
1079 if (x == -1 && PyErr_Occurred()) {
1080 PyErr_SetString(StructError,
1081 "required argument is not a float");
1082 return -1;
1083 }
1084 return _PyFloat_Pack8(x, (unsigned char *)p, 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001085}
1086
1087static formatdef lilendian_table[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001088 {'x', 1, 0, NULL},
1089 {'b', 1, 0, nu_byte, np_byte},
1090 {'B', 1, 0, nu_ubyte, np_ubyte},
1091 {'c', 1, 0, nu_char, np_char},
1092 {'s', 1, 0, NULL},
1093 {'p', 1, 0, NULL},
1094 {'h', 2, 0, lu_int, lp_int},
1095 {'H', 2, 0, lu_uint, lp_uint},
1096 {'i', 4, 0, lu_int, lp_int},
1097 {'I', 4, 0, lu_uint, lp_uint},
1098 {'l', 4, 0, lu_int, lp_int},
1099 {'L', 4, 0, lu_uint, lp_uint},
1100 {'q', 8, 0, lu_longlong, lp_longlong},
1101 {'Q', 8, 0, lu_ulonglong, lp_ulonglong},
1102 {'?', 1, 0, bu_bool, bp_bool}, /* Std rep not endian dep,
1103 but potentially different from native rep -- reuse bx_bool funcs. */
1104 {'f', 4, 0, lu_float, lp_float},
1105 {'d', 8, 0, lu_double, lp_double},
1106 {0}
Thomas Wouters477c8d52006-05-27 19:21:47 +00001107};
1108
1109
1110static const formatdef *
1111whichtable(char **pfmt)
1112{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001113 const char *fmt = (*pfmt)++; /* May be backed out of later */
1114 switch (*fmt) {
1115 case '<':
1116 return lilendian_table;
1117 case '>':
1118 case '!': /* Network byte order is big-endian */
1119 return bigendian_table;
1120 case '=': { /* Host byte order -- different from native in aligment! */
1121 int n = 1;
1122 char *p = (char *) &n;
1123 if (*p == 1)
1124 return lilendian_table;
1125 else
1126 return bigendian_table;
1127 }
1128 default:
1129 --*pfmt; /* Back out of pointer increment */
1130 /* Fall through */
1131 case '@':
1132 return native_table;
1133 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001134}
1135
1136
1137/* Get the table entry for a format code */
1138
1139static const formatdef *
1140getentry(int c, const formatdef *f)
1141{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001142 for (; f->format != '\0'; f++) {
1143 if (f->format == c) {
1144 return f;
1145 }
1146 }
1147 PyErr_SetString(StructError, "bad char in struct format");
1148 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001149}
1150
1151
Mark Dickinsonb72e6862010-06-11 19:50:30 +00001152/* Align a size according to a format code. Return -1 on overflow. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001153
Mark Dickinsoneac0e682010-06-11 19:05:08 +00001154static Py_ssize_t
Thomas Wouters477c8d52006-05-27 19:21:47 +00001155align(Py_ssize_t size, char c, const formatdef *e)
1156{
Mark Dickinsonb72e6862010-06-11 19:50:30 +00001157 Py_ssize_t extra;
1158
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001159 if (e->format == c) {
Mark Dickinsonb72e6862010-06-11 19:50:30 +00001160 if (e->alignment && size > 0) {
1161 extra = (e->alignment - 1) - (size - 1) % (e->alignment);
1162 if (extra > PY_SSIZE_T_MAX - size)
1163 return -1;
1164 size += extra;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001165 }
1166 }
1167 return size;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001168}
1169
1170
1171/* calculate the size of a format string */
1172
1173static int
1174prepare_s(PyStructObject *self)
1175{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001176 const formatdef *f;
1177 const formatdef *e;
1178 formatcode *codes;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001179
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001180 const char *s;
1181 const char *fmt;
1182 char c;
Mark Dickinsonb72e6862010-06-11 19:50:30 +00001183 Py_ssize_t size, len, num, itemsize;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001184
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001185 fmt = PyBytes_AS_STRING(self->s_format);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001186
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001187 f = whichtable((char **)&fmt);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001188
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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') {
Mark Dickinsonab4096f2010-06-11 16:56:34 +00001198 /* overflow-safe version of
1199 if (num*10 + (c - '0') > PY_SSIZE_T_MAX) { ... } */
1200 if (num >= PY_SSIZE_T_MAX / 10 && (
1201 num > PY_SSIZE_T_MAX / 10 ||
Mark Dickinsonb72e6862010-06-11 19:50:30 +00001202 (c - '0') > PY_SSIZE_T_MAX % 10))
1203 goto overflow;
Mark Dickinsonab4096f2010-06-11 16:56:34 +00001204 num = num*10 + (c - '0');
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001205 }
Alexander Belopolsky177e8532010-06-11 16:04:59 +00001206 if (c == '\0') {
1207 PyErr_SetString(StructError,
1208 "repeat count given without format specifier");
1209 return -1;
1210 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001211 }
1212 else
1213 num = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001214
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001215 e = getentry(c, f);
1216 if (e == NULL)
1217 return -1;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001218
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001219 switch (c) {
1220 case 's': /* fall through */
1221 case 'p': len++; break;
1222 case 'x': break;
1223 default: len += num; break;
1224 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001225
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001226 itemsize = e->size;
1227 size = align(size, c, e);
Mark Dickinsonb72e6862010-06-11 19:50:30 +00001228 if (size == -1)
1229 goto overflow;
1230
1231 /* if (size + num * itemsize > PY_SSIZE_T_MAX) { ... } */
1232 if (num > (PY_SSIZE_T_MAX - size) / itemsize)
1233 goto overflow;
1234 size += num * itemsize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001235 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001236
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001237 /* check for overflow */
1238 if ((len + 1) > (PY_SSIZE_T_MAX / sizeof(formatcode))) {
1239 PyErr_NoMemory();
1240 return -1;
1241 }
Amaury Forgeot d'Arc35c86582008-06-17 21:11:29 +00001242
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001243 self->s_size = size;
1244 self->s_len = len;
1245 codes = PyMem_MALLOC((len + 1) * sizeof(formatcode));
1246 if (codes == NULL) {
1247 PyErr_NoMemory();
1248 return -1;
1249 }
Mark Dickinsoncf28b952010-07-29 21:41:59 +00001250 /* Free any s_codes value left over from a previous initialization. */
1251 if (self->s_codes != NULL)
1252 PyMem_FREE(self->s_codes);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001253 self->s_codes = codes;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001254
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001255 s = fmt;
1256 size = 0;
1257 while ((c = *s++) != '\0') {
1258 if (isspace(Py_CHARMASK(c)))
1259 continue;
1260 if ('0' <= c && c <= '9') {
1261 num = c - '0';
1262 while ('0' <= (c = *s++) && c <= '9')
1263 num = num*10 + (c - '0');
1264 if (c == '\0')
1265 break;
1266 }
1267 else
1268 num = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001269
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001270 e = getentry(c, f);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001272 size = align(size, c, e);
1273 if (c == 's' || c == 'p') {
1274 codes->offset = size;
1275 codes->size = num;
1276 codes->fmtdef = e;
1277 codes++;
1278 size += num;
1279 } else if (c == 'x') {
1280 size += num;
1281 } else {
1282 while (--num >= 0) {
1283 codes->offset = size;
1284 codes->size = e->size;
1285 codes->fmtdef = e;
1286 codes++;
1287 size += e->size;
1288 }
1289 }
1290 }
1291 codes->fmtdef = NULL;
1292 codes->offset = size;
1293 codes->size = 0;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001294
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001295 return 0;
Mark Dickinsonb72e6862010-06-11 19:50:30 +00001296
1297 overflow:
1298 PyErr_SetString(StructError,
1299 "total struct size too long");
1300 return -1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001301}
1302
1303static PyObject *
1304s_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1305{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001306 PyObject *self;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001307
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001308 assert(type != NULL && type->tp_alloc != NULL);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001309
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001310 self = type->tp_alloc(type, 0);
1311 if (self != NULL) {
1312 PyStructObject *s = (PyStructObject*)self;
1313 Py_INCREF(Py_None);
1314 s->s_format = Py_None;
1315 s->s_codes = NULL;
1316 s->s_size = -1;
1317 s->s_len = -1;
1318 }
1319 return self;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001320}
1321
1322static int
1323s_init(PyObject *self, PyObject *args, PyObject *kwds)
1324{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001325 PyStructObject *soself = (PyStructObject *)self;
1326 PyObject *o_format = NULL;
1327 int ret = 0;
1328 static char *kwlist[] = {"format", 0};
Thomas Wouters477c8d52006-05-27 19:21:47 +00001329
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001330 assert(PyStruct_Check(self));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001331
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001332 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:Struct", kwlist,
1333 &o_format))
1334 return -1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001335
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001336 if (PyUnicode_Check(o_format)) {
1337 o_format = PyUnicode_AsASCIIString(o_format);
1338 if (o_format == NULL)
1339 return -1;
1340 }
1341 /* XXX support buffer interface, too */
1342 else {
1343 Py_INCREF(o_format);
1344 }
Christian Heimesa34706f2008-01-04 03:06:10 +00001345
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001346 if (!PyBytes_Check(o_format)) {
1347 Py_DECREF(o_format);
1348 PyErr_Format(PyExc_TypeError,
1349 "Struct() argument 1 must be bytes, not %.200s",
1350 Py_TYPE(o_format)->tp_name);
1351 return -1;
1352 }
Christian Heimesa34706f2008-01-04 03:06:10 +00001353
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001354 Py_CLEAR(soself->s_format);
1355 soself->s_format = o_format;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001356
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001357 ret = prepare_s(soself);
1358 return ret;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001359}
1360
1361static void
1362s_dealloc(PyStructObject *s)
1363{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001364 if (s->weakreflist != NULL)
1365 PyObject_ClearWeakRefs((PyObject *)s);
1366 if (s->s_codes != NULL) {
1367 PyMem_FREE(s->s_codes);
1368 }
1369 Py_XDECREF(s->s_format);
1370 Py_TYPE(s)->tp_free((PyObject *)s);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001371}
1372
1373static PyObject *
1374s_unpack_internal(PyStructObject *soself, char *startfrom) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001375 formatcode *code;
1376 Py_ssize_t i = 0;
1377 PyObject *result = PyTuple_New(soself->s_len);
1378 if (result == NULL)
1379 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001380
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001381 for (code = soself->s_codes; code->fmtdef != NULL; code++) {
1382 PyObject *v;
1383 const formatdef *e = code->fmtdef;
1384 const char *res = startfrom + code->offset;
1385 if (e->format == 's') {
1386 v = PyBytes_FromStringAndSize(res, code->size);
1387 } else if (e->format == 'p') {
1388 Py_ssize_t n = *(unsigned char*)res;
1389 if (n >= code->size)
1390 n = code->size - 1;
1391 v = PyBytes_FromStringAndSize(res + 1, n);
1392 } else {
1393 v = e->unpack(res, e);
1394 }
1395 if (v == NULL)
1396 goto fail;
1397 PyTuple_SET_ITEM(result, i++, v);
1398 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001399
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001400 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001401fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001402 Py_DECREF(result);
1403 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001404}
1405
1406
1407PyDoc_STRVAR(s_unpack__doc__,
Guido van Rossum913dd0b2007-04-13 03:33:53 +00001408"S.unpack(buffer) -> (v1, v2, ...)\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +00001409\n\
Mark Dickinsonaacfa952010-06-12 15:43:45 +00001410Return a tuple containing values unpacked according to the format\n\
1411string S.format. Requires len(buffer) == S.size. See help(struct)\n\
1412for more on format strings.");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001413
1414static PyObject *
Guido van Rossum98297ee2007-11-06 21:34:58 +00001415s_unpack(PyObject *self, PyObject *input)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001416{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001417 Py_buffer vbuf;
1418 PyObject *result;
1419 PyStructObject *soself = (PyStructObject *)self;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001420
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001421 assert(PyStruct_Check(self));
1422 assert(soself->s_codes != NULL);
1423 if (PyObject_GetBuffer(input, &vbuf, PyBUF_SIMPLE) < 0)
1424 return NULL;
1425 if (vbuf.len != soself->s_size) {
1426 PyErr_Format(StructError,
1427 "unpack requires a bytes argument of length %zd",
1428 soself->s_size);
1429 PyBuffer_Release(&vbuf);
1430 return NULL;
1431 }
1432 result = s_unpack_internal(soself, vbuf.buf);
1433 PyBuffer_Release(&vbuf);
1434 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001435}
1436
1437PyDoc_STRVAR(s_unpack_from__doc__,
Mark Dickinsonc6f13962010-06-13 09:17:13 +00001438"S.unpack_from(buffer, offset=0) -> (v1, v2, ...)\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +00001439\n\
Mark Dickinsonaacfa952010-06-12 15:43:45 +00001440Return a tuple containing values unpacked according to the format\n\
1441string S.format. Requires len(buffer[offset:]) >= S.size. See\n\
1442help(struct) for more on format strings.");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001443
1444static PyObject *
1445s_unpack_from(PyObject *self, PyObject *args, PyObject *kwds)
1446{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001447 static char *kwlist[] = {"buffer", "offset", 0};
Guido van Rossum98297ee2007-11-06 21:34:58 +00001448
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001449 PyObject *input;
1450 Py_ssize_t offset = 0;
1451 Py_buffer vbuf;
1452 PyObject *result;
1453 PyStructObject *soself = (PyStructObject *)self;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001454
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001455 assert(PyStruct_Check(self));
1456 assert(soself->s_codes != NULL);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001457
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001458 if (!PyArg_ParseTupleAndKeywords(args, kwds,
1459 "O|n:unpack_from", kwlist,
1460 &input, &offset))
1461 return NULL;
1462 if (PyObject_GetBuffer(input, &vbuf, PyBUF_SIMPLE) < 0)
1463 return NULL;
1464 if (offset < 0)
1465 offset += vbuf.len;
1466 if (offset < 0 || vbuf.len - offset < soself->s_size) {
1467 PyErr_Format(StructError,
1468 "unpack_from requires a buffer of at least %zd bytes",
1469 soself->s_size);
1470 PyBuffer_Release(&vbuf);
1471 return NULL;
1472 }
1473 result = s_unpack_internal(soself, (char*)vbuf.buf + offset);
1474 PyBuffer_Release(&vbuf);
1475 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001476}
1477
1478
1479/*
1480 * Guts of the pack function.
1481 *
1482 * Takes a struct object, a tuple of arguments, and offset in that tuple of
1483 * argument for where to start processing the arguments for packing, and a
1484 * character buffer for writing the packed string. The caller must insure
1485 * that the buffer may contain the required length for packing the arguments.
1486 * 0 is returned on success, 1 is returned if there is an error.
1487 *
1488 */
1489static int
1490s_pack_internal(PyStructObject *soself, PyObject *args, int offset, char* buf)
1491{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001492 formatcode *code;
1493 /* XXX(nnorwitz): why does i need to be a local? can we use
1494 the offset parameter or do we need the wider width? */
1495 Py_ssize_t i;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001496
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001497 memset(buf, '\0', soself->s_size);
1498 i = offset;
1499 for (code = soself->s_codes; code->fmtdef != NULL; code++) {
1500 Py_ssize_t n;
1501 PyObject *v = PyTuple_GET_ITEM(args, i++);
1502 const formatdef *e = code->fmtdef;
1503 char *res = buf + code->offset;
1504 if (e->format == 's') {
1505 int isstring;
1506 void *p;
1507 if (PyUnicode_Check(v)) {
1508 v = _PyUnicode_AsDefaultEncodedString(v, NULL);
1509 if (v == NULL)
1510 return -1;
1511 }
1512 isstring = PyBytes_Check(v);
1513 if (!isstring && !PyByteArray_Check(v)) {
1514 PyErr_SetString(StructError,
1515 "argument for 's' must be a bytes or string");
1516 return -1;
1517 }
1518 if (isstring) {
1519 n = PyBytes_GET_SIZE(v);
1520 p = PyBytes_AS_STRING(v);
1521 }
1522 else {
1523 n = PyByteArray_GET_SIZE(v);
1524 p = PyByteArray_AS_STRING(v);
1525 }
1526 if (n > code->size)
1527 n = code->size;
1528 if (n > 0)
1529 memcpy(res, p, n);
1530 } else if (e->format == 'p') {
1531 int isstring;
1532 void *p;
1533 if (PyUnicode_Check(v)) {
1534 v = _PyUnicode_AsDefaultEncodedString(v, NULL);
1535 if (v == NULL)
1536 return -1;
1537 }
1538 isstring = PyBytes_Check(v);
1539 if (!isstring && !PyByteArray_Check(v)) {
1540 PyErr_SetString(StructError,
1541 "argument for 'p' must be a bytes or string");
1542 return -1;
1543 }
1544 if (isstring) {
1545 n = PyBytes_GET_SIZE(v);
1546 p = PyBytes_AS_STRING(v);
1547 }
1548 else {
1549 n = PyByteArray_GET_SIZE(v);
1550 p = PyByteArray_AS_STRING(v);
1551 }
1552 if (n > (code->size - 1))
1553 n = code->size - 1;
1554 if (n > 0)
1555 memcpy(res + 1, p, n);
1556 if (n > 255)
1557 n = 255;
1558 *res = Py_SAFE_DOWNCAST(n, Py_ssize_t, unsigned char);
1559 } else {
1560 if (e->pack(res, v, e) < 0) {
1561 if (PyLong_Check(v) && PyErr_ExceptionMatches(PyExc_OverflowError))
1562 PyErr_SetString(StructError,
1563 "long too large to convert to int");
1564 return -1;
1565 }
1566 }
1567 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001568
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001569 /* Success */
1570 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001571}
1572
1573
1574PyDoc_STRVAR(s_pack__doc__,
Benjamin Peterson4ae19462008-07-31 15:03:40 +00001575"S.pack(v1, v2, ...) -> bytes\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +00001576\n\
Mark Dickinsonaacfa952010-06-12 15:43:45 +00001577Return a bytes object containing values v1, v2, ... packed according\n\
1578to the format string S.format. See help(struct) for more on format\n\
1579strings.");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001580
1581static PyObject *
1582s_pack(PyObject *self, PyObject *args)
1583{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001584 PyStructObject *soself;
1585 PyObject *result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001586
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001587 /* Validate arguments. */
1588 soself = (PyStructObject *)self;
1589 assert(PyStruct_Check(self));
1590 assert(soself->s_codes != NULL);
1591 if (PyTuple_GET_SIZE(args) != soself->s_len)
1592 {
1593 PyErr_Format(StructError,
1594 "pack requires exactly %zd arguments", soself->s_len);
1595 return NULL;
1596 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001597
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001598 /* Allocate a new string */
1599 result = PyBytes_FromStringAndSize((char *)NULL, soself->s_size);
1600 if (result == NULL)
1601 return NULL;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001602
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001603 /* Call the guts */
1604 if ( s_pack_internal(soself, args, 0, PyBytes_AS_STRING(result)) != 0 ) {
1605 Py_DECREF(result);
1606 return NULL;
1607 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001608
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001609 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001610}
1611
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001612PyDoc_STRVAR(s_pack_into__doc__,
1613"S.pack_into(buffer, offset, v1, v2, ...)\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +00001614\n\
Mark Dickinsonaacfa952010-06-12 15:43:45 +00001615Pack the values v1, v2, ... according to the format string S.format\n\
1616and write the packed bytes into the writable buffer buf starting at\n\
Mark Dickinsonfdb99f12010-06-12 16:30:53 +00001617offset. Note that the offset is a required argument. See\n\
Mark Dickinsonaacfa952010-06-12 15:43:45 +00001618help(struct) for more on format strings.");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001619
1620static PyObject *
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001621s_pack_into(PyObject *self, PyObject *args)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001622{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001623 PyStructObject *soself;
1624 char *buffer;
1625 Py_ssize_t buffer_len, offset;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001626
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001627 /* Validate arguments. +1 is for the first arg as buffer. */
1628 soself = (PyStructObject *)self;
1629 assert(PyStruct_Check(self));
1630 assert(soself->s_codes != NULL);
1631 if (PyTuple_GET_SIZE(args) != (soself->s_len + 2))
1632 {
1633 PyErr_Format(StructError,
1634 "pack_into requires exactly %zd arguments",
1635 (soself->s_len + 2));
1636 return NULL;
1637 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001638
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001639 /* Extract a writable memory buffer from the first argument */
1640 if ( PyObject_AsWriteBuffer(PyTuple_GET_ITEM(args, 0),
1641 (void**)&buffer, &buffer_len) == -1 ) {
1642 return NULL;
1643 }
1644 assert( buffer_len >= 0 );
Thomas Wouters477c8d52006-05-27 19:21:47 +00001645
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001646 /* Extract the offset from the first argument */
1647 offset = PyNumber_AsSsize_t(PyTuple_GET_ITEM(args, 1), PyExc_IndexError);
1648 if (offset == -1 && PyErr_Occurred())
1649 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001650
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001651 /* Support negative offsets. */
1652 if (offset < 0)
1653 offset += buffer_len;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001654
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001655 /* Check boundaries */
1656 if (offset < 0 || (buffer_len - offset) < soself->s_size) {
1657 PyErr_Format(StructError,
1658 "pack_into requires a buffer of at least %zd bytes",
1659 soself->s_size);
1660 return NULL;
1661 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001662
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001663 /* Call the guts */
1664 if ( s_pack_internal(soself, args, 2, buffer + offset) != 0 ) {
1665 return NULL;
1666 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001667
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001668 Py_RETURN_NONE;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001669}
1670
1671static PyObject *
1672s_get_format(PyStructObject *self, void *unused)
1673{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001674 Py_INCREF(self->s_format);
1675 return self->s_format;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001676}
1677
1678static PyObject *
1679s_get_size(PyStructObject *self, void *unused)
1680{
Christian Heimes217cfd12007-12-02 14:31:20 +00001681 return PyLong_FromSsize_t(self->s_size);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001682}
1683
1684/* List of functions */
1685
1686static struct PyMethodDef s_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001687 {"pack", s_pack, METH_VARARGS, s_pack__doc__},
1688 {"pack_into", s_pack_into, METH_VARARGS, s_pack_into__doc__},
1689 {"unpack", s_unpack, METH_O, s_unpack__doc__},
1690 {"unpack_from", (PyCFunction)s_unpack_from, METH_VARARGS|METH_KEYWORDS,
1691 s_unpack_from__doc__},
1692 {NULL, NULL} /* sentinel */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001693};
1694
Alexander Belopolsky0bd003a2010-06-12 19:36:28 +00001695PyDoc_STRVAR(s__doc__,
1696"Struct(fmt) --> compiled struct object\n"
1697"\n"
1698"Return a new Struct object which writes and reads binary data according to\n"
1699"the format string fmt. See help(struct) for more on format strings.");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001700
1701#define OFF(x) offsetof(PyStructObject, x)
1702
1703static PyGetSetDef s_getsetlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001704 {"format", (getter)s_get_format, (setter)NULL, "struct format string", NULL},
1705 {"size", (getter)s_get_size, (setter)NULL, "struct size in bytes", NULL},
1706 {NULL} /* sentinel */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001707};
1708
1709static
1710PyTypeObject PyStructType = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001711 PyVarObject_HEAD_INIT(NULL, 0)
1712 "Struct",
1713 sizeof(PyStructObject),
1714 0,
1715 (destructor)s_dealloc, /* tp_dealloc */
1716 0, /* tp_print */
1717 0, /* tp_getattr */
1718 0, /* tp_setattr */
1719 0, /* tp_reserved */
1720 0, /* tp_repr */
1721 0, /* tp_as_number */
1722 0, /* tp_as_sequence */
1723 0, /* tp_as_mapping */
1724 0, /* tp_hash */
1725 0, /* tp_call */
1726 0, /* tp_str */
1727 PyObject_GenericGetAttr, /* tp_getattro */
1728 PyObject_GenericSetAttr, /* tp_setattro */
1729 0, /* tp_as_buffer */
1730 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
1731 s__doc__, /* tp_doc */
1732 0, /* tp_traverse */
1733 0, /* tp_clear */
1734 0, /* tp_richcompare */
1735 offsetof(PyStructObject, weakreflist), /* tp_weaklistoffset */
1736 0, /* tp_iter */
1737 0, /* tp_iternext */
1738 s_methods, /* tp_methods */
1739 NULL, /* tp_members */
1740 s_getsetlist, /* tp_getset */
1741 0, /* tp_base */
1742 0, /* tp_dict */
1743 0, /* tp_descr_get */
1744 0, /* tp_descr_set */
1745 0, /* tp_dictoffset */
1746 s_init, /* tp_init */
1747 PyType_GenericAlloc,/* tp_alloc */
1748 s_new, /* tp_new */
1749 PyObject_Del, /* tp_free */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001750};
1751
Christian Heimesa34706f2008-01-04 03:06:10 +00001752
1753/* ---- Standalone functions ---- */
1754
1755#define MAXCACHE 100
1756static PyObject *cache = NULL;
1757
1758static PyObject *
1759cache_struct(PyObject *fmt)
1760{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001761 PyObject * s_object;
Christian Heimesa34706f2008-01-04 03:06:10 +00001762
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001763 if (cache == NULL) {
1764 cache = PyDict_New();
1765 if (cache == NULL)
1766 return NULL;
1767 }
Christian Heimesa34706f2008-01-04 03:06:10 +00001768
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001769 s_object = PyDict_GetItem(cache, fmt);
1770 if (s_object != NULL) {
1771 Py_INCREF(s_object);
1772 return s_object;
1773 }
Christian Heimesa34706f2008-01-04 03:06:10 +00001774
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001775 s_object = PyObject_CallFunctionObjArgs((PyObject *)(&PyStructType), fmt, NULL);
1776 if (s_object != NULL) {
1777 if (PyDict_Size(cache) >= MAXCACHE)
1778 PyDict_Clear(cache);
1779 /* Attempt to cache the result */
1780 if (PyDict_SetItem(cache, fmt, s_object) == -1)
1781 PyErr_Clear();
1782 }
1783 return s_object;
Christian Heimesa34706f2008-01-04 03:06:10 +00001784}
1785
1786PyDoc_STRVAR(clearcache_doc,
1787"Clear the internal cache.");
1788
1789static PyObject *
1790clearcache(PyObject *self)
1791{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001792 Py_CLEAR(cache);
1793 Py_RETURN_NONE;
Christian Heimesa34706f2008-01-04 03:06:10 +00001794}
1795
1796PyDoc_STRVAR(calcsize_doc,
Mark Dickinsonaacfa952010-06-12 15:43:45 +00001797"calcsize(fmt) -> integer\n\
1798\n\
1799Return size in bytes of the struct described by the format string fmt.");
Christian Heimesa34706f2008-01-04 03:06:10 +00001800
1801static PyObject *
1802calcsize(PyObject *self, PyObject *fmt)
1803{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001804 Py_ssize_t n;
1805 PyObject *s_object = cache_struct(fmt);
1806 if (s_object == NULL)
1807 return NULL;
1808 n = ((PyStructObject *)s_object)->s_size;
1809 Py_DECREF(s_object);
1810 return PyLong_FromSsize_t(n);
Christian Heimesa34706f2008-01-04 03:06:10 +00001811}
1812
1813PyDoc_STRVAR(pack_doc,
Mark Dickinsonaacfa952010-06-12 15:43:45 +00001814"pack(fmt, v1, v2, ...) -> bytes\n\
1815\n\
Mark Dickinsonfdb99f12010-06-12 16:30:53 +00001816Return a bytes object containing the values v1, v2, ... packed according\n\
1817to the format string fmt. See help(struct) for more on format strings.");
Christian Heimesa34706f2008-01-04 03:06:10 +00001818
1819static PyObject *
1820pack(PyObject *self, PyObject *args)
1821{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001822 PyObject *s_object, *fmt, *newargs, *result;
1823 Py_ssize_t n = PyTuple_GET_SIZE(args);
Christian Heimesa34706f2008-01-04 03:06:10 +00001824
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001825 if (n == 0) {
1826 PyErr_SetString(PyExc_TypeError, "missing format argument");
1827 return NULL;
1828 }
1829 fmt = PyTuple_GET_ITEM(args, 0);
1830 newargs = PyTuple_GetSlice(args, 1, n);
1831 if (newargs == NULL)
1832 return NULL;
Christian Heimesa34706f2008-01-04 03:06:10 +00001833
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001834 s_object = cache_struct(fmt);
1835 if (s_object == NULL) {
1836 Py_DECREF(newargs);
1837 return NULL;
1838 }
1839 result = s_pack(s_object, newargs);
1840 Py_DECREF(newargs);
1841 Py_DECREF(s_object);
1842 return result;
Christian Heimesa34706f2008-01-04 03:06:10 +00001843}
1844
1845PyDoc_STRVAR(pack_into_doc,
Mark Dickinsonaacfa952010-06-12 15:43:45 +00001846"pack_into(fmt, buffer, offset, v1, v2, ...)\n\
1847\n\
1848Pack the values v1, v2, ... according to the format string fmt and write\n\
1849the packed bytes into the writable buffer buf starting at offset. Note\n\
Mark Dickinsonfdb99f12010-06-12 16:30:53 +00001850that the offset is a required argument. See help(struct) for more\n\
Mark Dickinsonaacfa952010-06-12 15:43:45 +00001851on format strings.");
Christian Heimesa34706f2008-01-04 03:06:10 +00001852
1853static PyObject *
1854pack_into(PyObject *self, PyObject *args)
1855{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001856 PyObject *s_object, *fmt, *newargs, *result;
1857 Py_ssize_t n = PyTuple_GET_SIZE(args);
Christian Heimesa34706f2008-01-04 03:06:10 +00001858
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001859 if (n == 0) {
1860 PyErr_SetString(PyExc_TypeError, "missing format argument");
1861 return NULL;
1862 }
1863 fmt = PyTuple_GET_ITEM(args, 0);
1864 newargs = PyTuple_GetSlice(args, 1, n);
1865 if (newargs == NULL)
1866 return NULL;
Christian Heimesa34706f2008-01-04 03:06:10 +00001867
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001868 s_object = cache_struct(fmt);
1869 if (s_object == NULL) {
1870 Py_DECREF(newargs);
1871 return NULL;
1872 }
1873 result = s_pack_into(s_object, newargs);
1874 Py_DECREF(newargs);
1875 Py_DECREF(s_object);
1876 return result;
Christian Heimesa34706f2008-01-04 03:06:10 +00001877}
1878
1879PyDoc_STRVAR(unpack_doc,
Mark Dickinsonaacfa952010-06-12 15:43:45 +00001880"unpack(fmt, buffer) -> (v1, v2, ...)\n\
1881\n\
1882Return a tuple containing values unpacked according to the format string\n\
1883fmt. Requires len(buffer) == calcsize(fmt). See help(struct) for more\n\
1884on format strings.");
Christian Heimesa34706f2008-01-04 03:06:10 +00001885
1886static PyObject *
1887unpack(PyObject *self, PyObject *args)
1888{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001889 PyObject *s_object, *fmt, *inputstr, *result;
Christian Heimesa34706f2008-01-04 03:06:10 +00001890
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001891 if (!PyArg_UnpackTuple(args, "unpack", 2, 2, &fmt, &inputstr))
1892 return NULL;
Christian Heimesa34706f2008-01-04 03:06:10 +00001893
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001894 s_object = cache_struct(fmt);
1895 if (s_object == NULL)
1896 return NULL;
1897 result = s_unpack(s_object, inputstr);
1898 Py_DECREF(s_object);
1899 return result;
Christian Heimesa34706f2008-01-04 03:06:10 +00001900}
1901
1902PyDoc_STRVAR(unpack_from_doc,
Mark Dickinsonc6f13962010-06-13 09:17:13 +00001903"unpack_from(fmt, buffer, offset=0) -> (v1, v2, ...)\n\
Mark Dickinsonaacfa952010-06-12 15:43:45 +00001904\n\
1905Return a tuple containing values unpacked according to the format string\n\
1906fmt. Requires len(buffer[offset:]) >= calcsize(fmt). See help(struct)\n\
1907for more on format strings.");
Christian Heimesa34706f2008-01-04 03:06:10 +00001908
1909static PyObject *
1910unpack_from(PyObject *self, PyObject *args, PyObject *kwds)
1911{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001912 PyObject *s_object, *fmt, *newargs, *result;
1913 Py_ssize_t n = PyTuple_GET_SIZE(args);
Christian Heimesa34706f2008-01-04 03:06:10 +00001914
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001915 if (n == 0) {
1916 PyErr_SetString(PyExc_TypeError, "missing format argument");
1917 return NULL;
1918 }
1919 fmt = PyTuple_GET_ITEM(args, 0);
1920 newargs = PyTuple_GetSlice(args, 1, n);
1921 if (newargs == NULL)
1922 return NULL;
Christian Heimesa34706f2008-01-04 03:06:10 +00001923
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001924 s_object = cache_struct(fmt);
1925 if (s_object == NULL) {
1926 Py_DECREF(newargs);
1927 return NULL;
1928 }
1929 result = s_unpack_from(s_object, newargs, kwds);
1930 Py_DECREF(newargs);
1931 Py_DECREF(s_object);
1932 return result;
Christian Heimesa34706f2008-01-04 03:06:10 +00001933}
1934
1935static struct PyMethodDef module_functions[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001936 {"_clearcache", (PyCFunction)clearcache, METH_NOARGS, clearcache_doc},
1937 {"calcsize", calcsize, METH_O, calcsize_doc},
1938 {"pack", pack, METH_VARARGS, pack_doc},
1939 {"pack_into", pack_into, METH_VARARGS, pack_into_doc},
1940 {"unpack", unpack, METH_VARARGS, unpack_doc},
1941 {"unpack_from", (PyCFunction)unpack_from,
1942 METH_VARARGS|METH_KEYWORDS, unpack_from_doc},
1943 {NULL, NULL} /* sentinel */
Christian Heimesa34706f2008-01-04 03:06:10 +00001944};
1945
1946
Thomas Wouters477c8d52006-05-27 19:21:47 +00001947/* Module initialization */
1948
Christian Heimesa34706f2008-01-04 03:06:10 +00001949PyDoc_STRVAR(module_doc,
1950"Functions to convert between Python values and C structs.\n\
Benjamin Peterson4ae19462008-07-31 15:03:40 +00001951Python bytes objects are used to hold the data representing the C struct\n\
Mark Dickinson40714af2009-10-08 15:59:20 +00001952and also as format strings (explained below) to describe the layout of data\n\
1953in the C struct.\n\
Christian Heimesa34706f2008-01-04 03:06:10 +00001954\n\
1955The optional first format char indicates byte order, size and alignment:\n\
Mark Dickinson40714af2009-10-08 15:59:20 +00001956 @: native order, size & alignment (default)\n\
1957 =: native order, std. size & alignment\n\
1958 <: little-endian, std. size & alignment\n\
1959 >: big-endian, std. size & alignment\n\
1960 !: same as >\n\
Christian Heimesa34706f2008-01-04 03:06:10 +00001961\n\
1962The remaining chars indicate types of args and must match exactly;\n\
1963these can be preceded by a decimal repeat count:\n\
1964 x: pad byte (no data); c:char; b:signed byte; B:unsigned byte;\n\
Mark Dickinson40714af2009-10-08 15:59:20 +00001965 ?: _Bool (requires C99; if not available, char is used instead)\n\
Christian Heimesa34706f2008-01-04 03:06:10 +00001966 h:short; H:unsigned short; i:int; I:unsigned int;\n\
1967 l:long; L:unsigned long; f:float; d:double.\n\
1968Special cases (preceding decimal count indicates length):\n\
1969 s:string (array of char); p: pascal string (with count byte).\n\
1970Special case (only available in native format):\n\
1971 P:an integer type that is wide enough to hold a pointer.\n\
1972Special case (not in native mode unless 'long long' in platform C):\n\
1973 q:long long; Q:unsigned long long\n\
1974Whitespace between formats is ignored.\n\
1975\n\
1976The variable struct.error is an exception raised on errors.\n");
1977
Martin v. Löwis1a214512008-06-11 05:26:20 +00001978
1979static struct PyModuleDef _structmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001980 PyModuleDef_HEAD_INIT,
1981 "_struct",
1982 module_doc,
1983 -1,
1984 module_functions,
1985 NULL,
1986 NULL,
1987 NULL,
1988 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001989};
1990
Thomas Wouters477c8d52006-05-27 19:21:47 +00001991PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001992PyInit__struct(void)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001993{
Mark Dickinson06817852010-06-12 09:25:13 +00001994 PyObject *m;
Christian Heimesa34706f2008-01-04 03:06:10 +00001995
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001996 m = PyModule_Create(&_structmodule);
1997 if (m == NULL)
1998 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001999
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002000 Py_TYPE(&PyStructType) = &PyType_Type;
2001 if (PyType_Ready(&PyStructType) < 0)
2002 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002003
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002004 /* Check endian and swap in faster functions */
2005 {
2006 int one = 1;
2007 formatdef *native = native_table;
2008 formatdef *other, *ptr;
2009 if ((int)*(unsigned char*)&one)
2010 other = lilendian_table;
2011 else
2012 other = bigendian_table;
2013 /* Scan through the native table, find a matching
2014 entry in the endian table and swap in the
2015 native implementations whenever possible
2016 (64-bit platforms may not have "standard" sizes) */
2017 while (native->format != '\0' && other->format != '\0') {
2018 ptr = other;
2019 while (ptr->format != '\0') {
2020 if (ptr->format == native->format) {
2021 /* Match faster when formats are
2022 listed in the same order */
2023 if (ptr == other)
2024 other++;
2025 /* Only use the trick if the
2026 size matches */
2027 if (ptr->size != native->size)
2028 break;
2029 /* Skip float and double, could be
2030 "unknown" float format */
2031 if (ptr->format == 'd' || ptr->format == 'f')
2032 break;
2033 ptr->pack = native->pack;
2034 ptr->unpack = native->unpack;
2035 break;
2036 }
2037 ptr++;
2038 }
2039 native++;
2040 }
2041 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002042
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002043 /* Add some symbolic constants to the module */
2044 if (StructError == NULL) {
2045 StructError = PyErr_NewException("struct.error", NULL, NULL);
2046 if (StructError == NULL)
2047 return NULL;
2048 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002049
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002050 Py_INCREF(StructError);
2051 PyModule_AddObject(m, "error", StructError);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002052
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002053 Py_INCREF((PyObject*)&PyStructType);
2054 PyModule_AddObject(m, "Struct", (PyObject*)&PyStructType);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002055
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002056 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002057}