]> git.jsancho.org Git - lugaru.git/blob - Source/Utils/private.h
787a3ad325282a5bf61ec7301c4d2486ab33473b
[lugaru.git] / Source / Utils / private.h
1 /*
2 Copyright (C) 2003, 2010 - Wolfire Games
3 Copyright (C) 2010-2016 - Lugaru contributors (see AUTHORS file)
4
5 This file is part of Lugaru.
6
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.
11
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.
16
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/>.
19 */
20
21 #ifndef private_h
22 #define private_h
23
24 #include <stdarg.h>
25 #include <stddef.h>
26
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'
34
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'
39
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;
46 #else
47 typedef unsigned char      uint8_t;
48 typedef unsigned short     uint16_t;
49 typedef unsigned long      uint32_t;
50 #ifdef WIN32
51 typedef unsigned __int64   uint64_t;
52 #else
53 typedef unsigned long long uint64_t;
54 #endif
55 typedef float              float32_t;
56 typedef double             float64_t;
57 #endif
58 #endif
59
60 #ifndef BinIO_INLINE
61 #if defined(__GNUC__)
62 #define BinIO_INLINE static inline
63 #else
64 #define BinIO_INLINE static
65 #endif
66 #endif
67
68 #ifndef BinIO_BYTE_ORDER
69 #if defined(__ppc__) || defined(__POWERPC__)
70 #define BinIO_BYTE_ORDER BinIO_BIG_ENDIAN_BYTE_ORDER
71 #else
72 #define BinIO_BYTE_ORDER BinIO_LITTLE_ENDIAN_BYTE_ORDER
73 #endif
74 #endif
75
76 BinIO_INLINE void BinIOSwap1(const uint8_t *src, uint8_t *dst)
77 {
78     *dst = *src;
79 }
80
81 BinIO_INLINE void BinIOSwap2(const uint8_t *src, uint8_t *dst)
82 {
83     *(dst + 1) = *(src + 0);
84     *(dst + 0) = *(src + 1);
85 }
86
87 BinIO_INLINE void BinIOSwap4(const uint8_t *src, uint8_t *dst)
88 {
89     *(dst + 3) = *(src + 0);
90     *(dst + 2) = *(src + 1);
91     *(dst + 1) = *(src + 2);
92     *(dst + 0) = *(src + 3);
93 }
94
95 BinIO_INLINE void BinIOSwap8(const uint8_t *src, uint8_t *dst)
96 {
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);
105 }
106
107 BinIO_INLINE int BinIONormalizeByteOrder(int byte_order)
108 {
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;
113     }
114
115     return byte_order;
116 }
117
118 extern void BinIOConvert1(int from_byte_order, int to_byte_order,
119                           const uint8_t *src, uint8_t *dst,
120                           unsigned int count);
121 extern void BinIOConvert2(int from_byte_order, int to_byte_order,
122                           const uint8_t *src, uint8_t *dst,
123                           unsigned int count);
124 extern void BinIOConvert4(int from_byte_order, int to_byte_order,
125                           const uint8_t *src, uint8_t *dst,
126                           unsigned int count);
127 extern void BinIOConvert8(int from_byte_order, int to_byte_order,
128                           const uint8_t *src, uint8_t *dst,
129                           unsigned int count);
130
131 struct BinIOFormatCursor {
132     const char *cursor;
133     int         byte_order;
134     int         count;
135 };
136
137 typedef void (*BinIOProcessFunction)(void *context,
138                                      int   type,
139                                      int   byte_order,
140                                      int   count);
141
142 extern void BinIOInitFormatCursor(struct BinIOFormatCursor *cursor,
143                                   const char               *format);
144
145 extern int BinIONextChar(void                     *context,
146                          struct BinIOFormatCursor *cursor,
147                          BinIOProcessFunction      func);
148
149 extern void BinIOCountBytes(void *context, int type, int byte_order, int count);
150
151 extern size_t BinIOFormatByteCount(const char *format);
152
153 #endif
154