1 |
root |
1.1 |
/* |
2 |
pcg |
1.2 |
* Copyright (c) 2000-2003 Marc Alexander Lehmann <pcg@goof.com> |
3 |
root |
1.1 |
* |
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 |
pcg |
1.2 |
* |
28 |
|
|
* Alternatively, the contents of this file may be used under the terms of |
29 |
|
|
* the GNU General Public License version 2 (the "GPL"), in which case the |
30 |
|
|
* provisions of the GPL are applicable instead of the above. If you wish to |
31 |
|
|
* allow the use of your version of this file only under the terms of the |
32 |
|
|
* GPL and not to allow others to use your version of this file under the |
33 |
|
|
* BSD license, indicate your decision by deleting the provisions above and |
34 |
|
|
* replace them with the notice and other provisions required by the GPL. If |
35 |
|
|
* you do not delete the provisions above, a recipient may use your version |
36 |
|
|
* of this file under either the BSD or the GPL. |
37 |
root |
1.1 |
*/ |
38 |
|
|
|
39 |
pcg |
1.2 |
#include "lzfP.h" |
40 |
root |
1.1 |
|
41 |
pcg |
1.2 |
#if AVOID_ERRNO |
42 |
|
|
# define SET_ERRNO(n) |
43 |
|
|
#else |
44 |
|
|
# include <errno.h> |
45 |
|
|
# define SET_ERRNO(n) errno = (n) |
46 |
|
|
#endif |
47 |
root |
1.1 |
|
48 |
|
|
unsigned int |
49 |
|
|
lzf_decompress (const void *const in_data, unsigned int in_len, |
50 |
|
|
void *out_data, unsigned int out_len) |
51 |
|
|
{ |
52 |
pcg |
1.2 |
u8 const *ip = (const u8 *)in_data; |
53 |
|
|
u8 *op = (u8 *)out_data; |
54 |
root |
1.1 |
u8 const *const in_end = ip + in_len; |
55 |
|
|
u8 *const out_end = op + out_len; |
56 |
|
|
|
57 |
|
|
do |
58 |
|
|
{ |
59 |
|
|
unsigned int ctrl = *ip++; |
60 |
|
|
|
61 |
|
|
if (ctrl < (1 << 5)) /* literal run */ |
62 |
|
|
{ |
63 |
|
|
ctrl++; |
64 |
|
|
|
65 |
|
|
if (op + ctrl > out_end) |
66 |
|
|
{ |
67 |
pcg |
1.2 |
SET_ERRNO (E2BIG); |
68 |
root |
1.1 |
return 0; |
69 |
|
|
} |
70 |
|
|
|
71 |
|
|
#if USE_MEMCPY |
72 |
|
|
memcpy (op, ip, ctrl); |
73 |
|
|
op += ctrl; |
74 |
|
|
ip += ctrl; |
75 |
|
|
#else |
76 |
|
|
do |
77 |
|
|
*op++ = *ip++; |
78 |
|
|
while (--ctrl); |
79 |
|
|
#endif |
80 |
|
|
} |
81 |
|
|
else /* back reference */ |
82 |
|
|
{ |
83 |
|
|
unsigned int len = ctrl >> 5; |
84 |
|
|
|
85 |
|
|
u8 *ref = op - ((ctrl & 0x1f) << 8) - 1; |
86 |
|
|
|
87 |
|
|
if (len == 7) |
88 |
|
|
len += *ip++; |
89 |
|
|
|
90 |
|
|
ref -= *ip++; |
91 |
|
|
|
92 |
|
|
if (op + len + 2 > out_end) |
93 |
|
|
{ |
94 |
pcg |
1.2 |
SET_ERRNO (E2BIG); |
95 |
root |
1.1 |
return 0; |
96 |
|
|
} |
97 |
|
|
|
98 |
|
|
if (ref < (u8 *)out_data) |
99 |
|
|
{ |
100 |
pcg |
1.2 |
SET_ERRNO (EINVAL); |
101 |
root |
1.1 |
return 0; |
102 |
|
|
} |
103 |
|
|
|
104 |
|
|
*op++ = *ref++; |
105 |
|
|
*op++ = *ref++; |
106 |
|
|
|
107 |
|
|
do |
108 |
|
|
*op++ = *ref++; |
109 |
|
|
while (--len); |
110 |
|
|
} |
111 |
|
|
} |
112 |
|
|
while (op < out_end && ip < in_end); |
113 |
|
|
|
114 |
|
|
return op - (u8 *)out_data; |
115 |
|
|
} |
116 |
|
|
|