/src/c-blosc2/internal-complibs/zlib-ng-2.0.7/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 "deflate.h" |
22 | | #include "deflate_p.h" |
23 | | #include "functable.h" |
24 | | #include "trees_emit.h" |
25 | | |
26 | | extern const ct_data static_ltree[L_CODES+2]; |
27 | | extern const ct_data static_dtree[D_CODES]; |
28 | | |
29 | 6.57k | #define QUICK_START_BLOCK(s, last) { \ |
30 | 6.57k | zng_tr_emit_tree(s, STATIC_TREES, last); \ |
31 | 6.57k | s->block_open = 1 + (int)last; \ |
32 | 6.57k | s->block_start = (int)s->strstart; \ |
33 | 6.57k | } |
34 | | |
35 | 13.1k | #define QUICK_END_BLOCK(s, last) { \ |
36 | 13.1k | if (s->block_open) { \ |
37 | 6.57k | zng_tr_emit_end_block(s, static_ltree, last); \ |
38 | 6.57k | s->block_open = 0; \ |
39 | 6.57k | s->block_start = (int)s->strstart; \ |
40 | 6.57k | flush_pending(s->strm); \ |
41 | 6.57k | if (s->strm->avail_out == 0) \ |
42 | 6.57k | return (last) ? finish_started : need_more; \ |
43 | 6.57k | } \ |
44 | 13.1k | } |
45 | | |
46 | 6.57k | Z_INTERNAL block_state deflate_quick(deflate_state *s, int flush) { |
47 | 6.57k | Pos hash_head; |
48 | 6.57k | int64_t dist; |
49 | 6.57k | unsigned match_len, last; |
50 | | |
51 | | |
52 | 6.57k | last = (flush == Z_FINISH) ? 1 : 0; |
53 | 6.57k | if (UNLIKELY(last && s->block_open != 2)) { |
54 | | /* Emit end of previous block */ |
55 | 6.57k | QUICK_END_BLOCK(s, 0); |
56 | | /* Emit start of last block */ |
57 | 6.57k | QUICK_START_BLOCK(s, last); |
58 | 6.57k | } 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 | 17.3M | for (;;) { |
65 | 17.3M | if (UNLIKELY(s->pending + ((BIT_BUF_SIZE + 7) >> 3) >= s->pending_buf_size)) { |
66 | 0 | flush_pending(s->strm); |
67 | 0 | if (s->strm->avail_out == 0) { |
68 | 0 | return (last && s->strm->avail_in == 0 && s->bi_valid == 0 && s->block_open == 0) ? finish_started : need_more; |
69 | 0 | } |
70 | 0 | } |
71 | | |
72 | 17.3M | if (UNLIKELY(s->lookahead < MIN_LOOKAHEAD)) { |
73 | 1.05M | fill_window(s); |
74 | 1.05M | if (UNLIKELY(s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH)) { |
75 | 0 | return need_more; |
76 | 0 | } |
77 | 1.05M | if (UNLIKELY(s->lookahead == 0)) |
78 | 6.57k | break; |
79 | | |
80 | 1.04M | if (UNLIKELY(s->block_open == 0)) { |
81 | | /* Start new block when we have lookahead data, so that if no |
82 | | input data is given an empty block will not be written */ |
83 | 0 | QUICK_START_BLOCK(s, last); |
84 | 0 | } |
85 | 1.04M | } |
86 | | |
87 | 17.3M | if (LIKELY(s->lookahead >= MIN_MATCH)) { |
88 | 17.3M | hash_head = functable.quick_insert_string(s, s->strstart); |
89 | 17.3M | dist = (int64_t)s->strstart - hash_head; |
90 | | |
91 | 17.3M | if (dist <= MAX_DIST(s) && dist > 0) { |
92 | 17.3M | match_len = functable.compare258(s->window + s->strstart, s->window + hash_head); |
93 | | |
94 | 17.3M | if (match_len >= MIN_MATCH) { |
95 | 219k | if (UNLIKELY(match_len > s->lookahead)) |
96 | 939 | match_len = s->lookahead; |
97 | | |
98 | 219k | check_match(s, s->strstart, hash_head, match_len); |
99 | | |
100 | 219k | zng_tr_emit_dist(s, static_ltree, static_dtree, match_len - MIN_MATCH, (uint32_t)dist); |
101 | 219k | s->lookahead -= match_len; |
102 | 219k | s->strstart += match_len; |
103 | 219k | continue; |
104 | 219k | } |
105 | 17.3M | } |
106 | 17.3M | } |
107 | | |
108 | 17.0M | zng_tr_emit_lit(s, static_ltree, s->window[s->strstart]); |
109 | 17.0M | s->strstart++; |
110 | 17.0M | s->lookahead--; |
111 | 17.0M | } |
112 | | |
113 | 6.57k | s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1; |
114 | 6.57k | if (UNLIKELY(last)) { |
115 | 6.57k | QUICK_END_BLOCK(s, 1); |
116 | 3.13k | return finish_done; |
117 | 6.57k | } |
118 | | |
119 | 0 | QUICK_END_BLOCK(s, 0); |
120 | 0 | return block_done; |
121 | 0 | } |