blob: ba8a8ed979e24b82d670c483902bd6ba3ec7c61e [file] [log] [blame]
Benjamin Peterson4ae19462008-07-31 15:03:40 +00001/* struct module -- pack values into and (out of) bytes objects */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002
3/* New version supporting byte order, alignment and size options,
4 character strings, and unsigned numbers */
5
6#define PY_SSIZE_T_CLEAN
7
8#include "Python.h"
9#include "structseq.h"
10#include "structmember.h"
11#include <ctype.h>
12
13static PyTypeObject PyStructType;
14
Thomas Wouters477c8d52006-05-27 19:21:47 +000015/* The translation function for each format character is table driven */
16typedef struct _formatdef {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000017 char format;
18 Py_ssize_t size;
19 Py_ssize_t alignment;
20 PyObject* (*unpack)(const char *,
21 const struct _formatdef *);
22 int (*pack)(char *, PyObject *,
23 const struct _formatdef *);
Thomas Wouters477c8d52006-05-27 19:21:47 +000024} formatdef;
25
26typedef struct _formatcode {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000027 const struct _formatdef *fmtdef;
28 Py_ssize_t offset;
29 Py_ssize_t size;
Thomas Wouters477c8d52006-05-27 19:21:47 +000030} formatcode;
31
32/* Struct object interface */
33
34typedef struct {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000035 PyObject_HEAD
36 Py_ssize_t s_size;
37 Py_ssize_t s_len;
38 formatcode *s_codes;
39 PyObject *s_format;
40 PyObject *weakreflist; /* List of weak references */
Thomas Wouters477c8d52006-05-27 19:21:47 +000041} PyStructObject;
42
43
44#define PyStruct_Check(op) PyObject_TypeCheck(op, &PyStructType)
Christian Heimes90aa7642007-12-19 02:45:37 +000045#define PyStruct_CheckExact(op) (Py_TYPE(op) == &PyStructType)
Thomas Wouters477c8d52006-05-27 19:21:47 +000046
47
48/* Exception */
49
50static PyObject *StructError;
51
52
53/* Define various structs to figure out the alignments of types */
54
55
56typedef struct { char c; short x; } st_short;
57typedef struct { char c; int x; } st_int;
58typedef struct { char c; long x; } st_long;
59typedef struct { char c; float x; } st_float;
60typedef struct { char c; double x; } st_double;
61typedef struct { char c; void *x; } st_void_p;
62
63#define SHORT_ALIGN (sizeof(st_short) - sizeof(short))
64#define INT_ALIGN (sizeof(st_int) - sizeof(int))
65#define LONG_ALIGN (sizeof(st_long) - sizeof(long))
66#define FLOAT_ALIGN (sizeof(st_float) - sizeof(float))
67#define DOUBLE_ALIGN (sizeof(st_double) - sizeof(double))
68#define VOID_P_ALIGN (sizeof(st_void_p) - sizeof(void *))
69
70/* We can't support q and Q in native mode unless the compiler does;
71 in std mode, they're 8 bytes on all platforms. */
72#ifdef HAVE_LONG_LONG
73typedef struct { char c; PY_LONG_LONG x; } s_long_long;
74#define LONG_LONG_ALIGN (sizeof(s_long_long) - sizeof(PY_LONG_LONG))
75#endif
76
Thomas Woutersb2137042007-02-01 18:02:27 +000077#ifdef HAVE_C99_BOOL
78#define BOOL_TYPE _Bool
79typedef struct { char c; _Bool x; } s_bool;
80#define BOOL_ALIGN (sizeof(s_bool) - sizeof(BOOL_TYPE))
81#else
82#define BOOL_TYPE char
83#define BOOL_ALIGN 0
84#endif
85
Thomas Wouters477c8d52006-05-27 19:21:47 +000086#define STRINGIFY(x) #x
87
88#ifdef __powerc
89#pragma options align=reset
90#endif
91
Mark Dickinsonea835e72009-04-19 20:40:33 +000092/* Helper to get a PyLongObject. Caller should decref. */
Thomas Wouters477c8d52006-05-27 19:21:47 +000093
94static PyObject *
95get_pylong(PyObject *v)
96{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000097 assert(v != NULL);
98 if (!PyLong_Check(v)) {
99 PyErr_SetString(StructError,
100 "required argument is not an integer");
101 return NULL;
102 }
Mark Dickinsonea835e72009-04-19 20:40:33 +0000103
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000104 Py_INCREF(v);
105 return v;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000106}
107
Mark Dickinsonea835e72009-04-19 20:40:33 +0000108/* Helper routine to get a C long and raise the appropriate error if it isn't
109 one */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000110
111static int
112get_long(PyObject *v, long *p)
113{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000114 long x;
Mark Dickinsonea835e72009-04-19 20:40:33 +0000115
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000116 if (!PyLong_Check(v)) {
117 PyErr_SetString(StructError,
118 "required argument is not an integer");
119 return -1;
120 }
121 x = PyLong_AsLong(v);
122 if (x == -1 && PyErr_Occurred()) {
123 if (PyErr_ExceptionMatches(PyExc_OverflowError))
124 PyErr_SetString(StructError,
125 "argument out of range");
126 return -1;
127 }
128 *p = x;
129 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000130}
131
132
133/* Same, but handling unsigned long */
134
Benjamin Petersond76c8da2009-06-28 17:35:48 +0000135#ifndef PY_STRUCT_OVERFLOW_MASKING
Thomas Wouters477c8d52006-05-27 19:21:47 +0000136static int
137get_ulong(PyObject *v, unsigned long *p)
138{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000139 unsigned long x;
Mark Dickinsonea835e72009-04-19 20:40:33 +0000140
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000141 if (!PyLong_Check(v)) {
142 PyErr_SetString(StructError,
143 "required argument is not an integer");
144 return -1;
145 }
146 x = PyLong_AsUnsignedLong(v);
147 if (x == (unsigned long)-1 && PyErr_Occurred()) {
148 if (PyErr_ExceptionMatches(PyExc_OverflowError))
149 PyErr_SetString(StructError,
150 "argument out of range");
151 return -1;
152 }
153 *p = x;
154 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000155}
Benjamin Petersond76c8da2009-06-28 17:35:48 +0000156#endif /* PY_STRUCT_OVERFLOW_MASKING */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000157
158#ifdef HAVE_LONG_LONG
159
160/* Same, but handling native long long. */
161
162static int
163get_longlong(PyObject *v, PY_LONG_LONG *p)
164{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000165 PY_LONG_LONG x;
166 if (!PyLong_Check(v)) {
167 PyErr_SetString(StructError,
168 "required argument is not an integer");
169 return -1;
170 }
171 x = PyLong_AsLongLong(v);
172 if (x == -1 && PyErr_Occurred()) {
173 if (PyErr_ExceptionMatches(PyExc_OverflowError))
174 PyErr_SetString(StructError,
175 "argument out of range");
176 return -1;
177 }
178 *p = x;
179 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000180}
181
182/* Same, but handling native unsigned long long. */
183
184static int
185get_ulonglong(PyObject *v, unsigned PY_LONG_LONG *p)
186{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000187 unsigned PY_LONG_LONG x;
188 if (!PyLong_Check(v)) {
189 PyErr_SetString(StructError,
190 "required argument is not an integer");
191 return -1;
192 }
193 x = PyLong_AsUnsignedLongLong(v);
194 if (x == -1 && PyErr_Occurred()) {
195 if (PyErr_ExceptionMatches(PyExc_OverflowError))
196 PyErr_SetString(StructError,
197 "argument out of range");
198 return -1;
199 }
200 *p = x;
201 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000202}
203
204#endif
205
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000206
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000207#define RANGE_ERROR(x, f, flag, mask) return _range_error(f, flag)
208
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000209
Thomas Wouters477c8d52006-05-27 19:21:47 +0000210/* Floating point helpers */
211
212static PyObject *
213unpack_float(const char *p, /* start of 4-byte string */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000214 int le) /* true for little-endian, false for big-endian */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000215{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000216 double x;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000217
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000218 x = _PyFloat_Unpack4((unsigned char *)p, le);
219 if (x == -1.0 && PyErr_Occurred())
220 return NULL;
221 return PyFloat_FromDouble(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000222}
223
224static PyObject *
225unpack_double(const char *p, /* start of 8-byte string */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000226 int le) /* true for little-endian, false for big-endian */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000227{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000228 double x;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000229
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000230 x = _PyFloat_Unpack8((unsigned char *)p, le);
231 if (x == -1.0 && PyErr_Occurred())
232 return NULL;
233 return PyFloat_FromDouble(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000234}
235
236/* Helper to format the range error exceptions */
237static int
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000238_range_error(const formatdef *f, int is_unsigned)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000239{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000240 /* ulargest is the largest unsigned value with f->size bytes.
241 * Note that the simpler:
242 * ((size_t)1 << (f->size * 8)) - 1
243 * doesn't work when f->size == sizeof(size_t) because C doesn't
244 * define what happens when a left shift count is >= the number of
245 * bits in the integer being shifted; e.g., on some boxes it doesn't
246 * shift at all when they're equal.
247 */
248 const size_t ulargest = (size_t)-1 >> ((SIZEOF_SIZE_T - f->size)*8);
249 assert(f->size >= 1 && f->size <= SIZEOF_SIZE_T);
250 if (is_unsigned)
251 PyErr_Format(StructError,
252 "'%c' format requires 0 <= number <= %zu",
253 f->format,
254 ulargest);
255 else {
256 const Py_ssize_t largest = (Py_ssize_t)(ulargest >> 1);
257 PyErr_Format(StructError,
258 "'%c' format requires %zd <= number <= %zd",
259 f->format,
260 ~ largest,
261 largest);
262 }
Mark Dickinsonae681df2009-03-21 10:26:31 +0000263
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000264 return -1;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000265}
266
267
268
269/* A large number of small routines follow, with names of the form
270
271 [bln][up]_TYPE
272
273 [bln] distiguishes among big-endian, little-endian and native.
274 [pu] distiguishes between pack (to struct) and unpack (from struct).
275 TYPE is one of char, byte, ubyte, etc.
276*/
277
278/* Native mode routines. ****************************************************/
279/* NOTE:
280 In all n[up]_<type> routines handling types larger than 1 byte, there is
281 *no* guarantee that the p pointer is properly aligned for each type,
282 therefore memcpy is called. An intermediate variable is used to
283 compensate for big-endian architectures.
284 Normally both the intermediate variable and the memcpy call will be
285 skipped by C optimisation in little-endian architectures (gcc >= 2.91
286 does this). */
287
288static PyObject *
289nu_char(const char *p, const formatdef *f)
290{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000291 return PyBytes_FromStringAndSize(p, 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000292}
293
294static PyObject *
295nu_byte(const char *p, const formatdef *f)
296{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000297 return PyLong_FromLong((long) *(signed char *)p);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000298}
299
300static PyObject *
301nu_ubyte(const char *p, const formatdef *f)
302{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000303 return PyLong_FromLong((long) *(unsigned char *)p);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000304}
305
306static PyObject *
307nu_short(const char *p, const formatdef *f)
308{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000309 short x;
310 memcpy((char *)&x, p, sizeof x);
311 return PyLong_FromLong((long)x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000312}
313
314static PyObject *
315nu_ushort(const char *p, const formatdef *f)
316{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000317 unsigned short x;
318 memcpy((char *)&x, p, sizeof x);
319 return PyLong_FromLong((long)x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000320}
321
322static PyObject *
323nu_int(const char *p, const formatdef *f)
324{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000325 int x;
326 memcpy((char *)&x, p, sizeof x);
327 return PyLong_FromLong((long)x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000328}
329
330static PyObject *
331nu_uint(const char *p, const formatdef *f)
332{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000333 unsigned int x;
334 memcpy((char *)&x, p, sizeof x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000335#if (SIZEOF_LONG > SIZEOF_INT)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000336 return PyLong_FromLong((long)x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000337#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000338 if (x <= ((unsigned int)LONG_MAX))
339 return PyLong_FromLong((long)x);
340 return PyLong_FromUnsignedLong((unsigned long)x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000341#endif
342}
343
344static PyObject *
345nu_long(const char *p, const formatdef *f)
346{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000347 long x;
348 memcpy((char *)&x, p, sizeof x);
349 return PyLong_FromLong(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000350}
351
352static PyObject *
353nu_ulong(const char *p, const formatdef *f)
354{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000355 unsigned long x;
356 memcpy((char *)&x, p, sizeof x);
357 if (x <= LONG_MAX)
358 return PyLong_FromLong((long)x);
359 return PyLong_FromUnsignedLong(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000360}
361
362/* Native mode doesn't support q or Q unless the platform C supports
363 long long (or, on Windows, __int64). */
364
365#ifdef HAVE_LONG_LONG
366
367static PyObject *
368nu_longlong(const char *p, const formatdef *f)
369{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000370 PY_LONG_LONG x;
371 memcpy((char *)&x, p, sizeof x);
372 if (x >= LONG_MIN && x <= LONG_MAX)
373 return PyLong_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
374 return PyLong_FromLongLong(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000375}
376
377static PyObject *
378nu_ulonglong(const char *p, const formatdef *f)
379{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000380 unsigned PY_LONG_LONG x;
381 memcpy((char *)&x, p, sizeof x);
382 if (x <= LONG_MAX)
383 return PyLong_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
384 return PyLong_FromUnsignedLongLong(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000385}
386
387#endif
388
389static PyObject *
Thomas Woutersb2137042007-02-01 18:02:27 +0000390nu_bool(const char *p, const formatdef *f)
391{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000392 BOOL_TYPE x;
393 memcpy((char *)&x, p, sizeof x);
394 return PyBool_FromLong(x != 0);
Thomas Woutersb2137042007-02-01 18:02:27 +0000395}
396
397
398static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +0000399nu_float(const char *p, const formatdef *f)
400{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000401 float x;
402 memcpy((char *)&x, p, sizeof x);
403 return PyFloat_FromDouble((double)x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000404}
405
406static PyObject *
407nu_double(const char *p, const formatdef *f)
408{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000409 double x;
410 memcpy((char *)&x, p, sizeof x);
411 return PyFloat_FromDouble(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000412}
413
414static PyObject *
415nu_void_p(const char *p, const formatdef *f)
416{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000417 void *x;
418 memcpy((char *)&x, p, sizeof x);
419 return PyLong_FromVoidPtr(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000420}
421
422static int
423np_byte(char *p, PyObject *v, const formatdef *f)
424{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000425 long x;
426 if (get_long(v, &x) < 0)
427 return -1;
428 if (x < -128 || x > 127){
429 PyErr_SetString(StructError,
430 "byte format requires -128 <= number <= 127");
431 return -1;
432 }
433 *p = (char)x;
434 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000435}
436
437static int
438np_ubyte(char *p, PyObject *v, const formatdef *f)
439{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000440 long x;
441 if (get_long(v, &x) < 0)
442 return -1;
443 if (x < 0 || x > 255){
444 PyErr_SetString(StructError,
445 "ubyte format requires 0 <= number <= 255");
446 return -1;
447 }
448 *p = (char)x;
449 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000450}
451
452static int
453np_char(char *p, PyObject *v, const formatdef *f)
454{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000455 if (PyUnicode_Check(v)) {
456 v = _PyUnicode_AsDefaultEncodedString(v, NULL);
457 if (v == NULL)
458 return -1;
459 }
460 if (!PyBytes_Check(v) || PyBytes_Size(v) != 1) {
461 PyErr_SetString(StructError,
462 "char format requires bytes or string of length 1");
463 return -1;
464 }
465 *p = *PyBytes_AsString(v);
466 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000467}
468
469static int
470np_short(char *p, PyObject *v, const formatdef *f)
471{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000472 long x;
473 short y;
474 if (get_long(v, &x) < 0)
475 return -1;
476 if (x < SHRT_MIN || x > SHRT_MAX){
477 PyErr_SetString(StructError,
478 "short format requires " STRINGIFY(SHRT_MIN)
479 " <= number <= " STRINGIFY(SHRT_MAX));
480 return -1;
481 }
482 y = (short)x;
483 memcpy(p, (char *)&y, sizeof y);
484 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000485}
486
487static int
488np_ushort(char *p, PyObject *v, const formatdef *f)
489{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000490 long x;
491 unsigned short y;
492 if (get_long(v, &x) < 0)
493 return -1;
494 if (x < 0 || x > USHRT_MAX){
495 PyErr_SetString(StructError,
496 "ushort format requires 0 <= number <= " STRINGIFY(USHRT_MAX));
497 return -1;
498 }
499 y = (unsigned short)x;
500 memcpy(p, (char *)&y, sizeof y);
501 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000502}
503
504static int
505np_int(char *p, PyObject *v, const formatdef *f)
506{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000507 long x;
508 int y;
509 if (get_long(v, &x) < 0)
510 return -1;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000511#if (SIZEOF_LONG > SIZEOF_INT)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000512 if ((x < ((long)INT_MIN)) || (x > ((long)INT_MAX)))
513 RANGE_ERROR(x, f, 0, -1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000514#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000515 y = (int)x;
516 memcpy(p, (char *)&y, sizeof y);
517 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000518}
519
520static int
521np_uint(char *p, PyObject *v, const formatdef *f)
522{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000523 unsigned long x;
524 unsigned int y;
525 if (get_ulong(v, &x) < 0)
526 return -1;
527 y = (unsigned int)x;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000528#if (SIZEOF_LONG > SIZEOF_INT)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000529 if (x > ((unsigned long)UINT_MAX))
530 RANGE_ERROR(y, f, 1, -1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000531#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000532 memcpy(p, (char *)&y, sizeof y);
533 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000534}
535
536static int
537np_long(char *p, PyObject *v, const formatdef *f)
538{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000539 long x;
540 if (get_long(v, &x) < 0)
541 return -1;
542 memcpy(p, (char *)&x, sizeof x);
543 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000544}
545
546static int
547np_ulong(char *p, PyObject *v, const formatdef *f)
548{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000549 unsigned long x;
550 if (get_ulong(v, &x) < 0)
551 return -1;
552 memcpy(p, (char *)&x, sizeof x);
553 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000554}
555
556#ifdef HAVE_LONG_LONG
557
558static int
559np_longlong(char *p, PyObject *v, const formatdef *f)
560{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000561 PY_LONG_LONG x;
562 if (get_longlong(v, &x) < 0)
563 return -1;
564 memcpy(p, (char *)&x, sizeof x);
565 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000566}
567
568static int
569np_ulonglong(char *p, PyObject *v, const formatdef *f)
570{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000571 unsigned PY_LONG_LONG x;
572 if (get_ulonglong(v, &x) < 0)
573 return -1;
574 memcpy(p, (char *)&x, sizeof x);
575 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000576}
577#endif
578
Thomas Woutersb2137042007-02-01 18:02:27 +0000579
580static int
581np_bool(char *p, PyObject *v, const formatdef *f)
582{
Benjamin Petersonf092c7c2010-07-07 22:46:00 +0000583 int y;
584 BOOL_TYPE x;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000585 y = PyObject_IsTrue(v);
Benjamin Petersonf092c7c2010-07-07 22:46:00 +0000586 if (y < 0)
587 return -1;
588 x = y;
589 memcpy(p, (char *)&x, sizeof x);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000590 return 0;
Thomas Woutersb2137042007-02-01 18:02:27 +0000591}
592
Thomas Wouters477c8d52006-05-27 19:21:47 +0000593static int
594np_float(char *p, PyObject *v, const formatdef *f)
595{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000596 float x = (float)PyFloat_AsDouble(v);
597 if (x == -1 && PyErr_Occurred()) {
598 PyErr_SetString(StructError,
599 "required argument is not a float");
600 return -1;
601 }
602 memcpy(p, (char *)&x, sizeof x);
603 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000604}
605
606static int
607np_double(char *p, PyObject *v, const formatdef *f)
608{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000609 double x = PyFloat_AsDouble(v);
610 if (x == -1 && PyErr_Occurred()) {
611 PyErr_SetString(StructError,
612 "required argument is not a float");
613 return -1;
614 }
615 memcpy(p, (char *)&x, sizeof(double));
616 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000617}
618
619static int
620np_void_p(char *p, PyObject *v, const formatdef *f)
621{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000622 void *x;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000623
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000624 v = get_pylong(v);
625 if (v == NULL)
626 return -1;
627 assert(PyLong_Check(v));
628 x = PyLong_AsVoidPtr(v);
629 Py_DECREF(v);
630 if (x == NULL && PyErr_Occurred())
631 return -1;
632 memcpy(p, (char *)&x, sizeof x);
633 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000634}
635
636static formatdef native_table[] = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000637 {'x', sizeof(char), 0, NULL},
638 {'b', sizeof(char), 0, nu_byte, np_byte},
639 {'B', sizeof(char), 0, nu_ubyte, np_ubyte},
640 {'c', sizeof(char), 0, nu_char, np_char},
641 {'s', sizeof(char), 0, NULL},
642 {'p', sizeof(char), 0, NULL},
643 {'h', sizeof(short), SHORT_ALIGN, nu_short, np_short},
644 {'H', sizeof(short), SHORT_ALIGN, nu_ushort, np_ushort},
645 {'i', sizeof(int), INT_ALIGN, nu_int, np_int},
646 {'I', sizeof(int), INT_ALIGN, nu_uint, np_uint},
647 {'l', sizeof(long), LONG_ALIGN, nu_long, np_long},
648 {'L', sizeof(long), LONG_ALIGN, nu_ulong, np_ulong},
Thomas Wouters477c8d52006-05-27 19:21:47 +0000649#ifdef HAVE_LONG_LONG
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000650 {'q', sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_longlong, np_longlong},
651 {'Q', sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_ulonglong,np_ulonglong},
Thomas Wouters477c8d52006-05-27 19:21:47 +0000652#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000653 {'?', sizeof(BOOL_TYPE), BOOL_ALIGN, nu_bool, np_bool},
654 {'f', sizeof(float), FLOAT_ALIGN, nu_float, np_float},
655 {'d', sizeof(double), DOUBLE_ALIGN, nu_double, np_double},
656 {'P', sizeof(void *), VOID_P_ALIGN, nu_void_p, np_void_p},
657 {0}
Thomas Wouters477c8d52006-05-27 19:21:47 +0000658};
659
660/* Big-endian routines. *****************************************************/
661
662static PyObject *
663bu_int(const char *p, const formatdef *f)
664{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000665 long x = 0;
666 Py_ssize_t i = f->size;
667 const unsigned char *bytes = (const unsigned char *)p;
668 do {
669 x = (x<<8) | *bytes++;
670 } while (--i > 0);
671 /* Extend the sign bit. */
672 if (SIZEOF_LONG > f->size)
673 x |= -(x & (1L << ((8 * f->size) - 1)));
674 return PyLong_FromLong(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000675}
676
677static PyObject *
678bu_uint(const char *p, const formatdef *f)
679{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000680 unsigned long x = 0;
681 Py_ssize_t i = f->size;
682 const unsigned char *bytes = (const unsigned char *)p;
683 do {
684 x = (x<<8) | *bytes++;
685 } while (--i > 0);
686 if (x <= LONG_MAX)
687 return PyLong_FromLong((long)x);
688 return PyLong_FromUnsignedLong(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000689}
690
691static PyObject *
692bu_longlong(const char *p, const formatdef *f)
693{
694#ifdef HAVE_LONG_LONG
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000695 PY_LONG_LONG x = 0;
696 Py_ssize_t i = f->size;
697 const unsigned char *bytes = (const unsigned char *)p;
698 do {
699 x = (x<<8) | *bytes++;
700 } while (--i > 0);
701 /* Extend the sign bit. */
702 if (SIZEOF_LONG_LONG > f->size)
703 x |= -(x & ((PY_LONG_LONG)1 << ((8 * f->size) - 1)));
704 if (x >= LONG_MIN && x <= LONG_MAX)
705 return PyLong_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
706 return PyLong_FromLongLong(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000707#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000708 return _PyLong_FromByteArray((const unsigned char *)p,
709 8,
710 0, /* little-endian */
711 1 /* signed */);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000712#endif
713}
714
715static PyObject *
716bu_ulonglong(const char *p, const formatdef *f)
717{
718#ifdef HAVE_LONG_LONG
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000719 unsigned PY_LONG_LONG x = 0;
720 Py_ssize_t i = f->size;
721 const unsigned char *bytes = (const unsigned char *)p;
722 do {
723 x = (x<<8) | *bytes++;
724 } while (--i > 0);
725 if (x <= LONG_MAX)
726 return PyLong_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
727 return PyLong_FromUnsignedLongLong(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000728#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000729 return _PyLong_FromByteArray((const unsigned char *)p,
730 8,
731 0, /* little-endian */
732 0 /* signed */);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000733#endif
734}
735
736static PyObject *
737bu_float(const char *p, const formatdef *f)
738{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000739 return unpack_float(p, 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000740}
741
742static PyObject *
743bu_double(const char *p, const formatdef *f)
744{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000745 return unpack_double(p, 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000746}
747
Thomas Woutersb2137042007-02-01 18:02:27 +0000748static PyObject *
749bu_bool(const char *p, const formatdef *f)
750{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000751 char x;
752 memcpy((char *)&x, p, sizeof x);
753 return PyBool_FromLong(x != 0);
Thomas Woutersb2137042007-02-01 18:02:27 +0000754}
755
Thomas Wouters477c8d52006-05-27 19:21:47 +0000756static int
757bp_int(char *p, PyObject *v, const formatdef *f)
758{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000759 long x;
760 Py_ssize_t i;
761 if (get_long(v, &x) < 0)
762 return -1;
763 i = f->size;
764 if (i != SIZEOF_LONG) {
765 if ((i == 2) && (x < -32768 || x > 32767))
766 RANGE_ERROR(x, f, 0, 0xffffL);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000767#if (SIZEOF_LONG != 4)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000768 else if ((i == 4) && (x < -2147483648L || x > 2147483647L))
769 RANGE_ERROR(x, f, 0, 0xffffffffL);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000770#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000771 }
772 do {
773 p[--i] = (char)x;
774 x >>= 8;
775 } while (i > 0);
776 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000777}
778
779static int
780bp_uint(char *p, PyObject *v, const formatdef *f)
781{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000782 unsigned long x;
783 Py_ssize_t i;
784 if (get_ulong(v, &x) < 0)
785 return -1;
786 i = f->size;
787 if (i != SIZEOF_LONG) {
788 unsigned long maxint = 1;
789 maxint <<= (unsigned long)(i * 8);
790 if (x >= maxint)
791 RANGE_ERROR(x, f, 1, maxint - 1);
792 }
793 do {
794 p[--i] = (char)x;
795 x >>= 8;
796 } while (i > 0);
797 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000798}
799
800static int
801bp_longlong(char *p, PyObject *v, const formatdef *f)
802{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000803 int res;
804 v = get_pylong(v);
805 if (v == NULL)
806 return -1;
807 res = _PyLong_AsByteArray((PyLongObject *)v,
808 (unsigned char *)p,
809 8,
810 0, /* little_endian */
811 1 /* signed */);
812 Py_DECREF(v);
813 return res;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000814}
815
816static int
817bp_ulonglong(char *p, PyObject *v, const formatdef *f)
818{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000819 int res;
820 v = get_pylong(v);
821 if (v == NULL)
822 return -1;
823 res = _PyLong_AsByteArray((PyLongObject *)v,
824 (unsigned char *)p,
825 8,
826 0, /* little_endian */
827 0 /* signed */);
828 Py_DECREF(v);
829 return res;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000830}
831
832static int
833bp_float(char *p, PyObject *v, const formatdef *f)
834{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000835 double x = PyFloat_AsDouble(v);
836 if (x == -1 && PyErr_Occurred()) {
837 PyErr_SetString(StructError,
838 "required argument is not a float");
839 return -1;
840 }
841 return _PyFloat_Pack4(x, (unsigned char *)p, 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000842}
843
844static int
845bp_double(char *p, PyObject *v, const formatdef *f)
846{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000847 double x = PyFloat_AsDouble(v);
848 if (x == -1 && PyErr_Occurred()) {
849 PyErr_SetString(StructError,
850 "required argument is not a float");
851 return -1;
852 }
853 return _PyFloat_Pack8(x, (unsigned char *)p, 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000854}
855
Thomas Woutersb2137042007-02-01 18:02:27 +0000856static int
857bp_bool(char *p, PyObject *v, const formatdef *f)
858{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000859 char y;
860 y = PyObject_IsTrue(v);
Benjamin Petersonf092c7c2010-07-07 22:46:00 +0000861 if (y < 0)
862 return -1;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000863 memcpy(p, (char *)&y, sizeof y);
864 return 0;
Thomas Woutersb2137042007-02-01 18:02:27 +0000865}
866
Thomas Wouters477c8d52006-05-27 19:21:47 +0000867static formatdef bigendian_table[] = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000868 {'x', 1, 0, NULL},
869 {'b', 1, 0, nu_byte, np_byte},
870 {'B', 1, 0, nu_ubyte, np_ubyte},
871 {'c', 1, 0, nu_char, np_char},
872 {'s', 1, 0, NULL},
873 {'p', 1, 0, NULL},
874 {'h', 2, 0, bu_int, bp_int},
875 {'H', 2, 0, bu_uint, bp_uint},
876 {'i', 4, 0, bu_int, bp_int},
877 {'I', 4, 0, bu_uint, bp_uint},
878 {'l', 4, 0, bu_int, bp_int},
879 {'L', 4, 0, bu_uint, bp_uint},
880 {'q', 8, 0, bu_longlong, bp_longlong},
881 {'Q', 8, 0, bu_ulonglong, bp_ulonglong},
882 {'?', 1, 0, bu_bool, bp_bool},
883 {'f', 4, 0, bu_float, bp_float},
884 {'d', 8, 0, bu_double, bp_double},
885 {0}
Thomas Wouters477c8d52006-05-27 19:21:47 +0000886};
887
888/* Little-endian routines. *****************************************************/
889
890static PyObject *
891lu_int(const char *p, const formatdef *f)
892{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000893 long x = 0;
894 Py_ssize_t i = f->size;
895 const unsigned char *bytes = (const unsigned char *)p;
896 do {
897 x = (x<<8) | bytes[--i];
898 } while (i > 0);
899 /* Extend the sign bit. */
900 if (SIZEOF_LONG > f->size)
901 x |= -(x & (1L << ((8 * f->size) - 1)));
902 return PyLong_FromLong(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000903}
904
905static PyObject *
906lu_uint(const char *p, const formatdef *f)
907{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000908 unsigned long x = 0;
909 Py_ssize_t i = f->size;
910 const unsigned char *bytes = (const unsigned char *)p;
911 do {
912 x = (x<<8) | bytes[--i];
913 } while (i > 0);
914 if (x <= LONG_MAX)
915 return PyLong_FromLong((long)x);
916 return PyLong_FromUnsignedLong((long)x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000917}
918
919static PyObject *
920lu_longlong(const char *p, const formatdef *f)
921{
922#ifdef HAVE_LONG_LONG
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000923 PY_LONG_LONG x = 0;
924 Py_ssize_t i = f->size;
925 const unsigned char *bytes = (const unsigned char *)p;
926 do {
927 x = (x<<8) | bytes[--i];
928 } while (i > 0);
929 /* Extend the sign bit. */
930 if (SIZEOF_LONG_LONG > f->size)
931 x |= -(x & ((PY_LONG_LONG)1 << ((8 * f->size) - 1)));
932 if (x >= LONG_MIN && x <= LONG_MAX)
933 return PyLong_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
934 return PyLong_FromLongLong(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000935#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000936 return _PyLong_FromByteArray((const unsigned char *)p,
937 8,
938 1, /* little-endian */
939 1 /* signed */);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000940#endif
941}
942
943static PyObject *
944lu_ulonglong(const char *p, const formatdef *f)
945{
946#ifdef HAVE_LONG_LONG
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000947 unsigned PY_LONG_LONG x = 0;
948 Py_ssize_t i = f->size;
949 const unsigned char *bytes = (const unsigned char *)p;
950 do {
951 x = (x<<8) | bytes[--i];
952 } while (i > 0);
953 if (x <= LONG_MAX)
954 return PyLong_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
955 return PyLong_FromUnsignedLongLong(x);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000956#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000957 return _PyLong_FromByteArray((const unsigned char *)p,
958 8,
959 1, /* little-endian */
960 0 /* signed */);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000961#endif
962}
963
964static PyObject *
965lu_float(const char *p, const formatdef *f)
966{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000967 return unpack_float(p, 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000968}
969
970static PyObject *
971lu_double(const char *p, const formatdef *f)
972{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000973 return unpack_double(p, 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000974}
975
976static int
977lp_int(char *p, PyObject *v, const formatdef *f)
978{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000979 long x;
980 Py_ssize_t i;
981 if (get_long(v, &x) < 0)
982 return -1;
983 i = f->size;
984 if (i != SIZEOF_LONG) {
985 if ((i == 2) && (x < -32768 || x > 32767))
986 RANGE_ERROR(x, f, 0, 0xffffL);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000987#if (SIZEOF_LONG != 4)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000988 else if ((i == 4) && (x < -2147483648L || x > 2147483647L))
989 RANGE_ERROR(x, f, 0, 0xffffffffL);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000990#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000991 }
992 do {
993 *p++ = (char)x;
994 x >>= 8;
995 } while (--i > 0);
996 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000997}
998
999static int
1000lp_uint(char *p, PyObject *v, const formatdef *f)
1001{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001002 unsigned long x;
1003 Py_ssize_t i;
1004 if (get_ulong(v, &x) < 0)
1005 return -1;
1006 i = f->size;
1007 if (i != SIZEOF_LONG) {
1008 unsigned long maxint = 1;
1009 maxint <<= (unsigned long)(i * 8);
1010 if (x >= maxint)
1011 RANGE_ERROR(x, f, 1, maxint - 1);
1012 }
1013 do {
1014 *p++ = (char)x;
1015 x >>= 8;
1016 } while (--i > 0);
1017 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001018}
1019
1020static int
1021lp_longlong(char *p, PyObject *v, const formatdef *f)
1022{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001023 int res;
1024 v = get_pylong(v);
1025 if (v == NULL)
1026 return -1;
1027 res = _PyLong_AsByteArray((PyLongObject*)v,
1028 (unsigned char *)p,
1029 8,
1030 1, /* little_endian */
1031 1 /* signed */);
1032 Py_DECREF(v);
1033 return res;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001034}
1035
1036static int
1037lp_ulonglong(char *p, PyObject *v, const formatdef *f)
1038{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001039 int res;
1040 v = get_pylong(v);
1041 if (v == NULL)
1042 return -1;
1043 res = _PyLong_AsByteArray((PyLongObject*)v,
1044 (unsigned char *)p,
1045 8,
1046 1, /* little_endian */
1047 0 /* signed */);
1048 Py_DECREF(v);
1049 return res;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001050}
1051
1052static int
1053lp_float(char *p, PyObject *v, const formatdef *f)
1054{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001055 double x = PyFloat_AsDouble(v);
1056 if (x == -1 && PyErr_Occurred()) {
1057 PyErr_SetString(StructError,
1058 "required argument is not a float");
1059 return -1;
1060 }
1061 return _PyFloat_Pack4(x, (unsigned char *)p, 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001062}
1063
1064static int
1065lp_double(char *p, PyObject *v, const formatdef *f)
1066{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001067 double x = PyFloat_AsDouble(v);
1068 if (x == -1 && PyErr_Occurred()) {
1069 PyErr_SetString(StructError,
1070 "required argument is not a float");
1071 return -1;
1072 }
1073 return _PyFloat_Pack8(x, (unsigned char *)p, 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001074}
1075
1076static formatdef lilendian_table[] = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001077 {'x', 1, 0, NULL},
1078 {'b', 1, 0, nu_byte, np_byte},
1079 {'B', 1, 0, nu_ubyte, np_ubyte},
1080 {'c', 1, 0, nu_char, np_char},
1081 {'s', 1, 0, NULL},
1082 {'p', 1, 0, NULL},
1083 {'h', 2, 0, lu_int, lp_int},
1084 {'H', 2, 0, lu_uint, lp_uint},
1085 {'i', 4, 0, lu_int, lp_int},
1086 {'I', 4, 0, lu_uint, lp_uint},
1087 {'l', 4, 0, lu_int, lp_int},
1088 {'L', 4, 0, lu_uint, lp_uint},
1089 {'q', 8, 0, lu_longlong, lp_longlong},
1090 {'Q', 8, 0, lu_ulonglong, lp_ulonglong},
1091 {'?', 1, 0, bu_bool, bp_bool}, /* Std rep not endian dep,
1092 but potentially different from native rep -- reuse bx_bool funcs. */
1093 {'f', 4, 0, lu_float, lp_float},
1094 {'d', 8, 0, lu_double, lp_double},
1095 {0}
Thomas Wouters477c8d52006-05-27 19:21:47 +00001096};
1097
1098
1099static const formatdef *
1100whichtable(char **pfmt)
1101{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001102 const char *fmt = (*pfmt)++; /* May be backed out of later */
1103 switch (*fmt) {
1104 case '<':
1105 return lilendian_table;
1106 case '>':
1107 case '!': /* Network byte order is big-endian */
1108 return bigendian_table;
1109 case '=': { /* Host byte order -- different from native in aligment! */
1110 int n = 1;
1111 char *p = (char *) &n;
1112 if (*p == 1)
1113 return lilendian_table;
1114 else
1115 return bigendian_table;
1116 }
1117 default:
1118 --*pfmt; /* Back out of pointer increment */
1119 /* Fall through */
1120 case '@':
1121 return native_table;
1122 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001123}
1124
1125
1126/* Get the table entry for a format code */
1127
1128static const formatdef *
1129getentry(int c, const formatdef *f)
1130{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001131 for (; f->format != '\0'; f++) {
1132 if (f->format == c) {
1133 return f;
1134 }
1135 }
1136 PyErr_SetString(StructError, "bad char in struct format");
1137 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001138}
1139
1140
Mark Dickinson4a3acca2010-06-11 20:08:36 +00001141/* Align a size according to a format code. Return -1 on overflow. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001142
Mark Dickinson4a3acca2010-06-11 20:08:36 +00001143static Py_ssize_t
Thomas Wouters477c8d52006-05-27 19:21:47 +00001144align(Py_ssize_t size, char c, const formatdef *e)
1145{
Mark Dickinson4a3acca2010-06-11 20:08:36 +00001146 Py_ssize_t extra;
1147
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001148 if (e->format == c) {
Mark Dickinson4a3acca2010-06-11 20:08:36 +00001149 if (e->alignment && size > 0) {
1150 extra = (e->alignment - 1) - (size - 1) % (e->alignment);
1151 if (extra > PY_SSIZE_T_MAX - size)
1152 return -1;
1153 size += extra;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001154 }
1155 }
1156 return size;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001157}
1158
1159
1160/* calculate the size of a format string */
1161
1162static int
1163prepare_s(PyStructObject *self)
1164{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001165 const formatdef *f;
1166 const formatdef *e;
1167 formatcode *codes;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001168
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001169 const char *s;
1170 const char *fmt;
1171 char c;
Mark Dickinson4a3acca2010-06-11 20:08:36 +00001172 Py_ssize_t size, len, num, itemsize;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001173
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001174 fmt = PyBytes_AS_STRING(self->s_format);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001175
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001176 f = whichtable((char **)&fmt);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001177
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001178 s = fmt;
1179 size = 0;
1180 len = 0;
1181 while ((c = *s++) != '\0') {
1182 if (isspace(Py_CHARMASK(c)))
1183 continue;
1184 if ('0' <= c && c <= '9') {
1185 num = c - '0';
1186 while ('0' <= (c = *s++) && c <= '9') {
Mark Dickinson4a3acca2010-06-11 20:08:36 +00001187 /* overflow-safe version of
1188 if (num*10 + (c - '0') > PY_SSIZE_T_MAX) { ... } */
1189 if (num >= PY_SSIZE_T_MAX / 10 && (
1190 num > PY_SSIZE_T_MAX / 10 ||
1191 (c - '0') > PY_SSIZE_T_MAX % 10))
1192 goto overflow;
1193 num = num*10 + (c - '0');
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001194 }
1195 if (c == '\0')
1196 break;
1197 }
1198 else
1199 num = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001200
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001201 e = getentry(c, f);
1202 if (e == NULL)
1203 return -1;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001204
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001205 switch (c) {
1206 case 's': /* fall through */
1207 case 'p': len++; break;
1208 case 'x': break;
1209 default: len += num; break;
1210 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001211
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001212 itemsize = e->size;
1213 size = align(size, c, e);
Mark Dickinson4a3acca2010-06-11 20:08:36 +00001214 if (size == -1)
1215 goto overflow;
1216
1217 /* if (size + num * itemsize > PY_SSIZE_T_MAX) { ... } */
1218 if (num > (PY_SSIZE_T_MAX - size) / itemsize)
1219 goto overflow;
1220 size += num * itemsize;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001221 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001222
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001223 /* check for overflow */
1224 if ((len + 1) > (PY_SSIZE_T_MAX / sizeof(formatcode))) {
1225 PyErr_NoMemory();
1226 return -1;
1227 }
Amaury Forgeot d'Arc35c86582008-06-17 21:11:29 +00001228
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001229 self->s_size = size;
1230 self->s_len = len;
1231 codes = PyMem_MALLOC((len + 1) * sizeof(formatcode));
1232 if (codes == NULL) {
1233 PyErr_NoMemory();
1234 return -1;
1235 }
1236 self->s_codes = codes;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001237
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001238 s = fmt;
1239 size = 0;
1240 while ((c = *s++) != '\0') {
1241 if (isspace(Py_CHARMASK(c)))
1242 continue;
1243 if ('0' <= c && c <= '9') {
1244 num = c - '0';
1245 while ('0' <= (c = *s++) && c <= '9')
1246 num = num*10 + (c - '0');
1247 if (c == '\0')
1248 break;
1249 }
1250 else
1251 num = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001252
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001253 e = getentry(c, f);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001254
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001255 size = align(size, c, e);
1256 if (c == 's' || c == 'p') {
1257 codes->offset = size;
1258 codes->size = num;
1259 codes->fmtdef = e;
1260 codes++;
1261 size += num;
1262 } else if (c == 'x') {
1263 size += num;
1264 } else {
1265 while (--num >= 0) {
1266 codes->offset = size;
1267 codes->size = e->size;
1268 codes->fmtdef = e;
1269 codes++;
1270 size += e->size;
1271 }
1272 }
1273 }
1274 codes->fmtdef = NULL;
1275 codes->offset = size;
1276 codes->size = 0;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001277
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001278 return 0;
Mark Dickinson4a3acca2010-06-11 20:08:36 +00001279
1280 overflow:
1281 PyErr_SetString(StructError,
1282 "total struct size too long");
1283 return -1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001284}
1285
1286static PyObject *
1287s_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1288{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001289 PyObject *self;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001290
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001291 assert(type != NULL && type->tp_alloc != NULL);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001292
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001293 self = type->tp_alloc(type, 0);
1294 if (self != NULL) {
1295 PyStructObject *s = (PyStructObject*)self;
1296 Py_INCREF(Py_None);
1297 s->s_format = Py_None;
1298 s->s_codes = NULL;
1299 s->s_size = -1;
1300 s->s_len = -1;
1301 }
1302 return self;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001303}
1304
1305static int
1306s_init(PyObject *self, PyObject *args, PyObject *kwds)
1307{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001308 PyStructObject *soself = (PyStructObject *)self;
1309 PyObject *o_format = NULL;
1310 int ret = 0;
1311 static char *kwlist[] = {"format", 0};
Thomas Wouters477c8d52006-05-27 19:21:47 +00001312
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001313 assert(PyStruct_Check(self));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001314
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001315 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:Struct", kwlist,
1316 &o_format))
1317 return -1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001318
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001319 if (PyUnicode_Check(o_format)) {
1320 o_format = PyUnicode_AsASCIIString(o_format);
1321 if (o_format == NULL)
1322 return -1;
1323 }
1324 /* XXX support buffer interface, too */
1325 else {
1326 Py_INCREF(o_format);
1327 }
Christian Heimesa34706f2008-01-04 03:06:10 +00001328
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001329 if (!PyBytes_Check(o_format)) {
1330 Py_DECREF(o_format);
1331 PyErr_Format(PyExc_TypeError,
1332 "Struct() argument 1 must be bytes, not %.200s",
1333 Py_TYPE(o_format)->tp_name);
1334 return -1;
1335 }
Christian Heimesa34706f2008-01-04 03:06:10 +00001336
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001337 Py_CLEAR(soself->s_format);
1338 soself->s_format = o_format;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001339
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001340 ret = prepare_s(soself);
1341 return ret;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001342}
1343
1344static void
1345s_dealloc(PyStructObject *s)
1346{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001347 if (s->weakreflist != NULL)
1348 PyObject_ClearWeakRefs((PyObject *)s);
1349 if (s->s_codes != NULL) {
1350 PyMem_FREE(s->s_codes);
1351 }
1352 Py_XDECREF(s->s_format);
1353 Py_TYPE(s)->tp_free((PyObject *)s);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001354}
1355
1356static PyObject *
1357s_unpack_internal(PyStructObject *soself, char *startfrom) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001358 formatcode *code;
1359 Py_ssize_t i = 0;
1360 PyObject *result = PyTuple_New(soself->s_len);
1361 if (result == NULL)
1362 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001363
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001364 for (code = soself->s_codes; code->fmtdef != NULL; code++) {
1365 PyObject *v;
1366 const formatdef *e = code->fmtdef;
1367 const char *res = startfrom + code->offset;
1368 if (e->format == 's') {
1369 v = PyBytes_FromStringAndSize(res, code->size);
1370 } else if (e->format == 'p') {
1371 Py_ssize_t n = *(unsigned char*)res;
1372 if (n >= code->size)
1373 n = code->size - 1;
1374 v = PyBytes_FromStringAndSize(res + 1, n);
1375 } else {
1376 v = e->unpack(res, e);
1377 }
1378 if (v == NULL)
1379 goto fail;
1380 PyTuple_SET_ITEM(result, i++, v);
1381 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001382
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001383 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001384fail:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001385 Py_DECREF(result);
1386 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001387}
1388
1389
1390PyDoc_STRVAR(s_unpack__doc__,
Guido van Rossum913dd0b2007-04-13 03:33:53 +00001391"S.unpack(buffer) -> (v1, v2, ...)\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +00001392\n\
Mark Dickinsonf9e091a2010-06-12 19:18:51 +00001393Return a tuple containing values unpacked according to the format\n\
1394string S.format. Requires len(buffer) == S.size. See help(struct)\n\
1395for more on format strings.");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001396
1397static PyObject *
Guido van Rossum98297ee2007-11-06 21:34:58 +00001398s_unpack(PyObject *self, PyObject *input)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001399{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001400 Py_buffer vbuf;
1401 PyObject *result;
1402 PyStructObject *soself = (PyStructObject *)self;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001403
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001404 assert(PyStruct_Check(self));
1405 assert(soself->s_codes != NULL);
1406 if (PyObject_GetBuffer(input, &vbuf, PyBUF_SIMPLE) < 0)
1407 return NULL;
1408 if (vbuf.len != soself->s_size) {
1409 PyErr_Format(StructError,
1410 "unpack requires a bytes argument of length %zd",
1411 soself->s_size);
1412 PyBuffer_Release(&vbuf);
1413 return NULL;
1414 }
1415 result = s_unpack_internal(soself, vbuf.buf);
1416 PyBuffer_Release(&vbuf);
1417 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001418}
1419
1420PyDoc_STRVAR(s_unpack_from__doc__,
Mark Dickinson13305072010-06-13 09:18:16 +00001421"S.unpack_from(buffer, offset=0) -> (v1, v2, ...)\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +00001422\n\
Mark Dickinsonf9e091a2010-06-12 19:18:51 +00001423Return a tuple containing values unpacked according to the format\n\
1424string S.format. Requires len(buffer[offset:]) >= S.size. See\n\
1425help(struct) for more on format strings.");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001426
1427static PyObject *
1428s_unpack_from(PyObject *self, PyObject *args, PyObject *kwds)
1429{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001430 static char *kwlist[] = {"buffer", "offset", 0};
Guido van Rossum98297ee2007-11-06 21:34:58 +00001431
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001432 PyObject *input;
1433 Py_ssize_t offset = 0;
1434 Py_buffer vbuf;
1435 PyObject *result;
1436 PyStructObject *soself = (PyStructObject *)self;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001437
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001438 assert(PyStruct_Check(self));
1439 assert(soself->s_codes != NULL);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001440
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001441 if (!PyArg_ParseTupleAndKeywords(args, kwds,
1442 "O|n:unpack_from", kwlist,
1443 &input, &offset))
1444 return NULL;
1445 if (PyObject_GetBuffer(input, &vbuf, PyBUF_SIMPLE) < 0)
1446 return NULL;
1447 if (offset < 0)
1448 offset += vbuf.len;
1449 if (offset < 0 || vbuf.len - offset < soself->s_size) {
1450 PyErr_Format(StructError,
1451 "unpack_from requires a buffer of at least %zd bytes",
1452 soself->s_size);
1453 PyBuffer_Release(&vbuf);
1454 return NULL;
1455 }
1456 result = s_unpack_internal(soself, (char*)vbuf.buf + offset);
1457 PyBuffer_Release(&vbuf);
1458 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001459}
1460
1461
1462/*
1463 * Guts of the pack function.
1464 *
1465 * Takes a struct object, a tuple of arguments, and offset in that tuple of
1466 * argument for where to start processing the arguments for packing, and a
1467 * character buffer for writing the packed string. The caller must insure
1468 * that the buffer may contain the required length for packing the arguments.
1469 * 0 is returned on success, 1 is returned if there is an error.
1470 *
1471 */
1472static int
1473s_pack_internal(PyStructObject *soself, PyObject *args, int offset, char* buf)
1474{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001475 formatcode *code;
1476 /* XXX(nnorwitz): why does i need to be a local? can we use
1477 the offset parameter or do we need the wider width? */
1478 Py_ssize_t i;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001479
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001480 memset(buf, '\0', soself->s_size);
1481 i = offset;
1482 for (code = soself->s_codes; code->fmtdef != NULL; code++) {
1483 Py_ssize_t n;
1484 PyObject *v = PyTuple_GET_ITEM(args, i++);
1485 const formatdef *e = code->fmtdef;
1486 char *res = buf + code->offset;
1487 if (e->format == 's') {
1488 int isstring;
1489 void *p;
1490 if (PyUnicode_Check(v)) {
1491 v = _PyUnicode_AsDefaultEncodedString(v, NULL);
1492 if (v == NULL)
1493 return -1;
1494 }
1495 isstring = PyBytes_Check(v);
1496 if (!isstring && !PyByteArray_Check(v)) {
1497 PyErr_SetString(StructError,
1498 "argument for 's' must be a bytes or string");
1499 return -1;
1500 }
1501 if (isstring) {
1502 n = PyBytes_GET_SIZE(v);
1503 p = PyBytes_AS_STRING(v);
1504 }
1505 else {
1506 n = PyByteArray_GET_SIZE(v);
1507 p = PyByteArray_AS_STRING(v);
1508 }
1509 if (n > code->size)
1510 n = code->size;
1511 if (n > 0)
1512 memcpy(res, p, n);
1513 } else if (e->format == 'p') {
1514 int isstring;
1515 void *p;
1516 if (PyUnicode_Check(v)) {
1517 v = _PyUnicode_AsDefaultEncodedString(v, NULL);
1518 if (v == NULL)
1519 return -1;
1520 }
1521 isstring = PyBytes_Check(v);
1522 if (!isstring && !PyByteArray_Check(v)) {
1523 PyErr_SetString(StructError,
1524 "argument for 'p' must be a bytes or string");
1525 return -1;
1526 }
1527 if (isstring) {
1528 n = PyBytes_GET_SIZE(v);
1529 p = PyBytes_AS_STRING(v);
1530 }
1531 else {
1532 n = PyByteArray_GET_SIZE(v);
1533 p = PyByteArray_AS_STRING(v);
1534 }
1535 if (n > (code->size - 1))
1536 n = code->size - 1;
1537 if (n > 0)
1538 memcpy(res + 1, p, n);
1539 if (n > 255)
1540 n = 255;
1541 *res = Py_SAFE_DOWNCAST(n, Py_ssize_t, unsigned char);
1542 } else {
1543 if (e->pack(res, v, e) < 0) {
1544 if (PyLong_Check(v) && PyErr_ExceptionMatches(PyExc_OverflowError))
1545 PyErr_SetString(StructError,
1546 "long too large to convert to int");
1547 return -1;
1548 }
1549 }
1550 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001551
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001552 /* Success */
1553 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001554}
1555
1556
1557PyDoc_STRVAR(s_pack__doc__,
Benjamin Peterson4ae19462008-07-31 15:03:40 +00001558"S.pack(v1, v2, ...) -> bytes\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +00001559\n\
Mark Dickinsonf9e091a2010-06-12 19:18:51 +00001560Return a bytes object containing values v1, v2, ... packed according\n\
1561to the format string S.format. See help(struct) for more on format\n\
1562strings.");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001563
1564static PyObject *
1565s_pack(PyObject *self, PyObject *args)
1566{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001567 PyStructObject *soself;
1568 PyObject *result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001569
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001570 /* Validate arguments. */
1571 soself = (PyStructObject *)self;
1572 assert(PyStruct_Check(self));
1573 assert(soself->s_codes != NULL);
1574 if (PyTuple_GET_SIZE(args) != soself->s_len)
1575 {
1576 PyErr_Format(StructError,
1577 "pack requires exactly %zd arguments", soself->s_len);
1578 return NULL;
1579 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001580
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001581 /* Allocate a new string */
1582 result = PyBytes_FromStringAndSize((char *)NULL, soself->s_size);
1583 if (result == NULL)
1584 return NULL;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001585
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001586 /* Call the guts */
1587 if ( s_pack_internal(soself, args, 0, PyBytes_AS_STRING(result)) != 0 ) {
1588 Py_DECREF(result);
1589 return NULL;
1590 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001591
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001592 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001593}
1594
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001595PyDoc_STRVAR(s_pack_into__doc__,
1596"S.pack_into(buffer, offset, v1, v2, ...)\n\
Thomas Wouters477c8d52006-05-27 19:21:47 +00001597\n\
Mark Dickinsonf9e091a2010-06-12 19:18:51 +00001598Pack the values v1, v2, ... according to the format string S.format\n\
1599and write the packed bytes into the writable buffer buf starting at\n\
1600offset. Note that the offset is a required argument. See\n\
1601help(struct) for more on format strings.");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001602
1603static PyObject *
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001604s_pack_into(PyObject *self, PyObject *args)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001605{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001606 PyStructObject *soself;
1607 char *buffer;
1608 Py_ssize_t buffer_len, offset;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001609
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001610 /* Validate arguments. +1 is for the first arg as buffer. */
1611 soself = (PyStructObject *)self;
1612 assert(PyStruct_Check(self));
1613 assert(soself->s_codes != NULL);
1614 if (PyTuple_GET_SIZE(args) != (soself->s_len + 2))
1615 {
1616 PyErr_Format(StructError,
1617 "pack_into requires exactly %zd arguments",
1618 (soself->s_len + 2));
1619 return NULL;
1620 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001621
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001622 /* Extract a writable memory buffer from the first argument */
1623 if ( PyObject_AsWriteBuffer(PyTuple_GET_ITEM(args, 0),
1624 (void**)&buffer, &buffer_len) == -1 ) {
1625 return NULL;
1626 }
1627 assert( buffer_len >= 0 );
Thomas Wouters477c8d52006-05-27 19:21:47 +00001628
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001629 /* Extract the offset from the first argument */
1630 offset = PyNumber_AsSsize_t(PyTuple_GET_ITEM(args, 1), PyExc_IndexError);
1631 if (offset == -1 && PyErr_Occurred())
1632 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001633
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001634 /* Support negative offsets. */
1635 if (offset < 0)
1636 offset += buffer_len;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001637
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001638 /* Check boundaries */
1639 if (offset < 0 || (buffer_len - offset) < soself->s_size) {
1640 PyErr_Format(StructError,
1641 "pack_into requires a buffer of at least %zd bytes",
1642 soself->s_size);
1643 return NULL;
1644 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001645
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001646 /* Call the guts */
1647 if ( s_pack_internal(soself, args, 2, buffer + offset) != 0 ) {
1648 return NULL;
1649 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001650
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001651 Py_RETURN_NONE;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001652}
1653
1654static PyObject *
1655s_get_format(PyStructObject *self, void *unused)
1656{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001657 Py_INCREF(self->s_format);
1658 return self->s_format;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001659}
1660
1661static PyObject *
1662s_get_size(PyStructObject *self, void *unused)
1663{
Christian Heimes217cfd12007-12-02 14:31:20 +00001664 return PyLong_FromSsize_t(self->s_size);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001665}
1666
1667/* List of functions */
1668
1669static struct PyMethodDef s_methods[] = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001670 {"pack", s_pack, METH_VARARGS, s_pack__doc__},
1671 {"pack_into", s_pack_into, METH_VARARGS, s_pack_into__doc__},
1672 {"unpack", s_unpack, METH_O, s_unpack__doc__},
1673 {"unpack_from", (PyCFunction)s_unpack_from, METH_VARARGS|METH_KEYWORDS,
1674 s_unpack_from__doc__},
1675 {NULL, NULL} /* sentinel */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001676};
1677
Mark Dickinsond12dfe22010-06-12 19:44:22 +00001678PyDoc_STRVAR(s__doc__,
1679"Struct(fmt) --> compiled struct object\n"
1680"\n"
1681"Return a new Struct object which writes and reads binary data according to\n"
1682"the format string fmt. See help(struct) for more on format strings.");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001683
1684#define OFF(x) offsetof(PyStructObject, x)
1685
1686static PyGetSetDef s_getsetlist[] = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001687 {"format", (getter)s_get_format, (setter)NULL, "struct format string", NULL},
1688 {"size", (getter)s_get_size, (setter)NULL, "struct size in bytes", NULL},
1689 {NULL} /* sentinel */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001690};
1691
1692static
1693PyTypeObject PyStructType = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001694 PyVarObject_HEAD_INIT(NULL, 0)
1695 "Struct",
1696 sizeof(PyStructObject),
1697 0,
1698 (destructor)s_dealloc, /* tp_dealloc */
1699 0, /* tp_print */
1700 0, /* tp_getattr */
1701 0, /* tp_setattr */
1702 0, /* tp_reserved */
1703 0, /* tp_repr */
1704 0, /* tp_as_number */
1705 0, /* tp_as_sequence */
1706 0, /* tp_as_mapping */
1707 0, /* tp_hash */
1708 0, /* tp_call */
1709 0, /* tp_str */
1710 PyObject_GenericGetAttr, /* tp_getattro */
1711 PyObject_GenericSetAttr, /* tp_setattro */
1712 0, /* tp_as_buffer */
1713 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
1714 s__doc__, /* tp_doc */
1715 0, /* tp_traverse */
1716 0, /* tp_clear */
1717 0, /* tp_richcompare */
1718 offsetof(PyStructObject, weakreflist), /* tp_weaklistoffset */
1719 0, /* tp_iter */
1720 0, /* tp_iternext */
1721 s_methods, /* tp_methods */
1722 NULL, /* tp_members */
1723 s_getsetlist, /* tp_getset */
1724 0, /* tp_base */
1725 0, /* tp_dict */
1726 0, /* tp_descr_get */
1727 0, /* tp_descr_set */
1728 0, /* tp_dictoffset */
1729 s_init, /* tp_init */
1730 PyType_GenericAlloc,/* tp_alloc */
1731 s_new, /* tp_new */
1732 PyObject_Del, /* tp_free */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001733};
1734
Christian Heimesa34706f2008-01-04 03:06:10 +00001735
1736/* ---- Standalone functions ---- */
1737
1738#define MAXCACHE 100
1739static PyObject *cache = NULL;
1740
1741static PyObject *
1742cache_struct(PyObject *fmt)
1743{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001744 PyObject * s_object;
Christian Heimesa34706f2008-01-04 03:06:10 +00001745
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001746 if (cache == NULL) {
1747 cache = PyDict_New();
1748 if (cache == NULL)
1749 return NULL;
1750 }
Christian Heimesa34706f2008-01-04 03:06:10 +00001751
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001752 s_object = PyDict_GetItem(cache, fmt);
1753 if (s_object != NULL) {
1754 Py_INCREF(s_object);
1755 return s_object;
1756 }
Christian Heimesa34706f2008-01-04 03:06:10 +00001757
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001758 s_object = PyObject_CallFunctionObjArgs((PyObject *)(&PyStructType), fmt, NULL);
1759 if (s_object != NULL) {
1760 if (PyDict_Size(cache) >= MAXCACHE)
1761 PyDict_Clear(cache);
1762 /* Attempt to cache the result */
1763 if (PyDict_SetItem(cache, fmt, s_object) == -1)
1764 PyErr_Clear();
1765 }
1766 return s_object;
Christian Heimesa34706f2008-01-04 03:06:10 +00001767}
1768
1769PyDoc_STRVAR(clearcache_doc,
1770"Clear the internal cache.");
1771
1772static PyObject *
1773clearcache(PyObject *self)
1774{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001775 Py_CLEAR(cache);
1776 Py_RETURN_NONE;
Christian Heimesa34706f2008-01-04 03:06:10 +00001777}
1778
1779PyDoc_STRVAR(calcsize_doc,
Mark Dickinsonf9e091a2010-06-12 19:18:51 +00001780"calcsize(fmt) -> integer\n\
1781\n\
1782Return size in bytes of the struct described by the format string fmt.");
Christian Heimesa34706f2008-01-04 03:06:10 +00001783
1784static PyObject *
1785calcsize(PyObject *self, PyObject *fmt)
1786{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001787 Py_ssize_t n;
1788 PyObject *s_object = cache_struct(fmt);
1789 if (s_object == NULL)
1790 return NULL;
1791 n = ((PyStructObject *)s_object)->s_size;
1792 Py_DECREF(s_object);
1793 return PyLong_FromSsize_t(n);
Christian Heimesa34706f2008-01-04 03:06:10 +00001794}
1795
1796PyDoc_STRVAR(pack_doc,
Mark Dickinsonf9e091a2010-06-12 19:18:51 +00001797"pack(fmt, v1, v2, ...) -> bytes\n\
1798\n\
1799Return a bytes object containing the values v1, v2, ... packed according\n\
1800to the format string fmt. See help(struct) for more on format strings.");
Christian Heimesa34706f2008-01-04 03:06:10 +00001801
1802static PyObject *
1803pack(PyObject *self, PyObject *args)
1804{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001805 PyObject *s_object, *fmt, *newargs, *result;
1806 Py_ssize_t n = PyTuple_GET_SIZE(args);
Christian Heimesa34706f2008-01-04 03:06:10 +00001807
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001808 if (n == 0) {
1809 PyErr_SetString(PyExc_TypeError, "missing format argument");
1810 return NULL;
1811 }
1812 fmt = PyTuple_GET_ITEM(args, 0);
1813 newargs = PyTuple_GetSlice(args, 1, n);
1814 if (newargs == NULL)
1815 return NULL;
Christian Heimesa34706f2008-01-04 03:06:10 +00001816
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001817 s_object = cache_struct(fmt);
1818 if (s_object == NULL) {
1819 Py_DECREF(newargs);
1820 return NULL;
1821 }
1822 result = s_pack(s_object, newargs);
1823 Py_DECREF(newargs);
1824 Py_DECREF(s_object);
1825 return result;
Christian Heimesa34706f2008-01-04 03:06:10 +00001826}
1827
1828PyDoc_STRVAR(pack_into_doc,
Mark Dickinsonf9e091a2010-06-12 19:18:51 +00001829"pack_into(fmt, buffer, offset, v1, v2, ...)\n\
1830\n\
1831Pack the values v1, v2, ... according to the format string fmt and write\n\
1832the packed bytes into the writable buffer buf starting at offset. Note\n\
1833that the offset is a required argument. See help(struct) for more\n\
1834on format strings.");
Christian Heimesa34706f2008-01-04 03:06:10 +00001835
1836static PyObject *
1837pack_into(PyObject *self, PyObject *args)
1838{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001839 PyObject *s_object, *fmt, *newargs, *result;
1840 Py_ssize_t n = PyTuple_GET_SIZE(args);
Christian Heimesa34706f2008-01-04 03:06:10 +00001841
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001842 if (n == 0) {
1843 PyErr_SetString(PyExc_TypeError, "missing format argument");
1844 return NULL;
1845 }
1846 fmt = PyTuple_GET_ITEM(args, 0);
1847 newargs = PyTuple_GetSlice(args, 1, n);
1848 if (newargs == NULL)
1849 return NULL;
Christian Heimesa34706f2008-01-04 03:06:10 +00001850
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001851 s_object = cache_struct(fmt);
1852 if (s_object == NULL) {
1853 Py_DECREF(newargs);
1854 return NULL;
1855 }
1856 result = s_pack_into(s_object, newargs);
1857 Py_DECREF(newargs);
1858 Py_DECREF(s_object);
1859 return result;
Christian Heimesa34706f2008-01-04 03:06:10 +00001860}
1861
1862PyDoc_STRVAR(unpack_doc,
Mark Dickinsonf9e091a2010-06-12 19:18:51 +00001863"unpack(fmt, buffer) -> (v1, v2, ...)\n\
1864\n\
1865Return a tuple containing values unpacked according to the format string\n\
1866fmt. Requires len(buffer) == calcsize(fmt). See help(struct) for more\n\
1867on format strings.");
Christian Heimesa34706f2008-01-04 03:06:10 +00001868
1869static PyObject *
1870unpack(PyObject *self, PyObject *args)
1871{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001872 PyObject *s_object, *fmt, *inputstr, *result;
Christian Heimesa34706f2008-01-04 03:06:10 +00001873
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001874 if (!PyArg_UnpackTuple(args, "unpack", 2, 2, &fmt, &inputstr))
1875 return NULL;
Christian Heimesa34706f2008-01-04 03:06:10 +00001876
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001877 s_object = cache_struct(fmt);
1878 if (s_object == NULL)
1879 return NULL;
1880 result = s_unpack(s_object, inputstr);
1881 Py_DECREF(s_object);
1882 return result;
Christian Heimesa34706f2008-01-04 03:06:10 +00001883}
1884
1885PyDoc_STRVAR(unpack_from_doc,
Mark Dickinson13305072010-06-13 09:18:16 +00001886"unpack_from(fmt, buffer, offset=0) -> (v1, v2, ...)\n\
Mark Dickinsonf9e091a2010-06-12 19:18:51 +00001887\n\
1888Return a tuple containing values unpacked according to the format string\n\
1889fmt. Requires len(buffer[offset:]) >= calcsize(fmt). See help(struct)\n\
1890for more on format strings.");
Christian Heimesa34706f2008-01-04 03:06:10 +00001891
1892static PyObject *
1893unpack_from(PyObject *self, PyObject *args, PyObject *kwds)
1894{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001895 PyObject *s_object, *fmt, *newargs, *result;
1896 Py_ssize_t n = PyTuple_GET_SIZE(args);
Christian Heimesa34706f2008-01-04 03:06:10 +00001897
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001898 if (n == 0) {
1899 PyErr_SetString(PyExc_TypeError, "missing format argument");
1900 return NULL;
1901 }
1902 fmt = PyTuple_GET_ITEM(args, 0);
1903 newargs = PyTuple_GetSlice(args, 1, n);
1904 if (newargs == NULL)
1905 return NULL;
Christian Heimesa34706f2008-01-04 03:06:10 +00001906
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001907 s_object = cache_struct(fmt);
1908 if (s_object == NULL) {
1909 Py_DECREF(newargs);
1910 return NULL;
1911 }
1912 result = s_unpack_from(s_object, newargs, kwds);
1913 Py_DECREF(newargs);
1914 Py_DECREF(s_object);
1915 return result;
Christian Heimesa34706f2008-01-04 03:06:10 +00001916}
1917
1918static struct PyMethodDef module_functions[] = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001919 {"_clearcache", (PyCFunction)clearcache, METH_NOARGS, clearcache_doc},
1920 {"calcsize", calcsize, METH_O, calcsize_doc},
1921 {"pack", pack, METH_VARARGS, pack_doc},
1922 {"pack_into", pack_into, METH_VARARGS, pack_into_doc},
1923 {"unpack", unpack, METH_VARARGS, unpack_doc},
1924 {"unpack_from", (PyCFunction)unpack_from,
1925 METH_VARARGS|METH_KEYWORDS, unpack_from_doc},
1926 {NULL, NULL} /* sentinel */
Christian Heimesa34706f2008-01-04 03:06:10 +00001927};
1928
1929
Thomas Wouters477c8d52006-05-27 19:21:47 +00001930/* Module initialization */
1931
Christian Heimesa34706f2008-01-04 03:06:10 +00001932PyDoc_STRVAR(module_doc,
1933"Functions to convert between Python values and C structs.\n\
Benjamin Peterson4ae19462008-07-31 15:03:40 +00001934Python bytes objects are used to hold the data representing the C struct\n\
Mark Dickinson015fa032009-10-08 16:02:50 +00001935and also as format strings (explained below) to describe the layout of data\n\
1936in the C struct.\n\
Christian Heimesa34706f2008-01-04 03:06:10 +00001937\n\
1938The optional first format char indicates byte order, size and alignment:\n\
Mark Dickinson015fa032009-10-08 16:02:50 +00001939 @: native order, size & alignment (default)\n\
1940 =: native order, std. size & alignment\n\
1941 <: little-endian, std. size & alignment\n\
1942 >: big-endian, std. size & alignment\n\
1943 !: same as >\n\
Christian Heimesa34706f2008-01-04 03:06:10 +00001944\n\
1945The remaining chars indicate types of args and must match exactly;\n\
1946these can be preceded by a decimal repeat count:\n\
1947 x: pad byte (no data); c:char; b:signed byte; B:unsigned byte;\n\
Mark Dickinson015fa032009-10-08 16:02:50 +00001948 ?: _Bool (requires C99; if not available, char is used instead)\n\
Christian Heimesa34706f2008-01-04 03:06:10 +00001949 h:short; H:unsigned short; i:int; I:unsigned int;\n\
1950 l:long; L:unsigned long; f:float; d:double.\n\
1951Special cases (preceding decimal count indicates length):\n\
1952 s:string (array of char); p: pascal string (with count byte).\n\
1953Special case (only available in native format):\n\
1954 P:an integer type that is wide enough to hold a pointer.\n\
1955Special case (not in native mode unless 'long long' in platform C):\n\
1956 q:long long; Q:unsigned long long\n\
1957Whitespace between formats is ignored.\n\
1958\n\
1959The variable struct.error is an exception raised on errors.\n");
1960
Martin v. Löwis1a214512008-06-11 05:26:20 +00001961
1962static struct PyModuleDef _structmodule = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001963 PyModuleDef_HEAD_INIT,
1964 "_struct",
1965 module_doc,
1966 -1,
1967 module_functions,
1968 NULL,
1969 NULL,
1970 NULL,
1971 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001972};
1973
Thomas Wouters477c8d52006-05-27 19:21:47 +00001974PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001975PyInit__struct(void)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001976{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001977 PyObject *ver, *m;
Christian Heimesa34706f2008-01-04 03:06:10 +00001978
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001979 ver = PyBytes_FromString("0.3");
1980 if (ver == NULL)
1981 return NULL;
Christian Heimesa34706f2008-01-04 03:06:10 +00001982
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001983 m = PyModule_Create(&_structmodule);
1984 if (m == NULL)
1985 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001986
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001987 Py_TYPE(&PyStructType) = &PyType_Type;
1988 if (PyType_Ready(&PyStructType) < 0)
1989 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001990
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001991 /* Check endian and swap in faster functions */
1992 {
1993 int one = 1;
1994 formatdef *native = native_table;
1995 formatdef *other, *ptr;
1996 if ((int)*(unsigned char*)&one)
1997 other = lilendian_table;
1998 else
1999 other = bigendian_table;
2000 /* Scan through the native table, find a matching
2001 entry in the endian table and swap in the
2002 native implementations whenever possible
2003 (64-bit platforms may not have "standard" sizes) */
2004 while (native->format != '\0' && other->format != '\0') {
2005 ptr = other;
2006 while (ptr->format != '\0') {
2007 if (ptr->format == native->format) {
2008 /* Match faster when formats are
2009 listed in the same order */
2010 if (ptr == other)
2011 other++;
2012 /* Only use the trick if the
2013 size matches */
2014 if (ptr->size != native->size)
2015 break;
2016 /* Skip float and double, could be
2017 "unknown" float format */
2018 if (ptr->format == 'd' || ptr->format == 'f')
2019 break;
2020 ptr->pack = native->pack;
2021 ptr->unpack = native->unpack;
2022 break;
2023 }
2024 ptr++;
2025 }
2026 native++;
2027 }
2028 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002029
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002030 /* Add some symbolic constants to the module */
2031 if (StructError == NULL) {
2032 StructError = PyErr_NewException("struct.error", NULL, NULL);
2033 if (StructError == NULL)
2034 return NULL;
2035 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002036
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002037 Py_INCREF(StructError);
2038 PyModule_AddObject(m, "error", StructError);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002039
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002040 Py_INCREF((PyObject*)&PyStructType);
2041 PyModule_AddObject(m, "Struct", (PyObject*)&PyStructType);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002042
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002043 PyModule_AddObject(m, "__version__", ver);
Christian Heimesa34706f2008-01-04 03:06:10 +00002044
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002045 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002046}