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