1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the names of Stichting Mathematisch
12 Centrum or CWI not be used in advertising or publicity pertaining to
13 distribution of the software without specific, written prior permission.
15 STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18 FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 ******************************************************************/
25 /* struct module -- pack values into and (out of) strings */
27 #include "allobjects.h"
28 #include "modsupport.h"
30 static object
*StructError
;
33 /* Define various structs to figure out the alignments of types */
37 ** XXXX We have a problem here. There are no unique alignment rules
38 ** on the PowerPC mac.
41 #pragma options align=mac68k
43 #endif /* __MWERKS__ */
45 typedef struct { char c
; short x
; } s_short
;
46 typedef struct { char c
; int x
; } s_int
;
47 typedef struct { char c
; long x
; } s_long
;
48 typedef struct { char c
; float x
; } s_float
;
49 typedef struct { char c
; double x
; } s_double
;
51 #define SHORT_ALIGN (sizeof(s_short) - sizeof(short))
52 #define INT_ALIGN (sizeof(s_int) - sizeof(int))
53 #define LONG_ALIGN (sizeof(s_long) - sizeof(long))
54 #define FLOAT_ALIGN (sizeof(s_float) - sizeof(float))
55 #define DOUBLE_ALIGN (sizeof(s_double) - sizeof(double))
58 #pragma options align=reset
62 /* Align a size according to a format code */
72 case 'h': a
= SHORT_ALIGN
; break;
73 case 'i': a
= INT_ALIGN
; break;
74 case 'l': a
= LONG_ALIGN
; break;
75 case 'f': a
= FLOAT_ALIGN
; break;
76 case 'd': a
= DOUBLE_ALIGN
; break;
79 return (size
+ a
- 1) / a
* a
;
83 /* calculate the size of a format string */
91 int size
, num
, itemsize
, x
;
95 while ((c
= *s
++) != '\0') {
96 if ('0' <= c
&& c
<= '9') {
98 while ('0' <= (c
= *s
++) && c
<= '9') {
99 x
= num
*10 + (c
- '0');
101 err_setstr(StructError
,
102 "int overflow in fmt");
113 size
= align(size
, c
);
117 case 'x': /* pad byte */
118 case 'b': /* byte-sized int */
123 case 'h': /* short ("half-size)" int */
124 itemsize
= sizeof(short);
127 case 'i': /* natural-size int */
128 itemsize
= sizeof(int);
131 case 'l': /* long int */
132 itemsize
= sizeof(long);
135 case 'f': /* float */
136 itemsize
= sizeof(float);
139 case 'd': /* double */
140 itemsize
= sizeof(double);
144 err_setstr(StructError
, "bad char in fmt");
151 if (x
/itemsize
!= num
|| size
< 0) {
152 err_setstr(StructError
, "total struct size too long");
162 /* pack(fmt, v1, v2, ...) --> string */
165 struct_calcsize(self
, args
)
166 object
*self
; /* Not used */
172 if (!getargs(args
, "s", &fmt
))
174 size
= calcsize(fmt
);
177 return newintobject((long)size
);
181 /* pack(fmt, v1, v2, ...) --> string */
184 struct_pack(self
, args
)
185 object
*self
; /* Not used */
188 object
*format
, *result
, *v
;
192 char *s
, *res
, *restart
;
197 if (args
== NULL
|| !is_tupleobject(args
) ||
198 (n
= gettuplesize(args
)) < 1) {
202 format
= gettupleitem(args
, 0);
203 if (!getargs(format
, "s", &fmt
))
205 size
= calcsize(fmt
);
208 result
= newsizedstringobject((char *)NULL
, size
);
214 res
= restart
= getstringvalue(result
);
216 while ((c
= *s
++) != '\0') {
217 if ('0' <= c
&& c
<= '9') {
219 while ('0' <= (c
= *s
++) && c
<= '9')
220 num
= num
*10 + (c
- '0');
227 res
= restart
+ align((int)(res
-restart
), c
);
232 case 'x': /* pad byte */
241 err_setstr(StructError
,
242 "insufficient arguments to pack");
245 v
= gettupleitem(args
, i
++);
246 if (!is_intobject(v
)) {
247 err_setstr(StructError
,
248 "bad argument type to pack");
251 ival
= getintvalue(v
);
258 res
+= sizeof(short);
273 err_setstr(StructError
,
274 "insufficient arguments to pack");
277 v
= gettupleitem(args
, i
++);
278 if (!is_stringobject(v
) ||
279 getstringsize(v
) != 1) {
280 err_setstr(StructError
,
281 "bad argument type to pack");
284 *res
++ = getstringvalue(v
)[0];
290 err_setstr(StructError
,
291 "insufficient arguments to pack");
294 v
= gettupleitem(args
, i
++);
295 if (!is_floatobject(v
)) {
296 err_setstr(StructError
,
297 "bad argument type to pack");
300 fval
= getfloatvalue(v
);
303 *(float*)res
= (float)fval
;
304 res
+= sizeof(float);
307 memcpy(res
, (char*)&fval
, sizeof fval
);
308 res
+= sizeof(double);
314 err_setstr(StructError
, "bad char in fmt");
322 err_setstr(StructError
, "too many arguments for pack fmt");
334 /* Helper to convert a list to a tuple */
340 int len
= getlistsize(list
);
341 object
*tuple
= newtupleobject(len
);
344 for (i
= 0; i
< len
; i
++) {
345 object
*v
= getlistitem(list
, i
);
347 settupleitem(tuple
, i
, v
);
355 /* unpack(fmt, string) --> (v1, v2, ...) */
358 struct_unpack(self
, args
)
359 object
*self
; /* Not used */
362 char *str
, *start
, *fmt
, *s
;
364 int len
, size
, num
, x
;
367 if (!getargs(args
, "(ss#)", &fmt
, &start
, &len
))
369 size
= calcsize(fmt
);
371 err_setstr(StructError
, "unpack str size does not match fmt");
374 res
= newlistobject(0);
379 while ((c
= *s
++) != '\0') {
380 if ('0' <= c
&& c
<= '9') {
382 while ('0' <= (c
= *s
++) && c
<= '9')
383 num
= num
*10 + (c
- '0');
390 str
= start
+ align((int)(str
-start
), c
);
403 v
= newintobject((long)x
);
407 v
= newsizedstringobject(str
, 1);
412 v
= newintobject((long)*(short*)str
);
413 str
+= sizeof(short);
417 v
= newintobject((long)*(int*)str
);
422 v
= newintobject(*(long*)str
);
427 v
= newfloatobject((double)*(float*)str
);
428 str
+= sizeof(float);
434 memcpy((char *)&d
, str
, sizeof d
);
435 v
= newfloatobject(d
);
436 str
+= sizeof(double);
441 err_setstr(StructError
, "bad char in fmt");
445 if (v
== NULL
|| addlistitem(res
, v
) < 0)
459 /* List of functions */
461 static struct methodlist struct_methods
[] = {
462 {"calcsize", struct_calcsize
},
463 {"pack", struct_pack
, 1/*varargs*/},
464 {"unpack", struct_unpack
},
465 {NULL
, NULL
} /* sentinel */
469 /* Module initialization */
476 /* Create the module and add the functions */
477 m
= initmodule("struct", struct_methods
);
479 /* Add some symbolic constants to the module */
480 d
= getmoduledict(m
);
481 StructError
= newstringobject("struct.error");
482 dictinsert(d
, "error", StructError
);
484 /* Check for errors */
486 fatal("can't initialize module struct");