gen-tree.awk: Improve compatibility with vintage compilers.
[dxcommon.git] / src / pack.c
bloba4dd119e4e78b339e672be9eca3750c87efa86a5
1 /*
2 * Copyright © 2009 Nick Bowler
4 * Portable binary (de-)serialisation of integral types.
6 * License WTFPL2: Do What The Fuck You Want To Public License, version 2.
7 * This is free software: you are free to do what the fuck you want to.
8 * There is NO WARRANTY, to the extent permitted by law.
9 */
11 #include "pack.h"
13 /* Unsigned integer packing. */
14 #define DEFPACK_BE(bits, type) void pack_ ## bits ## _be ( \
15 unsigned char *out, type v \
16 ) { \
17 unsigned i; \
18 for (i = 1; i <= bits/8; i++) { \
19 out[bits/8 - i] = v % 256; \
20 v /= 256; \
21 } \
24 #define DEFPACK_LE(bits, type) void pack_ ## bits ## _le ( \
25 unsigned char *out, type v \
26 ) { \
27 unsigned i; \
28 for (i = 0; i < bits/8; i++) { \
29 out[i] = v % 256; \
30 v /= 256; \
31 } \
34 DEFPACK_BE(16, unsigned short)
35 DEFPACK_BE(32, unsigned long)
36 #if PACK_HAVE_64BIT
37 DEFPACK_BE(64, unsigned long long)
38 #endif
40 DEFPACK_LE(16, unsigned short)
41 DEFPACK_LE(32, unsigned long)
42 #if PACK_HAVE_64BIT
43 DEFPACK_LE(64, unsigned long long)
44 #endif
46 #define DEFUNPACK_BE(bits, type) type unpack_ ## bits ## _be ( \
47 const unsigned char *in \
48 ) { \
49 type v = 0; \
50 unsigned i; \
51 for (i = 0; i < bits/8; i++) { \
52 v *= 256; \
53 v += in[i]; \
54 } \
55 return v; \
58 #define DEFUNPACK_LE(bits, type) type unpack_ ## bits ## _le ( \
59 const unsigned char *in \
60 ) { \
61 type v = 0; \
62 unsigned i; \
63 for (i = 1; i <= bits/8; i++) { \
64 v *= 256; \
65 v += in[bits/8 - i]; \
66 } \
67 return v; \
70 DEFUNPACK_BE(16, unsigned short)
71 DEFUNPACK_BE(32, unsigned long)
72 #if PACK_HAVE_64BIT
73 DEFUNPACK_BE(64, unsigned long long)
74 #endif
76 DEFUNPACK_LE(16, unsigned short)
77 DEFUNPACK_LE(32, unsigned long)
78 #if PACK_HAVE_64BIT
79 DEFUNPACK_LE(64, unsigned long long)
80 #endif
83 * Two's complement signed integer packing. This is unlikely to work on
84 * systems that don't themselves use two's complement.
87 #define DEFUNPACK_SBE(bits, max, type) type unpack_s ## bits ## _be ( \
88 const unsigned char *in \
89 ) { \
90 type v = 0; \
91 unsigned i; \
92 int sign = (in[0] & 0x80) ? 1 : 0; \
93 for (i = 0; i < bits/8; i++) { \
94 v *= 256; \
95 v += in[i] & (i == 0 ? 0x7f : 0xff); \
96 } \
97 return sign*(-max-1) + v; \
100 #define DEFUNPACK_SLE(bits, max, type) type unpack_s ## bits ## _le ( \
101 const unsigned char *in \
102 ) { \
103 type v = 0; \
104 unsigned i; \
105 int sign = (in[bits/8 - 1] & 0x80) ? 1 : 0; \
106 for (i = 1; i <= bits/8; i++) { \
107 v *= 256; \
108 v += in[bits/8 - i] & (i == 1 ? 0x7f : 0xff); \
110 return sign*(-max-1) + v; \
113 DEFUNPACK_SBE(16, 32767, short)
114 DEFUNPACK_SBE(32, 2147483647l, long)
115 #if PACK_HAVE_64BIT
116 DEFUNPACK_SBE(64, 9223372036854775807ll, long long)
117 #endif
119 DEFUNPACK_SLE(16, 32767, short)
120 DEFUNPACK_SLE(32, 2147483647l, long)
121 #if PACK_HAVE_64BIT
122 DEFUNPACK_SLE(64, 9223372036854775807ll, long long)
123 #endif