deflate_quick.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * The deflate_quick deflate strategy, designed to be used when cycles are
  3. * at a premium.
  4. *
  5. * Copyright (C) 2013 Intel Corporation. All rights reserved.
  6. * Authors:
  7. * Wajdi Feghali <wajdi.k.feghali@intel.com>
  8. * Jim Guilford <james.guilford@intel.com>
  9. * Vinodh Gopal <vinodh.gopal@intel.com>
  10. * Erdinc Ozturk <erdinc.ozturk@intel.com>
  11. * Jim Kukunas <james.t.kukunas@linux.intel.com>
  12. *
  13. * Portions are Copyright (C) 2016 12Sided Technology, LLC.
  14. * Author:
  15. * Phil Vachon <pvachon@12sidedtech.com>
  16. *
  17. * For conditions of distribution and use, see copyright notice in zlib.h
  18. */
  19. #include "zbuild.h"
  20. #include "zutil_p.h"
  21. #include "deflate.h"
  22. #include "deflate_p.h"
  23. #include "functable.h"
  24. #include "trees_emit.h"
  25. extern const ct_data static_ltree[L_CODES+2];
  26. extern const ct_data static_dtree[D_CODES];
  27. #define QUICK_START_BLOCK(s, last) { \
  28. zng_tr_emit_tree(s, STATIC_TREES, last); \
  29. s->block_open = 1 + (int)last; \
  30. s->block_start = (int)s->strstart; \
  31. }
  32. #define QUICK_END_BLOCK(s, last) { \
  33. if (s->block_open) { \
  34. zng_tr_emit_end_block(s, static_ltree, last); \
  35. s->block_open = 0; \
  36. s->block_start = (int)s->strstart; \
  37. PREFIX(flush_pending)(s->strm); \
  38. if (s->strm->avail_out == 0) \
  39. return (last) ? finish_started : need_more; \
  40. } \
  41. }
  42. Z_INTERNAL block_state deflate_quick(deflate_state *s, int flush) {
  43. Pos hash_head;
  44. int64_t dist;
  45. unsigned match_len, last;
  46. last = (flush == Z_FINISH) ? 1 : 0;
  47. if (UNLIKELY(last && s->block_open != 2)) {
  48. /* Emit end of previous block */
  49. QUICK_END_BLOCK(s, 0);
  50. /* Emit start of last block */
  51. QUICK_START_BLOCK(s, last);
  52. } else if (UNLIKELY(s->block_open == 0 && s->lookahead > 0)) {
  53. /* Start new block only when we have lookahead data, so that if no
  54. input data is given an empty block will not be written */
  55. QUICK_START_BLOCK(s, last);
  56. }
  57. for (;;) {
  58. if (UNLIKELY(s->pending + ((BIT_BUF_SIZE + 7) >> 3) >= s->pending_buf_size)) {
  59. PREFIX(flush_pending)(s->strm);
  60. if (s->strm->avail_out == 0) {
  61. return (last && s->strm->avail_in == 0 && s->bi_valid == 0 && s->block_open == 0) ? finish_started : need_more;
  62. }
  63. }
  64. if (UNLIKELY(s->lookahead < MIN_LOOKAHEAD)) {
  65. PREFIX(fill_window)(s);
  66. if (UNLIKELY(s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH)) {
  67. return need_more;
  68. }
  69. if (UNLIKELY(s->lookahead == 0))
  70. break;
  71. if (UNLIKELY(s->block_open == 0)) {
  72. /* Start new block when we have lookahead data, so that if no
  73. input data is given an empty block will not be written */
  74. QUICK_START_BLOCK(s, last);
  75. }
  76. }
  77. if (LIKELY(s->lookahead >= WANT_MIN_MATCH)) {
  78. hash_head = quick_insert_string(s, s->strstart);
  79. dist = (int64_t)s->strstart - hash_head;
  80. if (dist <= MAX_DIST(s) && dist > 0) {
  81. const uint8_t *str_start = s->window + s->strstart;
  82. const uint8_t *match_start = s->window + hash_head;
  83. if (zng_memcmp_2(str_start, match_start) == 0) {
  84. match_len = FUNCTABLE_CALL(compare256)(str_start+2, match_start+2) + 2;
  85. if (match_len >= WANT_MIN_MATCH) {
  86. if (UNLIKELY(match_len > s->lookahead))
  87. match_len = s->lookahead;
  88. if (UNLIKELY(match_len > STD_MAX_MATCH))
  89. match_len = STD_MAX_MATCH;
  90. check_match(s, s->strstart, hash_head, match_len);
  91. zng_tr_emit_dist(s, static_ltree, static_dtree, match_len - STD_MIN_MATCH, (uint32_t)dist);
  92. s->lookahead -= match_len;
  93. s->strstart += match_len;
  94. continue;
  95. }
  96. }
  97. }
  98. }
  99. zng_tr_emit_lit(s, static_ltree, s->window[s->strstart]);
  100. s->strstart++;
  101. s->lookahead--;
  102. }
  103. s->insert = s->strstart < (STD_MIN_MATCH - 1) ? s->strstart : (STD_MIN_MATCH - 1);
  104. if (UNLIKELY(last)) {
  105. QUICK_END_BLOCK(s, 1);
  106. return finish_done;
  107. }
  108. QUICK_END_BLOCK(s, 0);
  109. return block_done;
  110. }