2 Copyright (C) 2003, 2010 - Wolfire Games
3 Copyright (C) 2010-2017 - Lugaru contributors (see AUTHORS file)
5 This file is part of Lugaru.
7 Lugaru is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 Lugaru is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Lugaru. If not, see <http://www.gnu.org/licenses/>.
27 #define BinIO_TYPE_IGNORE_BYTE 'x'
28 #define BinIO_TYPE_BYTE 'b'
29 #define BinIO_TYPE_INT16 's'
30 #define BinIO_TYPE_INT32 'i'
31 #define BinIO_TYPE_INT64 'l'
32 #define BinIO_TYPE_FLOAT32 'f'
33 #define BinIO_TYPE_FLOAT64 'd'
35 #define BinIO_LITTLE_ENDIAN_BYTE_ORDER 'L'
36 #define BinIO_BIG_ENDIAN_BYTE_ORDER 'B'
37 #define BinIO_HOST_BYTE_ORDER 'H'
38 #define BinIO_NETWORK_BYTE_ORDER 'N'
40 #ifndef ALREADY_DID_BINIO_STDINT
41 #define ALREADY_DID_BINIO_STDINT
42 #if defined(BinIO_STDINT_HEADER)
43 #include BinIO_STDINT_HEADER
44 typedef float float32_t;
45 typedef double float64_t;
47 typedef unsigned char uint8_t;
48 typedef unsigned short uint16_t;
49 typedef unsigned long uint32_t;
51 typedef unsigned __int64 uint64_t;
53 typedef unsigned long long uint64_t;
55 typedef float float32_t;
56 typedef double float64_t;
62 #define BinIO_INLINE static inline
64 #define BinIO_INLINE static
68 #ifndef BinIO_BYTE_ORDER
69 #if defined(__ppc__) || defined(__POWERPC__)
70 #define BinIO_BYTE_ORDER BinIO_BIG_ENDIAN_BYTE_ORDER
72 #define BinIO_BYTE_ORDER BinIO_LITTLE_ENDIAN_BYTE_ORDER
76 BinIO_INLINE void BinIOSwap1(const uint8_t *src, uint8_t *dst)
81 BinIO_INLINE void BinIOSwap2(const uint8_t *src, uint8_t *dst)
83 *(dst + 1) = *(src + 0);
84 *(dst + 0) = *(src + 1);
87 BinIO_INLINE void BinIOSwap4(const uint8_t *src, uint8_t *dst)
89 *(dst + 3) = *(src + 0);
90 *(dst + 2) = *(src + 1);
91 *(dst + 1) = *(src + 2);
92 *(dst + 0) = *(src + 3);
95 BinIO_INLINE void BinIOSwap8(const uint8_t *src, uint8_t *dst)
97 *(dst + 7) = *(src + 0);
98 *(dst + 6) = *(src + 1);
99 *(dst + 5) = *(src + 2);
100 *(dst + 4) = *(src + 3);
101 *(dst + 3) = *(src + 4);
102 *(dst + 2) = *(src + 5);
103 *(dst + 1) = *(src + 6);
104 *(dst + 0) = *(src + 7);
107 BinIO_INLINE int BinIONormalizeByteOrder(int byte_order)
109 if (byte_order == BinIO_HOST_BYTE_ORDER) {
110 byte_order = BinIO_BYTE_ORDER;
111 } else if (byte_order == BinIO_NETWORK_BYTE_ORDER) {
112 byte_order = BinIO_BIG_ENDIAN_BYTE_ORDER;
118 extern void BinIOConvert1(int from_byte_order, int to_byte_order,
119 const uint8_t *src, uint8_t *dst,
121 extern void BinIOConvert2(int from_byte_order, int to_byte_order,
122 const uint8_t *src, uint8_t *dst,
124 extern void BinIOConvert4(int from_byte_order, int to_byte_order,
125 const uint8_t *src, uint8_t *dst,
127 extern void BinIOConvert8(int from_byte_order, int to_byte_order,
128 const uint8_t *src, uint8_t *dst,
131 struct BinIOFormatCursor {
137 typedef void (*BinIOProcessFunction)(void *context,
142 extern void BinIOInitFormatCursor(struct BinIOFormatCursor *cursor,
145 extern int BinIONextChar(void *context,
146 struct BinIOFormatCursor *cursor,
147 BinIOProcessFunction func);
149 extern void BinIOCountBytes(void *context, int type, int byte_order, int count);
151 extern size_t BinIOFormatByteCount(const char *format);