-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathu_bstream.c
203 lines (171 loc) · 3.99 KB
/
u_bstream.c
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
/*
* Copyright (c) 2023 Manuel Pietschmann.
* All rights reserved.
*
* The software in this package is published under the terms of the BSD
* style license a copy of which has been included with this distribution in
* the LICENSE.txt file.
*
*/
#include "u_bstream.h"
void U_bstream_init(U_BStream *bs, void *data, unsigned long size)
{
bs->data = (unsigned char*)data;
bs->pos = 0;
bs->size = size;
bs->status = U_BSTREAM_OK;
}
static int U_bstream_verify_write(U_BStream *bs, unsigned long size)
{
if (bs->status != U_BSTREAM_OK)
return 0;
if (!bs->data)
{
bs->status = U_BSTREAM_NOT_INITIALISED;
return 0;
}
if ((bs->pos + size) > bs->size)
{
bs->status = U_BSTREAM_WRITE_PAST_END;
return 0;
}
return 1;
}
static int U_bstream_verify_read(U_BStream *bs, unsigned long size)
{
if (bs->status != U_BSTREAM_OK)
return 0;
if (!bs->data)
{
bs->status = U_BSTREAM_NOT_INITIALISED;
return 0;
}
if ((bs->pos + size) > bs->size)
{
bs->status = U_BSTREAM_READ_PAST_END;
return 0;
}
return 1;
}
void U_bstream_put_u8(U_BStream *bs, unsigned char v)
{
if (U_bstream_verify_write(bs, 1))
{
bs->data[bs->pos++] = v;
}
}
void U_bstream_put_u16_le(U_BStream *bs, unsigned short v)
{
if (U_bstream_verify_write(bs, 2))
{
bs->data[bs->pos++] = (v >> 0) & 0xFF;
bs->data[bs->pos++] = (v >> 8) & 0xFF;
}
}
void U_bstream_put_s16_le(U_BStream *bs, signed short v)
{
U_bstream_put_u16_le(bs, (unsigned short)v);
}
void U_bstream_put_u32_le(U_BStream *bs, unsigned long v)
{
if (U_bstream_verify_write(bs, 4))
{
bs->data[bs->pos++] = (v >> 0) & 0xFF;
bs->data[bs->pos++] = (v >> 8) & 0xFF;
bs->data[bs->pos++] = (v >> 16) & 0xFF;
bs->data[bs->pos++] = (v >> 24) & 0xFF;
}
}
void U_bstream_put_s32_le(U_BStream *bs, signed long v)
{
U_bstream_put_u32_le(bs, (unsigned long)v);
}
unsigned char U_bstream_get_u8(U_BStream *bs)
{
unsigned char result;
result = 0;
if (U_bstream_verify_read(bs, 1))
{
result = bs->data[bs->pos];
bs->pos++;
}
return result;
}
unsigned short U_bstream_get_u16_le(U_BStream *bs)
{
unsigned short result;
result = 0;
if (U_bstream_verify_read(bs, 2))
{
result = bs->data[bs->pos + 1];
result <<= 8;
result |= bs->data[bs->pos];
bs->pos += 2;
}
return result;
}
signed short U_bstream_get_s16_le(U_BStream *bs)
{
unsigned short u16;
u16 = U_bstream_get_u16_le(bs);
return (signed short)u16;
}
unsigned short U_bstream_get_u16_be(U_BStream *bs)
{
unsigned short result;
result = 0;
if (U_bstream_verify_read(bs, 2))
{
result = bs->data[bs->pos];
result <<= 8;
result |= bs->data[bs->pos + 1];
bs->pos += 2;
}
return result;
}
unsigned long U_bstream_get_u32_le(U_BStream *bs)
{
unsigned long result;
result = 0;
if (U_bstream_verify_read(bs, 4))
{
result = bs->data[bs->pos + 3];
result <<= 8;
result |= bs->data[bs->pos + 2];
result <<= 8;
result |= bs->data[bs->pos + 1];
result <<= 8;
result |= bs->data[bs->pos + 0];
bs->pos += 4;
}
return result;
}
signed long U_bstream_get_s32_le(U_BStream *bs)
{
#ifdef __LP64__
unsigned int u32;
u32 = (unsigned int)U_bstream_get_u32_le(bs);
return (signed int)u32;
#else
unsigned long u32;
u32 = U_bstream_get_u32_le(bs);
return (signed long)u32;
#endif
}
unsigned long U_bstream_get_u32_be(U_BStream *bs)
{
unsigned long result;
result = 0;
if (U_bstream_verify_read(bs, 4))
{
result = bs->data[bs->pos];
result <<= 8;
result |= bs->data[bs->pos + 1];
result <<= 8;
result |= bs->data[bs->pos + 2];
result <<= 8;
result |= bs->data[bs->pos + 3];
bs->pos += 4;
}
return result;
}