| 1 |
root |
1.1 |
/* |
| 2 |
|
|
* Copyright (c) 2000 Marc Alexander Lehmann <pcg@goof.com> |
| 3 |
|
|
* |
| 4 |
|
|
* Redistribution and use in source and binary forms, with or without modifica- |
| 5 |
|
|
* tion, are permitted provided that the following conditions are met: |
| 6 |
|
|
* |
| 7 |
|
|
* 1. Redistributions of source code must retain the above copyright notice, |
| 8 |
|
|
* this list of conditions and the following disclaimer. |
| 9 |
|
|
* |
| 10 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright |
| 11 |
|
|
* notice, this list of conditions and the following disclaimer in the |
| 12 |
|
|
* documentation and/or other materials provided with the distribution. |
| 13 |
|
|
* |
| 14 |
|
|
* 3. The name of the author may not be used to endorse or promote products |
| 15 |
|
|
* derived from this software without specific prior written permission. |
| 16 |
|
|
* |
| 17 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 18 |
|
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- |
| 19 |
|
|
* CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 20 |
|
|
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- |
| 21 |
|
|
* CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 22 |
|
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 23 |
|
|
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 24 |
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- |
| 25 |
|
|
* ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 26 |
|
|
* OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 |
|
|
*/ |
| 28 |
|
|
|
| 29 |
|
|
#include <errno.h> |
| 30 |
|
|
|
| 31 |
|
|
#include "lzfP.h" |
| 32 |
|
|
|
| 33 |
|
|
unsigned int |
| 34 |
|
|
lzf_decompress (const void *const in_data, unsigned int in_len, |
| 35 |
|
|
void *out_data, unsigned int out_len) |
| 36 |
|
|
{ |
| 37 |
|
|
u8 const *ip = in_data; |
| 38 |
|
|
u8 *op = out_data; |
| 39 |
|
|
u8 const *const in_end = ip + in_len; |
| 40 |
|
|
u8 *const out_end = op + out_len; |
| 41 |
|
|
|
| 42 |
|
|
do |
| 43 |
|
|
{ |
| 44 |
|
|
unsigned int ctrl = *ip++; |
| 45 |
|
|
|
| 46 |
|
|
if (ctrl < (1 << 5)) /* literal run */ |
| 47 |
|
|
{ |
| 48 |
|
|
ctrl++; |
| 49 |
|
|
|
| 50 |
|
|
if (op + ctrl > out_end) |
| 51 |
|
|
{ |
| 52 |
|
|
errno = E2BIG; |
| 53 |
|
|
return 0; |
| 54 |
|
|
} |
| 55 |
|
|
|
| 56 |
|
|
#if USE_MEMCPY |
| 57 |
|
|
memcpy (op, ip, ctrl); |
| 58 |
|
|
op += ctrl; |
| 59 |
|
|
ip += ctrl; |
| 60 |
|
|
#else |
| 61 |
|
|
do |
| 62 |
|
|
*op++ = *ip++; |
| 63 |
|
|
while (--ctrl); |
| 64 |
|
|
#endif |
| 65 |
|
|
} |
| 66 |
|
|
else /* back reference */ |
| 67 |
|
|
{ |
| 68 |
|
|
unsigned int len = ctrl >> 5; |
| 69 |
|
|
|
| 70 |
|
|
u8 *ref = op - ((ctrl & 0x1f) << 8) - 1; |
| 71 |
|
|
|
| 72 |
|
|
if (len == 7) |
| 73 |
|
|
len += *ip++; |
| 74 |
|
|
|
| 75 |
|
|
ref -= *ip++; |
| 76 |
|
|
|
| 77 |
|
|
if (op + len + 2 > out_end) |
| 78 |
|
|
{ |
| 79 |
|
|
errno = E2BIG; |
| 80 |
|
|
return 0; |
| 81 |
|
|
} |
| 82 |
|
|
|
| 83 |
|
|
if (ref < (u8 *)out_data) |
| 84 |
|
|
{ |
| 85 |
|
|
errno = EINVAL; |
| 86 |
|
|
return 0; |
| 87 |
|
|
} |
| 88 |
|
|
|
| 89 |
|
|
*op++ = *ref++; |
| 90 |
|
|
*op++ = *ref++; |
| 91 |
|
|
|
| 92 |
|
|
do |
| 93 |
|
|
*op++ = *ref++; |
| 94 |
|
|
while (--len); |
| 95 |
|
|
} |
| 96 |
|
|
} |
| 97 |
|
|
while (op < out_end && ip < in_end); |
| 98 |
|
|
|
| 99 |
|
|
return op - (u8 *)out_data; |
| 100 |
|
|
} |
| 101 |
|
|
|