/src/zlib-ng/deflate_quick.c
Line | Count | Source |
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 | | #include "insert_string_p.h" |
27 | | |
28 | | extern const ct_data static_ltree[L_CODES+2]; |
29 | | extern const ct_data static_dtree[D_CODES]; |
30 | | |
31 | 6.94k | Z_FORCEINLINE static void quick_start_block(deflate_state *s, int last) { |
32 | 6.94k | zng_tr_emit_tree(s, STATIC_TREES, last); |
33 | 6.94k | s->block_open = 1 + last; |
34 | 6.94k | s->block_start = (int)s->strstart; |
35 | 6.94k | } |
36 | | |
37 | 13.4k | Z_FORCEINLINE static int quick_end_block(deflate_state *s, int last) { |
38 | 13.4k | if (s->block_open) { |
39 | 6.94k | zng_tr_emit_end_block(s, static_ltree, last); |
40 | 6.94k | s->block_open = 0; |
41 | 6.94k | s->block_start = (int)s->strstart; |
42 | 6.94k | PREFIX(flush_pending)(s->strm); |
43 | 6.94k | return (s->strm->avail_out == 0); |
44 | 6.94k | } |
45 | 6.48k | return 0; |
46 | 13.4k | } |
47 | | |
48 | 7.69k | Z_INTERNAL block_state deflate_quick(deflate_state *s, int flush) { |
49 | 7.69k | unsigned char *window; |
50 | 7.69k | unsigned last = (flush == Z_FINISH) ? 1 : 0; |
51 | | |
52 | 7.69k | if (UNLIKELY(last && s->block_open != 2)) { |
53 | | /* Emit end of previous block */ |
54 | 6.71k | if (quick_end_block(s, 0)) |
55 | 4 | return need_more; |
56 | | /* Emit start of last block */ |
57 | 6.71k | quick_start_block(s, last); |
58 | 6.71k | } else if (UNLIKELY(s->block_open == 0 && s->lookahead > 0)) { |
59 | | /* Start new block only when we have lookahead data, so that if no |
60 | | input data is given an empty block will not be written */ |
61 | 0 | quick_start_block(s, last); |
62 | 0 | } |
63 | | |
64 | 7.69k | window = s->window; |
65 | | |
66 | 196M | for (;;) { |
67 | 196M | uint8_t lc; |
68 | | |
69 | 196M | if (UNLIKELY(s->pending + ((BIT_BUF_SIZE + 7) >> 3) >= s->pending_buf_size)) { |
70 | 2.55k | PREFIX(flush_pending)(s->strm); |
71 | 2.55k | if (s->strm->avail_out == 0) { |
72 | 180 | return (last && s->strm->avail_in == 0 && s->bi_valid == 0 && s->block_open == 0) ? finish_started : need_more; |
73 | 180 | } |
74 | 2.55k | } |
75 | | |
76 | 196M | if (UNLIKELY(s->lookahead < MIN_LOOKAHEAD)) { |
77 | 464k | PREFIX(fill_window)(s); |
78 | 464k | if (UNLIKELY(s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH)) { |
79 | 798 | return need_more; |
80 | 798 | } |
81 | 463k | if (UNLIKELY(s->lookahead == 0)) |
82 | 6.71k | break; |
83 | | |
84 | 457k | if (UNLIKELY(s->block_open == 0)) { |
85 | | /* Start new block when we have lookahead data, so that if no |
86 | | input data is given an empty block will not be written */ |
87 | 232 | quick_start_block(s, last); |
88 | 232 | } |
89 | 457k | } |
90 | | |
91 | 196M | if (LIKELY(s->lookahead >= WANT_MIN_MATCH)) { |
92 | 196M | uint32_t str_val = Z_U32_FROM_LE(zng_memread_4(window + s->strstart)); |
93 | 196M | uint32_t hash_head = quick_insert_value(s, s->strstart, str_val); |
94 | 196M | int64_t dist = (int64_t)s->strstart - hash_head; |
95 | 196M | lc = (uint8_t)str_val; |
96 | | |
97 | 196M | if (dist <= MAX_DIST(s) && dist > 0) { |
98 | 95.4M | const uint8_t *match_start = window + hash_head; |
99 | 95.4M | uint32_t match_val = Z_U32_FROM_LE(zng_memread_4(match_start)); |
100 | | |
101 | 95.4M | if (str_val == match_val) { |
102 | 4.95M | const uint8_t *str_start = window + s->strstart; |
103 | 4.95M | uint32_t match_len = FUNCTABLE_CALL(compare256)(str_start+2, match_start+2) + 2; |
104 | | |
105 | 4.95M | if (match_len >= WANT_MIN_MATCH) { |
106 | 4.95M | if (UNLIKELY(match_len > s->lookahead)) |
107 | 464 | match_len = s->lookahead; |
108 | | |
109 | 4.95M | Assert(match_len <= STD_MAX_MATCH, "match too long"); |
110 | 4.95M | Assert(s->strstart <= UINT16_MAX, "strstart should fit in uint16_t"); |
111 | 4.95M | check_match(s, s->strstart, hash_head, match_len); |
112 | | |
113 | 4.95M | zng_tr_emit_dist(s, static_ltree, static_dtree, match_len - STD_MIN_MATCH, (uint32_t)dist); |
114 | 4.95M | s->lookahead -= match_len; |
115 | 4.95M | s->strstart += match_len; |
116 | 4.95M | continue; |
117 | 4.95M | } |
118 | 4.95M | } |
119 | 95.4M | } |
120 | 196M | } else { |
121 | 14.1k | lc = window[s->strstart]; |
122 | 14.1k | } |
123 | 191M | zng_tr_emit_lit(s, static_ltree, lc); |
124 | 191M | s->strstart++; |
125 | 191M | s->lookahead--; |
126 | 191M | } |
127 | | |
128 | 6.71k | s->insert = s->strstart < (STD_MIN_MATCH - 1) ? s->strstart : (STD_MIN_MATCH - 1); |
129 | 6.71k | if (UNLIKELY(last)) { |
130 | 6.71k | if (quick_end_block(s, 1)) |
131 | 271 | return finish_started; |
132 | 6.44k | return finish_done; |
133 | 6.71k | } |
134 | | |
135 | 0 | if (quick_end_block(s, 0)) |
136 | 0 | return need_more; |
137 | 0 | return block_done; |
138 | 0 | } |