2 Copyright (C) 2003, 2010 - Wolfire Games
4 This file is part of Lugaru.
6 Lugaru is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 Lugaru is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Lugaru. If not, see <http://www.gnu.org/licenses/>.
24 void BinIOConvert1(int from_byte_order, int to_byte_order,
25 const uint8_t *src, uint8_t *dst,
28 if (BinIONormalizeByteOrder(from_byte_order) !=
29 BinIONormalizeByteOrder(to_byte_order)) {
31 for (i = 0; i < count; ++i) {
37 memcpy(dst, src, 1 * count);
41 void BinIOConvert2(int from_byte_order, int to_byte_order,
42 const uint8_t *src, uint8_t *dst,
45 if (BinIONormalizeByteOrder(from_byte_order) !=
46 BinIONormalizeByteOrder(to_byte_order)) {
48 for (i = 0; i < count; ++i) {
54 memcpy(dst, src, 2 * count);
58 void BinIOConvert4(int from_byte_order, int to_byte_order,
59 const uint8_t *src, uint8_t *dst,
62 if (BinIONormalizeByteOrder(from_byte_order) !=
63 BinIONormalizeByteOrder(to_byte_order)) {
65 for (i = 0; i < count; ++i) {
71 memcpy(dst, src, 4 * count);
75 void BinIOConvert8(int from_byte_order, int to_byte_order,
76 const uint8_t *src, uint8_t *dst,
79 if (BinIONormalizeByteOrder(from_byte_order) !=
80 BinIONormalizeByteOrder(to_byte_order)) {
82 for (i = 0; i < count; ++i) {
88 memcpy(dst, src, 8 * count);
92 void BinIOInitFormatCursor(struct BinIOFormatCursor *cursor,
95 cursor->cursor = format;
96 cursor->byte_order = BinIO_HOST_BYTE_ORDER;
100 int BinIONextChar(void *context,
101 struct BinIOFormatCursor *cursor,
102 BinIOProcessFunction func)
106 switch (c = *(cursor->cursor)++) {
107 case BinIO_LITTLE_ENDIAN_BYTE_ORDER:
108 case BinIO_BIG_ENDIAN_BYTE_ORDER:
109 case BinIO_HOST_BYTE_ORDER:
110 case BinIO_NETWORK_BYTE_ORDER:
111 cursor->byte_order = c;
124 count = cursor->count;
127 cursor->count = value;
129 cursor->count = (count * 10) + value;
133 case BinIO_TYPE_IGNORE_BYTE:
134 case BinIO_TYPE_BYTE:
135 case BinIO_TYPE_INT16:
136 case BinIO_TYPE_INT32:
137 case BinIO_TYPE_INT64:
138 case BinIO_TYPE_FLOAT32:
139 case BinIO_TYPE_FLOAT64:
140 func(context, c, cursor->byte_order, cursor->count);
141 cursor->byte_order = BinIO_HOST_BYTE_ORDER;
158 extern void BinIOCountBytes(void *context, int type, int byte_order, int count)
160 size_t type_size = 0;
167 case BinIO_TYPE_IGNORE_BYTE:
170 case BinIO_TYPE_BYTE:
173 case BinIO_TYPE_INT16:
176 case BinIO_TYPE_INT32:
179 case BinIO_TYPE_INT64:
182 case BinIO_TYPE_FLOAT32:
185 case BinIO_TYPE_FLOAT64:
190 *(size_t*)context += type_size * count;
193 extern size_t BinIOFormatByteCount(const char *format)
195 struct BinIOFormatCursor cursor;
198 BinIOInitFormatCursor(&cursor, format);
200 while (BinIONextChar(&n_bytes, &cursor, BinIOCountBytes)) {}