ยปCore Development>Code coverage>Modules/_struct.c

Python code coverage for Modules/_struct.c

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