+++ /dev/null
-/* struct module -- pack values into and (out of) strings */\r
-\r
-/* New version supporting byte order, alignment and size options,\r
- character strings, and unsigned numbers */\r
-\r
-#define PY_SSIZE_T_CLEAN\r
-\r
-#include "Python.h"\r
-#include "structseq.h"\r
-#include "structmember.h"\r
-#include <ctype.h>\r
-\r
-static PyTypeObject PyStructType;\r
-\r
-/* compatibility macros */\r
-#if (PY_VERSION_HEX < 0x02050000)\r
-typedef int Py_ssize_t;\r
-#endif\r
-\r
-/* warning messages */\r
-#define FLOAT_COERCE_WARN "integer argument expected, got float"\r
-#define NON_INTEGER_WARN "integer argument expected, got non-integer " \\r
- "(implicit conversion using __int__ is deprecated)"\r
-\r
-\r
-/* The translation function for each format character is table driven */\r
-typedef struct _formatdef {\r
- char format;\r
- Py_ssize_t size;\r
- Py_ssize_t alignment;\r
- PyObject* (*unpack)(const char *,\r
- const struct _formatdef *);\r
- int (*pack)(char *, PyObject *,\r
- const struct _formatdef *);\r
-} formatdef;\r
-\r
-typedef struct _formatcode {\r
- const struct _formatdef *fmtdef;\r
- Py_ssize_t offset;\r
- Py_ssize_t size;\r
-} formatcode;\r
-\r
-/* Struct object interface */\r
-\r
-typedef struct {\r
- PyObject_HEAD\r
- Py_ssize_t s_size;\r
- Py_ssize_t s_len;\r
- formatcode *s_codes;\r
- PyObject *s_format;\r
- PyObject *weakreflist; /* List of weak references */\r
-} PyStructObject;\r
-\r
-\r
-#define PyStruct_Check(op) PyObject_TypeCheck(op, &PyStructType)\r
-#define PyStruct_CheckExact(op) (Py_TYPE(op) == &PyStructType)\r
-\r
-\r
-/* Exception */\r
-\r
-static PyObject *StructError;\r
-\r
-\r
-/* Define various structs to figure out the alignments of types */\r
-\r
-\r
-typedef struct { char c; short x; } st_short;\r
-typedef struct { char c; int x; } st_int;\r
-typedef struct { char c; long x; } st_long;\r
-typedef struct { char c; float x; } st_float;\r
-typedef struct { char c; double x; } st_double;\r
-typedef struct { char c; void *x; } st_void_p;\r
-\r
-#define SHORT_ALIGN (sizeof(st_short) - sizeof(short))\r
-#define INT_ALIGN (sizeof(st_int) - sizeof(int))\r
-#define LONG_ALIGN (sizeof(st_long) - sizeof(long))\r
-#define FLOAT_ALIGN (sizeof(st_float) - sizeof(float))\r
-#define DOUBLE_ALIGN (sizeof(st_double) - sizeof(double))\r
-#define VOID_P_ALIGN (sizeof(st_void_p) - sizeof(void *))\r
-\r
-/* We can't support q and Q in native mode unless the compiler does;\r
- in std mode, they're 8 bytes on all platforms. */\r
-#ifdef HAVE_LONG_LONG\r
-typedef struct { char c; PY_LONG_LONG x; } s_long_long;\r
-#define LONG_LONG_ALIGN (sizeof(s_long_long) - sizeof(PY_LONG_LONG))\r
-#endif\r
-\r
-#ifdef HAVE_C99_BOOL\r
-#define BOOL_TYPE _Bool\r
-typedef struct { char c; _Bool x; } s_bool;\r
-#define BOOL_ALIGN (sizeof(s_bool) - sizeof(BOOL_TYPE))\r
-#else\r
-#define BOOL_TYPE char\r
-#define BOOL_ALIGN 0\r
-#endif\r
-\r
-#define STRINGIFY(x) #x\r
-\r
-#ifdef __powerc\r
-#pragma options align=reset\r
-#endif\r
-\r
-static char *integer_codes = "bBhHiIlLqQ";\r
-\r
-/* Helper to get a PyLongObject by hook or by crook. Caller should decref. */\r
-\r
-static PyObject *\r
-get_pylong(PyObject *v)\r
-{\r
- PyObject *r, *w;\r
- int converted = 0;\r
- assert(v != NULL);\r
- if (!PyInt_Check(v) && !PyLong_Check(v)) {\r
- PyNumberMethods *m;\r
- /* Not an integer; first try to use __index__ to\r
- convert to an integer. If the __index__ method\r
- doesn't exist, or raises a TypeError, try __int__.\r
- Use of the latter is deprecated, and will fail in\r
- Python 3.x. */\r
-\r
- m = Py_TYPE(v)->tp_as_number;\r
- if (PyIndex_Check(v)) {\r
- w = PyNumber_Index(v);\r
- if (w != NULL) {\r
- v = w;\r
- /* successfully converted to an integer */\r
- converted = 1;\r
- }\r
- else if (PyErr_ExceptionMatches(PyExc_TypeError)) {\r
- PyErr_Clear();\r
- }\r
- else\r
- return NULL;\r
- }\r
- if (!converted && m != NULL && m->nb_int != NULL) {\r
- /* Special case warning message for floats, for\r
- backwards compatibility. */\r
- if (PyFloat_Check(v)) {\r
- if (PyErr_WarnEx(\r
- PyExc_DeprecationWarning,\r
- FLOAT_COERCE_WARN, 1))\r
- return NULL;\r
- }\r
- else {\r
- if (PyErr_WarnEx(\r
- PyExc_DeprecationWarning,\r
- NON_INTEGER_WARN, 1))\r
- return NULL;\r
- }\r
- v = m->nb_int(v);\r
- if (v == NULL)\r
- return NULL;\r
- if (!PyInt_Check(v) && !PyLong_Check(v)) {\r
- PyErr_SetString(PyExc_TypeError,\r
- "__int__ method returned "\r
- "non-integer");\r
- return NULL;\r
- }\r
- converted = 1;\r
- }\r
- if (!converted) {\r
- PyErr_SetString(StructError,\r
- "cannot convert argument "\r
- "to integer");\r
- return NULL;\r
- }\r
- }\r
- else\r
- /* Ensure we own a reference to v. */\r
- Py_INCREF(v);\r
-\r
- assert(PyInt_Check(v) || PyLong_Check(v));\r
- if (PyInt_Check(v)) {\r
- r = PyLong_FromLong(PyInt_AS_LONG(v));\r
- Py_DECREF(v);\r
- }\r
- else if (PyLong_Check(v)) {\r
- assert(PyLong_Check(v));\r
- r = v;\r
- }\r
- else {\r
- r = NULL; /* silence compiler warning about\r
- possibly uninitialized variable */\r
- assert(0); /* shouldn't ever get here */\r
- }\r
-\r
- return r;\r
-}\r
-\r
-/* Helper to convert a Python object to a C long. Sets an exception\r
- (struct.error for an inconvertible type, OverflowError for\r
- out-of-range values) and returns -1 on error. */\r
-\r
-static int\r
-get_long(PyObject *v, long *p)\r
-{\r
- long x;\r
-\r
- v = get_pylong(v);\r
- if (v == NULL)\r
- return -1;\r
- assert(PyLong_Check(v));\r
- x = PyLong_AsLong(v);\r
- Py_DECREF(v);\r
- if (x == (long)-1 && PyErr_Occurred())\r
- return -1;\r
- *p = x;\r
- return 0;\r
-}\r
-\r
-/* Same, but handling unsigned long */\r
-\r
-static int\r
-get_ulong(PyObject *v, unsigned long *p)\r
-{\r
- unsigned long x;\r
-\r
- v = get_pylong(v);\r
- if (v == NULL)\r
- return -1;\r
- assert(PyLong_Check(v));\r
- x = PyLong_AsUnsignedLong(v);\r
- Py_DECREF(v);\r
- if (x == (unsigned long)-1 && PyErr_Occurred())\r
- return -1;\r
- *p = x;\r
- return 0;\r
-}\r
-\r
-#ifdef HAVE_LONG_LONG\r
-\r
-/* Same, but handling native long long. */\r
-\r
-static int\r
-get_longlong(PyObject *v, PY_LONG_LONG *p)\r
-{\r
- PY_LONG_LONG x;\r
-\r
- v = get_pylong(v);\r
- if (v == NULL)\r
- return -1;\r
- assert(PyLong_Check(v));\r
- x = PyLong_AsLongLong(v);\r
- Py_DECREF(v);\r
- if (x == (PY_LONG_LONG)-1 && PyErr_Occurred())\r
- return -1;\r
- *p = x;\r
- return 0;\r
-}\r
-\r
-/* Same, but handling native unsigned long long. */\r
-\r
-static int\r
-get_ulonglong(PyObject *v, unsigned PY_LONG_LONG *p)\r
-{\r
- unsigned PY_LONG_LONG x;\r
-\r
- v = get_pylong(v);\r
- if (v == NULL)\r
- return -1;\r
- assert(PyLong_Check(v));\r
- x = PyLong_AsUnsignedLongLong(v);\r
- Py_DECREF(v);\r
- if (x == (unsigned PY_LONG_LONG)-1 && PyErr_Occurred())\r
- return -1;\r
- *p = x;\r
- return 0;\r
-}\r
-\r
-#endif\r
-\r
-/* Floating point helpers */\r
-\r
-static PyObject *\r
-unpack_float(const char *p, /* start of 4-byte string */\r
- int le) /* true for little-endian, false for big-endian */\r
-{\r
- double x;\r
-\r
- x = _PyFloat_Unpack4((unsigned char *)p, le);\r
- if (x == -1.0 && PyErr_Occurred())\r
- return NULL;\r
- return PyFloat_FromDouble(x);\r
-}\r
-\r
-static PyObject *\r
-unpack_double(const char *p, /* start of 8-byte string */\r
- int le) /* true for little-endian, false for big-endian */\r
-{\r
- double x;\r
-\r
- x = _PyFloat_Unpack8((unsigned char *)p, le);\r
- if (x == -1.0 && PyErr_Occurred())\r
- return NULL;\r
- return PyFloat_FromDouble(x);\r
-}\r
-\r
-/* Helper to format the range error exceptions */\r
-static int\r
-_range_error(const formatdef *f, int is_unsigned)\r
-{\r
- /* ulargest is the largest unsigned value with f->size bytes.\r
- * Note that the simpler:\r
- * ((size_t)1 << (f->size * 8)) - 1\r
- * doesn't work when f->size == sizeof(size_t) because C doesn't\r
- * define what happens when a left shift count is >= the number of\r
- * bits in the integer being shifted; e.g., on some boxes it doesn't\r
- * shift at all when they're equal.\r
- */\r
- const size_t ulargest = (size_t)-1 >> ((SIZEOF_SIZE_T - f->size)*8);\r
- assert(f->size >= 1 && f->size <= SIZEOF_SIZE_T);\r
- if (is_unsigned)\r
- PyErr_Format(StructError,\r
- "'%c' format requires 0 <= number <= %zu",\r
- f->format,\r
- ulargest);\r
- else {\r
- const Py_ssize_t largest = (Py_ssize_t)(ulargest >> 1);\r
- PyErr_Format(StructError,\r
- "'%c' format requires %zd <= number <= %zd",\r
- f->format,\r
- ~ largest,\r
- largest);\r
- }\r
- return -1;\r
-}\r
-\r
-\r
-\r
-/* A large number of small routines follow, with names of the form\r
-\r
- [bln][up]_TYPE\r
-\r
- [bln] distiguishes among big-endian, little-endian and native.\r
- [pu] distiguishes between pack (to struct) and unpack (from struct).\r
- TYPE is one of char, byte, ubyte, etc.\r
-*/\r
-\r
-/* Native mode routines. ****************************************************/\r
-/* NOTE:\r
- In all n[up]_<type> routines handling types larger than 1 byte, there is\r
- *no* guarantee that the p pointer is properly aligned for each type,\r
- therefore memcpy is called. An intermediate variable is used to\r
- compensate for big-endian architectures.\r
- Normally both the intermediate variable and the memcpy call will be\r
- skipped by C optimisation in little-endian architectures (gcc >= 2.91\r
- does this). */\r
-\r
-static PyObject *\r
-nu_char(const char *p, const formatdef *f)\r
-{\r
- return PyString_FromStringAndSize(p, 1);\r
-}\r
-\r
-static PyObject *\r
-nu_byte(const char *p, const formatdef *f)\r
-{\r
- return PyInt_FromLong((long) *(signed char *)p);\r
-}\r
-\r
-static PyObject *\r
-nu_ubyte(const char *p, const formatdef *f)\r
-{\r
- return PyInt_FromLong((long) *(unsigned char *)p);\r
-}\r
-\r
-static PyObject *\r
-nu_short(const char *p, const formatdef *f)\r
-{\r
- short x;\r
- memcpy((char *)&x, p, sizeof x);\r
- return PyInt_FromLong((long)x);\r
-}\r
-\r
-static PyObject *\r
-nu_ushort(const char *p, const formatdef *f)\r
-{\r
- unsigned short x;\r
- memcpy((char *)&x, p, sizeof x);\r
- return PyInt_FromLong((long)x);\r
-}\r
-\r
-static PyObject *\r
-nu_int(const char *p, const formatdef *f)\r
-{\r
- int x;\r
- memcpy((char *)&x, p, sizeof x);\r
- return PyInt_FromLong((long)x);\r
-}\r
-\r
-static PyObject *\r
-nu_uint(const char *p, const formatdef *f)\r
-{\r
- unsigned int x;\r
- memcpy((char *)&x, p, sizeof x);\r
-#if (SIZEOF_LONG > SIZEOF_INT)\r
- return PyInt_FromLong((long)x);\r
-#else\r
- if (x <= ((unsigned int)LONG_MAX))\r
- return PyInt_FromLong((long)x);\r
- return PyLong_FromUnsignedLong((unsigned long)x);\r
-#endif\r
-}\r
-\r
-static PyObject *\r
-nu_long(const char *p, const formatdef *f)\r
-{\r
- long x;\r
- memcpy((char *)&x, p, sizeof x);\r
- return PyInt_FromLong(x);\r
-}\r
-\r
-static PyObject *\r
-nu_ulong(const char *p, const formatdef *f)\r
-{\r
- unsigned long x;\r
- memcpy((char *)&x, p, sizeof x);\r
- if (x <= LONG_MAX)\r
- return PyInt_FromLong((long)x);\r
- return PyLong_FromUnsignedLong(x);\r
-}\r
-\r
-/* Native mode doesn't support q or Q unless the platform C supports\r
- long long (or, on Windows, __int64). */\r
-\r
-#ifdef HAVE_LONG_LONG\r
-\r
-static PyObject *\r
-nu_longlong(const char *p, const formatdef *f)\r
-{\r
- PY_LONG_LONG x;\r
- memcpy((char *)&x, p, sizeof x);\r
- if (x >= LONG_MIN && x <= LONG_MAX)\r
- return PyInt_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));\r
- return PyLong_FromLongLong(x);\r
-}\r
-\r
-static PyObject *\r
-nu_ulonglong(const char *p, const formatdef *f)\r
-{\r
- unsigned PY_LONG_LONG x;\r
- memcpy((char *)&x, p, sizeof x);\r
- if (x <= LONG_MAX)\r
- return PyInt_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));\r
- return PyLong_FromUnsignedLongLong(x);\r
-}\r
-\r
-#endif\r
-\r
-static PyObject *\r
-nu_bool(const char *p, const formatdef *f)\r
-{\r
- BOOL_TYPE x;\r
- memcpy((char *)&x, p, sizeof x);\r
- return PyBool_FromLong(x != 0);\r
-}\r
-\r
-\r
-static PyObject *\r
-nu_float(const char *p, const formatdef *f)\r
-{\r
- float x;\r
- memcpy((char *)&x, p, sizeof x);\r
- return PyFloat_FromDouble((double)x);\r
-}\r
-\r
-static PyObject *\r
-nu_double(const char *p, const formatdef *f)\r
-{\r
- double x;\r
- memcpy((char *)&x, p, sizeof x);\r
- return PyFloat_FromDouble(x);\r
-}\r
-\r
-static PyObject *\r
-nu_void_p(const char *p, const formatdef *f)\r
-{\r
- void *x;\r
- memcpy((char *)&x, p, sizeof x);\r
- return PyLong_FromVoidPtr(x);\r
-}\r
-\r
-static int\r
-np_byte(char *p, PyObject *v, const formatdef *f)\r
-{\r
- long x;\r
- if (get_long(v, &x) < 0)\r
- return -1;\r
- if (x < -128 || x > 127){\r
- PyErr_SetString(StructError,\r
- "byte format requires -128 <= number <= 127");\r
- return -1;\r
- }\r
- *p = (char)x;\r
- return 0;\r
-}\r
-\r
-static int\r
-np_ubyte(char *p, PyObject *v, const formatdef *f)\r
-{\r
- long x;\r
- if (get_long(v, &x) < 0)\r
- return -1;\r
- if (x < 0 || x > 255){\r
- PyErr_SetString(StructError,\r
- "ubyte format requires 0 <= number <= 255");\r
- return -1;\r
- }\r
- *p = (char)x;\r
- return 0;\r
-}\r
-\r
-static int\r
-np_char(char *p, PyObject *v, const formatdef *f)\r
-{\r
- if (!PyString_Check(v) || PyString_Size(v) != 1) {\r
- PyErr_SetString(StructError,\r
- "char format require string of length 1");\r
- return -1;\r
- }\r
- *p = *PyString_AsString(v);\r
- return 0;\r
-}\r
-\r
-static int\r
-np_short(char *p, PyObject *v, const formatdef *f)\r
-{\r
- long x;\r
- short y;\r
- if (get_long(v, &x) < 0)\r
- return -1;\r
- if (x < SHRT_MIN || x > SHRT_MAX){\r
- PyErr_SetString(StructError,\r
- "short format requires " STRINGIFY(SHRT_MIN)\r
- " <= number <= " STRINGIFY(SHRT_MAX));\r
- return -1;\r
- }\r
- y = (short)x;\r
- memcpy(p, (char *)&y, sizeof y);\r
- return 0;\r
-}\r
-\r
-static int\r
-np_ushort(char *p, PyObject *v, const formatdef *f)\r
-{\r
- long x;\r
- unsigned short y;\r
- if (get_long(v, &x) < 0)\r
- return -1;\r
- if (x < 0 || x > USHRT_MAX){\r
- PyErr_SetString(StructError,\r
- "ushort format requires 0 <= number <= " STRINGIFY(USHRT_MAX));\r
- return -1;\r
- }\r
- y = (unsigned short)x;\r
- memcpy(p, (char *)&y, sizeof y);\r
- return 0;\r
-}\r
-\r
-static int\r
-np_int(char *p, PyObject *v, const formatdef *f)\r
-{\r
- long x;\r
- int y;\r
- if (get_long(v, &x) < 0)\r
- return -1;\r
-#if (SIZEOF_LONG > SIZEOF_INT)\r
- if ((x < ((long)INT_MIN)) || (x > ((long)INT_MAX)))\r
- return _range_error(f, 0);\r
-#endif\r
- y = (int)x;\r
- memcpy(p, (char *)&y, sizeof y);\r
- return 0;\r
-}\r
-\r
-static int\r
-np_uint(char *p, PyObject *v, const formatdef *f)\r
-{\r
- unsigned long x;\r
- unsigned int y;\r
- if (get_ulong(v, &x) < 0)\r
- return -1;\r
- y = (unsigned int)x;\r
-#if (SIZEOF_LONG > SIZEOF_INT)\r
- if (x > ((unsigned long)UINT_MAX))\r
- return _range_error(f, 1);\r
-#endif\r
- memcpy(p, (char *)&y, sizeof y);\r
- return 0;\r
-}\r
-\r
-static int\r
-np_long(char *p, PyObject *v, const formatdef *f)\r
-{\r
- long x;\r
- if (get_long(v, &x) < 0)\r
- return -1;\r
- memcpy(p, (char *)&x, sizeof x);\r
- return 0;\r
-}\r
-\r
-static int\r
-np_ulong(char *p, PyObject *v, const formatdef *f)\r
-{\r
- unsigned long x;\r
- if (get_ulong(v, &x) < 0)\r
- return -1;\r
- memcpy(p, (char *)&x, sizeof x);\r
- return 0;\r
-}\r
-\r
-#ifdef HAVE_LONG_LONG\r
-\r
-static int\r
-np_longlong(char *p, PyObject *v, const formatdef *f)\r
-{\r
- PY_LONG_LONG x;\r
- if (get_longlong(v, &x) < 0)\r
- return -1;\r
- memcpy(p, (char *)&x, sizeof x);\r
- return 0;\r
-}\r
-\r
-static int\r
-np_ulonglong(char *p, PyObject *v, const formatdef *f)\r
-{\r
- unsigned PY_LONG_LONG x;\r
- if (get_ulonglong(v, &x) < 0)\r
- return -1;\r
- memcpy(p, (char *)&x, sizeof x);\r
- return 0;\r
-}\r
-#endif\r
-\r
-\r
-static int\r
-np_bool(char *p, PyObject *v, const formatdef *f)\r
-{\r
- int y;\r
- BOOL_TYPE x;\r
- y = PyObject_IsTrue(v);\r
- if (y < 0)\r
- return -1;\r
- x = y;\r
- memcpy(p, (char *)&x, sizeof x);\r
- return 0;\r
-}\r
-\r
-static int\r
-np_float(char *p, PyObject *v, const formatdef *f)\r
-{\r
- float x = (float)PyFloat_AsDouble(v);\r
- if (x == -1 && PyErr_Occurred()) {\r
- PyErr_SetString(StructError,\r
- "required argument is not a float");\r
- return -1;\r
- }\r
- memcpy(p, (char *)&x, sizeof x);\r
- return 0;\r
-}\r
-\r
-static int\r
-np_double(char *p, PyObject *v, const formatdef *f)\r
-{\r
- double x = PyFloat_AsDouble(v);\r
- if (x == -1 && PyErr_Occurred()) {\r
- PyErr_SetString(StructError,\r
- "required argument is not a float");\r
- return -1;\r
- }\r
- memcpy(p, (char *)&x, sizeof(double));\r
- return 0;\r
-}\r
-\r
-static int\r
-np_void_p(char *p, PyObject *v, const formatdef *f)\r
-{\r
- void *x;\r
-\r
- v = get_pylong(v);\r
- if (v == NULL)\r
- return -1;\r
- assert(PyLong_Check(v));\r
- x = PyLong_AsVoidPtr(v);\r
- Py_DECREF(v);\r
- if (x == NULL && PyErr_Occurred())\r
- return -1;\r
- memcpy(p, (char *)&x, sizeof x);\r
- return 0;\r
-}\r
-\r
-static formatdef native_table[] = {\r
- {'x', sizeof(char), 0, NULL},\r
- {'b', sizeof(char), 0, nu_byte, np_byte},\r
- {'B', sizeof(char), 0, nu_ubyte, np_ubyte},\r
- {'c', sizeof(char), 0, nu_char, np_char},\r
- {'s', sizeof(char), 0, NULL},\r
- {'p', sizeof(char), 0, NULL},\r
- {'h', sizeof(short), SHORT_ALIGN, nu_short, np_short},\r
- {'H', sizeof(short), SHORT_ALIGN, nu_ushort, np_ushort},\r
- {'i', sizeof(int), INT_ALIGN, nu_int, np_int},\r
- {'I', sizeof(int), INT_ALIGN, nu_uint, np_uint},\r
- {'l', sizeof(long), LONG_ALIGN, nu_long, np_long},\r
- {'L', sizeof(long), LONG_ALIGN, nu_ulong, np_ulong},\r
-#ifdef HAVE_LONG_LONG\r
- {'q', sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_longlong, np_longlong},\r
- {'Q', sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_ulonglong,np_ulonglong},\r
-#endif\r
- {'?', sizeof(BOOL_TYPE), BOOL_ALIGN, nu_bool, np_bool},\r
- {'f', sizeof(float), FLOAT_ALIGN, nu_float, np_float},\r
- {'d', sizeof(double), DOUBLE_ALIGN, nu_double, np_double},\r
- {'P', sizeof(void *), VOID_P_ALIGN, nu_void_p, np_void_p},\r
- {0}\r
-};\r
-\r
-/* Big-endian routines. *****************************************************/\r
-\r
-static PyObject *\r
-bu_int(const char *p, const formatdef *f)\r
-{\r
- long x = 0;\r
- Py_ssize_t i = f->size;\r
- const unsigned char *bytes = (const unsigned char *)p;\r
- do {\r
- x = (x<<8) | *bytes++;\r
- } while (--i > 0);\r
- /* Extend the sign bit. */\r
- if (SIZEOF_LONG > f->size)\r
- x |= -(x & (1L << ((8 * f->size) - 1)));\r
- return PyInt_FromLong(x);\r
-}\r
-\r
-static PyObject *\r
-bu_uint(const char *p, const formatdef *f)\r
-{\r
- unsigned long x = 0;\r
- Py_ssize_t i = f->size;\r
- const unsigned char *bytes = (const unsigned char *)p;\r
- do {\r
- x = (x<<8) | *bytes++;\r
- } while (--i > 0);\r
- if (x <= LONG_MAX)\r
- return PyInt_FromLong((long)x);\r
- return PyLong_FromUnsignedLong(x);\r
-}\r
-\r
-static PyObject *\r
-bu_longlong(const char *p, const formatdef *f)\r
-{\r
-#ifdef HAVE_LONG_LONG\r
- PY_LONG_LONG x = 0;\r
- Py_ssize_t i = f->size;\r
- const unsigned char *bytes = (const unsigned char *)p;\r
- do {\r
- x = (x<<8) | *bytes++;\r
- } while (--i > 0);\r
- /* Extend the sign bit. */\r
- if (SIZEOF_LONG_LONG > f->size)\r
- x |= -(x & ((PY_LONG_LONG)1 << ((8 * f->size) - 1)));\r
- if (x >= LONG_MIN && x <= LONG_MAX)\r
- return PyInt_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));\r
- return PyLong_FromLongLong(x);\r
-#else\r
- return _PyLong_FromByteArray((const unsigned char *)p,\r
- 8,\r
- 0, /* little-endian */\r
- 1 /* signed */);\r
-#endif\r
-}\r
-\r
-static PyObject *\r
-bu_ulonglong(const char *p, const formatdef *f)\r
-{\r
-#ifdef HAVE_LONG_LONG\r
- unsigned PY_LONG_LONG x = 0;\r
- Py_ssize_t i = f->size;\r
- const unsigned char *bytes = (const unsigned char *)p;\r
- do {\r
- x = (x<<8) | *bytes++;\r
- } while (--i > 0);\r
- if (x <= LONG_MAX)\r
- return PyInt_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));\r
- return PyLong_FromUnsignedLongLong(x);\r
-#else\r
- return _PyLong_FromByteArray((const unsigned char *)p,\r
- 8,\r
- 0, /* little-endian */\r
- 0 /* signed */);\r
-#endif\r
-}\r
-\r
-static PyObject *\r
-bu_float(const char *p, const formatdef *f)\r
-{\r
- return unpack_float(p, 0);\r
-}\r
-\r
-static PyObject *\r
-bu_double(const char *p, const formatdef *f)\r
-{\r
- return unpack_double(p, 0);\r
-}\r
-\r
-static PyObject *\r
-bu_bool(const char *p, const formatdef *f)\r
-{\r
- char x;\r
- memcpy((char *)&x, p, sizeof x);\r
- return PyBool_FromLong(x != 0);\r
-}\r
-\r
-static int\r
-bp_int(char *p, PyObject *v, const formatdef *f)\r
-{\r
- long x;\r
- Py_ssize_t i;\r
- if (get_long(v, &x) < 0)\r
- return -1;\r
- i = f->size;\r
- if (i != SIZEOF_LONG) {\r
- if ((i == 2) && (x < -32768 || x > 32767))\r
- return _range_error(f, 0);\r
-#if (SIZEOF_LONG != 4)\r
- else if ((i == 4) && (x < -2147483648L || x > 2147483647L))\r
- return _range_error(f, 0);\r
-#endif\r
- }\r
- do {\r
- p[--i] = (char)x;\r
- x >>= 8;\r
- } while (i > 0);\r
- return 0;\r
-}\r
-\r
-static int\r
-bp_uint(char *p, PyObject *v, const formatdef *f)\r
-{\r
- unsigned long x;\r
- Py_ssize_t i;\r
- if (get_ulong(v, &x) < 0)\r
- return -1;\r
- i = f->size;\r
- if (i != SIZEOF_LONG) {\r
- unsigned long maxint = 1;\r
- maxint <<= (unsigned long)(i * 8);\r
- if (x >= maxint)\r
- return _range_error(f, 1);\r
- }\r
- do {\r
- p[--i] = (char)x;\r
- x >>= 8;\r
- } while (i > 0);\r
- return 0;\r
-}\r
-\r
-static int\r
-bp_longlong(char *p, PyObject *v, const formatdef *f)\r
-{\r
- int res;\r
- v = get_pylong(v);\r
- if (v == NULL)\r
- return -1;\r
- res = _PyLong_AsByteArray((PyLongObject *)v,\r
- (unsigned char *)p,\r
- 8,\r
- 0, /* little_endian */\r
- 1 /* signed */);\r
- Py_DECREF(v);\r
- return res;\r
-}\r
-\r
-static int\r
-bp_ulonglong(char *p, PyObject *v, const formatdef *f)\r
-{\r
- int res;\r
- v = get_pylong(v);\r
- if (v == NULL)\r
- return -1;\r
- res = _PyLong_AsByteArray((PyLongObject *)v,\r
- (unsigned char *)p,\r
- 8,\r
- 0, /* little_endian */\r
- 0 /* signed */);\r
- Py_DECREF(v);\r
- return res;\r
-}\r
-\r
-static int\r
-bp_float(char *p, PyObject *v, const formatdef *f)\r
-{\r
- double x = PyFloat_AsDouble(v);\r
- if (x == -1 && PyErr_Occurred()) {\r
- PyErr_SetString(StructError,\r
- "required argument is not a float");\r
- return -1;\r
- }\r
- return _PyFloat_Pack4(x, (unsigned char *)p, 0);\r
-}\r
-\r
-static int\r
-bp_double(char *p, PyObject *v, const formatdef *f)\r
-{\r
- double x = PyFloat_AsDouble(v);\r
- if (x == -1 && PyErr_Occurred()) {\r
- PyErr_SetString(StructError,\r
- "required argument is not a float");\r
- return -1;\r
- }\r
- return _PyFloat_Pack8(x, (unsigned char *)p, 0);\r
-}\r
-\r
-static int\r
-bp_bool(char *p, PyObject *v, const formatdef *f)\r
-{\r
- int y;\r
- y = PyObject_IsTrue(v);\r
- if (y < 0)\r
- return -1;\r
- *p = (char)y;\r
- return 0;\r
-}\r
-\r
-static formatdef bigendian_table[] = {\r
- {'x', 1, 0, NULL},\r
- {'b', 1, 0, nu_byte, np_byte},\r
- {'B', 1, 0, nu_ubyte, np_ubyte},\r
- {'c', 1, 0, nu_char, np_char},\r
- {'s', 1, 0, NULL},\r
- {'p', 1, 0, NULL},\r
- {'h', 2, 0, bu_int, bp_int},\r
- {'H', 2, 0, bu_uint, bp_uint},\r
- {'i', 4, 0, bu_int, bp_int},\r
- {'I', 4, 0, bu_uint, bp_uint},\r
- {'l', 4, 0, bu_int, bp_int},\r
- {'L', 4, 0, bu_uint, bp_uint},\r
- {'q', 8, 0, bu_longlong, bp_longlong},\r
- {'Q', 8, 0, bu_ulonglong, bp_ulonglong},\r
- {'?', 1, 0, bu_bool, bp_bool},\r
- {'f', 4, 0, bu_float, bp_float},\r
- {'d', 8, 0, bu_double, bp_double},\r
- {0}\r
-};\r
-\r
-/* Little-endian routines. *****************************************************/\r
-\r
-static PyObject *\r
-lu_int(const char *p, const formatdef *f)\r
-{\r
- long x = 0;\r
- Py_ssize_t i = f->size;\r
- const unsigned char *bytes = (const unsigned char *)p;\r
- do {\r
- x = (x<<8) | bytes[--i];\r
- } while (i > 0);\r
- /* Extend the sign bit. */\r
- if (SIZEOF_LONG > f->size)\r
- x |= -(x & (1L << ((8 * f->size) - 1)));\r
- return PyInt_FromLong(x);\r
-}\r
-\r
-static PyObject *\r
-lu_uint(const char *p, const formatdef *f)\r
-{\r
- unsigned long x = 0;\r
- Py_ssize_t i = f->size;\r
- const unsigned char *bytes = (const unsigned char *)p;\r
- do {\r
- x = (x<<8) | bytes[--i];\r
- } while (i > 0);\r
- if (x <= LONG_MAX)\r
- return PyInt_FromLong((long)x);\r
- return PyLong_FromUnsignedLong((long)x);\r
-}\r
-\r
-static PyObject *\r
-lu_longlong(const char *p, const formatdef *f)\r
-{\r
-#ifdef HAVE_LONG_LONG\r
- PY_LONG_LONG x = 0;\r
- Py_ssize_t i = f->size;\r
- const unsigned char *bytes = (const unsigned char *)p;\r
- do {\r
- x = (x<<8) | bytes[--i];\r
- } while (i > 0);\r
- /* Extend the sign bit. */\r
- if (SIZEOF_LONG_LONG > f->size)\r
- x |= -(x & ((PY_LONG_LONG)1 << ((8 * f->size) - 1)));\r
- if (x >= LONG_MIN && x <= LONG_MAX)\r
- return PyInt_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));\r
- return PyLong_FromLongLong(x);\r
-#else\r
- return _PyLong_FromByteArray((const unsigned char *)p,\r
- 8,\r
- 1, /* little-endian */\r
- 1 /* signed */);\r
-#endif\r
-}\r
-\r
-static PyObject *\r
-lu_ulonglong(const char *p, const formatdef *f)\r
-{\r
-#ifdef HAVE_LONG_LONG\r
- unsigned PY_LONG_LONG x = 0;\r
- Py_ssize_t i = f->size;\r
- const unsigned char *bytes = (const unsigned char *)p;\r
- do {\r
- x = (x<<8) | bytes[--i];\r
- } while (i > 0);\r
- if (x <= LONG_MAX)\r
- return PyInt_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));\r
- return PyLong_FromUnsignedLongLong(x);\r
-#else\r
- return _PyLong_FromByteArray((const unsigned char *)p,\r
- 8,\r
- 1, /* little-endian */\r
- 0 /* signed */);\r
-#endif\r
-}\r
-\r
-static PyObject *\r
-lu_float(const char *p, const formatdef *f)\r
-{\r
- return unpack_float(p, 1);\r
-}\r
-\r
-static PyObject *\r
-lu_double(const char *p, const formatdef *f)\r
-{\r
- return unpack_double(p, 1);\r
-}\r
-\r
-static int\r
-lp_int(char *p, PyObject *v, const formatdef *f)\r
-{\r
- long x;\r
- Py_ssize_t i;\r
- if (get_long(v, &x) < 0)\r
- return -1;\r
- i = f->size;\r
- if (i != SIZEOF_LONG) {\r
- if ((i == 2) && (x < -32768 || x > 32767))\r
- return _range_error(f, 0);\r
-#if (SIZEOF_LONG != 4)\r
- else if ((i == 4) && (x < -2147483648L || x > 2147483647L))\r
- return _range_error(f, 0);\r
-#endif\r
- }\r
- do {\r
- *p++ = (char)x;\r
- x >>= 8;\r
- } while (--i > 0);\r
- return 0;\r
-}\r
-\r
-static int\r
-lp_uint(char *p, PyObject *v, const formatdef *f)\r
-{\r
- unsigned long x;\r
- Py_ssize_t i;\r
- if (get_ulong(v, &x) < 0)\r
- return -1;\r
- i = f->size;\r
- if (i != SIZEOF_LONG) {\r
- unsigned long maxint = 1;\r
- maxint <<= (unsigned long)(i * 8);\r
- if (x >= maxint)\r
- return _range_error(f, 1);\r
- }\r
- do {\r
- *p++ = (char)x;\r
- x >>= 8;\r
- } while (--i > 0);\r
- return 0;\r
-}\r
-\r
-static int\r
-lp_longlong(char *p, PyObject *v, const formatdef *f)\r
-{\r
- int res;\r
- v = get_pylong(v);\r
- if (v == NULL)\r
- return -1;\r
- res = _PyLong_AsByteArray((PyLongObject*)v,\r
- (unsigned char *)p,\r
- 8,\r
- 1, /* little_endian */\r
- 1 /* signed */);\r
- Py_DECREF(v);\r
- return res;\r
-}\r
-\r
-static int\r
-lp_ulonglong(char *p, PyObject *v, const formatdef *f)\r
-{\r
- int res;\r
- v = get_pylong(v);\r
- if (v == NULL)\r
- return -1;\r
- res = _PyLong_AsByteArray((PyLongObject*)v,\r
- (unsigned char *)p,\r
- 8,\r
- 1, /* little_endian */\r
- 0 /* signed */);\r
- Py_DECREF(v);\r
- return res;\r
-}\r
-\r
-static int\r
-lp_float(char *p, PyObject *v, const formatdef *f)\r
-{\r
- double x = PyFloat_AsDouble(v);\r
- if (x == -1 && PyErr_Occurred()) {\r
- PyErr_SetString(StructError,\r
- "required argument is not a float");\r
- return -1;\r
- }\r
- return _PyFloat_Pack4(x, (unsigned char *)p, 1);\r
-}\r
-\r
-static int\r
-lp_double(char *p, PyObject *v, const formatdef *f)\r
-{\r
- double x = PyFloat_AsDouble(v);\r
- if (x == -1 && PyErr_Occurred()) {\r
- PyErr_SetString(StructError,\r
- "required argument is not a float");\r
- return -1;\r
- }\r
- return _PyFloat_Pack8(x, (unsigned char *)p, 1);\r
-}\r
-\r
-static formatdef lilendian_table[] = {\r
- {'x', 1, 0, NULL},\r
- {'b', 1, 0, nu_byte, np_byte},\r
- {'B', 1, 0, nu_ubyte, np_ubyte},\r
- {'c', 1, 0, nu_char, np_char},\r
- {'s', 1, 0, NULL},\r
- {'p', 1, 0, NULL},\r
- {'h', 2, 0, lu_int, lp_int},\r
- {'H', 2, 0, lu_uint, lp_uint},\r
- {'i', 4, 0, lu_int, lp_int},\r
- {'I', 4, 0, lu_uint, lp_uint},\r
- {'l', 4, 0, lu_int, lp_int},\r
- {'L', 4, 0, lu_uint, lp_uint},\r
- {'q', 8, 0, lu_longlong, lp_longlong},\r
- {'Q', 8, 0, lu_ulonglong, lp_ulonglong},\r
- {'?', 1, 0, bu_bool, bp_bool}, /* Std rep not endian dep,\r
- but potentially different from native rep -- reuse bx_bool funcs. */\r
- {'f', 4, 0, lu_float, lp_float},\r
- {'d', 8, 0, lu_double, lp_double},\r
- {0}\r
-};\r
-\r
-\r
-static const formatdef *\r
-whichtable(char **pfmt)\r
-{\r
- const char *fmt = (*pfmt)++; /* May be backed out of later */\r
- switch (*fmt) {\r
- case '<':\r
- return lilendian_table;\r
- case '>':\r
- case '!': /* Network byte order is big-endian */\r
- return bigendian_table;\r
- case '=': { /* Host byte order -- different from native in alignment! */\r
- int n = 1;\r
- char *p = (char *) &n;\r
- if (*p == 1)\r
- return lilendian_table;\r
- else\r
- return bigendian_table;\r
- }\r
- default:\r
- --*pfmt; /* Back out of pointer increment */\r
- /* Fall through */\r
- case '@':\r
- return native_table;\r
- }\r
-}\r
-\r
-\r
-/* Get the table entry for a format code */\r
-\r
-static const formatdef *\r
-getentry(int c, const formatdef *f)\r
-{\r
- for (; f->format != '\0'; f++) {\r
- if (f->format == c) {\r
- return f;\r
- }\r
- }\r
- PyErr_SetString(StructError, "bad char in struct format");\r
- return NULL;\r
-}\r
-\r
-\r
-/* Align a size according to a format code. Return -1 on overflow. */\r
-\r
-static Py_ssize_t\r
-align(Py_ssize_t size, char c, const formatdef *e)\r
-{\r
- Py_ssize_t extra;\r
-\r
- if (e->format == c) {\r
- if (e->alignment && size > 0) {\r
- extra = (e->alignment - 1) - (size - 1) % (e->alignment);\r
- if (extra > PY_SSIZE_T_MAX - size)\r
- return -1;\r
- size += extra;\r
- }\r
- }\r
- return size;\r
-}\r
-\r
-\r
-/* calculate the size of a format string */\r
-\r
-static int\r
-prepare_s(PyStructObject *self)\r
-{\r
- const formatdef *f;\r
- const formatdef *e;\r
- formatcode *codes;\r
-\r
- const char *s;\r
- const char *fmt;\r
- char c;\r
- Py_ssize_t size, len, num, itemsize;\r
-\r
- fmt = PyString_AS_STRING(self->s_format);\r
-\r
- f = whichtable((char **)&fmt);\r
-\r
- s = fmt;\r
- size = 0;\r
- len = 0;\r
- while ((c = *s++) != '\0') {\r
- if (isspace(Py_CHARMASK(c)))\r
- continue;\r
- if ('0' <= c && c <= '9') {\r
- num = c - '0';\r
- while ('0' <= (c = *s++) && c <= '9') {\r
- /* overflow-safe version of\r
- if (num*10 + (c - '0') > PY_SSIZE_T_MAX) { ... } */\r
- if (num >= PY_SSIZE_T_MAX / 10 && (\r
- num > PY_SSIZE_T_MAX / 10 ||\r
- (c - '0') > PY_SSIZE_T_MAX % 10))\r
- goto overflow;\r
- num = num*10 + (c - '0');\r
- }\r
- if (c == '\0')\r
- break;\r
- }\r
- else\r
- num = 1;\r
-\r
- e = getentry(c, f);\r
- if (e == NULL)\r
- return -1;\r
-\r
- switch (c) {\r
- case 's': /* fall through */\r
- case 'p': len++; break;\r
- case 'x': break;\r
- default: len += num; break;\r
- }\r
-\r
- itemsize = e->size;\r
- size = align(size, c, e);\r
- if (size == -1)\r
- goto overflow;\r
-\r
- /* if (size + num * itemsize > PY_SSIZE_T_MAX) { ... } */\r
- if (num > (PY_SSIZE_T_MAX - size) / itemsize)\r
- goto overflow;\r
- size += num * itemsize;\r
- }\r
-\r
- /* check for overflow */\r
- if ((len + 1) > (PY_SSIZE_T_MAX / sizeof(formatcode))) {\r
- PyErr_NoMemory();\r
- return -1;\r
- }\r
-\r
- self->s_size = size;\r
- self->s_len = len;\r
- codes = PyMem_MALLOC((len + 1) * sizeof(formatcode));\r
- if (codes == NULL) {\r
- PyErr_NoMemory();\r
- return -1;\r
- }\r
- /* Free any s_codes value left over from a previous initialization. */\r
- if (self->s_codes != NULL)\r
- PyMem_FREE(self->s_codes);\r
- self->s_codes = codes;\r
-\r
- s = fmt;\r
- size = 0;\r
- while ((c = *s++) != '\0') {\r
- if (isspace(Py_CHARMASK(c)))\r
- continue;\r
- if ('0' <= c && c <= '9') {\r
- num = c - '0';\r
- while ('0' <= (c = *s++) && c <= '9')\r
- num = num*10 + (c - '0');\r
- if (c == '\0')\r
- break;\r
- }\r
- else\r
- num = 1;\r
-\r
- e = getentry(c, f);\r
-\r
- size = align(size, c, e);\r
- if (c == 's' || c == 'p') {\r
- codes->offset = size;\r
- codes->size = num;\r
- codes->fmtdef = e;\r
- codes++;\r
- size += num;\r
- } else if (c == 'x') {\r
- size += num;\r
- } else {\r
- while (--num >= 0) {\r
- codes->offset = size;\r
- codes->size = e->size;\r
- codes->fmtdef = e;\r
- codes++;\r
- size += e->size;\r
- }\r
- }\r
- }\r
- codes->fmtdef = NULL;\r
- codes->offset = size;\r
- codes->size = 0;\r
-\r
- return 0;\r
-\r
- overflow:\r
- PyErr_SetString(StructError,\r
- "total struct size too long");\r
- return -1;\r
-}\r
-\r
-static PyObject *\r
-s_new(PyTypeObject *type, PyObject *args, PyObject *kwds)\r
-{\r
- PyObject *self;\r
-\r
- assert(type != NULL && type->tp_alloc != NULL);\r
-\r
- self = type->tp_alloc(type, 0);\r
- if (self != NULL) {\r
- PyStructObject *s = (PyStructObject*)self;\r
- Py_INCREF(Py_None);\r
- s->s_format = Py_None;\r
- s->s_codes = NULL;\r
- s->s_size = -1;\r
- s->s_len = -1;\r
- }\r
- return self;\r
-}\r
-\r
-static int\r
-s_init(PyObject *self, PyObject *args, PyObject *kwds)\r
-{\r
- PyStructObject *soself = (PyStructObject *)self;\r
- PyObject *o_format = NULL;\r
- int ret = 0;\r
- static char *kwlist[] = {"format", 0};\r
-\r
- assert(PyStruct_Check(self));\r
-\r
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:Struct", kwlist,\r
- &o_format))\r
- return -1;\r
-\r
- if (PyString_Check(o_format)) {\r
- Py_INCREF(o_format);\r
- Py_CLEAR(soself->s_format);\r
- soself->s_format = o_format;\r
- }\r
- else if (PyUnicode_Check(o_format)) {\r
- PyObject *str = PyUnicode_AsEncodedString(o_format, "ascii", NULL);\r
- if (str == NULL)\r
- return -1;\r
- Py_CLEAR(soself->s_format);\r
- soself->s_format = str;\r
- }\r
- else {\r
- PyErr_Format(PyExc_TypeError,\r
- "Struct() argument 1 must be string, not %s",\r
- Py_TYPE(o_format)->tp_name);\r
- return -1;\r
- }\r
-\r
- ret = prepare_s(soself);\r
- return ret;\r
-}\r
-\r
-static void\r
-s_dealloc(PyStructObject *s)\r
-{\r
- if (s->weakreflist != NULL)\r
- PyObject_ClearWeakRefs((PyObject *)s);\r
- if (s->s_codes != NULL) {\r
- PyMem_FREE(s->s_codes);\r
- }\r
- Py_XDECREF(s->s_format);\r
- Py_TYPE(s)->tp_free((PyObject *)s);\r
-}\r
-\r
-static PyObject *\r
-s_unpack_internal(PyStructObject *soself, char *startfrom) {\r
- formatcode *code;\r
- Py_ssize_t i = 0;\r
- PyObject *result = PyTuple_New(soself->s_len);\r
- if (result == NULL)\r
- return NULL;\r
-\r
- for (code = soself->s_codes; code->fmtdef != NULL; code++) {\r
- PyObject *v;\r
- const formatdef *e = code->fmtdef;\r
- const char *res = startfrom + code->offset;\r
- if (e->format == 's') {\r
- v = PyString_FromStringAndSize(res, code->size);\r
- } else if (e->format == 'p') {\r
- Py_ssize_t n = *(unsigned char*)res;\r
- if (n >= code->size)\r
- n = code->size - 1;\r
- v = PyString_FromStringAndSize(res + 1, n);\r
- } else {\r
- v = e->unpack(res, e);\r
- }\r
- if (v == NULL)\r
- goto fail;\r
- PyTuple_SET_ITEM(result, i++, v);\r
- }\r
-\r
- return result;\r
-fail:\r
- Py_DECREF(result);\r
- return NULL;\r
-}\r
-\r
-\r
-PyDoc_STRVAR(s_unpack__doc__,\r
-"S.unpack(str) -> (v1, v2, ...)\n\\r
-\n\\r
-Return tuple containing values unpacked according to this Struct's format.\n\\r
-Requires len(str) == self.size. See struct.__doc__ for more on format\n\\r
-strings.");\r
-\r
-static PyObject *\r
-s_unpack(PyObject *self, PyObject *inputstr)\r
-{\r
- Py_buffer buf;\r
- char *start;\r
- Py_ssize_t len;\r
- PyObject *args=NULL, *result;\r
- PyStructObject *soself = (PyStructObject *)self;\r
- assert(PyStruct_Check(self));\r
- assert(soself->s_codes != NULL);\r
- if (inputstr == NULL)\r
- goto fail;\r
- if (PyString_Check(inputstr) &&\r
- PyString_GET_SIZE(inputstr) == soself->s_size) {\r
- return s_unpack_internal(soself, PyString_AS_STRING(inputstr));\r
- }\r
- args = PyTuple_Pack(1, inputstr);\r
- if (args == NULL)\r
- return NULL;\r
- if (!PyArg_ParseTuple(args, "s*:unpack", &buf))\r
- goto fail;\r
- start = buf.buf;\r
- len = buf.len;\r
- if (soself->s_size != len) {\r
- PyBuffer_Release(&buf);\r
- goto fail;\r
- }\r
- result = s_unpack_internal(soself, start);\r
- Py_DECREF(args);\r
- PyBuffer_Release(&buf);\r
- return result;\r
-\r
-fail:\r
- Py_XDECREF(args);\r
- PyErr_Format(StructError,\r
- "unpack requires a string argument of length %zd",\r
- soself->s_size);\r
- return NULL;\r
-}\r
-\r
-PyDoc_STRVAR(s_unpack_from__doc__,\r
-"S.unpack_from(buffer[, offset]) -> (v1, v2, ...)\n\\r
-\n\\r
-Return tuple containing values unpacked according to this Struct's format.\n\\r
-Unlike unpack, unpack_from can unpack values from any object supporting\n\\r
-the buffer API, not just str. Requires len(buffer[offset:]) >= self.size.\n\\r
-See struct.__doc__ for more on format strings.");\r
-\r
-static PyObject *\r
-s_unpack_from(PyObject *self, PyObject *args, PyObject *kwds)\r
-{\r
- static char *kwlist[] = {"buffer", "offset", 0};\r
- static char *fmt = "z*|n:unpack_from";\r
- Py_buffer buf;\r
- Py_ssize_t buffer_len = 0, offset = 0;\r
- char *buffer = NULL;\r
- PyStructObject *soself = (PyStructObject *)self;\r
- PyObject *result;\r
- assert(PyStruct_Check(self));\r
- assert(soself->s_codes != NULL);\r
-\r
- if (!PyArg_ParseTupleAndKeywords(args, kwds, fmt, kwlist,\r
- &buf, &offset))\r
- return NULL;\r
- buffer = buf.buf;\r
- buffer_len = buf.len;\r
- if (buffer == NULL) {\r
- PyErr_Format(StructError,\r
- "unpack_from requires a buffer argument");\r
- PyBuffer_Release(&buf);\r
- return NULL;\r
- }\r
-\r
- if (offset < 0)\r
- offset += buffer_len;\r
-\r
- if (offset < 0 || (buffer_len - offset) < soself->s_size) {\r
- PyErr_Format(StructError,\r
- "unpack_from requires a buffer of at least %zd bytes",\r
- soself->s_size);\r
- PyBuffer_Release(&buf);\r
- return NULL;\r
- }\r
- result = s_unpack_internal(soself, buffer + offset);\r
- PyBuffer_Release(&buf);\r
- return result;\r
-}\r
-\r
-\r
-/*\r
- * Guts of the pack function.\r
- *\r
- * Takes a struct object, a tuple of arguments, and offset in that tuple of\r
- * argument for where to start processing the arguments for packing, and a\r
- * character buffer for writing the packed string. The caller must insure\r
- * that the buffer may contain the required length for packing the arguments.\r
- * 0 is returned on success, 1 is returned if there is an error.\r
- *\r
- */\r
-static int\r
-s_pack_internal(PyStructObject *soself, PyObject *args, int offset, char* buf)\r
-{\r
- formatcode *code;\r
- /* XXX(nnorwitz): why does i need to be a local? can we use\r
- the offset parameter or do we need the wider width? */\r
- Py_ssize_t i;\r
-\r
- memset(buf, '\0', soself->s_size);\r
- i = offset;\r
- for (code = soself->s_codes; code->fmtdef != NULL; code++) {\r
- Py_ssize_t n;\r
- PyObject *v = PyTuple_GET_ITEM(args, i++);\r
- const formatdef *e = code->fmtdef;\r
- char *res = buf + code->offset;\r
- if (e->format == 's') {\r
- if (!PyString_Check(v)) {\r
- PyErr_SetString(StructError,\r
- "argument for 's' must "\r
- "be a string");\r
- return -1;\r
- }\r
- n = PyString_GET_SIZE(v);\r
- if (n > code->size)\r
- n = code->size;\r
- if (n > 0)\r
- memcpy(res, PyString_AS_STRING(v), n);\r
- } else if (e->format == 'p') {\r
- if (!PyString_Check(v)) {\r
- PyErr_SetString(StructError,\r
- "argument for 'p' must "\r
- "be a string");\r
- return -1;\r
- }\r
- n = PyString_GET_SIZE(v);\r
- if (n > (code->size - 1))\r
- n = code->size - 1;\r
- if (n > 0)\r
- memcpy(res + 1, PyString_AS_STRING(v), n);\r
- if (n > 255)\r
- n = 255;\r
- *res = Py_SAFE_DOWNCAST(n, Py_ssize_t, unsigned char);\r
- } else if (e->pack(res, v, e) < 0) {\r
- if (strchr(integer_codes, e->format) != NULL &&\r
- PyErr_ExceptionMatches(PyExc_OverflowError))\r
- PyErr_Format(StructError,\r
- "integer out of range for "\r
- "'%c' format code",\r
- e->format);\r
- return -1;\r
- }\r
- }\r
-\r
- /* Success */\r
- return 0;\r
-}\r
-\r
-\r
-PyDoc_STRVAR(s_pack__doc__,\r
-"S.pack(v1, v2, ...) -> string\n\\r
-\n\\r
-Return a string containing values v1, v2, ... packed according to this\n\\r
-Struct's format. See struct.__doc__ for more on format strings.");\r
-\r
-static PyObject *\r
-s_pack(PyObject *self, PyObject *args)\r
-{\r
- PyStructObject *soself;\r
- PyObject *result;\r
-\r
- /* Validate arguments. */\r
- soself = (PyStructObject *)self;\r
- assert(PyStruct_Check(self));\r
- assert(soself->s_codes != NULL);\r
- if (PyTuple_GET_SIZE(args) != soself->s_len)\r
- {\r
- PyErr_Format(StructError,\r
- "pack expected %zd items for packing (got %zd)", soself->s_len, PyTuple_GET_SIZE(args));\r
- return NULL;\r
- }\r
-\r
- /* Allocate a new string */\r
- result = PyString_FromStringAndSize((char *)NULL, soself->s_size);\r
- if (result == NULL)\r
- return NULL;\r
-\r
- /* Call the guts */\r
- if ( s_pack_internal(soself, args, 0, PyString_AS_STRING(result)) != 0 ) {\r
- Py_DECREF(result);\r
- return NULL;\r
- }\r
-\r
- return result;\r
-}\r
-\r
-PyDoc_STRVAR(s_pack_into__doc__,\r
-"S.pack_into(buffer, offset, v1, v2, ...)\n\\r
-\n\\r
-Pack the values v1, v2, ... according to this Struct's format, write \n\\r
-the packed bytes into the writable buffer buf starting at offset. Note\n\\r
-that the offset is not an optional argument. See struct.__doc__ for \n\\r
-more on format strings.");\r
-\r
-static PyObject *\r
-s_pack_into(PyObject *self, PyObject *args)\r
-{\r
- PyStructObject *soself;\r
- Py_buffer buf;\r
- Py_ssize_t offset;\r
-\r
- /* Validate arguments. +1 is for the first arg as buffer. */\r
- soself = (PyStructObject *)self;\r
- assert(PyStruct_Check(self));\r
- assert(soself->s_codes != NULL);\r
- if (PyTuple_GET_SIZE(args) != (soself->s_len + 2))\r
- {\r
- if (PyTuple_GET_SIZE(args) == 0) {\r
- PyErr_Format(StructError,\r
- "pack_into expected buffer argument");\r
- }\r
- else if (PyTuple_GET_SIZE(args) == 1) {\r
- PyErr_Format(StructError,\r
- "pack_into expected offset argument");\r
- }\r
- else {\r
- PyErr_Format(StructError,\r
- "pack_into expected %zd items for packing (got %zd)",\r
- soself->s_len, (PyTuple_GET_SIZE(args) - 2));\r
- }\r
- return NULL;\r
- }\r
-\r
- /* Extract a writable memory buffer from the first argument */\r
- if (!PyArg_Parse(PyTuple_GET_ITEM(args, 0), "w*", &buf))\r
- return NULL;\r
-\r
- /* Extract the offset from the first argument */\r
- offset = PyInt_AsSsize_t(PyTuple_GET_ITEM(args, 1));\r
- if (offset == -1 && PyErr_Occurred()) {\r
- PyBuffer_Release(&buf);\r
- return NULL;\r
- }\r
-\r
- /* Support negative offsets. */\r
- if (offset < 0)\r
- offset += buf.len;\r
-\r
- /* Check boundaries */\r
- if (offset < 0 || (buf.len - offset) < soself->s_size) {\r
- PyErr_Format(StructError,\r
- "pack_into requires a buffer of at least %zd bytes",\r
- soself->s_size);\r
- PyBuffer_Release(&buf);\r
- return NULL;\r
- }\r
-\r
- /* Call the guts */\r
- if (s_pack_internal(soself, args, 2, (char *)buf.buf + offset) != 0) {\r
- PyBuffer_Release(&buf);\r
- return NULL;\r
- }\r
- PyBuffer_Release(&buf);\r
-\r
- Py_RETURN_NONE;\r
-}\r
-\r
-static PyObject *\r
-s_get_format(PyStructObject *self, void *unused)\r
-{\r
- Py_INCREF(self->s_format);\r
- return self->s_format;\r
-}\r
-\r
-static PyObject *\r
-s_get_size(PyStructObject *self, void *unused)\r
-{\r
- return PyInt_FromSsize_t(self->s_size);\r
-}\r
-\r
-PyDoc_STRVAR(s_sizeof__doc__,\r
-"S.__sizeof__() -> size of S in memory, in bytes");\r
-\r
-static PyObject *\r
-s_sizeof(PyStructObject *self, void *unused)\r
-{\r
- Py_ssize_t size;\r
-\r
- size = sizeof(PyStructObject) + sizeof(formatcode) * (self->s_len + 1);\r
- return PyLong_FromSsize_t(size);\r
-}\r
-\r
-/* List of functions */\r
-\r
-static struct PyMethodDef s_methods[] = {\r
- {"pack", s_pack, METH_VARARGS, s_pack__doc__},\r
- {"pack_into", s_pack_into, METH_VARARGS, s_pack_into__doc__},\r
- {"unpack", s_unpack, METH_O, s_unpack__doc__},\r
- {"unpack_from", (PyCFunction)s_unpack_from, METH_VARARGS|METH_KEYWORDS,\r
- s_unpack_from__doc__},\r
- {"__sizeof__", (PyCFunction)s_sizeof, METH_NOARGS, s_sizeof__doc__},\r
- {NULL, NULL} /* sentinel */\r
-};\r
-\r
-PyDoc_STRVAR(s__doc__, "Compiled struct object");\r
-\r
-#define OFF(x) offsetof(PyStructObject, x)\r
-\r
-static PyGetSetDef s_getsetlist[] = {\r
- {"format", (getter)s_get_format, (setter)NULL, "struct format string", NULL},\r
- {"size", (getter)s_get_size, (setter)NULL, "struct size in bytes", NULL},\r
- {NULL} /* sentinel */\r
-};\r
-\r
-static\r
-PyTypeObject PyStructType = {\r
- PyVarObject_HEAD_INIT(NULL, 0)\r
- "Struct",\r
- sizeof(PyStructObject),\r
- 0,\r
- (destructor)s_dealloc, /* tp_dealloc */\r
- 0, /* tp_print */\r
- 0, /* tp_getattr */\r
- 0, /* tp_setattr */\r
- 0, /* tp_compare */\r
- 0, /* tp_repr */\r
- 0, /* tp_as_number */\r
- 0, /* tp_as_sequence */\r
- 0, /* tp_as_mapping */\r
- 0, /* tp_hash */\r
- 0, /* tp_call */\r
- 0, /* tp_str */\r
- PyObject_GenericGetAttr, /* tp_getattro */\r
- PyObject_GenericSetAttr, /* tp_setattro */\r
- 0, /* tp_as_buffer */\r
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_WEAKREFS,/* tp_flags */\r
- s__doc__, /* tp_doc */\r
- 0, /* tp_traverse */\r
- 0, /* tp_clear */\r
- 0, /* tp_richcompare */\r
- offsetof(PyStructObject, weakreflist), /* tp_weaklistoffset */\r
- 0, /* tp_iter */\r
- 0, /* tp_iternext */\r
- s_methods, /* tp_methods */\r
- NULL, /* tp_members */\r
- s_getsetlist, /* tp_getset */\r
- 0, /* tp_base */\r
- 0, /* tp_dict */\r
- 0, /* tp_descr_get */\r
- 0, /* tp_descr_set */\r
- 0, /* tp_dictoffset */\r
- s_init, /* tp_init */\r
- PyType_GenericAlloc,/* tp_alloc */\r
- s_new, /* tp_new */\r
- PyObject_Del, /* tp_free */\r
-};\r
-\r
-\r
-/* ---- Standalone functions ---- */\r
-\r
-#define MAXCACHE 100\r
-static PyObject *cache = NULL;\r
-\r
-static PyObject *\r
-cache_struct(PyObject *fmt)\r
-{\r
- PyObject * s_object;\r
-\r
- if (cache == NULL) {\r
- cache = PyDict_New();\r
- if (cache == NULL)\r
- return NULL;\r
- }\r
-\r
- s_object = PyDict_GetItem(cache, fmt);\r
- if (s_object != NULL) {\r
- Py_INCREF(s_object);\r
- return s_object;\r
- }\r
-\r
- s_object = PyObject_CallFunctionObjArgs((PyObject *)(&PyStructType), fmt, NULL);\r
- if (s_object != NULL) {\r
- if (PyDict_Size(cache) >= MAXCACHE)\r
- PyDict_Clear(cache);\r
- /* Attempt to cache the result */\r
- if (PyDict_SetItem(cache, fmt, s_object) == -1)\r
- PyErr_Clear();\r
- }\r
- return s_object;\r
-}\r
-\r
-PyDoc_STRVAR(clearcache_doc,\r
-"Clear the internal cache.");\r
-\r
-static PyObject *\r
-clearcache(PyObject *self)\r
-{\r
- Py_CLEAR(cache);\r
- Py_RETURN_NONE;\r
-}\r
-\r
-PyDoc_STRVAR(calcsize_doc,\r
-"Return size of C struct described by format string fmt.");\r
-\r
-static PyObject *\r
-calcsize(PyObject *self, PyObject *fmt)\r
-{\r
- Py_ssize_t n;\r
- PyObject *s_object = cache_struct(fmt);\r
- if (s_object == NULL)\r
- return NULL;\r
- n = ((PyStructObject *)s_object)->s_size;\r
- Py_DECREF(s_object);\r
- return PyInt_FromSsize_t(n);\r
-}\r
-\r
-PyDoc_STRVAR(pack_doc,\r
-"Return string containing values v1, v2, ... packed according to fmt.");\r
-\r
-static PyObject *\r
-pack(PyObject *self, PyObject *args)\r
-{\r
- PyObject *s_object, *fmt, *newargs, *result;\r
- Py_ssize_t n = PyTuple_GET_SIZE(args);\r
-\r
- if (n == 0) {\r
- PyErr_SetString(PyExc_TypeError, "missing format argument");\r
- return NULL;\r
- }\r
- fmt = PyTuple_GET_ITEM(args, 0);\r
- newargs = PyTuple_GetSlice(args, 1, n);\r
- if (newargs == NULL)\r
- return NULL;\r
-\r
- s_object = cache_struct(fmt);\r
- if (s_object == NULL) {\r
- Py_DECREF(newargs);\r
- return NULL;\r
- }\r
- result = s_pack(s_object, newargs);\r
- Py_DECREF(newargs);\r
- Py_DECREF(s_object);\r
- return result;\r
-}\r
-\r
-PyDoc_STRVAR(pack_into_doc,\r
-"Pack the values v1, v2, ... according to fmt.\n\\r
-Write the packed bytes into the writable buffer buf starting at offset.");\r
-\r
-static PyObject *\r
-pack_into(PyObject *self, PyObject *args)\r
-{\r
- PyObject *s_object, *fmt, *newargs, *result;\r
- Py_ssize_t n = PyTuple_GET_SIZE(args);\r
-\r
- if (n == 0) {\r
- PyErr_SetString(PyExc_TypeError, "missing format argument");\r
- return NULL;\r
- }\r
- fmt = PyTuple_GET_ITEM(args, 0);\r
- newargs = PyTuple_GetSlice(args, 1, n);\r
- if (newargs == NULL)\r
- return NULL;\r
-\r
- s_object = cache_struct(fmt);\r
- if (s_object == NULL) {\r
- Py_DECREF(newargs);\r
- return NULL;\r
- }\r
- result = s_pack_into(s_object, newargs);\r
- Py_DECREF(newargs);\r
- Py_DECREF(s_object);\r
- return result;\r
-}\r
-\r
-PyDoc_STRVAR(unpack_doc,\r
-"Unpack the string containing packed C structure data, according to fmt.\n\\r
-Requires len(string) == calcsize(fmt).");\r
-\r
-static PyObject *\r
-unpack(PyObject *self, PyObject *args)\r
-{\r
- PyObject *s_object, *fmt, *inputstr, *result;\r
-\r
- if (!PyArg_UnpackTuple(args, "unpack", 2, 2, &fmt, &inputstr))\r
- return NULL;\r
-\r
- s_object = cache_struct(fmt);\r
- if (s_object == NULL)\r
- return NULL;\r
- result = s_unpack(s_object, inputstr);\r
- Py_DECREF(s_object);\r
- return result;\r
-}\r
-\r
-PyDoc_STRVAR(unpack_from_doc,\r
-"Unpack the buffer, containing packed C structure data, according to\n\\r
-fmt, starting at offset. Requires len(buffer[offset:]) >= calcsize(fmt).");\r
-\r
-static PyObject *\r
-unpack_from(PyObject *self, PyObject *args, PyObject *kwds)\r
-{\r
- PyObject *s_object, *fmt, *newargs, *result;\r
- Py_ssize_t n = PyTuple_GET_SIZE(args);\r
-\r
- if (n == 0) {\r
- PyErr_SetString(PyExc_TypeError, "missing format argument");\r
- return NULL;\r
- }\r
- fmt = PyTuple_GET_ITEM(args, 0);\r
- newargs = PyTuple_GetSlice(args, 1, n);\r
- if (newargs == NULL)\r
- return NULL;\r
-\r
- s_object = cache_struct(fmt);\r
- if (s_object == NULL) {\r
- Py_DECREF(newargs);\r
- return NULL;\r
- }\r
- result = s_unpack_from(s_object, newargs, kwds);\r
- Py_DECREF(newargs);\r
- Py_DECREF(s_object);\r
- return result;\r
-}\r
-\r
-static struct PyMethodDef module_functions[] = {\r
- {"_clearcache", (PyCFunction)clearcache, METH_NOARGS, clearcache_doc},\r
- {"calcsize", calcsize, METH_O, calcsize_doc},\r
- {"pack", pack, METH_VARARGS, pack_doc},\r
- {"pack_into", pack_into, METH_VARARGS, pack_into_doc},\r
- {"unpack", unpack, METH_VARARGS, unpack_doc},\r
- {"unpack_from", (PyCFunction)unpack_from,\r
- METH_VARARGS|METH_KEYWORDS, unpack_from_doc},\r
- {NULL, NULL} /* sentinel */\r
-};\r
-\r
-\r
-/* Module initialization */\r
-\r
-PyDoc_STRVAR(module_doc,\r
-"Functions to convert between Python values and C structs represented\n\\r
-as Python strings. It uses format strings (explained below) as compact\n\\r
-descriptions of the lay-out of the C structs and the intended conversion\n\\r
-to/from Python values.\n\\r
-\n\\r
-The optional first format char indicates byte order, size and alignment:\n\\r
- @: native order, size & alignment (default)\n\\r
- =: native order, std. size & alignment\n\\r
- <: little-endian, std. size & alignment\n\\r
- >: big-endian, std. size & alignment\n\\r
- !: same as >\n\\r
-\n\\r
-The remaining chars indicate types of args and must match exactly;\n\\r
-these can be preceded by a decimal repeat count:\n\\r
- x: pad byte (no data); c:char; b:signed byte; B:unsigned byte;\n\\r
- ?: _Bool (requires C99; if not available, char is used instead)\n\\r
- h:short; H:unsigned short; i:int; I:unsigned int;\n\\r
- l:long; L:unsigned long; f:float; d:double.\n\\r
-Special cases (preceding decimal count indicates length):\n\\r
- s:string (array of char); p: pascal string (with count byte).\n\\r
-Special case (only available in native format):\n\\r
- P:an integer type that is wide enough to hold a pointer.\n\\r
-Special case (not in native mode unless 'long long' in platform C):\n\\r
- q:long long; Q:unsigned long long\n\\r
-Whitespace between formats is ignored.\n\\r
-\n\\r
-The variable struct.error is an exception raised on errors.\n");\r
-\r
-PyMODINIT_FUNC\r
-init_struct(void)\r
-{\r
- PyObject *ver, *m;\r
-\r
- ver = PyString_FromString("0.2");\r
- if (ver == NULL)\r
- return;\r
-\r
- m = Py_InitModule3("_struct", module_functions, module_doc);\r
- if (m == NULL)\r
- return;\r
-\r
- Py_TYPE(&PyStructType) = &PyType_Type;\r
- if (PyType_Ready(&PyStructType) < 0)\r
- return;\r
-\r
- /* This speed trick can't be used until overflow masking goes\r
- away, because native endian always raises exceptions\r
- instead of overflow masking. */\r
-\r
- /* Check endian and swap in faster functions */\r
- {\r
- int one = 1;\r
- formatdef *native = native_table;\r
- formatdef *other, *ptr;\r
- if ((int)*(unsigned char*)&one)\r
- other = lilendian_table;\r
- else\r
- other = bigendian_table;\r
- /* Scan through the native table, find a matching\r
- entry in the endian table and swap in the\r
- native implementations whenever possible\r
- (64-bit platforms may not have "standard" sizes) */\r
- while (native->format != '\0' && other->format != '\0') {\r
- ptr = other;\r
- while (ptr->format != '\0') {\r
- if (ptr->format == native->format) {\r
- /* Match faster when formats are\r
- listed in the same order */\r
- if (ptr == other)\r
- other++;\r
- /* Only use the trick if the\r
- size matches */\r
- if (ptr->size != native->size)\r
- break;\r
- /* Skip float and double, could be\r
- "unknown" float format */\r
- if (ptr->format == 'd' || ptr->format == 'f')\r
- break;\r
- ptr->pack = native->pack;\r
- ptr->unpack = native->unpack;\r
- break;\r
- }\r
- ptr++;\r
- }\r
- native++;\r
- }\r
- }\r
-\r
- /* Add some symbolic constants to the module */\r
- if (StructError == NULL) {\r
- StructError = PyErr_NewException("struct.error", NULL, NULL);\r
- if (StructError == NULL)\r
- return;\r
- }\r
-\r
- Py_INCREF(StructError);\r
- PyModule_AddObject(m, "error", StructError);\r
-\r
- Py_INCREF((PyObject*)&PyStructType);\r
- PyModule_AddObject(m, "Struct", (PyObject*)&PyStructType);\r
-\r
- PyModule_AddObject(m, "__version__", ver);\r
-\r
- PyModule_AddIntConstant(m, "_PY_STRUCT_RANGE_CHECKING", 1);\r
- PyModule_AddIntConstant(m, "_PY_STRUCT_FLOAT_COERCE", 1);\r
-}\r