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