Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

189 lines
5.8 KiB

  1. /* DEC/CMS REPLACEMENT HISTORY, Element BITS.C */
  2. /* *1 14-NOV-1996 10:25:36 ANIGBOGU "[113914]Functions to output variable-length bit strings" */
  3. /* DEC/CMS REPLACEMENT HISTORY, Element BITS.C */
  4. /* PRIVATE FILE
  5. ******************************************************************************
  6. **
  7. ** (c) Copyright Schlumberger Technology Corp., unpublished work, created 1996.
  8. **
  9. ** This computer program includes Confidential, Proprietary Information and is
  10. ** a Trade Secret of Schlumberger Technology Corp. All use, disclosure, and/or
  11. ** reproduction is prohibited unless authorized in writing by Schlumberger.
  12. ** All Rights Reserved.
  13. **
  14. ******************************************************************************
  15. **
  16. ** compress/bits.c
  17. **
  18. ** PURPOSE
  19. **
  20. ** Output variable-length bit strings. Compression can be done
  21. ** to a buffer or to memory. (The latter is not supported in this version.)
  22. **
  23. ** CLASSIFICATION TERMS
  24. **
  25. ** DISCUSSION
  26. **
  27. ** The PKZIP "deflate" format interprets compressed data
  28. ** as a sequence of bits. Multi-bit strings in the data may cross
  29. ** byte boundaries without restriction.
  30. **
  31. ** The first bit of each byte is the low-order bit.
  32. **
  33. ** The routines in this file allow a variable-length bit value to
  34. ** be output right-to-left (useful for literal values). For
  35. ** left-to-right output (useful for code strings from the tree routines),
  36. ** the bits must have been reversed first with ReverseBits().
  37. **
  38. ** INTERFACE
  39. **
  40. ** void InitializeBits(LocalBits_t *Bits)
  41. ** Initialize the bit string routines.
  42. **
  43. ** void SendBits(int Value, int Length, LocalBits_t *Bits,
  44. ** CompParam_t *Comp)
  45. ** Write out a bit string, taking the source bits right to
  46. ** left.
  47. **
  48. ** int ReverseBits(int Value, int Length)
  49. ** Reverse the bits of a bit string, taking the source bits left to
  50. ** right and emitting them right to left.
  51. **
  52. ** void WindupBits(LocalBits_t *Bits, CompParam_t *Comp)
  53. ** Write out any remaining bits in an incomplete byte.
  54. **
  55. ** void CopyBlock(char *Input, unsigned Length, int Header, LocalBits_t *Bits
  56. ** CompParam_t *Comp)
  57. ** Copy a stored block to the zip buffer, storing first the length and
  58. ** its one's complement if requested.
  59. **
  60. ** SPECIAL REQUIREMENTS & NOTES
  61. **
  62. ** AUTHOR
  63. **
  64. ** J. C. Anigbogu
  65. ** Austin Systems Center
  66. ** Nov 1996
  67. **
  68. ******************************************************************************
  69. */
  70. #include "comppriv.h"
  71. #define BufSize (8 * 2*sizeof(char))
  72. /* Number of bits used within ValidBits. (ValidBits might be implemented on
  73. * more than 16 bits on some systems.)
  74. */
  75. /* ===========================================================================
  76. * Initialize the bit string routines.
  77. */
  78. void
  79. InitializeBits(
  80. LocalBits_t *Bits
  81. )
  82. {
  83. Bits->BitBuffer = 0;
  84. Bits->ValidBits = 0;
  85. }
  86. /* ===========================================================================
  87. * Send a value on a given number of bits.
  88. * IN assertion: length <= 16 and value fits in length bits.
  89. */
  90. void
  91. SendBits(
  92. int Value, /* value to send */
  93. int Length, /* number of bits */
  94. LocalBits_t *Bits,
  95. CompParam_t *Comp
  96. )
  97. {
  98. /* If not enough room in BitBuffer, use (valid) bits from BitBuffer and
  99. * (16 - ValidBits) bits from value, leaving (width - (16-ValidBits))
  100. * unused bits in value.
  101. */
  102. if (Bits->ValidBits > (unsigned int)(BufSize - Length))
  103. {
  104. Bits->BitBuffer |= ((unsigned int)Value << Bits->ValidBits);
  105. PutShort(Bits->BitBuffer, Comp);
  106. Bits->BitBuffer = (unsigned int)Value >> (BufSize - Bits->ValidBits);
  107. Bits->ValidBits += (unsigned int)(Length - BufSize);
  108. }
  109. else
  110. {
  111. Bits->BitBuffer |= ((unsigned int)Value << Bits->ValidBits);
  112. Bits->ValidBits += (unsigned int)Length;
  113. }
  114. }
  115. /* ===========================================================================
  116. * Reverse the first length bits of a code, using straightforward code (a faster
  117. * method would use a table)
  118. * IN assertion: 1 <= len <= 15
  119. */
  120. unsigned int
  121. ReverseBits(
  122. unsigned int Code, /* the value to invert */
  123. int Length /* its bit length */
  124. )
  125. {
  126. unsigned int Result = 0;
  127. do
  128. {
  129. Result |= Code & 1;
  130. Code >>= 1;
  131. Result <<= 1;
  132. } while (--Length > 0);
  133. return Result >> 1;
  134. }
  135. /* ===========================================================================
  136. * Write out any remaining bits in an incomplete byte.
  137. */
  138. void
  139. WindupBits(
  140. LocalBits_t *Bits,
  141. CompParam_t *Comp
  142. )
  143. {
  144. if (Bits->ValidBits > 8)
  145. {
  146. PutShort(Bits->BitBuffer, Comp);
  147. }
  148. else if (Bits->ValidBits > 0)
  149. {
  150. PutByte(Bits->BitBuffer, Comp);
  151. }
  152. Bits->BitBuffer = 0;
  153. Bits->ValidBits = 0;
  154. }
  155. /* ===========================================================================
  156. * Copy a stored block to the zip buffer, storing first the length and its
  157. * one's complement if requested.
  158. */
  159. void
  160. CopyBlock(
  161. char *Input, /* the input data */
  162. unsigned int Length, /* its length */
  163. int Header, /* true if block header must be written */
  164. LocalBits_t *Bits,
  165. CompParam_t *Comp
  166. )
  167. {
  168. WindupBits(Bits, Comp); /* align on byte boundary */
  169. if (Header)
  170. {
  171. PutShort((unsigned short)Length, Comp);
  172. PutShort((unsigned short)~Length, Comp);
  173. }
  174. while (Length--)
  175. {
  176. PutByte(*Input++, Comp);
  177. }
  178. }