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