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 (PyUnicode_Check(v)) { |
| 466 | v = _PyUnicode_AsDefaultEncodedString(v, NULL); |
| 467 | if (v == NULL) |
| 468 | return -1; |
| 469 | } |
| 470 | if (!PyBytes_Check(v) || PyBytes_Size(v) != 1) { |
| 471 | PyErr_SetString(StructError, |
| 472 | "char format requires bytes or string of length 1"); |
| 473 | return -1; |
| 474 | } |
| 475 | *p = *PyBytes_AsString(v); |
| 476 | return 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | static int |
| 480 | np_short(char *p, PyObject *v, const formatdef *f) |
| 481 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 482 | long x; |
| 483 | short y; |
| 484 | if (get_long(v, &x) < 0) |
| 485 | return -1; |
| 486 | if (x < SHRT_MIN || x > SHRT_MAX){ |
| 487 | PyErr_SetString(StructError, |
| 488 | "short format requires " STRINGIFY(SHRT_MIN) |
| 489 | " <= number <= " STRINGIFY(SHRT_MAX)); |
| 490 | return -1; |
| 491 | } |
| 492 | y = (short)x; |
| 493 | memcpy(p, (char *)&y, sizeof y); |
| 494 | return 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | static int |
| 498 | np_ushort(char *p, PyObject *v, const formatdef *f) |
| 499 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 500 | long x; |
| 501 | unsigned short y; |
| 502 | if (get_long(v, &x) < 0) |
| 503 | return -1; |
| 504 | if (x < 0 || x > USHRT_MAX){ |
| 505 | PyErr_SetString(StructError, |
| 506 | "ushort format requires 0 <= number <= " STRINGIFY(USHRT_MAX)); |
| 507 | return -1; |
| 508 | } |
| 509 | y = (unsigned short)x; |
| 510 | memcpy(p, (char *)&y, sizeof y); |
| 511 | return 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | static int |
| 515 | np_int(char *p, PyObject *v, const formatdef *f) |
| 516 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 517 | long x; |
| 518 | int y; |
| 519 | if (get_long(v, &x) < 0) |
| 520 | return -1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 521 | #if (SIZEOF_LONG > SIZEOF_INT) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 522 | if ((x < ((long)INT_MIN)) || (x > ((long)INT_MAX))) |
| 523 | RANGE_ERROR(x, f, 0, -1); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 524 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 525 | y = (int)x; |
| 526 | memcpy(p, (char *)&y, sizeof y); |
| 527 | return 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | static int |
| 531 | np_uint(char *p, PyObject *v, const formatdef *f) |
| 532 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 533 | unsigned long x; |
| 534 | unsigned int y; |
| 535 | if (get_ulong(v, &x) < 0) |
| 536 | return -1; |
| 537 | y = (unsigned int)x; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 538 | #if (SIZEOF_LONG > SIZEOF_INT) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 539 | if (x > ((unsigned long)UINT_MAX)) |
| 540 | RANGE_ERROR(y, f, 1, -1); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 541 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 542 | memcpy(p, (char *)&y, sizeof y); |
| 543 | return 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | static int |
| 547 | np_long(char *p, PyObject *v, const formatdef *f) |
| 548 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 549 | long x; |
| 550 | if (get_long(v, &x) < 0) |
| 551 | return -1; |
| 552 | memcpy(p, (char *)&x, sizeof x); |
| 553 | return 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | static int |
| 557 | np_ulong(char *p, PyObject *v, const formatdef *f) |
| 558 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 559 | unsigned long x; |
| 560 | if (get_ulong(v, &x) < 0) |
| 561 | return -1; |
| 562 | memcpy(p, (char *)&x, sizeof x); |
| 563 | return 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | #ifdef HAVE_LONG_LONG |
| 567 | |
| 568 | static int |
| 569 | np_longlong(char *p, PyObject *v, const formatdef *f) |
| 570 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 571 | PY_LONG_LONG x; |
| 572 | if (get_longlong(v, &x) < 0) |
| 573 | return -1; |
| 574 | memcpy(p, (char *)&x, sizeof x); |
| 575 | return 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | static int |
| 579 | np_ulonglong(char *p, PyObject *v, const formatdef *f) |
| 580 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 581 | unsigned PY_LONG_LONG x; |
| 582 | if (get_ulonglong(v, &x) < 0) |
| 583 | return -1; |
| 584 | memcpy(p, (char *)&x, sizeof x); |
| 585 | return 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 586 | } |
| 587 | #endif |
| 588 | |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 589 | |
| 590 | static int |
| 591 | np_bool(char *p, PyObject *v, const formatdef *f) |
| 592 | { |
Benjamin Peterson | de73c45 | 2010-07-07 18:54:59 +0000 | [diff] [blame] | 593 | int y; |
| 594 | BOOL_TYPE x; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 595 | y = PyObject_IsTrue(v); |
Benjamin Peterson | de73c45 | 2010-07-07 18:54:59 +0000 | [diff] [blame] | 596 | if (y < 0) |
| 597 | return -1; |
| 598 | x = y; |
| 599 | memcpy(p, (char *)&x, sizeof x); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 600 | return 0; |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 601 | } |
| 602 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 603 | static int |
| 604 | np_float(char *p, PyObject *v, const formatdef *f) |
| 605 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 606 | float x = (float)PyFloat_AsDouble(v); |
| 607 | if (x == -1 && PyErr_Occurred()) { |
| 608 | PyErr_SetString(StructError, |
| 609 | "required argument is not a float"); |
| 610 | return -1; |
| 611 | } |
| 612 | memcpy(p, (char *)&x, sizeof x); |
| 613 | return 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | static int |
| 617 | np_double(char *p, PyObject *v, const formatdef *f) |
| 618 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 619 | double x = PyFloat_AsDouble(v); |
| 620 | if (x == -1 && PyErr_Occurred()) { |
| 621 | PyErr_SetString(StructError, |
| 622 | "required argument is not a float"); |
| 623 | return -1; |
| 624 | } |
| 625 | memcpy(p, (char *)&x, sizeof(double)); |
| 626 | return 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | static int |
| 630 | np_void_p(char *p, PyObject *v, const formatdef *f) |
| 631 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 632 | void *x; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 633 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 634 | v = get_pylong(v); |
| 635 | if (v == NULL) |
| 636 | return -1; |
| 637 | assert(PyLong_Check(v)); |
| 638 | x = PyLong_AsVoidPtr(v); |
| 639 | Py_DECREF(v); |
| 640 | if (x == NULL && PyErr_Occurred()) |
| 641 | return -1; |
| 642 | memcpy(p, (char *)&x, sizeof x); |
| 643 | return 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | static formatdef native_table[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 647 | {'x', sizeof(char), 0, NULL}, |
| 648 | {'b', sizeof(char), 0, nu_byte, np_byte}, |
| 649 | {'B', sizeof(char), 0, nu_ubyte, np_ubyte}, |
| 650 | {'c', sizeof(char), 0, nu_char, np_char}, |
| 651 | {'s', sizeof(char), 0, NULL}, |
| 652 | {'p', sizeof(char), 0, NULL}, |
| 653 | {'h', sizeof(short), SHORT_ALIGN, nu_short, np_short}, |
| 654 | {'H', sizeof(short), SHORT_ALIGN, nu_ushort, np_ushort}, |
| 655 | {'i', sizeof(int), INT_ALIGN, nu_int, np_int}, |
| 656 | {'I', sizeof(int), INT_ALIGN, nu_uint, np_uint}, |
| 657 | {'l', sizeof(long), LONG_ALIGN, nu_long, np_long}, |
| 658 | {'L', sizeof(long), LONG_ALIGN, nu_ulong, np_ulong}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 659 | #ifdef HAVE_LONG_LONG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 660 | {'q', sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_longlong, np_longlong}, |
| 661 | {'Q', sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_ulonglong,np_ulonglong}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 662 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 663 | {'?', sizeof(BOOL_TYPE), BOOL_ALIGN, nu_bool, np_bool}, |
| 664 | {'f', sizeof(float), FLOAT_ALIGN, nu_float, np_float}, |
| 665 | {'d', sizeof(double), DOUBLE_ALIGN, nu_double, np_double}, |
| 666 | {'P', sizeof(void *), VOID_P_ALIGN, nu_void_p, np_void_p}, |
| 667 | {0} |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 668 | }; |
| 669 | |
| 670 | /* Big-endian routines. *****************************************************/ |
| 671 | |
| 672 | static PyObject * |
| 673 | bu_int(const char *p, const formatdef *f) |
| 674 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 675 | long x = 0; |
| 676 | Py_ssize_t i = f->size; |
| 677 | const unsigned char *bytes = (const unsigned char *)p; |
| 678 | do { |
| 679 | x = (x<<8) | *bytes++; |
| 680 | } while (--i > 0); |
| 681 | /* Extend the sign bit. */ |
| 682 | if (SIZEOF_LONG > f->size) |
| 683 | x |= -(x & (1L << ((8 * f->size) - 1))); |
| 684 | return PyLong_FromLong(x); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | static PyObject * |
| 688 | bu_uint(const char *p, const formatdef *f) |
| 689 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 690 | unsigned long x = 0; |
| 691 | Py_ssize_t i = f->size; |
| 692 | const unsigned char *bytes = (const unsigned char *)p; |
| 693 | do { |
| 694 | x = (x<<8) | *bytes++; |
| 695 | } while (--i > 0); |
| 696 | if (x <= LONG_MAX) |
| 697 | return PyLong_FromLong((long)x); |
| 698 | return PyLong_FromUnsignedLong(x); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | static PyObject * |
| 702 | bu_longlong(const char *p, const formatdef *f) |
| 703 | { |
| 704 | #ifdef HAVE_LONG_LONG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 705 | PY_LONG_LONG x = 0; |
| 706 | Py_ssize_t i = f->size; |
| 707 | const unsigned char *bytes = (const unsigned char *)p; |
| 708 | do { |
| 709 | x = (x<<8) | *bytes++; |
| 710 | } while (--i > 0); |
| 711 | /* Extend the sign bit. */ |
| 712 | if (SIZEOF_LONG_LONG > f->size) |
| 713 | x |= -(x & ((PY_LONG_LONG)1 << ((8 * f->size) - 1))); |
| 714 | if (x >= LONG_MIN && x <= LONG_MAX) |
| 715 | return PyLong_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long)); |
| 716 | return PyLong_FromLongLong(x); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 717 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 718 | return _PyLong_FromByteArray((const unsigned char *)p, |
| 719 | 8, |
| 720 | 0, /* little-endian */ |
| 721 | 1 /* signed */); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 722 | #endif |
| 723 | } |
| 724 | |
| 725 | static PyObject * |
| 726 | bu_ulonglong(const char *p, const formatdef *f) |
| 727 | { |
| 728 | #ifdef HAVE_LONG_LONG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 729 | unsigned PY_LONG_LONG x = 0; |
| 730 | Py_ssize_t i = f->size; |
| 731 | const unsigned char *bytes = (const unsigned char *)p; |
| 732 | do { |
| 733 | x = (x<<8) | *bytes++; |
| 734 | } while (--i > 0); |
| 735 | if (x <= LONG_MAX) |
| 736 | return PyLong_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long)); |
| 737 | return PyLong_FromUnsignedLongLong(x); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 738 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 739 | return _PyLong_FromByteArray((const unsigned char *)p, |
| 740 | 8, |
| 741 | 0, /* little-endian */ |
| 742 | 0 /* signed */); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 743 | #endif |
| 744 | } |
| 745 | |
| 746 | static PyObject * |
| 747 | bu_float(const char *p, const formatdef *f) |
| 748 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 749 | return unpack_float(p, 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | static PyObject * |
| 753 | bu_double(const char *p, const formatdef *f) |
| 754 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 755 | return unpack_double(p, 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 756 | } |
| 757 | |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 758 | static PyObject * |
| 759 | bu_bool(const char *p, const formatdef *f) |
| 760 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 761 | char x; |
| 762 | memcpy((char *)&x, p, sizeof x); |
| 763 | return PyBool_FromLong(x != 0); |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 764 | } |
| 765 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 766 | static int |
| 767 | bp_int(char *p, PyObject *v, const formatdef *f) |
| 768 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 769 | long x; |
| 770 | Py_ssize_t i; |
| 771 | if (get_long(v, &x) < 0) |
| 772 | return -1; |
| 773 | i = f->size; |
| 774 | if (i != SIZEOF_LONG) { |
| 775 | if ((i == 2) && (x < -32768 || x > 32767)) |
| 776 | RANGE_ERROR(x, f, 0, 0xffffL); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 777 | #if (SIZEOF_LONG != 4) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 778 | else if ((i == 4) && (x < -2147483648L || x > 2147483647L)) |
| 779 | RANGE_ERROR(x, f, 0, 0xffffffffL); |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 780 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 781 | } |
| 782 | do { |
| 783 | p[--i] = (char)x; |
| 784 | x >>= 8; |
| 785 | } while (i > 0); |
| 786 | return 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | static int |
| 790 | bp_uint(char *p, PyObject *v, const formatdef *f) |
| 791 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 792 | unsigned long x; |
| 793 | Py_ssize_t i; |
| 794 | if (get_ulong(v, &x) < 0) |
| 795 | return -1; |
| 796 | i = f->size; |
| 797 | if (i != SIZEOF_LONG) { |
| 798 | unsigned long maxint = 1; |
| 799 | maxint <<= (unsigned long)(i * 8); |
| 800 | if (x >= maxint) |
| 801 | RANGE_ERROR(x, f, 1, maxint - 1); |
| 802 | } |
| 803 | do { |
| 804 | p[--i] = (char)x; |
| 805 | x >>= 8; |
| 806 | } while (i > 0); |
| 807 | return 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 808 | } |
| 809 | |
| 810 | static int |
| 811 | bp_longlong(char *p, PyObject *v, const formatdef *f) |
| 812 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 813 | int res; |
| 814 | v = get_pylong(v); |
| 815 | if (v == NULL) |
| 816 | return -1; |
| 817 | res = _PyLong_AsByteArray((PyLongObject *)v, |
| 818 | (unsigned char *)p, |
| 819 | 8, |
| 820 | 0, /* little_endian */ |
| 821 | 1 /* signed */); |
| 822 | Py_DECREF(v); |
| 823 | return res; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | static int |
| 827 | bp_ulonglong(char *p, PyObject *v, const formatdef *f) |
| 828 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 829 | int res; |
| 830 | v = get_pylong(v); |
| 831 | if (v == NULL) |
| 832 | return -1; |
| 833 | res = _PyLong_AsByteArray((PyLongObject *)v, |
| 834 | (unsigned char *)p, |
| 835 | 8, |
| 836 | 0, /* little_endian */ |
| 837 | 0 /* signed */); |
| 838 | Py_DECREF(v); |
| 839 | return res; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | static int |
| 843 | bp_float(char *p, PyObject *v, const formatdef *f) |
| 844 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 845 | double x = PyFloat_AsDouble(v); |
| 846 | if (x == -1 && PyErr_Occurred()) { |
| 847 | PyErr_SetString(StructError, |
| 848 | "required argument is not a float"); |
| 849 | return -1; |
| 850 | } |
| 851 | return _PyFloat_Pack4(x, (unsigned char *)p, 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | static int |
| 855 | bp_double(char *p, PyObject *v, const formatdef *f) |
| 856 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 857 | double x = PyFloat_AsDouble(v); |
| 858 | if (x == -1 && PyErr_Occurred()) { |
| 859 | PyErr_SetString(StructError, |
| 860 | "required argument is not a float"); |
| 861 | return -1; |
| 862 | } |
| 863 | return _PyFloat_Pack8(x, (unsigned char *)p, 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 864 | } |
| 865 | |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 866 | static int |
| 867 | bp_bool(char *p, PyObject *v, const formatdef *f) |
| 868 | { |
Mark Dickinson | eff5d85 | 2010-07-18 07:29:02 +0000 | [diff] [blame] | 869 | int y; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 870 | y = PyObject_IsTrue(v); |
Benjamin Peterson | de73c45 | 2010-07-07 18:54:59 +0000 | [diff] [blame] | 871 | if (y < 0) |
| 872 | return -1; |
Mark Dickinson | eff5d85 | 2010-07-18 07:29:02 +0000 | [diff] [blame] | 873 | *p = (char)y; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 874 | return 0; |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 875 | } |
| 876 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 877 | static formatdef bigendian_table[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 878 | {'x', 1, 0, NULL}, |
| 879 | {'b', 1, 0, nu_byte, np_byte}, |
| 880 | {'B', 1, 0, nu_ubyte, np_ubyte}, |
| 881 | {'c', 1, 0, nu_char, np_char}, |
| 882 | {'s', 1, 0, NULL}, |
| 883 | {'p', 1, 0, NULL}, |
| 884 | {'h', 2, 0, bu_int, bp_int}, |
| 885 | {'H', 2, 0, bu_uint, bp_uint}, |
| 886 | {'i', 4, 0, bu_int, bp_int}, |
| 887 | {'I', 4, 0, bu_uint, bp_uint}, |
| 888 | {'l', 4, 0, bu_int, bp_int}, |
| 889 | {'L', 4, 0, bu_uint, bp_uint}, |
| 890 | {'q', 8, 0, bu_longlong, bp_longlong}, |
| 891 | {'Q', 8, 0, bu_ulonglong, bp_ulonglong}, |
| 892 | {'?', 1, 0, bu_bool, bp_bool}, |
| 893 | {'f', 4, 0, bu_float, bp_float}, |
| 894 | {'d', 8, 0, bu_double, bp_double}, |
| 895 | {0} |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 896 | }; |
| 897 | |
| 898 | /* Little-endian routines. *****************************************************/ |
| 899 | |
| 900 | static PyObject * |
| 901 | lu_int(const char *p, const formatdef *f) |
| 902 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 903 | long x = 0; |
| 904 | Py_ssize_t i = f->size; |
| 905 | const unsigned char *bytes = (const unsigned char *)p; |
| 906 | do { |
| 907 | x = (x<<8) | bytes[--i]; |
| 908 | } while (i > 0); |
| 909 | /* Extend the sign bit. */ |
| 910 | if (SIZEOF_LONG > f->size) |
| 911 | x |= -(x & (1L << ((8 * f->size) - 1))); |
| 912 | return PyLong_FromLong(x); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | static PyObject * |
| 916 | lu_uint(const char *p, const formatdef *f) |
| 917 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 918 | unsigned long x = 0; |
| 919 | Py_ssize_t i = f->size; |
| 920 | const unsigned char *bytes = (const unsigned char *)p; |
| 921 | do { |
| 922 | x = (x<<8) | bytes[--i]; |
| 923 | } while (i > 0); |
| 924 | if (x <= LONG_MAX) |
| 925 | return PyLong_FromLong((long)x); |
| 926 | return PyLong_FromUnsignedLong((long)x); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 927 | } |
| 928 | |
| 929 | static PyObject * |
| 930 | lu_longlong(const char *p, const formatdef *f) |
| 931 | { |
| 932 | #ifdef HAVE_LONG_LONG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 933 | PY_LONG_LONG x = 0; |
| 934 | Py_ssize_t i = f->size; |
| 935 | const unsigned char *bytes = (const unsigned char *)p; |
| 936 | do { |
| 937 | x = (x<<8) | bytes[--i]; |
| 938 | } while (i > 0); |
| 939 | /* Extend the sign bit. */ |
| 940 | if (SIZEOF_LONG_LONG > f->size) |
| 941 | x |= -(x & ((PY_LONG_LONG)1 << ((8 * f->size) - 1))); |
| 942 | if (x >= LONG_MIN && x <= LONG_MAX) |
| 943 | return PyLong_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long)); |
| 944 | return PyLong_FromLongLong(x); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 945 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 946 | return _PyLong_FromByteArray((const unsigned char *)p, |
| 947 | 8, |
| 948 | 1, /* little-endian */ |
| 949 | 1 /* signed */); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 950 | #endif |
| 951 | } |
| 952 | |
| 953 | static PyObject * |
| 954 | lu_ulonglong(const char *p, const formatdef *f) |
| 955 | { |
| 956 | #ifdef HAVE_LONG_LONG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 957 | unsigned PY_LONG_LONG x = 0; |
| 958 | Py_ssize_t i = f->size; |
| 959 | const unsigned char *bytes = (const unsigned char *)p; |
| 960 | do { |
| 961 | x = (x<<8) | bytes[--i]; |
| 962 | } while (i > 0); |
| 963 | if (x <= LONG_MAX) |
| 964 | return PyLong_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long)); |
| 965 | return PyLong_FromUnsignedLongLong(x); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 966 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 967 | return _PyLong_FromByteArray((const unsigned char *)p, |
| 968 | 8, |
| 969 | 1, /* little-endian */ |
| 970 | 0 /* signed */); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 971 | #endif |
| 972 | } |
| 973 | |
| 974 | static PyObject * |
| 975 | lu_float(const char *p, const formatdef *f) |
| 976 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 977 | return unpack_float(p, 1); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 978 | } |
| 979 | |
| 980 | static PyObject * |
| 981 | lu_double(const char *p, const formatdef *f) |
| 982 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 983 | return unpack_double(p, 1); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 984 | } |
| 985 | |
| 986 | static int |
| 987 | lp_int(char *p, PyObject *v, const formatdef *f) |
| 988 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 989 | long x; |
| 990 | Py_ssize_t i; |
| 991 | if (get_long(v, &x) < 0) |
| 992 | return -1; |
| 993 | i = f->size; |
| 994 | if (i != SIZEOF_LONG) { |
| 995 | if ((i == 2) && (x < -32768 || x > 32767)) |
| 996 | RANGE_ERROR(x, f, 0, 0xffffL); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 997 | #if (SIZEOF_LONG != 4) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 998 | else if ((i == 4) && (x < -2147483648L || x > 2147483647L)) |
| 999 | RANGE_ERROR(x, f, 0, 0xffffffffL); |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1000 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1001 | } |
| 1002 | do { |
| 1003 | *p++ = (char)x; |
| 1004 | x >>= 8; |
| 1005 | } while (--i > 0); |
| 1006 | return 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1007 | } |
| 1008 | |
| 1009 | static int |
| 1010 | lp_uint(char *p, PyObject *v, const formatdef *f) |
| 1011 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1012 | unsigned long x; |
| 1013 | Py_ssize_t i; |
| 1014 | if (get_ulong(v, &x) < 0) |
| 1015 | return -1; |
| 1016 | i = f->size; |
| 1017 | if (i != SIZEOF_LONG) { |
| 1018 | unsigned long maxint = 1; |
| 1019 | maxint <<= (unsigned long)(i * 8); |
| 1020 | if (x >= maxint) |
| 1021 | RANGE_ERROR(x, f, 1, maxint - 1); |
| 1022 | } |
| 1023 | do { |
| 1024 | *p++ = (char)x; |
| 1025 | x >>= 8; |
| 1026 | } while (--i > 0); |
| 1027 | return 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | static int |
| 1031 | lp_longlong(char *p, PyObject *v, const formatdef *f) |
| 1032 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1033 | int res; |
| 1034 | v = get_pylong(v); |
| 1035 | if (v == NULL) |
| 1036 | return -1; |
| 1037 | res = _PyLong_AsByteArray((PyLongObject*)v, |
| 1038 | (unsigned char *)p, |
| 1039 | 8, |
| 1040 | 1, /* little_endian */ |
| 1041 | 1 /* signed */); |
| 1042 | Py_DECREF(v); |
| 1043 | return res; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1044 | } |
| 1045 | |
| 1046 | static int |
| 1047 | lp_ulonglong(char *p, PyObject *v, const formatdef *f) |
| 1048 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1049 | int res; |
| 1050 | v = get_pylong(v); |
| 1051 | if (v == NULL) |
| 1052 | return -1; |
| 1053 | res = _PyLong_AsByteArray((PyLongObject*)v, |
| 1054 | (unsigned char *)p, |
| 1055 | 8, |
| 1056 | 1, /* little_endian */ |
| 1057 | 0 /* signed */); |
| 1058 | Py_DECREF(v); |
| 1059 | return res; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1060 | } |
| 1061 | |
| 1062 | static int |
| 1063 | lp_float(char *p, PyObject *v, const formatdef *f) |
| 1064 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1065 | double x = PyFloat_AsDouble(v); |
| 1066 | if (x == -1 && PyErr_Occurred()) { |
| 1067 | PyErr_SetString(StructError, |
| 1068 | "required argument is not a float"); |
| 1069 | return -1; |
| 1070 | } |
| 1071 | return _PyFloat_Pack4(x, (unsigned char *)p, 1); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1072 | } |
| 1073 | |
| 1074 | static int |
| 1075 | lp_double(char *p, PyObject *v, const formatdef *f) |
| 1076 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1077 | double x = PyFloat_AsDouble(v); |
| 1078 | if (x == -1 && PyErr_Occurred()) { |
| 1079 | PyErr_SetString(StructError, |
| 1080 | "required argument is not a float"); |
| 1081 | return -1; |
| 1082 | } |
| 1083 | return _PyFloat_Pack8(x, (unsigned char *)p, 1); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1084 | } |
| 1085 | |
| 1086 | static formatdef lilendian_table[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1087 | {'x', 1, 0, NULL}, |
| 1088 | {'b', 1, 0, nu_byte, np_byte}, |
| 1089 | {'B', 1, 0, nu_ubyte, np_ubyte}, |
| 1090 | {'c', 1, 0, nu_char, np_char}, |
| 1091 | {'s', 1, 0, NULL}, |
| 1092 | {'p', 1, 0, NULL}, |
| 1093 | {'h', 2, 0, lu_int, lp_int}, |
| 1094 | {'H', 2, 0, lu_uint, lp_uint}, |
| 1095 | {'i', 4, 0, lu_int, lp_int}, |
| 1096 | {'I', 4, 0, lu_uint, lp_uint}, |
| 1097 | {'l', 4, 0, lu_int, lp_int}, |
| 1098 | {'L', 4, 0, lu_uint, lp_uint}, |
| 1099 | {'q', 8, 0, lu_longlong, lp_longlong}, |
| 1100 | {'Q', 8, 0, lu_ulonglong, lp_ulonglong}, |
| 1101 | {'?', 1, 0, bu_bool, bp_bool}, /* Std rep not endian dep, |
| 1102 | but potentially different from native rep -- reuse bx_bool funcs. */ |
| 1103 | {'f', 4, 0, lu_float, lp_float}, |
| 1104 | {'d', 8, 0, lu_double, lp_double}, |
| 1105 | {0} |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1106 | }; |
| 1107 | |
| 1108 | |
| 1109 | static const formatdef * |
| 1110 | whichtable(char **pfmt) |
| 1111 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1112 | const char *fmt = (*pfmt)++; /* May be backed out of later */ |
| 1113 | switch (*fmt) { |
| 1114 | case '<': |
| 1115 | return lilendian_table; |
| 1116 | case '>': |
| 1117 | case '!': /* Network byte order is big-endian */ |
| 1118 | return bigendian_table; |
| 1119 | case '=': { /* Host byte order -- different from native in aligment! */ |
| 1120 | int n = 1; |
| 1121 | char *p = (char *) &n; |
| 1122 | if (*p == 1) |
| 1123 | return lilendian_table; |
| 1124 | else |
| 1125 | return bigendian_table; |
| 1126 | } |
| 1127 | default: |
| 1128 | --*pfmt; /* Back out of pointer increment */ |
| 1129 | /* Fall through */ |
| 1130 | case '@': |
| 1131 | return native_table; |
| 1132 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1133 | } |
| 1134 | |
| 1135 | |
| 1136 | /* Get the table entry for a format code */ |
| 1137 | |
| 1138 | static const formatdef * |
| 1139 | getentry(int c, const formatdef *f) |
| 1140 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1141 | for (; f->format != '\0'; f++) { |
| 1142 | if (f->format == c) { |
| 1143 | return f; |
| 1144 | } |
| 1145 | } |
| 1146 | PyErr_SetString(StructError, "bad char in struct format"); |
| 1147 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1148 | } |
| 1149 | |
| 1150 | |
Mark Dickinson | b72e686 | 2010-06-11 19:50:30 +0000 | [diff] [blame] | 1151 | /* Align a size according to a format code. Return -1 on overflow. */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1152 | |
Mark Dickinson | eac0e68 | 2010-06-11 19:05:08 +0000 | [diff] [blame] | 1153 | static Py_ssize_t |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1154 | align(Py_ssize_t size, char c, const formatdef *e) |
| 1155 | { |
Mark Dickinson | b72e686 | 2010-06-11 19:50:30 +0000 | [diff] [blame] | 1156 | Py_ssize_t extra; |
| 1157 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1158 | if (e->format == c) { |
Mark Dickinson | b72e686 | 2010-06-11 19:50:30 +0000 | [diff] [blame] | 1159 | if (e->alignment && size > 0) { |
| 1160 | extra = (e->alignment - 1) - (size - 1) % (e->alignment); |
| 1161 | if (extra > PY_SSIZE_T_MAX - size) |
| 1162 | return -1; |
| 1163 | size += extra; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1164 | } |
| 1165 | } |
| 1166 | return size; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1167 | } |
| 1168 | |
| 1169 | |
| 1170 | /* calculate the size of a format string */ |
| 1171 | |
| 1172 | static int |
| 1173 | prepare_s(PyStructObject *self) |
| 1174 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1175 | const formatdef *f; |
| 1176 | const formatdef *e; |
| 1177 | formatcode *codes; |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1178 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1179 | const char *s; |
| 1180 | const char *fmt; |
| 1181 | char c; |
Mark Dickinson | b72e686 | 2010-06-11 19:50:30 +0000 | [diff] [blame] | 1182 | Py_ssize_t size, len, num, itemsize; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1183 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1184 | fmt = PyBytes_AS_STRING(self->s_format); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1185 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1186 | f = whichtable((char **)&fmt); |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1187 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1188 | s = fmt; |
| 1189 | size = 0; |
| 1190 | len = 0; |
| 1191 | while ((c = *s++) != '\0') { |
| 1192 | if (isspace(Py_CHARMASK(c))) |
| 1193 | continue; |
| 1194 | if ('0' <= c && c <= '9') { |
| 1195 | num = c - '0'; |
| 1196 | while ('0' <= (c = *s++) && c <= '9') { |
Mark Dickinson | ab4096f | 2010-06-11 16:56:34 +0000 | [diff] [blame] | 1197 | /* overflow-safe version of |
| 1198 | if (num*10 + (c - '0') > PY_SSIZE_T_MAX) { ... } */ |
| 1199 | if (num >= PY_SSIZE_T_MAX / 10 && ( |
| 1200 | num > PY_SSIZE_T_MAX / 10 || |
Mark Dickinson | b72e686 | 2010-06-11 19:50:30 +0000 | [diff] [blame] | 1201 | (c - '0') > PY_SSIZE_T_MAX % 10)) |
| 1202 | goto overflow; |
Mark Dickinson | ab4096f | 2010-06-11 16:56:34 +0000 | [diff] [blame] | 1203 | num = num*10 + (c - '0'); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1204 | } |
Alexander Belopolsky | 177e853 | 2010-06-11 16:04:59 +0000 | [diff] [blame] | 1205 | if (c == '\0') { |
| 1206 | PyErr_SetString(StructError, |
| 1207 | "repeat count given without format specifier"); |
| 1208 | return -1; |
| 1209 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1210 | } |
| 1211 | else |
| 1212 | num = 1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1213 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1214 | e = getentry(c, f); |
| 1215 | if (e == NULL) |
| 1216 | return -1; |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1217 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1218 | switch (c) { |
| 1219 | case 's': /* fall through */ |
| 1220 | case 'p': len++; break; |
| 1221 | case 'x': break; |
| 1222 | default: len += num; break; |
| 1223 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1224 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1225 | itemsize = e->size; |
| 1226 | size = align(size, c, e); |
Mark Dickinson | b72e686 | 2010-06-11 19:50:30 +0000 | [diff] [blame] | 1227 | if (size == -1) |
| 1228 | goto overflow; |
| 1229 | |
| 1230 | /* if (size + num * itemsize > PY_SSIZE_T_MAX) { ... } */ |
| 1231 | if (num > (PY_SSIZE_T_MAX - size) / itemsize) |
| 1232 | goto overflow; |
| 1233 | size += num * itemsize; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1234 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1235 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1236 | /* check for overflow */ |
| 1237 | if ((len + 1) > (PY_SSIZE_T_MAX / sizeof(formatcode))) { |
| 1238 | PyErr_NoMemory(); |
| 1239 | return -1; |
| 1240 | } |
Amaury Forgeot d'Arc | 35c8658 | 2008-06-17 21:11:29 +0000 | [diff] [blame] | 1241 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1242 | self->s_size = size; |
| 1243 | self->s_len = len; |
| 1244 | codes = PyMem_MALLOC((len + 1) * sizeof(formatcode)); |
| 1245 | if (codes == NULL) { |
| 1246 | PyErr_NoMemory(); |
| 1247 | return -1; |
| 1248 | } |
Mark Dickinson | cf28b95 | 2010-07-29 21:41:59 +0000 | [diff] [blame] | 1249 | /* Free any s_codes value left over from a previous initialization. */ |
| 1250 | if (self->s_codes != NULL) |
| 1251 | PyMem_FREE(self->s_codes); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1252 | self->s_codes = codes; |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1253 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1254 | s = fmt; |
| 1255 | size = 0; |
| 1256 | while ((c = *s++) != '\0') { |
| 1257 | if (isspace(Py_CHARMASK(c))) |
| 1258 | continue; |
| 1259 | if ('0' <= c && c <= '9') { |
| 1260 | num = c - '0'; |
| 1261 | while ('0' <= (c = *s++) && c <= '9') |
| 1262 | num = num*10 + (c - '0'); |
| 1263 | if (c == '\0') |
| 1264 | break; |
| 1265 | } |
| 1266 | else |
| 1267 | num = 1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1268 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1269 | e = getentry(c, f); |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1270 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1271 | size = align(size, c, e); |
| 1272 | if (c == 's' || c == 'p') { |
| 1273 | codes->offset = size; |
| 1274 | codes->size = num; |
| 1275 | codes->fmtdef = e; |
| 1276 | codes++; |
| 1277 | size += num; |
| 1278 | } else if (c == 'x') { |
| 1279 | size += num; |
| 1280 | } else { |
| 1281 | while (--num >= 0) { |
| 1282 | codes->offset = size; |
| 1283 | codes->size = e->size; |
| 1284 | codes->fmtdef = e; |
| 1285 | codes++; |
| 1286 | size += e->size; |
| 1287 | } |
| 1288 | } |
| 1289 | } |
| 1290 | codes->fmtdef = NULL; |
| 1291 | codes->offset = size; |
| 1292 | codes->size = 0; |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1293 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1294 | return 0; |
Mark Dickinson | b72e686 | 2010-06-11 19:50:30 +0000 | [diff] [blame] | 1295 | |
| 1296 | overflow: |
| 1297 | PyErr_SetString(StructError, |
| 1298 | "total struct size too long"); |
| 1299 | return -1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1300 | } |
| 1301 | |
| 1302 | static PyObject * |
| 1303 | s_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1304 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1305 | PyObject *self; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1306 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1307 | assert(type != NULL && type->tp_alloc != NULL); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1308 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1309 | self = type->tp_alloc(type, 0); |
| 1310 | if (self != NULL) { |
| 1311 | PyStructObject *s = (PyStructObject*)self; |
| 1312 | Py_INCREF(Py_None); |
| 1313 | s->s_format = Py_None; |
| 1314 | s->s_codes = NULL; |
| 1315 | s->s_size = -1; |
| 1316 | s->s_len = -1; |
| 1317 | } |
| 1318 | return self; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1319 | } |
| 1320 | |
| 1321 | static int |
| 1322 | s_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 1323 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1324 | PyStructObject *soself = (PyStructObject *)self; |
| 1325 | PyObject *o_format = NULL; |
| 1326 | int ret = 0; |
| 1327 | static char *kwlist[] = {"format", 0}; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1328 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1329 | assert(PyStruct_Check(self)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1330 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1331 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:Struct", kwlist, |
| 1332 | &o_format)) |
| 1333 | return -1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1334 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1335 | if (PyUnicode_Check(o_format)) { |
| 1336 | o_format = PyUnicode_AsASCIIString(o_format); |
| 1337 | if (o_format == NULL) |
| 1338 | return -1; |
| 1339 | } |
| 1340 | /* XXX support buffer interface, too */ |
| 1341 | else { |
| 1342 | Py_INCREF(o_format); |
| 1343 | } |
Christian Heimes | a34706f | 2008-01-04 03:06:10 +0000 | [diff] [blame] | 1344 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1345 | if (!PyBytes_Check(o_format)) { |
| 1346 | Py_DECREF(o_format); |
| 1347 | PyErr_Format(PyExc_TypeError, |
| 1348 | "Struct() argument 1 must be bytes, not %.200s", |
| 1349 | Py_TYPE(o_format)->tp_name); |
| 1350 | return -1; |
| 1351 | } |
Christian Heimes | a34706f | 2008-01-04 03:06:10 +0000 | [diff] [blame] | 1352 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1353 | Py_CLEAR(soself->s_format); |
| 1354 | soself->s_format = o_format; |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1355 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1356 | ret = prepare_s(soself); |
| 1357 | return ret; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1358 | } |
| 1359 | |
| 1360 | static void |
| 1361 | s_dealloc(PyStructObject *s) |
| 1362 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1363 | if (s->weakreflist != NULL) |
| 1364 | PyObject_ClearWeakRefs((PyObject *)s); |
| 1365 | if (s->s_codes != NULL) { |
| 1366 | PyMem_FREE(s->s_codes); |
| 1367 | } |
| 1368 | Py_XDECREF(s->s_format); |
| 1369 | Py_TYPE(s)->tp_free((PyObject *)s); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1370 | } |
| 1371 | |
| 1372 | static PyObject * |
| 1373 | s_unpack_internal(PyStructObject *soself, char *startfrom) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1374 | formatcode *code; |
| 1375 | Py_ssize_t i = 0; |
| 1376 | PyObject *result = PyTuple_New(soself->s_len); |
| 1377 | if (result == NULL) |
| 1378 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1379 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1380 | for (code = soself->s_codes; code->fmtdef != NULL; code++) { |
| 1381 | PyObject *v; |
| 1382 | const formatdef *e = code->fmtdef; |
| 1383 | const char *res = startfrom + code->offset; |
| 1384 | if (e->format == 's') { |
| 1385 | v = PyBytes_FromStringAndSize(res, code->size); |
| 1386 | } else if (e->format == 'p') { |
| 1387 | Py_ssize_t n = *(unsigned char*)res; |
| 1388 | if (n >= code->size) |
| 1389 | n = code->size - 1; |
| 1390 | v = PyBytes_FromStringAndSize(res + 1, n); |
| 1391 | } else { |
| 1392 | v = e->unpack(res, e); |
| 1393 | } |
| 1394 | if (v == NULL) |
| 1395 | goto fail; |
| 1396 | PyTuple_SET_ITEM(result, i++, v); |
| 1397 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1398 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1399 | return result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1400 | fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1401 | Py_DECREF(result); |
| 1402 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1403 | } |
| 1404 | |
| 1405 | |
| 1406 | PyDoc_STRVAR(s_unpack__doc__, |
Guido van Rossum | 913dd0b | 2007-04-13 03:33:53 +0000 | [diff] [blame] | 1407 | "S.unpack(buffer) -> (v1, v2, ...)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1408 | \n\ |
Mark Dickinson | aacfa95 | 2010-06-12 15:43:45 +0000 | [diff] [blame] | 1409 | Return a tuple containing values unpacked according to the format\n\ |
| 1410 | string S.format. Requires len(buffer) == S.size. See help(struct)\n\ |
| 1411 | for more on format strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1412 | |
| 1413 | static PyObject * |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1414 | s_unpack(PyObject *self, PyObject *input) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1415 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1416 | Py_buffer vbuf; |
| 1417 | PyObject *result; |
| 1418 | PyStructObject *soself = (PyStructObject *)self; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1419 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1420 | assert(PyStruct_Check(self)); |
| 1421 | assert(soself->s_codes != NULL); |
| 1422 | if (PyObject_GetBuffer(input, &vbuf, PyBUF_SIMPLE) < 0) |
| 1423 | return NULL; |
| 1424 | if (vbuf.len != soself->s_size) { |
| 1425 | PyErr_Format(StructError, |
| 1426 | "unpack requires a bytes argument of length %zd", |
| 1427 | soself->s_size); |
| 1428 | PyBuffer_Release(&vbuf); |
| 1429 | return NULL; |
| 1430 | } |
| 1431 | result = s_unpack_internal(soself, vbuf.buf); |
| 1432 | PyBuffer_Release(&vbuf); |
| 1433 | return result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1434 | } |
| 1435 | |
| 1436 | PyDoc_STRVAR(s_unpack_from__doc__, |
Mark Dickinson | c6f1396 | 2010-06-13 09:17:13 +0000 | [diff] [blame] | 1437 | "S.unpack_from(buffer, offset=0) -> (v1, v2, ...)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1438 | \n\ |
Mark Dickinson | aacfa95 | 2010-06-12 15:43:45 +0000 | [diff] [blame] | 1439 | Return a tuple containing values unpacked according to the format\n\ |
| 1440 | string S.format. Requires len(buffer[offset:]) >= S.size. See\n\ |
| 1441 | help(struct) for more on format strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1442 | |
| 1443 | static PyObject * |
| 1444 | s_unpack_from(PyObject *self, PyObject *args, PyObject *kwds) |
| 1445 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1446 | static char *kwlist[] = {"buffer", "offset", 0}; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1447 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1448 | PyObject *input; |
| 1449 | Py_ssize_t offset = 0; |
| 1450 | Py_buffer vbuf; |
| 1451 | PyObject *result; |
| 1452 | PyStructObject *soself = (PyStructObject *)self; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1453 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1454 | assert(PyStruct_Check(self)); |
| 1455 | assert(soself->s_codes != NULL); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1456 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1457 | if (!PyArg_ParseTupleAndKeywords(args, kwds, |
| 1458 | "O|n:unpack_from", kwlist, |
| 1459 | &input, &offset)) |
| 1460 | return NULL; |
| 1461 | if (PyObject_GetBuffer(input, &vbuf, PyBUF_SIMPLE) < 0) |
| 1462 | return NULL; |
| 1463 | if (offset < 0) |
| 1464 | offset += vbuf.len; |
| 1465 | if (offset < 0 || vbuf.len - offset < soself->s_size) { |
| 1466 | PyErr_Format(StructError, |
| 1467 | "unpack_from requires a buffer of at least %zd bytes", |
| 1468 | soself->s_size); |
| 1469 | PyBuffer_Release(&vbuf); |
| 1470 | return NULL; |
| 1471 | } |
| 1472 | result = s_unpack_internal(soself, (char*)vbuf.buf + offset); |
| 1473 | PyBuffer_Release(&vbuf); |
| 1474 | return result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1475 | } |
| 1476 | |
| 1477 | |
| 1478 | /* |
| 1479 | * Guts of the pack function. |
| 1480 | * |
| 1481 | * Takes a struct object, a tuple of arguments, and offset in that tuple of |
| 1482 | * argument for where to start processing the arguments for packing, and a |
| 1483 | * character buffer for writing the packed string. The caller must insure |
| 1484 | * that the buffer may contain the required length for packing the arguments. |
| 1485 | * 0 is returned on success, 1 is returned if there is an error. |
| 1486 | * |
| 1487 | */ |
| 1488 | static int |
| 1489 | s_pack_internal(PyStructObject *soself, PyObject *args, int offset, char* buf) |
| 1490 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1491 | formatcode *code; |
| 1492 | /* XXX(nnorwitz): why does i need to be a local? can we use |
| 1493 | the offset parameter or do we need the wider width? */ |
| 1494 | Py_ssize_t i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1495 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1496 | memset(buf, '\0', soself->s_size); |
| 1497 | i = offset; |
| 1498 | for (code = soself->s_codes; code->fmtdef != NULL; code++) { |
| 1499 | Py_ssize_t n; |
| 1500 | PyObject *v = PyTuple_GET_ITEM(args, i++); |
| 1501 | const formatdef *e = code->fmtdef; |
| 1502 | char *res = buf + code->offset; |
| 1503 | if (e->format == 's') { |
| 1504 | int isstring; |
| 1505 | void *p; |
| 1506 | if (PyUnicode_Check(v)) { |
| 1507 | v = _PyUnicode_AsDefaultEncodedString(v, NULL); |
| 1508 | if (v == NULL) |
| 1509 | return -1; |
| 1510 | } |
| 1511 | isstring = PyBytes_Check(v); |
| 1512 | if (!isstring && !PyByteArray_Check(v)) { |
| 1513 | PyErr_SetString(StructError, |
| 1514 | "argument for 's' must be a bytes or string"); |
| 1515 | return -1; |
| 1516 | } |
| 1517 | if (isstring) { |
| 1518 | n = PyBytes_GET_SIZE(v); |
| 1519 | p = PyBytes_AS_STRING(v); |
| 1520 | } |
| 1521 | else { |
| 1522 | n = PyByteArray_GET_SIZE(v); |
| 1523 | p = PyByteArray_AS_STRING(v); |
| 1524 | } |
| 1525 | if (n > code->size) |
| 1526 | n = code->size; |
| 1527 | if (n > 0) |
| 1528 | memcpy(res, p, n); |
| 1529 | } else if (e->format == 'p') { |
| 1530 | int isstring; |
| 1531 | void *p; |
| 1532 | if (PyUnicode_Check(v)) { |
| 1533 | v = _PyUnicode_AsDefaultEncodedString(v, NULL); |
| 1534 | if (v == NULL) |
| 1535 | return -1; |
| 1536 | } |
| 1537 | isstring = PyBytes_Check(v); |
| 1538 | if (!isstring && !PyByteArray_Check(v)) { |
| 1539 | PyErr_SetString(StructError, |
| 1540 | "argument for 'p' must be a bytes or string"); |
| 1541 | return -1; |
| 1542 | } |
| 1543 | if (isstring) { |
| 1544 | n = PyBytes_GET_SIZE(v); |
| 1545 | p = PyBytes_AS_STRING(v); |
| 1546 | } |
| 1547 | else { |
| 1548 | n = PyByteArray_GET_SIZE(v); |
| 1549 | p = PyByteArray_AS_STRING(v); |
| 1550 | } |
| 1551 | if (n > (code->size - 1)) |
| 1552 | n = code->size - 1; |
| 1553 | if (n > 0) |
| 1554 | memcpy(res + 1, p, n); |
| 1555 | if (n > 255) |
| 1556 | n = 255; |
| 1557 | *res = Py_SAFE_DOWNCAST(n, Py_ssize_t, unsigned char); |
| 1558 | } else { |
| 1559 | if (e->pack(res, v, e) < 0) { |
| 1560 | if (PyLong_Check(v) && PyErr_ExceptionMatches(PyExc_OverflowError)) |
| 1561 | PyErr_SetString(StructError, |
| 1562 | "long too large to convert to int"); |
| 1563 | return -1; |
| 1564 | } |
| 1565 | } |
| 1566 | } |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1567 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1568 | /* Success */ |
| 1569 | return 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1570 | } |
| 1571 | |
| 1572 | |
| 1573 | PyDoc_STRVAR(s_pack__doc__, |
Benjamin Peterson | 4ae1946 | 2008-07-31 15:03:40 +0000 | [diff] [blame] | 1574 | "S.pack(v1, v2, ...) -> bytes\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1575 | \n\ |
Mark Dickinson | aacfa95 | 2010-06-12 15:43:45 +0000 | [diff] [blame] | 1576 | Return a bytes object containing values v1, v2, ... packed according\n\ |
| 1577 | to the format string S.format. See help(struct) for more on format\n\ |
| 1578 | strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1579 | |
| 1580 | static PyObject * |
| 1581 | s_pack(PyObject *self, PyObject *args) |
| 1582 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1583 | PyStructObject *soself; |
| 1584 | PyObject *result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1585 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1586 | /* Validate arguments. */ |
| 1587 | soself = (PyStructObject *)self; |
| 1588 | assert(PyStruct_Check(self)); |
| 1589 | assert(soself->s_codes != NULL); |
| 1590 | if (PyTuple_GET_SIZE(args) != soself->s_len) |
| 1591 | { |
| 1592 | PyErr_Format(StructError, |
| 1593 | "pack requires exactly %zd arguments", soself->s_len); |
| 1594 | return NULL; |
| 1595 | } |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1596 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1597 | /* Allocate a new string */ |
| 1598 | result = PyBytes_FromStringAndSize((char *)NULL, soself->s_size); |
| 1599 | if (result == NULL) |
| 1600 | return NULL; |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1601 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1602 | /* Call the guts */ |
| 1603 | if ( s_pack_internal(soself, args, 0, PyBytes_AS_STRING(result)) != 0 ) { |
| 1604 | Py_DECREF(result); |
| 1605 | return NULL; |
| 1606 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1607 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1608 | return result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1609 | } |
| 1610 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1611 | PyDoc_STRVAR(s_pack_into__doc__, |
| 1612 | "S.pack_into(buffer, offset, v1, v2, ...)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1613 | \n\ |
Mark Dickinson | aacfa95 | 2010-06-12 15:43:45 +0000 | [diff] [blame] | 1614 | Pack the values v1, v2, ... according to the format string S.format\n\ |
| 1615 | 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] | 1616 | offset. Note that the offset is a required argument. See\n\ |
Mark Dickinson | aacfa95 | 2010-06-12 15:43:45 +0000 | [diff] [blame] | 1617 | help(struct) for more on format strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1618 | |
| 1619 | static PyObject * |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1620 | s_pack_into(PyObject *self, PyObject *args) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1621 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1622 | PyStructObject *soself; |
| 1623 | char *buffer; |
| 1624 | Py_ssize_t buffer_len, offset; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1625 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1626 | /* Validate arguments. +1 is for the first arg as buffer. */ |
| 1627 | soself = (PyStructObject *)self; |
| 1628 | assert(PyStruct_Check(self)); |
| 1629 | assert(soself->s_codes != NULL); |
| 1630 | if (PyTuple_GET_SIZE(args) != (soself->s_len + 2)) |
| 1631 | { |
| 1632 | PyErr_Format(StructError, |
| 1633 | "pack_into requires exactly %zd arguments", |
| 1634 | (soself->s_len + 2)); |
| 1635 | return NULL; |
| 1636 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1637 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1638 | /* Extract a writable memory buffer from the first argument */ |
| 1639 | if ( PyObject_AsWriteBuffer(PyTuple_GET_ITEM(args, 0), |
| 1640 | (void**)&buffer, &buffer_len) == -1 ) { |
| 1641 | return NULL; |
| 1642 | } |
| 1643 | assert( buffer_len >= 0 ); |
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 | /* Extract the offset from the first argument */ |
| 1646 | offset = PyNumber_AsSsize_t(PyTuple_GET_ITEM(args, 1), PyExc_IndexError); |
| 1647 | if (offset == -1 && PyErr_Occurred()) |
| 1648 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1649 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1650 | /* Support negative offsets. */ |
| 1651 | if (offset < 0) |
| 1652 | offset += buffer_len; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1653 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1654 | /* Check boundaries */ |
| 1655 | if (offset < 0 || (buffer_len - offset) < soself->s_size) { |
| 1656 | PyErr_Format(StructError, |
| 1657 | "pack_into requires a buffer of at least %zd bytes", |
| 1658 | soself->s_size); |
| 1659 | return NULL; |
| 1660 | } |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1661 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1662 | /* Call the guts */ |
| 1663 | if ( s_pack_internal(soself, args, 2, buffer + offset) != 0 ) { |
| 1664 | return NULL; |
| 1665 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1666 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1667 | Py_RETURN_NONE; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1668 | } |
| 1669 | |
| 1670 | static PyObject * |
| 1671 | s_get_format(PyStructObject *self, void *unused) |
| 1672 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1673 | Py_INCREF(self->s_format); |
| 1674 | return self->s_format; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1675 | } |
| 1676 | |
| 1677 | static PyObject * |
| 1678 | s_get_size(PyStructObject *self, void *unused) |
| 1679 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1680 | return PyLong_FromSsize_t(self->s_size); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1681 | } |
| 1682 | |
| 1683 | /* List of functions */ |
| 1684 | |
| 1685 | static struct PyMethodDef s_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1686 | {"pack", s_pack, METH_VARARGS, s_pack__doc__}, |
| 1687 | {"pack_into", s_pack_into, METH_VARARGS, s_pack_into__doc__}, |
| 1688 | {"unpack", s_unpack, METH_O, s_unpack__doc__}, |
| 1689 | {"unpack_from", (PyCFunction)s_unpack_from, METH_VARARGS|METH_KEYWORDS, |
| 1690 | s_unpack_from__doc__}, |
| 1691 | {NULL, NULL} /* sentinel */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1692 | }; |
| 1693 | |
Alexander Belopolsky | 0bd003a | 2010-06-12 19:36:28 +0000 | [diff] [blame] | 1694 | PyDoc_STRVAR(s__doc__, |
| 1695 | "Struct(fmt) --> compiled struct object\n" |
| 1696 | "\n" |
| 1697 | "Return a new Struct object which writes and reads binary data according to\n" |
| 1698 | "the format string fmt. See help(struct) for more on format strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1699 | |
| 1700 | #define OFF(x) offsetof(PyStructObject, x) |
| 1701 | |
| 1702 | static PyGetSetDef s_getsetlist[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1703 | {"format", (getter)s_get_format, (setter)NULL, "struct format string", NULL}, |
| 1704 | {"size", (getter)s_get_size, (setter)NULL, "struct size in bytes", NULL}, |
| 1705 | {NULL} /* sentinel */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1706 | }; |
| 1707 | |
| 1708 | static |
| 1709 | PyTypeObject PyStructType = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1710 | PyVarObject_HEAD_INIT(NULL, 0) |
| 1711 | "Struct", |
| 1712 | sizeof(PyStructObject), |
| 1713 | 0, |
| 1714 | (destructor)s_dealloc, /* tp_dealloc */ |
| 1715 | 0, /* tp_print */ |
| 1716 | 0, /* tp_getattr */ |
| 1717 | 0, /* tp_setattr */ |
| 1718 | 0, /* tp_reserved */ |
| 1719 | 0, /* tp_repr */ |
| 1720 | 0, /* tp_as_number */ |
| 1721 | 0, /* tp_as_sequence */ |
| 1722 | 0, /* tp_as_mapping */ |
| 1723 | 0, /* tp_hash */ |
| 1724 | 0, /* tp_call */ |
| 1725 | 0, /* tp_str */ |
| 1726 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1727 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 1728 | 0, /* tp_as_buffer */ |
| 1729 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 1730 | s__doc__, /* tp_doc */ |
| 1731 | 0, /* tp_traverse */ |
| 1732 | 0, /* tp_clear */ |
| 1733 | 0, /* tp_richcompare */ |
| 1734 | offsetof(PyStructObject, weakreflist), /* tp_weaklistoffset */ |
| 1735 | 0, /* tp_iter */ |
| 1736 | 0, /* tp_iternext */ |
| 1737 | s_methods, /* tp_methods */ |
| 1738 | NULL, /* tp_members */ |
| 1739 | s_getsetlist, /* tp_getset */ |
| 1740 | 0, /* tp_base */ |
| 1741 | 0, /* tp_dict */ |
| 1742 | 0, /* tp_descr_get */ |
| 1743 | 0, /* tp_descr_set */ |
| 1744 | 0, /* tp_dictoffset */ |
| 1745 | s_init, /* tp_init */ |
| 1746 | PyType_GenericAlloc,/* tp_alloc */ |
| 1747 | s_new, /* tp_new */ |
| 1748 | PyObject_Del, /* tp_free */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1749 | }; |
| 1750 | |
Christian Heimes | a34706f | 2008-01-04 03:06:10 +0000 | [diff] [blame] | 1751 | |
| 1752 | /* ---- Standalone functions ---- */ |
| 1753 | |
| 1754 | #define MAXCACHE 100 |
| 1755 | static PyObject *cache = NULL; |
| 1756 | |
| 1757 | static PyObject * |
| 1758 | cache_struct(PyObject *fmt) |
| 1759 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1760 | PyObject * s_object; |
Christian Heimes | a34706f | 2008-01-04 03:06:10 +0000 | [diff] [blame] | 1761 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1762 | if (cache == NULL) { |
| 1763 | cache = PyDict_New(); |
| 1764 | if (cache == NULL) |
| 1765 | return NULL; |
| 1766 | } |
Christian Heimes | a34706f | 2008-01-04 03:06:10 +0000 | [diff] [blame] | 1767 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1768 | s_object = PyDict_GetItem(cache, fmt); |
| 1769 | if (s_object != NULL) { |
| 1770 | Py_INCREF(s_object); |
| 1771 | return s_object; |
| 1772 | } |
Christian Heimes | a34706f | 2008-01-04 03:06:10 +0000 | [diff] [blame] | 1773 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1774 | s_object = PyObject_CallFunctionObjArgs((PyObject *)(&PyStructType), fmt, NULL); |
| 1775 | if (s_object != NULL) { |
| 1776 | if (PyDict_Size(cache) >= MAXCACHE) |
| 1777 | PyDict_Clear(cache); |
| 1778 | /* Attempt to cache the result */ |
| 1779 | if (PyDict_SetItem(cache, fmt, s_object) == -1) |
| 1780 | PyErr_Clear(); |
| 1781 | } |
| 1782 | return s_object; |
Christian Heimes | a34706f | 2008-01-04 03:06:10 +0000 | [diff] [blame] | 1783 | } |
| 1784 | |
| 1785 | PyDoc_STRVAR(clearcache_doc, |
| 1786 | "Clear the internal cache."); |
| 1787 | |
| 1788 | static PyObject * |
| 1789 | clearcache(PyObject *self) |
| 1790 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1791 | Py_CLEAR(cache); |
| 1792 | Py_RETURN_NONE; |
Christian Heimes | a34706f | 2008-01-04 03:06:10 +0000 | [diff] [blame] | 1793 | } |
| 1794 | |
| 1795 | PyDoc_STRVAR(calcsize_doc, |
Mark Dickinson | aacfa95 | 2010-06-12 15:43:45 +0000 | [diff] [blame] | 1796 | "calcsize(fmt) -> integer\n\ |
| 1797 | \n\ |
| 1798 | 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] | 1799 | |
| 1800 | static PyObject * |
| 1801 | calcsize(PyObject *self, PyObject *fmt) |
| 1802 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1803 | Py_ssize_t n; |
| 1804 | PyObject *s_object = cache_struct(fmt); |
| 1805 | if (s_object == NULL) |
| 1806 | return NULL; |
| 1807 | n = ((PyStructObject *)s_object)->s_size; |
| 1808 | Py_DECREF(s_object); |
| 1809 | return PyLong_FromSsize_t(n); |
Christian Heimes | a34706f | 2008-01-04 03:06:10 +0000 | [diff] [blame] | 1810 | } |
| 1811 | |
| 1812 | PyDoc_STRVAR(pack_doc, |
Mark Dickinson | aacfa95 | 2010-06-12 15:43:45 +0000 | [diff] [blame] | 1813 | "pack(fmt, v1, v2, ...) -> bytes\n\ |
| 1814 | \n\ |
Mark Dickinson | fdb99f1 | 2010-06-12 16:30:53 +0000 | [diff] [blame] | 1815 | Return a bytes object containing the values v1, v2, ... packed according\n\ |
| 1816 | 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] | 1817 | |
| 1818 | static PyObject * |
| 1819 | pack(PyObject *self, PyObject *args) |
| 1820 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1821 | PyObject *s_object, *fmt, *newargs, *result; |
| 1822 | Py_ssize_t n = PyTuple_GET_SIZE(args); |
Christian Heimes | a34706f | 2008-01-04 03:06:10 +0000 | [diff] [blame] | 1823 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1824 | if (n == 0) { |
| 1825 | PyErr_SetString(PyExc_TypeError, "missing format argument"); |
| 1826 | return NULL; |
| 1827 | } |
| 1828 | fmt = PyTuple_GET_ITEM(args, 0); |
| 1829 | newargs = PyTuple_GetSlice(args, 1, n); |
| 1830 | if (newargs == NULL) |
| 1831 | return NULL; |
Christian Heimes | a34706f | 2008-01-04 03:06:10 +0000 | [diff] [blame] | 1832 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1833 | s_object = cache_struct(fmt); |
| 1834 | if (s_object == NULL) { |
| 1835 | Py_DECREF(newargs); |
| 1836 | return NULL; |
| 1837 | } |
| 1838 | result = s_pack(s_object, newargs); |
| 1839 | Py_DECREF(newargs); |
| 1840 | Py_DECREF(s_object); |
| 1841 | return result; |
Christian Heimes | a34706f | 2008-01-04 03:06:10 +0000 | [diff] [blame] | 1842 | } |
| 1843 | |
| 1844 | PyDoc_STRVAR(pack_into_doc, |
Mark Dickinson | aacfa95 | 2010-06-12 15:43:45 +0000 | [diff] [blame] | 1845 | "pack_into(fmt, buffer, offset, v1, v2, ...)\n\ |
| 1846 | \n\ |
| 1847 | Pack the values v1, v2, ... according to the format string fmt and write\n\ |
| 1848 | 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] | 1849 | 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] | 1850 | on format strings."); |
Christian Heimes | a34706f | 2008-01-04 03:06:10 +0000 | [diff] [blame] | 1851 | |
| 1852 | static PyObject * |
| 1853 | pack_into(PyObject *self, PyObject *args) |
| 1854 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1855 | PyObject *s_object, *fmt, *newargs, *result; |
| 1856 | Py_ssize_t n = PyTuple_GET_SIZE(args); |
Christian Heimes | a34706f | 2008-01-04 03:06:10 +0000 | [diff] [blame] | 1857 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1858 | if (n == 0) { |
| 1859 | PyErr_SetString(PyExc_TypeError, "missing format argument"); |
| 1860 | return NULL; |
| 1861 | } |
| 1862 | fmt = PyTuple_GET_ITEM(args, 0); |
| 1863 | newargs = PyTuple_GetSlice(args, 1, n); |
| 1864 | if (newargs == NULL) |
| 1865 | return NULL; |
Christian Heimes | a34706f | 2008-01-04 03:06:10 +0000 | [diff] [blame] | 1866 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1867 | s_object = cache_struct(fmt); |
| 1868 | if (s_object == NULL) { |
| 1869 | Py_DECREF(newargs); |
| 1870 | return NULL; |
| 1871 | } |
| 1872 | result = s_pack_into(s_object, newargs); |
| 1873 | Py_DECREF(newargs); |
| 1874 | Py_DECREF(s_object); |
| 1875 | return result; |
Christian Heimes | a34706f | 2008-01-04 03:06:10 +0000 | [diff] [blame] | 1876 | } |
| 1877 | |
| 1878 | PyDoc_STRVAR(unpack_doc, |
Mark Dickinson | aacfa95 | 2010-06-12 15:43:45 +0000 | [diff] [blame] | 1879 | "unpack(fmt, buffer) -> (v1, v2, ...)\n\ |
| 1880 | \n\ |
| 1881 | Return a tuple containing values unpacked according to the format string\n\ |
| 1882 | fmt. Requires len(buffer) == calcsize(fmt). See help(struct) for more\n\ |
| 1883 | on format strings."); |
Christian Heimes | a34706f | 2008-01-04 03:06:10 +0000 | [diff] [blame] | 1884 | |
| 1885 | static PyObject * |
| 1886 | unpack(PyObject *self, PyObject *args) |
| 1887 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1888 | PyObject *s_object, *fmt, *inputstr, *result; |
Christian Heimes | a34706f | 2008-01-04 03:06:10 +0000 | [diff] [blame] | 1889 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1890 | if (!PyArg_UnpackTuple(args, "unpack", 2, 2, &fmt, &inputstr)) |
| 1891 | return NULL; |
Christian Heimes | a34706f | 2008-01-04 03:06:10 +0000 | [diff] [blame] | 1892 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1893 | s_object = cache_struct(fmt); |
| 1894 | if (s_object == NULL) |
| 1895 | return NULL; |
| 1896 | result = s_unpack(s_object, inputstr); |
| 1897 | Py_DECREF(s_object); |
| 1898 | return result; |
Christian Heimes | a34706f | 2008-01-04 03:06:10 +0000 | [diff] [blame] | 1899 | } |
| 1900 | |
| 1901 | PyDoc_STRVAR(unpack_from_doc, |
Mark Dickinson | c6f1396 | 2010-06-13 09:17:13 +0000 | [diff] [blame] | 1902 | "unpack_from(fmt, buffer, offset=0) -> (v1, v2, ...)\n\ |
Mark Dickinson | aacfa95 | 2010-06-12 15:43:45 +0000 | [diff] [blame] | 1903 | \n\ |
| 1904 | Return a tuple containing values unpacked according to the format string\n\ |
| 1905 | fmt. Requires len(buffer[offset:]) >= calcsize(fmt). See help(struct)\n\ |
| 1906 | for more on format strings."); |
Christian Heimes | a34706f | 2008-01-04 03:06:10 +0000 | [diff] [blame] | 1907 | |
| 1908 | static PyObject * |
| 1909 | unpack_from(PyObject *self, PyObject *args, PyObject *kwds) |
| 1910 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1911 | PyObject *s_object, *fmt, *newargs, *result; |
| 1912 | Py_ssize_t n = PyTuple_GET_SIZE(args); |
Christian Heimes | a34706f | 2008-01-04 03:06:10 +0000 | [diff] [blame] | 1913 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1914 | if (n == 0) { |
| 1915 | PyErr_SetString(PyExc_TypeError, "missing format argument"); |
| 1916 | return NULL; |
| 1917 | } |
| 1918 | fmt = PyTuple_GET_ITEM(args, 0); |
| 1919 | newargs = PyTuple_GetSlice(args, 1, n); |
| 1920 | if (newargs == NULL) |
| 1921 | return NULL; |
Christian Heimes | a34706f | 2008-01-04 03:06:10 +0000 | [diff] [blame] | 1922 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1923 | s_object = cache_struct(fmt); |
| 1924 | if (s_object == NULL) { |
| 1925 | Py_DECREF(newargs); |
| 1926 | return NULL; |
| 1927 | } |
| 1928 | result = s_unpack_from(s_object, newargs, kwds); |
| 1929 | Py_DECREF(newargs); |
| 1930 | Py_DECREF(s_object); |
| 1931 | return result; |
Christian Heimes | a34706f | 2008-01-04 03:06:10 +0000 | [diff] [blame] | 1932 | } |
| 1933 | |
| 1934 | static struct PyMethodDef module_functions[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1935 | {"_clearcache", (PyCFunction)clearcache, METH_NOARGS, clearcache_doc}, |
| 1936 | {"calcsize", calcsize, METH_O, calcsize_doc}, |
| 1937 | {"pack", pack, METH_VARARGS, pack_doc}, |
| 1938 | {"pack_into", pack_into, METH_VARARGS, pack_into_doc}, |
| 1939 | {"unpack", unpack, METH_VARARGS, unpack_doc}, |
| 1940 | {"unpack_from", (PyCFunction)unpack_from, |
| 1941 | METH_VARARGS|METH_KEYWORDS, unpack_from_doc}, |
| 1942 | {NULL, NULL} /* sentinel */ |
Christian Heimes | a34706f | 2008-01-04 03:06:10 +0000 | [diff] [blame] | 1943 | }; |
| 1944 | |
| 1945 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1946 | /* Module initialization */ |
| 1947 | |
Christian Heimes | a34706f | 2008-01-04 03:06:10 +0000 | [diff] [blame] | 1948 | PyDoc_STRVAR(module_doc, |
| 1949 | "Functions to convert between Python values and C structs.\n\ |
Benjamin Peterson | 4ae1946 | 2008-07-31 15:03:40 +0000 | [diff] [blame] | 1950 | 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] | 1951 | and also as format strings (explained below) to describe the layout of data\n\ |
| 1952 | in the C struct.\n\ |
Christian Heimes | a34706f | 2008-01-04 03:06:10 +0000 | [diff] [blame] | 1953 | \n\ |
| 1954 | The optional first format char indicates byte order, size and alignment:\n\ |
Mark Dickinson | 40714af | 2009-10-08 15:59:20 +0000 | [diff] [blame] | 1955 | @: native order, size & alignment (default)\n\ |
| 1956 | =: native order, std. size & alignment\n\ |
| 1957 | <: little-endian, std. size & alignment\n\ |
| 1958 | >: big-endian, std. size & alignment\n\ |
| 1959 | !: same as >\n\ |
Christian Heimes | a34706f | 2008-01-04 03:06:10 +0000 | [diff] [blame] | 1960 | \n\ |
| 1961 | The remaining chars indicate types of args and must match exactly;\n\ |
| 1962 | these can be preceded by a decimal repeat count:\n\ |
| 1963 | 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] | 1964 | ?: _Bool (requires C99; if not available, char is used instead)\n\ |
Christian Heimes | a34706f | 2008-01-04 03:06:10 +0000 | [diff] [blame] | 1965 | h:short; H:unsigned short; i:int; I:unsigned int;\n\ |
| 1966 | l:long; L:unsigned long; f:float; d:double.\n\ |
| 1967 | Special cases (preceding decimal count indicates length):\n\ |
| 1968 | s:string (array of char); p: pascal string (with count byte).\n\ |
| 1969 | Special case (only available in native format):\n\ |
| 1970 | P:an integer type that is wide enough to hold a pointer.\n\ |
| 1971 | Special case (not in native mode unless 'long long' in platform C):\n\ |
| 1972 | q:long long; Q:unsigned long long\n\ |
| 1973 | Whitespace between formats is ignored.\n\ |
| 1974 | \n\ |
| 1975 | The variable struct.error is an exception raised on errors.\n"); |
| 1976 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1977 | |
| 1978 | static struct PyModuleDef _structmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1979 | PyModuleDef_HEAD_INIT, |
| 1980 | "_struct", |
| 1981 | module_doc, |
| 1982 | -1, |
| 1983 | module_functions, |
| 1984 | NULL, |
| 1985 | NULL, |
| 1986 | NULL, |
| 1987 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1988 | }; |
| 1989 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1990 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1991 | PyInit__struct(void) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1992 | { |
Mark Dickinson | 0681785 | 2010-06-12 09:25:13 +0000 | [diff] [blame] | 1993 | PyObject *m; |
Christian Heimes | a34706f | 2008-01-04 03:06:10 +0000 | [diff] [blame] | 1994 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1995 | m = PyModule_Create(&_structmodule); |
| 1996 | if (m == NULL) |
| 1997 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1998 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1999 | Py_TYPE(&PyStructType) = &PyType_Type; |
| 2000 | if (PyType_Ready(&PyStructType) < 0) |
| 2001 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2002 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2003 | /* Check endian and swap in faster functions */ |
| 2004 | { |
| 2005 | int one = 1; |
| 2006 | formatdef *native = native_table; |
| 2007 | formatdef *other, *ptr; |
| 2008 | if ((int)*(unsigned char*)&one) |
| 2009 | other = lilendian_table; |
| 2010 | else |
| 2011 | other = bigendian_table; |
| 2012 | /* Scan through the native table, find a matching |
| 2013 | entry in the endian table and swap in the |
| 2014 | native implementations whenever possible |
| 2015 | (64-bit platforms may not have "standard" sizes) */ |
| 2016 | while (native->format != '\0' && other->format != '\0') { |
| 2017 | ptr = other; |
| 2018 | while (ptr->format != '\0') { |
| 2019 | if (ptr->format == native->format) { |
| 2020 | /* Match faster when formats are |
| 2021 | listed in the same order */ |
| 2022 | if (ptr == other) |
| 2023 | other++; |
| 2024 | /* Only use the trick if the |
| 2025 | size matches */ |
| 2026 | if (ptr->size != native->size) |
| 2027 | break; |
| 2028 | /* Skip float and double, could be |
| 2029 | "unknown" float format */ |
| 2030 | if (ptr->format == 'd' || ptr->format == 'f') |
| 2031 | break; |
| 2032 | ptr->pack = native->pack; |
| 2033 | ptr->unpack = native->unpack; |
| 2034 | break; |
| 2035 | } |
| 2036 | ptr++; |
| 2037 | } |
| 2038 | native++; |
| 2039 | } |
| 2040 | } |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2041 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2042 | /* Add some symbolic constants to the module */ |
| 2043 | if (StructError == NULL) { |
| 2044 | StructError = PyErr_NewException("struct.error", NULL, NULL); |
| 2045 | if (StructError == NULL) |
| 2046 | return NULL; |
| 2047 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2048 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2049 | Py_INCREF(StructError); |
| 2050 | PyModule_AddObject(m, "error", StructError); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2051 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2052 | Py_INCREF((PyObject*)&PyStructType); |
| 2053 | PyModule_AddObject(m, "Struct", (PyObject*)&PyStructType); |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2054 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2055 | return m; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2056 | } |