blob: 208559cf1c2c1ea32d39dbe861be34e0055dc1a2 [file] [log] [blame]
Benjamin Peterson4ae19462008-07-31 15:03:40 +00001/* struct module -- pack values into and (out of) bytes objects */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002
3/* New version supporting byte order, alignment and size options,
4 character strings, and unsigned numbers */
5
6#define PY_SSIZE_T_CLEAN
7
8#include "Python.h"
Thomas Wouters477c8d52006-05-27 19:21:47 +00009#include "structmember.h"
10#include <ctype.h>
11
12static PyTypeObject PyStructType;
13
Thomas Wouters477c8d52006-05-27 19:21:47 +000014/* The translation function for each format character is table driven */
15typedef struct _formatdef {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000016 char format;
17 Py_ssize_t size;
18 Py_ssize_t alignment;
19 PyObject* (*unpack)(const char *,
20 const struct _formatdef *);
21 int (*pack)(char *, PyObject *,
22 const struct _formatdef *);
Thomas Wouters477c8d52006-05-27 19:21:47 +000023} formatdef;
24
25typedef struct _formatcode {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000026 const struct _formatdef *fmtdef;
27 Py_ssize_t offset;
28 Py_ssize_t size;
Thomas Wouters477c8d52006-05-27 19:21:47 +000029} formatcode;
30
31/* Struct object interface */
32
33typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000034 PyObject_HEAD
35 Py_ssize_t s_size;
36 Py_ssize_t s_len;
37 formatcode *s_codes;
38 PyObject *s_format;
39 PyObject *weakreflist; /* List of weak references */
Thomas Wouters477c8d52006-05-27 19:21:47 +000040} PyStructObject;
41
42
43#define PyStruct_Check(op) PyObject_TypeCheck(op, &PyStructType)
Christian Heimes90aa7642007-12-19 02:45:37 +000044#define PyStruct_CheckExact(op) (Py_TYPE(op) == &PyStructType)
Thomas Wouters477c8d52006-05-27 19:21:47 +000045
46
47/* Exception */
48
49static PyObject *StructError;
50
51
52/* Define various structs to figure out the alignments of types */
53
54
55typedef struct { char c; short x; } st_short;
56typedef struct { char c; int x; } st_int;
57typedef struct { char c; long x; } st_long;
58typedef struct { char c; float x; } st_float;
59typedef struct { char c; double x; } st_double;
60typedef struct { char c; void *x; } st_void_p;
Antoine Pitrou45d9c912011-10-06 15:27:40 +020061typedef struct { char c; size_t x; } st_size_t;
Thomas Wouters477c8d52006-05-27 19:21:47 +000062
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 *))
Antoine Pitrou45d9c912011-10-06 15:27:40 +020069#define SIZE_T_ALIGN (sizeof(st_size_t) - sizeof(size_t))
Thomas Wouters477c8d52006-05-27 19:21:47 +000070
71/* We can't support q and Q in native mode unless the compiler does;
72 in std mode, they're 8 bytes on all platforms. */
73#ifdef HAVE_LONG_LONG
74typedef struct { char c; PY_LONG_LONG x; } s_long_long;
75#define LONG_LONG_ALIGN (sizeof(s_long_long) - sizeof(PY_LONG_LONG))
76#endif
77
Thomas Woutersb2137042007-02-01 18:02:27 +000078#ifdef HAVE_C99_BOOL
79#define BOOL_TYPE _Bool
80typedef struct { char c; _Bool x; } s_bool;
81#define BOOL_ALIGN (sizeof(s_bool) - sizeof(BOOL_TYPE))
82#else
83#define BOOL_TYPE char
84#define BOOL_ALIGN 0
85#endif
86
Thomas Wouters477c8d52006-05-27 19:21:47 +000087#define STRINGIFY(x) #x
88
89#ifdef __powerc
90#pragma options align=reset
91#endif
92
Mark Dickinson055a3fb2010-04-03 15:26:31 +000093/* Helper for integer format codes: converts an arbitrary Python object to a
94 PyLongObject if possible, otherwise fails. Caller should decref. */
Thomas Wouters477c8d52006-05-27 19:21:47 +000095
96static PyObject *
97get_pylong(PyObject *v)
98{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000099 assert(v != NULL);
100 if (!PyLong_Check(v)) {
101 /* Not an integer; try to use __index__ to convert. */
102 if (PyIndex_Check(v)) {
103 v = PyNumber_Index(v);
104 if (v == NULL)
105 return NULL;
106 }
107 else {
108 PyErr_SetString(StructError,
109 "required argument is not an integer");
110 return NULL;
111 }
112 }
113 else
114 Py_INCREF(v);
Mark Dickinsonea835e72009-04-19 20:40:33 +0000115
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000116 assert(PyLong_Check(v));
117 return v;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000118}
119
Mark Dickinsonea835e72009-04-19 20:40:33 +0000120/* Helper routine to get a C long and raise the appropriate error if it isn't
121 one */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000122
123static int
124get_long(PyObject *v, long *p)
125{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126 long x;
Mark Dickinsonea835e72009-04-19 20:40:33 +0000127
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000128 v = get_pylong(v);
129 if (v == NULL)
130 return -1;
131 assert(PyLong_Check(v));
132 x = PyLong_AsLong(v);
133 Py_DECREF(v);
134 if (x == (long)-1 && PyErr_Occurred()) {
135 if (PyErr_ExceptionMatches(PyExc_OverflowError))
136 PyErr_SetString(StructError,
137 "argument out of range");
138 return -1;
139 }
140 *p = x;
141 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000142}
143
144
145/* Same, but handling unsigned long */
146
147static int
148get_ulong(PyObject *v, unsigned long *p)
149{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000150 unsigned long x;
Mark Dickinsonea835e72009-04-19 20:40:33 +0000151
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000152 v = get_pylong(v);
153 if (v == NULL)
154 return -1;
155 assert(PyLong_Check(v));
156 x = PyLong_AsUnsignedLong(v);
157 Py_DECREF(v);
158 if (x == (unsigned long)-1 && PyErr_Occurred()) {
159 if (PyErr_ExceptionMatches(PyExc_OverflowError))
160 PyErr_SetString(StructError,
161 "argument out of range");
162 return -1;
163 }
164 *p = x;
165 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000166}
167
168#ifdef HAVE_LONG_LONG
169
170/* Same, but handling native long long. */
171
172static int
173get_longlong(PyObject *v, PY_LONG_LONG *p)
174{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000175 PY_LONG_LONG x;
Mark Dickinson055a3fb2010-04-03 15:26:31 +0000176
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000177 v = get_pylong(v);
178 if (v == NULL)
179 return -1;
180 assert(PyLong_Check(v));
181 x = PyLong_AsLongLong(v);
182 Py_DECREF(v);
183 if (x == (PY_LONG_LONG)-1 && PyErr_Occurred()) {
184 if (PyErr_ExceptionMatches(PyExc_OverflowError))
185 PyErr_SetString(StructError,
186 "argument out of range");
187 return -1;
188 }
189 *p = x;
190 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000191}
192
193/* Same, but handling native unsigned long long. */
194
195static int
196get_ulonglong(PyObject *v, unsigned PY_LONG_LONG *p)
197{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000198 unsigned PY_LONG_LONG x;
Mark Dickinson055a3fb2010-04-03 15:26:31 +0000199
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000200 v = get_pylong(v);
201 if (v == NULL)
202 return -1;
203 assert(PyLong_Check(v));
204 x = PyLong_AsUnsignedLongLong(v);
205 Py_DECREF(v);
206 if (x == (unsigned PY_LONG_LONG)-1 && PyErr_Occurred()) {
207 if (PyErr_ExceptionMatches(PyExc_OverflowError))
208 PyErr_SetString(StructError,
209 "argument out of range");
210 return -1;
211 }
212 *p = x;
213 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000214}
215
216#endif
217
Antoine Pitrou45d9c912011-10-06 15:27:40 +0200218/* Same, but handling Py_ssize_t */
219
220static int
221get_ssize_t(PyObject *v, Py_ssize_t *p)
222{
223 Py_ssize_t x;
224
225 v = get_pylong(v);
226 if (v == NULL)
227 return -1;
228 assert(PyLong_Check(v));
229 x = PyLong_AsSsize_t(v);
230 Py_DECREF(v);
231 if (x == (Py_ssize_t)-1 && PyErr_Occurred()) {
232 if (PyErr_ExceptionMatches(PyExc_OverflowError))
233 PyErr_SetString(StructError,
234 "argument out of range");
235 return -1;
236 }
237 *p = x;
238 return 0;
239}
240
241/* Same, but handling size_t */
242
243static int
244get_size_t(PyObject *v, size_t *p)
245{
246 size_t x;
247
248 v = get_pylong(v);
249 if (v == NULL)
250 return -1;
251 assert(PyLong_Check(v));
252 x = PyLong_AsSize_t(v);
253 Py_DECREF(v);
254 if (x == (size_t)-1 && PyErr_Occurred()) {
255 if (PyErr_ExceptionMatches(PyExc_OverflowError))
256 PyErr_SetString(StructError,
257 "argument out of range");
258 return -1;
259 }
260 *p = x;
261 return 0;
262}
263
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000264
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000265#define RANGE_ERROR(x, f, flag, mask) return _range_error(f, flag)
266
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000267
Thomas Wouters477c8d52006-05-27 19:21:47 +0000268/* Floating point helpers */
269
270static PyObject *
271unpack_float(const char *p, /* start of 4-byte string */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000272 int le) /* true for little-endian, false for big-endian */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000273{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000274 double x;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000275
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000276 x = _PyFloat_Unpack4((unsigned char *)p, le);
277 if (x == -1.0 && PyErr_Occurred())
278 return NULL;
279 return PyFloat_FromDouble(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000280}
281
282static PyObject *
283unpack_double(const char *p, /* start of 8-byte string */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000284 int le) /* true for little-endian, false for big-endian */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000285{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000286 double x;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000287
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000288 x = _PyFloat_Unpack8((unsigned char *)p, le);
289 if (x == -1.0 && PyErr_Occurred())
290 return NULL;
291 return PyFloat_FromDouble(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000292}
293
294/* Helper to format the range error exceptions */
295static int
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000296_range_error(const formatdef *f, int is_unsigned)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000297{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000298 /* ulargest is the largest unsigned value with f->size bytes.
299 * Note that the simpler:
300 * ((size_t)1 << (f->size * 8)) - 1
301 * doesn't work when f->size == sizeof(size_t) because C doesn't
302 * define what happens when a left shift count is >= the number of
303 * bits in the integer being shifted; e.g., on some boxes it doesn't
304 * shift at all when they're equal.
305 */
306 const size_t ulargest = (size_t)-1 >> ((SIZEOF_SIZE_T - f->size)*8);
307 assert(f->size >= 1 && f->size <= SIZEOF_SIZE_T);
308 if (is_unsigned)
309 PyErr_Format(StructError,
310 "'%c' format requires 0 <= number <= %zu",
311 f->format,
312 ulargest);
313 else {
314 const Py_ssize_t largest = (Py_ssize_t)(ulargest >> 1);
315 PyErr_Format(StructError,
316 "'%c' format requires %zd <= number <= %zd",
317 f->format,
318 ~ largest,
319 largest);
320 }
Mark Dickinsonae681df2009-03-21 10:26:31 +0000321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000322 return -1;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000323}
324
325
326
327/* A large number of small routines follow, with names of the form
328
329 [bln][up]_TYPE
330
331 [bln] distiguishes among big-endian, little-endian and native.
332 [pu] distiguishes between pack (to struct) and unpack (from struct).
333 TYPE is one of char, byte, ubyte, etc.
334*/
335
336/* Native mode routines. ****************************************************/
337/* NOTE:
338 In all n[up]_<type> routines handling types larger than 1 byte, there is
339 *no* guarantee that the p pointer is properly aligned for each type,
340 therefore memcpy is called. An intermediate variable is used to
341 compensate for big-endian architectures.
342 Normally both the intermediate variable and the memcpy call will be
343 skipped by C optimisation in little-endian architectures (gcc >= 2.91
344 does this). */
345
346static PyObject *
347nu_char(const char *p, const formatdef *f)
348{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000349 return PyBytes_FromStringAndSize(p, 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000350}
351
352static PyObject *
353nu_byte(const char *p, const formatdef *f)
354{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000355 return PyLong_FromLong((long) *(signed char *)p);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000356}
357
358static PyObject *
359nu_ubyte(const char *p, const formatdef *f)
360{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 return PyLong_FromLong((long) *(unsigned char *)p);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000362}
363
364static PyObject *
365nu_short(const char *p, const formatdef *f)
366{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000367 short x;
368 memcpy((char *)&x, p, sizeof x);
369 return PyLong_FromLong((long)x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000370}
371
372static PyObject *
373nu_ushort(const char *p, const formatdef *f)
374{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000375 unsigned short x;
376 memcpy((char *)&x, p, sizeof x);
377 return PyLong_FromLong((long)x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000378}
379
380static PyObject *
381nu_int(const char *p, const formatdef *f)
382{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000383 int x;
384 memcpy((char *)&x, p, sizeof x);
385 return PyLong_FromLong((long)x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000386}
387
388static PyObject *
389nu_uint(const char *p, const formatdef *f)
390{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000391 unsigned int x;
392 memcpy((char *)&x, p, sizeof x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000393#if (SIZEOF_LONG > SIZEOF_INT)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000394 return PyLong_FromLong((long)x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000395#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000396 if (x <= ((unsigned int)LONG_MAX))
397 return PyLong_FromLong((long)x);
398 return PyLong_FromUnsignedLong((unsigned long)x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000399#endif
400}
401
402static PyObject *
403nu_long(const char *p, const formatdef *f)
404{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000405 long x;
406 memcpy((char *)&x, p, sizeof x);
407 return PyLong_FromLong(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000408}
409
410static PyObject *
411nu_ulong(const char *p, const formatdef *f)
412{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000413 unsigned long x;
414 memcpy((char *)&x, p, sizeof x);
415 if (x <= LONG_MAX)
416 return PyLong_FromLong((long)x);
417 return PyLong_FromUnsignedLong(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000418}
419
Antoine Pitrou45d9c912011-10-06 15:27:40 +0200420static PyObject *
421nu_ssize_t(const char *p, const formatdef *f)
422{
423 Py_ssize_t x;
424 memcpy((char *)&x, p, sizeof x);
425 return PyLong_FromSsize_t(x);
426}
427
428static PyObject *
429nu_size_t(const char *p, const formatdef *f)
430{
431 size_t x;
432 memcpy((char *)&x, p, sizeof x);
433 return PyLong_FromSize_t(x);
434}
435
436
Thomas Wouters477c8d52006-05-27 19:21:47 +0000437/* Native mode doesn't support q or Q unless the platform C supports
438 long long (or, on Windows, __int64). */
439
440#ifdef HAVE_LONG_LONG
441
442static PyObject *
443nu_longlong(const char *p, const formatdef *f)
444{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000445 PY_LONG_LONG x;
446 memcpy((char *)&x, p, sizeof x);
447 if (x >= LONG_MIN && x <= LONG_MAX)
448 return PyLong_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
449 return PyLong_FromLongLong(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000450}
451
452static PyObject *
453nu_ulonglong(const char *p, const formatdef *f)
454{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000455 unsigned PY_LONG_LONG x;
456 memcpy((char *)&x, p, sizeof x);
457 if (x <= LONG_MAX)
458 return PyLong_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
459 return PyLong_FromUnsignedLongLong(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000460}
461
462#endif
463
464static PyObject *
Thomas Woutersb2137042007-02-01 18:02:27 +0000465nu_bool(const char *p, const formatdef *f)
466{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467 BOOL_TYPE x;
468 memcpy((char *)&x, p, sizeof x);
469 return PyBool_FromLong(x != 0);
Thomas Woutersb2137042007-02-01 18:02:27 +0000470}
471
472
473static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +0000474nu_float(const char *p, const formatdef *f)
475{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000476 float x;
477 memcpy((char *)&x, p, sizeof x);
478 return PyFloat_FromDouble((double)x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000479}
480
481static PyObject *
482nu_double(const char *p, const formatdef *f)
483{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000484 double x;
485 memcpy((char *)&x, p, sizeof x);
486 return PyFloat_FromDouble(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000487}
488
489static PyObject *
490nu_void_p(const char *p, const formatdef *f)
491{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 void *x;
493 memcpy((char *)&x, p, sizeof x);
494 return PyLong_FromVoidPtr(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000495}
496
497static int
498np_byte(char *p, PyObject *v, const formatdef *f)
499{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 long x;
501 if (get_long(v, &x) < 0)
502 return -1;
503 if (x < -128 || x > 127){
504 PyErr_SetString(StructError,
505 "byte format requires -128 <= number <= 127");
506 return -1;
507 }
508 *p = (char)x;
509 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000510}
511
512static int
513np_ubyte(char *p, PyObject *v, const formatdef *f)
514{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000515 long x;
516 if (get_long(v, &x) < 0)
517 return -1;
518 if (x < 0 || x > 255){
519 PyErr_SetString(StructError,
520 "ubyte format requires 0 <= number <= 255");
521 return -1;
522 }
523 *p = (char)x;
524 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000525}
526
527static int
528np_char(char *p, PyObject *v, const formatdef *f)
529{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 if (!PyBytes_Check(v) || PyBytes_Size(v) != 1) {
531 PyErr_SetString(StructError,
Victor Stinnerda9ec992010-12-28 13:26:42 +0000532 "char format requires a bytes object of length 1");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000533 return -1;
534 }
535 *p = *PyBytes_AsString(v);
536 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000537}
538
539static int
540np_short(char *p, PyObject *v, const formatdef *f)
541{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 long x;
543 short y;
544 if (get_long(v, &x) < 0)
545 return -1;
546 if (x < SHRT_MIN || x > SHRT_MAX){
547 PyErr_SetString(StructError,
548 "short format requires " STRINGIFY(SHRT_MIN)
549 " <= number <= " STRINGIFY(SHRT_MAX));
550 return -1;
551 }
552 y = (short)x;
553 memcpy(p, (char *)&y, sizeof y);
554 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000555}
556
557static int
558np_ushort(char *p, PyObject *v, const formatdef *f)
559{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 long x;
561 unsigned short y;
562 if (get_long(v, &x) < 0)
563 return -1;
564 if (x < 0 || x > USHRT_MAX){
565 PyErr_SetString(StructError,
566 "ushort format requires 0 <= number <= " STRINGIFY(USHRT_MAX));
567 return -1;
568 }
569 y = (unsigned short)x;
570 memcpy(p, (char *)&y, sizeof y);
571 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000572}
573
574static int
575np_int(char *p, PyObject *v, const formatdef *f)
576{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000577 long x;
578 int y;
579 if (get_long(v, &x) < 0)
580 return -1;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000581#if (SIZEOF_LONG > SIZEOF_INT)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000582 if ((x < ((long)INT_MIN)) || (x > ((long)INT_MAX)))
583 RANGE_ERROR(x, f, 0, -1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000584#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000585 y = (int)x;
586 memcpy(p, (char *)&y, sizeof y);
587 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000588}
589
590static int
591np_uint(char *p, PyObject *v, const formatdef *f)
592{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593 unsigned long x;
594 unsigned int y;
595 if (get_ulong(v, &x) < 0)
596 return -1;
597 y = (unsigned int)x;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000598#if (SIZEOF_LONG > SIZEOF_INT)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000599 if (x > ((unsigned long)UINT_MAX))
600 RANGE_ERROR(y, f, 1, -1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000601#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000602 memcpy(p, (char *)&y, sizeof y);
603 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000604}
605
606static int
607np_long(char *p, PyObject *v, const formatdef *f)
608{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000609 long x;
610 if (get_long(v, &x) < 0)
611 return -1;
612 memcpy(p, (char *)&x, sizeof x);
613 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000614}
615
616static int
617np_ulong(char *p, PyObject *v, const formatdef *f)
618{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000619 unsigned long x;
620 if (get_ulong(v, &x) < 0)
621 return -1;
622 memcpy(p, (char *)&x, sizeof x);
623 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000624}
625
Antoine Pitrou45d9c912011-10-06 15:27:40 +0200626static int
627np_ssize_t(char *p, PyObject *v, const formatdef *f)
628{
629 Py_ssize_t x;
630 if (get_ssize_t(v, &x) < 0)
631 return -1;
632 memcpy(p, (char *)&x, sizeof x);
633 return 0;
634}
635
636static int
637np_size_t(char *p, PyObject *v, const formatdef *f)
638{
639 size_t x;
640 if (get_size_t(v, &x) < 0)
641 return -1;
642 memcpy(p, (char *)&x, sizeof x);
643 return 0;
644}
645
Thomas Wouters477c8d52006-05-27 19:21:47 +0000646#ifdef HAVE_LONG_LONG
647
648static int
649np_longlong(char *p, PyObject *v, const formatdef *f)
650{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000651 PY_LONG_LONG x;
652 if (get_longlong(v, &x) < 0)
653 return -1;
654 memcpy(p, (char *)&x, sizeof x);
655 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000656}
657
658static int
659np_ulonglong(char *p, PyObject *v, const formatdef *f)
660{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000661 unsigned PY_LONG_LONG x;
662 if (get_ulonglong(v, &x) < 0)
663 return -1;
664 memcpy(p, (char *)&x, sizeof x);
665 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000666}
667#endif
668
Thomas Woutersb2137042007-02-01 18:02:27 +0000669
670static int
671np_bool(char *p, PyObject *v, const formatdef *f)
672{
Benjamin Petersonde73c452010-07-07 18:54:59 +0000673 int y;
674 BOOL_TYPE x;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000675 y = PyObject_IsTrue(v);
Benjamin Petersonde73c452010-07-07 18:54:59 +0000676 if (y < 0)
677 return -1;
678 x = y;
679 memcpy(p, (char *)&x, sizeof x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680 return 0;
Thomas Woutersb2137042007-02-01 18:02:27 +0000681}
682
Thomas Wouters477c8d52006-05-27 19:21:47 +0000683static int
684np_float(char *p, PyObject *v, const formatdef *f)
685{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000686 float x = (float)PyFloat_AsDouble(v);
687 if (x == -1 && PyErr_Occurred()) {
688 PyErr_SetString(StructError,
689 "required argument is not a float");
690 return -1;
691 }
692 memcpy(p, (char *)&x, sizeof x);
693 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000694}
695
696static int
697np_double(char *p, PyObject *v, const formatdef *f)
698{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000699 double x = PyFloat_AsDouble(v);
700 if (x == -1 && PyErr_Occurred()) {
701 PyErr_SetString(StructError,
702 "required argument is not a float");
703 return -1;
704 }
705 memcpy(p, (char *)&x, sizeof(double));
706 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000707}
708
709static int
710np_void_p(char *p, PyObject *v, const formatdef *f)
711{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000712 void *x;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000713
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 v = get_pylong(v);
715 if (v == NULL)
716 return -1;
717 assert(PyLong_Check(v));
718 x = PyLong_AsVoidPtr(v);
719 Py_DECREF(v);
720 if (x == NULL && PyErr_Occurred())
721 return -1;
722 memcpy(p, (char *)&x, sizeof x);
723 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000724}
725
726static formatdef native_table[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000727 {'x', sizeof(char), 0, NULL},
728 {'b', sizeof(char), 0, nu_byte, np_byte},
729 {'B', sizeof(char), 0, nu_ubyte, np_ubyte},
730 {'c', sizeof(char), 0, nu_char, np_char},
731 {'s', sizeof(char), 0, NULL},
732 {'p', sizeof(char), 0, NULL},
733 {'h', sizeof(short), SHORT_ALIGN, nu_short, np_short},
734 {'H', sizeof(short), SHORT_ALIGN, nu_ushort, np_ushort},
735 {'i', sizeof(int), INT_ALIGN, nu_int, np_int},
736 {'I', sizeof(int), INT_ALIGN, nu_uint, np_uint},
737 {'l', sizeof(long), LONG_ALIGN, nu_long, np_long},
738 {'L', sizeof(long), LONG_ALIGN, nu_ulong, np_ulong},
Antoine Pitrou45d9c912011-10-06 15:27:40 +0200739 {'n', sizeof(size_t), SIZE_T_ALIGN, nu_ssize_t, np_ssize_t},
740 {'N', sizeof(size_t), SIZE_T_ALIGN, nu_size_t, np_size_t},
Thomas Wouters477c8d52006-05-27 19:21:47 +0000741#ifdef HAVE_LONG_LONG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000742 {'q', sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_longlong, np_longlong},
743 {'Q', sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_ulonglong,np_ulonglong},
Thomas Wouters477c8d52006-05-27 19:21:47 +0000744#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000745 {'?', sizeof(BOOL_TYPE), BOOL_ALIGN, nu_bool, np_bool},
746 {'f', sizeof(float), FLOAT_ALIGN, nu_float, np_float},
747 {'d', sizeof(double), DOUBLE_ALIGN, nu_double, np_double},
748 {'P', sizeof(void *), VOID_P_ALIGN, nu_void_p, np_void_p},
749 {0}
Thomas Wouters477c8d52006-05-27 19:21:47 +0000750};
751
752/* Big-endian routines. *****************************************************/
753
754static PyObject *
755bu_int(const char *p, const formatdef *f)
756{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 long x = 0;
758 Py_ssize_t i = f->size;
759 const unsigned char *bytes = (const unsigned char *)p;
760 do {
761 x = (x<<8) | *bytes++;
762 } while (--i > 0);
763 /* Extend the sign bit. */
764 if (SIZEOF_LONG > f->size)
765 x |= -(x & (1L << ((8 * f->size) - 1)));
766 return PyLong_FromLong(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000767}
768
769static PyObject *
770bu_uint(const char *p, const formatdef *f)
771{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000772 unsigned long x = 0;
773 Py_ssize_t i = f->size;
774 const unsigned char *bytes = (const unsigned char *)p;
775 do {
776 x = (x<<8) | *bytes++;
777 } while (--i > 0);
778 if (x <= LONG_MAX)
779 return PyLong_FromLong((long)x);
780 return PyLong_FromUnsignedLong(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000781}
782
783static PyObject *
784bu_longlong(const char *p, const formatdef *f)
785{
786#ifdef HAVE_LONG_LONG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000787 PY_LONG_LONG x = 0;
788 Py_ssize_t i = f->size;
789 const unsigned char *bytes = (const unsigned char *)p;
790 do {
791 x = (x<<8) | *bytes++;
792 } while (--i > 0);
793 /* Extend the sign bit. */
794 if (SIZEOF_LONG_LONG > f->size)
795 x |= -(x & ((PY_LONG_LONG)1 << ((8 * f->size) - 1)));
796 if (x >= LONG_MIN && x <= LONG_MAX)
797 return PyLong_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
798 return PyLong_FromLongLong(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000799#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000800 return _PyLong_FromByteArray((const unsigned char *)p,
801 8,
802 0, /* little-endian */
803 1 /* signed */);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000804#endif
805}
806
807static PyObject *
808bu_ulonglong(const char *p, const formatdef *f)
809{
810#ifdef HAVE_LONG_LONG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000811 unsigned PY_LONG_LONG x = 0;
812 Py_ssize_t i = f->size;
813 const unsigned char *bytes = (const unsigned char *)p;
814 do {
815 x = (x<<8) | *bytes++;
816 } while (--i > 0);
817 if (x <= LONG_MAX)
818 return PyLong_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
819 return PyLong_FromUnsignedLongLong(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000820#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000821 return _PyLong_FromByteArray((const unsigned char *)p,
822 8,
823 0, /* little-endian */
824 0 /* signed */);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000825#endif
826}
827
828static PyObject *
829bu_float(const char *p, const formatdef *f)
830{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000831 return unpack_float(p, 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000832}
833
834static PyObject *
835bu_double(const char *p, const formatdef *f)
836{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000837 return unpack_double(p, 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000838}
839
Thomas Woutersb2137042007-02-01 18:02:27 +0000840static PyObject *
841bu_bool(const char *p, const formatdef *f)
842{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000843 char x;
844 memcpy((char *)&x, p, sizeof x);
845 return PyBool_FromLong(x != 0);
Thomas Woutersb2137042007-02-01 18:02:27 +0000846}
847
Thomas Wouters477c8d52006-05-27 19:21:47 +0000848static int
849bp_int(char *p, PyObject *v, const formatdef *f)
850{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000851 long x;
852 Py_ssize_t i;
853 if (get_long(v, &x) < 0)
854 return -1;
855 i = f->size;
856 if (i != SIZEOF_LONG) {
857 if ((i == 2) && (x < -32768 || x > 32767))
858 RANGE_ERROR(x, f, 0, 0xffffL);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000859#if (SIZEOF_LONG != 4)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 else if ((i == 4) && (x < -2147483648L || x > 2147483647L))
861 RANGE_ERROR(x, f, 0, 0xffffffffL);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000862#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000863 }
864 do {
865 p[--i] = (char)x;
866 x >>= 8;
867 } while (i > 0);
868 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000869}
870
871static int
872bp_uint(char *p, PyObject *v, const formatdef *f)
873{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000874 unsigned long x;
875 Py_ssize_t i;
876 if (get_ulong(v, &x) < 0)
877 return -1;
878 i = f->size;
879 if (i != SIZEOF_LONG) {
880 unsigned long maxint = 1;
881 maxint <<= (unsigned long)(i * 8);
882 if (x >= maxint)
883 RANGE_ERROR(x, f, 1, maxint - 1);
884 }
885 do {
886 p[--i] = (char)x;
887 x >>= 8;
888 } while (i > 0);
889 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000890}
891
892static int
893bp_longlong(char *p, PyObject *v, const formatdef *f)
894{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000895 int res;
896 v = get_pylong(v);
897 if (v == NULL)
898 return -1;
899 res = _PyLong_AsByteArray((PyLongObject *)v,
900 (unsigned char *)p,
901 8,
902 0, /* little_endian */
903 1 /* signed */);
904 Py_DECREF(v);
905 return res;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000906}
907
908static int
909bp_ulonglong(char *p, PyObject *v, const formatdef *f)
910{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000911 int res;
912 v = get_pylong(v);
913 if (v == NULL)
914 return -1;
915 res = _PyLong_AsByteArray((PyLongObject *)v,
916 (unsigned char *)p,
917 8,
918 0, /* little_endian */
919 0 /* signed */);
920 Py_DECREF(v);
921 return res;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000922}
923
924static int
925bp_float(char *p, PyObject *v, const formatdef *f)
926{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000927 double x = PyFloat_AsDouble(v);
928 if (x == -1 && PyErr_Occurred()) {
929 PyErr_SetString(StructError,
930 "required argument is not a float");
931 return -1;
932 }
933 return _PyFloat_Pack4(x, (unsigned char *)p, 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000934}
935
936static int
937bp_double(char *p, PyObject *v, const formatdef *f)
938{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000939 double x = PyFloat_AsDouble(v);
940 if (x == -1 && PyErr_Occurred()) {
941 PyErr_SetString(StructError,
942 "required argument is not a float");
943 return -1;
944 }
945 return _PyFloat_Pack8(x, (unsigned char *)p, 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000946}
947
Thomas Woutersb2137042007-02-01 18:02:27 +0000948static int
949bp_bool(char *p, PyObject *v, const formatdef *f)
950{
Mark Dickinsoneff5d852010-07-18 07:29:02 +0000951 int y;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000952 y = PyObject_IsTrue(v);
Benjamin Petersonde73c452010-07-07 18:54:59 +0000953 if (y < 0)
954 return -1;
Mark Dickinsoneff5d852010-07-18 07:29:02 +0000955 *p = (char)y;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956 return 0;
Thomas Woutersb2137042007-02-01 18:02:27 +0000957}
958
Thomas Wouters477c8d52006-05-27 19:21:47 +0000959static formatdef bigendian_table[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000960 {'x', 1, 0, NULL},
961 {'b', 1, 0, nu_byte, np_byte},
962 {'B', 1, 0, nu_ubyte, np_ubyte},
963 {'c', 1, 0, nu_char, np_char},
964 {'s', 1, 0, NULL},
965 {'p', 1, 0, NULL},
966 {'h', 2, 0, bu_int, bp_int},
967 {'H', 2, 0, bu_uint, bp_uint},
968 {'i', 4, 0, bu_int, bp_int},
969 {'I', 4, 0, bu_uint, bp_uint},
970 {'l', 4, 0, bu_int, bp_int},
971 {'L', 4, 0, bu_uint, bp_uint},
972 {'q', 8, 0, bu_longlong, bp_longlong},
973 {'Q', 8, 0, bu_ulonglong, bp_ulonglong},
974 {'?', 1, 0, bu_bool, bp_bool},
975 {'f', 4, 0, bu_float, bp_float},
976 {'d', 8, 0, bu_double, bp_double},
977 {0}
Thomas Wouters477c8d52006-05-27 19:21:47 +0000978};
979
980/* Little-endian routines. *****************************************************/
981
982static PyObject *
983lu_int(const char *p, const formatdef *f)
984{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000985 long x = 0;
986 Py_ssize_t i = f->size;
987 const unsigned char *bytes = (const unsigned char *)p;
988 do {
989 x = (x<<8) | bytes[--i];
990 } while (i > 0);
991 /* Extend the sign bit. */
992 if (SIZEOF_LONG > f->size)
993 x |= -(x & (1L << ((8 * f->size) - 1)));
994 return PyLong_FromLong(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000995}
996
997static PyObject *
998lu_uint(const char *p, const formatdef *f)
999{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001000 unsigned long x = 0;
1001 Py_ssize_t i = f->size;
1002 const unsigned char *bytes = (const unsigned char *)p;
1003 do {
1004 x = (x<<8) | bytes[--i];
1005 } while (i > 0);
1006 if (x <= LONG_MAX)
1007 return PyLong_FromLong((long)x);
1008 return PyLong_FromUnsignedLong((long)x);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001009}
1010
1011static PyObject *
1012lu_longlong(const char *p, const formatdef *f)
1013{
1014#ifdef HAVE_LONG_LONG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001015 PY_LONG_LONG x = 0;
1016 Py_ssize_t i = f->size;
1017 const unsigned char *bytes = (const unsigned char *)p;
1018 do {
1019 x = (x<<8) | bytes[--i];
1020 } while (i > 0);
1021 /* Extend the sign bit. */
1022 if (SIZEOF_LONG_LONG > f->size)
1023 x |= -(x & ((PY_LONG_LONG)1 << ((8 * f->size) - 1)));
1024 if (x >= LONG_MIN && x <= LONG_MAX)
1025 return PyLong_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
1026 return PyLong_FromLongLong(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001027#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 return _PyLong_FromByteArray((const unsigned char *)p,
1029 8,
1030 1, /* little-endian */
1031 1 /* signed */);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001032#endif
1033}
1034
1035static PyObject *
1036lu_ulonglong(const char *p, const formatdef *f)
1037{
1038#ifdef HAVE_LONG_LONG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001039 unsigned PY_LONG_LONG x = 0;
1040 Py_ssize_t i = f->size;
1041 const unsigned char *bytes = (const unsigned char *)p;
1042 do {
1043 x = (x<<8) | bytes[--i];
1044 } while (i > 0);
1045 if (x <= LONG_MAX)
1046 return PyLong_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
1047 return PyLong_FromUnsignedLongLong(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001048#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001049 return _PyLong_FromByteArray((const unsigned char *)p,
1050 8,
1051 1, /* little-endian */
1052 0 /* signed */);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001053#endif
1054}
1055
1056static PyObject *
1057lu_float(const char *p, const formatdef *f)
1058{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059 return unpack_float(p, 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001060}
1061
1062static PyObject *
1063lu_double(const char *p, const formatdef *f)
1064{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001065 return unpack_double(p, 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001066}
1067
1068static int
1069lp_int(char *p, PyObject *v, const formatdef *f)
1070{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001071 long x;
1072 Py_ssize_t i;
1073 if (get_long(v, &x) < 0)
1074 return -1;
1075 i = f->size;
1076 if (i != SIZEOF_LONG) {
1077 if ((i == 2) && (x < -32768 || x > 32767))
1078 RANGE_ERROR(x, f, 0, 0xffffL);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001079#if (SIZEOF_LONG != 4)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001080 else if ((i == 4) && (x < -2147483648L || x > 2147483647L))
1081 RANGE_ERROR(x, f, 0, 0xffffffffL);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001082#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001083 }
1084 do {
1085 *p++ = (char)x;
1086 x >>= 8;
1087 } while (--i > 0);
1088 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001089}
1090
1091static int
1092lp_uint(char *p, PyObject *v, const formatdef *f)
1093{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001094 unsigned long x;
1095 Py_ssize_t i;
1096 if (get_ulong(v, &x) < 0)
1097 return -1;
1098 i = f->size;
1099 if (i != SIZEOF_LONG) {
1100 unsigned long maxint = 1;
1101 maxint <<= (unsigned long)(i * 8);
1102 if (x >= maxint)
1103 RANGE_ERROR(x, f, 1, maxint - 1);
1104 }
1105 do {
1106 *p++ = (char)x;
1107 x >>= 8;
1108 } while (--i > 0);
1109 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001110}
1111
1112static int
1113lp_longlong(char *p, PyObject *v, const formatdef *f)
1114{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001115 int res;
1116 v = get_pylong(v);
1117 if (v == NULL)
1118 return -1;
1119 res = _PyLong_AsByteArray((PyLongObject*)v,
1120 (unsigned char *)p,
1121 8,
1122 1, /* little_endian */
1123 1 /* signed */);
1124 Py_DECREF(v);
1125 return res;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001126}
1127
1128static int
1129lp_ulonglong(char *p, PyObject *v, const formatdef *f)
1130{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001131 int res;
1132 v = get_pylong(v);
1133 if (v == NULL)
1134 return -1;
1135 res = _PyLong_AsByteArray((PyLongObject*)v,
1136 (unsigned char *)p,
1137 8,
1138 1, /* little_endian */
1139 0 /* signed */);
1140 Py_DECREF(v);
1141 return res;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001142}
1143
1144static int
1145lp_float(char *p, PyObject *v, const formatdef *f)
1146{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001147 double x = PyFloat_AsDouble(v);
1148 if (x == -1 && PyErr_Occurred()) {
1149 PyErr_SetString(StructError,
1150 "required argument is not a float");
1151 return -1;
1152 }
1153 return _PyFloat_Pack4(x, (unsigned char *)p, 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001154}
1155
1156static int
1157lp_double(char *p, PyObject *v, const formatdef *f)
1158{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001159 double x = PyFloat_AsDouble(v);
1160 if (x == -1 && PyErr_Occurred()) {
1161 PyErr_SetString(StructError,
1162 "required argument is not a float");
1163 return -1;
1164 }
1165 return _PyFloat_Pack8(x, (unsigned char *)p, 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001166}
1167
1168static formatdef lilendian_table[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001169 {'x', 1, 0, NULL},
1170 {'b', 1, 0, nu_byte, np_byte},
1171 {'B', 1, 0, nu_ubyte, np_ubyte},
1172 {'c', 1, 0, nu_char, np_char},
1173 {'s', 1, 0, NULL},
1174 {'p', 1, 0, NULL},
1175 {'h', 2, 0, lu_int, lp_int},
1176 {'H', 2, 0, lu_uint, lp_uint},
1177 {'i', 4, 0, lu_int, lp_int},
1178 {'I', 4, 0, lu_uint, lp_uint},
1179 {'l', 4, 0, lu_int, lp_int},
1180 {'L', 4, 0, lu_uint, lp_uint},
1181 {'q', 8, 0, lu_longlong, lp_longlong},
1182 {'Q', 8, 0, lu_ulonglong, lp_ulonglong},
1183 {'?', 1, 0, bu_bool, bp_bool}, /* Std rep not endian dep,
1184 but potentially different from native rep -- reuse bx_bool funcs. */
1185 {'f', 4, 0, lu_float, lp_float},
1186 {'d', 8, 0, lu_double, lp_double},
1187 {0}
Thomas Wouters477c8d52006-05-27 19:21:47 +00001188};
1189
1190
1191static const formatdef *
1192whichtable(char **pfmt)
1193{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001194 const char *fmt = (*pfmt)++; /* May be backed out of later */
1195 switch (*fmt) {
1196 case '<':
1197 return lilendian_table;
1198 case '>':
1199 case '!': /* Network byte order is big-endian */
1200 return bigendian_table;
Ezio Melotti42da6632011-03-15 05:18:48 +02001201 case '=': { /* Host byte order -- different from native in alignment! */
Christian Heimes743e0cd2012-10-17 23:52:17 +02001202#if PY_LITTLE_ENDIAN
1203 return lilendian_table;
1204#else
1205 return bigendian_table;
1206#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001207 }
1208 default:
1209 --*pfmt; /* Back out of pointer increment */
1210 /* Fall through */
1211 case '@':
1212 return native_table;
1213 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001214}
1215
1216
1217/* Get the table entry for a format code */
1218
1219static const formatdef *
1220getentry(int c, const formatdef *f)
1221{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001222 for (; f->format != '\0'; f++) {
1223 if (f->format == c) {
1224 return f;
1225 }
1226 }
1227 PyErr_SetString(StructError, "bad char in struct format");
1228 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001229}
1230
1231
Mark Dickinsonb72e6862010-06-11 19:50:30 +00001232/* Align a size according to a format code. Return -1 on overflow. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001233
Mark Dickinsoneac0e682010-06-11 19:05:08 +00001234static Py_ssize_t
Thomas Wouters477c8d52006-05-27 19:21:47 +00001235align(Py_ssize_t size, char c, const formatdef *e)
1236{
Mark Dickinsonb72e6862010-06-11 19:50:30 +00001237 Py_ssize_t extra;
1238
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001239 if (e->format == c) {
Mark Dickinsonb72e6862010-06-11 19:50:30 +00001240 if (e->alignment && size > 0) {
1241 extra = (e->alignment - 1) - (size - 1) % (e->alignment);
1242 if (extra > PY_SSIZE_T_MAX - size)
1243 return -1;
1244 size += extra;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 }
1246 }
1247 return size;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001248}
1249
1250
1251/* calculate the size of a format string */
1252
1253static int
1254prepare_s(PyStructObject *self)
1255{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001256 const formatdef *f;
1257 const formatdef *e;
1258 formatcode *codes;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001259
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001260 const char *s;
1261 const char *fmt;
1262 char c;
Mark Dickinsonb72e6862010-06-11 19:50:30 +00001263 Py_ssize_t size, len, num, itemsize;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001264
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001265 fmt = PyBytes_AS_STRING(self->s_format);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001266
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001267 f = whichtable((char **)&fmt);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001268
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 s = fmt;
1270 size = 0;
1271 len = 0;
1272 while ((c = *s++) != '\0') {
Antoine Pitrou4de74572013-02-09 23:11:27 +01001273 if (Py_ISSPACE(Py_CHARMASK(c)))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001274 continue;
1275 if ('0' <= c && c <= '9') {
1276 num = c - '0';
1277 while ('0' <= (c = *s++) && c <= '9') {
Mark Dickinsonab4096f2010-06-11 16:56:34 +00001278 /* overflow-safe version of
1279 if (num*10 + (c - '0') > PY_SSIZE_T_MAX) { ... } */
1280 if (num >= PY_SSIZE_T_MAX / 10 && (
1281 num > PY_SSIZE_T_MAX / 10 ||
Mark Dickinsonb72e6862010-06-11 19:50:30 +00001282 (c - '0') > PY_SSIZE_T_MAX % 10))
1283 goto overflow;
Mark Dickinsonab4096f2010-06-11 16:56:34 +00001284 num = num*10 + (c - '0');
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001285 }
Alexander Belopolsky177e8532010-06-11 16:04:59 +00001286 if (c == '\0') {
1287 PyErr_SetString(StructError,
1288 "repeat count given without format specifier");
1289 return -1;
1290 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 }
1292 else
1293 num = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001294
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001295 e = getentry(c, f);
1296 if (e == NULL)
1297 return -1;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001298
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001299 switch (c) {
1300 case 's': /* fall through */
1301 case 'p': len++; break;
1302 case 'x': break;
1303 default: len += num; break;
1304 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001305
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001306 itemsize = e->size;
1307 size = align(size, c, e);
Mark Dickinsonb72e6862010-06-11 19:50:30 +00001308 if (size == -1)
1309 goto overflow;
1310
1311 /* if (size + num * itemsize > PY_SSIZE_T_MAX) { ... } */
1312 if (num > (PY_SSIZE_T_MAX - size) / itemsize)
1313 goto overflow;
1314 size += num * itemsize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001315 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001316
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001317 /* check for overflow */
1318 if ((len + 1) > (PY_SSIZE_T_MAX / sizeof(formatcode))) {
1319 PyErr_NoMemory();
1320 return -1;
1321 }
Amaury Forgeot d'Arc35c86582008-06-17 21:11:29 +00001322
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001323 self->s_size = size;
1324 self->s_len = len;
1325 codes = PyMem_MALLOC((len + 1) * sizeof(formatcode));
1326 if (codes == NULL) {
1327 PyErr_NoMemory();
1328 return -1;
1329 }
Mark Dickinsoncf28b952010-07-29 21:41:59 +00001330 /* Free any s_codes value left over from a previous initialization. */
1331 if (self->s_codes != NULL)
1332 PyMem_FREE(self->s_codes);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001333 self->s_codes = codes;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001334
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001335 s = fmt;
1336 size = 0;
1337 while ((c = *s++) != '\0') {
Antoine Pitrou4de74572013-02-09 23:11:27 +01001338 if (Py_ISSPACE(Py_CHARMASK(c)))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001339 continue;
1340 if ('0' <= c && c <= '9') {
1341 num = c - '0';
1342 while ('0' <= (c = *s++) && c <= '9')
1343 num = num*10 + (c - '0');
1344 if (c == '\0')
1345 break;
1346 }
1347 else
1348 num = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001349
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001350 e = getentry(c, f);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001351
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001352 size = align(size, c, e);
1353 if (c == 's' || c == 'p') {
1354 codes->offset = size;
1355 codes->size = num;
1356 codes->fmtdef = e;
1357 codes++;
1358 size += num;
1359 } else if (c == 'x') {
1360 size += num;
1361 } else {
1362 while (--num >= 0) {
1363 codes->offset = size;
1364 codes->size = e->size;
1365 codes->fmtdef = e;
1366 codes++;
1367 size += e->size;
1368 }
1369 }
1370 }
1371 codes->fmtdef = NULL;
1372 codes->offset = size;
1373 codes->size = 0;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001374
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001375 return 0;
Mark Dickinsonb72e6862010-06-11 19:50:30 +00001376
1377 overflow:
1378 PyErr_SetString(StructError,
1379 "total struct size too long");
1380 return -1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001381}
1382
1383static PyObject *
1384s_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1385{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001386 PyObject *self;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001387
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001388 assert(type != NULL && type->tp_alloc != NULL);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001389
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001390 self = type->tp_alloc(type, 0);
1391 if (self != NULL) {
1392 PyStructObject *s = (PyStructObject*)self;
1393 Py_INCREF(Py_None);
1394 s->s_format = Py_None;
1395 s->s_codes = NULL;
1396 s->s_size = -1;
1397 s->s_len = -1;
1398 }
1399 return self;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001400}
1401
1402static int
1403s_init(PyObject *self, PyObject *args, PyObject *kwds)
1404{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001405 PyStructObject *soself = (PyStructObject *)self;
1406 PyObject *o_format = NULL;
1407 int ret = 0;
1408 static char *kwlist[] = {"format", 0};
Thomas Wouters477c8d52006-05-27 19:21:47 +00001409
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001410 assert(PyStruct_Check(self));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001411
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001412 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:Struct", kwlist,
1413 &o_format))
1414 return -1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001415
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001416 if (PyUnicode_Check(o_format)) {
1417 o_format = PyUnicode_AsASCIIString(o_format);
1418 if (o_format == NULL)
1419 return -1;
1420 }
1421 /* XXX support buffer interface, too */
1422 else {
1423 Py_INCREF(o_format);
1424 }
Christian Heimesa34706f2008-01-04 03:06:10 +00001425
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001426 if (!PyBytes_Check(o_format)) {
1427 Py_DECREF(o_format);
1428 PyErr_Format(PyExc_TypeError,
Victor Stinnerda9ec992010-12-28 13:26:42 +00001429 "Struct() argument 1 must be a bytes object, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001430 Py_TYPE(o_format)->tp_name);
1431 return -1;
1432 }
Christian Heimesa34706f2008-01-04 03:06:10 +00001433
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001434 Py_CLEAR(soself->s_format);
1435 soself->s_format = o_format;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001436
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001437 ret = prepare_s(soself);
1438 return ret;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001439}
1440
1441static void
1442s_dealloc(PyStructObject *s)
1443{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001444 if (s->weakreflist != NULL)
1445 PyObject_ClearWeakRefs((PyObject *)s);
1446 if (s->s_codes != NULL) {
1447 PyMem_FREE(s->s_codes);
1448 }
1449 Py_XDECREF(s->s_format);
1450 Py_TYPE(s)->tp_free((PyObject *)s);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001451}
1452
1453static PyObject *
1454s_unpack_internal(PyStructObject *soself, char *startfrom) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001455 formatcode *code;
1456 Py_ssize_t i = 0;
1457 PyObject *result = PyTuple_New(soself->s_len);
1458 if (result == NULL)
1459 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001460
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001461 for (code = soself->s_codes; code->fmtdef != NULL; code++) {
1462 PyObject *v;
1463 const formatdef *e = code->fmtdef;
1464 const char *res = startfrom + code->offset;
1465 if (e->format == 's') {
1466 v = PyBytes_FromStringAndSize(res, code->size);
1467 } else if (e->format == 'p') {
1468 Py_ssize_t n = *(unsigned char*)res;
1469 if (n >= code->size)
1470 n = code->size - 1;
1471 v = PyBytes_FromStringAndSize(res + 1, n);
1472 } else {
1473 v = e->unpack(res, e);
1474 }
1475 if (v == NULL)
1476 goto fail;
1477 PyTuple_SET_ITEM(result, i++, v);
1478 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001479
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001480 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001481fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001482 Py_DECREF(result);
1483 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001484}
1485
1486
1487PyDoc_STRVAR(s_unpack__doc__,
Guido van Rossum913dd0b2007-04-13 03:33:53 +00001488"S.unpack(buffer) -> (v1, v2, ...)\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +00001489\n\
Mark Dickinsonaacfa952010-06-12 15:43:45 +00001490Return a tuple containing values unpacked according to the format\n\
1491string S.format. Requires len(buffer) == S.size. See help(struct)\n\
1492for more on format strings.");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001493
1494static PyObject *
Guido van Rossum98297ee2007-11-06 21:34:58 +00001495s_unpack(PyObject *self, PyObject *input)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001496{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001497 Py_buffer vbuf;
1498 PyObject *result;
1499 PyStructObject *soself = (PyStructObject *)self;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001500
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001501 assert(PyStruct_Check(self));
1502 assert(soself->s_codes != NULL);
1503 if (PyObject_GetBuffer(input, &vbuf, PyBUF_SIMPLE) < 0)
1504 return NULL;
1505 if (vbuf.len != soself->s_size) {
1506 PyErr_Format(StructError,
Victor Stinnerda9ec992010-12-28 13:26:42 +00001507 "unpack requires a bytes object of length %zd",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001508 soself->s_size);
1509 PyBuffer_Release(&vbuf);
1510 return NULL;
1511 }
1512 result = s_unpack_internal(soself, vbuf.buf);
1513 PyBuffer_Release(&vbuf);
1514 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001515}
1516
1517PyDoc_STRVAR(s_unpack_from__doc__,
Mark Dickinsonc6f13962010-06-13 09:17:13 +00001518"S.unpack_from(buffer, offset=0) -> (v1, v2, ...)\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +00001519\n\
Mark Dickinsonaacfa952010-06-12 15:43:45 +00001520Return a tuple containing values unpacked according to the format\n\
1521string S.format. Requires len(buffer[offset:]) >= S.size. See\n\
1522help(struct) for more on format strings.");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001523
1524static PyObject *
1525s_unpack_from(PyObject *self, PyObject *args, PyObject *kwds)
1526{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001527 static char *kwlist[] = {"buffer", "offset", 0};
Guido van Rossum98297ee2007-11-06 21:34:58 +00001528
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001529 PyObject *input;
1530 Py_ssize_t offset = 0;
1531 Py_buffer vbuf;
1532 PyObject *result;
1533 PyStructObject *soself = (PyStructObject *)self;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001534
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001535 assert(PyStruct_Check(self));
1536 assert(soself->s_codes != NULL);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001537
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001538 if (!PyArg_ParseTupleAndKeywords(args, kwds,
1539 "O|n:unpack_from", kwlist,
1540 &input, &offset))
1541 return NULL;
1542 if (PyObject_GetBuffer(input, &vbuf, PyBUF_SIMPLE) < 0)
1543 return NULL;
1544 if (offset < 0)
1545 offset += vbuf.len;
1546 if (offset < 0 || vbuf.len - offset < soself->s_size) {
1547 PyErr_Format(StructError,
1548 "unpack_from requires a buffer of at least %zd bytes",
1549 soself->s_size);
1550 PyBuffer_Release(&vbuf);
1551 return NULL;
1552 }
1553 result = s_unpack_internal(soself, (char*)vbuf.buf + offset);
1554 PyBuffer_Release(&vbuf);
1555 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001556}
1557
1558
1559/*
1560 * Guts of the pack function.
1561 *
1562 * Takes a struct object, a tuple of arguments, and offset in that tuple of
1563 * argument for where to start processing the arguments for packing, and a
1564 * character buffer for writing the packed string. The caller must insure
1565 * that the buffer may contain the required length for packing the arguments.
1566 * 0 is returned on success, 1 is returned if there is an error.
1567 *
1568 */
1569static int
1570s_pack_internal(PyStructObject *soself, PyObject *args, int offset, char* buf)
1571{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001572 formatcode *code;
1573 /* XXX(nnorwitz): why does i need to be a local? can we use
1574 the offset parameter or do we need the wider width? */
1575 Py_ssize_t i;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001576
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001577 memset(buf, '\0', soself->s_size);
1578 i = offset;
1579 for (code = soself->s_codes; code->fmtdef != NULL; code++) {
1580 Py_ssize_t n;
1581 PyObject *v = PyTuple_GET_ITEM(args, i++);
1582 const formatdef *e = code->fmtdef;
1583 char *res = buf + code->offset;
1584 if (e->format == 's') {
1585 int isstring;
1586 void *p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001587 isstring = PyBytes_Check(v);
1588 if (!isstring && !PyByteArray_Check(v)) {
1589 PyErr_SetString(StructError,
Victor Stinnerda9ec992010-12-28 13:26:42 +00001590 "argument for 's' must be a bytes object");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001591 return -1;
1592 }
1593 if (isstring) {
1594 n = PyBytes_GET_SIZE(v);
1595 p = PyBytes_AS_STRING(v);
1596 }
1597 else {
1598 n = PyByteArray_GET_SIZE(v);
1599 p = PyByteArray_AS_STRING(v);
1600 }
1601 if (n > code->size)
1602 n = code->size;
1603 if (n > 0)
1604 memcpy(res, p, n);
1605 } else if (e->format == 'p') {
1606 int isstring;
1607 void *p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001608 isstring = PyBytes_Check(v);
1609 if (!isstring && !PyByteArray_Check(v)) {
1610 PyErr_SetString(StructError,
Victor Stinnerda9ec992010-12-28 13:26:42 +00001611 "argument for 'p' must be a bytes object");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001612 return -1;
1613 }
1614 if (isstring) {
1615 n = PyBytes_GET_SIZE(v);
1616 p = PyBytes_AS_STRING(v);
1617 }
1618 else {
1619 n = PyByteArray_GET_SIZE(v);
1620 p = PyByteArray_AS_STRING(v);
1621 }
1622 if (n > (code->size - 1))
1623 n = code->size - 1;
1624 if (n > 0)
1625 memcpy(res + 1, p, n);
1626 if (n > 255)
1627 n = 255;
1628 *res = Py_SAFE_DOWNCAST(n, Py_ssize_t, unsigned char);
1629 } else {
1630 if (e->pack(res, v, e) < 0) {
1631 if (PyLong_Check(v) && PyErr_ExceptionMatches(PyExc_OverflowError))
1632 PyErr_SetString(StructError,
1633 "long too large to convert to int");
1634 return -1;
1635 }
1636 }
1637 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001638
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001639 /* Success */
1640 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001641}
1642
1643
1644PyDoc_STRVAR(s_pack__doc__,
Benjamin Peterson4ae19462008-07-31 15:03:40 +00001645"S.pack(v1, v2, ...) -> bytes\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +00001646\n\
Mark Dickinsonaacfa952010-06-12 15:43:45 +00001647Return a bytes object containing values v1, v2, ... packed according\n\
1648to the format string S.format. See help(struct) for more on format\n\
1649strings.");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001650
1651static PyObject *
1652s_pack(PyObject *self, PyObject *args)
1653{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001654 PyStructObject *soself;
1655 PyObject *result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001656
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001657 /* Validate arguments. */
1658 soself = (PyStructObject *)self;
1659 assert(PyStruct_Check(self));
1660 assert(soself->s_codes != NULL);
1661 if (PyTuple_GET_SIZE(args) != soself->s_len)
1662 {
1663 PyErr_Format(StructError,
Petri Lehtinen92c28ca2012-10-29 21:16:57 +02001664 "pack expected %zd items for packing (got %zd)", soself->s_len, PyTuple_GET_SIZE(args));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001665 return NULL;
1666 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001667
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001668 /* Allocate a new string */
1669 result = PyBytes_FromStringAndSize((char *)NULL, soself->s_size);
1670 if (result == NULL)
1671 return NULL;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001672
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001673 /* Call the guts */
1674 if ( s_pack_internal(soself, args, 0, PyBytes_AS_STRING(result)) != 0 ) {
1675 Py_DECREF(result);
1676 return NULL;
1677 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001678
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001679 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001680}
1681
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001682PyDoc_STRVAR(s_pack_into__doc__,
1683"S.pack_into(buffer, offset, v1, v2, ...)\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +00001684\n\
Mark Dickinsonaacfa952010-06-12 15:43:45 +00001685Pack the values v1, v2, ... according to the format string S.format\n\
1686and write the packed bytes into the writable buffer buf starting at\n\
Mark Dickinsonfdb99f12010-06-12 16:30:53 +00001687offset. Note that the offset is a required argument. See\n\
Mark Dickinsonaacfa952010-06-12 15:43:45 +00001688help(struct) for more on format strings.");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001689
1690static PyObject *
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001691s_pack_into(PyObject *self, PyObject *args)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001692{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001693 PyStructObject *soself;
1694 char *buffer;
1695 Py_ssize_t buffer_len, offset;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001696
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001697 /* Validate arguments. +1 is for the first arg as buffer. */
1698 soself = (PyStructObject *)self;
1699 assert(PyStruct_Check(self));
1700 assert(soself->s_codes != NULL);
1701 if (PyTuple_GET_SIZE(args) != (soself->s_len + 2))
1702 {
Petri Lehtinen92c28ca2012-10-29 21:16:57 +02001703 if (PyTuple_GET_SIZE(args) == 0) {
1704 PyErr_Format(StructError,
1705 "pack_into expected buffer argument");
1706 }
1707 else if (PyTuple_GET_SIZE(args) == 1) {
1708 PyErr_Format(StructError,
1709 "pack_into expected offset argument");
1710 }
1711 else {
1712 PyErr_Format(StructError,
1713 "pack_into expected %zd items for packing (got %zd)",
1714 soself->s_len, (PyTuple_GET_SIZE(args) - 2));
1715 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001716 return NULL;
1717 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001718
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001719 /* Extract a writable memory buffer from the first argument */
1720 if ( PyObject_AsWriteBuffer(PyTuple_GET_ITEM(args, 0),
1721 (void**)&buffer, &buffer_len) == -1 ) {
1722 return NULL;
1723 }
1724 assert( buffer_len >= 0 );
Thomas Wouters477c8d52006-05-27 19:21:47 +00001725
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001726 /* Extract the offset from the first argument */
1727 offset = PyNumber_AsSsize_t(PyTuple_GET_ITEM(args, 1), PyExc_IndexError);
1728 if (offset == -1 && PyErr_Occurred())
1729 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001730
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001731 /* Support negative offsets. */
1732 if (offset < 0)
1733 offset += buffer_len;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001734
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001735 /* Check boundaries */
1736 if (offset < 0 || (buffer_len - offset) < soself->s_size) {
1737 PyErr_Format(StructError,
1738 "pack_into requires a buffer of at least %zd bytes",
1739 soself->s_size);
1740 return NULL;
1741 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001742
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001743 /* Call the guts */
1744 if ( s_pack_internal(soself, args, 2, buffer + offset) != 0 ) {
1745 return NULL;
1746 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001747
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001748 Py_RETURN_NONE;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001749}
1750
1751static PyObject *
1752s_get_format(PyStructObject *self, void *unused)
1753{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001754 Py_INCREF(self->s_format);
1755 return self->s_format;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001756}
1757
1758static PyObject *
1759s_get_size(PyStructObject *self, void *unused)
1760{
Christian Heimes217cfd12007-12-02 14:31:20 +00001761 return PyLong_FromSsize_t(self->s_size);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001762}
1763
Meador Ingeb14d8c92012-07-23 10:01:29 -05001764PyDoc_STRVAR(s_sizeof__doc__,
1765"S.__sizeof__() -> size of S in memory, in bytes");
1766
1767static PyObject *
Meador Inge90bc2dbc2012-07-28 22:16:39 -05001768s_sizeof(PyStructObject *self, void *unused)
Meador Ingeb14d8c92012-07-23 10:01:29 -05001769{
1770 Py_ssize_t size;
Meador Ingeb14d8c92012-07-23 10:01:29 -05001771
Meador Inge90bc2dbc2012-07-28 22:16:39 -05001772 size = sizeof(PyStructObject) + sizeof(formatcode) * (self->s_len + 1);
Meador Ingeb14d8c92012-07-23 10:01:29 -05001773 return PyLong_FromSsize_t(size);
1774}
1775
Thomas Wouters477c8d52006-05-27 19:21:47 +00001776/* List of functions */
1777
1778static struct PyMethodDef s_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001779 {"pack", s_pack, METH_VARARGS, s_pack__doc__},
1780 {"pack_into", s_pack_into, METH_VARARGS, s_pack_into__doc__},
1781 {"unpack", s_unpack, METH_O, s_unpack__doc__},
1782 {"unpack_from", (PyCFunction)s_unpack_from, METH_VARARGS|METH_KEYWORDS,
1783 s_unpack_from__doc__},
Meador Ingeb14d8c92012-07-23 10:01:29 -05001784 {"__sizeof__", (PyCFunction)s_sizeof, METH_NOARGS, s_sizeof__doc__},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001785 {NULL, NULL} /* sentinel */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001786};
1787
Victor Stinnerda9ec992010-12-28 13:26:42 +00001788PyDoc_STRVAR(s__doc__,
Alexander Belopolsky0bd003a2010-06-12 19:36:28 +00001789"Struct(fmt) --> compiled struct object\n"
1790"\n"
1791"Return a new Struct object which writes and reads binary data according to\n"
1792"the format string fmt. See help(struct) for more on format strings.");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001793
1794#define OFF(x) offsetof(PyStructObject, x)
1795
1796static PyGetSetDef s_getsetlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001797 {"format", (getter)s_get_format, (setter)NULL, "struct format string", NULL},
1798 {"size", (getter)s_get_size, (setter)NULL, "struct size in bytes", NULL},
1799 {NULL} /* sentinel */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001800};
1801
1802static
1803PyTypeObject PyStructType = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001804 PyVarObject_HEAD_INIT(NULL, 0)
1805 "Struct",
1806 sizeof(PyStructObject),
1807 0,
1808 (destructor)s_dealloc, /* tp_dealloc */
1809 0, /* tp_print */
1810 0, /* tp_getattr */
1811 0, /* tp_setattr */
1812 0, /* tp_reserved */
1813 0, /* tp_repr */
1814 0, /* tp_as_number */
1815 0, /* tp_as_sequence */
1816 0, /* tp_as_mapping */
1817 0, /* tp_hash */
1818 0, /* tp_call */
1819 0, /* tp_str */
1820 PyObject_GenericGetAttr, /* tp_getattro */
1821 PyObject_GenericSetAttr, /* tp_setattro */
1822 0, /* tp_as_buffer */
1823 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
1824 s__doc__, /* tp_doc */
1825 0, /* tp_traverse */
1826 0, /* tp_clear */
1827 0, /* tp_richcompare */
1828 offsetof(PyStructObject, weakreflist), /* tp_weaklistoffset */
1829 0, /* tp_iter */
1830 0, /* tp_iternext */
1831 s_methods, /* tp_methods */
1832 NULL, /* tp_members */
1833 s_getsetlist, /* tp_getset */
1834 0, /* tp_base */
1835 0, /* tp_dict */
1836 0, /* tp_descr_get */
1837 0, /* tp_descr_set */
1838 0, /* tp_dictoffset */
1839 s_init, /* tp_init */
1840 PyType_GenericAlloc,/* tp_alloc */
1841 s_new, /* tp_new */
1842 PyObject_Del, /* tp_free */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001843};
1844
Christian Heimesa34706f2008-01-04 03:06:10 +00001845
1846/* ---- Standalone functions ---- */
1847
1848#define MAXCACHE 100
1849static PyObject *cache = NULL;
1850
1851static PyObject *
1852cache_struct(PyObject *fmt)
1853{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001854 PyObject * s_object;
Christian Heimesa34706f2008-01-04 03:06:10 +00001855
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001856 if (cache == NULL) {
1857 cache = PyDict_New();
1858 if (cache == NULL)
1859 return NULL;
1860 }
Christian Heimesa34706f2008-01-04 03:06:10 +00001861
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001862 s_object = PyDict_GetItem(cache, fmt);
1863 if (s_object != NULL) {
1864 Py_INCREF(s_object);
1865 return s_object;
1866 }
Christian Heimesa34706f2008-01-04 03:06:10 +00001867
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001868 s_object = PyObject_CallFunctionObjArgs((PyObject *)(&PyStructType), fmt, NULL);
1869 if (s_object != NULL) {
1870 if (PyDict_Size(cache) >= MAXCACHE)
1871 PyDict_Clear(cache);
1872 /* Attempt to cache the result */
1873 if (PyDict_SetItem(cache, fmt, s_object) == -1)
1874 PyErr_Clear();
1875 }
1876 return s_object;
Christian Heimesa34706f2008-01-04 03:06:10 +00001877}
1878
1879PyDoc_STRVAR(clearcache_doc,
1880"Clear the internal cache.");
1881
1882static PyObject *
1883clearcache(PyObject *self)
1884{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001885 Py_CLEAR(cache);
1886 Py_RETURN_NONE;
Christian Heimesa34706f2008-01-04 03:06:10 +00001887}
1888
1889PyDoc_STRVAR(calcsize_doc,
Mark Dickinsonaacfa952010-06-12 15:43:45 +00001890"calcsize(fmt) -> integer\n\
1891\n\
1892Return size in bytes of the struct described by the format string fmt.");
Christian Heimesa34706f2008-01-04 03:06:10 +00001893
1894static PyObject *
1895calcsize(PyObject *self, PyObject *fmt)
1896{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001897 Py_ssize_t n;
1898 PyObject *s_object = cache_struct(fmt);
1899 if (s_object == NULL)
1900 return NULL;
1901 n = ((PyStructObject *)s_object)->s_size;
1902 Py_DECREF(s_object);
1903 return PyLong_FromSsize_t(n);
Christian Heimesa34706f2008-01-04 03:06:10 +00001904}
1905
1906PyDoc_STRVAR(pack_doc,
Mark Dickinsonaacfa952010-06-12 15:43:45 +00001907"pack(fmt, v1, v2, ...) -> bytes\n\
1908\n\
Mark Dickinsonfdb99f12010-06-12 16:30:53 +00001909Return a bytes object containing the values v1, v2, ... packed according\n\
1910to the format string fmt. See help(struct) for more on format strings.");
Christian Heimesa34706f2008-01-04 03:06:10 +00001911
1912static PyObject *
1913pack(PyObject *self, PyObject *args)
1914{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001915 PyObject *s_object, *fmt, *newargs, *result;
1916 Py_ssize_t n = PyTuple_GET_SIZE(args);
Christian Heimesa34706f2008-01-04 03:06:10 +00001917
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001918 if (n == 0) {
1919 PyErr_SetString(PyExc_TypeError, "missing format argument");
1920 return NULL;
1921 }
1922 fmt = PyTuple_GET_ITEM(args, 0);
1923 newargs = PyTuple_GetSlice(args, 1, n);
1924 if (newargs == NULL)
1925 return NULL;
Christian Heimesa34706f2008-01-04 03:06:10 +00001926
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001927 s_object = cache_struct(fmt);
1928 if (s_object == NULL) {
1929 Py_DECREF(newargs);
1930 return NULL;
1931 }
1932 result = s_pack(s_object, newargs);
1933 Py_DECREF(newargs);
1934 Py_DECREF(s_object);
1935 return result;
Christian Heimesa34706f2008-01-04 03:06:10 +00001936}
1937
1938PyDoc_STRVAR(pack_into_doc,
Mark Dickinsonaacfa952010-06-12 15:43:45 +00001939"pack_into(fmt, buffer, offset, v1, v2, ...)\n\
1940\n\
1941Pack the values v1, v2, ... according to the format string fmt and write\n\
1942the packed bytes into the writable buffer buf starting at offset. Note\n\
Mark Dickinsonfdb99f12010-06-12 16:30:53 +00001943that the offset is a required argument. See help(struct) for more\n\
Mark Dickinsonaacfa952010-06-12 15:43:45 +00001944on format strings.");
Christian Heimesa34706f2008-01-04 03:06:10 +00001945
1946static PyObject *
1947pack_into(PyObject *self, PyObject *args)
1948{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001949 PyObject *s_object, *fmt, *newargs, *result;
1950 Py_ssize_t n = PyTuple_GET_SIZE(args);
Christian Heimesa34706f2008-01-04 03:06:10 +00001951
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001952 if (n == 0) {
1953 PyErr_SetString(PyExc_TypeError, "missing format argument");
1954 return NULL;
1955 }
1956 fmt = PyTuple_GET_ITEM(args, 0);
1957 newargs = PyTuple_GetSlice(args, 1, n);
1958 if (newargs == NULL)
1959 return NULL;
Christian Heimesa34706f2008-01-04 03:06:10 +00001960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001961 s_object = cache_struct(fmt);
1962 if (s_object == NULL) {
1963 Py_DECREF(newargs);
1964 return NULL;
1965 }
1966 result = s_pack_into(s_object, newargs);
1967 Py_DECREF(newargs);
1968 Py_DECREF(s_object);
1969 return result;
Christian Heimesa34706f2008-01-04 03:06:10 +00001970}
1971
1972PyDoc_STRVAR(unpack_doc,
Mark Dickinsonaacfa952010-06-12 15:43:45 +00001973"unpack(fmt, buffer) -> (v1, v2, ...)\n\
1974\n\
1975Return a tuple containing values unpacked according to the format string\n\
1976fmt. Requires len(buffer) == calcsize(fmt). See help(struct) for more\n\
1977on format strings.");
Christian Heimesa34706f2008-01-04 03:06:10 +00001978
1979static PyObject *
1980unpack(PyObject *self, PyObject *args)
1981{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001982 PyObject *s_object, *fmt, *inputstr, *result;
Christian Heimesa34706f2008-01-04 03:06:10 +00001983
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001984 if (!PyArg_UnpackTuple(args, "unpack", 2, 2, &fmt, &inputstr))
1985 return NULL;
Christian Heimesa34706f2008-01-04 03:06:10 +00001986
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001987 s_object = cache_struct(fmt);
1988 if (s_object == NULL)
1989 return NULL;
1990 result = s_unpack(s_object, inputstr);
1991 Py_DECREF(s_object);
1992 return result;
Christian Heimesa34706f2008-01-04 03:06:10 +00001993}
1994
1995PyDoc_STRVAR(unpack_from_doc,
Mark Dickinsonc6f13962010-06-13 09:17:13 +00001996"unpack_from(fmt, buffer, offset=0) -> (v1, v2, ...)\n\
Mark Dickinsonaacfa952010-06-12 15:43:45 +00001997\n\
1998Return a tuple containing values unpacked according to the format string\n\
1999fmt. Requires len(buffer[offset:]) >= calcsize(fmt). See help(struct)\n\
2000for more on format strings.");
Christian Heimesa34706f2008-01-04 03:06:10 +00002001
2002static PyObject *
2003unpack_from(PyObject *self, PyObject *args, PyObject *kwds)
2004{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002005 PyObject *s_object, *fmt, *newargs, *result;
2006 Py_ssize_t n = PyTuple_GET_SIZE(args);
Christian Heimesa34706f2008-01-04 03:06:10 +00002007
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002008 if (n == 0) {
2009 PyErr_SetString(PyExc_TypeError, "missing format argument");
2010 return NULL;
2011 }
2012 fmt = PyTuple_GET_ITEM(args, 0);
2013 newargs = PyTuple_GetSlice(args, 1, n);
2014 if (newargs == NULL)
2015 return NULL;
Christian Heimesa34706f2008-01-04 03:06:10 +00002016
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002017 s_object = cache_struct(fmt);
2018 if (s_object == NULL) {
2019 Py_DECREF(newargs);
2020 return NULL;
2021 }
2022 result = s_unpack_from(s_object, newargs, kwds);
2023 Py_DECREF(newargs);
2024 Py_DECREF(s_object);
2025 return result;
Christian Heimesa34706f2008-01-04 03:06:10 +00002026}
2027
2028static struct PyMethodDef module_functions[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002029 {"_clearcache", (PyCFunction)clearcache, METH_NOARGS, clearcache_doc},
2030 {"calcsize", calcsize, METH_O, calcsize_doc},
2031 {"pack", pack, METH_VARARGS, pack_doc},
2032 {"pack_into", pack_into, METH_VARARGS, pack_into_doc},
2033 {"unpack", unpack, METH_VARARGS, unpack_doc},
2034 {"unpack_from", (PyCFunction)unpack_from,
2035 METH_VARARGS|METH_KEYWORDS, unpack_from_doc},
2036 {NULL, NULL} /* sentinel */
Christian Heimesa34706f2008-01-04 03:06:10 +00002037};
2038
2039
Thomas Wouters477c8d52006-05-27 19:21:47 +00002040/* Module initialization */
2041
Christian Heimesa34706f2008-01-04 03:06:10 +00002042PyDoc_STRVAR(module_doc,
2043"Functions to convert between Python values and C structs.\n\
Benjamin Peterson4ae19462008-07-31 15:03:40 +00002044Python bytes objects are used to hold the data representing the C struct\n\
Mark Dickinson40714af2009-10-08 15:59:20 +00002045and also as format strings (explained below) to describe the layout of data\n\
2046in the C struct.\n\
Christian Heimesa34706f2008-01-04 03:06:10 +00002047\n\
2048The optional first format char indicates byte order, size and alignment:\n\
Mark Dickinson40714af2009-10-08 15:59:20 +00002049 @: native order, size & alignment (default)\n\
2050 =: native order, std. size & alignment\n\
2051 <: little-endian, std. size & alignment\n\
2052 >: big-endian, std. size & alignment\n\
2053 !: same as >\n\
Christian Heimesa34706f2008-01-04 03:06:10 +00002054\n\
2055The remaining chars indicate types of args and must match exactly;\n\
2056these can be preceded by a decimal repeat count:\n\
2057 x: pad byte (no data); c:char; b:signed byte; B:unsigned byte;\n\
Mark Dickinson40714af2009-10-08 15:59:20 +00002058 ?: _Bool (requires C99; if not available, char is used instead)\n\
Christian Heimesa34706f2008-01-04 03:06:10 +00002059 h:short; H:unsigned short; i:int; I:unsigned int;\n\
2060 l:long; L:unsigned long; f:float; d:double.\n\
2061Special cases (preceding decimal count indicates length):\n\
2062 s:string (array of char); p: pascal string (with count byte).\n\
Antoine Pitrou45d9c912011-10-06 15:27:40 +02002063Special cases (only available in native format):\n\
2064 n:ssize_t; N:size_t;\n\
Christian Heimesa34706f2008-01-04 03:06:10 +00002065 P:an integer type that is wide enough to hold a pointer.\n\
2066Special case (not in native mode unless 'long long' in platform C):\n\
2067 q:long long; Q:unsigned long long\n\
2068Whitespace between formats is ignored.\n\
2069\n\
2070The variable struct.error is an exception raised on errors.\n");
2071
Martin v. Löwis1a214512008-06-11 05:26:20 +00002072
2073static struct PyModuleDef _structmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002074 PyModuleDef_HEAD_INIT,
2075 "_struct",
2076 module_doc,
2077 -1,
2078 module_functions,
2079 NULL,
2080 NULL,
2081 NULL,
2082 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00002083};
2084
Thomas Wouters477c8d52006-05-27 19:21:47 +00002085PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00002086PyInit__struct(void)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002087{
Mark Dickinson06817852010-06-12 09:25:13 +00002088 PyObject *m;
Christian Heimesa34706f2008-01-04 03:06:10 +00002089
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002090 m = PyModule_Create(&_structmodule);
2091 if (m == NULL)
2092 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002093
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002094 Py_TYPE(&PyStructType) = &PyType_Type;
2095 if (PyType_Ready(&PyStructType) < 0)
2096 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002097
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002098 /* Check endian and swap in faster functions */
2099 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002100 formatdef *native = native_table;
2101 formatdef *other, *ptr;
Christian Heimes743e0cd2012-10-17 23:52:17 +02002102#if PY_LITTLE_ENDIAN
2103 other = lilendian_table;
2104#else
2105 other = bigendian_table;
2106#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002107 /* Scan through the native table, find a matching
2108 entry in the endian table and swap in the
2109 native implementations whenever possible
2110 (64-bit platforms may not have "standard" sizes) */
2111 while (native->format != '\0' && other->format != '\0') {
2112 ptr = other;
2113 while (ptr->format != '\0') {
2114 if (ptr->format == native->format) {
2115 /* Match faster when formats are
2116 listed in the same order */
2117 if (ptr == other)
2118 other++;
2119 /* Only use the trick if the
2120 size matches */
2121 if (ptr->size != native->size)
2122 break;
2123 /* Skip float and double, could be
2124 "unknown" float format */
2125 if (ptr->format == 'd' || ptr->format == 'f')
2126 break;
2127 ptr->pack = native->pack;
2128 ptr->unpack = native->unpack;
2129 break;
2130 }
2131 ptr++;
2132 }
2133 native++;
2134 }
2135 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002136
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002137 /* Add some symbolic constants to the module */
2138 if (StructError == NULL) {
2139 StructError = PyErr_NewException("struct.error", NULL, NULL);
2140 if (StructError == NULL)
2141 return NULL;
2142 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002143
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002144 Py_INCREF(StructError);
2145 PyModule_AddObject(m, "error", StructError);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002146
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002147 Py_INCREF((PyObject*)&PyStructType);
2148 PyModule_AddObject(m, "Struct", (PyObject*)&PyStructType);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002149
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002150 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002151}