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