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