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