2
Catlib Copyright Notice
4
The author of this software is Christopher Adam Telfer
5
Copyright (c) 1998, 1999, 2000, 2001, 2002
6
by Christopher Adam Telfer. All Rights Reserved.
8
Permission to use, copy, modify, and distribute this software for any
9
purpose without fee is hereby granted, provided that the above copyright
10
notice, this paragraph, and the following two paragraphs appear in all
11
copies, modifications, and distributions.
13
IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
14
SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
15
ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
16
THE AUTHOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
18
THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
19
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20
PARTICULAR PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF
21
ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS". THE AUTHOR HAS NO
22
OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
30
typedef unsigned char byte;
31
typedef unsigned short half;
32
typedef unsigned int word;
33
typedef signed char sbyte;
34
typedef signed short shalf;
35
typedef signed int sword;
41
Small letters: do not convert (not implemented for arrays and P)
42
Captial letters: convert from network byte order to host byte order
47
a - array (32-byte unsigned long + that many bytes)
48
c - signed 8 bit value
49
s - signed 16 bit value
50
l - signed 32 bit value
51
p - (unpack only) value is a pointer to a pointer. Generate the buffer
54
prefixing with a number K means that the argument will be an array of K
55
of the arguments specified by the letter
59
EXTRACTOR_common_cat_pack (void *buf, const char *fmt, ...)
64
unsigned int nreps, i;
69
struct cat_bvec *cbvp;
83
/* We use cp instead of fmt to keep the 'const' qualifier of fmt */
84
nreps = strtoul (fmt, &cp, 0);
96
*bp++ = va_arg (ap, int);
101
bytep = va_arg (ap, byte *);
102
for (i = 0; i < nreps; ++i)
114
val = va_arg (ap, int);
121
halfp = va_arg (ap, half *);
122
for (i = 0; i < nreps; ++i)
136
val = va_arg (ap, int);
143
halfp = va_arg (ap, half *);
144
for (i = 0; i < nreps; ++i)
158
val = va_arg (ap, word);
167
wordp = va_arg (ap, word *);
168
for (i = 0; i < nreps; ++i)
184
val = va_arg (ap, word);
193
wordp = va_arg (ap, word *);
194
for (i = 0; i < nreps; ++i)
209
blen = va_arg (ap, word);
210
arr = va_arg (ap, void *);
215
memmove (bp, arr, blen);
217
npacked += blen + 4; /* +4 for the 32 bits of length field */
221
cbvp = va_arg (ap, struct cat_bvec *);
222
for (i = 0; i < nreps; ++i)
230
memmove (bp, arr, blen);
232
npacked += blen + 4; /* see above */
251
EXTRACTOR_common_cat_unpack (const void *buf, const char *fmt, ...)
254
word maxlen, len, *wordp;
256
byte *bp, *bytep, *newbuf;
262
unsigned int nreps, i, isnonprefixed = 1; /* used for 'a' types only */
263
struct cat_bvec *cbvp;
277
/* We use cp instead of format to keep the 'const' qualifier of fmt */
278
nreps = strtoul (fmt, &cp, 0);
288
bytep = va_arg (ap, byte *);
289
for (i = 0; i < nreps; ++i)
300
halfp = va_arg (ap, half *);
301
for (i = 0; i < nreps; ++i)
304
*halfp |= *bp++ << 8;
311
halfp = va_arg (ap, half *);
312
for (i = 0; i < nreps; ++i)
323
wordp = va_arg (ap, word *);
324
for (i = 0; i < nreps; ++i)
327
*wordp |= *bp++ << 8;
328
*wordp |= *bp++ << 16;
329
*wordp |= *bp++ << 24;
336
wordp = va_arg (ap, word *);
337
for (i = 0; i < nreps; ++i)
339
*wordp = *bp++ << 24;
340
*wordp |= *bp++ << 16;
341
*wordp |= *bp++ << 8;
351
maxlen = va_arg (ap, word);
352
arr = va_arg (ap, void *);
365
memmove (arr, bp, len);
372
cbvp = va_arg (ap, struct cat_bvec *);
373
for (i = 0; i < nreps; ++i)
386
memmove (arr, bp, len);
399
sbytep = va_arg (ap, sbyte *);
400
for (i = 0; i < nreps; ++i)
404
if ((sizeof (sbyte) > 1) && (*sbytep & 0x80))
405
*sbytep |= (~0) << ((sizeof (sbyte) - 1) * 8);
414
shalfp = va_arg (ap, shalf *);
415
for (i = 0; i < nreps; ++i)
418
*shalfp |= *bp++ << 8;
420
if ((sizeof (shalf) > 2) && (*shalfp & 0x8000))
421
*shalfp |= (~0) << ((sizeof (shalf) - 2) * 8);
429
shalfp = va_arg (ap, shalf *);
430
for (i = 0; i < nreps; ++i)
432
*shalfp = *bp++ << 8;
435
if ((sizeof (shalf) > 2) && (*shalfp & 0x8000))
436
*shalfp |= (~0) << ((sizeof (shalf) - 2) * 8);
444
swordp = va_arg (ap, sword *);
445
for (i = 0; i < nreps; ++i)
448
*swordp |= *bp++ << 8;
449
*swordp |= *bp++ << 16;
450
*swordp |= *bp++ << 24;
452
if ((sizeof (swordp) > 4) && (*swordp & 0x80000000))
453
*swordp |= (~0) << ((sizeof (sword) - 4) * 8);
461
swordp = va_arg (ap, sword *);
462
for (i = 0; i < nreps; ++i)
464
*swordp = *bp++ << 24;
465
*swordp |= *bp++ << 16;
466
*swordp |= *bp++ << 8;
469
if ((sizeof (swordp) > 4) && (*swordp & 0x80000000))
470
*swordp |= (~0) << ((sizeof (sword) - 4) * 8);
478
cbvp = va_arg (ap, struct cat_bvec *);
479
for (i = 0; i < nreps; ++i)
486
newbuf = (byte *) malloc (len);
491
for (j = 0; j < i; j++)
497
memmove (newbuf, bp, len);
498
cbvp[i].data = newbuf;