Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1 | /* struct module -- pack values into and (out of) strings */ |
| 2 | |
| 3 | /* New version supporting byte order, alignment and size options, |
| 4 | character strings, and unsigned numbers */ |
| 5 | |
Bob Ippolito | aa70a17 | 2006-05-26 20:25:23 +0000 | [diff] [blame] | 6 | #define PY_SSIZE_T_CLEAN |
| 7 | |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 8 | #include "Python.h" |
| 9 | #include "structseq.h" |
| 10 | #include "structmember.h" |
| 11 | #include <ctype.h> |
| 12 | |
Bob Ippolito | d3611eb | 2006-05-23 19:31:23 +0000 | [diff] [blame] | 13 | static PyTypeObject PyStructType; |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 14 | |
| 15 | /* compatibility macros */ |
| 16 | #if (PY_VERSION_HEX < 0x02050000) |
| 17 | typedef int Py_ssize_t; |
| 18 | #endif |
| 19 | |
Bob Ippolito | 4182a75 | 2006-05-30 17:37:54 +0000 | [diff] [blame] | 20 | /* If PY_STRUCT_OVERFLOW_MASKING is defined, the struct module will wrap all input |
Bob Ippolito | 2fd3977 | 2006-05-29 22:55:48 +0000 | [diff] [blame] | 21 | numbers for explicit endians such that they fit in the given type, much |
| 22 | like explicit casting in C. A warning will be raised if the number did |
| 23 | not originally fit within the range of the requested type. If it is |
| 24 | not defined, then all range errors and overflow will be struct.error |
| 25 | exceptions. */ |
| 26 | |
Bob Ippolito | 4182a75 | 2006-05-30 17:37:54 +0000 | [diff] [blame] | 27 | #define PY_STRUCT_OVERFLOW_MASKING 1 |
Bob Ippolito | 2fd3977 | 2006-05-29 22:55:48 +0000 | [diff] [blame] | 28 | |
Bob Ippolito | 4182a75 | 2006-05-30 17:37:54 +0000 | [diff] [blame] | 29 | #ifdef PY_STRUCT_OVERFLOW_MASKING |
Bob Ippolito | 2fd3977 | 2006-05-29 22:55:48 +0000 | [diff] [blame] | 30 | static PyObject *pylong_ulong_mask = NULL; |
| 31 | static PyObject *pyint_zero = NULL; |
| 32 | #endif |
| 33 | |
Bob Ippolito | e6c9f98 | 2006-08-04 23:59:21 +0000 | [diff] [blame] | 34 | /* If PY_STRUCT_FLOAT_COERCE is defined, the struct module will allow float |
| 35 | arguments for integer formats with a warning for backwards |
| 36 | compatibility. */ |
| 37 | |
| 38 | #define PY_STRUCT_FLOAT_COERCE 1 |
| 39 | |
| 40 | #ifdef PY_STRUCT_FLOAT_COERCE |
| 41 | #define FLOAT_COERCE "integer argument expected, got float" |
| 42 | #endif |
| 43 | |
| 44 | |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 45 | /* The translation function for each format character is table driven */ |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 46 | typedef struct _formatdef { |
| 47 | char format; |
Bob Ippolito | aa70a17 | 2006-05-26 20:25:23 +0000 | [diff] [blame] | 48 | Py_ssize_t size; |
| 49 | Py_ssize_t alignment; |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 50 | PyObject* (*unpack)(const char *, |
| 51 | const struct _formatdef *); |
| 52 | int (*pack)(char *, PyObject *, |
| 53 | const struct _formatdef *); |
| 54 | } formatdef; |
| 55 | |
| 56 | typedef struct _formatcode { |
| 57 | const struct _formatdef *fmtdef; |
Bob Ippolito | aa70a17 | 2006-05-26 20:25:23 +0000 | [diff] [blame] | 58 | Py_ssize_t offset; |
| 59 | Py_ssize_t size; |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 60 | } formatcode; |
| 61 | |
| 62 | /* Struct object interface */ |
| 63 | |
| 64 | typedef struct { |
| 65 | PyObject_HEAD |
Bob Ippolito | aa70a17 | 2006-05-26 20:25:23 +0000 | [diff] [blame] | 66 | Py_ssize_t s_size; |
| 67 | Py_ssize_t s_len; |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 68 | formatcode *s_codes; |
| 69 | PyObject *s_format; |
| 70 | PyObject *weakreflist; /* List of weak references */ |
| 71 | } PyStructObject; |
| 72 | |
Bob Ippolito | eb62127 | 2006-05-24 15:32:06 +0000 | [diff] [blame] | 73 | |
Bob Ippolito | 07c023b | 2006-05-23 19:32:25 +0000 | [diff] [blame] | 74 | #define PyStruct_Check(op) PyObject_TypeCheck(op, &PyStructType) |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 75 | #define PyStruct_CheckExact(op) (Py_TYPE(op) == &PyStructType) |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 76 | |
| 77 | |
| 78 | /* Exception */ |
| 79 | |
| 80 | static PyObject *StructError; |
| 81 | |
| 82 | |
| 83 | /* Define various structs to figure out the alignments of types */ |
| 84 | |
| 85 | |
| 86 | typedef struct { char c; short x; } st_short; |
| 87 | typedef struct { char c; int x; } st_int; |
| 88 | typedef struct { char c; long x; } st_long; |
| 89 | typedef struct { char c; float x; } st_float; |
| 90 | typedef struct { char c; double x; } st_double; |
| 91 | typedef struct { char c; void *x; } st_void_p; |
| 92 | |
| 93 | #define SHORT_ALIGN (sizeof(st_short) - sizeof(short)) |
| 94 | #define INT_ALIGN (sizeof(st_int) - sizeof(int)) |
| 95 | #define LONG_ALIGN (sizeof(st_long) - sizeof(long)) |
| 96 | #define FLOAT_ALIGN (sizeof(st_float) - sizeof(float)) |
| 97 | #define DOUBLE_ALIGN (sizeof(st_double) - sizeof(double)) |
| 98 | #define VOID_P_ALIGN (sizeof(st_void_p) - sizeof(void *)) |
| 99 | |
| 100 | /* We can't support q and Q in native mode unless the compiler does; |
| 101 | in std mode, they're 8 bytes on all platforms. */ |
| 102 | #ifdef HAVE_LONG_LONG |
| 103 | typedef struct { char c; PY_LONG_LONG x; } s_long_long; |
| 104 | #define LONG_LONG_ALIGN (sizeof(s_long_long) - sizeof(PY_LONG_LONG)) |
| 105 | #endif |
| 106 | |
Martin v. Löwis | aef4c6b | 2007-01-21 09:33:07 +0000 | [diff] [blame] | 107 | #ifdef HAVE_C99_BOOL |
| 108 | #define BOOL_TYPE _Bool |
| 109 | typedef struct { char c; _Bool x; } s_bool; |
| 110 | #define BOOL_ALIGN (sizeof(s_bool) - sizeof(BOOL_TYPE)) |
| 111 | #else |
| 112 | #define BOOL_TYPE char |
| 113 | #define BOOL_ALIGN 0 |
| 114 | #endif |
| 115 | |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 116 | #define STRINGIFY(x) #x |
| 117 | |
| 118 | #ifdef __powerc |
| 119 | #pragma options align=reset |
| 120 | #endif |
| 121 | |
| 122 | /* Helper to get a PyLongObject by hook or by crook. Caller should decref. */ |
| 123 | |
| 124 | static PyObject * |
| 125 | get_pylong(PyObject *v) |
| 126 | { |
| 127 | PyNumberMethods *m; |
| 128 | |
| 129 | assert(v != NULL); |
| 130 | if (PyInt_Check(v)) |
| 131 | return PyLong_FromLong(PyInt_AS_LONG(v)); |
| 132 | if (PyLong_Check(v)) { |
| 133 | Py_INCREF(v); |
| 134 | return v; |
| 135 | } |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 136 | m = Py_TYPE(v)->tp_as_number; |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 137 | if (m != NULL && m->nb_long != NULL) { |
| 138 | v = m->nb_long(v); |
| 139 | if (v == NULL) |
| 140 | return NULL; |
| 141 | if (PyLong_Check(v)) |
| 142 | return v; |
| 143 | Py_DECREF(v); |
| 144 | } |
| 145 | PyErr_SetString(StructError, |
| 146 | "cannot convert argument to long"); |
| 147 | return NULL; |
| 148 | } |
| 149 | |
| 150 | /* Helper routine to get a Python integer and raise the appropriate error |
| 151 | if it isn't one */ |
| 152 | |
| 153 | static int |
| 154 | get_long(PyObject *v, long *p) |
| 155 | { |
| 156 | long x = PyInt_AsLong(v); |
| 157 | if (x == -1 && PyErr_Occurred()) { |
Bob Ippolito | e6c9f98 | 2006-08-04 23:59:21 +0000 | [diff] [blame] | 158 | #ifdef PY_STRUCT_FLOAT_COERCE |
| 159 | if (PyFloat_Check(v)) { |
| 160 | PyObject *o; |
| 161 | int res; |
| 162 | PyErr_Clear(); |
| 163 | if (PyErr_WarnEx(PyExc_DeprecationWarning, FLOAT_COERCE, 2) < 0) |
| 164 | return -1; |
| 165 | o = PyNumber_Int(v); |
| 166 | if (o == NULL) |
| 167 | return -1; |
| 168 | res = get_long(o, p); |
| 169 | Py_DECREF(o); |
| 170 | return res; |
| 171 | } |
| 172 | #endif |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 173 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 174 | PyErr_SetString(StructError, |
| 175 | "required argument is not an integer"); |
| 176 | return -1; |
| 177 | } |
| 178 | *p = x; |
| 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | |
| 183 | /* Same, but handling unsigned long */ |
| 184 | |
Jeffrey Yasskin | 7937d93 | 2009-05-29 03:44:31 +0000 | [diff] [blame] | 185 | #ifndef PY_STRUCT_OVERFLOW_MASKING |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 186 | static int |
| 187 | get_ulong(PyObject *v, unsigned long *p) |
| 188 | { |
| 189 | if (PyLong_Check(v)) { |
| 190 | unsigned long x = PyLong_AsUnsignedLong(v); |
| 191 | if (x == (unsigned long)(-1) && PyErr_Occurred()) |
| 192 | return -1; |
| 193 | *p = x; |
| 194 | return 0; |
| 195 | } |
Bob Ippolito | e27337b | 2006-05-26 13:15:44 +0000 | [diff] [blame] | 196 | if (get_long(v, (long *)p) < 0) |
| 197 | return -1; |
| 198 | if (((long)*p) < 0) { |
| 199 | PyErr_SetString(StructError, |
| 200 | "unsigned argument is < 0"); |
| 201 | return -1; |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 202 | } |
Bob Ippolito | e27337b | 2006-05-26 13:15:44 +0000 | [diff] [blame] | 203 | return 0; |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 204 | } |
Jeffrey Yasskin | 7937d93 | 2009-05-29 03:44:31 +0000 | [diff] [blame] | 205 | #endif /* PY_STRUCT_OVERFLOW_MASKING */ |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 206 | |
| 207 | #ifdef HAVE_LONG_LONG |
| 208 | |
| 209 | /* Same, but handling native long long. */ |
| 210 | |
| 211 | static int |
| 212 | get_longlong(PyObject *v, PY_LONG_LONG *p) |
| 213 | { |
| 214 | PY_LONG_LONG x; |
| 215 | |
| 216 | v = get_pylong(v); |
| 217 | if (v == NULL) |
| 218 | return -1; |
| 219 | assert(PyLong_Check(v)); |
| 220 | x = PyLong_AsLongLong(v); |
| 221 | Py_DECREF(v); |
| 222 | if (x == (PY_LONG_LONG)-1 && PyErr_Occurred()) |
| 223 | return -1; |
| 224 | *p = x; |
| 225 | return 0; |
| 226 | } |
| 227 | |
| 228 | /* Same, but handling native unsigned long long. */ |
| 229 | |
| 230 | static int |
| 231 | get_ulonglong(PyObject *v, unsigned PY_LONG_LONG *p) |
| 232 | { |
| 233 | unsigned PY_LONG_LONG x; |
| 234 | |
| 235 | v = get_pylong(v); |
| 236 | if (v == NULL) |
| 237 | return -1; |
| 238 | assert(PyLong_Check(v)); |
| 239 | x = PyLong_AsUnsignedLongLong(v); |
| 240 | Py_DECREF(v); |
| 241 | if (x == (unsigned PY_LONG_LONG)-1 && PyErr_Occurred()) |
| 242 | return -1; |
| 243 | *p = x; |
| 244 | return 0; |
| 245 | } |
| 246 | |
| 247 | #endif |
| 248 | |
Bob Ippolito | 4182a75 | 2006-05-30 17:37:54 +0000 | [diff] [blame] | 249 | #ifdef PY_STRUCT_OVERFLOW_MASKING |
Bob Ippolito | 2fd3977 | 2006-05-29 22:55:48 +0000 | [diff] [blame] | 250 | |
| 251 | /* Helper routine to get a Python integer and raise the appropriate error |
| 252 | if it isn't one */ |
| 253 | |
Neal Norwitz | 07aadb1 | 2006-07-30 06:55:48 +0000 | [diff] [blame] | 254 | #define INT_OVERFLOW "struct integer overflow masking is deprecated" |
| 255 | |
Bob Ippolito | 2fd3977 | 2006-05-29 22:55:48 +0000 | [diff] [blame] | 256 | static int |
| 257 | get_wrapped_long(PyObject *v, long *p) |
| 258 | { |
| 259 | if (get_long(v, p) < 0) { |
Neal Norwitz | 3c5431e | 2006-06-11 05:45:25 +0000 | [diff] [blame] | 260 | if (PyLong_Check(v) && |
| 261 | PyErr_ExceptionMatches(PyExc_OverflowError)) { |
Bob Ippolito | 2fd3977 | 2006-05-29 22:55:48 +0000 | [diff] [blame] | 262 | PyObject *wrapped; |
| 263 | long x; |
| 264 | PyErr_Clear(); |
Bob Ippolito | e6c9f98 | 2006-08-04 23:59:21 +0000 | [diff] [blame] | 265 | #ifdef PY_STRUCT_FLOAT_COERCE |
| 266 | if (PyFloat_Check(v)) { |
| 267 | PyObject *o; |
| 268 | int res; |
| 269 | PyErr_Clear(); |
| 270 | if (PyErr_WarnEx(PyExc_DeprecationWarning, FLOAT_COERCE, 2) < 0) |
| 271 | return -1; |
| 272 | o = PyNumber_Int(v); |
| 273 | if (o == NULL) |
| 274 | return -1; |
| 275 | res = get_wrapped_long(o, p); |
| 276 | Py_DECREF(o); |
| 277 | return res; |
| 278 | } |
| 279 | #endif |
Neal Norwitz | 07aadb1 | 2006-07-30 06:55:48 +0000 | [diff] [blame] | 280 | if (PyErr_WarnEx(PyExc_DeprecationWarning, INT_OVERFLOW, 2) < 0) |
Bob Ippolito | 2fd3977 | 2006-05-29 22:55:48 +0000 | [diff] [blame] | 281 | return -1; |
| 282 | wrapped = PyNumber_And(v, pylong_ulong_mask); |
| 283 | if (wrapped == NULL) |
| 284 | return -1; |
| 285 | x = (long)PyLong_AsUnsignedLong(wrapped); |
| 286 | Py_DECREF(wrapped); |
| 287 | if (x == -1 && PyErr_Occurred()) |
| 288 | return -1; |
| 289 | *p = x; |
| 290 | } else { |
| 291 | return -1; |
| 292 | } |
| 293 | } |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | static int |
| 298 | get_wrapped_ulong(PyObject *v, unsigned long *p) |
| 299 | { |
| 300 | long x = (long)PyLong_AsUnsignedLong(v); |
| 301 | if (x == -1 && PyErr_Occurred()) { |
| 302 | PyObject *wrapped; |
| 303 | PyErr_Clear(); |
Bob Ippolito | e6c9f98 | 2006-08-04 23:59:21 +0000 | [diff] [blame] | 304 | #ifdef PY_STRUCT_FLOAT_COERCE |
| 305 | if (PyFloat_Check(v)) { |
| 306 | PyObject *o; |
| 307 | int res; |
| 308 | PyErr_Clear(); |
| 309 | if (PyErr_WarnEx(PyExc_DeprecationWarning, FLOAT_COERCE, 2) < 0) |
| 310 | return -1; |
| 311 | o = PyNumber_Int(v); |
| 312 | if (o == NULL) |
| 313 | return -1; |
| 314 | res = get_wrapped_ulong(o, p); |
| 315 | Py_DECREF(o); |
| 316 | return res; |
| 317 | } |
| 318 | #endif |
Bob Ippolito | 2fd3977 | 2006-05-29 22:55:48 +0000 | [diff] [blame] | 319 | wrapped = PyNumber_And(v, pylong_ulong_mask); |
| 320 | if (wrapped == NULL) |
| 321 | return -1; |
Neal Norwitz | 07aadb1 | 2006-07-30 06:55:48 +0000 | [diff] [blame] | 322 | if (PyErr_WarnEx(PyExc_DeprecationWarning, INT_OVERFLOW, 2) < 0) { |
Bob Ippolito | 2fd3977 | 2006-05-29 22:55:48 +0000 | [diff] [blame] | 323 | Py_DECREF(wrapped); |
| 324 | return -1; |
| 325 | } |
| 326 | x = (long)PyLong_AsUnsignedLong(wrapped); |
| 327 | Py_DECREF(wrapped); |
| 328 | if (x == -1 && PyErr_Occurred()) |
| 329 | return -1; |
| 330 | } |
| 331 | *p = (unsigned long)x; |
| 332 | return 0; |
| 333 | } |
| 334 | |
| 335 | #define RANGE_ERROR(x, f, flag, mask) \ |
| 336 | do { \ |
| 337 | if (_range_error(f, flag) < 0) \ |
| 338 | return -1; \ |
| 339 | else \ |
| 340 | (x) &= (mask); \ |
| 341 | } while (0) |
| 342 | |
| 343 | #else |
| 344 | |
| 345 | #define get_wrapped_long get_long |
| 346 | #define get_wrapped_ulong get_ulong |
| 347 | #define RANGE_ERROR(x, f, flag, mask) return _range_error(f, flag) |
| 348 | |
| 349 | #endif |
| 350 | |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 351 | /* Floating point helpers */ |
| 352 | |
| 353 | static PyObject * |
| 354 | unpack_float(const char *p, /* start of 4-byte string */ |
| 355 | int le) /* true for little-endian, false for big-endian */ |
| 356 | { |
| 357 | double x; |
| 358 | |
| 359 | x = _PyFloat_Unpack4((unsigned char *)p, le); |
| 360 | if (x == -1.0 && PyErr_Occurred()) |
| 361 | return NULL; |
| 362 | return PyFloat_FromDouble(x); |
| 363 | } |
| 364 | |
| 365 | static PyObject * |
| 366 | unpack_double(const char *p, /* start of 8-byte string */ |
| 367 | int le) /* true for little-endian, false for big-endian */ |
| 368 | { |
| 369 | double x; |
| 370 | |
| 371 | x = _PyFloat_Unpack8((unsigned char *)p, le); |
| 372 | if (x == -1.0 && PyErr_Occurred()) |
| 373 | return NULL; |
| 374 | return PyFloat_FromDouble(x); |
| 375 | } |
| 376 | |
Bob Ippolito | e27337b | 2006-05-26 13:15:44 +0000 | [diff] [blame] | 377 | /* Helper to format the range error exceptions */ |
| 378 | static int |
Armin Rigo | 162997e | 2006-05-29 17:59:47 +0000 | [diff] [blame] | 379 | _range_error(const formatdef *f, int is_unsigned) |
Bob Ippolito | e27337b | 2006-05-26 13:15:44 +0000 | [diff] [blame] | 380 | { |
Tim Peters | d6a6f02 | 2006-05-31 15:33:22 +0000 | [diff] [blame] | 381 | /* ulargest is the largest unsigned value with f->size bytes. |
| 382 | * Note that the simpler: |
| 383 | * ((size_t)1 << (f->size * 8)) - 1 |
Tim Peters | 72270c2 | 2006-05-31 15:34:37 +0000 | [diff] [blame] | 384 | * doesn't work when f->size == sizeof(size_t) because C doesn't |
| 385 | * define what happens when a left shift count is >= the number of |
| 386 | * bits in the integer being shifted; e.g., on some boxes it doesn't |
| 387 | * shift at all when they're equal. |
Tim Peters | d6a6f02 | 2006-05-31 15:33:22 +0000 | [diff] [blame] | 388 | */ |
| 389 | const size_t ulargest = (size_t)-1 >> ((SIZEOF_SIZE_T - f->size)*8); |
| 390 | assert(f->size >= 1 && f->size <= SIZEOF_SIZE_T); |
| 391 | if (is_unsigned) |
Bob Ippolito | e27337b | 2006-05-26 13:15:44 +0000 | [diff] [blame] | 392 | PyErr_Format(StructError, |
Neal Norwitz | 971ea11 | 2006-05-31 07:43:27 +0000 | [diff] [blame] | 393 | "'%c' format requires 0 <= number <= %zu", |
Bob Ippolito | 28b2686 | 2006-05-29 15:47:29 +0000 | [diff] [blame] | 394 | f->format, |
Tim Peters | d6a6f02 | 2006-05-31 15:33:22 +0000 | [diff] [blame] | 395 | ulargest); |
| 396 | else { |
| 397 | const Py_ssize_t largest = (Py_ssize_t)(ulargest >> 1); |
| 398 | PyErr_Format(StructError, |
| 399 | "'%c' format requires %zd <= number <= %zd", |
| 400 | f->format, |
| 401 | ~ largest, |
Bob Ippolito | e27337b | 2006-05-26 13:15:44 +0000 | [diff] [blame] | 402 | largest); |
| 403 | } |
Bob Ippolito | 4182a75 | 2006-05-30 17:37:54 +0000 | [diff] [blame] | 404 | #ifdef PY_STRUCT_OVERFLOW_MASKING |
Bob Ippolito | 2fd3977 | 2006-05-29 22:55:48 +0000 | [diff] [blame] | 405 | { |
| 406 | PyObject *ptype, *pvalue, *ptraceback; |
| 407 | PyObject *msg; |
| 408 | int rval; |
| 409 | PyErr_Fetch(&ptype, &pvalue, &ptraceback); |
| 410 | assert(pvalue != NULL); |
| 411 | msg = PyObject_Str(pvalue); |
| 412 | Py_XDECREF(ptype); |
| 413 | Py_XDECREF(pvalue); |
| 414 | Py_XDECREF(ptraceback); |
| 415 | if (msg == NULL) |
| 416 | return -1; |
Neal Norwitz | 07aadb1 | 2006-07-30 06:55:48 +0000 | [diff] [blame] | 417 | rval = PyErr_WarnEx(PyExc_DeprecationWarning, |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 418 | PyString_AS_STRING(msg), 2); |
Bob Ippolito | 2fd3977 | 2006-05-29 22:55:48 +0000 | [diff] [blame] | 419 | Py_DECREF(msg); |
| 420 | if (rval == 0) |
| 421 | return 0; |
| 422 | } |
| 423 | #endif |
Bob Ippolito | e27337b | 2006-05-26 13:15:44 +0000 | [diff] [blame] | 424 | return -1; |
| 425 | } |
Bob Ippolito | e27337b | 2006-05-26 13:15:44 +0000 | [diff] [blame] | 426 | |
| 427 | |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 428 | |
| 429 | /* A large number of small routines follow, with names of the form |
| 430 | |
Bob Ippolito | aa70a17 | 2006-05-26 20:25:23 +0000 | [diff] [blame] | 431 | [bln][up]_TYPE |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 432 | |
| 433 | [bln] distiguishes among big-endian, little-endian and native. |
| 434 | [pu] distiguishes between pack (to struct) and unpack (from struct). |
| 435 | TYPE is one of char, byte, ubyte, etc. |
| 436 | */ |
| 437 | |
| 438 | /* Native mode routines. ****************************************************/ |
| 439 | /* NOTE: |
| 440 | In all n[up]_<type> routines handling types larger than 1 byte, there is |
| 441 | *no* guarantee that the p pointer is properly aligned for each type, |
| 442 | therefore memcpy is called. An intermediate variable is used to |
| 443 | compensate for big-endian architectures. |
| 444 | Normally both the intermediate variable and the memcpy call will be |
| 445 | skipped by C optimisation in little-endian architectures (gcc >= 2.91 |
| 446 | does this). */ |
| 447 | |
| 448 | static PyObject * |
| 449 | nu_char(const char *p, const formatdef *f) |
| 450 | { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 451 | return PyString_FromStringAndSize(p, 1); |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | static PyObject * |
| 455 | nu_byte(const char *p, const formatdef *f) |
| 456 | { |
| 457 | return PyInt_FromLong((long) *(signed char *)p); |
| 458 | } |
| 459 | |
| 460 | static PyObject * |
| 461 | nu_ubyte(const char *p, const formatdef *f) |
| 462 | { |
| 463 | return PyInt_FromLong((long) *(unsigned char *)p); |
| 464 | } |
| 465 | |
| 466 | static PyObject * |
| 467 | nu_short(const char *p, const formatdef *f) |
| 468 | { |
| 469 | short x; |
| 470 | memcpy((char *)&x, p, sizeof x); |
| 471 | return PyInt_FromLong((long)x); |
| 472 | } |
| 473 | |
| 474 | static PyObject * |
| 475 | nu_ushort(const char *p, const formatdef *f) |
| 476 | { |
| 477 | unsigned short x; |
| 478 | memcpy((char *)&x, p, sizeof x); |
| 479 | return PyInt_FromLong((long)x); |
| 480 | } |
| 481 | |
| 482 | static PyObject * |
| 483 | nu_int(const char *p, const formatdef *f) |
| 484 | { |
| 485 | int x; |
| 486 | memcpy((char *)&x, p, sizeof x); |
| 487 | return PyInt_FromLong((long)x); |
| 488 | } |
| 489 | |
| 490 | static PyObject * |
| 491 | nu_uint(const char *p, const formatdef *f) |
| 492 | { |
| 493 | unsigned int x; |
| 494 | memcpy((char *)&x, p, sizeof x); |
Bob Ippolito | cd51ca5 | 2006-05-27 15:53:49 +0000 | [diff] [blame] | 495 | #if (SIZEOF_LONG > SIZEOF_INT) |
| 496 | return PyInt_FromLong((long)x); |
| 497 | #else |
| 498 | if (x <= ((unsigned int)LONG_MAX)) |
Bob Ippolito | 94f68ee | 2006-05-25 18:44:50 +0000 | [diff] [blame] | 499 | return PyInt_FromLong((long)x); |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 500 | return PyLong_FromUnsignedLong((unsigned long)x); |
Bob Ippolito | cd51ca5 | 2006-05-27 15:53:49 +0000 | [diff] [blame] | 501 | #endif |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | static PyObject * |
| 505 | nu_long(const char *p, const formatdef *f) |
| 506 | { |
| 507 | long x; |
| 508 | memcpy((char *)&x, p, sizeof x); |
| 509 | return PyInt_FromLong(x); |
| 510 | } |
| 511 | |
| 512 | static PyObject * |
| 513 | nu_ulong(const char *p, const formatdef *f) |
| 514 | { |
| 515 | unsigned long x; |
| 516 | memcpy((char *)&x, p, sizeof x); |
Bob Ippolito | 04ab994 | 2006-05-25 19:33:38 +0000 | [diff] [blame] | 517 | if (x <= LONG_MAX) |
Bob Ippolito | 94f68ee | 2006-05-25 18:44:50 +0000 | [diff] [blame] | 518 | return PyInt_FromLong((long)x); |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 519 | return PyLong_FromUnsignedLong(x); |
| 520 | } |
| 521 | |
| 522 | /* Native mode doesn't support q or Q unless the platform C supports |
| 523 | long long (or, on Windows, __int64). */ |
| 524 | |
| 525 | #ifdef HAVE_LONG_LONG |
| 526 | |
| 527 | static PyObject * |
| 528 | nu_longlong(const char *p, const formatdef *f) |
| 529 | { |
| 530 | PY_LONG_LONG x; |
| 531 | memcpy((char *)&x, p, sizeof x); |
Bob Ippolito | 04ab994 | 2006-05-25 19:33:38 +0000 | [diff] [blame] | 532 | if (x >= LONG_MIN && x <= LONG_MAX) |
Bob Ippolito | 94f68ee | 2006-05-25 18:44:50 +0000 | [diff] [blame] | 533 | return PyInt_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long)); |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 534 | return PyLong_FromLongLong(x); |
| 535 | } |
| 536 | |
| 537 | static PyObject * |
| 538 | nu_ulonglong(const char *p, const formatdef *f) |
| 539 | { |
| 540 | unsigned PY_LONG_LONG x; |
| 541 | memcpy((char *)&x, p, sizeof x); |
Bob Ippolito | 04ab994 | 2006-05-25 19:33:38 +0000 | [diff] [blame] | 542 | if (x <= LONG_MAX) |
Bob Ippolito | 94f68ee | 2006-05-25 18:44:50 +0000 | [diff] [blame] | 543 | return PyInt_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long)); |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 544 | return PyLong_FromUnsignedLongLong(x); |
| 545 | } |
| 546 | |
| 547 | #endif |
| 548 | |
| 549 | static PyObject * |
Martin v. Löwis | aef4c6b | 2007-01-21 09:33:07 +0000 | [diff] [blame] | 550 | nu_bool(const char *p, const formatdef *f) |
| 551 | { |
| 552 | BOOL_TYPE x; |
| 553 | memcpy((char *)&x, p, sizeof x); |
| 554 | return PyBool_FromLong(x != 0); |
| 555 | } |
| 556 | |
| 557 | |
| 558 | static PyObject * |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 559 | nu_float(const char *p, const formatdef *f) |
| 560 | { |
| 561 | float x; |
| 562 | memcpy((char *)&x, p, sizeof x); |
| 563 | return PyFloat_FromDouble((double)x); |
| 564 | } |
| 565 | |
| 566 | static PyObject * |
| 567 | nu_double(const char *p, const formatdef *f) |
| 568 | { |
| 569 | double x; |
| 570 | memcpy((char *)&x, p, sizeof x); |
| 571 | return PyFloat_FromDouble(x); |
| 572 | } |
| 573 | |
| 574 | static PyObject * |
| 575 | nu_void_p(const char *p, const formatdef *f) |
| 576 | { |
| 577 | void *x; |
| 578 | memcpy((char *)&x, p, sizeof x); |
| 579 | return PyLong_FromVoidPtr(x); |
| 580 | } |
| 581 | |
| 582 | static int |
| 583 | np_byte(char *p, PyObject *v, const formatdef *f) |
| 584 | { |
| 585 | long x; |
| 586 | if (get_long(v, &x) < 0) |
| 587 | return -1; |
| 588 | if (x < -128 || x > 127){ |
| 589 | PyErr_SetString(StructError, |
Bob Ippolito | e27337b | 2006-05-26 13:15:44 +0000 | [diff] [blame] | 590 | "byte format requires -128 <= number <= 127"); |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 591 | return -1; |
| 592 | } |
| 593 | *p = (char)x; |
| 594 | return 0; |
| 595 | } |
| 596 | |
| 597 | static int |
| 598 | np_ubyte(char *p, PyObject *v, const formatdef *f) |
| 599 | { |
| 600 | long x; |
| 601 | if (get_long(v, &x) < 0) |
| 602 | return -1; |
| 603 | if (x < 0 || x > 255){ |
| 604 | PyErr_SetString(StructError, |
Bob Ippolito | e27337b | 2006-05-26 13:15:44 +0000 | [diff] [blame] | 605 | "ubyte format requires 0 <= number <= 255"); |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 606 | return -1; |
| 607 | } |
| 608 | *p = (char)x; |
| 609 | return 0; |
| 610 | } |
| 611 | |
| 612 | static int |
| 613 | np_char(char *p, PyObject *v, const formatdef *f) |
| 614 | { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 615 | if (!PyString_Check(v) || PyString_Size(v) != 1) { |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 616 | PyErr_SetString(StructError, |
| 617 | "char format require string of length 1"); |
| 618 | return -1; |
| 619 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 620 | *p = *PyString_AsString(v); |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 621 | return 0; |
| 622 | } |
| 623 | |
| 624 | static int |
| 625 | np_short(char *p, PyObject *v, const formatdef *f) |
| 626 | { |
| 627 | long x; |
| 628 | short y; |
| 629 | if (get_long(v, &x) < 0) |
| 630 | return -1; |
| 631 | if (x < SHRT_MIN || x > SHRT_MAX){ |
| 632 | PyErr_SetString(StructError, |
| 633 | "short format requires " STRINGIFY(SHRT_MIN) |
Bob Ippolito | e27337b | 2006-05-26 13:15:44 +0000 | [diff] [blame] | 634 | " <= number <= " STRINGIFY(SHRT_MAX)); |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 635 | return -1; |
| 636 | } |
| 637 | y = (short)x; |
| 638 | memcpy(p, (char *)&y, sizeof y); |
| 639 | return 0; |
| 640 | } |
| 641 | |
| 642 | static int |
| 643 | np_ushort(char *p, PyObject *v, const formatdef *f) |
| 644 | { |
| 645 | long x; |
| 646 | unsigned short y; |
| 647 | if (get_long(v, &x) < 0) |
| 648 | return -1; |
| 649 | if (x < 0 || x > USHRT_MAX){ |
| 650 | PyErr_SetString(StructError, |
Bob Ippolito | e27337b | 2006-05-26 13:15:44 +0000 | [diff] [blame] | 651 | "short format requires 0 <= number <= " STRINGIFY(USHRT_MAX)); |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 652 | return -1; |
| 653 | } |
| 654 | y = (unsigned short)x; |
| 655 | memcpy(p, (char *)&y, sizeof y); |
| 656 | return 0; |
| 657 | } |
| 658 | |
| 659 | static int |
| 660 | np_int(char *p, PyObject *v, const formatdef *f) |
| 661 | { |
| 662 | long x; |
| 663 | int y; |
| 664 | if (get_long(v, &x) < 0) |
| 665 | return -1; |
Bob Ippolito | 90bd0a5 | 2006-05-27 11:47:12 +0000 | [diff] [blame] | 666 | #if (SIZEOF_LONG > SIZEOF_INT) |
Bob Ippolito | cd51ca5 | 2006-05-27 15:53:49 +0000 | [diff] [blame] | 667 | if ((x < ((long)INT_MIN)) || (x > ((long)INT_MAX))) |
Georg Brandl | 6269fec | 2009-01-01 12:15:31 +0000 | [diff] [blame] | 668 | RANGE_ERROR(x, f, 0, -1); |
Bob Ippolito | e27337b | 2006-05-26 13:15:44 +0000 | [diff] [blame] | 669 | #endif |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 670 | y = (int)x; |
| 671 | memcpy(p, (char *)&y, sizeof y); |
| 672 | return 0; |
| 673 | } |
| 674 | |
| 675 | static int |
| 676 | np_uint(char *p, PyObject *v, const formatdef *f) |
| 677 | { |
| 678 | unsigned long x; |
| 679 | unsigned int y; |
Georg Brandl | 6269fec | 2009-01-01 12:15:31 +0000 | [diff] [blame] | 680 | if (get_wrapped_ulong(v, &x) < 0) |
| 681 | return -1; |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 682 | y = (unsigned int)x; |
Bob Ippolito | 90bd0a5 | 2006-05-27 11:47:12 +0000 | [diff] [blame] | 683 | #if (SIZEOF_LONG > SIZEOF_INT) |
Bob Ippolito | cd51ca5 | 2006-05-27 15:53:49 +0000 | [diff] [blame] | 684 | if (x > ((unsigned long)UINT_MAX)) |
Georg Brandl | 6269fec | 2009-01-01 12:15:31 +0000 | [diff] [blame] | 685 | RANGE_ERROR(y, f, 1, -1); |
Bob Ippolito | e27337b | 2006-05-26 13:15:44 +0000 | [diff] [blame] | 686 | #endif |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 687 | memcpy(p, (char *)&y, sizeof y); |
| 688 | return 0; |
| 689 | } |
| 690 | |
| 691 | static int |
| 692 | np_long(char *p, PyObject *v, const formatdef *f) |
| 693 | { |
| 694 | long x; |
| 695 | if (get_long(v, &x) < 0) |
| 696 | return -1; |
| 697 | memcpy(p, (char *)&x, sizeof x); |
| 698 | return 0; |
| 699 | } |
| 700 | |
| 701 | static int |
| 702 | np_ulong(char *p, PyObject *v, const formatdef *f) |
| 703 | { |
| 704 | unsigned long x; |
Georg Brandl | 6269fec | 2009-01-01 12:15:31 +0000 | [diff] [blame] | 705 | if (get_wrapped_ulong(v, &x) < 0) |
| 706 | return -1; |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 707 | memcpy(p, (char *)&x, sizeof x); |
| 708 | return 0; |
| 709 | } |
| 710 | |
| 711 | #ifdef HAVE_LONG_LONG |
| 712 | |
| 713 | static int |
| 714 | np_longlong(char *p, PyObject *v, const formatdef *f) |
| 715 | { |
| 716 | PY_LONG_LONG x; |
| 717 | if (get_longlong(v, &x) < 0) |
| 718 | return -1; |
| 719 | memcpy(p, (char *)&x, sizeof x); |
| 720 | return 0; |
| 721 | } |
| 722 | |
| 723 | static int |
| 724 | np_ulonglong(char *p, PyObject *v, const formatdef *f) |
| 725 | { |
| 726 | unsigned PY_LONG_LONG x; |
| 727 | if (get_ulonglong(v, &x) < 0) |
| 728 | return -1; |
| 729 | memcpy(p, (char *)&x, sizeof x); |
| 730 | return 0; |
| 731 | } |
| 732 | #endif |
| 733 | |
Martin v. Löwis | aef4c6b | 2007-01-21 09:33:07 +0000 | [diff] [blame] | 734 | |
| 735 | static int |
| 736 | np_bool(char *p, PyObject *v, const formatdef *f) |
| 737 | { |
| 738 | BOOL_TYPE y; |
| 739 | y = PyObject_IsTrue(v); |
| 740 | memcpy(p, (char *)&y, sizeof y); |
| 741 | return 0; |
| 742 | } |
| 743 | |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 744 | static int |
| 745 | np_float(char *p, PyObject *v, const formatdef *f) |
| 746 | { |
| 747 | float x = (float)PyFloat_AsDouble(v); |
| 748 | if (x == -1 && PyErr_Occurred()) { |
| 749 | PyErr_SetString(StructError, |
| 750 | "required argument is not a float"); |
| 751 | return -1; |
| 752 | } |
| 753 | memcpy(p, (char *)&x, sizeof x); |
| 754 | return 0; |
| 755 | } |
| 756 | |
| 757 | static int |
| 758 | np_double(char *p, PyObject *v, const formatdef *f) |
| 759 | { |
| 760 | double x = PyFloat_AsDouble(v); |
| 761 | if (x == -1 && PyErr_Occurred()) { |
| 762 | PyErr_SetString(StructError, |
| 763 | "required argument is not a float"); |
| 764 | return -1; |
| 765 | } |
| 766 | memcpy(p, (char *)&x, sizeof(double)); |
| 767 | return 0; |
| 768 | } |
| 769 | |
| 770 | static int |
| 771 | np_void_p(char *p, PyObject *v, const formatdef *f) |
| 772 | { |
| 773 | void *x; |
| 774 | |
| 775 | v = get_pylong(v); |
| 776 | if (v == NULL) |
| 777 | return -1; |
| 778 | assert(PyLong_Check(v)); |
| 779 | x = PyLong_AsVoidPtr(v); |
| 780 | Py_DECREF(v); |
| 781 | if (x == NULL && PyErr_Occurred()) |
| 782 | return -1; |
| 783 | memcpy(p, (char *)&x, sizeof x); |
| 784 | return 0; |
| 785 | } |
| 786 | |
| 787 | static formatdef native_table[] = { |
| 788 | {'x', sizeof(char), 0, NULL}, |
| 789 | {'b', sizeof(char), 0, nu_byte, np_byte}, |
| 790 | {'B', sizeof(char), 0, nu_ubyte, np_ubyte}, |
| 791 | {'c', sizeof(char), 0, nu_char, np_char}, |
| 792 | {'s', sizeof(char), 0, NULL}, |
| 793 | {'p', sizeof(char), 0, NULL}, |
| 794 | {'h', sizeof(short), SHORT_ALIGN, nu_short, np_short}, |
| 795 | {'H', sizeof(short), SHORT_ALIGN, nu_ushort, np_ushort}, |
| 796 | {'i', sizeof(int), INT_ALIGN, nu_int, np_int}, |
| 797 | {'I', sizeof(int), INT_ALIGN, nu_uint, np_uint}, |
| 798 | {'l', sizeof(long), LONG_ALIGN, nu_long, np_long}, |
| 799 | {'L', sizeof(long), LONG_ALIGN, nu_ulong, np_ulong}, |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 800 | #ifdef HAVE_LONG_LONG |
| 801 | {'q', sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_longlong, np_longlong}, |
| 802 | {'Q', sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_ulonglong,np_ulonglong}, |
| 803 | #endif |
Thomas Heller | f3c0559 | 2008-03-05 15:34:29 +0000 | [diff] [blame] | 804 | {'?', sizeof(BOOL_TYPE), BOOL_ALIGN, nu_bool, np_bool}, |
Bob Ippolito | a99865b | 2006-05-25 19:56:56 +0000 | [diff] [blame] | 805 | {'f', sizeof(float), FLOAT_ALIGN, nu_float, np_float}, |
| 806 | {'d', sizeof(double), DOUBLE_ALIGN, nu_double, np_double}, |
| 807 | {'P', sizeof(void *), VOID_P_ALIGN, nu_void_p, np_void_p}, |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 808 | {0} |
| 809 | }; |
| 810 | |
| 811 | /* Big-endian routines. *****************************************************/ |
| 812 | |
| 813 | static PyObject * |
| 814 | bu_int(const char *p, const formatdef *f) |
| 815 | { |
| 816 | long x = 0; |
Bob Ippolito | aa70a17 | 2006-05-26 20:25:23 +0000 | [diff] [blame] | 817 | Py_ssize_t i = f->size; |
Bob Ippolito | 28b2686 | 2006-05-29 15:47:29 +0000 | [diff] [blame] | 818 | const unsigned char *bytes = (const unsigned char *)p; |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 819 | do { |
Bob Ippolito | 28b2686 | 2006-05-29 15:47:29 +0000 | [diff] [blame] | 820 | x = (x<<8) | *bytes++; |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 821 | } while (--i > 0); |
| 822 | /* Extend the sign bit. */ |
| 823 | if (SIZEOF_LONG > f->size) |
Bob Ippolito | cd51ca5 | 2006-05-27 15:53:49 +0000 | [diff] [blame] | 824 | x |= -(x & (1L << ((8 * f->size) - 1))); |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 825 | return PyInt_FromLong(x); |
| 826 | } |
| 827 | |
| 828 | static PyObject * |
| 829 | bu_uint(const char *p, const formatdef *f) |
| 830 | { |
| 831 | unsigned long x = 0; |
Bob Ippolito | aa70a17 | 2006-05-26 20:25:23 +0000 | [diff] [blame] | 832 | Py_ssize_t i = f->size; |
Bob Ippolito | 28b2686 | 2006-05-29 15:47:29 +0000 | [diff] [blame] | 833 | const unsigned char *bytes = (const unsigned char *)p; |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 834 | do { |
Bob Ippolito | 28b2686 | 2006-05-29 15:47:29 +0000 | [diff] [blame] | 835 | x = (x<<8) | *bytes++; |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 836 | } while (--i > 0); |
Bob Ippolito | 04ab994 | 2006-05-25 19:33:38 +0000 | [diff] [blame] | 837 | if (x <= LONG_MAX) |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 838 | return PyInt_FromLong((long)x); |
Bob Ippolito | 94f68ee | 2006-05-25 18:44:50 +0000 | [diff] [blame] | 839 | return PyLong_FromUnsignedLong(x); |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | static PyObject * |
| 843 | bu_longlong(const char *p, const formatdef *f) |
| 844 | { |
Bob Ippolito | 90bd0a5 | 2006-05-27 11:47:12 +0000 | [diff] [blame] | 845 | #ifdef HAVE_LONG_LONG |
Bob Ippolito | 94f68ee | 2006-05-25 18:44:50 +0000 | [diff] [blame] | 846 | PY_LONG_LONG x = 0; |
Bob Ippolito | aa70a17 | 2006-05-26 20:25:23 +0000 | [diff] [blame] | 847 | Py_ssize_t i = f->size; |
Bob Ippolito | 28b2686 | 2006-05-29 15:47:29 +0000 | [diff] [blame] | 848 | const unsigned char *bytes = (const unsigned char *)p; |
Bob Ippolito | 94f68ee | 2006-05-25 18:44:50 +0000 | [diff] [blame] | 849 | do { |
Bob Ippolito | 28b2686 | 2006-05-29 15:47:29 +0000 | [diff] [blame] | 850 | x = (x<<8) | *bytes++; |
Bob Ippolito | 94f68ee | 2006-05-25 18:44:50 +0000 | [diff] [blame] | 851 | } while (--i > 0); |
| 852 | /* Extend the sign bit. */ |
| 853 | if (SIZEOF_LONG_LONG > f->size) |
Kristján Valur Jónsson | 67387fb | 2007-04-25 00:17:39 +0000 | [diff] [blame] | 854 | x |= -(x & ((PY_LONG_LONG)1 << ((8 * f->size) - 1))); |
Bob Ippolito | 04ab994 | 2006-05-25 19:33:38 +0000 | [diff] [blame] | 855 | if (x >= LONG_MIN && x <= LONG_MAX) |
Bob Ippolito | 94f68ee | 2006-05-25 18:44:50 +0000 | [diff] [blame] | 856 | return PyInt_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long)); |
Bob Ippolito | 94f68ee | 2006-05-25 18:44:50 +0000 | [diff] [blame] | 857 | return PyLong_FromLongLong(x); |
| 858 | #else |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 859 | return _PyLong_FromByteArray((const unsigned char *)p, |
| 860 | 8, |
| 861 | 0, /* little-endian */ |
| 862 | 1 /* signed */); |
Bob Ippolito | 94f68ee | 2006-05-25 18:44:50 +0000 | [diff] [blame] | 863 | #endif |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | static PyObject * |
| 867 | bu_ulonglong(const char *p, const formatdef *f) |
| 868 | { |
Bob Ippolito | 90bd0a5 | 2006-05-27 11:47:12 +0000 | [diff] [blame] | 869 | #ifdef HAVE_LONG_LONG |
Bob Ippolito | 94f68ee | 2006-05-25 18:44:50 +0000 | [diff] [blame] | 870 | unsigned PY_LONG_LONG x = 0; |
Bob Ippolito | aa70a17 | 2006-05-26 20:25:23 +0000 | [diff] [blame] | 871 | Py_ssize_t i = f->size; |
Bob Ippolito | 28b2686 | 2006-05-29 15:47:29 +0000 | [diff] [blame] | 872 | const unsigned char *bytes = (const unsigned char *)p; |
Bob Ippolito | 94f68ee | 2006-05-25 18:44:50 +0000 | [diff] [blame] | 873 | do { |
Bob Ippolito | 28b2686 | 2006-05-29 15:47:29 +0000 | [diff] [blame] | 874 | x = (x<<8) | *bytes++; |
Bob Ippolito | 94f68ee | 2006-05-25 18:44:50 +0000 | [diff] [blame] | 875 | } while (--i > 0); |
Bob Ippolito | 04ab994 | 2006-05-25 19:33:38 +0000 | [diff] [blame] | 876 | if (x <= LONG_MAX) |
Bob Ippolito | 94f68ee | 2006-05-25 18:44:50 +0000 | [diff] [blame] | 877 | return PyInt_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long)); |
Bob Ippolito | 94f68ee | 2006-05-25 18:44:50 +0000 | [diff] [blame] | 878 | return PyLong_FromUnsignedLongLong(x); |
| 879 | #else |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 880 | return _PyLong_FromByteArray((const unsigned char *)p, |
| 881 | 8, |
| 882 | 0, /* little-endian */ |
| 883 | 0 /* signed */); |
Bob Ippolito | 94f68ee | 2006-05-25 18:44:50 +0000 | [diff] [blame] | 884 | #endif |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | static PyObject * |
| 888 | bu_float(const char *p, const formatdef *f) |
| 889 | { |
| 890 | return unpack_float(p, 0); |
| 891 | } |
| 892 | |
| 893 | static PyObject * |
| 894 | bu_double(const char *p, const formatdef *f) |
| 895 | { |
| 896 | return unpack_double(p, 0); |
| 897 | } |
| 898 | |
Martin v. Löwis | aef4c6b | 2007-01-21 09:33:07 +0000 | [diff] [blame] | 899 | static PyObject * |
| 900 | bu_bool(const char *p, const formatdef *f) |
| 901 | { |
| 902 | char x; |
| 903 | memcpy((char *)&x, p, sizeof x); |
| 904 | return PyBool_FromLong(x != 0); |
| 905 | } |
| 906 | |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 907 | static int |
| 908 | bp_int(char *p, PyObject *v, const formatdef *f) |
| 909 | { |
| 910 | long x; |
Bob Ippolito | aa70a17 | 2006-05-26 20:25:23 +0000 | [diff] [blame] | 911 | Py_ssize_t i; |
Bob Ippolito | 2fd3977 | 2006-05-29 22:55:48 +0000 | [diff] [blame] | 912 | if (get_wrapped_long(v, &x) < 0) |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 913 | return -1; |
| 914 | i = f->size; |
Bob Ippolito | cd51ca5 | 2006-05-27 15:53:49 +0000 | [diff] [blame] | 915 | if (i != SIZEOF_LONG) { |
| 916 | if ((i == 2) && (x < -32768 || x > 32767)) |
Bob Ippolito | 2fd3977 | 2006-05-29 22:55:48 +0000 | [diff] [blame] | 917 | RANGE_ERROR(x, f, 0, 0xffffL); |
Bob Ippolito | 90bd0a5 | 2006-05-27 11:47:12 +0000 | [diff] [blame] | 918 | #if (SIZEOF_LONG != 4) |
Bob Ippolito | cd51ca5 | 2006-05-27 15:53:49 +0000 | [diff] [blame] | 919 | else if ((i == 4) && (x < -2147483648L || x > 2147483647L)) |
Bob Ippolito | 2fd3977 | 2006-05-29 22:55:48 +0000 | [diff] [blame] | 920 | RANGE_ERROR(x, f, 0, 0xffffffffL); |
| 921 | #endif |
Bob Ippolito | 4182a75 | 2006-05-30 17:37:54 +0000 | [diff] [blame] | 922 | #ifdef PY_STRUCT_OVERFLOW_MASKING |
Bob Ippolito | 2fd3977 | 2006-05-29 22:55:48 +0000 | [diff] [blame] | 923 | else if ((i == 1) && (x < -128 || x > 127)) |
| 924 | RANGE_ERROR(x, f, 0, 0xffL); |
Bob Ippolito | e27337b | 2006-05-26 13:15:44 +0000 | [diff] [blame] | 925 | #endif |
Bob Ippolito | cd51ca5 | 2006-05-27 15:53:49 +0000 | [diff] [blame] | 926 | } |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 927 | do { |
| 928 | p[--i] = (char)x; |
| 929 | x >>= 8; |
| 930 | } while (i > 0); |
| 931 | return 0; |
| 932 | } |
| 933 | |
| 934 | static int |
| 935 | bp_uint(char *p, PyObject *v, const formatdef *f) |
| 936 | { |
| 937 | unsigned long x; |
Bob Ippolito | aa70a17 | 2006-05-26 20:25:23 +0000 | [diff] [blame] | 938 | Py_ssize_t i; |
Bob Ippolito | 2fd3977 | 2006-05-29 22:55:48 +0000 | [diff] [blame] | 939 | if (get_wrapped_ulong(v, &x) < 0) |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 940 | return -1; |
| 941 | i = f->size; |
Bob Ippolito | cd51ca5 | 2006-05-27 15:53:49 +0000 | [diff] [blame] | 942 | if (i != SIZEOF_LONG) { |
| 943 | unsigned long maxint = 1; |
| 944 | maxint <<= (unsigned long)(i * 8); |
| 945 | if (x >= maxint) |
Bob Ippolito | 2fd3977 | 2006-05-29 22:55:48 +0000 | [diff] [blame] | 946 | RANGE_ERROR(x, f, 1, maxint - 1); |
Bob Ippolito | cd51ca5 | 2006-05-27 15:53:49 +0000 | [diff] [blame] | 947 | } |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 948 | do { |
| 949 | p[--i] = (char)x; |
| 950 | x >>= 8; |
| 951 | } while (i > 0); |
| 952 | return 0; |
| 953 | } |
| 954 | |
| 955 | static int |
| 956 | bp_longlong(char *p, PyObject *v, const formatdef *f) |
| 957 | { |
| 958 | int res; |
| 959 | v = get_pylong(v); |
| 960 | if (v == NULL) |
| 961 | return -1; |
| 962 | res = _PyLong_AsByteArray((PyLongObject *)v, |
| 963 | (unsigned char *)p, |
| 964 | 8, |
| 965 | 0, /* little_endian */ |
| 966 | 1 /* signed */); |
| 967 | Py_DECREF(v); |
| 968 | return res; |
| 969 | } |
| 970 | |
| 971 | static int |
| 972 | bp_ulonglong(char *p, PyObject *v, const formatdef *f) |
| 973 | { |
| 974 | int res; |
| 975 | v = get_pylong(v); |
| 976 | if (v == NULL) |
| 977 | return -1; |
| 978 | res = _PyLong_AsByteArray((PyLongObject *)v, |
| 979 | (unsigned char *)p, |
| 980 | 8, |
| 981 | 0, /* little_endian */ |
| 982 | 0 /* signed */); |
| 983 | Py_DECREF(v); |
| 984 | return res; |
| 985 | } |
| 986 | |
| 987 | static int |
| 988 | bp_float(char *p, PyObject *v, const formatdef *f) |
| 989 | { |
| 990 | double x = PyFloat_AsDouble(v); |
| 991 | if (x == -1 && PyErr_Occurred()) { |
| 992 | PyErr_SetString(StructError, |
| 993 | "required argument is not a float"); |
| 994 | return -1; |
| 995 | } |
| 996 | return _PyFloat_Pack4(x, (unsigned char *)p, 0); |
| 997 | } |
| 998 | |
| 999 | static int |
| 1000 | bp_double(char *p, PyObject *v, const formatdef *f) |
| 1001 | { |
| 1002 | double x = PyFloat_AsDouble(v); |
| 1003 | if (x == -1 && PyErr_Occurred()) { |
| 1004 | PyErr_SetString(StructError, |
| 1005 | "required argument is not a float"); |
| 1006 | return -1; |
| 1007 | } |
| 1008 | return _PyFloat_Pack8(x, (unsigned char *)p, 0); |
| 1009 | } |
| 1010 | |
Martin v. Löwis | aef4c6b | 2007-01-21 09:33:07 +0000 | [diff] [blame] | 1011 | static int |
| 1012 | bp_bool(char *p, PyObject *v, const formatdef *f) |
| 1013 | { |
| 1014 | char y; |
| 1015 | y = PyObject_IsTrue(v); |
| 1016 | memcpy(p, (char *)&y, sizeof y); |
| 1017 | return 0; |
| 1018 | } |
| 1019 | |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1020 | static formatdef bigendian_table[] = { |
| 1021 | {'x', 1, 0, NULL}, |
Bob Ippolito | 4182a75 | 2006-05-30 17:37:54 +0000 | [diff] [blame] | 1022 | #ifdef PY_STRUCT_OVERFLOW_MASKING |
| 1023 | /* Native packers do range checking without overflow masking. */ |
Bob Ippolito | 2fd3977 | 2006-05-29 22:55:48 +0000 | [diff] [blame] | 1024 | {'b', 1, 0, nu_byte, bp_int}, |
| 1025 | {'B', 1, 0, nu_ubyte, bp_uint}, |
| 1026 | #else |
Bob Ippolito | e27337b | 2006-05-26 13:15:44 +0000 | [diff] [blame] | 1027 | {'b', 1, 0, nu_byte, np_byte}, |
| 1028 | {'B', 1, 0, nu_ubyte, np_ubyte}, |
Bob Ippolito | 2fd3977 | 2006-05-29 22:55:48 +0000 | [diff] [blame] | 1029 | #endif |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1030 | {'c', 1, 0, nu_char, np_char}, |
| 1031 | {'s', 1, 0, NULL}, |
| 1032 | {'p', 1, 0, NULL}, |
| 1033 | {'h', 2, 0, bu_int, bp_int}, |
| 1034 | {'H', 2, 0, bu_uint, bp_uint}, |
| 1035 | {'i', 4, 0, bu_int, bp_int}, |
| 1036 | {'I', 4, 0, bu_uint, bp_uint}, |
| 1037 | {'l', 4, 0, bu_int, bp_int}, |
| 1038 | {'L', 4, 0, bu_uint, bp_uint}, |
| 1039 | {'q', 8, 0, bu_longlong, bp_longlong}, |
| 1040 | {'Q', 8, 0, bu_ulonglong, bp_ulonglong}, |
Thomas Heller | f3c0559 | 2008-03-05 15:34:29 +0000 | [diff] [blame] | 1041 | {'?', 1, 0, bu_bool, bp_bool}, |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1042 | {'f', 4, 0, bu_float, bp_float}, |
| 1043 | {'d', 8, 0, bu_double, bp_double}, |
| 1044 | {0} |
| 1045 | }; |
| 1046 | |
| 1047 | /* Little-endian routines. *****************************************************/ |
| 1048 | |
| 1049 | static PyObject * |
| 1050 | lu_int(const char *p, const formatdef *f) |
| 1051 | { |
| 1052 | long x = 0; |
Bob Ippolito | aa70a17 | 2006-05-26 20:25:23 +0000 | [diff] [blame] | 1053 | Py_ssize_t i = f->size; |
Bob Ippolito | 28b2686 | 2006-05-29 15:47:29 +0000 | [diff] [blame] | 1054 | const unsigned char *bytes = (const unsigned char *)p; |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1055 | do { |
Bob Ippolito | 28b2686 | 2006-05-29 15:47:29 +0000 | [diff] [blame] | 1056 | x = (x<<8) | bytes[--i]; |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1057 | } while (i > 0); |
| 1058 | /* Extend the sign bit. */ |
| 1059 | if (SIZEOF_LONG > f->size) |
Bob Ippolito | cd51ca5 | 2006-05-27 15:53:49 +0000 | [diff] [blame] | 1060 | x |= -(x & (1L << ((8 * f->size) - 1))); |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1061 | return PyInt_FromLong(x); |
| 1062 | } |
| 1063 | |
| 1064 | static PyObject * |
| 1065 | lu_uint(const char *p, const formatdef *f) |
| 1066 | { |
| 1067 | unsigned long x = 0; |
Bob Ippolito | aa70a17 | 2006-05-26 20:25:23 +0000 | [diff] [blame] | 1068 | Py_ssize_t i = f->size; |
Bob Ippolito | 28b2686 | 2006-05-29 15:47:29 +0000 | [diff] [blame] | 1069 | const unsigned char *bytes = (const unsigned char *)p; |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1070 | do { |
Bob Ippolito | 28b2686 | 2006-05-29 15:47:29 +0000 | [diff] [blame] | 1071 | x = (x<<8) | bytes[--i]; |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1072 | } while (i > 0); |
Bob Ippolito | 04ab994 | 2006-05-25 19:33:38 +0000 | [diff] [blame] | 1073 | if (x <= LONG_MAX) |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1074 | return PyInt_FromLong((long)x); |
Bob Ippolito | 94f68ee | 2006-05-25 18:44:50 +0000 | [diff] [blame] | 1075 | return PyLong_FromUnsignedLong((long)x); |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1076 | } |
| 1077 | |
| 1078 | static PyObject * |
| 1079 | lu_longlong(const char *p, const formatdef *f) |
| 1080 | { |
Bob Ippolito | 90bd0a5 | 2006-05-27 11:47:12 +0000 | [diff] [blame] | 1081 | #ifdef HAVE_LONG_LONG |
Bob Ippolito | 94f68ee | 2006-05-25 18:44:50 +0000 | [diff] [blame] | 1082 | PY_LONG_LONG x = 0; |
Bob Ippolito | aa70a17 | 2006-05-26 20:25:23 +0000 | [diff] [blame] | 1083 | Py_ssize_t i = f->size; |
Bob Ippolito | 28b2686 | 2006-05-29 15:47:29 +0000 | [diff] [blame] | 1084 | const unsigned char *bytes = (const unsigned char *)p; |
Bob Ippolito | 94f68ee | 2006-05-25 18:44:50 +0000 | [diff] [blame] | 1085 | do { |
Bob Ippolito | 28b2686 | 2006-05-29 15:47:29 +0000 | [diff] [blame] | 1086 | x = (x<<8) | bytes[--i]; |
Bob Ippolito | 94f68ee | 2006-05-25 18:44:50 +0000 | [diff] [blame] | 1087 | } while (i > 0); |
| 1088 | /* Extend the sign bit. */ |
| 1089 | if (SIZEOF_LONG_LONG > f->size) |
Kristján Valur Jónsson | 67387fb | 2007-04-25 00:17:39 +0000 | [diff] [blame] | 1090 | x |= -(x & ((PY_LONG_LONG)1 << ((8 * f->size) - 1))); |
Bob Ippolito | 04ab994 | 2006-05-25 19:33:38 +0000 | [diff] [blame] | 1091 | if (x >= LONG_MIN && x <= LONG_MAX) |
Bob Ippolito | 94f68ee | 2006-05-25 18:44:50 +0000 | [diff] [blame] | 1092 | return PyInt_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long)); |
Bob Ippolito | 94f68ee | 2006-05-25 18:44:50 +0000 | [diff] [blame] | 1093 | return PyLong_FromLongLong(x); |
| 1094 | #else |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1095 | return _PyLong_FromByteArray((const unsigned char *)p, |
| 1096 | 8, |
| 1097 | 1, /* little-endian */ |
| 1098 | 1 /* signed */); |
Bob Ippolito | 94f68ee | 2006-05-25 18:44:50 +0000 | [diff] [blame] | 1099 | #endif |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1100 | } |
| 1101 | |
| 1102 | static PyObject * |
| 1103 | lu_ulonglong(const char *p, const formatdef *f) |
| 1104 | { |
Bob Ippolito | 90bd0a5 | 2006-05-27 11:47:12 +0000 | [diff] [blame] | 1105 | #ifdef HAVE_LONG_LONG |
Bob Ippolito | 94f68ee | 2006-05-25 18:44:50 +0000 | [diff] [blame] | 1106 | unsigned PY_LONG_LONG x = 0; |
Bob Ippolito | aa70a17 | 2006-05-26 20:25:23 +0000 | [diff] [blame] | 1107 | Py_ssize_t i = f->size; |
Bob Ippolito | 28b2686 | 2006-05-29 15:47:29 +0000 | [diff] [blame] | 1108 | const unsigned char *bytes = (const unsigned char *)p; |
Bob Ippolito | 94f68ee | 2006-05-25 18:44:50 +0000 | [diff] [blame] | 1109 | do { |
Bob Ippolito | 28b2686 | 2006-05-29 15:47:29 +0000 | [diff] [blame] | 1110 | x = (x<<8) | bytes[--i]; |
Bob Ippolito | 94f68ee | 2006-05-25 18:44:50 +0000 | [diff] [blame] | 1111 | } while (i > 0); |
Bob Ippolito | 04ab994 | 2006-05-25 19:33:38 +0000 | [diff] [blame] | 1112 | if (x <= LONG_MAX) |
Bob Ippolito | 94f68ee | 2006-05-25 18:44:50 +0000 | [diff] [blame] | 1113 | return PyInt_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long)); |
Bob Ippolito | 94f68ee | 2006-05-25 18:44:50 +0000 | [diff] [blame] | 1114 | return PyLong_FromUnsignedLongLong(x); |
| 1115 | #else |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1116 | return _PyLong_FromByteArray((const unsigned char *)p, |
| 1117 | 8, |
| 1118 | 1, /* little-endian */ |
| 1119 | 0 /* signed */); |
Bob Ippolito | 94f68ee | 2006-05-25 18:44:50 +0000 | [diff] [blame] | 1120 | #endif |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1121 | } |
| 1122 | |
| 1123 | static PyObject * |
| 1124 | lu_float(const char *p, const formatdef *f) |
| 1125 | { |
| 1126 | return unpack_float(p, 1); |
| 1127 | } |
| 1128 | |
| 1129 | static PyObject * |
| 1130 | lu_double(const char *p, const formatdef *f) |
| 1131 | { |
| 1132 | return unpack_double(p, 1); |
| 1133 | } |
| 1134 | |
| 1135 | static int |
| 1136 | lp_int(char *p, PyObject *v, const formatdef *f) |
| 1137 | { |
| 1138 | long x; |
Bob Ippolito | aa70a17 | 2006-05-26 20:25:23 +0000 | [diff] [blame] | 1139 | Py_ssize_t i; |
Bob Ippolito | 2fd3977 | 2006-05-29 22:55:48 +0000 | [diff] [blame] | 1140 | if (get_wrapped_long(v, &x) < 0) |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1141 | return -1; |
| 1142 | i = f->size; |
Bob Ippolito | cd51ca5 | 2006-05-27 15:53:49 +0000 | [diff] [blame] | 1143 | if (i != SIZEOF_LONG) { |
| 1144 | if ((i == 2) && (x < -32768 || x > 32767)) |
Bob Ippolito | 2fd3977 | 2006-05-29 22:55:48 +0000 | [diff] [blame] | 1145 | RANGE_ERROR(x, f, 0, 0xffffL); |
Bob Ippolito | 90bd0a5 | 2006-05-27 11:47:12 +0000 | [diff] [blame] | 1146 | #if (SIZEOF_LONG != 4) |
Bob Ippolito | cd51ca5 | 2006-05-27 15:53:49 +0000 | [diff] [blame] | 1147 | else if ((i == 4) && (x < -2147483648L || x > 2147483647L)) |
Bob Ippolito | 2fd3977 | 2006-05-29 22:55:48 +0000 | [diff] [blame] | 1148 | RANGE_ERROR(x, f, 0, 0xffffffffL); |
| 1149 | #endif |
Bob Ippolito | 4182a75 | 2006-05-30 17:37:54 +0000 | [diff] [blame] | 1150 | #ifdef PY_STRUCT_OVERFLOW_MASKING |
Bob Ippolito | 2fd3977 | 2006-05-29 22:55:48 +0000 | [diff] [blame] | 1151 | else if ((i == 1) && (x < -128 || x > 127)) |
| 1152 | RANGE_ERROR(x, f, 0, 0xffL); |
Bob Ippolito | e27337b | 2006-05-26 13:15:44 +0000 | [diff] [blame] | 1153 | #endif |
Bob Ippolito | cd51ca5 | 2006-05-27 15:53:49 +0000 | [diff] [blame] | 1154 | } |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1155 | do { |
| 1156 | *p++ = (char)x; |
| 1157 | x >>= 8; |
| 1158 | } while (--i > 0); |
| 1159 | return 0; |
| 1160 | } |
| 1161 | |
| 1162 | static int |
| 1163 | lp_uint(char *p, PyObject *v, const formatdef *f) |
| 1164 | { |
| 1165 | unsigned long x; |
Bob Ippolito | aa70a17 | 2006-05-26 20:25:23 +0000 | [diff] [blame] | 1166 | Py_ssize_t i; |
Bob Ippolito | 2fd3977 | 2006-05-29 22:55:48 +0000 | [diff] [blame] | 1167 | if (get_wrapped_ulong(v, &x) < 0) |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1168 | return -1; |
| 1169 | i = f->size; |
Bob Ippolito | cd51ca5 | 2006-05-27 15:53:49 +0000 | [diff] [blame] | 1170 | if (i != SIZEOF_LONG) { |
| 1171 | unsigned long maxint = 1; |
| 1172 | maxint <<= (unsigned long)(i * 8); |
| 1173 | if (x >= maxint) |
Bob Ippolito | 2fd3977 | 2006-05-29 22:55:48 +0000 | [diff] [blame] | 1174 | RANGE_ERROR(x, f, 1, maxint - 1); |
Bob Ippolito | cd51ca5 | 2006-05-27 15:53:49 +0000 | [diff] [blame] | 1175 | } |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1176 | do { |
| 1177 | *p++ = (char)x; |
| 1178 | x >>= 8; |
| 1179 | } while (--i > 0); |
| 1180 | return 0; |
| 1181 | } |
| 1182 | |
| 1183 | static int |
| 1184 | lp_longlong(char *p, PyObject *v, const formatdef *f) |
| 1185 | { |
| 1186 | int res; |
| 1187 | v = get_pylong(v); |
| 1188 | if (v == NULL) |
| 1189 | return -1; |
| 1190 | res = _PyLong_AsByteArray((PyLongObject*)v, |
| 1191 | (unsigned char *)p, |
| 1192 | 8, |
| 1193 | 1, /* little_endian */ |
| 1194 | 1 /* signed */); |
| 1195 | Py_DECREF(v); |
| 1196 | return res; |
| 1197 | } |
| 1198 | |
| 1199 | static int |
| 1200 | lp_ulonglong(char *p, PyObject *v, const formatdef *f) |
| 1201 | { |
| 1202 | int res; |
| 1203 | v = get_pylong(v); |
| 1204 | if (v == NULL) |
| 1205 | return -1; |
| 1206 | res = _PyLong_AsByteArray((PyLongObject*)v, |
| 1207 | (unsigned char *)p, |
| 1208 | 8, |
| 1209 | 1, /* little_endian */ |
| 1210 | 0 /* signed */); |
| 1211 | Py_DECREF(v); |
| 1212 | return res; |
| 1213 | } |
| 1214 | |
| 1215 | static int |
| 1216 | lp_float(char *p, PyObject *v, const formatdef *f) |
| 1217 | { |
| 1218 | double x = PyFloat_AsDouble(v); |
| 1219 | if (x == -1 && PyErr_Occurred()) { |
| 1220 | PyErr_SetString(StructError, |
| 1221 | "required argument is not a float"); |
| 1222 | return -1; |
| 1223 | } |
| 1224 | return _PyFloat_Pack4(x, (unsigned char *)p, 1); |
| 1225 | } |
| 1226 | |
| 1227 | static int |
| 1228 | lp_double(char *p, PyObject *v, const formatdef *f) |
| 1229 | { |
| 1230 | double x = PyFloat_AsDouble(v); |
| 1231 | if (x == -1 && PyErr_Occurred()) { |
| 1232 | PyErr_SetString(StructError, |
| 1233 | "required argument is not a float"); |
| 1234 | return -1; |
| 1235 | } |
| 1236 | return _PyFloat_Pack8(x, (unsigned char *)p, 1); |
| 1237 | } |
| 1238 | |
| 1239 | static formatdef lilendian_table[] = { |
| 1240 | {'x', 1, 0, NULL}, |
Bob Ippolito | 4182a75 | 2006-05-30 17:37:54 +0000 | [diff] [blame] | 1241 | #ifdef PY_STRUCT_OVERFLOW_MASKING |
| 1242 | /* Native packers do range checking without overflow masking. */ |
Bob Ippolito | 2fd3977 | 2006-05-29 22:55:48 +0000 | [diff] [blame] | 1243 | {'b', 1, 0, nu_byte, lp_int}, |
| 1244 | {'B', 1, 0, nu_ubyte, lp_uint}, |
| 1245 | #else |
Bob Ippolito | e27337b | 2006-05-26 13:15:44 +0000 | [diff] [blame] | 1246 | {'b', 1, 0, nu_byte, np_byte}, |
| 1247 | {'B', 1, 0, nu_ubyte, np_ubyte}, |
Bob Ippolito | 2fd3977 | 2006-05-29 22:55:48 +0000 | [diff] [blame] | 1248 | #endif |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1249 | {'c', 1, 0, nu_char, np_char}, |
| 1250 | {'s', 1, 0, NULL}, |
| 1251 | {'p', 1, 0, NULL}, |
| 1252 | {'h', 2, 0, lu_int, lp_int}, |
| 1253 | {'H', 2, 0, lu_uint, lp_uint}, |
| 1254 | {'i', 4, 0, lu_int, lp_int}, |
| 1255 | {'I', 4, 0, lu_uint, lp_uint}, |
| 1256 | {'l', 4, 0, lu_int, lp_int}, |
| 1257 | {'L', 4, 0, lu_uint, lp_uint}, |
| 1258 | {'q', 8, 0, lu_longlong, lp_longlong}, |
| 1259 | {'Q', 8, 0, lu_ulonglong, lp_ulonglong}, |
Thomas Heller | f3c0559 | 2008-03-05 15:34:29 +0000 | [diff] [blame] | 1260 | {'?', 1, 0, bu_bool, bp_bool}, /* Std rep not endian dep, |
Martin v. Löwis | aef4c6b | 2007-01-21 09:33:07 +0000 | [diff] [blame] | 1261 | but potentially different from native rep -- reuse bx_bool funcs. */ |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1262 | {'f', 4, 0, lu_float, lp_float}, |
| 1263 | {'d', 8, 0, lu_double, lp_double}, |
| 1264 | {0} |
| 1265 | }; |
| 1266 | |
| 1267 | |
| 1268 | static const formatdef * |
| 1269 | whichtable(char **pfmt) |
| 1270 | { |
| 1271 | const char *fmt = (*pfmt)++; /* May be backed out of later */ |
| 1272 | switch (*fmt) { |
| 1273 | case '<': |
| 1274 | return lilendian_table; |
| 1275 | case '>': |
| 1276 | case '!': /* Network byte order is big-endian */ |
| 1277 | return bigendian_table; |
| 1278 | case '=': { /* Host byte order -- different from native in aligment! */ |
| 1279 | int n = 1; |
| 1280 | char *p = (char *) &n; |
| 1281 | if (*p == 1) |
| 1282 | return lilendian_table; |
| 1283 | else |
| 1284 | return bigendian_table; |
| 1285 | } |
| 1286 | default: |
| 1287 | --*pfmt; /* Back out of pointer increment */ |
| 1288 | /* Fall through */ |
| 1289 | case '@': |
| 1290 | return native_table; |
| 1291 | } |
| 1292 | } |
| 1293 | |
| 1294 | |
| 1295 | /* Get the table entry for a format code */ |
| 1296 | |
| 1297 | static const formatdef * |
| 1298 | getentry(int c, const formatdef *f) |
| 1299 | { |
| 1300 | for (; f->format != '\0'; f++) { |
| 1301 | if (f->format == c) { |
| 1302 | return f; |
| 1303 | } |
| 1304 | } |
| 1305 | PyErr_SetString(StructError, "bad char in struct format"); |
| 1306 | return NULL; |
| 1307 | } |
| 1308 | |
| 1309 | |
| 1310 | /* Align a size according to a format code */ |
| 1311 | |
| 1312 | static int |
Bob Ippolito | aa70a17 | 2006-05-26 20:25:23 +0000 | [diff] [blame] | 1313 | align(Py_ssize_t size, char c, const formatdef *e) |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1314 | { |
| 1315 | if (e->format == c) { |
| 1316 | if (e->alignment) { |
| 1317 | size = ((size + e->alignment - 1) |
| 1318 | / e->alignment) |
| 1319 | * e->alignment; |
| 1320 | } |
| 1321 | } |
| 1322 | return size; |
| 1323 | } |
| 1324 | |
| 1325 | |
| 1326 | /* calculate the size of a format string */ |
| 1327 | |
| 1328 | static int |
| 1329 | prepare_s(PyStructObject *self) |
| 1330 | { |
| 1331 | const formatdef *f; |
| 1332 | const formatdef *e; |
| 1333 | formatcode *codes; |
Tim Peters | c2b550e | 2006-05-31 14:28:07 +0000 | [diff] [blame] | 1334 | |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1335 | const char *s; |
| 1336 | const char *fmt; |
| 1337 | char c; |
Bob Ippolito | aa70a17 | 2006-05-26 20:25:23 +0000 | [diff] [blame] | 1338 | Py_ssize_t size, len, num, itemsize, x; |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1339 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1340 | fmt = PyString_AS_STRING(self->s_format); |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1341 | |
| 1342 | f = whichtable((char **)&fmt); |
Tim Peters | c2b550e | 2006-05-31 14:28:07 +0000 | [diff] [blame] | 1343 | |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1344 | s = fmt; |
| 1345 | size = 0; |
| 1346 | len = 0; |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1347 | while ((c = *s++) != '\0') { |
| 1348 | if (isspace(Py_CHARMASK(c))) |
| 1349 | continue; |
| 1350 | if ('0' <= c && c <= '9') { |
| 1351 | num = c - '0'; |
| 1352 | while ('0' <= (c = *s++) && c <= '9') { |
| 1353 | x = num*10 + (c - '0'); |
| 1354 | if (x/10 != num) { |
| 1355 | PyErr_SetString( |
| 1356 | StructError, |
| 1357 | "overflow in item count"); |
| 1358 | return -1; |
| 1359 | } |
| 1360 | num = x; |
| 1361 | } |
| 1362 | if (c == '\0') |
| 1363 | break; |
| 1364 | } |
| 1365 | else |
| 1366 | num = 1; |
| 1367 | |
| 1368 | e = getentry(c, f); |
| 1369 | if (e == NULL) |
| 1370 | return -1; |
Tim Peters | c2b550e | 2006-05-31 14:28:07 +0000 | [diff] [blame] | 1371 | |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1372 | switch (c) { |
| 1373 | case 's': /* fall through */ |
| 1374 | case 'p': len++; break; |
| 1375 | case 'x': break; |
| 1376 | default: len += num; break; |
| 1377 | } |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1378 | |
| 1379 | itemsize = e->size; |
| 1380 | size = align(size, c, e); |
| 1381 | x = num * itemsize; |
| 1382 | size += x; |
| 1383 | if (x/itemsize != num || size < 0) { |
| 1384 | PyErr_SetString(StructError, |
| 1385 | "total struct size too long"); |
| 1386 | return -1; |
| 1387 | } |
| 1388 | } |
| 1389 | |
Gregory P. Smith | 9d53457 | 2008-06-11 07:41:16 +0000 | [diff] [blame] | 1390 | /* check for overflow */ |
| 1391 | if ((len + 1) > (PY_SSIZE_T_MAX / sizeof(formatcode))) { |
| 1392 | PyErr_NoMemory(); |
| 1393 | return -1; |
| 1394 | } |
| 1395 | |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1396 | self->s_size = size; |
| 1397 | self->s_len = len; |
Bob Ippolito | eb62127 | 2006-05-24 15:32:06 +0000 | [diff] [blame] | 1398 | codes = PyMem_MALLOC((len + 1) * sizeof(formatcode)); |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1399 | if (codes == NULL) { |
| 1400 | PyErr_NoMemory(); |
| 1401 | return -1; |
| 1402 | } |
| 1403 | self->s_codes = codes; |
Tim Peters | c2b550e | 2006-05-31 14:28:07 +0000 | [diff] [blame] | 1404 | |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1405 | s = fmt; |
| 1406 | size = 0; |
| 1407 | while ((c = *s++) != '\0') { |
| 1408 | if (isspace(Py_CHARMASK(c))) |
| 1409 | continue; |
| 1410 | if ('0' <= c && c <= '9') { |
| 1411 | num = c - '0'; |
| 1412 | while ('0' <= (c = *s++) && c <= '9') |
| 1413 | num = num*10 + (c - '0'); |
| 1414 | if (c == '\0') |
| 1415 | break; |
| 1416 | } |
| 1417 | else |
| 1418 | num = 1; |
| 1419 | |
| 1420 | e = getentry(c, f); |
Tim Peters | c2b550e | 2006-05-31 14:28:07 +0000 | [diff] [blame] | 1421 | |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1422 | size = align(size, c, e); |
Bob Ippolito | eb62127 | 2006-05-24 15:32:06 +0000 | [diff] [blame] | 1423 | if (c == 's' || c == 'p') { |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1424 | codes->offset = size; |
Bob Ippolito | eb62127 | 2006-05-24 15:32:06 +0000 | [diff] [blame] | 1425 | codes->size = num; |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1426 | codes->fmtdef = e; |
| 1427 | codes++; |
Bob Ippolito | eb62127 | 2006-05-24 15:32:06 +0000 | [diff] [blame] | 1428 | size += num; |
| 1429 | } else if (c == 'x') { |
| 1430 | size += num; |
| 1431 | } else { |
| 1432 | while (--num >= 0) { |
| 1433 | codes->offset = size; |
| 1434 | codes->size = e->size; |
| 1435 | codes->fmtdef = e; |
| 1436 | codes++; |
| 1437 | size += e->size; |
| 1438 | } |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1439 | } |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1440 | } |
| 1441 | codes->fmtdef = NULL; |
Bob Ippolito | eb62127 | 2006-05-24 15:32:06 +0000 | [diff] [blame] | 1442 | codes->offset = size; |
| 1443 | codes->size = 0; |
Tim Peters | c2b550e | 2006-05-31 14:28:07 +0000 | [diff] [blame] | 1444 | |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1445 | return 0; |
| 1446 | } |
| 1447 | |
| 1448 | static PyObject * |
| 1449 | s_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1450 | { |
| 1451 | PyObject *self; |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1452 | |
| 1453 | assert(type != NULL && type->tp_alloc != NULL); |
| 1454 | |
| 1455 | self = type->tp_alloc(type, 0); |
| 1456 | if (self != NULL) { |
| 1457 | PyStructObject *s = (PyStructObject*)self; |
| 1458 | Py_INCREF(Py_None); |
| 1459 | s->s_format = Py_None; |
| 1460 | s->s_codes = NULL; |
| 1461 | s->s_size = -1; |
| 1462 | s->s_len = -1; |
| 1463 | } |
| 1464 | return self; |
| 1465 | } |
| 1466 | |
| 1467 | static int |
| 1468 | s_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 1469 | { |
| 1470 | PyStructObject *soself = (PyStructObject *)self; |
| 1471 | PyObject *o_format = NULL; |
| 1472 | int ret = 0; |
| 1473 | static char *kwlist[] = {"format", 0}; |
| 1474 | |
| 1475 | assert(PyStruct_Check(self)); |
| 1476 | |
| 1477 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "S:Struct", kwlist, |
| 1478 | &o_format)) |
Bob Ippolito | aa70a17 | 2006-05-26 20:25:23 +0000 | [diff] [blame] | 1479 | return -1; |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1480 | |
| 1481 | Py_INCREF(o_format); |
Amaury Forgeot d'Arc | 588ff93 | 2008-02-16 14:34:57 +0000 | [diff] [blame] | 1482 | Py_CLEAR(soself->s_format); |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1483 | soself->s_format = o_format; |
Tim Peters | c2b550e | 2006-05-31 14:28:07 +0000 | [diff] [blame] | 1484 | |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1485 | ret = prepare_s(soself); |
| 1486 | return ret; |
| 1487 | } |
| 1488 | |
| 1489 | static void |
| 1490 | s_dealloc(PyStructObject *s) |
| 1491 | { |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1492 | if (s->weakreflist != NULL) |
| 1493 | PyObject_ClearWeakRefs((PyObject *)s); |
| 1494 | if (s->s_codes != NULL) { |
| 1495 | PyMem_FREE(s->s_codes); |
| 1496 | } |
| 1497 | Py_XDECREF(s->s_format); |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1498 | Py_TYPE(s)->tp_free((PyObject *)s); |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1499 | } |
| 1500 | |
Bob Ippolito | eb62127 | 2006-05-24 15:32:06 +0000 | [diff] [blame] | 1501 | static PyObject * |
| 1502 | s_unpack_internal(PyStructObject *soself, char *startfrom) { |
| 1503 | formatcode *code; |
| 1504 | Py_ssize_t i = 0; |
| 1505 | PyObject *result = PyTuple_New(soself->s_len); |
| 1506 | if (result == NULL) |
| 1507 | return NULL; |
| 1508 | |
| 1509 | for (code = soself->s_codes; code->fmtdef != NULL; code++) { |
| 1510 | PyObject *v; |
| 1511 | const formatdef *e = code->fmtdef; |
| 1512 | const char *res = startfrom + code->offset; |
| 1513 | if (e->format == 's') { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1514 | v = PyString_FromStringAndSize(res, code->size); |
Bob Ippolito | eb62127 | 2006-05-24 15:32:06 +0000 | [diff] [blame] | 1515 | } else if (e->format == 'p') { |
| 1516 | Py_ssize_t n = *(unsigned char*)res; |
| 1517 | if (n >= code->size) |
| 1518 | n = code->size - 1; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1519 | v = PyString_FromStringAndSize(res + 1, n); |
Bob Ippolito | eb62127 | 2006-05-24 15:32:06 +0000 | [diff] [blame] | 1520 | } else { |
| 1521 | v = e->unpack(res, e); |
Bob Ippolito | eb62127 | 2006-05-24 15:32:06 +0000 | [diff] [blame] | 1522 | } |
Neal Norwitz | 3c5431e | 2006-06-11 05:45:25 +0000 | [diff] [blame] | 1523 | if (v == NULL) |
| 1524 | goto fail; |
| 1525 | PyTuple_SET_ITEM(result, i++, v); |
Bob Ippolito | eb62127 | 2006-05-24 15:32:06 +0000 | [diff] [blame] | 1526 | } |
| 1527 | |
| 1528 | return result; |
| 1529 | fail: |
| 1530 | Py_DECREF(result); |
| 1531 | return NULL; |
Bob Ippolito | cd51ca5 | 2006-05-27 15:53:49 +0000 | [diff] [blame] | 1532 | } |
Bob Ippolito | eb62127 | 2006-05-24 15:32:06 +0000 | [diff] [blame] | 1533 | |
| 1534 | |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1535 | PyDoc_STRVAR(s_unpack__doc__, |
Bob Ippolito | 1fcdc23 | 2006-05-27 12:11:36 +0000 | [diff] [blame] | 1536 | "S.unpack(str) -> (v1, v2, ...)\n\ |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1537 | \n\ |
| 1538 | Return tuple containing values unpacked according to this Struct's format.\n\ |
| 1539 | Requires len(str) == self.size. See struct.__doc__ for more on format\n\ |
| 1540 | strings."); |
| 1541 | |
| 1542 | static PyObject * |
| 1543 | s_unpack(PyObject *self, PyObject *inputstr) |
| 1544 | { |
Raymond Hettinger | 7a3d41f | 2007-04-05 18:00:03 +0000 | [diff] [blame] | 1545 | char *start; |
| 1546 | Py_ssize_t len; |
| 1547 | PyObject *args=NULL, *result; |
Bob Ippolito | eb62127 | 2006-05-24 15:32:06 +0000 | [diff] [blame] | 1548 | PyStructObject *soself = (PyStructObject *)self; |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1549 | assert(PyStruct_Check(self)); |
Tim Peters | c2b550e | 2006-05-31 14:28:07 +0000 | [diff] [blame] | 1550 | assert(soself->s_codes != NULL); |
Raymond Hettinger | 7a3d41f | 2007-04-05 18:00:03 +0000 | [diff] [blame] | 1551 | if (inputstr == NULL) |
| 1552 | goto fail; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1553 | if (PyString_Check(inputstr) && |
| 1554 | PyString_GET_SIZE(inputstr) == soself->s_size) { |
| 1555 | return s_unpack_internal(soself, PyString_AS_STRING(inputstr)); |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1556 | } |
Raymond Hettinger | 7a3d41f | 2007-04-05 18:00:03 +0000 | [diff] [blame] | 1557 | args = PyTuple_Pack(1, inputstr); |
| 1558 | if (args == NULL) |
| 1559 | return NULL; |
| 1560 | if (!PyArg_ParseTuple(args, "s#:unpack", &start, &len)) |
| 1561 | goto fail; |
| 1562 | if (soself->s_size != len) |
| 1563 | goto fail; |
| 1564 | result = s_unpack_internal(soself, start); |
| 1565 | Py_DECREF(args); |
| 1566 | return result; |
| 1567 | |
| 1568 | fail: |
| 1569 | Py_XDECREF(args); |
| 1570 | PyErr_Format(StructError, |
| 1571 | "unpack requires a string argument of length %zd", |
| 1572 | soself->s_size); |
| 1573 | return NULL; |
Bob Ippolito | eb62127 | 2006-05-24 15:32:06 +0000 | [diff] [blame] | 1574 | } |
| 1575 | |
| 1576 | PyDoc_STRVAR(s_unpack_from__doc__, |
Bob Ippolito | 1fcdc23 | 2006-05-27 12:11:36 +0000 | [diff] [blame] | 1577 | "S.unpack_from(buffer[, offset]) -> (v1, v2, ...)\n\ |
Bob Ippolito | eb62127 | 2006-05-24 15:32:06 +0000 | [diff] [blame] | 1578 | \n\ |
| 1579 | Return tuple containing values unpacked according to this Struct's format.\n\ |
| 1580 | Unlike unpack, unpack_from can unpack values from any object supporting\n\ |
| 1581 | the buffer API, not just str. Requires len(buffer[offset:]) >= self.size.\n\ |
| 1582 | See struct.__doc__ for more on format strings."); |
| 1583 | |
| 1584 | static PyObject * |
| 1585 | s_unpack_from(PyObject *self, PyObject *args, PyObject *kwds) |
| 1586 | { |
| 1587 | static char *kwlist[] = {"buffer", "offset", 0}; |
| 1588 | #if (PY_VERSION_HEX < 0x02050000) |
| 1589 | static char *fmt = "z#|i:unpack_from"; |
| 1590 | #else |
| 1591 | static char *fmt = "z#|n:unpack_from"; |
| 1592 | #endif |
| 1593 | Py_ssize_t buffer_len = 0, offset = 0; |
| 1594 | char *buffer = NULL; |
| 1595 | PyStructObject *soself = (PyStructObject *)self; |
| 1596 | assert(PyStruct_Check(self)); |
| 1597 | assert(soself->s_codes != NULL); |
| 1598 | |
| 1599 | if (!PyArg_ParseTupleAndKeywords(args, kwds, fmt, kwlist, |
| 1600 | &buffer, &buffer_len, &offset)) |
Bob Ippolito | aa70a17 | 2006-05-26 20:25:23 +0000 | [diff] [blame] | 1601 | return NULL; |
Bob Ippolito | eb62127 | 2006-05-24 15:32:06 +0000 | [diff] [blame] | 1602 | |
| 1603 | if (buffer == NULL) { |
| 1604 | PyErr_Format(StructError, |
| 1605 | "unpack_from requires a buffer argument"); |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1606 | return NULL; |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1607 | } |
Tim Peters | c2b550e | 2006-05-31 14:28:07 +0000 | [diff] [blame] | 1608 | |
Bob Ippolito | eb62127 | 2006-05-24 15:32:06 +0000 | [diff] [blame] | 1609 | if (offset < 0) |
| 1610 | offset += buffer_len; |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1611 | |
Bob Ippolito | eb62127 | 2006-05-24 15:32:06 +0000 | [diff] [blame] | 1612 | if (offset < 0 || (buffer_len - offset) < soself->s_size) { |
| 1613 | PyErr_Format(StructError, |
Bob Ippolito | aa70a17 | 2006-05-26 20:25:23 +0000 | [diff] [blame] | 1614 | "unpack_from requires a buffer of at least %zd bytes", |
Bob Ippolito | eb62127 | 2006-05-24 15:32:06 +0000 | [diff] [blame] | 1615 | soself->s_size); |
| 1616 | return NULL; |
| 1617 | } |
| 1618 | return s_unpack_internal(soself, buffer + offset); |
| 1619 | } |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1620 | |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1621 | |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 1622 | /* |
| 1623 | * Guts of the pack function. |
| 1624 | * |
| 1625 | * Takes a struct object, a tuple of arguments, and offset in that tuple of |
| 1626 | * argument for where to start processing the arguments for packing, and a |
| 1627 | * character buffer for writing the packed string. The caller must insure |
| 1628 | * that the buffer may contain the required length for packing the arguments. |
| 1629 | * 0 is returned on success, 1 is returned if there is an error. |
| 1630 | * |
| 1631 | */ |
| 1632 | static int |
| 1633 | s_pack_internal(PyStructObject *soself, PyObject *args, int offset, char* buf) |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1634 | { |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1635 | formatcode *code; |
Neal Norwitz | 3c5431e | 2006-06-11 05:45:25 +0000 | [diff] [blame] | 1636 | /* XXX(nnorwitz): why does i need to be a local? can we use |
| 1637 | the offset parameter or do we need the wider width? */ |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1638 | Py_ssize_t i; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 1639 | |
| 1640 | memset(buf, '\0', soself->s_size); |
| 1641 | i = offset; |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1642 | for (code = soself->s_codes; code->fmtdef != NULL; code++) { |
| 1643 | Py_ssize_t n; |
Neal Norwitz | 3c5431e | 2006-06-11 05:45:25 +0000 | [diff] [blame] | 1644 | PyObject *v = PyTuple_GET_ITEM(args, i++); |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1645 | const formatdef *e = code->fmtdef; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 1646 | char *res = buf + code->offset; |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1647 | if (e->format == 's') { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1648 | if (!PyString_Check(v)) { |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1649 | PyErr_SetString(StructError, |
| 1650 | "argument for 's' must be a string"); |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 1651 | return -1; |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1652 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1653 | n = PyString_GET_SIZE(v); |
Bob Ippolito | eb62127 | 2006-05-24 15:32:06 +0000 | [diff] [blame] | 1654 | if (n > code->size) |
| 1655 | n = code->size; |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1656 | if (n > 0) |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1657 | memcpy(res, PyString_AS_STRING(v), n); |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1658 | } else if (e->format == 'p') { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1659 | if (!PyString_Check(v)) { |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1660 | PyErr_SetString(StructError, |
| 1661 | "argument for 'p' must be a string"); |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 1662 | return -1; |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1663 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1664 | n = PyString_GET_SIZE(v); |
Bob Ippolito | eb62127 | 2006-05-24 15:32:06 +0000 | [diff] [blame] | 1665 | if (n > (code->size - 1)) |
| 1666 | n = code->size - 1; |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1667 | if (n > 0) |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1668 | memcpy(res + 1, PyString_AS_STRING(v), n); |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1669 | if (n > 255) |
| 1670 | n = 255; |
| 1671 | *res = Py_SAFE_DOWNCAST(n, Py_ssize_t, unsigned char); |
| 1672 | } else { |
Bob Ippolito | 2fd3977 | 2006-05-29 22:55:48 +0000 | [diff] [blame] | 1673 | if (e->pack(res, v, e) < 0) { |
| 1674 | if (PyLong_Check(v) && PyErr_ExceptionMatches(PyExc_OverflowError)) |
| 1675 | PyErr_SetString(StructError, |
| 1676 | "long too large to convert to int"); |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 1677 | return -1; |
Bob Ippolito | 2fd3977 | 2006-05-29 22:55:48 +0000 | [diff] [blame] | 1678 | } |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1679 | } |
| 1680 | } |
Tim Peters | c2b550e | 2006-05-31 14:28:07 +0000 | [diff] [blame] | 1681 | |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 1682 | /* Success */ |
| 1683 | return 0; |
| 1684 | } |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1685 | |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 1686 | |
| 1687 | PyDoc_STRVAR(s_pack__doc__, |
Bob Ippolito | 1fcdc23 | 2006-05-27 12:11:36 +0000 | [diff] [blame] | 1688 | "S.pack(v1, v2, ...) -> string\n\ |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 1689 | \n\ |
| 1690 | Return a string containing values v1, v2, ... packed according to this\n\ |
| 1691 | Struct's format. See struct.__doc__ for more on format strings."); |
| 1692 | |
| 1693 | static PyObject * |
| 1694 | s_pack(PyObject *self, PyObject *args) |
| 1695 | { |
| 1696 | PyStructObject *soself; |
| 1697 | PyObject *result; |
| 1698 | |
| 1699 | /* Validate arguments. */ |
| 1700 | soself = (PyStructObject *)self; |
| 1701 | assert(PyStruct_Check(self)); |
| 1702 | assert(soself->s_codes != NULL); |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 1703 | if (PyTuple_GET_SIZE(args) != soself->s_len) |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 1704 | { |
| 1705 | PyErr_Format(StructError, |
Bob Ippolito | aa70a17 | 2006-05-26 20:25:23 +0000 | [diff] [blame] | 1706 | "pack requires exactly %zd arguments", soself->s_len); |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 1707 | return NULL; |
| 1708 | } |
Tim Peters | c2b550e | 2006-05-31 14:28:07 +0000 | [diff] [blame] | 1709 | |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 1710 | /* Allocate a new string */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1711 | result = PyString_FromStringAndSize((char *)NULL, soself->s_size); |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 1712 | if (result == NULL) |
| 1713 | return NULL; |
Tim Peters | c2b550e | 2006-05-31 14:28:07 +0000 | [diff] [blame] | 1714 | |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 1715 | /* Call the guts */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1716 | if ( s_pack_internal(soself, args, 0, PyString_AS_STRING(result)) != 0 ) { |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 1717 | Py_DECREF(result); |
| 1718 | return NULL; |
| 1719 | } |
| 1720 | |
| 1721 | return result; |
| 1722 | } |
| 1723 | |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 1724 | PyDoc_STRVAR(s_pack_into__doc__, |
| 1725 | "S.pack_into(buffer, offset, v1, v2, ...)\n\ |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 1726 | \n\ |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 1727 | Pack the values v1, v2, ... according to this Struct's format, write \n\ |
Bob Ippolito | 1fcdc23 | 2006-05-27 12:11:36 +0000 | [diff] [blame] | 1728 | the packed bytes into the writable buffer buf starting at offset. Note\n\ |
| 1729 | that the offset is not an optional argument. See struct.__doc__ for \n\ |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 1730 | more on format strings."); |
| 1731 | |
| 1732 | static PyObject * |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 1733 | s_pack_into(PyObject *self, PyObject *args) |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 1734 | { |
| 1735 | PyStructObject *soself; |
| 1736 | char *buffer; |
| 1737 | Py_ssize_t buffer_len, offset; |
| 1738 | |
| 1739 | /* Validate arguments. +1 is for the first arg as buffer. */ |
| 1740 | soself = (PyStructObject *)self; |
| 1741 | assert(PyStruct_Check(self)); |
| 1742 | assert(soself->s_codes != NULL); |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 1743 | if (PyTuple_GET_SIZE(args) != (soself->s_len + 2)) |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 1744 | { |
| 1745 | PyErr_Format(StructError, |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 1746 | "pack_into requires exactly %zd arguments", |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 1747 | (soself->s_len + 2)); |
| 1748 | return NULL; |
| 1749 | } |
| 1750 | |
| 1751 | /* Extract a writable memory buffer from the first argument */ |
Tim Peters | c2b550e | 2006-05-31 14:28:07 +0000 | [diff] [blame] | 1752 | if ( PyObject_AsWriteBuffer(PyTuple_GET_ITEM(args, 0), |
| 1753 | (void**)&buffer, &buffer_len) == -1 ) { |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 1754 | return NULL; |
Bob Ippolito | aa70a17 | 2006-05-26 20:25:23 +0000 | [diff] [blame] | 1755 | } |
| 1756 | assert( buffer_len >= 0 ); |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 1757 | |
| 1758 | /* Extract the offset from the first argument */ |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 1759 | offset = PyInt_AsSsize_t(PyTuple_GET_ITEM(args, 1)); |
Benjamin Peterson | 0225248 | 2008-09-30 02:11:07 +0000 | [diff] [blame] | 1760 | if (offset == -1 && PyErr_Occurred()) |
| 1761 | return NULL; |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 1762 | |
Bob Ippolito | aa70a17 | 2006-05-26 20:25:23 +0000 | [diff] [blame] | 1763 | /* Support negative offsets. */ |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 1764 | if (offset < 0) |
| 1765 | offset += buffer_len; |
| 1766 | |
| 1767 | /* Check boundaries */ |
| 1768 | if (offset < 0 || (buffer_len - offset) < soself->s_size) { |
| 1769 | PyErr_Format(StructError, |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 1770 | "pack_into requires a buffer of at least %zd bytes", |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 1771 | soself->s_size); |
| 1772 | return NULL; |
| 1773 | } |
Tim Peters | c2b550e | 2006-05-31 14:28:07 +0000 | [diff] [blame] | 1774 | |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 1775 | /* Call the guts */ |
| 1776 | if ( s_pack_internal(soself, args, 2, buffer + offset) != 0 ) { |
| 1777 | return NULL; |
| 1778 | } |
| 1779 | |
Georg Brandl | c26025c | 2006-05-28 21:42:54 +0000 | [diff] [blame] | 1780 | Py_RETURN_NONE; |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1781 | } |
| 1782 | |
Bob Ippolito | aa70a17 | 2006-05-26 20:25:23 +0000 | [diff] [blame] | 1783 | static PyObject * |
| 1784 | s_get_format(PyStructObject *self, void *unused) |
| 1785 | { |
| 1786 | Py_INCREF(self->s_format); |
| 1787 | return self->s_format; |
| 1788 | } |
| 1789 | |
| 1790 | static PyObject * |
| 1791 | s_get_size(PyStructObject *self, void *unused) |
| 1792 | { |
| 1793 | return PyInt_FromSsize_t(self->s_size); |
| 1794 | } |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1795 | |
| 1796 | /* List of functions */ |
| 1797 | |
| 1798 | static struct PyMethodDef s_methods[] = { |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 1799 | {"pack", s_pack, METH_VARARGS, s_pack__doc__}, |
| 1800 | {"pack_into", s_pack_into, METH_VARARGS, s_pack_into__doc__}, |
| 1801 | {"unpack", s_unpack, METH_O, s_unpack__doc__}, |
Neal Norwitz | a84dcd7 | 2007-05-22 07:16:44 +0000 | [diff] [blame] | 1802 | {"unpack_from", (PyCFunction)s_unpack_from, METH_VARARGS|METH_KEYWORDS, |
Tim Peters | 5ec2e85 | 2006-06-04 15:49:07 +0000 | [diff] [blame] | 1803 | s_unpack_from__doc__}, |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1804 | {NULL, NULL} /* sentinel */ |
| 1805 | }; |
| 1806 | |
| 1807 | PyDoc_STRVAR(s__doc__, "Compiled struct object"); |
| 1808 | |
| 1809 | #define OFF(x) offsetof(PyStructObject, x) |
| 1810 | |
Bob Ippolito | aa70a17 | 2006-05-26 20:25:23 +0000 | [diff] [blame] | 1811 | static PyGetSetDef s_getsetlist[] = { |
Bob Ippolito | 1fcdc23 | 2006-05-27 12:11:36 +0000 | [diff] [blame] | 1812 | {"format", (getter)s_get_format, (setter)NULL, "struct format string", NULL}, |
| 1813 | {"size", (getter)s_get_size, (setter)NULL, "struct size in bytes", NULL}, |
Bob Ippolito | aa70a17 | 2006-05-26 20:25:23 +0000 | [diff] [blame] | 1814 | {NULL} /* sentinel */ |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1815 | }; |
| 1816 | |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1817 | static |
| 1818 | PyTypeObject PyStructType = { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 1819 | PyVarObject_HEAD_INIT(NULL, 0) |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1820 | "Struct", |
| 1821 | sizeof(PyStructObject), |
| 1822 | 0, |
Bob Ippolito | aa70a17 | 2006-05-26 20:25:23 +0000 | [diff] [blame] | 1823 | (destructor)s_dealloc, /* tp_dealloc */ |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1824 | 0, /* tp_print */ |
| 1825 | 0, /* tp_getattr */ |
| 1826 | 0, /* tp_setattr */ |
| 1827 | 0, /* tp_compare */ |
| 1828 | 0, /* tp_repr */ |
| 1829 | 0, /* tp_as_number */ |
| 1830 | 0, /* tp_as_sequence */ |
| 1831 | 0, /* tp_as_mapping */ |
| 1832 | 0, /* tp_hash */ |
| 1833 | 0, /* tp_call */ |
| 1834 | 0, /* tp_str */ |
Bob Ippolito | aa70a17 | 2006-05-26 20:25:23 +0000 | [diff] [blame] | 1835 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1836 | PyObject_GenericSetAttr, /* tp_setattro */ |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1837 | 0, /* tp_as_buffer */ |
Bob Ippolito | aa70a17 | 2006-05-26 20:25:23 +0000 | [diff] [blame] | 1838 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_WEAKREFS,/* tp_flags */ |
| 1839 | s__doc__, /* tp_doc */ |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1840 | 0, /* tp_traverse */ |
| 1841 | 0, /* tp_clear */ |
| 1842 | 0, /* tp_richcompare */ |
| 1843 | offsetof(PyStructObject, weakreflist), /* tp_weaklistoffset */ |
| 1844 | 0, /* tp_iter */ |
| 1845 | 0, /* tp_iternext */ |
Bob Ippolito | aa70a17 | 2006-05-26 20:25:23 +0000 | [diff] [blame] | 1846 | s_methods, /* tp_methods */ |
| 1847 | NULL, /* tp_members */ |
| 1848 | s_getsetlist, /* tp_getset */ |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1849 | 0, /* tp_base */ |
| 1850 | 0, /* tp_dict */ |
| 1851 | 0, /* tp_descr_get */ |
| 1852 | 0, /* tp_descr_set */ |
| 1853 | 0, /* tp_dictoffset */ |
Bob Ippolito | aa70a17 | 2006-05-26 20:25:23 +0000 | [diff] [blame] | 1854 | s_init, /* tp_init */ |
| 1855 | PyType_GenericAlloc,/* tp_alloc */ |
| 1856 | s_new, /* tp_new */ |
| 1857 | PyObject_Del, /* tp_free */ |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 1858 | }; |
| 1859 | |
Raymond Hettinger | 2f6621c | 2008-01-04 00:01:15 +0000 | [diff] [blame] | 1860 | |
| 1861 | /* ---- Standalone functions ---- */ |
| 1862 | |
| 1863 | #define MAXCACHE 100 |
Christian Heimes | 76d19f6 | 2008-01-04 02:54:42 +0000 | [diff] [blame] | 1864 | static PyObject *cache = NULL; |
Raymond Hettinger | 2f6621c | 2008-01-04 00:01:15 +0000 | [diff] [blame] | 1865 | |
| 1866 | static PyObject * |
| 1867 | cache_struct(PyObject *fmt) |
| 1868 | { |
Raymond Hettinger | 2f6621c | 2008-01-04 00:01:15 +0000 | [diff] [blame] | 1869 | PyObject * s_object; |
| 1870 | |
| 1871 | if (cache == NULL) { |
| 1872 | cache = PyDict_New(); |
| 1873 | if (cache == NULL) |
| 1874 | return NULL; |
| 1875 | } |
| 1876 | |
| 1877 | s_object = PyDict_GetItem(cache, fmt); |
| 1878 | if (s_object != NULL) { |
| 1879 | Py_INCREF(s_object); |
| 1880 | return s_object; |
| 1881 | } |
| 1882 | |
| 1883 | s_object = PyObject_CallFunctionObjArgs((PyObject *)(&PyStructType), fmt, NULL); |
| 1884 | if (s_object != NULL) { |
| 1885 | if (PyDict_Size(cache) >= MAXCACHE) |
| 1886 | PyDict_Clear(cache); |
| 1887 | /* Attempt to cache the result */ |
| 1888 | if (PyDict_SetItem(cache, fmt, s_object) == -1) |
| 1889 | PyErr_Clear(); |
| 1890 | } |
| 1891 | return s_object; |
| 1892 | } |
| 1893 | |
Christian Heimes | 76d19f6 | 2008-01-04 02:54:42 +0000 | [diff] [blame] | 1894 | PyDoc_STRVAR(clearcache_doc, |
| 1895 | "Clear the internal cache."); |
| 1896 | |
| 1897 | static PyObject * |
| 1898 | clearcache(PyObject *self) |
| 1899 | { |
Raymond Hettinger | 18e08e5 | 2008-01-18 00:10:42 +0000 | [diff] [blame] | 1900 | Py_CLEAR(cache); |
Christian Heimes | 76d19f6 | 2008-01-04 02:54:42 +0000 | [diff] [blame] | 1901 | Py_RETURN_NONE; |
| 1902 | } |
| 1903 | |
Raymond Hettinger | 2f6621c | 2008-01-04 00:01:15 +0000 | [diff] [blame] | 1904 | PyDoc_STRVAR(calcsize_doc, |
| 1905 | "Return size of C struct described by format string fmt."); |
| 1906 | |
| 1907 | static PyObject * |
| 1908 | calcsize(PyObject *self, PyObject *fmt) |
| 1909 | { |
| 1910 | Py_ssize_t n; |
| 1911 | PyObject *s_object = cache_struct(fmt); |
| 1912 | if (s_object == NULL) |
| 1913 | return NULL; |
| 1914 | n = ((PyStructObject *)s_object)->s_size; |
| 1915 | Py_DECREF(s_object); |
| 1916 | return PyInt_FromSsize_t(n); |
| 1917 | } |
| 1918 | |
| 1919 | PyDoc_STRVAR(pack_doc, |
| 1920 | "Return string containing values v1, v2, ... packed according to fmt."); |
| 1921 | |
| 1922 | static PyObject * |
| 1923 | pack(PyObject *self, PyObject *args) |
| 1924 | { |
| 1925 | PyObject *s_object, *fmt, *newargs, *result; |
| 1926 | Py_ssize_t n = PyTuple_GET_SIZE(args); |
| 1927 | |
| 1928 | if (n == 0) { |
| 1929 | PyErr_SetString(PyExc_TypeError, "missing format argument"); |
| 1930 | return NULL; |
| 1931 | } |
| 1932 | fmt = PyTuple_GET_ITEM(args, 0); |
| 1933 | newargs = PyTuple_GetSlice(args, 1, n); |
| 1934 | if (newargs == NULL) |
| 1935 | return NULL; |
| 1936 | |
| 1937 | s_object = cache_struct(fmt); |
| 1938 | if (s_object == NULL) { |
| 1939 | Py_DECREF(newargs); |
| 1940 | return NULL; |
| 1941 | } |
| 1942 | result = s_pack(s_object, newargs); |
| 1943 | Py_DECREF(newargs); |
| 1944 | Py_DECREF(s_object); |
| 1945 | return result; |
| 1946 | } |
| 1947 | |
| 1948 | PyDoc_STRVAR(pack_into_doc, |
| 1949 | "Pack the values v1, v2, ... according to fmt.\n\ |
| 1950 | Write the packed bytes into the writable buffer buf starting at offset."); |
| 1951 | |
| 1952 | static PyObject * |
| 1953 | pack_into(PyObject *self, PyObject *args) |
| 1954 | { |
| 1955 | PyObject *s_object, *fmt, *newargs, *result; |
| 1956 | Py_ssize_t n = PyTuple_GET_SIZE(args); |
| 1957 | |
| 1958 | if (n == 0) { |
| 1959 | PyErr_SetString(PyExc_TypeError, "missing format argument"); |
| 1960 | return NULL; |
| 1961 | } |
| 1962 | fmt = PyTuple_GET_ITEM(args, 0); |
| 1963 | newargs = PyTuple_GetSlice(args, 1, n); |
| 1964 | if (newargs == NULL) |
| 1965 | return NULL; |
| 1966 | |
| 1967 | s_object = cache_struct(fmt); |
| 1968 | if (s_object == NULL) { |
| 1969 | Py_DECREF(newargs); |
| 1970 | return NULL; |
| 1971 | } |
| 1972 | result = s_pack_into(s_object, newargs); |
| 1973 | Py_DECREF(newargs); |
| 1974 | Py_DECREF(s_object); |
| 1975 | return result; |
| 1976 | } |
| 1977 | |
| 1978 | PyDoc_STRVAR(unpack_doc, |
| 1979 | "Unpack the string containing packed C structure data, according to fmt.\n\ |
| 1980 | Requires len(string) == calcsize(fmt)."); |
| 1981 | |
| 1982 | static PyObject * |
| 1983 | unpack(PyObject *self, PyObject *args) |
| 1984 | { |
| 1985 | PyObject *s_object, *fmt, *inputstr, *result; |
| 1986 | |
| 1987 | if (!PyArg_UnpackTuple(args, "unpack", 2, 2, &fmt, &inputstr)) |
| 1988 | return NULL; |
| 1989 | |
| 1990 | s_object = cache_struct(fmt); |
| 1991 | if (s_object == NULL) |
| 1992 | return NULL; |
| 1993 | result = s_unpack(s_object, inputstr); |
| 1994 | Py_DECREF(s_object); |
| 1995 | return result; |
| 1996 | } |
| 1997 | |
| 1998 | PyDoc_STRVAR(unpack_from_doc, |
| 1999 | "Unpack the buffer, containing packed C structure data, according to\n\ |
| 2000 | fmt, starting at offset. Requires len(buffer[offset:]) >= calcsize(fmt)."); |
| 2001 | |
| 2002 | static PyObject * |
| 2003 | unpack_from(PyObject *self, PyObject *args, PyObject *kwds) |
| 2004 | { |
| 2005 | PyObject *s_object, *fmt, *newargs, *result; |
| 2006 | Py_ssize_t n = PyTuple_GET_SIZE(args); |
| 2007 | |
| 2008 | if (n == 0) { |
| 2009 | PyErr_SetString(PyExc_TypeError, "missing format argument"); |
| 2010 | return NULL; |
| 2011 | } |
| 2012 | fmt = PyTuple_GET_ITEM(args, 0); |
| 2013 | newargs = PyTuple_GetSlice(args, 1, n); |
| 2014 | if (newargs == NULL) |
| 2015 | return NULL; |
| 2016 | |
| 2017 | s_object = cache_struct(fmt); |
| 2018 | if (s_object == NULL) { |
| 2019 | Py_DECREF(newargs); |
| 2020 | return NULL; |
| 2021 | } |
| 2022 | result = s_unpack_from(s_object, newargs, kwds); |
| 2023 | Py_DECREF(newargs); |
| 2024 | Py_DECREF(s_object); |
| 2025 | return result; |
| 2026 | } |
| 2027 | |
| 2028 | static struct PyMethodDef module_functions[] = { |
Christian Heimes | 76d19f6 | 2008-01-04 02:54:42 +0000 | [diff] [blame] | 2029 | {"_clearcache", (PyCFunction)clearcache, METH_NOARGS, clearcache_doc}, |
Raymond Hettinger | 2f6621c | 2008-01-04 00:01:15 +0000 | [diff] [blame] | 2030 | {"calcsize", calcsize, METH_O, calcsize_doc}, |
| 2031 | {"pack", pack, METH_VARARGS, pack_doc}, |
| 2032 | {"pack_into", pack_into, METH_VARARGS, pack_into_doc}, |
| 2033 | {"unpack", unpack, METH_VARARGS, unpack_doc}, |
| 2034 | {"unpack_from", (PyCFunction)unpack_from, |
| 2035 | METH_VARARGS|METH_KEYWORDS, unpack_from_doc}, |
| 2036 | {NULL, NULL} /* sentinel */ |
| 2037 | }; |
| 2038 | |
| 2039 | |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 2040 | /* Module initialization */ |
| 2041 | |
Raymond Hettinger | 2f6621c | 2008-01-04 00:01:15 +0000 | [diff] [blame] | 2042 | PyDoc_STRVAR(module_doc, |
| 2043 | "Functions to convert between Python values and C structs.\n\ |
| 2044 | Python strings are used to hold the data representing the C struct\n\ |
| 2045 | and also as format strings to describe the layout of data in the C struct.\n\ |
| 2046 | \n\ |
| 2047 | The optional first format char indicates byte order, size and alignment:\n\ |
| 2048 | @: native order, size & alignment (default)\n\ |
| 2049 | =: native order, std. size & alignment\n\ |
| 2050 | <: little-endian, std. size & alignment\n\ |
| 2051 | >: big-endian, std. size & alignment\n\ |
| 2052 | !: same as >\n\ |
| 2053 | \n\ |
| 2054 | The remaining chars indicate types of args and must match exactly;\n\ |
| 2055 | these can be preceded by a decimal repeat count:\n\ |
| 2056 | x: pad byte (no data); c:char; b:signed byte; B:unsigned byte;\n\ |
| 2057 | h:short; H:unsigned short; i:int; I:unsigned int;\n\ |
| 2058 | l:long; L:unsigned long; f:float; d:double.\n\ |
| 2059 | Special cases (preceding decimal count indicates length):\n\ |
| 2060 | s:string (array of char); p: pascal string (with count byte).\n\ |
| 2061 | Special case (only available in native format):\n\ |
| 2062 | P:an integer type that is wide enough to hold a pointer.\n\ |
| 2063 | Special case (not in native mode unless 'long long' in platform C):\n\ |
| 2064 | q:long long; Q:unsigned long long\n\ |
| 2065 | Whitespace between formats is ignored.\n\ |
| 2066 | \n\ |
| 2067 | The variable struct.error is an exception raised on errors.\n"); |
| 2068 | |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 2069 | PyMODINIT_FUNC |
| 2070 | init_struct(void) |
| 2071 | { |
Raymond Hettinger | 2f6621c | 2008-01-04 00:01:15 +0000 | [diff] [blame] | 2072 | PyObject *ver, *m; |
| 2073 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2074 | ver = PyString_FromString("0.2"); |
Raymond Hettinger | 2f6621c | 2008-01-04 00:01:15 +0000 | [diff] [blame] | 2075 | if (ver == NULL) |
| 2076 | return; |
| 2077 | |
| 2078 | m = Py_InitModule3("_struct", module_functions, module_doc); |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 2079 | if (m == NULL) |
| 2080 | return; |
| 2081 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 2082 | Py_TYPE(&PyStructType) = &PyType_Type; |
Bob Ippolito | 3fc2bb9 | 2006-05-25 19:03:19 +0000 | [diff] [blame] | 2083 | if (PyType_Ready(&PyStructType) < 0) |
| 2084 | return; |
| 2085 | |
Bob Ippolito | 4182a75 | 2006-05-30 17:37:54 +0000 | [diff] [blame] | 2086 | #ifdef PY_STRUCT_OVERFLOW_MASKING |
Bob Ippolito | 2fd3977 | 2006-05-29 22:55:48 +0000 | [diff] [blame] | 2087 | if (pyint_zero == NULL) { |
| 2088 | pyint_zero = PyInt_FromLong(0); |
| 2089 | if (pyint_zero == NULL) |
| 2090 | return; |
| 2091 | } |
| 2092 | if (pylong_ulong_mask == NULL) { |
| 2093 | #if (SIZEOF_LONG == 4) |
| 2094 | pylong_ulong_mask = PyLong_FromString("FFFFFFFF", NULL, 16); |
| 2095 | #else |
| 2096 | pylong_ulong_mask = PyLong_FromString("FFFFFFFFFFFFFFFF", NULL, 16); |
| 2097 | #endif |
| 2098 | if (pylong_ulong_mask == NULL) |
| 2099 | return; |
| 2100 | } |
| 2101 | |
Tim Peters | c2b550e | 2006-05-31 14:28:07 +0000 | [diff] [blame] | 2102 | #else |
Bob Ippolito | 4182a75 | 2006-05-30 17:37:54 +0000 | [diff] [blame] | 2103 | /* This speed trick can't be used until overflow masking goes away, because |
| 2104 | native endian always raises exceptions instead of overflow masking. */ |
Tim Peters | c2b550e | 2006-05-31 14:28:07 +0000 | [diff] [blame] | 2105 | |
Bob Ippolito | a99865b | 2006-05-25 19:56:56 +0000 | [diff] [blame] | 2106 | /* Check endian and swap in faster functions */ |
| 2107 | { |
| 2108 | int one = 1; |
| 2109 | formatdef *native = native_table; |
| 2110 | formatdef *other, *ptr; |
| 2111 | if ((int)*(unsigned char*)&one) |
| 2112 | other = lilendian_table; |
| 2113 | else |
| 2114 | other = bigendian_table; |
Bob Ippolito | 964e02a | 2006-05-25 21:09:45 +0000 | [diff] [blame] | 2115 | /* Scan through the native table, find a matching |
| 2116 | entry in the endian table and swap in the |
| 2117 | native implementations whenever possible |
| 2118 | (64-bit platforms may not have "standard" sizes) */ |
Bob Ippolito | a99865b | 2006-05-25 19:56:56 +0000 | [diff] [blame] | 2119 | while (native->format != '\0' && other->format != '\0') { |
| 2120 | ptr = other; |
| 2121 | while (ptr->format != '\0') { |
| 2122 | if (ptr->format == native->format) { |
Bob Ippolito | 964e02a | 2006-05-25 21:09:45 +0000 | [diff] [blame] | 2123 | /* Match faster when formats are |
| 2124 | listed in the same order */ |
Bob Ippolito | a99865b | 2006-05-25 19:56:56 +0000 | [diff] [blame] | 2125 | if (ptr == other) |
| 2126 | other++; |
Tim Peters | c2b550e | 2006-05-31 14:28:07 +0000 | [diff] [blame] | 2127 | /* Only use the trick if the |
Bob Ippolito | 964e02a | 2006-05-25 21:09:45 +0000 | [diff] [blame] | 2128 | size matches */ |
| 2129 | if (ptr->size != native->size) |
| 2130 | break; |
| 2131 | /* Skip float and double, could be |
| 2132 | "unknown" float format */ |
| 2133 | if (ptr->format == 'd' || ptr->format == 'f') |
| 2134 | break; |
| 2135 | ptr->pack = native->pack; |
| 2136 | ptr->unpack = native->unpack; |
Bob Ippolito | a99865b | 2006-05-25 19:56:56 +0000 | [diff] [blame] | 2137 | break; |
| 2138 | } |
| 2139 | ptr++; |
| 2140 | } |
| 2141 | native++; |
| 2142 | } |
| 2143 | } |
Bob Ippolito | 2fd3977 | 2006-05-29 22:55:48 +0000 | [diff] [blame] | 2144 | #endif |
Tim Peters | c2b550e | 2006-05-31 14:28:07 +0000 | [diff] [blame] | 2145 | |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 2146 | /* Add some symbolic constants to the module */ |
| 2147 | if (StructError == NULL) { |
| 2148 | StructError = PyErr_NewException("struct.error", NULL, NULL); |
| 2149 | if (StructError == NULL) |
| 2150 | return; |
| 2151 | } |
Bob Ippolito | 04ab994 | 2006-05-25 19:33:38 +0000 | [diff] [blame] | 2152 | |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 2153 | Py_INCREF(StructError); |
| 2154 | PyModule_AddObject(m, "error", StructError); |
Bob Ippolito | 04ab994 | 2006-05-25 19:33:38 +0000 | [diff] [blame] | 2155 | |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 2156 | Py_INCREF((PyObject*)&PyStructType); |
| 2157 | PyModule_AddObject(m, "Struct", (PyObject*)&PyStructType); |
Tim Peters | c2b550e | 2006-05-31 14:28:07 +0000 | [diff] [blame] | 2158 | |
Raymond Hettinger | 2f6621c | 2008-01-04 00:01:15 +0000 | [diff] [blame] | 2159 | PyModule_AddObject(m, "__version__", ver); |
| 2160 | |
Bob Ippolito | 2fd3977 | 2006-05-29 22:55:48 +0000 | [diff] [blame] | 2161 | PyModule_AddIntConstant(m, "_PY_STRUCT_RANGE_CHECKING", 1); |
Bob Ippolito | 4182a75 | 2006-05-30 17:37:54 +0000 | [diff] [blame] | 2162 | #ifdef PY_STRUCT_OVERFLOW_MASKING |
| 2163 | PyModule_AddIntConstant(m, "_PY_STRUCT_OVERFLOW_MASKING", 1); |
Bob Ippolito | 2fd3977 | 2006-05-29 22:55:48 +0000 | [diff] [blame] | 2164 | #endif |
Bob Ippolito | e6c9f98 | 2006-08-04 23:59:21 +0000 | [diff] [blame] | 2165 | #ifdef PY_STRUCT_FLOAT_COERCE |
| 2166 | PyModule_AddIntConstant(m, "_PY_STRUCT_FLOAT_COERCE", 1); |
| 2167 | #endif |
| 2168 | |
Bob Ippolito | 232f3c9 | 2006-05-23 19:12:41 +0000 | [diff] [blame] | 2169 | } |