blob: a168b9fc0aa5ca3af8ed2547e56c1243fc498543 [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\
12The optional first format char indicates byte ordering and alignment:\n\
13 @: native w/native alignment(default)\n\
14 =: native w/standard alignment\n\
15 <: little-endian, std. alignment\n\
16 >: big-endian, std. alignment\n\
17 !: network, std (same as >)\n\
18\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
49** on the PowerPC mac.
50*/
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))
75
76#else
77static char qQ_error_msg[] =
78"q and Q unavailable in native mode on this platform; use a standard mode.\0";
79
80#endif
81
Martin v. Löwis2af72d52000-09-15 08:10:33 +000082#define STRINGIFY(x) #x
83
Jack Jansen971e1df1995-02-02 14:29:10 +000084#ifdef __powerc
85#pragma options align=reset
86#endif
87
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +000088/* Helper routine to get a Python integer and raise the appropriate error
89 if it isn't one */
90
91static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +000092get_long(PyObject *v, long *p)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +000093{
94 long x = PyInt_AsLong(v);
95 if (x == -1 && PyErr_Occurred()) {
Fred Draked3dbb381998-05-28 04:35:49 +000096 if (PyErr_ExceptionMatches(PyExc_TypeError))
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +000097 PyErr_SetString(StructError,
98 "required argument is not an integer");
99 return -1;
100 }
101 *p = x;
102 return 0;
103}
104
105
Guido van Rossum60c50611996-12-31 16:29:52 +0000106/* Same, but handling unsigned long */
107
108static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000109get_ulong(PyObject *v, unsigned long *p)
Guido van Rossum60c50611996-12-31 16:29:52 +0000110{
Guido van Rossum6c87eca1997-01-03 19:08:16 +0000111 if (PyLong_Check(v)) {
112 unsigned long x = PyLong_AsUnsignedLong(v);
113 if (x == (unsigned long)(-1) && PyErr_Occurred())
Guido van Rossum60c50611996-12-31 16:29:52 +0000114 return -1;
Guido van Rossum6c87eca1997-01-03 19:08:16 +0000115 *p = x;
116 return 0;
Guido van Rossum60c50611996-12-31 16:29:52 +0000117 }
Guido van Rossum6c87eca1997-01-03 19:08:16 +0000118 else {
119 return get_long(v, (long *)p);
120 }
Guido van Rossum60c50611996-12-31 16:29:52 +0000121}
122
Tim Peters7b9542a2001-06-10 23:40:19 +0000123#ifdef HAVE_LONG_LONG
124
125/* Same, but handling native long long. */
126
127static int
128get_longlong(PyObject *v, LONG_LONG *p)
129{
130 LONG_LONG x;
131 int v_needs_decref = 0;
132
133 if (PyInt_Check(v)) {
134 x = (LONG_LONG)PyInt_AS_LONG(v);
135 *p = x;
136 return 0;
137 }
138 if (!PyLong_Check(v)) {
139 PyNumberMethods *m = v->ob_type->tp_as_number;
140 if (m != NULL && m->nb_long != NULL) {
141 v = m->nb_long(v);
142 if (v == NULL)
143 return -1;
144 v_needs_decref = 1;
145 }
146 if (!PyLong_Check(v)) {
147 PyErr_SetString(StructError,
148 "cannot convert argument to long");
149 if (v_needs_decref)
150 Py_DECREF(v);
151 return -1;
152 }
153 }
154 assert(PyLong_Check(v));
155 x = PyLong_AsLongLong(v);
156 if (v_needs_decref)
157 Py_DECREF(v);
158 if (x == (LONG_LONG)-1 && PyErr_Occurred())
159 return -1;
160 *p = x;
161 return 0;
162}
163
164/* Same, but handling native unsigned long long. */
165
166static int
167get_ulonglong(PyObject *v, unsigned LONG_LONG *p)
168{
169 unsigned LONG_LONG x;
170 int v_needs_decref = 0;
171
172 if (PyInt_Check(v)) {
173 long i = PyInt_AS_LONG(v);
174 if (i < 0) {
175 PyErr_SetString(StructError, "can't convert negative "
176 "int to unsigned");
177 return -1;
178 }
179 x = (unsigned LONG_LONG)i;
180 *p = x;
181 return 0;
182 }
183 if (!PyLong_Check(v)) {
184 PyNumberMethods *m = v->ob_type->tp_as_number;
185 if (m != NULL && m->nb_long != NULL) {
186 v = m->nb_long(v);
187 if (v == NULL)
188 return -1;
189 v_needs_decref = 1;
190 }
191 if (!PyLong_Check(v)) {
192 PyErr_SetString(StructError,
193 "cannot convert argument to long");
194 if (v_needs_decref)
195 Py_DECREF(v);
196 return -1;
197 }
198 }
199 assert(PyLong_Check(v));
200 x = PyLong_AsUnsignedLongLong(v);
201 if (v_needs_decref)
202 Py_DECREF(v);
203 if (x == (unsigned LONG_LONG)-1 && PyErr_Occurred())
204 return -1;
205 *p = x;
206 return 0;
207}
208
209#endif
Guido van Rossum60c50611996-12-31 16:29:52 +0000210
Guido van Rossum74679b41997-01-02 22:21:36 +0000211/* Floating point helpers */
212
213/* These use ANSI/IEEE Standard 754-1985 (Standard for Binary Floating
214 Point Arithmetic). See the following URL:
215 http://www.psc.edu/general/software/packages/ieee/ieee.html */
216
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000217/* XXX Inf/NaN are not handled quite right (but underflow is!) */
Guido van Rossum74679b41997-01-02 22:21:36 +0000218
219static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000220pack_float(double x, /* The number to pack */
221 char *p, /* Where to pack the high order byte */
222 int incr) /* 1 for big-endian; -1 for little-endian */
Guido van Rossum74679b41997-01-02 22:21:36 +0000223{
224 int s;
225 int e;
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000226 double f;
227 long fbits;
Guido van Rossum74679b41997-01-02 22:21:36 +0000228
229 if (x < 0) {
230 s = 1;
231 x = -x;
232 }
233 else
234 s = 0;
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000235
236 f = frexp(x, &e);
237
238 /* Normalize f to be in the range [1.0, 2.0) */
239 if (0.5 <= f && f < 1.0) {
240 f *= 2.0;
Guido van Rossum74679b41997-01-02 22:21:36 +0000241 e--;
242 }
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000243 else if (f == 0.0) {
Guido van Rossum74679b41997-01-02 22:21:36 +0000244 e = 0;
245 }
246 else {
247 PyErr_SetString(PyExc_SystemError,
248 "frexp() result out of range");
249 return -1;
250 }
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000251
252 if (e >= 128) {
253 /* XXX 128 itself is reserved for Inf/NaN */
Guido van Rossum74679b41997-01-02 22:21:36 +0000254 PyErr_SetString(PyExc_OverflowError,
255 "float too large to pack with f format");
256 return -1;
257 }
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000258 else if (e < -126) {
259 /* Gradual underflow */
260 f = ldexp(f, 126 + e);
Guido van Rossum74679b41997-01-02 22:21:36 +0000261 e = 0;
262 }
Guido van Rossum8f3c8121997-11-04 17:12:33 +0000263 else if (!(e == 0 && f == 0.0)) {
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000264 e += 127;
265 f -= 1.0; /* Get rid of leading 1 */
Guido van Rossum74679b41997-01-02 22:21:36 +0000266 }
267
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000268 f *= 8388608.0; /* 2**23 */
269 fbits = (long) floor(f + 0.5); /* Round */
270
Guido van Rossum74679b41997-01-02 22:21:36 +0000271 /* First byte */
272 *p = (s<<7) | (e>>1);
273 p += incr;
274
275 /* Second byte */
Guido van Rossum7844e381997-04-11 20:44:04 +0000276 *p = (char) (((e&1)<<7) | (fbits>>16));
Guido van Rossum74679b41997-01-02 22:21:36 +0000277 p += incr;
278
279 /* Third byte */
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000280 *p = (fbits>>8) & 0xFF;
Guido van Rossum74679b41997-01-02 22:21:36 +0000281 p += incr;
282
283 /* Fourth byte */
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000284 *p = fbits&0xFF;
Guido van Rossum74679b41997-01-02 22:21:36 +0000285
286 /* Done */
287 return 0;
288}
289
290static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000291pack_double(double x, /* The number to pack */
292 char *p, /* Where to pack the high order byte */
293 int incr) /* 1 for big-endian; -1 for little-endian */
Guido van Rossum74679b41997-01-02 22:21:36 +0000294{
295 int s;
296 int e;
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000297 double f;
Guido van Rossum74679b41997-01-02 22:21:36 +0000298 long fhi, flo;
299
300 if (x < 0) {
301 s = 1;
302 x = -x;
303 }
304 else
305 s = 0;
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000306
307 f = frexp(x, &e);
308
309 /* Normalize f to be in the range [1.0, 2.0) */
310 if (0.5 <= f && f < 1.0) {
311 f *= 2.0;
Guido van Rossum74679b41997-01-02 22:21:36 +0000312 e--;
313 }
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000314 else if (f == 0.0) {
Guido van Rossum74679b41997-01-02 22:21:36 +0000315 e = 0;
316 }
317 else {
318 PyErr_SetString(PyExc_SystemError,
319 "frexp() result out of range");
320 return -1;
321 }
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000322
323 if (e >= 1024) {
324 /* XXX 1024 itself is reserved for Inf/NaN */
Guido van Rossum74679b41997-01-02 22:21:36 +0000325 PyErr_SetString(PyExc_OverflowError,
326 "float too large to pack with d format");
327 return -1;
328 }
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000329 else if (e < -1022) {
330 /* Gradual underflow */
331 f = ldexp(f, 1022 + e);
Guido van Rossum74679b41997-01-02 22:21:36 +0000332 e = 0;
333 }
Guido van Rossum8f3c8121997-11-04 17:12:33 +0000334 else if (!(e == 0 && f == 0.0)) {
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000335 e += 1023;
336 f -= 1.0; /* Get rid of leading 1 */
Guido van Rossum74679b41997-01-02 22:21:36 +0000337 }
338
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000339 /* fhi receives the high 28 bits; flo the low 24 bits (== 52 bits) */
340 f *= 268435456.0; /* 2**28 */
341 fhi = (long) floor(f); /* Truncate */
342 f -= (double)fhi;
343 f *= 16777216.0; /* 2**24 */
344 flo = (long) floor(f + 0.5); /* Round */
345
Guido van Rossum74679b41997-01-02 22:21:36 +0000346 /* First byte */
347 *p = (s<<7) | (e>>4);
348 p += incr;
349
350 /* Second byte */
Guido van Rossum7844e381997-04-11 20:44:04 +0000351 *p = (char) (((e&0xF)<<4) | (fhi>>24));
Guido van Rossum74679b41997-01-02 22:21:36 +0000352 p += incr;
353
354 /* Third byte */
355 *p = (fhi>>16) & 0xFF;
356 p += incr;
357
358 /* Fourth byte */
359 *p = (fhi>>8) & 0xFF;
360 p += incr;
361
362 /* Fifth byte */
363 *p = fhi & 0xFF;
364 p += incr;
365
366 /* Sixth byte */
367 *p = (flo>>16) & 0xFF;
368 p += incr;
369
370 /* Seventh byte */
371 *p = (flo>>8) & 0xFF;
372 p += incr;
373
374 /* Eighth byte */
375 *p = flo & 0xFF;
376 p += incr;
377
378 /* Done */
379 return 0;
380}
381
382static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000383unpack_float(const char *p, /* Where the high order byte is */
384 int incr) /* 1 for big-endian; -1 for little-endian */
Guido van Rossum74679b41997-01-02 22:21:36 +0000385{
386 int s;
387 int e;
388 long f;
389 double x;
390
391 /* First byte */
392 s = (*p>>7) & 1;
393 e = (*p & 0x7F) << 1;
394 p += incr;
395
396 /* Second byte */
397 e |= (*p>>7) & 1;
398 f = (*p & 0x7F) << 16;
399 p += incr;
400
401 /* Third byte */
402 f |= (*p & 0xFF) << 8;
403 p += incr;
404
405 /* Fourth byte */
406 f |= *p & 0xFF;
407
408 x = (double)f / 8388608.0;
409
410 /* XXX This sadly ignores Inf/NaN issues */
Guido van Rossum07ef6551997-01-02 22:31:07 +0000411 if (e == 0)
412 e = -126;
413 else {
414 x += 1.0;
415 e -= 127;
416 }
417 x = ldexp(x, e);
Guido van Rossum74679b41997-01-02 22:21:36 +0000418
419 if (s)
420 x = -x;
421
422 return PyFloat_FromDouble(x);
423}
424
425static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000426unpack_double(const char *p, /* Where the high order byte is */
427 int incr) /* 1 for big-endian; -1 for little-endian */
Guido van Rossum74679b41997-01-02 22:21:36 +0000428{
429 int s;
430 int e;
431 long fhi, flo;
432 double x;
433
434 /* First byte */
435 s = (*p>>7) & 1;
436 e = (*p & 0x7F) << 4;
437 p += incr;
438
439 /* Second byte */
440 e |= (*p>>4) & 0xF;
441 fhi = (*p & 0xF) << 24;
442 p += incr;
443
444 /* Third byte */
445 fhi |= (*p & 0xFF) << 16;
446 p += incr;
447
448 /* Fourth byte */
449 fhi |= (*p & 0xFF) << 8;
450 p += incr;
451
452 /* Fifth byte */
453 fhi |= *p & 0xFF;
454 p += incr;
455
456 /* Sixth byte */
457 flo = (*p & 0xFF) << 16;
458 p += incr;
459
460 /* Seventh byte */
461 flo |= (*p & 0xFF) << 8;
462 p += incr;
463
464 /* Eighth byte */
465 flo |= *p & 0xFF;
466 p += incr;
467
468 x = (double)fhi + (double)flo / 16777216.0; /* 2**24 */
469 x /= 268435456.0; /* 2**28 */
470
471 /* XXX This sadly ignores Inf/NaN */
Guido van Rossum07ef6551997-01-02 22:31:07 +0000472 if (e == 0)
473 e = -1022;
474 else {
475 x += 1.0;
476 e -= 1023;
477 }
478 x = ldexp(x, e);
Guido van Rossum74679b41997-01-02 22:21:36 +0000479
480 if (s)
481 x = -x;
482
483 return PyFloat_FromDouble(x);
484}
485
486
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000487/* The translation function for each format character is table driven */
488
489typedef struct _formatdef {
490 char format;
491 int size;
492 int alignment;
Tim Petersdbd9ba62000-07-09 03:09:57 +0000493 PyObject* (*unpack)(const char *,
494 const struct _formatdef *);
495 int (*pack)(char *, PyObject *,
496 const struct _formatdef *);
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000497} formatdef;
498
Tim Peters7b9542a2001-06-10 23:40:19 +0000499/* A large number of small routines follow, with names of the form
500
501 [bln][up]_TYPE
502
503 [bln] distiguishes among big-endian, little-endian and native.
504 [pu] distiguishes between pack (to struct) and unpack (from struct).
505 TYPE is one of char, byte, ubyte, etc.
506*/
507
508/* Native mode routines. */
509
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000510static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000511nu_char(const char *p, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000512{
513 return PyString_FromStringAndSize(p, 1);
514}
515
516static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000517nu_byte(const char *p, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000518{
519 return PyInt_FromLong((long) *(signed char *)p);
520}
521
522static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000523nu_ubyte(const char *p, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000524{
525 return PyInt_FromLong((long) *(unsigned char *)p);
526}
527
528static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000529nu_short(const char *p, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000530{
531 return PyInt_FromLong((long) *(short *)p);
532}
533
534static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000535nu_ushort(const char *p, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000536{
537 return PyInt_FromLong((long) *(unsigned short *)p);
538}
539
540static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000541nu_int(const char *p, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000542{
543 return PyInt_FromLong((long) *(int *)p);
544}
545
546static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000547nu_uint(const char *p, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000548{
549 unsigned int x = *(unsigned int *)p;
Guido van Rossum6c87eca1997-01-03 19:08:16 +0000550 return PyLong_FromUnsignedLong((unsigned long)x);
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000551}
552
553static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000554nu_long(const char *p, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000555{
556 return PyInt_FromLong(*(long *)p);
557}
558
559static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000560nu_ulong(const char *p, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000561{
Guido van Rossum6c87eca1997-01-03 19:08:16 +0000562 return PyLong_FromUnsignedLong(*(unsigned long *)p);
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000563}
564
Tim Peters7b9542a2001-06-10 23:40:19 +0000565/* Native mode doesn't support q or Q unless the platform C supports
566 long long (or, on Windows, __int64). */
567
568#ifdef HAVE_LONG_LONG
569
570static PyObject *
571nu_longlong(const char *p, const formatdef *f)
572{
573 return PyLong_FromLongLong(*(LONG_LONG *)p);
574}
575
576static PyObject *
577nu_ulonglong(const char *p, const formatdef *f)
578{
579 return PyLong_FromUnsignedLongLong(*(unsigned LONG_LONG *)p);
580}
581
582#else
583
584static PyObject *
585nu_qQerror(const char *p, const formatdef *f)
586{
587 PyErr_SetString(StructError, qQ_error_msg);
588 return NULL;
589}
590
591#endif
592
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000593static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000594nu_float(const char *p, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000595{
596 float x;
597 memcpy((char *)&x, p, sizeof(float));
598 return PyFloat_FromDouble((double)x);
599}
600
601static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000602nu_double(const char *p, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000603{
604 double x;
605 memcpy((char *)&x, p, sizeof(double));
606 return PyFloat_FromDouble(x);
607}
608
Guido van Rossum78694d91998-09-18 14:14:13 +0000609static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000610nu_void_p(const char *p, const formatdef *f)
Guido van Rossum78694d91998-09-18 14:14:13 +0000611{
612 return PyLong_FromVoidPtr(*(void **)p);
613}
614
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000615static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000616np_byte(char *p, PyObject *v, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000617{
618 long x;
619 if (get_long(v, &x) < 0)
620 return -1;
Martin v. Löwis66de5492000-09-15 07:31:57 +0000621 if (x < -128 || x > 127){
622 PyErr_SetString(StructError,
623 "byte format requires -128<=number<=127");
624 return -1;
625 }
626 *p = (char)x;
627 return 0;
628}
629
630static int
631np_ubyte(char *p, PyObject *v, const formatdef *f)
632{
633 long x;
634 if (get_long(v, &x) < 0)
635 return -1;
636 if (x < 0 || x > 255){
637 PyErr_SetString(StructError,
638 "ubyte format requires 0<=number<=255");
639 return -1;
640 }
Guido van Rossum7844e381997-04-11 20:44:04 +0000641 *p = (char)x;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000642 return 0;
643}
644
645static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000646np_char(char *p, PyObject *v, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000647{
648 if (!PyString_Check(v) || PyString_Size(v) != 1) {
649 PyErr_SetString(StructError,
650 "char format require string of length 1");
651 return -1;
652 }
653 *p = *PyString_AsString(v);
654 return 0;
655}
656
657static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000658np_short(char *p, PyObject *v, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000659{
660 long x;
661 if (get_long(v, &x) < 0)
662 return -1;
Martin v. Löwis2af72d52000-09-15 08:10:33 +0000663 if (x < SHRT_MIN || x > SHRT_MAX){
Martin v. Löwis66de5492000-09-15 07:31:57 +0000664 PyErr_SetString(StructError,
Martin v. Löwis2af72d52000-09-15 08:10:33 +0000665 "short format requires " STRINGIFY(SHRT_MIN)
666 "<=number<=" STRINGIFY(SHRT_MAX));
Martin v. Löwis66de5492000-09-15 07:31:57 +0000667 return -1;
668 }
Guido van Rossum7844e381997-04-11 20:44:04 +0000669 * (short *)p = (short)x;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000670 return 0;
671}
672
673static int
Martin v. Löwis66de5492000-09-15 07:31:57 +0000674np_ushort(char *p, PyObject *v, const formatdef *f)
675{
676 long x;
677 if (get_long(v, &x) < 0)
678 return -1;
Martin v. Löwis2af72d52000-09-15 08:10:33 +0000679 if (x < 0 || x > USHRT_MAX){
Martin v. Löwis66de5492000-09-15 07:31:57 +0000680 PyErr_SetString(StructError,
Martin v. Löwis2af72d52000-09-15 08:10:33 +0000681 "short format requires 0<=number<=" STRINGIFY(USHRT_MAX));
Martin v. Löwis66de5492000-09-15 07:31:57 +0000682 return -1;
683 }
684 * (unsigned short *)p = (unsigned short)x;
685 return 0;
686}
687
688static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000689np_int(char *p, PyObject *v, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000690{
691 long x;
692 if (get_long(v, &x) < 0)
693 return -1;
694 * (int *)p = x;
695 return 0;
696}
697
698static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000699np_uint(char *p, PyObject *v, const formatdef *f)
Guido van Rossum60c50611996-12-31 16:29:52 +0000700{
701 unsigned long x;
702 if (get_ulong(v, &x) < 0)
703 return -1;
704 * (unsigned int *)p = x;
705 return 0;
706}
707
708static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000709np_long(char *p, PyObject *v, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000710{
711 long x;
712 if (get_long(v, &x) < 0)
713 return -1;
714 * (long *)p = x;
715 return 0;
716}
717
718static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000719np_ulong(char *p, PyObject *v, const formatdef *f)
Guido van Rossum60c50611996-12-31 16:29:52 +0000720{
721 unsigned long x;
722 if (get_ulong(v, &x) < 0)
723 return -1;
724 * (unsigned long *)p = x;
725 return 0;
726}
727
Tim Peters7b9542a2001-06-10 23:40:19 +0000728#ifdef HAVE_LONG_LONG
729
730static int
731np_longlong(char *p, PyObject *v, const formatdef *f)
732{
733 LONG_LONG x;
734 if (get_longlong(v, &x) < 0)
735 return -1;
736 * (LONG_LONG *)p = x;
737 return 0;
738}
739
740static int
741np_ulonglong(char *p, PyObject *v, const formatdef *f)
742{
743 unsigned LONG_LONG x;
744 if (get_ulonglong(v, &x) < 0)
745 return -1;
746 * (unsigned LONG_LONG *)p = x;
747 return 0;
748}
749
750#else
751
752static int
753np_qQerror(char *p, PyObject *v, const formatdef *f)
754{
755 PyErr_SetString(StructError, qQ_error_msg);
756 return -1;
757}
758
759#endif
760
Guido van Rossum60c50611996-12-31 16:29:52 +0000761static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000762np_float(char *p, PyObject *v, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000763{
764 float x = (float)PyFloat_AsDouble(v);
765 if (x == -1 && PyErr_Occurred()) {
766 PyErr_SetString(StructError,
767 "required argument is not a float");
768 return -1;
769 }
770 memcpy(p, (char *)&x, sizeof(float));
771 return 0;
772}
773
774static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000775np_double(char *p, PyObject *v, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000776{
777 double x = PyFloat_AsDouble(v);
778 if (x == -1 && PyErr_Occurred()) {
779 PyErr_SetString(StructError,
780 "required argument is not a float");
781 return -1;
782 }
783 memcpy(p, (char *)&x, sizeof(double));
784 return 0;
785}
786
Guido van Rossum78694d91998-09-18 14:14:13 +0000787static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000788np_void_p(char *p, PyObject *v, const formatdef *f)
Guido van Rossum78694d91998-09-18 14:14:13 +0000789{
790 void *x = PyLong_AsVoidPtr(v);
791 if (x == NULL && PyErr_Occurred()) {
792 /* ### hrm. PyLong_AsVoidPtr raises SystemError */
793 if (PyErr_ExceptionMatches(PyExc_TypeError))
794 PyErr_SetString(StructError,
795 "required argument is not an integer");
796 return -1;
797 }
798 *(void **)p = x;
799 return 0;
800}
801
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000802static formatdef native_table[] = {
803 {'x', sizeof(char), 0, NULL},
804 {'b', sizeof(char), 0, nu_byte, np_byte},
Martin v. Löwis66de5492000-09-15 07:31:57 +0000805 {'B', sizeof(char), 0, nu_ubyte, np_ubyte},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000806 {'c', sizeof(char), 0, nu_char, np_char},
807 {'s', sizeof(char), 0, NULL},
Guido van Rossum9eb671f1997-09-05 07:08:39 +0000808 {'p', sizeof(char), 0, NULL},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000809 {'h', sizeof(short), SHORT_ALIGN, nu_short, np_short},
Martin v. Löwis66de5492000-09-15 07:31:57 +0000810 {'H', sizeof(short), SHORT_ALIGN, nu_ushort, np_ushort},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000811 {'i', sizeof(int), INT_ALIGN, nu_int, np_int},
Guido van Rossum60c50611996-12-31 16:29:52 +0000812 {'I', sizeof(int), INT_ALIGN, nu_uint, np_uint},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000813 {'l', sizeof(long), LONG_ALIGN, nu_long, np_long},
Guido van Rossum60c50611996-12-31 16:29:52 +0000814 {'L', sizeof(long), LONG_ALIGN, nu_ulong, np_ulong},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000815 {'f', sizeof(float), FLOAT_ALIGN, nu_float, np_float},
816 {'d', sizeof(double), DOUBLE_ALIGN, nu_double, np_double},
Guido van Rossum78694d91998-09-18 14:14:13 +0000817 {'P', sizeof(void *), VOID_P_ALIGN, nu_void_p, np_void_p},
Tim Peters7b9542a2001-06-10 23:40:19 +0000818#ifdef HAVE_LONG_LONG
819 {'q', sizeof(LONG_LONG), LONG_LONG_ALIGN, nu_longlong, np_longlong},
820 {'Q', sizeof(LONG_LONG), LONG_LONG_ALIGN, nu_ulonglong,np_ulonglong},
821#else
822 /* n[pu]_qQerror just raise errors, but give them "the expected" size
823 and alignment anyway so that calcsize returns something reasonable,
824 and so unpack code that works on a 'long long' platform ends up in
825 the error routine instead of with a mysterious "unpack str size
826 does not match format" msg when run on a non-'long long' box. */
827 {'q', 8, 8, nu_qQerror, np_qQerror},
828 {'Q', 8, 8, nu_qQerror, np_qQerror},
829#endif
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000830 {0}
831};
832
833static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000834bu_int(const char *p, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000835{
836 long x = 0;
837 int i = f->size;
838 do {
839 x = (x<<8) | (*p++ & 0xFF);
840 } while (--i > 0);
Tim Petersf0e717b2001-04-08 23:39:38 +0000841 /* Extend the sign bit. */
842 if (SIZEOF_LONG > f->size)
843 x |= -(x & (1L << (8*f->size - 1)));
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000844 return PyInt_FromLong(x);
845}
846
847static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000848bu_uint(const char *p, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000849{
Guido van Rossum6c87eca1997-01-03 19:08:16 +0000850 unsigned long x = 0;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000851 int i = f->size;
852 do {
853 x = (x<<8) | (*p++ & 0xFF);
854 } while (--i > 0);
Guido van Rossum39ef2271998-06-29 04:00:40 +0000855 if (f->size >= 4)
856 return PyLong_FromUnsignedLong(x);
857 else
858 return PyInt_FromLong((long)x);
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000859}
860
Guido van Rossum74679b41997-01-02 22:21:36 +0000861static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000862bu_float(const char *p, const formatdef *f)
Guido van Rossum74679b41997-01-02 22:21:36 +0000863{
864 return unpack_float(p, 1);
865}
866
867static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000868bu_double(const char *p, const formatdef *f)
Guido van Rossum74679b41997-01-02 22:21:36 +0000869{
870 return unpack_double(p, 1);
871}
872
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000873static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000874bp_int(char *p, PyObject *v, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000875{
876 long x;
877 int i;
878 if (get_long(v, &x) < 0)
879 return -1;
880 i = f->size;
881 do {
Guido van Rossum7844e381997-04-11 20:44:04 +0000882 p[--i] = (char)x;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000883 x >>= 8;
884 } while (i > 0);
885 return 0;
886}
887
Guido van Rossum60c50611996-12-31 16:29:52 +0000888static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000889bp_uint(char *p, PyObject *v, const formatdef *f)
Guido van Rossum60c50611996-12-31 16:29:52 +0000890{
891 unsigned long x;
892 int i;
893 if (get_ulong(v, &x) < 0)
894 return -1;
895 i = f->size;
896 do {
Guido van Rossum7844e381997-04-11 20:44:04 +0000897 p[--i] = (char)x;
Guido van Rossum60c50611996-12-31 16:29:52 +0000898 x >>= 8;
899 } while (i > 0);
900 return 0;
901}
902
Guido van Rossum74679b41997-01-02 22:21:36 +0000903static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000904bp_float(char *p, PyObject *v, const formatdef *f)
Guido van Rossum74679b41997-01-02 22:21:36 +0000905{
906 double x = PyFloat_AsDouble(v);
907 if (x == -1 && PyErr_Occurred()) {
908 PyErr_SetString(StructError,
909 "required argument is not a float");
910 return -1;
911 }
912 return pack_float(x, p, 1);
913}
914
915static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000916bp_double(char *p, PyObject *v, const formatdef *f)
Guido van Rossum74679b41997-01-02 22:21:36 +0000917{
918 double x = PyFloat_AsDouble(v);
919 if (x == -1 && PyErr_Occurred()) {
920 PyErr_SetString(StructError,
921 "required argument is not a float");
922 return -1;
923 }
924 return pack_double(x, p, 1);
925}
926
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000927static formatdef bigendian_table[] = {
928 {'x', 1, 0, NULL},
929 {'b', 1, 0, bu_int, bp_int},
930 {'B', 1, 0, bu_uint, bp_int},
931 {'c', 1, 0, nu_char, np_char},
932 {'s', 1, 0, NULL},
Guido van Rossum9eb671f1997-09-05 07:08:39 +0000933 {'p', 1, 0, NULL},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000934 {'h', 2, 0, bu_int, bp_int},
Guido van Rossum60c50611996-12-31 16:29:52 +0000935 {'H', 2, 0, bu_uint, bp_uint},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000936 {'i', 4, 0, bu_int, bp_int},
Guido van Rossum60c50611996-12-31 16:29:52 +0000937 {'I', 4, 0, bu_uint, bp_uint},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000938 {'l', 4, 0, bu_int, bp_int},
Guido van Rossum60c50611996-12-31 16:29:52 +0000939 {'L', 4, 0, bu_uint, bp_uint},
Guido van Rossum74679b41997-01-02 22:21:36 +0000940 {'f', 4, 0, bu_float, bp_float},
941 {'d', 8, 0, bu_double, bp_double},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000942 {0}
943};
944
945static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000946lu_int(const char *p, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000947{
948 long x = 0;
949 int i = f->size;
950 do {
951 x = (x<<8) | (p[--i] & 0xFF);
952 } while (i > 0);
Tim Petersf0e717b2001-04-08 23:39:38 +0000953 /* Extend the sign bit. */
954 if (SIZEOF_LONG > f->size)
955 x |= -(x & (1L << (8*f->size - 1)));
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000956 return PyInt_FromLong(x);
957}
958
959static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000960lu_uint(const char *p, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000961{
Guido van Rossum6c87eca1997-01-03 19:08:16 +0000962 unsigned long x = 0;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000963 int i = f->size;
964 do {
965 x = (x<<8) | (p[--i] & 0xFF);
966 } while (i > 0);
Guido van Rossum39ef2271998-06-29 04:00:40 +0000967 if (f->size >= 4)
968 return PyLong_FromUnsignedLong(x);
969 else
970 return PyInt_FromLong((long)x);
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000971}
972
Guido van Rossum74679b41997-01-02 22:21:36 +0000973static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000974lu_float(const char *p, const formatdef *f)
Guido van Rossum74679b41997-01-02 22:21:36 +0000975{
976 return unpack_float(p+3, -1);
977}
978
979static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000980lu_double(const char *p, const formatdef *f)
Guido van Rossum74679b41997-01-02 22:21:36 +0000981{
982 return unpack_double(p+7, -1);
983}
984
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000985static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000986lp_int(char *p, PyObject *v, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000987{
988 long x;
989 int i;
990 if (get_long(v, &x) < 0)
991 return -1;
992 i = f->size;
993 do {
Guido van Rossum7844e381997-04-11 20:44:04 +0000994 *p++ = (char)x;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000995 x >>= 8;
996 } while (--i > 0);
997 return 0;
998}
999
Guido van Rossum60c50611996-12-31 16:29:52 +00001000static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +00001001lp_uint(char *p, PyObject *v, const formatdef *f)
Guido van Rossum60c50611996-12-31 16:29:52 +00001002{
1003 unsigned long x;
1004 int i;
1005 if (get_ulong(v, &x) < 0)
1006 return -1;
1007 i = f->size;
1008 do {
Guido van Rossum7844e381997-04-11 20:44:04 +00001009 *p++ = (char)x;
Guido van Rossum60c50611996-12-31 16:29:52 +00001010 x >>= 8;
1011 } while (--i > 0);
1012 return 0;
1013}
1014
Guido van Rossum74679b41997-01-02 22:21:36 +00001015static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +00001016lp_float(char *p, PyObject *v, const formatdef *f)
Guido van Rossum74679b41997-01-02 22:21:36 +00001017{
1018 double x = PyFloat_AsDouble(v);
1019 if (x == -1 && PyErr_Occurred()) {
1020 PyErr_SetString(StructError,
1021 "required argument is not a float");
1022 return -1;
1023 }
1024 return pack_float(x, p+3, -1);
1025}
1026
1027static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +00001028lp_double(char *p, PyObject *v, const formatdef *f)
Guido van Rossum74679b41997-01-02 22:21:36 +00001029{
1030 double x = PyFloat_AsDouble(v);
1031 if (x == -1 && PyErr_Occurred()) {
1032 PyErr_SetString(StructError,
1033 "required argument is not a float");
1034 return -1;
1035 }
1036 return pack_double(x, p+7, -1);
1037}
1038
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001039static formatdef lilendian_table[] = {
1040 {'x', 1, 0, NULL},
1041 {'b', 1, 0, lu_int, lp_int},
1042 {'B', 1, 0, lu_uint, lp_int},
1043 {'c', 1, 0, nu_char, np_char},
1044 {'s', 1, 0, NULL},
Guido van Rossum9eb671f1997-09-05 07:08:39 +00001045 {'p', 1, 0, NULL},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001046 {'h', 2, 0, lu_int, lp_int},
Guido van Rossum60c50611996-12-31 16:29:52 +00001047 {'H', 2, 0, lu_uint, lp_uint},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001048 {'i', 4, 0, lu_int, lp_int},
Guido van Rossum60c50611996-12-31 16:29:52 +00001049 {'I', 4, 0, lu_uint, lp_uint},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001050 {'l', 4, 0, lu_int, lp_int},
Guido van Rossum60c50611996-12-31 16:29:52 +00001051 {'L', 4, 0, lu_uint, lp_uint},
Guido van Rossum74679b41997-01-02 22:21:36 +00001052 {'f', 4, 0, lu_float, lp_float},
1053 {'d', 8, 0, lu_double, lp_double},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001054 {0}
1055};
1056
1057
1058static const formatdef *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +00001059whichtable(char **pfmt)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001060{
1061 const char *fmt = (*pfmt)++; /* May be backed out of later */
1062 switch (*fmt) {
1063 case '<':
1064 return lilendian_table;
1065 case '>':
1066 case '!': /* Network byte order is big-endian */
1067 return bigendian_table;
1068 case '=': { /* Host byte order -- different from native in aligment! */
1069 int n = 1;
1070 char *p = (char *) &n;
1071 if (*p == 1)
1072 return lilendian_table;
1073 else
1074 return bigendian_table;
1075 }
1076 default:
1077 --*pfmt; /* Back out of pointer increment */
1078 /* Fall through */
1079 case '@':
1080 return native_table;
1081 }
1082}
1083
1084
1085/* Get the table entry for a format code */
1086
1087static const formatdef *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +00001088getentry(int c, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001089{
1090 for (; f->format != '\0'; f++) {
1091 if (f->format == c) {
1092 return f;
1093 }
1094 }
1095 PyErr_SetString(StructError, "bad char in struct format");
1096 return NULL;
1097}
1098
1099
Guido van Rossum02975121992-08-17 08:55:12 +00001100/* Align a size according to a format code */
1101
1102static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +00001103align(int size, int c, const formatdef *e)
Guido van Rossum02975121992-08-17 08:55:12 +00001104{
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001105 if (e->format == c) {
1106 if (e->alignment) {
1107 size = ((size + e->alignment - 1)
1108 / e->alignment)
1109 * e->alignment;
1110 }
Guido van Rossum02975121992-08-17 08:55:12 +00001111 }
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001112 return size;
Guido van Rossum02975121992-08-17 08:55:12 +00001113}
1114
1115
1116/* calculate the size of a format string */
1117
1118static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +00001119calcsize(const char *fmt, const formatdef *f)
Guido van Rossum02975121992-08-17 08:55:12 +00001120{
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001121 const formatdef *e;
1122 const char *s;
Guido van Rossum02975121992-08-17 08:55:12 +00001123 char c;
1124 int size, num, itemsize, x;
1125
1126 s = fmt;
1127 size = 0;
1128 while ((c = *s++) != '\0') {
Guido van Rossum730806d1998-04-10 22:27:42 +00001129 if (isspace((int)c))
Guido van Rossume20aef51997-08-26 20:39:54 +00001130 continue;
Guido van Rossum02975121992-08-17 08:55:12 +00001131 if ('0' <= c && c <= '9') {
1132 num = c - '0';
1133 while ('0' <= (c = *s++) && c <= '9') {
1134 x = num*10 + (c - '0');
1135 if (x/10 != num) {
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001136 PyErr_SetString(
1137 StructError,
1138 "overflow in item count");
Guido van Rossum02975121992-08-17 08:55:12 +00001139 return -1;
1140 }
1141 num = x;
1142 }
1143 if (c == '\0')
1144 break;
1145 }
1146 else
1147 num = 1;
1148
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001149 e = getentry(c, f);
1150 if (e == NULL)
Guido van Rossum02975121992-08-17 08:55:12 +00001151 return -1;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001152 itemsize = e->size;
1153 size = align(size, c, e);
Guido van Rossum02975121992-08-17 08:55:12 +00001154 x = num * itemsize;
1155 size += x;
1156 if (x/itemsize != num || size < 0) {
Barry Warsaw30695fa1996-12-12 23:32:31 +00001157 PyErr_SetString(StructError,
1158 "total struct size too long");
Guido van Rossum02975121992-08-17 08:55:12 +00001159 return -1;
1160 }
Guido van Rossum02975121992-08-17 08:55:12 +00001161 }
1162
1163 return size;
1164}
1165
1166
Guido van Rossum414fd481997-12-19 04:24:24 +00001167static char calcsize__doc__[] = "\
1168calcsize(fmt) -> int\n\
1169Return size of C struct described by format string fmt.\n\
1170See struct.__doc__ for more on format strings.";
Guido van Rossum02975121992-08-17 08:55:12 +00001171
Barry Warsaw30695fa1996-12-12 23:32:31 +00001172static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +00001173struct_calcsize(PyObject *self, PyObject *args)
Guido van Rossum02975121992-08-17 08:55:12 +00001174{
1175 char *fmt;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001176 const formatdef *f;
Guido van Rossum02975121992-08-17 08:55:12 +00001177 int size;
1178
Guido van Rossum43713e52000-02-29 13:59:29 +00001179 if (!PyArg_ParseTuple(args, "s:calcsize", &fmt))
Guido van Rossum02975121992-08-17 08:55:12 +00001180 return NULL;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001181 f = whichtable(&fmt);
1182 size = calcsize(fmt, f);
Guido van Rossum02975121992-08-17 08:55:12 +00001183 if (size < 0)
1184 return NULL;
Barry Warsaw30695fa1996-12-12 23:32:31 +00001185 return PyInt_FromLong((long)size);
Guido van Rossum02975121992-08-17 08:55:12 +00001186}
1187
1188
Guido van Rossum414fd481997-12-19 04:24:24 +00001189static char pack__doc__[] = "\
1190pack(fmt, v1, v2, ...) -> string\n\
1191Return string containing values v1, v2, ... packed according to fmt.\n\
1192See struct.__doc__ for more on format strings.";
Guido van Rossum02975121992-08-17 08:55:12 +00001193
Barry Warsaw30695fa1996-12-12 23:32:31 +00001194static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +00001195struct_pack(PyObject *self, PyObject *args)
Guido van Rossum02975121992-08-17 08:55:12 +00001196{
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001197 const formatdef *f, *e;
Barry Warsaw30695fa1996-12-12 23:32:31 +00001198 PyObject *format, *result, *v;
Guido van Rossum02975121992-08-17 08:55:12 +00001199 char *fmt;
1200 int size, num;
1201 int i, n;
Guido van Rossumb9d338c1997-01-03 15:40:33 +00001202 char *s, *res, *restart, *nres;
Guido van Rossum02975121992-08-17 08:55:12 +00001203 char c;
Guido van Rossum02975121992-08-17 08:55:12 +00001204
Barry Warsaw30695fa1996-12-12 23:32:31 +00001205 if (args == NULL || !PyTuple_Check(args) ||
1206 (n = PyTuple_Size(args)) < 1)
1207 {
Fred Drake137507e2000-06-01 02:02:46 +00001208 PyErr_SetString(PyExc_TypeError,
1209 "struct.pack requires at least one argument");
Guido van Rossum02975121992-08-17 08:55:12 +00001210 return NULL;
1211 }
Barry Warsaw30695fa1996-12-12 23:32:31 +00001212 format = PyTuple_GetItem(args, 0);
1213 if (!PyArg_Parse(format, "s", &fmt))
Guido van Rossum02975121992-08-17 08:55:12 +00001214 return NULL;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001215 f = whichtable(&fmt);
1216 size = calcsize(fmt, f);
Guido van Rossum02975121992-08-17 08:55:12 +00001217 if (size < 0)
1218 return NULL;
Barry Warsaw30695fa1996-12-12 23:32:31 +00001219 result = PyString_FromStringAndSize((char *)NULL, size);
Guido van Rossum02975121992-08-17 08:55:12 +00001220 if (result == NULL)
1221 return NULL;
1222
1223 s = fmt;
1224 i = 1;
Barry Warsaw30695fa1996-12-12 23:32:31 +00001225 res = restart = PyString_AsString(result);
Guido van Rossum02975121992-08-17 08:55:12 +00001226
1227 while ((c = *s++) != '\0') {
Guido van Rossum730806d1998-04-10 22:27:42 +00001228 if (isspace((int)c))
Guido van Rossume20aef51997-08-26 20:39:54 +00001229 continue;
Guido van Rossum02975121992-08-17 08:55:12 +00001230 if ('0' <= c && c <= '9') {
1231 num = c - '0';
1232 while ('0' <= (c = *s++) && c <= '9')
1233 num = num*10 + (c - '0');
1234 if (c == '\0')
1235 break;
1236 }
1237 else
1238 num = 1;
1239
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001240 e = getentry(c, f);
1241 if (e == NULL)
1242 goto fail;
Guido van Rossumb9d338c1997-01-03 15:40:33 +00001243 nres = restart + align((int)(res-restart), c, e);
1244 /* Fill padd bytes with zeros */
1245 while (res < nres)
1246 *res++ = '\0';
Guido van Rossum3aa27fd1996-12-31 02:10:45 +00001247 if (num == 0 && c != 's')
1248 continue;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001249 do {
1250 if (c == 'x') {
1251 /* doesn't consume arguments */
Guido van Rossum3aa27fd1996-12-31 02:10:45 +00001252 memset(res, '\0', num);
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001253 res += num;
Guido van Rossum02975121992-08-17 08:55:12 +00001254 break;
Guido van Rossum02975121992-08-17 08:55:12 +00001255 }
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001256 if (i >= n) {
1257 PyErr_SetString(StructError,
1258 "insufficient arguments to pack");
1259 goto fail;
1260 }
1261 v = PyTuple_GetItem(args, i++);
1262 if (v == NULL)
1263 goto fail;
1264 if (c == 's') {
1265 /* num is string size, not repeat count */
1266 int n;
1267 if (!PyString_Check(v)) {
1268 PyErr_SetString(StructError,
1269 "argument for 's' must be a string");
1270 goto fail;
1271 }
1272 n = PyString_Size(v);
1273 if (n > num)
1274 n = num;
1275 if (n > 0)
1276 memcpy(res, PyString_AsString(v), n);
1277 if (n < num)
Guido van Rossum3aa27fd1996-12-31 02:10:45 +00001278 memset(res+n, '\0', num-n);
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001279 res += num;
1280 break;
1281 }
Guido van Rossum9eb671f1997-09-05 07:08:39 +00001282 else if (c == 'p') {
1283 /* num is string size + 1,
1284 to fit in the count byte */
1285 int n;
1286 num--; /* now num is max string size */
1287 if (!PyString_Check(v)) {
1288 PyErr_SetString(StructError,
1289 "argument for 'p' must be a string");
1290 goto fail;
1291 }
1292 n = PyString_Size(v);
1293 if (n > num)
1294 n = num;
1295 if (n > 0)
1296 memcpy(res+1, PyString_AsString(v), n);
1297 if (n < num)
1298 /* no real need, just to be nice */
1299 memset(res+1+n, '\0', num-n);
1300 *res++ = n; /* store the length byte */
1301 res += num;
1302 break;
1303 }
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001304 else {
1305 if (e->pack(res, v, e) < 0)
1306 goto fail;
1307 res += e->size;
1308 }
1309 } while (--num > 0);
Guido van Rossum02975121992-08-17 08:55:12 +00001310 }
1311
1312 if (i < n) {
Barry Warsaw30695fa1996-12-12 23:32:31 +00001313 PyErr_SetString(StructError,
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001314 "too many arguments for pack format");
Guido van Rossum02975121992-08-17 08:55:12 +00001315 goto fail;
1316 }
1317
1318 return result;
1319
1320 fail:
Barry Warsaw30695fa1996-12-12 23:32:31 +00001321 Py_DECREF(result);
Guido van Rossum02975121992-08-17 08:55:12 +00001322 return NULL;
1323}
1324
1325
Guido van Rossum9897f0f1997-12-21 06:46:20 +00001326static char unpack__doc__[] = "\
1327unpack(fmt, string) -> (v1, v2, ...)\n\
1328Unpack the string, containing packed C structure data, according\n\
1329to fmt. Requires len(string)==calcsize(fmt).\n\
Guido van Rossum414fd481997-12-19 04:24:24 +00001330See struct.__doc__ for more on format strings.";
Guido van Rossum02975121992-08-17 08:55:12 +00001331
Barry Warsaw30695fa1996-12-12 23:32:31 +00001332static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +00001333struct_unpack(PyObject *self, PyObject *args)
Guido van Rossum02975121992-08-17 08:55:12 +00001334{
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001335 const formatdef *f, *e;
Guido van Rossum02975121992-08-17 08:55:12 +00001336 char *str, *start, *fmt, *s;
1337 char c;
Barry Warsawb9a781e1997-01-03 00:26:28 +00001338 int len, size, num;
Barry Warsaw30695fa1996-12-12 23:32:31 +00001339 PyObject *res, *v;
Guido van Rossum02975121992-08-17 08:55:12 +00001340
Guido van Rossum43713e52000-02-29 13:59:29 +00001341 if (!PyArg_ParseTuple(args, "ss#:unpack", &fmt, &start, &len))
Guido van Rossum02975121992-08-17 08:55:12 +00001342 return NULL;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001343 f = whichtable(&fmt);
1344 size = calcsize(fmt, f);
1345 if (size < 0)
1346 return NULL;
Guido van Rossum02975121992-08-17 08:55:12 +00001347 if (size != len) {
Barry Warsaw30695fa1996-12-12 23:32:31 +00001348 PyErr_SetString(StructError,
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001349 "unpack str size does not match format");
Guido van Rossum02975121992-08-17 08:55:12 +00001350 return NULL;
1351 }
Barry Warsaw30695fa1996-12-12 23:32:31 +00001352 res = PyList_New(0);
Guido van Rossum02975121992-08-17 08:55:12 +00001353 if (res == NULL)
1354 return NULL;
1355 str = start;
1356 s = fmt;
1357 while ((c = *s++) != '\0') {
Guido van Rossum730806d1998-04-10 22:27:42 +00001358 if (isspace((int)c))
Guido van Rossume20aef51997-08-26 20:39:54 +00001359 continue;
Guido van Rossum02975121992-08-17 08:55:12 +00001360 if ('0' <= c && c <= '9') {
1361 num = c - '0';
1362 while ('0' <= (c = *s++) && c <= '9')
1363 num = num*10 + (c - '0');
1364 if (c == '\0')
1365 break;
1366 }
1367 else
1368 num = 1;
1369
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001370 e = getentry(c, f);
1371 if (e == NULL)
1372 goto fail;
1373 str = start + align((int)(str-start), c, e);
Guido van Rossum3aa27fd1996-12-31 02:10:45 +00001374 if (num == 0 && c != 's')
1375 continue;
Guido van Rossum02975121992-08-17 08:55:12 +00001376
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001377 do {
1378 if (c == 'x') {
1379 str += num;
Guido van Rossum02975121992-08-17 08:55:12 +00001380 break;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001381 }
1382 if (c == 's') {
1383 /* num is string size, not repeat count */
1384 v = PyString_FromStringAndSize(str, num);
1385 if (v == NULL)
1386 goto fail;
1387 str += num;
1388 num = 0;
1389 }
Guido van Rossum9eb671f1997-09-05 07:08:39 +00001390 else if (c == 'p') {
1391 /* num is string buffer size,
1392 not repeat count */
1393 int n = *(unsigned char*)str;
1394 /* first byte (unsigned) is string size */
1395 if (n >= num)
1396 n = num-1;
1397 v = PyString_FromStringAndSize(str+1, n);
1398 if (v == NULL)
1399 goto fail;
1400 str += num;
1401 num = 0;
1402 }
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001403 else {
1404 v = e->unpack(str, e);
1405 if (v == NULL)
1406 goto fail;
1407 str += e->size;
Guido van Rossum02975121992-08-17 08:55:12 +00001408 }
Barry Warsaw30695fa1996-12-12 23:32:31 +00001409 if (v == NULL || PyList_Append(res, v) < 0)
Guido van Rossum02975121992-08-17 08:55:12 +00001410 goto fail;
Barry Warsaw30695fa1996-12-12 23:32:31 +00001411 Py_DECREF(v);
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001412 } while (--num > 0);
Guido van Rossum02975121992-08-17 08:55:12 +00001413 }
1414
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001415 v = PyList_AsTuple(res);
1416 Py_DECREF(res);
1417 return v;
Guido van Rossum02975121992-08-17 08:55:12 +00001418
1419 fail:
Barry Warsaw30695fa1996-12-12 23:32:31 +00001420 Py_DECREF(res);
Guido van Rossum02975121992-08-17 08:55:12 +00001421 return NULL;
1422}
1423
Guido van Rossum90ddb7b1992-08-19 16:44:15 +00001424
Guido van Rossum02975121992-08-17 08:55:12 +00001425/* List of functions */
1426
Barry Warsaw30695fa1996-12-12 23:32:31 +00001427static PyMethodDef struct_methods[] = {
Guido van Rossum414fd481997-12-19 04:24:24 +00001428 {"calcsize", struct_calcsize, METH_VARARGS, calcsize__doc__},
1429 {"pack", struct_pack, METH_VARARGS, pack__doc__},
1430 {"unpack", struct_unpack, METH_VARARGS, unpack__doc__},
Guido van Rossum02975121992-08-17 08:55:12 +00001431 {NULL, NULL} /* sentinel */
1432};
1433
1434
1435/* Module initialization */
1436
Guido van Rossum3886bb61998-12-04 18:50:17 +00001437DL_EXPORT(void)
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001438initstruct(void)
Guido van Rossum02975121992-08-17 08:55:12 +00001439{
Barry Warsaw30695fa1996-12-12 23:32:31 +00001440 PyObject *m, *d;
Guido van Rossum02975121992-08-17 08:55:12 +00001441
1442 /* Create the module and add the functions */
Guido van Rossum414fd481997-12-19 04:24:24 +00001443 m = Py_InitModule4("struct", struct_methods, struct__doc__,
1444 (PyObject*)NULL, PYTHON_API_VERSION);
Guido van Rossum02975121992-08-17 08:55:12 +00001445
1446 /* Add some symbolic constants to the module */
Barry Warsaw30695fa1996-12-12 23:32:31 +00001447 d = PyModule_GetDict(m);
Guido van Rossum0cb96de1997-10-01 04:29:29 +00001448 StructError = PyErr_NewException("struct.error", NULL, NULL);
1449 if (StructError == NULL)
1450 return;
Barry Warsaw30695fa1996-12-12 23:32:31 +00001451 PyDict_SetItemString(d, "error", StructError);
Guido van Rossum02975121992-08-17 08:55:12 +00001452}