blob: 068c5d1e1d83853ce7b23781c53304c047158954 [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;
Serhiy Storchakafff61f22013-05-17 10:49:44 +030029 Py_ssize_t repeat;
Thomas Wouters477c8d52006-05-27 19:21:47 +000030} formatcode;
31
32/* Struct object interface */
33
34typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000035 PyObject_HEAD
36 Py_ssize_t s_size;
37 Py_ssize_t s_len;
38 formatcode *s_codes;
39 PyObject *s_format;
40 PyObject *weakreflist; /* List of weak references */
Thomas Wouters477c8d52006-05-27 19:21:47 +000041} PyStructObject;
42
43
44#define PyStruct_Check(op) PyObject_TypeCheck(op, &PyStructType)
Christian Heimes90aa7642007-12-19 02:45:37 +000045#define PyStruct_CheckExact(op) (Py_TYPE(op) == &PyStructType)
Thomas Wouters477c8d52006-05-27 19:21:47 +000046
47
48/* Exception */
49
50static PyObject *StructError;
51
52
53/* Define various structs to figure out the alignments of types */
54
55
56typedef struct { char c; short x; } st_short;
57typedef struct { char c; int x; } st_int;
58typedef struct { char c; long x; } st_long;
59typedef struct { char c; float x; } st_float;
60typedef struct { char c; double x; } st_double;
61typedef struct { char c; void *x; } st_void_p;
Antoine Pitrou45d9c912011-10-06 15:27:40 +020062typedef struct { char c; size_t x; } st_size_t;
Thomas Wouters477c8d52006-05-27 19:21:47 +000063
64#define SHORT_ALIGN (sizeof(st_short) - sizeof(short))
65#define INT_ALIGN (sizeof(st_int) - sizeof(int))
66#define LONG_ALIGN (sizeof(st_long) - sizeof(long))
67#define FLOAT_ALIGN (sizeof(st_float) - sizeof(float))
68#define DOUBLE_ALIGN (sizeof(st_double) - sizeof(double))
69#define VOID_P_ALIGN (sizeof(st_void_p) - sizeof(void *))
Antoine Pitrou45d9c912011-10-06 15:27:40 +020070#define SIZE_T_ALIGN (sizeof(st_size_t) - sizeof(size_t))
Thomas Wouters477c8d52006-05-27 19:21:47 +000071
72/* We can't support q and Q in native mode unless the compiler does;
73 in std mode, they're 8 bytes on all platforms. */
74#ifdef HAVE_LONG_LONG
75typedef struct { char c; PY_LONG_LONG x; } s_long_long;
76#define LONG_LONG_ALIGN (sizeof(s_long_long) - sizeof(PY_LONG_LONG))
77#endif
78
Thomas Woutersb2137042007-02-01 18:02:27 +000079#ifdef HAVE_C99_BOOL
80#define BOOL_TYPE _Bool
81typedef struct { char c; _Bool x; } s_bool;
82#define BOOL_ALIGN (sizeof(s_bool) - sizeof(BOOL_TYPE))
83#else
84#define BOOL_TYPE char
85#define BOOL_ALIGN 0
86#endif
87
Thomas Wouters477c8d52006-05-27 19:21:47 +000088#ifdef __powerc
89#pragma options align=reset
90#endif
91
Mark Dickinson055a3fb2010-04-03 15:26:31 +000092/* Helper for integer format codes: converts an arbitrary Python object to a
93 PyLongObject if possible, otherwise fails. Caller should decref. */
Thomas Wouters477c8d52006-05-27 19:21:47 +000094
95static PyObject *
96get_pylong(PyObject *v)
97{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000098 assert(v != NULL);
99 if (!PyLong_Check(v)) {
100 /* Not an integer; try to use __index__ to convert. */
101 if (PyIndex_Check(v)) {
102 v = PyNumber_Index(v);
103 if (v == NULL)
104 return NULL;
105 }
106 else {
107 PyErr_SetString(StructError,
108 "required argument is not an integer");
109 return NULL;
110 }
111 }
112 else
113 Py_INCREF(v);
Mark Dickinsonea835e72009-04-19 20:40:33 +0000114
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 assert(PyLong_Check(v));
116 return v;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000117}
118
Mark Dickinsonea835e72009-04-19 20:40:33 +0000119/* Helper routine to get a C long and raise the appropriate error if it isn't
120 one */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000121
122static int
123get_long(PyObject *v, long *p)
124{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000125 long x;
Mark Dickinsonea835e72009-04-19 20:40:33 +0000126
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 v = get_pylong(v);
128 if (v == NULL)
129 return -1;
130 assert(PyLong_Check(v));
131 x = PyLong_AsLong(v);
132 Py_DECREF(v);
133 if (x == (long)-1 && PyErr_Occurred()) {
134 if (PyErr_ExceptionMatches(PyExc_OverflowError))
135 PyErr_SetString(StructError,
136 "argument out of range");
137 return -1;
138 }
139 *p = x;
140 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000141}
142
143
144/* Same, but handling unsigned long */
145
146static int
147get_ulong(PyObject *v, unsigned long *p)
148{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000149 unsigned long x;
Mark Dickinsonea835e72009-04-19 20:40:33 +0000150
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000151 v = get_pylong(v);
152 if (v == NULL)
153 return -1;
154 assert(PyLong_Check(v));
155 x = PyLong_AsUnsignedLong(v);
156 Py_DECREF(v);
157 if (x == (unsigned long)-1 && PyErr_Occurred()) {
158 if (PyErr_ExceptionMatches(PyExc_OverflowError))
159 PyErr_SetString(StructError,
160 "argument out of range");
161 return -1;
162 }
163 *p = x;
164 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000165}
166
167#ifdef HAVE_LONG_LONG
168
169/* Same, but handling native long long. */
170
171static int
172get_longlong(PyObject *v, PY_LONG_LONG *p)
173{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000174 PY_LONG_LONG x;
Mark Dickinson055a3fb2010-04-03 15:26:31 +0000175
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000176 v = get_pylong(v);
177 if (v == NULL)
178 return -1;
179 assert(PyLong_Check(v));
180 x = PyLong_AsLongLong(v);
181 Py_DECREF(v);
182 if (x == (PY_LONG_LONG)-1 && PyErr_Occurred()) {
183 if (PyErr_ExceptionMatches(PyExc_OverflowError))
184 PyErr_SetString(StructError,
185 "argument out of range");
186 return -1;
187 }
188 *p = x;
189 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000190}
191
192/* Same, but handling native unsigned long long. */
193
194static int
195get_ulonglong(PyObject *v, unsigned PY_LONG_LONG *p)
196{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000197 unsigned PY_LONG_LONG x;
Mark Dickinson055a3fb2010-04-03 15:26:31 +0000198
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000199 v = get_pylong(v);
200 if (v == NULL)
201 return -1;
202 assert(PyLong_Check(v));
203 x = PyLong_AsUnsignedLongLong(v);
204 Py_DECREF(v);
205 if (x == (unsigned PY_LONG_LONG)-1 && PyErr_Occurred()) {
206 if (PyErr_ExceptionMatches(PyExc_OverflowError))
207 PyErr_SetString(StructError,
208 "argument out of range");
209 return -1;
210 }
211 *p = x;
212 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000213}
214
215#endif
216
Antoine Pitrou45d9c912011-10-06 15:27:40 +0200217/* Same, but handling Py_ssize_t */
218
219static int
220get_ssize_t(PyObject *v, Py_ssize_t *p)
221{
222 Py_ssize_t x;
223
224 v = get_pylong(v);
225 if (v == NULL)
226 return -1;
227 assert(PyLong_Check(v));
228 x = PyLong_AsSsize_t(v);
229 Py_DECREF(v);
230 if (x == (Py_ssize_t)-1 && PyErr_Occurred()) {
231 if (PyErr_ExceptionMatches(PyExc_OverflowError))
232 PyErr_SetString(StructError,
233 "argument out of range");
234 return -1;
235 }
236 *p = x;
237 return 0;
238}
239
240/* Same, but handling size_t */
241
242static int
243get_size_t(PyObject *v, size_t *p)
244{
245 size_t x;
246
247 v = get_pylong(v);
248 if (v == NULL)
249 return -1;
250 assert(PyLong_Check(v));
251 x = PyLong_AsSize_t(v);
252 Py_DECREF(v);
253 if (x == (size_t)-1 && PyErr_Occurred()) {
254 if (PyErr_ExceptionMatches(PyExc_OverflowError))
255 PyErr_SetString(StructError,
256 "argument out of range");
257 return -1;
258 }
259 *p = x;
260 return 0;
261}
262
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000263
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000264#define RANGE_ERROR(x, f, flag, mask) return _range_error(f, flag)
265
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000266
Thomas Wouters477c8d52006-05-27 19:21:47 +0000267/* Floating point helpers */
268
269static PyObject *
270unpack_float(const char *p, /* start of 4-byte string */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 int le) /* true for little-endian, false for big-endian */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000272{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 double x;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000274
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000275 x = _PyFloat_Unpack4((unsigned char *)p, le);
276 if (x == -1.0 && PyErr_Occurred())
277 return NULL;
278 return PyFloat_FromDouble(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000279}
280
281static PyObject *
282unpack_double(const char *p, /* start of 8-byte string */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000283 int le) /* true for little-endian, false for big-endian */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000284{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000285 double x;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000287 x = _PyFloat_Unpack8((unsigned char *)p, le);
288 if (x == -1.0 && PyErr_Occurred())
289 return NULL;
290 return PyFloat_FromDouble(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000291}
292
293/* Helper to format the range error exceptions */
294static int
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000295_range_error(const formatdef *f, int is_unsigned)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000296{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 /* ulargest is the largest unsigned value with f->size bytes.
298 * Note that the simpler:
299 * ((size_t)1 << (f->size * 8)) - 1
300 * doesn't work when f->size == sizeof(size_t) because C doesn't
301 * define what happens when a left shift count is >= the number of
302 * bits in the integer being shifted; e.g., on some boxes it doesn't
303 * shift at all when they're equal.
304 */
305 const size_t ulargest = (size_t)-1 >> ((SIZEOF_SIZE_T - f->size)*8);
306 assert(f->size >= 1 && f->size <= SIZEOF_SIZE_T);
307 if (is_unsigned)
308 PyErr_Format(StructError,
309 "'%c' format requires 0 <= number <= %zu",
310 f->format,
311 ulargest);
312 else {
313 const Py_ssize_t largest = (Py_ssize_t)(ulargest >> 1);
314 PyErr_Format(StructError,
315 "'%c' format requires %zd <= number <= %zd",
316 f->format,
317 ~ largest,
318 largest);
319 }
Mark Dickinsonae681df2009-03-21 10:26:31 +0000320
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000321 return -1;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000322}
323
324
325
326/* A large number of small routines follow, with names of the form
327
328 [bln][up]_TYPE
329
330 [bln] distiguishes among big-endian, little-endian and native.
331 [pu] distiguishes between pack (to struct) and unpack (from struct).
332 TYPE is one of char, byte, ubyte, etc.
333*/
334
335/* Native mode routines. ****************************************************/
336/* NOTE:
337 In all n[up]_<type> routines handling types larger than 1 byte, there is
338 *no* guarantee that the p pointer is properly aligned for each type,
339 therefore memcpy is called. An intermediate variable is used to
340 compensate for big-endian architectures.
341 Normally both the intermediate variable and the memcpy call will be
342 skipped by C optimisation in little-endian architectures (gcc >= 2.91
343 does this). */
344
345static PyObject *
346nu_char(const char *p, const formatdef *f)
347{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 return PyBytes_FromStringAndSize(p, 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000349}
350
351static PyObject *
352nu_byte(const char *p, const formatdef *f)
353{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000354 return PyLong_FromLong((long) *(signed char *)p);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000355}
356
357static PyObject *
358nu_ubyte(const char *p, const formatdef *f)
359{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000360 return PyLong_FromLong((long) *(unsigned char *)p);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000361}
362
363static PyObject *
364nu_short(const char *p, const formatdef *f)
365{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 short x;
367 memcpy((char *)&x, p, sizeof x);
368 return PyLong_FromLong((long)x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000369}
370
371static PyObject *
372nu_ushort(const char *p, const formatdef *f)
373{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000374 unsigned short x;
375 memcpy((char *)&x, p, sizeof x);
376 return PyLong_FromLong((long)x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000377}
378
379static PyObject *
380nu_int(const char *p, const formatdef *f)
381{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000382 int x;
383 memcpy((char *)&x, p, sizeof x);
384 return PyLong_FromLong((long)x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000385}
386
387static PyObject *
388nu_uint(const char *p, const formatdef *f)
389{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000390 unsigned int x;
391 memcpy((char *)&x, p, sizeof x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000392#if (SIZEOF_LONG > SIZEOF_INT)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000393 return PyLong_FromLong((long)x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000394#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000395 if (x <= ((unsigned int)LONG_MAX))
396 return PyLong_FromLong((long)x);
397 return PyLong_FromUnsignedLong((unsigned long)x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000398#endif
399}
400
401static PyObject *
402nu_long(const char *p, const formatdef *f)
403{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000404 long x;
405 memcpy((char *)&x, p, sizeof x);
406 return PyLong_FromLong(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000407}
408
409static PyObject *
410nu_ulong(const char *p, const formatdef *f)
411{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000412 unsigned long x;
413 memcpy((char *)&x, p, sizeof x);
414 if (x <= LONG_MAX)
415 return PyLong_FromLong((long)x);
416 return PyLong_FromUnsignedLong(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000417}
418
Antoine Pitrou45d9c912011-10-06 15:27:40 +0200419static PyObject *
420nu_ssize_t(const char *p, const formatdef *f)
421{
422 Py_ssize_t x;
423 memcpy((char *)&x, p, sizeof x);
424 return PyLong_FromSsize_t(x);
425}
426
427static PyObject *
428nu_size_t(const char *p, const formatdef *f)
429{
430 size_t x;
431 memcpy((char *)&x, p, sizeof x);
432 return PyLong_FromSize_t(x);
433}
434
435
Thomas Wouters477c8d52006-05-27 19:21:47 +0000436/* Native mode doesn't support q or Q unless the platform C supports
437 long long (or, on Windows, __int64). */
438
439#ifdef HAVE_LONG_LONG
440
441static PyObject *
442nu_longlong(const char *p, const formatdef *f)
443{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000444 PY_LONG_LONG x;
445 memcpy((char *)&x, p, sizeof x);
446 if (x >= LONG_MIN && x <= LONG_MAX)
447 return PyLong_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
448 return PyLong_FromLongLong(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000449}
450
451static PyObject *
452nu_ulonglong(const char *p, const formatdef *f)
453{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454 unsigned PY_LONG_LONG x;
455 memcpy((char *)&x, p, sizeof x);
456 if (x <= LONG_MAX)
457 return PyLong_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
458 return PyLong_FromUnsignedLongLong(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000459}
460
461#endif
462
463static PyObject *
Thomas Woutersb2137042007-02-01 18:02:27 +0000464nu_bool(const char *p, const formatdef *f)
465{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000466 BOOL_TYPE x;
467 memcpy((char *)&x, p, sizeof x);
468 return PyBool_FromLong(x != 0);
Thomas Woutersb2137042007-02-01 18:02:27 +0000469}
470
471
472static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +0000473nu_float(const char *p, const formatdef *f)
474{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000475 float x;
476 memcpy((char *)&x, p, sizeof x);
477 return PyFloat_FromDouble((double)x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000478}
479
480static PyObject *
481nu_double(const char *p, const formatdef *f)
482{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000483 double x;
484 memcpy((char *)&x, p, sizeof x);
485 return PyFloat_FromDouble(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000486}
487
488static PyObject *
489nu_void_p(const char *p, const formatdef *f)
490{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000491 void *x;
492 memcpy((char *)&x, p, sizeof x);
493 return PyLong_FromVoidPtr(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000494}
495
496static int
497np_byte(char *p, PyObject *v, const formatdef *f)
498{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 long x;
500 if (get_long(v, &x) < 0)
501 return -1;
502 if (x < -128 || x > 127){
503 PyErr_SetString(StructError,
504 "byte format requires -128 <= number <= 127");
505 return -1;
506 }
507 *p = (char)x;
508 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000509}
510
511static int
512np_ubyte(char *p, PyObject *v, const formatdef *f)
513{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000514 long x;
515 if (get_long(v, &x) < 0)
516 return -1;
517 if (x < 0 || x > 255){
518 PyErr_SetString(StructError,
519 "ubyte format requires 0 <= number <= 255");
520 return -1;
521 }
522 *p = (char)x;
523 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000524}
525
526static int
527np_char(char *p, PyObject *v, const formatdef *f)
528{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000529 if (!PyBytes_Check(v) || PyBytes_Size(v) != 1) {
530 PyErr_SetString(StructError,
Victor Stinnerda9ec992010-12-28 13:26:42 +0000531 "char format requires a bytes object of length 1");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000532 return -1;
533 }
534 *p = *PyBytes_AsString(v);
535 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000536}
537
538static int
539np_short(char *p, PyObject *v, const formatdef *f)
540{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000541 long x;
542 short y;
543 if (get_long(v, &x) < 0)
544 return -1;
545 if (x < SHRT_MIN || x > SHRT_MAX){
546 PyErr_SetString(StructError,
Victor Stinner45e8e2f2014-05-14 17:24:35 +0200547 "short format requires " Py_STRINGIFY(SHRT_MIN)
548 " <= number <= " Py_STRINGIFY(SHRT_MAX));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 return -1;
550 }
551 y = (short)x;
552 memcpy(p, (char *)&y, sizeof y);
553 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000554}
555
556static int
557np_ushort(char *p, PyObject *v, const formatdef *f)
558{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 long x;
560 unsigned short y;
561 if (get_long(v, &x) < 0)
562 return -1;
563 if (x < 0 || x > USHRT_MAX){
564 PyErr_SetString(StructError,
Victor Stinner45e8e2f2014-05-14 17:24:35 +0200565 "ushort format requires 0 <= number <= "
566 Py_STRINGIFY(USHRT_MAX));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000567 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
Antoine Pitrou9f146812013-04-27 00:20:04 +02001250/*
1251 * Struct object implementation.
1252 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001253
1254/* calculate the size of a format string */
1255
1256static int
1257prepare_s(PyStructObject *self)
1258{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001259 const formatdef *f;
1260 const formatdef *e;
1261 formatcode *codes;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001262
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001263 const char *s;
1264 const char *fmt;
1265 char c;
Victor Stinner706768c2014-08-16 01:03:39 +02001266 Py_ssize_t size, len, num, itemsize;
1267 size_t ncodes;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001268
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 fmt = PyBytes_AS_STRING(self->s_format);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001270
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001271 f = whichtable((char **)&fmt);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001272
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001273 s = fmt;
1274 size = 0;
1275 len = 0;
Serhiy Storchakafff61f22013-05-17 10:49:44 +03001276 ncodes = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001277 while ((c = *s++) != '\0') {
Antoine Pitrou4de74572013-02-09 23:11:27 +01001278 if (Py_ISSPACE(Py_CHARMASK(c)))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001279 continue;
1280 if ('0' <= c && c <= '9') {
1281 num = c - '0';
1282 while ('0' <= (c = *s++) && c <= '9') {
Mark Dickinsonab4096f2010-06-11 16:56:34 +00001283 /* overflow-safe version of
1284 if (num*10 + (c - '0') > PY_SSIZE_T_MAX) { ... } */
1285 if (num >= PY_SSIZE_T_MAX / 10 && (
1286 num > PY_SSIZE_T_MAX / 10 ||
Mark Dickinsonb72e6862010-06-11 19:50:30 +00001287 (c - '0') > PY_SSIZE_T_MAX % 10))
1288 goto overflow;
Mark Dickinsonab4096f2010-06-11 16:56:34 +00001289 num = num*10 + (c - '0');
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001290 }
Alexander Belopolsky177e8532010-06-11 16:04:59 +00001291 if (c == '\0') {
1292 PyErr_SetString(StructError,
1293 "repeat count given without format specifier");
1294 return -1;
1295 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001296 }
1297 else
1298 num = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001299
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001300 e = getentry(c, f);
1301 if (e == NULL)
1302 return -1;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001303
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001304 switch (c) {
1305 case 's': /* fall through */
Serhiy Storchakafff61f22013-05-17 10:49:44 +03001306 case 'p': len++; ncodes++; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001307 case 'x': break;
Serhiy Storchakafff61f22013-05-17 10:49:44 +03001308 default: len += num; if (num) ncodes++; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001309 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001310
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001311 itemsize = e->size;
1312 size = align(size, c, e);
Mark Dickinsonb72e6862010-06-11 19:50:30 +00001313 if (size == -1)
1314 goto overflow;
1315
1316 /* if (size + num * itemsize > PY_SSIZE_T_MAX) { ... } */
1317 if (num > (PY_SSIZE_T_MAX - size) / itemsize)
1318 goto overflow;
1319 size += num * itemsize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001320 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001322 /* check for overflow */
Victor Stinner706768c2014-08-16 01:03:39 +02001323 if ((ncodes + 1) > ((size_t)PY_SSIZE_T_MAX / sizeof(formatcode))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001324 PyErr_NoMemory();
1325 return -1;
1326 }
Amaury Forgeot d'Arc35c86582008-06-17 21:11:29 +00001327
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001328 self->s_size = size;
1329 self->s_len = len;
Serhiy Storchakafff61f22013-05-17 10:49:44 +03001330 codes = PyMem_MALLOC((ncodes + 1) * sizeof(formatcode));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001331 if (codes == NULL) {
1332 PyErr_NoMemory();
1333 return -1;
1334 }
Mark Dickinsoncf28b952010-07-29 21:41:59 +00001335 /* Free any s_codes value left over from a previous initialization. */
1336 if (self->s_codes != NULL)
1337 PyMem_FREE(self->s_codes);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001338 self->s_codes = codes;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001339
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001340 s = fmt;
1341 size = 0;
1342 while ((c = *s++) != '\0') {
Antoine Pitrou4de74572013-02-09 23:11:27 +01001343 if (Py_ISSPACE(Py_CHARMASK(c)))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001344 continue;
1345 if ('0' <= c && c <= '9') {
1346 num = c - '0';
1347 while ('0' <= (c = *s++) && c <= '9')
1348 num = num*10 + (c - '0');
1349 if (c == '\0')
1350 break;
1351 }
1352 else
1353 num = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001354
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001355 e = getentry(c, f);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001356
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001357 size = align(size, c, e);
1358 if (c == 's' || c == 'p') {
1359 codes->offset = size;
1360 codes->size = num;
1361 codes->fmtdef = e;
Serhiy Storchakafff61f22013-05-17 10:49:44 +03001362 codes->repeat = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001363 codes++;
1364 size += num;
1365 } else if (c == 'x') {
1366 size += num;
Serhiy Storchakafff61f22013-05-17 10:49:44 +03001367 } else if (num) {
1368 codes->offset = size;
1369 codes->size = e->size;
1370 codes->fmtdef = e;
1371 codes->repeat = num;
1372 codes++;
1373 size += e->size * num;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001374 }
1375 }
1376 codes->fmtdef = NULL;
1377 codes->offset = size;
1378 codes->size = 0;
Serhiy Storchakafff61f22013-05-17 10:49:44 +03001379 codes->repeat = 0;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001380
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001381 return 0;
Mark Dickinsonb72e6862010-06-11 19:50:30 +00001382
1383 overflow:
1384 PyErr_SetString(StructError,
1385 "total struct size too long");
1386 return -1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001387}
1388
1389static PyObject *
1390s_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1391{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001392 PyObject *self;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001393
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001394 assert(type != NULL && type->tp_alloc != NULL);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001395
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001396 self = type->tp_alloc(type, 0);
1397 if (self != NULL) {
1398 PyStructObject *s = (PyStructObject*)self;
1399 Py_INCREF(Py_None);
1400 s->s_format = Py_None;
1401 s->s_codes = NULL;
1402 s->s_size = -1;
1403 s->s_len = -1;
1404 }
1405 return self;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001406}
1407
1408static int
1409s_init(PyObject *self, PyObject *args, PyObject *kwds)
1410{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001411 PyStructObject *soself = (PyStructObject *)self;
1412 PyObject *o_format = NULL;
1413 int ret = 0;
1414 static char *kwlist[] = {"format", 0};
Thomas Wouters477c8d52006-05-27 19:21:47 +00001415
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001416 assert(PyStruct_Check(self));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001417
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001418 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:Struct", kwlist,
1419 &o_format))
1420 return -1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001421
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001422 if (PyUnicode_Check(o_format)) {
1423 o_format = PyUnicode_AsASCIIString(o_format);
1424 if (o_format == NULL)
1425 return -1;
1426 }
1427 /* XXX support buffer interface, too */
1428 else {
1429 Py_INCREF(o_format);
1430 }
Christian Heimesa34706f2008-01-04 03:06:10 +00001431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001432 if (!PyBytes_Check(o_format)) {
1433 Py_DECREF(o_format);
1434 PyErr_Format(PyExc_TypeError,
Victor Stinnerda9ec992010-12-28 13:26:42 +00001435 "Struct() argument 1 must be a bytes object, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001436 Py_TYPE(o_format)->tp_name);
1437 return -1;
1438 }
Christian Heimesa34706f2008-01-04 03:06:10 +00001439
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001440 Py_CLEAR(soself->s_format);
1441 soself->s_format = o_format;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001442
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001443 ret = prepare_s(soself);
1444 return ret;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001445}
1446
1447static void
1448s_dealloc(PyStructObject *s)
1449{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001450 if (s->weakreflist != NULL)
1451 PyObject_ClearWeakRefs((PyObject *)s);
1452 if (s->s_codes != NULL) {
1453 PyMem_FREE(s->s_codes);
1454 }
1455 Py_XDECREF(s->s_format);
1456 Py_TYPE(s)->tp_free((PyObject *)s);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001457}
1458
1459static PyObject *
1460s_unpack_internal(PyStructObject *soself, char *startfrom) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001461 formatcode *code;
1462 Py_ssize_t i = 0;
1463 PyObject *result = PyTuple_New(soself->s_len);
1464 if (result == NULL)
1465 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001466
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001467 for (code = soself->s_codes; code->fmtdef != NULL; code++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001468 const formatdef *e = code->fmtdef;
1469 const char *res = startfrom + code->offset;
Serhiy Storchakafff61f22013-05-17 10:49:44 +03001470 Py_ssize_t j = code->repeat;
1471 while (j--) {
1472 PyObject *v;
1473 if (e->format == 's') {
1474 v = PyBytes_FromStringAndSize(res, code->size);
1475 } else if (e->format == 'p') {
1476 Py_ssize_t n = *(unsigned char*)res;
1477 if (n >= code->size)
1478 n = code->size - 1;
1479 v = PyBytes_FromStringAndSize(res + 1, n);
1480 } else {
1481 v = e->unpack(res, e);
1482 }
1483 if (v == NULL)
1484 goto fail;
1485 PyTuple_SET_ITEM(result, i++, v);
1486 res += code->size;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001487 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001488 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001489
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001490 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001491fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001492 Py_DECREF(result);
1493 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001494}
1495
1496
1497PyDoc_STRVAR(s_unpack__doc__,
Guido van Rossum913dd0b2007-04-13 03:33:53 +00001498"S.unpack(buffer) -> (v1, v2, ...)\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +00001499\n\
Mark Dickinsonaacfa952010-06-12 15:43:45 +00001500Return a tuple containing values unpacked according to the format\n\
1501string S.format. Requires len(buffer) == S.size. See help(struct)\n\
1502for more on format strings.");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001503
1504static PyObject *
Guido van Rossum98297ee2007-11-06 21:34:58 +00001505s_unpack(PyObject *self, PyObject *input)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001506{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001507 Py_buffer vbuf;
1508 PyObject *result;
1509 PyStructObject *soself = (PyStructObject *)self;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001510
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001511 assert(PyStruct_Check(self));
1512 assert(soself->s_codes != NULL);
1513 if (PyObject_GetBuffer(input, &vbuf, PyBUF_SIMPLE) < 0)
1514 return NULL;
1515 if (vbuf.len != soself->s_size) {
1516 PyErr_Format(StructError,
Victor Stinnerda9ec992010-12-28 13:26:42 +00001517 "unpack requires a bytes object of length %zd",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001518 soself->s_size);
1519 PyBuffer_Release(&vbuf);
1520 return NULL;
1521 }
1522 result = s_unpack_internal(soself, vbuf.buf);
1523 PyBuffer_Release(&vbuf);
1524 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001525}
1526
1527PyDoc_STRVAR(s_unpack_from__doc__,
Mark Dickinsonc6f13962010-06-13 09:17:13 +00001528"S.unpack_from(buffer, offset=0) -> (v1, v2, ...)\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +00001529\n\
Mark Dickinsonaacfa952010-06-12 15:43:45 +00001530Return a tuple containing values unpacked according to the format\n\
1531string S.format. Requires len(buffer[offset:]) >= S.size. See\n\
1532help(struct) for more on format strings.");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001533
1534static PyObject *
1535s_unpack_from(PyObject *self, PyObject *args, PyObject *kwds)
1536{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001537 static char *kwlist[] = {"buffer", "offset", 0};
Guido van Rossum98297ee2007-11-06 21:34:58 +00001538
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001539 PyObject *input;
1540 Py_ssize_t offset = 0;
1541 Py_buffer vbuf;
1542 PyObject *result;
1543 PyStructObject *soself = (PyStructObject *)self;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001544
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001545 assert(PyStruct_Check(self));
1546 assert(soself->s_codes != NULL);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001547
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001548 if (!PyArg_ParseTupleAndKeywords(args, kwds,
1549 "O|n:unpack_from", kwlist,
1550 &input, &offset))
1551 return NULL;
1552 if (PyObject_GetBuffer(input, &vbuf, PyBUF_SIMPLE) < 0)
1553 return NULL;
1554 if (offset < 0)
1555 offset += vbuf.len;
1556 if (offset < 0 || vbuf.len - offset < soself->s_size) {
1557 PyErr_Format(StructError,
1558 "unpack_from requires a buffer of at least %zd bytes",
1559 soself->s_size);
1560 PyBuffer_Release(&vbuf);
1561 return NULL;
1562 }
1563 result = s_unpack_internal(soself, (char*)vbuf.buf + offset);
1564 PyBuffer_Release(&vbuf);
1565 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001566}
1567
1568
Antoine Pitrou9f146812013-04-27 00:20:04 +02001569/* Unpack iterator type */
1570
1571typedef struct {
1572 PyObject_HEAD
1573 PyStructObject *so;
1574 Py_buffer buf;
1575 Py_ssize_t index;
1576} unpackiterobject;
1577
1578static void
1579unpackiter_dealloc(unpackiterobject *self)
1580{
1581 Py_XDECREF(self->so);
1582 PyBuffer_Release(&self->buf);
1583 PyObject_GC_Del(self);
1584}
1585
1586static int
1587unpackiter_traverse(unpackiterobject *self, visitproc visit, void *arg)
1588{
1589 Py_VISIT(self->so);
1590 Py_VISIT(self->buf.obj);
1591 return 0;
1592}
1593
1594static PyObject *
1595unpackiter_len(unpackiterobject *self)
1596{
1597 Py_ssize_t len;
1598 if (self->so == NULL)
1599 len = 0;
1600 else
1601 len = (self->buf.len - self->index) / self->so->s_size;
1602 return PyLong_FromSsize_t(len);
1603}
1604
1605static PyMethodDef unpackiter_methods[] = {
1606 {"__length_hint__", (PyCFunction) unpackiter_len, METH_NOARGS, NULL},
1607 {NULL, NULL} /* sentinel */
1608};
1609
1610static PyObject *
1611unpackiter_iternext(unpackiterobject *self)
1612{
1613 PyObject *result;
1614 if (self->so == NULL)
1615 return NULL;
1616 if (self->index >= self->buf.len) {
1617 /* Iterator exhausted */
1618 Py_CLEAR(self->so);
1619 PyBuffer_Release(&self->buf);
1620 return NULL;
1621 }
1622 assert(self->index + self->so->s_size <= self->buf.len);
1623 result = s_unpack_internal(self->so,
1624 (char*) self->buf.buf + self->index);
1625 self->index += self->so->s_size;
1626 return result;
1627}
1628
doko@ubuntu.com46c5deb2013-11-23 16:07:55 +01001629static PyTypeObject unpackiter_type = {
Antoine Pitrou9f146812013-04-27 00:20:04 +02001630 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1631 "unpack_iterator", /* tp_name */
1632 sizeof(unpackiterobject), /* tp_basicsize */
1633 0, /* tp_itemsize */
1634 (destructor)unpackiter_dealloc, /* tp_dealloc */
1635 0, /* tp_print */
1636 0, /* tp_getattr */
1637 0, /* tp_setattr */
1638 0, /* tp_reserved */
1639 0, /* tp_repr */
1640 0, /* tp_as_number */
1641 0, /* tp_as_sequence */
1642 0, /* tp_as_mapping */
1643 0, /* tp_hash */
1644 0, /* tp_call */
1645 0, /* tp_str */
1646 PyObject_GenericGetAttr, /* tp_getattro */
1647 0, /* tp_setattro */
1648 0, /* tp_as_buffer */
1649 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
1650 0, /* tp_doc */
1651 (traverseproc)unpackiter_traverse, /* tp_traverse */
1652 0, /* tp_clear */
1653 0, /* tp_richcompare */
1654 0, /* tp_weaklistoffset */
1655 PyObject_SelfIter, /* tp_iter */
1656 (iternextfunc)unpackiter_iternext, /* tp_iternext */
1657 unpackiter_methods /* tp_methods */
1658};
1659
1660PyDoc_STRVAR(s_iter_unpack__doc__,
1661"S.iter_unpack(buffer) -> iterator(v1, v2, ...)\n\
1662\n\
1663Return an iterator yielding tuples unpacked from the given bytes\n\
1664source, like a repeated invocation of unpack_from(). Requires\n\
1665that the bytes length be a multiple of the struct size.");
1666
1667static PyObject *
1668s_iter_unpack(PyObject *_so, PyObject *input)
1669{
1670 PyStructObject *so = (PyStructObject *) _so;
1671 unpackiterobject *self;
1672
1673 assert(PyStruct_Check(_so));
1674 assert(so->s_codes != NULL);
1675
1676 if (so->s_size == 0) {
1677 PyErr_Format(StructError,
1678 "cannot iteratively unpack with a struct of length 0");
1679 return NULL;
1680 }
1681
1682 self = (unpackiterobject *) PyType_GenericAlloc(&unpackiter_type, 0);
1683 if (self == NULL)
1684 return NULL;
1685
1686 if (PyObject_GetBuffer(input, &self->buf, PyBUF_SIMPLE) < 0) {
1687 Py_DECREF(self);
1688 return NULL;
1689 }
1690 if (self->buf.len % so->s_size != 0) {
1691 PyErr_Format(StructError,
1692 "iterative unpacking requires a bytes length "
1693 "multiple of %zd",
1694 so->s_size);
1695 Py_DECREF(self);
1696 return NULL;
1697 }
1698 Py_INCREF(so);
1699 self->so = so;
1700 self->index = 0;
1701 return (PyObject *) self;
1702}
1703
1704
Thomas Wouters477c8d52006-05-27 19:21:47 +00001705/*
1706 * Guts of the pack function.
1707 *
1708 * Takes a struct object, a tuple of arguments, and offset in that tuple of
1709 * argument for where to start processing the arguments for packing, and a
1710 * character buffer for writing the packed string. The caller must insure
1711 * that the buffer may contain the required length for packing the arguments.
1712 * 0 is returned on success, 1 is returned if there is an error.
1713 *
1714 */
1715static int
1716s_pack_internal(PyStructObject *soself, PyObject *args, int offset, char* buf)
1717{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001718 formatcode *code;
1719 /* XXX(nnorwitz): why does i need to be a local? can we use
1720 the offset parameter or do we need the wider width? */
1721 Py_ssize_t i;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001722
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001723 memset(buf, '\0', soself->s_size);
1724 i = offset;
1725 for (code = soself->s_codes; code->fmtdef != NULL; code++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001726 const formatdef *e = code->fmtdef;
1727 char *res = buf + code->offset;
Serhiy Storchakafff61f22013-05-17 10:49:44 +03001728 Py_ssize_t j = code->repeat;
1729 while (j--) {
1730 PyObject *v = PyTuple_GET_ITEM(args, i++);
1731 if (e->format == 's') {
1732 Py_ssize_t n;
1733 int isstring;
1734 void *p;
1735 isstring = PyBytes_Check(v);
1736 if (!isstring && !PyByteArray_Check(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001737 PyErr_SetString(StructError,
Serhiy Storchakafff61f22013-05-17 10:49:44 +03001738 "argument for 's' must be a bytes object");
1739 return -1;
1740 }
1741 if (isstring) {
1742 n = PyBytes_GET_SIZE(v);
1743 p = PyBytes_AS_STRING(v);
1744 }
1745 else {
1746 n = PyByteArray_GET_SIZE(v);
1747 p = PyByteArray_AS_STRING(v);
1748 }
1749 if (n > code->size)
1750 n = code->size;
1751 if (n > 0)
1752 memcpy(res, p, n);
1753 } else if (e->format == 'p') {
1754 Py_ssize_t n;
1755 int isstring;
1756 void *p;
1757 isstring = PyBytes_Check(v);
1758 if (!isstring && !PyByteArray_Check(v)) {
1759 PyErr_SetString(StructError,
1760 "argument for 'p' must be a bytes object");
1761 return -1;
1762 }
1763 if (isstring) {
1764 n = PyBytes_GET_SIZE(v);
1765 p = PyBytes_AS_STRING(v);
1766 }
1767 else {
1768 n = PyByteArray_GET_SIZE(v);
1769 p = PyByteArray_AS_STRING(v);
1770 }
1771 if (n > (code->size - 1))
1772 n = code->size - 1;
1773 if (n > 0)
1774 memcpy(res + 1, p, n);
1775 if (n > 255)
1776 n = 255;
1777 *res = Py_SAFE_DOWNCAST(n, Py_ssize_t, unsigned char);
1778 } else {
1779 if (e->pack(res, v, e) < 0) {
1780 if (PyLong_Check(v) && PyErr_ExceptionMatches(PyExc_OverflowError))
1781 PyErr_SetString(StructError,
Serhiy Storchaka46e1ce22013-08-27 20:17:03 +03001782 "int too large to convert");
Serhiy Storchakafff61f22013-05-17 10:49:44 +03001783 return -1;
1784 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001785 }
Serhiy Storchakafff61f22013-05-17 10:49:44 +03001786 res += code->size;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001787 }
1788 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001789
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001790 /* Success */
1791 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001792}
1793
1794
1795PyDoc_STRVAR(s_pack__doc__,
Benjamin Peterson4ae19462008-07-31 15:03:40 +00001796"S.pack(v1, v2, ...) -> bytes\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +00001797\n\
Mark Dickinsonaacfa952010-06-12 15:43:45 +00001798Return a bytes object containing values v1, v2, ... packed according\n\
1799to the format string S.format. See help(struct) for more on format\n\
1800strings.");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001801
1802static PyObject *
1803s_pack(PyObject *self, PyObject *args)
1804{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001805 PyStructObject *soself;
1806 PyObject *result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001807
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001808 /* Validate arguments. */
1809 soself = (PyStructObject *)self;
1810 assert(PyStruct_Check(self));
1811 assert(soself->s_codes != NULL);
1812 if (PyTuple_GET_SIZE(args) != soself->s_len)
1813 {
1814 PyErr_Format(StructError,
Petri Lehtinen92c28ca2012-10-29 21:16:57 +02001815 "pack expected %zd items for packing (got %zd)", soself->s_len, PyTuple_GET_SIZE(args));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001816 return NULL;
1817 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001818
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001819 /* Allocate a new string */
1820 result = PyBytes_FromStringAndSize((char *)NULL, soself->s_size);
1821 if (result == NULL)
1822 return NULL;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001823
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001824 /* Call the guts */
1825 if ( s_pack_internal(soself, args, 0, PyBytes_AS_STRING(result)) != 0 ) {
1826 Py_DECREF(result);
1827 return NULL;
1828 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001829
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001830 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001831}
1832
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001833PyDoc_STRVAR(s_pack_into__doc__,
1834"S.pack_into(buffer, offset, v1, v2, ...)\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +00001835\n\
Mark Dickinsonaacfa952010-06-12 15:43:45 +00001836Pack the values v1, v2, ... according to the format string S.format\n\
1837and write the packed bytes into the writable buffer buf starting at\n\
Mark Dickinsonfdb99f12010-06-12 16:30:53 +00001838offset. Note that the offset is a required argument. See\n\
Mark Dickinsonaacfa952010-06-12 15:43:45 +00001839help(struct) for more on format strings.");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001840
1841static PyObject *
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001842s_pack_into(PyObject *self, PyObject *args)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001843{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001844 PyStructObject *soself;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001845 Py_buffer buffer;
1846 Py_ssize_t offset;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001847
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001848 /* Validate arguments. +1 is for the first arg as buffer. */
1849 soself = (PyStructObject *)self;
1850 assert(PyStruct_Check(self));
1851 assert(soself->s_codes != NULL);
1852 if (PyTuple_GET_SIZE(args) != (soself->s_len + 2))
1853 {
Petri Lehtinen92c28ca2012-10-29 21:16:57 +02001854 if (PyTuple_GET_SIZE(args) == 0) {
1855 PyErr_Format(StructError,
1856 "pack_into expected buffer argument");
1857 }
1858 else if (PyTuple_GET_SIZE(args) == 1) {
1859 PyErr_Format(StructError,
1860 "pack_into expected offset argument");
1861 }
1862 else {
1863 PyErr_Format(StructError,
1864 "pack_into expected %zd items for packing (got %zd)",
1865 soself->s_len, (PyTuple_GET_SIZE(args) - 2));
1866 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001867 return NULL;
1868 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001869
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001870 /* Extract a writable memory buffer from the first argument */
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001871 if (!PyArg_Parse(PyTuple_GET_ITEM(args, 0), "w*", &buffer))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001872 return NULL;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001873 assert(buffer.len >= 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001874
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001875 /* Extract the offset from the first argument */
1876 offset = PyNumber_AsSsize_t(PyTuple_GET_ITEM(args, 1), PyExc_IndexError);
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001877 if (offset == -1 && PyErr_Occurred()) {
1878 PyBuffer_Release(&buffer);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001879 return NULL;
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001880 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001881
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001882 /* Support negative offsets. */
1883 if (offset < 0)
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001884 offset += buffer.len;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001885
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001886 /* Check boundaries */
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001887 if (offset < 0 || (buffer.len - offset) < soself->s_size) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001888 PyErr_Format(StructError,
1889 "pack_into requires a buffer of at least %zd bytes",
1890 soself->s_size);
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001891 PyBuffer_Release(&buffer);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001892 return NULL;
1893 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001894
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001895 /* Call the guts */
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001896 if (s_pack_internal(soself, args, 2, (char*)buffer.buf + offset) != 0) {
1897 PyBuffer_Release(&buffer);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001898 return NULL;
1899 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001900
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +02001901 PyBuffer_Release(&buffer);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001902 Py_RETURN_NONE;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001903}
1904
1905static PyObject *
1906s_get_format(PyStructObject *self, void *unused)
1907{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001908 Py_INCREF(self->s_format);
1909 return self->s_format;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001910}
1911
1912static PyObject *
1913s_get_size(PyStructObject *self, void *unused)
1914{
Christian Heimes217cfd12007-12-02 14:31:20 +00001915 return PyLong_FromSsize_t(self->s_size);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001916}
1917
Meador Ingeb14d8c92012-07-23 10:01:29 -05001918PyDoc_STRVAR(s_sizeof__doc__,
1919"S.__sizeof__() -> size of S in memory, in bytes");
1920
1921static PyObject *
Meador Inge90bc2dbc2012-07-28 22:16:39 -05001922s_sizeof(PyStructObject *self, void *unused)
Meador Ingeb14d8c92012-07-23 10:01:29 -05001923{
1924 Py_ssize_t size;
Serhiy Storchakafff61f22013-05-17 10:49:44 +03001925 formatcode *code;
Meador Ingeb14d8c92012-07-23 10:01:29 -05001926
Serhiy Storchakafff61f22013-05-17 10:49:44 +03001927 size = sizeof(PyStructObject) + sizeof(formatcode);
1928 for (code = self->s_codes; code->fmtdef != NULL; code++)
1929 size += sizeof(formatcode);
Meador Ingeb14d8c92012-07-23 10:01:29 -05001930 return PyLong_FromSsize_t(size);
1931}
1932
Thomas Wouters477c8d52006-05-27 19:21:47 +00001933/* List of functions */
1934
1935static struct PyMethodDef s_methods[] = {
Antoine Pitrou9f146812013-04-27 00:20:04 +02001936 {"iter_unpack", s_iter_unpack, METH_O, s_iter_unpack__doc__},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001937 {"pack", s_pack, METH_VARARGS, s_pack__doc__},
1938 {"pack_into", s_pack_into, METH_VARARGS, s_pack_into__doc__},
1939 {"unpack", s_unpack, METH_O, s_unpack__doc__},
1940 {"unpack_from", (PyCFunction)s_unpack_from, METH_VARARGS|METH_KEYWORDS,
1941 s_unpack_from__doc__},
Meador Ingeb14d8c92012-07-23 10:01:29 -05001942 {"__sizeof__", (PyCFunction)s_sizeof, METH_NOARGS, s_sizeof__doc__},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001943 {NULL, NULL} /* sentinel */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001944};
1945
Victor Stinnerda9ec992010-12-28 13:26:42 +00001946PyDoc_STRVAR(s__doc__,
Alexander Belopolsky0bd003a2010-06-12 19:36:28 +00001947"Struct(fmt) --> compiled struct object\n"
1948"\n"
1949"Return a new Struct object which writes and reads binary data according to\n"
1950"the format string fmt. See help(struct) for more on format strings.");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001951
1952#define OFF(x) offsetof(PyStructObject, x)
1953
1954static PyGetSetDef s_getsetlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001955 {"format", (getter)s_get_format, (setter)NULL, "struct format string", NULL},
1956 {"size", (getter)s_get_size, (setter)NULL, "struct size in bytes", NULL},
1957 {NULL} /* sentinel */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001958};
1959
1960static
1961PyTypeObject PyStructType = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001962 PyVarObject_HEAD_INIT(NULL, 0)
1963 "Struct",
1964 sizeof(PyStructObject),
1965 0,
1966 (destructor)s_dealloc, /* tp_dealloc */
1967 0, /* tp_print */
1968 0, /* tp_getattr */
1969 0, /* tp_setattr */
1970 0, /* tp_reserved */
1971 0, /* tp_repr */
1972 0, /* tp_as_number */
1973 0, /* tp_as_sequence */
1974 0, /* tp_as_mapping */
1975 0, /* tp_hash */
1976 0, /* tp_call */
1977 0, /* tp_str */
1978 PyObject_GenericGetAttr, /* tp_getattro */
1979 PyObject_GenericSetAttr, /* tp_setattro */
1980 0, /* tp_as_buffer */
1981 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
1982 s__doc__, /* tp_doc */
1983 0, /* tp_traverse */
1984 0, /* tp_clear */
1985 0, /* tp_richcompare */
1986 offsetof(PyStructObject, weakreflist), /* tp_weaklistoffset */
1987 0, /* tp_iter */
1988 0, /* tp_iternext */
1989 s_methods, /* tp_methods */
1990 NULL, /* tp_members */
1991 s_getsetlist, /* tp_getset */
1992 0, /* tp_base */
1993 0, /* tp_dict */
1994 0, /* tp_descr_get */
1995 0, /* tp_descr_set */
1996 0, /* tp_dictoffset */
1997 s_init, /* tp_init */
1998 PyType_GenericAlloc,/* tp_alloc */
1999 s_new, /* tp_new */
2000 PyObject_Del, /* tp_free */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002001};
2002
Christian Heimesa34706f2008-01-04 03:06:10 +00002003
2004/* ---- Standalone functions ---- */
2005
2006#define MAXCACHE 100
2007static PyObject *cache = NULL;
2008
2009static PyObject *
2010cache_struct(PyObject *fmt)
2011{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002012 PyObject * s_object;
Christian Heimesa34706f2008-01-04 03:06:10 +00002013
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002014 if (cache == NULL) {
2015 cache = PyDict_New();
2016 if (cache == NULL)
2017 return NULL;
2018 }
Christian Heimesa34706f2008-01-04 03:06:10 +00002019
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002020 s_object = PyDict_GetItem(cache, fmt);
2021 if (s_object != NULL) {
2022 Py_INCREF(s_object);
2023 return s_object;
2024 }
Christian Heimesa34706f2008-01-04 03:06:10 +00002025
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002026 s_object = PyObject_CallFunctionObjArgs((PyObject *)(&PyStructType), fmt, NULL);
2027 if (s_object != NULL) {
2028 if (PyDict_Size(cache) >= MAXCACHE)
2029 PyDict_Clear(cache);
2030 /* Attempt to cache the result */
2031 if (PyDict_SetItem(cache, fmt, s_object) == -1)
2032 PyErr_Clear();
2033 }
2034 return s_object;
Christian Heimesa34706f2008-01-04 03:06:10 +00002035}
2036
2037PyDoc_STRVAR(clearcache_doc,
2038"Clear the internal cache.");
2039
2040static PyObject *
2041clearcache(PyObject *self)
2042{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002043 Py_CLEAR(cache);
2044 Py_RETURN_NONE;
Christian Heimesa34706f2008-01-04 03:06:10 +00002045}
2046
2047PyDoc_STRVAR(calcsize_doc,
Mark Dickinsonaacfa952010-06-12 15:43:45 +00002048"calcsize(fmt) -> integer\n\
2049\n\
2050Return size in bytes of the struct described by the format string fmt.");
Christian Heimesa34706f2008-01-04 03:06:10 +00002051
2052static PyObject *
2053calcsize(PyObject *self, PyObject *fmt)
2054{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002055 Py_ssize_t n;
2056 PyObject *s_object = cache_struct(fmt);
2057 if (s_object == NULL)
2058 return NULL;
2059 n = ((PyStructObject *)s_object)->s_size;
2060 Py_DECREF(s_object);
2061 return PyLong_FromSsize_t(n);
Christian Heimesa34706f2008-01-04 03:06:10 +00002062}
2063
2064PyDoc_STRVAR(pack_doc,
Mark Dickinsonaacfa952010-06-12 15:43:45 +00002065"pack(fmt, v1, v2, ...) -> bytes\n\
2066\n\
Mark Dickinsonfdb99f12010-06-12 16:30:53 +00002067Return a bytes object containing the values v1, v2, ... packed according\n\
2068to the format string fmt. See help(struct) for more on format strings.");
Christian Heimesa34706f2008-01-04 03:06:10 +00002069
2070static PyObject *
2071pack(PyObject *self, PyObject *args)
2072{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002073 PyObject *s_object, *fmt, *newargs, *result;
2074 Py_ssize_t n = PyTuple_GET_SIZE(args);
Christian Heimesa34706f2008-01-04 03:06:10 +00002075
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002076 if (n == 0) {
2077 PyErr_SetString(PyExc_TypeError, "missing format argument");
2078 return NULL;
2079 }
2080 fmt = PyTuple_GET_ITEM(args, 0);
2081 newargs = PyTuple_GetSlice(args, 1, n);
2082 if (newargs == NULL)
2083 return NULL;
Christian Heimesa34706f2008-01-04 03:06:10 +00002084
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002085 s_object = cache_struct(fmt);
2086 if (s_object == NULL) {
2087 Py_DECREF(newargs);
2088 return NULL;
2089 }
2090 result = s_pack(s_object, newargs);
2091 Py_DECREF(newargs);
2092 Py_DECREF(s_object);
2093 return result;
Christian Heimesa34706f2008-01-04 03:06:10 +00002094}
2095
2096PyDoc_STRVAR(pack_into_doc,
Mark Dickinsonaacfa952010-06-12 15:43:45 +00002097"pack_into(fmt, buffer, offset, v1, v2, ...)\n\
2098\n\
2099Pack the values v1, v2, ... according to the format string fmt and write\n\
2100the packed bytes into the writable buffer buf starting at offset. Note\n\
Mark Dickinsonfdb99f12010-06-12 16:30:53 +00002101that the offset is a required argument. See help(struct) for more\n\
Mark Dickinsonaacfa952010-06-12 15:43:45 +00002102on format strings.");
Christian Heimesa34706f2008-01-04 03:06:10 +00002103
2104static PyObject *
2105pack_into(PyObject *self, PyObject *args)
2106{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002107 PyObject *s_object, *fmt, *newargs, *result;
2108 Py_ssize_t n = PyTuple_GET_SIZE(args);
Christian Heimesa34706f2008-01-04 03:06:10 +00002109
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002110 if (n == 0) {
2111 PyErr_SetString(PyExc_TypeError, "missing format argument");
2112 return NULL;
2113 }
2114 fmt = PyTuple_GET_ITEM(args, 0);
2115 newargs = PyTuple_GetSlice(args, 1, n);
2116 if (newargs == NULL)
2117 return NULL;
Christian Heimesa34706f2008-01-04 03:06:10 +00002118
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002119 s_object = cache_struct(fmt);
2120 if (s_object == NULL) {
2121 Py_DECREF(newargs);
2122 return NULL;
2123 }
2124 result = s_pack_into(s_object, newargs);
2125 Py_DECREF(newargs);
2126 Py_DECREF(s_object);
2127 return result;
Christian Heimesa34706f2008-01-04 03:06:10 +00002128}
2129
2130PyDoc_STRVAR(unpack_doc,
Mark Dickinsonaacfa952010-06-12 15:43:45 +00002131"unpack(fmt, buffer) -> (v1, v2, ...)\n\
2132\n\
2133Return a tuple containing values unpacked according to the format string\n\
2134fmt. Requires len(buffer) == calcsize(fmt). See help(struct) for more\n\
2135on format strings.");
Christian Heimesa34706f2008-01-04 03:06:10 +00002136
2137static PyObject *
2138unpack(PyObject *self, PyObject *args)
2139{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002140 PyObject *s_object, *fmt, *inputstr, *result;
Christian Heimesa34706f2008-01-04 03:06:10 +00002141
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002142 if (!PyArg_UnpackTuple(args, "unpack", 2, 2, &fmt, &inputstr))
2143 return NULL;
Christian Heimesa34706f2008-01-04 03:06:10 +00002144
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002145 s_object = cache_struct(fmt);
2146 if (s_object == NULL)
2147 return NULL;
2148 result = s_unpack(s_object, inputstr);
2149 Py_DECREF(s_object);
2150 return result;
Christian Heimesa34706f2008-01-04 03:06:10 +00002151}
2152
2153PyDoc_STRVAR(unpack_from_doc,
Mark Dickinsonc6f13962010-06-13 09:17:13 +00002154"unpack_from(fmt, buffer, offset=0) -> (v1, v2, ...)\n\
Mark Dickinsonaacfa952010-06-12 15:43:45 +00002155\n\
2156Return a tuple containing values unpacked according to the format string\n\
2157fmt. Requires len(buffer[offset:]) >= calcsize(fmt). See help(struct)\n\
2158for more on format strings.");
Christian Heimesa34706f2008-01-04 03:06:10 +00002159
2160static PyObject *
2161unpack_from(PyObject *self, PyObject *args, PyObject *kwds)
2162{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002163 PyObject *s_object, *fmt, *newargs, *result;
2164 Py_ssize_t n = PyTuple_GET_SIZE(args);
Christian Heimesa34706f2008-01-04 03:06:10 +00002165
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002166 if (n == 0) {
2167 PyErr_SetString(PyExc_TypeError, "missing format argument");
2168 return NULL;
2169 }
2170 fmt = PyTuple_GET_ITEM(args, 0);
2171 newargs = PyTuple_GetSlice(args, 1, n);
2172 if (newargs == NULL)
2173 return NULL;
Christian Heimesa34706f2008-01-04 03:06:10 +00002174
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002175 s_object = cache_struct(fmt);
2176 if (s_object == NULL) {
2177 Py_DECREF(newargs);
2178 return NULL;
2179 }
2180 result = s_unpack_from(s_object, newargs, kwds);
2181 Py_DECREF(newargs);
2182 Py_DECREF(s_object);
2183 return result;
Christian Heimesa34706f2008-01-04 03:06:10 +00002184}
2185
Antoine Pitrou9f146812013-04-27 00:20:04 +02002186PyDoc_STRVAR(iter_unpack_doc,
2187"iter_unpack(fmt, buffer) -> iterator(v1, v2, ...)\n\
2188\n\
2189Return an iterator yielding tuples unpacked from the given bytes\n\
2190source according to the format string, like a repeated invocation of\n\
2191unpack_from(). Requires that the bytes length be a multiple of the\n\
2192format struct size.");
2193
2194static PyObject *
2195iter_unpack(PyObject *self, PyObject *args)
2196{
2197 PyObject *s_object, *fmt, *input, *result;
2198
2199 if (!PyArg_ParseTuple(args, "OO:iter_unpack", &fmt, &input))
2200 return NULL;
2201
2202 s_object = cache_struct(fmt);
2203 if (s_object == NULL)
2204 return NULL;
2205 result = s_iter_unpack(s_object, input);
2206 Py_DECREF(s_object);
2207 return result;
2208}
2209
Christian Heimesa34706f2008-01-04 03:06:10 +00002210static struct PyMethodDef module_functions[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002211 {"_clearcache", (PyCFunction)clearcache, METH_NOARGS, clearcache_doc},
2212 {"calcsize", calcsize, METH_O, calcsize_doc},
Antoine Pitrou9f146812013-04-27 00:20:04 +02002213 {"iter_unpack", iter_unpack, METH_VARARGS, iter_unpack_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002214 {"pack", pack, METH_VARARGS, pack_doc},
2215 {"pack_into", pack_into, METH_VARARGS, pack_into_doc},
2216 {"unpack", unpack, METH_VARARGS, unpack_doc},
2217 {"unpack_from", (PyCFunction)unpack_from,
2218 METH_VARARGS|METH_KEYWORDS, unpack_from_doc},
2219 {NULL, NULL} /* sentinel */
Christian Heimesa34706f2008-01-04 03:06:10 +00002220};
2221
2222
Thomas Wouters477c8d52006-05-27 19:21:47 +00002223/* Module initialization */
2224
Christian Heimesa34706f2008-01-04 03:06:10 +00002225PyDoc_STRVAR(module_doc,
2226"Functions to convert between Python values and C structs.\n\
Benjamin Peterson4ae19462008-07-31 15:03:40 +00002227Python bytes objects are used to hold the data representing the C struct\n\
Mark Dickinson40714af2009-10-08 15:59:20 +00002228and also as format strings (explained below) to describe the layout of data\n\
2229in the C struct.\n\
Christian Heimesa34706f2008-01-04 03:06:10 +00002230\n\
2231The optional first format char indicates byte order, size and alignment:\n\
Mark Dickinson40714af2009-10-08 15:59:20 +00002232 @: native order, size & alignment (default)\n\
2233 =: native order, std. size & alignment\n\
2234 <: little-endian, std. size & alignment\n\
2235 >: big-endian, std. size & alignment\n\
2236 !: same as >\n\
Christian Heimesa34706f2008-01-04 03:06:10 +00002237\n\
2238The remaining chars indicate types of args and must match exactly;\n\
2239these can be preceded by a decimal repeat count:\n\
2240 x: pad byte (no data); c:char; b:signed byte; B:unsigned byte;\n\
Mark Dickinson40714af2009-10-08 15:59:20 +00002241 ?: _Bool (requires C99; if not available, char is used instead)\n\
Christian Heimesa34706f2008-01-04 03:06:10 +00002242 h:short; H:unsigned short; i:int; I:unsigned int;\n\
2243 l:long; L:unsigned long; f:float; d:double.\n\
2244Special cases (preceding decimal count indicates length):\n\
2245 s:string (array of char); p: pascal string (with count byte).\n\
Antoine Pitrou45d9c912011-10-06 15:27:40 +02002246Special cases (only available in native format):\n\
2247 n:ssize_t; N:size_t;\n\
Christian Heimesa34706f2008-01-04 03:06:10 +00002248 P:an integer type that is wide enough to hold a pointer.\n\
2249Special case (not in native mode unless 'long long' in platform C):\n\
2250 q:long long; Q:unsigned long long\n\
2251Whitespace between formats is ignored.\n\
2252\n\
2253The variable struct.error is an exception raised on errors.\n");
2254
Martin v. Löwis1a214512008-06-11 05:26:20 +00002255
2256static struct PyModuleDef _structmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002257 PyModuleDef_HEAD_INIT,
2258 "_struct",
2259 module_doc,
2260 -1,
2261 module_functions,
2262 NULL,
2263 NULL,
2264 NULL,
2265 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00002266};
2267
Thomas Wouters477c8d52006-05-27 19:21:47 +00002268PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00002269PyInit__struct(void)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002270{
Mark Dickinson06817852010-06-12 09:25:13 +00002271 PyObject *m;
Christian Heimesa34706f2008-01-04 03:06:10 +00002272
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002273 m = PyModule_Create(&_structmodule);
2274 if (m == NULL)
2275 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002276
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002277 Py_TYPE(&PyStructType) = &PyType_Type;
2278 if (PyType_Ready(&PyStructType) < 0)
2279 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002280
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002281 /* Check endian and swap in faster functions */
2282 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002283 formatdef *native = native_table;
2284 formatdef *other, *ptr;
Christian Heimes743e0cd2012-10-17 23:52:17 +02002285#if PY_LITTLE_ENDIAN
2286 other = lilendian_table;
2287#else
2288 other = bigendian_table;
2289#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002290 /* Scan through the native table, find a matching
2291 entry in the endian table and swap in the
2292 native implementations whenever possible
2293 (64-bit platforms may not have "standard" sizes) */
2294 while (native->format != '\0' && other->format != '\0') {
2295 ptr = other;
2296 while (ptr->format != '\0') {
2297 if (ptr->format == native->format) {
2298 /* Match faster when formats are
2299 listed in the same order */
2300 if (ptr == other)
2301 other++;
2302 /* Only use the trick if the
2303 size matches */
2304 if (ptr->size != native->size)
2305 break;
2306 /* Skip float and double, could be
2307 "unknown" float format */
2308 if (ptr->format == 'd' || ptr->format == 'f')
2309 break;
2310 ptr->pack = native->pack;
2311 ptr->unpack = native->unpack;
2312 break;
2313 }
2314 ptr++;
2315 }
2316 native++;
2317 }
2318 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002319
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002320 /* Add some symbolic constants to the module */
2321 if (StructError == NULL) {
2322 StructError = PyErr_NewException("struct.error", NULL, NULL);
2323 if (StructError == NULL)
2324 return NULL;
2325 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002326
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002327 Py_INCREF(StructError);
2328 PyModule_AddObject(m, "error", StructError);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002329
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002330 Py_INCREF((PyObject*)&PyStructType);
2331 PyModule_AddObject(m, "Struct", (PyObject*)&PyStructType);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002332
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002333 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002334}