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