| 1 |
/* \ |
| 2 |
* $Id: bitio.c,v 1.5 1992/11/24 19:36:46 dws Exp dws $ |
| 3 |
* |
| 4 |
* bitio.c - bitstream I/O |
| 5 |
* |
| 6 |
* Works for (sizeof(unsigned long)-1)*8 bits. |
| 7 |
* |
| 8 |
* Copyright (C) 1992 by David W. Sanderson. |
| 9 |
* |
| 10 |
* Permission to use, copy, modify, and distribute this software and its |
| 11 |
* documentation for any purpose and without fee is hereby granted, |
| 12 |
* provided that the above copyright notice appear in all copies and |
| 13 |
* that both that copyright notice and this permission notice appear |
| 14 |
* in supporting documentation. This software is provided "as is" |
| 15 |
* without express or implied warranty. |
| 16 |
* |
| 17 |
* $Log: bitio.c,v $ |
| 18 |
* Revision 1.5 1992/11/24 19:36:46 dws |
| 19 |
* Added copyright. |
| 20 |
* |
| 21 |
* Revision 1.4 1992/11/17 03:37:50 dws |
| 22 |
* updated comment |
| 23 |
* |
| 24 |
* Revision 1.3 1992/11/10 23:15:16 dws |
| 25 |
* Removed superfluous code. |
| 26 |
* |
| 27 |
* Revision 1.2 1992/11/10 23:11:22 dws |
| 28 |
* Generalized to handle more than one bitstream at once. |
| 29 |
* |
| 30 |
* Revision 1.1 1992/11/10 18:33:21 dws |
| 31 |
* Initial revision |
| 32 |
* |
| 33 |
\*/ |
| 34 |
|
| 35 |
#include <string.h> |
| 36 |
|
| 37 |
#include "bitio.h" |
| 38 |
|
| 39 |
struct bitstream { |
| 40 |
FILE * f; /* bytestream */ |
| 41 |
unsigned long int bitbuf; /* bit buffer */ |
| 42 |
int nbitbuf; /* number of bits in 'bitbuf' */ |
| 43 |
char mode; /* read or write */ |
| 44 |
}; |
| 45 |
|
| 46 |
#define Mask(n) ((1<<(n))-1) |
| 47 |
|
| 48 |
#define BitPut(b,ul,n) ((b)->bitbuf = (((b)->bitbuf<<(n)) \ |
| 49 |
|((ul)&Mask(n))), \ |
| 50 |
(b)->nbitbuf += (n)) |
| 51 |
|
| 52 |
#define BitGet(b,n) (((b)->bitbuf>>((b)->nbitbuf-=(n))) & Mask(n)) |
| 53 |
|
| 54 |
/* |
| 55 |
* pm_bitinit() - allocate and return a struct bitstream * for the |
| 56 |
* given FILE*. |
| 57 |
* |
| 58 |
* mode must be one of "r" or "w", according to whether you will be |
| 59 |
* reading from or writing to the struct bitstream *. |
| 60 |
* |
| 61 |
* Returns 0 on error. |
| 62 |
*/ |
| 63 |
|
| 64 |
struct bitstream * |
| 65 |
pm_bitinit(FILE * const f, |
| 66 |
const char * const mode) { |
| 67 |
|
| 68 |
struct bitstream * ans; |
| 69 |
|
| 70 |
if (!f || !mode) |
| 71 |
ans = NULL; |
| 72 |
else if (strcmp(mode, "r") != 0 && strcmp(mode, "w") != 0) |
| 73 |
ans = NULL; |
| 74 |
else { |
| 75 |
ans = (struct bitstream *)calloc(1, sizeof(struct bitstream)); |
| 76 |
if (ans != NULL) { |
| 77 |
ans->f = f; |
| 78 |
ans->mode = *mode; |
| 79 |
} |
| 80 |
} |
| 81 |
return ans; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
/* |
| 87 |
* pm_bitfini() - deallocate the given struct bitstream *. |
| 88 |
* |
| 89 |
* You must call this after you are done with the struct bitstream *. |
| 90 |
* |
| 91 |
* It may flush some bits left in the buffer. |
| 92 |
* |
| 93 |
* Returns the number of bytes written, -1 on error. |
| 94 |
*/ |
| 95 |
|
| 96 |
int |
| 97 |
pm_bitfini(struct bitstream * const b) { |
| 98 |
|
| 99 |
int nbyte = 0; |
| 100 |
|
| 101 |
if(!b) |
| 102 |
return -1; |
| 103 |
|
| 104 |
/* flush the output */ |
| 105 |
if(b->mode == 'w') |
| 106 |
{ |
| 107 |
/* flush the bits */ |
| 108 |
if (b->nbitbuf < 0 || b->nbitbuf >= 8) |
| 109 |
{ |
| 110 |
/* pm_bitwrite() didn't work */ |
| 111 |
return -1; |
| 112 |
} |
| 113 |
|
| 114 |
/* |
| 115 |
* If we get to here, nbitbuf is 0..7 |
| 116 |
*/ |
| 117 |
if(b->nbitbuf) |
| 118 |
{ |
| 119 |
char c; |
| 120 |
|
| 121 |
BitPut(b, 0, (long int)8-(b->nbitbuf)); |
| 122 |
c = (char) BitGet(b, (long int)8); |
| 123 |
if(putc(c, b->f) == EOF) |
| 124 |
{ |
| 125 |
return -1; |
| 126 |
} |
| 127 |
nbyte++; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
free(b); |
| 132 |
return nbyte; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
/* |
| 138 |
* pm_bitread() - read the next nbits into *val from the given file. |
| 139 |
* |
| 140 |
* The last pm_bitread() must be followed by a call to pm_bitfini(). |
| 141 |
* |
| 142 |
* Returns the number of bytes read, -1 on error. |
| 143 |
*/ |
| 144 |
|
| 145 |
int |
| 146 |
pm_bitread(struct bitstream * const b, |
| 147 |
unsigned long int const nbits, |
| 148 |
unsigned long int * const val) { |
| 149 |
|
| 150 |
int nbyte = 0; |
| 151 |
int c; |
| 152 |
|
| 153 |
if(!b) |
| 154 |
return -1; |
| 155 |
|
| 156 |
while (b->nbitbuf < nbits) |
| 157 |
{ |
| 158 |
if((c = getc(b->f)) == EOF) |
| 159 |
{ |
| 160 |
return -1; |
| 161 |
} |
| 162 |
nbyte++; |
| 163 |
|
| 164 |
BitPut(b, c, (long int) 8); |
| 165 |
} |
| 166 |
|
| 167 |
*val = BitGet(b, nbits); |
| 168 |
return nbyte; |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
/* |
| 174 |
* pm_bitwrite() - write the low nbits of val to the given file. |
| 175 |
* |
| 176 |
* The last pm_bitwrite() must be followed by a call to pm_bitfini(). |
| 177 |
* |
| 178 |
* Returns the number of bytes written, -1 on error. |
| 179 |
*/ |
| 180 |
|
| 181 |
int |
| 182 |
pm_bitwrite(struct bitstream * const b, |
| 183 |
unsigned long int const nbits, |
| 184 |
unsigned long int const val) { |
| 185 |
|
| 186 |
int nbyte = 0; |
| 187 |
char c; |
| 188 |
|
| 189 |
if(!b) |
| 190 |
return -1; |
| 191 |
|
| 192 |
BitPut(b, val, nbits); |
| 193 |
|
| 194 |
while (b->nbitbuf >= 8) { |
| 195 |
c = (char) BitGet(b, (long int)8); |
| 196 |
|
| 197 |
if(putc(c, b->f) == EOF) { |
| 198 |
return -1; |
| 199 |
} |
| 200 |
nbyte++; |
| 201 |
} |
| 202 |
|
| 203 |
return nbyte; |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|