blob: 41cdca706241e519f17c87dd610a45bac0dcafd3 [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
185static int
186get_ulong(PyObject *v, unsigned long *p)
187{
188 if (PyLong_Check(v)) {
189 unsigned long x = PyLong_AsUnsignedLong(v);
190 if (x == (unsigned long)(-1) && PyErr_Occurred())
191 return -1;
192 *p = x;
193 return 0;
194 }
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000195 if (get_long(v, (long *)p) < 0)
196 return -1;
197 if (((long)*p) < 0) {
198 PyErr_SetString(StructError,
199 "unsigned argument is < 0");
200 return -1;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000201 }
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000202 return 0;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000203}
204
205#ifdef HAVE_LONG_LONG
206
207/* Same, but handling native long long. */
208
209static int
210get_longlong(PyObject *v, PY_LONG_LONG *p)
211{
212 PY_LONG_LONG x;
213
214 v = get_pylong(v);
215 if (v == NULL)
216 return -1;
217 assert(PyLong_Check(v));
218 x = PyLong_AsLongLong(v);
219 Py_DECREF(v);
220 if (x == (PY_LONG_LONG)-1 && PyErr_Occurred())
221 return -1;
222 *p = x;
223 return 0;
224}
225
226/* Same, but handling native unsigned long long. */
227
228static int
229get_ulonglong(PyObject *v, unsigned PY_LONG_LONG *p)
230{
231 unsigned PY_LONG_LONG x;
232
233 v = get_pylong(v);
234 if (v == NULL)
235 return -1;
236 assert(PyLong_Check(v));
237 x = PyLong_AsUnsignedLongLong(v);
238 Py_DECREF(v);
239 if (x == (unsigned PY_LONG_LONG)-1 && PyErr_Occurred())
240 return -1;
241 *p = x;
242 return 0;
243}
244
245#endif
246
Bob Ippolito4182a752006-05-30 17:37:54 +0000247#ifdef PY_STRUCT_OVERFLOW_MASKING
Bob Ippolito2fd39772006-05-29 22:55:48 +0000248
249/* Helper routine to get a Python integer and raise the appropriate error
250 if it isn't one */
251
Neal Norwitz07aadb12006-07-30 06:55:48 +0000252#define INT_OVERFLOW "struct integer overflow masking is deprecated"
253
Bob Ippolito2fd39772006-05-29 22:55:48 +0000254static int
255get_wrapped_long(PyObject *v, long *p)
256{
257 if (get_long(v, p) < 0) {
Neal Norwitz3c5431e2006-06-11 05:45:25 +0000258 if (PyLong_Check(v) &&
259 PyErr_ExceptionMatches(PyExc_OverflowError)) {
Bob Ippolito2fd39772006-05-29 22:55:48 +0000260 PyObject *wrapped;
261 long x;
262 PyErr_Clear();
Bob Ippolitoe6c9f982006-08-04 23:59:21 +0000263#ifdef PY_STRUCT_FLOAT_COERCE
264 if (PyFloat_Check(v)) {
265 PyObject *o;
266 int res;
267 PyErr_Clear();
268 if (PyErr_WarnEx(PyExc_DeprecationWarning, FLOAT_COERCE, 2) < 0)
269 return -1;
270 o = PyNumber_Int(v);
271 if (o == NULL)
272 return -1;
273 res = get_wrapped_long(o, p);
274 Py_DECREF(o);
275 return res;
276 }
277#endif
Neal Norwitz07aadb12006-07-30 06:55:48 +0000278 if (PyErr_WarnEx(PyExc_DeprecationWarning, INT_OVERFLOW, 2) < 0)
Bob Ippolito2fd39772006-05-29 22:55:48 +0000279 return -1;
280 wrapped = PyNumber_And(v, pylong_ulong_mask);
281 if (wrapped == NULL)
282 return -1;
283 x = (long)PyLong_AsUnsignedLong(wrapped);
284 Py_DECREF(wrapped);
285 if (x == -1 && PyErr_Occurred())
286 return -1;
287 *p = x;
288 } else {
289 return -1;
290 }
291 }
292 return 0;
293}
294
295static int
296get_wrapped_ulong(PyObject *v, unsigned long *p)
297{
298 long x = (long)PyLong_AsUnsignedLong(v);
299 if (x == -1 && PyErr_Occurred()) {
300 PyObject *wrapped;
301 PyErr_Clear();
Bob Ippolitoe6c9f982006-08-04 23:59:21 +0000302#ifdef PY_STRUCT_FLOAT_COERCE
303 if (PyFloat_Check(v)) {
304 PyObject *o;
305 int res;
306 PyErr_Clear();
307 if (PyErr_WarnEx(PyExc_DeprecationWarning, FLOAT_COERCE, 2) < 0)
308 return -1;
309 o = PyNumber_Int(v);
310 if (o == NULL)
311 return -1;
312 res = get_wrapped_ulong(o, p);
313 Py_DECREF(o);
314 return res;
315 }
316#endif
Bob Ippolito2fd39772006-05-29 22:55:48 +0000317 wrapped = PyNumber_And(v, pylong_ulong_mask);
318 if (wrapped == NULL)
319 return -1;
Neal Norwitz07aadb12006-07-30 06:55:48 +0000320 if (PyErr_WarnEx(PyExc_DeprecationWarning, INT_OVERFLOW, 2) < 0) {
Bob Ippolito2fd39772006-05-29 22:55:48 +0000321 Py_DECREF(wrapped);
322 return -1;
323 }
324 x = (long)PyLong_AsUnsignedLong(wrapped);
325 Py_DECREF(wrapped);
326 if (x == -1 && PyErr_Occurred())
327 return -1;
328 }
329 *p = (unsigned long)x;
330 return 0;
331}
332
333#define RANGE_ERROR(x, f, flag, mask) \
334 do { \
335 if (_range_error(f, flag) < 0) \
336 return -1; \
337 else \
338 (x) &= (mask); \
339 } while (0)
340
341#else
342
343#define get_wrapped_long get_long
344#define get_wrapped_ulong get_ulong
345#define RANGE_ERROR(x, f, flag, mask) return _range_error(f, flag)
346
347#endif
348
Bob Ippolito232f3c92006-05-23 19:12:41 +0000349/* Floating point helpers */
350
351static PyObject *
352unpack_float(const char *p, /* start of 4-byte string */
353 int le) /* true for little-endian, false for big-endian */
354{
355 double x;
356
357 x = _PyFloat_Unpack4((unsigned char *)p, le);
358 if (x == -1.0 && PyErr_Occurred())
359 return NULL;
360 return PyFloat_FromDouble(x);
361}
362
363static PyObject *
364unpack_double(const char *p, /* start of 8-byte string */
365 int le) /* true for little-endian, false for big-endian */
366{
367 double x;
368
369 x = _PyFloat_Unpack8((unsigned char *)p, le);
370 if (x == -1.0 && PyErr_Occurred())
371 return NULL;
372 return PyFloat_FromDouble(x);
373}
374
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000375/* Helper to format the range error exceptions */
376static int
Armin Rigo162997e2006-05-29 17:59:47 +0000377_range_error(const formatdef *f, int is_unsigned)
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000378{
Tim Petersd6a6f022006-05-31 15:33:22 +0000379 /* ulargest is the largest unsigned value with f->size bytes.
380 * Note that the simpler:
381 * ((size_t)1 << (f->size * 8)) - 1
Tim Peters72270c22006-05-31 15:34:37 +0000382 * doesn't work when f->size == sizeof(size_t) because C doesn't
383 * define what happens when a left shift count is >= the number of
384 * bits in the integer being shifted; e.g., on some boxes it doesn't
385 * shift at all when they're equal.
Tim Petersd6a6f022006-05-31 15:33:22 +0000386 */
387 const size_t ulargest = (size_t)-1 >> ((SIZEOF_SIZE_T - f->size)*8);
388 assert(f->size >= 1 && f->size <= SIZEOF_SIZE_T);
389 if (is_unsigned)
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000390 PyErr_Format(StructError,
Neal Norwitz971ea112006-05-31 07:43:27 +0000391 "'%c' format requires 0 <= number <= %zu",
Bob Ippolito28b26862006-05-29 15:47:29 +0000392 f->format,
Tim Petersd6a6f022006-05-31 15:33:22 +0000393 ulargest);
394 else {
395 const Py_ssize_t largest = (Py_ssize_t)(ulargest >> 1);
396 PyErr_Format(StructError,
397 "'%c' format requires %zd <= number <= %zd",
398 f->format,
399 ~ largest,
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000400 largest);
401 }
Bob Ippolito4182a752006-05-30 17:37:54 +0000402#ifdef PY_STRUCT_OVERFLOW_MASKING
Bob Ippolito2fd39772006-05-29 22:55:48 +0000403 {
404 PyObject *ptype, *pvalue, *ptraceback;
405 PyObject *msg;
406 int rval;
407 PyErr_Fetch(&ptype, &pvalue, &ptraceback);
408 assert(pvalue != NULL);
409 msg = PyObject_Str(pvalue);
410 Py_XDECREF(ptype);
411 Py_XDECREF(pvalue);
412 Py_XDECREF(ptraceback);
413 if (msg == NULL)
414 return -1;
Neal Norwitz07aadb12006-07-30 06:55:48 +0000415 rval = PyErr_WarnEx(PyExc_DeprecationWarning,
416 PyString_AS_STRING(msg), 2);
Bob Ippolito2fd39772006-05-29 22:55:48 +0000417 Py_DECREF(msg);
418 if (rval == 0)
419 return 0;
420 }
421#endif
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000422 return -1;
423}
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000424
425
Bob Ippolito232f3c92006-05-23 19:12:41 +0000426
427/* A large number of small routines follow, with names of the form
428
Bob Ippolitoaa70a172006-05-26 20:25:23 +0000429 [bln][up]_TYPE
Bob Ippolito232f3c92006-05-23 19:12:41 +0000430
431 [bln] distiguishes among big-endian, little-endian and native.
432 [pu] distiguishes between pack (to struct) and unpack (from struct).
433 TYPE is one of char, byte, ubyte, etc.
434*/
435
436/* Native mode routines. ****************************************************/
437/* NOTE:
438 In all n[up]_<type> routines handling types larger than 1 byte, there is
439 *no* guarantee that the p pointer is properly aligned for each type,
440 therefore memcpy is called. An intermediate variable is used to
441 compensate for big-endian architectures.
442 Normally both the intermediate variable and the memcpy call will be
443 skipped by C optimisation in little-endian architectures (gcc >= 2.91
444 does this). */
445
446static PyObject *
447nu_char(const char *p, const formatdef *f)
448{
449 return PyString_FromStringAndSize(p, 1);
450}
451
452static PyObject *
453nu_byte(const char *p, const formatdef *f)
454{
455 return PyInt_FromLong((long) *(signed char *)p);
456}
457
458static PyObject *
459nu_ubyte(const char *p, const formatdef *f)
460{
461 return PyInt_FromLong((long) *(unsigned char *)p);
462}
463
464static PyObject *
465nu_short(const char *p, const formatdef *f)
466{
467 short x;
468 memcpy((char *)&x, p, sizeof x);
469 return PyInt_FromLong((long)x);
470}
471
472static PyObject *
473nu_ushort(const char *p, const formatdef *f)
474{
475 unsigned short x;
476 memcpy((char *)&x, p, sizeof x);
477 return PyInt_FromLong((long)x);
478}
479
480static PyObject *
481nu_int(const char *p, const formatdef *f)
482{
483 int x;
484 memcpy((char *)&x, p, sizeof x);
485 return PyInt_FromLong((long)x);
486}
487
488static PyObject *
489nu_uint(const char *p, const formatdef *f)
490{
491 unsigned int x;
492 memcpy((char *)&x, p, sizeof x);
Bob Ippolitocd51ca52006-05-27 15:53:49 +0000493#if (SIZEOF_LONG > SIZEOF_INT)
494 return PyInt_FromLong((long)x);
495#else
496 if (x <= ((unsigned int)LONG_MAX))
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000497 return PyInt_FromLong((long)x);
Bob Ippolito232f3c92006-05-23 19:12:41 +0000498 return PyLong_FromUnsignedLong((unsigned long)x);
Bob Ippolitocd51ca52006-05-27 15:53:49 +0000499#endif
Bob Ippolito232f3c92006-05-23 19:12:41 +0000500}
501
502static PyObject *
503nu_long(const char *p, const formatdef *f)
504{
505 long x;
506 memcpy((char *)&x, p, sizeof x);
507 return PyInt_FromLong(x);
508}
509
510static PyObject *
511nu_ulong(const char *p, const formatdef *f)
512{
513 unsigned long x;
514 memcpy((char *)&x, p, sizeof x);
Bob Ippolito04ab9942006-05-25 19:33:38 +0000515 if (x <= LONG_MAX)
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000516 return PyInt_FromLong((long)x);
Bob Ippolito232f3c92006-05-23 19:12:41 +0000517 return PyLong_FromUnsignedLong(x);
518}
519
520/* Native mode doesn't support q or Q unless the platform C supports
521 long long (or, on Windows, __int64). */
522
523#ifdef HAVE_LONG_LONG
524
525static PyObject *
526nu_longlong(const char *p, const formatdef *f)
527{
528 PY_LONG_LONG x;
529 memcpy((char *)&x, p, sizeof x);
Bob Ippolito04ab9942006-05-25 19:33:38 +0000530 if (x >= LONG_MIN && x <= LONG_MAX)
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000531 return PyInt_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
Bob Ippolito232f3c92006-05-23 19:12:41 +0000532 return PyLong_FromLongLong(x);
533}
534
535static PyObject *
536nu_ulonglong(const char *p, const formatdef *f)
537{
538 unsigned PY_LONG_LONG x;
539 memcpy((char *)&x, p, sizeof x);
Bob Ippolito04ab9942006-05-25 19:33:38 +0000540 if (x <= LONG_MAX)
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000541 return PyInt_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
Bob Ippolito232f3c92006-05-23 19:12:41 +0000542 return PyLong_FromUnsignedLongLong(x);
543}
544
545#endif
546
547static PyObject *
Martin v. Löwisaef4c6b2007-01-21 09:33:07 +0000548nu_bool(const char *p, const formatdef *f)
549{
550 BOOL_TYPE x;
551 memcpy((char *)&x, p, sizeof x);
552 return PyBool_FromLong(x != 0);
553}
554
555
556static PyObject *
Bob Ippolito232f3c92006-05-23 19:12:41 +0000557nu_float(const char *p, const formatdef *f)
558{
559 float x;
560 memcpy((char *)&x, p, sizeof x);
561 return PyFloat_FromDouble((double)x);
562}
563
564static PyObject *
565nu_double(const char *p, const formatdef *f)
566{
567 double x;
568 memcpy((char *)&x, p, sizeof x);
569 return PyFloat_FromDouble(x);
570}
571
572static PyObject *
573nu_void_p(const char *p, const formatdef *f)
574{
575 void *x;
576 memcpy((char *)&x, p, sizeof x);
577 return PyLong_FromVoidPtr(x);
578}
579
580static int
581np_byte(char *p, PyObject *v, const formatdef *f)
582{
583 long x;
584 if (get_long(v, &x) < 0)
585 return -1;
586 if (x < -128 || x > 127){
587 PyErr_SetString(StructError,
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000588 "byte format requires -128 <= number <= 127");
Bob Ippolito232f3c92006-05-23 19:12:41 +0000589 return -1;
590 }
591 *p = (char)x;
592 return 0;
593}
594
595static int
596np_ubyte(char *p, PyObject *v, const formatdef *f)
597{
598 long x;
599 if (get_long(v, &x) < 0)
600 return -1;
601 if (x < 0 || x > 255){
602 PyErr_SetString(StructError,
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000603 "ubyte format requires 0 <= number <= 255");
Bob Ippolito232f3c92006-05-23 19:12:41 +0000604 return -1;
605 }
606 *p = (char)x;
607 return 0;
608}
609
610static int
611np_char(char *p, PyObject *v, const formatdef *f)
612{
613 if (!PyString_Check(v) || PyString_Size(v) != 1) {
614 PyErr_SetString(StructError,
615 "char format require string of length 1");
616 return -1;
617 }
618 *p = *PyString_AsString(v);
619 return 0;
620}
621
622static int
623np_short(char *p, PyObject *v, const formatdef *f)
624{
625 long x;
626 short y;
627 if (get_long(v, &x) < 0)
628 return -1;
629 if (x < SHRT_MIN || x > SHRT_MAX){
630 PyErr_SetString(StructError,
631 "short format requires " STRINGIFY(SHRT_MIN)
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000632 " <= number <= " STRINGIFY(SHRT_MAX));
Bob Ippolito232f3c92006-05-23 19:12:41 +0000633 return -1;
634 }
635 y = (short)x;
636 memcpy(p, (char *)&y, sizeof y);
637 return 0;
638}
639
640static int
641np_ushort(char *p, PyObject *v, const formatdef *f)
642{
643 long x;
644 unsigned short y;
645 if (get_long(v, &x) < 0)
646 return -1;
647 if (x < 0 || x > USHRT_MAX){
648 PyErr_SetString(StructError,
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000649 "short format requires 0 <= number <= " STRINGIFY(USHRT_MAX));
Bob Ippolito232f3c92006-05-23 19:12:41 +0000650 return -1;
651 }
652 y = (unsigned short)x;
653 memcpy(p, (char *)&y, sizeof y);
654 return 0;
655}
656
657static int
658np_int(char *p, PyObject *v, const formatdef *f)
659{
660 long x;
661 int y;
662 if (get_long(v, &x) < 0)
663 return -1;
Bob Ippolito90bd0a52006-05-27 11:47:12 +0000664#if (SIZEOF_LONG > SIZEOF_INT)
Bob Ippolitocd51ca52006-05-27 15:53:49 +0000665 if ((x < ((long)INT_MIN)) || (x > ((long)INT_MAX)))
Bob Ippolito28b26862006-05-29 15:47:29 +0000666 return _range_error(f, 0);
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000667#endif
Bob Ippolito232f3c92006-05-23 19:12:41 +0000668 y = (int)x;
669 memcpy(p, (char *)&y, sizeof y);
670 return 0;
671}
672
673static int
674np_uint(char *p, PyObject *v, const formatdef *f)
675{
676 unsigned long x;
677 unsigned int y;
678 if (get_ulong(v, &x) < 0)
Bob Ippolito28b26862006-05-29 15:47:29 +0000679 return _range_error(f, 1);
Bob Ippolito232f3c92006-05-23 19:12:41 +0000680 y = (unsigned int)x;
Bob Ippolito90bd0a52006-05-27 11:47:12 +0000681#if (SIZEOF_LONG > SIZEOF_INT)
Bob Ippolitocd51ca52006-05-27 15:53:49 +0000682 if (x > ((unsigned long)UINT_MAX))
Bob Ippolito28b26862006-05-29 15:47:29 +0000683 return _range_error(f, 1);
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000684#endif
Bob Ippolito232f3c92006-05-23 19:12:41 +0000685 memcpy(p, (char *)&y, sizeof y);
686 return 0;
687}
688
689static int
690np_long(char *p, PyObject *v, const formatdef *f)
691{
692 long x;
693 if (get_long(v, &x) < 0)
694 return -1;
695 memcpy(p, (char *)&x, sizeof x);
696 return 0;
697}
698
699static int
700np_ulong(char *p, PyObject *v, const formatdef *f)
701{
702 unsigned long x;
703 if (get_ulong(v, &x) < 0)
Bob Ippolito28b26862006-05-29 15:47:29 +0000704 return _range_error(f, 1);
Bob Ippolito232f3c92006-05-23 19:12:41 +0000705 memcpy(p, (char *)&x, sizeof x);
706 return 0;
707}
708
709#ifdef HAVE_LONG_LONG
710
711static int
712np_longlong(char *p, PyObject *v, const formatdef *f)
713{
714 PY_LONG_LONG x;
715 if (get_longlong(v, &x) < 0)
716 return -1;
717 memcpy(p, (char *)&x, sizeof x);
718 return 0;
719}
720
721static int
722np_ulonglong(char *p, PyObject *v, const formatdef *f)
723{
724 unsigned PY_LONG_LONG x;
725 if (get_ulonglong(v, &x) < 0)
726 return -1;
727 memcpy(p, (char *)&x, sizeof x);
728 return 0;
729}
730#endif
731
Martin v. Löwisaef4c6b2007-01-21 09:33:07 +0000732
733static int
734np_bool(char *p, PyObject *v, const formatdef *f)
735{
736 BOOL_TYPE y;
737 y = PyObject_IsTrue(v);
738 memcpy(p, (char *)&y, sizeof y);
739 return 0;
740}
741
Bob Ippolito232f3c92006-05-23 19:12:41 +0000742static int
743np_float(char *p, PyObject *v, const formatdef *f)
744{
745 float x = (float)PyFloat_AsDouble(v);
746 if (x == -1 && PyErr_Occurred()) {
747 PyErr_SetString(StructError,
748 "required argument is not a float");
749 return -1;
750 }
751 memcpy(p, (char *)&x, sizeof x);
752 return 0;
753}
754
755static int
756np_double(char *p, PyObject *v, const formatdef *f)
757{
758 double x = PyFloat_AsDouble(v);
759 if (x == -1 && PyErr_Occurred()) {
760 PyErr_SetString(StructError,
761 "required argument is not a float");
762 return -1;
763 }
764 memcpy(p, (char *)&x, sizeof(double));
765 return 0;
766}
767
768static int
769np_void_p(char *p, PyObject *v, const formatdef *f)
770{
771 void *x;
772
773 v = get_pylong(v);
774 if (v == NULL)
775 return -1;
776 assert(PyLong_Check(v));
777 x = PyLong_AsVoidPtr(v);
778 Py_DECREF(v);
779 if (x == NULL && PyErr_Occurred())
780 return -1;
781 memcpy(p, (char *)&x, sizeof x);
782 return 0;
783}
784
785static formatdef native_table[] = {
786 {'x', sizeof(char), 0, NULL},
787 {'b', sizeof(char), 0, nu_byte, np_byte},
788 {'B', sizeof(char), 0, nu_ubyte, np_ubyte},
789 {'c', sizeof(char), 0, nu_char, np_char},
790 {'s', sizeof(char), 0, NULL},
791 {'p', sizeof(char), 0, NULL},
792 {'h', sizeof(short), SHORT_ALIGN, nu_short, np_short},
793 {'H', sizeof(short), SHORT_ALIGN, nu_ushort, np_ushort},
794 {'i', sizeof(int), INT_ALIGN, nu_int, np_int},
795 {'I', sizeof(int), INT_ALIGN, nu_uint, np_uint},
796 {'l', sizeof(long), LONG_ALIGN, nu_long, np_long},
797 {'L', sizeof(long), LONG_ALIGN, nu_ulong, np_ulong},
Bob Ippolito232f3c92006-05-23 19:12:41 +0000798#ifdef HAVE_LONG_LONG
799 {'q', sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_longlong, np_longlong},
800 {'Q', sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_ulonglong,np_ulonglong},
801#endif
Martin v. Löwisaef4c6b2007-01-21 09:33:07 +0000802 {'t', sizeof(BOOL_TYPE), BOOL_ALIGN, nu_bool, np_bool},
Bob Ippolitoa99865b2006-05-25 19:56:56 +0000803 {'f', sizeof(float), FLOAT_ALIGN, nu_float, np_float},
804 {'d', sizeof(double), DOUBLE_ALIGN, nu_double, np_double},
805 {'P', sizeof(void *), VOID_P_ALIGN, nu_void_p, np_void_p},
Bob Ippolito232f3c92006-05-23 19:12:41 +0000806 {0}
807};
808
809/* Big-endian routines. *****************************************************/
810
811static PyObject *
812bu_int(const char *p, const formatdef *f)
813{
814 long x = 0;
Bob Ippolitoaa70a172006-05-26 20:25:23 +0000815 Py_ssize_t i = f->size;
Bob Ippolito28b26862006-05-29 15:47:29 +0000816 const unsigned char *bytes = (const unsigned char *)p;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000817 do {
Bob Ippolito28b26862006-05-29 15:47:29 +0000818 x = (x<<8) | *bytes++;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000819 } while (--i > 0);
820 /* Extend the sign bit. */
821 if (SIZEOF_LONG > f->size)
Bob Ippolitocd51ca52006-05-27 15:53:49 +0000822 x |= -(x & (1L << ((8 * f->size) - 1)));
Bob Ippolito232f3c92006-05-23 19:12:41 +0000823 return PyInt_FromLong(x);
824}
825
826static PyObject *
827bu_uint(const char *p, const formatdef *f)
828{
829 unsigned long x = 0;
Bob Ippolitoaa70a172006-05-26 20:25:23 +0000830 Py_ssize_t i = f->size;
Bob Ippolito28b26862006-05-29 15:47:29 +0000831 const unsigned char *bytes = (const unsigned char *)p;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000832 do {
Bob Ippolito28b26862006-05-29 15:47:29 +0000833 x = (x<<8) | *bytes++;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000834 } while (--i > 0);
Bob Ippolito04ab9942006-05-25 19:33:38 +0000835 if (x <= LONG_MAX)
Bob Ippolito232f3c92006-05-23 19:12:41 +0000836 return PyInt_FromLong((long)x);
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000837 return PyLong_FromUnsignedLong(x);
Bob Ippolito232f3c92006-05-23 19:12:41 +0000838}
839
840static PyObject *
841bu_longlong(const char *p, const formatdef *f)
842{
Bob Ippolito90bd0a52006-05-27 11:47:12 +0000843#ifdef HAVE_LONG_LONG
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000844 PY_LONG_LONG x = 0;
Bob Ippolitoaa70a172006-05-26 20:25:23 +0000845 Py_ssize_t i = f->size;
Bob Ippolito28b26862006-05-29 15:47:29 +0000846 const unsigned char *bytes = (const unsigned char *)p;
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000847 do {
Bob Ippolito28b26862006-05-29 15:47:29 +0000848 x = (x<<8) | *bytes++;
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000849 } while (--i > 0);
850 /* Extend the sign bit. */
851 if (SIZEOF_LONG_LONG > f->size)
Kristján Valur Jónsson67387fb2007-04-25 00:17:39 +0000852 x |= -(x & ((PY_LONG_LONG)1 << ((8 * f->size) - 1)));
Bob Ippolito04ab9942006-05-25 19:33:38 +0000853 if (x >= LONG_MIN && x <= LONG_MAX)
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000854 return PyInt_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000855 return PyLong_FromLongLong(x);
856#else
Bob Ippolito232f3c92006-05-23 19:12:41 +0000857 return _PyLong_FromByteArray((const unsigned char *)p,
858 8,
859 0, /* little-endian */
860 1 /* signed */);
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000861#endif
Bob Ippolito232f3c92006-05-23 19:12:41 +0000862}
863
864static PyObject *
865bu_ulonglong(const char *p, const formatdef *f)
866{
Bob Ippolito90bd0a52006-05-27 11:47:12 +0000867#ifdef HAVE_LONG_LONG
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000868 unsigned PY_LONG_LONG x = 0;
Bob Ippolitoaa70a172006-05-26 20:25:23 +0000869 Py_ssize_t i = f->size;
Bob Ippolito28b26862006-05-29 15:47:29 +0000870 const unsigned char *bytes = (const unsigned char *)p;
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000871 do {
Bob Ippolito28b26862006-05-29 15:47:29 +0000872 x = (x<<8) | *bytes++;
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000873 } while (--i > 0);
Bob Ippolito04ab9942006-05-25 19:33:38 +0000874 if (x <= LONG_MAX)
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000875 return PyInt_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000876 return PyLong_FromUnsignedLongLong(x);
877#else
Bob Ippolito232f3c92006-05-23 19:12:41 +0000878 return _PyLong_FromByteArray((const unsigned char *)p,
879 8,
880 0, /* little-endian */
881 0 /* signed */);
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000882#endif
Bob Ippolito232f3c92006-05-23 19:12:41 +0000883}
884
885static PyObject *
886bu_float(const char *p, const formatdef *f)
887{
888 return unpack_float(p, 0);
889}
890
891static PyObject *
892bu_double(const char *p, const formatdef *f)
893{
894 return unpack_double(p, 0);
895}
896
Martin v. Löwisaef4c6b2007-01-21 09:33:07 +0000897static PyObject *
898bu_bool(const char *p, const formatdef *f)
899{
900 char x;
901 memcpy((char *)&x, p, sizeof x);
902 return PyBool_FromLong(x != 0);
903}
904
Bob Ippolito232f3c92006-05-23 19:12:41 +0000905static int
906bp_int(char *p, PyObject *v, const formatdef *f)
907{
908 long x;
Bob Ippolitoaa70a172006-05-26 20:25:23 +0000909 Py_ssize_t i;
Bob Ippolito2fd39772006-05-29 22:55:48 +0000910 if (get_wrapped_long(v, &x) < 0)
Bob Ippolito232f3c92006-05-23 19:12:41 +0000911 return -1;
912 i = f->size;
Bob Ippolitocd51ca52006-05-27 15:53:49 +0000913 if (i != SIZEOF_LONG) {
914 if ((i == 2) && (x < -32768 || x > 32767))
Bob Ippolito2fd39772006-05-29 22:55:48 +0000915 RANGE_ERROR(x, f, 0, 0xffffL);
Bob Ippolito90bd0a52006-05-27 11:47:12 +0000916#if (SIZEOF_LONG != 4)
Bob Ippolitocd51ca52006-05-27 15:53:49 +0000917 else if ((i == 4) && (x < -2147483648L || x > 2147483647L))
Bob Ippolito2fd39772006-05-29 22:55:48 +0000918 RANGE_ERROR(x, f, 0, 0xffffffffL);
919#endif
Bob Ippolito4182a752006-05-30 17:37:54 +0000920#ifdef PY_STRUCT_OVERFLOW_MASKING
Bob Ippolito2fd39772006-05-29 22:55:48 +0000921 else if ((i == 1) && (x < -128 || x > 127))
922 RANGE_ERROR(x, f, 0, 0xffL);
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000923#endif
Bob Ippolitocd51ca52006-05-27 15:53:49 +0000924 }
Bob Ippolito232f3c92006-05-23 19:12:41 +0000925 do {
926 p[--i] = (char)x;
927 x >>= 8;
928 } while (i > 0);
929 return 0;
930}
931
932static int
933bp_uint(char *p, PyObject *v, const formatdef *f)
934{
935 unsigned long x;
Bob Ippolitoaa70a172006-05-26 20:25:23 +0000936 Py_ssize_t i;
Bob Ippolito2fd39772006-05-29 22:55:48 +0000937 if (get_wrapped_ulong(v, &x) < 0)
Bob Ippolito232f3c92006-05-23 19:12:41 +0000938 return -1;
939 i = f->size;
Bob Ippolitocd51ca52006-05-27 15:53:49 +0000940 if (i != SIZEOF_LONG) {
941 unsigned long maxint = 1;
942 maxint <<= (unsigned long)(i * 8);
943 if (x >= maxint)
Bob Ippolito2fd39772006-05-29 22:55:48 +0000944 RANGE_ERROR(x, f, 1, maxint - 1);
Bob Ippolitocd51ca52006-05-27 15:53:49 +0000945 }
Bob Ippolito232f3c92006-05-23 19:12:41 +0000946 do {
947 p[--i] = (char)x;
948 x >>= 8;
949 } while (i > 0);
950 return 0;
951}
952
953static int
954bp_longlong(char *p, PyObject *v, const formatdef *f)
955{
956 int res;
957 v = get_pylong(v);
958 if (v == NULL)
959 return -1;
960 res = _PyLong_AsByteArray((PyLongObject *)v,
961 (unsigned char *)p,
962 8,
963 0, /* little_endian */
964 1 /* signed */);
965 Py_DECREF(v);
966 return res;
967}
968
969static int
970bp_ulonglong(char *p, PyObject *v, const formatdef *f)
971{
972 int res;
973 v = get_pylong(v);
974 if (v == NULL)
975 return -1;
976 res = _PyLong_AsByteArray((PyLongObject *)v,
977 (unsigned char *)p,
978 8,
979 0, /* little_endian */
980 0 /* signed */);
981 Py_DECREF(v);
982 return res;
983}
984
985static int
986bp_float(char *p, PyObject *v, const formatdef *f)
987{
988 double x = PyFloat_AsDouble(v);
989 if (x == -1 && PyErr_Occurred()) {
990 PyErr_SetString(StructError,
991 "required argument is not a float");
992 return -1;
993 }
994 return _PyFloat_Pack4(x, (unsigned char *)p, 0);
995}
996
997static int
998bp_double(char *p, PyObject *v, const formatdef *f)
999{
1000 double x = PyFloat_AsDouble(v);
1001 if (x == -1 && PyErr_Occurred()) {
1002 PyErr_SetString(StructError,
1003 "required argument is not a float");
1004 return -1;
1005 }
1006 return _PyFloat_Pack8(x, (unsigned char *)p, 0);
1007}
1008
Martin v. Löwisaef4c6b2007-01-21 09:33:07 +00001009static int
1010bp_bool(char *p, PyObject *v, const formatdef *f)
1011{
1012 char y;
1013 y = PyObject_IsTrue(v);
1014 memcpy(p, (char *)&y, sizeof y);
1015 return 0;
1016}
1017
Bob Ippolito232f3c92006-05-23 19:12:41 +00001018static formatdef bigendian_table[] = {
1019 {'x', 1, 0, NULL},
Bob Ippolito4182a752006-05-30 17:37:54 +00001020#ifdef PY_STRUCT_OVERFLOW_MASKING
1021 /* Native packers do range checking without overflow masking. */
Bob Ippolito2fd39772006-05-29 22:55:48 +00001022 {'b', 1, 0, nu_byte, bp_int},
1023 {'B', 1, 0, nu_ubyte, bp_uint},
1024#else
Bob Ippolitoe27337b2006-05-26 13:15:44 +00001025 {'b', 1, 0, nu_byte, np_byte},
1026 {'B', 1, 0, nu_ubyte, np_ubyte},
Bob Ippolito2fd39772006-05-29 22:55:48 +00001027#endif
Bob Ippolito232f3c92006-05-23 19:12:41 +00001028 {'c', 1, 0, nu_char, np_char},
1029 {'s', 1, 0, NULL},
1030 {'p', 1, 0, NULL},
1031 {'h', 2, 0, bu_int, bp_int},
1032 {'H', 2, 0, bu_uint, bp_uint},
1033 {'i', 4, 0, bu_int, bp_int},
1034 {'I', 4, 0, bu_uint, bp_uint},
1035 {'l', 4, 0, bu_int, bp_int},
1036 {'L', 4, 0, bu_uint, bp_uint},
1037 {'q', 8, 0, bu_longlong, bp_longlong},
1038 {'Q', 8, 0, bu_ulonglong, bp_ulonglong},
Martin v. Löwisaef4c6b2007-01-21 09:33:07 +00001039 {'t', 1, 0, bu_bool, bp_bool},
Bob Ippolito232f3c92006-05-23 19:12:41 +00001040 {'f', 4, 0, bu_float, bp_float},
1041 {'d', 8, 0, bu_double, bp_double},
1042 {0}
1043};
1044
1045/* Little-endian routines. *****************************************************/
1046
1047static PyObject *
1048lu_int(const char *p, const formatdef *f)
1049{
1050 long x = 0;
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001051 Py_ssize_t i = f->size;
Bob Ippolito28b26862006-05-29 15:47:29 +00001052 const unsigned char *bytes = (const unsigned char *)p;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001053 do {
Bob Ippolito28b26862006-05-29 15:47:29 +00001054 x = (x<<8) | bytes[--i];
Bob Ippolito232f3c92006-05-23 19:12:41 +00001055 } while (i > 0);
1056 /* Extend the sign bit. */
1057 if (SIZEOF_LONG > f->size)
Bob Ippolitocd51ca52006-05-27 15:53:49 +00001058 x |= -(x & (1L << ((8 * f->size) - 1)));
Bob Ippolito232f3c92006-05-23 19:12:41 +00001059 return PyInt_FromLong(x);
1060}
1061
1062static PyObject *
1063lu_uint(const char *p, const formatdef *f)
1064{
1065 unsigned long x = 0;
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001066 Py_ssize_t i = f->size;
Bob Ippolito28b26862006-05-29 15:47:29 +00001067 const unsigned char *bytes = (const unsigned char *)p;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001068 do {
Bob Ippolito28b26862006-05-29 15:47:29 +00001069 x = (x<<8) | bytes[--i];
Bob Ippolito232f3c92006-05-23 19:12:41 +00001070 } while (i > 0);
Bob Ippolito04ab9942006-05-25 19:33:38 +00001071 if (x <= LONG_MAX)
Bob Ippolito232f3c92006-05-23 19:12:41 +00001072 return PyInt_FromLong((long)x);
Bob Ippolito94f68ee2006-05-25 18:44:50 +00001073 return PyLong_FromUnsignedLong((long)x);
Bob Ippolito232f3c92006-05-23 19:12:41 +00001074}
1075
1076static PyObject *
1077lu_longlong(const char *p, const formatdef *f)
1078{
Bob Ippolito90bd0a52006-05-27 11:47:12 +00001079#ifdef HAVE_LONG_LONG
Bob Ippolito94f68ee2006-05-25 18:44:50 +00001080 PY_LONG_LONG x = 0;
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001081 Py_ssize_t i = f->size;
Bob Ippolito28b26862006-05-29 15:47:29 +00001082 const unsigned char *bytes = (const unsigned char *)p;
Bob Ippolito94f68ee2006-05-25 18:44:50 +00001083 do {
Bob Ippolito28b26862006-05-29 15:47:29 +00001084 x = (x<<8) | bytes[--i];
Bob Ippolito94f68ee2006-05-25 18:44:50 +00001085 } while (i > 0);
1086 /* Extend the sign bit. */
1087 if (SIZEOF_LONG_LONG > f->size)
Kristján Valur Jónsson67387fb2007-04-25 00:17:39 +00001088 x |= -(x & ((PY_LONG_LONG)1 << ((8 * f->size) - 1)));
Bob Ippolito04ab9942006-05-25 19:33:38 +00001089 if (x >= LONG_MIN && x <= LONG_MAX)
Bob Ippolito94f68ee2006-05-25 18:44:50 +00001090 return PyInt_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
Bob Ippolito94f68ee2006-05-25 18:44:50 +00001091 return PyLong_FromLongLong(x);
1092#else
Bob Ippolito232f3c92006-05-23 19:12:41 +00001093 return _PyLong_FromByteArray((const unsigned char *)p,
1094 8,
1095 1, /* little-endian */
1096 1 /* signed */);
Bob Ippolito94f68ee2006-05-25 18:44:50 +00001097#endif
Bob Ippolito232f3c92006-05-23 19:12:41 +00001098}
1099
1100static PyObject *
1101lu_ulonglong(const char *p, const formatdef *f)
1102{
Bob Ippolito90bd0a52006-05-27 11:47:12 +00001103#ifdef HAVE_LONG_LONG
Bob Ippolito94f68ee2006-05-25 18:44:50 +00001104 unsigned PY_LONG_LONG x = 0;
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001105 Py_ssize_t i = f->size;
Bob Ippolito28b26862006-05-29 15:47:29 +00001106 const unsigned char *bytes = (const unsigned char *)p;
Bob Ippolito94f68ee2006-05-25 18:44:50 +00001107 do {
Bob Ippolito28b26862006-05-29 15:47:29 +00001108 x = (x<<8) | bytes[--i];
Bob Ippolito94f68ee2006-05-25 18:44:50 +00001109 } while (i > 0);
Bob Ippolito04ab9942006-05-25 19:33:38 +00001110 if (x <= LONG_MAX)
Bob Ippolito94f68ee2006-05-25 18:44:50 +00001111 return PyInt_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
Bob Ippolito94f68ee2006-05-25 18:44:50 +00001112 return PyLong_FromUnsignedLongLong(x);
1113#else
Bob Ippolito232f3c92006-05-23 19:12:41 +00001114 return _PyLong_FromByteArray((const unsigned char *)p,
1115 8,
1116 1, /* little-endian */
1117 0 /* signed */);
Bob Ippolito94f68ee2006-05-25 18:44:50 +00001118#endif
Bob Ippolito232f3c92006-05-23 19:12:41 +00001119}
1120
1121static PyObject *
1122lu_float(const char *p, const formatdef *f)
1123{
1124 return unpack_float(p, 1);
1125}
1126
1127static PyObject *
1128lu_double(const char *p, const formatdef *f)
1129{
1130 return unpack_double(p, 1);
1131}
1132
1133static int
1134lp_int(char *p, PyObject *v, const formatdef *f)
1135{
1136 long x;
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001137 Py_ssize_t i;
Bob Ippolito2fd39772006-05-29 22:55:48 +00001138 if (get_wrapped_long(v, &x) < 0)
Bob Ippolito232f3c92006-05-23 19:12:41 +00001139 return -1;
1140 i = f->size;
Bob Ippolitocd51ca52006-05-27 15:53:49 +00001141 if (i != SIZEOF_LONG) {
1142 if ((i == 2) && (x < -32768 || x > 32767))
Bob Ippolito2fd39772006-05-29 22:55:48 +00001143 RANGE_ERROR(x, f, 0, 0xffffL);
Bob Ippolito90bd0a52006-05-27 11:47:12 +00001144#if (SIZEOF_LONG != 4)
Bob Ippolitocd51ca52006-05-27 15:53:49 +00001145 else if ((i == 4) && (x < -2147483648L || x > 2147483647L))
Bob Ippolito2fd39772006-05-29 22:55:48 +00001146 RANGE_ERROR(x, f, 0, 0xffffffffL);
1147#endif
Bob Ippolito4182a752006-05-30 17:37:54 +00001148#ifdef PY_STRUCT_OVERFLOW_MASKING
Bob Ippolito2fd39772006-05-29 22:55:48 +00001149 else if ((i == 1) && (x < -128 || x > 127))
1150 RANGE_ERROR(x, f, 0, 0xffL);
Bob Ippolitoe27337b2006-05-26 13:15:44 +00001151#endif
Bob Ippolitocd51ca52006-05-27 15:53:49 +00001152 }
Bob Ippolito232f3c92006-05-23 19:12:41 +00001153 do {
1154 *p++ = (char)x;
1155 x >>= 8;
1156 } while (--i > 0);
1157 return 0;
1158}
1159
1160static int
1161lp_uint(char *p, PyObject *v, const formatdef *f)
1162{
1163 unsigned long x;
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001164 Py_ssize_t i;
Bob Ippolito2fd39772006-05-29 22:55:48 +00001165 if (get_wrapped_ulong(v, &x) < 0)
Bob Ippolito232f3c92006-05-23 19:12:41 +00001166 return -1;
1167 i = f->size;
Bob Ippolitocd51ca52006-05-27 15:53:49 +00001168 if (i != SIZEOF_LONG) {
1169 unsigned long maxint = 1;
1170 maxint <<= (unsigned long)(i * 8);
1171 if (x >= maxint)
Bob Ippolito2fd39772006-05-29 22:55:48 +00001172 RANGE_ERROR(x, f, 1, maxint - 1);
Bob Ippolitocd51ca52006-05-27 15:53:49 +00001173 }
Bob Ippolito232f3c92006-05-23 19:12:41 +00001174 do {
1175 *p++ = (char)x;
1176 x >>= 8;
1177 } while (--i > 0);
1178 return 0;
1179}
1180
1181static int
1182lp_longlong(char *p, PyObject *v, const formatdef *f)
1183{
1184 int res;
1185 v = get_pylong(v);
1186 if (v == NULL)
1187 return -1;
1188 res = _PyLong_AsByteArray((PyLongObject*)v,
1189 (unsigned char *)p,
1190 8,
1191 1, /* little_endian */
1192 1 /* signed */);
1193 Py_DECREF(v);
1194 return res;
1195}
1196
1197static int
1198lp_ulonglong(char *p, PyObject *v, const formatdef *f)
1199{
1200 int res;
1201 v = get_pylong(v);
1202 if (v == NULL)
1203 return -1;
1204 res = _PyLong_AsByteArray((PyLongObject*)v,
1205 (unsigned char *)p,
1206 8,
1207 1, /* little_endian */
1208 0 /* signed */);
1209 Py_DECREF(v);
1210 return res;
1211}
1212
1213static int
1214lp_float(char *p, PyObject *v, const formatdef *f)
1215{
1216 double x = PyFloat_AsDouble(v);
1217 if (x == -1 && PyErr_Occurred()) {
1218 PyErr_SetString(StructError,
1219 "required argument is not a float");
1220 return -1;
1221 }
1222 return _PyFloat_Pack4(x, (unsigned char *)p, 1);
1223}
1224
1225static int
1226lp_double(char *p, PyObject *v, const formatdef *f)
1227{
1228 double x = PyFloat_AsDouble(v);
1229 if (x == -1 && PyErr_Occurred()) {
1230 PyErr_SetString(StructError,
1231 "required argument is not a float");
1232 return -1;
1233 }
1234 return _PyFloat_Pack8(x, (unsigned char *)p, 1);
1235}
1236
1237static formatdef lilendian_table[] = {
1238 {'x', 1, 0, NULL},
Bob Ippolito4182a752006-05-30 17:37:54 +00001239#ifdef PY_STRUCT_OVERFLOW_MASKING
1240 /* Native packers do range checking without overflow masking. */
Bob Ippolito2fd39772006-05-29 22:55:48 +00001241 {'b', 1, 0, nu_byte, lp_int},
1242 {'B', 1, 0, nu_ubyte, lp_uint},
1243#else
Bob Ippolitoe27337b2006-05-26 13:15:44 +00001244 {'b', 1, 0, nu_byte, np_byte},
1245 {'B', 1, 0, nu_ubyte, np_ubyte},
Bob Ippolito2fd39772006-05-29 22:55:48 +00001246#endif
Bob Ippolito232f3c92006-05-23 19:12:41 +00001247 {'c', 1, 0, nu_char, np_char},
1248 {'s', 1, 0, NULL},
1249 {'p', 1, 0, NULL},
1250 {'h', 2, 0, lu_int, lp_int},
1251 {'H', 2, 0, lu_uint, lp_uint},
1252 {'i', 4, 0, lu_int, lp_int},
1253 {'I', 4, 0, lu_uint, lp_uint},
1254 {'l', 4, 0, lu_int, lp_int},
1255 {'L', 4, 0, lu_uint, lp_uint},
1256 {'q', 8, 0, lu_longlong, lp_longlong},
1257 {'Q', 8, 0, lu_ulonglong, lp_ulonglong},
Martin v. Löwisaef4c6b2007-01-21 09:33:07 +00001258 {'t', 1, 0, bu_bool, bp_bool}, /* Std rep not endian dep,
1259 but potentially different from native rep -- reuse bx_bool funcs. */
Bob Ippolito232f3c92006-05-23 19:12:41 +00001260 {'f', 4, 0, lu_float, lp_float},
1261 {'d', 8, 0, lu_double, lp_double},
1262 {0}
1263};
1264
1265
1266static const formatdef *
1267whichtable(char **pfmt)
1268{
1269 const char *fmt = (*pfmt)++; /* May be backed out of later */
1270 switch (*fmt) {
1271 case '<':
1272 return lilendian_table;
1273 case '>':
1274 case '!': /* Network byte order is big-endian */
1275 return bigendian_table;
1276 case '=': { /* Host byte order -- different from native in aligment! */
1277 int n = 1;
1278 char *p = (char *) &n;
1279 if (*p == 1)
1280 return lilendian_table;
1281 else
1282 return bigendian_table;
1283 }
1284 default:
1285 --*pfmt; /* Back out of pointer increment */
1286 /* Fall through */
1287 case '@':
1288 return native_table;
1289 }
1290}
1291
1292
1293/* Get the table entry for a format code */
1294
1295static const formatdef *
1296getentry(int c, const formatdef *f)
1297{
1298 for (; f->format != '\0'; f++) {
1299 if (f->format == c) {
1300 return f;
1301 }
1302 }
1303 PyErr_SetString(StructError, "bad char in struct format");
1304 return NULL;
1305}
1306
1307
1308/* Align a size according to a format code */
1309
1310static int
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001311align(Py_ssize_t size, char c, const formatdef *e)
Bob Ippolito232f3c92006-05-23 19:12:41 +00001312{
1313 if (e->format == c) {
1314 if (e->alignment) {
1315 size = ((size + e->alignment - 1)
1316 / e->alignment)
1317 * e->alignment;
1318 }
1319 }
1320 return size;
1321}
1322
1323
1324/* calculate the size of a format string */
1325
1326static int
1327prepare_s(PyStructObject *self)
1328{
1329 const formatdef *f;
1330 const formatdef *e;
1331 formatcode *codes;
Tim Petersc2b550e2006-05-31 14:28:07 +00001332
Bob Ippolito232f3c92006-05-23 19:12:41 +00001333 const char *s;
1334 const char *fmt;
1335 char c;
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001336 Py_ssize_t size, len, num, itemsize, x;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001337
1338 fmt = PyString_AS_STRING(self->s_format);
1339
1340 f = whichtable((char **)&fmt);
Tim Petersc2b550e2006-05-31 14:28:07 +00001341
Bob Ippolito232f3c92006-05-23 19:12:41 +00001342 s = fmt;
1343 size = 0;
1344 len = 0;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001345 while ((c = *s++) != '\0') {
1346 if (isspace(Py_CHARMASK(c)))
1347 continue;
1348 if ('0' <= c && c <= '9') {
1349 num = c - '0';
1350 while ('0' <= (c = *s++) && c <= '9') {
1351 x = num*10 + (c - '0');
1352 if (x/10 != num) {
1353 PyErr_SetString(
1354 StructError,
1355 "overflow in item count");
1356 return -1;
1357 }
1358 num = x;
1359 }
1360 if (c == '\0')
1361 break;
1362 }
1363 else
1364 num = 1;
1365
1366 e = getentry(c, f);
1367 if (e == NULL)
1368 return -1;
Tim Petersc2b550e2006-05-31 14:28:07 +00001369
Bob Ippolito232f3c92006-05-23 19:12:41 +00001370 switch (c) {
1371 case 's': /* fall through */
1372 case 'p': len++; break;
1373 case 'x': break;
1374 default: len += num; break;
1375 }
Bob Ippolito232f3c92006-05-23 19:12:41 +00001376
1377 itemsize = e->size;
1378 size = align(size, c, e);
1379 x = num * itemsize;
1380 size += x;
1381 if (x/itemsize != num || size < 0) {
1382 PyErr_SetString(StructError,
1383 "total struct size too long");
1384 return -1;
1385 }
1386 }
1387
1388 self->s_size = size;
1389 self->s_len = len;
Bob Ippolitoeb621272006-05-24 15:32:06 +00001390 codes = PyMem_MALLOC((len + 1) * sizeof(formatcode));
Bob Ippolito232f3c92006-05-23 19:12:41 +00001391 if (codes == NULL) {
1392 PyErr_NoMemory();
1393 return -1;
1394 }
1395 self->s_codes = codes;
Tim Petersc2b550e2006-05-31 14:28:07 +00001396
Bob Ippolito232f3c92006-05-23 19:12:41 +00001397 s = fmt;
1398 size = 0;
1399 while ((c = *s++) != '\0') {
1400 if (isspace(Py_CHARMASK(c)))
1401 continue;
1402 if ('0' <= c && c <= '9') {
1403 num = c - '0';
1404 while ('0' <= (c = *s++) && c <= '9')
1405 num = num*10 + (c - '0');
1406 if (c == '\0')
1407 break;
1408 }
1409 else
1410 num = 1;
1411
1412 e = getentry(c, f);
Tim Petersc2b550e2006-05-31 14:28:07 +00001413
Bob Ippolito232f3c92006-05-23 19:12:41 +00001414 size = align(size, c, e);
Bob Ippolitoeb621272006-05-24 15:32:06 +00001415 if (c == 's' || c == 'p') {
Bob Ippolito232f3c92006-05-23 19:12:41 +00001416 codes->offset = size;
Bob Ippolitoeb621272006-05-24 15:32:06 +00001417 codes->size = num;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001418 codes->fmtdef = e;
1419 codes++;
Bob Ippolitoeb621272006-05-24 15:32:06 +00001420 size += num;
1421 } else if (c == 'x') {
1422 size += num;
1423 } else {
1424 while (--num >= 0) {
1425 codes->offset = size;
1426 codes->size = e->size;
1427 codes->fmtdef = e;
1428 codes++;
1429 size += e->size;
1430 }
Bob Ippolito232f3c92006-05-23 19:12:41 +00001431 }
Bob Ippolito232f3c92006-05-23 19:12:41 +00001432 }
1433 codes->fmtdef = NULL;
Bob Ippolitoeb621272006-05-24 15:32:06 +00001434 codes->offset = size;
1435 codes->size = 0;
Tim Petersc2b550e2006-05-31 14:28:07 +00001436
Bob Ippolito232f3c92006-05-23 19:12:41 +00001437 return 0;
1438}
1439
1440static PyObject *
1441s_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1442{
1443 PyObject *self;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001444
1445 assert(type != NULL && type->tp_alloc != NULL);
1446
1447 self = type->tp_alloc(type, 0);
1448 if (self != NULL) {
1449 PyStructObject *s = (PyStructObject*)self;
1450 Py_INCREF(Py_None);
1451 s->s_format = Py_None;
1452 s->s_codes = NULL;
1453 s->s_size = -1;
1454 s->s_len = -1;
1455 }
1456 return self;
1457}
1458
1459static int
1460s_init(PyObject *self, PyObject *args, PyObject *kwds)
1461{
1462 PyStructObject *soself = (PyStructObject *)self;
1463 PyObject *o_format = NULL;
1464 int ret = 0;
1465 static char *kwlist[] = {"format", 0};
1466
1467 assert(PyStruct_Check(self));
1468
1469 if (!PyArg_ParseTupleAndKeywords(args, kwds, "S:Struct", kwlist,
1470 &o_format))
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001471 return -1;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001472
1473 Py_INCREF(o_format);
1474 Py_XDECREF(soself->s_format);
1475 soself->s_format = o_format;
Tim Petersc2b550e2006-05-31 14:28:07 +00001476
Bob Ippolito232f3c92006-05-23 19:12:41 +00001477 ret = prepare_s(soself);
1478 return ret;
1479}
1480
1481static void
1482s_dealloc(PyStructObject *s)
1483{
Bob Ippolito232f3c92006-05-23 19:12:41 +00001484 if (s->weakreflist != NULL)
1485 PyObject_ClearWeakRefs((PyObject *)s);
1486 if (s->s_codes != NULL) {
1487 PyMem_FREE(s->s_codes);
1488 }
1489 Py_XDECREF(s->s_format);
Christian Heimese93237d2007-12-19 02:37:44 +00001490 Py_TYPE(s)->tp_free((PyObject *)s);
Bob Ippolito232f3c92006-05-23 19:12:41 +00001491}
1492
Bob Ippolitoeb621272006-05-24 15:32:06 +00001493static PyObject *
1494s_unpack_internal(PyStructObject *soself, char *startfrom) {
1495 formatcode *code;
1496 Py_ssize_t i = 0;
1497 PyObject *result = PyTuple_New(soself->s_len);
1498 if (result == NULL)
1499 return NULL;
1500
1501 for (code = soself->s_codes; code->fmtdef != NULL; code++) {
1502 PyObject *v;
1503 const formatdef *e = code->fmtdef;
1504 const char *res = startfrom + code->offset;
1505 if (e->format == 's') {
1506 v = PyString_FromStringAndSize(res, code->size);
Bob Ippolitoeb621272006-05-24 15:32:06 +00001507 } else if (e->format == 'p') {
1508 Py_ssize_t n = *(unsigned char*)res;
1509 if (n >= code->size)
1510 n = code->size - 1;
1511 v = PyString_FromStringAndSize(res + 1, n);
Bob Ippolitoeb621272006-05-24 15:32:06 +00001512 } else {
1513 v = e->unpack(res, e);
Bob Ippolitoeb621272006-05-24 15:32:06 +00001514 }
Neal Norwitz3c5431e2006-06-11 05:45:25 +00001515 if (v == NULL)
1516 goto fail;
1517 PyTuple_SET_ITEM(result, i++, v);
Bob Ippolitoeb621272006-05-24 15:32:06 +00001518 }
1519
1520 return result;
1521fail:
1522 Py_DECREF(result);
1523 return NULL;
Bob Ippolitocd51ca52006-05-27 15:53:49 +00001524}
Bob Ippolitoeb621272006-05-24 15:32:06 +00001525
1526
Bob Ippolito232f3c92006-05-23 19:12:41 +00001527PyDoc_STRVAR(s_unpack__doc__,
Bob Ippolito1fcdc232006-05-27 12:11:36 +00001528"S.unpack(str) -> (v1, v2, ...)\n\
Bob Ippolito232f3c92006-05-23 19:12:41 +00001529\n\
1530Return tuple containing values unpacked according to this Struct's format.\n\
1531Requires len(str) == self.size. See struct.__doc__ for more on format\n\
1532strings.");
1533
1534static PyObject *
1535s_unpack(PyObject *self, PyObject *inputstr)
1536{
Raymond Hettinger7a3d41f2007-04-05 18:00:03 +00001537 char *start;
1538 Py_ssize_t len;
1539 PyObject *args=NULL, *result;
Bob Ippolitoeb621272006-05-24 15:32:06 +00001540 PyStructObject *soself = (PyStructObject *)self;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001541 assert(PyStruct_Check(self));
Tim Petersc2b550e2006-05-31 14:28:07 +00001542 assert(soself->s_codes != NULL);
Raymond Hettinger7a3d41f2007-04-05 18:00:03 +00001543 if (inputstr == NULL)
1544 goto fail;
1545 if (PyString_Check(inputstr) &&
1546 PyString_GET_SIZE(inputstr) == soself->s_size) {
1547 return s_unpack_internal(soself, PyString_AS_STRING(inputstr));
Bob Ippolito232f3c92006-05-23 19:12:41 +00001548 }
Raymond Hettinger7a3d41f2007-04-05 18:00:03 +00001549 args = PyTuple_Pack(1, inputstr);
1550 if (args == NULL)
1551 return NULL;
1552 if (!PyArg_ParseTuple(args, "s#:unpack", &start, &len))
1553 goto fail;
1554 if (soself->s_size != len)
1555 goto fail;
1556 result = s_unpack_internal(soself, start);
1557 Py_DECREF(args);
1558 return result;
1559
1560fail:
1561 Py_XDECREF(args);
1562 PyErr_Format(StructError,
1563 "unpack requires a string argument of length %zd",
1564 soself->s_size);
1565 return NULL;
Bob Ippolitoeb621272006-05-24 15:32:06 +00001566}
1567
1568PyDoc_STRVAR(s_unpack_from__doc__,
Bob Ippolito1fcdc232006-05-27 12:11:36 +00001569"S.unpack_from(buffer[, offset]) -> (v1, v2, ...)\n\
Bob Ippolitoeb621272006-05-24 15:32:06 +00001570\n\
1571Return tuple containing values unpacked according to this Struct's format.\n\
1572Unlike unpack, unpack_from can unpack values from any object supporting\n\
1573the buffer API, not just str. Requires len(buffer[offset:]) >= self.size.\n\
1574See struct.__doc__ for more on format strings.");
1575
1576static PyObject *
1577s_unpack_from(PyObject *self, PyObject *args, PyObject *kwds)
1578{
1579 static char *kwlist[] = {"buffer", "offset", 0};
1580#if (PY_VERSION_HEX < 0x02050000)
1581 static char *fmt = "z#|i:unpack_from";
1582#else
1583 static char *fmt = "z#|n:unpack_from";
1584#endif
1585 Py_ssize_t buffer_len = 0, offset = 0;
1586 char *buffer = NULL;
1587 PyStructObject *soself = (PyStructObject *)self;
1588 assert(PyStruct_Check(self));
1589 assert(soself->s_codes != NULL);
1590
1591 if (!PyArg_ParseTupleAndKeywords(args, kwds, fmt, kwlist,
1592 &buffer, &buffer_len, &offset))
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001593 return NULL;
Bob Ippolitoeb621272006-05-24 15:32:06 +00001594
1595 if (buffer == NULL) {
1596 PyErr_Format(StructError,
1597 "unpack_from requires a buffer argument");
Bob Ippolito232f3c92006-05-23 19:12:41 +00001598 return NULL;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001599 }
Tim Petersc2b550e2006-05-31 14:28:07 +00001600
Bob Ippolitoeb621272006-05-24 15:32:06 +00001601 if (offset < 0)
1602 offset += buffer_len;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001603
Bob Ippolitoeb621272006-05-24 15:32:06 +00001604 if (offset < 0 || (buffer_len - offset) < soself->s_size) {
1605 PyErr_Format(StructError,
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001606 "unpack_from requires a buffer of at least %zd bytes",
Bob Ippolitoeb621272006-05-24 15:32:06 +00001607 soself->s_size);
1608 return NULL;
1609 }
1610 return s_unpack_internal(soself, buffer + offset);
1611}
Bob Ippolito232f3c92006-05-23 19:12:41 +00001612
Bob Ippolito232f3c92006-05-23 19:12:41 +00001613
Martin Blais2856e5f2006-05-26 12:03:27 +00001614/*
1615 * Guts of the pack function.
1616 *
1617 * Takes a struct object, a tuple of arguments, and offset in that tuple of
1618 * argument for where to start processing the arguments for packing, and a
1619 * character buffer for writing the packed string. The caller must insure
1620 * that the buffer may contain the required length for packing the arguments.
1621 * 0 is returned on success, 1 is returned if there is an error.
1622 *
1623 */
1624static int
1625s_pack_internal(PyStructObject *soself, PyObject *args, int offset, char* buf)
Bob Ippolito232f3c92006-05-23 19:12:41 +00001626{
Bob Ippolito232f3c92006-05-23 19:12:41 +00001627 formatcode *code;
Neal Norwitz3c5431e2006-06-11 05:45:25 +00001628 /* XXX(nnorwitz): why does i need to be a local? can we use
1629 the offset parameter or do we need the wider width? */
Bob Ippolito232f3c92006-05-23 19:12:41 +00001630 Py_ssize_t i;
Martin Blais2856e5f2006-05-26 12:03:27 +00001631
1632 memset(buf, '\0', soself->s_size);
1633 i = offset;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001634 for (code = soself->s_codes; code->fmtdef != NULL; code++) {
1635 Py_ssize_t n;
Neal Norwitz3c5431e2006-06-11 05:45:25 +00001636 PyObject *v = PyTuple_GET_ITEM(args, i++);
Bob Ippolito232f3c92006-05-23 19:12:41 +00001637 const formatdef *e = code->fmtdef;
Martin Blais2856e5f2006-05-26 12:03:27 +00001638 char *res = buf + code->offset;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001639 if (e->format == 's') {
Bob Ippolito232f3c92006-05-23 19:12:41 +00001640 if (!PyString_Check(v)) {
1641 PyErr_SetString(StructError,
1642 "argument for 's' must be a string");
Martin Blais2856e5f2006-05-26 12:03:27 +00001643 return -1;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001644 }
1645 n = PyString_GET_SIZE(v);
Bob Ippolitoeb621272006-05-24 15:32:06 +00001646 if (n > code->size)
1647 n = code->size;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001648 if (n > 0)
1649 memcpy(res, PyString_AS_STRING(v), n);
1650 } else if (e->format == 'p') {
Bob Ippolito232f3c92006-05-23 19:12:41 +00001651 if (!PyString_Check(v)) {
1652 PyErr_SetString(StructError,
1653 "argument for 'p' must be a string");
Martin Blais2856e5f2006-05-26 12:03:27 +00001654 return -1;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001655 }
1656 n = PyString_GET_SIZE(v);
Bob Ippolitoeb621272006-05-24 15:32:06 +00001657 if (n > (code->size - 1))
1658 n = code->size - 1;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001659 if (n > 0)
1660 memcpy(res + 1, PyString_AS_STRING(v), n);
1661 if (n > 255)
1662 n = 255;
1663 *res = Py_SAFE_DOWNCAST(n, Py_ssize_t, unsigned char);
1664 } else {
Bob Ippolito2fd39772006-05-29 22:55:48 +00001665 if (e->pack(res, v, e) < 0) {
1666 if (PyLong_Check(v) && PyErr_ExceptionMatches(PyExc_OverflowError))
1667 PyErr_SetString(StructError,
1668 "long too large to convert to int");
Martin Blais2856e5f2006-05-26 12:03:27 +00001669 return -1;
Bob Ippolito2fd39772006-05-29 22:55:48 +00001670 }
Bob Ippolito232f3c92006-05-23 19:12:41 +00001671 }
1672 }
Tim Petersc2b550e2006-05-31 14:28:07 +00001673
Martin Blais2856e5f2006-05-26 12:03:27 +00001674 /* Success */
1675 return 0;
1676}
Bob Ippolito232f3c92006-05-23 19:12:41 +00001677
Martin Blais2856e5f2006-05-26 12:03:27 +00001678
1679PyDoc_STRVAR(s_pack__doc__,
Bob Ippolito1fcdc232006-05-27 12:11:36 +00001680"S.pack(v1, v2, ...) -> string\n\
Martin Blais2856e5f2006-05-26 12:03:27 +00001681\n\
1682Return a string containing values v1, v2, ... packed according to this\n\
1683Struct's format. See struct.__doc__ for more on format strings.");
1684
1685static PyObject *
1686s_pack(PyObject *self, PyObject *args)
1687{
1688 PyStructObject *soself;
1689 PyObject *result;
1690
1691 /* Validate arguments. */
1692 soself = (PyStructObject *)self;
1693 assert(PyStruct_Check(self));
1694 assert(soself->s_codes != NULL);
Martin Blaisaf2ae722006-06-04 13:49:49 +00001695 if (PyTuple_GET_SIZE(args) != soself->s_len)
Martin Blais2856e5f2006-05-26 12:03:27 +00001696 {
1697 PyErr_Format(StructError,
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001698 "pack requires exactly %zd arguments", soself->s_len);
Martin Blais2856e5f2006-05-26 12:03:27 +00001699 return NULL;
1700 }
Tim Petersc2b550e2006-05-31 14:28:07 +00001701
Martin Blais2856e5f2006-05-26 12:03:27 +00001702 /* Allocate a new string */
1703 result = PyString_FromStringAndSize((char *)NULL, soself->s_size);
1704 if (result == NULL)
1705 return NULL;
Tim Petersc2b550e2006-05-31 14:28:07 +00001706
Martin Blais2856e5f2006-05-26 12:03:27 +00001707 /* Call the guts */
1708 if ( s_pack_internal(soself, args, 0, PyString_AS_STRING(result)) != 0 ) {
1709 Py_DECREF(result);
1710 return NULL;
1711 }
1712
1713 return result;
1714}
1715
Martin Blaisaf2ae722006-06-04 13:49:49 +00001716PyDoc_STRVAR(s_pack_into__doc__,
1717"S.pack_into(buffer, offset, v1, v2, ...)\n\
Martin Blais2856e5f2006-05-26 12:03:27 +00001718\n\
Martin Blaisaf2ae722006-06-04 13:49:49 +00001719Pack the values v1, v2, ... according to this Struct's format, write \n\
Bob Ippolito1fcdc232006-05-27 12:11:36 +00001720the packed bytes into the writable buffer buf starting at offset. Note\n\
1721that the offset is not an optional argument. See struct.__doc__ for \n\
Martin Blais2856e5f2006-05-26 12:03:27 +00001722more on format strings.");
1723
1724static PyObject *
Martin Blaisaf2ae722006-06-04 13:49:49 +00001725s_pack_into(PyObject *self, PyObject *args)
Martin Blais2856e5f2006-05-26 12:03:27 +00001726{
1727 PyStructObject *soself;
1728 char *buffer;
1729 Py_ssize_t buffer_len, offset;
1730
1731 /* Validate arguments. +1 is for the first arg as buffer. */
1732 soself = (PyStructObject *)self;
1733 assert(PyStruct_Check(self));
1734 assert(soself->s_codes != NULL);
Martin Blaisaf2ae722006-06-04 13:49:49 +00001735 if (PyTuple_GET_SIZE(args) != (soself->s_len + 2))
Martin Blais2856e5f2006-05-26 12:03:27 +00001736 {
1737 PyErr_Format(StructError,
Martin Blaisaf2ae722006-06-04 13:49:49 +00001738 "pack_into requires exactly %zd arguments",
Martin Blais2856e5f2006-05-26 12:03:27 +00001739 (soself->s_len + 2));
1740 return NULL;
1741 }
1742
1743 /* Extract a writable memory buffer from the first argument */
Tim Petersc2b550e2006-05-31 14:28:07 +00001744 if ( PyObject_AsWriteBuffer(PyTuple_GET_ITEM(args, 0),
1745 (void**)&buffer, &buffer_len) == -1 ) {
Martin Blais2856e5f2006-05-26 12:03:27 +00001746 return NULL;
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001747 }
1748 assert( buffer_len >= 0 );
Martin Blais2856e5f2006-05-26 12:03:27 +00001749
1750 /* Extract the offset from the first argument */
Martin Blaisaf2ae722006-06-04 13:49:49 +00001751 offset = PyInt_AsSsize_t(PyTuple_GET_ITEM(args, 1));
Martin Blais2856e5f2006-05-26 12:03:27 +00001752
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001753 /* Support negative offsets. */
Martin Blais2856e5f2006-05-26 12:03:27 +00001754 if (offset < 0)
1755 offset += buffer_len;
1756
1757 /* Check boundaries */
1758 if (offset < 0 || (buffer_len - offset) < soself->s_size) {
1759 PyErr_Format(StructError,
Martin Blaisaf2ae722006-06-04 13:49:49 +00001760 "pack_into requires a buffer of at least %zd bytes",
Martin Blais2856e5f2006-05-26 12:03:27 +00001761 soself->s_size);
1762 return NULL;
1763 }
Tim Petersc2b550e2006-05-31 14:28:07 +00001764
Martin Blais2856e5f2006-05-26 12:03:27 +00001765 /* Call the guts */
1766 if ( s_pack_internal(soself, args, 2, buffer + offset) != 0 ) {
1767 return NULL;
1768 }
1769
Georg Brandlc26025c2006-05-28 21:42:54 +00001770 Py_RETURN_NONE;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001771}
1772
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001773static PyObject *
1774s_get_format(PyStructObject *self, void *unused)
1775{
1776 Py_INCREF(self->s_format);
1777 return self->s_format;
1778}
1779
1780static PyObject *
1781s_get_size(PyStructObject *self, void *unused)
1782{
1783 return PyInt_FromSsize_t(self->s_size);
1784}
Bob Ippolito232f3c92006-05-23 19:12:41 +00001785
1786/* List of functions */
1787
1788static struct PyMethodDef s_methods[] = {
Martin Blaisaf2ae722006-06-04 13:49:49 +00001789 {"pack", s_pack, METH_VARARGS, s_pack__doc__},
1790 {"pack_into", s_pack_into, METH_VARARGS, s_pack_into__doc__},
1791 {"unpack", s_unpack, METH_O, s_unpack__doc__},
Neal Norwitza84dcd72007-05-22 07:16:44 +00001792 {"unpack_from", (PyCFunction)s_unpack_from, METH_VARARGS|METH_KEYWORDS,
Tim Peters5ec2e852006-06-04 15:49:07 +00001793 s_unpack_from__doc__},
Bob Ippolito232f3c92006-05-23 19:12:41 +00001794 {NULL, NULL} /* sentinel */
1795};
1796
1797PyDoc_STRVAR(s__doc__, "Compiled struct object");
1798
1799#define OFF(x) offsetof(PyStructObject, x)
1800
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001801static PyGetSetDef s_getsetlist[] = {
Bob Ippolito1fcdc232006-05-27 12:11:36 +00001802 {"format", (getter)s_get_format, (setter)NULL, "struct format string", NULL},
1803 {"size", (getter)s_get_size, (setter)NULL, "struct size in bytes", NULL},
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001804 {NULL} /* sentinel */
Bob Ippolito232f3c92006-05-23 19:12:41 +00001805};
1806
Bob Ippolito232f3c92006-05-23 19:12:41 +00001807static
1808PyTypeObject PyStructType = {
Martin v. Löwis68192102007-07-21 06:55:02 +00001809 PyVarObject_HEAD_INIT(NULL, 0)
Bob Ippolito232f3c92006-05-23 19:12:41 +00001810 "Struct",
1811 sizeof(PyStructObject),
1812 0,
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001813 (destructor)s_dealloc, /* tp_dealloc */
Bob Ippolito232f3c92006-05-23 19:12:41 +00001814 0, /* tp_print */
1815 0, /* tp_getattr */
1816 0, /* tp_setattr */
1817 0, /* tp_compare */
1818 0, /* tp_repr */
1819 0, /* tp_as_number */
1820 0, /* tp_as_sequence */
1821 0, /* tp_as_mapping */
1822 0, /* tp_hash */
1823 0, /* tp_call */
1824 0, /* tp_str */
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001825 PyObject_GenericGetAttr, /* tp_getattro */
1826 PyObject_GenericSetAttr, /* tp_setattro */
Bob Ippolito232f3c92006-05-23 19:12:41 +00001827 0, /* tp_as_buffer */
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001828 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_WEAKREFS,/* tp_flags */
1829 s__doc__, /* tp_doc */
Bob Ippolito232f3c92006-05-23 19:12:41 +00001830 0, /* tp_traverse */
1831 0, /* tp_clear */
1832 0, /* tp_richcompare */
1833 offsetof(PyStructObject, weakreflist), /* tp_weaklistoffset */
1834 0, /* tp_iter */
1835 0, /* tp_iternext */
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001836 s_methods, /* tp_methods */
1837 NULL, /* tp_members */
1838 s_getsetlist, /* tp_getset */
Bob Ippolito232f3c92006-05-23 19:12:41 +00001839 0, /* tp_base */
1840 0, /* tp_dict */
1841 0, /* tp_descr_get */
1842 0, /* tp_descr_set */
1843 0, /* tp_dictoffset */
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001844 s_init, /* tp_init */
1845 PyType_GenericAlloc,/* tp_alloc */
1846 s_new, /* tp_new */
1847 PyObject_Del, /* tp_free */
Bob Ippolito232f3c92006-05-23 19:12:41 +00001848};
1849
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00001850
1851/* ---- Standalone functions ---- */
1852
1853#define MAXCACHE 100
Christian Heimes76d19f62008-01-04 02:54:42 +00001854static PyObject *cache = NULL;
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00001855
1856static PyObject *
1857cache_struct(PyObject *fmt)
1858{
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00001859 PyObject * s_object;
1860
1861 if (cache == NULL) {
1862 cache = PyDict_New();
1863 if (cache == NULL)
1864 return NULL;
1865 }
1866
1867 s_object = PyDict_GetItem(cache, fmt);
1868 if (s_object != NULL) {
1869 Py_INCREF(s_object);
1870 return s_object;
1871 }
1872
1873 s_object = PyObject_CallFunctionObjArgs((PyObject *)(&PyStructType), fmt, NULL);
1874 if (s_object != NULL) {
1875 if (PyDict_Size(cache) >= MAXCACHE)
1876 PyDict_Clear(cache);
1877 /* Attempt to cache the result */
1878 if (PyDict_SetItem(cache, fmt, s_object) == -1)
1879 PyErr_Clear();
1880 }
1881 return s_object;
1882}
1883
Christian Heimes76d19f62008-01-04 02:54:42 +00001884PyDoc_STRVAR(clearcache_doc,
1885"Clear the internal cache.");
1886
1887static PyObject *
1888clearcache(PyObject *self)
1889{
1890 if (cache != NULL)
1891 PyDict_Clear(cache);
1892 Py_RETURN_NONE;
1893}
1894
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00001895PyDoc_STRVAR(calcsize_doc,
1896"Return size of C struct described by format string fmt.");
1897
1898static PyObject *
1899calcsize(PyObject *self, PyObject *fmt)
1900{
1901 Py_ssize_t n;
1902 PyObject *s_object = cache_struct(fmt);
1903 if (s_object == NULL)
1904 return NULL;
1905 n = ((PyStructObject *)s_object)->s_size;
1906 Py_DECREF(s_object);
1907 return PyInt_FromSsize_t(n);
1908}
1909
1910PyDoc_STRVAR(pack_doc,
1911"Return string containing values v1, v2, ... packed according to fmt.");
1912
1913static PyObject *
1914pack(PyObject *self, PyObject *args)
1915{
1916 PyObject *s_object, *fmt, *newargs, *result;
1917 Py_ssize_t n = PyTuple_GET_SIZE(args);
1918
1919 if (n == 0) {
1920 PyErr_SetString(PyExc_TypeError, "missing format argument");
1921 return NULL;
1922 }
1923 fmt = PyTuple_GET_ITEM(args, 0);
1924 newargs = PyTuple_GetSlice(args, 1, n);
1925 if (newargs == NULL)
1926 return NULL;
1927
1928 s_object = cache_struct(fmt);
1929 if (s_object == NULL) {
1930 Py_DECREF(newargs);
1931 return NULL;
1932 }
1933 result = s_pack(s_object, newargs);
1934 Py_DECREF(newargs);
1935 Py_DECREF(s_object);
1936 return result;
1937}
1938
1939PyDoc_STRVAR(pack_into_doc,
1940"Pack the values v1, v2, ... according to fmt.\n\
1941Write the packed bytes into the writable buffer buf starting at offset.");
1942
1943static PyObject *
1944pack_into(PyObject *self, PyObject *args)
1945{
1946 PyObject *s_object, *fmt, *newargs, *result;
1947 Py_ssize_t n = PyTuple_GET_SIZE(args);
1948
1949 if (n == 0) {
1950 PyErr_SetString(PyExc_TypeError, "missing format argument");
1951 return NULL;
1952 }
1953 fmt = PyTuple_GET_ITEM(args, 0);
1954 newargs = PyTuple_GetSlice(args, 1, n);
1955 if (newargs == NULL)
1956 return NULL;
1957
1958 s_object = cache_struct(fmt);
1959 if (s_object == NULL) {
1960 Py_DECREF(newargs);
1961 return NULL;
1962 }
1963 result = s_pack_into(s_object, newargs);
1964 Py_DECREF(newargs);
1965 Py_DECREF(s_object);
1966 return result;
1967}
1968
1969PyDoc_STRVAR(unpack_doc,
1970"Unpack the string containing packed C structure data, according to fmt.\n\
1971Requires len(string) == calcsize(fmt).");
1972
1973static PyObject *
1974unpack(PyObject *self, PyObject *args)
1975{
1976 PyObject *s_object, *fmt, *inputstr, *result;
1977
1978 if (!PyArg_UnpackTuple(args, "unpack", 2, 2, &fmt, &inputstr))
1979 return NULL;
1980
1981 s_object = cache_struct(fmt);
1982 if (s_object == NULL)
1983 return NULL;
1984 result = s_unpack(s_object, inputstr);
1985 Py_DECREF(s_object);
1986 return result;
1987}
1988
1989PyDoc_STRVAR(unpack_from_doc,
1990"Unpack the buffer, containing packed C structure data, according to\n\
1991fmt, starting at offset. Requires len(buffer[offset:]) >= calcsize(fmt).");
1992
1993static PyObject *
1994unpack_from(PyObject *self, PyObject *args, PyObject *kwds)
1995{
1996 PyObject *s_object, *fmt, *newargs, *result;
1997 Py_ssize_t n = PyTuple_GET_SIZE(args);
1998
1999 if (n == 0) {
2000 PyErr_SetString(PyExc_TypeError, "missing format argument");
2001 return NULL;
2002 }
2003 fmt = PyTuple_GET_ITEM(args, 0);
2004 newargs = PyTuple_GetSlice(args, 1, n);
2005 if (newargs == NULL)
2006 return NULL;
2007
2008 s_object = cache_struct(fmt);
2009 if (s_object == NULL) {
2010 Py_DECREF(newargs);
2011 return NULL;
2012 }
2013 result = s_unpack_from(s_object, newargs, kwds);
2014 Py_DECREF(newargs);
2015 Py_DECREF(s_object);
2016 return result;
2017}
2018
2019static struct PyMethodDef module_functions[] = {
Christian Heimes76d19f62008-01-04 02:54:42 +00002020 {"_clearcache", (PyCFunction)clearcache, METH_NOARGS, clearcache_doc},
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00002021 {"calcsize", calcsize, METH_O, calcsize_doc},
2022 {"pack", pack, METH_VARARGS, pack_doc},
2023 {"pack_into", pack_into, METH_VARARGS, pack_into_doc},
2024 {"unpack", unpack, METH_VARARGS, unpack_doc},
2025 {"unpack_from", (PyCFunction)unpack_from,
2026 METH_VARARGS|METH_KEYWORDS, unpack_from_doc},
2027 {NULL, NULL} /* sentinel */
2028};
2029
2030
Bob Ippolito232f3c92006-05-23 19:12:41 +00002031/* Module initialization */
2032
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00002033PyDoc_STRVAR(module_doc,
2034"Functions to convert between Python values and C structs.\n\
2035Python strings are used to hold the data representing the C struct\n\
2036and also as format strings to describe the layout of data in the C struct.\n\
2037\n\
2038The optional first format char indicates byte order, size and alignment:\n\
2039 @: native order, size & alignment (default)\n\
2040 =: native order, std. size & alignment\n\
2041 <: little-endian, std. size & alignment\n\
2042 >: big-endian, std. size & alignment\n\
2043 !: same as >\n\
2044\n\
2045The remaining chars indicate types of args and must match exactly;\n\
2046these can be preceded by a decimal repeat count:\n\
2047 x: pad byte (no data); c:char; b:signed byte; B:unsigned byte;\n\
2048 h:short; H:unsigned short; i:int; I:unsigned int;\n\
2049 l:long; L:unsigned long; f:float; d:double.\n\
2050Special cases (preceding decimal count indicates length):\n\
2051 s:string (array of char); p: pascal string (with count byte).\n\
2052Special case (only available in native format):\n\
2053 P:an integer type that is wide enough to hold a pointer.\n\
2054Special case (not in native mode unless 'long long' in platform C):\n\
2055 q:long long; Q:unsigned long long\n\
2056Whitespace between formats is ignored.\n\
2057\n\
2058The variable struct.error is an exception raised on errors.\n");
2059
Bob Ippolito232f3c92006-05-23 19:12:41 +00002060PyMODINIT_FUNC
2061init_struct(void)
2062{
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00002063 PyObject *ver, *m;
2064
2065 ver = PyString_FromString("0.2");
2066 if (ver == NULL)
2067 return;
2068
2069 m = Py_InitModule3("_struct", module_functions, module_doc);
Bob Ippolito232f3c92006-05-23 19:12:41 +00002070 if (m == NULL)
2071 return;
2072
Christian Heimese93237d2007-12-19 02:37:44 +00002073 Py_TYPE(&PyStructType) = &PyType_Type;
Bob Ippolito3fc2bb92006-05-25 19:03:19 +00002074 if (PyType_Ready(&PyStructType) < 0)
2075 return;
2076
Bob Ippolito4182a752006-05-30 17:37:54 +00002077#ifdef PY_STRUCT_OVERFLOW_MASKING
Bob Ippolito2fd39772006-05-29 22:55:48 +00002078 if (pyint_zero == NULL) {
2079 pyint_zero = PyInt_FromLong(0);
2080 if (pyint_zero == NULL)
2081 return;
2082 }
2083 if (pylong_ulong_mask == NULL) {
2084#if (SIZEOF_LONG == 4)
2085 pylong_ulong_mask = PyLong_FromString("FFFFFFFF", NULL, 16);
2086#else
2087 pylong_ulong_mask = PyLong_FromString("FFFFFFFFFFFFFFFF", NULL, 16);
2088#endif
2089 if (pylong_ulong_mask == NULL)
2090 return;
2091 }
2092
Tim Petersc2b550e2006-05-31 14:28:07 +00002093#else
Bob Ippolito4182a752006-05-30 17:37:54 +00002094 /* This speed trick can't be used until overflow masking goes away, because
2095 native endian always raises exceptions instead of overflow masking. */
Tim Petersc2b550e2006-05-31 14:28:07 +00002096
Bob Ippolitoa99865b2006-05-25 19:56:56 +00002097 /* Check endian and swap in faster functions */
2098 {
2099 int one = 1;
2100 formatdef *native = native_table;
2101 formatdef *other, *ptr;
2102 if ((int)*(unsigned char*)&one)
2103 other = lilendian_table;
2104 else
2105 other = bigendian_table;
Bob Ippolito964e02a2006-05-25 21:09:45 +00002106 /* Scan through the native table, find a matching
2107 entry in the endian table and swap in the
2108 native implementations whenever possible
2109 (64-bit platforms may not have "standard" sizes) */
Bob Ippolitoa99865b2006-05-25 19:56:56 +00002110 while (native->format != '\0' && other->format != '\0') {
2111 ptr = other;
2112 while (ptr->format != '\0') {
2113 if (ptr->format == native->format) {
Bob Ippolito964e02a2006-05-25 21:09:45 +00002114 /* Match faster when formats are
2115 listed in the same order */
Bob Ippolitoa99865b2006-05-25 19:56:56 +00002116 if (ptr == other)
2117 other++;
Tim Petersc2b550e2006-05-31 14:28:07 +00002118 /* Only use the trick if the
Bob Ippolito964e02a2006-05-25 21:09:45 +00002119 size matches */
2120 if (ptr->size != native->size)
2121 break;
2122 /* Skip float and double, could be
2123 "unknown" float format */
2124 if (ptr->format == 'd' || ptr->format == 'f')
2125 break;
2126 ptr->pack = native->pack;
2127 ptr->unpack = native->unpack;
Bob Ippolitoa99865b2006-05-25 19:56:56 +00002128 break;
2129 }
2130 ptr++;
2131 }
2132 native++;
2133 }
2134 }
Bob Ippolito2fd39772006-05-29 22:55:48 +00002135#endif
Tim Petersc2b550e2006-05-31 14:28:07 +00002136
Bob Ippolito232f3c92006-05-23 19:12:41 +00002137 /* Add some symbolic constants to the module */
2138 if (StructError == NULL) {
2139 StructError = PyErr_NewException("struct.error", NULL, NULL);
2140 if (StructError == NULL)
2141 return;
2142 }
Bob Ippolito04ab9942006-05-25 19:33:38 +00002143
Bob Ippolito232f3c92006-05-23 19:12:41 +00002144 Py_INCREF(StructError);
2145 PyModule_AddObject(m, "error", StructError);
Bob Ippolito04ab9942006-05-25 19:33:38 +00002146
Bob Ippolito232f3c92006-05-23 19:12:41 +00002147 Py_INCREF((PyObject*)&PyStructType);
2148 PyModule_AddObject(m, "Struct", (PyObject*)&PyStructType);
Tim Petersc2b550e2006-05-31 14:28:07 +00002149
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00002150 PyModule_AddObject(m, "__version__", ver);
2151
Bob Ippolito2fd39772006-05-29 22:55:48 +00002152 PyModule_AddIntConstant(m, "_PY_STRUCT_RANGE_CHECKING", 1);
Bob Ippolito4182a752006-05-30 17:37:54 +00002153#ifdef PY_STRUCT_OVERFLOW_MASKING
2154 PyModule_AddIntConstant(m, "_PY_STRUCT_OVERFLOW_MASKING", 1);
Bob Ippolito2fd39772006-05-29 22:55:48 +00002155#endif
Bob Ippolitoe6c9f982006-08-04 23:59:21 +00002156#ifdef PY_STRUCT_FLOAT_COERCE
2157 PyModule_AddIntConstant(m, "_PY_STRUCT_FLOAT_COERCE", 1);
2158#endif
2159
Bob Ippolito232f3c92006-05-23 19:12:41 +00002160}