mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
Sync Compress and RVIO with latest from RVWorld
This commit is contained in:
@@ -1,613 +0,0 @@
|
||||
/*
|
||||
Copyright (c) 1990-2007 Info-ZIP. All rights reserved.
|
||||
See the accompanying file LICENSE, version 2007-Mar-04 or later
|
||||
(the contents of which are also included in unzip.h) for terms of use.
|
||||
If, for some reason, all these files are missing, the Info-ZIP license
|
||||
also may be found at: ftp://ftp.info-zip.org/pub/infozip/license.html
|
||||
*/
|
||||
/* explode.c -- by Mark Adler
|
||||
version c17d, 01 December 2007 */
|
||||
|
||||
|
||||
/* Copyright history:
|
||||
- Starting with UnZip 5.41 of 16-April-2000, this source file
|
||||
is covered by the Info-Zip LICENSE cited above.
|
||||
- Prior versions of this source file, found in UnZip source packages
|
||||
up to UnZip 5.40, were put in the public domain.
|
||||
The original copyright note by Mark Adler was:
|
||||
"You can do whatever you like with this source file,
|
||||
though I would prefer that if you modify it and
|
||||
redistribute it that you include comments to that effect
|
||||
with your name and the date. Thank you."
|
||||
History:
|
||||
vers date who what
|
||||
---- --------- -------------- ------------------------------------
|
||||
c1 30 Mar 92 M. Adler explode that uses huft_build from inflate
|
||||
(this gives over a 70% speed improvement
|
||||
over the original unimplode.c, which
|
||||
decoded a bit at a time)
|
||||
c2 4 Apr 92 M. Adler fixed bug for file sizes a multiple of 32k.
|
||||
c3 10 Apr 92 M. Adler added a little memory tracking if DEBUG
|
||||
c4 11 Apr 92 M. Adler added NOMEMCPY do kill use of memcpy()
|
||||
c5 21 Apr 92 M. Adler added the WSIZE #define to allow reducing
|
||||
the 32K window size for specialized
|
||||
applications.
|
||||
c6 31 May 92 M. Adler added typecasts to eliminate some warnings
|
||||
c7 27 Jun 92 G. Roelofs added more typecasts.
|
||||
c8 17 Oct 92 G. Roelofs changed ULONG/UWORD/byte to ulg/ush/uch.
|
||||
c9 19 Jul 93 J. Bush added more typecasts (to return values);
|
||||
made l[256] array static for Amiga.
|
||||
c10 8 Oct 93 G. Roelofs added used_csize for diagnostics; added
|
||||
buf and unshrink arguments to flush();
|
||||
undef'd various macros at end for Turbo C;
|
||||
removed NEXTBYTE macro (now in unzip.h)
|
||||
and bytebuf variable (not used); changed
|
||||
memset() to memzero().
|
||||
c11 9 Jan 94 M. Adler fixed incorrect used_csize calculation.
|
||||
c12 9 Apr 94 G. Roelofs fixed split comments on preprocessor lines
|
||||
to avoid bug in Encore compiler.
|
||||
c13 25 Aug 94 M. Adler fixed distance-length comment (orig c9 fix)
|
||||
c14 22 Nov 95 S. Maxwell removed unnecessary "static" on auto array
|
||||
c15 6 Jul 96 W. Haidinger added ulg typecasts to flush() calls.
|
||||
c16 8 Feb 98 C. Spieler added ZCONST modifiers to const tables
|
||||
and #ifdef DEBUG around debugging code.
|
||||
c16b 25 Mar 98 C. Spieler modified DLL code for slide redirection.
|
||||
c16d 05 Jul 99 C. Spieler take care of flush() return values and
|
||||
stop processing in case of errors
|
||||
c17 04 Feb 01 C. Spieler reorganized code to reduce repetitions
|
||||
of large code parts; adapted huft decoding
|
||||
to the changes in inflate's huft_build()
|
||||
due to support of deflate64; fixed memory
|
||||
leaks (huft tables were not free'd when
|
||||
get_tree() failed).
|
||||
c17b 16 Feb 02 C. Spieler changed type of the "extra lengths" array
|
||||
"extra" from ush into uch (to save space)
|
||||
c17c 10 Aug 04 NN file sizes use zoff_t.
|
||||
c17d 01 Dec 07 C. Spieler type for file sizes changed from zoff_t
|
||||
into zusz_t.
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
Explode imploded (PKZIP method 6 compressed) data. This compression
|
||||
method searches for as much of the current string of bytes (up to a length
|
||||
of ~320) in the previous 4K or 8K bytes. If it doesn't find any matches
|
||||
(of at least length 2 or 3), it codes the next byte. Otherwise, it codes
|
||||
the length of the matched string and its distance backwards from the
|
||||
current position. Single bytes ("literals") are preceded by a one (a
|
||||
single bit) and are either uncoded (the eight bits go directly into the
|
||||
compressed stream for a total of nine bits) or Huffman coded with a
|
||||
supplied literal code tree. If literals are coded, then the minimum match
|
||||
length is three, otherwise it is two.
|
||||
There are therefore four kinds of imploded streams: 8K search with coded
|
||||
literals (min match = 3), 4K search with coded literals (min match = 3),
|
||||
8K with uncoded literals (min match = 2), and 4K with uncoded literals
|
||||
(min match = 2). The kind of stream is identified in two bits of a
|
||||
general purpose bit flag that is outside of the compressed stream.
|
||||
Distance-length pairs for matched strings are preceded by a zero bit (to
|
||||
distinguish them from literals) and are always coded. The distance comes
|
||||
first and is either the low six (4K) or low seven (8K) bits of the
|
||||
distance (uncoded), followed by the high six bits of the distance coded.
|
||||
Then the length is six bits coded (0..63 + min match length), and if the
|
||||
maximum such length is coded, then it's followed by another eight bits
|
||||
(uncoded) to be added to the coded length. This gives a match length
|
||||
range of 2..320 or 3..321 bytes.
|
||||
The literal, length, and distance codes are all represented in a slightly
|
||||
compressed form themselves. What is sent are the lengths of the codes for
|
||||
each value, which is sufficient to construct the codes. Each byte of the
|
||||
code representation is the code length (the low four bits representing
|
||||
1..16), and the number of values sequentially with that length (the high
|
||||
four bits also representing 1..16). There are 256 literal code values (if
|
||||
literals are coded), 64 length code values, and 64 distance code values,
|
||||
in that order at the beginning of the compressed stream. Each set of code
|
||||
values is preceded (redundantly) with a byte indicating how many bytes are
|
||||
in the code description that follows, in the range 1..256.
|
||||
The codes themselves are decoded using tables made by huft_build() from
|
||||
the bit lengths. That routine and its comments are in the inflate.c
|
||||
module.
|
||||
*/
|
||||
|
||||
#define __EXPLODE_C /* identifies this source module */
|
||||
#define UNZIP_INTERNAL
|
||||
#include "unzip.h" /* must supply slide[] (uch) array and NEXTBYTE macro */
|
||||
|
||||
#ifndef WSIZE
|
||||
# define WSIZE 0x8000 /* window size--must be a power of two, and */
|
||||
#endif /* at least 8K for zip's implode method */
|
||||
|
||||
#if (defined(DLL) && !defined(NO_SLIDE_REDIR))
|
||||
# define wszimpl (unsigned)(G._wsize)
|
||||
#else
|
||||
# if defined(USE_DEFLATE64) && defined(INT_16BIT)
|
||||
# define wszimpl (unsigned)(WSIZE>>1)
|
||||
# else /* !(USE_DEFLATE64 && INT_16BIT) */
|
||||
# define wszimpl WSIZE
|
||||
# endif /* !(USE_DEFLATE64 && INT_16BIT) */
|
||||
#endif
|
||||
|
||||
/* routines here */
|
||||
static int get_tree OF((__GPRO__ unsigned* l, unsigned n));
|
||||
static int explode_lit OF((__GPRO__ struct huft* tb, struct huft* tl,
|
||||
struct huft* td, unsigned bb, unsigned bl,
|
||||
unsigned bd, unsigned bdl));
|
||||
static int explode_nolit OF((__GPRO__ struct huft* tl, struct huft* td,
|
||||
unsigned bl, unsigned bd, unsigned bdl));
|
||||
int explode OF((__GPRO));
|
||||
|
||||
|
||||
/* The implode algorithm uses a sliding 4K or 8K byte window on the
|
||||
uncompressed stream to find repeated byte strings. This is implemented
|
||||
here as a circular buffer. The index is updated simply by incrementing
|
||||
and then and'ing with 0x0fff (4K-1) or 0x1fff (8K-1). Here, the 32K
|
||||
buffer of inflate is used, and it works just as well to always have
|
||||
a 32K circular buffer, so the index is anded with 0x7fff. This is
|
||||
done to allow the window to also be used as the output buffer. */
|
||||
/* This must be supplied in an external module useable like "uch slide[8192];"
|
||||
or "uch *slide;", where the latter would be malloc'ed. In unzip, slide[]
|
||||
is actually a 32K area for use by inflate, which uses a 32K sliding window.
|
||||
*/
|
||||
|
||||
|
||||
#define INVALID_CODE 99
|
||||
#define IS_INVALID_CODE(c) ((c) == INVALID_CODE)
|
||||
|
||||
/* Tables for length and distance */
|
||||
static ZCONST ush cplen2[] =
|
||||
{ 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
|
||||
18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34,
|
||||
35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
|
||||
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65 };
|
||||
static ZCONST ush cplen3[] =
|
||||
{ 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
|
||||
19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
|
||||
36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52,
|
||||
53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66 };
|
||||
static ZCONST uch extra[] =
|
||||
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
8 };
|
||||
static ZCONST ush cpdist4[] =
|
||||
{ 1, 65, 129, 193, 257, 321, 385, 449, 513, 577, 641, 705,
|
||||
769, 833, 897, 961, 1025, 1089, 1153, 1217, 1281, 1345, 1409, 1473,
|
||||
1537, 1601, 1665, 1729, 1793, 1857, 1921, 1985, 2049, 2113, 2177,
|
||||
2241, 2305, 2369, 2433, 2497, 2561, 2625, 2689, 2753, 2817, 2881,
|
||||
2945, 3009, 3073, 3137, 3201, 3265, 3329, 3393, 3457, 3521, 3585,
|
||||
3649, 3713, 3777, 3841, 3905, 3969, 4033 };
|
||||
static ZCONST ush cpdist8[] =
|
||||
{ 1, 129, 257, 385, 513, 641, 769, 897, 1025, 1153, 1281,
|
||||
1409, 1537, 1665, 1793, 1921, 2049, 2177, 2305, 2433, 2561, 2689,
|
||||
2817, 2945, 3073, 3201, 3329, 3457, 3585, 3713, 3841, 3969, 4097,
|
||||
4225, 4353, 4481, 4609, 4737, 4865, 4993, 5121, 5249, 5377, 5505,
|
||||
5633, 5761, 5889, 6017, 6145, 6273, 6401, 6529, 6657, 6785, 6913,
|
||||
7041, 7169, 7297, 7425, 7553, 7681, 7809, 7937, 8065 };
|
||||
|
||||
|
||||
/* Macros for inflate() bit peeking and grabbing.
|
||||
The usage is:
|
||||
NEEDBITS(j)
|
||||
x = b & mask_bits[j];
|
||||
DUMPBITS(j)
|
||||
where NEEDBITS makes sure that b has at least j bits in it, and
|
||||
DUMPBITS removes the bits from b. The macros use the variable k
|
||||
for the number of bits in b. Normally, b and k are register
|
||||
variables for speed.
|
||||
*/
|
||||
|
||||
#define NEEDBITS(n) {while(k<(n)){b|=((ulg)NEXTBYTE)<<k;k+=8;}}
|
||||
#define DUMPBITS(n) {b>>=(n);k-=(n);}
|
||||
|
||||
#define DECODEHUFT(htab, bits, mask) {\
|
||||
NEEDBITS((unsigned)(bits))\
|
||||
t = (htab) + ((~(unsigned)b)&(mask));\
|
||||
while (1) {\
|
||||
DUMPBITS(t->b)\
|
||||
if ((e=t->e) <= 32) break;\
|
||||
if (IS_INVALID_CODE(e)) return 1;\
|
||||
e &= 31;\
|
||||
NEEDBITS(e)\
|
||||
t = t->v.t + ((~(unsigned)b)&mask_bits[e]);\
|
||||
}\
|
||||
}
|
||||
|
||||
|
||||
static int get_tree(__G__ l, n)
|
||||
__GDEF
|
||||
unsigned* l; /* bit lengths */
|
||||
unsigned n; /* number expected */
|
||||
/* Get the bit lengths for a code representation from the compressed
|
||||
stream. If get_tree() returns 4, then there is an error in the data.
|
||||
Otherwise zero is returned. */
|
||||
{
|
||||
unsigned i; /* bytes remaining in list */
|
||||
unsigned k; /* lengths entered */
|
||||
unsigned j; /* number of codes */
|
||||
unsigned b; /* bit length for those codes */
|
||||
|
||||
|
||||
/* get bit lengths */
|
||||
i = NEXTBYTE + 1; /* length/count pairs to read */
|
||||
k = 0; /* next code */
|
||||
do {
|
||||
b = ((j = NEXTBYTE) & 0xf) + 1; /* bits in code (1..16) */
|
||||
j = ((j & 0xf0) >> 4) + 1; /* codes with those bits (1..16) */
|
||||
if (k + j > n)
|
||||
return 4; /* don't overflow l[] */
|
||||
do {
|
||||
l[k++] = b;
|
||||
} while (--j);
|
||||
} while (--i);
|
||||
return k != n ? 4 : 0; /* should have read n of them */
|
||||
}
|
||||
|
||||
|
||||
|
||||
static int explode_lit(__G__ tb, tl, td, bb, bl, bd, bdl)
|
||||
__GDEF
|
||||
struct huft* tb, * tl, * td; /* literal, length, and distance tables */
|
||||
unsigned bb, bl, bd; /* number of bits decoded by those */
|
||||
unsigned bdl; /* number of distance low bits */
|
||||
/* Decompress the imploded data using coded literals and a sliding
|
||||
window (of size 2^(6+bdl) bytes). */
|
||||
{
|
||||
zusz_t s; /* bytes to decompress */
|
||||
register unsigned e; /* table entry flag/number of extra bits */
|
||||
unsigned n, d; /* length and index for copy */
|
||||
unsigned w; /* current window position */
|
||||
struct huft* t; /* pointer to table entry */
|
||||
unsigned mb, ml, md; /* masks for bb, bl, and bd bits */
|
||||
unsigned mdl; /* mask for bdl (distance lower) bits */
|
||||
register ulg b; /* bit buffer */
|
||||
register unsigned k; /* number of bits in bit buffer */
|
||||
unsigned u; /* true if unflushed */
|
||||
int retval = 0; /* error code returned: initialized to "no error" */
|
||||
|
||||
|
||||
/* explode the coded data */
|
||||
b = k = w = 0; /* initialize bit buffer, window */
|
||||
u = 1; /* buffer unflushed */
|
||||
mb = mask_bits[bb]; /* precompute masks for speed */
|
||||
ml = mask_bits[bl];
|
||||
md = mask_bits[bd];
|
||||
mdl = mask_bits[bdl];
|
||||
s = G.lrec.ucsize;
|
||||
while (s > 0) /* do until ucsize bytes uncompressed */
|
||||
{
|
||||
NEEDBITS(1)
|
||||
if (b & 1) /* then literal--decode it */
|
||||
{
|
||||
DUMPBITS(1)
|
||||
s--;
|
||||
DECODEHUFT(tb, bb, mb) /* get coded literal */
|
||||
redirSlide[w++] = (uch)t->v.n;
|
||||
if (w == wszimpl)
|
||||
{
|
||||
if ((retval = flush(__G__ redirSlide, (ulg)w, 0)) != 0)
|
||||
return retval;
|
||||
w = u = 0;
|
||||
}
|
||||
}
|
||||
else /* else distance/length */
|
||||
{
|
||||
DUMPBITS(1)
|
||||
NEEDBITS(bdl) /* get distance low bits */
|
||||
d = (unsigned)b & mdl;
|
||||
DUMPBITS(bdl)
|
||||
DECODEHUFT(td, bd, md) /* get coded distance high bits */
|
||||
d = w - d - t->v.n; /* construct offset */
|
||||
DECODEHUFT(tl, bl, ml) /* get coded length */
|
||||
n = t->v.n;
|
||||
if (e) /* get length extra bits */
|
||||
{
|
||||
NEEDBITS(8)
|
||||
n += (unsigned)b & 0xff;
|
||||
DUMPBITS(8)
|
||||
}
|
||||
|
||||
/* do the copy */
|
||||
s = (s > (zusz_t)n ? s - (zusz_t)n : 0);
|
||||
do {
|
||||
#if (defined(DLL) && !defined(NO_SLIDE_REDIR))
|
||||
if (G.redirect_slide) {
|
||||
/* &= w/ wszimpl not needed and wrong if redirect */
|
||||
if (d >= wszimpl)
|
||||
return 1;
|
||||
e = wszimpl - (d > w ? d : w);
|
||||
}
|
||||
else
|
||||
#endif
|
||||
e = wszimpl - ((d &= wszimpl - 1) > w ? d : w);
|
||||
if (e > n) e = n;
|
||||
n -= e;
|
||||
if (u && w <= d)
|
||||
{
|
||||
memzero(redirSlide + w, e);
|
||||
w += e;
|
||||
d += e;
|
||||
}
|
||||
else
|
||||
#ifndef NOMEMCPY
|
||||
if (w - d >= e) /* (this test assumes unsigned comparison) */
|
||||
{
|
||||
memcpy(redirSlide + w, redirSlide + d, e);
|
||||
w += e;
|
||||
d += e;
|
||||
}
|
||||
else /* do it slow to avoid memcpy() overlap */
|
||||
#endif /* !NOMEMCPY */
|
||||
do {
|
||||
redirSlide[w++] = redirSlide[d++];
|
||||
} while (--e);
|
||||
if (w == wszimpl)
|
||||
{
|
||||
if ((retval = flush(__G__ redirSlide, (ulg)w, 0)) != 0)
|
||||
return retval;
|
||||
w = u = 0;
|
||||
}
|
||||
} while (n);
|
||||
}
|
||||
}
|
||||
|
||||
/* flush out redirSlide */
|
||||
if ((retval = flush(__G__ redirSlide, (ulg)w, 0)) != 0)
|
||||
return retval;
|
||||
if (G.csize + G.incnt + (k >> 3)) /* should have read csize bytes, but */
|
||||
{ /* sometimes read one too many: k>>3 compensates */
|
||||
G.used_csize = G.lrec.csize - G.csize - G.incnt - (k >> 3);
|
||||
return 5;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static int explode_nolit(__G__ tl, td, bl, bd, bdl)
|
||||
__GDEF
|
||||
struct huft* tl, * td; /* length and distance decoder tables */
|
||||
unsigned bl, bd; /* number of bits decoded by tl[] and td[] */
|
||||
unsigned bdl; /* number of distance low bits */
|
||||
/* Decompress the imploded data using uncoded literals and a sliding
|
||||
window (of size 2^(6+bdl) bytes). */
|
||||
{
|
||||
zusz_t s; /* bytes to decompress */
|
||||
register unsigned e; /* table entry flag/number of extra bits */
|
||||
unsigned n, d; /* length and index for copy */
|
||||
unsigned w; /* current window position */
|
||||
struct huft* t; /* pointer to table entry */
|
||||
unsigned ml, md; /* masks for bl and bd bits */
|
||||
unsigned mdl; /* mask for bdl (distance lower) bits */
|
||||
register ulg b; /* bit buffer */
|
||||
register unsigned k; /* number of bits in bit buffer */
|
||||
unsigned u; /* true if unflushed */
|
||||
int retval = 0; /* error code returned: initialized to "no error" */
|
||||
|
||||
|
||||
/* explode the coded data */
|
||||
b = k = w = 0; /* initialize bit buffer, window */
|
||||
u = 1; /* buffer unflushed */
|
||||
ml = mask_bits[bl]; /* precompute masks for speed */
|
||||
md = mask_bits[bd];
|
||||
mdl = mask_bits[bdl];
|
||||
s = G.lrec.ucsize;
|
||||
while (s > 0) /* do until ucsize bytes uncompressed */
|
||||
{
|
||||
NEEDBITS(1)
|
||||
if (b & 1) /* then literal--get eight bits */
|
||||
{
|
||||
DUMPBITS(1)
|
||||
s--;
|
||||
NEEDBITS(8)
|
||||
redirSlide[w++] = (uch)b;
|
||||
if (w == wszimpl)
|
||||
{
|
||||
if ((retval = flush(__G__ redirSlide, (ulg)w, 0)) != 0)
|
||||
return retval;
|
||||
w = u = 0;
|
||||
}
|
||||
DUMPBITS(8)
|
||||
}
|
||||
else /* else distance/length */
|
||||
{
|
||||
DUMPBITS(1)
|
||||
NEEDBITS(bdl) /* get distance low bits */
|
||||
d = (unsigned)b & mdl;
|
||||
DUMPBITS(bdl)
|
||||
DECODEHUFT(td, bd, md) /* get coded distance high bits */
|
||||
d = w - d - t->v.n; /* construct offset */
|
||||
DECODEHUFT(tl, bl, ml) /* get coded length */
|
||||
n = t->v.n;
|
||||
if (e) /* get length extra bits */
|
||||
{
|
||||
NEEDBITS(8)
|
||||
n += (unsigned)b & 0xff;
|
||||
DUMPBITS(8)
|
||||
}
|
||||
|
||||
/* do the copy */
|
||||
s = (s > (zusz_t)n ? s - (zusz_t)n : 0);
|
||||
do {
|
||||
#if (defined(DLL) && !defined(NO_SLIDE_REDIR))
|
||||
if (G.redirect_slide) {
|
||||
/* &= w/ wszimpl not needed and wrong if redirect */
|
||||
if (d >= wszimpl)
|
||||
return 1;
|
||||
e = wszimpl - (d > w ? d : w);
|
||||
}
|
||||
else
|
||||
#endif
|
||||
e = wszimpl - ((d &= wszimpl - 1) > w ? d : w);
|
||||
if (e > n) e = n;
|
||||
n -= e;
|
||||
if (u && w <= d)
|
||||
{
|
||||
memzero(redirSlide + w, e);
|
||||
w += e;
|
||||
d += e;
|
||||
}
|
||||
else
|
||||
#ifndef NOMEMCPY
|
||||
if (w - d >= e) /* (this test assumes unsigned comparison) */
|
||||
{
|
||||
memcpy(redirSlide + w, redirSlide + d, e);
|
||||
w += e;
|
||||
d += e;
|
||||
}
|
||||
else /* do it slow to avoid memcpy() overlap */
|
||||
#endif /* !NOMEMCPY */
|
||||
do {
|
||||
redirSlide[w++] = redirSlide[d++];
|
||||
} while (--e);
|
||||
if (w == wszimpl)
|
||||
{
|
||||
if ((retval = flush(__G__ redirSlide, (ulg)w, 0)) != 0)
|
||||
return retval;
|
||||
w = u = 0;
|
||||
}
|
||||
} while (n);
|
||||
}
|
||||
}
|
||||
|
||||
/* flush out redirSlide */
|
||||
if ((retval = flush(__G__ redirSlide, (ulg)w, 0)) != 0)
|
||||
return retval;
|
||||
if (G.csize + G.incnt + (k >> 3)) /* should have read csize bytes, but */
|
||||
{ /* sometimes read one too many: k>>3 compensates */
|
||||
G.used_csize = G.lrec.csize - G.csize - G.incnt - (k >> 3);
|
||||
return 5;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int explode(__G)
|
||||
__GDEF
|
||||
/* Explode an imploded compressed stream. Based on the general purpose
|
||||
bit flag, decide on coded or uncoded literals, and an 8K or 4K sliding
|
||||
window. Construct the literal (if any), length, and distance codes and
|
||||
the tables needed to decode them (using huft_build() from inflate.c),
|
||||
and call the appropriate routine for the type of data in the remainder
|
||||
of the stream. The four routines are nearly identical, differing only
|
||||
in whether the literal is decoded or simply read in, and in how many
|
||||
bits are read in, uncoded, for the low distance bits. */
|
||||
{
|
||||
unsigned r; /* return codes */
|
||||
struct huft* tb; /* literal code table */
|
||||
struct huft* tl; /* length code table */
|
||||
struct huft* td; /* distance code table */
|
||||
unsigned bb; /* bits for tb */
|
||||
unsigned bl; /* bits for tl */
|
||||
unsigned bd; /* bits for td */
|
||||
unsigned bdl; /* number of uncoded lower distance bits */
|
||||
unsigned l[256]; /* bit lengths for codes */
|
||||
|
||||
#if (defined(DLL) && !defined(NO_SLIDE_REDIR))
|
||||
if (G.redirect_slide)
|
||||
/* For 16-bit systems, it has already been checked at DLL entrance that
|
||||
* the buffer size in G.redirect_size does not exceed unsigned range.
|
||||
*/
|
||||
G._wsize = G.redirect_size, redirSlide = G.redirect_buffer;
|
||||
else
|
||||
#if defined(USE_DEFLATE64) && defined(INT_16BIT)
|
||||
/* For systems using 16-bit ints, reduce the used buffer size below
|
||||
* the limit of "unsigned int" numbers range.
|
||||
*/
|
||||
G._wsize = WSIZE >> 1, redirSlide = slide;
|
||||
#else /* !(USE_DEFLATE64 && INT_16BIT) */
|
||||
G._wsize = WSIZE, redirSlide = slide;
|
||||
#endif /* !(USE_DEFLATE64 && INT_16BIT) */
|
||||
#endif /* DLL && !NO_SLIDE_REDIR */
|
||||
|
||||
/* Tune base table sizes. Note: I thought that to truly optimize speed,
|
||||
I would have to select different bl, bd, and bb values for different
|
||||
compressed file sizes. I was surprised to find out that the values of
|
||||
7, 7, and 9 worked best over a very wide range of sizes, except that
|
||||
bd = 8 worked marginally better for large compressed sizes. */
|
||||
bl = 7;
|
||||
bd = (G.csize + G.incnt) > 200000L ? 8 : 7;
|
||||
|
||||
#ifdef DEBUG
|
||||
G.hufts = 0; /* initialize huft's malloc'ed */
|
||||
#endif
|
||||
|
||||
if (G.lrec.general_purpose_bit_flag & 4)
|
||||
/* With literal tree--minimum match length is 3 */
|
||||
{
|
||||
bb = 9; /* base table size for literals */
|
||||
if ((r = get_tree(__G__ l, 256)) != 0)
|
||||
return (int)r;
|
||||
if ((r = huft_build(__G__ l, 256, 256, NULL, NULL, &tb, &bb)) != 0)
|
||||
{
|
||||
if (r == 1)
|
||||
huft_free(tb);
|
||||
return (int)r;
|
||||
}
|
||||
if ((r = get_tree(__G__ l, 64)) != 0) {
|
||||
huft_free(tb);
|
||||
return (int)r;
|
||||
}
|
||||
if ((r = huft_build(__G__ l, 64, 0, cplen3, extra, &tl, &bl)) != 0)
|
||||
{
|
||||
if (r == 1)
|
||||
huft_free(tl);
|
||||
huft_free(tb);
|
||||
return (int)r;
|
||||
}
|
||||
}
|
||||
else
|
||||
/* No literal tree--minimum match length is 2 */
|
||||
{
|
||||
tb = (struct huft*)NULL;
|
||||
if ((r = get_tree(__G__ l, 64)) != 0)
|
||||
return (int)r;
|
||||
if ((r = huft_build(__G__ l, 64, 0, cplen2, extra, &tl, &bl)) != 0)
|
||||
{
|
||||
if (r == 1)
|
||||
huft_free(tl);
|
||||
return (int)r;
|
||||
}
|
||||
}
|
||||
|
||||
if ((r = get_tree(__G__ l, 64)) != 0) {
|
||||
huft_free(tl);
|
||||
if (tb != (struct huft*)NULL) huft_free(tb);
|
||||
return (int)r;
|
||||
}
|
||||
if (G.lrec.general_purpose_bit_flag & 2) /* true if 8K */
|
||||
{
|
||||
bdl = 7;
|
||||
r = huft_build(__G__ l, 64, 0, cpdist8, extra, &td, &bd);
|
||||
}
|
||||
else /* else 4K */
|
||||
{
|
||||
bdl = 6;
|
||||
r = huft_build(__G__ l, 64, 0, cpdist4, extra, &td, &bd);
|
||||
}
|
||||
if (r != 0)
|
||||
{
|
||||
if (r == 1)
|
||||
huft_free(td);
|
||||
huft_free(tl);
|
||||
if (tb != (struct huft*)NULL) huft_free(tb);
|
||||
return (int)r;
|
||||
}
|
||||
|
||||
if (tb != NULL) {
|
||||
r = explode_lit(__G__ tb, tl, td, bb, bl, bd, bdl);
|
||||
huft_free(tb);
|
||||
}
|
||||
else {
|
||||
r = explode_nolit(__G__ tl, td, bl, bd, bdl);
|
||||
}
|
||||
|
||||
huft_free(td);
|
||||
huft_free(tl);
|
||||
Trace((stderr, "<%u > ", G.hufts));
|
||||
return (int)r;
|
||||
}
|
||||
|
||||
/* so explode.c and inflate.c can be compiled together into one object: */
|
||||
#undef DECODEHUFT
|
||||
#undef NEEDBITS
|
||||
#undef DUMPBITS
|
||||
#undef wszimpl
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,436 +0,0 @@
|
||||
// Inftree.cs
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (c) 2009 Dino Chiesa and Microsoft Corporation.
|
||||
// All rights reserved.
|
||||
//
|
||||
// This code module is part of DotNetZip, a zipfile class library.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This code is licensed under the Microsoft Public License.
|
||||
// See the file License.txt for the license details.
|
||||
// More info on: http://dotnetzip.codeplex.com
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// last saved (in emacs):
|
||||
// Time-stamp: <2009-October-28 12:43:54>
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This module defines classes used in decompression. This code is derived
|
||||
// from the jzlib implementation of zlib. In keeping with the license for jzlib,
|
||||
// the copyright to that code is below.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright notice,
|
||||
// this list of conditions and the following disclaimer.
|
||||
//
|
||||
// 2. Redistributions in binary form must reproduce the above copyright
|
||||
// notice, this list of conditions and the following disclaimer in
|
||||
// the documentation and/or other materials provided with the distribution.
|
||||
//
|
||||
// 3. The names of the authors may not be used to endorse or promote products
|
||||
// derived from this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
// INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
//
|
||||
// This program is based on zlib-1.1.3; credit to authors
|
||||
// Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
|
||||
// and contributors of zlib.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
|
||||
using System;
|
||||
|
||||
namespace Compress.ZipFile.ZLib
|
||||
{
|
||||
|
||||
sealed class InfTree
|
||||
{
|
||||
|
||||
private const int MANY = 1440;
|
||||
|
||||
private const int Z_OK = 0;
|
||||
private const int Z_STREAM_END = 1;
|
||||
private const int Z_NEED_DICT = 2;
|
||||
private const int Z_ERRNO = - 1;
|
||||
private const int Z_STREAM_ERROR = - 2;
|
||||
private const int Z_DATA_ERROR = - 3;
|
||||
private const int Z_MEM_ERROR = - 4;
|
||||
private const int Z_BUF_ERROR = - 5;
|
||||
private const int Z_VERSION_ERROR = - 6;
|
||||
|
||||
internal const int fixed_bl = 9;
|
||||
internal const int fixed_bd = 5;
|
||||
|
||||
//UPGRADE_NOTE: Final was removed from the declaration of 'fixed_tl'. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
|
||||
internal static readonly int[] fixed_tl = new int[]{96, 7, 256, 0, 8, 80, 0, 8, 16, 84, 8, 115, 82, 7, 31, 0, 8, 112, 0, 8, 48, 0, 9, 192, 80, 7, 10, 0, 8, 96, 0, 8, 32, 0, 9, 160, 0, 8, 0, 0, 8, 128, 0, 8, 64, 0, 9, 224, 80, 7, 6, 0, 8, 88, 0, 8, 24, 0, 9, 144, 83, 7, 59, 0, 8, 120, 0, 8, 56, 0, 9, 208, 81, 7, 17, 0, 8, 104, 0, 8, 40, 0, 9, 176, 0, 8, 8, 0, 8, 136, 0, 8, 72, 0, 9, 240, 80, 7, 4, 0, 8, 84, 0, 8, 20, 85, 8, 227, 83, 7, 43, 0, 8, 116, 0, 8, 52, 0, 9, 200, 81, 7, 13, 0, 8, 100, 0, 8, 36, 0, 9, 168, 0, 8, 4, 0, 8, 132, 0, 8, 68, 0, 9, 232, 80, 7, 8, 0, 8, 92, 0, 8, 28, 0, 9, 152, 84, 7, 83, 0, 8, 124, 0, 8, 60, 0, 9, 216, 82, 7, 23, 0, 8, 108, 0, 8, 44, 0, 9, 184, 0, 8, 12, 0, 8, 140, 0, 8, 76, 0, 9, 248, 80, 7, 3, 0, 8, 82, 0, 8, 18, 85, 8, 163, 83, 7, 35, 0, 8, 114, 0, 8, 50, 0, 9, 196, 81, 7, 11, 0, 8, 98, 0, 8, 34, 0, 9, 164, 0, 8, 2, 0, 8, 130, 0, 8, 66, 0, 9, 228, 80, 7, 7, 0, 8, 90, 0, 8, 26, 0, 9, 148, 84, 7, 67, 0, 8, 122, 0, 8, 58, 0, 9, 212, 82, 7, 19, 0, 8, 106, 0, 8, 42, 0, 9, 180, 0, 8, 10, 0, 8, 138, 0, 8, 74, 0, 9, 244, 80, 7, 5, 0, 8, 86, 0, 8, 22, 192, 8, 0, 83, 7, 51, 0, 8, 118, 0, 8, 54, 0, 9, 204, 81, 7, 15, 0, 8, 102, 0, 8, 38, 0, 9, 172, 0, 8, 6, 0, 8, 134, 0, 8, 70, 0, 9, 236, 80, 7, 9, 0, 8, 94, 0, 8, 30, 0, 9, 156, 84, 7, 99, 0, 8, 126, 0, 8, 62, 0, 9, 220, 82, 7, 27, 0, 8, 110, 0, 8, 46, 0, 9, 188, 0, 8, 14, 0, 8, 142, 0, 8, 78, 0, 9, 252, 96, 7, 256, 0, 8, 81, 0, 8, 17, 85, 8, 131, 82, 7, 31, 0, 8, 113, 0, 8, 49, 0, 9, 194, 80, 7, 10, 0, 8, 97, 0, 8, 33, 0, 9, 162, 0, 8, 1, 0, 8, 129, 0, 8, 65, 0, 9, 226, 80, 7, 6, 0, 8, 89, 0, 8, 25, 0, 9, 146, 83, 7, 59, 0, 8, 121, 0, 8, 57, 0, 9, 210, 81, 7, 17, 0, 8, 105, 0, 8, 41, 0, 9, 178, 0, 8, 9, 0, 8, 137, 0, 8, 73, 0, 9, 242, 80, 7, 4, 0, 8, 85, 0, 8, 21, 80, 8, 258, 83, 7, 43, 0, 8, 117, 0, 8, 53, 0, 9, 202, 81, 7, 13, 0, 8, 101, 0, 8, 37, 0, 9, 170, 0, 8, 5, 0, 8, 133, 0, 8, 69, 0, 9, 234, 80, 7, 8, 0, 8, 93, 0, 8, 29, 0, 9, 154, 84, 7, 83, 0, 8, 125, 0, 8, 61, 0, 9, 218, 82, 7, 23, 0, 8, 109, 0, 8, 45, 0, 9, 186,
|
||||
0, 8, 13, 0, 8, 141, 0, 8, 77, 0, 9, 250, 80, 7, 3, 0, 8, 83, 0, 8, 19, 85, 8, 195, 83, 7, 35, 0, 8, 115, 0, 8, 51, 0, 9, 198, 81, 7, 11, 0, 8, 99, 0, 8, 35, 0, 9, 166, 0, 8, 3, 0, 8, 131, 0, 8, 67, 0, 9, 230, 80, 7, 7, 0, 8, 91, 0, 8, 27, 0, 9, 150, 84, 7, 67, 0, 8, 123, 0, 8, 59, 0, 9, 214, 82, 7, 19, 0, 8, 107, 0, 8, 43, 0, 9, 182, 0, 8, 11, 0, 8, 139, 0, 8, 75, 0, 9, 246, 80, 7, 5, 0, 8, 87, 0, 8, 23, 192, 8, 0, 83, 7, 51, 0, 8, 119, 0, 8, 55, 0, 9, 206, 81, 7, 15, 0, 8, 103, 0, 8, 39, 0, 9, 174, 0, 8, 7, 0, 8, 135, 0, 8, 71, 0, 9, 238, 80, 7, 9, 0, 8, 95, 0, 8, 31, 0, 9, 158, 84, 7, 99, 0, 8, 127, 0, 8, 63, 0, 9, 222, 82, 7, 27, 0, 8, 111, 0, 8, 47, 0, 9, 190, 0, 8, 15, 0, 8, 143, 0, 8, 79, 0, 9, 254, 96, 7, 256, 0, 8, 80, 0, 8, 16, 84, 8, 115, 82, 7, 31, 0, 8, 112, 0, 8, 48, 0, 9, 193, 80, 7, 10, 0, 8, 96, 0, 8, 32, 0, 9, 161, 0, 8, 0, 0, 8, 128, 0, 8, 64, 0, 9, 225, 80, 7, 6, 0, 8, 88, 0, 8, 24, 0, 9, 145, 83, 7, 59, 0, 8, 120, 0, 8, 56, 0, 9, 209, 81, 7, 17, 0, 8, 104, 0, 8, 40, 0, 9, 177, 0, 8, 8, 0, 8, 136, 0, 8, 72, 0, 9, 241, 80, 7, 4, 0, 8, 84, 0, 8, 20, 85, 8, 227, 83, 7, 43, 0, 8, 116, 0, 8, 52, 0, 9, 201, 81, 7, 13, 0, 8, 100, 0, 8, 36, 0, 9, 169, 0, 8, 4, 0, 8, 132, 0, 8, 68, 0, 9, 233, 80, 7, 8, 0, 8, 92, 0, 8, 28, 0, 9, 153, 84, 7, 83, 0, 8, 124, 0, 8, 60, 0, 9, 217, 82, 7, 23, 0, 8, 108, 0, 8, 44, 0, 9, 185, 0, 8, 12, 0, 8, 140, 0, 8, 76, 0, 9, 249, 80, 7, 3, 0, 8, 82, 0, 8, 18, 85, 8, 163, 83, 7, 35, 0, 8, 114, 0, 8, 50, 0, 9, 197, 81, 7, 11, 0, 8, 98, 0, 8, 34, 0, 9, 165, 0, 8, 2, 0, 8, 130, 0, 8, 66, 0, 9, 229, 80, 7, 7, 0, 8, 90, 0, 8, 26, 0, 9, 149, 84, 7, 67, 0, 8, 122, 0, 8, 58, 0, 9, 213, 82, 7, 19, 0, 8, 106, 0, 8, 42, 0, 9, 181, 0, 8, 10, 0, 8, 138, 0, 8, 74, 0, 9, 245, 80, 7, 5, 0, 8, 86, 0, 8, 22, 192, 8, 0, 83, 7, 51, 0, 8, 118, 0, 8, 54, 0, 9, 205, 81, 7, 15, 0, 8, 102, 0, 8, 38, 0, 9, 173, 0, 8, 6, 0, 8, 134, 0, 8, 70, 0, 9, 237, 80, 7, 9, 0, 8, 94, 0, 8, 30, 0, 9, 157, 84, 7, 99, 0, 8, 126, 0, 8, 62, 0, 9, 221, 82, 7, 27, 0, 8, 110, 0, 8, 46, 0, 9, 189, 0, 8,
|
||||
14, 0, 8, 142, 0, 8, 78, 0, 9, 253, 96, 7, 256, 0, 8, 81, 0, 8, 17, 85, 8, 131, 82, 7, 31, 0, 8, 113, 0, 8, 49, 0, 9, 195, 80, 7, 10, 0, 8, 97, 0, 8, 33, 0, 9, 163, 0, 8, 1, 0, 8, 129, 0, 8, 65, 0, 9, 227, 80, 7, 6, 0, 8, 89, 0, 8, 25, 0, 9, 147, 83, 7, 59, 0, 8, 121, 0, 8, 57, 0, 9, 211, 81, 7, 17, 0, 8, 105, 0, 8, 41, 0, 9, 179, 0, 8, 9, 0, 8, 137, 0, 8, 73, 0, 9, 243, 80, 7, 4, 0, 8, 85, 0, 8, 21, 80, 8, 258, 83, 7, 43, 0, 8, 117, 0, 8, 53, 0, 9, 203, 81, 7, 13, 0, 8, 101, 0, 8, 37, 0, 9, 171, 0, 8, 5, 0, 8, 133, 0, 8, 69, 0, 9, 235, 80, 7, 8, 0, 8, 93, 0, 8, 29, 0, 9, 155, 84, 7, 83, 0, 8, 125, 0, 8, 61, 0, 9, 219, 82, 7, 23, 0, 8, 109, 0, 8, 45, 0, 9, 187, 0, 8, 13, 0, 8, 141, 0, 8, 77, 0, 9, 251, 80, 7, 3, 0, 8, 83, 0, 8, 19, 85, 8, 195, 83, 7, 35, 0, 8, 115, 0, 8, 51, 0, 9, 199, 81, 7, 11, 0, 8, 99, 0, 8, 35, 0, 9, 167, 0, 8, 3, 0, 8, 131, 0, 8, 67, 0, 9, 231, 80, 7, 7, 0, 8, 91, 0, 8, 27, 0, 9, 151, 84, 7, 67, 0, 8, 123, 0, 8, 59, 0, 9, 215, 82, 7, 19, 0, 8, 107, 0, 8, 43, 0, 9, 183, 0, 8, 11, 0, 8, 139, 0, 8, 75, 0, 9, 247, 80, 7, 5, 0, 8, 87, 0, 8, 23, 192, 8, 0, 83, 7, 51, 0, 8, 119, 0, 8, 55, 0, 9, 207, 81, 7, 15, 0, 8, 103, 0, 8, 39, 0, 9, 175, 0, 8, 7, 0, 8, 135, 0, 8, 71, 0, 9, 239, 80, 7, 9, 0, 8, 95, 0, 8, 31, 0, 9, 159, 84, 7, 99, 0, 8, 127, 0, 8, 63, 0, 9, 223, 82, 7, 27, 0, 8, 111, 0, 8, 47, 0, 9, 191, 0, 8, 15, 0, 8, 143, 0, 8, 79, 0, 9, 255};
|
||||
//UPGRADE_NOTE: Final was removed from the declaration of 'fixed_td'. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
|
||||
internal static readonly int[] fixed_td = new int[]{80, 5, 1, 87, 5, 257, 83, 5, 17, 91, 5, 4097, 81, 5, 5, 89, 5, 1025, 85, 5, 65, 93, 5, 16385, 80, 5, 3, 88, 5, 513, 84, 5, 33, 92, 5, 8193, 82, 5, 9, 90, 5, 2049, 86, 5, 129, 192, 5, 24577, 80, 5, 2, 87, 5, 385, 83, 5, 25, 91, 5, 6145, 81, 5, 7, 89, 5, 1537, 85, 5, 97, 93, 5, 24577, 80, 5, 4, 88, 5, 769, 84, 5, 49, 92, 5, 12289, 82, 5, 13, 90, 5, 3073, 86, 5, 193, 192, 5, 24577};
|
||||
|
||||
// Tables for deflate from PKZIP's appnote.txt.
|
||||
//UPGRADE_NOTE: Final was removed from the declaration of 'cplens'. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
|
||||
internal static readonly int[] cplens = new int[]{3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
|
||||
|
||||
// see note #13 above about 258
|
||||
//UPGRADE_NOTE: Final was removed from the declaration of 'cplext'. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
|
||||
internal static readonly int[] cplext = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 112, 112};
|
||||
|
||||
//UPGRADE_NOTE: Final was removed from the declaration of 'cpdist'. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
|
||||
internal static readonly int[] cpdist = new int[]{1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577};
|
||||
|
||||
//UPGRADE_NOTE: Final was removed from the declaration of 'cpdext'. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
|
||||
internal static readonly int[] cpdext = new int[]{0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13};
|
||||
|
||||
// If BMAX needs to be larger than 16, then h and x[] should be uLong.
|
||||
internal const int BMAX = 15; // maximum bit length of any code
|
||||
|
||||
internal int[] hn = null; // hufts used in space
|
||||
internal int[] v = null; // work area for huft_build
|
||||
internal int[] c = null; // bit length count table
|
||||
internal int[] r = null; // table entry for structure assignment
|
||||
internal int[] u = null; // table stack
|
||||
internal int[] x = null; // bit offsets, then code stack
|
||||
|
||||
private int huft_build(int[] b, int bindex, int n, int s, int[] d, int[] e, int[] t, int[] m, int[] hp, int[] hn, int[] v)
|
||||
{
|
||||
// Given a list of code lengths and a maximum table size, make a set of
|
||||
// tables to decode that set of codes. Return Z_OK on success, Z_BUF_ERROR
|
||||
// if the given code set is incomplete (the tables are still built in this
|
||||
// case), Z_DATA_ERROR if the input is invalid (an over-subscribed set of
|
||||
// lengths), or Z_MEM_ERROR if not enough memory.
|
||||
|
||||
int a; // counter for codes of length k
|
||||
int f; // i repeats in table every f entries
|
||||
int g; // maximum code length
|
||||
int h; // table level
|
||||
int i; // counter, current code
|
||||
int j; // counter
|
||||
int k; // number of bits in current code
|
||||
int l; // bits per table (returned in m)
|
||||
int mask; // (1 << w) - 1, to avoid cc -O bug on HP
|
||||
int p; // pointer into c[], b[], or v[]
|
||||
int q; // points to current table
|
||||
int w; // bits before this table == (l * h)
|
||||
int xp; // pointer into x
|
||||
int y; // number of dummy codes added
|
||||
int z; // number of entries in current table
|
||||
|
||||
// Generate counts for each bit length
|
||||
|
||||
p = 0; i = n;
|
||||
do
|
||||
{
|
||||
c[b[bindex + p]]++; p++; i--; // assume all entries <= BMAX
|
||||
}
|
||||
while (i != 0);
|
||||
|
||||
if (c[0] == n)
|
||||
{
|
||||
// null input--all zero length codes
|
||||
t[0] = - 1;
|
||||
m[0] = 0;
|
||||
return Z_OK;
|
||||
}
|
||||
|
||||
// Find minimum and maximum length, bound *m by those
|
||||
l = m[0];
|
||||
for (j = 1; j <= BMAX; j++)
|
||||
if (c[j] != 0)
|
||||
break;
|
||||
k = j; // minimum code length
|
||||
if (l < j)
|
||||
{
|
||||
l = j;
|
||||
}
|
||||
for (i = BMAX; i != 0; i--)
|
||||
{
|
||||
if (c[i] != 0)
|
||||
break;
|
||||
}
|
||||
g = i; // maximum code length
|
||||
if (l > i)
|
||||
{
|
||||
l = i;
|
||||
}
|
||||
m[0] = l;
|
||||
|
||||
// Adjust last length count to fill out codes, if needed
|
||||
for (y = 1 << j; j < i; j++, y <<= 1)
|
||||
{
|
||||
if ((y -= c[j]) < 0)
|
||||
{
|
||||
return Z_DATA_ERROR;
|
||||
}
|
||||
}
|
||||
if ((y -= c[i]) < 0)
|
||||
{
|
||||
return Z_DATA_ERROR;
|
||||
}
|
||||
c[i] += y;
|
||||
|
||||
// Generate starting offsets into the value table for each length
|
||||
x[1] = j = 0;
|
||||
p = 1; xp = 2;
|
||||
while (--i != 0)
|
||||
{
|
||||
// note that i == g from above
|
||||
x[xp] = (j += c[p]);
|
||||
xp++;
|
||||
p++;
|
||||
}
|
||||
|
||||
// Make a table of values in order of bit lengths
|
||||
i = 0; p = 0;
|
||||
do
|
||||
{
|
||||
if ((j = b[bindex + p]) != 0)
|
||||
{
|
||||
v[x[j]++] = i;
|
||||
}
|
||||
p++;
|
||||
}
|
||||
while (++i < n);
|
||||
n = x[g]; // set n to length of v
|
||||
|
||||
// Generate the Huffman codes and for each, make the table entries
|
||||
x[0] = i = 0; // first Huffman code is zero
|
||||
p = 0; // grab values in bit order
|
||||
h = - 1; // no tables yet--level -1
|
||||
w = - l; // bits decoded == (l * h)
|
||||
u[0] = 0; // just to keep compilers happy
|
||||
q = 0; // ditto
|
||||
z = 0; // ditto
|
||||
|
||||
// go through the bit lengths (k already is bits in shortest code)
|
||||
for (; k <= g; k++)
|
||||
{
|
||||
a = c[k];
|
||||
while (a-- != 0)
|
||||
{
|
||||
// here i is the Huffman code of length k bits for value *p
|
||||
// make tables up to required level
|
||||
while (k > w + l)
|
||||
{
|
||||
h++;
|
||||
w += l; // previous table always l bits
|
||||
// compute minimum size table less than or equal to l bits
|
||||
z = g - w;
|
||||
z = (z > l)?l:z; // table size upper limit
|
||||
if ((f = 1 << (j = k - w)) > a + 1)
|
||||
{
|
||||
// try a k-w bit table
|
||||
// too few codes for k-w bit table
|
||||
f -= (a + 1); // deduct codes from patterns left
|
||||
xp = k;
|
||||
if (j < z)
|
||||
{
|
||||
while (++j < z)
|
||||
{
|
||||
// try smaller tables up to z bits
|
||||
if ((f <<= 1) <= c[++xp])
|
||||
break; // enough codes to use up j bits
|
||||
f -= c[xp]; // else deduct codes from patterns
|
||||
}
|
||||
}
|
||||
}
|
||||
z = 1 << j; // table entries for j-bit table
|
||||
|
||||
// allocate new table
|
||||
if (hn[0] + z > MANY)
|
||||
{
|
||||
// (note: doesn't matter for fixed)
|
||||
return Z_DATA_ERROR; // overflow of MANY
|
||||
}
|
||||
u[h] = q = hn[0]; // DEBUG
|
||||
hn[0] += z;
|
||||
|
||||
// connect to last table, if there is one
|
||||
if (h != 0)
|
||||
{
|
||||
x[h] = i; // save pattern for backing up
|
||||
r[0] = (sbyte) j; // bits in this table
|
||||
r[1] = (sbyte) l; // bits to dump before this table
|
||||
j = SharedUtils.URShift(i, (w - l));
|
||||
r[2] = (int) (q - u[h - 1] - j); // offset to this table
|
||||
Array.Copy(r, 0, hp, (u[h - 1] + j) * 3, 3); // connect to last table
|
||||
}
|
||||
else
|
||||
{
|
||||
t[0] = q; // first table is returned result
|
||||
}
|
||||
}
|
||||
|
||||
// set up table entry in r
|
||||
r[1] = (sbyte) (k - w);
|
||||
if (p >= n)
|
||||
{
|
||||
r[0] = 128 + 64; // out of values--invalid code
|
||||
}
|
||||
else if (v[p] < s)
|
||||
{
|
||||
r[0] = (sbyte) (v[p] < 256?0:32 + 64); // 256 is end-of-block
|
||||
r[2] = v[p++]; // simple code is just the value
|
||||
}
|
||||
else
|
||||
{
|
||||
r[0] = (sbyte) (e[v[p] - s] + 16 + 64); // non-simple--look up in lists
|
||||
r[2] = d[v[p++] - s];
|
||||
}
|
||||
|
||||
// fill code-like entries with r
|
||||
f = 1 << (k - w);
|
||||
for (j = SharedUtils.URShift(i, w); j < z; j += f)
|
||||
{
|
||||
Array.Copy(r, 0, hp, (q + j) * 3, 3);
|
||||
}
|
||||
|
||||
// backwards increment the k-bit code i
|
||||
for (j = 1 << (k - 1); (i & j) != 0; j = SharedUtils.URShift(j, 1))
|
||||
{
|
||||
i ^= j;
|
||||
}
|
||||
i ^= j;
|
||||
|
||||
// backup over finished tables
|
||||
mask = (1 << w) - 1; // needed on HP, cc -O bug
|
||||
while ((i & mask) != x[h])
|
||||
{
|
||||
h--; // don't need to update q
|
||||
w -= l;
|
||||
mask = (1 << w) - 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Return Z_BUF_ERROR if we were given an incomplete table
|
||||
return y != 0 && g != 1?Z_BUF_ERROR:Z_OK;
|
||||
}
|
||||
|
||||
internal int inflate_trees_bits(int[] c, int[] bb, int[] tb, int[] hp, ZlibCodec z)
|
||||
{
|
||||
int result;
|
||||
initWorkArea(19);
|
||||
hn[0] = 0;
|
||||
result = huft_build(c, 0, 19, 19, null, null, tb, bb, hp, hn, v);
|
||||
|
||||
if (result == Z_DATA_ERROR)
|
||||
{
|
||||
z.Message = "oversubscribed dynamic bit lengths tree";
|
||||
}
|
||||
else if (result == Z_BUF_ERROR || bb[0] == 0)
|
||||
{
|
||||
z.Message = "incomplete dynamic bit lengths tree";
|
||||
result = Z_DATA_ERROR;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
internal int inflate_trees_dynamic(int nl, int nd, int[] c, int[] bl, int[] bd, int[] tl, int[] td, int[] hp, ZlibCodec z)
|
||||
{
|
||||
int result;
|
||||
|
||||
// build literal/length tree
|
||||
initWorkArea(288);
|
||||
hn[0] = 0;
|
||||
result = huft_build(c, 0, nl, 257, cplens, cplext, tl, bl, hp, hn, v);
|
||||
if (result != Z_OK || bl[0] == 0)
|
||||
{
|
||||
if (result == Z_DATA_ERROR)
|
||||
{
|
||||
z.Message = "oversubscribed literal/length tree";
|
||||
}
|
||||
else if (result != Z_MEM_ERROR)
|
||||
{
|
||||
z.Message = "incomplete literal/length tree";
|
||||
result = Z_DATA_ERROR;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// build distance tree
|
||||
initWorkArea(288);
|
||||
result = huft_build(c, nl, nd, 0, cpdist, cpdext, td, bd, hp, hn, v);
|
||||
|
||||
if (result != Z_OK || (bd[0] == 0 && nl > 257))
|
||||
{
|
||||
if (result == Z_DATA_ERROR)
|
||||
{
|
||||
z.Message = "oversubscribed distance tree";
|
||||
}
|
||||
else if (result == Z_BUF_ERROR)
|
||||
{
|
||||
z.Message = "incomplete distance tree";
|
||||
result = Z_DATA_ERROR;
|
||||
}
|
||||
else if (result != Z_MEM_ERROR)
|
||||
{
|
||||
z.Message = "empty distance tree with lengths";
|
||||
result = Z_DATA_ERROR;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
return Z_OK;
|
||||
}
|
||||
|
||||
internal static int inflate_trees_fixed(int[] bl, int[] bd, int[][] tl, int[][] td, ZlibCodec z)
|
||||
{
|
||||
bl[0] = fixed_bl;
|
||||
bd[0] = fixed_bd;
|
||||
tl[0] = fixed_tl;
|
||||
td[0] = fixed_td;
|
||||
return Z_OK;
|
||||
}
|
||||
|
||||
private void initWorkArea(int vsize)
|
||||
{
|
||||
if (hn == null)
|
||||
{
|
||||
hn = new int[1];
|
||||
v = new int[vsize];
|
||||
c = new int[BMAX + 1];
|
||||
r = new int[3];
|
||||
u = new int[BMAX];
|
||||
x = new int[BMAX + 1];
|
||||
}
|
||||
else
|
||||
{
|
||||
if (v.Length < vsize)
|
||||
{
|
||||
v = new int[vsize];
|
||||
}
|
||||
Array.Clear(v,0,vsize);
|
||||
Array.Clear(c,0,BMAX+1);
|
||||
r[0]=0; r[1]=0; r[2]=0;
|
||||
// for(int i=0; i<BMAX; i++){u[i]=0;}
|
||||
//Array.Copy(c, 0, u, 0, BMAX);
|
||||
Array.Clear(u,0,BMAX);
|
||||
// for(int i=0; i<BMAX+1; i++){x[i]=0;}
|
||||
//Array.Copy(c, 0, x, 0, BMAX + 1);
|
||||
Array.Clear(x,0,BMAX+1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,421 +0,0 @@
|
||||
// Tree.cs
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (c) 2009 Dino Chiesa and Microsoft Corporation.
|
||||
// All rights reserved.
|
||||
//
|
||||
// This code module is part of DotNetZip, a zipfile class library.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This code is licensed under the Microsoft Public License.
|
||||
// See the file License.txt for the license details.
|
||||
// More info on: http://dotnetzip.codeplex.com
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// last saved (in emacs):
|
||||
// Time-stamp: <2009-October-28 13:29:50>
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This module defines classes for zlib compression and
|
||||
// decompression. This code is derived from the jzlib implementation of
|
||||
// zlib. In keeping with the license for jzlib, the copyright to that
|
||||
// code is below.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright notice,
|
||||
// this list of conditions and the following disclaimer.
|
||||
//
|
||||
// 2. Redistributions in binary form must reproduce the above copyright
|
||||
// notice, this list of conditions and the following disclaimer in
|
||||
// the documentation and/or other materials provided with the distribution.
|
||||
//
|
||||
// 3. The names of the authors may not be used to endorse or promote products
|
||||
// derived from this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
// INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
//
|
||||
// This program is based on zlib-1.1.3; credit to authors
|
||||
// Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
|
||||
// and contributors of zlib.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
|
||||
namespace Compress.ZipFile.ZLib
|
||||
{
|
||||
sealed class Tree
|
||||
{
|
||||
private static readonly int HEAP_SIZE = (2 * InternalConstants.L_CODES + 1);
|
||||
|
||||
// extra bits for each length code
|
||||
internal static readonly int[] ExtraLengthBits = new int[]
|
||||
{
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
|
||||
3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0
|
||||
};
|
||||
|
||||
// extra bits for each distance code
|
||||
internal static readonly int[] ExtraDistanceBits = new int[]
|
||||
{
|
||||
0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
|
||||
7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13
|
||||
};
|
||||
|
||||
// extra bits for each bit length code
|
||||
internal static readonly int[] extra_blbits = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 3, 7};
|
||||
|
||||
internal static readonly sbyte[] bl_order = new sbyte[]{16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
|
||||
|
||||
|
||||
// The lengths of the bit length codes are sent in order of decreasing
|
||||
// probability, to avoid transmitting the lengths for unused bit
|
||||
// length codes.
|
||||
|
||||
internal const int Buf_size = 8 * 2;
|
||||
|
||||
// see definition of array dist_code below
|
||||
//internal const int DIST_CODE_LEN = 512;
|
||||
|
||||
private static readonly sbyte[] _dist_code = new sbyte[]
|
||||
{
|
||||
0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7,
|
||||
8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9,
|
||||
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
|
||||
11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
|
||||
12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
|
||||
12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
|
||||
13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
|
||||
13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
|
||||
14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
|
||||
14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
|
||||
14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
|
||||
14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
|
||||
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
|
||||
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
|
||||
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
|
||||
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
|
||||
0, 0, 16, 17, 18, 18, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21,
|
||||
22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23,
|
||||
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
|
||||
25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
|
||||
26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
|
||||
26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
|
||||
27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
|
||||
27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
|
||||
28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
|
||||
28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
|
||||
28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
|
||||
28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
|
||||
29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
|
||||
29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
|
||||
29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
|
||||
29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29
|
||||
};
|
||||
|
||||
internal static readonly sbyte[] LengthCode = new sbyte[]
|
||||
{
|
||||
0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 9, 10, 10, 11, 11,
|
||||
12, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15,
|
||||
16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 17, 17, 17, 17,
|
||||
18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19, 19, 19, 19, 19,
|
||||
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
|
||||
21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
|
||||
22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22,
|
||||
23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23,
|
||||
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
|
||||
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
|
||||
25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
|
||||
25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
|
||||
26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
|
||||
26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
|
||||
27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
|
||||
27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28
|
||||
};
|
||||
|
||||
|
||||
internal static readonly int[] LengthBase = new int[]
|
||||
{
|
||||
0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28,
|
||||
32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 0
|
||||
};
|
||||
|
||||
|
||||
internal static readonly int[] DistanceBase = new int[]
|
||||
{
|
||||
0, 1, 2, 3, 4, 6, 8, 12, 16, 24, 32, 48, 64, 96, 128, 192,
|
||||
256, 384, 512, 768, 1024, 1536, 2048, 3072, 4096, 6144, 8192, 12288, 16384, 24576
|
||||
};
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Map from a distance to a distance code.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// No side effects. _dist_code[256] and _dist_code[257] are never used.
|
||||
/// </remarks>
|
||||
internal static int DistanceCode(int dist)
|
||||
{
|
||||
return (dist < 256)
|
||||
? _dist_code[dist]
|
||||
: _dist_code[256 + SharedUtils.URShift(dist, 7)];
|
||||
}
|
||||
|
||||
internal short[] dyn_tree; // the dynamic tree
|
||||
internal int max_code; // largest code with non zero frequency
|
||||
internal StaticTree staticTree; // the corresponding static tree
|
||||
|
||||
// Compute the optimal bit lengths for a tree and update the total bit length
|
||||
// for the current block.
|
||||
// IN assertion: the fields freq and dad are set, heap[heap_max] and
|
||||
// above are the tree nodes sorted by increasing frequency.
|
||||
// OUT assertions: the field len is set to the optimal bit length, the
|
||||
// array bl_count contains the frequencies for each bit length.
|
||||
// The length opt_len is updated; static_len is also updated if stree is
|
||||
// not null.
|
||||
internal void gen_bitlen(DeflateManager s)
|
||||
{
|
||||
short[] tree = dyn_tree;
|
||||
short[] stree = staticTree.treeCodes;
|
||||
int[] extra = staticTree.extraBits;
|
||||
int base_Renamed = staticTree.extraBase;
|
||||
int max_length = staticTree.maxLength;
|
||||
int h; // heap index
|
||||
int n, m; // iterate over the tree elements
|
||||
int bits; // bit length
|
||||
int xbits; // extra bits
|
||||
short f; // frequency
|
||||
int overflow = 0; // number of elements with bit length too large
|
||||
|
||||
for (bits = 0; bits <= InternalConstants.MAX_BITS; bits++)
|
||||
s.bl_count[bits] = 0;
|
||||
|
||||
// In a first pass, compute the optimal bit lengths (which may
|
||||
// overflow in the case of the bit length tree).
|
||||
tree[s.heap[s.heap_max] * 2 + 1] = 0; // root of the heap
|
||||
|
||||
for (h = s.heap_max + 1; h < HEAP_SIZE; h++)
|
||||
{
|
||||
n = s.heap[h];
|
||||
bits = tree[tree[n * 2 + 1] * 2 + 1] + 1;
|
||||
if (bits > max_length)
|
||||
{
|
||||
bits = max_length; overflow++;
|
||||
}
|
||||
tree[n * 2 + 1] = (short) bits;
|
||||
// We overwrite tree[n*2+1] which is no longer needed
|
||||
|
||||
if (n > max_code)
|
||||
continue; // not a leaf node
|
||||
|
||||
s.bl_count[bits]++;
|
||||
xbits = 0;
|
||||
if (n >= base_Renamed)
|
||||
xbits = extra[n - base_Renamed];
|
||||
f = tree[n * 2];
|
||||
s.opt_len += f * (bits + xbits);
|
||||
if (stree != null)
|
||||
s.static_len += f * (stree[n * 2 + 1] + xbits);
|
||||
}
|
||||
if (overflow == 0)
|
||||
return ;
|
||||
|
||||
// This happens for example on obj2 and pic of the Calgary corpus
|
||||
// Find the first bit length which could increase:
|
||||
do
|
||||
{
|
||||
bits = max_length - 1;
|
||||
while (s.bl_count[bits] == 0)
|
||||
bits--;
|
||||
s.bl_count[bits]--; // move one leaf down the tree
|
||||
s.bl_count[bits + 1] = (short) (s.bl_count[bits + 1] + 2); // move one overflow item as its brother
|
||||
s.bl_count[max_length]--;
|
||||
// The brother of the overflow item also moves one step up,
|
||||
// but this does not affect bl_count[max_length]
|
||||
overflow -= 2;
|
||||
}
|
||||
while (overflow > 0);
|
||||
|
||||
for (bits = max_length; bits != 0; bits--)
|
||||
{
|
||||
n = s.bl_count[bits];
|
||||
while (n != 0)
|
||||
{
|
||||
m = s.heap[--h];
|
||||
if (m > max_code)
|
||||
continue;
|
||||
if (tree[m * 2 + 1] != bits)
|
||||
{
|
||||
s.opt_len = (int) (s.opt_len + ((long) bits - (long) tree[m * 2 + 1]) * (long) tree[m * 2]);
|
||||
tree[m * 2 + 1] = (short) bits;
|
||||
}
|
||||
n--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Construct one Huffman tree and assigns the code bit strings and lengths.
|
||||
// Update the total bit length for the current block.
|
||||
// IN assertion: the field freq is set for all tree elements.
|
||||
// OUT assertions: the fields len and code are set to the optimal bit length
|
||||
// and corresponding code. The length opt_len is updated; static_len is
|
||||
// also updated if stree is not null. The field max_code is set.
|
||||
internal void build_tree(DeflateManager s)
|
||||
{
|
||||
short[] tree = dyn_tree;
|
||||
short[] stree = staticTree.treeCodes;
|
||||
int elems = staticTree.elems;
|
||||
int n, m; // iterate over heap elements
|
||||
int max_code = -1; // largest code with non zero frequency
|
||||
int node; // new node being created
|
||||
|
||||
// Construct the initial heap, with least frequent element in
|
||||
// heap[1]. The sons of heap[n] are heap[2*n] and heap[2*n+1].
|
||||
// heap[0] is not used.
|
||||
s.heap_len = 0;
|
||||
s.heap_max = HEAP_SIZE;
|
||||
|
||||
for (n = 0; n < elems; n++)
|
||||
{
|
||||
if (tree[n * 2] != 0)
|
||||
{
|
||||
s.heap[++s.heap_len] = max_code = n;
|
||||
s.depth[n] = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
tree[n * 2 + 1] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// The pkzip format requires that at least one distance code exists,
|
||||
// and that at least one bit should be sent even if there is only one
|
||||
// possible code. So to avoid special checks later on we force at least
|
||||
// two codes of non zero frequency.
|
||||
while (s.heap_len < 2)
|
||||
{
|
||||
node = s.heap[++s.heap_len] = (max_code < 2?++max_code:0);
|
||||
tree[node * 2] = 1;
|
||||
s.depth[node] = 0;
|
||||
s.opt_len--;
|
||||
if (stree != null)
|
||||
s.static_len -= stree[node * 2 + 1];
|
||||
// node is 0 or 1 so it does not have extra bits
|
||||
}
|
||||
this.max_code = max_code;
|
||||
|
||||
// The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
|
||||
// establish sub-heaps of increasing lengths:
|
||||
|
||||
for (n = s.heap_len / 2; n >= 1; n--)
|
||||
s.pqdownheap(tree, n);
|
||||
|
||||
// Construct the Huffman tree by repeatedly combining the least two
|
||||
// frequent nodes.
|
||||
|
||||
node = elems; // next internal node of the tree
|
||||
do
|
||||
{
|
||||
// n = node of least frequency
|
||||
n = s.heap[1];
|
||||
s.heap[1] = s.heap[s.heap_len--];
|
||||
s.pqdownheap(tree, 1);
|
||||
m = s.heap[1]; // m = node of next least frequency
|
||||
|
||||
s.heap[--s.heap_max] = n; // keep the nodes sorted by frequency
|
||||
s.heap[--s.heap_max] = m;
|
||||
|
||||
// Create a new node father of n and m
|
||||
tree[node * 2] = unchecked((short) (tree[n * 2] + tree[m * 2]));
|
||||
s.depth[node] = (sbyte) (System.Math.Max((byte) s.depth[n], (byte) s.depth[m]) + 1);
|
||||
tree[n * 2 + 1] = tree[m * 2 + 1] = (short) node;
|
||||
|
||||
// and insert the new node in the heap
|
||||
s.heap[1] = node++;
|
||||
s.pqdownheap(tree, 1);
|
||||
}
|
||||
while (s.heap_len >= 2);
|
||||
|
||||
s.heap[--s.heap_max] = s.heap[1];
|
||||
|
||||
// At this point, the fields freq and dad are set. We can now
|
||||
// generate the bit lengths.
|
||||
|
||||
gen_bitlen(s);
|
||||
|
||||
// The field len is now set, we can generate the bit codes
|
||||
gen_codes(tree, max_code, s.bl_count);
|
||||
}
|
||||
|
||||
// Generate the codes for a given tree and bit counts (which need not be
|
||||
// optimal).
|
||||
// IN assertion: the array bl_count contains the bit length statistics for
|
||||
// the given tree and the field len is set for all tree elements.
|
||||
// OUT assertion: the field code is set for all tree elements of non
|
||||
// zero code length.
|
||||
internal static void gen_codes(short[] tree, int max_code, short[] bl_count)
|
||||
{
|
||||
short[] next_code = new short[InternalConstants.MAX_BITS + 1]; // next code value for each bit length
|
||||
short code = 0; // running code value
|
||||
int bits; // bit index
|
||||
int n; // code index
|
||||
|
||||
// The distribution counts are first used to generate the code values
|
||||
// without bit reversal.
|
||||
for (bits = 1; bits <= InternalConstants.MAX_BITS; bits++)
|
||||
unchecked {
|
||||
next_code[bits] = code = (short) ((code + bl_count[bits - 1]) << 1);
|
||||
}
|
||||
|
||||
// Check that the bit counts in bl_count are consistent. The last code
|
||||
// must be all ones.
|
||||
//Assert (code + bl_count[MAX_BITS]-1 == (1<<MAX_BITS)-1,
|
||||
// "inconsistent bit counts");
|
||||
//Tracev((stderr,"\ngen_codes: max_code %d ", max_code));
|
||||
|
||||
for (n = 0; n <= max_code; n++)
|
||||
{
|
||||
int len = tree[n * 2 + 1];
|
||||
if (len == 0)
|
||||
continue;
|
||||
// Now reverse the bits
|
||||
tree[n * 2] = unchecked((short) (bi_reverse(next_code[len]++, len)));
|
||||
}
|
||||
}
|
||||
|
||||
// Reverse the first len bits of a code, using straightforward code (a faster
|
||||
// method would use a table)
|
||||
// IN assertion: 1 <= len <= 15
|
||||
internal static int bi_reverse(int code, int len)
|
||||
{
|
||||
int res = 0;
|
||||
do
|
||||
{
|
||||
res |= code & 1;
|
||||
code >>= 1; //SharedUtils.URShift(code, 1);
|
||||
res <<= 1;
|
||||
}
|
||||
while (--len > 0);
|
||||
return res >> 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,480 +0,0 @@
|
||||
// Zlib.cs
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (c) 2009-2011 Dino Chiesa and Microsoft Corporation.
|
||||
// All rights reserved.
|
||||
//
|
||||
// This code module is part of DotNetZip, a zipfile class library.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This code is licensed under the Microsoft Public License.
|
||||
// See the file License.txt for the license details.
|
||||
// More info on: http://dotnetzip.codeplex.com
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// Last Saved: <2011-August-03 19:52:28>
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This module defines classes for ZLIB compression and
|
||||
// decompression. This code is derived from the jzlib implementation of
|
||||
// zlib, but significantly modified. The object model is not the same,
|
||||
// and many of the behaviors are new or different. Nonetheless, in
|
||||
// keeping with the license for jzlib, the copyright to that code is
|
||||
// included below.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// The following notice applies to jzlib:
|
||||
//
|
||||
// Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright notice,
|
||||
// this list of conditions and the following disclaimer.
|
||||
//
|
||||
// 2. Redistributions in binary form must reproduce the above copyright
|
||||
// notice, this list of conditions and the following disclaimer in
|
||||
// the documentation and/or other materials provided with the distribution.
|
||||
//
|
||||
// 3. The names of the authors may not be used to endorse or promote products
|
||||
// derived from this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
// INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
//
|
||||
// jzlib is based on zlib-1.1.3.
|
||||
//
|
||||
// The following notice applies to zlib:
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (C) 1995-2004 Jean-loup Gailly and Mark Adler
|
||||
//
|
||||
// The ZLIB software is provided 'as-is', without any express or implied
|
||||
// warranty. In no event will the authors be held liable for any damages
|
||||
// arising from the use of this software.
|
||||
//
|
||||
// Permission is granted to anyone to use this software for any purpose,
|
||||
// including commercial applications, and to alter it and redistribute it
|
||||
// freely, subject to the following restrictions:
|
||||
//
|
||||
// 1. The origin of this software must not be misrepresented; you must not
|
||||
// claim that you wrote the original software. If you use this software
|
||||
// in a product, an acknowledgment in the product documentation would be
|
||||
// appreciated but is not required.
|
||||
// 2. Altered source versions must be plainly marked as such, and must not be
|
||||
// misrepresented as being the original software.
|
||||
// 3. This notice may not be removed or altered from any source distribution.
|
||||
//
|
||||
// Jean-loup Gailly jloup@gzip.org
|
||||
// Mark Adler madler@alumni.caltech.edu
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Compress.ZipFile.ZLib
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Describes how to flush the current deflate operation.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The different FlushType values are useful when using a Deflate in a streaming application.
|
||||
/// </remarks>
|
||||
public enum FlushType
|
||||
{
|
||||
/// <summary>No flush at all.</summary>
|
||||
None = 0,
|
||||
|
||||
/// <summary>Closes the current block, but doesn't flush it to
|
||||
/// the output. Used internally only in hypothetical
|
||||
/// scenarios. This was supposed to be removed by Zlib, but it is
|
||||
/// still in use in some edge cases.
|
||||
/// </summary>
|
||||
Partial,
|
||||
|
||||
/// <summary>
|
||||
/// Use this during compression to specify that all pending output should be
|
||||
/// flushed to the output buffer and the output should be aligned on a byte
|
||||
/// boundary. You might use this in a streaming communication scenario, so that
|
||||
/// the decompressor can get all input data available so far. When using this
|
||||
/// with a ZlibCodec, <c>AvailableBytesIn</c> will be zero after the call if
|
||||
/// enough output space has been provided before the call. Flushing will
|
||||
/// degrade compression and so it should be used only when necessary.
|
||||
/// </summary>
|
||||
Sync,
|
||||
|
||||
/// <summary>
|
||||
/// Use this during compression to specify that all output should be flushed, as
|
||||
/// with <c>FlushType.Sync</c>, but also, the compression state should be reset
|
||||
/// so that decompression can restart from this point if previous compressed
|
||||
/// data has been damaged or if random access is desired. Using
|
||||
/// <c>FlushType.Full</c> too often can significantly degrade the compression.
|
||||
/// </summary>
|
||||
Full,
|
||||
|
||||
/// <summary>Signals the end of the compression/decompression stream.</summary>
|
||||
Finish,
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// The compression level to be used when using a DeflateStream or ZlibStream with CompressionMode.Compress.
|
||||
/// </summary>
|
||||
public enum CompressionLevel
|
||||
{
|
||||
/// <summary>
|
||||
/// None means that the data will be simply stored, with no change at all.
|
||||
/// If you are producing ZIPs for use on Mac OSX, be aware that archives produced with CompressionLevel.None
|
||||
/// cannot be opened with the default zip reader. Use a different CompressionLevel.
|
||||
/// </summary>
|
||||
None= 0,
|
||||
/// <summary>
|
||||
/// Same as None.
|
||||
/// </summary>
|
||||
Level0 = 0,
|
||||
|
||||
/// <summary>
|
||||
/// The fastest but least effective compression.
|
||||
/// </summary>
|
||||
BestSpeed = 1,
|
||||
|
||||
/// <summary>
|
||||
/// A synonym for BestSpeed.
|
||||
/// </summary>
|
||||
Level1 = 1,
|
||||
|
||||
/// <summary>
|
||||
/// A little slower, but better, than level 1.
|
||||
/// </summary>
|
||||
Level2 = 2,
|
||||
|
||||
/// <summary>
|
||||
/// A little slower, but better, than level 2.
|
||||
/// </summary>
|
||||
Level3 = 3,
|
||||
|
||||
/// <summary>
|
||||
/// A little slower, but better, than level 3.
|
||||
/// </summary>
|
||||
Level4 = 4,
|
||||
|
||||
/// <summary>
|
||||
/// A little slower than level 4, but with better compression.
|
||||
/// </summary>
|
||||
Level5 = 5,
|
||||
|
||||
/// <summary>
|
||||
/// The default compression level, with a good balance of speed and compression efficiency.
|
||||
/// </summary>
|
||||
Default = 6,
|
||||
/// <summary>
|
||||
/// A synonym for Default.
|
||||
/// </summary>
|
||||
Level6 = 6,
|
||||
|
||||
/// <summary>
|
||||
/// Pretty good compression!
|
||||
/// </summary>
|
||||
Level7 = 7,
|
||||
|
||||
/// <summary>
|
||||
/// Better compression than Level7!
|
||||
/// </summary>
|
||||
Level8 = 8,
|
||||
|
||||
/// <summary>
|
||||
/// The "best" compression, where best means greatest reduction in size of the input data stream.
|
||||
/// This is also the slowest compression.
|
||||
/// </summary>
|
||||
BestCompression = 9,
|
||||
|
||||
/// <summary>
|
||||
/// A synonym for BestCompression.
|
||||
/// </summary>
|
||||
Level9 = 9,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Describes options for how the compression algorithm is executed. Different strategies
|
||||
/// work better on different sorts of data. The strategy parameter can affect the compression
|
||||
/// ratio and the speed of compression but not the correctness of the compresssion.
|
||||
/// </summary>
|
||||
public enum CompressionStrategy
|
||||
{
|
||||
/// <summary>
|
||||
/// The default strategy is probably the best for normal data.
|
||||
/// </summary>
|
||||
Default = 0,
|
||||
|
||||
/// <summary>
|
||||
/// The <c>Filtered</c> strategy is intended to be used most effectively with data produced by a
|
||||
/// filter or predictor. By this definition, filtered data consists mostly of small
|
||||
/// values with a somewhat random distribution. In this case, the compression algorithm
|
||||
/// is tuned to compress them better. The effect of <c>Filtered</c> is to force more Huffman
|
||||
/// coding and less string matching; it is a half-step between <c>Default</c> and <c>HuffmanOnly</c>.
|
||||
/// </summary>
|
||||
Filtered = 1,
|
||||
|
||||
/// <summary>
|
||||
/// Using <c>HuffmanOnly</c> will force the compressor to do Huffman encoding only, with no
|
||||
/// string matching.
|
||||
/// </summary>
|
||||
HuffmanOnly = 2,
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// An enum to specify the direction of transcoding - whether to compress or decompress.
|
||||
/// </summary>
|
||||
public enum CompressionMode
|
||||
{
|
||||
/// <summary>
|
||||
/// Used to specify that the stream should compress the data.
|
||||
/// </summary>
|
||||
Compress= 0,
|
||||
/// <summary>
|
||||
/// Used to specify that the stream should decompress the data.
|
||||
/// </summary>
|
||||
Decompress = 1,
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// A general purpose exception class for exceptions in the Zlib library.
|
||||
/// </summary>
|
||||
[Guid("ebc25cf6-9120-4283-b972-0e5520d0000E")]
|
||||
public class ZlibException : System.Exception
|
||||
{
|
||||
/// <summary>
|
||||
/// The ZlibException class captures exception information generated
|
||||
/// by the Zlib library.
|
||||
/// </summary>
|
||||
public ZlibException()
|
||||
: base()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This ctor collects a message attached to the exception.
|
||||
/// </summary>
|
||||
/// <param name="s">the message for the exception.</param>
|
||||
public ZlibException(System.String s)
|
||||
: base(s)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
internal class SharedUtils
|
||||
{
|
||||
/// <summary>
|
||||
/// Performs an unsigned bitwise right shift with the specified number
|
||||
/// </summary>
|
||||
/// <param name="number">Number to operate on</param>
|
||||
/// <param name="bits">Ammount of bits to shift</param>
|
||||
/// <returns>The resulting number from the shift operation</returns>
|
||||
public static int URShift(int number, int bits)
|
||||
{
|
||||
return (int)((uint)number >> bits);
|
||||
}
|
||||
}
|
||||
|
||||
internal static class InternalConstants
|
||||
{
|
||||
internal static readonly int MAX_BITS = 15;
|
||||
internal static readonly int BL_CODES = 19;
|
||||
internal static readonly int D_CODES = 30;
|
||||
internal static readonly int LITERALS = 256;
|
||||
internal static readonly int LENGTH_CODES = 29;
|
||||
internal static readonly int L_CODES = (LITERALS + 1 + LENGTH_CODES);
|
||||
|
||||
// Bit length codes must not exceed MAX_BL_BITS bits
|
||||
internal static readonly int MAX_BL_BITS = 7;
|
||||
|
||||
// repeat previous bit length 3-6 times (2 bits of repeat count)
|
||||
internal static readonly int REP_3_6 = 16;
|
||||
|
||||
// repeat a zero length 3-10 times (3 bits of repeat count)
|
||||
internal static readonly int REPZ_3_10 = 17;
|
||||
|
||||
// repeat a zero length 11-138 times (7 bits of repeat count)
|
||||
internal static readonly int REPZ_11_138 = 18;
|
||||
|
||||
}
|
||||
|
||||
internal sealed class StaticTree
|
||||
{
|
||||
internal static readonly short[] lengthAndLiteralsTreeCodes = new short[] {
|
||||
12, 8, 140, 8, 76, 8, 204, 8, 44, 8, 172, 8, 108, 8, 236, 8,
|
||||
28, 8, 156, 8, 92, 8, 220, 8, 60, 8, 188, 8, 124, 8, 252, 8,
|
||||
2, 8, 130, 8, 66, 8, 194, 8, 34, 8, 162, 8, 98, 8, 226, 8,
|
||||
18, 8, 146, 8, 82, 8, 210, 8, 50, 8, 178, 8, 114, 8, 242, 8,
|
||||
10, 8, 138, 8, 74, 8, 202, 8, 42, 8, 170, 8, 106, 8, 234, 8,
|
||||
26, 8, 154, 8, 90, 8, 218, 8, 58, 8, 186, 8, 122, 8, 250, 8,
|
||||
6, 8, 134, 8, 70, 8, 198, 8, 38, 8, 166, 8, 102, 8, 230, 8,
|
||||
22, 8, 150, 8, 86, 8, 214, 8, 54, 8, 182, 8, 118, 8, 246, 8,
|
||||
14, 8, 142, 8, 78, 8, 206, 8, 46, 8, 174, 8, 110, 8, 238, 8,
|
||||
30, 8, 158, 8, 94, 8, 222, 8, 62, 8, 190, 8, 126, 8, 254, 8,
|
||||
1, 8, 129, 8, 65, 8, 193, 8, 33, 8, 161, 8, 97, 8, 225, 8,
|
||||
17, 8, 145, 8, 81, 8, 209, 8, 49, 8, 177, 8, 113, 8, 241, 8,
|
||||
9, 8, 137, 8, 73, 8, 201, 8, 41, 8, 169, 8, 105, 8, 233, 8,
|
||||
25, 8, 153, 8, 89, 8, 217, 8, 57, 8, 185, 8, 121, 8, 249, 8,
|
||||
5, 8, 133, 8, 69, 8, 197, 8, 37, 8, 165, 8, 101, 8, 229, 8,
|
||||
21, 8, 149, 8, 85, 8, 213, 8, 53, 8, 181, 8, 117, 8, 245, 8,
|
||||
13, 8, 141, 8, 77, 8, 205, 8, 45, 8, 173, 8, 109, 8, 237, 8,
|
||||
29, 8, 157, 8, 93, 8, 221, 8, 61, 8, 189, 8, 125, 8, 253, 8,
|
||||
19, 9, 275, 9, 147, 9, 403, 9, 83, 9, 339, 9, 211, 9, 467, 9,
|
||||
51, 9, 307, 9, 179, 9, 435, 9, 115, 9, 371, 9, 243, 9, 499, 9,
|
||||
11, 9, 267, 9, 139, 9, 395, 9, 75, 9, 331, 9, 203, 9, 459, 9,
|
||||
43, 9, 299, 9, 171, 9, 427, 9, 107, 9, 363, 9, 235, 9, 491, 9,
|
||||
27, 9, 283, 9, 155, 9, 411, 9, 91, 9, 347, 9, 219, 9, 475, 9,
|
||||
59, 9, 315, 9, 187, 9, 443, 9, 123, 9, 379, 9, 251, 9, 507, 9,
|
||||
7, 9, 263, 9, 135, 9, 391, 9, 71, 9, 327, 9, 199, 9, 455, 9,
|
||||
39, 9, 295, 9, 167, 9, 423, 9, 103, 9, 359, 9, 231, 9, 487, 9,
|
||||
23, 9, 279, 9, 151, 9, 407, 9, 87, 9, 343, 9, 215, 9, 471, 9,
|
||||
55, 9, 311, 9, 183, 9, 439, 9, 119, 9, 375, 9, 247, 9, 503, 9,
|
||||
15, 9, 271, 9, 143, 9, 399, 9, 79, 9, 335, 9, 207, 9, 463, 9,
|
||||
47, 9, 303, 9, 175, 9, 431, 9, 111, 9, 367, 9, 239, 9, 495, 9,
|
||||
31, 9, 287, 9, 159, 9, 415, 9, 95, 9, 351, 9, 223, 9, 479, 9,
|
||||
63, 9, 319, 9, 191, 9, 447, 9, 127, 9, 383, 9, 255, 9, 511, 9,
|
||||
0, 7, 64, 7, 32, 7, 96, 7, 16, 7, 80, 7, 48, 7, 112, 7,
|
||||
8, 7, 72, 7, 40, 7, 104, 7, 24, 7, 88, 7, 56, 7, 120, 7,
|
||||
4, 7, 68, 7, 36, 7, 100, 7, 20, 7, 84, 7, 52, 7, 116, 7,
|
||||
3, 8, 131, 8, 67, 8, 195, 8, 35, 8, 163, 8, 99, 8, 227, 8
|
||||
};
|
||||
|
||||
internal static readonly short[] distTreeCodes = new short[] {
|
||||
0, 5, 16, 5, 8, 5, 24, 5, 4, 5, 20, 5, 12, 5, 28, 5,
|
||||
2, 5, 18, 5, 10, 5, 26, 5, 6, 5, 22, 5, 14, 5, 30, 5,
|
||||
1, 5, 17, 5, 9, 5, 25, 5, 5, 5, 21, 5, 13, 5, 29, 5,
|
||||
3, 5, 19, 5, 11, 5, 27, 5, 7, 5, 23, 5 };
|
||||
|
||||
internal static readonly StaticTree Literals;
|
||||
internal static readonly StaticTree Distances;
|
||||
internal static readonly StaticTree BitLengths;
|
||||
|
||||
internal short[] treeCodes; // static tree or null
|
||||
internal int[] extraBits; // extra bits for each code or null
|
||||
internal int extraBase; // base index for extra_bits
|
||||
internal int elems; // max number of elements in the tree
|
||||
internal int maxLength; // max bit length for the codes
|
||||
|
||||
private StaticTree(short[] treeCodes, int[] extraBits, int extraBase, int elems, int maxLength)
|
||||
{
|
||||
this.treeCodes = treeCodes;
|
||||
this.extraBits = extraBits;
|
||||
this.extraBase = extraBase;
|
||||
this.elems = elems;
|
||||
this.maxLength = maxLength;
|
||||
}
|
||||
static StaticTree()
|
||||
{
|
||||
Literals = new StaticTree(lengthAndLiteralsTreeCodes, Tree.ExtraLengthBits, InternalConstants.LITERALS + 1, InternalConstants.L_CODES, InternalConstants.MAX_BITS);
|
||||
Distances = new StaticTree(distTreeCodes, Tree.ExtraDistanceBits, 0, InternalConstants.D_CODES, InternalConstants.MAX_BITS);
|
||||
BitLengths = new StaticTree(null, Tree.extra_blbits, 0, InternalConstants.BL_CODES, InternalConstants.MAX_BL_BITS);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Computes an Adler-32 checksum.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The Adler checksum is similar to a CRC checksum, but faster to compute, though less
|
||||
/// reliable. It is used in producing RFC1950 compressed streams. The Adler checksum
|
||||
/// is a required part of the "ZLIB" standard. Applications will almost never need to
|
||||
/// use this class directly.
|
||||
/// </remarks>
|
||||
///
|
||||
/// <exclude/>
|
||||
public sealed class Adler
|
||||
{
|
||||
// largest prime smaller than 65536
|
||||
private static readonly uint BASE = 65521;
|
||||
// NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1
|
||||
private static readonly int NMAX = 5552;
|
||||
|
||||
/// <summary>
|
||||
/// Calculates the Adler32 checksum.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// This is used within ZLIB. You probably don't need to use this directly.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
/// <example>
|
||||
/// To compute an Adler32 checksum on a byte array:
|
||||
/// <code>
|
||||
/// var adler = Adler.Adler32(0, null, 0, 0);
|
||||
/// adler = Adler.Adler32(adler, buffer, index, length);
|
||||
/// </code>
|
||||
/// </example>
|
||||
public static uint Adler32(uint adler, byte[] buf, int index, int len)
|
||||
{
|
||||
if (buf == null)
|
||||
return 1;
|
||||
|
||||
uint s1 = (uint) (adler & 0xffff);
|
||||
uint s2 = (uint) ((adler >> 16) & 0xffff);
|
||||
|
||||
while (len > 0)
|
||||
{
|
||||
int k = len < NMAX ? len : NMAX;
|
||||
len -= k;
|
||||
while (k >= 16)
|
||||
{
|
||||
//s1 += (buf[index++] & 0xff); s2 += s1;
|
||||
s1 += buf[index++]; s2 += s1;
|
||||
s1 += buf[index++]; s2 += s1;
|
||||
s1 += buf[index++]; s2 += s1;
|
||||
s1 += buf[index++]; s2 += s1;
|
||||
s1 += buf[index++]; s2 += s1;
|
||||
s1 += buf[index++]; s2 += s1;
|
||||
s1 += buf[index++]; s2 += s1;
|
||||
s1 += buf[index++]; s2 += s1;
|
||||
s1 += buf[index++]; s2 += s1;
|
||||
s1 += buf[index++]; s2 += s1;
|
||||
s1 += buf[index++]; s2 += s1;
|
||||
s1 += buf[index++]; s2 += s1;
|
||||
s1 += buf[index++]; s2 += s1;
|
||||
s1 += buf[index++]; s2 += s1;
|
||||
s1 += buf[index++]; s2 += s1;
|
||||
s1 += buf[index++]; s2 += s1;
|
||||
k -= 16;
|
||||
}
|
||||
if (k != 0)
|
||||
{
|
||||
do
|
||||
{
|
||||
s1 += buf[index++];
|
||||
s2 += s1;
|
||||
}
|
||||
while (--k != 0);
|
||||
}
|
||||
s1 %= BASE;
|
||||
s2 %= BASE;
|
||||
}
|
||||
return (uint)((s2 << 16) | s1);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,557 +0,0 @@
|
||||
// ZlibBaseStream.cs
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (c) 2009 Dino Chiesa and Microsoft Corporation.
|
||||
// All rights reserved.
|
||||
//
|
||||
// This code module is part of DotNetZip, a zipfile class library.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This code is licensed under the Microsoft Public License.
|
||||
// See the file License.txt for the license details.
|
||||
// More info on: http://dotnetzip.codeplex.com
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// last saved (in emacs):
|
||||
// Time-stamp: <2011-August-06 21:22:38>
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This module defines the ZlibBaseStream class, which is an intnernal
|
||||
// base class for DeflateStream, ZlibStream and GZipStream.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace Compress.ZipFile.ZLib
|
||||
{
|
||||
|
||||
public enum ZlibStreamFlavor { ZLIB = 1950, DEFLATE = 1951, GZIP = 1952 }
|
||||
|
||||
public class ZlibBaseStream : System.IO.Stream
|
||||
{
|
||||
protected internal ZlibCodec _z = null; // deferred init... new ZlibCodec();
|
||||
|
||||
protected internal StreamMode _streamMode = StreamMode.Undefined;
|
||||
protected internal FlushType _flushMode;
|
||||
protected internal ZlibStreamFlavor _flavor;
|
||||
protected internal CompressionMode _compressionMode;
|
||||
protected internal CompressionLevel _level;
|
||||
protected internal bool _leaveOpen;
|
||||
protected internal byte[] _workingBuffer;
|
||||
protected internal int _bufferSize = ZlibConstants.WorkingBufferSizeDefault;
|
||||
protected internal byte[] _buf1 = new byte[1];
|
||||
|
||||
protected internal System.IO.Stream _stream;
|
||||
protected internal CompressionStrategy Strategy = CompressionStrategy.Default;
|
||||
|
||||
// workitem 7159
|
||||
Compress.Utils.CRC crc;
|
||||
protected internal string _GzipFileName;
|
||||
protected internal string _GzipComment;
|
||||
protected internal DateTime _GzipMtime;
|
||||
protected internal int _gzipHeaderByteCount;
|
||||
|
||||
internal int Crc32 { get { if (crc == null) return 0; return crc.Crc32Result; } }
|
||||
|
||||
public ZlibBaseStream(System.IO.Stream stream,
|
||||
CompressionMode compressionMode,
|
||||
CompressionLevel level,
|
||||
ZlibStreamFlavor flavor,
|
||||
bool leaveOpen)
|
||||
: base()
|
||||
{
|
||||
this._flushMode = FlushType.None;
|
||||
//this._workingBuffer = new byte[WORKING_BUFFER_SIZE_DEFAULT];
|
||||
this._stream = stream;
|
||||
this._leaveOpen = leaveOpen;
|
||||
this._compressionMode = compressionMode;
|
||||
this._flavor = flavor;
|
||||
this._level = level;
|
||||
// workitem 7159
|
||||
if (flavor == ZlibStreamFlavor.GZIP)
|
||||
{
|
||||
this.crc = new Compress.Utils.CRC();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected internal bool _wantCompress
|
||||
{
|
||||
get
|
||||
{
|
||||
return (this._compressionMode == CompressionMode.Compress);
|
||||
}
|
||||
}
|
||||
|
||||
private ZlibCodec z
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_z == null)
|
||||
{
|
||||
bool wantRfc1950Header = (this._flavor == ZlibStreamFlavor.ZLIB);
|
||||
_z = new ZlibCodec();
|
||||
if (this._compressionMode == CompressionMode.Decompress)
|
||||
{
|
||||
_z.InitializeInflate(wantRfc1950Header);
|
||||
}
|
||||
else
|
||||
{
|
||||
_z.Strategy = Strategy;
|
||||
_z.InitializeDeflate(this._level, wantRfc1950Header);
|
||||
}
|
||||
}
|
||||
return _z;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
private byte[] workingBuffer
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_workingBuffer == null)
|
||||
_workingBuffer = new byte[_bufferSize];
|
||||
return _workingBuffer;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public override void Write(System.Byte[] buffer, int offset, int count)
|
||||
{
|
||||
// workitem 7159
|
||||
// calculate the CRC on the unccompressed data (before writing)
|
||||
if (crc != null)
|
||||
crc.SlurpBlock(buffer, offset, count);
|
||||
|
||||
if (_streamMode == StreamMode.Undefined)
|
||||
_streamMode = StreamMode.Writer;
|
||||
else if (_streamMode != StreamMode.Writer)
|
||||
throw new ZlibException("Cannot Write after Reading.");
|
||||
|
||||
if (count == 0)
|
||||
return;
|
||||
|
||||
// first reference of z property will initialize the private var _z
|
||||
z.InputBuffer = buffer;
|
||||
_z.NextIn = offset;
|
||||
_z.AvailableBytesIn = count;
|
||||
bool done = false;
|
||||
do
|
||||
{
|
||||
_z.OutputBuffer = workingBuffer;
|
||||
_z.NextOut = 0;
|
||||
_z.AvailableBytesOut = _workingBuffer.Length;
|
||||
int rc = (_wantCompress)
|
||||
? _z.Deflate(_flushMode)
|
||||
: _z.Inflate(_flushMode);
|
||||
if (rc != ZlibConstants.Z_OK && rc != ZlibConstants.Z_STREAM_END)
|
||||
throw new ZlibException((_wantCompress ? "de" : "in") + "flating: " + _z.Message);
|
||||
|
||||
//if (_workingBuffer.Length - _z.AvailableBytesOut > 0)
|
||||
_stream.Write(_workingBuffer, 0, _workingBuffer.Length - _z.AvailableBytesOut);
|
||||
|
||||
done = _z.AvailableBytesIn == 0 && _z.AvailableBytesOut != 0;
|
||||
|
||||
// If GZIP and de-compress, we're done when 8 bytes remain.
|
||||
if (_flavor == ZlibStreamFlavor.GZIP && !_wantCompress)
|
||||
done = (_z.AvailableBytesIn == 8 && _z.AvailableBytesOut != 0);
|
||||
|
||||
}
|
||||
while (!done);
|
||||
}
|
||||
|
||||
|
||||
|
||||
private void finish()
|
||||
{
|
||||
if (_z == null) return;
|
||||
|
||||
if (_streamMode == StreamMode.Writer)
|
||||
{
|
||||
bool done = false;
|
||||
do
|
||||
{
|
||||
_z.OutputBuffer = workingBuffer;
|
||||
_z.NextOut = 0;
|
||||
_z.AvailableBytesOut = _workingBuffer.Length;
|
||||
int rc = (_wantCompress)
|
||||
? _z.Deflate(FlushType.Finish)
|
||||
: _z.Inflate(FlushType.Finish);
|
||||
|
||||
if (rc != ZlibConstants.Z_STREAM_END && rc != ZlibConstants.Z_OK)
|
||||
{
|
||||
string verb = (_wantCompress ? "de" : "in") + "flating";
|
||||
if (_z.Message == null)
|
||||
throw new ZlibException(String.Format("{0}: (rc = {1})", verb, rc));
|
||||
else
|
||||
throw new ZlibException(verb + ": " + _z.Message);
|
||||
}
|
||||
|
||||
if (_workingBuffer.Length - _z.AvailableBytesOut > 0)
|
||||
{
|
||||
_stream.Write(_workingBuffer, 0, _workingBuffer.Length - _z.AvailableBytesOut);
|
||||
}
|
||||
|
||||
done = _z.AvailableBytesIn == 0 && _z.AvailableBytesOut != 0;
|
||||
// If GZIP and de-compress, we're done when 8 bytes remain.
|
||||
if (_flavor == ZlibStreamFlavor.GZIP && !_wantCompress)
|
||||
done = (_z.AvailableBytesIn == 8 && _z.AvailableBytesOut != 0);
|
||||
|
||||
}
|
||||
while (!done);
|
||||
|
||||
Flush();
|
||||
|
||||
// workitem 7159
|
||||
if (_flavor == ZlibStreamFlavor.GZIP)
|
||||
{
|
||||
if (_wantCompress)
|
||||
{
|
||||
// Emit the GZIP trailer: CRC32 and size mod 2^32
|
||||
int c1 = crc.Crc32Result;
|
||||
_stream.Write(BitConverter.GetBytes(c1), 0, 4);
|
||||
int c2 = (Int32)(crc.TotalBytesRead & 0x00000000FFFFFFFF);
|
||||
_stream.Write(BitConverter.GetBytes(c2), 0, 4);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ZlibException("Writing with decompression is not supported.");
|
||||
}
|
||||
}
|
||||
}
|
||||
// workitem 7159
|
||||
else if (_streamMode == StreamMode.Reader)
|
||||
{
|
||||
if (_flavor == ZlibStreamFlavor.GZIP)
|
||||
{
|
||||
if (!_wantCompress)
|
||||
{
|
||||
// workitem 8501: handle edge case (decompress empty stream)
|
||||
if (_z.TotalBytesOut == 0L)
|
||||
return;
|
||||
|
||||
// Read and potentially verify the GZIP trailer:
|
||||
// CRC32 and size mod 2^32
|
||||
byte[] trailer = new byte[8];
|
||||
|
||||
// workitems 8679 & 12554
|
||||
if (_z.AvailableBytesIn < 8)
|
||||
{
|
||||
// Make sure we have read to the end of the stream
|
||||
Array.Copy(_z.InputBuffer, _z.NextIn, trailer, 0, _z.AvailableBytesIn);
|
||||
int bytesNeeded = 8 - _z.AvailableBytesIn;
|
||||
int bytesRead = _stream.Read(trailer,
|
||||
_z.AvailableBytesIn,
|
||||
bytesNeeded);
|
||||
if (bytesNeeded != bytesRead)
|
||||
{
|
||||
throw new ZlibException(String.Format("Missing or incomplete GZIP trailer. Expected 8 bytes, got {0}.",
|
||||
_z.AvailableBytesIn + bytesRead));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Array.Copy(_z.InputBuffer, _z.NextIn, trailer, 0, trailer.Length);
|
||||
}
|
||||
|
||||
Int32 crc32_expected = BitConverter.ToInt32(trailer, 0);
|
||||
Int32 crc32_actual = crc.Crc32Result;
|
||||
Int32 isize_expected = BitConverter.ToInt32(trailer, 4);
|
||||
Int32 isize_actual = (Int32)(_z.TotalBytesOut & 0x00000000FFFFFFFF);
|
||||
|
||||
if (crc32_actual != crc32_expected)
|
||||
throw new ZlibException(String.Format("Bad CRC32 in GZIP trailer. (actual({0:X8})!=expected({1:X8}))", crc32_actual, crc32_expected));
|
||||
|
||||
if (isize_actual != isize_expected)
|
||||
throw new ZlibException(String.Format("Bad size in GZIP trailer. (actual({0})!=expected({1}))", isize_actual, isize_expected));
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ZlibException("Reading with compression is not supported.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void end()
|
||||
{
|
||||
if (z == null)
|
||||
return;
|
||||
if (_wantCompress)
|
||||
{
|
||||
_z.EndDeflate();
|
||||
}
|
||||
else
|
||||
{
|
||||
_z.EndInflate();
|
||||
}
|
||||
_z = null;
|
||||
}
|
||||
|
||||
|
||||
public override void Close()
|
||||
{
|
||||
if (_stream == null) return;
|
||||
try
|
||||
{
|
||||
finish();
|
||||
}
|
||||
finally
|
||||
{
|
||||
end();
|
||||
if (!_leaveOpen) _stream.Close();
|
||||
_stream = null;
|
||||
}
|
||||
}
|
||||
|
||||
public override void Flush()
|
||||
{
|
||||
_stream.Flush();
|
||||
}
|
||||
|
||||
public override System.Int64 Seek(System.Int64 offset, System.IO.SeekOrigin origin)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
//_outStream.Seek(offset, origin);
|
||||
}
|
||||
public override void SetLength(System.Int64 value)
|
||||
{
|
||||
_stream.SetLength(value);
|
||||
}
|
||||
|
||||
private bool nomoreinput = false;
|
||||
|
||||
|
||||
|
||||
private string ReadZeroTerminatedString()
|
||||
{
|
||||
var list = new System.Collections.Generic.List<byte>();
|
||||
bool done = false;
|
||||
do
|
||||
{
|
||||
// workitem 7740
|
||||
int n = _stream.Read(_buf1, 0, 1);
|
||||
if (n != 1)
|
||||
throw new ZlibException("Unexpected EOF reading GZIP header.");
|
||||
else
|
||||
{
|
||||
if (_buf1[0] == 0)
|
||||
done = true;
|
||||
else
|
||||
list.Add(_buf1[0]);
|
||||
}
|
||||
} while (!done);
|
||||
byte[] a = list.ToArray();
|
||||
return iso8859dash1.GetString(a, 0, a.Length);
|
||||
}
|
||||
internal static readonly System.Text.Encoding iso8859dash1 = System.Text.Encoding.GetEncoding("iso-8859-1");
|
||||
internal static readonly System.DateTime _unixEpoch = new System.DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
|
||||
|
||||
|
||||
private int _ReadAndValidateGzipHeader()
|
||||
{
|
||||
int totalBytesRead = 0;
|
||||
// read the header on the first read
|
||||
byte[] header = new byte[10];
|
||||
int n = _stream.Read(header, 0, header.Length);
|
||||
|
||||
// workitem 8501: handle edge case (decompress empty stream)
|
||||
if (n == 0)
|
||||
return 0;
|
||||
|
||||
if (n != 10)
|
||||
throw new ZlibException("Not a valid GZIP stream.");
|
||||
|
||||
if (header[0] != 0x1F || header[1] != 0x8B || header[2] != 8)
|
||||
throw new ZlibException("Bad GZIP header.");
|
||||
|
||||
Int32 timet = BitConverter.ToInt32(header, 4);
|
||||
_GzipMtime = _unixEpoch.AddSeconds(timet);
|
||||
totalBytesRead += n;
|
||||
if ((header[3] & 0x04) == 0x04)
|
||||
{
|
||||
// read and discard extra field
|
||||
n = _stream.Read(header, 0, 2); // 2-byte length field
|
||||
totalBytesRead += n;
|
||||
|
||||
Int16 extraLength = (Int16)(header[0] + header[1] * 256);
|
||||
byte[] extra = new byte[extraLength];
|
||||
n = _stream.Read(extra, 0, extra.Length);
|
||||
if (n != extraLength)
|
||||
throw new ZlibException("Unexpected end-of-file reading GZIP header.");
|
||||
totalBytesRead += n;
|
||||
}
|
||||
if ((header[3] & 0x08) == 0x08)
|
||||
_GzipFileName = ReadZeroTerminatedString();
|
||||
if ((header[3] & 0x10) == 0x010)
|
||||
_GzipComment = ReadZeroTerminatedString();
|
||||
if ((header[3] & 0x02) == 0x02)
|
||||
Read(_buf1, 0, 1); // CRC16, ignore
|
||||
|
||||
return totalBytesRead;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public override System.Int32 Read(System.Byte[] buffer, System.Int32 offset, System.Int32 count)
|
||||
{
|
||||
// According to MS documentation, any implementation of the IO.Stream.Read function must:
|
||||
// (a) throw an exception if offset & count reference an invalid part of the buffer,
|
||||
// or if count < 0, or if buffer is null
|
||||
// (b) return 0 only upon EOF, or if count = 0
|
||||
// (c) if not EOF, then return at least 1 byte, up to <count> bytes
|
||||
|
||||
if (_streamMode == StreamMode.Undefined)
|
||||
{
|
||||
if (!this._stream.CanRead) throw new ZlibException("The stream is not readable.");
|
||||
// for the first read, set up some controls.
|
||||
_streamMode = StreamMode.Reader;
|
||||
// (The first reference to _z goes through the private accessor which
|
||||
// may initialize it.)
|
||||
z.AvailableBytesIn = 0;
|
||||
if (_flavor == ZlibStreamFlavor.GZIP)
|
||||
{
|
||||
_gzipHeaderByteCount = _ReadAndValidateGzipHeader();
|
||||
// workitem 8501: handle edge case (decompress empty stream)
|
||||
if (_gzipHeaderByteCount == 0)
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (_streamMode != StreamMode.Reader)
|
||||
throw new ZlibException("Cannot Read after Writing.");
|
||||
|
||||
if (count == 0) return 0;
|
||||
if (nomoreinput && _wantCompress) return 0; // workitem 8557
|
||||
if (buffer == null) throw new ArgumentNullException("buffer");
|
||||
if (count < 0) throw new ArgumentOutOfRangeException("count");
|
||||
if (offset < buffer.GetLowerBound(0)) throw new ArgumentOutOfRangeException("offset");
|
||||
if ((offset + count) > buffer.GetLength(0)) throw new ArgumentOutOfRangeException("count");
|
||||
|
||||
int rc = 0;
|
||||
|
||||
// set up the output of the deflate/inflate codec:
|
||||
_z.OutputBuffer = buffer;
|
||||
_z.NextOut = offset;
|
||||
_z.AvailableBytesOut = count;
|
||||
|
||||
// This is necessary in case _workingBuffer has been resized. (new byte[])
|
||||
// (The first reference to _workingBuffer goes through the private accessor which
|
||||
// may initialize it.)
|
||||
_z.InputBuffer = workingBuffer;
|
||||
|
||||
do
|
||||
{
|
||||
// need data in _workingBuffer in order to deflate/inflate. Here, we check if we have any.
|
||||
if ((_z.AvailableBytesIn == 0) && (!nomoreinput))
|
||||
{
|
||||
// No data available, so try to Read data from the captive stream.
|
||||
_z.NextIn = 0;
|
||||
_z.AvailableBytesIn = _stream.Read(_workingBuffer, 0, _workingBuffer.Length);
|
||||
if (_z.AvailableBytesIn == 0)
|
||||
nomoreinput = true;
|
||||
|
||||
}
|
||||
// we have data in InputBuffer; now compress or decompress as appropriate
|
||||
rc = (_wantCompress)
|
||||
? _z.Deflate(_flushMode)
|
||||
: _z.Inflate(_flushMode);
|
||||
|
||||
if (nomoreinput && (rc == ZlibConstants.Z_BUF_ERROR))
|
||||
return 0;
|
||||
|
||||
if (rc != ZlibConstants.Z_OK && rc != ZlibConstants.Z_STREAM_END)
|
||||
throw new ZlibException(String.Format("{0}flating: rc={1} msg={2}", (_wantCompress ? "de" : "in"), rc, _z.Message));
|
||||
|
||||
if ((nomoreinput || rc == ZlibConstants.Z_STREAM_END) && (_z.AvailableBytesOut == count))
|
||||
break; // nothing more to read
|
||||
}
|
||||
//while (_z.AvailableBytesOut == count && rc == ZlibConstants.Z_OK);
|
||||
while (_z.AvailableBytesOut > 0 && !nomoreinput && rc == ZlibConstants.Z_OK);
|
||||
|
||||
|
||||
// workitem 8557
|
||||
// is there more room in output?
|
||||
if (_z.AvailableBytesOut > 0)
|
||||
{
|
||||
if (rc == ZlibConstants.Z_OK && _z.AvailableBytesIn == 0)
|
||||
{
|
||||
// deferred
|
||||
}
|
||||
|
||||
// are we completely done reading?
|
||||
if (nomoreinput)
|
||||
{
|
||||
// and in compression?
|
||||
if (_wantCompress)
|
||||
{
|
||||
// no more input data available; therefore we flush to
|
||||
// try to complete the read
|
||||
rc = _z.Deflate(FlushType.Finish);
|
||||
|
||||
if (rc != ZlibConstants.Z_OK && rc != ZlibConstants.Z_STREAM_END)
|
||||
throw new ZlibException(String.Format("Deflating: rc={0} msg={1}", rc, _z.Message));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
rc = (count - _z.AvailableBytesOut);
|
||||
|
||||
// calculate CRC after reading
|
||||
if (crc != null)
|
||||
crc.SlurpBlock(buffer, offset, rc);
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public override System.Boolean CanRead
|
||||
{
|
||||
get { return this._stream.CanRead; }
|
||||
}
|
||||
|
||||
public override System.Boolean CanSeek
|
||||
{
|
||||
get { return this._stream.CanSeek; }
|
||||
}
|
||||
|
||||
public override System.Boolean CanWrite
|
||||
{
|
||||
get { return this._stream.CanWrite; }
|
||||
}
|
||||
|
||||
public override System.Int64 Length
|
||||
{
|
||||
get { return _stream.Length; }
|
||||
}
|
||||
|
||||
public override long Position
|
||||
{
|
||||
get { throw new NotImplementedException(); }
|
||||
set { throw new NotImplementedException(); }
|
||||
}
|
||||
|
||||
public enum StreamMode
|
||||
{
|
||||
Writer,
|
||||
Reader,
|
||||
Undefined,
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,718 +0,0 @@
|
||||
// ZlibCodec.cs
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (c) 2009 Dino Chiesa and Microsoft Corporation.
|
||||
// All rights reserved.
|
||||
//
|
||||
// This code module is part of DotNetZip, a zipfile class library.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This code is licensed under the Microsoft Public License.
|
||||
// See the file License.txt for the license details.
|
||||
// More info on: http://dotnetzip.codeplex.com
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// last saved (in emacs):
|
||||
// Time-stamp: <2009-November-03 15:40:51>
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This module defines a Codec for ZLIB compression and
|
||||
// decompression. This code extends code that was based the jzlib
|
||||
// implementation of zlib, but this code is completely novel. The codec
|
||||
// class is new, and encapsulates some behaviors that are new, and some
|
||||
// that were present in other classes in the jzlib code base. In
|
||||
// keeping with the license for jzlib, the copyright to the jzlib code
|
||||
// is included below.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright notice,
|
||||
// this list of conditions and the following disclaimer.
|
||||
//
|
||||
// 2. Redistributions in binary form must reproduce the above copyright
|
||||
// notice, this list of conditions and the following disclaimer in
|
||||
// the documentation and/or other materials provided with the distribution.
|
||||
//
|
||||
// 3. The names of the authors may not be used to endorse or promote products
|
||||
// derived from this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
// INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
//
|
||||
// This program is based on zlib-1.1.3; credit to authors
|
||||
// Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
|
||||
// and contributors of zlib.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using Interop=System.Runtime.InteropServices;
|
||||
|
||||
namespace Compress.ZipFile.ZLib
|
||||
{
|
||||
/// <summary>
|
||||
/// Encoder and Decoder for ZLIB and DEFLATE (IETF RFC1950 and RFC1951).
|
||||
/// </summary>
|
||||
///
|
||||
/// <remarks>
|
||||
/// This class compresses and decompresses data according to the Deflate algorithm
|
||||
/// and optionally, the ZLIB format, as documented in <see
|
||||
/// href="http://www.ietf.org/rfc/rfc1950.txt">RFC 1950 - ZLIB</see> and <see
|
||||
/// href="http://www.ietf.org/rfc/rfc1951.txt">RFC 1951 - DEFLATE</see>.
|
||||
/// </remarks>
|
||||
[Guid("ebc25cf6-9120-4283-b972-0e5520d0000D")]
|
||||
[Interop.ComVisible(true)]
|
||||
#if !NETCF
|
||||
[Interop.ClassInterface(Interop.ClassInterfaceType.AutoDispatch)]
|
||||
#endif
|
||||
sealed public class ZlibCodec
|
||||
{
|
||||
/// <summary>
|
||||
/// The buffer from which data is taken.
|
||||
/// </summary>
|
||||
public byte[] InputBuffer;
|
||||
|
||||
/// <summary>
|
||||
/// An index into the InputBuffer array, indicating where to start reading.
|
||||
/// </summary>
|
||||
public int NextIn;
|
||||
|
||||
/// <summary>
|
||||
/// The number of bytes available in the InputBuffer, starting at NextIn.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Generally you should set this to InputBuffer.Length before the first Inflate() or Deflate() call.
|
||||
/// The class will update this number as calls to Inflate/Deflate are made.
|
||||
/// </remarks>
|
||||
public int AvailableBytesIn;
|
||||
|
||||
/// <summary>
|
||||
/// Total number of bytes read so far, through all calls to Inflate()/Deflate().
|
||||
/// </summary>
|
||||
public long TotalBytesIn;
|
||||
|
||||
/// <summary>
|
||||
/// Buffer to store output data.
|
||||
/// </summary>
|
||||
public byte[] OutputBuffer;
|
||||
|
||||
/// <summary>
|
||||
/// An index into the OutputBuffer array, indicating where to start writing.
|
||||
/// </summary>
|
||||
public int NextOut;
|
||||
|
||||
/// <summary>
|
||||
/// The number of bytes available in the OutputBuffer, starting at NextOut.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Generally you should set this to OutputBuffer.Length before the first Inflate() or Deflate() call.
|
||||
/// The class will update this number as calls to Inflate/Deflate are made.
|
||||
/// </remarks>
|
||||
public int AvailableBytesOut;
|
||||
|
||||
/// <summary>
|
||||
/// Total number of bytes written to the output so far, through all calls to Inflate()/Deflate().
|
||||
/// </summary>
|
||||
public long TotalBytesOut;
|
||||
|
||||
/// <summary>
|
||||
/// used for diagnostics, when something goes wrong!
|
||||
/// </summary>
|
||||
public System.String Message;
|
||||
|
||||
internal DeflateManager dstate;
|
||||
internal InflateManager istate;
|
||||
|
||||
internal uint _Adler32;
|
||||
|
||||
/// <summary>
|
||||
/// The compression level to use in this codec. Useful only in compression mode.
|
||||
/// </summary>
|
||||
public CompressionLevel CompressLevel = CompressionLevel.Default;
|
||||
|
||||
/// <summary>
|
||||
/// The number of Window Bits to use.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This gauges the size of the sliding window, and hence the
|
||||
/// compression effectiveness as well as memory consumption. It's best to just leave this
|
||||
/// setting alone if you don't know what it is. The maximum value is 15 bits, which implies
|
||||
/// a 32k window.
|
||||
/// </remarks>
|
||||
public int WindowBits = ZlibConstants.WindowBitsDefault;
|
||||
|
||||
/// <summary>
|
||||
/// The compression strategy to use.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This is only effective in compression. The theory offered by ZLIB is that different
|
||||
/// strategies could potentially produce significant differences in compression behavior
|
||||
/// for different data sets. Unfortunately I don't have any good recommendations for how
|
||||
/// to set it differently. When I tested changing the strategy I got minimally different
|
||||
/// compression performance. It's best to leave this property alone if you don't have a
|
||||
/// good feel for it. Or, you may want to produce a test harness that runs through the
|
||||
/// different strategy options and evaluates them on different file types. If you do that,
|
||||
/// let me know your results.
|
||||
/// </remarks>
|
||||
public CompressionStrategy Strategy = CompressionStrategy.Default;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// The Adler32 checksum on the data transferred through the codec so far. You probably don't need to look at this.
|
||||
/// </summary>
|
||||
public int Adler32 { get { return (int)_Adler32; } }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Create a ZlibCodec.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// If you use this default constructor, you will later have to explicitly call
|
||||
/// InitializeInflate() or InitializeDeflate() before using the ZlibCodec to compress
|
||||
/// or decompress.
|
||||
/// </remarks>
|
||||
public ZlibCodec() { }
|
||||
|
||||
/// <summary>
|
||||
/// Create a ZlibCodec that either compresses or decompresses.
|
||||
/// </summary>
|
||||
/// <param name="mode">
|
||||
/// Indicates whether the codec should compress (deflate) or decompress (inflate).
|
||||
/// </param>
|
||||
public ZlibCodec(CompressionMode mode)
|
||||
{
|
||||
if (mode == CompressionMode.Compress)
|
||||
{
|
||||
int rc = InitializeDeflate();
|
||||
if (rc != ZlibConstants.Z_OK) throw new ZlibException("Cannot initialize for deflate.");
|
||||
}
|
||||
else if (mode == CompressionMode.Decompress)
|
||||
{
|
||||
int rc = InitializeInflate();
|
||||
if (rc != ZlibConstants.Z_OK) throw new ZlibException("Cannot initialize for inflate.");
|
||||
}
|
||||
else throw new ZlibException("Invalid ZlibStreamFlavor.");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initialize the inflation state.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// It is not necessary to call this before using the ZlibCodec to inflate data;
|
||||
/// It is implicitly called when you call the constructor.
|
||||
/// </remarks>
|
||||
/// <returns>Z_OK if everything goes well.</returns>
|
||||
public int InitializeInflate()
|
||||
{
|
||||
return InitializeInflate(this.WindowBits);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initialize the inflation state with an explicit flag to
|
||||
/// govern the handling of RFC1950 header bytes.
|
||||
/// </summary>
|
||||
///
|
||||
/// <remarks>
|
||||
/// By default, the ZLIB header defined in <see
|
||||
/// href="http://www.ietf.org/rfc/rfc1950.txt">RFC 1950</see> is expected. If
|
||||
/// you want to read a zlib stream you should specify true for
|
||||
/// expectRfc1950Header. If you have a deflate stream, you will want to specify
|
||||
/// false. It is only necessary to invoke this initializer explicitly if you
|
||||
/// want to specify false.
|
||||
/// </remarks>
|
||||
///
|
||||
/// <param name="expectRfc1950Header">whether to expect an RFC1950 header byte
|
||||
/// pair when reading the stream of data to be inflated.</param>
|
||||
///
|
||||
/// <returns>Z_OK if everything goes well.</returns>
|
||||
public int InitializeInflate(bool expectRfc1950Header)
|
||||
{
|
||||
return InitializeInflate(this.WindowBits, expectRfc1950Header);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initialize the ZlibCodec for inflation, with the specified number of window bits.
|
||||
/// </summary>
|
||||
/// <param name="windowBits">The number of window bits to use. If you need to ask what that is,
|
||||
/// then you shouldn't be calling this initializer.</param>
|
||||
/// <returns>Z_OK if all goes well.</returns>
|
||||
public int InitializeInflate(int windowBits)
|
||||
{
|
||||
this.WindowBits = windowBits;
|
||||
return InitializeInflate(windowBits, true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initialize the inflation state with an explicit flag to govern the handling of
|
||||
/// RFC1950 header bytes.
|
||||
/// </summary>
|
||||
///
|
||||
/// <remarks>
|
||||
/// If you want to read a zlib stream you should specify true for
|
||||
/// expectRfc1950Header. In this case, the library will expect to find a ZLIB
|
||||
/// header, as defined in <see href="http://www.ietf.org/rfc/rfc1950.txt">RFC
|
||||
/// 1950</see>, in the compressed stream. If you will be reading a DEFLATE or
|
||||
/// GZIP stream, which does not have such a header, you will want to specify
|
||||
/// false.
|
||||
/// </remarks>
|
||||
///
|
||||
/// <param name="expectRfc1950Header">whether to expect an RFC1950 header byte pair when reading
|
||||
/// the stream of data to be inflated.</param>
|
||||
/// <param name="windowBits">The number of window bits to use. If you need to ask what that is,
|
||||
/// then you shouldn't be calling this initializer.</param>
|
||||
/// <returns>Z_OK if everything goes well.</returns>
|
||||
public int InitializeInflate(int windowBits, bool expectRfc1950Header)
|
||||
{
|
||||
this.WindowBits = windowBits;
|
||||
if (dstate != null) throw new ZlibException("You may not call InitializeInflate() after calling InitializeDeflate().");
|
||||
istate = new InflateManager(expectRfc1950Header);
|
||||
return istate.Initialize(this, windowBits);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Inflate the data in the InputBuffer, placing the result in the OutputBuffer.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// You must have set InputBuffer and OutputBuffer, NextIn and NextOut, and AvailableBytesIn and
|
||||
/// AvailableBytesOut before calling this method.
|
||||
/// </remarks>
|
||||
/// <example>
|
||||
/// <code>
|
||||
/// private void InflateBuffer()
|
||||
/// {
|
||||
/// int bufferSize = 1024;
|
||||
/// byte[] buffer = new byte[bufferSize];
|
||||
/// ZlibCodec decompressor = new ZlibCodec();
|
||||
///
|
||||
/// Console.WriteLine("\n============================================");
|
||||
/// Console.WriteLine("Size of Buffer to Inflate: {0} bytes.", CompressedBytes.Length);
|
||||
/// MemoryStream ms = new MemoryStream(DecompressedBytes);
|
||||
///
|
||||
/// int rc = decompressor.InitializeInflate();
|
||||
///
|
||||
/// decompressor.InputBuffer = CompressedBytes;
|
||||
/// decompressor.NextIn = 0;
|
||||
/// decompressor.AvailableBytesIn = CompressedBytes.Length;
|
||||
///
|
||||
/// decompressor.OutputBuffer = buffer;
|
||||
///
|
||||
/// // pass 1: inflate
|
||||
/// do
|
||||
/// {
|
||||
/// decompressor.NextOut = 0;
|
||||
/// decompressor.AvailableBytesOut = buffer.Length;
|
||||
/// rc = decompressor.Inflate(FlushType.None);
|
||||
///
|
||||
/// if (rc != ZlibConstants.Z_OK && rc != ZlibConstants.Z_STREAM_END)
|
||||
/// throw new Exception("inflating: " + decompressor.Message);
|
||||
///
|
||||
/// ms.Write(decompressor.OutputBuffer, 0, buffer.Length - decompressor.AvailableBytesOut);
|
||||
/// }
|
||||
/// while (decompressor.AvailableBytesIn > 0 || decompressor.AvailableBytesOut == 0);
|
||||
///
|
||||
/// // pass 2: finish and flush
|
||||
/// do
|
||||
/// {
|
||||
/// decompressor.NextOut = 0;
|
||||
/// decompressor.AvailableBytesOut = buffer.Length;
|
||||
/// rc = decompressor.Inflate(FlushType.Finish);
|
||||
///
|
||||
/// if (rc != ZlibConstants.Z_STREAM_END && rc != ZlibConstants.Z_OK)
|
||||
/// throw new Exception("inflating: " + decompressor.Message);
|
||||
///
|
||||
/// if (buffer.Length - decompressor.AvailableBytesOut > 0)
|
||||
/// ms.Write(buffer, 0, buffer.Length - decompressor.AvailableBytesOut);
|
||||
/// }
|
||||
/// while (decompressor.AvailableBytesIn > 0 || decompressor.AvailableBytesOut == 0);
|
||||
///
|
||||
/// decompressor.EndInflate();
|
||||
/// }
|
||||
///
|
||||
/// </code>
|
||||
/// </example>
|
||||
/// <param name="flush">The flush to use when inflating.</param>
|
||||
/// <returns>Z_OK if everything goes well.</returns>
|
||||
public int Inflate(FlushType flush)
|
||||
{
|
||||
if (istate == null)
|
||||
throw new ZlibException("No Inflate State!");
|
||||
return istate.Inflate(flush);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Ends an inflation session.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Call this after successively calling Inflate(). This will cause all buffers to be flushed.
|
||||
/// After calling this you cannot call Inflate() without a intervening call to one of the
|
||||
/// InitializeInflate() overloads.
|
||||
/// </remarks>
|
||||
/// <returns>Z_OK if everything goes well.</returns>
|
||||
public int EndInflate()
|
||||
{
|
||||
if (istate == null)
|
||||
throw new ZlibException("No Inflate State!");
|
||||
int ret = istate.End();
|
||||
istate = null;
|
||||
return ret;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// I don't know what this does!
|
||||
/// </summary>
|
||||
/// <returns>Z_OK if everything goes well.</returns>
|
||||
public int SyncInflate()
|
||||
{
|
||||
if (istate == null)
|
||||
throw new ZlibException("No Inflate State!");
|
||||
return istate.Sync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initialize the ZlibCodec for deflation operation.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The codec will use the MAX window bits and the default level of compression.
|
||||
/// </remarks>
|
||||
/// <example>
|
||||
/// <code>
|
||||
/// int bufferSize = 40000;
|
||||
/// byte[] CompressedBytes = new byte[bufferSize];
|
||||
/// byte[] DecompressedBytes = new byte[bufferSize];
|
||||
///
|
||||
/// ZlibCodec compressor = new ZlibCodec();
|
||||
///
|
||||
/// compressor.InitializeDeflate(CompressionLevel.Default);
|
||||
///
|
||||
/// compressor.InputBuffer = System.Text.ASCIIEncoding.ASCII.GetBytes(TextToCompress);
|
||||
/// compressor.NextIn = 0;
|
||||
/// compressor.AvailableBytesIn = compressor.InputBuffer.Length;
|
||||
///
|
||||
/// compressor.OutputBuffer = CompressedBytes;
|
||||
/// compressor.NextOut = 0;
|
||||
/// compressor.AvailableBytesOut = CompressedBytes.Length;
|
||||
///
|
||||
/// while (compressor.TotalBytesIn != TextToCompress.Length && compressor.TotalBytesOut < bufferSize)
|
||||
/// {
|
||||
/// compressor.Deflate(FlushType.None);
|
||||
/// }
|
||||
///
|
||||
/// while (true)
|
||||
/// {
|
||||
/// int rc= compressor.Deflate(FlushType.Finish);
|
||||
/// if (rc == ZlibConstants.Z_STREAM_END) break;
|
||||
/// }
|
||||
///
|
||||
/// compressor.EndDeflate();
|
||||
///
|
||||
/// </code>
|
||||
/// </example>
|
||||
/// <returns>Z_OK if all goes well. You generally don't need to check the return code.</returns>
|
||||
public int InitializeDeflate()
|
||||
{
|
||||
return _InternalInitializeDeflate(true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initialize the ZlibCodec for deflation operation, using the specified CompressionLevel.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The codec will use the maximum window bits (15) and the specified
|
||||
/// CompressionLevel. It will emit a ZLIB stream as it compresses.
|
||||
/// </remarks>
|
||||
/// <param name="level">The compression level for the codec.</param>
|
||||
/// <returns>Z_OK if all goes well.</returns>
|
||||
public int InitializeDeflate(CompressionLevel level)
|
||||
{
|
||||
this.CompressLevel = level;
|
||||
return _InternalInitializeDeflate(true);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Initialize the ZlibCodec for deflation operation, using the specified CompressionLevel,
|
||||
/// and the explicit flag governing whether to emit an RFC1950 header byte pair.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The codec will use the maximum window bits (15) and the specified CompressionLevel.
|
||||
/// If you want to generate a zlib stream, you should specify true for
|
||||
/// wantRfc1950Header. In this case, the library will emit a ZLIB
|
||||
/// header, as defined in <see href="http://www.ietf.org/rfc/rfc1950.txt">RFC
|
||||
/// 1950</see>, in the compressed stream.
|
||||
/// </remarks>
|
||||
/// <param name="level">The compression level for the codec.</param>
|
||||
/// <param name="wantRfc1950Header">whether to emit an initial RFC1950 byte pair in the compressed stream.</param>
|
||||
/// <returns>Z_OK if all goes well.</returns>
|
||||
public int InitializeDeflate(CompressionLevel level, bool wantRfc1950Header)
|
||||
{
|
||||
this.CompressLevel = level;
|
||||
return _InternalInitializeDeflate(wantRfc1950Header);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Initialize the ZlibCodec for deflation operation, using the specified CompressionLevel,
|
||||
/// and the specified number of window bits.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The codec will use the specified number of window bits and the specified CompressionLevel.
|
||||
/// </remarks>
|
||||
/// <param name="level">The compression level for the codec.</param>
|
||||
/// <param name="bits">the number of window bits to use. If you don't know what this means, don't use this method.</param>
|
||||
/// <returns>Z_OK if all goes well.</returns>
|
||||
public int InitializeDeflate(CompressionLevel level, int bits)
|
||||
{
|
||||
this.CompressLevel = level;
|
||||
this.WindowBits = bits;
|
||||
return _InternalInitializeDeflate(true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initialize the ZlibCodec for deflation operation, using the specified
|
||||
/// CompressionLevel, the specified number of window bits, and the explicit flag
|
||||
/// governing whether to emit an RFC1950 header byte pair.
|
||||
/// </summary>
|
||||
///
|
||||
/// <param name="level">The compression level for the codec.</param>
|
||||
/// <param name="wantRfc1950Header">whether to emit an initial RFC1950 byte pair in the compressed stream.</param>
|
||||
/// <param name="bits">the number of window bits to use. If you don't know what this means, don't use this method.</param>
|
||||
/// <returns>Z_OK if all goes well.</returns>
|
||||
public int InitializeDeflate(CompressionLevel level, int bits, bool wantRfc1950Header)
|
||||
{
|
||||
this.CompressLevel = level;
|
||||
this.WindowBits = bits;
|
||||
return _InternalInitializeDeflate(wantRfc1950Header);
|
||||
}
|
||||
|
||||
private int _InternalInitializeDeflate(bool wantRfc1950Header)
|
||||
{
|
||||
if (istate != null) throw new ZlibException("You may not call InitializeDeflate() after calling InitializeInflate().");
|
||||
dstate = new DeflateManager();
|
||||
dstate.WantRfc1950HeaderBytes = wantRfc1950Header;
|
||||
|
||||
return dstate.Initialize(this, this.CompressLevel, this.WindowBits, this.Strategy);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deflate one batch of data.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// You must have set InputBuffer and OutputBuffer before calling this method.
|
||||
/// </remarks>
|
||||
/// <example>
|
||||
/// <code>
|
||||
/// private void DeflateBuffer(CompressionLevel level)
|
||||
/// {
|
||||
/// int bufferSize = 1024;
|
||||
/// byte[] buffer = new byte[bufferSize];
|
||||
/// ZlibCodec compressor = new ZlibCodec();
|
||||
///
|
||||
/// Console.WriteLine("\n============================================");
|
||||
/// Console.WriteLine("Size of Buffer to Deflate: {0} bytes.", UncompressedBytes.Length);
|
||||
/// MemoryStream ms = new MemoryStream();
|
||||
///
|
||||
/// int rc = compressor.InitializeDeflate(level);
|
||||
///
|
||||
/// compressor.InputBuffer = UncompressedBytes;
|
||||
/// compressor.NextIn = 0;
|
||||
/// compressor.AvailableBytesIn = UncompressedBytes.Length;
|
||||
///
|
||||
/// compressor.OutputBuffer = buffer;
|
||||
///
|
||||
/// // pass 1: deflate
|
||||
/// do
|
||||
/// {
|
||||
/// compressor.NextOut = 0;
|
||||
/// compressor.AvailableBytesOut = buffer.Length;
|
||||
/// rc = compressor.Deflate(FlushType.None);
|
||||
///
|
||||
/// if (rc != ZlibConstants.Z_OK && rc != ZlibConstants.Z_STREAM_END)
|
||||
/// throw new Exception("deflating: " + compressor.Message);
|
||||
///
|
||||
/// ms.Write(compressor.OutputBuffer, 0, buffer.Length - compressor.AvailableBytesOut);
|
||||
/// }
|
||||
/// while (compressor.AvailableBytesIn > 0 || compressor.AvailableBytesOut == 0);
|
||||
///
|
||||
/// // pass 2: finish and flush
|
||||
/// do
|
||||
/// {
|
||||
/// compressor.NextOut = 0;
|
||||
/// compressor.AvailableBytesOut = buffer.Length;
|
||||
/// rc = compressor.Deflate(FlushType.Finish);
|
||||
///
|
||||
/// if (rc != ZlibConstants.Z_STREAM_END && rc != ZlibConstants.Z_OK)
|
||||
/// throw new Exception("deflating: " + compressor.Message);
|
||||
///
|
||||
/// if (buffer.Length - compressor.AvailableBytesOut > 0)
|
||||
/// ms.Write(buffer, 0, buffer.Length - compressor.AvailableBytesOut);
|
||||
/// }
|
||||
/// while (compressor.AvailableBytesIn > 0 || compressor.AvailableBytesOut == 0);
|
||||
///
|
||||
/// compressor.EndDeflate();
|
||||
///
|
||||
/// ms.Seek(0, SeekOrigin.Begin);
|
||||
/// CompressedBytes = new byte[compressor.TotalBytesOut];
|
||||
/// ms.Read(CompressedBytes, 0, CompressedBytes.Length);
|
||||
/// }
|
||||
/// </code>
|
||||
/// </example>
|
||||
/// <param name="flush">whether to flush all data as you deflate. Generally you will want to
|
||||
/// use Z_NO_FLUSH here, in a series of calls to Deflate(), and then call EndDeflate() to
|
||||
/// flush everything.
|
||||
/// </param>
|
||||
/// <returns>Z_OK if all goes well.</returns>
|
||||
public int Deflate(FlushType flush)
|
||||
{
|
||||
if (dstate == null)
|
||||
throw new ZlibException("No Deflate State!");
|
||||
return dstate.Deflate(flush);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// End a deflation session.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Call this after making a series of one or more calls to Deflate(). All buffers are flushed.
|
||||
/// </remarks>
|
||||
/// <returns>Z_OK if all goes well.</returns>
|
||||
public int EndDeflate()
|
||||
{
|
||||
if (dstate == null)
|
||||
throw new ZlibException("No Deflate State!");
|
||||
// TODO: dinoch Tue, 03 Nov 2009 15:39 (test this)
|
||||
//int ret = dstate.End();
|
||||
dstate = null;
|
||||
return ZlibConstants.Z_OK; //ret;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reset a codec for another deflation session.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Call this to reset the deflation state. For example if a thread is deflating
|
||||
/// non-consecutive blocks, you can call Reset() after the Deflate(Sync) of the first
|
||||
/// block and before the next Deflate(None) of the second block.
|
||||
/// </remarks>
|
||||
/// <returns>Z_OK if all goes well.</returns>
|
||||
public void ResetDeflate()
|
||||
{
|
||||
if (dstate == null)
|
||||
throw new ZlibException("No Deflate State!");
|
||||
dstate.Reset();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Set the CompressionStrategy and CompressionLevel for a deflation session.
|
||||
/// </summary>
|
||||
/// <param name="level">the level of compression to use.</param>
|
||||
/// <param name="strategy">the strategy to use for compression.</param>
|
||||
/// <returns>Z_OK if all goes well.</returns>
|
||||
public int SetDeflateParams(CompressionLevel level, CompressionStrategy strategy)
|
||||
{
|
||||
if (dstate == null)
|
||||
throw new ZlibException("No Deflate State!");
|
||||
return dstate.SetParams(level, strategy);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Set the dictionary to be used for either Inflation or Deflation.
|
||||
/// </summary>
|
||||
/// <param name="dictionary">The dictionary bytes to use.</param>
|
||||
/// <returns>Z_OK if all goes well.</returns>
|
||||
public int SetDictionary(byte[] dictionary)
|
||||
{
|
||||
if (istate != null)
|
||||
return istate.SetDictionary(dictionary);
|
||||
|
||||
if (dstate != null)
|
||||
return dstate.SetDictionary(dictionary);
|
||||
|
||||
throw new ZlibException("No Inflate or Deflate state!");
|
||||
}
|
||||
|
||||
// Flush as much pending output as possible. All deflate() output goes
|
||||
// through this function so some applications may wish to modify it
|
||||
// to avoid allocating a large strm->next_out buffer and copying into it.
|
||||
// (See also read_buf()).
|
||||
internal void flush_pending()
|
||||
{
|
||||
int len = dstate.pendingCount;
|
||||
|
||||
if (len > AvailableBytesOut)
|
||||
len = AvailableBytesOut;
|
||||
if (len == 0)
|
||||
return;
|
||||
|
||||
if (dstate.pending.Length <= dstate.nextPending ||
|
||||
OutputBuffer.Length <= NextOut ||
|
||||
dstate.pending.Length < (dstate.nextPending + len) ||
|
||||
OutputBuffer.Length < (NextOut + len))
|
||||
{
|
||||
throw new ZlibException(String.Format("Invalid State. (pending.Length={0}, pendingCount={1})",
|
||||
dstate.pending.Length, dstate.pendingCount));
|
||||
}
|
||||
|
||||
Array.Copy(dstate.pending, dstate.nextPending, OutputBuffer, NextOut, len);
|
||||
|
||||
NextOut += len;
|
||||
dstate.nextPending += len;
|
||||
TotalBytesOut += len;
|
||||
AvailableBytesOut -= len;
|
||||
dstate.pendingCount -= len;
|
||||
if (dstate.pendingCount == 0)
|
||||
{
|
||||
dstate.nextPending = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Read a new buffer from the current input stream, update the adler32
|
||||
// and total number of bytes read. All deflate() input goes through
|
||||
// this function so some applications may wish to modify it to avoid
|
||||
// allocating a large strm->next_in buffer and copying from it.
|
||||
// (See also flush_pending()).
|
||||
internal int read_buf(byte[] buf, int start, int size)
|
||||
{
|
||||
int len = AvailableBytesIn;
|
||||
|
||||
if (len > size)
|
||||
len = size;
|
||||
if (len == 0)
|
||||
return 0;
|
||||
|
||||
AvailableBytesIn -= len;
|
||||
|
||||
if (dstate.WantRfc1950HeaderBytes)
|
||||
{
|
||||
_Adler32 = Adler.Adler32(_Adler32, InputBuffer, NextIn, len);
|
||||
}
|
||||
Array.Copy(InputBuffer, NextIn, buf, start, len);
|
||||
NextIn += len;
|
||||
TotalBytesIn += len;
|
||||
return len;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,126 +0,0 @@
|
||||
// ZlibConstants.cs
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (c) 2009 Dino Chiesa and Microsoft Corporation.
|
||||
// All rights reserved.
|
||||
//
|
||||
// This code module is part of DotNetZip, a zipfile class library.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This code is licensed under the Microsoft Public License.
|
||||
// See the file License.txt for the license details.
|
||||
// More info on: http://dotnetzip.codeplex.com
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// last saved (in emacs):
|
||||
// Time-stamp: <2009-November-03 18:50:19>
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This module defines constants used by the zlib class library. This
|
||||
// code is derived from the jzlib implementation of zlib, but
|
||||
// significantly modified. In keeping with the license for jzlib, the
|
||||
// copyright to that code is included here.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright notice,
|
||||
// this list of conditions and the following disclaimer.
|
||||
//
|
||||
// 2. Redistributions in binary form must reproduce the above copyright
|
||||
// notice, this list of conditions and the following disclaimer in
|
||||
// the documentation and/or other materials provided with the distribution.
|
||||
//
|
||||
// 3. The names of the authors may not be used to endorse or promote products
|
||||
// derived from this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
// INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
//
|
||||
// This program is based on zlib-1.1.3; credit to authors
|
||||
// Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
|
||||
// and contributors of zlib.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
|
||||
namespace Compress.ZipFile.ZLib
|
||||
{
|
||||
/// <summary>
|
||||
/// A bunch of constants used in the Zlib interface.
|
||||
/// </summary>
|
||||
public static class ZlibConstants
|
||||
{
|
||||
/// <summary>
|
||||
/// The maximum number of window bits for the Deflate algorithm.
|
||||
/// </summary>
|
||||
public const int WindowBitsMax = 15; // 32K LZ77 window
|
||||
|
||||
/// <summary>
|
||||
/// The default number of window bits for the Deflate algorithm.
|
||||
/// </summary>
|
||||
public const int WindowBitsDefault = WindowBitsMax;
|
||||
|
||||
/// <summary>
|
||||
/// indicates everything is A-OK
|
||||
/// </summary>
|
||||
public const int Z_OK = 0;
|
||||
|
||||
/// <summary>
|
||||
/// Indicates that the last operation reached the end of the stream.
|
||||
/// </summary>
|
||||
public const int Z_STREAM_END = 1;
|
||||
|
||||
/// <summary>
|
||||
/// The operation ended in need of a dictionary.
|
||||
/// </summary>
|
||||
public const int Z_NEED_DICT = 2;
|
||||
|
||||
/// <summary>
|
||||
/// There was an error with the stream - not enough data, not open and readable, etc.
|
||||
/// </summary>
|
||||
public const int Z_STREAM_ERROR = -2;
|
||||
|
||||
/// <summary>
|
||||
/// There was an error with the data - not enough data, bad data, etc.
|
||||
/// </summary>
|
||||
public const int Z_DATA_ERROR = -3;
|
||||
|
||||
/// <summary>
|
||||
/// There was an error with the working buffer.
|
||||
/// </summary>
|
||||
public const int Z_BUF_ERROR = -5;
|
||||
|
||||
/// <summary>
|
||||
/// The size of the working buffer used in the ZlibCodec class. Defaults to 8192 bytes.
|
||||
/// </summary>
|
||||
#if NETCF
|
||||
public const int WorkingBufferSizeDefault = 8192;
|
||||
#else
|
||||
public const int WorkingBufferSizeDefault = 16384;
|
||||
#endif
|
||||
/// <summary>
|
||||
/// The minimum size of the working buffer used in the ZlibCodec class. Currently it is 128 bytes.
|
||||
/// </summary>
|
||||
public const int WorkingBufferSizeMin = 1024;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -11,108 +11,69 @@ namespace Compress.ZipFile
|
||||
{
|
||||
public partial class Zip : ICompress
|
||||
{
|
||||
private readonly List<LocalFile> _localFiles = new List<LocalFile>();
|
||||
|
||||
private readonly List<ZipLocalFile> _localFiles = new();
|
||||
|
||||
private FileInfo _zipFileInfo;
|
||||
|
||||
|
||||
private byte[] _fileComment;
|
||||
private Stream _zipFs;
|
||||
|
||||
private uint _localFilesCount;
|
||||
|
||||
private ulong _centralDirStart;
|
||||
private ulong _centralDirSize;
|
||||
private ulong _endOfCentralDir64;
|
||||
|
||||
private bool _zip64;
|
||||
|
||||
public string ZipFilename => _zipFileInfo != null ? _zipFileInfo.FullName : "";
|
||||
|
||||
public long TimeStamp => _zipFileInfo?.LastWriteTime ?? 0;
|
||||
|
||||
public ZipOpenType ZipOpen { get; private set; }
|
||||
|
||||
|
||||
public ZipStatus ZipStatus { get; set; }
|
||||
public byte[] FileComment { get; private set; }
|
||||
|
||||
public Zip()
|
||||
{
|
||||
ZipUtils.EncodeSetup();
|
||||
CompressUtils.EncodeSetup();
|
||||
}
|
||||
|
||||
public ZipOpenType ZipOpen { get; private set; }
|
||||
|
||||
|
||||
public ZipStatus ZipStatus { get; private set; }
|
||||
|
||||
// If writeZipType == trrntzip then it will force a trrntzip file and error out if not.
|
||||
// If writeZipType == None it will still try and make a trrntzip if everything is supplied matching trrntzip parameters.
|
||||
private OutputZipType writeZipType = OutputZipType.None;
|
||||
|
||||
private ulong offset = 0;
|
||||
|
||||
public int LocalFilesCount()
|
||||
{
|
||||
return _localFiles.Count;
|
||||
}
|
||||
|
||||
public string Filename(int i)
|
||||
public LocalFile GetLocalFile(int i)
|
||||
{
|
||||
return _localFiles[i].FileName;
|
||||
return _localFiles[i];
|
||||
}
|
||||
|
||||
public ulong UncompressedSize(int i)
|
||||
{
|
||||
return _localFiles[i].UncompressedSize;
|
||||
}
|
||||
|
||||
public ulong? LocalHeader(int i)
|
||||
{
|
||||
return (_localFiles[i].GeneralPurposeBitFlag & 8) == 0 ? (ulong?)_localFiles[i].RelativeOffsetOfLocalHeader : null;
|
||||
}
|
||||
|
||||
public byte[] CRC32(int i)
|
||||
{
|
||||
return _localFiles[i].CRC;
|
||||
}
|
||||
|
||||
public bool IsDirectory(int i)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (_localFiles[i].UncompressedSize != 0)
|
||||
return false;
|
||||
string filename = _localFiles[i].FileName;
|
||||
char lastChar = filename[filename.Length - 1];
|
||||
return lastChar == '/' || lastChar == '\\';
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ArgumentException argEx = new ArgumentException("Error in file " + _zipFileInfo?.FullName + " : " + ex.Message, ex.InnerException);
|
||||
throw argEx;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public long LastModified(int i)
|
||||
{
|
||||
return _localFiles[i].DateTime;
|
||||
}
|
||||
|
||||
public long? Created(int i)
|
||||
{
|
||||
return _localFiles[i].DateTimeCreate;
|
||||
}
|
||||
|
||||
public long? Accessed(int i)
|
||||
{
|
||||
return _localFiles[i].DateTimeAccess;
|
||||
}
|
||||
|
||||
|
||||
public void ZipFileClose()
|
||||
{
|
||||
if (ZipOpen == ZipOpenType.Closed)
|
||||
switch (ZipOpen)
|
||||
{
|
||||
return;
|
||||
}
|
||||
case ZipOpenType.Closed:
|
||||
return;
|
||||
|
||||
if (ZipOpen == ZipOpenType.OpenRead)
|
||||
{
|
||||
zipFileCloseRead();
|
||||
return;
|
||||
}
|
||||
case ZipOpenType.OpenRead:
|
||||
zipFileCloseRead();
|
||||
return;
|
||||
|
||||
zipFileCloseWrite();
|
||||
default:
|
||||
zipFileCloseWrite();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] Filecomment => _fileComment;
|
||||
|
||||
/*
|
||||
public void BreakTrrntZip(string filename)
|
||||
|
||||
@@ -56,65 +56,65 @@ namespace Compress.ZipFile
|
||||
|
||||
private ZipReturn EndOfCentralDirRead()
|
||||
{
|
||||
using (BinaryReader zipBr = new BinaryReader(_zipFs, Encoding.UTF8, true))
|
||||
using BinaryReader zipBr = new(_zipFs, Encoding.UTF8, true);
|
||||
uint thisSignature = zipBr.ReadUInt32();
|
||||
if (thisSignature != EndOfCentralDirSignature)
|
||||
{
|
||||
uint thisSignature = zipBr.ReadUInt32();
|
||||
if (thisSignature != EndOfCentralDirSignature)
|
||||
{
|
||||
return ZipReturn.ZipEndOfCentralDirectoryError;
|
||||
}
|
||||
|
||||
ushort tUShort = zipBr.ReadUInt16(); // NumberOfThisDisk
|
||||
if (tUShort != 0)
|
||||
{
|
||||
return ZipReturn.ZipEndOfCentralDirectoryError;
|
||||
}
|
||||
|
||||
tUShort = zipBr.ReadUInt16(); // NumberOfThisDiskCenterDir
|
||||
if (tUShort != 0)
|
||||
{
|
||||
return ZipReturn.ZipEndOfCentralDirectoryError;
|
||||
}
|
||||
|
||||
_localFilesCount = zipBr.ReadUInt16(); // TotalNumberOfEntriesDisk
|
||||
|
||||
tUShort = zipBr.ReadUInt16(); // TotalNumber of entries in the central directory
|
||||
if (tUShort != _localFilesCount)
|
||||
{
|
||||
return ZipReturn.ZipEndOfCentralDirectoryError;
|
||||
}
|
||||
|
||||
_centralDirSize = zipBr.ReadUInt32(); // SizeOfCentralDir
|
||||
_centralDirStart = zipBr.ReadUInt32(); // Offset
|
||||
|
||||
ushort zipFileCommentLength = zipBr.ReadUInt16();
|
||||
|
||||
_fileComment = zipBr.ReadBytes(zipFileCommentLength);
|
||||
|
||||
if (_zipFs.Position != _zipFs.Length)
|
||||
{
|
||||
ZipStatus |= ZipStatus.ExtraData;
|
||||
}
|
||||
|
||||
return ZipReturn.ZipGood;
|
||||
return ZipReturn.ZipEndOfCentralDirectoryError;
|
||||
}
|
||||
|
||||
ushort tUShort = zipBr.ReadUInt16(); // NumberOfThisDisk
|
||||
if (tUShort != 0)
|
||||
{
|
||||
return ZipReturn.ZipEndOfCentralDirectoryError;
|
||||
}
|
||||
|
||||
tUShort = zipBr.ReadUInt16(); // NumberOfThisDiskCenterDir
|
||||
if (tUShort != 0)
|
||||
{
|
||||
return ZipReturn.ZipEndOfCentralDirectoryError;
|
||||
}
|
||||
|
||||
_localFilesCount = zipBr.ReadUInt16(); // TotalNumberOfEntriesDisk
|
||||
|
||||
tUShort = zipBr.ReadUInt16(); // TotalNumber of entries in the central directory
|
||||
if (tUShort != _localFilesCount)
|
||||
{
|
||||
return ZipReturn.ZipEndOfCentralDirectoryError;
|
||||
}
|
||||
|
||||
_centralDirSize = zipBr.ReadUInt32(); // SizeOfCentralDir
|
||||
_centralDirStart = zipBr.ReadUInt32(); // Offset
|
||||
|
||||
ushort zipFileCommentLength = zipBr.ReadUInt16();
|
||||
|
||||
FileComment = zipBr.ReadBytes(zipFileCommentLength);
|
||||
|
||||
if (_zipFs.Position != _zipFs.Length)
|
||||
{
|
||||
ZipStatus |= ZipStatus.ExtraData;
|
||||
}
|
||||
if (offset != 0)
|
||||
{
|
||||
ZipStatus |= ZipStatus.ExtraData;
|
||||
}
|
||||
|
||||
return ZipReturn.ZipGood;
|
||||
}
|
||||
|
||||
|
||||
private void EndOfCentralDirWrite()
|
||||
{
|
||||
using (BinaryWriter bw = new BinaryWriter(_zipFs, Encoding.UTF8, true))
|
||||
{
|
||||
bw.Write(EndOfCentralDirSignature);
|
||||
bw.Write((ushort)0); // NumberOfThisDisk
|
||||
bw.Write((ushort)0); // NumberOfThisDiskCenterDir
|
||||
bw.Write((ushort)(_localFiles.Count >= 0xffff ? 0xffff : _localFiles.Count)); // TotalNumberOfEntriesDisk
|
||||
bw.Write((ushort)(_localFiles.Count >= 0xffff ? 0xffff : _localFiles.Count)); // TotalNumber of entries in the central directory
|
||||
bw.Write((uint)(_centralDirSize >= 0xffffffff ? 0xffffffff : _centralDirSize));
|
||||
bw.Write((uint)(_centralDirStart >= 0xffffffff ? 0xffffffff : _centralDirStart));
|
||||
bw.Write((ushort)_fileComment.Length);
|
||||
bw.Write(_fileComment, 0, _fileComment.Length);
|
||||
}
|
||||
using BinaryWriter bw = new(_zipFs, Encoding.UTF8, true);
|
||||
bw.Write(EndOfCentralDirSignature);
|
||||
bw.Write((ushort)0); // NumberOfThisDisk
|
||||
bw.Write((ushort)0); // NumberOfThisDiskCenterDir
|
||||
bw.Write((ushort)(_localFiles.Count >= 0xffff ? 0xffff : _localFiles.Count)); // TotalNumberOfEntriesDisk
|
||||
bw.Write((ushort)(_localFiles.Count >= 0xffff ? 0xffff : _localFiles.Count)); // TotalNumber of entries in the central directory
|
||||
bw.Write((uint)(_centralDirSize >= 0xffffffff ? 0xffffffff : _centralDirSize));
|
||||
bw.Write((uint)(_centralDirStart >= 0xffffffff ? 0xffffffff : _centralDirStart));
|
||||
bw.Write((ushort)FileComment.Length);
|
||||
bw.Write(FileComment, 0, FileComment.Length);
|
||||
}
|
||||
|
||||
|
||||
@@ -122,116 +122,106 @@ namespace Compress.ZipFile
|
||||
|
||||
private ZipReturn Zip64EndOfCentralDirRead()
|
||||
{
|
||||
using (BinaryReader zipBr = new BinaryReader(_zipFs, Encoding.UTF8, true))
|
||||
using BinaryReader zipBr = new(_zipFs, Encoding.UTF8, true);
|
||||
uint thisSignature = zipBr.ReadUInt32();
|
||||
if (thisSignature != Zip64EndOfCentralDirSignature)
|
||||
{
|
||||
uint thisSignature = zipBr.ReadUInt32();
|
||||
if (thisSignature != Zip64EndOfCentralDirSignature)
|
||||
{
|
||||
return ZipReturn.ZipEndOfCentralDirectoryError;
|
||||
}
|
||||
|
||||
//_zip64 = true;
|
||||
ulong tULong = zipBr.ReadUInt64(); // Size of zip64 end of central directory record
|
||||
if (tULong != 44)
|
||||
{
|
||||
return ZipReturn.Zip64EndOfCentralDirError;
|
||||
}
|
||||
|
||||
zipBr.ReadUInt16(); // version made by
|
||||
|
||||
ushort tUShort = zipBr.ReadUInt16(); // version needed to extract
|
||||
if (tUShort != 45)
|
||||
{
|
||||
return ZipReturn.Zip64EndOfCentralDirError;
|
||||
}
|
||||
|
||||
uint tUInt = zipBr.ReadUInt32(); // number of this disk
|
||||
if (tUInt != 0)
|
||||
{
|
||||
return ZipReturn.Zip64EndOfCentralDirError;
|
||||
}
|
||||
|
||||
tUInt = zipBr.ReadUInt32(); // number of the disk with the start of the central directory
|
||||
if (tUInt != 0)
|
||||
{
|
||||
return ZipReturn.Zip64EndOfCentralDirError;
|
||||
}
|
||||
|
||||
_localFilesCount =
|
||||
(uint)zipBr.ReadUInt64(); // total number of entries in the central directory on this disk
|
||||
|
||||
tULong = zipBr.ReadUInt64(); // total number of entries in the central directory
|
||||
if (tULong != _localFilesCount)
|
||||
{
|
||||
return ZipReturn.Zip64EndOfCentralDirError;
|
||||
}
|
||||
|
||||
_zip64 = true;
|
||||
_centralDirSize = zipBr.ReadUInt64(); // size of central directory
|
||||
|
||||
_centralDirStart = zipBr.ReadUInt64(); // offset of start of central directory with respect to the starting disk number
|
||||
|
||||
return ZipReturn.ZipGood;
|
||||
return ZipReturn.ZipEndOfCentralDirectoryError;
|
||||
}
|
||||
|
||||
ulong tULong = zipBr.ReadUInt64(); // Size of zip64 end of central directory record
|
||||
if (tULong != 44)
|
||||
{
|
||||
return ZipReturn.Zip64EndOfCentralDirError;
|
||||
}
|
||||
|
||||
zipBr.ReadUInt16(); // version made by
|
||||
|
||||
ushort tUShort = zipBr.ReadUInt16(); // version needed to extract
|
||||
if (tUShort != 45)
|
||||
{
|
||||
return ZipReturn.Zip64EndOfCentralDirError;
|
||||
}
|
||||
|
||||
uint tUInt = zipBr.ReadUInt32(); // number of this disk
|
||||
if (tUInt != 0)
|
||||
{
|
||||
return ZipReturn.Zip64EndOfCentralDirError;
|
||||
}
|
||||
|
||||
tUInt = zipBr.ReadUInt32(); // number of the disk with the start of the central directory
|
||||
if (tUInt != 0)
|
||||
{
|
||||
return ZipReturn.Zip64EndOfCentralDirError;
|
||||
}
|
||||
|
||||
_localFilesCount = (uint)zipBr.ReadUInt64(); // total number of entries in the central directory on this disk
|
||||
|
||||
tULong = zipBr.ReadUInt64(); // total number of entries in the central directory
|
||||
if (tULong != _localFilesCount)
|
||||
{
|
||||
return ZipReturn.Zip64EndOfCentralDirError;
|
||||
}
|
||||
|
||||
_zip64 = true;
|
||||
_centralDirSize = zipBr.ReadUInt64(); // size of central directory
|
||||
|
||||
_centralDirStart = zipBr.ReadUInt64(); // offset of start of central directory with respect to the starting disk number
|
||||
|
||||
return ZipReturn.ZipGood;
|
||||
}
|
||||
|
||||
|
||||
private void Zip64EndOfCentralDirWrite()
|
||||
{
|
||||
using (BinaryWriter bw = new BinaryWriter(_zipFs, Encoding.UTF8, true))
|
||||
{
|
||||
bw.Write(Zip64EndOfCentralDirSignature);
|
||||
bw.Write((ulong)44); // Size of zip64 end of central directory record
|
||||
bw.Write((ushort)45); // version made by
|
||||
bw.Write((ushort)45); // version needed to extract
|
||||
bw.Write((uint)0); // number of this disk
|
||||
bw.Write((uint)0); // number of the disk with the start of the central directory
|
||||
bw.Write((ulong)_localFiles.Count); // total number of entries in the central directory on this disk
|
||||
bw.Write((ulong)_localFiles.Count); // total number of entries in the central directory
|
||||
bw.Write(_centralDirSize); // size of central directory
|
||||
bw.Write(_centralDirStart); // offset of start of central directory with respect to the starting disk number
|
||||
}
|
||||
using BinaryWriter bw = new(_zipFs, Encoding.UTF8, true);
|
||||
bw.Write(Zip64EndOfCentralDirSignature);
|
||||
bw.Write((ulong)44); // Size of zip64 end of central directory record
|
||||
bw.Write((ushort)45); // version made by
|
||||
bw.Write((ushort)45); // version needed to extract
|
||||
bw.Write((uint)0); // number of this disk
|
||||
bw.Write((uint)0); // number of the disk with the start of the central directory
|
||||
bw.Write((ulong)_localFiles.Count); // total number of entries in the central directory on this disk
|
||||
bw.Write((ulong)_localFiles.Count); // total number of entries in the central directory
|
||||
bw.Write(_centralDirSize); // size of central directory
|
||||
bw.Write(_centralDirStart); // offset of start of central directory with respect to the starting disk number
|
||||
}
|
||||
|
||||
|
||||
|
||||
private ZipReturn Zip64EndOfCentralDirectoryLocatorRead()
|
||||
{
|
||||
using (BinaryReader zipBr = new BinaryReader(_zipFs, Encoding.UTF8, true))
|
||||
using BinaryReader zipBr = new(_zipFs, Encoding.UTF8, true);
|
||||
uint thisSignature = zipBr.ReadUInt32();
|
||||
if (thisSignature != Zip64EndOfCentralDirectoryLocator)
|
||||
{
|
||||
uint thisSignature = zipBr.ReadUInt32();
|
||||
if (thisSignature != Zip64EndOfCentralDirectoryLocator)
|
||||
{
|
||||
return ZipReturn.ZipEndOfCentralDirectoryError;
|
||||
}
|
||||
|
||||
uint tUInt = zipBr.ReadUInt32(); // number of the disk with the start of the zip64 end of central directory
|
||||
if (tUInt != 0)
|
||||
{
|
||||
return ZipReturn.Zip64EndOfCentralDirectoryLocatorError;
|
||||
}
|
||||
|
||||
_endOfCenterDir64 = zipBr.ReadUInt64(); // relative offset of the zip64 end of central directory record
|
||||
|
||||
tUInt = zipBr.ReadUInt32(); // total number of disks
|
||||
if (tUInt != 1)
|
||||
{
|
||||
return ZipReturn.Zip64EndOfCentralDirectoryLocatorError;
|
||||
}
|
||||
|
||||
return ZipReturn.ZipGood;
|
||||
return ZipReturn.ZipEndOfCentralDirectoryError;
|
||||
}
|
||||
|
||||
uint tUInt = zipBr.ReadUInt32(); // number of the disk with the start of the zip64 end of central directory
|
||||
if (tUInt != 0)
|
||||
{
|
||||
return ZipReturn.Zip64EndOfCentralDirectoryLocatorError;
|
||||
}
|
||||
|
||||
_endOfCentralDir64 = zipBr.ReadUInt64(); // relative offset of the zip64 end of central directory record
|
||||
|
||||
tUInt = zipBr.ReadUInt32(); // total number of disks
|
||||
if (tUInt > 1)
|
||||
{
|
||||
return ZipReturn.Zip64EndOfCentralDirectoryLocatorError;
|
||||
}
|
||||
|
||||
return ZipReturn.ZipGood;
|
||||
}
|
||||
|
||||
private void Zip64EndOfCentralDirectoryLocatorWrite()
|
||||
{
|
||||
using (BinaryWriter bw = new BinaryWriter(_zipFs, Encoding.UTF8, true))
|
||||
{
|
||||
bw.Write(Zip64EndOfCentralDirectoryLocator);
|
||||
bw.Write((uint)0); // number of the disk with the start of the zip64 end of central directory
|
||||
bw.Write(_endOfCenterDir64); // relative offset of the zip64 end of central directory record
|
||||
bw.Write((uint)1); // total number of disks
|
||||
}
|
||||
using BinaryWriter bw = new(_zipFs, Encoding.UTF8, true);
|
||||
bw.Write(Zip64EndOfCentralDirectoryLocator);
|
||||
bw.Write((uint)0); // number of the disk with the start of the zip64 end of central directory
|
||||
bw.Write(_endOfCentralDir64); // relative offset of the zip64 end of central directory record
|
||||
bw.Write((uint)1); // total number of disks
|
||||
}
|
||||
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
132
SabreTools.FileTypes/Compress/ZipFile/ZipExtraFieldWrite.cs
Normal file
132
SabreTools.FileTypes/Compress/ZipFile/ZipExtraFieldWrite.cs
Normal file
@@ -0,0 +1,132 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Compress.ZipFile
|
||||
{
|
||||
internal class ZipExtraFieldWrite
|
||||
{
|
||||
private readonly List<byte> _extraField;
|
||||
|
||||
public ZipExtraFieldWrite()
|
||||
{
|
||||
_extraField = new List<byte>();
|
||||
}
|
||||
|
||||
public byte[] ExtraField => _extraField.ToArray();
|
||||
|
||||
public bool Zip64(ulong unCompressedSize, ulong compressedSize, ulong relativeOffsetOfLocalHeader, bool centralDir,
|
||||
out uint headerUnCompressedSize, out uint headerCompressedSize, out uint headerRelativeOffsetOfLocalHeader)
|
||||
{
|
||||
List<byte> eZip64 = new();
|
||||
|
||||
if (!centralDir)
|
||||
{
|
||||
if (unCompressedSize >= 0xffffffff || compressedSize >= 0xffffffff)
|
||||
{
|
||||
eZip64.AddRange(BitConverter.GetBytes(unCompressedSize));
|
||||
eZip64.AddRange(BitConverter.GetBytes(compressedSize));
|
||||
headerUnCompressedSize = 0xffffffff;
|
||||
headerCompressedSize = 0xffffffff;
|
||||
}
|
||||
else
|
||||
{
|
||||
headerUnCompressedSize = (uint)unCompressedSize;
|
||||
headerCompressedSize = (uint)compressedSize;
|
||||
}
|
||||
|
||||
headerRelativeOffsetOfLocalHeader = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (unCompressedSize >= 0xffffffff)
|
||||
{
|
||||
headerUnCompressedSize = 0xffffffff;
|
||||
eZip64.AddRange(BitConverter.GetBytes(unCompressedSize));
|
||||
}
|
||||
else
|
||||
{
|
||||
headerUnCompressedSize = (uint)unCompressedSize;
|
||||
}
|
||||
if (compressedSize >= 0xffffffff)
|
||||
{
|
||||
headerCompressedSize = 0xffffffff;
|
||||
eZip64.AddRange(BitConverter.GetBytes(compressedSize));
|
||||
}
|
||||
else
|
||||
{
|
||||
headerCompressedSize = (uint)compressedSize;
|
||||
}
|
||||
|
||||
if (relativeOffsetOfLocalHeader >= 0xffffffff)
|
||||
{
|
||||
headerRelativeOffsetOfLocalHeader = 0xffffffff;
|
||||
eZip64.AddRange(BitConverter.GetBytes(relativeOffsetOfLocalHeader));
|
||||
}
|
||||
else
|
||||
{
|
||||
headerRelativeOffsetOfLocalHeader = (uint)relativeOffsetOfLocalHeader;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (eZip64.Count == 0)
|
||||
return false;
|
||||
|
||||
_extraField.AddRange(BitConverter.GetBytes((ushort)0x0001));
|
||||
_extraField.AddRange(BitConverter.GetBytes((ushort)eZip64.Count));
|
||||
_extraField.AddRange(eZip64);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
public void NtfsTime(long mTime, long aTime, long cTime)
|
||||
{
|
||||
_extraField.AddRange(BitConverter.GetBytes((ushort)0x000a));
|
||||
_extraField.AddRange(BitConverter.GetBytes((ushort)32)); // this block is 32 bytes long
|
||||
_extraField.AddRange(BitConverter.GetBytes((uint)0)); // Reserved
|
||||
_extraField.AddRange(BitConverter.GetBytes((ushort)0x0001)); // tag1 = 1
|
||||
_extraField.AddRange(BitConverter.GetBytes((ushort)24)); // size1 block size of date/times
|
||||
|
||||
_extraField.AddRange(BitConverter.GetBytes(CompressUtils.UtcTicksToNtfsDateTime(mTime)));
|
||||
_extraField.AddRange(BitConverter.GetBytes(CompressUtils.UtcTicksToNtfsDateTime(aTime)));
|
||||
_extraField.AddRange(BitConverter.GetBytes(CompressUtils.UtcTicksToNtfsDateTime(cTime)));
|
||||
}
|
||||
|
||||
|
||||
public void LinuxTime(long? mTime, long? aTime, long? cTime, bool centralDir)
|
||||
{
|
||||
List<byte> eTime = new();
|
||||
byte flags = 0;
|
||||
if (mTime != null)
|
||||
{
|
||||
flags |= 0x01;
|
||||
eTime.AddRange(BitConverter.GetBytes(CompressUtils.UtcTicksToUnixDateTime((long)mTime)));
|
||||
}
|
||||
|
||||
if (!centralDir)
|
||||
{
|
||||
if (aTime != null)
|
||||
{
|
||||
flags |= 0x02;
|
||||
eTime.AddRange(BitConverter.GetBytes(CompressUtils.UtcTicksToUnixDateTime((long)aTime)));
|
||||
}
|
||||
if (cTime != null)
|
||||
{
|
||||
flags |= 0x04;
|
||||
eTime.AddRange(BitConverter.GetBytes(CompressUtils.UtcTicksToUnixDateTime((long)cTime)));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (flags == 0)
|
||||
return;
|
||||
|
||||
_extraField.AddRange(BitConverter.GetBytes((ushort)0x5455));
|
||||
_extraField.AddRange(BitConverter.GetBytes((ushort)eTime.Count + 1));
|
||||
|
||||
_extraField.Add(flags);
|
||||
_extraField.AddRange(eTime);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
using System.IO;
|
||||
using Compress.Utils;
|
||||
using Compress.Support.Utils;
|
||||
|
||||
namespace Compress.ZipFile
|
||||
{
|
||||
@@ -25,19 +25,19 @@ namespace Compress.ZipFile
|
||||
return;
|
||||
}
|
||||
|
||||
_zip64 = false;
|
||||
bool lTrrntzip = true;
|
||||
|
||||
_zipFs = new MemoryStream();
|
||||
|
||||
_centralDirStart = fileOffset;
|
||||
|
||||
CrcCalculatorStream crcCs = new CrcCalculatorStream(_zipFs, true);
|
||||
|
||||
foreach (LocalFile t in _localFiles)
|
||||
CrcCalculatorStream crcCs = new(_zipFs, true);
|
||||
|
||||
foreach (ZipLocalFile t in _localFiles)
|
||||
{
|
||||
t.CenteralDirectoryWrite(crcCs);
|
||||
lTrrntzip &= t.TrrntZip;
|
||||
t.CentralDirectoryWrite(crcCs);
|
||||
lTrrntzip &= t.GetStatus(LocalFileStatus.TrrntZip);
|
||||
}
|
||||
|
||||
crcCs.Flush();
|
||||
@@ -45,7 +45,7 @@ namespace Compress.ZipFile
|
||||
|
||||
_centralDirSize = (ulong)_zipFs.Position;
|
||||
|
||||
_fileComment = lTrrntzip ? ZipUtils.GetBytes("TORRENTZIPPED-" + crcCs.Crc.ToString("X8")) : new byte[0];
|
||||
FileComment = lTrrntzip ? CompressUtils.GetBytes("TORRENTZIPPED-" + crcCs.Crc.ToString("X8")) : new byte[0];
|
||||
ZipStatus = lTrrntzip ? ZipStatus.TrrntZip : ZipStatus.None;
|
||||
|
||||
crcCs.Dispose();
|
||||
@@ -54,7 +54,7 @@ namespace Compress.ZipFile
|
||||
|
||||
if (_zip64)
|
||||
{
|
||||
_endOfCenterDir64 = fileOffset + (ulong)_zipFs.Position;
|
||||
_endOfCentralDir64 = fileOffset + (ulong)_zipFs.Position;
|
||||
Zip64EndOfCentralDirWrite();
|
||||
Zip64EndOfCentralDirectoryLocatorWrite();
|
||||
}
|
||||
@@ -76,10 +76,10 @@ namespace Compress.ZipFile
|
||||
return ZipReturn.ZipWritingToInputFile;
|
||||
}
|
||||
|
||||
LocalFile lf = new LocalFile(filename);
|
||||
ZipLocalFile lf = new(filename);
|
||||
_localFiles.Add(lf);
|
||||
|
||||
MemoryStream ms = new MemoryStream();
|
||||
MemoryStream ms = new();
|
||||
lf.LocalFileHeaderFake(fileOffset, uncompressedSize, compressedSize, crc32, ms);
|
||||
|
||||
localHeader = ms.ToArray();
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,6 +1,6 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using Compress.Utils;
|
||||
using Compress.Support.Utils;
|
||||
using FileInfo = RVIO.FileInfo;
|
||||
using FileStream = RVIO.FileStream;
|
||||
|
||||
@@ -35,7 +35,7 @@ namespace Compress.ZipFile
|
||||
if (errorCode != 0)
|
||||
{
|
||||
ZipFileClose();
|
||||
if (errorCode == 32)
|
||||
if (errorCode == 32 || errorCode==5)
|
||||
{
|
||||
return ZipReturn.ZipFileLocked;
|
||||
}
|
||||
@@ -109,7 +109,6 @@ namespace Compress.ZipFile
|
||||
return zRet;
|
||||
}
|
||||
|
||||
|
||||
// check if ZIP64 header is required
|
||||
bool zip64Required = (_centralDirStart == 0xffffffff || _centralDirSize == 0xffffffff || _localFilesCount == 0xffff);
|
||||
|
||||
@@ -118,12 +117,12 @@ namespace Compress.ZipFile
|
||||
zRet = Zip64EndOfCentralDirectoryLocatorRead();
|
||||
if (zRet == ZipReturn.ZipGood)
|
||||
{
|
||||
_zipFs.Position = (long)_endOfCenterDir64;
|
||||
_zipFs.Position = (long)_endOfCentralDir64;
|
||||
zRet = Zip64EndOfCentralDirRead();
|
||||
if (zRet == ZipReturn.ZipGood)
|
||||
{
|
||||
_zip64 = true;
|
||||
endOfCentralDir = _endOfCenterDir64;
|
||||
endOfCentralDir = _endOfCentralDir64;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -132,14 +131,18 @@ namespace Compress.ZipFile
|
||||
return ZipReturn.Zip64EndOfCentralDirError;
|
||||
}
|
||||
|
||||
offset = (endOfCentralDir - _centralDirSize) - _centralDirStart;
|
||||
|
||||
_centralDirStart += offset;
|
||||
|
||||
bool trrntzip = false;
|
||||
|
||||
// check if the ZIP has a valid TorrentZip file comment
|
||||
if (_fileComment.Length == 22)
|
||||
if (FileComment.Length == 22)
|
||||
{
|
||||
if (ZipUtils.GetString(_fileComment).Substring(0, 14) == "TORRENTZIPPED-")
|
||||
if (CompressUtils.GetString(FileComment).Substring(0, 14) == "TORRENTZIPPED-")
|
||||
{
|
||||
CrcCalculatorStream crcCs = new CrcCalculatorStream(_zipFs, true);
|
||||
CrcCalculatorStream crcCs = new(_zipFs, true);
|
||||
byte[] buffer = new byte[_centralDirSize];
|
||||
_zipFs.Position = (long)_centralDirStart;
|
||||
crcCs.Read(buffer, 0, (int)_centralDirSize);
|
||||
@@ -149,7 +152,7 @@ namespace Compress.ZipFile
|
||||
uint r = (uint)crcCs.Crc;
|
||||
crcCs.Dispose();
|
||||
|
||||
string tcrc = ZipUtils.GetString(_fileComment).Substring(14, 8);
|
||||
string tcrc = CompressUtils.GetString(FileComment).Substring(14, 8);
|
||||
string zcrc = r.ToString("X8");
|
||||
if (string.Compare(tcrc, zcrc, StringComparison.Ordinal) == 0)
|
||||
{
|
||||
@@ -157,7 +160,7 @@ namespace Compress.ZipFile
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (zip64Required != _zip64)
|
||||
trrntzip = false;
|
||||
|
||||
@@ -168,14 +171,14 @@ namespace Compress.ZipFile
|
||||
_localFiles.Capacity = (int)_localFilesCount;
|
||||
for (int i = 0; i < _localFilesCount; i++)
|
||||
{
|
||||
LocalFile lc = new LocalFile();
|
||||
zRet = lc.CenteralDirectoryRead(_zipFs);
|
||||
ZipLocalFile lc = new();
|
||||
zRet = lc.CentralDirectoryRead(_zipFs, offset);
|
||||
if (zRet != ZipReturn.ZipGood)
|
||||
{
|
||||
ZipFileClose();
|
||||
return zRet;
|
||||
}
|
||||
_zip64 |= lc.Zip64;
|
||||
_zip64 |= lc.GetStatus(LocalFileStatus.Zip64);
|
||||
_localFiles.Add(lc);
|
||||
}
|
||||
|
||||
@@ -187,7 +190,7 @@ namespace Compress.ZipFile
|
||||
ZipFileClose();
|
||||
return zRet;
|
||||
}
|
||||
trrntzip &= _localFiles[i].TrrntZip;
|
||||
trrntzip &= _localFiles[i].GetStatus(LocalFileStatus.TrrntZip);
|
||||
}
|
||||
|
||||
// check trrntzip file order
|
||||
@@ -195,7 +198,7 @@ namespace Compress.ZipFile
|
||||
{
|
||||
for (int i = 0; i < _localFilesCount - 1; i++)
|
||||
{
|
||||
if (ZipUtils.TrrntZipStringCompare(_localFiles[i].FileName, _localFiles[i + 1].FileName) < 0)
|
||||
if (CompressUtils.TrrntZipStringCompare(_localFiles[i].Filename, _localFiles[i + 1].Filename) < 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
@@ -210,19 +213,19 @@ namespace Compress.ZipFile
|
||||
for (int i = 0; i < _localFilesCount - 1; i++)
|
||||
{
|
||||
// see if we found a directory
|
||||
string filename0 = _localFiles[i].FileName;
|
||||
string filename0 = _localFiles[i].Filename;
|
||||
if (filename0.Substring(filename0.Length - 1, 1) != "/")
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// see if the next file is in that directory
|
||||
string filename1 = _localFiles[i + 1].FileName;
|
||||
string filename1 = _localFiles[i + 1].Filename;
|
||||
if (filename1.Length <= filename0.Length)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (ZipUtils.TrrntZipStringCompare(filename0, filename1.Substring(0, filename0.Length)) != 0)
|
||||
if (CompressUtils.TrrntZipStringCompare(filename0, filename1.Substring(0, filename0.Length)) != 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using System.IO;
|
||||
using Compress.ZipFile.ZLib;
|
||||
|
||||
namespace Compress.ZipFile
|
||||
{
|
||||
@@ -38,7 +37,7 @@ namespace Compress.ZipFile
|
||||
{
|
||||
ZipFileCloseReadStream();
|
||||
|
||||
LocalFile tmpFile = new LocalFile { LocalFilePos = pos };
|
||||
ZipLocalFile tmpFile = new ZipLocalFile { RelativeOffsetOfLocalHeader = pos };
|
||||
_localFiles.Clear();
|
||||
_localFiles.Add(tmpFile);
|
||||
ZipReturn zRet = tmpFile.LocalFileHeaderReadQuick(_zipFs);
|
||||
@@ -60,16 +59,13 @@ namespace Compress.ZipFile
|
||||
{
|
||||
if (_compressionStream == null)
|
||||
return ZipReturn.ZipGood;
|
||||
if (_compressionStream is ZlibBaseStream dfStream)
|
||||
|
||||
if (_compressionStream != _zipFs)
|
||||
{
|
||||
dfStream.Close();
|
||||
dfStream.Dispose();
|
||||
}
|
||||
else if (_compressionStream is System.IO.Compression.DeflateStream dfIOStream)
|
||||
{
|
||||
dfIOStream.Close();
|
||||
dfIOStream.Dispose();
|
||||
_compressionStream.Close();
|
||||
_compressionStream.Dispose();
|
||||
}
|
||||
|
||||
_compressionStream = null;
|
||||
|
||||
return ZipReturn.ZipGood;
|
||||
|
||||
@@ -1,250 +0,0 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
|
||||
namespace Compress.ZipFile
|
||||
{
|
||||
public static class ZipUtils
|
||||
{
|
||||
// according to the zip documents, zip filesname are stored as MS-DOS Code Page 437.
|
||||
// (Unless the uncode flag is set, in which case they are stored as UTF-8.
|
||||
private static Encoding enc = null;
|
||||
|
||||
public static void EncodeSetup()
|
||||
{
|
||||
if (enc != null)
|
||||
return;
|
||||
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
|
||||
enc = Encoding.GetEncoding(437);
|
||||
}
|
||||
|
||||
public static string GetString(byte[] byteArr)
|
||||
{
|
||||
return enc.GetString(byteArr);
|
||||
}
|
||||
|
||||
// to test if a filename can be stored as codepage 437 we take the filename string
|
||||
// convert it to bytes using the 437 code page, and then convert it back to a string
|
||||
// and we see if we lost characters as a result of the conversion there and back.
|
||||
internal static bool IsCodePage437(string s)
|
||||
{
|
||||
byte[] bOut = enc.GetBytes(s);
|
||||
string sOut = enc.GetString(bOut);
|
||||
|
||||
return CompareString(s, sOut);
|
||||
}
|
||||
|
||||
internal static byte[] GetBytes(string s)
|
||||
{
|
||||
return enc.GetBytes(s);
|
||||
}
|
||||
|
||||
internal static bool CompareString(string s1, string s2)
|
||||
{
|
||||
char[] c1 = s1.ToCharArray();
|
||||
char[] c2 = s2.ToCharArray();
|
||||
|
||||
if (c1.Length != c2.Length)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 0; i < c1.Length; i++)
|
||||
{
|
||||
if (c1[i] != c2[i])
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
internal static bool CompareStringSlash(string s1, string s2)
|
||||
{
|
||||
char[] c1 = s1.ToCharArray();
|
||||
char[] c2 = s2.ToCharArray();
|
||||
|
||||
if (c1.Length != c2.Length)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 0; i < c1.Length; i++)
|
||||
{
|
||||
if (c1[i] == '/') c1[i] = '\\';
|
||||
if (c2[i] == '/') c2[i] = '\\';
|
||||
if (c1[i] != c2[i])
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
internal static bool ByteArrCompare(byte[] b0, byte[] b1)
|
||||
{
|
||||
if ((b0 == null) || (b1 == null))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (b0.Length != b1.Length)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 0; i < b0.Length; i++)
|
||||
{
|
||||
if (b0[i] != b1[i])
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
internal static int TrrntZipStringCompare(string string1, string string2)
|
||||
{
|
||||
char[] bytes1 = string1.ToCharArray();
|
||||
char[] bytes2 = string2.ToCharArray();
|
||||
|
||||
int pos1 = 0;
|
||||
int pos2 = 0;
|
||||
|
||||
for (; ; )
|
||||
{
|
||||
if (pos1 == bytes1.Length)
|
||||
{
|
||||
return pos2 == bytes2.Length ? 0 : -1;
|
||||
}
|
||||
if (pos2 == bytes2.Length)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
int byte1 = bytes1[pos1++];
|
||||
int byte2 = bytes2[pos2++];
|
||||
|
||||
if (byte1 >= 65 && byte1 <= 90)
|
||||
{
|
||||
byte1 += 0x20;
|
||||
}
|
||||
if (byte2 >= 65 && byte2 <= 90)
|
||||
{
|
||||
byte2 += 0x20;
|
||||
}
|
||||
|
||||
if (byte1 < byte2)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
if (byte1 > byte2)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static string ZipErrorMessageText(ZipReturn zS)
|
||||
{
|
||||
string ret = "Unknown";
|
||||
switch (zS)
|
||||
{
|
||||
case ZipReturn.ZipGood:
|
||||
ret = "";
|
||||
break;
|
||||
case ZipReturn.ZipFileCountError:
|
||||
ret = "The number of file in the Zip does not mach the number of files in the Zips Centeral Directory";
|
||||
break;
|
||||
case ZipReturn.ZipSignatureError:
|
||||
ret = "An unknown Signature Block was found in the Zip";
|
||||
break;
|
||||
case ZipReturn.ZipExtraDataOnEndOfZip:
|
||||
ret = "Extra Data was found on the end of the Zip";
|
||||
break;
|
||||
case ZipReturn.ZipUnsupportedCompression:
|
||||
ret = "An unsupported Compression method was found in the Zip, if you recompress this zip it will be usable";
|
||||
break;
|
||||
case ZipReturn.ZipLocalFileHeaderError:
|
||||
ret = "Error reading a zipped file header information";
|
||||
break;
|
||||
case ZipReturn.ZipCentralDirError:
|
||||
ret = "There is an error in the Zip Centeral Directory";
|
||||
break;
|
||||
case ZipReturn.ZipReadingFromOutputFile:
|
||||
ret = "Trying to write to a Zip file open for output only";
|
||||
break;
|
||||
case ZipReturn.ZipWritingToInputFile:
|
||||
ret = "Tring to read from a Zip file open for input only";
|
||||
break;
|
||||
case ZipReturn.ZipErrorGettingDataStream:
|
||||
ret = "Error creating Data Stream";
|
||||
break;
|
||||
case ZipReturn.ZipCRCDecodeError:
|
||||
ret = "CRC error";
|
||||
break;
|
||||
case ZipReturn.ZipDecodeError:
|
||||
ret = "Error unzipping a file";
|
||||
break;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
public const long fileTimeToUTCTime = 504911232000000000;
|
||||
public const long epochTimeToUTCTime = 621355968000000000;
|
||||
public const long trrntzipDateTime = 629870671200000000;
|
||||
|
||||
private const long TicksPerMillisecond = 10000;
|
||||
private const long TicksPerSecond = TicksPerMillisecond * 1000;
|
||||
|
||||
public static void SetDateTime(long ticks, out ushort DosFileDate, out ushort DosFileTime)
|
||||
{
|
||||
DateTime DateTime = new DateTime(ticks, DateTimeKind.Unspecified);
|
||||
DosFileDate = (ushort)((DateTime.Day & 0x1f) | ((DateTime.Month & 0x0f) << 5) | (((DateTime.Year - 1980) & 0x7f) << 9));
|
||||
DosFileTime = (ushort)(((DateTime.Second >> 1) & 0x1f) | ((DateTime.Minute & 0x3f) << 5) | ((DateTime.Hour & 0x1f) << 11));
|
||||
}
|
||||
|
||||
public static long SetDateTime(ushort DosFileDate, ushort DosFileTime)
|
||||
{
|
||||
if (DosFileDate == 0)
|
||||
return 0;
|
||||
|
||||
int second = (DosFileTime & 0x1f) << 1;
|
||||
int minute = (DosFileTime >> 5) & 0x3f;
|
||||
int hour = (DosFileTime >> 11) & 0x1f;
|
||||
|
||||
int day = DosFileDate & 0x1f;
|
||||
int month = (DosFileDate >> 5) & 0x0f;
|
||||
int year = ((DosFileDate >> 9) & 0x7f) + 1980;
|
||||
|
||||
// valid hours 0 to 23
|
||||
// valid minutes 0 to 59
|
||||
// valid seconds 0 to 59
|
||||
// valid month 1 to 12
|
||||
// valid day 1 to 31
|
||||
|
||||
if (hour > 23 || minute > 59 || second > 59 || month < 1 || month > 12 || day < 1 || day > 31)
|
||||
return 0;
|
||||
|
||||
try
|
||||
{
|
||||
return new DateTime(year, month, day, hour, minute, second, DateTimeKind.Unspecified).Ticks;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public static long FileTimeToUTCTime(long ticks)
|
||||
{
|
||||
return ticks + fileTimeToUTCTime;
|
||||
}
|
||||
|
||||
public static long SetDateTimeFromUnixSeconds(int seconds)
|
||||
{
|
||||
return seconds * TicksPerSecond + epochTimeToUTCTime;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,27 +1,32 @@
|
||||
using Compress.Utils;
|
||||
using Compress.Support.Utils;
|
||||
using FileInfo = RVIO.FileInfo;
|
||||
using FileStream = RVIO.FileStream;
|
||||
|
||||
// UInt16 = ushort
|
||||
// UInt32 = uint
|
||||
// ULong = ulong
|
||||
|
||||
namespace Compress.ZipFile
|
||||
{
|
||||
|
||||
public partial class Zip
|
||||
{
|
||||
private ulong _centralDirStart;
|
||||
private ulong _centralDirSize;
|
||||
private ulong _endOfCenterDir64;
|
||||
|
||||
public ZipReturn ZipFileCreate(string newFilename)
|
||||
{
|
||||
return ZipFileCreate(newFilename, OutputZipType.None);
|
||||
}
|
||||
|
||||
// OutType of Trrntzip forces that we must have a trrtnzip file made
|
||||
// OutType of None will still make a trrntzip file if everything was supplied in trrntzip format.
|
||||
|
||||
public ZipReturn ZipFileCreate(string newFilename, OutputZipType outType)
|
||||
{
|
||||
if (ZipOpen != ZipOpenType.Closed)
|
||||
{
|
||||
return ZipReturn.ZipFileAlreadyOpen;
|
||||
}
|
||||
|
||||
DirUtil.CreateDirForFile(newFilename);
|
||||
writeZipType = outType;
|
||||
|
||||
CompressUtils.CreateDirForFile(newFilename);
|
||||
_zipFileInfo = new FileInfo(newFilename);
|
||||
|
||||
int errorCode = FileStream.OpenFileWrite(newFilename, out _zipFs);
|
||||
@@ -42,10 +47,10 @@ namespace Compress.ZipFile
|
||||
|
||||
using (CrcCalculatorStream crcCs = new CrcCalculatorStream(_zipFs, true))
|
||||
{
|
||||
foreach (LocalFile t in _localFiles)
|
||||
foreach (ZipLocalFile t in _localFiles)
|
||||
{
|
||||
t.CenteralDirectoryWrite(crcCs);
|
||||
lTrrntzip &= t.TrrntZip;
|
||||
t.CentralDirectoryWrite(crcCs);
|
||||
lTrrntzip &= t.GetStatus(LocalFileStatus.TrrntZip);
|
||||
}
|
||||
|
||||
crcCs.Flush();
|
||||
@@ -53,7 +58,7 @@ namespace Compress.ZipFile
|
||||
|
||||
_centralDirSize = (ulong)_zipFs.Position - _centralDirStart;
|
||||
|
||||
_fileComment = lTrrntzip ? ZipUtils.GetBytes("TORRENTZIPPED-" + crcCs.Crc.ToString("X8")) : new byte[0];
|
||||
FileComment = lTrrntzip ? CompressUtils.GetBytes("TORRENTZIPPED-" + crcCs.Crc.ToString("X8")) : new byte[0];
|
||||
ZipStatus = lTrrntzip ? ZipStatus.TrrntZip : ZipStatus.None;
|
||||
}
|
||||
|
||||
@@ -64,7 +69,7 @@ namespace Compress.ZipFile
|
||||
|
||||
if (_zip64)
|
||||
{
|
||||
_endOfCenterDir64 = (ulong)_zipFs.Position;
|
||||
_endOfCentralDir64 = (ulong)_zipFs.Position;
|
||||
Zip64EndOfCentralDirWrite();
|
||||
Zip64EndOfCentralDirectoryLocatorWrite();
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
using System.IO;
|
||||
using Compress.Utils;
|
||||
using Compress.ZipFile.ZLib;
|
||||
using Compress.Support.Compression.Deflate;
|
||||
using Compress.Support.Utils;
|
||||
|
||||
// UInt16 = ushort
|
||||
// UInt32 = uint
|
||||
@@ -12,6 +12,14 @@ namespace Compress.ZipFile
|
||||
{
|
||||
private Stream _compressionStream;
|
||||
|
||||
/*
|
||||
raw is true if we are just going to copy the raw data stream from the source to the destination zip file
|
||||
trrntzip is thue if the source zip is a valid trrntzip file
|
||||
compressionMethod must be set to 8 to make a valid trrntzip file.
|
||||
|
||||
if raw is false then compressionMthod must be 0,8 or 93 (zstd)
|
||||
*/
|
||||
|
||||
public ZipReturn ZipFileOpenWriteStream(bool raw, bool trrntzip, string filename, ulong uncompressedSize, ushort compressionMethod, out Stream stream, TimeStamps timeStamp = null)
|
||||
{
|
||||
stream = null;
|
||||
@@ -20,19 +28,56 @@ namespace Compress.ZipFile
|
||||
return ZipReturn.ZipWritingToInputFile;
|
||||
}
|
||||
|
||||
LocalFile lf = new LocalFile(filename, timeStamp);
|
||||
ZipReturn validTrrntzip = ZipReturn.ZipGood;
|
||||
|
||||
ZipReturn retVal = lf.LocalFileOpenWriteStream(_zipFs, raw, trrntzip, uncompressedSize, compressionMethod, out stream);
|
||||
//invalid torrentZip Input If:
|
||||
if (compressionMethod != 8) validTrrntzip = ZipReturn.ZipTrrntzipIncorrectCompressionUsed;
|
||||
if (raw && !trrntzip) validTrrntzip = ZipReturn.ZipTrrntZipIncorrectDataStream;
|
||||
|
||||
int localFilesCount = _localFiles.Count;
|
||||
if (localFilesCount > 0)
|
||||
{
|
||||
// check that filenames are in trrntzip order
|
||||
string lastFilename = _localFiles[localFilesCount - 1].Filename;
|
||||
if (CompressUtils.TrrntZipStringCompare(lastFilename, filename) > 0)
|
||||
validTrrntzip = ZipReturn.ZipTrrntzipIncorrectFileOrder;
|
||||
|
||||
// check that no un-needed directory entries are added
|
||||
if (_localFiles[localFilesCount - 1].IsDirectory && filename.Length > lastFilename.Length)
|
||||
{
|
||||
if (CompressUtils.TrrntZipStringCompare(lastFilename, filename.Substring(0, lastFilename.Length)) == 0)
|
||||
{
|
||||
validTrrntzip = ZipReturn.ZipTrrntzipIncorrectDirectoryAddedToZip;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// if we are requirering a trrrntzp file and it is not a trrntzip formated supplied stream then error out
|
||||
if (writeZipType == OutputZipType.TrrntZip)
|
||||
{
|
||||
if (validTrrntzip != ZipReturn.ZipGood)
|
||||
return validTrrntzip;
|
||||
}
|
||||
|
||||
ZipLocalFile lf = new ZipLocalFile(filename, timeStamp);
|
||||
lf.SetStatus(LocalFileStatus.TrrntZip, validTrrntzip == ZipReturn.ZipGood);
|
||||
|
||||
ZipReturn retVal = lf.LocalFileOpenWriteStream(_zipFs, raw, uncompressedSize, compressionMethod, out stream);
|
||||
if (retVal != ZipReturn.ZipGood)
|
||||
return retVal;
|
||||
|
||||
if (filename.Length > 0)
|
||||
lf.IsDirectory = (filename.Substring(filename.Length - 1, 1) == "/");
|
||||
|
||||
_compressionStream = stream;
|
||||
_localFiles.Add(lf);
|
||||
|
||||
return retVal;
|
||||
return ZipReturn.ZipGood;
|
||||
}
|
||||
|
||||
public void ZipFileAddZeroLengthFile()
|
||||
{
|
||||
LocalFile.LocalFileAddZeroLengthFile(_zipFs);
|
||||
ZipLocalFile.LocalFileAddZeroLengthFile(_zipFs);
|
||||
}
|
||||
|
||||
|
||||
@@ -62,10 +107,8 @@ namespace Compress.ZipFile
|
||||
return ZipReturn.ZipErrorRollBackFile;
|
||||
}
|
||||
|
||||
LocalFile lf = _localFiles[fileCount - 1];
|
||||
|
||||
_localFiles.RemoveAt(fileCount - 1);
|
||||
_zipFs.Position = (long)lf.LocalFilePos;
|
||||
_zipFs.Position = (long)_localFiles[fileCount - 1].RelativeOffsetOfLocalHeader;
|
||||
return ZipReturn.ZipGood;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user