bits.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /* bits.c -- output variable-length bit strings
  2. Copyright (C) 1999 Free Software Foundation, Inc.
  3. Copyright (C) 1992-1993 Jean-loup Gailly
  4. Copyright (C) 2008 Josh Triplett
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software Foundation,
  15. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  16. /*
  17. * PURPOSE
  18. *
  19. * Output variable-length bit strings. Compression can be done
  20. * to a file or to memory. (The latter is not supported in this version.)
  21. *
  22. * DISCUSSION
  23. *
  24. * The PKZIP "deflate" file format interprets compressed file data
  25. * as a sequence of bits. Multi-bit strings in the file may cross
  26. * byte boundaries without restriction.
  27. *
  28. * The first bit of each byte is the low-order bit.
  29. *
  30. * The routines in this file allow a variable-length bit value to
  31. * be output right-to-left (useful for literal values). For
  32. * left-to-right output (useful for code strings from the tree routines),
  33. * the bits must have been reversed first with bi_reverse().
  34. *
  35. * For in-memory compression, the compressed bit stream goes directly
  36. * into the requested output buffer. The input data is read in blocks
  37. * by the mem_read() function. The buffer is limited to 64K on 16 bit
  38. * machines.
  39. *
  40. * INTERFACE
  41. *
  42. * void bi_init (FILE *zipfile)
  43. * Initialize the bit string routines.
  44. *
  45. * void send_bits (int value, int length)
  46. * Write out a bit string, taking the source bits right to
  47. * left.
  48. *
  49. * int bi_reverse (int value, int length)
  50. * Reverse the bits of a bit string, taking the source bits left to
  51. * right and emitting them right to left.
  52. *
  53. * void bi_windup (void)
  54. * Write out any remaining bits in an incomplete byte.
  55. *
  56. * void copy_block(char *buf, unsigned len, int header)
  57. * Copy a stored block to the zip file, storing first the length and
  58. * its one's complement if requested.
  59. *
  60. */
  61. #include "gzip.h"
  62. /* ===========================================================================
  63. * Local data used by the "bit string" routines.
  64. */
  65. static int zfile; /* output gzip file */
  66. static unsigned short bi_buf;
  67. /* Output buffer. bits are inserted starting at the bottom (least significant
  68. * bits).
  69. */
  70. #define Buf_size (8 * 2*sizeof(char))
  71. /* Number of bits used within bi_buf. (bi_buf might be implemented on
  72. * more than 16 bits on some systems.)
  73. */
  74. static int bi_valid;
  75. /* Number of valid bits in bi_buf. All bits above the last valid bit
  76. * are always zero.
  77. */
  78. int (*read_buf)(char *buf, unsigned size);
  79. /* Current input function. Set to mem_read for in-memory compression */
  80. /* ===========================================================================
  81. * Initialize the bit string routines.
  82. */
  83. void bi_init (int zipfile) /* output zip file, NO_FILE for in-memory compression */
  84. {
  85. zfile = zipfile;
  86. bi_buf = 0;
  87. bi_valid = 0;
  88. /* Set the defaults for file compression. They are set by memcompress
  89. * for in-memory compression.
  90. */
  91. if (zfile != NO_FILE) {
  92. read_buf = file_read;
  93. }
  94. }
  95. /* ===========================================================================
  96. * Send a value on a given number of bits.
  97. * IN assertion: length <= 16 and value fits in length bits.
  98. */
  99. void send_bits(int value, /* value to send */
  100. int length) /* number of bits */
  101. {
  102. /* If not enough room in bi_buf, use (valid) bits from bi_buf and
  103. * (16 - bi_valid) bits from value, leaving (width - (16-bi_valid))
  104. * unused bits in value.
  105. */
  106. if (bi_valid > (int)Buf_size - length) {
  107. bi_buf |= (value << bi_valid);
  108. put_short(bi_buf);
  109. bi_buf = (ush)value >> (Buf_size - bi_valid);
  110. bi_valid += length - Buf_size;
  111. } else {
  112. bi_buf |= value << bi_valid;
  113. bi_valid += length;
  114. }
  115. }
  116. /* ===========================================================================
  117. * Reverse the first len bits of a code, using straightforward code (a faster
  118. * method would use a table)
  119. * IN assertion: 1 <= len <= 15
  120. */
  121. unsigned bi_reverse(unsigned code, /* the value to invert */
  122. int len) /* its bit length */
  123. {
  124. register unsigned res = 0;
  125. do {
  126. res |= code & 1;
  127. code >>= 1, res <<= 1;
  128. } while (--len > 0);
  129. return res >> 1;
  130. }
  131. /* ===========================================================================
  132. * Write out any remaining bits in an incomplete byte.
  133. */
  134. void bi_windup(void)
  135. {
  136. if (bi_valid > 8) {
  137. put_short(bi_buf);
  138. } else if (bi_valid > 0) {
  139. put_byte(bi_buf);
  140. }
  141. bi_buf = 0;
  142. bi_valid = 0;
  143. }
  144. /* ===========================================================================
  145. * Copy a stored block to the zip file, storing first the length and its
  146. * one's complement if requested.
  147. */
  148. void copy_block(char *buf, /* the input data */
  149. unsigned len, /* its length */
  150. int header) /* true if block header must be written */
  151. {
  152. bi_windup(); /* align on byte boundary */
  153. if (header) {
  154. put_short((ush)len);
  155. put_short((ush)~len);
  156. }
  157. while (len--) {
  158. put_byte(*buf++);
  159. }
  160. }