Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1 | |
| 2 | /* struct module -- pack values into and (out of) strings */ |
| 3 | |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 4 | /* New version supporting byte order, alignment and size options, |
| 5 | character strings, and unsigned numbers */ |
| 6 | |
Guido van Rossum | 414fd48 | 1997-12-19 04:24:24 +0000 | [diff] [blame] | 7 | static char struct__doc__[] = "\ |
| 8 | Functions to convert between Python values and C structs.\n\ |
| 9 | Python strings are used to hold the data representing the C struct\n\ |
| 10 | and also as format strings to describe the layout of data in the C struct.\n\ |
| 11 | \n\ |
| 12 | The optional first format char indicates byte ordering and alignment:\n\ |
| 13 | @: native w/native alignment(default)\n\ |
| 14 | =: native w/standard alignment\n\ |
| 15 | <: little-endian, std. alignment\n\ |
| 16 | >: big-endian, std. alignment\n\ |
| 17 | !: network, std (same as >)\n\ |
| 18 | \n\ |
| 19 | The remaining chars indicate types of args and must match exactly;\n\ |
| 20 | these can be preceded by a decimal repeat count:\n\ |
| 21 | x: pad byte (no data); c:char; b:signed byte; B:unsigned byte;\n\ |
| 22 | h:short; H:unsigned short; i:int; I:unsigned int;\n\ |
| 23 | l:long; L:unsigned long; f:float; d:double.\n\ |
| 24 | Special cases (preceding decimal count indicates length):\n\ |
| 25 | 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] | 26 | Special case (only available in native format):\n\ |
| 27 | 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] | 28 | Whitespace between formats is ignored.\n\ |
| 29 | \n\ |
| 30 | The variable struct.error is an exception raised on errors."; |
| 31 | |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 32 | #include "Python.h" |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 33 | |
Guido van Rossum | e20aef5 | 1997-08-26 20:39:54 +0000 | [diff] [blame] | 34 | #include <ctype.h> |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 35 | |
| 36 | |
| 37 | /* Exception */ |
| 38 | |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 39 | static PyObject *StructError; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 40 | |
| 41 | |
| 42 | /* Define various structs to figure out the alignments of types */ |
| 43 | |
Jack Jansen | 971e1df | 1995-02-02 14:29:10 +0000 | [diff] [blame] | 44 | #ifdef __MWERKS__ |
| 45 | /* |
| 46 | ** XXXX We have a problem here. There are no unique alignment rules |
| 47 | ** on the PowerPC mac. |
| 48 | */ |
| 49 | #ifdef __powerc |
| 50 | #pragma options align=mac68k |
| 51 | #endif |
| 52 | #endif /* __MWERKS__ */ |
| 53 | |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 54 | typedef struct { char c; short x; } s_short; |
| 55 | typedef struct { char c; int x; } s_int; |
| 56 | typedef struct { char c; long x; } s_long; |
| 57 | typedef struct { char c; float x; } s_float; |
| 58 | typedef struct { char c; double x; } s_double; |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 59 | typedef struct { char c; void *x; } s_void_p; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 60 | |
| 61 | #define SHORT_ALIGN (sizeof(s_short) - sizeof(short)) |
| 62 | #define INT_ALIGN (sizeof(s_int) - sizeof(int)) |
| 63 | #define LONG_ALIGN (sizeof(s_long) - sizeof(long)) |
| 64 | #define FLOAT_ALIGN (sizeof(s_float) - sizeof(float)) |
| 65 | #define DOUBLE_ALIGN (sizeof(s_double) - sizeof(double)) |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 66 | #define VOID_P_ALIGN (sizeof(s_void_p) - sizeof(void *)) |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 67 | |
Martin v. Löwis | 2af72d5 | 2000-09-15 08:10:33 +0000 | [diff] [blame] | 68 | #define STRINGIFY(x) #x |
| 69 | |
Jack Jansen | 971e1df | 1995-02-02 14:29:10 +0000 | [diff] [blame] | 70 | #ifdef __powerc |
| 71 | #pragma options align=reset |
| 72 | #endif |
| 73 | |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 74 | /* Helper routine to get a Python integer and raise the appropriate error |
| 75 | if it isn't one */ |
| 76 | |
| 77 | static int |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 78 | get_long(PyObject *v, long *p) |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 79 | { |
| 80 | long x = PyInt_AsLong(v); |
| 81 | if (x == -1 && PyErr_Occurred()) { |
Fred Drake | d3dbb38 | 1998-05-28 04:35:49 +0000 | [diff] [blame] | 82 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 83 | PyErr_SetString(StructError, |
| 84 | "required argument is not an integer"); |
| 85 | return -1; |
| 86 | } |
| 87 | *p = x; |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame] | 92 | /* Same, but handling unsigned long */ |
| 93 | |
| 94 | static int |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 95 | get_ulong(PyObject *v, unsigned long *p) |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame] | 96 | { |
Guido van Rossum | 6c87eca | 1997-01-03 19:08:16 +0000 | [diff] [blame] | 97 | if (PyLong_Check(v)) { |
| 98 | unsigned long x = PyLong_AsUnsignedLong(v); |
| 99 | if (x == (unsigned long)(-1) && PyErr_Occurred()) |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame] | 100 | return -1; |
Guido van Rossum | 6c87eca | 1997-01-03 19:08:16 +0000 | [diff] [blame] | 101 | *p = x; |
| 102 | return 0; |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame] | 103 | } |
Guido van Rossum | 6c87eca | 1997-01-03 19:08:16 +0000 | [diff] [blame] | 104 | else { |
| 105 | return get_long(v, (long *)p); |
| 106 | } |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 110 | /* Floating point helpers */ |
| 111 | |
| 112 | /* These use ANSI/IEEE Standard 754-1985 (Standard for Binary Floating |
| 113 | Point Arithmetic). See the following URL: |
| 114 | http://www.psc.edu/general/software/packages/ieee/ieee.html */ |
| 115 | |
Guido van Rossum | 4ccc531 | 1997-01-02 23:23:20 +0000 | [diff] [blame] | 116 | /* XXX Inf/NaN are not handled quite right (but underflow is!) */ |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 117 | |
| 118 | static int |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 119 | pack_float(double x, /* The number to pack */ |
| 120 | char *p, /* Where to pack the high order byte */ |
| 121 | int incr) /* 1 for big-endian; -1 for little-endian */ |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 122 | { |
| 123 | int s; |
| 124 | int e; |
Guido van Rossum | 4ccc531 | 1997-01-02 23:23:20 +0000 | [diff] [blame] | 125 | double f; |
| 126 | long fbits; |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 127 | |
| 128 | if (x < 0) { |
| 129 | s = 1; |
| 130 | x = -x; |
| 131 | } |
| 132 | else |
| 133 | s = 0; |
Guido van Rossum | 4ccc531 | 1997-01-02 23:23:20 +0000 | [diff] [blame] | 134 | |
| 135 | f = frexp(x, &e); |
| 136 | |
| 137 | /* Normalize f to be in the range [1.0, 2.0) */ |
| 138 | if (0.5 <= f && f < 1.0) { |
| 139 | f *= 2.0; |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 140 | e--; |
| 141 | } |
Guido van Rossum | 4ccc531 | 1997-01-02 23:23:20 +0000 | [diff] [blame] | 142 | else if (f == 0.0) { |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 143 | e = 0; |
| 144 | } |
| 145 | else { |
| 146 | PyErr_SetString(PyExc_SystemError, |
| 147 | "frexp() result out of range"); |
| 148 | return -1; |
| 149 | } |
Guido van Rossum | 4ccc531 | 1997-01-02 23:23:20 +0000 | [diff] [blame] | 150 | |
| 151 | if (e >= 128) { |
| 152 | /* XXX 128 itself is reserved for Inf/NaN */ |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 153 | PyErr_SetString(PyExc_OverflowError, |
| 154 | "float too large to pack with f format"); |
| 155 | return -1; |
| 156 | } |
Guido van Rossum | 4ccc531 | 1997-01-02 23:23:20 +0000 | [diff] [blame] | 157 | else if (e < -126) { |
| 158 | /* Gradual underflow */ |
| 159 | f = ldexp(f, 126 + e); |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 160 | e = 0; |
| 161 | } |
Guido van Rossum | 8f3c812 | 1997-11-04 17:12:33 +0000 | [diff] [blame] | 162 | else if (!(e == 0 && f == 0.0)) { |
Guido van Rossum | 4ccc531 | 1997-01-02 23:23:20 +0000 | [diff] [blame] | 163 | e += 127; |
| 164 | f -= 1.0; /* Get rid of leading 1 */ |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Guido van Rossum | 4ccc531 | 1997-01-02 23:23:20 +0000 | [diff] [blame] | 167 | f *= 8388608.0; /* 2**23 */ |
| 168 | fbits = (long) floor(f + 0.5); /* Round */ |
| 169 | |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 170 | /* First byte */ |
| 171 | *p = (s<<7) | (e>>1); |
| 172 | p += incr; |
| 173 | |
| 174 | /* Second byte */ |
Guido van Rossum | 7844e38 | 1997-04-11 20:44:04 +0000 | [diff] [blame] | 175 | *p = (char) (((e&1)<<7) | (fbits>>16)); |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 176 | p += incr; |
| 177 | |
| 178 | /* Third byte */ |
Guido van Rossum | 4ccc531 | 1997-01-02 23:23:20 +0000 | [diff] [blame] | 179 | *p = (fbits>>8) & 0xFF; |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 180 | p += incr; |
| 181 | |
| 182 | /* Fourth byte */ |
Guido van Rossum | 4ccc531 | 1997-01-02 23:23:20 +0000 | [diff] [blame] | 183 | *p = fbits&0xFF; |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 184 | |
| 185 | /* Done */ |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | static int |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 190 | pack_double(double x, /* The number to pack */ |
| 191 | char *p, /* Where to pack the high order byte */ |
| 192 | int incr) /* 1 for big-endian; -1 for little-endian */ |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 193 | { |
| 194 | int s; |
| 195 | int e; |
Guido van Rossum | 4ccc531 | 1997-01-02 23:23:20 +0000 | [diff] [blame] | 196 | double f; |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 197 | long fhi, flo; |
| 198 | |
| 199 | if (x < 0) { |
| 200 | s = 1; |
| 201 | x = -x; |
| 202 | } |
| 203 | else |
| 204 | s = 0; |
Guido van Rossum | 4ccc531 | 1997-01-02 23:23:20 +0000 | [diff] [blame] | 205 | |
| 206 | f = frexp(x, &e); |
| 207 | |
| 208 | /* Normalize f to be in the range [1.0, 2.0) */ |
| 209 | if (0.5 <= f && f < 1.0) { |
| 210 | f *= 2.0; |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 211 | e--; |
| 212 | } |
Guido van Rossum | 4ccc531 | 1997-01-02 23:23:20 +0000 | [diff] [blame] | 213 | else if (f == 0.0) { |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 214 | e = 0; |
| 215 | } |
| 216 | else { |
| 217 | PyErr_SetString(PyExc_SystemError, |
| 218 | "frexp() result out of range"); |
| 219 | return -1; |
| 220 | } |
Guido van Rossum | 4ccc531 | 1997-01-02 23:23:20 +0000 | [diff] [blame] | 221 | |
| 222 | if (e >= 1024) { |
| 223 | /* XXX 1024 itself is reserved for Inf/NaN */ |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 224 | PyErr_SetString(PyExc_OverflowError, |
| 225 | "float too large to pack with d format"); |
| 226 | return -1; |
| 227 | } |
Guido van Rossum | 4ccc531 | 1997-01-02 23:23:20 +0000 | [diff] [blame] | 228 | else if (e < -1022) { |
| 229 | /* Gradual underflow */ |
| 230 | f = ldexp(f, 1022 + e); |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 231 | e = 0; |
| 232 | } |
Guido van Rossum | 8f3c812 | 1997-11-04 17:12:33 +0000 | [diff] [blame] | 233 | else if (!(e == 0 && f == 0.0)) { |
Guido van Rossum | 4ccc531 | 1997-01-02 23:23:20 +0000 | [diff] [blame] | 234 | e += 1023; |
| 235 | f -= 1.0; /* Get rid of leading 1 */ |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Guido van Rossum | 4ccc531 | 1997-01-02 23:23:20 +0000 | [diff] [blame] | 238 | /* fhi receives the high 28 bits; flo the low 24 bits (== 52 bits) */ |
| 239 | f *= 268435456.0; /* 2**28 */ |
| 240 | fhi = (long) floor(f); /* Truncate */ |
| 241 | f -= (double)fhi; |
| 242 | f *= 16777216.0; /* 2**24 */ |
| 243 | flo = (long) floor(f + 0.5); /* Round */ |
| 244 | |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 245 | /* First byte */ |
| 246 | *p = (s<<7) | (e>>4); |
| 247 | p += incr; |
| 248 | |
| 249 | /* Second byte */ |
Guido van Rossum | 7844e38 | 1997-04-11 20:44:04 +0000 | [diff] [blame] | 250 | *p = (char) (((e&0xF)<<4) | (fhi>>24)); |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 251 | p += incr; |
| 252 | |
| 253 | /* Third byte */ |
| 254 | *p = (fhi>>16) & 0xFF; |
| 255 | p += incr; |
| 256 | |
| 257 | /* Fourth byte */ |
| 258 | *p = (fhi>>8) & 0xFF; |
| 259 | p += incr; |
| 260 | |
| 261 | /* Fifth byte */ |
| 262 | *p = fhi & 0xFF; |
| 263 | p += incr; |
| 264 | |
| 265 | /* Sixth byte */ |
| 266 | *p = (flo>>16) & 0xFF; |
| 267 | p += incr; |
| 268 | |
| 269 | /* Seventh byte */ |
| 270 | *p = (flo>>8) & 0xFF; |
| 271 | p += incr; |
| 272 | |
| 273 | /* Eighth byte */ |
| 274 | *p = flo & 0xFF; |
| 275 | p += incr; |
| 276 | |
| 277 | /* Done */ |
| 278 | return 0; |
| 279 | } |
| 280 | |
| 281 | static PyObject * |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 282 | unpack_float(const char *p, /* Where the high order byte is */ |
| 283 | int incr) /* 1 for big-endian; -1 for little-endian */ |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 284 | { |
| 285 | int s; |
| 286 | int e; |
| 287 | long f; |
| 288 | double x; |
| 289 | |
| 290 | /* First byte */ |
| 291 | s = (*p>>7) & 1; |
| 292 | e = (*p & 0x7F) << 1; |
| 293 | p += incr; |
| 294 | |
| 295 | /* Second byte */ |
| 296 | e |= (*p>>7) & 1; |
| 297 | f = (*p & 0x7F) << 16; |
| 298 | p += incr; |
| 299 | |
| 300 | /* Third byte */ |
| 301 | f |= (*p & 0xFF) << 8; |
| 302 | p += incr; |
| 303 | |
| 304 | /* Fourth byte */ |
| 305 | f |= *p & 0xFF; |
| 306 | |
| 307 | x = (double)f / 8388608.0; |
| 308 | |
| 309 | /* XXX This sadly ignores Inf/NaN issues */ |
Guido van Rossum | 07ef655 | 1997-01-02 22:31:07 +0000 | [diff] [blame] | 310 | if (e == 0) |
| 311 | e = -126; |
| 312 | else { |
| 313 | x += 1.0; |
| 314 | e -= 127; |
| 315 | } |
| 316 | x = ldexp(x, e); |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 317 | |
| 318 | if (s) |
| 319 | x = -x; |
| 320 | |
| 321 | return PyFloat_FromDouble(x); |
| 322 | } |
| 323 | |
| 324 | static PyObject * |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 325 | unpack_double(const char *p, /* Where the high order byte is */ |
| 326 | int incr) /* 1 for big-endian; -1 for little-endian */ |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 327 | { |
| 328 | int s; |
| 329 | int e; |
| 330 | long fhi, flo; |
| 331 | double x; |
| 332 | |
| 333 | /* First byte */ |
| 334 | s = (*p>>7) & 1; |
| 335 | e = (*p & 0x7F) << 4; |
| 336 | p += incr; |
| 337 | |
| 338 | /* Second byte */ |
| 339 | e |= (*p>>4) & 0xF; |
| 340 | fhi = (*p & 0xF) << 24; |
| 341 | p += incr; |
| 342 | |
| 343 | /* Third byte */ |
| 344 | fhi |= (*p & 0xFF) << 16; |
| 345 | p += incr; |
| 346 | |
| 347 | /* Fourth byte */ |
| 348 | fhi |= (*p & 0xFF) << 8; |
| 349 | p += incr; |
| 350 | |
| 351 | /* Fifth byte */ |
| 352 | fhi |= *p & 0xFF; |
| 353 | p += incr; |
| 354 | |
| 355 | /* Sixth byte */ |
| 356 | flo = (*p & 0xFF) << 16; |
| 357 | p += incr; |
| 358 | |
| 359 | /* Seventh byte */ |
| 360 | flo |= (*p & 0xFF) << 8; |
| 361 | p += incr; |
| 362 | |
| 363 | /* Eighth byte */ |
| 364 | flo |= *p & 0xFF; |
| 365 | p += incr; |
| 366 | |
| 367 | x = (double)fhi + (double)flo / 16777216.0; /* 2**24 */ |
| 368 | x /= 268435456.0; /* 2**28 */ |
| 369 | |
| 370 | /* XXX This sadly ignores Inf/NaN */ |
Guido van Rossum | 07ef655 | 1997-01-02 22:31:07 +0000 | [diff] [blame] | 371 | if (e == 0) |
| 372 | e = -1022; |
| 373 | else { |
| 374 | x += 1.0; |
| 375 | e -= 1023; |
| 376 | } |
| 377 | x = ldexp(x, e); |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 378 | |
| 379 | if (s) |
| 380 | x = -x; |
| 381 | |
| 382 | return PyFloat_FromDouble(x); |
| 383 | } |
| 384 | |
| 385 | |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 386 | /* The translation function for each format character is table driven */ |
| 387 | |
| 388 | typedef struct _formatdef { |
| 389 | char format; |
| 390 | int size; |
| 391 | int alignment; |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 392 | PyObject* (*unpack)(const char *, |
| 393 | const struct _formatdef *); |
| 394 | int (*pack)(char *, PyObject *, |
| 395 | const struct _formatdef *); |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 396 | } formatdef; |
| 397 | |
| 398 | static PyObject * |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 399 | nu_char(const char *p, const formatdef *f) |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 400 | { |
| 401 | return PyString_FromStringAndSize(p, 1); |
| 402 | } |
| 403 | |
| 404 | static PyObject * |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 405 | nu_byte(const char *p, const formatdef *f) |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 406 | { |
| 407 | return PyInt_FromLong((long) *(signed char *)p); |
| 408 | } |
| 409 | |
| 410 | static PyObject * |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 411 | nu_ubyte(const char *p, const formatdef *f) |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 412 | { |
| 413 | return PyInt_FromLong((long) *(unsigned char *)p); |
| 414 | } |
| 415 | |
| 416 | static PyObject * |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 417 | nu_short(const char *p, const formatdef *f) |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 418 | { |
| 419 | return PyInt_FromLong((long) *(short *)p); |
| 420 | } |
| 421 | |
| 422 | static PyObject * |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 423 | nu_ushort(const char *p, const formatdef *f) |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 424 | { |
| 425 | return PyInt_FromLong((long) *(unsigned short *)p); |
| 426 | } |
| 427 | |
| 428 | static PyObject * |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 429 | nu_int(const char *p, const formatdef *f) |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 430 | { |
| 431 | return PyInt_FromLong((long) *(int *)p); |
| 432 | } |
| 433 | |
| 434 | static PyObject * |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 435 | nu_uint(const char *p, const formatdef *f) |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 436 | { |
| 437 | unsigned int x = *(unsigned int *)p; |
Guido van Rossum | 6c87eca | 1997-01-03 19:08:16 +0000 | [diff] [blame] | 438 | return PyLong_FromUnsignedLong((unsigned long)x); |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | static PyObject * |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 442 | nu_long(const char *p, const formatdef *f) |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 443 | { |
| 444 | return PyInt_FromLong(*(long *)p); |
| 445 | } |
| 446 | |
| 447 | static PyObject * |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 448 | nu_ulong(const char *p, const formatdef *f) |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 449 | { |
Guido van Rossum | 6c87eca | 1997-01-03 19:08:16 +0000 | [diff] [blame] | 450 | return PyLong_FromUnsignedLong(*(unsigned long *)p); |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | static PyObject * |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 454 | nu_float(const char *p, const formatdef *f) |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 455 | { |
| 456 | float x; |
| 457 | memcpy((char *)&x, p, sizeof(float)); |
| 458 | return PyFloat_FromDouble((double)x); |
| 459 | } |
| 460 | |
| 461 | static PyObject * |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 462 | nu_double(const char *p, const formatdef *f) |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 463 | { |
| 464 | double x; |
| 465 | memcpy((char *)&x, p, sizeof(double)); |
| 466 | return PyFloat_FromDouble(x); |
| 467 | } |
| 468 | |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 469 | static PyObject * |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 470 | nu_void_p(const char *p, const formatdef *f) |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 471 | { |
| 472 | return PyLong_FromVoidPtr(*(void **)p); |
| 473 | } |
| 474 | |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 475 | static int |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 476 | np_byte(char *p, PyObject *v, const formatdef *f) |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 477 | { |
| 478 | long x; |
| 479 | if (get_long(v, &x) < 0) |
| 480 | return -1; |
Martin v. Löwis | 66de549 | 2000-09-15 07:31:57 +0000 | [diff] [blame] | 481 | if (x < -128 || x > 127){ |
| 482 | PyErr_SetString(StructError, |
| 483 | "byte format requires -128<=number<=127"); |
| 484 | return -1; |
| 485 | } |
| 486 | *p = (char)x; |
| 487 | return 0; |
| 488 | } |
| 489 | |
| 490 | static int |
| 491 | np_ubyte(char *p, PyObject *v, const formatdef *f) |
| 492 | { |
| 493 | long x; |
| 494 | if (get_long(v, &x) < 0) |
| 495 | return -1; |
| 496 | if (x < 0 || x > 255){ |
| 497 | PyErr_SetString(StructError, |
| 498 | "ubyte format requires 0<=number<=255"); |
| 499 | return -1; |
| 500 | } |
Guido van Rossum | 7844e38 | 1997-04-11 20:44:04 +0000 | [diff] [blame] | 501 | *p = (char)x; |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 502 | return 0; |
| 503 | } |
| 504 | |
| 505 | static int |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 506 | np_char(char *p, PyObject *v, const formatdef *f) |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 507 | { |
| 508 | if (!PyString_Check(v) || PyString_Size(v) != 1) { |
| 509 | PyErr_SetString(StructError, |
| 510 | "char format require string of length 1"); |
| 511 | return -1; |
| 512 | } |
| 513 | *p = *PyString_AsString(v); |
| 514 | return 0; |
| 515 | } |
| 516 | |
| 517 | static int |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 518 | np_short(char *p, PyObject *v, const formatdef *f) |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 519 | { |
| 520 | long x; |
| 521 | if (get_long(v, &x) < 0) |
| 522 | return -1; |
Martin v. Löwis | 2af72d5 | 2000-09-15 08:10:33 +0000 | [diff] [blame] | 523 | if (x < SHRT_MIN || x > SHRT_MAX){ |
Martin v. Löwis | 66de549 | 2000-09-15 07:31:57 +0000 | [diff] [blame] | 524 | PyErr_SetString(StructError, |
Martin v. Löwis | 2af72d5 | 2000-09-15 08:10:33 +0000 | [diff] [blame] | 525 | "short format requires " STRINGIFY(SHRT_MIN) |
| 526 | "<=number<=" STRINGIFY(SHRT_MAX)); |
Martin v. Löwis | 66de549 | 2000-09-15 07:31:57 +0000 | [diff] [blame] | 527 | return -1; |
| 528 | } |
Guido van Rossum | 7844e38 | 1997-04-11 20:44:04 +0000 | [diff] [blame] | 529 | * (short *)p = (short)x; |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 530 | return 0; |
| 531 | } |
| 532 | |
| 533 | static int |
Martin v. Löwis | 66de549 | 2000-09-15 07:31:57 +0000 | [diff] [blame] | 534 | np_ushort(char *p, PyObject *v, const formatdef *f) |
| 535 | { |
| 536 | long x; |
| 537 | if (get_long(v, &x) < 0) |
| 538 | return -1; |
Martin v. Löwis | 2af72d5 | 2000-09-15 08:10:33 +0000 | [diff] [blame] | 539 | if (x < 0 || x > USHRT_MAX){ |
Martin v. Löwis | 66de549 | 2000-09-15 07:31:57 +0000 | [diff] [blame] | 540 | PyErr_SetString(StructError, |
Martin v. Löwis | 2af72d5 | 2000-09-15 08:10:33 +0000 | [diff] [blame] | 541 | "short format requires 0<=number<=" STRINGIFY(USHRT_MAX)); |
Martin v. Löwis | 66de549 | 2000-09-15 07:31:57 +0000 | [diff] [blame] | 542 | return -1; |
| 543 | } |
| 544 | * (unsigned short *)p = (unsigned short)x; |
| 545 | return 0; |
| 546 | } |
| 547 | |
| 548 | static int |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 549 | np_int(char *p, PyObject *v, const formatdef *f) |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 550 | { |
| 551 | long x; |
| 552 | if (get_long(v, &x) < 0) |
| 553 | return -1; |
| 554 | * (int *)p = x; |
| 555 | return 0; |
| 556 | } |
| 557 | |
| 558 | static int |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 559 | np_uint(char *p, PyObject *v, const formatdef *f) |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame] | 560 | { |
| 561 | unsigned long x; |
| 562 | if (get_ulong(v, &x) < 0) |
| 563 | return -1; |
| 564 | * (unsigned int *)p = x; |
| 565 | return 0; |
| 566 | } |
| 567 | |
| 568 | static int |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 569 | np_long(char *p, PyObject *v, const formatdef *f) |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 570 | { |
| 571 | long x; |
| 572 | if (get_long(v, &x) < 0) |
| 573 | return -1; |
| 574 | * (long *)p = x; |
| 575 | return 0; |
| 576 | } |
| 577 | |
| 578 | static int |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 579 | np_ulong(char *p, PyObject *v, const formatdef *f) |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame] | 580 | { |
| 581 | unsigned long x; |
| 582 | if (get_ulong(v, &x) < 0) |
| 583 | return -1; |
| 584 | * (unsigned long *)p = x; |
| 585 | return 0; |
| 586 | } |
| 587 | |
| 588 | static int |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 589 | np_float(char *p, PyObject *v, const formatdef *f) |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 590 | { |
| 591 | float x = (float)PyFloat_AsDouble(v); |
| 592 | if (x == -1 && PyErr_Occurred()) { |
| 593 | PyErr_SetString(StructError, |
| 594 | "required argument is not a float"); |
| 595 | return -1; |
| 596 | } |
| 597 | memcpy(p, (char *)&x, sizeof(float)); |
| 598 | return 0; |
| 599 | } |
| 600 | |
| 601 | static int |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 602 | np_double(char *p, PyObject *v, const formatdef *f) |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 603 | { |
| 604 | double x = PyFloat_AsDouble(v); |
| 605 | if (x == -1 && PyErr_Occurred()) { |
| 606 | PyErr_SetString(StructError, |
| 607 | "required argument is not a float"); |
| 608 | return -1; |
| 609 | } |
| 610 | memcpy(p, (char *)&x, sizeof(double)); |
| 611 | return 0; |
| 612 | } |
| 613 | |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 614 | static int |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 615 | np_void_p(char *p, PyObject *v, const formatdef *f) |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 616 | { |
| 617 | void *x = PyLong_AsVoidPtr(v); |
| 618 | if (x == NULL && PyErr_Occurred()) { |
| 619 | /* ### hrm. PyLong_AsVoidPtr raises SystemError */ |
| 620 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 621 | PyErr_SetString(StructError, |
| 622 | "required argument is not an integer"); |
| 623 | return -1; |
| 624 | } |
| 625 | *(void **)p = x; |
| 626 | return 0; |
| 627 | } |
| 628 | |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 629 | static formatdef native_table[] = { |
| 630 | {'x', sizeof(char), 0, NULL}, |
| 631 | {'b', sizeof(char), 0, nu_byte, np_byte}, |
Martin v. Löwis | 66de549 | 2000-09-15 07:31:57 +0000 | [diff] [blame] | 632 | {'B', sizeof(char), 0, nu_ubyte, np_ubyte}, |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 633 | {'c', sizeof(char), 0, nu_char, np_char}, |
| 634 | {'s', sizeof(char), 0, NULL}, |
Guido van Rossum | 9eb671f | 1997-09-05 07:08:39 +0000 | [diff] [blame] | 635 | {'p', sizeof(char), 0, NULL}, |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 636 | {'h', sizeof(short), SHORT_ALIGN, nu_short, np_short}, |
Martin v. Löwis | 66de549 | 2000-09-15 07:31:57 +0000 | [diff] [blame] | 637 | {'H', sizeof(short), SHORT_ALIGN, nu_ushort, np_ushort}, |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 638 | {'i', sizeof(int), INT_ALIGN, nu_int, np_int}, |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame] | 639 | {'I', sizeof(int), INT_ALIGN, nu_uint, np_uint}, |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 640 | {'l', sizeof(long), LONG_ALIGN, nu_long, np_long}, |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame] | 641 | {'L', sizeof(long), LONG_ALIGN, nu_ulong, np_ulong}, |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 642 | {'f', sizeof(float), FLOAT_ALIGN, nu_float, np_float}, |
| 643 | {'d', sizeof(double), DOUBLE_ALIGN, nu_double, np_double}, |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 644 | {'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] | 645 | {0} |
| 646 | }; |
| 647 | |
| 648 | static PyObject * |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 649 | bu_int(const char *p, const formatdef *f) |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 650 | { |
| 651 | long x = 0; |
| 652 | int i = f->size; |
| 653 | do { |
| 654 | x = (x<<8) | (*p++ & 0xFF); |
| 655 | } while (--i > 0); |
Tim Peters | f0e717b | 2001-04-08 23:39:38 +0000 | [diff] [blame] | 656 | /* Extend the sign bit. */ |
| 657 | if (SIZEOF_LONG > f->size) |
| 658 | x |= -(x & (1L << (8*f->size - 1))); |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 659 | return PyInt_FromLong(x); |
| 660 | } |
| 661 | |
| 662 | static PyObject * |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 663 | bu_uint(const char *p, const formatdef *f) |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 664 | { |
Guido van Rossum | 6c87eca | 1997-01-03 19:08:16 +0000 | [diff] [blame] | 665 | unsigned long x = 0; |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 666 | int i = f->size; |
| 667 | do { |
| 668 | x = (x<<8) | (*p++ & 0xFF); |
| 669 | } while (--i > 0); |
Guido van Rossum | 39ef227 | 1998-06-29 04:00:40 +0000 | [diff] [blame] | 670 | if (f->size >= 4) |
| 671 | return PyLong_FromUnsignedLong(x); |
| 672 | else |
| 673 | return PyInt_FromLong((long)x); |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 674 | } |
| 675 | |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 676 | static PyObject * |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 677 | bu_float(const char *p, const formatdef *f) |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 678 | { |
| 679 | return unpack_float(p, 1); |
| 680 | } |
| 681 | |
| 682 | static PyObject * |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 683 | bu_double(const char *p, const formatdef *f) |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 684 | { |
| 685 | return unpack_double(p, 1); |
| 686 | } |
| 687 | |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 688 | static int |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 689 | bp_int(char *p, PyObject *v, const formatdef *f) |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 690 | { |
| 691 | long x; |
| 692 | int i; |
| 693 | if (get_long(v, &x) < 0) |
| 694 | return -1; |
| 695 | i = f->size; |
| 696 | do { |
Guido van Rossum | 7844e38 | 1997-04-11 20:44:04 +0000 | [diff] [blame] | 697 | p[--i] = (char)x; |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 698 | x >>= 8; |
| 699 | } while (i > 0); |
| 700 | return 0; |
| 701 | } |
| 702 | |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame] | 703 | static int |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 704 | bp_uint(char *p, PyObject *v, const formatdef *f) |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame] | 705 | { |
| 706 | unsigned long x; |
| 707 | int i; |
| 708 | if (get_ulong(v, &x) < 0) |
| 709 | return -1; |
| 710 | i = f->size; |
| 711 | do { |
Guido van Rossum | 7844e38 | 1997-04-11 20:44:04 +0000 | [diff] [blame] | 712 | p[--i] = (char)x; |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame] | 713 | x >>= 8; |
| 714 | } while (i > 0); |
| 715 | return 0; |
| 716 | } |
| 717 | |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 718 | static int |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 719 | bp_float(char *p, PyObject *v, const formatdef *f) |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 720 | { |
| 721 | double x = PyFloat_AsDouble(v); |
| 722 | if (x == -1 && PyErr_Occurred()) { |
| 723 | PyErr_SetString(StructError, |
| 724 | "required argument is not a float"); |
| 725 | return -1; |
| 726 | } |
| 727 | return pack_float(x, p, 1); |
| 728 | } |
| 729 | |
| 730 | static int |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 731 | bp_double(char *p, PyObject *v, const formatdef *f) |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 732 | { |
| 733 | double x = PyFloat_AsDouble(v); |
| 734 | if (x == -1 && PyErr_Occurred()) { |
| 735 | PyErr_SetString(StructError, |
| 736 | "required argument is not a float"); |
| 737 | return -1; |
| 738 | } |
| 739 | return pack_double(x, p, 1); |
| 740 | } |
| 741 | |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 742 | static formatdef bigendian_table[] = { |
| 743 | {'x', 1, 0, NULL}, |
| 744 | {'b', 1, 0, bu_int, bp_int}, |
| 745 | {'B', 1, 0, bu_uint, bp_int}, |
| 746 | {'c', 1, 0, nu_char, np_char}, |
| 747 | {'s', 1, 0, NULL}, |
Guido van Rossum | 9eb671f | 1997-09-05 07:08:39 +0000 | [diff] [blame] | 748 | {'p', 1, 0, NULL}, |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 749 | {'h', 2, 0, bu_int, bp_int}, |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame] | 750 | {'H', 2, 0, bu_uint, bp_uint}, |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 751 | {'i', 4, 0, bu_int, bp_int}, |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame] | 752 | {'I', 4, 0, bu_uint, bp_uint}, |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 753 | {'l', 4, 0, bu_int, bp_int}, |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame] | 754 | {'L', 4, 0, bu_uint, bp_uint}, |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 755 | {'f', 4, 0, bu_float, bp_float}, |
| 756 | {'d', 8, 0, bu_double, bp_double}, |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 757 | {0} |
| 758 | }; |
| 759 | |
| 760 | static PyObject * |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 761 | lu_int(const char *p, const formatdef *f) |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 762 | { |
| 763 | long x = 0; |
| 764 | int i = f->size; |
| 765 | do { |
| 766 | x = (x<<8) | (p[--i] & 0xFF); |
| 767 | } while (i > 0); |
Tim Peters | f0e717b | 2001-04-08 23:39:38 +0000 | [diff] [blame] | 768 | /* Extend the sign bit. */ |
| 769 | if (SIZEOF_LONG > f->size) |
| 770 | x |= -(x & (1L << (8*f->size - 1))); |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 771 | return PyInt_FromLong(x); |
| 772 | } |
| 773 | |
| 774 | static PyObject * |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 775 | lu_uint(const char *p, const formatdef *f) |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 776 | { |
Guido van Rossum | 6c87eca | 1997-01-03 19:08:16 +0000 | [diff] [blame] | 777 | unsigned long x = 0; |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 778 | int i = f->size; |
| 779 | do { |
| 780 | x = (x<<8) | (p[--i] & 0xFF); |
| 781 | } while (i > 0); |
Guido van Rossum | 39ef227 | 1998-06-29 04:00:40 +0000 | [diff] [blame] | 782 | if (f->size >= 4) |
| 783 | return PyLong_FromUnsignedLong(x); |
| 784 | else |
| 785 | return PyInt_FromLong((long)x); |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 786 | } |
| 787 | |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 788 | static PyObject * |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 789 | lu_float(const char *p, const formatdef *f) |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 790 | { |
| 791 | return unpack_float(p+3, -1); |
| 792 | } |
| 793 | |
| 794 | static PyObject * |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 795 | lu_double(const char *p, const formatdef *f) |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 796 | { |
| 797 | return unpack_double(p+7, -1); |
| 798 | } |
| 799 | |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 800 | static int |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 801 | lp_int(char *p, PyObject *v, const formatdef *f) |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 802 | { |
| 803 | long x; |
| 804 | int i; |
| 805 | if (get_long(v, &x) < 0) |
| 806 | return -1; |
| 807 | i = f->size; |
| 808 | do { |
Guido van Rossum | 7844e38 | 1997-04-11 20:44:04 +0000 | [diff] [blame] | 809 | *p++ = (char)x; |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 810 | x >>= 8; |
| 811 | } while (--i > 0); |
| 812 | return 0; |
| 813 | } |
| 814 | |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame] | 815 | static int |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 816 | lp_uint(char *p, PyObject *v, const formatdef *f) |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame] | 817 | { |
| 818 | unsigned long x; |
| 819 | int i; |
| 820 | if (get_ulong(v, &x) < 0) |
| 821 | return -1; |
| 822 | i = f->size; |
| 823 | do { |
Guido van Rossum | 7844e38 | 1997-04-11 20:44:04 +0000 | [diff] [blame] | 824 | *p++ = (char)x; |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame] | 825 | x >>= 8; |
| 826 | } while (--i > 0); |
| 827 | return 0; |
| 828 | } |
| 829 | |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 830 | static int |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 831 | lp_float(char *p, PyObject *v, const formatdef *f) |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 832 | { |
| 833 | double x = PyFloat_AsDouble(v); |
| 834 | if (x == -1 && PyErr_Occurred()) { |
| 835 | PyErr_SetString(StructError, |
| 836 | "required argument is not a float"); |
| 837 | return -1; |
| 838 | } |
| 839 | return pack_float(x, p+3, -1); |
| 840 | } |
| 841 | |
| 842 | static int |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 843 | lp_double(char *p, PyObject *v, const formatdef *f) |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 844 | { |
| 845 | double x = PyFloat_AsDouble(v); |
| 846 | if (x == -1 && PyErr_Occurred()) { |
| 847 | PyErr_SetString(StructError, |
| 848 | "required argument is not a float"); |
| 849 | return -1; |
| 850 | } |
| 851 | return pack_double(x, p+7, -1); |
| 852 | } |
| 853 | |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 854 | static formatdef lilendian_table[] = { |
| 855 | {'x', 1, 0, NULL}, |
| 856 | {'b', 1, 0, lu_int, lp_int}, |
| 857 | {'B', 1, 0, lu_uint, lp_int}, |
| 858 | {'c', 1, 0, nu_char, np_char}, |
| 859 | {'s', 1, 0, NULL}, |
Guido van Rossum | 9eb671f | 1997-09-05 07:08:39 +0000 | [diff] [blame] | 860 | {'p', 1, 0, NULL}, |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 861 | {'h', 2, 0, lu_int, lp_int}, |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame] | 862 | {'H', 2, 0, lu_uint, lp_uint}, |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 863 | {'i', 4, 0, lu_int, lp_int}, |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame] | 864 | {'I', 4, 0, lu_uint, lp_uint}, |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 865 | {'l', 4, 0, lu_int, lp_int}, |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame] | 866 | {'L', 4, 0, lu_uint, lp_uint}, |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 867 | {'f', 4, 0, lu_float, lp_float}, |
| 868 | {'d', 8, 0, lu_double, lp_double}, |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 869 | {0} |
| 870 | }; |
| 871 | |
| 872 | |
| 873 | static const formatdef * |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 874 | whichtable(char **pfmt) |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 875 | { |
| 876 | const char *fmt = (*pfmt)++; /* May be backed out of later */ |
| 877 | switch (*fmt) { |
| 878 | case '<': |
| 879 | return lilendian_table; |
| 880 | case '>': |
| 881 | case '!': /* Network byte order is big-endian */ |
| 882 | return bigendian_table; |
| 883 | case '=': { /* Host byte order -- different from native in aligment! */ |
| 884 | int n = 1; |
| 885 | char *p = (char *) &n; |
| 886 | if (*p == 1) |
| 887 | return lilendian_table; |
| 888 | else |
| 889 | return bigendian_table; |
| 890 | } |
| 891 | default: |
| 892 | --*pfmt; /* Back out of pointer increment */ |
| 893 | /* Fall through */ |
| 894 | case '@': |
| 895 | return native_table; |
| 896 | } |
| 897 | } |
| 898 | |
| 899 | |
| 900 | /* Get the table entry for a format code */ |
| 901 | |
| 902 | static const formatdef * |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 903 | getentry(int c, const formatdef *f) |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 904 | { |
| 905 | for (; f->format != '\0'; f++) { |
| 906 | if (f->format == c) { |
| 907 | return f; |
| 908 | } |
| 909 | } |
| 910 | PyErr_SetString(StructError, "bad char in struct format"); |
| 911 | return NULL; |
| 912 | } |
| 913 | |
| 914 | |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 915 | /* Align a size according to a format code */ |
| 916 | |
| 917 | static int |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 918 | align(int size, int c, const formatdef *e) |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 919 | { |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 920 | if (e->format == c) { |
| 921 | if (e->alignment) { |
| 922 | size = ((size + e->alignment - 1) |
| 923 | / e->alignment) |
| 924 | * e->alignment; |
| 925 | } |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 926 | } |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 927 | return size; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | |
| 931 | /* calculate the size of a format string */ |
| 932 | |
| 933 | static int |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 934 | calcsize(const char *fmt, const formatdef *f) |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 935 | { |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 936 | const formatdef *e; |
| 937 | const char *s; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 938 | char c; |
| 939 | int size, num, itemsize, x; |
| 940 | |
| 941 | s = fmt; |
| 942 | size = 0; |
| 943 | while ((c = *s++) != '\0') { |
Guido van Rossum | 730806d | 1998-04-10 22:27:42 +0000 | [diff] [blame] | 944 | if (isspace((int)c)) |
Guido van Rossum | e20aef5 | 1997-08-26 20:39:54 +0000 | [diff] [blame] | 945 | continue; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 946 | if ('0' <= c && c <= '9') { |
| 947 | num = c - '0'; |
| 948 | while ('0' <= (c = *s++) && c <= '9') { |
| 949 | x = num*10 + (c - '0'); |
| 950 | if (x/10 != num) { |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 951 | PyErr_SetString( |
| 952 | StructError, |
| 953 | "overflow in item count"); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 954 | return -1; |
| 955 | } |
| 956 | num = x; |
| 957 | } |
| 958 | if (c == '\0') |
| 959 | break; |
| 960 | } |
| 961 | else |
| 962 | num = 1; |
| 963 | |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 964 | e = getentry(c, f); |
| 965 | if (e == NULL) |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 966 | return -1; |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 967 | itemsize = e->size; |
| 968 | size = align(size, c, e); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 969 | x = num * itemsize; |
| 970 | size += x; |
| 971 | if (x/itemsize != num || size < 0) { |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 972 | PyErr_SetString(StructError, |
| 973 | "total struct size too long"); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 974 | return -1; |
| 975 | } |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 976 | } |
| 977 | |
| 978 | return size; |
| 979 | } |
| 980 | |
| 981 | |
Guido van Rossum | 414fd48 | 1997-12-19 04:24:24 +0000 | [diff] [blame] | 982 | static char calcsize__doc__[] = "\ |
| 983 | calcsize(fmt) -> int\n\ |
| 984 | Return size of C struct described by format string fmt.\n\ |
| 985 | See struct.__doc__ for more on format strings."; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 986 | |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 987 | static PyObject * |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 988 | struct_calcsize(PyObject *self, PyObject *args) |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 989 | { |
| 990 | char *fmt; |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 991 | const formatdef *f; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 992 | int size; |
| 993 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 994 | if (!PyArg_ParseTuple(args, "s:calcsize", &fmt)) |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 995 | return NULL; |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 996 | f = whichtable(&fmt); |
| 997 | size = calcsize(fmt, f); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 998 | if (size < 0) |
| 999 | return NULL; |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 1000 | return PyInt_FromLong((long)size); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1001 | } |
| 1002 | |
| 1003 | |
Guido van Rossum | 414fd48 | 1997-12-19 04:24:24 +0000 | [diff] [blame] | 1004 | static char pack__doc__[] = "\ |
| 1005 | pack(fmt, v1, v2, ...) -> string\n\ |
| 1006 | Return string containing values v1, v2, ... packed according to fmt.\n\ |
| 1007 | See struct.__doc__ for more on format strings."; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1008 | |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 1009 | static PyObject * |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 1010 | struct_pack(PyObject *self, PyObject *args) |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1011 | { |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1012 | const formatdef *f, *e; |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 1013 | PyObject *format, *result, *v; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1014 | char *fmt; |
| 1015 | int size, num; |
| 1016 | int i, n; |
Guido van Rossum | b9d338c | 1997-01-03 15:40:33 +0000 | [diff] [blame] | 1017 | char *s, *res, *restart, *nres; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1018 | char c; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1019 | |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 1020 | if (args == NULL || !PyTuple_Check(args) || |
| 1021 | (n = PyTuple_Size(args)) < 1) |
| 1022 | { |
Fred Drake | 137507e | 2000-06-01 02:02:46 +0000 | [diff] [blame] | 1023 | PyErr_SetString(PyExc_TypeError, |
| 1024 | "struct.pack requires at least one argument"); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1025 | return NULL; |
| 1026 | } |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 1027 | format = PyTuple_GetItem(args, 0); |
| 1028 | if (!PyArg_Parse(format, "s", &fmt)) |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1029 | return NULL; |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1030 | f = whichtable(&fmt); |
| 1031 | size = calcsize(fmt, f); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1032 | if (size < 0) |
| 1033 | return NULL; |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 1034 | result = PyString_FromStringAndSize((char *)NULL, size); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1035 | if (result == NULL) |
| 1036 | return NULL; |
| 1037 | |
| 1038 | s = fmt; |
| 1039 | i = 1; |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 1040 | res = restart = PyString_AsString(result); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1041 | |
| 1042 | while ((c = *s++) != '\0') { |
Guido van Rossum | 730806d | 1998-04-10 22:27:42 +0000 | [diff] [blame] | 1043 | if (isspace((int)c)) |
Guido van Rossum | e20aef5 | 1997-08-26 20:39:54 +0000 | [diff] [blame] | 1044 | continue; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1045 | if ('0' <= c && c <= '9') { |
| 1046 | num = c - '0'; |
| 1047 | while ('0' <= (c = *s++) && c <= '9') |
| 1048 | num = num*10 + (c - '0'); |
| 1049 | if (c == '\0') |
| 1050 | break; |
| 1051 | } |
| 1052 | else |
| 1053 | num = 1; |
| 1054 | |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1055 | e = getentry(c, f); |
| 1056 | if (e == NULL) |
| 1057 | goto fail; |
Guido van Rossum | b9d338c | 1997-01-03 15:40:33 +0000 | [diff] [blame] | 1058 | nres = restart + align((int)(res-restart), c, e); |
| 1059 | /* Fill padd bytes with zeros */ |
| 1060 | while (res < nres) |
| 1061 | *res++ = '\0'; |
Guido van Rossum | 3aa27fd | 1996-12-31 02:10:45 +0000 | [diff] [blame] | 1062 | if (num == 0 && c != 's') |
| 1063 | continue; |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1064 | do { |
| 1065 | if (c == 'x') { |
| 1066 | /* doesn't consume arguments */ |
Guido van Rossum | 3aa27fd | 1996-12-31 02:10:45 +0000 | [diff] [blame] | 1067 | memset(res, '\0', num); |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1068 | res += num; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1069 | break; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1070 | } |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1071 | if (i >= n) { |
| 1072 | PyErr_SetString(StructError, |
| 1073 | "insufficient arguments to pack"); |
| 1074 | goto fail; |
| 1075 | } |
| 1076 | v = PyTuple_GetItem(args, i++); |
| 1077 | if (v == NULL) |
| 1078 | goto fail; |
| 1079 | if (c == 's') { |
| 1080 | /* num is string size, not repeat count */ |
| 1081 | int n; |
| 1082 | if (!PyString_Check(v)) { |
| 1083 | PyErr_SetString(StructError, |
| 1084 | "argument for 's' must be a string"); |
| 1085 | goto fail; |
| 1086 | } |
| 1087 | n = PyString_Size(v); |
| 1088 | if (n > num) |
| 1089 | n = num; |
| 1090 | if (n > 0) |
| 1091 | memcpy(res, PyString_AsString(v), n); |
| 1092 | if (n < num) |
Guido van Rossum | 3aa27fd | 1996-12-31 02:10:45 +0000 | [diff] [blame] | 1093 | memset(res+n, '\0', num-n); |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1094 | res += num; |
| 1095 | break; |
| 1096 | } |
Guido van Rossum | 9eb671f | 1997-09-05 07:08:39 +0000 | [diff] [blame] | 1097 | else if (c == 'p') { |
| 1098 | /* num is string size + 1, |
| 1099 | to fit in the count byte */ |
| 1100 | int n; |
| 1101 | num--; /* now num is max string size */ |
| 1102 | if (!PyString_Check(v)) { |
| 1103 | PyErr_SetString(StructError, |
| 1104 | "argument for 'p' must be a string"); |
| 1105 | goto fail; |
| 1106 | } |
| 1107 | n = PyString_Size(v); |
| 1108 | if (n > num) |
| 1109 | n = num; |
| 1110 | if (n > 0) |
| 1111 | memcpy(res+1, PyString_AsString(v), n); |
| 1112 | if (n < num) |
| 1113 | /* no real need, just to be nice */ |
| 1114 | memset(res+1+n, '\0', num-n); |
| 1115 | *res++ = n; /* store the length byte */ |
| 1116 | res += num; |
| 1117 | break; |
| 1118 | } |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1119 | else { |
| 1120 | if (e->pack(res, v, e) < 0) |
| 1121 | goto fail; |
| 1122 | res += e->size; |
| 1123 | } |
| 1124 | } while (--num > 0); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1125 | } |
| 1126 | |
| 1127 | if (i < n) { |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 1128 | PyErr_SetString(StructError, |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1129 | "too many arguments for pack format"); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1130 | goto fail; |
| 1131 | } |
| 1132 | |
| 1133 | return result; |
| 1134 | |
| 1135 | fail: |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 1136 | Py_DECREF(result); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1137 | return NULL; |
| 1138 | } |
| 1139 | |
| 1140 | |
Guido van Rossum | 9897f0f | 1997-12-21 06:46:20 +0000 | [diff] [blame] | 1141 | static char unpack__doc__[] = "\ |
| 1142 | unpack(fmt, string) -> (v1, v2, ...)\n\ |
| 1143 | Unpack the string, containing packed C structure data, according\n\ |
| 1144 | to fmt. Requires len(string)==calcsize(fmt).\n\ |
Guido van Rossum | 414fd48 | 1997-12-19 04:24:24 +0000 | [diff] [blame] | 1145 | See struct.__doc__ for more on format strings."; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1146 | |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 1147 | static PyObject * |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 1148 | struct_unpack(PyObject *self, PyObject *args) |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1149 | { |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1150 | const formatdef *f, *e; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1151 | char *str, *start, *fmt, *s; |
| 1152 | char c; |
Barry Warsaw | b9a781e | 1997-01-03 00:26:28 +0000 | [diff] [blame] | 1153 | int len, size, num; |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 1154 | PyObject *res, *v; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1155 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1156 | if (!PyArg_ParseTuple(args, "ss#:unpack", &fmt, &start, &len)) |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1157 | return NULL; |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1158 | f = whichtable(&fmt); |
| 1159 | size = calcsize(fmt, f); |
| 1160 | if (size < 0) |
| 1161 | return NULL; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1162 | if (size != len) { |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 1163 | PyErr_SetString(StructError, |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1164 | "unpack str size does not match format"); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1165 | return NULL; |
| 1166 | } |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 1167 | res = PyList_New(0); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1168 | if (res == NULL) |
| 1169 | return NULL; |
| 1170 | str = start; |
| 1171 | s = fmt; |
| 1172 | while ((c = *s++) != '\0') { |
Guido van Rossum | 730806d | 1998-04-10 22:27:42 +0000 | [diff] [blame] | 1173 | if (isspace((int)c)) |
Guido van Rossum | e20aef5 | 1997-08-26 20:39:54 +0000 | [diff] [blame] | 1174 | continue; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1175 | if ('0' <= c && c <= '9') { |
| 1176 | num = c - '0'; |
| 1177 | while ('0' <= (c = *s++) && c <= '9') |
| 1178 | num = num*10 + (c - '0'); |
| 1179 | if (c == '\0') |
| 1180 | break; |
| 1181 | } |
| 1182 | else |
| 1183 | num = 1; |
| 1184 | |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1185 | e = getentry(c, f); |
| 1186 | if (e == NULL) |
| 1187 | goto fail; |
| 1188 | str = start + align((int)(str-start), c, e); |
Guido van Rossum | 3aa27fd | 1996-12-31 02:10:45 +0000 | [diff] [blame] | 1189 | if (num == 0 && c != 's') |
| 1190 | continue; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1191 | |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1192 | do { |
| 1193 | if (c == 'x') { |
| 1194 | str += num; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1195 | break; |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1196 | } |
| 1197 | if (c == 's') { |
| 1198 | /* num is string size, not repeat count */ |
| 1199 | v = PyString_FromStringAndSize(str, num); |
| 1200 | if (v == NULL) |
| 1201 | goto fail; |
| 1202 | str += num; |
| 1203 | num = 0; |
| 1204 | } |
Guido van Rossum | 9eb671f | 1997-09-05 07:08:39 +0000 | [diff] [blame] | 1205 | else if (c == 'p') { |
| 1206 | /* num is string buffer size, |
| 1207 | not repeat count */ |
| 1208 | int n = *(unsigned char*)str; |
| 1209 | /* first byte (unsigned) is string size */ |
| 1210 | if (n >= num) |
| 1211 | n = num-1; |
| 1212 | v = PyString_FromStringAndSize(str+1, n); |
| 1213 | if (v == NULL) |
| 1214 | goto fail; |
| 1215 | str += num; |
| 1216 | num = 0; |
| 1217 | } |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1218 | else { |
| 1219 | v = e->unpack(str, e); |
| 1220 | if (v == NULL) |
| 1221 | goto fail; |
| 1222 | str += e->size; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1223 | } |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 1224 | if (v == NULL || PyList_Append(res, v) < 0) |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1225 | goto fail; |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 1226 | Py_DECREF(v); |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1227 | } while (--num > 0); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1228 | } |
| 1229 | |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1230 | v = PyList_AsTuple(res); |
| 1231 | Py_DECREF(res); |
| 1232 | return v; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1233 | |
| 1234 | fail: |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 1235 | Py_DECREF(res); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1236 | return NULL; |
| 1237 | } |
| 1238 | |
Guido van Rossum | 90ddb7b | 1992-08-19 16:44:15 +0000 | [diff] [blame] | 1239 | |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1240 | /* List of functions */ |
| 1241 | |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 1242 | static PyMethodDef struct_methods[] = { |
Guido van Rossum | 414fd48 | 1997-12-19 04:24:24 +0000 | [diff] [blame] | 1243 | {"calcsize", struct_calcsize, METH_VARARGS, calcsize__doc__}, |
| 1244 | {"pack", struct_pack, METH_VARARGS, pack__doc__}, |
| 1245 | {"unpack", struct_unpack, METH_VARARGS, unpack__doc__}, |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1246 | {NULL, NULL} /* sentinel */ |
| 1247 | }; |
| 1248 | |
| 1249 | |
| 1250 | /* Module initialization */ |
| 1251 | |
Guido van Rossum | 3886bb6 | 1998-12-04 18:50:17 +0000 | [diff] [blame] | 1252 | DL_EXPORT(void) |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1253 | initstruct(void) |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1254 | { |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 1255 | PyObject *m, *d; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1256 | |
| 1257 | /* Create the module and add the functions */ |
Guido van Rossum | 414fd48 | 1997-12-19 04:24:24 +0000 | [diff] [blame] | 1258 | m = Py_InitModule4("struct", struct_methods, struct__doc__, |
| 1259 | (PyObject*)NULL, PYTHON_API_VERSION); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1260 | |
| 1261 | /* Add some symbolic constants to the module */ |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 1262 | d = PyModule_GetDict(m); |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 1263 | StructError = PyErr_NewException("struct.error", NULL, NULL); |
| 1264 | if (StructError == NULL) |
| 1265 | return; |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 1266 | PyDict_SetItemString(d, "error", StructError); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1267 | } |