/src/zlib-ng/deflate_quick.c
Line | Count | Source (jump to first uncovered line) |
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 | | |
20 | | #include "zbuild.h" |
21 | | #include "zmemory.h" |
22 | | #include "deflate.h" |
23 | | #include "deflate_p.h" |
24 | | #include "functable.h" |
25 | | #include "trees_emit.h" |
26 | | |
27 | | extern const ct_data static_ltree[L_CODES+2]; |
28 | | extern const ct_data static_dtree[D_CODES]; |
29 | | |
30 | 5.20k | #define QUICK_START_BLOCK(s, last) { \ |
31 | 5.20k | zng_tr_emit_tree(s, STATIC_TREES, last); \ |
32 | 5.20k | s->block_open = 1 + (int)last; \ |
33 | 5.20k | s->block_start = (int)s->strstart; \ |
34 | 5.20k | } |
35 | | |
36 | 10.2k | #define QUICK_END_BLOCK(s, last) { \ |
37 | 10.2k | if (s->block_open) { \ |
38 | 5.20k | zng_tr_emit_end_block(s, static_ltree, last); \ |
39 | 5.20k | s->block_open = 0; \ |
40 | 5.20k | s->block_start = (int)s->strstart; \ |
41 | 5.20k | PREFIX(flush_pending)(s->strm); \ |
42 | 5.20k | if (s->strm->avail_out == 0) \ |
43 | 5.20k | return (last) ? finish_started : need_more; \ |
44 | 5.20k | } \ |
45 | 10.2k | } |
46 | | |
47 | 5.66k | Z_INTERNAL block_state deflate_quick(deflate_state *s, int flush) { |
48 | 5.66k | Pos hash_head; |
49 | 5.66k | int64_t dist; |
50 | 5.66k | unsigned match_len, last; |
51 | | |
52 | | |
53 | 5.66k | last = (flush == Z_FINISH) ? 1 : 0; |
54 | 5.66k | if (UNLIKELY(last && s->block_open != 2)) { |
55 | | /* Emit end of previous block */ |
56 | 5.10k | QUICK_END_BLOCK(s, 0); |
57 | | /* Emit start of last block */ |
58 | 5.09k | QUICK_START_BLOCK(s, last); |
59 | 5.09k | } else if (UNLIKELY(s->block_open == 0 && s->lookahead > 0)) { |
60 | | /* Start new block only when we have lookahead data, so that if no |
61 | | input data is given an empty block will not be written */ |
62 | 0 | QUICK_START_BLOCK(s, last); |
63 | 0 | } |
64 | | |
65 | 133M | for (;;) { |
66 | 133M | if (UNLIKELY(s->pending + ((BIT_BUF_SIZE + 7) >> 3) >= s->pending_buf_size)) { |
67 | 1.80k | PREFIX(flush_pending)(s->strm); |
68 | 1.80k | if (s->strm->avail_out == 0) { |
69 | 101 | return (last && s->strm->avail_in == 0 && s->bi_valid == 0 && s->block_open == 0) ? finish_started : need_more; |
70 | 101 | } |
71 | 1.80k | } |
72 | | |
73 | 133M | if (UNLIKELY(s->lookahead < MIN_LOOKAHEAD)) { |
74 | 328k | PREFIX(fill_window)(s); |
75 | 328k | if (UNLIKELY(s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH)) { |
76 | 462 | return need_more; |
77 | 462 | } |
78 | 327k | if (UNLIKELY(s->lookahead == 0)) |
79 | 5.09k | break; |
80 | | |
81 | 322k | if (UNLIKELY(s->block_open == 0)) { |
82 | | /* Start new block when we have lookahead data, so that if no |
83 | | input data is given an empty block will not be written */ |
84 | 102 | QUICK_START_BLOCK(s, last); |
85 | 102 | } |
86 | 322k | } |
87 | | |
88 | 133M | if (LIKELY(s->lookahead >= WANT_MIN_MATCH)) { |
89 | 133M | hash_head = quick_insert_string(s, s->strstart); |
90 | 133M | dist = (int64_t)s->strstart - hash_head; |
91 | | |
92 | 133M | if (dist <= MAX_DIST(s) && dist > 0) { |
93 | 62.2M | const uint8_t *str_start = s->window + s->strstart; |
94 | 62.2M | const uint8_t *match_start = s->window + hash_head; |
95 | | |
96 | 62.2M | if (zng_memcmp_2(str_start, match_start) == 0) { |
97 | 6.24M | match_len = FUNCTABLE_CALL(compare256)(str_start+2, match_start+2) + 2; |
98 | | |
99 | 6.24M | if (match_len >= WANT_MIN_MATCH) { |
100 | 6.24M | if (UNLIKELY(match_len > s->lookahead)) |
101 | 440 | match_len = s->lookahead; |
102 | 6.24M | if (UNLIKELY(match_len > STD_MAX_MATCH)) |
103 | 0 | match_len = STD_MAX_MATCH; |
104 | | |
105 | 6.24M | Assert(s->strstart <= UINT16_MAX, "strstart should fit in uint16_t"); |
106 | 6.24M | check_match(s, (Pos)s->strstart, hash_head, match_len); |
107 | | |
108 | 6.24M | zng_tr_emit_dist(s, static_ltree, static_dtree, match_len - STD_MIN_MATCH, (uint32_t)dist); |
109 | 6.24M | s->lookahead -= match_len; |
110 | 6.24M | s->strstart += match_len; |
111 | 6.24M | continue; |
112 | 6.24M | } |
113 | 6.24M | } |
114 | 62.2M | } |
115 | 133M | } |
116 | | |
117 | 127M | zng_tr_emit_lit(s, static_ltree, s->window[s->strstart]); |
118 | 127M | s->strstart++; |
119 | 127M | s->lookahead--; |
120 | 127M | } |
121 | | |
122 | 5.09k | s->insert = s->strstart < (STD_MIN_MATCH - 1) ? s->strstart : (STD_MIN_MATCH - 1); |
123 | 5.09k | if (UNLIKELY(last)) { |
124 | 5.09k | QUICK_END_BLOCK(s, 1); |
125 | 5.00k | return finish_done; |
126 | 5.09k | } |
127 | | |
128 | 0 | QUICK_END_BLOCK(s, 0); |
129 | 0 | return block_done; |
130 | 0 | } |