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