2 * upkg: tool for manipulating Unreal Tournament packages.
3 * Copyright (C) 2009 Nick Bowler
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 /* Unsigned integer packing. */
25 #define DEFPACK_BE(bits, type) void pack_ ## bits ## _be ( \
26 unsigned char *out, type v \
29 for (i = 1; i <= bits/8; i++) { \
30 out[bits/8 - i] = v % 256; \
35 #define DEFPACK_LE(bits, type) void pack_ ## bits ## _le ( \
36 unsigned char *out, type v \
39 for (i = 0; i < bits/8; i++) { \
45 DEFPACK_BE(16, unsigned short)
46 DEFPACK_BE(32, unsigned long)
48 DEFPACK_BE(64, unsigned long long)
51 DEFPACK_LE(16, unsigned short)
52 DEFPACK_LE(32, unsigned long)
54 DEFPACK_LE(64, unsigned long long)
57 #define DEFUNPACK_BE(bits, type) type unpack_ ## bits ## _be ( \
58 const unsigned char *in \
62 for (i = 0; i < bits/8; i++) { \
69 #define DEFUNPACK_LE(bits, type) type unpack_ ## bits ## _le ( \
70 const unsigned char *in \
74 for (i = 1; i <= bits/8; i++) { \
76 v += in[bits/8 - i]; \
81 DEFUNPACK_BE(16, unsigned short)
82 DEFUNPACK_BE(32, unsigned long)
84 DEFUNPACK_BE(64, unsigned long long)
87 DEFUNPACK_LE(16, unsigned short)
88 DEFUNPACK_LE(32, unsigned long)
90 DEFUNPACK_LE(64, unsigned long long)
94 * Two's complement signed integer packing. This is unlikely to work on
95 * systems that don't themselves use two's complement.
98 #define DEFUNPACK_SBE(bits, max, type) type unpack_s ## bits ## _be ( \
99 const unsigned char *in \
103 int sign = (in[0] & 0x80) ? 1 : 0; \
104 for (i = 0; i < bits/8; i++) { \
106 v += in[i] & (i == 0 ? 0x7f : 0xff); \
108 return sign*(-max-1) + v; \
111 #define DEFUNPACK_SLE(bits, max, type) type unpack_s ## bits ## _le ( \
112 const unsigned char *in \
116 int sign = (in[bits/8 - 1] & 0x80) ? 1 : 0; \
117 for (i = 1; i <= bits/8; i++) { \
119 v += in[bits/8 - i] & (i == 1 ? 0x7f : 0xff); \
121 return sign*(-max-1) + v; \
124 DEFUNPACK_SBE(16, 32767, short)
125 DEFUNPACK_SBE(32, 2147483647l, long)
127 DEFUNPACK_SBE(64, 9223372036854775807ll, long long)
130 DEFUNPACK_SLE(16, 32767, short)
131 DEFUNPACK_SLE(32, 2147483647l, long)
133 DEFUNPACK_SLE(64, 9223372036854775807ll, long long)