Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 15 additions & 19 deletions Deflate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ internal Config(int good_length, int max_lazy, int nice_length, int max_chain, i
// Depth of each subtree used as tie breaker for trees of equal frequency
internal byte[] depth = new byte[2 * L_CODES + 1];

internal int l_buf; // index for literals or lengths */
internal int symbol_buf; // triples: high distance, low distance, literal/length

// Size of match buffer for literals/lengths. There are 4 reasons for
// limiting lit_bufsize to 64K:
Expand All @@ -283,14 +283,9 @@ internal Config(int good_length, int max_lazy, int nice_length, int max_chain, i
// - I can't count above 4
internal int lit_bufsize;

internal int last_lit; // running index in l_buf

// Buffer for distances. To simplify the code, d_buf and l_buf have
// the same number of elements. To use different lengths, an extra flag
// array would be necessary.

internal int d_buf; // index of pendig_buf
internal int last_lit; // number of buffered symbols


internal int opt_len; // bit length of current block with optimal trees
internal int static_len; // bit length of current block with static trees
internal int matches; // number of string matches in current block
Expand Down Expand Up @@ -660,10 +655,10 @@ internal void _tr_align()
internal bool _tr_tally(int dist, int lc)
{

pending_buf[d_buf + last_lit * 2] = (byte) (SupportClass.URShift(dist, 8));
pending_buf[d_buf + last_lit * 2 + 1] = (byte) dist;
pending_buf[symbol_buf + last_lit * 3] = (byte) (SupportClass.URShift(dist, 8));
pending_buf[symbol_buf + last_lit * 3 + 1] = (byte) dist;

pending_buf[l_buf + last_lit] = (byte) lc; last_lit++;
pending_buf[symbol_buf + last_lit * 3 + 2] = (byte) lc; last_lit++;

if (dist == 0)
{
Expand Down Expand Up @@ -705,16 +700,16 @@ internal void compress_block(short[] ltree, short[] dtree)
{
int dist; // distance of matched string
int lc; // match length or unmatched char (if dist == 0)
int lx = 0; // running index in l_buf
int lx = 0; // running symbol index
int code; // the code to send
int extra; // number of extra bits to send

if (last_lit != 0)
{
do
{
dist = ((pending_buf[d_buf + lx * 2] << 8) & 0xff00) | (pending_buf[d_buf + lx * 2 + 1] & 0xff);
lc = (pending_buf[l_buf + lx]) & 0xff; lx++;
dist = ((pending_buf[symbol_buf + lx * 3] << 8) & 0xff00) | (pending_buf[symbol_buf + lx * 3 + 1] & 0xff);
lc = (pending_buf[symbol_buf + lx * 3 + 2]) & 0xff; lx++;

if (dist == 0)
{
Expand Down Expand Up @@ -744,7 +739,7 @@ internal void compress_block(short[] ltree, short[] dtree)
}
} // literal or match pair ?

// Check that the overlay between pending_buf and d_buf+l_buf is ok:
// Consuming a symbol frees three bytes before the next unread symbol.
}
while (lx < last_lit);
}
Expand Down Expand Up @@ -1502,13 +1497,14 @@ internal int deflateInit2(ZStream strm, int level, int method, int windowBits, i

lit_bufsize = 1 << (memLevel + 6); // 16K elements by default

// We overlay pending_buf and d_buf+l_buf. This works since the average
// output size for (length,distance) codes is <= 24 bits.
// Interleave three-byte symbols in the last 3/4 of the pending buffer.
// Each consumed symbol frees three bytes; a fixed code uses at most 31 bits.
// With one byte of headroom per symbol, output cannot overtake unread symbols.
// Separate distance and literal regions did not satisfy this invariant.
pending_buf = new byte[lit_bufsize * 4];
pending_buf_size = lit_bufsize * 4;

d_buf = lit_bufsize;
l_buf = (1 + 2) * lit_bufsize;
symbol_buf = lit_bufsize;

this.level = level;

Expand Down
29 changes: 29 additions & 0 deletions InfBlocks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ internal sealed class InfBlocks
internal int bitb; // bit buffer
internal int[] hufts; // single malloc for tree space
internal byte[] window; // sliding window
internal int history; // initialized history bytes, including a preset dictionary
internal int end; // one byte after sliding window
internal int read; // window read pointer
internal int write; // window write pointer
Expand Down Expand Up @@ -127,6 +128,7 @@ internal void reset(ZStream z, long[] c)
bitk = 0;
bitb = 0;
read = write = 0;
history = 0;

if (checkfn != null)
z.adler = check = Adler32.adler32(0L, null, 0, 0);
Expand Down Expand Up @@ -316,6 +318,7 @@ internal int proc(ZStream z, int r)
if (t > m)
t = m;
Array.Copy(z.next_in, p, window, q, t);
AddHistory(t);
p += t; n -= t;
q += t; m -= t;
if ((left -= t) != 0)
Expand Down Expand Up @@ -631,10 +634,36 @@ internal void free(ZStream z)
//ZFREE(z, s);
}

// Copy an expanding DEFLATE match into a contiguous free section of the window.
// Seed at most one period from history (which may wrap), then double already-produced
// bytes. Each Array.Copy reads initialized bytes and never needs memmove-style expansion.
internal void CopyMatch(int destination, int distance, int count)
{
int source = destination - distance;
if (source < 0) source += end;
int seed = Math.Min(distance, count);
int first = Math.Min(seed, end - source);
Array.Copy(window, source, window, destination, first);
if (first < seed) Array.Copy(window, 0, window, destination + first, seed - first);
for (int copied = seed; copied < count;)
{
int next = Math.Min(copied, count - copied);
Array.Copy(window, destination, window, destination + copied, next);
copied += next;
}
AddHistory(count);
}

internal void AddHistory(int count)
{
history = Math.Min(end, history + count);
}

internal void set_dictionary(byte[] d, int start, int n)
{
Array.Copy(d, start, window, 0, n);
read = write = n;
history = n;
}

// Returns true if inflate is currently at the end of a block generated
Expand Down
106 changes: 32 additions & 74 deletions InfCodes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ internal int proc(InfBlocks s, ZStream z, int r)
int n; // bytes available there
int q; // output window write pointer
int m; // bytes to end of window or read pointer
int f; // pointer to copy strings from

// copy input/output information to locals (UPDATE macro restores)
p = z.next_in_index; n = z.avail_in; b = s.bitb; k = s.bitk;
Expand Down Expand Up @@ -177,6 +176,11 @@ internal int proc(InfBlocks s, ZStream z, int r)
r = Z_OK;
else
{
// A terminal entry can require fewer bits than the table lookup width.
// With no more input, decode only if the entry is fully buffered.
// Same correction as SharpCompress PR #118 (LEN and DIST).
int partial = (tree_index + (b & inflate_mask[k])) * 3;
if (k >= tree[partial + 1]) break;

s.bitb = b; s.bitk = k;
z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
Expand Down Expand Up @@ -272,6 +276,11 @@ internal int proc(InfBlocks s, ZStream z, int r)
r = Z_OK;
else
{
// A terminal entry can require fewer bits than the table lookup width.
// With no more input, decode only if the entry is fully buffered.
// Same correction as SharpCompress PR #118 (LEN and DIST).
int partial = (tree_index + (b & inflate_mask[k])) * 3;
if (k >= tree[partial + 1]) break;

s.bitb = b; s.bitk = k;
z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
Expand Down Expand Up @@ -337,16 +346,16 @@ internal int proc(InfBlocks s, ZStream z, int r)
b >>= j;
k -= j;

if (dist > s.history)
{
z.msg = "invalid distance too far back";
mode = BADCODE;
goto case BADCODE;
}
mode = COPY;
goto case COPY;

case COPY: // o: copying bytes in window, waiting for space
f = q - dist;
while (f < 0)
{
// modulo window size-"while" instead
f += s.end; // of "if" handles invalid distances
}
while (len != 0)
{

Expand Down Expand Up @@ -376,11 +385,9 @@ internal int proc(InfBlocks s, ZStream z, int r)
}
}

s.window[q++] = s.window[f++]; m--;

if (f == s.end)
f = 0;
len--;
int copied = Math.Min(len, m);
s.CopyMatch(q, dist, copied);
q += copied; m -= copied; len -= copied;
}
mode = START;
break;
Expand Down Expand Up @@ -413,6 +420,7 @@ internal int proc(InfBlocks s, ZStream z, int r)
r = Z_OK;

s.window[q++] = (byte) lit; m--;
s.AddHistory(1);

mode = START;
break;
Expand Down Expand Up @@ -496,7 +504,6 @@ internal static int inflate_fast(int bl, int bd, int[] tl, int tl_index, int[] t
int md; // mask for distance tree
int c; // bytes to copy
int d; // distance back to copy from
int r; // copy source pointer

// load input, output, bit values
p = z.next_in_index; n = z.avail_in; b = s.bitb; k = s.bitk;
Expand Down Expand Up @@ -526,6 +533,7 @@ internal static int inflate_fast(int bl, int bd, int[] tl, int tl_index, int[] t
b >>= (tp[(tp_index + t) * 3 + 1]); k -= (tp[(tp_index + t) * 3 + 1]);

s.window[q++] = (byte) tp[(tp_index + t) * 3 + 2];
s.AddHistory(1);
m--;
continue;
}
Expand Down Expand Up @@ -574,69 +582,18 @@ internal static int inflate_fast(int bl, int bd, int[] tl, int tl_index, int[] t

b >>= (e); k -= (e);

// do the copy
m -= c;
if (q >= d)
if (d > s.history)
{
// offset before dest
// just copy
r = q - d;
if (q - r > 0 && 2 > (q - r))
{
s.window[q++] = s.window[r++]; c--; // minimum count is three,
s.window[q++] = s.window[r++]; c--; // so unroll loop a little
}
else
{
Array.Copy(s.window, r, s.window, q, 2);
q += 2; r += 2; c -= 2;
}
}
else
{
// else offset after destination
r = q - d;
do
{
r += s.end; // force pointer in window
}
while (r < 0); // covers invalid distances
e = s.end - r;
if (c > e)
{
// if source crosses,
c -= e; // wrapped copy
if (q - r > 0 && e > (q - r))
{
do
{
s.window[q++] = s.window[r++];
}
while (--e != 0);
}
else
{
Array.Copy(s.window, r, s.window, q, e);
q += e; r += e; e = 0;
}
r = 0; // copy rest from start of window
}
}

// copy all or what's left
if (q - r > 0 && c > (q - r))
{
do
{
s.window[q++] = s.window[r++];
}
while (--c != 0);
}
else
{
Array.Copy(s.window, r, s.window, q, c);
q += c; r += c; c = 0;
z.msg = "invalid distance too far back";
c = z.avail_in - n; c = Math.Min(k >> 3, c); n += c; p -= c; k -= c << 3;
s.bitb = b; s.bitk = k;
z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
s.write = q;
return Z_DATA_ERROR;
}
s.CopyMatch(q, d, c);
m -= c; q += c;
c = 0;
break;
}
else if ((e & 64) == 0)
Expand Down Expand Up @@ -672,6 +629,7 @@ internal static int inflate_fast(int bl, int bd, int[] tl, int tl_index, int[] t
b >>= (tp[(tp_index + t) * 3 + 1]); k -= (tp[(tp_index + t) * 3 + 1]);

s.window[q++] = (byte) tp[(tp_index + t) * 3 + 2];
s.AddHistory(1);
m--;
break;
}
Expand Down
Loading
Loading