annotate Modules/structmodule.c @ 13502:7348cceee8fa legacy-trunk

ANSI-fication
author Peter Schneider-Kamp <nowonder@nowonder.de>
date Mon, 10 Jul 2000 12:29:26 +0000
parents a89f7f268de4
children 8a063423e0b7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 /***********************************************************
13255
916fb024ce94 Change copyright notice.
Guido van Rossum <guido@python.org>
parents: 12814
diff changeset
2 Copyright (c) 2000, BeOpen.com.
916fb024ce94 Change copyright notice.
Guido van Rossum <guido@python.org>
parents: 12814
diff changeset
3 Copyright (c) 1995-2000, Corporation for National Research Initiatives.
916fb024ce94 Change copyright notice.
Guido van Rossum <guido@python.org>
parents: 12814
diff changeset
4 Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
916fb024ce94 Change copyright notice.
Guido van Rossum <guido@python.org>
parents: 12814
diff changeset
5 All rights reserved.
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
6
13255
916fb024ce94 Change copyright notice.
Guido van Rossum <guido@python.org>
parents: 12814
diff changeset
7 See the file "Misc/COPYRIGHT" for information on usage and
916fb024ce94 Change copyright notice.
Guido van Rossum <guido@python.org>
parents: 12814
diff changeset
8 redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 ******************************************************************/
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
10
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 /* struct module -- pack values into and (out of) strings */
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
12
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
13 /* New version supporting byte order, alignment and size options,
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
14 character strings, and unsigned numbers */
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
15
6922
7629c4d985bc Added doc strings, suggested by Charles G. Waldman (but massaged quite a bit).
Guido van Rossum <guido@python.org>
parents: 6551
diff changeset
16 static char struct__doc__[] = "\
7629c4d985bc Added doc strings, suggested by Charles G. Waldman (but massaged quite a bit).
Guido van Rossum <guido@python.org>
parents: 6551
diff changeset
17 Functions to convert between Python values and C structs.\n\
7629c4d985bc Added doc strings, suggested by Charles G. Waldman (but massaged quite a bit).
Guido van Rossum <guido@python.org>
parents: 6551
diff changeset
18 Python strings are used to hold the data representing the C struct\n\
7629c4d985bc Added doc strings, suggested by Charles G. Waldman (but massaged quite a bit).
Guido van Rossum <guido@python.org>
parents: 6551
diff changeset
19 and also as format strings to describe the layout of data in the C struct.\n\
7629c4d985bc Added doc strings, suggested by Charles G. Waldman (but massaged quite a bit).
Guido van Rossum <guido@python.org>
parents: 6551
diff changeset
20 \n\
7629c4d985bc Added doc strings, suggested by Charles G. Waldman (but massaged quite a bit).
Guido van Rossum <guido@python.org>
parents: 6551
diff changeset
21 The optional first format char indicates byte ordering and alignment:\n\
7629c4d985bc Added doc strings, suggested by Charles G. Waldman (but massaged quite a bit).
Guido van Rossum <guido@python.org>
parents: 6551
diff changeset
22 @: native w/native alignment(default)\n\
7629c4d985bc Added doc strings, suggested by Charles G. Waldman (but massaged quite a bit).
Guido van Rossum <guido@python.org>
parents: 6551
diff changeset
23 =: native w/standard alignment\n\
7629c4d985bc Added doc strings, suggested by Charles G. Waldman (but massaged quite a bit).
Guido van Rossum <guido@python.org>
parents: 6551
diff changeset
24 <: little-endian, std. alignment\n\
7629c4d985bc Added doc strings, suggested by Charles G. Waldman (but massaged quite a bit).
Guido van Rossum <guido@python.org>
parents: 6551
diff changeset
25 >: big-endian, std. alignment\n\
7629c4d985bc Added doc strings, suggested by Charles G. Waldman (but massaged quite a bit).
Guido van Rossum <guido@python.org>
parents: 6551
diff changeset
26 !: network, std (same as >)\n\
7629c4d985bc Added doc strings, suggested by Charles G. Waldman (but massaged quite a bit).
Guido van Rossum <guido@python.org>
parents: 6551
diff changeset
27 \n\
7629c4d985bc Added doc strings, suggested by Charles G. Waldman (but massaged quite a bit).
Guido van Rossum <guido@python.org>
parents: 6551
diff changeset
28 The remaining chars indicate types of args and must match exactly;\n\
7629c4d985bc Added doc strings, suggested by Charles G. Waldman (but massaged quite a bit).
Guido van Rossum <guido@python.org>
parents: 6551
diff changeset
29 these can be preceded by a decimal repeat count:\n\
7629c4d985bc Added doc strings, suggested by Charles G. Waldman (but massaged quite a bit).
Guido van Rossum <guido@python.org>
parents: 6551
diff changeset
30 x: pad byte (no data); c:char; b:signed byte; B:unsigned byte;\n\
7629c4d985bc Added doc strings, suggested by Charles G. Waldman (but massaged quite a bit).
Guido van Rossum <guido@python.org>
parents: 6551
diff changeset
31 h:short; H:unsigned short; i:int; I:unsigned int;\n\
7629c4d985bc Added doc strings, suggested by Charles G. Waldman (but massaged quite a bit).
Guido van Rossum <guido@python.org>
parents: 6551
diff changeset
32 l:long; L:unsigned long; f:float; d:double.\n\
7629c4d985bc Added doc strings, suggested by Charles G. Waldman (but massaged quite a bit).
Guido van Rossum <guido@python.org>
parents: 6551
diff changeset
33 Special cases (preceding decimal count indicates length):\n\
7629c4d985bc Added doc strings, suggested by Charles G. Waldman (but massaged quite a bit).
Guido van Rossum <guido@python.org>
parents: 6551
diff changeset
34 s:string (array of char); p: pascal string (w. count byte).\n\
9123
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 8580
diff changeset
35 Special case (only available in native format):\n\
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 8580
diff changeset
36 P:an integer type that is wide enough to hold a pointer.\n\
6922
7629c4d985bc Added doc strings, suggested by Charles G. Waldman (but massaged quite a bit).
Guido van Rossum <guido@python.org>
parents: 6551
diff changeset
37 Whitespace between formats is ignored.\n\
7629c4d985bc Added doc strings, suggested by Charles G. Waldman (but massaged quite a bit).
Guido van Rossum <guido@python.org>
parents: 6551
diff changeset
38 \n\
7629c4d985bc Added doc strings, suggested by Charles G. Waldman (but massaged quite a bit).
Guido van Rossum <guido@python.org>
parents: 6551
diff changeset
39 The variable struct.error is an exception raised on errors.";
7629c4d985bc Added doc strings, suggested by Charles G. Waldman (but massaged quite a bit).
Guido van Rossum <guido@python.org>
parents: 6551
diff changeset
40
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
41 #include "Python.h"
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
42 #include "mymath.h"
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
43
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
44 #include <limits.h>
6167
9e432468f756 Ignore whitespace between formats (not internal to a count+format).
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
45 #include <ctype.h>
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
46
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
47
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
48 /* Exception */
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
49
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
50 static PyObject *StructError;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
51
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
52
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 /* Define various structs to figure out the alignments of types */
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
54
2278
65644766158a Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents: 2061
diff changeset
55 #ifdef __MWERKS__
65644766158a Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents: 2061
diff changeset
56 /*
65644766158a Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents: 2061
diff changeset
57 ** XXXX We have a problem here. There are no unique alignment rules
65644766158a Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents: 2061
diff changeset
58 ** on the PowerPC mac.
65644766158a Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents: 2061
diff changeset
59 */
65644766158a Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents: 2061
diff changeset
60 #ifdef __powerc
65644766158a Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents: 2061
diff changeset
61 #pragma options align=mac68k
65644766158a Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents: 2061
diff changeset
62 #endif
65644766158a Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents: 2061
diff changeset
63 #endif /* __MWERKS__ */
65644766158a Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents: 2061
diff changeset
64
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 typedef struct { char c; short x; } s_short;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 typedef struct { char c; int x; } s_int;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 typedef struct { char c; long x; } s_long;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 typedef struct { char c; float x; } s_float;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 typedef struct { char c; double x; } s_double;
9123
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 8580
diff changeset
70 typedef struct { char c; void *x; } s_void_p;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
71
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 #define SHORT_ALIGN (sizeof(s_short) - sizeof(short))
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 #define INT_ALIGN (sizeof(s_int) - sizeof(int))
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 #define LONG_ALIGN (sizeof(s_long) - sizeof(long))
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 #define FLOAT_ALIGN (sizeof(s_float) - sizeof(float))
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 #define DOUBLE_ALIGN (sizeof(s_double) - sizeof(double))
9123
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 8580
diff changeset
77 #define VOID_P_ALIGN (sizeof(s_void_p) - sizeof(void *))
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
78
2278
65644766158a Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents: 2061
diff changeset
79 #ifdef __powerc
65644766158a Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents: 2061
diff changeset
80 #pragma options align=reset
65644766158a Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents: 2061
diff changeset
81 #endif
65644766158a Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents: 2061
diff changeset
82
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
83 /* Helper routine to get a Python integer and raise the appropriate error
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
84 if it isn't one */
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
85
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
86 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
87 get_long(PyObject *v, long *p)
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
88 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
89 long x = PyInt_AsLong(v);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
90 if (x == -1 && PyErr_Occurred()) {
8434
ae746eab2a96 get_long(): Use PyErr_ExceptionMatches(...) instead of PyErr_Occurred(...).
Fred Drake <fdrake@acm.org>
parents: 7982
diff changeset
91 if (PyErr_ExceptionMatches(PyExc_TypeError))
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
92 PyErr_SetString(StructError,
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
93 "required argument is not an integer");
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
94 return -1;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
95 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
96 *p = x;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
97 return 0;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
98 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
99
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
100
4846
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
101 /* Same, but handling unsigned long */
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
102
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
103 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
104 get_ulong(PyObject *v, unsigned long *p)
4846
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
105 {
4874
03bcdd22f6a4 Changed the ``add/sub_offset'' hacks for dealing with C's unsigned
Guido van Rossum <guido@python.org>
parents: 4867
diff changeset
106 if (PyLong_Check(v)) {
03bcdd22f6a4 Changed the ``add/sub_offset'' hacks for dealing with C's unsigned
Guido van Rossum <guido@python.org>
parents: 4867
diff changeset
107 unsigned long x = PyLong_AsUnsignedLong(v);
03bcdd22f6a4 Changed the ``add/sub_offset'' hacks for dealing with C's unsigned
Guido van Rossum <guido@python.org>
parents: 4867
diff changeset
108 if (x == (unsigned long)(-1) && PyErr_Occurred())
4846
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
109 return -1;
4874
03bcdd22f6a4 Changed the ``add/sub_offset'' hacks for dealing with C's unsigned
Guido van Rossum <guido@python.org>
parents: 4867
diff changeset
110 *p = x;
03bcdd22f6a4 Changed the ``add/sub_offset'' hacks for dealing with C's unsigned
Guido van Rossum <guido@python.org>
parents: 4867
diff changeset
111 return 0;
4846
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
112 }
4874
03bcdd22f6a4 Changed the ``add/sub_offset'' hacks for dealing with C's unsigned
Guido van Rossum <guido@python.org>
parents: 4867
diff changeset
113 else {
03bcdd22f6a4 Changed the ``add/sub_offset'' hacks for dealing with C's unsigned
Guido van Rossum <guido@python.org>
parents: 4867
diff changeset
114 return get_long(v, (long *)p);
03bcdd22f6a4 Changed the ``add/sub_offset'' hacks for dealing with C's unsigned
Guido van Rossum <guido@python.org>
parents: 4867
diff changeset
115 }
4846
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
116 }
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
117
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
118
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
119 /* Floating point helpers */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
120
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
121 /* These use ANSI/IEEE Standard 754-1985 (Standard for Binary Floating
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
122 Point Arithmetic). See the following URL:
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
123 http://www.psc.edu/general/software/packages/ieee/ieee.html */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
124
4861
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
125 /* XXX Inf/NaN are not handled quite right (but underflow is!) */
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
126
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
127 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
128 pack_float(double x, /* The number to pack */
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
129 char *p, /* Where to pack the high order byte */
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
130 int incr) /* 1 for big-endian; -1 for little-endian */
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
131 {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
132 int s;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
133 int e;
4861
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
134 double f;
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
135 long fbits;
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
136
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
137 if (x < 0) {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
138 s = 1;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
139 x = -x;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
140 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
141 else
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
142 s = 0;
4861
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
143
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
144 f = frexp(x, &e);
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
145
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
146 /* Normalize f to be in the range [1.0, 2.0) */
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
147 if (0.5 <= f && f < 1.0) {
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
148 f *= 2.0;
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
149 e--;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
150 }
4861
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
151 else if (f == 0.0) {
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
152 e = 0;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
153 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
154 else {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
155 PyErr_SetString(PyExc_SystemError,
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
156 "frexp() result out of range");
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
157 return -1;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
158 }
4861
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
159
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
160 if (e >= 128) {
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
161 /* XXX 128 itself is reserved for Inf/NaN */
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
162 PyErr_SetString(PyExc_OverflowError,
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
163 "float too large to pack with f format");
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
164 return -1;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
165 }
4861
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
166 else if (e < -126) {
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
167 /* Gradual underflow */
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
168 f = ldexp(f, 126 + e);
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
169 e = 0;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
170 }
6551
bdd4cb2a83b5 Fix due to Bill Noon for problem discovered by Ken Manheimer: packing
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
171 else if (!(e == 0 && f == 0.0)) {
4861
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
172 e += 127;
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
173 f -= 1.0; /* Get rid of leading 1 */
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
174 }
4861
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
175
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
176 f *= 8388608.0; /* 2**23 */
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
177 fbits = (long) floor(f + 0.5); /* Round */
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
178
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
179 /* First byte */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
180 *p = (s<<7) | (e>>1);
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
181 p += incr;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
182
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
183 /* Second byte */
5318
2c4b802a3b5d Keep Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents: 4874
diff changeset
184 *p = (char) (((e&1)<<7) | (fbits>>16));
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
185 p += incr;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
186
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
187 /* Third byte */
4861
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
188 *p = (fbits>>8) & 0xFF;
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
189 p += incr;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
190
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
191 /* Fourth byte */
4861
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
192 *p = fbits&0xFF;
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
193
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
194 /* Done */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
195 return 0;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
196 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
197
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
198 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
199 pack_double(double x, /* The number to pack */
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
200 char *p, /* Where to pack the high order byte */
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
201 int incr) /* 1 for big-endian; -1 for little-endian */
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
202 {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
203 int s;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
204 int e;
4861
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
205 double f;
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
206 long fhi, flo;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
207
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
208 if (x < 0) {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
209 s = 1;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
210 x = -x;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
211 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
212 else
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
213 s = 0;
4861
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
214
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
215 f = frexp(x, &e);
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
216
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
217 /* Normalize f to be in the range [1.0, 2.0) */
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
218 if (0.5 <= f && f < 1.0) {
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
219 f *= 2.0;
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
220 e--;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
221 }
4861
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
222 else if (f == 0.0) {
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
223 e = 0;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
224 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
225 else {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
226 PyErr_SetString(PyExc_SystemError,
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
227 "frexp() result out of range");
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
228 return -1;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
229 }
4861
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
230
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
231 if (e >= 1024) {
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
232 /* XXX 1024 itself is reserved for Inf/NaN */
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
233 PyErr_SetString(PyExc_OverflowError,
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
234 "float too large to pack with d format");
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
235 return -1;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
236 }
4861
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
237 else if (e < -1022) {
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
238 /* Gradual underflow */
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
239 f = ldexp(f, 1022 + e);
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
240 e = 0;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
241 }
6551
bdd4cb2a83b5 Fix due to Bill Noon for problem discovered by Ken Manheimer: packing
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
242 else if (!(e == 0 && f == 0.0)) {
4861
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
243 e += 1023;
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
244 f -= 1.0; /* Get rid of leading 1 */
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
245 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
246
4861
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
247 /* fhi receives the high 28 bits; flo the low 24 bits (== 52 bits) */
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
248 f *= 268435456.0; /* 2**28 */
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
249 fhi = (long) floor(f); /* Truncate */
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
250 f -= (double)fhi;
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
251 f *= 16777216.0; /* 2**24 */
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
252 flo = (long) floor(f + 0.5); /* Round */
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
253
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
254 /* First byte */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
255 *p = (s<<7) | (e>>4);
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
256 p += incr;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
257
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
258 /* Second byte */
5318
2c4b802a3b5d Keep Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents: 4874
diff changeset
259 *p = (char) (((e&0xF)<<4) | (fhi>>24));
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
260 p += incr;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
261
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
262 /* Third byte */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
263 *p = (fhi>>16) & 0xFF;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
264 p += incr;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
265
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
266 /* Fourth byte */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
267 *p = (fhi>>8) & 0xFF;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
268 p += incr;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
269
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
270 /* Fifth byte */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
271 *p = fhi & 0xFF;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
272 p += incr;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
273
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
274 /* Sixth byte */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
275 *p = (flo>>16) & 0xFF;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
276 p += incr;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
277
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
278 /* Seventh byte */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
279 *p = (flo>>8) & 0xFF;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
280 p += incr;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
281
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
282 /* Eighth byte */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
283 *p = flo & 0xFF;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
284 p += incr;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
285
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
286 /* Done */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
287 return 0;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
288 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
289
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
290 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
291 unpack_float(const char *p, /* Where the high order byte is */
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
292 int incr) /* 1 for big-endian; -1 for little-endian */
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
293 {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
294 int s;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
295 int e;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
296 long f;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
297 double x;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
298
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
299 /* First byte */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
300 s = (*p>>7) & 1;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
301 e = (*p & 0x7F) << 1;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
302 p += incr;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
303
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
304 /* Second byte */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
305 e |= (*p>>7) & 1;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
306 f = (*p & 0x7F) << 16;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
307 p += incr;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
308
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
309 /* Third byte */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
310 f |= (*p & 0xFF) << 8;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
311 p += incr;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
312
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
313 /* Fourth byte */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
314 f |= *p & 0xFF;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
315
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
316 x = (double)f / 8388608.0;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
317
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
318 /* XXX This sadly ignores Inf/NaN issues */
4859
ab461c762559 Oops -- unpack float/double didn't do the right thing if e==0.
Guido van Rossum <guido@python.org>
parents: 4858
diff changeset
319 if (e == 0)
ab461c762559 Oops -- unpack float/double didn't do the right thing if e==0.
Guido van Rossum <guido@python.org>
parents: 4858
diff changeset
320 e = -126;
ab461c762559 Oops -- unpack float/double didn't do the right thing if e==0.
Guido van Rossum <guido@python.org>
parents: 4858
diff changeset
321 else {
ab461c762559 Oops -- unpack float/double didn't do the right thing if e==0.
Guido van Rossum <guido@python.org>
parents: 4858
diff changeset
322 x += 1.0;
ab461c762559 Oops -- unpack float/double didn't do the right thing if e==0.
Guido van Rossum <guido@python.org>
parents: 4858
diff changeset
323 e -= 127;
ab461c762559 Oops -- unpack float/double didn't do the right thing if e==0.
Guido van Rossum <guido@python.org>
parents: 4858
diff changeset
324 }
ab461c762559 Oops -- unpack float/double didn't do the right thing if e==0.
Guido van Rossum <guido@python.org>
parents: 4858
diff changeset
325 x = ldexp(x, e);
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
326
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
327 if (s)
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
328 x = -x;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
329
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
330 return PyFloat_FromDouble(x);
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
331 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
332
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
333 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
334 unpack_double(const char *p, /* Where the high order byte is */
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
335 int incr) /* 1 for big-endian; -1 for little-endian */
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
336 {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
337 int s;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
338 int e;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
339 long fhi, flo;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
340 double x;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
341
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
342 /* First byte */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
343 s = (*p>>7) & 1;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
344 e = (*p & 0x7F) << 4;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
345 p += incr;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
346
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
347 /* Second byte */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
348 e |= (*p>>4) & 0xF;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
349 fhi = (*p & 0xF) << 24;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
350 p += incr;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
351
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
352 /* Third byte */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
353 fhi |= (*p & 0xFF) << 16;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
354 p += incr;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
355
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
356 /* Fourth byte */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
357 fhi |= (*p & 0xFF) << 8;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
358 p += incr;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
359
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
360 /* Fifth byte */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
361 fhi |= *p & 0xFF;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
362 p += incr;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
363
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
364 /* Sixth byte */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
365 flo = (*p & 0xFF) << 16;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
366 p += incr;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
367
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
368 /* Seventh byte */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
369 flo |= (*p & 0xFF) << 8;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
370 p += incr;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
371
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
372 /* Eighth byte */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
373 flo |= *p & 0xFF;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
374 p += incr;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
375
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
376 x = (double)fhi + (double)flo / 16777216.0; /* 2**24 */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
377 x /= 268435456.0; /* 2**28 */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
378
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
379 /* XXX This sadly ignores Inf/NaN */
4859
ab461c762559 Oops -- unpack float/double didn't do the right thing if e==0.
Guido van Rossum <guido@python.org>
parents: 4858
diff changeset
380 if (e == 0)
ab461c762559 Oops -- unpack float/double didn't do the right thing if e==0.
Guido van Rossum <guido@python.org>
parents: 4858
diff changeset
381 e = -1022;
ab461c762559 Oops -- unpack float/double didn't do the right thing if e==0.
Guido van Rossum <guido@python.org>
parents: 4858
diff changeset
382 else {
ab461c762559 Oops -- unpack float/double didn't do the right thing if e==0.
Guido van Rossum <guido@python.org>
parents: 4858
diff changeset
383 x += 1.0;
ab461c762559 Oops -- unpack float/double didn't do the right thing if e==0.
Guido van Rossum <guido@python.org>
parents: 4858
diff changeset
384 e -= 1023;
ab461c762559 Oops -- unpack float/double didn't do the right thing if e==0.
Guido van Rossum <guido@python.org>
parents: 4858
diff changeset
385 }
ab461c762559 Oops -- unpack float/double didn't do the right thing if e==0.
Guido van Rossum <guido@python.org>
parents: 4858
diff changeset
386 x = ldexp(x, e);
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
387
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
388 if (s)
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
389 x = -x;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
390
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
391 return PyFloat_FromDouble(x);
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
392 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
393
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
394
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
395 /* The translation function for each format character is table driven */
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
396
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
397 typedef struct _formatdef {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
398 char format;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
399 int size;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
400 int alignment;
13447
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
401 PyObject* (*unpack)(const char *,
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
402 const struct _formatdef *);
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
403 int (*pack)(char *, PyObject *,
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
404 const struct _formatdef *);
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
405 } formatdef;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
406
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
407 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
408 nu_char(const char *p, const formatdef *f)
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
409 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
410 return PyString_FromStringAndSize(p, 1);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
411 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
412
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
413 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
414 nu_byte(const char *p, const formatdef *f)
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
415 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
416 return PyInt_FromLong((long) *(signed char *)p);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
417 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
418
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
419 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
420 nu_ubyte(const char *p, const formatdef *f)
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
421 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
422 return PyInt_FromLong((long) *(unsigned char *)p);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
423 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
424
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
425 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
426 nu_short(const char *p, const formatdef *f)
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
427 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
428 return PyInt_FromLong((long) *(short *)p);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
429 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
430
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
431 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
432 nu_ushort(const char *p, const formatdef *f)
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
433 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
434 return PyInt_FromLong((long) *(unsigned short *)p);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
435 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
436
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
437 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
438 nu_int(const char *p, const formatdef *f)
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
439 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
440 return PyInt_FromLong((long) *(int *)p);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
441 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
442
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
443 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
444 nu_uint(const char *p, const formatdef *f)
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
445 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
446 unsigned int x = *(unsigned int *)p;
4874
03bcdd22f6a4 Changed the ``add/sub_offset'' hacks for dealing with C's unsigned
Guido van Rossum <guido@python.org>
parents: 4867
diff changeset
447 return PyLong_FromUnsignedLong((unsigned long)x);
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
448 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
449
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
450 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
451 nu_long(const char *p, const formatdef *f)
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
452 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
453 return PyInt_FromLong(*(long *)p);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
454 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
455
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
456 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
457 nu_ulong(const char *p, const formatdef *f)
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
458 {
4874
03bcdd22f6a4 Changed the ``add/sub_offset'' hacks for dealing with C's unsigned
Guido van Rossum <guido@python.org>
parents: 4867
diff changeset
459 return PyLong_FromUnsignedLong(*(unsigned long *)p);
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
460 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
461
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
462 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
463 nu_float(const char *p, const formatdef *f)
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
464 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
465 float x;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
466 memcpy((char *)&x, p, sizeof(float));
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
467 return PyFloat_FromDouble((double)x);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
468 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
469
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
470 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
471 nu_double(const char *p, const formatdef *f)
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
472 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
473 double x;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
474 memcpy((char *)&x, p, sizeof(double));
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
475 return PyFloat_FromDouble(x);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
476 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
477
9123
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 8580
diff changeset
478 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
479 nu_void_p(const char *p, const formatdef *f)
9123
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 8580
diff changeset
480 {
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 8580
diff changeset
481 return PyLong_FromVoidPtr(*(void **)p);
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 8580
diff changeset
482 }
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 8580
diff changeset
483
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
484 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
485 np_byte(char *p, PyObject *v, const formatdef *f)
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
486 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
487 long x;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
488 if (get_long(v, &x) < 0)
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
489 return -1;
5318
2c4b802a3b5d Keep Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents: 4874
diff changeset
490 *p = (char)x;
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
491 return 0;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
492 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
493
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
494 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
495 np_char(char *p, PyObject *v, const formatdef *f)
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
496 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
497 if (!PyString_Check(v) || PyString_Size(v) != 1) {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
498 PyErr_SetString(StructError,
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
499 "char format require string of length 1");
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
500 return -1;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
501 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
502 *p = *PyString_AsString(v);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
503 return 0;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
504 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
505
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
506 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
507 np_short(char *p, PyObject *v, const formatdef *f)
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
508 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
509 long x;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
510 if (get_long(v, &x) < 0)
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
511 return -1;
5318
2c4b802a3b5d Keep Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents: 4874
diff changeset
512 * (short *)p = (short)x;
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
513 return 0;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
514 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
515
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
516 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
517 np_int(char *p, PyObject *v, const formatdef *f)
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
518 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
519 long x;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
520 if (get_long(v, &x) < 0)
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
521 return -1;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
522 * (int *)p = x;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
523 return 0;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
524 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
525
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
526 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
527 np_uint(char *p, PyObject *v, const formatdef *f)
4846
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
528 {
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
529 unsigned long x;
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
530 if (get_ulong(v, &x) < 0)
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
531 return -1;
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
532 * (unsigned int *)p = x;
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
533 return 0;
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
534 }
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
535
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
536 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
537 np_long(char *p, PyObject *v, const formatdef *f)
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
538 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
539 long x;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
540 if (get_long(v, &x) < 0)
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
541 return -1;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
542 * (long *)p = x;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
543 return 0;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
544 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
545
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
546 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
547 np_ulong(char *p, PyObject *v, const formatdef *f)
4846
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
548 {
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
549 unsigned long x;
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
550 if (get_ulong(v, &x) < 0)
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
551 return -1;
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
552 * (unsigned long *)p = x;
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
553 return 0;
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
554 }
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
555
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
556 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
557 np_float(char *p, PyObject *v, const formatdef *f)
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
558 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
559 float x = (float)PyFloat_AsDouble(v);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
560 if (x == -1 && PyErr_Occurred()) {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
561 PyErr_SetString(StructError,
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
562 "required argument is not a float");
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
563 return -1;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
564 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
565 memcpy(p, (char *)&x, sizeof(float));
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
566 return 0;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
567 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
568
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
569 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
570 np_double(char *p, PyObject *v, const formatdef *f)
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
571 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
572 double x = PyFloat_AsDouble(v);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
573 if (x == -1 && PyErr_Occurred()) {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
574 PyErr_SetString(StructError,
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
575 "required argument is not a float");
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
576 return -1;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
577 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
578 memcpy(p, (char *)&x, sizeof(double));
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
579 return 0;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
580 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
581
9123
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 8580
diff changeset
582 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
583 np_void_p(char *p, PyObject *v, const formatdef *f)
9123
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 8580
diff changeset
584 {
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 8580
diff changeset
585 void *x = PyLong_AsVoidPtr(v);
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 8580
diff changeset
586 if (x == NULL && PyErr_Occurred()) {
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 8580
diff changeset
587 /* ### hrm. PyLong_AsVoidPtr raises SystemError */
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 8580
diff changeset
588 if (PyErr_ExceptionMatches(PyExc_TypeError))
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 8580
diff changeset
589 PyErr_SetString(StructError,
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 8580
diff changeset
590 "required argument is not an integer");
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 8580
diff changeset
591 return -1;
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 8580
diff changeset
592 }
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 8580
diff changeset
593 *(void **)p = x;
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 8580
diff changeset
594 return 0;
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 8580
diff changeset
595 }
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 8580
diff changeset
596
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
597 static formatdef native_table[] = {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
598 {'x', sizeof(char), 0, NULL},
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
599 {'b', sizeof(char), 0, nu_byte, np_byte},
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
600 {'B', sizeof(char), 0, nu_ubyte, np_byte},
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
601 {'c', sizeof(char), 0, nu_char, np_char},
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
602 {'s', sizeof(char), 0, NULL},
6241
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
603 {'p', sizeof(char), 0, NULL},
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
604 {'h', sizeof(short), SHORT_ALIGN, nu_short, np_short},
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
605 {'H', sizeof(short), SHORT_ALIGN, nu_ushort, np_short},
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
606 {'i', sizeof(int), INT_ALIGN, nu_int, np_int},
4846
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
607 {'I', sizeof(int), INT_ALIGN, nu_uint, np_uint},
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
608 {'l', sizeof(long), LONG_ALIGN, nu_long, np_long},
4846
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
609 {'L', sizeof(long), LONG_ALIGN, nu_ulong, np_ulong},
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
610 {'f', sizeof(float), FLOAT_ALIGN, nu_float, np_float},
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
611 {'d', sizeof(double), DOUBLE_ALIGN, nu_double, np_double},
9123
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 8580
diff changeset
612 {'P', sizeof(void *), VOID_P_ALIGN, nu_void_p, np_void_p},
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
613 {0}
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
614 };
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
615
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
616 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
617 bu_int(const char *p, const formatdef *f)
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
618 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
619 long x = 0;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
620 int i = f->size;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
621 do {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
622 x = (x<<8) | (*p++ & 0xFF);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
623 } while (--i > 0);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
624 i = 8*(sizeof(long) - f->size);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
625 if (i) {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
626 x <<= i;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
627 x >>= i;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
628 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
629 return PyInt_FromLong(x);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
630 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
631
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
632 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
633 bu_uint(const char *p, const formatdef *f)
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
634 {
4874
03bcdd22f6a4 Changed the ``add/sub_offset'' hacks for dealing with C's unsigned
Guido van Rossum <guido@python.org>
parents: 4867
diff changeset
635 unsigned long x = 0;
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
636 int i = f->size;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
637 do {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
638 x = (x<<8) | (*p++ & 0xFF);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
639 } while (--i > 0);
8580
1ca6895024d9 Unsigned 1 and 2 byte sized formats shouldn't result in long integer values!
Guido van Rossum <guido@python.org>
parents: 8434
diff changeset
640 if (f->size >= 4)
1ca6895024d9 Unsigned 1 and 2 byte sized formats shouldn't result in long integer values!
Guido van Rossum <guido@python.org>
parents: 8434
diff changeset
641 return PyLong_FromUnsignedLong(x);
1ca6895024d9 Unsigned 1 and 2 byte sized formats shouldn't result in long integer values!
Guido van Rossum <guido@python.org>
parents: 8434
diff changeset
642 else
1ca6895024d9 Unsigned 1 and 2 byte sized formats shouldn't result in long integer values!
Guido van Rossum <guido@python.org>
parents: 8434
diff changeset
643 return PyInt_FromLong((long)x);
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
644 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
645
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
646 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
647 bu_float(const char *p, const formatdef *f)
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
648 {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
649 return unpack_float(p, 1);
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
650 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
651
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
652 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
653 bu_double(const char *p, const formatdef *f)
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
654 {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
655 return unpack_double(p, 1);
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
656 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
657
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
658 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
659 bp_int(char *p, PyObject *v, const formatdef *f)
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
660 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
661 long x;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
662 int i;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
663 if (get_long(v, &x) < 0)
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
664 return -1;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
665 i = f->size;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
666 do {
5318
2c4b802a3b5d Keep Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents: 4874
diff changeset
667 p[--i] = (char)x;
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
668 x >>= 8;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
669 } while (i > 0);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
670 return 0;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
671 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
672
4846
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
673 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
674 bp_uint(char *p, PyObject *v, const formatdef *f)
4846
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
675 {
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
676 unsigned long x;
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
677 int i;
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
678 if (get_ulong(v, &x) < 0)
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
679 return -1;
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
680 i = f->size;
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
681 do {
5318
2c4b802a3b5d Keep Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents: 4874
diff changeset
682 p[--i] = (char)x;
4846
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
683 x >>= 8;
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
684 } while (i > 0);
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
685 return 0;
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
686 }
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
687
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
688 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
689 bp_float(char *p, PyObject *v, const formatdef *f)
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
690 {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
691 double x = PyFloat_AsDouble(v);
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
692 if (x == -1 && PyErr_Occurred()) {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
693 PyErr_SetString(StructError,
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
694 "required argument is not a float");
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
695 return -1;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
696 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
697 return pack_float(x, p, 1);
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
698 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
699
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
700 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
701 bp_double(char *p, PyObject *v, const formatdef *f)
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
702 {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
703 double x = PyFloat_AsDouble(v);
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
704 if (x == -1 && PyErr_Occurred()) {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
705 PyErr_SetString(StructError,
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
706 "required argument is not a float");
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
707 return -1;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
708 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
709 return pack_double(x, p, 1);
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
710 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
711
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
712 static formatdef bigendian_table[] = {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
713 {'x', 1, 0, NULL},
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
714 {'b', 1, 0, bu_int, bp_int},
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
715 {'B', 1, 0, bu_uint, bp_int},
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
716 {'c', 1, 0, nu_char, np_char},
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
717 {'s', 1, 0, NULL},
6241
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
718 {'p', 1, 0, NULL},
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
719 {'h', 2, 0, bu_int, bp_int},
4846
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
720 {'H', 2, 0, bu_uint, bp_uint},
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
721 {'i', 4, 0, bu_int, bp_int},
4846
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
722 {'I', 4, 0, bu_uint, bp_uint},
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
723 {'l', 4, 0, bu_int, bp_int},
4846
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
724 {'L', 4, 0, bu_uint, bp_uint},
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
725 {'f', 4, 0, bu_float, bp_float},
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
726 {'d', 8, 0, bu_double, bp_double},
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
727 {0}
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
728 };
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
729
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
730 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
731 lu_int(const char *p, const formatdef *f)
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
732 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
733 long x = 0;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
734 int i = f->size;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
735 do {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
736 x = (x<<8) | (p[--i] & 0xFF);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
737 } while (i > 0);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
738 i = 8*(sizeof(long) - f->size);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
739 if (i) {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
740 x <<= i;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
741 x >>= i;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
742 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
743 return PyInt_FromLong(x);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
744 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
745
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
746 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
747 lu_uint(const char *p, const formatdef *f)
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
748 {
4874
03bcdd22f6a4 Changed the ``add/sub_offset'' hacks for dealing with C's unsigned
Guido van Rossum <guido@python.org>
parents: 4867
diff changeset
749 unsigned long x = 0;
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
750 int i = f->size;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
751 do {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
752 x = (x<<8) | (p[--i] & 0xFF);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
753 } while (i > 0);
8580
1ca6895024d9 Unsigned 1 and 2 byte sized formats shouldn't result in long integer values!
Guido van Rossum <guido@python.org>
parents: 8434
diff changeset
754 if (f->size >= 4)
1ca6895024d9 Unsigned 1 and 2 byte sized formats shouldn't result in long integer values!
Guido van Rossum <guido@python.org>
parents: 8434
diff changeset
755 return PyLong_FromUnsignedLong(x);
1ca6895024d9 Unsigned 1 and 2 byte sized formats shouldn't result in long integer values!
Guido van Rossum <guido@python.org>
parents: 8434
diff changeset
756 else
1ca6895024d9 Unsigned 1 and 2 byte sized formats shouldn't result in long integer values!
Guido van Rossum <guido@python.org>
parents: 8434
diff changeset
757 return PyInt_FromLong((long)x);
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
758 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
759
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
760 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
761 lu_float(const char *p, const formatdef *f)
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
762 {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
763 return unpack_float(p+3, -1);
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
764 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
765
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
766 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
767 lu_double(const char *p, const formatdef *f)
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
768 {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
769 return unpack_double(p+7, -1);
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
770 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
771
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
772 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
773 lp_int(char *p, PyObject *v, const formatdef *f)
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
774 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
775 long x;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
776 int i;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
777 if (get_long(v, &x) < 0)
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
778 return -1;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
779 i = f->size;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
780 do {
5318
2c4b802a3b5d Keep Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents: 4874
diff changeset
781 *p++ = (char)x;
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
782 x >>= 8;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
783 } while (--i > 0);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
784 return 0;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
785 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
786
4846
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
787 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
788 lp_uint(char *p, PyObject *v, const formatdef *f)
4846
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
789 {
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
790 unsigned long x;
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
791 int i;
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
792 if (get_ulong(v, &x) < 0)
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
793 return -1;
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
794 i = f->size;
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
795 do {
5318
2c4b802a3b5d Keep Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents: 4874
diff changeset
796 *p++ = (char)x;
4846
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
797 x >>= 8;
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
798 } while (--i > 0);
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
799 return 0;
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
800 }
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
801
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
802 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
803 lp_float(char *p, PyObject *v, const formatdef *f)
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
804 {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
805 double x = PyFloat_AsDouble(v);
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
806 if (x == -1 && PyErr_Occurred()) {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
807 PyErr_SetString(StructError,
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
808 "required argument is not a float");
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
809 return -1;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
810 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
811 return pack_float(x, p+3, -1);
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
812 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
813
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
814 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
815 lp_double(char *p, PyObject *v, const formatdef *f)
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
816 {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
817 double x = PyFloat_AsDouble(v);
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
818 if (x == -1 && PyErr_Occurred()) {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
819 PyErr_SetString(StructError,
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
820 "required argument is not a float");
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
821 return -1;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
822 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
823 return pack_double(x, p+7, -1);
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
824 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
825
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
826 static formatdef lilendian_table[] = {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
827 {'x', 1, 0, NULL},
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
828 {'b', 1, 0, lu_int, lp_int},
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
829 {'B', 1, 0, lu_uint, lp_int},
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
830 {'c', 1, 0, nu_char, np_char},
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
831 {'s', 1, 0, NULL},
6241
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
832 {'p', 1, 0, NULL},
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
833 {'h', 2, 0, lu_int, lp_int},
4846
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
834 {'H', 2, 0, lu_uint, lp_uint},
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
835 {'i', 4, 0, lu_int, lp_int},
4846
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
836 {'I', 4, 0, lu_uint, lp_uint},
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
837 {'l', 4, 0, lu_int, lp_int},
4846
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
838 {'L', 4, 0, lu_uint, lp_uint},
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
839 {'f', 4, 0, lu_float, lp_float},
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
840 {'d', 8, 0, lu_double, lp_double},
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
841 {0}
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
842 };
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
843
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
844
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
845 static const formatdef *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
846 whichtable(char **pfmt)
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
847 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
848 const char *fmt = (*pfmt)++; /* May be backed out of later */
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
849 switch (*fmt) {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
850 case '<':
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
851 return lilendian_table;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
852 case '>':
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
853 case '!': /* Network byte order is big-endian */
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
854 return bigendian_table;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
855 case '=': { /* Host byte order -- different from native in aligment! */
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
856 int n = 1;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
857 char *p = (char *) &n;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
858 if (*p == 1)
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
859 return lilendian_table;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
860 else
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
861 return bigendian_table;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
862 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
863 default:
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
864 --*pfmt; /* Back out of pointer increment */
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
865 /* Fall through */
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
866 case '@':
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
867 return native_table;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
868 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
869 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
870
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
871
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
872 /* Get the table entry for a format code */
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
873
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
874 static const formatdef *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
875 getentry(int c, const formatdef *f)
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
876 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
877 for (; f->format != '\0'; f++) {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
878 if (f->format == c) {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
879 return f;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
880 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
881 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
882 PyErr_SetString(StructError, "bad char in struct format");
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
883 return NULL;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
884 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
885
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
886
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
887 /* Align a size according to a format code */
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
888
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
889 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
890 align(int size, int c, const formatdef *e)
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
891 {
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
892 if (e->format == c) {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
893 if (e->alignment) {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
894 size = ((size + e->alignment - 1)
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
895 / e->alignment)
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
896 * e->alignment;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
897 }
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
898 }
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
899 return size;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
900 }
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
901
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
902
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
903 /* calculate the size of a format string */
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
904
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
905 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
906 calcsize(const char *fmt, const formatdef *f)
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
907 {
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
908 const formatdef *e;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
909 const char *s;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
910 char c;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
911 int size, num, itemsize, x;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
912
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
913 s = fmt;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
914 size = 0;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
915 while ((c = *s++) != '\0') {
7982
a909a2869ac5 Make new gcc -Wall happy
Guido van Rossum <guido@python.org>
parents: 6927
diff changeset
916 if (isspace((int)c))
6167
9e432468f756 Ignore whitespace between formats (not internal to a count+format).
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
917 continue;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
918 if ('0' <= c && c <= '9') {
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
919 num = c - '0';
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
920 while ('0' <= (c = *s++) && c <= '9') {
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
921 x = num*10 + (c - '0');
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
922 if (x/10 != num) {
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
923 PyErr_SetString(
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
924 StructError,
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
925 "overflow in item count");
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
926 return -1;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
927 }
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
928 num = x;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
929 }
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
930 if (c == '\0')
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
931 break;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
932 }
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
933 else
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
934 num = 1;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
935
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
936 e = getentry(c, f);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
937 if (e == NULL)
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
938 return -1;
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
939 itemsize = e->size;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
940 size = align(size, c, e);
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
941 x = num * itemsize;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
942 size += x;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
943 if (x/itemsize != num || size < 0) {
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
944 PyErr_SetString(StructError,
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
945 "total struct size too long");
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
946 return -1;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
947 }
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
948 }
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
949
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
950 return size;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
951 }
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
952
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
953
6922
7629c4d985bc Added doc strings, suggested by Charles G. Waldman (but massaged quite a bit).
Guido van Rossum <guido@python.org>
parents: 6551
diff changeset
954 static char calcsize__doc__[] = "\
7629c4d985bc Added doc strings, suggested by Charles G. Waldman (but massaged quite a bit).
Guido van Rossum <guido@python.org>
parents: 6551
diff changeset
955 calcsize(fmt) -> int\n\
7629c4d985bc Added doc strings, suggested by Charles G. Waldman (but massaged quite a bit).
Guido van Rossum <guido@python.org>
parents: 6551
diff changeset
956 Return size of C struct described by format string fmt.\n\
7629c4d985bc Added doc strings, suggested by Charles G. Waldman (but massaged quite a bit).
Guido van Rossum <guido@python.org>
parents: 6551
diff changeset
957 See struct.__doc__ for more on format strings.";
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
958
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
959 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
960 struct_calcsize(PyObject *self, PyObject *args)
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
961 {
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
962 char *fmt;
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
963 const formatdef *f;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
964 int size;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
965
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
966 if (!PyArg_ParseTuple(args, "s:calcsize", &fmt))
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
967 return NULL;
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
968 f = whichtable(&fmt);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
969 size = calcsize(fmt, f);
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
970 if (size < 0)
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
971 return NULL;
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
972 return PyInt_FromLong((long)size);
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
973 }
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
974
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
975
6922
7629c4d985bc Added doc strings, suggested by Charles G. Waldman (but massaged quite a bit).
Guido van Rossum <guido@python.org>
parents: 6551
diff changeset
976 static char pack__doc__[] = "\
7629c4d985bc Added doc strings, suggested by Charles G. Waldman (but massaged quite a bit).
Guido van Rossum <guido@python.org>
parents: 6551
diff changeset
977 pack(fmt, v1, v2, ...) -> string\n\
7629c4d985bc Added doc strings, suggested by Charles G. Waldman (but massaged quite a bit).
Guido van Rossum <guido@python.org>
parents: 6551
diff changeset
978 Return string containing values v1, v2, ... packed according to fmt.\n\
7629c4d985bc Added doc strings, suggested by Charles G. Waldman (but massaged quite a bit).
Guido van Rossum <guido@python.org>
parents: 6551
diff changeset
979 See struct.__doc__ for more on format strings.";
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
980
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
981 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
982 struct_pack(PyObject *self, PyObject *args)
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
983 {
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
984 const formatdef *f, *e;
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
985 PyObject *format, *result, *v;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
986 char *fmt;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
987 int size, num;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
988 int i, n;
4867
e3f2ab0b0aa1 Fill pad bytes with zeros (fixing a bug dating from the very first version!).
Guido van Rossum <guido@python.org>
parents: 4865
diff changeset
989 char *s, *res, *restart, *nres;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
990 char c;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
991
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
992 if (args == NULL || !PyTuple_Check(args) ||
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
993 (n = PyTuple_Size(args)) < 1)
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
994 {
12814
b1b260a2de98 Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents: 11882
diff changeset
995 PyErr_SetString(PyExc_TypeError,
b1b260a2de98 Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents: 11882
diff changeset
996 "struct.pack requires at least one argument");
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
997 return NULL;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
998 }
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
999 format = PyTuple_GetItem(args, 0);
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1000 if (!PyArg_Parse(format, "s", &fmt))
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1001 return NULL;
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1002 f = whichtable(&fmt);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1003 size = calcsize(fmt, f);
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1004 if (size < 0)
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1005 return NULL;
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1006 result = PyString_FromStringAndSize((char *)NULL, size);
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1007 if (result == NULL)
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1008 return NULL;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1009
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1010 s = fmt;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1011 i = 1;
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1012 res = restart = PyString_AsString(result);
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1013
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1014 while ((c = *s++) != '\0') {
7982
a909a2869ac5 Make new gcc -Wall happy
Guido van Rossum <guido@python.org>
parents: 6927
diff changeset
1015 if (isspace((int)c))
6167
9e432468f756 Ignore whitespace between formats (not internal to a count+format).
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1016 continue;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1017 if ('0' <= c && c <= '9') {
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1018 num = c - '0';
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1019 while ('0' <= (c = *s++) && c <= '9')
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1020 num = num*10 + (c - '0');
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1021 if (c == '\0')
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1022 break;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1023 }
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1024 else
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1025 num = 1;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1026
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1027 e = getentry(c, f);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1028 if (e == NULL)
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1029 goto fail;
4867
e3f2ab0b0aa1 Fill pad bytes with zeros (fixing a bug dating from the very first version!).
Guido van Rossum <guido@python.org>
parents: 4865
diff changeset
1030 nres = restart + align((int)(res-restart), c, e);
e3f2ab0b0aa1 Fill pad bytes with zeros (fixing a bug dating from the very first version!).
Guido van Rossum <guido@python.org>
parents: 4865
diff changeset
1031 /* Fill padd bytes with zeros */
e3f2ab0b0aa1 Fill pad bytes with zeros (fixing a bug dating from the very first version!).
Guido van Rossum <guido@python.org>
parents: 4865
diff changeset
1032 while (res < nres)
e3f2ab0b0aa1 Fill pad bytes with zeros (fixing a bug dating from the very first version!).
Guido van Rossum <guido@python.org>
parents: 4865
diff changeset
1033 *res++ = '\0';
4842
84679f7bbe6d Fix the first bugs... treatment of 0 count was wrong, and memchr()
Guido van Rossum <guido@python.org>
parents: 4841
diff changeset
1034 if (num == 0 && c != 's')
84679f7bbe6d Fix the first bugs... treatment of 0 count was wrong, and memchr()
Guido van Rossum <guido@python.org>
parents: 4841
diff changeset
1035 continue;
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1036 do {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1037 if (c == 'x') {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1038 /* doesn't consume arguments */
4842
84679f7bbe6d Fix the first bugs... treatment of 0 count was wrong, and memchr()
Guido van Rossum <guido@python.org>
parents: 4841
diff changeset
1039 memset(res, '\0', num);
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1040 res += num;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1041 break;
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1042 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1043 if (i >= n) {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1044 PyErr_SetString(StructError,
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1045 "insufficient arguments to pack");
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1046 goto fail;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1047 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1048 v = PyTuple_GetItem(args, i++);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1049 if (v == NULL)
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1050 goto fail;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1051 if (c == 's') {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1052 /* num is string size, not repeat count */
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1053 int n;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1054 if (!PyString_Check(v)) {
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1055 PyErr_SetString(StructError,
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1056 "argument for 's' must be a string");
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1057 goto fail;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1058 }
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1059 n = PyString_Size(v);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1060 if (n > num)
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1061 n = num;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1062 if (n > 0)
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1063 memcpy(res, PyString_AsString(v), n);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1064 if (n < num)
4842
84679f7bbe6d Fix the first bugs... treatment of 0 count was wrong, and memchr()
Guido van Rossum <guido@python.org>
parents: 4841
diff changeset
1065 memset(res+n, '\0', num-n);
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1066 res += num;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1067 break;
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1068 }
6241
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1069 else if (c == 'p') {
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1070 /* num is string size + 1,
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1071 to fit in the count byte */
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1072 int n;
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1073 num--; /* now num is max string size */
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1074 if (!PyString_Check(v)) {
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1075 PyErr_SetString(StructError,
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1076 "argument for 'p' must be a string");
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1077 goto fail;
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1078 }
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1079 n = PyString_Size(v);
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1080 if (n > num)
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1081 n = num;
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1082 if (n > 0)
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1083 memcpy(res+1, PyString_AsString(v), n);
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1084 if (n < num)
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1085 /* no real need, just to be nice */
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1086 memset(res+1+n, '\0', num-n);
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1087 *res++ = n; /* store the length byte */
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1088 res += num;
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1089 break;
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1090 }
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1091 else {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1092 if (e->pack(res, v, e) < 0)
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1093 goto fail;
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1094 res += e->size;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1095 }
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1096 } while (--num > 0);
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1097 }
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1098
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1099 if (i < n) {
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1100 PyErr_SetString(StructError,
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1101 "too many arguments for pack format");
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1102 goto fail;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1103 }
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1104
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1105 return result;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1106
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1107 fail:
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1108 Py_DECREF(result);
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1109 return NULL;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1110 }
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1111
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1112
6927
52125ce376a4 Oops, left in a non-standard multi-line doc string that GCC finds okay
Guido van Rossum <guido@python.org>
parents: 6922
diff changeset
1113 static char unpack__doc__[] = "\
52125ce376a4 Oops, left in a non-standard multi-line doc string that GCC finds okay
Guido van Rossum <guido@python.org>
parents: 6922
diff changeset
1114 unpack(fmt, string) -> (v1, v2, ...)\n\
52125ce376a4 Oops, left in a non-standard multi-line doc string that GCC finds okay
Guido van Rossum <guido@python.org>
parents: 6922
diff changeset
1115 Unpack the string, containing packed C structure data, according\n\
52125ce376a4 Oops, left in a non-standard multi-line doc string that GCC finds okay
Guido van Rossum <guido@python.org>
parents: 6922
diff changeset
1116 to fmt. Requires len(string)==calcsize(fmt).\n\
6922
7629c4d985bc Added doc strings, suggested by Charles G. Waldman (but massaged quite a bit).
Guido van Rossum <guido@python.org>
parents: 6551
diff changeset
1117 See struct.__doc__ for more on format strings.";
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1118
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1119 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
1120 struct_unpack(PyObject *self, PyObject *args)
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1121 {
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1122 const formatdef *f, *e;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1123 char *str, *start, *fmt, *s;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1124 char c;
4865
f5073e24147c Scratch the ears of gcc -Wall.
Barry Warsaw <barry@python.org>
parents: 4861
diff changeset
1125 int len, size, num;
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1126 PyObject *res, *v;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1127
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
1128 if (!PyArg_ParseTuple(args, "ss#:unpack", &fmt, &start, &len))
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1129 return NULL;
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1130 f = whichtable(&fmt);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1131 size = calcsize(fmt, f);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1132 if (size < 0)
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1133 return NULL;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1134 if (size != len) {
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1135 PyErr_SetString(StructError,
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1136 "unpack str size does not match format");
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1137 return NULL;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1138 }
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1139 res = PyList_New(0);
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1140 if (res == NULL)
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1141 return NULL;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1142 str = start;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1143 s = fmt;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1144 while ((c = *s++) != '\0') {
7982
a909a2869ac5 Make new gcc -Wall happy
Guido van Rossum <guido@python.org>
parents: 6927
diff changeset
1145 if (isspace((int)c))
6167
9e432468f756 Ignore whitespace between formats (not internal to a count+format).
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1146 continue;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1147 if ('0' <= c && c <= '9') {
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1148 num = c - '0';
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1149 while ('0' <= (c = *s++) && c <= '9')
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1150 num = num*10 + (c - '0');
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1151 if (c == '\0')
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1152 break;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1153 }
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1154 else
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1155 num = 1;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1156
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1157 e = getentry(c, f);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1158 if (e == NULL)
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1159 goto fail;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1160 str = start + align((int)(str-start), c, e);
4842
84679f7bbe6d Fix the first bugs... treatment of 0 count was wrong, and memchr()
Guido van Rossum <guido@python.org>
parents: 4841
diff changeset
1161 if (num == 0 && c != 's')
84679f7bbe6d Fix the first bugs... treatment of 0 count was wrong, and memchr()
Guido van Rossum <guido@python.org>
parents: 4841
diff changeset
1162 continue;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1163
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1164 do {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1165 if (c == 'x') {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1166 str += num;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1167 break;
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1168 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1169 if (c == 's') {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1170 /* num is string size, not repeat count */
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1171 v = PyString_FromStringAndSize(str, num);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1172 if (v == NULL)
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1173 goto fail;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1174 str += num;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1175 num = 0;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1176 }
6241
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1177 else if (c == 'p') {
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1178 /* num is string buffer size,
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1179 not repeat count */
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1180 int n = *(unsigned char*)str;
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1181 /* first byte (unsigned) is string size */
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1182 if (n >= num)
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1183 n = num-1;
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1184 v = PyString_FromStringAndSize(str+1, n);
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1185 if (v == NULL)
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1186 goto fail;
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1187 str += num;
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1188 num = 0;
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1189 }
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1190 else {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1191 v = e->unpack(str, e);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1192 if (v == NULL)
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1193 goto fail;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1194 str += e->size;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1195 }
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1196 if (v == NULL || PyList_Append(res, v) < 0)
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1197 goto fail;
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1198 Py_DECREF(v);
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1199 } while (--num > 0);
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1200 }
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1201
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1202 v = PyList_AsTuple(res);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1203 Py_DECREF(res);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1204 return v;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1205
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1206 fail:
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1207 Py_DECREF(res);
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1208 return NULL;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1209 }
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1210
976
31b018bda7e2 unpack() now returns a tuple, not a list
Guido van Rossum <guido@python.org>
parents: 960
diff changeset
1211
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1212 /* List of functions */
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1213
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1214 static PyMethodDef struct_methods[] = {
6922
7629c4d985bc Added doc strings, suggested by Charles G. Waldman (but massaged quite a bit).
Guido van Rossum <guido@python.org>
parents: 6551
diff changeset
1215 {"calcsize", struct_calcsize, METH_VARARGS, calcsize__doc__},
7629c4d985bc Added doc strings, suggested by Charles G. Waldman (but massaged quite a bit).
Guido van Rossum <guido@python.org>
parents: 6551
diff changeset
1216 {"pack", struct_pack, METH_VARARGS, pack__doc__},
7629c4d985bc Added doc strings, suggested by Charles G. Waldman (but massaged quite a bit).
Guido van Rossum <guido@python.org>
parents: 6551
diff changeset
1217 {"unpack", struct_unpack, METH_VARARGS, unpack__doc__},
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1218 {NULL, NULL} /* sentinel */
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1219 };
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1220
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1221
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1222 /* Module initialization */
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1223
9588
38023a425e48 Add DL_EXPORT() to all modules that could possibly be used
Guido van Rossum <guido@python.org>
parents: 9123
diff changeset
1224 DL_EXPORT(void)
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1225 initstruct()
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1226 {
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1227 PyObject *m, *d;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1228
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1229 /* Create the module and add the functions */
6922
7629c4d985bc Added doc strings, suggested by Charles G. Waldman (but massaged quite a bit).
Guido van Rossum <guido@python.org>
parents: 6551
diff changeset
1230 m = Py_InitModule4("struct", struct_methods, struct__doc__,
7629c4d985bc Added doc strings, suggested by Charles G. Waldman (but massaged quite a bit).
Guido van Rossum <guido@python.org>
parents: 6551
diff changeset
1231 (PyObject*)NULL, PYTHON_API_VERSION);
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1232
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1233 /* Add some symbolic constants to the module */
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1234 d = PyModule_GetDict(m);
6367
2c29b8982643 Apply two changes, systematically:
Guido van Rossum <guido@python.org>
parents: 6241
diff changeset
1235 StructError = PyErr_NewException("struct.error", NULL, NULL);
2c29b8982643 Apply two changes, systematically:
Guido van Rossum <guido@python.org>
parents: 6241
diff changeset
1236 if (StructError == NULL)
2c29b8982643 Apply two changes, systematically:
Guido van Rossum <guido@python.org>
parents: 6241
diff changeset
1237 return;
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1238 PyDict_SetItemString(d, "error", StructError);
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1239 }