-
Notifications
You must be signed in to change notification settings - Fork 1
/
bitfile.cpp
executable file
·321 lines (270 loc) · 6.8 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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/***
bitfile.cpp
Bitfile implementation: bitwise I/O for files and memory buffers.
Copyright (c) 2014-2022 C. M. Street
***/
/*** helper function: read a byte from the source file or memory buffer ***/
static int __bitfile_getc(bitfile* bf) {
if (BF_IS_RAM(bf)) {
if (bf->buf < bf->bufe) {
++bf->buf;
return *(unsigned char *)(bf->buf - 1);
}
return(-1); /* end-of-buffer */
}
// else
return(fgetc(bf->f));
}
/*** helper function: write a byte to the destination file or memory buffer from the bitfile's accumulator ***/
static void __bitfile_putc(bitfile* bf) {
if (BF_IS_RAM(bf)) {
if (bf->buf < bf->bufe) {
*bf->buf = (char)(bf->acc);
++bf->buf;
}
// TODO: we currently have silent failure when a RAM bitfile reaches the end of its buffer. No security risk, but potentially annoying.
} else {
fputc(bf->acc, bf->f);
}
}
/*** Opens a disk file to read/write using bit I/O. ***/
void bitfile_open(bitfile* bf, const char* fname, bool is_write) {
bf->f = fopen(fname, (is_write ? "wb" : "rb"));
bf->buf = NULL;
bf->bufe = NULL;
bf->bufs = NULL;
bf->acc = 0UL;
bf->acc_c = 0UL;
bf->opt = (is_write ? BITFILE_IS_WRITE : BITFILE_NONE);
}
/*** Opens a bitfile that will read or write to a memory buffer. ***/
void bitfile_open_mem(bitfile* bf, char* buf, u32 bufsize, bool is_write) {
bf->f = NULL;
bf->buf = bf->bufs = buf;
bf->bufe = buf + bufsize;
bf->acc = 0UL;
bf->acc_c = 0UL;
bf->opt = (is_write ? BITFILE_IS_WRITE : BITFILE_NONE) | BITFILE_IS_RAM;
}
/*** Closes the bitfile. ***/
void bitfile_close(bitfile* bf) {
/* flush the last byte if needed */
if (BF_IS_WRITE(bf) && bf->acc_c != 0UL) {
__bitfile_putc(bf);
bf->acc = 0UL;
bf->acc_c = 0UL;
}
if (!BF_IS_RAM(bf))
fclose(bf->f);
}
/*** Writes a single bit to the bitfile. ***/
void bitfile_writebit(bitfile* bf, bool on) {
if (!BF_IS_WRITE(bf))
return;
bf->acc += bf->acc;
if (on)
bf->acc |= 1UL;
bf->acc_c++;
if (8 == bf->acc_c) {
__bitfile_putc(bf);
bf->acc = 0UL;
bf->acc_c = 0UL;
}
}
/*** Writes a single byte to the bitfile. ***/
void bitfile_writebyte(bitfile* bf, u32 byte) {
int t = 128UL;
while (t) {
bitfile_writebit(bf, ((byte & t) != 0));
t >>= 1;
}
}
/*** Writes a 32-bit unsigned integer to the bitfile. ***/
void bitfile_write32(bitfile* bf, u32 val) {
bitfile_writebyte(bf, val & 0xff);
val >>= 8;
bitfile_writebyte(bf, val & 0xff);
val >>= 8;
bitfile_writebyte(bf, val & 0xff);
val >>= 8;
bitfile_writebyte(bf, val & 0xff);
}
/*** Writes a 64-bit unsigned integer to the bitfile. ***/
extern void bitfile_write64(bitfile* bf, u64 val) {
bitfile_writebyte(bf, val & 0xff);
val >>= 8;
bitfile_writebyte(bf, val & 0xff);
val >>= 8;
bitfile_writebyte(bf, val & 0xff);
val >>= 8;
bitfile_writebyte(bf, val & 0xff);
val >>= 8;
bitfile_writebyte(bf, val & 0xff);
val >>= 8;
bitfile_writebyte(bf, val & 0xff);
val >>= 8;
bitfile_writebyte(bf, val & 0xff);
val >>= 8;
bitfile_writebyte(bf, val & 0xff);
}
/*** Reads a bit from the bitfile. Returns -1 on EOF. ***/
int bitfile_readbit(bitfile* bf) {
int ret;
if (0UL == bf->acc_c)
{
bf->acc = __bitfile_getc(bf);
if (bf->acc < 0) /* EOF */
return(bf->acc);
bf->acc_c = 128UL;
}
ret = ((bf->acc & bf->acc_c) ? 1 : 0);
bf->acc_c >>= 1;
return(ret);
}
/*** Reads an (unsigned) byte from the bitfile. Returns -1 on EOF. ***/
int bitfile_readbyte(bitfile* bf) {
int t = 128UL;
int ret = 0UL;
while (t) {
int b;
b = bitfile_readbit(bf);
if (b < 0)
return(b);
if (b)
ret |= t;
t >>= 1;
}
return(ret);
}
/*** Reads a 32 bit integer from the bitfile. Sets the is_eof flag to true on EOF, if is_eof is non-NULL. ***/
u32 bitfile_read32(bitfile* bf, bool* is_eof) {
int r;
u32 ret = 0UL;
if (not_null(is_eof))
*is_eof = false;
r = bitfile_readbyte(bf);
if (unlikely(r < 0)) {
LEOF: if (not_null(is_eof))
*is_eof = true;
return(-1);
}
ret = r;
r = bitfile_readbyte(bf);
if (unlikely(r < 0)) goto LEOF;
ret |= (r << 8);
r = bitfile_readbyte(bf);
if (unlikely(r < 0)) goto LEOF;
ret |= (r << 16);
r = bitfile_readbyte(bf);
if (unlikely(r < 0)) goto LEOF;
ret |= (r << 24);
return(ret);
}
/*** Reads a 64 bit integer from the bitfile. Sets the is_eof flag to true on EOF, if is_eof is non-NULL. ***/
u64 bitfile_read64(bitfile* bf, bool* is_eof) {
u64 ret = 0UL;
u64 r;
if (not_null(is_eof))
*is_eof = false;
r = bitfile_readbyte(bf);
if (unlikely(r < 0)) {
LEOF: if (not_null(is_eof))
*is_eof = true;
return(-1);
}
ret = r;
r = bitfile_readbyte(bf);
if (unlikely(r < 0)) goto LEOF;
ret |= (r << 8);
r = bitfile_readbyte(bf);
if (unlikely(r < 0)) goto LEOF;
ret |= (r << 16);
r = bitfile_readbyte(bf);
if (unlikely(r < 0)) goto LEOF;
ret |= (r << 24);
r = bitfile_readbyte(bf);
if (unlikely(r < 0)) goto LEOF;
ret |= (r << 32);
r = bitfile_readbyte(bf);
if (unlikely(r < 0)) goto LEOF;
ret |= (r << 40);
r = bitfile_readbyte(bf);
if (unlikely(r < 0)) goto LEOF;
ret |= (r << 48);
r = bitfile_readbyte(bf);
if (unlikely(r < 0)) goto LEOF;
ret |= (r << 56);
return(ret);
}
/*** Class wrapper for bitfile ***/
BitFile::BitFile() {
this->init_ = false;
}
BitFile::BitFile(const char* filename, bool is_write) {
this->open(filename, is_write);
}
BitFile::~BitFile() {
this->close();
}
void BitFile::open(const char* fname, bool is_write) {
bitfile_open(&bf_, fname, is_write);
init_ = true;
}
void BitFile::open_mem(char* buf, u32 bufsize, bool is_write) {
bitfile_open_mem(&bf_, buf, bufsize, is_write);
init_ = true;
}
void BitFile::close(void) {
if (!init_)
return;
bitfile_close(&bf_);
init_ = false;
}
void BitFile::write(char byte) {
bitfile_writebyte(&bf_, byte);
}
void BitFile::write(u32 ui32) {
bitfile_write32(&bf_, ui32);
}
void BitFile::write(u64 ui64) {
bitfile_write64(&bf_, ui64);
}
void BitFile::writebit(bool on) {
bitfile_writebit(&bf_, on);
}
void BitFile::writebyte(u32 byte) {
bitfile_writebyte(&bf_, byte);
}
void BitFile::write32(u32 val) {
bitfile_write32(&bf_, val);
}
void BitFile::write64(u64 val) {
bitfile_write64(&bf_, val);
}
int BitFile::readbit(void) {
if (!init_)
return(-1);
return bitfile_readbit(&bf_);
}
int BitFile::readbyte(void) {
if (!init_)
return(-1);
return bitfile_readbyte(&bf_);
}
u32 BitFile::read32(bool* is_eof) {
if (!init_) {
if (not_null(is_eof))
*is_eof = true;
return(0UL);
}
return bitfile_read32(&bf_, is_eof);
}
u64 BitFile::read64(bool* is_eof) {
if (!init_) {
if (not_null(is_eof))
*is_eof = true;
return(0UL);
}
return bitfile_read64(&bf_, is_eof);
}
/*** end bitfile.cpp ***/