mirror of https://github.com/libAthena/athena.git
* Fix styling
This commit is contained in:
parent
423a9a37d2
commit
6ee11b9a08
|
@ -366,9 +366,18 @@ LZO_EXTERN(const lzo_uint32_tp)
|
|||
|
||||
/* misc. */
|
||||
LZO_EXTERN(int) _lzo_config_check(void);
|
||||
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;
|
||||
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;
|
||||
#if defined(lzo_int64_t)
|
||||
lzo_uint64_t a10;
|
||||
#endif
|
||||
|
|
|
@ -160,8 +160,10 @@ lzo1_info ( int *rbits, int *clevel )
|
|||
{
|
||||
if (rbits)
|
||||
*rbits = RBITS;
|
||||
|
||||
if (clevel)
|
||||
*clevel = CLEVEL;
|
||||
|
||||
return D_SIZE * lzo_sizeof(lzo_bytep);
|
||||
}
|
||||
|
||||
|
@ -203,6 +205,7 @@ lzo1_decompress ( const lzo_bytep in , lzo_uint in_len,
|
|||
|
||||
op = out;
|
||||
ip = in;
|
||||
|
||||
while (ip < ip_end)
|
||||
{
|
||||
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 */
|
||||
{
|
||||
t = *ip++;
|
||||
|
||||
if (t >= R0FAST - R0MIN) /* a long R0 run */
|
||||
{
|
||||
t -= R0FAST - R0MIN;
|
||||
|
||||
if (t == 0)
|
||||
t = R0FAST;
|
||||
else
|
||||
|
@ -224,15 +229,22 @@ lzo1_decompress ( const lzo_bytep in , lzo_uint in_len,
|
|||
#else
|
||||
/* help the optimizer */
|
||||
lzo_uint tt = 256;
|
||||
do tt <<= 1; while (--t > 0);
|
||||
|
||||
do tt <<= 1;
|
||||
|
||||
while (--t > 0);
|
||||
|
||||
t = tt;
|
||||
#endif
|
||||
}
|
||||
|
||||
MEMCPY8_DS(op, ip, t);
|
||||
continue;
|
||||
}
|
||||
|
||||
t += R0MIN;
|
||||
}
|
||||
|
||||
MEMCPY_DS(op, ip, t);
|
||||
}
|
||||
else /* a match */
|
||||
|
@ -282,26 +294,33 @@ store_run(lzo_bytep op, const lzo_bytep ii, lzo_uint r_len)
|
|||
if (r_len >= 512)
|
||||
{
|
||||
unsigned r_bits = 7; /* 256 << 7 == 32768 */
|
||||
do {
|
||||
|
||||
do
|
||||
{
|
||||
while (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));
|
||||
}
|
||||
} while (--r_bits > 0);
|
||||
}
|
||||
while (--r_bits > 0);
|
||||
}
|
||||
|
||||
while (r_len >= R0FAST)
|
||||
{
|
||||
r_len -= R0FAST;
|
||||
*op++ = 0; *op++ = R0FAST - R0MIN;
|
||||
*op++ = 0;
|
||||
*op++ = R0FAST - R0MIN;
|
||||
MEMCPY8_DS(op, ii, R0FAST);
|
||||
}
|
||||
|
||||
if (r_len >= R0MIN)
|
||||
{
|
||||
/* 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);
|
||||
}
|
||||
else if (r_len > 0)
|
||||
|
@ -348,6 +367,7 @@ do_compress ( const lzo_bytep in , lzo_uint in_len,
|
|||
op = out;
|
||||
ip = in;
|
||||
ii = ip; /* point to start of literal run */
|
||||
|
||||
if (in_len <= MIN_MATCH_LONG + DVAL_LEN + 1)
|
||||
goto the_end;
|
||||
|
||||
|
@ -361,29 +381,38 @@ do_compress ( const lzo_bytep in , lzo_uint in_len,
|
|||
ip++;
|
||||
DVAL_NEXT(dv, ip);
|
||||
|
||||
do {
|
||||
do
|
||||
{
|
||||
LZO_DEFINE_UNINITIALIZED_VAR(lzo_uint, m_off, 0);
|
||||
lzo_uint dindex;
|
||||
|
||||
DINDEX1(dindex, ip);
|
||||
GINDEX(m_pos, m_off, dict, dindex, in);
|
||||
|
||||
if (LZO_CHECK_MPOS(m_pos, m_off, in, ip, MAX_OFFSET))
|
||||
goto literal;
|
||||
|
||||
if (m_pos[0] == ip[0] && m_pos[1] == ip[1] && m_pos[2] == ip[2])
|
||||
goto match;
|
||||
|
||||
DINDEX2(dindex, ip);
|
||||
GINDEX(m_pos, m_off, dict, dindex, in);
|
||||
|
||||
if (LZO_CHECK_MPOS(m_pos, m_off, in, ip, MAX_OFFSET))
|
||||
goto literal;
|
||||
|
||||
if (m_pos[0] == ip[0] && m_pos[1] == ip[1] && m_pos[2] == ip[2])
|
||||
goto match;
|
||||
|
||||
goto literal;
|
||||
|
||||
|
||||
literal:
|
||||
UPDATE_I(dict, 0, dindex, ip, in);
|
||||
|
||||
if (++ip >= ip_end)
|
||||
break;
|
||||
|
||||
continue;
|
||||
|
||||
match:
|
||||
|
@ -397,11 +426,13 @@ match:
|
|||
#if !defined(NDEBUG) && !(LZO_DICT_USE_PTR)
|
||||
assert((m_pos_sav = ip - m_off) == (m_pos - 3));
|
||||
#endif
|
||||
|
||||
/* 1) store the current literal run */
|
||||
if (pd(ip, ii) > 0)
|
||||
{
|
||||
lzo_uint t = pd(ip, ii);
|
||||
#if 1
|
||||
|
||||
/* OPTIMIZED: inline the copying of a short run */
|
||||
if (t < R0MIN)
|
||||
{
|
||||
|
@ -427,6 +458,7 @@ match:
|
|||
#define PS *m_pos++ != *ip++
|
||||
|
||||
#if (MIN_MATCH_LONG - MIN_MATCH == 2) /* MBITS == 2 */
|
||||
|
||||
if (PS || PS)
|
||||
#elif (MIN_MATCH_LONG - MIN_MATCH == 6) /* MBITS == 3 */
|
||||
if (PS || PS || PS || PS || PS || PS)
|
||||
|
@ -471,10 +503,14 @@ match:
|
|||
#if (CLEVEL == 9) || (CLEVEL >= 7 && MBITS <= 4) || (CLEVEL >= 5 && MBITS <= 3)
|
||||
/* Insert the whole match (ii+1)..(ip-1) into dictionary. */
|
||||
++ii;
|
||||
do {
|
||||
|
||||
do
|
||||
{
|
||||
DVAL_NEXT(dv, ii);
|
||||
UPDATE_D(dict, 0, dv, ii, in);
|
||||
} while (++ii < ip);
|
||||
}
|
||||
while (++ii < ip);
|
||||
|
||||
DVAL_NEXT(dv, ii);
|
||||
assert(ii == ip);
|
||||
DVAL_ASSERT(dv, ip);
|
||||
|
@ -506,6 +542,7 @@ match:
|
|||
|
||||
while (ip < end && *m_pos == *ip)
|
||||
m_pos++, ip++;
|
||||
|
||||
assert(ip <= in_end);
|
||||
|
||||
/* 2b) code the long match */
|
||||
|
@ -531,10 +568,14 @@ match:
|
|||
/* Insert the whole match (ii+1)..(ip-1) into dictionary. */
|
||||
/* This is not recommended because it is slow. */
|
||||
++ii;
|
||||
do {
|
||||
|
||||
do
|
||||
{
|
||||
DVAL_NEXT(dv, ii);
|
||||
UPDATE_D(dict, 0, dv, ii, in);
|
||||
} while (++ii < ip);
|
||||
}
|
||||
while (++ii < ip);
|
||||
|
||||
DVAL_NEXT(dv, ii);
|
||||
assert(ii == ip);
|
||||
DVAL_ASSERT(dv, ip);
|
||||
|
@ -560,7 +601,8 @@ match:
|
|||
/* ii now points to the start of next literal run */
|
||||
assert(ii == ip);
|
||||
}
|
||||
} while (ip < ip_end);
|
||||
}
|
||||
while (ip < ip_end);
|
||||
|
||||
|
||||
|
||||
|
@ -569,6 +611,7 @@ the_end:
|
|||
|
||||
|
||||
#if defined(LZO_RETURN_IF_NOT_COMPRESSIBLE)
|
||||
|
||||
/* return -1 if op == out to indicate that we
|
||||
* couldn't compress and didn't copy anything.
|
||||
*/
|
||||
|
@ -577,6 +620,7 @@ the_end:
|
|||
*out_len = 0;
|
||||
return LZO_E_NOT_COMPRESSIBLE;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
|
|
@ -108,8 +108,10 @@ lzo1a_info ( int *rbits, int *clevel )
|
|||
{
|
||||
if (rbits)
|
||||
*rbits = RBITS;
|
||||
|
||||
if (clevel)
|
||||
*clevel = CLEVEL;
|
||||
|
||||
return D_SIZE * lzo_sizeof(lzo_bytep);
|
||||
}
|
||||
|
||||
|
@ -135,6 +137,7 @@ lzo1a_decompress ( const lzo_bytep in , lzo_uint in_len,
|
|||
|
||||
op = out;
|
||||
ip = in;
|
||||
|
||||
while (ip < ip_end)
|
||||
{
|
||||
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 */
|
||||
{
|
||||
t = *ip++;
|
||||
|
||||
if (t >= R0FAST - R0MIN) /* a long R0 run */
|
||||
{
|
||||
t -= R0FAST - R0MIN;
|
||||
|
||||
if (t == 0)
|
||||
t = R0FAST;
|
||||
else
|
||||
|
@ -155,13 +160,19 @@ lzo1a_decompress ( const lzo_bytep in , lzo_uint in_len,
|
|||
#else
|
||||
/* help the optimizer */
|
||||
lzo_uint tt = 256;
|
||||
do tt <<= 1; while (--t > 0);
|
||||
|
||||
do tt <<= 1;
|
||||
|
||||
while (--t > 0);
|
||||
|
||||
t = tt;
|
||||
#endif
|
||||
}
|
||||
|
||||
MEMCPY8_DS(op, ip, t);
|
||||
continue;
|
||||
}
|
||||
|
||||
t += R0MIN;
|
||||
goto literal;
|
||||
}
|
||||
|
@ -174,6 +185,7 @@ literal:
|
|||
while (ip < ip_end)
|
||||
{
|
||||
t = *ip++; /* get R1 marker */
|
||||
|
||||
if (t >= R0MIN)
|
||||
goto match;
|
||||
|
||||
|
@ -181,7 +193,8 @@ literal:
|
|||
assert((t & OMASK) == t);
|
||||
m_pos = op - MIN_OFFSET;
|
||||
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[1];
|
||||
*op++ = m_pos[2];
|
||||
|
@ -194,7 +207,8 @@ match:
|
|||
/* get match offset */
|
||||
m_pos = op - MIN_OFFSET;
|
||||
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 */
|
||||
if (t < ((MSIZE - 1) << OBITS)) /* a short match */
|
||||
|
@ -217,9 +231,12 @@ match:
|
|||
#if (LBITS < 8)
|
||||
/* a very short literal following a long match */
|
||||
t = ip[-1] >> LBITS;
|
||||
|
||||
if (t) do
|
||||
*op++ = *ip++;
|
||||
|
||||
while (--t);
|
||||
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
@ -275,31 +292,42 @@ do_compress ( const lzo_bytep in , lzo_uint in_len,
|
|||
BZERO8_PTR(wrkmem, sizeof(lzo_dict_t), D_SIZE);
|
||||
#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);
|
||||
|
||||
do {
|
||||
do
|
||||
{
|
||||
LZO_DEFINE_UNINITIALIZED_VAR(lzo_uint, m_off, 0);
|
||||
lzo_uint dindex;
|
||||
|
||||
DINDEX1(dindex, ip);
|
||||
GINDEX(m_pos, m_off, dict, dindex, in);
|
||||
|
||||
if (LZO_CHECK_MPOS_NON_DET(m_pos, m_off, in, ip, MAX_OFFSET))
|
||||
goto literal;
|
||||
|
||||
if (m_pos[0] == ip[0] && m_pos[1] == ip[1] && m_pos[2] == ip[2])
|
||||
goto match;
|
||||
|
||||
DINDEX2(dindex, ip);
|
||||
GINDEX(m_pos, m_off, dict, dindex, in);
|
||||
|
||||
if (LZO_CHECK_MPOS_NON_DET(m_pos, m_off, in, ip, MAX_OFFSET))
|
||||
goto literal;
|
||||
|
||||
if (m_pos[0] == ip[0] && m_pos[1] == ip[1] && m_pos[2] == ip[2])
|
||||
goto match;
|
||||
|
||||
goto literal;
|
||||
|
||||
literal:
|
||||
UPDATE_I(dict, 0, dindex, ip, in);
|
||||
|
||||
if (++ip >= ip_end)
|
||||
break;
|
||||
|
||||
continue;
|
||||
|
||||
match:
|
||||
|
@ -376,13 +404,15 @@ match:
|
|||
{
|
||||
/* inline the copying of a short R0 run */
|
||||
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);
|
||||
r1 = ip; /* set new R1 pointer */
|
||||
}
|
||||
else
|
||||
op = store_run(op, ii, t);
|
||||
}
|
||||
|
||||
#if (LBITS < 8)
|
||||
im = ip;
|
||||
#endif
|
||||
|
@ -402,6 +432,7 @@ match:
|
|||
#define PS *m_pos++ != *ip++
|
||||
|
||||
#if (MIN_MATCH_LONG - MIN_MATCH == 2) /* MBITS == 2 */
|
||||
|
||||
if (PS || PS)
|
||||
#elif (MIN_MATCH_LONG - MIN_MATCH == 6) /* MBITS == 3 */
|
||||
if (PS || PS || PS || PS || PS || PS)
|
||||
|
@ -443,12 +474,16 @@ match:
|
|||
#if (LZO_COLLECT_STATS)
|
||||
lzo_stats->short_matches++;
|
||||
lzo_stats->short_match[m_len]++;
|
||||
|
||||
if (m_off < OSIZE)
|
||||
lzo_stats->short_match_offset_osize[m_len]++;
|
||||
|
||||
if (m_off < 256)
|
||||
lzo_stats->short_match_offset_256[m_len]++;
|
||||
|
||||
if (m_off < 1024)
|
||||
lzo_stats->short_match_offset_1024[m_len]++;
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
@ -461,10 +496,14 @@ match:
|
|||
#if (CLEVEL == 9) || (CLEVEL >= 7 && MBITS <= 4) || (CLEVEL >= 5 && MBITS <= 3)
|
||||
/* Insert the whole match (ii+1)..(ip-1) into dictionary. */
|
||||
++ii;
|
||||
do {
|
||||
|
||||
do
|
||||
{
|
||||
DVAL_NEXT(dv, ii);
|
||||
UPDATE_D(dict, 0, dv, ii, in);
|
||||
} while (++ii < ip);
|
||||
}
|
||||
while (++ii < ip);
|
||||
|
||||
DVAL_NEXT(dv, ii);
|
||||
assert(ii == ip);
|
||||
DVAL_ASSERT(dv, ip);
|
||||
|
@ -496,6 +535,7 @@ match:
|
|||
|
||||
while (ip < end && *m_pos == *ip)
|
||||
m_pos++, ip++;
|
||||
|
||||
assert(ip <= in_end);
|
||||
|
||||
/* 2a) compute match parameters */
|
||||
|
@ -529,10 +569,14 @@ match:
|
|||
/* Insert the whole match (ii+1)..(ip-1) into dictionary. */
|
||||
/* This is not recommended because it is slow. */
|
||||
++ii;
|
||||
do {
|
||||
|
||||
do
|
||||
{
|
||||
DVAL_NEXT(dv, ii);
|
||||
UPDATE_D(dict, 0, dv, ii, in);
|
||||
} while (++ii < ip);
|
||||
}
|
||||
while (++ii < ip);
|
||||
|
||||
DVAL_NEXT(dv, ii);
|
||||
assert(ii == ip);
|
||||
DVAL_ASSERT(dv, ip);
|
||||
|
@ -559,12 +603,14 @@ match:
|
|||
assert(ii == ip);
|
||||
}
|
||||
|
||||
} while (ip < ip_end);
|
||||
}
|
||||
while (ip < ip_end);
|
||||
|
||||
assert(ip <= in_end);
|
||||
|
||||
|
||||
#if defined(LZO_RETURN_IF_NOT_COMPRESSIBLE)
|
||||
|
||||
/* return -1 if op == out to indicate that we
|
||||
* couldn't compress and didn't copy anything.
|
||||
*/
|
||||
|
@ -573,6 +619,7 @@ match:
|
|||
*out_len = 0;
|
||||
return LZO_E_NOT_COMPRESSIBLE;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* store the final literal run */
|
||||
|
|
|
@ -108,7 +108,9 @@
|
|||
#if (CLEVEL == 9) || (CLEVEL >= 7 && M2L_BITS <= 4) || (CLEVEL >= 5 && M2L_BITS <= 3)
|
||||
/* Insert the whole match (ii+1)..(ip-1) into dictionary. */
|
||||
++ii;
|
||||
do {
|
||||
|
||||
do
|
||||
{
|
||||
DVAL_NEXT(dv, ii);
|
||||
#if 0
|
||||
UPDATE_D(dict, drun, dv, ii, in);
|
||||
|
@ -116,7 +118,9 @@
|
|||
dict[ DINDEX(dv, ii) ] = DENTRY(ii, in);
|
||||
#endif
|
||||
MI
|
||||
} while (++ii < ip);
|
||||
}
|
||||
while (++ii < ip);
|
||||
|
||||
DVAL_NEXT(dv, ii);
|
||||
assert(ii == ip);
|
||||
DVAL_ASSERT(dv, ip);
|
||||
|
@ -155,6 +159,7 @@
|
|||
|
||||
while (ip < end && *m_pos == *ip)
|
||||
m_pos++, ip++;
|
||||
|
||||
assert(ip <= in_end);
|
||||
|
||||
/* 2a) compute match parameters */
|
||||
|
@ -190,7 +195,9 @@
|
|||
/* Insert the whole match (ii+1)..(ip-1) into dictionary. */
|
||||
/* This is not recommended because it can be slow. */
|
||||
++ii;
|
||||
do {
|
||||
|
||||
do
|
||||
{
|
||||
DVAL_NEXT(dv, ii);
|
||||
#if 0
|
||||
UPDATE_D(dict, drun, dv, ii, in);
|
||||
|
@ -198,7 +205,9 @@
|
|||
dict[ DINDEX(dv, ii) ] = DENTRY(ii, in);
|
||||
#endif
|
||||
MI
|
||||
} while (++ii < ip);
|
||||
}
|
||||
while (++ii < ip);
|
||||
|
||||
DVAL_NEXT(dv, ii);
|
||||
assert(ii == ip);
|
||||
DVAL_ASSERT(dv, ip);
|
||||
|
|
|
@ -60,37 +60,48 @@ store_run(lzo_bytep const oo, const lzo_bytep const ii, lzo_uint r_len)
|
|||
while (r_len >= (t = tt))
|
||||
{
|
||||
r_len -= t;
|
||||
*op++ = 0; *op++ = (R0MAX - R0MIN);
|
||||
*op++ = 0;
|
||||
*op++ = (R0MAX - R0MIN);
|
||||
MEMCPY8_DS(op, ip, t);
|
||||
LZO_STATS(lzo_stats->r0long_runs++);
|
||||
}
|
||||
|
||||
tt >>= 1;
|
||||
do {
|
||||
|
||||
do
|
||||
{
|
||||
if (r_len >= (t = tt))
|
||||
{
|
||||
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);
|
||||
LZO_STATS(lzo_stats->r0long_runs++);
|
||||
}
|
||||
|
||||
tt >>= 1;
|
||||
} while (--r_bits > 0);
|
||||
}
|
||||
while (--r_bits > 0);
|
||||
}
|
||||
|
||||
assert(r_len < 512);
|
||||
|
||||
while (r_len >= (t = R0FAST))
|
||||
{
|
||||
r_len -= t;
|
||||
*op++ = 0; *op++ = (R0FAST - R0MIN);
|
||||
*op++ = 0;
|
||||
*op++ = (R0FAST - R0MIN);
|
||||
MEMCPY8_DS(op, ip, t);
|
||||
LZO_STATS(lzo_stats->r0fast_runs++);
|
||||
}
|
||||
|
||||
t = r_len;
|
||||
|
||||
if (t >= R0MIN)
|
||||
{
|
||||
/* code a short R0 run */
|
||||
*op++ = 0; *op++ = LZO_BYTE(t - R0MIN);
|
||||
*op++ = 0;
|
||||
*op++ = LZO_BYTE(t - R0MIN);
|
||||
MEMCPY_DS(op, ip, t);
|
||||
LZO_STATS(lzo_stats->r0short_runs++);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
||||
m_off -= M3_MIN_OFFSET - M3_EOF_OFFSET;
|
||||
|
||||
/* code match len */
|
||||
if (m_len <= M3_MAX_LEN)
|
||||
*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;
|
||||
/* code match len */
|
||||
m_len -= M4_MIN_LEN - 1;
|
||||
|
||||
while (m_len > 255)
|
||||
{
|
||||
m_len -= 255;
|
||||
*op++ = 0;
|
||||
}
|
||||
|
||||
assert(m_len > 0);
|
||||
*op++ = LZO_BYTE(m_len);
|
||||
}
|
||||
|
||||
/* code low offset bits */
|
||||
*op++ = LZO_BYTE(m_off & M3O_MASK);
|
||||
/* 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->m3_m++;
|
||||
}
|
||||
|
||||
return op;
|
||||
}
|
||||
|
||||
|
@ -143,14 +148,18 @@ lzo1b_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
|
|||
c->r1_m_len = 0;
|
||||
|
||||
r = init_match(c, swd, NULL, 0, 0);
|
||||
|
||||
if (r != 0)
|
||||
return r;
|
||||
|
||||
if (max_chain > 0)
|
||||
swd->max_chain = max_chain;
|
||||
|
||||
r = find_match(c, swd, 0, 0);
|
||||
|
||||
if (r != 0)
|
||||
return r;
|
||||
|
||||
while (c->look > 0)
|
||||
{
|
||||
int lazy_match_min_gain = -1;
|
||||
|
@ -165,8 +174,10 @@ lzo1b_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
|
|||
#endif
|
||||
|
||||
assert(c->ip - c->look >= in);
|
||||
|
||||
if (lit == 0)
|
||||
ii = c->ip - c->look;
|
||||
|
||||
assert(ii + lit == 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;
|
||||
|
||||
#if (M2_MIN_LEN == 2)
|
||||
|
||||
if (m_len == 2)
|
||||
{
|
||||
/* don't code a match of len 2 if we have to
|
||||
code a literal run. Code a literal instead. */
|
||||
m_len = 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
#if (M2_MIN_LEN == M3_MIN_LEN)
|
||||
|
||||
if (m_len == M2_MIN_LEN && m_off > M2_MAX_OFFSET)
|
||||
{
|
||||
/* don't code a M3 match of len 3 if we have to
|
||||
code a literal run. Code a literal instead. */
|
||||
m_len = 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
}
|
||||
else
|
||||
|
@ -223,12 +238,14 @@ lzo1b_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
|
|||
/* try a lazy match */
|
||||
if (m_len == 0)
|
||||
lazy_match_min_gain = -1;
|
||||
|
||||
if (lazy_match_min_gain >= 0 && c->look > m_len)
|
||||
{
|
||||
assert(m_len > 0);
|
||||
|
||||
r = find_match(c, swd, 1, 0);
|
||||
assert(r == 0); LZO_UNUSED(r);
|
||||
assert(r == 0);
|
||||
LZO_UNUSED(r);
|
||||
assert(c->look > 0);
|
||||
|
||||
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;
|
||||
assert(ii + lit + 1 == c->ip - c->look);
|
||||
}
|
||||
|
||||
assert(m_len > 0);
|
||||
}
|
||||
|
||||
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 */
|
||||
lit++;
|
||||
r = find_match(c, swd, 1, 0);
|
||||
assert(r == 0); LZO_UNUSED(r);
|
||||
assert(r == 0);
|
||||
LZO_UNUSED(r);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -286,10 +306,12 @@ lzo1b_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
|
|||
{
|
||||
op = STORE_RUN(op, ii, lit);
|
||||
}
|
||||
|
||||
if (lit < R0FAST)
|
||||
c->r1_m_len = m_len;
|
||||
else
|
||||
c->r1_m_len = 0;
|
||||
|
||||
lit = 0;
|
||||
}
|
||||
else
|
||||
|
@ -298,7 +320,8 @@ lzo1b_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
|
|||
/* 2 - code match */
|
||||
op = code_match(c, op, m_len, m_off);
|
||||
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);
|
||||
|
|
|
@ -120,6 +120,7 @@ do_compress ( const lzo_bytep in , lzo_uint in_len,
|
|||
#endif
|
||||
|
||||
assert(ip < ip_end);
|
||||
|
||||
for (;;)
|
||||
{
|
||||
const lzo_bytep m_pos;
|
||||
|
@ -162,8 +163,10 @@ literal:
|
|||
#if (DD_BITS == 0)
|
||||
UPDATE_I(dict, 0, dindex, ip, in);
|
||||
#endif
|
||||
|
||||
if (++ip >= ip_end)
|
||||
break;
|
||||
|
||||
#if (DD_BITS > 0)
|
||||
DVAL_NEXT(dv, ip);
|
||||
#endif
|
||||
|
@ -224,9 +227,11 @@ match:
|
|||
for (i = 0; i < D_SIZE; i++)
|
||||
{
|
||||
p = dict[i];
|
||||
|
||||
if (BOUNDS_CHECKING_OFF_IN_EXPR(p == NULL || p < in || p > in_end))
|
||||
lzo_stats->unused_dict_entries++;
|
||||
}
|
||||
|
||||
lzo_stats->unused_dict_entries_percent =
|
||||
100.0 * lzo_stats->unused_dict_entries / D_SIZE;
|
||||
}
|
||||
|
@ -234,6 +239,7 @@ match:
|
|||
|
||||
|
||||
#if defined(LZO_RETURN_IF_NOT_COMPRESSIBLE)
|
||||
|
||||
/* return if op == out to indicate that we
|
||||
* couldn't compress and didn't copy anything.
|
||||
*/
|
||||
|
@ -242,6 +248,7 @@ match:
|
|||
*out_len = 0;
|
||||
return LZO_E_NOT_COMPRESSIBLE;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* store the final literal run */
|
||||
|
|
|
@ -74,9 +74,12 @@ _lzo1b_do_compress ( const lzo_bytep in, lzo_uint in_len,
|
|||
|
||||
#if defined(LZO_EOF_CODE)
|
||||
#if defined(LZO_TEST_COMPRESS_OVERRUN)
|
||||
|
||||
if (r == LZO_E_OK && avail_out - *out_len < 3)
|
||||
r = LZO_E_COMPRESS_OVERRUN;
|
||||
|
||||
#endif
|
||||
|
||||
if (r == LZO_E_OK)
|
||||
{
|
||||
lzo_bytep op = out + *out_len;
|
||||
|
@ -85,6 +88,7 @@ _lzo1b_do_compress ( const lzo_bytep in, lzo_uint in_len,
|
|||
op[2] = 0;
|
||||
*out_len += 3;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
|
|
@ -86,6 +86,7 @@
|
|||
|
||||
/* 2b) code the match */
|
||||
#if (_M2_MAX_OFFSET != _M3_MAX_OFFSET)
|
||||
|
||||
if (m_off <= M2_MAX_OFFSET)
|
||||
{
|
||||
#else
|
||||
|
@ -122,6 +123,7 @@
|
|||
m3 = op; /* set M3 pointer */
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif /* (_M2_MAX_OFFSET != _M3_MAX_OFFSET) */
|
||||
|
||||
|
||||
|
@ -137,7 +139,9 @@
|
|||
#if (CLEVEL == 9) || (CLEVEL >= 7 && M2L_BITS <= 4) || (CLEVEL >= 5 && M2L_BITS <= 3)
|
||||
/* Insert the whole match (ii+1)..(ip-1) into dictionary. */
|
||||
++ii;
|
||||
do {
|
||||
|
||||
do
|
||||
{
|
||||
DVAL_NEXT(dv, ii);
|
||||
#if 0
|
||||
UPDATE_D(dict, drun, dv, ii, in);
|
||||
|
@ -145,7 +149,9 @@
|
|||
dict[ DINDEX(dv, ii) ] = DENTRY(ii, in);
|
||||
#endif
|
||||
MI
|
||||
} while (++ii < ip);
|
||||
}
|
||||
while (++ii < ip);
|
||||
|
||||
DVAL_NEXT(dv, ii);
|
||||
assert(ii == ip);
|
||||
DVAL_ASSERT(dv, ip);
|
||||
|
@ -176,8 +182,10 @@
|
|||
{
|
||||
const lzo_bytep end;
|
||||
end = in_end;
|
||||
|
||||
while (ip < end && *m_pos == *ip)
|
||||
m_pos++, ip++;
|
||||
|
||||
assert(ip <= in_end);
|
||||
m_len = pd(ip, ii);
|
||||
}
|
||||
|
@ -209,11 +217,13 @@
|
|||
*op++ = M4_MARKER;
|
||||
/* code match len */
|
||||
m_len -= M4_MIN_LEN - 1;
|
||||
|
||||
while (m_len > 255)
|
||||
{
|
||||
m_len -= 255;
|
||||
*op++ = 0;
|
||||
}
|
||||
|
||||
assert(m_len > 0);
|
||||
*op++ = LZO_BYTE(m_len);
|
||||
LZO_STATS(lzo_stats->m4_matches++);
|
||||
|
@ -242,7 +252,9 @@
|
|||
/* Insert the whole match (ii+1)..(ip-1) into dictionary. */
|
||||
/* This is not recommended because it can be slow. */
|
||||
++ii;
|
||||
do {
|
||||
|
||||
do
|
||||
{
|
||||
DVAL_NEXT(dv, ii);
|
||||
#if 0
|
||||
UPDATE_D(dict, drun, dv, ii, in);
|
||||
|
@ -250,7 +262,9 @@
|
|||
dict[ DINDEX(dv, ii) ] = DENTRY(ii, in);
|
||||
#endif
|
||||
MI
|
||||
} while (++ii < ip);
|
||||
}
|
||||
while (++ii < ip);
|
||||
|
||||
DVAL_NEXT(dv, ii);
|
||||
assert(ii == ip);
|
||||
DVAL_ASSERT(dv, ip);
|
||||
|
|
|
@ -38,11 +38,13 @@
|
|||
************************************************************************/
|
||||
|
||||
assert(ip < ip_end);
|
||||
|
||||
if (pd(ip, ii) > 0)
|
||||
{
|
||||
lzo_uint t = pd(ip, ii);
|
||||
|
||||
#if defined(LZO_HAVE_R1)
|
||||
|
||||
if (ip == r1)
|
||||
{
|
||||
/* Code a context sensitive R1 match. */
|
||||
|
@ -66,6 +68,7 @@
|
|||
LZO_STATS(lzo_stats->lit_runs++);
|
||||
LZO_STATS(lzo_stats->lit_run[t]++);
|
||||
#if defined(LZO_HAVE_M3)
|
||||
|
||||
if (t < LZO_SIZE(8 - M3O_BITS) && op == m3)
|
||||
{
|
||||
/* Code a very short literal run into the low offset bits
|
||||
|
@ -81,6 +84,7 @@
|
|||
{
|
||||
*op++ = LZO_BYTE(t);
|
||||
}
|
||||
|
||||
MEMCPY_DS(op, ii, t);
|
||||
#if defined(LZO_HAVE_R1)
|
||||
r1 = ip + (M2_MIN_LEN + 1); /* set new R1 pointer */
|
||||
|
@ -91,7 +95,8 @@
|
|||
/* inline the copying of a short R0 run */
|
||||
LZO_STATS(lzo_stats->literals += t);
|
||||
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);
|
||||
#if defined(LZO_HAVE_R1)
|
||||
r1 = ip + (M2_MIN_LEN + 1); /* set new R1 pointer */
|
||||
|
|
|
@ -63,9 +63,11 @@ DO_DECOMPRESS ( const lzo_bytep in , lzo_uint in_len,
|
|||
{
|
||||
NEED_IP(1);
|
||||
t = *ip++;
|
||||
|
||||
if (t >= R0FAST - R0MIN) /* a long R0 run */
|
||||
{
|
||||
t -= R0FAST - R0MIN;
|
||||
|
||||
if (t == 0)
|
||||
t = R0FAST;
|
||||
else
|
||||
|
@ -75,37 +77,57 @@ DO_DECOMPRESS ( const lzo_bytep in , lzo_uint in_len,
|
|||
#else
|
||||
/* help the optimizer */
|
||||
lzo_uint tt = 256;
|
||||
do tt <<= 1; while (--t > 0);
|
||||
|
||||
do tt <<= 1;
|
||||
|
||||
while (--t > 0);
|
||||
|
||||
t = tt;
|
||||
#endif
|
||||
}
|
||||
|
||||
NEED_IP(t); NEED_OP(t);
|
||||
NEED_IP(t);
|
||||
NEED_OP(t);
|
||||
#if 1 && (LZO_OPT_UNALIGNED32)
|
||||
do {
|
||||
|
||||
do
|
||||
{
|
||||
UA_COPY4(op + 0, ip + 0);
|
||||
UA_COPY4(op + 4, ip + 4);
|
||||
op += 8; ip += 8;
|
||||
op += 8;
|
||||
ip += 8;
|
||||
t -= 8;
|
||||
} while (t > 0);
|
||||
}
|
||||
while (t > 0);
|
||||
|
||||
#else
|
||||
MEMCPY8_DS(op, ip, t);
|
||||
#endif
|
||||
continue;
|
||||
}
|
||||
|
||||
t += R0MIN; /* a short R0 run */
|
||||
}
|
||||
|
||||
NEED_IP(t); NEED_OP(t);
|
||||
NEED_IP(t);
|
||||
NEED_OP(t);
|
||||
/* copy literal run */
|
||||
#if 1 && (LZO_OPT_UNALIGNED32)
|
||||
|
||||
if (t >= 4)
|
||||
{
|
||||
do {
|
||||
do
|
||||
{
|
||||
UA_COPY4(op, ip);
|
||||
op += 4; ip += 4; t -= 4;
|
||||
} while (t >= 4);
|
||||
if (t > 0) do *op++ = *ip++; while (--t > 0);
|
||||
op += 4;
|
||||
ip += 4;
|
||||
t -= 4;
|
||||
}
|
||||
while (t >= 4);
|
||||
|
||||
if (t > 0) do* op++ = *ip++;
|
||||
|
||||
while (--t > 0);
|
||||
}
|
||||
else
|
||||
#endif
|
||||
|
@ -113,7 +135,10 @@ DO_DECOMPRESS ( const lzo_bytep in , lzo_uint in_len,
|
|||
#if (M3O_BITS < 7)
|
||||
literal1:
|
||||
#endif
|
||||
do *op++ = *ip++; while (--t > 0);
|
||||
|
||||
do* op++ = *ip++;
|
||||
|
||||
while (--t > 0);
|
||||
}
|
||||
|
||||
#if (M3O_BITS == 7)
|
||||
|
@ -124,16 +149,19 @@ literal2:
|
|||
while (TEST_IP_AND_TEST_OP)
|
||||
{
|
||||
t = *ip++; /* get R1 marker */
|
||||
|
||||
if (t >= R0MIN)
|
||||
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 */
|
||||
assert((t & M2O_MASK) == t);
|
||||
m_pos = op - M2_MIN_OFFSET;
|
||||
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);
|
||||
COPY_M2;
|
||||
*op++ = *ip++;
|
||||
|
@ -152,7 +180,8 @@ match:
|
|||
NEED_IP(1);
|
||||
m_pos = op - M2_MIN_OFFSET;
|
||||
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);
|
||||
|
||||
/* get match len */
|
||||
|
@ -165,9 +194,11 @@ match:
|
|||
{
|
||||
/* get match len */
|
||||
t &= M3L_MASK;
|
||||
|
||||
if (t == 0) /* a M4 match */
|
||||
{
|
||||
NEED_IP(1);
|
||||
|
||||
while (*ip == 0)
|
||||
{
|
||||
t += 255;
|
||||
|
@ -175,6 +206,7 @@ match:
|
|||
TEST_OV(t);
|
||||
NEED_IP(1);
|
||||
}
|
||||
|
||||
t += (M4_MIN_LEN - M3_MIN_LEN) + *ip++;
|
||||
}
|
||||
|
||||
|
@ -184,23 +216,38 @@ match:
|
|||
m_pos -= *ip++ & M3O_MASK;
|
||||
m_pos -= (lzo_uint)(*ip++) << M3O_BITS;
|
||||
#if defined(LZO_EOF_CODE)
|
||||
|
||||
if (m_pos == op)
|
||||
goto eof_found;
|
||||
|
||||
#endif
|
||||
|
||||
/* copy match */
|
||||
assert(m_pos >= out); assert(m_pos < op);
|
||||
TEST_LB(m_pos); NEED_OP(t + M3_MIN_LEN - 1);
|
||||
assert(m_pos >= out);
|
||||
assert(m_pos < op);
|
||||
TEST_LB(m_pos);
|
||||
NEED_OP(t + M3_MIN_LEN - 1);
|
||||
#if (LZO_OPT_UNALIGNED32)
|
||||
|
||||
if (t >= 2 * 4 - (M3_MIN_LEN - 1) && (op - m_pos) >= 4)
|
||||
{
|
||||
UA_COPY4(op, m_pos);
|
||||
op += 4; m_pos += 4; t -= 4 - (M3_MIN_LEN - 1);
|
||||
do {
|
||||
op += 4;
|
||||
m_pos += 4;
|
||||
t -= 4 - (M3_MIN_LEN - 1);
|
||||
|
||||
do
|
||||
{
|
||||
UA_COPY4(op, m_pos);
|
||||
op += 4; m_pos += 4; t -= 4;
|
||||
} while (t >= 4);
|
||||
if (t > 0) do *op++ = *m_pos++; while (--t > 0);
|
||||
op += 4;
|
||||
m_pos += 4;
|
||||
t -= 4;
|
||||
}
|
||||
while (t >= 4);
|
||||
|
||||
if (t > 0) do* op++ = *m_pos++;
|
||||
|
||||
while (--t > 0);
|
||||
}
|
||||
else
|
||||
#endif
|
||||
|
@ -212,19 +259,25 @@ match:
|
|||
|
||||
#if (M3O_BITS < 7)
|
||||
t = ip[-2] >> M3O_BITS;
|
||||
|
||||
if (t)
|
||||
{
|
||||
NEED_IP(t); NEED_OP(t);
|
||||
NEED_IP(t);
|
||||
NEED_OP(t);
|
||||
goto literal1;
|
||||
}
|
||||
|
||||
#elif (M3O_BITS == 7)
|
||||
|
||||
/* optimized version */
|
||||
if (ip[-2] & (1 << M3O_BITS))
|
||||
{
|
||||
NEED_IP(1); NEED_OP(1);
|
||||
NEED_IP(1);
|
||||
NEED_OP(1);
|
||||
*op++ = *ip++;
|
||||
goto literal2;
|
||||
}
|
||||
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
|
|
@ -52,37 +52,48 @@ STORE_RUN ( lzo_bytep const oo, const lzo_bytep const ii, lzo_uint r_len)
|
|||
while (r_len >= (t = tt))
|
||||
{
|
||||
r_len -= t;
|
||||
*op++ = 0; *op++ = (R0FAST - R0MIN) + 7;
|
||||
*op++ = 0;
|
||||
*op++ = (R0FAST - R0MIN) + 7;
|
||||
MEMCPY8_DS(op, ip, t);
|
||||
LZO_STATS(lzo_stats->r0long_runs++);
|
||||
}
|
||||
|
||||
tt >>= 1;
|
||||
do {
|
||||
|
||||
do
|
||||
{
|
||||
if (r_len >= (t = tt))
|
||||
{
|
||||
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);
|
||||
LZO_STATS(lzo_stats->r0long_runs++);
|
||||
}
|
||||
|
||||
tt >>= 1;
|
||||
} while (--r_bits > 0);
|
||||
}
|
||||
while (--r_bits > 0);
|
||||
}
|
||||
|
||||
assert(r_len < 512);
|
||||
|
||||
while (r_len >= (t = R0FAST))
|
||||
{
|
||||
r_len -= t;
|
||||
*op++ = 0; *op++ = (R0FAST - R0MIN);
|
||||
*op++ = 0;
|
||||
*op++ = (R0FAST - R0MIN);
|
||||
MEMCPY8_DS(op, ip, t);
|
||||
LZO_STATS(lzo_stats->r0fast_runs++);
|
||||
}
|
||||
|
||||
t = r_len;
|
||||
|
||||
if (t >= R0MIN)
|
||||
{
|
||||
/* code a short R0 run */
|
||||
*op++ = 0; *op++ = LZO_BYTE(t - R0MIN);
|
||||
*op++ = 0;
|
||||
*op++ = LZO_BYTE(t - R0MIN);
|
||||
MEMCPY_DS(op, ip, t);
|
||||
LZO_STATS(lzo_stats->r0short_runs++);
|
||||
}
|
||||
|
|
|
@ -42,18 +42,25 @@
|
|||
/* search ip in the dictionary */
|
||||
DINDEX1(dindex, ip);
|
||||
GINDEX(m_pos, m_off, dict, dindex, in);
|
||||
|
||||
if (LZO_CHECK_MPOS_NON_DET(m_pos, m_off, in, ip, M3_MAX_OFFSET))
|
||||
goto literal;
|
||||
|
||||
#if 1
|
||||
|
||||
if (m_off <= M2_MAX_OFFSET || m_pos[3] == ip[3])
|
||||
goto try_match;
|
||||
|
||||
DINDEX2(dindex, ip);
|
||||
#endif
|
||||
GINDEX(m_pos, m_off, dict, dindex, in);
|
||||
|
||||
if (LZO_CHECK_MPOS_NON_DET(m_pos, m_off, in, ip, M3_MAX_OFFSET))
|
||||
goto literal;
|
||||
|
||||
if (m_off <= M2_MAX_OFFSET || m_pos[3] == ip[3])
|
||||
goto try_match;
|
||||
|
||||
goto literal;
|
||||
|
||||
|
||||
|
@ -73,7 +80,9 @@
|
|||
|
||||
ip_sav = ip;
|
||||
m_len = 0;
|
||||
do {
|
||||
|
||||
do
|
||||
{
|
||||
#if !defined(NDEBUG)
|
||||
const lzo_bytep z_pos = NULL;
|
||||
#endif
|
||||
|
@ -92,6 +101,7 @@
|
|||
if (LZO_CHECK_MPOS(m_pos, x_off, in, ip, MAX_OFFSET))
|
||||
#if (CLEVEL == 9)
|
||||
*d = DENTRY(ip, in);
|
||||
|
||||
#else
|
||||
((void)(0));
|
||||
#endif
|
||||
|
@ -102,16 +112,19 @@
|
|||
#if !(LZO_DICT_USE_PTR)
|
||||
assert((z_pos = ip - 3 - x_off) == (m_pos - 3));
|
||||
#endif
|
||||
|
||||
/* a match */
|
||||
if (MATCH_M2)
|
||||
{
|
||||
x_len = pd((ip - 1), ip_sav);
|
||||
|
||||
if (x_len > m_len)
|
||||
{
|
||||
m_len = x_len;
|
||||
m_off = x_off;
|
||||
assert((m_pos_sav = z_pos) != NULL);
|
||||
}
|
||||
|
||||
#if (CLEVEL == 9)
|
||||
/* try to find a closer match */
|
||||
else if (x_len == m_len && x_off < m_off)
|
||||
|
@ -119,6 +132,7 @@
|
|||
m_off = x_off;
|
||||
assert((m_pos_sav = z_pos) != NULL);
|
||||
}
|
||||
|
||||
#endif
|
||||
}
|
||||
else
|
||||
|
@ -129,16 +143,20 @@
|
|||
{
|
||||
const lzo_bytep end;
|
||||
end = MATCH_IP_END;
|
||||
|
||||
while (ip < end && *m_pos == *ip)
|
||||
m_pos++, ip++;
|
||||
|
||||
assert(ip <= in_end);
|
||||
x_len = pd(ip, ip_sav);
|
||||
}
|
||||
|
||||
if (x_len > m_len)
|
||||
{
|
||||
m_len = x_len;
|
||||
m_off = x_off;
|
||||
assert((m_pos_sav = z_pos) != NULL);
|
||||
|
||||
if (ip >= MATCH_IP_END)
|
||||
{
|
||||
ip = ip_sav;
|
||||
|
@ -156,7 +174,9 @@
|
|||
m_off = x_off;
|
||||
assert((m_pos_sav = z_pos) != NULL);
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
/* try to find a closer match */
|
||||
if (m_len < M2_MAX_LEN + 1 || x_off < m_off)
|
||||
{
|
||||
|
@ -164,6 +184,7 @@
|
|||
m_off = x_off;
|
||||
assert((m_pos_sav = z_pos) != NULL);
|
||||
}
|
||||
|
||||
#endif
|
||||
#else
|
||||
/* don't search for a longer/closer match */
|
||||
|
@ -177,12 +198,16 @@
|
|||
goto match;
|
||||
#endif
|
||||
}
|
||||
|
||||
ip = ip_sav;
|
||||
}
|
||||
else
|
||||
ip = ip_sav;
|
||||
|
||||
d++;
|
||||
} while (--j > 0);
|
||||
}
|
||||
while (--j > 0);
|
||||
|
||||
assert(ip == ip_sav);
|
||||
|
||||
d -= DD_SIZE;
|
||||
|
|
|
@ -48,6 +48,7 @@ try_match:
|
|||
#endif
|
||||
m_pos_sav = m_pos;
|
||||
#endif
|
||||
|
||||
if (m_pos[0] == ip[0] && m_pos[1] == ip[1] && m_pos[2] == ip[2])
|
||||
{
|
||||
m_pos += 3;
|
||||
|
@ -62,19 +63,25 @@ try_match:
|
|||
|
||||
if (m_len > M2_MIN_LEN)
|
||||
goto match;
|
||||
|
||||
if (m_len == M2_MIN_LEN)
|
||||
{
|
||||
#if (_MAX_OFFSET == _M2_MAX_OFFSET)
|
||||
goto match;
|
||||
#else
|
||||
|
||||
if (m_off <= M2_MAX_OFFSET)
|
||||
goto match;
|
||||
|
||||
#if 0 && (M3_MIN_LEN == M2_MIN_LEN)
|
||||
|
||||
if (ip == ii)
|
||||
goto match;
|
||||
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
goto literal;
|
||||
|
||||
|
||||
|
|
|
@ -58,6 +58,7 @@ static lzo_compress_t lzo1b_get_compress_func(int clevel)
|
|||
else
|
||||
return (lzo_compress_t) 0;
|
||||
}
|
||||
|
||||
f = c_funcs[clevel - 1];
|
||||
assert(f && *f);
|
||||
return *f;
|
||||
|
@ -73,8 +74,10 @@ lzo1b_compress ( const lzo_bytep src, lzo_uint src_len,
|
|||
lzo_compress_t f;
|
||||
|
||||
f = lzo1b_get_compress_func(clevel);
|
||||
|
||||
if (!f)
|
||||
return LZO_E_ERROR;
|
||||
|
||||
return _lzo1b_do_compress(src, src_len, dst, dst_len, wrkmem, f);
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
||||
m_off -= M3_MIN_OFFSET - M3_EOF_OFFSET;
|
||||
|
||||
/* code match len */
|
||||
if (m_len <= M3_MAX_LEN)
|
||||
*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;
|
||||
/* code match len */
|
||||
m_len -= M4_MIN_LEN - 1;
|
||||
|
||||
while (m_len > 255)
|
||||
{
|
||||
m_len -= 255;
|
||||
*op++ = 0;
|
||||
}
|
||||
|
||||
assert(m_len > 0);
|
||||
*op++ = LZO_BYTE(m_len);
|
||||
}
|
||||
|
||||
/* code low offset bits */
|
||||
*op++ = LZO_BYTE(m_off & M3O_MASK);
|
||||
/* 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_m++;
|
||||
}
|
||||
|
||||
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 */
|
||||
|
||||
r = init_match(c, swd, NULL, 0, 0);
|
||||
|
||||
if (r != 0)
|
||||
return r;
|
||||
|
||||
if (max_chain > 0)
|
||||
swd->max_chain = max_chain;
|
||||
|
||||
r = find_match(c, swd, 0, 0);
|
||||
|
||||
if (r != 0)
|
||||
return r;
|
||||
|
||||
while (c->look > 0)
|
||||
{
|
||||
int lazy_match_min_gain = -1;
|
||||
|
@ -167,8 +176,10 @@ lzo1c_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
|
|||
#endif
|
||||
|
||||
assert(c->ip - c->look >= in);
|
||||
|
||||
if (lit == 0)
|
||||
ii = c->ip - c->look;
|
||||
|
||||
assert(ii + lit == 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;
|
||||
|
||||
#if (M2_MIN_LEN == 2)
|
||||
|
||||
if (m_len == 2)
|
||||
{
|
||||
/* don't code a match of len 2 if we have to
|
||||
code a literal run. Code a literal instead. */
|
||||
m_len = 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
#if (M2_MIN_LEN == M3_MIN_LEN)
|
||||
|
||||
if (m_len == M2_MIN_LEN && m_off > M2_MAX_OFFSET)
|
||||
{
|
||||
/* don't code a M3 match of len 3 if we have to
|
||||
code a literal run. Code a literal instead. */
|
||||
m_len = 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
}
|
||||
else
|
||||
|
@ -229,12 +244,14 @@ lzo1c_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
|
|||
/* try a lazy match */
|
||||
if (m_len == 0)
|
||||
lazy_match_min_gain = -1;
|
||||
|
||||
if (lazy_match_min_gain >= 0 && c->look > m_len)
|
||||
{
|
||||
assert(m_len > 0);
|
||||
|
||||
r = find_match(c, swd, 1, 0);
|
||||
assert(r == 0); LZO_UNUSED(r);
|
||||
assert(r == 0);
|
||||
LZO_UNUSED(r);
|
||||
assert(c->look > 0);
|
||||
|
||||
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;
|
||||
assert(ii + lit + 1 == c->ip - c->look);
|
||||
}
|
||||
|
||||
assert(m_len > 0);
|
||||
}
|
||||
|
||||
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 */
|
||||
lit++;
|
||||
r = find_match(c, swd, 1, 0);
|
||||
assert(r == 0); LZO_UNUSED(r);
|
||||
assert(r == 0);
|
||||
LZO_UNUSED(r);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -300,10 +320,12 @@ lzo1c_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
|
|||
{
|
||||
op = STORE_RUN(op, ii, lit);
|
||||
}
|
||||
|
||||
if (lit < R0FAST)
|
||||
c->r1_m_len = m_len;
|
||||
else
|
||||
c->r1_m_len = 0;
|
||||
|
||||
lit = 0;
|
||||
}
|
||||
else
|
||||
|
@ -312,7 +334,8 @@ lzo1c_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
|
|||
/* 2 - code match */
|
||||
op = code_match(c, op, m_len, m_off);
|
||||
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);
|
||||
|
|
|
@ -74,9 +74,12 @@ _lzo1c_do_compress ( const lzo_bytep in, lzo_uint in_len,
|
|||
|
||||
#if defined(LZO_EOF_CODE)
|
||||
#if defined(LZO_TEST_COMPRESS_OVERRUN)
|
||||
|
||||
if (r == LZO_E_OK && avail_out - *out_len < 3)
|
||||
r = LZO_E_COMPRESS_OVERRUN;
|
||||
|
||||
#endif
|
||||
|
||||
if (r == LZO_E_OK)
|
||||
{
|
||||
lzo_bytep op = out + *out_len;
|
||||
|
@ -85,6 +88,7 @@ _lzo1c_do_compress ( const lzo_bytep in, lzo_uint in_len,
|
|||
op[2] = 0;
|
||||
*out_len += 3;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
|
|
@ -58,6 +58,7 @@ static lzo_compress_t lzo1c_get_compress_func(int clevel)
|
|||
else
|
||||
return (lzo_compress_t) 0;
|
||||
}
|
||||
|
||||
f = c_funcs[clevel - 1];
|
||||
assert(f && *f);
|
||||
return *f;
|
||||
|
@ -73,8 +74,10 @@ lzo1c_compress ( const lzo_bytep src, lzo_uint src_len,
|
|||
lzo_compress_t f;
|
||||
|
||||
f = lzo1c_get_compress_func(clevel);
|
||||
|
||||
if (!f)
|
||||
return LZO_E_ERROR;
|
||||
|
||||
return _lzo1c_do_compress(src, src_len, dst, dst_len, wrkmem, f);
|
||||
}
|
||||
|
||||
|
|
|
@ -69,6 +69,7 @@ int do_compress ( const lzo_bytep in , lzo_uint in_len,
|
|||
ii = ip;
|
||||
|
||||
ip++;
|
||||
|
||||
for (;;)
|
||||
{
|
||||
const lzo_bytep m_pos;
|
||||
|
@ -79,23 +80,31 @@ int do_compress ( const lzo_bytep in , lzo_uint in_len,
|
|||
|
||||
DINDEX1(dindex, ip);
|
||||
GINDEX(m_pos, m_off, dict, dindex, in);
|
||||
|
||||
if (LZO_CHECK_MPOS_NON_DET(m_pos, m_off, in, ip, M3_MAX_OFFSET))
|
||||
goto literal;
|
||||
|
||||
#if 1
|
||||
|
||||
if (m_off <= M2_MAX_OFFSET || m_pos[3] == ip[3])
|
||||
goto try_match;
|
||||
|
||||
DINDEX2(dindex, ip);
|
||||
#endif
|
||||
GINDEX(m_pos, m_off, dict, dindex, in);
|
||||
|
||||
if (LZO_CHECK_MPOS_NON_DET(m_pos, m_off, in, ip, M3_MAX_OFFSET))
|
||||
goto literal;
|
||||
|
||||
if (m_off <= M2_MAX_OFFSET || m_pos[3] == ip[3])
|
||||
goto try_match;
|
||||
|
||||
goto literal;
|
||||
|
||||
|
||||
try_match:
|
||||
#if 0 && (LZO_OPT_UNALIGNED16)
|
||||
|
||||
if (UA_GET_NE16(m_pos) != UA_GET_NE16(ip))
|
||||
#else
|
||||
if (m_pos[0] != ip[0] || m_pos[1] != ip[1])
|
||||
|
@ -108,16 +117,23 @@ try_match:
|
|||
{
|
||||
m_pos += 3;
|
||||
#if 0
|
||||
|
||||
if (m_off <= M2_MAX_OFFSET)
|
||||
goto match;
|
||||
|
||||
if (lit <= 3)
|
||||
goto match;
|
||||
|
||||
if (lit == 3) /* better compression, but slower */
|
||||
{
|
||||
assert(op - 2 > out); op[-2] |= LZO_BYTE(3);
|
||||
*op++ = *ii++; *op++ = *ii++; *op++ = *ii++;
|
||||
assert(op - 2 > out);
|
||||
op[-2] |= LZO_BYTE(3);
|
||||
*op++ = *ii++;
|
||||
*op++ = *ii++;
|
||||
*op++ = *ii++;
|
||||
goto code_match;
|
||||
}
|
||||
|
||||
if (*m_pos == ip[3])
|
||||
#endif
|
||||
goto match;
|
||||
|
@ -128,8 +144,10 @@ try_match:
|
|||
/* a literal */
|
||||
literal:
|
||||
UPDATE_I(dict, 0, dindex, ip, in);
|
||||
|
||||
if (++ip >= ip_end)
|
||||
break;
|
||||
|
||||
continue;
|
||||
|
||||
|
||||
|
@ -138,6 +156,7 @@ match:
|
|||
UPDATE_I(dict, 0, dindex, ip, in);
|
||||
/* store current literal run */
|
||||
lit = pd(ip, ii);
|
||||
|
||||
if (lit > 0)
|
||||
{
|
||||
lzo_uint t = lit;
|
||||
|
@ -151,28 +170,36 @@ match:
|
|||
lzo_uint tt = t - 31;
|
||||
|
||||
*op++ = 0;
|
||||
|
||||
while (tt > 255)
|
||||
{
|
||||
tt -= 255;
|
||||
UA_SET1(op, 0);
|
||||
op++;
|
||||
}
|
||||
|
||||
assert(tt > 0);
|
||||
*op++ = LZO_BYTE(tt);
|
||||
}
|
||||
do *op++ = *ii++; while (--t > 0);
|
||||
|
||||
do* op++ = *ii++;
|
||||
|
||||
while (--t > 0);
|
||||
}
|
||||
|
||||
assert(ii == ip);
|
||||
|
||||
|
||||
/* code the match */
|
||||
ip += 3;
|
||||
|
||||
if (*m_pos++ != *ip++ || *m_pos++ != *ip++ || *m_pos++ != *ip++ ||
|
||||
*m_pos++ != *ip++ || *m_pos++ != *ip++ || *m_pos++ != *ip++)
|
||||
{
|
||||
--ip;
|
||||
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)
|
||||
{
|
||||
|
@ -199,8 +226,10 @@ match:
|
|||
{
|
||||
const lzo_bytep end;
|
||||
end = in_end;
|
||||
|
||||
while (ip < end && *m_pos == *ip)
|
||||
m_pos++, ip++;
|
||||
|
||||
m_len = pd(ip, ii);
|
||||
}
|
||||
assert(m_len >= 3);
|
||||
|
@ -211,20 +240,24 @@ match:
|
|||
{
|
||||
m_len -= 33;
|
||||
*op++ = M3_MARKER | 0;
|
||||
|
||||
while (m_len > 255)
|
||||
{
|
||||
m_len -= 255;
|
||||
UA_SET1(op, 0);
|
||||
op++;
|
||||
}
|
||||
|
||||
assert(m_len > 0);
|
||||
*op++ = LZO_BYTE(m_len);
|
||||
}
|
||||
|
||||
*op++ = LZO_BYTE((m_off & 63) << 2);
|
||||
*op++ = LZO_BYTE(m_off >> 6);
|
||||
}
|
||||
|
||||
ii = ip;
|
||||
|
||||
if (ip >= ip_end)
|
||||
break;
|
||||
}
|
||||
|
@ -244,15 +277,18 @@ match:
|
|||
lzo_uint tt = t - 31;
|
||||
|
||||
*op++ = 0;
|
||||
|
||||
while (tt > 255)
|
||||
{
|
||||
tt -= 255;
|
||||
UA_SET1(op, 0);
|
||||
op++;
|
||||
}
|
||||
|
||||
assert(tt > 0);
|
||||
*op++ = LZO_BYTE(tt);
|
||||
}
|
||||
|
||||
UA_COPYN(op, ii, t);
|
||||
op += t;
|
||||
}
|
||||
|
@ -279,7 +315,11 @@ lzo1f_1_compress ( const lzo_bytep in , lzo_uint in_len,
|
|||
else if (in_len <= 10)
|
||||
{
|
||||
*op++ = LZO_BYTE(in_len);
|
||||
do *op++ = *in++; while (--in_len > 0);
|
||||
|
||||
do* op++ = *in++;
|
||||
|
||||
while (--in_len > 0);
|
||||
|
||||
*out_len = pd(op, out);
|
||||
}
|
||||
else
|
||||
|
|
|
@ -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;
|
||||
*op++ = M3_MARKER | 0;
|
||||
|
||||
while (m_len > 255)
|
||||
{
|
||||
m_len -= 255;
|
||||
*op++ = 0;
|
||||
}
|
||||
|
||||
assert(m_len > 0);
|
||||
*op++ = LZO_BYTE(m_len);
|
||||
}
|
||||
|
||||
*op++ = LZO_BYTE((m_off & 63) << 2);
|
||||
*op++ = LZO_BYTE(m_off >> 6);
|
||||
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;
|
||||
|
||||
*op++ = 0;
|
||||
|
||||
while (tt > 255)
|
||||
{
|
||||
tt -= 255;
|
||||
*op++ = 0;
|
||||
}
|
||||
|
||||
assert(tt > 0);
|
||||
*op++ = LZO_BYTE(tt);
|
||||
}
|
||||
do *op++ = *ii++; while (--t > 0);
|
||||
|
||||
do* op++ = *ii++;
|
||||
|
||||
while (--t > 0);
|
||||
|
||||
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;
|
||||
|
||||
r = init_match(c, swd, NULL, 0, 0);
|
||||
|
||||
if (r != 0)
|
||||
return r;
|
||||
|
||||
if (max_chain > 0)
|
||||
swd->max_chain = max_chain;
|
||||
|
||||
r = find_match(c, swd, 0, 0);
|
||||
|
||||
if (r != 0)
|
||||
return r;
|
||||
|
||||
while (c->look > 0)
|
||||
{
|
||||
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;
|
||||
|
||||
assert(c->ip - c->look >= in);
|
||||
|
||||
if (lit == 0)
|
||||
ii = c->ip - c->look;
|
||||
|
||||
assert(ii + lit == 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)
|
||||
{
|
||||
r = find_match(c, swd, 1, 0);
|
||||
assert(r == 0); LZO_UNUSED(r);
|
||||
assert(r == 0);
|
||||
LZO_UNUSED(r);
|
||||
assert(c->look > 0);
|
||||
|
||||
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;
|
||||
assert(ii + lit + 1 == c->ip - c->look);
|
||||
}
|
||||
|
||||
assert(m_len > 0);
|
||||
}
|
||||
|
||||
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 */
|
||||
lit++;
|
||||
r = find_match(c, swd, 1, 0);
|
||||
assert(r == 0); LZO_UNUSED(r);
|
||||
assert(r == 0);
|
||||
LZO_UNUSED(r);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -278,7 +296,8 @@ lzo1f_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
|
|||
/* 2 - code match */
|
||||
op = code_match(c, op, m_len, m_off);
|
||||
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);
|
||||
|
|
|
@ -58,6 +58,7 @@ DO_DECOMPRESS ( const lzo_bytep in , lzo_uint in_len,
|
|||
while (TEST_IP_AND_TEST_OP)
|
||||
{
|
||||
t = *ip++;
|
||||
|
||||
if (t > 31)
|
||||
goto match;
|
||||
|
||||
|
@ -65,6 +66,7 @@ DO_DECOMPRESS ( const lzo_bytep in , lzo_uint in_len,
|
|||
if (t == 0)
|
||||
{
|
||||
NEED_IP(1);
|
||||
|
||||
while (*ip == 0)
|
||||
{
|
||||
t += 255;
|
||||
|
@ -72,22 +74,36 @@ DO_DECOMPRESS ( const lzo_bytep in , lzo_uint in_len,
|
|||
TEST_IV(t);
|
||||
NEED_IP(1);
|
||||
}
|
||||
|
||||
t += 31 + *ip++;
|
||||
}
|
||||
|
||||
/* 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 (t >= 4)
|
||||
{
|
||||
do {
|
||||
do
|
||||
{
|
||||
UA_COPY4(op, ip);
|
||||
op += 4; ip += 4; t -= 4;
|
||||
} while (t >= 4);
|
||||
if (t > 0) do *op++ = *ip++; while (--t > 0);
|
||||
op += 4;
|
||||
ip += 4;
|
||||
t -= 4;
|
||||
}
|
||||
while (t >= 4);
|
||||
|
||||
if (t > 0) do* op++ = *ip++;
|
||||
|
||||
while (--t > 0);
|
||||
}
|
||||
else
|
||||
#endif
|
||||
do *op++ = *ip++; while (--t > 0);
|
||||
do* op++ = *ip++;
|
||||
|
||||
while (--t > 0);
|
||||
|
||||
t = *ip++;
|
||||
|
||||
|
@ -99,27 +115,35 @@ DO_DECOMPRESS ( const lzo_bytep in , lzo_uint in_len,
|
|||
m_pos = op - 1 - 0x800;
|
||||
m_pos -= (t >> 2) & 7;
|
||||
m_pos -= *ip++ << 3;
|
||||
TEST_LB(m_pos); NEED_OP(3);
|
||||
*op++ = *m_pos++; *op++ = *m_pos++; *op++ = *m_pos++;
|
||||
TEST_LB(m_pos);
|
||||
NEED_OP(3);
|
||||
*op++ = *m_pos++;
|
||||
*op++ = *m_pos++;
|
||||
*op++ = *m_pos++;
|
||||
}
|
||||
else
|
||||
{
|
||||
match:
|
||||
|
||||
if (t < M3_MARKER)
|
||||
{
|
||||
m_pos = op - 1;
|
||||
m_pos -= (t >> 2) & 7;
|
||||
m_pos -= *ip++ << 3;
|
||||
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;
|
||||
}
|
||||
else
|
||||
{
|
||||
t &= 31;
|
||||
|
||||
if (t == 0)
|
||||
{
|
||||
NEED_IP(1);
|
||||
|
||||
while (*ip == 0)
|
||||
{
|
||||
t += 255;
|
||||
|
@ -127,8 +151,10 @@ match:
|
|||
TEST_OV(t);
|
||||
NEED_IP(1);
|
||||
}
|
||||
|
||||
t += 31 + *ip++;
|
||||
}
|
||||
|
||||
NEED_IP(2);
|
||||
m_pos = op;
|
||||
#if (LZO_OPT_UNALIGNED16) && (LZO_ABI_LITTLE_ENDIAN)
|
||||
|
@ -138,38 +164,64 @@ match:
|
|||
m_pos -= *ip++ >> 2;
|
||||
m_pos -= *ip++ << 6;
|
||||
#endif
|
||||
|
||||
if (m_pos == op)
|
||||
goto eof_found;
|
||||
}
|
||||
|
||||
/* 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 (t >= 2 * 4 - (3 - 1) && (op - m_pos) >= 4)
|
||||
{
|
||||
UA_COPY4(op, m_pos);
|
||||
op += 4; m_pos += 4; t -= 4 - (3 - 1);
|
||||
do {
|
||||
op += 4;
|
||||
m_pos += 4;
|
||||
t -= 4 - (3 - 1);
|
||||
|
||||
do
|
||||
{
|
||||
UA_COPY4(op, m_pos);
|
||||
op += 4; m_pos += 4; t -= 4;
|
||||
} while (t >= 4);
|
||||
if (t > 0) do *op++ = *m_pos++; while (--t > 0);
|
||||
op += 4;
|
||||
m_pos += 4;
|
||||
t -= 4;
|
||||
}
|
||||
while (t >= 4);
|
||||
|
||||
if (t > 0) do* op++ = *m_pos++;
|
||||
|
||||
while (--t > 0);
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
copy_match:
|
||||
*op++ = *m_pos++; *op++ = *m_pos++;
|
||||
do *op++ = *m_pos++; while (--t > 0);
|
||||
*op++ = *m_pos++;
|
||||
*op++ = *m_pos++;
|
||||
|
||||
do* op++ = *m_pos++;
|
||||
|
||||
while (--t > 0);
|
||||
}
|
||||
}
|
||||
|
||||
t = ip[-2] & 3;
|
||||
|
||||
if (t == 0)
|
||||
break;
|
||||
|
||||
/* copy literals */
|
||||
assert(t > 0); NEED_OP(t); NEED_IP(t+1);
|
||||
do *op++ = *ip++; while (--t > 0);
|
||||
assert(t > 0);
|
||||
NEED_OP(t);
|
||||
NEED_IP(t + 1);
|
||||
|
||||
do* op++ = *ip++;
|
||||
|
||||
while (--t > 0);
|
||||
|
||||
t = *ip++;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -140,10 +140,12 @@ code_match ( LZO_COMPRESS_T *c, lzo_bytep op, lzo_uint m_len, lzo_uint m_off )
|
|||
#endif
|
||||
|
||||
assert(op > c->out);
|
||||
|
||||
if (m_len == 2)
|
||||
{
|
||||
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;
|
||||
#if defined(LZO1Z)
|
||||
*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
|
||||
c->m1a_m++;
|
||||
}
|
||||
|
||||
#if defined(LZO1Z)
|
||||
else if (m_len <= M2_MAX_LEN && (m_off <= M2_MAX_OFFSET || m_off == c->last_m_off))
|
||||
#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);
|
||||
assert(op[-2] >= M2_MARKER);
|
||||
#elif defined(LZO1Z)
|
||||
|
||||
if (m_off == c->last_m_off)
|
||||
*op++ = LZO_BYTE(((m_len - 1) << 5) | (0x700 >> 6));
|
||||
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_off << 2);
|
||||
}
|
||||
|
||||
#endif
|
||||
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);
|
||||
m_off -= 1;
|
||||
|
||||
if (m_len <= M3_MAX_LEN)
|
||||
*op++ = LZO_BYTE(M3_MARKER | (m_len - 2));
|
||||
else
|
||||
{
|
||||
m_len -= M3_MAX_LEN;
|
||||
*op++ = M3_MARKER | 0;
|
||||
|
||||
while (m_len > 255)
|
||||
{
|
||||
m_len -= 255;
|
||||
*op++ = 0;
|
||||
}
|
||||
|
||||
assert(m_len > 0);
|
||||
*op++ = LZO_BYTE(m_len);
|
||||
}
|
||||
|
||||
#if defined(LZO1Z)
|
||||
*op++ = LZO_BYTE(m_off >> 6);
|
||||
*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;
|
||||
|
||||
assert(m_len >= 3);
|
||||
assert(m_off > 0x4000); assert(m_off <= 0xbfff);
|
||||
assert(m_off > 0x4000);
|
||||
assert(m_off <= 0xbfff);
|
||||
m_off -= 0x4000;
|
||||
k = (m_off & 0x4000) >> 11;
|
||||
|
||||
if (m_len <= M4_MAX_LEN)
|
||||
*op++ = LZO_BYTE(M4_MARKER | k | (m_len - 2));
|
||||
else
|
||||
{
|
||||
m_len -= M4_MAX_LEN;
|
||||
*op++ = LZO_BYTE(M4_MARKER | k | 0);
|
||||
|
||||
while (m_len > 255)
|
||||
{
|
||||
m_len -= 255;
|
||||
*op++ = 0;
|
||||
}
|
||||
|
||||
assert(m_len > 0);
|
||||
*op++ = LZO_BYTE(m_len);
|
||||
}
|
||||
|
||||
#if defined(LZO1Z)
|
||||
*op++ = LZO_BYTE(m_off >> 6);
|
||||
*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;
|
||||
|
||||
*op++ = 0;
|
||||
|
||||
while (tt > 255)
|
||||
{
|
||||
tt -= 255;
|
||||
*op++ = 0;
|
||||
}
|
||||
|
||||
assert(tt > 0);
|
||||
*op++ = LZO_BYTE(tt);
|
||||
c->lit3_r++;
|
||||
}
|
||||
do *op++ = *ii++; while (--t > 0);
|
||||
|
||||
do* op++ = *ii++;
|
||||
|
||||
while (--t > 0);
|
||||
|
||||
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)
|
||||
return 0;
|
||||
|
||||
if (m_len == 2)
|
||||
return (m_off <= M1_MAX_OFFSET && lit > 0 && lit < 4) ? 2 : 0;
|
||||
|
||||
if (m_len <= M2_MAX_LEN && m_off <= M2_MAX_OFFSET)
|
||||
return 2;
|
||||
|
||||
if (m_len == M2_MIN_LEN && m_off <= MX_MAX_OFFSET && lit >= 4)
|
||||
return 2;
|
||||
|
||||
if (m_off <= M3_MAX_OFFSET)
|
||||
{
|
||||
if (m_len <= M3_MAX_LEN)
|
||||
return 3;
|
||||
|
||||
m_len -= M3_MAX_LEN;
|
||||
|
||||
while (m_len > 255)
|
||||
{
|
||||
m_len -= 255;
|
||||
n++;
|
||||
}
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
if (m_off <= M4_MAX_OFFSET)
|
||||
{
|
||||
if (m_len <= M4_MAX_LEN)
|
||||
return 3;
|
||||
|
||||
m_len -= M4_MAX_LEN;
|
||||
|
||||
while (m_len > 255)
|
||||
{
|
||||
m_len -= 255;
|
||||
n++;
|
||||
}
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
#if 0
|
||||
|
||||
if (l3)
|
||||
lit2 -= ahead;
|
||||
|
||||
#endif
|
||||
|
||||
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 += (l2 - l1) * 2;
|
||||
|
||||
if (l3)
|
||||
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;
|
||||
|
||||
#if 0
|
||||
|
||||
if (l1 == 2)
|
||||
if (lazy_match_min_gain == 0)
|
||||
lazy_match_min_gain = 1;
|
||||
|
||||
#endif
|
||||
|
||||
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;
|
||||
|
||||
assert(m_len >= 2);
|
||||
|
||||
if (m_off <= (lzo_uint)(c->bp - c->in))
|
||||
{
|
||||
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);
|
||||
d_off = m_off - (lzo_uint)(c->bp - c->in);
|
||||
assert(d_off <= swd->dict_len);
|
||||
|
||||
if (m_len > d_off)
|
||||
{
|
||||
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)
|
||||
return;
|
||||
|
||||
#if defined(LZO1Z)
|
||||
|
||||
if (*m_off == c->last_m_off && *m_len <= M2_MAX_LEN)
|
||||
return;
|
||||
|
||||
#if 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)
|
||||
{
|
||||
|
@ -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];
|
||||
return;
|
||||
}
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
@ -475,6 +518,7 @@ better_match ( const lzo_swd_p swd, lzo_uint *m_len, lzo_uint *m_off )
|
|||
return;
|
||||
|
||||
#if 1
|
||||
|
||||
/* M3/M4 -> M2 */
|
||||
if (*m_off > M2_MAX_OFFSET &&
|
||||
*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];
|
||||
return;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#if 1
|
||||
|
||||
/* M4 -> M2 */
|
||||
if (*m_off > M3_MAX_OFFSET &&
|
||||
*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];
|
||||
return;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#if 1
|
||||
|
||||
/* M4 -> M3 */
|
||||
if (*m_off > M3_MAX_OFFSET &&
|
||||
*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_off = swd->best_off[*m_len];
|
||||
}
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
|
@ -554,17 +603,22 @@ lzo1x_999_compress_internal ( const lzo_bytep in , lzo_uint in_len,
|
|||
/* setup parameter defaults */
|
||||
/* number of lazy match tries */
|
||||
try_lazy = (lzo_uint) try_lazy_parm;
|
||||
|
||||
if (try_lazy_parm < 0)
|
||||
try_lazy = 1;
|
||||
|
||||
/* reduce lazy match search if we already have a match with this length */
|
||||
if (good_length == 0)
|
||||
good_length = 32;
|
||||
|
||||
/* do not try a lazy match if we already have a match with this length */
|
||||
if (max_lazy == 0)
|
||||
max_lazy = 32;
|
||||
|
||||
/* stop searching for longer matches than this one */
|
||||
if (nice_length == 0)
|
||||
nice_length = 0;
|
||||
|
||||
/* don't search more positions than this */
|
||||
if (max_chain == 0)
|
||||
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;
|
||||
|
||||
r = init_match(c, swd, dict, dict_len, flags);
|
||||
|
||||
if (r != 0)
|
||||
return r;
|
||||
|
||||
if (max_chain > 0)
|
||||
swd->max_chain = max_chain;
|
||||
|
||||
if (nice_length > 0)
|
||||
swd->nice_length = nice_length;
|
||||
|
||||
r = find_match(c, swd, 0, 0);
|
||||
|
||||
if (r != 0)
|
||||
return r;
|
||||
|
||||
while (c->look > 0)
|
||||
{
|
||||
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 >= in);
|
||||
|
||||
if (lit == 0)
|
||||
ii = c->bp;
|
||||
|
||||
assert(ii + lit == 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++;
|
||||
swd->max_chain = max_chain;
|
||||
r = find_match(c, swd, 1, 0);
|
||||
assert(r == 0); LZO_UNUSED(r);
|
||||
assert(r == 0);
|
||||
LZO_UNUSED(r);
|
||||
continue;
|
||||
}
|
||||
|
||||
/* a match */
|
||||
#if defined(SWD_BEST_OFF)
|
||||
|
||||
if (swd->use_best_off)
|
||||
better_match(swd, &m_len, &m_off);
|
||||
|
||||
#endif
|
||||
assert_match(swd, m_len, m_off);
|
||||
|
||||
|
||||
/* shall we try a lazy match ? */
|
||||
ahead = 0;
|
||||
|
||||
if (try_lazy == 0 || m_len >= max_lazy)
|
||||
{
|
||||
/* no */
|
||||
|
@ -679,34 +744,48 @@ lzo1x_999_compress_internal ( const lzo_bytep in , lzo_uint in_len,
|
|||
swd->max_chain = max_chain >> 2;
|
||||
else
|
||||
swd->max_chain = max_chain;
|
||||
|
||||
r = find_match(c, swd, 1, 0);
|
||||
ahead++;
|
||||
|
||||
assert(r == 0); LZO_UNUSED(r);
|
||||
assert(r == 0);
|
||||
LZO_UNUSED(r);
|
||||
assert(c->look > 0);
|
||||
assert(ii + lit + ahead == c->bp);
|
||||
|
||||
#if defined(LZO1Z)
|
||||
|
||||
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)
|
||||
c->m_len = 0;
|
||||
|
||||
#endif
|
||||
|
||||
if (c->m_len < m_len)
|
||||
continue;
|
||||
|
||||
#if 1
|
||||
|
||||
if (c->m_len == m_len && c->m_off >= m_off)
|
||||
continue;
|
||||
|
||||
#endif
|
||||
#if defined(SWD_BEST_OFF)
|
||||
|
||||
if (swd->use_best_off)
|
||||
better_match(swd, &c->m_len, &c->m_off);
|
||||
|
||||
#endif
|
||||
l2 = len_of_coded_match(c->m_len, c->m_off, lit + ahead);
|
||||
|
||||
if (l2 == 0)
|
||||
continue;
|
||||
|
||||
#if 0
|
||||
|
||||
if (c->m_len == m_len && l2 >= l1)
|
||||
continue;
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
@ -718,6 +797,7 @@ lzo1x_999_compress_internal ( const lzo_bytep in , lzo_uint in_len,
|
|||
#endif
|
||||
|
||||
lazy_match_min_gain = min_gain(ahead, lit, lit + ahead, l1, l2, l3);
|
||||
|
||||
if (c->m_len >= m_len + lazy_match_min_gain)
|
||||
{
|
||||
c->lazy++;
|
||||
|
@ -736,6 +816,7 @@ lzo1x_999_compress_internal ( const lzo_bytep in , lzo_uint in_len,
|
|||
lit += ahead;
|
||||
assert(ii + lit == c->bp);
|
||||
}
|
||||
|
||||
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);
|
||||
swd->max_chain = max_chain;
|
||||
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 max_chain;
|
||||
lzo_uint32_t flags;
|
||||
} c[9] = {
|
||||
} c[9] =
|
||||
{
|
||||
/* faster compression */
|
||||
{ 0, 0, 0, 8, 4, 0 },
|
||||
{ 0, 0, 0, 16, 8, 0 },
|
||||
|
|
|
@ -54,6 +54,7 @@ do_compress ( const lzo_bytep in , lzo_uint in_len,
|
|||
ii = ip;
|
||||
|
||||
ip += ti < 4 ? 4 - ti : 0;
|
||||
|
||||
for (;;)
|
||||
{
|
||||
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 dindex;
|
||||
next:
|
||||
|
||||
if __lzo_unlikely(ip >= ip_end)
|
||||
break;
|
||||
|
||||
DINDEX1(dindex, ip);
|
||||
GINDEX(m_pos, m_off, dict, dindex, in);
|
||||
|
||||
if (LZO_CHECK_MPOS_NON_DET(m_pos, m_off, in, ip, M4_MAX_OFFSET))
|
||||
goto literal;
|
||||
|
||||
#if 1
|
||||
|
||||
if (m_off <= M2_MAX_OFFSET || m_pos[3] == ip[3])
|
||||
goto try_match;
|
||||
|
||||
DINDEX2(dindex, ip);
|
||||
#endif
|
||||
GINDEX(m_pos, m_off, dict, dindex, in);
|
||||
|
||||
if (LZO_CHECK_MPOS_NON_DET(m_pos, m_off, in, ip, M4_MAX_OFFSET))
|
||||
goto literal;
|
||||
|
||||
if (m_off <= M2_MAX_OFFSET || m_pos[3] == ip[3])
|
||||
goto try_match;
|
||||
|
||||
goto literal;
|
||||
|
||||
try_match:
|
||||
#if (LZO_OPT_UNALIGNED32)
|
||||
|
||||
if (UA_GET_NE32(m_pos) != UA_GET_NE32(ip))
|
||||
#else
|
||||
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);
|
||||
continue;
|
||||
}
|
||||
|
||||
/*match:*/
|
||||
UPDATE_I(dict, 0, dindex, ip, in);
|
||||
#else
|
||||
|
@ -104,12 +116,15 @@ literal:
|
|||
literal:
|
||||
ip += 1 + ((ip - ii) >> 5);
|
||||
next:
|
||||
|
||||
if __lzo_unlikely(ip >= ip_end)
|
||||
break;
|
||||
|
||||
dv = UA_GET_LE32(ip);
|
||||
dindex = DINDEX(dv, ip);
|
||||
GINDEX(m_off, m_pos, in + dict, dindex, in);
|
||||
UPDATE_I(dict, 0, dindex, ip, in);
|
||||
|
||||
if __lzo_unlikely(dv != UA_GET_LE32(m_pos))
|
||||
goto literal;
|
||||
}
|
||||
|
@ -117,9 +132,11 @@ next:
|
|||
|
||||
/* a match */
|
||||
|
||||
ii -= ti; ti = 0;
|
||||
ii -= ti;
|
||||
ti = 0;
|
||||
{
|
||||
lzo_uint t = pd(ip, ii);
|
||||
|
||||
if (t != 0)
|
||||
{
|
||||
if (t <= 3)
|
||||
|
@ -132,6 +149,7 @@ next:
|
|||
{ do* op++ = *ii++; while (--t > 0); }
|
||||
#endif
|
||||
}
|
||||
|
||||
#if (LZO_OPT_UNALIGNED32) || (LZO_OPT_UNALIGNED64)
|
||||
else if (t <= 16)
|
||||
{
|
||||
|
@ -140,6 +158,7 @@ next:
|
|||
UA_COPY8(op + 8, ii + 8);
|
||||
op += t;
|
||||
}
|
||||
|
||||
#endif
|
||||
else
|
||||
{
|
||||
|
@ -149,21 +168,31 @@ next:
|
|||
{
|
||||
lzo_uint tt = t - 18;
|
||||
*op++ = 0;
|
||||
|
||||
while __lzo_unlikely(tt > 255)
|
||||
{
|
||||
tt -= 255;
|
||||
UA_SET1(op, 0);
|
||||
op++;
|
||||
}
|
||||
|
||||
assert(tt > 0);
|
||||
*op++ = LZO_BYTE(tt);
|
||||
}
|
||||
|
||||
#if (LZO_OPT_UNALIGNED32) || (LZO_OPT_UNALIGNED64)
|
||||
do {
|
||||
|
||||
do
|
||||
{
|
||||
UA_COPY8(op, ii);
|
||||
UA_COPY8(op + 8, ii + 8);
|
||||
op += 16; ii += 16; t -= 16;
|
||||
} while (t >= 16); if (t > 0)
|
||||
op += 16;
|
||||
ii += 16;
|
||||
t -= 16;
|
||||
}
|
||||
while (t >= 16);
|
||||
|
||||
if (t > 0)
|
||||
#endif
|
||||
{ do* op++ = *ii++; while (--t > 0); }
|
||||
}
|
||||
|
@ -174,102 +203,161 @@ next:
|
|||
#if (LZO_OPT_UNALIGNED64)
|
||||
lzo_uint64_t v;
|
||||
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;
|
||||
v = UA_GET_NE64(ip + m_len) ^ UA_GET_NE64(m_pos + m_len);
|
||||
|
||||
if __lzo_unlikely(ip + m_len >= ip_end)
|
||||
goto m_len_done;
|
||||
} while (v == 0);
|
||||
}
|
||||
while (v == 0);
|
||||
}
|
||||
|
||||
#if (LZO_ABI_BIG_ENDIAN) && defined(lzo_bitops_ctlz64)
|
||||
m_len += lzo_bitops_ctlz64(v) / CHAR_BIT;
|
||||
#elif (LZO_ABI_BIG_ENDIAN)
|
||||
if ((v >> (64 - CHAR_BIT)) == 0) do {
|
||||
|
||||
if ((v >> (64 - CHAR_BIT)) == 0) do
|
||||
{
|
||||
v <<= CHAR_BIT;
|
||||
m_len += 1;
|
||||
} while ((v >> (64 - CHAR_BIT)) == 0);
|
||||
}
|
||||
while ((v >> (64 - CHAR_BIT)) == 0);
|
||||
|
||||
#elif (LZO_ABI_LITTLE_ENDIAN) && defined(lzo_bitops_cttz64)
|
||||
m_len += lzo_bitops_cttz64(v) / CHAR_BIT;
|
||||
#elif (LZO_ABI_LITTLE_ENDIAN)
|
||||
if ((v & UCHAR_MAX) == 0) do {
|
||||
|
||||
if ((v & UCHAR_MAX) == 0) do
|
||||
{
|
||||
v >>= CHAR_BIT;
|
||||
m_len += 1;
|
||||
} while ((v & UCHAR_MAX) == 0);
|
||||
}
|
||||
while ((v & UCHAR_MAX) == 0);
|
||||
|
||||
#else
|
||||
if (ip[m_len] == m_pos[m_len]) do {
|
||||
|
||||
if (ip[m_len] == m_pos[m_len]) do
|
||||
{
|
||||
m_len += 1;
|
||||
} while (ip[m_len] == m_pos[m_len]);
|
||||
}
|
||||
while (ip[m_len] == m_pos[m_len]);
|
||||
|
||||
#endif
|
||||
#elif (LZO_OPT_UNALIGNED32)
|
||||
lzo_uint32_t v;
|
||||
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;
|
||||
v = UA_GET_NE32(ip + m_len) ^ UA_GET_NE32(m_pos + m_len);
|
||||
|
||||
if (v != 0)
|
||||
break;
|
||||
|
||||
m_len += 4;
|
||||
v = UA_GET_NE32(ip + m_len) ^ UA_GET_NE32(m_pos + m_len);
|
||||
|
||||
if __lzo_unlikely(ip + m_len >= ip_end)
|
||||
goto m_len_done;
|
||||
} while (v == 0);
|
||||
}
|
||||
while (v == 0);
|
||||
}
|
||||
|
||||
#if (LZO_ABI_BIG_ENDIAN) && defined(lzo_bitops_ctlz32)
|
||||
m_len += lzo_bitops_ctlz32(v) / CHAR_BIT;
|
||||
#elif (LZO_ABI_BIG_ENDIAN)
|
||||
if ((v >> (32 - CHAR_BIT)) == 0) do {
|
||||
|
||||
if ((v >> (32 - CHAR_BIT)) == 0) do
|
||||
{
|
||||
v <<= CHAR_BIT;
|
||||
m_len += 1;
|
||||
} while ((v >> (32 - CHAR_BIT)) == 0);
|
||||
}
|
||||
while ((v >> (32 - CHAR_BIT)) == 0);
|
||||
|
||||
#elif (LZO_ABI_LITTLE_ENDIAN) && defined(lzo_bitops_cttz32)
|
||||
m_len += lzo_bitops_cttz32(v) / CHAR_BIT;
|
||||
#elif (LZO_ABI_LITTLE_ENDIAN)
|
||||
if ((v & UCHAR_MAX) == 0) do {
|
||||
|
||||
if ((v & UCHAR_MAX) == 0) do
|
||||
{
|
||||
v >>= CHAR_BIT;
|
||||
m_len += 1;
|
||||
} while ((v & UCHAR_MAX) == 0);
|
||||
}
|
||||
while ((v & UCHAR_MAX) == 0);
|
||||
|
||||
#else
|
||||
if (ip[m_len] == m_pos[m_len]) do {
|
||||
|
||||
if (ip[m_len] == m_pos[m_len]) do
|
||||
{
|
||||
m_len += 1;
|
||||
} while (ip[m_len] == m_pos[m_len]);
|
||||
}
|
||||
while (ip[m_len] == m_pos[m_len]);
|
||||
|
||||
#endif
|
||||
#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;
|
||||
|
||||
if (ip[m_len] != m_pos[m_len])
|
||||
break;
|
||||
|
||||
m_len += 1;
|
||||
|
||||
if (ip[m_len] != m_pos[m_len])
|
||||
break;
|
||||
|
||||
m_len += 1;
|
||||
|
||||
if (ip[m_len] != m_pos[m_len])
|
||||
break;
|
||||
|
||||
m_len += 1;
|
||||
|
||||
if (ip[m_len] != m_pos[m_len])
|
||||
break;
|
||||
|
||||
m_len += 1;
|
||||
|
||||
if (ip[m_len] != m_pos[m_len])
|
||||
break;
|
||||
|
||||
m_len += 1;
|
||||
|
||||
if (ip[m_len] != m_pos[m_len])
|
||||
break;
|
||||
|
||||
m_len += 1;
|
||||
|
||||
if (ip[m_len] != m_pos[m_len])
|
||||
break;
|
||||
|
||||
m_len += 1;
|
||||
|
||||
if __lzo_unlikely(ip + m_len >= ip_end)
|
||||
goto m_len_done;
|
||||
} while (ip[m_len] == m_pos[m_len]);
|
||||
}
|
||||
while (ip[m_len] == m_pos[m_len]);
|
||||
}
|
||||
|
||||
#endif
|
||||
}
|
||||
m_len_done:
|
||||
m_off = pd(ip, m_pos);
|
||||
ip += m_len;
|
||||
ii = ip;
|
||||
|
||||
if (m_len <= M2_MAX_LEN && m_off <= M2_MAX_OFFSET)
|
||||
{
|
||||
m_off -= 1;
|
||||
|
@ -284,43 +372,52 @@ m_len_done:
|
|||
else if (m_off <= M3_MAX_OFFSET)
|
||||
{
|
||||
m_off -= 1;
|
||||
|
||||
if (m_len <= M3_MAX_LEN)
|
||||
*op++ = LZO_BYTE(M3_MARKER | (m_len - 2));
|
||||
else
|
||||
{
|
||||
m_len -= M3_MAX_LEN;
|
||||
*op++ = M3_MARKER | 0;
|
||||
|
||||
while __lzo_unlikely(m_len > 255)
|
||||
{
|
||||
m_len -= 255;
|
||||
UA_SET1(op, 0);
|
||||
op++;
|
||||
}
|
||||
|
||||
*op++ = LZO_BYTE(m_len);
|
||||
}
|
||||
|
||||
*op++ = LZO_BYTE(m_off << 2);
|
||||
*op++ = LZO_BYTE(m_off >> 6);
|
||||
}
|
||||
else
|
||||
{
|
||||
m_off -= 0x4000;
|
||||
|
||||
if (m_len <= M4_MAX_LEN)
|
||||
*op++ = LZO_BYTE(M4_MARKER | ((m_off >> 11) & 8) | (m_len - 2));
|
||||
else
|
||||
{
|
||||
m_len -= M4_MAX_LEN;
|
||||
*op++ = LZO_BYTE(M4_MARKER | ((m_off >> 11) & 8));
|
||||
|
||||
while __lzo_unlikely(m_len > 255)
|
||||
{
|
||||
m_len -= 255;
|
||||
UA_SET1(op, 0);
|
||||
op++;
|
||||
}
|
||||
|
||||
*op++ = LZO_BYTE(m_len);
|
||||
}
|
||||
|
||||
*op++ = LZO_BYTE(m_off << 2);
|
||||
*op++ = LZO_BYTE(m_off >> 6);
|
||||
}
|
||||
|
||||
goto next;
|
||||
}
|
||||
|
||||
|
@ -351,8 +448,10 @@ DO_COMPRESS ( const lzo_bytep in , lzo_uint in_len,
|
|||
ll = LZO_MIN(ll, 49152);
|
||||
#endif
|
||||
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)
|
||||
break;
|
||||
|
||||
#if (LZO_DETERMINISTIC)
|
||||
lzo_memset(wrkmem, 0, ((lzo_uint)1 << D_BITS) * sizeof(lzo_dict_t));
|
||||
#endif
|
||||
|
@ -361,6 +460,7 @@ DO_COMPRESS ( const lzo_bytep in , lzo_uint in_len,
|
|||
op += *out_len;
|
||||
l -= ll;
|
||||
}
|
||||
|
||||
t += l;
|
||||
|
||||
if (t > 0)
|
||||
|
@ -378,15 +478,18 @@ DO_COMPRESS ( const lzo_bytep in , lzo_uint in_len,
|
|||
lzo_uint tt = t - 18;
|
||||
|
||||
*op++ = 0;
|
||||
|
||||
while (tt > 255)
|
||||
{
|
||||
tt -= 255;
|
||||
UA_SET1(op, 0);
|
||||
op++;
|
||||
}
|
||||
|
||||
assert(tt > 0);
|
||||
*op++ = LZO_BYTE(tt);
|
||||
}
|
||||
|
||||
UA_COPYN(op, ii, t);
|
||||
op += t;
|
||||
}
|
||||
|
|
|
@ -61,6 +61,7 @@ DO_DECOMPRESS ( const lzo_bytep in , lzo_uint in_len,
|
|||
LZO_UNUSED(wrkmem);
|
||||
|
||||
#if defined(COPY_DICT)
|
||||
|
||||
if (dict)
|
||||
{
|
||||
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_len = M4_MAX_OFFSET;
|
||||
}
|
||||
|
||||
dict_end = dict + dict_len;
|
||||
}
|
||||
else
|
||||
|
@ -75,6 +77,7 @@ DO_DECOMPRESS ( const lzo_bytep in , lzo_uint in_len,
|
|||
dict_len = 0;
|
||||
dict_end = NULL;
|
||||
}
|
||||
|
||||
#endif /* COPY_DICT */
|
||||
|
||||
*out_len = 0;
|
||||
|
@ -83,13 +86,22 @@ DO_DECOMPRESS ( const lzo_bytep in , lzo_uint in_len,
|
|||
ip = in;
|
||||
|
||||
NEED_IP(1);
|
||||
|
||||
if (*ip > 17)
|
||||
{
|
||||
t = *ip++ - 17;
|
||||
|
||||
if (t < 4)
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -97,8 +109,10 @@ DO_DECOMPRESS ( const lzo_bytep in , lzo_uint in_len,
|
|||
{
|
||||
NEED_IP(3);
|
||||
t = *ip++;
|
||||
|
||||
if (t >= 16)
|
||||
goto match;
|
||||
|
||||
/* a literal run */
|
||||
if (t == 0)
|
||||
{
|
||||
|
@ -109,47 +123,74 @@ DO_DECOMPRESS ( const lzo_bytep in , lzo_uint in_len,
|
|||
TEST_IV(t);
|
||||
NEED_IP(1);
|
||||
}
|
||||
|
||||
t += 15 + *ip++;
|
||||
}
|
||||
|
||||
/* 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)
|
||||
t += 3;
|
||||
|
||||
if (t >= 8) do
|
||||
{
|
||||
UA_COPY8(op, ip);
|
||||
op += 8; ip += 8; t -= 8;
|
||||
} while (t >= 8);
|
||||
op += 8;
|
||||
ip += 8;
|
||||
t -= 8;
|
||||
}
|
||||
while (t >= 8);
|
||||
|
||||
if (t >= 4)
|
||||
{
|
||||
UA_COPY4(op, ip);
|
||||
op += 4; ip += 4; t -= 4;
|
||||
op += 4;
|
||||
ip += 4;
|
||||
t -= 4;
|
||||
}
|
||||
|
||||
if (t > 0)
|
||||
{
|
||||
*op++ = *ip++;
|
||||
|
||||
if (t > 1) { *op++ = *ip++; if (t > 2) { *op++ = *ip++; } }
|
||||
}
|
||||
|
||||
#elif (LZO_OPT_UNALIGNED32) || (LZO_ALIGNED_OK_4)
|
||||
#if !(LZO_OPT_UNALIGNED32)
|
||||
|
||||
if (PTR_ALIGNED2_4(op, ip))
|
||||
{
|
||||
#endif
|
||||
UA_COPY4(op, ip);
|
||||
op += 4; ip += 4;
|
||||
op += 4;
|
||||
ip += 4;
|
||||
|
||||
if (--t > 0)
|
||||
{
|
||||
if (t >= 4)
|
||||
{
|
||||
do {
|
||||
do
|
||||
{
|
||||
UA_COPY4(op, ip);
|
||||
op += 4; ip += 4; t -= 4;
|
||||
} while (t >= 4);
|
||||
if (t > 0) do *op++ = *ip++; while (--t > 0);
|
||||
op += 4;
|
||||
ip += 4;
|
||||
t -= 4;
|
||||
}
|
||||
while (t >= 4);
|
||||
|
||||
if (t > 0) do* op++ = *ip++;
|
||||
|
||||
while (--t > 0);
|
||||
}
|
||||
else
|
||||
do *op++ = *ip++; while (--t > 0);
|
||||
do* op++ = *ip++;
|
||||
|
||||
while (--t > 0);
|
||||
}
|
||||
|
||||
#if !(LZO_OPT_UNALIGNED32)
|
||||
}
|
||||
else
|
||||
|
@ -157,9 +198,15 @@ DO_DECOMPRESS ( const lzo_bytep in , lzo_uint in_len,
|
|||
#endif
|
||||
#if !(LZO_OPT_UNALIGNED32)
|
||||
{
|
||||
*op++ = *ip++; *op++ = *ip++; *op++ = *ip++;
|
||||
do *op++ = *ip++; while (--t > 0);
|
||||
*op++ = *ip++;
|
||||
*op++ = *ip++;
|
||||
*op++ = *ip++;
|
||||
|
||||
do* op++ = *ip++;
|
||||
|
||||
while (--t > 0);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
@ -167,8 +214,10 @@ first_literal_run:
|
|||
|
||||
|
||||
t = *ip++;
|
||||
|
||||
if (t >= 16)
|
||||
goto match;
|
||||
|
||||
#if defined(COPY_DICT)
|
||||
#if defined(LZO1Z)
|
||||
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);
|
||||
#endif
|
||||
NEED_OP(3);
|
||||
t = 3; COPY_DICT(t,m_off)
|
||||
t = 3;
|
||||
COPY_DICT(t, m_off)
|
||||
#else /* !COPY_DICT */
|
||||
#if defined(LZO1Z)
|
||||
t = (1 + M2_MAX_OFFSET) + (t << 6) + (*ip++ >> 2);
|
||||
|
@ -188,15 +238,20 @@ first_literal_run:
|
|||
m_pos -= t >> 2;
|
||||
m_pos -= *ip++ << 2;
|
||||
#endif
|
||||
TEST_LB(m_pos); NEED_OP(3);
|
||||
*op++ = *m_pos++; *op++ = *m_pos++; *op++ = *m_pos;
|
||||
TEST_LB(m_pos);
|
||||
NEED_OP(3);
|
||||
*op++ = *m_pos++;
|
||||
*op++ = *m_pos++;
|
||||
*op++ = *m_pos;
|
||||
#endif /* COPY_DICT */
|
||||
goto match_done;
|
||||
|
||||
|
||||
/* handle matches */
|
||||
for (;;) {
|
||||
for (;;)
|
||||
{
|
||||
match:
|
||||
|
||||
if (t >= 64) /* a M2 match */
|
||||
{
|
||||
#if defined(COPY_DICT)
|
||||
|
@ -208,6 +263,7 @@ match:
|
|||
t = (t >> 4) - 3;
|
||||
#elif defined(LZO1Z)
|
||||
m_off = t & 0x1f;
|
||||
|
||||
if (m_off >= 0x1c)
|
||||
m_off = last_m_off;
|
||||
else
|
||||
|
@ -215,6 +271,7 @@ match:
|
|||
m_off = 1 + (m_off << 6) + (*ip++ >> 2);
|
||||
last_m_off = m_off;
|
||||
}
|
||||
|
||||
t = (t >> 5) - 1;
|
||||
#endif
|
||||
#else /* !COPY_DICT */
|
||||
|
@ -232,6 +289,7 @@ match:
|
|||
{
|
||||
lzo_uint off = t & 0x1f;
|
||||
m_pos = op;
|
||||
|
||||
if (off >= 0x1c)
|
||||
{
|
||||
assert(last_m_off > 0);
|
||||
|
@ -246,13 +304,16 @@ match:
|
|||
}
|
||||
t = (t >> 5) - 1;
|
||||
#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;
|
||||
#endif /* COPY_DICT */
|
||||
}
|
||||
else if (t >= 32) /* a M3 match */
|
||||
{
|
||||
t &= 31;
|
||||
|
||||
if (t == 0)
|
||||
{
|
||||
while (*ip == 0)
|
||||
|
@ -262,9 +323,11 @@ match:
|
|||
TEST_OV(t);
|
||||
NEED_IP(1);
|
||||
}
|
||||
|
||||
t += 31 + *ip++;
|
||||
NEED_IP(2);
|
||||
}
|
||||
|
||||
#if defined(COPY_DICT)
|
||||
#if defined(LZO1Z)
|
||||
m_off = 1 + (ip[0] << 6) + (ip[1] >> 2);
|
||||
|
@ -298,6 +361,7 @@ match:
|
|||
m_pos -= (t & 8) << 11;
|
||||
#endif /* COPY_DICT */
|
||||
t &= 7;
|
||||
|
||||
if (t == 0)
|
||||
{
|
||||
while (*ip == 0)
|
||||
|
@ -307,9 +371,11 @@ match:
|
|||
TEST_OV(t);
|
||||
NEED_IP(1);
|
||||
}
|
||||
|
||||
t += 7 + *ip++;
|
||||
NEED_IP(2);
|
||||
}
|
||||
|
||||
#if defined(COPY_DICT)
|
||||
#if defined(LZO1Z)
|
||||
m_off += (ip[0] << 6) + (ip[1] >> 2);
|
||||
|
@ -317,8 +383,10 @@ match:
|
|||
m_off += (ip[0] >> 2) + (ip[1] << 6);
|
||||
#endif
|
||||
ip += 2;
|
||||
|
||||
if (m_off == 0)
|
||||
goto eof_found;
|
||||
|
||||
m_off += 0x4000;
|
||||
#if defined(LZO1Z)
|
||||
last_m_off = m_off;
|
||||
|
@ -332,8 +400,10 @@ match:
|
|||
m_pos -= (ip[0] >> 2) + (ip[1] << 6);
|
||||
#endif
|
||||
ip += 2;
|
||||
|
||||
if (m_pos == op)
|
||||
goto eof_found;
|
||||
|
||||
m_pos -= 0x4000;
|
||||
#if defined(LZO1Z)
|
||||
last_m_off = pd((const lzo_bytep)op, m_pos);
|
||||
|
@ -350,7 +420,8 @@ match:
|
|||
m_off = 1 + (t >> 2) + (*ip++ << 2);
|
||||
#endif
|
||||
NEED_OP(2);
|
||||
t = 2; COPY_DICT(t,m_off)
|
||||
t = 2;
|
||||
COPY_DICT(t, m_off)
|
||||
#else /* !COPY_DICT */
|
||||
#if defined(LZO1Z)
|
||||
t = 1 + (t << 6) + (*ip++ >> 2);
|
||||
|
@ -361,8 +432,10 @@ match:
|
|||
m_pos -= t >> 2;
|
||||
m_pos -= *ip++ << 2;
|
||||
#endif
|
||||
TEST_LB(m_pos); NEED_OP(2);
|
||||
*op++ = *m_pos++; *op++ = *m_pos;
|
||||
TEST_LB(m_pos);
|
||||
NEED_OP(2);
|
||||
*op++ = *m_pos++;
|
||||
*op++ = *m_pos;
|
||||
#endif /* COPY_DICT */
|
||||
goto match_done;
|
||||
}
|
||||
|
@ -371,28 +444,41 @@ match:
|
|||
#if defined(COPY_DICT)
|
||||
|
||||
NEED_OP(t + 3 - 1);
|
||||
t += 3-1; COPY_DICT(t,m_off)
|
||||
t += 3 - 1;
|
||||
COPY_DICT(t, m_off)
|
||||
|
||||
#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 (op - m_pos >= 8)
|
||||
{
|
||||
t += (3 - 1);
|
||||
|
||||
if (t >= 8) do
|
||||
{
|
||||
UA_COPY8(op, m_pos);
|
||||
op += 8; m_pos += 8; t -= 8;
|
||||
} while (t >= 8);
|
||||
op += 8;
|
||||
m_pos += 8;
|
||||
t -= 8;
|
||||
}
|
||||
while (t >= 8);
|
||||
|
||||
if (t >= 4)
|
||||
{
|
||||
UA_COPY4(op, m_pos);
|
||||
op += 4; m_pos += 4; t -= 4;
|
||||
op += 4;
|
||||
m_pos += 4;
|
||||
t -= 4;
|
||||
}
|
||||
|
||||
if (t > 0)
|
||||
{
|
||||
*op++ = m_pos[0];
|
||||
|
||||
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 */
|
||||
#else
|
||||
|
||||
if (t >= 2 * 4 - (3 - 1) && (op - m_pos) >= 4)
|
||||
{
|
||||
#endif
|
||||
UA_COPY4(op, m_pos);
|
||||
op += 4; m_pos += 4; t -= 4 - (3 - 1);
|
||||
do {
|
||||
op += 4;
|
||||
m_pos += 4;
|
||||
t -= 4 - (3 - 1);
|
||||
|
||||
do
|
||||
{
|
||||
UA_COPY4(op, m_pos);
|
||||
op += 4; m_pos += 4; t -= 4;
|
||||
} while (t >= 4);
|
||||
if (t > 0) do *op++ = *m_pos++; while (--t > 0);
|
||||
op += 4;
|
||||
m_pos += 4;
|
||||
t -= 4;
|
||||
}
|
||||
while (t >= 4);
|
||||
|
||||
if (t > 0) do* op++ = *m_pos++;
|
||||
|
||||
while (--t > 0);
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
copy_match:
|
||||
*op++ = *m_pos++; *op++ = *m_pos++;
|
||||
do *op++ = *m_pos++; while (--t > 0);
|
||||
*op++ = *m_pos++;
|
||||
*op++ = *m_pos++;
|
||||
|
||||
do* op++ = *m_pos++;
|
||||
|
||||
while (--t > 0);
|
||||
}
|
||||
|
||||
#endif /* COPY_DICT */
|
||||
|
@ -430,17 +531,27 @@ match_done:
|
|||
#else
|
||||
t = ip[-2] & 3;
|
||||
#endif
|
||||
|
||||
if (t == 0)
|
||||
break;
|
||||
|
||||
/* copy literals */
|
||||
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
|
||||
do *op++ = *ip++; while (--t > 0);
|
||||
|
||||
do* op++ = *ip++;
|
||||
|
||||
while (--t > 0);
|
||||
|
||||
#else
|
||||
*op++ = *ip++;
|
||||
|
||||
if (t > 1) { *op++ = *ip++; if (t > 2) { *op++ = *ip++; } }
|
||||
|
||||
#endif
|
||||
t = *ip++;
|
||||
}
|
||||
|
|
|
@ -41,6 +41,7 @@ static void copy2(lzo_bytep ip, const lzo_bytep m_pos, lzo_uint off)
|
|||
{
|
||||
assert(off > 0);
|
||||
ip[0] = m_pos[0];
|
||||
|
||||
if (off == 1)
|
||||
ip[1] = m_pos[0];
|
||||
else
|
||||
|
@ -52,6 +53,7 @@ static void copy3(lzo_bytep ip, const lzo_bytep m_pos, lzo_uint off)
|
|||
{
|
||||
assert(off > 0);
|
||||
ip[0] = m_pos[0];
|
||||
|
||||
if (off == 1)
|
||||
{
|
||||
ip[2] = ip[1] = m_pos[0];
|
||||
|
@ -98,41 +100,57 @@ DO_OPTIMIZE ( lzo_bytep in , lzo_uint in_len,
|
|||
ip = in;
|
||||
|
||||
assert(in_len >= 3);
|
||||
|
||||
if (*ip > 17)
|
||||
{
|
||||
t = *ip++ - 17;
|
||||
|
||||
if (t < 4)
|
||||
goto match_next;
|
||||
|
||||
goto first_literal_run;
|
||||
}
|
||||
|
||||
assert(*ip < 16 || (*ip == 17 && in_len == 3));
|
||||
|
||||
while (TEST_IP_AND_TEST_OP)
|
||||
{
|
||||
t = *ip++;
|
||||
|
||||
if (t >= 16)
|
||||
goto match;
|
||||
|
||||
/* a literal run */
|
||||
litp = ip - 1;
|
||||
|
||||
if (t == 0)
|
||||
{
|
||||
t = 15;
|
||||
|
||||
while (*ip == 0)
|
||||
t += 255, ip++;
|
||||
|
||||
t += *ip++;
|
||||
}
|
||||
|
||||
lit = t + 3;
|
||||
/* copy literals */
|
||||
copy_literal_run:
|
||||
*op++ = *ip++; *op++ = *ip++; *op++ = *ip++;
|
||||
*op++ = *ip++;
|
||||
*op++ = *ip++;
|
||||
*op++ = *ip++;
|
||||
first_literal_run:
|
||||
do *op++ = *ip++; while (--t > 0);
|
||||
|
||||
do* op++ = *ip++;
|
||||
|
||||
while (--t > 0);
|
||||
|
||||
|
||||
t = *ip++;
|
||||
|
||||
if (t >= 16)
|
||||
goto match;
|
||||
|
||||
#if defined(LZO1X)
|
||||
m_pos = op - 1 - 0x800;
|
||||
#elif defined(LZO1Y)
|
||||
|
@ -140,13 +158,16 @@ first_literal_run:
|
|||
#endif
|
||||
m_pos -= t >> 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;
|
||||
goto match_done;
|
||||
|
||||
|
||||
/* handle matches */
|
||||
do {
|
||||
do
|
||||
{
|
||||
if (t < 16) /* a M1 match */
|
||||
{
|
||||
m_pos = op - 1;
|
||||
|
@ -161,6 +182,7 @@ first_literal_run:
|
|||
assert(litp == ip - 2 - lit - 2);
|
||||
assert((lzo_uint)(*litp & 3) == lit);
|
||||
nl = ip[-2] & 3;
|
||||
|
||||
/* test if a match follows */
|
||||
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));
|
||||
/* move literals 1 byte ahead */
|
||||
litp += 2;
|
||||
|
||||
if (lit > 0)
|
||||
lzo_memmove(litp + 1, litp, lit);
|
||||
|
||||
/* 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);
|
||||
|
||||
o_m1_b++;
|
||||
*op++ = *m_pos++; *op++ = *m_pos++;
|
||||
*op++ = *m_pos++;
|
||||
*op++ = *m_pos++;
|
||||
goto copy_literal_run;
|
||||
}
|
||||
|
||||
copy_m1:
|
||||
*op++ = *m_pos++; *op++ = *m_pos++;
|
||||
*op++ = *m_pos++;
|
||||
*op++ = *m_pos++;
|
||||
}
|
||||
else
|
||||
{
|
||||
match:
|
||||
|
||||
if (t >= 64) /* a M2 match */
|
||||
{
|
||||
m_pos = op - 1;
|
||||
|
@ -211,10 +240,12 @@ match:
|
|||
m_pos -= *ip++ << 2;
|
||||
t = (t >> 4) - 3;
|
||||
#endif
|
||||
|
||||
if (litp == NULL)
|
||||
goto copy_m;
|
||||
|
||||
nl = ip[-2] & 3;
|
||||
|
||||
/* test if in beetween two long literal runs */
|
||||
if (t == 1 && lit > 3 && nl == 0 &&
|
||||
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 */
|
||||
copy3(ip - 1 - 2, m_pos, pd(op, m_pos));
|
||||
/* 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);
|
||||
o_m2++;
|
||||
*op++ = *m_pos++; *op++ = *m_pos++; *op++ = *m_pos++;
|
||||
*op++ = *m_pos++;
|
||||
*op++ = *m_pos++;
|
||||
*op++ = *m_pos++;
|
||||
goto copy_literal_run;
|
||||
}
|
||||
}
|
||||
|
@ -236,13 +270,17 @@ match:
|
|||
if (t >= 32) /* a M3 match */
|
||||
{
|
||||
t &= 31;
|
||||
|
||||
if (t == 0)
|
||||
{
|
||||
t = 31;
|
||||
|
||||
while (*ip == 0)
|
||||
t += 255, ip++;
|
||||
|
||||
t += *ip++;
|
||||
}
|
||||
|
||||
m_pos = op - 1;
|
||||
m_pos -= *ip++ >> 2;
|
||||
m_pos -= *ip++ << 6;
|
||||
|
@ -252,23 +290,31 @@ match:
|
|||
m_pos = op;
|
||||
m_pos -= (t & 8) << 11;
|
||||
t &= 7;
|
||||
|
||||
if (t == 0)
|
||||
{
|
||||
t = 7;
|
||||
|
||||
while (*ip == 0)
|
||||
t += 255, ip++;
|
||||
|
||||
t += *ip++;
|
||||
}
|
||||
|
||||
m_pos -= *ip++ >> 2;
|
||||
m_pos -= *ip++ << 6;
|
||||
|
||||
if (m_pos == op)
|
||||
goto eof_found;
|
||||
|
||||
m_pos -= 0x4000;
|
||||
}
|
||||
|
||||
if (litp == NULL)
|
||||
goto copy_m;
|
||||
|
||||
nl = ip[-2] & 3;
|
||||
|
||||
/* test if in beetween two matches */
|
||||
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));
|
||||
/* move literals 1 byte ahead */
|
||||
litp += 2;
|
||||
|
||||
if (lit > 0)
|
||||
lzo_memmove(litp + 1, litp, lit);
|
||||
|
||||
/* 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);
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
copy_m:
|
||||
*op++ = *m_pos++; *op++ = *m_pos++;
|
||||
do *op++ = *m_pos++; while (--t > 0);
|
||||
*op++ = *m_pos++;
|
||||
*op++ = *m_pos++;
|
||||
|
||||
do* op++ = *m_pos++;
|
||||
|
||||
while (--t > 0);
|
||||
}
|
||||
|
||||
match_done:
|
||||
|
||||
if (next_lit == NO_LIT)
|
||||
{
|
||||
t = ip[-2] & 3;
|
||||
|
@ -320,15 +377,23 @@ match_done:
|
|||
}
|
||||
else
|
||||
t = next_lit;
|
||||
|
||||
assert(t <= 3);
|
||||
next_lit = NO_LIT;
|
||||
|
||||
if (t == 0)
|
||||
break;
|
||||
|
||||
/* copy literals */
|
||||
match_next:
|
||||
do *op++ = *ip++; while (--t > 0);
|
||||
|
||||
do* op++ = *ip++;
|
||||
|
||||
while (--t > 0);
|
||||
|
||||
t = *ip++;
|
||||
} while (TEST_IP_AND_TEST_OP);
|
||||
}
|
||||
while (TEST_IP_AND_TEST_OP);
|
||||
}
|
||||
|
||||
/* no EOF code was found */
|
||||
|
@ -341,8 +406,11 @@ eof_found:
|
|||
printf("optimize: %5lu %5lu %5lu %5lu %5lu\n",
|
||||
o_m1_a, o_m1_b, o_m2, o_m3_a, o_m3_b);
|
||||
#endif
|
||||
LZO_UNUSED(o_m1_a); LZO_UNUSED(o_m1_b); LZO_UNUSED(o_m2);
|
||||
LZO_UNUSED(o_m3_a); LZO_UNUSED(o_m3_b);
|
||||
LZO_UNUSED(o_m1_a);
|
||||
LZO_UNUSED(o_m1_b);
|
||||
LZO_UNUSED(o_m2);
|
||||
LZO_UNUSED(o_m3_a);
|
||||
LZO_UNUSED(o_m3_b);
|
||||
*out_len = pd(op, out);
|
||||
return (ip == ip_end ? LZO_E_OK :
|
||||
(ip < ip_end ? LZO_E_INPUT_NOT_CONSUMED : LZO_E_INPUT_OVERRUN));
|
||||
|
|
|
@ -108,14 +108,18 @@ lzo2a_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
|
|||
op = out;
|
||||
|
||||
r = init_match(c, swd, NULL, 0, 0);
|
||||
|
||||
if (r != 0)
|
||||
return r;
|
||||
|
||||
if (max_chain > 0)
|
||||
swd->max_chain = max_chain;
|
||||
|
||||
r = find_match(c, swd, 0, 0);
|
||||
|
||||
if (r != 0)
|
||||
return r;
|
||||
|
||||
while (c->look > 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;
|
||||
|
||||
#if (SWD_N >= 8192)
|
||||
|
||||
if (m_off >= 8192)
|
||||
{
|
||||
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);
|
||||
|
||||
r = find_match(c, swd, 1, 0);
|
||||
assert(r == 0); LZO_UNUSED(r);
|
||||
assert(r == 0);
|
||||
LZO_UNUSED(r);
|
||||
assert(c->look > 0);
|
||||
|
||||
#if (SWD_N >= 8192)
|
||||
|
||||
if (m_off < 8192 && c->m_off >= 8192)
|
||||
lazy_match_min_gain += extra1;
|
||||
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))
|
||||
lazy_match_min_gain += extra2;
|
||||
}
|
||||
|
||||
if (c->m_len >= M1_MIN_LEN &&
|
||||
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
|
||||
ahead = 1;
|
||||
|
||||
assert(m_len > 0);
|
||||
}
|
||||
|
||||
|
@ -217,7 +226,8 @@ lzo2a_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
|
|||
putbyte(swd->b_char);
|
||||
c->lit_bytes++;
|
||||
r = find_match(c, swd, 1, 0);
|
||||
assert(r == 0); LZO_UNUSED(r);
|
||||
assert(r == 0);
|
||||
LZO_UNUSED(r);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -234,6 +244,7 @@ lzo2a_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
|
|||
putbyte(m_off - 1);
|
||||
c->m1++;
|
||||
}
|
||||
|
||||
#if (SWD_N >= 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);
|
||||
putbit(1);
|
||||
len -= M3_MIN_LEN - 1;
|
||||
|
||||
while (len > 255)
|
||||
{
|
||||
len -= 255;
|
||||
putbyte(0);
|
||||
}
|
||||
|
||||
putbyte(len);
|
||||
c->m4++;
|
||||
}
|
||||
|
||||
#endif
|
||||
else
|
||||
{
|
||||
|
@ -260,6 +274,7 @@ lzo2a_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
|
|||
|
||||
putbit(1);
|
||||
putbit(1);
|
||||
|
||||
if (m_len <= 9)
|
||||
{
|
||||
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);
|
||||
#endif
|
||||
len -= 10 - 1;
|
||||
|
||||
while (len > 255)
|
||||
{
|
||||
len -= 255;
|
||||
putbyte(0);
|
||||
}
|
||||
|
||||
putbyte(len);
|
||||
c->m3++;
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
|
@ -301,6 +320,7 @@ lzo2a_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
|
|||
|
||||
/* flush remaining bits */
|
||||
assert(k < CHAR_BIT);
|
||||
|
||||
if (k > 0)
|
||||
{
|
||||
assert(b == MASKBITS(k));
|
||||
|
|
|
@ -62,17 +62,21 @@ DO_DECOMPRESS ( const lzo_bytep in , lzo_uint in_len,
|
|||
while (TEST_IP_AND_TEST_OP)
|
||||
{
|
||||
NEEDBITS(1);
|
||||
|
||||
if (MASKBITS(1) == 0)
|
||||
{
|
||||
DUMPBITS(1);
|
||||
/* a literal */
|
||||
NEED_IP(1); NEED_OP(1);
|
||||
NEED_IP(1);
|
||||
NEED_OP(1);
|
||||
*op++ = *ip++;
|
||||
continue;
|
||||
}
|
||||
|
||||
DUMPBITS(1);
|
||||
|
||||
NEEDBITS(1);
|
||||
|
||||
if (MASKBITS(1) == 0)
|
||||
{
|
||||
DUMPBITS(1);
|
||||
|
@ -80,13 +84,16 @@ DO_DECOMPRESS ( const lzo_bytep in , lzo_uint in_len,
|
|||
NEEDBITS(2);
|
||||
t = M1_MIN_LEN + (lzo_uint) MASKBITS(2);
|
||||
DUMPBITS(2);
|
||||
NEED_IP(1); NEED_OP(t);
|
||||
NEED_IP(1);
|
||||
NEED_OP(t);
|
||||
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);
|
||||
MEMCPY_DS(op, m_pos, t);
|
||||
continue;
|
||||
}
|
||||
|
||||
DUMPBITS(1);
|
||||
|
||||
NEED_IP(2);
|
||||
|
@ -94,12 +101,14 @@ DO_DECOMPRESS ( const lzo_bytep in , lzo_uint in_len,
|
|||
m_pos = op;
|
||||
m_pos -= (t & 31) | (((lzo_uint) * ip++) << 5);
|
||||
t >>= 5;
|
||||
|
||||
if (t == 0)
|
||||
{
|
||||
#if (SWD_N >= 8192)
|
||||
NEEDBITS(1);
|
||||
t = MASKBITS(1);
|
||||
DUMPBITS(1);
|
||||
|
||||
if (t == 0)
|
||||
t = 10 - 1;
|
||||
else
|
||||
|
@ -108,10 +117,12 @@ DO_DECOMPRESS ( const lzo_bytep in , lzo_uint in_len,
|
|||
m_pos -= 8192; /* t << 13 */
|
||||
t = M3_MIN_LEN - 1;
|
||||
}
|
||||
|
||||
#else
|
||||
t = 10 - 1;
|
||||
#endif
|
||||
NEED_IP(1);
|
||||
|
||||
while (*ip == 0)
|
||||
{
|
||||
t += 255;
|
||||
|
@ -119,17 +130,22 @@ DO_DECOMPRESS ( const lzo_bytep in , lzo_uint in_len,
|
|||
TEST_OV(t);
|
||||
NEED_IP(1);
|
||||
}
|
||||
|
||||
t += *ip++;
|
||||
}
|
||||
else
|
||||
{
|
||||
#if defined(LZO_EOF_CODE)
|
||||
|
||||
if (m_pos == op)
|
||||
goto eof_found;
|
||||
|
||||
#endif
|
||||
t += 2;
|
||||
}
|
||||
assert(m_pos >= out); assert(m_pos < op);
|
||||
|
||||
assert(m_pos >= out);
|
||||
assert(m_pos < op);
|
||||
TEST_LB(m_pos);
|
||||
NEED_OP(t);
|
||||
MEMCPY_DS(op, m_pos, t);
|
||||
|
|
|
@ -35,7 +35,8 @@
|
|||
// 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,
|
||||
0x706af48fL, 0xe963a535L, 0x9e6495a3L, 0x0edb8832L, 0x79dcb8a4L,
|
||||
0xe0d5e91eL, 0x97d2d988L, 0x09b64c2bL, 0x7eb17cbdL, 0xe7b82d07L,
|
||||
|
@ -126,18 +127,22 @@ lzo_crc32(lzo_uint32_t c, const lzo_bytep buf, lzo_uint len)
|
|||
return 0;
|
||||
|
||||
crc = (c & LZO_UINT32_C(0xffffffff)) ^ LZO_UINT32_C(0xffffffff);
|
||||
|
||||
if (len >= 16) do
|
||||
{
|
||||
LZO_DO16(buf, 0);
|
||||
buf += 16;
|
||||
len -= 16;
|
||||
} while (len >= 16);
|
||||
}
|
||||
while (len >= 16);
|
||||
|
||||
if (len != 0) do
|
||||
{
|
||||
LZO_DO1(buf, 0);
|
||||
buf += 1;
|
||||
len -= 1;
|
||||
} while (len > 0);
|
||||
}
|
||||
while (len > 0);
|
||||
|
||||
return crc ^ LZO_UINT32_C(0xffffffff);
|
||||
#undef table
|
||||
|
|
|
@ -40,7 +40,10 @@ BOOL FAR PASCAL LibMain ( HANDLE hInstance, WORD wDataSegment,
|
|||
int __far __pascal LibMain(int a, short b, short c, long d)
|
||||
#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;
|
||||
}
|
||||
|
||||
|
|
|
@ -64,7 +64,9 @@
|
|||
__lzo_static_forceinline unsigned lzo_bitops_ctlz32_func(lzo_uint32_t v)
|
||||
{
|
||||
#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)
|
||||
#elif (LZO_BITOPS_USE_ASM_BITSCAN) && (LZO_ARCH_AMD64 || LZO_ARCH_I386) && (LZO_ASM_SYNTAX_GNUC)
|
||||
lzo_uint32_t r;
|
||||
|
@ -72,13 +74,18 @@ __lzo_static_forceinline unsigned lzo_bitops_ctlz32_func(lzo_uint32_t v)
|
|||
return (unsigned) r ^ 31;
|
||||
#define lzo_bitops_ctlz32(v) lzo_bitops_ctlz32_func(v)
|
||||
#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))
|
||||
#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)
|
||||
#else
|
||||
LZO_UNUSED(v); return 0;
|
||||
LZO_UNUSED(v);
|
||||
return 0;
|
||||
#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)
|
||||
{
|
||||
#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)
|
||||
#elif (LZO_BITOPS_USE_ASM_BITSCAN) && (LZO_ARCH_AMD64) && (LZO_ASM_SYNTAX_GNUC)
|
||||
lzo_uint64_t r;
|
||||
|
@ -94,13 +103,18 @@ __lzo_static_forceinline unsigned lzo_bitops_ctlz64_func(lzo_uint64_t v)
|
|||
return (unsigned) r ^ 63;
|
||||
#define lzo_bitops_ctlz64(v) lzo_bitops_ctlz64_func(v)
|
||||
#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))
|
||||
#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))
|
||||
#else
|
||||
LZO_UNUSED(v); return 0;
|
||||
LZO_UNUSED(v);
|
||||
return 0;
|
||||
#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)
|
||||
{
|
||||
#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)
|
||||
#elif (LZO_BITOPS_USE_ASM_BITSCAN) && (LZO_ARCH_AMD64 || LZO_ARCH_I386) && (LZO_ASM_SYNTAX_GNUC)
|
||||
lzo_uint32_t r;
|
||||
|
@ -116,10 +132,13 @@ __lzo_static_forceinline unsigned lzo_bitops_cttz32_func(lzo_uint32_t v)
|
|||
return (unsigned) r;
|
||||
#define lzo_bitops_cttz32(v) lzo_bitops_cttz32_func(v)
|
||||
#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))
|
||||
#else
|
||||
LZO_UNUSED(v); return 0;
|
||||
LZO_UNUSED(v);
|
||||
return 0;
|
||||
#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)
|
||||
{
|
||||
#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)
|
||||
#elif (LZO_BITOPS_USE_ASM_BITSCAN) && (LZO_ARCH_AMD64) && (LZO_ASM_SYNTAX_GNUC)
|
||||
lzo_uint64_t r;
|
||||
|
@ -135,13 +156,18 @@ __lzo_static_forceinline unsigned lzo_bitops_cttz64_func(lzo_uint64_t v)
|
|||
return (unsigned) r;
|
||||
#define lzo_bitops_cttz64(v) lzo_bitops_cttz64_func(v)
|
||||
#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))
|
||||
#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))
|
||||
#else
|
||||
LZO_UNUSED(v); return 0;
|
||||
LZO_UNUSED(v);
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -65,7 +65,8 @@
|
|||
//
|
||||
************************************************************************/
|
||||
|
||||
union lzo_config_check_union {
|
||||
union lzo_config_check_union
|
||||
{
|
||||
lzo_uint a[2];
|
||||
unsigned char b[2 * LZO_MAX(8, sizeof(lzo_uint))];
|
||||
#if defined(lzo_uint64_t)
|
||||
|
@ -102,17 +103,20 @@ _lzo_config_check(void)
|
|||
r &= ((* (lzo_bytep) p) == 0);
|
||||
#if !(LZO_CFG_NO_CONFIG_CHECK)
|
||||
#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);
|
||||
r &= ((* (lzo_uintp) p) == 128);
|
||||
#endif
|
||||
#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);
|
||||
r &= ((* (lzo_uintp) p) == 128);
|
||||
#endif
|
||||
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);
|
||||
r &= UA_GET_NE16(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);
|
||||
#endif
|
||||
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);
|
||||
r &= UA_GET_NE32(p) == 0;
|
||||
r &= UA_GET_LE32(p) == 0;
|
||||
u.b[1] = 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);
|
||||
#if (LZO_ABI_BIG_ENDIAN)
|
||||
r &= UA_GET_NE32(p) == LZO_UINT32_C(0x80818283);
|
||||
|
@ -143,7 +150,8 @@ _lzo_config_check(void)
|
|||
#endif
|
||||
#if defined(UA_GET_NE64)
|
||||
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);
|
||||
u.c[0] = u.c[1] = 0;
|
||||
r &= UA_GET_NE64(p) == 0;
|
||||
|
@ -154,32 +162,52 @@ _lzo_config_check(void)
|
|||
#endif
|
||||
#endif
|
||||
#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_func(v) == 31 - i;
|
||||
}}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#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_func(v) == 63 - i;
|
||||
}}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#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_func(v) == i;
|
||||
}}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#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_func(v) == i;
|
||||
}}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
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*)) &&
|
||||
(s8 == -1 || s8 == (int) sizeof(lzo_voidp)) &&
|
||||
(s9 == -1 || s9 == (int) sizeof(lzo_callback_t));
|
||||
|
||||
if (!r)
|
||||
return LZO_E_ERROR;
|
||||
|
||||
r = _lzo_config_check();
|
||||
|
||||
if (r != LZO_E_OK)
|
||||
return r;
|
||||
|
||||
|
|
|
@ -128,6 +128,7 @@ init_match ( LZO_COMPRESS_T *c, lzo_swd_p s,
|
|||
c->lazy = 0;
|
||||
|
||||
r = swd_init(s, dict, dict_len);
|
||||
|
||||
if (r != LZO_E_OK)
|
||||
{
|
||||
swd_exit(s);
|
||||
|
@ -164,8 +165,10 @@ find_match ( LZO_COMPRESS_T *c, lzo_swd_p s,
|
|||
s->m_len = SWD_THRESHOLD;
|
||||
s->m_off = 0;
|
||||
#ifdef SWD_BEST_OFF
|
||||
|
||||
if (s->use_best_off)
|
||||
lzo_memset(s->best_pos, 0, sizeof(s->best_pos));
|
||||
|
||||
#endif
|
||||
swd_findbest(s);
|
||||
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->bp = c->ip - c->look;
|
||||
|
||||
#if 0
|
||||
|
||||
/* brute force match search */
|
||||
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)
|
||||
in = ip - s->swd_n;
|
||||
|
||||
for (;;)
|
||||
{
|
||||
while (*in != *ip)
|
||||
in++;
|
||||
|
||||
if (in == ip)
|
||||
break;
|
||||
|
||||
if (in != m)
|
||||
if (lzo_memcmp(in, ip, c->m_len + 1) == 0)
|
||||
printf("%p %p %p %5d\n", in, ip, m, c->m_len);
|
||||
|
||||
in++;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
if (c->cb && c->cb->nprogress && c->textsize > c->printcount)
|
||||
|
|
|
@ -61,14 +61,19 @@ __lzo_align_gap(const lzo_voidp ptr, lzo_uint size)
|
|||
#error "__LZO_UINTPTR_T_IS_POINTER is unsupported"
|
||||
#else
|
||||
lzo_uintptr_t p, n;
|
||||
|
||||
if (size < 2) return 0;
|
||||
|
||||
p = __lzo_ptr_linear(ptr);
|
||||
#if 0
|
||||
n = (((p + size - 1) / size) * size) - p;
|
||||
#else
|
||||
|
||||
if ((size & (size - 1)) != 0)
|
||||
return 0;
|
||||
n = size; n = ((p + n - 1) & ~(n - 1)) - p;
|
||||
|
||||
n = size;
|
||||
n = ((p + n - 1) & ~(n - 1)) - p;
|
||||
#endif
|
||||
#endif
|
||||
assert((long)n >= 0);
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -189,6 +189,7 @@ void swd_initdict(lzo_swd_p s, const lzo_bytep dict, lzo_uint dict_len)
|
|||
|
||||
if (!dict || dict_len == 0)
|
||||
return;
|
||||
|
||||
if (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);
|
||||
|
||||
#ifdef HEAD2
|
||||
IF_HEAD2(s) {
|
||||
IF_HEAD2(s)
|
||||
{
|
||||
key = HEAD2(s_b(s), 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->llen3 != NULL;
|
||||
#ifdef HEAD2
|
||||
IF_HEAD2(s) {
|
||||
IF_HEAD2(s)
|
||||
{
|
||||
s->head2 = (swd_uintp) malloc(sizeof(swd_uint) * 65536L);
|
||||
r &= s->head2 != NULL;
|
||||
}
|
||||
#endif
|
||||
if (r != 1) {
|
||||
|
||||
if (r != 1)
|
||||
{
|
||||
swd_exit(s);
|
||||
return LZO_E_OUT_OF_MEMORY;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
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)
|
||||
{
|
||||
unsigned i;
|
||||
|
||||
for (i = 0; i < SWD_BEST_OFF; i++)
|
||||
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;
|
||||
#if 0
|
||||
|
||||
if (2 * s->swd_f >= s->swd_n || s->b_size + s->swd_f >= SWD_UINT_MAX)
|
||||
return LZO_E_ERROR;
|
||||
|
||||
#else
|
||||
LZO_COMPILE_TIME_ASSERT(!(0ul + 2 * SWD_F >= SWD_N))
|
||||
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);
|
||||
#ifdef HEAD2
|
||||
IF_HEAD2(s) {
|
||||
IF_HEAD2(s)
|
||||
{
|
||||
#if 1
|
||||
lzo_memset(s_head2(s), 0xff, (lzo_uint)sizeof(s_head2(s)[0]) * 65536L);
|
||||
assert(s_head2(s)[0] == NIL2);
|
||||
#else
|
||||
lzo_xint i;
|
||||
|
||||
for (i = 0; i < 65536L; i++)
|
||||
s_head2(s)[i] = NIL2;
|
||||
|
||||
#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);
|
||||
#if 1
|
||||
s->look = (lzo_uint)(s->c->in_end - s->c->ip);
|
||||
|
||||
if (s->look > 0)
|
||||
{
|
||||
if (s->look > s->swd_f)
|
||||
s->look = s->swd_f;
|
||||
|
||||
lzo_memcpy(&s_b(s)[s->ip], s->c->ip, s->look);
|
||||
s->c->ip += s->look;
|
||||
s->ip += s->look;
|
||||
}
|
||||
|
||||
#else
|
||||
s->look = 0;
|
||||
|
||||
while (s->look < s->swd_f)
|
||||
{
|
||||
int c;
|
||||
|
||||
if ((c = getbyte(*(s->c))) < 0)
|
||||
break;
|
||||
|
||||
s_b(s)[s->ip] = LZO_BYTE(c);
|
||||
s->ip++;
|
||||
s->look++;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
if (s->ip == s->b_size)
|
||||
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);
|
||||
|
||||
s->rp = s->first_rp;
|
||||
|
||||
if (s->rp >= s->node_count)
|
||||
s->rp -= s->node_count;
|
||||
else
|
||||
s->rp += s->b_size - s->node_count;
|
||||
|
||||
#if 1 || defined(__LZO_CHECKER)
|
||||
|
||||
/* 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. */
|
||||
if (s->look < 3) {
|
||||
if (s->look < 3)
|
||||
{
|
||||
lzo_bytep p = &s_b(s)[s->bp + s->look];
|
||||
p[0] = p[1] = p[2] = 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
return LZO_E_OK;
|
||||
|
@ -370,13 +394,19 @@ void swd_exit(lzo_swd_p s)
|
|||
#if defined(__LZO_CHECKER)
|
||||
/* free in reverse order of allocations */
|
||||
#ifdef HEAD2
|
||||
free(s->head2); s->head2 = NULL;
|
||||
free(s->head2);
|
||||
s->head2 = NULL;
|
||||
#endif
|
||||
free(s->llen3); s->llen3 = NULL;
|
||||
free(s->best3); s->best3 = NULL;
|
||||
free(s->succ3); s->succ3 = NULL;
|
||||
free(s->head3); s->head3 = NULL;
|
||||
free(s->b); s->b = NULL;
|
||||
free(s->llen3);
|
||||
s->llen3 = NULL;
|
||||
free(s->best3);
|
||||
s->best3 = NULL;
|
||||
free(s->succ3);
|
||||
s->succ3 = NULL;
|
||||
free(s->head3);
|
||||
s->head3 = NULL;
|
||||
free(s->b);
|
||||
s->b = NULL;
|
||||
#else
|
||||
LZO_UNUSED(s);
|
||||
#endif
|
||||
|
@ -400,23 +430,30 @@ void swd_getbyte(lzo_swd_p s)
|
|||
{
|
||||
if (s->look > 0)
|
||||
--s->look;
|
||||
|
||||
#if 1 || defined(__LZO_CHECKER)
|
||||
/* initialize memory - value doesn't matter */
|
||||
s_b(s)[s->ip] = 0;
|
||||
|
||||
if (s->ip < s->swd_f)
|
||||
s->b_wrap[s->ip] = 0;
|
||||
|
||||
#endif
|
||||
}
|
||||
else
|
||||
{
|
||||
s_b(s)[s->ip] = LZO_BYTE(c);
|
||||
|
||||
if (s->ip < s->swd_f)
|
||||
s->b_wrap[s->ip] = LZO_BYTE(c);
|
||||
}
|
||||
|
||||
if (++s->ip == s->b_size)
|
||||
s->ip = 0;
|
||||
|
||||
if (++s->bp == s->b_size)
|
||||
s->bp = 0;
|
||||
|
||||
if (++s->rp == s->b_size)
|
||||
s->rp = 0;
|
||||
}
|
||||
|
@ -434,6 +471,7 @@ void swd_remove_node(lzo_swd_p s, lzo_uint node)
|
|||
lzo_uint key;
|
||||
|
||||
#ifdef LZO_DEBUG
|
||||
|
||||
if (s->first_rp != LZO_UINT_MAX)
|
||||
{
|
||||
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)s->first_rp, (long)(s->ip - node),
|
||||
(long)(s->ip - s->bp));
|
||||
|
||||
assert(node == s->first_rp);
|
||||
s->first_rp = LZO_UINT_MAX;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
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];
|
||||
|
||||
#ifdef HEAD2
|
||||
IF_HEAD2(s) {
|
||||
IF_HEAD2(s)
|
||||
{
|
||||
key = HEAD2(s_b(s), node);
|
||||
assert(s_head2(s)[key] != NIL2);
|
||||
|
||||
if ((lzo_uint) s_head2(s)[key] == node)
|
||||
s_head2(s)[key] = NIL2;
|
||||
}
|
||||
|
@ -489,14 +531,16 @@ void swd_accept(lzo_swd_p s, lzo_uint n)
|
|||
|
||||
#ifdef HEAD2
|
||||
/* add bp into HEAD2 */
|
||||
IF_HEAD2(s) {
|
||||
IF_HEAD2(s)
|
||||
{
|
||||
key = HEAD2(s_b(s), s->bp);
|
||||
s_head2(s)[key] = SWD_UINT(s->bp);
|
||||
}
|
||||
#endif
|
||||
|
||||
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);
|
||||
|
||||
scan_end1 = bp[m_len - 1];
|
||||
|
||||
for (; cnt-- > 0; node = s_succ3(s)[node])
|
||||
{
|
||||
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);
|
||||
|
||||
#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))
|
||||
p1 += 4, p2 += 4;
|
||||
|
||||
while (p1 < px && *p1 == *p2)
|
||||
p1 += 1, p2 += 1;
|
||||
|
||||
#else
|
||||
p1 += 2; p2 += 2;
|
||||
do {} while (++p1 < px && *p1 == *++p2);
|
||||
p1 += 2;
|
||||
p2 += 2;
|
||||
|
||||
do {}
|
||||
while (++p1 < px && *p1 == *++p2);
|
||||
|
||||
#endif
|
||||
i = pd(p1, bp);
|
||||
|
||||
#ifdef LZO_DEBUG
|
||||
|
||||
if (lzo_memcmp(bp, &b[node], i) != 0)
|
||||
printf("%5ld %5ld %5ld %02x/%02x %02x/%02x\n",
|
||||
(long)s->bp, (long) node, (long) i,
|
||||
bp[0], bp[1], b[node], b[node + 1]);
|
||||
|
||||
#endif
|
||||
assert(lzo_memcmp(bp, &b[node], i) == 0);
|
||||
|
||||
#if defined(SWD_BEST_OFF)
|
||||
|
||||
if (i < SWD_BEST_OFF)
|
||||
{
|
||||
if (s->best_pos[i] == 0)
|
||||
s->best_pos[i] = node + 1;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
if (i > m_len)
|
||||
{
|
||||
s->m_len = m_len = i;
|
||||
s->m_pos = node;
|
||||
|
||||
if (m_len == s->look)
|
||||
return;
|
||||
|
||||
if (m_len >= s->nice_length)
|
||||
return;
|
||||
|
||||
if (m_len > (lzo_uint) s_best3(s)[node])
|
||||
return;
|
||||
|
||||
scan_end1 = bp[m_len - 1];
|
||||
}
|
||||
}
|
||||
|
@ -597,17 +659,23 @@ lzo_bool swd_search2(lzo_swd_p s)
|
|||
assert(s->m_len > 0);
|
||||
|
||||
key = s_head2(s)[ HEAD2(s_b(s), s->bp) ];
|
||||
|
||||
if (key == NIL2)
|
||||
return 0;
|
||||
|
||||
#ifdef LZO_DEBUG
|
||||
|
||||
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,
|
||||
s_b(s)[s->bp], s_b(s)[s->bp + 1], s_b(s)[key], s_b(s)[key + 1]);
|
||||
|
||||
#endif
|
||||
assert(lzo_memcmp(&s_b(s)[s->bp], &s_b(s)[key], 2) == 0);
|
||||
#if defined(SWD_BEST_OFF)
|
||||
|
||||
if (s->best_pos[2] == 0)
|
||||
s->best_pos[2] = key + 1;
|
||||
|
||||
#endif
|
||||
|
||||
if (s->m_len < 2)
|
||||
|
@ -615,6 +683,7 @@ lzo_bool swd_search2(lzo_swd_p s)
|
|||
s->m_len = 2;
|
||||
s->m_pos = key;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -639,42 +708,55 @@ void swd_findbest(lzo_swd_p s)
|
|||
node = s_succ3(s)[s->bp] = s_get_head3(s, key);
|
||||
cnt = s_llen3(s)[key]++;
|
||||
assert(s_llen3(s)[key] <= s->swd_n + s->swd_f);
|
||||
|
||||
if (cnt > s->max_chain && s->max_chain > 0)
|
||||
cnt = s->max_chain;
|
||||
|
||||
s_head3(s)[key] = SWD_UINT(s->bp);
|
||||
|
||||
s->b_char = s_b(s)[s->bp];
|
||||
len = s->m_len;
|
||||
|
||||
if (s->m_len >= s->look)
|
||||
{
|
||||
if (s->look == 0)
|
||||
s->b_char = -1;
|
||||
|
||||
s->m_off = 0;
|
||||
s_best3(s)[s->bp] = SWD_UINT(s->swd_f + 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
#if defined(HEAD2)
|
||||
|
||||
if (swd_search2(s) && s->look >= 3)
|
||||
swd_search(s, node, cnt);
|
||||
|
||||
#else
|
||||
|
||||
if (s->look >= 3)
|
||||
swd_search(s, node, cnt);
|
||||
|
||||
#endif
|
||||
|
||||
if (s->m_len > len)
|
||||
s->m_off = swd_pos2off(s, s->m_pos);
|
||||
|
||||
s_best3(s)[s->bp] = SWD_UINT(s->m_len);
|
||||
|
||||
#if defined(SWD_BEST_OFF)
|
||||
|
||||
if (s->use_best_off)
|
||||
{
|
||||
unsigned i;
|
||||
|
||||
for (i = 2; i < SWD_BEST_OFF; i++)
|
||||
if (s->best_pos[i] > 0)
|
||||
s->best_off[i] = swd_pos2off(s, s->best_pos[i] - 1);
|
||||
else
|
||||
s->best_off[i] = 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
|
@ -682,7 +764,8 @@ void swd_findbest(lzo_swd_p s)
|
|||
|
||||
#ifdef HEAD2
|
||||
/* add bp into HEAD2 */
|
||||
IF_HEAD2(s) {
|
||||
IF_HEAD2(s)
|
||||
{
|
||||
key = HEAD2(s_b(s), s->bp);
|
||||
s_head2(s)[key] = SWD_UINT(s->bp);
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
len -= k;
|
||||
|
||||
if (k >= 16) do
|
||||
{
|
||||
LZO_DO16(buf, 0);
|
||||
buf += 16;
|
||||
k -= 16;
|
||||
} while (k >= 16);
|
||||
}
|
||||
while (k >= 16);
|
||||
|
||||
if (k != 0) do
|
||||
{
|
||||
s1 += *buf++;
|
||||
s2 += s1;
|
||||
} while (--k > 0);
|
||||
}
|
||||
while (--k > 0);
|
||||
|
||||
s1 %= LZO_BASE;
|
||||
s2 %= LZO_BASE;
|
||||
}
|
||||
|
||||
return (s2 << 16) | s1;
|
||||
}
|
||||
|
||||
|
|
|
@ -57,7 +57,8 @@ extern "C" {
|
|||
//
|
||||
************************************************************************/
|
||||
|
||||
typedef struct {
|
||||
typedef struct
|
||||
{
|
||||
|
||||
/* configuration */
|
||||
unsigned rbits;
|
||||
|
|
|
@ -79,13 +79,18 @@ uLong ZEXPORT adler32(adler, buf, len)
|
|||
adler &= 0xffff;
|
||||
|
||||
/* in case user likes doing a byte at a time, keep it fast */
|
||||
if (len == 1) {
|
||||
if (len == 1)
|
||||
{
|
||||
adler += buf[0];
|
||||
|
||||
if (adler >= BASE)
|
||||
adler -= BASE;
|
||||
|
||||
sum2 += adler;
|
||||
|
||||
if (sum2 >= BASE)
|
||||
sum2 -= BASE;
|
||||
|
||||
return adler | (sum2 << 16);
|
||||
}
|
||||
|
||||
|
@ -94,40 +99,54 @@ uLong ZEXPORT adler32(adler, buf, len)
|
|||
return 1L;
|
||||
|
||||
/* in case short lengths are provided, keep it somewhat fast */
|
||||
if (len < 16) {
|
||||
while (len--) {
|
||||
if (len < 16)
|
||||
{
|
||||
while (len--)
|
||||
{
|
||||
adler += *buf++;
|
||||
sum2 += adler;
|
||||
}
|
||||
|
||||
if (adler >= BASE)
|
||||
adler -= BASE;
|
||||
|
||||
MOD28(sum2); /* only added so many BASE's */
|
||||
return adler | (sum2 << 16);
|
||||
}
|
||||
|
||||
/* do length NMAX blocks -- requires just one modulo operation */
|
||||
while (len >= NMAX) {
|
||||
while (len >= NMAX)
|
||||
{
|
||||
len -= NMAX;
|
||||
n = NMAX / 16; /* NMAX is divisible by 16 */
|
||||
do {
|
||||
|
||||
do
|
||||
{
|
||||
DO16(buf); /* 16 sums unrolled */
|
||||
buf += 16;
|
||||
} while (--n);
|
||||
}
|
||||
while (--n);
|
||||
|
||||
MOD(adler);
|
||||
MOD(sum2);
|
||||
}
|
||||
|
||||
/* do remaining bytes (less than NMAX, still just one modulo) */
|
||||
if (len) { /* avoid modulos if none remaining */
|
||||
while (len >= 16) {
|
||||
if (len) /* avoid modulos if none remaining */
|
||||
{
|
||||
while (len >= 16)
|
||||
{
|
||||
len -= 16;
|
||||
DO16(buf);
|
||||
buf += 16;
|
||||
}
|
||||
while (len--) {
|
||||
|
||||
while (len--)
|
||||
{
|
||||
adler += *buf++;
|
||||
sum2 += adler;
|
||||
}
|
||||
|
||||
MOD(adler);
|
||||
MOD(sum2);
|
||||
}
|
||||
|
@ -162,10 +181,15 @@ local uLong adler32_combine_(adler1, adler2, len2)
|
|||
MOD(sum2);
|
||||
sum1 += (adler2 & 0xffff) + BASE - 1;
|
||||
sum2 += ((adler1 >> 16) & 0xffff) + ((adler2 >> 16) & 0xffff) + BASE - rem;
|
||||
|
||||
if (sum1 >= BASE) sum1 -= BASE;
|
||||
|
||||
if (sum1 >= BASE) sum1 -= BASE;
|
||||
|
||||
if (sum2 >= (BASE << 1)) sum2 -= (BASE << 1);
|
||||
|
||||
if (sum2 >= BASE) sum2 -= BASE;
|
||||
|
||||
return sum1 | (sum2 << 16);
|
||||
}
|
||||
|
||||
|
|
|
@ -36,11 +36,14 @@ int ZEXPORT compress2 (dest, destLen, source, sourceLen, level)
|
|||
stream.next_in = (Bytef*)source;
|
||||
stream.avail_in = (uInt)sourceLen;
|
||||
#ifdef MAXSEG_64K
|
||||
|
||||
/* Check for source > 64K on 16-bit machine: */
|
||||
if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
|
||||
|
||||
#endif
|
||||
stream.next_out = dest;
|
||||
stream.avail_out = (uInt) * destLen;
|
||||
|
||||
if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
|
||||
|
||||
stream.zalloc = (alloc_func)0;
|
||||
|
@ -48,13 +51,17 @@ int ZEXPORT compress2 (dest, destLen, source, sourceLen, level)
|
|||
stream.opaque = (voidpf)0;
|
||||
|
||||
err = deflateInit(&stream, level);
|
||||
|
||||
if (err != Z_OK) return err;
|
||||
|
||||
err = deflate(&stream, Z_FINISH);
|
||||
if (err != Z_STREAM_END) {
|
||||
|
||||
if (err != Z_STREAM_END)
|
||||
{
|
||||
deflateEnd(&stream);
|
||||
return err == Z_OK ? Z_BUF_ERROR : err;
|
||||
}
|
||||
|
||||
*destLen = stream.total_out;
|
||||
|
||||
err = deflateEnd(&stream);
|
||||
|
|
|
@ -99,39 +99,50 @@ local void make_crc_table()
|
|||
/* See if another task is already doing this (not thread-safe, but better
|
||||
than nothing -- significantly reduces duration of vulnerability in
|
||||
case the advice about DYNAMIC_CRC_TABLE is ignored) */
|
||||
if (first) {
|
||||
if (first)
|
||||
{
|
||||
first = 0;
|
||||
|
||||
/* make exclusive-or pattern from polynomial (0xedb88320UL) */
|
||||
poly = 0;
|
||||
|
||||
for (n = 0; n < (int)(sizeof(p) / sizeof(unsigned char)); n++)
|
||||
poly |= (z_crc_t)1 << (31 - p[n]);
|
||||
|
||||
/* 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;
|
||||
|
||||
for (k = 0; k < 8; k++)
|
||||
c = c & 1 ? poly ^ (c >> 1) : c >> 1;
|
||||
|
||||
crc_table[0][n] = c;
|
||||
}
|
||||
|
||||
#ifdef BYFOUR
|
||||
|
||||
/* 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 */
|
||||
for (n = 0; n < 256; n++) {
|
||||
for (n = 0; n < 256; n++)
|
||||
{
|
||||
c = crc_table[0][n];
|
||||
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);
|
||||
crc_table[k][n] = c;
|
||||
crc_table[k + 4][n] = ZSWAP32(c);
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* BYFOUR */
|
||||
|
||||
crc_table_empty = 0;
|
||||
}
|
||||
else { /* not first */
|
||||
else /* not first */
|
||||
{
|
||||
/* wait for the other guy to finish (not efficient, but rare) */
|
||||
while (crc_table_empty)
|
||||
;
|
||||
|
@ -143,7 +154,9 @@ local void make_crc_table()
|
|||
FILE* out;
|
||||
|
||||
out = fopen("crc32.h", "w");
|
||||
|
||||
if (out == NULL) return;
|
||||
|
||||
fprintf(out, "/* crc32.h -- tables for rapid CRC calculation\n");
|
||||
fprintf(out, " * Generated automatically by crc32.c\n */\n\n");
|
||||
fprintf(out, "local const z_crc_t FAR ");
|
||||
|
@ -151,10 +164,13 @@ local void make_crc_table()
|
|||
write_table(out, crc_table[0]);
|
||||
# ifdef BYFOUR
|
||||
fprintf(out, "#ifdef BYFOUR\n");
|
||||
for (k = 1; k < 8; k++) {
|
||||
|
||||
for (k = 1; k < 8; k++)
|
||||
{
|
||||
fprintf(out, " },\n {\n");
|
||||
write_table(out, crc_table[k]);
|
||||
}
|
||||
|
||||
fprintf(out, "#endif\n");
|
||||
# endif /* BYFOUR */
|
||||
fprintf(out, " }\n};\n");
|
||||
|
@ -190,8 +206,10 @@ local void write_table(out, table)
|
|||
const z_crc_t FAR* ZEXPORT get_crc_table()
|
||||
{
|
||||
#ifdef DYNAMIC_CRC_TABLE
|
||||
|
||||
if (crc_table_empty)
|
||||
make_crc_table();
|
||||
|
||||
#endif /* DYNAMIC_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;
|
||||
|
||||
#ifdef DYNAMIC_CRC_TABLE
|
||||
|
||||
if (crc_table_empty)
|
||||
make_crc_table();
|
||||
|
||||
#endif /* DYNAMIC_CRC_TABLE */
|
||||
|
||||
#ifdef BYFOUR
|
||||
#ifdef WIN32
|
||||
int size_match = (sizeof(void*) == sizeof(ptrdiff_t));
|
||||
if (size_match) {
|
||||
|
||||
if (size_match)
|
||||
{
|
||||
#else
|
||||
if (sizeof(void *) == sizeof(ptrdiff_t)) {
|
||||
|
||||
if (sizeof(void*) == sizeof(ptrdiff_t))
|
||||
{
|
||||
#endif /* WIN32 */
|
||||
z_crc_t endian;
|
||||
|
||||
endian = 1;
|
||||
|
||||
if (*((unsigned char*)(&endian)))
|
||||
return crc32_little(crc, buf, len);
|
||||
else
|
||||
return crc32_big(crc, buf, len);
|
||||
}
|
||||
|
||||
#endif /* BYFOUR */
|
||||
crc = crc ^ 0xffffffffUL;
|
||||
while (len >= 8) {
|
||||
|
||||
while (len >= 8)
|
||||
{
|
||||
DO8;
|
||||
len -= 8;
|
||||
}
|
||||
if (len) do {
|
||||
|
||||
if (len) do
|
||||
{
|
||||
DO1;
|
||||
} while (--len);
|
||||
}
|
||||
while (--len);
|
||||
|
||||
return crc ^ 0xffffffffUL;
|
||||
}
|
||||
|
||||
|
@ -267,25 +299,35 @@ local unsigned long crc32_little(crc, buf, len)
|
|||
|
||||
c = (z_crc_t)crc;
|
||||
c = ~c;
|
||||
while (len && ((ptrdiff_t)buf & 3)) {
|
||||
|
||||
while (len && ((ptrdiff_t)buf & 3))
|
||||
{
|
||||
c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8);
|
||||
len--;
|
||||
}
|
||||
|
||||
buf4 = (const z_crc_t FAR*)(const void FAR*)buf;
|
||||
while (len >= 32) {
|
||||
|
||||
while (len >= 32)
|
||||
{
|
||||
DOLIT32;
|
||||
len -= 32;
|
||||
}
|
||||
while (len >= 4) {
|
||||
|
||||
while (len >= 4)
|
||||
{
|
||||
DOLIT4;
|
||||
len -= 4;
|
||||
}
|
||||
|
||||
buf = (const unsigned char FAR*)buf4;
|
||||
|
||||
if (len) do {
|
||||
if (len) do
|
||||
{
|
||||
c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8);
|
||||
} while (--len);
|
||||
}
|
||||
while (--len);
|
||||
|
||||
c = ~c;
|
||||
return (unsigned long)c;
|
||||
}
|
||||
|
@ -311,27 +353,37 @@ local unsigned long crc32_big(crc, buf, len)
|
|||
|
||||
c = ZSWAP32((z_crc_t)crc);
|
||||
c = ~c;
|
||||
while (len && ((ptrdiff_t)buf & 3)) {
|
||||
|
||||
while (len && ((ptrdiff_t)buf & 3))
|
||||
{
|
||||
c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
|
||||
len--;
|
||||
}
|
||||
|
||||
buf4 = (const z_crc_t FAR*)(const void FAR*)buf;
|
||||
buf4--;
|
||||
while (len >= 32) {
|
||||
|
||||
while (len >= 32)
|
||||
{
|
||||
DOBIG32;
|
||||
len -= 32;
|
||||
}
|
||||
while (len >= 4) {
|
||||
|
||||
while (len >= 4)
|
||||
{
|
||||
DOBIG4;
|
||||
len -= 4;
|
||||
}
|
||||
|
||||
buf4++;
|
||||
buf = (const unsigned char FAR*)buf4;
|
||||
|
||||
if (len) do {
|
||||
if (len) do
|
||||
{
|
||||
c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
|
||||
} while (--len);
|
||||
}
|
||||
while (--len);
|
||||
|
||||
c = ~c;
|
||||
return (unsigned long)(ZSWAP32(c));
|
||||
}
|
||||
|
@ -352,12 +404,16 @@ local unsigned long gf2_matrix_times(mat, vec)
|
|||
unsigned long sum;
|
||||
|
||||
sum = 0;
|
||||
while (vec) {
|
||||
|
||||
while (vec)
|
||||
{
|
||||
if (vec & 1)
|
||||
sum ^= *mat;
|
||||
|
||||
vec >>= 1;
|
||||
mat++;
|
||||
}
|
||||
|
||||
return sum;
|
||||
}
|
||||
|
||||
|
@ -398,7 +454,9 @@ local uLong crc32_combine_(crc1, crc2, len2)
|
|||
/* put operator for one zero bit in odd */
|
||||
odd[0] = 0xedb88320UL; /* CRC-32 polynomial */
|
||||
row = 1;
|
||||
for (n = 1; n < GF2_DIM; n++) {
|
||||
|
||||
for (n = 1; n < GF2_DIM; n++)
|
||||
{
|
||||
odd[n] = row;
|
||||
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
|
||||
zero byte, eight zero bits, in even) */
|
||||
do {
|
||||
do
|
||||
{
|
||||
/* apply zeros operator for this bit of len2 */
|
||||
gf2_matrix_square(even, odd);
|
||||
|
||||
if (len2 & 1)
|
||||
crc1 = gf2_matrix_times(even, crc1);
|
||||
|
||||
len2 >>= 1;
|
||||
|
||||
/* 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 */
|
||||
gf2_matrix_square(odd, even);
|
||||
|
||||
if (len2 & 1)
|
||||
crc1 = gf2_matrix_times(odd, crc1);
|
||||
|
||||
len2 >>= 1;
|
||||
|
||||
/* if no more bits set, then done */
|
||||
} while (len2 != 0);
|
||||
}
|
||||
while (len2 != 0);
|
||||
|
||||
/* return combined crc */
|
||||
crc1 ^= crc2;
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -62,12 +62,15 @@
|
|||
|
||||
|
||||
/* Data structure describing a single value and its code string. */
|
||||
typedef struct ct_data_s {
|
||||
union {
|
||||
typedef struct ct_data_s
|
||||
{
|
||||
union
|
||||
{
|
||||
ush freq; /* frequency count */
|
||||
ush code; /* bit string */
|
||||
} fc;
|
||||
union {
|
||||
union
|
||||
{
|
||||
ush dad; /* father node in Huffman tree */
|
||||
ush len; /* length of bit string */
|
||||
} dl;
|
||||
|
@ -80,7 +83,8 @@ typedef struct ct_data_s {
|
|||
|
||||
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 */
|
||||
int max_code; /* largest code with non zero frequency */
|
||||
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.
|
||||
*/
|
||||
|
||||
typedef struct internal_state {
|
||||
typedef struct internal_state
|
||||
{
|
||||
z_streamp strm; /* pointer back to this zlib stream */
|
||||
int status; /* as the name implies */
|
||||
Bytef* pending_buf; /* output still pending */
|
||||
|
|
|
@ -20,6 +20,7 @@ int ZEXPORT gzclose(file)
|
|||
|
||||
if (file == NULL)
|
||||
return Z_STREAM_ERROR;
|
||||
|
||||
state = (gz_statep)file;
|
||||
|
||||
return state->mode == GZ_READ ? gzclose_r(file) : gzclose_w(file);
|
||||
|
|
|
@ -147,7 +147,8 @@
|
|||
#define GZIP 2 /* decompress a gzip stream */
|
||||
|
||||
/* internal gzip file state data structure */
|
||||
typedef struct {
|
||||
typedef struct
|
||||
{
|
||||
/* exposed contents for gzgetc() macro */
|
||||
struct gzFile_s x; /* "x" for exposed */
|
||||
/* x.have: number of bytes available at x.next */
|
||||
|
|
|
@ -45,15 +45,19 @@ char ZLIB_INTERNAL *gz_strwinerror (error)
|
|||
(LPVOID)&msgbuf,
|
||||
0,
|
||||
NULL);
|
||||
if (chars != 0) {
|
||||
|
||||
if (chars != 0)
|
||||
{
|
||||
/* If there is an \r\n appended, zap it. */
|
||||
if (chars >= 2
|
||||
&& msgbuf[chars - 2] == '\r' && msgbuf[chars - 1] == '\n') {
|
||||
&& msgbuf[chars - 2] == '\r' && msgbuf[chars - 1] == '\n')
|
||||
{
|
||||
chars -= 2;
|
||||
msgbuf[chars] = 0;
|
||||
}
|
||||
|
||||
if (chars > sizeof (buf) - 1) {
|
||||
if (chars > sizeof(buf) - 1)
|
||||
{
|
||||
chars = sizeof(buf) - 1;
|
||||
msgbuf[chars] = 0;
|
||||
}
|
||||
|
@ -61,7 +65,8 @@ char ZLIB_INTERNAL *gz_strwinerror (error)
|
|||
wcstombs(buf, msgbuf, chars + 1);
|
||||
LocalFree(msgbuf);
|
||||
}
|
||||
else {
|
||||
else
|
||||
{
|
||||
sprintf(buf, "unknown win32 error (%ld)", error);
|
||||
}
|
||||
|
||||
|
@ -80,11 +85,14 @@ local void gz_reset(state)
|
|||
#endif
|
||||
{
|
||||
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->past = 0; /* have not read past end yet */
|
||||
state->how = LOOK; /* look for gzip header */
|
||||
}
|
||||
|
||||
state->seek = 0; /* no seek request pending */
|
||||
gz_error(state, Z_OK, NULL); /* clear error */
|
||||
state->x.pos = 0; /* no uncompressed data yet */
|
||||
|
@ -117,8 +125,10 @@ local gzFile gz_open(path, fd, mode)
|
|||
|
||||
/* allocate gzFile structure to return */
|
||||
state = malloc(sizeof(gz_state));
|
||||
|
||||
if (state == NULL)
|
||||
return NULL;
|
||||
|
||||
state->size = 0; /* no buffers allocated yet */
|
||||
state->want = GZBUFSIZE; /* requested buffer size */
|
||||
state->msg = NULL; /* no error message yet */
|
||||
|
@ -128,87 +138,115 @@ local gzFile gz_open(path, fd, mode)
|
|||
state->level = Z_DEFAULT_COMPRESSION;
|
||||
state->strategy = Z_DEFAULT_STRATEGY;
|
||||
state->direct = 0;
|
||||
while (*mode) {
|
||||
|
||||
while (*mode)
|
||||
{
|
||||
if (*mode >= '0' && *mode <= '9')
|
||||
state->level = *mode - '0';
|
||||
else
|
||||
switch (*mode) {
|
||||
switch (*mode)
|
||||
{
|
||||
case 'r':
|
||||
state->mode = GZ_READ;
|
||||
break;
|
||||
#ifndef NO_GZCOMPRESS
|
||||
|
||||
case 'w':
|
||||
state->mode = GZ_WRITE;
|
||||
break;
|
||||
|
||||
case 'a':
|
||||
state->mode = GZ_APPEND;
|
||||
break;
|
||||
#endif
|
||||
|
||||
case '+': /* can't read and write at the same time */
|
||||
free(state);
|
||||
return NULL;
|
||||
|
||||
case 'b': /* ignore -- will request binary anyway */
|
||||
break;
|
||||
#ifdef O_CLOEXEC
|
||||
|
||||
case 'e':
|
||||
cloexec = 1;
|
||||
break;
|
||||
#endif
|
||||
#ifdef O_EXCL
|
||||
|
||||
case 'x':
|
||||
exclusive = 1;
|
||||
break;
|
||||
#endif
|
||||
|
||||
case 'f':
|
||||
state->strategy = Z_FILTERED;
|
||||
break;
|
||||
|
||||
case 'h':
|
||||
state->strategy = Z_HUFFMAN_ONLY;
|
||||
break;
|
||||
|
||||
case 'R':
|
||||
state->strategy = Z_RLE;
|
||||
break;
|
||||
|
||||
case 'F':
|
||||
state->strategy = Z_FIXED;
|
||||
|
||||
case 'T':
|
||||
state->direct = 1;
|
||||
|
||||
default: /* could consider as an error, but just ignore */
|
||||
;
|
||||
}
|
||||
|
||||
mode++;
|
||||
}
|
||||
|
||||
/* must provide an "r", "w", or "a" */
|
||||
if (state->mode == GZ_NONE) {
|
||||
if (state->mode == GZ_NONE)
|
||||
{
|
||||
free(state);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* can't force transparent read */
|
||||
if (state->mode == GZ_READ) {
|
||||
if (state->direct) {
|
||||
if (state->mode == GZ_READ)
|
||||
{
|
||||
if (state->direct)
|
||||
{
|
||||
free(state);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
state->direct = 1; /* for empty file */
|
||||
}
|
||||
|
||||
/* save the path name for error messages */
|
||||
#ifdef _WIN32
|
||||
if (fd == -2) {
|
||||
|
||||
if (fd == -2)
|
||||
{
|
||||
len = wcstombs(NULL, path, 0);
|
||||
|
||||
if (len == (size_t) - 1)
|
||||
len = 0;
|
||||
}
|
||||
else
|
||||
#endif
|
||||
len = strlen(path);
|
||||
|
||||
state->path = malloc(len + 1);
|
||||
if (state->path == NULL) {
|
||||
|
||||
if (state->path == NULL)
|
||||
{
|
||||
free(state);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
|
||||
if (fd == -2)
|
||||
if (len)
|
||||
wcstombs(state->path, path, len + 1);
|
||||
|
@ -245,17 +283,22 @@ local gzFile gz_open(path, fd, mode)
|
|||
fd == -2 ? _wopen(path, oflag, 0666) :
|
||||
#endif
|
||||
open(path, oflag, 0666));
|
||||
if (state->fd == -1) {
|
||||
|
||||
if (state->fd == -1)
|
||||
{
|
||||
free(state->path);
|
||||
free(state);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (state->mode == GZ_APPEND)
|
||||
state->mode = GZ_WRITE; /* simplify later checks */
|
||||
|
||||
/* 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);
|
||||
|
||||
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)
|
||||
return NULL;
|
||||
|
||||
sprintf(path, "<fd:%d>", fd); /* for debugging */
|
||||
gz = gz_open(path, fd, mode);
|
||||
free(path);
|
||||
|
@ -336,7 +380,9 @@ int ZEXPORT gzbuffer(file, size)
|
|||
/* get internal structure and check integrity */
|
||||
if (file == NULL)
|
||||
return -1;
|
||||
|
||||
state = (gz_statep)file;
|
||||
|
||||
if (state->mode != GZ_READ && state->mode != GZ_WRITE)
|
||||
return -1;
|
||||
|
||||
|
@ -347,6 +393,7 @@ int ZEXPORT gzbuffer(file, size)
|
|||
/* check and set requested size */
|
||||
if (size < 2)
|
||||
size = 2; /* need two bytes to check magic header */
|
||||
|
||||
state->want = size;
|
||||
return 0;
|
||||
}
|
||||
|
@ -364,6 +411,7 @@ int ZEXPORT gzrewind(file)
|
|||
/* get internal structure */
|
||||
if (file == NULL)
|
||||
return -1;
|
||||
|
||||
state = (gz_statep)file;
|
||||
|
||||
/* check that we're reading and that there's no error */
|
||||
|
@ -374,6 +422,7 @@ int ZEXPORT gzrewind(file)
|
|||
/* back up and start over */
|
||||
if (LSEEK(state->fd, state->start, SEEK_SET) == -1)
|
||||
return -1;
|
||||
|
||||
gz_reset(state);
|
||||
return 0;
|
||||
}
|
||||
|
@ -395,7 +444,9 @@ z_off64_t ZEXPORT gzseek64(file, offset, whence)
|
|||
/* get internal structure and check integrity */
|
||||
if (file == NULL)
|
||||
return -1;
|
||||
|
||||
state = (gz_statep)file;
|
||||
|
||||
if (state->mode != GZ_READ && state->mode != GZ_WRITE)
|
||||
return -1;
|
||||
|
||||
|
@ -412,14 +463,18 @@ z_off64_t ZEXPORT gzseek64(file, offset, whence)
|
|||
offset -= state->x.pos;
|
||||
else if (state->seek)
|
||||
offset += state->skip;
|
||||
|
||||
state->seek = 0;
|
||||
|
||||
/* if within raw area while reading, just go there */
|
||||
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);
|
||||
|
||||
if (ret == -1)
|
||||
return -1;
|
||||
|
||||
state->x.have = 0;
|
||||
state->eof = 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 */
|
||||
if (offset < 0) {
|
||||
if (offset < 0)
|
||||
{
|
||||
if (state->mode != GZ_READ) /* writing -- can't go backwards */
|
||||
return -1;
|
||||
|
||||
offset += state->x.pos;
|
||||
|
||||
if (offset < 0) /* before start of file! */
|
||||
return -1;
|
||||
|
||||
if (gzrewind(file) == -1) /* rewind, then skip to offset */
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* 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 ?
|
||||
(unsigned)offset : state->x.have;
|
||||
state->x.have -= n;
|
||||
|
@ -452,10 +512,12 @@ z_off64_t ZEXPORT gzseek64(file, offset, whence)
|
|||
}
|
||||
|
||||
/* request skip (if not zero) */
|
||||
if (offset) {
|
||||
if (offset)
|
||||
{
|
||||
state->seek = 1;
|
||||
state->skip = offset;
|
||||
}
|
||||
|
||||
return state->x.pos + offset;
|
||||
}
|
||||
|
||||
|
@ -488,7 +550,9 @@ z_off64_t ZEXPORT gztell64(file)
|
|||
/* get internal structure and check integrity */
|
||||
if (file == NULL)
|
||||
return -1;
|
||||
|
||||
state = (gz_statep)file;
|
||||
|
||||
if (state->mode != GZ_READ && state->mode != GZ_WRITE)
|
||||
return -1;
|
||||
|
||||
|
@ -524,16 +588,21 @@ z_off64_t ZEXPORT gzoffset64(file)
|
|||
/* get internal structure and check integrity */
|
||||
if (file == NULL)
|
||||
return -1;
|
||||
|
||||
state = (gz_statep)file;
|
||||
|
||||
if (state->mode != GZ_READ && state->mode != GZ_WRITE)
|
||||
return -1;
|
||||
|
||||
/* compute and return effective offset in file */
|
||||
offset = LSEEK(state->fd, 0, SEEK_CUR);
|
||||
|
||||
if (offset == -1)
|
||||
return -1;
|
||||
|
||||
if (state->mode == GZ_READ) /* reading */
|
||||
offset -= state->strm.avail_in; /* don't count buffered input */
|
||||
|
||||
return offset;
|
||||
}
|
||||
|
||||
|
@ -564,7 +633,9 @@ int ZEXPORT gzeof(file)
|
|||
/* get internal structure and check integrity */
|
||||
if (file == NULL)
|
||||
return 0;
|
||||
|
||||
state = (gz_statep)file;
|
||||
|
||||
if (state->mode != GZ_READ && state->mode != GZ_WRITE)
|
||||
return 0;
|
||||
|
||||
|
@ -586,13 +657,16 @@ const char * ZEXPORT gzerror(file, errnum)
|
|||
/* get internal structure and check integrity */
|
||||
if (file == NULL)
|
||||
return NULL;
|
||||
|
||||
state = (gz_statep)file;
|
||||
|
||||
if (state->mode != GZ_READ && state->mode != GZ_WRITE)
|
||||
return NULL;
|
||||
|
||||
/* return error information */
|
||||
if (errnum != NULL)
|
||||
*errnum = state->err;
|
||||
|
||||
return state->msg == NULL ? "" : state->msg;
|
||||
}
|
||||
|
||||
|
@ -609,15 +683,19 @@ void ZEXPORT gzclearerr(file)
|
|||
/* get internal structure and check integrity */
|
||||
if (file == NULL)
|
||||
return;
|
||||
|
||||
state = (gz_statep)file;
|
||||
|
||||
if (state->mode != GZ_READ && state->mode != GZ_WRITE)
|
||||
return;
|
||||
|
||||
/* clear error and end-of-file */
|
||||
if (state->mode == GZ_READ) {
|
||||
if (state->mode == GZ_READ)
|
||||
{
|
||||
state->eof = 0;
|
||||
state->past = 0;
|
||||
}
|
||||
|
||||
gz_error(state, Z_OK, NULL);
|
||||
}
|
||||
|
||||
|
@ -637,9 +715,11 @@ void ZLIB_INTERNAL gz_error(state, err, msg)
|
|||
#endif
|
||||
{
|
||||
/* free previously allocated message and clear */
|
||||
if (state->msg != NULL) {
|
||||
if (state->msg != NULL)
|
||||
{
|
||||
if (state->err != Z_MEM_ERROR)
|
||||
free(state->msg);
|
||||
|
||||
state->msg = NULL;
|
||||
}
|
||||
|
||||
|
@ -649,21 +729,25 @@ void ZLIB_INTERNAL gz_error(state, err, msg)
|
|||
|
||||
/* set error code, and if no message, then done */
|
||||
state->err = err;
|
||||
|
||||
if (msg == NULL)
|
||||
return;
|
||||
|
||||
/* for an out of memory error, save as static string */
|
||||
if (err == Z_MEM_ERROR) {
|
||||
if (err == Z_MEM_ERROR)
|
||||
{
|
||||
state->msg = (char*)msg;
|
||||
return;
|
||||
}
|
||||
|
||||
/* 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->msg = (char*)"out of memory";
|
||||
return;
|
||||
}
|
||||
|
||||
strcpy(state->msg, state->path);
|
||||
strcat(state->msg, ": ");
|
||||
strcat(state->msg, msg);
|
||||
|
@ -680,11 +764,15 @@ unsigned ZLIB_INTERNAL gz_intmax()
|
|||
unsigned p, q;
|
||||
|
||||
p = 1;
|
||||
do {
|
||||
|
||||
do
|
||||
{
|
||||
q = p;
|
||||
p <<= 1;
|
||||
p++;
|
||||
} while (p > q);
|
||||
}
|
||||
while (p > q);
|
||||
|
||||
return q >> 1;
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -30,18 +30,27 @@ local int gz_load(state, buf, len, have)
|
|||
int ret;
|
||||
|
||||
*have = 0;
|
||||
do {
|
||||
|
||||
do
|
||||
{
|
||||
ret = read(state->fd, buf + *have, len - *have);
|
||||
|
||||
if (ret <= 0)
|
||||
break;
|
||||
|
||||
*have += ret;
|
||||
} while (*have < len);
|
||||
if (ret < 0) {
|
||||
}
|
||||
while (*have < len);
|
||||
|
||||
if (ret < 0)
|
||||
{
|
||||
gz_error(state, Z_ERRNO, zstrerror());
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (ret == 0)
|
||||
state->eof = 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -64,20 +73,29 @@ local int gz_avail(state)
|
|||
|
||||
if (state->err != Z_OK && state->err != Z_BUF_ERROR)
|
||||
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 n = strm->avail_in;
|
||||
do {
|
||||
|
||||
do
|
||||
{
|
||||
*p++ = *q++;
|
||||
} while (--n);
|
||||
}
|
||||
while (--n);
|
||||
}
|
||||
|
||||
if (gz_load(state, state->in + strm->avail_in,
|
||||
state->size - strm->avail_in, &got) == -1)
|
||||
return -1;
|
||||
|
||||
strm->avail_in += got;
|
||||
strm->next_in = state->in;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -100,18 +118,24 @@ local int gz_look(state)
|
|||
z_streamp strm = &(state->strm);
|
||||
|
||||
/* allocate read buffers and inflate memory */
|
||||
if (state->size == 0) {
|
||||
if (state->size == 0)
|
||||
{
|
||||
/* allocate buffers */
|
||||
state->in = malloc(state->want);
|
||||
state->out = malloc(state->want << 1);
|
||||
if (state->in == NULL || state->out == NULL) {
|
||||
|
||||
if (state->in == NULL || state->out == NULL)
|
||||
{
|
||||
if (state->out != NULL)
|
||||
free(state->out);
|
||||
|
||||
if (state->in != NULL)
|
||||
free(state->in);
|
||||
|
||||
gz_error(state, Z_MEM_ERROR, "out of memory");
|
||||
return -1;
|
||||
}
|
||||
|
||||
state->size = state->want;
|
||||
|
||||
/* allocate inflate memory */
|
||||
|
@ -120,7 +144,9 @@ local int gz_look(state)
|
|||
state->strm.opaque = Z_NULL;
|
||||
state->strm.avail_in = 0;
|
||||
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->in);
|
||||
state->size = 0;
|
||||
|
@ -130,9 +156,11 @@ local int gz_look(state)
|
|||
}
|
||||
|
||||
/* 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)
|
||||
return -1;
|
||||
|
||||
if (strm->avail_in == 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
|
||||
single byte is sufficient indication that it is not a gzip file) */
|
||||
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);
|
||||
state->how = GZIP;
|
||||
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
|
||||
garbage. Ignore the trailing garbage and finish. */
|
||||
if (state->direct == 0) {
|
||||
if (state->direct == 0)
|
||||
{
|
||||
strm->avail_in = 0;
|
||||
state->eof = 1;
|
||||
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
|
||||
space for gzungetc() */
|
||||
state->x.next = state->out;
|
||||
if (strm->avail_in) {
|
||||
|
||||
if (strm->avail_in)
|
||||
{
|
||||
memcpy(state->x.next, strm->next_in, strm->avail_in);
|
||||
state->x.have = strm->avail_in;
|
||||
strm->avail_in = 0;
|
||||
}
|
||||
|
||||
state->how = COPY;
|
||||
state->direct = 1;
|
||||
return 0;
|
||||
|
@ -193,32 +226,43 @@ local int gz_decomp(state)
|
|||
|
||||
/* fill output buffer up to end of deflate stream */
|
||||
had = strm->avail_out;
|
||||
do {
|
||||
|
||||
do
|
||||
{
|
||||
/* get more input for inflate() */
|
||||
if (strm->avail_in == 0 && gz_avail(state) == -1)
|
||||
return -1;
|
||||
if (strm->avail_in == 0) {
|
||||
|
||||
if (strm->avail_in == 0)
|
||||
{
|
||||
gz_error(state, Z_BUF_ERROR, "unexpected end of file");
|
||||
break;
|
||||
}
|
||||
|
||||
/* decompress and handle errors */
|
||||
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,
|
||||
"internal error: inflate stream corrupt");
|
||||
return -1;
|
||||
}
|
||||
if (ret == Z_MEM_ERROR) {
|
||||
|
||||
if (ret == Z_MEM_ERROR)
|
||||
{
|
||||
gz_error(state, Z_MEM_ERROR, "out of memory");
|
||||
return -1;
|
||||
}
|
||||
if (ret == Z_DATA_ERROR) { /* deflate stream invalid */
|
||||
|
||||
if (ret == Z_DATA_ERROR) /* deflate stream invalid */
|
||||
{
|
||||
gz_error(state, Z_DATA_ERROR,
|
||||
strm->msg == NULL ? "compressed data error" : strm->msg);
|
||||
return -1;
|
||||
}
|
||||
} while (strm->avail_out && ret != Z_STREAM_END);
|
||||
}
|
||||
while (strm->avail_out && ret != Z_STREAM_END);
|
||||
|
||||
/* update available output */
|
||||
state->x.have = had - strm->avail_out;
|
||||
|
@ -247,27 +291,37 @@ local int gz_fetch(state)
|
|||
{
|
||||
z_streamp strm = &(state->strm);
|
||||
|
||||
do {
|
||||
switch(state->how) {
|
||||
do
|
||||
{
|
||||
switch (state->how)
|
||||
{
|
||||
case LOOK: /* -> LOOK, COPY (only if never GZIP), or GZIP */
|
||||
if (gz_look(state) == -1)
|
||||
return -1;
|
||||
|
||||
if (state->how == LOOK)
|
||||
return 0;
|
||||
|
||||
break;
|
||||
|
||||
case COPY: /* -> COPY */
|
||||
if (gz_load(state, state->out, state->size << 1, &(state->x.have))
|
||||
== -1)
|
||||
return -1;
|
||||
|
||||
state->x.next = state->out;
|
||||
return 0;
|
||||
|
||||
case GZIP: /* -> GZIP or LOOK (if end of gzip stream) */
|
||||
strm->avail_out = state->size << 1;
|
||||
strm->next_out = state->out;
|
||||
|
||||
if (gz_decomp(state) == -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;
|
||||
}
|
||||
|
||||
|
@ -284,8 +338,10 @@ local int gz_skip(state, len)
|
|||
|
||||
/* skip over len bytes or reach end-of-file, whichever comes first */
|
||||
while (len)
|
||||
|
||||
/* 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 ?
|
||||
(unsigned)len : state->x.have;
|
||||
state->x.have -= n;
|
||||
|
@ -299,11 +355,13 @@ local int gz_skip(state, len)
|
|||
break;
|
||||
|
||||
/* need more data to skip -- load up output buffer */
|
||||
else {
|
||||
else
|
||||
{
|
||||
/* get more output, looking for header if required */
|
||||
if (gz_fetch(state) == -1)
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -324,6 +382,7 @@ int ZEXPORT gzread(file, buf, len)
|
|||
/* get internal structure */
|
||||
if (file == NULL)
|
||||
return -1;
|
||||
|
||||
state = (gz_statep)file;
|
||||
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
|
||||
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");
|
||||
return -1;
|
||||
}
|
||||
|
@ -344,17 +404,22 @@ int ZEXPORT gzread(file, buf, len)
|
|||
return 0;
|
||||
|
||||
/* process a skip request */
|
||||
if (state->seek) {
|
||||
if (state->seek)
|
||||
{
|
||||
state->seek = 0;
|
||||
|
||||
if (gz_skip(state, state->skip) == -1)
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* get len bytes to buf, or less than len if at the end */
|
||||
got = 0;
|
||||
do {
|
||||
|
||||
do
|
||||
{
|
||||
/* 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;
|
||||
memcpy(buf, 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 */
|
||||
else if (state->eof && strm->avail_in == 0) {
|
||||
else if (state->eof && strm->avail_in == 0)
|
||||
{
|
||||
state->past = 1; /* tried to read past end */
|
||||
break;
|
||||
}
|
||||
|
||||
/* need output data -- for small len or new stream load up our output
|
||||
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 */
|
||||
if (gz_fetch(state) == -1)
|
||||
return -1;
|
||||
|
||||
continue; /* no progress yet -- go back to copy above */
|
||||
/* the copy above assures that we will leave with space in the
|
||||
output buffer, allowing at least one gzungetc() to succeed */
|
||||
}
|
||||
|
||||
/* 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)
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* large len -- decompress directly into user buffer */
|
||||
else { /* state->how == GZIP */
|
||||
else /* state->how == GZIP */
|
||||
{
|
||||
strm->avail_out = len;
|
||||
strm->next_out = buf;
|
||||
|
||||
if (gz_decomp(state) == -1)
|
||||
return -1;
|
||||
|
||||
n = state->x.have;
|
||||
state->x.have = 0;
|
||||
}
|
||||
|
@ -399,7 +471,8 @@ int ZEXPORT gzread(file, buf, len)
|
|||
buf = (char*)buf + n;
|
||||
got += n;
|
||||
state->x.pos += n;
|
||||
} while (len);
|
||||
}
|
||||
while (len);
|
||||
|
||||
/* return number of bytes read into user buffer (will fit in int) */
|
||||
return (int)got;
|
||||
|
@ -421,6 +494,7 @@ int ZEXPORT gzgetc(file)
|
|||
/* get internal structure */
|
||||
if (file == NULL)
|
||||
return -1;
|
||||
|
||||
state = (gz_statep)file;
|
||||
|
||||
/* check that we're reading and that there's no (serious) error */
|
||||
|
@ -429,7 +503,8 @@ int ZEXPORT gzgetc(file)
|
|||
return -1;
|
||||
|
||||
/* try output buffer (no need to check for skip request) */
|
||||
if (state->x.have) {
|
||||
if (state->x.have)
|
||||
{
|
||||
state->x.have--;
|
||||
state->x.pos++;
|
||||
return *(state->x.next)++;
|
||||
|
@ -464,6 +539,7 @@ int ZEXPORT gzungetc(c, file)
|
|||
/* get internal structure */
|
||||
if (file == NULL)
|
||||
return -1;
|
||||
|
||||
state = (gz_statep)file;
|
||||
|
||||
/* check that we're reading and that there's no (serious) error */
|
||||
|
@ -472,8 +548,10 @@ int ZEXPORT gzungetc(c, file)
|
|||
return -1;
|
||||
|
||||
/* process a skip request */
|
||||
if (state->seek) {
|
||||
if (state->seek)
|
||||
{
|
||||
state->seek = 0;
|
||||
|
||||
if (gz_skip(state, state->skip) == -1)
|
||||
return -1;
|
||||
}
|
||||
|
@ -483,7 +561,8 @@ int ZEXPORT gzungetc(c, file)
|
|||
return -1;
|
||||
|
||||
/* 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.next = state->out + (state->size << 1) - 1;
|
||||
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 (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");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* 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* dest = state->out + (state->size << 1);
|
||||
|
||||
while (src > state->out)
|
||||
*--dest = *--src;
|
||||
|
||||
state->x.next = dest;
|
||||
}
|
||||
|
||||
state->x.have++;
|
||||
state->x.next--;
|
||||
state->x.next[0] = c;
|
||||
|
@ -532,6 +616,7 @@ char * ZEXPORT gzgets(file, buf, len)
|
|||
/* check parameters and get internal structure */
|
||||
if (file == NULL || buf == NULL || len < 1)
|
||||
return NULL;
|
||||
|
||||
state = (gz_statep)file;
|
||||
|
||||
/* check that we're reading and that there's no (serious) error */
|
||||
|
@ -540,8 +625,10 @@ char * ZEXPORT gzgets(file, buf, len)
|
|||
return NULL;
|
||||
|
||||
/* process a skip request */
|
||||
if (state->seek) {
|
||||
if (state->seek)
|
||||
{
|
||||
state->seek = 0;
|
||||
|
||||
if (gz_skip(state, state->skip) == -1)
|
||||
return NULL;
|
||||
}
|
||||
|
@ -551,11 +638,15 @@ char * ZEXPORT gzgets(file, buf, len)
|
|||
the contents, let the user worry about that) */
|
||||
str = buf;
|
||||
left = (unsigned)len - 1;
|
||||
if (left) do {
|
||||
|
||||
if (left) do
|
||||
{
|
||||
/* assure that something is in the output buffer */
|
||||
if (state->x.have == 0 && gz_fetch(state) == -1)
|
||||
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 */
|
||||
break; /* return what we have */
|
||||
}
|
||||
|
@ -563,6 +654,7 @@ char * ZEXPORT gzgets(file, buf, len)
|
|||
/* look for end-of-line in current output buffer */
|
||||
n = state->x.have > left ? left : state->x.have;
|
||||
eol = memchr(state->x.next, '\n', n);
|
||||
|
||||
if (eol != NULL)
|
||||
n = (unsigned)(eol - state->x.next) + 1;
|
||||
|
||||
|
@ -573,11 +665,13 @@ char * ZEXPORT gzgets(file, buf, len)
|
|||
state->x.pos += n;
|
||||
left -= n;
|
||||
buf += n;
|
||||
} while (left && eol == NULL);
|
||||
}
|
||||
while (left && eol == NULL);
|
||||
|
||||
/* return terminated string, or if nothing, end of file */
|
||||
if (buf == str)
|
||||
return NULL;
|
||||
|
||||
buf[0] = 0;
|
||||
return str;
|
||||
}
|
||||
|
@ -595,6 +689,7 @@ int ZEXPORT gzdirect(file)
|
|||
/* get internal structure */
|
||||
if (file == NULL)
|
||||
return 0;
|
||||
|
||||
state = (gz_statep)file;
|
||||
|
||||
/* 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 */
|
||||
if (file == NULL)
|
||||
return Z_STREAM_ERROR;
|
||||
|
||||
state = (gz_statep)file;
|
||||
|
||||
/* check that we're reading */
|
||||
|
@ -627,11 +723,13 @@ int ZEXPORT gzclose_r(file)
|
|||
return Z_STREAM_ERROR;
|
||||
|
||||
/* free memory and close file */
|
||||
if (state->size) {
|
||||
if (state->size)
|
||||
{
|
||||
inflateEnd(&(state->strm));
|
||||
free(state->out);
|
||||
free(state->in);
|
||||
}
|
||||
|
||||
err = state->err == Z_BUF_ERROR ? Z_BUF_ERROR : Z_OK;
|
||||
gz_error(state, Z_OK, NULL);
|
||||
free(state->path);
|
||||
|
|
|
@ -24,16 +24,21 @@ local int gz_init(state)
|
|||
|
||||
/* allocate input buffer */
|
||||
state->in = malloc(state->want);
|
||||
if (state->in == NULL) {
|
||||
|
||||
if (state->in == NULL)
|
||||
{
|
||||
gz_error(state, Z_MEM_ERROR, "out of memory");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* only need output buffer and deflate state if compressing */
|
||||
if (!state->direct) {
|
||||
if (!state->direct)
|
||||
{
|
||||
/* allocate output buffer */
|
||||
state->out = malloc(state->want);
|
||||
if (state->out == NULL) {
|
||||
|
||||
if (state->out == NULL)
|
||||
{
|
||||
free(state->in);
|
||||
gz_error(state, Z_MEM_ERROR, "out of memory");
|
||||
return -1;
|
||||
|
@ -45,7 +50,9 @@ local int gz_init(state)
|
|||
strm->opaque = Z_NULL;
|
||||
ret = deflateInit2(strm, state->level, Z_DEFLATED,
|
||||
MAX_WBITS + 16, DEF_MEM_LEVEL, state->strategy);
|
||||
if (ret != Z_OK) {
|
||||
|
||||
if (ret != Z_OK)
|
||||
{
|
||||
free(state->out);
|
||||
free(state->in);
|
||||
gz_error(state, Z_MEM_ERROR, "out of memory");
|
||||
|
@ -57,11 +64,13 @@ local int gz_init(state)
|
|||
state->size = state->want;
|
||||
|
||||
/* initialize write buffer if compressing */
|
||||
if (!state->direct) {
|
||||
if (!state->direct)
|
||||
{
|
||||
strm->avail_out = state->size;
|
||||
strm->next_out = state->out;
|
||||
state->x.next = strm->next_out;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -88,46 +97,62 @@ local int gz_comp(state, flush)
|
|||
return -1;
|
||||
|
||||
/* write directly if requested */
|
||||
if (state->direct) {
|
||||
if (state->direct)
|
||||
{
|
||||
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());
|
||||
return -1;
|
||||
}
|
||||
|
||||
strm->avail_in = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* run deflate() on provided input until it produces no more output */
|
||||
ret = Z_OK;
|
||||
do {
|
||||
|
||||
do
|
||||
{
|
||||
/* 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 */
|
||||
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);
|
||||
|
||||
if (have && ((got = write(state->fd, state->x.next, have)) < 0 ||
|
||||
(unsigned)got != have)) {
|
||||
(unsigned)got != have))
|
||||
{
|
||||
gz_error(state, Z_ERRNO, zstrerror());
|
||||
return -1;
|
||||
}
|
||||
if (strm->avail_out == 0) {
|
||||
|
||||
if (strm->avail_out == 0)
|
||||
{
|
||||
strm->avail_out = state->size;
|
||||
strm->next_out = state->out;
|
||||
}
|
||||
|
||||
state->x.next = strm->next_out;
|
||||
}
|
||||
|
||||
/* compress */
|
||||
have = strm->avail_out;
|
||||
ret = deflate(strm, flush);
|
||||
if (ret == Z_STREAM_ERROR) {
|
||||
|
||||
if (ret == Z_STREAM_ERROR)
|
||||
{
|
||||
gz_error(state, Z_STREAM_ERROR,
|
||||
"internal error: deflate stream corrupt");
|
||||
return -1;
|
||||
}
|
||||
|
||||
have -= strm->avail_out;
|
||||
} while (have);
|
||||
}
|
||||
while (have);
|
||||
|
||||
/* if that completed a deflate stream, allow another to start */
|
||||
if (flush == Z_FINISH)
|
||||
|
@ -156,20 +181,28 @@ local int gz_zero(state, len)
|
|||
|
||||
/* compress len zeros (len guaranteed > 0) */
|
||||
first = 1;
|
||||
while (len) {
|
||||
|
||||
while (len)
|
||||
{
|
||||
n = GT_OFF(state->size) || (z_off64_t)state->size > len ?
|
||||
(unsigned)len : state->size;
|
||||
if (first) {
|
||||
|
||||
if (first)
|
||||
{
|
||||
memset(state->in, 0, n);
|
||||
first = 0;
|
||||
}
|
||||
|
||||
strm->avail_in = n;
|
||||
strm->next_in = state->in;
|
||||
state->x.pos += n;
|
||||
|
||||
if (gz_comp(state, Z_NO_FLUSH) == -1)
|
||||
return -1;
|
||||
|
||||
len -= n;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -191,6 +224,7 @@ int ZEXPORT gzwrite(file, buf, len)
|
|||
/* get internal structure */
|
||||
if (file == NULL)
|
||||
return 0;
|
||||
|
||||
state = (gz_statep)file;
|
||||
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
|
||||
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");
|
||||
return 0;
|
||||
}
|
||||
|
@ -214,31 +249,41 @@ int ZEXPORT gzwrite(file, buf, len)
|
|||
return 0;
|
||||
|
||||
/* check for seek request */
|
||||
if (state->seek) {
|
||||
if (state->seek)
|
||||
{
|
||||
state->seek = 0;
|
||||
|
||||
if (gz_zero(state, state->skip) == -1)
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* 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 */
|
||||
do {
|
||||
do
|
||||
{
|
||||
if (strm->avail_in == 0)
|
||||
strm->next_in = state->in;
|
||||
|
||||
n = state->size - strm->avail_in;
|
||||
|
||||
if (n > len)
|
||||
n = len;
|
||||
|
||||
memcpy(strm->next_in + strm->avail_in, buf, n);
|
||||
strm->avail_in += n;
|
||||
state->x.pos += n;
|
||||
buf = (char*)buf + n;
|
||||
len -= n;
|
||||
|
||||
if (len && gz_comp(state, Z_NO_FLUSH) == -1)
|
||||
return 0;
|
||||
} while (len);
|
||||
}
|
||||
else {
|
||||
while (len);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* consume whatever's left in the input buffer */
|
||||
if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
|
||||
return 0;
|
||||
|
@ -247,6 +292,7 @@ int ZEXPORT gzwrite(file, buf, len)
|
|||
strm->avail_in = len;
|
||||
strm->next_in = (voidp)buf;
|
||||
state->x.pos += len;
|
||||
|
||||
if (gz_comp(state, Z_NO_FLUSH) == -1)
|
||||
return 0;
|
||||
}
|
||||
|
@ -271,6 +317,7 @@ int ZEXPORT gzputc(file, c)
|
|||
/* get internal structure */
|
||||
if (file == NULL)
|
||||
return -1;
|
||||
|
||||
state = (gz_statep)file;
|
||||
strm = &(state->strm);
|
||||
|
||||
|
@ -279,17 +326,21 @@ int ZEXPORT gzputc(file, c)
|
|||
return -1;
|
||||
|
||||
/* check for seek request */
|
||||
if (state->seek) {
|
||||
if (state->seek)
|
||||
{
|
||||
state->seek = 0;
|
||||
|
||||
if (gz_zero(state, state->skip) == -1)
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* try writing to input buffer for speed (state->size == 0 if buffer not
|
||||
initialized) */
|
||||
if (strm->avail_in < state->size) {
|
||||
if (strm->avail_in < state->size)
|
||||
{
|
||||
if (strm->avail_in == 0)
|
||||
strm->next_in = state->in;
|
||||
|
||||
strm->next_in[strm->avail_in++] = c;
|
||||
state->x.pos++;
|
||||
return c & 0xff;
|
||||
|
@ -297,8 +348,10 @@ int ZEXPORT gzputc(file, c)
|
|||
|
||||
/* no room in buffer or not initialized, use gz_write() */
|
||||
buf[0] = c;
|
||||
|
||||
if (gzwrite(file, buf, 1) != 1)
|
||||
return -1;
|
||||
|
||||
return c & 0xff;
|
||||
}
|
||||
|
||||
|
@ -334,6 +387,7 @@ int ZEXPORTVA gzprintf (gzFile file, const char *format, ...)
|
|||
/* get internal structure */
|
||||
if (file == NULL)
|
||||
return -1;
|
||||
|
||||
state = (gz_statep)file;
|
||||
strm = &(state->strm);
|
||||
|
||||
|
@ -346,8 +400,10 @@ int ZEXPORTVA gzprintf (gzFile file, const char *format, ...)
|
|||
return 0;
|
||||
|
||||
/* check for seek request */
|
||||
if (state->seek) {
|
||||
if (state->seek)
|
||||
{
|
||||
state->seek = 0;
|
||||
|
||||
if (gz_zero(state, state->skip) == -1)
|
||||
return 0;
|
||||
}
|
||||
|
@ -364,8 +420,10 @@ int ZEXPORTVA gzprintf (gzFile file, const char *format, ...)
|
|||
# ifdef HAS_vsprintf_void
|
||||
(void)vsprintf((char*)(state->in), format, va);
|
||||
va_end(va);
|
||||
|
||||
for (len = 0; len < size; len++)
|
||||
if (state->in[len] == 0) break;
|
||||
|
||||
# else
|
||||
len = vsprintf((char*)(state->in), format, 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 */
|
||||
if (file == NULL)
|
||||
return -1;
|
||||
|
||||
state = (gz_statep)file;
|
||||
strm = &(state->strm);
|
||||
|
||||
|
@ -425,8 +484,10 @@ int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
|
|||
return 0;
|
||||
|
||||
/* check for seek request */
|
||||
if (state->seek) {
|
||||
if (state->seek)
|
||||
{
|
||||
state->seek = 0;
|
||||
|
||||
if (gz_zero(state, state->skip) == -1)
|
||||
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
|
||||
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);
|
||||
|
||||
for (len = 0; len < size; len++)
|
||||
if (state->in[len] == 0) break;
|
||||
|
||||
# else
|
||||
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);
|
||||
|
@ -487,6 +550,7 @@ int ZEXPORT gzflush(file, flush)
|
|||
/* get internal structure */
|
||||
if (file == NULL)
|
||||
return -1;
|
||||
|
||||
state = (gz_statep)file;
|
||||
|
||||
/* check that we're writing and that there's no error */
|
||||
|
@ -498,8 +562,10 @@ int ZEXPORT gzflush(file, flush)
|
|||
return Z_STREAM_ERROR;
|
||||
|
||||
/* check for seek request */
|
||||
if (state->seek) {
|
||||
if (state->seek)
|
||||
{
|
||||
state->seek = 0;
|
||||
|
||||
if (gz_zero(state, state->skip) == -1)
|
||||
return -1;
|
||||
}
|
||||
|
@ -525,6 +591,7 @@ int ZEXPORT gzsetparams(file, level, strategy)
|
|||
/* get internal structure */
|
||||
if (file == NULL)
|
||||
return Z_STREAM_ERROR;
|
||||
|
||||
state = (gz_statep)file;
|
||||
strm = &(state->strm);
|
||||
|
||||
|
@ -537,19 +604,24 @@ int ZEXPORT gzsetparams(file, level, strategy)
|
|||
return Z_OK;
|
||||
|
||||
/* check for seek request */
|
||||
if (state->seek) {
|
||||
if (state->seek)
|
||||
{
|
||||
state->seek = 0;
|
||||
|
||||
if (gz_zero(state, state->skip) == -1)
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* change compression parameters for subsequent input */
|
||||
if (state->size) {
|
||||
if (state->size)
|
||||
{
|
||||
/* flush previous input with previous parameters before changing */
|
||||
if (strm->avail_in && gz_comp(state, Z_PARTIAL_FLUSH) == -1)
|
||||
return state->err;
|
||||
|
||||
deflateParams(strm, level, strategy);
|
||||
}
|
||||
|
||||
state->level = level;
|
||||
state->strategy = strategy;
|
||||
return Z_OK;
|
||||
|
@ -569,6 +641,7 @@ int ZEXPORT gzclose_w(file)
|
|||
/* get internal structure */
|
||||
if (file == NULL)
|
||||
return Z_STREAM_ERROR;
|
||||
|
||||
state = (gz_statep)file;
|
||||
|
||||
/* check that we're writing */
|
||||
|
@ -576,26 +649,35 @@ int ZEXPORT gzclose_w(file)
|
|||
return Z_STREAM_ERROR;
|
||||
|
||||
/* check for seek request */
|
||||
if (state->seek) {
|
||||
if (state->seek)
|
||||
{
|
||||
state->seek = 0;
|
||||
|
||||
if (gz_zero(state, state->skip) == -1)
|
||||
ret = state->err;
|
||||
}
|
||||
|
||||
/* flush, free memory, and close file */
|
||||
if (state->size) {
|
||||
if (state->size)
|
||||
{
|
||||
if (gz_comp(state, Z_FINISH) == -1)
|
||||
ret = state->err;
|
||||
if (!state->direct) {
|
||||
|
||||
if (!state->direct)
|
||||
{
|
||||
(void)deflateEnd(&(state->strm));
|
||||
free(state->out);
|
||||
}
|
||||
|
||||
free(state->in);
|
||||
}
|
||||
|
||||
gz_error(state, Z_OK, NULL);
|
||||
free(state->path);
|
||||
|
||||
if (close(state->fd) == -1)
|
||||
ret = Z_ERRNO;
|
||||
|
||||
free(state);
|
||||
return ret;
|
||||
}
|
||||
|
|
|
@ -58,11 +58,15 @@ int stream_size;
|
|||
if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
|
||||
stream_size != (int)(sizeof(z_stream)))
|
||||
return Z_VERSION_ERROR;
|
||||
|
||||
if (strm == Z_NULL || window == Z_NULL ||
|
||||
windowBits < 8 || windowBits > 15)
|
||||
return Z_STREAM_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
|
||||
return Z_STREAM_ERROR;
|
||||
#else
|
||||
|
@ -70,15 +74,19 @@ int stream_size;
|
|||
strm->opaque = (voidpf)0;
|
||||
#endif
|
||||
}
|
||||
|
||||
if (strm->zfree == (free_func)0)
|
||||
#ifdef Z_SOLO
|
||||
return Z_STREAM_ERROR;
|
||||
|
||||
#else
|
||||
strm->zfree = zcfree;
|
||||
#endif
|
||||
state = (struct inflate_state FAR*)ZALLOC(strm, 1,
|
||||
sizeof(struct inflate_state));
|
||||
|
||||
if (state == Z_NULL) return Z_MEM_ERROR;
|
||||
|
||||
Tracev((stderr, "inflate: allocated\n"));
|
||||
strm->state = (struct internal_state FAR*)state;
|
||||
state->dmax = 32768U;
|
||||
|
@ -113,16 +121,22 @@ struct inflate_state FAR *state;
|
|||
static code fixed[544];
|
||||
|
||||
/* build fixed huffman tables if first call (may not be thread safe) */
|
||||
if (virgin) {
|
||||
if (virgin)
|
||||
{
|
||||
unsigned sym, bits;
|
||||
static code* next;
|
||||
|
||||
/* literal/length table */
|
||||
sym = 0;
|
||||
|
||||
while (sym < 144) state->lens[sym++] = 8;
|
||||
|
||||
while (sym < 256) state->lens[sym++] = 9;
|
||||
|
||||
while (sym < 280) state->lens[sym++] = 7;
|
||||
|
||||
while (sym < 288) state->lens[sym++] = 8;
|
||||
|
||||
next = fixed;
|
||||
lenfix = next;
|
||||
bits = 9;
|
||||
|
@ -130,7 +144,9 @@ struct inflate_state FAR *state;
|
|||
|
||||
/* distance table */
|
||||
sym = 0;
|
||||
|
||||
while (sym < 32) state->lens[sym++] = 5;
|
||||
|
||||
distfix = next;
|
||||
bits = 5;
|
||||
inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work);
|
||||
|
@ -138,6 +154,7 @@ struct inflate_state FAR *state;
|
|||
/* do this just once */
|
||||
virgin = 0;
|
||||
}
|
||||
|
||||
#else /* !BUILDFIXED */
|
||||
# include "inffixed.h"
|
||||
#endif /* BUILDFIXED */
|
||||
|
@ -301,6 +318,7 @@ void FAR *out_desc;
|
|||
/* Check that the strm exists and that the state was initialized */
|
||||
if (strm == Z_NULL || strm->state == Z_NULL)
|
||||
return Z_STREAM_ERROR;
|
||||
|
||||
state = (struct inflate_state FAR*)strm->state;
|
||||
|
||||
/* Reset the state */
|
||||
|
@ -317,38 +335,48 @@ void FAR *out_desc;
|
|||
|
||||
/* Inflate until end of block marked as last */
|
||||
for (;;)
|
||||
switch (state->mode) {
|
||||
switch (state->mode)
|
||||
{
|
||||
case TYPE:
|
||||
|
||||
/* determine and dispatch block type */
|
||||
if (state->last) {
|
||||
if (state->last)
|
||||
{
|
||||
BYTEBITS();
|
||||
state->mode = DONE;
|
||||
break;
|
||||
}
|
||||
|
||||
NEEDBITS(3);
|
||||
state->last = BITS(1);
|
||||
DROPBITS(1);
|
||||
switch (BITS(2)) {
|
||||
|
||||
switch (BITS(2))
|
||||
{
|
||||
case 0: /* stored block */
|
||||
Tracev((stderr, "inflate: stored block%s\n",
|
||||
state->last ? " (last)" : ""));
|
||||
state->mode = STORED;
|
||||
break;
|
||||
|
||||
case 1: /* fixed block */
|
||||
fixedtables(state);
|
||||
Tracev((stderr, "inflate: fixed codes block%s\n",
|
||||
state->last ? " (last)" : ""));
|
||||
state->mode = LEN; /* decode codes */
|
||||
break;
|
||||
|
||||
case 2: /* dynamic block */
|
||||
Tracev((stderr, "inflate: dynamic codes block%s\n",
|
||||
state->last ? " (last)" : ""));
|
||||
state->mode = TABLE;
|
||||
break;
|
||||
|
||||
case 3:
|
||||
strm->msg = (char*)"invalid block type";
|
||||
state->mode = BAD;
|
||||
}
|
||||
|
||||
DROPBITS(2);
|
||||
break;
|
||||
|
||||
|
@ -356,23 +384,30 @@ void FAR *out_desc;
|
|||
/* get and verify stored block length */
|
||||
BYTEBITS(); /* go to byte boundary */
|
||||
NEEDBITS(32);
|
||||
if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
|
||||
|
||||
if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff))
|
||||
{
|
||||
strm->msg = (char*)"invalid stored block lengths";
|
||||
state->mode = BAD;
|
||||
break;
|
||||
}
|
||||
|
||||
state->length = (unsigned)hold & 0xffff;
|
||||
Tracev((stderr, "inflate: stored length %u\n",
|
||||
state->length));
|
||||
INITBITS();
|
||||
|
||||
/* copy stored block from input to output */
|
||||
while (state->length != 0) {
|
||||
while (state->length != 0)
|
||||
{
|
||||
copy = state->length;
|
||||
PULL();
|
||||
ROOM();
|
||||
|
||||
if (copy > have) copy = have;
|
||||
|
||||
if (copy > left) copy = left;
|
||||
|
||||
zmemcpy(put, next, copy);
|
||||
have -= copy;
|
||||
next += copy;
|
||||
|
@ -380,6 +415,7 @@ void FAR *out_desc;
|
|||
put += copy;
|
||||
state->length -= copy;
|
||||
}
|
||||
|
||||
Tracev((stderr, "inflate: stored end\n"));
|
||||
state->mode = TYPE;
|
||||
break;
|
||||
|
@ -394,79 +430,106 @@ void FAR *out_desc;
|
|||
state->ncode = BITS(4) + 4;
|
||||
DROPBITS(4);
|
||||
#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";
|
||||
state->mode = BAD;
|
||||
break;
|
||||
}
|
||||
|
||||
#endif
|
||||
Tracev((stderr, "inflate: table sizes ok\n"));
|
||||
|
||||
/* get code length code lengths (not a typo) */
|
||||
state->have = 0;
|
||||
while (state->have < state->ncode) {
|
||||
|
||||
while (state->have < state->ncode)
|
||||
{
|
||||
NEEDBITS(3);
|
||||
state->lens[order[state->have++]] = (unsigned short)BITS(3);
|
||||
DROPBITS(3);
|
||||
}
|
||||
|
||||
while (state->have < 19)
|
||||
state->lens[order[state->have++]] = 0;
|
||||
|
||||
state->next = state->codes;
|
||||
state->lencode = (code const FAR*)(state->next);
|
||||
state->lenbits = 7;
|
||||
ret = inflate_table(CODES, state->lens, 19, &(state->next),
|
||||
&(state->lenbits), state->work);
|
||||
if (ret) {
|
||||
|
||||
if (ret)
|
||||
{
|
||||
strm->msg = (char*)"invalid code lengths set";
|
||||
state->mode = BAD;
|
||||
break;
|
||||
}
|
||||
|
||||
Tracev((stderr, "inflate: code lengths ok\n"));
|
||||
|
||||
/* get length and distance code code lengths */
|
||||
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)];
|
||||
|
||||
if ((unsigned)(here.bits) <= bits) break;
|
||||
|
||||
PULLBYTE();
|
||||
}
|
||||
if (here.val < 16) {
|
||||
|
||||
if (here.val < 16)
|
||||
{
|
||||
DROPBITS(here.bits);
|
||||
state->lens[state->have++] = here.val;
|
||||
}
|
||||
else {
|
||||
if (here.val == 16) {
|
||||
else
|
||||
{
|
||||
if (here.val == 16)
|
||||
{
|
||||
NEEDBITS(here.bits + 2);
|
||||
DROPBITS(here.bits);
|
||||
if (state->have == 0) {
|
||||
|
||||
if (state->have == 0)
|
||||
{
|
||||
strm->msg = (char*)"invalid bit length repeat";
|
||||
state->mode = BAD;
|
||||
break;
|
||||
}
|
||||
|
||||
len = (unsigned)(state->lens[state->have - 1]);
|
||||
copy = 3 + BITS(2);
|
||||
DROPBITS(2);
|
||||
}
|
||||
else if (here.val == 17) {
|
||||
else if (here.val == 17)
|
||||
{
|
||||
NEEDBITS(here.bits + 3);
|
||||
DROPBITS(here.bits);
|
||||
len = 0;
|
||||
copy = 3 + BITS(3);
|
||||
DROPBITS(3);
|
||||
}
|
||||
else {
|
||||
else
|
||||
{
|
||||
NEEDBITS(here.bits + 7);
|
||||
DROPBITS(here.bits);
|
||||
len = 0;
|
||||
copy = 11 + BITS(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";
|
||||
state->mode = BAD;
|
||||
break;
|
||||
}
|
||||
|
||||
while (copy--)
|
||||
state->lens[state->have++] = (unsigned short)len;
|
||||
}
|
||||
|
@ -476,7 +539,8 @@ void FAR *out_desc;
|
|||
if (state->mode == BAD) break;
|
||||
|
||||
/* 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";
|
||||
state->mode = BAD;
|
||||
break;
|
||||
|
@ -490,55 +554,77 @@ void FAR *out_desc;
|
|||
state->lenbits = 9;
|
||||
ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
|
||||
&(state->lenbits), state->work);
|
||||
if (ret) {
|
||||
|
||||
if (ret)
|
||||
{
|
||||
strm->msg = (char*)"invalid literal/lengths set";
|
||||
state->mode = BAD;
|
||||
break;
|
||||
}
|
||||
|
||||
state->distcode = (code const FAR*)(state->next);
|
||||
state->distbits = 6;
|
||||
ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
|
||||
&(state->next), &(state->distbits), state->work);
|
||||
if (ret) {
|
||||
|
||||
if (ret)
|
||||
{
|
||||
strm->msg = (char*)"invalid distances set";
|
||||
state->mode = BAD;
|
||||
break;
|
||||
}
|
||||
|
||||
Tracev((stderr, "inflate: codes ok\n"));
|
||||
state->mode = LEN;
|
||||
|
||||
case LEN:
|
||||
|
||||
/* use inflate_fast() if we have enough input and output */
|
||||
if (have >= 6 && left >= 258) {
|
||||
if (have >= 6 && left >= 258)
|
||||
{
|
||||
RESTORE();
|
||||
|
||||
if (state->whave < state->wsize)
|
||||
state->whave = state->wsize - left;
|
||||
|
||||
inflate_fast(strm, state->wsize);
|
||||
LOAD();
|
||||
break;
|
||||
}
|
||||
|
||||
/* get a literal, length, or end-of-block code */
|
||||
for (;;) {
|
||||
for (;;)
|
||||
{
|
||||
here = state->lencode[BITS(state->lenbits)];
|
||||
|
||||
if ((unsigned)(here.bits) <= bits) break;
|
||||
|
||||
PULLBYTE();
|
||||
}
|
||||
if (here.op && (here.op & 0xf0) == 0) {
|
||||
|
||||
if (here.op && (here.op & 0xf0) == 0)
|
||||
{
|
||||
last = here;
|
||||
for (;;) {
|
||||
|
||||
for (;;)
|
||||
{
|
||||
here = state->lencode[last.val +
|
||||
(BITS(last.bits + last.op) >> last.bits)];
|
||||
|
||||
if ((unsigned)(last.bits + here.bits) <= bits) break;
|
||||
|
||||
PULLBYTE();
|
||||
}
|
||||
|
||||
DROPBITS(last.bits);
|
||||
}
|
||||
|
||||
DROPBITS(here.bits);
|
||||
state->length = (unsigned)here.val;
|
||||
|
||||
/* process literal */
|
||||
if (here.op == 0) {
|
||||
if (here.op == 0)
|
||||
{
|
||||
Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
|
||||
"inflate: literal '%c'\n" :
|
||||
"inflate: literal 0x%02x\n", here.val));
|
||||
|
@ -550,14 +636,16 @@ void FAR *out_desc;
|
|||
}
|
||||
|
||||
/* process end of block */
|
||||
if (here.op & 32) {
|
||||
if (here.op & 32)
|
||||
{
|
||||
Tracevv((stderr, "inflate: end of block\n"));
|
||||
state->mode = TYPE;
|
||||
break;
|
||||
}
|
||||
|
||||
/* invalid code */
|
||||
if (here.op & 64) {
|
||||
if (here.op & 64)
|
||||
{
|
||||
strm->msg = (char*)"invalid literal/length code";
|
||||
state->mode = BAD;
|
||||
break;
|
||||
|
@ -565,80 +653,116 @@ void FAR *out_desc;
|
|||
|
||||
/* length code -- get extra bits, if any */
|
||||
state->extra = (unsigned)(here.op) & 15;
|
||||
if (state->extra != 0) {
|
||||
|
||||
if (state->extra != 0)
|
||||
{
|
||||
NEEDBITS(state->extra);
|
||||
state->length += BITS(state->extra);
|
||||
DROPBITS(state->extra);
|
||||
}
|
||||
|
||||
Tracevv((stderr, "inflate: length %u\n", state->length));
|
||||
|
||||
/* get distance code */
|
||||
for (;;) {
|
||||
for (;;)
|
||||
{
|
||||
here = state->distcode[BITS(state->distbits)];
|
||||
|
||||
if ((unsigned)(here.bits) <= bits) break;
|
||||
|
||||
PULLBYTE();
|
||||
}
|
||||
if ((here.op & 0xf0) == 0) {
|
||||
|
||||
if ((here.op & 0xf0) == 0)
|
||||
{
|
||||
last = here;
|
||||
for (;;) {
|
||||
|
||||
for (;;)
|
||||
{
|
||||
here = state->distcode[last.val +
|
||||
(BITS(last.bits + last.op) >> last.bits)];
|
||||
|
||||
if ((unsigned)(last.bits + here.bits) <= bits) break;
|
||||
|
||||
PULLBYTE();
|
||||
}
|
||||
|
||||
DROPBITS(last.bits);
|
||||
}
|
||||
|
||||
DROPBITS(here.bits);
|
||||
if (here.op & 64) {
|
||||
|
||||
if (here.op & 64)
|
||||
{
|
||||
strm->msg = (char*)"invalid distance code";
|
||||
state->mode = BAD;
|
||||
break;
|
||||
}
|
||||
|
||||
state->offset = (unsigned)here.val;
|
||||
|
||||
/* get distance extra bits, if any */
|
||||
state->extra = (unsigned)(here.op) & 15;
|
||||
if (state->extra != 0) {
|
||||
|
||||
if (state->extra != 0)
|
||||
{
|
||||
NEEDBITS(state->extra);
|
||||
state->offset += BITS(state->extra);
|
||||
DROPBITS(state->extra);
|
||||
}
|
||||
|
||||
if (state->offset > state->wsize - (state->whave < state->wsize ?
|
||||
left : 0)) {
|
||||
left : 0))
|
||||
{
|
||||
strm->msg = (char*)"invalid distance too far back";
|
||||
state->mode = BAD;
|
||||
break;
|
||||
}
|
||||
|
||||
Tracevv((stderr, "inflate: distance %u\n", state->offset));
|
||||
|
||||
/* copy match from window to output */
|
||||
do {
|
||||
do
|
||||
{
|
||||
ROOM();
|
||||
copy = state->wsize - state->offset;
|
||||
if (copy < left) {
|
||||
|
||||
if (copy < left)
|
||||
{
|
||||
from = put + copy;
|
||||
copy = left - copy;
|
||||
}
|
||||
else {
|
||||
else
|
||||
{
|
||||
from = put - state->offset;
|
||||
copy = left;
|
||||
}
|
||||
|
||||
if (copy > state->length) copy = state->length;
|
||||
|
||||
state->length -= copy;
|
||||
left -= copy;
|
||||
do {
|
||||
|
||||
do
|
||||
{
|
||||
*put++ = *from++;
|
||||
} while (--copy);
|
||||
} while (state->length != 0);
|
||||
}
|
||||
while (--copy);
|
||||
}
|
||||
while (state->length != 0);
|
||||
|
||||
break;
|
||||
|
||||
case DONE:
|
||||
/* inflate stream terminated properly -- write leftover output */
|
||||
ret = Z_STREAM_END;
|
||||
if (left < state->wsize) {
|
||||
|
||||
if (left < state->wsize)
|
||||
{
|
||||
if (out(out_desc, state->window, state->wsize - left))
|
||||
ret = Z_BUF_ERROR;
|
||||
}
|
||||
|
||||
goto inf_leave;
|
||||
|
||||
case BAD:
|
||||
|
@ -666,6 +790,7 @@ z_streamp strm;
|
|||
{
|
||||
if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0)
|
||||
return Z_STREAM_ERROR;
|
||||
|
||||
ZFREE(strm, strm->state);
|
||||
strm->state = Z_NULL;
|
||||
Tracev((stderr, "inflate: end\n"));
|
||||
|
|
|
@ -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
|
||||
input data or output space */
|
||||
do {
|
||||
if (bits < 15) {
|
||||
do
|
||||
{
|
||||
if (bits < 15)
|
||||
{
|
||||
hold += (unsigned long)(PUP(in)) << bits;
|
||||
bits += 8;
|
||||
hold += (unsigned long)(PUP(in)) << bits;
|
||||
bits += 8;
|
||||
}
|
||||
|
||||
here = lcode[hold & lmask];
|
||||
dolen:
|
||||
op = (unsigned)(here.bits);
|
||||
hold >>= op;
|
||||
bits -= op;
|
||||
op = (unsigned)(here.op);
|
||||
if (op == 0) { /* literal */
|
||||
|
||||
if (op == 0) /* literal */
|
||||
{
|
||||
Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
|
||||
"inflate: literal '%c'\n" :
|
||||
"inflate: literal 0x%02x\n", here.val));
|
||||
PUP(out) = (unsigned char)(here.val);
|
||||
}
|
||||
else if (op & 16) { /* length base */
|
||||
else if (op & 16) /* length base */
|
||||
{
|
||||
len = (unsigned)(here.val);
|
||||
op &= 15; /* number of extra bits */
|
||||
if (op) {
|
||||
if (bits < op) {
|
||||
|
||||
if (op)
|
||||
{
|
||||
if (bits < op)
|
||||
{
|
||||
hold += (unsigned long)(PUP(in)) << bits;
|
||||
bits += 8;
|
||||
}
|
||||
|
||||
len += (unsigned)hold & ((1U << op) - 1);
|
||||
hold >>= op;
|
||||
bits -= op;
|
||||
}
|
||||
|
||||
Tracevv((stderr, "inflate: length %u\n", len));
|
||||
if (bits < 15) {
|
||||
|
||||
if (bits < 15)
|
||||
{
|
||||
hold += (unsigned long)(PUP(in)) << bits;
|
||||
bits += 8;
|
||||
hold += (unsigned long)(PUP(in)) << bits;
|
||||
bits += 8;
|
||||
}
|
||||
|
||||
here = dcode[hold & dmask];
|
||||
dodist:
|
||||
op = (unsigned)(here.bits);
|
||||
hold >>= op;
|
||||
bits -= op;
|
||||
op = (unsigned)(here.op);
|
||||
if (op & 16) { /* distance base */
|
||||
|
||||
if (op & 16) /* distance base */
|
||||
{
|
||||
dist = (unsigned)(here.val);
|
||||
op &= 15; /* number of extra bits */
|
||||
if (bits < op) {
|
||||
|
||||
if (bits < op)
|
||||
{
|
||||
hold += (unsigned long)(PUP(in)) << bits;
|
||||
bits += 8;
|
||||
if (bits < op) {
|
||||
|
||||
if (bits < op)
|
||||
{
|
||||
hold += (unsigned long)(PUP(in)) << bits;
|
||||
bits += 8;
|
||||
}
|
||||
}
|
||||
|
||||
dist += (unsigned)hold & ((1U << op) - 1);
|
||||
#ifdef INFLATE_STRICT
|
||||
if (dist > dmax) {
|
||||
|
||||
if (dist > dmax)
|
||||
{
|
||||
strm->msg = (char*)"invalid distance too far back";
|
||||
state->mode = BAD;
|
||||
break;
|
||||
}
|
||||
|
||||
#endif
|
||||
hold >>= op;
|
||||
bits -= op;
|
||||
Tracevv((stderr, "inflate: distance %u\n", dist));
|
||||
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 */
|
||||
if (op > whave) {
|
||||
if (state->sane) {
|
||||
|
||||
if (op > whave)
|
||||
{
|
||||
if (state->sane)
|
||||
{
|
||||
strm->msg =
|
||||
(char*)"invalid distance too far back";
|
||||
state->mode = BAD;
|
||||
break;
|
||||
}
|
||||
|
||||
#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
|
||||
if (len <= op - whave) {
|
||||
do {
|
||||
|
||||
if (len <= op - whave)
|
||||
{
|
||||
do
|
||||
{
|
||||
PUP(out) = 0;
|
||||
} while (--len);
|
||||
}
|
||||
while (--len);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
len -= op - whave;
|
||||
do {
|
||||
|
||||
do
|
||||
{
|
||||
PUP(out) = 0;
|
||||
} while (--op > whave);
|
||||
if (op == 0) {
|
||||
}
|
||||
while (--op > whave);
|
||||
|
||||
if (op == 0)
|
||||
{
|
||||
from = out - dist;
|
||||
do {
|
||||
|
||||
do
|
||||
{
|
||||
PUP(out) = PUP(from);
|
||||
} while (--len);
|
||||
}
|
||||
while (--len);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
from = window - OFF;
|
||||
if (wnext == 0) { /* very common case */
|
||||
|
||||
if (wnext == 0) /* very common case */
|
||||
{
|
||||
from += wsize - op;
|
||||
if (op < len) { /* some from window */
|
||||
|
||||
if (op < len) /* some from window */
|
||||
{
|
||||
len -= op;
|
||||
do {
|
||||
|
||||
do
|
||||
{
|
||||
PUP(out) = PUP(from);
|
||||
} while (--op);
|
||||
}
|
||||
while (--op);
|
||||
|
||||
from = out - dist; /* rest from output */
|
||||
}
|
||||
}
|
||||
else if (wnext < op) { /* wrap around window */
|
||||
else if (wnext < op) /* wrap around window */
|
||||
{
|
||||
from += wsize + wnext - op;
|
||||
op -= wnext;
|
||||
if (op < len) { /* some from end of window */
|
||||
|
||||
if (op < len) /* some from end of window */
|
||||
{
|
||||
len -= op;
|
||||
do {
|
||||
|
||||
do
|
||||
{
|
||||
PUP(out) = PUP(from);
|
||||
} while (--op);
|
||||
}
|
||||
while (--op);
|
||||
|
||||
from = window - OFF;
|
||||
if (wnext < len) { /* some from start of window */
|
||||
|
||||
if (wnext < len) /* some from start of window */
|
||||
{
|
||||
op = wnext;
|
||||
len -= op;
|
||||
do {
|
||||
|
||||
do
|
||||
{
|
||||
PUP(out) = PUP(from);
|
||||
} while (--op);
|
||||
}
|
||||
while (--op);
|
||||
|
||||
from = out - dist; /* rest from output */
|
||||
}
|
||||
}
|
||||
}
|
||||
else { /* contiguous in window */
|
||||
else /* contiguous in window */
|
||||
{
|
||||
from += wnext - op;
|
||||
if (op < len) { /* some from window */
|
||||
|
||||
if (op < len) /* some from window */
|
||||
{
|
||||
len -= op;
|
||||
do {
|
||||
|
||||
do
|
||||
{
|
||||
PUP(out) = PUP(from);
|
||||
} while (--op);
|
||||
}
|
||||
while (--op);
|
||||
|
||||
from = out - dist; /* rest from output */
|
||||
}
|
||||
}
|
||||
while (len > 2) {
|
||||
|
||||
while (len > 2)
|
||||
{
|
||||
PUP(out) = PUP(from);
|
||||
PUP(out) = PUP(from);
|
||||
PUP(out) = PUP(from);
|
||||
len -= 3;
|
||||
}
|
||||
if (len) {
|
||||
|
||||
if (len)
|
||||
{
|
||||
PUP(out) = PUP(from);
|
||||
|
||||
if (len > 1)
|
||||
PUP(out) = PUP(from);
|
||||
}
|
||||
}
|
||||
else {
|
||||
else
|
||||
{
|
||||
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);
|
||||
len -= 3;
|
||||
} while (len > 2);
|
||||
if (len) {
|
||||
}
|
||||
while (len > 2);
|
||||
|
||||
if (len)
|
||||
{
|
||||
PUP(out) = PUP(from);
|
||||
|
||||
if (len > 1)
|
||||
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))];
|
||||
goto dodist;
|
||||
}
|
||||
else {
|
||||
else
|
||||
{
|
||||
strm->msg = (char*)"invalid distance code";
|
||||
state->mode = BAD;
|
||||
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))];
|
||||
goto dolen;
|
||||
}
|
||||
else if (op & 32) { /* end-of-block */
|
||||
else if (op & 32) /* end-of-block */
|
||||
{
|
||||
Tracevv((stderr, "inflate: end of block\n"));
|
||||
state->mode = TYPE;
|
||||
break;
|
||||
}
|
||||
else {
|
||||
else
|
||||
{
|
||||
strm->msg = (char*)"invalid literal/length code";
|
||||
state->mode = BAD;
|
||||
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) */
|
||||
len = bits >> 3;
|
||||
|
|
|
@ -7,7 +7,8 @@
|
|||
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},
|
||||
{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},
|
||||
|
@ -84,7 +85,8 @@
|
|||
{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},
|
||||
{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},
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -17,7 +17,8 @@
|
|||
#endif
|
||||
|
||||
/* Possible inflate modes between inflate() calls */
|
||||
typedef enum {
|
||||
typedef enum
|
||||
{
|
||||
HEAD, /* i: waiting for magic header */
|
||||
FLAGS, /* i: waiting for method and flags (gzip) */
|
||||
TIME, /* i: waiting for modification time (gzip) */
|
||||
|
@ -78,7 +79,8 @@ typedef enum {
|
|||
*/
|
||||
|
||||
/* state maintained between inflate() calls. Approximately 10K bytes. */
|
||||
struct inflate_state {
|
||||
struct inflate_state
|
||||
{
|
||||
inflate_mode mode; /* current inflate mode */
|
||||
int last; /* true if processing last block */
|
||||
int wrap; /* bit 0 true for zlib, bit 1 true for gzip */
|
||||
|
|
|
@ -74,20 +74,28 @@ unsigned short FAR *work;
|
|||
int end; /* use base and extra for symbol > end */
|
||||
unsigned short count[MAXBITS + 1]; /* number of codes of 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,
|
||||
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 */
|
||||
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 */
|
||||
{
|
||||
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};
|
||||
static const unsigned short dbase[32] = { /* Distance codes 0..29 base */
|
||||
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 */
|
||||
{
|
||||
1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
|
||||
257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
|
||||
8193, 12289, 16385, 24577, 0, 0};
|
||||
static const unsigned short dext[32] = { /* Distance codes 0..29 extra */
|
||||
8193, 12289, 16385, 24577, 0, 0
|
||||
};
|
||||
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,
|
||||
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
|
||||
|
@ -123,15 +131,20 @@ unsigned short FAR *work;
|
|||
/* accumulate lengths for codes (assumes lens[] all in 0..MAXBITS) */
|
||||
for (len = 0; len <= MAXBITS; len++)
|
||||
count[len] = 0;
|
||||
|
||||
for (sym = 0; sym < codes; sym++)
|
||||
count[lens[sym]]++;
|
||||
|
||||
/* bound code lengths, force root to be within code lengths */
|
||||
root = *bits;
|
||||
|
||||
for (max = MAXBITS; max >= 1; max--)
|
||||
if (count[max] != 0) break;
|
||||
|
||||
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.bits = (unsigned char)1;
|
||||
here.val = (unsigned short)0;
|
||||
|
@ -140,22 +153,29 @@ unsigned short FAR *work;
|
|||
*bits = 1;
|
||||
return 0; /* no symbols, but wait for decoding to report error */
|
||||
}
|
||||
|
||||
for (min = 1; min < max; min++)
|
||||
if (count[min] != 0) break;
|
||||
|
||||
if (root < min) root = min;
|
||||
|
||||
/* check for an over-subscribed or incomplete set of lengths */
|
||||
left = 1;
|
||||
for (len = 1; len <= MAXBITS; len++) {
|
||||
|
||||
for (len = 1; len <= MAXBITS; len++)
|
||||
{
|
||||
left <<= 1;
|
||||
left -= count[len];
|
||||
|
||||
if (left < 0) return -1; /* over-subscribed */
|
||||
}
|
||||
|
||||
if (left > 0 && (type == CODES || max != 1))
|
||||
return -1; /* incomplete set */
|
||||
|
||||
/* generate offsets into symbol table for each length for sorting */
|
||||
offs[1] = 0;
|
||||
|
||||
for (len = 1; len < MAXBITS; len++)
|
||||
offs[len + 1] = offs[len] + count[len];
|
||||
|
||||
|
@ -195,11 +215,13 @@ unsigned short FAR *work;
|
|||
*/
|
||||
|
||||
/* set up for code type */
|
||||
switch (type) {
|
||||
switch (type)
|
||||
{
|
||||
case CODES:
|
||||
base = extra = work; /* dummy value--not used */
|
||||
end = 19;
|
||||
break;
|
||||
|
||||
case LENS:
|
||||
base = lbase;
|
||||
base -= 257;
|
||||
|
@ -207,6 +229,7 @@ unsigned short FAR *work;
|
|||
extra -= 257;
|
||||
end = 256;
|
||||
break;
|
||||
|
||||
default: /* DISTS */
|
||||
base = dbase;
|
||||
extra = dext;
|
||||
|
@ -230,18 +253,23 @@ unsigned short FAR *work;
|
|||
return 1;
|
||||
|
||||
/* process all codes and make table entries */
|
||||
for (;;) {
|
||||
for (;;)
|
||||
{
|
||||
/* create table entry */
|
||||
here.bits = (unsigned char)(len - drop);
|
||||
if ((int)(work[sym]) < end) {
|
||||
|
||||
if ((int)(work[sym]) < end)
|
||||
{
|
||||
here.op = (unsigned char)0;
|
||||
here.val = work[sym];
|
||||
}
|
||||
else if ((int)(work[sym]) > end) {
|
||||
else if ((int)(work[sym]) > end)
|
||||
{
|
||||
here.op = (unsigned char)(extra[work[sym]]);
|
||||
here.val = base[work[sym]];
|
||||
}
|
||||
else {
|
||||
else
|
||||
{
|
||||
here.op = (unsigned char)(32 + 64); /* end of block */
|
||||
here.val = 0;
|
||||
}
|
||||
|
@ -250,16 +278,22 @@ unsigned short FAR *work;
|
|||
incr = 1U << (len - drop);
|
||||
fill = 1U << curr;
|
||||
min = fill; /* save offset to next table */
|
||||
do {
|
||||
|
||||
do
|
||||
{
|
||||
fill -= incr;
|
||||
next[(huff >> drop) + fill] = here;
|
||||
} while (fill != 0);
|
||||
}
|
||||
while (fill != 0);
|
||||
|
||||
/* backwards increment the len-bit code huff */
|
||||
incr = 1U << (len - 1);
|
||||
|
||||
while (huff & incr)
|
||||
incr >>= 1;
|
||||
if (incr != 0) {
|
||||
|
||||
if (incr != 0)
|
||||
{
|
||||
huff &= incr - 1;
|
||||
huff += incr;
|
||||
}
|
||||
|
@ -268,13 +302,17 @@ unsigned short FAR *work;
|
|||
|
||||
/* go to next symbol, update count, len */
|
||||
sym++;
|
||||
if (--(count[len]) == 0) {
|
||||
|
||||
if (--(count[len]) == 0)
|
||||
{
|
||||
if (len == max) break;
|
||||
|
||||
len = lens[work[sym]];
|
||||
}
|
||||
|
||||
/* 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 (drop == 0)
|
||||
drop = root;
|
||||
|
@ -285,15 +323,20 @@ unsigned short FAR *work;
|
|||
/* determine length of next table */
|
||||
curr = len - drop;
|
||||
left = (int)(1 << curr);
|
||||
while (curr + drop < max) {
|
||||
|
||||
while (curr + drop < max)
|
||||
{
|
||||
left -= count[curr + drop];
|
||||
|
||||
if (left <= 0) break;
|
||||
|
||||
curr++;
|
||||
left <<= 1;
|
||||
}
|
||||
|
||||
/* check for enough space */
|
||||
used += 1U << curr;
|
||||
|
||||
if ((type == LENS && used >= ENOUGH_LENS) ||
|
||||
(type == DISTS && used >= ENOUGH_DISTS))
|
||||
return 1;
|
||||
|
@ -309,7 +352,8 @@ unsigned short FAR *work;
|
|||
/* fill in remaining table entry if code is incomplete (guaranteed to have
|
||||
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) */
|
||||
if (huff != 0) {
|
||||
if (huff != 0)
|
||||
{
|
||||
here.op = (unsigned char)64; /* invalid code marker */
|
||||
here.bits = (unsigned char)(len - drop);
|
||||
here.val = (unsigned short)0;
|
||||
|
|
|
@ -21,7 +21,8 @@
|
|||
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
|
||||
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 bits; /* bits in this part of the code */
|
||||
unsigned short val; /* offset in table or code value */
|
||||
|
@ -51,7 +52,8 @@ typedef struct {
|
|||
#define ENOUGH (ENOUGH_LENS+ENOUGH_DISTS)
|
||||
|
||||
/* Type of code to build for inflate_table() */
|
||||
typedef enum {
|
||||
typedef enum
|
||||
{
|
||||
CODES,
|
||||
LENS,
|
||||
DISTS
|
||||
|
|
|
@ -100,10 +100,13 @@ void test_compress(compr, comprLen, uncompr, uncomprLen)
|
|||
err = uncompress(uncompr, &uncomprLen, compr, comprLen);
|
||||
CHECK_ERR(err, "uncompress");
|
||||
|
||||
if (strcmp((char*)uncompr, hello)) {
|
||||
if (strcmp((char*)uncompr, hello))
|
||||
{
|
||||
fprintf(stderr, "bad uncompress\n");
|
||||
exit(1);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("uncompress(): %s\n", (char*)uncompr);
|
||||
}
|
||||
}
|
||||
|
@ -125,66 +128,92 @@ void test_gzio(fname, uncompr, uncomprLen)
|
|||
z_off_t pos;
|
||||
|
||||
file = gzopen(fname, "wb");
|
||||
if (file == NULL) {
|
||||
|
||||
if (file == NULL)
|
||||
{
|
||||
fprintf(stderr, "gzopen error\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
gzputc(file, 'h');
|
||||
if (gzputs(file, "ello") != 4) {
|
||||
|
||||
if (gzputs(file, "ello") != 4)
|
||||
{
|
||||
fprintf(stderr, "gzputs err: %s\n", gzerror(file, &err));
|
||||
exit(1);
|
||||
}
|
||||
if (gzprintf(file, ", %s!", "hello") != 8) {
|
||||
|
||||
if (gzprintf(file, ", %s!", "hello") != 8)
|
||||
{
|
||||
fprintf(stderr, "gzprintf err: %s\n", gzerror(file, &err));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
gzseek(file, 1L, SEEK_CUR); /* add one zero byte */
|
||||
gzclose(file);
|
||||
|
||||
file = gzopen(fname, "rb");
|
||||
if (file == NULL) {
|
||||
|
||||
if (file == NULL)
|
||||
{
|
||||
fprintf(stderr, "gzopen error\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
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));
|
||||
exit(1);
|
||||
}
|
||||
if (strcmp((char*)uncompr, hello)) {
|
||||
|
||||
if (strcmp((char*)uncompr, hello))
|
||||
{
|
||||
fprintf(stderr, "bad gzread: %s\n", (char*)uncompr);
|
||||
exit(1);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("gzread(): %s\n", (char*)uncompr);
|
||||
}
|
||||
|
||||
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",
|
||||
(long)pos, (long)gztell(file));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (gzgetc(file) != ' ') {
|
||||
if (gzgetc(file) != ' ')
|
||||
{
|
||||
fprintf(stderr, "gzgetc error\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (gzungetc(' ', file) != ' ') {
|
||||
if (gzungetc(' ', file) != ' ')
|
||||
{
|
||||
fprintf(stderr, "gzungetc error\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
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));
|
||||
exit(1);
|
||||
}
|
||||
if (strcmp((char*)uncompr, hello + 6)) {
|
||||
|
||||
if (strcmp((char*)uncompr, hello + 6))
|
||||
{
|
||||
fprintf(stderr, "bad gzgets after gzseek\n");
|
||||
exit(1);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
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_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 */
|
||||
err = deflate(&c_stream, Z_NO_FLUSH);
|
||||
CHECK_ERR(err, "deflate");
|
||||
}
|
||||
|
||||
/* Finish the stream, still forcing small buffers: */
|
||||
for (;;) {
|
||||
for (;;)
|
||||
{
|
||||
c_stream.avail_out = 1;
|
||||
err = deflate(&c_stream, Z_FINISH);
|
||||
|
||||
if (err == Z_STREAM_END) break;
|
||||
|
||||
CHECK_ERR(err, "deflate");
|
||||
}
|
||||
|
||||
|
@ -255,20 +289,26 @@ void test_inflate(compr, comprLen, uncompr, uncomprLen)
|
|||
err = inflateInit(&d_stream);
|
||||
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 */
|
||||
err = inflate(&d_stream, Z_NO_FLUSH);
|
||||
|
||||
if (err == Z_STREAM_END) break;
|
||||
|
||||
CHECK_ERR(err, "inflate");
|
||||
}
|
||||
|
||||
err = inflateEnd(&d_stream);
|
||||
CHECK_ERR(err, "inflateEnd");
|
||||
|
||||
if (strcmp((char*)uncompr, hello)) {
|
||||
if (strcmp((char*)uncompr, hello))
|
||||
{
|
||||
fprintf(stderr, "bad inflate\n");
|
||||
exit(1);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("inflate(): %s\n", (char*)uncompr);
|
||||
}
|
||||
}
|
||||
|
@ -300,7 +340,9 @@ void test_large_deflate(compr, comprLen, uncompr, uncomprLen)
|
|||
c_stream.avail_in = (uInt)uncomprLen;
|
||||
err = deflate(&c_stream, Z_NO_FLUSH);
|
||||
CHECK_ERR(err, "deflate");
|
||||
if (c_stream.avail_in != 0) {
|
||||
|
||||
if (c_stream.avail_in != 0)
|
||||
{
|
||||
fprintf(stderr, "deflate not greedy\n");
|
||||
exit(1);
|
||||
}
|
||||
|
@ -320,10 +362,13 @@ void test_large_deflate(compr, comprLen, uncompr, uncomprLen)
|
|||
CHECK_ERR(err, "deflate");
|
||||
|
||||
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");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
err = deflateEnd(&c_stream);
|
||||
CHECK_ERR(err, "deflateEnd");
|
||||
}
|
||||
|
@ -350,21 +395,27 @@ void test_large_inflate(compr, comprLen, uncompr, uncomprLen)
|
|||
err = inflateInit(&d_stream);
|
||||
CHECK_ERR(err, "inflateInit");
|
||||
|
||||
for (;;) {
|
||||
for (;;)
|
||||
{
|
||||
d_stream.next_out = uncompr; /* discard the output */
|
||||
d_stream.avail_out = (uInt)uncomprLen;
|
||||
err = inflate(&d_stream, Z_NO_FLUSH);
|
||||
|
||||
if (err == Z_STREAM_END) break;
|
||||
|
||||
CHECK_ERR(err, "large inflate");
|
||||
}
|
||||
|
||||
err = inflateEnd(&d_stream);
|
||||
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);
|
||||
exit(1);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("large_inflate(): OK\n");
|
||||
}
|
||||
}
|
||||
|
@ -398,9 +449,12 @@ void test_flush(compr, comprLen)
|
|||
c_stream.avail_in = len - 3;
|
||||
|
||||
err = deflate(&c_stream, Z_FINISH);
|
||||
if (err != Z_STREAM_END) {
|
||||
|
||||
if (err != Z_STREAM_END)
|
||||
{
|
||||
CHECK_ERR(err, "deflate");
|
||||
}
|
||||
|
||||
err = deflateEnd(&c_stream);
|
||||
CHECK_ERR(err, "deflateEnd");
|
||||
|
||||
|
@ -440,11 +494,14 @@ void test_sync(compr, comprLen, uncompr, uncomprLen)
|
|||
CHECK_ERR(err, "inflateSync");
|
||||
|
||||
err = inflate(&d_stream, Z_FINISH);
|
||||
if (err != Z_DATA_ERROR) {
|
||||
|
||||
if (err != Z_DATA_ERROR)
|
||||
{
|
||||
fprintf(stderr, "inflate should report DATA_ERROR\n");
|
||||
/* Because of incorrect adler32 */
|
||||
exit(1);
|
||||
}
|
||||
|
||||
err = inflateEnd(&d_stream);
|
||||
CHECK_ERR(err, "inflateEnd");
|
||||
|
||||
|
@ -480,10 +537,13 @@ void test_dict_deflate(compr, comprLen)
|
|||
c_stream.avail_in = (uInt)strlen(hello) + 1;
|
||||
|
||||
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");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
err = deflateEnd(&c_stream);
|
||||
CHECK_ERR(err, "deflateEnd");
|
||||
}
|
||||
|
@ -513,27 +573,37 @@ void test_dict_inflate(compr, comprLen, uncompr, uncomprLen)
|
|||
d_stream.next_out = uncompr;
|
||||
d_stream.avail_out = (uInt)uncomprLen;
|
||||
|
||||
for (;;) {
|
||||
for (;;)
|
||||
{
|
||||
err = inflate(&d_stream, Z_NO_FLUSH);
|
||||
|
||||
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");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
err = inflateSetDictionary(&d_stream, (const Bytef*)dictionary,
|
||||
(int)sizeof(dictionary));
|
||||
}
|
||||
|
||||
CHECK_ERR(err, "inflate with dict");
|
||||
}
|
||||
|
||||
err = inflateEnd(&d_stream);
|
||||
CHECK_ERR(err, "inflateEnd");
|
||||
|
||||
if (strcmp((char*)uncompr, hello)) {
|
||||
if (strcmp((char*)uncompr, hello))
|
||||
{
|
||||
fprintf(stderr, "bad inflate with dict\n");
|
||||
exit(1);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("inflate with dictionary: %s\n", (char*)uncompr);
|
||||
}
|
||||
}
|
||||
|
@ -551,11 +621,14 @@ int main(argc, argv)
|
|||
uLong uncomprLen = comprLen;
|
||||
static const char* myVersion = ZLIB_VERSION;
|
||||
|
||||
if (zlibVersion()[0] != myVersion[0]) {
|
||||
if (zlibVersion()[0] != myVersion[0])
|
||||
{
|
||||
fprintf(stderr, "incompatible zlib version\n");
|
||||
exit(1);
|
||||
|
||||
} else if (strcmp(zlibVersion(), ZLIB_VERSION) != 0) {
|
||||
}
|
||||
else if (strcmp(zlibVersion(), ZLIB_VERSION) != 0)
|
||||
{
|
||||
fprintf(stderr, "warning: different zlib version\n");
|
||||
}
|
||||
|
||||
|
@ -564,10 +637,12 @@ int main(argc, argv)
|
|||
|
||||
compr = (Byte*)calloc((uInt)comprLen, 1);
|
||||
uncompr = (Byte*)calloc((uInt)uncomprLen, 1);
|
||||
|
||||
/* compr and uncompr are cleared to avoid reading uninitialized
|
||||
* 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");
|
||||
exit(1);
|
||||
}
|
||||
|
|
|
@ -53,14 +53,16 @@
|
|||
*/
|
||||
|
||||
/* 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 */
|
||||
size_t size; /* requested size of allocation */
|
||||
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 */
|
||||
struct mem_zone {
|
||||
struct mem_zone
|
||||
{
|
||||
struct mem_item* first; /* pointer to first item in list, or NULL */
|
||||
size_t total, highwater; /* total allocations, and largest total */
|
||||
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
|
||||
non-zero value to make sure that the code isn't depending on zeros */
|
||||
ptr = malloc(len);
|
||||
|
||||
if (ptr == NULL)
|
||||
return NULL;
|
||||
|
||||
memset(ptr, 0xa5, len);
|
||||
|
||||
/* create a new item for the list */
|
||||
item = malloc(sizeof(struct mem_item));
|
||||
if (item == NULL) {
|
||||
|
||||
if (item == NULL)
|
||||
{
|
||||
free(ptr);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
item->ptr = ptr;
|
||||
item->size = len;
|
||||
|
||||
|
@ -101,6 +108,7 @@ local void *mem_alloc(void *mem, unsigned count, unsigned size)
|
|||
|
||||
/* update the statistics */
|
||||
zone->total += item->size;
|
||||
|
||||
if (zone->total > zone->highwater)
|
||||
zone->highwater = zone->total;
|
||||
|
||||
|
@ -115,7 +123,8 @@ local void mem_free(void *mem, void *ptr)
|
|||
struct mem_zone* zone = mem;
|
||||
|
||||
/* if no zone, just do a free */
|
||||
if (zone == NULL) {
|
||||
if (zone == NULL)
|
||||
{
|
||||
free(ptr);
|
||||
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
|
||||
the item from the linked list if found */
|
||||
next = zone->first;
|
||||
if (next) {
|
||||
|
||||
if (next)
|
||||
{
|
||||
if (next->ptr == ptr)
|
||||
zone->first = next->next; /* first one is it, remove from list */
|
||||
else {
|
||||
do { /* search the linked list */
|
||||
else
|
||||
{
|
||||
do /* search the linked list */
|
||||
{
|
||||
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;
|
||||
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 (next) {
|
||||
if (next)
|
||||
{
|
||||
zone->total -= next->size;
|
||||
free(next);
|
||||
}
|
||||
|
@ -208,7 +225,9 @@ local void mem_done(z_stream *strm, char *prefix)
|
|||
|
||||
/* free leftover allocations and item structures, if any */
|
||||
item = zone->first;
|
||||
while (item != NULL) {
|
||||
|
||||
while (item != NULL)
|
||||
{
|
||||
free(item->ptr);
|
||||
next = item->next;
|
||||
free(item);
|
||||
|
@ -220,8 +239,10 @@ local void mem_done(z_stream *strm, char *prefix)
|
|||
if (count || zone->total)
|
||||
fprintf(stderr, "** %s: %lu bytes in %d blocks not freed\n",
|
||||
prefix, zone->total, count);
|
||||
|
||||
if (zone->notlifo)
|
||||
fprintf(stderr, "** %s: %d frees not LIFO\n", prefix, zone->notlifo);
|
||||
|
||||
if (zone->rogue)
|
||||
fprintf(stderr, "** %s: %d frees not recognized\n",
|
||||
prefix, zone->rogue);
|
||||
|
@ -248,11 +269,15 @@ local unsigned char *h2b(const char *hex, unsigned *len)
|
|||
unsigned next, val;
|
||||
|
||||
in = malloc((strlen(hex) + 1) >> 1);
|
||||
|
||||
if (in == NULL)
|
||||
return NULL;
|
||||
|
||||
next = 0;
|
||||
val = 1;
|
||||
do {
|
||||
|
||||
do
|
||||
{
|
||||
if (*hex >= '0' && *hex <= '9')
|
||||
val = (val << 4) + *hex - '0';
|
||||
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;
|
||||
else if (val != 1 && val < 32) /* one digit followed by delimiter */
|
||||
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 */
|
||||
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)
|
||||
*len = next;
|
||||
|
||||
in = reallocf(in, next);
|
||||
return in;
|
||||
}
|
||||
|
@ -294,33 +324,50 @@ local void inf(char *hex, char *what, unsigned step, int win, unsigned len,
|
|||
strm.avail_in = 0;
|
||||
strm.next_in = Z_NULL;
|
||||
ret = inflateInit2(&strm, win);
|
||||
if (ret != Z_OK) {
|
||||
|
||||
if (ret != Z_OK)
|
||||
{
|
||||
mem_done(&strm, what);
|
||||
return;
|
||||
}
|
||||
out = malloc(len); assert(out != NULL);
|
||||
if (win == 47) {
|
||||
|
||||
out = malloc(len);
|
||||
assert(out != NULL);
|
||||
|
||||
if (win == 47)
|
||||
{
|
||||
head.extra = out;
|
||||
head.extra_max = len;
|
||||
head.name = out;
|
||||
head.name_max = len;
|
||||
head.comment = out;
|
||||
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)
|
||||
step = have;
|
||||
|
||||
strm.avail_in = step;
|
||||
have -= step;
|
||||
strm.next_in = in;
|
||||
do {
|
||||
|
||||
do
|
||||
{
|
||||
strm.avail_out = len;
|
||||
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)
|
||||
break;
|
||||
if (ret == Z_NEED_DICT) {
|
||||
|
||||
if (ret == Z_NEED_DICT)
|
||||
{
|
||||
ret = inflateSetDictionary(&strm, in, 1);
|
||||
assert(ret == Z_DATA_ERROR);
|
||||
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;
|
||||
ret = inflateSetDictionary(&strm, out, 0);
|
||||
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(©, &strm); assert(ret == Z_OK);
|
||||
ret = inflateEnd(©); assert(ret == Z_OK);
|
||||
|
||||
ret = inflateCopy(©, &strm);
|
||||
assert(ret == Z_OK);
|
||||
ret = inflateEnd(©);
|
||||
assert(ret == Z_OK);
|
||||
err = 9; /* don't care next time around */
|
||||
have += strm.avail_in;
|
||||
strm.avail_in = step > have ? have : step;
|
||||
have -= strm.avail_in;
|
||||
} while (strm.avail_in);
|
||||
}
|
||||
while (strm.avail_in);
|
||||
|
||||
free(in);
|
||||
free(out);
|
||||
ret = inflateReset2(&strm, -8); assert(ret == Z_OK);
|
||||
ret = inflateEnd(&strm); assert(ret == Z_OK);
|
||||
ret = inflateReset2(&strm, -8);
|
||||
assert(ret == Z_OK);
|
||||
ret = inflateEnd(&strm);
|
||||
assert(ret == Z_OK);
|
||||
mem_done(&strm, what);
|
||||
}
|
||||
|
||||
|
@ -355,13 +410,17 @@ local void cover_support(void)
|
|||
mem_setup(&strm);
|
||||
strm.avail_in = 0;
|
||||
strm.next_in = Z_NULL;
|
||||
ret = inflateInit(&strm); assert(ret == Z_OK);
|
||||
ret = inflateInit(&strm);
|
||||
assert(ret == Z_OK);
|
||||
mem_used(&strm, "inflate init");
|
||||
ret = inflatePrime(&strm, 5, 31); assert(ret == Z_OK);
|
||||
ret = inflatePrime(&strm, -1, 0); assert(ret == Z_OK);
|
||||
ret = inflatePrime(&strm, 5, 31);
|
||||
assert(ret == Z_OK);
|
||||
ret = inflatePrime(&strm, -1, 0);
|
||||
assert(ret == Z_OK);
|
||||
ret = inflateSetDictionary(&strm, Z_NULL, 0);
|
||||
assert(ret == Z_STREAM_ERROR);
|
||||
ret = inflateEnd(&strm); assert(ret == Z_OK);
|
||||
ret = inflateEnd(&strm);
|
||||
assert(ret == Z_OK);
|
||||
mem_done(&strm, "prime");
|
||||
|
||||
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.next_in = Z_NULL;
|
||||
ret = inflateInit(&strm); assert(ret == Z_OK);
|
||||
ret = inflateEnd(&strm); assert(ret == Z_OK);
|
||||
ret = inflateInit(&strm);
|
||||
assert(ret == Z_OK);
|
||||
ret = inflateEnd(&strm);
|
||||
assert(ret == Z_OK);
|
||||
fputs("inflate built-in memory routines\n", stderr);
|
||||
}
|
||||
|
||||
|
@ -391,9 +452,12 @@ local void cover_wrap(void)
|
|||
z_stream strm, copy;
|
||||
unsigned char dict[257];
|
||||
|
||||
ret = inflate(Z_NULL, 0); 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);
|
||||
ret = inflate(Z_NULL, 0);
|
||||
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);
|
||||
|
||||
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.next_out = (void*)&ret;
|
||||
mem_limit(&strm, 1);
|
||||
ret = inflate(&strm, Z_NO_FLUSH); assert(ret == Z_MEM_ERROR);
|
||||
ret = inflate(&strm, Z_NO_FLUSH); assert(ret == Z_MEM_ERROR);
|
||||
ret = inflate(&strm, Z_NO_FLUSH);
|
||||
assert(ret == Z_MEM_ERROR);
|
||||
ret = inflate(&strm, Z_NO_FLUSH);
|
||||
assert(ret == Z_MEM_ERROR);
|
||||
mem_limit(&strm, 0);
|
||||
memset(dict, 0, 257);
|
||||
ret = inflateSetDictionary(&strm, dict, 257);
|
||||
assert(ret == Z_OK);
|
||||
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.next_in = (void*)"\x80";
|
||||
ret = inflateSync(&strm); assert(ret == Z_DATA_ERROR);
|
||||
ret = inflate(&strm, Z_NO_FLUSH); assert(ret == Z_STREAM_ERROR);
|
||||
ret = inflateSync(&strm);
|
||||
assert(ret == Z_DATA_ERROR);
|
||||
ret = inflate(&strm, Z_NO_FLUSH);
|
||||
assert(ret == Z_STREAM_ERROR);
|
||||
strm.avail_in = 4;
|
||||
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);
|
||||
ret = inflateCopy(©, &strm); assert(ret == Z_MEM_ERROR);
|
||||
ret = inflateCopy(©, &strm);
|
||||
assert(ret == Z_MEM_ERROR);
|
||||
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);
|
||||
ret = inflateEnd(&strm); assert(ret == Z_OK);
|
||||
ret = inflateEnd(&strm);
|
||||
assert(ret == Z_OK);
|
||||
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};
|
||||
struct inflate_state* state;
|
||||
|
||||
if (desc == Z_NULL) {
|
||||
if (desc == Z_NULL)
|
||||
{
|
||||
next = 0;
|
||||
return 0; /* no input (already provided at next_in) */
|
||||
}
|
||||
|
||||
state = (void*)((z_stream*)desc)->state;
|
||||
|
||||
if (state != Z_NULL)
|
||||
state->mode = SYNC; /* force an otherwise impossible situation */
|
||||
|
||||
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);
|
||||
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);
|
||||
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);
|
||||
|
||||
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.next_in = (void*)"\x03";
|
||||
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 */
|
||||
ret = inflateBack(&strm, pull, &strm, push, Z_NULL);
|
||||
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");
|
||||
|
||||
ret = inflateBackInit(&strm, 15, win); assert(ret == Z_OK);
|
||||
ret = inflateBackEnd(&strm); assert(ret == Z_OK);
|
||||
ret = inflateBackInit(&strm, 15, win);
|
||||
assert(ret == Z_OK);
|
||||
ret = inflateBackEnd(&strm);
|
||||
assert(ret == Z_OK);
|
||||
fputs("inflateBack built-in memory routines\n", stderr);
|
||||
}
|
||||
|
||||
/* do a raw inflate of data in hexadecimal with both inflate and inflateBack */
|
||||
|
||||
local int try(char* hex, char* id, int err)
|
||||
{
|
||||
int ret;
|
||||
|
@ -535,23 +619,31 @@ local int try(char *hex, char *id, int err)
|
|||
assert(ret == Z_OK);
|
||||
strm.avail_in = len;
|
||||
strm.next_in = in;
|
||||
do {
|
||||
|
||||
do
|
||||
{
|
||||
strm.avail_out = size;
|
||||
strm.next_out = out;
|
||||
ret = inflate(&strm, Z_TREES);
|
||||
assert(ret != Z_STREAM_ERROR && ret != Z_MEM_ERROR);
|
||||
|
||||
if (ret == Z_DATA_ERROR || ret == Z_NEED_DICT)
|
||||
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(strcmp(id, strm.msg) == 0);
|
||||
}
|
||||
|
||||
inflateEnd(&strm);
|
||||
mem_done(&strm, prefix);
|
||||
|
||||
/* then with inflateBack */
|
||||
if (err >= 0) {
|
||||
if (err >= 0)
|
||||
{
|
||||
strcpy(prefix, id);
|
||||
strcat(prefix, "-back");
|
||||
mem_setup(&strm);
|
||||
|
@ -561,10 +653,13 @@ local int try(char *hex, char *id, int err)
|
|||
strm.next_in = in;
|
||||
ret = inflateBack(&strm, pull, Z_NULL, push, Z_NULL);
|
||||
assert(ret != Z_STREAM_ERROR);
|
||||
if (err) {
|
||||
|
||||
if (err)
|
||||
{
|
||||
assert(ret == Z_DATA_ERROR);
|
||||
assert(strcmp(id, strm.msg) == 0);
|
||||
}
|
||||
|
||||
inflateBackEnd(&strm);
|
||||
mem_done(&strm, prefix);
|
||||
}
|
||||
|
@ -581,33 +676,53 @@ local int try(char *hex, char *id, int err)
|
|||
local void cover_inflate(void)
|
||||
{
|
||||
try("0 0 0 0 0", "invalid stored block lengths", 1);
|
||||
|
||||
try("3 0", "fixed", 0);
|
||||
|
||||
try("6", "invalid block type", 1);
|
||||
|
||||
try("1 1 0 fe ff 0", "stored", 0);
|
||||
|
||||
try("fc 0 0", "too many length or distance symbols", 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 e9 ff ff", "invalid bit length repeat", 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",
|
||||
"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 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("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() */
|
||||
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",
|
||||
"incorrect length check", -1);
|
||||
|
||||
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",
|
||||
"long code", 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",
|
||||
"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 "
|
||||
"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,
|
||||
Z_STREAM_END);
|
||||
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 */
|
||||
for (bits = 0; bits < 15; bits++)
|
||||
lens[bits] = (unsigned short)(bits + 1);
|
||||
|
||||
lens[15] = 15;
|
||||
next = table;
|
||||
bits = 15;
|
||||
|
|
|
@ -88,15 +88,19 @@ static char *strwinerror (error)
|
|||
(LPVOID)&msgbuf,
|
||||
0,
|
||||
NULL);
|
||||
if (chars != 0) {
|
||||
|
||||
if (chars != 0)
|
||||
{
|
||||
/* If there is an \r\n appended, zap it. */
|
||||
if (chars >= 2
|
||||
&& msgbuf[chars - 2] == '\r' && msgbuf[chars - 1] == '\n') {
|
||||
&& msgbuf[chars - 2] == '\r' && msgbuf[chars - 1] == '\n')
|
||||
{
|
||||
chars -= 2;
|
||||
msgbuf[chars] = 0;
|
||||
}
|
||||
|
||||
if (chars > sizeof (buf) - 1) {
|
||||
if (chars > sizeof(buf) - 1)
|
||||
{
|
||||
chars = sizeof(buf) - 1;
|
||||
msgbuf[chars] = 0;
|
||||
}
|
||||
|
@ -104,7 +108,8 @@ static char *strwinerror (error)
|
|||
wcstombs(buf, msgbuf, chars + 1);
|
||||
LocalFree(msgbuf);
|
||||
}
|
||||
else {
|
||||
else
|
||||
{
|
||||
sprintf(buf, "unknown win32 error (%ld)", error);
|
||||
}
|
||||
|
||||
|
@ -163,7 +168,8 @@ void myfree(q, p)
|
|||
free(p);
|
||||
}
|
||||
|
||||
typedef struct gzFile_s {
|
||||
typedef struct gzFile_s
|
||||
{
|
||||
FILE* file;
|
||||
int write;
|
||||
int err;
|
||||
|
@ -198,30 +204,40 @@ gzFile gz_open(path, fd, mode)
|
|||
int ret;
|
||||
|
||||
gz = malloc(sizeof(struct gzFile_s));
|
||||
|
||||
if (gz == NULL)
|
||||
return NULL;
|
||||
|
||||
gz->write = strchr(mode, 'w') != NULL;
|
||||
gz->strm.zalloc = myalloc;
|
||||
gz->strm.zfree = myfree;
|
||||
gz->strm.opaque = Z_NULL;
|
||||
|
||||
if (gz->write)
|
||||
ret = deflateInit2(&(gz->strm), -1, 8, 15 + 16, 8, 0);
|
||||
else {
|
||||
else
|
||||
{
|
||||
gz->strm.next_in = 0;
|
||||
gz->strm.avail_in = Z_NULL;
|
||||
ret = inflateInit2(&(gz->strm), 15 + 16);
|
||||
}
|
||||
if (ret != Z_OK) {
|
||||
|
||||
if (ret != Z_OK)
|
||||
{
|
||||
free(gz);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
gz->file = path == NULL ? fdopen(fd, 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));
|
||||
free(gz);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
gz->err = 0;
|
||||
gz->msg = "";
|
||||
return gz;
|
||||
|
@ -239,15 +255,20 @@ int gzwrite(gz, buf, len)
|
|||
|
||||
if (gz == NULL || !gz->write)
|
||||
return 0;
|
||||
|
||||
strm = &(gz->strm);
|
||||
strm->next_in = (void*)buf;
|
||||
strm->avail_in = len;
|
||||
do {
|
||||
|
||||
do
|
||||
{
|
||||
strm->next_out = out;
|
||||
strm->avail_out = BUFLEN;
|
||||
(void)deflate(strm, Z_NO_FLUSH);
|
||||
fwrite(out, 1, BUFLEN - strm->avail_out, gz->file);
|
||||
} while (strm->avail_out == 0);
|
||||
}
|
||||
while (strm->avail_out == 0);
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
|
@ -265,26 +286,37 @@ int gzread(gz, buf, len)
|
|||
|
||||
if (gz == NULL || gz->write)
|
||||
return 0;
|
||||
|
||||
if (gz->err)
|
||||
return 0;
|
||||
|
||||
strm = &(gz->strm);
|
||||
strm->next_out = (void*)buf;
|
||||
strm->avail_out = len;
|
||||
do {
|
||||
|
||||
do
|
||||
{
|
||||
got = fread(in, 1, 1, gz->file);
|
||||
|
||||
if (got == 0)
|
||||
break;
|
||||
|
||||
strm->next_in = in;
|
||||
strm->avail_in = 1;
|
||||
ret = inflate(strm, Z_NO_FLUSH);
|
||||
if (ret == Z_DATA_ERROR) {
|
||||
|
||||
if (ret == Z_DATA_ERROR)
|
||||
{
|
||||
gz->err = Z_DATA_ERROR;
|
||||
gz->msg = strm->msg;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (ret == Z_STREAM_END)
|
||||
inflateReset(strm);
|
||||
} while (strm->avail_out);
|
||||
}
|
||||
while (strm->avail_out);
|
||||
|
||||
return len - strm->avail_out;
|
||||
}
|
||||
|
||||
|
@ -298,20 +330,28 @@ int gzclose(gz)
|
|||
|
||||
if (gz == NULL)
|
||||
return Z_STREAM_ERROR;
|
||||
|
||||
strm = &(gz->strm);
|
||||
if (gz->write) {
|
||||
|
||||
if (gz->write)
|
||||
{
|
||||
strm->next_in = Z_NULL;
|
||||
strm->avail_in = 0;
|
||||
do {
|
||||
|
||||
do
|
||||
{
|
||||
strm->next_out = out;
|
||||
strm->avail_out = BUFLEN;
|
||||
(void)deflate(strm, Z_FINISH);
|
||||
fwrite(out, 1, BUFLEN - strm->avail_out, gz->file);
|
||||
} while (strm->avail_out == 0);
|
||||
}
|
||||
while (strm->avail_out == 0);
|
||||
|
||||
deflateEnd(strm);
|
||||
}
|
||||
else
|
||||
inflateEnd(strm);
|
||||
|
||||
fclose(gz->file);
|
||||
free(gz);
|
||||
return Z_OK;
|
||||
|
@ -364,22 +404,31 @@ void gz_compress(in, out)
|
|||
int err;
|
||||
|
||||
#ifdef USE_MMAP
|
||||
|
||||
/* Try first compressing with mmap. If mmap fails (minigzip used in a
|
||||
* pipe), use the normal fread loop.
|
||||
*/
|
||||
if (gz_compress_mmap(in, out) == Z_OK) return;
|
||||
|
||||
#endif
|
||||
for (;;) {
|
||||
|
||||
for (;;)
|
||||
{
|
||||
len = (int)fread(buf, 1, sizeof(buf), in);
|
||||
if (ferror(in)) {
|
||||
|
||||
if (ferror(in))
|
||||
{
|
||||
perror("fread");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (len == 0) break;
|
||||
|
||||
if (gzwrite(out, buf, (unsigned)len) != len) error(gzerror(out, &err));
|
||||
}
|
||||
|
||||
fclose(in);
|
||||
|
||||
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: */
|
||||
if (fstat(ifd, &sb) < 0) return Z_ERRNO;
|
||||
|
||||
buf_len = sb.st_size;
|
||||
|
||||
if (buf_len <= 0) return Z_ERRNO;
|
||||
|
||||
/* Now do the actual mmap: */
|
||||
buf = mmap((caddr_t) 0, buf_len, PROT_READ, MAP_SHARED, ifd, (off_t)0);
|
||||
|
||||
if (buf == (caddr_t)(-1)) return Z_ERRNO;
|
||||
|
||||
/* Compress the whole file at once: */
|
||||
|
@ -415,7 +467,9 @@ int gz_compress_mmap(in, out)
|
|||
|
||||
munmap(buf, buf_len);
|
||||
fclose(in);
|
||||
|
||||
if (gzclose(out) != Z_OK) error("failed gzclose");
|
||||
|
||||
return Z_OK;
|
||||
}
|
||||
#endif /* USE_MMAP */
|
||||
|
@ -431,15 +485,20 @@ void gz_uncompress(in, out)
|
|||
int len;
|
||||
int err;
|
||||
|
||||
for (;;) {
|
||||
for (;;)
|
||||
{
|
||||
len = gzread(in, buf, sizeof(buf));
|
||||
|
||||
if (len < 0) error(gzerror(in, &err));
|
||||
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
if (fclose(out)) error("failed fclose");
|
||||
|
||||
if (gzclose(in) != Z_OK) error("failed gzclose");
|
||||
|
@ -458,7 +517,8 @@ void file_compress(file, mode)
|
|||
FILE* in;
|
||||
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);
|
||||
exit(1);
|
||||
}
|
||||
|
@ -467,15 +527,21 @@ void file_compress(file, mode)
|
|||
strcat(outfile, GZ_SUFFIX);
|
||||
|
||||
in = fopen(file, "rb");
|
||||
if (in == NULL) {
|
||||
|
||||
if (in == NULL)
|
||||
{
|
||||
perror(file);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
out = gzopen(outfile, mode);
|
||||
if (out == NULL) {
|
||||
|
||||
if (out == NULL)
|
||||
{
|
||||
fprintf(stderr, "%s: can't gzopen %s\n", prog, outfile);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
gz_compress(in, out);
|
||||
|
||||
unlink(file);
|
||||
|
@ -494,29 +560,39 @@ void file_uncompress(file)
|
|||
gzFile in;
|
||||
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);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
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;
|
||||
outfile = buf;
|
||||
outfile[len - 3] = '\0';
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
outfile = file;
|
||||
infile = buf;
|
||||
strcat(infile, GZ_SUFFIX);
|
||||
}
|
||||
|
||||
in = gzopen(infile, "rb");
|
||||
if (in == NULL) {
|
||||
|
||||
if (in == NULL)
|
||||
{
|
||||
fprintf(stderr, "%s: can't gzopen %s\n", prog, infile);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
out = fopen(outfile, "wb");
|
||||
if (out == NULL) {
|
||||
|
||||
if (out == NULL)
|
||||
{
|
||||
perror(file);
|
||||
exit(1);
|
||||
}
|
||||
|
@ -550,10 +626,12 @@ int main(argc, argv)
|
|||
|
||||
prog = argv[0];
|
||||
bname = strrchr(argv[0], '/');
|
||||
|
||||
if (bname)
|
||||
bname++;
|
||||
else
|
||||
bname = argv[0];
|
||||
|
||||
argc--, argv++;
|
||||
|
||||
if (!strcmp(bname, "gunzip"))
|
||||
|
@ -561,7 +639,8 @@ int main(argc, argv)
|
|||
else if (!strcmp(bname, "zcat"))
|
||||
copyout = uncompr = 1;
|
||||
|
||||
while (argc > 0) {
|
||||
while (argc > 0)
|
||||
{
|
||||
if (strcmp(*argv, "-c") == 0)
|
||||
copyout = 1;
|
||||
else if (strcmp(*argv, "-d") == 0)
|
||||
|
@ -577,55 +656,88 @@ int main(argc, argv)
|
|||
outmode[2] = (*argv)[1];
|
||||
else
|
||||
break;
|
||||
|
||||
argc--, argv++;
|
||||
}
|
||||
|
||||
if (outmode[3] == ' ')
|
||||
outmode[3] = 0;
|
||||
if (argc == 0) {
|
||||
|
||||
if (argc == 0)
|
||||
{
|
||||
SET_BINARY_MODE(stdin);
|
||||
SET_BINARY_MODE(stdout);
|
||||
if (uncompr) {
|
||||
|
||||
if (uncompr)
|
||||
{
|
||||
file = gzdopen(fileno(stdin), "rb");
|
||||
|
||||
if (file == NULL) error("can't gzdopen stdin");
|
||||
|
||||
gz_uncompress(file, stdout);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
file = gzdopen(fileno(stdout), outmode);
|
||||
|
||||
if (file == NULL) error("can't gzdopen stdout");
|
||||
|
||||
gz_compress(stdin, file);
|
||||
}
|
||||
} else {
|
||||
if (copyout) {
|
||||
}
|
||||
else
|
||||
{
|
||||
if (copyout)
|
||||
{
|
||||
SET_BINARY_MODE(stdout);
|
||||
}
|
||||
do {
|
||||
if (uncompr) {
|
||||
if (copyout) {
|
||||
|
||||
do
|
||||
{
|
||||
if (uncompr)
|
||||
{
|
||||
if (copyout)
|
||||
{
|
||||
file = gzopen(*argv, "rb");
|
||||
|
||||
if (file == NULL)
|
||||
fprintf(stderr, "%s: can't gzopen %s\n", prog, *argv);
|
||||
else
|
||||
gz_uncompress(file, stdout);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
file_uncompress(*argv);
|
||||
}
|
||||
} else {
|
||||
if (copyout) {
|
||||
}
|
||||
else
|
||||
{
|
||||
if (copyout)
|
||||
{
|
||||
FILE* in = fopen(*argv, "rb");
|
||||
|
||||
if (in == NULL) {
|
||||
if (in == NULL)
|
||||
{
|
||||
perror(*argv);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
file = gzdopen(fileno(stdout), outmode);
|
||||
|
||||
if (file == NULL) error("can't gzdopen stdout");
|
||||
|
||||
gz_compress(in, file);
|
||||
}
|
||||
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
file_compress(*argv, outmode);
|
||||
}
|
||||
}
|
||||
} while (argv++, --argc);
|
||||
}
|
||||
while (argv++, --argc);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -114,7 +114,8 @@ local int base_dist[D_CODES];
|
|||
# include "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 intf* extra_bits; /* extra bits for each code or NULL */
|
||||
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))
|
||||
* 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;
|
||||
put_short(s, s->bi_buf);
|
||||
s->bi_buf = (ush)value >> (Buf_size - s->bi_valid);
|
||||
s->bi_valid += length - Buf_size;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
s->bi_buf |= (ush)value << s->bi_valid;
|
||||
s->bi_valid += length;
|
||||
}
|
||||
|
@ -256,12 +260,17 @@ local void tr_static_init()
|
|||
|
||||
/* Initialize the mapping length (0..255) -> length code (0..28) */
|
||||
length = 0;
|
||||
for (code = 0; code < LENGTH_CODES-1; code++) {
|
||||
|
||||
for (code = 0; code < LENGTH_CODES - 1; code++)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
Assert(length == 256, "tr_static_init: length != 256");
|
||||
/* Note that the length 255 (match length 258) can be represented
|
||||
* 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) */
|
||||
dist = 0;
|
||||
for (code = 0 ; code < 16; code++) {
|
||||
|
||||
for (code = 0 ; code < 16; code++)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
Assert(dist == 256, "tr_static_init: dist != 256");
|
||||
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;
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
Assert(dist == 256, "tr_static_init: 256+dist != 512");
|
||||
|
||||
/* Construct the codes of the static literal tree */
|
||||
for (bits = 0; bits <= MAX_BITS; bits++) bl_count[bits] = 0;
|
||||
|
||||
n = 0;
|
||||
|
||||
while (n <= 143) static_ltree[n++].Len = 8, bl_count[8]++;
|
||||
|
||||
while (n <= 255) static_ltree[n++].Len = 9, bl_count[9]++;
|
||||
|
||||
while (n <= 279) static_ltree[n++].Len = 7, bl_count[7]++;
|
||||
|
||||
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
|
||||
* tree construction to get a canonical Huffman tree (longest code
|
||||
* all ones)
|
||||
|
@ -301,10 +326,12 @@ local void tr_static_init()
|
|||
gen_codes((ct_data*)static_ltree, L_CODES + 1, bl_count);
|
||||
|
||||
/* 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].Code = bi_reverse((unsigned)n, 5);
|
||||
}
|
||||
|
||||
static_init_done = 1;
|
||||
|
||||
# ifdef GEN_TREES_H
|
||||
|
@ -335,38 +362,50 @@ void gen_trees_header()
|
|||
"/* header created automatically with -DGEN_TREES_H */\n\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,
|
||||
static_ltree[i].Len, SEPARATOR(i, L_CODES + 1, 5));
|
||||
}
|
||||
|
||||
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,
|
||||
static_dtree[i].Len, SEPARATOR(i, D_CODES - 1, 5));
|
||||
}
|
||||
|
||||
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],
|
||||
SEPARATOR(i, DIST_CODE_LEN - 1, 20));
|
||||
}
|
||||
|
||||
fprintf(header,
|
||||
"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],
|
||||
SEPARATOR(i, MAX_MATCH - MIN_MATCH, 20));
|
||||
}
|
||||
|
||||
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],
|
||||
SEPARATOR(i, LENGTH_CODES - 1, 20));
|
||||
}
|
||||
|
||||
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],
|
||||
SEPARATOR(i, D_CODES - 1, 10));
|
||||
}
|
||||
|
@ -421,7 +460,9 @@ local void init_block(s)
|
|||
|
||||
/* Initialize the trees. */
|
||||
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 < BL_CODES; n++) s->bl_tree[n].Freq = 0;
|
||||
|
||||
s->dyn_ltree[END_BLOCK].Freq = 1;
|
||||
|
@ -469,21 +510,27 @@ local void pqdownheap(s, tree, k)
|
|||
{
|
||||
int v = s->heap[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: */
|
||||
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++;
|
||||
}
|
||||
|
||||
/* Exit if v is smaller than both sons */
|
||||
if (smaller(tree, v, s->heap[j], s->depth)) break;
|
||||
|
||||
/* 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 */
|
||||
j <<= 1;
|
||||
}
|
||||
|
||||
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 */
|
||||
|
||||
for (h = s->heap_max+1; h < HEAP_SIZE; h++) {
|
||||
for (h = s->heap_max + 1; h < HEAP_SIZE; h++)
|
||||
{
|
||||
n = s->heap[h];
|
||||
bits = tree[tree[n].Dad].Len + 1;
|
||||
|
||||
if (bits > max_length) bits = max_length, overflow++;
|
||||
|
||||
tree[n].Len = (ush)bits;
|
||||
/* We overwrite tree[n].Dad which is no longer needed */
|
||||
|
||||
|
@ -536,20 +586,27 @@ local void gen_bitlen(s, desc)
|
|||
|
||||
s->bl_count[bits]++;
|
||||
xbits = 0;
|
||||
|
||||
if (n >= base) xbits = extra[n - base];
|
||||
|
||||
f = tree[n].Freq;
|
||||
s->opt_len += (ulg)f * (bits + xbits);
|
||||
|
||||
if (stree) s->static_len += (ulg)f * (stree[n].Len + xbits);
|
||||
}
|
||||
|
||||
if (overflow == 0) return;
|
||||
|
||||
Trace((stderr, "\nbit length overflow\n"));
|
||||
/* This happens for example on obj2 and pic of the Calgary corpus */
|
||||
|
||||
/* Find the first bit length which could increase: */
|
||||
do {
|
||||
do
|
||||
{
|
||||
bits = max_length - 1;
|
||||
|
||||
while (s->bl_count[bits] == 0) bits--;
|
||||
|
||||
s->bl_count[bits]--; /* move one leaf down the tree */
|
||||
s->bl_count[bits + 1] += 2; /* move one overflow item as its brother */
|
||||
s->bl_count[max_length]--;
|
||||
|
@ -557,24 +614,32 @@ local void gen_bitlen(s, desc)
|
|||
* but this does not affect bl_count[max_length]
|
||||
*/
|
||||
overflow -= 2;
|
||||
} while (overflow > 0);
|
||||
}
|
||||
while (overflow > 0);
|
||||
|
||||
/* Now recompute all bit lengths, scanning in increasing frequency.
|
||||
* 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
|
||||
* 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];
|
||||
while (n != 0) {
|
||||
|
||||
while (n != 0)
|
||||
{
|
||||
m = s->heap[--h];
|
||||
|
||||
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));
|
||||
s->opt_len += ((long)bits - (long)tree[m].Len)
|
||||
* (long)tree[m].Freq;
|
||||
tree[m].Len = (ush)bits;
|
||||
}
|
||||
|
||||
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
|
||||
* 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;
|
||||
}
|
||||
|
||||
/* Check that the bit counts in bl_count are consistent. The last code
|
||||
* must be all ones.
|
||||
*/
|
||||
|
@ -615,13 +682,16 @@ local void gen_codes (tree, max_code, bl_count)
|
|||
"inconsistent bit counts");
|
||||
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
|
||||
ush len = tree[n].Len;
|
||||
#else
|
||||
int len = tree[n].Len;
|
||||
#endif
|
||||
|
||||
if (len == 0) continue;
|
||||
|
||||
/* Now reverse the bits */
|
||||
#ifdef WIN32
|
||||
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;
|
||||
|
||||
for (n = 0; n < elems; n++) {
|
||||
if (tree[n].Freq != 0) {
|
||||
for (n = 0; n < elems; n++)
|
||||
{
|
||||
if (tree[n].Freq != 0)
|
||||
{
|
||||
s->heap[++(s->heap_len)] = max_code = n;
|
||||
s->depth[n] = 0;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
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
|
||||
* 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);
|
||||
tree[node].Freq = 1;
|
||||
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 */
|
||||
}
|
||||
|
||||
desc->max_code = max_code;
|
||||
|
||||
/* 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.
|
||||
*/
|
||||
node = elems; /* next internal node of the tree */
|
||||
do {
|
||||
|
||||
do
|
||||
{
|
||||
pqremove(s, tree, n); /* n = node of 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);
|
||||
tree[n].Dad = tree[m].Dad = (ush)node;
|
||||
#ifdef DUMP_BL_TREE
|
||||
if (tree == s->bl_tree) {
|
||||
|
||||
if (tree == s->bl_tree)
|
||||
{
|
||||
fprintf(stderr, "\nnode %d(%d), sons %d(%d) %d(%d)",
|
||||
node, tree[node].Freq, n, tree[n].Freq, m, tree[m].Freq);
|
||||
}
|
||||
|
||||
#endif
|
||||
/* and insert the new node in the heap */
|
||||
s->heap[SMALLEST] = node++;
|
||||
pqdownheap(s, tree, SMALLEST);
|
||||
|
||||
} while (s->heap_len >= 2);
|
||||
}
|
||||
while (s->heap_len >= 2);
|
||||
|
||||
s->heap[--(s->heap_max)] = s->heap[SMALLEST];
|
||||
|
||||
|
@ -759,28 +844,50 @@ local void scan_tree (s, tree, max_code)
|
|||
#endif
|
||||
|
||||
if (nextlen == 0) max_count = 138, min_count = 3;
|
||||
|
||||
tree[max_code + 1].Len = (ush)0xffff; /* guard */
|
||||
|
||||
for (n = 0; n <= max_code; n++) {
|
||||
curlen = nextlen; nextlen = tree[n+1].Len;
|
||||
if (++count < max_count && curlen == nextlen) {
|
||||
for (n = 0; n <= max_code; n++)
|
||||
{
|
||||
curlen = nextlen;
|
||||
nextlen = tree[n + 1].Len;
|
||||
|
||||
if (++count < max_count && curlen == nextlen)
|
||||
{
|
||||
continue;
|
||||
} else if (count < min_count) {
|
||||
}
|
||||
else if (count < min_count)
|
||||
{
|
||||
s->bl_tree[curlen].Freq += count;
|
||||
} else if (curlen != 0) {
|
||||
}
|
||||
else if (curlen != 0)
|
||||
{
|
||||
if (curlen != prevlen) s->bl_tree[curlen].Freq++;
|
||||
|
||||
s->bl_tree[REP_3_6].Freq++;
|
||||
} else if (count <= 10) {
|
||||
}
|
||||
else if (count <= 10)
|
||||
{
|
||||
s->bl_tree[REPZ_3_10].Freq++;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
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;
|
||||
} else if (curlen == nextlen) {
|
||||
}
|
||||
else if (curlen == nextlen)
|
||||
{
|
||||
max_count = 6, min_count = 3;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
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 */
|
||||
if (nextlen == 0) max_count = 138, min_count = 3;
|
||||
|
||||
for (n = 0; n <= max_code; n++) {
|
||||
curlen = nextlen; nextlen = tree[n+1].Len;
|
||||
if (++count < max_count && curlen == nextlen) {
|
||||
for (n = 0; n <= max_code; n++)
|
||||
{
|
||||
curlen = nextlen;
|
||||
nextlen = tree[n + 1].Len;
|
||||
|
||||
if (++count < max_count && curlen == nextlen)
|
||||
{
|
||||
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?");
|
||||
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;
|
||||
if (nextlen == 0) {
|
||||
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;
|
||||
|
||||
if (nextlen == 0)
|
||||
{
|
||||
max_count = 138, min_count = 3;
|
||||
} else if (curlen == nextlen) {
|
||||
}
|
||||
else if (curlen == nextlen)
|
||||
{
|
||||
max_count = 6, min_count = 3;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
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
|
||||
* 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;
|
||||
}
|
||||
|
||||
/* Update opt_len to include the bit length tree and counts */
|
||||
s->opt_len += 3 * (max_blindex + 1) + 5 + 5 + 4;
|
||||
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, dcodes - 1, 5);
|
||||
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]));
|
||||
send_bits(s, s->bl_tree[bl_order[rank]].Len, 3);
|
||||
}
|
||||
|
||||
Tracev((stderr, "\nbl tree: sent %ld", s->bits_sent));
|
||||
|
||||
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 */
|
||||
|
||||
/* 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 */
|
||||
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;
|
||||
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert(buf != (char*)0, "lost buf");
|
||||
opt_lenb = static_lenb = stored_len + 5; /* force a stored block */
|
||||
}
|
||||
|
||||
#ifdef FORCE_STORED
|
||||
if (buf != (char*)0) { /* force stored block */
|
||||
|
||||
if (buf != (char*)0) /* force stored block */
|
||||
{
|
||||
#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 */
|
||||
#endif
|
||||
/* 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);
|
||||
|
||||
#ifdef FORCE_STATIC
|
||||
} else if (static_lenb >= 0) { /* force static trees */
|
||||
}
|
||||
else if (static_lenb >= 0) /* force static trees */
|
||||
{
|
||||
#else
|
||||
} else if (s->strategy == Z_FIXED || static_lenb == opt_lenb) {
|
||||
}
|
||||
else if (s->strategy == Z_FIXED || static_lenb == opt_lenb)
|
||||
{
|
||||
#endif
|
||||
send_bits(s, (STATIC_TREES << 1) + last, 3);
|
||||
compress_block(s, (ct_data*)static_ltree, (ct_data*)static_dtree);
|
||||
#ifdef DEBUG
|
||||
s->compressed_len += 3 + s->static_len;
|
||||
#endif
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
send_bits(s, (DYN_TREES << 1) + last, 3);
|
||||
send_all_trees(s, s->l_desc.max_code + 1, s->d_desc.max_code + 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;
|
||||
#endif
|
||||
}
|
||||
|
||||
Assert(s->compressed_len == s->bits_sent, "bad compressed size");
|
||||
/* The above check is made mod 2^32, for files larger than 512 MB
|
||||
* and uLong implemented on 32 bits.
|
||||
*/
|
||||
init_block(s);
|
||||
|
||||
if (last) {
|
||||
if (last)
|
||||
{
|
||||
bi_windup(s);
|
||||
#ifdef DEBUG
|
||||
s->compressed_len += 7; /* align on byte boundary */
|
||||
#endif
|
||||
}
|
||||
|
||||
Tracev((stderr, "\ncomprlen %lu(%lu) ", s->compressed_len >> 3,
|
||||
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->l_buf[s->last_lit++] = (uch)lc;
|
||||
if (dist == 0) {
|
||||
|
||||
if (dist == 0)
|
||||
{
|
||||
/* lc is the unmatched char */
|
||||
s->dyn_ltree[lc].Freq++;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
s->matches++;
|
||||
/* Here, lc is the match length - MIN_MATCH */
|
||||
dist--; /* dist = match distance - 1 */
|
||||
|
@ -1103,22 +1262,29 @@ int ZLIB_INTERNAL _tr_tally (s, dist, lc)
|
|||
}
|
||||
|
||||
#ifdef TRUNCATE_BLOCK
|
||||
|
||||
/* 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 */
|
||||
ulg out_length = (ulg)s->last_lit * 8L;
|
||||
ulg in_length = (ulg)((long)s->strstart - s->block_start);
|
||||
int dcode;
|
||||
for (dcode = 0; dcode < D_CODES; dcode++) {
|
||||
|
||||
for (dcode = 0; dcode < D_CODES; dcode++)
|
||||
{
|
||||
out_length += (ulg)s->dyn_dtree[dcode].Freq *
|
||||
(5L + extra_dbits[dcode]);
|
||||
}
|
||||
|
||||
out_length >>= 3;
|
||||
Tracev((stderr, "\nlast_lit %u, in %ld, out ~%ld(%ld%%) ",
|
||||
s->last_lit, in_length, out_length,
|
||||
100L - out_length * 100L / in_length));
|
||||
|
||||
if (s->matches < s->last_lit / 2 && out_length < in_length / 2) return 1;
|
||||
}
|
||||
|
||||
#endif
|
||||
return (s->last_lit == s->lit_bufsize - 1);
|
||||
/* 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 */
|
||||
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];
|
||||
lc = s->l_buf[lx++];
|
||||
if (dist == 0) {
|
||||
|
||||
if (dist == 0)
|
||||
{
|
||||
send_code(s, lc, ltree); /* send a literal byte */
|
||||
Tracecv(isgraph(lc), (stderr, " '%c' ", lc));
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Here, lc is the match length - MIN_MATCH */
|
||||
code = _length_code[lc];
|
||||
send_code(s, code + LITERALS + 1, ltree); /* send the length code */
|
||||
extra = extra_lbits[code];
|
||||
if (extra != 0) {
|
||||
|
||||
if (extra != 0)
|
||||
{
|
||||
lc -= base_length[code];
|
||||
send_bits(s, lc, extra); /* send the extra length bits */
|
||||
}
|
||||
|
||||
dist--; /* dist is now the match distance - 1 */
|
||||
code = d_code(dist);
|
||||
Assert(code < D_CODES, "bad d_code");
|
||||
|
||||
send_code(s, code, dtree); /* send the distance code */
|
||||
extra = extra_dbits[code];
|
||||
if (extra != 0) {
|
||||
|
||||
if (extra != 0)
|
||||
{
|
||||
dist -= base_dist[code];
|
||||
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,
|
||||
"pendingBuf overflow");
|
||||
|
||||
} while (lx < s->last_lit);
|
||||
}
|
||||
while (lx < s->last_lit);
|
||||
|
||||
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
|
||||
|| s->dyn_ltree[13].Freq != 0)
|
||||
return Z_TEXT;
|
||||
|
||||
for (n = 32; n < LITERALS; n++)
|
||||
if (s->dyn_ltree[n].Freq != 0)
|
||||
return Z_TEXT;
|
||||
|
@ -1241,10 +1419,14 @@ local unsigned bi_reverse(code, len)
|
|||
#endif
|
||||
{
|
||||
register unsigned res = 0;
|
||||
do {
|
||||
|
||||
do
|
||||
{
|
||||
res |= code & 1;
|
||||
code >>= 1, res <<= 1;
|
||||
} while (--len > 0);
|
||||
}
|
||||
while (--len > 0);
|
||||
|
||||
return res >> 1;
|
||||
}
|
||||
|
||||
|
@ -1258,11 +1440,14 @@ local void bi_flush(s)
|
|||
deflate_state* s;
|
||||
#endif
|
||||
{
|
||||
if (s->bi_valid == 16) {
|
||||
if (s->bi_valid == 16)
|
||||
{
|
||||
put_short(s, s->bi_buf);
|
||||
s->bi_buf = 0;
|
||||
s->bi_valid = 0;
|
||||
} else if (s->bi_valid >= 8) {
|
||||
}
|
||||
else if (s->bi_valid >= 8)
|
||||
{
|
||||
put_byte(s, (Byte)s->bi_buf);
|
||||
s->bi_buf >>= 8;
|
||||
s->bi_valid -= 8;
|
||||
|
@ -1279,11 +1464,15 @@ local void bi_windup(s)
|
|||
deflate_state* s;
|
||||
#endif
|
||||
{
|
||||
if (s->bi_valid > 8) {
|
||||
if (s->bi_valid > 8)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
s->bi_buf = 0;
|
||||
s->bi_valid = 0;
|
||||
#ifdef DEBUG
|
||||
|
@ -1307,17 +1496,21 @@ local void copy_block(s, buf, len, header)
|
|||
{
|
||||
bi_windup(s); /* align on byte boundary */
|
||||
|
||||
if (header) {
|
||||
if (header)
|
||||
{
|
||||
put_short(s, (ush)len);
|
||||
put_short(s, (ush)~len);
|
||||
#ifdef DEBUG
|
||||
s->bits_sent += 2 * 16;
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef DEBUG
|
||||
s->bits_sent += (ulg)len << 3;
|
||||
#endif
|
||||
while (len--) {
|
||||
|
||||
while (len--)
|
||||
{
|
||||
put_byte(s, *buf++);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
/* 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}},
|
||||
{{172}, { 8}}, {{108}, { 8}}, {{236}, { 8}}, {{ 28}, { 8}}, {{156}, { 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}}
|
||||
};
|
||||
|
||||
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}},
|
||||
{{20}, { 5}}, {{12}, { 5}}, {{28}, { 5}}, {{ 2}, { 5}}, {{18}, { 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}}
|
||||
};
|
||||
|
||||
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,
|
||||
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,
|
||||
|
@ -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
|
||||
};
|
||||
|
||||
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,
|
||||
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,
|
||||
|
@ -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
|
||||
};
|
||||
|
||||
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,
|
||||
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,
|
||||
32, 48, 64, 96, 128, 192, 256, 384, 512, 768,
|
||||
1024, 1536, 2048, 3072, 4096, 6144, 8192, 12288, 16384, 24576
|
||||
|
|
|
@ -36,26 +36,34 @@ int ZEXPORT uncompress (dest, destLen, source, sourceLen)
|
|||
|
||||
stream.next_in = (Bytef*)source;
|
||||
stream.avail_in = (uInt)sourceLen;
|
||||
|
||||
/* Check for source > 64K on 16-bit machine: */
|
||||
if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
|
||||
|
||||
stream.next_out = dest;
|
||||
stream.avail_out = (uInt) * destLen;
|
||||
|
||||
if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
|
||||
|
||||
stream.zalloc = (alloc_func)0;
|
||||
stream.zfree = (free_func)0;
|
||||
|
||||
err = inflateInit(&stream);
|
||||
|
||||
if (err != Z_OK) return err;
|
||||
|
||||
err = inflate(&stream, Z_FINISH);
|
||||
if (err != Z_STREAM_END) {
|
||||
|
||||
if (err != Z_STREAM_END)
|
||||
{
|
||||
inflateEnd(&stream);
|
||||
|
||||
if (err == Z_NEED_DICT || (err == Z_BUF_ERROR && stream.avail_in == 0))
|
||||
return Z_DATA_ERROR;
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
*destLen = stream.total_out;
|
||||
|
||||
err = inflateEnd(&stream);
|
||||
|
|
|
@ -107,7 +107,8 @@ typedef void (*free_func) OF((voidpf opaque, voidpf address));
|
|||
|
||||
struct internal_state;
|
||||
|
||||
typedef struct z_stream_s {
|
||||
typedef struct z_stream_s
|
||||
{
|
||||
z_const Bytef* next_in; /* next input byte */
|
||||
uInt avail_in; /* number of bytes available at next_in */
|
||||
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
|
||||
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 */
|
||||
uLong time; /* modification time */
|
||||
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
|
||||
* only be used by the gzgetc() macro. You have been warned.
|
||||
*/
|
||||
struct gzFile_s {
|
||||
struct gzFile_s
|
||||
{
|
||||
unsigned have;
|
||||
unsigned char* next;
|
||||
z_off64_t pos;
|
||||
|
|
|
@ -14,7 +14,8 @@
|
|||
struct internal_state {int dummy;}; /* for buggy compilers */
|
||||
#endif
|
||||
|
||||
const char * const z_errmsg[10] = {
|
||||
const char* const z_errmsg[10] =
|
||||
{
|
||||
"need dictionary", /* Z_NEED_DICT 2 */
|
||||
"stream end", /* Z_STREAM_END 1 */
|
||||
"", /* Z_OK 0 */
|
||||
|
@ -24,7 +25,8 @@ const char * const z_errmsg[10] = {
|
|||
"insufficient memory", /* Z_MEM_ERROR (-4) */
|
||||
"buffer error", /* Z_BUF_ERROR (-5) */
|
||||
"incompatible version",/* Z_VERSION_ERROR (-6) */
|
||||
""};
|
||||
""
|
||||
};
|
||||
|
||||
|
||||
const char* ZEXPORT zlibVersion()
|
||||
|
@ -37,30 +39,75 @@ uLong ZEXPORT zlibCompileFlags()
|
|||
uLong flags;
|
||||
|
||||
flags = 0;
|
||||
switch ((int)(sizeof(uInt))) {
|
||||
case 2: break;
|
||||
case 4: flags += 1; break;
|
||||
case 8: flags += 2; break;
|
||||
default: flags += 3;
|
||||
|
||||
switch ((int)(sizeof(uInt)))
|
||||
{
|
||||
case 2:
|
||||
break;
|
||||
|
||||
case 4:
|
||||
flags += 1;
|
||||
break;
|
||||
|
||||
case 8:
|
||||
flags += 2;
|
||||
break;
|
||||
|
||||
default:
|
||||
flags += 3;
|
||||
}
|
||||
switch ((int)(sizeof(uLong))) {
|
||||
case 2: break;
|
||||
case 4: flags += 1 << 2; break;
|
||||
case 8: flags += 2 << 2; break;
|
||||
default: flags += 3 << 2;
|
||||
|
||||
switch ((int)(sizeof(uLong)))
|
||||
{
|
||||
case 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;
|
||||
case 4: flags += 1 << 4; break;
|
||||
case 8: flags += 2 << 4; break;
|
||||
default: flags += 3 << 4;
|
||||
|
||||
switch ((int)(sizeof(voidpf)))
|
||||
{
|
||||
case 2:
|
||||
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;
|
||||
case 4: flags += 1 << 6; break;
|
||||
case 8: flags += 2 << 6; break;
|
||||
default: flags += 3 << 6;
|
||||
|
||||
switch ((int)(sizeof(z_off_t)))
|
||||
{
|
||||
case 2:
|
||||
break;
|
||||
|
||||
case 4:
|
||||
flags += 1 << 6;
|
||||
break;
|
||||
|
||||
case 8:
|
||||
flags += 2 << 6;
|
||||
break;
|
||||
|
||||
default:
|
||||
flags += 3 << 6;
|
||||
}
|
||||
|
||||
#ifdef DEBUG
|
||||
flags += 1 << 8;
|
||||
#endif
|
||||
|
@ -159,9 +206,12 @@ void ZLIB_INTERNAL zmemcpy(dest, source, len)
|
|||
uInt len;
|
||||
{
|
||||
if (len == 0) return;
|
||||
do {
|
||||
|
||||
do
|
||||
{
|
||||
*dest++ = *source++; /* ??? to be unrolled */
|
||||
} while (--len != 0);
|
||||
}
|
||||
while (--len != 0);
|
||||
}
|
||||
|
||||
int ZLIB_INTERNAL zmemcmp(s1, s2, len)
|
||||
|
@ -171,9 +221,11 @@ int ZLIB_INTERNAL zmemcmp(s1, s2, len)
|
|||
{
|
||||
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;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -182,9 +234,12 @@ void ZLIB_INTERNAL zmemzero(dest, len)
|
|||
uInt len;
|
||||
{
|
||||
if (len == 0) return;
|
||||
do {
|
||||
|
||||
do
|
||||
{
|
||||
*dest++ = 0; /* ??? to be unrolled */
|
||||
} while (--len != 0);
|
||||
}
|
||||
while (--len != 0);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
@ -208,7 +263,8 @@ void ZLIB_INTERNAL zmemzero(dest, len)
|
|||
|
||||
local int next_ptr = 0;
|
||||
|
||||
typedef struct ptr_table_s {
|
||||
typedef struct ptr_table_s
|
||||
{
|
||||
voidpf org_ptr;
|
||||
voidpf new_ptr;
|
||||
} 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
|
||||
* will return a usable pointer which doesn't have to be normalized.
|
||||
*/
|
||||
if (bsize < 65520L) {
|
||||
if (bsize < 65520L)
|
||||
{
|
||||
buf = farmalloc(bsize);
|
||||
|
||||
if (*(ush*)&buf != 0) return buf;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
buf = farmalloc(bsize + 16L);
|
||||
}
|
||||
|
||||
if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
|
||||
|
||||
table[next_ptr].org_ptr = buf;
|
||||
|
||||
/* 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)
|
||||
{
|
||||
int n;
|
||||
if (*(ush*)&ptr != 0) { /* object < 64K */
|
||||
|
||||
if (*(ush*)&ptr != 0) /* object < 64K */
|
||||
{
|
||||
farfree(ptr);
|
||||
return;
|
||||
}
|
||||
|
||||
/* 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;
|
||||
|
||||
farfree(table[n].org_ptr);
|
||||
while (++n < next_ptr) {
|
||||
|
||||
while (++n < next_ptr)
|
||||
{
|
||||
table[n - 1] = table[n];
|
||||
}
|
||||
|
||||
next_ptr--;
|
||||
return;
|
||||
}
|
||||
|
||||
ptr = opaque; /* just to make some compilers happy */
|
||||
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)
|
||||
{
|
||||
if (opaque) opaque = 0; /* to make compiler happy */
|
||||
|
||||
return _halloc((long)items, size);
|
||||
}
|
||||
|
||||
void ZLIB_INTERNAL zcfree(voidpf opaque, voidpf ptr)
|
||||
{
|
||||
if (opaque) opaque = 0; /* to make compiler happy */
|
||||
|
||||
_hfree(ptr);
|
||||
}
|
||||
|
||||
|
@ -315,6 +387,7 @@ voidpf ZLIB_INTERNAL zcalloc (opaque, items, size)
|
|||
#endif
|
||||
{
|
||||
if (opaque) items += size - size; /* make compiler happy */
|
||||
|
||||
return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) :
|
||||
(voidpf)calloc(items, size);
|
||||
}
|
||||
|
@ -328,6 +401,7 @@ void ZLIB_INTERNAL zcfree (opaque, ptr)
|
|||
#endif
|
||||
{
|
||||
free(ptr);
|
||||
|
||||
if (opaque) return; /* make compiler happy */
|
||||
}
|
||||
|
||||
|
|
|
@ -337,7 +337,8 @@ public:
|
|||
* \brief setCrystals
|
||||
* \param val
|
||||
*/
|
||||
void setCrystals(ALTTPCrystals val);\
|
||||
void setCrystals(ALTTPCrystals val);
|
||||
\
|
||||
|
||||
/*!
|
||||
* \brief crystals
|
||||
|
|
|
@ -54,9 +54,12 @@ public:
|
|||
do { \
|
||||
if (atGetExceptionHandler()) {atGetExceptionHandler()(__FILE__, AT_PRETTY_FUNCTION, __LINE__, __VA_ARGS__); return; \
|
||||
} else {
|
||||
std::string msg = Athena::utility::sprintf(args, __VA_ARGS__); \
|
||||
throw Athena::error::IOException(std::string("IOException: ")+msg, __FILE__, AT_PRETTY_FUNCTION, __LINE__); \
|
||||
std::string msg = Athena::utility::sprintf(args, __VA_ARGS__);
|
||||
\
|
||||
throw Athena::error::IOException(std::string("IOException: ") + msg, __FILE__, AT_PRETTY_FUNCTION, __LINE__);
|
||||
\
|
||||
} \
|
||||
|
||||
} while (0)
|
||||
#elif defined(__GNUC__)
|
||||
#define THROW_IO_EXCEPTION(args...) \
|
||||
|
@ -73,9 +76,13 @@ public:
|
|||
do { \
|
||||
if (atGetExceptionHandler()) {atGetExceptionHandler()(__FILE__, AT_PRETTY_FUNCTION, __LINE__, __VA_ARGS__); return ret; \
|
||||
} else {
|
||||
std::string msg = Athena::utility::sprintf(args, __VA_ARGS__); \
|
||||
throw Athena::error::IOException(std::string("IOException: ")+msg, __FILE__, AT_PRETTY_FUNCTION, __LINE__); \
|
||||
std::string msg = Athena::utility::sprintf(args, __VA_ARGS__);
|
||||
|
||||
\
|
||||
throw Athena::error::IOException(std::string("IOException: ") + msg, __FILE__, AT_PRETTY_FUNCTION, __LINE__);
|
||||
\
|
||||
} \
|
||||
|
||||
} while (0)
|
||||
#elif defined(__GNUC__)
|
||||
#define THROW_IO_EXCEPTION_RETURN(ret, args...) \
|
||||
|
|
|
@ -53,9 +53,12 @@ public:
|
|||
do { \
|
||||
if (atGetExceptionHandler()) {atGetExceptionHandler()(__FILE__, AT_PRETTY_FUNCTION, __LINE__, __VA_ARGS__); return; \
|
||||
} else {
|
||||
std::string msg = Athena::utility::sprintf(args, __VA_ARGS__); \
|
||||
throw Athena::error::InvalidOperationException(std::string("InvalidOperationException: ")+msg, __FILE__, AT_PRETTY_FUNCTION, __LINE__); \
|
||||
std::string msg = Athena::utility::sprintf(args, __VA_ARGS__);
|
||||
\
|
||||
throw Athena::error::InvalidOperationException(std::string("InvalidOperationException: ") + msg, __FILE__, AT_PRETTY_FUNCTION, __LINE__);
|
||||
\
|
||||
} \
|
||||
|
||||
} while (0)
|
||||
#elif defined (__GNUC__)
|
||||
#define THROW_INVALID_OPERATION_EXCEPTION(args...) \
|
||||
|
@ -72,9 +75,13 @@ public:
|
|||
do { \
|
||||
if (atGetExceptionHandler()) {atGetExceptionHandler()(__FILE__, AT_PRETTY_FUNCTION, __LINE__, __VA_ARGS__); return ret; \
|
||||
} else {
|
||||
std::string msg = Athena::utility::sprintf(args, __VA_ARGS__); \
|
||||
throw Athena::error::InvalidOperationException(std::string("InvalidOperationException: ")+msg, __FILE__, AT_PRETTY_FUNCTION, __LINE__); \
|
||||
std::string msg = Athena::utility::sprintf(args, __VA_ARGS__);
|
||||
|
||||
\
|
||||
throw Athena::error::InvalidOperationException(std::string("InvalidOperationException: ") + msg, __FILE__, AT_PRETTY_FUNCTION, __LINE__);
|
||||
\
|
||||
} \
|
||||
|
||||
} while (0)
|
||||
#elif defined(__GNUC__)
|
||||
#define THROW_INVALID_OPERATION_EXCEPTION_RETURN(ret, args...) \
|
||||
|
|
|
@ -3,7 +3,8 @@
|
|||
|
||||
#include "LZBase.hpp"
|
||||
|
||||
class LZType10 : public LZBase {
|
||||
class LZType10 : public LZBase
|
||||
{
|
||||
public:
|
||||
explicit LZType10(atInt32 minimumOffset = 1, atInt32 SlidingWindow = 4096, atInt32 MinimumMatch = 3, atInt32 BlockSize = 8);
|
||||
atUint32 compress(const atUint8* src, atUint8** dest, atUint32 srcLength);
|
||||
|
|
|
@ -4,7 +4,8 @@
|
|||
|
||||
#include "LZBase.hpp"
|
||||
|
||||
class LZType11 : public LZBase {
|
||||
class LZType11 : public LZBase
|
||||
{
|
||||
public:
|
||||
explicit LZType11(atInt32 MinimumOffset = 1, atInt32 SlidingWindow = 4096, atInt32 MinimumMatch = 3, atInt32 BlockSize = 8);
|
||||
atUint32 compress(const atUint8* src, atUint8** dest, atUint32 srcLength);
|
||||
|
|
|
@ -34,11 +34,13 @@ DEALINGS IN THE SOFTWARE.
|
|||
namespace utf8
|
||||
{
|
||||
// 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.
|
||||
class invalid_code_point : public exception {
|
||||
class invalid_code_point : public exception
|
||||
{
|
||||
uint32_t cp;
|
||||
public:
|
||||
invalid_code_point(uint32_t cp) : cp(cp) {}
|
||||
|
@ -46,7 +48,8 @@ public:
|
|||
uint32_t code_point() const {return cp;}
|
||||
};
|
||||
|
||||
class invalid_utf8 : public exception {
|
||||
class invalid_utf8 : public exception
|
||||
{
|
||||
uint8_t u8;
|
||||
public:
|
||||
invalid_utf8(uint8_t u) : u8(u) {}
|
||||
|
@ -54,7 +57,8 @@ public:
|
|||
uint8_t utf8_octet() const {return u8;}
|
||||
};
|
||||
|
||||
class invalid_utf16 : public exception {
|
||||
class invalid_utf16 : public exception
|
||||
{
|
||||
uint16_t u16;
|
||||
public:
|
||||
invalid_utf16(uint16_t u) : u16(u) {}
|
||||
|
@ -62,7 +66,8 @@ public:
|
|||
uint16_t utf16_word() const {return u16;}
|
||||
};
|
||||
|
||||
class not_enough_room : public exception {
|
||||
class not_enough_room : public exception
|
||||
{
|
||||
public:
|
||||
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
|
||||
*(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 & 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 >> 6) & 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 >> 12) & 0x3f) | 0x80);
|
||||
*(result++) = static_cast<uint8_t>(((cp >> 6) & 0x3f) | 0x80);
|
||||
*(result++) = static_cast<uint8_t>((cp & 0x3f) | 0x80);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
template <typename octet_iterator, typename output_iterator>
|
||||
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;
|
||||
internal::utf_error err_code = utf8::internal::validate_next(start, end);
|
||||
switch (err_code) {
|
||||
|
||||
switch (err_code)
|
||||
{
|
||||
case internal::UTF8_OK :
|
||||
for (octet_iterator it = sequence_start; it != start; ++it)
|
||||
*out++ = *it;
|
||||
|
||||
break;
|
||||
|
||||
case internal::NOT_ENOUGH_ROOM:
|
||||
throw not_enough_room();
|
||||
|
||||
case internal::INVALID_LEAD:
|
||||
utf8::append(replacement, out);
|
||||
++start;
|
||||
break;
|
||||
|
||||
case internal::INCOMPLETE_SEQUENCE:
|
||||
case internal::OVERLONG_SEQUENCE:
|
||||
case internal::INVALID_CODE_POINT:
|
||||
utf8::append(replacement, out);
|
||||
++start;
|
||||
|
||||
// just one replacement mark for the sequence
|
||||
while (start != end && utf8::internal::is_trail(*start))
|
||||
++start;
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
|
@ -138,18 +157,24 @@ uint32_t next(octet_iterator& it, octet_iterator end)
|
|||
{
|
||||
uint32_t cp = 0;
|
||||
internal::utf_error err_code = utf8::internal::validate_next(it, end, cp);
|
||||
switch (err_code) {
|
||||
|
||||
switch (err_code)
|
||||
{
|
||||
case internal::UTF8_OK :
|
||||
break;
|
||||
|
||||
case internal::NOT_ENOUGH_ROOM :
|
||||
throw not_enough_room();
|
||||
|
||||
case internal::INVALID_LEAD :
|
||||
case internal::INCOMPLETE_SEQUENCE :
|
||||
case internal::OVERLONG_SEQUENCE :
|
||||
throw invalid_utf8(*it);
|
||||
|
||||
case internal::INVALID_CODE_POINT :
|
||||
throw invalid_code_point(cp);
|
||||
}
|
||||
|
||||
return cp;
|
||||
}
|
||||
|
||||
|
@ -167,10 +192,12 @@ uint32_t prior(octet_iterator& it, octet_iterator start)
|
|||
throw not_enough_room();
|
||||
|
||||
octet_iterator end = it;
|
||||
|
||||
// Go back until we hit either a lead octet or start
|
||||
while (utf8::internal::is_trail(*(--it)))
|
||||
if (it == start)
|
||||
throw invalid_utf8(*it); // error - no lead byte in the sequence
|
||||
|
||||
return utf8::peek_next(it, end);
|
||||
}
|
||||
|
||||
|
@ -179,9 +206,11 @@ template <typename octet_iterator>
|
|||
uint32_t previous(octet_iterator& it, octet_iterator pass_start)
|
||||
{
|
||||
octet_iterator end = it;
|
||||
|
||||
while (utf8::internal::is_trail(*(--it)))
|
||||
if (it == pass_start)
|
||||
throw invalid_utf8(*it); // error - no lead byte in the sequence
|
||||
|
||||
octet_iterator temp = it;
|
||||
return utf8::next(temp, end);
|
||||
}
|
||||
|
@ -198,20 +227,27 @@ typename std::iterator_traits<octet_iterator>::difference_type
|
|||
distance(octet_iterator first, octet_iterator last)
|
||||
{
|
||||
typename std::iterator_traits<octet_iterator>::difference_type dist;
|
||||
|
||||
for (dist = 0; first < last; ++dist)
|
||||
utf8::next(first, last);
|
||||
|
||||
return dist;
|
||||
}
|
||||
|
||||
template <typename u16bit_iterator, typename octet_iterator>
|
||||
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++);
|
||||
|
||||
// Take care of surrogate pairs first
|
||||
if (utf8::internal::is_lead_surrogate(cp)) {
|
||||
if (start != end) {
|
||||
if (utf8::internal::is_lead_surrogate(cp))
|
||||
{
|
||||
if (start != end)
|
||||
{
|
||||
uint32_t trail_surrogate = utf8::internal::mask16(*start++);
|
||||
|
||||
if (utf8::internal::is_trail_surrogate(trail_surrogate))
|
||||
cp = (cp << 10) + trail_surrogate + internal::SURROGATE_OFFSET;
|
||||
else
|
||||
|
@ -227,21 +263,26 @@ octet_iterator utf16to8 (u16bit_iterator start, u16bit_iterator end, octet_itera
|
|||
|
||||
result = utf8::append(cp, result);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
template <typename u16bit_iterator, typename octet_iterator>
|
||||
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);
|
||||
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 & 0x3ff) + internal::TRAIL_SURROGATE_MIN);
|
||||
}
|
||||
else
|
||||
*result++ = static_cast<uint16_t>(cp);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -265,7 +306,8 @@ u32bit_iterator utf8to32 (octet_iterator start, octet_iterator end, u32bit_itera
|
|||
|
||||
// The iterator class
|
||||
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 range_start;
|
||||
octet_iterator range_end;
|
||||
|
@ -290,6 +332,7 @@ public:
|
|||
{
|
||||
if (range_start != rhs.range_start || range_end != rhs.range_end)
|
||||
throw std::logic_error("Comparing utf-8 iterators defined with different ranges");
|
||||
|
||||
return (it == rhs.it);
|
||||
}
|
||||
bool operator != (const iterator& rhs) const
|
||||
|
|
|
@ -100,6 +100,7 @@ inline typename std::iterator_traits<octet_iterator>::difference_type
|
|||
sequence_length(octet_iterator lead_it)
|
||||
{
|
||||
uint8_t lead = utf8::internal::mask8(*lead_it);
|
||||
|
||||
if (lead < 0x80)
|
||||
return 1;
|
||||
else if ((lead >> 5) == 0x6)
|
||||
|
@ -115,15 +116,18 @@ sequence_length(octet_iterator lead_it)
|
|||
template <typename octet_difference_type>
|
||||
inline bool is_overlong_sequence(uint32_t cp, octet_difference_type length)
|
||||
{
|
||||
if (cp < 0x80) {
|
||||
if (cp < 0x80)
|
||||
{
|
||||
if (length != 1)
|
||||
return true;
|
||||
}
|
||||
else if (cp < 0x800) {
|
||||
else if (cp < 0x800)
|
||||
{
|
||||
if (length != 2)
|
||||
return true;
|
||||
}
|
||||
else if (cp < 0x10000) {
|
||||
else if (cp < 0x10000)
|
||||
{
|
||||
if (length != 3)
|
||||
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
|
||||
utf_error err = UTF8_OK;
|
||||
switch (length) {
|
||||
|
||||
switch (length)
|
||||
{
|
||||
case 0:
|
||||
return INVALID_LEAD;
|
||||
|
||||
case 1:
|
||||
err = utf8::internal::get_sequence_1(it, end, cp);
|
||||
break;
|
||||
|
||||
case 2:
|
||||
err = utf8::internal::get_sequence_2(it, end, cp);
|
||||
break;
|
||||
|
||||
case 3:
|
||||
err = utf8::internal::get_sequence_3(it, end, cp);
|
||||
break;
|
||||
|
||||
case 4:
|
||||
err = utf8::internal::get_sequence_4(it, end, cp);
|
||||
break;
|
||||
}
|
||||
|
||||
if (err == UTF8_OK) {
|
||||
if (err == UTF8_OK)
|
||||
{
|
||||
// Decoding succeeded. Now, security checks...
|
||||
if (utf8::internal::is_code_point_valid(cp)) {
|
||||
if (!utf8::internal::is_overlong_sequence(cp, length)){
|
||||
if (utf8::internal::is_code_point_valid(cp))
|
||||
{
|
||||
if (!utf8::internal::is_overlong_sequence(cp, length))
|
||||
{
|
||||
// Passed! Return here.
|
||||
code_point = cp;
|
||||
++it;
|
||||
|
@ -272,7 +285,8 @@ utf_error validate_next(octet_iterator& it, octet_iterator end, uint32_t& code_p
|
|||
}
|
||||
|
||||
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;
|
||||
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 result = start;
|
||||
while (result != end) {
|
||||
|
||||
while (result != end)
|
||||
{
|
||||
utf8::internal::utf_error err_code = utf8::internal::validate_next(result, end);
|
||||
|
||||
if (err_code != internal::UTF8_OK)
|
||||
return result;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
|
@ -39,21 +39,25 @@ octet_iterator append(uint32_t cp, octet_iterator result)
|
|||
{
|
||||
if (cp < 0x80) // one octet
|
||||
*(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 & 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 >> 6) & 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 >> 12) & 0x3f) | 0x80);
|
||||
*(result++) = static_cast<uint8_t>(((cp >> 6) & 0x3f) | 0x80);
|
||||
*(result++) = static_cast<uint8_t>((cp & 0x3f) | 0x80);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -62,19 +66,24 @@ uint32_t next(octet_iterator& it)
|
|||
{
|
||||
uint32_t cp = utf8::internal::mask8(*it);
|
||||
typename std::iterator_traits<octet_iterator>::difference_type length = utf8::internal::sequence_length(it);
|
||||
switch (length) {
|
||||
|
||||
switch (length)
|
||||
{
|
||||
case 1:
|
||||
break;
|
||||
|
||||
case 2:
|
||||
it++;
|
||||
cp = ((cp << 6) & 0x7ff) + ((*it) & 0x3f);
|
||||
break;
|
||||
|
||||
case 3:
|
||||
++it;
|
||||
cp = ((cp << 12) & 0xffff) + ((utf8::internal::mask8(*it) << 6) & 0xfff);
|
||||
++it;
|
||||
cp += (*it) & 0x3f;
|
||||
break;
|
||||
|
||||
case 4:
|
||||
++it;
|
||||
cp = ((cp << 18) & 0x1fffff) + ((utf8::internal::mask8(*it) << 12) & 0x3ffff);
|
||||
|
@ -84,6 +93,7 @@ uint32_t next(octet_iterator& it)
|
|||
cp += (*it) & 0x3f;
|
||||
break;
|
||||
}
|
||||
|
||||
++it;
|
||||
return cp;
|
||||
}
|
||||
|
@ -98,6 +108,7 @@ template <typename octet_iterator>
|
|||
uint32_t prior(octet_iterator& it)
|
||||
{
|
||||
while (utf8::internal::is_trail(*(--it))) ;
|
||||
|
||||
octet_iterator temp = it;
|
||||
return utf8::unchecked::next(temp);
|
||||
}
|
||||
|
@ -121,38 +132,49 @@ typename std::iterator_traits<octet_iterator>::difference_type
|
|||
distance(octet_iterator first, octet_iterator last)
|
||||
{
|
||||
typename std::iterator_traits<octet_iterator>::difference_type dist;
|
||||
|
||||
for (dist = 0; first < last; ++dist)
|
||||
utf8::unchecked::next(first);
|
||||
|
||||
return dist;
|
||||
}
|
||||
|
||||
template <typename u16bit_iterator, typename octet_iterator>
|
||||
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++);
|
||||
|
||||
// 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++);
|
||||
cp = (cp << 10) + trail_surrogate + internal::SURROGATE_OFFSET;
|
||||
}
|
||||
|
||||
result = utf8::unchecked::append(cp, result);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
template <typename u16bit_iterator, typename octet_iterator>
|
||||
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);
|
||||
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 & 0x3ff) + internal::TRAIL_SURROGATE_MIN);
|
||||
}
|
||||
else
|
||||
*result++ = static_cast<uint16_t>(cp);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -176,7 +198,8 @@ u32bit_iterator utf8to32 (octet_iterator start, octet_iterator end, u32bit_itera
|
|||
|
||||
// The iterator class
|
||||
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;
|
||||
public:
|
||||
iterator() {};
|
||||
|
|
|
@ -53,13 +53,16 @@ ALTTPFile* ALTTPFileReader::readFile()
|
|||
std::vector<atUint16> dungeonDeaths;
|
||||
|
||||
int j = 0x140;
|
||||
|
||||
while ((j--) > 0)
|
||||
{
|
||||
roomFlags.push_back(readRoomFlags());
|
||||
}
|
||||
|
||||
quest->setRoomFlags(roomFlags);
|
||||
|
||||
j = 0x0C0;
|
||||
|
||||
while ((j--) > 0)
|
||||
owEvents.push_back(readOverworldEvent());
|
||||
|
||||
|
@ -108,6 +111,7 @@ ALTTPFile* ALTTPFileReader::readFile()
|
|||
quest->setMagicUsage((ALTTPMagicUsage&)*base::readBytes(sizeof(ALTTPMagicUsage)));
|
||||
|
||||
j = 0x10;
|
||||
|
||||
while ((j--) > 0)
|
||||
{
|
||||
dungeonKeys.push_back(base::readByte());
|
||||
|
@ -125,6 +129,7 @@ ALTTPFile* ALTTPFileReader::readFile()
|
|||
quest->setTagAlong((ALTTPTagAlong)base::readByte());
|
||||
|
||||
j = 6;
|
||||
|
||||
while ((j--) > 0)
|
||||
{
|
||||
oldmanFlags.push_back(base::readByte());
|
||||
|
@ -134,6 +139,7 @@ ALTTPFile* ALTTPFileReader::readFile()
|
|||
quest->setBombFlag(base::readByte());
|
||||
|
||||
j = 5;
|
||||
|
||||
while ((j--) > 0)
|
||||
{
|
||||
unknown1.push_back(base::readByte());
|
||||
|
@ -142,6 +148,7 @@ ALTTPFile* ALTTPFileReader::readFile()
|
|||
quest->setUnknown1(unknown1);
|
||||
|
||||
j = 6;
|
||||
|
||||
while ((j--) > 0)
|
||||
{
|
||||
playerName.push_back(base::readUint16());
|
||||
|
@ -151,10 +158,12 @@ ALTTPFile* ALTTPFileReader::readFile()
|
|||
quest->setValid((base::readUint16() == 0x55AA));
|
||||
|
||||
j = 0x0D;
|
||||
|
||||
while ((j--) > 0)
|
||||
{
|
||||
dungeonDeaths.push_back(base::readUint16());
|
||||
}
|
||||
|
||||
quest->setDungeonDeathTotals(dungeonDeaths);
|
||||
|
||||
quest->setUnknown2(base::readUint16());
|
||||
|
|
|
@ -38,6 +38,7 @@ ALTTPFileWriter::ALTTPFileWriter(const std::string& filename)
|
|||
void ALTTPFileWriter::writeFile(ALTTPFile* file)
|
||||
{
|
||||
ALTTPQuest* quest = NULL;
|
||||
|
||||
for (atUint32 i = 0; i < 6; i++)
|
||||
{
|
||||
if (i < 3)
|
||||
|
@ -204,6 +205,7 @@ atUint16 ALTTPFileWriter::calculateChecksum(atUint32 game)
|
|||
|
||||
// First we start at 0
|
||||
atUint16 sum = 0;
|
||||
|
||||
for (atUint32 i = 0; i < 0x4FE; i += 2)
|
||||
// Add each word one by one
|
||||
sum += *(atUint16*)(m_data + (i + (0x500 * game)));
|
||||
|
|
|
@ -73,6 +73,7 @@ ALTTPOverworldEvent* ALTTPQuest::overworldEvent(atUint32 id) const
|
|||
{
|
||||
if (id > m_overworldEvents.size() - 1)
|
||||
THROW_INVALID_OPERATION_EXCEPTION_RETURN(nullptr, "index out of range");
|
||||
|
||||
return m_overworldEvents[id];
|
||||
}
|
||||
|
||||
|
@ -472,25 +473,30 @@ void ALTTPQuest::setPlayerName(const std::string& playerName)
|
|||
m_playerName.push_back((atUint16)0xA9);
|
||||
continue;
|
||||
}
|
||||
|
||||
char c = playerName[i];
|
||||
|
||||
if (c >= 'A' && c <= 'P' && c != 'I')
|
||||
{
|
||||
|
||||
m_playerName.push_back((atUint16)(c - 'A'));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (c >= 'Q' && c <= 'Z')
|
||||
{
|
||||
std::cout << std::hex << (atUint16)((c - 'Q') + 0x20) << std::endl;
|
||||
m_playerName.push_back((atUint16)((c - 'Q') + 0x20));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (c >= 'a' && c <= 'f')
|
||||
{
|
||||
std::cout << std::hex << (atUint16)((c - 'a') + 0x2A) << std::endl;
|
||||
m_playerName.push_back((atUint16)((c - 'a') + 0x2A));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (c >= 'g' && c <= 'v')
|
||||
{
|
||||
if (c == 'k')
|
||||
|
@ -498,24 +504,29 @@ void ALTTPQuest::setPlayerName(const std::string& playerName)
|
|||
m_playerName.push_back(0x42);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (c == 'i')
|
||||
{
|
||||
m_playerName.push_back(0x44);
|
||||
continue;
|
||||
}
|
||||
|
||||
m_playerName.push_back((atUint16)((c - 'g') + 0x40));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (c >= 'w' && c <= 'z')
|
||||
{
|
||||
m_playerName.push_back((atUint16)((c - 'w') + 0x60));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (c >= '0' && c <= '9')
|
||||
{
|
||||
m_playerName.push_back((atUint16)((c - '0') + 0x64));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (c == '-' || c == '.')
|
||||
{
|
||||
m_playerName.push_back((atUint16)(c - '-') + 0x80);
|
||||
|
@ -524,12 +535,29 @@ void ALTTPQuest::setPlayerName(const std::string& playerName)
|
|||
|
||||
switch (c)
|
||||
{
|
||||
case '?' : m_playerName.push_back(0x6E); break;
|
||||
case '!' : 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;
|
||||
case '?' :
|
||||
m_playerName.push_back(0x6E);
|
||||
break;
|
||||
|
||||
case '!' :
|
||||
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));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (c >= 0x20 && c <= 0x29)
|
||||
{
|
||||
ret.push_back((char)('Q' + (c - 0x20)));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (c >= 0x2A && c <= 0x2F)
|
||||
{
|
||||
ret.push_back((char)('a' + (c - 0x2A)));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (c >= 0x40 && c <= 0x4F)
|
||||
{
|
||||
if (c == 0x42)
|
||||
|
@ -567,23 +598,28 @@ std::string ALTTPQuest::playerNameToString() const
|
|||
ret.push_back('k');
|
||||
continue;
|
||||
}
|
||||
|
||||
if (c == 0x44)
|
||||
{
|
||||
ret.push_back('i');
|
||||
continue;
|
||||
}
|
||||
|
||||
ret.push_back((char)('g' + (c - 0x40)));
|
||||
}
|
||||
|
||||
if (c >= 0x60 && c <= 0x63)
|
||||
{
|
||||
ret.push_back((char)('w' + (c - 0x60)));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (c >= 0x64 && c <= 0x6D)
|
||||
{
|
||||
ret.push_back((char)('0' + (c - 0x64)));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (c == 0x80 || c == 0x81)
|
||||
{
|
||||
ret.push_back((char)('-' + (c - 0x80)));
|
||||
|
@ -592,14 +628,32 @@ std::string ALTTPQuest::playerNameToString() const
|
|||
|
||||
switch (c)
|
||||
{
|
||||
case 0x6E: ret.push_back('?'); break;
|
||||
case 0x6F: 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;
|
||||
case 0x6E:
|
||||
ret.push_back('?');
|
||||
break;
|
||||
|
||||
case 0x6F:
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
|
@ -157,8 +157,10 @@ atUint16 crc16(const atUint8* data, atUint64 length)
|
|||
|
||||
atInt32 pos = 0;
|
||||
atUint16 checksum = 0;
|
||||
|
||||
while (length--)
|
||||
checksum = (crc16Table[(checksum ^ data[pos++]) & 0xFF] ^ (checksum >> 8));
|
||||
|
||||
return checksum;
|
||||
}
|
||||
|
||||
|
|
|
@ -44,9 +44,11 @@ atInt32 decompressZlib(const atUint8* src, atUint32 srcLen, atUint8* dst, atUint
|
|||
atInt32 ret = -1;
|
||||
|
||||
err = inflateInit(&strm); //15 window bits, and the +32 tells zlib to to detect if using gzip or zlib
|
||||
|
||||
if (err == Z_OK)
|
||||
{
|
||||
err = inflate(&strm, Z_FINISH);
|
||||
|
||||
if (err == Z_STREAM_END)
|
||||
ret = strm.total_out;
|
||||
else
|
||||
|
@ -86,6 +88,7 @@ atInt32 compressZlib(const atUint8 *src, atUint32 srcLen, atUint8* dst, atUint32
|
|||
if (err == Z_OK)
|
||||
{
|
||||
err = deflate(&strm, Z_FINISH);
|
||||
|
||||
if (err == Z_STREAM_END)
|
||||
ret = strm.total_out;
|
||||
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
|
||||
atUint8 currCodeByte;
|
||||
|
||||
while (dstPlace < uncompressedSize)
|
||||
{
|
||||
//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 numBytes = byte1 >> 4;
|
||||
|
||||
if (numBytes == 0)
|
||||
{
|
||||
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
|
||||
atUint8 currCodeByte = 0;
|
||||
|
||||
while (r.srcPos < srcSize)
|
||||
{
|
||||
atUint32 numBytes;
|
||||
atUint32 matchPos;
|
||||
numBytes = nintendoEnc(src, srcSize, r.srcPos, &matchPos);
|
||||
|
||||
if (numBytes < 3)
|
||||
{
|
||||
//straight copy
|
||||
|
@ -222,9 +229,11 @@ atUint32 yaz0Encode(const atUint8* src, atUint32 srcSize, atUint8* data)
|
|||
byte2 = dist & 0xff;
|
||||
dst[r.dstPos++] = byte1;
|
||||
dst[r.dstPos++] = byte2;
|
||||
|
||||
// maximum runlength for 3 byte encoding
|
||||
if (numBytes > 0xff + 0x12)
|
||||
numBytes = 0xff + 0x12;
|
||||
|
||||
byte3 = numBytes - 0x12;
|
||||
dst[r.dstPos++] = byte3;
|
||||
}
|
||||
|
@ -235,16 +244,21 @@ atUint32 yaz0Encode(const atUint8* src, atUint32 srcSize, atUint8* data)
|
|||
dst[r.dstPos++] = byte1;
|
||||
dst[r.dstPos++] = byte2;
|
||||
}
|
||||
|
||||
r.srcPos += numBytes;
|
||||
}
|
||||
|
||||
validBitCount++;
|
||||
|
||||
//write eight codes
|
||||
if (validBitCount == 8)
|
||||
{
|
||||
data[pos] = currCodeByte;
|
||||
pos++;
|
||||
|
||||
for (i = 0; i </*=*/r.dstPos; pos++, i++)
|
||||
data[pos] = dst[i];
|
||||
|
||||
dstSize += r.dstPos + 1;
|
||||
|
||||
currCodeByte = 0;
|
||||
|
@ -252,12 +266,15 @@ atUint32 yaz0Encode(const atUint8* src, atUint32 srcSize, atUint8* data)
|
|||
r.dstPos = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (validBitCount > 0)
|
||||
{
|
||||
data[pos] = currCodeByte;
|
||||
pos++;
|
||||
|
||||
for (i = 0; i </*=*/r.dstPos; pos++, i++)
|
||||
data[pos] = dst[i];
|
||||
|
||||
dstSize += r.dstPos + 1;
|
||||
|
||||
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.
|
||||
// so just use it. this is not the best optimization, but nintendo's choice for speed.
|
||||
if (prevFlag == 1) {
|
||||
if (prevFlag == 1)
|
||||
{
|
||||
*pMatchPos = matchPos;
|
||||
prevFlag = 0;
|
||||
return numBytes1;
|
||||
}
|
||||
|
||||
prevFlag = 0;
|
||||
numBytes = simpleEnc(src, size, pos, &matchPos);
|
||||
*pMatchPos = matchPos;
|
||||
|
||||
// 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);
|
||||
|
||||
// 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.
|
||||
if (numBytes1 >= numBytes+2) {
|
||||
if (numBytes1 >= numBytes + 2)
|
||||
{
|
||||
numBytes = 1;
|
||||
prevFlag = 1;
|
||||
}
|
||||
}
|
||||
|
||||
return numBytes;
|
||||
}
|
||||
|
||||
|
@ -309,6 +332,7 @@ atUint32 simpleEnc(const atUint8* src, atInt32 size, atInt32 pos, atUint32 *pMat
|
|||
|
||||
if (startPos < 0)
|
||||
startPos = 0;
|
||||
|
||||
for (i = startPos; i < pos; i++)
|
||||
{
|
||||
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])
|
||||
break;
|
||||
}
|
||||
|
||||
if ((atUint32)j > numBytes)
|
||||
{
|
||||
numBytes = j;
|
||||
matchPos = i;
|
||||
}
|
||||
}
|
||||
|
||||
*pMatchPos = matchPos;
|
||||
|
||||
if (numBytes == 2)
|
||||
numBytes = 1;
|
||||
|
||||
return numBytes;
|
||||
}
|
||||
|
||||
atUint32 decompressLZ77(const atUint8* src, atUint32 srcLen, atUint8** dst)
|
||||
{
|
||||
LZBase* lzCodec;
|
||||
|
||||
if (*(atUint8*)src == 0x11)
|
||||
lzCodec = new LZType11;
|
||||
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)
|
||||
{
|
||||
LZBase* lzCodec;
|
||||
|
||||
if (extended)
|
||||
lzCodec = new LZType11;
|
||||
else
|
||||
|
|
|
@ -72,6 +72,7 @@ bool FileReader::isLittleEndian() const
|
|||
void FileReader::open()
|
||||
{
|
||||
m_fileHandle = fopen(m_filename.c_str(), "rb");
|
||||
|
||||
if (!m_fileHandle)
|
||||
THROW_FILE_NOT_FOUND_EXCEPTION(m_filename);
|
||||
|
||||
|
@ -143,6 +144,7 @@ bool FileReader::readBit()
|
|||
if (!m_bitValid)
|
||||
{
|
||||
size_t size = fread(&m_currentByte, 1, 1, m_fileHandle);
|
||||
|
||||
if (size != sizeof(atUint8))
|
||||
THROW_IO_EXCEPTION_RETURN(false, "Error reading from file.");
|
||||
|
||||
|
@ -152,6 +154,7 @@ bool FileReader::readBit()
|
|||
|
||||
atUint8 flag = (1 << m_bitShift);
|
||||
m_bitShift++;
|
||||
|
||||
if (m_bitShift > 7)
|
||||
m_bitValid = false;
|
||||
|
||||
|
@ -173,6 +176,7 @@ atInt8 FileReader::readByte()
|
|||
{
|
||||
if (!isOpen())
|
||||
THROW_INVALID_OPERATION_EXCEPTION_RETURN(0, "File not open for reading");
|
||||
|
||||
return (atInt8)readUByte();
|
||||
}
|
||||
|
||||
|
@ -180,6 +184,7 @@ atUint8* FileReader::readUBytes(atUint64 len)
|
|||
{
|
||||
if (!isOpen())
|
||||
THROW_INVALID_OPERATION_EXCEPTION_RETURN(nullptr, "File not open for reading");
|
||||
|
||||
m_bitValid = false;
|
||||
atUint8* val = new atUint8[len];
|
||||
fread(val, 1, len, m_fileHandle);
|
||||
|
@ -190,6 +195,7 @@ atUint64 FileReader::readUBytesToBuf(void* buf, atUint64 len)
|
|||
{
|
||||
if (!isOpen())
|
||||
THROW_INVALID_OPERATION_EXCEPTION_RETURN(0, "File not open for reading");
|
||||
|
||||
m_bitValid = false;
|
||||
return fread(buf, 1, len, m_fileHandle);
|
||||
}
|
||||
|
@ -198,6 +204,7 @@ atInt8* FileReader::readBytes(atUint64 len)
|
|||
{
|
||||
if (!isOpen())
|
||||
THROW_INVALID_OPERATION_EXCEPTION_RETURN(nullptr, "File not open for reading");
|
||||
|
||||
return (atInt8*)readUBytes(len);
|
||||
}
|
||||
|
||||
|
@ -314,6 +321,7 @@ std::string FileReader::readString(atInt32 maxlen)
|
|||
atUint8 chr = readByte();
|
||||
|
||||
atInt32 i = 0;
|
||||
|
||||
while (chr != 0)
|
||||
{
|
||||
if (maxlen >= 0 && i >= maxlen - 1)
|
||||
|
@ -336,20 +344,24 @@ std::string FileReader::readUnicode(atInt32 maxlen)
|
|||
std::vector<short> tmp;
|
||||
|
||||
atInt32 i = 0;
|
||||
|
||||
for (;;)
|
||||
{
|
||||
if (maxlen >= 0 && i >= maxlen - 1)
|
||||
break;
|
||||
|
||||
short chr = readUint16();
|
||||
|
||||
if (chr)
|
||||
tmp.push_back(chr);
|
||||
else
|
||||
break;
|
||||
|
||||
i++;
|
||||
};
|
||||
|
||||
utf8::utf16to8(tmp.begin(), tmp.end(), back_inserter(ret));
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
|
|
@ -71,6 +71,7 @@ bool FileWriter::isLittleEndian() const
|
|||
void FileWriter::open()
|
||||
{
|
||||
m_fileHandle = fopen(m_filename.c_str(), "w+b");
|
||||
|
||||
if (!m_fileHandle)
|
||||
THROW_FILE_NOT_FOUND_EXCEPTION(m_filename);
|
||||
|
||||
|
@ -141,6 +142,7 @@ void FileWriter::writeBit(bool val)
|
|||
m_bitValid = false;
|
||||
|
||||
fseeko64(m_fileHandle, m_bytePosition, (int)SeekOrigin::Begin);
|
||||
|
||||
if (fwrite(&m_currentByte, 1, 1, m_fileHandle) != sizeof(atInt8))
|
||||
THROW_IO_EXCEPTION("Unable to data to file");
|
||||
}
|
||||
|
@ -149,6 +151,7 @@ void FileWriter::seekBit(int bit)
|
|||
{
|
||||
if (bit < 0 || bit > 7)
|
||||
THROW_INVALID_OPERATION_EXCEPTION("bit must be >= 0 and <= 7");
|
||||
|
||||
m_bitShift = bit;
|
||||
m_bitValid = true;
|
||||
}
|
||||
|
@ -283,8 +286,10 @@ void FileWriter::writeString(const std::string& val)
|
|||
m_bitValid = false;
|
||||
|
||||
char term = '\0';
|
||||
|
||||
if (fwrite(val.c_str(), 1, val.length(), m_fileHandle) != val.length())
|
||||
THROW_IO_EXCEPTION("Unable to write to stream");
|
||||
|
||||
if (fwrite(&term, 1, 1, m_fileHandle) != 1)
|
||||
THROW_IO_EXCEPTION("Unable to write to stream");
|
||||
}
|
||||
|
@ -311,6 +316,7 @@ void FileWriter::fill(atInt8 byte, atUint64 len)
|
|||
{
|
||||
if (!isOpen())
|
||||
THROW_INVALID_OPERATION_EXCEPTION("File not open for writing");
|
||||
|
||||
fwrite(&byte, 1, len, m_fileHandle);
|
||||
}
|
||||
|
||||
|
|
|
@ -22,13 +22,16 @@ std::ostream& operator<<(std::ostream& os, const Athena::SeekOrigin& origin)
|
|||
case Athena::SeekOrigin::Begin:
|
||||
os << "Begin";
|
||||
break;
|
||||
|
||||
case Athena::SeekOrigin::Current:
|
||||
os << "Current";
|
||||
break;
|
||||
|
||||
case Athena::SeekOrigin::End:
|
||||
os << "End";
|
||||
break;
|
||||
}
|
||||
|
||||
return os;
|
||||
}
|
||||
|
||||
|
@ -40,10 +43,12 @@ std::ostream& operator<<(std::ostream& os, const Athena::Endian& endian)
|
|||
case Athena::Endian::LittleEndian:
|
||||
os << "LittleEndian";
|
||||
break;
|
||||
|
||||
case Athena::Endian::BigEndian:
|
||||
os << "BigEndian";
|
||||
break;
|
||||
}
|
||||
|
||||
return os;
|
||||
}
|
||||
|
||||
|
|
|
@ -43,6 +43,7 @@ MemoryReader::MemoryReader(const atUint8* data, atUint64 length)
|
|||
{
|
||||
if (!data)
|
||||
THROW_INVALID_DATA_EXCEPTION("data cannot be NULL");
|
||||
|
||||
if (length == 0)
|
||||
THROW_INVALID_OPERATION_EXCEPTION("length cannot be 0");
|
||||
|
||||
|
@ -100,16 +101,21 @@ void MemoryReader::seek(atInt64 position, SeekOrigin origin)
|
|||
case SeekOrigin::Begin:
|
||||
if ((position < 0 || (atInt64)position > (atInt64)m_length))
|
||||
THROW_IO_EXCEPTION("Position %0.8X outside stream bounds ", position);
|
||||
|
||||
m_position = position;
|
||||
break;
|
||||
|
||||
case SeekOrigin::Current:
|
||||
if ((((atInt64)m_position + position) < 0 || (m_position + position) > m_length))
|
||||
THROW_IO_EXCEPTION("Position %0.8X outside stream bounds ", position);
|
||||
|
||||
m_position += position;
|
||||
break;
|
||||
|
||||
case SeekOrigin::End:
|
||||
if ((((atInt64)m_length - position < 0) || (m_length - position) > m_length))
|
||||
THROW_IO_EXCEPTION("Position %0.8X outside stream bounds ", position);
|
||||
|
||||
m_position = m_length - position;
|
||||
break;
|
||||
}
|
||||
|
@ -174,12 +180,14 @@ bool MemoryReader::readBit()
|
|||
{
|
||||
if (!m_data)
|
||||
loadData();
|
||||
|
||||
if (m_position > m_length)
|
||||
THROW_IO_EXCEPTION_RETURN(false, "Position %0.8X outside stream bounds ", m_position);
|
||||
|
||||
bool ret = (*(atUint8*)(m_data + m_position) & (1 << m_bitPosition)) != 0;
|
||||
|
||||
m_bitPosition++;
|
||||
|
||||
if (m_bitPosition > 7)
|
||||
{
|
||||
m_bitPosition = 0;
|
||||
|
@ -199,6 +207,7 @@ atInt8 MemoryReader::readByte()
|
|||
m_bitPosition = 0;
|
||||
m_position += sizeof(atUint8);
|
||||
}
|
||||
|
||||
if (m_position + 1 > m_length)
|
||||
THROW_IO_EXCEPTION_RETURN(0, "Position %0.8X outside stream bounds ", m_position);
|
||||
|
||||
|
@ -218,6 +227,7 @@ atUint8 MemoryReader::readUByte()
|
|||
m_bitPosition = 0;
|
||||
m_position += sizeof(atUint8);
|
||||
}
|
||||
|
||||
if (m_position + 1 > m_length)
|
||||
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)
|
||||
THROW_IO_EXCEPTION_RETURN(0, "Position %0.8X outside stream bounds ", m_position);
|
||||
|
||||
atInt16 ret = *(atInt16*)(m_data + m_position);
|
||||
m_position += sizeof(atInt16);
|
||||
|
||||
|
@ -307,6 +318,7 @@ atInt32 MemoryReader::readInt32()
|
|||
|
||||
if (m_position + sizeof(atInt32) > m_length)
|
||||
THROW_IO_EXCEPTION_RETURN(0, "Position %0.8X outside stream bounds ", m_position);
|
||||
|
||||
atInt32 ret = *(atInt32*)(m_data + m_position);
|
||||
m_position += 4;
|
||||
|
||||
|
@ -333,6 +345,7 @@ atInt64 MemoryReader::readInt64()
|
|||
m_bitPosition = 0;
|
||||
m_position += sizeof(atUint8);
|
||||
}
|
||||
|
||||
if (m_position + sizeof(atInt64) > m_length)
|
||||
THROW_IO_EXCEPTION_RETURN(0, "Position %0.8X outside stream bounds ", m_position);
|
||||
|
||||
|
@ -362,6 +375,7 @@ float MemoryReader::readFloat()
|
|||
m_bitPosition = 0;
|
||||
m_position += sizeof(atUint8);
|
||||
}
|
||||
|
||||
if (m_position + sizeof(float) > m_length)
|
||||
THROW_IO_EXCEPTION_RETURN(0, "Position %0.8X outside stream bounds ", m_position);
|
||||
|
||||
|
@ -386,6 +400,7 @@ double MemoryReader::readDouble()
|
|||
m_bitPosition = 0;
|
||||
m_position += sizeof(atUint8);
|
||||
}
|
||||
|
||||
if (m_position + sizeof(double) > m_length)
|
||||
THROW_IO_EXCEPTION_RETURN(0, "Position %0.8X outside stream bounds ", m_position);
|
||||
|
||||
|
@ -396,6 +411,7 @@ double MemoryReader::readDouble()
|
|||
utility::BigDouble(ret);
|
||||
else
|
||||
utility::LittleDouble(ret);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
@ -409,6 +425,7 @@ bool MemoryReader::readBool()
|
|||
m_bitPosition = 0;
|
||||
m_position += sizeof(atUint8);
|
||||
}
|
||||
|
||||
if (m_position + sizeof(bool) > m_length)
|
||||
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)
|
||||
loadData();
|
||||
|
||||
std::string ret;
|
||||
std::vector<short> tmp;
|
||||
atUint16 chr = readUint16();
|
||||
|
||||
atInt32 i = 0;
|
||||
|
||||
for (;;)
|
||||
{
|
||||
if (maxlen >= 0 && i >= maxlen - 1)
|
||||
|
@ -433,6 +452,7 @@ std::string MemoryReader::readUnicode(atInt32 maxlen)
|
|||
|
||||
if (!chr)
|
||||
break;
|
||||
|
||||
tmp.push_back(chr);
|
||||
chr = readUint16();
|
||||
i++;
|
||||
|
@ -448,6 +468,7 @@ std::string MemoryReader::readString(atInt32 maxlen)
|
|||
atUint8 chr = readByte();
|
||||
|
||||
atInt32 i = 0;
|
||||
|
||||
while (chr != 0)
|
||||
{
|
||||
if (maxlen >= 0 && i >= maxlen - 1)
|
||||
|
@ -474,6 +495,7 @@ void MemoryReader::loadData()
|
|||
|
||||
if (!in)
|
||||
THROW_FILE_NOT_FOUND_EXCEPTION(m_filepath);
|
||||
|
||||
rewind(in);
|
||||
|
||||
length = utility::fileSize(m_filepath);
|
||||
|
@ -485,6 +507,7 @@ void MemoryReader::loadData()
|
|||
|
||||
atUint64 done = 0;
|
||||
atUint64 blocksize = BLOCKSZ;
|
||||
|
||||
do
|
||||
{
|
||||
if (blocksize > length - done)
|
||||
|
@ -502,7 +525,8 @@ void MemoryReader::loadData()
|
|||
if (m_progressCallback)
|
||||
m_progressCallback((int)((float)(done * 100.f) / length));
|
||||
|
||||
} while (done < length);
|
||||
}
|
||||
while (done < length);
|
||||
|
||||
fclose(in);
|
||||
m_length = length;
|
||||
|
|
|
@ -106,8 +106,10 @@ void MemoryWriter::seek(atInt64 position, SeekOrigin origin)
|
|||
|
||||
if ((atUint64)position > m_length)
|
||||
resize(position);
|
||||
|
||||
m_position = position;
|
||||
break;
|
||||
|
||||
case SeekOrigin::Current:
|
||||
if ((((atInt64)m_position + position) < 0))
|
||||
THROW_IO_EXCEPTION("Position outside stream bounds");
|
||||
|
@ -117,12 +119,14 @@ void MemoryWriter::seek(atInt64 position, SeekOrigin origin)
|
|||
|
||||
m_position += position;
|
||||
break;
|
||||
|
||||
case SeekOrigin::End:
|
||||
if (((atInt64)m_length - position) < 0)
|
||||
THROW_IO_EXCEPTION("Position outside stream bounds");
|
||||
|
||||
if ((atUint64)position > m_length)
|
||||
resize(position);
|
||||
|
||||
m_position = m_length - position;
|
||||
break;
|
||||
}
|
||||
|
@ -185,11 +189,13 @@ void MemoryWriter::save(const std::string& filename)
|
|||
m_filepath = filename;
|
||||
|
||||
FILE* out = fopen(m_filepath.c_str(), "wb");
|
||||
|
||||
if (!out)
|
||||
THROW_FILE_NOT_FOUND_EXCEPTION(m_filepath);
|
||||
|
||||
atUint64 done = 0;
|
||||
atUint64 blocksize = BLOCKSZ;
|
||||
|
||||
do
|
||||
{
|
||||
if (blocksize > m_length - done)
|
||||
|
@ -203,7 +209,8 @@ void MemoryWriter::save(const std::string& filename)
|
|||
break;
|
||||
|
||||
done += blocksize;
|
||||
}while (done < m_length);
|
||||
}
|
||||
while (done < m_length);
|
||||
|
||||
fclose(out);
|
||||
}
|
||||
|
@ -228,7 +235,9 @@ void MemoryWriter::writeBit(bool val)
|
|||
*(atUint8*)(m_data + m_position) |= (1 << m_bitPosition);
|
||||
else
|
||||
*(atUint8*)(m_data + m_position) &= ~(1 << m_bitPosition);
|
||||
|
||||
m_bitPosition++;
|
||||
|
||||
if (m_bitPosition > 7)
|
||||
{
|
||||
m_bitPosition = 0;
|
||||
|
@ -246,6 +255,7 @@ void MemoryWriter::writeUByte(atUint8 val)
|
|||
m_bitPosition = 0;
|
||||
m_position += sizeof(atUint8);
|
||||
}
|
||||
|
||||
if (m_position + 1 > m_length)
|
||||
resize(m_position + 1);
|
||||
|
||||
|
@ -272,6 +282,7 @@ void MemoryWriter::writeUBytes(atUint8* data, atUint64 length)
|
|||
|
||||
if (!data)
|
||||
THROW_INVALID_DATA_EXCEPTION("data cannnot be NULL");
|
||||
|
||||
if (m_position + length > m_length)
|
||||
resize(m_position + length);
|
||||
|
||||
|
@ -445,6 +456,7 @@ void MemoryWriter::writeUnicode(const std::string& str)
|
|||
if (chr != 0xFEFF)
|
||||
writeInt16(chr);
|
||||
}
|
||||
|
||||
writeInt16(0);
|
||||
}
|
||||
|
||||
|
@ -453,9 +465,11 @@ void MemoryWriter::writeString(const std::string& str)
|
|||
for (atUint8 c : str)
|
||||
{
|
||||
writeUByte(c);
|
||||
|
||||
if (c == '\0')
|
||||
break;
|
||||
}
|
||||
|
||||
writeUByte(0);
|
||||
}
|
||||
|
||||
|
|
|
@ -42,21 +42,27 @@ bool PHYSFSFileReader::isOpen() const
|
|||
void PHYSFSFileReader::seek(atInt64 position, SeekOrigin origin)
|
||||
{
|
||||
atInt64 curPos = PHYSFS_tell(m_handle);
|
||||
|
||||
switch (origin)
|
||||
{
|
||||
case SeekOrigin::Begin:
|
||||
if ((position < 0 || (atInt64)position > (atInt64)m_length))
|
||||
THROW_IO_EXCEPTION("Position %0.16X outside stream bounds ", position);
|
||||
|
||||
PHYSFS_seek(m_handle, position);
|
||||
break;
|
||||
|
||||
case SeekOrigin::Current:
|
||||
if ((((atInt64)curPos + position) < 0 || (curPos + position) > m_length))
|
||||
THROW_IO_EXCEPTION("Position %0.16X outside stream bounds ", position);
|
||||
|
||||
PHYSFS_seek(m_handle, curPos + position);
|
||||
break;
|
||||
|
||||
case SeekOrigin::End:
|
||||
if ((((atInt64)m_length - position < 0) || (m_length - position) > m_length))
|
||||
THROW_IO_EXCEPTION("Position %0.16X outside stream bounds ", position);
|
||||
|
||||
PHYSFS_seek(m_handle, m_length - position);
|
||||
break;
|
||||
}
|
||||
|
@ -80,8 +86,10 @@ atInt8 PHYSFSFileReader::readByte()
|
|||
atUint8* PHYSFSFileReader::readUBytes(atUint64 length)
|
||||
{
|
||||
atUint8* data = new atUint8[length];
|
||||
|
||||
if (PHYSFS_read(m_handle, data, 1, length) == length)
|
||||
return data;
|
||||
|
||||
delete[] data;
|
||||
THROW_IO_EXCEPTION("Position outside stream bounds");
|
||||
}
|
||||
|
|
|
@ -41,6 +41,7 @@ SkywardSwordFileReader::SkywardSwordFileReader(const std::string& filename)
|
|||
SkywardSwordFile* SkywardSwordFileReader::read()
|
||||
{
|
||||
SkywardSwordFile* file = NULL;
|
||||
|
||||
try
|
||||
{
|
||||
if (base::length() != 0xFBE0)
|
||||
|
@ -60,6 +61,7 @@ SkywardSwordFile* SkywardSwordFileReader::read()
|
|||
// Time to read in each slot
|
||||
file = new SkywardSwordFile;
|
||||
file->setRegion((magic == SkywardSwordFile::USMagic ? Region::NTSC : (magic == SkywardSwordFile::JAMagic ? Region::NTSCJ : Region::PAL)));
|
||||
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
SkywardSwordQuest* q = new SkywardSwordQuest((atUint8*)base::readBytes(0x53C0), 0x53C0);
|
||||
|
|
|
@ -51,10 +51,12 @@ void SkywardSwordFileWriter::write(SkywardSwordFile *file)
|
|||
|
||||
std::vector<SkywardSwordQuest*> quests = file->questList();
|
||||
int i = 0;
|
||||
|
||||
for (SkywardSwordQuest* q : quests)
|
||||
{
|
||||
if (q->length() != 0x53C0)
|
||||
THROW_INVALID_DATA_EXCEPTION("q->data() not 0x53C0 bytes in length");
|
||||
|
||||
// Update the checksums
|
||||
q->fixChecksums();
|
||||
// Write the save data
|
||||
|
|
|
@ -85,6 +85,7 @@ void SkywardSwordQuest::setPlayerName(const std::string& name)
|
|||
for (atUint32 i = 0; i < 8; i++)
|
||||
{
|
||||
atUint16& c = *(atUint16*)(m_data + priv::NAME_OFFSET + (i * 2));
|
||||
|
||||
if (i >= val.size())
|
||||
{
|
||||
c = 0;
|
||||
|
@ -99,9 +100,11 @@ void SkywardSwordQuest::setPlayerName(const std::string& name)
|
|||
std::string SkywardSwordQuest::playerName() const
|
||||
{
|
||||
std::vector<atUint16> val;
|
||||
|
||||
for (atUint32 i = 0; i < 8; i++)
|
||||
{
|
||||
atUint16 c = *(atUint16*)(m_data + priv::NAME_OFFSET + (i * 2));
|
||||
|
||||
if (c == 0)
|
||||
break;
|
||||
|
||||
|
@ -137,9 +140,11 @@ void SkywardSwordQuest::setAmmoCount(SkywardSwordQuest::AmmoType type, atUint32
|
|||
case Arrows:
|
||||
values.arrows = count;
|
||||
break;
|
||||
|
||||
case Bombs:
|
||||
values.bombs = count;
|
||||
break;
|
||||
|
||||
case Seeds:
|
||||
values.seeds = count;
|
||||
break;
|
||||
|
@ -155,10 +160,17 @@ atUint32 SkywardSwordQuest::ammoCount(AmmoType type)
|
|||
|
||||
switch (type)
|
||||
{
|
||||
case Arrows: return values.arrows;
|
||||
case Bombs: return values.bombs;
|
||||
case Seeds: return values.seeds;
|
||||
default: return 0;
|
||||
case Arrows:
|
||||
return values.arrows;
|
||||
|
||||
case Bombs:
|
||||
return values.bombs;
|
||||
|
||||
case Seeds:
|
||||
return values.seeds;
|
||||
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -48,6 +48,7 @@ Sprite::Sprite(SpriteFile* root, const std::string& name)
|
|||
Sprite::~Sprite()
|
||||
{
|
||||
#ifndef ATHENA_USE_QT
|
||||
|
||||
for (SpriteFrame* frame : m_frames)
|
||||
#else
|
||||
foreach (SpriteFrame* frame, m_frames)
|
||||
|
@ -172,6 +173,7 @@ bool Sprite::addFrame(SpriteFrame* part)
|
|||
{
|
||||
if (m_frames.size() > 65536)
|
||||
return false;
|
||||
|
||||
for (SpriteFrame* tmp : m_frames)
|
||||
{
|
||||
if (tmp == part)
|
||||
|
@ -186,14 +188,18 @@ bool Sprite::removeFrame(SpriteFrame* frame)
|
|||
{
|
||||
#ifndef ATHENA_USE_QT
|
||||
std::vector<SpriteFrame*>::iterator iter = std::find(m_frames.begin(), m_frames.end(), frame);
|
||||
|
||||
if (iter != m_frames.end())
|
||||
{
|
||||
m_frames.erase(iter);
|
||||
return true;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
if (m_frames.removeOne(frame))
|
||||
return true;
|
||||
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
|
@ -209,6 +215,7 @@ void Sprite::setFrames(std::vector<SpriteFrame*> frames)
|
|||
{
|
||||
if (frames.size() == 0)
|
||||
return;
|
||||
|
||||
if (m_frames.size() > 0)
|
||||
{
|
||||
for (SpriteFrame* frame : m_frames)
|
||||
|
@ -216,8 +223,10 @@ void Sprite::setFrames(std::vector<SpriteFrame*> frames)
|
|||
delete frame;
|
||||
frame = NULL;
|
||||
}
|
||||
|
||||
m_frames.clear();
|
||||
}
|
||||
|
||||
m_frames = frames;
|
||||
}
|
||||
#else
|
||||
|
@ -251,6 +260,7 @@ SpriteFile* Sprite::container() const
|
|||
void Sprite::setCurrentFrame(SpriteFrame* frame)
|
||||
{
|
||||
atUint32 id = 0;
|
||||
|
||||
for (SpriteFrame* tmpFrame : m_frames)
|
||||
{
|
||||
if (tmpFrame == frame)
|
||||
|
@ -258,6 +268,7 @@ void Sprite::setCurrentFrame(SpriteFrame* frame)
|
|||
setCurrentFrame(id);
|
||||
return;
|
||||
}
|
||||
|
||||
id++;
|
||||
}
|
||||
}
|
||||
|
@ -281,6 +292,7 @@ SpriteFrame* Sprite::currentFrame() const
|
|||
void Sprite::advanceFrame()
|
||||
{
|
||||
m_currentFrame++;
|
||||
|
||||
if (m_currentFrame >= m_frames.size())
|
||||
m_currentFrame = (atUint32)m_frames.size() - 1;
|
||||
}
|
||||
|
|
|
@ -55,11 +55,13 @@ SpriteFile::SpriteFile(const QSize& size, const QPoint& origin)
|
|||
SpriteFile::~SpriteFile()
|
||||
{
|
||||
#ifndef ATHENA_USE_QT
|
||||
|
||||
for (std::pair<std::string, Sprite*> sprite : m_sprites)
|
||||
{
|
||||
delete sprite.second;
|
||||
sprite.second = NULL;
|
||||
}
|
||||
|
||||
#endif
|
||||
m_sprites.clear();
|
||||
}
|
||||
|
@ -207,12 +209,16 @@ void SpriteFile::addSprite(Sprite* sprite)
|
|||
#ifndef ATHENA_USE_QT
|
||||
std::string name(sprite->name());
|
||||
Athena::utility::tolower(name);
|
||||
|
||||
if (m_sprites.find(name) != m_sprites.end())
|
||||
return;
|
||||
|
||||
#else
|
||||
QString name = sprite->name().toLower();
|
||||
|
||||
if (m_sprites.contains(name))
|
||||
return;
|
||||
|
||||
#endif
|
||||
|
||||
m_sprites[name] = sprite;
|
||||
|
@ -224,6 +230,7 @@ void SpriteFile::removeSprite(const std::string& name)
|
|||
std::string tmpName(name);
|
||||
Athena::utility::tolower(tmpName);
|
||||
std::unordered_map<std::string, Sprite*>::iterator iterator = m_sprites.find(tmpName);
|
||||
|
||||
if (iterator != m_sprites.end())
|
||||
m_sprites.erase(iterator);
|
||||
}
|
||||
|
@ -244,6 +251,7 @@ void SpriteFile::setSprites(std::unordered_map<std::string, Sprite*> sprites)
|
|||
{
|
||||
if (sprites.size() == 0)
|
||||
return;
|
||||
|
||||
if (m_sprites.size() > 0)
|
||||
{
|
||||
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;
|
||||
sprite.second = NULL;
|
||||
}
|
||||
|
||||
m_sprites.clear();
|
||||
}
|
||||
|
||||
|
@ -261,6 +270,7 @@ void SpriteFile::setSprites(QMap<QString, Sprite *> sprites)
|
|||
{
|
||||
if (sprites.size() == 0)
|
||||
return;
|
||||
|
||||
m_sprites.clear();
|
||||
m_sprites = sprites;
|
||||
}
|
||||
|
@ -271,6 +281,7 @@ Sprite* SpriteFile::sprite(const std::string& name)
|
|||
{
|
||||
std::string nameLow(name);
|
||||
Athena::utility::tolower(nameLow);
|
||||
|
||||
if (m_sprites.find(nameLow) == m_sprites.end())
|
||||
return NULL;
|
||||
|
||||
|
@ -313,6 +324,7 @@ void SpriteFile::setTextures(std::vector<STexture*> textures)
|
|||
delete tex;
|
||||
tex = NULL;
|
||||
}
|
||||
|
||||
m_textures.clear();
|
||||
}
|
||||
|
||||
|
|
|
@ -42,6 +42,7 @@ SpriteFileReader::SpriteFileReader(const std::string& filepath)
|
|||
Sakura::SpriteFile* SpriteFileReader::readFile()
|
||||
{
|
||||
Sakura::SpriteFile* ret = NULL;
|
||||
|
||||
try
|
||||
{
|
||||
atUint32 magic = base::readUint32();
|
||||
|
@ -126,6 +127,7 @@ Sakura::SpriteFile* SpriteFileReader::readFile()
|
|||
|
||||
// Each state id corresponds to a texture held in the parent class
|
||||
std::vector<int> stateIds;
|
||||
|
||||
for (int j = 0; j < stateCount; j++)
|
||||
stateIds.push_back(base::readUint16());
|
||||
|
||||
|
@ -157,6 +159,7 @@ Sakura::SpriteFile* SpriteFileReader::readFile()
|
|||
#else
|
||||
QList<Sakura::SpritePart*> parts;
|
||||
#endif
|
||||
|
||||
for (atUint8 j = 0; j < partCount; j++)
|
||||
{
|
||||
Sakura::SpritePart* part = new Sakura::SpritePart(frame);
|
||||
|
@ -184,21 +187,26 @@ Sakura::SpriteFile* SpriteFileReader::readFile()
|
|||
|
||||
parts.push_back(part);
|
||||
}
|
||||
|
||||
frame->setParts(parts);
|
||||
frames.push_back(frame);
|
||||
}
|
||||
|
||||
sprite->setFrames(frames);
|
||||
#ifndef ATHENA_USE_QT
|
||||
|
||||
if (sprite->name() != std::string())
|
||||
{
|
||||
std::string nameLow(sprite->name());
|
||||
Athena::utility::tolower(nameLow);
|
||||
sprites[nameLow] = sprite;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
if (!sprite->name().isEmpty())
|
||||
sprites[sprite->name().toLower()] = sprite;
|
||||
|
||||
#endif
|
||||
else
|
||||
THROW_IO_EXCEPTION_RETURN(nullptr, "Sprite names cannot be empty");
|
||||
|
|
|
@ -60,11 +60,13 @@ void SpriteFileWriter::writeFile(Sakura::SpriteFile* file)
|
|||
}
|
||||
|
||||
#ifndef ATHENA_USE_QT
|
||||
|
||||
for (std::pair<std::string, Sakura::Sprite*> spritePair : file->sprites())
|
||||
{
|
||||
Sakura::Sprite* sprite = spritePair.second;
|
||||
base::writeString(sprite->name());
|
||||
#else
|
||||
|
||||
foreach (Sakura::Sprite* sprite, file->sprites().values())
|
||||
{
|
||||
|
||||
|
@ -80,6 +82,7 @@ void SpriteFileWriter::writeFile(Sakura::SpriteFile* file)
|
|||
{
|
||||
base::writeFloat(frame->frameTime());
|
||||
base::writeUint16(frame->partCount());
|
||||
|
||||
for (Sakura::SpritePart* part : frame->parts())
|
||||
{
|
||||
#ifndef ATHENA_USE_QT
|
||||
|
|
|
@ -51,8 +51,10 @@ std::vector<std::string> &split(const std::string &s, char delim, std::vector<st
|
|||
{
|
||||
std::stringstream ss(s);
|
||||
std::string item;
|
||||
|
||||
while (std::getline(ss, item, delim))
|
||||
elems.push_back(item);
|
||||
|
||||
return elems;
|
||||
}
|
||||
|
||||
|
@ -88,13 +90,16 @@ std::string vsprintf(const char* fmt, va_list list)
|
|||
char* buffer = 0;
|
||||
buffer = new char[size];
|
||||
int nsize = ::vsnprintf(buffer, size, fmt, list);
|
||||
|
||||
while (size <= nsize)
|
||||
{ //fail delete buffer and try again
|
||||
{
|
||||
//fail delete buffer and try again
|
||||
delete[] buffer;
|
||||
buffer = 0;
|
||||
buffer = new char[nsize + 1]; //+1 for /0
|
||||
nsize = ::vsnprintf(buffer, size, fmt, list);
|
||||
}
|
||||
|
||||
std::string ret(buffer);
|
||||
delete[] buffer;
|
||||
return ret;
|
||||
|
@ -120,6 +125,7 @@ bool parseBool(const std::string& boolean, bool* valid)
|
|||
{
|
||||
if (valid)
|
||||
*valid = true;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -128,6 +134,7 @@ bool parseBool(const std::string& boolean, bool* valid)
|
|||
{
|
||||
if (valid)
|
||||
*valid = true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -144,6 +151,7 @@ int countChar(const std::string& str, const char chr, int* lastOccur)
|
|||
int ret = 0;
|
||||
|
||||
int index = 0;
|
||||
|
||||
for (char c : str)
|
||||
{
|
||||
if (c == chr)
|
||||
|
@ -153,6 +161,7 @@ int countChar(const std::string& str, const char chr, int* lastOccur)
|
|||
|
||||
ret++;
|
||||
}
|
||||
|
||||
index++;
|
||||
}
|
||||
|
||||
|
@ -171,6 +180,7 @@ std::string &trim(std::string &s)
|
|||
{
|
||||
// Find first non whitespace char in StrToTrim
|
||||
std::string::size_type first = s.find_first_not_of(' ');
|
||||
|
||||
// Check whether something went wrong?
|
||||
if (first == std::string::npos)
|
||||
{
|
||||
|
@ -179,6 +189,7 @@ std::string &trim(std::string &s)
|
|||
|
||||
// Find last non whitespace char from StrToTrim
|
||||
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 (last != std::string::npos)
|
||||
{
|
||||
|
|
|
@ -92,6 +92,7 @@ void WiiFile::setData(const atUint8* data)
|
|||
delete[] m_fileData;
|
||||
m_fileData = NULL;
|
||||
}
|
||||
|
||||
m_fileData = (atUint8*)data;
|
||||
}
|
||||
|
||||
|
@ -153,10 +154,12 @@ void WiiFile::addChild(WiiFile *file)
|
|||
// Since we only support *NIX paths this is simple
|
||||
atUint32 depth = Athena::utility::countChar(tmpName, '/');
|
||||
bool owned = false;
|
||||
|
||||
while ((depth--) > 0)
|
||||
{
|
||||
// add them from the beginning of the path up
|
||||
tmpName = tmpName.substr(0, tmpName.find('/'));
|
||||
|
||||
for (atUint32 i = 0; i < m_children.size(); i++)
|
||||
{
|
||||
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(),
|
||||
[&name](WiiFile * f) { return !f->filename().compare(name); });
|
||||
|
||||
if (iter != m_children.end())
|
||||
return *iter;
|
||||
|
||||
|
@ -193,6 +197,7 @@ WiiFile* WiiFile::child(const std::string &name)
|
|||
continue;
|
||||
|
||||
WiiFile* ret = f->child(tmpName);
|
||||
|
||||
if (ret)
|
||||
return ret;
|
||||
}
|
||||
|
@ -242,8 +247,10 @@ atUint32 WiiFile::fileCount()
|
|||
std::vector<WiiFile*> WiiFile::allChildren()
|
||||
{
|
||||
std::vector<WiiFile*> ret;
|
||||
|
||||
if (m_children.size() == 0)
|
||||
return ret;
|
||||
|
||||
// Add our children first
|
||||
for (WiiFile* f : m_children)
|
||||
ret.push_back(f);
|
||||
|
@ -268,6 +275,7 @@ std::vector<WiiFile *> WiiFile::allChildren()
|
|||
std::string WiiFile::fullpath()
|
||||
{
|
||||
std::string ret;
|
||||
|
||||
if (m_parent)
|
||||
ret = m_parent->filename() + "/";
|
||||
|
||||
|
|
|
@ -33,6 +33,7 @@ WiiImage::~WiiImage()
|
|||
{
|
||||
if (m_data)
|
||||
delete[] m_data;
|
||||
|
||||
m_data = NULL;
|
||||
}
|
||||
|
||||
|
@ -59,6 +60,7 @@ atUint8 *WiiImage::toRGBA()
|
|||
atUint8* bitmapdata = NULL;
|
||||
|
||||
bitmapdata = new atUint8[m_width * m_height * 4];
|
||||
|
||||
if (bitmapdata == NULL)
|
||||
return NULL;
|
||||
|
||||
|
@ -74,6 +76,7 @@ atUint8 *WiiImage::toRGBA()
|
|||
//if((x >= m_width) || (y >= m_height))
|
||||
// continue;
|
||||
oldpixel = utility::swapU16(oldpixel);
|
||||
|
||||
if (oldpixel & (1 << 15))
|
||||
{
|
||||
// RGB5
|
||||
|
@ -98,6 +101,7 @@ atUint8 *WiiImage::toRGBA()
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
return bitmapdata;
|
||||
}
|
||||
|
||||
|
|
|
@ -52,12 +52,14 @@ WiiSaveReader::WiiSaveReader(const std::string& filename)
|
|||
WiiSave* WiiSaveReader::readSave()
|
||||
{
|
||||
WiiSave* ret = new WiiSave;
|
||||
|
||||
try
|
||||
{
|
||||
if (length() < 0xF0C0)
|
||||
THROW_INVALID_DATA_EXCEPTION_RETURN(nullptr, "Not a valid WiiSave");
|
||||
|
||||
WiiBanner* banner = this->readBanner();
|
||||
|
||||
if (!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");
|
||||
|
||||
atUint32 bkMagic = base::readUint32();
|
||||
|
||||
if (bkMagic != 0x426B0001)
|
||||
THROW_INVALID_DATA_EXCEPTION_RETURN(nullptr, "Invalid BacKup header magic");
|
||||
|
||||
|
@ -85,9 +88,11 @@ WiiSave* WiiSaveReader::readSave()
|
|||
base::seek(0x10);
|
||||
|
||||
std::vector<WiiFile*> files;
|
||||
|
||||
for (atUint32 i = 0; i < numFiles; ++i)
|
||||
{
|
||||
WiiFile* file = readFile();
|
||||
|
||||
if (file)
|
||||
files.push_back(file);
|
||||
}
|
||||
|
@ -141,18 +146,23 @@ WiiBanner* WiiSaveReader::readBanner()
|
|||
std::cerr << "MD5 Mismatch" << std::endl;
|
||||
// Make sure to reset m_reader values back to the old ones.
|
||||
std::cerr << "MD5 provided: ";
|
||||
|
||||
for (int i = 0; i < 16; ++i)
|
||||
std::cerr << std::setw(2) << std::setfill('0') << std::hex << (int)(md5[i]);
|
||||
|
||||
std::cerr << std::endl;
|
||||
|
||||
std::cerr << "MD5 Calculated: ";
|
||||
|
||||
for (int i = 0; i < 16; ++i)
|
||||
std::cerr << std::hex << (int)(md5Calc[i]);
|
||||
|
||||
std::cerr << std::endl;
|
||||
base::setData(oldData, oldLen);
|
||||
base::seek(oldPos, SeekOrigin::Begin);
|
||||
THROW_INVALID_DATA_EXCEPTION_RETURN(nullptr, "MD5 Mismatch");
|
||||
}
|
||||
|
||||
// Set the binary reader buffer;
|
||||
base::setData(dec, 0xF0C0);
|
||||
// Start reading the header
|
||||
|
@ -186,10 +196,12 @@ WiiBanner* WiiSaveReader::readBanner()
|
|||
base::seek(22);
|
||||
|
||||
gameTitle = base::readUnicode();
|
||||
|
||||
if (base::position() != 0x0080)
|
||||
base::seek(0x0080, SeekOrigin::Begin);
|
||||
|
||||
subTitle = base::readUnicode();
|
||||
|
||||
if (base::position() != 0x00C0)
|
||||
base::seek(0x00C0, SeekOrigin::Begin);
|
||||
|
||||
|
@ -208,6 +220,7 @@ WiiBanner* WiiSaveReader::readBanner()
|
|||
if (banner->bannerSize() == 0x72a0)
|
||||
{
|
||||
WiiImage* icon = readImage(48, 48);
|
||||
|
||||
if (icon)
|
||||
banner->addIcon(icon);
|
||||
else
|
||||
|
@ -218,6 +231,7 @@ WiiBanner* WiiSaveReader::readBanner()
|
|||
for (int i = 0; i < 8; i++)
|
||||
{
|
||||
WiiImage* icon = readImage(48, 48);
|
||||
|
||||
if (icon)
|
||||
banner->addIcon(icon);
|
||||
else
|
||||
|
@ -252,6 +266,7 @@ WiiFile* WiiSaveReader::readFile()
|
|||
WiiFile* ret;
|
||||
|
||||
atUint32 magic = base::readUint32();
|
||||
|
||||
if (magic != 0x03adf17e)
|
||||
{
|
||||
std::cerr << "Not a valid File entry header: 0x" << std::hex << magic << std::endl;
|
||||
|
|
|
@ -56,6 +56,7 @@ bool WiiSaveWriter::writeSave(WiiSave *save, atUint8 *macAddress, atUint32 ngId,
|
|||
{
|
||||
if (!save)
|
||||
THROW_INVALID_OPERATION_EXCEPTION_RETURN(false, "save cannot be NULL");
|
||||
|
||||
if (filepath != "")
|
||||
m_filepath = filepath;
|
||||
|
||||
|
@ -74,10 +75,12 @@ bool WiiSaveWriter::writeSave(WiiSave *save, atUint8 *macAddress, atUint32 ngId,
|
|||
base::seek(2); // unknown;
|
||||
base::seek(0x10); // padding;
|
||||
atUint32 totalSize = 0;
|
||||
|
||||
for (WiiFile* file : save->allFiles())
|
||||
{
|
||||
totalSize += writeFile(file);
|
||||
}
|
||||
|
||||
atUint64 pos = base::position();
|
||||
// Write size data
|
||||
base::seek(0xF0C0 + 0x10, SeekOrigin::Begin);
|
||||
|
@ -123,6 +126,7 @@ void WiiSaveWriter::writeBanner(WiiBanner *banner)
|
|||
// For empty icons
|
||||
atUint8* tmpIcon = new atUint8[48 * 48 * 2];
|
||||
memset(tmpIcon, 0, 48 * 48 * 2);
|
||||
|
||||
for (atUint32 i = 0; i < 8; ++i)
|
||||
{
|
||||
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);
|
||||
int stuff = 0x2f536969;
|
||||
|
||||
if (!utility::isSystemBigEndian())
|
||||
stuff = utility::swap32(stuff);
|
||||
|
||||
|
|
|
@ -70,6 +70,7 @@ ZQuestFile::ZQuestFile(ZQuestFile::Game game, Endian endian, atUint8* data, atUi
|
|||
m_length(length)
|
||||
{
|
||||
initGameStrings();
|
||||
|
||||
if (gameString.empty() && (m_game < GameStrings.size() - 1))
|
||||
m_gameString = GameStrings[m_game];
|
||||
}
|
||||
|
@ -84,6 +85,7 @@ ZQuestFile::~ZQuestFile()
|
|||
void ZQuestFile::setGame(ZQuestFile::Game game)
|
||||
{
|
||||
m_game = game;
|
||||
|
||||
if (m_game > GameStrings.size() - 1)
|
||||
return;
|
||||
|
||||
|
@ -145,6 +147,7 @@ const std::vector<std::string> ZQuestFile::gameStringList()
|
|||
{
|
||||
if (GameStrings.size() <= 0)
|
||||
initGameStrings();
|
||||
|
||||
return GameStrings;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -65,6 +65,7 @@ ZQuestFile *ZQuestFileReader::read()
|
|||
if (version >= ZQUEST_VERSION_CHECK(2, 0, 0))
|
||||
{
|
||||
gameString = std::string((const char*)base::readBytes(0x0A), 0x0A);
|
||||
|
||||
for (size_t i = 0; i < ZQuestFile::gameStringList().size(); i++)
|
||||
{
|
||||
if (!ZQuestFile::gameStringList().at(i).substr(0, 0x0A).compare(gameString))
|
||||
|
@ -74,6 +75,7 @@ ZQuestFile *ZQuestFileReader::read()
|
|||
break;
|
||||
}
|
||||
}
|
||||
|
||||
BOM = base::readUint16();
|
||||
checksum = base::readUint32();
|
||||
}
|
||||
|
|
|
@ -44,6 +44,7 @@ void ZQuestFileWriter::write(ZQuestFile* quest, bool compress)
|
|||
base::writeUint32(ZQuestFile::Version);
|
||||
atUint8* questData = quest->data();
|
||||
atUint32 compLen;
|
||||
|
||||
if (compress)
|
||||
{
|
||||
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::save();
|
||||
|
||||
// Delete compressed data to prevent memory leaks
|
||||
if (questData != quest->data())
|
||||
{
|
||||
|
|
|
@ -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
|
||||
int lookAheadBuffer_len = ((int)(dataEnd - posPtr) < m_readAheadBuffer) ? (int)(dataEnd - posPtr) : m_readAheadBuffer;
|
||||
int slidingBuffer = (int)(posPtr - dataBegin) - m_slidingWindow;
|
||||
|
||||
if (slidingBuffer > 0)
|
||||
searchWindow = dataBegin + slidingBuffer;
|
||||
else
|
||||
|
@ -79,6 +80,7 @@ LZLengthOffset LZBase::search(atUint8* posPtr, atUint8* dataBegin, atUint8* data
|
|||
|
||||
if (!((posPtr - dataBegin < 1) || (dataEnd - posPtr < m_minMatch)))
|
||||
results = windowSearch(searchWindow, posPtr, endPos, posPtr - m_minOffset);
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
|
@ -116,6 +118,7 @@ LZLengthOffset LZBase::windowSearch(atUint8* beginSearchPtr, atUint8* searchPosP
|
|||
atInt32 n = (atUint32)(endLABufferPtr - searchPosPtr);
|
||||
LZLengthOffset result = {0, 0};
|
||||
atInt32 temp = 0;
|
||||
|
||||
if (n > size) //If the string that is being looked for is bigger than the string that is being searched
|
||||
return result;
|
||||
|
||||
|
@ -127,11 +130,13 @@ LZLengthOffset LZBase::windowSearch(atUint8* beginSearchPtr, atUint8* searchPosP
|
|||
do
|
||||
{
|
||||
temp = subMatch(startLBPtr, searchPosPtr, n);
|
||||
|
||||
if (result.length < (atUint32)temp)
|
||||
{
|
||||
result.length = temp;
|
||||
result.offset = (atInt32)(searchPosPtr - startLBPtr);
|
||||
}
|
||||
|
||||
if (result.length == (atUint32)n)
|
||||
return result;
|
||||
|
||||
|
@ -139,6 +144,7 @@ LZLengthOffset LZBase::windowSearch(atUint8* beginSearchPtr, atUint8* searchPosP
|
|||
|
||||
}
|
||||
while ((startLBPtr--) > beginSearchPtr);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
|
@ -15,14 +15,17 @@ LZLookupTable::LZLookupTable(atInt32 minimumMatch, atInt32 slidingWindow, atInt3
|
|||
m_minimumMatch = minimumMatch;
|
||||
else
|
||||
m_minimumMatch = 3;
|
||||
|
||||
if (slidingWindow > 0)
|
||||
m_slidingWindow = slidingWindow;
|
||||
else
|
||||
m_slidingWindow = 4096;
|
||||
|
||||
if (lookAheadWindow > 0)
|
||||
m_lookAheadWindow = lookAheadWindow;
|
||||
else
|
||||
m_lookAheadWindow = 18;
|
||||
|
||||
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 loPair = {0, 0};
|
||||
|
||||
//Returns negative 1 for search failures since the current position is passed the size to be compressed
|
||||
if (curPos >= dataEnd)
|
||||
{
|
||||
loPair.length = -1;
|
||||
return loPair;
|
||||
}
|
||||
|
||||
std::copy(curPos, curPos + m_minimumMatch, m_buffer.begin());
|
||||
int32_t currentOffset = static_cast<atInt32>(curPos - dataBegin);
|
||||
|
||||
//Find code
|
||||
if (currentOffset > 0 && (dataEnd - curPos) >= m_minimumMatch)
|
||||
{
|
||||
auto elements = table.equal_range(m_buffer);
|
||||
elements.second--;
|
||||
elements.first--;
|
||||
|
||||
//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--)
|
||||
{
|
||||
int32_t matchLength = m_minimumMatch;
|
||||
int32_t lookAheadBufferLength = ((dataEnd - curPos) < m_lookAheadWindow) ? static_cast<int32_t>(dataEnd - curPos) : m_lookAheadWindow;
|
||||
|
||||
for (; matchLength < lookAheadBufferLength; ++matchLength)
|
||||
{
|
||||
if (*(dataBegin + iter->second + matchLength) != *(curPos + matchLength))
|
||||
break;
|
||||
}
|
||||
|
||||
//Store the longest match found so far into length_offset struct.
|
||||
//When lengths are the same the closer offset to the lookahead buffer wins
|
||||
if (loPair.length < (atUint32)matchLength)
|
||||
|
@ -71,30 +80,37 @@ LZLengthOffset LZLookupTable::search(atUint8* curPos, const atUint8* dataBegin,
|
|||
loPair.length = matchLength;
|
||||
loPair.offset = currentOffset - iter->second;
|
||||
}
|
||||
|
||||
//Found the longest match so break out of loop
|
||||
if (loPair.length == (atUint32)m_lookAheadWindow)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
//end find code
|
||||
//Insert code
|
||||
table.insert(std::make_pair(m_buffer, currentOffset));
|
||||
|
||||
for (atUint32 i = 1; i < loPair.length; i++)
|
||||
{
|
||||
if (dataEnd - (curPos + i) < m_minimumMatch)
|
||||
break;
|
||||
|
||||
std::copy(curPos + i, curPos + m_minimumMatch + i, m_buffer.begin());
|
||||
table.insert(std::make_pair(m_buffer, currentOffset + i));
|
||||
|
||||
}
|
||||
|
||||
//end insert code
|
||||
//Delete code
|
||||
int32_t slidingWindowOffset = std::max(0, currentOffset - m_slidingWindow);//Absolute offset
|
||||
int32_t tablesize = static_cast<int32_t>(table.size());
|
||||
|
||||
for (int32_t i = 0; i < tablesize - m_slidingWindow; ++i)
|
||||
{
|
||||
std::copy(dataBegin + slidingWindowOffset + i, dataBegin + slidingWindowOffset + m_minimumMatch + i, m_buffer.begin());
|
||||
auto elements = table.equal_range(m_buffer);
|
||||
|
||||
for (auto iter = elements.first; iter != elements.second; iter++)
|
||||
{
|
||||
if (slidingWindowOffset + i == iter->second)
|
||||
|
@ -105,6 +121,7 @@ LZLengthOffset LZLookupTable::search(atUint8* curPos, const atUint8* dataBegin,
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
//end delete code
|
||||
return loPair;
|
||||
//break lookupTable.cpp:109 if table.size()> 4096
|
||||
|
|
|
@ -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
|
||||
atUint8* compressedBytes = new atUint8[m_blockSize * 2]; //Holds the compressed bytes yet to be written
|
||||
|
||||
while (ptrStart < ptrEnd)
|
||||
{
|
||||
atUint8 blockLen = 0;
|
||||
//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
|
||||
atUint8* ptrBytes = compressedBytes;
|
||||
|
||||
for (atInt32 i = 0; i < m_blockSize; i++)
|
||||
{
|
||||
//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 (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);
|
||||
Athena::utility::BigUint16(lenOff);
|
||||
|
||||
|
@ -54,10 +57,12 @@ atUint32 LZType10::compress(const atUint8* src, atUint8** dstBuf, atUint32 srcLe
|
|||
else
|
||||
*ptrBytes++ = *ptrStart++;
|
||||
}
|
||||
|
||||
outbuf.writeByte(blockLen);
|
||||
outbuf.writeUBytes(compressedBytes, (atUint64)(ptrBytes - compressedBytes));
|
||||
|
||||
}
|
||||
|
||||
delete[] compressedBytes;
|
||||
compressedBytes = nullptr;
|
||||
|
||||
|
@ -84,6 +89,7 @@ atUint32 LZType10::decompress(const atUint8* src, atUint8** dst, atUint32 srcLen
|
|||
atUint8* outputEndPtr = uncompressedData + uncompressedSize;
|
||||
atUint8* inputPtr = (atUint8*)src + 4;
|
||||
atUint8* inputEndPtr = (atUint8*)src + srcLength;
|
||||
|
||||
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 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;
|
||||
uncompressedData = 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)
|
||||
outputPtr[j] = (outputPtr - decoding.offset)[j];
|
||||
|
||||
outputPtr += decoding.length;
|
||||
}
|
||||
else
|
||||
|
|
|
@ -14,7 +14,9 @@ LZType11::LZType11(atInt32 minimumOffset, atInt32 slidingWindow, atInt32 minimum
|
|||
atUint32 LZType11::compress(const atUint8* src, atUint8** dst, atUint32 srcLength)
|
||||
{
|
||||
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;
|
||||
Athena::utility::LittleUint32(encodeFlag);
|
||||
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);
|
||||
|
||||
}
|
||||
else{
|
||||
else
|
||||
{
|
||||
atUint32 encodeSize = (srcLength << 8) | (0x11);
|
||||
Athena::utility::LittleUint32(encodeSize);
|
||||
outbuff.writeUint32(encodeSize);
|
||||
|
@ -39,6 +42,7 @@ atUint32 LZType11::compress(const atUint8* src, atUint8** dst, atUint32 srcLengt
|
|||
atUint16 maxThreeByteMatch = 0xFF + minThreeByteMatch;
|
||||
atUint16 minFourByteMatch = maxThreeByteMatch + 1; //Minimum Four byte match is maximum Three Byte match + 1
|
||||
atInt32 maxFourByteMatch = 0xFFFF + minFourByteMatch;
|
||||
|
||||
/*
|
||||
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
|
||||
|
@ -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
|
||||
//For example 01001000 means that the second and fifth byte in the blockSize from the left is compressed
|
||||
atUint8* ptrBytes = compressedBytes;
|
||||
|
||||
for (atInt32 i = 0; i < m_blockSize; i++)
|
||||
{
|
||||
//length_offset searchResult=Search(filedata,ptrStart,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 (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
|
||||
((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);
|
||||
ptrBytes += 2;
|
||||
}
|
||||
else if(searchResult.length <= maxThreeByteMatch){
|
||||
else if (searchResult.length <= maxThreeByteMatch)
|
||||
{
|
||||
atUint32 lenOff = ((((searchResult.length - minThreeByteMatch) & 0xFF) << 12) | //Bits 20-12
|
||||
((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
|
||||
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
|
||||
(((searchResult.length - minFourByteMatch) & 0xFFFF) << 12) | //Bits 28-12
|
||||
((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);
|
||||
ptrBytes += 4;
|
||||
}
|
||||
|
||||
ptrStart += searchResult.length;
|
||||
|
||||
blockSize |= (1 << (7 - i));
|
||||
|
@ -102,10 +113,12 @@ atUint32 LZType11::compress(const atUint8* src, atUint8** dst, atUint32 srcLengt
|
|||
*ptrBytes++ = *ptrStart++;
|
||||
|
||||
}
|
||||
|
||||
outbuff.writeByte(blockSize);
|
||||
outbuff.writeUBytes(compressedBytes, (atUint64)(ptrBytes - compressedBytes));
|
||||
|
||||
}
|
||||
|
||||
delete []compressedBytes;
|
||||
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
|
||||
uncompressedLen = uncompressedLen >> 8; //First byte is the encode flag
|
||||
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
|
||||
{
|
||||
atUint32 filesize = *(atUint32*)(src + 4);
|
||||
|
@ -158,7 +172,9 @@ atUint32 LZType11::decompress(const atUint8* src, atUint8** dst, atUint32 srcLen
|
|||
if ((isCompressed >> (7 - i)) & 0x1)
|
||||
{
|
||||
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;
|
||||
memcpy(&lenOff, inputPtr, 2);
|
||||
inputPtr += 2;
|
||||
|
@ -166,7 +182,8 @@ atUint32 LZType11::decompress(const atUint8* src, atUint8** dst, atUint32 srcLen
|
|||
decoding.length = (lenOff >> 12) + 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;
|
||||
memcpy((atUint8*)&lenOff + 1, inputPtr, 3);
|
||||
inputPtr += 3;
|
||||
|
@ -174,7 +191,8 @@ atUint32 LZType11::decompress(const atUint8* src, atUint8** dst, atUint32 srcLen
|
|||
decoding.length = (lenOff >> 12) + threeByteDenorm;
|
||||
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;
|
||||
memcpy(&lenOff, 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.offset = (lenOff & 0xFFF) + 1;
|
||||
}
|
||||
else{
|
||||
else
|
||||
{
|
||||
delete[] uncompressedData;
|
||||
uncompressedData = nullptr;
|
||||
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;
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (atUint32 j = 0; j < decoding.length; ++j)
|
||||
outputPtr[j] = (outputPtr - decoding.offset)[j];
|
||||
|
||||
outputPtr += decoding.length;
|
||||
}
|
||||
else
|
||||
|
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue