blob: 2b4341ce2f682f8b2af9472219583ca2e718d4fc [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"
Thomas Wouters477c8d52006-05-27 19:21:47 +00009#include "structmember.h"
10#include <ctype.h>
11
12static PyTypeObject PyStructType;
13
Thomas Wouters477c8d52006-05-27 19:21:47 +000014/* The translation function for each format character is table driven */
15typedef struct _formatdef {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000016 char format;
17 Py_ssize_t size;
18 Py_ssize_t alignment;
19 PyObject* (*unpack)(const char *,
20 const struct _formatdef *);
21 int (*pack)(char *, PyObject *,
22 const struct _formatdef *);
Thomas Wouters477c8d52006-05-27 19:21:47 +000023} formatdef;
24
25typedef struct _formatcode {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000026 const struct _formatdef *fmtdef;
27 Py_ssize_t offset;
28 Py_ssize_t size;
Thomas Wouters477c8d52006-05-27 19:21:47 +000029} formatcode;
30
31/* Struct object interface */
32
33typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000034 PyObject_HEAD
35 Py_ssize_t s_size;
36 Py_ssize_t s_len;
37 formatcode *s_codes;
38 PyObject *s_format;
39 PyObject *weakreflist; /* List of weak references */
Thomas Wouters477c8d52006-05-27 19:21:47 +000040} PyStructObject;
41
42
43#define PyStruct_Check(op) PyObject_TypeCheck(op, &PyStructType)
Christian Heimes90aa7642007-12-19 02:45:37 +000044#define PyStruct_CheckExact(op) (Py_TYPE(op) == &PyStructType)
Thomas Wouters477c8d52006-05-27 19:21:47 +000045
46
47/* Exception */
48
49static PyObject *StructError;
50
51
52/* Define various structs to figure out the alignments of types */
53
54
55typedef struct { char c; short x; } st_short;
56typedef struct { char c; int x; } st_int;
57typedef struct { char c; long x; } st_long;
58typedef struct { char c; float x; } st_float;
59typedef struct { char c; double x; } st_double;
60typedef struct { char c; void *x; } st_void_p;
61
62#define SHORT_ALIGN (sizeof(st_short) - sizeof(short))
63#define INT_ALIGN (sizeof(st_int) - sizeof(int))
64#define LONG_ALIGN (sizeof(st_long) - sizeof(long))
65#define FLOAT_ALIGN (sizeof(st_float) - sizeof(float))
66#define DOUBLE_ALIGN (sizeof(st_double) - sizeof(double))
67#define VOID_P_ALIGN (sizeof(st_void_p) - sizeof(void *))
68
69/* We can't support q and Q in native mode unless the compiler does;
70 in std mode, they're 8 bytes on all platforms. */
71#ifdef HAVE_LONG_LONG
72typedef struct { char c; PY_LONG_LONG x; } s_long_long;
73#define LONG_LONG_ALIGN (sizeof(s_long_long) - sizeof(PY_LONG_LONG))
74#endif
75
Thomas Woutersb2137042007-02-01 18:02:27 +000076#ifdef HAVE_C99_BOOL
77#define BOOL_TYPE _Bool
78typedef struct { char c; _Bool x; } s_bool;
79#define BOOL_ALIGN (sizeof(s_bool) - sizeof(BOOL_TYPE))
80#else
81#define BOOL_TYPE char
82#define BOOL_ALIGN 0
83#endif
84
Thomas Wouters477c8d52006-05-27 19:21:47 +000085#define STRINGIFY(x) #x
86
87#ifdef __powerc
88#pragma options align=reset
89#endif
90
Mark Dickinson055a3fb2010-04-03 15:26:31 +000091/* Helper for integer format codes: converts an arbitrary Python object to a
92 PyLongObject if possible, otherwise fails. Caller should decref. */
Thomas Wouters477c8d52006-05-27 19:21:47 +000093
94static PyObject *
95get_pylong(PyObject *v)
96{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000097 assert(v != NULL);
98 if (!PyLong_Check(v)) {
99 /* Not an integer; try to use __index__ to convert. */
100 if (PyIndex_Check(v)) {
101 v = PyNumber_Index(v);
102 if (v == NULL)
103 return NULL;
104 }
105 else {
106 PyErr_SetString(StructError,
107 "required argument is not an integer");
108 return NULL;
109 }
110 }
111 else
112 Py_INCREF(v);
Mark Dickinsonea835e72009-04-19 20:40:33 +0000113
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000114 assert(PyLong_Check(v));
115 return v;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000116}
117
Mark Dickinsonea835e72009-04-19 20:40:33 +0000118/* Helper routine to get a C long and raise the appropriate error if it isn't
119 one */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000120
121static int
122get_long(PyObject *v, long *p)
123{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000124 long x;
Mark Dickinsonea835e72009-04-19 20:40:33 +0000125
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126 v = get_pylong(v);
127 if (v == NULL)
128 return -1;
129 assert(PyLong_Check(v));
130 x = PyLong_AsLong(v);
131 Py_DECREF(v);
132 if (x == (long)-1 && PyErr_Occurred()) {
133 if (PyErr_ExceptionMatches(PyExc_OverflowError))
134 PyErr_SetString(StructError,
135 "argument out of range");
136 return -1;
137 }
138 *p = x;
139 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000140}
141
142
143/* Same, but handling unsigned long */
144
145static int
146get_ulong(PyObject *v, unsigned long *p)
147{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000148 unsigned long x;
Mark Dickinsonea835e72009-04-19 20:40:33 +0000149
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000150 v = get_pylong(v);
151 if (v == NULL)
152 return -1;
153 assert(PyLong_Check(v));
154 x = PyLong_AsUnsignedLong(v);
155 Py_DECREF(v);
156 if (x == (unsigned long)-1 && PyErr_Occurred()) {
157 if (PyErr_ExceptionMatches(PyExc_OverflowError))
158 PyErr_SetString(StructError,
159 "argument out of range");
160 return -1;
161 }
162 *p = x;
163 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000164}
165
166#ifdef HAVE_LONG_LONG
167
168/* Same, but handling native long long. */
169
170static int
171get_longlong(PyObject *v, PY_LONG_LONG *p)
172{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000173 PY_LONG_LONG x;
Mark Dickinson055a3fb2010-04-03 15:26:31 +0000174
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000175 v = get_pylong(v);
176 if (v == NULL)
177 return -1;
178 assert(PyLong_Check(v));
179 x = PyLong_AsLongLong(v);
180 Py_DECREF(v);
181 if (x == (PY_LONG_LONG)-1 && PyErr_Occurred()) {
182 if (PyErr_ExceptionMatches(PyExc_OverflowError))
183 PyErr_SetString(StructError,
184 "argument out of range");
185 return -1;
186 }
187 *p = x;
188 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000189}
190
191/* Same, but handling native unsigned long long. */
192
193static int
194get_ulonglong(PyObject *v, unsigned PY_LONG_LONG *p)
195{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000196 unsigned PY_LONG_LONG x;
Mark Dickinson055a3fb2010-04-03 15:26:31 +0000197
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000198 v = get_pylong(v);
199 if (v == NULL)
200 return -1;
201 assert(PyLong_Check(v));
202 x = PyLong_AsUnsignedLongLong(v);
203 Py_DECREF(v);
204 if (x == (unsigned PY_LONG_LONG)-1 && PyErr_Occurred()) {
205 if (PyErr_ExceptionMatches(PyExc_OverflowError))
206 PyErr_SetString(StructError,
207 "argument out of range");
208 return -1;
209 }
210 *p = x;
211 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000212}
213
214#endif
215
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000216
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000217#define RANGE_ERROR(x, f, flag, mask) return _range_error(f, flag)
218
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000219
Thomas Wouters477c8d52006-05-27 19:21:47 +0000220/* Floating point helpers */
221
222static PyObject *
223unpack_float(const char *p, /* start of 4-byte string */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000224 int le) /* true for little-endian, false for big-endian */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000225{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000226 double x;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000227
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000228 x = _PyFloat_Unpack4((unsigned char *)p, le);
229 if (x == -1.0 && PyErr_Occurred())
230 return NULL;
231 return PyFloat_FromDouble(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000232}
233
234static PyObject *
235unpack_double(const char *p, /* start of 8-byte string */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000236 int le) /* true for little-endian, false for big-endian */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000237{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000238 double x;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000239
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000240 x = _PyFloat_Unpack8((unsigned char *)p, le);
241 if (x == -1.0 && PyErr_Occurred())
242 return NULL;
243 return PyFloat_FromDouble(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000244}
245
246/* Helper to format the range error exceptions */
247static int
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000248_range_error(const formatdef *f, int is_unsigned)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000249{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 /* ulargest is the largest unsigned value with f->size bytes.
251 * Note that the simpler:
252 * ((size_t)1 << (f->size * 8)) - 1
253 * doesn't work when f->size == sizeof(size_t) because C doesn't
254 * define what happens when a left shift count is >= the number of
255 * bits in the integer being shifted; e.g., on some boxes it doesn't
256 * shift at all when they're equal.
257 */
258 const size_t ulargest = (size_t)-1 >> ((SIZEOF_SIZE_T - f->size)*8);
259 assert(f->size >= 1 && f->size <= SIZEOF_SIZE_T);
260 if (is_unsigned)
261 PyErr_Format(StructError,
262 "'%c' format requires 0 <= number <= %zu",
263 f->format,
264 ulargest);
265 else {
266 const Py_ssize_t largest = (Py_ssize_t)(ulargest >> 1);
267 PyErr_Format(StructError,
268 "'%c' format requires %zd <= number <= %zd",
269 f->format,
270 ~ largest,
271 largest);
272 }
Mark Dickinsonae681df2009-03-21 10:26:31 +0000273
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000274 return -1;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000275}
276
277
278
279/* A large number of small routines follow, with names of the form
280
281 [bln][up]_TYPE
282
283 [bln] distiguishes among big-endian, little-endian and native.
284 [pu] distiguishes between pack (to struct) and unpack (from struct).
285 TYPE is one of char, byte, ubyte, etc.
286*/
287
288/* Native mode routines. ****************************************************/
289/* NOTE:
290 In all n[up]_<type> routines handling types larger than 1 byte, there is
291 *no* guarantee that the p pointer is properly aligned for each type,
292 therefore memcpy is called. An intermediate variable is used to
293 compensate for big-endian architectures.
294 Normally both the intermediate variable and the memcpy call will be
295 skipped by C optimisation in little-endian architectures (gcc >= 2.91
296 does this). */
297
298static PyObject *
299nu_char(const char *p, const formatdef *f)
300{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000301 return PyBytes_FromStringAndSize(p, 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000302}
303
304static PyObject *
305nu_byte(const char *p, const formatdef *f)
306{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 return PyLong_FromLong((long) *(signed char *)p);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000308}
309
310static PyObject *
311nu_ubyte(const char *p, const formatdef *f)
312{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000313 return PyLong_FromLong((long) *(unsigned char *)p);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000314}
315
316static PyObject *
317nu_short(const char *p, const formatdef *f)
318{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000319 short x;
320 memcpy((char *)&x, p, sizeof x);
321 return PyLong_FromLong((long)x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000322}
323
324static PyObject *
325nu_ushort(const char *p, const formatdef *f)
326{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000327 unsigned short x;
328 memcpy((char *)&x, p, sizeof x);
329 return PyLong_FromLong((long)x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000330}
331
332static PyObject *
333nu_int(const char *p, const formatdef *f)
334{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000335 int x;
336 memcpy((char *)&x, p, sizeof x);
337 return PyLong_FromLong((long)x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000338}
339
340static PyObject *
341nu_uint(const char *p, const formatdef *f)
342{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 unsigned int x;
344 memcpy((char *)&x, p, sizeof x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000345#if (SIZEOF_LONG > SIZEOF_INT)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000346 return PyLong_FromLong((long)x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000347#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 if (x <= ((unsigned int)LONG_MAX))
349 return PyLong_FromLong((long)x);
350 return PyLong_FromUnsignedLong((unsigned long)x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000351#endif
352}
353
354static PyObject *
355nu_long(const char *p, const formatdef *f)
356{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000357 long x;
358 memcpy((char *)&x, p, sizeof x);
359 return PyLong_FromLong(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000360}
361
362static PyObject *
363nu_ulong(const char *p, const formatdef *f)
364{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000365 unsigned long x;
366 memcpy((char *)&x, p, sizeof x);
367 if (x <= LONG_MAX)
368 return PyLong_FromLong((long)x);
369 return PyLong_FromUnsignedLong(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000370}
371
372/* Native mode doesn't support q or Q unless the platform C supports
373 long long (or, on Windows, __int64). */
374
375#ifdef HAVE_LONG_LONG
376
377static PyObject *
378nu_longlong(const char *p, const formatdef *f)
379{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 PY_LONG_LONG x;
381 memcpy((char *)&x, p, sizeof x);
382 if (x >= LONG_MIN && x <= LONG_MAX)
383 return PyLong_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
384 return PyLong_FromLongLong(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000385}
386
387static PyObject *
388nu_ulonglong(const char *p, const formatdef *f)
389{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000390 unsigned PY_LONG_LONG x;
391 memcpy((char *)&x, p, sizeof x);
392 if (x <= LONG_MAX)
393 return PyLong_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
394 return PyLong_FromUnsignedLongLong(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000395}
396
397#endif
398
399static PyObject *
Thomas Woutersb2137042007-02-01 18:02:27 +0000400nu_bool(const char *p, const formatdef *f)
401{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000402 BOOL_TYPE x;
403 memcpy((char *)&x, p, sizeof x);
404 return PyBool_FromLong(x != 0);
Thomas Woutersb2137042007-02-01 18:02:27 +0000405}
406
407
408static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +0000409nu_float(const char *p, const formatdef *f)
410{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411 float x;
412 memcpy((char *)&x, p, sizeof x);
413 return PyFloat_FromDouble((double)x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000414}
415
416static PyObject *
417nu_double(const char *p, const formatdef *f)
418{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000419 double x;
420 memcpy((char *)&x, p, sizeof x);
421 return PyFloat_FromDouble(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000422}
423
424static PyObject *
425nu_void_p(const char *p, const formatdef *f)
426{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000427 void *x;
428 memcpy((char *)&x, p, sizeof x);
429 return PyLong_FromVoidPtr(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000430}
431
432static int
433np_byte(char *p, PyObject *v, const formatdef *f)
434{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000435 long x;
436 if (get_long(v, &x) < 0)
437 return -1;
438 if (x < -128 || x > 127){
439 PyErr_SetString(StructError,
440 "byte format requires -128 <= number <= 127");
441 return -1;
442 }
443 *p = (char)x;
444 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000445}
446
447static int
448np_ubyte(char *p, PyObject *v, const formatdef *f)
449{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450 long x;
451 if (get_long(v, &x) < 0)
452 return -1;
453 if (x < 0 || x > 255){
454 PyErr_SetString(StructError,
455 "ubyte format requires 0 <= number <= 255");
456 return -1;
457 }
458 *p = (char)x;
459 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000460}
461
462static int
463np_char(char *p, PyObject *v, const formatdef *f)
464{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000465 if (PyUnicode_Check(v)) {
466 v = _PyUnicode_AsDefaultEncodedString(v, NULL);
467 if (v == NULL)
468 return -1;
469 }
470 if (!PyBytes_Check(v) || PyBytes_Size(v) != 1) {
471 PyErr_SetString(StructError,
472 "char format requires bytes or string of length 1");
473 return -1;
474 }
475 *p = *PyBytes_AsString(v);
476 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000477}
478
479static int
480np_short(char *p, PyObject *v, const formatdef *f)
481{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 long x;
483 short y;
484 if (get_long(v, &x) < 0)
485 return -1;
486 if (x < SHRT_MIN || x > SHRT_MAX){
487 PyErr_SetString(StructError,
488 "short format requires " STRINGIFY(SHRT_MIN)
489 " <= number <= " STRINGIFY(SHRT_MAX));
490 return -1;
491 }
492 y = (short)x;
493 memcpy(p, (char *)&y, sizeof y);
494 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000495}
496
497static int
498np_ushort(char *p, PyObject *v, const formatdef *f)
499{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 long x;
501 unsigned short y;
502 if (get_long(v, &x) < 0)
503 return -1;
504 if (x < 0 || x > USHRT_MAX){
505 PyErr_SetString(StructError,
506 "ushort format requires 0 <= number <= " STRINGIFY(USHRT_MAX));
507 return -1;
508 }
509 y = (unsigned short)x;
510 memcpy(p, (char *)&y, sizeof y);
511 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000512}
513
514static int
515np_int(char *p, PyObject *v, const formatdef *f)
516{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000517 long x;
518 int y;
519 if (get_long(v, &x) < 0)
520 return -1;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000521#if (SIZEOF_LONG > SIZEOF_INT)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000522 if ((x < ((long)INT_MIN)) || (x > ((long)INT_MAX)))
523 RANGE_ERROR(x, f, 0, -1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000524#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000525 y = (int)x;
526 memcpy(p, (char *)&y, sizeof y);
527 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000528}
529
530static int
531np_uint(char *p, PyObject *v, const formatdef *f)
532{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000533 unsigned long x;
534 unsigned int y;
535 if (get_ulong(v, &x) < 0)
536 return -1;
537 y = (unsigned int)x;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000538#if (SIZEOF_LONG > SIZEOF_INT)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 if (x > ((unsigned long)UINT_MAX))
540 RANGE_ERROR(y, f, 1, -1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000541#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 memcpy(p, (char *)&y, sizeof y);
543 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000544}
545
546static int
547np_long(char *p, PyObject *v, const formatdef *f)
548{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 long x;
550 if (get_long(v, &x) < 0)
551 return -1;
552 memcpy(p, (char *)&x, sizeof x);
553 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000554}
555
556static int
557np_ulong(char *p, PyObject *v, const formatdef *f)
558{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 unsigned long x;
560 if (get_ulong(v, &x) < 0)
561 return -1;
562 memcpy(p, (char *)&x, sizeof x);
563 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000564}
565
566#ifdef HAVE_LONG_LONG
567
568static int
569np_longlong(char *p, PyObject *v, const formatdef *f)
570{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000571 PY_LONG_LONG x;
572 if (get_longlong(v, &x) < 0)
573 return -1;
574 memcpy(p, (char *)&x, sizeof x);
575 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000576}
577
578static int
579np_ulonglong(char *p, PyObject *v, const formatdef *f)
580{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 unsigned PY_LONG_LONG x;
582 if (get_ulonglong(v, &x) < 0)
583 return -1;
584 memcpy(p, (char *)&x, sizeof x);
585 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000586}
587#endif
588
Thomas Woutersb2137042007-02-01 18:02:27 +0000589
590static int
591np_bool(char *p, PyObject *v, const formatdef *f)
592{
Benjamin Petersonde73c452010-07-07 18:54:59 +0000593 int y;
594 BOOL_TYPE x;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000595 y = PyObject_IsTrue(v);
Benjamin Petersonde73c452010-07-07 18:54:59 +0000596 if (y < 0)
597 return -1;
598 x = y;
599 memcpy(p, (char *)&x, sizeof x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000600 return 0;
Thomas Woutersb2137042007-02-01 18:02:27 +0000601}
602
Thomas Wouters477c8d52006-05-27 19:21:47 +0000603static int
604np_float(char *p, PyObject *v, const formatdef *f)
605{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000606 float x = (float)PyFloat_AsDouble(v);
607 if (x == -1 && PyErr_Occurred()) {
608 PyErr_SetString(StructError,
609 "required argument is not a float");
610 return -1;
611 }
612 memcpy(p, (char *)&x, sizeof x);
613 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000614}
615
616static int
617np_double(char *p, PyObject *v, const formatdef *f)
618{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000619 double x = PyFloat_AsDouble(v);
620 if (x == -1 && PyErr_Occurred()) {
621 PyErr_SetString(StructError,
622 "required argument is not a float");
623 return -1;
624 }
625 memcpy(p, (char *)&x, sizeof(double));
626 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000627}
628
629static int
630np_void_p(char *p, PyObject *v, const formatdef *f)
631{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000632 void *x;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000633
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000634 v = get_pylong(v);
635 if (v == NULL)
636 return -1;
637 assert(PyLong_Check(v));
638 x = PyLong_AsVoidPtr(v);
639 Py_DECREF(v);
640 if (x == NULL && PyErr_Occurred())
641 return -1;
642 memcpy(p, (char *)&x, sizeof x);
643 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000644}
645
646static formatdef native_table[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000647 {'x', sizeof(char), 0, NULL},
648 {'b', sizeof(char), 0, nu_byte, np_byte},
649 {'B', sizeof(char), 0, nu_ubyte, np_ubyte},
650 {'c', sizeof(char), 0, nu_char, np_char},
651 {'s', sizeof(char), 0, NULL},
652 {'p', sizeof(char), 0, NULL},
653 {'h', sizeof(short), SHORT_ALIGN, nu_short, np_short},
654 {'H', sizeof(short), SHORT_ALIGN, nu_ushort, np_ushort},
655 {'i', sizeof(int), INT_ALIGN, nu_int, np_int},
656 {'I', sizeof(int), INT_ALIGN, nu_uint, np_uint},
657 {'l', sizeof(long), LONG_ALIGN, nu_long, np_long},
658 {'L', sizeof(long), LONG_ALIGN, nu_ulong, np_ulong},
Thomas Wouters477c8d52006-05-27 19:21:47 +0000659#ifdef HAVE_LONG_LONG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000660 {'q', sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_longlong, np_longlong},
661 {'Q', sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_ulonglong,np_ulonglong},
Thomas Wouters477c8d52006-05-27 19:21:47 +0000662#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000663 {'?', sizeof(BOOL_TYPE), BOOL_ALIGN, nu_bool, np_bool},
664 {'f', sizeof(float), FLOAT_ALIGN, nu_float, np_float},
665 {'d', sizeof(double), DOUBLE_ALIGN, nu_double, np_double},
666 {'P', sizeof(void *), VOID_P_ALIGN, nu_void_p, np_void_p},
667 {0}
Thomas Wouters477c8d52006-05-27 19:21:47 +0000668};
669
670/* Big-endian routines. *****************************************************/
671
672static PyObject *
673bu_int(const char *p, const formatdef *f)
674{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000675 long x = 0;
676 Py_ssize_t i = f->size;
677 const unsigned char *bytes = (const unsigned char *)p;
678 do {
679 x = (x<<8) | *bytes++;
680 } while (--i > 0);
681 /* Extend the sign bit. */
682 if (SIZEOF_LONG > f->size)
683 x |= -(x & (1L << ((8 * f->size) - 1)));
684 return PyLong_FromLong(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000685}
686
687static PyObject *
688bu_uint(const char *p, const formatdef *f)
689{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000690 unsigned long x = 0;
691 Py_ssize_t i = f->size;
692 const unsigned char *bytes = (const unsigned char *)p;
693 do {
694 x = (x<<8) | *bytes++;
695 } while (--i > 0);
696 if (x <= LONG_MAX)
697 return PyLong_FromLong((long)x);
698 return PyLong_FromUnsignedLong(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000699}
700
701static PyObject *
702bu_longlong(const char *p, const formatdef *f)
703{
704#ifdef HAVE_LONG_LONG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000705 PY_LONG_LONG x = 0;
706 Py_ssize_t i = f->size;
707 const unsigned char *bytes = (const unsigned char *)p;
708 do {
709 x = (x<<8) | *bytes++;
710 } while (--i > 0);
711 /* Extend the sign bit. */
712 if (SIZEOF_LONG_LONG > f->size)
713 x |= -(x & ((PY_LONG_LONG)1 << ((8 * f->size) - 1)));
714 if (x >= LONG_MIN && x <= LONG_MAX)
715 return PyLong_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
716 return PyLong_FromLongLong(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000717#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000718 return _PyLong_FromByteArray((const unsigned char *)p,
719 8,
720 0, /* little-endian */
721 1 /* signed */);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000722#endif
723}
724
725static PyObject *
726bu_ulonglong(const char *p, const formatdef *f)
727{
728#ifdef HAVE_LONG_LONG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000729 unsigned PY_LONG_LONG x = 0;
730 Py_ssize_t i = f->size;
731 const unsigned char *bytes = (const unsigned char *)p;
732 do {
733 x = (x<<8) | *bytes++;
734 } while (--i > 0);
735 if (x <= LONG_MAX)
736 return PyLong_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
737 return PyLong_FromUnsignedLongLong(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000738#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000739 return _PyLong_FromByteArray((const unsigned char *)p,
740 8,
741 0, /* little-endian */
742 0 /* signed */);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000743#endif
744}
745
746static PyObject *
747bu_float(const char *p, const formatdef *f)
748{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000749 return unpack_float(p, 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000750}
751
752static PyObject *
753bu_double(const char *p, const formatdef *f)
754{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000755 return unpack_double(p, 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000756}
757
Thomas Woutersb2137042007-02-01 18:02:27 +0000758static PyObject *
759bu_bool(const char *p, const formatdef *f)
760{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000761 char x;
762 memcpy((char *)&x, p, sizeof x);
763 return PyBool_FromLong(x != 0);
Thomas Woutersb2137042007-02-01 18:02:27 +0000764}
765
Thomas Wouters477c8d52006-05-27 19:21:47 +0000766static int
767bp_int(char *p, PyObject *v, const formatdef *f)
768{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 long x;
770 Py_ssize_t i;
771 if (get_long(v, &x) < 0)
772 return -1;
773 i = f->size;
774 if (i != SIZEOF_LONG) {
775 if ((i == 2) && (x < -32768 || x > 32767))
776 RANGE_ERROR(x, f, 0, 0xffffL);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000777#if (SIZEOF_LONG != 4)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000778 else if ((i == 4) && (x < -2147483648L || x > 2147483647L))
779 RANGE_ERROR(x, f, 0, 0xffffffffL);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000780#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000781 }
782 do {
783 p[--i] = (char)x;
784 x >>= 8;
785 } while (i > 0);
786 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000787}
788
789static int
790bp_uint(char *p, PyObject *v, const formatdef *f)
791{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 unsigned long x;
793 Py_ssize_t i;
794 if (get_ulong(v, &x) < 0)
795 return -1;
796 i = f->size;
797 if (i != SIZEOF_LONG) {
798 unsigned long maxint = 1;
799 maxint <<= (unsigned long)(i * 8);
800 if (x >= maxint)
801 RANGE_ERROR(x, f, 1, maxint - 1);
802 }
803 do {
804 p[--i] = (char)x;
805 x >>= 8;
806 } while (i > 0);
807 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000808}
809
810static int
811bp_longlong(char *p, PyObject *v, const formatdef *f)
812{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 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 1 /* signed */);
822 Py_DECREF(v);
823 return res;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000824}
825
826static int
827bp_ulonglong(char *p, PyObject *v, const formatdef *f)
828{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000829 int res;
830 v = get_pylong(v);
831 if (v == NULL)
832 return -1;
833 res = _PyLong_AsByteArray((PyLongObject *)v,
834 (unsigned char *)p,
835 8,
836 0, /* little_endian */
837 0 /* signed */);
838 Py_DECREF(v);
839 return res;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000840}
841
842static int
843bp_float(char *p, PyObject *v, const formatdef *f)
844{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000845 double x = PyFloat_AsDouble(v);
846 if (x == -1 && PyErr_Occurred()) {
847 PyErr_SetString(StructError,
848 "required argument is not a float");
849 return -1;
850 }
851 return _PyFloat_Pack4(x, (unsigned char *)p, 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000852}
853
854static int
855bp_double(char *p, PyObject *v, const formatdef *f)
856{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 double x = PyFloat_AsDouble(v);
858 if (x == -1 && PyErr_Occurred()) {
859 PyErr_SetString(StructError,
860 "required argument is not a float");
861 return -1;
862 }
863 return _PyFloat_Pack8(x, (unsigned char *)p, 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000864}
865
Thomas Woutersb2137042007-02-01 18:02:27 +0000866static int
867bp_bool(char *p, PyObject *v, const formatdef *f)
868{
Mark Dickinsoneff5d852010-07-18 07:29:02 +0000869 int y;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000870 y = PyObject_IsTrue(v);
Benjamin Petersonde73c452010-07-07 18:54:59 +0000871 if (y < 0)
872 return -1;
Mark Dickinsoneff5d852010-07-18 07:29:02 +0000873 *p = (char)y;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000874 return 0;
Thomas Woutersb2137042007-02-01 18:02:27 +0000875}
876
Thomas Wouters477c8d52006-05-27 19:21:47 +0000877static formatdef bigendian_table[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 {'x', 1, 0, NULL},
879 {'b', 1, 0, nu_byte, np_byte},
880 {'B', 1, 0, nu_ubyte, np_ubyte},
881 {'c', 1, 0, nu_char, np_char},
882 {'s', 1, 0, NULL},
883 {'p', 1, 0, NULL},
884 {'h', 2, 0, bu_int, bp_int},
885 {'H', 2, 0, bu_uint, bp_uint},
886 {'i', 4, 0, bu_int, bp_int},
887 {'I', 4, 0, bu_uint, bp_uint},
888 {'l', 4, 0, bu_int, bp_int},
889 {'L', 4, 0, bu_uint, bp_uint},
890 {'q', 8, 0, bu_longlong, bp_longlong},
891 {'Q', 8, 0, bu_ulonglong, bp_ulonglong},
892 {'?', 1, 0, bu_bool, bp_bool},
893 {'f', 4, 0, bu_float, bp_float},
894 {'d', 8, 0, bu_double, bp_double},
895 {0}
Thomas Wouters477c8d52006-05-27 19:21:47 +0000896};
897
898/* Little-endian routines. *****************************************************/
899
900static PyObject *
901lu_int(const char *p, const formatdef *f)
902{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000903 long x = 0;
904 Py_ssize_t i = f->size;
905 const unsigned char *bytes = (const unsigned char *)p;
906 do {
907 x = (x<<8) | bytes[--i];
908 } while (i > 0);
909 /* Extend the sign bit. */
910 if (SIZEOF_LONG > f->size)
911 x |= -(x & (1L << ((8 * f->size) - 1)));
912 return PyLong_FromLong(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000913}
914
915static PyObject *
916lu_uint(const char *p, const formatdef *f)
917{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 unsigned long x = 0;
919 Py_ssize_t i = f->size;
920 const unsigned char *bytes = (const unsigned char *)p;
921 do {
922 x = (x<<8) | bytes[--i];
923 } while (i > 0);
924 if (x <= LONG_MAX)
925 return PyLong_FromLong((long)x);
926 return PyLong_FromUnsignedLong((long)x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000927}
928
929static PyObject *
930lu_longlong(const char *p, const formatdef *f)
931{
932#ifdef HAVE_LONG_LONG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933 PY_LONG_LONG x = 0;
934 Py_ssize_t i = f->size;
935 const unsigned char *bytes = (const unsigned char *)p;
936 do {
937 x = (x<<8) | bytes[--i];
938 } while (i > 0);
939 /* Extend the sign bit. */
940 if (SIZEOF_LONG_LONG > f->size)
941 x |= -(x & ((PY_LONG_LONG)1 << ((8 * f->size) - 1)));
942 if (x >= LONG_MIN && x <= LONG_MAX)
943 return PyLong_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
944 return PyLong_FromLongLong(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000945#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000946 return _PyLong_FromByteArray((const unsigned char *)p,
947 8,
948 1, /* little-endian */
949 1 /* signed */);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000950#endif
951}
952
953static PyObject *
954lu_ulonglong(const char *p, const formatdef *f)
955{
956#ifdef HAVE_LONG_LONG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000957 unsigned PY_LONG_LONG x = 0;
958 Py_ssize_t i = f->size;
959 const unsigned char *bytes = (const unsigned char *)p;
960 do {
961 x = (x<<8) | bytes[--i];
962 } while (i > 0);
963 if (x <= LONG_MAX)
964 return PyLong_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
965 return PyLong_FromUnsignedLongLong(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000966#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000967 return _PyLong_FromByteArray((const unsigned char *)p,
968 8,
969 1, /* little-endian */
970 0 /* signed */);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000971#endif
972}
973
974static PyObject *
975lu_float(const char *p, const formatdef *f)
976{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977 return unpack_float(p, 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000978}
979
980static PyObject *
981lu_double(const char *p, const formatdef *f)
982{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000983 return unpack_double(p, 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000984}
985
986static int
987lp_int(char *p, PyObject *v, const formatdef *f)
988{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000989 long x;
990 Py_ssize_t i;
991 if (get_long(v, &x) < 0)
992 return -1;
993 i = f->size;
994 if (i != SIZEOF_LONG) {
995 if ((i == 2) && (x < -32768 || x > 32767))
996 RANGE_ERROR(x, f, 0, 0xffffL);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000997#if (SIZEOF_LONG != 4)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000998 else if ((i == 4) && (x < -2147483648L || x > 2147483647L))
999 RANGE_ERROR(x, f, 0, 0xffffffffL);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001000#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 }
1002 do {
1003 *p++ = (char)x;
1004 x >>= 8;
1005 } while (--i > 0);
1006 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001007}
1008
1009static int
1010lp_uint(char *p, PyObject *v, const formatdef *f)
1011{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 unsigned long x;
1013 Py_ssize_t i;
1014 if (get_ulong(v, &x) < 0)
1015 return -1;
1016 i = f->size;
1017 if (i != SIZEOF_LONG) {
1018 unsigned long maxint = 1;
1019 maxint <<= (unsigned long)(i * 8);
1020 if (x >= maxint)
1021 RANGE_ERROR(x, f, 1, maxint - 1);
1022 }
1023 do {
1024 *p++ = (char)x;
1025 x >>= 8;
1026 } while (--i > 0);
1027 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001028}
1029
1030static int
1031lp_longlong(char *p, PyObject *v, const formatdef *f)
1032{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 int res;
1034 v = get_pylong(v);
1035 if (v == NULL)
1036 return -1;
1037 res = _PyLong_AsByteArray((PyLongObject*)v,
1038 (unsigned char *)p,
1039 8,
1040 1, /* little_endian */
1041 1 /* signed */);
1042 Py_DECREF(v);
1043 return res;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001044}
1045
1046static int
1047lp_ulonglong(char *p, PyObject *v, const formatdef *f)
1048{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001049 int res;
1050 v = get_pylong(v);
1051 if (v == NULL)
1052 return -1;
1053 res = _PyLong_AsByteArray((PyLongObject*)v,
1054 (unsigned char *)p,
1055 8,
1056 1, /* little_endian */
1057 0 /* signed */);
1058 Py_DECREF(v);
1059 return res;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001060}
1061
1062static int
1063lp_float(char *p, PyObject *v, const formatdef *f)
1064{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001065 double x = PyFloat_AsDouble(v);
1066 if (x == -1 && PyErr_Occurred()) {
1067 PyErr_SetString(StructError,
1068 "required argument is not a float");
1069 return -1;
1070 }
1071 return _PyFloat_Pack4(x, (unsigned char *)p, 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001072}
1073
1074static int
1075lp_double(char *p, PyObject *v, const formatdef *f)
1076{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001077 double x = PyFloat_AsDouble(v);
1078 if (x == -1 && PyErr_Occurred()) {
1079 PyErr_SetString(StructError,
1080 "required argument is not a float");
1081 return -1;
1082 }
1083 return _PyFloat_Pack8(x, (unsigned char *)p, 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001084}
1085
1086static formatdef lilendian_table[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001087 {'x', 1, 0, NULL},
1088 {'b', 1, 0, nu_byte, np_byte},
1089 {'B', 1, 0, nu_ubyte, np_ubyte},
1090 {'c', 1, 0, nu_char, np_char},
1091 {'s', 1, 0, NULL},
1092 {'p', 1, 0, NULL},
1093 {'h', 2, 0, lu_int, lp_int},
1094 {'H', 2, 0, lu_uint, lp_uint},
1095 {'i', 4, 0, lu_int, lp_int},
1096 {'I', 4, 0, lu_uint, lp_uint},
1097 {'l', 4, 0, lu_int, lp_int},
1098 {'L', 4, 0, lu_uint, lp_uint},
1099 {'q', 8, 0, lu_longlong, lp_longlong},
1100 {'Q', 8, 0, lu_ulonglong, lp_ulonglong},
1101 {'?', 1, 0, bu_bool, bp_bool}, /* Std rep not endian dep,
1102 but potentially different from native rep -- reuse bx_bool funcs. */
1103 {'f', 4, 0, lu_float, lp_float},
1104 {'d', 8, 0, lu_double, lp_double},
1105 {0}
Thomas Wouters477c8d52006-05-27 19:21:47 +00001106};
1107
1108
1109static const formatdef *
1110whichtable(char **pfmt)
1111{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001112 const char *fmt = (*pfmt)++; /* May be backed out of later */
1113 switch (*fmt) {
1114 case '<':
1115 return lilendian_table;
1116 case '>':
1117 case '!': /* Network byte order is big-endian */
1118 return bigendian_table;
1119 case '=': { /* Host byte order -- different from native in aligment! */
1120 int n = 1;
1121 char *p = (char *) &n;
1122 if (*p == 1)
1123 return lilendian_table;
1124 else
1125 return bigendian_table;
1126 }
1127 default:
1128 --*pfmt; /* Back out of pointer increment */
1129 /* Fall through */
1130 case '@':
1131 return native_table;
1132 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001133}
1134
1135
1136/* Get the table entry for a format code */
1137
1138static const formatdef *
1139getentry(int c, const formatdef *f)
1140{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001141 for (; f->format != '\0'; f++) {
1142 if (f->format == c) {
1143 return f;
1144 }
1145 }
1146 PyErr_SetString(StructError, "bad char in struct format");
1147 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001148}
1149
1150
Mark Dickinsonb72e6862010-06-11 19:50:30 +00001151/* Align a size according to a format code. Return -1 on overflow. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001152
Mark Dickinsoneac0e682010-06-11 19:05:08 +00001153static Py_ssize_t
Thomas Wouters477c8d52006-05-27 19:21:47 +00001154align(Py_ssize_t size, char c, const formatdef *e)
1155{
Mark Dickinsonb72e6862010-06-11 19:50:30 +00001156 Py_ssize_t extra;
1157
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001158 if (e->format == c) {
Mark Dickinsonb72e6862010-06-11 19:50:30 +00001159 if (e->alignment && size > 0) {
1160 extra = (e->alignment - 1) - (size - 1) % (e->alignment);
1161 if (extra > PY_SSIZE_T_MAX - size)
1162 return -1;
1163 size += extra;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001164 }
1165 }
1166 return size;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001167}
1168
1169
1170/* calculate the size of a format string */
1171
1172static int
1173prepare_s(PyStructObject *self)
1174{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001175 const formatdef *f;
1176 const formatdef *e;
1177 formatcode *codes;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001178
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001179 const char *s;
1180 const char *fmt;
1181 char c;
Mark Dickinsonb72e6862010-06-11 19:50:30 +00001182 Py_ssize_t size, len, num, itemsize;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001183
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001184 fmt = PyBytes_AS_STRING(self->s_format);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001185
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001186 f = whichtable((char **)&fmt);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001187
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001188 s = fmt;
1189 size = 0;
1190 len = 0;
1191 while ((c = *s++) != '\0') {
1192 if (isspace(Py_CHARMASK(c)))
1193 continue;
1194 if ('0' <= c && c <= '9') {
1195 num = c - '0';
1196 while ('0' <= (c = *s++) && c <= '9') {
Mark Dickinsonab4096f2010-06-11 16:56:34 +00001197 /* overflow-safe version of
1198 if (num*10 + (c - '0') > PY_SSIZE_T_MAX) { ... } */
1199 if (num >= PY_SSIZE_T_MAX / 10 && (
1200 num > PY_SSIZE_T_MAX / 10 ||
Mark Dickinsonb72e6862010-06-11 19:50:30 +00001201 (c - '0') > PY_SSIZE_T_MAX % 10))
1202 goto overflow;
Mark Dickinsonab4096f2010-06-11 16:56:34 +00001203 num = num*10 + (c - '0');
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001204 }
Alexander Belopolsky177e8532010-06-11 16:04:59 +00001205 if (c == '\0') {
1206 PyErr_SetString(StructError,
1207 "repeat count given without format specifier");
1208 return -1;
1209 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001210 }
1211 else
1212 num = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001213
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001214 e = getentry(c, f);
1215 if (e == NULL)
1216 return -1;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001217
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001218 switch (c) {
1219 case 's': /* fall through */
1220 case 'p': len++; break;
1221 case 'x': break;
1222 default: len += num; break;
1223 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001224
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001225 itemsize = e->size;
1226 size = align(size, c, e);
Mark Dickinsonb72e6862010-06-11 19:50:30 +00001227 if (size == -1)
1228 goto overflow;
1229
1230 /* if (size + num * itemsize > PY_SSIZE_T_MAX) { ... } */
1231 if (num > (PY_SSIZE_T_MAX - size) / itemsize)
1232 goto overflow;
1233 size += num * itemsize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001234 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001235
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001236 /* check for overflow */
1237 if ((len + 1) > (PY_SSIZE_T_MAX / sizeof(formatcode))) {
1238 PyErr_NoMemory();
1239 return -1;
1240 }
Amaury Forgeot d'Arc35c86582008-06-17 21:11:29 +00001241
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001242 self->s_size = size;
1243 self->s_len = len;
1244 codes = PyMem_MALLOC((len + 1) * sizeof(formatcode));
1245 if (codes == NULL) {
1246 PyErr_NoMemory();
1247 return -1;
1248 }
Mark Dickinsoncf28b952010-07-29 21:41:59 +00001249 /* Free any s_codes value left over from a previous initialization. */
1250 if (self->s_codes != NULL)
1251 PyMem_FREE(self->s_codes);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001252 self->s_codes = codes;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001253
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001254 s = fmt;
1255 size = 0;
1256 while ((c = *s++) != '\0') {
1257 if (isspace(Py_CHARMASK(c)))
1258 continue;
1259 if ('0' <= c && c <= '9') {
1260 num = c - '0';
1261 while ('0' <= (c = *s++) && c <= '9')
1262 num = num*10 + (c - '0');
1263 if (c == '\0')
1264 break;
1265 }
1266 else
1267 num = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001268
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 e = getentry(c, f);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001270
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001271 size = align(size, c, e);
1272 if (c == 's' || c == 'p') {
1273 codes->offset = size;
1274 codes->size = num;
1275 codes->fmtdef = e;
1276 codes++;
1277 size += num;
1278 } else if (c == 'x') {
1279 size += num;
1280 } else {
1281 while (--num >= 0) {
1282 codes->offset = size;
1283 codes->size = e->size;
1284 codes->fmtdef = e;
1285 codes++;
1286 size += e->size;
1287 }
1288 }
1289 }
1290 codes->fmtdef = NULL;
1291 codes->offset = size;
1292 codes->size = 0;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001293
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001294 return 0;
Mark Dickinsonb72e6862010-06-11 19:50:30 +00001295
1296 overflow:
1297 PyErr_SetString(StructError,
1298 "total struct size too long");
1299 return -1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001300}
1301
1302static PyObject *
1303s_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1304{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001305 PyObject *self;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001306
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001307 assert(type != NULL && type->tp_alloc != NULL);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001308
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001309 self = type->tp_alloc(type, 0);
1310 if (self != NULL) {
1311 PyStructObject *s = (PyStructObject*)self;
1312 Py_INCREF(Py_None);
1313 s->s_format = Py_None;
1314 s->s_codes = NULL;
1315 s->s_size = -1;
1316 s->s_len = -1;
1317 }
1318 return self;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001319}
1320
1321static int
1322s_init(PyObject *self, PyObject *args, PyObject *kwds)
1323{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001324 PyStructObject *soself = (PyStructObject *)self;
1325 PyObject *o_format = NULL;
1326 int ret = 0;
1327 static char *kwlist[] = {"format", 0};
Thomas Wouters477c8d52006-05-27 19:21:47 +00001328
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001329 assert(PyStruct_Check(self));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001330
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001331 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:Struct", kwlist,
1332 &o_format))
1333 return -1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001334
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001335 if (PyUnicode_Check(o_format)) {
1336 o_format = PyUnicode_AsASCIIString(o_format);
1337 if (o_format == NULL)
1338 return -1;
1339 }
1340 /* XXX support buffer interface, too */
1341 else {
1342 Py_INCREF(o_format);
1343 }
Christian Heimesa34706f2008-01-04 03:06:10 +00001344
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001345 if (!PyBytes_Check(o_format)) {
1346 Py_DECREF(o_format);
1347 PyErr_Format(PyExc_TypeError,
1348 "Struct() argument 1 must be bytes, not %.200s",
1349 Py_TYPE(o_format)->tp_name);
1350 return -1;
1351 }
Christian Heimesa34706f2008-01-04 03:06:10 +00001352
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001353 Py_CLEAR(soself->s_format);
1354 soself->s_format = o_format;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001355
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001356 ret = prepare_s(soself);
1357 return ret;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001358}
1359
1360static void
1361s_dealloc(PyStructObject *s)
1362{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001363 if (s->weakreflist != NULL)
1364 PyObject_ClearWeakRefs((PyObject *)s);
1365 if (s->s_codes != NULL) {
1366 PyMem_FREE(s->s_codes);
1367 }
1368 Py_XDECREF(s->s_format);
1369 Py_TYPE(s)->tp_free((PyObject *)s);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001370}
1371
1372static PyObject *
1373s_unpack_internal(PyStructObject *soself, char *startfrom) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001374 formatcode *code;
1375 Py_ssize_t i = 0;
1376 PyObject *result = PyTuple_New(soself->s_len);
1377 if (result == NULL)
1378 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001379
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001380 for (code = soself->s_codes; code->fmtdef != NULL; code++) {
1381 PyObject *v;
1382 const formatdef *e = code->fmtdef;
1383 const char *res = startfrom + code->offset;
1384 if (e->format == 's') {
1385 v = PyBytes_FromStringAndSize(res, code->size);
1386 } else if (e->format == 'p') {
1387 Py_ssize_t n = *(unsigned char*)res;
1388 if (n >= code->size)
1389 n = code->size - 1;
1390 v = PyBytes_FromStringAndSize(res + 1, n);
1391 } else {
1392 v = e->unpack(res, e);
1393 }
1394 if (v == NULL)
1395 goto fail;
1396 PyTuple_SET_ITEM(result, i++, v);
1397 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001398
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001399 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001400fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001401 Py_DECREF(result);
1402 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001403}
1404
1405
1406PyDoc_STRVAR(s_unpack__doc__,
Guido van Rossum913dd0b2007-04-13 03:33:53 +00001407"S.unpack(buffer) -> (v1, v2, ...)\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +00001408\n\
Mark Dickinsonaacfa952010-06-12 15:43:45 +00001409Return a tuple containing values unpacked according to the format\n\
1410string S.format. Requires len(buffer) == S.size. See help(struct)\n\
1411for more on format strings.");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001412
1413static PyObject *
Guido van Rossum98297ee2007-11-06 21:34:58 +00001414s_unpack(PyObject *self, PyObject *input)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001415{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001416 Py_buffer vbuf;
1417 PyObject *result;
1418 PyStructObject *soself = (PyStructObject *)self;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001419
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001420 assert(PyStruct_Check(self));
1421 assert(soself->s_codes != NULL);
1422 if (PyObject_GetBuffer(input, &vbuf, PyBUF_SIMPLE) < 0)
1423 return NULL;
1424 if (vbuf.len != soself->s_size) {
1425 PyErr_Format(StructError,
1426 "unpack requires a bytes argument of length %zd",
1427 soself->s_size);
1428 PyBuffer_Release(&vbuf);
1429 return NULL;
1430 }
1431 result = s_unpack_internal(soself, vbuf.buf);
1432 PyBuffer_Release(&vbuf);
1433 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001434}
1435
1436PyDoc_STRVAR(s_unpack_from__doc__,
Mark Dickinsonc6f13962010-06-13 09:17:13 +00001437"S.unpack_from(buffer, offset=0) -> (v1, v2, ...)\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +00001438\n\
Mark Dickinsonaacfa952010-06-12 15:43:45 +00001439Return a tuple containing values unpacked according to the format\n\
1440string S.format. Requires len(buffer[offset:]) >= S.size. See\n\
1441help(struct) for more on format strings.");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001442
1443static PyObject *
1444s_unpack_from(PyObject *self, PyObject *args, PyObject *kwds)
1445{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001446 static char *kwlist[] = {"buffer", "offset", 0};
Guido van Rossum98297ee2007-11-06 21:34:58 +00001447
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001448 PyObject *input;
1449 Py_ssize_t offset = 0;
1450 Py_buffer vbuf;
1451 PyObject *result;
1452 PyStructObject *soself = (PyStructObject *)self;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001453
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001454 assert(PyStruct_Check(self));
1455 assert(soself->s_codes != NULL);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001456
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001457 if (!PyArg_ParseTupleAndKeywords(args, kwds,
1458 "O|n:unpack_from", kwlist,
1459 &input, &offset))
1460 return NULL;
1461 if (PyObject_GetBuffer(input, &vbuf, PyBUF_SIMPLE) < 0)
1462 return NULL;
1463 if (offset < 0)
1464 offset += vbuf.len;
1465 if (offset < 0 || vbuf.len - offset < soself->s_size) {
1466 PyErr_Format(StructError,
1467 "unpack_from requires a buffer of at least %zd bytes",
1468 soself->s_size);
1469 PyBuffer_Release(&vbuf);
1470 return NULL;
1471 }
1472 result = s_unpack_internal(soself, (char*)vbuf.buf + offset);
1473 PyBuffer_Release(&vbuf);
1474 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001475}
1476
1477
1478/*
1479 * Guts of the pack function.
1480 *
1481 * Takes a struct object, a tuple of arguments, and offset in that tuple of
1482 * argument for where to start processing the arguments for packing, and a
1483 * character buffer for writing the packed string. The caller must insure
1484 * that the buffer may contain the required length for packing the arguments.
1485 * 0 is returned on success, 1 is returned if there is an error.
1486 *
1487 */
1488static int
1489s_pack_internal(PyStructObject *soself, PyObject *args, int offset, char* buf)
1490{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001491 formatcode *code;
1492 /* XXX(nnorwitz): why does i need to be a local? can we use
1493 the offset parameter or do we need the wider width? */
1494 Py_ssize_t i;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001495
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001496 memset(buf, '\0', soself->s_size);
1497 i = offset;
1498 for (code = soself->s_codes; code->fmtdef != NULL; code++) {
1499 Py_ssize_t n;
1500 PyObject *v = PyTuple_GET_ITEM(args, i++);
1501 const formatdef *e = code->fmtdef;
1502 char *res = buf + code->offset;
1503 if (e->format == 's') {
1504 int isstring;
1505 void *p;
1506 if (PyUnicode_Check(v)) {
1507 v = _PyUnicode_AsDefaultEncodedString(v, NULL);
1508 if (v == NULL)
1509 return -1;
1510 }
1511 isstring = PyBytes_Check(v);
1512 if (!isstring && !PyByteArray_Check(v)) {
1513 PyErr_SetString(StructError,
1514 "argument for 's' must be a bytes or string");
1515 return -1;
1516 }
1517 if (isstring) {
1518 n = PyBytes_GET_SIZE(v);
1519 p = PyBytes_AS_STRING(v);
1520 }
1521 else {
1522 n = PyByteArray_GET_SIZE(v);
1523 p = PyByteArray_AS_STRING(v);
1524 }
1525 if (n > code->size)
1526 n = code->size;
1527 if (n > 0)
1528 memcpy(res, p, n);
1529 } else if (e->format == 'p') {
1530 int isstring;
1531 void *p;
1532 if (PyUnicode_Check(v)) {
1533 v = _PyUnicode_AsDefaultEncodedString(v, NULL);
1534 if (v == NULL)
1535 return -1;
1536 }
1537 isstring = PyBytes_Check(v);
1538 if (!isstring && !PyByteArray_Check(v)) {
1539 PyErr_SetString(StructError,
1540 "argument for 'p' must be a bytes or string");
1541 return -1;
1542 }
1543 if (isstring) {
1544 n = PyBytes_GET_SIZE(v);
1545 p = PyBytes_AS_STRING(v);
1546 }
1547 else {
1548 n = PyByteArray_GET_SIZE(v);
1549 p = PyByteArray_AS_STRING(v);
1550 }
1551 if (n > (code->size - 1))
1552 n = code->size - 1;
1553 if (n > 0)
1554 memcpy(res + 1, p, n);
1555 if (n > 255)
1556 n = 255;
1557 *res = Py_SAFE_DOWNCAST(n, Py_ssize_t, unsigned char);
1558 } else {
1559 if (e->pack(res, v, e) < 0) {
1560 if (PyLong_Check(v) && PyErr_ExceptionMatches(PyExc_OverflowError))
1561 PyErr_SetString(StructError,
1562 "long too large to convert to int");
1563 return -1;
1564 }
1565 }
1566 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001567
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001568 /* Success */
1569 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001570}
1571
1572
1573PyDoc_STRVAR(s_pack__doc__,
Benjamin Peterson4ae19462008-07-31 15:03:40 +00001574"S.pack(v1, v2, ...) -> bytes\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +00001575\n\
Mark Dickinsonaacfa952010-06-12 15:43:45 +00001576Return a bytes object containing values v1, v2, ... packed according\n\
1577to the format string S.format. See help(struct) for more on format\n\
1578strings.");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001579
1580static PyObject *
1581s_pack(PyObject *self, PyObject *args)
1582{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001583 PyStructObject *soself;
1584 PyObject *result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001585
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001586 /* Validate arguments. */
1587 soself = (PyStructObject *)self;
1588 assert(PyStruct_Check(self));
1589 assert(soself->s_codes != NULL);
1590 if (PyTuple_GET_SIZE(args) != soself->s_len)
1591 {
1592 PyErr_Format(StructError,
1593 "pack requires exactly %zd arguments", soself->s_len);
1594 return NULL;
1595 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001596
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001597 /* Allocate a new string */
1598 result = PyBytes_FromStringAndSize((char *)NULL, soself->s_size);
1599 if (result == NULL)
1600 return NULL;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001601
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001602 /* Call the guts */
1603 if ( s_pack_internal(soself, args, 0, PyBytes_AS_STRING(result)) != 0 ) {
1604 Py_DECREF(result);
1605 return NULL;
1606 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001607
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001608 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001609}
1610
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001611PyDoc_STRVAR(s_pack_into__doc__,
1612"S.pack_into(buffer, offset, v1, v2, ...)\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +00001613\n\
Mark Dickinsonaacfa952010-06-12 15:43:45 +00001614Pack the values v1, v2, ... according to the format string S.format\n\
1615and write the packed bytes into the writable buffer buf starting at\n\
Mark Dickinsonfdb99f12010-06-12 16:30:53 +00001616offset. Note that the offset is a required argument. See\n\
Mark Dickinsonaacfa952010-06-12 15:43:45 +00001617help(struct) for more on format strings.");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001618
1619static PyObject *
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001620s_pack_into(PyObject *self, PyObject *args)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001621{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001622 PyStructObject *soself;
1623 char *buffer;
1624 Py_ssize_t buffer_len, offset;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001625
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001626 /* Validate arguments. +1 is for the first arg as buffer. */
1627 soself = (PyStructObject *)self;
1628 assert(PyStruct_Check(self));
1629 assert(soself->s_codes != NULL);
1630 if (PyTuple_GET_SIZE(args) != (soself->s_len + 2))
1631 {
1632 PyErr_Format(StructError,
1633 "pack_into requires exactly %zd arguments",
1634 (soself->s_len + 2));
1635 return NULL;
1636 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001637
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001638 /* Extract a writable memory buffer from the first argument */
1639 if ( PyObject_AsWriteBuffer(PyTuple_GET_ITEM(args, 0),
1640 (void**)&buffer, &buffer_len) == -1 ) {
1641 return NULL;
1642 }
1643 assert( buffer_len >= 0 );
Thomas Wouters477c8d52006-05-27 19:21:47 +00001644
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001645 /* Extract the offset from the first argument */
1646 offset = PyNumber_AsSsize_t(PyTuple_GET_ITEM(args, 1), PyExc_IndexError);
1647 if (offset == -1 && PyErr_Occurred())
1648 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001649
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001650 /* Support negative offsets. */
1651 if (offset < 0)
1652 offset += buffer_len;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001653
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001654 /* Check boundaries */
1655 if (offset < 0 || (buffer_len - offset) < soself->s_size) {
1656 PyErr_Format(StructError,
1657 "pack_into requires a buffer of at least %zd bytes",
1658 soself->s_size);
1659 return NULL;
1660 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001661
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001662 /* Call the guts */
1663 if ( s_pack_internal(soself, args, 2, buffer + offset) != 0 ) {
1664 return NULL;
1665 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001666
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001667 Py_RETURN_NONE;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001668}
1669
1670static PyObject *
1671s_get_format(PyStructObject *self, void *unused)
1672{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001673 Py_INCREF(self->s_format);
1674 return self->s_format;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001675}
1676
1677static PyObject *
1678s_get_size(PyStructObject *self, void *unused)
1679{
Christian Heimes217cfd12007-12-02 14:31:20 +00001680 return PyLong_FromSsize_t(self->s_size);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001681}
1682
1683/* List of functions */
1684
1685static struct PyMethodDef s_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001686 {"pack", s_pack, METH_VARARGS, s_pack__doc__},
1687 {"pack_into", s_pack_into, METH_VARARGS, s_pack_into__doc__},
1688 {"unpack", s_unpack, METH_O, s_unpack__doc__},
1689 {"unpack_from", (PyCFunction)s_unpack_from, METH_VARARGS|METH_KEYWORDS,
1690 s_unpack_from__doc__},
1691 {NULL, NULL} /* sentinel */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001692};
1693
Alexander Belopolsky0bd003a2010-06-12 19:36:28 +00001694PyDoc_STRVAR(s__doc__,
1695"Struct(fmt) --> compiled struct object\n"
1696"\n"
1697"Return a new Struct object which writes and reads binary data according to\n"
1698"the format string fmt. See help(struct) for more on format strings.");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001699
1700#define OFF(x) offsetof(PyStructObject, x)
1701
1702static PyGetSetDef s_getsetlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001703 {"format", (getter)s_get_format, (setter)NULL, "struct format string", NULL},
1704 {"size", (getter)s_get_size, (setter)NULL, "struct size in bytes", NULL},
1705 {NULL} /* sentinel */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001706};
1707
1708static
1709PyTypeObject PyStructType = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001710 PyVarObject_HEAD_INIT(NULL, 0)
1711 "Struct",
1712 sizeof(PyStructObject),
1713 0,
1714 (destructor)s_dealloc, /* tp_dealloc */
1715 0, /* tp_print */
1716 0, /* tp_getattr */
1717 0, /* tp_setattr */
1718 0, /* tp_reserved */
1719 0, /* tp_repr */
1720 0, /* tp_as_number */
1721 0, /* tp_as_sequence */
1722 0, /* tp_as_mapping */
1723 0, /* tp_hash */
1724 0, /* tp_call */
1725 0, /* tp_str */
1726 PyObject_GenericGetAttr, /* tp_getattro */
1727 PyObject_GenericSetAttr, /* tp_setattro */
1728 0, /* tp_as_buffer */
1729 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
1730 s__doc__, /* tp_doc */
1731 0, /* tp_traverse */
1732 0, /* tp_clear */
1733 0, /* tp_richcompare */
1734 offsetof(PyStructObject, weakreflist), /* tp_weaklistoffset */
1735 0, /* tp_iter */
1736 0, /* tp_iternext */
1737 s_methods, /* tp_methods */
1738 NULL, /* tp_members */
1739 s_getsetlist, /* tp_getset */
1740 0, /* tp_base */
1741 0, /* tp_dict */
1742 0, /* tp_descr_get */
1743 0, /* tp_descr_set */
1744 0, /* tp_dictoffset */
1745 s_init, /* tp_init */
1746 PyType_GenericAlloc,/* tp_alloc */
1747 s_new, /* tp_new */
1748 PyObject_Del, /* tp_free */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001749};
1750
Christian Heimesa34706f2008-01-04 03:06:10 +00001751
1752/* ---- Standalone functions ---- */
1753
1754#define MAXCACHE 100
1755static PyObject *cache = NULL;
1756
1757static PyObject *
1758cache_struct(PyObject *fmt)
1759{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001760 PyObject * s_object;
Christian Heimesa34706f2008-01-04 03:06:10 +00001761
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001762 if (cache == NULL) {
1763 cache = PyDict_New();
1764 if (cache == NULL)
1765 return NULL;
1766 }
Christian Heimesa34706f2008-01-04 03:06:10 +00001767
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001768 s_object = PyDict_GetItem(cache, fmt);
1769 if (s_object != NULL) {
1770 Py_INCREF(s_object);
1771 return s_object;
1772 }
Christian Heimesa34706f2008-01-04 03:06:10 +00001773
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001774 s_object = PyObject_CallFunctionObjArgs((PyObject *)(&PyStructType), fmt, NULL);
1775 if (s_object != NULL) {
1776 if (PyDict_Size(cache) >= MAXCACHE)
1777 PyDict_Clear(cache);
1778 /* Attempt to cache the result */
1779 if (PyDict_SetItem(cache, fmt, s_object) == -1)
1780 PyErr_Clear();
1781 }
1782 return s_object;
Christian Heimesa34706f2008-01-04 03:06:10 +00001783}
1784
1785PyDoc_STRVAR(clearcache_doc,
1786"Clear the internal cache.");
1787
1788static PyObject *
1789clearcache(PyObject *self)
1790{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001791 Py_CLEAR(cache);
1792 Py_RETURN_NONE;
Christian Heimesa34706f2008-01-04 03:06:10 +00001793}
1794
1795PyDoc_STRVAR(calcsize_doc,
Mark Dickinsonaacfa952010-06-12 15:43:45 +00001796"calcsize(fmt) -> integer\n\
1797\n\
1798Return size in bytes of the struct described by the format string fmt.");
Christian Heimesa34706f2008-01-04 03:06:10 +00001799
1800static PyObject *
1801calcsize(PyObject *self, PyObject *fmt)
1802{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001803 Py_ssize_t n;
1804 PyObject *s_object = cache_struct(fmt);
1805 if (s_object == NULL)
1806 return NULL;
1807 n = ((PyStructObject *)s_object)->s_size;
1808 Py_DECREF(s_object);
1809 return PyLong_FromSsize_t(n);
Christian Heimesa34706f2008-01-04 03:06:10 +00001810}
1811
1812PyDoc_STRVAR(pack_doc,
Mark Dickinsonaacfa952010-06-12 15:43:45 +00001813"pack(fmt, v1, v2, ...) -> bytes\n\
1814\n\
Mark Dickinsonfdb99f12010-06-12 16:30:53 +00001815Return a bytes object containing the values v1, v2, ... packed according\n\
1816to the format string fmt. See help(struct) for more on format strings.");
Christian Heimesa34706f2008-01-04 03:06:10 +00001817
1818static PyObject *
1819pack(PyObject *self, PyObject *args)
1820{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001821 PyObject *s_object, *fmt, *newargs, *result;
1822 Py_ssize_t n = PyTuple_GET_SIZE(args);
Christian Heimesa34706f2008-01-04 03:06:10 +00001823
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001824 if (n == 0) {
1825 PyErr_SetString(PyExc_TypeError, "missing format argument");
1826 return NULL;
1827 }
1828 fmt = PyTuple_GET_ITEM(args, 0);
1829 newargs = PyTuple_GetSlice(args, 1, n);
1830 if (newargs == NULL)
1831 return NULL;
Christian Heimesa34706f2008-01-04 03:06:10 +00001832
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001833 s_object = cache_struct(fmt);
1834 if (s_object == NULL) {
1835 Py_DECREF(newargs);
1836 return NULL;
1837 }
1838 result = s_pack(s_object, newargs);
1839 Py_DECREF(newargs);
1840 Py_DECREF(s_object);
1841 return result;
Christian Heimesa34706f2008-01-04 03:06:10 +00001842}
1843
1844PyDoc_STRVAR(pack_into_doc,
Mark Dickinsonaacfa952010-06-12 15:43:45 +00001845"pack_into(fmt, buffer, offset, v1, v2, ...)\n\
1846\n\
1847Pack the values v1, v2, ... according to the format string fmt and write\n\
1848the packed bytes into the writable buffer buf starting at offset. Note\n\
Mark Dickinsonfdb99f12010-06-12 16:30:53 +00001849that the offset is a required argument. See help(struct) for more\n\
Mark Dickinsonaacfa952010-06-12 15:43:45 +00001850on format strings.");
Christian Heimesa34706f2008-01-04 03:06:10 +00001851
1852static PyObject *
1853pack_into(PyObject *self, PyObject *args)
1854{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001855 PyObject *s_object, *fmt, *newargs, *result;
1856 Py_ssize_t n = PyTuple_GET_SIZE(args);
Christian Heimesa34706f2008-01-04 03:06:10 +00001857
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001858 if (n == 0) {
1859 PyErr_SetString(PyExc_TypeError, "missing format argument");
1860 return NULL;
1861 }
1862 fmt = PyTuple_GET_ITEM(args, 0);
1863 newargs = PyTuple_GetSlice(args, 1, n);
1864 if (newargs == NULL)
1865 return NULL;
Christian Heimesa34706f2008-01-04 03:06:10 +00001866
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001867 s_object = cache_struct(fmt);
1868 if (s_object == NULL) {
1869 Py_DECREF(newargs);
1870 return NULL;
1871 }
1872 result = s_pack_into(s_object, newargs);
1873 Py_DECREF(newargs);
1874 Py_DECREF(s_object);
1875 return result;
Christian Heimesa34706f2008-01-04 03:06:10 +00001876}
1877
1878PyDoc_STRVAR(unpack_doc,
Mark Dickinsonaacfa952010-06-12 15:43:45 +00001879"unpack(fmt, buffer) -> (v1, v2, ...)\n\
1880\n\
1881Return a tuple containing values unpacked according to the format string\n\
1882fmt. Requires len(buffer) == calcsize(fmt). See help(struct) for more\n\
1883on format strings.");
Christian Heimesa34706f2008-01-04 03:06:10 +00001884
1885static PyObject *
1886unpack(PyObject *self, PyObject *args)
1887{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001888 PyObject *s_object, *fmt, *inputstr, *result;
Christian Heimesa34706f2008-01-04 03:06:10 +00001889
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001890 if (!PyArg_UnpackTuple(args, "unpack", 2, 2, &fmt, &inputstr))
1891 return NULL;
Christian Heimesa34706f2008-01-04 03:06:10 +00001892
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001893 s_object = cache_struct(fmt);
1894 if (s_object == NULL)
1895 return NULL;
1896 result = s_unpack(s_object, inputstr);
1897 Py_DECREF(s_object);
1898 return result;
Christian Heimesa34706f2008-01-04 03:06:10 +00001899}
1900
1901PyDoc_STRVAR(unpack_from_doc,
Mark Dickinsonc6f13962010-06-13 09:17:13 +00001902"unpack_from(fmt, buffer, offset=0) -> (v1, v2, ...)\n\
Mark Dickinsonaacfa952010-06-12 15:43:45 +00001903\n\
1904Return a tuple containing values unpacked according to the format string\n\
1905fmt. Requires len(buffer[offset:]) >= calcsize(fmt). See help(struct)\n\
1906for more on format strings.");
Christian Heimesa34706f2008-01-04 03:06:10 +00001907
1908static PyObject *
1909unpack_from(PyObject *self, PyObject *args, PyObject *kwds)
1910{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001911 PyObject *s_object, *fmt, *newargs, *result;
1912 Py_ssize_t n = PyTuple_GET_SIZE(args);
Christian Heimesa34706f2008-01-04 03:06:10 +00001913
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001914 if (n == 0) {
1915 PyErr_SetString(PyExc_TypeError, "missing format argument");
1916 return NULL;
1917 }
1918 fmt = PyTuple_GET_ITEM(args, 0);
1919 newargs = PyTuple_GetSlice(args, 1, n);
1920 if (newargs == NULL)
1921 return NULL;
Christian Heimesa34706f2008-01-04 03:06:10 +00001922
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001923 s_object = cache_struct(fmt);
1924 if (s_object == NULL) {
1925 Py_DECREF(newargs);
1926 return NULL;
1927 }
1928 result = s_unpack_from(s_object, newargs, kwds);
1929 Py_DECREF(newargs);
1930 Py_DECREF(s_object);
1931 return result;
Christian Heimesa34706f2008-01-04 03:06:10 +00001932}
1933
1934static struct PyMethodDef module_functions[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001935 {"_clearcache", (PyCFunction)clearcache, METH_NOARGS, clearcache_doc},
1936 {"calcsize", calcsize, METH_O, calcsize_doc},
1937 {"pack", pack, METH_VARARGS, pack_doc},
1938 {"pack_into", pack_into, METH_VARARGS, pack_into_doc},
1939 {"unpack", unpack, METH_VARARGS, unpack_doc},
1940 {"unpack_from", (PyCFunction)unpack_from,
1941 METH_VARARGS|METH_KEYWORDS, unpack_from_doc},
1942 {NULL, NULL} /* sentinel */
Christian Heimesa34706f2008-01-04 03:06:10 +00001943};
1944
1945
Thomas Wouters477c8d52006-05-27 19:21:47 +00001946/* Module initialization */
1947
Christian Heimesa34706f2008-01-04 03:06:10 +00001948PyDoc_STRVAR(module_doc,
1949"Functions to convert between Python values and C structs.\n\
Benjamin Peterson4ae19462008-07-31 15:03:40 +00001950Python bytes objects are used to hold the data representing the C struct\n\
Mark Dickinson40714af2009-10-08 15:59:20 +00001951and also as format strings (explained below) to describe the layout of data\n\
1952in the C struct.\n\
Christian Heimesa34706f2008-01-04 03:06:10 +00001953\n\
1954The optional first format char indicates byte order, size and alignment:\n\
Mark Dickinson40714af2009-10-08 15:59:20 +00001955 @: native order, size & alignment (default)\n\
1956 =: native order, std. size & alignment\n\
1957 <: little-endian, std. size & alignment\n\
1958 >: big-endian, std. size & alignment\n\
1959 !: same as >\n\
Christian Heimesa34706f2008-01-04 03:06:10 +00001960\n\
1961The remaining chars indicate types of args and must match exactly;\n\
1962these can be preceded by a decimal repeat count:\n\
1963 x: pad byte (no data); c:char; b:signed byte; B:unsigned byte;\n\
Mark Dickinson40714af2009-10-08 15:59:20 +00001964 ?: _Bool (requires C99; if not available, char is used instead)\n\
Christian Heimesa34706f2008-01-04 03:06:10 +00001965 h:short; H:unsigned short; i:int; I:unsigned int;\n\
1966 l:long; L:unsigned long; f:float; d:double.\n\
1967Special cases (preceding decimal count indicates length):\n\
1968 s:string (array of char); p: pascal string (with count byte).\n\
1969Special case (only available in native format):\n\
1970 P:an integer type that is wide enough to hold a pointer.\n\
1971Special case (not in native mode unless 'long long' in platform C):\n\
1972 q:long long; Q:unsigned long long\n\
1973Whitespace between formats is ignored.\n\
1974\n\
1975The variable struct.error is an exception raised on errors.\n");
1976
Martin v. Löwis1a214512008-06-11 05:26:20 +00001977
1978static struct PyModuleDef _structmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001979 PyModuleDef_HEAD_INIT,
1980 "_struct",
1981 module_doc,
1982 -1,
1983 module_functions,
1984 NULL,
1985 NULL,
1986 NULL,
1987 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001988};
1989
Thomas Wouters477c8d52006-05-27 19:21:47 +00001990PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001991PyInit__struct(void)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001992{
Mark Dickinson06817852010-06-12 09:25:13 +00001993 PyObject *m;
Christian Heimesa34706f2008-01-04 03:06:10 +00001994
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001995 m = PyModule_Create(&_structmodule);
1996 if (m == NULL)
1997 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001998
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001999 Py_TYPE(&PyStructType) = &PyType_Type;
2000 if (PyType_Ready(&PyStructType) < 0)
2001 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002002
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002003 /* Check endian and swap in faster functions */
2004 {
2005 int one = 1;
2006 formatdef *native = native_table;
2007 formatdef *other, *ptr;
2008 if ((int)*(unsigned char*)&one)
2009 other = lilendian_table;
2010 else
2011 other = bigendian_table;
2012 /* Scan through the native table, find a matching
2013 entry in the endian table and swap in the
2014 native implementations whenever possible
2015 (64-bit platforms may not have "standard" sizes) */
2016 while (native->format != '\0' && other->format != '\0') {
2017 ptr = other;
2018 while (ptr->format != '\0') {
2019 if (ptr->format == native->format) {
2020 /* Match faster when formats are
2021 listed in the same order */
2022 if (ptr == other)
2023 other++;
2024 /* Only use the trick if the
2025 size matches */
2026 if (ptr->size != native->size)
2027 break;
2028 /* Skip float and double, could be
2029 "unknown" float format */
2030 if (ptr->format == 'd' || ptr->format == 'f')
2031 break;
2032 ptr->pack = native->pack;
2033 ptr->unpack = native->unpack;
2034 break;
2035 }
2036 ptr++;
2037 }
2038 native++;
2039 }
2040 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002041
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002042 /* Add some symbolic constants to the module */
2043 if (StructError == NULL) {
2044 StructError = PyErr_NewException("struct.error", NULL, NULL);
2045 if (StructError == NULL)
2046 return NULL;
2047 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002048
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002049 Py_INCREF(StructError);
2050 PyModule_AddObject(m, "error", StructError);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002051
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002052 Py_INCREF((PyObject*)&PyStructType);
2053 PyModule_AddObject(m, "Struct", (PyObject*)&PyStructType);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002054
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002055 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002056}