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