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 2 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, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 /* Unsigned integer packing. */
26 #define DEFPACK_BE(bits, type) void pack_ ## bits ## _be ( \
27 unsigned char *out, type v \
30 for (i = 1; i <= bits/8; i++) { \
31 out[bits/8 - i] = v % 256; \
36 #define DEFPACK_LE(bits, type) void pack_ ## bits ## _le ( \
37 unsigned char *out, type v \
40 for (i = 0; i < bits/8; i++) { \
46 DEFPACK_BE(16, unsigned short)
47 DEFPACK_BE(32, unsigned long)
49 DEFPACK_BE(64, unsigned long long)
52 DEFPACK_LE(16, unsigned short)
53 DEFPACK_LE(32, unsigned long)
55 DEFPACK_LE(64, unsigned long long)
58 #define DEFUNPACK_BE(bits, type) type unpack_ ## bits ## _be ( \
63 for (i = 0; i < bits/8; i++) { \
70 #define DEFUNPACK_LE(bits, type) type unpack_ ## bits ## _le ( \
75 for (i = 1; i <= bits/8; i++) { \
77 v += in[bits/8 - i]; \
82 DEFUNPACK_BE(16, unsigned short)
83 DEFUNPACK_BE(32, unsigned long)
85 DEFUNPACK_BE(64, unsigned long long)
88 DEFUNPACK_LE(16, unsigned short)
89 DEFUNPACK_LE(32, unsigned long)
91 DEFUNPACK_LE(64, unsigned long long)
95 * Two's complement signed integer packing. This is unlikely to work on
96 * systems that don't themselves use two's complement.
99 #define DEFUNPACK_SBE(bits, max, type) type unpack_s ## bits ## _be ( \
104 int sign = (in[0] & 0x80) ? 1 : 0; \
106 for (i = 0; i < bits/8; i++) { \
110 return sign*(-max-1) + v; \
113 #define DEFUNPACK_SLE(bits, max, type) type unpack_s ## bits ## _le ( \
118 int sign = (in[bits/8 - 1] & 0x80) ? 1 : 0; \
119 in[bits/8 - 1] &= 0x7f; \
120 for (i = 1; i <= bits/8; i++) { \
122 v += in[bits/8 - i]; \
124 return sign*(-max-1) + v; \
127 DEFUNPACK_SBE(16, 32767, short)
128 DEFUNPACK_SBE(32, 2147483647l, long)
130 DEFUNPACK_SBE(64, 9223372036854775807ll, long long)
133 DEFUNPACK_SLE(16, 32767, short)
134 DEFUNPACK_SLE(32, 2147483647l, long)
136 DEFUNPACK_SLE(64, 9223372036854775807ll, long long)