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