| 1 |
/* |
| 2 |
* bit-io.c: Buffered and bit oriented file I/O |
| 3 |
* |
| 4 |
* Written by: Ullrich Hafner |
| 5 |
* |
| 6 |
* This file is part of FIASCO (Fractal Image And Sequence COdec) |
| 7 |
* Copyright (C) 1994-2000 Ullrich Hafner |
| 8 |
*/ |
| 9 |
|
| 10 |
/* |
| 11 |
* $Date: 2000/06/14 20:49:37 $ |
| 12 |
* $Author: hafner $ |
| 13 |
* $Revision: 5.1 $ |
| 14 |
* $State: Exp $ |
| 15 |
*/ |
| 16 |
|
| 17 |
#define _DEFAULT_SOURCE 1 /* New name for SVID & BSD source defines */ |
| 18 |
#define _BSD_SOURCE 1 /* Make sure strdup() is in string.h */ |
| 19 |
#define _XOPEN_SOURCE 500 /* Make sure strdup() is in string.h */ |
| 20 |
|
| 21 |
#include "config.h" |
| 22 |
|
| 23 |
#include <string.h> |
| 24 |
#include <stdlib.h> |
| 25 |
|
| 26 |
#include "nstring.h" |
| 27 |
|
| 28 |
#include "macros.h" |
| 29 |
#include "types.h" |
| 30 |
#include "error.h" |
| 31 |
|
| 32 |
#include "misc.h" |
| 33 |
#include "bit-io.h" |
| 34 |
|
| 35 |
/***************************************************************************** |
| 36 |
|
| 37 |
local constants |
| 38 |
|
| 39 |
*****************************************************************************/ |
| 40 |
|
| 41 |
static const unsigned BUFFER_SIZE = 16350; |
| 42 |
|
| 43 |
static const unsigned mask[] = {0x0001, 0x0002, 0x0004, 0x0008, |
| 44 |
0x0010, 0x0020, 0x0040, 0x0080, |
| 45 |
0x0100, 0x0200, 0x0400, 0x0800, |
| 46 |
0x1000, 0x2000, 0x4000, 0x8000}; |
| 47 |
|
| 48 |
/***************************************************************************** |
| 49 |
|
| 50 |
public code |
| 51 |
|
| 52 |
*****************************************************************************/ |
| 53 |
|
| 54 |
FILE * |
| 55 |
open_file (const char *filename, const char *env_var, openmode_e mode) |
| 56 |
/* |
| 57 |
* Try to open file 'filename' with mode 'mode' (READ_ACCESS, WRITE_ACCESS). |
| 58 |
* Scan the current directory first and then cycle through the |
| 59 |
* path given in the environment variable 'env_var', if set. |
| 60 |
* |
| 61 |
* Return value: |
| 62 |
* Pointer to open file on success, else NULL. |
| 63 |
*/ |
| 64 |
{ |
| 65 |
char *path; /* current path */ |
| 66 |
FILE *fp; /* file pointer of I/O stream */ |
| 67 |
char *ext_filename = NULL; /* full path of file */ |
| 68 |
char *env_path = NULL; /* path given by 'env_var' */ |
| 69 |
char const * const PATH_SEP = " ;:,"; /* path separation characters */ |
| 70 |
char const * const DEFAULT_PATH = "."; /* default for output files */ |
| 71 |
const char * const read_mode = "rb"; |
| 72 |
const char * const write_mode = "wb"; |
| 73 |
|
| 74 |
|
| 75 |
assert (mode == READ_ACCESS || mode == WRITE_ACCESS); |
| 76 |
|
| 77 |
/* |
| 78 |
* First check for stdin or stdout |
| 79 |
*/ |
| 80 |
if (filename == NULL || streq (filename, "-")) |
| 81 |
{ |
| 82 |
if (mode == READ_ACCESS) |
| 83 |
return stdin; |
| 84 |
else |
| 85 |
return stdout; |
| 86 |
} |
| 87 |
|
| 88 |
/* |
| 89 |
* Try to open 'readonly' file in the current directory |
| 90 |
*/ |
| 91 |
if (mode == READ_ACCESS && (fp = fopen (filename, read_mode))) |
| 92 |
return fp; |
| 93 |
|
| 94 |
if (mode == WRITE_ACCESS && strchr (filename, '/')) /* contains path */ |
| 95 |
return fopen (filename, write_mode); |
| 96 |
|
| 97 |
/* |
| 98 |
* Get value of environment variable 'env_var', if set |
| 99 |
* else use DEFAULT_PATH ("./") |
| 100 |
*/ |
| 101 |
if (env_var != NULL) |
| 102 |
env_path = getenv (env_var); |
| 103 |
if (env_path == NULL) |
| 104 |
env_path = strdup (DEFAULT_PATH); |
| 105 |
else |
| 106 |
env_path = strdup (env_path); |
| 107 |
|
| 108 |
/* |
| 109 |
* Try to open file in the directory given by the environment |
| 110 |
* variable env_var - individual path components are separated by PATH_SEP |
| 111 |
*/ |
| 112 |
path = strtok (env_path, PATH_SEP); |
| 113 |
do |
| 114 |
{ |
| 115 |
if (ext_filename) |
| 116 |
Free (ext_filename); |
| 117 |
ext_filename = Calloc (strlen (path) + strlen (filename) + 2, |
| 118 |
sizeof (char)); |
| 119 |
strcpy (ext_filename, path); |
| 120 |
if (*(ext_filename + strlen (ext_filename) - 1) != '/') |
| 121 |
strcat (ext_filename, "/"); |
| 122 |
strcat (ext_filename, filename); |
| 123 |
fp = fopen (ext_filename, mode == READ_ACCESS ? read_mode : write_mode); |
| 124 |
} |
| 125 |
while (fp == NULL && (path = strtok (NULL, PATH_SEP)) != NULL); |
| 126 |
|
| 127 |
Free (env_path); |
| 128 |
|
| 129 |
return fp; |
| 130 |
} |
| 131 |
|
| 132 |
bitfile_t * |
| 133 |
open_bitfile (const char *filename, const char *env_var, openmode_e mode) |
| 134 |
/* |
| 135 |
* Bitfile constructor: |
| 136 |
* Try to open file 'filename' for buffered bit oriented access with mode |
| 137 |
* 'mode'. Scan the current directory first and then cycle through the path |
| 138 |
* given in the environment variable 'env_var', if set. |
| 139 |
* |
| 140 |
* Return value: |
| 141 |
* Pointer to open bitfile on success, |
| 142 |
* otherwise the program is terminated. |
| 143 |
*/ |
| 144 |
{ |
| 145 |
bitfile_t *bitfile = Calloc (1, sizeof (bitfile_t)); |
| 146 |
|
| 147 |
bitfile->file = open_file (filename, env_var, mode); |
| 148 |
|
| 149 |
if (bitfile->file == NULL) |
| 150 |
file_error (filename); |
| 151 |
|
| 152 |
if (mode == READ_ACCESS) |
| 153 |
{ |
| 154 |
bitfile->bytepos = 0; |
| 155 |
bitfile->bitpos = 0; |
| 156 |
bitfile->mode = mode; |
| 157 |
bitfile->filename = filename ? strdup (filename) : strdup ("(stdin)"); |
| 158 |
} |
| 159 |
else if (mode == WRITE_ACCESS) |
| 160 |
{ |
| 161 |
bitfile->bytepos = BUFFER_SIZE - 1; |
| 162 |
bitfile->bitpos = 8; |
| 163 |
bitfile->mode = mode; |
| 164 |
bitfile->filename = filename ? strdup (filename) : strdup ("(stdout)"); |
| 165 |
} |
| 166 |
else |
| 167 |
error ("Unknown file access mode '%d'.", mode); |
| 168 |
|
| 169 |
bitfile->bits_processed = 0; |
| 170 |
bitfile->buffer = Calloc (BUFFER_SIZE, sizeof (byte_t)); |
| 171 |
bitfile->ptr = bitfile->buffer; |
| 172 |
|
| 173 |
return bitfile; |
| 174 |
} |
| 175 |
|
| 176 |
bool_t |
| 177 |
get_bit (bitfile_t *bitfile) |
| 178 |
/* |
| 179 |
* Get one bit from the given stream 'bitfile'. |
| 180 |
* |
| 181 |
* Return value: |
| 182 |
* 1 H bit |
| 183 |
* 0 L bit |
| 184 |
* |
| 185 |
* Side effects: |
| 186 |
* Buffer of 'bitfile' is modified accordingly. |
| 187 |
*/ |
| 188 |
{ |
| 189 |
assert (bitfile); |
| 190 |
|
| 191 |
if (!bitfile->bitpos--) /* use next byte ? */ |
| 192 |
{ |
| 193 |
bitfile->ptr++; |
| 194 |
if (!bitfile->bytepos--) /* no more bytes left in the buffer? */ |
| 195 |
{ |
| 196 |
/* |
| 197 |
* Fill buffer with new data |
| 198 |
*/ |
| 199 |
int bytes = fread (bitfile->buffer, sizeof (byte_t), |
| 200 |
BUFFER_SIZE, bitfile->file) - 1; |
| 201 |
if (bytes < 0) /* Error or EOF */ |
| 202 |
error ("Can't read next bit from bitfile %s.", bitfile->filename); |
| 203 |
else |
| 204 |
bitfile->bytepos = bytes; |
| 205 |
|
| 206 |
bitfile->ptr = bitfile->buffer; |
| 207 |
} |
| 208 |
bitfile->bitpos = 7; |
| 209 |
} |
| 210 |
|
| 211 |
bitfile->bits_processed++; |
| 212 |
|
| 213 |
return *bitfile->ptr & mask [bitfile->bitpos] ? 1 : 0; |
| 214 |
} |
| 215 |
|
| 216 |
unsigned int |
| 217 |
get_bits (bitfile_t *bitfile, unsigned bits) |
| 218 |
/* |
| 219 |
* Get #'bits' bits from the given stream 'bitfile'. |
| 220 |
* |
| 221 |
* Return value: |
| 222 |
* composed integer value |
| 223 |
* |
| 224 |
* Side effects: |
| 225 |
* Buffer of 'bitfile' is modified. |
| 226 |
*/ |
| 227 |
{ |
| 228 |
unsigned value = 0; /* input value */ |
| 229 |
|
| 230 |
while (bits--) |
| 231 |
value = (unsigned) (value << 1) | get_bit (bitfile); |
| 232 |
|
| 233 |
return value; |
| 234 |
} |
| 235 |
|
| 236 |
void |
| 237 |
put_bit (bitfile_t *bitfile, unsigned value) |
| 238 |
/* |
| 239 |
* Put the bit 'value' to the bitfile buffer. |
| 240 |
* The buffer is written to the file 'bitfile->file' if the number of |
| 241 |
* buffer bytes exceeds 'BUFFER_SIZE'. |
| 242 |
* |
| 243 |
* No return value. |
| 244 |
* |
| 245 |
* Side effects: |
| 246 |
* Buffer of 'bitfile' is modified. |
| 247 |
*/ |
| 248 |
{ |
| 249 |
assert (bitfile); |
| 250 |
|
| 251 |
if (!bitfile->bitpos--) /* use next byte ? */ |
| 252 |
{ |
| 253 |
bitfile->ptr++; |
| 254 |
if (!bitfile->bytepos--) /* no more bytes left ? */ |
| 255 |
{ |
| 256 |
/* |
| 257 |
* Write buffer to disk and fill buffer with zeros |
| 258 |
*/ |
| 259 |
if (fwrite (bitfile->buffer, sizeof (byte_t), |
| 260 |
BUFFER_SIZE, bitfile->file) != BUFFER_SIZE) |
| 261 |
error ("Can't write next bit of bitfile %s!", bitfile->filename); |
| 262 |
memset (bitfile->buffer, 0, BUFFER_SIZE); |
| 263 |
bitfile->bytepos = BUFFER_SIZE - 1; |
| 264 |
bitfile->ptr = bitfile->buffer; |
| 265 |
} |
| 266 |
bitfile->bitpos = 7; |
| 267 |
} |
| 268 |
|
| 269 |
if (value) |
| 270 |
*bitfile->ptr |= mask [bitfile->bitpos]; |
| 271 |
|
| 272 |
bitfile->bits_processed++; |
| 273 |
} |
| 274 |
|
| 275 |
void |
| 276 |
put_bits (bitfile_t *bitfile, unsigned value, unsigned bits) |
| 277 |
/* |
| 278 |
* Put #'bits' bits of integer 'value' to the bitfile buffer 'bitfile'. |
| 279 |
* |
| 280 |
* No return value. |
| 281 |
* |
| 282 |
* Side effects: |
| 283 |
* Buffer of 'bitfile' is modified. |
| 284 |
*/ |
| 285 |
{ |
| 286 |
while (bits--) |
| 287 |
put_bit (bitfile, value & mask [bits]); |
| 288 |
} |
| 289 |
|
| 290 |
void |
| 291 |
close_bitfile (bitfile_t *bitfile) |
| 292 |
/* |
| 293 |
* Bitfile destructor: |
| 294 |
* Close 'bitfile', if 'bitfile->mode' == WRITE_ACCESS write bit buffer |
| 295 |
* to disk. |
| 296 |
* |
| 297 |
* No return value. |
| 298 |
* |
| 299 |
* Side effects: |
| 300 |
* Structure 'bitfile' is discarded. |
| 301 |
*/ |
| 302 |
{ |
| 303 |
assert (bitfile); |
| 304 |
|
| 305 |
if (bitfile->mode == WRITE_ACCESS) |
| 306 |
{ |
| 307 |
unsigned bytes = fwrite (bitfile->buffer, sizeof (byte_t), |
| 308 |
BUFFER_SIZE - bitfile->bytepos, bitfile->file); |
| 309 |
if (bytes != BUFFER_SIZE - bitfile->bytepos) |
| 310 |
error ("Can't write remaining %d bytes of bitfile " |
| 311 |
"(only %d bytes written)!", |
| 312 |
BUFFER_SIZE - bitfile->bytepos, bytes); |
| 313 |
} |
| 314 |
fclose (bitfile->file); |
| 315 |
Free (bitfile->buffer); |
| 316 |
Free (bitfile->filename); |
| 317 |
Free (bitfile); |
| 318 |
} |
| 319 |
|
| 320 |
unsigned |
| 321 |
bits_processed (const bitfile_t *bitfile) |
| 322 |
/* |
| 323 |
* Return value: |
| 324 |
* Number of bits processed up to now |
| 325 |
*/ |
| 326 |
{ |
| 327 |
return bitfile->bits_processed; |
| 328 |
} |