Skip to content

Instantly share code, notes, and snippets.

@kiljacken
Created August 25, 2014 12:45
Show Gist options
  • Save kiljacken/68979b061e1611a7fc14 to your computer and use it in GitHub Desktop.
Save kiljacken/68979b061e1611a7fc14 to your computer and use it in GitHub Desktop.
LZO Decompress
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++; /* 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));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment