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
|
/***********************************************************HeaderBegin*******
*
* File: bitOut.c
*
* Author: K.S.
* Created: 5-Nov-97
*
* Description: source code of routines for buffering and bit-io
*
* Notes:
*
* Modified:
*
***********************************************************HeaderEnd*********/
#include <stdlib.h>
#include "defs.h"
#include "structs.h"
#include "Util.h"
#include "bitOut.h"
/***********************************************************CommentBegin******
*****************************************************************************
*
* -- AllocBitstr -- Alloc bitstream with buffer but without filepointer
*
* Author: T.W.
*
* Created: 23-July-96
*
* Purpose: Creates a new 'Bitstr' with a buffer size of 'n' bytes
* and sets the filepointer to NULL.
*
* Arguments in: int n size of buffer in byte (min. sizeof(Int)).
*
* Arguments in/out: -
*
* Arguments out: -
*
* Return values: Bitstr * pointer to allocated bitstream structure.
* NULL in case of error.
*
* Example: b = AllocBitstr(1024);
*
* Side effects: -
*
* Description: The memory of a 'Bitstr' structure is allocated.
* No file is opened!
* If a file shall be associated with the bitstream, use
* FopenBitstr!
* This function is included in FopenBitstr.
*
* See also: FopenBitstr
*
* Modified: -
*
*****************************************************************************/
Bitstr *AllocBitstr(int n)
/***********************************************************CommentEnd********/
{
Bitstr *b = (Bitstr *) malloc(sizeof(Bitstr));
n = MAX(n, sizeof(int));
b->size = n * 8;
b->ind = 0;
b->actualSize = 0;
b->b = (Byte *) malloc(n * sizeof(Byte));
b->fp = NULL;
return(b);
}
/***********************************************************CommentBegin******
*****************************************************************************
*
* -- FreeBitstr -- Free a bitstream structure (file is n o t closed!)
*
* Author: T.W.
*
* Created: 23-July-96
*
* Purpose: Frees a 'Bitstr' and sets the pointer to NULL.
*
* Arguments in: -
*
* Arguments in/out: Bitstr **b_p pointer to bitstream structure
*
* Arguments out: -
*
* Return values: -
*
* Example: FreeBitstr(&b);
*
* Side effects: -
*
* Description: The memory of a 'Bitstr' structure is freed.
* No file closing is done!
* If a file is associated with the bitstream, use
* FcloseBitstr!
* This function is included in FcloseBitstr.
*
* See also: FcloseBitstr
*
* Modified: -
*
*****************************************************************************/
void FreeBitstr(Bitstr **b_p)
/***********************************************************CommentEnd********/
{
free((*b_p)->b);
free(*b_p);
*b_p = NULL;
}
/***********************************************************CommentBegin******
*****************************************************************************
*
* -- CheckOutBuffer -- Write bytes from buffer to file
*
* Author: T.W.
*
* Created: 23-July-96
*
* Purpose: Writes the bytes from the partially filled buffer 'b'
* to the file addressed by 'b->fp'. The last byte is
* filled with zeros.
*
* Arguments in: Bitstr *b bitstream structure
*
* Arguments in/out: -
*
* Arguments out: -
*
* Return values: FALSE (0) on 'EOF' or 'b->fp == NULL'
* TRUE (1) on success
*
* Example: CheckOutBuffer(b);
*
* Side effects: Content of 'b->b' is written to file 'b->fp'.
*
* Description: The buffer is flashed to the file. The last byte is
* filled with zeros.
* The buffer indicater 'b->ind' is reset.
* This function is called automatically if a file is
* associated with the bitstream and you use
* PutBit or PutCodeword.
*
* See also: CheckInBuffer, PutBit, PutCodeword
*
* Modified: -
*
*****************************************************************************/
int CheckOutBuffer(Bitstr *b)
/***********************************************************CommentEnd********/
{
int i;
int r = b->ind;
if (b->fp == NULL)
return (FALSE);
if(b->size!=r) {
int rem = b->ind % 8;
int quot = b->ind / 8;
if(rem > 0) {
for(i=rem;i<8;++i) {
UNSETBIT(b->b[quot],7-i);
}
r = (quot+1)*8;
}
}
b->ind = 0;
if (fwrite(&b->b[0],1,r/8,b->fp) < r/8)
return (FALSE);
if (fflush(b->fp) == EOF)
return (FALSE);
return (TRUE);
}
/***********************************************************CommentBegin******
*****************************************************************************
*
* -- FopenBitstr -- New bitstream with buffer and filepointer
*
* Author: T.W.
*
* Created: 23-July-96
*
* Purpose: Creates a new 'Bitstr' with a buffer size of 'n' bytes
* and opens file 'name' with mode 'mode' (see fopen()).
*
* Arguments in: int n buffer size in bytes
* char *name file name of bitstream
* char *mode "w" (write), "a" (append) or
* "r" (read) (see fopen()!)
*
* Arguments in/out: -
*
* Arguments out: -
*
* Return values: Bitstr * allocated bitstream structure
*
* Example: b = FopenBitstr(1024, "bit.stream", "r");
*
* Side effects: If mode is "w", the file 'name' is created or,
* if it exists, its contents are deleted.
*
* Description: -
*
* See also: FcloseBitstr
*
* Modified:
*
*****************************************************************************/
Bitstr *FopenBitstr(int n, char *name, char *mode)
/***********************************************************CommentEnd********/
{
Bitstr *b = AllocBitstr(n);
b->fp = fopen(name,mode);
return(b);
}
/***********************************************************CommentBegin******
*****************************************************************************
*
* -- FcloseBitstr -- Free bitstream buffer and close filepointer
*
* Author: T.W.
*
* Created: 23-July-96
*
* Purpose: Closes the file associated with the bitstream
* (see fclose()) and frees the bitstream structure.
*
* Arguments in: -
*
* Arguments in/out: Bitstr **b_p pointer to the bitstream structure
*
* Arguments out: -
*
* Return values: -
*
* Example: FcloseBitstr(&b);
*
* Side effects: -
*
* Description: The file associated with the bitstream is closed
* (see fclose()) and the bitstream structure is freed.
* The pointer to the bitstream structure is set to NULL.
*
* See also: FopenBitstr
*
* Modified: -
*
*****************************************************************************/
void FcloseBitstr(Bitstr **b_p)
/***********************************************************CommentEnd********/
{
CheckOutBuffer(*b_p);
if ((*b_p)->fp != NULL)
fclose((*b_p)->fp);
FreeBitstr(b_p);
}
/***********************************************************CommentBegin******
*****************************************************************************
*
* -- BitsToByteAlignment -- Return remaining bits in current byte
*
* Author: Niko Faerber
*
* Created: 08-March-97
*
* Purpose: Calculate the number of bits to complete the current byte
*
* Arguments in: Bitstr *bs : bitstream
*
* Arguments in/out: -
*
* Arguments out: -
*
* Return values: Int n : number of bits to complete the current byte
*
* Example: n = BitsToByteAlignment(bs);
*
* Side effects: -
*
* Description: -
*
* See also: -
*
* Modified: -
*
*****************************************************************************/
int BitsToByteAlignment(Bitstr *bs)
/***********************************************************CommentEnd********/
{
int n;
n = 8-(bs->ind)%8;
if(n==8) n=0;
return n;
}
/***********************************************************CommentBegin******
*****************************************************************************
*
* -- ByteAlign0X -- Fill the remaining incomplete byte of the stream with 0
*
* Author: K.S.
*
* Created: 31-Aug-97
*
* Purpose: Sets the stream indicator to the first bit of the
* next byte. The skipped bits are set to zero.
*
* Arguments in: Bitstr *b bitstream structure
*
* Arguments in/out: -
*
* Arguments out: -
*
* Return values: Number of bits to byte alignment that are sent.
*
* Example: ByteAlign0X(b);
*
* Side effects: -
*
* Description: Zeros are written to the bitstream 'b' until the actual
* byte is full.
*
* See also: ByteAlign0, ByteAlign, BitsToByteAlignment
*
* Modified: -
*
*****************************************************************************/
int ByteAlign0X(Bitstr *b)
/***********************************************************CommentEnd********/
{
int n;
n = BitsToByteAlignment(b);
PutNumberX(0, n, b);
return(n);
}
/***********************************************************CommentBegin******
*****************************************************************************
*
* -- PutBitX -- Put bit to bitstream without checking buffersize
*
* Author: K.S.
*
* Created: 23-Aug-97
*
* Purpose: Writes a bit to the bit-stream 'b' at the current
* pointer stage 'b->ind' of the bit-stream.
* It is not checked if the buffersize is exceeded.
*
* Arguments in: int bit bitvalue (0 or 1)
* (if != 0 --> 1 is emitted)
*
* Arguments in/out: Bitstr *b bitstream structure
*
* Arguments out: -
*
* Return values: -
*
* Example: PutBitX(1, b);
*
* Side effects: -
*
* Description: If the buffer is filled.
*
* See also: PutBit, GetBitX
*
* Modified: -
*
*****************************************************************************/
void PutBitX(int bit, Bitstr *b)
/***********************************************************CommentEnd********/
{
int quot = b->ind >> 3;
int bitP = 7 - (b->ind & 0x7);
if(bit) {
SETBIT(b->b[quot], bitP);
} else {
UNSETBIT(b->b[quot], bitP);
}
b->ind++;
}
/***********************************************************CommentBegin******
*****************************************************************************
*
* -- PutCodewordX -- Add codeword to actual buffer pointer position (w/o file)
*
* Author: K.S.
*
* Created: 27-Aug-97
*
* Purpose: Writes the codeword 'cw' to the bit-stream 'b' at
* the current pointer stage 'b->ind' of the bit-stream.
* Buffersize and file are not regarded (-> 'PutBitX()').
*
* Arguments in: char *cv codeword as a string of 0 and 1.
*
* Arguments in/out: Bitstr *b bitstream structure
*
* Arguments out: -
*
* Return values: -
*
* Example: PutCodewordX("01001000", b);
*
* Side effects: -
*
* Description: -
*
* See also: PutCodeword, GetCodewordX, GetCodewordIndexX, PutBitX
*
* Modified: -
*
*****************************************************************************/
void PutCodewordX(char *cw, Bitstr *b)
/***********************************************************CommentEnd********/
{
int l = strlen(cw);
int i;
for(i=0;i<l;++i) {
PutBitX(cw[i]-'0', b);
}
}
/***********************************************************CommentBegin******
*****************************************************************************
*
* -- PutNumberX -- Add some bits of a number to buffer w/o regarding buf-size
*
* Author: K.S.
*
* Created: 27-Aug-97
*
* Purpose: Writes the 'n' least significant bits of the integer
* 'num' to the bit-stream 'b' at
* the current pointer stage 'b->ind' of the bit-stream.
* Buffersize and file are not regarded.
*
* Arguments in: int num number from which the 'n' least
* significant bits are written to 'b'.
* The bits are put in the bitstream
* in descending order
* (from left to right).
* int n number of bits that are written.
* (max. sizeof(Int) || sizeof(Long) - 7).
*
* Arguments in/out: Bitstr *b bitstream structure
*
* Arguments out: -
*
* Return values: -
*
* Example: PutNumberX(19, 6, b);
* puts the bits '010011' to 'b'.
*
* Side effects: -
*
* Description: -
*
* See also: PutNumber, GetNumberX, PutCodewordX, PutBitX
*
* Modified: -
*
*****************************************************************************/
void PutNumberX(int num, int n, Bitstr *b)
/***********************************************************CommentEnd********/
{
if (n > 0) {
int i;
long numl = (long)num;
int rem = b->ind & 0x7;
long mask = (0x1 << (8 - rem)) - 1;
int ind = b->ind + n - 1;
int byteInd = ind >> 3;
int bitPEnd = 7 - (ind & 0x7);
numl <<= bitPEnd;
for (i = rem + n - 8; i > 0; i -= 8) {
b->b[byteInd--] = (Byte)numl;
numl >>= 8;
}
b->b[byteInd] &= ~mask;
b->b[byteInd] |= (numl & mask);
b->ind += n;
}
}
|