-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBitFile.cpp
166 lines (132 loc) · 3.17 KB
/
BitFile.cpp
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "BitFile.hpp"
int BitFile::fileSize() {
fseek(fp, 0L, SEEK_END);
int size = ftell(fp);
rewind(fp);
return size;
}
int BitFile::openBitFile(const char filename[], const char mode[]) {
// assume mode is either "rb" or "wb"
fp = fopen(filename, mode);
if (!(fp)) {
return 1; // failed
}
buf = 0;
pos = 0;
if (strncmp(mode, "wb", 2) == 0)
bWriting = true;
else
bWriting = false;
return 0; // 0 means okay
}
int BitFile::closeBitFile() {
if (pos > 0 && bWriting) {
writeFlush();
}
fclose(fp);
return 0;
}
// bits are written from lsb to msb
int BitFile::writeBit(uint8_t bitval) {
if (bitval == 1) {
bitval = bitval << pos;
buf = buf | bitval;
}
if (pos == BIT_END) {
fwrite(&buf, 1, 1, fp);
pos = 0;
buf = 0;
} else {
pos = pos + 1;
}
return 0;
}
// bit 0 is written first as the lsb in the output byte
// bit count-1 is written last and will be the high order bit of those written
// by this routine eg. bits = 0x06, count = 4 Then the bits will be 0110
int BitFile::writeBits(uint32_t bits, int count) {
int i;
for (i = 0; i < count; i++) {
if (bits & 0x01) {
writeBit(1);
} else {
writeBit(0);
}
bits = bits >> 1;
}
return 0;
}
/*used to force write out contents when encoding is done*/
/*This must be called before closing the output file during compression*/
int BitFile::writeFlush() {
if (pos > 0 && bWriting) {
fwrite(&buf, 1, 1, fp);
}
pos = 0;
buf = 0;
fflush(fp);
return 0;
}
// You need to call writeFlush() before calling the following two functions if
// you call writeBits() or writeBit() previously. If you want to write a byte
// (or 32-bits ) immediately following writing a bunch of bits, you should use
// writeBits() instead of writeByte(), write32Bits().
int BitFile::writeByte(uint8_t byteVal) {
int cnt;
cnt = fwrite(&byteVal, sizeof(uint8_t), 1, fp);
if (cnt == 1)
return 0; // good
else
return 1; // bad
}
// write a 32-bit value to file
int BitFile::write32Bits(uint32_t wordVal) {
int cnt;
cnt = fwrite(&wordVal, sizeof(uint32_t), 1, fp);
if (cnt == sizeof(uint32_t))
return 0; // good
else
return 1; // bad
}
/*read next bit and store as 0 or 1 in return value*/
int BitFile::readBit() { // returns either 0x00 or 0x01 , unless something
// goes wrong which returns -1.
uint8_t bitval = 0;
int cnt;
if (pos == 0) {
cnt = fread(&buf, 1, 1, fp);
if (cnt != 1) {
return -1; // to denote something went wrong
}
}
bitval = buf & 0x01; // get bit value
buf = buf >> 1; // move to next bit
// update position
if (pos == BIT_END) {
pos = 0;
} else {
pos = pos + 1;
}
return bitval;
}
// read an byte to file
int BitFile::readByte(uint8_t *byte) {
int cnt;
cnt = fread(byte, 1, 1, fp);
if (cnt == 1)
return 0; // good
else
return 1; // bad
}
// read a 32 bit value from file
int BitFile::read32Bits(uint32_t *wordVal) {
int cnt;
cnt = fread(wordVal, sizeof(uint32_t), 1, fp);
if (cnt == sizeof(uint32_t))
return 0; // good
else
return 1; // bad
}