]> git.jsancho.org Git - lugaru.git/blob - Source/private.c
e50c68a9242dfb5775e83adce640de75b6f7c21a
[lugaru.git] / Source / private.c
1 #include <string.h>
2
3 #include "private.h"
4
5 void BinIOConvert1(int from_byte_order, int to_byte_order,
6                    const uint8_t *src, uint8_t *dst,
7                    unsigned int count)
8 {
9     if (BinIONormalizeByteOrder(from_byte_order) !=
10         BinIONormalizeByteOrder(to_byte_order))
11     {
12         unsigned int i;
13         for (i = 0; i < count; ++i)
14         {
15             BinIOSwap1(src, dst);
16             src += 1;
17             dst += 1;
18         }
19     }
20     else
21     {
22         memcpy(dst, src, 1 * count);
23     }
24 }
25
26 void BinIOConvert2(int from_byte_order, int to_byte_order,
27                    const uint8_t *src, uint8_t *dst,
28                    unsigned int count)
29 {
30     if (BinIONormalizeByteOrder(from_byte_order) !=
31         BinIONormalizeByteOrder(to_byte_order))
32     {
33         unsigned int i;
34         for (i = 0; i < count; ++i)
35         {
36             BinIOSwap2(src, dst);
37             src += 2;
38             dst += 2;
39         }
40     }
41     else
42     {
43         memcpy(dst, src, 2 * count);
44     }
45 }
46
47 void BinIOConvert4(int from_byte_order, int to_byte_order,
48                    const uint8_t *src, uint8_t *dst,
49                    unsigned int count)
50 {
51     if (BinIONormalizeByteOrder(from_byte_order) !=
52         BinIONormalizeByteOrder(to_byte_order))
53     {
54         unsigned int i;
55         for (i = 0; i < count; ++i)
56         {
57             BinIOSwap4(src, dst);
58             src += 4;
59             dst += 4;
60         }
61     }
62     else
63     {
64         memcpy(dst, src, 4 * count);
65     }
66 }
67
68 void BinIOConvert8(int from_byte_order, int to_byte_order,
69                    const uint8_t *src, uint8_t *dst,
70                    unsigned int count)
71 {
72     if (BinIONormalizeByteOrder(from_byte_order) !=
73         BinIONormalizeByteOrder(to_byte_order))
74     {
75         unsigned int i;
76         for (i = 0; i < count; ++i)
77         {
78             BinIOSwap8(src, dst);
79             src += 8;
80             dst += 8;
81         }
82     }
83     else
84     {
85         memcpy(dst, src, 8 * count);
86     }
87 }
88
89 void BinIOInitFormatCursor(struct BinIOFormatCursor *cursor,
90                            const char               *format)
91 {
92     cursor->cursor = format;
93     cursor->byte_order = BinIO_HOST_BYTE_ORDER;
94     cursor->count = -1;
95 }
96
97 int BinIONextChar(void                     *context,
98                   struct BinIOFormatCursor *cursor,
99                   BinIOProcessFunction      func)
100 {
101     int count, value;
102     int c;
103     switch (c = *(cursor->cursor)++)
104     {
105         case BinIO_LITTLE_ENDIAN_BYTE_ORDER:
106         case BinIO_BIG_ENDIAN_BYTE_ORDER:
107         case BinIO_HOST_BYTE_ORDER:
108         case BinIO_NETWORK_BYTE_ORDER:
109             cursor->byte_order = c;
110             break;
111         
112         case '0': case '1': case '2': case '3': case '4':
113         case '5': case '6': case '7': case '8': case '9':
114             count = cursor->count;
115             value = c - '0';
116             if (count == -1)
117             {
118                 cursor->count = value;
119             }
120             else
121             {
122                 cursor->count = (count * 10) + value;
123             }
124             break;
125             
126         case BinIO_TYPE_IGNORE_BYTE:
127         case BinIO_TYPE_BYTE:
128         case BinIO_TYPE_INT16:
129         case BinIO_TYPE_INT32:
130         case BinIO_TYPE_INT64:
131         case BinIO_TYPE_FLOAT32:
132         case BinIO_TYPE_FLOAT64:
133             func(context, c, cursor->byte_order, cursor->count);
134             cursor->byte_order = BinIO_HOST_BYTE_ORDER;
135             cursor->count = -1;
136             break;
137             
138         case ' ': case '\t': case '\r': case '\n':
139             break;
140             
141         default:
142             return 0;
143     }
144     
145     return 1;
146 }
147
148 extern void BinIOCountBytes(void *context, int type, int byte_order, int count)
149 {
150     size_t type_size = 0;
151     
152     if (count == -1)
153     {
154         count = 1;
155     }
156     
157     switch (type)
158     {
159         case BinIO_TYPE_IGNORE_BYTE: type_size = 1; break;
160         case BinIO_TYPE_BYTE:        type_size = 1; break;
161         case BinIO_TYPE_INT16:       type_size = 2; break;
162         case BinIO_TYPE_INT32:       type_size = 4; break;
163         case BinIO_TYPE_INT64:       type_size = 8; break;
164         case BinIO_TYPE_FLOAT32:     type_size = 4; break;
165         case BinIO_TYPE_FLOAT64:     type_size = 8; break;
166     }
167     
168     *(size_t*)context += type_size * count;
169 }
170
171 extern size_t BinIOFormatByteCount(const char *format)
172 {
173     struct BinIOFormatCursor cursor;
174     size_t n_bytes = 0;
175
176     BinIOInitFormatCursor(&cursor, format);
177     
178     while (BinIONextChar(&n_bytes, &cursor, BinIOCountBytes)) {}
179     
180     return n_bytes;
181 }