]> git.jsancho.org Git - lugaru.git/blob - Source/private.h
79edf26612a2a01f765f58a8d420f44479e660ab
[lugaru.git] / Source / private.h
1 #ifndef private_h
2 #define private_h
3
4 #include <stdarg.h>
5 #include <stddef.h>
6
7 #define BinIO_TYPE_IGNORE_BYTE         'x'
8 #define BinIO_TYPE_BYTE                'b'
9 #define BinIO_TYPE_INT16               's'
10 #define BinIO_TYPE_INT32               'i'
11 #define BinIO_TYPE_INT64               'l'
12 #define BinIO_TYPE_FLOAT32             'f'
13 #define BinIO_TYPE_FLOAT64             'd'
14
15 #define BinIO_LITTLE_ENDIAN_BYTE_ORDER 'L'
16 #define BinIO_BIG_ENDIAN_BYTE_ORDER    'B'
17 #define BinIO_HOST_BYTE_ORDER          'H'
18 #define BinIO_NETWORK_BYTE_ORDER       'N'
19
20 #ifndef ALREADY_DID_BINIO_STDINT
21 #define ALREADY_DID_BINIO_STDINT
22 #if defined(BinIO_STDINT_HEADER)
23 #include BinIO_STDINT_HEADER
24 typedef float              float32_t;
25 typedef double             float64_t;
26 #else
27 typedef unsigned char      uint8_t;
28 typedef unsigned short     uint16_t;
29 typedef unsigned long      uint32_t;
30 #ifdef WIN32
31         typedef unsigned __int64        uint64_t;
32 #else
33         typedef unsigned long long uint64_t;
34 #endif
35 typedef float              float32_t;
36 typedef double             float64_t;
37 #endif
38 #endif
39
40 #ifndef BinIO_INLINE
41 #if defined(__GNUC__)
42 #define BinIO_INLINE static inline
43 #else
44 #define BinIO_INLINE static
45 #endif
46 #endif
47
48 #ifndef BinIO_BYTE_ORDER
49 #if defined(__ppc__) || defined(__POWERPC__)
50 #define BinIO_BYTE_ORDER BinIO_BIG_ENDIAN_BYTE_ORDER
51 #else
52 #define BinIO_BYTE_ORDER BinIO_LITTLE_ENDIAN_BYTE_ORDER
53 #endif
54 #endif
55
56 BinIO_INLINE void BinIOSwap1(const uint8_t *src, uint8_t *dst)
57 {
58     *dst = *src;
59 }
60
61 BinIO_INLINE void BinIOSwap2(const uint8_t *src, uint8_t *dst)
62 {
63     *(dst + 1) = *(src + 0);
64     *(dst + 0) = *(src + 1);
65 }
66
67 BinIO_INLINE void BinIOSwap4(const uint8_t *src, uint8_t *dst)
68 {
69     *(dst + 3) = *(src + 0);
70     *(dst + 2) = *(src + 1);
71     *(dst + 1) = *(src + 2);
72     *(dst + 0) = *(src + 3);
73 }
74
75 BinIO_INLINE void BinIOSwap8(const uint8_t *src, uint8_t *dst)
76 {
77     *(dst + 7) = *(src + 0);
78     *(dst + 6) = *(src + 1);
79     *(dst + 5) = *(src + 2);
80     *(dst + 4) = *(src + 3);
81     *(dst + 3) = *(src + 4);
82     *(dst + 2) = *(src + 5);
83     *(dst + 1) = *(src + 6);
84     *(dst + 0) = *(src + 7);
85 }
86
87 BinIO_INLINE int BinIONormalizeByteOrder(int byte_order)
88 {
89     if (byte_order == BinIO_HOST_BYTE_ORDER)
90     {
91         byte_order = BinIO_BYTE_ORDER;
92     }
93     else if (byte_order == BinIO_NETWORK_BYTE_ORDER)
94     {
95         byte_order = BinIO_BIG_ENDIAN_BYTE_ORDER;
96     }
97     
98     return byte_order;
99 }
100
101 extern void BinIOConvert1(int from_byte_order, int to_byte_order,
102                           const uint8_t *src, uint8_t *dst,
103                           unsigned int count);
104 extern void BinIOConvert2(int from_byte_order, int to_byte_order,
105                           const uint8_t *src, uint8_t *dst,
106                           unsigned int count);
107 extern void BinIOConvert4(int from_byte_order, int to_byte_order,
108                           const uint8_t *src, uint8_t *dst,
109                           unsigned int count);
110 extern void BinIOConvert8(int from_byte_order, int to_byte_order,
111                           const uint8_t *src, uint8_t *dst,
112                           unsigned int count);
113
114 struct BinIOFormatCursor
115 {
116     const char *cursor;
117     int         byte_order;
118     int         count;
119 };
120
121 typedef void (*BinIOProcessFunction)(void *context,
122                                      int   type,
123                                      int   byte_order,
124                                      int   count);
125
126 extern void BinIOInitFormatCursor(struct BinIOFormatCursor *cursor,
127                                   const char               *format);
128
129 extern int BinIONextChar(void                     *context,
130                          struct BinIOFormatCursor *cursor,
131                          BinIOProcessFunction      func);
132
133 extern void BinIOCountBytes(void *context, int type, int byte_order, int count);
134
135 extern size_t BinIOFormatByteCount(const char *format);
136
137 #endif
138