2 Copyright (C) 2003, 2010 - Wolfire Games
3 Copyright (C) 2010-2016 - Lugaru contributors (see AUTHORS file)
5 This file is part of Lugaru.
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.
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.
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/>.
25 void BinIOConvert1(int from_byte_order, int to_byte_order,
26 const uint8_t *src, uint8_t *dst,
29 if (BinIONormalizeByteOrder(from_byte_order) !=
30 BinIONormalizeByteOrder(to_byte_order)) {
32 for (i = 0; i < count; ++i) {
38 memcpy(dst, src, 1 * count);
42 void BinIOConvert2(int from_byte_order, int to_byte_order,
43 const uint8_t *src, uint8_t *dst,
46 if (BinIONormalizeByteOrder(from_byte_order) !=
47 BinIONormalizeByteOrder(to_byte_order)) {
49 for (i = 0; i < count; ++i) {
55 memcpy(dst, src, 2 * count);
59 void BinIOConvert4(int from_byte_order, int to_byte_order,
60 const uint8_t *src, uint8_t *dst,
63 if (BinIONormalizeByteOrder(from_byte_order) !=
64 BinIONormalizeByteOrder(to_byte_order)) {
66 for (i = 0; i < count; ++i) {
72 memcpy(dst, src, 4 * count);
76 void BinIOConvert8(int from_byte_order, int to_byte_order,
77 const uint8_t *src, uint8_t *dst,
80 if (BinIONormalizeByteOrder(from_byte_order) !=
81 BinIONormalizeByteOrder(to_byte_order)) {
83 for (i = 0; i < count; ++i) {
89 memcpy(dst, src, 8 * count);
93 void BinIOInitFormatCursor(struct BinIOFormatCursor *cursor,
96 cursor->cursor = format;
97 cursor->byte_order = BinIO_HOST_BYTE_ORDER;
101 int BinIONextChar(void *context,
102 struct BinIOFormatCursor *cursor,
103 BinIOProcessFunction func)
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;
125 count = cursor->count;
128 cursor->count = value;
130 cursor->count = (count * 10) + value;
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;
159 extern void BinIOCountBytes(void *context, int type, int byte_order, int count)
161 size_t type_size = 0;
168 case BinIO_TYPE_IGNORE_BYTE:
171 case BinIO_TYPE_BYTE:
174 case BinIO_TYPE_INT16:
177 case BinIO_TYPE_INT32:
180 case BinIO_TYPE_INT64:
183 case BinIO_TYPE_FLOAT32:
186 case BinIO_TYPE_FLOAT64:
191 *(size_t*)context += type_size * count;
194 extern size_t BinIOFormatByteCount(const char *format)
196 struct BinIOFormatCursor cursor;
199 BinIOInitFormatCursor(&cursor, format);
201 while (BinIONextChar(&n_bytes, &cursor, BinIOCountBytes)) {}