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