aboutsummaryrefslogtreecommitdiff
path: root/saga/ys_binread.cpp
blob: 021a0a8436abc851056aa26e7bc47bceecb91fed (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
/* 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;
}

// 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 ys_read_u8(const unsigned char *data_p, const unsigned char **data_pp) {
	unsigned int u8 = *data_p;

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

	return u8;
}

// 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. 
int ys_read_s8(const unsigned char *data_p, const unsigned char **data_pp) {
	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;
}

// 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 ys_read_u16_be(const unsigned char *data_p, const unsigned char **data_pp) {
	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;
}

// 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 ys_read_u16_le(const unsigned char *data_p, const unsigned char **data_pp) {
	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;
}

// 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.
int ys_read_s16_be(const unsigned char *data_p, const unsigned char **data_pp) {
	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;
}

// 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.
int ys_read_s16_le(const unsigned char *data_p, const unsigned char **data_pp) {
	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;
}

// 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 ys_read_u24_le(const unsigned char *data_p, const unsigned char **data_pp) {
	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;
}

// 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 ys_read_u32_be(const unsigned char *data_p, const unsigned char **data_pp) {
	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;
}

// 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 ys_read_u32_le(const unsigned char *data_p, const unsigned char **data_pp) {
	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;
}

} // End of namespace Saga