bitio.c
上传用户:ykyjsl
上传日期:2022-01-30
资源大小:145k
文件大小:3k
源码类别:

压缩解压

开发平台:

C/C++

  1. /******************************************************************************
  2. File:           bitio.c
  3. Authors:        John Carpinelli   (johnfc@ecr.mu.oz.au)          
  4.                 Wayne Salamonsen  (wbs@mundil.cs.mu.oz.au)       
  5.                 Lang Stuiver      (langs@cs.mu.oz.au)
  6. Purpose:        Data compression using a revised arithmetic coding method.
  7. Based on:       A. Moffat, R. Neal, I.H. Witten, "Arithmetic Coding Revisted",
  8.                 Proc. IEEE Data Compression Conference, Snowbird, Utah, 
  9.                 March 1995.
  10.                 Low-Precision Arithmetic Coding Implementation by
  11.                 Radford M. Neal
  12. Copyright 1996 Lang Stuiver, All Rights Reserved.
  13. These programs are supplied free of charge for research purposes only,
  14. and may not sold or incorporated into any commercial product.  There is
  15. ABSOLUTELY NO WARRANTY of any sort, nor any undertaking that they are
  16. fit for ANY PURPOSE WHATSOEVER.  Use them at your own risk.  If you do
  17. happen to find a bug, or have modifications to suggest, please report
  18. the same to Alistair Moffat, alistair@cs.mu.oz.au.  The copyright
  19. notice above and this statement of conditions must remain an integral
  20. part of each and every copy made of these files.
  21. ******************************************************************************
  22.  Input output module.  Inputs and outputs at bit, character, and
  23.  fread/fwrite level.
  24.  $Log: bitio.c,v $
  25.  Revision 1.1  1996/08/07 01:34:11  langs
  26.  Initial revision
  27. ******************************************************************************/
  28. #include <stdio.h>
  29. #include "bitio.h"
  30. #ifdef RCSID
  31. static char
  32.     rcsid[] = "$Id: bitio.c,v 1.1 1996/08/07 01:34:11 langs Exp $"; 
  33. #endif
  34. /* 
  35.  * The following variables are supposedly local, but actually global so they
  36.  * can be referenced by macro
  37.  */
  38. unsigned int _bytes_input = 0;
  39. unsigned int _bytes_output = 0;
  40. int _in_buffer; /* I/O buffer */
  41. unsigned char _in_bit_ptr = 0; /* bits left in buffer */
  42. int _in_garbage; /* bytes read beyond eof */
  43. int _out_buffer; /* I/O buffer */
  44. int _out_bits_to_go; /* bits to fill buffer */
  45. #ifndef FAST_BITIO
  46. int _bitio_tmp; /* Used by some of the */
  47. #endif /* bitio.h macros */
  48. /*
  49.  *
  50.  * initialize the bit output function
  51.  *
  52.  */
  53. void startoutputtingbits(void)
  54. {
  55.     _out_buffer = 0;
  56.     _out_bits_to_go = BYTE_SIZE;
  57. }
  58. /*
  59.  *
  60.  * start the bit input function
  61.  *
  62.  */
  63. void startinputtingbits(void)
  64. {
  65.     _in_garbage = 0; /* Number of bytes read past end of file */
  66.     _in_bit_ptr = 0; /* No valid bits yet in input buffer */
  67. }
  68. /*
  69.  *
  70.  * complete outputting bits
  71.  *
  72.  */
  73. void doneoutputtingbits(void)
  74. {
  75.     if (_out_bits_to_go != BYTE_SIZE)
  76. OUTPUT_BYTE(_out_buffer << _out_bits_to_go);
  77.     _out_bits_to_go = BYTE_SIZE;
  78. }
  79. /*
  80.  *
  81.  * complete inputting bits
  82.  *
  83.  */
  84. void doneinputtingbits(void)
  85. {
  86.       _in_bit_ptr = 0;       /* "Wipe" buffer (in case more input follows) */
  87. }
  88. /*
  89.  * Number of bytes read with bitio functions.
  90.  */
  91. int bitio_bytes_in(void)
  92. {
  93.     return _bytes_input;
  94. }
  95. /*
  96.  * Number of bytes written with bitio functions.
  97.  */
  98. int bitio_bytes_out(void)
  99. {
  100.     return _bytes_output;
  101. }
  102. /*
  103.  * Return bit to input stream.
  104.  * Only guaranteed to be able to backup by 1 bit.
  105.  */
  106. void unget_bit(int bit)
  107. {
  108.   _in_bit_ptr <<= 1;
  109.   if (_in_bit_ptr == 0)
  110. _in_bit_ptr = 1;
  111.   _in_buffer = _in_buffer & (_in_bit_ptr - 1); /* Only keep bits still to */
  112. /* to be read.    */
  113.   if (bit)
  114. _in_buffer |= _in_bit_ptr;  /* Replace bit    */
  115. }