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