blob: a7fce105a0b946a3bf52a8cd5e05c5c482ae7af3 [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 Ippolito4182a752006-05-30 17:37:54 +000020/* If PY_STRUCT_OVERFLOW_MASKING is defined, the struct module will wrap all input
Bob Ippolito2fd39772006-05-29 22:55:48 +000021 numbers for explicit endians such that they fit in the given type, much
22 like explicit casting in C. A warning will be raised if the number did
23 not originally fit within the range of the requested type. If it is
24 not defined, then all range errors and overflow will be struct.error
25 exceptions. */
26
Bob Ippolito4182a752006-05-30 17:37:54 +000027#define PY_STRUCT_OVERFLOW_MASKING 1
Bob Ippolito2fd39772006-05-29 22:55:48 +000028
Bob Ippolito4182a752006-05-30 17:37:54 +000029#ifdef PY_STRUCT_OVERFLOW_MASKING
Bob Ippolito2fd39772006-05-29 22:55:48 +000030static PyObject *pylong_ulong_mask = NULL;
31static PyObject *pyint_zero = NULL;
32#endif
33
Bob Ippolitoe6c9f982006-08-04 23:59:21 +000034/* If PY_STRUCT_FLOAT_COERCE is defined, the struct module will allow float
35 arguments for integer formats with a warning for backwards
36 compatibility. */
37
38#define PY_STRUCT_FLOAT_COERCE 1
39
40#ifdef PY_STRUCT_FLOAT_COERCE
41#define FLOAT_COERCE "integer argument expected, got float"
42#endif
43
44
Bob Ippolito232f3c92006-05-23 19:12:41 +000045/* The translation function for each format character is table driven */
Bob Ippolito232f3c92006-05-23 19:12:41 +000046typedef struct _formatdef {
47 char format;
Bob Ippolitoaa70a172006-05-26 20:25:23 +000048 Py_ssize_t size;
49 Py_ssize_t alignment;
Bob Ippolito232f3c92006-05-23 19:12:41 +000050 PyObject* (*unpack)(const char *,
51 const struct _formatdef *);
52 int (*pack)(char *, PyObject *,
53 const struct _formatdef *);
54} formatdef;
55
56typedef struct _formatcode {
57 const struct _formatdef *fmtdef;
Bob Ippolitoaa70a172006-05-26 20:25:23 +000058 Py_ssize_t offset;
59 Py_ssize_t size;
Bob Ippolito232f3c92006-05-23 19:12:41 +000060} formatcode;
61
62/* Struct object interface */
63
64typedef struct {
65 PyObject_HEAD
Bob Ippolitoaa70a172006-05-26 20:25:23 +000066 Py_ssize_t s_size;
67 Py_ssize_t s_len;
Bob Ippolito232f3c92006-05-23 19:12:41 +000068 formatcode *s_codes;
69 PyObject *s_format;
70 PyObject *weakreflist; /* List of weak references */
71} PyStructObject;
72
Bob Ippolitoeb621272006-05-24 15:32:06 +000073
Bob Ippolito07c023b2006-05-23 19:32:25 +000074#define PyStruct_Check(op) PyObject_TypeCheck(op, &PyStructType)
Christian Heimese93237d2007-12-19 02:37:44 +000075#define PyStruct_CheckExact(op) (Py_TYPE(op) == &PyStructType)
Bob Ippolito232f3c92006-05-23 19:12:41 +000076
77
78/* Exception */
79
80static PyObject *StructError;
81
82
83/* Define various structs to figure out the alignments of types */
84
85
86typedef struct { char c; short x; } st_short;
87typedef struct { char c; int x; } st_int;
88typedef struct { char c; long x; } st_long;
89typedef struct { char c; float x; } st_float;
90typedef struct { char c; double x; } st_double;
91typedef struct { char c; void *x; } st_void_p;
92
93#define SHORT_ALIGN (sizeof(st_short) - sizeof(short))
94#define INT_ALIGN (sizeof(st_int) - sizeof(int))
95#define LONG_ALIGN (sizeof(st_long) - sizeof(long))
96#define FLOAT_ALIGN (sizeof(st_float) - sizeof(float))
97#define DOUBLE_ALIGN (sizeof(st_double) - sizeof(double))
98#define VOID_P_ALIGN (sizeof(st_void_p) - sizeof(void *))
99
100/* We can't support q and Q in native mode unless the compiler does;
101 in std mode, they're 8 bytes on all platforms. */
102#ifdef HAVE_LONG_LONG
103typedef struct { char c; PY_LONG_LONG x; } s_long_long;
104#define LONG_LONG_ALIGN (sizeof(s_long_long) - sizeof(PY_LONG_LONG))
105#endif
106
Martin v. Löwisaef4c6b2007-01-21 09:33:07 +0000107#ifdef HAVE_C99_BOOL
108#define BOOL_TYPE _Bool
109typedef struct { char c; _Bool x; } s_bool;
110#define BOOL_ALIGN (sizeof(s_bool) - sizeof(BOOL_TYPE))
111#else
112#define BOOL_TYPE char
113#define BOOL_ALIGN 0
114#endif
115
Bob Ippolito232f3c92006-05-23 19:12:41 +0000116#define STRINGIFY(x) #x
117
118#ifdef __powerc
119#pragma options align=reset
120#endif
121
122/* Helper to get a PyLongObject by hook or by crook. Caller should decref. */
123
124static PyObject *
125get_pylong(PyObject *v)
126{
127 PyNumberMethods *m;
128
129 assert(v != NULL);
130 if (PyInt_Check(v))
131 return PyLong_FromLong(PyInt_AS_LONG(v));
132 if (PyLong_Check(v)) {
133 Py_INCREF(v);
134 return v;
135 }
Christian Heimese93237d2007-12-19 02:37:44 +0000136 m = Py_TYPE(v)->tp_as_number;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000137 if (m != NULL && m->nb_long != NULL) {
138 v = m->nb_long(v);
139 if (v == NULL)
140 return NULL;
141 if (PyLong_Check(v))
142 return v;
143 Py_DECREF(v);
144 }
145 PyErr_SetString(StructError,
146 "cannot convert argument to long");
147 return NULL;
148}
149
150/* Helper routine to get a Python integer and raise the appropriate error
151 if it isn't one */
152
153static int
154get_long(PyObject *v, long *p)
155{
156 long x = PyInt_AsLong(v);
157 if (x == -1 && PyErr_Occurred()) {
Bob Ippolitoe6c9f982006-08-04 23:59:21 +0000158#ifdef PY_STRUCT_FLOAT_COERCE
159 if (PyFloat_Check(v)) {
160 PyObject *o;
161 int res;
162 PyErr_Clear();
163 if (PyErr_WarnEx(PyExc_DeprecationWarning, FLOAT_COERCE, 2) < 0)
164 return -1;
165 o = PyNumber_Int(v);
166 if (o == NULL)
167 return -1;
168 res = get_long(o, p);
169 Py_DECREF(o);
170 return res;
171 }
172#endif
Bob Ippolito232f3c92006-05-23 19:12:41 +0000173 if (PyErr_ExceptionMatches(PyExc_TypeError))
174 PyErr_SetString(StructError,
175 "required argument is not an integer");
176 return -1;
177 }
178 *p = x;
179 return 0;
180}
181
182
183/* Same, but handling unsigned long */
184
Jeffrey Yasskin7937d932009-05-29 03:44:31 +0000185#ifndef PY_STRUCT_OVERFLOW_MASKING
Bob Ippolito232f3c92006-05-23 19:12:41 +0000186static int
187get_ulong(PyObject *v, unsigned long *p)
188{
189 if (PyLong_Check(v)) {
190 unsigned long x = PyLong_AsUnsignedLong(v);
191 if (x == (unsigned long)(-1) && PyErr_Occurred())
192 return -1;
193 *p = x;
194 return 0;
195 }
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000196 if (get_long(v, (long *)p) < 0)
197 return -1;
198 if (((long)*p) < 0) {
199 PyErr_SetString(StructError,
200 "unsigned argument is < 0");
201 return -1;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000202 }
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000203 return 0;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000204}
Jeffrey Yasskin7937d932009-05-29 03:44:31 +0000205#endif /* PY_STRUCT_OVERFLOW_MASKING */
Bob Ippolito232f3c92006-05-23 19:12:41 +0000206
207#ifdef HAVE_LONG_LONG
208
209/* Same, but handling native long long. */
210
211static int
212get_longlong(PyObject *v, PY_LONG_LONG *p)
213{
214 PY_LONG_LONG x;
215
216 v = get_pylong(v);
217 if (v == NULL)
218 return -1;
219 assert(PyLong_Check(v));
220 x = PyLong_AsLongLong(v);
221 Py_DECREF(v);
222 if (x == (PY_LONG_LONG)-1 && PyErr_Occurred())
223 return -1;
224 *p = x;
225 return 0;
226}
227
228/* Same, but handling native unsigned long long. */
229
230static int
231get_ulonglong(PyObject *v, unsigned PY_LONG_LONG *p)
232{
233 unsigned PY_LONG_LONG x;
234
235 v = get_pylong(v);
236 if (v == NULL)
237 return -1;
238 assert(PyLong_Check(v));
239 x = PyLong_AsUnsignedLongLong(v);
240 Py_DECREF(v);
241 if (x == (unsigned PY_LONG_LONG)-1 && PyErr_Occurred())
242 return -1;
243 *p = x;
244 return 0;
245}
246
247#endif
248
Bob Ippolito4182a752006-05-30 17:37:54 +0000249#ifdef PY_STRUCT_OVERFLOW_MASKING
Bob Ippolito2fd39772006-05-29 22:55:48 +0000250
251/* Helper routine to get a Python integer and raise the appropriate error
252 if it isn't one */
253
Neal Norwitz07aadb12006-07-30 06:55:48 +0000254#define INT_OVERFLOW "struct integer overflow masking is deprecated"
255
Bob Ippolito2fd39772006-05-29 22:55:48 +0000256static int
257get_wrapped_long(PyObject *v, long *p)
258{
259 if (get_long(v, p) < 0) {
Neal Norwitz3c5431e2006-06-11 05:45:25 +0000260 if (PyLong_Check(v) &&
261 PyErr_ExceptionMatches(PyExc_OverflowError)) {
Bob Ippolito2fd39772006-05-29 22:55:48 +0000262 PyObject *wrapped;
263 long x;
264 PyErr_Clear();
Bob Ippolitoe6c9f982006-08-04 23:59:21 +0000265#ifdef PY_STRUCT_FLOAT_COERCE
266 if (PyFloat_Check(v)) {
267 PyObject *o;
268 int res;
269 PyErr_Clear();
270 if (PyErr_WarnEx(PyExc_DeprecationWarning, FLOAT_COERCE, 2) < 0)
271 return -1;
272 o = PyNumber_Int(v);
273 if (o == NULL)
274 return -1;
275 res = get_wrapped_long(o, p);
276 Py_DECREF(o);
277 return res;
278 }
279#endif
Neal Norwitz07aadb12006-07-30 06:55:48 +0000280 if (PyErr_WarnEx(PyExc_DeprecationWarning, INT_OVERFLOW, 2) < 0)
Bob Ippolito2fd39772006-05-29 22:55:48 +0000281 return -1;
282 wrapped = PyNumber_And(v, pylong_ulong_mask);
283 if (wrapped == NULL)
284 return -1;
285 x = (long)PyLong_AsUnsignedLong(wrapped);
286 Py_DECREF(wrapped);
287 if (x == -1 && PyErr_Occurred())
288 return -1;
289 *p = x;
290 } else {
291 return -1;
292 }
293 }
294 return 0;
295}
296
297static int
298get_wrapped_ulong(PyObject *v, unsigned long *p)
299{
300 long x = (long)PyLong_AsUnsignedLong(v);
301 if (x == -1 && PyErr_Occurred()) {
302 PyObject *wrapped;
303 PyErr_Clear();
Bob Ippolitoe6c9f982006-08-04 23:59:21 +0000304#ifdef PY_STRUCT_FLOAT_COERCE
305 if (PyFloat_Check(v)) {
306 PyObject *o;
307 int res;
308 PyErr_Clear();
309 if (PyErr_WarnEx(PyExc_DeprecationWarning, FLOAT_COERCE, 2) < 0)
310 return -1;
311 o = PyNumber_Int(v);
312 if (o == NULL)
313 return -1;
314 res = get_wrapped_ulong(o, p);
315 Py_DECREF(o);
316 return res;
317 }
318#endif
Bob Ippolito2fd39772006-05-29 22:55:48 +0000319 wrapped = PyNumber_And(v, pylong_ulong_mask);
320 if (wrapped == NULL)
321 return -1;
Neal Norwitz07aadb12006-07-30 06:55:48 +0000322 if (PyErr_WarnEx(PyExc_DeprecationWarning, INT_OVERFLOW, 2) < 0) {
Bob Ippolito2fd39772006-05-29 22:55:48 +0000323 Py_DECREF(wrapped);
324 return -1;
325 }
326 x = (long)PyLong_AsUnsignedLong(wrapped);
327 Py_DECREF(wrapped);
328 if (x == -1 && PyErr_Occurred())
329 return -1;
330 }
331 *p = (unsigned long)x;
332 return 0;
333}
334
335#define RANGE_ERROR(x, f, flag, mask) \
336 do { \
337 if (_range_error(f, flag) < 0) \
338 return -1; \
339 else \
340 (x) &= (mask); \
341 } while (0)
342
343#else
344
345#define get_wrapped_long get_long
346#define get_wrapped_ulong get_ulong
347#define RANGE_ERROR(x, f, flag, mask) return _range_error(f, flag)
348
349#endif
350
Bob Ippolito232f3c92006-05-23 19:12:41 +0000351/* Floating point helpers */
352
353static PyObject *
354unpack_float(const char *p, /* start of 4-byte string */
355 int le) /* true for little-endian, false for big-endian */
356{
357 double x;
358
359 x = _PyFloat_Unpack4((unsigned char *)p, le);
360 if (x == -1.0 && PyErr_Occurred())
361 return NULL;
362 return PyFloat_FromDouble(x);
363}
364
365static PyObject *
366unpack_double(const char *p, /* start of 8-byte string */
367 int le) /* true for little-endian, false for big-endian */
368{
369 double x;
370
371 x = _PyFloat_Unpack8((unsigned char *)p, le);
372 if (x == -1.0 && PyErr_Occurred())
373 return NULL;
374 return PyFloat_FromDouble(x);
375}
376
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000377/* Helper to format the range error exceptions */
378static int
Armin Rigo162997e2006-05-29 17:59:47 +0000379_range_error(const formatdef *f, int is_unsigned)
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000380{
Tim Petersd6a6f022006-05-31 15:33:22 +0000381 /* ulargest is the largest unsigned value with f->size bytes.
382 * Note that the simpler:
383 * ((size_t)1 << (f->size * 8)) - 1
Tim Peters72270c22006-05-31 15:34:37 +0000384 * doesn't work when f->size == sizeof(size_t) because C doesn't
385 * define what happens when a left shift count is >= the number of
386 * bits in the integer being shifted; e.g., on some boxes it doesn't
387 * shift at all when they're equal.
Tim Petersd6a6f022006-05-31 15:33:22 +0000388 */
389 const size_t ulargest = (size_t)-1 >> ((SIZEOF_SIZE_T - f->size)*8);
390 assert(f->size >= 1 && f->size <= SIZEOF_SIZE_T);
391 if (is_unsigned)
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000392 PyErr_Format(StructError,
Neal Norwitz971ea112006-05-31 07:43:27 +0000393 "'%c' format requires 0 <= number <= %zu",
Bob Ippolito28b26862006-05-29 15:47:29 +0000394 f->format,
Tim Petersd6a6f022006-05-31 15:33:22 +0000395 ulargest);
396 else {
397 const Py_ssize_t largest = (Py_ssize_t)(ulargest >> 1);
398 PyErr_Format(StructError,
399 "'%c' format requires %zd <= number <= %zd",
400 f->format,
401 ~ largest,
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000402 largest);
403 }
Bob Ippolito4182a752006-05-30 17:37:54 +0000404#ifdef PY_STRUCT_OVERFLOW_MASKING
Bob Ippolito2fd39772006-05-29 22:55:48 +0000405 {
406 PyObject *ptype, *pvalue, *ptraceback;
407 PyObject *msg;
408 int rval;
409 PyErr_Fetch(&ptype, &pvalue, &ptraceback);
410 assert(pvalue != NULL);
411 msg = PyObject_Str(pvalue);
412 Py_XDECREF(ptype);
413 Py_XDECREF(pvalue);
414 Py_XDECREF(ptraceback);
415 if (msg == NULL)
416 return -1;
Neal Norwitz07aadb12006-07-30 06:55:48 +0000417 rval = PyErr_WarnEx(PyExc_DeprecationWarning,
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000418 PyString_AS_STRING(msg), 2);
Bob Ippolito2fd39772006-05-29 22:55:48 +0000419 Py_DECREF(msg);
420 if (rval == 0)
421 return 0;
422 }
423#endif
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000424 return -1;
425}
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000426
427
Bob Ippolito232f3c92006-05-23 19:12:41 +0000428
429/* A large number of small routines follow, with names of the form
430
Bob Ippolitoaa70a172006-05-26 20:25:23 +0000431 [bln][up]_TYPE
Bob Ippolito232f3c92006-05-23 19:12:41 +0000432
433 [bln] distiguishes among big-endian, little-endian and native.
434 [pu] distiguishes between pack (to struct) and unpack (from struct).
435 TYPE is one of char, byte, ubyte, etc.
436*/
437
438/* Native mode routines. ****************************************************/
439/* NOTE:
440 In all n[up]_<type> routines handling types larger than 1 byte, there is
441 *no* guarantee that the p pointer is properly aligned for each type,
442 therefore memcpy is called. An intermediate variable is used to
443 compensate for big-endian architectures.
444 Normally both the intermediate variable and the memcpy call will be
445 skipped by C optimisation in little-endian architectures (gcc >= 2.91
446 does this). */
447
448static PyObject *
449nu_char(const char *p, const formatdef *f)
450{
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000451 return PyString_FromStringAndSize(p, 1);
Bob Ippolito232f3c92006-05-23 19:12:41 +0000452}
453
454static PyObject *
455nu_byte(const char *p, const formatdef *f)
456{
457 return PyInt_FromLong((long) *(signed char *)p);
458}
459
460static PyObject *
461nu_ubyte(const char *p, const formatdef *f)
462{
463 return PyInt_FromLong((long) *(unsigned char *)p);
464}
465
466static PyObject *
467nu_short(const char *p, const formatdef *f)
468{
469 short x;
470 memcpy((char *)&x, p, sizeof x);
471 return PyInt_FromLong((long)x);
472}
473
474static PyObject *
475nu_ushort(const char *p, const formatdef *f)
476{
477 unsigned short x;
478 memcpy((char *)&x, p, sizeof x);
479 return PyInt_FromLong((long)x);
480}
481
482static PyObject *
483nu_int(const char *p, const formatdef *f)
484{
485 int x;
486 memcpy((char *)&x, p, sizeof x);
487 return PyInt_FromLong((long)x);
488}
489
490static PyObject *
491nu_uint(const char *p, const formatdef *f)
492{
493 unsigned int x;
494 memcpy((char *)&x, p, sizeof x);
Bob Ippolitocd51ca52006-05-27 15:53:49 +0000495#if (SIZEOF_LONG > SIZEOF_INT)
496 return PyInt_FromLong((long)x);
497#else
498 if (x <= ((unsigned int)LONG_MAX))
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000499 return PyInt_FromLong((long)x);
Bob Ippolito232f3c92006-05-23 19:12:41 +0000500 return PyLong_FromUnsignedLong((unsigned long)x);
Bob Ippolitocd51ca52006-05-27 15:53:49 +0000501#endif
Bob Ippolito232f3c92006-05-23 19:12:41 +0000502}
503
504static PyObject *
505nu_long(const char *p, const formatdef *f)
506{
507 long x;
508 memcpy((char *)&x, p, sizeof x);
509 return PyInt_FromLong(x);
510}
511
512static PyObject *
513nu_ulong(const char *p, const formatdef *f)
514{
515 unsigned long x;
516 memcpy((char *)&x, p, sizeof x);
Bob Ippolito04ab9942006-05-25 19:33:38 +0000517 if (x <= LONG_MAX)
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000518 return PyInt_FromLong((long)x);
Bob Ippolito232f3c92006-05-23 19:12:41 +0000519 return PyLong_FromUnsignedLong(x);
520}
521
522/* Native mode doesn't support q or Q unless the platform C supports
523 long long (or, on Windows, __int64). */
524
525#ifdef HAVE_LONG_LONG
526
527static PyObject *
528nu_longlong(const char *p, const formatdef *f)
529{
530 PY_LONG_LONG x;
531 memcpy((char *)&x, p, sizeof x);
Bob Ippolito04ab9942006-05-25 19:33:38 +0000532 if (x >= LONG_MIN && x <= LONG_MAX)
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000533 return PyInt_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
Bob Ippolito232f3c92006-05-23 19:12:41 +0000534 return PyLong_FromLongLong(x);
535}
536
537static PyObject *
538nu_ulonglong(const char *p, const formatdef *f)
539{
540 unsigned PY_LONG_LONG x;
541 memcpy((char *)&x, p, sizeof x);
Bob Ippolito04ab9942006-05-25 19:33:38 +0000542 if (x <= LONG_MAX)
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000543 return PyInt_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
Bob Ippolito232f3c92006-05-23 19:12:41 +0000544 return PyLong_FromUnsignedLongLong(x);
545}
546
547#endif
548
549static PyObject *
Martin v. Löwisaef4c6b2007-01-21 09:33:07 +0000550nu_bool(const char *p, const formatdef *f)
551{
552 BOOL_TYPE x;
553 memcpy((char *)&x, p, sizeof x);
554 return PyBool_FromLong(x != 0);
555}
556
557
558static PyObject *
Bob Ippolito232f3c92006-05-23 19:12:41 +0000559nu_float(const char *p, const formatdef *f)
560{
561 float x;
562 memcpy((char *)&x, p, sizeof x);
563 return PyFloat_FromDouble((double)x);
564}
565
566static PyObject *
567nu_double(const char *p, const formatdef *f)
568{
569 double x;
570 memcpy((char *)&x, p, sizeof x);
571 return PyFloat_FromDouble(x);
572}
573
574static PyObject *
575nu_void_p(const char *p, const formatdef *f)
576{
577 void *x;
578 memcpy((char *)&x, p, sizeof x);
579 return PyLong_FromVoidPtr(x);
580}
581
582static int
583np_byte(char *p, PyObject *v, const formatdef *f)
584{
585 long x;
586 if (get_long(v, &x) < 0)
587 return -1;
588 if (x < -128 || x > 127){
589 PyErr_SetString(StructError,
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000590 "byte format requires -128 <= number <= 127");
Bob Ippolito232f3c92006-05-23 19:12:41 +0000591 return -1;
592 }
593 *p = (char)x;
594 return 0;
595}
596
597static int
598np_ubyte(char *p, PyObject *v, const formatdef *f)
599{
600 long x;
601 if (get_long(v, &x) < 0)
602 return -1;
603 if (x < 0 || x > 255){
604 PyErr_SetString(StructError,
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000605 "ubyte format requires 0 <= number <= 255");
Bob Ippolito232f3c92006-05-23 19:12:41 +0000606 return -1;
607 }
608 *p = (char)x;
609 return 0;
610}
611
612static int
613np_char(char *p, PyObject *v, const formatdef *f)
614{
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000615 if (!PyString_Check(v) || PyString_Size(v) != 1) {
Bob Ippolito232f3c92006-05-23 19:12:41 +0000616 PyErr_SetString(StructError,
617 "char format require string of length 1");
618 return -1;
619 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000620 *p = *PyString_AsString(v);
Bob Ippolito232f3c92006-05-23 19:12:41 +0000621 return 0;
622}
623
624static int
625np_short(char *p, PyObject *v, const formatdef *f)
626{
627 long x;
628 short y;
629 if (get_long(v, &x) < 0)
630 return -1;
631 if (x < SHRT_MIN || x > SHRT_MAX){
632 PyErr_SetString(StructError,
633 "short format requires " STRINGIFY(SHRT_MIN)
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000634 " <= number <= " STRINGIFY(SHRT_MAX));
Bob Ippolito232f3c92006-05-23 19:12:41 +0000635 return -1;
636 }
637 y = (short)x;
638 memcpy(p, (char *)&y, sizeof y);
639 return 0;
640}
641
642static int
643np_ushort(char *p, PyObject *v, const formatdef *f)
644{
645 long x;
646 unsigned short y;
647 if (get_long(v, &x) < 0)
648 return -1;
649 if (x < 0 || x > USHRT_MAX){
650 PyErr_SetString(StructError,
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000651 "short format requires 0 <= number <= " STRINGIFY(USHRT_MAX));
Bob Ippolito232f3c92006-05-23 19:12:41 +0000652 return -1;
653 }
654 y = (unsigned short)x;
655 memcpy(p, (char *)&y, sizeof y);
656 return 0;
657}
658
659static int
660np_int(char *p, PyObject *v, const formatdef *f)
661{
662 long x;
663 int y;
664 if (get_long(v, &x) < 0)
665 return -1;
Bob Ippolito90bd0a52006-05-27 11:47:12 +0000666#if (SIZEOF_LONG > SIZEOF_INT)
Bob Ippolitocd51ca52006-05-27 15:53:49 +0000667 if ((x < ((long)INT_MIN)) || (x > ((long)INT_MAX)))
Georg Brandl6269fec2009-01-01 12:15:31 +0000668 RANGE_ERROR(x, f, 0, -1);
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000669#endif
Bob Ippolito232f3c92006-05-23 19:12:41 +0000670 y = (int)x;
671 memcpy(p, (char *)&y, sizeof y);
672 return 0;
673}
674
675static int
676np_uint(char *p, PyObject *v, const formatdef *f)
677{
678 unsigned long x;
679 unsigned int y;
Georg Brandl6269fec2009-01-01 12:15:31 +0000680 if (get_wrapped_ulong(v, &x) < 0)
681 return -1;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000682 y = (unsigned int)x;
Bob Ippolito90bd0a52006-05-27 11:47:12 +0000683#if (SIZEOF_LONG > SIZEOF_INT)
Bob Ippolitocd51ca52006-05-27 15:53:49 +0000684 if (x > ((unsigned long)UINT_MAX))
Georg Brandl6269fec2009-01-01 12:15:31 +0000685 RANGE_ERROR(y, f, 1, -1);
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000686#endif
Bob Ippolito232f3c92006-05-23 19:12:41 +0000687 memcpy(p, (char *)&y, sizeof y);
688 return 0;
689}
690
691static int
692np_long(char *p, PyObject *v, const formatdef *f)
693{
694 long x;
695 if (get_long(v, &x) < 0)
696 return -1;
697 memcpy(p, (char *)&x, sizeof x);
698 return 0;
699}
700
701static int
702np_ulong(char *p, PyObject *v, const formatdef *f)
703{
704 unsigned long x;
Georg Brandl6269fec2009-01-01 12:15:31 +0000705 if (get_wrapped_ulong(v, &x) < 0)
706 return -1;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000707 memcpy(p, (char *)&x, sizeof x);
708 return 0;
709}
710
711#ifdef HAVE_LONG_LONG
712
713static int
714np_longlong(char *p, PyObject *v, const formatdef *f)
715{
716 PY_LONG_LONG x;
717 if (get_longlong(v, &x) < 0)
718 return -1;
719 memcpy(p, (char *)&x, sizeof x);
720 return 0;
721}
722
723static int
724np_ulonglong(char *p, PyObject *v, const formatdef *f)
725{
726 unsigned PY_LONG_LONG x;
727 if (get_ulonglong(v, &x) < 0)
728 return -1;
729 memcpy(p, (char *)&x, sizeof x);
730 return 0;
731}
732#endif
733
Martin v. Löwisaef4c6b2007-01-21 09:33:07 +0000734
735static int
736np_bool(char *p, PyObject *v, const formatdef *f)
737{
738 BOOL_TYPE y;
739 y = PyObject_IsTrue(v);
740 memcpy(p, (char *)&y, sizeof y);
741 return 0;
742}
743
Bob Ippolito232f3c92006-05-23 19:12:41 +0000744static int
745np_float(char *p, PyObject *v, const formatdef *f)
746{
747 float x = (float)PyFloat_AsDouble(v);
748 if (x == -1 && PyErr_Occurred()) {
749 PyErr_SetString(StructError,
750 "required argument is not a float");
751 return -1;
752 }
753 memcpy(p, (char *)&x, sizeof x);
754 return 0;
755}
756
757static int
758np_double(char *p, PyObject *v, const formatdef *f)
759{
760 double x = PyFloat_AsDouble(v);
761 if (x == -1 && PyErr_Occurred()) {
762 PyErr_SetString(StructError,
763 "required argument is not a float");
764 return -1;
765 }
766 memcpy(p, (char *)&x, sizeof(double));
767 return 0;
768}
769
770static int
771np_void_p(char *p, PyObject *v, const formatdef *f)
772{
773 void *x;
774
775 v = get_pylong(v);
776 if (v == NULL)
777 return -1;
778 assert(PyLong_Check(v));
779 x = PyLong_AsVoidPtr(v);
780 Py_DECREF(v);
781 if (x == NULL && PyErr_Occurred())
782 return -1;
783 memcpy(p, (char *)&x, sizeof x);
784 return 0;
785}
786
787static formatdef native_table[] = {
788 {'x', sizeof(char), 0, NULL},
789 {'b', sizeof(char), 0, nu_byte, np_byte},
790 {'B', sizeof(char), 0, nu_ubyte, np_ubyte},
791 {'c', sizeof(char), 0, nu_char, np_char},
792 {'s', sizeof(char), 0, NULL},
793 {'p', sizeof(char), 0, NULL},
794 {'h', sizeof(short), SHORT_ALIGN, nu_short, np_short},
795 {'H', sizeof(short), SHORT_ALIGN, nu_ushort, np_ushort},
796 {'i', sizeof(int), INT_ALIGN, nu_int, np_int},
797 {'I', sizeof(int), INT_ALIGN, nu_uint, np_uint},
798 {'l', sizeof(long), LONG_ALIGN, nu_long, np_long},
799 {'L', sizeof(long), LONG_ALIGN, nu_ulong, np_ulong},
Bob Ippolito232f3c92006-05-23 19:12:41 +0000800#ifdef HAVE_LONG_LONG
801 {'q', sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_longlong, np_longlong},
802 {'Q', sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_ulonglong,np_ulonglong},
803#endif
Thomas Hellerf3c05592008-03-05 15:34:29 +0000804 {'?', sizeof(BOOL_TYPE), BOOL_ALIGN, nu_bool, np_bool},
Bob Ippolitoa99865b2006-05-25 19:56:56 +0000805 {'f', sizeof(float), FLOAT_ALIGN, nu_float, np_float},
806 {'d', sizeof(double), DOUBLE_ALIGN, nu_double, np_double},
807 {'P', sizeof(void *), VOID_P_ALIGN, nu_void_p, np_void_p},
Bob Ippolito232f3c92006-05-23 19:12:41 +0000808 {0}
809};
810
811/* Big-endian routines. *****************************************************/
812
813static PyObject *
814bu_int(const char *p, const formatdef *f)
815{
816 long x = 0;
Bob Ippolitoaa70a172006-05-26 20:25:23 +0000817 Py_ssize_t i = f->size;
Bob Ippolito28b26862006-05-29 15:47:29 +0000818 const unsigned char *bytes = (const unsigned char *)p;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000819 do {
Bob Ippolito28b26862006-05-29 15:47:29 +0000820 x = (x<<8) | *bytes++;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000821 } while (--i > 0);
822 /* Extend the sign bit. */
823 if (SIZEOF_LONG > f->size)
Bob Ippolitocd51ca52006-05-27 15:53:49 +0000824 x |= -(x & (1L << ((8 * f->size) - 1)));
Bob Ippolito232f3c92006-05-23 19:12:41 +0000825 return PyInt_FromLong(x);
826}
827
828static PyObject *
829bu_uint(const char *p, const formatdef *f)
830{
831 unsigned long x = 0;
Bob Ippolitoaa70a172006-05-26 20:25:23 +0000832 Py_ssize_t i = f->size;
Bob Ippolito28b26862006-05-29 15:47:29 +0000833 const unsigned char *bytes = (const unsigned char *)p;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000834 do {
Bob Ippolito28b26862006-05-29 15:47:29 +0000835 x = (x<<8) | *bytes++;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000836 } while (--i > 0);
Bob Ippolito04ab9942006-05-25 19:33:38 +0000837 if (x <= LONG_MAX)
Bob Ippolito232f3c92006-05-23 19:12:41 +0000838 return PyInt_FromLong((long)x);
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000839 return PyLong_FromUnsignedLong(x);
Bob Ippolito232f3c92006-05-23 19:12:41 +0000840}
841
842static PyObject *
843bu_longlong(const char *p, const formatdef *f)
844{
Bob Ippolito90bd0a52006-05-27 11:47:12 +0000845#ifdef HAVE_LONG_LONG
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000846 PY_LONG_LONG x = 0;
Bob Ippolitoaa70a172006-05-26 20:25:23 +0000847 Py_ssize_t i = f->size;
Bob Ippolito28b26862006-05-29 15:47:29 +0000848 const unsigned char *bytes = (const unsigned char *)p;
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000849 do {
Bob Ippolito28b26862006-05-29 15:47:29 +0000850 x = (x<<8) | *bytes++;
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000851 } while (--i > 0);
852 /* Extend the sign bit. */
853 if (SIZEOF_LONG_LONG > f->size)
Kristján Valur Jónsson67387fb2007-04-25 00:17:39 +0000854 x |= -(x & ((PY_LONG_LONG)1 << ((8 * f->size) - 1)));
Bob Ippolito04ab9942006-05-25 19:33:38 +0000855 if (x >= LONG_MIN && x <= LONG_MAX)
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000856 return PyInt_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000857 return PyLong_FromLongLong(x);
858#else
Bob Ippolito232f3c92006-05-23 19:12:41 +0000859 return _PyLong_FromByteArray((const unsigned char *)p,
860 8,
861 0, /* little-endian */
862 1 /* signed */);
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000863#endif
Bob Ippolito232f3c92006-05-23 19:12:41 +0000864}
865
866static PyObject *
867bu_ulonglong(const char *p, const formatdef *f)
868{
Bob Ippolito90bd0a52006-05-27 11:47:12 +0000869#ifdef HAVE_LONG_LONG
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000870 unsigned PY_LONG_LONG x = 0;
Bob Ippolitoaa70a172006-05-26 20:25:23 +0000871 Py_ssize_t i = f->size;
Bob Ippolito28b26862006-05-29 15:47:29 +0000872 const unsigned char *bytes = (const unsigned char *)p;
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000873 do {
Bob Ippolito28b26862006-05-29 15:47:29 +0000874 x = (x<<8) | *bytes++;
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000875 } while (--i > 0);
Bob Ippolito04ab9942006-05-25 19:33:38 +0000876 if (x <= LONG_MAX)
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000877 return PyInt_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000878 return PyLong_FromUnsignedLongLong(x);
879#else
Bob Ippolito232f3c92006-05-23 19:12:41 +0000880 return _PyLong_FromByteArray((const unsigned char *)p,
881 8,
882 0, /* little-endian */
883 0 /* signed */);
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000884#endif
Bob Ippolito232f3c92006-05-23 19:12:41 +0000885}
886
887static PyObject *
888bu_float(const char *p, const formatdef *f)
889{
890 return unpack_float(p, 0);
891}
892
893static PyObject *
894bu_double(const char *p, const formatdef *f)
895{
896 return unpack_double(p, 0);
897}
898
Martin v. Löwisaef4c6b2007-01-21 09:33:07 +0000899static PyObject *
900bu_bool(const char *p, const formatdef *f)
901{
902 char x;
903 memcpy((char *)&x, p, sizeof x);
904 return PyBool_FromLong(x != 0);
905}
906
Bob Ippolito232f3c92006-05-23 19:12:41 +0000907static int
908bp_int(char *p, PyObject *v, const formatdef *f)
909{
910 long x;
Bob Ippolitoaa70a172006-05-26 20:25:23 +0000911 Py_ssize_t i;
Bob Ippolito2fd39772006-05-29 22:55:48 +0000912 if (get_wrapped_long(v, &x) < 0)
Bob Ippolito232f3c92006-05-23 19:12:41 +0000913 return -1;
914 i = f->size;
Bob Ippolitocd51ca52006-05-27 15:53:49 +0000915 if (i != SIZEOF_LONG) {
916 if ((i == 2) && (x < -32768 || x > 32767))
Bob Ippolito2fd39772006-05-29 22:55:48 +0000917 RANGE_ERROR(x, f, 0, 0xffffL);
Bob Ippolito90bd0a52006-05-27 11:47:12 +0000918#if (SIZEOF_LONG != 4)
Bob Ippolitocd51ca52006-05-27 15:53:49 +0000919 else if ((i == 4) && (x < -2147483648L || x > 2147483647L))
Bob Ippolito2fd39772006-05-29 22:55:48 +0000920 RANGE_ERROR(x, f, 0, 0xffffffffL);
921#endif
Bob Ippolito4182a752006-05-30 17:37:54 +0000922#ifdef PY_STRUCT_OVERFLOW_MASKING
Bob Ippolito2fd39772006-05-29 22:55:48 +0000923 else if ((i == 1) && (x < -128 || x > 127))
924 RANGE_ERROR(x, f, 0, 0xffL);
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000925#endif
Bob Ippolitocd51ca52006-05-27 15:53:49 +0000926 }
Bob Ippolito232f3c92006-05-23 19:12:41 +0000927 do {
928 p[--i] = (char)x;
929 x >>= 8;
930 } while (i > 0);
931 return 0;
932}
933
934static int
935bp_uint(char *p, PyObject *v, const formatdef *f)
936{
937 unsigned long x;
Bob Ippolitoaa70a172006-05-26 20:25:23 +0000938 Py_ssize_t i;
Bob Ippolito2fd39772006-05-29 22:55:48 +0000939 if (get_wrapped_ulong(v, &x) < 0)
Bob Ippolito232f3c92006-05-23 19:12:41 +0000940 return -1;
941 i = f->size;
Bob Ippolitocd51ca52006-05-27 15:53:49 +0000942 if (i != SIZEOF_LONG) {
943 unsigned long maxint = 1;
944 maxint <<= (unsigned long)(i * 8);
945 if (x >= maxint)
Bob Ippolito2fd39772006-05-29 22:55:48 +0000946 RANGE_ERROR(x, f, 1, maxint - 1);
Bob Ippolitocd51ca52006-05-27 15:53:49 +0000947 }
Bob Ippolito232f3c92006-05-23 19:12:41 +0000948 do {
949 p[--i] = (char)x;
950 x >>= 8;
951 } while (i > 0);
952 return 0;
953}
954
955static int
956bp_longlong(char *p, PyObject *v, const formatdef *f)
957{
958 int res;
959 v = get_pylong(v);
960 if (v == NULL)
961 return -1;
962 res = _PyLong_AsByteArray((PyLongObject *)v,
963 (unsigned char *)p,
964 8,
965 0, /* little_endian */
966 1 /* signed */);
967 Py_DECREF(v);
968 return res;
969}
970
971static int
972bp_ulonglong(char *p, PyObject *v, const formatdef *f)
973{
974 int res;
975 v = get_pylong(v);
976 if (v == NULL)
977 return -1;
978 res = _PyLong_AsByteArray((PyLongObject *)v,
979 (unsigned char *)p,
980 8,
981 0, /* little_endian */
982 0 /* signed */);
983 Py_DECREF(v);
984 return res;
985}
986
987static int
988bp_float(char *p, PyObject *v, const formatdef *f)
989{
990 double x = PyFloat_AsDouble(v);
991 if (x == -1 && PyErr_Occurred()) {
992 PyErr_SetString(StructError,
993 "required argument is not a float");
994 return -1;
995 }
996 return _PyFloat_Pack4(x, (unsigned char *)p, 0);
997}
998
999static int
1000bp_double(char *p, PyObject *v, const formatdef *f)
1001{
1002 double x = PyFloat_AsDouble(v);
1003 if (x == -1 && PyErr_Occurred()) {
1004 PyErr_SetString(StructError,
1005 "required argument is not a float");
1006 return -1;
1007 }
1008 return _PyFloat_Pack8(x, (unsigned char *)p, 0);
1009}
1010
Martin v. Löwisaef4c6b2007-01-21 09:33:07 +00001011static int
1012bp_bool(char *p, PyObject *v, const formatdef *f)
1013{
1014 char y;
1015 y = PyObject_IsTrue(v);
1016 memcpy(p, (char *)&y, sizeof y);
1017 return 0;
1018}
1019
Bob Ippolito232f3c92006-05-23 19:12:41 +00001020static formatdef bigendian_table[] = {
1021 {'x', 1, 0, NULL},
Bob Ippolito4182a752006-05-30 17:37:54 +00001022#ifdef PY_STRUCT_OVERFLOW_MASKING
1023 /* Native packers do range checking without overflow masking. */
Bob Ippolito2fd39772006-05-29 22:55:48 +00001024 {'b', 1, 0, nu_byte, bp_int},
1025 {'B', 1, 0, nu_ubyte, bp_uint},
1026#else
Bob Ippolitoe27337b2006-05-26 13:15:44 +00001027 {'b', 1, 0, nu_byte, np_byte},
1028 {'B', 1, 0, nu_ubyte, np_ubyte},
Bob Ippolito2fd39772006-05-29 22:55:48 +00001029#endif
Bob Ippolito232f3c92006-05-23 19:12:41 +00001030 {'c', 1, 0, nu_char, np_char},
1031 {'s', 1, 0, NULL},
1032 {'p', 1, 0, NULL},
1033 {'h', 2, 0, bu_int, bp_int},
1034 {'H', 2, 0, bu_uint, bp_uint},
1035 {'i', 4, 0, bu_int, bp_int},
1036 {'I', 4, 0, bu_uint, bp_uint},
1037 {'l', 4, 0, bu_int, bp_int},
1038 {'L', 4, 0, bu_uint, bp_uint},
1039 {'q', 8, 0, bu_longlong, bp_longlong},
1040 {'Q', 8, 0, bu_ulonglong, bp_ulonglong},
Thomas Hellerf3c05592008-03-05 15:34:29 +00001041 {'?', 1, 0, bu_bool, bp_bool},
Bob Ippolito232f3c92006-05-23 19:12:41 +00001042 {'f', 4, 0, bu_float, bp_float},
1043 {'d', 8, 0, bu_double, bp_double},
1044 {0}
1045};
1046
1047/* Little-endian routines. *****************************************************/
1048
1049static PyObject *
1050lu_int(const char *p, const formatdef *f)
1051{
1052 long x = 0;
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001053 Py_ssize_t i = f->size;
Bob Ippolito28b26862006-05-29 15:47:29 +00001054 const unsigned char *bytes = (const unsigned char *)p;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001055 do {
Bob Ippolito28b26862006-05-29 15:47:29 +00001056 x = (x<<8) | bytes[--i];
Bob Ippolito232f3c92006-05-23 19:12:41 +00001057 } while (i > 0);
1058 /* Extend the sign bit. */
1059 if (SIZEOF_LONG > f->size)
Bob Ippolitocd51ca52006-05-27 15:53:49 +00001060 x |= -(x & (1L << ((8 * f->size) - 1)));
Bob Ippolito232f3c92006-05-23 19:12:41 +00001061 return PyInt_FromLong(x);
1062}
1063
1064static PyObject *
1065lu_uint(const char *p, const formatdef *f)
1066{
1067 unsigned long x = 0;
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001068 Py_ssize_t i = f->size;
Bob Ippolito28b26862006-05-29 15:47:29 +00001069 const unsigned char *bytes = (const unsigned char *)p;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001070 do {
Bob Ippolito28b26862006-05-29 15:47:29 +00001071 x = (x<<8) | bytes[--i];
Bob Ippolito232f3c92006-05-23 19:12:41 +00001072 } while (i > 0);
Bob Ippolito04ab9942006-05-25 19:33:38 +00001073 if (x <= LONG_MAX)
Bob Ippolito232f3c92006-05-23 19:12:41 +00001074 return PyInt_FromLong((long)x);
Bob Ippolito94f68ee2006-05-25 18:44:50 +00001075 return PyLong_FromUnsignedLong((long)x);
Bob Ippolito232f3c92006-05-23 19:12:41 +00001076}
1077
1078static PyObject *
1079lu_longlong(const char *p, const formatdef *f)
1080{
Bob Ippolito90bd0a52006-05-27 11:47:12 +00001081#ifdef HAVE_LONG_LONG
Bob Ippolito94f68ee2006-05-25 18:44:50 +00001082 PY_LONG_LONG x = 0;
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001083 Py_ssize_t i = f->size;
Bob Ippolito28b26862006-05-29 15:47:29 +00001084 const unsigned char *bytes = (const unsigned char *)p;
Bob Ippolito94f68ee2006-05-25 18:44:50 +00001085 do {
Bob Ippolito28b26862006-05-29 15:47:29 +00001086 x = (x<<8) | bytes[--i];
Bob Ippolito94f68ee2006-05-25 18:44:50 +00001087 } while (i > 0);
1088 /* Extend the sign bit. */
1089 if (SIZEOF_LONG_LONG > f->size)
Kristján Valur Jónsson67387fb2007-04-25 00:17:39 +00001090 x |= -(x & ((PY_LONG_LONG)1 << ((8 * f->size) - 1)));
Bob Ippolito04ab9942006-05-25 19:33:38 +00001091 if (x >= LONG_MIN && x <= LONG_MAX)
Bob Ippolito94f68ee2006-05-25 18:44:50 +00001092 return PyInt_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
Bob Ippolito94f68ee2006-05-25 18:44:50 +00001093 return PyLong_FromLongLong(x);
1094#else
Bob Ippolito232f3c92006-05-23 19:12:41 +00001095 return _PyLong_FromByteArray((const unsigned char *)p,
1096 8,
1097 1, /* little-endian */
1098 1 /* signed */);
Bob Ippolito94f68ee2006-05-25 18:44:50 +00001099#endif
Bob Ippolito232f3c92006-05-23 19:12:41 +00001100}
1101
1102static PyObject *
1103lu_ulonglong(const char *p, const formatdef *f)
1104{
Bob Ippolito90bd0a52006-05-27 11:47:12 +00001105#ifdef HAVE_LONG_LONG
Bob Ippolito94f68ee2006-05-25 18:44:50 +00001106 unsigned PY_LONG_LONG x = 0;
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001107 Py_ssize_t i = f->size;
Bob Ippolito28b26862006-05-29 15:47:29 +00001108 const unsigned char *bytes = (const unsigned char *)p;
Bob Ippolito94f68ee2006-05-25 18:44:50 +00001109 do {
Bob Ippolito28b26862006-05-29 15:47:29 +00001110 x = (x<<8) | bytes[--i];
Bob Ippolito94f68ee2006-05-25 18:44:50 +00001111 } while (i > 0);
Bob Ippolito04ab9942006-05-25 19:33:38 +00001112 if (x <= LONG_MAX)
Bob Ippolito94f68ee2006-05-25 18:44:50 +00001113 return PyInt_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
Bob Ippolito94f68ee2006-05-25 18:44:50 +00001114 return PyLong_FromUnsignedLongLong(x);
1115#else
Bob Ippolito232f3c92006-05-23 19:12:41 +00001116 return _PyLong_FromByteArray((const unsigned char *)p,
1117 8,
1118 1, /* little-endian */
1119 0 /* signed */);
Bob Ippolito94f68ee2006-05-25 18:44:50 +00001120#endif
Bob Ippolito232f3c92006-05-23 19:12:41 +00001121}
1122
1123static PyObject *
1124lu_float(const char *p, const formatdef *f)
1125{
1126 return unpack_float(p, 1);
1127}
1128
1129static PyObject *
1130lu_double(const char *p, const formatdef *f)
1131{
1132 return unpack_double(p, 1);
1133}
1134
1135static int
1136lp_int(char *p, PyObject *v, const formatdef *f)
1137{
1138 long x;
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001139 Py_ssize_t i;
Bob Ippolito2fd39772006-05-29 22:55:48 +00001140 if (get_wrapped_long(v, &x) < 0)
Bob Ippolito232f3c92006-05-23 19:12:41 +00001141 return -1;
1142 i = f->size;
Bob Ippolitocd51ca52006-05-27 15:53:49 +00001143 if (i != SIZEOF_LONG) {
1144 if ((i == 2) && (x < -32768 || x > 32767))
Bob Ippolito2fd39772006-05-29 22:55:48 +00001145 RANGE_ERROR(x, f, 0, 0xffffL);
Bob Ippolito90bd0a52006-05-27 11:47:12 +00001146#if (SIZEOF_LONG != 4)
Bob Ippolitocd51ca52006-05-27 15:53:49 +00001147 else if ((i == 4) && (x < -2147483648L || x > 2147483647L))
Bob Ippolito2fd39772006-05-29 22:55:48 +00001148 RANGE_ERROR(x, f, 0, 0xffffffffL);
1149#endif
Bob Ippolito4182a752006-05-30 17:37:54 +00001150#ifdef PY_STRUCT_OVERFLOW_MASKING
Bob Ippolito2fd39772006-05-29 22:55:48 +00001151 else if ((i == 1) && (x < -128 || x > 127))
1152 RANGE_ERROR(x, f, 0, 0xffL);
Bob Ippolitoe27337b2006-05-26 13:15:44 +00001153#endif
Bob Ippolitocd51ca52006-05-27 15:53:49 +00001154 }
Bob Ippolito232f3c92006-05-23 19:12:41 +00001155 do {
1156 *p++ = (char)x;
1157 x >>= 8;
1158 } while (--i > 0);
1159 return 0;
1160}
1161
1162static int
1163lp_uint(char *p, PyObject *v, const formatdef *f)
1164{
1165 unsigned long x;
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001166 Py_ssize_t i;
Bob Ippolito2fd39772006-05-29 22:55:48 +00001167 if (get_wrapped_ulong(v, &x) < 0)
Bob Ippolito232f3c92006-05-23 19:12:41 +00001168 return -1;
1169 i = f->size;
Bob Ippolitocd51ca52006-05-27 15:53:49 +00001170 if (i != SIZEOF_LONG) {
1171 unsigned long maxint = 1;
1172 maxint <<= (unsigned long)(i * 8);
1173 if (x >= maxint)
Bob Ippolito2fd39772006-05-29 22:55:48 +00001174 RANGE_ERROR(x, f, 1, maxint - 1);
Bob Ippolitocd51ca52006-05-27 15:53:49 +00001175 }
Bob Ippolito232f3c92006-05-23 19:12:41 +00001176 do {
1177 *p++ = (char)x;
1178 x >>= 8;
1179 } while (--i > 0);
1180 return 0;
1181}
1182
1183static int
1184lp_longlong(char *p, PyObject *v, const formatdef *f)
1185{
1186 int res;
1187 v = get_pylong(v);
1188 if (v == NULL)
1189 return -1;
1190 res = _PyLong_AsByteArray((PyLongObject*)v,
1191 (unsigned char *)p,
1192 8,
1193 1, /* little_endian */
1194 1 /* signed */);
1195 Py_DECREF(v);
1196 return res;
1197}
1198
1199static int
1200lp_ulonglong(char *p, PyObject *v, const formatdef *f)
1201{
1202 int res;
1203 v = get_pylong(v);
1204 if (v == NULL)
1205 return -1;
1206 res = _PyLong_AsByteArray((PyLongObject*)v,
1207 (unsigned char *)p,
1208 8,
1209 1, /* little_endian */
1210 0 /* signed */);
1211 Py_DECREF(v);
1212 return res;
1213}
1214
1215static int
1216lp_float(char *p, PyObject *v, const formatdef *f)
1217{
1218 double x = PyFloat_AsDouble(v);
1219 if (x == -1 && PyErr_Occurred()) {
1220 PyErr_SetString(StructError,
1221 "required argument is not a float");
1222 return -1;
1223 }
1224 return _PyFloat_Pack4(x, (unsigned char *)p, 1);
1225}
1226
1227static int
1228lp_double(char *p, PyObject *v, const formatdef *f)
1229{
1230 double x = PyFloat_AsDouble(v);
1231 if (x == -1 && PyErr_Occurred()) {
1232 PyErr_SetString(StructError,
1233 "required argument is not a float");
1234 return -1;
1235 }
1236 return _PyFloat_Pack8(x, (unsigned char *)p, 1);
1237}
1238
1239static formatdef lilendian_table[] = {
1240 {'x', 1, 0, NULL},
Bob Ippolito4182a752006-05-30 17:37:54 +00001241#ifdef PY_STRUCT_OVERFLOW_MASKING
1242 /* Native packers do range checking without overflow masking. */
Bob Ippolito2fd39772006-05-29 22:55:48 +00001243 {'b', 1, 0, nu_byte, lp_int},
1244 {'B', 1, 0, nu_ubyte, lp_uint},
1245#else
Bob Ippolitoe27337b2006-05-26 13:15:44 +00001246 {'b', 1, 0, nu_byte, np_byte},
1247 {'B', 1, 0, nu_ubyte, np_ubyte},
Bob Ippolito2fd39772006-05-29 22:55:48 +00001248#endif
Bob Ippolito232f3c92006-05-23 19:12:41 +00001249 {'c', 1, 0, nu_char, np_char},
1250 {'s', 1, 0, NULL},
1251 {'p', 1, 0, NULL},
1252 {'h', 2, 0, lu_int, lp_int},
1253 {'H', 2, 0, lu_uint, lp_uint},
1254 {'i', 4, 0, lu_int, lp_int},
1255 {'I', 4, 0, lu_uint, lp_uint},
1256 {'l', 4, 0, lu_int, lp_int},
1257 {'L', 4, 0, lu_uint, lp_uint},
1258 {'q', 8, 0, lu_longlong, lp_longlong},
1259 {'Q', 8, 0, lu_ulonglong, lp_ulonglong},
Thomas Hellerf3c05592008-03-05 15:34:29 +00001260 {'?', 1, 0, bu_bool, bp_bool}, /* Std rep not endian dep,
Martin v. Löwisaef4c6b2007-01-21 09:33:07 +00001261 but potentially different from native rep -- reuse bx_bool funcs. */
Bob Ippolito232f3c92006-05-23 19:12:41 +00001262 {'f', 4, 0, lu_float, lp_float},
1263 {'d', 8, 0, lu_double, lp_double},
1264 {0}
1265};
1266
1267
1268static const formatdef *
1269whichtable(char **pfmt)
1270{
1271 const char *fmt = (*pfmt)++; /* May be backed out of later */
1272 switch (*fmt) {
1273 case '<':
1274 return lilendian_table;
1275 case '>':
1276 case '!': /* Network byte order is big-endian */
1277 return bigendian_table;
1278 case '=': { /* Host byte order -- different from native in aligment! */
1279 int n = 1;
1280 char *p = (char *) &n;
1281 if (*p == 1)
1282 return lilendian_table;
1283 else
1284 return bigendian_table;
1285 }
1286 default:
1287 --*pfmt; /* Back out of pointer increment */
1288 /* Fall through */
1289 case '@':
1290 return native_table;
1291 }
1292}
1293
1294
1295/* Get the table entry for a format code */
1296
1297static const formatdef *
1298getentry(int c, const formatdef *f)
1299{
1300 for (; f->format != '\0'; f++) {
1301 if (f->format == c) {
1302 return f;
1303 }
1304 }
1305 PyErr_SetString(StructError, "bad char in struct format");
1306 return NULL;
1307}
1308
1309
1310/* Align a size according to a format code */
1311
1312static int
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001313align(Py_ssize_t size, char c, const formatdef *e)
Bob Ippolito232f3c92006-05-23 19:12:41 +00001314{
1315 if (e->format == c) {
1316 if (e->alignment) {
1317 size = ((size + e->alignment - 1)
1318 / e->alignment)
1319 * e->alignment;
1320 }
1321 }
1322 return size;
1323}
1324
1325
1326/* calculate the size of a format string */
1327
1328static int
1329prepare_s(PyStructObject *self)
1330{
1331 const formatdef *f;
1332 const formatdef *e;
1333 formatcode *codes;
Tim Petersc2b550e2006-05-31 14:28:07 +00001334
Bob Ippolito232f3c92006-05-23 19:12:41 +00001335 const char *s;
1336 const char *fmt;
1337 char c;
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001338 Py_ssize_t size, len, num, itemsize, x;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001339
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001340 fmt = PyString_AS_STRING(self->s_format);
Bob Ippolito232f3c92006-05-23 19:12:41 +00001341
1342 f = whichtable((char **)&fmt);
Tim Petersc2b550e2006-05-31 14:28:07 +00001343
Bob Ippolito232f3c92006-05-23 19:12:41 +00001344 s = fmt;
1345 size = 0;
1346 len = 0;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001347 while ((c = *s++) != '\0') {
1348 if (isspace(Py_CHARMASK(c)))
1349 continue;
1350 if ('0' <= c && c <= '9') {
1351 num = c - '0';
1352 while ('0' <= (c = *s++) && c <= '9') {
1353 x = num*10 + (c - '0');
1354 if (x/10 != num) {
1355 PyErr_SetString(
1356 StructError,
1357 "overflow in item count");
1358 return -1;
1359 }
1360 num = x;
1361 }
1362 if (c == '\0')
1363 break;
1364 }
1365 else
1366 num = 1;
1367
1368 e = getentry(c, f);
1369 if (e == NULL)
1370 return -1;
Tim Petersc2b550e2006-05-31 14:28:07 +00001371
Bob Ippolito232f3c92006-05-23 19:12:41 +00001372 switch (c) {
1373 case 's': /* fall through */
1374 case 'p': len++; break;
1375 case 'x': break;
1376 default: len += num; break;
1377 }
Bob Ippolito232f3c92006-05-23 19:12:41 +00001378
1379 itemsize = e->size;
1380 size = align(size, c, e);
1381 x = num * itemsize;
1382 size += x;
1383 if (x/itemsize != num || size < 0) {
1384 PyErr_SetString(StructError,
1385 "total struct size too long");
1386 return -1;
1387 }
1388 }
1389
Gregory P. Smith9d534572008-06-11 07:41:16 +00001390 /* check for overflow */
1391 if ((len + 1) > (PY_SSIZE_T_MAX / sizeof(formatcode))) {
1392 PyErr_NoMemory();
1393 return -1;
1394 }
1395
Bob Ippolito232f3c92006-05-23 19:12:41 +00001396 self->s_size = size;
1397 self->s_len = len;
Bob Ippolitoeb621272006-05-24 15:32:06 +00001398 codes = PyMem_MALLOC((len + 1) * sizeof(formatcode));
Bob Ippolito232f3c92006-05-23 19:12:41 +00001399 if (codes == NULL) {
1400 PyErr_NoMemory();
1401 return -1;
1402 }
1403 self->s_codes = codes;
Tim Petersc2b550e2006-05-31 14:28:07 +00001404
Bob Ippolito232f3c92006-05-23 19:12:41 +00001405 s = fmt;
1406 size = 0;
1407 while ((c = *s++) != '\0') {
1408 if (isspace(Py_CHARMASK(c)))
1409 continue;
1410 if ('0' <= c && c <= '9') {
1411 num = c - '0';
1412 while ('0' <= (c = *s++) && c <= '9')
1413 num = num*10 + (c - '0');
1414 if (c == '\0')
1415 break;
1416 }
1417 else
1418 num = 1;
1419
1420 e = getentry(c, f);
Tim Petersc2b550e2006-05-31 14:28:07 +00001421
Bob Ippolito232f3c92006-05-23 19:12:41 +00001422 size = align(size, c, e);
Bob Ippolitoeb621272006-05-24 15:32:06 +00001423 if (c == 's' || c == 'p') {
Bob Ippolito232f3c92006-05-23 19:12:41 +00001424 codes->offset = size;
Bob Ippolitoeb621272006-05-24 15:32:06 +00001425 codes->size = num;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001426 codes->fmtdef = e;
1427 codes++;
Bob Ippolitoeb621272006-05-24 15:32:06 +00001428 size += num;
1429 } else if (c == 'x') {
1430 size += num;
1431 } else {
1432 while (--num >= 0) {
1433 codes->offset = size;
1434 codes->size = e->size;
1435 codes->fmtdef = e;
1436 codes++;
1437 size += e->size;
1438 }
Bob Ippolito232f3c92006-05-23 19:12:41 +00001439 }
Bob Ippolito232f3c92006-05-23 19:12:41 +00001440 }
1441 codes->fmtdef = NULL;
Bob Ippolitoeb621272006-05-24 15:32:06 +00001442 codes->offset = size;
1443 codes->size = 0;
Tim Petersc2b550e2006-05-31 14:28:07 +00001444
Bob Ippolito232f3c92006-05-23 19:12:41 +00001445 return 0;
1446}
1447
1448static PyObject *
1449s_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1450{
1451 PyObject *self;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001452
1453 assert(type != NULL && type->tp_alloc != NULL);
1454
1455 self = type->tp_alloc(type, 0);
1456 if (self != NULL) {
1457 PyStructObject *s = (PyStructObject*)self;
1458 Py_INCREF(Py_None);
1459 s->s_format = Py_None;
1460 s->s_codes = NULL;
1461 s->s_size = -1;
1462 s->s_len = -1;
1463 }
1464 return self;
1465}
1466
1467static int
1468s_init(PyObject *self, PyObject *args, PyObject *kwds)
1469{
1470 PyStructObject *soself = (PyStructObject *)self;
1471 PyObject *o_format = NULL;
1472 int ret = 0;
1473 static char *kwlist[] = {"format", 0};
1474
1475 assert(PyStruct_Check(self));
1476
1477 if (!PyArg_ParseTupleAndKeywords(args, kwds, "S:Struct", kwlist,
1478 &o_format))
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001479 return -1;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001480
1481 Py_INCREF(o_format);
Amaury Forgeot d'Arc588ff932008-02-16 14:34:57 +00001482 Py_CLEAR(soself->s_format);
Bob Ippolito232f3c92006-05-23 19:12:41 +00001483 soself->s_format = o_format;
Tim Petersc2b550e2006-05-31 14:28:07 +00001484
Bob Ippolito232f3c92006-05-23 19:12:41 +00001485 ret = prepare_s(soself);
1486 return ret;
1487}
1488
1489static void
1490s_dealloc(PyStructObject *s)
1491{
Bob Ippolito232f3c92006-05-23 19:12:41 +00001492 if (s->weakreflist != NULL)
1493 PyObject_ClearWeakRefs((PyObject *)s);
1494 if (s->s_codes != NULL) {
1495 PyMem_FREE(s->s_codes);
1496 }
1497 Py_XDECREF(s->s_format);
Christian Heimese93237d2007-12-19 02:37:44 +00001498 Py_TYPE(s)->tp_free((PyObject *)s);
Bob Ippolito232f3c92006-05-23 19:12:41 +00001499}
1500
Bob Ippolitoeb621272006-05-24 15:32:06 +00001501static PyObject *
1502s_unpack_internal(PyStructObject *soself, char *startfrom) {
1503 formatcode *code;
1504 Py_ssize_t i = 0;
1505 PyObject *result = PyTuple_New(soself->s_len);
1506 if (result == NULL)
1507 return NULL;
1508
1509 for (code = soself->s_codes; code->fmtdef != NULL; code++) {
1510 PyObject *v;
1511 const formatdef *e = code->fmtdef;
1512 const char *res = startfrom + code->offset;
1513 if (e->format == 's') {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001514 v = PyString_FromStringAndSize(res, code->size);
Bob Ippolitoeb621272006-05-24 15:32:06 +00001515 } else if (e->format == 'p') {
1516 Py_ssize_t n = *(unsigned char*)res;
1517 if (n >= code->size)
1518 n = code->size - 1;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001519 v = PyString_FromStringAndSize(res + 1, n);
Bob Ippolitoeb621272006-05-24 15:32:06 +00001520 } else {
1521 v = e->unpack(res, e);
Bob Ippolitoeb621272006-05-24 15:32:06 +00001522 }
Neal Norwitz3c5431e2006-06-11 05:45:25 +00001523 if (v == NULL)
1524 goto fail;
1525 PyTuple_SET_ITEM(result, i++, v);
Bob Ippolitoeb621272006-05-24 15:32:06 +00001526 }
1527
1528 return result;
1529fail:
1530 Py_DECREF(result);
1531 return NULL;
Bob Ippolitocd51ca52006-05-27 15:53:49 +00001532}
Bob Ippolitoeb621272006-05-24 15:32:06 +00001533
1534
Bob Ippolito232f3c92006-05-23 19:12:41 +00001535PyDoc_STRVAR(s_unpack__doc__,
Bob Ippolito1fcdc232006-05-27 12:11:36 +00001536"S.unpack(str) -> (v1, v2, ...)\n\
Bob Ippolito232f3c92006-05-23 19:12:41 +00001537\n\
1538Return tuple containing values unpacked according to this Struct's format.\n\
1539Requires len(str) == self.size. See struct.__doc__ for more on format\n\
1540strings.");
1541
1542static PyObject *
1543s_unpack(PyObject *self, PyObject *inputstr)
1544{
Raymond Hettinger7a3d41f2007-04-05 18:00:03 +00001545 char *start;
1546 Py_ssize_t len;
1547 PyObject *args=NULL, *result;
Bob Ippolitoeb621272006-05-24 15:32:06 +00001548 PyStructObject *soself = (PyStructObject *)self;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001549 assert(PyStruct_Check(self));
Tim Petersc2b550e2006-05-31 14:28:07 +00001550 assert(soself->s_codes != NULL);
Raymond Hettinger7a3d41f2007-04-05 18:00:03 +00001551 if (inputstr == NULL)
1552 goto fail;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001553 if (PyString_Check(inputstr) &&
1554 PyString_GET_SIZE(inputstr) == soself->s_size) {
1555 return s_unpack_internal(soself, PyString_AS_STRING(inputstr));
Bob Ippolito232f3c92006-05-23 19:12:41 +00001556 }
Raymond Hettinger7a3d41f2007-04-05 18:00:03 +00001557 args = PyTuple_Pack(1, inputstr);
1558 if (args == NULL)
1559 return NULL;
1560 if (!PyArg_ParseTuple(args, "s#:unpack", &start, &len))
1561 goto fail;
1562 if (soself->s_size != len)
1563 goto fail;
1564 result = s_unpack_internal(soself, start);
1565 Py_DECREF(args);
1566 return result;
1567
1568fail:
1569 Py_XDECREF(args);
1570 PyErr_Format(StructError,
1571 "unpack requires a string argument of length %zd",
1572 soself->s_size);
1573 return NULL;
Bob Ippolitoeb621272006-05-24 15:32:06 +00001574}
1575
1576PyDoc_STRVAR(s_unpack_from__doc__,
Bob Ippolito1fcdc232006-05-27 12:11:36 +00001577"S.unpack_from(buffer[, offset]) -> (v1, v2, ...)\n\
Bob Ippolitoeb621272006-05-24 15:32:06 +00001578\n\
1579Return tuple containing values unpacked according to this Struct's format.\n\
1580Unlike unpack, unpack_from can unpack values from any object supporting\n\
1581the buffer API, not just str. Requires len(buffer[offset:]) >= self.size.\n\
1582See struct.__doc__ for more on format strings.");
1583
1584static PyObject *
1585s_unpack_from(PyObject *self, PyObject *args, PyObject *kwds)
1586{
1587 static char *kwlist[] = {"buffer", "offset", 0};
1588#if (PY_VERSION_HEX < 0x02050000)
1589 static char *fmt = "z#|i:unpack_from";
1590#else
1591 static char *fmt = "z#|n:unpack_from";
1592#endif
1593 Py_ssize_t buffer_len = 0, offset = 0;
1594 char *buffer = NULL;
1595 PyStructObject *soself = (PyStructObject *)self;
1596 assert(PyStruct_Check(self));
1597 assert(soself->s_codes != NULL);
1598
1599 if (!PyArg_ParseTupleAndKeywords(args, kwds, fmt, kwlist,
1600 &buffer, &buffer_len, &offset))
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001601 return NULL;
Bob Ippolitoeb621272006-05-24 15:32:06 +00001602
1603 if (buffer == NULL) {
1604 PyErr_Format(StructError,
1605 "unpack_from requires a buffer argument");
Bob Ippolito232f3c92006-05-23 19:12:41 +00001606 return NULL;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001607 }
Tim Petersc2b550e2006-05-31 14:28:07 +00001608
Bob Ippolitoeb621272006-05-24 15:32:06 +00001609 if (offset < 0)
1610 offset += buffer_len;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001611
Bob Ippolitoeb621272006-05-24 15:32:06 +00001612 if (offset < 0 || (buffer_len - offset) < soself->s_size) {
1613 PyErr_Format(StructError,
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001614 "unpack_from requires a buffer of at least %zd bytes",
Bob Ippolitoeb621272006-05-24 15:32:06 +00001615 soself->s_size);
1616 return NULL;
1617 }
1618 return s_unpack_internal(soself, buffer + offset);
1619}
Bob Ippolito232f3c92006-05-23 19:12:41 +00001620
Bob Ippolito232f3c92006-05-23 19:12:41 +00001621
Martin Blais2856e5f2006-05-26 12:03:27 +00001622/*
1623 * Guts of the pack function.
1624 *
1625 * Takes a struct object, a tuple of arguments, and offset in that tuple of
1626 * argument for where to start processing the arguments for packing, and a
1627 * character buffer for writing the packed string. The caller must insure
1628 * that the buffer may contain the required length for packing the arguments.
1629 * 0 is returned on success, 1 is returned if there is an error.
1630 *
1631 */
1632static int
1633s_pack_internal(PyStructObject *soself, PyObject *args, int offset, char* buf)
Bob Ippolito232f3c92006-05-23 19:12:41 +00001634{
Bob Ippolito232f3c92006-05-23 19:12:41 +00001635 formatcode *code;
Neal Norwitz3c5431e2006-06-11 05:45:25 +00001636 /* XXX(nnorwitz): why does i need to be a local? can we use
1637 the offset parameter or do we need the wider width? */
Bob Ippolito232f3c92006-05-23 19:12:41 +00001638 Py_ssize_t i;
Martin Blais2856e5f2006-05-26 12:03:27 +00001639
1640 memset(buf, '\0', soself->s_size);
1641 i = offset;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001642 for (code = soself->s_codes; code->fmtdef != NULL; code++) {
1643 Py_ssize_t n;
Neal Norwitz3c5431e2006-06-11 05:45:25 +00001644 PyObject *v = PyTuple_GET_ITEM(args, i++);
Bob Ippolito232f3c92006-05-23 19:12:41 +00001645 const formatdef *e = code->fmtdef;
Martin Blais2856e5f2006-05-26 12:03:27 +00001646 char *res = buf + code->offset;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001647 if (e->format == 's') {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001648 if (!PyString_Check(v)) {
Bob Ippolito232f3c92006-05-23 19:12:41 +00001649 PyErr_SetString(StructError,
1650 "argument for 's' must be a string");
Martin Blais2856e5f2006-05-26 12:03:27 +00001651 return -1;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001652 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001653 n = PyString_GET_SIZE(v);
Bob Ippolitoeb621272006-05-24 15:32:06 +00001654 if (n > code->size)
1655 n = code->size;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001656 if (n > 0)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001657 memcpy(res, PyString_AS_STRING(v), n);
Bob Ippolito232f3c92006-05-23 19:12:41 +00001658 } else if (e->format == 'p') {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001659 if (!PyString_Check(v)) {
Bob Ippolito232f3c92006-05-23 19:12:41 +00001660 PyErr_SetString(StructError,
1661 "argument for 'p' must be a string");
Martin Blais2856e5f2006-05-26 12:03:27 +00001662 return -1;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001663 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001664 n = PyString_GET_SIZE(v);
Bob Ippolitoeb621272006-05-24 15:32:06 +00001665 if (n > (code->size - 1))
1666 n = code->size - 1;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001667 if (n > 0)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001668 memcpy(res + 1, PyString_AS_STRING(v), n);
Bob Ippolito232f3c92006-05-23 19:12:41 +00001669 if (n > 255)
1670 n = 255;
1671 *res = Py_SAFE_DOWNCAST(n, Py_ssize_t, unsigned char);
1672 } else {
Bob Ippolito2fd39772006-05-29 22:55:48 +00001673 if (e->pack(res, v, e) < 0) {
1674 if (PyLong_Check(v) && PyErr_ExceptionMatches(PyExc_OverflowError))
1675 PyErr_SetString(StructError,
1676 "long too large to convert to int");
Martin Blais2856e5f2006-05-26 12:03:27 +00001677 return -1;
Bob Ippolito2fd39772006-05-29 22:55:48 +00001678 }
Bob Ippolito232f3c92006-05-23 19:12:41 +00001679 }
1680 }
Tim Petersc2b550e2006-05-31 14:28:07 +00001681
Martin Blais2856e5f2006-05-26 12:03:27 +00001682 /* Success */
1683 return 0;
1684}
Bob Ippolito232f3c92006-05-23 19:12:41 +00001685
Martin Blais2856e5f2006-05-26 12:03:27 +00001686
1687PyDoc_STRVAR(s_pack__doc__,
Bob Ippolito1fcdc232006-05-27 12:11:36 +00001688"S.pack(v1, v2, ...) -> string\n\
Martin Blais2856e5f2006-05-26 12:03:27 +00001689\n\
1690Return a string containing values v1, v2, ... packed according to this\n\
1691Struct's format. See struct.__doc__ for more on format strings.");
1692
1693static PyObject *
1694s_pack(PyObject *self, PyObject *args)
1695{
1696 PyStructObject *soself;
1697 PyObject *result;
1698
1699 /* Validate arguments. */
1700 soself = (PyStructObject *)self;
1701 assert(PyStruct_Check(self));
1702 assert(soself->s_codes != NULL);
Martin Blaisaf2ae722006-06-04 13:49:49 +00001703 if (PyTuple_GET_SIZE(args) != soself->s_len)
Martin Blais2856e5f2006-05-26 12:03:27 +00001704 {
1705 PyErr_Format(StructError,
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001706 "pack requires exactly %zd arguments", soself->s_len);
Martin Blais2856e5f2006-05-26 12:03:27 +00001707 return NULL;
1708 }
Tim Petersc2b550e2006-05-31 14:28:07 +00001709
Martin Blais2856e5f2006-05-26 12:03:27 +00001710 /* Allocate a new string */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001711 result = PyString_FromStringAndSize((char *)NULL, soself->s_size);
Martin Blais2856e5f2006-05-26 12:03:27 +00001712 if (result == NULL)
1713 return NULL;
Tim Petersc2b550e2006-05-31 14:28:07 +00001714
Martin Blais2856e5f2006-05-26 12:03:27 +00001715 /* Call the guts */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001716 if ( s_pack_internal(soself, args, 0, PyString_AS_STRING(result)) != 0 ) {
Martin Blais2856e5f2006-05-26 12:03:27 +00001717 Py_DECREF(result);
1718 return NULL;
1719 }
1720
1721 return result;
1722}
1723
Martin Blaisaf2ae722006-06-04 13:49:49 +00001724PyDoc_STRVAR(s_pack_into__doc__,
1725"S.pack_into(buffer, offset, v1, v2, ...)\n\
Martin Blais2856e5f2006-05-26 12:03:27 +00001726\n\
Martin Blaisaf2ae722006-06-04 13:49:49 +00001727Pack the values v1, v2, ... according to this Struct's format, write \n\
Bob Ippolito1fcdc232006-05-27 12:11:36 +00001728the packed bytes into the writable buffer buf starting at offset. Note\n\
1729that the offset is not an optional argument. See struct.__doc__ for \n\
Martin Blais2856e5f2006-05-26 12:03:27 +00001730more on format strings.");
1731
1732static PyObject *
Martin Blaisaf2ae722006-06-04 13:49:49 +00001733s_pack_into(PyObject *self, PyObject *args)
Martin Blais2856e5f2006-05-26 12:03:27 +00001734{
1735 PyStructObject *soself;
1736 char *buffer;
1737 Py_ssize_t buffer_len, offset;
1738
1739 /* Validate arguments. +1 is for the first arg as buffer. */
1740 soself = (PyStructObject *)self;
1741 assert(PyStruct_Check(self));
1742 assert(soself->s_codes != NULL);
Martin Blaisaf2ae722006-06-04 13:49:49 +00001743 if (PyTuple_GET_SIZE(args) != (soself->s_len + 2))
Martin Blais2856e5f2006-05-26 12:03:27 +00001744 {
1745 PyErr_Format(StructError,
Martin Blaisaf2ae722006-06-04 13:49:49 +00001746 "pack_into requires exactly %zd arguments",
Martin Blais2856e5f2006-05-26 12:03:27 +00001747 (soself->s_len + 2));
1748 return NULL;
1749 }
1750
1751 /* Extract a writable memory buffer from the first argument */
Tim Petersc2b550e2006-05-31 14:28:07 +00001752 if ( PyObject_AsWriteBuffer(PyTuple_GET_ITEM(args, 0),
1753 (void**)&buffer, &buffer_len) == -1 ) {
Martin Blais2856e5f2006-05-26 12:03:27 +00001754 return NULL;
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001755 }
1756 assert( buffer_len >= 0 );
Martin Blais2856e5f2006-05-26 12:03:27 +00001757
1758 /* Extract the offset from the first argument */
Martin Blaisaf2ae722006-06-04 13:49:49 +00001759 offset = PyInt_AsSsize_t(PyTuple_GET_ITEM(args, 1));
Benjamin Peterson02252482008-09-30 02:11:07 +00001760 if (offset == -1 && PyErr_Occurred())
1761 return NULL;
Martin Blais2856e5f2006-05-26 12:03:27 +00001762
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001763 /* Support negative offsets. */
Martin Blais2856e5f2006-05-26 12:03:27 +00001764 if (offset < 0)
1765 offset += buffer_len;
1766
1767 /* Check boundaries */
1768 if (offset < 0 || (buffer_len - offset) < soself->s_size) {
1769 PyErr_Format(StructError,
Martin Blaisaf2ae722006-06-04 13:49:49 +00001770 "pack_into requires a buffer of at least %zd bytes",
Martin Blais2856e5f2006-05-26 12:03:27 +00001771 soself->s_size);
1772 return NULL;
1773 }
Tim Petersc2b550e2006-05-31 14:28:07 +00001774
Martin Blais2856e5f2006-05-26 12:03:27 +00001775 /* Call the guts */
1776 if ( s_pack_internal(soself, args, 2, buffer + offset) != 0 ) {
1777 return NULL;
1778 }
1779
Georg Brandlc26025c2006-05-28 21:42:54 +00001780 Py_RETURN_NONE;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001781}
1782
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001783static PyObject *
1784s_get_format(PyStructObject *self, void *unused)
1785{
1786 Py_INCREF(self->s_format);
1787 return self->s_format;
1788}
1789
1790static PyObject *
1791s_get_size(PyStructObject *self, void *unused)
1792{
1793 return PyInt_FromSsize_t(self->s_size);
1794}
Bob Ippolito232f3c92006-05-23 19:12:41 +00001795
1796/* List of functions */
1797
1798static struct PyMethodDef s_methods[] = {
Martin Blaisaf2ae722006-06-04 13:49:49 +00001799 {"pack", s_pack, METH_VARARGS, s_pack__doc__},
1800 {"pack_into", s_pack_into, METH_VARARGS, s_pack_into__doc__},
1801 {"unpack", s_unpack, METH_O, s_unpack__doc__},
Neal Norwitza84dcd72007-05-22 07:16:44 +00001802 {"unpack_from", (PyCFunction)s_unpack_from, METH_VARARGS|METH_KEYWORDS,
Tim Peters5ec2e852006-06-04 15:49:07 +00001803 s_unpack_from__doc__},
Bob Ippolito232f3c92006-05-23 19:12:41 +00001804 {NULL, NULL} /* sentinel */
1805};
1806
1807PyDoc_STRVAR(s__doc__, "Compiled struct object");
1808
1809#define OFF(x) offsetof(PyStructObject, x)
1810
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001811static PyGetSetDef s_getsetlist[] = {
Bob Ippolito1fcdc232006-05-27 12:11:36 +00001812 {"format", (getter)s_get_format, (setter)NULL, "struct format string", NULL},
1813 {"size", (getter)s_get_size, (setter)NULL, "struct size in bytes", NULL},
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001814 {NULL} /* sentinel */
Bob Ippolito232f3c92006-05-23 19:12:41 +00001815};
1816
Bob Ippolito232f3c92006-05-23 19:12:41 +00001817static
1818PyTypeObject PyStructType = {
Martin v. Löwis68192102007-07-21 06:55:02 +00001819 PyVarObject_HEAD_INIT(NULL, 0)
Bob Ippolito232f3c92006-05-23 19:12:41 +00001820 "Struct",
1821 sizeof(PyStructObject),
1822 0,
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001823 (destructor)s_dealloc, /* tp_dealloc */
Bob Ippolito232f3c92006-05-23 19:12:41 +00001824 0, /* tp_print */
1825 0, /* tp_getattr */
1826 0, /* tp_setattr */
1827 0, /* tp_compare */
1828 0, /* tp_repr */
1829 0, /* tp_as_number */
1830 0, /* tp_as_sequence */
1831 0, /* tp_as_mapping */
1832 0, /* tp_hash */
1833 0, /* tp_call */
1834 0, /* tp_str */
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001835 PyObject_GenericGetAttr, /* tp_getattro */
1836 PyObject_GenericSetAttr, /* tp_setattro */
Bob Ippolito232f3c92006-05-23 19:12:41 +00001837 0, /* tp_as_buffer */
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001838 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_WEAKREFS,/* tp_flags */
1839 s__doc__, /* tp_doc */
Bob Ippolito232f3c92006-05-23 19:12:41 +00001840 0, /* tp_traverse */
1841 0, /* tp_clear */
1842 0, /* tp_richcompare */
1843 offsetof(PyStructObject, weakreflist), /* tp_weaklistoffset */
1844 0, /* tp_iter */
1845 0, /* tp_iternext */
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001846 s_methods, /* tp_methods */
1847 NULL, /* tp_members */
1848 s_getsetlist, /* tp_getset */
Bob Ippolito232f3c92006-05-23 19:12:41 +00001849 0, /* tp_base */
1850 0, /* tp_dict */
1851 0, /* tp_descr_get */
1852 0, /* tp_descr_set */
1853 0, /* tp_dictoffset */
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001854 s_init, /* tp_init */
1855 PyType_GenericAlloc,/* tp_alloc */
1856 s_new, /* tp_new */
1857 PyObject_Del, /* tp_free */
Bob Ippolito232f3c92006-05-23 19:12:41 +00001858};
1859
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00001860
1861/* ---- Standalone functions ---- */
1862
1863#define MAXCACHE 100
Christian Heimes76d19f62008-01-04 02:54:42 +00001864static PyObject *cache = NULL;
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00001865
1866static PyObject *
1867cache_struct(PyObject *fmt)
1868{
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00001869 PyObject * s_object;
1870
1871 if (cache == NULL) {
1872 cache = PyDict_New();
1873 if (cache == NULL)
1874 return NULL;
1875 }
1876
1877 s_object = PyDict_GetItem(cache, fmt);
1878 if (s_object != NULL) {
1879 Py_INCREF(s_object);
1880 return s_object;
1881 }
1882
1883 s_object = PyObject_CallFunctionObjArgs((PyObject *)(&PyStructType), fmt, NULL);
1884 if (s_object != NULL) {
1885 if (PyDict_Size(cache) >= MAXCACHE)
1886 PyDict_Clear(cache);
1887 /* Attempt to cache the result */
1888 if (PyDict_SetItem(cache, fmt, s_object) == -1)
1889 PyErr_Clear();
1890 }
1891 return s_object;
1892}
1893
Christian Heimes76d19f62008-01-04 02:54:42 +00001894PyDoc_STRVAR(clearcache_doc,
1895"Clear the internal cache.");
1896
1897static PyObject *
1898clearcache(PyObject *self)
1899{
Raymond Hettinger18e08e52008-01-18 00:10:42 +00001900 Py_CLEAR(cache);
Christian Heimes76d19f62008-01-04 02:54:42 +00001901 Py_RETURN_NONE;
1902}
1903
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00001904PyDoc_STRVAR(calcsize_doc,
1905"Return size of C struct described by format string fmt.");
1906
1907static PyObject *
1908calcsize(PyObject *self, PyObject *fmt)
1909{
1910 Py_ssize_t n;
1911 PyObject *s_object = cache_struct(fmt);
1912 if (s_object == NULL)
1913 return NULL;
1914 n = ((PyStructObject *)s_object)->s_size;
1915 Py_DECREF(s_object);
1916 return PyInt_FromSsize_t(n);
1917}
1918
1919PyDoc_STRVAR(pack_doc,
1920"Return string containing values v1, v2, ... packed according to fmt.");
1921
1922static PyObject *
1923pack(PyObject *self, PyObject *args)
1924{
1925 PyObject *s_object, *fmt, *newargs, *result;
1926 Py_ssize_t n = PyTuple_GET_SIZE(args);
1927
1928 if (n == 0) {
1929 PyErr_SetString(PyExc_TypeError, "missing format argument");
1930 return NULL;
1931 }
1932 fmt = PyTuple_GET_ITEM(args, 0);
1933 newargs = PyTuple_GetSlice(args, 1, n);
1934 if (newargs == NULL)
1935 return NULL;
1936
1937 s_object = cache_struct(fmt);
1938 if (s_object == NULL) {
1939 Py_DECREF(newargs);
1940 return NULL;
1941 }
1942 result = s_pack(s_object, newargs);
1943 Py_DECREF(newargs);
1944 Py_DECREF(s_object);
1945 return result;
1946}
1947
1948PyDoc_STRVAR(pack_into_doc,
1949"Pack the values v1, v2, ... according to fmt.\n\
1950Write the packed bytes into the writable buffer buf starting at offset.");
1951
1952static PyObject *
1953pack_into(PyObject *self, PyObject *args)
1954{
1955 PyObject *s_object, *fmt, *newargs, *result;
1956 Py_ssize_t n = PyTuple_GET_SIZE(args);
1957
1958 if (n == 0) {
1959 PyErr_SetString(PyExc_TypeError, "missing format argument");
1960 return NULL;
1961 }
1962 fmt = PyTuple_GET_ITEM(args, 0);
1963 newargs = PyTuple_GetSlice(args, 1, n);
1964 if (newargs == NULL)
1965 return NULL;
1966
1967 s_object = cache_struct(fmt);
1968 if (s_object == NULL) {
1969 Py_DECREF(newargs);
1970 return NULL;
1971 }
1972 result = s_pack_into(s_object, newargs);
1973 Py_DECREF(newargs);
1974 Py_DECREF(s_object);
1975 return result;
1976}
1977
1978PyDoc_STRVAR(unpack_doc,
1979"Unpack the string containing packed C structure data, according to fmt.\n\
1980Requires len(string) == calcsize(fmt).");
1981
1982static PyObject *
1983unpack(PyObject *self, PyObject *args)
1984{
1985 PyObject *s_object, *fmt, *inputstr, *result;
1986
1987 if (!PyArg_UnpackTuple(args, "unpack", 2, 2, &fmt, &inputstr))
1988 return NULL;
1989
1990 s_object = cache_struct(fmt);
1991 if (s_object == NULL)
1992 return NULL;
1993 result = s_unpack(s_object, inputstr);
1994 Py_DECREF(s_object);
1995 return result;
1996}
1997
1998PyDoc_STRVAR(unpack_from_doc,
1999"Unpack the buffer, containing packed C structure data, according to\n\
2000fmt, starting at offset. Requires len(buffer[offset:]) >= calcsize(fmt).");
2001
2002static PyObject *
2003unpack_from(PyObject *self, PyObject *args, PyObject *kwds)
2004{
2005 PyObject *s_object, *fmt, *newargs, *result;
2006 Py_ssize_t n = PyTuple_GET_SIZE(args);
2007
2008 if (n == 0) {
2009 PyErr_SetString(PyExc_TypeError, "missing format argument");
2010 return NULL;
2011 }
2012 fmt = PyTuple_GET_ITEM(args, 0);
2013 newargs = PyTuple_GetSlice(args, 1, n);
2014 if (newargs == NULL)
2015 return NULL;
2016
2017 s_object = cache_struct(fmt);
2018 if (s_object == NULL) {
2019 Py_DECREF(newargs);
2020 return NULL;
2021 }
2022 result = s_unpack_from(s_object, newargs, kwds);
2023 Py_DECREF(newargs);
2024 Py_DECREF(s_object);
2025 return result;
2026}
2027
2028static struct PyMethodDef module_functions[] = {
Christian Heimes76d19f62008-01-04 02:54:42 +00002029 {"_clearcache", (PyCFunction)clearcache, METH_NOARGS, clearcache_doc},
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00002030 {"calcsize", calcsize, METH_O, calcsize_doc},
2031 {"pack", pack, METH_VARARGS, pack_doc},
2032 {"pack_into", pack_into, METH_VARARGS, pack_into_doc},
2033 {"unpack", unpack, METH_VARARGS, unpack_doc},
2034 {"unpack_from", (PyCFunction)unpack_from,
2035 METH_VARARGS|METH_KEYWORDS, unpack_from_doc},
2036 {NULL, NULL} /* sentinel */
2037};
2038
2039
Bob Ippolito232f3c92006-05-23 19:12:41 +00002040/* Module initialization */
2041
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00002042PyDoc_STRVAR(module_doc,
2043"Functions to convert between Python values and C structs.\n\
2044Python strings are used to hold the data representing the C struct\n\
2045and also as format strings to describe the layout of data in the C struct.\n\
2046\n\
2047The optional first format char indicates byte order, size and alignment:\n\
2048 @: native order, size & alignment (default)\n\
2049 =: native order, std. size & alignment\n\
2050 <: little-endian, std. size & alignment\n\
2051 >: big-endian, std. size & alignment\n\
2052 !: same as >\n\
2053\n\
2054The remaining chars indicate types of args and must match exactly;\n\
2055these can be preceded by a decimal repeat count:\n\
2056 x: pad byte (no data); c:char; b:signed byte; B:unsigned byte;\n\
2057 h:short; H:unsigned short; i:int; I:unsigned int;\n\
2058 l:long; L:unsigned long; f:float; d:double.\n\
2059Special cases (preceding decimal count indicates length):\n\
2060 s:string (array of char); p: pascal string (with count byte).\n\
2061Special case (only available in native format):\n\
2062 P:an integer type that is wide enough to hold a pointer.\n\
2063Special case (not in native mode unless 'long long' in platform C):\n\
2064 q:long long; Q:unsigned long long\n\
2065Whitespace between formats is ignored.\n\
2066\n\
2067The variable struct.error is an exception raised on errors.\n");
2068
Bob Ippolito232f3c92006-05-23 19:12:41 +00002069PyMODINIT_FUNC
2070init_struct(void)
2071{
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00002072 PyObject *ver, *m;
2073
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002074 ver = PyString_FromString("0.2");
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00002075 if (ver == NULL)
2076 return;
2077
2078 m = Py_InitModule3("_struct", module_functions, module_doc);
Bob Ippolito232f3c92006-05-23 19:12:41 +00002079 if (m == NULL)
2080 return;
2081
Christian Heimese93237d2007-12-19 02:37:44 +00002082 Py_TYPE(&PyStructType) = &PyType_Type;
Bob Ippolito3fc2bb92006-05-25 19:03:19 +00002083 if (PyType_Ready(&PyStructType) < 0)
2084 return;
2085
Bob Ippolito4182a752006-05-30 17:37:54 +00002086#ifdef PY_STRUCT_OVERFLOW_MASKING
Bob Ippolito2fd39772006-05-29 22:55:48 +00002087 if (pyint_zero == NULL) {
2088 pyint_zero = PyInt_FromLong(0);
2089 if (pyint_zero == NULL)
2090 return;
2091 }
2092 if (pylong_ulong_mask == NULL) {
2093#if (SIZEOF_LONG == 4)
2094 pylong_ulong_mask = PyLong_FromString("FFFFFFFF", NULL, 16);
2095#else
2096 pylong_ulong_mask = PyLong_FromString("FFFFFFFFFFFFFFFF", NULL, 16);
2097#endif
2098 if (pylong_ulong_mask == NULL)
2099 return;
2100 }
2101
Tim Petersc2b550e2006-05-31 14:28:07 +00002102#else
Bob Ippolito4182a752006-05-30 17:37:54 +00002103 /* This speed trick can't be used until overflow masking goes away, because
2104 native endian always raises exceptions instead of overflow masking. */
Tim Petersc2b550e2006-05-31 14:28:07 +00002105
Bob Ippolitoa99865b2006-05-25 19:56:56 +00002106 /* Check endian and swap in faster functions */
2107 {
2108 int one = 1;
2109 formatdef *native = native_table;
2110 formatdef *other, *ptr;
2111 if ((int)*(unsigned char*)&one)
2112 other = lilendian_table;
2113 else
2114 other = bigendian_table;
Bob Ippolito964e02a2006-05-25 21:09:45 +00002115 /* Scan through the native table, find a matching
2116 entry in the endian table and swap in the
2117 native implementations whenever possible
2118 (64-bit platforms may not have "standard" sizes) */
Bob Ippolitoa99865b2006-05-25 19:56:56 +00002119 while (native->format != '\0' && other->format != '\0') {
2120 ptr = other;
2121 while (ptr->format != '\0') {
2122 if (ptr->format == native->format) {
Bob Ippolito964e02a2006-05-25 21:09:45 +00002123 /* Match faster when formats are
2124 listed in the same order */
Bob Ippolitoa99865b2006-05-25 19:56:56 +00002125 if (ptr == other)
2126 other++;
Tim Petersc2b550e2006-05-31 14:28:07 +00002127 /* Only use the trick if the
Bob Ippolito964e02a2006-05-25 21:09:45 +00002128 size matches */
2129 if (ptr->size != native->size)
2130 break;
2131 /* Skip float and double, could be
2132 "unknown" float format */
2133 if (ptr->format == 'd' || ptr->format == 'f')
2134 break;
2135 ptr->pack = native->pack;
2136 ptr->unpack = native->unpack;
Bob Ippolitoa99865b2006-05-25 19:56:56 +00002137 break;
2138 }
2139 ptr++;
2140 }
2141 native++;
2142 }
2143 }
Bob Ippolito2fd39772006-05-29 22:55:48 +00002144#endif
Tim Petersc2b550e2006-05-31 14:28:07 +00002145
Bob Ippolito232f3c92006-05-23 19:12:41 +00002146 /* Add some symbolic constants to the module */
2147 if (StructError == NULL) {
2148 StructError = PyErr_NewException("struct.error", NULL, NULL);
2149 if (StructError == NULL)
2150 return;
2151 }
Bob Ippolito04ab9942006-05-25 19:33:38 +00002152
Bob Ippolito232f3c92006-05-23 19:12:41 +00002153 Py_INCREF(StructError);
2154 PyModule_AddObject(m, "error", StructError);
Bob Ippolito04ab9942006-05-25 19:33:38 +00002155
Bob Ippolito232f3c92006-05-23 19:12:41 +00002156 Py_INCREF((PyObject*)&PyStructType);
2157 PyModule_AddObject(m, "Struct", (PyObject*)&PyStructType);
Tim Petersc2b550e2006-05-31 14:28:07 +00002158
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00002159 PyModule_AddObject(m, "__version__", ver);
2160
Bob Ippolito2fd39772006-05-29 22:55:48 +00002161 PyModule_AddIntConstant(m, "_PY_STRUCT_RANGE_CHECKING", 1);
Bob Ippolito4182a752006-05-30 17:37:54 +00002162#ifdef PY_STRUCT_OVERFLOW_MASKING
2163 PyModule_AddIntConstant(m, "_PY_STRUCT_OVERFLOW_MASKING", 1);
Bob Ippolito2fd39772006-05-29 22:55:48 +00002164#endif
Bob Ippolitoe6c9f982006-08-04 23:59:21 +00002165#ifdef PY_STRUCT_FLOAT_COERCE
2166 PyModule_AddIntConstant(m, "_PY_STRUCT_FLOAT_COERCE", 1);
2167#endif
2168
Bob Ippolito232f3c92006-05-23 19:12:41 +00002169}