Counter Strike : Global Offensive Source Code
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.

233 lines
8.4 KiB

  1. /* fitblk.c: example of fitting compressed output to a specified size
  2. Not copyrighted -- provided to the public domain
  3. Version 1.1 25 November 2004 Mark Adler */
  4. /* Version history:
  5. 1.0 24 Nov 2004 First version
  6. 1.1 25 Nov 2004 Change deflateInit2() to deflateInit()
  7. Use fixed-size, stack-allocated raw buffers
  8. Simplify code moving compression to subroutines
  9. Use assert() for internal errors
  10. Add detailed description of approach
  11. */
  12. /* Approach to just fitting a requested compressed size:
  13. fitblk performs three compression passes on a portion of the input
  14. data in order to determine how much of that input will compress to
  15. nearly the requested output block size. The first pass generates
  16. enough deflate blocks to produce output to fill the requested
  17. output size plus a specfied excess amount (see the EXCESS define
  18. below). The last deflate block may go quite a bit past that, but
  19. is discarded. The second pass decompresses and recompresses just
  20. the compressed data that fit in the requested plus excess sized
  21. buffer. The deflate process is terminated after that amount of
  22. input, which is less than the amount consumed on the first pass.
  23. The last deflate block of the result will be of a comparable size
  24. to the final product, so that the header for that deflate block and
  25. the compression ratio for that block will be about the same as in
  26. the final product. The third compression pass decompresses the
  27. result of the second step, but only the compressed data up to the
  28. requested size minus an amount to allow the compressed stream to
  29. complete (see the MARGIN define below). That will result in a
  30. final compressed stream whose length is less than or equal to the
  31. requested size. Assuming sufficient input and a requested size
  32. greater than a few hundred bytes, the shortfall will typically be
  33. less than ten bytes.
  34. If the input is short enough that the first compression completes
  35. before filling the requested output size, then that compressed
  36. stream is return with no recompression.
  37. EXCESS is chosen to be just greater than the shortfall seen in a
  38. two pass approach similar to the above. That shortfall is due to
  39. the last deflate block compressing more efficiently with a smaller
  40. header on the second pass. EXCESS is set to be large enough so
  41. that there is enough uncompressed data for the second pass to fill
  42. out the requested size, and small enough so that the final deflate
  43. block of the second pass will be close in size to the final deflate
  44. block of the third and final pass. MARGIN is chosen to be just
  45. large enough to assure that the final compression has enough room
  46. to complete in all cases.
  47. */
  48. #include <stdio.h>
  49. #include <stdlib.h>
  50. #include <assert.h>
  51. #include "zlib.h"
  52. #define local static
  53. /* print nastygram and leave */
  54. local void quit(char *why)
  55. {
  56. fprintf(stderr, "fitblk abort: %s\n", why);
  57. exit(1);
  58. }
  59. #define RAWLEN 4096 /* intermediate uncompressed buffer size */
  60. /* compress from file to def until provided buffer is full or end of
  61. input reached; return last deflate() return value, or Z_ERRNO if
  62. there was read error on the file */
  63. local int partcompress(FILE *in, z_streamp def)
  64. {
  65. int ret, flush;
  66. unsigned char raw[RAWLEN];
  67. flush = Z_NO_FLUSH;
  68. do {
  69. def->avail_in = fread(raw, 1, RAWLEN, in);
  70. if (ferror(in))
  71. return Z_ERRNO;
  72. def->next_in = raw;
  73. if (feof(in))
  74. flush = Z_FINISH;
  75. ret = deflate(def, flush);
  76. assert(ret != Z_STREAM_ERROR);
  77. } while (def->avail_out != 0 && flush == Z_NO_FLUSH);
  78. return ret;
  79. }
  80. /* recompress from inf's input to def's output; the input for inf and
  81. the output for def are set in those structures before calling;
  82. return last deflate() return value, or Z_MEM_ERROR if inflate()
  83. was not able to allocate enough memory when it needed to */
  84. local int recompress(z_streamp inf, z_streamp def)
  85. {
  86. int ret, flush;
  87. unsigned char raw[RAWLEN];
  88. flush = Z_NO_FLUSH;
  89. do {
  90. /* decompress */
  91. inf->avail_out = RAWLEN;
  92. inf->next_out = raw;
  93. ret = inflate(inf, Z_NO_FLUSH);
  94. assert(ret != Z_STREAM_ERROR && ret != Z_DATA_ERROR &&
  95. ret != Z_NEED_DICT);
  96. if (ret == Z_MEM_ERROR)
  97. return ret;
  98. /* compress what was decompresed until done or no room */
  99. def->avail_in = RAWLEN - inf->avail_out;
  100. def->next_in = raw;
  101. if (inf->avail_out != 0)
  102. flush = Z_FINISH;
  103. ret = deflate(def, flush);
  104. assert(ret != Z_STREAM_ERROR);
  105. } while (ret != Z_STREAM_END && def->avail_out != 0);
  106. return ret;
  107. }
  108. #define EXCESS 256 /* empirically determined stream overage */
  109. #define MARGIN 8 /* amount to back off for completion */
  110. /* compress from stdin to fixed-size block on stdout */
  111. int main(int argc, char **argv)
  112. {
  113. int ret; /* return code */
  114. unsigned size; /* requested fixed output block size */
  115. unsigned have; /* bytes written by deflate() call */
  116. unsigned char *blk; /* intermediate and final stream */
  117. unsigned char *tmp; /* close to desired size stream */
  118. z_stream def, inf; /* zlib deflate and inflate states */
  119. /* get requested output size */
  120. if (argc != 2)
  121. quit("need one argument: size of output block");
  122. ret = strtol(argv[1], argv + 1, 10);
  123. if (argv[1][0] != 0)
  124. quit("argument must be a number");
  125. if (ret < 8) /* 8 is minimum zlib stream size */
  126. quit("need positive size of 8 or greater");
  127. size = (unsigned)ret;
  128. /* allocate memory for buffers and compression engine */
  129. blk = malloc(size + EXCESS);
  130. def.zalloc = Z_NULL;
  131. def.zfree = Z_NULL;
  132. def.opaque = Z_NULL;
  133. ret = deflateInit(&def, Z_DEFAULT_COMPRESSION);
  134. if (ret != Z_OK || blk == NULL)
  135. quit("out of memory");
  136. /* compress from stdin until output full, or no more input */
  137. def.avail_out = size + EXCESS;
  138. def.next_out = blk;
  139. ret = partcompress(stdin, &def);
  140. if (ret == Z_ERRNO)
  141. quit("error reading input");
  142. /* if it all fit, then size was undersubscribed -- done! */
  143. if (ret == Z_STREAM_END && def.avail_out >= EXCESS) {
  144. /* write block to stdout */
  145. have = size + EXCESS - def.avail_out;
  146. if (fwrite(blk, 1, have, stdout) != have || ferror(stdout))
  147. quit("error writing output");
  148. /* clean up and print results to stderr */
  149. ret = deflateEnd(&def);
  150. assert(ret != Z_STREAM_ERROR);
  151. free(blk);
  152. fprintf(stderr,
  153. "%u bytes unused out of %u requested (all input)\n",
  154. size - have, size);
  155. return 0;
  156. }
  157. /* it didn't all fit -- set up for recompression */
  158. inf.zalloc = Z_NULL;
  159. inf.zfree = Z_NULL;
  160. inf.opaque = Z_NULL;
  161. inf.avail_in = 0;
  162. inf.next_in = Z_NULL;
  163. ret = inflateInit(&inf);
  164. tmp = malloc(size + EXCESS);
  165. if (ret != Z_OK || tmp == NULL)
  166. quit("out of memory");
  167. ret = deflateReset(&def);
  168. assert(ret != Z_STREAM_ERROR);
  169. /* do first recompression close to the right amount */
  170. inf.avail_in = size + EXCESS;
  171. inf.next_in = blk;
  172. def.avail_out = size + EXCESS;
  173. def.next_out = tmp;
  174. ret = recompress(&inf, &def);
  175. if (ret == Z_MEM_ERROR)
  176. quit("out of memory");
  177. /* set up for next reocmpression */
  178. ret = inflateReset(&inf);
  179. assert(ret != Z_STREAM_ERROR);
  180. ret = deflateReset(&def);
  181. assert(ret != Z_STREAM_ERROR);
  182. /* do second and final recompression (third compression) */
  183. inf.avail_in = size - MARGIN; /* assure stream will complete */
  184. inf.next_in = tmp;
  185. def.avail_out = size;
  186. def.next_out = blk;
  187. ret = recompress(&inf, &def);
  188. if (ret == Z_MEM_ERROR)
  189. quit("out of memory");
  190. assert(ret == Z_STREAM_END); /* otherwise MARGIN too small */
  191. /* done -- write block to stdout */
  192. have = size - def.avail_out;
  193. if (fwrite(blk, 1, have, stdout) != have || ferror(stdout))
  194. quit("error writing output");
  195. /* clean up and print results to stderr */
  196. free(tmp);
  197. ret = inflateEnd(&inf);
  198. assert(ret != Z_STREAM_ERROR);
  199. ret = deflateEnd(&def);
  200. assert(ret != Z_STREAM_ERROR);
  201. free(blk);
  202. fprintf(stderr,
  203. "%u bytes unused out of %u requested (%lu input)\n",
  204. size - have, size, def.total_in);
  205. return 0;
  206. }