deflate_p.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* deflate_p.h -- Private inline functions and macros shared with more than
  2. * one deflate method
  3. *
  4. * Copyright (C) 1995-2024 Jean-loup Gailly and Mark Adler
  5. * For conditions of distribution and use, see copyright notice in zlib.h
  6. *
  7. */
  8. #ifndef DEFLATE_P_H
  9. #define DEFLATE_P_H
  10. /* Forward declare common non-inlined functions declared in deflate.c */
  11. #ifdef ZLIB_DEBUG
  12. /* ===========================================================================
  13. * Check that the match at match_start is indeed a match.
  14. */
  15. static inline void check_match(deflate_state *s, Pos start, Pos match, int length) {
  16. /* check that the match length is valid*/
  17. if (length < STD_MIN_MATCH || length > STD_MAX_MATCH) {
  18. fprintf(stderr, " start %u, match %u, length %d\n", start, match, length);
  19. z_error("invalid match length");
  20. }
  21. /* check that the match isn't at the same position as the start string */
  22. if (match == start) {
  23. fprintf(stderr, " start %u, match %u, length %d\n", start, match, length);
  24. z_error("invalid match position");
  25. }
  26. /* check that the match is indeed a match */
  27. if (memcmp(s->window + match, s->window + start, length) != 0) {
  28. int32_t i = 0;
  29. fprintf(stderr, " start %u, match %u, length %d\n", start, match, length);
  30. do {
  31. fprintf(stderr, " %03d: match [%02x] start [%02x]\n", i++,
  32. s->window[match++], s->window[start++]);
  33. } while (--length != 0);
  34. z_error("invalid match");
  35. }
  36. if (z_verbose > 1) {
  37. fprintf(stderr, "\\[%u,%d]", start-match, length);
  38. do {
  39. putc(s->window[start++], stderr);
  40. } while (--length != 0);
  41. }
  42. }
  43. #else
  44. #define check_match(s, start, match, length)
  45. #endif
  46. Z_INTERNAL void PREFIX(flush_pending)(PREFIX3(stream) *strm);
  47. Z_INTERNAL unsigned PREFIX(read_buf)(PREFIX3(stream) *strm, unsigned char *buf, unsigned size);
  48. /* ===========================================================================
  49. * Save the match info and tally the frequency counts. Return true if
  50. * the current block must be flushed.
  51. */
  52. extern const unsigned char Z_INTERNAL zng_length_code[];
  53. extern const unsigned char Z_INTERNAL zng_dist_code[];
  54. static inline int zng_tr_tally_lit(deflate_state *s, unsigned char c) {
  55. /* c is the unmatched char */
  56. #ifdef LIT_MEM
  57. s->d_buf[s->sym_next] = 0;
  58. s->l_buf[s->sym_next++] = c;
  59. #else
  60. s->sym_buf[s->sym_next++] = 0;
  61. s->sym_buf[s->sym_next++] = 0;
  62. s->sym_buf[s->sym_next++] = c;
  63. #endif
  64. s->dyn_ltree[c].Freq++;
  65. Tracevv((stderr, "%c", c));
  66. Assert(c <= (STD_MAX_MATCH-STD_MIN_MATCH), "zng_tr_tally: bad literal");
  67. return (s->sym_next == s->sym_end);
  68. }
  69. static inline int zng_tr_tally_dist(deflate_state* s, uint32_t dist, uint32_t len) {
  70. /* dist: distance of matched string */
  71. /* len: match length-STD_MIN_MATCH */
  72. #ifdef LIT_MEM
  73. s->d_buf[s->sym_next] = dist;
  74. s->l_buf[s->sym_next++] = len;
  75. #else
  76. s->sym_buf[s->sym_next++] = (uint8_t)(dist);
  77. s->sym_buf[s->sym_next++] = (uint8_t)(dist >> 8);
  78. s->sym_buf[s->sym_next++] = (uint8_t)len;
  79. #endif
  80. s->matches++;
  81. dist--;
  82. Assert(dist < MAX_DIST(s) && (uint16_t)d_code(dist) < (uint16_t)D_CODES,
  83. "zng_tr_tally: bad match");
  84. s->dyn_ltree[zng_length_code[len] + LITERALS + 1].Freq++;
  85. s->dyn_dtree[d_code(dist)].Freq++;
  86. return (s->sym_next == s->sym_end);
  87. }
  88. /* ===========================================================================
  89. * Flush the current block, with given end-of-file flag.
  90. * IN assertion: strstart is set to the end of the current match.
  91. */
  92. #define FLUSH_BLOCK_ONLY(s, last) { \
  93. zng_tr_flush_block(s, (s->block_start >= 0 ? \
  94. (char *)&s->window[(unsigned)s->block_start] : \
  95. NULL), \
  96. (uint32_t)((int)s->strstart - s->block_start), \
  97. (last)); \
  98. s->block_start = (int)s->strstart; \
  99. PREFIX(flush_pending)(s->strm); \
  100. }
  101. /* Same but force premature exit if necessary. */
  102. #define FLUSH_BLOCK(s, last) { \
  103. FLUSH_BLOCK_ONLY(s, last); \
  104. if (s->strm->avail_out == 0) return (last) ? finish_started : need_more; \
  105. }
  106. /* Maximum stored block length in deflate format (not including header). */
  107. #define MAX_STORED 65535
  108. /* Compression function. Returns the block state after the call. */
  109. typedef block_state (*compress_func) (deflate_state *s, int flush);
  110. /* Match function. Returns the longest match. */
  111. typedef uint32_t (*match_func) (deflate_state *const s, Pos cur_match);
  112. #endif