blob: 50fb1385bfbfdaa29d0be7ea14346daefd29a9e8 [file] [log] [blame]
Bob Ippolito232f3c92006-05-23 19:12:41 +00001/* struct module -- pack values into and (out of) strings */
2
3/* New version supporting byte order, alignment and size options,
4 character strings, and unsigned numbers */
5
Bob Ippolitoaa70a172006-05-26 20:25:23 +00006#define PY_SSIZE_T_CLEAN
7
Bob Ippolito232f3c92006-05-23 19:12:41 +00008#include "Python.h"
9#include "structseq.h"
10#include "structmember.h"
11#include <ctype.h>
12
Bob Ippolitod3611eb2006-05-23 19:31:23 +000013static PyTypeObject PyStructType;
Bob Ippolito232f3c92006-05-23 19:12:41 +000014
15/* compatibility macros */
16#if (PY_VERSION_HEX < 0x02050000)
17typedef int Py_ssize_t;
18#endif
19
Mark Dickinson154b7ad2010-03-07 16:24:45 +000020/* warning messages */
21#define FLOAT_COERCE_WARN "integer argument expected, got float"
22#define NON_INTEGER_WARN "integer argument expected, got non-integer " \
Antoine Pitrouc83ea132010-05-09 14:46:46 +000023 "(implicit conversion using __int__ is deprecated)"
Bob Ippolitoe6c9f982006-08-04 23:59:21 +000024
25
Bob Ippolito232f3c92006-05-23 19:12:41 +000026/* The translation function for each format character is table driven */
Bob Ippolito232f3c92006-05-23 19:12:41 +000027typedef struct _formatdef {
Antoine Pitrouc83ea132010-05-09 14:46:46 +000028 char format;
29 Py_ssize_t size;
30 Py_ssize_t alignment;
31 PyObject* (*unpack)(const char *,
32 const struct _formatdef *);
33 int (*pack)(char *, PyObject *,
34 const struct _formatdef *);
Bob Ippolito232f3c92006-05-23 19:12:41 +000035} formatdef;
36
37typedef struct _formatcode {
Antoine Pitrouc83ea132010-05-09 14:46:46 +000038 const struct _formatdef *fmtdef;
39 Py_ssize_t offset;
40 Py_ssize_t size;
Bob Ippolito232f3c92006-05-23 19:12:41 +000041} formatcode;
42
43/* Struct object interface */
44
45typedef struct {
Antoine Pitrouc83ea132010-05-09 14:46:46 +000046 PyObject_HEAD
47 Py_ssize_t s_size;
48 Py_ssize_t s_len;
49 formatcode *s_codes;
50 PyObject *s_format;
51 PyObject *weakreflist; /* List of weak references */
Bob Ippolito232f3c92006-05-23 19:12:41 +000052} PyStructObject;
53
Bob Ippolitoeb621272006-05-24 15:32:06 +000054
Bob Ippolito07c023b2006-05-23 19:32:25 +000055#define PyStruct_Check(op) PyObject_TypeCheck(op, &PyStructType)
Christian Heimese93237d2007-12-19 02:37:44 +000056#define PyStruct_CheckExact(op) (Py_TYPE(op) == &PyStructType)
Bob Ippolito232f3c92006-05-23 19:12:41 +000057
58
59/* Exception */
60
61static PyObject *StructError;
62
63
64/* Define various structs to figure out the alignments of types */
65
66
67typedef struct { char c; short x; } st_short;
68typedef struct { char c; int x; } st_int;
69typedef struct { char c; long x; } st_long;
70typedef struct { char c; float x; } st_float;
71typedef struct { char c; double x; } st_double;
72typedef struct { char c; void *x; } st_void_p;
73
74#define SHORT_ALIGN (sizeof(st_short) - sizeof(short))
75#define INT_ALIGN (sizeof(st_int) - sizeof(int))
76#define LONG_ALIGN (sizeof(st_long) - sizeof(long))
77#define FLOAT_ALIGN (sizeof(st_float) - sizeof(float))
78#define DOUBLE_ALIGN (sizeof(st_double) - sizeof(double))
79#define VOID_P_ALIGN (sizeof(st_void_p) - sizeof(void *))
80
81/* We can't support q and Q in native mode unless the compiler does;
82 in std mode, they're 8 bytes on all platforms. */
83#ifdef HAVE_LONG_LONG
84typedef struct { char c; PY_LONG_LONG x; } s_long_long;
85#define LONG_LONG_ALIGN (sizeof(s_long_long) - sizeof(PY_LONG_LONG))
86#endif
87
Martin v. Löwisaef4c6b2007-01-21 09:33:07 +000088#ifdef HAVE_C99_BOOL
89#define BOOL_TYPE _Bool
90typedef struct { char c; _Bool x; } s_bool;
91#define BOOL_ALIGN (sizeof(s_bool) - sizeof(BOOL_TYPE))
92#else
93#define BOOL_TYPE char
94#define BOOL_ALIGN 0
95#endif
96
Bob Ippolito232f3c92006-05-23 19:12:41 +000097#define STRINGIFY(x) #x
98
99#ifdef __powerc
100#pragma options align=reset
101#endif
102
Mark Dickinson5fd3af22009-07-07 15:08:28 +0000103static char *integer_codes = "bBhHiIlLqQ";
104
Bob Ippolito232f3c92006-05-23 19:12:41 +0000105/* Helper to get a PyLongObject by hook or by crook. Caller should decref. */
106
107static PyObject *
108get_pylong(PyObject *v)
109{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000110 PyObject *r, *w;
111 int converted = 0;
112 assert(v != NULL);
113 if (!PyInt_Check(v) && !PyLong_Check(v)) {
114 PyNumberMethods *m;
115 /* Not an integer; first try to use __index__ to
116 convert to an integer. If the __index__ method
117 doesn't exist, or raises a TypeError, try __int__.
118 Use of the latter is deprecated, and will fail in
119 Python 3.x. */
Mark Dickinson4846a8e2010-04-03 14:05:10 +0000120
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000121 m = Py_TYPE(v)->tp_as_number;
122 if (PyIndex_Check(v)) {
123 w = PyNumber_Index(v);
124 if (w != NULL) {
125 v = w;
126 /* successfully converted to an integer */
127 converted = 1;
128 }
129 else if (PyErr_ExceptionMatches(PyExc_TypeError)) {
130 PyErr_Clear();
131 }
132 else
133 return NULL;
134 }
135 if (!converted && m != NULL && m->nb_int != NULL) {
136 /* Special case warning message for floats, for
137 backwards compatibility. */
138 if (PyFloat_Check(v)) {
139 if (PyErr_WarnEx(
140 PyExc_DeprecationWarning,
141 FLOAT_COERCE_WARN, 1))
142 return NULL;
143 }
144 else {
145 if (PyErr_WarnEx(
146 PyExc_DeprecationWarning,
147 NON_INTEGER_WARN, 1))
148 return NULL;
149 }
150 v = m->nb_int(v);
151 if (v == NULL)
152 return NULL;
153 if (!PyInt_Check(v) && !PyLong_Check(v)) {
154 PyErr_SetString(PyExc_TypeError,
155 "__int__ method returned "
156 "non-integer");
157 return NULL;
158 }
159 converted = 1;
160 }
161 if (!converted) {
162 PyErr_SetString(StructError,
163 "cannot convert argument "
164 "to integer");
165 return NULL;
166 }
167 }
168 else
169 /* Ensure we own a reference to v. */
170 Py_INCREF(v);
Mark Dickinson154b7ad2010-03-07 16:24:45 +0000171
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000172 assert(PyInt_Check(v) || PyLong_Check(v));
173 if (PyInt_Check(v)) {
174 r = PyLong_FromLong(PyInt_AS_LONG(v));
175 Py_DECREF(v);
176 }
177 else if (PyLong_Check(v)) {
178 assert(PyLong_Check(v));
179 r = v;
180 }
181 else {
182 r = NULL; /* silence compiler warning about
183 possibly uninitialized variable */
184 assert(0); /* shouldn't ever get here */
185 }
Mark Dickinson154b7ad2010-03-07 16:24:45 +0000186
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000187 return r;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000188}
189
Mark Dickinson5fd3af22009-07-07 15:08:28 +0000190/* Helper to convert a Python object to a C long. Sets an exception
191 (struct.error for an inconvertible type, OverflowError for
192 out-of-range values) and returns -1 on error. */
Bob Ippolito232f3c92006-05-23 19:12:41 +0000193
194static int
195get_long(PyObject *v, long *p)
196{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000197 long x;
Mark Dickinson463dc4b2009-07-05 10:01:24 +0000198
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000199 v = get_pylong(v);
200 if (v == NULL)
201 return -1;
202 assert(PyLong_Check(v));
203 x = PyLong_AsLong(v);
204 Py_DECREF(v);
205 if (x == (long)-1 && PyErr_Occurred())
206 return -1;
207 *p = x;
208 return 0;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000209}
210
Bob Ippolito232f3c92006-05-23 19:12:41 +0000211/* Same, but handling unsigned long */
212
213static int
214get_ulong(PyObject *v, unsigned long *p)
215{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000216 unsigned long x;
Mark Dickinson463dc4b2009-07-05 10:01:24 +0000217
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000218 v = get_pylong(v);
219 if (v == NULL)
220 return -1;
221 assert(PyLong_Check(v));
222 x = PyLong_AsUnsignedLong(v);
223 Py_DECREF(v);
224 if (x == (unsigned long)-1 && PyErr_Occurred())
225 return -1;
226 *p = x;
227 return 0;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000228}
229
230#ifdef HAVE_LONG_LONG
231
232/* Same, but handling native long long. */
233
234static int
235get_longlong(PyObject *v, PY_LONG_LONG *p)
236{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000237 PY_LONG_LONG x;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000238
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000239 v = get_pylong(v);
240 if (v == NULL)
241 return -1;
242 assert(PyLong_Check(v));
243 x = PyLong_AsLongLong(v);
244 Py_DECREF(v);
245 if (x == (PY_LONG_LONG)-1 && PyErr_Occurred())
246 return -1;
247 *p = x;
248 return 0;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000249}
250
251/* Same, but handling native unsigned long long. */
252
253static int
254get_ulonglong(PyObject *v, unsigned PY_LONG_LONG *p)
255{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000256 unsigned PY_LONG_LONG x;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000257
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000258 v = get_pylong(v);
259 if (v == NULL)
260 return -1;
261 assert(PyLong_Check(v));
262 x = PyLong_AsUnsignedLongLong(v);
263 Py_DECREF(v);
264 if (x == (unsigned PY_LONG_LONG)-1 && PyErr_Occurred())
265 return -1;
266 *p = x;
267 return 0;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000268}
269
270#endif
271
272/* Floating point helpers */
273
274static PyObject *
275unpack_float(const char *p, /* start of 4-byte string */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000276 int le) /* true for little-endian, false for big-endian */
Bob Ippolito232f3c92006-05-23 19:12:41 +0000277{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000278 double x;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000279
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000280 x = _PyFloat_Unpack4((unsigned char *)p, le);
281 if (x == -1.0 && PyErr_Occurred())
282 return NULL;
283 return PyFloat_FromDouble(x);
Bob Ippolito232f3c92006-05-23 19:12:41 +0000284}
285
286static PyObject *
287unpack_double(const char *p, /* start of 8-byte string */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000288 int le) /* true for little-endian, false for big-endian */
Bob Ippolito232f3c92006-05-23 19:12:41 +0000289{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000290 double x;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000291
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000292 x = _PyFloat_Unpack8((unsigned char *)p, le);
293 if (x == -1.0 && PyErr_Occurred())
294 return NULL;
295 return PyFloat_FromDouble(x);
Bob Ippolito232f3c92006-05-23 19:12:41 +0000296}
297
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000298/* Helper to format the range error exceptions */
299static int
Armin Rigo162997e2006-05-29 17:59:47 +0000300_range_error(const formatdef *f, int is_unsigned)
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000301{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000302 /* ulargest is the largest unsigned value with f->size bytes.
303 * Note that the simpler:
304 * ((size_t)1 << (f->size * 8)) - 1
305 * doesn't work when f->size == sizeof(size_t) because C doesn't
306 * define what happens when a left shift count is >= the number of
307 * bits in the integer being shifted; e.g., on some boxes it doesn't
308 * shift at all when they're equal.
309 */
310 const size_t ulargest = (size_t)-1 >> ((SIZEOF_SIZE_T - f->size)*8);
311 assert(f->size >= 1 && f->size <= SIZEOF_SIZE_T);
312 if (is_unsigned)
313 PyErr_Format(StructError,
314 "'%c' format requires 0 <= number <= %zu",
315 f->format,
316 ulargest);
317 else {
318 const Py_ssize_t largest = (Py_ssize_t)(ulargest >> 1);
319 PyErr_Format(StructError,
320 "'%c' format requires %zd <= number <= %zd",
321 f->format,
322 ~ largest,
323 largest);
324 }
325 return -1;
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000326}
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000327
328
Bob Ippolito232f3c92006-05-23 19:12:41 +0000329
330/* A large number of small routines follow, with names of the form
331
Bob Ippolitoaa70a172006-05-26 20:25:23 +0000332 [bln][up]_TYPE
Bob Ippolito232f3c92006-05-23 19:12:41 +0000333
334 [bln] distiguishes among big-endian, little-endian and native.
335 [pu] distiguishes between pack (to struct) and unpack (from struct).
336 TYPE is one of char, byte, ubyte, etc.
337*/
338
339/* Native mode routines. ****************************************************/
340/* NOTE:
341 In all n[up]_<type> routines handling types larger than 1 byte, there is
342 *no* guarantee that the p pointer is properly aligned for each type,
343 therefore memcpy is called. An intermediate variable is used to
344 compensate for big-endian architectures.
345 Normally both the intermediate variable and the memcpy call will be
346 skipped by C optimisation in little-endian architectures (gcc >= 2.91
347 does this). */
348
349static PyObject *
350nu_char(const char *p, const formatdef *f)
351{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000352 return PyString_FromStringAndSize(p, 1);
Bob Ippolito232f3c92006-05-23 19:12:41 +0000353}
354
355static PyObject *
356nu_byte(const char *p, const formatdef *f)
357{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000358 return PyInt_FromLong((long) *(signed char *)p);
Bob Ippolito232f3c92006-05-23 19:12:41 +0000359}
360
361static PyObject *
362nu_ubyte(const char *p, const formatdef *f)
363{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000364 return PyInt_FromLong((long) *(unsigned char *)p);
Bob Ippolito232f3c92006-05-23 19:12:41 +0000365}
366
367static PyObject *
368nu_short(const char *p, const formatdef *f)
369{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000370 short x;
371 memcpy((char *)&x, p, sizeof x);
372 return PyInt_FromLong((long)x);
Bob Ippolito232f3c92006-05-23 19:12:41 +0000373}
374
375static PyObject *
376nu_ushort(const char *p, const formatdef *f)
377{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000378 unsigned short x;
379 memcpy((char *)&x, p, sizeof x);
380 return PyInt_FromLong((long)x);
Bob Ippolito232f3c92006-05-23 19:12:41 +0000381}
382
383static PyObject *
384nu_int(const char *p, const formatdef *f)
385{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000386 int x;
387 memcpy((char *)&x, p, sizeof x);
388 return PyInt_FromLong((long)x);
Bob Ippolito232f3c92006-05-23 19:12:41 +0000389}
390
391static PyObject *
392nu_uint(const char *p, const formatdef *f)
393{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000394 unsigned int x;
395 memcpy((char *)&x, p, sizeof x);
Bob Ippolitocd51ca52006-05-27 15:53:49 +0000396#if (SIZEOF_LONG > SIZEOF_INT)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000397 return PyInt_FromLong((long)x);
Bob Ippolitocd51ca52006-05-27 15:53:49 +0000398#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000399 if (x <= ((unsigned int)LONG_MAX))
400 return PyInt_FromLong((long)x);
401 return PyLong_FromUnsignedLong((unsigned long)x);
Bob Ippolitocd51ca52006-05-27 15:53:49 +0000402#endif
Bob Ippolito232f3c92006-05-23 19:12:41 +0000403}
404
405static PyObject *
406nu_long(const char *p, const formatdef *f)
407{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000408 long x;
409 memcpy((char *)&x, p, sizeof x);
410 return PyInt_FromLong(x);
Bob Ippolito232f3c92006-05-23 19:12:41 +0000411}
412
413static PyObject *
414nu_ulong(const char *p, const formatdef *f)
415{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000416 unsigned long x;
417 memcpy((char *)&x, p, sizeof x);
418 if (x <= LONG_MAX)
419 return PyInt_FromLong((long)x);
420 return PyLong_FromUnsignedLong(x);
Bob Ippolito232f3c92006-05-23 19:12:41 +0000421}
422
423/* Native mode doesn't support q or Q unless the platform C supports
424 long long (or, on Windows, __int64). */
425
426#ifdef HAVE_LONG_LONG
427
428static PyObject *
429nu_longlong(const char *p, const formatdef *f)
430{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000431 PY_LONG_LONG x;
432 memcpy((char *)&x, p, sizeof x);
433 if (x >= LONG_MIN && x <= LONG_MAX)
434 return PyInt_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
435 return PyLong_FromLongLong(x);
Bob Ippolito232f3c92006-05-23 19:12:41 +0000436}
437
438static PyObject *
439nu_ulonglong(const char *p, const formatdef *f)
440{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000441 unsigned PY_LONG_LONG x;
442 memcpy((char *)&x, p, sizeof x);
443 if (x <= LONG_MAX)
444 return PyInt_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
445 return PyLong_FromUnsignedLongLong(x);
Bob Ippolito232f3c92006-05-23 19:12:41 +0000446}
447
448#endif
449
450static PyObject *
Martin v. Löwisaef4c6b2007-01-21 09:33:07 +0000451nu_bool(const char *p, const formatdef *f)
452{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000453 BOOL_TYPE x;
454 memcpy((char *)&x, p, sizeof x);
455 return PyBool_FromLong(x != 0);
Martin v. Löwisaef4c6b2007-01-21 09:33:07 +0000456}
457
458
459static PyObject *
Bob Ippolito232f3c92006-05-23 19:12:41 +0000460nu_float(const char *p, const formatdef *f)
461{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000462 float x;
463 memcpy((char *)&x, p, sizeof x);
464 return PyFloat_FromDouble((double)x);
Bob Ippolito232f3c92006-05-23 19:12:41 +0000465}
466
467static PyObject *
468nu_double(const char *p, const formatdef *f)
469{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000470 double x;
471 memcpy((char *)&x, p, sizeof x);
472 return PyFloat_FromDouble(x);
Bob Ippolito232f3c92006-05-23 19:12:41 +0000473}
474
475static PyObject *
476nu_void_p(const char *p, const formatdef *f)
477{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000478 void *x;
479 memcpy((char *)&x, p, sizeof x);
480 return PyLong_FromVoidPtr(x);
Bob Ippolito232f3c92006-05-23 19:12:41 +0000481}
482
483static int
484np_byte(char *p, PyObject *v, const formatdef *f)
485{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000486 long x;
487 if (get_long(v, &x) < 0)
488 return -1;
489 if (x < -128 || x > 127){
490 PyErr_SetString(StructError,
491 "byte format requires -128 <= number <= 127");
492 return -1;
493 }
494 *p = (char)x;
495 return 0;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000496}
497
498static int
499np_ubyte(char *p, PyObject *v, const formatdef *f)
500{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000501 long x;
502 if (get_long(v, &x) < 0)
503 return -1;
504 if (x < 0 || x > 255){
505 PyErr_SetString(StructError,
506 "ubyte format requires 0 <= number <= 255");
507 return -1;
508 }
509 *p = (char)x;
510 return 0;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000511}
512
513static int
514np_char(char *p, PyObject *v, const formatdef *f)
515{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000516 if (!PyString_Check(v) || PyString_Size(v) != 1) {
517 PyErr_SetString(StructError,
518 "char format require string of length 1");
519 return -1;
520 }
521 *p = *PyString_AsString(v);
522 return 0;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000523}
524
525static int
526np_short(char *p, PyObject *v, const formatdef *f)
527{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000528 long x;
529 short y;
530 if (get_long(v, &x) < 0)
531 return -1;
532 if (x < SHRT_MIN || x > SHRT_MAX){
533 PyErr_SetString(StructError,
534 "short format requires " STRINGIFY(SHRT_MIN)
535 " <= number <= " STRINGIFY(SHRT_MAX));
536 return -1;
537 }
538 y = (short)x;
539 memcpy(p, (char *)&y, sizeof y);
540 return 0;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000541}
542
543static int
544np_ushort(char *p, PyObject *v, const formatdef *f)
545{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000546 long x;
547 unsigned short y;
548 if (get_long(v, &x) < 0)
549 return -1;
550 if (x < 0 || x > USHRT_MAX){
551 PyErr_SetString(StructError,
552 "ushort format requires 0 <= number <= " STRINGIFY(USHRT_MAX));
553 return -1;
554 }
555 y = (unsigned short)x;
556 memcpy(p, (char *)&y, sizeof y);
557 return 0;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000558}
559
560static int
561np_int(char *p, PyObject *v, const formatdef *f)
562{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000563 long x;
564 int y;
565 if (get_long(v, &x) < 0)
566 return -1;
Bob Ippolito90bd0a52006-05-27 11:47:12 +0000567#if (SIZEOF_LONG > SIZEOF_INT)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000568 if ((x < ((long)INT_MIN)) || (x > ((long)INT_MAX)))
569 return _range_error(f, 0);
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000570#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000571 y = (int)x;
572 memcpy(p, (char *)&y, sizeof y);
573 return 0;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000574}
575
576static int
577np_uint(char *p, PyObject *v, const formatdef *f)
578{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000579 unsigned long x;
580 unsigned int y;
581 if (get_ulong(v, &x) < 0)
582 return -1;
583 y = (unsigned int)x;
Bob Ippolito90bd0a52006-05-27 11:47:12 +0000584#if (SIZEOF_LONG > SIZEOF_INT)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000585 if (x > ((unsigned long)UINT_MAX))
586 return _range_error(f, 1);
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000587#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000588 memcpy(p, (char *)&y, sizeof y);
589 return 0;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000590}
591
592static int
593np_long(char *p, PyObject *v, const formatdef *f)
594{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000595 long x;
596 if (get_long(v, &x) < 0)
597 return -1;
598 memcpy(p, (char *)&x, sizeof x);
599 return 0;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000600}
601
602static int
603np_ulong(char *p, PyObject *v, const formatdef *f)
604{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000605 unsigned long x;
606 if (get_ulong(v, &x) < 0)
607 return -1;
608 memcpy(p, (char *)&x, sizeof x);
609 return 0;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000610}
611
612#ifdef HAVE_LONG_LONG
613
614static int
615np_longlong(char *p, PyObject *v, const formatdef *f)
616{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000617 PY_LONG_LONG x;
618 if (get_longlong(v, &x) < 0)
619 return -1;
620 memcpy(p, (char *)&x, sizeof x);
621 return 0;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000622}
623
624static int
625np_ulonglong(char *p, PyObject *v, const formatdef *f)
626{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000627 unsigned PY_LONG_LONG x;
628 if (get_ulonglong(v, &x) < 0)
629 return -1;
630 memcpy(p, (char *)&x, sizeof x);
631 return 0;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000632}
633#endif
634
Martin v. Löwisaef4c6b2007-01-21 09:33:07 +0000635
636static int
637np_bool(char *p, PyObject *v, const formatdef *f)
638{
Benjamin Peterson489113f2010-07-07 19:03:36 +0000639 int y;
640 BOOL_TYPE x;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000641 y = PyObject_IsTrue(v);
Benjamin Peterson489113f2010-07-07 19:03:36 +0000642 if (y < 0)
643 return -1;
644 x = y;
645 memcpy(p, (char *)&x, sizeof x);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000646 return 0;
Martin v. Löwisaef4c6b2007-01-21 09:33:07 +0000647}
648
Bob Ippolito232f3c92006-05-23 19:12:41 +0000649static int
650np_float(char *p, PyObject *v, const formatdef *f)
651{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000652 float x = (float)PyFloat_AsDouble(v);
653 if (x == -1 && PyErr_Occurred()) {
654 PyErr_SetString(StructError,
655 "required argument is not a float");
656 return -1;
657 }
658 memcpy(p, (char *)&x, sizeof x);
659 return 0;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000660}
661
662static int
663np_double(char *p, PyObject *v, const formatdef *f)
664{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000665 double x = PyFloat_AsDouble(v);
666 if (x == -1 && PyErr_Occurred()) {
667 PyErr_SetString(StructError,
668 "required argument is not a float");
669 return -1;
670 }
671 memcpy(p, (char *)&x, sizeof(double));
672 return 0;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000673}
674
675static int
676np_void_p(char *p, PyObject *v, const formatdef *f)
677{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000678 void *x;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000679
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000680 v = get_pylong(v);
681 if (v == NULL)
682 return -1;
683 assert(PyLong_Check(v));
684 x = PyLong_AsVoidPtr(v);
685 Py_DECREF(v);
686 if (x == NULL && PyErr_Occurred())
687 return -1;
688 memcpy(p, (char *)&x, sizeof x);
689 return 0;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000690}
691
692static formatdef native_table[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000693 {'x', sizeof(char), 0, NULL},
694 {'b', sizeof(char), 0, nu_byte, np_byte},
695 {'B', sizeof(char), 0, nu_ubyte, np_ubyte},
696 {'c', sizeof(char), 0, nu_char, np_char},
697 {'s', sizeof(char), 0, NULL},
698 {'p', sizeof(char), 0, NULL},
699 {'h', sizeof(short), SHORT_ALIGN, nu_short, np_short},
700 {'H', sizeof(short), SHORT_ALIGN, nu_ushort, np_ushort},
701 {'i', sizeof(int), INT_ALIGN, nu_int, np_int},
702 {'I', sizeof(int), INT_ALIGN, nu_uint, np_uint},
703 {'l', sizeof(long), LONG_ALIGN, nu_long, np_long},
704 {'L', sizeof(long), LONG_ALIGN, nu_ulong, np_ulong},
Bob Ippolito232f3c92006-05-23 19:12:41 +0000705#ifdef HAVE_LONG_LONG
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000706 {'q', sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_longlong, np_longlong},
707 {'Q', sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_ulonglong,np_ulonglong},
Bob Ippolito232f3c92006-05-23 19:12:41 +0000708#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000709 {'?', sizeof(BOOL_TYPE), BOOL_ALIGN, nu_bool, np_bool},
710 {'f', sizeof(float), FLOAT_ALIGN, nu_float, np_float},
711 {'d', sizeof(double), DOUBLE_ALIGN, nu_double, np_double},
712 {'P', sizeof(void *), VOID_P_ALIGN, nu_void_p, np_void_p},
713 {0}
Bob Ippolito232f3c92006-05-23 19:12:41 +0000714};
715
716/* Big-endian routines. *****************************************************/
717
718static PyObject *
719bu_int(const char *p, const formatdef *f)
720{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000721 long x = 0;
722 Py_ssize_t i = f->size;
723 const unsigned char *bytes = (const unsigned char *)p;
724 do {
725 x = (x<<8) | *bytes++;
726 } while (--i > 0);
727 /* Extend the sign bit. */
728 if (SIZEOF_LONG > f->size)
729 x |= -(x & (1L << ((8 * f->size) - 1)));
730 return PyInt_FromLong(x);
Bob Ippolito232f3c92006-05-23 19:12:41 +0000731}
732
733static PyObject *
734bu_uint(const char *p, const formatdef *f)
735{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000736 unsigned long x = 0;
737 Py_ssize_t i = f->size;
738 const unsigned char *bytes = (const unsigned char *)p;
739 do {
740 x = (x<<8) | *bytes++;
741 } while (--i > 0);
742 if (x <= LONG_MAX)
743 return PyInt_FromLong((long)x);
744 return PyLong_FromUnsignedLong(x);
Bob Ippolito232f3c92006-05-23 19:12:41 +0000745}
746
747static PyObject *
748bu_longlong(const char *p, const formatdef *f)
749{
Bob Ippolito90bd0a52006-05-27 11:47:12 +0000750#ifdef HAVE_LONG_LONG
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000751 PY_LONG_LONG x = 0;
752 Py_ssize_t i = f->size;
753 const unsigned char *bytes = (const unsigned char *)p;
754 do {
755 x = (x<<8) | *bytes++;
756 } while (--i > 0);
757 /* Extend the sign bit. */
758 if (SIZEOF_LONG_LONG > f->size)
759 x |= -(x & ((PY_LONG_LONG)1 << ((8 * f->size) - 1)));
760 if (x >= LONG_MIN && x <= LONG_MAX)
761 return PyInt_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
762 return PyLong_FromLongLong(x);
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000763#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000764 return _PyLong_FromByteArray((const unsigned char *)p,
765 8,
766 0, /* little-endian */
767 1 /* signed */);
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000768#endif
Bob Ippolito232f3c92006-05-23 19:12:41 +0000769}
770
771static PyObject *
772bu_ulonglong(const char *p, const formatdef *f)
773{
Bob Ippolito90bd0a52006-05-27 11:47:12 +0000774#ifdef HAVE_LONG_LONG
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000775 unsigned PY_LONG_LONG x = 0;
776 Py_ssize_t i = f->size;
777 const unsigned char *bytes = (const unsigned char *)p;
778 do {
779 x = (x<<8) | *bytes++;
780 } while (--i > 0);
781 if (x <= LONG_MAX)
782 return PyInt_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
783 return PyLong_FromUnsignedLongLong(x);
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000784#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000785 return _PyLong_FromByteArray((const unsigned char *)p,
786 8,
787 0, /* little-endian */
788 0 /* signed */);
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000789#endif
Bob Ippolito232f3c92006-05-23 19:12:41 +0000790}
791
792static PyObject *
793bu_float(const char *p, const formatdef *f)
794{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000795 return unpack_float(p, 0);
Bob Ippolito232f3c92006-05-23 19:12:41 +0000796}
797
798static PyObject *
799bu_double(const char *p, const formatdef *f)
800{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000801 return unpack_double(p, 0);
Bob Ippolito232f3c92006-05-23 19:12:41 +0000802}
803
Martin v. Löwisaef4c6b2007-01-21 09:33:07 +0000804static PyObject *
805bu_bool(const char *p, const formatdef *f)
806{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000807 char x;
808 memcpy((char *)&x, p, sizeof x);
809 return PyBool_FromLong(x != 0);
Martin v. Löwisaef4c6b2007-01-21 09:33:07 +0000810}
811
Bob Ippolito232f3c92006-05-23 19:12:41 +0000812static int
813bp_int(char *p, PyObject *v, const formatdef *f)
814{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000815 long x;
816 Py_ssize_t i;
817 if (get_long(v, &x) < 0)
818 return -1;
819 i = f->size;
820 if (i != SIZEOF_LONG) {
821 if ((i == 2) && (x < -32768 || x > 32767))
822 return _range_error(f, 0);
Bob Ippolito90bd0a52006-05-27 11:47:12 +0000823#if (SIZEOF_LONG != 4)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000824 else if ((i == 4) && (x < -2147483648L || x > 2147483647L))
825 return _range_error(f, 0);
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000826#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000827 }
828 do {
829 p[--i] = (char)x;
830 x >>= 8;
831 } while (i > 0);
832 return 0;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000833}
834
835static int
836bp_uint(char *p, PyObject *v, const formatdef *f)
837{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000838 unsigned long x;
839 Py_ssize_t i;
840 if (get_ulong(v, &x) < 0)
841 return -1;
842 i = f->size;
843 if (i != SIZEOF_LONG) {
844 unsigned long maxint = 1;
845 maxint <<= (unsigned long)(i * 8);
846 if (x >= maxint)
847 return _range_error(f, 1);
848 }
849 do {
850 p[--i] = (char)x;
851 x >>= 8;
852 } while (i > 0);
853 return 0;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000854}
855
856static int
857bp_longlong(char *p, PyObject *v, const formatdef *f)
858{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000859 int res;
860 v = get_pylong(v);
861 if (v == NULL)
862 return -1;
863 res = _PyLong_AsByteArray((PyLongObject *)v,
864 (unsigned char *)p,
865 8,
866 0, /* little_endian */
867 1 /* signed */);
868 Py_DECREF(v);
869 return res;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000870}
871
872static int
873bp_ulonglong(char *p, PyObject *v, const formatdef *f)
874{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000875 int res;
876 v = get_pylong(v);
877 if (v == NULL)
878 return -1;
879 res = _PyLong_AsByteArray((PyLongObject *)v,
880 (unsigned char *)p,
881 8,
882 0, /* little_endian */
883 0 /* signed */);
884 Py_DECREF(v);
885 return res;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000886}
887
888static int
889bp_float(char *p, PyObject *v, const formatdef *f)
890{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000891 double x = PyFloat_AsDouble(v);
892 if (x == -1 && PyErr_Occurred()) {
893 PyErr_SetString(StructError,
894 "required argument is not a float");
895 return -1;
896 }
897 return _PyFloat_Pack4(x, (unsigned char *)p, 0);
Bob Ippolito232f3c92006-05-23 19:12:41 +0000898}
899
900static int
901bp_double(char *p, PyObject *v, const formatdef *f)
902{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000903 double x = PyFloat_AsDouble(v);
904 if (x == -1 && PyErr_Occurred()) {
905 PyErr_SetString(StructError,
906 "required argument is not a float");
907 return -1;
908 }
909 return _PyFloat_Pack8(x, (unsigned char *)p, 0);
Bob Ippolito232f3c92006-05-23 19:12:41 +0000910}
911
Martin v. Löwisaef4c6b2007-01-21 09:33:07 +0000912static int
913bp_bool(char *p, PyObject *v, const formatdef *f)
914{
Mark Dickinsoncac0b832010-07-18 07:55:55 +0000915 int y;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000916 y = PyObject_IsTrue(v);
Benjamin Peterson489113f2010-07-07 19:03:36 +0000917 if (y < 0)
918 return -1;
Mark Dickinsoncac0b832010-07-18 07:55:55 +0000919 *p = (char)y;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000920 return 0;
Martin v. Löwisaef4c6b2007-01-21 09:33:07 +0000921}
922
Bob Ippolito232f3c92006-05-23 19:12:41 +0000923static formatdef bigendian_table[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000924 {'x', 1, 0, NULL},
925 {'b', 1, 0, nu_byte, np_byte},
926 {'B', 1, 0, nu_ubyte, np_ubyte},
927 {'c', 1, 0, nu_char, np_char},
928 {'s', 1, 0, NULL},
929 {'p', 1, 0, NULL},
930 {'h', 2, 0, bu_int, bp_int},
931 {'H', 2, 0, bu_uint, bp_uint},
932 {'i', 4, 0, bu_int, bp_int},
933 {'I', 4, 0, bu_uint, bp_uint},
934 {'l', 4, 0, bu_int, bp_int},
935 {'L', 4, 0, bu_uint, bp_uint},
936 {'q', 8, 0, bu_longlong, bp_longlong},
937 {'Q', 8, 0, bu_ulonglong, bp_ulonglong},
938 {'?', 1, 0, bu_bool, bp_bool},
939 {'f', 4, 0, bu_float, bp_float},
940 {'d', 8, 0, bu_double, bp_double},
941 {0}
Bob Ippolito232f3c92006-05-23 19:12:41 +0000942};
943
944/* Little-endian routines. *****************************************************/
945
946static PyObject *
947lu_int(const char *p, const formatdef *f)
948{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000949 long x = 0;
950 Py_ssize_t i = f->size;
951 const unsigned char *bytes = (const unsigned char *)p;
952 do {
953 x = (x<<8) | bytes[--i];
954 } while (i > 0);
955 /* Extend the sign bit. */
956 if (SIZEOF_LONG > f->size)
957 x |= -(x & (1L << ((8 * f->size) - 1)));
958 return PyInt_FromLong(x);
Bob Ippolito232f3c92006-05-23 19:12:41 +0000959}
960
961static PyObject *
962lu_uint(const char *p, const formatdef *f)
963{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000964 unsigned long x = 0;
965 Py_ssize_t i = f->size;
966 const unsigned char *bytes = (const unsigned char *)p;
967 do {
968 x = (x<<8) | bytes[--i];
969 } while (i > 0);
970 if (x <= LONG_MAX)
971 return PyInt_FromLong((long)x);
972 return PyLong_FromUnsignedLong((long)x);
Bob Ippolito232f3c92006-05-23 19:12:41 +0000973}
974
975static PyObject *
976lu_longlong(const char *p, const formatdef *f)
977{
Bob Ippolito90bd0a52006-05-27 11:47:12 +0000978#ifdef HAVE_LONG_LONG
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000979 PY_LONG_LONG x = 0;
980 Py_ssize_t i = f->size;
981 const unsigned char *bytes = (const unsigned char *)p;
982 do {
983 x = (x<<8) | bytes[--i];
984 } while (i > 0);
985 /* Extend the sign bit. */
986 if (SIZEOF_LONG_LONG > f->size)
987 x |= -(x & ((PY_LONG_LONG)1 << ((8 * f->size) - 1)));
988 if (x >= LONG_MIN && x <= LONG_MAX)
989 return PyInt_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
990 return PyLong_FromLongLong(x);
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000991#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000992 return _PyLong_FromByteArray((const unsigned char *)p,
993 8,
994 1, /* little-endian */
995 1 /* signed */);
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000996#endif
Bob Ippolito232f3c92006-05-23 19:12:41 +0000997}
998
999static PyObject *
1000lu_ulonglong(const char *p, const formatdef *f)
1001{
Bob Ippolito90bd0a52006-05-27 11:47:12 +00001002#ifdef HAVE_LONG_LONG
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001003 unsigned PY_LONG_LONG x = 0;
1004 Py_ssize_t i = f->size;
1005 const unsigned char *bytes = (const unsigned char *)p;
1006 do {
1007 x = (x<<8) | bytes[--i];
1008 } while (i > 0);
1009 if (x <= LONG_MAX)
1010 return PyInt_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
1011 return PyLong_FromUnsignedLongLong(x);
Bob Ippolito94f68ee2006-05-25 18:44:50 +00001012#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001013 return _PyLong_FromByteArray((const unsigned char *)p,
1014 8,
1015 1, /* little-endian */
1016 0 /* signed */);
Bob Ippolito94f68ee2006-05-25 18:44:50 +00001017#endif
Bob Ippolito232f3c92006-05-23 19:12:41 +00001018}
1019
1020static PyObject *
1021lu_float(const char *p, const formatdef *f)
1022{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001023 return unpack_float(p, 1);
Bob Ippolito232f3c92006-05-23 19:12:41 +00001024}
1025
1026static PyObject *
1027lu_double(const char *p, const formatdef *f)
1028{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001029 return unpack_double(p, 1);
Bob Ippolito232f3c92006-05-23 19:12:41 +00001030}
1031
1032static int
1033lp_int(char *p, PyObject *v, const formatdef *f)
1034{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001035 long x;
1036 Py_ssize_t i;
1037 if (get_long(v, &x) < 0)
1038 return -1;
1039 i = f->size;
1040 if (i != SIZEOF_LONG) {
1041 if ((i == 2) && (x < -32768 || x > 32767))
1042 return _range_error(f, 0);
Bob Ippolito90bd0a52006-05-27 11:47:12 +00001043#if (SIZEOF_LONG != 4)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001044 else if ((i == 4) && (x < -2147483648L || x > 2147483647L))
1045 return _range_error(f, 0);
Bob Ippolitoe27337b2006-05-26 13:15:44 +00001046#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001047 }
1048 do {
1049 *p++ = (char)x;
1050 x >>= 8;
1051 } while (--i > 0);
1052 return 0;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001053}
1054
1055static int
1056lp_uint(char *p, PyObject *v, const formatdef *f)
1057{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001058 unsigned long x;
1059 Py_ssize_t i;
1060 if (get_ulong(v, &x) < 0)
1061 return -1;
1062 i = f->size;
1063 if (i != SIZEOF_LONG) {
1064 unsigned long maxint = 1;
1065 maxint <<= (unsigned long)(i * 8);
1066 if (x >= maxint)
1067 return _range_error(f, 1);
1068 }
1069 do {
1070 *p++ = (char)x;
1071 x >>= 8;
1072 } while (--i > 0);
1073 return 0;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001074}
1075
1076static int
1077lp_longlong(char *p, PyObject *v, const formatdef *f)
1078{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001079 int res;
1080 v = get_pylong(v);
1081 if (v == NULL)
1082 return -1;
1083 res = _PyLong_AsByteArray((PyLongObject*)v,
1084 (unsigned char *)p,
1085 8,
1086 1, /* little_endian */
1087 1 /* signed */);
1088 Py_DECREF(v);
1089 return res;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001090}
1091
1092static int
1093lp_ulonglong(char *p, PyObject *v, const formatdef *f)
1094{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001095 int res;
1096 v = get_pylong(v);
1097 if (v == NULL)
1098 return -1;
1099 res = _PyLong_AsByteArray((PyLongObject*)v,
1100 (unsigned char *)p,
1101 8,
1102 1, /* little_endian */
1103 0 /* signed */);
1104 Py_DECREF(v);
1105 return res;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001106}
1107
1108static int
1109lp_float(char *p, PyObject *v, const formatdef *f)
1110{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001111 double x = PyFloat_AsDouble(v);
1112 if (x == -1 && PyErr_Occurred()) {
1113 PyErr_SetString(StructError,
1114 "required argument is not a float");
1115 return -1;
1116 }
1117 return _PyFloat_Pack4(x, (unsigned char *)p, 1);
Bob Ippolito232f3c92006-05-23 19:12:41 +00001118}
1119
1120static int
1121lp_double(char *p, PyObject *v, const formatdef *f)
1122{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001123 double x = PyFloat_AsDouble(v);
1124 if (x == -1 && PyErr_Occurred()) {
1125 PyErr_SetString(StructError,
1126 "required argument is not a float");
1127 return -1;
1128 }
1129 return _PyFloat_Pack8(x, (unsigned char *)p, 1);
Bob Ippolito232f3c92006-05-23 19:12:41 +00001130}
1131
1132static formatdef lilendian_table[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001133 {'x', 1, 0, NULL},
1134 {'b', 1, 0, nu_byte, np_byte},
1135 {'B', 1, 0, nu_ubyte, np_ubyte},
1136 {'c', 1, 0, nu_char, np_char},
1137 {'s', 1, 0, NULL},
1138 {'p', 1, 0, NULL},
1139 {'h', 2, 0, lu_int, lp_int},
1140 {'H', 2, 0, lu_uint, lp_uint},
1141 {'i', 4, 0, lu_int, lp_int},
1142 {'I', 4, 0, lu_uint, lp_uint},
1143 {'l', 4, 0, lu_int, lp_int},
1144 {'L', 4, 0, lu_uint, lp_uint},
1145 {'q', 8, 0, lu_longlong, lp_longlong},
1146 {'Q', 8, 0, lu_ulonglong, lp_ulonglong},
1147 {'?', 1, 0, bu_bool, bp_bool}, /* Std rep not endian dep,
1148 but potentially different from native rep -- reuse bx_bool funcs. */
1149 {'f', 4, 0, lu_float, lp_float},
1150 {'d', 8, 0, lu_double, lp_double},
1151 {0}
Bob Ippolito232f3c92006-05-23 19:12:41 +00001152};
1153
1154
1155static const formatdef *
1156whichtable(char **pfmt)
1157{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001158 const char *fmt = (*pfmt)++; /* May be backed out of later */
1159 switch (*fmt) {
1160 case '<':
1161 return lilendian_table;
1162 case '>':
1163 case '!': /* Network byte order is big-endian */
1164 return bigendian_table;
Ezio Melotti24b07bc2011-03-15 18:55:01 +02001165 case '=': { /* Host byte order -- different from native in alignment! */
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001166 int n = 1;
1167 char *p = (char *) &n;
1168 if (*p == 1)
1169 return lilendian_table;
1170 else
1171 return bigendian_table;
1172 }
1173 default:
1174 --*pfmt; /* Back out of pointer increment */
1175 /* Fall through */
1176 case '@':
1177 return native_table;
1178 }
Bob Ippolito232f3c92006-05-23 19:12:41 +00001179}
1180
1181
1182/* Get the table entry for a format code */
1183
1184static const formatdef *
1185getentry(int c, const formatdef *f)
1186{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001187 for (; f->format != '\0'; f++) {
1188 if (f->format == c) {
1189 return f;
1190 }
1191 }
1192 PyErr_SetString(StructError, "bad char in struct format");
1193 return NULL;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001194}
1195
1196
Mark Dickinson40228912010-06-11 20:27:05 +00001197/* Align a size according to a format code. Return -1 on overflow. */
Bob Ippolito232f3c92006-05-23 19:12:41 +00001198
Mark Dickinson40228912010-06-11 20:27:05 +00001199static Py_ssize_t
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001200align(Py_ssize_t size, char c, const formatdef *e)
Bob Ippolito232f3c92006-05-23 19:12:41 +00001201{
Mark Dickinson40228912010-06-11 20:27:05 +00001202 Py_ssize_t extra;
1203
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001204 if (e->format == c) {
Mark Dickinson40228912010-06-11 20:27:05 +00001205 if (e->alignment && size > 0) {
1206 extra = (e->alignment - 1) - (size - 1) % (e->alignment);
1207 if (extra > PY_SSIZE_T_MAX - size)
1208 return -1;
1209 size += extra;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001210 }
1211 }
1212 return size;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001213}
1214
1215
1216/* calculate the size of a format string */
1217
1218static int
1219prepare_s(PyStructObject *self)
1220{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001221 const formatdef *f;
1222 const formatdef *e;
1223 formatcode *codes;
Tim Petersc2b550e2006-05-31 14:28:07 +00001224
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001225 const char *s;
1226 const char *fmt;
1227 char c;
Mark Dickinson40228912010-06-11 20:27:05 +00001228 Py_ssize_t size, len, num, itemsize;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001229
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001230 fmt = PyString_AS_STRING(self->s_format);
Bob Ippolito232f3c92006-05-23 19:12:41 +00001231
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001232 f = whichtable((char **)&fmt);
Tim Petersc2b550e2006-05-31 14:28:07 +00001233
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001234 s = fmt;
1235 size = 0;
1236 len = 0;
1237 while ((c = *s++) != '\0') {
1238 if (isspace(Py_CHARMASK(c)))
1239 continue;
1240 if ('0' <= c && c <= '9') {
1241 num = c - '0';
1242 while ('0' <= (c = *s++) && c <= '9') {
Mark Dickinson40228912010-06-11 20:27:05 +00001243 /* overflow-safe version of
1244 if (num*10 + (c - '0') > PY_SSIZE_T_MAX) { ... } */
1245 if (num >= PY_SSIZE_T_MAX / 10 && (
1246 num > PY_SSIZE_T_MAX / 10 ||
1247 (c - '0') > PY_SSIZE_T_MAX % 10))
1248 goto overflow;
1249 num = num*10 + (c - '0');
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001250 }
1251 if (c == '\0')
1252 break;
1253 }
1254 else
1255 num = 1;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001256
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001257 e = getentry(c, f);
1258 if (e == NULL)
1259 return -1;
Tim Petersc2b550e2006-05-31 14:28:07 +00001260
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001261 switch (c) {
1262 case 's': /* fall through */
1263 case 'p': len++; break;
1264 case 'x': break;
1265 default: len += num; break;
1266 }
Bob Ippolito232f3c92006-05-23 19:12:41 +00001267
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001268 itemsize = e->size;
1269 size = align(size, c, e);
Mark Dickinson40228912010-06-11 20:27:05 +00001270 if (size == -1)
1271 goto overflow;
1272
1273 /* if (size + num * itemsize > PY_SSIZE_T_MAX) { ... } */
1274 if (num > (PY_SSIZE_T_MAX - size) / itemsize)
1275 goto overflow;
1276 size += num * itemsize;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001277 }
Bob Ippolito232f3c92006-05-23 19:12:41 +00001278
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001279 /* check for overflow */
1280 if ((len + 1) > (PY_SSIZE_T_MAX / sizeof(formatcode))) {
1281 PyErr_NoMemory();
1282 return -1;
1283 }
Gregory P. Smith9d534572008-06-11 07:41:16 +00001284
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001285 self->s_size = size;
1286 self->s_len = len;
1287 codes = PyMem_MALLOC((len + 1) * sizeof(formatcode));
1288 if (codes == NULL) {
1289 PyErr_NoMemory();
1290 return -1;
1291 }
Mark Dickinson96527c32010-07-29 21:44:47 +00001292 /* Free any s_codes value left over from a previous initialization. */
1293 if (self->s_codes != NULL)
1294 PyMem_FREE(self->s_codes);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001295 self->s_codes = codes;
Tim Petersc2b550e2006-05-31 14:28:07 +00001296
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001297 s = fmt;
1298 size = 0;
1299 while ((c = *s++) != '\0') {
1300 if (isspace(Py_CHARMASK(c)))
1301 continue;
1302 if ('0' <= c && c <= '9') {
1303 num = c - '0';
1304 while ('0' <= (c = *s++) && c <= '9')
1305 num = num*10 + (c - '0');
1306 if (c == '\0')
1307 break;
1308 }
1309 else
1310 num = 1;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001311
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001312 e = getentry(c, f);
Tim Petersc2b550e2006-05-31 14:28:07 +00001313
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001314 size = align(size, c, e);
1315 if (c == 's' || c == 'p') {
1316 codes->offset = size;
1317 codes->size = num;
1318 codes->fmtdef = e;
1319 codes++;
1320 size += num;
1321 } else if (c == 'x') {
1322 size += num;
1323 } else {
1324 while (--num >= 0) {
1325 codes->offset = size;
1326 codes->size = e->size;
1327 codes->fmtdef = e;
1328 codes++;
1329 size += e->size;
1330 }
1331 }
1332 }
1333 codes->fmtdef = NULL;
1334 codes->offset = size;
1335 codes->size = 0;
Tim Petersc2b550e2006-05-31 14:28:07 +00001336
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001337 return 0;
Mark Dickinson40228912010-06-11 20:27:05 +00001338
1339 overflow:
1340 PyErr_SetString(StructError,
1341 "total struct size too long");
1342 return -1;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001343}
1344
1345static PyObject *
1346s_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1347{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001348 PyObject *self;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001349
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001350 assert(type != NULL && type->tp_alloc != NULL);
Bob Ippolito232f3c92006-05-23 19:12:41 +00001351
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001352 self = type->tp_alloc(type, 0);
1353 if (self != NULL) {
1354 PyStructObject *s = (PyStructObject*)self;
1355 Py_INCREF(Py_None);
1356 s->s_format = Py_None;
1357 s->s_codes = NULL;
1358 s->s_size = -1;
1359 s->s_len = -1;
1360 }
1361 return self;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001362}
1363
1364static int
1365s_init(PyObject *self, PyObject *args, PyObject *kwds)
1366{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001367 PyStructObject *soself = (PyStructObject *)self;
1368 PyObject *o_format = NULL;
1369 int ret = 0;
1370 static char *kwlist[] = {"format", 0};
Bob Ippolito232f3c92006-05-23 19:12:41 +00001371
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001372 assert(PyStruct_Check(self));
Bob Ippolito232f3c92006-05-23 19:12:41 +00001373
Serhiy Storchaka5493d5e2013-12-08 17:44:50 +02001374 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:Struct", kwlist,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001375 &o_format))
1376 return -1;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001377
Serhiy Storchaka5493d5e2013-12-08 17:44:50 +02001378 if (PyString_Check(o_format)) {
1379 Py_INCREF(o_format);
Serhiy Storchakabc62af12016-04-06 09:51:18 +03001380 Py_XSETREF(soself->s_format, o_format);
Serhiy Storchaka5493d5e2013-12-08 17:44:50 +02001381 }
1382 else if (PyUnicode_Check(o_format)) {
1383 PyObject *str = PyUnicode_AsEncodedString(o_format, "ascii", NULL);
1384 if (str == NULL)
1385 return -1;
Serhiy Storchakabc62af12016-04-06 09:51:18 +03001386 Py_XSETREF(soself->s_format, str);
Serhiy Storchaka5493d5e2013-12-08 17:44:50 +02001387 }
1388 else {
1389 PyErr_Format(PyExc_TypeError,
1390 "Struct() argument 1 must be string, not %s",
1391 Py_TYPE(o_format)->tp_name);
1392 return -1;
1393 }
Tim Petersc2b550e2006-05-31 14:28:07 +00001394
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001395 ret = prepare_s(soself);
1396 return ret;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001397}
1398
1399static void
1400s_dealloc(PyStructObject *s)
1401{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001402 if (s->weakreflist != NULL)
1403 PyObject_ClearWeakRefs((PyObject *)s);
1404 if (s->s_codes != NULL) {
1405 PyMem_FREE(s->s_codes);
1406 }
1407 Py_XDECREF(s->s_format);
1408 Py_TYPE(s)->tp_free((PyObject *)s);
Bob Ippolito232f3c92006-05-23 19:12:41 +00001409}
1410
Bob Ippolitoeb621272006-05-24 15:32:06 +00001411static PyObject *
1412s_unpack_internal(PyStructObject *soself, char *startfrom) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001413 formatcode *code;
1414 Py_ssize_t i = 0;
1415 PyObject *result = PyTuple_New(soself->s_len);
1416 if (result == NULL)
1417 return NULL;
Bob Ippolitoeb621272006-05-24 15:32:06 +00001418
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001419 for (code = soself->s_codes; code->fmtdef != NULL; code++) {
1420 PyObject *v;
1421 const formatdef *e = code->fmtdef;
1422 const char *res = startfrom + code->offset;
1423 if (e->format == 's') {
1424 v = PyString_FromStringAndSize(res, code->size);
1425 } else if (e->format == 'p') {
1426 Py_ssize_t n = *(unsigned char*)res;
1427 if (n >= code->size)
1428 n = code->size - 1;
1429 v = PyString_FromStringAndSize(res + 1, n);
1430 } else {
1431 v = e->unpack(res, e);
1432 }
1433 if (v == NULL)
1434 goto fail;
1435 PyTuple_SET_ITEM(result, i++, v);
1436 }
Bob Ippolitoeb621272006-05-24 15:32:06 +00001437
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001438 return result;
Bob Ippolitoeb621272006-05-24 15:32:06 +00001439fail:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001440 Py_DECREF(result);
1441 return NULL;
Bob Ippolitocd51ca52006-05-27 15:53:49 +00001442}
Bob Ippolitoeb621272006-05-24 15:32:06 +00001443
1444
Bob Ippolito232f3c92006-05-23 19:12:41 +00001445PyDoc_STRVAR(s_unpack__doc__,
Bob Ippolito1fcdc232006-05-27 12:11:36 +00001446"S.unpack(str) -> (v1, v2, ...)\n\
Bob Ippolito232f3c92006-05-23 19:12:41 +00001447\n\
1448Return tuple containing values unpacked according to this Struct's format.\n\
1449Requires len(str) == self.size. See struct.__doc__ for more on format\n\
1450strings.");
1451
1452static PyObject *
1453s_unpack(PyObject *self, PyObject *inputstr)
1454{
Kristján Valur Jónsson9795ca42013-03-19 17:17:47 -07001455 Py_buffer buf;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001456 char *start;
1457 Py_ssize_t len;
1458 PyObject *args=NULL, *result;
1459 PyStructObject *soself = (PyStructObject *)self;
1460 assert(PyStruct_Check(self));
1461 assert(soself->s_codes != NULL);
1462 if (inputstr == NULL)
1463 goto fail;
1464 if (PyString_Check(inputstr) &&
1465 PyString_GET_SIZE(inputstr) == soself->s_size) {
1466 return s_unpack_internal(soself, PyString_AS_STRING(inputstr));
1467 }
1468 args = PyTuple_Pack(1, inputstr);
1469 if (args == NULL)
1470 return NULL;
Kristján Valur Jónsson9795ca42013-03-19 17:17:47 -07001471 if (!PyArg_ParseTuple(args, "s*:unpack", &buf))
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001472 goto fail;
Kristján Valur Jónsson9795ca42013-03-19 17:17:47 -07001473 start = buf.buf;
1474 len = buf.len;
1475 if (soself->s_size != len) {
1476 PyBuffer_Release(&buf);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001477 goto fail;
Kristján Valur Jónsson9795ca42013-03-19 17:17:47 -07001478 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001479 result = s_unpack_internal(soself, start);
1480 Py_DECREF(args);
Kristján Valur Jónsson9795ca42013-03-19 17:17:47 -07001481 PyBuffer_Release(&buf);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001482 return result;
Raymond Hettinger7a3d41f2007-04-05 18:00:03 +00001483
1484fail:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001485 Py_XDECREF(args);
1486 PyErr_Format(StructError,
1487 "unpack requires a string argument of length %zd",
1488 soself->s_size);
1489 return NULL;
Bob Ippolitoeb621272006-05-24 15:32:06 +00001490}
1491
1492PyDoc_STRVAR(s_unpack_from__doc__,
Bob Ippolito1fcdc232006-05-27 12:11:36 +00001493"S.unpack_from(buffer[, offset]) -> (v1, v2, ...)\n\
Bob Ippolitoeb621272006-05-24 15:32:06 +00001494\n\
1495Return tuple containing values unpacked according to this Struct's format.\n\
1496Unlike unpack, unpack_from can unpack values from any object supporting\n\
1497the buffer API, not just str. Requires len(buffer[offset:]) >= self.size.\n\
1498See struct.__doc__ for more on format strings.");
1499
1500static PyObject *
1501s_unpack_from(PyObject *self, PyObject *args, PyObject *kwds)
1502{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001503 static char *kwlist[] = {"buffer", "offset", 0};
Kristján Valur Jónsson9795ca42013-03-19 17:17:47 -07001504 static char *fmt = "z*|n:unpack_from";
1505 Py_buffer buf;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001506 Py_ssize_t buffer_len = 0, offset = 0;
1507 char *buffer = NULL;
1508 PyStructObject *soself = (PyStructObject *)self;
Kristján Valur Jónsson9795ca42013-03-19 17:17:47 -07001509 PyObject *result;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001510 assert(PyStruct_Check(self));
1511 assert(soself->s_codes != NULL);
Bob Ippolitoeb621272006-05-24 15:32:06 +00001512
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001513 if (!PyArg_ParseTupleAndKeywords(args, kwds, fmt, kwlist,
Kristján Valur Jónsson9795ca42013-03-19 17:17:47 -07001514 &buf, &offset))
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001515 return NULL;
Kristján Valur Jónsson9795ca42013-03-19 17:17:47 -07001516 buffer = buf.buf;
1517 buffer_len = buf.len;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001518 if (buffer == NULL) {
1519 PyErr_Format(StructError,
1520 "unpack_from requires a buffer argument");
Kristján Valur Jónsson9795ca42013-03-19 17:17:47 -07001521 PyBuffer_Release(&buf);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001522 return NULL;
1523 }
Tim Petersc2b550e2006-05-31 14:28:07 +00001524
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001525 if (offset < 0)
1526 offset += buffer_len;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001527
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001528 if (offset < 0 || (buffer_len - offset) < soself->s_size) {
1529 PyErr_Format(StructError,
1530 "unpack_from requires a buffer of at least %zd bytes",
1531 soself->s_size);
Kristján Valur Jónsson9795ca42013-03-19 17:17:47 -07001532 PyBuffer_Release(&buf);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001533 return NULL;
1534 }
Kristján Valur Jónsson9795ca42013-03-19 17:17:47 -07001535 result = s_unpack_internal(soself, buffer + offset);
1536 PyBuffer_Release(&buf);
1537 return result;
Bob Ippolitoeb621272006-05-24 15:32:06 +00001538}
Bob Ippolito232f3c92006-05-23 19:12:41 +00001539
Bob Ippolito232f3c92006-05-23 19:12:41 +00001540
Martin Blais2856e5f2006-05-26 12:03:27 +00001541/*
1542 * Guts of the pack function.
1543 *
1544 * Takes a struct object, a tuple of arguments, and offset in that tuple of
1545 * argument for where to start processing the arguments for packing, and a
1546 * character buffer for writing the packed string. The caller must insure
1547 * that the buffer may contain the required length for packing the arguments.
1548 * 0 is returned on success, 1 is returned if there is an error.
1549 *
1550 */
1551static int
1552s_pack_internal(PyStructObject *soself, PyObject *args, int offset, char* buf)
Bob Ippolito232f3c92006-05-23 19:12:41 +00001553{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001554 formatcode *code;
1555 /* XXX(nnorwitz): why does i need to be a local? can we use
1556 the offset parameter or do we need the wider width? */
1557 Py_ssize_t i;
Martin Blais2856e5f2006-05-26 12:03:27 +00001558
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001559 memset(buf, '\0', soself->s_size);
1560 i = offset;
1561 for (code = soself->s_codes; code->fmtdef != NULL; code++) {
1562 Py_ssize_t n;
1563 PyObject *v = PyTuple_GET_ITEM(args, i++);
1564 const formatdef *e = code->fmtdef;
1565 char *res = buf + code->offset;
1566 if (e->format == 's') {
1567 if (!PyString_Check(v)) {
1568 PyErr_SetString(StructError,
1569 "argument for 's' must "
1570 "be a string");
1571 return -1;
1572 }
1573 n = PyString_GET_SIZE(v);
1574 if (n > code->size)
1575 n = code->size;
1576 if (n > 0)
1577 memcpy(res, PyString_AS_STRING(v), n);
1578 } else if (e->format == 'p') {
1579 if (!PyString_Check(v)) {
1580 PyErr_SetString(StructError,
1581 "argument for 'p' must "
1582 "be a string");
1583 return -1;
1584 }
1585 n = PyString_GET_SIZE(v);
1586 if (n > (code->size - 1))
1587 n = code->size - 1;
1588 if (n > 0)
1589 memcpy(res + 1, PyString_AS_STRING(v), n);
1590 if (n > 255)
1591 n = 255;
1592 *res = Py_SAFE_DOWNCAST(n, Py_ssize_t, unsigned char);
1593 } else if (e->pack(res, v, e) < 0) {
1594 if (strchr(integer_codes, e->format) != NULL &&
1595 PyErr_ExceptionMatches(PyExc_OverflowError))
1596 PyErr_Format(StructError,
1597 "integer out of range for "
1598 "'%c' format code",
1599 e->format);
1600 return -1;
1601 }
1602 }
Tim Petersc2b550e2006-05-31 14:28:07 +00001603
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001604 /* Success */
1605 return 0;
Martin Blais2856e5f2006-05-26 12:03:27 +00001606}
Bob Ippolito232f3c92006-05-23 19:12:41 +00001607
Martin Blais2856e5f2006-05-26 12:03:27 +00001608
1609PyDoc_STRVAR(s_pack__doc__,
Bob Ippolito1fcdc232006-05-27 12:11:36 +00001610"S.pack(v1, v2, ...) -> string\n\
Martin Blais2856e5f2006-05-26 12:03:27 +00001611\n\
1612Return a string containing values v1, v2, ... packed according to this\n\
1613Struct's format. See struct.__doc__ for more on format strings.");
1614
1615static PyObject *
1616s_pack(PyObject *self, PyObject *args)
1617{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001618 PyStructObject *soself;
1619 PyObject *result;
Martin Blais2856e5f2006-05-26 12:03:27 +00001620
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001621 /* Validate arguments. */
1622 soself = (PyStructObject *)self;
1623 assert(PyStruct_Check(self));
1624 assert(soself->s_codes != NULL);
1625 if (PyTuple_GET_SIZE(args) != soself->s_len)
1626 {
1627 PyErr_Format(StructError,
Petri Lehtinen5c89c192012-10-29 21:16:57 +02001628 "pack expected %zd items for packing (got %zd)", soself->s_len, PyTuple_GET_SIZE(args));
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001629 return NULL;
1630 }
Tim Petersc2b550e2006-05-31 14:28:07 +00001631
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001632 /* Allocate a new string */
1633 result = PyString_FromStringAndSize((char *)NULL, soself->s_size);
1634 if (result == NULL)
1635 return NULL;
Tim Petersc2b550e2006-05-31 14:28:07 +00001636
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001637 /* Call the guts */
1638 if ( s_pack_internal(soself, args, 0, PyString_AS_STRING(result)) != 0 ) {
1639 Py_DECREF(result);
1640 return NULL;
1641 }
Martin Blais2856e5f2006-05-26 12:03:27 +00001642
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001643 return result;
Martin Blais2856e5f2006-05-26 12:03:27 +00001644}
1645
Martin Blaisaf2ae722006-06-04 13:49:49 +00001646PyDoc_STRVAR(s_pack_into__doc__,
1647"S.pack_into(buffer, offset, v1, v2, ...)\n\
Martin Blais2856e5f2006-05-26 12:03:27 +00001648\n\
Martin Blaisaf2ae722006-06-04 13:49:49 +00001649Pack the values v1, v2, ... according to this Struct's format, write \n\
Bob Ippolito1fcdc232006-05-27 12:11:36 +00001650the packed bytes into the writable buffer buf starting at offset. Note\n\
1651that the offset is not an optional argument. See struct.__doc__ for \n\
Martin Blais2856e5f2006-05-26 12:03:27 +00001652more on format strings.");
1653
1654static PyObject *
Martin Blaisaf2ae722006-06-04 13:49:49 +00001655s_pack_into(PyObject *self, PyObject *args)
Martin Blais2856e5f2006-05-26 12:03:27 +00001656{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001657 PyStructObject *soself;
Serhiy Storchakab8285d92015-02-21 19:51:17 +02001658 Py_buffer buf;
1659 Py_ssize_t offset;
Martin Blais2856e5f2006-05-26 12:03:27 +00001660
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001661 /* Validate arguments. +1 is for the first arg as buffer. */
1662 soself = (PyStructObject *)self;
1663 assert(PyStruct_Check(self));
1664 assert(soself->s_codes != NULL);
1665 if (PyTuple_GET_SIZE(args) != (soself->s_len + 2))
1666 {
Petri Lehtinen5c89c192012-10-29 21:16:57 +02001667 if (PyTuple_GET_SIZE(args) == 0) {
1668 PyErr_Format(StructError,
1669 "pack_into expected buffer argument");
1670 }
1671 else if (PyTuple_GET_SIZE(args) == 1) {
1672 PyErr_Format(StructError,
1673 "pack_into expected offset argument");
1674 }
1675 else {
1676 PyErr_Format(StructError,
1677 "pack_into expected %zd items for packing (got %zd)",
1678 soself->s_len, (PyTuple_GET_SIZE(args) - 2));
1679 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001680 return NULL;
1681 }
Martin Blais2856e5f2006-05-26 12:03:27 +00001682
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001683 /* Extract a writable memory buffer from the first argument */
Serhiy Storchakab8285d92015-02-21 19:51:17 +02001684 if (!PyArg_Parse(PyTuple_GET_ITEM(args, 0), "w*", &buf))
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001685 return NULL;
Martin Blais2856e5f2006-05-26 12:03:27 +00001686
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001687 /* Extract the offset from the first argument */
1688 offset = PyInt_AsSsize_t(PyTuple_GET_ITEM(args, 1));
Serhiy Storchakab8285d92015-02-21 19:51:17 +02001689 if (offset == -1 && PyErr_Occurred()) {
1690 PyBuffer_Release(&buf);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001691 return NULL;
Serhiy Storchakab8285d92015-02-21 19:51:17 +02001692 }
Martin Blais2856e5f2006-05-26 12:03:27 +00001693
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001694 /* Support negative offsets. */
1695 if (offset < 0)
Serhiy Storchakab8285d92015-02-21 19:51:17 +02001696 offset += buf.len;
Martin Blais2856e5f2006-05-26 12:03:27 +00001697
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001698 /* Check boundaries */
Serhiy Storchakab8285d92015-02-21 19:51:17 +02001699 if (offset < 0 || (buf.len - offset) < soself->s_size) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001700 PyErr_Format(StructError,
1701 "pack_into requires a buffer of at least %zd bytes",
1702 soself->s_size);
Serhiy Storchakab8285d92015-02-21 19:51:17 +02001703 PyBuffer_Release(&buf);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001704 return NULL;
1705 }
Tim Petersc2b550e2006-05-31 14:28:07 +00001706
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001707 /* Call the guts */
Serhiy Storchakab8285d92015-02-21 19:51:17 +02001708 if (s_pack_internal(soself, args, 2, (char *)buf.buf + offset) != 0) {
1709 PyBuffer_Release(&buf);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001710 return NULL;
1711 }
Serhiy Storchakab8285d92015-02-21 19:51:17 +02001712 PyBuffer_Release(&buf);
Martin Blais2856e5f2006-05-26 12:03:27 +00001713
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001714 Py_RETURN_NONE;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001715}
1716
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001717static PyObject *
1718s_get_format(PyStructObject *self, void *unused)
1719{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001720 Py_INCREF(self->s_format);
1721 return self->s_format;
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001722}
1723
1724static PyObject *
1725s_get_size(PyStructObject *self, void *unused)
1726{
1727 return PyInt_FromSsize_t(self->s_size);
1728}
Bob Ippolito232f3c92006-05-23 19:12:41 +00001729
Meador Inge87c5b942012-07-23 09:27:00 -05001730PyDoc_STRVAR(s_sizeof__doc__,
1731"S.__sizeof__() -> size of S in memory, in bytes");
1732
1733static PyObject *
Meador Inge68123462012-07-28 21:58:44 -05001734s_sizeof(PyStructObject *self, void *unused)
Meador Inge87c5b942012-07-23 09:27:00 -05001735{
1736 Py_ssize_t size;
Meador Inge87c5b942012-07-23 09:27:00 -05001737
Serhiy Storchakac06a6d02015-12-19 20:07:48 +02001738 size = _PyObject_SIZE(Py_TYPE(self)) + sizeof(formatcode) * (self->s_len + 1);
Meador Inge87c5b942012-07-23 09:27:00 -05001739 return PyLong_FromSsize_t(size);
1740}
1741
Bob Ippolito232f3c92006-05-23 19:12:41 +00001742/* List of functions */
1743
1744static struct PyMethodDef s_methods[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001745 {"pack", s_pack, METH_VARARGS, s_pack__doc__},
1746 {"pack_into", s_pack_into, METH_VARARGS, s_pack_into__doc__},
1747 {"unpack", s_unpack, METH_O, s_unpack__doc__},
1748 {"unpack_from", (PyCFunction)s_unpack_from, METH_VARARGS|METH_KEYWORDS,
1749 s_unpack_from__doc__},
Meador Inge87c5b942012-07-23 09:27:00 -05001750 {"__sizeof__", (PyCFunction)s_sizeof, METH_NOARGS, s_sizeof__doc__},
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001751 {NULL, NULL} /* sentinel */
Bob Ippolito232f3c92006-05-23 19:12:41 +00001752};
1753
1754PyDoc_STRVAR(s__doc__, "Compiled struct object");
1755
1756#define OFF(x) offsetof(PyStructObject, x)
1757
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001758static PyGetSetDef s_getsetlist[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001759 {"format", (getter)s_get_format, (setter)NULL, "struct format string", NULL},
1760 {"size", (getter)s_get_size, (setter)NULL, "struct size in bytes", NULL},
1761 {NULL} /* sentinel */
Bob Ippolito232f3c92006-05-23 19:12:41 +00001762};
1763
Bob Ippolito232f3c92006-05-23 19:12:41 +00001764static
1765PyTypeObject PyStructType = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001766 PyVarObject_HEAD_INIT(NULL, 0)
1767 "Struct",
1768 sizeof(PyStructObject),
1769 0,
1770 (destructor)s_dealloc, /* tp_dealloc */
1771 0, /* tp_print */
1772 0, /* tp_getattr */
1773 0, /* tp_setattr */
1774 0, /* tp_compare */
1775 0, /* tp_repr */
1776 0, /* tp_as_number */
1777 0, /* tp_as_sequence */
1778 0, /* tp_as_mapping */
1779 0, /* tp_hash */
1780 0, /* tp_call */
1781 0, /* tp_str */
1782 PyObject_GenericGetAttr, /* tp_getattro */
1783 PyObject_GenericSetAttr, /* tp_setattro */
1784 0, /* tp_as_buffer */
1785 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_WEAKREFS,/* tp_flags */
1786 s__doc__, /* tp_doc */
1787 0, /* tp_traverse */
1788 0, /* tp_clear */
1789 0, /* tp_richcompare */
1790 offsetof(PyStructObject, weakreflist), /* tp_weaklistoffset */
1791 0, /* tp_iter */
1792 0, /* tp_iternext */
1793 s_methods, /* tp_methods */
1794 NULL, /* tp_members */
1795 s_getsetlist, /* tp_getset */
1796 0, /* tp_base */
1797 0, /* tp_dict */
1798 0, /* tp_descr_get */
1799 0, /* tp_descr_set */
1800 0, /* tp_dictoffset */
1801 s_init, /* tp_init */
1802 PyType_GenericAlloc,/* tp_alloc */
1803 s_new, /* tp_new */
1804 PyObject_Del, /* tp_free */
Bob Ippolito232f3c92006-05-23 19:12:41 +00001805};
1806
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00001807
1808/* ---- Standalone functions ---- */
1809
1810#define MAXCACHE 100
Christian Heimes76d19f62008-01-04 02:54:42 +00001811static PyObject *cache = NULL;
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00001812
1813static PyObject *
1814cache_struct(PyObject *fmt)
1815{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001816 PyObject * s_object;
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00001817
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001818 if (cache == NULL) {
1819 cache = PyDict_New();
1820 if (cache == NULL)
1821 return NULL;
1822 }
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00001823
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001824 s_object = PyDict_GetItem(cache, fmt);
1825 if (s_object != NULL) {
1826 Py_INCREF(s_object);
1827 return s_object;
1828 }
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00001829
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001830 s_object = PyObject_CallFunctionObjArgs((PyObject *)(&PyStructType), fmt, NULL);
1831 if (s_object != NULL) {
1832 if (PyDict_Size(cache) >= MAXCACHE)
1833 PyDict_Clear(cache);
1834 /* Attempt to cache the result */
1835 if (PyDict_SetItem(cache, fmt, s_object) == -1)
1836 PyErr_Clear();
1837 }
1838 return s_object;
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00001839}
1840
Christian Heimes76d19f62008-01-04 02:54:42 +00001841PyDoc_STRVAR(clearcache_doc,
1842"Clear the internal cache.");
1843
1844static PyObject *
1845clearcache(PyObject *self)
1846{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001847 Py_CLEAR(cache);
1848 Py_RETURN_NONE;
Christian Heimes76d19f62008-01-04 02:54:42 +00001849}
1850
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00001851PyDoc_STRVAR(calcsize_doc,
1852"Return size of C struct described by format string fmt.");
1853
1854static PyObject *
1855calcsize(PyObject *self, PyObject *fmt)
1856{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001857 Py_ssize_t n;
1858 PyObject *s_object = cache_struct(fmt);
1859 if (s_object == NULL)
1860 return NULL;
1861 n = ((PyStructObject *)s_object)->s_size;
1862 Py_DECREF(s_object);
1863 return PyInt_FromSsize_t(n);
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00001864}
1865
1866PyDoc_STRVAR(pack_doc,
1867"Return string containing values v1, v2, ... packed according to fmt.");
1868
1869static PyObject *
1870pack(PyObject *self, PyObject *args)
1871{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001872 PyObject *s_object, *fmt, *newargs, *result;
1873 Py_ssize_t n = PyTuple_GET_SIZE(args);
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00001874
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001875 if (n == 0) {
1876 PyErr_SetString(PyExc_TypeError, "missing format argument");
1877 return NULL;
1878 }
1879 fmt = PyTuple_GET_ITEM(args, 0);
1880 newargs = PyTuple_GetSlice(args, 1, n);
1881 if (newargs == NULL)
1882 return NULL;
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00001883
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001884 s_object = cache_struct(fmt);
1885 if (s_object == NULL) {
1886 Py_DECREF(newargs);
1887 return NULL;
1888 }
1889 result = s_pack(s_object, newargs);
1890 Py_DECREF(newargs);
1891 Py_DECREF(s_object);
1892 return result;
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00001893}
1894
1895PyDoc_STRVAR(pack_into_doc,
1896"Pack the values v1, v2, ... according to fmt.\n\
1897Write the packed bytes into the writable buffer buf starting at offset.");
1898
1899static PyObject *
1900pack_into(PyObject *self, PyObject *args)
1901{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001902 PyObject *s_object, *fmt, *newargs, *result;
1903 Py_ssize_t n = PyTuple_GET_SIZE(args);
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00001904
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001905 if (n == 0) {
1906 PyErr_SetString(PyExc_TypeError, "missing format argument");
1907 return NULL;
1908 }
1909 fmt = PyTuple_GET_ITEM(args, 0);
1910 newargs = PyTuple_GetSlice(args, 1, n);
1911 if (newargs == NULL)
1912 return NULL;
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00001913
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001914 s_object = cache_struct(fmt);
1915 if (s_object == NULL) {
1916 Py_DECREF(newargs);
1917 return NULL;
1918 }
1919 result = s_pack_into(s_object, newargs);
1920 Py_DECREF(newargs);
1921 Py_DECREF(s_object);
1922 return result;
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00001923}
1924
1925PyDoc_STRVAR(unpack_doc,
1926"Unpack the string containing packed C structure data, according to fmt.\n\
1927Requires len(string) == calcsize(fmt).");
1928
1929static PyObject *
1930unpack(PyObject *self, PyObject *args)
1931{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001932 PyObject *s_object, *fmt, *inputstr, *result;
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00001933
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001934 if (!PyArg_UnpackTuple(args, "unpack", 2, 2, &fmt, &inputstr))
1935 return NULL;
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00001936
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001937 s_object = cache_struct(fmt);
1938 if (s_object == NULL)
1939 return NULL;
1940 result = s_unpack(s_object, inputstr);
1941 Py_DECREF(s_object);
1942 return result;
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00001943}
1944
1945PyDoc_STRVAR(unpack_from_doc,
1946"Unpack the buffer, containing packed C structure data, according to\n\
1947fmt, starting at offset. Requires len(buffer[offset:]) >= calcsize(fmt).");
1948
1949static PyObject *
1950unpack_from(PyObject *self, PyObject *args, PyObject *kwds)
1951{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001952 PyObject *s_object, *fmt, *newargs, *result;
1953 Py_ssize_t n = PyTuple_GET_SIZE(args);
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00001954
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001955 if (n == 0) {
1956 PyErr_SetString(PyExc_TypeError, "missing format argument");
1957 return NULL;
1958 }
1959 fmt = PyTuple_GET_ITEM(args, 0);
1960 newargs = PyTuple_GetSlice(args, 1, n);
1961 if (newargs == NULL)
1962 return NULL;
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00001963
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001964 s_object = cache_struct(fmt);
1965 if (s_object == NULL) {
1966 Py_DECREF(newargs);
1967 return NULL;
1968 }
1969 result = s_unpack_from(s_object, newargs, kwds);
1970 Py_DECREF(newargs);
1971 Py_DECREF(s_object);
1972 return result;
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00001973}
1974
1975static struct PyMethodDef module_functions[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001976 {"_clearcache", (PyCFunction)clearcache, METH_NOARGS, clearcache_doc},
1977 {"calcsize", calcsize, METH_O, calcsize_doc},
1978 {"pack", pack, METH_VARARGS, pack_doc},
1979 {"pack_into", pack_into, METH_VARARGS, pack_into_doc},
1980 {"unpack", unpack, METH_VARARGS, unpack_doc},
1981 {"unpack_from", (PyCFunction)unpack_from,
1982 METH_VARARGS|METH_KEYWORDS, unpack_from_doc},
1983 {NULL, NULL} /* sentinel */
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00001984};
1985
1986
Bob Ippolito232f3c92006-05-23 19:12:41 +00001987/* Module initialization */
1988
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00001989PyDoc_STRVAR(module_doc,
Mark Dickinson3d830822009-10-08 15:54:10 +00001990"Functions to convert between Python values and C structs represented\n\
1991as Python strings. It uses format strings (explained below) as compact\n\
1992descriptions of the lay-out of the C structs and the intended conversion\n\
1993to/from Python values.\n\
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00001994\n\
1995The optional first format char indicates byte order, size and alignment:\n\
Mark Dickinson3d830822009-10-08 15:54:10 +00001996 @: native order, size & alignment (default)\n\
1997 =: native order, std. size & alignment\n\
1998 <: little-endian, std. size & alignment\n\
1999 >: big-endian, std. size & alignment\n\
2000 !: same as >\n\
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00002001\n\
2002The remaining chars indicate types of args and must match exactly;\n\
2003these can be preceded by a decimal repeat count:\n\
2004 x: pad byte (no data); c:char; b:signed byte; B:unsigned byte;\n\
Mark Dickinson3d830822009-10-08 15:54:10 +00002005 ?: _Bool (requires C99; if not available, char is used instead)\n\
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00002006 h:short; H:unsigned short; i:int; I:unsigned int;\n\
2007 l:long; L:unsigned long; f:float; d:double.\n\
2008Special cases (preceding decimal count indicates length):\n\
2009 s:string (array of char); p: pascal string (with count byte).\n\
2010Special case (only available in native format):\n\
2011 P:an integer type that is wide enough to hold a pointer.\n\
2012Special case (not in native mode unless 'long long' in platform C):\n\
2013 q:long long; Q:unsigned long long\n\
2014Whitespace between formats is ignored.\n\
2015\n\
2016The variable struct.error is an exception raised on errors.\n");
2017
Bob Ippolito232f3c92006-05-23 19:12:41 +00002018PyMODINIT_FUNC
2019init_struct(void)
2020{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002021 PyObject *ver, *m;
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00002022
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002023 ver = PyString_FromString("0.2");
2024 if (ver == NULL)
2025 return;
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00002026
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002027 m = Py_InitModule3("_struct", module_functions, module_doc);
2028 if (m == NULL)
2029 return;
Bob Ippolito232f3c92006-05-23 19:12:41 +00002030
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002031 Py_TYPE(&PyStructType) = &PyType_Type;
2032 if (PyType_Ready(&PyStructType) < 0)
2033 return;
Bob Ippolito3fc2bb92006-05-25 19:03:19 +00002034
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002035 /* This speed trick can't be used until overflow masking goes
2036 away, because native endian always raises exceptions
2037 instead of overflow masking. */
Tim Petersc2b550e2006-05-31 14:28:07 +00002038
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002039 /* Check endian and swap in faster functions */
2040 {
2041 int one = 1;
2042 formatdef *native = native_table;
2043 formatdef *other, *ptr;
2044 if ((int)*(unsigned char*)&one)
2045 other = lilendian_table;
2046 else
2047 other = bigendian_table;
2048 /* Scan through the native table, find a matching
2049 entry in the endian table and swap in the
2050 native implementations whenever possible
2051 (64-bit platforms may not have "standard" sizes) */
2052 while (native->format != '\0' && other->format != '\0') {
2053 ptr = other;
2054 while (ptr->format != '\0') {
2055 if (ptr->format == native->format) {
2056 /* Match faster when formats are
2057 listed in the same order */
2058 if (ptr == other)
2059 other++;
2060 /* Only use the trick if the
2061 size matches */
2062 if (ptr->size != native->size)
2063 break;
2064 /* Skip float and double, could be
2065 "unknown" float format */
2066 if (ptr->format == 'd' || ptr->format == 'f')
2067 break;
2068 ptr->pack = native->pack;
2069 ptr->unpack = native->unpack;
2070 break;
2071 }
2072 ptr++;
2073 }
2074 native++;
2075 }
2076 }
Tim Petersc2b550e2006-05-31 14:28:07 +00002077
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002078 /* Add some symbolic constants to the module */
2079 if (StructError == NULL) {
2080 StructError = PyErr_NewException("struct.error", NULL, NULL);
2081 if (StructError == NULL)
2082 return;
2083 }
Bob Ippolito04ab9942006-05-25 19:33:38 +00002084
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002085 Py_INCREF(StructError);
2086 PyModule_AddObject(m, "error", StructError);
Bob Ippolito04ab9942006-05-25 19:33:38 +00002087
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002088 Py_INCREF((PyObject*)&PyStructType);
2089 PyModule_AddObject(m, "Struct", (PyObject*)&PyStructType);
Tim Petersc2b550e2006-05-31 14:28:07 +00002090
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002091 PyModule_AddObject(m, "__version__", ver);
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00002092
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002093 PyModule_AddIntConstant(m, "_PY_STRUCT_RANGE_CHECKING", 1);
2094 PyModule_AddIntConstant(m, "_PY_STRUCT_FLOAT_COERCE", 1);
Bob Ippolito232f3c92006-05-23 19:12:41 +00002095}