deflate_huff.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /* deflate_huff.c -- compress data using huffman encoding only strategy
  2. *
  3. * Copyright (C) 1995-2024 Jean-loup Gailly and Mark Adler
  4. * For conditions of distribution and use, see copyright notice in zlib.h
  5. */
  6. #include "zbuild.h"
  7. #include "deflate.h"
  8. #include "deflate_p.h"
  9. #include "functable.h"
  10. /* ===========================================================================
  11. * For Z_HUFFMAN_ONLY, do not look for matches. Do not maintain a hash table.
  12. * (It will be regenerated if this run of deflate switches away from Huffman.)
  13. */
  14. Z_INTERNAL block_state deflate_huff(deflate_state *s, int flush) {
  15. int bflush = 0; /* set if current block must be flushed */
  16. for (;;) {
  17. /* Make sure that we have a literal to write. */
  18. if (s->lookahead == 0) {
  19. PREFIX(fill_window)(s);
  20. if (s->lookahead == 0) {
  21. if (flush == Z_NO_FLUSH)
  22. return need_more;
  23. break; /* flush the current block */
  24. }
  25. }
  26. /* Output a literal byte */
  27. bflush = zng_tr_tally_lit(s, s->window[s->strstart]);
  28. s->lookahead--;
  29. s->strstart++;
  30. if (bflush)
  31. FLUSH_BLOCK(s, 0);
  32. }
  33. s->insert = 0;
  34. if (flush == Z_FINISH) {
  35. FLUSH_BLOCK(s, 1);
  36. return finish_done;
  37. }
  38. if (s->sym_next)
  39. FLUSH_BLOCK(s, 0);
  40. return block_done;
  41. }