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
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
11 This program 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.
15 See the GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 #define BinIO_TYPE_IGNORE_BYTE 'x'
29 #define BinIO_TYPE_BYTE 'b'
30 #define BinIO_TYPE_INT16 's'
31 #define BinIO_TYPE_INT32 'i'
32 #define BinIO_TYPE_INT64 'l'
33 #define BinIO_TYPE_FLOAT32 'f'
34 #define BinIO_TYPE_FLOAT64 'd'
36 #define BinIO_LITTLE_ENDIAN_BYTE_ORDER 'L'
37 #define BinIO_BIG_ENDIAN_BYTE_ORDER 'B'
38 #define BinIO_HOST_BYTE_ORDER 'H'
39 #define BinIO_NETWORK_BYTE_ORDER 'N'
41 #ifndef ALREADY_DID_BINIO_STDINT
42 #define ALREADY_DID_BINIO_STDINT
43 #if defined(BinIO_STDINT_HEADER)
44 #include BinIO_STDINT_HEADER
45 typedef float float32_t;
46 typedef double float64_t;
48 typedef unsigned char uint8_t;
49 typedef unsigned short uint16_t;
50 typedef unsigned long uint32_t;
52 typedef unsigned __int64 uint64_t;
54 typedef unsigned long long uint64_t;
56 typedef float float32_t;
57 typedef double float64_t;
63 #define BinIO_INLINE static inline
65 #define BinIO_INLINE static
69 #ifndef BinIO_BYTE_ORDER
70 #if defined(__ppc__) || defined(__POWERPC__)
71 #define BinIO_BYTE_ORDER BinIO_BIG_ENDIAN_BYTE_ORDER
73 #define BinIO_BYTE_ORDER BinIO_LITTLE_ENDIAN_BYTE_ORDER
77 BinIO_INLINE void BinIOSwap1(const uint8_t *src, uint8_t *dst)
82 BinIO_INLINE void BinIOSwap2(const uint8_t *src, uint8_t *dst)
84 *(dst + 1) = *(src + 0);
85 *(dst + 0) = *(src + 1);
88 BinIO_INLINE void BinIOSwap4(const uint8_t *src, uint8_t *dst)
90 *(dst + 3) = *(src + 0);
91 *(dst + 2) = *(src + 1);
92 *(dst + 1) = *(src + 2);
93 *(dst + 0) = *(src + 3);
96 BinIO_INLINE void BinIOSwap8(const uint8_t *src, uint8_t *dst)
98 *(dst + 7) = *(src + 0);
99 *(dst + 6) = *(src + 1);
100 *(dst + 5) = *(src + 2);
101 *(dst + 4) = *(src + 3);
102 *(dst + 3) = *(src + 4);
103 *(dst + 2) = *(src + 5);
104 *(dst + 1) = *(src + 6);
105 *(dst + 0) = *(src + 7);
108 BinIO_INLINE int BinIONormalizeByteOrder(int byte_order)
110 if (byte_order == BinIO_HOST_BYTE_ORDER)
112 byte_order = BinIO_BYTE_ORDER;
114 else if (byte_order == BinIO_NETWORK_BYTE_ORDER)
116 byte_order = BinIO_BIG_ENDIAN_BYTE_ORDER;
122 extern void BinIOConvert1(int from_byte_order, int to_byte_order,
123 const uint8_t *src, uint8_t *dst,
125 extern void BinIOConvert2(int from_byte_order, int to_byte_order,
126 const uint8_t *src, uint8_t *dst,
128 extern void BinIOConvert4(int from_byte_order, int to_byte_order,
129 const uint8_t *src, uint8_t *dst,
131 extern void BinIOConvert8(int from_byte_order, int to_byte_order,
132 const uint8_t *src, uint8_t *dst,
135 struct BinIOFormatCursor
142 typedef void (*BinIOProcessFunction)(void *context,
147 extern void BinIOInitFormatCursor(struct BinIOFormatCursor *cursor,
150 extern int BinIONextChar(void *context,
151 struct BinIOFormatCursor *cursor,
152 BinIOProcessFunction func);
154 extern void BinIOCountBytes(void *context, int type, int byte_order, int count);
156 extern size_t BinIOFormatByteCount(const char *format);