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