#include "lzo_conf.h"
#include <lzo/lzo1.h>
#if !defined(RBITS)
# define RBITS 5
#endif
#if !defined(CLEVEL)
# define CLEVEL 1
#endif
#if (RBITS < 3 || RBITS > 5)
# error "invalid RBITS"
#endif
#if (CLEVEL < 1 || CLEVEL > 9)
# error "invalid CLEVEL"
#endif
#define RSIZE (1 << RBITS)
#define RMASK (RSIZE - 1)
#define OBITS RBITS
#define OSIZE (1 << OBITS)
#define OMASK (OSIZE - 1)
#define MBITS (8 - OBITS)
#define MSIZE (1 << MBITS)
#define MMASK (MSIZE - 1)
#if (OBITS < 3 || OBITS > 5)
# error "invalid OBITS"
#endif
#if (MBITS < 3 || MBITS > 5)
# error "invalid MBITS"
#endif
#define MIN_MATCH 3
#define THRESHOLD (MIN_MATCH - 1)
#define MIN_MATCH_SHORT MIN_MATCH
#define MAX_MATCH_SHORT (THRESHOLD + (MSIZE - 2))
#define MIN_MATCH_LONG (MAX_MATCH_SHORT + 1)
#define MAX_MATCH_LONG (MIN_MATCH_LONG + 255)
#define MAX_OFFSET (1 << (8 + OBITS))
#ifndef LZO_HASH
#define LZO_HASH LZO_HASH_LZO_INCREMENTAL_A
#endif
#define D_INDEX1(d,p) d = DM(DMUL(0x21,DX2(p,5,5)) >> 5)
#define D_INDEX2(d,p) d = d ^ D_MASK
#define DBITS (8 + RBITS)
#include "lzo_dict.h"
#define DVAL_LEN DVAL_LOOKAHEAD
LZO_EXTERN(lzo_uint) lzo1_info ( int *rbits, int *clevel );
LZO_PUBLIC(lzo_uint)
lzo1_info ( int *rbits, int *clevel )
{
if (rbits)
*rbits = RBITS;
if (clevel)
*clevel = CLEVEL;
return D_SIZE * lzo_sizeof(lzo_bytep);
}
#define R0MIN (RSIZE)
#define R0MAX (R0MIN + 255)
#define R0FAST (R0MAX & ~7u)
#if (R0MAX - R0FAST != 7) || ((R0FAST & 7) != 0)
# error "something went wrong"
#endif
LZO_PUBLIC(int)
lzo1_decompress ( const lzo_bytep in , lzo_uint in_len,
lzo_bytep out, lzo_uintp out_len,
lzo_voidp wrkmem )
{
lzo_bytep op;
const lzo_bytep ip;
const lzo_bytep const ip_end = in + in_len;
lzo_uint t;
LZO_UNUSED(wrkmem);
op = out;
ip = in;
while (ip < ip_end)
{
t = *ip++;
if (t < R0MIN)
{
if (t == 0)
{
t = *ip++;
if (t >= R0FAST - R0MIN)
{
t -= R0FAST - R0MIN;
if (t == 0)
t = R0FAST;
else
{
#if 0#else
lzo_uint tt = 256;
do tt <<= 1; while (--t > 0);
t = tt;
#endif
}
MEMCPY8_DS(op,ip,t);
continue;
}
t += R0MIN;
}
MEMCPY_DS(op,ip,t);
}
else
{
lzo_uint tt;
const lzo_bytep m_pos = op - 1;
m_pos -= (lzo_uint)(t & OMASK) | (((lzo_uint) *ip++) << OBITS);
if (t >= ((MSIZE - 1) << OBITS))
tt = (MIN_MATCH_LONG - THRESHOLD) + *ip++;
else
tt = t >> OBITS;
assert(m_pos >= out);
assert(m_pos < op);
*op++ = *m_pos++;
*op++ = *m_pos++;
MEMCPY_DS(op,m_pos,tt);
}
}
*out_len = pd(op, out);
return (ip == ip_end ? LZO_E_OK :
(ip < ip_end ? LZO_E_INPUT_NOT_CONSUMED : LZO_E_INPUT_OVERRUN));
}
static
#if LZO_ARCH_AVR
__lzo_noinline
#endif
lzo_bytep
store_run(lzo_bytep op, const lzo_bytep ii, lzo_uint r_len)
{
assert(r_len > 0);
if (r_len >= 512)
{
unsigned r_bits = 7;
do {
while (r_len >= (256u << r_bits))
{
r_len -= (256u << r_bits);
*op++ = 0; *op++ = LZO_BYTE((R0FAST - R0MIN) + r_bits);
MEMCPY8_DS(op, ii, (256u << r_bits));
}
} while (--r_bits > 0);
}
while (r_len >= R0FAST)
{
r_len -= R0FAST;
*op++ = 0; *op++ = R0FAST - R0MIN;
MEMCPY8_DS(op, ii, R0FAST);
}
if (r_len >= R0MIN)
{
*op++ = 0; *op++ = LZO_BYTE(r_len - R0MIN);
MEMCPY_DS(op, ii, r_len);
}
else if (r_len > 0)
{
*op++ = LZO_BYTE(r_len);
MEMCPY_DS(op, ii, r_len);
}
assert(r_len == 0);
return op;
}
static int
do_compress ( const lzo_bytep in , lzo_uint in_len,
lzo_bytep out, lzo_uintp out_len,
lzo_voidp wrkmem )
{
const lzo_bytep ip;
#if defined(__LZO_HASH_INCREMENTAL)
lzo_xint dv;
#endif
lzo_bytep op;
const lzo_bytep m_pos;
const lzo_bytep const ip_end = in+in_len - DVAL_LEN - MIN_MATCH_LONG;
const lzo_bytep const in_end = in+in_len - DVAL_LEN;
const lzo_bytep ii;
lzo_dict_p const dict = (lzo_dict_p) wrkmem;
#if !defined(NDEBUG)
const lzo_bytep m_pos_sav;
#endif
op = out;
ip = in;
ii = ip;
if (in_len <= MIN_MATCH_LONG + DVAL_LEN + 1)
goto the_end;
#if (LZO_DETERMINISTIC)
BZERO8_PTR(wrkmem,sizeof(lzo_dict_t),D_SIZE);
#endif
DVAL_FIRST(dv,ip);
UPDATE_D(dict,0,dv,ip,in);
ip++;
DVAL_NEXT(dv,ip);
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:
UPDATE_I(dict,0,dindex,ip,in);
#if !defined(NDEBUG) && (LZO_DICT_USE_PTR)
m_pos_sav = m_pos;
#endif
m_pos += 3;
{
#if !defined(NDEBUG) && !(LZO_DICT_USE_PTR)
assert((m_pos_sav = ip - m_off) == (m_pos - 3));
#endif
if (pd(ip,ii) > 0)
{
lzo_uint t = pd(ip,ii);
#if 1
if (t < R0MIN)
{
*op++ = LZO_BYTE(t);
MEMCPY_DS(op, ii, t);
}
else
#endif
op = store_run(op,ii,t);
}
ii = ip;
ip += MIN_MATCH;
assert(m_pos < ip);
#define PS *m_pos++ != *ip++
#if (MIN_MATCH_LONG - MIN_MATCH == 2)
if (PS || PS)
#elif (MIN_MATCH_LONG - MIN_MATCH == 6)
if (PS || PS || PS || PS || PS || PS)
#elif (MIN_MATCH_LONG - MIN_MATCH == 14)
if (PS || PS || PS || PS || PS || PS || PS ||
PS || PS || PS || PS || PS || PS || PS)
#elif (MIN_MATCH_LONG - MIN_MATCH == 30)
if (PS || PS || PS || PS || PS || PS || PS || PS ||
PS || PS || PS || PS || PS || PS || PS || PS ||
PS || PS || PS || PS || PS || PS || PS || PS ||
PS || PS || PS || PS || PS || PS)
#else
# error "MBITS not yet implemented"
#endif
{
lzo_uint m_len;
assert(pd(ip,m_pos) == m_off);
--ip;
m_len = pd(ip, ii);
assert(m_len >= MIN_MATCH_SHORT);
assert(m_len <= MAX_MATCH_SHORT);
assert(m_off > 0);
assert(m_off <= MAX_OFFSET);
assert(ii-m_off == m_pos_sav);
assert(lzo_memcmp(m_pos_sav,ii,m_len) == 0);
--m_off;
*op++ = LZO_BYTE(((m_len - THRESHOLD) << OBITS) |
(m_off & OMASK));
*op++ = LZO_BYTE(m_off >> OBITS);
#define SI
#define DI ++ii; DVAL_NEXT(dv,ii); UPDATE_D(dict,0,dv,ii,in);
#define XI assert(ii < ip); ii = ip; DVAL_FIRST(dv,(ip));
#if (CLEVEL == 9) || (CLEVEL >= 7 && MBITS <= 4) || (CLEVEL >= 5 && MBITS <= 3)
++ii;
do {
DVAL_NEXT(dv,ii);
UPDATE_D(dict,0,dv,ii,in);
} while (++ii < ip);
DVAL_NEXT(dv,ii);
assert(ii == ip);
DVAL_ASSERT(dv,ip);
#elif (CLEVEL >= 3)
SI DI DI XI
#elif (CLEVEL >= 2)
SI DI XI
#else
XI
#endif
}
else
{
const lzo_bytep end;
lzo_uint m_len;
assert(ip <= in_end);
assert(ii == ip - MIN_MATCH_LONG);
if (pd(in_end,ip) <= (MAX_MATCH_LONG - MIN_MATCH_LONG))
end = in_end;
else
{
end = ip + (MAX_MATCH_LONG - MIN_MATCH_LONG);
assert(end < in_end);
}
while (ip < end && *m_pos == *ip)
{ m_pos++; ip++; }
assert(ip <= in_end);
m_len = pd(ip, ii);
assert(m_len >= MIN_MATCH_LONG);
assert(m_len <= MAX_MATCH_LONG);
assert(m_off > 0);
assert(m_off <= MAX_OFFSET);
assert(ii-m_off == m_pos_sav);
assert(lzo_memcmp(m_pos_sav,ii,m_len) == 0);
assert(pd(ip,m_pos) == m_off);
--m_off;
*op++ = LZO_BYTE(((MSIZE - 1) << OBITS) | (m_off & OMASK));
*op++ = LZO_BYTE(m_off >> OBITS);
*op++ = LZO_BYTE(m_len - MIN_MATCH_LONG);
#if (CLEVEL == 9)
++ii;
do {
DVAL_NEXT(dv,ii);
UPDATE_D(dict,0,dv,ii,in);
} while (++ii < ip);
DVAL_NEXT(dv,ii);
assert(ii == ip);
DVAL_ASSERT(dv,ip);
#elif (CLEVEL >= 8)
SI DI DI DI DI DI DI DI DI XI
#elif (CLEVEL >= 7)
SI DI DI DI DI DI DI DI XI
#elif (CLEVEL >= 6)
SI DI DI DI DI DI DI XI
#elif (CLEVEL >= 5)
SI DI DI DI DI XI
#elif (CLEVEL >= 4)
SI DI DI DI XI
#elif (CLEVEL >= 3)
SI DI DI XI
#elif (CLEVEL >= 2)
SI DI XI
#else
XI
#endif
}
assert(ii == ip);
}
} while (ip < ip_end);
the_end:
assert(ip <= in_end);
#if defined(LZO_RETURN_IF_NOT_COMPRESSIBLE)
if (op == out)
{
*out_len = 0;
return LZO_E_NOT_COMPRESSIBLE;
}
#endif
if (pd(in_end+DVAL_LEN,ii) > 0)
op = store_run(op,ii,pd(in_end+DVAL_LEN,ii));
*out_len = pd(op, out);
return 0;
}
LZO_PUBLIC(int)
lzo1_compress ( const lzo_bytep in , lzo_uint in_len,
lzo_bytep out, lzo_uintp out_len,
lzo_voidp wrkmem )
{
int r = LZO_E_OK;
if (in_len == 0)
*out_len = 0;
else if (in_len <= MIN_MATCH_LONG + DVAL_LEN + 1)
{
#if defined(LZO_RETURN_IF_NOT_COMPRESSIBLE)
r = LZO_E_NOT_COMPRESSIBLE;
#else
*out_len = pd(store_run(out,in,in_len), out);
#endif
}
else
r = do_compress(in,in_len,out,out_len,wrkmem);
return r;
}