aboutsummaryrefslogtreecommitdiff
path: root/saga/ys_binread.cpp
blob: 3712b28011ab4c24cb6ebf547df9b7b985dc7e27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/* ScummVM - Scumm Interpreter
 * Copyright (C) 2004 The ScummVM project
 *
 * The ReInherit Engine is (C)2000-2003 by Daniel Balsom.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Header$
 *
 */
#include <stdio.h>
#include "yslib.h"

namespace Saga {

void
ys_read_4cc(char *fourcc,
    const unsigned char *data_p, const unsigned char **data_pp)
{
	fourcc[0] = (char)data_p[0];
	fourcc[1] = (char)data_p[1];
	fourcc[2] = (char)data_p[2];
	fourcc[3] = (char)data_p[3];

	if (data_pp) {

		*data_pp = data_p + 4;
	}

	return;
}

unsigned int
ys_read_u8(const unsigned char *data_p, const unsigned char **data_pp)
/*---------------------------------------------------------------------------*\
 * Reads an unsigned 8 bit integer in from the array of bytes pointed to by
 *  'data_p'. If 'data_pp' is not null, it will set '*data_pp' to point past
 *  the integer read. 
\*---------------------------------------------------------------------------*/
{
	unsigned int u8 = *data_p;

	if (data_pp != NULL) {
		*data_pp = data_p + 1;
	}

	return u8;
}

int ys_read_s8(const unsigned char *data_p, const unsigned char **data_pp)
/*---------------------------------------------------------------------------*\
 * Reads a signed 8 bit integer in two's complement notation from the array
 *  of bytes pointed to by 'data_p'. If 'data_pp' is not null, it will set
 * '*data_pp' to point past the integer read. 
\*---------------------------------------------------------------------------*/
{
	unsigned int u8 = *data_p;
	int s8;

#ifndef YS_ASSUME_2S_COMP
	if (u8 & 0x80U) {
		s8 = (int)(u8 - 0x80U) - 0x7F - 1;
	} else
#endif
		s8 = u8;

	if (data_pp != NULL) {
		*data_pp = data_p + 1;
	}

	return s8;
}

unsigned int
ys_read_u16_be(const unsigned char *data_p, const unsigned char **data_pp)
/*---------------------------------------------------------------------------*\
 * Reads an unsigned 16 bit integer in big-endian format from the array of 
 * bytes pointed to by 'data_p'. If 'data_pp' is not null, it will set   
 * '*data_pp' to point past the integer read. 
\*---------------------------------------------------------------------------*/
{
	unsigned int u16_be = ((unsigned int)data_p[0] << 8) | data_p[1];

	if (data_pp != NULL) {
		*data_pp = data_p + 2;
	}

	return u16_be;
}

unsigned int
ys_read_u16_le(const unsigned char *data_p, const unsigned char **data_pp)
/*---------------------------------------------------------------------------*\
 * Reads an unsigned 16 bit integer in little-endian format from the array of 
 * bytes pointed to by 'data_p'. If 'data_pp' is not null, it will set   
 * '*data_pp' to point past the integer read. 
\*---------------------------------------------------------------------------*/
{
	unsigned int u16_le = ((unsigned int)data_p[1] << 8) | data_p[0];

	if (data_pp != NULL) {
		*data_pp = data_p + 2;
	}

	return u16_le;
}

int ys_read_s16_be(const unsigned char *data_p, const unsigned char **data_pp)
/*---------------------------------------------------------------------------*\
 * Reads a signed 16 bit integer in big-endian, 2's complement format from
 *  the array of bytes pointed to by 'data_p'. 
 * If 'data_pp' is not null, it will set '*data_pp' to point past the integer
 *  read.
\*---------------------------------------------------------------------------*/
{
	unsigned int u16_be = ((unsigned int)data_p[0] << 8) | data_p[1];
	int s16_be;

#ifndef YS_ASSUME_2S_COMP
	if (u16_be & 0x8000U) {
		s16_be = (int)(u16_be - 0x8000U) - 0x7FFF - 1;
	} else
#endif
		s16_be = u16_be;

	if (data_pp != NULL) {
		*data_pp = data_p + 2;
	}

	return s16_be;
}

int ys_read_s16_le(const unsigned char *data_p, const unsigned char **data_pp)
/*---------------------------------------------------------------------------*\
 * Reads a signed 16 bit integer in little-endian, 2's complement format from
 *  the array of bytes pointed to by 'data_p'. 
 * If 'data_pp' is not null, it will set '*data_pp' to point past the integer
 *  read.
\*---------------------------------------------------------------------------*/
{
	unsigned int u16_le = ((unsigned int)data_p[1] << 8) | data_p[0];
	int s16_le;

#ifndef YS_ASSUME_2S_COMP
	if (u16_le & 0x8000U) {
		s16_le = (int)(u16_le - 0x8000U) - 0x7FFF - 1;
	} else
#endif
		s16_le = u16_le;

	if (data_pp != NULL) {
		*data_pp = data_p + 2;
	}

	return s16_le;
}

unsigned long
ys_read_u24_be(const unsigned char *data_p, const unsigned char **data_pp)
/*---------------------------------------------------------------------------*\
 * Reads an unsigned 24 bit integer in big-endian format from the array of 
 * bytes pointed to by 'data_p'. If 'data_pp' is not null, it will set
 * '*data_pp' to point past the integer read. 
\*---------------------------------------------------------------------------*/
{
	unsigned long u24_be = ((unsigned long)data_p[0] << 16) |
	    ((unsigned long)data_p[1] << 8) | data_p[2];

	if (data_pp != NULL) {
		*data_pp = data_p + 3;
	}

	return u24_be;
}

unsigned long
ys_read_u24_le(const unsigned char *data_p, const unsigned char **data_pp)
/*---------------------------------------------------------------------------*\
 * Reads an unsigned 24 bit integer in big-endian format from the array of 
 * bytes pointed to by 'data_p'. If 'data_pp' is not null, it will set
 * '*data_pp' to point past the integer read. 
\*---------------------------------------------------------------------------*/
{
	unsigned long u24_le = ((unsigned long)data_p[3] << 16) |
	    ((unsigned long)data_p[2] << 8) | data_p[0];

	if (data_pp != NULL) {
		*data_pp = data_p + 3;
	}

	return u24_le;
}

long ys_read_s24_be(const unsigned char *data_p, const unsigned char **data_pp)
/*---------------------------------------------------------------------------*\
 * Reads a signed 24 bit integer in big-endian, 2's complement format from
 *  the array of bytes pointed to by 'data_p'. 
 * If 'data_pp' is not null, it will set '*data_pp' to point past the integer
 *  read.
\*---------------------------------------------------------------------------*/
{
	unsigned long u24_be = ((unsigned long)data_p[0] << 16) |
	    ((unsigned long)data_p[1] << 8) | data_p[2];
	long s24_be;

#ifndef YS_ASSUME_2S_COMP
	if (u24_be & 0x800000UL) {
		s24_be = (long)(u24_be - 0x800000UL) - 0x800000;
	} else
#endif
		s24_be = u24_be;

	if (data_pp != NULL) {
		*data_pp = data_p + 3;
	}

	return s24_be;
}

long ys_read_s24_le(const unsigned char *data_p, const unsigned char **data_pp)
/*---------------------------------------------------------------------------*\
 * Reads a signed 24 bit integer in little-endian, 2's complement format from
 *  the array of bytes pointed to by 'data_p'. 
 * If 'data_pp' is not null, it will set '*data_pp' to point past the integer
 *  read.
\*---------------------------------------------------------------------------*/
{
	unsigned long u24_be = ((unsigned long)data_p[2] << 16) |
	    ((unsigned long)data_p[1] << 8) | data_p[0];
	long s24_be;

#ifndef YS_ASSUME_2S_COMP
	if (u24_be & 0x800000UL) {
		s24_be = (long)(u24_be - 0x800000UL) - 0x800000;
	} else
#endif
		s24_be = u24_be;

	if (data_pp != NULL) {
		*data_pp = data_p + 3;
	}

	return s24_be;
}

unsigned long
ys_read_u32_be(const unsigned char *data_p, const unsigned char **data_pp)
/*---------------------------------------------------------------------------*\
 * Reads an unsigned 32 bit integer in big-endian format from the array of 
 * bytes pointed to by 'data_p'. If 'data_pp' is not null, it will set
 * '*data_pp' to point past the integer read. 
\*---------------------------------------------------------------------------*/
{
	unsigned long u32_be = ((unsigned long)data_p[0] << 24) |
	    ((unsigned long)data_p[1] << 16) |
	    ((unsigned long)data_p[2] << 8) | data_p[3];

	if (data_pp != NULL) {
		*data_pp = data_p + 4;
	}

	return u32_be;
}

unsigned long
ys_read_u32_le(const unsigned char *data_p, const unsigned char **data_pp)
/*---------------------------------------------------------------------------*\
 * Reads an unsigned 32 bit integer in little-endian format from the array of 
 * bytes pointed to by 'data_p'. If 'data_pp' is not null, it will set
 * '*data_pp' to point past the integer read. 
\*---------------------------------------------------------------------------*/
{
	unsigned long u32_le = ((unsigned long)data_p[3] << 24) |
	    ((unsigned long)data_p[2] << 16) |
	    ((unsigned long)data_p[1] << 8) | data_p[0];

	if (data_pp != NULL) {
		*data_pp = data_p + 4;
	}

	return u32_le;
}

long ys_read_s32_be(const unsigned char *data_p, const unsigned char **data_pp)
/*---------------------------------------------------------------------------*\
 * Reads a signed 32 bit integer in big-endian, 2's complement format from
 *  the array of bytes pointed to by 'data_p'. 
 * If 'data_pp' is not null, it will set '*data_pp' to point past the integer
 *  read.
\*---------------------------------------------------------------------------*/
{
	unsigned long u32_be = ((unsigned long)data_p[0] << 24) |
	    ((unsigned long)data_p[1] << 16) |
	    ((unsigned long)data_p[2] << 8) | data_p[3];
	long s32_be;

#ifndef YS_ASSUME_2S_COMP
	if (u32_be & 0x80000000UL) {
		s32_be = (long)(u32_be - 0x80000000UL) - 0x7FFFFFFF - 1;
	} else
#endif
		s32_be = u32_be;

	if (data_pp != NULL) {
		*data_pp = data_p + 4;
	}

	return s32_be;
}

long ys_read_s32_le(const unsigned char *data_p, const unsigned char **data_pp)
/*---------------------------------------------------------------------------*\
 * Reads a signed 32 bit integer in little-endian, 2's complement format from
 *  the array of bytes pointed to by 'data_p'. 
 * If 'data_pp' is not null, it will set '*data_pp' to point past the integer
 *  read.
\*---------------------------------------------------------------------------*/
{
	unsigned long u32_le = ((unsigned long)data_p[3] << 24) |
	    ((unsigned long)data_p[2] << 16) |
	    ((unsigned long)data_p[1] << 8) | data_p[0];
	long s32_le;

#ifndef YS_ASSUME_2S_COMP
	if (u32_le & 0x80000000UL) {
		s32_le = (long)(u32_le - 0x80000000UL) - 0x7FFFFFFF - 1;
	} else
#endif
		s32_le = u32_le;

	if (data_pp != NULL) {
		*data_pp = data_p + 4;
	}

	return s32_le;
}

} // End of namespace Saga