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