summaryrefslogtreecommitdiff
path: root/deflate_medium.c
diff options
context:
space:
mode:
Diffstat (limited to 'deflate_medium.c')
-rw-r--r--deflate_medium.c94
1 files changed, 44 insertions, 50 deletions
diff --git a/deflate_medium.c b/deflate_medium.c
index c6b53df..c3cd1a2 100644
--- a/deflate_medium.c
+++ b/deflate_medium.c
@@ -10,32 +10,29 @@
#include "deflate.h"
struct match {
- uInt match_start;
- uInt match_length;
- uInt strstart;
- uInt orgstart;
+ uInt match_start;
+ uInt match_length;
+ uInt strstart;
+ uInt orgstart;
};
#define MAX_DIST2 ((1 << MAX_WBITS) - MIN_LOOKAHEAD)
-static int tr_tally_dist(deflate_state *s, int distance, int length)
-{
+static int tr_tally_dist(deflate_state *s, int distance, int length) {
return _tr_tally(s, distance, length);
}
-static int tr_tally_lit(deflate_state *s, int c)
-{
+static int tr_tally_lit(deflate_state *s, int c) {
return _tr_tally(s, 0, c);
}
-static int emit_match(deflate_state *s, struct match match)
-{
+static int emit_match(deflate_state *s, struct match match) {
int flush = 0;
-
+
/* matches that are not long enough we need to emit as litterals */
if (match.match_length < MIN_MATCH) {
while (match.match_length) {
- flush += tr_tally_lit (s, s->window[match.strstart]);
+ flush += tr_tally_lit(s, s->window[match.strstart]);
s->lookahead--;
match.strstart++;
match.match_length--;
@@ -45,15 +42,13 @@ static int emit_match(deflate_state *s, struct match match)
check_match(s, match.strstart, match.match_start, match.match_length);
- flush += tr_tally_dist(s, match.strstart - match.match_start,
- match.match_length - MIN_MATCH);
+ flush += tr_tally_dist(s, match.strstart - match.match_start, match.match_length - MIN_MATCH);
s->lookahead -= match.match_length;
return flush;
}
-static void insert_match(deflate_state *s, struct match match)
-{
+static void insert_match(deflate_state *s, struct match match) {
if (unlikely(s->lookahead <= match.match_length + MIN_MATCH))
return;
@@ -75,8 +70,7 @@ static void insert_match(deflate_state *s, struct match match)
/* Insert new strings in the hash table only if the match length
* is not too large. This saves time but degrades compression.
*/
- if (match.match_length <= 16* s->max_insert_length &&
- s->lookahead >= MIN_MATCH) {
+ if (match.match_length <= 16* s->max_insert_length && s->lookahead >= MIN_MATCH) {
match.match_length--; /* string at strstart already in table */
do {
match.strstart++;
@@ -103,20 +97,19 @@ static void insert_match(deflate_state *s, struct match match)
}
}
-static void fizzle_matches(deflate_state *s, struct match *current, struct match *next)
-{
+static void fizzle_matches(deflate_state *s, struct match *current, struct match *next) {
IPos limit;
unsigned char *match, *orig;
int changed = 0;
- struct match c,n;
+ struct match c, n;
/* step zero: sanity checks */
if (current->match_length <= 1)
return;
- match = s->window - current->match_length + 1 + next->match_start ;
- orig = s->window - current->match_length + 1 + next->strstart ;
-
+ match = s->window - current->match_length + 1 + next->match_start;
+ orig = s->window - current->match_length + 1 + next->strstart;
+
/* quick exit check.. if this fails then don't bother with anything else */
if (likely(*match != *orig))
return;
@@ -127,13 +120,13 @@ static void fizzle_matches(deflate_state *s, struct match *current, struct match
*/
if (next->match_start + next->match_length >= current->strstart)
return;
-
+
c = *current;
n = *next;
/* step one: try to move the "next" match to the left as much as possible */
limit = next->strstart > MAX_DIST2 ? next->strstart - MAX_DIST2 : 0;
-
+
match = s->window + n.match_start - 1;
orig = s->window + n.strstart - 1;
@@ -153,30 +146,31 @@ static void fizzle_matches(deflate_state *s, struct match *current, struct match
c.match_length--;
match--;
orig--;
- changed ++;
+ changed++;
}
if (!changed)
return;
- if ( (c.match_length <= 1) && n.match_length != 2) {
+ if (c.match_length <= 1 && n.match_length != 2) {
n.orgstart++;
*current = c;
*next = n;
- } else return;
+ } else {
+ return;
+ }
}
-block_state deflate_medium(deflate_state *s, int flush)
-{
+block_state deflate_medium(deflate_state *s, int flush) {
struct match current_match, next_match;
-
+
memset(&current_match, 0, sizeof(struct match));
memset(&next_match, 0, sizeof(struct match));
for (;;) {
IPos hash_head = 0; /* head of the hash chain */
int bflush; /* set if current block must be flushed */
-
+
/* Make sure that we always have enough lookahead, except
* at the end of the input file. We need MAX_MATCH bytes
* for the next match, plus MIN_MATCH bytes to insert the
@@ -195,7 +189,7 @@ block_state deflate_medium(deflate_state *s, int flush)
/* Insert the string window[strstart .. strstart+2] in the
* dictionary, and set hash_head to the head of the hash chain:
*/
-
+
/* If we already have a future match from a previous round, just use that */
if (next_match.match_length > 0) {
current_match = next_match;
@@ -206,40 +200,40 @@ block_state deflate_medium(deflate_state *s, int flush)
if (s->lookahead >= MIN_MATCH) {
hash_head = insert_string(s, s->strstart);
}
-
+
/* set up the initial match to be a 1 byte literal */
current_match.match_start = 0;
current_match.match_length = 1;
current_match.strstart = s->strstart;
current_match.orgstart = current_match.strstart;
-
+
/* Find the longest match, discarding those <= prev_length.
* At this point we have always match_length < MIN_MATCH
*/
-
+
if (hash_head != 0 && s->strstart - hash_head <= MAX_DIST2) {
/* To simplify the code, we prevent matches with the string
* of window index 0 (in particular we have to avoid a match
* of the string with itself at the start of the input file).
*/
- current_match.match_length = longest_match (s, hash_head);
+ current_match.match_length = longest_match(s, hash_head);
current_match.match_start = s->match_start;
if (current_match.match_length < MIN_MATCH)
current_match.match_length = 1;
- if (current_match.match_start >= current_match.strstart) {
+ if (current_match.match_start >= current_match.strstart) {
/* this can happen due to some restarts */
current_match.match_length = 1;
}
}
}
-
+
insert_match(s, current_match);
/* now, look ahead one */
if (s->lookahead > MIN_LOOKAHEAD) {
s->strstart = current_match.strstart + current_match.match_length;
hash_head = insert_string(s, s->strstart);
-
+
/* set up the initial match to be a 1 byte literal */
next_match.match_start = 0;
next_match.match_length = 1;
@@ -254,7 +248,7 @@ block_state deflate_medium(deflate_state *s, int flush)
* of window index 0 (in particular we have to avoid a match
* of the string with itself at the start of the input file).
*/
- next_match.match_length = longest_match (s, hash_head);
+ next_match.match_length = longest_match(s, hash_head);
next_match.match_start = s->match_start;
if (next_match.match_start >= next_match.strstart)
/* this can happen due to some restarts */
@@ -264,23 +258,22 @@ block_state deflate_medium(deflate_state *s, int flush)
else
fizzle_matches(s, &current_match, &next_match);
}
-
+
/* short matches with a very long distance are rarely a good idea encoding wise */
- if (next_match.match_length == 3 &&
- (next_match.strstart - next_match.match_start) > 12000)
+ if (next_match.match_length == 3 && (next_match.strstart - next_match.match_start) > 12000)
next_match.match_length = 1;
s->strstart = current_match.strstart;
-
+
} else {
next_match.match_length = 0;
}
-
+
/* now emit the current match */
bflush = emit_match(s, current_match);
-
+
/* move the "cursor" forward */
- s->strstart += current_match.match_length;
-
+ s->strstart += current_match.match_length;
+
if (bflush)
FLUSH_BLOCK(s, 0);
}
@@ -291,5 +284,6 @@ block_state deflate_medium(deflate_state *s, int flush)
}
if (s->last_lit)
FLUSH_BLOCK(s, 0);
+
return block_done;
}