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