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