cmcompress.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Copyright (c) 1985, 1986 The Regents of the University of California.
  3. * All rights reserved.
  4. *
  5. * This code is derived from software contributed to Berkeley by
  6. * James A. Woods, derived from original work by Spencer Thomas
  7. * and Joseph Orost.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. All advertising materials mentioning features or use of this software
  18. * must display the following acknowledgement:
  19. * This product includes software developed by the University of
  20. * California, Berkeley and its contributors.
  21. * 4. Neither the name of the University nor the names of its contributors
  22. * may be used to endorse or promote products derived from this software
  23. * without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  26. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  29. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  31. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  32. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  34. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  35. * SUCH DAMAGE.
  36. */
  37. #ifndef cmcompress__h_
  38. #define cmcompress__h_
  39. #include <stdio.h>
  40. #ifdef __cplusplus
  41. extern "C"
  42. {
  43. #endif
  44. /*
  45. * Set USERMEM to the maximum amount of physical user memory available
  46. * in bytes. USERMEM is used to determine the maximum BITS that can be used
  47. * for compression.
  48. *
  49. * SACREDMEM is the amount of physical memory saved for others; compress
  50. * will hog the rest.
  51. */
  52. #ifndef SACREDMEM
  53. #define SACREDMEM 0
  54. #endif
  55. #ifndef USERMEM
  56. # define USERMEM 450000 /* default user memory */
  57. #endif
  58. #ifdef pdp11
  59. # define BITS 12 /* max bits/code for 16-bit machine */
  60. # define NO_UCHAR /* also if "unsigned char" functions as signed char */
  61. # undef USERMEM
  62. #endif /* pdp11 */ /* don't forget to compile with -i */
  63. #ifdef USERMEM
  64. # if USERMEM >= (433484+SACREDMEM)
  65. # define PBITS 16
  66. # else
  67. # if USERMEM >= (229600+SACREDMEM)
  68. # define PBITS 15
  69. # else
  70. # if USERMEM >= (127536+SACREDMEM)
  71. # define PBITS 14
  72. # else
  73. # if USERMEM >= (73464+SACREDMEM)
  74. # define PBITS 13
  75. # else
  76. # define PBITS 12
  77. # endif
  78. # endif
  79. # endif
  80. # endif
  81. # undef USERMEM
  82. #endif /* USERMEM */
  83. #ifdef PBITS /* Preferred BITS for this memory size */
  84. # ifndef BITS
  85. # define BITS PBITS
  86. # endif /* BITS */
  87. #endif /* PBITS */
  88. #if BITS == 16
  89. # define HSIZE 69001 /* 95% occupancy */
  90. #endif
  91. #if BITS == 15
  92. # define HSIZE 35023 /* 94% occupancy */
  93. #endif
  94. #if BITS == 14
  95. # define HSIZE 18013 /* 91% occupancy */
  96. #endif
  97. #if BITS == 13
  98. # define HSIZE 9001 /* 91% occupancy */
  99. #endif
  100. #if BITS <= 12
  101. # define HSIZE 5003 /* 80% occupancy */
  102. #endif
  103. /*
  104. * a code_int must be able to hold 2**BITS values of type int, and also -1
  105. */
  106. #if BITS > 15
  107. typedef long int code_int;
  108. #else
  109. typedef int code_int;
  110. #endif
  111. #ifdef SIGNED_COMPARE_SLOW
  112. typedef unsigned long int count_int;
  113. typedef unsigned short int count_short;
  114. #else
  115. typedef long int count_int;
  116. #endif
  117. #ifdef NO_UCHAR
  118. typedef char char_type;
  119. #else
  120. typedef unsigned char char_type;
  121. #endif /* UCHAR */
  122. struct cmcompress_stream
  123. {
  124. int n_bits; /* number of bits/code */
  125. int maxbits; /* user settable max # bits/code */
  126. code_int maxcode; /* maximum code, given n_bits */
  127. code_int maxmaxcode; /* should NEVER generate this code */
  128. count_int htab [HSIZE];
  129. unsigned short codetab [HSIZE];
  130. code_int hsize; /* for dynamic table sizing */
  131. code_int free_ent; /* first unused entry */
  132. int nomagic; /* Use a 3-byte magic number header, unless old file */
  133. /*
  134. * block compression parameters -- after all codes are used up,
  135. * and compression rate changes, start over.
  136. */
  137. int block_compress;
  138. int clear_flg;
  139. long int ratio;
  140. count_int checkpoint;
  141. #ifdef DEBUG
  142. int debug;
  143. int verbose;
  144. #endif
  145. /* compress internals */
  146. int offset;
  147. long int in_count; /* length of input */
  148. long int bytes_out; /* length of compressed output */
  149. long int out_count; /* # of codes output (for debugging) */
  150. /* internals */
  151. code_int ent;
  152. code_int hsize_reg;
  153. int hshift;
  154. long fcode;
  155. int first_pass;
  156. /* For input and output */
  157. int (*input_stream)(void*);
  158. int (*output_stream)(void*, const char*,int);
  159. void* client_data;
  160. };
  161. int cmcompress_compress_initialize(struct cmcompress_stream* cdata);
  162. int cmcompress_compress_start(struct cmcompress_stream* cdata);
  163. int cmcompress_compress(struct cmcompress_stream* cdata, void* buff, size_t n);
  164. int cmcompress_compress_finalize(struct cmcompress_stream* cdata);
  165. #ifdef __cplusplus
  166. }
  167. #endif
  168. #endif /* cmcompress__h_ */