-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBStream.h
More file actions
63 lines (50 loc) · 1.36 KB
/
BStream.h
File metadata and controls
63 lines (50 loc) · 1.36 KB
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
#ifndef LH_C_BSTREAM_H
#define LH_C_BSTREAM_H
//-----------------------------------------------
// DEFINITIONS
//-----------------------------------------------
typedef struct {
u8* dest;
u32 size;
u32 stream;
u32 streamSize;
} BStream;
static void initStream(BStream* b, u8* dest) {
b->dest = dest;
b->size = 0;
b->stream = 0;
b->streamSize = 0;
}
static void writeStream(BStream* b, u32 data, u32 width ) {
u32 stream = b->stream;
u32 size = b->size;
u32 streamSize = b->streamSize;
u32 mask = (1 << width) - 1;
if (width == 0) return;
stream = (stream << width) | ( data & mask );
streamSize += width;
for (u32 i = 0; i < streamSize / 8; i++) {
b->dest[ size++ ] = (u8)( stream >> (streamSize - (i + 1 ) * 8 ) );
}
streamSize %= 8;
b->stream = stream;
b->size = size;
b->streamSize = streamSize;
}
static void closeStream(BStream* b, u32 align) {
u32 stream = b->stream;
u32 size = b->size;
u32 streamSize = b->streamSize;
if (streamSize > 0 ) {
stream <<= 8 - streamSize;
if (b->streamSize != 0) {
b->dest[size++] = (u8)(stream);
}
}
while (size % align) {
b->dest[size++] = 0;
}
b->size = size;
b->streamSize = 0;
}
#endif //LH_C_BSTREAM_H