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