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