-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbitfile.c
638 lines (543 loc) · 20.3 KB
/
bitfile.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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
/***************************************************************************
* Bit Stream File Implementation
*
* File : bitfile.c
* Purpose : This file implements a simple library of I/O functions for
* files that contain data in sizes that aren't integral bytes.
* An attempt was made to make the functions in this library
* analogous to functions provided to manipulate byte streams.
* The functions contained in this library were created with
* compression algorithms in mind, but may be suited to other
* applications.
* Author : Michael Dipperstein
* Date : January 9, 2004
*
****************************************************************************
*
* Bitfile: Bit stream File I/O Routines
* Copyright (C) 2004-2014 by Michael Dipperstein (mdipper@alumni.cs.ucsb.edu)
*
* This file is part of the bit file library.
*
* The bit file library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* The bit file library 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 Lesser
* General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
***************************************************************************/
/***************************************************************************
* INCLUDED FILES
***************************************************************************/
#include <stdlib.h>
#include <errno.h>
#include "bitfile.h"
/***************************************************************************
* TYPE DEFINITIONS
***************************************************************************/
/***************************************************************************
* type to point to the kind of functions that put/get bits from/to numerical
* data types (short, int, long, ...)
* parameters: file pointer, data structure, number of bits, sizeof data.
***************************************************************************/
typedef int (*num_func_t)(bit_file_t*, void*, const unsigned int, const size_t);
struct bit_file_t
{
FILE *fp; /* file pointer used by stdio functions */
unsigned char bitBuffer; /* bits waiting to be read/written */
unsigned char bitCount; /* number of bits in bitBuffer */
BF_MODES mode; /* open for read, write, or append */
};
typedef enum
{
BF_UNKNOWN_ENDIAN,
BF_LITTLE_ENDIAN,
BF_BIG_ENDIAN
} endian_t;
/* union used to test for endianess */
typedef union
{
unsigned long word;
unsigned char bytes[sizeof(unsigned long)];
} endian_test_t;
/***************************************************************************
* PROTOTYPES
***************************************************************************/
/***************************************************************************
* FUNCTIONS
***************************************************************************/
/***************************************************************************
* Function : BitFileOpen
* Description: This function opens a bit file for reading, writing,
* or appending. If successful, a bit_file_t data
* structure will be allocated and a pointer to the
* structure will be returned.
* Parameters : fileName - NULL terminated string containing the name of
* the file to be opened.
* mode - The mode of the file to be opened
* Effects : The specified file will be opened and file structure will
* be allocated.
* Returned : Pointer to the bit_file_t structure for the bit file
* opened, or NULL on failure. errno will be set for all
* failure cases.
***************************************************************************/
bit_file_t *BitFileOpen(const char *fileName, const BF_MODES mode)
{
const char modes[3][3] = {"rb", "wb", "ab"}; /* binary modes for fopen */
bit_file_t *bf;
bf = (bit_file_t *)malloc(sizeof(bit_file_t));
if (bf == NULL)
{
/* malloc failed */
errno = ENOMEM;
}
else
{
bf->fp = fopen(fileName, modes[mode]);
if (bf->fp == NULL)
{
/* fopen failed */
free(bf);
bf = NULL;
}
else
{
/* fopen succeeded fill in remaining bf data */
bf->bitBuffer = 0;
bf->bitCount = 0;
bf->mode = mode;
/* I don't care about endianness! */
/***************************************************************
* TO DO: Consider using the last byte in a file to indicate
* the number of bits in the previous byte that actually have
* data. If I do that, I'll need special handling of files
* opened with a mode of BF_APPEND.
***************************************************************/
}
}
return (bf);
}
/***************************************************************************
* Function : MakeBitFile
* Description: This function naively wraps a standard file in a
* bit_file_t structure. ANSI-C doesn't support file status
* functions commonly found in other C variants, so the
* caller must be passed as a parameter.
* Parameters : stream - pointer to the standard file being wrapped.
* mode - The mode of the file being wrapped.
* Effects : A bit_file_t structure will be created for the stream
* passed as a parameter.
* Returned : Pointer to the bit_file_t structure for the bit file
* or NULL on failure. errno will be set for all failure
* cases.
***************************************************************************/
bit_file_t *MakeBitFile(FILE *stream, const BF_MODES mode)
{
bit_file_t *bf;
if (stream == NULL)
{
/* can't wrapper empty steam */
errno = EBADF;
bf = NULL;
}
else
{
bf = (bit_file_t *)malloc(sizeof(bit_file_t));
if (bf == NULL)
{
/* malloc failed */
errno = ENOMEM;
}
else
{
/* set structure data */
bf->fp = stream;
bf->bitBuffer = 0;
bf->bitCount = 0;
bf->mode = mode;
/* Repeat: I don't care about endianness! */
}
}
return (bf);
}
/***************************************************************************
* Function : BitFileClose
* Description: This function closes a bit file and frees all associated
* data.
* Parameters : stream - pointer to bit file stream being closed
* Effects : The specified file will be closed and the file structure
* will be freed.
* Returned : 0 for success or EOF for failure.
***************************************************************************/
int BitFileClose(bit_file_t *stream)
{
int returnValue = 0;
if (stream == NULL)
{
return(EOF);
}
if ((stream->mode == BF_WRITE) || (stream->mode == BF_APPEND))
{
/* write out any unwritten bits */
if (stream->bitCount != 0)
{
(stream->bitBuffer) <<= 8 - (stream->bitCount);
fputc(stream->bitBuffer, stream->fp); /* handle error? */
}
}
/***********************************************************************
* TO DO: Consider writing an additional byte indicating the number of
* valid bits (bitCount) in the previous byte.
***********************************************************************/
/* close file */
returnValue = fclose(stream->fp);
/* free memory allocated for bit file */
free(stream);
return(returnValue);
}
/***************************************************************************
* Function : BitFileToFILE
* Description: This function flushes and frees the bitfile structure,
* returning a pointer to a stdio file.
* Parameters : stream - pointer to bit file stream being closed
* Effects : The specified bitfile will be made usable as a stdio
* FILE.
* Returned : Pointer to FILE. NULL for failure.
***************************************************************************/
FILE *BitFileToFILE(bit_file_t *stream)
{
FILE *fp = NULL;
if (stream == NULL)
{
return(NULL);
}
if ((stream->mode == BF_WRITE) || (stream->mode == BF_APPEND))
{
/* write out any unwritten bits */
if (stream->bitCount != 0)
{
(stream->bitBuffer) <<= 8 - (stream->bitCount);
fputc(stream->bitBuffer, stream->fp); /* handle error? */
}
}
/***********************************************************************
* TO DO: Consider writing an additional byte indicating the number of
* valid bits (bitCount) in the previous byte.
***********************************************************************/
/* close file */
fp = stream->fp;
/* free memory allocated for bit file */
free(stream);
return(fp);
}
/***************************************************************************
* Function : BitFileByteAlign
* Description: This function aligns the bitfile to the nearest byte. For
* output files, this means writing out the bit buffer with
* extra bits set to 0. For input files, this means flushing
* the bit buffer.
* Parameters : stream - pointer to bit file stream to align
* Effects : Flushes out the bit buffer.
* Returned : EOF if stream is NULL or write fails. Writes return the
* byte aligned contents of the bit buffer. Reads returns
* the unaligned contents of the bit buffer.
***************************************************************************/
int BitFileByteAlign(bit_file_t *stream)
{
int returnValue;
if (stream == NULL)
{
return(EOF);
}
returnValue = stream->bitBuffer;
if ((stream->mode == BF_WRITE) || (stream->mode == BF_APPEND))
{
/* write out any unwritten bits */
if (stream->bitCount != 0)
{
(stream->bitBuffer) <<= 8 - (stream->bitCount);
fputc(stream->bitBuffer, stream->fp); /* handle error? */
}
}
stream->bitBuffer = 0;
stream->bitCount = 0;
return (returnValue);
}
/***************************************************************************
* Function : BitFileFlushOutput
* Description: This function flushes the output bit buffer. This means
* left justifying any pending bits, and filling spare bits
* with the fill value.
* Parameters : stream - pointer to bit file stream to align
* onesFill - non-zero if spare bits are filled with ones
* Effects : Flushes out the bit buffer, filling spare bits with ones
* or zeros.
* Returned : EOF if stream is NULL or not writeable. Otherwise, the
* bit buffer value written. -1 if no data was written.
***************************************************************************/
int BitFileFlushOutput(bit_file_t *stream, const unsigned char onesFill)
{
int returnValue;
if (stream == NULL)
{
return(EOF);
}
returnValue = -1;
/* write out any unwritten bits */
if (stream->bitCount != 0)
{
stream->bitBuffer <<= (8 - stream->bitCount);
if (onesFill)
{
stream->bitBuffer |= (0xFF >> stream->bitCount);
}
returnValue = fputc(stream->bitBuffer, stream->fp);
}
stream->bitBuffer = 0;
stream->bitCount = 0;
return (returnValue);
}
/***************************************************************************
* Function : BitFileGetChar
* Description: This function returns the next byte from the file passed as
* a parameter.
* Parameters : stream - pointer to bit file stream to read from
* Effects : Reads next byte from file and updates buffer accordingly.
* Returned : EOF if a whole byte cannot be obtained. Otherwise,
* the character read.
***************************************************************************/
int BitFileGetChar(bit_file_t *stream)
{
int returnValue;
unsigned char tmp;
if (stream == NULL)
{
return(EOF);
}
returnValue = fgetc(stream->fp);
if (stream->bitCount == 0)
{
/* we can just get byte from file */
return returnValue;
}
/* we have some buffered bits to return too */
if (returnValue != EOF)
{
/* figure out what to return */
tmp = ((unsigned char)returnValue) >> (stream->bitCount);
tmp |= ((stream->bitBuffer) << (8 - (stream->bitCount)));
/* put remaining in buffer. count shouldn't change. */
stream->bitBuffer = returnValue;
returnValue = tmp;
}
return returnValue;
}
/***************************************************************************
* Function : BitFilePutChar
* Description: This function writes the byte passed as a parameter to the
* file passed a parameter.
* Parameters : c - the character to be written
* stream - pointer to bit file stream to write to
* Effects : Writes a byte to the file and updates buffer accordingly.
* Returned : On success, the character written, otherwise EOF.
***************************************************************************/
int BitFilePutChar(const int c, bit_file_t *stream)
{
unsigned char tmp;
/* fprintf(stderr,"PutChar: 0x%x\n", c); */
if (stream == NULL)
{
return(EOF);
}
if (stream->bitCount == 0)
{
/* we can just put byte from file */
return fputc(c, stream->fp);
}
/* figure out what to write */
tmp = ((unsigned char)c) >> (stream->bitCount);
tmp = tmp | ((stream->bitBuffer) << (8 - stream->bitCount));
if (fputc(tmp, stream->fp) != EOF)
{
/* put remaining in buffer. count shouldn't change. */
stream->bitBuffer = c;
}
else
{
return EOF;
}
return tmp;
}
/***************************************************************************
* Function : BitFileGetBit
* Description: This function returns the next bit from the file passed as
* a parameter. The bit value returned is the msb in the
* bit buffer.
* Parameters : stream - pointer to bit file stream to read from
* Effects : Reads next bit from bit buffer. If the buffer is empty,
* a new byte will be read from the file.
* Returned : 0 if bit == 0, 1 if bit == 1, and EOF if operation fails.
***************************************************************************/
int BitFileGetBit(bit_file_t *stream)
{
int returnValue;
if (stream == NULL)
{
return(EOF);
}
if (stream->bitCount == 0)
{
/* buffer is empty, read another character */
if ((returnValue = fgetc(stream->fp)) == EOF)
{
return EOF;
}
else
{
stream->bitCount = 8;
stream->bitBuffer = returnValue;
}
}
/* bit to return is msb in buffer */
stream->bitCount--;
returnValue = (stream->bitBuffer) >> (stream->bitCount);
return (returnValue & 0x01);
}
/***************************************************************************
* Function : BitFilePutBit
* Description: This function writes the bit passed as a parameter to the
* file passed a parameter.
* Parameters : c - the bit value to be written
* stream - pointer to bit file stream to write to
* Effects : Writes a bit to the bit buffer. If the buffer has a byte,
* the buffer is written to the file and cleared.
* Returned : On success, the bit value written, otherwise EOF.
***************************************************************************/
int BitFilePutBit(const int c, bit_file_t *stream)
{
int returnValue = c;
/* fprintf(stderr,"PutBit: %d\n", c); */
if (stream == NULL)
{
return(EOF);
}
stream->bitCount++;
stream->bitBuffer <<= 1;
if (c != 0)
{
stream->bitBuffer |= 1;
}
/* write bit buffer if we have 8 bits */
if (stream->bitCount == 8)
{
if (fputc(stream->bitBuffer, stream->fp) == EOF)
{
returnValue = EOF;
}
/* reset buffer */
stream->bitCount = 0;
stream->bitBuffer = 0;
}
return returnValue;
}
/***************************************************************************
* Function : BitFileGetBits
* Description: This function reads the specified number of bits from the
* file passed as a parameter and writes them to the
* requested memory location (msb to lsb).
* Parameters : stream - pointer to bit file stream to read from
* bits - address to store bits read
* count - number of bits to read
* Effects : Reads bits from the bit buffer and file stream. The bit
* buffer will be modified as necessary.
* Returned : EOF for failure, otherwise the number of bits read. If
* an EOF is reached before all the bits are read, bits
* will contain every bit through the last complete byte.
***************************************************************************/
int BitFileGetBits(bit_file_t *stream, unsigned int *bits, const unsigned int count)
{
int offset, remaining, returnValue;
if ((stream == NULL) || (bits == NULL))
{
return(EOF);
}
offset = 0;
remaining = count;
*bits = 0;
/* read whole bytes */
while (remaining >= 8)
{
returnValue = BitFileGetChar(stream);
if (returnValue == EOF)
{
return EOF;
}
*bits = (*bits << 8) | (unsigned char)returnValue;
remaining -= 8;
offset++;
}
while (remaining > 0)
{
returnValue = BitFileGetBit(stream);
if (returnValue == EOF)
{
return EOF;
}
*bits = (*bits << 1) | (returnValue & 0x01);
remaining--;
}
return count;
}
/***************************************************************************
* Function : BitFilePutBits
* Description: This function writes the specified number of bits from the
* memory location passed as a parameter to the file passed
* as a parameter. Bits are written msb to lsb.
* Parameters : stream - pointer to bit file stream to write to
* bits - pointer to bits to write
* count - number of bits to write
* Effects : Writes bits to the bit buffer and file stream. The bit
* buffer will be modified as necessary.
* Returned : EOF for failure, otherwise the number of bits written. If
* an error occurs after a partial write, the partially
* written bits will not be unwritten.
***************************************************************************/
int BitFilePutBits(bit_file_t *stream, unsigned int bits, const unsigned int count)
{
unsigned int tmp;
int offset, remaining, returnValue;
if (stream == NULL)
{
return(EOF);
}
remaining = count;
offset = (remaining + 1)/8;
tmp = (bits >> (offset * 8)) & 0xFF;
while (remaining % 8 != 0)
{
returnValue = BitFilePutBit((tmp >> ((remaining % 8) - 1)) & 1, stream);
if (returnValue == EOF)
{
return EOF;
}
remaining--;
}
/* write whole bytes */
while (remaining >= 8)
{
returnValue = BitFilePutChar(bits >> ((remaining/8 ) - 1) & 0xFF, stream);
if (returnValue == EOF)
{
return EOF;
}
remaining -= 8;
}
return count;
}