blob: 092b72c9f8885833e4055a4aa9388a718b5dfb9b [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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Dickinsonea835e72009-04-19 20:40:33 +000092/* Helper to get a PyLongObject. Caller should decref. */
Thomas Wouters477c8d52006-05-27 19:21:47 +000093
94static PyObject *
95get_pylong(PyObject *v)
96{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000097 assert(v != NULL);
98 if (!PyLong_Check(v)) {
99 PyErr_SetString(StructError,
100 "required argument is not an integer");
101 return NULL;
102 }
Mark Dickinsonea835e72009-04-19 20:40:33 +0000103
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000104 Py_INCREF(v);
105 return v;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000106}
107
Mark Dickinsonea835e72009-04-19 20:40:33 +0000108/* Helper routine to get a C long and raise the appropriate error if it isn't
109 one */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000110
111static int
112get_long(PyObject *v, long *p)
113{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000114 long x;
Mark Dickinsonea835e72009-04-19 20:40:33 +0000115
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000116 if (!PyLong_Check(v)) {
117 PyErr_SetString(StructError,
118 "required argument is not an integer");
119 return -1;
120 }
121 x = PyLong_AsLong(v);
122 if (x == -1 && PyErr_Occurred()) {
123 if (PyErr_ExceptionMatches(PyExc_OverflowError))
124 PyErr_SetString(StructError,
125 "argument out of range");
126 return -1;
127 }
128 *p = x;
129 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000130}
131
132
133/* Same, but handling unsigned long */
134
Benjamin Petersond76c8da2009-06-28 17:35:48 +0000135#ifndef PY_STRUCT_OVERFLOW_MASKING
Thomas Wouters477c8d52006-05-27 19:21:47 +0000136static int
137get_ulong(PyObject *v, unsigned long *p)
138{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000139 unsigned long x;
Mark Dickinsonea835e72009-04-19 20:40:33 +0000140
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000141 if (!PyLong_Check(v)) {
142 PyErr_SetString(StructError,
143 "required argument is not an integer");
144 return -1;
145 }
146 x = PyLong_AsUnsignedLong(v);
147 if (x == (unsigned long)-1 && PyErr_Occurred()) {
148 if (PyErr_ExceptionMatches(PyExc_OverflowError))
149 PyErr_SetString(StructError,
150 "argument out of range");
151 return -1;
152 }
153 *p = x;
154 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000155}
Benjamin Petersond76c8da2009-06-28 17:35:48 +0000156#endif /* PY_STRUCT_OVERFLOW_MASKING */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000157
158#ifdef HAVE_LONG_LONG
159
160/* Same, but handling native long long. */
161
162static int
163get_longlong(PyObject *v, PY_LONG_LONG *p)
164{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000165 PY_LONG_LONG x;
166 if (!PyLong_Check(v)) {
167 PyErr_SetString(StructError,
168 "required argument is not an integer");
169 return -1;
170 }
171 x = PyLong_AsLongLong(v);
172 if (x == -1 && PyErr_Occurred()) {
173 if (PyErr_ExceptionMatches(PyExc_OverflowError))
174 PyErr_SetString(StructError,
175 "argument out of range");
176 return -1;
177 }
178 *p = x;
179 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000180}
181
182/* Same, but handling native unsigned long long. */
183
184static int
185get_ulonglong(PyObject *v, unsigned PY_LONG_LONG *p)
186{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000187 unsigned PY_LONG_LONG x;
188 if (!PyLong_Check(v)) {
189 PyErr_SetString(StructError,
190 "required argument is not an integer");
191 return -1;
192 }
193 x = PyLong_AsUnsignedLongLong(v);
194 if (x == -1 && PyErr_Occurred()) {
195 if (PyErr_ExceptionMatches(PyExc_OverflowError))
196 PyErr_SetString(StructError,
197 "argument out of range");
198 return -1;
199 }
200 *p = x;
201 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000202}
203
204#endif
205
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000206
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000207#define RANGE_ERROR(x, f, flag, mask) return _range_error(f, flag)
208
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000209
Thomas Wouters477c8d52006-05-27 19:21:47 +0000210/* Floating point helpers */
211
212static PyObject *
213unpack_float(const char *p, /* start of 4-byte string */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000214 int le) /* true for little-endian, false for big-endian */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000215{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000216 double x;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000217
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000218 x = _PyFloat_Unpack4((unsigned char *)p, le);
219 if (x == -1.0 && PyErr_Occurred())
220 return NULL;
221 return PyFloat_FromDouble(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000222}
223
224static PyObject *
225unpack_double(const char *p, /* start of 8-byte string */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000226 int le) /* true for little-endian, false for big-endian */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000227{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000228 double x;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000229
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000230 x = _PyFloat_Unpack8((unsigned char *)p, le);
231 if (x == -1.0 && PyErr_Occurred())
232 return NULL;
233 return PyFloat_FromDouble(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000234}
235
236/* Helper to format the range error exceptions */
237static int
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000238_range_error(const formatdef *f, int is_unsigned)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000239{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000240 /* ulargest is the largest unsigned value with f->size bytes.
241 * Note that the simpler:
242 * ((size_t)1 << (f->size * 8)) - 1
243 * doesn't work when f->size == sizeof(size_t) because C doesn't
244 * define what happens when a left shift count is >= the number of
245 * bits in the integer being shifted; e.g., on some boxes it doesn't
246 * shift at all when they're equal.
247 */
248 const size_t ulargest = (size_t)-1 >> ((SIZEOF_SIZE_T - f->size)*8);
249 assert(f->size >= 1 && f->size <= SIZEOF_SIZE_T);
250 if (is_unsigned)
251 PyErr_Format(StructError,
252 "'%c' format requires 0 <= number <= %zu",
253 f->format,
254 ulargest);
255 else {
256 const Py_ssize_t largest = (Py_ssize_t)(ulargest >> 1);
257 PyErr_Format(StructError,
258 "'%c' format requires %zd <= number <= %zd",
259 f->format,
260 ~ largest,
261 largest);
262 }
Mark Dickinsonae681df2009-03-21 10:26:31 +0000263
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000264 return -1;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000265}
266
267
268
269/* A large number of small routines follow, with names of the form
270
271 [bln][up]_TYPE
272
273 [bln] distiguishes among big-endian, little-endian and native.
274 [pu] distiguishes between pack (to struct) and unpack (from struct).
275 TYPE is one of char, byte, ubyte, etc.
276*/
277
278/* Native mode routines. ****************************************************/
279/* NOTE:
280 In all n[up]_<type> routines handling types larger than 1 byte, there is
281 *no* guarantee that the p pointer is properly aligned for each type,
282 therefore memcpy is called. An intermediate variable is used to
283 compensate for big-endian architectures.
284 Normally both the intermediate variable and the memcpy call will be
285 skipped by C optimisation in little-endian architectures (gcc >= 2.91
286 does this). */
287
288static PyObject *
289nu_char(const char *p, const formatdef *f)
290{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000291 return PyBytes_FromStringAndSize(p, 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000292}
293
294static PyObject *
295nu_byte(const char *p, const formatdef *f)
296{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000297 return PyLong_FromLong((long) *(signed char *)p);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000298}
299
300static PyObject *
301nu_ubyte(const char *p, const formatdef *f)
302{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000303 return PyLong_FromLong((long) *(unsigned char *)p);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000304}
305
306static PyObject *
307nu_short(const char *p, const formatdef *f)
308{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000309 short x;
310 memcpy((char *)&x, p, sizeof x);
311 return PyLong_FromLong((long)x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000312}
313
314static PyObject *
315nu_ushort(const char *p, const formatdef *f)
316{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000317 unsigned short x;
318 memcpy((char *)&x, p, sizeof x);
319 return PyLong_FromLong((long)x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000320}
321
322static PyObject *
323nu_int(const char *p, const formatdef *f)
324{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000325 int x;
326 memcpy((char *)&x, p, sizeof x);
327 return PyLong_FromLong((long)x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000328}
329
330static PyObject *
331nu_uint(const char *p, const formatdef *f)
332{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000333 unsigned int x;
334 memcpy((char *)&x, p, sizeof x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000335#if (SIZEOF_LONG > SIZEOF_INT)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000336 return PyLong_FromLong((long)x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000337#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000338 if (x <= ((unsigned int)LONG_MAX))
339 return PyLong_FromLong((long)x);
340 return PyLong_FromUnsignedLong((unsigned long)x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000341#endif
342}
343
344static PyObject *
345nu_long(const char *p, const formatdef *f)
346{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000347 long x;
348 memcpy((char *)&x, p, sizeof x);
349 return PyLong_FromLong(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000350}
351
352static PyObject *
353nu_ulong(const char *p, const formatdef *f)
354{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000355 unsigned long x;
356 memcpy((char *)&x, p, sizeof x);
357 if (x <= LONG_MAX)
358 return PyLong_FromLong((long)x);
359 return PyLong_FromUnsignedLong(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000360}
361
362/* Native mode doesn't support q or Q unless the platform C supports
363 long long (or, on Windows, __int64). */
364
365#ifdef HAVE_LONG_LONG
366
367static PyObject *
368nu_longlong(const char *p, const formatdef *f)
369{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000370 PY_LONG_LONG x;
371 memcpy((char *)&x, p, sizeof x);
372 if (x >= LONG_MIN && x <= LONG_MAX)
373 return PyLong_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
374 return PyLong_FromLongLong(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000375}
376
377static PyObject *
378nu_ulonglong(const char *p, const formatdef *f)
379{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000380 unsigned PY_LONG_LONG x;
381 memcpy((char *)&x, p, sizeof x);
382 if (x <= LONG_MAX)
383 return PyLong_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
384 return PyLong_FromUnsignedLongLong(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000385}
386
387#endif
388
389static PyObject *
Thomas Woutersb2137042007-02-01 18:02:27 +0000390nu_bool(const char *p, const formatdef *f)
391{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000392 BOOL_TYPE x;
393 memcpy((char *)&x, p, sizeof x);
394 return PyBool_FromLong(x != 0);
Thomas Woutersb2137042007-02-01 18:02:27 +0000395}
396
397
398static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +0000399nu_float(const char *p, const formatdef *f)
400{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000401 float x;
402 memcpy((char *)&x, p, sizeof x);
403 return PyFloat_FromDouble((double)x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000404}
405
406static PyObject *
407nu_double(const char *p, const formatdef *f)
408{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000409 double x;
410 memcpy((char *)&x, p, sizeof x);
411 return PyFloat_FromDouble(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000412}
413
414static PyObject *
415nu_void_p(const char *p, const formatdef *f)
416{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000417 void *x;
418 memcpy((char *)&x, p, sizeof x);
419 return PyLong_FromVoidPtr(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000420}
421
422static int
423np_byte(char *p, PyObject *v, const formatdef *f)
424{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000425 long x;
426 if (get_long(v, &x) < 0)
427 return -1;
428 if (x < -128 || x > 127){
429 PyErr_SetString(StructError,
430 "byte format requires -128 <= number <= 127");
431 return -1;
432 }
433 *p = (char)x;
434 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000435}
436
437static int
438np_ubyte(char *p, PyObject *v, const formatdef *f)
439{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000440 long x;
441 if (get_long(v, &x) < 0)
442 return -1;
443 if (x < 0 || x > 255){
444 PyErr_SetString(StructError,
445 "ubyte format requires 0 <= number <= 255");
446 return -1;
447 }
448 *p = (char)x;
449 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000450}
451
452static int
453np_char(char *p, PyObject *v, const formatdef *f)
454{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000455 if (PyUnicode_Check(v)) {
456 v = _PyUnicode_AsDefaultEncodedString(v, NULL);
457 if (v == NULL)
458 return -1;
459 }
460 if (!PyBytes_Check(v) || PyBytes_Size(v) != 1) {
461 PyErr_SetString(StructError,
462 "char format requires bytes or string of length 1");
463 return -1;
464 }
465 *p = *PyBytes_AsString(v);
466 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000467}
468
469static int
470np_short(char *p, PyObject *v, const formatdef *f)
471{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000472 long x;
473 short y;
474 if (get_long(v, &x) < 0)
475 return -1;
476 if (x < SHRT_MIN || x > SHRT_MAX){
477 PyErr_SetString(StructError,
478 "short format requires " STRINGIFY(SHRT_MIN)
479 " <= number <= " STRINGIFY(SHRT_MAX));
480 return -1;
481 }
482 y = (short)x;
483 memcpy(p, (char *)&y, sizeof y);
484 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000485}
486
487static int
488np_ushort(char *p, PyObject *v, const formatdef *f)
489{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000490 long x;
491 unsigned short y;
492 if (get_long(v, &x) < 0)
493 return -1;
494 if (x < 0 || x > USHRT_MAX){
495 PyErr_SetString(StructError,
496 "ushort format requires 0 <= number <= " STRINGIFY(USHRT_MAX));
497 return -1;
498 }
499 y = (unsigned short)x;
500 memcpy(p, (char *)&y, sizeof y);
501 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000502}
503
504static int
505np_int(char *p, PyObject *v, const formatdef *f)
506{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000507 long x;
508 int y;
509 if (get_long(v, &x) < 0)
510 return -1;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000511#if (SIZEOF_LONG > SIZEOF_INT)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000512 if ((x < ((long)INT_MIN)) || (x > ((long)INT_MAX)))
513 RANGE_ERROR(x, f, 0, -1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000514#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000515 y = (int)x;
516 memcpy(p, (char *)&y, sizeof y);
517 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000518}
519
520static int
521np_uint(char *p, PyObject *v, const formatdef *f)
522{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000523 unsigned long x;
524 unsigned int y;
525 if (get_ulong(v, &x) < 0)
526 return -1;
527 y = (unsigned int)x;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000528#if (SIZEOF_LONG > SIZEOF_INT)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000529 if (x > ((unsigned long)UINT_MAX))
530 RANGE_ERROR(y, f, 1, -1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000531#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000532 memcpy(p, (char *)&y, sizeof y);
533 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000534}
535
536static int
537np_long(char *p, PyObject *v, const formatdef *f)
538{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000539 long x;
540 if (get_long(v, &x) < 0)
541 return -1;
542 memcpy(p, (char *)&x, sizeof x);
543 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000544}
545
546static int
547np_ulong(char *p, PyObject *v, const formatdef *f)
548{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000549 unsigned long x;
550 if (get_ulong(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
556#ifdef HAVE_LONG_LONG
557
558static int
559np_longlong(char *p, PyObject *v, const formatdef *f)
560{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000561 PY_LONG_LONG x;
562 if (get_longlong(v, &x) < 0)
563 return -1;
564 memcpy(p, (char *)&x, sizeof x);
565 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000566}
567
568static int
569np_ulonglong(char *p, PyObject *v, const formatdef *f)
570{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000571 unsigned PY_LONG_LONG x;
572 if (get_ulonglong(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#endif
578
Thomas Woutersb2137042007-02-01 18:02:27 +0000579
580static int
581np_bool(char *p, PyObject *v, const formatdef *f)
582{
Benjamin Petersonf092c7c2010-07-07 22:46:00 +0000583 int y;
584 BOOL_TYPE x;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000585 y = PyObject_IsTrue(v);
Benjamin Petersonf092c7c2010-07-07 22:46:00 +0000586 if (y < 0)
587 return -1;
588 x = y;
589 memcpy(p, (char *)&x, sizeof x);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000590 return 0;
Thomas Woutersb2137042007-02-01 18:02:27 +0000591}
592
Thomas Wouters477c8d52006-05-27 19:21:47 +0000593static int
594np_float(char *p, PyObject *v, const formatdef *f)
595{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000596 float x = (float)PyFloat_AsDouble(v);
597 if (x == -1 && PyErr_Occurred()) {
598 PyErr_SetString(StructError,
599 "required argument is not a float");
600 return -1;
601 }
602 memcpy(p, (char *)&x, sizeof x);
603 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000604}
605
606static int
607np_double(char *p, PyObject *v, const formatdef *f)
608{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000609 double x = PyFloat_AsDouble(v);
610 if (x == -1 && PyErr_Occurred()) {
611 PyErr_SetString(StructError,
612 "required argument is not a float");
613 return -1;
614 }
615 memcpy(p, (char *)&x, sizeof(double));
616 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000617}
618
619static int
620np_void_p(char *p, PyObject *v, const formatdef *f)
621{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000622 void *x;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000623
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000624 v = get_pylong(v);
625 if (v == NULL)
626 return -1;
627 assert(PyLong_Check(v));
628 x = PyLong_AsVoidPtr(v);
629 Py_DECREF(v);
630 if (x == NULL && PyErr_Occurred())
631 return -1;
632 memcpy(p, (char *)&x, sizeof x);
633 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000634}
635
636static formatdef native_table[] = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000637 {'x', sizeof(char), 0, NULL},
638 {'b', sizeof(char), 0, nu_byte, np_byte},
639 {'B', sizeof(char), 0, nu_ubyte, np_ubyte},
640 {'c', sizeof(char), 0, nu_char, np_char},
641 {'s', sizeof(char), 0, NULL},
642 {'p', sizeof(char), 0, NULL},
643 {'h', sizeof(short), SHORT_ALIGN, nu_short, np_short},
644 {'H', sizeof(short), SHORT_ALIGN, nu_ushort, np_ushort},
645 {'i', sizeof(int), INT_ALIGN, nu_int, np_int},
646 {'I', sizeof(int), INT_ALIGN, nu_uint, np_uint},
647 {'l', sizeof(long), LONG_ALIGN, nu_long, np_long},
648 {'L', sizeof(long), LONG_ALIGN, nu_ulong, np_ulong},
Thomas Wouters477c8d52006-05-27 19:21:47 +0000649#ifdef HAVE_LONG_LONG
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000650 {'q', sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_longlong, np_longlong},
651 {'Q', sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_ulonglong,np_ulonglong},
Thomas Wouters477c8d52006-05-27 19:21:47 +0000652#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000653 {'?', sizeof(BOOL_TYPE), BOOL_ALIGN, nu_bool, np_bool},
654 {'f', sizeof(float), FLOAT_ALIGN, nu_float, np_float},
655 {'d', sizeof(double), DOUBLE_ALIGN, nu_double, np_double},
656 {'P', sizeof(void *), VOID_P_ALIGN, nu_void_p, np_void_p},
657 {0}
Thomas Wouters477c8d52006-05-27 19:21:47 +0000658};
659
660/* Big-endian routines. *****************************************************/
661
662static PyObject *
663bu_int(const char *p, const formatdef *f)
664{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000665 long x = 0;
666 Py_ssize_t i = f->size;
667 const unsigned char *bytes = (const unsigned char *)p;
668 do {
669 x = (x<<8) | *bytes++;
670 } while (--i > 0);
671 /* Extend the sign bit. */
672 if (SIZEOF_LONG > f->size)
673 x |= -(x & (1L << ((8 * f->size) - 1)));
674 return PyLong_FromLong(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000675}
676
677static PyObject *
678bu_uint(const char *p, const formatdef *f)
679{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000680 unsigned long x = 0;
681 Py_ssize_t i = f->size;
682 const unsigned char *bytes = (const unsigned char *)p;
683 do {
684 x = (x<<8) | *bytes++;
685 } while (--i > 0);
686 if (x <= LONG_MAX)
687 return PyLong_FromLong((long)x);
688 return PyLong_FromUnsignedLong(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000689}
690
691static PyObject *
692bu_longlong(const char *p, const formatdef *f)
693{
694#ifdef HAVE_LONG_LONG
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000695 PY_LONG_LONG x = 0;
696 Py_ssize_t i = f->size;
697 const unsigned char *bytes = (const unsigned char *)p;
698 do {
699 x = (x<<8) | *bytes++;
700 } while (--i > 0);
701 /* Extend the sign bit. */
702 if (SIZEOF_LONG_LONG > f->size)
703 x |= -(x & ((PY_LONG_LONG)1 << ((8 * f->size) - 1)));
704 if (x >= LONG_MIN && x <= LONG_MAX)
705 return PyLong_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
706 return PyLong_FromLongLong(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000707#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000708 return _PyLong_FromByteArray((const unsigned char *)p,
709 8,
710 0, /* little-endian */
711 1 /* signed */);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000712#endif
713}
714
715static PyObject *
716bu_ulonglong(const char *p, const formatdef *f)
717{
718#ifdef HAVE_LONG_LONG
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000719 unsigned PY_LONG_LONG x = 0;
720 Py_ssize_t i = f->size;
721 const unsigned char *bytes = (const unsigned char *)p;
722 do {
723 x = (x<<8) | *bytes++;
724 } while (--i > 0);
725 if (x <= LONG_MAX)
726 return PyLong_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
727 return PyLong_FromUnsignedLongLong(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000728#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000729 return _PyLong_FromByteArray((const unsigned char *)p,
730 8,
731 0, /* little-endian */
732 0 /* signed */);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000733#endif
734}
735
736static PyObject *
737bu_float(const char *p, const formatdef *f)
738{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000739 return unpack_float(p, 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000740}
741
742static PyObject *
743bu_double(const char *p, const formatdef *f)
744{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000745 return unpack_double(p, 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000746}
747
Thomas Woutersb2137042007-02-01 18:02:27 +0000748static PyObject *
749bu_bool(const char *p, const formatdef *f)
750{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000751 char x;
752 memcpy((char *)&x, p, sizeof x);
753 return PyBool_FromLong(x != 0);
Thomas Woutersb2137042007-02-01 18:02:27 +0000754}
755
Thomas Wouters477c8d52006-05-27 19:21:47 +0000756static int
757bp_int(char *p, PyObject *v, const formatdef *f)
758{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000759 long x;
760 Py_ssize_t i;
761 if (get_long(v, &x) < 0)
762 return -1;
763 i = f->size;
764 if (i != SIZEOF_LONG) {
765 if ((i == 2) && (x < -32768 || x > 32767))
766 RANGE_ERROR(x, f, 0, 0xffffL);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000767#if (SIZEOF_LONG != 4)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000768 else if ((i == 4) && (x < -2147483648L || x > 2147483647L))
769 RANGE_ERROR(x, f, 0, 0xffffffffL);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000770#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000771 }
772 do {
773 p[--i] = (char)x;
774 x >>= 8;
775 } while (i > 0);
776 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000777}
778
779static int
780bp_uint(char *p, PyObject *v, const formatdef *f)
781{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000782 unsigned long x;
783 Py_ssize_t i;
784 if (get_ulong(v, &x) < 0)
785 return -1;
786 i = f->size;
787 if (i != SIZEOF_LONG) {
788 unsigned long maxint = 1;
789 maxint <<= (unsigned long)(i * 8);
790 if (x >= maxint)
791 RANGE_ERROR(x, f, 1, maxint - 1);
792 }
793 do {
794 p[--i] = (char)x;
795 x >>= 8;
796 } while (i > 0);
797 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000798}
799
800static int
801bp_longlong(char *p, PyObject *v, const formatdef *f)
802{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000803 int res;
804 v = get_pylong(v);
805 if (v == NULL)
806 return -1;
807 res = _PyLong_AsByteArray((PyLongObject *)v,
808 (unsigned char *)p,
809 8,
810 0, /* little_endian */
811 1 /* signed */);
812 Py_DECREF(v);
813 return res;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000814}
815
816static int
817bp_ulonglong(char *p, PyObject *v, const formatdef *f)
818{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000819 int res;
820 v = get_pylong(v);
821 if (v == NULL)
822 return -1;
823 res = _PyLong_AsByteArray((PyLongObject *)v,
824 (unsigned char *)p,
825 8,
826 0, /* little_endian */
827 0 /* signed */);
828 Py_DECREF(v);
829 return res;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000830}
831
832static int
833bp_float(char *p, PyObject *v, const formatdef *f)
834{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000835 double x = PyFloat_AsDouble(v);
836 if (x == -1 && PyErr_Occurred()) {
837 PyErr_SetString(StructError,
838 "required argument is not a float");
839 return -1;
840 }
841 return _PyFloat_Pack4(x, (unsigned char *)p, 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000842}
843
844static int
845bp_double(char *p, PyObject *v, const formatdef *f)
846{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000847 double x = PyFloat_AsDouble(v);
848 if (x == -1 && PyErr_Occurred()) {
849 PyErr_SetString(StructError,
850 "required argument is not a float");
851 return -1;
852 }
853 return _PyFloat_Pack8(x, (unsigned char *)p, 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000854}
855
Thomas Woutersb2137042007-02-01 18:02:27 +0000856static int
857bp_bool(char *p, PyObject *v, const formatdef *f)
858{
Mark Dickinson101d16c2010-07-18 07:42:29 +0000859 int y;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000860 y = PyObject_IsTrue(v);
Benjamin Petersonf092c7c2010-07-07 22:46:00 +0000861 if (y < 0)
862 return -1;
Mark Dickinson101d16c2010-07-18 07:42:29 +0000863 *p = (char)y;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000864 return 0;
Thomas Woutersb2137042007-02-01 18:02:27 +0000865}
866
Thomas Wouters477c8d52006-05-27 19:21:47 +0000867static formatdef bigendian_table[] = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000868 {'x', 1, 0, NULL},
869 {'b', 1, 0, nu_byte, np_byte},
870 {'B', 1, 0, nu_ubyte, np_ubyte},
871 {'c', 1, 0, nu_char, np_char},
872 {'s', 1, 0, NULL},
873 {'p', 1, 0, NULL},
874 {'h', 2, 0, bu_int, bp_int},
875 {'H', 2, 0, bu_uint, bp_uint},
876 {'i', 4, 0, bu_int, bp_int},
877 {'I', 4, 0, bu_uint, bp_uint},
878 {'l', 4, 0, bu_int, bp_int},
879 {'L', 4, 0, bu_uint, bp_uint},
880 {'q', 8, 0, bu_longlong, bp_longlong},
881 {'Q', 8, 0, bu_ulonglong, bp_ulonglong},
882 {'?', 1, 0, bu_bool, bp_bool},
883 {'f', 4, 0, bu_float, bp_float},
884 {'d', 8, 0, bu_double, bp_double},
885 {0}
Thomas Wouters477c8d52006-05-27 19:21:47 +0000886};
887
888/* Little-endian routines. *****************************************************/
889
890static PyObject *
891lu_int(const char *p, const formatdef *f)
892{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000893 long x = 0;
894 Py_ssize_t i = f->size;
895 const unsigned char *bytes = (const unsigned char *)p;
896 do {
897 x = (x<<8) | bytes[--i];
898 } while (i > 0);
899 /* Extend the sign bit. */
900 if (SIZEOF_LONG > f->size)
901 x |= -(x & (1L << ((8 * f->size) - 1)));
902 return PyLong_FromLong(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000903}
904
905static PyObject *
906lu_uint(const char *p, const formatdef *f)
907{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000908 unsigned long x = 0;
909 Py_ssize_t i = f->size;
910 const unsigned char *bytes = (const unsigned char *)p;
911 do {
912 x = (x<<8) | bytes[--i];
913 } while (i > 0);
914 if (x <= LONG_MAX)
915 return PyLong_FromLong((long)x);
916 return PyLong_FromUnsignedLong((long)x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000917}
918
919static PyObject *
920lu_longlong(const char *p, const formatdef *f)
921{
922#ifdef HAVE_LONG_LONG
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000923 PY_LONG_LONG x = 0;
924 Py_ssize_t i = f->size;
925 const unsigned char *bytes = (const unsigned char *)p;
926 do {
927 x = (x<<8) | bytes[--i];
928 } while (i > 0);
929 /* Extend the sign bit. */
930 if (SIZEOF_LONG_LONG > f->size)
931 x |= -(x & ((PY_LONG_LONG)1 << ((8 * f->size) - 1)));
932 if (x >= LONG_MIN && x <= LONG_MAX)
933 return PyLong_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
934 return PyLong_FromLongLong(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000935#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000936 return _PyLong_FromByteArray((const unsigned char *)p,
937 8,
938 1, /* little-endian */
939 1 /* signed */);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000940#endif
941}
942
943static PyObject *
944lu_ulonglong(const char *p, const formatdef *f)
945{
946#ifdef HAVE_LONG_LONG
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000947 unsigned PY_LONG_LONG x = 0;
948 Py_ssize_t i = f->size;
949 const unsigned char *bytes = (const unsigned char *)p;
950 do {
951 x = (x<<8) | bytes[--i];
952 } while (i > 0);
953 if (x <= LONG_MAX)
954 return PyLong_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
955 return PyLong_FromUnsignedLongLong(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000956#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000957 return _PyLong_FromByteArray((const unsigned char *)p,
958 8,
959 1, /* little-endian */
960 0 /* signed */);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000961#endif
962}
963
964static PyObject *
965lu_float(const char *p, const formatdef *f)
966{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000967 return unpack_float(p, 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000968}
969
970static PyObject *
971lu_double(const char *p, const formatdef *f)
972{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000973 return unpack_double(p, 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000974}
975
976static int
977lp_int(char *p, PyObject *v, const formatdef *f)
978{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000979 long x;
980 Py_ssize_t i;
981 if (get_long(v, &x) < 0)
982 return -1;
983 i = f->size;
984 if (i != SIZEOF_LONG) {
985 if ((i == 2) && (x < -32768 || x > 32767))
986 RANGE_ERROR(x, f, 0, 0xffffL);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000987#if (SIZEOF_LONG != 4)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000988 else if ((i == 4) && (x < -2147483648L || x > 2147483647L))
989 RANGE_ERROR(x, f, 0, 0xffffffffL);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000990#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000991 }
992 do {
993 *p++ = (char)x;
994 x >>= 8;
995 } while (--i > 0);
996 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000997}
998
999static int
1000lp_uint(char *p, PyObject *v, const formatdef *f)
1001{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001002 unsigned long x;
1003 Py_ssize_t i;
1004 if (get_ulong(v, &x) < 0)
1005 return -1;
1006 i = f->size;
1007 if (i != SIZEOF_LONG) {
1008 unsigned long maxint = 1;
1009 maxint <<= (unsigned long)(i * 8);
1010 if (x >= maxint)
1011 RANGE_ERROR(x, f, 1, maxint - 1);
1012 }
1013 do {
1014 *p++ = (char)x;
1015 x >>= 8;
1016 } while (--i > 0);
1017 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001018}
1019
1020static int
1021lp_longlong(char *p, PyObject *v, const formatdef *f)
1022{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001023 int res;
1024 v = get_pylong(v);
1025 if (v == NULL)
1026 return -1;
1027 res = _PyLong_AsByteArray((PyLongObject*)v,
1028 (unsigned char *)p,
1029 8,
1030 1, /* little_endian */
1031 1 /* signed */);
1032 Py_DECREF(v);
1033 return res;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001034}
1035
1036static int
1037lp_ulonglong(char *p, PyObject *v, const formatdef *f)
1038{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001039 int res;
1040 v = get_pylong(v);
1041 if (v == NULL)
1042 return -1;
1043 res = _PyLong_AsByteArray((PyLongObject*)v,
1044 (unsigned char *)p,
1045 8,
1046 1, /* little_endian */
1047 0 /* signed */);
1048 Py_DECREF(v);
1049 return res;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001050}
1051
1052static int
1053lp_float(char *p, PyObject *v, const formatdef *f)
1054{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001055 double x = PyFloat_AsDouble(v);
1056 if (x == -1 && PyErr_Occurred()) {
1057 PyErr_SetString(StructError,
1058 "required argument is not a float");
1059 return -1;
1060 }
1061 return _PyFloat_Pack4(x, (unsigned char *)p, 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001062}
1063
1064static int
1065lp_double(char *p, PyObject *v, const formatdef *f)
1066{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001067 double x = PyFloat_AsDouble(v);
1068 if (x == -1 && PyErr_Occurred()) {
1069 PyErr_SetString(StructError,
1070 "required argument is not a float");
1071 return -1;
1072 }
1073 return _PyFloat_Pack8(x, (unsigned char *)p, 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001074}
1075
1076static formatdef lilendian_table[] = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001077 {'x', 1, 0, NULL},
1078 {'b', 1, 0, nu_byte, np_byte},
1079 {'B', 1, 0, nu_ubyte, np_ubyte},
1080 {'c', 1, 0, nu_char, np_char},
1081 {'s', 1, 0, NULL},
1082 {'p', 1, 0, NULL},
1083 {'h', 2, 0, lu_int, lp_int},
1084 {'H', 2, 0, lu_uint, lp_uint},
1085 {'i', 4, 0, lu_int, lp_int},
1086 {'I', 4, 0, lu_uint, lp_uint},
1087 {'l', 4, 0, lu_int, lp_int},
1088 {'L', 4, 0, lu_uint, lp_uint},
1089 {'q', 8, 0, lu_longlong, lp_longlong},
1090 {'Q', 8, 0, lu_ulonglong, lp_ulonglong},
1091 {'?', 1, 0, bu_bool, bp_bool}, /* Std rep not endian dep,
1092 but potentially different from native rep -- reuse bx_bool funcs. */
1093 {'f', 4, 0, lu_float, lp_float},
1094 {'d', 8, 0, lu_double, lp_double},
1095 {0}
Thomas Wouters477c8d52006-05-27 19:21:47 +00001096};
1097
1098
1099static const formatdef *
1100whichtable(char **pfmt)
1101{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001102 const char *fmt = (*pfmt)++; /* May be backed out of later */
1103 switch (*fmt) {
1104 case '<':
1105 return lilendian_table;
1106 case '>':
1107 case '!': /* Network byte order is big-endian */
1108 return bigendian_table;
Ezio Melotti42da6632011-03-15 05:18:48 +02001109 case '=': { /* Host byte order -- different from native in alignment! */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001110 int n = 1;
1111 char *p = (char *) &n;
1112 if (*p == 1)
1113 return lilendian_table;
1114 else
1115 return bigendian_table;
1116 }
1117 default:
1118 --*pfmt; /* Back out of pointer increment */
1119 /* Fall through */
1120 case '@':
1121 return native_table;
1122 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001123}
1124
1125
1126/* Get the table entry for a format code */
1127
1128static const formatdef *
1129getentry(int c, const formatdef *f)
1130{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001131 for (; f->format != '\0'; f++) {
1132 if (f->format == c) {
1133 return f;
1134 }
1135 }
1136 PyErr_SetString(StructError, "bad char in struct format");
1137 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001138}
1139
1140
Mark Dickinson4a3acca2010-06-11 20:08:36 +00001141/* Align a size according to a format code. Return -1 on overflow. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001142
Mark Dickinson4a3acca2010-06-11 20:08:36 +00001143static Py_ssize_t
Thomas Wouters477c8d52006-05-27 19:21:47 +00001144align(Py_ssize_t size, char c, const formatdef *e)
1145{
Mark Dickinson4a3acca2010-06-11 20:08:36 +00001146 Py_ssize_t extra;
1147
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001148 if (e->format == c) {
Mark Dickinson4a3acca2010-06-11 20:08:36 +00001149 if (e->alignment && size > 0) {
1150 extra = (e->alignment - 1) - (size - 1) % (e->alignment);
1151 if (extra > PY_SSIZE_T_MAX - size)
1152 return -1;
1153 size += extra;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001154 }
1155 }
1156 return size;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001157}
1158
1159
1160/* calculate the size of a format string */
1161
1162static int
1163prepare_s(PyStructObject *self)
1164{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001165 const formatdef *f;
1166 const formatdef *e;
1167 formatcode *codes;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001168
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001169 const char *s;
1170 const char *fmt;
1171 char c;
Mark Dickinson4a3acca2010-06-11 20:08:36 +00001172 Py_ssize_t size, len, num, itemsize;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001173
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001174 fmt = PyBytes_AS_STRING(self->s_format);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001175
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001176 f = whichtable((char **)&fmt);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001177
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001178 s = fmt;
1179 size = 0;
1180 len = 0;
1181 while ((c = *s++) != '\0') {
1182 if (isspace(Py_CHARMASK(c)))
1183 continue;
1184 if ('0' <= c && c <= '9') {
1185 num = c - '0';
1186 while ('0' <= (c = *s++) && c <= '9') {
Mark Dickinson4a3acca2010-06-11 20:08:36 +00001187 /* overflow-safe version of
1188 if (num*10 + (c - '0') > PY_SSIZE_T_MAX) { ... } */
1189 if (num >= PY_SSIZE_T_MAX / 10 && (
1190 num > PY_SSIZE_T_MAX / 10 ||
1191 (c - '0') > PY_SSIZE_T_MAX % 10))
1192 goto overflow;
1193 num = num*10 + (c - '0');
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001194 }
1195 if (c == '\0')
1196 break;
1197 }
1198 else
1199 num = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001200
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001201 e = getentry(c, f);
1202 if (e == NULL)
1203 return -1;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001204
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001205 switch (c) {
1206 case 's': /* fall through */
1207 case 'p': len++; break;
1208 case 'x': break;
1209 default: len += num; break;
1210 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001211
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001212 itemsize = e->size;
1213 size = align(size, c, e);
Mark Dickinson4a3acca2010-06-11 20:08:36 +00001214 if (size == -1)
1215 goto overflow;
1216
1217 /* if (size + num * itemsize > PY_SSIZE_T_MAX) { ... } */
1218 if (num > (PY_SSIZE_T_MAX - size) / itemsize)
1219 goto overflow;
1220 size += num * itemsize;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001221 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001222
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001223 /* check for overflow */
1224 if ((len + 1) > (PY_SSIZE_T_MAX / sizeof(formatcode))) {
1225 PyErr_NoMemory();
1226 return -1;
1227 }
Amaury Forgeot d'Arc35c86582008-06-17 21:11:29 +00001228
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001229 self->s_size = size;
1230 self->s_len = len;
1231 codes = PyMem_MALLOC((len + 1) * sizeof(formatcode));
1232 if (codes == NULL) {
1233 PyErr_NoMemory();
1234 return -1;
1235 }
Mark Dickinson2da63cc2010-07-29 21:43:24 +00001236 /* Free any s_codes value left over from a previous initialization. */
1237 if (self->s_codes != NULL)
1238 PyMem_FREE(self->s_codes);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001239 self->s_codes = codes;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001240
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001241 s = fmt;
1242 size = 0;
1243 while ((c = *s++) != '\0') {
1244 if (isspace(Py_CHARMASK(c)))
1245 continue;
1246 if ('0' <= c && c <= '9') {
1247 num = c - '0';
1248 while ('0' <= (c = *s++) && c <= '9')
1249 num = num*10 + (c - '0');
1250 if (c == '\0')
1251 break;
1252 }
1253 else
1254 num = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001255
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001256 e = getentry(c, f);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001257
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001258 size = align(size, c, e);
1259 if (c == 's' || c == 'p') {
1260 codes->offset = size;
1261 codes->size = num;
1262 codes->fmtdef = e;
1263 codes++;
1264 size += num;
1265 } else if (c == 'x') {
1266 size += num;
1267 } else {
1268 while (--num >= 0) {
1269 codes->offset = size;
1270 codes->size = e->size;
1271 codes->fmtdef = e;
1272 codes++;
1273 size += e->size;
1274 }
1275 }
1276 }
1277 codes->fmtdef = NULL;
1278 codes->offset = size;
1279 codes->size = 0;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001280
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001281 return 0;
Mark Dickinson4a3acca2010-06-11 20:08:36 +00001282
1283 overflow:
1284 PyErr_SetString(StructError,
1285 "total struct size too long");
1286 return -1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001287}
1288
1289static PyObject *
1290s_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1291{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001292 PyObject *self;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001293
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001294 assert(type != NULL && type->tp_alloc != NULL);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001295
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001296 self = type->tp_alloc(type, 0);
1297 if (self != NULL) {
1298 PyStructObject *s = (PyStructObject*)self;
1299 Py_INCREF(Py_None);
1300 s->s_format = Py_None;
1301 s->s_codes = NULL;
1302 s->s_size = -1;
1303 s->s_len = -1;
1304 }
1305 return self;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001306}
1307
1308static int
1309s_init(PyObject *self, PyObject *args, PyObject *kwds)
1310{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001311 PyStructObject *soself = (PyStructObject *)self;
1312 PyObject *o_format = NULL;
1313 int ret = 0;
1314 static char *kwlist[] = {"format", 0};
Thomas Wouters477c8d52006-05-27 19:21:47 +00001315
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001316 assert(PyStruct_Check(self));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001317
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001318 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:Struct", kwlist,
1319 &o_format))
1320 return -1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001321
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001322 if (PyUnicode_Check(o_format)) {
1323 o_format = PyUnicode_AsASCIIString(o_format);
1324 if (o_format == NULL)
1325 return -1;
1326 }
1327 /* XXX support buffer interface, too */
1328 else {
1329 Py_INCREF(o_format);
1330 }
Christian Heimesa34706f2008-01-04 03:06:10 +00001331
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001332 if (!PyBytes_Check(o_format)) {
1333 Py_DECREF(o_format);
1334 PyErr_Format(PyExc_TypeError,
1335 "Struct() argument 1 must be bytes, not %.200s",
1336 Py_TYPE(o_format)->tp_name);
1337 return -1;
1338 }
Christian Heimesa34706f2008-01-04 03:06:10 +00001339
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001340 Py_CLEAR(soself->s_format);
1341 soself->s_format = o_format;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001342
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001343 ret = prepare_s(soself);
1344 return ret;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001345}
1346
1347static void
1348s_dealloc(PyStructObject *s)
1349{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001350 if (s->weakreflist != NULL)
1351 PyObject_ClearWeakRefs((PyObject *)s);
1352 if (s->s_codes != NULL) {
1353 PyMem_FREE(s->s_codes);
1354 }
1355 Py_XDECREF(s->s_format);
1356 Py_TYPE(s)->tp_free((PyObject *)s);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001357}
1358
1359static PyObject *
1360s_unpack_internal(PyStructObject *soself, char *startfrom) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001361 formatcode *code;
1362 Py_ssize_t i = 0;
1363 PyObject *result = PyTuple_New(soself->s_len);
1364 if (result == NULL)
1365 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001366
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001367 for (code = soself->s_codes; code->fmtdef != NULL; code++) {
1368 PyObject *v;
1369 const formatdef *e = code->fmtdef;
1370 const char *res = startfrom + code->offset;
1371 if (e->format == 's') {
1372 v = PyBytes_FromStringAndSize(res, code->size);
1373 } else if (e->format == 'p') {
1374 Py_ssize_t n = *(unsigned char*)res;
1375 if (n >= code->size)
1376 n = code->size - 1;
1377 v = PyBytes_FromStringAndSize(res + 1, n);
1378 } else {
1379 v = e->unpack(res, e);
1380 }
1381 if (v == NULL)
1382 goto fail;
1383 PyTuple_SET_ITEM(result, i++, v);
1384 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001385
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001386 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001387fail:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001388 Py_DECREF(result);
1389 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001390}
1391
1392
1393PyDoc_STRVAR(s_unpack__doc__,
Guido van Rossum913dd0b2007-04-13 03:33:53 +00001394"S.unpack(buffer) -> (v1, v2, ...)\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +00001395\n\
Mark Dickinsonf9e091a2010-06-12 19:18:51 +00001396Return a tuple containing values unpacked according to the format\n\
1397string S.format. Requires len(buffer) == S.size. See help(struct)\n\
1398for more on format strings.");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001399
1400static PyObject *
Guido van Rossum98297ee2007-11-06 21:34:58 +00001401s_unpack(PyObject *self, PyObject *input)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001402{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001403 Py_buffer vbuf;
1404 PyObject *result;
1405 PyStructObject *soself = (PyStructObject *)self;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001406
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001407 assert(PyStruct_Check(self));
1408 assert(soself->s_codes != NULL);
1409 if (PyObject_GetBuffer(input, &vbuf, PyBUF_SIMPLE) < 0)
1410 return NULL;
1411 if (vbuf.len != soself->s_size) {
1412 PyErr_Format(StructError,
1413 "unpack requires a bytes argument of length %zd",
1414 soself->s_size);
1415 PyBuffer_Release(&vbuf);
1416 return NULL;
1417 }
1418 result = s_unpack_internal(soself, vbuf.buf);
1419 PyBuffer_Release(&vbuf);
1420 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001421}
1422
1423PyDoc_STRVAR(s_unpack_from__doc__,
Mark Dickinson13305072010-06-13 09:18:16 +00001424"S.unpack_from(buffer, offset=0) -> (v1, v2, ...)\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +00001425\n\
Mark Dickinsonf9e091a2010-06-12 19:18:51 +00001426Return a tuple containing values unpacked according to the format\n\
1427string S.format. Requires len(buffer[offset:]) >= S.size. See\n\
1428help(struct) for more on format strings.");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001429
1430static PyObject *
1431s_unpack_from(PyObject *self, PyObject *args, PyObject *kwds)
1432{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001433 static char *kwlist[] = {"buffer", "offset", 0};
Guido van Rossum98297ee2007-11-06 21:34:58 +00001434
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001435 PyObject *input;
1436 Py_ssize_t offset = 0;
1437 Py_buffer vbuf;
1438 PyObject *result;
1439 PyStructObject *soself = (PyStructObject *)self;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001440
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001441 assert(PyStruct_Check(self));
1442 assert(soself->s_codes != NULL);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001443
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001444 if (!PyArg_ParseTupleAndKeywords(args, kwds,
1445 "O|n:unpack_from", kwlist,
1446 &input, &offset))
1447 return NULL;
1448 if (PyObject_GetBuffer(input, &vbuf, PyBUF_SIMPLE) < 0)
1449 return NULL;
1450 if (offset < 0)
1451 offset += vbuf.len;
1452 if (offset < 0 || vbuf.len - offset < soself->s_size) {
1453 PyErr_Format(StructError,
1454 "unpack_from requires a buffer of at least %zd bytes",
1455 soself->s_size);
1456 PyBuffer_Release(&vbuf);
1457 return NULL;
1458 }
1459 result = s_unpack_internal(soself, (char*)vbuf.buf + offset);
1460 PyBuffer_Release(&vbuf);
1461 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001462}
1463
1464
1465/*
1466 * Guts of the pack function.
1467 *
1468 * Takes a struct object, a tuple of arguments, and offset in that tuple of
1469 * argument for where to start processing the arguments for packing, and a
1470 * character buffer for writing the packed string. The caller must insure
1471 * that the buffer may contain the required length for packing the arguments.
1472 * 0 is returned on success, 1 is returned if there is an error.
1473 *
1474 */
1475static int
1476s_pack_internal(PyStructObject *soself, PyObject *args, int offset, char* buf)
1477{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001478 formatcode *code;
1479 /* XXX(nnorwitz): why does i need to be a local? can we use
1480 the offset parameter or do we need the wider width? */
1481 Py_ssize_t i;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001482
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001483 memset(buf, '\0', soself->s_size);
1484 i = offset;
1485 for (code = soself->s_codes; code->fmtdef != NULL; code++) {
1486 Py_ssize_t n;
1487 PyObject *v = PyTuple_GET_ITEM(args, i++);
1488 const formatdef *e = code->fmtdef;
1489 char *res = buf + code->offset;
1490 if (e->format == 's') {
1491 int isstring;
1492 void *p;
1493 if (PyUnicode_Check(v)) {
1494 v = _PyUnicode_AsDefaultEncodedString(v, NULL);
1495 if (v == NULL)
1496 return -1;
1497 }
1498 isstring = PyBytes_Check(v);
1499 if (!isstring && !PyByteArray_Check(v)) {
1500 PyErr_SetString(StructError,
1501 "argument for 's' must be a bytes or string");
1502 return -1;
1503 }
1504 if (isstring) {
1505 n = PyBytes_GET_SIZE(v);
1506 p = PyBytes_AS_STRING(v);
1507 }
1508 else {
1509 n = PyByteArray_GET_SIZE(v);
1510 p = PyByteArray_AS_STRING(v);
1511 }
1512 if (n > code->size)
1513 n = code->size;
1514 if (n > 0)
1515 memcpy(res, p, n);
1516 } else if (e->format == 'p') {
1517 int isstring;
1518 void *p;
1519 if (PyUnicode_Check(v)) {
1520 v = _PyUnicode_AsDefaultEncodedString(v, NULL);
1521 if (v == NULL)
1522 return -1;
1523 }
1524 isstring = PyBytes_Check(v);
1525 if (!isstring && !PyByteArray_Check(v)) {
1526 PyErr_SetString(StructError,
1527 "argument for 'p' must be a bytes or string");
1528 return -1;
1529 }
1530 if (isstring) {
1531 n = PyBytes_GET_SIZE(v);
1532 p = PyBytes_AS_STRING(v);
1533 }
1534 else {
1535 n = PyByteArray_GET_SIZE(v);
1536 p = PyByteArray_AS_STRING(v);
1537 }
1538 if (n > (code->size - 1))
1539 n = code->size - 1;
1540 if (n > 0)
1541 memcpy(res + 1, p, n);
1542 if (n > 255)
1543 n = 255;
1544 *res = Py_SAFE_DOWNCAST(n, Py_ssize_t, unsigned char);
1545 } else {
1546 if (e->pack(res, v, e) < 0) {
1547 if (PyLong_Check(v) && PyErr_ExceptionMatches(PyExc_OverflowError))
1548 PyErr_SetString(StructError,
1549 "long too large to convert to int");
1550 return -1;
1551 }
1552 }
1553 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001554
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001555 /* Success */
1556 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001557}
1558
1559
1560PyDoc_STRVAR(s_pack__doc__,
Benjamin Peterson4ae19462008-07-31 15:03:40 +00001561"S.pack(v1, v2, ...) -> bytes\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +00001562\n\
Mark Dickinsonf9e091a2010-06-12 19:18:51 +00001563Return a bytes object containing values v1, v2, ... packed according\n\
1564to the format string S.format. See help(struct) for more on format\n\
1565strings.");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001566
1567static PyObject *
1568s_pack(PyObject *self, PyObject *args)
1569{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001570 PyStructObject *soself;
1571 PyObject *result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001572
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001573 /* Validate arguments. */
1574 soself = (PyStructObject *)self;
1575 assert(PyStruct_Check(self));
1576 assert(soself->s_codes != NULL);
1577 if (PyTuple_GET_SIZE(args) != soself->s_len)
1578 {
1579 PyErr_Format(StructError,
1580 "pack requires exactly %zd arguments", soself->s_len);
1581 return NULL;
1582 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001583
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001584 /* Allocate a new string */
1585 result = PyBytes_FromStringAndSize((char *)NULL, soself->s_size);
1586 if (result == NULL)
1587 return NULL;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001588
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001589 /* Call the guts */
1590 if ( s_pack_internal(soself, args, 0, PyBytes_AS_STRING(result)) != 0 ) {
1591 Py_DECREF(result);
1592 return NULL;
1593 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001594
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001595 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001596}
1597
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001598PyDoc_STRVAR(s_pack_into__doc__,
1599"S.pack_into(buffer, offset, v1, v2, ...)\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +00001600\n\
Mark Dickinsonf9e091a2010-06-12 19:18:51 +00001601Pack the values v1, v2, ... according to the format string S.format\n\
1602and write the packed bytes into the writable buffer buf starting at\n\
1603offset. Note that the offset is a required argument. See\n\
1604help(struct) for more on format strings.");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001605
1606static PyObject *
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001607s_pack_into(PyObject *self, PyObject *args)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001608{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001609 PyStructObject *soself;
1610 char *buffer;
1611 Py_ssize_t buffer_len, offset;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001612
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001613 /* Validate arguments. +1 is for the first arg as buffer. */
1614 soself = (PyStructObject *)self;
1615 assert(PyStruct_Check(self));
1616 assert(soself->s_codes != NULL);
1617 if (PyTuple_GET_SIZE(args) != (soself->s_len + 2))
1618 {
1619 PyErr_Format(StructError,
1620 "pack_into requires exactly %zd arguments",
1621 (soself->s_len + 2));
1622 return NULL;
1623 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001624
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001625 /* Extract a writable memory buffer from the first argument */
1626 if ( PyObject_AsWriteBuffer(PyTuple_GET_ITEM(args, 0),
1627 (void**)&buffer, &buffer_len) == -1 ) {
1628 return NULL;
1629 }
1630 assert( buffer_len >= 0 );
Thomas Wouters477c8d52006-05-27 19:21:47 +00001631
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001632 /* Extract the offset from the first argument */
1633 offset = PyNumber_AsSsize_t(PyTuple_GET_ITEM(args, 1), PyExc_IndexError);
1634 if (offset == -1 && PyErr_Occurred())
1635 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001636
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001637 /* Support negative offsets. */
1638 if (offset < 0)
1639 offset += buffer_len;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001640
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001641 /* Check boundaries */
1642 if (offset < 0 || (buffer_len - offset) < soself->s_size) {
1643 PyErr_Format(StructError,
1644 "pack_into requires a buffer of at least %zd bytes",
1645 soself->s_size);
1646 return NULL;
1647 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001648
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001649 /* Call the guts */
1650 if ( s_pack_internal(soself, args, 2, buffer + offset) != 0 ) {
1651 return NULL;
1652 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001653
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001654 Py_RETURN_NONE;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001655}
1656
1657static PyObject *
1658s_get_format(PyStructObject *self, void *unused)
1659{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001660 Py_INCREF(self->s_format);
1661 return self->s_format;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001662}
1663
1664static PyObject *
1665s_get_size(PyStructObject *self, void *unused)
1666{
Christian Heimes217cfd12007-12-02 14:31:20 +00001667 return PyLong_FromSsize_t(self->s_size);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001668}
1669
1670/* List of functions */
1671
1672static struct PyMethodDef s_methods[] = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001673 {"pack", s_pack, METH_VARARGS, s_pack__doc__},
1674 {"pack_into", s_pack_into, METH_VARARGS, s_pack_into__doc__},
1675 {"unpack", s_unpack, METH_O, s_unpack__doc__},
1676 {"unpack_from", (PyCFunction)s_unpack_from, METH_VARARGS|METH_KEYWORDS,
1677 s_unpack_from__doc__},
1678 {NULL, NULL} /* sentinel */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001679};
1680
Mark Dickinsond12dfe22010-06-12 19:44:22 +00001681PyDoc_STRVAR(s__doc__,
1682"Struct(fmt) --> compiled struct object\n"
1683"\n"
1684"Return a new Struct object which writes and reads binary data according to\n"
1685"the format string fmt. See help(struct) for more on format strings.");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001686
1687#define OFF(x) offsetof(PyStructObject, x)
1688
1689static PyGetSetDef s_getsetlist[] = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001690 {"format", (getter)s_get_format, (setter)NULL, "struct format string", NULL},
1691 {"size", (getter)s_get_size, (setter)NULL, "struct size in bytes", NULL},
1692 {NULL} /* sentinel */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001693};
1694
1695static
1696PyTypeObject PyStructType = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001697 PyVarObject_HEAD_INIT(NULL, 0)
1698 "Struct",
1699 sizeof(PyStructObject),
1700 0,
1701 (destructor)s_dealloc, /* tp_dealloc */
1702 0, /* tp_print */
1703 0, /* tp_getattr */
1704 0, /* tp_setattr */
1705 0, /* tp_reserved */
1706 0, /* tp_repr */
1707 0, /* tp_as_number */
1708 0, /* tp_as_sequence */
1709 0, /* tp_as_mapping */
1710 0, /* tp_hash */
1711 0, /* tp_call */
1712 0, /* tp_str */
1713 PyObject_GenericGetAttr, /* tp_getattro */
1714 PyObject_GenericSetAttr, /* tp_setattro */
1715 0, /* tp_as_buffer */
1716 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
1717 s__doc__, /* tp_doc */
1718 0, /* tp_traverse */
1719 0, /* tp_clear */
1720 0, /* tp_richcompare */
1721 offsetof(PyStructObject, weakreflist), /* tp_weaklistoffset */
1722 0, /* tp_iter */
1723 0, /* tp_iternext */
1724 s_methods, /* tp_methods */
1725 NULL, /* tp_members */
1726 s_getsetlist, /* tp_getset */
1727 0, /* tp_base */
1728 0, /* tp_dict */
1729 0, /* tp_descr_get */
1730 0, /* tp_descr_set */
1731 0, /* tp_dictoffset */
1732 s_init, /* tp_init */
1733 PyType_GenericAlloc,/* tp_alloc */
1734 s_new, /* tp_new */
1735 PyObject_Del, /* tp_free */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001736};
1737
Christian Heimesa34706f2008-01-04 03:06:10 +00001738
1739/* ---- Standalone functions ---- */
1740
1741#define MAXCACHE 100
1742static PyObject *cache = NULL;
1743
1744static PyObject *
1745cache_struct(PyObject *fmt)
1746{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001747 PyObject * s_object;
Christian Heimesa34706f2008-01-04 03:06:10 +00001748
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001749 if (cache == NULL) {
1750 cache = PyDict_New();
1751 if (cache == NULL)
1752 return NULL;
1753 }
Christian Heimesa34706f2008-01-04 03:06:10 +00001754
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001755 s_object = PyDict_GetItem(cache, fmt);
1756 if (s_object != NULL) {
1757 Py_INCREF(s_object);
1758 return s_object;
1759 }
Christian Heimesa34706f2008-01-04 03:06:10 +00001760
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001761 s_object = PyObject_CallFunctionObjArgs((PyObject *)(&PyStructType), fmt, NULL);
1762 if (s_object != NULL) {
1763 if (PyDict_Size(cache) >= MAXCACHE)
1764 PyDict_Clear(cache);
1765 /* Attempt to cache the result */
1766 if (PyDict_SetItem(cache, fmt, s_object) == -1)
1767 PyErr_Clear();
1768 }
1769 return s_object;
Christian Heimesa34706f2008-01-04 03:06:10 +00001770}
1771
1772PyDoc_STRVAR(clearcache_doc,
1773"Clear the internal cache.");
1774
1775static PyObject *
1776clearcache(PyObject *self)
1777{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001778 Py_CLEAR(cache);
1779 Py_RETURN_NONE;
Christian Heimesa34706f2008-01-04 03:06:10 +00001780}
1781
1782PyDoc_STRVAR(calcsize_doc,
Mark Dickinsonf9e091a2010-06-12 19:18:51 +00001783"calcsize(fmt) -> integer\n\
1784\n\
1785Return size in bytes of the struct described by the format string fmt.");
Christian Heimesa34706f2008-01-04 03:06:10 +00001786
1787static PyObject *
1788calcsize(PyObject *self, PyObject *fmt)
1789{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001790 Py_ssize_t n;
1791 PyObject *s_object = cache_struct(fmt);
1792 if (s_object == NULL)
1793 return NULL;
1794 n = ((PyStructObject *)s_object)->s_size;
1795 Py_DECREF(s_object);
1796 return PyLong_FromSsize_t(n);
Christian Heimesa34706f2008-01-04 03:06:10 +00001797}
1798
1799PyDoc_STRVAR(pack_doc,
Mark Dickinsonf9e091a2010-06-12 19:18:51 +00001800"pack(fmt, v1, v2, ...) -> bytes\n\
1801\n\
1802Return a bytes object containing the values v1, v2, ... packed according\n\
1803to the format string fmt. See help(struct) for more on format strings.");
Christian Heimesa34706f2008-01-04 03:06:10 +00001804
1805static PyObject *
1806pack(PyObject *self, PyObject *args)
1807{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001808 PyObject *s_object, *fmt, *newargs, *result;
1809 Py_ssize_t n = PyTuple_GET_SIZE(args);
Christian Heimesa34706f2008-01-04 03:06:10 +00001810
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001811 if (n == 0) {
1812 PyErr_SetString(PyExc_TypeError, "missing format argument");
1813 return NULL;
1814 }
1815 fmt = PyTuple_GET_ITEM(args, 0);
1816 newargs = PyTuple_GetSlice(args, 1, n);
1817 if (newargs == NULL)
1818 return NULL;
Christian Heimesa34706f2008-01-04 03:06:10 +00001819
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001820 s_object = cache_struct(fmt);
1821 if (s_object == NULL) {
1822 Py_DECREF(newargs);
1823 return NULL;
1824 }
1825 result = s_pack(s_object, newargs);
1826 Py_DECREF(newargs);
1827 Py_DECREF(s_object);
1828 return result;
Christian Heimesa34706f2008-01-04 03:06:10 +00001829}
1830
1831PyDoc_STRVAR(pack_into_doc,
Mark Dickinsonf9e091a2010-06-12 19:18:51 +00001832"pack_into(fmt, buffer, offset, v1, v2, ...)\n\
1833\n\
1834Pack the values v1, v2, ... according to the format string fmt and write\n\
1835the packed bytes into the writable buffer buf starting at offset. Note\n\
1836that the offset is a required argument. See help(struct) for more\n\
1837on format strings.");
Christian Heimesa34706f2008-01-04 03:06:10 +00001838
1839static PyObject *
1840pack_into(PyObject *self, PyObject *args)
1841{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001842 PyObject *s_object, *fmt, *newargs, *result;
1843 Py_ssize_t n = PyTuple_GET_SIZE(args);
Christian Heimesa34706f2008-01-04 03:06:10 +00001844
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001845 if (n == 0) {
1846 PyErr_SetString(PyExc_TypeError, "missing format argument");
1847 return NULL;
1848 }
1849 fmt = PyTuple_GET_ITEM(args, 0);
1850 newargs = PyTuple_GetSlice(args, 1, n);
1851 if (newargs == NULL)
1852 return NULL;
Christian Heimesa34706f2008-01-04 03:06:10 +00001853
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001854 s_object = cache_struct(fmt);
1855 if (s_object == NULL) {
1856 Py_DECREF(newargs);
1857 return NULL;
1858 }
1859 result = s_pack_into(s_object, newargs);
1860 Py_DECREF(newargs);
1861 Py_DECREF(s_object);
1862 return result;
Christian Heimesa34706f2008-01-04 03:06:10 +00001863}
1864
1865PyDoc_STRVAR(unpack_doc,
Mark Dickinsonf9e091a2010-06-12 19:18:51 +00001866"unpack(fmt, buffer) -> (v1, v2, ...)\n\
1867\n\
1868Return a tuple containing values unpacked according to the format string\n\
1869fmt. Requires len(buffer) == calcsize(fmt). See help(struct) for more\n\
1870on format strings.");
Christian Heimesa34706f2008-01-04 03:06:10 +00001871
1872static PyObject *
1873unpack(PyObject *self, PyObject *args)
1874{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001875 PyObject *s_object, *fmt, *inputstr, *result;
Christian Heimesa34706f2008-01-04 03:06:10 +00001876
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001877 if (!PyArg_UnpackTuple(args, "unpack", 2, 2, &fmt, &inputstr))
1878 return NULL;
Christian Heimesa34706f2008-01-04 03:06:10 +00001879
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001880 s_object = cache_struct(fmt);
1881 if (s_object == NULL)
1882 return NULL;
1883 result = s_unpack(s_object, inputstr);
1884 Py_DECREF(s_object);
1885 return result;
Christian Heimesa34706f2008-01-04 03:06:10 +00001886}
1887
1888PyDoc_STRVAR(unpack_from_doc,
Mark Dickinson13305072010-06-13 09:18:16 +00001889"unpack_from(fmt, buffer, offset=0) -> (v1, v2, ...)\n\
Mark Dickinsonf9e091a2010-06-12 19:18:51 +00001890\n\
1891Return a tuple containing values unpacked according to the format string\n\
1892fmt. Requires len(buffer[offset:]) >= calcsize(fmt). See help(struct)\n\
1893for more on format strings.");
Christian Heimesa34706f2008-01-04 03:06:10 +00001894
1895static PyObject *
1896unpack_from(PyObject *self, PyObject *args, PyObject *kwds)
1897{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001898 PyObject *s_object, *fmt, *newargs, *result;
1899 Py_ssize_t n = PyTuple_GET_SIZE(args);
Christian Heimesa34706f2008-01-04 03:06:10 +00001900
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001901 if (n == 0) {
1902 PyErr_SetString(PyExc_TypeError, "missing format argument");
1903 return NULL;
1904 }
1905 fmt = PyTuple_GET_ITEM(args, 0);
1906 newargs = PyTuple_GetSlice(args, 1, n);
1907 if (newargs == NULL)
1908 return NULL;
Christian Heimesa34706f2008-01-04 03:06:10 +00001909
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001910 s_object = cache_struct(fmt);
1911 if (s_object == NULL) {
1912 Py_DECREF(newargs);
1913 return NULL;
1914 }
1915 result = s_unpack_from(s_object, newargs, kwds);
1916 Py_DECREF(newargs);
1917 Py_DECREF(s_object);
1918 return result;
Christian Heimesa34706f2008-01-04 03:06:10 +00001919}
1920
1921static struct PyMethodDef module_functions[] = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001922 {"_clearcache", (PyCFunction)clearcache, METH_NOARGS, clearcache_doc},
1923 {"calcsize", calcsize, METH_O, calcsize_doc},
1924 {"pack", pack, METH_VARARGS, pack_doc},
1925 {"pack_into", pack_into, METH_VARARGS, pack_into_doc},
1926 {"unpack", unpack, METH_VARARGS, unpack_doc},
1927 {"unpack_from", (PyCFunction)unpack_from,
1928 METH_VARARGS|METH_KEYWORDS, unpack_from_doc},
1929 {NULL, NULL} /* sentinel */
Christian Heimesa34706f2008-01-04 03:06:10 +00001930};
1931
1932
Thomas Wouters477c8d52006-05-27 19:21:47 +00001933/* Module initialization */
1934
Christian Heimesa34706f2008-01-04 03:06:10 +00001935PyDoc_STRVAR(module_doc,
1936"Functions to convert between Python values and C structs.\n\
Benjamin Peterson4ae19462008-07-31 15:03:40 +00001937Python bytes objects are used to hold the data representing the C struct\n\
Mark Dickinson015fa032009-10-08 16:02:50 +00001938and also as format strings (explained below) to describe the layout of data\n\
1939in the C struct.\n\
Christian Heimesa34706f2008-01-04 03:06:10 +00001940\n\
1941The optional first format char indicates byte order, size and alignment:\n\
Mark Dickinson015fa032009-10-08 16:02:50 +00001942 @: native order, size & alignment (default)\n\
1943 =: native order, std. size & alignment\n\
1944 <: little-endian, std. size & alignment\n\
1945 >: big-endian, std. size & alignment\n\
1946 !: same as >\n\
Christian Heimesa34706f2008-01-04 03:06:10 +00001947\n\
1948The remaining chars indicate types of args and must match exactly;\n\
1949these can be preceded by a decimal repeat count:\n\
1950 x: pad byte (no data); c:char; b:signed byte; B:unsigned byte;\n\
Mark Dickinson015fa032009-10-08 16:02:50 +00001951 ?: _Bool (requires C99; if not available, char is used instead)\n\
Christian Heimesa34706f2008-01-04 03:06:10 +00001952 h:short; H:unsigned short; i:int; I:unsigned int;\n\
1953 l:long; L:unsigned long; f:float; d:double.\n\
1954Special cases (preceding decimal count indicates length):\n\
1955 s:string (array of char); p: pascal string (with count byte).\n\
1956Special case (only available in native format):\n\
1957 P:an integer type that is wide enough to hold a pointer.\n\
1958Special case (not in native mode unless 'long long' in platform C):\n\
1959 q:long long; Q:unsigned long long\n\
1960Whitespace between formats is ignored.\n\
1961\n\
1962The variable struct.error is an exception raised on errors.\n");
1963
Martin v. Löwis1a214512008-06-11 05:26:20 +00001964
1965static struct PyModuleDef _structmodule = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001966 PyModuleDef_HEAD_INIT,
1967 "_struct",
1968 module_doc,
1969 -1,
1970 module_functions,
1971 NULL,
1972 NULL,
1973 NULL,
1974 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001975};
1976
Thomas Wouters477c8d52006-05-27 19:21:47 +00001977PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001978PyInit__struct(void)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001979{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001980 PyObject *ver, *m;
Christian Heimesa34706f2008-01-04 03:06:10 +00001981
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001982 ver = PyBytes_FromString("0.3");
1983 if (ver == NULL)
1984 return NULL;
Christian Heimesa34706f2008-01-04 03:06:10 +00001985
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001986 m = PyModule_Create(&_structmodule);
1987 if (m == NULL)
1988 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001989
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001990 Py_TYPE(&PyStructType) = &PyType_Type;
1991 if (PyType_Ready(&PyStructType) < 0)
1992 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001993
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001994 /* Check endian and swap in faster functions */
1995 {
1996 int one = 1;
1997 formatdef *native = native_table;
1998 formatdef *other, *ptr;
1999 if ((int)*(unsigned char*)&one)
2000 other = lilendian_table;
2001 else
2002 other = bigendian_table;
2003 /* Scan through the native table, find a matching
2004 entry in the endian table and swap in the
2005 native implementations whenever possible
2006 (64-bit platforms may not have "standard" sizes) */
2007 while (native->format != '\0' && other->format != '\0') {
2008 ptr = other;
2009 while (ptr->format != '\0') {
2010 if (ptr->format == native->format) {
2011 /* Match faster when formats are
2012 listed in the same order */
2013 if (ptr == other)
2014 other++;
2015 /* Only use the trick if the
2016 size matches */
2017 if (ptr->size != native->size)
2018 break;
2019 /* Skip float and double, could be
2020 "unknown" float format */
2021 if (ptr->format == 'd' || ptr->format == 'f')
2022 break;
2023 ptr->pack = native->pack;
2024 ptr->unpack = native->unpack;
2025 break;
2026 }
2027 ptr++;
2028 }
2029 native++;
2030 }
2031 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002032
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002033 /* Add some symbolic constants to the module */
2034 if (StructError == NULL) {
2035 StructError = PyErr_NewException("struct.error", NULL, NULL);
2036 if (StructError == NULL)
2037 return NULL;
2038 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002039
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002040 Py_INCREF(StructError);
2041 PyModule_AddObject(m, "error", StructError);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002042
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002043 Py_INCREF((PyObject*)&PyStructType);
2044 PyModule_AddObject(m, "Struct", (PyObject*)&PyStructType);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002045
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002046 PyModule_AddObject(m, "__version__", ver);
Christian Heimesa34706f2008-01-04 03:06:10 +00002047
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002048 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002049}