2 Copyright (C) 2003, 2010 - Wolfire Games
4 This file is part of Lugaru.
6 Lugaru is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 Lugaru is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Lugaru. If not, see <http://www.gnu.org/licenses/>.
26 #define BinIO_TYPE_IGNORE_BYTE 'x'
27 #define BinIO_TYPE_BYTE 'b'
28 #define BinIO_TYPE_INT16 's'
29 #define BinIO_TYPE_INT32 'i'
30 #define BinIO_TYPE_INT64 'l'
31 #define BinIO_TYPE_FLOAT32 'f'
32 #define BinIO_TYPE_FLOAT64 'd'
34 #define BinIO_LITTLE_ENDIAN_BYTE_ORDER 'L'
35 #define BinIO_BIG_ENDIAN_BYTE_ORDER 'B'
36 #define BinIO_HOST_BYTE_ORDER 'H'
37 #define BinIO_NETWORK_BYTE_ORDER 'N'
39 #ifndef ALREADY_DID_BINIO_STDINT
40 #define ALREADY_DID_BINIO_STDINT
41 #if defined(BinIO_STDINT_HEADER)
42 #include BinIO_STDINT_HEADER
43 typedef float float32_t;
44 typedef double float64_t;
46 typedef unsigned char uint8_t;
47 typedef unsigned short uint16_t;
48 typedef unsigned long uint32_t;
50 typedef unsigned __int64 uint64_t;
52 typedef unsigned long long uint64_t;
54 typedef float float32_t;
55 typedef double float64_t;
61 #define BinIO_INLINE static inline
63 #define BinIO_INLINE static
67 #ifndef BinIO_BYTE_ORDER
68 #if defined(__ppc__) || defined(__POWERPC__)
69 #define BinIO_BYTE_ORDER BinIO_BIG_ENDIAN_BYTE_ORDER
71 #define BinIO_BYTE_ORDER BinIO_LITTLE_ENDIAN_BYTE_ORDER
75 BinIO_INLINE void BinIOSwap1(const uint8_t *src, uint8_t *dst)
80 BinIO_INLINE void BinIOSwap2(const uint8_t *src, uint8_t *dst)
82 *(dst + 1) = *(src + 0);
83 *(dst + 0) = *(src + 1);
86 BinIO_INLINE void BinIOSwap4(const uint8_t *src, uint8_t *dst)
88 *(dst + 3) = *(src + 0);
89 *(dst + 2) = *(src + 1);
90 *(dst + 1) = *(src + 2);
91 *(dst + 0) = *(src + 3);
94 BinIO_INLINE void BinIOSwap8(const uint8_t *src, uint8_t *dst)
96 *(dst + 7) = *(src + 0);
97 *(dst + 6) = *(src + 1);
98 *(dst + 5) = *(src + 2);
99 *(dst + 4) = *(src + 3);
100 *(dst + 3) = *(src + 4);
101 *(dst + 2) = *(src + 5);
102 *(dst + 1) = *(src + 6);
103 *(dst + 0) = *(src + 7);
106 BinIO_INLINE int BinIONormalizeByteOrder(int byte_order)
108 if (byte_order == BinIO_HOST_BYTE_ORDER) {
109 byte_order = BinIO_BYTE_ORDER;
110 } else if (byte_order == BinIO_NETWORK_BYTE_ORDER) {
111 byte_order = BinIO_BIG_ENDIAN_BYTE_ORDER;
117 extern void BinIOConvert1(int from_byte_order, int to_byte_order,
118 const uint8_t *src, uint8_t *dst,
120 extern void BinIOConvert2(int from_byte_order, int to_byte_order,
121 const uint8_t *src, uint8_t *dst,
123 extern void BinIOConvert4(int from_byte_order, int to_byte_order,
124 const uint8_t *src, uint8_t *dst,
126 extern void BinIOConvert8(int from_byte_order, int to_byte_order,
127 const uint8_t *src, uint8_t *dst,
130 struct BinIOFormatCursor {
136 typedef void (*BinIOProcessFunction)(void *context,
141 extern void BinIOInitFormatCursor(struct BinIOFormatCursor *cursor,
144 extern int BinIONextChar(void *context,
145 struct BinIOFormatCursor *cursor,
146 BinIOProcessFunction func);
148 extern void BinIOCountBytes(void *context, int type, int byte_order, int count);
150 extern size_t BinIOFormatByteCount(const char *format);