blob: 7f8198b31657ddb2826d863d67a81b7f4b0dd45e [file] [log] [blame]
Bob Ippolito232f3c92006-05-23 19:12:41 +00001/* struct module -- pack values into and (out of) strings */
2
3/* New version supporting byte order, alignment and size options,
4 character strings, and unsigned numbers */
5
Bob Ippolitoaa70a172006-05-26 20:25:23 +00006#define PY_SSIZE_T_CLEAN
7
Bob Ippolito232f3c92006-05-23 19:12:41 +00008#include "Python.h"
9#include "structseq.h"
10#include "structmember.h"
11#include <ctype.h>
12
Bob Ippolitod3611eb2006-05-23 19:31:23 +000013static PyTypeObject PyStructType;
Bob Ippolito232f3c92006-05-23 19:12:41 +000014
15/* compatibility macros */
16#if (PY_VERSION_HEX < 0x02050000)
17typedef int Py_ssize_t;
18#endif
19
Bob Ippolitoe6c9f982006-08-04 23:59:21 +000020/* If PY_STRUCT_FLOAT_COERCE is defined, the struct module will allow float
21 arguments for integer formats with a warning for backwards
22 compatibility. */
23
24#define PY_STRUCT_FLOAT_COERCE 1
25
26#ifdef PY_STRUCT_FLOAT_COERCE
27#define FLOAT_COERCE "integer argument expected, got float"
28#endif
29
30
Bob Ippolito232f3c92006-05-23 19:12:41 +000031/* The translation function for each format character is table driven */
Bob Ippolito232f3c92006-05-23 19:12:41 +000032typedef struct _formatdef {
33 char format;
Bob Ippolitoaa70a172006-05-26 20:25:23 +000034 Py_ssize_t size;
35 Py_ssize_t alignment;
Bob Ippolito232f3c92006-05-23 19:12:41 +000036 PyObject* (*unpack)(const char *,
37 const struct _formatdef *);
38 int (*pack)(char *, PyObject *,
39 const struct _formatdef *);
40} formatdef;
41
42typedef struct _formatcode {
43 const struct _formatdef *fmtdef;
Bob Ippolitoaa70a172006-05-26 20:25:23 +000044 Py_ssize_t offset;
45 Py_ssize_t size;
Bob Ippolito232f3c92006-05-23 19:12:41 +000046} formatcode;
47
48/* Struct object interface */
49
50typedef struct {
51 PyObject_HEAD
Bob Ippolitoaa70a172006-05-26 20:25:23 +000052 Py_ssize_t s_size;
53 Py_ssize_t s_len;
Bob Ippolito232f3c92006-05-23 19:12:41 +000054 formatcode *s_codes;
55 PyObject *s_format;
56 PyObject *weakreflist; /* List of weak references */
57} PyStructObject;
58
Bob Ippolitoeb621272006-05-24 15:32:06 +000059
Bob Ippolito07c023b2006-05-23 19:32:25 +000060#define PyStruct_Check(op) PyObject_TypeCheck(op, &PyStructType)
Christian Heimese93237d2007-12-19 02:37:44 +000061#define PyStruct_CheckExact(op) (Py_TYPE(op) == &PyStructType)
Bob Ippolito232f3c92006-05-23 19:12:41 +000062
63
64/* Exception */
65
66static PyObject *StructError;
67
68
69/* Define various structs to figure out the alignments of types */
70
71
72typedef struct { char c; short x; } st_short;
73typedef struct { char c; int x; } st_int;
74typedef struct { char c; long x; } st_long;
75typedef struct { char c; float x; } st_float;
76typedef struct { char c; double x; } st_double;
77typedef struct { char c; void *x; } st_void_p;
78
79#define SHORT_ALIGN (sizeof(st_short) - sizeof(short))
80#define INT_ALIGN (sizeof(st_int) - sizeof(int))
81#define LONG_ALIGN (sizeof(st_long) - sizeof(long))
82#define FLOAT_ALIGN (sizeof(st_float) - sizeof(float))
83#define DOUBLE_ALIGN (sizeof(st_double) - sizeof(double))
84#define VOID_P_ALIGN (sizeof(st_void_p) - sizeof(void *))
85
86/* We can't support q and Q in native mode unless the compiler does;
87 in std mode, they're 8 bytes on all platforms. */
88#ifdef HAVE_LONG_LONG
89typedef struct { char c; PY_LONG_LONG x; } s_long_long;
90#define LONG_LONG_ALIGN (sizeof(s_long_long) - sizeof(PY_LONG_LONG))
91#endif
92
Martin v. Löwisaef4c6b2007-01-21 09:33:07 +000093#ifdef HAVE_C99_BOOL
94#define BOOL_TYPE _Bool
95typedef struct { char c; _Bool x; } s_bool;
96#define BOOL_ALIGN (sizeof(s_bool) - sizeof(BOOL_TYPE))
97#else
98#define BOOL_TYPE char
99#define BOOL_ALIGN 0
100#endif
101
Bob Ippolito232f3c92006-05-23 19:12:41 +0000102#define STRINGIFY(x) #x
103
104#ifdef __powerc
105#pragma options align=reset
106#endif
107
Mark Dickinson5fd3af22009-07-07 15:08:28 +0000108static char *integer_codes = "bBhHiIlLqQ";
109
Bob Ippolito232f3c92006-05-23 19:12:41 +0000110/* Helper to get a PyLongObject by hook or by crook. Caller should decref. */
111
112static PyObject *
113get_pylong(PyObject *v)
114{
Bob Ippolito232f3c92006-05-23 19:12:41 +0000115 assert(v != NULL);
116 if (PyInt_Check(v))
117 return PyLong_FromLong(PyInt_AS_LONG(v));
118 if (PyLong_Check(v)) {
119 Py_INCREF(v);
120 return v;
121 }
Mark Dickinson463dc4b2009-07-05 10:01:24 +0000122#ifdef PY_STRUCT_FLOAT_COERCE
123 if (PyFloat_Check(v)) {
124 if (PyErr_WarnEx(PyExc_DeprecationWarning, FLOAT_COERCE, 2)<0)
Bob Ippolito232f3c92006-05-23 19:12:41 +0000125 return NULL;
Mark Dickinson463dc4b2009-07-05 10:01:24 +0000126 return PyNumber_Long(v);
Bob Ippolito232f3c92006-05-23 19:12:41 +0000127 }
Mark Dickinson463dc4b2009-07-05 10:01:24 +0000128#endif
Bob Ippolito232f3c92006-05-23 19:12:41 +0000129 PyErr_SetString(StructError,
130 "cannot convert argument to long");
131 return NULL;
132}
133
Mark Dickinson5fd3af22009-07-07 15:08:28 +0000134/* Helper to convert a Python object to a C long. Sets an exception
135 (struct.error for an inconvertible type, OverflowError for
136 out-of-range values) and returns -1 on error. */
Bob Ippolito232f3c92006-05-23 19:12:41 +0000137
138static int
139get_long(PyObject *v, long *p)
140{
Mark Dickinson463dc4b2009-07-05 10:01:24 +0000141 long x;
142
143 v = get_pylong(v);
144 if (v == NULL)
145 return -1;
146 assert(PyLong_Check(v));
147 x = PyLong_AsLong(v);
148 Py_DECREF(v);
Mark Dickinson5fd3af22009-07-07 15:08:28 +0000149 if (x == (long)-1 && PyErr_Occurred())
Bob Ippolito232f3c92006-05-23 19:12:41 +0000150 return -1;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000151 *p = x;
152 return 0;
153}
154
Bob Ippolito232f3c92006-05-23 19:12:41 +0000155/* Same, but handling unsigned long */
156
157static int
158get_ulong(PyObject *v, unsigned long *p)
159{
Mark Dickinson463dc4b2009-07-05 10:01:24 +0000160 unsigned long x;
161
162 v = get_pylong(v);
163 if (v == NULL)
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000164 return -1;
Mark Dickinson463dc4b2009-07-05 10:01:24 +0000165 assert(PyLong_Check(v));
166 x = PyLong_AsUnsignedLong(v);
167 Py_DECREF(v);
Mark Dickinson5fd3af22009-07-07 15:08:28 +0000168 if (x == (unsigned long)-1 && PyErr_Occurred())
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000169 return -1;
Mark Dickinson463dc4b2009-07-05 10:01:24 +0000170 *p = x;
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000171 return 0;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000172}
173
174#ifdef HAVE_LONG_LONG
175
176/* Same, but handling native long long. */
177
178static int
179get_longlong(PyObject *v, PY_LONG_LONG *p)
180{
181 PY_LONG_LONG x;
182
183 v = get_pylong(v);
184 if (v == NULL)
185 return -1;
186 assert(PyLong_Check(v));
187 x = PyLong_AsLongLong(v);
188 Py_DECREF(v);
Mark Dickinson5fd3af22009-07-07 15:08:28 +0000189 if (x == (PY_LONG_LONG)-1 && PyErr_Occurred())
Bob Ippolito232f3c92006-05-23 19:12:41 +0000190 return -1;
191 *p = x;
192 return 0;
193}
194
195/* Same, but handling native unsigned long long. */
196
197static int
198get_ulonglong(PyObject *v, unsigned PY_LONG_LONG *p)
199{
200 unsigned PY_LONG_LONG x;
201
202 v = get_pylong(v);
203 if (v == NULL)
204 return -1;
205 assert(PyLong_Check(v));
206 x = PyLong_AsUnsignedLongLong(v);
207 Py_DECREF(v);
Mark Dickinson5fd3af22009-07-07 15:08:28 +0000208 if (x == (unsigned PY_LONG_LONG)-1 && PyErr_Occurred())
Bob Ippolito232f3c92006-05-23 19:12:41 +0000209 return -1;
210 *p = x;
211 return 0;
212}
213
214#endif
215
Bob Ippolito2fd39772006-05-29 22:55:48 +0000216#define get_wrapped_long get_long
217#define get_wrapped_ulong get_ulong
Bob Ippolito2fd39772006-05-29 22:55:48 +0000218
Bob Ippolito232f3c92006-05-23 19:12:41 +0000219/* Floating point helpers */
220
221static PyObject *
222unpack_float(const char *p, /* start of 4-byte string */
223 int le) /* true for little-endian, false for big-endian */
224{
225 double x;
226
227 x = _PyFloat_Unpack4((unsigned char *)p, le);
228 if (x == -1.0 && PyErr_Occurred())
229 return NULL;
230 return PyFloat_FromDouble(x);
231}
232
233static PyObject *
234unpack_double(const char *p, /* start of 8-byte string */
235 int le) /* true for little-endian, false for big-endian */
236{
237 double x;
238
239 x = _PyFloat_Unpack8((unsigned char *)p, le);
240 if (x == -1.0 && PyErr_Occurred())
241 return NULL;
242 return PyFloat_FromDouble(x);
243}
244
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000245/* Helper to format the range error exceptions */
246static int
Armin Rigo162997e2006-05-29 17:59:47 +0000247_range_error(const formatdef *f, int is_unsigned)
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000248{
Tim Petersd6a6f022006-05-31 15:33:22 +0000249 /* ulargest is the largest unsigned value with f->size bytes.
250 * Note that the simpler:
251 * ((size_t)1 << (f->size * 8)) - 1
Tim Peters72270c22006-05-31 15:34:37 +0000252 * doesn't work when f->size == sizeof(size_t) because C doesn't
253 * define what happens when a left shift count is >= the number of
254 * bits in the integer being shifted; e.g., on some boxes it doesn't
255 * shift at all when they're equal.
Tim Petersd6a6f022006-05-31 15:33:22 +0000256 */
257 const size_t ulargest = (size_t)-1 >> ((SIZEOF_SIZE_T - f->size)*8);
258 assert(f->size >= 1 && f->size <= SIZEOF_SIZE_T);
259 if (is_unsigned)
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000260 PyErr_Format(StructError,
Neal Norwitz971ea112006-05-31 07:43:27 +0000261 "'%c' format requires 0 <= number <= %zu",
Bob Ippolito28b26862006-05-29 15:47:29 +0000262 f->format,
Tim Petersd6a6f022006-05-31 15:33:22 +0000263 ulargest);
264 else {
265 const Py_ssize_t largest = (Py_ssize_t)(ulargest >> 1);
266 PyErr_Format(StructError,
267 "'%c' format requires %zd <= number <= %zd",
268 f->format,
269 ~ largest,
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000270 largest);
271 }
272 return -1;
273}
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000274
275
Bob Ippolito232f3c92006-05-23 19:12:41 +0000276
277/* A large number of small routines follow, with names of the form
278
Bob Ippolitoaa70a172006-05-26 20:25:23 +0000279 [bln][up]_TYPE
Bob Ippolito232f3c92006-05-23 19:12:41 +0000280
281 [bln] distiguishes among big-endian, little-endian and native.
282 [pu] distiguishes between pack (to struct) and unpack (from struct).
283 TYPE is one of char, byte, ubyte, etc.
284*/
285
286/* Native mode routines. ****************************************************/
287/* NOTE:
288 In all n[up]_<type> routines handling types larger than 1 byte, there is
289 *no* guarantee that the p pointer is properly aligned for each type,
290 therefore memcpy is called. An intermediate variable is used to
291 compensate for big-endian architectures.
292 Normally both the intermediate variable and the memcpy call will be
293 skipped by C optimisation in little-endian architectures (gcc >= 2.91
294 does this). */
295
296static PyObject *
297nu_char(const char *p, const formatdef *f)
298{
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000299 return PyString_FromStringAndSize(p, 1);
Bob Ippolito232f3c92006-05-23 19:12:41 +0000300}
301
302static PyObject *
303nu_byte(const char *p, const formatdef *f)
304{
305 return PyInt_FromLong((long) *(signed char *)p);
306}
307
308static PyObject *
309nu_ubyte(const char *p, const formatdef *f)
310{
311 return PyInt_FromLong((long) *(unsigned char *)p);
312}
313
314static PyObject *
315nu_short(const char *p, const formatdef *f)
316{
317 short x;
318 memcpy((char *)&x, p, sizeof x);
319 return PyInt_FromLong((long)x);
320}
321
322static PyObject *
323nu_ushort(const char *p, const formatdef *f)
324{
325 unsigned short x;
326 memcpy((char *)&x, p, sizeof x);
327 return PyInt_FromLong((long)x);
328}
329
330static PyObject *
331nu_int(const char *p, const formatdef *f)
332{
333 int x;
334 memcpy((char *)&x, p, sizeof x);
335 return PyInt_FromLong((long)x);
336}
337
338static PyObject *
339nu_uint(const char *p, const formatdef *f)
340{
341 unsigned int x;
342 memcpy((char *)&x, p, sizeof x);
Bob Ippolitocd51ca52006-05-27 15:53:49 +0000343#if (SIZEOF_LONG > SIZEOF_INT)
344 return PyInt_FromLong((long)x);
345#else
346 if (x <= ((unsigned int)LONG_MAX))
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000347 return PyInt_FromLong((long)x);
Bob Ippolito232f3c92006-05-23 19:12:41 +0000348 return PyLong_FromUnsignedLong((unsigned long)x);
Bob Ippolitocd51ca52006-05-27 15:53:49 +0000349#endif
Bob Ippolito232f3c92006-05-23 19:12:41 +0000350}
351
352static PyObject *
353nu_long(const char *p, const formatdef *f)
354{
355 long x;
356 memcpy((char *)&x, p, sizeof x);
357 return PyInt_FromLong(x);
358}
359
360static PyObject *
361nu_ulong(const char *p, const formatdef *f)
362{
363 unsigned long x;
364 memcpy((char *)&x, p, sizeof x);
Bob Ippolito04ab9942006-05-25 19:33:38 +0000365 if (x <= LONG_MAX)
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000366 return PyInt_FromLong((long)x);
Bob Ippolito232f3c92006-05-23 19:12:41 +0000367 return PyLong_FromUnsignedLong(x);
368}
369
370/* Native mode doesn't support q or Q unless the platform C supports
371 long long (or, on Windows, __int64). */
372
373#ifdef HAVE_LONG_LONG
374
375static PyObject *
376nu_longlong(const char *p, const formatdef *f)
377{
378 PY_LONG_LONG x;
379 memcpy((char *)&x, p, sizeof x);
Bob Ippolito04ab9942006-05-25 19:33:38 +0000380 if (x >= LONG_MIN && x <= LONG_MAX)
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000381 return PyInt_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
Bob Ippolito232f3c92006-05-23 19:12:41 +0000382 return PyLong_FromLongLong(x);
383}
384
385static PyObject *
386nu_ulonglong(const char *p, const formatdef *f)
387{
388 unsigned PY_LONG_LONG x;
389 memcpy((char *)&x, p, sizeof x);
Bob Ippolito04ab9942006-05-25 19:33:38 +0000390 if (x <= LONG_MAX)
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000391 return PyInt_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
Bob Ippolito232f3c92006-05-23 19:12:41 +0000392 return PyLong_FromUnsignedLongLong(x);
393}
394
395#endif
396
397static PyObject *
Martin v. Löwisaef4c6b2007-01-21 09:33:07 +0000398nu_bool(const char *p, const formatdef *f)
399{
400 BOOL_TYPE x;
401 memcpy((char *)&x, p, sizeof x);
402 return PyBool_FromLong(x != 0);
403}
404
405
406static PyObject *
Bob Ippolito232f3c92006-05-23 19:12:41 +0000407nu_float(const char *p, const formatdef *f)
408{
409 float x;
410 memcpy((char *)&x, p, sizeof x);
411 return PyFloat_FromDouble((double)x);
412}
413
414static PyObject *
415nu_double(const char *p, const formatdef *f)
416{
417 double x;
418 memcpy((char *)&x, p, sizeof x);
419 return PyFloat_FromDouble(x);
420}
421
422static PyObject *
423nu_void_p(const char *p, const formatdef *f)
424{
425 void *x;
426 memcpy((char *)&x, p, sizeof x);
427 return PyLong_FromVoidPtr(x);
428}
429
430static int
431np_byte(char *p, PyObject *v, const formatdef *f)
432{
433 long x;
434 if (get_long(v, &x) < 0)
435 return -1;
436 if (x < -128 || x > 127){
437 PyErr_SetString(StructError,
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000438 "byte format requires -128 <= number <= 127");
Bob Ippolito232f3c92006-05-23 19:12:41 +0000439 return -1;
440 }
441 *p = (char)x;
442 return 0;
443}
444
445static int
446np_ubyte(char *p, PyObject *v, const formatdef *f)
447{
448 long x;
449 if (get_long(v, &x) < 0)
450 return -1;
451 if (x < 0 || x > 255){
452 PyErr_SetString(StructError,
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000453 "ubyte format requires 0 <= number <= 255");
Bob Ippolito232f3c92006-05-23 19:12:41 +0000454 return -1;
455 }
456 *p = (char)x;
457 return 0;
458}
459
460static int
461np_char(char *p, PyObject *v, const formatdef *f)
462{
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000463 if (!PyString_Check(v) || PyString_Size(v) != 1) {
Bob Ippolito232f3c92006-05-23 19:12:41 +0000464 PyErr_SetString(StructError,
465 "char format require string of length 1");
466 return -1;
467 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000468 *p = *PyString_AsString(v);
Bob Ippolito232f3c92006-05-23 19:12:41 +0000469 return 0;
470}
471
472static int
473np_short(char *p, PyObject *v, const formatdef *f)
474{
475 long x;
476 short y;
477 if (get_long(v, &x) < 0)
478 return -1;
479 if (x < SHRT_MIN || x > SHRT_MAX){
480 PyErr_SetString(StructError,
481 "short format requires " STRINGIFY(SHRT_MIN)
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000482 " <= number <= " STRINGIFY(SHRT_MAX));
Bob Ippolito232f3c92006-05-23 19:12:41 +0000483 return -1;
484 }
485 y = (short)x;
486 memcpy(p, (char *)&y, sizeof y);
487 return 0;
488}
489
490static int
491np_ushort(char *p, PyObject *v, const formatdef *f)
492{
493 long x;
494 unsigned short y;
495 if (get_long(v, &x) < 0)
496 return -1;
497 if (x < 0 || x > USHRT_MAX){
498 PyErr_SetString(StructError,
Mark Dickinson24766ba2009-07-07 10:18:22 +0000499 "ushort format requires 0 <= number <= " STRINGIFY(USHRT_MAX));
Bob Ippolito232f3c92006-05-23 19:12:41 +0000500 return -1;
501 }
502 y = (unsigned short)x;
503 memcpy(p, (char *)&y, sizeof y);
504 return 0;
505}
506
507static int
508np_int(char *p, PyObject *v, const formatdef *f)
509{
510 long x;
511 int y;
512 if (get_long(v, &x) < 0)
513 return -1;
Bob Ippolito90bd0a52006-05-27 11:47:12 +0000514#if (SIZEOF_LONG > SIZEOF_INT)
Bob Ippolitocd51ca52006-05-27 15:53:49 +0000515 if ((x < ((long)INT_MIN)) || (x > ((long)INT_MAX)))
Mark Dickinson5fd3af22009-07-07 15:08:28 +0000516 return _range_error(f, 0);
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000517#endif
Bob Ippolito232f3c92006-05-23 19:12:41 +0000518 y = (int)x;
519 memcpy(p, (char *)&y, sizeof y);
520 return 0;
521}
522
523static int
524np_uint(char *p, PyObject *v, const formatdef *f)
525{
526 unsigned long x;
527 unsigned int y;
Georg Brandl6269fec2009-01-01 12:15:31 +0000528 if (get_wrapped_ulong(v, &x) < 0)
529 return -1;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000530 y = (unsigned int)x;
Bob Ippolito90bd0a52006-05-27 11:47:12 +0000531#if (SIZEOF_LONG > SIZEOF_INT)
Bob Ippolitocd51ca52006-05-27 15:53:49 +0000532 if (x > ((unsigned long)UINT_MAX))
Mark Dickinson5fd3af22009-07-07 15:08:28 +0000533 return _range_error(f, 1);
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000534#endif
Bob Ippolito232f3c92006-05-23 19:12:41 +0000535 memcpy(p, (char *)&y, sizeof y);
536 return 0;
537}
538
539static int
540np_long(char *p, PyObject *v, const formatdef *f)
541{
542 long x;
543 if (get_long(v, &x) < 0)
544 return -1;
545 memcpy(p, (char *)&x, sizeof x);
546 return 0;
547}
548
549static int
550np_ulong(char *p, PyObject *v, const formatdef *f)
551{
552 unsigned long x;
Georg Brandl6269fec2009-01-01 12:15:31 +0000553 if (get_wrapped_ulong(v, &x) < 0)
554 return -1;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000555 memcpy(p, (char *)&x, sizeof x);
556 return 0;
557}
558
559#ifdef HAVE_LONG_LONG
560
561static int
562np_longlong(char *p, PyObject *v, const formatdef *f)
563{
564 PY_LONG_LONG x;
565 if (get_longlong(v, &x) < 0)
566 return -1;
567 memcpy(p, (char *)&x, sizeof x);
568 return 0;
569}
570
571static int
572np_ulonglong(char *p, PyObject *v, const formatdef *f)
573{
574 unsigned PY_LONG_LONG x;
575 if (get_ulonglong(v, &x) < 0)
576 return -1;
577 memcpy(p, (char *)&x, sizeof x);
578 return 0;
579}
580#endif
581
Martin v. Löwisaef4c6b2007-01-21 09:33:07 +0000582
583static int
584np_bool(char *p, PyObject *v, const formatdef *f)
585{
586 BOOL_TYPE y;
587 y = PyObject_IsTrue(v);
588 memcpy(p, (char *)&y, sizeof y);
589 return 0;
590}
591
Bob Ippolito232f3c92006-05-23 19:12:41 +0000592static int
593np_float(char *p, PyObject *v, const formatdef *f)
594{
595 float x = (float)PyFloat_AsDouble(v);
596 if (x == -1 && PyErr_Occurred()) {
597 PyErr_SetString(StructError,
598 "required argument is not a float");
599 return -1;
600 }
601 memcpy(p, (char *)&x, sizeof x);
602 return 0;
603}
604
605static int
606np_double(char *p, PyObject *v, const formatdef *f)
607{
608 double x = PyFloat_AsDouble(v);
609 if (x == -1 && PyErr_Occurred()) {
610 PyErr_SetString(StructError,
611 "required argument is not a float");
612 return -1;
613 }
614 memcpy(p, (char *)&x, sizeof(double));
615 return 0;
616}
617
618static int
619np_void_p(char *p, PyObject *v, const formatdef *f)
620{
621 void *x;
622
623 v = get_pylong(v);
624 if (v == NULL)
625 return -1;
626 assert(PyLong_Check(v));
627 x = PyLong_AsVoidPtr(v);
628 Py_DECREF(v);
629 if (x == NULL && PyErr_Occurred())
630 return -1;
631 memcpy(p, (char *)&x, sizeof x);
632 return 0;
633}
634
635static formatdef native_table[] = {
636 {'x', sizeof(char), 0, NULL},
637 {'b', sizeof(char), 0, nu_byte, np_byte},
638 {'B', sizeof(char), 0, nu_ubyte, np_ubyte},
639 {'c', sizeof(char), 0, nu_char, np_char},
640 {'s', sizeof(char), 0, NULL},
641 {'p', sizeof(char), 0, NULL},
642 {'h', sizeof(short), SHORT_ALIGN, nu_short, np_short},
643 {'H', sizeof(short), SHORT_ALIGN, nu_ushort, np_ushort},
644 {'i', sizeof(int), INT_ALIGN, nu_int, np_int},
645 {'I', sizeof(int), INT_ALIGN, nu_uint, np_uint},
646 {'l', sizeof(long), LONG_ALIGN, nu_long, np_long},
647 {'L', sizeof(long), LONG_ALIGN, nu_ulong, np_ulong},
Bob Ippolito232f3c92006-05-23 19:12:41 +0000648#ifdef HAVE_LONG_LONG
649 {'q', sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_longlong, np_longlong},
650 {'Q', sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_ulonglong,np_ulonglong},
651#endif
Thomas Hellerf3c05592008-03-05 15:34:29 +0000652 {'?', sizeof(BOOL_TYPE), BOOL_ALIGN, nu_bool, np_bool},
Bob Ippolitoa99865b2006-05-25 19:56:56 +0000653 {'f', sizeof(float), FLOAT_ALIGN, nu_float, np_float},
654 {'d', sizeof(double), DOUBLE_ALIGN, nu_double, np_double},
655 {'P', sizeof(void *), VOID_P_ALIGN, nu_void_p, np_void_p},
Bob Ippolito232f3c92006-05-23 19:12:41 +0000656 {0}
657};
658
659/* Big-endian routines. *****************************************************/
660
661static PyObject *
662bu_int(const char *p, const formatdef *f)
663{
664 long x = 0;
Bob Ippolitoaa70a172006-05-26 20:25:23 +0000665 Py_ssize_t i = f->size;
Bob Ippolito28b26862006-05-29 15:47:29 +0000666 const unsigned char *bytes = (const unsigned char *)p;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000667 do {
Bob Ippolito28b26862006-05-29 15:47:29 +0000668 x = (x<<8) | *bytes++;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000669 } while (--i > 0);
670 /* Extend the sign bit. */
671 if (SIZEOF_LONG > f->size)
Bob Ippolitocd51ca52006-05-27 15:53:49 +0000672 x |= -(x & (1L << ((8 * f->size) - 1)));
Bob Ippolito232f3c92006-05-23 19:12:41 +0000673 return PyInt_FromLong(x);
674}
675
676static PyObject *
677bu_uint(const char *p, const formatdef *f)
678{
679 unsigned long x = 0;
Bob Ippolitoaa70a172006-05-26 20:25:23 +0000680 Py_ssize_t i = f->size;
Bob Ippolito28b26862006-05-29 15:47:29 +0000681 const unsigned char *bytes = (const unsigned char *)p;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000682 do {
Bob Ippolito28b26862006-05-29 15:47:29 +0000683 x = (x<<8) | *bytes++;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000684 } while (--i > 0);
Bob Ippolito04ab9942006-05-25 19:33:38 +0000685 if (x <= LONG_MAX)
Bob Ippolito232f3c92006-05-23 19:12:41 +0000686 return PyInt_FromLong((long)x);
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000687 return PyLong_FromUnsignedLong(x);
Bob Ippolito232f3c92006-05-23 19:12:41 +0000688}
689
690static PyObject *
691bu_longlong(const char *p, const formatdef *f)
692{
Bob Ippolito90bd0a52006-05-27 11:47:12 +0000693#ifdef HAVE_LONG_LONG
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000694 PY_LONG_LONG x = 0;
Bob Ippolitoaa70a172006-05-26 20:25:23 +0000695 Py_ssize_t i = f->size;
Bob Ippolito28b26862006-05-29 15:47:29 +0000696 const unsigned char *bytes = (const unsigned char *)p;
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000697 do {
Bob Ippolito28b26862006-05-29 15:47:29 +0000698 x = (x<<8) | *bytes++;
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000699 } while (--i > 0);
700 /* Extend the sign bit. */
701 if (SIZEOF_LONG_LONG > f->size)
Kristján Valur Jónsson67387fb2007-04-25 00:17:39 +0000702 x |= -(x & ((PY_LONG_LONG)1 << ((8 * f->size) - 1)));
Bob Ippolito04ab9942006-05-25 19:33:38 +0000703 if (x >= LONG_MIN && x <= LONG_MAX)
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000704 return PyInt_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000705 return PyLong_FromLongLong(x);
706#else
Bob Ippolito232f3c92006-05-23 19:12:41 +0000707 return _PyLong_FromByteArray((const unsigned char *)p,
708 8,
709 0, /* little-endian */
710 1 /* signed */);
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000711#endif
Bob Ippolito232f3c92006-05-23 19:12:41 +0000712}
713
714static PyObject *
715bu_ulonglong(const char *p, const formatdef *f)
716{
Bob Ippolito90bd0a52006-05-27 11:47:12 +0000717#ifdef HAVE_LONG_LONG
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000718 unsigned PY_LONG_LONG x = 0;
Bob Ippolitoaa70a172006-05-26 20:25:23 +0000719 Py_ssize_t i = f->size;
Bob Ippolito28b26862006-05-29 15:47:29 +0000720 const unsigned char *bytes = (const unsigned char *)p;
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000721 do {
Bob Ippolito28b26862006-05-29 15:47:29 +0000722 x = (x<<8) | *bytes++;
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000723 } while (--i > 0);
Bob Ippolito04ab9942006-05-25 19:33:38 +0000724 if (x <= LONG_MAX)
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000725 return PyInt_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000726 return PyLong_FromUnsignedLongLong(x);
727#else
Bob Ippolito232f3c92006-05-23 19:12:41 +0000728 return _PyLong_FromByteArray((const unsigned char *)p,
729 8,
730 0, /* little-endian */
731 0 /* signed */);
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000732#endif
Bob Ippolito232f3c92006-05-23 19:12:41 +0000733}
734
735static PyObject *
736bu_float(const char *p, const formatdef *f)
737{
738 return unpack_float(p, 0);
739}
740
741static PyObject *
742bu_double(const char *p, const formatdef *f)
743{
744 return unpack_double(p, 0);
745}
746
Martin v. Löwisaef4c6b2007-01-21 09:33:07 +0000747static PyObject *
748bu_bool(const char *p, const formatdef *f)
749{
750 char x;
751 memcpy((char *)&x, p, sizeof x);
752 return PyBool_FromLong(x != 0);
753}
754
Bob Ippolito232f3c92006-05-23 19:12:41 +0000755static int
756bp_int(char *p, PyObject *v, const formatdef *f)
757{
758 long x;
Bob Ippolitoaa70a172006-05-26 20:25:23 +0000759 Py_ssize_t i;
Bob Ippolito2fd39772006-05-29 22:55:48 +0000760 if (get_wrapped_long(v, &x) < 0)
Bob Ippolito232f3c92006-05-23 19:12:41 +0000761 return -1;
762 i = f->size;
Bob Ippolitocd51ca52006-05-27 15:53:49 +0000763 if (i != SIZEOF_LONG) {
764 if ((i == 2) && (x < -32768 || x > 32767))
Mark Dickinson5fd3af22009-07-07 15:08:28 +0000765 return _range_error(f, 0);
Bob Ippolito90bd0a52006-05-27 11:47:12 +0000766#if (SIZEOF_LONG != 4)
Bob Ippolitocd51ca52006-05-27 15:53:49 +0000767 else if ((i == 4) && (x < -2147483648L || x > 2147483647L))
Mark Dickinson5fd3af22009-07-07 15:08:28 +0000768 return _range_error(f, 0);
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000769#endif
Bob Ippolitocd51ca52006-05-27 15:53:49 +0000770 }
Bob Ippolito232f3c92006-05-23 19:12:41 +0000771 do {
772 p[--i] = (char)x;
773 x >>= 8;
774 } while (i > 0);
775 return 0;
776}
777
778static int
779bp_uint(char *p, PyObject *v, const formatdef *f)
780{
781 unsigned long x;
Bob Ippolitoaa70a172006-05-26 20:25:23 +0000782 Py_ssize_t i;
Bob Ippolito2fd39772006-05-29 22:55:48 +0000783 if (get_wrapped_ulong(v, &x) < 0)
Bob Ippolito232f3c92006-05-23 19:12:41 +0000784 return -1;
785 i = f->size;
Bob Ippolitocd51ca52006-05-27 15:53:49 +0000786 if (i != SIZEOF_LONG) {
787 unsigned long maxint = 1;
788 maxint <<= (unsigned long)(i * 8);
789 if (x >= maxint)
Mark Dickinson5fd3af22009-07-07 15:08:28 +0000790 return _range_error(f, 1);
Bob Ippolitocd51ca52006-05-27 15:53:49 +0000791 }
Bob Ippolito232f3c92006-05-23 19:12:41 +0000792 do {
793 p[--i] = (char)x;
794 x >>= 8;
795 } while (i > 0);
796 return 0;
797}
798
799static int
800bp_longlong(char *p, PyObject *v, const formatdef *f)
801{
802 int res;
803 v = get_pylong(v);
804 if (v == NULL)
805 return -1;
806 res = _PyLong_AsByteArray((PyLongObject *)v,
807 (unsigned char *)p,
808 8,
809 0, /* little_endian */
810 1 /* signed */);
811 Py_DECREF(v);
812 return res;
813}
814
815static int
816bp_ulonglong(char *p, PyObject *v, const formatdef *f)
817{
818 int res;
819 v = get_pylong(v);
820 if (v == NULL)
821 return -1;
822 res = _PyLong_AsByteArray((PyLongObject *)v,
823 (unsigned char *)p,
824 8,
825 0, /* little_endian */
826 0 /* signed */);
827 Py_DECREF(v);
828 return res;
829}
830
831static int
832bp_float(char *p, PyObject *v, const formatdef *f)
833{
834 double x = PyFloat_AsDouble(v);
835 if (x == -1 && PyErr_Occurred()) {
836 PyErr_SetString(StructError,
837 "required argument is not a float");
838 return -1;
839 }
840 return _PyFloat_Pack4(x, (unsigned char *)p, 0);
841}
842
843static int
844bp_double(char *p, PyObject *v, const formatdef *f)
845{
846 double x = PyFloat_AsDouble(v);
847 if (x == -1 && PyErr_Occurred()) {
848 PyErr_SetString(StructError,
849 "required argument is not a float");
850 return -1;
851 }
852 return _PyFloat_Pack8(x, (unsigned char *)p, 0);
853}
854
Martin v. Löwisaef4c6b2007-01-21 09:33:07 +0000855static int
856bp_bool(char *p, PyObject *v, const formatdef *f)
857{
858 char y;
859 y = PyObject_IsTrue(v);
860 memcpy(p, (char *)&y, sizeof y);
861 return 0;
862}
863
Bob Ippolito232f3c92006-05-23 19:12:41 +0000864static formatdef bigendian_table[] = {
865 {'x', 1, 0, NULL},
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000866 {'b', 1, 0, nu_byte, np_byte},
867 {'B', 1, 0, nu_ubyte, np_ubyte},
Bob Ippolito232f3c92006-05-23 19:12:41 +0000868 {'c', 1, 0, nu_char, np_char},
869 {'s', 1, 0, NULL},
870 {'p', 1, 0, NULL},
871 {'h', 2, 0, bu_int, bp_int},
872 {'H', 2, 0, bu_uint, bp_uint},
873 {'i', 4, 0, bu_int, bp_int},
874 {'I', 4, 0, bu_uint, bp_uint},
875 {'l', 4, 0, bu_int, bp_int},
876 {'L', 4, 0, bu_uint, bp_uint},
877 {'q', 8, 0, bu_longlong, bp_longlong},
878 {'Q', 8, 0, bu_ulonglong, bp_ulonglong},
Thomas Hellerf3c05592008-03-05 15:34:29 +0000879 {'?', 1, 0, bu_bool, bp_bool},
Bob Ippolito232f3c92006-05-23 19:12:41 +0000880 {'f', 4, 0, bu_float, bp_float},
881 {'d', 8, 0, bu_double, bp_double},
882 {0}
883};
884
885/* Little-endian routines. *****************************************************/
886
887static PyObject *
888lu_int(const char *p, const formatdef *f)
889{
890 long x = 0;
Bob Ippolitoaa70a172006-05-26 20:25:23 +0000891 Py_ssize_t i = f->size;
Bob Ippolito28b26862006-05-29 15:47:29 +0000892 const unsigned char *bytes = (const unsigned char *)p;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000893 do {
Bob Ippolito28b26862006-05-29 15:47:29 +0000894 x = (x<<8) | bytes[--i];
Bob Ippolito232f3c92006-05-23 19:12:41 +0000895 } while (i > 0);
896 /* Extend the sign bit. */
897 if (SIZEOF_LONG > f->size)
Bob Ippolitocd51ca52006-05-27 15:53:49 +0000898 x |= -(x & (1L << ((8 * f->size) - 1)));
Bob Ippolito232f3c92006-05-23 19:12:41 +0000899 return PyInt_FromLong(x);
900}
901
902static PyObject *
903lu_uint(const char *p, const formatdef *f)
904{
905 unsigned long x = 0;
Bob Ippolitoaa70a172006-05-26 20:25:23 +0000906 Py_ssize_t i = f->size;
Bob Ippolito28b26862006-05-29 15:47:29 +0000907 const unsigned char *bytes = (const unsigned char *)p;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000908 do {
Bob Ippolito28b26862006-05-29 15:47:29 +0000909 x = (x<<8) | bytes[--i];
Bob Ippolito232f3c92006-05-23 19:12:41 +0000910 } while (i > 0);
Bob Ippolito04ab9942006-05-25 19:33:38 +0000911 if (x <= LONG_MAX)
Bob Ippolito232f3c92006-05-23 19:12:41 +0000912 return PyInt_FromLong((long)x);
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000913 return PyLong_FromUnsignedLong((long)x);
Bob Ippolito232f3c92006-05-23 19:12:41 +0000914}
915
916static PyObject *
917lu_longlong(const char *p, const formatdef *f)
918{
Bob Ippolito90bd0a52006-05-27 11:47:12 +0000919#ifdef HAVE_LONG_LONG
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000920 PY_LONG_LONG x = 0;
Bob Ippolitoaa70a172006-05-26 20:25:23 +0000921 Py_ssize_t i = f->size;
Bob Ippolito28b26862006-05-29 15:47:29 +0000922 const unsigned char *bytes = (const unsigned char *)p;
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000923 do {
Bob Ippolito28b26862006-05-29 15:47:29 +0000924 x = (x<<8) | bytes[--i];
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000925 } while (i > 0);
926 /* Extend the sign bit. */
927 if (SIZEOF_LONG_LONG > f->size)
Kristján Valur Jónsson67387fb2007-04-25 00:17:39 +0000928 x |= -(x & ((PY_LONG_LONG)1 << ((8 * f->size) - 1)));
Bob Ippolito04ab9942006-05-25 19:33:38 +0000929 if (x >= LONG_MIN && x <= LONG_MAX)
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000930 return PyInt_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000931 return PyLong_FromLongLong(x);
932#else
Bob Ippolito232f3c92006-05-23 19:12:41 +0000933 return _PyLong_FromByteArray((const unsigned char *)p,
934 8,
935 1, /* little-endian */
936 1 /* signed */);
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000937#endif
Bob Ippolito232f3c92006-05-23 19:12:41 +0000938}
939
940static PyObject *
941lu_ulonglong(const char *p, const formatdef *f)
942{
Bob Ippolito90bd0a52006-05-27 11:47:12 +0000943#ifdef HAVE_LONG_LONG
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000944 unsigned PY_LONG_LONG x = 0;
Bob Ippolitoaa70a172006-05-26 20:25:23 +0000945 Py_ssize_t i = f->size;
Bob Ippolito28b26862006-05-29 15:47:29 +0000946 const unsigned char *bytes = (const unsigned char *)p;
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000947 do {
Bob Ippolito28b26862006-05-29 15:47:29 +0000948 x = (x<<8) | bytes[--i];
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000949 } while (i > 0);
Bob Ippolito04ab9942006-05-25 19:33:38 +0000950 if (x <= LONG_MAX)
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000951 return PyInt_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000952 return PyLong_FromUnsignedLongLong(x);
953#else
Bob Ippolito232f3c92006-05-23 19:12:41 +0000954 return _PyLong_FromByteArray((const unsigned char *)p,
955 8,
956 1, /* little-endian */
957 0 /* signed */);
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000958#endif
Bob Ippolito232f3c92006-05-23 19:12:41 +0000959}
960
961static PyObject *
962lu_float(const char *p, const formatdef *f)
963{
964 return unpack_float(p, 1);
965}
966
967static PyObject *
968lu_double(const char *p, const formatdef *f)
969{
970 return unpack_double(p, 1);
971}
972
973static int
974lp_int(char *p, PyObject *v, const formatdef *f)
975{
976 long x;
Bob Ippolitoaa70a172006-05-26 20:25:23 +0000977 Py_ssize_t i;
Bob Ippolito2fd39772006-05-29 22:55:48 +0000978 if (get_wrapped_long(v, &x) < 0)
Bob Ippolito232f3c92006-05-23 19:12:41 +0000979 return -1;
980 i = f->size;
Bob Ippolitocd51ca52006-05-27 15:53:49 +0000981 if (i != SIZEOF_LONG) {
982 if ((i == 2) && (x < -32768 || x > 32767))
Mark Dickinson5fd3af22009-07-07 15:08:28 +0000983 return _range_error(f, 0);
Bob Ippolito90bd0a52006-05-27 11:47:12 +0000984#if (SIZEOF_LONG != 4)
Bob Ippolitocd51ca52006-05-27 15:53:49 +0000985 else if ((i == 4) && (x < -2147483648L || x > 2147483647L))
Mark Dickinson5fd3af22009-07-07 15:08:28 +0000986 return _range_error(f, 0);
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000987#endif
Bob Ippolitocd51ca52006-05-27 15:53:49 +0000988 }
Bob Ippolito232f3c92006-05-23 19:12:41 +0000989 do {
990 *p++ = (char)x;
991 x >>= 8;
992 } while (--i > 0);
993 return 0;
994}
995
996static int
997lp_uint(char *p, PyObject *v, const formatdef *f)
998{
999 unsigned long x;
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001000 Py_ssize_t i;
Bob Ippolito2fd39772006-05-29 22:55:48 +00001001 if (get_wrapped_ulong(v, &x) < 0)
Bob Ippolito232f3c92006-05-23 19:12:41 +00001002 return -1;
1003 i = f->size;
Bob Ippolitocd51ca52006-05-27 15:53:49 +00001004 if (i != SIZEOF_LONG) {
1005 unsigned long maxint = 1;
1006 maxint <<= (unsigned long)(i * 8);
1007 if (x >= maxint)
Mark Dickinson5fd3af22009-07-07 15:08:28 +00001008 return _range_error(f, 1);
Bob Ippolitocd51ca52006-05-27 15:53:49 +00001009 }
Bob Ippolito232f3c92006-05-23 19:12:41 +00001010 do {
1011 *p++ = (char)x;
1012 x >>= 8;
1013 } while (--i > 0);
1014 return 0;
1015}
1016
1017static int
1018lp_longlong(char *p, PyObject *v, const formatdef *f)
1019{
1020 int res;
1021 v = get_pylong(v);
1022 if (v == NULL)
1023 return -1;
1024 res = _PyLong_AsByteArray((PyLongObject*)v,
1025 (unsigned char *)p,
1026 8,
1027 1, /* little_endian */
1028 1 /* signed */);
1029 Py_DECREF(v);
1030 return res;
1031}
1032
1033static int
1034lp_ulonglong(char *p, PyObject *v, const formatdef *f)
1035{
1036 int res;
1037 v = get_pylong(v);
1038 if (v == NULL)
1039 return -1;
1040 res = _PyLong_AsByteArray((PyLongObject*)v,
1041 (unsigned char *)p,
1042 8,
1043 1, /* little_endian */
1044 0 /* signed */);
1045 Py_DECREF(v);
1046 return res;
1047}
1048
1049static int
1050lp_float(char *p, PyObject *v, const formatdef *f)
1051{
1052 double x = PyFloat_AsDouble(v);
1053 if (x == -1 && PyErr_Occurred()) {
1054 PyErr_SetString(StructError,
1055 "required argument is not a float");
1056 return -1;
1057 }
1058 return _PyFloat_Pack4(x, (unsigned char *)p, 1);
1059}
1060
1061static int
1062lp_double(char *p, PyObject *v, const formatdef *f)
1063{
1064 double x = PyFloat_AsDouble(v);
1065 if (x == -1 && PyErr_Occurred()) {
1066 PyErr_SetString(StructError,
1067 "required argument is not a float");
1068 return -1;
1069 }
1070 return _PyFloat_Pack8(x, (unsigned char *)p, 1);
1071}
1072
1073static formatdef lilendian_table[] = {
1074 {'x', 1, 0, NULL},
Bob Ippolitoe27337b2006-05-26 13:15:44 +00001075 {'b', 1, 0, nu_byte, np_byte},
1076 {'B', 1, 0, nu_ubyte, np_ubyte},
Bob Ippolito232f3c92006-05-23 19:12:41 +00001077 {'c', 1, 0, nu_char, np_char},
1078 {'s', 1, 0, NULL},
1079 {'p', 1, 0, NULL},
1080 {'h', 2, 0, lu_int, lp_int},
1081 {'H', 2, 0, lu_uint, lp_uint},
1082 {'i', 4, 0, lu_int, lp_int},
1083 {'I', 4, 0, lu_uint, lp_uint},
1084 {'l', 4, 0, lu_int, lp_int},
1085 {'L', 4, 0, lu_uint, lp_uint},
1086 {'q', 8, 0, lu_longlong, lp_longlong},
1087 {'Q', 8, 0, lu_ulonglong, lp_ulonglong},
Thomas Hellerf3c05592008-03-05 15:34:29 +00001088 {'?', 1, 0, bu_bool, bp_bool}, /* Std rep not endian dep,
Martin v. Löwisaef4c6b2007-01-21 09:33:07 +00001089 but potentially different from native rep -- reuse bx_bool funcs. */
Bob Ippolito232f3c92006-05-23 19:12:41 +00001090 {'f', 4, 0, lu_float, lp_float},
1091 {'d', 8, 0, lu_double, lp_double},
1092 {0}
1093};
1094
1095
1096static const formatdef *
1097whichtable(char **pfmt)
1098{
1099 const char *fmt = (*pfmt)++; /* May be backed out of later */
1100 switch (*fmt) {
1101 case '<':
1102 return lilendian_table;
1103 case '>':
1104 case '!': /* Network byte order is big-endian */
1105 return bigendian_table;
1106 case '=': { /* Host byte order -- different from native in aligment! */
1107 int n = 1;
1108 char *p = (char *) &n;
1109 if (*p == 1)
1110 return lilendian_table;
1111 else
1112 return bigendian_table;
1113 }
1114 default:
1115 --*pfmt; /* Back out of pointer increment */
1116 /* Fall through */
1117 case '@':
1118 return native_table;
1119 }
1120}
1121
1122
1123/* Get the table entry for a format code */
1124
1125static const formatdef *
1126getentry(int c, const formatdef *f)
1127{
1128 for (; f->format != '\0'; f++) {
1129 if (f->format == c) {
1130 return f;
1131 }
1132 }
1133 PyErr_SetString(StructError, "bad char in struct format");
1134 return NULL;
1135}
1136
1137
1138/* Align a size according to a format code */
1139
1140static int
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001141align(Py_ssize_t size, char c, const formatdef *e)
Bob Ippolito232f3c92006-05-23 19:12:41 +00001142{
1143 if (e->format == c) {
1144 if (e->alignment) {
1145 size = ((size + e->alignment - 1)
1146 / e->alignment)
1147 * e->alignment;
1148 }
1149 }
1150 return size;
1151}
1152
1153
1154/* calculate the size of a format string */
1155
1156static int
1157prepare_s(PyStructObject *self)
1158{
1159 const formatdef *f;
1160 const formatdef *e;
1161 formatcode *codes;
Tim Petersc2b550e2006-05-31 14:28:07 +00001162
Bob Ippolito232f3c92006-05-23 19:12:41 +00001163 const char *s;
1164 const char *fmt;
1165 char c;
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001166 Py_ssize_t size, len, num, itemsize, x;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001167
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001168 fmt = PyString_AS_STRING(self->s_format);
Bob Ippolito232f3c92006-05-23 19:12:41 +00001169
1170 f = whichtable((char **)&fmt);
Tim Petersc2b550e2006-05-31 14:28:07 +00001171
Bob Ippolito232f3c92006-05-23 19:12:41 +00001172 s = fmt;
1173 size = 0;
1174 len = 0;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001175 while ((c = *s++) != '\0') {
1176 if (isspace(Py_CHARMASK(c)))
1177 continue;
1178 if ('0' <= c && c <= '9') {
1179 num = c - '0';
1180 while ('0' <= (c = *s++) && c <= '9') {
1181 x = num*10 + (c - '0');
1182 if (x/10 != num) {
1183 PyErr_SetString(
1184 StructError,
1185 "overflow in item count");
1186 return -1;
1187 }
1188 num = x;
1189 }
1190 if (c == '\0')
1191 break;
1192 }
1193 else
1194 num = 1;
1195
1196 e = getentry(c, f);
1197 if (e == NULL)
1198 return -1;
Tim Petersc2b550e2006-05-31 14:28:07 +00001199
Bob Ippolito232f3c92006-05-23 19:12:41 +00001200 switch (c) {
1201 case 's': /* fall through */
1202 case 'p': len++; break;
1203 case 'x': break;
1204 default: len += num; break;
1205 }
Bob Ippolito232f3c92006-05-23 19:12:41 +00001206
1207 itemsize = e->size;
1208 size = align(size, c, e);
1209 x = num * itemsize;
1210 size += x;
1211 if (x/itemsize != num || size < 0) {
1212 PyErr_SetString(StructError,
1213 "total struct size too long");
1214 return -1;
1215 }
1216 }
1217
Gregory P. Smith9d534572008-06-11 07:41:16 +00001218 /* check for overflow */
1219 if ((len + 1) > (PY_SSIZE_T_MAX / sizeof(formatcode))) {
1220 PyErr_NoMemory();
1221 return -1;
1222 }
1223
Bob Ippolito232f3c92006-05-23 19:12:41 +00001224 self->s_size = size;
1225 self->s_len = len;
Bob Ippolitoeb621272006-05-24 15:32:06 +00001226 codes = PyMem_MALLOC((len + 1) * sizeof(formatcode));
Bob Ippolito232f3c92006-05-23 19:12:41 +00001227 if (codes == NULL) {
1228 PyErr_NoMemory();
1229 return -1;
1230 }
1231 self->s_codes = codes;
Tim Petersc2b550e2006-05-31 14:28:07 +00001232
Bob Ippolito232f3c92006-05-23 19:12:41 +00001233 s = fmt;
1234 size = 0;
1235 while ((c = *s++) != '\0') {
1236 if (isspace(Py_CHARMASK(c)))
1237 continue;
1238 if ('0' <= c && c <= '9') {
1239 num = c - '0';
1240 while ('0' <= (c = *s++) && c <= '9')
1241 num = num*10 + (c - '0');
1242 if (c == '\0')
1243 break;
1244 }
1245 else
1246 num = 1;
1247
1248 e = getentry(c, f);
Tim Petersc2b550e2006-05-31 14:28:07 +00001249
Bob Ippolito232f3c92006-05-23 19:12:41 +00001250 size = align(size, c, e);
Bob Ippolitoeb621272006-05-24 15:32:06 +00001251 if (c == 's' || c == 'p') {
Bob Ippolito232f3c92006-05-23 19:12:41 +00001252 codes->offset = size;
Bob Ippolitoeb621272006-05-24 15:32:06 +00001253 codes->size = num;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001254 codes->fmtdef = e;
1255 codes++;
Bob Ippolitoeb621272006-05-24 15:32:06 +00001256 size += num;
1257 } else if (c == 'x') {
1258 size += num;
1259 } else {
1260 while (--num >= 0) {
1261 codes->offset = size;
1262 codes->size = e->size;
1263 codes->fmtdef = e;
1264 codes++;
1265 size += e->size;
1266 }
Bob Ippolito232f3c92006-05-23 19:12:41 +00001267 }
Bob Ippolito232f3c92006-05-23 19:12:41 +00001268 }
1269 codes->fmtdef = NULL;
Bob Ippolitoeb621272006-05-24 15:32:06 +00001270 codes->offset = size;
1271 codes->size = 0;
Tim Petersc2b550e2006-05-31 14:28:07 +00001272
Bob Ippolito232f3c92006-05-23 19:12:41 +00001273 return 0;
1274}
1275
1276static PyObject *
1277s_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1278{
1279 PyObject *self;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001280
1281 assert(type != NULL && type->tp_alloc != NULL);
1282
1283 self = type->tp_alloc(type, 0);
1284 if (self != NULL) {
1285 PyStructObject *s = (PyStructObject*)self;
1286 Py_INCREF(Py_None);
1287 s->s_format = Py_None;
1288 s->s_codes = NULL;
1289 s->s_size = -1;
1290 s->s_len = -1;
1291 }
1292 return self;
1293}
1294
1295static int
1296s_init(PyObject *self, PyObject *args, PyObject *kwds)
1297{
1298 PyStructObject *soself = (PyStructObject *)self;
1299 PyObject *o_format = NULL;
1300 int ret = 0;
1301 static char *kwlist[] = {"format", 0};
1302
1303 assert(PyStruct_Check(self));
1304
1305 if (!PyArg_ParseTupleAndKeywords(args, kwds, "S:Struct", kwlist,
1306 &o_format))
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001307 return -1;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001308
1309 Py_INCREF(o_format);
Amaury Forgeot d'Arc588ff932008-02-16 14:34:57 +00001310 Py_CLEAR(soself->s_format);
Bob Ippolito232f3c92006-05-23 19:12:41 +00001311 soself->s_format = o_format;
Tim Petersc2b550e2006-05-31 14:28:07 +00001312
Bob Ippolito232f3c92006-05-23 19:12:41 +00001313 ret = prepare_s(soself);
1314 return ret;
1315}
1316
1317static void
1318s_dealloc(PyStructObject *s)
1319{
Bob Ippolito232f3c92006-05-23 19:12:41 +00001320 if (s->weakreflist != NULL)
1321 PyObject_ClearWeakRefs((PyObject *)s);
1322 if (s->s_codes != NULL) {
1323 PyMem_FREE(s->s_codes);
1324 }
1325 Py_XDECREF(s->s_format);
Christian Heimese93237d2007-12-19 02:37:44 +00001326 Py_TYPE(s)->tp_free((PyObject *)s);
Bob Ippolito232f3c92006-05-23 19:12:41 +00001327}
1328
Bob Ippolitoeb621272006-05-24 15:32:06 +00001329static PyObject *
1330s_unpack_internal(PyStructObject *soself, char *startfrom) {
1331 formatcode *code;
1332 Py_ssize_t i = 0;
1333 PyObject *result = PyTuple_New(soself->s_len);
1334 if (result == NULL)
1335 return NULL;
1336
1337 for (code = soself->s_codes; code->fmtdef != NULL; code++) {
1338 PyObject *v;
1339 const formatdef *e = code->fmtdef;
1340 const char *res = startfrom + code->offset;
1341 if (e->format == 's') {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001342 v = PyString_FromStringAndSize(res, code->size);
Bob Ippolitoeb621272006-05-24 15:32:06 +00001343 } else if (e->format == 'p') {
1344 Py_ssize_t n = *(unsigned char*)res;
1345 if (n >= code->size)
1346 n = code->size - 1;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001347 v = PyString_FromStringAndSize(res + 1, n);
Bob Ippolitoeb621272006-05-24 15:32:06 +00001348 } else {
1349 v = e->unpack(res, e);
Bob Ippolitoeb621272006-05-24 15:32:06 +00001350 }
Neal Norwitz3c5431e2006-06-11 05:45:25 +00001351 if (v == NULL)
1352 goto fail;
1353 PyTuple_SET_ITEM(result, i++, v);
Bob Ippolitoeb621272006-05-24 15:32:06 +00001354 }
1355
1356 return result;
1357fail:
1358 Py_DECREF(result);
1359 return NULL;
Bob Ippolitocd51ca52006-05-27 15:53:49 +00001360}
Bob Ippolitoeb621272006-05-24 15:32:06 +00001361
1362
Bob Ippolito232f3c92006-05-23 19:12:41 +00001363PyDoc_STRVAR(s_unpack__doc__,
Bob Ippolito1fcdc232006-05-27 12:11:36 +00001364"S.unpack(str) -> (v1, v2, ...)\n\
Bob Ippolito232f3c92006-05-23 19:12:41 +00001365\n\
1366Return tuple containing values unpacked according to this Struct's format.\n\
1367Requires len(str) == self.size. See struct.__doc__ for more on format\n\
1368strings.");
1369
1370static PyObject *
1371s_unpack(PyObject *self, PyObject *inputstr)
1372{
Raymond Hettinger7a3d41f2007-04-05 18:00:03 +00001373 char *start;
1374 Py_ssize_t len;
1375 PyObject *args=NULL, *result;
Bob Ippolitoeb621272006-05-24 15:32:06 +00001376 PyStructObject *soself = (PyStructObject *)self;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001377 assert(PyStruct_Check(self));
Tim Petersc2b550e2006-05-31 14:28:07 +00001378 assert(soself->s_codes != NULL);
Raymond Hettinger7a3d41f2007-04-05 18:00:03 +00001379 if (inputstr == NULL)
1380 goto fail;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001381 if (PyString_Check(inputstr) &&
1382 PyString_GET_SIZE(inputstr) == soself->s_size) {
1383 return s_unpack_internal(soself, PyString_AS_STRING(inputstr));
Bob Ippolito232f3c92006-05-23 19:12:41 +00001384 }
Raymond Hettinger7a3d41f2007-04-05 18:00:03 +00001385 args = PyTuple_Pack(1, inputstr);
1386 if (args == NULL)
1387 return NULL;
1388 if (!PyArg_ParseTuple(args, "s#:unpack", &start, &len))
1389 goto fail;
1390 if (soself->s_size != len)
1391 goto fail;
1392 result = s_unpack_internal(soself, start);
1393 Py_DECREF(args);
1394 return result;
1395
1396fail:
1397 Py_XDECREF(args);
1398 PyErr_Format(StructError,
1399 "unpack requires a string argument of length %zd",
1400 soself->s_size);
1401 return NULL;
Bob Ippolitoeb621272006-05-24 15:32:06 +00001402}
1403
1404PyDoc_STRVAR(s_unpack_from__doc__,
Bob Ippolito1fcdc232006-05-27 12:11:36 +00001405"S.unpack_from(buffer[, offset]) -> (v1, v2, ...)\n\
Bob Ippolitoeb621272006-05-24 15:32:06 +00001406\n\
1407Return tuple containing values unpacked according to this Struct's format.\n\
1408Unlike unpack, unpack_from can unpack values from any object supporting\n\
1409the buffer API, not just str. Requires len(buffer[offset:]) >= self.size.\n\
1410See struct.__doc__ for more on format strings.");
1411
1412static PyObject *
1413s_unpack_from(PyObject *self, PyObject *args, PyObject *kwds)
1414{
1415 static char *kwlist[] = {"buffer", "offset", 0};
1416#if (PY_VERSION_HEX < 0x02050000)
1417 static char *fmt = "z#|i:unpack_from";
1418#else
1419 static char *fmt = "z#|n:unpack_from";
1420#endif
1421 Py_ssize_t buffer_len = 0, offset = 0;
1422 char *buffer = NULL;
1423 PyStructObject *soself = (PyStructObject *)self;
1424 assert(PyStruct_Check(self));
1425 assert(soself->s_codes != NULL);
1426
1427 if (!PyArg_ParseTupleAndKeywords(args, kwds, fmt, kwlist,
1428 &buffer, &buffer_len, &offset))
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001429 return NULL;
Bob Ippolitoeb621272006-05-24 15:32:06 +00001430
1431 if (buffer == NULL) {
1432 PyErr_Format(StructError,
1433 "unpack_from requires a buffer argument");
Bob Ippolito232f3c92006-05-23 19:12:41 +00001434 return NULL;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001435 }
Tim Petersc2b550e2006-05-31 14:28:07 +00001436
Bob Ippolitoeb621272006-05-24 15:32:06 +00001437 if (offset < 0)
1438 offset += buffer_len;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001439
Bob Ippolitoeb621272006-05-24 15:32:06 +00001440 if (offset < 0 || (buffer_len - offset) < soself->s_size) {
1441 PyErr_Format(StructError,
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001442 "unpack_from requires a buffer of at least %zd bytes",
Bob Ippolitoeb621272006-05-24 15:32:06 +00001443 soself->s_size);
1444 return NULL;
1445 }
1446 return s_unpack_internal(soself, buffer + offset);
1447}
Bob Ippolito232f3c92006-05-23 19:12:41 +00001448
Bob Ippolito232f3c92006-05-23 19:12:41 +00001449
Martin Blais2856e5f2006-05-26 12:03:27 +00001450/*
1451 * Guts of the pack function.
1452 *
1453 * Takes a struct object, a tuple of arguments, and offset in that tuple of
1454 * argument for where to start processing the arguments for packing, and a
1455 * character buffer for writing the packed string. The caller must insure
1456 * that the buffer may contain the required length for packing the arguments.
1457 * 0 is returned on success, 1 is returned if there is an error.
1458 *
1459 */
1460static int
1461s_pack_internal(PyStructObject *soself, PyObject *args, int offset, char* buf)
Bob Ippolito232f3c92006-05-23 19:12:41 +00001462{
Bob Ippolito232f3c92006-05-23 19:12:41 +00001463 formatcode *code;
Neal Norwitz3c5431e2006-06-11 05:45:25 +00001464 /* XXX(nnorwitz): why does i need to be a local? can we use
1465 the offset parameter or do we need the wider width? */
Bob Ippolito232f3c92006-05-23 19:12:41 +00001466 Py_ssize_t i;
Martin Blais2856e5f2006-05-26 12:03:27 +00001467
1468 memset(buf, '\0', soself->s_size);
1469 i = offset;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001470 for (code = soself->s_codes; code->fmtdef != NULL; code++) {
1471 Py_ssize_t n;
Neal Norwitz3c5431e2006-06-11 05:45:25 +00001472 PyObject *v = PyTuple_GET_ITEM(args, i++);
Bob Ippolito232f3c92006-05-23 19:12:41 +00001473 const formatdef *e = code->fmtdef;
Martin Blais2856e5f2006-05-26 12:03:27 +00001474 char *res = buf + code->offset;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001475 if (e->format == 's') {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001476 if (!PyString_Check(v)) {
Bob Ippolito232f3c92006-05-23 19:12:41 +00001477 PyErr_SetString(StructError,
Mark Dickinson5fd3af22009-07-07 15:08:28 +00001478 "argument for 's' must "
1479 "be a string");
Martin Blais2856e5f2006-05-26 12:03:27 +00001480 return -1;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001481 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001482 n = PyString_GET_SIZE(v);
Bob Ippolitoeb621272006-05-24 15:32:06 +00001483 if (n > code->size)
1484 n = code->size;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001485 if (n > 0)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001486 memcpy(res, PyString_AS_STRING(v), n);
Bob Ippolito232f3c92006-05-23 19:12:41 +00001487 } else if (e->format == 'p') {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001488 if (!PyString_Check(v)) {
Bob Ippolito232f3c92006-05-23 19:12:41 +00001489 PyErr_SetString(StructError,
Mark Dickinson5fd3af22009-07-07 15:08:28 +00001490 "argument for 'p' must "
1491 "be a string");
Martin Blais2856e5f2006-05-26 12:03:27 +00001492 return -1;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001493 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001494 n = PyString_GET_SIZE(v);
Bob Ippolitoeb621272006-05-24 15:32:06 +00001495 if (n > (code->size - 1))
1496 n = code->size - 1;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001497 if (n > 0)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001498 memcpy(res + 1, PyString_AS_STRING(v), n);
Bob Ippolito232f3c92006-05-23 19:12:41 +00001499 if (n > 255)
1500 n = 255;
1501 *res = Py_SAFE_DOWNCAST(n, Py_ssize_t, unsigned char);
Mark Dickinson5fd3af22009-07-07 15:08:28 +00001502 } else if (e->pack(res, v, e) < 0) {
1503 if (strchr(integer_codes, e->format) != NULL &&
1504 PyErr_ExceptionMatches(PyExc_OverflowError))
1505 PyErr_Format(StructError,
1506 "integer out of range for "
1507 "'%c' format code",
1508 e->format);
1509 return -1;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001510 }
1511 }
Tim Petersc2b550e2006-05-31 14:28:07 +00001512
Martin Blais2856e5f2006-05-26 12:03:27 +00001513 /* Success */
1514 return 0;
1515}
Bob Ippolito232f3c92006-05-23 19:12:41 +00001516
Martin Blais2856e5f2006-05-26 12:03:27 +00001517
1518PyDoc_STRVAR(s_pack__doc__,
Bob Ippolito1fcdc232006-05-27 12:11:36 +00001519"S.pack(v1, v2, ...) -> string\n\
Martin Blais2856e5f2006-05-26 12:03:27 +00001520\n\
1521Return a string containing values v1, v2, ... packed according to this\n\
1522Struct's format. See struct.__doc__ for more on format strings.");
1523
1524static PyObject *
1525s_pack(PyObject *self, PyObject *args)
1526{
1527 PyStructObject *soself;
1528 PyObject *result;
1529
1530 /* Validate arguments. */
1531 soself = (PyStructObject *)self;
1532 assert(PyStruct_Check(self));
1533 assert(soself->s_codes != NULL);
Martin Blaisaf2ae722006-06-04 13:49:49 +00001534 if (PyTuple_GET_SIZE(args) != soself->s_len)
Martin Blais2856e5f2006-05-26 12:03:27 +00001535 {
1536 PyErr_Format(StructError,
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001537 "pack requires exactly %zd arguments", soself->s_len);
Martin Blais2856e5f2006-05-26 12:03:27 +00001538 return NULL;
1539 }
Tim Petersc2b550e2006-05-31 14:28:07 +00001540
Martin Blais2856e5f2006-05-26 12:03:27 +00001541 /* Allocate a new string */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001542 result = PyString_FromStringAndSize((char *)NULL, soself->s_size);
Martin Blais2856e5f2006-05-26 12:03:27 +00001543 if (result == NULL)
1544 return NULL;
Tim Petersc2b550e2006-05-31 14:28:07 +00001545
Martin Blais2856e5f2006-05-26 12:03:27 +00001546 /* Call the guts */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001547 if ( s_pack_internal(soself, args, 0, PyString_AS_STRING(result)) != 0 ) {
Martin Blais2856e5f2006-05-26 12:03:27 +00001548 Py_DECREF(result);
1549 return NULL;
1550 }
1551
1552 return result;
1553}
1554
Martin Blaisaf2ae722006-06-04 13:49:49 +00001555PyDoc_STRVAR(s_pack_into__doc__,
1556"S.pack_into(buffer, offset, v1, v2, ...)\n\
Martin Blais2856e5f2006-05-26 12:03:27 +00001557\n\
Martin Blaisaf2ae722006-06-04 13:49:49 +00001558Pack the values v1, v2, ... according to this Struct's format, write \n\
Bob Ippolito1fcdc232006-05-27 12:11:36 +00001559the packed bytes into the writable buffer buf starting at offset. Note\n\
1560that the offset is not an optional argument. See struct.__doc__ for \n\
Martin Blais2856e5f2006-05-26 12:03:27 +00001561more on format strings.");
1562
1563static PyObject *
Martin Blaisaf2ae722006-06-04 13:49:49 +00001564s_pack_into(PyObject *self, PyObject *args)
Martin Blais2856e5f2006-05-26 12:03:27 +00001565{
1566 PyStructObject *soself;
1567 char *buffer;
1568 Py_ssize_t buffer_len, offset;
1569
1570 /* Validate arguments. +1 is for the first arg as buffer. */
1571 soself = (PyStructObject *)self;
1572 assert(PyStruct_Check(self));
1573 assert(soself->s_codes != NULL);
Martin Blaisaf2ae722006-06-04 13:49:49 +00001574 if (PyTuple_GET_SIZE(args) != (soself->s_len + 2))
Martin Blais2856e5f2006-05-26 12:03:27 +00001575 {
1576 PyErr_Format(StructError,
Martin Blaisaf2ae722006-06-04 13:49:49 +00001577 "pack_into requires exactly %zd arguments",
Martin Blais2856e5f2006-05-26 12:03:27 +00001578 (soself->s_len + 2));
1579 return NULL;
1580 }
1581
1582 /* Extract a writable memory buffer from the first argument */
Tim Petersc2b550e2006-05-31 14:28:07 +00001583 if ( PyObject_AsWriteBuffer(PyTuple_GET_ITEM(args, 0),
1584 (void**)&buffer, &buffer_len) == -1 ) {
Martin Blais2856e5f2006-05-26 12:03:27 +00001585 return NULL;
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001586 }
1587 assert( buffer_len >= 0 );
Martin Blais2856e5f2006-05-26 12:03:27 +00001588
1589 /* Extract the offset from the first argument */
Martin Blaisaf2ae722006-06-04 13:49:49 +00001590 offset = PyInt_AsSsize_t(PyTuple_GET_ITEM(args, 1));
Benjamin Peterson02252482008-09-30 02:11:07 +00001591 if (offset == -1 && PyErr_Occurred())
1592 return NULL;
Martin Blais2856e5f2006-05-26 12:03:27 +00001593
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001594 /* Support negative offsets. */
Martin Blais2856e5f2006-05-26 12:03:27 +00001595 if (offset < 0)
1596 offset += buffer_len;
1597
1598 /* Check boundaries */
1599 if (offset < 0 || (buffer_len - offset) < soself->s_size) {
1600 PyErr_Format(StructError,
Martin Blaisaf2ae722006-06-04 13:49:49 +00001601 "pack_into requires a buffer of at least %zd bytes",
Martin Blais2856e5f2006-05-26 12:03:27 +00001602 soself->s_size);
1603 return NULL;
1604 }
Tim Petersc2b550e2006-05-31 14:28:07 +00001605
Martin Blais2856e5f2006-05-26 12:03:27 +00001606 /* Call the guts */
1607 if ( s_pack_internal(soself, args, 2, buffer + offset) != 0 ) {
1608 return NULL;
1609 }
1610
Georg Brandlc26025c2006-05-28 21:42:54 +00001611 Py_RETURN_NONE;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001612}
1613
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001614static PyObject *
1615s_get_format(PyStructObject *self, void *unused)
1616{
1617 Py_INCREF(self->s_format);
1618 return self->s_format;
1619}
1620
1621static PyObject *
1622s_get_size(PyStructObject *self, void *unused)
1623{
1624 return PyInt_FromSsize_t(self->s_size);
1625}
Bob Ippolito232f3c92006-05-23 19:12:41 +00001626
1627/* List of functions */
1628
1629static struct PyMethodDef s_methods[] = {
Martin Blaisaf2ae722006-06-04 13:49:49 +00001630 {"pack", s_pack, METH_VARARGS, s_pack__doc__},
1631 {"pack_into", s_pack_into, METH_VARARGS, s_pack_into__doc__},
1632 {"unpack", s_unpack, METH_O, s_unpack__doc__},
Neal Norwitza84dcd72007-05-22 07:16:44 +00001633 {"unpack_from", (PyCFunction)s_unpack_from, METH_VARARGS|METH_KEYWORDS,
Tim Peters5ec2e852006-06-04 15:49:07 +00001634 s_unpack_from__doc__},
Bob Ippolito232f3c92006-05-23 19:12:41 +00001635 {NULL, NULL} /* sentinel */
1636};
1637
1638PyDoc_STRVAR(s__doc__, "Compiled struct object");
1639
1640#define OFF(x) offsetof(PyStructObject, x)
1641
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001642static PyGetSetDef s_getsetlist[] = {
Bob Ippolito1fcdc232006-05-27 12:11:36 +00001643 {"format", (getter)s_get_format, (setter)NULL, "struct format string", NULL},
1644 {"size", (getter)s_get_size, (setter)NULL, "struct size in bytes", NULL},
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001645 {NULL} /* sentinel */
Bob Ippolito232f3c92006-05-23 19:12:41 +00001646};
1647
Bob Ippolito232f3c92006-05-23 19:12:41 +00001648static
1649PyTypeObject PyStructType = {
Martin v. Löwis68192102007-07-21 06:55:02 +00001650 PyVarObject_HEAD_INIT(NULL, 0)
Bob Ippolito232f3c92006-05-23 19:12:41 +00001651 "Struct",
1652 sizeof(PyStructObject),
1653 0,
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001654 (destructor)s_dealloc, /* tp_dealloc */
Bob Ippolito232f3c92006-05-23 19:12:41 +00001655 0, /* tp_print */
1656 0, /* tp_getattr */
1657 0, /* tp_setattr */
1658 0, /* tp_compare */
1659 0, /* tp_repr */
1660 0, /* tp_as_number */
1661 0, /* tp_as_sequence */
1662 0, /* tp_as_mapping */
1663 0, /* tp_hash */
1664 0, /* tp_call */
1665 0, /* tp_str */
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001666 PyObject_GenericGetAttr, /* tp_getattro */
1667 PyObject_GenericSetAttr, /* tp_setattro */
Bob Ippolito232f3c92006-05-23 19:12:41 +00001668 0, /* tp_as_buffer */
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001669 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_WEAKREFS,/* tp_flags */
1670 s__doc__, /* tp_doc */
Bob Ippolito232f3c92006-05-23 19:12:41 +00001671 0, /* tp_traverse */
1672 0, /* tp_clear */
1673 0, /* tp_richcompare */
1674 offsetof(PyStructObject, weakreflist), /* tp_weaklistoffset */
1675 0, /* tp_iter */
1676 0, /* tp_iternext */
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001677 s_methods, /* tp_methods */
1678 NULL, /* tp_members */
1679 s_getsetlist, /* tp_getset */
Bob Ippolito232f3c92006-05-23 19:12:41 +00001680 0, /* tp_base */
1681 0, /* tp_dict */
1682 0, /* tp_descr_get */
1683 0, /* tp_descr_set */
1684 0, /* tp_dictoffset */
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001685 s_init, /* tp_init */
1686 PyType_GenericAlloc,/* tp_alloc */
1687 s_new, /* tp_new */
1688 PyObject_Del, /* tp_free */
Bob Ippolito232f3c92006-05-23 19:12:41 +00001689};
1690
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00001691
1692/* ---- Standalone functions ---- */
1693
1694#define MAXCACHE 100
Christian Heimes76d19f62008-01-04 02:54:42 +00001695static PyObject *cache = NULL;
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00001696
1697static PyObject *
1698cache_struct(PyObject *fmt)
1699{
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00001700 PyObject * s_object;
1701
1702 if (cache == NULL) {
1703 cache = PyDict_New();
1704 if (cache == NULL)
1705 return NULL;
1706 }
1707
1708 s_object = PyDict_GetItem(cache, fmt);
1709 if (s_object != NULL) {
1710 Py_INCREF(s_object);
1711 return s_object;
1712 }
1713
1714 s_object = PyObject_CallFunctionObjArgs((PyObject *)(&PyStructType), fmt, NULL);
1715 if (s_object != NULL) {
1716 if (PyDict_Size(cache) >= MAXCACHE)
1717 PyDict_Clear(cache);
1718 /* Attempt to cache the result */
1719 if (PyDict_SetItem(cache, fmt, s_object) == -1)
1720 PyErr_Clear();
1721 }
1722 return s_object;
1723}
1724
Christian Heimes76d19f62008-01-04 02:54:42 +00001725PyDoc_STRVAR(clearcache_doc,
1726"Clear the internal cache.");
1727
1728static PyObject *
1729clearcache(PyObject *self)
1730{
Raymond Hettinger18e08e52008-01-18 00:10:42 +00001731 Py_CLEAR(cache);
Christian Heimes76d19f62008-01-04 02:54:42 +00001732 Py_RETURN_NONE;
1733}
1734
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00001735PyDoc_STRVAR(calcsize_doc,
1736"Return size of C struct described by format string fmt.");
1737
1738static PyObject *
1739calcsize(PyObject *self, PyObject *fmt)
1740{
1741 Py_ssize_t n;
1742 PyObject *s_object = cache_struct(fmt);
1743 if (s_object == NULL)
1744 return NULL;
1745 n = ((PyStructObject *)s_object)->s_size;
1746 Py_DECREF(s_object);
1747 return PyInt_FromSsize_t(n);
1748}
1749
1750PyDoc_STRVAR(pack_doc,
1751"Return string containing values v1, v2, ... packed according to fmt.");
1752
1753static PyObject *
1754pack(PyObject *self, PyObject *args)
1755{
1756 PyObject *s_object, *fmt, *newargs, *result;
1757 Py_ssize_t n = PyTuple_GET_SIZE(args);
1758
1759 if (n == 0) {
1760 PyErr_SetString(PyExc_TypeError, "missing format argument");
1761 return NULL;
1762 }
1763 fmt = PyTuple_GET_ITEM(args, 0);
1764 newargs = PyTuple_GetSlice(args, 1, n);
1765 if (newargs == NULL)
1766 return NULL;
1767
1768 s_object = cache_struct(fmt);
1769 if (s_object == NULL) {
1770 Py_DECREF(newargs);
1771 return NULL;
1772 }
1773 result = s_pack(s_object, newargs);
1774 Py_DECREF(newargs);
1775 Py_DECREF(s_object);
1776 return result;
1777}
1778
1779PyDoc_STRVAR(pack_into_doc,
1780"Pack the values v1, v2, ... according to fmt.\n\
1781Write the packed bytes into the writable buffer buf starting at offset.");
1782
1783static PyObject *
1784pack_into(PyObject *self, PyObject *args)
1785{
1786 PyObject *s_object, *fmt, *newargs, *result;
1787 Py_ssize_t n = PyTuple_GET_SIZE(args);
1788
1789 if (n == 0) {
1790 PyErr_SetString(PyExc_TypeError, "missing format argument");
1791 return NULL;
1792 }
1793 fmt = PyTuple_GET_ITEM(args, 0);
1794 newargs = PyTuple_GetSlice(args, 1, n);
1795 if (newargs == NULL)
1796 return NULL;
1797
1798 s_object = cache_struct(fmt);
1799 if (s_object == NULL) {
1800 Py_DECREF(newargs);
1801 return NULL;
1802 }
1803 result = s_pack_into(s_object, newargs);
1804 Py_DECREF(newargs);
1805 Py_DECREF(s_object);
1806 return result;
1807}
1808
1809PyDoc_STRVAR(unpack_doc,
1810"Unpack the string containing packed C structure data, according to fmt.\n\
1811Requires len(string) == calcsize(fmt).");
1812
1813static PyObject *
1814unpack(PyObject *self, PyObject *args)
1815{
1816 PyObject *s_object, *fmt, *inputstr, *result;
1817
1818 if (!PyArg_UnpackTuple(args, "unpack", 2, 2, &fmt, &inputstr))
1819 return NULL;
1820
1821 s_object = cache_struct(fmt);
1822 if (s_object == NULL)
1823 return NULL;
1824 result = s_unpack(s_object, inputstr);
1825 Py_DECREF(s_object);
1826 return result;
1827}
1828
1829PyDoc_STRVAR(unpack_from_doc,
1830"Unpack the buffer, containing packed C structure data, according to\n\
1831fmt, starting at offset. Requires len(buffer[offset:]) >= calcsize(fmt).");
1832
1833static PyObject *
1834unpack_from(PyObject *self, PyObject *args, PyObject *kwds)
1835{
1836 PyObject *s_object, *fmt, *newargs, *result;
1837 Py_ssize_t n = PyTuple_GET_SIZE(args);
1838
1839 if (n == 0) {
1840 PyErr_SetString(PyExc_TypeError, "missing format argument");
1841 return NULL;
1842 }
1843 fmt = PyTuple_GET_ITEM(args, 0);
1844 newargs = PyTuple_GetSlice(args, 1, n);
1845 if (newargs == NULL)
1846 return NULL;
1847
1848 s_object = cache_struct(fmt);
1849 if (s_object == NULL) {
1850 Py_DECREF(newargs);
1851 return NULL;
1852 }
1853 result = s_unpack_from(s_object, newargs, kwds);
1854 Py_DECREF(newargs);
1855 Py_DECREF(s_object);
1856 return result;
1857}
1858
1859static struct PyMethodDef module_functions[] = {
Christian Heimes76d19f62008-01-04 02:54:42 +00001860 {"_clearcache", (PyCFunction)clearcache, METH_NOARGS, clearcache_doc},
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00001861 {"calcsize", calcsize, METH_O, calcsize_doc},
1862 {"pack", pack, METH_VARARGS, pack_doc},
1863 {"pack_into", pack_into, METH_VARARGS, pack_into_doc},
1864 {"unpack", unpack, METH_VARARGS, unpack_doc},
1865 {"unpack_from", (PyCFunction)unpack_from,
1866 METH_VARARGS|METH_KEYWORDS, unpack_from_doc},
1867 {NULL, NULL} /* sentinel */
1868};
1869
1870
Bob Ippolito232f3c92006-05-23 19:12:41 +00001871/* Module initialization */
1872
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00001873PyDoc_STRVAR(module_doc,
1874"Functions to convert between Python values and C structs.\n\
1875Python strings are used to hold the data representing the C struct\n\
1876and also as format strings to describe the layout of data in the C struct.\n\
1877\n\
1878The optional first format char indicates byte order, size and alignment:\n\
1879 @: native order, size & alignment (default)\n\
1880 =: native order, std. size & alignment\n\
1881 <: little-endian, std. size & alignment\n\
1882 >: big-endian, std. size & alignment\n\
1883 !: same as >\n\
1884\n\
1885The remaining chars indicate types of args and must match exactly;\n\
1886these can be preceded by a decimal repeat count:\n\
1887 x: pad byte (no data); c:char; b:signed byte; B:unsigned byte;\n\
1888 h:short; H:unsigned short; i:int; I:unsigned int;\n\
1889 l:long; L:unsigned long; f:float; d:double.\n\
1890Special cases (preceding decimal count indicates length):\n\
1891 s:string (array of char); p: pascal string (with count byte).\n\
1892Special case (only available in native format):\n\
1893 P:an integer type that is wide enough to hold a pointer.\n\
1894Special case (not in native mode unless 'long long' in platform C):\n\
1895 q:long long; Q:unsigned long long\n\
1896Whitespace between formats is ignored.\n\
1897\n\
1898The variable struct.error is an exception raised on errors.\n");
1899
Bob Ippolito232f3c92006-05-23 19:12:41 +00001900PyMODINIT_FUNC
1901init_struct(void)
1902{
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00001903 PyObject *ver, *m;
1904
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001905 ver = PyString_FromString("0.2");
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00001906 if (ver == NULL)
1907 return;
1908
1909 m = Py_InitModule3("_struct", module_functions, module_doc);
Bob Ippolito232f3c92006-05-23 19:12:41 +00001910 if (m == NULL)
1911 return;
1912
Christian Heimese93237d2007-12-19 02:37:44 +00001913 Py_TYPE(&PyStructType) = &PyType_Type;
Bob Ippolito3fc2bb92006-05-25 19:03:19 +00001914 if (PyType_Ready(&PyStructType) < 0)
1915 return;
1916
Mark Dickinson5fd3af22009-07-07 15:08:28 +00001917 /* This speed trick can't be used until overflow masking goes
1918 away, because native endian always raises exceptions
1919 instead of overflow masking. */
Tim Petersc2b550e2006-05-31 14:28:07 +00001920
Bob Ippolitoa99865b2006-05-25 19:56:56 +00001921 /* Check endian and swap in faster functions */
1922 {
1923 int one = 1;
1924 formatdef *native = native_table;
1925 formatdef *other, *ptr;
1926 if ((int)*(unsigned char*)&one)
1927 other = lilendian_table;
1928 else
1929 other = bigendian_table;
Bob Ippolito964e02a2006-05-25 21:09:45 +00001930 /* Scan through the native table, find a matching
1931 entry in the endian table and swap in the
1932 native implementations whenever possible
1933 (64-bit platforms may not have "standard" sizes) */
Bob Ippolitoa99865b2006-05-25 19:56:56 +00001934 while (native->format != '\0' && other->format != '\0') {
1935 ptr = other;
1936 while (ptr->format != '\0') {
1937 if (ptr->format == native->format) {
Bob Ippolito964e02a2006-05-25 21:09:45 +00001938 /* Match faster when formats are
1939 listed in the same order */
Bob Ippolitoa99865b2006-05-25 19:56:56 +00001940 if (ptr == other)
1941 other++;
Tim Petersc2b550e2006-05-31 14:28:07 +00001942 /* Only use the trick if the
Bob Ippolito964e02a2006-05-25 21:09:45 +00001943 size matches */
1944 if (ptr->size != native->size)
1945 break;
1946 /* Skip float and double, could be
1947 "unknown" float format */
1948 if (ptr->format == 'd' || ptr->format == 'f')
1949 break;
1950 ptr->pack = native->pack;
1951 ptr->unpack = native->unpack;
Bob Ippolitoa99865b2006-05-25 19:56:56 +00001952 break;
1953 }
1954 ptr++;
1955 }
1956 native++;
1957 }
1958 }
Tim Petersc2b550e2006-05-31 14:28:07 +00001959
Bob Ippolito232f3c92006-05-23 19:12:41 +00001960 /* Add some symbolic constants to the module */
1961 if (StructError == NULL) {
1962 StructError = PyErr_NewException("struct.error", NULL, NULL);
1963 if (StructError == NULL)
1964 return;
1965 }
Bob Ippolito04ab9942006-05-25 19:33:38 +00001966
Bob Ippolito232f3c92006-05-23 19:12:41 +00001967 Py_INCREF(StructError);
1968 PyModule_AddObject(m, "error", StructError);
Bob Ippolito04ab9942006-05-25 19:33:38 +00001969
Bob Ippolito232f3c92006-05-23 19:12:41 +00001970 Py_INCREF((PyObject*)&PyStructType);
1971 PyModule_AddObject(m, "Struct", (PyObject*)&PyStructType);
Tim Petersc2b550e2006-05-31 14:28:07 +00001972
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00001973 PyModule_AddObject(m, "__version__", ver);
1974
Bob Ippolito2fd39772006-05-29 22:55:48 +00001975 PyModule_AddIntConstant(m, "_PY_STRUCT_RANGE_CHECKING", 1);
Bob Ippolitoe6c9f982006-08-04 23:59:21 +00001976#ifdef PY_STRUCT_FLOAT_COERCE
1977 PyModule_AddIntConstant(m, "_PY_STRUCT_FLOAT_COERCE", 1);
1978#endif
1979
Bob Ippolito232f3c92006-05-23 19:12:41 +00001980}