deflate_rle.c 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* deflate_rle.c -- compress data using RLE strategy of deflation algorithm
  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 "compare256_rle.h"
  8. #include "deflate.h"
  9. #include "deflate_p.h"
  10. #include "functable.h"
  11. #ifdef UNALIGNED_OK
  12. # if defined(UNALIGNED64_OK) && defined(HAVE_BUILTIN_CTZLL)
  13. # define compare256_rle compare256_rle_unaligned_64
  14. # elif defined(HAVE_BUILTIN_CTZ)
  15. # define compare256_rle compare256_rle_unaligned_32
  16. # else
  17. # define compare256_rle compare256_rle_unaligned_16
  18. # endif
  19. #else
  20. # define compare256_rle compare256_rle_c
  21. #endif
  22. /* ===========================================================================
  23. * For Z_RLE, simply look for runs of bytes, generate matches only of distance
  24. * one. Do not maintain a hash table. (It will be regenerated if this run of
  25. * deflate switches away from Z_RLE.)
  26. */
  27. Z_INTERNAL block_state deflate_rle(deflate_state *s, int flush) {
  28. int bflush = 0; /* set if current block must be flushed */
  29. unsigned char *scan; /* scan goes up to strend for length of run */
  30. uint32_t match_len = 0;
  31. for (;;) {
  32. /* Make sure that we always have enough lookahead, except
  33. * at the end of the input file. We need STD_MAX_MATCH bytes
  34. * for the longest run, plus one for the unrolled loop.
  35. */
  36. if (s->lookahead <= STD_MAX_MATCH) {
  37. PREFIX(fill_window)(s);
  38. if (s->lookahead <= STD_MAX_MATCH && flush == Z_NO_FLUSH)
  39. return need_more;
  40. if (s->lookahead == 0)
  41. break; /* flush the current block */
  42. }
  43. /* See how many times the previous byte repeats */
  44. if (s->lookahead >= STD_MIN_MATCH && s->strstart > 0) {
  45. scan = s->window + s->strstart - 1;
  46. if (scan[0] == scan[1] && scan[1] == scan[2]) {
  47. match_len = compare256_rle(scan, scan+3)+2;
  48. match_len = MIN(match_len, s->lookahead);
  49. match_len = MIN(match_len, STD_MAX_MATCH);
  50. }
  51. Assert(scan+match_len <= s->window + s->window_size - 1, "wild scan");
  52. }
  53. /* Emit match if have run of STD_MIN_MATCH or longer, else emit literal */
  54. if (match_len >= STD_MIN_MATCH) {
  55. check_match(s, s->strstart, s->strstart - 1, match_len);
  56. bflush = zng_tr_tally_dist(s, 1, match_len - STD_MIN_MATCH);
  57. s->lookahead -= match_len;
  58. s->strstart += match_len;
  59. match_len = 0;
  60. } else {
  61. /* No match, output a literal byte */
  62. bflush = zng_tr_tally_lit(s, s->window[s->strstart]);
  63. s->lookahead--;
  64. s->strstart++;
  65. }
  66. if (bflush)
  67. FLUSH_BLOCK(s, 0);
  68. }
  69. s->insert = 0;
  70. if (flush == Z_FINISH) {
  71. FLUSH_BLOCK(s, 1);
  72. return finish_done;
  73. }
  74. if (s->sym_next)
  75. FLUSH_BLOCK(s, 0);
  76. return block_done;
  77. }