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