blob: d4f8d861c275724aa4597c74444823c9243dc734 [file] [log] [blame]
Guido van Rossum02975121992-08-17 08:55:12 +00001/* struct module -- pack values into and (out of) strings */
2
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00003/* New version supporting byte order, alignment and size options,
4 character strings, and unsigned numbers */
5
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006#include "Python.h"
7#include <ctype.h>
8
9PyDoc_STRVAR(struct__doc__,
10"Functions to convert between Python values and C structs.\n\
Guido van Rossum414fd481997-12-19 04:24:24 +000011Python strings are used to hold the data representing the C struct\n\
12and also as format strings to describe the layout of data in the C struct.\n\
13\n\
Tim Petersbe800852001-06-11 16:45:33 +000014The optional first format char indicates byte order, size and alignment:\n\
15 @: native order, size & alignment (default)\n\
16 =: native order, std. size & alignment\n\
17 <: little-endian, std. size & alignment\n\
18 >: big-endian, std. size & alignment\n\
19 !: same as >\n\
Guido van Rossum414fd481997-12-19 04:24:24 +000020\n\
21The remaining chars indicate types of args and must match exactly;\n\
22these can be preceded by a decimal repeat count:\n\
23 x: pad byte (no data); c:char; b:signed byte; B:unsigned byte;\n\
24 h:short; H:unsigned short; i:int; I:unsigned int;\n\
25 l:long; L:unsigned long; f:float; d:double.\n\
26Special cases (preceding decimal count indicates length):\n\
Tim Peters7b9542a2001-06-10 23:40:19 +000027 s:string (array of char); p: pascal string (with count byte).\n\
Guido van Rossum78694d91998-09-18 14:14:13 +000028Special case (only available in native format):\n\
29 P:an integer type that is wide enough to hold a pointer.\n\
Tim Peters7b9542a2001-06-10 23:40:19 +000030Special case (not in native mode unless 'long long' in platform C):\n\
31 q:long long; Q:unsigned long long\n\
Guido van Rossum414fd481997-12-19 04:24:24 +000032Whitespace between formats is ignored.\n\
33\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000034The variable struct.error is an exception raised on errors.");
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +000035
36
37/* Exception */
38
Barry Warsaw30695fa1996-12-12 23:32:31 +000039static PyObject *StructError;
Guido van Rossum02975121992-08-17 08:55:12 +000040
41
42/* Define various structs to figure out the alignments of types */
43
Jack Jansen971e1df1995-02-02 14:29:10 +000044#ifdef __MWERKS__
45/*
46** XXXX We have a problem here. There are no unique alignment rules
Tim Peters2d4e0772001-06-11 16:57:33 +000047** on the PowerPC mac.
Jack Jansen971e1df1995-02-02 14:29:10 +000048*/
49#ifdef __powerc
50#pragma options align=mac68k
51#endif
52#endif /* __MWERKS__ */
53
Guido van Rossume2ae77b2001-10-24 20:42:55 +000054typedef struct { char c; short x; } st_short;
55typedef struct { char c; int x; } st_int;
56typedef struct { char c; long x; } st_long;
57typedef struct { char c; float x; } st_float;
58typedef struct { char c; double x; } st_double;
59typedef struct { char c; void *x; } st_void_p;
Guido van Rossum02975121992-08-17 08:55:12 +000060
Guido van Rossume2ae77b2001-10-24 20:42:55 +000061#define SHORT_ALIGN (sizeof(st_short) - sizeof(short))
62#define INT_ALIGN (sizeof(st_int) - sizeof(int))
63#define LONG_ALIGN (sizeof(st_long) - sizeof(long))
64#define FLOAT_ALIGN (sizeof(st_float) - sizeof(float))
65#define DOUBLE_ALIGN (sizeof(st_double) - sizeof(double))
66#define VOID_P_ALIGN (sizeof(st_void_p) - sizeof(void *))
Guido van Rossum02975121992-08-17 08:55:12 +000067
Tim Peters7b9542a2001-06-10 23:40:19 +000068/* We can't support q and Q in native mode unless the compiler does;
69 in std mode, they're 8 bytes on all platforms. */
70#ifdef HAVE_LONG_LONG
71typedef struct { char c; LONG_LONG x; } s_long_long;
72#define LONG_LONG_ALIGN (sizeof(s_long_long) - sizeof(LONG_LONG))
Tim Peters7b9542a2001-06-10 23:40:19 +000073#endif
74
Martin v. Löwis2af72d52000-09-15 08:10:33 +000075#define STRINGIFY(x) #x
76
Jack Jansen971e1df1995-02-02 14:29:10 +000077#ifdef __powerc
78#pragma options align=reset
79#endif
80
Tim Peters7a3bfc32001-06-12 01:22:22 +000081/* Helper to get a PyLongObject by hook or by crook. Caller should decref. */
82
83static PyObject *
84get_pylong(PyObject *v)
85{
86 PyNumberMethods *m;
87
88 assert(v != NULL);
89 if (PyInt_Check(v))
90 return PyLong_FromLong(PyInt_AS_LONG(v));
91 if (PyLong_Check(v)) {
92 Py_INCREF(v);
93 return v;
94 }
95 m = v->ob_type->tp_as_number;
96 if (m != NULL && m->nb_long != NULL) {
97 v = m->nb_long(v);
98 if (v == NULL)
99 return NULL;
100 if (PyLong_Check(v))
101 return v;
102 Py_DECREF(v);
103 }
104 PyErr_SetString(StructError,
105 "cannot convert argument to long");
106 return NULL;
107}
108
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000109/* Helper routine to get a Python integer and raise the appropriate error
110 if it isn't one */
111
112static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000113get_long(PyObject *v, long *p)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000114{
115 long x = PyInt_AsLong(v);
116 if (x == -1 && PyErr_Occurred()) {
Fred Draked3dbb381998-05-28 04:35:49 +0000117 if (PyErr_ExceptionMatches(PyExc_TypeError))
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000118 PyErr_SetString(StructError,
119 "required argument is not an integer");
120 return -1;
121 }
122 *p = x;
123 return 0;
124}
125
126
Guido van Rossum60c50611996-12-31 16:29:52 +0000127/* Same, but handling unsigned long */
128
129static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000130get_ulong(PyObject *v, unsigned long *p)
Guido van Rossum60c50611996-12-31 16:29:52 +0000131{
Guido van Rossum6c87eca1997-01-03 19:08:16 +0000132 if (PyLong_Check(v)) {
133 unsigned long x = PyLong_AsUnsignedLong(v);
134 if (x == (unsigned long)(-1) && PyErr_Occurred())
Guido van Rossum60c50611996-12-31 16:29:52 +0000135 return -1;
Guido van Rossum6c87eca1997-01-03 19:08:16 +0000136 *p = x;
137 return 0;
Guido van Rossum60c50611996-12-31 16:29:52 +0000138 }
Guido van Rossum6c87eca1997-01-03 19:08:16 +0000139 else {
140 return get_long(v, (long *)p);
141 }
Guido van Rossum60c50611996-12-31 16:29:52 +0000142}
143
Tim Peters7b9542a2001-06-10 23:40:19 +0000144#ifdef HAVE_LONG_LONG
145
146/* Same, but handling native long long. */
147
148static int
149get_longlong(PyObject *v, LONG_LONG *p)
150{
151 LONG_LONG x;
Tim Peters7b9542a2001-06-10 23:40:19 +0000152
Tim Peters7a3bfc32001-06-12 01:22:22 +0000153 v = get_pylong(v);
154 if (v == NULL)
155 return -1;
Tim Peters7b9542a2001-06-10 23:40:19 +0000156 assert(PyLong_Check(v));
157 x = PyLong_AsLongLong(v);
Tim Peters7a3bfc32001-06-12 01:22:22 +0000158 Py_DECREF(v);
Tim Peters7b9542a2001-06-10 23:40:19 +0000159 if (x == (LONG_LONG)-1 && PyErr_Occurred())
160 return -1;
161 *p = x;
162 return 0;
163}
164
165/* Same, but handling native unsigned long long. */
166
167static int
168get_ulonglong(PyObject *v, unsigned LONG_LONG *p)
169{
170 unsigned LONG_LONG x;
Tim Peters7b9542a2001-06-10 23:40:19 +0000171
Tim Peters7a3bfc32001-06-12 01:22:22 +0000172 v = get_pylong(v);
173 if (v == NULL)
174 return -1;
Tim Peters7b9542a2001-06-10 23:40:19 +0000175 assert(PyLong_Check(v));
176 x = PyLong_AsUnsignedLongLong(v);
Tim Peters7a3bfc32001-06-12 01:22:22 +0000177 Py_DECREF(v);
Tim Peters7b9542a2001-06-10 23:40:19 +0000178 if (x == (unsigned LONG_LONG)-1 && PyErr_Occurred())
179 return -1;
180 *p = x;
181 return 0;
182}
183
184#endif
Guido van Rossum60c50611996-12-31 16:29:52 +0000185
Guido van Rossum74679b41997-01-02 22:21:36 +0000186/* Floating point helpers */
187
188/* These use ANSI/IEEE Standard 754-1985 (Standard for Binary Floating
189 Point Arithmetic). See the following URL:
190 http://www.psc.edu/general/software/packages/ieee/ieee.html */
191
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000192/* XXX Inf/NaN are not handled quite right (but underflow is!) */
Guido van Rossum74679b41997-01-02 22:21:36 +0000193
194static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000195pack_float(double x, /* The number to pack */
196 char *p, /* Where to pack the high order byte */
197 int incr) /* 1 for big-endian; -1 for little-endian */
Guido van Rossum74679b41997-01-02 22:21:36 +0000198{
199 int s;
200 int e;
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000201 double f;
202 long fbits;
Guido van Rossum74679b41997-01-02 22:21:36 +0000203
204 if (x < 0) {
205 s = 1;
206 x = -x;
207 }
208 else
209 s = 0;
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000210
211 f = frexp(x, &e);
212
213 /* Normalize f to be in the range [1.0, 2.0) */
214 if (0.5 <= f && f < 1.0) {
215 f *= 2.0;
Guido van Rossum74679b41997-01-02 22:21:36 +0000216 e--;
217 }
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000218 else if (f == 0.0) {
Guido van Rossum74679b41997-01-02 22:21:36 +0000219 e = 0;
220 }
221 else {
222 PyErr_SetString(PyExc_SystemError,
223 "frexp() result out of range");
224 return -1;
225 }
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000226
227 if (e >= 128) {
228 /* XXX 128 itself is reserved for Inf/NaN */
Guido van Rossum74679b41997-01-02 22:21:36 +0000229 PyErr_SetString(PyExc_OverflowError,
230 "float too large to pack with f format");
231 return -1;
232 }
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000233 else if (e < -126) {
234 /* Gradual underflow */
235 f = ldexp(f, 126 + e);
Guido van Rossum74679b41997-01-02 22:21:36 +0000236 e = 0;
237 }
Guido van Rossum8f3c8121997-11-04 17:12:33 +0000238 else if (!(e == 0 && f == 0.0)) {
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000239 e += 127;
240 f -= 1.0; /* Get rid of leading 1 */
Guido van Rossum74679b41997-01-02 22:21:36 +0000241 }
242
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000243 f *= 8388608.0; /* 2**23 */
244 fbits = (long) floor(f + 0.5); /* Round */
245
Guido van Rossum74679b41997-01-02 22:21:36 +0000246 /* First byte */
247 *p = (s<<7) | (e>>1);
248 p += incr;
249
250 /* Second byte */
Guido van Rossum7844e381997-04-11 20:44:04 +0000251 *p = (char) (((e&1)<<7) | (fbits>>16));
Guido van Rossum74679b41997-01-02 22:21:36 +0000252 p += incr;
253
254 /* Third byte */
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000255 *p = (fbits>>8) & 0xFF;
Guido van Rossum74679b41997-01-02 22:21:36 +0000256 p += incr;
257
258 /* Fourth byte */
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000259 *p = fbits&0xFF;
Guido van Rossum74679b41997-01-02 22:21:36 +0000260
261 /* Done */
262 return 0;
263}
264
265static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000266pack_double(double x, /* The number to pack */
267 char *p, /* Where to pack the high order byte */
268 int incr) /* 1 for big-endian; -1 for little-endian */
Guido van Rossum74679b41997-01-02 22:21:36 +0000269{
270 int s;
271 int e;
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000272 double f;
Guido van Rossum74679b41997-01-02 22:21:36 +0000273 long fhi, flo;
274
275 if (x < 0) {
276 s = 1;
277 x = -x;
278 }
279 else
280 s = 0;
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000281
282 f = frexp(x, &e);
283
284 /* Normalize f to be in the range [1.0, 2.0) */
285 if (0.5 <= f && f < 1.0) {
286 f *= 2.0;
Guido van Rossum74679b41997-01-02 22:21:36 +0000287 e--;
288 }
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000289 else if (f == 0.0) {
Guido van Rossum74679b41997-01-02 22:21:36 +0000290 e = 0;
291 }
292 else {
293 PyErr_SetString(PyExc_SystemError,
294 "frexp() result out of range");
295 return -1;
296 }
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000297
298 if (e >= 1024) {
299 /* XXX 1024 itself is reserved for Inf/NaN */
Guido van Rossum74679b41997-01-02 22:21:36 +0000300 PyErr_SetString(PyExc_OverflowError,
301 "float too large to pack with d format");
302 return -1;
303 }
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000304 else if (e < -1022) {
305 /* Gradual underflow */
306 f = ldexp(f, 1022 + e);
Guido van Rossum74679b41997-01-02 22:21:36 +0000307 e = 0;
308 }
Guido van Rossum8f3c8121997-11-04 17:12:33 +0000309 else if (!(e == 0 && f == 0.0)) {
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000310 e += 1023;
311 f -= 1.0; /* Get rid of leading 1 */
Guido van Rossum74679b41997-01-02 22:21:36 +0000312 }
313
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000314 /* fhi receives the high 28 bits; flo the low 24 bits (== 52 bits) */
315 f *= 268435456.0; /* 2**28 */
316 fhi = (long) floor(f); /* Truncate */
317 f -= (double)fhi;
318 f *= 16777216.0; /* 2**24 */
319 flo = (long) floor(f + 0.5); /* Round */
320
Guido van Rossum74679b41997-01-02 22:21:36 +0000321 /* First byte */
322 *p = (s<<7) | (e>>4);
323 p += incr;
324
325 /* Second byte */
Guido van Rossum7844e381997-04-11 20:44:04 +0000326 *p = (char) (((e&0xF)<<4) | (fhi>>24));
Guido van Rossum74679b41997-01-02 22:21:36 +0000327 p += incr;
328
329 /* Third byte */
330 *p = (fhi>>16) & 0xFF;
331 p += incr;
332
333 /* Fourth byte */
334 *p = (fhi>>8) & 0xFF;
335 p += incr;
336
337 /* Fifth byte */
338 *p = fhi & 0xFF;
339 p += incr;
340
341 /* Sixth byte */
342 *p = (flo>>16) & 0xFF;
343 p += incr;
344
345 /* Seventh byte */
346 *p = (flo>>8) & 0xFF;
347 p += incr;
348
349 /* Eighth byte */
350 *p = flo & 0xFF;
351 p += incr;
352
353 /* Done */
354 return 0;
355}
356
357static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000358unpack_float(const char *p, /* Where the high order byte is */
359 int incr) /* 1 for big-endian; -1 for little-endian */
Guido van Rossum74679b41997-01-02 22:21:36 +0000360{
361 int s;
362 int e;
363 long f;
364 double x;
365
366 /* First byte */
367 s = (*p>>7) & 1;
368 e = (*p & 0x7F) << 1;
369 p += incr;
370
371 /* Second byte */
372 e |= (*p>>7) & 1;
373 f = (*p & 0x7F) << 16;
374 p += incr;
375
376 /* Third byte */
377 f |= (*p & 0xFF) << 8;
378 p += incr;
379
380 /* Fourth byte */
381 f |= *p & 0xFF;
382
383 x = (double)f / 8388608.0;
384
385 /* XXX This sadly ignores Inf/NaN issues */
Guido van Rossum07ef6551997-01-02 22:31:07 +0000386 if (e == 0)
387 e = -126;
388 else {
389 x += 1.0;
390 e -= 127;
391 }
392 x = ldexp(x, e);
Guido van Rossum74679b41997-01-02 22:21:36 +0000393
394 if (s)
395 x = -x;
396
397 return PyFloat_FromDouble(x);
398}
399
400static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000401unpack_double(const char *p, /* Where the high order byte is */
402 int incr) /* 1 for big-endian; -1 for little-endian */
Guido van Rossum74679b41997-01-02 22:21:36 +0000403{
404 int s;
405 int e;
406 long fhi, flo;
407 double x;
408
409 /* First byte */
410 s = (*p>>7) & 1;
411 e = (*p & 0x7F) << 4;
412 p += incr;
413
414 /* Second byte */
415 e |= (*p>>4) & 0xF;
416 fhi = (*p & 0xF) << 24;
417 p += incr;
418
419 /* Third byte */
420 fhi |= (*p & 0xFF) << 16;
421 p += incr;
422
423 /* Fourth byte */
424 fhi |= (*p & 0xFF) << 8;
425 p += incr;
426
427 /* Fifth byte */
428 fhi |= *p & 0xFF;
429 p += incr;
430
431 /* Sixth byte */
432 flo = (*p & 0xFF) << 16;
433 p += incr;
434
435 /* Seventh byte */
436 flo |= (*p & 0xFF) << 8;
437 p += incr;
438
439 /* Eighth byte */
440 flo |= *p & 0xFF;
441 p += incr;
442
443 x = (double)fhi + (double)flo / 16777216.0; /* 2**24 */
444 x /= 268435456.0; /* 2**28 */
445
446 /* XXX This sadly ignores Inf/NaN */
Guido van Rossum07ef6551997-01-02 22:31:07 +0000447 if (e == 0)
448 e = -1022;
449 else {
450 x += 1.0;
451 e -= 1023;
452 }
453 x = ldexp(x, e);
Guido van Rossum74679b41997-01-02 22:21:36 +0000454
455 if (s)
456 x = -x;
457
458 return PyFloat_FromDouble(x);
459}
460
461
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000462/* The translation function for each format character is table driven */
463
464typedef struct _formatdef {
465 char format;
466 int size;
467 int alignment;
Tim Petersdbd9ba62000-07-09 03:09:57 +0000468 PyObject* (*unpack)(const char *,
469 const struct _formatdef *);
470 int (*pack)(char *, PyObject *,
471 const struct _formatdef *);
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000472} formatdef;
473
Tim Peters7b9542a2001-06-10 23:40:19 +0000474/* A large number of small routines follow, with names of the form
475
476 [bln][up]_TYPE
477
478 [bln] distiguishes among big-endian, little-endian and native.
479 [pu] distiguishes between pack (to struct) and unpack (from struct).
480 TYPE is one of char, byte, ubyte, etc.
481*/
482
Tim Peters7a3bfc32001-06-12 01:22:22 +0000483/* Native mode routines. ****************************************************/
Guido van Rossum960bc542002-09-03 18:42:21 +0000484/* NOTE:
485 In all n[up]_<type> routines handling types larger than 1 byte, there is
486 *no* guarantee that the p pointer is properly aligned for each type,
487 therefore memcpy is called. An intermediate variable is used to
488 compensate for big-endian architectures.
489 Normally both the intermediate variable and the memcpy call will be
490 skipped by C optimisation in little-endian architectures (gcc >= 2.91
491 does this). */
Tim Peters7b9542a2001-06-10 23:40:19 +0000492
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000493static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000494nu_char(const char *p, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000495{
496 return PyString_FromStringAndSize(p, 1);
497}
498
499static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000500nu_byte(const char *p, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000501{
502 return PyInt_FromLong((long) *(signed char *)p);
503}
504
505static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000506nu_ubyte(const char *p, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000507{
508 return PyInt_FromLong((long) *(unsigned char *)p);
509}
510
511static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000512nu_short(const char *p, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000513{
Guido van Rossum960bc542002-09-03 18:42:21 +0000514 short x;
515 memcpy((char *)&x, p, sizeof x);
516 return PyInt_FromLong((long)x);
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000517}
518
519static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000520nu_ushort(const char *p, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000521{
Guido van Rossum960bc542002-09-03 18:42:21 +0000522 unsigned short x;
523 memcpy((char *)&x, p, sizeof x);
524 return PyInt_FromLong((long)x);
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000525}
526
527static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000528nu_int(const char *p, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000529{
Guido van Rossum960bc542002-09-03 18:42:21 +0000530 int x;
531 memcpy((char *)&x, p, sizeof x);
532 return PyInt_FromLong((long)x);
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000533}
534
535static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000536nu_uint(const char *p, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000537{
Guido van Rossum960bc542002-09-03 18:42:21 +0000538 unsigned int x;
539 memcpy((char *)&x, p, sizeof x);
Guido van Rossum6c87eca1997-01-03 19:08:16 +0000540 return PyLong_FromUnsignedLong((unsigned long)x);
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000541}
542
543static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000544nu_long(const char *p, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000545{
Guido van Rossum960bc542002-09-03 18:42:21 +0000546 long x;
547 memcpy((char *)&x, p, sizeof x);
548 return PyInt_FromLong(x);
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000549}
550
551static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000552nu_ulong(const char *p, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000553{
Guido van Rossum960bc542002-09-03 18:42:21 +0000554 unsigned long x;
555 memcpy((char *)&x, p, sizeof x);
556 return PyLong_FromUnsignedLong(x);
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000557}
558
Tim Peters7b9542a2001-06-10 23:40:19 +0000559/* Native mode doesn't support q or Q unless the platform C supports
560 long long (or, on Windows, __int64). */
561
562#ifdef HAVE_LONG_LONG
563
564static PyObject *
565nu_longlong(const char *p, const formatdef *f)
566{
Tim Peters3dac5592001-07-18 20:47:31 +0000567 LONG_LONG x;
Guido van Rossum960bc542002-09-03 18:42:21 +0000568 memcpy((char *)&x, p, sizeof x);
Tim Peters3dac5592001-07-18 20:47:31 +0000569 return PyLong_FromLongLong(x);
Tim Peters7b9542a2001-06-10 23:40:19 +0000570}
571
572static PyObject *
573nu_ulonglong(const char *p, const formatdef *f)
574{
Tim Peters3dac5592001-07-18 20:47:31 +0000575 unsigned LONG_LONG x;
Guido van Rossum960bc542002-09-03 18:42:21 +0000576 memcpy((char *)&x, p, sizeof x);
Tim Peters3dac5592001-07-18 20:47:31 +0000577 return PyLong_FromUnsignedLongLong(x);
Tim Peters7b9542a2001-06-10 23:40:19 +0000578}
Guido van Rossum960bc542002-09-03 18:42:21 +0000579
Tim Peters7b9542a2001-06-10 23:40:19 +0000580#endif
581
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000582static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000583nu_float(const char *p, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000584{
585 float x;
Guido van Rossum960bc542002-09-03 18:42:21 +0000586 memcpy((char *)&x, p, sizeof x);
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000587 return PyFloat_FromDouble((double)x);
588}
589
590static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000591nu_double(const char *p, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000592{
593 double x;
Guido van Rossum960bc542002-09-03 18:42:21 +0000594 memcpy((char *)&x, p, sizeof x);
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000595 return PyFloat_FromDouble(x);
596}
597
Guido van Rossum78694d91998-09-18 14:14:13 +0000598static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000599nu_void_p(const char *p, const formatdef *f)
Guido van Rossum78694d91998-09-18 14:14:13 +0000600{
Guido van Rossum960bc542002-09-03 18:42:21 +0000601 void *x;
602 memcpy((char *)&x, p, sizeof x);
603 return PyLong_FromVoidPtr(x);
Guido van Rossum78694d91998-09-18 14:14:13 +0000604}
605
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000606static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000607np_byte(char *p, PyObject *v, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000608{
609 long x;
610 if (get_long(v, &x) < 0)
611 return -1;
Martin v. Löwis66de5492000-09-15 07:31:57 +0000612 if (x < -128 || x > 127){
613 PyErr_SetString(StructError,
614 "byte format requires -128<=number<=127");
615 return -1;
616 }
617 *p = (char)x;
618 return 0;
619}
620
621static int
622np_ubyte(char *p, PyObject *v, const formatdef *f)
623{
624 long x;
625 if (get_long(v, &x) < 0)
626 return -1;
627 if (x < 0 || x > 255){
628 PyErr_SetString(StructError,
629 "ubyte format requires 0<=number<=255");
630 return -1;
631 }
Guido van Rossum7844e381997-04-11 20:44:04 +0000632 *p = (char)x;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000633 return 0;
634}
635
636static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000637np_char(char *p, PyObject *v, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000638{
639 if (!PyString_Check(v) || PyString_Size(v) != 1) {
640 PyErr_SetString(StructError,
641 "char format require string of length 1");
642 return -1;
643 }
644 *p = *PyString_AsString(v);
645 return 0;
646}
647
648static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000649np_short(char *p, PyObject *v, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000650{
651 long x;
Guido van Rossum960bc542002-09-03 18:42:21 +0000652 short y;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000653 if (get_long(v, &x) < 0)
654 return -1;
Martin v. Löwis2af72d52000-09-15 08:10:33 +0000655 if (x < SHRT_MIN || x > SHRT_MAX){
Martin v. Löwis66de5492000-09-15 07:31:57 +0000656 PyErr_SetString(StructError,
Martin v. Löwis2af72d52000-09-15 08:10:33 +0000657 "short format requires " STRINGIFY(SHRT_MIN)
Guido van Rossum960bc542002-09-03 18:42:21 +0000658 "<=number<=" STRINGIFY(SHRT_MAX));
Martin v. Löwis66de5492000-09-15 07:31:57 +0000659 return -1;
660 }
Guido van Rossum960bc542002-09-03 18:42:21 +0000661 y = (short)x;
662 memcpy(p, (char *)&y, sizeof y);
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000663 return 0;
664}
665
666static int
Martin v. Löwis66de5492000-09-15 07:31:57 +0000667np_ushort(char *p, PyObject *v, const formatdef *f)
668{
669 long x;
Guido van Rossum960bc542002-09-03 18:42:21 +0000670 unsigned short y;
Martin v. Löwis66de5492000-09-15 07:31:57 +0000671 if (get_long(v, &x) < 0)
672 return -1;
Martin v. Löwis2af72d52000-09-15 08:10:33 +0000673 if (x < 0 || x > USHRT_MAX){
Martin v. Löwis66de5492000-09-15 07:31:57 +0000674 PyErr_SetString(StructError,
Martin v. Löwis2af72d52000-09-15 08:10:33 +0000675 "short format requires 0<=number<=" STRINGIFY(USHRT_MAX));
Martin v. Löwis66de5492000-09-15 07:31:57 +0000676 return -1;
677 }
Guido van Rossum960bc542002-09-03 18:42:21 +0000678 y = (unsigned short)x;
679 memcpy(p, (char *)&y, sizeof y);
Martin v. Löwis66de5492000-09-15 07:31:57 +0000680 return 0;
681}
682
683static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000684np_int(char *p, PyObject *v, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000685{
686 long x;
Guido van Rossum960bc542002-09-03 18:42:21 +0000687 int y;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000688 if (get_long(v, &x) < 0)
689 return -1;
Guido van Rossum960bc542002-09-03 18:42:21 +0000690 y = (int)x;
691 memcpy(p, (char *)&y, sizeof y);
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000692 return 0;
693}
694
695static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000696np_uint(char *p, PyObject *v, const formatdef *f)
Guido van Rossum60c50611996-12-31 16:29:52 +0000697{
698 unsigned long x;
Guido van Rossum960bc542002-09-03 18:42:21 +0000699 unsigned int y;
Guido van Rossum60c50611996-12-31 16:29:52 +0000700 if (get_ulong(v, &x) < 0)
701 return -1;
Guido van Rossum960bc542002-09-03 18:42:21 +0000702 y = (unsigned int)x;
703 memcpy(p, (char *)&y, sizeof y);
Guido van Rossum60c50611996-12-31 16:29:52 +0000704 return 0;
705}
706
707static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000708np_long(char *p, PyObject *v, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000709{
710 long x;
711 if (get_long(v, &x) < 0)
712 return -1;
Guido van Rossum960bc542002-09-03 18:42:21 +0000713 memcpy(p, (char *)&x, sizeof x);
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000714 return 0;
715}
716
717static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000718np_ulong(char *p, PyObject *v, const formatdef *f)
Guido van Rossum60c50611996-12-31 16:29:52 +0000719{
720 unsigned long x;
721 if (get_ulong(v, &x) < 0)
722 return -1;
Guido van Rossum960bc542002-09-03 18:42:21 +0000723 memcpy(p, (char *)&x, sizeof x);
Guido van Rossum60c50611996-12-31 16:29:52 +0000724 return 0;
725}
726
Tim Peters7b9542a2001-06-10 23:40:19 +0000727#ifdef HAVE_LONG_LONG
728
729static int
730np_longlong(char *p, PyObject *v, const formatdef *f)
731{
732 LONG_LONG x;
733 if (get_longlong(v, &x) < 0)
734 return -1;
Guido van Rossum960bc542002-09-03 18:42:21 +0000735 memcpy(p, (char *)&x, sizeof x);
Tim Peters7b9542a2001-06-10 23:40:19 +0000736 return 0;
737}
738
739static int
740np_ulonglong(char *p, PyObject *v, const formatdef *f)
741{
742 unsigned LONG_LONG x;
743 if (get_ulonglong(v, &x) < 0)
744 return -1;
Guido van Rossum960bc542002-09-03 18:42:21 +0000745 memcpy(p, (char *)&x, sizeof x);
Tim Peters7b9542a2001-06-10 23:40:19 +0000746 return 0;
747}
Tim Peters7b9542a2001-06-10 23:40:19 +0000748#endif
749
Guido van Rossum60c50611996-12-31 16:29:52 +0000750static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000751np_float(char *p, PyObject *v, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000752{
753 float x = (float)PyFloat_AsDouble(v);
754 if (x == -1 && PyErr_Occurred()) {
755 PyErr_SetString(StructError,
756 "required argument is not a float");
757 return -1;
758 }
Guido van Rossum960bc542002-09-03 18:42:21 +0000759 memcpy(p, (char *)&x, sizeof x);
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000760 return 0;
761}
762
763static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000764np_double(char *p, PyObject *v, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000765{
766 double x = PyFloat_AsDouble(v);
767 if (x == -1 && PyErr_Occurred()) {
768 PyErr_SetString(StructError,
769 "required argument is not a float");
770 return -1;
771 }
772 memcpy(p, (char *)&x, sizeof(double));
773 return 0;
774}
775
Guido van Rossum78694d91998-09-18 14:14:13 +0000776static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000777np_void_p(char *p, PyObject *v, const formatdef *f)
Guido van Rossum78694d91998-09-18 14:14:13 +0000778{
779 void *x = PyLong_AsVoidPtr(v);
780 if (x == NULL && PyErr_Occurred()) {
781 /* ### hrm. PyLong_AsVoidPtr raises SystemError */
782 if (PyErr_ExceptionMatches(PyExc_TypeError))
783 PyErr_SetString(StructError,
784 "required argument is not an integer");
785 return -1;
786 }
Guido van Rossum960bc542002-09-03 18:42:21 +0000787 memcpy(p, (char *)&x, sizeof x);
Guido van Rossum78694d91998-09-18 14:14:13 +0000788 return 0;
789}
790
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000791static formatdef native_table[] = {
792 {'x', sizeof(char), 0, NULL},
793 {'b', sizeof(char), 0, nu_byte, np_byte},
Martin v. Löwis66de5492000-09-15 07:31:57 +0000794 {'B', sizeof(char), 0, nu_ubyte, np_ubyte},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000795 {'c', sizeof(char), 0, nu_char, np_char},
796 {'s', sizeof(char), 0, NULL},
Guido van Rossum9eb671f1997-09-05 07:08:39 +0000797 {'p', sizeof(char), 0, NULL},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000798 {'h', sizeof(short), SHORT_ALIGN, nu_short, np_short},
Martin v. Löwis66de5492000-09-15 07:31:57 +0000799 {'H', sizeof(short), SHORT_ALIGN, nu_ushort, np_ushort},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000800 {'i', sizeof(int), INT_ALIGN, nu_int, np_int},
Guido van Rossum60c50611996-12-31 16:29:52 +0000801 {'I', sizeof(int), INT_ALIGN, nu_uint, np_uint},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000802 {'l', sizeof(long), LONG_ALIGN, nu_long, np_long},
Guido van Rossum60c50611996-12-31 16:29:52 +0000803 {'L', sizeof(long), LONG_ALIGN, nu_ulong, np_ulong},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000804 {'f', sizeof(float), FLOAT_ALIGN, nu_float, np_float},
805 {'d', sizeof(double), DOUBLE_ALIGN, nu_double, np_double},
Guido van Rossum78694d91998-09-18 14:14:13 +0000806 {'P', sizeof(void *), VOID_P_ALIGN, nu_void_p, np_void_p},
Tim Peters7b9542a2001-06-10 23:40:19 +0000807#ifdef HAVE_LONG_LONG
808 {'q', sizeof(LONG_LONG), LONG_LONG_ALIGN, nu_longlong, np_longlong},
809 {'Q', sizeof(LONG_LONG), LONG_LONG_ALIGN, nu_ulonglong,np_ulonglong},
Tim Peters7b9542a2001-06-10 23:40:19 +0000810#endif
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000811 {0}
812};
813
Tim Peters7a3bfc32001-06-12 01:22:22 +0000814/* Big-endian routines. *****************************************************/
815
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000816static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000817bu_int(const char *p, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000818{
819 long x = 0;
820 int i = f->size;
821 do {
822 x = (x<<8) | (*p++ & 0xFF);
823 } while (--i > 0);
Tim Petersf0e717b2001-04-08 23:39:38 +0000824 /* Extend the sign bit. */
825 if (SIZEOF_LONG > f->size)
826 x |= -(x & (1L << (8*f->size - 1)));
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000827 return PyInt_FromLong(x);
828}
829
830static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000831bu_uint(const char *p, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000832{
Guido van Rossum6c87eca1997-01-03 19:08:16 +0000833 unsigned long x = 0;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000834 int i = f->size;
835 do {
836 x = (x<<8) | (*p++ & 0xFF);
837 } while (--i > 0);
Guido van Rossum39ef2271998-06-29 04:00:40 +0000838 if (f->size >= 4)
839 return PyLong_FromUnsignedLong(x);
840 else
841 return PyInt_FromLong((long)x);
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000842}
843
Guido van Rossum74679b41997-01-02 22:21:36 +0000844static PyObject *
Tim Peters7a3bfc32001-06-12 01:22:22 +0000845bu_longlong(const char *p, const formatdef *f)
846{
847 return _PyLong_FromByteArray((const unsigned char *)p,
848 8,
849 0, /* little-endian */
850 1 /* signed */);
851}
852
853static PyObject *
854bu_ulonglong(const char *p, const formatdef *f)
855{
856 return _PyLong_FromByteArray((const unsigned char *)p,
857 8,
858 0, /* little-endian */
859 0 /* signed */);
860}
861
862static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000863bu_float(const char *p, const formatdef *f)
Guido van Rossum74679b41997-01-02 22:21:36 +0000864{
865 return unpack_float(p, 1);
866}
867
868static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000869bu_double(const char *p, const formatdef *f)
Guido van Rossum74679b41997-01-02 22:21:36 +0000870{
871 return unpack_double(p, 1);
872}
873
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000874static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000875bp_int(char *p, PyObject *v, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000876{
877 long x;
878 int i;
879 if (get_long(v, &x) < 0)
880 return -1;
881 i = f->size;
882 do {
Guido van Rossum7844e381997-04-11 20:44:04 +0000883 p[--i] = (char)x;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000884 x >>= 8;
885 } while (i > 0);
886 return 0;
887}
888
Guido van Rossum60c50611996-12-31 16:29:52 +0000889static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000890bp_uint(char *p, PyObject *v, const formatdef *f)
Guido van Rossum60c50611996-12-31 16:29:52 +0000891{
892 unsigned long x;
893 int i;
894 if (get_ulong(v, &x) < 0)
895 return -1;
896 i = f->size;
897 do {
Guido van Rossum7844e381997-04-11 20:44:04 +0000898 p[--i] = (char)x;
Guido van Rossum60c50611996-12-31 16:29:52 +0000899 x >>= 8;
900 } while (i > 0);
901 return 0;
902}
903
Guido van Rossum74679b41997-01-02 22:21:36 +0000904static int
Tim Peters7a3bfc32001-06-12 01:22:22 +0000905bp_longlong(char *p, PyObject *v, const formatdef *f)
906{
907 int res;
908 v = get_pylong(v);
Tim Petersda9c5b32001-06-13 01:26:35 +0000909 if (v == NULL)
910 return -1;
Tim Peters7a3bfc32001-06-12 01:22:22 +0000911 res = _PyLong_AsByteArray((PyLongObject *)v,
912 (unsigned char *)p,
913 8,
914 0, /* little_endian */
915 1 /* signed */);
916 Py_DECREF(v);
917 return res;
918}
919
920static int
921bp_ulonglong(char *p, PyObject *v, const formatdef *f)
922{
923 int res;
924 v = get_pylong(v);
Tim Petersda9c5b32001-06-13 01:26:35 +0000925 if (v == NULL)
926 return -1;
Tim Peters7a3bfc32001-06-12 01:22:22 +0000927 res = _PyLong_AsByteArray((PyLongObject *)v,
928 (unsigned char *)p,
929 8,
930 0, /* little_endian */
931 0 /* signed */);
932 Py_DECREF(v);
933 return res;
934}
935
936static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000937bp_float(char *p, PyObject *v, const formatdef *f)
Guido van Rossum74679b41997-01-02 22:21:36 +0000938{
939 double x = PyFloat_AsDouble(v);
940 if (x == -1 && PyErr_Occurred()) {
941 PyErr_SetString(StructError,
942 "required argument is not a float");
943 return -1;
944 }
945 return pack_float(x, p, 1);
946}
947
948static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000949bp_double(char *p, PyObject *v, const formatdef *f)
Guido van Rossum74679b41997-01-02 22:21:36 +0000950{
951 double x = PyFloat_AsDouble(v);
952 if (x == -1 && PyErr_Occurred()) {
953 PyErr_SetString(StructError,
954 "required argument is not a float");
955 return -1;
956 }
957 return pack_double(x, p, 1);
958}
959
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000960static formatdef bigendian_table[] = {
961 {'x', 1, 0, NULL},
962 {'b', 1, 0, bu_int, bp_int},
963 {'B', 1, 0, bu_uint, bp_int},
964 {'c', 1, 0, nu_char, np_char},
965 {'s', 1, 0, NULL},
Guido van Rossum9eb671f1997-09-05 07:08:39 +0000966 {'p', 1, 0, NULL},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000967 {'h', 2, 0, bu_int, bp_int},
Guido van Rossum60c50611996-12-31 16:29:52 +0000968 {'H', 2, 0, bu_uint, bp_uint},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000969 {'i', 4, 0, bu_int, bp_int},
Guido van Rossum60c50611996-12-31 16:29:52 +0000970 {'I', 4, 0, bu_uint, bp_uint},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000971 {'l', 4, 0, bu_int, bp_int},
Guido van Rossum60c50611996-12-31 16:29:52 +0000972 {'L', 4, 0, bu_uint, bp_uint},
Tim Peters7a3bfc32001-06-12 01:22:22 +0000973 {'q', 8, 0, bu_longlong, bp_longlong},
974 {'Q', 8, 0, bu_ulonglong, bp_ulonglong},
Guido van Rossum74679b41997-01-02 22:21:36 +0000975 {'f', 4, 0, bu_float, bp_float},
976 {'d', 8, 0, bu_double, bp_double},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000977 {0}
978};
979
Tim Peters7a3bfc32001-06-12 01:22:22 +0000980/* Little-endian routines. *****************************************************/
981
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000982static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000983lu_int(const char *p, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000984{
985 long x = 0;
986 int i = f->size;
987 do {
988 x = (x<<8) | (p[--i] & 0xFF);
989 } while (i > 0);
Tim Petersf0e717b2001-04-08 23:39:38 +0000990 /* Extend the sign bit. */
991 if (SIZEOF_LONG > f->size)
992 x |= -(x & (1L << (8*f->size - 1)));
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000993 return PyInt_FromLong(x);
994}
995
996static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000997lu_uint(const char *p, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000998{
Guido van Rossum6c87eca1997-01-03 19:08:16 +0000999 unsigned long x = 0;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001000 int i = f->size;
1001 do {
1002 x = (x<<8) | (p[--i] & 0xFF);
1003 } while (i > 0);
Guido van Rossum39ef2271998-06-29 04:00:40 +00001004 if (f->size >= 4)
1005 return PyLong_FromUnsignedLong(x);
1006 else
1007 return PyInt_FromLong((long)x);
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001008}
1009
Guido van Rossum74679b41997-01-02 22:21:36 +00001010static PyObject *
Tim Peters7a3bfc32001-06-12 01:22:22 +00001011lu_longlong(const char *p, const formatdef *f)
1012{
1013 return _PyLong_FromByteArray((const unsigned char *)p,
1014 8,
1015 1, /* little-endian */
1016 1 /* signed */);
1017}
1018
1019static PyObject *
1020lu_ulonglong(const char *p, const formatdef *f)
1021{
1022 return _PyLong_FromByteArray((const unsigned char *)p,
1023 8,
1024 1, /* little-endian */
1025 0 /* signed */);
1026}
1027
1028static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +00001029lu_float(const char *p, const formatdef *f)
Guido van Rossum74679b41997-01-02 22:21:36 +00001030{
1031 return unpack_float(p+3, -1);
1032}
1033
1034static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +00001035lu_double(const char *p, const formatdef *f)
Guido van Rossum74679b41997-01-02 22:21:36 +00001036{
1037 return unpack_double(p+7, -1);
1038}
1039
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001040static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +00001041lp_int(char *p, PyObject *v, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001042{
1043 long x;
1044 int i;
1045 if (get_long(v, &x) < 0)
1046 return -1;
1047 i = f->size;
1048 do {
Guido van Rossum7844e381997-04-11 20:44:04 +00001049 *p++ = (char)x;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001050 x >>= 8;
1051 } while (--i > 0);
1052 return 0;
1053}
1054
Guido van Rossum60c50611996-12-31 16:29:52 +00001055static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +00001056lp_uint(char *p, PyObject *v, const formatdef *f)
Guido van Rossum60c50611996-12-31 16:29:52 +00001057{
1058 unsigned long x;
1059 int i;
1060 if (get_ulong(v, &x) < 0)
1061 return -1;
1062 i = f->size;
1063 do {
Guido van Rossum7844e381997-04-11 20:44:04 +00001064 *p++ = (char)x;
Guido van Rossum60c50611996-12-31 16:29:52 +00001065 x >>= 8;
1066 } while (--i > 0);
1067 return 0;
1068}
1069
Guido van Rossum74679b41997-01-02 22:21:36 +00001070static int
Tim Peters7a3bfc32001-06-12 01:22:22 +00001071lp_longlong(char *p, PyObject *v, const formatdef *f)
1072{
1073 int res;
1074 v = get_pylong(v);
Tim Petersda9c5b32001-06-13 01:26:35 +00001075 if (v == NULL)
1076 return -1;
Tim Peters7a3bfc32001-06-12 01:22:22 +00001077 res = _PyLong_AsByteArray((PyLongObject*)v,
1078 (unsigned char *)p,
1079 8,
1080 1, /* little_endian */
1081 1 /* signed */);
1082 Py_DECREF(v);
1083 return res;
1084}
1085
1086static int
1087lp_ulonglong(char *p, PyObject *v, const formatdef *f)
1088{
1089 int res;
1090 v = get_pylong(v);
Tim Petersda9c5b32001-06-13 01:26:35 +00001091 if (v == NULL)
1092 return -1;
Tim Peters7a3bfc32001-06-12 01:22:22 +00001093 res = _PyLong_AsByteArray((PyLongObject*)v,
1094 (unsigned char *)p,
1095 8,
1096 1, /* little_endian */
1097 0 /* signed */);
1098 Py_DECREF(v);
1099 return res;
1100}
1101
1102static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +00001103lp_float(char *p, PyObject *v, const formatdef *f)
Guido van Rossum74679b41997-01-02 22:21:36 +00001104{
1105 double x = PyFloat_AsDouble(v);
1106 if (x == -1 && PyErr_Occurred()) {
1107 PyErr_SetString(StructError,
1108 "required argument is not a float");
1109 return -1;
1110 }
1111 return pack_float(x, p+3, -1);
1112}
1113
1114static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +00001115lp_double(char *p, PyObject *v, const formatdef *f)
Guido van Rossum74679b41997-01-02 22:21:36 +00001116{
1117 double x = PyFloat_AsDouble(v);
1118 if (x == -1 && PyErr_Occurred()) {
1119 PyErr_SetString(StructError,
1120 "required argument is not a float");
1121 return -1;
1122 }
1123 return pack_double(x, p+7, -1);
1124}
1125
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001126static formatdef lilendian_table[] = {
1127 {'x', 1, 0, NULL},
1128 {'b', 1, 0, lu_int, lp_int},
1129 {'B', 1, 0, lu_uint, lp_int},
1130 {'c', 1, 0, nu_char, np_char},
1131 {'s', 1, 0, NULL},
Guido van Rossum9eb671f1997-09-05 07:08:39 +00001132 {'p', 1, 0, NULL},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001133 {'h', 2, 0, lu_int, lp_int},
Guido van Rossum60c50611996-12-31 16:29:52 +00001134 {'H', 2, 0, lu_uint, lp_uint},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001135 {'i', 4, 0, lu_int, lp_int},
Guido van Rossum60c50611996-12-31 16:29:52 +00001136 {'I', 4, 0, lu_uint, lp_uint},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001137 {'l', 4, 0, lu_int, lp_int},
Guido van Rossum60c50611996-12-31 16:29:52 +00001138 {'L', 4, 0, lu_uint, lp_uint},
Tim Peters7a3bfc32001-06-12 01:22:22 +00001139 {'q', 8, 0, lu_longlong, lp_longlong},
1140 {'Q', 8, 0, lu_ulonglong, lp_ulonglong},
Guido van Rossum74679b41997-01-02 22:21:36 +00001141 {'f', 4, 0, lu_float, lp_float},
1142 {'d', 8, 0, lu_double, lp_double},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001143 {0}
1144};
1145
1146
1147static const formatdef *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +00001148whichtable(char **pfmt)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001149{
1150 const char *fmt = (*pfmt)++; /* May be backed out of later */
1151 switch (*fmt) {
1152 case '<':
1153 return lilendian_table;
1154 case '>':
1155 case '!': /* Network byte order is big-endian */
1156 return bigendian_table;
1157 case '=': { /* Host byte order -- different from native in aligment! */
1158 int n = 1;
1159 char *p = (char *) &n;
1160 if (*p == 1)
1161 return lilendian_table;
1162 else
1163 return bigendian_table;
1164 }
1165 default:
1166 --*pfmt; /* Back out of pointer increment */
1167 /* Fall through */
1168 case '@':
1169 return native_table;
1170 }
1171}
1172
1173
1174/* Get the table entry for a format code */
1175
1176static const formatdef *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +00001177getentry(int c, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001178{
1179 for (; f->format != '\0'; f++) {
1180 if (f->format == c) {
1181 return f;
1182 }
1183 }
1184 PyErr_SetString(StructError, "bad char in struct format");
1185 return NULL;
1186}
1187
1188
Guido van Rossum02975121992-08-17 08:55:12 +00001189/* Align a size according to a format code */
1190
1191static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +00001192align(int size, int c, const formatdef *e)
Guido van Rossum02975121992-08-17 08:55:12 +00001193{
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001194 if (e->format == c) {
1195 if (e->alignment) {
1196 size = ((size + e->alignment - 1)
1197 / e->alignment)
1198 * e->alignment;
1199 }
Guido van Rossum02975121992-08-17 08:55:12 +00001200 }
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001201 return size;
Guido van Rossum02975121992-08-17 08:55:12 +00001202}
1203
1204
1205/* calculate the size of a format string */
1206
1207static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +00001208calcsize(const char *fmt, const formatdef *f)
Guido van Rossum02975121992-08-17 08:55:12 +00001209{
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001210 const formatdef *e;
1211 const char *s;
Guido van Rossum02975121992-08-17 08:55:12 +00001212 char c;
1213 int size, num, itemsize, x;
1214
1215 s = fmt;
1216 size = 0;
1217 while ((c = *s++) != '\0') {
Guido van Rossum730806d1998-04-10 22:27:42 +00001218 if (isspace((int)c))
Guido van Rossume20aef51997-08-26 20:39:54 +00001219 continue;
Guido van Rossum02975121992-08-17 08:55:12 +00001220 if ('0' <= c && c <= '9') {
1221 num = c - '0';
1222 while ('0' <= (c = *s++) && c <= '9') {
1223 x = num*10 + (c - '0');
1224 if (x/10 != num) {
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001225 PyErr_SetString(
1226 StructError,
1227 "overflow in item count");
Guido van Rossum02975121992-08-17 08:55:12 +00001228 return -1;
1229 }
1230 num = x;
1231 }
1232 if (c == '\0')
1233 break;
1234 }
1235 else
1236 num = 1;
Tim Peters2d4e0772001-06-11 16:57:33 +00001237
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001238 e = getentry(c, f);
1239 if (e == NULL)
Guido van Rossum02975121992-08-17 08:55:12 +00001240 return -1;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001241 itemsize = e->size;
1242 size = align(size, c, e);
Guido van Rossum02975121992-08-17 08:55:12 +00001243 x = num * itemsize;
1244 size += x;
1245 if (x/itemsize != num || size < 0) {
Barry Warsaw30695fa1996-12-12 23:32:31 +00001246 PyErr_SetString(StructError,
Guido van Rossum960bc542002-09-03 18:42:21 +00001247 "total struct size too long");
Guido van Rossum02975121992-08-17 08:55:12 +00001248 return -1;
1249 }
Guido van Rossum02975121992-08-17 08:55:12 +00001250 }
1251
1252 return size;
1253}
1254
1255
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001256PyDoc_STRVAR(calcsize__doc__,
1257"calcsize(fmt) -> int\n\
Guido van Rossum414fd481997-12-19 04:24:24 +00001258Return size of C struct described by format string fmt.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001259See struct.__doc__ for more on format strings.");
Guido van Rossum02975121992-08-17 08:55:12 +00001260
Barry Warsaw30695fa1996-12-12 23:32:31 +00001261static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +00001262struct_calcsize(PyObject *self, PyObject *args)
Guido van Rossum02975121992-08-17 08:55:12 +00001263{
1264 char *fmt;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001265 const formatdef *f;
Guido van Rossum02975121992-08-17 08:55:12 +00001266 int size;
1267
Guido van Rossum43713e52000-02-29 13:59:29 +00001268 if (!PyArg_ParseTuple(args, "s:calcsize", &fmt))
Guido van Rossum02975121992-08-17 08:55:12 +00001269 return NULL;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001270 f = whichtable(&fmt);
1271 size = calcsize(fmt, f);
Guido van Rossum02975121992-08-17 08:55:12 +00001272 if (size < 0)
1273 return NULL;
Barry Warsaw30695fa1996-12-12 23:32:31 +00001274 return PyInt_FromLong((long)size);
Guido van Rossum02975121992-08-17 08:55:12 +00001275}
1276
1277
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001278PyDoc_STRVAR(pack__doc__,
1279"pack(fmt, v1, v2, ...) -> string\n\
Guido van Rossum414fd481997-12-19 04:24:24 +00001280Return string containing values v1, v2, ... packed according to fmt.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001281See struct.__doc__ for more on format strings.");
Guido van Rossum02975121992-08-17 08:55:12 +00001282
Barry Warsaw30695fa1996-12-12 23:32:31 +00001283static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +00001284struct_pack(PyObject *self, PyObject *args)
Guido van Rossum02975121992-08-17 08:55:12 +00001285{
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001286 const formatdef *f, *e;
Barry Warsaw30695fa1996-12-12 23:32:31 +00001287 PyObject *format, *result, *v;
Guido van Rossum02975121992-08-17 08:55:12 +00001288 char *fmt;
1289 int size, num;
1290 int i, n;
Guido van Rossumb9d338c1997-01-03 15:40:33 +00001291 char *s, *res, *restart, *nres;
Guido van Rossum02975121992-08-17 08:55:12 +00001292 char c;
Guido van Rossum02975121992-08-17 08:55:12 +00001293
Barry Warsaw30695fa1996-12-12 23:32:31 +00001294 if (args == NULL || !PyTuple_Check(args) ||
1295 (n = PyTuple_Size(args)) < 1)
Guido van Rossum960bc542002-09-03 18:42:21 +00001296 {
Tim Peters2d4e0772001-06-11 16:57:33 +00001297 PyErr_SetString(PyExc_TypeError,
Fred Drake137507e2000-06-01 02:02:46 +00001298 "struct.pack requires at least one argument");
Guido van Rossum02975121992-08-17 08:55:12 +00001299 return NULL;
1300 }
Barry Warsaw30695fa1996-12-12 23:32:31 +00001301 format = PyTuple_GetItem(args, 0);
Neal Norwitz187ae562002-04-02 18:17:57 +00001302 fmt = PyString_AsString(format);
1303 if (!fmt)
Guido van Rossum02975121992-08-17 08:55:12 +00001304 return NULL;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001305 f = whichtable(&fmt);
1306 size = calcsize(fmt, f);
Guido van Rossum02975121992-08-17 08:55:12 +00001307 if (size < 0)
1308 return NULL;
Barry Warsaw30695fa1996-12-12 23:32:31 +00001309 result = PyString_FromStringAndSize((char *)NULL, size);
Guido van Rossum02975121992-08-17 08:55:12 +00001310 if (result == NULL)
1311 return NULL;
1312
1313 s = fmt;
1314 i = 1;
Barry Warsaw30695fa1996-12-12 23:32:31 +00001315 res = restart = PyString_AsString(result);
Guido van Rossum02975121992-08-17 08:55:12 +00001316
1317 while ((c = *s++) != '\0') {
Guido van Rossum730806d1998-04-10 22:27:42 +00001318 if (isspace((int)c))
Guido van Rossume20aef51997-08-26 20:39:54 +00001319 continue;
Guido van Rossum02975121992-08-17 08:55:12 +00001320 if ('0' <= c && c <= '9') {
1321 num = c - '0';
1322 while ('0' <= (c = *s++) && c <= '9')
1323 num = num*10 + (c - '0');
1324 if (c == '\0')
1325 break;
1326 }
1327 else
1328 num = 1;
1329
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001330 e = getentry(c, f);
1331 if (e == NULL)
1332 goto fail;
Guido van Rossumb9d338c1997-01-03 15:40:33 +00001333 nres = restart + align((int)(res-restart), c, e);
1334 /* Fill padd bytes with zeros */
1335 while (res < nres)
1336 *res++ = '\0';
Guido van Rossum3aa27fd1996-12-31 02:10:45 +00001337 if (num == 0 && c != 's')
1338 continue;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001339 do {
1340 if (c == 'x') {
1341 /* doesn't consume arguments */
Guido van Rossum3aa27fd1996-12-31 02:10:45 +00001342 memset(res, '\0', num);
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001343 res += num;
Guido van Rossum02975121992-08-17 08:55:12 +00001344 break;
Guido van Rossum02975121992-08-17 08:55:12 +00001345 }
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001346 if (i >= n) {
1347 PyErr_SetString(StructError,
1348 "insufficient arguments to pack");
1349 goto fail;
1350 }
1351 v = PyTuple_GetItem(args, i++);
1352 if (v == NULL)
1353 goto fail;
1354 if (c == 's') {
1355 /* num is string size, not repeat count */
1356 int n;
1357 if (!PyString_Check(v)) {
1358 PyErr_SetString(StructError,
1359 "argument for 's' must be a string");
1360 goto fail;
1361 }
1362 n = PyString_Size(v);
1363 if (n > num)
1364 n = num;
1365 if (n > 0)
1366 memcpy(res, PyString_AsString(v), n);
1367 if (n < num)
Guido van Rossum3aa27fd1996-12-31 02:10:45 +00001368 memset(res+n, '\0', num-n);
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001369 res += num;
1370 break;
1371 }
Guido van Rossum9eb671f1997-09-05 07:08:39 +00001372 else if (c == 'p') {
1373 /* num is string size + 1,
1374 to fit in the count byte */
1375 int n;
1376 num--; /* now num is max string size */
1377 if (!PyString_Check(v)) {
1378 PyErr_SetString(StructError,
1379 "argument for 'p' must be a string");
1380 goto fail;
1381 }
1382 n = PyString_Size(v);
1383 if (n > num)
1384 n = num;
1385 if (n > 0)
1386 memcpy(res+1, PyString_AsString(v), n);
1387 if (n < num)
1388 /* no real need, just to be nice */
1389 memset(res+1+n, '\0', num-n);
Tim Peters0891ac02001-09-15 02:35:15 +00001390 if (n > 255)
1391 n = 255;
Guido van Rossum9eb671f1997-09-05 07:08:39 +00001392 *res++ = n; /* store the length byte */
1393 res += num;
1394 break;
1395 }
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001396 else {
1397 if (e->pack(res, v, e) < 0)
1398 goto fail;
1399 res += e->size;
1400 }
1401 } while (--num > 0);
Guido van Rossum02975121992-08-17 08:55:12 +00001402 }
1403
1404 if (i < n) {
Barry Warsaw30695fa1996-12-12 23:32:31 +00001405 PyErr_SetString(StructError,
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001406 "too many arguments for pack format");
Guido van Rossum02975121992-08-17 08:55:12 +00001407 goto fail;
1408 }
1409
1410 return result;
1411
1412 fail:
Barry Warsaw30695fa1996-12-12 23:32:31 +00001413 Py_DECREF(result);
Guido van Rossum02975121992-08-17 08:55:12 +00001414 return NULL;
1415}
1416
1417
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001418PyDoc_STRVAR(unpack__doc__,
1419"unpack(fmt, string) -> (v1, v2, ...)\n\
Guido van Rossum9897f0f1997-12-21 06:46:20 +00001420Unpack the string, containing packed C structure data, according\n\
1421to fmt. Requires len(string)==calcsize(fmt).\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001422See struct.__doc__ for more on format strings.");
Guido van Rossum02975121992-08-17 08:55:12 +00001423
Barry Warsaw30695fa1996-12-12 23:32:31 +00001424static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +00001425struct_unpack(PyObject *self, PyObject *args)
Guido van Rossum02975121992-08-17 08:55:12 +00001426{
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001427 const formatdef *f, *e;
Guido van Rossum02975121992-08-17 08:55:12 +00001428 char *str, *start, *fmt, *s;
1429 char c;
Barry Warsawb9a781e1997-01-03 00:26:28 +00001430 int len, size, num;
Barry Warsaw30695fa1996-12-12 23:32:31 +00001431 PyObject *res, *v;
Guido van Rossum02975121992-08-17 08:55:12 +00001432
Guido van Rossum43713e52000-02-29 13:59:29 +00001433 if (!PyArg_ParseTuple(args, "ss#:unpack", &fmt, &start, &len))
Guido van Rossum02975121992-08-17 08:55:12 +00001434 return NULL;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001435 f = whichtable(&fmt);
1436 size = calcsize(fmt, f);
1437 if (size < 0)
1438 return NULL;
Guido van Rossum02975121992-08-17 08:55:12 +00001439 if (size != len) {
Barry Warsaw30695fa1996-12-12 23:32:31 +00001440 PyErr_SetString(StructError,
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001441 "unpack str size does not match format");
Guido van Rossum02975121992-08-17 08:55:12 +00001442 return NULL;
1443 }
Barry Warsaw30695fa1996-12-12 23:32:31 +00001444 res = PyList_New(0);
Guido van Rossum02975121992-08-17 08:55:12 +00001445 if (res == NULL)
1446 return NULL;
1447 str = start;
1448 s = fmt;
1449 while ((c = *s++) != '\0') {
Guido van Rossum730806d1998-04-10 22:27:42 +00001450 if (isspace((int)c))
Guido van Rossume20aef51997-08-26 20:39:54 +00001451 continue;
Guido van Rossum02975121992-08-17 08:55:12 +00001452 if ('0' <= c && c <= '9') {
1453 num = c - '0';
1454 while ('0' <= (c = *s++) && c <= '9')
1455 num = num*10 + (c - '0');
1456 if (c == '\0')
1457 break;
1458 }
1459 else
1460 num = 1;
1461
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001462 e = getentry(c, f);
1463 if (e == NULL)
1464 goto fail;
1465 str = start + align((int)(str-start), c, e);
Guido van Rossum3aa27fd1996-12-31 02:10:45 +00001466 if (num == 0 && c != 's')
1467 continue;
Guido van Rossum02975121992-08-17 08:55:12 +00001468
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001469 do {
1470 if (c == 'x') {
1471 str += num;
Guido van Rossum02975121992-08-17 08:55:12 +00001472 break;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001473 }
1474 if (c == 's') {
1475 /* num is string size, not repeat count */
1476 v = PyString_FromStringAndSize(str, num);
1477 if (v == NULL)
1478 goto fail;
1479 str += num;
1480 num = 0;
1481 }
Guido van Rossum9eb671f1997-09-05 07:08:39 +00001482 else if (c == 'p') {
1483 /* num is string buffer size,
1484 not repeat count */
1485 int n = *(unsigned char*)str;
1486 /* first byte (unsigned) is string size */
1487 if (n >= num)
1488 n = num-1;
1489 v = PyString_FromStringAndSize(str+1, n);
1490 if (v == NULL)
1491 goto fail;
1492 str += num;
1493 num = 0;
1494 }
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001495 else {
1496 v = e->unpack(str, e);
1497 if (v == NULL)
1498 goto fail;
1499 str += e->size;
Guido van Rossum02975121992-08-17 08:55:12 +00001500 }
Barry Warsaw30695fa1996-12-12 23:32:31 +00001501 if (v == NULL || PyList_Append(res, v) < 0)
Guido van Rossum02975121992-08-17 08:55:12 +00001502 goto fail;
Barry Warsaw30695fa1996-12-12 23:32:31 +00001503 Py_DECREF(v);
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001504 } while (--num > 0);
Guido van Rossum02975121992-08-17 08:55:12 +00001505 }
1506
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001507 v = PyList_AsTuple(res);
1508 Py_DECREF(res);
1509 return v;
Guido van Rossum02975121992-08-17 08:55:12 +00001510
1511 fail:
Barry Warsaw30695fa1996-12-12 23:32:31 +00001512 Py_DECREF(res);
Guido van Rossum02975121992-08-17 08:55:12 +00001513 return NULL;
1514}
1515
Guido van Rossum90ddb7b1992-08-19 16:44:15 +00001516
Guido van Rossum02975121992-08-17 08:55:12 +00001517/* List of functions */
1518
Barry Warsaw30695fa1996-12-12 23:32:31 +00001519static PyMethodDef struct_methods[] = {
Guido van Rossum414fd481997-12-19 04:24:24 +00001520 {"calcsize", struct_calcsize, METH_VARARGS, calcsize__doc__},
1521 {"pack", struct_pack, METH_VARARGS, pack__doc__},
1522 {"unpack", struct_unpack, METH_VARARGS, unpack__doc__},
Guido van Rossum02975121992-08-17 08:55:12 +00001523 {NULL, NULL} /* sentinel */
1524};
1525
1526
1527/* Module initialization */
1528
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001529PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001530initstruct(void)
Guido van Rossum02975121992-08-17 08:55:12 +00001531{
Fred Drake78f6c862002-02-14 07:11:23 +00001532 PyObject *m;
Guido van Rossum02975121992-08-17 08:55:12 +00001533
1534 /* Create the module and add the functions */
Guido van Rossum414fd481997-12-19 04:24:24 +00001535 m = Py_InitModule4("struct", struct_methods, struct__doc__,
1536 (PyObject*)NULL, PYTHON_API_VERSION);
Guido van Rossum02975121992-08-17 08:55:12 +00001537
1538 /* Add some symbolic constants to the module */
Fred Drake78f6c862002-02-14 07:11:23 +00001539 if (StructError == NULL) {
1540 StructError = PyErr_NewException("struct.error", NULL, NULL);
1541 if (StructError == NULL)
1542 return;
1543 }
1544 Py_INCREF(StructError);
Fred Drake2eeec9b2002-02-14 07:16:30 +00001545 PyModule_AddObject(m, "error", StructError);
Guido van Rossum02975121992-08-17 08:55:12 +00001546}