summaryrefslogtreecommitdiff
path: root/src/libs/uio/getint.h
blob: ad6e81091fe97cd47b3dd7d00dc5f127c313fa7c (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
/*
 * Copyright (C) 2007  Serge van den Boom
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 * Nota bene: later versions of the GNU General Public License do not apply
 * to this program.
 *
 * 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
 *
 */

#ifndef LIBS_UIO_GETINT_H_
#define LIBS_UIO_GETINT_H_

/* All these functions return true on success, or false on failure */

#include "types.h"
#include "uioport.h"

static inline uio_bool
uio_getU8(uio_Stream *stream, uio_uint8 *result) {
	int val = uio_getc(stream);
	if (val == EOF)
		return false;

	*result = (uio_uint8) val;
	return true;
}

static inline uio_bool
uio_getS8(uio_Stream *stream, uio_sint8 *result) {
	int val = uio_getc(stream);
	if (val == EOF)
		return false;

	*result = (uio_sint8) val;
	return true;
}

static inline uio_bool
uio_getU16LE(uio_Stream *stream, uio_uint16 *result) {
	uio_uint8 buf[2];

	if (uio_fread(buf, sizeof buf, 1, stream) != 1)
		return false;

	*result = (buf[1] << 8) | buf[0];
	return true;
}

static inline uio_bool
uio_getU16BE(uio_Stream *stream, uio_uint16 *result) {
	uio_uint8 buf[2];

	if (uio_fread(buf, sizeof buf, 1, stream) != 1)
		return false;

	*result = (buf[0] << 8) | buf[1];
	return true;
}

static inline uio_bool
uio_getS16LE(uio_Stream *stream, uio_sint16 *result) {
	uio_uint8 buf[2];

	if (uio_fread(buf, sizeof buf, 1, stream) != 1)
		return false;

	*result = (uio_sint16) ((buf[1] << 8) | buf[0]);
	return true;
}

static inline uio_bool
uio_getS16BE(uio_Stream *stream, uio_sint16 *result) {
	uio_uint8 buf[2];

	if (uio_fread(buf, sizeof buf, 1, stream) != 1)
		return false;

	*result = (uio_sint16) ((buf[0] << 8) | buf[1]);
	return true;
}

static inline uio_bool
uio_getU32LE(uio_Stream *stream, uio_uint32 *result) {
	uio_uint8 buf[4];

	if (uio_fread(buf, sizeof buf, 1, stream) != 1)
		return false;

	*result = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
	return true;
}

static inline uio_bool
uio_getU32BE(uio_Stream *stream, uio_uint32 *result) {
	uio_uint8 buf[4];

	if (uio_fread(buf, sizeof buf, 1, stream) != 1)
		return false;

	*result = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
	return true;
}

static inline uio_bool
uio_getS32LE(uio_Stream *stream, uio_sint32 *result) {
	uio_uint8 buf[4];

	if (uio_fread(buf, sizeof buf, 1, stream) != 1)
		return false;

	*result = (uio_sint32) ((buf[3] << 24) | (buf[2] << 16) |
				(buf[1] << 8) | buf[0]);
	return true;
}

static inline uio_bool
uio_getS32BE(uio_Stream *stream, uio_sint32 *result) {
	uio_uint8 buf[4];

	if (uio_fread(buf, sizeof buf, 1, stream) != 1)
		return false;

	*result = (uio_sint32) ((buf[0] << 24) | (buf[1] << 16) |
				(buf[2] << 8) | buf[3]);
	return true;
}


#endif  /* LIBS_UIO_GETINT_H_ */