_lzo_config_check(void) { lzo_bool r = 1; union { unsigned char c[2*sizeof(lzo_xint)]; lzo_xint l[2]; } u; lzo_uintptr_t p; #if !defined(LZO_CFG_NO_CONFIG_CHECK) #if defined(LZO_ABI_BIG_ENDIAN) u.l[0] = u.l[1] = 0; u.c[sizeof(lzo_xint) - 1] = 128; r &= (u.l[0] == 128); #endif #if defined(LZO_ABI_LITTLE_ENDIAN) u.l[0] = u.l[1] = 0; u.c[0] = 128; r &= (u.l[0] == 128); #endif #if defined(LZO_UNALIGNED_OK_2) p = (lzo_uintptr_t) (const lzo_voidp) &u.c[0]; u.l[0] = u.l[1] = 0; r &= ((* (const lzo_ushortp) (p+1)) == 0); #endif #if defined(LZO_UNALIGNED_OK_4) p = (lzo_uintptr_t) (const lzo_voidp) &u.c[0]; u.l[0] = u.l[1] = 0; r &= ((* (const lzo_uint32p) (p+1)) == 0); #endif #endif LZO_UNUSED(u); LZO_UNUSED(p); return r == 1 ? LZO_E_OK : LZO_E_ERROR; }
lzo1a_decompress ( const lzo_byte *in , lzo_uint in_len, lzo_byte *out, lzo_uint *out_len, lzo_voidp wrkmem ) { #if defined(LZO_OPTIMIZE_GNUC_i386) register lzo_byte *op __asm__("%edi"); register const lzo_byte *ip __asm__("%esi"); register lzo_uint t __asm__("%ecx"); register const lzo_byte *m_pos __asm__("%ebx"); #else register lzo_byte *op; register const lzo_byte *ip; register lzo_uint t; register const lzo_byte *m_pos; #endif const lzo_byte * const ip_end = in + in_len; LZO_UNUSED(wrkmem); #if defined(__LZO_QUERY_DECOMPRESS) if (__LZO_IS_DECOMPRESS_QUERY(in,in_len,out,out_len,wrkmem)) return __LZO_QUERY_DECOMPRESS(in,in_len,out,out_len,wrkmem,0,0); #endif op = out; ip = in; while (ip < ip_end) { t = *ip++; /* get marker */ LZO_STATS(lzo_stats->marker[t]++); 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 { #if 0 t = 256u << ((unsigned) t); #else /* help the optimizer */ lzo_uint tt = 256; do tt <<= 1; while (--t > 0); t = tt; #endif } MEMCPY8_DS(op,ip,t); continue; } t += R0MIN; goto literal; } else if (t < R0MIN) /* a short literal run */ { literal: MEMCPY_DS(op,ip,t); /* after a literal a match must follow */ while (ip < ip_end) { t = *ip++; /* get R1 marker */ if (t >= R0MIN) goto match; /* R1 match - a context sensitive 3 byte match + 1 byte 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); *op++ = *m_pos++; *op++ = *m_pos++; *op++ = *m_pos++; *op++ = *ip++; } } else /* a match */ { 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); /* get match len */ if (t < ((MSIZE - 1) << OBITS)) /* a short match */ { t >>= OBITS; *op++ = *m_pos++; *op++ = *m_pos++; MEMMOVE_DS(op,m_pos,t); } else /* a long match */ { #if (LBITS < 8) t = (MIN_MATCH_LONG - THRESHOLD) + ((lzo_uint)(*ip++) & LMASK); #else t = (MIN_MATCH_LONG - THRESHOLD) + (lzo_uint)(*ip++); #endif *op++ = *m_pos++; *op++ = *m_pos++; MEMMOVE_DS(op,m_pos,t); #if (LBITS < 8) /* a very short literal following a long match */ t = ip[-1] >> LBITS; if (t) do *op++ = *ip++; while (--t); #endif } }
lzo2a_999_compress_callback ( const lzo_byte *in , lzo_uint in_len, lzo_byte *out, lzo_uintp out_len, lzo_voidp wrkmem, lzo_progress_callback_t cb, lzo_uint max_chain ) { lzo_byte *op; lzo_byte *bitp = 0; lzo_uint m_len, m_off; LZO_COMPRESS_T cc; LZO_COMPRESS_T * const c = &cc; lzo_swd_t * const swd = (lzo_swd_t *) wrkmem; int r; lzo_uint32 b = 0; /* bit buffer */ unsigned k = 0; /* bits in bit buffer */ #if defined(__LZO_QUERY_COMPRESS) if (__LZO_IS_COMPRESS_QUERY(in,in_len,out,out_len,wrkmem)) return __LZO_QUERY_COMPRESS(in,in_len,out,out_len,wrkmem,1,lzo_sizeof(lzo_swd_t)); #endif /* sanity check */ if (!lzo_assert(LZO2A_999_MEM_COMPRESS >= lzo_sizeof(lzo_swd_t))) return LZO_E_ERROR; c->init = 0; c->ip = c->in = in; c->in_end = in + in_len; c->cb = cb; c->m1 = c->m2 = c->m3 = c->m4 = 0; 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) { int lazy_match_min_gain = 0; int extra1 = 0; int extra2 = 0; lzo_uint ahead = 0; LZO_UNUSED(extra1); m_len = c->m_len; m_off = c->m_off; #if (N >= 8192) if (m_off >= 8192) { if (m_len < M3_MIN_LEN) m_len = 0; else lazy_match_min_gain = 1; } else #endif if (m_len >= M1_MIN_LEN && m_len <= M1_MAX_LEN && m_off <= 256) { lazy_match_min_gain = 2; extra1 = 3; extra2 = 2; } else if (m_len >= 10) lazy_match_min_gain = 1; else if (m_len >= 3) { lazy_match_min_gain = 1; extra1 = 1; } else m_len = 0; /* try a lazy match */ if (lazy_match_min_gain > 0 && c->look > m_len) { int lit = swd->b_char; r = find_match(c,swd,1,0); assert(r == 0); assert(c->look > 0); #if (N >= 8192) if (m_off < 8192 && c->m_off >= 8192) lazy_match_min_gain += extra1; else #endif if (m_len >= M1_MIN_LEN && m_len <= M1_MAX_LEN && m_off <= 256) { if (!(c->m_len >= M1_MIN_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) { lazy_match_min_gain -= 1; } if (lazy_match_min_gain < 1) lazy_match_min_gain = 1; if (c->m_len >= m_len + lazy_match_min_gain) { c->lazy++; #if !defined(NDEBUG) m_len = c->m_len; m_off = c->m_off; assert(lzo_memcmp(c->ip - c->look, c->ip - c->look - m_off, m_len) == 0); assert(m_len >= 3 || (m_len >= 2 && m_off <= 256)); #endif /* code literal */ putbit(0); putbyte(lit); c->lit_bytes++; continue; } else ahead = 1; assert(m_len > 0); } if (m_len == 0) { /* a literal */ putbit(0); putbyte(swd->b_char); c->lit_bytes++; r = find_match(c,swd,1,0); assert(r == 0); } else { assert(m_len >= M1_MIN_LEN); assert(m_off > 0); assert(m_off <= N); /* 2 - code match */ if (m_len >= M1_MIN_LEN && m_len <= M1_MAX_LEN && m_off <= 256) { putbit(1); putbit(0); putbits(2,m_len - M1_MIN_LEN); putbyte(m_off - 1); c->m1++; } #if (N >= 8192) else if (m_off >= 8192) { unsigned len = m_len; assert(m_len >= M3_MIN_LEN); putbit(1); putbit(1); putbyte(m_off & 31); putbyte(m_off >> 5); putbit(1); len -= M3_MIN_LEN - 1; while (len > 255) { len -= 255; putbyte(0); } putbyte(len); c->m4++; } #endif else { assert(m_len >= 3); putbit(1); putbit(1); if (m_len <= 9) { putbyte(((m_len - 2) << 5) | (m_off & 31)); putbyte(m_off >> 5); c->m2++; } else {
lzo2a_999_compress_callback(const lzo_bytep in , lzo_uint in_len, lzo_bytep out, lzo_uintp out_len, lzo_voidp wrkmem, lzo_callback_p cb, lzo_uint max_chain) { lzo_bytep op; lzo_bytep bitp = 0; lzo_uint m_len, m_off; LZO_COMPRESS_T cc; LZO_COMPRESS_T* const c = &cc; lzo_swd_p const swd = (lzo_swd_p) wrkmem; int r; lzo_uint32_t b = 0; /* bit buffer */ unsigned k = 0; /* bits in bit buffer */ /* sanity check */ LZO_COMPILE_TIME_ASSERT(LZO2A_999_MEM_COMPRESS >= SIZEOF_LZO_SWD_T) c->init = 0; c->ip = c->in = in; c->in_end = in + in_len; c->cb = cb; c->m1 = c->m2 = c->m3 = c->m4 = 0; 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; #if (SWD_N >= 8192) lzo_uint extra1 = 0; #endif lzo_uint extra2 = 0; lzo_uint ahead = 0; m_len = c->m_len; m_off = c->m_off; #if (SWD_N >= 8192) if (m_off >= 8192) { if (m_len < M3_MIN_LEN) m_len = 0; else lazy_match_min_gain = 1; } else #endif if (m_len >= M1_MIN_LEN && m_len <= M1_MAX_LEN && m_off <= 256) { lazy_match_min_gain = 2; #if (SWD_N >= 8192) extra1 = 3; #endif extra2 = 2; } else if (m_len >= 10) lazy_match_min_gain = 1; else if (m_len >= 3) { lazy_match_min_gain = 1; #if (SWD_N >= 8192) extra1 = 1; #endif } else m_len = 0; /* try a lazy match */ if (lazy_match_min_gain > 0 && c->look > m_len) { unsigned char lit = LZO_BYTE(swd->b_char); r = find_match(c, swd, 1, 0); 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 #endif if (m_len >= M1_MIN_LEN && m_len <= M1_MAX_LEN && m_off <= 256) { if (!(c->m_len >= M1_MIN_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) { lazy_match_min_gain -= 1; } if ((lzo_int) lazy_match_min_gain < 1) lazy_match_min_gain = 1; if (c->m_len >= m_len + lazy_match_min_gain) { c->lazy++; #if !defined(NDEBUG) m_len = c->m_len; m_off = c->m_off; assert(lzo_memcmp(c->ip - c->look, c->ip - c->look - m_off, m_len) == 0); assert(m_len >= 3 || (m_len >= 2 && m_off <= 256)); #endif /* code literal */ putbit(0); putbyte(lit); c->lit_bytes++; continue; } else ahead = 1; assert(m_len > 0); } if (m_len == 0) { /* a literal */ putbit(0); putbyte(swd->b_char); c->lit_bytes++; r = find_match(c, swd, 1, 0); assert(r == 0); LZO_UNUSED(r); } else { assert(m_len >= M1_MIN_LEN); assert(m_off > 0); assert(m_off <= SWD_N); /* 2 - code match */ if (m_len >= M1_MIN_LEN && m_len <= M1_MAX_LEN && m_off <= 256) { putbit(1); putbit(0); putbits(2, m_len - M1_MIN_LEN); putbyte(m_off - 1); c->m1++; } #if (SWD_N >= 8192) else if (m_off >= 8192) { unsigned len = m_len; assert(m_len >= M3_MIN_LEN); putbit(1); putbit(1); putbyte(m_off & 31); putbyte(m_off >> 5); putbit(1); len -= M3_MIN_LEN - 1; while (len > 255) { len -= 255; putbyte(0); } putbyte(len); c->m4++; } #endif else { assert(m_len >= 3); putbit(1); putbit(1); if (m_len <= 9) { putbyte(((m_len - 2) << 5) | (m_off & 31)); putbyte(m_off >> 5); c->m2++; } else {
lzo1a_decompress ( const lzo_bytep in , lzo_uint in_len, lzo_bytep out, lzo_uintp out_len, lzo_voidp wrkmem ) { register lzo_bytep op; register const lzo_bytep ip; register lzo_uint t; register const lzo_bytep m_pos; const lzo_bytep const ip_end = in + in_len; LZO_UNUSED(wrkmem); op = out; ip = in; while (ip < ip_end) { t = *ip++; /* get marker */ LZO_STATS(lzo_stats->marker[t]++); 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 { #if 0 t = 256u << ((unsigned) t); #else /* help the optimizer */ lzo_uint tt = 256; do tt <<= 1; while (--t > 0); t = tt; #endif } MEMCPY8_DS(op,ip,t); continue; } t += R0MIN; goto literal; } else if (t < R0MIN) /* a short literal run */ { literal: MEMCPY_DS(op,ip,t); /* after a literal a match must follow */ while (ip < ip_end) { t = *ip++; /* get R1 marker */ if (t >= R0MIN) goto match; /* R1 match - a context sensitive 3 byte match + 1 byte 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); *op++ = m_pos[0]; *op++ = m_pos[1]; *op++ = m_pos[2]; *op++ = *ip++; } } else /* a match */ { 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); /* get match len */ if (t < ((MSIZE - 1) << OBITS)) /* a short match */ { t >>= OBITS; *op++ = *m_pos++; *op++ = *m_pos++; MEMCPY_DS(op,m_pos,t); } else /* a long match */ { #if (LBITS < 8) t = (MIN_MATCH_LONG - THRESHOLD) + ((lzo_uint)(*ip++) & LMASK); #else t = (MIN_MATCH_LONG - THRESHOLD) + (lzo_uint)(*ip++); #endif *op++ = *m_pos++; *op++ = *m_pos++; MEMCPY_DS(op,m_pos,t); #if (LBITS < 8) /* a very short literal following a long match */ t = ip[-1] >> LBITS; if (t) do *op++ = *ip++; while (--t); #endif } }
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++; /* get marker */ if (t < R0MIN) /* a literal run */ { 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 { #if 0 t = 256u << ((unsigned) t); #else /* help the optimizer */ 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 /* a match */ { lzo_uint tt; /* get match offset */ const lzo_bytep m_pos = op - 1; m_pos -= (lzo_uint)(t & OMASK) | (((lzo_uint) *ip++) << OBITS); /* get match len */ if (t >= ((MSIZE - 1) << OBITS)) /* all m-bits set */ tt = (MIN_MATCH_LONG - THRESHOLD) + *ip++; /* a long match */ else tt = t >> OBITS; /* a short match */ assert(m_pos >= out); assert(m_pos < op); /* a half unrolled loop */ *op++ = *m_pos++; *op++ = *m_pos++; MEMCPY_DS(op,m_pos,tt); } } *out_len = pd(op, out); /* the next line is the only check in the decompressor ! */ return (ip == ip_end ? LZO_E_OK : (ip < ip_end ? LZO_E_INPUT_NOT_CONSUMED : LZO_E_INPUT_OVERRUN)); }