BITIO.C 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
  3. SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
  4. END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
  5. ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
  6. IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
  7. SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
  8. FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
  9. CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
  10. AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
  11. COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
  12. */
  13. /*
  14. * $Source: f:/miner/source/pslib/rcs/bitio.c $
  15. * $Revision: 1.9 $
  16. * $Author: yuan $
  17. * $Date: 1993/10/22 17:51:09 $
  18. *
  19. * Contains all the necessary bit routines.
  20. * The file handling has been optimized and the pacifier has been removed.
  21. *
  22. * $Log: bitio.c $
  23. * Revision 1.9 1993/10/22 17:51:09 yuan
  24. * No major revisions
  25. *
  26. * Revision 1.8 1993/10/18 17:59:55 yuan
  27. * Fixed memory alloc errors
  28. *
  29. * Revision 1.7 1993/09/21 17:22:17 yuan
  30. * *** empty log message ***
  31. *
  32. * Revision 1.6 1993/09/21 17:16:19 yuan
  33. * cleaning up
  34. *
  35. * Revision 1.5 1993/09/20 12:25:39 yuan
  36. * ReadFile, WriteFile, AppendFile removed to readfile.lib
  37. *
  38. * Revision 1.4 1993/09/14 13:06:25 yuan
  39. * CloseOutputBitFile no longer frees the buffer.
  40. *
  41. * Revision 1.3 1993/09/09 17:42:02 yuan
  42. * tab added to ERROR messages
  43. *
  44. * Revision 1.2 1993/09/09 12:38:28 yuan
  45. * WriteFile and AppendFile fixed.
  46. *
  47. * Revision 1.1 1993/09/08 16:14:04 yuan
  48. * Initial revision
  49. *
  50. * Revision 1.3 1993/07/24 19:04:52 yuan
  51. * *** empty log message ***
  52. *
  53. * Revision 1.2 1993/07/22 11:25:35 yuan
  54. * No change
  55. *
  56. * Revision 1.1 1993/07/21 15:28:13 matt
  57. * Initial revision
  58. *
  59. *
  60. */
  61. #pragma off (unreferenced)
  62. static char rcsid[] = "$Id: bitio.c 1.9 1993/10/22 17:51:09 yuan Exp $";
  63. #pragma on (unreferenced)
  64. #include <stdio.h>
  65. #include <stdlib.h>
  66. #include <fcntl.h>
  67. #include <io.h>
  68. #include <sys\types.h>
  69. #include <sys\stat.h>
  70. #include "library.h"
  71. #include "mem.h"
  72. //#include "mem2.h"
  73. #define PACIFIER_COUNT 2047
  74. BIT_BUF *OpenOutputBitBuf( ) {
  75. BIT_BUF *bit_buf;
  76. //MALLOC( bit_buf, BIT_BUF, 1 );//Compile hack again -KRB
  77. bit_buf = (BIT_BUF *)malloc(1*sizeof(BIT_BUF));
  78. if ( bit_buf == NULL )
  79. return( bit_buf );
  80. bit_buf->current_byte = 0;
  81. bit_buf->rack = 0;
  82. bit_buf->mask = 0x80;
  83. bit_buf->pacifier_counter = 0;
  84. return( bit_buf );
  85. }
  86. BIT_BUF *OpenInputBitBuf( ubyte *buffer ) {
  87. BIT_BUF *bit_buf;
  88. //bit_buf = (BIT_BUF *) calloc( 1, sizeof( BIT_BUF ) );
  89. // MALLOC(bit_buf, BIT_BUF, 1);//Compile hack again -KRB
  90. bit_buf = (BIT_BUF *)malloc(1*sizeof(BIT_BUF));
  91. if ( bit_buf == NULL )
  92. return( bit_buf );
  93. bit_buf->buf = buffer;
  94. bit_buf->current_byte = 0;
  95. bit_buf->rack = 0;
  96. bit_buf->mask = 0x80;
  97. bit_buf->pacifier_counter = 0;
  98. return( bit_buf );
  99. }
  100. void CloseOutputBitBuf( BIT_BUF *bit_buf ) {
  101. if ( bit_buf->mask != 0x80 )
  102. bit_buf->buf[bit_buf->current_byte++] = bit_buf->rack;
  103. free( bit_buf );
  104. }
  105. void CloseInputBitBuf( BIT_BUF *bit_buf )
  106. {
  107. free( bit_buf->buf );
  108. free( bit_buf );
  109. }
  110. void OutputBit( BIT_BUF *bit_buf, int bit ) {
  111. if ( bit )
  112. bit_buf->rack |= bit_buf->mask;
  113. bit_buf->mask >>= 1;
  114. if ( bit_buf->mask == 0 ) {
  115. bit_buf->buf[bit_buf->current_byte++] = bit_buf->rack;
  116. bit_buf->rack = 0;
  117. bit_buf->mask = 0x80;
  118. }
  119. }
  120. void OutputBits( BIT_BUF *bit_buf, unsigned int code, int count ) {
  121. unsigned int mask;
  122. mask = 1L << ( count - 1 );
  123. while ( mask != 0) {
  124. if ( mask & code )
  125. bit_buf->rack |= bit_buf->mask;
  126. bit_buf->mask >>= 1;
  127. if ( bit_buf->mask == 0 ) {
  128. bit_buf->buf[bit_buf->current_byte++] = bit_buf->rack;
  129. bit_buf->rack = 0;
  130. bit_buf->mask = 0x80;
  131. }
  132. mask >>= 1;
  133. }
  134. }
  135. int InputBit( BIT_BUF *bit_buf ) {
  136. int value;
  137. if ( bit_buf->mask == 0x80 ) {
  138. bit_buf->rack = bit_buf->buf[bit_buf->current_byte++];
  139. if ( bit_buf->rack == EOF ) {
  140. printf( " ERROR : Fatal error in InputBit!\n" );
  141. exit(4);
  142. }
  143. }
  144. value = bit_buf->rack & bit_buf->mask;
  145. bit_buf->mask >>= 1;
  146. if ( bit_buf->mask == 0 )
  147. bit_buf->mask = 0x80;
  148. return( value ? 1 : 0 );
  149. }
  150. unsigned int InputBits( BIT_BUF *bit_buf, int bit_count ) {
  151. unsigned int mask;
  152. unsigned int return_value;
  153. mask = 1L << ( bit_count - 1 );
  154. return_value = 0;
  155. while ( mask != 0) {
  156. if ( bit_buf->mask == 0x80 ) {
  157. bit_buf->rack = bit_buf->buf[bit_buf->current_byte++];
  158. if ( bit_buf->rack == EOF ) {
  159. printf( " ERROR : Fatal error in InputBits!\n" );
  160. exit(5);
  161. }
  162. }
  163. if ( bit_buf->rack & bit_buf->mask )
  164. return_value |= mask;
  165. mask >>= 1;
  166. bit_buf->mask >>= 1;
  167. if ( bit_buf->mask == 0 )
  168. bit_buf->mask = 0x80;
  169. }
  170. return( return_value );
  171. }
  172. /*
  173. void FilePrintBinary( FILE *file, unsigned int code, int bits ) {
  174. unsigned int mask;
  175. mask = 1 << ( bits - 1 );
  176. while ( mask != 0 ) {
  177. if ( code & mask )
  178. fputc( '1', file );
  179. else
  180. fputc( '0', file );
  181. mask >>= 1;
  182. }
  183. } */