Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1 | /* struct module -- pack values into and (out of) strings */ |
| 2 | |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 3 | /* New version supporting byte order, alignment and size options, |
| 4 | character strings, and unsigned numbers */ |
| 5 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6 | #include "Python.h" |
| 7 | #include <ctype.h> |
| 8 | |
| 9 | PyDoc_STRVAR(struct__doc__, |
| 10 | "Functions to convert between Python values and C structs.\n\ |
Guido van Rossum | 414fd48 | 1997-12-19 04:24:24 +0000 | [diff] [blame] | 11 | Python strings are used to hold the data representing the C struct\n\ |
| 12 | and also as format strings to describe the layout of data in the C struct.\n\ |
| 13 | \n\ |
Tim Peters | be80085 | 2001-06-11 16:45:33 +0000 | [diff] [blame] | 14 | The optional first format char indicates byte order, size and alignment:\n\ |
| 15 | @: native order, size & alignment (default)\n\ |
| 16 | =: native order, std. size & alignment\n\ |
| 17 | <: little-endian, std. size & alignment\n\ |
| 18 | >: big-endian, std. size & alignment\n\ |
| 19 | !: same as >\n\ |
Guido van Rossum | 414fd48 | 1997-12-19 04:24:24 +0000 | [diff] [blame] | 20 | \n\ |
| 21 | The remaining chars indicate types of args and must match exactly;\n\ |
| 22 | these can be preceded by a decimal repeat count:\n\ |
| 23 | x: pad byte (no data); c:char; b:signed byte; B:unsigned byte;\n\ |
| 24 | h:short; H:unsigned short; i:int; I:unsigned int;\n\ |
| 25 | l:long; L:unsigned long; f:float; d:double.\n\ |
| 26 | Special cases (preceding decimal count indicates length):\n\ |
Tim Peters | 7b9542a | 2001-06-10 23:40:19 +0000 | [diff] [blame] | 27 | s:string (array of char); p: pascal string (with count byte).\n\ |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 28 | Special case (only available in native format):\n\ |
| 29 | P:an integer type that is wide enough to hold a pointer.\n\ |
Tim Peters | 7b9542a | 2001-06-10 23:40:19 +0000 | [diff] [blame] | 30 | Special case (not in native mode unless 'long long' in platform C):\n\ |
| 31 | q:long long; Q:unsigned long long\n\ |
Guido van Rossum | 414fd48 | 1997-12-19 04:24:24 +0000 | [diff] [blame] | 32 | Whitespace between formats is ignored.\n\ |
| 33 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 34 | The variable struct.error is an exception raised on errors."); |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 35 | |
| 36 | |
| 37 | /* Exception */ |
| 38 | |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 39 | static PyObject *StructError; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 40 | |
| 41 | |
| 42 | /* Define various structs to figure out the alignments of types */ |
| 43 | |
Jack Jansen | 971e1df | 1995-02-02 14:29:10 +0000 | [diff] [blame] | 44 | |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 45 | typedef struct { char c; short x; } st_short; |
| 46 | typedef struct { char c; int x; } st_int; |
| 47 | typedef struct { char c; long x; } st_long; |
| 48 | typedef struct { char c; float x; } st_float; |
| 49 | typedef struct { char c; double x; } st_double; |
| 50 | typedef struct { char c; void *x; } st_void_p; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 51 | |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 52 | #define SHORT_ALIGN (sizeof(st_short) - sizeof(short)) |
| 53 | #define INT_ALIGN (sizeof(st_int) - sizeof(int)) |
| 54 | #define LONG_ALIGN (sizeof(st_long) - sizeof(long)) |
| 55 | #define FLOAT_ALIGN (sizeof(st_float) - sizeof(float)) |
| 56 | #define DOUBLE_ALIGN (sizeof(st_double) - sizeof(double)) |
| 57 | #define VOID_P_ALIGN (sizeof(st_void_p) - sizeof(void *)) |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 58 | |
Tim Peters | 7b9542a | 2001-06-10 23:40:19 +0000 | [diff] [blame] | 59 | /* We can't support q and Q in native mode unless the compiler does; |
| 60 | in std mode, they're 8 bytes on all platforms. */ |
| 61 | #ifdef HAVE_LONG_LONG |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 62 | typedef struct { char c; PY_LONG_LONG x; } s_long_long; |
| 63 | #define LONG_LONG_ALIGN (sizeof(s_long_long) - sizeof(PY_LONG_LONG)) |
Tim Peters | 7b9542a | 2001-06-10 23:40:19 +0000 | [diff] [blame] | 64 | #endif |
| 65 | |
Martin v. Löwis | 2af72d5 | 2000-09-15 08:10:33 +0000 | [diff] [blame] | 66 | #define STRINGIFY(x) #x |
| 67 | |
Jack Jansen | 971e1df | 1995-02-02 14:29:10 +0000 | [diff] [blame] | 68 | #ifdef __powerc |
| 69 | #pragma options align=reset |
| 70 | #endif |
| 71 | |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 72 | /* Helper to get a PyLongObject by hook or by crook. Caller should decref. */ |
| 73 | |
| 74 | static PyObject * |
| 75 | get_pylong(PyObject *v) |
| 76 | { |
| 77 | PyNumberMethods *m; |
| 78 | |
| 79 | assert(v != NULL); |
| 80 | if (PyInt_Check(v)) |
| 81 | return PyLong_FromLong(PyInt_AS_LONG(v)); |
| 82 | if (PyLong_Check(v)) { |
| 83 | Py_INCREF(v); |
| 84 | return v; |
| 85 | } |
| 86 | m = v->ob_type->tp_as_number; |
| 87 | if (m != NULL && m->nb_long != NULL) { |
| 88 | v = m->nb_long(v); |
| 89 | if (v == NULL) |
| 90 | return NULL; |
| 91 | if (PyLong_Check(v)) |
| 92 | return v; |
| 93 | Py_DECREF(v); |
| 94 | } |
| 95 | PyErr_SetString(StructError, |
| 96 | "cannot convert argument to long"); |
| 97 | return NULL; |
| 98 | } |
| 99 | |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 100 | /* Helper routine to get a Python integer and raise the appropriate error |
| 101 | if it isn't one */ |
| 102 | |
| 103 | static int |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 104 | get_long(PyObject *v, long *p) |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 105 | { |
| 106 | long x = PyInt_AsLong(v); |
| 107 | if (x == -1 && PyErr_Occurred()) { |
Fred Drake | d3dbb38 | 1998-05-28 04:35:49 +0000 | [diff] [blame] | 108 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 109 | PyErr_SetString(StructError, |
| 110 | "required argument is not an integer"); |
| 111 | return -1; |
| 112 | } |
| 113 | *p = x; |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame] | 118 | /* Same, but handling unsigned long */ |
| 119 | |
| 120 | static int |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 121 | get_ulong(PyObject *v, unsigned long *p) |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame] | 122 | { |
Guido van Rossum | 6c87eca | 1997-01-03 19:08:16 +0000 | [diff] [blame] | 123 | if (PyLong_Check(v)) { |
| 124 | unsigned long x = PyLong_AsUnsignedLong(v); |
| 125 | if (x == (unsigned long)(-1) && PyErr_Occurred()) |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame] | 126 | return -1; |
Guido van Rossum | 6c87eca | 1997-01-03 19:08:16 +0000 | [diff] [blame] | 127 | *p = x; |
| 128 | return 0; |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame] | 129 | } |
Guido van Rossum | 6c87eca | 1997-01-03 19:08:16 +0000 | [diff] [blame] | 130 | else { |
| 131 | return get_long(v, (long *)p); |
| 132 | } |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Tim Peters | 7b9542a | 2001-06-10 23:40:19 +0000 | [diff] [blame] | 135 | #ifdef HAVE_LONG_LONG |
| 136 | |
| 137 | /* Same, but handling native long long. */ |
| 138 | |
| 139 | static int |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 140 | get_longlong(PyObject *v, PY_LONG_LONG *p) |
Tim Peters | 7b9542a | 2001-06-10 23:40:19 +0000 | [diff] [blame] | 141 | { |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 142 | PY_LONG_LONG x; |
Tim Peters | 7b9542a | 2001-06-10 23:40:19 +0000 | [diff] [blame] | 143 | |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 144 | v = get_pylong(v); |
| 145 | if (v == NULL) |
| 146 | return -1; |
Tim Peters | 7b9542a | 2001-06-10 23:40:19 +0000 | [diff] [blame] | 147 | assert(PyLong_Check(v)); |
| 148 | x = PyLong_AsLongLong(v); |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 149 | Py_DECREF(v); |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 150 | if (x == (PY_LONG_LONG)-1 && PyErr_Occurred()) |
Tim Peters | 7b9542a | 2001-06-10 23:40:19 +0000 | [diff] [blame] | 151 | return -1; |
| 152 | *p = x; |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | /* Same, but handling native unsigned long long. */ |
| 157 | |
| 158 | static int |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 159 | get_ulonglong(PyObject *v, unsigned PY_LONG_LONG *p) |
Tim Peters | 7b9542a | 2001-06-10 23:40:19 +0000 | [diff] [blame] | 160 | { |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 161 | unsigned PY_LONG_LONG x; |
Tim Peters | 7b9542a | 2001-06-10 23:40:19 +0000 | [diff] [blame] | 162 | |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 163 | v = get_pylong(v); |
| 164 | if (v == NULL) |
| 165 | return -1; |
Tim Peters | 7b9542a | 2001-06-10 23:40:19 +0000 | [diff] [blame] | 166 | assert(PyLong_Check(v)); |
| 167 | x = PyLong_AsUnsignedLongLong(v); |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 168 | Py_DECREF(v); |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 169 | if (x == (unsigned PY_LONG_LONG)-1 && PyErr_Occurred()) |
Tim Peters | 7b9542a | 2001-06-10 23:40:19 +0000 | [diff] [blame] | 170 | return -1; |
| 171 | *p = x; |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | #endif |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame] | 176 | |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 177 | /* Floating point helpers */ |
| 178 | |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 179 | static PyObject * |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 180 | unpack_float(const char *p, /* start of 4-byte string */ |
| 181 | int le) /* true for little-endian, false for big-endian */ |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 182 | { |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 183 | double x; |
| 184 | |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 185 | x = _PyFloat_Unpack4((unsigned char *)p, le); |
| 186 | if (x == -1.0 && PyErr_Occurred()) |
| 187 | return NULL; |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 188 | return PyFloat_FromDouble(x); |
| 189 | } |
| 190 | |
| 191 | static PyObject * |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 192 | unpack_double(const char *p, /* start of 8-byte string */ |
| 193 | int le) /* true for little-endian, false for big-endian */ |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 194 | { |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 195 | double x; |
| 196 | |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 197 | x = _PyFloat_Unpack8((unsigned char *)p, le); |
| 198 | if (x == -1.0 && PyErr_Occurred()) |
| 199 | return NULL; |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 200 | return PyFloat_FromDouble(x); |
| 201 | } |
| 202 | |
| 203 | |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 204 | /* The translation function for each format character is table driven */ |
| 205 | |
| 206 | typedef struct _formatdef { |
| 207 | char format; |
| 208 | int size; |
| 209 | int alignment; |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 210 | PyObject* (*unpack)(const char *, |
| 211 | const struct _formatdef *); |
| 212 | int (*pack)(char *, PyObject *, |
| 213 | const struct _formatdef *); |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 214 | } formatdef; |
| 215 | |
Tim Peters | 7b9542a | 2001-06-10 23:40:19 +0000 | [diff] [blame] | 216 | /* A large number of small routines follow, with names of the form |
| 217 | |
| 218 | [bln][up]_TYPE |
| 219 | |
| 220 | [bln] distiguishes among big-endian, little-endian and native. |
| 221 | [pu] distiguishes between pack (to struct) and unpack (from struct). |
| 222 | TYPE is one of char, byte, ubyte, etc. |
| 223 | */ |
| 224 | |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 225 | /* Native mode routines. ****************************************************/ |
Guido van Rossum | 960bc54 | 2002-09-03 18:42:21 +0000 | [diff] [blame] | 226 | /* NOTE: |
| 227 | In all n[up]_<type> routines handling types larger than 1 byte, there is |
| 228 | *no* guarantee that the p pointer is properly aligned for each type, |
| 229 | therefore memcpy is called. An intermediate variable is used to |
| 230 | compensate for big-endian architectures. |
| 231 | Normally both the intermediate variable and the memcpy call will be |
| 232 | skipped by C optimisation in little-endian architectures (gcc >= 2.91 |
| 233 | does this). */ |
Tim Peters | 7b9542a | 2001-06-10 23:40:19 +0000 | [diff] [blame] | 234 | |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 235 | static PyObject * |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 236 | nu_char(const char *p, const formatdef *f) |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 237 | { |
| 238 | return PyString_FromStringAndSize(p, 1); |
| 239 | } |
| 240 | |
| 241 | static PyObject * |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 242 | nu_byte(const char *p, const formatdef *f) |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 243 | { |
| 244 | return PyInt_FromLong((long) *(signed char *)p); |
| 245 | } |
| 246 | |
| 247 | static PyObject * |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 248 | nu_ubyte(const char *p, const formatdef *f) |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 249 | { |
| 250 | return PyInt_FromLong((long) *(unsigned char *)p); |
| 251 | } |
| 252 | |
| 253 | static PyObject * |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 254 | nu_short(const char *p, const formatdef *f) |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 255 | { |
Guido van Rossum | 960bc54 | 2002-09-03 18:42:21 +0000 | [diff] [blame] | 256 | short x; |
| 257 | memcpy((char *)&x, p, sizeof x); |
| 258 | return PyInt_FromLong((long)x); |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | static PyObject * |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 262 | nu_ushort(const char *p, const formatdef *f) |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 263 | { |
Guido van Rossum | 960bc54 | 2002-09-03 18:42:21 +0000 | [diff] [blame] | 264 | unsigned short x; |
| 265 | memcpy((char *)&x, p, sizeof x); |
| 266 | return PyInt_FromLong((long)x); |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | static PyObject * |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 270 | nu_int(const char *p, const formatdef *f) |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 271 | { |
Guido van Rossum | 960bc54 | 2002-09-03 18:42:21 +0000 | [diff] [blame] | 272 | int x; |
| 273 | memcpy((char *)&x, p, sizeof x); |
| 274 | return PyInt_FromLong((long)x); |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | static PyObject * |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 278 | nu_uint(const char *p, const formatdef *f) |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 279 | { |
Guido van Rossum | 960bc54 | 2002-09-03 18:42:21 +0000 | [diff] [blame] | 280 | unsigned int x; |
| 281 | memcpy((char *)&x, p, sizeof x); |
Guido van Rossum | 6c87eca | 1997-01-03 19:08:16 +0000 | [diff] [blame] | 282 | return PyLong_FromUnsignedLong((unsigned long)x); |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | static PyObject * |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 286 | nu_long(const char *p, const formatdef *f) |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 287 | { |
Guido van Rossum | 960bc54 | 2002-09-03 18:42:21 +0000 | [diff] [blame] | 288 | long x; |
| 289 | memcpy((char *)&x, p, sizeof x); |
| 290 | return PyInt_FromLong(x); |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | static PyObject * |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 294 | nu_ulong(const char *p, const formatdef *f) |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 295 | { |
Guido van Rossum | 960bc54 | 2002-09-03 18:42:21 +0000 | [diff] [blame] | 296 | unsigned long x; |
| 297 | memcpy((char *)&x, p, sizeof x); |
| 298 | return PyLong_FromUnsignedLong(x); |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 299 | } |
| 300 | |
Tim Peters | 7b9542a | 2001-06-10 23:40:19 +0000 | [diff] [blame] | 301 | /* Native mode doesn't support q or Q unless the platform C supports |
| 302 | long long (or, on Windows, __int64). */ |
| 303 | |
| 304 | #ifdef HAVE_LONG_LONG |
| 305 | |
| 306 | static PyObject * |
| 307 | nu_longlong(const char *p, const formatdef *f) |
| 308 | { |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 309 | PY_LONG_LONG x; |
Guido van Rossum | 960bc54 | 2002-09-03 18:42:21 +0000 | [diff] [blame] | 310 | memcpy((char *)&x, p, sizeof x); |
Tim Peters | 3dac559 | 2001-07-18 20:47:31 +0000 | [diff] [blame] | 311 | return PyLong_FromLongLong(x); |
Tim Peters | 7b9542a | 2001-06-10 23:40:19 +0000 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | static PyObject * |
| 315 | nu_ulonglong(const char *p, const formatdef *f) |
| 316 | { |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 317 | unsigned PY_LONG_LONG x; |
Guido van Rossum | 960bc54 | 2002-09-03 18:42:21 +0000 | [diff] [blame] | 318 | memcpy((char *)&x, p, sizeof x); |
Tim Peters | 3dac559 | 2001-07-18 20:47:31 +0000 | [diff] [blame] | 319 | return PyLong_FromUnsignedLongLong(x); |
Tim Peters | 7b9542a | 2001-06-10 23:40:19 +0000 | [diff] [blame] | 320 | } |
Guido van Rossum | 960bc54 | 2002-09-03 18:42:21 +0000 | [diff] [blame] | 321 | |
Tim Peters | 7b9542a | 2001-06-10 23:40:19 +0000 | [diff] [blame] | 322 | #endif |
| 323 | |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 324 | static PyObject * |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 325 | nu_float(const char *p, const formatdef *f) |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 326 | { |
| 327 | float x; |
Guido van Rossum | 960bc54 | 2002-09-03 18:42:21 +0000 | [diff] [blame] | 328 | memcpy((char *)&x, p, sizeof x); |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 329 | return PyFloat_FromDouble((double)x); |
| 330 | } |
| 331 | |
| 332 | static PyObject * |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 333 | nu_double(const char *p, const formatdef *f) |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 334 | { |
| 335 | double x; |
Guido van Rossum | 960bc54 | 2002-09-03 18:42:21 +0000 | [diff] [blame] | 336 | memcpy((char *)&x, p, sizeof x); |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 337 | return PyFloat_FromDouble(x); |
| 338 | } |
| 339 | |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 340 | static PyObject * |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 341 | nu_void_p(const char *p, const formatdef *f) |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 342 | { |
Guido van Rossum | 960bc54 | 2002-09-03 18:42:21 +0000 | [diff] [blame] | 343 | void *x; |
| 344 | memcpy((char *)&x, p, sizeof x); |
| 345 | return PyLong_FromVoidPtr(x); |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 346 | } |
| 347 | |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 348 | static int |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 349 | np_byte(char *p, PyObject *v, const formatdef *f) |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 350 | { |
| 351 | long x; |
| 352 | if (get_long(v, &x) < 0) |
| 353 | return -1; |
Martin v. Löwis | 66de549 | 2000-09-15 07:31:57 +0000 | [diff] [blame] | 354 | if (x < -128 || x > 127){ |
| 355 | PyErr_SetString(StructError, |
| 356 | "byte format requires -128<=number<=127"); |
| 357 | return -1; |
| 358 | } |
| 359 | *p = (char)x; |
| 360 | return 0; |
| 361 | } |
| 362 | |
| 363 | static int |
| 364 | np_ubyte(char *p, PyObject *v, const formatdef *f) |
| 365 | { |
| 366 | long x; |
| 367 | if (get_long(v, &x) < 0) |
| 368 | return -1; |
| 369 | if (x < 0 || x > 255){ |
| 370 | PyErr_SetString(StructError, |
| 371 | "ubyte format requires 0<=number<=255"); |
| 372 | return -1; |
| 373 | } |
Guido van Rossum | 7844e38 | 1997-04-11 20:44:04 +0000 | [diff] [blame] | 374 | *p = (char)x; |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 375 | return 0; |
| 376 | } |
| 377 | |
| 378 | static int |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 379 | np_char(char *p, PyObject *v, const formatdef *f) |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 380 | { |
| 381 | if (!PyString_Check(v) || PyString_Size(v) != 1) { |
| 382 | PyErr_SetString(StructError, |
| 383 | "char format require string of length 1"); |
| 384 | return -1; |
| 385 | } |
| 386 | *p = *PyString_AsString(v); |
| 387 | return 0; |
| 388 | } |
| 389 | |
| 390 | static int |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 391 | np_short(char *p, PyObject *v, const formatdef *f) |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 392 | { |
| 393 | long x; |
Guido van Rossum | 960bc54 | 2002-09-03 18:42:21 +0000 | [diff] [blame] | 394 | short y; |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 395 | if (get_long(v, &x) < 0) |
| 396 | return -1; |
Martin v. Löwis | 2af72d5 | 2000-09-15 08:10:33 +0000 | [diff] [blame] | 397 | if (x < SHRT_MIN || x > SHRT_MAX){ |
Martin v. Löwis | 66de549 | 2000-09-15 07:31:57 +0000 | [diff] [blame] | 398 | PyErr_SetString(StructError, |
Martin v. Löwis | 2af72d5 | 2000-09-15 08:10:33 +0000 | [diff] [blame] | 399 | "short format requires " STRINGIFY(SHRT_MIN) |
Guido van Rossum | 960bc54 | 2002-09-03 18:42:21 +0000 | [diff] [blame] | 400 | "<=number<=" STRINGIFY(SHRT_MAX)); |
Martin v. Löwis | 66de549 | 2000-09-15 07:31:57 +0000 | [diff] [blame] | 401 | return -1; |
| 402 | } |
Guido van Rossum | 960bc54 | 2002-09-03 18:42:21 +0000 | [diff] [blame] | 403 | y = (short)x; |
| 404 | memcpy(p, (char *)&y, sizeof y); |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 405 | return 0; |
| 406 | } |
| 407 | |
| 408 | static int |
Martin v. Löwis | 66de549 | 2000-09-15 07:31:57 +0000 | [diff] [blame] | 409 | np_ushort(char *p, PyObject *v, const formatdef *f) |
| 410 | { |
| 411 | long x; |
Guido van Rossum | 960bc54 | 2002-09-03 18:42:21 +0000 | [diff] [blame] | 412 | unsigned short y; |
Martin v. Löwis | 66de549 | 2000-09-15 07:31:57 +0000 | [diff] [blame] | 413 | if (get_long(v, &x) < 0) |
| 414 | return -1; |
Martin v. Löwis | 2af72d5 | 2000-09-15 08:10:33 +0000 | [diff] [blame] | 415 | if (x < 0 || x > USHRT_MAX){ |
Martin v. Löwis | 66de549 | 2000-09-15 07:31:57 +0000 | [diff] [blame] | 416 | PyErr_SetString(StructError, |
Martin v. Löwis | 2af72d5 | 2000-09-15 08:10:33 +0000 | [diff] [blame] | 417 | "short format requires 0<=number<=" STRINGIFY(USHRT_MAX)); |
Martin v. Löwis | 66de549 | 2000-09-15 07:31:57 +0000 | [diff] [blame] | 418 | return -1; |
| 419 | } |
Guido van Rossum | 960bc54 | 2002-09-03 18:42:21 +0000 | [diff] [blame] | 420 | y = (unsigned short)x; |
| 421 | memcpy(p, (char *)&y, sizeof y); |
Martin v. Löwis | 66de549 | 2000-09-15 07:31:57 +0000 | [diff] [blame] | 422 | return 0; |
| 423 | } |
| 424 | |
| 425 | static int |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 426 | np_int(char *p, PyObject *v, const formatdef *f) |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 427 | { |
| 428 | long x; |
Guido van Rossum | 960bc54 | 2002-09-03 18:42:21 +0000 | [diff] [blame] | 429 | int y; |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 430 | if (get_long(v, &x) < 0) |
| 431 | return -1; |
Guido van Rossum | 960bc54 | 2002-09-03 18:42:21 +0000 | [diff] [blame] | 432 | y = (int)x; |
| 433 | memcpy(p, (char *)&y, sizeof y); |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 434 | return 0; |
| 435 | } |
| 436 | |
| 437 | static int |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 438 | np_uint(char *p, PyObject *v, const formatdef *f) |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame] | 439 | { |
| 440 | unsigned long x; |
Guido van Rossum | 960bc54 | 2002-09-03 18:42:21 +0000 | [diff] [blame] | 441 | unsigned int y; |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame] | 442 | if (get_ulong(v, &x) < 0) |
| 443 | return -1; |
Guido van Rossum | 960bc54 | 2002-09-03 18:42:21 +0000 | [diff] [blame] | 444 | y = (unsigned int)x; |
| 445 | memcpy(p, (char *)&y, sizeof y); |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame] | 446 | return 0; |
| 447 | } |
| 448 | |
| 449 | static int |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 450 | np_long(char *p, PyObject *v, const formatdef *f) |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 451 | { |
| 452 | long x; |
| 453 | if (get_long(v, &x) < 0) |
| 454 | return -1; |
Guido van Rossum | 960bc54 | 2002-09-03 18:42:21 +0000 | [diff] [blame] | 455 | memcpy(p, (char *)&x, sizeof x); |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 456 | return 0; |
| 457 | } |
| 458 | |
| 459 | static int |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 460 | np_ulong(char *p, PyObject *v, const formatdef *f) |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame] | 461 | { |
| 462 | unsigned long x; |
| 463 | if (get_ulong(v, &x) < 0) |
| 464 | return -1; |
Guido van Rossum | 960bc54 | 2002-09-03 18:42:21 +0000 | [diff] [blame] | 465 | memcpy(p, (char *)&x, sizeof x); |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame] | 466 | return 0; |
| 467 | } |
| 468 | |
Tim Peters | 7b9542a | 2001-06-10 23:40:19 +0000 | [diff] [blame] | 469 | #ifdef HAVE_LONG_LONG |
| 470 | |
| 471 | static int |
| 472 | np_longlong(char *p, PyObject *v, const formatdef *f) |
| 473 | { |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 474 | PY_LONG_LONG x; |
Tim Peters | 7b9542a | 2001-06-10 23:40:19 +0000 | [diff] [blame] | 475 | if (get_longlong(v, &x) < 0) |
| 476 | return -1; |
Guido van Rossum | 960bc54 | 2002-09-03 18:42:21 +0000 | [diff] [blame] | 477 | memcpy(p, (char *)&x, sizeof x); |
Tim Peters | 7b9542a | 2001-06-10 23:40:19 +0000 | [diff] [blame] | 478 | return 0; |
| 479 | } |
| 480 | |
| 481 | static int |
| 482 | np_ulonglong(char *p, PyObject *v, const formatdef *f) |
| 483 | { |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 484 | unsigned PY_LONG_LONG x; |
Tim Peters | 7b9542a | 2001-06-10 23:40:19 +0000 | [diff] [blame] | 485 | if (get_ulonglong(v, &x) < 0) |
| 486 | return -1; |
Guido van Rossum | 960bc54 | 2002-09-03 18:42:21 +0000 | [diff] [blame] | 487 | memcpy(p, (char *)&x, sizeof x); |
Tim Peters | 7b9542a | 2001-06-10 23:40:19 +0000 | [diff] [blame] | 488 | return 0; |
| 489 | } |
Tim Peters | 7b9542a | 2001-06-10 23:40:19 +0000 | [diff] [blame] | 490 | #endif |
| 491 | |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame] | 492 | static int |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 493 | np_float(char *p, PyObject *v, const formatdef *f) |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 494 | { |
| 495 | float x = (float)PyFloat_AsDouble(v); |
| 496 | if (x == -1 && PyErr_Occurred()) { |
| 497 | PyErr_SetString(StructError, |
| 498 | "required argument is not a float"); |
| 499 | return -1; |
| 500 | } |
Guido van Rossum | 960bc54 | 2002-09-03 18:42:21 +0000 | [diff] [blame] | 501 | memcpy(p, (char *)&x, sizeof x); |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 502 | return 0; |
| 503 | } |
| 504 | |
| 505 | static int |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 506 | np_double(char *p, PyObject *v, const formatdef *f) |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 507 | { |
| 508 | double x = PyFloat_AsDouble(v); |
| 509 | if (x == -1 && PyErr_Occurred()) { |
| 510 | PyErr_SetString(StructError, |
| 511 | "required argument is not a float"); |
| 512 | return -1; |
| 513 | } |
| 514 | memcpy(p, (char *)&x, sizeof(double)); |
| 515 | return 0; |
| 516 | } |
| 517 | |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 518 | static int |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 519 | np_void_p(char *p, PyObject *v, const formatdef *f) |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 520 | { |
Armin Rigo | 9f90439 | 2004-09-27 19:27:51 +0000 | [diff] [blame] | 521 | void *x; |
| 522 | |
| 523 | v = get_pylong(v); |
| 524 | if (v == NULL) |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 525 | return -1; |
Armin Rigo | 9f90439 | 2004-09-27 19:27:51 +0000 | [diff] [blame] | 526 | assert(PyLong_Check(v)); |
| 527 | x = PyLong_AsVoidPtr(v); |
| 528 | Py_DECREF(v); |
| 529 | if (x == NULL && PyErr_Occurred()) |
| 530 | return -1; |
Guido van Rossum | 960bc54 | 2002-09-03 18:42:21 +0000 | [diff] [blame] | 531 | memcpy(p, (char *)&x, sizeof x); |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 532 | return 0; |
| 533 | } |
| 534 | |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 535 | static formatdef native_table[] = { |
| 536 | {'x', sizeof(char), 0, NULL}, |
| 537 | {'b', sizeof(char), 0, nu_byte, np_byte}, |
Martin v. Löwis | 66de549 | 2000-09-15 07:31:57 +0000 | [diff] [blame] | 538 | {'B', sizeof(char), 0, nu_ubyte, np_ubyte}, |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 539 | {'c', sizeof(char), 0, nu_char, np_char}, |
| 540 | {'s', sizeof(char), 0, NULL}, |
Guido van Rossum | 9eb671f | 1997-09-05 07:08:39 +0000 | [diff] [blame] | 541 | {'p', sizeof(char), 0, NULL}, |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 542 | {'h', sizeof(short), SHORT_ALIGN, nu_short, np_short}, |
Martin v. Löwis | 66de549 | 2000-09-15 07:31:57 +0000 | [diff] [blame] | 543 | {'H', sizeof(short), SHORT_ALIGN, nu_ushort, np_ushort}, |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 544 | {'i', sizeof(int), INT_ALIGN, nu_int, np_int}, |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame] | 545 | {'I', sizeof(int), INT_ALIGN, nu_uint, np_uint}, |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 546 | {'l', sizeof(long), LONG_ALIGN, nu_long, np_long}, |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame] | 547 | {'L', sizeof(long), LONG_ALIGN, nu_ulong, np_ulong}, |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 548 | {'f', sizeof(float), FLOAT_ALIGN, nu_float, np_float}, |
| 549 | {'d', sizeof(double), DOUBLE_ALIGN, nu_double, np_double}, |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 550 | {'P', sizeof(void *), VOID_P_ALIGN, nu_void_p, np_void_p}, |
Tim Peters | 7b9542a | 2001-06-10 23:40:19 +0000 | [diff] [blame] | 551 | #ifdef HAVE_LONG_LONG |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 552 | {'q', sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_longlong, np_longlong}, |
| 553 | {'Q', sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_ulonglong,np_ulonglong}, |
Tim Peters | 7b9542a | 2001-06-10 23:40:19 +0000 | [diff] [blame] | 554 | #endif |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 555 | {0} |
| 556 | }; |
| 557 | |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 558 | /* Big-endian routines. *****************************************************/ |
| 559 | |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 560 | static PyObject * |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 561 | bu_int(const char *p, const formatdef *f) |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 562 | { |
| 563 | long x = 0; |
| 564 | int i = f->size; |
| 565 | do { |
| 566 | x = (x<<8) | (*p++ & 0xFF); |
| 567 | } while (--i > 0); |
Tim Peters | f0e717b | 2001-04-08 23:39:38 +0000 | [diff] [blame] | 568 | /* Extend the sign bit. */ |
| 569 | if (SIZEOF_LONG > f->size) |
| 570 | x |= -(x & (1L << (8*f->size - 1))); |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 571 | return PyInt_FromLong(x); |
| 572 | } |
| 573 | |
| 574 | static PyObject * |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 575 | bu_uint(const char *p, const formatdef *f) |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 576 | { |
Guido van Rossum | 6c87eca | 1997-01-03 19:08:16 +0000 | [diff] [blame] | 577 | unsigned long x = 0; |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 578 | int i = f->size; |
| 579 | do { |
| 580 | x = (x<<8) | (*p++ & 0xFF); |
| 581 | } while (--i > 0); |
Guido van Rossum | 39ef227 | 1998-06-29 04:00:40 +0000 | [diff] [blame] | 582 | if (f->size >= 4) |
| 583 | return PyLong_FromUnsignedLong(x); |
| 584 | else |
| 585 | return PyInt_FromLong((long)x); |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 586 | } |
| 587 | |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 588 | static PyObject * |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 589 | bu_longlong(const char *p, const formatdef *f) |
| 590 | { |
| 591 | return _PyLong_FromByteArray((const unsigned char *)p, |
| 592 | 8, |
| 593 | 0, /* little-endian */ |
| 594 | 1 /* signed */); |
| 595 | } |
| 596 | |
| 597 | static PyObject * |
| 598 | bu_ulonglong(const char *p, const formatdef *f) |
| 599 | { |
| 600 | return _PyLong_FromByteArray((const unsigned char *)p, |
| 601 | 8, |
| 602 | 0, /* little-endian */ |
| 603 | 0 /* signed */); |
| 604 | } |
| 605 | |
| 606 | static PyObject * |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 607 | bu_float(const char *p, const formatdef *f) |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 608 | { |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 609 | return unpack_float(p, 0); |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | static PyObject * |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 613 | bu_double(const char *p, const formatdef *f) |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 614 | { |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 615 | return unpack_double(p, 0); |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 616 | } |
| 617 | |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 618 | static int |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 619 | bp_int(char *p, PyObject *v, const formatdef *f) |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 620 | { |
| 621 | long x; |
| 622 | int i; |
| 623 | if (get_long(v, &x) < 0) |
| 624 | return -1; |
| 625 | i = f->size; |
| 626 | do { |
Guido van Rossum | 7844e38 | 1997-04-11 20:44:04 +0000 | [diff] [blame] | 627 | p[--i] = (char)x; |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 628 | x >>= 8; |
| 629 | } while (i > 0); |
| 630 | return 0; |
| 631 | } |
| 632 | |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame] | 633 | static int |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 634 | bp_uint(char *p, PyObject *v, const formatdef *f) |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame] | 635 | { |
| 636 | unsigned long x; |
| 637 | int i; |
| 638 | if (get_ulong(v, &x) < 0) |
| 639 | return -1; |
| 640 | i = f->size; |
| 641 | do { |
Guido van Rossum | 7844e38 | 1997-04-11 20:44:04 +0000 | [diff] [blame] | 642 | p[--i] = (char)x; |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame] | 643 | x >>= 8; |
| 644 | } while (i > 0); |
| 645 | return 0; |
| 646 | } |
| 647 | |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 648 | static int |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 649 | bp_longlong(char *p, PyObject *v, const formatdef *f) |
| 650 | { |
| 651 | int res; |
| 652 | v = get_pylong(v); |
Tim Peters | da9c5b3 | 2001-06-13 01:26:35 +0000 | [diff] [blame] | 653 | if (v == NULL) |
| 654 | return -1; |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 655 | res = _PyLong_AsByteArray((PyLongObject *)v, |
| 656 | (unsigned char *)p, |
| 657 | 8, |
| 658 | 0, /* little_endian */ |
| 659 | 1 /* signed */); |
| 660 | Py_DECREF(v); |
| 661 | return res; |
| 662 | } |
| 663 | |
| 664 | static int |
| 665 | bp_ulonglong(char *p, PyObject *v, const formatdef *f) |
| 666 | { |
| 667 | int res; |
| 668 | v = get_pylong(v); |
Tim Peters | da9c5b3 | 2001-06-13 01:26:35 +0000 | [diff] [blame] | 669 | if (v == NULL) |
| 670 | return -1; |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 671 | res = _PyLong_AsByteArray((PyLongObject *)v, |
| 672 | (unsigned char *)p, |
| 673 | 8, |
| 674 | 0, /* little_endian */ |
| 675 | 0 /* signed */); |
| 676 | Py_DECREF(v); |
| 677 | return res; |
| 678 | } |
| 679 | |
| 680 | static int |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 681 | bp_float(char *p, PyObject *v, const formatdef *f) |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 682 | { |
| 683 | double x = PyFloat_AsDouble(v); |
| 684 | if (x == -1 && PyErr_Occurred()) { |
| 685 | PyErr_SetString(StructError, |
| 686 | "required argument is not a float"); |
| 687 | return -1; |
| 688 | } |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 689 | return _PyFloat_Pack4(x, (unsigned char *)p, 0); |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | static int |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 693 | bp_double(char *p, PyObject *v, const formatdef *f) |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 694 | { |
| 695 | double x = PyFloat_AsDouble(v); |
| 696 | if (x == -1 && PyErr_Occurred()) { |
| 697 | PyErr_SetString(StructError, |
| 698 | "required argument is not a float"); |
| 699 | return -1; |
| 700 | } |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 701 | return _PyFloat_Pack8(x, (unsigned char *)p, 0); |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 702 | } |
| 703 | |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 704 | static formatdef bigendian_table[] = { |
| 705 | {'x', 1, 0, NULL}, |
| 706 | {'b', 1, 0, bu_int, bp_int}, |
| 707 | {'B', 1, 0, bu_uint, bp_int}, |
| 708 | {'c', 1, 0, nu_char, np_char}, |
| 709 | {'s', 1, 0, NULL}, |
Guido van Rossum | 9eb671f | 1997-09-05 07:08:39 +0000 | [diff] [blame] | 710 | {'p', 1, 0, NULL}, |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 711 | {'h', 2, 0, bu_int, bp_int}, |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame] | 712 | {'H', 2, 0, bu_uint, bp_uint}, |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 713 | {'i', 4, 0, bu_int, bp_int}, |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame] | 714 | {'I', 4, 0, bu_uint, bp_uint}, |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 715 | {'l', 4, 0, bu_int, bp_int}, |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame] | 716 | {'L', 4, 0, bu_uint, bp_uint}, |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 717 | {'q', 8, 0, bu_longlong, bp_longlong}, |
| 718 | {'Q', 8, 0, bu_ulonglong, bp_ulonglong}, |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 719 | {'f', 4, 0, bu_float, bp_float}, |
| 720 | {'d', 8, 0, bu_double, bp_double}, |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 721 | {0} |
| 722 | }; |
| 723 | |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 724 | /* Little-endian routines. *****************************************************/ |
| 725 | |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 726 | static PyObject * |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 727 | lu_int(const char *p, const formatdef *f) |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 728 | { |
| 729 | long x = 0; |
| 730 | int i = f->size; |
| 731 | do { |
| 732 | x = (x<<8) | (p[--i] & 0xFF); |
| 733 | } while (i > 0); |
Tim Peters | f0e717b | 2001-04-08 23:39:38 +0000 | [diff] [blame] | 734 | /* Extend the sign bit. */ |
| 735 | if (SIZEOF_LONG > f->size) |
| 736 | x |= -(x & (1L << (8*f->size - 1))); |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 737 | return PyInt_FromLong(x); |
| 738 | } |
| 739 | |
| 740 | static PyObject * |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 741 | lu_uint(const char *p, const formatdef *f) |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 742 | { |
Guido van Rossum | 6c87eca | 1997-01-03 19:08:16 +0000 | [diff] [blame] | 743 | unsigned long x = 0; |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 744 | int i = f->size; |
| 745 | do { |
| 746 | x = (x<<8) | (p[--i] & 0xFF); |
| 747 | } while (i > 0); |
Guido van Rossum | 39ef227 | 1998-06-29 04:00:40 +0000 | [diff] [blame] | 748 | if (f->size >= 4) |
| 749 | return PyLong_FromUnsignedLong(x); |
| 750 | else |
| 751 | return PyInt_FromLong((long)x); |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 752 | } |
| 753 | |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 754 | static PyObject * |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 755 | lu_longlong(const char *p, const formatdef *f) |
| 756 | { |
| 757 | return _PyLong_FromByteArray((const unsigned char *)p, |
| 758 | 8, |
| 759 | 1, /* little-endian */ |
| 760 | 1 /* signed */); |
| 761 | } |
| 762 | |
| 763 | static PyObject * |
| 764 | lu_ulonglong(const char *p, const formatdef *f) |
| 765 | { |
| 766 | return _PyLong_FromByteArray((const unsigned char *)p, |
| 767 | 8, |
| 768 | 1, /* little-endian */ |
| 769 | 0 /* signed */); |
| 770 | } |
| 771 | |
| 772 | static PyObject * |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 773 | lu_float(const char *p, const formatdef *f) |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 774 | { |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 775 | return unpack_float(p, 1); |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | static PyObject * |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 779 | lu_double(const char *p, const formatdef *f) |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 780 | { |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 781 | return unpack_double(p, 1); |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 782 | } |
| 783 | |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 784 | static int |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 785 | lp_int(char *p, PyObject *v, const formatdef *f) |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 786 | { |
| 787 | long x; |
| 788 | int i; |
| 789 | if (get_long(v, &x) < 0) |
| 790 | return -1; |
| 791 | i = f->size; |
| 792 | do { |
Guido van Rossum | 7844e38 | 1997-04-11 20:44:04 +0000 | [diff] [blame] | 793 | *p++ = (char)x; |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 794 | x >>= 8; |
| 795 | } while (--i > 0); |
| 796 | return 0; |
| 797 | } |
| 798 | |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame] | 799 | static int |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 800 | lp_uint(char *p, PyObject *v, const formatdef *f) |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame] | 801 | { |
| 802 | unsigned long x; |
| 803 | int i; |
| 804 | if (get_ulong(v, &x) < 0) |
| 805 | return -1; |
| 806 | i = f->size; |
| 807 | do { |
Guido van Rossum | 7844e38 | 1997-04-11 20:44:04 +0000 | [diff] [blame] | 808 | *p++ = (char)x; |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame] | 809 | x >>= 8; |
| 810 | } while (--i > 0); |
| 811 | return 0; |
| 812 | } |
| 813 | |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 814 | static int |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 815 | lp_longlong(char *p, PyObject *v, const formatdef *f) |
| 816 | { |
| 817 | int res; |
| 818 | v = get_pylong(v); |
Tim Peters | da9c5b3 | 2001-06-13 01:26:35 +0000 | [diff] [blame] | 819 | if (v == NULL) |
| 820 | return -1; |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 821 | res = _PyLong_AsByteArray((PyLongObject*)v, |
| 822 | (unsigned char *)p, |
| 823 | 8, |
| 824 | 1, /* little_endian */ |
| 825 | 1 /* signed */); |
| 826 | Py_DECREF(v); |
| 827 | return res; |
| 828 | } |
| 829 | |
| 830 | static int |
| 831 | lp_ulonglong(char *p, PyObject *v, const formatdef *f) |
| 832 | { |
| 833 | int res; |
| 834 | v = get_pylong(v); |
Tim Peters | da9c5b3 | 2001-06-13 01:26:35 +0000 | [diff] [blame] | 835 | if (v == NULL) |
| 836 | return -1; |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 837 | res = _PyLong_AsByteArray((PyLongObject*)v, |
| 838 | (unsigned char *)p, |
| 839 | 8, |
| 840 | 1, /* little_endian */ |
| 841 | 0 /* signed */); |
| 842 | Py_DECREF(v); |
| 843 | return res; |
| 844 | } |
| 845 | |
| 846 | static int |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 847 | lp_float(char *p, PyObject *v, const formatdef *f) |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 848 | { |
| 849 | double x = PyFloat_AsDouble(v); |
| 850 | if (x == -1 && PyErr_Occurred()) { |
| 851 | PyErr_SetString(StructError, |
| 852 | "required argument is not a float"); |
| 853 | return -1; |
| 854 | } |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 855 | return _PyFloat_Pack4(x, (unsigned char *)p, 1); |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | static int |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 859 | lp_double(char *p, PyObject *v, const formatdef *f) |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 860 | { |
| 861 | double x = PyFloat_AsDouble(v); |
| 862 | if (x == -1 && PyErr_Occurred()) { |
| 863 | PyErr_SetString(StructError, |
| 864 | "required argument is not a float"); |
| 865 | return -1; |
| 866 | } |
Tim Peters | 9905b94 | 2003-03-20 20:53:32 +0000 | [diff] [blame] | 867 | return _PyFloat_Pack8(x, (unsigned char *)p, 1); |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 868 | } |
| 869 | |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 870 | static formatdef lilendian_table[] = { |
| 871 | {'x', 1, 0, NULL}, |
| 872 | {'b', 1, 0, lu_int, lp_int}, |
| 873 | {'B', 1, 0, lu_uint, lp_int}, |
| 874 | {'c', 1, 0, nu_char, np_char}, |
| 875 | {'s', 1, 0, NULL}, |
Guido van Rossum | 9eb671f | 1997-09-05 07:08:39 +0000 | [diff] [blame] | 876 | {'p', 1, 0, NULL}, |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 877 | {'h', 2, 0, lu_int, lp_int}, |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame] | 878 | {'H', 2, 0, lu_uint, lp_uint}, |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 879 | {'i', 4, 0, lu_int, lp_int}, |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame] | 880 | {'I', 4, 0, lu_uint, lp_uint}, |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 881 | {'l', 4, 0, lu_int, lp_int}, |
Guido van Rossum | 60c5061 | 1996-12-31 16:29:52 +0000 | [diff] [blame] | 882 | {'L', 4, 0, lu_uint, lp_uint}, |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 883 | {'q', 8, 0, lu_longlong, lp_longlong}, |
| 884 | {'Q', 8, 0, lu_ulonglong, lp_ulonglong}, |
Guido van Rossum | 74679b4 | 1997-01-02 22:21:36 +0000 | [diff] [blame] | 885 | {'f', 4, 0, lu_float, lp_float}, |
| 886 | {'d', 8, 0, lu_double, lp_double}, |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 887 | {0} |
| 888 | }; |
| 889 | |
| 890 | |
| 891 | static const formatdef * |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 892 | whichtable(char **pfmt) |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 893 | { |
| 894 | const char *fmt = (*pfmt)++; /* May be backed out of later */ |
| 895 | switch (*fmt) { |
| 896 | case '<': |
| 897 | return lilendian_table; |
| 898 | case '>': |
| 899 | case '!': /* Network byte order is big-endian */ |
| 900 | return bigendian_table; |
| 901 | case '=': { /* Host byte order -- different from native in aligment! */ |
| 902 | int n = 1; |
| 903 | char *p = (char *) &n; |
| 904 | if (*p == 1) |
| 905 | return lilendian_table; |
| 906 | else |
| 907 | return bigendian_table; |
| 908 | } |
| 909 | default: |
| 910 | --*pfmt; /* Back out of pointer increment */ |
| 911 | /* Fall through */ |
| 912 | case '@': |
| 913 | return native_table; |
| 914 | } |
| 915 | } |
| 916 | |
| 917 | |
| 918 | /* Get the table entry for a format code */ |
| 919 | |
| 920 | static const formatdef * |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 921 | getentry(int c, const formatdef *f) |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 922 | { |
| 923 | for (; f->format != '\0'; f++) { |
| 924 | if (f->format == c) { |
| 925 | return f; |
| 926 | } |
| 927 | } |
| 928 | PyErr_SetString(StructError, "bad char in struct format"); |
| 929 | return NULL; |
| 930 | } |
| 931 | |
| 932 | |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 933 | /* Align a size according to a format code */ |
| 934 | |
| 935 | static int |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 936 | align(int size, int c, const formatdef *e) |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 937 | { |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 938 | if (e->format == c) { |
| 939 | if (e->alignment) { |
| 940 | size = ((size + e->alignment - 1) |
| 941 | / e->alignment) |
| 942 | * e->alignment; |
| 943 | } |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 944 | } |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 945 | return size; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | |
| 949 | /* calculate the size of a format string */ |
| 950 | |
| 951 | static int |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 952 | calcsize(const char *fmt, const formatdef *f) |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 953 | { |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 954 | const formatdef *e; |
| 955 | const char *s; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 956 | char c; |
| 957 | int size, num, itemsize, x; |
| 958 | |
| 959 | s = fmt; |
| 960 | size = 0; |
| 961 | while ((c = *s++) != '\0') { |
Raymond Hettinger | b3f55f4 | 2005-08-26 08:39:56 +0000 | [diff] [blame] | 962 | if (isspace(Py_CHARMASK(c))) |
Guido van Rossum | e20aef5 | 1997-08-26 20:39:54 +0000 | [diff] [blame] | 963 | continue; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 964 | if ('0' <= c && c <= '9') { |
| 965 | num = c - '0'; |
| 966 | while ('0' <= (c = *s++) && c <= '9') { |
| 967 | x = num*10 + (c - '0'); |
| 968 | if (x/10 != num) { |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 969 | PyErr_SetString( |
| 970 | StructError, |
| 971 | "overflow in item count"); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 972 | return -1; |
| 973 | } |
| 974 | num = x; |
| 975 | } |
| 976 | if (c == '\0') |
| 977 | break; |
| 978 | } |
| 979 | else |
| 980 | num = 1; |
Tim Peters | 2d4e077 | 2001-06-11 16:57:33 +0000 | [diff] [blame] | 981 | |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 982 | e = getentry(c, f); |
| 983 | if (e == NULL) |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 984 | return -1; |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 985 | itemsize = e->size; |
| 986 | size = align(size, c, e); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 987 | x = num * itemsize; |
| 988 | size += x; |
| 989 | if (x/itemsize != num || size < 0) { |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 990 | PyErr_SetString(StructError, |
Guido van Rossum | 960bc54 | 2002-09-03 18:42:21 +0000 | [diff] [blame] | 991 | "total struct size too long"); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 992 | return -1; |
| 993 | } |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 994 | } |
| 995 | |
| 996 | return size; |
| 997 | } |
| 998 | |
| 999 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1000 | PyDoc_STRVAR(calcsize__doc__, |
| 1001 | "calcsize(fmt) -> int\n\ |
Guido van Rossum | 414fd48 | 1997-12-19 04:24:24 +0000 | [diff] [blame] | 1002 | Return size of C struct described by format string fmt.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1003 | See struct.__doc__ for more on format strings."); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1004 | |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 1005 | static PyObject * |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 1006 | struct_calcsize(PyObject *self, PyObject *args) |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1007 | { |
| 1008 | char *fmt; |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1009 | const formatdef *f; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1010 | int size; |
| 1011 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1012 | if (!PyArg_ParseTuple(args, "s:calcsize", &fmt)) |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1013 | return NULL; |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1014 | f = whichtable(&fmt); |
| 1015 | size = calcsize(fmt, f); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1016 | if (size < 0) |
| 1017 | return NULL; |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 1018 | return PyInt_FromLong((long)size); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1019 | } |
| 1020 | |
| 1021 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1022 | PyDoc_STRVAR(pack__doc__, |
| 1023 | "pack(fmt, v1, v2, ...) -> string\n\ |
Guido van Rossum | 414fd48 | 1997-12-19 04:24:24 +0000 | [diff] [blame] | 1024 | Return string containing values v1, v2, ... packed according to fmt.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1025 | See struct.__doc__ for more on format strings."); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1026 | |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 1027 | static PyObject * |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 1028 | struct_pack(PyObject *self, PyObject *args) |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1029 | { |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1030 | const formatdef *f, *e; |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 1031 | PyObject *format, *result, *v; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1032 | char *fmt; |
| 1033 | int size, num; |
Martin v. Löwis | ad0a462 | 2006-02-16 14:30:23 +0000 | [diff] [blame] | 1034 | Py_ssize_t i, n; |
Guido van Rossum | b9d338c | 1997-01-03 15:40:33 +0000 | [diff] [blame] | 1035 | char *s, *res, *restart, *nres; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1036 | char c; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1037 | |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 1038 | if (args == NULL || !PyTuple_Check(args) || |
| 1039 | (n = PyTuple_Size(args)) < 1) |
Guido van Rossum | 960bc54 | 2002-09-03 18:42:21 +0000 | [diff] [blame] | 1040 | { |
Tim Peters | 2d4e077 | 2001-06-11 16:57:33 +0000 | [diff] [blame] | 1041 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 137507e | 2000-06-01 02:02:46 +0000 | [diff] [blame] | 1042 | "struct.pack requires at least one argument"); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1043 | return NULL; |
| 1044 | } |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 1045 | format = PyTuple_GetItem(args, 0); |
Neal Norwitz | 187ae56 | 2002-04-02 18:17:57 +0000 | [diff] [blame] | 1046 | fmt = PyString_AsString(format); |
| 1047 | if (!fmt) |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1048 | return NULL; |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1049 | f = whichtable(&fmt); |
| 1050 | size = calcsize(fmt, f); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1051 | if (size < 0) |
| 1052 | return NULL; |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 1053 | result = PyString_FromStringAndSize((char *)NULL, size); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1054 | if (result == NULL) |
| 1055 | return NULL; |
| 1056 | |
| 1057 | s = fmt; |
| 1058 | i = 1; |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 1059 | res = restart = PyString_AsString(result); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1060 | |
| 1061 | while ((c = *s++) != '\0') { |
Raymond Hettinger | b3f55f4 | 2005-08-26 08:39:56 +0000 | [diff] [blame] | 1062 | if (isspace(Py_CHARMASK(c))) |
Guido van Rossum | e20aef5 | 1997-08-26 20:39:54 +0000 | [diff] [blame] | 1063 | continue; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1064 | if ('0' <= c && c <= '9') { |
| 1065 | num = c - '0'; |
| 1066 | while ('0' <= (c = *s++) && c <= '9') |
| 1067 | num = num*10 + (c - '0'); |
| 1068 | if (c == '\0') |
| 1069 | break; |
| 1070 | } |
| 1071 | else |
| 1072 | num = 1; |
| 1073 | |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1074 | e = getentry(c, f); |
| 1075 | if (e == NULL) |
| 1076 | goto fail; |
Guido van Rossum | b9d338c | 1997-01-03 15:40:33 +0000 | [diff] [blame] | 1077 | nres = restart + align((int)(res-restart), c, e); |
| 1078 | /* Fill padd bytes with zeros */ |
| 1079 | while (res < nres) |
| 1080 | *res++ = '\0'; |
Guido van Rossum | 3aa27fd | 1996-12-31 02:10:45 +0000 | [diff] [blame] | 1081 | if (num == 0 && c != 's') |
| 1082 | continue; |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1083 | do { |
| 1084 | if (c == 'x') { |
| 1085 | /* doesn't consume arguments */ |
Guido van Rossum | 3aa27fd | 1996-12-31 02:10:45 +0000 | [diff] [blame] | 1086 | memset(res, '\0', num); |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1087 | res += num; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1088 | break; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1089 | } |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1090 | if (i >= n) { |
| 1091 | PyErr_SetString(StructError, |
| 1092 | "insufficient arguments to pack"); |
| 1093 | goto fail; |
| 1094 | } |
| 1095 | v = PyTuple_GetItem(args, i++); |
| 1096 | if (v == NULL) |
| 1097 | goto fail; |
| 1098 | if (c == 's') { |
| 1099 | /* num is string size, not repeat count */ |
Martin v. Löwis | ad0a462 | 2006-02-16 14:30:23 +0000 | [diff] [blame] | 1100 | Py_ssize_t n; |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1101 | if (!PyString_Check(v)) { |
| 1102 | PyErr_SetString(StructError, |
| 1103 | "argument for 's' must be a string"); |
| 1104 | goto fail; |
| 1105 | } |
| 1106 | n = PyString_Size(v); |
| 1107 | if (n > num) |
| 1108 | n = num; |
| 1109 | if (n > 0) |
| 1110 | memcpy(res, PyString_AsString(v), n); |
| 1111 | if (n < num) |
Guido van Rossum | 3aa27fd | 1996-12-31 02:10:45 +0000 | [diff] [blame] | 1112 | memset(res+n, '\0', num-n); |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1113 | res += num; |
| 1114 | break; |
| 1115 | } |
Guido van Rossum | 9eb671f | 1997-09-05 07:08:39 +0000 | [diff] [blame] | 1116 | else if (c == 'p') { |
| 1117 | /* num is string size + 1, |
| 1118 | to fit in the count byte */ |
Martin v. Löwis | ad0a462 | 2006-02-16 14:30:23 +0000 | [diff] [blame] | 1119 | Py_ssize_t n; |
Guido van Rossum | 9eb671f | 1997-09-05 07:08:39 +0000 | [diff] [blame] | 1120 | num--; /* now num is max string size */ |
| 1121 | if (!PyString_Check(v)) { |
| 1122 | PyErr_SetString(StructError, |
| 1123 | "argument for 'p' must be a string"); |
| 1124 | goto fail; |
| 1125 | } |
| 1126 | n = PyString_Size(v); |
| 1127 | if (n > num) |
| 1128 | n = num; |
| 1129 | if (n > 0) |
| 1130 | memcpy(res+1, PyString_AsString(v), n); |
| 1131 | if (n < num) |
| 1132 | /* no real need, just to be nice */ |
| 1133 | memset(res+1+n, '\0', num-n); |
Tim Peters | 0891ac0 | 2001-09-15 02:35:15 +0000 | [diff] [blame] | 1134 | if (n > 255) |
| 1135 | n = 255; |
Martin v. Löwis | ad0a462 | 2006-02-16 14:30:23 +0000 | [diff] [blame] | 1136 | /* store the length byte */ |
Tim Peters | 2ad8c56 | 2006-02-16 20:19:46 +0000 | [diff] [blame] | 1137 | *res++ = Py_SAFE_DOWNCAST(n, Py_ssize_t, unsigned char); |
Guido van Rossum | 9eb671f | 1997-09-05 07:08:39 +0000 | [diff] [blame] | 1138 | res += num; |
| 1139 | break; |
| 1140 | } |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1141 | else { |
| 1142 | if (e->pack(res, v, e) < 0) |
| 1143 | goto fail; |
| 1144 | res += e->size; |
| 1145 | } |
| 1146 | } while (--num > 0); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1147 | } |
| 1148 | |
| 1149 | if (i < n) { |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 1150 | PyErr_SetString(StructError, |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1151 | "too many arguments for pack format"); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1152 | goto fail; |
| 1153 | } |
| 1154 | |
| 1155 | return result; |
| 1156 | |
| 1157 | fail: |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 1158 | Py_DECREF(result); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1159 | return NULL; |
| 1160 | } |
| 1161 | |
| 1162 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1163 | PyDoc_STRVAR(unpack__doc__, |
| 1164 | "unpack(fmt, string) -> (v1, v2, ...)\n\ |
Guido van Rossum | 9897f0f | 1997-12-21 06:46:20 +0000 | [diff] [blame] | 1165 | Unpack the string, containing packed C structure data, according\n\ |
| 1166 | to fmt. Requires len(string)==calcsize(fmt).\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1167 | See struct.__doc__ for more on format strings."); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1168 | |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 1169 | static PyObject * |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 1170 | struct_unpack(PyObject *self, PyObject *args) |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1171 | { |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1172 | const formatdef *f, *e; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1173 | char *str, *start, *fmt, *s; |
| 1174 | char c; |
Barry Warsaw | b9a781e | 1997-01-03 00:26:28 +0000 | [diff] [blame] | 1175 | int len, size, num; |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 1176 | PyObject *res, *v; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1177 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1178 | if (!PyArg_ParseTuple(args, "ss#:unpack", &fmt, &start, &len)) |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1179 | return NULL; |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1180 | f = whichtable(&fmt); |
| 1181 | size = calcsize(fmt, f); |
| 1182 | if (size < 0) |
| 1183 | return NULL; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1184 | if (size != len) { |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 1185 | PyErr_SetString(StructError, |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1186 | "unpack str size does not match format"); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1187 | return NULL; |
| 1188 | } |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 1189 | res = PyList_New(0); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1190 | if (res == NULL) |
| 1191 | return NULL; |
| 1192 | str = start; |
| 1193 | s = fmt; |
| 1194 | while ((c = *s++) != '\0') { |
Raymond Hettinger | b3f55f4 | 2005-08-26 08:39:56 +0000 | [diff] [blame] | 1195 | if (isspace(Py_CHARMASK(c))) |
Guido van Rossum | e20aef5 | 1997-08-26 20:39:54 +0000 | [diff] [blame] | 1196 | continue; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1197 | if ('0' <= c && c <= '9') { |
| 1198 | num = c - '0'; |
| 1199 | while ('0' <= (c = *s++) && c <= '9') |
| 1200 | num = num*10 + (c - '0'); |
| 1201 | if (c == '\0') |
| 1202 | break; |
| 1203 | } |
| 1204 | else |
| 1205 | num = 1; |
| 1206 | |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1207 | e = getentry(c, f); |
| 1208 | if (e == NULL) |
| 1209 | goto fail; |
| 1210 | str = start + align((int)(str-start), c, e); |
Guido van Rossum | 3aa27fd | 1996-12-31 02:10:45 +0000 | [diff] [blame] | 1211 | if (num == 0 && c != 's') |
| 1212 | continue; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1213 | |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1214 | do { |
| 1215 | if (c == 'x') { |
| 1216 | str += num; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1217 | break; |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1218 | } |
| 1219 | if (c == 's') { |
| 1220 | /* num is string size, not repeat count */ |
| 1221 | v = PyString_FromStringAndSize(str, num); |
| 1222 | if (v == NULL) |
| 1223 | goto fail; |
| 1224 | str += num; |
| 1225 | num = 0; |
| 1226 | } |
Guido van Rossum | 9eb671f | 1997-09-05 07:08:39 +0000 | [diff] [blame] | 1227 | else if (c == 'p') { |
| 1228 | /* num is string buffer size, |
| 1229 | not repeat count */ |
| 1230 | int n = *(unsigned char*)str; |
| 1231 | /* first byte (unsigned) is string size */ |
| 1232 | if (n >= num) |
| 1233 | n = num-1; |
| 1234 | v = PyString_FromStringAndSize(str+1, n); |
| 1235 | if (v == NULL) |
| 1236 | goto fail; |
| 1237 | str += num; |
| 1238 | num = 0; |
| 1239 | } |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1240 | else { |
| 1241 | v = e->unpack(str, e); |
| 1242 | if (v == NULL) |
| 1243 | goto fail; |
| 1244 | str += e->size; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1245 | } |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 1246 | if (v == NULL || PyList_Append(res, v) < 0) |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1247 | goto fail; |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 1248 | Py_DECREF(v); |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1249 | } while (--num > 0); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1250 | } |
| 1251 | |
Guido van Rossum | f7e6b4b | 1996-12-31 01:41:25 +0000 | [diff] [blame] | 1252 | v = PyList_AsTuple(res); |
| 1253 | Py_DECREF(res); |
| 1254 | return v; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1255 | |
| 1256 | fail: |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 1257 | Py_DECREF(res); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1258 | return NULL; |
| 1259 | } |
| 1260 | |
Guido van Rossum | 90ddb7b | 1992-08-19 16:44:15 +0000 | [diff] [blame] | 1261 | |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1262 | /* List of functions */ |
| 1263 | |
Barry Warsaw | 30695fa | 1996-12-12 23:32:31 +0000 | [diff] [blame] | 1264 | static PyMethodDef struct_methods[] = { |
Guido van Rossum | 414fd48 | 1997-12-19 04:24:24 +0000 | [diff] [blame] | 1265 | {"calcsize", struct_calcsize, METH_VARARGS, calcsize__doc__}, |
| 1266 | {"pack", struct_pack, METH_VARARGS, pack__doc__}, |
| 1267 | {"unpack", struct_unpack, METH_VARARGS, unpack__doc__}, |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1268 | {NULL, NULL} /* sentinel */ |
| 1269 | }; |
| 1270 | |
| 1271 | |
| 1272 | /* Module initialization */ |
| 1273 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 1274 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1275 | initstruct(void) |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1276 | { |
Fred Drake | 78f6c86 | 2002-02-14 07:11:23 +0000 | [diff] [blame] | 1277 | PyObject *m; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1278 | |
| 1279 | /* Create the module and add the functions */ |
Guido van Rossum | 414fd48 | 1997-12-19 04:24:24 +0000 | [diff] [blame] | 1280 | m = Py_InitModule4("struct", struct_methods, struct__doc__, |
| 1281 | (PyObject*)NULL, PYTHON_API_VERSION); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 1282 | if (m == NULL) |
| 1283 | return; |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1284 | |
| 1285 | /* Add some symbolic constants to the module */ |
Fred Drake | 78f6c86 | 2002-02-14 07:11:23 +0000 | [diff] [blame] | 1286 | if (StructError == NULL) { |
| 1287 | StructError = PyErr_NewException("struct.error", NULL, NULL); |
| 1288 | if (StructError == NULL) |
| 1289 | return; |
| 1290 | } |
| 1291 | Py_INCREF(StructError); |
Fred Drake | 2eeec9b | 2002-02-14 07:16:30 +0000 | [diff] [blame] | 1292 | PyModule_AddObject(m, "error", StructError); |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1293 | } |