Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 2 | Copyright (c) 2000, BeOpen.com. |
| 3 | Copyright (c) 1995-2000, Corporation for National Research Initiatives. |
| 4 | Copyright (c) 1990-1995, Stichting Mathematisch Centrum. |
| 5 | All rights reserved. |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 7 | See the file "Misc/COPYRIGHT" for information on usage and |
| 8 | redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 9 | ******************************************************************/ |
| 10 | |
| 11 | /* struct module -- pack values into and (out of) strings */ |
| 12 | |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 13 | /* New version supporting byte order, alignment and size options, |
| 14 | character strings, and unsigned numbers */ |
| 15 | |
Guido van Rossum | 414fd48 | 1997-12-19 04:24:24 +0000 | [diff] [blame] | 16 | static char struct__doc__[] = "\ |
| 17 | Functions to convert between Python values and C structs.\n\ |
| 18 | Python strings are used to hold the data representing the C struct\n\ |
| 19 | and also as format strings to describe the layout of data in the C struct.\n\ |
| 20 | \n\ |
| 21 | The 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\ |
| 28 | The remaining chars indicate types of args and must match exactly;\n\ |
| 29 | these 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\ |
| 33 | Special cases (preceding decimal count indicates length):\n\ |
| 34 | s:string (array of char); p: pascal string (w. count byte).\n\ |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 35 | Special case (only available in native format):\n\ |
| 36 | P:an integer type that is wide enough to hold a pointer.\n\ |
Guido van Rossum | 414fd48 | 1997-12-19 04:24:24 +0000 | [diff] [blame] | 37 | Whitespace between formats is ignored.\n\ |
| 38 | \n\ |
| 39 | The variable struct.error is an exception raised on errors."; |
| 40 | |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 41 | #include "Python.h" |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 42 | #include "mymath.h" |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 43 | |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 44 | #include <limits.h> |
Guido van Rossum | e20aef5 | 1997-08-26 20:39:54 +0000 | [diff] [blame] | 45 | #include <ctype.h> |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 46 | |
| 47 | |
| 48 | /* Exception */ |
| 49 | |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 50 | static PyObject *StructError; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 51 | |
| 52 | |
| 53 | /* Define various structs to figure out the alignments of types */ |
| 54 | |
Jack Jansen | 971e1df | 1995-02-02 14:29:10 +0000 | [diff] [blame] | 55 | #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 Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 65 | typedef struct { char c; short x; } s_short; |
| 66 | typedef struct { char c; int x; } s_int; |
| 67 | typedef struct { char c; long x; } s_long; |
| 68 | typedef struct { char c; float x; } s_float; |
| 69 | typedef struct { char c; double x; } s_double; |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 70 | typedef struct { char c; void *x; } s_void_p; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 71 | |
| 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 Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 77 | #define VOID_P_ALIGN (sizeof(s_void_p) - sizeof(void *)) |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 78 | |
Jack Jansen | 971e1df | 1995-02-02 14:29:10 +0000 | [diff] [blame] | 79 | #ifdef __powerc |
| 80 | #pragma options align=reset |
| 81 | #endif |
| 82 | |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 83 | /* Helper routine to get a Python integer and raise the appropriate error |
| 84 | if it isn't one */ |
| 85 | |
| 86 | static int |
| 87 | get_long(v, p) |
| 88 | PyObject *v; |
| 89 | long *p; |
| 90 | { |
| 91 | long x = PyInt_AsLong(v); |
| 92 | if (x == -1 && PyErr_Occurred()) { |
Fred Drake | d3dbb38 | 1998-05-28 04:35:49 +0000 | [diff] [blame] | 93 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 94 | 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 Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame] | 103 | /* Same, but handling unsigned long */ |
| 104 | |
| 105 | static int |
| 106 | get_ulong(v, p) |
| 107 | PyObject *v; |
| 108 | unsigned long *p; |
| 109 | { |
Guido van Rossum | 6c87eca | 1997-01-03 19:08:16 +0000 | [diff] [blame] | 110 | if (PyLong_Check(v)) { |
| 111 | unsigned long x = PyLong_AsUnsignedLong(v); |
| 112 | if (x == (unsigned long)(-1) && PyErr_Occurred()) |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame] | 113 | return -1; |
Guido van Rossum | 6c87eca | 1997-01-03 19:08:16 +0000 | [diff] [blame] | 114 | *p = x; |
| 115 | return 0; |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame] | 116 | } |
Guido van Rossum | 6c87eca | 1997-01-03 19:08:16 +0000 | [diff] [blame] | 117 | else { |
| 118 | return get_long(v, (long *)p); |
| 119 | } |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 123 | /* 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 Rossum | 4ccc531 | 1997-01-02 23:23:20 +0000 | [diff] [blame] | 129 | /* XXX Inf/NaN are not handled quite right (but underflow is!) */ |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 130 | |
| 131 | static int |
| 132 | pack_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 Rossum | 4ccc531 | 1997-01-02 23:23:20 +0000 | [diff] [blame] | 139 | double f; |
| 140 | long fbits; |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 141 | |
| 142 | if (x < 0) { |
| 143 | s = 1; |
| 144 | x = -x; |
| 145 | } |
| 146 | else |
| 147 | s = 0; |
Guido van Rossum | 4ccc531 | 1997-01-02 23:23:20 +0000 | [diff] [blame] | 148 | |
| 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 Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 154 | e--; |
| 155 | } |
Guido van Rossum | 4ccc531 | 1997-01-02 23:23:20 +0000 | [diff] [blame] | 156 | else if (f == 0.0) { |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 157 | e = 0; |
| 158 | } |
| 159 | else { |
| 160 | PyErr_SetString(PyExc_SystemError, |
| 161 | "frexp() result out of range"); |
| 162 | return -1; |
| 163 | } |
Guido van Rossum | 4ccc531 | 1997-01-02 23:23:20 +0000 | [diff] [blame] | 164 | |
| 165 | if (e >= 128) { |
| 166 | /* XXX 128 itself is reserved for Inf/NaN */ |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 167 | PyErr_SetString(PyExc_OverflowError, |
| 168 | "float too large to pack with f format"); |
| 169 | return -1; |
| 170 | } |
Guido van Rossum | 4ccc531 | 1997-01-02 23:23:20 +0000 | [diff] [blame] | 171 | else if (e < -126) { |
| 172 | /* Gradual underflow */ |
| 173 | f = ldexp(f, 126 + e); |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 174 | e = 0; |
| 175 | } |
Guido van Rossum | 8f3c812 | 1997-11-04 17:12:33 +0000 | [diff] [blame] | 176 | else if (!(e == 0 && f == 0.0)) { |
Guido van Rossum | 4ccc531 | 1997-01-02 23:23:20 +0000 | [diff] [blame] | 177 | e += 127; |
| 178 | f -= 1.0; /* Get rid of leading 1 */ |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Guido van Rossum | 4ccc531 | 1997-01-02 23:23:20 +0000 | [diff] [blame] | 181 | f *= 8388608.0; /* 2**23 */ |
| 182 | fbits = (long) floor(f + 0.5); /* Round */ |
| 183 | |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 184 | /* First byte */ |
| 185 | *p = (s<<7) | (e>>1); |
| 186 | p += incr; |
| 187 | |
| 188 | /* Second byte */ |
Guido van Rossum | 7844e38 | 1997-04-11 20:44:04 +0000 | [diff] [blame] | 189 | *p = (char) (((e&1)<<7) | (fbits>>16)); |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 190 | p += incr; |
| 191 | |
| 192 | /* Third byte */ |
Guido van Rossum | 4ccc531 | 1997-01-02 23:23:20 +0000 | [diff] [blame] | 193 | *p = (fbits>>8) & 0xFF; |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 194 | p += incr; |
| 195 | |
| 196 | /* Fourth byte */ |
Guido van Rossum | 4ccc531 | 1997-01-02 23:23:20 +0000 | [diff] [blame] | 197 | *p = fbits&0xFF; |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 198 | |
| 199 | /* Done */ |
| 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | static int |
| 204 | pack_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 Rossum | 4ccc531 | 1997-01-02 23:23:20 +0000 | [diff] [blame] | 211 | double f; |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 212 | long fhi, flo; |
| 213 | |
| 214 | if (x < 0) { |
| 215 | s = 1; |
| 216 | x = -x; |
| 217 | } |
| 218 | else |
| 219 | s = 0; |
Guido van Rossum | 4ccc531 | 1997-01-02 23:23:20 +0000 | [diff] [blame] | 220 | |
| 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 Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 226 | e--; |
| 227 | } |
Guido van Rossum | 4ccc531 | 1997-01-02 23:23:20 +0000 | [diff] [blame] | 228 | else if (f == 0.0) { |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 229 | e = 0; |
| 230 | } |
| 231 | else { |
| 232 | PyErr_SetString(PyExc_SystemError, |
| 233 | "frexp() result out of range"); |
| 234 | return -1; |
| 235 | } |
Guido van Rossum | 4ccc531 | 1997-01-02 23:23:20 +0000 | [diff] [blame] | 236 | |
| 237 | if (e >= 1024) { |
| 238 | /* XXX 1024 itself is reserved for Inf/NaN */ |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 239 | PyErr_SetString(PyExc_OverflowError, |
| 240 | "float too large to pack with d format"); |
| 241 | return -1; |
| 242 | } |
Guido van Rossum | 4ccc531 | 1997-01-02 23:23:20 +0000 | [diff] [blame] | 243 | else if (e < -1022) { |
| 244 | /* Gradual underflow */ |
| 245 | f = ldexp(f, 1022 + e); |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 246 | e = 0; |
| 247 | } |
Guido van Rossum | 8f3c812 | 1997-11-04 17:12:33 +0000 | [diff] [blame] | 248 | else if (!(e == 0 && f == 0.0)) { |
Guido van Rossum | 4ccc531 | 1997-01-02 23:23:20 +0000 | [diff] [blame] | 249 | e += 1023; |
| 250 | f -= 1.0; /* Get rid of leading 1 */ |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Guido van Rossum | 4ccc531 | 1997-01-02 23:23:20 +0000 | [diff] [blame] | 253 | /* 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 Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 260 | /* First byte */ |
| 261 | *p = (s<<7) | (e>>4); |
| 262 | p += incr; |
| 263 | |
| 264 | /* Second byte */ |
Guido van Rossum | 7844e38 | 1997-04-11 20:44:04 +0000 | [diff] [blame] | 265 | *p = (char) (((e&0xF)<<4) | (fhi>>24)); |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 266 | 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 | |
| 296 | static PyObject * |
| 297 | unpack_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 Rossum | 07ef655 | 1997-01-02 22:31:07 +0000 | [diff] [blame] | 326 | if (e == 0) |
| 327 | e = -126; |
| 328 | else { |
| 329 | x += 1.0; |
| 330 | e -= 127; |
| 331 | } |
| 332 | x = ldexp(x, e); |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 333 | |
| 334 | if (s) |
| 335 | x = -x; |
| 336 | |
| 337 | return PyFloat_FromDouble(x); |
| 338 | } |
| 339 | |
| 340 | static PyObject * |
| 341 | unpack_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 Rossum | 07ef655 | 1997-01-02 22:31:07 +0000 | [diff] [blame] | 388 | if (e == 0) |
| 389 | e = -1022; |
| 390 | else { |
| 391 | x += 1.0; |
| 392 | e -= 1023; |
| 393 | } |
| 394 | x = ldexp(x, e); |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 395 | |
| 396 | if (s) |
| 397 | x = -x; |
| 398 | |
| 399 | return PyFloat_FromDouble(x); |
| 400 | } |
| 401 | |
| 402 | |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 403 | /* The translation function for each format character is table driven */ |
| 404 | |
| 405 | typedef 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 | |
| 416 | static PyObject * |
| 417 | nu_char(p, f) |
| 418 | const char *p; |
| 419 | const formatdef *f; |
| 420 | { |
| 421 | return PyString_FromStringAndSize(p, 1); |
| 422 | } |
| 423 | |
| 424 | static PyObject * |
| 425 | nu_byte(p, f) |
| 426 | const char *p; |
| 427 | const formatdef *f; |
| 428 | { |
| 429 | return PyInt_FromLong((long) *(signed char *)p); |
| 430 | } |
| 431 | |
| 432 | static PyObject * |
| 433 | nu_ubyte(p, f) |
| 434 | const char *p; |
| 435 | const formatdef *f; |
| 436 | { |
| 437 | return PyInt_FromLong((long) *(unsigned char *)p); |
| 438 | } |
| 439 | |
| 440 | static PyObject * |
| 441 | nu_short(p, f) |
| 442 | const char *p; |
| 443 | const formatdef *f; |
| 444 | { |
| 445 | return PyInt_FromLong((long) *(short *)p); |
| 446 | } |
| 447 | |
| 448 | static PyObject * |
| 449 | nu_ushort(p, f) |
| 450 | const char *p; |
| 451 | const formatdef *f; |
| 452 | { |
| 453 | return PyInt_FromLong((long) *(unsigned short *)p); |
| 454 | } |
| 455 | |
| 456 | static PyObject * |
| 457 | nu_int(p, f) |
| 458 | const char *p; |
| 459 | const formatdef *f; |
| 460 | { |
| 461 | return PyInt_FromLong((long) *(int *)p); |
| 462 | } |
| 463 | |
| 464 | static PyObject * |
| 465 | nu_uint(p, f) |
| 466 | const char *p; |
| 467 | const formatdef *f; |
| 468 | { |
| 469 | unsigned int x = *(unsigned int *)p; |
Guido van Rossum | 6c87eca | 1997-01-03 19:08:16 +0000 | [diff] [blame] | 470 | return PyLong_FromUnsignedLong((unsigned long)x); |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | static PyObject * |
| 474 | nu_long(p, f) |
| 475 | const char *p; |
| 476 | const formatdef *f; |
| 477 | { |
| 478 | return PyInt_FromLong(*(long *)p); |
| 479 | } |
| 480 | |
| 481 | static PyObject * |
| 482 | nu_ulong(p, f) |
| 483 | const char *p; |
| 484 | const formatdef *f; |
| 485 | { |
Guido van Rossum | 6c87eca | 1997-01-03 19:08:16 +0000 | [diff] [blame] | 486 | return PyLong_FromUnsignedLong(*(unsigned long *)p); |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | static PyObject * |
| 490 | nu_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 | |
| 499 | static PyObject * |
| 500 | nu_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 Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 509 | static PyObject * |
| 510 | nu_void_p(p, f) |
| 511 | const char *p; |
| 512 | const formatdef *f; |
| 513 | { |
| 514 | return PyLong_FromVoidPtr(*(void **)p); |
| 515 | } |
| 516 | |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 517 | static int |
| 518 | np_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 Rossum | 7844e38 | 1997-04-11 20:44:04 +0000 | [diff] [blame] | 526 | *p = (char)x; |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 527 | return 0; |
| 528 | } |
| 529 | |
| 530 | static int |
| 531 | np_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 | |
| 545 | static int |
| 546 | np_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 Rossum | 7844e38 | 1997-04-11 20:44:04 +0000 | [diff] [blame] | 554 | * (short *)p = (short)x; |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 555 | return 0; |
| 556 | } |
| 557 | |
| 558 | static int |
| 559 | np_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 | |
| 571 | static int |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame] | 572 | np_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 | |
| 584 | static int |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 585 | np_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 | |
| 597 | static int |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame] | 598 | np_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 | |
| 610 | static int |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 611 | np_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 | |
| 626 | static int |
| 627 | np_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 Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 642 | static int |
| 643 | np_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 Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 660 | static 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 Rossum | 9eb671f | 1997-09-05 07:08:39 +0000 | [diff] [blame] | 666 | {'p', sizeof(char), 0, NULL}, |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 667 | {'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 Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame] | 670 | {'I', sizeof(int), INT_ALIGN, nu_uint, np_uint}, |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 671 | {'l', sizeof(long), LONG_ALIGN, nu_long, np_long}, |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame] | 672 | {'L', sizeof(long), LONG_ALIGN, nu_ulong, np_ulong}, |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 673 | {'f', sizeof(float), FLOAT_ALIGN, nu_float, np_float}, |
| 674 | {'d', sizeof(double), DOUBLE_ALIGN, nu_double, np_double}, |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 675 | {'P', sizeof(void *), VOID_P_ALIGN, nu_void_p, np_void_p}, |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 676 | {0} |
| 677 | }; |
| 678 | |
| 679 | static PyObject * |
| 680 | bu_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 | |
| 697 | static PyObject * |
| 698 | bu_uint(p, f) |
| 699 | const char *p; |
| 700 | const formatdef *f; |
| 701 | { |
Guido van Rossum | 6c87eca | 1997-01-03 19:08:16 +0000 | [diff] [blame] | 702 | unsigned long x = 0; |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 703 | int i = f->size; |
| 704 | do { |
| 705 | x = (x<<8) | (*p++ & 0xFF); |
| 706 | } while (--i > 0); |
Guido van Rossum | 39ef227 | 1998-06-29 04:00:40 +0000 | [diff] [blame] | 707 | if (f->size >= 4) |
| 708 | return PyLong_FromUnsignedLong(x); |
| 709 | else |
| 710 | return PyInt_FromLong((long)x); |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 711 | } |
| 712 | |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 713 | static PyObject * |
| 714 | bu_float(p, f) |
| 715 | const char *p; |
| 716 | const formatdef *f; |
| 717 | { |
| 718 | return unpack_float(p, 1); |
| 719 | } |
| 720 | |
| 721 | static PyObject * |
| 722 | bu_double(p, f) |
| 723 | const char *p; |
| 724 | const formatdef *f; |
| 725 | { |
| 726 | return unpack_double(p, 1); |
| 727 | } |
| 728 | |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 729 | static int |
| 730 | bp_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 Rossum | 7844e38 | 1997-04-11 20:44:04 +0000 | [diff] [blame] | 741 | p[--i] = (char)x; |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 742 | x >>= 8; |
| 743 | } while (i > 0); |
| 744 | return 0; |
| 745 | } |
| 746 | |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame] | 747 | static int |
| 748 | bp_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 Rossum | 7844e38 | 1997-04-11 20:44:04 +0000 | [diff] [blame] | 759 | p[--i] = (char)x; |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame] | 760 | x >>= 8; |
| 761 | } while (i > 0); |
| 762 | return 0; |
| 763 | } |
| 764 | |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 765 | static int |
| 766 | bp_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 | |
| 780 | static int |
| 781 | bp_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 Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 795 | static 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 Rossum | 9eb671f | 1997-09-05 07:08:39 +0000 | [diff] [blame] | 801 | {'p', 1, 0, NULL}, |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 802 | {'h', 2, 0, bu_int, bp_int}, |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame] | 803 | {'H', 2, 0, bu_uint, bp_uint}, |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 804 | {'i', 4, 0, bu_int, bp_int}, |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame] | 805 | {'I', 4, 0, bu_uint, bp_uint}, |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 806 | {'l', 4, 0, bu_int, bp_int}, |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame] | 807 | {'L', 4, 0, bu_uint, bp_uint}, |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 808 | {'f', 4, 0, bu_float, bp_float}, |
| 809 | {'d', 8, 0, bu_double, bp_double}, |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 810 | {0} |
| 811 | }; |
| 812 | |
| 813 | static PyObject * |
| 814 | lu_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 | |
| 831 | static PyObject * |
| 832 | lu_uint(p, f) |
| 833 | const char *p; |
| 834 | const formatdef *f; |
| 835 | { |
Guido van Rossum | 6c87eca | 1997-01-03 19:08:16 +0000 | [diff] [blame] | 836 | unsigned long x = 0; |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 837 | int i = f->size; |
| 838 | do { |
| 839 | x = (x<<8) | (p[--i] & 0xFF); |
| 840 | } while (i > 0); |
Guido van Rossum | 39ef227 | 1998-06-29 04:00:40 +0000 | [diff] [blame] | 841 | if (f->size >= 4) |
| 842 | return PyLong_FromUnsignedLong(x); |
| 843 | else |
| 844 | return PyInt_FromLong((long)x); |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 845 | } |
| 846 | |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 847 | static PyObject * |
| 848 | lu_float(p, f) |
| 849 | const char *p; |
| 850 | const formatdef *f; |
| 851 | { |
| 852 | return unpack_float(p+3, -1); |
| 853 | } |
| 854 | |
| 855 | static PyObject * |
| 856 | lu_double(p, f) |
| 857 | const char *p; |
| 858 | const formatdef *f; |
| 859 | { |
| 860 | return unpack_double(p+7, -1); |
| 861 | } |
| 862 | |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 863 | static int |
| 864 | lp_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 Rossum | 7844e38 | 1997-04-11 20:44:04 +0000 | [diff] [blame] | 875 | *p++ = (char)x; |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 876 | x >>= 8; |
| 877 | } while (--i > 0); |
| 878 | return 0; |
| 879 | } |
| 880 | |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame] | 881 | static int |
| 882 | lp_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 Rossum | 7844e38 | 1997-04-11 20:44:04 +0000 | [diff] [blame] | 893 | *p++ = (char)x; |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame] | 894 | x >>= 8; |
| 895 | } while (--i > 0); |
| 896 | return 0; |
| 897 | } |
| 898 | |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 899 | static int |
| 900 | lp_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 | |
| 914 | static int |
| 915 | lp_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 Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 929 | static 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 Rossum | 9eb671f | 1997-09-05 07:08:39 +0000 | [diff] [blame] | 935 | {'p', 1, 0, NULL}, |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 936 | {'h', 2, 0, lu_int, lp_int}, |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame] | 937 | {'H', 2, 0, lu_uint, lp_uint}, |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 938 | {'i', 4, 0, lu_int, lp_int}, |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame] | 939 | {'I', 4, 0, lu_uint, lp_uint}, |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 940 | {'l', 4, 0, lu_int, lp_int}, |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame] | 941 | {'L', 4, 0, lu_uint, lp_uint}, |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 942 | {'f', 4, 0, lu_float, lp_float}, |
| 943 | {'d', 8, 0, lu_double, lp_double}, |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 944 | {0} |
| 945 | }; |
| 946 | |
| 947 | |
| 948 | static const formatdef * |
| 949 | whichtable(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 | |
| 978 | static const formatdef * |
| 979 | getentry(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 Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 993 | /* Align a size according to a format code */ |
| 994 | |
| 995 | static int |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 996 | align(size, c, e) |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 997 | int size; |
| 998 | int c; |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 999 | const formatdef *e; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1000 | { |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1001 | if (e->format == c) { |
| 1002 | if (e->alignment) { |
| 1003 | size = ((size + e->alignment - 1) |
| 1004 | / e->alignment) |
| 1005 | * e->alignment; |
| 1006 | } |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1007 | } |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1008 | return size; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1009 | } |
| 1010 | |
| 1011 | |
| 1012 | /* calculate the size of a format string */ |
| 1013 | |
| 1014 | static int |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1015 | calcsize(fmt, f) |
| 1016 | const char *fmt; |
| 1017 | const formatdef *f; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1018 | { |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1019 | const formatdef *e; |
| 1020 | const char *s; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1021 | char c; |
| 1022 | int size, num, itemsize, x; |
| 1023 | |
| 1024 | s = fmt; |
| 1025 | size = 0; |
| 1026 | while ((c = *s++) != '\0') { |
Guido van Rossum | 730806d | 1998-04-10 22:27:42 +0000 | [diff] [blame] | 1027 | if (isspace((int)c)) |
Guido van Rossum | e20aef5 | 1997-08-26 20:39:54 +0000 | [diff] [blame] | 1028 | continue; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1029 | 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 Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1034 | PyErr_SetString( |
| 1035 | StructError, |
| 1036 | "overflow in item count"); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1037 | return -1; |
| 1038 | } |
| 1039 | num = x; |
| 1040 | } |
| 1041 | if (c == '\0') |
| 1042 | break; |
| 1043 | } |
| 1044 | else |
| 1045 | num = 1; |
| 1046 | |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1047 | e = getentry(c, f); |
| 1048 | if (e == NULL) |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1049 | return -1; |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1050 | itemsize = e->size; |
| 1051 | size = align(size, c, e); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1052 | x = num * itemsize; |
| 1053 | size += x; |
| 1054 | if (x/itemsize != num || size < 0) { |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 1055 | PyErr_SetString(StructError, |
| 1056 | "total struct size too long"); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1057 | return -1; |
| 1058 | } |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1059 | } |
| 1060 | |
| 1061 | return size; |
| 1062 | } |
| 1063 | |
| 1064 | |
Guido van Rossum | 414fd48 | 1997-12-19 04:24:24 +0000 | [diff] [blame] | 1065 | static char calcsize__doc__[] = "\ |
| 1066 | calcsize(fmt) -> int\n\ |
| 1067 | Return size of C struct described by format string fmt.\n\ |
| 1068 | See struct.__doc__ for more on format strings."; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1069 | |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 1070 | static PyObject * |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1071 | struct_calcsize(self, args) |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 1072 | PyObject *self; /* Not used */ |
| 1073 | PyObject *args; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1074 | { |
| 1075 | char *fmt; |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1076 | const formatdef *f; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1077 | int size; |
| 1078 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1079 | if (!PyArg_ParseTuple(args, "s:calcsize", &fmt)) |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1080 | return NULL; |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1081 | f = whichtable(&fmt); |
| 1082 | size = calcsize(fmt, f); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1083 | if (size < 0) |
| 1084 | return NULL; |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 1085 | return PyInt_FromLong((long)size); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1086 | } |
| 1087 | |
| 1088 | |
Guido van Rossum | 414fd48 | 1997-12-19 04:24:24 +0000 | [diff] [blame] | 1089 | static char pack__doc__[] = "\ |
| 1090 | pack(fmt, v1, v2, ...) -> string\n\ |
| 1091 | Return string containing values v1, v2, ... packed according to fmt.\n\ |
| 1092 | See struct.__doc__ for more on format strings."; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1093 | |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 1094 | static PyObject * |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1095 | struct_pack(self, args) |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 1096 | PyObject *self; /* Not used */ |
| 1097 | PyObject *args; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1098 | { |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1099 | const formatdef *f, *e; |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 1100 | PyObject *format, *result, *v; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1101 | char *fmt; |
| 1102 | int size, num; |
| 1103 | int i, n; |
Guido van Rossum | b9d338c | 1997-01-03 15:40:33 +0000 | [diff] [blame] | 1104 | char *s, *res, *restart, *nres; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1105 | char c; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1106 | |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 1107 | if (args == NULL || !PyTuple_Check(args) || |
| 1108 | (n = PyTuple_Size(args)) < 1) |
| 1109 | { |
Fred Drake | 137507e | 2000-06-01 02:02:46 +0000 | [diff] [blame] | 1110 | PyErr_SetString(PyExc_TypeError, |
| 1111 | "struct.pack requires at least one argument"); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1112 | return NULL; |
| 1113 | } |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 1114 | format = PyTuple_GetItem(args, 0); |
| 1115 | if (!PyArg_Parse(format, "s", &fmt)) |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1116 | return NULL; |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1117 | f = whichtable(&fmt); |
| 1118 | size = calcsize(fmt, f); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1119 | if (size < 0) |
| 1120 | return NULL; |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 1121 | result = PyString_FromStringAndSize((char *)NULL, size); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1122 | if (result == NULL) |
| 1123 | return NULL; |
| 1124 | |
| 1125 | s = fmt; |
| 1126 | i = 1; |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 1127 | res = restart = PyString_AsString(result); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1128 | |
| 1129 | while ((c = *s++) != '\0') { |
Guido van Rossum | 730806d | 1998-04-10 22:27:42 +0000 | [diff] [blame] | 1130 | if (isspace((int)c)) |
Guido van Rossum | e20aef5 | 1997-08-26 20:39:54 +0000 | [diff] [blame] | 1131 | continue; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1132 | 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 Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1142 | e = getentry(c, f); |
| 1143 | if (e == NULL) |
| 1144 | goto fail; |
Guido van Rossum | b9d338c | 1997-01-03 15:40:33 +0000 | [diff] [blame] | 1145 | nres = restart + align((int)(res-restart), c, e); |
| 1146 | /* Fill padd bytes with zeros */ |
| 1147 | while (res < nres) |
| 1148 | *res++ = '\0'; |
Guido van Rossum | 3aa27fd | 1996-12-31 02:10:45 +0000 | [diff] [blame] | 1149 | if (num == 0 && c != 's') |
| 1150 | continue; |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1151 | do { |
| 1152 | if (c == 'x') { |
| 1153 | /* doesn't consume arguments */ |
Guido van Rossum | 3aa27fd | 1996-12-31 02:10:45 +0000 | [diff] [blame] | 1154 | memset(res, '\0', num); |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1155 | res += num; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1156 | break; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1157 | } |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1158 | 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 Rossum | 3aa27fd | 1996-12-31 02:10:45 +0000 | [diff] [blame] | 1180 | memset(res+n, '\0', num-n); |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1181 | res += num; |
| 1182 | break; |
| 1183 | } |
Guido van Rossum | 9eb671f | 1997-09-05 07:08:39 +0000 | [diff] [blame] | 1184 | 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 Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1206 | else { |
| 1207 | if (e->pack(res, v, e) < 0) |
| 1208 | goto fail; |
| 1209 | res += e->size; |
| 1210 | } |
| 1211 | } while (--num > 0); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1212 | } |
| 1213 | |
| 1214 | if (i < n) { |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 1215 | PyErr_SetString(StructError, |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1216 | "too many arguments for pack format"); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1217 | goto fail; |
| 1218 | } |
| 1219 | |
| 1220 | return result; |
| 1221 | |
| 1222 | fail: |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 1223 | Py_DECREF(result); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1224 | return NULL; |
| 1225 | } |
| 1226 | |
| 1227 | |
Guido van Rossum | 9897f0f | 1997-12-21 06:46:20 +0000 | [diff] [blame] | 1228 | static char unpack__doc__[] = "\ |
| 1229 | unpack(fmt, string) -> (v1, v2, ...)\n\ |
| 1230 | Unpack the string, containing packed C structure data, according\n\ |
| 1231 | to fmt. Requires len(string)==calcsize(fmt).\n\ |
Guido van Rossum | 414fd48 | 1997-12-19 04:24:24 +0000 | [diff] [blame] | 1232 | See struct.__doc__ for more on format strings."; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1233 | |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 1234 | static PyObject * |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1235 | struct_unpack(self, args) |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 1236 | PyObject *self; /* Not used */ |
| 1237 | PyObject *args; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1238 | { |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1239 | const formatdef *f, *e; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1240 | char *str, *start, *fmt, *s; |
| 1241 | char c; |
Barry Warsaw | b9a781e | 1997-01-03 00:26:28 +0000 | [diff] [blame] | 1242 | int len, size, num; |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 1243 | PyObject *res, *v; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1244 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1245 | if (!PyArg_ParseTuple(args, "ss#:unpack", &fmt, &start, &len)) |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1246 | return NULL; |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1247 | f = whichtable(&fmt); |
| 1248 | size = calcsize(fmt, f); |
| 1249 | if (size < 0) |
| 1250 | return NULL; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1251 | if (size != len) { |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 1252 | PyErr_SetString(StructError, |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1253 | "unpack str size does not match format"); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1254 | return NULL; |
| 1255 | } |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 1256 | res = PyList_New(0); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1257 | if (res == NULL) |
| 1258 | return NULL; |
| 1259 | str = start; |
| 1260 | s = fmt; |
| 1261 | while ((c = *s++) != '\0') { |
Guido van Rossum | 730806d | 1998-04-10 22:27:42 +0000 | [diff] [blame] | 1262 | if (isspace((int)c)) |
Guido van Rossum | e20aef5 | 1997-08-26 20:39:54 +0000 | [diff] [blame] | 1263 | continue; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1264 | 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 Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1274 | e = getentry(c, f); |
| 1275 | if (e == NULL) |
| 1276 | goto fail; |
| 1277 | str = start + align((int)(str-start), c, e); |
Guido van Rossum | 3aa27fd | 1996-12-31 02:10:45 +0000 | [diff] [blame] | 1278 | if (num == 0 && c != 's') |
| 1279 | continue; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1280 | |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1281 | do { |
| 1282 | if (c == 'x') { |
| 1283 | str += num; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1284 | break; |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1285 | } |
| 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 Rossum | 9eb671f | 1997-09-05 07:08:39 +0000 | [diff] [blame] | 1294 | 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 Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1307 | else { |
| 1308 | v = e->unpack(str, e); |
| 1309 | if (v == NULL) |
| 1310 | goto fail; |
| 1311 | str += e->size; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1312 | } |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 1313 | if (v == NULL || PyList_Append(res, v) < 0) |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1314 | goto fail; |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 1315 | Py_DECREF(v); |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1316 | } while (--num > 0); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1317 | } |
| 1318 | |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1319 | v = PyList_AsTuple(res); |
| 1320 | Py_DECREF(res); |
| 1321 | return v; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1322 | |
| 1323 | fail: |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 1324 | Py_DECREF(res); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1325 | return NULL; |
| 1326 | } |
| 1327 | |
Guido van Rossum | 90ddb7b | 1992-08-19 16:44:15 +0000 | [diff] [blame] | 1328 | |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1329 | /* List of functions */ |
| 1330 | |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 1331 | static PyMethodDef struct_methods[] = { |
Guido van Rossum | 414fd48 | 1997-12-19 04:24:24 +0000 | [diff] [blame] | 1332 | {"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 Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1335 | {NULL, NULL} /* sentinel */ |
| 1336 | }; |
| 1337 | |
| 1338 | |
| 1339 | /* Module initialization */ |
| 1340 | |
Guido van Rossum | 3886bb6 | 1998-12-04 18:50:17 +0000 | [diff] [blame] | 1341 | DL_EXPORT(void) |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1342 | initstruct() |
| 1343 | { |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 1344 | PyObject *m, *d; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1345 | |
| 1346 | /* Create the module and add the functions */ |
Guido van Rossum | 414fd48 | 1997-12-19 04:24:24 +0000 | [diff] [blame] | 1347 | m = Py_InitModule4("struct", struct_methods, struct__doc__, |
| 1348 | (PyObject*)NULL, PYTHON_API_VERSION); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1349 | |
| 1350 | /* Add some symbolic constants to the module */ |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 1351 | d = PyModule_GetDict(m); |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 1352 | StructError = PyErr_NewException("struct.error", NULL, NULL); |
| 1353 | if (StructError == NULL) |
| 1354 | return; |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 1355 | PyDict_SetItemString(d, "error", StructError); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1356 | } |