blob: 5b52a396db99b13c58a246c7a8391ce74cbf845b [file] [log] [blame]
Guido van Rossum02975121992-08-17 08:55:12 +00001/***********************************************************
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00002Copyright (c) 2000, BeOpen.com.
3Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
5All rights reserved.
Guido van Rossum02975121992-08-17 08:55:12 +00006
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00007See the file "Misc/COPYRIGHT" for information on usage and
8redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Guido van Rossum02975121992-08-17 08:55:12 +00009******************************************************************/
10
11/* struct module -- pack values into and (out of) strings */
12
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +000013/* New version supporting byte order, alignment and size options,
14 character strings, and unsigned numbers */
15
Guido van Rossum414fd481997-12-19 04:24:24 +000016static char struct__doc__[] = "\
17Functions to convert between Python values and C structs.\n\
18Python strings are used to hold the data representing the C struct\n\
19and also as format strings to describe the layout of data in the C struct.\n\
20\n\
21The optional first format char indicates byte ordering and alignment:\n\
22 @: native w/native alignment(default)\n\
23 =: native w/standard alignment\n\
24 <: little-endian, std. alignment\n\
25 >: big-endian, std. alignment\n\
26 !: network, std (same as >)\n\
27\n\
28The remaining chars indicate types of args and must match exactly;\n\
29these can be preceded by a decimal repeat count:\n\
30 x: pad byte (no data); c:char; b:signed byte; B:unsigned byte;\n\
31 h:short; H:unsigned short; i:int; I:unsigned int;\n\
32 l:long; L:unsigned long; f:float; d:double.\n\
33Special cases (preceding decimal count indicates length):\n\
34 s:string (array of char); p: pascal string (w. count byte).\n\
Guido van Rossum78694d91998-09-18 14:14:13 +000035Special case (only available in native format):\n\
36 P:an integer type that is wide enough to hold a pointer.\n\
Guido van Rossum414fd481997-12-19 04:24:24 +000037Whitespace between formats is ignored.\n\
38\n\
39The variable struct.error is an exception raised on errors.";
40
Barry Warsaw30695fa1996-12-12 23:32:31 +000041#include "Python.h"
Guido van Rossum74679b41997-01-02 22:21:36 +000042#include "mymath.h"
Guido van Rossum02975121992-08-17 08:55:12 +000043
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +000044#include <limits.h>
Guido van Rossume20aef51997-08-26 20:39:54 +000045#include <ctype.h>
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +000046
47
48/* Exception */
49
Barry Warsaw30695fa1996-12-12 23:32:31 +000050static PyObject *StructError;
Guido van Rossum02975121992-08-17 08:55:12 +000051
52
53/* Define various structs to figure out the alignments of types */
54
Jack Jansen971e1df1995-02-02 14:29:10 +000055#ifdef __MWERKS__
56/*
57** XXXX We have a problem here. There are no unique alignment rules
58** on the PowerPC mac.
59*/
60#ifdef __powerc
61#pragma options align=mac68k
62#endif
63#endif /* __MWERKS__ */
64
Guido van Rossum02975121992-08-17 08:55:12 +000065typedef struct { char c; short x; } s_short;
66typedef struct { char c; int x; } s_int;
67typedef struct { char c; long x; } s_long;
68typedef struct { char c; float x; } s_float;
69typedef struct { char c; double x; } s_double;
Guido van Rossum78694d91998-09-18 14:14:13 +000070typedef struct { char c; void *x; } s_void_p;
Guido van Rossum02975121992-08-17 08:55:12 +000071
72#define SHORT_ALIGN (sizeof(s_short) - sizeof(short))
73#define INT_ALIGN (sizeof(s_int) - sizeof(int))
74#define LONG_ALIGN (sizeof(s_long) - sizeof(long))
75#define FLOAT_ALIGN (sizeof(s_float) - sizeof(float))
76#define DOUBLE_ALIGN (sizeof(s_double) - sizeof(double))
Guido van Rossum78694d91998-09-18 14:14:13 +000077#define VOID_P_ALIGN (sizeof(s_void_p) - sizeof(void *))
Guido van Rossum02975121992-08-17 08:55:12 +000078
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
87get_long(v, p)
88 PyObject *v;
89 long *p;
90{
91 long x = PyInt_AsLong(v);
92 if (x == -1 && PyErr_Occurred()) {
Fred Draked3dbb381998-05-28 04:35:49 +000093 if (PyErr_ExceptionMatches(PyExc_TypeError))
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +000094 PyErr_SetString(StructError,
95 "required argument is not an integer");
96 return -1;
97 }
98 *p = x;
99 return 0;
100}
101
102
Guido van Rossum60c50611996-12-31 16:29:52 +0000103/* Same, but handling unsigned long */
104
105static int
106get_ulong(v, p)
107 PyObject *v;
108 unsigned long *p;
109{
Guido van Rossum6c87eca1997-01-03 19:08:16 +0000110 if (PyLong_Check(v)) {
111 unsigned long x = PyLong_AsUnsignedLong(v);
112 if (x == (unsigned long)(-1) && PyErr_Occurred())
Guido van Rossum60c50611996-12-31 16:29:52 +0000113 return -1;
Guido van Rossum6c87eca1997-01-03 19:08:16 +0000114 *p = x;
115 return 0;
Guido van Rossum60c50611996-12-31 16:29:52 +0000116 }
Guido van Rossum6c87eca1997-01-03 19:08:16 +0000117 else {
118 return get_long(v, (long *)p);
119 }
Guido van Rossum60c50611996-12-31 16:29:52 +0000120}
121
122
Guido van Rossum74679b41997-01-02 22:21:36 +0000123/* Floating point helpers */
124
125/* These use ANSI/IEEE Standard 754-1985 (Standard for Binary Floating
126 Point Arithmetic). See the following URL:
127 http://www.psc.edu/general/software/packages/ieee/ieee.html */
128
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000129/* XXX Inf/NaN are not handled quite right (but underflow is!) */
Guido van Rossum74679b41997-01-02 22:21:36 +0000130
131static int
132pack_float(x, p, incr)
133 double x; /* The number to pack */
134 char *p; /* Where to pack the high order byte */
135 int incr; /* 1 for big-endian; -1 for little-endian */
136{
137 int s;
138 int e;
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000139 double f;
140 long fbits;
Guido van Rossum74679b41997-01-02 22:21:36 +0000141
142 if (x < 0) {
143 s = 1;
144 x = -x;
145 }
146 else
147 s = 0;
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000148
149 f = frexp(x, &e);
150
151 /* Normalize f to be in the range [1.0, 2.0) */
152 if (0.5 <= f && f < 1.0) {
153 f *= 2.0;
Guido van Rossum74679b41997-01-02 22:21:36 +0000154 e--;
155 }
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000156 else if (f == 0.0) {
Guido van Rossum74679b41997-01-02 22:21:36 +0000157 e = 0;
158 }
159 else {
160 PyErr_SetString(PyExc_SystemError,
161 "frexp() result out of range");
162 return -1;
163 }
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000164
165 if (e >= 128) {
166 /* XXX 128 itself is reserved for Inf/NaN */
Guido van Rossum74679b41997-01-02 22:21:36 +0000167 PyErr_SetString(PyExc_OverflowError,
168 "float too large to pack with f format");
169 return -1;
170 }
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000171 else if (e < -126) {
172 /* Gradual underflow */
173 f = ldexp(f, 126 + e);
Guido van Rossum74679b41997-01-02 22:21:36 +0000174 e = 0;
175 }
Guido van Rossum8f3c8121997-11-04 17:12:33 +0000176 else if (!(e == 0 && f == 0.0)) {
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000177 e += 127;
178 f -= 1.0; /* Get rid of leading 1 */
Guido van Rossum74679b41997-01-02 22:21:36 +0000179 }
180
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000181 f *= 8388608.0; /* 2**23 */
182 fbits = (long) floor(f + 0.5); /* Round */
183
Guido van Rossum74679b41997-01-02 22:21:36 +0000184 /* First byte */
185 *p = (s<<7) | (e>>1);
186 p += incr;
187
188 /* Second byte */
Guido van Rossum7844e381997-04-11 20:44:04 +0000189 *p = (char) (((e&1)<<7) | (fbits>>16));
Guido van Rossum74679b41997-01-02 22:21:36 +0000190 p += incr;
191
192 /* Third byte */
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000193 *p = (fbits>>8) & 0xFF;
Guido van Rossum74679b41997-01-02 22:21:36 +0000194 p += incr;
195
196 /* Fourth byte */
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000197 *p = fbits&0xFF;
Guido van Rossum74679b41997-01-02 22:21:36 +0000198
199 /* Done */
200 return 0;
201}
202
203static int
204pack_double(x, p, incr)
205 double x; /* The number to pack */
206 char *p; /* Where to pack the high order byte */
207 int incr; /* 1 for big-endian; -1 for little-endian */
208{
209 int s;
210 int e;
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000211 double f;
Guido van Rossum74679b41997-01-02 22:21:36 +0000212 long fhi, flo;
213
214 if (x < 0) {
215 s = 1;
216 x = -x;
217 }
218 else
219 s = 0;
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000220
221 f = frexp(x, &e);
222
223 /* Normalize f to be in the range [1.0, 2.0) */
224 if (0.5 <= f && f < 1.0) {
225 f *= 2.0;
Guido van Rossum74679b41997-01-02 22:21:36 +0000226 e--;
227 }
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000228 else if (f == 0.0) {
Guido van Rossum74679b41997-01-02 22:21:36 +0000229 e = 0;
230 }
231 else {
232 PyErr_SetString(PyExc_SystemError,
233 "frexp() result out of range");
234 return -1;
235 }
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000236
237 if (e >= 1024) {
238 /* XXX 1024 itself is reserved for Inf/NaN */
Guido van Rossum74679b41997-01-02 22:21:36 +0000239 PyErr_SetString(PyExc_OverflowError,
240 "float too large to pack with d format");
241 return -1;
242 }
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000243 else if (e < -1022) {
244 /* Gradual underflow */
245 f = ldexp(f, 1022 + e);
Guido van Rossum74679b41997-01-02 22:21:36 +0000246 e = 0;
247 }
Guido van Rossum8f3c8121997-11-04 17:12:33 +0000248 else if (!(e == 0 && f == 0.0)) {
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000249 e += 1023;
250 f -= 1.0; /* Get rid of leading 1 */
Guido van Rossum74679b41997-01-02 22:21:36 +0000251 }
252
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000253 /* fhi receives the high 28 bits; flo the low 24 bits (== 52 bits) */
254 f *= 268435456.0; /* 2**28 */
255 fhi = (long) floor(f); /* Truncate */
256 f -= (double)fhi;
257 f *= 16777216.0; /* 2**24 */
258 flo = (long) floor(f + 0.5); /* Round */
259
Guido van Rossum74679b41997-01-02 22:21:36 +0000260 /* First byte */
261 *p = (s<<7) | (e>>4);
262 p += incr;
263
264 /* Second byte */
Guido van Rossum7844e381997-04-11 20:44:04 +0000265 *p = (char) (((e&0xF)<<4) | (fhi>>24));
Guido van Rossum74679b41997-01-02 22:21:36 +0000266 p += incr;
267
268 /* Third byte */
269 *p = (fhi>>16) & 0xFF;
270 p += incr;
271
272 /* Fourth byte */
273 *p = (fhi>>8) & 0xFF;
274 p += incr;
275
276 /* Fifth byte */
277 *p = fhi & 0xFF;
278 p += incr;
279
280 /* Sixth byte */
281 *p = (flo>>16) & 0xFF;
282 p += incr;
283
284 /* Seventh byte */
285 *p = (flo>>8) & 0xFF;
286 p += incr;
287
288 /* Eighth byte */
289 *p = flo & 0xFF;
290 p += incr;
291
292 /* Done */
293 return 0;
294}
295
296static PyObject *
297unpack_float(p, incr)
298 char *p; /* Where the high order byte is */
299 int incr; /* 1 for big-endian; -1 for little-endian */
300{
301 int s;
302 int e;
303 long f;
304 double x;
305
306 /* First byte */
307 s = (*p>>7) & 1;
308 e = (*p & 0x7F) << 1;
309 p += incr;
310
311 /* Second byte */
312 e |= (*p>>7) & 1;
313 f = (*p & 0x7F) << 16;
314 p += incr;
315
316 /* Third byte */
317 f |= (*p & 0xFF) << 8;
318 p += incr;
319
320 /* Fourth byte */
321 f |= *p & 0xFF;
322
323 x = (double)f / 8388608.0;
324
325 /* XXX This sadly ignores Inf/NaN issues */
Guido van Rossum07ef6551997-01-02 22:31:07 +0000326 if (e == 0)
327 e = -126;
328 else {
329 x += 1.0;
330 e -= 127;
331 }
332 x = ldexp(x, e);
Guido van Rossum74679b41997-01-02 22:21:36 +0000333
334 if (s)
335 x = -x;
336
337 return PyFloat_FromDouble(x);
338}
339
340static PyObject *
341unpack_double(p, incr)
342 char *p; /* Where the high order byte is */
343 int incr; /* 1 for big-endian; -1 for little-endian */
344{
345 int s;
346 int e;
347 long fhi, flo;
348 double x;
349
350 /* First byte */
351 s = (*p>>7) & 1;
352 e = (*p & 0x7F) << 4;
353 p += incr;
354
355 /* Second byte */
356 e |= (*p>>4) & 0xF;
357 fhi = (*p & 0xF) << 24;
358 p += incr;
359
360 /* Third byte */
361 fhi |= (*p & 0xFF) << 16;
362 p += incr;
363
364 /* Fourth byte */
365 fhi |= (*p & 0xFF) << 8;
366 p += incr;
367
368 /* Fifth byte */
369 fhi |= *p & 0xFF;
370 p += incr;
371
372 /* Sixth byte */
373 flo = (*p & 0xFF) << 16;
374 p += incr;
375
376 /* Seventh byte */
377 flo |= (*p & 0xFF) << 8;
378 p += incr;
379
380 /* Eighth byte */
381 flo |= *p & 0xFF;
382 p += incr;
383
384 x = (double)fhi + (double)flo / 16777216.0; /* 2**24 */
385 x /= 268435456.0; /* 2**28 */
386
387 /* XXX This sadly ignores Inf/NaN */
Guido van Rossum07ef6551997-01-02 22:31:07 +0000388 if (e == 0)
389 e = -1022;
390 else {
391 x += 1.0;
392 e -= 1023;
393 }
394 x = ldexp(x, e);
Guido van Rossum74679b41997-01-02 22:21:36 +0000395
396 if (s)
397 x = -x;
398
399 return PyFloat_FromDouble(x);
400}
401
402
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000403/* The translation function for each format character is table driven */
404
405typedef struct _formatdef {
406 char format;
407 int size;
408 int alignment;
409 PyObject* (*unpack) Py_PROTO((const char *,
410 const struct _formatdef *));
411 int (*pack) Py_PROTO((char *,
412 PyObject *,
413 const struct _formatdef *));
414} formatdef;
415
416static PyObject *
417nu_char(p, f)
418 const char *p;
419 const formatdef *f;
420{
421 return PyString_FromStringAndSize(p, 1);
422}
423
424static PyObject *
425nu_byte(p, f)
426 const char *p;
427 const formatdef *f;
428{
429 return PyInt_FromLong((long) *(signed char *)p);
430}
431
432static PyObject *
433nu_ubyte(p, f)
434 const char *p;
435 const formatdef *f;
436{
437 return PyInt_FromLong((long) *(unsigned char *)p);
438}
439
440static PyObject *
441nu_short(p, f)
442 const char *p;
443 const formatdef *f;
444{
445 return PyInt_FromLong((long) *(short *)p);
446}
447
448static PyObject *
449nu_ushort(p, f)
450 const char *p;
451 const formatdef *f;
452{
453 return PyInt_FromLong((long) *(unsigned short *)p);
454}
455
456static PyObject *
457nu_int(p, f)
458 const char *p;
459 const formatdef *f;
460{
461 return PyInt_FromLong((long) *(int *)p);
462}
463
464static PyObject *
465nu_uint(p, f)
466 const char *p;
467 const formatdef *f;
468{
469 unsigned int x = *(unsigned int *)p;
Guido van Rossum6c87eca1997-01-03 19:08:16 +0000470 return PyLong_FromUnsignedLong((unsigned long)x);
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000471}
472
473static PyObject *
474nu_long(p, f)
475 const char *p;
476 const formatdef *f;
477{
478 return PyInt_FromLong(*(long *)p);
479}
480
481static PyObject *
482nu_ulong(p, f)
483 const char *p;
484 const formatdef *f;
485{
Guido van Rossum6c87eca1997-01-03 19:08:16 +0000486 return PyLong_FromUnsignedLong(*(unsigned long *)p);
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000487}
488
489static PyObject *
490nu_float(p, f)
491 const char *p;
492 const formatdef *f;
493{
494 float x;
495 memcpy((char *)&x, p, sizeof(float));
496 return PyFloat_FromDouble((double)x);
497}
498
499static PyObject *
500nu_double(p, f)
501 const char *p;
502 const formatdef *f;
503{
504 double x;
505 memcpy((char *)&x, p, sizeof(double));
506 return PyFloat_FromDouble(x);
507}
508
Guido van Rossum78694d91998-09-18 14:14:13 +0000509static PyObject *
510nu_void_p(p, f)
511 const char *p;
512 const formatdef *f;
513{
514 return PyLong_FromVoidPtr(*(void **)p);
515}
516
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000517static int
518np_byte(p, v, f)
519 char *p;
520 PyObject *v;
521 const formatdef *f;
522{
523 long x;
524 if (get_long(v, &x) < 0)
525 return -1;
Guido van Rossum7844e381997-04-11 20:44:04 +0000526 *p = (char)x;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000527 return 0;
528}
529
530static int
531np_char(p, v, f)
532 char *p;
533 PyObject *v;
534 const formatdef *f;
535{
536 if (!PyString_Check(v) || PyString_Size(v) != 1) {
537 PyErr_SetString(StructError,
538 "char format require string of length 1");
539 return -1;
540 }
541 *p = *PyString_AsString(v);
542 return 0;
543}
544
545static int
546np_short(p, v, f)
547 char *p;
548 PyObject *v;
549 const formatdef *f;
550{
551 long x;
552 if (get_long(v, &x) < 0)
553 return -1;
Guido van Rossum7844e381997-04-11 20:44:04 +0000554 * (short *)p = (short)x;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000555 return 0;
556}
557
558static int
559np_int(p, v, f)
560 char *p;
561 PyObject *v;
562 const formatdef *f;
563{
564 long x;
565 if (get_long(v, &x) < 0)
566 return -1;
567 * (int *)p = x;
568 return 0;
569}
570
571static int
Guido van Rossum60c50611996-12-31 16:29:52 +0000572np_uint(p, v, f)
573 char *p;
574 PyObject *v;
575 const formatdef *f;
576{
577 unsigned long x;
578 if (get_ulong(v, &x) < 0)
579 return -1;
580 * (unsigned int *)p = x;
581 return 0;
582}
583
584static int
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000585np_long(p, v, f)
586 char *p;
587 PyObject *v;
588 const formatdef *f;
589{
590 long x;
591 if (get_long(v, &x) < 0)
592 return -1;
593 * (long *)p = x;
594 return 0;
595}
596
597static int
Guido van Rossum60c50611996-12-31 16:29:52 +0000598np_ulong(p, v, f)
599 char *p;
600 PyObject *v;
601 const formatdef *f;
602{
603 unsigned long x;
604 if (get_ulong(v, &x) < 0)
605 return -1;
606 * (unsigned long *)p = x;
607 return 0;
608}
609
610static int
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000611np_float(p, v, f)
612 char *p;
613 PyObject *v;
614 const formatdef *f;
615{
616 float x = (float)PyFloat_AsDouble(v);
617 if (x == -1 && PyErr_Occurred()) {
618 PyErr_SetString(StructError,
619 "required argument is not a float");
620 return -1;
621 }
622 memcpy(p, (char *)&x, sizeof(float));
623 return 0;
624}
625
626static int
627np_double(p, v, f)
628 char *p;
629 PyObject *v;
630 const formatdef *f;
631{
632 double x = PyFloat_AsDouble(v);
633 if (x == -1 && PyErr_Occurred()) {
634 PyErr_SetString(StructError,
635 "required argument is not a float");
636 return -1;
637 }
638 memcpy(p, (char *)&x, sizeof(double));
639 return 0;
640}
641
Guido van Rossum78694d91998-09-18 14:14:13 +0000642static int
643np_void_p(p, v, f)
644 char *p;
645 PyObject *v;
646 const formatdef *f;
647{
648 void *x = PyLong_AsVoidPtr(v);
649 if (x == NULL && PyErr_Occurred()) {
650 /* ### hrm. PyLong_AsVoidPtr raises SystemError */
651 if (PyErr_ExceptionMatches(PyExc_TypeError))
652 PyErr_SetString(StructError,
653 "required argument is not an integer");
654 return -1;
655 }
656 *(void **)p = x;
657 return 0;
658}
659
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000660static formatdef native_table[] = {
661 {'x', sizeof(char), 0, NULL},
662 {'b', sizeof(char), 0, nu_byte, np_byte},
663 {'B', sizeof(char), 0, nu_ubyte, np_byte},
664 {'c', sizeof(char), 0, nu_char, np_char},
665 {'s', sizeof(char), 0, NULL},
Guido van Rossum9eb671f1997-09-05 07:08:39 +0000666 {'p', sizeof(char), 0, NULL},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000667 {'h', sizeof(short), SHORT_ALIGN, nu_short, np_short},
668 {'H', sizeof(short), SHORT_ALIGN, nu_ushort, np_short},
669 {'i', sizeof(int), INT_ALIGN, nu_int, np_int},
Guido van Rossum60c50611996-12-31 16:29:52 +0000670 {'I', sizeof(int), INT_ALIGN, nu_uint, np_uint},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000671 {'l', sizeof(long), LONG_ALIGN, nu_long, np_long},
Guido van Rossum60c50611996-12-31 16:29:52 +0000672 {'L', sizeof(long), LONG_ALIGN, nu_ulong, np_ulong},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000673 {'f', sizeof(float), FLOAT_ALIGN, nu_float, np_float},
674 {'d', sizeof(double), DOUBLE_ALIGN, nu_double, np_double},
Guido van Rossum78694d91998-09-18 14:14:13 +0000675 {'P', sizeof(void *), VOID_P_ALIGN, nu_void_p, np_void_p},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000676 {0}
677};
678
679static PyObject *
680bu_int(p, f)
681 const char *p;
682 const formatdef *f;
683{
684 long x = 0;
685 int i = f->size;
686 do {
687 x = (x<<8) | (*p++ & 0xFF);
688 } while (--i > 0);
689 i = 8*(sizeof(long) - f->size);
690 if (i) {
691 x <<= i;
692 x >>= i;
693 }
694 return PyInt_FromLong(x);
695}
696
697static PyObject *
698bu_uint(p, f)
699 const char *p;
700 const formatdef *f;
701{
Guido van Rossum6c87eca1997-01-03 19:08:16 +0000702 unsigned long x = 0;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000703 int i = f->size;
704 do {
705 x = (x<<8) | (*p++ & 0xFF);
706 } while (--i > 0);
Guido van Rossum39ef2271998-06-29 04:00:40 +0000707 if (f->size >= 4)
708 return PyLong_FromUnsignedLong(x);
709 else
710 return PyInt_FromLong((long)x);
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000711}
712
Guido van Rossum74679b41997-01-02 22:21:36 +0000713static PyObject *
714bu_float(p, f)
715 const char *p;
716 const formatdef *f;
717{
718 return unpack_float(p, 1);
719}
720
721static PyObject *
722bu_double(p, f)
723 const char *p;
724 const formatdef *f;
725{
726 return unpack_double(p, 1);
727}
728
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000729static int
730bp_int(p, v, f)
731 char *p;
732 PyObject *v;
733 const formatdef *f;
734{
735 long x;
736 int i;
737 if (get_long(v, &x) < 0)
738 return -1;
739 i = f->size;
740 do {
Guido van Rossum7844e381997-04-11 20:44:04 +0000741 p[--i] = (char)x;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000742 x >>= 8;
743 } while (i > 0);
744 return 0;
745}
746
Guido van Rossum60c50611996-12-31 16:29:52 +0000747static int
748bp_uint(p, v, f)
749 char *p;
750 PyObject *v;
751 const formatdef *f;
752{
753 unsigned long x;
754 int i;
755 if (get_ulong(v, &x) < 0)
756 return -1;
757 i = f->size;
758 do {
Guido van Rossum7844e381997-04-11 20:44:04 +0000759 p[--i] = (char)x;
Guido van Rossum60c50611996-12-31 16:29:52 +0000760 x >>= 8;
761 } while (i > 0);
762 return 0;
763}
764
Guido van Rossum74679b41997-01-02 22:21:36 +0000765static int
766bp_float(p, v, f)
767 char *p;
768 PyObject *v;
769 const formatdef *f;
770{
771 double x = PyFloat_AsDouble(v);
772 if (x == -1 && PyErr_Occurred()) {
773 PyErr_SetString(StructError,
774 "required argument is not a float");
775 return -1;
776 }
777 return pack_float(x, p, 1);
778}
779
780static int
781bp_double(p, v, f)
782 char *p;
783 PyObject *v;
784 const formatdef *f;
785{
786 double x = PyFloat_AsDouble(v);
787 if (x == -1 && PyErr_Occurred()) {
788 PyErr_SetString(StructError,
789 "required argument is not a float");
790 return -1;
791 }
792 return pack_double(x, p, 1);
793}
794
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000795static formatdef bigendian_table[] = {
796 {'x', 1, 0, NULL},
797 {'b', 1, 0, bu_int, bp_int},
798 {'B', 1, 0, bu_uint, bp_int},
799 {'c', 1, 0, nu_char, np_char},
800 {'s', 1, 0, NULL},
Guido van Rossum9eb671f1997-09-05 07:08:39 +0000801 {'p', 1, 0, NULL},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000802 {'h', 2, 0, bu_int, bp_int},
Guido van Rossum60c50611996-12-31 16:29:52 +0000803 {'H', 2, 0, bu_uint, bp_uint},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000804 {'i', 4, 0, bu_int, bp_int},
Guido van Rossum60c50611996-12-31 16:29:52 +0000805 {'I', 4, 0, bu_uint, bp_uint},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000806 {'l', 4, 0, bu_int, bp_int},
Guido van Rossum60c50611996-12-31 16:29:52 +0000807 {'L', 4, 0, bu_uint, bp_uint},
Guido van Rossum74679b41997-01-02 22:21:36 +0000808 {'f', 4, 0, bu_float, bp_float},
809 {'d', 8, 0, bu_double, bp_double},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000810 {0}
811};
812
813static PyObject *
814lu_int(p, f)
815 const char *p;
816 const formatdef *f;
817{
818 long x = 0;
819 int i = f->size;
820 do {
821 x = (x<<8) | (p[--i] & 0xFF);
822 } while (i > 0);
823 i = 8*(sizeof(long) - f->size);
824 if (i) {
825 x <<= i;
826 x >>= i;
827 }
828 return PyInt_FromLong(x);
829}
830
831static PyObject *
832lu_uint(p, f)
833 const char *p;
834 const formatdef *f;
835{
Guido van Rossum6c87eca1997-01-03 19:08:16 +0000836 unsigned long x = 0;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000837 int i = f->size;
838 do {
839 x = (x<<8) | (p[--i] & 0xFF);
840 } while (i > 0);
Guido van Rossum39ef2271998-06-29 04:00:40 +0000841 if (f->size >= 4)
842 return PyLong_FromUnsignedLong(x);
843 else
844 return PyInt_FromLong((long)x);
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000845}
846
Guido van Rossum74679b41997-01-02 22:21:36 +0000847static PyObject *
848lu_float(p, f)
849 const char *p;
850 const formatdef *f;
851{
852 return unpack_float(p+3, -1);
853}
854
855static PyObject *
856lu_double(p, f)
857 const char *p;
858 const formatdef *f;
859{
860 return unpack_double(p+7, -1);
861}
862
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000863static int
864lp_int(p, v, f)
865 char *p;
866 PyObject *v;
867 const formatdef *f;
868{
869 long x;
870 int i;
871 if (get_long(v, &x) < 0)
872 return -1;
873 i = f->size;
874 do {
Guido van Rossum7844e381997-04-11 20:44:04 +0000875 *p++ = (char)x;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000876 x >>= 8;
877 } while (--i > 0);
878 return 0;
879}
880
Guido van Rossum60c50611996-12-31 16:29:52 +0000881static int
882lp_uint(p, v, f)
883 char *p;
884 PyObject *v;
885 const formatdef *f;
886{
887 unsigned long x;
888 int i;
889 if (get_ulong(v, &x) < 0)
890 return -1;
891 i = f->size;
892 do {
Guido van Rossum7844e381997-04-11 20:44:04 +0000893 *p++ = (char)x;
Guido van Rossum60c50611996-12-31 16:29:52 +0000894 x >>= 8;
895 } while (--i > 0);
896 return 0;
897}
898
Guido van Rossum74679b41997-01-02 22:21:36 +0000899static int
900lp_float(p, v, f)
901 char *p;
902 PyObject *v;
903 const formatdef *f;
904{
905 double x = PyFloat_AsDouble(v);
906 if (x == -1 && PyErr_Occurred()) {
907 PyErr_SetString(StructError,
908 "required argument is not a float");
909 return -1;
910 }
911 return pack_float(x, p+3, -1);
912}
913
914static int
915lp_double(p, v, f)
916 char *p;
917 PyObject *v;
918 const formatdef *f;
919{
920 double x = PyFloat_AsDouble(v);
921 if (x == -1 && PyErr_Occurred()) {
922 PyErr_SetString(StructError,
923 "required argument is not a float");
924 return -1;
925 }
926 return pack_double(x, p+7, -1);
927}
928
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000929static formatdef lilendian_table[] = {
930 {'x', 1, 0, NULL},
931 {'b', 1, 0, lu_int, lp_int},
932 {'B', 1, 0, lu_uint, lp_int},
933 {'c', 1, 0, nu_char, np_char},
934 {'s', 1, 0, NULL},
Guido van Rossum9eb671f1997-09-05 07:08:39 +0000935 {'p', 1, 0, NULL},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000936 {'h', 2, 0, lu_int, lp_int},
Guido van Rossum60c50611996-12-31 16:29:52 +0000937 {'H', 2, 0, lu_uint, lp_uint},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000938 {'i', 4, 0, lu_int, lp_int},
Guido van Rossum60c50611996-12-31 16:29:52 +0000939 {'I', 4, 0, lu_uint, lp_uint},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000940 {'l', 4, 0, lu_int, lp_int},
Guido van Rossum60c50611996-12-31 16:29:52 +0000941 {'L', 4, 0, lu_uint, lp_uint},
Guido van Rossum74679b41997-01-02 22:21:36 +0000942 {'f', 4, 0, lu_float, lp_float},
943 {'d', 8, 0, lu_double, lp_double},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000944 {0}
945};
946
947
948static const formatdef *
949whichtable(pfmt)
950 const char **pfmt;
951{
952 const char *fmt = (*pfmt)++; /* May be backed out of later */
953 switch (*fmt) {
954 case '<':
955 return lilendian_table;
956 case '>':
957 case '!': /* Network byte order is big-endian */
958 return bigendian_table;
959 case '=': { /* Host byte order -- different from native in aligment! */
960 int n = 1;
961 char *p = (char *) &n;
962 if (*p == 1)
963 return lilendian_table;
964 else
965 return bigendian_table;
966 }
967 default:
968 --*pfmt; /* Back out of pointer increment */
969 /* Fall through */
970 case '@':
971 return native_table;
972 }
973}
974
975
976/* Get the table entry for a format code */
977
978static const formatdef *
979getentry(c, f)
980 int c;
981 const formatdef *f;
982{
983 for (; f->format != '\0'; f++) {
984 if (f->format == c) {
985 return f;
986 }
987 }
988 PyErr_SetString(StructError, "bad char in struct format");
989 return NULL;
990}
991
992
Guido van Rossum02975121992-08-17 08:55:12 +0000993/* Align a size according to a format code */
994
995static int
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000996align(size, c, e)
Guido van Rossum02975121992-08-17 08:55:12 +0000997 int size;
998 int c;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000999 const formatdef *e;
Guido van Rossum02975121992-08-17 08:55:12 +00001000{
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001001 if (e->format == c) {
1002 if (e->alignment) {
1003 size = ((size + e->alignment - 1)
1004 / e->alignment)
1005 * e->alignment;
1006 }
Guido van Rossum02975121992-08-17 08:55:12 +00001007 }
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001008 return size;
Guido van Rossum02975121992-08-17 08:55:12 +00001009}
1010
1011
1012/* calculate the size of a format string */
1013
1014static int
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001015calcsize(fmt, f)
1016 const char *fmt;
1017 const formatdef *f;
Guido van Rossum02975121992-08-17 08:55:12 +00001018{
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001019 const formatdef *e;
1020 const char *s;
Guido van Rossum02975121992-08-17 08:55:12 +00001021 char c;
1022 int size, num, itemsize, x;
1023
1024 s = fmt;
1025 size = 0;
1026 while ((c = *s++) != '\0') {
Guido van Rossum730806d1998-04-10 22:27:42 +00001027 if (isspace((int)c))
Guido van Rossume20aef51997-08-26 20:39:54 +00001028 continue;
Guido van Rossum02975121992-08-17 08:55:12 +00001029 if ('0' <= c && c <= '9') {
1030 num = c - '0';
1031 while ('0' <= (c = *s++) && c <= '9') {
1032 x = num*10 + (c - '0');
1033 if (x/10 != num) {
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001034 PyErr_SetString(
1035 StructError,
1036 "overflow in item count");
Guido van Rossum02975121992-08-17 08:55:12 +00001037 return -1;
1038 }
1039 num = x;
1040 }
1041 if (c == '\0')
1042 break;
1043 }
1044 else
1045 num = 1;
1046
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001047 e = getentry(c, f);
1048 if (e == NULL)
Guido van Rossum02975121992-08-17 08:55:12 +00001049 return -1;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001050 itemsize = e->size;
1051 size = align(size, c, e);
Guido van Rossum02975121992-08-17 08:55:12 +00001052 x = num * itemsize;
1053 size += x;
1054 if (x/itemsize != num || size < 0) {
Barry Warsaw30695fa1996-12-12 23:32:31 +00001055 PyErr_SetString(StructError,
1056 "total struct size too long");
Guido van Rossum02975121992-08-17 08:55:12 +00001057 return -1;
1058 }
Guido van Rossum02975121992-08-17 08:55:12 +00001059 }
1060
1061 return size;
1062}
1063
1064
Guido van Rossum414fd481997-12-19 04:24:24 +00001065static char calcsize__doc__[] = "\
1066calcsize(fmt) -> int\n\
1067Return size of C struct described by format string fmt.\n\
1068See struct.__doc__ for more on format strings.";
Guido van Rossum02975121992-08-17 08:55:12 +00001069
Barry Warsaw30695fa1996-12-12 23:32:31 +00001070static PyObject *
Guido van Rossum02975121992-08-17 08:55:12 +00001071struct_calcsize(self, args)
Barry Warsaw30695fa1996-12-12 23:32:31 +00001072 PyObject *self; /* Not used */
1073 PyObject *args;
Guido van Rossum02975121992-08-17 08:55:12 +00001074{
1075 char *fmt;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001076 const formatdef *f;
Guido van Rossum02975121992-08-17 08:55:12 +00001077 int size;
1078
Guido van Rossum43713e52000-02-29 13:59:29 +00001079 if (!PyArg_ParseTuple(args, "s:calcsize", &fmt))
Guido van Rossum02975121992-08-17 08:55:12 +00001080 return NULL;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001081 f = whichtable(&fmt);
1082 size = calcsize(fmt, f);
Guido van Rossum02975121992-08-17 08:55:12 +00001083 if (size < 0)
1084 return NULL;
Barry Warsaw30695fa1996-12-12 23:32:31 +00001085 return PyInt_FromLong((long)size);
Guido van Rossum02975121992-08-17 08:55:12 +00001086}
1087
1088
Guido van Rossum414fd481997-12-19 04:24:24 +00001089static char pack__doc__[] = "\
1090pack(fmt, v1, v2, ...) -> string\n\
1091Return string containing values v1, v2, ... packed according to fmt.\n\
1092See struct.__doc__ for more on format strings.";
Guido van Rossum02975121992-08-17 08:55:12 +00001093
Barry Warsaw30695fa1996-12-12 23:32:31 +00001094static PyObject *
Guido van Rossum02975121992-08-17 08:55:12 +00001095struct_pack(self, args)
Barry Warsaw30695fa1996-12-12 23:32:31 +00001096 PyObject *self; /* Not used */
1097 PyObject *args;
Guido van Rossum02975121992-08-17 08:55:12 +00001098{
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001099 const formatdef *f, *e;
Barry Warsaw30695fa1996-12-12 23:32:31 +00001100 PyObject *format, *result, *v;
Guido van Rossum02975121992-08-17 08:55:12 +00001101 char *fmt;
1102 int size, num;
1103 int i, n;
Guido van Rossumb9d338c1997-01-03 15:40:33 +00001104 char *s, *res, *restart, *nres;
Guido van Rossum02975121992-08-17 08:55:12 +00001105 char c;
Guido van Rossum02975121992-08-17 08:55:12 +00001106
Barry Warsaw30695fa1996-12-12 23:32:31 +00001107 if (args == NULL || !PyTuple_Check(args) ||
1108 (n = PyTuple_Size(args)) < 1)
1109 {
Fred Drake137507e2000-06-01 02:02:46 +00001110 PyErr_SetString(PyExc_TypeError,
1111 "struct.pack requires at least one argument");
Guido van Rossum02975121992-08-17 08:55:12 +00001112 return NULL;
1113 }
Barry Warsaw30695fa1996-12-12 23:32:31 +00001114 format = PyTuple_GetItem(args, 0);
1115 if (!PyArg_Parse(format, "s", &fmt))
Guido van Rossum02975121992-08-17 08:55:12 +00001116 return NULL;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001117 f = whichtable(&fmt);
1118 size = calcsize(fmt, f);
Guido van Rossum02975121992-08-17 08:55:12 +00001119 if (size < 0)
1120 return NULL;
Barry Warsaw30695fa1996-12-12 23:32:31 +00001121 result = PyString_FromStringAndSize((char *)NULL, size);
Guido van Rossum02975121992-08-17 08:55:12 +00001122 if (result == NULL)
1123 return NULL;
1124
1125 s = fmt;
1126 i = 1;
Barry Warsaw30695fa1996-12-12 23:32:31 +00001127 res = restart = PyString_AsString(result);
Guido van Rossum02975121992-08-17 08:55:12 +00001128
1129 while ((c = *s++) != '\0') {
Guido van Rossum730806d1998-04-10 22:27:42 +00001130 if (isspace((int)c))
Guido van Rossume20aef51997-08-26 20:39:54 +00001131 continue;
Guido van Rossum02975121992-08-17 08:55:12 +00001132 if ('0' <= c && c <= '9') {
1133 num = c - '0';
1134 while ('0' <= (c = *s++) && c <= '9')
1135 num = num*10 + (c - '0');
1136 if (c == '\0')
1137 break;
1138 }
1139 else
1140 num = 1;
1141
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001142 e = getentry(c, f);
1143 if (e == NULL)
1144 goto fail;
Guido van Rossumb9d338c1997-01-03 15:40:33 +00001145 nres = restart + align((int)(res-restart), c, e);
1146 /* Fill padd bytes with zeros */
1147 while (res < nres)
1148 *res++ = '\0';
Guido van Rossum3aa27fd1996-12-31 02:10:45 +00001149 if (num == 0 && c != 's')
1150 continue;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001151 do {
1152 if (c == 'x') {
1153 /* doesn't consume arguments */
Guido van Rossum3aa27fd1996-12-31 02:10:45 +00001154 memset(res, '\0', num);
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001155 res += num;
Guido van Rossum02975121992-08-17 08:55:12 +00001156 break;
Guido van Rossum02975121992-08-17 08:55:12 +00001157 }
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001158 if (i >= n) {
1159 PyErr_SetString(StructError,
1160 "insufficient arguments to pack");
1161 goto fail;
1162 }
1163 v = PyTuple_GetItem(args, i++);
1164 if (v == NULL)
1165 goto fail;
1166 if (c == 's') {
1167 /* num is string size, not repeat count */
1168 int n;
1169 if (!PyString_Check(v)) {
1170 PyErr_SetString(StructError,
1171 "argument for 's' must be a string");
1172 goto fail;
1173 }
1174 n = PyString_Size(v);
1175 if (n > num)
1176 n = num;
1177 if (n > 0)
1178 memcpy(res, PyString_AsString(v), n);
1179 if (n < num)
Guido van Rossum3aa27fd1996-12-31 02:10:45 +00001180 memset(res+n, '\0', num-n);
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001181 res += num;
1182 break;
1183 }
Guido van Rossum9eb671f1997-09-05 07:08:39 +00001184 else if (c == 'p') {
1185 /* num is string size + 1,
1186 to fit in the count byte */
1187 int n;
1188 num--; /* now num is max string size */
1189 if (!PyString_Check(v)) {
1190 PyErr_SetString(StructError,
1191 "argument for 'p' must be a string");
1192 goto fail;
1193 }
1194 n = PyString_Size(v);
1195 if (n > num)
1196 n = num;
1197 if (n > 0)
1198 memcpy(res+1, PyString_AsString(v), n);
1199 if (n < num)
1200 /* no real need, just to be nice */
1201 memset(res+1+n, '\0', num-n);
1202 *res++ = n; /* store the length byte */
1203 res += num;
1204 break;
1205 }
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001206 else {
1207 if (e->pack(res, v, e) < 0)
1208 goto fail;
1209 res += e->size;
1210 }
1211 } while (--num > 0);
Guido van Rossum02975121992-08-17 08:55:12 +00001212 }
1213
1214 if (i < n) {
Barry Warsaw30695fa1996-12-12 23:32:31 +00001215 PyErr_SetString(StructError,
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001216 "too many arguments for pack format");
Guido van Rossum02975121992-08-17 08:55:12 +00001217 goto fail;
1218 }
1219
1220 return result;
1221
1222 fail:
Barry Warsaw30695fa1996-12-12 23:32:31 +00001223 Py_DECREF(result);
Guido van Rossum02975121992-08-17 08:55:12 +00001224 return NULL;
1225}
1226
1227
Guido van Rossum9897f0f1997-12-21 06:46:20 +00001228static char unpack__doc__[] = "\
1229unpack(fmt, string) -> (v1, v2, ...)\n\
1230Unpack the string, containing packed C structure data, according\n\
1231to fmt. Requires len(string)==calcsize(fmt).\n\
Guido van Rossum414fd481997-12-19 04:24:24 +00001232See struct.__doc__ for more on format strings.";
Guido van Rossum02975121992-08-17 08:55:12 +00001233
Barry Warsaw30695fa1996-12-12 23:32:31 +00001234static PyObject *
Guido van Rossum02975121992-08-17 08:55:12 +00001235struct_unpack(self, args)
Barry Warsaw30695fa1996-12-12 23:32:31 +00001236 PyObject *self; /* Not used */
1237 PyObject *args;
Guido van Rossum02975121992-08-17 08:55:12 +00001238{
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001239 const formatdef *f, *e;
Guido van Rossum02975121992-08-17 08:55:12 +00001240 char *str, *start, *fmt, *s;
1241 char c;
Barry Warsawb9a781e1997-01-03 00:26:28 +00001242 int len, size, num;
Barry Warsaw30695fa1996-12-12 23:32:31 +00001243 PyObject *res, *v;
Guido van Rossum02975121992-08-17 08:55:12 +00001244
Guido van Rossum43713e52000-02-29 13:59:29 +00001245 if (!PyArg_ParseTuple(args, "ss#:unpack", &fmt, &start, &len))
Guido van Rossum02975121992-08-17 08:55:12 +00001246 return NULL;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001247 f = whichtable(&fmt);
1248 size = calcsize(fmt, f);
1249 if (size < 0)
1250 return NULL;
Guido van Rossum02975121992-08-17 08:55:12 +00001251 if (size != len) {
Barry Warsaw30695fa1996-12-12 23:32:31 +00001252 PyErr_SetString(StructError,
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001253 "unpack str size does not match format");
Guido van Rossum02975121992-08-17 08:55:12 +00001254 return NULL;
1255 }
Barry Warsaw30695fa1996-12-12 23:32:31 +00001256 res = PyList_New(0);
Guido van Rossum02975121992-08-17 08:55:12 +00001257 if (res == NULL)
1258 return NULL;
1259 str = start;
1260 s = fmt;
1261 while ((c = *s++) != '\0') {
Guido van Rossum730806d1998-04-10 22:27:42 +00001262 if (isspace((int)c))
Guido van Rossume20aef51997-08-26 20:39:54 +00001263 continue;
Guido van Rossum02975121992-08-17 08:55:12 +00001264 if ('0' <= c && c <= '9') {
1265 num = c - '0';
1266 while ('0' <= (c = *s++) && c <= '9')
1267 num = num*10 + (c - '0');
1268 if (c == '\0')
1269 break;
1270 }
1271 else
1272 num = 1;
1273
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001274 e = getentry(c, f);
1275 if (e == NULL)
1276 goto fail;
1277 str = start + align((int)(str-start), c, e);
Guido van Rossum3aa27fd1996-12-31 02:10:45 +00001278 if (num == 0 && c != 's')
1279 continue;
Guido van Rossum02975121992-08-17 08:55:12 +00001280
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001281 do {
1282 if (c == 'x') {
1283 str += num;
Guido van Rossum02975121992-08-17 08:55:12 +00001284 break;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001285 }
1286 if (c == 's') {
1287 /* num is string size, not repeat count */
1288 v = PyString_FromStringAndSize(str, num);
1289 if (v == NULL)
1290 goto fail;
1291 str += num;
1292 num = 0;
1293 }
Guido van Rossum9eb671f1997-09-05 07:08:39 +00001294 else if (c == 'p') {
1295 /* num is string buffer size,
1296 not repeat count */
1297 int n = *(unsigned char*)str;
1298 /* first byte (unsigned) is string size */
1299 if (n >= num)
1300 n = num-1;
1301 v = PyString_FromStringAndSize(str+1, n);
1302 if (v == NULL)
1303 goto fail;
1304 str += num;
1305 num = 0;
1306 }
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001307 else {
1308 v = e->unpack(str, e);
1309 if (v == NULL)
1310 goto fail;
1311 str += e->size;
Guido van Rossum02975121992-08-17 08:55:12 +00001312 }
Barry Warsaw30695fa1996-12-12 23:32:31 +00001313 if (v == NULL || PyList_Append(res, v) < 0)
Guido van Rossum02975121992-08-17 08:55:12 +00001314 goto fail;
Barry Warsaw30695fa1996-12-12 23:32:31 +00001315 Py_DECREF(v);
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001316 } while (--num > 0);
Guido van Rossum02975121992-08-17 08:55:12 +00001317 }
1318
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001319 v = PyList_AsTuple(res);
1320 Py_DECREF(res);
1321 return v;
Guido van Rossum02975121992-08-17 08:55:12 +00001322
1323 fail:
Barry Warsaw30695fa1996-12-12 23:32:31 +00001324 Py_DECREF(res);
Guido van Rossum02975121992-08-17 08:55:12 +00001325 return NULL;
1326}
1327
Guido van Rossum90ddb7b1992-08-19 16:44:15 +00001328
Guido van Rossum02975121992-08-17 08:55:12 +00001329/* List of functions */
1330
Barry Warsaw30695fa1996-12-12 23:32:31 +00001331static PyMethodDef struct_methods[] = {
Guido van Rossum414fd481997-12-19 04:24:24 +00001332 {"calcsize", struct_calcsize, METH_VARARGS, calcsize__doc__},
1333 {"pack", struct_pack, METH_VARARGS, pack__doc__},
1334 {"unpack", struct_unpack, METH_VARARGS, unpack__doc__},
Guido van Rossum02975121992-08-17 08:55:12 +00001335 {NULL, NULL} /* sentinel */
1336};
1337
1338
1339/* Module initialization */
1340
Guido van Rossum3886bb61998-12-04 18:50:17 +00001341DL_EXPORT(void)
Guido van Rossum02975121992-08-17 08:55:12 +00001342initstruct()
1343{
Barry Warsaw30695fa1996-12-12 23:32:31 +00001344 PyObject *m, *d;
Guido van Rossum02975121992-08-17 08:55:12 +00001345
1346 /* Create the module and add the functions */
Guido van Rossum414fd481997-12-19 04:24:24 +00001347 m = Py_InitModule4("struct", struct_methods, struct__doc__,
1348 (PyObject*)NULL, PYTHON_API_VERSION);
Guido van Rossum02975121992-08-17 08:55:12 +00001349
1350 /* Add some symbolic constants to the module */
Barry Warsaw30695fa1996-12-12 23:32:31 +00001351 d = PyModule_GetDict(m);
Guido van Rossum0cb96de1997-10-01 04:29:29 +00001352 StructError = PyErr_NewException("struct.error", NULL, NULL);
1353 if (StructError == NULL)
1354 return;
Barry Warsaw30695fa1996-12-12 23:32:31 +00001355 PyDict_SetItemString(d, "error", StructError);
Guido van Rossum02975121992-08-17 08:55:12 +00001356}