* Fix styling

This commit is contained in:
Phillip Stephens 2015-05-18 20:24:56 -07:00
parent 423a9a37d2
commit 6ee11b9a08
160 changed files with 11539 additions and 7742 deletions

View File

@ -366,9 +366,18 @@ LZO_EXTERN(const lzo_uint32_tp)
/* misc. */ /* misc. */
LZO_EXTERN(int) _lzo_config_check(void); LZO_EXTERN(int) _lzo_config_check(void);
typedef union { typedef union
lzo_voidp a00; lzo_bytep a01; lzo_uint a02; lzo_xint a03; lzo_uintptr_t a04; {
void *a05; unsigned char *a06; unsigned long a07; size_t a08; ptrdiff_t a09; lzo_voidp a00;
lzo_bytep a01;
lzo_uint a02;
lzo_xint a03;
lzo_uintptr_t a04;
void* a05;
unsigned char* a06;
unsigned long a07;
size_t a08;
ptrdiff_t a09;
#if defined(lzo_int64_t) #if defined(lzo_int64_t)
lzo_uint64_t a10; lzo_uint64_t a10;
#endif #endif

68
extern/lzo/src/lzo1.c vendored
View File

@ -160,8 +160,10 @@ lzo1_info ( int *rbits, int *clevel )
{ {
if (rbits) if (rbits)
*rbits = RBITS; *rbits = RBITS;
if (clevel) if (clevel)
*clevel = CLEVEL; *clevel = CLEVEL;
return D_SIZE * lzo_sizeof(lzo_bytep); return D_SIZE * lzo_sizeof(lzo_bytep);
} }
@ -203,6 +205,7 @@ lzo1_decompress ( const lzo_bytep in , lzo_uint in_len,
op = out; op = out;
ip = in; ip = in;
while (ip < ip_end) while (ip < ip_end)
{ {
t = *ip++; /* get marker */ t = *ip++; /* get marker */
@ -212,9 +215,11 @@ lzo1_decompress ( const lzo_bytep in , lzo_uint in_len,
if (t == 0) /* a R0 literal run */ if (t == 0) /* a R0 literal run */
{ {
t = *ip++; t = *ip++;
if (t >= R0FAST - R0MIN) /* a long R0 run */ if (t >= R0FAST - R0MIN) /* a long R0 run */
{ {
t -= R0FAST - R0MIN; t -= R0FAST - R0MIN;
if (t == 0) if (t == 0)
t = R0FAST; t = R0FAST;
else else
@ -224,15 +229,22 @@ lzo1_decompress ( const lzo_bytep in , lzo_uint in_len,
#else #else
/* help the optimizer */ /* help the optimizer */
lzo_uint tt = 256; lzo_uint tt = 256;
do tt <<= 1; while (--t > 0);
do tt <<= 1;
while (--t > 0);
t = tt; t = tt;
#endif #endif
} }
MEMCPY8_DS(op, ip, t); MEMCPY8_DS(op, ip, t);
continue; continue;
} }
t += R0MIN; t += R0MIN;
} }
MEMCPY_DS(op, ip, t); MEMCPY_DS(op, ip, t);
} }
else /* a match */ else /* a match */
@ -282,26 +294,33 @@ store_run(lzo_bytep op, const lzo_bytep ii, lzo_uint r_len)
if (r_len >= 512) if (r_len >= 512)
{ {
unsigned r_bits = 7; /* 256 << 7 == 32768 */ unsigned r_bits = 7; /* 256 << 7 == 32768 */
do {
do
{
while (r_len >= (256u << r_bits)) while (r_len >= (256u << r_bits))
{ {
r_len -= (256u << r_bits); r_len -= (256u << r_bits);
*op++ = 0; *op++ = LZO_BYTE((R0FAST - R0MIN) + r_bits); *op++ = 0;
*op++ = LZO_BYTE((R0FAST - R0MIN) + r_bits);
MEMCPY8_DS(op, ii, (256u << r_bits)); MEMCPY8_DS(op, ii, (256u << r_bits));
} }
} while (--r_bits > 0);
} }
while (--r_bits > 0);
}
while (r_len >= R0FAST) while (r_len >= R0FAST)
{ {
r_len -= R0FAST; r_len -= R0FAST;
*op++ = 0; *op++ = R0FAST - R0MIN; *op++ = 0;
*op++ = R0FAST - R0MIN;
MEMCPY8_DS(op, ii, R0FAST); MEMCPY8_DS(op, ii, R0FAST);
} }
if (r_len >= R0MIN) if (r_len >= R0MIN)
{ {
/* code a short R0 run */ /* code a short R0 run */
*op++ = 0; *op++ = LZO_BYTE(r_len - R0MIN); *op++ = 0;
*op++ = LZO_BYTE(r_len - R0MIN);
MEMCPY_DS(op, ii, r_len); MEMCPY_DS(op, ii, r_len);
} }
else if (r_len > 0) else if (r_len > 0)
@ -348,6 +367,7 @@ do_compress ( const lzo_bytep in , lzo_uint in_len,
op = out; op = out;
ip = in; ip = in;
ii = ip; /* point to start of literal run */ ii = ip; /* point to start of literal run */
if (in_len <= MIN_MATCH_LONG + DVAL_LEN + 1) if (in_len <= MIN_MATCH_LONG + DVAL_LEN + 1)
goto the_end; goto the_end;
@ -361,29 +381,38 @@ do_compress ( const lzo_bytep in , lzo_uint in_len,
ip++; ip++;
DVAL_NEXT(dv, ip); DVAL_NEXT(dv, ip);
do { do
{
LZO_DEFINE_UNINITIALIZED_VAR(lzo_uint, m_off, 0); LZO_DEFINE_UNINITIALIZED_VAR(lzo_uint, m_off, 0);
lzo_uint dindex; lzo_uint dindex;
DINDEX1(dindex, ip); DINDEX1(dindex, ip);
GINDEX(m_pos, m_off, dict, dindex, in); GINDEX(m_pos, m_off, dict, dindex, in);
if (LZO_CHECK_MPOS(m_pos, m_off, in, ip, MAX_OFFSET)) if (LZO_CHECK_MPOS(m_pos, m_off, in, ip, MAX_OFFSET))
goto literal; goto literal;
if (m_pos[0] == ip[0] && m_pos[1] == ip[1] && m_pos[2] == ip[2]) if (m_pos[0] == ip[0] && m_pos[1] == ip[1] && m_pos[2] == ip[2])
goto match; goto match;
DINDEX2(dindex, ip); DINDEX2(dindex, ip);
GINDEX(m_pos, m_off, dict, dindex, in); GINDEX(m_pos, m_off, dict, dindex, in);
if (LZO_CHECK_MPOS(m_pos, m_off, in, ip, MAX_OFFSET)) if (LZO_CHECK_MPOS(m_pos, m_off, in, ip, MAX_OFFSET))
goto literal; goto literal;
if (m_pos[0] == ip[0] && m_pos[1] == ip[1] && m_pos[2] == ip[2]) if (m_pos[0] == ip[0] && m_pos[1] == ip[1] && m_pos[2] == ip[2])
goto match; goto match;
goto literal; goto literal;
literal: literal:
UPDATE_I(dict, 0, dindex, ip, in); UPDATE_I(dict, 0, dindex, ip, in);
if (++ip >= ip_end) if (++ip >= ip_end)
break; break;
continue; continue;
match: match:
@ -397,11 +426,13 @@ match:
#if !defined(NDEBUG) && !(LZO_DICT_USE_PTR) #if !defined(NDEBUG) && !(LZO_DICT_USE_PTR)
assert((m_pos_sav = ip - m_off) == (m_pos - 3)); assert((m_pos_sav = ip - m_off) == (m_pos - 3));
#endif #endif
/* 1) store the current literal run */ /* 1) store the current literal run */
if (pd(ip, ii) > 0) if (pd(ip, ii) > 0)
{ {
lzo_uint t = pd(ip, ii); lzo_uint t = pd(ip, ii);
#if 1 #if 1
/* OPTIMIZED: inline the copying of a short run */ /* OPTIMIZED: inline the copying of a short run */
if (t < R0MIN) if (t < R0MIN)
{ {
@ -427,6 +458,7 @@ match:
#define PS *m_pos++ != *ip++ #define PS *m_pos++ != *ip++
#if (MIN_MATCH_LONG - MIN_MATCH == 2) /* MBITS == 2 */ #if (MIN_MATCH_LONG - MIN_MATCH == 2) /* MBITS == 2 */
if (PS || PS) if (PS || PS)
#elif (MIN_MATCH_LONG - MIN_MATCH == 6) /* MBITS == 3 */ #elif (MIN_MATCH_LONG - MIN_MATCH == 6) /* MBITS == 3 */
if (PS || PS || PS || PS || PS || PS) if (PS || PS || PS || PS || PS || PS)
@ -471,10 +503,14 @@ match:
#if (CLEVEL == 9) || (CLEVEL >= 7 && MBITS <= 4) || (CLEVEL >= 5 && MBITS <= 3) #if (CLEVEL == 9) || (CLEVEL >= 7 && MBITS <= 4) || (CLEVEL >= 5 && MBITS <= 3)
/* Insert the whole match (ii+1)..(ip-1) into dictionary. */ /* Insert the whole match (ii+1)..(ip-1) into dictionary. */
++ii; ++ii;
do {
do
{
DVAL_NEXT(dv, ii); DVAL_NEXT(dv, ii);
UPDATE_D(dict, 0, dv, ii, in); UPDATE_D(dict, 0, dv, ii, in);
} while (++ii < ip); }
while (++ii < ip);
DVAL_NEXT(dv, ii); DVAL_NEXT(dv, ii);
assert(ii == ip); assert(ii == ip);
DVAL_ASSERT(dv, ip); DVAL_ASSERT(dv, ip);
@ -506,6 +542,7 @@ match:
while (ip < end && *m_pos == *ip) while (ip < end && *m_pos == *ip)
m_pos++, ip++; m_pos++, ip++;
assert(ip <= in_end); assert(ip <= in_end);
/* 2b) code the long match */ /* 2b) code the long match */
@ -531,10 +568,14 @@ match:
/* Insert the whole match (ii+1)..(ip-1) into dictionary. */ /* Insert the whole match (ii+1)..(ip-1) into dictionary. */
/* This is not recommended because it is slow. */ /* This is not recommended because it is slow. */
++ii; ++ii;
do {
do
{
DVAL_NEXT(dv, ii); DVAL_NEXT(dv, ii);
UPDATE_D(dict, 0, dv, ii, in); UPDATE_D(dict, 0, dv, ii, in);
} while (++ii < ip); }
while (++ii < ip);
DVAL_NEXT(dv, ii); DVAL_NEXT(dv, ii);
assert(ii == ip); assert(ii == ip);
DVAL_ASSERT(dv, ip); DVAL_ASSERT(dv, ip);
@ -560,7 +601,8 @@ match:
/* ii now points to the start of next literal run */ /* ii now points to the start of next literal run */
assert(ii == ip); assert(ii == ip);
} }
} while (ip < ip_end); }
while (ip < ip_end);
@ -569,6 +611,7 @@ the_end:
#if defined(LZO_RETURN_IF_NOT_COMPRESSIBLE) #if defined(LZO_RETURN_IF_NOT_COMPRESSIBLE)
/* return -1 if op == out to indicate that we /* return -1 if op == out to indicate that we
* couldn't compress and didn't copy anything. * couldn't compress and didn't copy anything.
*/ */
@ -577,6 +620,7 @@ the_end:
*out_len = 0; *out_len = 0;
return LZO_E_NOT_COMPRESSIBLE; return LZO_E_NOT_COMPRESSIBLE;
} }
#endif #endif

View File

@ -108,8 +108,10 @@ lzo1a_info ( int *rbits, int *clevel )
{ {
if (rbits) if (rbits)
*rbits = RBITS; *rbits = RBITS;
if (clevel) if (clevel)
*clevel = CLEVEL; *clevel = CLEVEL;
return D_SIZE * lzo_sizeof(lzo_bytep); return D_SIZE * lzo_sizeof(lzo_bytep);
} }
@ -135,6 +137,7 @@ lzo1a_decompress ( const lzo_bytep in , lzo_uint in_len,
op = out; op = out;
ip = in; ip = in;
while (ip < ip_end) while (ip < ip_end)
{ {
t = *ip++; /* get marker */ t = *ip++; /* get marker */
@ -143,9 +146,11 @@ lzo1a_decompress ( const lzo_bytep in , lzo_uint in_len,
if (t == 0) /* a R0 literal run */ if (t == 0) /* a R0 literal run */
{ {
t = *ip++; t = *ip++;
if (t >= R0FAST - R0MIN) /* a long R0 run */ if (t >= R0FAST - R0MIN) /* a long R0 run */
{ {
t -= R0FAST - R0MIN; t -= R0FAST - R0MIN;
if (t == 0) if (t == 0)
t = R0FAST; t = R0FAST;
else else
@ -155,13 +160,19 @@ lzo1a_decompress ( const lzo_bytep in , lzo_uint in_len,
#else #else
/* help the optimizer */ /* help the optimizer */
lzo_uint tt = 256; lzo_uint tt = 256;
do tt <<= 1; while (--t > 0);
do tt <<= 1;
while (--t > 0);
t = tt; t = tt;
#endif #endif
} }
MEMCPY8_DS(op, ip, t); MEMCPY8_DS(op, ip, t);
continue; continue;
} }
t += R0MIN; t += R0MIN;
goto literal; goto literal;
} }
@ -174,6 +185,7 @@ literal:
while (ip < ip_end) while (ip < ip_end)
{ {
t = *ip++; /* get R1 marker */ t = *ip++; /* get R1 marker */
if (t >= R0MIN) if (t >= R0MIN)
goto match; goto match;
@ -181,7 +193,8 @@ literal:
assert((t & OMASK) == t); assert((t & OMASK) == t);
m_pos = op - MIN_OFFSET; m_pos = op - MIN_OFFSET;
m_pos -= t | (((lzo_uint) * ip++) << OBITS); m_pos -= t | (((lzo_uint) * ip++) << OBITS);
assert(m_pos >= out); assert(m_pos < op); assert(m_pos >= out);
assert(m_pos < op);
*op++ = m_pos[0]; *op++ = m_pos[0];
*op++ = m_pos[1]; *op++ = m_pos[1];
*op++ = m_pos[2]; *op++ = m_pos[2];
@ -194,7 +207,8 @@ match:
/* get match offset */ /* get match offset */
m_pos = op - MIN_OFFSET; m_pos = op - MIN_OFFSET;
m_pos -= (t & OMASK) | (((lzo_uint) * ip++) << OBITS); m_pos -= (t & OMASK) | (((lzo_uint) * ip++) << OBITS);
assert(m_pos >= out); assert(m_pos < op); assert(m_pos >= out);
assert(m_pos < op);
/* get match len */ /* get match len */
if (t < ((MSIZE - 1) << OBITS)) /* a short match */ if (t < ((MSIZE - 1) << OBITS)) /* a short match */
@ -217,9 +231,12 @@ match:
#if (LBITS < 8) #if (LBITS < 8)
/* a very short literal following a long match */ /* a very short literal following a long match */
t = ip[-1] >> LBITS; t = ip[-1] >> LBITS;
if (t) do if (t) do
*op++ = *ip++; *op++ = *ip++;
while (--t); while (--t);
#endif #endif
} }
} }
@ -275,31 +292,42 @@ do_compress ( const lzo_bytep in , lzo_uint in_len,
BZERO8_PTR(wrkmem, sizeof(lzo_dict_t), D_SIZE); BZERO8_PTR(wrkmem, sizeof(lzo_dict_t), D_SIZE);
#endif #endif
DVAL_FIRST(dv,ip); UPDATE_D(dict,0,dv,ip,in); ip++; DVAL_FIRST(dv, ip);
UPDATE_D(dict, 0, dv, ip, in);
ip++;
DVAL_NEXT(dv, ip); DVAL_NEXT(dv, ip);
do { do
{
LZO_DEFINE_UNINITIALIZED_VAR(lzo_uint, m_off, 0); LZO_DEFINE_UNINITIALIZED_VAR(lzo_uint, m_off, 0);
lzo_uint dindex; lzo_uint dindex;
DINDEX1(dindex, ip); DINDEX1(dindex, ip);
GINDEX(m_pos, m_off, dict, dindex, in); GINDEX(m_pos, m_off, dict, dindex, in);
if (LZO_CHECK_MPOS_NON_DET(m_pos, m_off, in, ip, MAX_OFFSET)) if (LZO_CHECK_MPOS_NON_DET(m_pos, m_off, in, ip, MAX_OFFSET))
goto literal; goto literal;
if (m_pos[0] == ip[0] && m_pos[1] == ip[1] && m_pos[2] == ip[2]) if (m_pos[0] == ip[0] && m_pos[1] == ip[1] && m_pos[2] == ip[2])
goto match; goto match;
DINDEX2(dindex, ip); DINDEX2(dindex, ip);
GINDEX(m_pos, m_off, dict, dindex, in); GINDEX(m_pos, m_off, dict, dindex, in);
if (LZO_CHECK_MPOS_NON_DET(m_pos, m_off, in, ip, MAX_OFFSET)) if (LZO_CHECK_MPOS_NON_DET(m_pos, m_off, in, ip, MAX_OFFSET))
goto literal; goto literal;
if (m_pos[0] == ip[0] && m_pos[1] == ip[1] && m_pos[2] == ip[2]) if (m_pos[0] == ip[0] && m_pos[1] == ip[1] && m_pos[2] == ip[2])
goto match; goto match;
goto literal; goto literal;
literal: literal:
UPDATE_I(dict, 0, dindex, ip, in); UPDATE_I(dict, 0, dindex, ip, in);
if (++ip >= ip_end) if (++ip >= ip_end)
break; break;
continue; continue;
match: match:
@ -376,13 +404,15 @@ match:
{ {
/* inline the copying of a short R0 run */ /* inline the copying of a short R0 run */
LZO_STATS(lzo_stats->r0short_runs++); LZO_STATS(lzo_stats->r0short_runs++);
*op++ = 0; *op++ = LZO_BYTE(t - R0MIN); *op++ = 0;
*op++ = LZO_BYTE(t - R0MIN);
MEMCPY_DS(op, ii, t); MEMCPY_DS(op, ii, t);
r1 = ip; /* set new R1 pointer */ r1 = ip; /* set new R1 pointer */
} }
else else
op = store_run(op, ii, t); op = store_run(op, ii, t);
} }
#if (LBITS < 8) #if (LBITS < 8)
im = ip; im = ip;
#endif #endif
@ -402,6 +432,7 @@ match:
#define PS *m_pos++ != *ip++ #define PS *m_pos++ != *ip++
#if (MIN_MATCH_LONG - MIN_MATCH == 2) /* MBITS == 2 */ #if (MIN_MATCH_LONG - MIN_MATCH == 2) /* MBITS == 2 */
if (PS || PS) if (PS || PS)
#elif (MIN_MATCH_LONG - MIN_MATCH == 6) /* MBITS == 3 */ #elif (MIN_MATCH_LONG - MIN_MATCH == 6) /* MBITS == 3 */
if (PS || PS || PS || PS || PS || PS) if (PS || PS || PS || PS || PS || PS)
@ -443,12 +474,16 @@ match:
#if (LZO_COLLECT_STATS) #if (LZO_COLLECT_STATS)
lzo_stats->short_matches++; lzo_stats->short_matches++;
lzo_stats->short_match[m_len]++; lzo_stats->short_match[m_len]++;
if (m_off < OSIZE) if (m_off < OSIZE)
lzo_stats->short_match_offset_osize[m_len]++; lzo_stats->short_match_offset_osize[m_len]++;
if (m_off < 256) if (m_off < 256)
lzo_stats->short_match_offset_256[m_len]++; lzo_stats->short_match_offset_256[m_len]++;
if (m_off < 1024) if (m_off < 1024)
lzo_stats->short_match_offset_1024[m_len]++; lzo_stats->short_match_offset_1024[m_len]++;
#endif #endif
@ -461,10 +496,14 @@ match:
#if (CLEVEL == 9) || (CLEVEL >= 7 && MBITS <= 4) || (CLEVEL >= 5 && MBITS <= 3) #if (CLEVEL == 9) || (CLEVEL >= 7 && MBITS <= 4) || (CLEVEL >= 5 && MBITS <= 3)
/* Insert the whole match (ii+1)..(ip-1) into dictionary. */ /* Insert the whole match (ii+1)..(ip-1) into dictionary. */
++ii; ++ii;
do {
do
{
DVAL_NEXT(dv, ii); DVAL_NEXT(dv, ii);
UPDATE_D(dict, 0, dv, ii, in); UPDATE_D(dict, 0, dv, ii, in);
} while (++ii < ip); }
while (++ii < ip);
DVAL_NEXT(dv, ii); DVAL_NEXT(dv, ii);
assert(ii == ip); assert(ii == ip);
DVAL_ASSERT(dv, ip); DVAL_ASSERT(dv, ip);
@ -496,6 +535,7 @@ match:
while (ip < end && *m_pos == *ip) while (ip < end && *m_pos == *ip)
m_pos++, ip++; m_pos++, ip++;
assert(ip <= in_end); assert(ip <= in_end);
/* 2a) compute match parameters */ /* 2a) compute match parameters */
@ -529,10 +569,14 @@ match:
/* Insert the whole match (ii+1)..(ip-1) into dictionary. */ /* Insert the whole match (ii+1)..(ip-1) into dictionary. */
/* This is not recommended because it is slow. */ /* This is not recommended because it is slow. */
++ii; ++ii;
do {
do
{
DVAL_NEXT(dv, ii); DVAL_NEXT(dv, ii);
UPDATE_D(dict, 0, dv, ii, in); UPDATE_D(dict, 0, dv, ii, in);
} while (++ii < ip); }
while (++ii < ip);
DVAL_NEXT(dv, ii); DVAL_NEXT(dv, ii);
assert(ii == ip); assert(ii == ip);
DVAL_ASSERT(dv, ip); DVAL_ASSERT(dv, ip);
@ -559,12 +603,14 @@ match:
assert(ii == ip); assert(ii == ip);
} }
} while (ip < ip_end); }
while (ip < ip_end);
assert(ip <= in_end); assert(ip <= in_end);
#if defined(LZO_RETURN_IF_NOT_COMPRESSIBLE) #if defined(LZO_RETURN_IF_NOT_COMPRESSIBLE)
/* return -1 if op == out to indicate that we /* return -1 if op == out to indicate that we
* couldn't compress and didn't copy anything. * couldn't compress and didn't copy anything.
*/ */
@ -573,6 +619,7 @@ match:
*out_len = 0; *out_len = 0;
return LZO_E_NOT_COMPRESSIBLE; return LZO_E_NOT_COMPRESSIBLE;
} }
#endif #endif
/* store the final literal run */ /* store the final literal run */

View File

@ -108,7 +108,9 @@
#if (CLEVEL == 9) || (CLEVEL >= 7 && M2L_BITS <= 4) || (CLEVEL >= 5 && M2L_BITS <= 3) #if (CLEVEL == 9) || (CLEVEL >= 7 && M2L_BITS <= 4) || (CLEVEL >= 5 && M2L_BITS <= 3)
/* Insert the whole match (ii+1)..(ip-1) into dictionary. */ /* Insert the whole match (ii+1)..(ip-1) into dictionary. */
++ii; ++ii;
do {
do
{
DVAL_NEXT(dv, ii); DVAL_NEXT(dv, ii);
#if 0 #if 0
UPDATE_D(dict, drun, dv, ii, in); UPDATE_D(dict, drun, dv, ii, in);
@ -116,7 +118,9 @@
dict[ DINDEX(dv, ii) ] = DENTRY(ii, in); dict[ DINDEX(dv, ii) ] = DENTRY(ii, in);
#endif #endif
MI MI
} while (++ii < ip); }
while (++ii < ip);
DVAL_NEXT(dv, ii); DVAL_NEXT(dv, ii);
assert(ii == ip); assert(ii == ip);
DVAL_ASSERT(dv, ip); DVAL_ASSERT(dv, ip);
@ -155,6 +159,7 @@
while (ip < end && *m_pos == *ip) while (ip < end && *m_pos == *ip)
m_pos++, ip++; m_pos++, ip++;
assert(ip <= in_end); assert(ip <= in_end);
/* 2a) compute match parameters */ /* 2a) compute match parameters */
@ -190,7 +195,9 @@
/* Insert the whole match (ii+1)..(ip-1) into dictionary. */ /* Insert the whole match (ii+1)..(ip-1) into dictionary. */
/* This is not recommended because it can be slow. */ /* This is not recommended because it can be slow. */
++ii; ++ii;
do {
do
{
DVAL_NEXT(dv, ii); DVAL_NEXT(dv, ii);
#if 0 #if 0
UPDATE_D(dict, drun, dv, ii, in); UPDATE_D(dict, drun, dv, ii, in);
@ -198,7 +205,9 @@
dict[ DINDEX(dv, ii) ] = DENTRY(ii, in); dict[ DINDEX(dv, ii) ] = DENTRY(ii, in);
#endif #endif
MI MI
} while (++ii < ip); }
while (++ii < ip);
DVAL_NEXT(dv, ii); DVAL_NEXT(dv, ii);
assert(ii == ip); assert(ii == ip);
DVAL_ASSERT(dv, ip); DVAL_ASSERT(dv, ip);

View File

@ -60,37 +60,48 @@ store_run(lzo_bytep const oo, const lzo_bytep const ii, lzo_uint r_len)
while (r_len >= (t = tt)) while (r_len >= (t = tt))
{ {
r_len -= t; r_len -= t;
*op++ = 0; *op++ = (R0MAX - R0MIN); *op++ = 0;
*op++ = (R0MAX - R0MIN);
MEMCPY8_DS(op, ip, t); MEMCPY8_DS(op, ip, t);
LZO_STATS(lzo_stats->r0long_runs++); LZO_STATS(lzo_stats->r0long_runs++);
} }
tt >>= 1; tt >>= 1;
do {
do
{
if (r_len >= (t = tt)) if (r_len >= (t = tt))
{ {
r_len -= t; r_len -= t;
*op++ = 0; *op++ = LZO_BYTE((R0FAST - R0MIN) + r_bits); *op++ = 0;
*op++ = LZO_BYTE((R0FAST - R0MIN) + r_bits);
MEMCPY8_DS(op, ip, t); MEMCPY8_DS(op, ip, t);
LZO_STATS(lzo_stats->r0long_runs++); LZO_STATS(lzo_stats->r0long_runs++);
} }
tt >>= 1; tt >>= 1;
} while (--r_bits > 0);
} }
while (--r_bits > 0);
}
assert(r_len < 512); assert(r_len < 512);
while (r_len >= (t = R0FAST)) while (r_len >= (t = R0FAST))
{ {
r_len -= t; r_len -= t;
*op++ = 0; *op++ = (R0FAST - R0MIN); *op++ = 0;
*op++ = (R0FAST - R0MIN);
MEMCPY8_DS(op, ip, t); MEMCPY8_DS(op, ip, t);
LZO_STATS(lzo_stats->r0fast_runs++); LZO_STATS(lzo_stats->r0fast_runs++);
} }
t = r_len; t = r_len;
if (t >= R0MIN) if (t >= R0MIN)
{ {
/* code a short R0 run */ /* code a short R0 run */
*op++ = 0; *op++ = LZO_BYTE(t - R0MIN); *op++ = 0;
*op++ = LZO_BYTE(t - R0MIN);
MEMCPY_DS(op, ip, t); MEMCPY_DS(op, ip, t);
LZO_STATS(lzo_stats->r0short_runs++); LZO_STATS(lzo_stats->r0short_runs++);
} }

View File

@ -71,6 +71,7 @@ code_match ( LZO_COMPRESS_T *c, lzo_bytep op, lzo_uint m_len, lzo_uint m_off )
assert(m_off <= M3_MAX_OFFSET); assert(m_off <= M3_MAX_OFFSET);
m_off -= M3_MIN_OFFSET - M3_EOF_OFFSET; m_off -= M3_MIN_OFFSET - M3_EOF_OFFSET;
/* code match len */ /* code match len */
if (m_len <= M3_MAX_LEN) if (m_len <= M3_MAX_LEN)
*op++ = LZO_BYTE(M3_MARKER | (m_len - (M3_MIN_LEN - 1))); *op++ = LZO_BYTE(M3_MARKER | (m_len - (M3_MIN_LEN - 1)));
@ -81,14 +82,17 @@ code_match ( LZO_COMPRESS_T *c, lzo_bytep op, lzo_uint m_len, lzo_uint m_off )
*op++ = M4_MARKER; *op++ = M4_MARKER;
/* code match len */ /* code match len */
m_len -= M4_MIN_LEN - 1; m_len -= M4_MIN_LEN - 1;
while (m_len > 255) while (m_len > 255)
{ {
m_len -= 255; m_len -= 255;
*op++ = 0; *op++ = 0;
} }
assert(m_len > 0); assert(m_len > 0);
*op++ = LZO_BYTE(m_len); *op++ = LZO_BYTE(m_len);
} }
/* code low offset bits */ /* code low offset bits */
*op++ = LZO_BYTE(m_off & M3O_MASK); *op++ = LZO_BYTE(m_off & M3O_MASK);
/* code high offset bits */ /* code high offset bits */
@ -97,6 +101,7 @@ code_match ( LZO_COMPRESS_T *c, lzo_bytep op, lzo_uint m_len, lzo_uint m_off )
c->r1_m_len = 0; c->r1_m_len = 0;
c->m3_m++; c->m3_m++;
} }
return op; return op;
} }
@ -143,14 +148,18 @@ lzo1b_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
c->r1_m_len = 0; c->r1_m_len = 0;
r = init_match(c, swd, NULL, 0, 0); r = init_match(c, swd, NULL, 0, 0);
if (r != 0) if (r != 0)
return r; return r;
if (max_chain > 0) if (max_chain > 0)
swd->max_chain = max_chain; swd->max_chain = max_chain;
r = find_match(c, swd, 0, 0); r = find_match(c, swd, 0, 0);
if (r != 0) if (r != 0)
return r; return r;
while (c->look > 0) while (c->look > 0)
{ {
int lazy_match_min_gain = -1; int lazy_match_min_gain = -1;
@ -165,8 +174,10 @@ lzo1b_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
#endif #endif
assert(c->ip - c->look >= in); assert(c->ip - c->look >= in);
if (lit == 0) if (lit == 0)
ii = c->ip - c->look; ii = c->ip - c->look;
assert(ii + lit == c->ip - c->look); assert(ii + lit == c->ip - c->look);
assert(swd->b_char == *(c->ip - c->look)); assert(swd->b_char == *(c->ip - c->look));
@ -192,20 +203,24 @@ lzo1b_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
lazy_match_min_gain = 1; lazy_match_min_gain = 1;
#if (M2_MIN_LEN == 2) #if (M2_MIN_LEN == 2)
if (m_len == 2) if (m_len == 2)
{ {
/* don't code a match of len 2 if we have to /* don't code a match of len 2 if we have to
code a literal run. Code a literal instead. */ code a literal run. Code a literal instead. */
m_len = 0; m_len = 0;
} }
#endif #endif
#if (M2_MIN_LEN == M3_MIN_LEN) #if (M2_MIN_LEN == M3_MIN_LEN)
if (m_len == M2_MIN_LEN && m_off > M2_MAX_OFFSET) if (m_len == M2_MIN_LEN && m_off > M2_MAX_OFFSET)
{ {
/* don't code a M3 match of len 3 if we have to /* don't code a M3 match of len 3 if we have to
code a literal run. Code a literal instead. */ code a literal run. Code a literal instead. */
m_len = 0; m_len = 0;
} }
#endif #endif
} }
else else
@ -223,12 +238,14 @@ lzo1b_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
/* try a lazy match */ /* try a lazy match */
if (m_len == 0) if (m_len == 0)
lazy_match_min_gain = -1; lazy_match_min_gain = -1;
if (lazy_match_min_gain >= 0 && c->look > m_len) if (lazy_match_min_gain >= 0 && c->look > m_len)
{ {
assert(m_len > 0); assert(m_len > 0);
r = find_match(c, swd, 1, 0); r = find_match(c, swd, 1, 0);
assert(r == 0); LZO_UNUSED(r); assert(r == 0);
LZO_UNUSED(r);
assert(c->look > 0); assert(c->look > 0);
if (m_len <= M2_MAX_LEN && m_off <= M2_MAX_OFFSET && if (m_len <= M2_MAX_LEN && m_off <= M2_MAX_OFFSET &&
@ -253,8 +270,10 @@ lzo1b_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
ahead = 1; ahead = 1;
assert(ii + lit + 1 == c->ip - c->look); assert(ii + lit + 1 == c->ip - c->look);
} }
assert(m_len > 0); assert(m_len > 0);
} }
assert(ii + lit + ahead == c->ip - c->look); assert(ii + lit + ahead == c->ip - c->look);
@ -263,7 +282,8 @@ lzo1b_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
/* a literal */ /* a literal */
lit++; lit++;
r = find_match(c, swd, 1, 0); r = find_match(c, swd, 1, 0);
assert(r == 0); LZO_UNUSED(r); assert(r == 0);
LZO_UNUSED(r);
} }
else else
{ {
@ -286,10 +306,12 @@ lzo1b_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
{ {
op = STORE_RUN(op, ii, lit); op = STORE_RUN(op, ii, lit);
} }
if (lit < R0FAST) if (lit < R0FAST)
c->r1_m_len = m_len; c->r1_m_len = m_len;
else else
c->r1_m_len = 0; c->r1_m_len = 0;
lit = 0; lit = 0;
} }
else else
@ -298,7 +320,8 @@ lzo1b_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
/* 2 - code match */ /* 2 - code match */
op = code_match(c, op, m_len, m_off); op = code_match(c, op, m_len, m_off);
r = find_match(c, swd, m_len, 1 + ahead); r = find_match(c, swd, m_len, 1 + ahead);
assert(r == 0); LZO_UNUSED(r); assert(r == 0);
LZO_UNUSED(r);
} }
c->codesize = pd(op, out); c->codesize = pd(op, out);

View File

@ -120,6 +120,7 @@ do_compress ( const lzo_bytep in , lzo_uint in_len,
#endif #endif
assert(ip < ip_end); assert(ip < ip_end);
for (;;) for (;;)
{ {
const lzo_bytep m_pos; const lzo_bytep m_pos;
@ -162,8 +163,10 @@ literal:
#if (DD_BITS == 0) #if (DD_BITS == 0)
UPDATE_I(dict, 0, dindex, ip, in); UPDATE_I(dict, 0, dindex, ip, in);
#endif #endif
if (++ip >= ip_end) if (++ip >= ip_end)
break; break;
#if (DD_BITS > 0) #if (DD_BITS > 0)
DVAL_NEXT(dv, ip); DVAL_NEXT(dv, ip);
#endif #endif
@ -224,9 +227,11 @@ match:
for (i = 0; i < D_SIZE; i++) for (i = 0; i < D_SIZE; i++)
{ {
p = dict[i]; p = dict[i];
if (BOUNDS_CHECKING_OFF_IN_EXPR(p == NULL || p < in || p > in_end)) if (BOUNDS_CHECKING_OFF_IN_EXPR(p == NULL || p < in || p > in_end))
lzo_stats->unused_dict_entries++; lzo_stats->unused_dict_entries++;
} }
lzo_stats->unused_dict_entries_percent = lzo_stats->unused_dict_entries_percent =
100.0 * lzo_stats->unused_dict_entries / D_SIZE; 100.0 * lzo_stats->unused_dict_entries / D_SIZE;
} }
@ -234,6 +239,7 @@ match:
#if defined(LZO_RETURN_IF_NOT_COMPRESSIBLE) #if defined(LZO_RETURN_IF_NOT_COMPRESSIBLE)
/* return if op == out to indicate that we /* return if op == out to indicate that we
* couldn't compress and didn't copy anything. * couldn't compress and didn't copy anything.
*/ */
@ -242,6 +248,7 @@ match:
*out_len = 0; *out_len = 0;
return LZO_E_NOT_COMPRESSIBLE; return LZO_E_NOT_COMPRESSIBLE;
} }
#endif #endif
/* store the final literal run */ /* store the final literal run */

View File

@ -74,9 +74,12 @@ _lzo1b_do_compress ( const lzo_bytep in, lzo_uint in_len,
#if defined(LZO_EOF_CODE) #if defined(LZO_EOF_CODE)
#if defined(LZO_TEST_COMPRESS_OVERRUN) #if defined(LZO_TEST_COMPRESS_OVERRUN)
if (r == LZO_E_OK && avail_out - *out_len < 3) if (r == LZO_E_OK && avail_out - *out_len < 3)
r = LZO_E_COMPRESS_OVERRUN; r = LZO_E_COMPRESS_OVERRUN;
#endif #endif
if (r == LZO_E_OK) if (r == LZO_E_OK)
{ {
lzo_bytep op = out + *out_len; lzo_bytep op = out + *out_len;
@ -85,6 +88,7 @@ _lzo1b_do_compress ( const lzo_bytep in, lzo_uint in_len,
op[2] = 0; op[2] = 0;
*out_len += 3; *out_len += 3;
} }
#endif #endif

View File

@ -86,6 +86,7 @@
/* 2b) code the match */ /* 2b) code the match */
#if (_M2_MAX_OFFSET != _M3_MAX_OFFSET) #if (_M2_MAX_OFFSET != _M3_MAX_OFFSET)
if (m_off <= M2_MAX_OFFSET) if (m_off <= M2_MAX_OFFSET)
{ {
#else #else
@ -122,6 +123,7 @@
m3 = op; /* set M3 pointer */ m3 = op; /* set M3 pointer */
#endif #endif
} }
#endif /* (_M2_MAX_OFFSET != _M3_MAX_OFFSET) */ #endif /* (_M2_MAX_OFFSET != _M3_MAX_OFFSET) */
@ -137,7 +139,9 @@
#if (CLEVEL == 9) || (CLEVEL >= 7 && M2L_BITS <= 4) || (CLEVEL >= 5 && M2L_BITS <= 3) #if (CLEVEL == 9) || (CLEVEL >= 7 && M2L_BITS <= 4) || (CLEVEL >= 5 && M2L_BITS <= 3)
/* Insert the whole match (ii+1)..(ip-1) into dictionary. */ /* Insert the whole match (ii+1)..(ip-1) into dictionary. */
++ii; ++ii;
do {
do
{
DVAL_NEXT(dv, ii); DVAL_NEXT(dv, ii);
#if 0 #if 0
UPDATE_D(dict, drun, dv, ii, in); UPDATE_D(dict, drun, dv, ii, in);
@ -145,7 +149,9 @@
dict[ DINDEX(dv, ii) ] = DENTRY(ii, in); dict[ DINDEX(dv, ii) ] = DENTRY(ii, in);
#endif #endif
MI MI
} while (++ii < ip); }
while (++ii < ip);
DVAL_NEXT(dv, ii); DVAL_NEXT(dv, ii);
assert(ii == ip); assert(ii == ip);
DVAL_ASSERT(dv, ip); DVAL_ASSERT(dv, ip);
@ -176,8 +182,10 @@
{ {
const lzo_bytep end; const lzo_bytep end;
end = in_end; end = in_end;
while (ip < end && *m_pos == *ip) while (ip < end && *m_pos == *ip)
m_pos++, ip++; m_pos++, ip++;
assert(ip <= in_end); assert(ip <= in_end);
m_len = pd(ip, ii); m_len = pd(ip, ii);
} }
@ -209,11 +217,13 @@
*op++ = M4_MARKER; *op++ = M4_MARKER;
/* code match len */ /* code match len */
m_len -= M4_MIN_LEN - 1; m_len -= M4_MIN_LEN - 1;
while (m_len > 255) while (m_len > 255)
{ {
m_len -= 255; m_len -= 255;
*op++ = 0; *op++ = 0;
} }
assert(m_len > 0); assert(m_len > 0);
*op++ = LZO_BYTE(m_len); *op++ = LZO_BYTE(m_len);
LZO_STATS(lzo_stats->m4_matches++); LZO_STATS(lzo_stats->m4_matches++);
@ -242,7 +252,9 @@
/* Insert the whole match (ii+1)..(ip-1) into dictionary. */ /* Insert the whole match (ii+1)..(ip-1) into dictionary. */
/* This is not recommended because it can be slow. */ /* This is not recommended because it can be slow. */
++ii; ++ii;
do {
do
{
DVAL_NEXT(dv, ii); DVAL_NEXT(dv, ii);
#if 0 #if 0
UPDATE_D(dict, drun, dv, ii, in); UPDATE_D(dict, drun, dv, ii, in);
@ -250,7 +262,9 @@
dict[ DINDEX(dv, ii) ] = DENTRY(ii, in); dict[ DINDEX(dv, ii) ] = DENTRY(ii, in);
#endif #endif
MI MI
} while (++ii < ip); }
while (++ii < ip);
DVAL_NEXT(dv, ii); DVAL_NEXT(dv, ii);
assert(ii == ip); assert(ii == ip);
DVAL_ASSERT(dv, ip); DVAL_ASSERT(dv, ip);

View File

@ -38,11 +38,13 @@
************************************************************************/ ************************************************************************/
assert(ip < ip_end); assert(ip < ip_end);
if (pd(ip, ii) > 0) if (pd(ip, ii) > 0)
{ {
lzo_uint t = pd(ip, ii); lzo_uint t = pd(ip, ii);
#if defined(LZO_HAVE_R1) #if defined(LZO_HAVE_R1)
if (ip == r1) if (ip == r1)
{ {
/* Code a context sensitive R1 match. */ /* Code a context sensitive R1 match. */
@ -66,6 +68,7 @@
LZO_STATS(lzo_stats->lit_runs++); LZO_STATS(lzo_stats->lit_runs++);
LZO_STATS(lzo_stats->lit_run[t]++); LZO_STATS(lzo_stats->lit_run[t]++);
#if defined(LZO_HAVE_M3) #if defined(LZO_HAVE_M3)
if (t < LZO_SIZE(8 - M3O_BITS) && op == m3) if (t < LZO_SIZE(8 - M3O_BITS) && op == m3)
{ {
/* Code a very short literal run into the low offset bits /* Code a very short literal run into the low offset bits
@ -81,6 +84,7 @@
{ {
*op++ = LZO_BYTE(t); *op++ = LZO_BYTE(t);
} }
MEMCPY_DS(op, ii, t); MEMCPY_DS(op, ii, t);
#if defined(LZO_HAVE_R1) #if defined(LZO_HAVE_R1)
r1 = ip + (M2_MIN_LEN + 1); /* set new R1 pointer */ r1 = ip + (M2_MIN_LEN + 1); /* set new R1 pointer */
@ -91,7 +95,8 @@
/* inline the copying of a short R0 run */ /* inline the copying of a short R0 run */
LZO_STATS(lzo_stats->literals += t); LZO_STATS(lzo_stats->literals += t);
LZO_STATS(lzo_stats->r0short_runs++); LZO_STATS(lzo_stats->r0short_runs++);
*op++ = 0; *op++ = LZO_BYTE(t - R0MIN); *op++ = 0;
*op++ = LZO_BYTE(t - R0MIN);
MEMCPY_DS(op, ii, t); MEMCPY_DS(op, ii, t);
#if defined(LZO_HAVE_R1) #if defined(LZO_HAVE_R1)
r1 = ip + (M2_MIN_LEN + 1); /* set new R1 pointer */ r1 = ip + (M2_MIN_LEN + 1); /* set new R1 pointer */

View File

@ -63,9 +63,11 @@ DO_DECOMPRESS ( const lzo_bytep in , lzo_uint in_len,
{ {
NEED_IP(1); NEED_IP(1);
t = *ip++; t = *ip++;
if (t >= R0FAST - R0MIN) /* a long R0 run */ if (t >= R0FAST - R0MIN) /* a long R0 run */
{ {
t -= R0FAST - R0MIN; t -= R0FAST - R0MIN;
if (t == 0) if (t == 0)
t = R0FAST; t = R0FAST;
else else
@ -75,37 +77,57 @@ DO_DECOMPRESS ( const lzo_bytep in , lzo_uint in_len,
#else #else
/* help the optimizer */ /* help the optimizer */
lzo_uint tt = 256; lzo_uint tt = 256;
do tt <<= 1; while (--t > 0);
do tt <<= 1;
while (--t > 0);
t = tt; t = tt;
#endif #endif
} }
NEED_IP(t); NEED_OP(t); NEED_IP(t);
NEED_OP(t);
#if 1 && (LZO_OPT_UNALIGNED32) #if 1 && (LZO_OPT_UNALIGNED32)
do {
do
{
UA_COPY4(op + 0, ip + 0); UA_COPY4(op + 0, ip + 0);
UA_COPY4(op + 4, ip + 4); UA_COPY4(op + 4, ip + 4);
op += 8; ip += 8; op += 8;
ip += 8;
t -= 8; t -= 8;
} while (t > 0); }
while (t > 0);
#else #else
MEMCPY8_DS(op, ip, t); MEMCPY8_DS(op, ip, t);
#endif #endif
continue; continue;
} }
t += R0MIN; /* a short R0 run */ t += R0MIN; /* a short R0 run */
} }
NEED_IP(t); NEED_OP(t); NEED_IP(t);
NEED_OP(t);
/* copy literal run */ /* copy literal run */
#if 1 && (LZO_OPT_UNALIGNED32) #if 1 && (LZO_OPT_UNALIGNED32)
if (t >= 4) if (t >= 4)
{ {
do { do
{
UA_COPY4(op, ip); UA_COPY4(op, ip);
op += 4; ip += 4; t -= 4; op += 4;
} while (t >= 4); ip += 4;
if (t > 0) do *op++ = *ip++; while (--t > 0); t -= 4;
}
while (t >= 4);
if (t > 0) do* op++ = *ip++;
while (--t > 0);
} }
else else
#endif #endif
@ -113,7 +135,10 @@ DO_DECOMPRESS ( const lzo_bytep in , lzo_uint in_len,
#if (M3O_BITS < 7) #if (M3O_BITS < 7)
literal1: literal1:
#endif #endif
do *op++ = *ip++; while (--t > 0);
do* op++ = *ip++;
while (--t > 0);
} }
#if (M3O_BITS == 7) #if (M3O_BITS == 7)
@ -124,16 +149,19 @@ literal2:
while (TEST_IP_AND_TEST_OP) while (TEST_IP_AND_TEST_OP)
{ {
t = *ip++; /* get R1 marker */ t = *ip++; /* get R1 marker */
if (t >= R0MIN) if (t >= R0MIN)
goto match; goto match;
NEED_IP(2); NEED_OP(M2_MIN_LEN + 1); NEED_IP(2);
NEED_OP(M2_MIN_LEN + 1);
/* R1 match - a M2_MIN_LEN match + 1 byte literal */ /* R1 match - a M2_MIN_LEN match + 1 byte literal */
assert((t & M2O_MASK) == t); assert((t & M2O_MASK) == t);
m_pos = op - M2_MIN_OFFSET; m_pos = op - M2_MIN_OFFSET;
m_pos -= t | (((lzo_uint) * ip++) << M2O_BITS); m_pos -= t | (((lzo_uint) * ip++) << M2O_BITS);
assert(m_pos >= out); assert(m_pos < op); assert(m_pos >= out);
assert(m_pos < op);
TEST_LB(m_pos); TEST_LB(m_pos);
COPY_M2; COPY_M2;
*op++ = *ip++; *op++ = *ip++;
@ -152,7 +180,8 @@ match:
NEED_IP(1); NEED_IP(1);
m_pos = op - M2_MIN_OFFSET; m_pos = op - M2_MIN_OFFSET;
m_pos -= (t & M2O_MASK) | (((lzo_uint) * ip++) << M2O_BITS); m_pos -= (t & M2O_MASK) | (((lzo_uint) * ip++) << M2O_BITS);
assert(m_pos >= out); assert(m_pos < op); assert(m_pos >= out);
assert(m_pos < op);
TEST_LB(m_pos); TEST_LB(m_pos);
/* get match len */ /* get match len */
@ -165,9 +194,11 @@ match:
{ {
/* get match len */ /* get match len */
t &= M3L_MASK; t &= M3L_MASK;
if (t == 0) /* a M4 match */ if (t == 0) /* a M4 match */
{ {
NEED_IP(1); NEED_IP(1);
while (*ip == 0) while (*ip == 0)
{ {
t += 255; t += 255;
@ -175,6 +206,7 @@ match:
TEST_OV(t); TEST_OV(t);
NEED_IP(1); NEED_IP(1);
} }
t += (M4_MIN_LEN - M3_MIN_LEN) + *ip++; t += (M4_MIN_LEN - M3_MIN_LEN) + *ip++;
} }
@ -184,23 +216,38 @@ match:
m_pos -= *ip++ & M3O_MASK; m_pos -= *ip++ & M3O_MASK;
m_pos -= (lzo_uint)(*ip++) << M3O_BITS; m_pos -= (lzo_uint)(*ip++) << M3O_BITS;
#if defined(LZO_EOF_CODE) #if defined(LZO_EOF_CODE)
if (m_pos == op) if (m_pos == op)
goto eof_found; goto eof_found;
#endif #endif
/* copy match */ /* copy match */
assert(m_pos >= out); assert(m_pos < op); assert(m_pos >= out);
TEST_LB(m_pos); NEED_OP(t + M3_MIN_LEN - 1); assert(m_pos < op);
TEST_LB(m_pos);
NEED_OP(t + M3_MIN_LEN - 1);
#if (LZO_OPT_UNALIGNED32) #if (LZO_OPT_UNALIGNED32)
if (t >= 2 * 4 - (M3_MIN_LEN - 1) && (op - m_pos) >= 4) if (t >= 2 * 4 - (M3_MIN_LEN - 1) && (op - m_pos) >= 4)
{ {
UA_COPY4(op, m_pos); UA_COPY4(op, m_pos);
op += 4; m_pos += 4; t -= 4 - (M3_MIN_LEN - 1); op += 4;
do { m_pos += 4;
t -= 4 - (M3_MIN_LEN - 1);
do
{
UA_COPY4(op, m_pos); UA_COPY4(op, m_pos);
op += 4; m_pos += 4; t -= 4; op += 4;
} while (t >= 4); m_pos += 4;
if (t > 0) do *op++ = *m_pos++; while (--t > 0); t -= 4;
}
while (t >= 4);
if (t > 0) do* op++ = *m_pos++;
while (--t > 0);
} }
else else
#endif #endif
@ -212,19 +259,25 @@ match:
#if (M3O_BITS < 7) #if (M3O_BITS < 7)
t = ip[-2] >> M3O_BITS; t = ip[-2] >> M3O_BITS;
if (t) if (t)
{ {
NEED_IP(t); NEED_OP(t); NEED_IP(t);
NEED_OP(t);
goto literal1; goto literal1;
} }
#elif (M3O_BITS == 7) #elif (M3O_BITS == 7)
/* optimized version */ /* optimized version */
if (ip[-2] & (1 << M3O_BITS)) if (ip[-2] & (1 << M3O_BITS))
{ {
NEED_IP(1); NEED_OP(1); NEED_IP(1);
NEED_OP(1);
*op++ = *ip++; *op++ = *ip++;
goto literal2; goto literal2;
} }
#endif #endif
} }
} }

View File

@ -52,37 +52,48 @@ STORE_RUN ( lzo_bytep const oo, const lzo_bytep const ii, lzo_uint r_len)
while (r_len >= (t = tt)) while (r_len >= (t = tt))
{ {
r_len -= t; r_len -= t;
*op++ = 0; *op++ = (R0FAST - R0MIN) + 7; *op++ = 0;
*op++ = (R0FAST - R0MIN) + 7;
MEMCPY8_DS(op, ip, t); MEMCPY8_DS(op, ip, t);
LZO_STATS(lzo_stats->r0long_runs++); LZO_STATS(lzo_stats->r0long_runs++);
} }
tt >>= 1; tt >>= 1;
do {
do
{
if (r_len >= (t = tt)) if (r_len >= (t = tt))
{ {
r_len -= t; r_len -= t;
*op++ = 0; *op++ = LZO_BYTE((R0FAST - R0MIN) + r_bits); *op++ = 0;
*op++ = LZO_BYTE((R0FAST - R0MIN) + r_bits);
MEMCPY8_DS(op, ip, t); MEMCPY8_DS(op, ip, t);
LZO_STATS(lzo_stats->r0long_runs++); LZO_STATS(lzo_stats->r0long_runs++);
} }
tt >>= 1; tt >>= 1;
} while (--r_bits > 0);
} }
while (--r_bits > 0);
}
assert(r_len < 512); assert(r_len < 512);
while (r_len >= (t = R0FAST)) while (r_len >= (t = R0FAST))
{ {
r_len -= t; r_len -= t;
*op++ = 0; *op++ = (R0FAST - R0MIN); *op++ = 0;
*op++ = (R0FAST - R0MIN);
MEMCPY8_DS(op, ip, t); MEMCPY8_DS(op, ip, t);
LZO_STATS(lzo_stats->r0fast_runs++); LZO_STATS(lzo_stats->r0fast_runs++);
} }
t = r_len; t = r_len;
if (t >= R0MIN) if (t >= R0MIN)
{ {
/* code a short R0 run */ /* code a short R0 run */
*op++ = 0; *op++ = LZO_BYTE(t - R0MIN); *op++ = 0;
*op++ = LZO_BYTE(t - R0MIN);
MEMCPY_DS(op, ip, t); MEMCPY_DS(op, ip, t);
LZO_STATS(lzo_stats->r0short_runs++); LZO_STATS(lzo_stats->r0short_runs++);
} }

View File

@ -42,18 +42,25 @@
/* search ip in the dictionary */ /* search ip in the dictionary */
DINDEX1(dindex, ip); DINDEX1(dindex, ip);
GINDEX(m_pos, m_off, dict, dindex, in); GINDEX(m_pos, m_off, dict, dindex, in);
if (LZO_CHECK_MPOS_NON_DET(m_pos, m_off, in, ip, M3_MAX_OFFSET)) if (LZO_CHECK_MPOS_NON_DET(m_pos, m_off, in, ip, M3_MAX_OFFSET))
goto literal; goto literal;
#if 1 #if 1
if (m_off <= M2_MAX_OFFSET || m_pos[3] == ip[3]) if (m_off <= M2_MAX_OFFSET || m_pos[3] == ip[3])
goto try_match; goto try_match;
DINDEX2(dindex, ip); DINDEX2(dindex, ip);
#endif #endif
GINDEX(m_pos, m_off, dict, dindex, in); GINDEX(m_pos, m_off, dict, dindex, in);
if (LZO_CHECK_MPOS_NON_DET(m_pos, m_off, in, ip, M3_MAX_OFFSET)) if (LZO_CHECK_MPOS_NON_DET(m_pos, m_off, in, ip, M3_MAX_OFFSET))
goto literal; goto literal;
if (m_off <= M2_MAX_OFFSET || m_pos[3] == ip[3]) if (m_off <= M2_MAX_OFFSET || m_pos[3] == ip[3])
goto try_match; goto try_match;
goto literal; goto literal;
@ -73,7 +80,9 @@
ip_sav = ip; ip_sav = ip;
m_len = 0; m_len = 0;
do {
do
{
#if !defined(NDEBUG) #if !defined(NDEBUG)
const lzo_bytep z_pos = NULL; const lzo_bytep z_pos = NULL;
#endif #endif
@ -92,6 +101,7 @@
if (LZO_CHECK_MPOS(m_pos, x_off, in, ip, MAX_OFFSET)) if (LZO_CHECK_MPOS(m_pos, x_off, in, ip, MAX_OFFSET))
#if (CLEVEL == 9) #if (CLEVEL == 9)
*d = DENTRY(ip, in); *d = DENTRY(ip, in);
#else #else
((void)(0)); ((void)(0));
#endif #endif
@ -102,16 +112,19 @@
#if !(LZO_DICT_USE_PTR) #if !(LZO_DICT_USE_PTR)
assert((z_pos = ip - 3 - x_off) == (m_pos - 3)); assert((z_pos = ip - 3 - x_off) == (m_pos - 3));
#endif #endif
/* a match */ /* a match */
if (MATCH_M2) if (MATCH_M2)
{ {
x_len = pd((ip - 1), ip_sav); x_len = pd((ip - 1), ip_sav);
if (x_len > m_len) if (x_len > m_len)
{ {
m_len = x_len; m_len = x_len;
m_off = x_off; m_off = x_off;
assert((m_pos_sav = z_pos) != NULL); assert((m_pos_sav = z_pos) != NULL);
} }
#if (CLEVEL == 9) #if (CLEVEL == 9)
/* try to find a closer match */ /* try to find a closer match */
else if (x_len == m_len && x_off < m_off) else if (x_len == m_len && x_off < m_off)
@ -119,6 +132,7 @@
m_off = x_off; m_off = x_off;
assert((m_pos_sav = z_pos) != NULL); assert((m_pos_sav = z_pos) != NULL);
} }
#endif #endif
} }
else else
@ -129,16 +143,20 @@
{ {
const lzo_bytep end; const lzo_bytep end;
end = MATCH_IP_END; end = MATCH_IP_END;
while (ip < end && *m_pos == *ip) while (ip < end && *m_pos == *ip)
m_pos++, ip++; m_pos++, ip++;
assert(ip <= in_end); assert(ip <= in_end);
x_len = pd(ip, ip_sav); x_len = pd(ip, ip_sav);
} }
if (x_len > m_len) if (x_len > m_len)
{ {
m_len = x_len; m_len = x_len;
m_off = x_off; m_off = x_off;
assert((m_pos_sav = z_pos) != NULL); assert((m_pos_sav = z_pos) != NULL);
if (ip >= MATCH_IP_END) if (ip >= MATCH_IP_END)
{ {
ip = ip_sav; ip = ip_sav;
@ -156,7 +174,9 @@
m_off = x_off; m_off = x_off;
assert((m_pos_sav = z_pos) != NULL); assert((m_pos_sav = z_pos) != NULL);
} }
#else #else
/* try to find a closer match */ /* try to find a closer match */
if (m_len < M2_MAX_LEN + 1 || x_off < m_off) if (m_len < M2_MAX_LEN + 1 || x_off < m_off)
{ {
@ -164,6 +184,7 @@
m_off = x_off; m_off = x_off;
assert((m_pos_sav = z_pos) != NULL); assert((m_pos_sav = z_pos) != NULL);
} }
#endif #endif
#else #else
/* don't search for a longer/closer match */ /* don't search for a longer/closer match */
@ -177,12 +198,16 @@
goto match; goto match;
#endif #endif
} }
ip = ip_sav; ip = ip_sav;
} }
else else
ip = ip_sav; ip = ip_sav;
d++; d++;
} while (--j > 0); }
while (--j > 0);
assert(ip == ip_sav); assert(ip == ip_sav);
d -= DD_SIZE; d -= DD_SIZE;

View File

@ -48,6 +48,7 @@ try_match:
#endif #endif
m_pos_sav = m_pos; m_pos_sav = m_pos;
#endif #endif
if (m_pos[0] == ip[0] && m_pos[1] == ip[1] && m_pos[2] == ip[2]) if (m_pos[0] == ip[0] && m_pos[1] == ip[1] && m_pos[2] == ip[2])
{ {
m_pos += 3; m_pos += 3;
@ -62,19 +63,25 @@ try_match:
if (m_len > M2_MIN_LEN) if (m_len > M2_MIN_LEN)
goto match; goto match;
if (m_len == M2_MIN_LEN) if (m_len == M2_MIN_LEN)
{ {
#if (_MAX_OFFSET == _M2_MAX_OFFSET) #if (_MAX_OFFSET == _M2_MAX_OFFSET)
goto match; goto match;
#else #else
if (m_off <= M2_MAX_OFFSET) if (m_off <= M2_MAX_OFFSET)
goto match; goto match;
#if 0 && (M3_MIN_LEN == M2_MIN_LEN) #if 0 && (M3_MIN_LEN == M2_MIN_LEN)
if (ip == ii) if (ip == ii)
goto match; goto match;
#endif #endif
#endif #endif
} }
goto literal; goto literal;

View File

@ -58,6 +58,7 @@ static lzo_compress_t lzo1b_get_compress_func(int clevel)
else else
return (lzo_compress_t) 0; return (lzo_compress_t) 0;
} }
f = c_funcs[clevel - 1]; f = c_funcs[clevel - 1];
assert(f && *f); assert(f && *f);
return *f; return *f;
@ -73,8 +74,10 @@ lzo1b_compress ( const lzo_bytep src, lzo_uint src_len,
lzo_compress_t f; lzo_compress_t f;
f = lzo1b_get_compress_func(clevel); f = lzo1b_get_compress_func(clevel);
if (!f) if (!f)
return LZO_E_ERROR; return LZO_E_ERROR;
return _lzo1b_do_compress(src, src_len, dst, dst_len, wrkmem, f); return _lzo1b_do_compress(src, src_len, dst, dst_len, wrkmem, f);
} }

View File

@ -71,6 +71,7 @@ code_match ( LZO_COMPRESS_T *c, lzo_bytep op, lzo_uint m_len, lzo_uint m_off )
assert(m_off <= M3_MAX_OFFSET); assert(m_off <= M3_MAX_OFFSET);
m_off -= M3_MIN_OFFSET - M3_EOF_OFFSET; m_off -= M3_MIN_OFFSET - M3_EOF_OFFSET;
/* code match len */ /* code match len */
if (m_len <= M3_MAX_LEN) if (m_len <= M3_MAX_LEN)
*op++ = LZO_BYTE(M3_MARKER | (m_len - (M3_MIN_LEN - 1))); *op++ = LZO_BYTE(M3_MARKER | (m_len - (M3_MIN_LEN - 1)));
@ -81,14 +82,17 @@ code_match ( LZO_COMPRESS_T *c, lzo_bytep op, lzo_uint m_len, lzo_uint m_off )
*op++ = M4_MARKER; *op++ = M4_MARKER;
/* code match len */ /* code match len */
m_len -= M4_MIN_LEN - 1; m_len -= M4_MIN_LEN - 1;
while (m_len > 255) while (m_len > 255)
{ {
m_len -= 255; m_len -= 255;
*op++ = 0; *op++ = 0;
} }
assert(m_len > 0); assert(m_len > 0);
*op++ = LZO_BYTE(m_len); *op++ = LZO_BYTE(m_len);
} }
/* code low offset bits */ /* code low offset bits */
*op++ = LZO_BYTE(m_off & M3O_MASK); *op++ = LZO_BYTE(m_off & M3O_MASK);
/* code high offset bits */ /* code high offset bits */
@ -98,6 +102,7 @@ code_match ( LZO_COMPRESS_T *c, lzo_bytep op, lzo_uint m_len, lzo_uint m_off )
c->m3 = op; c->m3 = op;
c->m3_m++; c->m3_m++;
} }
return op; return op;
} }
@ -145,14 +150,18 @@ lzo1c_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
c->m3 = out + 1; /* pointer after last m3/m4 match */ c->m3 = out + 1; /* pointer after last m3/m4 match */
r = init_match(c, swd, NULL, 0, 0); r = init_match(c, swd, NULL, 0, 0);
if (r != 0) if (r != 0)
return r; return r;
if (max_chain > 0) if (max_chain > 0)
swd->max_chain = max_chain; swd->max_chain = max_chain;
r = find_match(c, swd, 0, 0); r = find_match(c, swd, 0, 0);
if (r != 0) if (r != 0)
return r; return r;
while (c->look > 0) while (c->look > 0)
{ {
int lazy_match_min_gain = -1; int lazy_match_min_gain = -1;
@ -167,8 +176,10 @@ lzo1c_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
#endif #endif
assert(c->ip - c->look >= in); assert(c->ip - c->look >= in);
if (lit == 0) if (lit == 0)
ii = c->ip - c->look; ii = c->ip - c->look;
assert(ii + lit == c->ip - c->look); assert(ii + lit == c->ip - c->look);
assert(swd->b_char == *(c->ip - c->look)); assert(swd->b_char == *(c->ip - c->look));
@ -198,20 +209,24 @@ lzo1c_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
lazy_match_min_gain = 1; lazy_match_min_gain = 1;
#if (M2_MIN_LEN == 2) #if (M2_MIN_LEN == 2)
if (m_len == 2) if (m_len == 2)
{ {
/* don't code a match of len 2 if we have to /* don't code a match of len 2 if we have to
code a literal run. Code a literal instead. */ code a literal run. Code a literal instead. */
m_len = 0; m_len = 0;
} }
#endif #endif
#if (M2_MIN_LEN == M3_MIN_LEN) #if (M2_MIN_LEN == M3_MIN_LEN)
if (m_len == M2_MIN_LEN && m_off > M2_MAX_OFFSET) if (m_len == M2_MIN_LEN && m_off > M2_MAX_OFFSET)
{ {
/* don't code a M3 match of len 3 if we have to /* don't code a M3 match of len 3 if we have to
code a literal run. Code a literal instead. */ code a literal run. Code a literal instead. */
m_len = 0; m_len = 0;
} }
#endif #endif
} }
else else
@ -229,12 +244,14 @@ lzo1c_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
/* try a lazy match */ /* try a lazy match */
if (m_len == 0) if (m_len == 0)
lazy_match_min_gain = -1; lazy_match_min_gain = -1;
if (lazy_match_min_gain >= 0 && c->look > m_len) if (lazy_match_min_gain >= 0 && c->look > m_len)
{ {
assert(m_len > 0); assert(m_len > 0);
r = find_match(c, swd, 1, 0); r = find_match(c, swd, 1, 0);
assert(r == 0); LZO_UNUSED(r); assert(r == 0);
LZO_UNUSED(r);
assert(c->look > 0); assert(c->look > 0);
if (m_len <= M2_MAX_LEN && m_off <= M2_MAX_OFFSET && if (m_len <= M2_MAX_LEN && m_off <= M2_MAX_OFFSET &&
@ -259,8 +276,10 @@ lzo1c_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
ahead = 1; ahead = 1;
assert(ii + lit + 1 == c->ip - c->look); assert(ii + lit + 1 == c->ip - c->look);
} }
assert(m_len > 0); assert(m_len > 0);
} }
assert(ii + lit + ahead == c->ip - c->look); assert(ii + lit + ahead == c->ip - c->look);
@ -269,7 +288,8 @@ lzo1c_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
/* a literal */ /* a literal */
lit++; lit++;
r = find_match(c, swd, 1, 0); r = find_match(c, swd, 1, 0);
assert(r == 0); LZO_UNUSED(r); assert(r == 0);
LZO_UNUSED(r);
} }
else else
{ {
@ -300,10 +320,12 @@ lzo1c_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
{ {
op = STORE_RUN(op, ii, lit); op = STORE_RUN(op, ii, lit);
} }
if (lit < R0FAST) if (lit < R0FAST)
c->r1_m_len = m_len; c->r1_m_len = m_len;
else else
c->r1_m_len = 0; c->r1_m_len = 0;
lit = 0; lit = 0;
} }
else else
@ -312,7 +334,8 @@ lzo1c_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
/* 2 - code match */ /* 2 - code match */
op = code_match(c, op, m_len, m_off); op = code_match(c, op, m_len, m_off);
r = find_match(c, swd, m_len, 1 + ahead); r = find_match(c, swd, m_len, 1 + ahead);
assert(r == 0); LZO_UNUSED(r); assert(r == 0);
LZO_UNUSED(r);
} }
c->codesize = pd(op, out); c->codesize = pd(op, out);

View File

@ -74,9 +74,12 @@ _lzo1c_do_compress ( const lzo_bytep in, lzo_uint in_len,
#if defined(LZO_EOF_CODE) #if defined(LZO_EOF_CODE)
#if defined(LZO_TEST_COMPRESS_OVERRUN) #if defined(LZO_TEST_COMPRESS_OVERRUN)
if (r == LZO_E_OK && avail_out - *out_len < 3) if (r == LZO_E_OK && avail_out - *out_len < 3)
r = LZO_E_COMPRESS_OVERRUN; r = LZO_E_COMPRESS_OVERRUN;
#endif #endif
if (r == LZO_E_OK) if (r == LZO_E_OK)
{ {
lzo_bytep op = out + *out_len; lzo_bytep op = out + *out_len;
@ -85,6 +88,7 @@ _lzo1c_do_compress ( const lzo_bytep in, lzo_uint in_len,
op[2] = 0; op[2] = 0;
*out_len += 3; *out_len += 3;
} }
#endif #endif

View File

@ -58,6 +58,7 @@ static lzo_compress_t lzo1c_get_compress_func(int clevel)
else else
return (lzo_compress_t) 0; return (lzo_compress_t) 0;
} }
f = c_funcs[clevel - 1]; f = c_funcs[clevel - 1];
assert(f && *f); assert(f && *f);
return *f; return *f;
@ -73,8 +74,10 @@ lzo1c_compress ( const lzo_bytep src, lzo_uint src_len,
lzo_compress_t f; lzo_compress_t f;
f = lzo1c_get_compress_func(clevel); f = lzo1c_get_compress_func(clevel);
if (!f) if (!f)
return LZO_E_ERROR; return LZO_E_ERROR;
return _lzo1c_do_compress(src, src_len, dst, dst_len, wrkmem, f); return _lzo1c_do_compress(src, src_len, dst, dst_len, wrkmem, f);
} }

View File

@ -69,6 +69,7 @@ int do_compress ( const lzo_bytep in , lzo_uint in_len,
ii = ip; ii = ip;
ip++; ip++;
for (;;) for (;;)
{ {
const lzo_bytep m_pos; const lzo_bytep m_pos;
@ -79,23 +80,31 @@ int do_compress ( const lzo_bytep in , lzo_uint in_len,
DINDEX1(dindex, ip); DINDEX1(dindex, ip);
GINDEX(m_pos, m_off, dict, dindex, in); GINDEX(m_pos, m_off, dict, dindex, in);
if (LZO_CHECK_MPOS_NON_DET(m_pos, m_off, in, ip, M3_MAX_OFFSET)) if (LZO_CHECK_MPOS_NON_DET(m_pos, m_off, in, ip, M3_MAX_OFFSET))
goto literal; goto literal;
#if 1 #if 1
if (m_off <= M2_MAX_OFFSET || m_pos[3] == ip[3]) if (m_off <= M2_MAX_OFFSET || m_pos[3] == ip[3])
goto try_match; goto try_match;
DINDEX2(dindex, ip); DINDEX2(dindex, ip);
#endif #endif
GINDEX(m_pos, m_off, dict, dindex, in); GINDEX(m_pos, m_off, dict, dindex, in);
if (LZO_CHECK_MPOS_NON_DET(m_pos, m_off, in, ip, M3_MAX_OFFSET)) if (LZO_CHECK_MPOS_NON_DET(m_pos, m_off, in, ip, M3_MAX_OFFSET))
goto literal; goto literal;
if (m_off <= M2_MAX_OFFSET || m_pos[3] == ip[3]) if (m_off <= M2_MAX_OFFSET || m_pos[3] == ip[3])
goto try_match; goto try_match;
goto literal; goto literal;
try_match: try_match:
#if 0 && (LZO_OPT_UNALIGNED16) #if 0 && (LZO_OPT_UNALIGNED16)
if (UA_GET_NE16(m_pos) != UA_GET_NE16(ip)) if (UA_GET_NE16(m_pos) != UA_GET_NE16(ip))
#else #else
if (m_pos[0] != ip[0] || m_pos[1] != ip[1]) if (m_pos[0] != ip[0] || m_pos[1] != ip[1])
@ -108,16 +117,23 @@ try_match:
{ {
m_pos += 3; m_pos += 3;
#if 0 #if 0
if (m_off <= M2_MAX_OFFSET) if (m_off <= M2_MAX_OFFSET)
goto match; goto match;
if (lit <= 3) if (lit <= 3)
goto match; goto match;
if (lit == 3) /* better compression, but slower */ if (lit == 3) /* better compression, but slower */
{ {
assert(op - 2 > out); op[-2] |= LZO_BYTE(3); assert(op - 2 > out);
*op++ = *ii++; *op++ = *ii++; *op++ = *ii++; op[-2] |= LZO_BYTE(3);
*op++ = *ii++;
*op++ = *ii++;
*op++ = *ii++;
goto code_match; goto code_match;
} }
if (*m_pos == ip[3]) if (*m_pos == ip[3])
#endif #endif
goto match; goto match;
@ -128,8 +144,10 @@ try_match:
/* a literal */ /* a literal */
literal: literal:
UPDATE_I(dict, 0, dindex, ip, in); UPDATE_I(dict, 0, dindex, ip, in);
if (++ip >= ip_end) if (++ip >= ip_end)
break; break;
continue; continue;
@ -138,6 +156,7 @@ match:
UPDATE_I(dict, 0, dindex, ip, in); UPDATE_I(dict, 0, dindex, ip, in);
/* store current literal run */ /* store current literal run */
lit = pd(ip, ii); lit = pd(ip, ii);
if (lit > 0) if (lit > 0)
{ {
lzo_uint t = lit; lzo_uint t = lit;
@ -151,28 +170,36 @@ match:
lzo_uint tt = t - 31; lzo_uint tt = t - 31;
*op++ = 0; *op++ = 0;
while (tt > 255) while (tt > 255)
{ {
tt -= 255; tt -= 255;
UA_SET1(op, 0); UA_SET1(op, 0);
op++; op++;
} }
assert(tt > 0); assert(tt > 0);
*op++ = LZO_BYTE(tt); *op++ = LZO_BYTE(tt);
} }
do *op++ = *ii++; while (--t > 0);
do* op++ = *ii++;
while (--t > 0);
} }
assert(ii == ip); assert(ii == ip);
/* code the match */ /* code the match */
ip += 3; ip += 3;
if (*m_pos++ != *ip++ || *m_pos++ != *ip++ || *m_pos++ != *ip++ || if (*m_pos++ != *ip++ || *m_pos++ != *ip++ || *m_pos++ != *ip++ ||
*m_pos++ != *ip++ || *m_pos++ != *ip++ || *m_pos++ != *ip++) *m_pos++ != *ip++ || *m_pos++ != *ip++ || *m_pos++ != *ip++)
{ {
--ip; --ip;
m_len = pd(ip, ii); m_len = pd(ip, ii);
assert(m_len >= 3); assert(m_len <= 8); assert(m_len >= 3);
assert(m_len <= 8);
if (m_off <= M2_MAX_OFFSET) if (m_off <= M2_MAX_OFFSET)
{ {
@ -199,8 +226,10 @@ match:
{ {
const lzo_bytep end; const lzo_bytep end;
end = in_end; end = in_end;
while (ip < end && *m_pos == *ip) while (ip < end && *m_pos == *ip)
m_pos++, ip++; m_pos++, ip++;
m_len = pd(ip, ii); m_len = pd(ip, ii);
} }
assert(m_len >= 3); assert(m_len >= 3);
@ -211,20 +240,24 @@ match:
{ {
m_len -= 33; m_len -= 33;
*op++ = M3_MARKER | 0; *op++ = M3_MARKER | 0;
while (m_len > 255) while (m_len > 255)
{ {
m_len -= 255; m_len -= 255;
UA_SET1(op, 0); UA_SET1(op, 0);
op++; op++;
} }
assert(m_len > 0); assert(m_len > 0);
*op++ = LZO_BYTE(m_len); *op++ = LZO_BYTE(m_len);
} }
*op++ = LZO_BYTE((m_off & 63) << 2); *op++ = LZO_BYTE((m_off & 63) << 2);
*op++ = LZO_BYTE(m_off >> 6); *op++ = LZO_BYTE(m_off >> 6);
} }
ii = ip; ii = ip;
if (ip >= ip_end) if (ip >= ip_end)
break; break;
} }
@ -244,15 +277,18 @@ match:
lzo_uint tt = t - 31; lzo_uint tt = t - 31;
*op++ = 0; *op++ = 0;
while (tt > 255) while (tt > 255)
{ {
tt -= 255; tt -= 255;
UA_SET1(op, 0); UA_SET1(op, 0);
op++; op++;
} }
assert(tt > 0); assert(tt > 0);
*op++ = LZO_BYTE(tt); *op++ = LZO_BYTE(tt);
} }
UA_COPYN(op, ii, t); UA_COPYN(op, ii, t);
op += t; op += t;
} }
@ -279,7 +315,11 @@ lzo1f_1_compress ( const lzo_bytep in , lzo_uint in_len,
else if (in_len <= 10) else if (in_len <= 10)
{ {
*op++ = LZO_BYTE(in_len); *op++ = LZO_BYTE(in_len);
do *op++ = *in++; while (--in_len > 0);
do* op++ = *in++;
while (--in_len > 0);
*out_len = pd(op, out); *out_len = pd(op, out);
} }
else else

View File

@ -76,14 +76,17 @@ code_match ( LZO_COMPRESS_T *c, lzo_bytep op, lzo_uint m_len, lzo_uint m_off )
{ {
m_len -= M3_MAX_LEN; m_len -= M3_MAX_LEN;
*op++ = M3_MARKER | 0; *op++ = M3_MARKER | 0;
while (m_len > 255) while (m_len > 255)
{ {
m_len -= 255; m_len -= 255;
*op++ = 0; *op++ = 0;
} }
assert(m_len > 0); assert(m_len > 0);
*op++ = LZO_BYTE(m_len); *op++ = LZO_BYTE(m_len);
} }
*op++ = LZO_BYTE((m_off & 63) << 2); *op++ = LZO_BYTE((m_off & 63) << 2);
*op++ = LZO_BYTE(m_off >> 6); *op++ = LZO_BYTE(m_off >> 6);
c->m3_m++; c->m3_m++;
@ -105,15 +108,20 @@ STORE_RUN ( lzo_bytep op, const lzo_bytep ii, lzo_uint t, lzo_bytep out )
lzo_uint tt = t - 31; lzo_uint tt = t - 31;
*op++ = 0; *op++ = 0;
while (tt > 255) while (tt > 255)
{ {
tt -= 255; tt -= 255;
*op++ = 0; *op++ = 0;
} }
assert(tt > 0); assert(tt > 0);
*op++ = LZO_BYTE(tt); *op++ = LZO_BYTE(tt);
} }
do *op++ = *ii++; while (--t > 0);
do* op++ = *ii++;
while (--t > 0);
return op; return op;
} }
@ -161,14 +169,18 @@ lzo1f_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
c->r1_lit = c->r1_m_len = 0; c->r1_lit = c->r1_m_len = 0;
r = init_match(c, swd, NULL, 0, 0); r = init_match(c, swd, NULL, 0, 0);
if (r != 0) if (r != 0)
return r; return r;
if (max_chain > 0) if (max_chain > 0)
swd->max_chain = max_chain; swd->max_chain = max_chain;
r = find_match(c, swd, 0, 0); r = find_match(c, swd, 0, 0);
if (r != 0) if (r != 0)
return r; return r;
while (c->look > 0) while (c->look > 0)
{ {
int lazy_match_min_gain = -1; int lazy_match_min_gain = -1;
@ -178,8 +190,10 @@ lzo1f_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
m_off = c->m_off; m_off = c->m_off;
assert(c->ip - c->look >= in); assert(c->ip - c->look >= in);
if (lit == 0) if (lit == 0)
ii = c->ip - c->look; ii = c->ip - c->look;
assert(ii + lit == c->ip - c->look); assert(ii + lit == c->ip - c->look);
assert(swd->b_char == *(c->ip - c->look)); assert(swd->b_char == *(c->ip - c->look));
@ -209,7 +223,8 @@ lzo1f_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
if (m_len > 0 && lazy_match_min_gain >= 0 && c->look > m_len) if (m_len > 0 && lazy_match_min_gain >= 0 && c->look > m_len)
{ {
r = find_match(c, swd, 1, 0); r = find_match(c, swd, 1, 0);
assert(r == 0); LZO_UNUSED(r); assert(r == 0);
LZO_UNUSED(r);
assert(c->look > 0); assert(c->look > 0);
if (m_len <= M2_MAX_LEN && m_off <= M2_MAX_OFFSET && if (m_len <= M2_MAX_LEN && m_off <= M2_MAX_OFFSET &&
@ -250,8 +265,10 @@ lzo1f_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
ahead = 1; ahead = 1;
assert(ii + lit + 1 == c->ip - c->look); assert(ii + lit + 1 == c->ip - c->look);
} }
assert(m_len > 0); assert(m_len > 0);
} }
assert(ii + lit + ahead == c->ip - c->look); assert(ii + lit + ahead == c->ip - c->look);
@ -260,7 +277,8 @@ lzo1f_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
/* a literal */ /* a literal */
lit++; lit++;
r = find_match(c, swd, 1, 0); r = find_match(c, swd, 1, 0);
assert(r == 0); LZO_UNUSED(r); assert(r == 0);
LZO_UNUSED(r);
} }
else else
{ {
@ -278,7 +296,8 @@ lzo1f_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
/* 2 - code match */ /* 2 - code match */
op = code_match(c, op, m_len, m_off); op = code_match(c, op, m_len, m_off);
r = find_match(c, swd, m_len, 1 + ahead); r = find_match(c, swd, m_len, 1 + ahead);
assert(r == 0); LZO_UNUSED(r); assert(r == 0);
LZO_UNUSED(r);
} }
c->codesize = pd(op, out); c->codesize = pd(op, out);

View File

@ -58,6 +58,7 @@ DO_DECOMPRESS ( const lzo_bytep in , lzo_uint in_len,
while (TEST_IP_AND_TEST_OP) while (TEST_IP_AND_TEST_OP)
{ {
t = *ip++; t = *ip++;
if (t > 31) if (t > 31)
goto match; goto match;
@ -65,6 +66,7 @@ DO_DECOMPRESS ( const lzo_bytep in , lzo_uint in_len,
if (t == 0) if (t == 0)
{ {
NEED_IP(1); NEED_IP(1);
while (*ip == 0) while (*ip == 0)
{ {
t += 255; t += 255;
@ -72,22 +74,36 @@ DO_DECOMPRESS ( const lzo_bytep in , lzo_uint in_len,
TEST_IV(t); TEST_IV(t);
NEED_IP(1); NEED_IP(1);
} }
t += 31 + *ip++; t += 31 + *ip++;
} }
/* copy literals */ /* copy literals */
assert(t > 0); NEED_OP(t); NEED_IP(t+1); assert(t > 0);
NEED_OP(t);
NEED_IP(t + 1);
#if (LZO_OPT_UNALIGNED32) #if (LZO_OPT_UNALIGNED32)
if (t >= 4) if (t >= 4)
{ {
do { do
{
UA_COPY4(op, ip); UA_COPY4(op, ip);
op += 4; ip += 4; t -= 4; op += 4;
} while (t >= 4); ip += 4;
if (t > 0) do *op++ = *ip++; while (--t > 0); t -= 4;
}
while (t >= 4);
if (t > 0) do* op++ = *ip++;
while (--t > 0);
} }
else else
#endif #endif
do *op++ = *ip++; while (--t > 0); do* op++ = *ip++;
while (--t > 0);
t = *ip++; t = *ip++;
@ -99,27 +115,35 @@ DO_DECOMPRESS ( const lzo_bytep in , lzo_uint in_len,
m_pos = op - 1 - 0x800; m_pos = op - 1 - 0x800;
m_pos -= (t >> 2) & 7; m_pos -= (t >> 2) & 7;
m_pos -= *ip++ << 3; m_pos -= *ip++ << 3;
TEST_LB(m_pos); NEED_OP(3); TEST_LB(m_pos);
*op++ = *m_pos++; *op++ = *m_pos++; *op++ = *m_pos++; NEED_OP(3);
*op++ = *m_pos++;
*op++ = *m_pos++;
*op++ = *m_pos++;
} }
else else
{ {
match: match:
if (t < M3_MARKER) if (t < M3_MARKER)
{ {
m_pos = op - 1; m_pos = op - 1;
m_pos -= (t >> 2) & 7; m_pos -= (t >> 2) & 7;
m_pos -= *ip++ << 3; m_pos -= *ip++ << 3;
t >>= 5; t >>= 5;
TEST_LB(m_pos); assert(t > 0); NEED_OP(t+3-1); TEST_LB(m_pos);
assert(t > 0);
NEED_OP(t + 3 - 1);
goto copy_match; goto copy_match;
} }
else else
{ {
t &= 31; t &= 31;
if (t == 0) if (t == 0)
{ {
NEED_IP(1); NEED_IP(1);
while (*ip == 0) while (*ip == 0)
{ {
t += 255; t += 255;
@ -127,8 +151,10 @@ match:
TEST_OV(t); TEST_OV(t);
NEED_IP(1); NEED_IP(1);
} }
t += 31 + *ip++; t += 31 + *ip++;
} }
NEED_IP(2); NEED_IP(2);
m_pos = op; m_pos = op;
#if (LZO_OPT_UNALIGNED16) && (LZO_ABI_LITTLE_ENDIAN) #if (LZO_OPT_UNALIGNED16) && (LZO_ABI_LITTLE_ENDIAN)
@ -138,38 +164,64 @@ match:
m_pos -= *ip++ >> 2; m_pos -= *ip++ >> 2;
m_pos -= *ip++ << 6; m_pos -= *ip++ << 6;
#endif #endif
if (m_pos == op) if (m_pos == op)
goto eof_found; goto eof_found;
} }
/* copy match */ /* copy match */
TEST_LB(m_pos); assert(t > 0); NEED_OP(t+3-1); TEST_LB(m_pos);
assert(t > 0);
NEED_OP(t + 3 - 1);
#if (LZO_OPT_UNALIGNED32) #if (LZO_OPT_UNALIGNED32)
if (t >= 2 * 4 - (3 - 1) && (op - m_pos) >= 4) if (t >= 2 * 4 - (3 - 1) && (op - m_pos) >= 4)
{ {
UA_COPY4(op, m_pos); UA_COPY4(op, m_pos);
op += 4; m_pos += 4; t -= 4 - (3 - 1); op += 4;
do { m_pos += 4;
t -= 4 - (3 - 1);
do
{
UA_COPY4(op, m_pos); UA_COPY4(op, m_pos);
op += 4; m_pos += 4; t -= 4; op += 4;
} while (t >= 4); m_pos += 4;
if (t > 0) do *op++ = *m_pos++; while (--t > 0); t -= 4;
}
while (t >= 4);
if (t > 0) do* op++ = *m_pos++;
while (--t > 0);
} }
else else
#endif #endif
{ {
copy_match: copy_match:
*op++ = *m_pos++; *op++ = *m_pos++; *op++ = *m_pos++;
do *op++ = *m_pos++; while (--t > 0); *op++ = *m_pos++;
do* op++ = *m_pos++;
while (--t > 0);
} }
} }
t = ip[-2] & 3; t = ip[-2] & 3;
if (t == 0) if (t == 0)
break; break;
/* copy literals */ /* copy literals */
assert(t > 0); NEED_OP(t); NEED_IP(t+1); assert(t > 0);
do *op++ = *ip++; while (--t > 0); NEED_OP(t);
NEED_IP(t + 1);
do* op++ = *ip++;
while (--t > 0);
t = *ip++; t = *ip++;
} }
} }

View File

@ -140,10 +140,12 @@ code_match ( LZO_COMPRESS_T *c, lzo_bytep op, lzo_uint m_len, lzo_uint m_off )
#endif #endif
assert(op > c->out); assert(op > c->out);
if (m_len == 2) if (m_len == 2)
{ {
assert(m_off <= M1_MAX_OFFSET); assert(m_off <= M1_MAX_OFFSET);
assert(c->r1_lit > 0); assert(c->r1_lit < 4); assert(c->r1_lit > 0);
assert(c->r1_lit < 4);
m_off -= 1; m_off -= 1;
#if defined(LZO1Z) #if defined(LZO1Z)
*op++ = LZO_BYTE(M1_MARKER | (m_off >> 6)); *op++ = LZO_BYTE(M1_MARKER | (m_off >> 6));
@ -154,6 +156,7 @@ code_match ( LZO_COMPRESS_T *c, lzo_bytep op, lzo_uint m_len, lzo_uint m_off )
#endif #endif
c->m1a_m++; c->m1a_m++;
} }
#if defined(LZO1Z) #if defined(LZO1Z)
else if (m_len <= M2_MAX_LEN && (m_off <= M2_MAX_OFFSET || m_off == c->last_m_off)) else if (m_len <= M2_MAX_LEN && (m_off <= M2_MAX_OFFSET || m_off == c->last_m_off))
#else #else
@ -172,6 +175,7 @@ code_match ( LZO_COMPRESS_T *c, lzo_bytep op, lzo_uint m_len, lzo_uint m_off )
*op++ = LZO_BYTE(m_off >> 2); *op++ = LZO_BYTE(m_off >> 2);
assert(op[-2] >= M2_MARKER); assert(op[-2] >= M2_MARKER);
#elif defined(LZO1Z) #elif defined(LZO1Z)
if (m_off == c->last_m_off) if (m_off == c->last_m_off)
*op++ = LZO_BYTE(((m_len - 1) << 5) | (0x700 >> 6)); *op++ = LZO_BYTE(((m_len - 1) << 5) | (0x700 >> 6));
else else
@ -180,6 +184,7 @@ code_match ( LZO_COMPRESS_T *c, lzo_bytep op, lzo_uint m_len, lzo_uint m_off )
*op++ = LZO_BYTE(((m_len - 1) << 5) | (m_off >> 6)); *op++ = LZO_BYTE(((m_len - 1) << 5) | (m_off >> 6));
*op++ = LZO_BYTE(m_off << 2); *op++ = LZO_BYTE(m_off << 2);
} }
#endif #endif
c->m2_m++; c->m2_m++;
} }
@ -201,20 +206,24 @@ code_match ( LZO_COMPRESS_T *c, lzo_bytep op, lzo_uint m_len, lzo_uint m_off )
{ {
assert(m_len >= 3); assert(m_len >= 3);
m_off -= 1; m_off -= 1;
if (m_len <= M3_MAX_LEN) if (m_len <= M3_MAX_LEN)
*op++ = LZO_BYTE(M3_MARKER | (m_len - 2)); *op++ = LZO_BYTE(M3_MARKER | (m_len - 2));
else else
{ {
m_len -= M3_MAX_LEN; m_len -= M3_MAX_LEN;
*op++ = M3_MARKER | 0; *op++ = M3_MARKER | 0;
while (m_len > 255) while (m_len > 255)
{ {
m_len -= 255; m_len -= 255;
*op++ = 0; *op++ = 0;
} }
assert(m_len > 0); assert(m_len > 0);
*op++ = LZO_BYTE(m_len); *op++ = LZO_BYTE(m_len);
} }
#if defined(LZO1Z) #if defined(LZO1Z)
*op++ = LZO_BYTE(m_off >> 6); *op++ = LZO_BYTE(m_off >> 6);
*op++ = LZO_BYTE(m_off << 2); *op++ = LZO_BYTE(m_off << 2);
@ -229,23 +238,28 @@ code_match ( LZO_COMPRESS_T *c, lzo_bytep op, lzo_uint m_len, lzo_uint m_off )
lzo_uint k; lzo_uint k;
assert(m_len >= 3); assert(m_len >= 3);
assert(m_off > 0x4000); assert(m_off <= 0xbfff); assert(m_off > 0x4000);
assert(m_off <= 0xbfff);
m_off -= 0x4000; m_off -= 0x4000;
k = (m_off & 0x4000) >> 11; k = (m_off & 0x4000) >> 11;
if (m_len <= M4_MAX_LEN) if (m_len <= M4_MAX_LEN)
*op++ = LZO_BYTE(M4_MARKER | k | (m_len - 2)); *op++ = LZO_BYTE(M4_MARKER | k | (m_len - 2));
else else
{ {
m_len -= M4_MAX_LEN; m_len -= M4_MAX_LEN;
*op++ = LZO_BYTE(M4_MARKER | k | 0); *op++ = LZO_BYTE(M4_MARKER | k | 0);
while (m_len > 255) while (m_len > 255)
{ {
m_len -= 255; m_len -= 255;
*op++ = 0; *op++ = 0;
} }
assert(m_len > 0); assert(m_len > 0);
*op++ = LZO_BYTE(m_len); *op++ = LZO_BYTE(m_len);
} }
#if defined(LZO1Z) #if defined(LZO1Z)
*op++ = LZO_BYTE(m_off >> 6); *op++ = LZO_BYTE(m_off >> 6);
*op++ = LZO_BYTE(m_off << 2); *op++ = LZO_BYTE(m_off << 2);
@ -290,16 +304,21 @@ STORE_RUN ( LZO_COMPRESS_T *c, lzo_bytep op, const lzo_bytep ii, lzo_uint t )
lzo_uint tt = t - 18; lzo_uint tt = t - 18;
*op++ = 0; *op++ = 0;
while (tt > 255) while (tt > 255)
{ {
tt -= 255; tt -= 255;
*op++ = 0; *op++ = 0;
} }
assert(tt > 0); assert(tt > 0);
*op++ = LZO_BYTE(tt); *op++ = LZO_BYTE(tt);
c->lit3_r++; c->lit3_r++;
} }
do *op++ = *ii++; while (--t > 0);
do* op++ = *ii++;
while (--t > 0);
return op; return op;
} }
@ -338,36 +357,48 @@ len_of_coded_match ( lzo_uint m_len, lzo_uint m_off, lzo_uint lit )
if (m_len < 2) if (m_len < 2)
return 0; return 0;
if (m_len == 2) if (m_len == 2)
return (m_off <= M1_MAX_OFFSET && lit > 0 && lit < 4) ? 2 : 0; return (m_off <= M1_MAX_OFFSET && lit > 0 && lit < 4) ? 2 : 0;
if (m_len <= M2_MAX_LEN && m_off <= M2_MAX_OFFSET) if (m_len <= M2_MAX_LEN && m_off <= M2_MAX_OFFSET)
return 2; return 2;
if (m_len == M2_MIN_LEN && m_off <= MX_MAX_OFFSET && lit >= 4) if (m_len == M2_MIN_LEN && m_off <= MX_MAX_OFFSET && lit >= 4)
return 2; return 2;
if (m_off <= M3_MAX_OFFSET) if (m_off <= M3_MAX_OFFSET)
{ {
if (m_len <= M3_MAX_LEN) if (m_len <= M3_MAX_LEN)
return 3; return 3;
m_len -= M3_MAX_LEN; m_len -= M3_MAX_LEN;
while (m_len > 255) while (m_len > 255)
{ {
m_len -= 255; m_len -= 255;
n++; n++;
} }
return n; return n;
} }
if (m_off <= M4_MAX_OFFSET) if (m_off <= M4_MAX_OFFSET)
{ {
if (m_len <= M4_MAX_LEN) if (m_len <= M4_MAX_LEN)
return 3; return 3;
m_len -= M4_MAX_LEN; m_len -= M4_MAX_LEN;
while (m_len > 255) while (m_len > 255)
{ {
m_len -= 255; m_len -= 255;
n++; n++;
} }
return n; return n;
} }
return 0; return 0;
} }
@ -381,8 +412,10 @@ min_gain(lzo_uint ahead, lzo_uint lit1, lzo_uint lit2, lzo_uint l1, lzo_uint l2,
lazy_match_min_gain = ahead; lazy_match_min_gain = ahead;
#if 0 #if 0
if (l3) if (l3)
lit2 -= ahead; lit2 -= ahead;
#endif #endif
if (lit1 <= 3) if (lit1 <= 3)
@ -391,6 +424,7 @@ min_gain(lzo_uint ahead, lzo_uint lit1, lzo_uint lit2, lzo_uint l1, lzo_uint l2,
lazy_match_min_gain += (lit2 <= 18) ? 0 : 1; lazy_match_min_gain += (lit2 <= 18) ? 0 : 1;
lazy_match_min_gain += (l2 - l1) * 2; lazy_match_min_gain += (l2 - l1) * 2;
if (l3) if (l3)
lazy_match_min_gain -= (ahead - l3) * 2; lazy_match_min_gain -= (ahead - l3) * 2;
@ -398,9 +432,11 @@ min_gain(lzo_uint ahead, lzo_uint lit1, lzo_uint lit2, lzo_uint l1, lzo_uint l2,
lazy_match_min_gain = 0; lazy_match_min_gain = 0;
#if 0 #if 0
if (l1 == 2) if (l1 == 2)
if (lazy_match_min_gain == 0) if (lazy_match_min_gain == 0)
lazy_match_min_gain = 1; lazy_match_min_gain = 1;
#endif #endif
return lazy_match_min_gain; return lazy_match_min_gain;
@ -419,6 +455,7 @@ void assert_match( const lzo_swd_p swd, lzo_uint m_len, lzo_uint m_off )
lzo_uint d_off; lzo_uint d_off;
assert(m_len >= 2); assert(m_len >= 2);
if (m_off <= (lzo_uint)(c->bp - c->in)) if (m_off <= (lzo_uint)(c->bp - c->in))
{ {
assert(c->bp - m_off + m_len < c->ip); assert(c->bp - m_off + m_len < c->ip);
@ -429,6 +466,7 @@ void assert_match( const lzo_swd_p swd, lzo_uint m_len, lzo_uint m_off )
assert(swd->dict != NULL); assert(swd->dict != NULL);
d_off = m_off - (lzo_uint)(c->bp - c->in); d_off = m_off - (lzo_uint)(c->bp - c->in);
assert(d_off <= swd->dict_len); assert(d_off <= swd->dict_len);
if (m_len > d_off) if (m_len > d_off)
{ {
assert(lzo_memcmp(c->bp, swd->dict_end - d_off, d_off) == 0); assert(lzo_memcmp(c->bp, swd->dict_end - d_off, d_off) == 0);
@ -457,10 +495,14 @@ better_match ( const lzo_swd_p swd, lzo_uint *m_len, lzo_uint *m_off )
if (*m_len <= M2_MIN_LEN) if (*m_len <= M2_MIN_LEN)
return; return;
#if defined(LZO1Z) #if defined(LZO1Z)
if (*m_off == c->last_m_off && *m_len <= M2_MAX_LEN) if (*m_off == c->last_m_off && *m_len <= M2_MAX_LEN)
return; return;
#if 1 #if 1
if (*m_len >= M2_MIN_LEN + 1 && *m_len <= M2_MAX_LEN + 1 && if (*m_len >= M2_MIN_LEN + 1 && *m_len <= M2_MAX_LEN + 1 &&
c->last_m_off && swd->best_off[*m_len - 1] == c->last_m_off) c->last_m_off && swd->best_off[*m_len - 1] == c->last_m_off)
{ {
@ -468,6 +510,7 @@ better_match ( const lzo_swd_p swd, lzo_uint *m_len, lzo_uint *m_off )
*m_off = swd->best_off[*m_len]; *m_off = swd->best_off[*m_len];
return; return;
} }
#endif #endif
#endif #endif
@ -475,6 +518,7 @@ better_match ( const lzo_swd_p swd, lzo_uint *m_len, lzo_uint *m_off )
return; return;
#if 1 #if 1
/* M3/M4 -> M2 */ /* M3/M4 -> M2 */
if (*m_off > M2_MAX_OFFSET && if (*m_off > M2_MAX_OFFSET &&
*m_len >= M2_MIN_LEN + 1 && *m_len <= M2_MAX_LEN + 1 && *m_len >= M2_MIN_LEN + 1 && *m_len <= M2_MAX_LEN + 1 &&
@ -484,9 +528,11 @@ better_match ( const lzo_swd_p swd, lzo_uint *m_len, lzo_uint *m_off )
*m_off = swd->best_off[*m_len]; *m_off = swd->best_off[*m_len];
return; return;
} }
#endif #endif
#if 1 #if 1
/* M4 -> M2 */ /* M4 -> M2 */
if (*m_off > M3_MAX_OFFSET && if (*m_off > M3_MAX_OFFSET &&
*m_len >= M4_MAX_LEN + 1 && *m_len <= M2_MAX_LEN + 2 && *m_len >= M4_MAX_LEN + 1 && *m_len <= M2_MAX_LEN + 2 &&
@ -496,9 +542,11 @@ better_match ( const lzo_swd_p swd, lzo_uint *m_len, lzo_uint *m_off )
*m_off = swd->best_off[*m_len]; *m_off = swd->best_off[*m_len];
return; return;
} }
#endif #endif
#if 1 #if 1
/* M4 -> M3 */ /* M4 -> M3 */
if (*m_off > M3_MAX_OFFSET && if (*m_off > M3_MAX_OFFSET &&
*m_len >= M4_MAX_LEN + 1 && *m_len <= M3_MAX_LEN + 1 && *m_len >= M4_MAX_LEN + 1 && *m_len <= M3_MAX_LEN + 1 &&
@ -507,6 +555,7 @@ better_match ( const lzo_swd_p swd, lzo_uint *m_len, lzo_uint *m_off )
*m_len = *m_len - 1; *m_len = *m_len - 1;
*m_off = swd->best_off[*m_len]; *m_off = swd->best_off[*m_len];
} }
#endif #endif
} }
@ -554,17 +603,22 @@ lzo1x_999_compress_internal ( const lzo_bytep in , lzo_uint in_len,
/* setup parameter defaults */ /* setup parameter defaults */
/* number of lazy match tries */ /* number of lazy match tries */
try_lazy = (lzo_uint) try_lazy_parm; try_lazy = (lzo_uint) try_lazy_parm;
if (try_lazy_parm < 0) if (try_lazy_parm < 0)
try_lazy = 1; try_lazy = 1;
/* reduce lazy match search if we already have a match with this length */ /* reduce lazy match search if we already have a match with this length */
if (good_length == 0) if (good_length == 0)
good_length = 32; good_length = 32;
/* do not try a lazy match if we already have a match with this length */ /* do not try a lazy match if we already have a match with this length */
if (max_lazy == 0) if (max_lazy == 0)
max_lazy = 32; max_lazy = 32;
/* stop searching for longer matches than this one */ /* stop searching for longer matches than this one */
if (nice_length == 0) if (nice_length == 0)
nice_length = 0; nice_length = 0;
/* don't search more positions than this */ /* don't search more positions than this */
if (max_chain == 0) if (max_chain == 0)
max_chain = SWD_MAX_CHAIN; max_chain = SWD_MAX_CHAIN;
@ -583,16 +637,21 @@ lzo1x_999_compress_internal ( const lzo_bytep in , lzo_uint in_len,
c->r1_lit = c->r1_m_len = 0; c->r1_lit = c->r1_m_len = 0;
r = init_match(c, swd, dict, dict_len, flags); r = init_match(c, swd, dict, dict_len, flags);
if (r != 0) if (r != 0)
return r; return r;
if (max_chain > 0) if (max_chain > 0)
swd->max_chain = max_chain; swd->max_chain = max_chain;
if (nice_length > 0) if (nice_length > 0)
swd->nice_length = nice_length; swd->nice_length = nice_length;
r = find_match(c, swd, 0, 0); r = find_match(c, swd, 0, 0);
if (r != 0) if (r != 0)
return r; return r;
while (c->look > 0) while (c->look > 0)
{ {
lzo_uint ahead; lzo_uint ahead;
@ -606,8 +665,10 @@ lzo1x_999_compress_internal ( const lzo_bytep in , lzo_uint in_len,
assert(c->bp == c->ip - c->look); assert(c->bp == c->ip - c->look);
assert(c->bp >= in); assert(c->bp >= in);
if (lit == 0) if (lit == 0)
ii = c->bp; ii = c->bp;
assert(ii + lit == c->bp); assert(ii + lit == c->bp);
assert(swd->b_char == *(c->bp)); assert(swd->b_char == *(c->bp));
@ -638,20 +699,24 @@ lzo1x_999_compress_internal ( const lzo_bytep in , lzo_uint in_len,
lit++; lit++;
swd->max_chain = max_chain; swd->max_chain = max_chain;
r = find_match(c, swd, 1, 0); r = find_match(c, swd, 1, 0);
assert(r == 0); LZO_UNUSED(r); assert(r == 0);
LZO_UNUSED(r);
continue; continue;
} }
/* a match */ /* a match */
#if defined(SWD_BEST_OFF) #if defined(SWD_BEST_OFF)
if (swd->use_best_off) if (swd->use_best_off)
better_match(swd, &m_len, &m_off); better_match(swd, &m_len, &m_off);
#endif #endif
assert_match(swd, m_len, m_off); assert_match(swd, m_len, m_off);
/* shall we try a lazy match ? */ /* shall we try a lazy match ? */
ahead = 0; ahead = 0;
if (try_lazy == 0 || m_len >= max_lazy) if (try_lazy == 0 || m_len >= max_lazy)
{ {
/* no */ /* no */
@ -679,34 +744,48 @@ lzo1x_999_compress_internal ( const lzo_bytep in , lzo_uint in_len,
swd->max_chain = max_chain >> 2; swd->max_chain = max_chain >> 2;
else else
swd->max_chain = max_chain; swd->max_chain = max_chain;
r = find_match(c, swd, 1, 0); r = find_match(c, swd, 1, 0);
ahead++; ahead++;
assert(r == 0); LZO_UNUSED(r); assert(r == 0);
LZO_UNUSED(r);
assert(c->look > 0); assert(c->look > 0);
assert(ii + lit + ahead == c->bp); assert(ii + lit + ahead == c->bp);
#if defined(LZO1Z) #if defined(LZO1Z)
if (m_off == c->last_m_off && c->m_off != c->last_m_off) if (m_off == c->last_m_off && c->m_off != c->last_m_off)
if (m_len >= M2_MIN_LEN && m_len <= M2_MAX_LEN) if (m_len >= M2_MIN_LEN && m_len <= M2_MAX_LEN)
c->m_len = 0; c->m_len = 0;
#endif #endif
if (c->m_len < m_len) if (c->m_len < m_len)
continue; continue;
#if 1 #if 1
if (c->m_len == m_len && c->m_off >= m_off) if (c->m_len == m_len && c->m_off >= m_off)
continue; continue;
#endif #endif
#if defined(SWD_BEST_OFF) #if defined(SWD_BEST_OFF)
if (swd->use_best_off) if (swd->use_best_off)
better_match(swd, &c->m_len, &c->m_off); better_match(swd, &c->m_len, &c->m_off);
#endif #endif
l2 = len_of_coded_match(c->m_len, c->m_off, lit + ahead); l2 = len_of_coded_match(c->m_len, c->m_off, lit + ahead);
if (l2 == 0) if (l2 == 0)
continue; continue;
#if 0 #if 0
if (c->m_len == m_len && l2 >= l1) if (c->m_len == m_len && l2 >= l1)
continue; continue;
#endif #endif
@ -718,6 +797,7 @@ lzo1x_999_compress_internal ( const lzo_bytep in , lzo_uint in_len,
#endif #endif
lazy_match_min_gain = min_gain(ahead, lit, lit + ahead, l1, l2, l3); lazy_match_min_gain = min_gain(ahead, lit, lit + ahead, l1, l2, l3);
if (c->m_len >= m_len + lazy_match_min_gain) if (c->m_len >= m_len + lazy_match_min_gain)
{ {
c->lazy++; c->lazy++;
@ -736,6 +816,7 @@ lzo1x_999_compress_internal ( const lzo_bytep in , lzo_uint in_len,
lit += ahead; lit += ahead;
assert(ii + lit == c->bp); assert(ii + lit == c->bp);
} }
goto lazy_match_done; goto lazy_match_done;
} }
} }
@ -751,9 +832,11 @@ lzo1x_999_compress_internal ( const lzo_bytep in , lzo_uint in_len,
op = code_match(c, op, m_len, m_off); op = code_match(c, op, m_len, m_off);
swd->max_chain = max_chain; swd->max_chain = max_chain;
r = find_match(c, swd, m_len, 1 + ahead); r = find_match(c, swd, m_len, 1 + ahead);
assert(r == 0); LZO_UNUSED(r); assert(r == 0);
LZO_UNUSED(r);
lazy_match_done: ; lazy_match_done:
;
} }
@ -807,7 +890,8 @@ lzo1x_999_compress_level ( const lzo_bytep in , lzo_uint in_len,
lzo_uint nice_length; lzo_uint nice_length;
lzo_uint max_chain; lzo_uint max_chain;
lzo_uint32_t flags; lzo_uint32_t flags;
} c[9] = { } c[9] =
{
/* faster compression */ /* faster compression */
{ 0, 0, 0, 8, 4, 0 }, { 0, 0, 0, 8, 4, 0 },
{ 0, 0, 0, 16, 8, 0 }, { 0, 0, 0, 16, 8, 0 },

View File

@ -54,6 +54,7 @@ do_compress ( const lzo_bytep in , lzo_uint in_len,
ii = ip; ii = ip;
ip += ti < 4 ? 4 - ti : 0; ip += ti < 4 ? 4 - ti : 0;
for (;;) for (;;)
{ {
const lzo_bytep m_pos; const lzo_bytep m_pos;
@ -62,26 +63,36 @@ do_compress ( const lzo_bytep in , lzo_uint in_len,
lzo_uint m_len; lzo_uint m_len;
lzo_uint dindex; lzo_uint dindex;
next: next:
if __lzo_unlikely(ip >= ip_end) if __lzo_unlikely(ip >= ip_end)
break; break;
DINDEX1(dindex, ip); DINDEX1(dindex, ip);
GINDEX(m_pos, m_off, dict, dindex, in); GINDEX(m_pos, m_off, dict, dindex, in);
if (LZO_CHECK_MPOS_NON_DET(m_pos, m_off, in, ip, M4_MAX_OFFSET)) if (LZO_CHECK_MPOS_NON_DET(m_pos, m_off, in, ip, M4_MAX_OFFSET))
goto literal; goto literal;
#if 1 #if 1
if (m_off <= M2_MAX_OFFSET || m_pos[3] == ip[3]) if (m_off <= M2_MAX_OFFSET || m_pos[3] == ip[3])
goto try_match; goto try_match;
DINDEX2(dindex, ip); DINDEX2(dindex, ip);
#endif #endif
GINDEX(m_pos, m_off, dict, dindex, in); GINDEX(m_pos, m_off, dict, dindex, in);
if (LZO_CHECK_MPOS_NON_DET(m_pos, m_off, in, ip, M4_MAX_OFFSET)) if (LZO_CHECK_MPOS_NON_DET(m_pos, m_off, in, ip, M4_MAX_OFFSET))
goto literal; goto literal;
if (m_off <= M2_MAX_OFFSET || m_pos[3] == ip[3]) if (m_off <= M2_MAX_OFFSET || m_pos[3] == ip[3])
goto try_match; goto try_match;
goto literal; goto literal;
try_match: try_match:
#if (LZO_OPT_UNALIGNED32) #if (LZO_OPT_UNALIGNED32)
if (UA_GET_NE32(m_pos) != UA_GET_NE32(ip)) if (UA_GET_NE32(m_pos) != UA_GET_NE32(ip))
#else #else
if (m_pos[0] != ip[0] || m_pos[1] != ip[1] || m_pos[2] != ip[2] || m_pos[3] != ip[3]) if (m_pos[0] != ip[0] || m_pos[1] != ip[1] || m_pos[2] != ip[2] || m_pos[3] != ip[3])
@ -93,6 +104,7 @@ literal:
ip += 1 + ((ip - ii) >> 5); ip += 1 + ((ip - ii) >> 5);
continue; continue;
} }
/*match:*/ /*match:*/
UPDATE_I(dict, 0, dindex, ip, in); UPDATE_I(dict, 0, dindex, ip, in);
#else #else
@ -104,12 +116,15 @@ literal:
literal: literal:
ip += 1 + ((ip - ii) >> 5); ip += 1 + ((ip - ii) >> 5);
next: next:
if __lzo_unlikely(ip >= ip_end) if __lzo_unlikely(ip >= ip_end)
break; break;
dv = UA_GET_LE32(ip); dv = UA_GET_LE32(ip);
dindex = DINDEX(dv, ip); dindex = DINDEX(dv, ip);
GINDEX(m_off, m_pos, in + dict, dindex, in); GINDEX(m_off, m_pos, in + dict, dindex, in);
UPDATE_I(dict, 0, dindex, ip, in); UPDATE_I(dict, 0, dindex, ip, in);
if __lzo_unlikely(dv != UA_GET_LE32(m_pos)) if __lzo_unlikely(dv != UA_GET_LE32(m_pos))
goto literal; goto literal;
} }
@ -117,9 +132,11 @@ next:
/* a match */ /* a match */
ii -= ti; ti = 0; ii -= ti;
ti = 0;
{ {
lzo_uint t = pd(ip, ii); lzo_uint t = pd(ip, ii);
if (t != 0) if (t != 0)
{ {
if (t <= 3) if (t <= 3)
@ -132,6 +149,7 @@ next:
{ do* op++ = *ii++; while (--t > 0); } { do* op++ = *ii++; while (--t > 0); }
#endif #endif
} }
#if (LZO_OPT_UNALIGNED32) || (LZO_OPT_UNALIGNED64) #if (LZO_OPT_UNALIGNED32) || (LZO_OPT_UNALIGNED64)
else if (t <= 16) else if (t <= 16)
{ {
@ -140,6 +158,7 @@ next:
UA_COPY8(op + 8, ii + 8); UA_COPY8(op + 8, ii + 8);
op += t; op += t;
} }
#endif #endif
else else
{ {
@ -149,21 +168,31 @@ next:
{ {
lzo_uint tt = t - 18; lzo_uint tt = t - 18;
*op++ = 0; *op++ = 0;
while __lzo_unlikely(tt > 255) while __lzo_unlikely(tt > 255)
{ {
tt -= 255; tt -= 255;
UA_SET1(op, 0); UA_SET1(op, 0);
op++; op++;
} }
assert(tt > 0); assert(tt > 0);
*op++ = LZO_BYTE(tt); *op++ = LZO_BYTE(tt);
} }
#if (LZO_OPT_UNALIGNED32) || (LZO_OPT_UNALIGNED64) #if (LZO_OPT_UNALIGNED32) || (LZO_OPT_UNALIGNED64)
do {
do
{
UA_COPY8(op, ii); UA_COPY8(op, ii);
UA_COPY8(op + 8, ii + 8); UA_COPY8(op + 8, ii + 8);
op += 16; ii += 16; t -= 16; op += 16;
} while (t >= 16); if (t > 0) ii += 16;
t -= 16;
}
while (t >= 16);
if (t > 0)
#endif #endif
{ do* op++ = *ii++; while (--t > 0); } { do* op++ = *ii++; while (--t > 0); }
} }
@ -174,102 +203,161 @@ next:
#if (LZO_OPT_UNALIGNED64) #if (LZO_OPT_UNALIGNED64)
lzo_uint64_t v; lzo_uint64_t v;
v = UA_GET_NE64(ip + m_len) ^ UA_GET_NE64(m_pos + m_len); v = UA_GET_NE64(ip + m_len) ^ UA_GET_NE64(m_pos + m_len);
if __lzo_unlikely(v == 0) {
do { if __lzo_unlikely(v == 0)
{
do
{
m_len += 8; m_len += 8;
v = UA_GET_NE64(ip + m_len) ^ UA_GET_NE64(m_pos + m_len); v = UA_GET_NE64(ip + m_len) ^ UA_GET_NE64(m_pos + m_len);
if __lzo_unlikely(ip + m_len >= ip_end) if __lzo_unlikely(ip + m_len >= ip_end)
goto m_len_done; goto m_len_done;
} while (v == 0);
} }
while (v == 0);
}
#if (LZO_ABI_BIG_ENDIAN) && defined(lzo_bitops_ctlz64) #if (LZO_ABI_BIG_ENDIAN) && defined(lzo_bitops_ctlz64)
m_len += lzo_bitops_ctlz64(v) / CHAR_BIT; m_len += lzo_bitops_ctlz64(v) / CHAR_BIT;
#elif (LZO_ABI_BIG_ENDIAN) #elif (LZO_ABI_BIG_ENDIAN)
if ((v >> (64 - CHAR_BIT)) == 0) do {
if ((v >> (64 - CHAR_BIT)) == 0) do
{
v <<= CHAR_BIT; v <<= CHAR_BIT;
m_len += 1; m_len += 1;
} while ((v >> (64 - CHAR_BIT)) == 0); }
while ((v >> (64 - CHAR_BIT)) == 0);
#elif (LZO_ABI_LITTLE_ENDIAN) && defined(lzo_bitops_cttz64) #elif (LZO_ABI_LITTLE_ENDIAN) && defined(lzo_bitops_cttz64)
m_len += lzo_bitops_cttz64(v) / CHAR_BIT; m_len += lzo_bitops_cttz64(v) / CHAR_BIT;
#elif (LZO_ABI_LITTLE_ENDIAN) #elif (LZO_ABI_LITTLE_ENDIAN)
if ((v & UCHAR_MAX) == 0) do {
if ((v & UCHAR_MAX) == 0) do
{
v >>= CHAR_BIT; v >>= CHAR_BIT;
m_len += 1; m_len += 1;
} while ((v & UCHAR_MAX) == 0); }
while ((v & UCHAR_MAX) == 0);
#else #else
if (ip[m_len] == m_pos[m_len]) do {
if (ip[m_len] == m_pos[m_len]) do
{
m_len += 1; m_len += 1;
} while (ip[m_len] == m_pos[m_len]); }
while (ip[m_len] == m_pos[m_len]);
#endif #endif
#elif (LZO_OPT_UNALIGNED32) #elif (LZO_OPT_UNALIGNED32)
lzo_uint32_t v; lzo_uint32_t v;
v = UA_GET_NE32(ip + m_len) ^ UA_GET_NE32(m_pos + m_len); v = UA_GET_NE32(ip + m_len) ^ UA_GET_NE32(m_pos + m_len);
if __lzo_unlikely(v == 0) {
do { if __lzo_unlikely(v == 0)
{
do
{
m_len += 4; m_len += 4;
v = UA_GET_NE32(ip + m_len) ^ UA_GET_NE32(m_pos + m_len); v = UA_GET_NE32(ip + m_len) ^ UA_GET_NE32(m_pos + m_len);
if (v != 0) if (v != 0)
break; break;
m_len += 4; m_len += 4;
v = UA_GET_NE32(ip + m_len) ^ UA_GET_NE32(m_pos + m_len); v = UA_GET_NE32(ip + m_len) ^ UA_GET_NE32(m_pos + m_len);
if __lzo_unlikely(ip + m_len >= ip_end) if __lzo_unlikely(ip + m_len >= ip_end)
goto m_len_done; goto m_len_done;
} while (v == 0);
} }
while (v == 0);
}
#if (LZO_ABI_BIG_ENDIAN) && defined(lzo_bitops_ctlz32) #if (LZO_ABI_BIG_ENDIAN) && defined(lzo_bitops_ctlz32)
m_len += lzo_bitops_ctlz32(v) / CHAR_BIT; m_len += lzo_bitops_ctlz32(v) / CHAR_BIT;
#elif (LZO_ABI_BIG_ENDIAN) #elif (LZO_ABI_BIG_ENDIAN)
if ((v >> (32 - CHAR_BIT)) == 0) do {
if ((v >> (32 - CHAR_BIT)) == 0) do
{
v <<= CHAR_BIT; v <<= CHAR_BIT;
m_len += 1; m_len += 1;
} while ((v >> (32 - CHAR_BIT)) == 0); }
while ((v >> (32 - CHAR_BIT)) == 0);
#elif (LZO_ABI_LITTLE_ENDIAN) && defined(lzo_bitops_cttz32) #elif (LZO_ABI_LITTLE_ENDIAN) && defined(lzo_bitops_cttz32)
m_len += lzo_bitops_cttz32(v) / CHAR_BIT; m_len += lzo_bitops_cttz32(v) / CHAR_BIT;
#elif (LZO_ABI_LITTLE_ENDIAN) #elif (LZO_ABI_LITTLE_ENDIAN)
if ((v & UCHAR_MAX) == 0) do {
if ((v & UCHAR_MAX) == 0) do
{
v >>= CHAR_BIT; v >>= CHAR_BIT;
m_len += 1; m_len += 1;
} while ((v & UCHAR_MAX) == 0); }
while ((v & UCHAR_MAX) == 0);
#else #else
if (ip[m_len] == m_pos[m_len]) do {
if (ip[m_len] == m_pos[m_len]) do
{
m_len += 1; m_len += 1;
} while (ip[m_len] == m_pos[m_len]); }
while (ip[m_len] == m_pos[m_len]);
#endif #endif
#else #else
if __lzo_unlikely(ip[m_len] == m_pos[m_len]) {
do { if __lzo_unlikely(ip[m_len] == m_pos[m_len])
{
do
{
m_len += 1; m_len += 1;
if (ip[m_len] != m_pos[m_len]) if (ip[m_len] != m_pos[m_len])
break; break;
m_len += 1; m_len += 1;
if (ip[m_len] != m_pos[m_len]) if (ip[m_len] != m_pos[m_len])
break; break;
m_len += 1; m_len += 1;
if (ip[m_len] != m_pos[m_len]) if (ip[m_len] != m_pos[m_len])
break; break;
m_len += 1; m_len += 1;
if (ip[m_len] != m_pos[m_len]) if (ip[m_len] != m_pos[m_len])
break; break;
m_len += 1; m_len += 1;
if (ip[m_len] != m_pos[m_len]) if (ip[m_len] != m_pos[m_len])
break; break;
m_len += 1; m_len += 1;
if (ip[m_len] != m_pos[m_len]) if (ip[m_len] != m_pos[m_len])
break; break;
m_len += 1; m_len += 1;
if (ip[m_len] != m_pos[m_len]) if (ip[m_len] != m_pos[m_len])
break; break;
m_len += 1; m_len += 1;
if __lzo_unlikely(ip + m_len >= ip_end) if __lzo_unlikely(ip + m_len >= ip_end)
goto m_len_done; goto m_len_done;
} while (ip[m_len] == m_pos[m_len]);
} }
while (ip[m_len] == m_pos[m_len]);
}
#endif #endif
} }
m_len_done: m_len_done:
m_off = pd(ip, m_pos); m_off = pd(ip, m_pos);
ip += m_len; ip += m_len;
ii = ip; ii = ip;
if (m_len <= M2_MAX_LEN && m_off <= M2_MAX_OFFSET) if (m_len <= M2_MAX_LEN && m_off <= M2_MAX_OFFSET)
{ {
m_off -= 1; m_off -= 1;
@ -284,43 +372,52 @@ m_len_done:
else if (m_off <= M3_MAX_OFFSET) else if (m_off <= M3_MAX_OFFSET)
{ {
m_off -= 1; m_off -= 1;
if (m_len <= M3_MAX_LEN) if (m_len <= M3_MAX_LEN)
*op++ = LZO_BYTE(M3_MARKER | (m_len - 2)); *op++ = LZO_BYTE(M3_MARKER | (m_len - 2));
else else
{ {
m_len -= M3_MAX_LEN; m_len -= M3_MAX_LEN;
*op++ = M3_MARKER | 0; *op++ = M3_MARKER | 0;
while __lzo_unlikely(m_len > 255) while __lzo_unlikely(m_len > 255)
{ {
m_len -= 255; m_len -= 255;
UA_SET1(op, 0); UA_SET1(op, 0);
op++; op++;
} }
*op++ = LZO_BYTE(m_len); *op++ = LZO_BYTE(m_len);
} }
*op++ = LZO_BYTE(m_off << 2); *op++ = LZO_BYTE(m_off << 2);
*op++ = LZO_BYTE(m_off >> 6); *op++ = LZO_BYTE(m_off >> 6);
} }
else else
{ {
m_off -= 0x4000; m_off -= 0x4000;
if (m_len <= M4_MAX_LEN) if (m_len <= M4_MAX_LEN)
*op++ = LZO_BYTE(M4_MARKER | ((m_off >> 11) & 8) | (m_len - 2)); *op++ = LZO_BYTE(M4_MARKER | ((m_off >> 11) & 8) | (m_len - 2));
else else
{ {
m_len -= M4_MAX_LEN; m_len -= M4_MAX_LEN;
*op++ = LZO_BYTE(M4_MARKER | ((m_off >> 11) & 8)); *op++ = LZO_BYTE(M4_MARKER | ((m_off >> 11) & 8));
while __lzo_unlikely(m_len > 255) while __lzo_unlikely(m_len > 255)
{ {
m_len -= 255; m_len -= 255;
UA_SET1(op, 0); UA_SET1(op, 0);
op++; op++;
} }
*op++ = LZO_BYTE(m_len); *op++ = LZO_BYTE(m_len);
} }
*op++ = LZO_BYTE(m_off << 2); *op++ = LZO_BYTE(m_off << 2);
*op++ = LZO_BYTE(m_off >> 6); *op++ = LZO_BYTE(m_off >> 6);
} }
goto next; goto next;
} }
@ -351,8 +448,10 @@ DO_COMPRESS ( const lzo_bytep in , lzo_uint in_len,
ll = LZO_MIN(ll, 49152); ll = LZO_MIN(ll, 49152);
#endif #endif
ll_end = (lzo_uintptr_t)ip + ll; ll_end = (lzo_uintptr_t)ip + ll;
if ((ll_end + ((t + ll) >> 5)) <= ll_end || (const lzo_bytep)(ll_end + ((t + ll) >> 5)) <= ip + ll) if ((ll_end + ((t + ll) >> 5)) <= ll_end || (const lzo_bytep)(ll_end + ((t + ll) >> 5)) <= ip + ll)
break; break;
#if (LZO_DETERMINISTIC) #if (LZO_DETERMINISTIC)
lzo_memset(wrkmem, 0, ((lzo_uint)1 << D_BITS) * sizeof(lzo_dict_t)); lzo_memset(wrkmem, 0, ((lzo_uint)1 << D_BITS) * sizeof(lzo_dict_t));
#endif #endif
@ -361,6 +460,7 @@ DO_COMPRESS ( const lzo_bytep in , lzo_uint in_len,
op += *out_len; op += *out_len;
l -= ll; l -= ll;
} }
t += l; t += l;
if (t > 0) if (t > 0)
@ -378,15 +478,18 @@ DO_COMPRESS ( const lzo_bytep in , lzo_uint in_len,
lzo_uint tt = t - 18; lzo_uint tt = t - 18;
*op++ = 0; *op++ = 0;
while (tt > 255) while (tt > 255)
{ {
tt -= 255; tt -= 255;
UA_SET1(op, 0); UA_SET1(op, 0);
op++; op++;
} }
assert(tt > 0); assert(tt > 0);
*op++ = LZO_BYTE(tt); *op++ = LZO_BYTE(tt);
} }
UA_COPYN(op, ii, t); UA_COPYN(op, ii, t);
op += t; op += t;
} }

View File

@ -61,6 +61,7 @@ DO_DECOMPRESS ( const lzo_bytep in , lzo_uint in_len,
LZO_UNUSED(wrkmem); LZO_UNUSED(wrkmem);
#if defined(COPY_DICT) #if defined(COPY_DICT)
if (dict) if (dict)
{ {
if (dict_len > M4_MAX_OFFSET) if (dict_len > M4_MAX_OFFSET)
@ -68,6 +69,7 @@ DO_DECOMPRESS ( const lzo_bytep in , lzo_uint in_len,
dict += dict_len - M4_MAX_OFFSET; dict += dict_len - M4_MAX_OFFSET;
dict_len = M4_MAX_OFFSET; dict_len = M4_MAX_OFFSET;
} }
dict_end = dict + dict_len; dict_end = dict + dict_len;
} }
else else
@ -75,6 +77,7 @@ DO_DECOMPRESS ( const lzo_bytep in , lzo_uint in_len,
dict_len = 0; dict_len = 0;
dict_end = NULL; dict_end = NULL;
} }
#endif /* COPY_DICT */ #endif /* COPY_DICT */
*out_len = 0; *out_len = 0;
@ -83,13 +86,22 @@ DO_DECOMPRESS ( const lzo_bytep in , lzo_uint in_len,
ip = in; ip = in;
NEED_IP(1); NEED_IP(1);
if (*ip > 17) if (*ip > 17)
{ {
t = *ip++ - 17; t = *ip++ - 17;
if (t < 4) if (t < 4)
goto match_next; goto match_next;
assert(t > 0); NEED_OP(t); NEED_IP(t+3);
do *op++ = *ip++; while (--t > 0); assert(t > 0);
NEED_OP(t);
NEED_IP(t + 3);
do* op++ = *ip++;
while (--t > 0);
goto first_literal_run; goto first_literal_run;
} }
@ -97,8 +109,10 @@ DO_DECOMPRESS ( const lzo_bytep in , lzo_uint in_len,
{ {
NEED_IP(3); NEED_IP(3);
t = *ip++; t = *ip++;
if (t >= 16) if (t >= 16)
goto match; goto match;
/* a literal run */ /* a literal run */
if (t == 0) if (t == 0)
{ {
@ -109,47 +123,74 @@ DO_DECOMPRESS ( const lzo_bytep in , lzo_uint in_len,
TEST_IV(t); TEST_IV(t);
NEED_IP(1); NEED_IP(1);
} }
t += 15 + *ip++; t += 15 + *ip++;
} }
/* copy literals */ /* copy literals */
assert(t > 0); NEED_OP(t+3); NEED_IP(t+6); assert(t > 0);
NEED_OP(t + 3);
NEED_IP(t + 6);
#if (LZO_OPT_UNALIGNED64) && (LZO_OPT_UNALIGNED32) #if (LZO_OPT_UNALIGNED64) && (LZO_OPT_UNALIGNED32)
t += 3; t += 3;
if (t >= 8) do if (t >= 8) do
{ {
UA_COPY8(op, ip); UA_COPY8(op, ip);
op += 8; ip += 8; t -= 8; op += 8;
} while (t >= 8); ip += 8;
t -= 8;
}
while (t >= 8);
if (t >= 4) if (t >= 4)
{ {
UA_COPY4(op, ip); UA_COPY4(op, ip);
op += 4; ip += 4; t -= 4; op += 4;
ip += 4;
t -= 4;
} }
if (t > 0) if (t > 0)
{ {
*op++ = *ip++; *op++ = *ip++;
if (t > 1) { *op++ = *ip++; if (t > 2) { *op++ = *ip++; } } if (t > 1) { *op++ = *ip++; if (t > 2) { *op++ = *ip++; } }
} }
#elif (LZO_OPT_UNALIGNED32) || (LZO_ALIGNED_OK_4) #elif (LZO_OPT_UNALIGNED32) || (LZO_ALIGNED_OK_4)
#if !(LZO_OPT_UNALIGNED32) #if !(LZO_OPT_UNALIGNED32)
if (PTR_ALIGNED2_4(op, ip)) if (PTR_ALIGNED2_4(op, ip))
{ {
#endif #endif
UA_COPY4(op, ip); UA_COPY4(op, ip);
op += 4; ip += 4; op += 4;
ip += 4;
if (--t > 0) if (--t > 0)
{ {
if (t >= 4) if (t >= 4)
{ {
do { do
{
UA_COPY4(op, ip); UA_COPY4(op, ip);
op += 4; ip += 4; t -= 4; op += 4;
} while (t >= 4); ip += 4;
if (t > 0) do *op++ = *ip++; while (--t > 0); t -= 4;
}
while (t >= 4);
if (t > 0) do* op++ = *ip++;
while (--t > 0);
} }
else else
do *op++ = *ip++; while (--t > 0); do* op++ = *ip++;
while (--t > 0);
} }
#if !(LZO_OPT_UNALIGNED32) #if !(LZO_OPT_UNALIGNED32)
} }
else else
@ -157,9 +198,15 @@ DO_DECOMPRESS ( const lzo_bytep in , lzo_uint in_len,
#endif #endif
#if !(LZO_OPT_UNALIGNED32) #if !(LZO_OPT_UNALIGNED32)
{ {
*op++ = *ip++; *op++ = *ip++; *op++ = *ip++; *op++ = *ip++;
do *op++ = *ip++; while (--t > 0); *op++ = *ip++;
*op++ = *ip++;
do* op++ = *ip++;
while (--t > 0);
} }
#endif #endif
@ -167,8 +214,10 @@ first_literal_run:
t = *ip++; t = *ip++;
if (t >= 16) if (t >= 16)
goto match; goto match;
#if defined(COPY_DICT) #if defined(COPY_DICT)
#if defined(LZO1Z) #if defined(LZO1Z)
m_off = (1 + M2_MAX_OFFSET) + (t << 6) + (*ip++ >> 2); m_off = (1 + M2_MAX_OFFSET) + (t << 6) + (*ip++ >> 2);
@ -177,7 +226,8 @@ first_literal_run:
m_off = (1 + M2_MAX_OFFSET) + (t >> 2) + (*ip++ << 2); m_off = (1 + M2_MAX_OFFSET) + (t >> 2) + (*ip++ << 2);
#endif #endif
NEED_OP(3); NEED_OP(3);
t = 3; COPY_DICT(t,m_off) t = 3;
COPY_DICT(t, m_off)
#else /* !COPY_DICT */ #else /* !COPY_DICT */
#if defined(LZO1Z) #if defined(LZO1Z)
t = (1 + M2_MAX_OFFSET) + (t << 6) + (*ip++ >> 2); t = (1 + M2_MAX_OFFSET) + (t << 6) + (*ip++ >> 2);
@ -188,15 +238,20 @@ first_literal_run:
m_pos -= t >> 2; m_pos -= t >> 2;
m_pos -= *ip++ << 2; m_pos -= *ip++ << 2;
#endif #endif
TEST_LB(m_pos); NEED_OP(3); TEST_LB(m_pos);
*op++ = *m_pos++; *op++ = *m_pos++; *op++ = *m_pos; NEED_OP(3);
*op++ = *m_pos++;
*op++ = *m_pos++;
*op++ = *m_pos;
#endif /* COPY_DICT */ #endif /* COPY_DICT */
goto match_done; goto match_done;
/* handle matches */ /* handle matches */
for (;;) { for (;;)
{
match: match:
if (t >= 64) /* a M2 match */ if (t >= 64) /* a M2 match */
{ {
#if defined(COPY_DICT) #if defined(COPY_DICT)
@ -208,6 +263,7 @@ match:
t = (t >> 4) - 3; t = (t >> 4) - 3;
#elif defined(LZO1Z) #elif defined(LZO1Z)
m_off = t & 0x1f; m_off = t & 0x1f;
if (m_off >= 0x1c) if (m_off >= 0x1c)
m_off = last_m_off; m_off = last_m_off;
else else
@ -215,6 +271,7 @@ match:
m_off = 1 + (m_off << 6) + (*ip++ >> 2); m_off = 1 + (m_off << 6) + (*ip++ >> 2);
last_m_off = m_off; last_m_off = m_off;
} }
t = (t >> 5) - 1; t = (t >> 5) - 1;
#endif #endif
#else /* !COPY_DICT */ #else /* !COPY_DICT */
@ -232,6 +289,7 @@ match:
{ {
lzo_uint off = t & 0x1f; lzo_uint off = t & 0x1f;
m_pos = op; m_pos = op;
if (off >= 0x1c) if (off >= 0x1c)
{ {
assert(last_m_off > 0); assert(last_m_off > 0);
@ -246,13 +304,16 @@ match:
} }
t = (t >> 5) - 1; t = (t >> 5) - 1;
#endif #endif
TEST_LB(m_pos); assert(t > 0); NEED_OP(t+3-1); TEST_LB(m_pos);
assert(t > 0);
NEED_OP(t + 3 - 1);
goto copy_match; goto copy_match;
#endif /* COPY_DICT */ #endif /* COPY_DICT */
} }
else if (t >= 32) /* a M3 match */ else if (t >= 32) /* a M3 match */
{ {
t &= 31; t &= 31;
if (t == 0) if (t == 0)
{ {
while (*ip == 0) while (*ip == 0)
@ -262,9 +323,11 @@ match:
TEST_OV(t); TEST_OV(t);
NEED_IP(1); NEED_IP(1);
} }
t += 31 + *ip++; t += 31 + *ip++;
NEED_IP(2); NEED_IP(2);
} }
#if defined(COPY_DICT) #if defined(COPY_DICT)
#if defined(LZO1Z) #if defined(LZO1Z)
m_off = 1 + (ip[0] << 6) + (ip[1] >> 2); m_off = 1 + (ip[0] << 6) + (ip[1] >> 2);
@ -298,6 +361,7 @@ match:
m_pos -= (t & 8) << 11; m_pos -= (t & 8) << 11;
#endif /* COPY_DICT */ #endif /* COPY_DICT */
t &= 7; t &= 7;
if (t == 0) if (t == 0)
{ {
while (*ip == 0) while (*ip == 0)
@ -307,9 +371,11 @@ match:
TEST_OV(t); TEST_OV(t);
NEED_IP(1); NEED_IP(1);
} }
t += 7 + *ip++; t += 7 + *ip++;
NEED_IP(2); NEED_IP(2);
} }
#if defined(COPY_DICT) #if defined(COPY_DICT)
#if defined(LZO1Z) #if defined(LZO1Z)
m_off += (ip[0] << 6) + (ip[1] >> 2); m_off += (ip[0] << 6) + (ip[1] >> 2);
@ -317,8 +383,10 @@ match:
m_off += (ip[0] >> 2) + (ip[1] << 6); m_off += (ip[0] >> 2) + (ip[1] << 6);
#endif #endif
ip += 2; ip += 2;
if (m_off == 0) if (m_off == 0)
goto eof_found; goto eof_found;
m_off += 0x4000; m_off += 0x4000;
#if defined(LZO1Z) #if defined(LZO1Z)
last_m_off = m_off; last_m_off = m_off;
@ -332,8 +400,10 @@ match:
m_pos -= (ip[0] >> 2) + (ip[1] << 6); m_pos -= (ip[0] >> 2) + (ip[1] << 6);
#endif #endif
ip += 2; ip += 2;
if (m_pos == op) if (m_pos == op)
goto eof_found; goto eof_found;
m_pos -= 0x4000; m_pos -= 0x4000;
#if defined(LZO1Z) #if defined(LZO1Z)
last_m_off = pd((const lzo_bytep)op, m_pos); last_m_off = pd((const lzo_bytep)op, m_pos);
@ -350,7 +420,8 @@ match:
m_off = 1 + (t >> 2) + (*ip++ << 2); m_off = 1 + (t >> 2) + (*ip++ << 2);
#endif #endif
NEED_OP(2); NEED_OP(2);
t = 2; COPY_DICT(t,m_off) t = 2;
COPY_DICT(t, m_off)
#else /* !COPY_DICT */ #else /* !COPY_DICT */
#if defined(LZO1Z) #if defined(LZO1Z)
t = 1 + (t << 6) + (*ip++ >> 2); t = 1 + (t << 6) + (*ip++ >> 2);
@ -361,8 +432,10 @@ match:
m_pos -= t >> 2; m_pos -= t >> 2;
m_pos -= *ip++ << 2; m_pos -= *ip++ << 2;
#endif #endif
TEST_LB(m_pos); NEED_OP(2); TEST_LB(m_pos);
*op++ = *m_pos++; *op++ = *m_pos; NEED_OP(2);
*op++ = *m_pos++;
*op++ = *m_pos;
#endif /* COPY_DICT */ #endif /* COPY_DICT */
goto match_done; goto match_done;
} }
@ -371,28 +444,41 @@ match:
#if defined(COPY_DICT) #if defined(COPY_DICT)
NEED_OP(t + 3 - 1); NEED_OP(t + 3 - 1);
t += 3-1; COPY_DICT(t,m_off) t += 3 - 1;
COPY_DICT(t, m_off)
#else /* !COPY_DICT */ #else /* !COPY_DICT */
TEST_LB(m_pos); assert(t > 0); NEED_OP(t+3-1); TEST_LB(m_pos);
assert(t > 0);
NEED_OP(t + 3 - 1);
#if (LZO_OPT_UNALIGNED64) && (LZO_OPT_UNALIGNED32) #if (LZO_OPT_UNALIGNED64) && (LZO_OPT_UNALIGNED32)
if (op - m_pos >= 8) if (op - m_pos >= 8)
{ {
t += (3 - 1); t += (3 - 1);
if (t >= 8) do if (t >= 8) do
{ {
UA_COPY8(op, m_pos); UA_COPY8(op, m_pos);
op += 8; m_pos += 8; t -= 8; op += 8;
} while (t >= 8); m_pos += 8;
t -= 8;
}
while (t >= 8);
if (t >= 4) if (t >= 4)
{ {
UA_COPY4(op, m_pos); UA_COPY4(op, m_pos);
op += 4; m_pos += 4; t -= 4; op += 4;
m_pos += 4;
t -= 4;
} }
if (t > 0) if (t > 0)
{ {
*op++ = m_pos[0]; *op++ = m_pos[0];
if (t > 1) { *op++ = m_pos[1]; if (t > 2) { *op++ = m_pos[2]; } } if (t > 1) { *op++ = m_pos[1]; if (t > 2) { *op++ = m_pos[2]; } }
} }
} }
@ -403,23 +489,38 @@ match:
{ {
assert((op - m_pos) >= 4); /* both pointers are aligned */ assert((op - m_pos) >= 4); /* both pointers are aligned */
#else #else
if (t >= 2 * 4 - (3 - 1) && (op - m_pos) >= 4) if (t >= 2 * 4 - (3 - 1) && (op - m_pos) >= 4)
{ {
#endif #endif
UA_COPY4(op, m_pos); UA_COPY4(op, m_pos);
op += 4; m_pos += 4; t -= 4 - (3 - 1); op += 4;
do { m_pos += 4;
t -= 4 - (3 - 1);
do
{
UA_COPY4(op, m_pos); UA_COPY4(op, m_pos);
op += 4; m_pos += 4; t -= 4; op += 4;
} while (t >= 4); m_pos += 4;
if (t > 0) do *op++ = *m_pos++; while (--t > 0); t -= 4;
}
while (t >= 4);
if (t > 0) do* op++ = *m_pos++;
while (--t > 0);
} }
else else
#endif #endif
{ {
copy_match: copy_match:
*op++ = *m_pos++; *op++ = *m_pos++; *op++ = *m_pos++;
do *op++ = *m_pos++; while (--t > 0); *op++ = *m_pos++;
do* op++ = *m_pos++;
while (--t > 0);
} }
#endif /* COPY_DICT */ #endif /* COPY_DICT */
@ -430,17 +531,27 @@ match_done:
#else #else
t = ip[-2] & 3; t = ip[-2] & 3;
#endif #endif
if (t == 0) if (t == 0)
break; break;
/* copy literals */ /* copy literals */
match_next: match_next:
assert(t > 0); assert(t < 4); NEED_OP(t); NEED_IP(t+3); assert(t > 0);
assert(t < 4);
NEED_OP(t);
NEED_IP(t + 3);
#if 0 #if 0
do *op++ = *ip++; while (--t > 0);
do* op++ = *ip++;
while (--t > 0);
#else #else
*op++ = *ip++; *op++ = *ip++;
if (t > 1) { *op++ = *ip++; if (t > 2) { *op++ = *ip++; } } if (t > 1) { *op++ = *ip++; if (t > 2) { *op++ = *ip++; } }
#endif #endif
t = *ip++; t = *ip++;
} }

View File

@ -41,6 +41,7 @@ static void copy2(lzo_bytep ip, const lzo_bytep m_pos, lzo_uint off)
{ {
assert(off > 0); assert(off > 0);
ip[0] = m_pos[0]; ip[0] = m_pos[0];
if (off == 1) if (off == 1)
ip[1] = m_pos[0]; ip[1] = m_pos[0];
else else
@ -52,6 +53,7 @@ static void copy3(lzo_bytep ip, const lzo_bytep m_pos, lzo_uint off)
{ {
assert(off > 0); assert(off > 0);
ip[0] = m_pos[0]; ip[0] = m_pos[0];
if (off == 1) if (off == 1)
{ {
ip[2] = ip[1] = m_pos[0]; ip[2] = ip[1] = m_pos[0];
@ -98,41 +100,57 @@ DO_OPTIMIZE ( lzo_bytep in , lzo_uint in_len,
ip = in; ip = in;
assert(in_len >= 3); assert(in_len >= 3);
if (*ip > 17) if (*ip > 17)
{ {
t = *ip++ - 17; t = *ip++ - 17;
if (t < 4) if (t < 4)
goto match_next; goto match_next;
goto first_literal_run; goto first_literal_run;
} }
assert(*ip < 16 || (*ip == 17 && in_len == 3)); assert(*ip < 16 || (*ip == 17 && in_len == 3));
while (TEST_IP_AND_TEST_OP) while (TEST_IP_AND_TEST_OP)
{ {
t = *ip++; t = *ip++;
if (t >= 16) if (t >= 16)
goto match; goto match;
/* a literal run */ /* a literal run */
litp = ip - 1; litp = ip - 1;
if (t == 0) if (t == 0)
{ {
t = 15; t = 15;
while (*ip == 0) while (*ip == 0)
t += 255, ip++; t += 255, ip++;
t += *ip++; t += *ip++;
} }
lit = t + 3; lit = t + 3;
/* copy literals */ /* copy literals */
copy_literal_run: copy_literal_run:
*op++ = *ip++; *op++ = *ip++; *op++ = *ip++; *op++ = *ip++;
*op++ = *ip++;
*op++ = *ip++;
first_literal_run: first_literal_run:
do *op++ = *ip++; while (--t > 0);
do* op++ = *ip++;
while (--t > 0);
t = *ip++; t = *ip++;
if (t >= 16) if (t >= 16)
goto match; goto match;
#if defined(LZO1X) #if defined(LZO1X)
m_pos = op - 1 - 0x800; m_pos = op - 1 - 0x800;
#elif defined(LZO1Y) #elif defined(LZO1Y)
@ -140,13 +158,16 @@ first_literal_run:
#endif #endif
m_pos -= t >> 2; m_pos -= t >> 2;
m_pos -= *ip++ << 2; m_pos -= *ip++ << 2;
*op++ = *m_pos++; *op++ = *m_pos++; *op++ = *m_pos++; *op++ = *m_pos++;
*op++ = *m_pos++;
*op++ = *m_pos++;
lit = 0; lit = 0;
goto match_done; goto match_done;
/* handle matches */ /* handle matches */
do { do
{
if (t < 16) /* a M1 match */ if (t < 16) /* a M1 match */
{ {
m_pos = op - 1; m_pos = op - 1;
@ -161,6 +182,7 @@ first_literal_run:
assert(litp == ip - 2 - lit - 2); assert(litp == ip - 2 - lit - 2);
assert((lzo_uint)(*litp & 3) == lit); assert((lzo_uint)(*litp & 3) == lit);
nl = ip[-2] & 3; nl = ip[-2] & 3;
/* test if a match follows */ /* test if a match follows */
if (nl == 0 && lit == 1 && ip[0] >= 16) if (nl == 0 && lit == 1 && ip[0] >= 16)
{ {
@ -183,22 +205,29 @@ first_literal_run:
copy2(ip - 3 + 1, m_pos, pd(op, m_pos)); copy2(ip - 3 + 1, m_pos, pd(op, m_pos));
/* move literals 1 byte ahead */ /* move literals 1 byte ahead */
litp += 2; litp += 2;
if (lit > 0) if (lit > 0)
lzo_memmove(litp + 1, litp, lit); lzo_memmove(litp + 1, litp, lit);
/* insert new length of long literal run */ /* insert new length of long literal run */
lit += 2 + t + 3; assert(lit <= 18); lit += 2 + t + 3;
assert(lit <= 18);
*litp = LZO_BYTE(lit - 3); *litp = LZO_BYTE(lit - 3);
o_m1_b++; o_m1_b++;
*op++ = *m_pos++; *op++ = *m_pos++; *op++ = *m_pos++;
*op++ = *m_pos++;
goto copy_literal_run; goto copy_literal_run;
} }
copy_m1: copy_m1:
*op++ = *m_pos++; *op++ = *m_pos++; *op++ = *m_pos++;
*op++ = *m_pos++;
} }
else else
{ {
match: match:
if (t >= 64) /* a M2 match */ if (t >= 64) /* a M2 match */
{ {
m_pos = op - 1; m_pos = op - 1;
@ -211,10 +240,12 @@ match:
m_pos -= *ip++ << 2; m_pos -= *ip++ << 2;
t = (t >> 4) - 3; t = (t >> 4) - 3;
#endif #endif
if (litp == NULL) if (litp == NULL)
goto copy_m; goto copy_m;
nl = ip[-2] & 3; nl = ip[-2] & 3;
/* test if in beetween two long literal runs */ /* test if in beetween two long literal runs */
if (t == 1 && lit > 3 && nl == 0 && if (t == 1 && lit > 3 && nl == 0 &&
ip[0] < 16 && ip[0] != 0 && (lit + 3 + ip[0] < 16)) ip[0] < 16 && ip[0] != 0 && (lit + 3 + ip[0] < 16))
@ -224,10 +255,13 @@ match:
/* copy over the 3 literals that replace the match */ /* copy over the 3 literals that replace the match */
copy3(ip - 1 - 2, m_pos, pd(op, m_pos)); copy3(ip - 1 - 2, m_pos, pd(op, m_pos));
/* set new length of previous literal run */ /* set new length of previous literal run */
lit += 3 + t + 3; assert(lit <= 18); lit += 3 + t + 3;
assert(lit <= 18);
*litp = LZO_BYTE(lit - 3); *litp = LZO_BYTE(lit - 3);
o_m2++; o_m2++;
*op++ = *m_pos++; *op++ = *m_pos++; *op++ = *m_pos++; *op++ = *m_pos++;
*op++ = *m_pos++;
*op++ = *m_pos++;
goto copy_literal_run; goto copy_literal_run;
} }
} }
@ -236,13 +270,17 @@ match:
if (t >= 32) /* a M3 match */ if (t >= 32) /* a M3 match */
{ {
t &= 31; t &= 31;
if (t == 0) if (t == 0)
{ {
t = 31; t = 31;
while (*ip == 0) while (*ip == 0)
t += 255, ip++; t += 255, ip++;
t += *ip++; t += *ip++;
} }
m_pos = op - 1; m_pos = op - 1;
m_pos -= *ip++ >> 2; m_pos -= *ip++ >> 2;
m_pos -= *ip++ << 6; m_pos -= *ip++ << 6;
@ -252,23 +290,31 @@ match:
m_pos = op; m_pos = op;
m_pos -= (t & 8) << 11; m_pos -= (t & 8) << 11;
t &= 7; t &= 7;
if (t == 0) if (t == 0)
{ {
t = 7; t = 7;
while (*ip == 0) while (*ip == 0)
t += 255, ip++; t += 255, ip++;
t += *ip++; t += *ip++;
} }
m_pos -= *ip++ >> 2; m_pos -= *ip++ >> 2;
m_pos -= *ip++ << 6; m_pos -= *ip++ << 6;
if (m_pos == op) if (m_pos == op)
goto eof_found; goto eof_found;
m_pos -= 0x4000; m_pos -= 0x4000;
} }
if (litp == NULL) if (litp == NULL)
goto copy_m; goto copy_m;
nl = ip[-2] & 3; nl = ip[-2] & 3;
/* test if in beetween two matches */ /* test if in beetween two matches */
if (t == 1 && lit == 0 && nl == 0 && ip[0] >= 16) if (t == 1 && lit == 0 && nl == 0 && ip[0] >= 16)
{ {
@ -295,23 +341,34 @@ match:
copy3(ip - 4 + 1, m_pos, pd(op, m_pos)); copy3(ip - 4 + 1, m_pos, pd(op, m_pos));
/* move literals 1 byte ahead */ /* move literals 1 byte ahead */
litp += 2; litp += 2;
if (lit > 0) if (lit > 0)
lzo_memmove(litp + 1, litp, lit); lzo_memmove(litp + 1, litp, lit);
/* insert new length of long literal run */ /* insert new length of long literal run */
lit += 3 + t + 3; assert(lit <= 18); lit += 3 + t + 3;
assert(lit <= 18);
*litp = LZO_BYTE(lit - 3); *litp = LZO_BYTE(lit - 3);
o_m3_b++; o_m3_b++;
*op++ = *m_pos++; *op++ = *m_pos++; *op++ = *m_pos++; *op++ = *m_pos++;
*op++ = *m_pos++;
*op++ = *m_pos++;
goto copy_literal_run; goto copy_literal_run;
} }
} }
copy_m: copy_m:
*op++ = *m_pos++; *op++ = *m_pos++; *op++ = *m_pos++;
do *op++ = *m_pos++; while (--t > 0); *op++ = *m_pos++;
do* op++ = *m_pos++;
while (--t > 0);
} }
match_done: match_done:
if (next_lit == NO_LIT) if (next_lit == NO_LIT)
{ {
t = ip[-2] & 3; t = ip[-2] & 3;
@ -320,15 +377,23 @@ match_done:
} }
else else
t = next_lit; t = next_lit;
assert(t <= 3); assert(t <= 3);
next_lit = NO_LIT; next_lit = NO_LIT;
if (t == 0) if (t == 0)
break; break;
/* copy literals */ /* copy literals */
match_next: match_next:
do *op++ = *ip++; while (--t > 0);
do* op++ = *ip++;
while (--t > 0);
t = *ip++; t = *ip++;
} while (TEST_IP_AND_TEST_OP); }
while (TEST_IP_AND_TEST_OP);
} }
/* no EOF code was found */ /* no EOF code was found */
@ -341,8 +406,11 @@ eof_found:
printf("optimize: %5lu %5lu %5lu %5lu %5lu\n", printf("optimize: %5lu %5lu %5lu %5lu %5lu\n",
o_m1_a, o_m1_b, o_m2, o_m3_a, o_m3_b); o_m1_a, o_m1_b, o_m2, o_m3_a, o_m3_b);
#endif #endif
LZO_UNUSED(o_m1_a); LZO_UNUSED(o_m1_b); LZO_UNUSED(o_m2); LZO_UNUSED(o_m1_a);
LZO_UNUSED(o_m3_a); LZO_UNUSED(o_m3_b); LZO_UNUSED(o_m1_b);
LZO_UNUSED(o_m2);
LZO_UNUSED(o_m3_a);
LZO_UNUSED(o_m3_b);
*out_len = pd(op, out); *out_len = pd(op, out);
return (ip == ip_end ? LZO_E_OK : return (ip == ip_end ? LZO_E_OK :
(ip < ip_end ? LZO_E_INPUT_NOT_CONSUMED : LZO_E_INPUT_OVERRUN)); (ip < ip_end ? LZO_E_INPUT_NOT_CONSUMED : LZO_E_INPUT_OVERRUN));

View File

@ -108,14 +108,18 @@ lzo2a_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
op = out; op = out;
r = init_match(c, swd, NULL, 0, 0); r = init_match(c, swd, NULL, 0, 0);
if (r != 0) if (r != 0)
return r; return r;
if (max_chain > 0) if (max_chain > 0)
swd->max_chain = max_chain; swd->max_chain = max_chain;
r = find_match(c, swd, 0, 0); r = find_match(c, swd, 0, 0);
if (r != 0) if (r != 0)
return r; return r;
while (c->look > 0) while (c->look > 0)
{ {
lzo_uint lazy_match_min_gain = 0; lzo_uint lazy_match_min_gain = 0;
@ -129,6 +133,7 @@ lzo2a_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
m_off = c->m_off; m_off = c->m_off;
#if (SWD_N >= 8192) #if (SWD_N >= 8192)
if (m_off >= 8192) if (m_off >= 8192)
{ {
if (m_len < M3_MIN_LEN) if (m_len < M3_MIN_LEN)
@ -165,10 +170,12 @@ lzo2a_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
unsigned char lit = LZO_BYTE(swd->b_char); unsigned char lit = LZO_BYTE(swd->b_char);
r = find_match(c, swd, 1, 0); r = find_match(c, swd, 1, 0);
assert(r == 0); LZO_UNUSED(r); assert(r == 0);
LZO_UNUSED(r);
assert(c->look > 0); assert(c->look > 0);
#if (SWD_N >= 8192) #if (SWD_N >= 8192)
if (m_off < 8192 && c->m_off >= 8192) if (m_off < 8192 && c->m_off >= 8192)
lazy_match_min_gain += extra1; lazy_match_min_gain += extra1;
else else
@ -179,6 +186,7 @@ lzo2a_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
c->m_len <= M1_MAX_LEN && c->m_off <= 256)) c->m_len <= M1_MAX_LEN && c->m_off <= 256))
lazy_match_min_gain += extra2; lazy_match_min_gain += extra2;
} }
if (c->m_len >= M1_MIN_LEN && if (c->m_len >= M1_MIN_LEN &&
c->m_len <= M1_MAX_LEN && c->m_off <= 256) c->m_len <= M1_MAX_LEN && c->m_off <= 256)
{ {
@ -206,6 +214,7 @@ lzo2a_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
} }
else else
ahead = 1; ahead = 1;
assert(m_len > 0); assert(m_len > 0);
} }
@ -217,7 +226,8 @@ lzo2a_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
putbyte(swd->b_char); putbyte(swd->b_char);
c->lit_bytes++; c->lit_bytes++;
r = find_match(c, swd, 1, 0); r = find_match(c, swd, 1, 0);
assert(r == 0); LZO_UNUSED(r); assert(r == 0);
LZO_UNUSED(r);
} }
else else
{ {
@ -234,6 +244,7 @@ lzo2a_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
putbyte(m_off - 1); putbyte(m_off - 1);
c->m1++; c->m1++;
} }
#if (SWD_N >= 8192) #if (SWD_N >= 8192)
else if (m_off >= 8192) else if (m_off >= 8192)
{ {
@ -245,14 +256,17 @@ lzo2a_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
putbyte(m_off >> 5); putbyte(m_off >> 5);
putbit(1); putbit(1);
len -= M3_MIN_LEN - 1; len -= M3_MIN_LEN - 1;
while (len > 255) while (len > 255)
{ {
len -= 255; len -= 255;
putbyte(0); putbyte(0);
} }
putbyte(len); putbyte(len);
c->m4++; c->m4++;
} }
#endif #endif
else else
{ {
@ -260,6 +274,7 @@ lzo2a_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
putbit(1); putbit(1);
putbit(1); putbit(1);
if (m_len <= 9) if (m_len <= 9)
{ {
putbyte(((m_len - 2) << 5) | (m_off & 31)); putbyte(((m_len - 2) << 5) | (m_off & 31));
@ -275,17 +290,21 @@ lzo2a_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
putbit(0); putbit(0);
#endif #endif
len -= 10 - 1; len -= 10 - 1;
while (len > 255) while (len > 255)
{ {
len -= 255; len -= 255;
putbyte(0); putbyte(0);
} }
putbyte(len); putbyte(len);
c->m3++; c->m3++;
} }
} }
r = find_match(c, swd, m_len, 1 + ahead); r = find_match(c, swd, m_len, 1 + ahead);
assert(r == 0); LZO_UNUSED(r); assert(r == 0);
LZO_UNUSED(r);
} }
c->codesize = pd(op, out); c->codesize = pd(op, out);
@ -301,6 +320,7 @@ lzo2a_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
/* flush remaining bits */ /* flush remaining bits */
assert(k < CHAR_BIT); assert(k < CHAR_BIT);
if (k > 0) if (k > 0)
{ {
assert(b == MASKBITS(k)); assert(b == MASKBITS(k));

View File

@ -62,17 +62,21 @@ DO_DECOMPRESS ( const lzo_bytep in , lzo_uint in_len,
while (TEST_IP_AND_TEST_OP) while (TEST_IP_AND_TEST_OP)
{ {
NEEDBITS(1); NEEDBITS(1);
if (MASKBITS(1) == 0) if (MASKBITS(1) == 0)
{ {
DUMPBITS(1); DUMPBITS(1);
/* a literal */ /* a literal */
NEED_IP(1); NEED_OP(1); NEED_IP(1);
NEED_OP(1);
*op++ = *ip++; *op++ = *ip++;
continue; continue;
} }
DUMPBITS(1); DUMPBITS(1);
NEEDBITS(1); NEEDBITS(1);
if (MASKBITS(1) == 0) if (MASKBITS(1) == 0)
{ {
DUMPBITS(1); DUMPBITS(1);
@ -80,13 +84,16 @@ DO_DECOMPRESS ( const lzo_bytep in , lzo_uint in_len,
NEEDBITS(2); NEEDBITS(2);
t = M1_MIN_LEN + (lzo_uint) MASKBITS(2); t = M1_MIN_LEN + (lzo_uint) MASKBITS(2);
DUMPBITS(2); DUMPBITS(2);
NEED_IP(1); NEED_OP(t); NEED_IP(1);
NEED_OP(t);
m_pos = op - 1 - *ip++; m_pos = op - 1 - *ip++;
assert(m_pos >= out); assert(m_pos < op); assert(m_pos >= out);
assert(m_pos < op);
TEST_LB(m_pos); TEST_LB(m_pos);
MEMCPY_DS(op, m_pos, t); MEMCPY_DS(op, m_pos, t);
continue; continue;
} }
DUMPBITS(1); DUMPBITS(1);
NEED_IP(2); NEED_IP(2);
@ -94,12 +101,14 @@ DO_DECOMPRESS ( const lzo_bytep in , lzo_uint in_len,
m_pos = op; m_pos = op;
m_pos -= (t & 31) | (((lzo_uint) * ip++) << 5); m_pos -= (t & 31) | (((lzo_uint) * ip++) << 5);
t >>= 5; t >>= 5;
if (t == 0) if (t == 0)
{ {
#if (SWD_N >= 8192) #if (SWD_N >= 8192)
NEEDBITS(1); NEEDBITS(1);
t = MASKBITS(1); t = MASKBITS(1);
DUMPBITS(1); DUMPBITS(1);
if (t == 0) if (t == 0)
t = 10 - 1; t = 10 - 1;
else else
@ -108,10 +117,12 @@ DO_DECOMPRESS ( const lzo_bytep in , lzo_uint in_len,
m_pos -= 8192; /* t << 13 */ m_pos -= 8192; /* t << 13 */
t = M3_MIN_LEN - 1; t = M3_MIN_LEN - 1;
} }
#else #else
t = 10 - 1; t = 10 - 1;
#endif #endif
NEED_IP(1); NEED_IP(1);
while (*ip == 0) while (*ip == 0)
{ {
t += 255; t += 255;
@ -119,17 +130,22 @@ DO_DECOMPRESS ( const lzo_bytep in , lzo_uint in_len,
TEST_OV(t); TEST_OV(t);
NEED_IP(1); NEED_IP(1);
} }
t += *ip++; t += *ip++;
} }
else else
{ {
#if defined(LZO_EOF_CODE) #if defined(LZO_EOF_CODE)
if (m_pos == op) if (m_pos == op)
goto eof_found; goto eof_found;
#endif #endif
t += 2; t += 2;
} }
assert(m_pos >= out); assert(m_pos < op);
assert(m_pos >= out);
assert(m_pos < op);
TEST_LB(m_pos); TEST_LB(m_pos);
NEED_OP(t); NEED_OP(t);
MEMCPY_DS(op, m_pos, t); MEMCPY_DS(op, m_pos, t);

View File

@ -35,7 +35,8 @@
// see http://www.zlib.org/ // see http://www.zlib.org/
************************************************************************/ ************************************************************************/
static const lzo_uint32_t lzo_crc32_table[256] = { static const lzo_uint32_t lzo_crc32_table[256] =
{
0x00000000L, 0x77073096L, 0xee0e612cL, 0x990951baL, 0x076dc419L, 0x00000000L, 0x77073096L, 0xee0e612cL, 0x990951baL, 0x076dc419L,
0x706af48fL, 0xe963a535L, 0x9e6495a3L, 0x0edb8832L, 0x79dcb8a4L, 0x706af48fL, 0xe963a535L, 0x9e6495a3L, 0x0edb8832L, 0x79dcb8a4L,
0xe0d5e91eL, 0x97d2d988L, 0x09b64c2bL, 0x7eb17cbdL, 0xe7b82d07L, 0xe0d5e91eL, 0x97d2d988L, 0x09b64c2bL, 0x7eb17cbdL, 0xe7b82d07L,
@ -126,18 +127,22 @@ lzo_crc32(lzo_uint32_t c, const lzo_bytep buf, lzo_uint len)
return 0; return 0;
crc = (c & LZO_UINT32_C(0xffffffff)) ^ LZO_UINT32_C(0xffffffff); crc = (c & LZO_UINT32_C(0xffffffff)) ^ LZO_UINT32_C(0xffffffff);
if (len >= 16) do if (len >= 16) do
{ {
LZO_DO16(buf, 0); LZO_DO16(buf, 0);
buf += 16; buf += 16;
len -= 16; len -= 16;
} while (len >= 16); }
while (len >= 16);
if (len != 0) do if (len != 0) do
{ {
LZO_DO1(buf, 0); LZO_DO1(buf, 0);
buf += 1; buf += 1;
len -= 1; len -= 1;
} while (len > 0); }
while (len > 0);
return crc ^ LZO_UINT32_C(0xffffffff); return crc ^ LZO_UINT32_C(0xffffffff);
#undef table #undef table

View File

@ -40,7 +40,10 @@ BOOL FAR PASCAL LibMain ( HANDLE hInstance, WORD wDataSegment,
int __far __pascal LibMain(int a, short b, short c, long d) int __far __pascal LibMain(int a, short b, short c, long d)
#endif #endif
{ {
LZO_UNUSED(a); LZO_UNUSED(b); LZO_UNUSED(c); LZO_UNUSED(d); LZO_UNUSED(a);
LZO_UNUSED(b);
LZO_UNUSED(c);
LZO_UNUSED(d);
return 1; return 1;
} }

View File

@ -64,7 +64,9 @@
__lzo_static_forceinline unsigned lzo_bitops_ctlz32_func(lzo_uint32_t v) __lzo_static_forceinline unsigned lzo_bitops_ctlz32_func(lzo_uint32_t v)
{ {
#if (LZO_BITOPS_USE_MSC_BITSCAN) && (LZO_ARCH_AMD64 || LZO_ARCH_I386) #if (LZO_BITOPS_USE_MSC_BITSCAN) && (LZO_ARCH_AMD64 || LZO_ARCH_I386)
unsigned long r; (void) _BitScanReverse(&r, v); return (unsigned) r ^ 31; unsigned long r;
(void) _BitScanReverse(&r, v);
return (unsigned) r ^ 31;
#define lzo_bitops_ctlz32(v) lzo_bitops_ctlz32_func(v) #define lzo_bitops_ctlz32(v) lzo_bitops_ctlz32_func(v)
#elif (LZO_BITOPS_USE_ASM_BITSCAN) && (LZO_ARCH_AMD64 || LZO_ARCH_I386) && (LZO_ASM_SYNTAX_GNUC) #elif (LZO_BITOPS_USE_ASM_BITSCAN) && (LZO_ARCH_AMD64 || LZO_ARCH_I386) && (LZO_ASM_SYNTAX_GNUC)
lzo_uint32_t r; lzo_uint32_t r;
@ -72,13 +74,18 @@ __lzo_static_forceinline unsigned lzo_bitops_ctlz32_func(lzo_uint32_t v)
return (unsigned) r ^ 31; return (unsigned) r ^ 31;
#define lzo_bitops_ctlz32(v) lzo_bitops_ctlz32_func(v) #define lzo_bitops_ctlz32(v) lzo_bitops_ctlz32_func(v)
#elif (LZO_BITOPS_USE_GNUC_BITSCAN) && (LZO_SIZEOF_INT == 4) #elif (LZO_BITOPS_USE_GNUC_BITSCAN) && (LZO_SIZEOF_INT == 4)
unsigned r; r = (unsigned) __builtin_clz(v); return r; unsigned r;
r = (unsigned) __builtin_clz(v);
return r;
#define lzo_bitops_ctlz32(v) ((unsigned) __builtin_clz(v)) #define lzo_bitops_ctlz32(v) ((unsigned) __builtin_clz(v))
#elif (LZO_BITOPS_USE_GNUC_BITSCAN) && (LZO_SIZEOF_LONG == 8) && (LZO_WORDSIZE >= 8) #elif (LZO_BITOPS_USE_GNUC_BITSCAN) && (LZO_SIZEOF_LONG == 8) && (LZO_WORDSIZE >= 8)
unsigned r; r = (unsigned) __builtin_clzl(v); return r ^ 32; unsigned r;
r = (unsigned) __builtin_clzl(v);
return r ^ 32;
#define lzo_bitops_ctlz32(v) (((unsigned) __builtin_clzl(v)) ^ 32) #define lzo_bitops_ctlz32(v) (((unsigned) __builtin_clzl(v)) ^ 32)
#else #else
LZO_UNUSED(v); return 0; LZO_UNUSED(v);
return 0;
#endif #endif
} }
@ -86,7 +93,9 @@ __lzo_static_forceinline unsigned lzo_bitops_ctlz32_func(lzo_uint32_t v)
__lzo_static_forceinline unsigned lzo_bitops_ctlz64_func(lzo_uint64_t v) __lzo_static_forceinline unsigned lzo_bitops_ctlz64_func(lzo_uint64_t v)
{ {
#if (LZO_BITOPS_USE_MSC_BITSCAN) && (LZO_ARCH_AMD64) #if (LZO_BITOPS_USE_MSC_BITSCAN) && (LZO_ARCH_AMD64)
unsigned long r; (void) _BitScanReverse64(&r, v); return (unsigned) r ^ 63; unsigned long r;
(void) _BitScanReverse64(&r, v);
return (unsigned) r ^ 63;
#define lzo_bitops_ctlz64(v) lzo_bitops_ctlz64_func(v) #define lzo_bitops_ctlz64(v) lzo_bitops_ctlz64_func(v)
#elif (LZO_BITOPS_USE_ASM_BITSCAN) && (LZO_ARCH_AMD64) && (LZO_ASM_SYNTAX_GNUC) #elif (LZO_BITOPS_USE_ASM_BITSCAN) && (LZO_ARCH_AMD64) && (LZO_ASM_SYNTAX_GNUC)
lzo_uint64_t r; lzo_uint64_t r;
@ -94,13 +103,18 @@ __lzo_static_forceinline unsigned lzo_bitops_ctlz64_func(lzo_uint64_t v)
return (unsigned) r ^ 63; return (unsigned) r ^ 63;
#define lzo_bitops_ctlz64(v) lzo_bitops_ctlz64_func(v) #define lzo_bitops_ctlz64(v) lzo_bitops_ctlz64_func(v)
#elif (LZO_BITOPS_USE_GNUC_BITSCAN) && (LZO_SIZEOF_LONG == 8) && (LZO_WORDSIZE >= 8) #elif (LZO_BITOPS_USE_GNUC_BITSCAN) && (LZO_SIZEOF_LONG == 8) && (LZO_WORDSIZE >= 8)
unsigned r; r = (unsigned) __builtin_clzl(v); return r; unsigned r;
r = (unsigned) __builtin_clzl(v);
return r;
#define lzo_bitops_ctlz64(v) ((unsigned) __builtin_clzl(v)) #define lzo_bitops_ctlz64(v) ((unsigned) __builtin_clzl(v))
#elif (LZO_BITOPS_USE_GNUC_BITSCAN) && (LZO_SIZEOF_LONG_LONG == 8) && (LZO_WORDSIZE >= 8) #elif (LZO_BITOPS_USE_GNUC_BITSCAN) && (LZO_SIZEOF_LONG_LONG == 8) && (LZO_WORDSIZE >= 8)
unsigned r; r = (unsigned) __builtin_clzll(v); return r; unsigned r;
r = (unsigned) __builtin_clzll(v);
return r;
#define lzo_bitops_ctlz64(v) ((unsigned) __builtin_clzll(v)) #define lzo_bitops_ctlz64(v) ((unsigned) __builtin_clzll(v))
#else #else
LZO_UNUSED(v); return 0; LZO_UNUSED(v);
return 0;
#endif #endif
} }
#endif #endif
@ -108,7 +122,9 @@ __lzo_static_forceinline unsigned lzo_bitops_ctlz64_func(lzo_uint64_t v)
__lzo_static_forceinline unsigned lzo_bitops_cttz32_func(lzo_uint32_t v) __lzo_static_forceinline unsigned lzo_bitops_cttz32_func(lzo_uint32_t v)
{ {
#if (LZO_BITOPS_USE_MSC_BITSCAN) && (LZO_ARCH_AMD64 || LZO_ARCH_I386) #if (LZO_BITOPS_USE_MSC_BITSCAN) && (LZO_ARCH_AMD64 || LZO_ARCH_I386)
unsigned long r; (void) _BitScanForward(&r, v); return (unsigned) r; unsigned long r;
(void) _BitScanForward(&r, v);
return (unsigned) r;
#define lzo_bitops_cttz32(v) lzo_bitops_cttz32_func(v) #define lzo_bitops_cttz32(v) lzo_bitops_cttz32_func(v)
#elif (LZO_BITOPS_USE_ASM_BITSCAN) && (LZO_ARCH_AMD64 || LZO_ARCH_I386) && (LZO_ASM_SYNTAX_GNUC) #elif (LZO_BITOPS_USE_ASM_BITSCAN) && (LZO_ARCH_AMD64 || LZO_ARCH_I386) && (LZO_ASM_SYNTAX_GNUC)
lzo_uint32_t r; lzo_uint32_t r;
@ -116,10 +132,13 @@ __lzo_static_forceinline unsigned lzo_bitops_cttz32_func(lzo_uint32_t v)
return (unsigned) r; return (unsigned) r;
#define lzo_bitops_cttz32(v) lzo_bitops_cttz32_func(v) #define lzo_bitops_cttz32(v) lzo_bitops_cttz32_func(v)
#elif (LZO_BITOPS_USE_GNUC_BITSCAN) && (LZO_SIZEOF_INT >= 4) #elif (LZO_BITOPS_USE_GNUC_BITSCAN) && (LZO_SIZEOF_INT >= 4)
unsigned r; r = (unsigned) __builtin_ctz(v); return r; unsigned r;
r = (unsigned) __builtin_ctz(v);
return r;
#define lzo_bitops_cttz32(v) ((unsigned) __builtin_ctz(v)) #define lzo_bitops_cttz32(v) ((unsigned) __builtin_ctz(v))
#else #else
LZO_UNUSED(v); return 0; LZO_UNUSED(v);
return 0;
#endif #endif
} }
@ -127,7 +146,9 @@ __lzo_static_forceinline unsigned lzo_bitops_cttz32_func(lzo_uint32_t v)
__lzo_static_forceinline unsigned lzo_bitops_cttz64_func(lzo_uint64_t v) __lzo_static_forceinline unsigned lzo_bitops_cttz64_func(lzo_uint64_t v)
{ {
#if (LZO_BITOPS_USE_MSC_BITSCAN) && (LZO_ARCH_AMD64) #if (LZO_BITOPS_USE_MSC_BITSCAN) && (LZO_ARCH_AMD64)
unsigned long r; (void) _BitScanForward64(&r, v); return (unsigned) r; unsigned long r;
(void) _BitScanForward64(&r, v);
return (unsigned) r;
#define lzo_bitops_cttz64(v) lzo_bitops_cttz64_func(v) #define lzo_bitops_cttz64(v) lzo_bitops_cttz64_func(v)
#elif (LZO_BITOPS_USE_ASM_BITSCAN) && (LZO_ARCH_AMD64) && (LZO_ASM_SYNTAX_GNUC) #elif (LZO_BITOPS_USE_ASM_BITSCAN) && (LZO_ARCH_AMD64) && (LZO_ASM_SYNTAX_GNUC)
lzo_uint64_t r; lzo_uint64_t r;
@ -135,13 +156,18 @@ __lzo_static_forceinline unsigned lzo_bitops_cttz64_func(lzo_uint64_t v)
return (unsigned) r; return (unsigned) r;
#define lzo_bitops_cttz64(v) lzo_bitops_cttz64_func(v) #define lzo_bitops_cttz64(v) lzo_bitops_cttz64_func(v)
#elif (LZO_BITOPS_USE_GNUC_BITSCAN) && (LZO_SIZEOF_LONG >= 8) && (LZO_WORDSIZE >= 8) #elif (LZO_BITOPS_USE_GNUC_BITSCAN) && (LZO_SIZEOF_LONG >= 8) && (LZO_WORDSIZE >= 8)
unsigned r; r = (unsigned) __builtin_ctzl(v); return r; unsigned r;
r = (unsigned) __builtin_ctzl(v);
return r;
#define lzo_bitops_cttz64(v) ((unsigned) __builtin_ctzl(v)) #define lzo_bitops_cttz64(v) ((unsigned) __builtin_ctzl(v))
#elif (LZO_BITOPS_USE_GNUC_BITSCAN) && (LZO_SIZEOF_LONG_LONG >= 8) && (LZO_WORDSIZE >= 8) #elif (LZO_BITOPS_USE_GNUC_BITSCAN) && (LZO_SIZEOF_LONG_LONG >= 8) && (LZO_WORDSIZE >= 8)
unsigned r; r = (unsigned) __builtin_ctzll(v); return r; unsigned r;
r = (unsigned) __builtin_ctzll(v);
return r;
#define lzo_bitops_cttz64(v) ((unsigned) __builtin_ctzll(v)) #define lzo_bitops_cttz64(v) ((unsigned) __builtin_ctzll(v))
#else #else
LZO_UNUSED(v); return 0; LZO_UNUSED(v);
return 0;
#endif #endif
} }
#endif #endif

View File

@ -65,7 +65,8 @@
// //
************************************************************************/ ************************************************************************/
union lzo_config_check_union { union lzo_config_check_union
{
lzo_uint a[2]; lzo_uint a[2];
unsigned char b[2 * LZO_MAX(8, sizeof(lzo_uint))]; unsigned char b[2 * LZO_MAX(8, sizeof(lzo_uint))];
#if defined(lzo_uint64_t) #if defined(lzo_uint64_t)
@ -102,17 +103,20 @@ _lzo_config_check(void)
r &= ((* (lzo_bytep) p) == 0); r &= ((* (lzo_bytep) p) == 0);
#if !(LZO_CFG_NO_CONFIG_CHECK) #if !(LZO_CFG_NO_CONFIG_CHECK)
#if (LZO_ABI_BIG_ENDIAN) #if (LZO_ABI_BIG_ENDIAN)
u.a[0] = u.a[1] = 0; u.b[sizeof(lzo_uint) - 1] = 128; u.a[0] = u.a[1] = 0;
u.b[sizeof(lzo_uint) - 1] = 128;
p = u2p(&u, 0); p = u2p(&u, 0);
r &= ((* (lzo_uintp) p) == 128); r &= ((* (lzo_uintp) p) == 128);
#endif #endif
#if (LZO_ABI_LITTLE_ENDIAN) #if (LZO_ABI_LITTLE_ENDIAN)
u.a[0] = u.a[1] = 0; u.b[0] = 128; u.a[0] = u.a[1] = 0;
u.b[0] = 128;
p = u2p(&u, 0); p = u2p(&u, 0);
r &= ((* (lzo_uintp) p) == 128); r &= ((* (lzo_uintp) p) == 128);
#endif #endif
u.a[0] = u.a[1] = 0; u.a[0] = u.a[1] = 0;
u.b[0] = 1; u.b[3] = 2; u.b[0] = 1;
u.b[3] = 2;
p = u2p(&u, 1); p = u2p(&u, 1);
r &= UA_GET_NE16(p) == 0; r &= UA_GET_NE16(p) == 0;
r &= UA_GET_LE16(p) == 0; r &= UA_GET_LE16(p) == 0;
@ -127,13 +131,16 @@ _lzo_config_check(void)
r &= UA_GET_NE16(p) == LZO_UINT16_C(0x8180); r &= UA_GET_NE16(p) == LZO_UINT16_C(0x8180);
#endif #endif
u.a[0] = u.a[1] = 0; u.a[0] = u.a[1] = 0;
u.b[0] = 3; u.b[5] = 4; u.b[0] = 3;
u.b[5] = 4;
p = u2p(&u, 1); p = u2p(&u, 1);
r &= UA_GET_NE32(p) == 0; r &= UA_GET_NE32(p) == 0;
r &= UA_GET_LE32(p) == 0; r &= UA_GET_LE32(p) == 0;
u.b[1] = 128; u.b[1] = 128;
r &= UA_GET_LE32(p) == 128; r &= UA_GET_LE32(p) == 128;
u.b[2] = 129; u.b[3] = 130; u.b[4] = 131; u.b[2] = 129;
u.b[3] = 130;
u.b[4] = 131;
r &= UA_GET_LE32(p) == LZO_UINT32_C(0x83828180); r &= UA_GET_LE32(p) == LZO_UINT32_C(0x83828180);
#if (LZO_ABI_BIG_ENDIAN) #if (LZO_ABI_BIG_ENDIAN)
r &= UA_GET_NE32(p) == LZO_UINT32_C(0x80818283); r &= UA_GET_NE32(p) == LZO_UINT32_C(0x80818283);
@ -143,7 +150,8 @@ _lzo_config_check(void)
#endif #endif
#if defined(UA_GET_NE64) #if defined(UA_GET_NE64)
u.c[0] = u.c[1] = 0; u.c[0] = u.c[1] = 0;
u.b[0] = 5; u.b[9] = 6; u.b[0] = 5;
u.b[9] = 6;
p = u2p(&u, 1); p = u2p(&u, 1);
u.c[0] = u.c[1] = 0; u.c[0] = u.c[1] = 0;
r &= UA_GET_NE64(p) == 0; r &= UA_GET_NE64(p) == 0;
@ -154,32 +162,52 @@ _lzo_config_check(void)
#endif #endif
#endif #endif
#if defined(lzo_bitops_ctlz32) #if defined(lzo_bitops_ctlz32)
{ unsigned i = 0; lzo_uint32_t v; {
for (v = 1; v != 0 && r == 1; v <<= 1, i++) { unsigned i = 0;
lzo_uint32_t v;
for (v = 1; v != 0 && r == 1; v <<= 1, i++)
{
r &= lzo_bitops_ctlz32(v) == 31 - i; r &= lzo_bitops_ctlz32(v) == 31 - i;
r &= lzo_bitops_ctlz32_func(v) == 31 - i; r &= lzo_bitops_ctlz32_func(v) == 31 - i;
}} }
}
#endif #endif
#if defined(lzo_bitops_ctlz64) #if defined(lzo_bitops_ctlz64)
{ unsigned i = 0; lzo_uint64_t v; {
for (v = 1; v != 0 && r == 1; v <<= 1, i++) { unsigned i = 0;
lzo_uint64_t v;
for (v = 1; v != 0 && r == 1; v <<= 1, i++)
{
r &= lzo_bitops_ctlz64(v) == 63 - i; r &= lzo_bitops_ctlz64(v) == 63 - i;
r &= lzo_bitops_ctlz64_func(v) == 63 - i; r &= lzo_bitops_ctlz64_func(v) == 63 - i;
}} }
}
#endif #endif
#if defined(lzo_bitops_cttz32) #if defined(lzo_bitops_cttz32)
{ unsigned i = 0; lzo_uint32_t v; {
for (v = 1; v != 0 && r == 1; v <<= 1, i++) { unsigned i = 0;
lzo_uint32_t v;
for (v = 1; v != 0 && r == 1; v <<= 1, i++)
{
r &= lzo_bitops_cttz32(v) == i; r &= lzo_bitops_cttz32(v) == i;
r &= lzo_bitops_cttz32_func(v) == i; r &= lzo_bitops_cttz32_func(v) == i;
}} }
}
#endif #endif
#if defined(lzo_bitops_cttz64) #if defined(lzo_bitops_cttz64)
{ unsigned i = 0; lzo_uint64_t v; {
for (v = 1; v != 0 && r == 1; v <<= 1, i++) { unsigned i = 0;
lzo_uint64_t v;
for (v = 1; v != 0 && r == 1; v <<= 1, i++)
{
r &= lzo_bitops_cttz64(v) == i; r &= lzo_bitops_cttz64(v) == i;
r &= lzo_bitops_cttz64_func(v) == i; r &= lzo_bitops_cttz64_func(v) == i;
}} }
}
#endif #endif
#endif #endif
LZO_UNUSED_FUNC(lzo_bitops_unused_funcs); LZO_UNUSED_FUNC(lzo_bitops_unused_funcs);
@ -220,10 +248,12 @@ __lzo_init_v2(unsigned v, int s1, int s2, int s3, int s4, int s5,
(s7 == -1 || s7 == (int) sizeof(char*)) && (s7 == -1 || s7 == (int) sizeof(char*)) &&
(s8 == -1 || s8 == (int) sizeof(lzo_voidp)) && (s8 == -1 || s8 == (int) sizeof(lzo_voidp)) &&
(s9 == -1 || s9 == (int) sizeof(lzo_callback_t)); (s9 == -1 || s9 == (int) sizeof(lzo_callback_t));
if (!r) if (!r)
return LZO_E_ERROR; return LZO_E_ERROR;
r = _lzo_config_check(); r = _lzo_config_check();
if (r != LZO_E_OK) if (r != LZO_E_OK)
return r; return r;

View File

@ -128,6 +128,7 @@ init_match ( LZO_COMPRESS_T *c, lzo_swd_p s,
c->lazy = 0; c->lazy = 0;
r = swd_init(s, dict, dict_len); r = swd_init(s, dict, dict_len);
if (r != LZO_E_OK) if (r != LZO_E_OK)
{ {
swd_exit(s); swd_exit(s);
@ -164,8 +165,10 @@ find_match ( LZO_COMPRESS_T *c, lzo_swd_p s,
s->m_len = SWD_THRESHOLD; s->m_len = SWD_THRESHOLD;
s->m_off = 0; s->m_off = 0;
#ifdef SWD_BEST_OFF #ifdef SWD_BEST_OFF
if (s->use_best_off) if (s->use_best_off)
lzo_memset(s->best_pos, 0, sizeof(s->best_pos)); lzo_memset(s->best_pos, 0, sizeof(s->best_pos));
#endif #endif
swd_findbest(s); swd_findbest(s);
c->m_len = s->m_len; c->m_len = s->m_len;
@ -183,9 +186,11 @@ find_match ( LZO_COMPRESS_T *c, lzo_swd_p s,
{ {
c->look = s->look + 1; c->look = s->look + 1;
} }
c->bp = c->ip - c->look; c->bp = c->ip - c->look;
#if 0 #if 0
/* brute force match search */ /* brute force match search */
if (c->m_len > SWD_THRESHOLD && c->m_len + 1 <= c->look) if (c->m_len > SWD_THRESHOLD && c->m_len + 1 <= c->look)
{ {
@ -195,18 +200,23 @@ find_match ( LZO_COMPRESS_T *c, lzo_swd_p s,
if (ip - in > s->swd_n) if (ip - in > s->swd_n)
in = ip - s->swd_n; in = ip - s->swd_n;
for (;;) for (;;)
{ {
while (*in != *ip) while (*in != *ip)
in++; in++;
if (in == ip) if (in == ip)
break; break;
if (in != m) if (in != m)
if (lzo_memcmp(in, ip, c->m_len + 1) == 0) if (lzo_memcmp(in, ip, c->m_len + 1) == 0)
printf("%p %p %p %5d\n", in, ip, m, c->m_len); printf("%p %p %p %5d\n", in, ip, m, c->m_len);
in++; in++;
} }
} }
#endif #endif
if (c->cb && c->cb->nprogress && c->textsize > c->printcount) if (c->cb && c->cb->nprogress && c->textsize > c->printcount)

View File

@ -61,14 +61,19 @@ __lzo_align_gap(const lzo_voidp ptr, lzo_uint size)
#error "__LZO_UINTPTR_T_IS_POINTER is unsupported" #error "__LZO_UINTPTR_T_IS_POINTER is unsupported"
#else #else
lzo_uintptr_t p, n; lzo_uintptr_t p, n;
if (size < 2) return 0; if (size < 2) return 0;
p = __lzo_ptr_linear(ptr); p = __lzo_ptr_linear(ptr);
#if 0 #if 0
n = (((p + size - 1) / size) * size) - p; n = (((p + size - 1) / size) * size) - p;
#else #else
if ((size & (size - 1)) != 0) if ((size & (size - 1)) != 0)
return 0; return 0;
n = size; n = ((p + n - 1) & ~(n - 1)) - p;
n = size;
n = ((p + n - 1) & ~(n - 1)) - p;
#endif #endif
#endif #endif
assert((long)n >= 0); assert((long)n >= 0);

File diff suppressed because it is too large Load Diff

View File

@ -189,6 +189,7 @@ void swd_initdict(lzo_swd_p s, const lzo_bytep dict, lzo_uint dict_len)
if (!dict || dict_len == 0) if (!dict || dict_len == 0)
return; return;
if (dict_len > s->swd_n) if (dict_len > s->swd_n)
{ {
dict += dict_len - s->swd_n; dict += dict_len - s->swd_n;
@ -221,7 +222,8 @@ void swd_insertdict(lzo_swd_p s, lzo_uint node, lzo_uint len)
assert(s_llen3(s)[key] <= s->swd_n); assert(s_llen3(s)[key] <= s->swd_n);
#ifdef HEAD2 #ifdef HEAD2
IF_HEAD2(s) { IF_HEAD2(s)
{
key = HEAD2(s_b(s), node); key = HEAD2(s_b(s), node);
s_head2(s)[key] = SWD_UINT(node); s_head2(s)[key] = SWD_UINT(node);
} }
@ -255,15 +257,19 @@ int swd_init(lzo_swd_p s, const lzo_bytep dict, lzo_uint dict_len)
r &= s->best3 != NULL; r &= s->best3 != NULL;
r &= s->llen3 != NULL; r &= s->llen3 != NULL;
#ifdef HEAD2 #ifdef HEAD2
IF_HEAD2(s) { IF_HEAD2(s)
{
s->head2 = (swd_uintp) malloc(sizeof(swd_uint) * 65536L); s->head2 = (swd_uintp) malloc(sizeof(swd_uint) * 65536L);
r &= s->head2 != NULL; r &= s->head2 != NULL;
} }
#endif #endif
if (r != 1) {
if (r != 1)
{
swd_exit(s); swd_exit(s);
return LZO_E_OUT_OF_MEMORY; return LZO_E_OUT_OF_MEMORY;
} }
#endif #endif
s->m_len = 0; s->m_len = 0;
@ -271,6 +277,7 @@ int swd_init(lzo_swd_p s, const lzo_bytep dict, lzo_uint dict_len)
#if defined(SWD_BEST_OFF) #if defined(SWD_BEST_OFF)
{ {
unsigned i; unsigned i;
for (i = 0; i < SWD_BEST_OFF; i++) for (i = 0; i < SWD_BEST_OFF; i++)
s->best_off[i] = s->best_pos[i] = 0; s->best_off[i] = s->best_pos[i] = 0;
} }
@ -288,8 +295,10 @@ int swd_init(lzo_swd_p s, const lzo_bytep dict, lzo_uint dict_len)
s->b_size = s->swd_n + s->swd_f; s->b_size = s->swd_n + s->swd_f;
#if 0 #if 0
if (2 * s->swd_f >= s->swd_n || s->b_size + s->swd_f >= SWD_UINT_MAX) if (2 * s->swd_f >= s->swd_n || s->b_size + s->swd_f >= SWD_UINT_MAX)
return LZO_E_ERROR; return LZO_E_ERROR;
#else #else
LZO_COMPILE_TIME_ASSERT(!(0ul + 2 * SWD_F >= SWD_N)) LZO_COMPILE_TIME_ASSERT(!(0ul + 2 * SWD_F >= SWD_N))
LZO_COMPILE_TIME_ASSERT(!(0ul + SWD_N + SWD_F + SWD_F >= SWD_UINT_MAX)) LZO_COMPILE_TIME_ASSERT(!(0ul + SWD_N + SWD_F + SWD_F >= SWD_UINT_MAX))
@ -299,14 +308,17 @@ int swd_init(lzo_swd_p s, const lzo_bytep dict, lzo_uint dict_len)
lzo_memset(s_llen3(s), 0, (lzo_uint)sizeof(s_llen3(s)[0]) * (lzo_uint)SWD_HSIZE); lzo_memset(s_llen3(s), 0, (lzo_uint)sizeof(s_llen3(s)[0]) * (lzo_uint)SWD_HSIZE);
#ifdef HEAD2 #ifdef HEAD2
IF_HEAD2(s) { IF_HEAD2(s)
{
#if 1 #if 1
lzo_memset(s_head2(s), 0xff, (lzo_uint)sizeof(s_head2(s)[0]) * 65536L); lzo_memset(s_head2(s), 0xff, (lzo_uint)sizeof(s_head2(s)[0]) * 65536L);
assert(s_head2(s)[0] == NIL2); assert(s_head2(s)[0] == NIL2);
#else #else
lzo_xint i; lzo_xint i;
for (i = 0; i < 65536L; i++) for (i = 0; i < 65536L; i++)
s_head2(s)[i] = NIL2; s_head2(s)[i] = NIL2;
#endif #endif
} }
#endif #endif
@ -319,26 +331,34 @@ int swd_init(lzo_swd_p s, const lzo_bytep dict, lzo_uint dict_len)
assert(s->ip + s->swd_f <= s->b_size); assert(s->ip + s->swd_f <= s->b_size);
#if 1 #if 1
s->look = (lzo_uint)(s->c->in_end - s->c->ip); s->look = (lzo_uint)(s->c->in_end - s->c->ip);
if (s->look > 0) if (s->look > 0)
{ {
if (s->look > s->swd_f) if (s->look > s->swd_f)
s->look = s->swd_f; s->look = s->swd_f;
lzo_memcpy(&s_b(s)[s->ip], s->c->ip, s->look); lzo_memcpy(&s_b(s)[s->ip], s->c->ip, s->look);
s->c->ip += s->look; s->c->ip += s->look;
s->ip += s->look; s->ip += s->look;
} }
#else #else
s->look = 0; s->look = 0;
while (s->look < s->swd_f) while (s->look < s->swd_f)
{ {
int c; int c;
if ((c = getbyte(*(s->c))) < 0) if ((c = getbyte(*(s->c))) < 0)
break; break;
s_b(s)[s->ip] = LZO_BYTE(c); s_b(s)[s->ip] = LZO_BYTE(c);
s->ip++; s->ip++;
s->look++; s->look++;
} }
#endif #endif
if (s->ip == s->b_size) if (s->ip == s->b_size)
s->ip = 0; s->ip = 0;
@ -346,18 +366,22 @@ int swd_init(lzo_swd_p s, const lzo_bytep dict, lzo_uint dict_len)
swd_insertdict(s, 0, s->dict_len); swd_insertdict(s, 0, s->dict_len);
s->rp = s->first_rp; s->rp = s->first_rp;
if (s->rp >= s->node_count) if (s->rp >= s->node_count)
s->rp -= s->node_count; s->rp -= s->node_count;
else else
s->rp += s->b_size - s->node_count; s->rp += s->b_size - s->node_count;
#if 1 || defined(__LZO_CHECKER) #if 1 || defined(__LZO_CHECKER)
/* initialize memory for the first few HEAD3 (if s->ip is not far /* initialize memory for the first few HEAD3 (if s->ip is not far
* enough ahead to do this job for us). The value doesn't matter. */ * enough ahead to do this job for us). The value doesn't matter. */
if (s->look < 3) { if (s->look < 3)
{
lzo_bytep p = &s_b(s)[s->bp + s->look]; lzo_bytep p = &s_b(s)[s->bp + s->look];
p[0] = p[1] = p[2] = 0; p[0] = p[1] = p[2] = 0;
} }
#endif #endif
return LZO_E_OK; return LZO_E_OK;
@ -370,13 +394,19 @@ void swd_exit(lzo_swd_p s)
#if defined(__LZO_CHECKER) #if defined(__LZO_CHECKER)
/* free in reverse order of allocations */ /* free in reverse order of allocations */
#ifdef HEAD2 #ifdef HEAD2
free(s->head2); s->head2 = NULL; free(s->head2);
s->head2 = NULL;
#endif #endif
free(s->llen3); s->llen3 = NULL; free(s->llen3);
free(s->best3); s->best3 = NULL; s->llen3 = NULL;
free(s->succ3); s->succ3 = NULL; free(s->best3);
free(s->head3); s->head3 = NULL; s->best3 = NULL;
free(s->b); s->b = NULL; free(s->succ3);
s->succ3 = NULL;
free(s->head3);
s->head3 = NULL;
free(s->b);
s->b = NULL;
#else #else
LZO_UNUSED(s); LZO_UNUSED(s);
#endif #endif
@ -400,23 +430,30 @@ void swd_getbyte(lzo_swd_p s)
{ {
if (s->look > 0) if (s->look > 0)
--s->look; --s->look;
#if 1 || defined(__LZO_CHECKER) #if 1 || defined(__LZO_CHECKER)
/* initialize memory - value doesn't matter */ /* initialize memory - value doesn't matter */
s_b(s)[s->ip] = 0; s_b(s)[s->ip] = 0;
if (s->ip < s->swd_f) if (s->ip < s->swd_f)
s->b_wrap[s->ip] = 0; s->b_wrap[s->ip] = 0;
#endif #endif
} }
else else
{ {
s_b(s)[s->ip] = LZO_BYTE(c); s_b(s)[s->ip] = LZO_BYTE(c);
if (s->ip < s->swd_f) if (s->ip < s->swd_f)
s->b_wrap[s->ip] = LZO_BYTE(c); s->b_wrap[s->ip] = LZO_BYTE(c);
} }
if (++s->ip == s->b_size) if (++s->ip == s->b_size)
s->ip = 0; s->ip = 0;
if (++s->bp == s->b_size) if (++s->bp == s->b_size)
s->bp = 0; s->bp = 0;
if (++s->rp == s->b_size) if (++s->rp == s->b_size)
s->rp = 0; s->rp = 0;
} }
@ -434,6 +471,7 @@ void swd_remove_node(lzo_swd_p s, lzo_uint node)
lzo_uint key; lzo_uint key;
#ifdef LZO_DEBUG #ifdef LZO_DEBUG
if (s->first_rp != LZO_UINT_MAX) if (s->first_rp != LZO_UINT_MAX)
{ {
if (node != s->first_rp) if (node != s->first_rp)
@ -441,9 +479,11 @@ void swd_remove_node(lzo_swd_p s, lzo_uint node)
(long)node, (long)s->rp, (long)s->ip, (long)s->bp, (long)node, (long)s->rp, (long)s->ip, (long)s->bp,
(long)s->first_rp, (long)(s->ip - node), (long)s->first_rp, (long)(s->ip - node),
(long)(s->ip - s->bp)); (long)(s->ip - s->bp));
assert(node == s->first_rp); assert(node == s->first_rp);
s->first_rp = LZO_UINT_MAX; s->first_rp = LZO_UINT_MAX;
} }
#endif #endif
key = HEAD3(s_b(s), node); key = HEAD3(s_b(s), node);
@ -451,9 +491,11 @@ void swd_remove_node(lzo_swd_p s, lzo_uint node)
--s_llen3(s)[key]; --s_llen3(s)[key];
#ifdef HEAD2 #ifdef HEAD2
IF_HEAD2(s) { IF_HEAD2(s)
{
key = HEAD2(s_b(s), node); key = HEAD2(s_b(s), node);
assert(s_head2(s)[key] != NIL2); assert(s_head2(s)[key] != NIL2);
if ((lzo_uint) s_head2(s)[key] == node) if ((lzo_uint) s_head2(s)[key] == node)
s_head2(s)[key] = NIL2; s_head2(s)[key] = NIL2;
} }
@ -489,14 +531,16 @@ void swd_accept(lzo_swd_p s, lzo_uint n)
#ifdef HEAD2 #ifdef HEAD2
/* add bp into HEAD2 */ /* add bp into HEAD2 */
IF_HEAD2(s) { IF_HEAD2(s)
{
key = HEAD2(s_b(s), s->bp); key = HEAD2(s_b(s), s->bp);
s_head2(s)[key] = SWD_UINT(s->bp); s_head2(s)[key] = SWD_UINT(s->bp);
} }
#endif #endif
swd_getbyte(s); swd_getbyte(s);
} while (--n != 0); }
while (--n != 0);
} }
@ -519,6 +563,7 @@ void swd_search(lzo_swd_p s, lzo_uint node, lzo_uint cnt)
assert(s->m_len > 0); assert(s->m_len > 0);
scan_end1 = bp[m_len - 1]; scan_end1 = bp[m_len - 1];
for (; cnt-- > 0; node = s_succ3(s)[node]) for (; cnt-- > 0; node = s_succ3(s)[node])
{ {
p1 = bp; p1 = bp;
@ -539,42 +584,59 @@ void swd_search(lzo_swd_p s, lzo_uint node, lzo_uint cnt)
assert(lzo_memcmp(bp, &b[node], 3) == 0); assert(lzo_memcmp(bp, &b[node], 3) == 0);
#if 0 && (LZO_OPT_UNALIGNED32) #if 0 && (LZO_OPT_UNALIGNED32)
p1 += 3; p2 += 3; p1 += 3;
p2 += 3;
while (p1 + 4 <= px && UA_GET_NE32(p1) == UA_GET_NE32(p2)) while (p1 + 4 <= px && UA_GET_NE32(p1) == UA_GET_NE32(p2))
p1 += 4, p2 += 4; p1 += 4, p2 += 4;
while (p1 < px && *p1 == *p2) while (p1 < px && *p1 == *p2)
p1 += 1, p2 += 1; p1 += 1, p2 += 1;
#else #else
p1 += 2; p2 += 2; p1 += 2;
do {} while (++p1 < px && *p1 == *++p2); p2 += 2;
do {}
while (++p1 < px && *p1 == *++p2);
#endif #endif
i = pd(p1, bp); i = pd(p1, bp);
#ifdef LZO_DEBUG #ifdef LZO_DEBUG
if (lzo_memcmp(bp, &b[node], i) != 0) if (lzo_memcmp(bp, &b[node], i) != 0)
printf("%5ld %5ld %5ld %02x/%02x %02x/%02x\n", printf("%5ld %5ld %5ld %02x/%02x %02x/%02x\n",
(long)s->bp, (long) node, (long) i, (long)s->bp, (long) node, (long) i,
bp[0], bp[1], b[node], b[node + 1]); bp[0], bp[1], b[node], b[node + 1]);
#endif #endif
assert(lzo_memcmp(bp, &b[node], i) == 0); assert(lzo_memcmp(bp, &b[node], i) == 0);
#if defined(SWD_BEST_OFF) #if defined(SWD_BEST_OFF)
if (i < SWD_BEST_OFF) if (i < SWD_BEST_OFF)
{ {
if (s->best_pos[i] == 0) if (s->best_pos[i] == 0)
s->best_pos[i] = node + 1; s->best_pos[i] = node + 1;
} }
#endif #endif
if (i > m_len) if (i > m_len)
{ {
s->m_len = m_len = i; s->m_len = m_len = i;
s->m_pos = node; s->m_pos = node;
if (m_len == s->look) if (m_len == s->look)
return; return;
if (m_len >= s->nice_length) if (m_len >= s->nice_length)
return; return;
if (m_len > (lzo_uint) s_best3(s)[node]) if (m_len > (lzo_uint) s_best3(s)[node])
return; return;
scan_end1 = bp[m_len - 1]; scan_end1 = bp[m_len - 1];
} }
} }
@ -597,17 +659,23 @@ lzo_bool swd_search2(lzo_swd_p s)
assert(s->m_len > 0); assert(s->m_len > 0);
key = s_head2(s)[ HEAD2(s_b(s), s->bp) ]; key = s_head2(s)[ HEAD2(s_b(s), s->bp) ];
if (key == NIL2) if (key == NIL2)
return 0; return 0;
#ifdef LZO_DEBUG #ifdef LZO_DEBUG
if (lzo_memcmp(&s_b(s)[s->bp], &s_b(s)[key], 2) != 0) if (lzo_memcmp(&s_b(s)[s->bp], &s_b(s)[key], 2) != 0)
printf("%5ld %5ld %02x/%02x %02x/%02x\n", (long)s->bp, (long)key, printf("%5ld %5ld %02x/%02x %02x/%02x\n", (long)s->bp, (long)key,
s_b(s)[s->bp], s_b(s)[s->bp + 1], s_b(s)[key], s_b(s)[key + 1]); s_b(s)[s->bp], s_b(s)[s->bp + 1], s_b(s)[key], s_b(s)[key + 1]);
#endif #endif
assert(lzo_memcmp(&s_b(s)[s->bp], &s_b(s)[key], 2) == 0); assert(lzo_memcmp(&s_b(s)[s->bp], &s_b(s)[key], 2) == 0);
#if defined(SWD_BEST_OFF) #if defined(SWD_BEST_OFF)
if (s->best_pos[2] == 0) if (s->best_pos[2] == 0)
s->best_pos[2] = key + 1; s->best_pos[2] = key + 1;
#endif #endif
if (s->m_len < 2) if (s->m_len < 2)
@ -615,6 +683,7 @@ lzo_bool swd_search2(lzo_swd_p s)
s->m_len = 2; s->m_len = 2;
s->m_pos = key; s->m_pos = key;
} }
return 1; return 1;
} }
@ -639,42 +708,55 @@ void swd_findbest(lzo_swd_p s)
node = s_succ3(s)[s->bp] = s_get_head3(s, key); node = s_succ3(s)[s->bp] = s_get_head3(s, key);
cnt = s_llen3(s)[key]++; cnt = s_llen3(s)[key]++;
assert(s_llen3(s)[key] <= s->swd_n + s->swd_f); assert(s_llen3(s)[key] <= s->swd_n + s->swd_f);
if (cnt > s->max_chain && s->max_chain > 0) if (cnt > s->max_chain && s->max_chain > 0)
cnt = s->max_chain; cnt = s->max_chain;
s_head3(s)[key] = SWD_UINT(s->bp); s_head3(s)[key] = SWD_UINT(s->bp);
s->b_char = s_b(s)[s->bp]; s->b_char = s_b(s)[s->bp];
len = s->m_len; len = s->m_len;
if (s->m_len >= s->look) if (s->m_len >= s->look)
{ {
if (s->look == 0) if (s->look == 0)
s->b_char = -1; s->b_char = -1;
s->m_off = 0; s->m_off = 0;
s_best3(s)[s->bp] = SWD_UINT(s->swd_f + 1); s_best3(s)[s->bp] = SWD_UINT(s->swd_f + 1);
} }
else else
{ {
#if defined(HEAD2) #if defined(HEAD2)
if (swd_search2(s) && s->look >= 3) if (swd_search2(s) && s->look >= 3)
swd_search(s, node, cnt); swd_search(s, node, cnt);
#else #else
if (s->look >= 3) if (s->look >= 3)
swd_search(s, node, cnt); swd_search(s, node, cnt);
#endif #endif
if (s->m_len > len) if (s->m_len > len)
s->m_off = swd_pos2off(s, s->m_pos); s->m_off = swd_pos2off(s, s->m_pos);
s_best3(s)[s->bp] = SWD_UINT(s->m_len); s_best3(s)[s->bp] = SWD_UINT(s->m_len);
#if defined(SWD_BEST_OFF) #if defined(SWD_BEST_OFF)
if (s->use_best_off) if (s->use_best_off)
{ {
unsigned i; unsigned i;
for (i = 2; i < SWD_BEST_OFF; i++) for (i = 2; i < SWD_BEST_OFF; i++)
if (s->best_pos[i] > 0) if (s->best_pos[i] > 0)
s->best_off[i] = swd_pos2off(s, s->best_pos[i] - 1); s->best_off[i] = swd_pos2off(s, s->best_pos[i] - 1);
else else
s->best_off[i] = 0; s->best_off[i] = 0;
} }
#endif #endif
} }
@ -682,7 +764,8 @@ void swd_findbest(lzo_swd_p s)
#ifdef HEAD2 #ifdef HEAD2
/* add bp into HEAD2 */ /* add bp into HEAD2 */
IF_HEAD2(s) { IF_HEAD2(s)
{
key = HEAD2(s_b(s), s->bp); key = HEAD2(s_b(s), s->bp);
s_head2(s)[key] = SWD_UINT(s->bp); s_head2(s)[key] = SWD_UINT(s->bp);
} }

View File

@ -121,20 +121,26 @@ lzo_adler32(lzo_uint32_t adler, const lzo_bytep buf, lzo_uint len)
{ {
k = len < LZO_NMAX ? (unsigned) len : LZO_NMAX; k = len < LZO_NMAX ? (unsigned) len : LZO_NMAX;
len -= k; len -= k;
if (k >= 16) do if (k >= 16) do
{ {
LZO_DO16(buf, 0); LZO_DO16(buf, 0);
buf += 16; buf += 16;
k -= 16; k -= 16;
} while (k >= 16); }
while (k >= 16);
if (k != 0) do if (k != 0) do
{ {
s1 += *buf++; s1 += *buf++;
s2 += s1; s2 += s1;
} while (--k > 0); }
while (--k > 0);
s1 %= LZO_BASE; s1 %= LZO_BASE;
s2 %= LZO_BASE; s2 %= LZO_BASE;
} }
return (s2 << 16) | s1; return (s2 << 16) | s1;
} }

View File

@ -57,7 +57,8 @@ extern "C" {
// //
************************************************************************/ ************************************************************************/
typedef struct { typedef struct
{
/* configuration */ /* configuration */
unsigned rbits; unsigned rbits;

42
extern/zlib/adler32.c vendored
View File

@ -79,13 +79,18 @@ uLong ZEXPORT adler32(adler, buf, len)
adler &= 0xffff; adler &= 0xffff;
/* in case user likes doing a byte at a time, keep it fast */ /* in case user likes doing a byte at a time, keep it fast */
if (len == 1) { if (len == 1)
{
adler += buf[0]; adler += buf[0];
if (adler >= BASE) if (adler >= BASE)
adler -= BASE; adler -= BASE;
sum2 += adler; sum2 += adler;
if (sum2 >= BASE) if (sum2 >= BASE)
sum2 -= BASE; sum2 -= BASE;
return adler | (sum2 << 16); return adler | (sum2 << 16);
} }
@ -94,40 +99,54 @@ uLong ZEXPORT adler32(adler, buf, len)
return 1L; return 1L;
/* in case short lengths are provided, keep it somewhat fast */ /* in case short lengths are provided, keep it somewhat fast */
if (len < 16) { if (len < 16)
while (len--) { {
while (len--)
{
adler += *buf++; adler += *buf++;
sum2 += adler; sum2 += adler;
} }
if (adler >= BASE) if (adler >= BASE)
adler -= BASE; adler -= BASE;
MOD28(sum2); /* only added so many BASE's */ MOD28(sum2); /* only added so many BASE's */
return adler | (sum2 << 16); return adler | (sum2 << 16);
} }
/* do length NMAX blocks -- requires just one modulo operation */ /* do length NMAX blocks -- requires just one modulo operation */
while (len >= NMAX) { while (len >= NMAX)
{
len -= NMAX; len -= NMAX;
n = NMAX / 16; /* NMAX is divisible by 16 */ n = NMAX / 16; /* NMAX is divisible by 16 */
do {
do
{
DO16(buf); /* 16 sums unrolled */ DO16(buf); /* 16 sums unrolled */
buf += 16; buf += 16;
} while (--n); }
while (--n);
MOD(adler); MOD(adler);
MOD(sum2); MOD(sum2);
} }
/* do remaining bytes (less than NMAX, still just one modulo) */ /* do remaining bytes (less than NMAX, still just one modulo) */
if (len) { /* avoid modulos if none remaining */ if (len) /* avoid modulos if none remaining */
while (len >= 16) { {
while (len >= 16)
{
len -= 16; len -= 16;
DO16(buf); DO16(buf);
buf += 16; buf += 16;
} }
while (len--) {
while (len--)
{
adler += *buf++; adler += *buf++;
sum2 += adler; sum2 += adler;
} }
MOD(adler); MOD(adler);
MOD(sum2); MOD(sum2);
} }
@ -162,10 +181,15 @@ local uLong adler32_combine_(adler1, adler2, len2)
MOD(sum2); MOD(sum2);
sum1 += (adler2 & 0xffff) + BASE - 1; sum1 += (adler2 & 0xffff) + BASE - 1;
sum2 += ((adler1 >> 16) & 0xffff) + ((adler2 >> 16) & 0xffff) + BASE - rem; sum2 += ((adler1 >> 16) & 0xffff) + ((adler2 >> 16) & 0xffff) + BASE - rem;
if (sum1 >= BASE) sum1 -= BASE; if (sum1 >= BASE) sum1 -= BASE;
if (sum1 >= BASE) sum1 -= BASE; if (sum1 >= BASE) sum1 -= BASE;
if (sum2 >= (BASE << 1)) sum2 -= (BASE << 1); if (sum2 >= (BASE << 1)) sum2 -= (BASE << 1);
if (sum2 >= BASE) sum2 -= BASE; if (sum2 >= BASE) sum2 -= BASE;
return sum1 | (sum2 << 16); return sum1 | (sum2 << 16);
} }

View File

@ -36,11 +36,14 @@ int ZEXPORT compress2 (dest, destLen, source, sourceLen, level)
stream.next_in = (Bytef*)source; stream.next_in = (Bytef*)source;
stream.avail_in = (uInt)sourceLen; stream.avail_in = (uInt)sourceLen;
#ifdef MAXSEG_64K #ifdef MAXSEG_64K
/* Check for source > 64K on 16-bit machine: */ /* Check for source > 64K on 16-bit machine: */
if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR; if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
#endif #endif
stream.next_out = dest; stream.next_out = dest;
stream.avail_out = (uInt) * destLen; stream.avail_out = (uInt) * destLen;
if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR; if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
stream.zalloc = (alloc_func)0; stream.zalloc = (alloc_func)0;
@ -48,13 +51,17 @@ int ZEXPORT compress2 (dest, destLen, source, sourceLen, level)
stream.opaque = (voidpf)0; stream.opaque = (voidpf)0;
err = deflateInit(&stream, level); err = deflateInit(&stream, level);
if (err != Z_OK) return err; if (err != Z_OK) return err;
err = deflate(&stream, Z_FINISH); err = deflate(&stream, Z_FINISH);
if (err != Z_STREAM_END) {
if (err != Z_STREAM_END)
{
deflateEnd(&stream); deflateEnd(&stream);
return err == Z_OK ? Z_BUF_ERROR : err; return err == Z_OK ? Z_BUF_ERROR : err;
} }
*destLen = stream.total_out; *destLen = stream.total_out;
err = deflateEnd(&stream); err = deflateEnd(&stream);

114
extern/zlib/crc32.c vendored
View File

@ -99,39 +99,50 @@ local void make_crc_table()
/* See if another task is already doing this (not thread-safe, but better /* See if another task is already doing this (not thread-safe, but better
than nothing -- significantly reduces duration of vulnerability in than nothing -- significantly reduces duration of vulnerability in
case the advice about DYNAMIC_CRC_TABLE is ignored) */ case the advice about DYNAMIC_CRC_TABLE is ignored) */
if (first) { if (first)
{
first = 0; first = 0;
/* make exclusive-or pattern from polynomial (0xedb88320UL) */ /* make exclusive-or pattern from polynomial (0xedb88320UL) */
poly = 0; poly = 0;
for (n = 0; n < (int)(sizeof(p) / sizeof(unsigned char)); n++) for (n = 0; n < (int)(sizeof(p) / sizeof(unsigned char)); n++)
poly |= (z_crc_t)1 << (31 - p[n]); poly |= (z_crc_t)1 << (31 - p[n]);
/* generate a crc for every 8-bit value */ /* generate a crc for every 8-bit value */
for (n = 0; n < 256; n++) { for (n = 0; n < 256; n++)
{
c = (z_crc_t)n; c = (z_crc_t)n;
for (k = 0; k < 8; k++) for (k = 0; k < 8; k++)
c = c & 1 ? poly ^ (c >> 1) : c >> 1; c = c & 1 ? poly ^ (c >> 1) : c >> 1;
crc_table[0][n] = c; crc_table[0][n] = c;
} }
#ifdef BYFOUR #ifdef BYFOUR
/* generate crc for each value followed by one, two, and three zeros, /* generate crc for each value followed by one, two, and three zeros,
and then the byte reversal of those as well as the first table */ and then the byte reversal of those as well as the first table */
for (n = 0; n < 256; n++) { for (n = 0; n < 256; n++)
{
c = crc_table[0][n]; c = crc_table[0][n];
crc_table[4][n] = ZSWAP32(c); crc_table[4][n] = ZSWAP32(c);
for (k = 1; k < 4; k++) {
for (k = 1; k < 4; k++)
{
c = crc_table[0][c & 0xff] ^ (c >> 8); c = crc_table[0][c & 0xff] ^ (c >> 8);
crc_table[k][n] = c; crc_table[k][n] = c;
crc_table[k + 4][n] = ZSWAP32(c); crc_table[k + 4][n] = ZSWAP32(c);
} }
} }
#endif /* BYFOUR */ #endif /* BYFOUR */
crc_table_empty = 0; crc_table_empty = 0;
} }
else { /* not first */ else /* not first */
{
/* wait for the other guy to finish (not efficient, but rare) */ /* wait for the other guy to finish (not efficient, but rare) */
while (crc_table_empty) while (crc_table_empty)
; ;
@ -143,7 +154,9 @@ local void make_crc_table()
FILE* out; FILE* out;
out = fopen("crc32.h", "w"); out = fopen("crc32.h", "w");
if (out == NULL) return; if (out == NULL) return;
fprintf(out, "/* crc32.h -- tables for rapid CRC calculation\n"); fprintf(out, "/* crc32.h -- tables for rapid CRC calculation\n");
fprintf(out, " * Generated automatically by crc32.c\n */\n\n"); fprintf(out, " * Generated automatically by crc32.c\n */\n\n");
fprintf(out, "local const z_crc_t FAR "); fprintf(out, "local const z_crc_t FAR ");
@ -151,10 +164,13 @@ local void make_crc_table()
write_table(out, crc_table[0]); write_table(out, crc_table[0]);
# ifdef BYFOUR # ifdef BYFOUR
fprintf(out, "#ifdef BYFOUR\n"); fprintf(out, "#ifdef BYFOUR\n");
for (k = 1; k < 8; k++) {
for (k = 1; k < 8; k++)
{
fprintf(out, " },\n {\n"); fprintf(out, " },\n {\n");
write_table(out, crc_table[k]); write_table(out, crc_table[k]);
} }
fprintf(out, "#endif\n"); fprintf(out, "#endif\n");
# endif /* BYFOUR */ # endif /* BYFOUR */
fprintf(out, " }\n};\n"); fprintf(out, " }\n};\n");
@ -190,8 +206,10 @@ local void write_table(out, table)
const z_crc_t FAR* ZEXPORT get_crc_table() const z_crc_t FAR* ZEXPORT get_crc_table()
{ {
#ifdef DYNAMIC_CRC_TABLE #ifdef DYNAMIC_CRC_TABLE
if (crc_table_empty) if (crc_table_empty)
make_crc_table(); make_crc_table();
#endif /* DYNAMIC_CRC_TABLE */ #endif /* DYNAMIC_CRC_TABLE */
return (const z_crc_t FAR*)crc_table; return (const z_crc_t FAR*)crc_table;
} }
@ -213,34 +231,48 @@ unsigned long ZEXPORT crc32(crc, buf, len)
if (buf == Z_NULL) return 0UL; if (buf == Z_NULL) return 0UL;
#ifdef DYNAMIC_CRC_TABLE #ifdef DYNAMIC_CRC_TABLE
if (crc_table_empty) if (crc_table_empty)
make_crc_table(); make_crc_table();
#endif /* DYNAMIC_CRC_TABLE */ #endif /* DYNAMIC_CRC_TABLE */
#ifdef BYFOUR #ifdef BYFOUR
#ifdef WIN32 #ifdef WIN32
int size_match = (sizeof(void*) == sizeof(ptrdiff_t)); int size_match = (sizeof(void*) == sizeof(ptrdiff_t));
if (size_match) {
if (size_match)
{
#else #else
if (sizeof(void *) == sizeof(ptrdiff_t)) {
if (sizeof(void*) == sizeof(ptrdiff_t))
{
#endif /* WIN32 */ #endif /* WIN32 */
z_crc_t endian; z_crc_t endian;
endian = 1; endian = 1;
if (*((unsigned char*)(&endian))) if (*((unsigned char*)(&endian)))
return crc32_little(crc, buf, len); return crc32_little(crc, buf, len);
else else
return crc32_big(crc, buf, len); return crc32_big(crc, buf, len);
} }
#endif /* BYFOUR */ #endif /* BYFOUR */
crc = crc ^ 0xffffffffUL; crc = crc ^ 0xffffffffUL;
while (len >= 8) {
while (len >= 8)
{
DO8; DO8;
len -= 8; len -= 8;
} }
if (len) do {
if (len) do
{
DO1; DO1;
} while (--len); }
while (--len);
return crc ^ 0xffffffffUL; return crc ^ 0xffffffffUL;
} }
@ -267,25 +299,35 @@ local unsigned long crc32_little(crc, buf, len)
c = (z_crc_t)crc; c = (z_crc_t)crc;
c = ~c; c = ~c;
while (len && ((ptrdiff_t)buf & 3)) {
while (len && ((ptrdiff_t)buf & 3))
{
c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8); c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8);
len--; len--;
} }
buf4 = (const z_crc_t FAR*)(const void FAR*)buf; buf4 = (const z_crc_t FAR*)(const void FAR*)buf;
while (len >= 32) {
while (len >= 32)
{
DOLIT32; DOLIT32;
len -= 32; len -= 32;
} }
while (len >= 4) {
while (len >= 4)
{
DOLIT4; DOLIT4;
len -= 4; len -= 4;
} }
buf = (const unsigned char FAR*)buf4; buf = (const unsigned char FAR*)buf4;
if (len) do { if (len) do
{
c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8); c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8);
} while (--len); }
while (--len);
c = ~c; c = ~c;
return (unsigned long)c; return (unsigned long)c;
} }
@ -311,27 +353,37 @@ local unsigned long crc32_big(crc, buf, len)
c = ZSWAP32((z_crc_t)crc); c = ZSWAP32((z_crc_t)crc);
c = ~c; c = ~c;
while (len && ((ptrdiff_t)buf & 3)) {
while (len && ((ptrdiff_t)buf & 3))
{
c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8); c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
len--; len--;
} }
buf4 = (const z_crc_t FAR*)(const void FAR*)buf; buf4 = (const z_crc_t FAR*)(const void FAR*)buf;
buf4--; buf4--;
while (len >= 32) {
while (len >= 32)
{
DOBIG32; DOBIG32;
len -= 32; len -= 32;
} }
while (len >= 4) {
while (len >= 4)
{
DOBIG4; DOBIG4;
len -= 4; len -= 4;
} }
buf4++; buf4++;
buf = (const unsigned char FAR*)buf4; buf = (const unsigned char FAR*)buf4;
if (len) do { if (len) do
{
c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8); c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
} while (--len); }
while (--len);
c = ~c; c = ~c;
return (unsigned long)(ZSWAP32(c)); return (unsigned long)(ZSWAP32(c));
} }
@ -352,12 +404,16 @@ local unsigned long gf2_matrix_times(mat, vec)
unsigned long sum; unsigned long sum;
sum = 0; sum = 0;
while (vec) {
while (vec)
{
if (vec & 1) if (vec & 1)
sum ^= *mat; sum ^= *mat;
vec >>= 1; vec >>= 1;
mat++; mat++;
} }
return sum; return sum;
} }
@ -398,7 +454,9 @@ local uLong crc32_combine_(crc1, crc2, len2)
/* put operator for one zero bit in odd */ /* put operator for one zero bit in odd */
odd[0] = 0xedb88320UL; /* CRC-32 polynomial */ odd[0] = 0xedb88320UL; /* CRC-32 polynomial */
row = 1; row = 1;
for (n = 1; n < GF2_DIM; n++) {
for (n = 1; n < GF2_DIM; n++)
{
odd[n] = row; odd[n] = row;
row <<= 1; row <<= 1;
} }
@ -411,11 +469,14 @@ local uLong crc32_combine_(crc1, crc2, len2)
/* apply len2 zeros to crc1 (first square will put the operator for one /* apply len2 zeros to crc1 (first square will put the operator for one
zero byte, eight zero bits, in even) */ zero byte, eight zero bits, in even) */
do { do
{
/* apply zeros operator for this bit of len2 */ /* apply zeros operator for this bit of len2 */
gf2_matrix_square(even, odd); gf2_matrix_square(even, odd);
if (len2 & 1) if (len2 & 1)
crc1 = gf2_matrix_times(even, crc1); crc1 = gf2_matrix_times(even, crc1);
len2 >>= 1; len2 >>= 1;
/* if no more bits set, then done */ /* if no more bits set, then done */
@ -424,12 +485,15 @@ local uLong crc32_combine_(crc1, crc2, len2)
/* another iteration of the loop with odd and even swapped */ /* another iteration of the loop with odd and even swapped */
gf2_matrix_square(odd, even); gf2_matrix_square(odd, even);
if (len2 & 1) if (len2 & 1)
crc1 = gf2_matrix_times(odd, crc1); crc1 = gf2_matrix_times(odd, crc1);
len2 >>= 1; len2 >>= 1;
/* if no more bits set, then done */ /* if no more bits set, then done */
} while (len2 != 0); }
while (len2 != 0);
/* return combined crc */ /* return combined crc */
crc1 ^= crc2; crc1 ^= crc2;

676
extern/zlib/deflate.c vendored

File diff suppressed because it is too large Load Diff

15
extern/zlib/deflate.h vendored
View File

@ -62,12 +62,15 @@
/* Data structure describing a single value and its code string. */ /* Data structure describing a single value and its code string. */
typedef struct ct_data_s { typedef struct ct_data_s
union { {
union
{
ush freq; /* frequency count */ ush freq; /* frequency count */
ush code; /* bit string */ ush code; /* bit string */
} fc; } fc;
union { union
{
ush dad; /* father node in Huffman tree */ ush dad; /* father node in Huffman tree */
ush len; /* length of bit string */ ush len; /* length of bit string */
} dl; } dl;
@ -80,7 +83,8 @@ typedef struct ct_data_s {
typedef struct static_tree_desc_s static_tree_desc; typedef struct static_tree_desc_s static_tree_desc;
typedef struct tree_desc_s { typedef struct tree_desc_s
{
ct_data* dyn_tree; /* the dynamic tree */ ct_data* dyn_tree; /* the dynamic tree */
int max_code; /* largest code with non zero frequency */ int max_code; /* largest code with non zero frequency */
static_tree_desc* stat_desc; /* the corresponding static tree */ static_tree_desc* stat_desc; /* the corresponding static tree */
@ -94,7 +98,8 @@ typedef unsigned IPos;
* save space in the various tables. IPos is used only for parameter passing. * save space in the various tables. IPos is used only for parameter passing.
*/ */
typedef struct internal_state { typedef struct internal_state
{
z_streamp strm; /* pointer back to this zlib stream */ z_streamp strm; /* pointer back to this zlib stream */
int status; /* as the name implies */ int status; /* as the name implies */
Bytef* pending_buf; /* output still pending */ Bytef* pending_buf; /* output still pending */

View File

@ -20,6 +20,7 @@ int ZEXPORT gzclose(file)
if (file == NULL) if (file == NULL)
return Z_STREAM_ERROR; return Z_STREAM_ERROR;
state = (gz_statep)file; state = (gz_statep)file;
return state->mode == GZ_READ ? gzclose_r(file) : gzclose_w(file); return state->mode == GZ_READ ? gzclose_r(file) : gzclose_w(file);

View File

@ -147,7 +147,8 @@
#define GZIP 2 /* decompress a gzip stream */ #define GZIP 2 /* decompress a gzip stream */
/* internal gzip file state data structure */ /* internal gzip file state data structure */
typedef struct { typedef struct
{
/* exposed contents for gzgetc() macro */ /* exposed contents for gzgetc() macro */
struct gzFile_s x; /* "x" for exposed */ struct gzFile_s x; /* "x" for exposed */
/* x.have: number of bytes available at x.next */ /* x.have: number of bytes available at x.next */

136
extern/zlib/gzlib.c vendored
View File

@ -45,15 +45,19 @@ char ZLIB_INTERNAL *gz_strwinerror (error)
(LPVOID)&msgbuf, (LPVOID)&msgbuf,
0, 0,
NULL); NULL);
if (chars != 0) {
if (chars != 0)
{
/* If there is an \r\n appended, zap it. */ /* If there is an \r\n appended, zap it. */
if (chars >= 2 if (chars >= 2
&& msgbuf[chars - 2] == '\r' && msgbuf[chars - 1] == '\n') { && msgbuf[chars - 2] == '\r' && msgbuf[chars - 1] == '\n')
{
chars -= 2; chars -= 2;
msgbuf[chars] = 0; msgbuf[chars] = 0;
} }
if (chars > sizeof (buf) - 1) { if (chars > sizeof(buf) - 1)
{
chars = sizeof(buf) - 1; chars = sizeof(buf) - 1;
msgbuf[chars] = 0; msgbuf[chars] = 0;
} }
@ -61,7 +65,8 @@ char ZLIB_INTERNAL *gz_strwinerror (error)
wcstombs(buf, msgbuf, chars + 1); wcstombs(buf, msgbuf, chars + 1);
LocalFree(msgbuf); LocalFree(msgbuf);
} }
else { else
{
sprintf(buf, "unknown win32 error (%ld)", error); sprintf(buf, "unknown win32 error (%ld)", error);
} }
@ -80,11 +85,14 @@ local void gz_reset(state)
#endif #endif
{ {
state->x.have = 0; /* no output data available */ state->x.have = 0; /* no output data available */
if (state->mode == GZ_READ) { /* for reading ... */
if (state->mode == GZ_READ) /* for reading ... */
{
state->eof = 0; /* not at end of file */ state->eof = 0; /* not at end of file */
state->past = 0; /* have not read past end yet */ state->past = 0; /* have not read past end yet */
state->how = LOOK; /* look for gzip header */ state->how = LOOK; /* look for gzip header */
} }
state->seek = 0; /* no seek request pending */ state->seek = 0; /* no seek request pending */
gz_error(state, Z_OK, NULL); /* clear error */ gz_error(state, Z_OK, NULL); /* clear error */
state->x.pos = 0; /* no uncompressed data yet */ state->x.pos = 0; /* no uncompressed data yet */
@ -117,8 +125,10 @@ local gzFile gz_open(path, fd, mode)
/* allocate gzFile structure to return */ /* allocate gzFile structure to return */
state = malloc(sizeof(gz_state)); state = malloc(sizeof(gz_state));
if (state == NULL) if (state == NULL)
return NULL; return NULL;
state->size = 0; /* no buffers allocated yet */ state->size = 0; /* no buffers allocated yet */
state->want = GZBUFSIZE; /* requested buffer size */ state->want = GZBUFSIZE; /* requested buffer size */
state->msg = NULL; /* no error message yet */ state->msg = NULL; /* no error message yet */
@ -128,87 +138,115 @@ local gzFile gz_open(path, fd, mode)
state->level = Z_DEFAULT_COMPRESSION; state->level = Z_DEFAULT_COMPRESSION;
state->strategy = Z_DEFAULT_STRATEGY; state->strategy = Z_DEFAULT_STRATEGY;
state->direct = 0; state->direct = 0;
while (*mode) {
while (*mode)
{
if (*mode >= '0' && *mode <= '9') if (*mode >= '0' && *mode <= '9')
state->level = *mode - '0'; state->level = *mode - '0';
else else
switch (*mode) { switch (*mode)
{
case 'r': case 'r':
state->mode = GZ_READ; state->mode = GZ_READ;
break; break;
#ifndef NO_GZCOMPRESS #ifndef NO_GZCOMPRESS
case 'w': case 'w':
state->mode = GZ_WRITE; state->mode = GZ_WRITE;
break; break;
case 'a': case 'a':
state->mode = GZ_APPEND; state->mode = GZ_APPEND;
break; break;
#endif #endif
case '+': /* can't read and write at the same time */ case '+': /* can't read and write at the same time */
free(state); free(state);
return NULL; return NULL;
case 'b': /* ignore -- will request binary anyway */ case 'b': /* ignore -- will request binary anyway */
break; break;
#ifdef O_CLOEXEC #ifdef O_CLOEXEC
case 'e': case 'e':
cloexec = 1; cloexec = 1;
break; break;
#endif #endif
#ifdef O_EXCL #ifdef O_EXCL
case 'x': case 'x':
exclusive = 1; exclusive = 1;
break; break;
#endif #endif
case 'f': case 'f':
state->strategy = Z_FILTERED; state->strategy = Z_FILTERED;
break; break;
case 'h': case 'h':
state->strategy = Z_HUFFMAN_ONLY; state->strategy = Z_HUFFMAN_ONLY;
break; break;
case 'R': case 'R':
state->strategy = Z_RLE; state->strategy = Z_RLE;
break; break;
case 'F': case 'F':
state->strategy = Z_FIXED; state->strategy = Z_FIXED;
case 'T': case 'T':
state->direct = 1; state->direct = 1;
default: /* could consider as an error, but just ignore */ default: /* could consider as an error, but just ignore */
; ;
} }
mode++; mode++;
} }
/* must provide an "r", "w", or "a" */ /* must provide an "r", "w", or "a" */
if (state->mode == GZ_NONE) { if (state->mode == GZ_NONE)
{
free(state); free(state);
return NULL; return NULL;
} }
/* can't force transparent read */ /* can't force transparent read */
if (state->mode == GZ_READ) { if (state->mode == GZ_READ)
if (state->direct) { {
if (state->direct)
{
free(state); free(state);
return NULL; return NULL;
} }
state->direct = 1; /* for empty file */ state->direct = 1; /* for empty file */
} }
/* save the path name for error messages */ /* save the path name for error messages */
#ifdef _WIN32 #ifdef _WIN32
if (fd == -2) {
if (fd == -2)
{
len = wcstombs(NULL, path, 0); len = wcstombs(NULL, path, 0);
if (len == (size_t) - 1) if (len == (size_t) - 1)
len = 0; len = 0;
} }
else else
#endif #endif
len = strlen(path); len = strlen(path);
state->path = malloc(len + 1); state->path = malloc(len + 1);
if (state->path == NULL) {
if (state->path == NULL)
{
free(state); free(state);
return NULL; return NULL;
} }
#ifdef _WIN32 #ifdef _WIN32
if (fd == -2) if (fd == -2)
if (len) if (len)
wcstombs(state->path, path, len + 1); wcstombs(state->path, path, len + 1);
@ -245,17 +283,22 @@ local gzFile gz_open(path, fd, mode)
fd == -2 ? _wopen(path, oflag, 0666) : fd == -2 ? _wopen(path, oflag, 0666) :
#endif #endif
open(path, oflag, 0666)); open(path, oflag, 0666));
if (state->fd == -1) {
if (state->fd == -1)
{
free(state->path); free(state->path);
free(state); free(state);
return NULL; return NULL;
} }
if (state->mode == GZ_APPEND) if (state->mode == GZ_APPEND)
state->mode = GZ_WRITE; /* simplify later checks */ state->mode = GZ_WRITE; /* simplify later checks */
/* save the current position for rewinding (only if reading) */ /* save the current position for rewinding (only if reading) */
if (state->mode == GZ_READ) { if (state->mode == GZ_READ)
{
state->start = LSEEK(state->fd, 0, SEEK_CUR); state->start = LSEEK(state->fd, 0, SEEK_CUR);
if (state->start == -1) state->start = 0; if (state->start == -1) state->start = 0;
} }
@ -304,6 +347,7 @@ gzFile ZEXPORT gzdopen(fd, mode)
if (fd == -1 || (path = malloc(7 + 3 * sizeof(int))) == NULL) if (fd == -1 || (path = malloc(7 + 3 * sizeof(int))) == NULL)
return NULL; return NULL;
sprintf(path, "<fd:%d>", fd); /* for debugging */ sprintf(path, "<fd:%d>", fd); /* for debugging */
gz = gz_open(path, fd, mode); gz = gz_open(path, fd, mode);
free(path); free(path);
@ -336,7 +380,9 @@ int ZEXPORT gzbuffer(file, size)
/* get internal structure and check integrity */ /* get internal structure and check integrity */
if (file == NULL) if (file == NULL)
return -1; return -1;
state = (gz_statep)file; state = (gz_statep)file;
if (state->mode != GZ_READ && state->mode != GZ_WRITE) if (state->mode != GZ_READ && state->mode != GZ_WRITE)
return -1; return -1;
@ -347,6 +393,7 @@ int ZEXPORT gzbuffer(file, size)
/* check and set requested size */ /* check and set requested size */
if (size < 2) if (size < 2)
size = 2; /* need two bytes to check magic header */ size = 2; /* need two bytes to check magic header */
state->want = size; state->want = size;
return 0; return 0;
} }
@ -364,6 +411,7 @@ int ZEXPORT gzrewind(file)
/* get internal structure */ /* get internal structure */
if (file == NULL) if (file == NULL)
return -1; return -1;
state = (gz_statep)file; state = (gz_statep)file;
/* check that we're reading and that there's no error */ /* check that we're reading and that there's no error */
@ -374,6 +422,7 @@ int ZEXPORT gzrewind(file)
/* back up and start over */ /* back up and start over */
if (LSEEK(state->fd, state->start, SEEK_SET) == -1) if (LSEEK(state->fd, state->start, SEEK_SET) == -1)
return -1; return -1;
gz_reset(state); gz_reset(state);
return 0; return 0;
} }
@ -395,7 +444,9 @@ z_off64_t ZEXPORT gzseek64(file, offset, whence)
/* get internal structure and check integrity */ /* get internal structure and check integrity */
if (file == NULL) if (file == NULL)
return -1; return -1;
state = (gz_statep)file; state = (gz_statep)file;
if (state->mode != GZ_READ && state->mode != GZ_WRITE) if (state->mode != GZ_READ && state->mode != GZ_WRITE)
return -1; return -1;
@ -412,14 +463,18 @@ z_off64_t ZEXPORT gzseek64(file, offset, whence)
offset -= state->x.pos; offset -= state->x.pos;
else if (state->seek) else if (state->seek)
offset += state->skip; offset += state->skip;
state->seek = 0; state->seek = 0;
/* if within raw area while reading, just go there */ /* if within raw area while reading, just go there */
if (state->mode == GZ_READ && state->how == COPY && if (state->mode == GZ_READ && state->how == COPY &&
state->x.pos + offset >= 0) { state->x.pos + offset >= 0)
{
ret = LSEEK(state->fd, offset - state->x.have, SEEK_CUR); ret = LSEEK(state->fd, offset - state->x.have, SEEK_CUR);
if (ret == -1) if (ret == -1)
return -1; return -1;
state->x.have = 0; state->x.have = 0;
state->eof = 0; state->eof = 0;
state->past = 0; state->past = 0;
@ -431,18 +486,23 @@ z_off64_t ZEXPORT gzseek64(file, offset, whence)
} }
/* calculate skip amount, rewinding if needed for back seek when reading */ /* calculate skip amount, rewinding if needed for back seek when reading */
if (offset < 0) { if (offset < 0)
{
if (state->mode != GZ_READ) /* writing -- can't go backwards */ if (state->mode != GZ_READ) /* writing -- can't go backwards */
return -1; return -1;
offset += state->x.pos; offset += state->x.pos;
if (offset < 0) /* before start of file! */ if (offset < 0) /* before start of file! */
return -1; return -1;
if (gzrewind(file) == -1) /* rewind, then skip to offset */ if (gzrewind(file) == -1) /* rewind, then skip to offset */
return -1; return -1;
} }
/* if reading, skip what's in output buffer (one less gzgetc() check) */ /* if reading, skip what's in output buffer (one less gzgetc() check) */
if (state->mode == GZ_READ) { if (state->mode == GZ_READ)
{
n = GT_OFF(state->x.have) || (z_off64_t)state->x.have > offset ? n = GT_OFF(state->x.have) || (z_off64_t)state->x.have > offset ?
(unsigned)offset : state->x.have; (unsigned)offset : state->x.have;
state->x.have -= n; state->x.have -= n;
@ -452,10 +512,12 @@ z_off64_t ZEXPORT gzseek64(file, offset, whence)
} }
/* request skip (if not zero) */ /* request skip (if not zero) */
if (offset) { if (offset)
{
state->seek = 1; state->seek = 1;
state->skip = offset; state->skip = offset;
} }
return state->x.pos + offset; return state->x.pos + offset;
} }
@ -488,7 +550,9 @@ z_off64_t ZEXPORT gztell64(file)
/* get internal structure and check integrity */ /* get internal structure and check integrity */
if (file == NULL) if (file == NULL)
return -1; return -1;
state = (gz_statep)file; state = (gz_statep)file;
if (state->mode != GZ_READ && state->mode != GZ_WRITE) if (state->mode != GZ_READ && state->mode != GZ_WRITE)
return -1; return -1;
@ -524,16 +588,21 @@ z_off64_t ZEXPORT gzoffset64(file)
/* get internal structure and check integrity */ /* get internal structure and check integrity */
if (file == NULL) if (file == NULL)
return -1; return -1;
state = (gz_statep)file; state = (gz_statep)file;
if (state->mode != GZ_READ && state->mode != GZ_WRITE) if (state->mode != GZ_READ && state->mode != GZ_WRITE)
return -1; return -1;
/* compute and return effective offset in file */ /* compute and return effective offset in file */
offset = LSEEK(state->fd, 0, SEEK_CUR); offset = LSEEK(state->fd, 0, SEEK_CUR);
if (offset == -1) if (offset == -1)
return -1; return -1;
if (state->mode == GZ_READ) /* reading */ if (state->mode == GZ_READ) /* reading */
offset -= state->strm.avail_in; /* don't count buffered input */ offset -= state->strm.avail_in; /* don't count buffered input */
return offset; return offset;
} }
@ -564,7 +633,9 @@ int ZEXPORT gzeof(file)
/* get internal structure and check integrity */ /* get internal structure and check integrity */
if (file == NULL) if (file == NULL)
return 0; return 0;
state = (gz_statep)file; state = (gz_statep)file;
if (state->mode != GZ_READ && state->mode != GZ_WRITE) if (state->mode != GZ_READ && state->mode != GZ_WRITE)
return 0; return 0;
@ -586,13 +657,16 @@ const char * ZEXPORT gzerror(file, errnum)
/* get internal structure and check integrity */ /* get internal structure and check integrity */
if (file == NULL) if (file == NULL)
return NULL; return NULL;
state = (gz_statep)file; state = (gz_statep)file;
if (state->mode != GZ_READ && state->mode != GZ_WRITE) if (state->mode != GZ_READ && state->mode != GZ_WRITE)
return NULL; return NULL;
/* return error information */ /* return error information */
if (errnum != NULL) if (errnum != NULL)
*errnum = state->err; *errnum = state->err;
return state->msg == NULL ? "" : state->msg; return state->msg == NULL ? "" : state->msg;
} }
@ -609,15 +683,19 @@ void ZEXPORT gzclearerr(file)
/* get internal structure and check integrity */ /* get internal structure and check integrity */
if (file == NULL) if (file == NULL)
return; return;
state = (gz_statep)file; state = (gz_statep)file;
if (state->mode != GZ_READ && state->mode != GZ_WRITE) if (state->mode != GZ_READ && state->mode != GZ_WRITE)
return; return;
/* clear error and end-of-file */ /* clear error and end-of-file */
if (state->mode == GZ_READ) { if (state->mode == GZ_READ)
{
state->eof = 0; state->eof = 0;
state->past = 0; state->past = 0;
} }
gz_error(state, Z_OK, NULL); gz_error(state, Z_OK, NULL);
} }
@ -637,9 +715,11 @@ void ZLIB_INTERNAL gz_error(state, err, msg)
#endif #endif
{ {
/* free previously allocated message and clear */ /* free previously allocated message and clear */
if (state->msg != NULL) { if (state->msg != NULL)
{
if (state->err != Z_MEM_ERROR) if (state->err != Z_MEM_ERROR)
free(state->msg); free(state->msg);
state->msg = NULL; state->msg = NULL;
} }
@ -649,21 +729,25 @@ void ZLIB_INTERNAL gz_error(state, err, msg)
/* set error code, and if no message, then done */ /* set error code, and if no message, then done */
state->err = err; state->err = err;
if (msg == NULL) if (msg == NULL)
return; return;
/* for an out of memory error, save as static string */ /* for an out of memory error, save as static string */
if (err == Z_MEM_ERROR) { if (err == Z_MEM_ERROR)
{
state->msg = (char*)msg; state->msg = (char*)msg;
return; return;
} }
/* construct error message with path */ /* construct error message with path */
if ((state->msg = malloc(strlen(state->path) + strlen(msg) + 3)) == NULL) { if ((state->msg = malloc(strlen(state->path) + strlen(msg) + 3)) == NULL)
{
state->err = Z_MEM_ERROR; state->err = Z_MEM_ERROR;
state->msg = (char*)"out of memory"; state->msg = (char*)"out of memory";
return; return;
} }
strcpy(state->msg, state->path); strcpy(state->msg, state->path);
strcat(state->msg, ": "); strcat(state->msg, ": ");
strcat(state->msg, msg); strcat(state->msg, msg);
@ -680,11 +764,15 @@ unsigned ZLIB_INTERNAL gz_intmax()
unsigned p, q; unsigned p, q;
p = 1; p = 1;
do {
do
{
q = p; q = p;
p <<= 1; p <<= 1;
p++; p++;
} while (p > q); }
while (p > q);
return q >> 1; return q >> 1;
} }
#endif #endif

186
extern/zlib/gzread.c vendored
View File

@ -30,18 +30,27 @@ local int gz_load(state, buf, len, have)
int ret; int ret;
*have = 0; *have = 0;
do {
do
{
ret = read(state->fd, buf + *have, len - *have); ret = read(state->fd, buf + *have, len - *have);
if (ret <= 0) if (ret <= 0)
break; break;
*have += ret; *have += ret;
} while (*have < len); }
if (ret < 0) { while (*have < len);
if (ret < 0)
{
gz_error(state, Z_ERRNO, zstrerror()); gz_error(state, Z_ERRNO, zstrerror());
return -1; return -1;
} }
if (ret == 0) if (ret == 0)
state->eof = 1; state->eof = 1;
return 0; return 0;
} }
@ -64,20 +73,29 @@ local int gz_avail(state)
if (state->err != Z_OK && state->err != Z_BUF_ERROR) if (state->err != Z_OK && state->err != Z_BUF_ERROR)
return -1; return -1;
if (state->eof == 0) {
if (strm->avail_in) { /* copy what's there to the start */ if (state->eof == 0)
{
if (strm->avail_in) /* copy what's there to the start */
{
unsigned char* p = state->in, *q = strm->next_in; unsigned char* p = state->in, *q = strm->next_in;
unsigned n = strm->avail_in; unsigned n = strm->avail_in;
do {
do
{
*p++ = *q++; *p++ = *q++;
} while (--n);
} }
while (--n);
}
if (gz_load(state, state->in + strm->avail_in, if (gz_load(state, state->in + strm->avail_in,
state->size - strm->avail_in, &got) == -1) state->size - strm->avail_in, &got) == -1)
return -1; return -1;
strm->avail_in += got; strm->avail_in += got;
strm->next_in = state->in; strm->next_in = state->in;
} }
return 0; return 0;
} }
@ -100,18 +118,24 @@ local int gz_look(state)
z_streamp strm = &(state->strm); z_streamp strm = &(state->strm);
/* allocate read buffers and inflate memory */ /* allocate read buffers and inflate memory */
if (state->size == 0) { if (state->size == 0)
{
/* allocate buffers */ /* allocate buffers */
state->in = malloc(state->want); state->in = malloc(state->want);
state->out = malloc(state->want << 1); state->out = malloc(state->want << 1);
if (state->in == NULL || state->out == NULL) {
if (state->in == NULL || state->out == NULL)
{
if (state->out != NULL) if (state->out != NULL)
free(state->out); free(state->out);
if (state->in != NULL) if (state->in != NULL)
free(state->in); free(state->in);
gz_error(state, Z_MEM_ERROR, "out of memory"); gz_error(state, Z_MEM_ERROR, "out of memory");
return -1; return -1;
} }
state->size = state->want; state->size = state->want;
/* allocate inflate memory */ /* allocate inflate memory */
@ -120,7 +144,9 @@ local int gz_look(state)
state->strm.opaque = Z_NULL; state->strm.opaque = Z_NULL;
state->strm.avail_in = 0; state->strm.avail_in = 0;
state->strm.next_in = Z_NULL; state->strm.next_in = Z_NULL;
if (inflateInit2(&(state->strm), 15 + 16) != Z_OK) { /* gunzip */
if (inflateInit2(&(state->strm), 15 + 16) != Z_OK) /* gunzip */
{
free(state->out); free(state->out);
free(state->in); free(state->in);
state->size = 0; state->size = 0;
@ -130,9 +156,11 @@ local int gz_look(state)
} }
/* get at least the magic bytes in the input buffer */ /* get at least the magic bytes in the input buffer */
if (strm->avail_in < 2) { if (strm->avail_in < 2)
{
if (gz_avail(state) == -1) if (gz_avail(state) == -1)
return -1; return -1;
if (strm->avail_in == 0) if (strm->avail_in == 0)
return 0; return 0;
} }
@ -145,7 +173,8 @@ local int gz_look(state)
the header will be written in a single operation, so that reading a the header will be written in a single operation, so that reading a
single byte is sufficient indication that it is not a gzip file) */ single byte is sufficient indication that it is not a gzip file) */
if (strm->avail_in > 1 && if (strm->avail_in > 1 &&
strm->next_in[0] == 31 && strm->next_in[1] == 139) { strm->next_in[0] == 31 && strm->next_in[1] == 139)
{
inflateReset(strm); inflateReset(strm);
state->how = GZIP; state->how = GZIP;
state->direct = 0; state->direct = 0;
@ -154,7 +183,8 @@ local int gz_look(state)
/* no gzip header -- if we were decoding gzip before, then this is trailing /* no gzip header -- if we were decoding gzip before, then this is trailing
garbage. Ignore the trailing garbage and finish. */ garbage. Ignore the trailing garbage and finish. */
if (state->direct == 0) { if (state->direct == 0)
{
strm->avail_in = 0; strm->avail_in = 0;
state->eof = 1; state->eof = 1;
state->x.have = 0; state->x.have = 0;
@ -165,11 +195,14 @@ local int gz_look(state)
the output buffer is larger than the input buffer, which also assures the output buffer is larger than the input buffer, which also assures
space for gzungetc() */ space for gzungetc() */
state->x.next = state->out; state->x.next = state->out;
if (strm->avail_in) {
if (strm->avail_in)
{
memcpy(state->x.next, strm->next_in, strm->avail_in); memcpy(state->x.next, strm->next_in, strm->avail_in);
state->x.have = strm->avail_in; state->x.have = strm->avail_in;
strm->avail_in = 0; strm->avail_in = 0;
} }
state->how = COPY; state->how = COPY;
state->direct = 1; state->direct = 1;
return 0; return 0;
@ -193,32 +226,43 @@ local int gz_decomp(state)
/* fill output buffer up to end of deflate stream */ /* fill output buffer up to end of deflate stream */
had = strm->avail_out; had = strm->avail_out;
do {
do
{
/* get more input for inflate() */ /* get more input for inflate() */
if (strm->avail_in == 0 && gz_avail(state) == -1) if (strm->avail_in == 0 && gz_avail(state) == -1)
return -1; return -1;
if (strm->avail_in == 0) {
if (strm->avail_in == 0)
{
gz_error(state, Z_BUF_ERROR, "unexpected end of file"); gz_error(state, Z_BUF_ERROR, "unexpected end of file");
break; break;
} }
/* decompress and handle errors */ /* decompress and handle errors */
ret = inflate(strm, Z_NO_FLUSH); ret = inflate(strm, Z_NO_FLUSH);
if (ret == Z_STREAM_ERROR || ret == Z_NEED_DICT) {
if (ret == Z_STREAM_ERROR || ret == Z_NEED_DICT)
{
gz_error(state, Z_STREAM_ERROR, gz_error(state, Z_STREAM_ERROR,
"internal error: inflate stream corrupt"); "internal error: inflate stream corrupt");
return -1; return -1;
} }
if (ret == Z_MEM_ERROR) {
if (ret == Z_MEM_ERROR)
{
gz_error(state, Z_MEM_ERROR, "out of memory"); gz_error(state, Z_MEM_ERROR, "out of memory");
return -1; return -1;
} }
if (ret == Z_DATA_ERROR) { /* deflate stream invalid */
if (ret == Z_DATA_ERROR) /* deflate stream invalid */
{
gz_error(state, Z_DATA_ERROR, gz_error(state, Z_DATA_ERROR,
strm->msg == NULL ? "compressed data error" : strm->msg); strm->msg == NULL ? "compressed data error" : strm->msg);
return -1; return -1;
} }
} while (strm->avail_out && ret != Z_STREAM_END); }
while (strm->avail_out && ret != Z_STREAM_END);
/* update available output */ /* update available output */
state->x.have = had - strm->avail_out; state->x.have = had - strm->avail_out;
@ -247,27 +291,37 @@ local int gz_fetch(state)
{ {
z_streamp strm = &(state->strm); z_streamp strm = &(state->strm);
do { do
switch(state->how) { {
switch (state->how)
{
case LOOK: /* -> LOOK, COPY (only if never GZIP), or GZIP */ case LOOK: /* -> LOOK, COPY (only if never GZIP), or GZIP */
if (gz_look(state) == -1) if (gz_look(state) == -1)
return -1; return -1;
if (state->how == LOOK) if (state->how == LOOK)
return 0; return 0;
break; break;
case COPY: /* -> COPY */ case COPY: /* -> COPY */
if (gz_load(state, state->out, state->size << 1, &(state->x.have)) if (gz_load(state, state->out, state->size << 1, &(state->x.have))
== -1) == -1)
return -1; return -1;
state->x.next = state->out; state->x.next = state->out;
return 0; return 0;
case GZIP: /* -> GZIP or LOOK (if end of gzip stream) */ case GZIP: /* -> GZIP or LOOK (if end of gzip stream) */
strm->avail_out = state->size << 1; strm->avail_out = state->size << 1;
strm->next_out = state->out; strm->next_out = state->out;
if (gz_decomp(state) == -1) if (gz_decomp(state) == -1)
return -1; return -1;
} }
} while (state->x.have == 0 && (!state->eof || strm->avail_in)); }
while (state->x.have == 0 && (!state->eof || strm->avail_in));
return 0; return 0;
} }
@ -284,8 +338,10 @@ local int gz_skip(state, len)
/* skip over len bytes or reach end-of-file, whichever comes first */ /* skip over len bytes or reach end-of-file, whichever comes first */
while (len) while (len)
/* skip over whatever is in output buffer */ /* skip over whatever is in output buffer */
if (state->x.have) { if (state->x.have)
{
n = GT_OFF(state->x.have) || (z_off64_t)state->x.have > len ? n = GT_OFF(state->x.have) || (z_off64_t)state->x.have > len ?
(unsigned)len : state->x.have; (unsigned)len : state->x.have;
state->x.have -= n; state->x.have -= n;
@ -299,11 +355,13 @@ local int gz_skip(state, len)
break; break;
/* need more data to skip -- load up output buffer */ /* need more data to skip -- load up output buffer */
else { else
{
/* get more output, looking for header if required */ /* get more output, looking for header if required */
if (gz_fetch(state) == -1) if (gz_fetch(state) == -1)
return -1; return -1;
} }
return 0; return 0;
} }
@ -324,6 +382,7 @@ int ZEXPORT gzread(file, buf, len)
/* get internal structure */ /* get internal structure */
if (file == NULL) if (file == NULL)
return -1; return -1;
state = (gz_statep)file; state = (gz_statep)file;
strm = &(state->strm); strm = &(state->strm);
@ -334,7 +393,8 @@ int ZEXPORT gzread(file, buf, len)
/* since an int is returned, make sure len fits in one, otherwise return /* since an int is returned, make sure len fits in one, otherwise return
with an error (this avoids the flaw in the interface) */ with an error (this avoids the flaw in the interface) */
if ((int)len < 0) { if ((int)len < 0)
{
gz_error(state, Z_DATA_ERROR, "requested length does not fit in int"); gz_error(state, Z_DATA_ERROR, "requested length does not fit in int");
return -1; return -1;
} }
@ -344,17 +404,22 @@ int ZEXPORT gzread(file, buf, len)
return 0; return 0;
/* process a skip request */ /* process a skip request */
if (state->seek) { if (state->seek)
{
state->seek = 0; state->seek = 0;
if (gz_skip(state, state->skip) == -1) if (gz_skip(state, state->skip) == -1)
return -1; return -1;
} }
/* get len bytes to buf, or less than len if at the end */ /* get len bytes to buf, or less than len if at the end */
got = 0; got = 0;
do {
do
{
/* first just try copying data from the output buffer */ /* first just try copying data from the output buffer */
if (state->x.have) { if (state->x.have)
{
n = state->x.have > len ? len : state->x.have; n = state->x.have > len ? len : state->x.have;
memcpy(buf, state->x.next, n); memcpy(buf, state->x.next, n);
state->x.next += n; state->x.next += n;
@ -362,34 +427,41 @@ int ZEXPORT gzread(file, buf, len)
} }
/* output buffer empty -- return if we're at the end of the input */ /* output buffer empty -- return if we're at the end of the input */
else if (state->eof && strm->avail_in == 0) { else if (state->eof && strm->avail_in == 0)
{
state->past = 1; /* tried to read past end */ state->past = 1; /* tried to read past end */
break; break;
} }
/* need output data -- for small len or new stream load up our output /* need output data -- for small len or new stream load up our output
buffer */ buffer */
else if (state->how == LOOK || len < (state->size << 1)) { else if (state->how == LOOK || len < (state->size << 1))
{
/* get more output, looking for header if required */ /* get more output, looking for header if required */
if (gz_fetch(state) == -1) if (gz_fetch(state) == -1)
return -1; return -1;
continue; /* no progress yet -- go back to copy above */ continue; /* no progress yet -- go back to copy above */
/* the copy above assures that we will leave with space in the /* the copy above assures that we will leave with space in the
output buffer, allowing at least one gzungetc() to succeed */ output buffer, allowing at least one gzungetc() to succeed */
} }
/* large len -- read directly into user buffer */ /* large len -- read directly into user buffer */
else if (state->how == COPY) { /* read directly */ else if (state->how == COPY) /* read directly */
{
if (gz_load(state, buf, len, &n) == -1) if (gz_load(state, buf, len, &n) == -1)
return -1; return -1;
} }
/* large len -- decompress directly into user buffer */ /* large len -- decompress directly into user buffer */
else { /* state->how == GZIP */ else /* state->how == GZIP */
{
strm->avail_out = len; strm->avail_out = len;
strm->next_out = buf; strm->next_out = buf;
if (gz_decomp(state) == -1) if (gz_decomp(state) == -1)
return -1; return -1;
n = state->x.have; n = state->x.have;
state->x.have = 0; state->x.have = 0;
} }
@ -399,7 +471,8 @@ int ZEXPORT gzread(file, buf, len)
buf = (char*)buf + n; buf = (char*)buf + n;
got += n; got += n;
state->x.pos += n; state->x.pos += n;
} while (len); }
while (len);
/* return number of bytes read into user buffer (will fit in int) */ /* return number of bytes read into user buffer (will fit in int) */
return (int)got; return (int)got;
@ -421,6 +494,7 @@ int ZEXPORT gzgetc(file)
/* get internal structure */ /* get internal structure */
if (file == NULL) if (file == NULL)
return -1; return -1;
state = (gz_statep)file; state = (gz_statep)file;
/* check that we're reading and that there's no (serious) error */ /* check that we're reading and that there's no (serious) error */
@ -429,7 +503,8 @@ int ZEXPORT gzgetc(file)
return -1; return -1;
/* try output buffer (no need to check for skip request) */ /* try output buffer (no need to check for skip request) */
if (state->x.have) { if (state->x.have)
{
state->x.have--; state->x.have--;
state->x.pos++; state->x.pos++;
return *(state->x.next)++; return *(state->x.next)++;
@ -464,6 +539,7 @@ int ZEXPORT gzungetc(c, file)
/* get internal structure */ /* get internal structure */
if (file == NULL) if (file == NULL)
return -1; return -1;
state = (gz_statep)file; state = (gz_statep)file;
/* check that we're reading and that there's no (serious) error */ /* check that we're reading and that there's no (serious) error */
@ -472,8 +548,10 @@ int ZEXPORT gzungetc(c, file)
return -1; return -1;
/* process a skip request */ /* process a skip request */
if (state->seek) { if (state->seek)
{
state->seek = 0; state->seek = 0;
if (gz_skip(state, state->skip) == -1) if (gz_skip(state, state->skip) == -1)
return -1; return -1;
} }
@ -483,7 +561,8 @@ int ZEXPORT gzungetc(c, file)
return -1; return -1;
/* if output buffer empty, put byte at end (allows more pushing) */ /* if output buffer empty, put byte at end (allows more pushing) */
if (state->x.have == 0) { if (state->x.have == 0)
{
state->x.have = 1; state->x.have = 1;
state->x.next = state->out + (state->size << 1) - 1; state->x.next = state->out + (state->size << 1) - 1;
state->x.next[0] = c; state->x.next[0] = c;
@ -493,19 +572,24 @@ int ZEXPORT gzungetc(c, file)
} }
/* if no room, give up (must have already done a gzungetc()) */ /* if no room, give up (must have already done a gzungetc()) */
if (state->x.have == (state->size << 1)) { if (state->x.have == (state->size << 1))
{
gz_error(state, Z_DATA_ERROR, "out of room to push characters"); gz_error(state, Z_DATA_ERROR, "out of room to push characters");
return -1; return -1;
} }
/* slide output data if needed and insert byte before existing data */ /* slide output data if needed and insert byte before existing data */
if (state->x.next == state->out) { if (state->x.next == state->out)
{
unsigned char* src = state->out + state->x.have; unsigned char* src = state->out + state->x.have;
unsigned char* dest = state->out + (state->size << 1); unsigned char* dest = state->out + (state->size << 1);
while (src > state->out) while (src > state->out)
*--dest = *--src; *--dest = *--src;
state->x.next = dest; state->x.next = dest;
} }
state->x.have++; state->x.have++;
state->x.next--; state->x.next--;
state->x.next[0] = c; state->x.next[0] = c;
@ -532,6 +616,7 @@ char * ZEXPORT gzgets(file, buf, len)
/* check parameters and get internal structure */ /* check parameters and get internal structure */
if (file == NULL || buf == NULL || len < 1) if (file == NULL || buf == NULL || len < 1)
return NULL; return NULL;
state = (gz_statep)file; state = (gz_statep)file;
/* check that we're reading and that there's no (serious) error */ /* check that we're reading and that there's no (serious) error */
@ -540,8 +625,10 @@ char * ZEXPORT gzgets(file, buf, len)
return NULL; return NULL;
/* process a skip request */ /* process a skip request */
if (state->seek) { if (state->seek)
{
state->seek = 0; state->seek = 0;
if (gz_skip(state, state->skip) == -1) if (gz_skip(state, state->skip) == -1)
return NULL; return NULL;
} }
@ -551,11 +638,15 @@ char * ZEXPORT gzgets(file, buf, len)
the contents, let the user worry about that) */ the contents, let the user worry about that) */
str = buf; str = buf;
left = (unsigned)len - 1; left = (unsigned)len - 1;
if (left) do {
if (left) do
{
/* assure that something is in the output buffer */ /* assure that something is in the output buffer */
if (state->x.have == 0 && gz_fetch(state) == -1) if (state->x.have == 0 && gz_fetch(state) == -1)
return NULL; /* error */ return NULL; /* error */
if (state->x.have == 0) { /* end of file */
if (state->x.have == 0) /* end of file */
{
state->past = 1; /* read past end */ state->past = 1; /* read past end */
break; /* return what we have */ break; /* return what we have */
} }
@ -563,6 +654,7 @@ char * ZEXPORT gzgets(file, buf, len)
/* look for end-of-line in current output buffer */ /* look for end-of-line in current output buffer */
n = state->x.have > left ? left : state->x.have; n = state->x.have > left ? left : state->x.have;
eol = memchr(state->x.next, '\n', n); eol = memchr(state->x.next, '\n', n);
if (eol != NULL) if (eol != NULL)
n = (unsigned)(eol - state->x.next) + 1; n = (unsigned)(eol - state->x.next) + 1;
@ -573,11 +665,13 @@ char * ZEXPORT gzgets(file, buf, len)
state->x.pos += n; state->x.pos += n;
left -= n; left -= n;
buf += n; buf += n;
} while (left && eol == NULL); }
while (left && eol == NULL);
/* return terminated string, or if nothing, end of file */ /* return terminated string, or if nothing, end of file */
if (buf == str) if (buf == str)
return NULL; return NULL;
buf[0] = 0; buf[0] = 0;
return str; return str;
} }
@ -595,6 +689,7 @@ int ZEXPORT gzdirect(file)
/* get internal structure */ /* get internal structure */
if (file == NULL) if (file == NULL)
return 0; return 0;
state = (gz_statep)file; state = (gz_statep)file;
/* if the state is not known, but we can find out, then do so (this is /* if the state is not known, but we can find out, then do so (this is
@ -620,6 +715,7 @@ int ZEXPORT gzclose_r(file)
/* get internal structure */ /* get internal structure */
if (file == NULL) if (file == NULL)
return Z_STREAM_ERROR; return Z_STREAM_ERROR;
state = (gz_statep)file; state = (gz_statep)file;
/* check that we're reading */ /* check that we're reading */
@ -627,11 +723,13 @@ int ZEXPORT gzclose_r(file)
return Z_STREAM_ERROR; return Z_STREAM_ERROR;
/* free memory and close file */ /* free memory and close file */
if (state->size) { if (state->size)
{
inflateEnd(&(state->strm)); inflateEnd(&(state->strm));
free(state->out); free(state->out);
free(state->in); free(state->in);
} }
err = state->err == Z_BUF_ERROR ? Z_BUF_ERROR : Z_OK; err = state->err == Z_BUF_ERROR ? Z_BUF_ERROR : Z_OK;
gz_error(state, Z_OK, NULL); gz_error(state, Z_OK, NULL);
free(state->path); free(state->path);

144
extern/zlib/gzwrite.c vendored
View File

@ -24,16 +24,21 @@ local int gz_init(state)
/* allocate input buffer */ /* allocate input buffer */
state->in = malloc(state->want); state->in = malloc(state->want);
if (state->in == NULL) {
if (state->in == NULL)
{
gz_error(state, Z_MEM_ERROR, "out of memory"); gz_error(state, Z_MEM_ERROR, "out of memory");
return -1; return -1;
} }
/* only need output buffer and deflate state if compressing */ /* only need output buffer and deflate state if compressing */
if (!state->direct) { if (!state->direct)
{
/* allocate output buffer */ /* allocate output buffer */
state->out = malloc(state->want); state->out = malloc(state->want);
if (state->out == NULL) {
if (state->out == NULL)
{
free(state->in); free(state->in);
gz_error(state, Z_MEM_ERROR, "out of memory"); gz_error(state, Z_MEM_ERROR, "out of memory");
return -1; return -1;
@ -45,7 +50,9 @@ local int gz_init(state)
strm->opaque = Z_NULL; strm->opaque = Z_NULL;
ret = deflateInit2(strm, state->level, Z_DEFLATED, ret = deflateInit2(strm, state->level, Z_DEFLATED,
MAX_WBITS + 16, DEF_MEM_LEVEL, state->strategy); MAX_WBITS + 16, DEF_MEM_LEVEL, state->strategy);
if (ret != Z_OK) {
if (ret != Z_OK)
{
free(state->out); free(state->out);
free(state->in); free(state->in);
gz_error(state, Z_MEM_ERROR, "out of memory"); gz_error(state, Z_MEM_ERROR, "out of memory");
@ -57,11 +64,13 @@ local int gz_init(state)
state->size = state->want; state->size = state->want;
/* initialize write buffer if compressing */ /* initialize write buffer if compressing */
if (!state->direct) { if (!state->direct)
{
strm->avail_out = state->size; strm->avail_out = state->size;
strm->next_out = state->out; strm->next_out = state->out;
state->x.next = strm->next_out; state->x.next = strm->next_out;
} }
return 0; return 0;
} }
@ -88,46 +97,62 @@ local int gz_comp(state, flush)
return -1; return -1;
/* write directly if requested */ /* write directly if requested */
if (state->direct) { if (state->direct)
{
got = write(state->fd, strm->next_in, strm->avail_in); got = write(state->fd, strm->next_in, strm->avail_in);
if (got < 0 || (unsigned)got != strm->avail_in) {
if (got < 0 || (unsigned)got != strm->avail_in)
{
gz_error(state, Z_ERRNO, zstrerror()); gz_error(state, Z_ERRNO, zstrerror());
return -1; return -1;
} }
strm->avail_in = 0; strm->avail_in = 0;
return 0; return 0;
} }
/* run deflate() on provided input until it produces no more output */ /* run deflate() on provided input until it produces no more output */
ret = Z_OK; ret = Z_OK;
do {
do
{
/* write out current buffer contents if full, or if flushing, but if /* write out current buffer contents if full, or if flushing, but if
doing Z_FINISH then don't write until we get to Z_STREAM_END */ doing Z_FINISH then don't write until we get to Z_STREAM_END */
if (strm->avail_out == 0 || (flush != Z_NO_FLUSH && if (strm->avail_out == 0 || (flush != Z_NO_FLUSH &&
(flush != Z_FINISH || ret == Z_STREAM_END))) { (flush != Z_FINISH || ret == Z_STREAM_END)))
{
have = (unsigned)(strm->next_out - state->x.next); have = (unsigned)(strm->next_out - state->x.next);
if (have && ((got = write(state->fd, state->x.next, have)) < 0 || if (have && ((got = write(state->fd, state->x.next, have)) < 0 ||
(unsigned)got != have)) { (unsigned)got != have))
{
gz_error(state, Z_ERRNO, zstrerror()); gz_error(state, Z_ERRNO, zstrerror());
return -1; return -1;
} }
if (strm->avail_out == 0) {
if (strm->avail_out == 0)
{
strm->avail_out = state->size; strm->avail_out = state->size;
strm->next_out = state->out; strm->next_out = state->out;
} }
state->x.next = strm->next_out; state->x.next = strm->next_out;
} }
/* compress */ /* compress */
have = strm->avail_out; have = strm->avail_out;
ret = deflate(strm, flush); ret = deflate(strm, flush);
if (ret == Z_STREAM_ERROR) {
if (ret == Z_STREAM_ERROR)
{
gz_error(state, Z_STREAM_ERROR, gz_error(state, Z_STREAM_ERROR,
"internal error: deflate stream corrupt"); "internal error: deflate stream corrupt");
return -1; return -1;
} }
have -= strm->avail_out; have -= strm->avail_out;
} while (have); }
while (have);
/* if that completed a deflate stream, allow another to start */ /* if that completed a deflate stream, allow another to start */
if (flush == Z_FINISH) if (flush == Z_FINISH)
@ -156,20 +181,28 @@ local int gz_zero(state, len)
/* compress len zeros (len guaranteed > 0) */ /* compress len zeros (len guaranteed > 0) */
first = 1; first = 1;
while (len) {
while (len)
{
n = GT_OFF(state->size) || (z_off64_t)state->size > len ? n = GT_OFF(state->size) || (z_off64_t)state->size > len ?
(unsigned)len : state->size; (unsigned)len : state->size;
if (first) {
if (first)
{
memset(state->in, 0, n); memset(state->in, 0, n);
first = 0; first = 0;
} }
strm->avail_in = n; strm->avail_in = n;
strm->next_in = state->in; strm->next_in = state->in;
state->x.pos += n; state->x.pos += n;
if (gz_comp(state, Z_NO_FLUSH) == -1) if (gz_comp(state, Z_NO_FLUSH) == -1)
return -1; return -1;
len -= n; len -= n;
} }
return 0; return 0;
} }
@ -191,6 +224,7 @@ int ZEXPORT gzwrite(file, buf, len)
/* get internal structure */ /* get internal structure */
if (file == NULL) if (file == NULL)
return 0; return 0;
state = (gz_statep)file; state = (gz_statep)file;
strm = &(state->strm); strm = &(state->strm);
@ -200,7 +234,8 @@ int ZEXPORT gzwrite(file, buf, len)
/* since an int is returned, make sure len fits in one, otherwise return /* since an int is returned, make sure len fits in one, otherwise return
with an error (this avoids the flaw in the interface) */ with an error (this avoids the flaw in the interface) */
if ((int)len < 0) { if ((int)len < 0)
{
gz_error(state, Z_DATA_ERROR, "requested length does not fit in int"); gz_error(state, Z_DATA_ERROR, "requested length does not fit in int");
return 0; return 0;
} }
@ -214,31 +249,41 @@ int ZEXPORT gzwrite(file, buf, len)
return 0; return 0;
/* check for seek request */ /* check for seek request */
if (state->seek) { if (state->seek)
{
state->seek = 0; state->seek = 0;
if (gz_zero(state, state->skip) == -1) if (gz_zero(state, state->skip) == -1)
return 0; return 0;
} }
/* for small len, copy to input buffer, otherwise compress directly */ /* for small len, copy to input buffer, otherwise compress directly */
if (len < state->size) { if (len < state->size)
{
/* copy to input buffer, compress when full */ /* copy to input buffer, compress when full */
do { do
{
if (strm->avail_in == 0) if (strm->avail_in == 0)
strm->next_in = state->in; strm->next_in = state->in;
n = state->size - strm->avail_in; n = state->size - strm->avail_in;
if (n > len) if (n > len)
n = len; n = len;
memcpy(strm->next_in + strm->avail_in, buf, n); memcpy(strm->next_in + strm->avail_in, buf, n);
strm->avail_in += n; strm->avail_in += n;
state->x.pos += n; state->x.pos += n;
buf = (char*)buf + n; buf = (char*)buf + n;
len -= n; len -= n;
if (len && gz_comp(state, Z_NO_FLUSH) == -1) if (len && gz_comp(state, Z_NO_FLUSH) == -1)
return 0; return 0;
} while (len);
} }
else { while (len);
}
else
{
/* consume whatever's left in the input buffer */ /* consume whatever's left in the input buffer */
if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1) if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
return 0; return 0;
@ -247,6 +292,7 @@ int ZEXPORT gzwrite(file, buf, len)
strm->avail_in = len; strm->avail_in = len;
strm->next_in = (voidp)buf; strm->next_in = (voidp)buf;
state->x.pos += len; state->x.pos += len;
if (gz_comp(state, Z_NO_FLUSH) == -1) if (gz_comp(state, Z_NO_FLUSH) == -1)
return 0; return 0;
} }
@ -271,6 +317,7 @@ int ZEXPORT gzputc(file, c)
/* get internal structure */ /* get internal structure */
if (file == NULL) if (file == NULL)
return -1; return -1;
state = (gz_statep)file; state = (gz_statep)file;
strm = &(state->strm); strm = &(state->strm);
@ -279,17 +326,21 @@ int ZEXPORT gzputc(file, c)
return -1; return -1;
/* check for seek request */ /* check for seek request */
if (state->seek) { if (state->seek)
{
state->seek = 0; state->seek = 0;
if (gz_zero(state, state->skip) == -1) if (gz_zero(state, state->skip) == -1)
return -1; return -1;
} }
/* try writing to input buffer for speed (state->size == 0 if buffer not /* try writing to input buffer for speed (state->size == 0 if buffer not
initialized) */ initialized) */
if (strm->avail_in < state->size) { if (strm->avail_in < state->size)
{
if (strm->avail_in == 0) if (strm->avail_in == 0)
strm->next_in = state->in; strm->next_in = state->in;
strm->next_in[strm->avail_in++] = c; strm->next_in[strm->avail_in++] = c;
state->x.pos++; state->x.pos++;
return c & 0xff; return c & 0xff;
@ -297,8 +348,10 @@ int ZEXPORT gzputc(file, c)
/* no room in buffer or not initialized, use gz_write() */ /* no room in buffer or not initialized, use gz_write() */
buf[0] = c; buf[0] = c;
if (gzwrite(file, buf, 1) != 1) if (gzwrite(file, buf, 1) != 1)
return -1; return -1;
return c & 0xff; return c & 0xff;
} }
@ -334,6 +387,7 @@ int ZEXPORTVA gzprintf (gzFile file, const char *format, ...)
/* get internal structure */ /* get internal structure */
if (file == NULL) if (file == NULL)
return -1; return -1;
state = (gz_statep)file; state = (gz_statep)file;
strm = &(state->strm); strm = &(state->strm);
@ -346,8 +400,10 @@ int ZEXPORTVA gzprintf (gzFile file, const char *format, ...)
return 0; return 0;
/* check for seek request */ /* check for seek request */
if (state->seek) { if (state->seek)
{
state->seek = 0; state->seek = 0;
if (gz_zero(state, state->skip) == -1) if (gz_zero(state, state->skip) == -1)
return 0; return 0;
} }
@ -364,8 +420,10 @@ int ZEXPORTVA gzprintf (gzFile file, const char *format, ...)
# ifdef HAS_vsprintf_void # ifdef HAS_vsprintf_void
(void)vsprintf((char*)(state->in), format, va); (void)vsprintf((char*)(state->in), format, va);
va_end(va); va_end(va);
for (len = 0; len < size; len++) for (len = 0; len < size; len++)
if (state->in[len] == 0) break; if (state->in[len] == 0) break;
# else # else
len = vsprintf((char*)(state->in), format, va); len = vsprintf((char*)(state->in), format, va);
va_end(va); va_end(va);
@ -409,6 +467,7 @@ int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
/* get internal structure */ /* get internal structure */
if (file == NULL) if (file == NULL)
return -1; return -1;
state = (gz_statep)file; state = (gz_statep)file;
strm = &(state->strm); strm = &(state->strm);
@ -425,8 +484,10 @@ int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
return 0; return 0;
/* check for seek request */ /* check for seek request */
if (state->seek) { if (state->seek)
{
state->seek = 0; state->seek = 0;
if (gz_zero(state, state->skip) == -1) if (gz_zero(state, state->skip) == -1)
return 0; return 0;
} }
@ -442,8 +503,10 @@ int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
# ifdef HAS_sprintf_void # ifdef HAS_sprintf_void
sprintf((char*)(state->in), format, a1, a2, a3, a4, a5, a6, a7, a8, sprintf((char*)(state->in), format, a1, a2, a3, a4, a5, a6, a7, a8,
a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20); a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
for (len = 0; len < size; len++) for (len = 0; len < size; len++)
if (state->in[len] == 0) break; if (state->in[len] == 0) break;
# else # else
len = sprintf((char*)(state->in), format, a1, a2, a3, a4, a5, a6, a7, a8, len = sprintf((char*)(state->in), format, a1, a2, a3, a4, a5, a6, a7, a8,
a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20); a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
@ -487,6 +550,7 @@ int ZEXPORT gzflush(file, flush)
/* get internal structure */ /* get internal structure */
if (file == NULL) if (file == NULL)
return -1; return -1;
state = (gz_statep)file; state = (gz_statep)file;
/* check that we're writing and that there's no error */ /* check that we're writing and that there's no error */
@ -498,8 +562,10 @@ int ZEXPORT gzflush(file, flush)
return Z_STREAM_ERROR; return Z_STREAM_ERROR;
/* check for seek request */ /* check for seek request */
if (state->seek) { if (state->seek)
{
state->seek = 0; state->seek = 0;
if (gz_zero(state, state->skip) == -1) if (gz_zero(state, state->skip) == -1)
return -1; return -1;
} }
@ -525,6 +591,7 @@ int ZEXPORT gzsetparams(file, level, strategy)
/* get internal structure */ /* get internal structure */
if (file == NULL) if (file == NULL)
return Z_STREAM_ERROR; return Z_STREAM_ERROR;
state = (gz_statep)file; state = (gz_statep)file;
strm = &(state->strm); strm = &(state->strm);
@ -537,19 +604,24 @@ int ZEXPORT gzsetparams(file, level, strategy)
return Z_OK; return Z_OK;
/* check for seek request */ /* check for seek request */
if (state->seek) { if (state->seek)
{
state->seek = 0; state->seek = 0;
if (gz_zero(state, state->skip) == -1) if (gz_zero(state, state->skip) == -1)
return -1; return -1;
} }
/* change compression parameters for subsequent input */ /* change compression parameters for subsequent input */
if (state->size) { if (state->size)
{
/* flush previous input with previous parameters before changing */ /* flush previous input with previous parameters before changing */
if (strm->avail_in && gz_comp(state, Z_PARTIAL_FLUSH) == -1) if (strm->avail_in && gz_comp(state, Z_PARTIAL_FLUSH) == -1)
return state->err; return state->err;
deflateParams(strm, level, strategy); deflateParams(strm, level, strategy);
} }
state->level = level; state->level = level;
state->strategy = strategy; state->strategy = strategy;
return Z_OK; return Z_OK;
@ -569,6 +641,7 @@ int ZEXPORT gzclose_w(file)
/* get internal structure */ /* get internal structure */
if (file == NULL) if (file == NULL)
return Z_STREAM_ERROR; return Z_STREAM_ERROR;
state = (gz_statep)file; state = (gz_statep)file;
/* check that we're writing */ /* check that we're writing */
@ -576,26 +649,35 @@ int ZEXPORT gzclose_w(file)
return Z_STREAM_ERROR; return Z_STREAM_ERROR;
/* check for seek request */ /* check for seek request */
if (state->seek) { if (state->seek)
{
state->seek = 0; state->seek = 0;
if (gz_zero(state, state->skip) == -1) if (gz_zero(state, state->skip) == -1)
ret = state->err; ret = state->err;
} }
/* flush, free memory, and close file */ /* flush, free memory, and close file */
if (state->size) { if (state->size)
{
if (gz_comp(state, Z_FINISH) == -1) if (gz_comp(state, Z_FINISH) == -1)
ret = state->err; ret = state->err;
if (!state->direct) {
if (!state->direct)
{
(void)deflateEnd(&(state->strm)); (void)deflateEnd(&(state->strm));
free(state->out); free(state->out);
} }
free(state->in); free(state->in);
} }
gz_error(state, Z_OK, NULL); gz_error(state, Z_OK, NULL);
free(state->path); free(state->path);
if (close(state->fd) == -1) if (close(state->fd) == -1)
ret = Z_ERRNO; ret = Z_ERRNO;
free(state); free(state);
return ret; return ret;
} }

211
extern/zlib/infback.c vendored
View File

@ -58,11 +58,15 @@ int stream_size;
if (version == Z_NULL || version[0] != ZLIB_VERSION[0] || if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
stream_size != (int)(sizeof(z_stream))) stream_size != (int)(sizeof(z_stream)))
return Z_VERSION_ERROR; return Z_VERSION_ERROR;
if (strm == Z_NULL || window == Z_NULL || if (strm == Z_NULL || window == Z_NULL ||
windowBits < 8 || windowBits > 15) windowBits < 8 || windowBits > 15)
return Z_STREAM_ERROR; return Z_STREAM_ERROR;
strm->msg = Z_NULL; /* in case we return an error */ strm->msg = Z_NULL; /* in case we return an error */
if (strm->zalloc == (alloc_func)0) {
if (strm->zalloc == (alloc_func)0)
{
#ifdef Z_SOLO #ifdef Z_SOLO
return Z_STREAM_ERROR; return Z_STREAM_ERROR;
#else #else
@ -70,15 +74,19 @@ int stream_size;
strm->opaque = (voidpf)0; strm->opaque = (voidpf)0;
#endif #endif
} }
if (strm->zfree == (free_func)0) if (strm->zfree == (free_func)0)
#ifdef Z_SOLO #ifdef Z_SOLO
return Z_STREAM_ERROR; return Z_STREAM_ERROR;
#else #else
strm->zfree = zcfree; strm->zfree = zcfree;
#endif #endif
state = (struct inflate_state FAR*)ZALLOC(strm, 1, state = (struct inflate_state FAR*)ZALLOC(strm, 1,
sizeof(struct inflate_state)); sizeof(struct inflate_state));
if (state == Z_NULL) return Z_MEM_ERROR; if (state == Z_NULL) return Z_MEM_ERROR;
Tracev((stderr, "inflate: allocated\n")); Tracev((stderr, "inflate: allocated\n"));
strm->state = (struct internal_state FAR*)state; strm->state = (struct internal_state FAR*)state;
state->dmax = 32768U; state->dmax = 32768U;
@ -113,16 +121,22 @@ struct inflate_state FAR *state;
static code fixed[544]; static code fixed[544];
/* build fixed huffman tables if first call (may not be thread safe) */ /* build fixed huffman tables if first call (may not be thread safe) */
if (virgin) { if (virgin)
{
unsigned sym, bits; unsigned sym, bits;
static code* next; static code* next;
/* literal/length table */ /* literal/length table */
sym = 0; sym = 0;
while (sym < 144) state->lens[sym++] = 8; while (sym < 144) state->lens[sym++] = 8;
while (sym < 256) state->lens[sym++] = 9; while (sym < 256) state->lens[sym++] = 9;
while (sym < 280) state->lens[sym++] = 7; while (sym < 280) state->lens[sym++] = 7;
while (sym < 288) state->lens[sym++] = 8; while (sym < 288) state->lens[sym++] = 8;
next = fixed; next = fixed;
lenfix = next; lenfix = next;
bits = 9; bits = 9;
@ -130,7 +144,9 @@ struct inflate_state FAR *state;
/* distance table */ /* distance table */
sym = 0; sym = 0;
while (sym < 32) state->lens[sym++] = 5; while (sym < 32) state->lens[sym++] = 5;
distfix = next; distfix = next;
bits = 5; bits = 5;
inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work); inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work);
@ -138,6 +154,7 @@ struct inflate_state FAR *state;
/* do this just once */ /* do this just once */
virgin = 0; virgin = 0;
} }
#else /* !BUILDFIXED */ #else /* !BUILDFIXED */
# include "inffixed.h" # include "inffixed.h"
#endif /* BUILDFIXED */ #endif /* BUILDFIXED */
@ -301,6 +318,7 @@ void FAR *out_desc;
/* Check that the strm exists and that the state was initialized */ /* Check that the strm exists and that the state was initialized */
if (strm == Z_NULL || strm->state == Z_NULL) if (strm == Z_NULL || strm->state == Z_NULL)
return Z_STREAM_ERROR; return Z_STREAM_ERROR;
state = (struct inflate_state FAR*)strm->state; state = (struct inflate_state FAR*)strm->state;
/* Reset the state */ /* Reset the state */
@ -317,38 +335,48 @@ void FAR *out_desc;
/* Inflate until end of block marked as last */ /* Inflate until end of block marked as last */
for (;;) for (;;)
switch (state->mode) { switch (state->mode)
{
case TYPE: case TYPE:
/* determine and dispatch block type */ /* determine and dispatch block type */
if (state->last) { if (state->last)
{
BYTEBITS(); BYTEBITS();
state->mode = DONE; state->mode = DONE;
break; break;
} }
NEEDBITS(3); NEEDBITS(3);
state->last = BITS(1); state->last = BITS(1);
DROPBITS(1); DROPBITS(1);
switch (BITS(2)) {
switch (BITS(2))
{
case 0: /* stored block */ case 0: /* stored block */
Tracev((stderr, "inflate: stored block%s\n", Tracev((stderr, "inflate: stored block%s\n",
state->last ? " (last)" : "")); state->last ? " (last)" : ""));
state->mode = STORED; state->mode = STORED;
break; break;
case 1: /* fixed block */ case 1: /* fixed block */
fixedtables(state); fixedtables(state);
Tracev((stderr, "inflate: fixed codes block%s\n", Tracev((stderr, "inflate: fixed codes block%s\n",
state->last ? " (last)" : "")); state->last ? " (last)" : ""));
state->mode = LEN; /* decode codes */ state->mode = LEN; /* decode codes */
break; break;
case 2: /* dynamic block */ case 2: /* dynamic block */
Tracev((stderr, "inflate: dynamic codes block%s\n", Tracev((stderr, "inflate: dynamic codes block%s\n",
state->last ? " (last)" : "")); state->last ? " (last)" : ""));
state->mode = TABLE; state->mode = TABLE;
break; break;
case 3: case 3:
strm->msg = (char*)"invalid block type"; strm->msg = (char*)"invalid block type";
state->mode = BAD; state->mode = BAD;
} }
DROPBITS(2); DROPBITS(2);
break; break;
@ -356,23 +384,30 @@ void FAR *out_desc;
/* get and verify stored block length */ /* get and verify stored block length */
BYTEBITS(); /* go to byte boundary */ BYTEBITS(); /* go to byte boundary */
NEEDBITS(32); NEEDBITS(32);
if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff))
{
strm->msg = (char*)"invalid stored block lengths"; strm->msg = (char*)"invalid stored block lengths";
state->mode = BAD; state->mode = BAD;
break; break;
} }
state->length = (unsigned)hold & 0xffff; state->length = (unsigned)hold & 0xffff;
Tracev((stderr, "inflate: stored length %u\n", Tracev((stderr, "inflate: stored length %u\n",
state->length)); state->length));
INITBITS(); INITBITS();
/* copy stored block from input to output */ /* copy stored block from input to output */
while (state->length != 0) { while (state->length != 0)
{
copy = state->length; copy = state->length;
PULL(); PULL();
ROOM(); ROOM();
if (copy > have) copy = have; if (copy > have) copy = have;
if (copy > left) copy = left; if (copy > left) copy = left;
zmemcpy(put, next, copy); zmemcpy(put, next, copy);
have -= copy; have -= copy;
next += copy; next += copy;
@ -380,6 +415,7 @@ void FAR *out_desc;
put += copy; put += copy;
state->length -= copy; state->length -= copy;
} }
Tracev((stderr, "inflate: stored end\n")); Tracev((stderr, "inflate: stored end\n"));
state->mode = TYPE; state->mode = TYPE;
break; break;
@ -394,79 +430,106 @@ void FAR *out_desc;
state->ncode = BITS(4) + 4; state->ncode = BITS(4) + 4;
DROPBITS(4); DROPBITS(4);
#ifndef PKZIP_BUG_WORKAROUND #ifndef PKZIP_BUG_WORKAROUND
if (state->nlen > 286 || state->ndist > 30) {
if (state->nlen > 286 || state->ndist > 30)
{
strm->msg = (char*)"too many length or distance symbols"; strm->msg = (char*)"too many length or distance symbols";
state->mode = BAD; state->mode = BAD;
break; break;
} }
#endif #endif
Tracev((stderr, "inflate: table sizes ok\n")); Tracev((stderr, "inflate: table sizes ok\n"));
/* get code length code lengths (not a typo) */ /* get code length code lengths (not a typo) */
state->have = 0; state->have = 0;
while (state->have < state->ncode) {
while (state->have < state->ncode)
{
NEEDBITS(3); NEEDBITS(3);
state->lens[order[state->have++]] = (unsigned short)BITS(3); state->lens[order[state->have++]] = (unsigned short)BITS(3);
DROPBITS(3); DROPBITS(3);
} }
while (state->have < 19) while (state->have < 19)
state->lens[order[state->have++]] = 0; state->lens[order[state->have++]] = 0;
state->next = state->codes; state->next = state->codes;
state->lencode = (code const FAR*)(state->next); state->lencode = (code const FAR*)(state->next);
state->lenbits = 7; state->lenbits = 7;
ret = inflate_table(CODES, state->lens, 19, &(state->next), ret = inflate_table(CODES, state->lens, 19, &(state->next),
&(state->lenbits), state->work); &(state->lenbits), state->work);
if (ret) {
if (ret)
{
strm->msg = (char*)"invalid code lengths set"; strm->msg = (char*)"invalid code lengths set";
state->mode = BAD; state->mode = BAD;
break; break;
} }
Tracev((stderr, "inflate: code lengths ok\n")); Tracev((stderr, "inflate: code lengths ok\n"));
/* get length and distance code code lengths */ /* get length and distance code code lengths */
state->have = 0; state->have = 0;
while (state->have < state->nlen + state->ndist) {
for (;;) { while (state->have < state->nlen + state->ndist)
{
for (;;)
{
here = state->lencode[BITS(state->lenbits)]; here = state->lencode[BITS(state->lenbits)];
if ((unsigned)(here.bits) <= bits) break; if ((unsigned)(here.bits) <= bits) break;
PULLBYTE(); PULLBYTE();
} }
if (here.val < 16) {
if (here.val < 16)
{
DROPBITS(here.bits); DROPBITS(here.bits);
state->lens[state->have++] = here.val; state->lens[state->have++] = here.val;
} }
else { else
if (here.val == 16) { {
if (here.val == 16)
{
NEEDBITS(here.bits + 2); NEEDBITS(here.bits + 2);
DROPBITS(here.bits); DROPBITS(here.bits);
if (state->have == 0) {
if (state->have == 0)
{
strm->msg = (char*)"invalid bit length repeat"; strm->msg = (char*)"invalid bit length repeat";
state->mode = BAD; state->mode = BAD;
break; break;
} }
len = (unsigned)(state->lens[state->have - 1]); len = (unsigned)(state->lens[state->have - 1]);
copy = 3 + BITS(2); copy = 3 + BITS(2);
DROPBITS(2); DROPBITS(2);
} }
else if (here.val == 17) { else if (here.val == 17)
{
NEEDBITS(here.bits + 3); NEEDBITS(here.bits + 3);
DROPBITS(here.bits); DROPBITS(here.bits);
len = 0; len = 0;
copy = 3 + BITS(3); copy = 3 + BITS(3);
DROPBITS(3); DROPBITS(3);
} }
else { else
{
NEEDBITS(here.bits + 7); NEEDBITS(here.bits + 7);
DROPBITS(here.bits); DROPBITS(here.bits);
len = 0; len = 0;
copy = 11 + BITS(7); copy = 11 + BITS(7);
DROPBITS(7); DROPBITS(7);
} }
if (state->have + copy > state->nlen + state->ndist) {
if (state->have + copy > state->nlen + state->ndist)
{
strm->msg = (char*)"invalid bit length repeat"; strm->msg = (char*)"invalid bit length repeat";
state->mode = BAD; state->mode = BAD;
break; break;
} }
while (copy--) while (copy--)
state->lens[state->have++] = (unsigned short)len; state->lens[state->have++] = (unsigned short)len;
} }
@ -476,7 +539,8 @@ void FAR *out_desc;
if (state->mode == BAD) break; if (state->mode == BAD) break;
/* check for end-of-block code (better have one) */ /* check for end-of-block code (better have one) */
if (state->lens[256] == 0) { if (state->lens[256] == 0)
{
strm->msg = (char*)"invalid code -- missing end-of-block"; strm->msg = (char*)"invalid code -- missing end-of-block";
state->mode = BAD; state->mode = BAD;
break; break;
@ -490,55 +554,77 @@ void FAR *out_desc;
state->lenbits = 9; state->lenbits = 9;
ret = inflate_table(LENS, state->lens, state->nlen, &(state->next), ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
&(state->lenbits), state->work); &(state->lenbits), state->work);
if (ret) {
if (ret)
{
strm->msg = (char*)"invalid literal/lengths set"; strm->msg = (char*)"invalid literal/lengths set";
state->mode = BAD; state->mode = BAD;
break; break;
} }
state->distcode = (code const FAR*)(state->next); state->distcode = (code const FAR*)(state->next);
state->distbits = 6; state->distbits = 6;
ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist, ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
&(state->next), &(state->distbits), state->work); &(state->next), &(state->distbits), state->work);
if (ret) {
if (ret)
{
strm->msg = (char*)"invalid distances set"; strm->msg = (char*)"invalid distances set";
state->mode = BAD; state->mode = BAD;
break; break;
} }
Tracev((stderr, "inflate: codes ok\n")); Tracev((stderr, "inflate: codes ok\n"));
state->mode = LEN; state->mode = LEN;
case LEN: case LEN:
/* use inflate_fast() if we have enough input and output */ /* use inflate_fast() if we have enough input and output */
if (have >= 6 && left >= 258) { if (have >= 6 && left >= 258)
{
RESTORE(); RESTORE();
if (state->whave < state->wsize) if (state->whave < state->wsize)
state->whave = state->wsize - left; state->whave = state->wsize - left;
inflate_fast(strm, state->wsize); inflate_fast(strm, state->wsize);
LOAD(); LOAD();
break; break;
} }
/* get a literal, length, or end-of-block code */ /* get a literal, length, or end-of-block code */
for (;;) { for (;;)
{
here = state->lencode[BITS(state->lenbits)]; here = state->lencode[BITS(state->lenbits)];
if ((unsigned)(here.bits) <= bits) break; if ((unsigned)(here.bits) <= bits) break;
PULLBYTE(); PULLBYTE();
} }
if (here.op && (here.op & 0xf0) == 0) {
if (here.op && (here.op & 0xf0) == 0)
{
last = here; last = here;
for (;;) {
for (;;)
{
here = state->lencode[last.val + here = state->lencode[last.val +
(BITS(last.bits + last.op) >> last.bits)]; (BITS(last.bits + last.op) >> last.bits)];
if ((unsigned)(last.bits + here.bits) <= bits) break; if ((unsigned)(last.bits + here.bits) <= bits) break;
PULLBYTE(); PULLBYTE();
} }
DROPBITS(last.bits); DROPBITS(last.bits);
} }
DROPBITS(here.bits); DROPBITS(here.bits);
state->length = (unsigned)here.val; state->length = (unsigned)here.val;
/* process literal */ /* process literal */
if (here.op == 0) { if (here.op == 0)
{
Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ? Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
"inflate: literal '%c'\n" : "inflate: literal '%c'\n" :
"inflate: literal 0x%02x\n", here.val)); "inflate: literal 0x%02x\n", here.val));
@ -550,14 +636,16 @@ void FAR *out_desc;
} }
/* process end of block */ /* process end of block */
if (here.op & 32) { if (here.op & 32)
{
Tracevv((stderr, "inflate: end of block\n")); Tracevv((stderr, "inflate: end of block\n"));
state->mode = TYPE; state->mode = TYPE;
break; break;
} }
/* invalid code */ /* invalid code */
if (here.op & 64) { if (here.op & 64)
{
strm->msg = (char*)"invalid literal/length code"; strm->msg = (char*)"invalid literal/length code";
state->mode = BAD; state->mode = BAD;
break; break;
@ -565,80 +653,116 @@ void FAR *out_desc;
/* length code -- get extra bits, if any */ /* length code -- get extra bits, if any */
state->extra = (unsigned)(here.op) & 15; state->extra = (unsigned)(here.op) & 15;
if (state->extra != 0) {
if (state->extra != 0)
{
NEEDBITS(state->extra); NEEDBITS(state->extra);
state->length += BITS(state->extra); state->length += BITS(state->extra);
DROPBITS(state->extra); DROPBITS(state->extra);
} }
Tracevv((stderr, "inflate: length %u\n", state->length)); Tracevv((stderr, "inflate: length %u\n", state->length));
/* get distance code */ /* get distance code */
for (;;) { for (;;)
{
here = state->distcode[BITS(state->distbits)]; here = state->distcode[BITS(state->distbits)];
if ((unsigned)(here.bits) <= bits) break; if ((unsigned)(here.bits) <= bits) break;
PULLBYTE(); PULLBYTE();
} }
if ((here.op & 0xf0) == 0) {
if ((here.op & 0xf0) == 0)
{
last = here; last = here;
for (;;) {
for (;;)
{
here = state->distcode[last.val + here = state->distcode[last.val +
(BITS(last.bits + last.op) >> last.bits)]; (BITS(last.bits + last.op) >> last.bits)];
if ((unsigned)(last.bits + here.bits) <= bits) break; if ((unsigned)(last.bits + here.bits) <= bits) break;
PULLBYTE(); PULLBYTE();
} }
DROPBITS(last.bits); DROPBITS(last.bits);
} }
DROPBITS(here.bits); DROPBITS(here.bits);
if (here.op & 64) {
if (here.op & 64)
{
strm->msg = (char*)"invalid distance code"; strm->msg = (char*)"invalid distance code";
state->mode = BAD; state->mode = BAD;
break; break;
} }
state->offset = (unsigned)here.val; state->offset = (unsigned)here.val;
/* get distance extra bits, if any */ /* get distance extra bits, if any */
state->extra = (unsigned)(here.op) & 15; state->extra = (unsigned)(here.op) & 15;
if (state->extra != 0) {
if (state->extra != 0)
{
NEEDBITS(state->extra); NEEDBITS(state->extra);
state->offset += BITS(state->extra); state->offset += BITS(state->extra);
DROPBITS(state->extra); DROPBITS(state->extra);
} }
if (state->offset > state->wsize - (state->whave < state->wsize ? if (state->offset > state->wsize - (state->whave < state->wsize ?
left : 0)) { left : 0))
{
strm->msg = (char*)"invalid distance too far back"; strm->msg = (char*)"invalid distance too far back";
state->mode = BAD; state->mode = BAD;
break; break;
} }
Tracevv((stderr, "inflate: distance %u\n", state->offset)); Tracevv((stderr, "inflate: distance %u\n", state->offset));
/* copy match from window to output */ /* copy match from window to output */
do { do
{
ROOM(); ROOM();
copy = state->wsize - state->offset; copy = state->wsize - state->offset;
if (copy < left) {
if (copy < left)
{
from = put + copy; from = put + copy;
copy = left - copy; copy = left - copy;
} }
else { else
{
from = put - state->offset; from = put - state->offset;
copy = left; copy = left;
} }
if (copy > state->length) copy = state->length; if (copy > state->length) copy = state->length;
state->length -= copy; state->length -= copy;
left -= copy; left -= copy;
do {
do
{
*put++ = *from++; *put++ = *from++;
} while (--copy); }
} while (state->length != 0); while (--copy);
}
while (state->length != 0);
break; break;
case DONE: case DONE:
/* inflate stream terminated properly -- write leftover output */ /* inflate stream terminated properly -- write leftover output */
ret = Z_STREAM_END; ret = Z_STREAM_END;
if (left < state->wsize) {
if (left < state->wsize)
{
if (out(out_desc, state->window, state->wsize - left)) if (out(out_desc, state->window, state->wsize - left))
ret = Z_BUF_ERROR; ret = Z_BUF_ERROR;
} }
goto inf_leave; goto inf_leave;
case BAD: case BAD:
@ -666,6 +790,7 @@ z_streamp strm;
{ {
if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0) if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0)
return Z_STREAM_ERROR; return Z_STREAM_ERROR;
ZFREE(strm, strm->state); ZFREE(strm, strm->state);
strm->state = Z_NULL; strm->state = Z_NULL;
Tracev((stderr, "inflate: end\n")); Tracev((stderr, "inflate: end\n"));

191
extern/zlib/inffast.c vendored
View File

@ -121,194 +121,287 @@ unsigned start; /* inflate()'s starting value for strm->avail_out */
/* decode literals and length/distances until end-of-block or not enough /* decode literals and length/distances until end-of-block or not enough
input data or output space */ input data or output space */
do { do
if (bits < 15) { {
if (bits < 15)
{
hold += (unsigned long)(PUP(in)) << bits; hold += (unsigned long)(PUP(in)) << bits;
bits += 8; bits += 8;
hold += (unsigned long)(PUP(in)) << bits; hold += (unsigned long)(PUP(in)) << bits;
bits += 8; bits += 8;
} }
here = lcode[hold & lmask]; here = lcode[hold & lmask];
dolen: dolen:
op = (unsigned)(here.bits); op = (unsigned)(here.bits);
hold >>= op; hold >>= op;
bits -= op; bits -= op;
op = (unsigned)(here.op); op = (unsigned)(here.op);
if (op == 0) { /* literal */
if (op == 0) /* literal */
{
Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ? Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
"inflate: literal '%c'\n" : "inflate: literal '%c'\n" :
"inflate: literal 0x%02x\n", here.val)); "inflate: literal 0x%02x\n", here.val));
PUP(out) = (unsigned char)(here.val); PUP(out) = (unsigned char)(here.val);
} }
else if (op & 16) { /* length base */ else if (op & 16) /* length base */
{
len = (unsigned)(here.val); len = (unsigned)(here.val);
op &= 15; /* number of extra bits */ op &= 15; /* number of extra bits */
if (op) {
if (bits < op) { if (op)
{
if (bits < op)
{
hold += (unsigned long)(PUP(in)) << bits; hold += (unsigned long)(PUP(in)) << bits;
bits += 8; bits += 8;
} }
len += (unsigned)hold & ((1U << op) - 1); len += (unsigned)hold & ((1U << op) - 1);
hold >>= op; hold >>= op;
bits -= op; bits -= op;
} }
Tracevv((stderr, "inflate: length %u\n", len)); Tracevv((stderr, "inflate: length %u\n", len));
if (bits < 15) {
if (bits < 15)
{
hold += (unsigned long)(PUP(in)) << bits; hold += (unsigned long)(PUP(in)) << bits;
bits += 8; bits += 8;
hold += (unsigned long)(PUP(in)) << bits; hold += (unsigned long)(PUP(in)) << bits;
bits += 8; bits += 8;
} }
here = dcode[hold & dmask]; here = dcode[hold & dmask];
dodist: dodist:
op = (unsigned)(here.bits); op = (unsigned)(here.bits);
hold >>= op; hold >>= op;
bits -= op; bits -= op;
op = (unsigned)(here.op); op = (unsigned)(here.op);
if (op & 16) { /* distance base */
if (op & 16) /* distance base */
{
dist = (unsigned)(here.val); dist = (unsigned)(here.val);
op &= 15; /* number of extra bits */ op &= 15; /* number of extra bits */
if (bits < op) {
if (bits < op)
{
hold += (unsigned long)(PUP(in)) << bits; hold += (unsigned long)(PUP(in)) << bits;
bits += 8; bits += 8;
if (bits < op) {
if (bits < op)
{
hold += (unsigned long)(PUP(in)) << bits; hold += (unsigned long)(PUP(in)) << bits;
bits += 8; bits += 8;
} }
} }
dist += (unsigned)hold & ((1U << op) - 1); dist += (unsigned)hold & ((1U << op) - 1);
#ifdef INFLATE_STRICT #ifdef INFLATE_STRICT
if (dist > dmax) {
if (dist > dmax)
{
strm->msg = (char*)"invalid distance too far back"; strm->msg = (char*)"invalid distance too far back";
state->mode = BAD; state->mode = BAD;
break; break;
} }
#endif #endif
hold >>= op; hold >>= op;
bits -= op; bits -= op;
Tracevv((stderr, "inflate: distance %u\n", dist)); Tracevv((stderr, "inflate: distance %u\n", dist));
op = (unsigned)(out - beg); /* max distance in output */ op = (unsigned)(out - beg); /* max distance in output */
if (dist > op) { /* see if copy from window */
if (dist > op) /* see if copy from window */
{
op = dist - op; /* distance back in window */ op = dist - op; /* distance back in window */
if (op > whave) {
if (state->sane) { if (op > whave)
{
if (state->sane)
{
strm->msg = strm->msg =
(char*)"invalid distance too far back"; (char*)"invalid distance too far back";
state->mode = BAD; state->mode = BAD;
break; break;
} }
#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
if (len <= op - whave) {
do { if (len <= op - whave)
{
do
{
PUP(out) = 0; PUP(out) = 0;
} while (--len); }
while (--len);
continue; continue;
} }
len -= op - whave; len -= op - whave;
do {
do
{
PUP(out) = 0; PUP(out) = 0;
} while (--op > whave); }
if (op == 0) { while (--op > whave);
if (op == 0)
{
from = out - dist; from = out - dist;
do {
do
{
PUP(out) = PUP(from); PUP(out) = PUP(from);
} while (--len); }
while (--len);
continue; continue;
} }
#endif #endif
} }
from = window - OFF; from = window - OFF;
if (wnext == 0) { /* very common case */
if (wnext == 0) /* very common case */
{
from += wsize - op; from += wsize - op;
if (op < len) { /* some from window */
if (op < len) /* some from window */
{
len -= op; len -= op;
do {
do
{
PUP(out) = PUP(from); PUP(out) = PUP(from);
} while (--op); }
while (--op);
from = out - dist; /* rest from output */ from = out - dist; /* rest from output */
} }
} }
else if (wnext < op) { /* wrap around window */ else if (wnext < op) /* wrap around window */
{
from += wsize + wnext - op; from += wsize + wnext - op;
op -= wnext; op -= wnext;
if (op < len) { /* some from end of window */
if (op < len) /* some from end of window */
{
len -= op; len -= op;
do {
do
{
PUP(out) = PUP(from); PUP(out) = PUP(from);
} while (--op); }
while (--op);
from = window - OFF; from = window - OFF;
if (wnext < len) { /* some from start of window */
if (wnext < len) /* some from start of window */
{
op = wnext; op = wnext;
len -= op; len -= op;
do {
do
{
PUP(out) = PUP(from); PUP(out) = PUP(from);
} while (--op); }
while (--op);
from = out - dist; /* rest from output */ from = out - dist; /* rest from output */
} }
} }
} }
else { /* contiguous in window */ else /* contiguous in window */
{
from += wnext - op; from += wnext - op;
if (op < len) { /* some from window */
if (op < len) /* some from window */
{
len -= op; len -= op;
do {
do
{
PUP(out) = PUP(from); PUP(out) = PUP(from);
} while (--op); }
while (--op);
from = out - dist; /* rest from output */ from = out - dist; /* rest from output */
} }
} }
while (len > 2) {
while (len > 2)
{
PUP(out) = PUP(from); PUP(out) = PUP(from);
PUP(out) = PUP(from); PUP(out) = PUP(from);
PUP(out) = PUP(from); PUP(out) = PUP(from);
len -= 3; len -= 3;
} }
if (len) {
if (len)
{
PUP(out) = PUP(from); PUP(out) = PUP(from);
if (len > 1) if (len > 1)
PUP(out) = PUP(from); PUP(out) = PUP(from);
} }
} }
else { else
{
from = out - dist; /* copy direct from output */ from = out - dist; /* copy direct from output */
do { /* minimum length is three */
do /* minimum length is three */
{
PUP(out) = PUP(from); PUP(out) = PUP(from);
PUP(out) = PUP(from); PUP(out) = PUP(from);
PUP(out) = PUP(from); PUP(out) = PUP(from);
len -= 3; len -= 3;
} while (len > 2); }
if (len) { while (len > 2);
if (len)
{
PUP(out) = PUP(from); PUP(out) = PUP(from);
if (len > 1) if (len > 1)
PUP(out) = PUP(from); PUP(out) = PUP(from);
} }
} }
} }
else if ((op & 64) == 0) { /* 2nd level distance code */ else if ((op & 64) == 0) /* 2nd level distance code */
{
here = dcode[here.val + (hold & ((1U << op) - 1))]; here = dcode[here.val + (hold & ((1U << op) - 1))];
goto dodist; goto dodist;
} }
else { else
{
strm->msg = (char*)"invalid distance code"; strm->msg = (char*)"invalid distance code";
state->mode = BAD; state->mode = BAD;
break; break;
} }
} }
else if ((op & 64) == 0) { /* 2nd level length code */ else if ((op & 64) == 0) /* 2nd level length code */
{
here = lcode[here.val + (hold & ((1U << op) - 1))]; here = lcode[here.val + (hold & ((1U << op) - 1))];
goto dolen; goto dolen;
} }
else if (op & 32) { /* end-of-block */ else if (op & 32) /* end-of-block */
{
Tracevv((stderr, "inflate: end of block\n")); Tracevv((stderr, "inflate: end of block\n"));
state->mode = TYPE; state->mode = TYPE;
break; break;
} }
else { else
{
strm->msg = (char*)"invalid literal/length code"; strm->msg = (char*)"invalid literal/length code";
state->mode = BAD; state->mode = BAD;
break; break;
} }
} while (in < last && out < end); }
while (in < last && out < end);
/* return unused bytes (on entry, bits < 8, so in won't go too far back) */ /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
len = bits >> 3; len = bits >> 3;

View File

@ -7,7 +7,8 @@
subject to change. Applications should only use zlib.h. subject to change. Applications should only use zlib.h.
*/ */
static const code lenfix[512] = { static const code lenfix[512] =
{
{96, 7, 0}, {0, 8, 80}, {0, 8, 16}, {20, 8, 115}, {18, 7, 31}, {0, 8, 112}, {0, 8, 48}, {96, 7, 0}, {0, 8, 80}, {0, 8, 16}, {20, 8, 115}, {18, 7, 31}, {0, 8, 112}, {0, 8, 48},
{0, 9, 192}, {16, 7, 10}, {0, 8, 96}, {0, 8, 32}, {0, 9, 160}, {0, 8, 0}, {0, 8, 128}, {0, 9, 192}, {16, 7, 10}, {0, 8, 96}, {0, 8, 32}, {0, 9, 160}, {0, 8, 0}, {0, 8, 128},
{0, 8, 64}, {0, 9, 224}, {16, 7, 6}, {0, 8, 88}, {0, 8, 24}, {0, 9, 144}, {19, 7, 59}, {0, 8, 64}, {0, 9, 224}, {16, 7, 6}, {0, 8, 88}, {0, 8, 24}, {0, 9, 144}, {19, 7, 59},
@ -84,7 +85,8 @@
{0, 9, 255} {0, 9, 255}
}; };
static const code distfix[32] = { static const code distfix[32] =
{
{16, 5, 1}, {23, 5, 257}, {19, 5, 17}, {27, 5, 4097}, {17, 5, 5}, {25, 5, 1025}, {16, 5, 1}, {23, 5, 257}, {19, 5, 17}, {27, 5, 4097}, {17, 5, 5}, {25, 5, 1025},
{21, 5, 65}, {29, 5, 16385}, {16, 5, 3}, {24, 5, 513}, {20, 5, 33}, {28, 5, 8193}, {21, 5, 65}, {29, 5, 16385}, {16, 5, 3}, {24, 5, 513}, {20, 5, 33}, {28, 5, 8193},
{18, 5, 9}, {26, 5, 2049}, {22, 5, 129}, {64, 5, 0}, {16, 5, 2}, {23, 5, 385}, {18, 5, 9}, {26, 5, 2049}, {22, 5, 129}, {64, 5, 0}, {16, 5, 2}, {23, 5, 385},

474
extern/zlib/inflate.c vendored

File diff suppressed because it is too large Load Diff

View File

@ -17,7 +17,8 @@
#endif #endif
/* Possible inflate modes between inflate() calls */ /* Possible inflate modes between inflate() calls */
typedef enum { typedef enum
{
HEAD, /* i: waiting for magic header */ HEAD, /* i: waiting for magic header */
FLAGS, /* i: waiting for method and flags (gzip) */ FLAGS, /* i: waiting for method and flags (gzip) */
TIME, /* i: waiting for modification time (gzip) */ TIME, /* i: waiting for modification time (gzip) */
@ -78,7 +79,8 @@ typedef enum {
*/ */
/* state maintained between inflate() calls. Approximately 10K bytes. */ /* state maintained between inflate() calls. Approximately 10K bytes. */
struct inflate_state { struct inflate_state
{
inflate_mode mode; /* current inflate mode */ inflate_mode mode; /* current inflate mode */
int last; /* true if processing last block */ int last; /* true if processing last block */
int wrap; /* bit 0 true for zlib, bit 1 true for gzip */ int wrap; /* bit 0 true for zlib, bit 1 true for gzip */

View File

@ -74,20 +74,28 @@ unsigned short FAR *work;
int end; /* use base and extra for symbol > end */ int end; /* use base and extra for symbol > end */
unsigned short count[MAXBITS + 1]; /* number of codes of each length */ unsigned short count[MAXBITS + 1]; /* number of codes of each length */
unsigned short offs[MAXBITS + 1]; /* offsets in table for each length */ unsigned short offs[MAXBITS + 1]; /* offsets in table for each length */
static const unsigned short lbase[31] = { /* Length codes 257..285 base */ static const unsigned short lbase[31] = /* Length codes 257..285 base */
{
3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 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}; 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0
static const unsigned short lext[31] = { /* Length codes 257..285 extra */ };
static const unsigned short lext[31] = /* Length codes 257..285 extra */
{
16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18, 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18,
19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 78, 68}; 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 78, 68
static const unsigned short dbase[32] = { /* Distance codes 0..29 base */ };
static const unsigned short dbase[32] = /* Distance codes 0..29 base */
{
1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 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, 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
8193, 12289, 16385, 24577, 0, 0}; 8193, 12289, 16385, 24577, 0, 0
static const unsigned short dext[32] = { /* Distance codes 0..29 extra */ };
static const unsigned short dext[32] = /* Distance codes 0..29 extra */
{
16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22,
23, 23, 24, 24, 25, 25, 26, 26, 27, 27, 23, 23, 24, 24, 25, 25, 26, 26, 27, 27,
28, 28, 29, 29, 64, 64}; 28, 28, 29, 29, 64, 64
};
/* /*
Process a set of code lengths to create a canonical Huffman code. The Process a set of code lengths to create a canonical Huffman code. The
@ -123,15 +131,20 @@ unsigned short FAR *work;
/* accumulate lengths for codes (assumes lens[] all in 0..MAXBITS) */ /* accumulate lengths for codes (assumes lens[] all in 0..MAXBITS) */
for (len = 0; len <= MAXBITS; len++) for (len = 0; len <= MAXBITS; len++)
count[len] = 0; count[len] = 0;
for (sym = 0; sym < codes; sym++) for (sym = 0; sym < codes; sym++)
count[lens[sym]]++; count[lens[sym]]++;
/* bound code lengths, force root to be within code lengths */ /* bound code lengths, force root to be within code lengths */
root = *bits; root = *bits;
for (max = MAXBITS; max >= 1; max--) for (max = MAXBITS; max >= 1; max--)
if (count[max] != 0) break; if (count[max] != 0) break;
if (root > max) root = max; if (root > max) root = max;
if (max == 0) { /* no symbols to code at all */
if (max == 0) /* no symbols to code at all */
{
here.op = (unsigned char)64; /* invalid code marker */ here.op = (unsigned char)64; /* invalid code marker */
here.bits = (unsigned char)1; here.bits = (unsigned char)1;
here.val = (unsigned short)0; here.val = (unsigned short)0;
@ -140,22 +153,29 @@ unsigned short FAR *work;
*bits = 1; *bits = 1;
return 0; /* no symbols, but wait for decoding to report error */ return 0; /* no symbols, but wait for decoding to report error */
} }
for (min = 1; min < max; min++) for (min = 1; min < max; min++)
if (count[min] != 0) break; if (count[min] != 0) break;
if (root < min) root = min; if (root < min) root = min;
/* check for an over-subscribed or incomplete set of lengths */ /* check for an over-subscribed or incomplete set of lengths */
left = 1; left = 1;
for (len = 1; len <= MAXBITS; len++) {
for (len = 1; len <= MAXBITS; len++)
{
left <<= 1; left <<= 1;
left -= count[len]; left -= count[len];
if (left < 0) return -1; /* over-subscribed */ if (left < 0) return -1; /* over-subscribed */
} }
if (left > 0 && (type == CODES || max != 1)) if (left > 0 && (type == CODES || max != 1))
return -1; /* incomplete set */ return -1; /* incomplete set */
/* generate offsets into symbol table for each length for sorting */ /* generate offsets into symbol table for each length for sorting */
offs[1] = 0; offs[1] = 0;
for (len = 1; len < MAXBITS; len++) for (len = 1; len < MAXBITS; len++)
offs[len + 1] = offs[len] + count[len]; offs[len + 1] = offs[len] + count[len];
@ -195,11 +215,13 @@ unsigned short FAR *work;
*/ */
/* set up for code type */ /* set up for code type */
switch (type) { switch (type)
{
case CODES: case CODES:
base = extra = work; /* dummy value--not used */ base = extra = work; /* dummy value--not used */
end = 19; end = 19;
break; break;
case LENS: case LENS:
base = lbase; base = lbase;
base -= 257; base -= 257;
@ -207,6 +229,7 @@ unsigned short FAR *work;
extra -= 257; extra -= 257;
end = 256; end = 256;
break; break;
default: /* DISTS */ default: /* DISTS */
base = dbase; base = dbase;
extra = dext; extra = dext;
@ -230,18 +253,23 @@ unsigned short FAR *work;
return 1; return 1;
/* process all codes and make table entries */ /* process all codes and make table entries */
for (;;) { for (;;)
{
/* create table entry */ /* create table entry */
here.bits = (unsigned char)(len - drop); here.bits = (unsigned char)(len - drop);
if ((int)(work[sym]) < end) {
if ((int)(work[sym]) < end)
{
here.op = (unsigned char)0; here.op = (unsigned char)0;
here.val = work[sym]; here.val = work[sym];
} }
else if ((int)(work[sym]) > end) { else if ((int)(work[sym]) > end)
{
here.op = (unsigned char)(extra[work[sym]]); here.op = (unsigned char)(extra[work[sym]]);
here.val = base[work[sym]]; here.val = base[work[sym]];
} }
else { else
{
here.op = (unsigned char)(32 + 64); /* end of block */ here.op = (unsigned char)(32 + 64); /* end of block */
here.val = 0; here.val = 0;
} }
@ -250,16 +278,22 @@ unsigned short FAR *work;
incr = 1U << (len - drop); incr = 1U << (len - drop);
fill = 1U << curr; fill = 1U << curr;
min = fill; /* save offset to next table */ min = fill; /* save offset to next table */
do {
do
{
fill -= incr; fill -= incr;
next[(huff >> drop) + fill] = here; next[(huff >> drop) + fill] = here;
} while (fill != 0); }
while (fill != 0);
/* backwards increment the len-bit code huff */ /* backwards increment the len-bit code huff */
incr = 1U << (len - 1); incr = 1U << (len - 1);
while (huff & incr) while (huff & incr)
incr >>= 1; incr >>= 1;
if (incr != 0) {
if (incr != 0)
{
huff &= incr - 1; huff &= incr - 1;
huff += incr; huff += incr;
} }
@ -268,13 +302,17 @@ unsigned short FAR *work;
/* go to next symbol, update count, len */ /* go to next symbol, update count, len */
sym++; sym++;
if (--(count[len]) == 0) {
if (--(count[len]) == 0)
{
if (len == max) break; if (len == max) break;
len = lens[work[sym]]; len = lens[work[sym]];
} }
/* create new sub-table if needed */ /* create new sub-table if needed */
if (len > root && (huff & mask) != low) { if (len > root && (huff & mask) != low)
{
/* if first time, transition to sub-tables */ /* if first time, transition to sub-tables */
if (drop == 0) if (drop == 0)
drop = root; drop = root;
@ -285,15 +323,20 @@ unsigned short FAR *work;
/* determine length of next table */ /* determine length of next table */
curr = len - drop; curr = len - drop;
left = (int)(1 << curr); left = (int)(1 << curr);
while (curr + drop < max) {
while (curr + drop < max)
{
left -= count[curr + drop]; left -= count[curr + drop];
if (left <= 0) break; if (left <= 0) break;
curr++; curr++;
left <<= 1; left <<= 1;
} }
/* check for enough space */ /* check for enough space */
used += 1U << curr; used += 1U << curr;
if ((type == LENS && used >= ENOUGH_LENS) || if ((type == LENS && used >= ENOUGH_LENS) ||
(type == DISTS && used >= ENOUGH_DISTS)) (type == DISTS && used >= ENOUGH_DISTS))
return 1; return 1;
@ -309,7 +352,8 @@ unsigned short FAR *work;
/* fill in remaining table entry if code is incomplete (guaranteed to have /* fill in remaining table entry if code is incomplete (guaranteed to have
at most one remaining entry, since if the code is incomplete, the at most one remaining entry, since if the code is incomplete, the
maximum code length that was allowed to get this far is one bit) */ maximum code length that was allowed to get this far is one bit) */
if (huff != 0) { if (huff != 0)
{
here.op = (unsigned char)64; /* invalid code marker */ here.op = (unsigned char)64; /* invalid code marker */
here.bits = (unsigned char)(len - drop); here.bits = (unsigned char)(len - drop);
here.val = (unsigned short)0; here.val = (unsigned short)0;

View File

@ -21,7 +21,8 @@
of the bit buffer. val is the actual byte to output in the case of the bit buffer. val is the actual byte to output in the case
of a literal, the base length or distance, or the offset from of a literal, the base length or distance, or the offset from
the current table to the next table. Each entry is four bytes. */ the current table to the next table. Each entry is four bytes. */
typedef struct { typedef struct
{
unsigned char op; /* operation, extra bits, table bits */ unsigned char op; /* operation, extra bits, table bits */
unsigned char bits; /* bits in this part of the code */ unsigned char bits; /* bits in this part of the code */
unsigned short val; /* offset in table or code value */ unsigned short val; /* offset in table or code value */
@ -51,7 +52,8 @@ typedef struct {
#define ENOUGH (ENOUGH_LENS+ENOUGH_DISTS) #define ENOUGH (ENOUGH_LENS+ENOUGH_DISTS)
/* Type of code to build for inflate_table() */ /* Type of code to build for inflate_table() */
typedef enum { typedef enum
{
CODES, CODES,
LENS, LENS,
DISTS DISTS

View File

@ -100,10 +100,13 @@ void test_compress(compr, comprLen, uncompr, uncomprLen)
err = uncompress(uncompr, &uncomprLen, compr, comprLen); err = uncompress(uncompr, &uncomprLen, compr, comprLen);
CHECK_ERR(err, "uncompress"); CHECK_ERR(err, "uncompress");
if (strcmp((char*)uncompr, hello)) { if (strcmp((char*)uncompr, hello))
{
fprintf(stderr, "bad uncompress\n"); fprintf(stderr, "bad uncompress\n");
exit(1); exit(1);
} else { }
else
{
printf("uncompress(): %s\n", (char*)uncompr); printf("uncompress(): %s\n", (char*)uncompr);
} }
} }
@ -125,66 +128,92 @@ void test_gzio(fname, uncompr, uncomprLen)
z_off_t pos; z_off_t pos;
file = gzopen(fname, "wb"); file = gzopen(fname, "wb");
if (file == NULL) {
if (file == NULL)
{
fprintf(stderr, "gzopen error\n"); fprintf(stderr, "gzopen error\n");
exit(1); exit(1);
} }
gzputc(file, 'h'); gzputc(file, 'h');
if (gzputs(file, "ello") != 4) {
if (gzputs(file, "ello") != 4)
{
fprintf(stderr, "gzputs err: %s\n", gzerror(file, &err)); fprintf(stderr, "gzputs err: %s\n", gzerror(file, &err));
exit(1); exit(1);
} }
if (gzprintf(file, ", %s!", "hello") != 8) {
if (gzprintf(file, ", %s!", "hello") != 8)
{
fprintf(stderr, "gzprintf err: %s\n", gzerror(file, &err)); fprintf(stderr, "gzprintf err: %s\n", gzerror(file, &err));
exit(1); exit(1);
} }
gzseek(file, 1L, SEEK_CUR); /* add one zero byte */ gzseek(file, 1L, SEEK_CUR); /* add one zero byte */
gzclose(file); gzclose(file);
file = gzopen(fname, "rb"); file = gzopen(fname, "rb");
if (file == NULL) {
if (file == NULL)
{
fprintf(stderr, "gzopen error\n"); fprintf(stderr, "gzopen error\n");
exit(1); exit(1);
} }
strcpy((char*)uncompr, "garbage"); strcpy((char*)uncompr, "garbage");
if (gzread(file, uncompr, (unsigned)uncomprLen) != len) { if (gzread(file, uncompr, (unsigned)uncomprLen) != len)
{
fprintf(stderr, "gzread err: %s\n", gzerror(file, &err)); fprintf(stderr, "gzread err: %s\n", gzerror(file, &err));
exit(1); exit(1);
} }
if (strcmp((char*)uncompr, hello)) {
if (strcmp((char*)uncompr, hello))
{
fprintf(stderr, "bad gzread: %s\n", (char*)uncompr); fprintf(stderr, "bad gzread: %s\n", (char*)uncompr);
exit(1); exit(1);
} else { }
else
{
printf("gzread(): %s\n", (char*)uncompr); printf("gzread(): %s\n", (char*)uncompr);
} }
pos = gzseek(file, -8L, SEEK_CUR); pos = gzseek(file, -8L, SEEK_CUR);
if (pos != 6 || gztell(file) != pos) {
if (pos != 6 || gztell(file) != pos)
{
fprintf(stderr, "gzseek error, pos=%ld, gztell=%ld\n", fprintf(stderr, "gzseek error, pos=%ld, gztell=%ld\n",
(long)pos, (long)gztell(file)); (long)pos, (long)gztell(file));
exit(1); exit(1);
} }
if (gzgetc(file) != ' ') { if (gzgetc(file) != ' ')
{
fprintf(stderr, "gzgetc error\n"); fprintf(stderr, "gzgetc error\n");
exit(1); exit(1);
} }
if (gzungetc(' ', file) != ' ') { if (gzungetc(' ', file) != ' ')
{
fprintf(stderr, "gzungetc error\n"); fprintf(stderr, "gzungetc error\n");
exit(1); exit(1);
} }
gzgets(file, (char*)uncompr, (int)uncomprLen); gzgets(file, (char*)uncompr, (int)uncomprLen);
if (strlen((char*)uncompr) != 7) { /* " hello!" */
if (strlen((char*)uncompr) != 7) /* " hello!" */
{
fprintf(stderr, "gzgets err after gzseek: %s\n", gzerror(file, &err)); fprintf(stderr, "gzgets err after gzseek: %s\n", gzerror(file, &err));
exit(1); exit(1);
} }
if (strcmp((char*)uncompr, hello + 6)) {
if (strcmp((char*)uncompr, hello + 6))
{
fprintf(stderr, "bad gzgets after gzseek\n"); fprintf(stderr, "bad gzgets after gzseek\n");
exit(1); exit(1);
} else { }
else
{
printf("gzgets() after gzseek: %s\n", (char*)uncompr); printf("gzgets() after gzseek: %s\n", (char*)uncompr);
} }
@ -215,16 +244,21 @@ void test_deflate(compr, comprLen)
c_stream.next_in = (Bytef*)hello; c_stream.next_in = (Bytef*)hello;
c_stream.next_out = compr; c_stream.next_out = compr;
while (c_stream.total_in != len && c_stream.total_out < comprLen) { while (c_stream.total_in != len && c_stream.total_out < comprLen)
{
c_stream.avail_in = c_stream.avail_out = 1; /* force small buffers */ c_stream.avail_in = c_stream.avail_out = 1; /* force small buffers */
err = deflate(&c_stream, Z_NO_FLUSH); err = deflate(&c_stream, Z_NO_FLUSH);
CHECK_ERR(err, "deflate"); CHECK_ERR(err, "deflate");
} }
/* Finish the stream, still forcing small buffers: */ /* Finish the stream, still forcing small buffers: */
for (;;) { for (;;)
{
c_stream.avail_out = 1; c_stream.avail_out = 1;
err = deflate(&c_stream, Z_FINISH); err = deflate(&c_stream, Z_FINISH);
if (err == Z_STREAM_END) break; if (err == Z_STREAM_END) break;
CHECK_ERR(err, "deflate"); CHECK_ERR(err, "deflate");
} }
@ -255,20 +289,26 @@ void test_inflate(compr, comprLen, uncompr, uncomprLen)
err = inflateInit(&d_stream); err = inflateInit(&d_stream);
CHECK_ERR(err, "inflateInit"); CHECK_ERR(err, "inflateInit");
while (d_stream.total_out < uncomprLen && d_stream.total_in < comprLen) { while (d_stream.total_out < uncomprLen && d_stream.total_in < comprLen)
{
d_stream.avail_in = d_stream.avail_out = 1; /* force small buffers */ d_stream.avail_in = d_stream.avail_out = 1; /* force small buffers */
err = inflate(&d_stream, Z_NO_FLUSH); err = inflate(&d_stream, Z_NO_FLUSH);
if (err == Z_STREAM_END) break; if (err == Z_STREAM_END) break;
CHECK_ERR(err, "inflate"); CHECK_ERR(err, "inflate");
} }
err = inflateEnd(&d_stream); err = inflateEnd(&d_stream);
CHECK_ERR(err, "inflateEnd"); CHECK_ERR(err, "inflateEnd");
if (strcmp((char*)uncompr, hello)) { if (strcmp((char*)uncompr, hello))
{
fprintf(stderr, "bad inflate\n"); fprintf(stderr, "bad inflate\n");
exit(1); exit(1);
} else { }
else
{
printf("inflate(): %s\n", (char*)uncompr); printf("inflate(): %s\n", (char*)uncompr);
} }
} }
@ -300,7 +340,9 @@ void test_large_deflate(compr, comprLen, uncompr, uncomprLen)
c_stream.avail_in = (uInt)uncomprLen; c_stream.avail_in = (uInt)uncomprLen;
err = deflate(&c_stream, Z_NO_FLUSH); err = deflate(&c_stream, Z_NO_FLUSH);
CHECK_ERR(err, "deflate"); CHECK_ERR(err, "deflate");
if (c_stream.avail_in != 0) {
if (c_stream.avail_in != 0)
{
fprintf(stderr, "deflate not greedy\n"); fprintf(stderr, "deflate not greedy\n");
exit(1); exit(1);
} }
@ -320,10 +362,13 @@ void test_large_deflate(compr, comprLen, uncompr, uncomprLen)
CHECK_ERR(err, "deflate"); CHECK_ERR(err, "deflate");
err = deflate(&c_stream, Z_FINISH); err = deflate(&c_stream, Z_FINISH);
if (err != Z_STREAM_END) {
if (err != Z_STREAM_END)
{
fprintf(stderr, "deflate should report Z_STREAM_END\n"); fprintf(stderr, "deflate should report Z_STREAM_END\n");
exit(1); exit(1);
} }
err = deflateEnd(&c_stream); err = deflateEnd(&c_stream);
CHECK_ERR(err, "deflateEnd"); CHECK_ERR(err, "deflateEnd");
} }
@ -350,21 +395,27 @@ void test_large_inflate(compr, comprLen, uncompr, uncomprLen)
err = inflateInit(&d_stream); err = inflateInit(&d_stream);
CHECK_ERR(err, "inflateInit"); CHECK_ERR(err, "inflateInit");
for (;;) { for (;;)
{
d_stream.next_out = uncompr; /* discard the output */ d_stream.next_out = uncompr; /* discard the output */
d_stream.avail_out = (uInt)uncomprLen; d_stream.avail_out = (uInt)uncomprLen;
err = inflate(&d_stream, Z_NO_FLUSH); err = inflate(&d_stream, Z_NO_FLUSH);
if (err == Z_STREAM_END) break; if (err == Z_STREAM_END) break;
CHECK_ERR(err, "large inflate"); CHECK_ERR(err, "large inflate");
} }
err = inflateEnd(&d_stream); err = inflateEnd(&d_stream);
CHECK_ERR(err, "inflateEnd"); CHECK_ERR(err, "inflateEnd");
if (d_stream.total_out != 2*uncomprLen + comprLen/2) { if (d_stream.total_out != 2 * uncomprLen + comprLen / 2)
{
fprintf(stderr, "bad large inflate: %ld\n", d_stream.total_out); fprintf(stderr, "bad large inflate: %ld\n", d_stream.total_out);
exit(1); exit(1);
} else { }
else
{
printf("large_inflate(): OK\n"); printf("large_inflate(): OK\n");
} }
} }
@ -398,9 +449,12 @@ void test_flush(compr, comprLen)
c_stream.avail_in = len - 3; c_stream.avail_in = len - 3;
err = deflate(&c_stream, Z_FINISH); err = deflate(&c_stream, Z_FINISH);
if (err != Z_STREAM_END) {
if (err != Z_STREAM_END)
{
CHECK_ERR(err, "deflate"); CHECK_ERR(err, "deflate");
} }
err = deflateEnd(&c_stream); err = deflateEnd(&c_stream);
CHECK_ERR(err, "deflateEnd"); CHECK_ERR(err, "deflateEnd");
@ -440,11 +494,14 @@ void test_sync(compr, comprLen, uncompr, uncomprLen)
CHECK_ERR(err, "inflateSync"); CHECK_ERR(err, "inflateSync");
err = inflate(&d_stream, Z_FINISH); err = inflate(&d_stream, Z_FINISH);
if (err != Z_DATA_ERROR) {
if (err != Z_DATA_ERROR)
{
fprintf(stderr, "inflate should report DATA_ERROR\n"); fprintf(stderr, "inflate should report DATA_ERROR\n");
/* Because of incorrect adler32 */ /* Because of incorrect adler32 */
exit(1); exit(1);
} }
err = inflateEnd(&d_stream); err = inflateEnd(&d_stream);
CHECK_ERR(err, "inflateEnd"); CHECK_ERR(err, "inflateEnd");
@ -480,10 +537,13 @@ void test_dict_deflate(compr, comprLen)
c_stream.avail_in = (uInt)strlen(hello) + 1; c_stream.avail_in = (uInt)strlen(hello) + 1;
err = deflate(&c_stream, Z_FINISH); err = deflate(&c_stream, Z_FINISH);
if (err != Z_STREAM_END) {
if (err != Z_STREAM_END)
{
fprintf(stderr, "deflate should report Z_STREAM_END\n"); fprintf(stderr, "deflate should report Z_STREAM_END\n");
exit(1); exit(1);
} }
err = deflateEnd(&c_stream); err = deflateEnd(&c_stream);
CHECK_ERR(err, "deflateEnd"); CHECK_ERR(err, "deflateEnd");
} }
@ -513,27 +573,37 @@ void test_dict_inflate(compr, comprLen, uncompr, uncomprLen)
d_stream.next_out = uncompr; d_stream.next_out = uncompr;
d_stream.avail_out = (uInt)uncomprLen; d_stream.avail_out = (uInt)uncomprLen;
for (;;) { for (;;)
{
err = inflate(&d_stream, Z_NO_FLUSH); err = inflate(&d_stream, Z_NO_FLUSH);
if (err == Z_STREAM_END) break; if (err == Z_STREAM_END) break;
if (err == Z_NEED_DICT) {
if (d_stream.adler != dictId) { if (err == Z_NEED_DICT)
{
if (d_stream.adler != dictId)
{
fprintf(stderr, "unexpected dictionary"); fprintf(stderr, "unexpected dictionary");
exit(1); exit(1);
} }
err = inflateSetDictionary(&d_stream, (const Bytef*)dictionary, err = inflateSetDictionary(&d_stream, (const Bytef*)dictionary,
(int)sizeof(dictionary)); (int)sizeof(dictionary));
} }
CHECK_ERR(err, "inflate with dict"); CHECK_ERR(err, "inflate with dict");
} }
err = inflateEnd(&d_stream); err = inflateEnd(&d_stream);
CHECK_ERR(err, "inflateEnd"); CHECK_ERR(err, "inflateEnd");
if (strcmp((char*)uncompr, hello)) { if (strcmp((char*)uncompr, hello))
{
fprintf(stderr, "bad inflate with dict\n"); fprintf(stderr, "bad inflate with dict\n");
exit(1); exit(1);
} else { }
else
{
printf("inflate with dictionary: %s\n", (char*)uncompr); printf("inflate with dictionary: %s\n", (char*)uncompr);
} }
} }
@ -551,11 +621,14 @@ int main(argc, argv)
uLong uncomprLen = comprLen; uLong uncomprLen = comprLen;
static const char* myVersion = ZLIB_VERSION; static const char* myVersion = ZLIB_VERSION;
if (zlibVersion()[0] != myVersion[0]) { if (zlibVersion()[0] != myVersion[0])
{
fprintf(stderr, "incompatible zlib version\n"); fprintf(stderr, "incompatible zlib version\n");
exit(1); exit(1);
} else if (strcmp(zlibVersion(), ZLIB_VERSION) != 0) { }
else if (strcmp(zlibVersion(), ZLIB_VERSION) != 0)
{
fprintf(stderr, "warning: different zlib version\n"); fprintf(stderr, "warning: different zlib version\n");
} }
@ -564,10 +637,12 @@ int main(argc, argv)
compr = (Byte*)calloc((uInt)comprLen, 1); compr = (Byte*)calloc((uInt)comprLen, 1);
uncompr = (Byte*)calloc((uInt)uncomprLen, 1); uncompr = (Byte*)calloc((uInt)uncomprLen, 1);
/* compr and uncompr are cleared to avoid reading uninitialized /* compr and uncompr are cleared to avoid reading uninitialized
* data and to ensure that uncompr compresses well. * data and to ensure that uncompr compresses well.
*/ */
if (compr == Z_NULL || uncompr == Z_NULL) { if (compr == Z_NULL || uncompr == Z_NULL)
{
printf("out of memory\n"); printf("out of memory\n");
exit(1); exit(1);
} }

View File

@ -53,14 +53,16 @@
*/ */
/* these items are strung together in a linked list, one for each allocation */ /* these items are strung together in a linked list, one for each allocation */
struct mem_item { struct mem_item
{
void* ptr; /* pointer to allocated memory */ void* ptr; /* pointer to allocated memory */
size_t size; /* requested size of allocation */ size_t size; /* requested size of allocation */
struct mem_item* next; /* pointer to next item in list, or NULL */ struct mem_item* next; /* pointer to next item in list, or NULL */
}; };
/* this structure is at the root of the linked list, and tracks statistics */ /* this structure is at the root of the linked list, and tracks statistics */
struct mem_zone { struct mem_zone
{
struct mem_item* first; /* pointer to first item in list, or NULL */ struct mem_item* first; /* pointer to first item in list, or NULL */
size_t total, highwater; /* total allocations, and largest total */ size_t total, highwater; /* total allocations, and largest total */
size_t limit; /* memory allocation limit, or 0 if no limit */ size_t limit; /* memory allocation limit, or 0 if no limit */
@ -82,16 +84,21 @@ local void *mem_alloc(void *mem, unsigned count, unsigned size)
/* perform allocation using the standard library, fill memory with a /* perform allocation using the standard library, fill memory with a
non-zero value to make sure that the code isn't depending on zeros */ non-zero value to make sure that the code isn't depending on zeros */
ptr = malloc(len); ptr = malloc(len);
if (ptr == NULL) if (ptr == NULL)
return NULL; return NULL;
memset(ptr, 0xa5, len); memset(ptr, 0xa5, len);
/* create a new item for the list */ /* create a new item for the list */
item = malloc(sizeof(struct mem_item)); item = malloc(sizeof(struct mem_item));
if (item == NULL) {
if (item == NULL)
{
free(ptr); free(ptr);
return NULL; return NULL;
} }
item->ptr = ptr; item->ptr = ptr;
item->size = len; item->size = len;
@ -101,6 +108,7 @@ local void *mem_alloc(void *mem, unsigned count, unsigned size)
/* update the statistics */ /* update the statistics */
zone->total += item->size; zone->total += item->size;
if (zone->total > zone->highwater) if (zone->total > zone->highwater)
zone->highwater = zone->total; zone->highwater = zone->total;
@ -115,7 +123,8 @@ local void mem_free(void *mem, void *ptr)
struct mem_zone* zone = mem; struct mem_zone* zone = mem;
/* if no zone, just do a free */ /* if no zone, just do a free */
if (zone == NULL) { if (zone == NULL)
{
free(ptr); free(ptr);
return; return;
} }
@ -123,15 +132,22 @@ local void mem_free(void *mem, void *ptr)
/* point next to the item that matches ptr, or NULL if not found -- remove /* point next to the item that matches ptr, or NULL if not found -- remove
the item from the linked list if found */ the item from the linked list if found */
next = zone->first; next = zone->first;
if (next) {
if (next)
{
if (next->ptr == ptr) if (next->ptr == ptr)
zone->first = next->next; /* first one is it, remove from list */ zone->first = next->next; /* first one is it, remove from list */
else { else
do { /* search the linked list */ {
do /* search the linked list */
{
item = next; item = next;
next = item->next; next = item->next;
} while (next != NULL && next->ptr != ptr); }
if (next) { /* if found, remove from linked list */ while (next != NULL && next->ptr != ptr);
if (next) /* if found, remove from linked list */
{
item->next = next->next; item->next = next->next;
zone->notlifo++; /* not a LIFO free */ zone->notlifo++; /* not a LIFO free */
} }
@ -140,7 +156,8 @@ local void mem_free(void *mem, void *ptr)
} }
/* if found, update the statistics and free the item */ /* if found, update the statistics and free the item */
if (next) { if (next)
{
zone->total -= next->size; zone->total -= next->size;
free(next); free(next);
} }
@ -208,7 +225,9 @@ local void mem_done(z_stream *strm, char *prefix)
/* free leftover allocations and item structures, if any */ /* free leftover allocations and item structures, if any */
item = zone->first; item = zone->first;
while (item != NULL) {
while (item != NULL)
{
free(item->ptr); free(item->ptr);
next = item->next; next = item->next;
free(item); free(item);
@ -220,8 +239,10 @@ local void mem_done(z_stream *strm, char *prefix)
if (count || zone->total) if (count || zone->total)
fprintf(stderr, "** %s: %lu bytes in %d blocks not freed\n", fprintf(stderr, "** %s: %lu bytes in %d blocks not freed\n",
prefix, zone->total, count); prefix, zone->total, count);
if (zone->notlifo) if (zone->notlifo)
fprintf(stderr, "** %s: %d frees not LIFO\n", prefix, zone->notlifo); fprintf(stderr, "** %s: %d frees not LIFO\n", prefix, zone->notlifo);
if (zone->rogue) if (zone->rogue)
fprintf(stderr, "** %s: %d frees not recognized\n", fprintf(stderr, "** %s: %d frees not recognized\n",
prefix, zone->rogue); prefix, zone->rogue);
@ -248,11 +269,15 @@ local unsigned char *h2b(const char *hex, unsigned *len)
unsigned next, val; unsigned next, val;
in = malloc((strlen(hex) + 1) >> 1); in = malloc((strlen(hex) + 1) >> 1);
if (in == NULL) if (in == NULL)
return NULL; return NULL;
next = 0; next = 0;
val = 1; val = 1;
do {
do
{
if (*hex >= '0' && *hex <= '9') if (*hex >= '0' && *hex <= '9')
val = (val << 4) + *hex - '0'; val = (val << 4) + *hex - '0';
else if (*hex >= 'A' && *hex <= 'F') else if (*hex >= 'A' && *hex <= 'F')
@ -261,13 +286,18 @@ local unsigned char *h2b(const char *hex, unsigned *len)
val = (val << 4) + *hex - 'a' + 10; val = (val << 4) + *hex - 'a' + 10;
else if (val != 1 && val < 32) /* one digit followed by delimiter */ else if (val != 1 && val < 32) /* one digit followed by delimiter */
val += 240; /* make it look like two digits */ val += 240; /* make it look like two digits */
if (val > 255) { /* have two digits */
if (val > 255) /* have two digits */
{
in[next++] = val & 0xff; /* save the decoded byte */ in[next++] = val & 0xff; /* save the decoded byte */
val = 1; /* start over */ val = 1; /* start over */
} }
} while (*hex++); /* go through the loop with the terminating null */ }
while (*hex++); /* go through the loop with the terminating null */
if (len != NULL) if (len != NULL)
*len = next; *len = next;
in = reallocf(in, next); in = reallocf(in, next);
return in; return in;
} }
@ -294,33 +324,50 @@ local void inf(char *hex, char *what, unsigned step, int win, unsigned len,
strm.avail_in = 0; strm.avail_in = 0;
strm.next_in = Z_NULL; strm.next_in = Z_NULL;
ret = inflateInit2(&strm, win); ret = inflateInit2(&strm, win);
if (ret != Z_OK) {
if (ret != Z_OK)
{
mem_done(&strm, what); mem_done(&strm, what);
return; return;
} }
out = malloc(len); assert(out != NULL);
if (win == 47) { out = malloc(len);
assert(out != NULL);
if (win == 47)
{
head.extra = out; head.extra = out;
head.extra_max = len; head.extra_max = len;
head.name = out; head.name = out;
head.name_max = len; head.name_max = len;
head.comment = out; head.comment = out;
head.comm_max = len; head.comm_max = len;
ret = inflateGetHeader(&strm, &head); assert(ret == Z_OK); ret = inflateGetHeader(&strm, &head);
assert(ret == Z_OK);
} }
in = h2b(hex, &have); assert(in != NULL);
in = h2b(hex, &have);
assert(in != NULL);
if (step == 0 || step > have) if (step == 0 || step > have)
step = have; step = have;
strm.avail_in = step; strm.avail_in = step;
have -= step; have -= step;
strm.next_in = in; strm.next_in = in;
do {
do
{
strm.avail_out = len; strm.avail_out = len;
strm.next_out = out; strm.next_out = out;
ret = inflate(&strm, Z_NO_FLUSH); assert(err == 9 || ret == err); ret = inflate(&strm, Z_NO_FLUSH);
assert(err == 9 || ret == err);
if (ret != Z_OK && ret != Z_BUF_ERROR && ret != Z_NEED_DICT) if (ret != Z_OK && ret != Z_BUF_ERROR && ret != Z_NEED_DICT)
break; break;
if (ret == Z_NEED_DICT) {
if (ret == Z_NEED_DICT)
{
ret = inflateSetDictionary(&strm, in, 1); ret = inflateSetDictionary(&strm, in, 1);
assert(ret == Z_DATA_ERROR); assert(ret == Z_DATA_ERROR);
mem_limit(&strm, 1); mem_limit(&strm, 1);
@ -330,19 +377,27 @@ local void inf(char *hex, char *what, unsigned step, int win, unsigned len,
((struct inflate_state*)strm.state)->mode = DICT; ((struct inflate_state*)strm.state)->mode = DICT;
ret = inflateSetDictionary(&strm, out, 0); ret = inflateSetDictionary(&strm, out, 0);
assert(ret == Z_OK); assert(ret == Z_OK);
ret = inflate(&strm, Z_NO_FLUSH); assert(ret == Z_BUF_ERROR); ret = inflate(&strm, Z_NO_FLUSH);
assert(ret == Z_BUF_ERROR);
} }
ret = inflateCopy(&copy, &strm); assert(ret == Z_OK);
ret = inflateEnd(&copy); assert(ret == Z_OK); ret = inflateCopy(&copy, &strm);
assert(ret == Z_OK);
ret = inflateEnd(&copy);
assert(ret == Z_OK);
err = 9; /* don't care next time around */ err = 9; /* don't care next time around */
have += strm.avail_in; have += strm.avail_in;
strm.avail_in = step > have ? have : step; strm.avail_in = step > have ? have : step;
have -= strm.avail_in; have -= strm.avail_in;
} while (strm.avail_in); }
while (strm.avail_in);
free(in); free(in);
free(out); free(out);
ret = inflateReset2(&strm, -8); assert(ret == Z_OK); ret = inflateReset2(&strm, -8);
ret = inflateEnd(&strm); assert(ret == Z_OK); assert(ret == Z_OK);
ret = inflateEnd(&strm);
assert(ret == Z_OK);
mem_done(&strm, what); mem_done(&strm, what);
} }
@ -355,13 +410,17 @@ local void cover_support(void)
mem_setup(&strm); mem_setup(&strm);
strm.avail_in = 0; strm.avail_in = 0;
strm.next_in = Z_NULL; strm.next_in = Z_NULL;
ret = inflateInit(&strm); assert(ret == Z_OK); ret = inflateInit(&strm);
assert(ret == Z_OK);
mem_used(&strm, "inflate init"); mem_used(&strm, "inflate init");
ret = inflatePrime(&strm, 5, 31); assert(ret == Z_OK); ret = inflatePrime(&strm, 5, 31);
ret = inflatePrime(&strm, -1, 0); assert(ret == Z_OK); assert(ret == Z_OK);
ret = inflatePrime(&strm, -1, 0);
assert(ret == Z_OK);
ret = inflateSetDictionary(&strm, Z_NULL, 0); ret = inflateSetDictionary(&strm, Z_NULL, 0);
assert(ret == Z_STREAM_ERROR); assert(ret == Z_STREAM_ERROR);
ret = inflateEnd(&strm); assert(ret == Z_OK); ret = inflateEnd(&strm);
assert(ret == Z_OK);
mem_done(&strm, "prime"); mem_done(&strm, "prime");
inf("63 0", "force window allocation", 0, -15, 1, Z_OK); inf("63 0", "force window allocation", 0, -15, 1, Z_OK);
@ -379,8 +438,10 @@ local void cover_support(void)
strm.avail_in = 0; strm.avail_in = 0;
strm.next_in = Z_NULL; strm.next_in = Z_NULL;
ret = inflateInit(&strm); assert(ret == Z_OK); ret = inflateInit(&strm);
ret = inflateEnd(&strm); assert(ret == Z_OK); assert(ret == Z_OK);
ret = inflateEnd(&strm);
assert(ret == Z_OK);
fputs("inflate built-in memory routines\n", stderr); fputs("inflate built-in memory routines\n", stderr);
} }
@ -391,9 +452,12 @@ local void cover_wrap(void)
z_stream strm, copy; z_stream strm, copy;
unsigned char dict[257]; unsigned char dict[257];
ret = inflate(Z_NULL, 0); assert(ret == Z_STREAM_ERROR); ret = inflate(Z_NULL, 0);
ret = inflateEnd(Z_NULL); assert(ret == Z_STREAM_ERROR); assert(ret == Z_STREAM_ERROR);
ret = inflateCopy(Z_NULL, Z_NULL); assert(ret == Z_STREAM_ERROR); ret = inflateEnd(Z_NULL);
assert(ret == Z_STREAM_ERROR);
ret = inflateCopy(Z_NULL, Z_NULL);
assert(ret == Z_STREAM_ERROR);
fputs("inflate bad parameters\n", stderr); fputs("inflate bad parameters\n", stderr);
inf("1f 8b 0 0", "bad gzip method", 0, 31, 0, Z_DATA_ERROR); inf("1f 8b 0 0", "bad gzip method", 0, 31, 0, Z_DATA_ERROR);
@ -419,27 +483,36 @@ local void cover_wrap(void)
strm.avail_out = 1; strm.avail_out = 1;
strm.next_out = (void*)&ret; strm.next_out = (void*)&ret;
mem_limit(&strm, 1); mem_limit(&strm, 1);
ret = inflate(&strm, Z_NO_FLUSH); assert(ret == Z_MEM_ERROR); ret = inflate(&strm, Z_NO_FLUSH);
ret = inflate(&strm, Z_NO_FLUSH); assert(ret == Z_MEM_ERROR); assert(ret == Z_MEM_ERROR);
ret = inflate(&strm, Z_NO_FLUSH);
assert(ret == Z_MEM_ERROR);
mem_limit(&strm, 0); mem_limit(&strm, 0);
memset(dict, 0, 257); memset(dict, 0, 257);
ret = inflateSetDictionary(&strm, dict, 257); ret = inflateSetDictionary(&strm, dict, 257);
assert(ret == Z_OK); assert(ret == Z_OK);
mem_limit(&strm, (sizeof(struct inflate_state) << 1) + 256); mem_limit(&strm, (sizeof(struct inflate_state) << 1) + 256);
ret = inflatePrime(&strm, 16, 0); assert(ret == Z_OK); ret = inflatePrime(&strm, 16, 0);
assert(ret == Z_OK);
strm.avail_in = 2; strm.avail_in = 2;
strm.next_in = (void*)"\x80"; strm.next_in = (void*)"\x80";
ret = inflateSync(&strm); assert(ret == Z_DATA_ERROR); ret = inflateSync(&strm);
ret = inflate(&strm, Z_NO_FLUSH); assert(ret == Z_STREAM_ERROR); assert(ret == Z_DATA_ERROR);
ret = inflate(&strm, Z_NO_FLUSH);
assert(ret == Z_STREAM_ERROR);
strm.avail_in = 4; strm.avail_in = 4;
strm.next_in = (void*)"\0\0\xff\xff"; strm.next_in = (void*)"\0\0\xff\xff";
ret = inflateSync(&strm); assert(ret == Z_OK); ret = inflateSync(&strm);
assert(ret == Z_OK);
(void)inflateSyncPoint(&strm); (void)inflateSyncPoint(&strm);
ret = inflateCopy(&copy, &strm); assert(ret == Z_MEM_ERROR); ret = inflateCopy(&copy, &strm);
assert(ret == Z_MEM_ERROR);
mem_limit(&strm, 0); mem_limit(&strm, 0);
ret = inflateUndermine(&strm, 1); assert(ret == Z_DATA_ERROR); ret = inflateUndermine(&strm, 1);
assert(ret == Z_DATA_ERROR);
(void)inflateMark(&strm); (void)inflateMark(&strm);
ret = inflateEnd(&strm); assert(ret == Z_OK); ret = inflateEnd(&strm);
assert(ret == Z_OK);
mem_done(&strm, "miscellaneous, force memory errors"); mem_done(&strm, "miscellaneous, force memory errors");
} }
@ -450,13 +523,17 @@ local unsigned pull(void *desc, unsigned char **buf)
static unsigned char dat[] = {0x63, 0, 2, 0}; static unsigned char dat[] = {0x63, 0, 2, 0};
struct inflate_state* state; struct inflate_state* state;
if (desc == Z_NULL) { if (desc == Z_NULL)
{
next = 0; next = 0;
return 0; /* no input (already provided at next_in) */ return 0; /* no input (already provided at next_in) */
} }
state = (void*)((z_stream*)desc)->state; state = (void*)((z_stream*)desc)->state;
if (state != Z_NULL) if (state != Z_NULL)
state->mode = SYNC; /* force an otherwise impossible situation */ state->mode = SYNC; /* force an otherwise impossible situation */
return next < sizeof(dat) ? (*buf = dat + next++, 1) : 0; return next < sizeof(dat) ? (*buf = dat + next++, 1) : 0;
} }
@ -475,14 +552,17 @@ local void cover_back(void)
ret = inflateBackInit_(Z_NULL, 0, win, 0, 0); ret = inflateBackInit_(Z_NULL, 0, win, 0, 0);
assert(ret == Z_VERSION_ERROR); assert(ret == Z_VERSION_ERROR);
ret = inflateBackInit(Z_NULL, 0, win); assert(ret == Z_STREAM_ERROR); ret = inflateBackInit(Z_NULL, 0, win);
assert(ret == Z_STREAM_ERROR);
ret = inflateBack(Z_NULL, Z_NULL, Z_NULL, Z_NULL, Z_NULL); ret = inflateBack(Z_NULL, Z_NULL, Z_NULL, Z_NULL, Z_NULL);
assert(ret == Z_STREAM_ERROR); assert(ret == Z_STREAM_ERROR);
ret = inflateBackEnd(Z_NULL); assert(ret == Z_STREAM_ERROR); ret = inflateBackEnd(Z_NULL);
assert(ret == Z_STREAM_ERROR);
fputs("inflateBack bad parameters\n", stderr); fputs("inflateBack bad parameters\n", stderr);
mem_setup(&strm); mem_setup(&strm);
ret = inflateBackInit(&strm, 15, win); assert(ret == Z_OK); ret = inflateBackInit(&strm, 15, win);
assert(ret == Z_OK);
strm.avail_in = 2; strm.avail_in = 2;
strm.next_in = (void*)"\x03"; strm.next_in = (void*)"\x03";
ret = inflateBack(&strm, pull, Z_NULL, push, Z_NULL); ret = inflateBack(&strm, pull, Z_NULL, push, Z_NULL);
@ -495,15 +575,19 @@ local void cover_back(void)
/* force mode error by mucking with state */ /* force mode error by mucking with state */
ret = inflateBack(&strm, pull, &strm, push, Z_NULL); ret = inflateBack(&strm, pull, &strm, push, Z_NULL);
assert(ret == Z_STREAM_ERROR); assert(ret == Z_STREAM_ERROR);
ret = inflateBackEnd(&strm); assert(ret == Z_OK); ret = inflateBackEnd(&strm);
assert(ret == Z_OK);
mem_done(&strm, "inflateBack bad state"); mem_done(&strm, "inflateBack bad state");
ret = inflateBackInit(&strm, 15, win); assert(ret == Z_OK); ret = inflateBackInit(&strm, 15, win);
ret = inflateBackEnd(&strm); assert(ret == Z_OK); assert(ret == Z_OK);
ret = inflateBackEnd(&strm);
assert(ret == Z_OK);
fputs("inflateBack built-in memory routines\n", stderr); fputs("inflateBack built-in memory routines\n", stderr);
} }
/* do a raw inflate of data in hexadecimal with both inflate and inflateBack */ /* do a raw inflate of data in hexadecimal with both inflate and inflateBack */
local int try(char* hex, char* id, int err) local int try(char* hex, char* id, int err)
{ {
int ret; int ret;
@ -535,23 +619,31 @@ local int try(char *hex, char *id, int err)
assert(ret == Z_OK); assert(ret == Z_OK);
strm.avail_in = len; strm.avail_in = len;
strm.next_in = in; strm.next_in = in;
do {
do
{
strm.avail_out = size; strm.avail_out = size;
strm.next_out = out; strm.next_out = out;
ret = inflate(&strm, Z_TREES); ret = inflate(&strm, Z_TREES);
assert(ret != Z_STREAM_ERROR && ret != Z_MEM_ERROR); assert(ret != Z_STREAM_ERROR && ret != Z_MEM_ERROR);
if (ret == Z_DATA_ERROR || ret == Z_NEED_DICT) if (ret == Z_DATA_ERROR || ret == Z_NEED_DICT)
break; break;
} while (strm.avail_in || strm.avail_out == 0); }
if (err) { while (strm.avail_in || strm.avail_out == 0);
if (err)
{
assert(ret == Z_DATA_ERROR); assert(ret == Z_DATA_ERROR);
assert(strcmp(id, strm.msg) == 0); assert(strcmp(id, strm.msg) == 0);
} }
inflateEnd(&strm); inflateEnd(&strm);
mem_done(&strm, prefix); mem_done(&strm, prefix);
/* then with inflateBack */ /* then with inflateBack */
if (err >= 0) { if (err >= 0)
{
strcpy(prefix, id); strcpy(prefix, id);
strcat(prefix, "-back"); strcat(prefix, "-back");
mem_setup(&strm); mem_setup(&strm);
@ -561,10 +653,13 @@ local int try(char *hex, char *id, int err)
strm.next_in = in; strm.next_in = in;
ret = inflateBack(&strm, pull, Z_NULL, push, Z_NULL); ret = inflateBack(&strm, pull, Z_NULL, push, Z_NULL);
assert(ret != Z_STREAM_ERROR); assert(ret != Z_STREAM_ERROR);
if (err) {
if (err)
{
assert(ret == Z_DATA_ERROR); assert(ret == Z_DATA_ERROR);
assert(strcmp(id, strm.msg) == 0); assert(strcmp(id, strm.msg) == 0);
} }
inflateBackEnd(&strm); inflateBackEnd(&strm);
mem_done(&strm, prefix); mem_done(&strm, prefix);
} }
@ -581,33 +676,53 @@ local int try(char *hex, char *id, int err)
local void cover_inflate(void) local void cover_inflate(void)
{ {
try("0 0 0 0 0", "invalid stored block lengths", 1); try("0 0 0 0 0", "invalid stored block lengths", 1);
try("3 0", "fixed", 0); try("3 0", "fixed", 0);
try("6", "invalid block type", 1); try("6", "invalid block type", 1);
try("1 1 0 fe ff 0", "stored", 0); try("1 1 0 fe ff 0", "stored", 0);
try("fc 0 0", "too many length or distance symbols", 1); try("fc 0 0", "too many length or distance symbols", 1);
try("4 0 fe ff", "invalid code lengths set", 1); try("4 0 fe ff", "invalid code lengths set", 1);
try("4 0 24 49 0", "invalid bit length repeat", 1); try("4 0 24 49 0", "invalid bit length repeat", 1);
try("4 0 24 e9 ff ff", "invalid bit length repeat", 1); try("4 0 24 e9 ff ff", "invalid bit length repeat", 1);
try("4 0 24 e9 ff 6d", "invalid code -- missing end-of-block", 1); try("4 0 24 e9 ff 6d", "invalid code -- missing end-of-block", 1);
try("4 80 49 92 24 49 92 24 71 ff ff 93 11 0", try("4 80 49 92 24 49 92 24 71 ff ff 93 11 0",
"invalid literal/lengths set", 1); "invalid literal/lengths set", 1);
try("4 80 49 92 24 49 92 24 f b4 ff ff c3 84", "invalid distances set", 1); try("4 80 49 92 24 49 92 24 f b4 ff ff c3 84", "invalid distances set", 1);
try("4 c0 81 8 0 0 0 0 20 7f eb b 0 0", "invalid literal/length code", 1); try("4 c0 81 8 0 0 0 0 20 7f eb b 0 0", "invalid literal/length code", 1);
try("2 7e ff ff", "invalid distance code", 1); try("2 7e ff ff", "invalid distance code", 1);
try("c c0 81 0 0 0 0 0 90 ff 6b 4 0", "invalid distance too far back", 1); try("c c0 81 0 0 0 0 0 90 ff 6b 4 0", "invalid distance too far back", 1);
/* also trailer mismatch just in inflate() */ /* also trailer mismatch just in inflate() */
try("1f 8b 8 0 0 0 0 0 0 0 3 0 0 0 0 1", "incorrect data check", -1); try("1f 8b 8 0 0 0 0 0 0 0 3 0 0 0 0 1", "incorrect data check", -1);
try("1f 8b 8 0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 0 1", try("1f 8b 8 0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 0 1",
"incorrect length check", -1); "incorrect length check", -1);
try("5 c0 21 d 0 0 0 80 b0 fe 6d 2f 91 6c", "pull 17", 0); try("5 c0 21 d 0 0 0 80 b0 fe 6d 2f 91 6c", "pull 17", 0);
try("5 e0 81 91 24 cb b2 2c 49 e2 f 2e 8b 9a 47 56 9f fb fe ec d2 ff 1f", try("5 e0 81 91 24 cb b2 2c 49 e2 f 2e 8b 9a 47 56 9f fb fe ec d2 ff 1f",
"long code", 0); "long code", 0);
try("ed c0 1 1 0 0 0 40 20 ff 57 1b 42 2c 4f", "length extra", 0); try("ed c0 1 1 0 0 0 40 20 ff 57 1b 42 2c 4f", "length extra", 0);
try("ed cf c1 b1 2c 47 10 c4 30 fa 6f 35 1d 1 82 59 3d fb be 2e 2a fc f c", try("ed cf c1 b1 2c 47 10 c4 30 fa 6f 35 1d 1 82 59 3d fb be 2e 2a fc f c",
"long distance and extra", 0); "long distance and extra", 0);
try("ed c0 81 0 0 0 0 80 a0 fd a9 17 a9 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 " try("ed c0 81 0 0 0 0 80 a0 fd a9 17 a9 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 6", "window end", 0); "0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 6", "window end", 0);
inf("2 8 20 80 0 3 0", "inflate_fast TYPE return", 0, -15, 258, inf("2 8 20 80 0 3 0", "inflate_fast TYPE return", 0, -15, 258,
Z_STREAM_END); Z_STREAM_END);
inf("63 18 5 40 c 0", "window wrap", 3, -8, 300, Z_OK); inf("63 18 5 40 c 0", "window wrap", 3, -8, 300, Z_OK);
@ -625,6 +740,7 @@ local void cover_trees(void)
enough errors, since zlib insures that enough is always enough */ enough errors, since zlib insures that enough is always enough */
for (bits = 0; bits < 15; bits++) for (bits = 0; bits < 15; bits++)
lens[bits] = (unsigned short)(bits + 1); lens[bits] = (unsigned short)(bits + 1);
lens[15] = 15; lens[15] = 15;
next = table; next = table;
bits = 15; bits = 15;

View File

@ -88,15 +88,19 @@ static char *strwinerror (error)
(LPVOID)&msgbuf, (LPVOID)&msgbuf,
0, 0,
NULL); NULL);
if (chars != 0) {
if (chars != 0)
{
/* If there is an \r\n appended, zap it. */ /* If there is an \r\n appended, zap it. */
if (chars >= 2 if (chars >= 2
&& msgbuf[chars - 2] == '\r' && msgbuf[chars - 1] == '\n') { && msgbuf[chars - 2] == '\r' && msgbuf[chars - 1] == '\n')
{
chars -= 2; chars -= 2;
msgbuf[chars] = 0; msgbuf[chars] = 0;
} }
if (chars > sizeof (buf) - 1) { if (chars > sizeof(buf) - 1)
{
chars = sizeof(buf) - 1; chars = sizeof(buf) - 1;
msgbuf[chars] = 0; msgbuf[chars] = 0;
} }
@ -104,7 +108,8 @@ static char *strwinerror (error)
wcstombs(buf, msgbuf, chars + 1); wcstombs(buf, msgbuf, chars + 1);
LocalFree(msgbuf); LocalFree(msgbuf);
} }
else { else
{
sprintf(buf, "unknown win32 error (%ld)", error); sprintf(buf, "unknown win32 error (%ld)", error);
} }
@ -163,7 +168,8 @@ void myfree(q, p)
free(p); free(p);
} }
typedef struct gzFile_s { typedef struct gzFile_s
{
FILE* file; FILE* file;
int write; int write;
int err; int err;
@ -198,30 +204,40 @@ gzFile gz_open(path, fd, mode)
int ret; int ret;
gz = malloc(sizeof(struct gzFile_s)); gz = malloc(sizeof(struct gzFile_s));
if (gz == NULL) if (gz == NULL)
return NULL; return NULL;
gz->write = strchr(mode, 'w') != NULL; gz->write = strchr(mode, 'w') != NULL;
gz->strm.zalloc = myalloc; gz->strm.zalloc = myalloc;
gz->strm.zfree = myfree; gz->strm.zfree = myfree;
gz->strm.opaque = Z_NULL; gz->strm.opaque = Z_NULL;
if (gz->write) if (gz->write)
ret = deflateInit2(&(gz->strm), -1, 8, 15 + 16, 8, 0); ret = deflateInit2(&(gz->strm), -1, 8, 15 + 16, 8, 0);
else { else
{
gz->strm.next_in = 0; gz->strm.next_in = 0;
gz->strm.avail_in = Z_NULL; gz->strm.avail_in = Z_NULL;
ret = inflateInit2(&(gz->strm), 15 + 16); ret = inflateInit2(&(gz->strm), 15 + 16);
} }
if (ret != Z_OK) {
if (ret != Z_OK)
{
free(gz); free(gz);
return NULL; return NULL;
} }
gz->file = path == NULL ? fdopen(fd, gz->write ? "wb" : "rb") : gz->file = path == NULL ? fdopen(fd, gz->write ? "wb" : "rb") :
fopen(path, gz->write ? "wb" : "rb"); fopen(path, gz->write ? "wb" : "rb");
if (gz->file == NULL) {
if (gz->file == NULL)
{
gz->write ? deflateEnd(&(gz->strm)) : inflateEnd(&(gz->strm)); gz->write ? deflateEnd(&(gz->strm)) : inflateEnd(&(gz->strm));
free(gz); free(gz);
return NULL; return NULL;
} }
gz->err = 0; gz->err = 0;
gz->msg = ""; gz->msg = "";
return gz; return gz;
@ -239,15 +255,20 @@ int gzwrite(gz, buf, len)
if (gz == NULL || !gz->write) if (gz == NULL || !gz->write)
return 0; return 0;
strm = &(gz->strm); strm = &(gz->strm);
strm->next_in = (void*)buf; strm->next_in = (void*)buf;
strm->avail_in = len; strm->avail_in = len;
do {
do
{
strm->next_out = out; strm->next_out = out;
strm->avail_out = BUFLEN; strm->avail_out = BUFLEN;
(void)deflate(strm, Z_NO_FLUSH); (void)deflate(strm, Z_NO_FLUSH);
fwrite(out, 1, BUFLEN - strm->avail_out, gz->file); fwrite(out, 1, BUFLEN - strm->avail_out, gz->file);
} while (strm->avail_out == 0); }
while (strm->avail_out == 0);
return len; return len;
} }
@ -265,26 +286,37 @@ int gzread(gz, buf, len)
if (gz == NULL || gz->write) if (gz == NULL || gz->write)
return 0; return 0;
if (gz->err) if (gz->err)
return 0; return 0;
strm = &(gz->strm); strm = &(gz->strm);
strm->next_out = (void*)buf; strm->next_out = (void*)buf;
strm->avail_out = len; strm->avail_out = len;
do {
do
{
got = fread(in, 1, 1, gz->file); got = fread(in, 1, 1, gz->file);
if (got == 0) if (got == 0)
break; break;
strm->next_in = in; strm->next_in = in;
strm->avail_in = 1; strm->avail_in = 1;
ret = inflate(strm, Z_NO_FLUSH); ret = inflate(strm, Z_NO_FLUSH);
if (ret == Z_DATA_ERROR) {
if (ret == Z_DATA_ERROR)
{
gz->err = Z_DATA_ERROR; gz->err = Z_DATA_ERROR;
gz->msg = strm->msg; gz->msg = strm->msg;
return 0; return 0;
} }
if (ret == Z_STREAM_END) if (ret == Z_STREAM_END)
inflateReset(strm); inflateReset(strm);
} while (strm->avail_out); }
while (strm->avail_out);
return len - strm->avail_out; return len - strm->avail_out;
} }
@ -298,20 +330,28 @@ int gzclose(gz)
if (gz == NULL) if (gz == NULL)
return Z_STREAM_ERROR; return Z_STREAM_ERROR;
strm = &(gz->strm); strm = &(gz->strm);
if (gz->write) {
if (gz->write)
{
strm->next_in = Z_NULL; strm->next_in = Z_NULL;
strm->avail_in = 0; strm->avail_in = 0;
do {
do
{
strm->next_out = out; strm->next_out = out;
strm->avail_out = BUFLEN; strm->avail_out = BUFLEN;
(void)deflate(strm, Z_FINISH); (void)deflate(strm, Z_FINISH);
fwrite(out, 1, BUFLEN - strm->avail_out, gz->file); fwrite(out, 1, BUFLEN - strm->avail_out, gz->file);
} while (strm->avail_out == 0); }
while (strm->avail_out == 0);
deflateEnd(strm); deflateEnd(strm);
} }
else else
inflateEnd(strm); inflateEnd(strm);
fclose(gz->file); fclose(gz->file);
free(gz); free(gz);
return Z_OK; return Z_OK;
@ -364,22 +404,31 @@ void gz_compress(in, out)
int err; int err;
#ifdef USE_MMAP #ifdef USE_MMAP
/* Try first compressing with mmap. If mmap fails (minigzip used in a /* Try first compressing with mmap. If mmap fails (minigzip used in a
* pipe), use the normal fread loop. * pipe), use the normal fread loop.
*/ */
if (gz_compress_mmap(in, out) == Z_OK) return; if (gz_compress_mmap(in, out) == Z_OK) return;
#endif #endif
for (;;) {
for (;;)
{
len = (int)fread(buf, 1, sizeof(buf), in); len = (int)fread(buf, 1, sizeof(buf), in);
if (ferror(in)) {
if (ferror(in))
{
perror("fread"); perror("fread");
exit(1); exit(1);
} }
if (len == 0) break; if (len == 0) break;
if (gzwrite(out, buf, (unsigned)len) != len) error(gzerror(out, &err)); if (gzwrite(out, buf, (unsigned)len) != len) error(gzerror(out, &err));
} }
fclose(in); fclose(in);
if (gzclose(out) != Z_OK) error("failed gzclose"); if (gzclose(out) != Z_OK) error("failed gzclose");
} }
@ -401,11 +450,14 @@ int gz_compress_mmap(in, out)
/* Determine the size of the file, needed for mmap: */ /* Determine the size of the file, needed for mmap: */
if (fstat(ifd, &sb) < 0) return Z_ERRNO; if (fstat(ifd, &sb) < 0) return Z_ERRNO;
buf_len = sb.st_size; buf_len = sb.st_size;
if (buf_len <= 0) return Z_ERRNO; if (buf_len <= 0) return Z_ERRNO;
/* Now do the actual mmap: */ /* Now do the actual mmap: */
buf = mmap((caddr_t) 0, buf_len, PROT_READ, MAP_SHARED, ifd, (off_t)0); buf = mmap((caddr_t) 0, buf_len, PROT_READ, MAP_SHARED, ifd, (off_t)0);
if (buf == (caddr_t)(-1)) return Z_ERRNO; if (buf == (caddr_t)(-1)) return Z_ERRNO;
/* Compress the whole file at once: */ /* Compress the whole file at once: */
@ -415,7 +467,9 @@ int gz_compress_mmap(in, out)
munmap(buf, buf_len); munmap(buf, buf_len);
fclose(in); fclose(in);
if (gzclose(out) != Z_OK) error("failed gzclose"); if (gzclose(out) != Z_OK) error("failed gzclose");
return Z_OK; return Z_OK;
} }
#endif /* USE_MMAP */ #endif /* USE_MMAP */
@ -431,15 +485,20 @@ void gz_uncompress(in, out)
int len; int len;
int err; int err;
for (;;) { for (;;)
{
len = gzread(in, buf, sizeof(buf)); len = gzread(in, buf, sizeof(buf));
if (len < 0) error(gzerror(in, &err)); if (len < 0) error(gzerror(in, &err));
if (len == 0) break; if (len == 0) break;
if ((int)fwrite(buf, 1, (unsigned)len, out) != len) { if ((int)fwrite(buf, 1, (unsigned)len, out) != len)
{
error("failed fwrite"); error("failed fwrite");
} }
} }
if (fclose(out)) error("failed fclose"); if (fclose(out)) error("failed fclose");
if (gzclose(in) != Z_OK) error("failed gzclose"); if (gzclose(in) != Z_OK) error("failed gzclose");
@ -458,7 +517,8 @@ void file_compress(file, mode)
FILE* in; FILE* in;
gzFile out; gzFile out;
if (strlen(file) + strlen(GZ_SUFFIX) >= sizeof(outfile)) { if (strlen(file) + strlen(GZ_SUFFIX) >= sizeof(outfile))
{
fprintf(stderr, "%s: filename too long\n", prog); fprintf(stderr, "%s: filename too long\n", prog);
exit(1); exit(1);
} }
@ -467,15 +527,21 @@ void file_compress(file, mode)
strcat(outfile, GZ_SUFFIX); strcat(outfile, GZ_SUFFIX);
in = fopen(file, "rb"); in = fopen(file, "rb");
if (in == NULL) {
if (in == NULL)
{
perror(file); perror(file);
exit(1); exit(1);
} }
out = gzopen(outfile, mode); out = gzopen(outfile, mode);
if (out == NULL) {
if (out == NULL)
{
fprintf(stderr, "%s: can't gzopen %s\n", prog, outfile); fprintf(stderr, "%s: can't gzopen %s\n", prog, outfile);
exit(1); exit(1);
} }
gz_compress(in, out); gz_compress(in, out);
unlink(file); unlink(file);
@ -494,29 +560,39 @@ void file_uncompress(file)
gzFile in; gzFile in;
size_t len = strlen(file); size_t len = strlen(file);
if (len + strlen(GZ_SUFFIX) >= sizeof(buf)) { if (len + strlen(GZ_SUFFIX) >= sizeof(buf))
{
fprintf(stderr, "%s: filename too long\n", prog); fprintf(stderr, "%s: filename too long\n", prog);
exit(1); exit(1);
} }
strcpy(buf, file); strcpy(buf, file);
if (len > SUFFIX_LEN && strcmp(file+len-SUFFIX_LEN, GZ_SUFFIX) == 0) { if (len > SUFFIX_LEN && strcmp(file + len - SUFFIX_LEN, GZ_SUFFIX) == 0)
{
infile = file; infile = file;
outfile = buf; outfile = buf;
outfile[len - 3] = '\0'; outfile[len - 3] = '\0';
} else { }
else
{
outfile = file; outfile = file;
infile = buf; infile = buf;
strcat(infile, GZ_SUFFIX); strcat(infile, GZ_SUFFIX);
} }
in = gzopen(infile, "rb"); in = gzopen(infile, "rb");
if (in == NULL) {
if (in == NULL)
{
fprintf(stderr, "%s: can't gzopen %s\n", prog, infile); fprintf(stderr, "%s: can't gzopen %s\n", prog, infile);
exit(1); exit(1);
} }
out = fopen(outfile, "wb"); out = fopen(outfile, "wb");
if (out == NULL) {
if (out == NULL)
{
perror(file); perror(file);
exit(1); exit(1);
} }
@ -550,10 +626,12 @@ int main(argc, argv)
prog = argv[0]; prog = argv[0];
bname = strrchr(argv[0], '/'); bname = strrchr(argv[0], '/');
if (bname) if (bname)
bname++; bname++;
else else
bname = argv[0]; bname = argv[0];
argc--, argv++; argc--, argv++;
if (!strcmp(bname, "gunzip")) if (!strcmp(bname, "gunzip"))
@ -561,7 +639,8 @@ int main(argc, argv)
else if (!strcmp(bname, "zcat")) else if (!strcmp(bname, "zcat"))
copyout = uncompr = 1; copyout = uncompr = 1;
while (argc > 0) { while (argc > 0)
{
if (strcmp(*argv, "-c") == 0) if (strcmp(*argv, "-c") == 0)
copyout = 1; copyout = 1;
else if (strcmp(*argv, "-d") == 0) else if (strcmp(*argv, "-d") == 0)
@ -577,55 +656,88 @@ int main(argc, argv)
outmode[2] = (*argv)[1]; outmode[2] = (*argv)[1];
else else
break; break;
argc--, argv++; argc--, argv++;
} }
if (outmode[3] == ' ') if (outmode[3] == ' ')
outmode[3] = 0; outmode[3] = 0;
if (argc == 0) {
if (argc == 0)
{
SET_BINARY_MODE(stdin); SET_BINARY_MODE(stdin);
SET_BINARY_MODE(stdout); SET_BINARY_MODE(stdout);
if (uncompr) {
if (uncompr)
{
file = gzdopen(fileno(stdin), "rb"); file = gzdopen(fileno(stdin), "rb");
if (file == NULL) error("can't gzdopen stdin"); if (file == NULL) error("can't gzdopen stdin");
gz_uncompress(file, stdout); gz_uncompress(file, stdout);
} else { }
else
{
file = gzdopen(fileno(stdout), outmode); file = gzdopen(fileno(stdout), outmode);
if (file == NULL) error("can't gzdopen stdout"); if (file == NULL) error("can't gzdopen stdout");
gz_compress(stdin, file); gz_compress(stdin, file);
} }
} else { }
if (copyout) { else
{
if (copyout)
{
SET_BINARY_MODE(stdout); SET_BINARY_MODE(stdout);
} }
do {
if (uncompr) { do
if (copyout) { {
if (uncompr)
{
if (copyout)
{
file = gzopen(*argv, "rb"); file = gzopen(*argv, "rb");
if (file == NULL) if (file == NULL)
fprintf(stderr, "%s: can't gzopen %s\n", prog, *argv); fprintf(stderr, "%s: can't gzopen %s\n", prog, *argv);
else else
gz_uncompress(file, stdout); gz_uncompress(file, stdout);
} else { }
else
{
file_uncompress(*argv); file_uncompress(*argv);
} }
} else { }
if (copyout) { else
{
if (copyout)
{
FILE* in = fopen(*argv, "rb"); FILE* in = fopen(*argv, "rb");
if (in == NULL) { if (in == NULL)
{
perror(*argv); perror(*argv);
} else { }
else
{
file = gzdopen(fileno(stdout), outmode); file = gzdopen(fileno(stdout), outmode);
if (file == NULL) error("can't gzdopen stdout"); if (file == NULL) error("can't gzdopen stdout");
gz_compress(in, file); gz_compress(in, file);
} }
} else { }
else
{
file_compress(*argv, outmode); file_compress(*argv, outmode);
} }
} }
} while (argv++, --argc);
} }
while (argv++, --argc);
}
return 0; return 0;
} }

379
extern/zlib/trees.c vendored
View File

@ -114,7 +114,8 @@ local int base_dist[D_CODES];
# include "trees.h" # include "trees.h"
#endif /* GEN_TREES_H */ #endif /* GEN_TREES_H */
struct static_tree_desc_s { struct static_tree_desc_s
{
const ct_data* static_tree; /* static tree or NULL */ const ct_data* static_tree; /* static tree or NULL */
const intf* extra_bits; /* extra bits for each code or NULL */ const intf* extra_bits; /* extra bits for each code or NULL */
int extra_base; /* base index for extra_bits */ int extra_base; /* base index for extra_bits */
@ -198,12 +199,15 @@ local void send_bits(s, value, length)
* (16 - bi_valid) bits from value, leaving (width - (16-bi_valid)) * (16 - bi_valid) bits from value, leaving (width - (16-bi_valid))
* unused bits in value. * unused bits in value.
*/ */
if (s->bi_valid > (int)Buf_size - length) { if (s->bi_valid > (int)Buf_size - length)
{
s->bi_buf |= (ush)value << s->bi_valid; s->bi_buf |= (ush)value << s->bi_valid;
put_short(s, s->bi_buf); put_short(s, s->bi_buf);
s->bi_buf = (ush)value >> (Buf_size - s->bi_valid); s->bi_buf = (ush)value >> (Buf_size - s->bi_valid);
s->bi_valid += length - Buf_size; s->bi_valid += length - Buf_size;
} else { }
else
{
s->bi_buf |= (ush)value << s->bi_valid; s->bi_buf |= (ush)value << s->bi_valid;
s->bi_valid += length; s->bi_valid += length;
} }
@ -256,12 +260,17 @@ local void tr_static_init()
/* Initialize the mapping length (0..255) -> length code (0..28) */ /* Initialize the mapping length (0..255) -> length code (0..28) */
length = 0; length = 0;
for (code = 0; code < LENGTH_CODES-1; code++) {
for (code = 0; code < LENGTH_CODES - 1; code++)
{
base_length[code] = length; base_length[code] = length;
for (n = 0; n < (1<<extra_lbits[code]); n++) {
for (n = 0; n < (1 << extra_lbits[code]); n++)
{
_length_code[length++] = (uch)code; _length_code[length++] = (uch)code;
} }
} }
Assert(length == 256, "tr_static_init: length != 256"); Assert(length == 256, "tr_static_init: length != 256");
/* Note that the length 255 (match length 258) can be represented /* Note that the length 255 (match length 258) can be represented
* in two different ways: code 284 + 5 bits or code 285, so we * in two different ways: code 284 + 5 bits or code 285, so we
@ -271,29 +280,45 @@ local void tr_static_init()
/* Initialize the mapping dist (0..32K) -> dist code (0..29) */ /* Initialize the mapping dist (0..32K) -> dist code (0..29) */
dist = 0; dist = 0;
for (code = 0 ; code < 16; code++) {
for (code = 0 ; code < 16; code++)
{
base_dist[code] = dist; base_dist[code] = dist;
for (n = 0; n < (1<<extra_dbits[code]); n++) {
for (n = 0; n < (1 << extra_dbits[code]); n++)
{
_dist_code[dist++] = (uch)code; _dist_code[dist++] = (uch)code;
} }
} }
Assert(dist == 256, "tr_static_init: dist != 256"); Assert(dist == 256, "tr_static_init: dist != 256");
dist >>= 7; /* from now on, all distances are divided by 128 */ dist >>= 7; /* from now on, all distances are divided by 128 */
for ( ; code < D_CODES; code++) {
for (; code < D_CODES; code++)
{
base_dist[code] = dist << 7; base_dist[code] = dist << 7;
for (n = 0; n < (1<<(extra_dbits[code]-7)); n++) {
for (n = 0; n < (1 << (extra_dbits[code] - 7)); n++)
{
_dist_code[256 + dist++] = (uch)code; _dist_code[256 + dist++] = (uch)code;
} }
} }
Assert(dist == 256, "tr_static_init: 256+dist != 512"); Assert(dist == 256, "tr_static_init: 256+dist != 512");
/* Construct the codes of the static literal tree */ /* Construct the codes of the static literal tree */
for (bits = 0; bits <= MAX_BITS; bits++) bl_count[bits] = 0; for (bits = 0; bits <= MAX_BITS; bits++) bl_count[bits] = 0;
n = 0; n = 0;
while (n <= 143) static_ltree[n++].Len = 8, bl_count[8]++; while (n <= 143) static_ltree[n++].Len = 8, bl_count[8]++;
while (n <= 255) static_ltree[n++].Len = 9, bl_count[9]++; while (n <= 255) static_ltree[n++].Len = 9, bl_count[9]++;
while (n <= 279) static_ltree[n++].Len = 7, bl_count[7]++; while (n <= 279) static_ltree[n++].Len = 7, bl_count[7]++;
while (n <= 287) static_ltree[n++].Len = 8, bl_count[8]++; while (n <= 287) static_ltree[n++].Len = 8, bl_count[8]++;
/* Codes 286 and 287 do not exist, but we must include them in the /* Codes 286 and 287 do not exist, but we must include them in the
* tree construction to get a canonical Huffman tree (longest code * tree construction to get a canonical Huffman tree (longest code
* all ones) * all ones)
@ -301,10 +326,12 @@ local void tr_static_init()
gen_codes((ct_data*)static_ltree, L_CODES + 1, bl_count); gen_codes((ct_data*)static_ltree, L_CODES + 1, bl_count);
/* The static distance tree is trivial: */ /* The static distance tree is trivial: */
for (n = 0; n < D_CODES; n++) { for (n = 0; n < D_CODES; n++)
{
static_dtree[n].Len = 5; static_dtree[n].Len = 5;
static_dtree[n].Code = bi_reverse((unsigned)n, 5); static_dtree[n].Code = bi_reverse((unsigned)n, 5);
} }
static_init_done = 1; static_init_done = 1;
# ifdef GEN_TREES_H # ifdef GEN_TREES_H
@ -335,38 +362,50 @@ void gen_trees_header()
"/* header created automatically with -DGEN_TREES_H */\n\n"); "/* header created automatically with -DGEN_TREES_H */\n\n");
fprintf(header, "local const ct_data static_ltree[L_CODES+2] = {\n"); fprintf(header, "local const ct_data static_ltree[L_CODES+2] = {\n");
for (i = 0; i < L_CODES+2; i++) {
for (i = 0; i < L_CODES + 2; i++)
{
fprintf(header, "{{%3u},{%3u}}%s", static_ltree[i].Code, fprintf(header, "{{%3u},{%3u}}%s", static_ltree[i].Code,
static_ltree[i].Len, SEPARATOR(i, L_CODES + 1, 5)); static_ltree[i].Len, SEPARATOR(i, L_CODES + 1, 5));
} }
fprintf(header, "local const ct_data static_dtree[D_CODES] = {\n"); fprintf(header, "local const ct_data static_dtree[D_CODES] = {\n");
for (i = 0; i < D_CODES; i++) {
for (i = 0; i < D_CODES; i++)
{
fprintf(header, "{{%2u},{%2u}}%s", static_dtree[i].Code, fprintf(header, "{{%2u},{%2u}}%s", static_dtree[i].Code,
static_dtree[i].Len, SEPARATOR(i, D_CODES - 1, 5)); static_dtree[i].Len, SEPARATOR(i, D_CODES - 1, 5));
} }
fprintf(header, "const uch ZLIB_INTERNAL _dist_code[DIST_CODE_LEN] = {\n"); fprintf(header, "const uch ZLIB_INTERNAL _dist_code[DIST_CODE_LEN] = {\n");
for (i = 0; i < DIST_CODE_LEN; i++) {
for (i = 0; i < DIST_CODE_LEN; i++)
{
fprintf(header, "%2u%s", _dist_code[i], fprintf(header, "%2u%s", _dist_code[i],
SEPARATOR(i, DIST_CODE_LEN - 1, 20)); SEPARATOR(i, DIST_CODE_LEN - 1, 20));
} }
fprintf(header, fprintf(header,
"const uch ZLIB_INTERNAL _length_code[MAX_MATCH-MIN_MATCH+1]= {\n"); "const uch ZLIB_INTERNAL _length_code[MAX_MATCH-MIN_MATCH+1]= {\n");
for (i = 0; i < MAX_MATCH-MIN_MATCH+1; i++) {
for (i = 0; i < MAX_MATCH - MIN_MATCH + 1; i++)
{
fprintf(header, "%2u%s", _length_code[i], fprintf(header, "%2u%s", _length_code[i],
SEPARATOR(i, MAX_MATCH - MIN_MATCH, 20)); SEPARATOR(i, MAX_MATCH - MIN_MATCH, 20));
} }
fprintf(header, "local const int base_length[LENGTH_CODES] = {\n"); fprintf(header, "local const int base_length[LENGTH_CODES] = {\n");
for (i = 0; i < LENGTH_CODES; i++) {
for (i = 0; i < LENGTH_CODES; i++)
{
fprintf(header, "%1u%s", base_length[i], fprintf(header, "%1u%s", base_length[i],
SEPARATOR(i, LENGTH_CODES - 1, 20)); SEPARATOR(i, LENGTH_CODES - 1, 20));
} }
fprintf(header, "local const int base_dist[D_CODES] = {\n"); fprintf(header, "local const int base_dist[D_CODES] = {\n");
for (i = 0; i < D_CODES; i++) {
for (i = 0; i < D_CODES; i++)
{
fprintf(header, "%5u%s", base_dist[i], fprintf(header, "%5u%s", base_dist[i],
SEPARATOR(i, D_CODES - 1, 10)); SEPARATOR(i, D_CODES - 1, 10));
} }
@ -421,7 +460,9 @@ local void init_block(s)
/* Initialize the trees. */ /* Initialize the trees. */
for (n = 0; n < L_CODES; n++) s->dyn_ltree[n].Freq = 0; for (n = 0; n < L_CODES; n++) s->dyn_ltree[n].Freq = 0;
for (n = 0; n < D_CODES; n++) s->dyn_dtree[n].Freq = 0; for (n = 0; n < D_CODES; n++) s->dyn_dtree[n].Freq = 0;
for (n = 0; n < BL_CODES; n++) s->bl_tree[n].Freq = 0; for (n = 0; n < BL_CODES; n++) s->bl_tree[n].Freq = 0;
s->dyn_ltree[END_BLOCK].Freq = 1; s->dyn_ltree[END_BLOCK].Freq = 1;
@ -469,21 +510,27 @@ local void pqdownheap(s, tree, k)
{ {
int v = s->heap[k]; int v = s->heap[k];
int j = k << 1; /* left son of k */ int j = k << 1; /* left son of k */
while (j <= s->heap_len) {
while (j <= s->heap_len)
{
/* Set j to the smallest of the two sons: */ /* Set j to the smallest of the two sons: */
if (j < s->heap_len && if (j < s->heap_len &&
smaller(tree, s->heap[j+1], s->heap[j], s->depth)) { smaller(tree, s->heap[j + 1], s->heap[j], s->depth))
{
j++; j++;
} }
/* Exit if v is smaller than both sons */ /* Exit if v is smaller than both sons */
if (smaller(tree, v, s->heap[j], s->depth)) break; if (smaller(tree, v, s->heap[j], s->depth)) break;
/* Exchange v with the smallest son */ /* Exchange v with the smallest son */
s->heap[k] = s->heap[j]; k = j; s->heap[k] = s->heap[j];
k = j;
/* And continue down the tree, setting j to the left son of k */ /* And continue down the tree, setting j to the left son of k */
j <<= 1; j <<= 1;
} }
s->heap[k] = v; s->heap[k] = v;
} }
@ -525,10 +572,13 @@ local void gen_bitlen(s, desc)
*/ */
tree[s->heap[s->heap_max]].Len = 0; /* root of the heap */ tree[s->heap[s->heap_max]].Len = 0; /* root of the heap */
for (h = s->heap_max+1; h < HEAP_SIZE; h++) { for (h = s->heap_max + 1; h < HEAP_SIZE; h++)
{
n = s->heap[h]; n = s->heap[h];
bits = tree[tree[n].Dad].Len + 1; bits = tree[tree[n].Dad].Len + 1;
if (bits > max_length) bits = max_length, overflow++; if (bits > max_length) bits = max_length, overflow++;
tree[n].Len = (ush)bits; tree[n].Len = (ush)bits;
/* We overwrite tree[n].Dad which is no longer needed */ /* We overwrite tree[n].Dad which is no longer needed */
@ -536,20 +586,27 @@ local void gen_bitlen(s, desc)
s->bl_count[bits]++; s->bl_count[bits]++;
xbits = 0; xbits = 0;
if (n >= base) xbits = extra[n - base]; if (n >= base) xbits = extra[n - base];
f = tree[n].Freq; f = tree[n].Freq;
s->opt_len += (ulg)f * (bits + xbits); s->opt_len += (ulg)f * (bits + xbits);
if (stree) s->static_len += (ulg)f * (stree[n].Len + xbits); if (stree) s->static_len += (ulg)f * (stree[n].Len + xbits);
} }
if (overflow == 0) return; if (overflow == 0) return;
Trace((stderr, "\nbit length overflow\n")); Trace((stderr, "\nbit length overflow\n"));
/* This happens for example on obj2 and pic of the Calgary corpus */ /* This happens for example on obj2 and pic of the Calgary corpus */
/* Find the first bit length which could increase: */ /* Find the first bit length which could increase: */
do { do
{
bits = max_length - 1; bits = max_length - 1;
while (s->bl_count[bits] == 0) bits--; while (s->bl_count[bits] == 0) bits--;
s->bl_count[bits]--; /* move one leaf down the tree */ s->bl_count[bits]--; /* move one leaf down the tree */
s->bl_count[bits + 1] += 2; /* move one overflow item as its brother */ s->bl_count[bits + 1] += 2; /* move one overflow item as its brother */
s->bl_count[max_length]--; s->bl_count[max_length]--;
@ -557,24 +614,32 @@ local void gen_bitlen(s, desc)
* but this does not affect bl_count[max_length] * but this does not affect bl_count[max_length]
*/ */
overflow -= 2; overflow -= 2;
} while (overflow > 0); }
while (overflow > 0);
/* Now recompute all bit lengths, scanning in increasing frequency. /* Now recompute all bit lengths, scanning in increasing frequency.
* h is still equal to HEAP_SIZE. (It is simpler to reconstruct all * h is still equal to HEAP_SIZE. (It is simpler to reconstruct all
* lengths instead of fixing only the wrong ones. This idea is taken * lengths instead of fixing only the wrong ones. This idea is taken
* from 'ar' written by Haruhiko Okumura.) * from 'ar' written by Haruhiko Okumura.)
*/ */
for (bits = max_length; bits != 0; bits--) { for (bits = max_length; bits != 0; bits--)
{
n = s->bl_count[bits]; n = s->bl_count[bits];
while (n != 0) {
while (n != 0)
{
m = s->heap[--h]; m = s->heap[--h];
if (m > max_code) continue; if (m > max_code) continue;
if ((unsigned) tree[m].Len != (unsigned) bits) {
if ((unsigned) tree[m].Len != (unsigned) bits)
{
Trace((stderr, "code %d bits %d->%d\n", m, tree[m].Len, bits)); Trace((stderr, "code %d bits %d->%d\n", m, tree[m].Len, bits));
s->opt_len += ((long)bits - (long)tree[m].Len) s->opt_len += ((long)bits - (long)tree[m].Len)
* (long)tree[m].Freq; * (long)tree[m].Freq;
tree[m].Len = (ush)bits; tree[m].Len = (ush)bits;
} }
n--; n--;
} }
} }
@ -605,9 +670,11 @@ local void gen_codes (tree, max_code, bl_count)
/* The distribution counts are first used to generate the code values /* The distribution counts are first used to generate the code values
* without bit reversal. * without bit reversal.
*/ */
for (bits = 1; bits <= MAX_BITS; bits++) { for (bits = 1; bits <= MAX_BITS; bits++)
{
next_code[bits] = code = (code + bl_count[bits - 1]) << 1; next_code[bits] = code = (code + bl_count[bits - 1]) << 1;
} }
/* Check that the bit counts in bl_count are consistent. The last code /* Check that the bit counts in bl_count are consistent. The last code
* must be all ones. * must be all ones.
*/ */
@ -615,13 +682,16 @@ local void gen_codes (tree, max_code, bl_count)
"inconsistent bit counts"); "inconsistent bit counts");
Tracev((stderr, "\ngen_codes: max_code %d ", max_code)); Tracev((stderr, "\ngen_codes: max_code %d ", max_code));
for (n = 0; n <= max_code; n++) { for (n = 0; n <= max_code; n++)
{
#ifdef WIN32 #ifdef WIN32
ush len = tree[n].Len; ush len = tree[n].Len;
#else #else
int len = tree[n].Len; int len = tree[n].Len;
#endif #endif
if (len == 0) continue; if (len == 0) continue;
/* Now reverse the bits */ /* Now reverse the bits */
#ifdef WIN32 #ifdef WIN32
tree[n].Code = (ush) bi_reverse(next_code[len]++, (int) len); tree[n].Code = (ush) bi_reverse(next_code[len]++, (int) len);
@ -663,11 +733,15 @@ local void build_tree(s, desc)
*/ */
s->heap_len = 0, s->heap_max = HEAP_SIZE; s->heap_len = 0, s->heap_max = HEAP_SIZE;
for (n = 0; n < elems; n++) { for (n = 0; n < elems; n++)
if (tree[n].Freq != 0) { {
if (tree[n].Freq != 0)
{
s->heap[++(s->heap_len)] = max_code = n; s->heap[++(s->heap_len)] = max_code = n;
s->depth[n] = 0; s->depth[n] = 0;
} else { }
else
{
tree[n].Len = 0; tree[n].Len = 0;
} }
} }
@ -677,13 +751,18 @@ local void build_tree(s, desc)
* possible code. So to avoid special checks later on we force at least * possible code. So to avoid special checks later on we force at least
* two codes of non zero frequency. * two codes of non zero frequency.
*/ */
while (s->heap_len < 2) { while (s->heap_len < 2)
{
node = s->heap[++(s->heap_len)] = (max_code < 2 ? ++max_code : 0); node = s->heap[++(s->heap_len)] = (max_code < 2 ? ++max_code : 0);
tree[node].Freq = 1; tree[node].Freq = 1;
s->depth[node] = 0; s->depth[node] = 0;
s->opt_len--; if (stree) s->static_len -= stree[node].Len; s->opt_len--;
if (stree) s->static_len -= stree[node].Len;
/* node is 0 or 1 so it does not have extra bits */ /* node is 0 or 1 so it does not have extra bits */
} }
desc->max_code = max_code; desc->max_code = max_code;
/* The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree, /* The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
@ -695,7 +774,9 @@ local void build_tree(s, desc)
* frequent nodes. * frequent nodes.
*/ */
node = elems; /* next internal node of the tree */ node = elems; /* next internal node of the tree */
do {
do
{
pqremove(s, tree, n); /* n = node of least frequency */ pqremove(s, tree, n); /* n = node of least frequency */
m = s->heap[SMALLEST]; /* m = node of next least frequency */ m = s->heap[SMALLEST]; /* m = node of next least frequency */
@ -708,16 +789,20 @@ local void build_tree(s, desc)
s->depth[n] : s->depth[m]) + 1); s->depth[n] : s->depth[m]) + 1);
tree[n].Dad = tree[m].Dad = (ush)node; tree[n].Dad = tree[m].Dad = (ush)node;
#ifdef DUMP_BL_TREE #ifdef DUMP_BL_TREE
if (tree == s->bl_tree) {
if (tree == s->bl_tree)
{
fprintf(stderr, "\nnode %d(%d), sons %d(%d) %d(%d)", fprintf(stderr, "\nnode %d(%d), sons %d(%d) %d(%d)",
node, tree[node].Freq, n, tree[n].Freq, m, tree[m].Freq); node, tree[node].Freq, n, tree[n].Freq, m, tree[m].Freq);
} }
#endif #endif
/* and insert the new node in the heap */ /* and insert the new node in the heap */
s->heap[SMALLEST] = node++; s->heap[SMALLEST] = node++;
pqdownheap(s, tree, SMALLEST); pqdownheap(s, tree, SMALLEST);
} while (s->heap_len >= 2); }
while (s->heap_len >= 2);
s->heap[--(s->heap_max)] = s->heap[SMALLEST]; s->heap[--(s->heap_max)] = s->heap[SMALLEST];
@ -759,28 +844,50 @@ local void scan_tree (s, tree, max_code)
#endif #endif
if (nextlen == 0) max_count = 138, min_count = 3; if (nextlen == 0) max_count = 138, min_count = 3;
tree[max_code + 1].Len = (ush)0xffff; /* guard */ tree[max_code + 1].Len = (ush)0xffff; /* guard */
for (n = 0; n <= max_code; n++) { for (n = 0; n <= max_code; n++)
curlen = nextlen; nextlen = tree[n+1].Len; {
if (++count < max_count && curlen == nextlen) { curlen = nextlen;
nextlen = tree[n + 1].Len;
if (++count < max_count && curlen == nextlen)
{
continue; continue;
} else if (count < min_count) { }
else if (count < min_count)
{
s->bl_tree[curlen].Freq += count; s->bl_tree[curlen].Freq += count;
} else if (curlen != 0) { }
else if (curlen != 0)
{
if (curlen != prevlen) s->bl_tree[curlen].Freq++; if (curlen != prevlen) s->bl_tree[curlen].Freq++;
s->bl_tree[REP_3_6].Freq++; s->bl_tree[REP_3_6].Freq++;
} else if (count <= 10) { }
else if (count <= 10)
{
s->bl_tree[REPZ_3_10].Freq++; s->bl_tree[REPZ_3_10].Freq++;
} else { }
else
{
s->bl_tree[REPZ_11_138].Freq++; s->bl_tree[REPZ_11_138].Freq++;
} }
count = 0; prevlen = curlen;
if (nextlen == 0) { count = 0;
prevlen = curlen;
if (nextlen == 0)
{
max_count = 138, min_count = 3; max_count = 138, min_count = 3;
} else if (curlen == nextlen) { }
else if (curlen == nextlen)
{
max_count = 6, min_count = 3; max_count = 6, min_count = 3;
} else { }
else
{
max_count = 7, min_count = 4; max_count = 7, min_count = 4;
} }
} }
@ -810,32 +917,59 @@ local void send_tree (s, tree, max_code)
/* tree[max_code+1].Len = -1; */ /* guard already set */ /* tree[max_code+1].Len = -1; */ /* guard already set */
if (nextlen == 0) max_count = 138, min_count = 3; if (nextlen == 0) max_count = 138, min_count = 3;
for (n = 0; n <= max_code; n++) { for (n = 0; n <= max_code; n++)
curlen = nextlen; nextlen = tree[n+1].Len; {
if (++count < max_count && curlen == nextlen) { curlen = nextlen;
nextlen = tree[n + 1].Len;
if (++count < max_count && curlen == nextlen)
{
continue; continue;
} else if (count < min_count) {
do { send_code(s, curlen, s->bl_tree); } while (--count != 0);
} else if (curlen != 0) {
if (curlen != prevlen) {
send_code(s, curlen, s->bl_tree); count--;
} }
else if (count < min_count)
{
do { send_code(s, curlen, s->bl_tree); }
while (--count != 0);
}
else if (curlen != 0)
{
if (curlen != prevlen)
{
send_code(s, curlen, s->bl_tree);
count--;
}
Assert(count >= 3 && count <= 6, " 3_6?"); Assert(count >= 3 && count <= 6, " 3_6?");
send_code(s, REP_3_6, s->bl_tree); send_bits(s, count-3, 2); send_code(s, REP_3_6, s->bl_tree);
send_bits(s, count - 3, 2);
} else if (count <= 10) {
send_code(s, REPZ_3_10, s->bl_tree); send_bits(s, count-3, 3);
} else {
send_code(s, REPZ_11_138, s->bl_tree); send_bits(s, count-11, 7);
} }
count = 0; prevlen = curlen; else if (count <= 10)
if (nextlen == 0) { {
send_code(s, REPZ_3_10, s->bl_tree);
send_bits(s, count - 3, 3);
}
else
{
send_code(s, REPZ_11_138, s->bl_tree);
send_bits(s, count - 11, 7);
}
count = 0;
prevlen = curlen;
if (nextlen == 0)
{
max_count = 138, min_count = 3; max_count = 138, min_count = 3;
} else if (curlen == nextlen) { }
else if (curlen == nextlen)
{
max_count = 6, min_count = 3; max_count = 6, min_count = 3;
} else { }
else
{
max_count = 7, min_count = 4; max_count = 7, min_count = 4;
} }
} }
@ -868,9 +1002,11 @@ local int build_bl_tree(s)
* requires that at least 4 bit length codes be sent. (appnote.txt says * requires that at least 4 bit length codes be sent. (appnote.txt says
* 3 but the actual value used is 4.) * 3 but the actual value used is 4.)
*/ */
for (max_blindex = BL_CODES-1; max_blindex >= 3; max_blindex--) { for (max_blindex = BL_CODES - 1; max_blindex >= 3; max_blindex--)
{
if (s->bl_tree[bl_order[max_blindex]].Len != 0) break; if (s->bl_tree[bl_order[max_blindex]].Len != 0) break;
} }
/* Update opt_len to include the bit length tree and counts */ /* Update opt_len to include the bit length tree and counts */
s->opt_len += 3 * (max_blindex + 1) + 5 + 5 + 4; s->opt_len += 3 * (max_blindex + 1) + 5 + 5 + 4;
Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld", Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld",
@ -901,10 +1037,13 @@ local void send_all_trees(s, lcodes, dcodes, blcodes)
send_bits(s, lcodes - 257, 5); /* not +255 as stated in appnote.txt */ send_bits(s, lcodes - 257, 5); /* not +255 as stated in appnote.txt */
send_bits(s, dcodes - 1, 5); send_bits(s, dcodes - 1, 5);
send_bits(s, blcodes - 4, 4); /* not -3 as stated in appnote.txt */ send_bits(s, blcodes - 4, 4); /* not -3 as stated in appnote.txt */
for (rank = 0; rank < blcodes; rank++) {
for (rank = 0; rank < blcodes; rank++)
{
Tracev((stderr, "\nbl code %2d ", bl_order[rank])); Tracev((stderr, "\nbl code %2d ", bl_order[rank]));
send_bits(s, s->bl_tree[bl_order[rank]].Len, 3); send_bits(s, s->bl_tree[bl_order[rank]].Len, 3);
} }
Tracev((stderr, "\nbl tree: sent %ld", s->bits_sent)); Tracev((stderr, "\nbl tree: sent %ld", s->bits_sent));
send_tree(s, (ct_data*)s->dyn_ltree, lcodes - 1); /* literal tree */ send_tree(s, (ct_data*)s->dyn_ltree, lcodes - 1); /* literal tree */
@ -985,7 +1124,8 @@ void ZLIB_INTERNAL _tr_flush_block(s, buf, stored_len, last)
int max_blindex = 0; /* index of last bit length code of non zero freq */ int max_blindex = 0; /* index of last bit length code of non zero freq */
/* Build the Huffman trees unless a stored block is forced */ /* Build the Huffman trees unless a stored block is forced */
if (s->level > 0) { if (s->level > 0)
{
/* Check if the file is binary or text */ /* Check if the file is binary or text */
if (s->strm->data_type == Z_UNKNOWN) if (s->strm->data_type == Z_UNKNOWN)
@ -1018,15 +1158,21 @@ void ZLIB_INTERNAL _tr_flush_block(s, buf, stored_len, last)
if (static_lenb <= opt_lenb) opt_lenb = static_lenb; if (static_lenb <= opt_lenb) opt_lenb = static_lenb;
} else { }
else
{
Assert(buf != (char*)0, "lost buf"); Assert(buf != (char*)0, "lost buf");
opt_lenb = static_lenb = stored_len + 5; /* force a stored block */ opt_lenb = static_lenb = stored_len + 5; /* force a stored block */
} }
#ifdef FORCE_STORED #ifdef FORCE_STORED
if (buf != (char*)0) { /* force stored block */
if (buf != (char*)0) /* force stored block */
{
#else #else
if (stored_len+4 <= opt_lenb && buf != (char*)0) {
if (stored_len + 4 <= opt_lenb && buf != (char*)0)
{
/* 4: two words for the lengths */ /* 4: two words for the lengths */
#endif #endif
/* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE. /* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE.
@ -1038,16 +1184,22 @@ void ZLIB_INTERNAL _tr_flush_block(s, buf, stored_len, last)
_tr_stored_block(s, buf, stored_len, last); _tr_stored_block(s, buf, stored_len, last);
#ifdef FORCE_STATIC #ifdef FORCE_STATIC
} else if (static_lenb >= 0) { /* force static trees */ }
else if (static_lenb >= 0) /* force static trees */
{
#else #else
} else if (s->strategy == Z_FIXED || static_lenb == opt_lenb) { }
else if (s->strategy == Z_FIXED || static_lenb == opt_lenb)
{
#endif #endif
send_bits(s, (STATIC_TREES << 1) + last, 3); send_bits(s, (STATIC_TREES << 1) + last, 3);
compress_block(s, (ct_data*)static_ltree, (ct_data*)static_dtree); compress_block(s, (ct_data*)static_ltree, (ct_data*)static_dtree);
#ifdef DEBUG #ifdef DEBUG
s->compressed_len += 3 + s->static_len; s->compressed_len += 3 + s->static_len;
#endif #endif
} else { }
else
{
send_bits(s, (DYN_TREES << 1) + last, 3); send_bits(s, (DYN_TREES << 1) + last, 3);
send_all_trees(s, s->l_desc.max_code + 1, s->d_desc.max_code + 1, send_all_trees(s, s->l_desc.max_code + 1, s->d_desc.max_code + 1,
max_blindex + 1); max_blindex + 1);
@ -1056,18 +1208,21 @@ void ZLIB_INTERNAL _tr_flush_block(s, buf, stored_len, last)
s->compressed_len += 3 + s->opt_len; s->compressed_len += 3 + s->opt_len;
#endif #endif
} }
Assert(s->compressed_len == s->bits_sent, "bad compressed size"); Assert(s->compressed_len == s->bits_sent, "bad compressed size");
/* The above check is made mod 2^32, for files larger than 512 MB /* The above check is made mod 2^32, for files larger than 512 MB
* and uLong implemented on 32 bits. * and uLong implemented on 32 bits.
*/ */
init_block(s); init_block(s);
if (last) { if (last)
{
bi_windup(s); bi_windup(s);
#ifdef DEBUG #ifdef DEBUG
s->compressed_len += 7; /* align on byte boundary */ s->compressed_len += 7; /* align on byte boundary */
#endif #endif
} }
Tracev((stderr, "\ncomprlen %lu(%lu) ", s->compressed_len >> 3, Tracev((stderr, "\ncomprlen %lu(%lu) ", s->compressed_len >> 3,
s->compressed_len - 7 * last)); s->compressed_len - 7 * last));
} }
@ -1087,10 +1242,14 @@ int ZLIB_INTERNAL _tr_tally (s, dist, lc)
{ {
s->d_buf[s->last_lit] = (ush)dist; s->d_buf[s->last_lit] = (ush)dist;
s->l_buf[s->last_lit++] = (uch)lc; s->l_buf[s->last_lit++] = (uch)lc;
if (dist == 0) {
if (dist == 0)
{
/* lc is the unmatched char */ /* lc is the unmatched char */
s->dyn_ltree[lc].Freq++; s->dyn_ltree[lc].Freq++;
} else { }
else
{
s->matches++; s->matches++;
/* Here, lc is the match length - MIN_MATCH */ /* Here, lc is the match length - MIN_MATCH */
dist--; /* dist = match distance - 1 */ dist--; /* dist = match distance - 1 */
@ -1103,22 +1262,29 @@ int ZLIB_INTERNAL _tr_tally (s, dist, lc)
} }
#ifdef TRUNCATE_BLOCK #ifdef TRUNCATE_BLOCK
/* Try to guess if it is profitable to stop the current block here */ /* Try to guess if it is profitable to stop the current block here */
if ((s->last_lit & 0x1fff) == 0 && s->level > 2) { if ((s->last_lit & 0x1fff) == 0 && s->level > 2)
{
/* Compute an upper bound for the compressed length */ /* Compute an upper bound for the compressed length */
ulg out_length = (ulg)s->last_lit * 8L; ulg out_length = (ulg)s->last_lit * 8L;
ulg in_length = (ulg)((long)s->strstart - s->block_start); ulg in_length = (ulg)((long)s->strstart - s->block_start);
int dcode; int dcode;
for (dcode = 0; dcode < D_CODES; dcode++) {
for (dcode = 0; dcode < D_CODES; dcode++)
{
out_length += (ulg)s->dyn_dtree[dcode].Freq * out_length += (ulg)s->dyn_dtree[dcode].Freq *
(5L + extra_dbits[dcode]); (5L + extra_dbits[dcode]);
} }
out_length >>= 3; out_length >>= 3;
Tracev((stderr, "\nlast_lit %u, in %ld, out ~%ld(%ld%%) ", Tracev((stderr, "\nlast_lit %u, in %ld, out ~%ld(%ld%%) ",
s->last_lit, in_length, out_length, s->last_lit, in_length, out_length,
100L - out_length * 100L / in_length)); 100L - out_length * 100L / in_length));
if (s->matches < s->last_lit / 2 && out_length < in_length / 2) return 1; if (s->matches < s->last_lit / 2 && out_length < in_length / 2) return 1;
} }
#endif #endif
return (s->last_lit == s->lit_bufsize - 1); return (s->last_lit == s->lit_bufsize - 1);
/* We avoid equality with lit_bufsize because of wraparound at 64K /* We avoid equality with lit_bufsize because of wraparound at 64K
@ -1145,28 +1311,38 @@ local void compress_block(s, ltree, dtree)
unsigned code; /* the code to send */ unsigned code; /* the code to send */
int extra; /* number of extra bits to send */ int extra; /* number of extra bits to send */
if (s->last_lit != 0) do { if (s->last_lit != 0) do
{
dist = s->d_buf[lx]; dist = s->d_buf[lx];
lc = s->l_buf[lx++]; lc = s->l_buf[lx++];
if (dist == 0) {
if (dist == 0)
{
send_code(s, lc, ltree); /* send a literal byte */ send_code(s, lc, ltree); /* send a literal byte */
Tracecv(isgraph(lc), (stderr, " '%c' ", lc)); Tracecv(isgraph(lc), (stderr, " '%c' ", lc));
} else { }
else
{
/* Here, lc is the match length - MIN_MATCH */ /* Here, lc is the match length - MIN_MATCH */
code = _length_code[lc]; code = _length_code[lc];
send_code(s, code + LITERALS + 1, ltree); /* send the length code */ send_code(s, code + LITERALS + 1, ltree); /* send the length code */
extra = extra_lbits[code]; extra = extra_lbits[code];
if (extra != 0) {
if (extra != 0)
{
lc -= base_length[code]; lc -= base_length[code];
send_bits(s, lc, extra); /* send the extra length bits */ send_bits(s, lc, extra); /* send the extra length bits */
} }
dist--; /* dist is now the match distance - 1 */ dist--; /* dist is now the match distance - 1 */
code = d_code(dist); code = d_code(dist);
Assert(code < D_CODES, "bad d_code"); Assert(code < D_CODES, "bad d_code");
send_code(s, code, dtree); /* send the distance code */ send_code(s, code, dtree); /* send the distance code */
extra = extra_dbits[code]; extra = extra_dbits[code];
if (extra != 0) {
if (extra != 0)
{
dist -= base_dist[code]; dist -= base_dist[code];
send_bits(s, dist, extra); /* send the extra distance bits */ send_bits(s, dist, extra); /* send the extra distance bits */
} }
@ -1176,7 +1352,8 @@ local void compress_block(s, ltree, dtree)
Assert((uInt)(s->pending) < s->lit_bufsize + 2 * lx, Assert((uInt)(s->pending) < s->lit_bufsize + 2 * lx,
"pendingBuf overflow"); "pendingBuf overflow");
} while (lx < s->last_lit); }
while (lx < s->last_lit);
send_code(s, END_BLOCK, ltree); send_code(s, END_BLOCK, ltree);
} }
@ -1217,6 +1394,7 @@ local int detect_data_type(s)
if (s->dyn_ltree[9].Freq != 0 || s->dyn_ltree[10].Freq != 0 if (s->dyn_ltree[9].Freq != 0 || s->dyn_ltree[10].Freq != 0
|| s->dyn_ltree[13].Freq != 0) || s->dyn_ltree[13].Freq != 0)
return Z_TEXT; return Z_TEXT;
for (n = 32; n < LITERALS; n++) for (n = 32; n < LITERALS; n++)
if (s->dyn_ltree[n].Freq != 0) if (s->dyn_ltree[n].Freq != 0)
return Z_TEXT; return Z_TEXT;
@ -1241,10 +1419,14 @@ local unsigned bi_reverse(code, len)
#endif #endif
{ {
register unsigned res = 0; register unsigned res = 0;
do {
do
{
res |= code & 1; res |= code & 1;
code >>= 1, res <<= 1; code >>= 1, res <<= 1;
} while (--len > 0); }
while (--len > 0);
return res >> 1; return res >> 1;
} }
@ -1258,11 +1440,14 @@ local void bi_flush(s)
deflate_state* s; deflate_state* s;
#endif #endif
{ {
if (s->bi_valid == 16) { if (s->bi_valid == 16)
{
put_short(s, s->bi_buf); put_short(s, s->bi_buf);
s->bi_buf = 0; s->bi_buf = 0;
s->bi_valid = 0; s->bi_valid = 0;
} else if (s->bi_valid >= 8) { }
else if (s->bi_valid >= 8)
{
put_byte(s, (Byte)s->bi_buf); put_byte(s, (Byte)s->bi_buf);
s->bi_buf >>= 8; s->bi_buf >>= 8;
s->bi_valid -= 8; s->bi_valid -= 8;
@ -1279,11 +1464,15 @@ local void bi_windup(s)
deflate_state* s; deflate_state* s;
#endif #endif
{ {
if (s->bi_valid > 8) { if (s->bi_valid > 8)
{
put_short(s, s->bi_buf); put_short(s, s->bi_buf);
} else if (s->bi_valid > 0) { }
else if (s->bi_valid > 0)
{
put_byte(s, (Byte)s->bi_buf); put_byte(s, (Byte)s->bi_buf);
} }
s->bi_buf = 0; s->bi_buf = 0;
s->bi_valid = 0; s->bi_valid = 0;
#ifdef DEBUG #ifdef DEBUG
@ -1307,17 +1496,21 @@ local void copy_block(s, buf, len, header)
{ {
bi_windup(s); /* align on byte boundary */ bi_windup(s); /* align on byte boundary */
if (header) { if (header)
{
put_short(s, (ush)len); put_short(s, (ush)len);
put_short(s, (ush)~len); put_short(s, (ush)~len);
#ifdef DEBUG #ifdef DEBUG
s->bits_sent += 2 * 16; s->bits_sent += 2 * 16;
#endif #endif
} }
#ifdef DEBUG #ifdef DEBUG
s->bits_sent += (ulg)len << 3; s->bits_sent += (ulg)len << 3;
#endif #endif
while (len--) {
while (len--)
{
put_byte(s, *buf++); put_byte(s, *buf++);
} }
} }

18
extern/zlib/trees.h vendored
View File

@ -1,6 +1,7 @@
/* header created automatically with -DGEN_TREES_H */ /* header created automatically with -DGEN_TREES_H */
local const ct_data static_ltree[L_CODES+2] = { local const ct_data static_ltree[L_CODES + 2] =
{
{{ 12}, { 8}}, {{140}, { 8}}, {{ 76}, { 8}}, {{204}, { 8}}, {{ 44}, { 8}}, {{ 12}, { 8}}, {{140}, { 8}}, {{ 76}, { 8}}, {{204}, { 8}}, {{ 44}, { 8}},
{{172}, { 8}}, {{108}, { 8}}, {{236}, { 8}}, {{ 28}, { 8}}, {{156}, { 8}}, {{172}, { 8}}, {{108}, { 8}}, {{236}, { 8}}, {{ 28}, { 8}}, {{156}, { 8}},
{{ 92}, { 8}}, {{220}, { 8}}, {{ 60}, { 8}}, {{188}, { 8}}, {{124}, { 8}}, {{ 92}, { 8}}, {{220}, { 8}}, {{ 60}, { 8}}, {{188}, { 8}}, {{124}, { 8}},
@ -61,7 +62,8 @@ local const ct_data static_ltree[L_CODES+2] = {
{{163}, { 8}}, {{ 99}, { 8}}, {{227}, { 8}} {{163}, { 8}}, {{ 99}, { 8}}, {{227}, { 8}}
}; };
local const ct_data static_dtree[D_CODES] = { local const ct_data static_dtree[D_CODES] =
{
{{ 0}, { 5}}, {{16}, { 5}}, {{ 8}, { 5}}, {{24}, { 5}}, {{ 4}, { 5}}, {{ 0}, { 5}}, {{16}, { 5}}, {{ 8}, { 5}}, {{24}, { 5}}, {{ 4}, { 5}},
{{20}, { 5}}, {{12}, { 5}}, {{28}, { 5}}, {{ 2}, { 5}}, {{18}, { 5}}, {{20}, { 5}}, {{12}, { 5}}, {{28}, { 5}}, {{ 2}, { 5}}, {{18}, { 5}},
{{10}, { 5}}, {{26}, { 5}}, {{ 6}, { 5}}, {{22}, { 5}}, {{14}, { 5}}, {{10}, { 5}}, {{26}, { 5}}, {{ 6}, { 5}}, {{22}, { 5}}, {{14}, { 5}},
@ -70,7 +72,8 @@ local const ct_data static_dtree[D_CODES] = {
{{19}, { 5}}, {{11}, { 5}}, {{27}, { 5}}, {{ 7}, { 5}}, {{23}, { 5}} {{19}, { 5}}, {{11}, { 5}}, {{27}, { 5}}, {{ 7}, { 5}}, {{23}, { 5}}
}; };
const uch ZLIB_INTERNAL _dist_code[DIST_CODE_LEN] = { const uch ZLIB_INTERNAL _dist_code[DIST_CODE_LEN] =
{
0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 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, 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, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
@ -99,7 +102,8 @@ const uch ZLIB_INTERNAL _dist_code[DIST_CODE_LEN] = {
29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29
}; };
const uch ZLIB_INTERNAL _length_code[MAX_MATCH-MIN_MATCH+1]= { const uch ZLIB_INTERNAL _length_code[MAX_MATCH - MIN_MATCH + 1] =
{
0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 12, 12, 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, 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, 17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19,
@ -115,12 +119,14 @@ const uch ZLIB_INTERNAL _length_code[MAX_MATCH-MIN_MATCH+1]= {
27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28
}; };
local const int base_length[LENGTH_CODES] = { local const int base_length[LENGTH_CODES] =
{
0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 32, 40, 48, 56, 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 64, 80, 96, 112, 128, 160, 192, 224, 0
}; };
local const int base_dist[D_CODES] = { local const int base_dist[D_CODES] =
{
0, 1, 2, 3, 4, 6, 8, 12, 16, 24, 0, 1, 2, 3, 4, 6, 8, 12, 16, 24,
32, 48, 64, 96, 128, 192, 256, 384, 512, 768, 32, 48, 64, 96, 128, 192, 256, 384, 512, 768,
1024, 1536, 2048, 3072, 4096, 6144, 8192, 12288, 16384, 24576 1024, 1536, 2048, 3072, 4096, 6144, 8192, 12288, 16384, 24576

10
extern/zlib/uncompr.c vendored
View File

@ -36,26 +36,34 @@ int ZEXPORT uncompress (dest, destLen, source, sourceLen)
stream.next_in = (Bytef*)source; stream.next_in = (Bytef*)source;
stream.avail_in = (uInt)sourceLen; stream.avail_in = (uInt)sourceLen;
/* Check for source > 64K on 16-bit machine: */ /* Check for source > 64K on 16-bit machine: */
if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR; if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
stream.next_out = dest; stream.next_out = dest;
stream.avail_out = (uInt) * destLen; stream.avail_out = (uInt) * destLen;
if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR; if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
stream.zalloc = (alloc_func)0; stream.zalloc = (alloc_func)0;
stream.zfree = (free_func)0; stream.zfree = (free_func)0;
err = inflateInit(&stream); err = inflateInit(&stream);
if (err != Z_OK) return err; if (err != Z_OK) return err;
err = inflate(&stream, Z_FINISH); err = inflate(&stream, Z_FINISH);
if (err != Z_STREAM_END) {
if (err != Z_STREAM_END)
{
inflateEnd(&stream); inflateEnd(&stream);
if (err == Z_NEED_DICT || (err == Z_BUF_ERROR && stream.avail_in == 0)) if (err == Z_NEED_DICT || (err == Z_BUF_ERROR && stream.avail_in == 0))
return Z_DATA_ERROR; return Z_DATA_ERROR;
return err; return err;
} }
*destLen = stream.total_out; *destLen = stream.total_out;
err = inflateEnd(&stream); err = inflateEnd(&stream);

9
extern/zlib/zlib.h vendored
View File

@ -107,7 +107,8 @@ typedef void (*free_func) OF((voidpf opaque, voidpf address));
struct internal_state; struct internal_state;
typedef struct z_stream_s { typedef struct z_stream_s
{
z_const Bytef* next_in; /* next input byte */ z_const Bytef* next_in; /* next input byte */
uInt avail_in; /* number of bytes available at next_in */ uInt avail_in; /* number of bytes available at next_in */
uLong total_in; /* total number of input bytes read so far */ uLong total_in; /* total number of input bytes read so far */
@ -134,7 +135,8 @@ typedef z_stream FAR *z_streamp;
gzip header information passed to and from zlib routines. See RFC 1952 gzip header information passed to and from zlib routines. See RFC 1952
for more details on the meanings of these fields. for more details on the meanings of these fields.
*/ */
typedef struct gz_header_s { typedef struct gz_header_s
{
int text; /* true if compressed data believed to be text */ int text; /* true if compressed data believed to be text */
uLong time; /* modification time */ uLong time; /* modification time */
int xflags; /* extra flags (not used when writing a gzip file) */ int xflags; /* extra flags (not used when writing a gzip file) */
@ -1683,7 +1685,8 @@ ZEXTERN int ZEXPORT inflateBackInit_ OF((z_streamp strm, int windowBits,
* behavior could change in the future, perhaps even capriciously. They can * behavior could change in the future, perhaps even capriciously. They can
* only be used by the gzgetc() macro. You have been warned. * only be used by the gzgetc() macro. You have been warned.
*/ */
struct gzFile_s { struct gzFile_s
{
unsigned have; unsigned have;
unsigned char* next; unsigned char* next;
z_off64_t pos; z_off64_t pos;

140
extern/zlib/zutil.c vendored
View File

@ -14,7 +14,8 @@
struct internal_state {int dummy;}; /* for buggy compilers */ struct internal_state {int dummy;}; /* for buggy compilers */
#endif #endif
const char * const z_errmsg[10] = { const char* const z_errmsg[10] =
{
"need dictionary", /* Z_NEED_DICT 2 */ "need dictionary", /* Z_NEED_DICT 2 */
"stream end", /* Z_STREAM_END 1 */ "stream end", /* Z_STREAM_END 1 */
"", /* Z_OK 0 */ "", /* Z_OK 0 */
@ -24,7 +25,8 @@ const char * const z_errmsg[10] = {
"insufficient memory", /* Z_MEM_ERROR (-4) */ "insufficient memory", /* Z_MEM_ERROR (-4) */
"buffer error", /* Z_BUF_ERROR (-5) */ "buffer error", /* Z_BUF_ERROR (-5) */
"incompatible version",/* Z_VERSION_ERROR (-6) */ "incompatible version",/* Z_VERSION_ERROR (-6) */
""}; ""
};
const char* ZEXPORT zlibVersion() const char* ZEXPORT zlibVersion()
@ -37,30 +39,75 @@ uLong ZEXPORT zlibCompileFlags()
uLong flags; uLong flags;
flags = 0; flags = 0;
switch ((int)(sizeof(uInt))) {
case 2: break; switch ((int)(sizeof(uInt)))
case 4: flags += 1; break; {
case 8: flags += 2; break; case 2:
default: flags += 3; break;
case 4:
flags += 1;
break;
case 8:
flags += 2;
break;
default:
flags += 3;
} }
switch ((int)(sizeof(uLong))) {
case 2: break; switch ((int)(sizeof(uLong)))
case 4: flags += 1 << 2; break; {
case 8: flags += 2 << 2; break; case 2:
default: flags += 3 << 2; break;
case 4:
flags += 1 << 2;
break;
case 8:
flags += 2 << 2;
break;
default:
flags += 3 << 2;
} }
switch ((int)(sizeof(voidpf))) {
case 2: break; switch ((int)(sizeof(voidpf)))
case 4: flags += 1 << 4; break; {
case 8: flags += 2 << 4; break; case 2:
default: flags += 3 << 4; break;
case 4:
flags += 1 << 4;
break;
case 8:
flags += 2 << 4;
break;
default:
flags += 3 << 4;
} }
switch ((int)(sizeof(z_off_t))) {
case 2: break; switch ((int)(sizeof(z_off_t)))
case 4: flags += 1 << 6; break; {
case 8: flags += 2 << 6; break; case 2:
default: flags += 3 << 6; break;
case 4:
flags += 1 << 6;
break;
case 8:
flags += 2 << 6;
break;
default:
flags += 3 << 6;
} }
#ifdef DEBUG #ifdef DEBUG
flags += 1 << 8; flags += 1 << 8;
#endif #endif
@ -159,9 +206,12 @@ void ZLIB_INTERNAL zmemcpy(dest, source, len)
uInt len; uInt len;
{ {
if (len == 0) return; if (len == 0) return;
do {
do
{
*dest++ = *source++; /* ??? to be unrolled */ *dest++ = *source++; /* ??? to be unrolled */
} while (--len != 0); }
while (--len != 0);
} }
int ZLIB_INTERNAL zmemcmp(s1, s2, len) int ZLIB_INTERNAL zmemcmp(s1, s2, len)
@ -171,9 +221,11 @@ int ZLIB_INTERNAL zmemcmp(s1, s2, len)
{ {
uInt j; uInt j;
for (j = 0; j < len; j++) { for (j = 0; j < len; j++)
{
if (s1[j] != s2[j]) return 2 * (s1[j] > s2[j]) - 1; if (s1[j] != s2[j]) return 2 * (s1[j] > s2[j]) - 1;
} }
return 0; return 0;
} }
@ -182,9 +234,12 @@ void ZLIB_INTERNAL zmemzero(dest, len)
uInt len; uInt len;
{ {
if (len == 0) return; if (len == 0) return;
do {
do
{
*dest++ = 0; /* ??? to be unrolled */ *dest++ = 0; /* ??? to be unrolled */
} while (--len != 0); }
while (--len != 0);
} }
#endif #endif
@ -208,7 +263,8 @@ void ZLIB_INTERNAL zmemzero(dest, len)
local int next_ptr = 0; local int next_ptr = 0;
typedef struct ptr_table_s { typedef struct ptr_table_s
{
voidpf org_ptr; voidpf org_ptr;
voidpf new_ptr; voidpf new_ptr;
} ptr_table; } ptr_table;
@ -229,13 +285,19 @@ voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, unsigned items, unsigned size)
/* If we allocate less than 65520 bytes, we assume that farmalloc /* If we allocate less than 65520 bytes, we assume that farmalloc
* will return a usable pointer which doesn't have to be normalized. * will return a usable pointer which doesn't have to be normalized.
*/ */
if (bsize < 65520L) { if (bsize < 65520L)
{
buf = farmalloc(bsize); buf = farmalloc(bsize);
if (*(ush*)&buf != 0) return buf; if (*(ush*)&buf != 0) return buf;
} else { }
else
{
buf = farmalloc(bsize + 16L); buf = farmalloc(bsize + 16L);
} }
if (buf == NULL || next_ptr >= MAX_PTR) return NULL; if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
table[next_ptr].org_ptr = buf; table[next_ptr].org_ptr = buf;
/* Normalize the pointer to seg:0 */ /* Normalize the pointer to seg:0 */
@ -248,21 +310,29 @@ voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, unsigned items, unsigned size)
void ZLIB_INTERNAL zcfree(voidpf opaque, voidpf ptr) void ZLIB_INTERNAL zcfree(voidpf opaque, voidpf ptr)
{ {
int n; int n;
if (*(ush*)&ptr != 0) { /* object < 64K */
if (*(ush*)&ptr != 0) /* object < 64K */
{
farfree(ptr); farfree(ptr);
return; return;
} }
/* Find the original pointer */ /* Find the original pointer */
for (n = 0; n < next_ptr; n++) { for (n = 0; n < next_ptr; n++)
{
if (ptr != table[n].new_ptr) continue; if (ptr != table[n].new_ptr) continue;
farfree(table[n].org_ptr); farfree(table[n].org_ptr);
while (++n < next_ptr) {
while (++n < next_ptr)
{
table[n - 1] = table[n]; table[n - 1] = table[n];
} }
next_ptr--; next_ptr--;
return; return;
} }
ptr = opaque; /* just to make some compilers happy */ ptr = opaque; /* just to make some compilers happy */
Assert(0, "zcfree: ptr not found"); Assert(0, "zcfree: ptr not found");
} }
@ -283,12 +353,14 @@ void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr)
voidpf ZLIB_INTERNAL zcalloc(voidpf opaque, uInt items, uInt size) voidpf ZLIB_INTERNAL zcalloc(voidpf opaque, uInt items, uInt size)
{ {
if (opaque) opaque = 0; /* to make compiler happy */ if (opaque) opaque = 0; /* to make compiler happy */
return _halloc((long)items, size); return _halloc((long)items, size);
} }
void ZLIB_INTERNAL zcfree(voidpf opaque, voidpf ptr) void ZLIB_INTERNAL zcfree(voidpf opaque, voidpf ptr)
{ {
if (opaque) opaque = 0; /* to make compiler happy */ if (opaque) opaque = 0; /* to make compiler happy */
_hfree(ptr); _hfree(ptr);
} }
@ -315,6 +387,7 @@ voidpf ZLIB_INTERNAL zcalloc (opaque, items, size)
#endif #endif
{ {
if (opaque) items += size - size; /* make compiler happy */ if (opaque) items += size - size; /* make compiler happy */
return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) : return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) :
(voidpf)calloc(items, size); (voidpf)calloc(items, size);
} }
@ -328,6 +401,7 @@ void ZLIB_INTERNAL zcfree (opaque, ptr)
#endif #endif
{ {
free(ptr); free(ptr);
if (opaque) return; /* make compiler happy */ if (opaque) return; /* make compiler happy */
} }

View File

@ -337,7 +337,8 @@ public:
* \brief setCrystals * \brief setCrystals
* \param val * \param val
*/ */
void setCrystals(ALTTPCrystals val);\ void setCrystals(ALTTPCrystals val);
\
/*! /*!
* \brief crystals * \brief crystals

View File

@ -54,9 +54,12 @@ public:
do { \ do { \
if (atGetExceptionHandler()) {atGetExceptionHandler()(__FILE__, AT_PRETTY_FUNCTION, __LINE__, __VA_ARGS__); return; \ if (atGetExceptionHandler()) {atGetExceptionHandler()(__FILE__, AT_PRETTY_FUNCTION, __LINE__, __VA_ARGS__); return; \
} else { } else {
std::string msg = Athena::utility::sprintf(args, __VA_ARGS__); \ std::string msg = Athena::utility::sprintf(args, __VA_ARGS__);
throw Athena::error::IOException(std::string("IOException: ")+msg, __FILE__, AT_PRETTY_FUNCTION, __LINE__); \ \
throw Athena::error::IOException(std::string("IOException: ") + msg, __FILE__, AT_PRETTY_FUNCTION, __LINE__);
\
} \ } \
} while (0) } while (0)
#elif defined(__GNUC__) #elif defined(__GNUC__)
#define THROW_IO_EXCEPTION(args...) \ #define THROW_IO_EXCEPTION(args...) \
@ -73,9 +76,13 @@ public:
do { \ do { \
if (atGetExceptionHandler()) {atGetExceptionHandler()(__FILE__, AT_PRETTY_FUNCTION, __LINE__, __VA_ARGS__); return ret; \ if (atGetExceptionHandler()) {atGetExceptionHandler()(__FILE__, AT_PRETTY_FUNCTION, __LINE__, __VA_ARGS__); return ret; \
} else { } else {
std::string msg = Athena::utility::sprintf(args, __VA_ARGS__); \ std::string msg = Athena::utility::sprintf(args, __VA_ARGS__);
throw Athena::error::IOException(std::string("IOException: ")+msg, __FILE__, AT_PRETTY_FUNCTION, __LINE__); \
\
throw Athena::error::IOException(std::string("IOException: ") + msg, __FILE__, AT_PRETTY_FUNCTION, __LINE__);
\
} \ } \
} while (0) } while (0)
#elif defined(__GNUC__) #elif defined(__GNUC__)
#define THROW_IO_EXCEPTION_RETURN(ret, args...) \ #define THROW_IO_EXCEPTION_RETURN(ret, args...) \

View File

@ -53,9 +53,12 @@ public:
do { \ do { \
if (atGetExceptionHandler()) {atGetExceptionHandler()(__FILE__, AT_PRETTY_FUNCTION, __LINE__, __VA_ARGS__); return; \ if (atGetExceptionHandler()) {atGetExceptionHandler()(__FILE__, AT_PRETTY_FUNCTION, __LINE__, __VA_ARGS__); return; \
} else { } else {
std::string msg = Athena::utility::sprintf(args, __VA_ARGS__); \ std::string msg = Athena::utility::sprintf(args, __VA_ARGS__);
throw Athena::error::InvalidOperationException(std::string("InvalidOperationException: ")+msg, __FILE__, AT_PRETTY_FUNCTION, __LINE__); \ \
throw Athena::error::InvalidOperationException(std::string("InvalidOperationException: ") + msg, __FILE__, AT_PRETTY_FUNCTION, __LINE__);
\
} \ } \
} while (0) } while (0)
#elif defined (__GNUC__) #elif defined (__GNUC__)
#define THROW_INVALID_OPERATION_EXCEPTION(args...) \ #define THROW_INVALID_OPERATION_EXCEPTION(args...) \
@ -72,9 +75,13 @@ public:
do { \ do { \
if (atGetExceptionHandler()) {atGetExceptionHandler()(__FILE__, AT_PRETTY_FUNCTION, __LINE__, __VA_ARGS__); return ret; \ if (atGetExceptionHandler()) {atGetExceptionHandler()(__FILE__, AT_PRETTY_FUNCTION, __LINE__, __VA_ARGS__); return ret; \
} else { } else {
std::string msg = Athena::utility::sprintf(args, __VA_ARGS__); \ std::string msg = Athena::utility::sprintf(args, __VA_ARGS__);
throw Athena::error::InvalidOperationException(std::string("InvalidOperationException: ")+msg, __FILE__, AT_PRETTY_FUNCTION, __LINE__); \
\
throw Athena::error::InvalidOperationException(std::string("InvalidOperationException: ") + msg, __FILE__, AT_PRETTY_FUNCTION, __LINE__);
\
} \ } \
} while (0) } while (0)
#elif defined(__GNUC__) #elif defined(__GNUC__)
#define THROW_INVALID_OPERATION_EXCEPTION_RETURN(ret, args...) \ #define THROW_INVALID_OPERATION_EXCEPTION_RETURN(ret, args...) \

View File

@ -3,7 +3,8 @@
#include "LZBase.hpp" #include "LZBase.hpp"
class LZType10 : public LZBase { class LZType10 : public LZBase
{
public: public:
explicit LZType10(atInt32 minimumOffset = 1, atInt32 SlidingWindow = 4096, atInt32 MinimumMatch = 3, atInt32 BlockSize = 8); explicit LZType10(atInt32 minimumOffset = 1, atInt32 SlidingWindow = 4096, atInt32 MinimumMatch = 3, atInt32 BlockSize = 8);
atUint32 compress(const atUint8* src, atUint8** dest, atUint32 srcLength); atUint32 compress(const atUint8* src, atUint8** dest, atUint32 srcLength);

View File

@ -4,7 +4,8 @@
#include "LZBase.hpp" #include "LZBase.hpp"
class LZType11 : public LZBase { class LZType11 : public LZBase
{
public: public:
explicit LZType11(atInt32 MinimumOffset = 1, atInt32 SlidingWindow = 4096, atInt32 MinimumMatch = 3, atInt32 BlockSize = 8); explicit LZType11(atInt32 MinimumOffset = 1, atInt32 SlidingWindow = 4096, atInt32 MinimumMatch = 3, atInt32 BlockSize = 8);
atUint32 compress(const atUint8* src, atUint8** dest, atUint32 srcLength); atUint32 compress(const atUint8* src, atUint8** dest, atUint32 srcLength);

View File

@ -34,11 +34,13 @@ DEALINGS IN THE SOFTWARE.
namespace utf8 namespace utf8
{ {
// Base for the exceptions that may be thrown from the library // Base for the exceptions that may be thrown from the library
class exception : public ::std::exception { class exception : public ::std::exception
{
}; };
// Exceptions that may be thrown from the library functions. // Exceptions that may be thrown from the library functions.
class invalid_code_point : public exception { class invalid_code_point : public exception
{
uint32_t cp; uint32_t cp;
public: public:
invalid_code_point(uint32_t cp) : cp(cp) {} invalid_code_point(uint32_t cp) : cp(cp) {}
@ -46,7 +48,8 @@ public:
uint32_t code_point() const {return cp;} uint32_t code_point() const {return cp;}
}; };
class invalid_utf8 : public exception { class invalid_utf8 : public exception
{
uint8_t u8; uint8_t u8;
public: public:
invalid_utf8(uint8_t u) : u8(u) {} invalid_utf8(uint8_t u) : u8(u) {}
@ -54,7 +57,8 @@ public:
uint8_t utf8_octet() const {return u8;} uint8_t utf8_octet() const {return u8;}
}; };
class invalid_utf16 : public exception { class invalid_utf16 : public exception
{
uint16_t u16; uint16_t u16;
public: public:
invalid_utf16(uint16_t u) : u16(u) {} invalid_utf16(uint16_t u) : u16(u) {}
@ -62,7 +66,8 @@ public:
uint16_t utf16_word() const {return u16;} uint16_t utf16_word() const {return u16;}
}; };
class not_enough_room : public exception { class not_enough_room : public exception
{
public: public:
virtual const char* what() const throw() { return "Not enough space"; } virtual const char* what() const throw() { return "Not enough space"; }
}; };
@ -77,52 +82,66 @@ octet_iterator append(uint32_t cp, octet_iterator result)
if (cp < 0x80) // one octet if (cp < 0x80) // one octet
*(result++) = static_cast<uint8_t>(cp); *(result++) = static_cast<uint8_t>(cp);
else if (cp < 0x800) { // two octets else if (cp < 0x800) // two octets
{
*(result++) = static_cast<uint8_t>((cp >> 6) | 0xc0); *(result++) = static_cast<uint8_t>((cp >> 6) | 0xc0);
*(result++) = static_cast<uint8_t>((cp & 0x3f) | 0x80); *(result++) = static_cast<uint8_t>((cp & 0x3f) | 0x80);
} }
else if (cp < 0x10000) { // three octets else if (cp < 0x10000) // three octets
{
*(result++) = static_cast<uint8_t>((cp >> 12) | 0xe0); *(result++) = static_cast<uint8_t>((cp >> 12) | 0xe0);
*(result++) = static_cast<uint8_t>(((cp >> 6) & 0x3f) | 0x80); *(result++) = static_cast<uint8_t>(((cp >> 6) & 0x3f) | 0x80);
*(result++) = static_cast<uint8_t>((cp & 0x3f) | 0x80); *(result++) = static_cast<uint8_t>((cp & 0x3f) | 0x80);
} }
else { // four octets else // four octets
{
*(result++) = static_cast<uint8_t>((cp >> 18) | 0xf0); *(result++) = static_cast<uint8_t>((cp >> 18) | 0xf0);
*(result++) = static_cast<uint8_t>(((cp >> 12) & 0x3f) | 0x80); *(result++) = static_cast<uint8_t>(((cp >> 12) & 0x3f) | 0x80);
*(result++) = static_cast<uint8_t>(((cp >> 6) & 0x3f) | 0x80); *(result++) = static_cast<uint8_t>(((cp >> 6) & 0x3f) | 0x80);
*(result++) = static_cast<uint8_t>((cp & 0x3f) | 0x80); *(result++) = static_cast<uint8_t>((cp & 0x3f) | 0x80);
} }
return result; return result;
} }
template <typename octet_iterator, typename output_iterator> template <typename octet_iterator, typename output_iterator>
output_iterator replace_invalid(octet_iterator start, octet_iterator end, output_iterator out, uint32_t replacement) output_iterator replace_invalid(octet_iterator start, octet_iterator end, output_iterator out, uint32_t replacement)
{ {
while (start != end) { while (start != end)
{
octet_iterator sequence_start = start; octet_iterator sequence_start = start;
internal::utf_error err_code = utf8::internal::validate_next(start, end); internal::utf_error err_code = utf8::internal::validate_next(start, end);
switch (err_code) {
switch (err_code)
{
case internal::UTF8_OK : case internal::UTF8_OK :
for (octet_iterator it = sequence_start; it != start; ++it) for (octet_iterator it = sequence_start; it != start; ++it)
*out++ = *it; *out++ = *it;
break; break;
case internal::NOT_ENOUGH_ROOM: case internal::NOT_ENOUGH_ROOM:
throw not_enough_room(); throw not_enough_room();
case internal::INVALID_LEAD: case internal::INVALID_LEAD:
utf8::append(replacement, out); utf8::append(replacement, out);
++start; ++start;
break; break;
case internal::INCOMPLETE_SEQUENCE: case internal::INCOMPLETE_SEQUENCE:
case internal::OVERLONG_SEQUENCE: case internal::OVERLONG_SEQUENCE:
case internal::INVALID_CODE_POINT: case internal::INVALID_CODE_POINT:
utf8::append(replacement, out); utf8::append(replacement, out);
++start; ++start;
// just one replacement mark for the sequence // just one replacement mark for the sequence
while (start != end && utf8::internal::is_trail(*start)) while (start != end && utf8::internal::is_trail(*start))
++start; ++start;
break; break;
} }
} }
return out; return out;
} }
@ -138,18 +157,24 @@ uint32_t next(octet_iterator& it, octet_iterator end)
{ {
uint32_t cp = 0; uint32_t cp = 0;
internal::utf_error err_code = utf8::internal::validate_next(it, end, cp); internal::utf_error err_code = utf8::internal::validate_next(it, end, cp);
switch (err_code) {
switch (err_code)
{
case internal::UTF8_OK : case internal::UTF8_OK :
break; break;
case internal::NOT_ENOUGH_ROOM : case internal::NOT_ENOUGH_ROOM :
throw not_enough_room(); throw not_enough_room();
case internal::INVALID_LEAD : case internal::INVALID_LEAD :
case internal::INCOMPLETE_SEQUENCE : case internal::INCOMPLETE_SEQUENCE :
case internal::OVERLONG_SEQUENCE : case internal::OVERLONG_SEQUENCE :
throw invalid_utf8(*it); throw invalid_utf8(*it);
case internal::INVALID_CODE_POINT : case internal::INVALID_CODE_POINT :
throw invalid_code_point(cp); throw invalid_code_point(cp);
} }
return cp; return cp;
} }
@ -167,10 +192,12 @@ uint32_t prior(octet_iterator& it, octet_iterator start)
throw not_enough_room(); throw not_enough_room();
octet_iterator end = it; octet_iterator end = it;
// Go back until we hit either a lead octet or start // Go back until we hit either a lead octet or start
while (utf8::internal::is_trail(*(--it))) while (utf8::internal::is_trail(*(--it)))
if (it == start) if (it == start)
throw invalid_utf8(*it); // error - no lead byte in the sequence throw invalid_utf8(*it); // error - no lead byte in the sequence
return utf8::peek_next(it, end); return utf8::peek_next(it, end);
} }
@ -179,9 +206,11 @@ template <typename octet_iterator>
uint32_t previous(octet_iterator& it, octet_iterator pass_start) uint32_t previous(octet_iterator& it, octet_iterator pass_start)
{ {
octet_iterator end = it; octet_iterator end = it;
while (utf8::internal::is_trail(*(--it))) while (utf8::internal::is_trail(*(--it)))
if (it == pass_start) if (it == pass_start)
throw invalid_utf8(*it); // error - no lead byte in the sequence throw invalid_utf8(*it); // error - no lead byte in the sequence
octet_iterator temp = it; octet_iterator temp = it;
return utf8::next(temp, end); return utf8::next(temp, end);
} }
@ -198,20 +227,27 @@ typename std::iterator_traits<octet_iterator>::difference_type
distance(octet_iterator first, octet_iterator last) distance(octet_iterator first, octet_iterator last)
{ {
typename std::iterator_traits<octet_iterator>::difference_type dist; typename std::iterator_traits<octet_iterator>::difference_type dist;
for (dist = 0; first < last; ++dist) for (dist = 0; first < last; ++dist)
utf8::next(first, last); utf8::next(first, last);
return dist; return dist;
} }
template <typename u16bit_iterator, typename octet_iterator> template <typename u16bit_iterator, typename octet_iterator>
octet_iterator utf16to8(u16bit_iterator start, u16bit_iterator end, octet_iterator result) octet_iterator utf16to8(u16bit_iterator start, u16bit_iterator end, octet_iterator result)
{ {
while (start != end) { while (start != end)
{
uint32_t cp = utf8::internal::mask16(*start++); uint32_t cp = utf8::internal::mask16(*start++);
// Take care of surrogate pairs first // Take care of surrogate pairs first
if (utf8::internal::is_lead_surrogate(cp)) { if (utf8::internal::is_lead_surrogate(cp))
if (start != end) { {
if (start != end)
{
uint32_t trail_surrogate = utf8::internal::mask16(*start++); uint32_t trail_surrogate = utf8::internal::mask16(*start++);
if (utf8::internal::is_trail_surrogate(trail_surrogate)) if (utf8::internal::is_trail_surrogate(trail_surrogate))
cp = (cp << 10) + trail_surrogate + internal::SURROGATE_OFFSET; cp = (cp << 10) + trail_surrogate + internal::SURROGATE_OFFSET;
else else
@ -227,21 +263,26 @@ octet_iterator utf16to8 (u16bit_iterator start, u16bit_iterator end, octet_itera
result = utf8::append(cp, result); result = utf8::append(cp, result);
} }
return result; return result;
} }
template <typename u16bit_iterator, typename octet_iterator> template <typename u16bit_iterator, typename octet_iterator>
u16bit_iterator utf8to16(octet_iterator start, octet_iterator end, u16bit_iterator result) u16bit_iterator utf8to16(octet_iterator start, octet_iterator end, u16bit_iterator result)
{ {
while (start != end) { while (start != end)
{
uint32_t cp = utf8::next(start, end); uint32_t cp = utf8::next(start, end);
if (cp > 0xffff) { //make a surrogate pair
if (cp > 0xffff) //make a surrogate pair
{
*result++ = static_cast<uint16_t>((cp >> 10) + internal::LEAD_OFFSET); *result++ = static_cast<uint16_t>((cp >> 10) + internal::LEAD_OFFSET);
*result++ = static_cast<uint16_t>((cp & 0x3ff) + internal::TRAIL_SURROGATE_MIN); *result++ = static_cast<uint16_t>((cp & 0x3ff) + internal::TRAIL_SURROGATE_MIN);
} }
else else
*result++ = static_cast<uint16_t>(cp); *result++ = static_cast<uint16_t>(cp);
} }
return result; return result;
} }
@ -265,7 +306,8 @@ u32bit_iterator utf8to32 (octet_iterator start, octet_iterator end, u32bit_itera
// The iterator class // The iterator class
template <typename octet_iterator> template <typename octet_iterator>
class iterator : public std::iterator <std::bidirectional_iterator_tag, uint32_t> { class iterator : public std::iterator <std::bidirectional_iterator_tag, uint32_t>
{
octet_iterator it; octet_iterator it;
octet_iterator range_start; octet_iterator range_start;
octet_iterator range_end; octet_iterator range_end;
@ -290,6 +332,7 @@ public:
{ {
if (range_start != rhs.range_start || range_end != rhs.range_end) if (range_start != rhs.range_start || range_end != rhs.range_end)
throw std::logic_error("Comparing utf-8 iterators defined with different ranges"); throw std::logic_error("Comparing utf-8 iterators defined with different ranges");
return (it == rhs.it); return (it == rhs.it);
} }
bool operator != (const iterator& rhs) const bool operator != (const iterator& rhs) const

View File

@ -100,6 +100,7 @@ inline typename std::iterator_traits<octet_iterator>::difference_type
sequence_length(octet_iterator lead_it) sequence_length(octet_iterator lead_it)
{ {
uint8_t lead = utf8::internal::mask8(*lead_it); uint8_t lead = utf8::internal::mask8(*lead_it);
if (lead < 0x80) if (lead < 0x80)
return 1; return 1;
else if ((lead >> 5) == 0x6) else if ((lead >> 5) == 0x6)
@ -115,15 +116,18 @@ sequence_length(octet_iterator lead_it)
template <typename octet_difference_type> template <typename octet_difference_type>
inline bool is_overlong_sequence(uint32_t cp, octet_difference_type length) inline bool is_overlong_sequence(uint32_t cp, octet_difference_type length)
{ {
if (cp < 0x80) { if (cp < 0x80)
{
if (length != 1) if (length != 1)
return true; return true;
} }
else if (cp < 0x800) { else if (cp < 0x800)
{
if (length != 2) if (length != 2)
return true; return true;
} }
else if (cp < 0x10000) { else if (cp < 0x10000)
{
if (length != 3) if (length != 3)
return true; return true;
} }
@ -233,27 +237,36 @@ utf_error validate_next(octet_iterator& it, octet_iterator end, uint32_t& code_p
// Get trail octets and calculate the code point // Get trail octets and calculate the code point
utf_error err = UTF8_OK; utf_error err = UTF8_OK;
switch (length) {
switch (length)
{
case 0: case 0:
return INVALID_LEAD; return INVALID_LEAD;
case 1: case 1:
err = utf8::internal::get_sequence_1(it, end, cp); err = utf8::internal::get_sequence_1(it, end, cp);
break; break;
case 2: case 2:
err = utf8::internal::get_sequence_2(it, end, cp); err = utf8::internal::get_sequence_2(it, end, cp);
break; break;
case 3: case 3:
err = utf8::internal::get_sequence_3(it, end, cp); err = utf8::internal::get_sequence_3(it, end, cp);
break; break;
case 4: case 4:
err = utf8::internal::get_sequence_4(it, end, cp); err = utf8::internal::get_sequence_4(it, end, cp);
break; break;
} }
if (err == UTF8_OK) { if (err == UTF8_OK)
{
// Decoding succeeded. Now, security checks... // Decoding succeeded. Now, security checks...
if (utf8::internal::is_code_point_valid(cp)) { if (utf8::internal::is_code_point_valid(cp))
if (!utf8::internal::is_overlong_sequence(cp, length)){ {
if (!utf8::internal::is_overlong_sequence(cp, length))
{
// Passed! Return here. // Passed! Return here.
code_point = cp; code_point = cp;
++it; ++it;
@ -272,7 +285,8 @@ utf_error validate_next(octet_iterator& it, octet_iterator end, uint32_t& code_p
} }
template <typename octet_iterator> template <typename octet_iterator>
inline utf_error validate_next(octet_iterator& it, octet_iterator end) { inline utf_error validate_next(octet_iterator& it, octet_iterator end)
{
uint32_t ignored; uint32_t ignored;
return utf8::internal::validate_next(it, end, ignored); return utf8::internal::validate_next(it, end, ignored);
} }
@ -288,11 +302,15 @@ template <typename octet_iterator>
octet_iterator find_invalid(octet_iterator start, octet_iterator end) octet_iterator find_invalid(octet_iterator start, octet_iterator end)
{ {
octet_iterator result = start; octet_iterator result = start;
while (result != end) {
while (result != end)
{
utf8::internal::utf_error err_code = utf8::internal::validate_next(result, end); utf8::internal::utf_error err_code = utf8::internal::validate_next(result, end);
if (err_code != internal::UTF8_OK) if (err_code != internal::UTF8_OK)
return result; return result;
} }
return result; return result;
} }

View File

@ -39,21 +39,25 @@ octet_iterator append(uint32_t cp, octet_iterator result)
{ {
if (cp < 0x80) // one octet if (cp < 0x80) // one octet
*(result++) = static_cast<uint8_t>(cp); *(result++) = static_cast<uint8_t>(cp);
else if (cp < 0x800) { // two octets else if (cp < 0x800) // two octets
{
*(result++) = static_cast<uint8_t>((cp >> 6) | 0xc0); *(result++) = static_cast<uint8_t>((cp >> 6) | 0xc0);
*(result++) = static_cast<uint8_t>((cp & 0x3f) | 0x80); *(result++) = static_cast<uint8_t>((cp & 0x3f) | 0x80);
} }
else if (cp < 0x10000) { // three octets else if (cp < 0x10000) // three octets
{
*(result++) = static_cast<uint8_t>((cp >> 12) | 0xe0); *(result++) = static_cast<uint8_t>((cp >> 12) | 0xe0);
*(result++) = static_cast<uint8_t>(((cp >> 6) & 0x3f) | 0x80); *(result++) = static_cast<uint8_t>(((cp >> 6) & 0x3f) | 0x80);
*(result++) = static_cast<uint8_t>((cp & 0x3f) | 0x80); *(result++) = static_cast<uint8_t>((cp & 0x3f) | 0x80);
} }
else { // four octets else // four octets
{
*(result++) = static_cast<uint8_t>((cp >> 18) | 0xf0); *(result++) = static_cast<uint8_t>((cp >> 18) | 0xf0);
*(result++) = static_cast<uint8_t>(((cp >> 12) & 0x3f) | 0x80); *(result++) = static_cast<uint8_t>(((cp >> 12) & 0x3f) | 0x80);
*(result++) = static_cast<uint8_t>(((cp >> 6) & 0x3f) | 0x80); *(result++) = static_cast<uint8_t>(((cp >> 6) & 0x3f) | 0x80);
*(result++) = static_cast<uint8_t>((cp & 0x3f) | 0x80); *(result++) = static_cast<uint8_t>((cp & 0x3f) | 0x80);
} }
return result; return result;
} }
@ -62,19 +66,24 @@ uint32_t next(octet_iterator& it)
{ {
uint32_t cp = utf8::internal::mask8(*it); uint32_t cp = utf8::internal::mask8(*it);
typename std::iterator_traits<octet_iterator>::difference_type length = utf8::internal::sequence_length(it); typename std::iterator_traits<octet_iterator>::difference_type length = utf8::internal::sequence_length(it);
switch (length) {
switch (length)
{
case 1: case 1:
break; break;
case 2: case 2:
it++; it++;
cp = ((cp << 6) & 0x7ff) + ((*it) & 0x3f); cp = ((cp << 6) & 0x7ff) + ((*it) & 0x3f);
break; break;
case 3: case 3:
++it; ++it;
cp = ((cp << 12) & 0xffff) + ((utf8::internal::mask8(*it) << 6) & 0xfff); cp = ((cp << 12) & 0xffff) + ((utf8::internal::mask8(*it) << 6) & 0xfff);
++it; ++it;
cp += (*it) & 0x3f; cp += (*it) & 0x3f;
break; break;
case 4: case 4:
++it; ++it;
cp = ((cp << 18) & 0x1fffff) + ((utf8::internal::mask8(*it) << 12) & 0x3ffff); cp = ((cp << 18) & 0x1fffff) + ((utf8::internal::mask8(*it) << 12) & 0x3ffff);
@ -84,6 +93,7 @@ uint32_t next(octet_iterator& it)
cp += (*it) & 0x3f; cp += (*it) & 0x3f;
break; break;
} }
++it; ++it;
return cp; return cp;
} }
@ -98,6 +108,7 @@ template <typename octet_iterator>
uint32_t prior(octet_iterator& it) uint32_t prior(octet_iterator& it)
{ {
while (utf8::internal::is_trail(*(--it))) ; while (utf8::internal::is_trail(*(--it))) ;
octet_iterator temp = it; octet_iterator temp = it;
return utf8::unchecked::next(temp); return utf8::unchecked::next(temp);
} }
@ -121,38 +132,49 @@ typename std::iterator_traits<octet_iterator>::difference_type
distance(octet_iterator first, octet_iterator last) distance(octet_iterator first, octet_iterator last)
{ {
typename std::iterator_traits<octet_iterator>::difference_type dist; typename std::iterator_traits<octet_iterator>::difference_type dist;
for (dist = 0; first < last; ++dist) for (dist = 0; first < last; ++dist)
utf8::unchecked::next(first); utf8::unchecked::next(first);
return dist; return dist;
} }
template <typename u16bit_iterator, typename octet_iterator> template <typename u16bit_iterator, typename octet_iterator>
octet_iterator utf16to8(u16bit_iterator start, u16bit_iterator end, octet_iterator result) octet_iterator utf16to8(u16bit_iterator start, u16bit_iterator end, octet_iterator result)
{ {
while (start != end) { while (start != end)
{
uint32_t cp = utf8::internal::mask16(*start++); uint32_t cp = utf8::internal::mask16(*start++);
// Take care of surrogate pairs first // Take care of surrogate pairs first
if (utf8::internal::is_lead_surrogate(cp)) { if (utf8::internal::is_lead_surrogate(cp))
{
uint32_t trail_surrogate = utf8::internal::mask16(*start++); uint32_t trail_surrogate = utf8::internal::mask16(*start++);
cp = (cp << 10) + trail_surrogate + internal::SURROGATE_OFFSET; cp = (cp << 10) + trail_surrogate + internal::SURROGATE_OFFSET;
} }
result = utf8::unchecked::append(cp, result); result = utf8::unchecked::append(cp, result);
} }
return result; return result;
} }
template <typename u16bit_iterator, typename octet_iterator> template <typename u16bit_iterator, typename octet_iterator>
u16bit_iterator utf8to16(octet_iterator start, octet_iterator end, u16bit_iterator result) u16bit_iterator utf8to16(octet_iterator start, octet_iterator end, u16bit_iterator result)
{ {
while (start < end) { while (start < end)
{
uint32_t cp = utf8::unchecked::next(start); uint32_t cp = utf8::unchecked::next(start);
if (cp > 0xffff) { //make a surrogate pair
if (cp > 0xffff) //make a surrogate pair
{
*result++ = static_cast<uint16_t>((cp >> 10) + internal::LEAD_OFFSET); *result++ = static_cast<uint16_t>((cp >> 10) + internal::LEAD_OFFSET);
*result++ = static_cast<uint16_t>((cp & 0x3ff) + internal::TRAIL_SURROGATE_MIN); *result++ = static_cast<uint16_t>((cp & 0x3ff) + internal::TRAIL_SURROGATE_MIN);
} }
else else
*result++ = static_cast<uint16_t>(cp); *result++ = static_cast<uint16_t>(cp);
} }
return result; return result;
} }
@ -176,7 +198,8 @@ u32bit_iterator utf8to32 (octet_iterator start, octet_iterator end, u32bit_itera
// The iterator class // The iterator class
template <typename octet_iterator> template <typename octet_iterator>
class iterator : public std::iterator <std::bidirectional_iterator_tag, uint32_t> { class iterator : public std::iterator <std::bidirectional_iterator_tag, uint32_t>
{
octet_iterator it; octet_iterator it;
public: public:
iterator() {}; iterator() {};

View File

@ -53,13 +53,16 @@ ALTTPFile* ALTTPFileReader::readFile()
std::vector<atUint16> dungeonDeaths; std::vector<atUint16> dungeonDeaths;
int j = 0x140; int j = 0x140;
while ((j--) > 0) while ((j--) > 0)
{ {
roomFlags.push_back(readRoomFlags()); roomFlags.push_back(readRoomFlags());
} }
quest->setRoomFlags(roomFlags); quest->setRoomFlags(roomFlags);
j = 0x0C0; j = 0x0C0;
while ((j--) > 0) while ((j--) > 0)
owEvents.push_back(readOverworldEvent()); owEvents.push_back(readOverworldEvent());
@ -108,6 +111,7 @@ ALTTPFile* ALTTPFileReader::readFile()
quest->setMagicUsage((ALTTPMagicUsage&)*base::readBytes(sizeof(ALTTPMagicUsage))); quest->setMagicUsage((ALTTPMagicUsage&)*base::readBytes(sizeof(ALTTPMagicUsage)));
j = 0x10; j = 0x10;
while ((j--) > 0) while ((j--) > 0)
{ {
dungeonKeys.push_back(base::readByte()); dungeonKeys.push_back(base::readByte());
@ -125,6 +129,7 @@ ALTTPFile* ALTTPFileReader::readFile()
quest->setTagAlong((ALTTPTagAlong)base::readByte()); quest->setTagAlong((ALTTPTagAlong)base::readByte());
j = 6; j = 6;
while ((j--) > 0) while ((j--) > 0)
{ {
oldmanFlags.push_back(base::readByte()); oldmanFlags.push_back(base::readByte());
@ -134,6 +139,7 @@ ALTTPFile* ALTTPFileReader::readFile()
quest->setBombFlag(base::readByte()); quest->setBombFlag(base::readByte());
j = 5; j = 5;
while ((j--) > 0) while ((j--) > 0)
{ {
unknown1.push_back(base::readByte()); unknown1.push_back(base::readByte());
@ -142,6 +148,7 @@ ALTTPFile* ALTTPFileReader::readFile()
quest->setUnknown1(unknown1); quest->setUnknown1(unknown1);
j = 6; j = 6;
while ((j--) > 0) while ((j--) > 0)
{ {
playerName.push_back(base::readUint16()); playerName.push_back(base::readUint16());
@ -151,10 +158,12 @@ ALTTPFile* ALTTPFileReader::readFile()
quest->setValid((base::readUint16() == 0x55AA)); quest->setValid((base::readUint16() == 0x55AA));
j = 0x0D; j = 0x0D;
while ((j--) > 0) while ((j--) > 0)
{ {
dungeonDeaths.push_back(base::readUint16()); dungeonDeaths.push_back(base::readUint16());
} }
quest->setDungeonDeathTotals(dungeonDeaths); quest->setDungeonDeathTotals(dungeonDeaths);
quest->setUnknown2(base::readUint16()); quest->setUnknown2(base::readUint16());

View File

@ -38,6 +38,7 @@ ALTTPFileWriter::ALTTPFileWriter(const std::string& filename)
void ALTTPFileWriter::writeFile(ALTTPFile* file) void ALTTPFileWriter::writeFile(ALTTPFile* file)
{ {
ALTTPQuest* quest = NULL; ALTTPQuest* quest = NULL;
for (atUint32 i = 0; i < 6; i++) for (atUint32 i = 0; i < 6; i++)
{ {
if (i < 3) if (i < 3)
@ -204,6 +205,7 @@ atUint16 ALTTPFileWriter::calculateChecksum(atUint32 game)
// First we start at 0 // First we start at 0
atUint16 sum = 0; atUint16 sum = 0;
for (atUint32 i = 0; i < 0x4FE; i += 2) for (atUint32 i = 0; i < 0x4FE; i += 2)
// Add each word one by one // Add each word one by one
sum += *(atUint16*)(m_data + (i + (0x500 * game))); sum += *(atUint16*)(m_data + (i + (0x500 * game)));

View File

@ -73,6 +73,7 @@ ALTTPOverworldEvent* ALTTPQuest::overworldEvent(atUint32 id) const
{ {
if (id > m_overworldEvents.size() - 1) if (id > m_overworldEvents.size() - 1)
THROW_INVALID_OPERATION_EXCEPTION_RETURN(nullptr, "index out of range"); THROW_INVALID_OPERATION_EXCEPTION_RETURN(nullptr, "index out of range");
return m_overworldEvents[id]; return m_overworldEvents[id];
} }
@ -472,25 +473,30 @@ void ALTTPQuest::setPlayerName(const std::string& playerName)
m_playerName.push_back((atUint16)0xA9); m_playerName.push_back((atUint16)0xA9);
continue; continue;
} }
char c = playerName[i]; char c = playerName[i];
if (c >= 'A' && c <= 'P' && c != 'I') if (c >= 'A' && c <= 'P' && c != 'I')
{ {
m_playerName.push_back((atUint16)(c - 'A')); m_playerName.push_back((atUint16)(c - 'A'));
continue; continue;
} }
if (c >= 'Q' && c <= 'Z') if (c >= 'Q' && c <= 'Z')
{ {
std::cout << std::hex << (atUint16)((c - 'Q') + 0x20) << std::endl; std::cout << std::hex << (atUint16)((c - 'Q') + 0x20) << std::endl;
m_playerName.push_back((atUint16)((c - 'Q') + 0x20)); m_playerName.push_back((atUint16)((c - 'Q') + 0x20));
continue; continue;
} }
if (c >= 'a' && c <= 'f') if (c >= 'a' && c <= 'f')
{ {
std::cout << std::hex << (atUint16)((c - 'a') + 0x2A) << std::endl; std::cout << std::hex << (atUint16)((c - 'a') + 0x2A) << std::endl;
m_playerName.push_back((atUint16)((c - 'a') + 0x2A)); m_playerName.push_back((atUint16)((c - 'a') + 0x2A));
continue; continue;
} }
if (c >= 'g' && c <= 'v') if (c >= 'g' && c <= 'v')
{ {
if (c == 'k') if (c == 'k')
@ -498,24 +504,29 @@ void ALTTPQuest::setPlayerName(const std::string& playerName)
m_playerName.push_back(0x42); m_playerName.push_back(0x42);
continue; continue;
} }
if (c == 'i') if (c == 'i')
{ {
m_playerName.push_back(0x44); m_playerName.push_back(0x44);
continue; continue;
} }
m_playerName.push_back((atUint16)((c - 'g') + 0x40)); m_playerName.push_back((atUint16)((c - 'g') + 0x40));
continue; continue;
} }
if (c >= 'w' && c <= 'z') if (c >= 'w' && c <= 'z')
{ {
m_playerName.push_back((atUint16)((c - 'w') + 0x60)); m_playerName.push_back((atUint16)((c - 'w') + 0x60));
continue; continue;
} }
if (c >= '0' && c <= '9') if (c >= '0' && c <= '9')
{ {
m_playerName.push_back((atUint16)((c - '0') + 0x64)); m_playerName.push_back((atUint16)((c - '0') + 0x64));
continue; continue;
} }
if (c == '-' || c == '.') if (c == '-' || c == '.')
{ {
m_playerName.push_back((atUint16)(c - '-') + 0x80); m_playerName.push_back((atUint16)(c - '-') + 0x80);
@ -524,12 +535,29 @@ void ALTTPQuest::setPlayerName(const std::string& playerName)
switch (c) switch (c)
{ {
case '?' : m_playerName.push_back(0x6E); break; case '?' :
case '!' : m_playerName.push_back(0x6F); break; m_playerName.push_back(0x6E);
case ',' : m_playerName.push_back(0x82); break; break;
case '(' : m_playerName.push_back(0x85); break;
case ')' : m_playerName.push_back(0x86); break; case '!' :
case 'I' : m_playerName.push_back(0xAF); break; m_playerName.push_back(0x6F);
break;
case ',' :
m_playerName.push_back(0x82);
break;
case '(' :
m_playerName.push_back(0x85);
break;
case ')' :
m_playerName.push_back(0x86);
break;
case 'I' :
m_playerName.push_back(0xAF);
break;
} }
} }
} }
@ -550,16 +578,19 @@ std::string ALTTPQuest::playerNameToString() const
ret.push_back((char)('A' + c)); ret.push_back((char)('A' + c));
continue; continue;
} }
if (c >= 0x20 && c <= 0x29) if (c >= 0x20 && c <= 0x29)
{ {
ret.push_back((char)('Q' + (c - 0x20))); ret.push_back((char)('Q' + (c - 0x20)));
continue; continue;
} }
if (c >= 0x2A && c <= 0x2F) if (c >= 0x2A && c <= 0x2F)
{ {
ret.push_back((char)('a' + (c - 0x2A))); ret.push_back((char)('a' + (c - 0x2A)));
continue; continue;
} }
if (c >= 0x40 && c <= 0x4F) if (c >= 0x40 && c <= 0x4F)
{ {
if (c == 0x42) if (c == 0x42)
@ -567,23 +598,28 @@ std::string ALTTPQuest::playerNameToString() const
ret.push_back('k'); ret.push_back('k');
continue; continue;
} }
if (c == 0x44) if (c == 0x44)
{ {
ret.push_back('i'); ret.push_back('i');
continue; continue;
} }
ret.push_back((char)('g' + (c - 0x40))); ret.push_back((char)('g' + (c - 0x40)));
} }
if (c >= 0x60 && c <= 0x63) if (c >= 0x60 && c <= 0x63)
{ {
ret.push_back((char)('w' + (c - 0x60))); ret.push_back((char)('w' + (c - 0x60)));
continue; continue;
} }
if (c >= 0x64 && c <= 0x6D) if (c >= 0x64 && c <= 0x6D)
{ {
ret.push_back((char)('0' + (c - 0x64))); ret.push_back((char)('0' + (c - 0x64)));
continue; continue;
} }
if (c == 0x80 || c == 0x81) if (c == 0x80 || c == 0x81)
{ {
ret.push_back((char)('-' + (c - 0x80))); ret.push_back((char)('-' + (c - 0x80)));
@ -592,14 +628,32 @@ std::string ALTTPQuest::playerNameToString() const
switch (c) switch (c)
{ {
case 0x6E: ret.push_back('?'); break; case 0x6E:
case 0x6F: ret.push_back('!'); break; ret.push_back('?');
case 0x82: ret.push_back(','); break; break;
case 0x85: ret.push_back('('); break;
case 0x86: ret.push_back(')'); break; case 0x6F:
case 0xAF: ret.push_back('I'); break; ret.push_back('!');
break;
case 0x82:
ret.push_back(',');
break;
case 0x85:
ret.push_back('(');
break;
case 0x86:
ret.push_back(')');
break;
case 0xAF:
ret.push_back('I');
break;
} }
} }
return ret; return ret;
} }

View File

@ -157,8 +157,10 @@ atUint16 crc16(const atUint8* data, atUint64 length)
atInt32 pos = 0; atInt32 pos = 0;
atUint16 checksum = 0; atUint16 checksum = 0;
while (length--) while (length--)
checksum = (crc16Table[(checksum ^ data[pos++]) & 0xFF] ^ (checksum >> 8)); checksum = (crc16Table[(checksum ^ data[pos++]) & 0xFF] ^ (checksum >> 8));
return checksum; return checksum;
} }

View File

@ -44,9 +44,11 @@ atInt32 decompressZlib(const atUint8* src, atUint32 srcLen, atUint8* dst, atUint
atInt32 ret = -1; atInt32 ret = -1;
err = inflateInit(&strm); //15 window bits, and the +32 tells zlib to to detect if using gzip or zlib err = inflateInit(&strm); //15 window bits, and the +32 tells zlib to to detect if using gzip or zlib
if (err == Z_OK) if (err == Z_OK)
{ {
err = inflate(&strm, Z_FINISH); err = inflate(&strm, Z_FINISH);
if (err == Z_STREAM_END) if (err == Z_STREAM_END)
ret = strm.total_out; ret = strm.total_out;
else else
@ -86,6 +88,7 @@ atInt32 compressZlib(const atUint8 *src, atUint32 srcLen, atUint8* dst, atUint32
if (err == Z_OK) if (err == Z_OK)
{ {
err = deflate(&strm, Z_FINISH); err = deflate(&strm, Z_FINISH);
if (err == Z_STREAM_END) if (err == Z_STREAM_END)
ret = strm.total_out; ret = strm.total_out;
else else
@ -124,6 +127,7 @@ atUint32 yaz0Decode(const atUint8* src, atUint8* dst, atUint32 uncompressedSize)
atInt32 validBitCount = 0; //number of valid bits left in "code" byte atInt32 validBitCount = 0; //number of valid bits left in "code" byte
atUint8 currCodeByte; atUint8 currCodeByte;
while (dstPlace < uncompressedSize) while (dstPlace < uncompressedSize)
{ {
//read new "code" byte if the current one is used up //read new "code" byte if the current one is used up
@ -152,6 +156,7 @@ atUint32 yaz0Decode(const atUint8* src, atUint8* dst, atUint32 uncompressedSize)
atUint32 copySource = dstPlace - (dist + 1); atUint32 copySource = dstPlace - (dist + 1);
atUint32 numBytes = byte1 >> 4; atUint32 numBytes = byte1 >> 4;
if (numBytes == 0) if (numBytes == 0)
{ {
numBytes = src[srcPlace] + 0x12; numBytes = src[srcPlace] + 0x12;
@ -196,11 +201,13 @@ atUint32 yaz0Encode(const atUint8* src, atUint32 srcSize, atUint8* data)
atUint32 validBitCount = 0; //number of valid bits left in "code" byte atUint32 validBitCount = 0; //number of valid bits left in "code" byte
atUint8 currCodeByte = 0; atUint8 currCodeByte = 0;
while (r.srcPos < srcSize) while (r.srcPos < srcSize)
{ {
atUint32 numBytes; atUint32 numBytes;
atUint32 matchPos; atUint32 matchPos;
numBytes = nintendoEnc(src, srcSize, r.srcPos, &matchPos); numBytes = nintendoEnc(src, srcSize, r.srcPos, &matchPos);
if (numBytes < 3) if (numBytes < 3)
{ {
//straight copy //straight copy
@ -222,9 +229,11 @@ atUint32 yaz0Encode(const atUint8* src, atUint32 srcSize, atUint8* data)
byte2 = dist & 0xff; byte2 = dist & 0xff;
dst[r.dstPos++] = byte1; dst[r.dstPos++] = byte1;
dst[r.dstPos++] = byte2; dst[r.dstPos++] = byte2;
// maximum runlength for 3 byte encoding // maximum runlength for 3 byte encoding
if (numBytes > 0xff + 0x12) if (numBytes > 0xff + 0x12)
numBytes = 0xff + 0x12; numBytes = 0xff + 0x12;
byte3 = numBytes - 0x12; byte3 = numBytes - 0x12;
dst[r.dstPos++] = byte3; dst[r.dstPos++] = byte3;
} }
@ -235,16 +244,21 @@ atUint32 yaz0Encode(const atUint8* src, atUint32 srcSize, atUint8* data)
dst[r.dstPos++] = byte1; dst[r.dstPos++] = byte1;
dst[r.dstPos++] = byte2; dst[r.dstPos++] = byte2;
} }
r.srcPos += numBytes; r.srcPos += numBytes;
} }
validBitCount++; validBitCount++;
//write eight codes //write eight codes
if (validBitCount == 8) if (validBitCount == 8)
{ {
data[pos] = currCodeByte; data[pos] = currCodeByte;
pos++; pos++;
for (i = 0; i </*=*/r.dstPos; pos++, i++) for (i = 0; i </*=*/r.dstPos; pos++, i++)
data[pos] = dst[i]; data[pos] = dst[i];
dstSize += r.dstPos + 1; dstSize += r.dstPos + 1;
currCodeByte = 0; currCodeByte = 0;
@ -252,12 +266,15 @@ atUint32 yaz0Encode(const atUint8* src, atUint32 srcSize, atUint8* data)
r.dstPos = 0; r.dstPos = 0;
} }
} }
if (validBitCount > 0) if (validBitCount > 0)
{ {
data[pos] = currCodeByte; data[pos] = currCodeByte;
pos++; pos++;
for (i = 0; i </*=*/r.dstPos; pos++, i++) for (i = 0; i </*=*/r.dstPos; pos++, i++)
data[pos] = dst[i]; data[pos] = dst[i];
dstSize += r.dstPos + 1; dstSize += r.dstPos + 1;
currCodeByte = 0; currCodeByte = 0;
@ -278,25 +295,31 @@ atUint32 nintendoEnc(const atUint8* src, atInt32 size, atInt32 pos, atUint32 *pM
// if prevFlag is set, it means that the previous position was determined by look-ahead try. // if prevFlag is set, it means that the previous position was determined by look-ahead try.
// so just use it. this is not the best optimization, but nintendo's choice for speed. // so just use it. this is not the best optimization, but nintendo's choice for speed.
if (prevFlag == 1) { if (prevFlag == 1)
{
*pMatchPos = matchPos; *pMatchPos = matchPos;
prevFlag = 0; prevFlag = 0;
return numBytes1; return numBytes1;
} }
prevFlag = 0; prevFlag = 0;
numBytes = simpleEnc(src, size, pos, &matchPos); numBytes = simpleEnc(src, size, pos, &matchPos);
*pMatchPos = matchPos; *pMatchPos = matchPos;
// if this position is RLE encoded, then compare to copying 1 byte and next position(pos+1) encoding // if this position is RLE encoded, then compare to copying 1 byte and next position(pos+1) encoding
if (numBytes >= 3) { if (numBytes >= 3)
{
numBytes1 = simpleEnc(src, size, pos + 1, &matchPos); numBytes1 = simpleEnc(src, size, pos + 1, &matchPos);
// if the next position encoding is +2 longer than current position, choose it. // if the next position encoding is +2 longer than current position, choose it.
// this does not guarantee the best optimization, but fairly good optimization with speed. // this does not guarantee the best optimization, but fairly good optimization with speed.
if (numBytes1 >= numBytes+2) { if (numBytes1 >= numBytes + 2)
{
numBytes = 1; numBytes = 1;
prevFlag = 1; prevFlag = 1;
} }
} }
return numBytes; return numBytes;
} }
@ -309,6 +332,7 @@ atUint32 simpleEnc(const atUint8* src, atInt32 size, atInt32 pos, atUint32 *pMat
if (startPos < 0) if (startPos < 0)
startPos = 0; startPos = 0;
for (i = startPos; i < pos; i++) for (i = startPos; i < pos; i++)
{ {
for (j = 0; j < size - pos; j++) for (j = 0; j < size - pos; j++)
@ -316,21 +340,26 @@ atUint32 simpleEnc(const atUint8* src, atInt32 size, atInt32 pos, atUint32 *pMat
if (src[i + j] != src[j + pos]) if (src[i + j] != src[j + pos])
break; break;
} }
if ((atUint32)j > numBytes) if ((atUint32)j > numBytes)
{ {
numBytes = j; numBytes = j;
matchPos = i; matchPos = i;
} }
} }
*pMatchPos = matchPos; *pMatchPos = matchPos;
if (numBytes == 2) if (numBytes == 2)
numBytes = 1; numBytes = 1;
return numBytes; return numBytes;
} }
atUint32 decompressLZ77(const atUint8* src, atUint32 srcLen, atUint8** dst) atUint32 decompressLZ77(const atUint8* src, atUint32 srcLen, atUint8** dst)
{ {
LZBase* lzCodec; LZBase* lzCodec;
if (*(atUint8*)src == 0x11) if (*(atUint8*)src == 0x11)
lzCodec = new LZType11; lzCodec = new LZType11;
else else
@ -345,6 +374,7 @@ atUint32 decompressLZ77(const atUint8* src, atUint32 srcLen, atUint8** dst)
atUint32 compressLZ77(const atUint8* src, atUint32 srcLen, atUint8** dst, bool extended) atUint32 compressLZ77(const atUint8* src, atUint32 srcLen, atUint8** dst, bool extended)
{ {
LZBase* lzCodec; LZBase* lzCodec;
if (extended) if (extended)
lzCodec = new LZType11; lzCodec = new LZType11;
else else

View File

@ -72,6 +72,7 @@ bool FileReader::isLittleEndian() const
void FileReader::open() void FileReader::open()
{ {
m_fileHandle = fopen(m_filename.c_str(), "rb"); m_fileHandle = fopen(m_filename.c_str(), "rb");
if (!m_fileHandle) if (!m_fileHandle)
THROW_FILE_NOT_FOUND_EXCEPTION(m_filename); THROW_FILE_NOT_FOUND_EXCEPTION(m_filename);
@ -143,6 +144,7 @@ bool FileReader::readBit()
if (!m_bitValid) if (!m_bitValid)
{ {
size_t size = fread(&m_currentByte, 1, 1, m_fileHandle); size_t size = fread(&m_currentByte, 1, 1, m_fileHandle);
if (size != sizeof(atUint8)) if (size != sizeof(atUint8))
THROW_IO_EXCEPTION_RETURN(false, "Error reading from file."); THROW_IO_EXCEPTION_RETURN(false, "Error reading from file.");
@ -152,6 +154,7 @@ bool FileReader::readBit()
atUint8 flag = (1 << m_bitShift); atUint8 flag = (1 << m_bitShift);
m_bitShift++; m_bitShift++;
if (m_bitShift > 7) if (m_bitShift > 7)
m_bitValid = false; m_bitValid = false;
@ -173,6 +176,7 @@ atInt8 FileReader::readByte()
{ {
if (!isOpen()) if (!isOpen())
THROW_INVALID_OPERATION_EXCEPTION_RETURN(0, "File not open for reading"); THROW_INVALID_OPERATION_EXCEPTION_RETURN(0, "File not open for reading");
return (atInt8)readUByte(); return (atInt8)readUByte();
} }
@ -180,6 +184,7 @@ atUint8* FileReader::readUBytes(atUint64 len)
{ {
if (!isOpen()) if (!isOpen())
THROW_INVALID_OPERATION_EXCEPTION_RETURN(nullptr, "File not open for reading"); THROW_INVALID_OPERATION_EXCEPTION_RETURN(nullptr, "File not open for reading");
m_bitValid = false; m_bitValid = false;
atUint8* val = new atUint8[len]; atUint8* val = new atUint8[len];
fread(val, 1, len, m_fileHandle); fread(val, 1, len, m_fileHandle);
@ -190,6 +195,7 @@ atUint64 FileReader::readUBytesToBuf(void* buf, atUint64 len)
{ {
if (!isOpen()) if (!isOpen())
THROW_INVALID_OPERATION_EXCEPTION_RETURN(0, "File not open for reading"); THROW_INVALID_OPERATION_EXCEPTION_RETURN(0, "File not open for reading");
m_bitValid = false; m_bitValid = false;
return fread(buf, 1, len, m_fileHandle); return fread(buf, 1, len, m_fileHandle);
} }
@ -198,6 +204,7 @@ atInt8* FileReader::readBytes(atUint64 len)
{ {
if (!isOpen()) if (!isOpen())
THROW_INVALID_OPERATION_EXCEPTION_RETURN(nullptr, "File not open for reading"); THROW_INVALID_OPERATION_EXCEPTION_RETURN(nullptr, "File not open for reading");
return (atInt8*)readUBytes(len); return (atInt8*)readUBytes(len);
} }
@ -314,6 +321,7 @@ std::string FileReader::readString(atInt32 maxlen)
atUint8 chr = readByte(); atUint8 chr = readByte();
atInt32 i = 0; atInt32 i = 0;
while (chr != 0) while (chr != 0)
{ {
if (maxlen >= 0 && i >= maxlen - 1) if (maxlen >= 0 && i >= maxlen - 1)
@ -336,20 +344,24 @@ std::string FileReader::readUnicode(atInt32 maxlen)
std::vector<short> tmp; std::vector<short> tmp;
atInt32 i = 0; atInt32 i = 0;
for (;;) for (;;)
{ {
if (maxlen >= 0 && i >= maxlen - 1) if (maxlen >= 0 && i >= maxlen - 1)
break; break;
short chr = readUint16(); short chr = readUint16();
if (chr) if (chr)
tmp.push_back(chr); tmp.push_back(chr);
else else
break; break;
i++; i++;
}; };
utf8::utf16to8(tmp.begin(), tmp.end(), back_inserter(ret)); utf8::utf16to8(tmp.begin(), tmp.end(), back_inserter(ret));
return ret; return ret;
} }

View File

@ -71,6 +71,7 @@ bool FileWriter::isLittleEndian() const
void FileWriter::open() void FileWriter::open()
{ {
m_fileHandle = fopen(m_filename.c_str(), "w+b"); m_fileHandle = fopen(m_filename.c_str(), "w+b");
if (!m_fileHandle) if (!m_fileHandle)
THROW_FILE_NOT_FOUND_EXCEPTION(m_filename); THROW_FILE_NOT_FOUND_EXCEPTION(m_filename);
@ -141,6 +142,7 @@ void FileWriter::writeBit(bool val)
m_bitValid = false; m_bitValid = false;
fseeko64(m_fileHandle, m_bytePosition, (int)SeekOrigin::Begin); fseeko64(m_fileHandle, m_bytePosition, (int)SeekOrigin::Begin);
if (fwrite(&m_currentByte, 1, 1, m_fileHandle) != sizeof(atInt8)) if (fwrite(&m_currentByte, 1, 1, m_fileHandle) != sizeof(atInt8))
THROW_IO_EXCEPTION("Unable to data to file"); THROW_IO_EXCEPTION("Unable to data to file");
} }
@ -149,6 +151,7 @@ void FileWriter::seekBit(int bit)
{ {
if (bit < 0 || bit > 7) if (bit < 0 || bit > 7)
THROW_INVALID_OPERATION_EXCEPTION("bit must be >= 0 and <= 7"); THROW_INVALID_OPERATION_EXCEPTION("bit must be >= 0 and <= 7");
m_bitShift = bit; m_bitShift = bit;
m_bitValid = true; m_bitValid = true;
} }
@ -283,8 +286,10 @@ void FileWriter::writeString(const std::string& val)
m_bitValid = false; m_bitValid = false;
char term = '\0'; char term = '\0';
if (fwrite(val.c_str(), 1, val.length(), m_fileHandle) != val.length()) if (fwrite(val.c_str(), 1, val.length(), m_fileHandle) != val.length())
THROW_IO_EXCEPTION("Unable to write to stream"); THROW_IO_EXCEPTION("Unable to write to stream");
if (fwrite(&term, 1, 1, m_fileHandle) != 1) if (fwrite(&term, 1, 1, m_fileHandle) != 1)
THROW_IO_EXCEPTION("Unable to write to stream"); THROW_IO_EXCEPTION("Unable to write to stream");
} }
@ -311,6 +316,7 @@ void FileWriter::fill(atInt8 byte, atUint64 len)
{ {
if (!isOpen()) if (!isOpen())
THROW_INVALID_OPERATION_EXCEPTION("File not open for writing"); THROW_INVALID_OPERATION_EXCEPTION("File not open for writing");
fwrite(&byte, 1, len, m_fileHandle); fwrite(&byte, 1, len, m_fileHandle);
} }

View File

@ -22,13 +22,16 @@ std::ostream& operator<<(std::ostream& os, const Athena::SeekOrigin& origin)
case Athena::SeekOrigin::Begin: case Athena::SeekOrigin::Begin:
os << "Begin"; os << "Begin";
break; break;
case Athena::SeekOrigin::Current: case Athena::SeekOrigin::Current:
os << "Current"; os << "Current";
break; break;
case Athena::SeekOrigin::End: case Athena::SeekOrigin::End:
os << "End"; os << "End";
break; break;
} }
return os; return os;
} }
@ -40,10 +43,12 @@ std::ostream& operator<<(std::ostream& os, const Athena::Endian& endian)
case Athena::Endian::LittleEndian: case Athena::Endian::LittleEndian:
os << "LittleEndian"; os << "LittleEndian";
break; break;
case Athena::Endian::BigEndian: case Athena::Endian::BigEndian:
os << "BigEndian"; os << "BigEndian";
break; break;
} }
return os; return os;
} }

View File

@ -43,6 +43,7 @@ MemoryReader::MemoryReader(const atUint8* data, atUint64 length)
{ {
if (!data) if (!data)
THROW_INVALID_DATA_EXCEPTION("data cannot be NULL"); THROW_INVALID_DATA_EXCEPTION("data cannot be NULL");
if (length == 0) if (length == 0)
THROW_INVALID_OPERATION_EXCEPTION("length cannot be 0"); THROW_INVALID_OPERATION_EXCEPTION("length cannot be 0");
@ -100,16 +101,21 @@ void MemoryReader::seek(atInt64 position, SeekOrigin origin)
case SeekOrigin::Begin: case SeekOrigin::Begin:
if ((position < 0 || (atInt64)position > (atInt64)m_length)) if ((position < 0 || (atInt64)position > (atInt64)m_length))
THROW_IO_EXCEPTION("Position %0.8X outside stream bounds ", position); THROW_IO_EXCEPTION("Position %0.8X outside stream bounds ", position);
m_position = position; m_position = position;
break; break;
case SeekOrigin::Current: case SeekOrigin::Current:
if ((((atInt64)m_position + position) < 0 || (m_position + position) > m_length)) if ((((atInt64)m_position + position) < 0 || (m_position + position) > m_length))
THROW_IO_EXCEPTION("Position %0.8X outside stream bounds ", position); THROW_IO_EXCEPTION("Position %0.8X outside stream bounds ", position);
m_position += position; m_position += position;
break; break;
case SeekOrigin::End: case SeekOrigin::End:
if ((((atInt64)m_length - position < 0) || (m_length - position) > m_length)) if ((((atInt64)m_length - position < 0) || (m_length - position) > m_length))
THROW_IO_EXCEPTION("Position %0.8X outside stream bounds ", position); THROW_IO_EXCEPTION("Position %0.8X outside stream bounds ", position);
m_position = m_length - position; m_position = m_length - position;
break; break;
} }
@ -174,12 +180,14 @@ bool MemoryReader::readBit()
{ {
if (!m_data) if (!m_data)
loadData(); loadData();
if (m_position > m_length) if (m_position > m_length)
THROW_IO_EXCEPTION_RETURN(false, "Position %0.8X outside stream bounds ", m_position); THROW_IO_EXCEPTION_RETURN(false, "Position %0.8X outside stream bounds ", m_position);
bool ret = (*(atUint8*)(m_data + m_position) & (1 << m_bitPosition)) != 0; bool ret = (*(atUint8*)(m_data + m_position) & (1 << m_bitPosition)) != 0;
m_bitPosition++; m_bitPosition++;
if (m_bitPosition > 7) if (m_bitPosition > 7)
{ {
m_bitPosition = 0; m_bitPosition = 0;
@ -199,6 +207,7 @@ atInt8 MemoryReader::readByte()
m_bitPosition = 0; m_bitPosition = 0;
m_position += sizeof(atUint8); m_position += sizeof(atUint8);
} }
if (m_position + 1 > m_length) if (m_position + 1 > m_length)
THROW_IO_EXCEPTION_RETURN(0, "Position %0.8X outside stream bounds ", m_position); THROW_IO_EXCEPTION_RETURN(0, "Position %0.8X outside stream bounds ", m_position);
@ -218,6 +227,7 @@ atUint8 MemoryReader::readUByte()
m_bitPosition = 0; m_bitPosition = 0;
m_position += sizeof(atUint8); m_position += sizeof(atUint8);
} }
if (m_position + 1 > m_length) if (m_position + 1 > m_length)
THROW_IO_EXCEPTION_RETURN(0, "Position %0.8X outside stream bounds ", m_position); THROW_IO_EXCEPTION_RETURN(0, "Position %0.8X outside stream bounds ", m_position);
@ -278,6 +288,7 @@ atInt16 MemoryReader::readInt16()
if (m_position + sizeof(atInt16) > m_length) if (m_position + sizeof(atInt16) > m_length)
THROW_IO_EXCEPTION_RETURN(0, "Position %0.8X outside stream bounds ", m_position); THROW_IO_EXCEPTION_RETURN(0, "Position %0.8X outside stream bounds ", m_position);
atInt16 ret = *(atInt16*)(m_data + m_position); atInt16 ret = *(atInt16*)(m_data + m_position);
m_position += sizeof(atInt16); m_position += sizeof(atInt16);
@ -307,6 +318,7 @@ atInt32 MemoryReader::readInt32()
if (m_position + sizeof(atInt32) > m_length) if (m_position + sizeof(atInt32) > m_length)
THROW_IO_EXCEPTION_RETURN(0, "Position %0.8X outside stream bounds ", m_position); THROW_IO_EXCEPTION_RETURN(0, "Position %0.8X outside stream bounds ", m_position);
atInt32 ret = *(atInt32*)(m_data + m_position); atInt32 ret = *(atInt32*)(m_data + m_position);
m_position += 4; m_position += 4;
@ -333,6 +345,7 @@ atInt64 MemoryReader::readInt64()
m_bitPosition = 0; m_bitPosition = 0;
m_position += sizeof(atUint8); m_position += sizeof(atUint8);
} }
if (m_position + sizeof(atInt64) > m_length) if (m_position + sizeof(atInt64) > m_length)
THROW_IO_EXCEPTION_RETURN(0, "Position %0.8X outside stream bounds ", m_position); THROW_IO_EXCEPTION_RETURN(0, "Position %0.8X outside stream bounds ", m_position);
@ -362,6 +375,7 @@ float MemoryReader::readFloat()
m_bitPosition = 0; m_bitPosition = 0;
m_position += sizeof(atUint8); m_position += sizeof(atUint8);
} }
if (m_position + sizeof(float) > m_length) if (m_position + sizeof(float) > m_length)
THROW_IO_EXCEPTION_RETURN(0, "Position %0.8X outside stream bounds ", m_position); THROW_IO_EXCEPTION_RETURN(0, "Position %0.8X outside stream bounds ", m_position);
@ -386,6 +400,7 @@ double MemoryReader::readDouble()
m_bitPosition = 0; m_bitPosition = 0;
m_position += sizeof(atUint8); m_position += sizeof(atUint8);
} }
if (m_position + sizeof(double) > m_length) if (m_position + sizeof(double) > m_length)
THROW_IO_EXCEPTION_RETURN(0, "Position %0.8X outside stream bounds ", m_position); THROW_IO_EXCEPTION_RETURN(0, "Position %0.8X outside stream bounds ", m_position);
@ -396,6 +411,7 @@ double MemoryReader::readDouble()
utility::BigDouble(ret); utility::BigDouble(ret);
else else
utility::LittleDouble(ret); utility::LittleDouble(ret);
return ret; return ret;
} }
@ -409,6 +425,7 @@ bool MemoryReader::readBool()
m_bitPosition = 0; m_bitPosition = 0;
m_position += sizeof(atUint8); m_position += sizeof(atUint8);
} }
if (m_position + sizeof(bool) > m_length) if (m_position + sizeof(bool) > m_length)
THROW_IO_EXCEPTION_RETURN(false, "Position %0.8X outside stream bounds ", m_position); THROW_IO_EXCEPTION_RETURN(false, "Position %0.8X outside stream bounds ", m_position);
@ -421,11 +438,13 @@ std::string MemoryReader::readUnicode(atInt32 maxlen)
{ {
if (!m_data) if (!m_data)
loadData(); loadData();
std::string ret; std::string ret;
std::vector<short> tmp; std::vector<short> tmp;
atUint16 chr = readUint16(); atUint16 chr = readUint16();
atInt32 i = 0; atInt32 i = 0;
for (;;) for (;;)
{ {
if (maxlen >= 0 && i >= maxlen - 1) if (maxlen >= 0 && i >= maxlen - 1)
@ -433,6 +452,7 @@ std::string MemoryReader::readUnicode(atInt32 maxlen)
if (!chr) if (!chr)
break; break;
tmp.push_back(chr); tmp.push_back(chr);
chr = readUint16(); chr = readUint16();
i++; i++;
@ -448,6 +468,7 @@ std::string MemoryReader::readString(atInt32 maxlen)
atUint8 chr = readByte(); atUint8 chr = readByte();
atInt32 i = 0; atInt32 i = 0;
while (chr != 0) while (chr != 0)
{ {
if (maxlen >= 0 && i >= maxlen - 1) if (maxlen >= 0 && i >= maxlen - 1)
@ -474,6 +495,7 @@ void MemoryReader::loadData()
if (!in) if (!in)
THROW_FILE_NOT_FOUND_EXCEPTION(m_filepath); THROW_FILE_NOT_FOUND_EXCEPTION(m_filepath);
rewind(in); rewind(in);
length = utility::fileSize(m_filepath); length = utility::fileSize(m_filepath);
@ -485,6 +507,7 @@ void MemoryReader::loadData()
atUint64 done = 0; atUint64 done = 0;
atUint64 blocksize = BLOCKSZ; atUint64 blocksize = BLOCKSZ;
do do
{ {
if (blocksize > length - done) if (blocksize > length - done)
@ -502,7 +525,8 @@ void MemoryReader::loadData()
if (m_progressCallback) if (m_progressCallback)
m_progressCallback((int)((float)(done * 100.f) / length)); m_progressCallback((int)((float)(done * 100.f) / length));
} while (done < length); }
while (done < length);
fclose(in); fclose(in);
m_length = length; m_length = length;

View File

@ -106,8 +106,10 @@ void MemoryWriter::seek(atInt64 position, SeekOrigin origin)
if ((atUint64)position > m_length) if ((atUint64)position > m_length)
resize(position); resize(position);
m_position = position; m_position = position;
break; break;
case SeekOrigin::Current: case SeekOrigin::Current:
if ((((atInt64)m_position + position) < 0)) if ((((atInt64)m_position + position) < 0))
THROW_IO_EXCEPTION("Position outside stream bounds"); THROW_IO_EXCEPTION("Position outside stream bounds");
@ -117,12 +119,14 @@ void MemoryWriter::seek(atInt64 position, SeekOrigin origin)
m_position += position; m_position += position;
break; break;
case SeekOrigin::End: case SeekOrigin::End:
if (((atInt64)m_length - position) < 0) if (((atInt64)m_length - position) < 0)
THROW_IO_EXCEPTION("Position outside stream bounds"); THROW_IO_EXCEPTION("Position outside stream bounds");
if ((atUint64)position > m_length) if ((atUint64)position > m_length)
resize(position); resize(position);
m_position = m_length - position; m_position = m_length - position;
break; break;
} }
@ -185,11 +189,13 @@ void MemoryWriter::save(const std::string& filename)
m_filepath = filename; m_filepath = filename;
FILE* out = fopen(m_filepath.c_str(), "wb"); FILE* out = fopen(m_filepath.c_str(), "wb");
if (!out) if (!out)
THROW_FILE_NOT_FOUND_EXCEPTION(m_filepath); THROW_FILE_NOT_FOUND_EXCEPTION(m_filepath);
atUint64 done = 0; atUint64 done = 0;
atUint64 blocksize = BLOCKSZ; atUint64 blocksize = BLOCKSZ;
do do
{ {
if (blocksize > m_length - done) if (blocksize > m_length - done)
@ -203,7 +209,8 @@ void MemoryWriter::save(const std::string& filename)
break; break;
done += blocksize; done += blocksize;
}while (done < m_length); }
while (done < m_length);
fclose(out); fclose(out);
} }
@ -228,7 +235,9 @@ void MemoryWriter::writeBit(bool val)
*(atUint8*)(m_data + m_position) |= (1 << m_bitPosition); *(atUint8*)(m_data + m_position) |= (1 << m_bitPosition);
else else
*(atUint8*)(m_data + m_position) &= ~(1 << m_bitPosition); *(atUint8*)(m_data + m_position) &= ~(1 << m_bitPosition);
m_bitPosition++; m_bitPosition++;
if (m_bitPosition > 7) if (m_bitPosition > 7)
{ {
m_bitPosition = 0; m_bitPosition = 0;
@ -246,6 +255,7 @@ void MemoryWriter::writeUByte(atUint8 val)
m_bitPosition = 0; m_bitPosition = 0;
m_position += sizeof(atUint8); m_position += sizeof(atUint8);
} }
if (m_position + 1 > m_length) if (m_position + 1 > m_length)
resize(m_position + 1); resize(m_position + 1);
@ -272,6 +282,7 @@ void MemoryWriter::writeUBytes(atUint8* data, atUint64 length)
if (!data) if (!data)
THROW_INVALID_DATA_EXCEPTION("data cannnot be NULL"); THROW_INVALID_DATA_EXCEPTION("data cannnot be NULL");
if (m_position + length > m_length) if (m_position + length > m_length)
resize(m_position + length); resize(m_position + length);
@ -445,6 +456,7 @@ void MemoryWriter::writeUnicode(const std::string& str)
if (chr != 0xFEFF) if (chr != 0xFEFF)
writeInt16(chr); writeInt16(chr);
} }
writeInt16(0); writeInt16(0);
} }
@ -453,9 +465,11 @@ void MemoryWriter::writeString(const std::string& str)
for (atUint8 c : str) for (atUint8 c : str)
{ {
writeUByte(c); writeUByte(c);
if (c == '\0') if (c == '\0')
break; break;
} }
writeUByte(0); writeUByte(0);
} }

View File

@ -42,21 +42,27 @@ bool PHYSFSFileReader::isOpen() const
void PHYSFSFileReader::seek(atInt64 position, SeekOrigin origin) void PHYSFSFileReader::seek(atInt64 position, SeekOrigin origin)
{ {
atInt64 curPos = PHYSFS_tell(m_handle); atInt64 curPos = PHYSFS_tell(m_handle);
switch (origin) switch (origin)
{ {
case SeekOrigin::Begin: case SeekOrigin::Begin:
if ((position < 0 || (atInt64)position > (atInt64)m_length)) if ((position < 0 || (atInt64)position > (atInt64)m_length))
THROW_IO_EXCEPTION("Position %0.16X outside stream bounds ", position); THROW_IO_EXCEPTION("Position %0.16X outside stream bounds ", position);
PHYSFS_seek(m_handle, position); PHYSFS_seek(m_handle, position);
break; break;
case SeekOrigin::Current: case SeekOrigin::Current:
if ((((atInt64)curPos + position) < 0 || (curPos + position) > m_length)) if ((((atInt64)curPos + position) < 0 || (curPos + position) > m_length))
THROW_IO_EXCEPTION("Position %0.16X outside stream bounds ", position); THROW_IO_EXCEPTION("Position %0.16X outside stream bounds ", position);
PHYSFS_seek(m_handle, curPos + position); PHYSFS_seek(m_handle, curPos + position);
break; break;
case SeekOrigin::End: case SeekOrigin::End:
if ((((atInt64)m_length - position < 0) || (m_length - position) > m_length)) if ((((atInt64)m_length - position < 0) || (m_length - position) > m_length))
THROW_IO_EXCEPTION("Position %0.16X outside stream bounds ", position); THROW_IO_EXCEPTION("Position %0.16X outside stream bounds ", position);
PHYSFS_seek(m_handle, m_length - position); PHYSFS_seek(m_handle, m_length - position);
break; break;
} }
@ -80,8 +86,10 @@ atInt8 PHYSFSFileReader::readByte()
atUint8* PHYSFSFileReader::readUBytes(atUint64 length) atUint8* PHYSFSFileReader::readUBytes(atUint64 length)
{ {
atUint8* data = new atUint8[length]; atUint8* data = new atUint8[length];
if (PHYSFS_read(m_handle, data, 1, length) == length) if (PHYSFS_read(m_handle, data, 1, length) == length)
return data; return data;
delete[] data; delete[] data;
THROW_IO_EXCEPTION("Position outside stream bounds"); THROW_IO_EXCEPTION("Position outside stream bounds");
} }

View File

@ -41,6 +41,7 @@ SkywardSwordFileReader::SkywardSwordFileReader(const std::string& filename)
SkywardSwordFile* SkywardSwordFileReader::read() SkywardSwordFile* SkywardSwordFileReader::read()
{ {
SkywardSwordFile* file = NULL; SkywardSwordFile* file = NULL;
try try
{ {
if (base::length() != 0xFBE0) if (base::length() != 0xFBE0)
@ -60,6 +61,7 @@ SkywardSwordFile* SkywardSwordFileReader::read()
// Time to read in each slot // Time to read in each slot
file = new SkywardSwordFile; file = new SkywardSwordFile;
file->setRegion((magic == SkywardSwordFile::USMagic ? Region::NTSC : (magic == SkywardSwordFile::JAMagic ? Region::NTSCJ : Region::PAL))); file->setRegion((magic == SkywardSwordFile::USMagic ? Region::NTSC : (magic == SkywardSwordFile::JAMagic ? Region::NTSCJ : Region::PAL)));
for (int i = 0; i < 3; i++) for (int i = 0; i < 3; i++)
{ {
SkywardSwordQuest* q = new SkywardSwordQuest((atUint8*)base::readBytes(0x53C0), 0x53C0); SkywardSwordQuest* q = new SkywardSwordQuest((atUint8*)base::readBytes(0x53C0), 0x53C0);

View File

@ -51,10 +51,12 @@ void SkywardSwordFileWriter::write(SkywardSwordFile *file)
std::vector<SkywardSwordQuest*> quests = file->questList(); std::vector<SkywardSwordQuest*> quests = file->questList();
int i = 0; int i = 0;
for (SkywardSwordQuest* q : quests) for (SkywardSwordQuest* q : quests)
{ {
if (q->length() != 0x53C0) if (q->length() != 0x53C0)
THROW_INVALID_DATA_EXCEPTION("q->data() not 0x53C0 bytes in length"); THROW_INVALID_DATA_EXCEPTION("q->data() not 0x53C0 bytes in length");
// Update the checksums // Update the checksums
q->fixChecksums(); q->fixChecksums();
// Write the save data // Write the save data

View File

@ -85,6 +85,7 @@ void SkywardSwordQuest::setPlayerName(const std::string& name)
for (atUint32 i = 0; i < 8; i++) for (atUint32 i = 0; i < 8; i++)
{ {
atUint16& c = *(atUint16*)(m_data + priv::NAME_OFFSET + (i * 2)); atUint16& c = *(atUint16*)(m_data + priv::NAME_OFFSET + (i * 2));
if (i >= val.size()) if (i >= val.size())
{ {
c = 0; c = 0;
@ -99,9 +100,11 @@ void SkywardSwordQuest::setPlayerName(const std::string& name)
std::string SkywardSwordQuest::playerName() const std::string SkywardSwordQuest::playerName() const
{ {
std::vector<atUint16> val; std::vector<atUint16> val;
for (atUint32 i = 0; i < 8; i++) for (atUint32 i = 0; i < 8; i++)
{ {
atUint16 c = *(atUint16*)(m_data + priv::NAME_OFFSET + (i * 2)); atUint16 c = *(atUint16*)(m_data + priv::NAME_OFFSET + (i * 2));
if (c == 0) if (c == 0)
break; break;
@ -137,9 +140,11 @@ void SkywardSwordQuest::setAmmoCount(SkywardSwordQuest::AmmoType type, atUint32
case Arrows: case Arrows:
values.arrows = count; values.arrows = count;
break; break;
case Bombs: case Bombs:
values.bombs = count; values.bombs = count;
break; break;
case Seeds: case Seeds:
values.seeds = count; values.seeds = count;
break; break;
@ -155,10 +160,17 @@ atUint32 SkywardSwordQuest::ammoCount(AmmoType type)
switch (type) switch (type)
{ {
case Arrows: return values.arrows; case Arrows:
case Bombs: return values.bombs; return values.arrows;
case Seeds: return values.seeds;
default: return 0; case Bombs:
return values.bombs;
case Seeds:
return values.seeds;
default:
return 0;
} }
} }

View File

@ -48,6 +48,7 @@ Sprite::Sprite(SpriteFile* root, const std::string& name)
Sprite::~Sprite() Sprite::~Sprite()
{ {
#ifndef ATHENA_USE_QT #ifndef ATHENA_USE_QT
for (SpriteFrame* frame : m_frames) for (SpriteFrame* frame : m_frames)
#else #else
foreach (SpriteFrame* frame, m_frames) foreach (SpriteFrame* frame, m_frames)
@ -172,6 +173,7 @@ bool Sprite::addFrame(SpriteFrame* part)
{ {
if (m_frames.size() > 65536) if (m_frames.size() > 65536)
return false; return false;
for (SpriteFrame* tmp : m_frames) for (SpriteFrame* tmp : m_frames)
{ {
if (tmp == part) if (tmp == part)
@ -186,14 +188,18 @@ bool Sprite::removeFrame(SpriteFrame* frame)
{ {
#ifndef ATHENA_USE_QT #ifndef ATHENA_USE_QT
std::vector<SpriteFrame*>::iterator iter = std::find(m_frames.begin(), m_frames.end(), frame); std::vector<SpriteFrame*>::iterator iter = std::find(m_frames.begin(), m_frames.end(), frame);
if (iter != m_frames.end()) if (iter != m_frames.end())
{ {
m_frames.erase(iter); m_frames.erase(iter);
return true; return true;
} }
#else #else
if (m_frames.removeOne(frame)) if (m_frames.removeOne(frame))
return true; return true;
#endif #endif
return false; return false;
} }
@ -209,6 +215,7 @@ void Sprite::setFrames(std::vector<SpriteFrame*> frames)
{ {
if (frames.size() == 0) if (frames.size() == 0)
return; return;
if (m_frames.size() > 0) if (m_frames.size() > 0)
{ {
for (SpriteFrame* frame : m_frames) for (SpriteFrame* frame : m_frames)
@ -216,8 +223,10 @@ void Sprite::setFrames(std::vector<SpriteFrame*> frames)
delete frame; delete frame;
frame = NULL; frame = NULL;
} }
m_frames.clear(); m_frames.clear();
} }
m_frames = frames; m_frames = frames;
} }
#else #else
@ -251,6 +260,7 @@ SpriteFile* Sprite::container() const
void Sprite::setCurrentFrame(SpriteFrame* frame) void Sprite::setCurrentFrame(SpriteFrame* frame)
{ {
atUint32 id = 0; atUint32 id = 0;
for (SpriteFrame* tmpFrame : m_frames) for (SpriteFrame* tmpFrame : m_frames)
{ {
if (tmpFrame == frame) if (tmpFrame == frame)
@ -258,6 +268,7 @@ void Sprite::setCurrentFrame(SpriteFrame* frame)
setCurrentFrame(id); setCurrentFrame(id);
return; return;
} }
id++; id++;
} }
} }
@ -281,6 +292,7 @@ SpriteFrame* Sprite::currentFrame() const
void Sprite::advanceFrame() void Sprite::advanceFrame()
{ {
m_currentFrame++; m_currentFrame++;
if (m_currentFrame >= m_frames.size()) if (m_currentFrame >= m_frames.size())
m_currentFrame = (atUint32)m_frames.size() - 1; m_currentFrame = (atUint32)m_frames.size() - 1;
} }

View File

@ -55,11 +55,13 @@ SpriteFile::SpriteFile(const QSize& size, const QPoint& origin)
SpriteFile::~SpriteFile() SpriteFile::~SpriteFile()
{ {
#ifndef ATHENA_USE_QT #ifndef ATHENA_USE_QT
for (std::pair<std::string, Sprite*> sprite : m_sprites) for (std::pair<std::string, Sprite*> sprite : m_sprites)
{ {
delete sprite.second; delete sprite.second;
sprite.second = NULL; sprite.second = NULL;
} }
#endif #endif
m_sprites.clear(); m_sprites.clear();
} }
@ -207,12 +209,16 @@ void SpriteFile::addSprite(Sprite* sprite)
#ifndef ATHENA_USE_QT #ifndef ATHENA_USE_QT
std::string name(sprite->name()); std::string name(sprite->name());
Athena::utility::tolower(name); Athena::utility::tolower(name);
if (m_sprites.find(name) != m_sprites.end()) if (m_sprites.find(name) != m_sprites.end())
return; return;
#else #else
QString name = sprite->name().toLower(); QString name = sprite->name().toLower();
if (m_sprites.contains(name)) if (m_sprites.contains(name))
return; return;
#endif #endif
m_sprites[name] = sprite; m_sprites[name] = sprite;
@ -224,6 +230,7 @@ void SpriteFile::removeSprite(const std::string& name)
std::string tmpName(name); std::string tmpName(name);
Athena::utility::tolower(tmpName); Athena::utility::tolower(tmpName);
std::unordered_map<std::string, Sprite*>::iterator iterator = m_sprites.find(tmpName); std::unordered_map<std::string, Sprite*>::iterator iterator = m_sprites.find(tmpName);
if (iterator != m_sprites.end()) if (iterator != m_sprites.end())
m_sprites.erase(iterator); m_sprites.erase(iterator);
} }
@ -244,6 +251,7 @@ void SpriteFile::setSprites(std::unordered_map<std::string, Sprite*> sprites)
{ {
if (sprites.size() == 0) if (sprites.size() == 0)
return; return;
if (m_sprites.size() > 0) if (m_sprites.size() > 0)
{ {
for (std::pair<std::string, Sprite*> sprite : m_sprites) for (std::pair<std::string, Sprite*> sprite : m_sprites)
@ -251,6 +259,7 @@ void SpriteFile::setSprites(std::unordered_map<std::string, Sprite*> sprites)
delete sprite.second; delete sprite.second;
sprite.second = NULL; sprite.second = NULL;
} }
m_sprites.clear(); m_sprites.clear();
} }
@ -261,6 +270,7 @@ void SpriteFile::setSprites(QMap<QString, Sprite *> sprites)
{ {
if (sprites.size() == 0) if (sprites.size() == 0)
return; return;
m_sprites.clear(); m_sprites.clear();
m_sprites = sprites; m_sprites = sprites;
} }
@ -271,6 +281,7 @@ Sprite* SpriteFile::sprite(const std::string& name)
{ {
std::string nameLow(name); std::string nameLow(name);
Athena::utility::tolower(nameLow); Athena::utility::tolower(nameLow);
if (m_sprites.find(nameLow) == m_sprites.end()) if (m_sprites.find(nameLow) == m_sprites.end())
return NULL; return NULL;
@ -313,6 +324,7 @@ void SpriteFile::setTextures(std::vector<STexture*> textures)
delete tex; delete tex;
tex = NULL; tex = NULL;
} }
m_textures.clear(); m_textures.clear();
} }

View File

@ -42,6 +42,7 @@ SpriteFileReader::SpriteFileReader(const std::string& filepath)
Sakura::SpriteFile* SpriteFileReader::readFile() Sakura::SpriteFile* SpriteFileReader::readFile()
{ {
Sakura::SpriteFile* ret = NULL; Sakura::SpriteFile* ret = NULL;
try try
{ {
atUint32 magic = base::readUint32(); atUint32 magic = base::readUint32();
@ -126,6 +127,7 @@ Sakura::SpriteFile* SpriteFileReader::readFile()
// Each state id corresponds to a texture held in the parent class // Each state id corresponds to a texture held in the parent class
std::vector<int> stateIds; std::vector<int> stateIds;
for (int j = 0; j < stateCount; j++) for (int j = 0; j < stateCount; j++)
stateIds.push_back(base::readUint16()); stateIds.push_back(base::readUint16());
@ -157,6 +159,7 @@ Sakura::SpriteFile* SpriteFileReader::readFile()
#else #else
QList<Sakura::SpritePart*> parts; QList<Sakura::SpritePart*> parts;
#endif #endif
for (atUint8 j = 0; j < partCount; j++) for (atUint8 j = 0; j < partCount; j++)
{ {
Sakura::SpritePart* part = new Sakura::SpritePart(frame); Sakura::SpritePart* part = new Sakura::SpritePart(frame);
@ -184,21 +187,26 @@ Sakura::SpriteFile* SpriteFileReader::readFile()
parts.push_back(part); parts.push_back(part);
} }
frame->setParts(parts); frame->setParts(parts);
frames.push_back(frame); frames.push_back(frame);
} }
sprite->setFrames(frames); sprite->setFrames(frames);
#ifndef ATHENA_USE_QT #ifndef ATHENA_USE_QT
if (sprite->name() != std::string()) if (sprite->name() != std::string())
{ {
std::string nameLow(sprite->name()); std::string nameLow(sprite->name());
Athena::utility::tolower(nameLow); Athena::utility::tolower(nameLow);
sprites[nameLow] = sprite; sprites[nameLow] = sprite;
} }
#else #else
if (!sprite->name().isEmpty()) if (!sprite->name().isEmpty())
sprites[sprite->name().toLower()] = sprite; sprites[sprite->name().toLower()] = sprite;
#endif #endif
else else
THROW_IO_EXCEPTION_RETURN(nullptr, "Sprite names cannot be empty"); THROW_IO_EXCEPTION_RETURN(nullptr, "Sprite names cannot be empty");

View File

@ -60,11 +60,13 @@ void SpriteFileWriter::writeFile(Sakura::SpriteFile* file)
} }
#ifndef ATHENA_USE_QT #ifndef ATHENA_USE_QT
for (std::pair<std::string, Sakura::Sprite*> spritePair : file->sprites()) for (std::pair<std::string, Sakura::Sprite*> spritePair : file->sprites())
{ {
Sakura::Sprite* sprite = spritePair.second; Sakura::Sprite* sprite = spritePair.second;
base::writeString(sprite->name()); base::writeString(sprite->name());
#else #else
foreach (Sakura::Sprite* sprite, file->sprites().values()) foreach (Sakura::Sprite* sprite, file->sprites().values())
{ {
@ -80,6 +82,7 @@ void SpriteFileWriter::writeFile(Sakura::SpriteFile* file)
{ {
base::writeFloat(frame->frameTime()); base::writeFloat(frame->frameTime());
base::writeUint16(frame->partCount()); base::writeUint16(frame->partCount());
for (Sakura::SpritePart* part : frame->parts()) for (Sakura::SpritePart* part : frame->parts())
{ {
#ifndef ATHENA_USE_QT #ifndef ATHENA_USE_QT

View File

@ -51,8 +51,10 @@ std::vector<std::string> &split(const std::string &s, char delim, std::vector<st
{ {
std::stringstream ss(s); std::stringstream ss(s);
std::string item; std::string item;
while (std::getline(ss, item, delim)) while (std::getline(ss, item, delim))
elems.push_back(item); elems.push_back(item);
return elems; return elems;
} }
@ -88,13 +90,16 @@ std::string vsprintf(const char* fmt, va_list list)
char* buffer = 0; char* buffer = 0;
buffer = new char[size]; buffer = new char[size];
int nsize = ::vsnprintf(buffer, size, fmt, list); int nsize = ::vsnprintf(buffer, size, fmt, list);
while (size <= nsize) while (size <= nsize)
{ //fail delete buffer and try again {
//fail delete buffer and try again
delete[] buffer; delete[] buffer;
buffer = 0; buffer = 0;
buffer = new char[nsize + 1]; //+1 for /0 buffer = new char[nsize + 1]; //+1 for /0
nsize = ::vsnprintf(buffer, size, fmt, list); nsize = ::vsnprintf(buffer, size, fmt, list);
} }
std::string ret(buffer); std::string ret(buffer);
delete[] buffer; delete[] buffer;
return ret; return ret;
@ -120,6 +125,7 @@ bool parseBool(const std::string& boolean, bool* valid)
{ {
if (valid) if (valid)
*valid = true; *valid = true;
return true; return true;
} }
@ -128,6 +134,7 @@ bool parseBool(const std::string& boolean, bool* valid)
{ {
if (valid) if (valid)
*valid = true; *valid = true;
return false; return false;
} }
@ -144,6 +151,7 @@ int countChar(const std::string& str, const char chr, int* lastOccur)
int ret = 0; int ret = 0;
int index = 0; int index = 0;
for (char c : str) for (char c : str)
{ {
if (c == chr) if (c == chr)
@ -153,6 +161,7 @@ int countChar(const std::string& str, const char chr, int* lastOccur)
ret++; ret++;
} }
index++; index++;
} }
@ -171,6 +180,7 @@ std::string &trim(std::string &s)
{ {
// Find first non whitespace char in StrToTrim // Find first non whitespace char in StrToTrim
std::string::size_type first = s.find_first_not_of(' '); std::string::size_type first = s.find_first_not_of(' ');
// Check whether something went wrong? // Check whether something went wrong?
if (first == std::string::npos) if (first == std::string::npos)
{ {
@ -179,6 +189,7 @@ std::string &trim(std::string &s)
// Find last non whitespace char from StrToTrim // Find last non whitespace char from StrToTrim
std::string::size_type last = s.find_last_not_of(' '); std::string::size_type last = s.find_last_not_of(' ');
// If something didn't go wrong, Last will be recomputed to get real length of substring // If something didn't go wrong, Last will be recomputed to get real length of substring
if (last != std::string::npos) if (last != std::string::npos)
{ {

View File

@ -92,6 +92,7 @@ void WiiFile::setData(const atUint8* data)
delete[] m_fileData; delete[] m_fileData;
m_fileData = NULL; m_fileData = NULL;
} }
m_fileData = (atUint8*)data; m_fileData = (atUint8*)data;
} }
@ -153,10 +154,12 @@ void WiiFile::addChild(WiiFile *file)
// Since we only support *NIX paths this is simple // Since we only support *NIX paths this is simple
atUint32 depth = Athena::utility::countChar(tmpName, '/'); atUint32 depth = Athena::utility::countChar(tmpName, '/');
bool owned = false; bool owned = false;
while ((depth--) > 0) while ((depth--) > 0)
{ {
// add them from the beginning of the path up // add them from the beginning of the path up
tmpName = tmpName.substr(0, tmpName.find('/')); tmpName = tmpName.substr(0, tmpName.find('/'));
for (atUint32 i = 0; i < m_children.size(); i++) for (atUint32 i = 0; i < m_children.size(); i++)
{ {
if (!m_children[i]->filename().compare(tmpName)) if (!m_children[i]->filename().compare(tmpName))
@ -181,6 +184,7 @@ WiiFile* WiiFile::child(const std::string &name)
{ {
std::vector<WiiFile*>::iterator iter = std::find_if(m_children.begin(), m_children.end(), std::vector<WiiFile*>::iterator iter = std::find_if(m_children.begin(), m_children.end(),
[&name](WiiFile * f) { return !f->filename().compare(name); }); [&name](WiiFile * f) { return !f->filename().compare(name); });
if (iter != m_children.end()) if (iter != m_children.end())
return *iter; return *iter;
@ -193,6 +197,7 @@ WiiFile* WiiFile::child(const std::string &name)
continue; continue;
WiiFile* ret = f->child(tmpName); WiiFile* ret = f->child(tmpName);
if (ret) if (ret)
return ret; return ret;
} }
@ -242,8 +247,10 @@ atUint32 WiiFile::fileCount()
std::vector<WiiFile*> WiiFile::allChildren() std::vector<WiiFile*> WiiFile::allChildren()
{ {
std::vector<WiiFile*> ret; std::vector<WiiFile*> ret;
if (m_children.size() == 0) if (m_children.size() == 0)
return ret; return ret;
// Add our children first // Add our children first
for (WiiFile* f : m_children) for (WiiFile* f : m_children)
ret.push_back(f); ret.push_back(f);
@ -268,6 +275,7 @@ std::vector<WiiFile *> WiiFile::allChildren()
std::string WiiFile::fullpath() std::string WiiFile::fullpath()
{ {
std::string ret; std::string ret;
if (m_parent) if (m_parent)
ret = m_parent->filename() + "/"; ret = m_parent->filename() + "/";

View File

@ -33,6 +33,7 @@ WiiImage::~WiiImage()
{ {
if (m_data) if (m_data)
delete[] m_data; delete[] m_data;
m_data = NULL; m_data = NULL;
} }
@ -59,6 +60,7 @@ atUint8 *WiiImage::toRGBA()
atUint8* bitmapdata = NULL; atUint8* bitmapdata = NULL;
bitmapdata = new atUint8[m_width * m_height * 4]; bitmapdata = new atUint8[m_width * m_height * 4];
if (bitmapdata == NULL) if (bitmapdata == NULL)
return NULL; return NULL;
@ -74,6 +76,7 @@ atUint8 *WiiImage::toRGBA()
//if((x >= m_width) || (y >= m_height)) //if((x >= m_width) || (y >= m_height))
// continue; // continue;
oldpixel = utility::swapU16(oldpixel); oldpixel = utility::swapU16(oldpixel);
if (oldpixel & (1 << 15)) if (oldpixel & (1 << 15))
{ {
// RGB5 // RGB5
@ -98,6 +101,7 @@ atUint8 *WiiImage::toRGBA()
} }
} }
} }
return bitmapdata; return bitmapdata;
} }

View File

@ -52,12 +52,14 @@ WiiSaveReader::WiiSaveReader(const std::string& filename)
WiiSave* WiiSaveReader::readSave() WiiSave* WiiSaveReader::readSave()
{ {
WiiSave* ret = new WiiSave; WiiSave* ret = new WiiSave;
try try
{ {
if (length() < 0xF0C0) if (length() < 0xF0C0)
THROW_INVALID_DATA_EXCEPTION_RETURN(nullptr, "Not a valid WiiSave"); THROW_INVALID_DATA_EXCEPTION_RETURN(nullptr, "Not a valid WiiSave");
WiiBanner* banner = this->readBanner(); WiiBanner* banner = this->readBanner();
if (!banner) if (!banner)
THROW_INVALID_DATA_EXCEPTION_RETURN(nullptr, "Invalid banner"); THROW_INVALID_DATA_EXCEPTION_RETURN(nullptr, "Invalid banner");
@ -68,6 +70,7 @@ WiiSave* WiiSaveReader::readSave()
THROW_INVALID_DATA_EXCEPTION_RETURN(nullptr, "Invalid BacKup header size"); THROW_INVALID_DATA_EXCEPTION_RETURN(nullptr, "Invalid BacKup header size");
atUint32 bkMagic = base::readUint32(); atUint32 bkMagic = base::readUint32();
if (bkMagic != 0x426B0001) if (bkMagic != 0x426B0001)
THROW_INVALID_DATA_EXCEPTION_RETURN(nullptr, "Invalid BacKup header magic"); THROW_INVALID_DATA_EXCEPTION_RETURN(nullptr, "Invalid BacKup header magic");
@ -85,9 +88,11 @@ WiiSave* WiiSaveReader::readSave()
base::seek(0x10); base::seek(0x10);
std::vector<WiiFile*> files; std::vector<WiiFile*> files;
for (atUint32 i = 0; i < numFiles; ++i) for (atUint32 i = 0; i < numFiles; ++i)
{ {
WiiFile* file = readFile(); WiiFile* file = readFile();
if (file) if (file)
files.push_back(file); files.push_back(file);
} }
@ -141,18 +146,23 @@ WiiBanner* WiiSaveReader::readBanner()
std::cerr << "MD5 Mismatch" << std::endl; std::cerr << "MD5 Mismatch" << std::endl;
// Make sure to reset m_reader values back to the old ones. // Make sure to reset m_reader values back to the old ones.
std::cerr << "MD5 provided: "; std::cerr << "MD5 provided: ";
for (int i = 0; i < 16; ++i) for (int i = 0; i < 16; ++i)
std::cerr << std::setw(2) << std::setfill('0') << std::hex << (int)(md5[i]); std::cerr << std::setw(2) << std::setfill('0') << std::hex << (int)(md5[i]);
std::cerr << std::endl; std::cerr << std::endl;
std::cerr << "MD5 Calculated: "; std::cerr << "MD5 Calculated: ";
for (int i = 0; i < 16; ++i) for (int i = 0; i < 16; ++i)
std::cerr << std::hex << (int)(md5Calc[i]); std::cerr << std::hex << (int)(md5Calc[i]);
std::cerr << std::endl; std::cerr << std::endl;
base::setData(oldData, oldLen); base::setData(oldData, oldLen);
base::seek(oldPos, SeekOrigin::Begin); base::seek(oldPos, SeekOrigin::Begin);
THROW_INVALID_DATA_EXCEPTION_RETURN(nullptr, "MD5 Mismatch"); THROW_INVALID_DATA_EXCEPTION_RETURN(nullptr, "MD5 Mismatch");
} }
// Set the binary reader buffer; // Set the binary reader buffer;
base::setData(dec, 0xF0C0); base::setData(dec, 0xF0C0);
// Start reading the header // Start reading the header
@ -186,10 +196,12 @@ WiiBanner* WiiSaveReader::readBanner()
base::seek(22); base::seek(22);
gameTitle = base::readUnicode(); gameTitle = base::readUnicode();
if (base::position() != 0x0080) if (base::position() != 0x0080)
base::seek(0x0080, SeekOrigin::Begin); base::seek(0x0080, SeekOrigin::Begin);
subTitle = base::readUnicode(); subTitle = base::readUnicode();
if (base::position() != 0x00C0) if (base::position() != 0x00C0)
base::seek(0x00C0, SeekOrigin::Begin); base::seek(0x00C0, SeekOrigin::Begin);
@ -208,6 +220,7 @@ WiiBanner* WiiSaveReader::readBanner()
if (banner->bannerSize() == 0x72a0) if (banner->bannerSize() == 0x72a0)
{ {
WiiImage* icon = readImage(48, 48); WiiImage* icon = readImage(48, 48);
if (icon) if (icon)
banner->addIcon(icon); banner->addIcon(icon);
else else
@ -218,6 +231,7 @@ WiiBanner* WiiSaveReader::readBanner()
for (int i = 0; i < 8; i++) for (int i = 0; i < 8; i++)
{ {
WiiImage* icon = readImage(48, 48); WiiImage* icon = readImage(48, 48);
if (icon) if (icon)
banner->addIcon(icon); banner->addIcon(icon);
else else
@ -252,6 +266,7 @@ WiiFile* WiiSaveReader::readFile()
WiiFile* ret; WiiFile* ret;
atUint32 magic = base::readUint32(); atUint32 magic = base::readUint32();
if (magic != 0x03adf17e) if (magic != 0x03adf17e)
{ {
std::cerr << "Not a valid File entry header: 0x" << std::hex << magic << std::endl; std::cerr << "Not a valid File entry header: 0x" << std::hex << magic << std::endl;

View File

@ -56,6 +56,7 @@ bool WiiSaveWriter::writeSave(WiiSave *save, atUint8 *macAddress, atUint32 ngId,
{ {
if (!save) if (!save)
THROW_INVALID_OPERATION_EXCEPTION_RETURN(false, "save cannot be NULL"); THROW_INVALID_OPERATION_EXCEPTION_RETURN(false, "save cannot be NULL");
if (filepath != "") if (filepath != "")
m_filepath = filepath; m_filepath = filepath;
@ -74,10 +75,12 @@ bool WiiSaveWriter::writeSave(WiiSave *save, atUint8 *macAddress, atUint32 ngId,
base::seek(2); // unknown; base::seek(2); // unknown;
base::seek(0x10); // padding; base::seek(0x10); // padding;
atUint32 totalSize = 0; atUint32 totalSize = 0;
for (WiiFile* file : save->allFiles()) for (WiiFile* file : save->allFiles())
{ {
totalSize += writeFile(file); totalSize += writeFile(file);
} }
atUint64 pos = base::position(); atUint64 pos = base::position();
// Write size data // Write size data
base::seek(0xF0C0 + 0x10, SeekOrigin::Begin); base::seek(0xF0C0 + 0x10, SeekOrigin::Begin);
@ -123,6 +126,7 @@ void WiiSaveWriter::writeBanner(WiiBanner *banner)
// For empty icons // For empty icons
atUint8* tmpIcon = new atUint8[48 * 48 * 2]; atUint8* tmpIcon = new atUint8[48 * 48 * 2];
memset(tmpIcon, 0, 48 * 48 * 2); memset(tmpIcon, 0, 48 * 48 * 2);
for (atUint32 i = 0; i < 8; ++i) for (atUint32 i = 0; i < 8; ++i)
{ {
if (i < banner->icons().size()) if (i < banner->icons().size())
@ -244,6 +248,7 @@ void WiiSaveWriter::writeCerts(atUint32 filesSize, atUint32 ngId, atUint8 *ngPri
generate_ecdsa(sig, sig + 30, apPriv, hash2); generate_ecdsa(sig, sig + 30, apPriv, hash2);
int stuff = 0x2f536969; int stuff = 0x2f536969;
if (!utility::isSystemBigEndian()) if (!utility::isSystemBigEndian())
stuff = utility::swap32(stuff); stuff = utility::swap32(stuff);

View File

@ -70,6 +70,7 @@ ZQuestFile::ZQuestFile(ZQuestFile::Game game, Endian endian, atUint8* data, atUi
m_length(length) m_length(length)
{ {
initGameStrings(); initGameStrings();
if (gameString.empty() && (m_game < GameStrings.size() - 1)) if (gameString.empty() && (m_game < GameStrings.size() - 1))
m_gameString = GameStrings[m_game]; m_gameString = GameStrings[m_game];
} }
@ -84,6 +85,7 @@ ZQuestFile::~ZQuestFile()
void ZQuestFile::setGame(ZQuestFile::Game game) void ZQuestFile::setGame(ZQuestFile::Game game)
{ {
m_game = game; m_game = game;
if (m_game > GameStrings.size() - 1) if (m_game > GameStrings.size() - 1)
return; return;
@ -145,6 +147,7 @@ const std::vector<std::string> ZQuestFile::gameStringList()
{ {
if (GameStrings.size() <= 0) if (GameStrings.size() <= 0)
initGameStrings(); initGameStrings();
return GameStrings; return GameStrings;
} }
} }

View File

@ -65,6 +65,7 @@ ZQuestFile *ZQuestFileReader::read()
if (version >= ZQUEST_VERSION_CHECK(2, 0, 0)) if (version >= ZQUEST_VERSION_CHECK(2, 0, 0))
{ {
gameString = std::string((const char*)base::readBytes(0x0A), 0x0A); gameString = std::string((const char*)base::readBytes(0x0A), 0x0A);
for (size_t i = 0; i < ZQuestFile::gameStringList().size(); i++) for (size_t i = 0; i < ZQuestFile::gameStringList().size(); i++)
{ {
if (!ZQuestFile::gameStringList().at(i).substr(0, 0x0A).compare(gameString)) if (!ZQuestFile::gameStringList().at(i).substr(0, 0x0A).compare(gameString))
@ -74,6 +75,7 @@ ZQuestFile *ZQuestFileReader::read()
break; break;
} }
} }
BOM = base::readUint16(); BOM = base::readUint16();
checksum = base::readUint32(); checksum = base::readUint32();
} }

View File

@ -44,6 +44,7 @@ void ZQuestFileWriter::write(ZQuestFile* quest, bool compress)
base::writeUint32(ZQuestFile::Version); base::writeUint32(ZQuestFile::Version);
atUint8* questData = quest->data(); atUint8* questData = quest->data();
atUint32 compLen; atUint32 compLen;
if (compress) if (compress)
{ {
atUint8* compData = new atUint8[quest->length() + 0x40]; // add 20 bytes because sometimes the file grows with compression atUint8* compData = new atUint8[quest->length() + 0x40]; // add 20 bytes because sometimes the file grows with compression
@ -79,6 +80,7 @@ void ZQuestFileWriter::write(ZQuestFile* quest, bool compress)
base::writeUBytes(questData, compLen); base::writeUBytes(questData, compLen);
base::save(); base::save();
// Delete compressed data to prevent memory leaks // Delete compressed data to prevent memory leaks
if (questData != quest->data()) if (questData != quest->data())
{ {

View File

@ -70,6 +70,7 @@ LZLengthOffset LZBase::search(atUint8* posPtr, atUint8* dataBegin, atUint8* data
//to be compressed else the number of remaining bytes is the LookAheadBuffer //to be compressed else the number of remaining bytes is the LookAheadBuffer
int lookAheadBuffer_len = ((int)(dataEnd - posPtr) < m_readAheadBuffer) ? (int)(dataEnd - posPtr) : m_readAheadBuffer; int lookAheadBuffer_len = ((int)(dataEnd - posPtr) < m_readAheadBuffer) ? (int)(dataEnd - posPtr) : m_readAheadBuffer;
int slidingBuffer = (int)(posPtr - dataBegin) - m_slidingWindow; int slidingBuffer = (int)(posPtr - dataBegin) - m_slidingWindow;
if (slidingBuffer > 0) if (slidingBuffer > 0)
searchWindow = dataBegin + slidingBuffer; searchWindow = dataBegin + slidingBuffer;
else else
@ -79,6 +80,7 @@ LZLengthOffset LZBase::search(atUint8* posPtr, atUint8* dataBegin, atUint8* data
if (!((posPtr - dataBegin < 1) || (dataEnd - posPtr < m_minMatch))) if (!((posPtr - dataBegin < 1) || (dataEnd - posPtr < m_minMatch)))
results = windowSearch(searchWindow, posPtr, endPos, posPtr - m_minOffset); results = windowSearch(searchWindow, posPtr, endPos, posPtr - m_minOffset);
return results; return results;
} }
@ -116,6 +118,7 @@ LZLengthOffset LZBase::windowSearch(atUint8* beginSearchPtr, atUint8* searchPosP
atInt32 n = (atUint32)(endLABufferPtr - searchPosPtr); atInt32 n = (atUint32)(endLABufferPtr - searchPosPtr);
LZLengthOffset result = {0, 0}; LZLengthOffset result = {0, 0};
atInt32 temp = 0; atInt32 temp = 0;
if (n > size) //If the string that is being looked for is bigger than the string that is being searched if (n > size) //If the string that is being looked for is bigger than the string that is being searched
return result; return result;
@ -127,11 +130,13 @@ LZLengthOffset LZBase::windowSearch(atUint8* beginSearchPtr, atUint8* searchPosP
do do
{ {
temp = subMatch(startLBPtr, searchPosPtr, n); temp = subMatch(startLBPtr, searchPosPtr, n);
if (result.length < (atUint32)temp) if (result.length < (atUint32)temp)
{ {
result.length = temp; result.length = temp;
result.offset = (atInt32)(searchPosPtr - startLBPtr); result.offset = (atInt32)(searchPosPtr - startLBPtr);
} }
if (result.length == (atUint32)n) if (result.length == (atUint32)n)
return result; return result;
@ -139,6 +144,7 @@ LZLengthOffset LZBase::windowSearch(atUint8* beginSearchPtr, atUint8* searchPosP
} }
while ((startLBPtr--) > beginSearchPtr); while ((startLBPtr--) > beginSearchPtr);
return result; return result;
} }

View File

@ -15,14 +15,17 @@ LZLookupTable::LZLookupTable(atInt32 minimumMatch, atInt32 slidingWindow, atInt3
m_minimumMatch = minimumMatch; m_minimumMatch = minimumMatch;
else else
m_minimumMatch = 3; m_minimumMatch = 3;
if (slidingWindow > 0) if (slidingWindow > 0)
m_slidingWindow = slidingWindow; m_slidingWindow = slidingWindow;
else else
m_slidingWindow = 4096; m_slidingWindow = 4096;
if (lookAheadWindow > 0) if (lookAheadWindow > 0)
m_lookAheadWindow = lookAheadWindow; m_lookAheadWindow = lookAheadWindow;
else else
m_lookAheadWindow = 18; m_lookAheadWindow = 18;
m_buffer.reserve(m_minimumMatch); m_buffer.reserve(m_minimumMatch);
} }
@ -40,30 +43,36 @@ void LZLookupTable::setLookAheadWindow(atInt32 lookAheadWindow)
LZLengthOffset LZLookupTable::search(atUint8* curPos, const atUint8* dataBegin, const atUint8* dataEnd) LZLengthOffset LZLookupTable::search(atUint8* curPos, const atUint8* dataBegin, const atUint8* dataEnd)
{ {
LZLengthOffset loPair = {0, 0}; LZLengthOffset loPair = {0, 0};
//Returns negative 1 for search failures since the current position is passed the size to be compressed //Returns negative 1 for search failures since the current position is passed the size to be compressed
if (curPos >= dataEnd) if (curPos >= dataEnd)
{ {
loPair.length = -1; loPair.length = -1;
return loPair; return loPair;
} }
std::copy(curPos, curPos + m_minimumMatch, m_buffer.begin()); std::copy(curPos, curPos + m_minimumMatch, m_buffer.begin());
int32_t currentOffset = static_cast<atInt32>(curPos - dataBegin); int32_t currentOffset = static_cast<atInt32>(curPos - dataBegin);
//Find code //Find code
if (currentOffset > 0 && (dataEnd - curPos) >= m_minimumMatch) if (currentOffset > 0 && (dataEnd - curPos) >= m_minimumMatch)
{ {
auto elements = table.equal_range(m_buffer); auto elements = table.equal_range(m_buffer);
elements.second--; elements.second--;
elements.first--; elements.first--;
//Iterate over keys in reverse order. C++11 guarantees that the relative order of elements is maintained for the same key //Iterate over keys in reverse order. C++11 guarantees that the relative order of elements is maintained for the same key
for (auto iter = elements.second; iter != elements.first; iter--) for (auto iter = elements.second; iter != elements.first; iter--)
{ {
int32_t matchLength = m_minimumMatch; int32_t matchLength = m_minimumMatch;
int32_t lookAheadBufferLength = ((dataEnd - curPos) < m_lookAheadWindow) ? static_cast<int32_t>(dataEnd - curPos) : m_lookAheadWindow; int32_t lookAheadBufferLength = ((dataEnd - curPos) < m_lookAheadWindow) ? static_cast<int32_t>(dataEnd - curPos) : m_lookAheadWindow;
for (; matchLength < lookAheadBufferLength; ++matchLength) for (; matchLength < lookAheadBufferLength; ++matchLength)
{ {
if (*(dataBegin + iter->second + matchLength) != *(curPos + matchLength)) if (*(dataBegin + iter->second + matchLength) != *(curPos + matchLength))
break; break;
} }
//Store the longest match found so far into length_offset struct. //Store the longest match found so far into length_offset struct.
//When lengths are the same the closer offset to the lookahead buffer wins //When lengths are the same the closer offset to the lookahead buffer wins
if (loPair.length < (atUint32)matchLength) if (loPair.length < (atUint32)matchLength)
@ -71,30 +80,37 @@ LZLengthOffset LZLookupTable::search(atUint8* curPos, const atUint8* dataBegin,
loPair.length = matchLength; loPair.length = matchLength;
loPair.offset = currentOffset - iter->second; loPair.offset = currentOffset - iter->second;
} }
//Found the longest match so break out of loop //Found the longest match so break out of loop
if (loPair.length == (atUint32)m_lookAheadWindow) if (loPair.length == (atUint32)m_lookAheadWindow)
break; break;
} }
} }
//end find code //end find code
//Insert code //Insert code
table.insert(std::make_pair(m_buffer, currentOffset)); table.insert(std::make_pair(m_buffer, currentOffset));
for (atUint32 i = 1; i < loPair.length; i++) for (atUint32 i = 1; i < loPair.length; i++)
{ {
if (dataEnd - (curPos + i) < m_minimumMatch) if (dataEnd - (curPos + i) < m_minimumMatch)
break; break;
std::copy(curPos + i, curPos + m_minimumMatch + i, m_buffer.begin()); std::copy(curPos + i, curPos + m_minimumMatch + i, m_buffer.begin());
table.insert(std::make_pair(m_buffer, currentOffset + i)); table.insert(std::make_pair(m_buffer, currentOffset + i));
} }
//end insert code //end insert code
//Delete code //Delete code
int32_t slidingWindowOffset = std::max(0, currentOffset - m_slidingWindow);//Absolute offset int32_t slidingWindowOffset = std::max(0, currentOffset - m_slidingWindow);//Absolute offset
int32_t tablesize = static_cast<int32_t>(table.size()); int32_t tablesize = static_cast<int32_t>(table.size());
for (int32_t i = 0; i < tablesize - m_slidingWindow; ++i) for (int32_t i = 0; i < tablesize - m_slidingWindow; ++i)
{ {
std::copy(dataBegin + slidingWindowOffset + i, dataBegin + slidingWindowOffset + m_minimumMatch + i, m_buffer.begin()); std::copy(dataBegin + slidingWindowOffset + i, dataBegin + slidingWindowOffset + m_minimumMatch + i, m_buffer.begin());
auto elements = table.equal_range(m_buffer); auto elements = table.equal_range(m_buffer);
for (auto iter = elements.first; iter != elements.second; iter++) for (auto iter = elements.first; iter != elements.second; iter++)
{ {
if (slidingWindowOffset + i == iter->second) if (slidingWindowOffset + i == iter->second)
@ -105,6 +121,7 @@ LZLengthOffset LZLookupTable::search(atUint8* curPos, const atUint8* dataBegin,
} }
} }
} }
//end delete code //end delete code
return loPair; return loPair;
//break lookupTable.cpp:109 if table.size()> 4096 //break lookupTable.cpp:109 if table.size()> 4096

View File

@ -24,12 +24,14 @@ atUint32 LZType10::compress(const atUint8* src, atUint8** dstBuf, atUint32 srcLe
//At most their will be two bytes written if the bytes can be compressed. So if all bytes in the block can be compressed it would take blockSize*2 bytes //At most their will be two bytes written if the bytes can be compressed. So if all bytes in the block can be compressed it would take blockSize*2 bytes
atUint8* compressedBytes = new atUint8[m_blockSize * 2]; //Holds the compressed bytes yet to be written atUint8* compressedBytes = new atUint8[m_blockSize * 2]; //Holds the compressed bytes yet to be written
while (ptrStart < ptrEnd) while (ptrStart < ptrEnd)
{ {
atUint8 blockLen = 0; atUint8 blockLen = 0;
//In Binary represents 1 if byte is compressed or 0 if not compressed //In Binary represents 1 if byte is compressed or 0 if not compressed
//For example 01001000 means that the second and fifth byte in the blockSize from the left is compressed //For example 01001000 means that the second and fifth byte in the blockSize from the left is compressed
atUint8* ptrBytes = compressedBytes; atUint8* ptrBytes = compressedBytes;
for (atInt32 i = 0; i < m_blockSize; i++) for (atInt32 i = 0; i < m_blockSize; i++)
{ {
//length_offset searchResult=Search(ptrStart, filedata, ptrEnd); //length_offset searchResult=Search(ptrStart, filedata, ptrEnd);
@ -37,7 +39,8 @@ atUint32 LZType10::compress(const atUint8* src, atUint8** dstBuf, atUint32 srcLe
//If the number of bytes to be compressed is at least the size of the Minimum match //If the number of bytes to be compressed is at least the size of the Minimum match
if (searchResult.length >= (atUint32)m_minMatch) if (searchResult.length >= (atUint32)m_minMatch)
{ //Gotta swap the bytes since system is wii is big endian and most computers are little endian {
//Gotta swap the bytes since system is wii is big endian and most computers are little endian
atUint16 lenOff = (((searchResult.length - m_minMatch) & 0xF) << 12) | ((searchResult.offset - 1) & 0xFFF); atUint16 lenOff = (((searchResult.length - m_minMatch) & 0xF) << 12) | ((searchResult.offset - 1) & 0xFFF);
Athena::utility::BigUint16(lenOff); Athena::utility::BigUint16(lenOff);
@ -54,10 +57,12 @@ atUint32 LZType10::compress(const atUint8* src, atUint8** dstBuf, atUint32 srcLe
else else
*ptrBytes++ = *ptrStart++; *ptrBytes++ = *ptrStart++;
} }
outbuf.writeByte(blockLen); outbuf.writeByte(blockLen);
outbuf.writeUBytes(compressedBytes, (atUint64)(ptrBytes - compressedBytes)); outbuf.writeUBytes(compressedBytes, (atUint64)(ptrBytes - compressedBytes));
} }
delete[] compressedBytes; delete[] compressedBytes;
compressedBytes = nullptr; compressedBytes = nullptr;
@ -84,6 +89,7 @@ atUint32 LZType10::decompress(const atUint8* src, atUint8** dst, atUint32 srcLen
atUint8* outputEndPtr = uncompressedData + uncompressedSize; atUint8* outputEndPtr = uncompressedData + uncompressedSize;
atUint8* inputPtr = (atUint8*)src + 4; atUint8* inputPtr = (atUint8*)src + 4;
atUint8* inputEndPtr = (atUint8*)src + srcLength; atUint8* inputEndPtr = (atUint8*)src + srcLength;
while (inputPtr < inputEndPtr && outputPtr < outputEndPtr) while (inputPtr < inputEndPtr && outputPtr < outputEndPtr)
{ {
@ -107,7 +113,8 @@ atUint32 LZType10::decompress(const atUint8* src, atUint8** dst, atUint32 srcLen
if ((outputPtr - decoding.offset) < uncompressedData) if ((outputPtr - decoding.offset) < uncompressedData)
{//If the offset to look for uncompressed is passed the current uncompresed data then the data is not compressed {
//If the offset to look for uncompressed is passed the current uncompresed data then the data is not compressed
delete[] uncompressedData; delete[] uncompressedData;
uncompressedData = nullptr; uncompressedData = nullptr;
dst = nullptr; dst = nullptr;
@ -116,6 +123,7 @@ atUint32 LZType10::decompress(const atUint8* src, atUint8** dst, atUint32 srcLen
for (atUint32 j = 0; j < decoding.length; ++j) for (atUint32 j = 0; j < decoding.length; ++j)
outputPtr[j] = (outputPtr - decoding.offset)[j]; outputPtr[j] = (outputPtr - decoding.offset)[j];
outputPtr += decoding.length; outputPtr += decoding.length;
} }
else else

View File

@ -14,7 +14,9 @@ LZType11::LZType11(atInt32 minimumOffset, atInt32 slidingWindow, atInt32 minimum
atUint32 LZType11::compress(const atUint8* src, atUint8** dst, atUint32 srcLength) atUint32 LZType11::compress(const atUint8* src, atUint8** dst, atUint32 srcLength)
{ {
Athena::io::MemoryWriter outbuff("tmp"); Athena::io::MemoryWriter outbuff("tmp");
if (srcLength>0xFFFFFF){// If length is greater than 24 bits or 16 Megs
if (srcLength > 0xFFFFFF) // If length is greater than 24 bits or 16 Megs
{
atUint32 encodeFlag = 0x11; atUint32 encodeFlag = 0x11;
Athena::utility::LittleUint32(encodeFlag); Athena::utility::LittleUint32(encodeFlag);
Athena::utility::LittleUint32(srcLength);//Filesize data is little endian Athena::utility::LittleUint32(srcLength);//Filesize data is little endian
@ -22,7 +24,8 @@ atUint32 LZType11::compress(const atUint8* src, atUint8** dst, atUint32 srcLengt
outbuff.writeUint32(srcLength); outbuff.writeUint32(srcLength);
} }
else{ else
{
atUint32 encodeSize = (srcLength << 8) | (0x11); atUint32 encodeSize = (srcLength << 8) | (0x11);
Athena::utility::LittleUint32(encodeSize); Athena::utility::LittleUint32(encodeSize);
outbuff.writeUint32(encodeSize); outbuff.writeUint32(encodeSize);
@ -39,6 +42,7 @@ atUint32 LZType11::compress(const atUint8* src, atUint8** dst, atUint32 srcLengt
atUint16 maxThreeByteMatch = 0xFF + minThreeByteMatch; atUint16 maxThreeByteMatch = 0xFF + minThreeByteMatch;
atUint16 minFourByteMatch = maxThreeByteMatch + 1; //Minimum Four byte match is maximum Three Byte match + 1 atUint16 minFourByteMatch = maxThreeByteMatch + 1; //Minimum Four byte match is maximum Three Byte match + 1
atInt32 maxFourByteMatch = 0xFFFF + minFourByteMatch; atInt32 maxFourByteMatch = 0xFFFF + minFourByteMatch;
/* /*
Normaliazation Example: If MIN_MATCH is 3 then 3 gets mapped to 2 and 16 gets mapped to 15. Normaliazation Example: If MIN_MATCH is 3 then 3 gets mapped to 2 and 16 gets mapped to 15.
17 gets mapped to 1 and 272 gets mapped to 255 17 gets mapped to 1 and 272 gets mapped to 255
@ -59,15 +63,19 @@ atUint32 LZType11::compress(const atUint8* src, atUint8** dst, atUint32 srcLengt
//In Binary represents 1 if byte is compressed or 0 if not compressed //In Binary represents 1 if byte is compressed or 0 if not compressed
//For example 01001000 means that the second and fifth byte in the blockSize from the left is compressed //For example 01001000 means that the second and fifth byte in the blockSize from the left is compressed
atUint8* ptrBytes = compressedBytes; atUint8* ptrBytes = compressedBytes;
for (atInt32 i = 0; i < m_blockSize; i++) for (atInt32 i = 0; i < m_blockSize; i++)
{ {
//length_offset searchResult=Search(filedata,ptrStart,ptrEnd); //length_offset searchResult=Search(filedata,ptrStart,ptrEnd);
LZLengthOffset searchResult = m_lookupTable.search(ptrStart, src, ptrEnd); LZLengthOffset searchResult = m_lookupTable.search(ptrStart, src, ptrEnd);
//If the number of bytes to be compressed is at least the size of the Minimum match //If the number of bytes to be compressed is at least the size of the Minimum match
if (searchResult.length >= (atUint32)m_minMatch) if (searchResult.length >= (atUint32)m_minMatch)
{ //Gotta swap the bytes since system is wii is big endian and most computers are little endian {
//Gotta swap the bytes since system is wii is big endian and most computers are little endian
if(searchResult.length <= maxTwoByteMatch){ if (searchResult.length <= maxTwoByteMatch)
{
atUint16 lenOff = ((((searchResult.length - 1) & 0xF) << 12) | //Bits 15-12 atUint16 lenOff = ((((searchResult.length - 1) & 0xF) << 12) | //Bits 15-12
((searchResult.offset - 1) & 0xFFF) //Bits 11-0 ((searchResult.offset - 1) & 0xFFF) //Bits 11-0
); );
@ -75,7 +83,8 @@ atUint32 LZType11::compress(const atUint8* src, atUint8** dst, atUint32 srcLengt
memcpy(ptrBytes, &lenOff, 2); memcpy(ptrBytes, &lenOff, 2);
ptrBytes += 2; ptrBytes += 2;
} }
else if(searchResult.length <= maxThreeByteMatch){ else if (searchResult.length <= maxThreeByteMatch)
{
atUint32 lenOff = ((((searchResult.length - minThreeByteMatch) & 0xFF) << 12) | //Bits 20-12 atUint32 lenOff = ((((searchResult.length - minThreeByteMatch) & 0xFF) << 12) | //Bits 20-12
((searchResult.offset - 1) & 0xFFF) //Bits 11-0 ((searchResult.offset - 1) & 0xFFF) //Bits 11-0
); );
@ -83,7 +92,8 @@ atUint32 LZType11::compress(const atUint8* src, atUint8** dst, atUint32 srcLengt
memcpy(ptrBytes, (atUint8*)&lenOff + 1, 3); //Make sure to copy the lower 24 bits. 0x12345678- This statement copies 0x123456 memcpy(ptrBytes, (atUint8*)&lenOff + 1, 3); //Make sure to copy the lower 24 bits. 0x12345678- This statement copies 0x123456
ptrBytes += 3; ptrBytes += 3;
} }
else if(searchResult.length <= (atUint32)maxFourByteMatch){ else if (searchResult.length <= (atUint32)maxFourByteMatch)
{
atUint32 lenOff = ((1 << 28) | //Bits 31-28 Flag to say that this is four bytes atUint32 lenOff = ((1 << 28) | //Bits 31-28 Flag to say that this is four bytes
(((searchResult.length - minFourByteMatch) & 0xFFFF) << 12) | //Bits 28-12 (((searchResult.length - minFourByteMatch) & 0xFFFF) << 12) | //Bits 28-12
((searchResult.offset - 1) & 0xFFF) //Bits 11-0 ((searchResult.offset - 1) & 0xFFF) //Bits 11-0
@ -92,6 +102,7 @@ atUint32 LZType11::compress(const atUint8* src, atUint8** dst, atUint32 srcLengt
memcpy(ptrBytes, &lenOff, 4); memcpy(ptrBytes, &lenOff, 4);
ptrBytes += 4; ptrBytes += 4;
} }
ptrStart += searchResult.length; ptrStart += searchResult.length;
blockSize |= (1 << (7 - i)); blockSize |= (1 << (7 - i));
@ -102,10 +113,12 @@ atUint32 LZType11::compress(const atUint8* src, atUint8** dst, atUint32 srcLengt
*ptrBytes++ = *ptrStart++; *ptrBytes++ = *ptrStart++;
} }
outbuff.writeByte(blockSize); outbuff.writeByte(blockSize);
outbuff.writeUBytes(compressedBytes, (atUint64)(ptrBytes - compressedBytes)); outbuff.writeUBytes(compressedBytes, (atUint64)(ptrBytes - compressedBytes));
} }
delete []compressedBytes; delete []compressedBytes;
compressedBytes = NULL; compressedBytes = NULL;
@ -126,6 +139,7 @@ atUint32 LZType11::decompress(const atUint8* src, atUint8** dst, atUint32 srcLen
Athena::utility::LittleUint32(uncompressedLen);//The compressed file has the filesize encoded in little endian Athena::utility::LittleUint32(uncompressedLen);//The compressed file has the filesize encoded in little endian
uncompressedLen = uncompressedLen >> 8; //First byte is the encode flag uncompressedLen = uncompressedLen >> 8; //First byte is the encode flag
atUint32 currentOffset = 4; atUint32 currentOffset = 4;
if (uncompressedLen == 0) //If the filesize var is zero then the true filesize is over 14MB and must be read in from the next 4 bytes if (uncompressedLen == 0) //If the filesize var is zero then the true filesize is over 14MB and must be read in from the next 4 bytes
{ {
atUint32 filesize = *(atUint32*)(src + 4); atUint32 filesize = *(atUint32*)(src + 4);
@ -158,7 +172,9 @@ atUint32 LZType11::decompress(const atUint8* src, atUint8** dst, atUint32 srcLen
if ((isCompressed >> (7 - i)) & 0x1) if ((isCompressed >> (7 - i)) & 0x1)
{ {
atUint8 metaDataSize = *inputPtr >> 4; //Look at the top 4 bits atUint8 metaDataSize = *inputPtr >> 4; //Look at the top 4 bits
if(metaDataSize >= 2){ //Two Bytes of Length/Offset MetaData
if (metaDataSize >= 2) //Two Bytes of Length/Offset MetaData
{
atUint16 lenOff = 0; atUint16 lenOff = 0;
memcpy(&lenOff, inputPtr, 2); memcpy(&lenOff, inputPtr, 2);
inputPtr += 2; inputPtr += 2;
@ -166,7 +182,8 @@ atUint32 LZType11::decompress(const atUint8* src, atUint8** dst, atUint32 srcLen
decoding.length = (lenOff >> 12) + 1; decoding.length = (lenOff >> 12) + 1;
decoding.offset = (lenOff & 0xFFF) + 1; decoding.offset = (lenOff & 0xFFF) + 1;
} }
else if (metaDataSize==0){ //Three Bytes of Length/Offset MetaData else if (metaDataSize == 0) //Three Bytes of Length/Offset MetaData
{
atUint32 lenOff = 0; atUint32 lenOff = 0;
memcpy((atUint8*)&lenOff + 1, inputPtr, 3); memcpy((atUint8*)&lenOff + 1, inputPtr, 3);
inputPtr += 3; inputPtr += 3;
@ -174,7 +191,8 @@ atUint32 LZType11::decompress(const atUint8* src, atUint8** dst, atUint32 srcLen
decoding.length = (lenOff >> 12) + threeByteDenorm; decoding.length = (lenOff >> 12) + threeByteDenorm;
decoding.offset = (lenOff & 0xFFF) + 1; decoding.offset = (lenOff & 0xFFF) + 1;
} }
else if(metaDataSize==1){ //Four Bytes of Length/Offset MetaData else if (metaDataSize == 1) //Four Bytes of Length/Offset MetaData
{
atUint32 lenOff = 0; atUint32 lenOff = 0;
memcpy(&lenOff, inputPtr, 4); memcpy(&lenOff, inputPtr, 4);
inputPtr += 4; inputPtr += 4;
@ -183,18 +201,22 @@ atUint32 LZType11::decompress(const atUint8* src, atUint8** dst, atUint32 srcLen
decoding.length = ((lenOff >> 12) & 0xFFFF) + fourByteDenorm; //Gets rid of the Four byte flag decoding.length = ((lenOff >> 12) & 0xFFFF) + fourByteDenorm; //Gets rid of the Four byte flag
decoding.offset = (lenOff & 0xFFF) + 1; decoding.offset = (lenOff & 0xFFF) + 1;
} }
else{ else
{
delete[] uncompressedData; delete[] uncompressedData;
uncompressedData = nullptr; uncompressedData = nullptr;
return 0; return 0;
} }
if((outputPtr - decoding.offset) < uncompressedData){//If the offset to look for uncompressed is passed the current uncompresed data then the data is not compressed if ((outputPtr - decoding.offset) < uncompressedData) //If the offset to look for uncompressed is passed the current uncompresed data then the data is not compressed
{
delete []uncompressedData; delete []uncompressedData;
return 0; return 0;
} }
for (atUint32 j = 0; j < decoding.length; ++j) for (atUint32 j = 0; j < decoding.length; ++j)
outputPtr[j] = (outputPtr - decoding.offset)[j]; outputPtr[j] = (outputPtr - decoding.offset)[j];
outputPtr += decoding.length; outputPtr += decoding.length;
} }
else else

Some files were not shown because too many files have changed in this diff Show More