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