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