]> git.jsancho.org Git - lugaru.git/blob - Source/Utils/unpack.c
5a31c91921b712e500c864aea02ac0c05447d551
[lugaru.git] / Source / Utils / unpack.c
1 /*
2 Copyright (C) 2003, 2010 - Wolfire Games
3 Copyright (C) 2010-2016 - 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 "binio.h"
22 #include "private.h"
23
24 #include <stdlib.h>
25
26 struct BinIOUnpackContext {
27     const uint8_t *data;
28     va_list        args;
29 };
30
31 static void BinIOUnpack(void *context, int type, int byte_order, int count)
32 {
33     struct BinIOUnpackContext *ctx = (struct BinIOUnpackContext*)context;
34     if (count == -1) {
35         count = 1;
36     }
37
38     switch (type) {
39     case BinIO_TYPE_IGNORE_BYTE:
40         ctx->data += 1 * count;
41         break;
42     case BinIO_TYPE_BYTE:
43         BinIOConvert1(byte_order, BinIO_HOST_BYTE_ORDER, ctx->data, va_arg(ctx->args, uint8_t *), count);
44         ctx->data += 1 * count;
45         break;
46     case BinIO_TYPE_INT16:
47         BinIOConvert2(byte_order, BinIO_HOST_BYTE_ORDER, ctx->data, va_arg(ctx->args, uint8_t *), count);
48         ctx->data += 2 * count;
49         break;
50     case BinIO_TYPE_INT32:
51         BinIOConvert4(byte_order, BinIO_HOST_BYTE_ORDER, ctx->data, va_arg(ctx->args, uint8_t *), count);
52         ctx->data += 4 * count;
53         break;
54     case BinIO_TYPE_INT64:
55         BinIOConvert8(byte_order, BinIO_HOST_BYTE_ORDER, ctx->data, va_arg(ctx->args, uint8_t *), count);
56         ctx->data += 8 * count;
57         break;
58     case BinIO_TYPE_FLOAT32:
59         BinIOConvert4(byte_order, BinIO_HOST_BYTE_ORDER, ctx->data, va_arg(ctx->args, uint8_t *), count);
60         ctx->data += 4 * count;
61         break;
62     case BinIO_TYPE_FLOAT64:
63         BinIOConvert8(byte_order, BinIO_HOST_BYTE_ORDER, ctx->data, va_arg(ctx->args, uint8_t *), count);
64         ctx->data += 8 * count;
65         break;
66     }
67 }
68
69 void unpackf(const char *format, ...)
70 {
71     va_list args;
72     va_start(args, format);
73     vfunpackf(stdin, format, args);
74     va_end(args);
75 }
76
77 void sunpackf(const void *buffer, const char *format, ...)
78 {
79     va_list args;
80     va_start(args, format);
81     vsunpackf(buffer, format, args);
82     va_end(args);
83 }
84
85 void funpackf(FILE *file, const char *format, ...)
86 {
87     va_list args;
88     va_start(args, format);
89     vfunpackf(file, format, args);
90     va_end(args);
91 }
92
93 void vsunpackf(const void *buffer, const char *format, va_list args)
94 {
95     struct BinIOFormatCursor cursor;
96     struct BinIOUnpackContext context;
97
98     BinIOInitFormatCursor(&cursor, format);
99
100     context.data = (const unsigned char*)buffer;
101     va_copy(context.args, args);
102
103     while (BinIONextChar(&context, &cursor, BinIOUnpack)) {}
104
105     va_end(context.args);
106 }
107
108 void vfunpackf(FILE *file, const char *format, va_list args)
109 {
110     size_t n_bytes = BinIOFormatByteCount(format);
111     void* buffer = malloc(n_bytes);
112     fread(buffer, n_bytes, 1, file);
113
114     vsunpackf(buffer, format, args);
115
116     free(buffer);
117 }