blob: 9b799781814484bd9fab31bdd7dffb1c8df5768d [file] [log] [blame]
Guido van Rossum02975121992-08-17 08:55:12 +00001
2/* struct module -- pack values into and (out of) strings */
3
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00004/* New version supporting byte order, alignment and size options,
5 character strings, and unsigned numbers */
6
Guido van Rossum414fd481997-12-19 04:24:24 +00007static char struct__doc__[] = "\
8Functions to convert between Python values and C structs.\n\
9Python strings are used to hold the data representing the C struct\n\
10and also as format strings to describe the layout of data in the C struct.\n\
11\n\
Tim Petersbe800852001-06-11 16:45:33 +000012The optional first format char indicates byte order, size and alignment:\n\
13 @: native order, size & alignment (default)\n\
14 =: native order, std. size & alignment\n\
15 <: little-endian, std. size & alignment\n\
16 >: big-endian, std. size & alignment\n\
17 !: same as >\n\
Guido van Rossum414fd481997-12-19 04:24:24 +000018\n\
19The remaining chars indicate types of args and must match exactly;\n\
20these can be preceded by a decimal repeat count:\n\
21 x: pad byte (no data); c:char; b:signed byte; B:unsigned byte;\n\
22 h:short; H:unsigned short; i:int; I:unsigned int;\n\
23 l:long; L:unsigned long; f:float; d:double.\n\
24Special cases (preceding decimal count indicates length):\n\
Tim Peters7b9542a2001-06-10 23:40:19 +000025 s:string (array of char); p: pascal string (with count byte).\n\
Guido van Rossum78694d91998-09-18 14:14:13 +000026Special case (only available in native format):\n\
27 P:an integer type that is wide enough to hold a pointer.\n\
Tim Peters7b9542a2001-06-10 23:40:19 +000028Special case (not in native mode unless 'long long' in platform C):\n\
29 q:long long; Q:unsigned long long\n\
Guido van Rossum414fd481997-12-19 04:24:24 +000030Whitespace between formats is ignored.\n\
31\n\
32The variable struct.error is an exception raised on errors.";
33
Barry Warsaw30695fa1996-12-12 23:32:31 +000034#include "Python.h"
Guido van Rossum02975121992-08-17 08:55:12 +000035
Guido van Rossume20aef51997-08-26 20:39:54 +000036#include <ctype.h>
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +000037
38
39/* Exception */
40
Barry Warsaw30695fa1996-12-12 23:32:31 +000041static PyObject *StructError;
Guido van Rossum02975121992-08-17 08:55:12 +000042
43
44/* Define various structs to figure out the alignments of types */
45
Jack Jansen971e1df1995-02-02 14:29:10 +000046#ifdef __MWERKS__
47/*
48** XXXX We have a problem here. There are no unique alignment rules
Tim Peters2d4e0772001-06-11 16:57:33 +000049** on the PowerPC mac.
Jack Jansen971e1df1995-02-02 14:29:10 +000050*/
51#ifdef __powerc
52#pragma options align=mac68k
53#endif
54#endif /* __MWERKS__ */
55
Guido van Rossum02975121992-08-17 08:55:12 +000056typedef struct { char c; short x; } s_short;
57typedef struct { char c; int x; } s_int;
58typedef struct { char c; long x; } s_long;
59typedef struct { char c; float x; } s_float;
60typedef struct { char c; double x; } s_double;
Guido van Rossum78694d91998-09-18 14:14:13 +000061typedef struct { char c; void *x; } s_void_p;
Guido van Rossum02975121992-08-17 08:55:12 +000062
63#define SHORT_ALIGN (sizeof(s_short) - sizeof(short))
64#define INT_ALIGN (sizeof(s_int) - sizeof(int))
65#define LONG_ALIGN (sizeof(s_long) - sizeof(long))
66#define FLOAT_ALIGN (sizeof(s_float) - sizeof(float))
67#define DOUBLE_ALIGN (sizeof(s_double) - sizeof(double))
Guido van Rossum78694d91998-09-18 14:14:13 +000068#define VOID_P_ALIGN (sizeof(s_void_p) - sizeof(void *))
Guido van Rossum02975121992-08-17 08:55:12 +000069
Tim Peters7b9542a2001-06-10 23:40:19 +000070/* 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; LONG_LONG x; } s_long_long;
74#define LONG_LONG_ALIGN (sizeof(s_long_long) - sizeof(LONG_LONG))
Tim Peters7b9542a2001-06-10 23:40:19 +000075#endif
76
Martin v. Löwis2af72d52000-09-15 08:10:33 +000077#define STRINGIFY(x) #x
78
Jack Jansen971e1df1995-02-02 14:29:10 +000079#ifdef __powerc
80#pragma options align=reset
81#endif
82
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +000083/* Helper routine to get a Python integer and raise the appropriate error
84 if it isn't one */
85
86static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +000087get_long(PyObject *v, long *p)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +000088{
89 long x = PyInt_AsLong(v);
90 if (x == -1 && PyErr_Occurred()) {
Fred Draked3dbb381998-05-28 04:35:49 +000091 if (PyErr_ExceptionMatches(PyExc_TypeError))
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +000092 PyErr_SetString(StructError,
93 "required argument is not an integer");
94 return -1;
95 }
96 *p = x;
97 return 0;
98}
99
100
Guido van Rossum60c50611996-12-31 16:29:52 +0000101/* Same, but handling unsigned long */
102
103static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000104get_ulong(PyObject *v, unsigned long *p)
Guido van Rossum60c50611996-12-31 16:29:52 +0000105{
Guido van Rossum6c87eca1997-01-03 19:08:16 +0000106 if (PyLong_Check(v)) {
107 unsigned long x = PyLong_AsUnsignedLong(v);
108 if (x == (unsigned long)(-1) && PyErr_Occurred())
Guido van Rossum60c50611996-12-31 16:29:52 +0000109 return -1;
Guido van Rossum6c87eca1997-01-03 19:08:16 +0000110 *p = x;
111 return 0;
Guido van Rossum60c50611996-12-31 16:29:52 +0000112 }
Guido van Rossum6c87eca1997-01-03 19:08:16 +0000113 else {
114 return get_long(v, (long *)p);
115 }
Guido van Rossum60c50611996-12-31 16:29:52 +0000116}
117
Tim Peters7b9542a2001-06-10 23:40:19 +0000118#ifdef HAVE_LONG_LONG
119
120/* Same, but handling native long long. */
121
122static int
123get_longlong(PyObject *v, LONG_LONG *p)
124{
125 LONG_LONG x;
126 int v_needs_decref = 0;
127
128 if (PyInt_Check(v)) {
129 x = (LONG_LONG)PyInt_AS_LONG(v);
130 *p = x;
131 return 0;
132 }
133 if (!PyLong_Check(v)) {
134 PyNumberMethods *m = v->ob_type->tp_as_number;
135 if (m != NULL && m->nb_long != NULL) {
136 v = m->nb_long(v);
137 if (v == NULL)
138 return -1;
139 v_needs_decref = 1;
140 }
141 if (!PyLong_Check(v)) {
142 PyErr_SetString(StructError,
143 "cannot convert argument to long");
144 if (v_needs_decref)
145 Py_DECREF(v);
146 return -1;
147 }
148 }
149 assert(PyLong_Check(v));
150 x = PyLong_AsLongLong(v);
151 if (v_needs_decref)
152 Py_DECREF(v);
153 if (x == (LONG_LONG)-1 && PyErr_Occurred())
154 return -1;
155 *p = x;
156 return 0;
157}
158
159/* Same, but handling native unsigned long long. */
160
161static int
162get_ulonglong(PyObject *v, unsigned LONG_LONG *p)
163{
164 unsigned LONG_LONG x;
165 int v_needs_decref = 0;
166
167 if (PyInt_Check(v)) {
168 long i = PyInt_AS_LONG(v);
169 if (i < 0) {
170 PyErr_SetString(StructError, "can't convert negative "
171 "int to unsigned");
172 return -1;
173 }
174 x = (unsigned LONG_LONG)i;
175 *p = x;
176 return 0;
177 }
178 if (!PyLong_Check(v)) {
179 PyNumberMethods *m = v->ob_type->tp_as_number;
180 if (m != NULL && m->nb_long != NULL) {
181 v = m->nb_long(v);
182 if (v == NULL)
183 return -1;
184 v_needs_decref = 1;
185 }
186 if (!PyLong_Check(v)) {
187 PyErr_SetString(StructError,
188 "cannot convert argument to long");
189 if (v_needs_decref)
190 Py_DECREF(v);
191 return -1;
192 }
193 }
194 assert(PyLong_Check(v));
195 x = PyLong_AsUnsignedLongLong(v);
196 if (v_needs_decref)
197 Py_DECREF(v);
198 if (x == (unsigned LONG_LONG)-1 && PyErr_Occurred())
199 return -1;
200 *p = x;
201 return 0;
202}
203
204#endif
Guido van Rossum60c50611996-12-31 16:29:52 +0000205
Guido van Rossum74679b41997-01-02 22:21:36 +0000206/* Floating point helpers */
207
208/* These use ANSI/IEEE Standard 754-1985 (Standard for Binary Floating
209 Point Arithmetic). See the following URL:
210 http://www.psc.edu/general/software/packages/ieee/ieee.html */
211
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000212/* XXX Inf/NaN are not handled quite right (but underflow is!) */
Guido van Rossum74679b41997-01-02 22:21:36 +0000213
214static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000215pack_float(double x, /* The number to pack */
216 char *p, /* Where to pack the high order byte */
217 int incr) /* 1 for big-endian; -1 for little-endian */
Guido van Rossum74679b41997-01-02 22:21:36 +0000218{
219 int s;
220 int e;
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000221 double f;
222 long fbits;
Guido van Rossum74679b41997-01-02 22:21:36 +0000223
224 if (x < 0) {
225 s = 1;
226 x = -x;
227 }
228 else
229 s = 0;
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000230
231 f = frexp(x, &e);
232
233 /* Normalize f to be in the range [1.0, 2.0) */
234 if (0.5 <= f && f < 1.0) {
235 f *= 2.0;
Guido van Rossum74679b41997-01-02 22:21:36 +0000236 e--;
237 }
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000238 else if (f == 0.0) {
Guido van Rossum74679b41997-01-02 22:21:36 +0000239 e = 0;
240 }
241 else {
242 PyErr_SetString(PyExc_SystemError,
243 "frexp() result out of range");
244 return -1;
245 }
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000246
247 if (e >= 128) {
248 /* XXX 128 itself is reserved for Inf/NaN */
Guido van Rossum74679b41997-01-02 22:21:36 +0000249 PyErr_SetString(PyExc_OverflowError,
250 "float too large to pack with f format");
251 return -1;
252 }
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000253 else if (e < -126) {
254 /* Gradual underflow */
255 f = ldexp(f, 126 + e);
Guido van Rossum74679b41997-01-02 22:21:36 +0000256 e = 0;
257 }
Guido van Rossum8f3c8121997-11-04 17:12:33 +0000258 else if (!(e == 0 && f == 0.0)) {
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000259 e += 127;
260 f -= 1.0; /* Get rid of leading 1 */
Guido van Rossum74679b41997-01-02 22:21:36 +0000261 }
262
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000263 f *= 8388608.0; /* 2**23 */
264 fbits = (long) floor(f + 0.5); /* Round */
265
Guido van Rossum74679b41997-01-02 22:21:36 +0000266 /* First byte */
267 *p = (s<<7) | (e>>1);
268 p += incr;
269
270 /* Second byte */
Guido van Rossum7844e381997-04-11 20:44:04 +0000271 *p = (char) (((e&1)<<7) | (fbits>>16));
Guido van Rossum74679b41997-01-02 22:21:36 +0000272 p += incr;
273
274 /* Third byte */
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000275 *p = (fbits>>8) & 0xFF;
Guido van Rossum74679b41997-01-02 22:21:36 +0000276 p += incr;
277
278 /* Fourth byte */
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000279 *p = fbits&0xFF;
Guido van Rossum74679b41997-01-02 22:21:36 +0000280
281 /* Done */
282 return 0;
283}
284
285static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000286pack_double(double x, /* The number to pack */
287 char *p, /* Where to pack the high order byte */
288 int incr) /* 1 for big-endian; -1 for little-endian */
Guido van Rossum74679b41997-01-02 22:21:36 +0000289{
290 int s;
291 int e;
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000292 double f;
Guido van Rossum74679b41997-01-02 22:21:36 +0000293 long fhi, flo;
294
295 if (x < 0) {
296 s = 1;
297 x = -x;
298 }
299 else
300 s = 0;
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000301
302 f = frexp(x, &e);
303
304 /* Normalize f to be in the range [1.0, 2.0) */
305 if (0.5 <= f && f < 1.0) {
306 f *= 2.0;
Guido van Rossum74679b41997-01-02 22:21:36 +0000307 e--;
308 }
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000309 else if (f == 0.0) {
Guido van Rossum74679b41997-01-02 22:21:36 +0000310 e = 0;
311 }
312 else {
313 PyErr_SetString(PyExc_SystemError,
314 "frexp() result out of range");
315 return -1;
316 }
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000317
318 if (e >= 1024) {
319 /* XXX 1024 itself is reserved for Inf/NaN */
Guido van Rossum74679b41997-01-02 22:21:36 +0000320 PyErr_SetString(PyExc_OverflowError,
321 "float too large to pack with d format");
322 return -1;
323 }
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000324 else if (e < -1022) {
325 /* Gradual underflow */
326 f = ldexp(f, 1022 + e);
Guido van Rossum74679b41997-01-02 22:21:36 +0000327 e = 0;
328 }
Guido van Rossum8f3c8121997-11-04 17:12:33 +0000329 else if (!(e == 0 && f == 0.0)) {
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000330 e += 1023;
331 f -= 1.0; /* Get rid of leading 1 */
Guido van Rossum74679b41997-01-02 22:21:36 +0000332 }
333
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000334 /* fhi receives the high 28 bits; flo the low 24 bits (== 52 bits) */
335 f *= 268435456.0; /* 2**28 */
336 fhi = (long) floor(f); /* Truncate */
337 f -= (double)fhi;
338 f *= 16777216.0; /* 2**24 */
339 flo = (long) floor(f + 0.5); /* Round */
340
Guido van Rossum74679b41997-01-02 22:21:36 +0000341 /* First byte */
342 *p = (s<<7) | (e>>4);
343 p += incr;
344
345 /* Second byte */
Guido van Rossum7844e381997-04-11 20:44:04 +0000346 *p = (char) (((e&0xF)<<4) | (fhi>>24));
Guido van Rossum74679b41997-01-02 22:21:36 +0000347 p += incr;
348
349 /* Third byte */
350 *p = (fhi>>16) & 0xFF;
351 p += incr;
352
353 /* Fourth byte */
354 *p = (fhi>>8) & 0xFF;
355 p += incr;
356
357 /* Fifth byte */
358 *p = fhi & 0xFF;
359 p += incr;
360
361 /* Sixth byte */
362 *p = (flo>>16) & 0xFF;
363 p += incr;
364
365 /* Seventh byte */
366 *p = (flo>>8) & 0xFF;
367 p += incr;
368
369 /* Eighth byte */
370 *p = flo & 0xFF;
371 p += incr;
372
373 /* Done */
374 return 0;
375}
376
377static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000378unpack_float(const char *p, /* Where the high order byte is */
379 int incr) /* 1 for big-endian; -1 for little-endian */
Guido van Rossum74679b41997-01-02 22:21:36 +0000380{
381 int s;
382 int e;
383 long f;
384 double x;
385
386 /* First byte */
387 s = (*p>>7) & 1;
388 e = (*p & 0x7F) << 1;
389 p += incr;
390
391 /* Second byte */
392 e |= (*p>>7) & 1;
393 f = (*p & 0x7F) << 16;
394 p += incr;
395
396 /* Third byte */
397 f |= (*p & 0xFF) << 8;
398 p += incr;
399
400 /* Fourth byte */
401 f |= *p & 0xFF;
402
403 x = (double)f / 8388608.0;
404
405 /* XXX This sadly ignores Inf/NaN issues */
Guido van Rossum07ef6551997-01-02 22:31:07 +0000406 if (e == 0)
407 e = -126;
408 else {
409 x += 1.0;
410 e -= 127;
411 }
412 x = ldexp(x, e);
Guido van Rossum74679b41997-01-02 22:21:36 +0000413
414 if (s)
415 x = -x;
416
417 return PyFloat_FromDouble(x);
418}
419
420static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000421unpack_double(const char *p, /* Where the high order byte is */
422 int incr) /* 1 for big-endian; -1 for little-endian */
Guido van Rossum74679b41997-01-02 22:21:36 +0000423{
424 int s;
425 int e;
426 long fhi, flo;
427 double x;
428
429 /* First byte */
430 s = (*p>>7) & 1;
431 e = (*p & 0x7F) << 4;
432 p += incr;
433
434 /* Second byte */
435 e |= (*p>>4) & 0xF;
436 fhi = (*p & 0xF) << 24;
437 p += incr;
438
439 /* Third byte */
440 fhi |= (*p & 0xFF) << 16;
441 p += incr;
442
443 /* Fourth byte */
444 fhi |= (*p & 0xFF) << 8;
445 p += incr;
446
447 /* Fifth byte */
448 fhi |= *p & 0xFF;
449 p += incr;
450
451 /* Sixth byte */
452 flo = (*p & 0xFF) << 16;
453 p += incr;
454
455 /* Seventh byte */
456 flo |= (*p & 0xFF) << 8;
457 p += incr;
458
459 /* Eighth byte */
460 flo |= *p & 0xFF;
461 p += incr;
462
463 x = (double)fhi + (double)flo / 16777216.0; /* 2**24 */
464 x /= 268435456.0; /* 2**28 */
465
466 /* XXX This sadly ignores Inf/NaN */
Guido van Rossum07ef6551997-01-02 22:31:07 +0000467 if (e == 0)
468 e = -1022;
469 else {
470 x += 1.0;
471 e -= 1023;
472 }
473 x = ldexp(x, e);
Guido van Rossum74679b41997-01-02 22:21:36 +0000474
475 if (s)
476 x = -x;
477
478 return PyFloat_FromDouble(x);
479}
480
481
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000482/* The translation function for each format character is table driven */
483
484typedef struct _formatdef {
485 char format;
486 int size;
487 int alignment;
Tim Petersdbd9ba62000-07-09 03:09:57 +0000488 PyObject* (*unpack)(const char *,
489 const struct _formatdef *);
490 int (*pack)(char *, PyObject *,
491 const struct _formatdef *);
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000492} formatdef;
493
Tim Peters7b9542a2001-06-10 23:40:19 +0000494/* A large number of small routines follow, with names of the form
495
496 [bln][up]_TYPE
497
498 [bln] distiguishes among big-endian, little-endian and native.
499 [pu] distiguishes between pack (to struct) and unpack (from struct).
500 TYPE is one of char, byte, ubyte, etc.
501*/
502
503/* Native mode routines. */
504
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000505static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000506nu_char(const char *p, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000507{
508 return PyString_FromStringAndSize(p, 1);
509}
510
511static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000512nu_byte(const char *p, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000513{
514 return PyInt_FromLong((long) *(signed char *)p);
515}
516
517static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000518nu_ubyte(const char *p, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000519{
520 return PyInt_FromLong((long) *(unsigned char *)p);
521}
522
523static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000524nu_short(const char *p, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000525{
526 return PyInt_FromLong((long) *(short *)p);
527}
528
529static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000530nu_ushort(const char *p, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000531{
532 return PyInt_FromLong((long) *(unsigned short *)p);
533}
534
535static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000536nu_int(const char *p, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000537{
538 return PyInt_FromLong((long) *(int *)p);
539}
540
541static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000542nu_uint(const char *p, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000543{
544 unsigned int x = *(unsigned int *)p;
Guido van Rossum6c87eca1997-01-03 19:08:16 +0000545 return PyLong_FromUnsignedLong((unsigned long)x);
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000546}
547
548static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000549nu_long(const char *p, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000550{
551 return PyInt_FromLong(*(long *)p);
552}
553
554static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000555nu_ulong(const char *p, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000556{
Guido van Rossum6c87eca1997-01-03 19:08:16 +0000557 return PyLong_FromUnsignedLong(*(unsigned long *)p);
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000558}
559
Tim Peters7b9542a2001-06-10 23:40:19 +0000560/* Native mode doesn't support q or Q unless the platform C supports
561 long long (or, on Windows, __int64). */
562
563#ifdef HAVE_LONG_LONG
564
565static PyObject *
566nu_longlong(const char *p, const formatdef *f)
567{
568 return PyLong_FromLongLong(*(LONG_LONG *)p);
569}
570
571static PyObject *
572nu_ulonglong(const char *p, const formatdef *f)
573{
574 return PyLong_FromUnsignedLongLong(*(unsigned LONG_LONG *)p);
575}
Tim Peters7b9542a2001-06-10 23:40:19 +0000576#endif
577
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000578static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000579nu_float(const char *p, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000580{
581 float x;
582 memcpy((char *)&x, p, sizeof(float));
583 return PyFloat_FromDouble((double)x);
584}
585
586static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000587nu_double(const char *p, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000588{
589 double x;
590 memcpy((char *)&x, p, sizeof(double));
591 return PyFloat_FromDouble(x);
592}
593
Guido van Rossum78694d91998-09-18 14:14:13 +0000594static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000595nu_void_p(const char *p, const formatdef *f)
Guido van Rossum78694d91998-09-18 14:14:13 +0000596{
597 return PyLong_FromVoidPtr(*(void **)p);
598}
599
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000600static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000601np_byte(char *p, PyObject *v, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000602{
603 long x;
604 if (get_long(v, &x) < 0)
605 return -1;
Martin v. Löwis66de5492000-09-15 07:31:57 +0000606 if (x < -128 || x > 127){
607 PyErr_SetString(StructError,
608 "byte format requires -128<=number<=127");
609 return -1;
610 }
611 *p = (char)x;
612 return 0;
613}
614
615static int
616np_ubyte(char *p, PyObject *v, const formatdef *f)
617{
618 long x;
619 if (get_long(v, &x) < 0)
620 return -1;
621 if (x < 0 || x > 255){
622 PyErr_SetString(StructError,
623 "ubyte format requires 0<=number<=255");
624 return -1;
625 }
Guido van Rossum7844e381997-04-11 20:44:04 +0000626 *p = (char)x;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000627 return 0;
628}
629
630static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000631np_char(char *p, PyObject *v, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000632{
633 if (!PyString_Check(v) || PyString_Size(v) != 1) {
634 PyErr_SetString(StructError,
635 "char format require string of length 1");
636 return -1;
637 }
638 *p = *PyString_AsString(v);
639 return 0;
640}
641
642static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000643np_short(char *p, PyObject *v, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000644{
645 long x;
646 if (get_long(v, &x) < 0)
647 return -1;
Martin v. Löwis2af72d52000-09-15 08:10:33 +0000648 if (x < SHRT_MIN || x > SHRT_MAX){
Martin v. Löwis66de5492000-09-15 07:31:57 +0000649 PyErr_SetString(StructError,
Martin v. Löwis2af72d52000-09-15 08:10:33 +0000650 "short format requires " STRINGIFY(SHRT_MIN)
651 "<=number<=" STRINGIFY(SHRT_MAX));
Martin v. Löwis66de5492000-09-15 07:31:57 +0000652 return -1;
653 }
Guido van Rossum7844e381997-04-11 20:44:04 +0000654 * (short *)p = (short)x;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000655 return 0;
656}
657
658static int
Martin v. Löwis66de5492000-09-15 07:31:57 +0000659np_ushort(char *p, PyObject *v, const formatdef *f)
660{
661 long x;
662 if (get_long(v, &x) < 0)
663 return -1;
Martin v. Löwis2af72d52000-09-15 08:10:33 +0000664 if (x < 0 || x > USHRT_MAX){
Martin v. Löwis66de5492000-09-15 07:31:57 +0000665 PyErr_SetString(StructError,
Martin v. Löwis2af72d52000-09-15 08:10:33 +0000666 "short format requires 0<=number<=" STRINGIFY(USHRT_MAX));
Martin v. Löwis66de5492000-09-15 07:31:57 +0000667 return -1;
668 }
669 * (unsigned short *)p = (unsigned short)x;
670 return 0;
671}
672
673static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000674np_int(char *p, PyObject *v, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000675{
676 long x;
677 if (get_long(v, &x) < 0)
678 return -1;
679 * (int *)p = x;
680 return 0;
681}
682
683static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000684np_uint(char *p, PyObject *v, const formatdef *f)
Guido van Rossum60c50611996-12-31 16:29:52 +0000685{
686 unsigned long x;
687 if (get_ulong(v, &x) < 0)
688 return -1;
689 * (unsigned int *)p = x;
690 return 0;
691}
692
693static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000694np_long(char *p, PyObject *v, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000695{
696 long x;
697 if (get_long(v, &x) < 0)
698 return -1;
699 * (long *)p = x;
700 return 0;
701}
702
703static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000704np_ulong(char *p, PyObject *v, const formatdef *f)
Guido van Rossum60c50611996-12-31 16:29:52 +0000705{
706 unsigned long x;
707 if (get_ulong(v, &x) < 0)
708 return -1;
709 * (unsigned long *)p = x;
710 return 0;
711}
712
Tim Peters7b9542a2001-06-10 23:40:19 +0000713#ifdef HAVE_LONG_LONG
714
715static int
716np_longlong(char *p, PyObject *v, const formatdef *f)
717{
718 LONG_LONG x;
719 if (get_longlong(v, &x) < 0)
720 return -1;
721 * (LONG_LONG *)p = x;
722 return 0;
723}
724
725static int
726np_ulonglong(char *p, PyObject *v, const formatdef *f)
727{
728 unsigned LONG_LONG x;
729 if (get_ulonglong(v, &x) < 0)
730 return -1;
731 * (unsigned LONG_LONG *)p = x;
732 return 0;
733}
Tim Peters7b9542a2001-06-10 23:40:19 +0000734#endif
735
Guido van Rossum60c50611996-12-31 16:29:52 +0000736static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000737np_float(char *p, PyObject *v, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000738{
739 float x = (float)PyFloat_AsDouble(v);
740 if (x == -1 && PyErr_Occurred()) {
741 PyErr_SetString(StructError,
742 "required argument is not a float");
743 return -1;
744 }
745 memcpy(p, (char *)&x, sizeof(float));
746 return 0;
747}
748
749static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000750np_double(char *p, PyObject *v, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000751{
752 double x = PyFloat_AsDouble(v);
753 if (x == -1 && PyErr_Occurred()) {
754 PyErr_SetString(StructError,
755 "required argument is not a float");
756 return -1;
757 }
758 memcpy(p, (char *)&x, sizeof(double));
759 return 0;
760}
761
Guido van Rossum78694d91998-09-18 14:14:13 +0000762static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000763np_void_p(char *p, PyObject *v, const formatdef *f)
Guido van Rossum78694d91998-09-18 14:14:13 +0000764{
765 void *x = PyLong_AsVoidPtr(v);
766 if (x == NULL && PyErr_Occurred()) {
767 /* ### hrm. PyLong_AsVoidPtr raises SystemError */
768 if (PyErr_ExceptionMatches(PyExc_TypeError))
769 PyErr_SetString(StructError,
770 "required argument is not an integer");
771 return -1;
772 }
773 *(void **)p = x;
774 return 0;
775}
776
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000777static formatdef native_table[] = {
778 {'x', sizeof(char), 0, NULL},
779 {'b', sizeof(char), 0, nu_byte, np_byte},
Martin v. Löwis66de5492000-09-15 07:31:57 +0000780 {'B', sizeof(char), 0, nu_ubyte, np_ubyte},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000781 {'c', sizeof(char), 0, nu_char, np_char},
782 {'s', sizeof(char), 0, NULL},
Guido van Rossum9eb671f1997-09-05 07:08:39 +0000783 {'p', sizeof(char), 0, NULL},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000784 {'h', sizeof(short), SHORT_ALIGN, nu_short, np_short},
Martin v. Löwis66de5492000-09-15 07:31:57 +0000785 {'H', sizeof(short), SHORT_ALIGN, nu_ushort, np_ushort},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000786 {'i', sizeof(int), INT_ALIGN, nu_int, np_int},
Guido van Rossum60c50611996-12-31 16:29:52 +0000787 {'I', sizeof(int), INT_ALIGN, nu_uint, np_uint},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000788 {'l', sizeof(long), LONG_ALIGN, nu_long, np_long},
Guido van Rossum60c50611996-12-31 16:29:52 +0000789 {'L', sizeof(long), LONG_ALIGN, nu_ulong, np_ulong},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000790 {'f', sizeof(float), FLOAT_ALIGN, nu_float, np_float},
791 {'d', sizeof(double), DOUBLE_ALIGN, nu_double, np_double},
Guido van Rossum78694d91998-09-18 14:14:13 +0000792 {'P', sizeof(void *), VOID_P_ALIGN, nu_void_p, np_void_p},
Tim Peters7b9542a2001-06-10 23:40:19 +0000793#ifdef HAVE_LONG_LONG
794 {'q', sizeof(LONG_LONG), LONG_LONG_ALIGN, nu_longlong, np_longlong},
795 {'Q', sizeof(LONG_LONG), LONG_LONG_ALIGN, nu_ulonglong,np_ulonglong},
Tim Peters7b9542a2001-06-10 23:40:19 +0000796#endif
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000797 {0}
798};
799
800static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000801bu_int(const char *p, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000802{
803 long x = 0;
804 int i = f->size;
805 do {
806 x = (x<<8) | (*p++ & 0xFF);
807 } while (--i > 0);
Tim Petersf0e717b2001-04-08 23:39:38 +0000808 /* Extend the sign bit. */
809 if (SIZEOF_LONG > f->size)
810 x |= -(x & (1L << (8*f->size - 1)));
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000811 return PyInt_FromLong(x);
812}
813
814static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000815bu_uint(const char *p, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000816{
Guido van Rossum6c87eca1997-01-03 19:08:16 +0000817 unsigned long x = 0;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000818 int i = f->size;
819 do {
820 x = (x<<8) | (*p++ & 0xFF);
821 } while (--i > 0);
Guido van Rossum39ef2271998-06-29 04:00:40 +0000822 if (f->size >= 4)
823 return PyLong_FromUnsignedLong(x);
824 else
825 return PyInt_FromLong((long)x);
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000826}
827
Guido van Rossum74679b41997-01-02 22:21:36 +0000828static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000829bu_float(const char *p, const formatdef *f)
Guido van Rossum74679b41997-01-02 22:21:36 +0000830{
831 return unpack_float(p, 1);
832}
833
834static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000835bu_double(const char *p, const formatdef *f)
Guido van Rossum74679b41997-01-02 22:21:36 +0000836{
837 return unpack_double(p, 1);
838}
839
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000840static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000841bp_int(char *p, PyObject *v, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000842{
843 long x;
844 int i;
845 if (get_long(v, &x) < 0)
846 return -1;
847 i = f->size;
848 do {
Guido van Rossum7844e381997-04-11 20:44:04 +0000849 p[--i] = (char)x;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000850 x >>= 8;
851 } while (i > 0);
852 return 0;
853}
854
Guido van Rossum60c50611996-12-31 16:29:52 +0000855static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000856bp_uint(char *p, PyObject *v, const formatdef *f)
Guido van Rossum60c50611996-12-31 16:29:52 +0000857{
858 unsigned long x;
859 int i;
860 if (get_ulong(v, &x) < 0)
861 return -1;
862 i = f->size;
863 do {
Guido van Rossum7844e381997-04-11 20:44:04 +0000864 p[--i] = (char)x;
Guido van Rossum60c50611996-12-31 16:29:52 +0000865 x >>= 8;
866 } while (i > 0);
867 return 0;
868}
869
Guido van Rossum74679b41997-01-02 22:21:36 +0000870static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000871bp_float(char *p, PyObject *v, const formatdef *f)
Guido van Rossum74679b41997-01-02 22:21:36 +0000872{
873 double x = PyFloat_AsDouble(v);
874 if (x == -1 && PyErr_Occurred()) {
875 PyErr_SetString(StructError,
876 "required argument is not a float");
877 return -1;
878 }
879 return pack_float(x, p, 1);
880}
881
882static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000883bp_double(char *p, PyObject *v, const formatdef *f)
Guido van Rossum74679b41997-01-02 22:21:36 +0000884{
885 double x = PyFloat_AsDouble(v);
886 if (x == -1 && PyErr_Occurred()) {
887 PyErr_SetString(StructError,
888 "required argument is not a float");
889 return -1;
890 }
891 return pack_double(x, p, 1);
892}
893
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000894static formatdef bigendian_table[] = {
895 {'x', 1, 0, NULL},
896 {'b', 1, 0, bu_int, bp_int},
897 {'B', 1, 0, bu_uint, bp_int},
898 {'c', 1, 0, nu_char, np_char},
899 {'s', 1, 0, NULL},
Guido van Rossum9eb671f1997-09-05 07:08:39 +0000900 {'p', 1, 0, NULL},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000901 {'h', 2, 0, bu_int, bp_int},
Guido van Rossum60c50611996-12-31 16:29:52 +0000902 {'H', 2, 0, bu_uint, bp_uint},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000903 {'i', 4, 0, bu_int, bp_int},
Guido van Rossum60c50611996-12-31 16:29:52 +0000904 {'I', 4, 0, bu_uint, bp_uint},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000905 {'l', 4, 0, bu_int, bp_int},
Guido van Rossum60c50611996-12-31 16:29:52 +0000906 {'L', 4, 0, bu_uint, bp_uint},
Guido van Rossum74679b41997-01-02 22:21:36 +0000907 {'f', 4, 0, bu_float, bp_float},
908 {'d', 8, 0, bu_double, bp_double},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000909 {0}
910};
911
912static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000913lu_int(const char *p, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000914{
915 long x = 0;
916 int i = f->size;
917 do {
918 x = (x<<8) | (p[--i] & 0xFF);
919 } while (i > 0);
Tim Petersf0e717b2001-04-08 23:39:38 +0000920 /* Extend the sign bit. */
921 if (SIZEOF_LONG > f->size)
922 x |= -(x & (1L << (8*f->size - 1)));
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000923 return PyInt_FromLong(x);
924}
925
926static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000927lu_uint(const char *p, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000928{
Guido van Rossum6c87eca1997-01-03 19:08:16 +0000929 unsigned long x = 0;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000930 int i = f->size;
931 do {
932 x = (x<<8) | (p[--i] & 0xFF);
933 } while (i > 0);
Guido van Rossum39ef2271998-06-29 04:00:40 +0000934 if (f->size >= 4)
935 return PyLong_FromUnsignedLong(x);
936 else
937 return PyInt_FromLong((long)x);
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000938}
939
Guido van Rossum74679b41997-01-02 22:21:36 +0000940static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000941lu_float(const char *p, const formatdef *f)
Guido van Rossum74679b41997-01-02 22:21:36 +0000942{
943 return unpack_float(p+3, -1);
944}
945
946static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000947lu_double(const char *p, const formatdef *f)
Guido van Rossum74679b41997-01-02 22:21:36 +0000948{
949 return unpack_double(p+7, -1);
950}
951
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000952static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000953lp_int(char *p, PyObject *v, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000954{
955 long x;
956 int i;
957 if (get_long(v, &x) < 0)
958 return -1;
959 i = f->size;
960 do {
Guido van Rossum7844e381997-04-11 20:44:04 +0000961 *p++ = (char)x;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000962 x >>= 8;
963 } while (--i > 0);
964 return 0;
965}
966
Guido van Rossum60c50611996-12-31 16:29:52 +0000967static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000968lp_uint(char *p, PyObject *v, const formatdef *f)
Guido van Rossum60c50611996-12-31 16:29:52 +0000969{
970 unsigned long x;
971 int i;
972 if (get_ulong(v, &x) < 0)
973 return -1;
974 i = f->size;
975 do {
Guido van Rossum7844e381997-04-11 20:44:04 +0000976 *p++ = (char)x;
Guido van Rossum60c50611996-12-31 16:29:52 +0000977 x >>= 8;
978 } while (--i > 0);
979 return 0;
980}
981
Guido van Rossum74679b41997-01-02 22:21:36 +0000982static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000983lp_float(char *p, PyObject *v, const formatdef *f)
Guido van Rossum74679b41997-01-02 22:21:36 +0000984{
985 double x = PyFloat_AsDouble(v);
986 if (x == -1 && PyErr_Occurred()) {
987 PyErr_SetString(StructError,
988 "required argument is not a float");
989 return -1;
990 }
991 return pack_float(x, p+3, -1);
992}
993
994static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000995lp_double(char *p, PyObject *v, const formatdef *f)
Guido van Rossum74679b41997-01-02 22:21:36 +0000996{
997 double x = PyFloat_AsDouble(v);
998 if (x == -1 && PyErr_Occurred()) {
999 PyErr_SetString(StructError,
1000 "required argument is not a float");
1001 return -1;
1002 }
1003 return pack_double(x, p+7, -1);
1004}
1005
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001006static formatdef lilendian_table[] = {
1007 {'x', 1, 0, NULL},
1008 {'b', 1, 0, lu_int, lp_int},
1009 {'B', 1, 0, lu_uint, lp_int},
1010 {'c', 1, 0, nu_char, np_char},
1011 {'s', 1, 0, NULL},
Guido van Rossum9eb671f1997-09-05 07:08:39 +00001012 {'p', 1, 0, NULL},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001013 {'h', 2, 0, lu_int, lp_int},
Guido van Rossum60c50611996-12-31 16:29:52 +00001014 {'H', 2, 0, lu_uint, lp_uint},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001015 {'i', 4, 0, lu_int, lp_int},
Guido van Rossum60c50611996-12-31 16:29:52 +00001016 {'I', 4, 0, lu_uint, lp_uint},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001017 {'l', 4, 0, lu_int, lp_int},
Guido van Rossum60c50611996-12-31 16:29:52 +00001018 {'L', 4, 0, lu_uint, lp_uint},
Guido van Rossum74679b41997-01-02 22:21:36 +00001019 {'f', 4, 0, lu_float, lp_float},
1020 {'d', 8, 0, lu_double, lp_double},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001021 {0}
1022};
1023
1024
1025static const formatdef *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +00001026whichtable(char **pfmt)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001027{
1028 const char *fmt = (*pfmt)++; /* May be backed out of later */
1029 switch (*fmt) {
1030 case '<':
1031 return lilendian_table;
1032 case '>':
1033 case '!': /* Network byte order is big-endian */
1034 return bigendian_table;
1035 case '=': { /* Host byte order -- different from native in aligment! */
1036 int n = 1;
1037 char *p = (char *) &n;
1038 if (*p == 1)
1039 return lilendian_table;
1040 else
1041 return bigendian_table;
1042 }
1043 default:
1044 --*pfmt; /* Back out of pointer increment */
1045 /* Fall through */
1046 case '@':
1047 return native_table;
1048 }
1049}
1050
1051
1052/* Get the table entry for a format code */
1053
1054static const formatdef *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +00001055getentry(int c, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001056{
1057 for (; f->format != '\0'; f++) {
1058 if (f->format == c) {
1059 return f;
1060 }
1061 }
1062 PyErr_SetString(StructError, "bad char in struct format");
1063 return NULL;
1064}
1065
1066
Guido van Rossum02975121992-08-17 08:55:12 +00001067/* Align a size according to a format code */
1068
1069static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +00001070align(int size, int c, const formatdef *e)
Guido van Rossum02975121992-08-17 08:55:12 +00001071{
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001072 if (e->format == c) {
1073 if (e->alignment) {
1074 size = ((size + e->alignment - 1)
1075 / e->alignment)
1076 * e->alignment;
1077 }
Guido van Rossum02975121992-08-17 08:55:12 +00001078 }
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001079 return size;
Guido van Rossum02975121992-08-17 08:55:12 +00001080}
1081
1082
1083/* calculate the size of a format string */
1084
1085static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +00001086calcsize(const char *fmt, const formatdef *f)
Guido van Rossum02975121992-08-17 08:55:12 +00001087{
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001088 const formatdef *e;
1089 const char *s;
Guido van Rossum02975121992-08-17 08:55:12 +00001090 char c;
1091 int size, num, itemsize, x;
1092
1093 s = fmt;
1094 size = 0;
1095 while ((c = *s++) != '\0') {
Guido van Rossum730806d1998-04-10 22:27:42 +00001096 if (isspace((int)c))
Guido van Rossume20aef51997-08-26 20:39:54 +00001097 continue;
Guido van Rossum02975121992-08-17 08:55:12 +00001098 if ('0' <= c && c <= '9') {
1099 num = c - '0';
1100 while ('0' <= (c = *s++) && c <= '9') {
1101 x = num*10 + (c - '0');
1102 if (x/10 != num) {
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001103 PyErr_SetString(
1104 StructError,
1105 "overflow in item count");
Guido van Rossum02975121992-08-17 08:55:12 +00001106 return -1;
1107 }
1108 num = x;
1109 }
1110 if (c == '\0')
1111 break;
1112 }
1113 else
1114 num = 1;
Tim Peters2d4e0772001-06-11 16:57:33 +00001115
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001116 e = getentry(c, f);
1117 if (e == NULL)
Guido van Rossum02975121992-08-17 08:55:12 +00001118 return -1;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001119 itemsize = e->size;
1120 size = align(size, c, e);
Guido van Rossum02975121992-08-17 08:55:12 +00001121 x = num * itemsize;
1122 size += x;
1123 if (x/itemsize != num || size < 0) {
Barry Warsaw30695fa1996-12-12 23:32:31 +00001124 PyErr_SetString(StructError,
1125 "total struct size too long");
Guido van Rossum02975121992-08-17 08:55:12 +00001126 return -1;
1127 }
Guido van Rossum02975121992-08-17 08:55:12 +00001128 }
1129
1130 return size;
1131}
1132
1133
Guido van Rossum414fd481997-12-19 04:24:24 +00001134static char calcsize__doc__[] = "\
1135calcsize(fmt) -> int\n\
1136Return size of C struct described by format string fmt.\n\
1137See struct.__doc__ for more on format strings.";
Guido van Rossum02975121992-08-17 08:55:12 +00001138
Barry Warsaw30695fa1996-12-12 23:32:31 +00001139static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +00001140struct_calcsize(PyObject *self, PyObject *args)
Guido van Rossum02975121992-08-17 08:55:12 +00001141{
1142 char *fmt;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001143 const formatdef *f;
Guido van Rossum02975121992-08-17 08:55:12 +00001144 int size;
1145
Guido van Rossum43713e52000-02-29 13:59:29 +00001146 if (!PyArg_ParseTuple(args, "s:calcsize", &fmt))
Guido van Rossum02975121992-08-17 08:55:12 +00001147 return NULL;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001148 f = whichtable(&fmt);
1149 size = calcsize(fmt, f);
Guido van Rossum02975121992-08-17 08:55:12 +00001150 if (size < 0)
1151 return NULL;
Barry Warsaw30695fa1996-12-12 23:32:31 +00001152 return PyInt_FromLong((long)size);
Guido van Rossum02975121992-08-17 08:55:12 +00001153}
1154
1155
Guido van Rossum414fd481997-12-19 04:24:24 +00001156static char pack__doc__[] = "\
1157pack(fmt, v1, v2, ...) -> string\n\
1158Return string containing values v1, v2, ... packed according to fmt.\n\
1159See struct.__doc__ for more on format strings.";
Guido van Rossum02975121992-08-17 08:55:12 +00001160
Barry Warsaw30695fa1996-12-12 23:32:31 +00001161static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +00001162struct_pack(PyObject *self, PyObject *args)
Guido van Rossum02975121992-08-17 08:55:12 +00001163{
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001164 const formatdef *f, *e;
Barry Warsaw30695fa1996-12-12 23:32:31 +00001165 PyObject *format, *result, *v;
Guido van Rossum02975121992-08-17 08:55:12 +00001166 char *fmt;
1167 int size, num;
1168 int i, n;
Guido van Rossumb9d338c1997-01-03 15:40:33 +00001169 char *s, *res, *restart, *nres;
Guido van Rossum02975121992-08-17 08:55:12 +00001170 char c;
Guido van Rossum02975121992-08-17 08:55:12 +00001171
Barry Warsaw30695fa1996-12-12 23:32:31 +00001172 if (args == NULL || !PyTuple_Check(args) ||
1173 (n = PyTuple_Size(args)) < 1)
1174 {
Tim Peters2d4e0772001-06-11 16:57:33 +00001175 PyErr_SetString(PyExc_TypeError,
Fred Drake137507e2000-06-01 02:02:46 +00001176 "struct.pack requires at least one argument");
Guido van Rossum02975121992-08-17 08:55:12 +00001177 return NULL;
1178 }
Barry Warsaw30695fa1996-12-12 23:32:31 +00001179 format = PyTuple_GetItem(args, 0);
1180 if (!PyArg_Parse(format, "s", &fmt))
Guido van Rossum02975121992-08-17 08:55:12 +00001181 return NULL;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001182 f = whichtable(&fmt);
1183 size = calcsize(fmt, f);
Guido van Rossum02975121992-08-17 08:55:12 +00001184 if (size < 0)
1185 return NULL;
Barry Warsaw30695fa1996-12-12 23:32:31 +00001186 result = PyString_FromStringAndSize((char *)NULL, size);
Guido van Rossum02975121992-08-17 08:55:12 +00001187 if (result == NULL)
1188 return NULL;
1189
1190 s = fmt;
1191 i = 1;
Barry Warsaw30695fa1996-12-12 23:32:31 +00001192 res = restart = PyString_AsString(result);
Guido van Rossum02975121992-08-17 08:55:12 +00001193
1194 while ((c = *s++) != '\0') {
Guido van Rossum730806d1998-04-10 22:27:42 +00001195 if (isspace((int)c))
Guido van Rossume20aef51997-08-26 20:39:54 +00001196 continue;
Guido van Rossum02975121992-08-17 08:55:12 +00001197 if ('0' <= c && c <= '9') {
1198 num = c - '0';
1199 while ('0' <= (c = *s++) && c <= '9')
1200 num = num*10 + (c - '0');
1201 if (c == '\0')
1202 break;
1203 }
1204 else
1205 num = 1;
1206
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001207 e = getentry(c, f);
1208 if (e == NULL)
1209 goto fail;
Guido van Rossumb9d338c1997-01-03 15:40:33 +00001210 nres = restart + align((int)(res-restart), c, e);
1211 /* Fill padd bytes with zeros */
1212 while (res < nres)
1213 *res++ = '\0';
Guido van Rossum3aa27fd1996-12-31 02:10:45 +00001214 if (num == 0 && c != 's')
1215 continue;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001216 do {
1217 if (c == 'x') {
1218 /* doesn't consume arguments */
Guido van Rossum3aa27fd1996-12-31 02:10:45 +00001219 memset(res, '\0', num);
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001220 res += num;
Guido van Rossum02975121992-08-17 08:55:12 +00001221 break;
Guido van Rossum02975121992-08-17 08:55:12 +00001222 }
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001223 if (i >= n) {
1224 PyErr_SetString(StructError,
1225 "insufficient arguments to pack");
1226 goto fail;
1227 }
1228 v = PyTuple_GetItem(args, i++);
1229 if (v == NULL)
1230 goto fail;
1231 if (c == 's') {
1232 /* num is string size, not repeat count */
1233 int n;
1234 if (!PyString_Check(v)) {
1235 PyErr_SetString(StructError,
1236 "argument for 's' must be a string");
1237 goto fail;
1238 }
1239 n = PyString_Size(v);
1240 if (n > num)
1241 n = num;
1242 if (n > 0)
1243 memcpy(res, PyString_AsString(v), n);
1244 if (n < num)
Guido van Rossum3aa27fd1996-12-31 02:10:45 +00001245 memset(res+n, '\0', num-n);
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001246 res += num;
1247 break;
1248 }
Guido van Rossum9eb671f1997-09-05 07:08:39 +00001249 else if (c == 'p') {
1250 /* num is string size + 1,
1251 to fit in the count byte */
1252 int n;
1253 num--; /* now num is max string size */
1254 if (!PyString_Check(v)) {
1255 PyErr_SetString(StructError,
1256 "argument for 'p' must be a string");
1257 goto fail;
1258 }
1259 n = PyString_Size(v);
1260 if (n > num)
1261 n = num;
1262 if (n > 0)
1263 memcpy(res+1, PyString_AsString(v), n);
1264 if (n < num)
1265 /* no real need, just to be nice */
1266 memset(res+1+n, '\0', num-n);
1267 *res++ = n; /* store the length byte */
1268 res += num;
1269 break;
1270 }
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001271 else {
1272 if (e->pack(res, v, e) < 0)
1273 goto fail;
1274 res += e->size;
1275 }
1276 } while (--num > 0);
Guido van Rossum02975121992-08-17 08:55:12 +00001277 }
1278
1279 if (i < n) {
Barry Warsaw30695fa1996-12-12 23:32:31 +00001280 PyErr_SetString(StructError,
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001281 "too many arguments for pack format");
Guido van Rossum02975121992-08-17 08:55:12 +00001282 goto fail;
1283 }
1284
1285 return result;
1286
1287 fail:
Barry Warsaw30695fa1996-12-12 23:32:31 +00001288 Py_DECREF(result);
Guido van Rossum02975121992-08-17 08:55:12 +00001289 return NULL;
1290}
1291
1292
Guido van Rossum9897f0f1997-12-21 06:46:20 +00001293static char unpack__doc__[] = "\
1294unpack(fmt, string) -> (v1, v2, ...)\n\
1295Unpack the string, containing packed C structure data, according\n\
1296to fmt. Requires len(string)==calcsize(fmt).\n\
Guido van Rossum414fd481997-12-19 04:24:24 +00001297See struct.__doc__ for more on format strings.";
Guido van Rossum02975121992-08-17 08:55:12 +00001298
Barry Warsaw30695fa1996-12-12 23:32:31 +00001299static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +00001300struct_unpack(PyObject *self, PyObject *args)
Guido van Rossum02975121992-08-17 08:55:12 +00001301{
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001302 const formatdef *f, *e;
Guido van Rossum02975121992-08-17 08:55:12 +00001303 char *str, *start, *fmt, *s;
1304 char c;
Barry Warsawb9a781e1997-01-03 00:26:28 +00001305 int len, size, num;
Barry Warsaw30695fa1996-12-12 23:32:31 +00001306 PyObject *res, *v;
Guido van Rossum02975121992-08-17 08:55:12 +00001307
Guido van Rossum43713e52000-02-29 13:59:29 +00001308 if (!PyArg_ParseTuple(args, "ss#:unpack", &fmt, &start, &len))
Guido van Rossum02975121992-08-17 08:55:12 +00001309 return NULL;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001310 f = whichtable(&fmt);
1311 size = calcsize(fmt, f);
1312 if (size < 0)
1313 return NULL;
Guido van Rossum02975121992-08-17 08:55:12 +00001314 if (size != len) {
Barry Warsaw30695fa1996-12-12 23:32:31 +00001315 PyErr_SetString(StructError,
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001316 "unpack str size does not match format");
Guido van Rossum02975121992-08-17 08:55:12 +00001317 return NULL;
1318 }
Barry Warsaw30695fa1996-12-12 23:32:31 +00001319 res = PyList_New(0);
Guido van Rossum02975121992-08-17 08:55:12 +00001320 if (res == NULL)
1321 return NULL;
1322 str = start;
1323 s = fmt;
1324 while ((c = *s++) != '\0') {
Guido van Rossum730806d1998-04-10 22:27:42 +00001325 if (isspace((int)c))
Guido van Rossume20aef51997-08-26 20:39:54 +00001326 continue;
Guido van Rossum02975121992-08-17 08:55:12 +00001327 if ('0' <= c && c <= '9') {
1328 num = c - '0';
1329 while ('0' <= (c = *s++) && c <= '9')
1330 num = num*10 + (c - '0');
1331 if (c == '\0')
1332 break;
1333 }
1334 else
1335 num = 1;
1336
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001337 e = getentry(c, f);
1338 if (e == NULL)
1339 goto fail;
1340 str = start + align((int)(str-start), c, e);
Guido van Rossum3aa27fd1996-12-31 02:10:45 +00001341 if (num == 0 && c != 's')
1342 continue;
Guido van Rossum02975121992-08-17 08:55:12 +00001343
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001344 do {
1345 if (c == 'x') {
1346 str += num;
Guido van Rossum02975121992-08-17 08:55:12 +00001347 break;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001348 }
1349 if (c == 's') {
1350 /* num is string size, not repeat count */
1351 v = PyString_FromStringAndSize(str, num);
1352 if (v == NULL)
1353 goto fail;
1354 str += num;
1355 num = 0;
1356 }
Guido van Rossum9eb671f1997-09-05 07:08:39 +00001357 else if (c == 'p') {
1358 /* num is string buffer size,
1359 not repeat count */
1360 int n = *(unsigned char*)str;
1361 /* first byte (unsigned) is string size */
1362 if (n >= num)
1363 n = num-1;
1364 v = PyString_FromStringAndSize(str+1, n);
1365 if (v == NULL)
1366 goto fail;
1367 str += num;
1368 num = 0;
1369 }
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001370 else {
1371 v = e->unpack(str, e);
1372 if (v == NULL)
1373 goto fail;
1374 str += e->size;
Guido van Rossum02975121992-08-17 08:55:12 +00001375 }
Barry Warsaw30695fa1996-12-12 23:32:31 +00001376 if (v == NULL || PyList_Append(res, v) < 0)
Guido van Rossum02975121992-08-17 08:55:12 +00001377 goto fail;
Barry Warsaw30695fa1996-12-12 23:32:31 +00001378 Py_DECREF(v);
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001379 } while (--num > 0);
Guido van Rossum02975121992-08-17 08:55:12 +00001380 }
1381
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001382 v = PyList_AsTuple(res);
1383 Py_DECREF(res);
1384 return v;
Guido van Rossum02975121992-08-17 08:55:12 +00001385
1386 fail:
Barry Warsaw30695fa1996-12-12 23:32:31 +00001387 Py_DECREF(res);
Guido van Rossum02975121992-08-17 08:55:12 +00001388 return NULL;
1389}
1390
Guido van Rossum90ddb7b1992-08-19 16:44:15 +00001391
Guido van Rossum02975121992-08-17 08:55:12 +00001392/* List of functions */
1393
Barry Warsaw30695fa1996-12-12 23:32:31 +00001394static PyMethodDef struct_methods[] = {
Guido van Rossum414fd481997-12-19 04:24:24 +00001395 {"calcsize", struct_calcsize, METH_VARARGS, calcsize__doc__},
1396 {"pack", struct_pack, METH_VARARGS, pack__doc__},
1397 {"unpack", struct_unpack, METH_VARARGS, unpack__doc__},
Guido van Rossum02975121992-08-17 08:55:12 +00001398 {NULL, NULL} /* sentinel */
1399};
1400
1401
1402/* Module initialization */
1403
Guido van Rossum3886bb61998-12-04 18:50:17 +00001404DL_EXPORT(void)
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001405initstruct(void)
Guido van Rossum02975121992-08-17 08:55:12 +00001406{
Barry Warsaw30695fa1996-12-12 23:32:31 +00001407 PyObject *m, *d;
Guido van Rossum02975121992-08-17 08:55:12 +00001408
1409 /* Create the module and add the functions */
Guido van Rossum414fd481997-12-19 04:24:24 +00001410 m = Py_InitModule4("struct", struct_methods, struct__doc__,
1411 (PyObject*)NULL, PYTHON_API_VERSION);
Guido van Rossum02975121992-08-17 08:55:12 +00001412
1413 /* Add some symbolic constants to the module */
Barry Warsaw30695fa1996-12-12 23:32:31 +00001414 d = PyModule_GetDict(m);
Guido van Rossum0cb96de1997-10-01 04:29:29 +00001415 StructError = PyErr_NewException("struct.error", NULL, NULL);
1416 if (StructError == NULL)
1417 return;
Barry Warsaw30695fa1996-12-12 23:32:31 +00001418 PyDict_SetItemString(d, "error", StructError);
Guido van Rossum02975121992-08-17 08:55:12 +00001419}