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
26extern const ct_data static_ltree[L_CODES+2];
27extern 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
46Z_INTERNAL block_state deflate_quick(deflate_state *s, int flush) {
47 Pos hash_head;
48 int64_t dist;
49 unsigned match_len, last;
50
51
52 last = (flush == Z_FINISH) ? 1 : 0;
53 if (UNLIKELY(last && s->block_open != 2)) {
54 /* Emit end of previous block */
55 QUICK_END_BLOCK(s, 0);
56 /* Emit start of last block */
57 QUICK_START_BLOCK(s, last);
58 } 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 QUICK_START_BLOCK(s, last);
62 }
63
64 for (;;) {
65 if (UNLIKELY(s->pending + ((BIT_BUF_SIZE + 7) >> 3) >= s->pending_buf_size)) {
66 flush_pending(strm: s->strm);
67 if (s->strm->avail_out == 0) {
68 return (last && s->strm->avail_in == 0 && s->bi_valid == 0 && s->block_open == 0) ? finish_started : need_more;
69 }
70 }
71
72 if (UNLIKELY(s->lookahead < MIN_LOOKAHEAD)) {
73 fill_window(s);
74 if (UNLIKELY(s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH)) {
75 return need_more;
76 }
77 if (UNLIKELY(s->lookahead == 0))
78 break;
79
80 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 QUICK_START_BLOCK(s, last);
84 }
85 }
86
87 if (LIKELY(s->lookahead >= MIN_MATCH)) {
88 hash_head = functable.quick_insert_string(s, s->strstart);
89 dist = (int64_t)s->strstart - hash_head;
90
91 if (dist <= MAX_DIST(s) && dist > 0) {
92 match_len = functable.compare258(s->window + s->strstart, s->window + hash_head);
93
94 if (match_len >= MIN_MATCH) {
95 if (UNLIKELY(match_len > s->lookahead))
96 match_len = s->lookahead;
97
98 check_match(s, s->strstart, hash_head, match_len);
99
100 zng_tr_emit_dist(s, ltree: static_ltree, dtree: static_dtree, lc: match_len - MIN_MATCH, dist: (uint32_t)dist);
101 s->lookahead -= match_len;
102 s->strstart += match_len;
103 continue;
104 }
105 }
106 }
107
108 zng_tr_emit_lit(s, ltree: static_ltree, c: s->window[s->strstart]);
109 s->strstart++;
110 s->lookahead--;
111 }
112
113 s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1;
114 if (UNLIKELY(last)) {
115 QUICK_END_BLOCK(s, 1);
116 return finish_done;
117 }
118
119 QUICK_END_BLOCK(s, 0);
120 return block_done;
121}
122