1 |
root |
1.1 |
/* |
2 |
root |
1.8 |
* Copyright (c) 2000-2007 Marc Alexander Lehmann <schmorp@schmorp.de> |
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 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
15 |
|
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- |
16 |
|
|
* CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
17 |
|
|
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- |
18 |
|
|
* CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
19 |
|
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
20 |
|
|
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
21 |
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- |
22 |
|
|
* ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
23 |
|
|
* OF THE POSSIBILITY OF SUCH DAMAGE. |
24 |
pcg |
1.4 |
* |
25 |
|
|
* Alternatively, the contents of this file may be used under the terms of |
26 |
|
|
* the GNU General Public License version 2 (the "GPL"), in which case the |
27 |
|
|
* provisions of the GPL are applicable instead of the above. If you wish to |
28 |
|
|
* allow the use of your version of this file only under the terms of the |
29 |
|
|
* GPL and not to allow others to use your version of this file under the |
30 |
|
|
* BSD license, indicate your decision by deleting the provisions above and |
31 |
|
|
* replace them with the notice and other provisions required by the GPL. If |
32 |
|
|
* you do not delete the provisions above, a recipient may use your version |
33 |
|
|
* of this file under either the BSD or the GPL. |
34 |
root |
1.1 |
*/ |
35 |
|
|
|
36 |
pcg |
1.3 |
#include "lzfP.h" |
37 |
root |
1.1 |
|
38 |
pcg |
1.3 |
#if AVOID_ERRNO |
39 |
|
|
# define SET_ERRNO(n) |
40 |
|
|
#else |
41 |
|
|
# include <errno.h> |
42 |
|
|
# define SET_ERRNO(n) errno = (n) |
43 |
|
|
#endif |
44 |
root |
1.1 |
|
45 |
root |
1.7 |
#if (__i386 || __amd64) && __GNUC__ >= 3 |
46 |
|
|
# define lzf_movsb(dst, src, len) \ |
47 |
|
|
asm ("rep movsb" \ |
48 |
|
|
: "=D" (dst), "=S" (src), "=c" (len) \ |
49 |
|
|
: "0" (dst), "1" (src), "2" (len)); |
50 |
|
|
#endif |
51 |
|
|
|
52 |
root |
1.1 |
unsigned int |
53 |
|
|
lzf_decompress (const void *const in_data, unsigned int in_len, |
54 |
|
|
void *out_data, unsigned int out_len) |
55 |
|
|
{ |
56 |
pcg |
1.3 |
u8 const *ip = (const u8 *)in_data; |
57 |
|
|
u8 *op = (u8 *)out_data; |
58 |
root |
1.1 |
u8 const *const in_end = ip + in_len; |
59 |
|
|
u8 *const out_end = op + out_len; |
60 |
|
|
|
61 |
|
|
do |
62 |
|
|
{ |
63 |
|
|
unsigned int ctrl = *ip++; |
64 |
|
|
|
65 |
|
|
if (ctrl < (1 << 5)) /* literal run */ |
66 |
|
|
{ |
67 |
|
|
ctrl++; |
68 |
|
|
|
69 |
|
|
if (op + ctrl > out_end) |
70 |
|
|
{ |
71 |
pcg |
1.3 |
SET_ERRNO (E2BIG); |
72 |
root |
1.1 |
return 0; |
73 |
|
|
} |
74 |
|
|
|
75 |
root |
1.6 |
#if CHECK_INPUT |
76 |
|
|
if (ip + ctrl > in_end) |
77 |
|
|
{ |
78 |
|
|
SET_ERRNO (EINVAL); |
79 |
|
|
return 0; |
80 |
|
|
} |
81 |
|
|
#endif |
82 |
|
|
|
83 |
root |
1.7 |
#ifdef lzf_movsb |
84 |
|
|
lzf_movsb (op, ip, ctrl); |
85 |
root |
1.1 |
#else |
86 |
|
|
do |
87 |
|
|
*op++ = *ip++; |
88 |
|
|
while (--ctrl); |
89 |
|
|
#endif |
90 |
|
|
} |
91 |
|
|
else /* back reference */ |
92 |
|
|
{ |
93 |
|
|
unsigned int len = ctrl >> 5; |
94 |
|
|
|
95 |
|
|
u8 *ref = op - ((ctrl & 0x1f) << 8) - 1; |
96 |
|
|
|
97 |
root |
1.6 |
#if CHECK_INPUT |
98 |
|
|
if (ip >= in_end) |
99 |
|
|
{ |
100 |
|
|
SET_ERRNO (EINVAL); |
101 |
|
|
return 0; |
102 |
|
|
} |
103 |
|
|
#endif |
104 |
root |
1.1 |
if (len == 7) |
105 |
root |
1.6 |
{ |
106 |
|
|
len += *ip++; |
107 |
|
|
#if CHECK_INPUT |
108 |
|
|
if (ip >= in_end) |
109 |
|
|
{ |
110 |
|
|
SET_ERRNO (EINVAL); |
111 |
|
|
return 0; |
112 |
|
|
} |
113 |
|
|
#endif |
114 |
|
|
} |
115 |
|
|
|
116 |
root |
1.1 |
ref -= *ip++; |
117 |
|
|
|
118 |
|
|
if (op + len + 2 > out_end) |
119 |
|
|
{ |
120 |
pcg |
1.3 |
SET_ERRNO (E2BIG); |
121 |
root |
1.1 |
return 0; |
122 |
|
|
} |
123 |
|
|
|
124 |
|
|
if (ref < (u8 *)out_data) |
125 |
|
|
{ |
126 |
pcg |
1.3 |
SET_ERRNO (EINVAL); |
127 |
root |
1.1 |
return 0; |
128 |
|
|
} |
129 |
|
|
|
130 |
root |
1.7 |
#ifdef lzf_movsb |
131 |
|
|
len += 2; |
132 |
|
|
lzf_movsb (op, ref, len); |
133 |
|
|
#else |
134 |
root |
1.1 |
*op++ = *ref++; |
135 |
|
|
*op++ = *ref++; |
136 |
|
|
|
137 |
|
|
do |
138 |
|
|
*op++ = *ref++; |
139 |
|
|
while (--len); |
140 |
root |
1.7 |
#endif |
141 |
root |
1.1 |
} |
142 |
|
|
} |
143 |
root |
1.6 |
while (ip < in_end); |
144 |
root |
1.1 |
|
145 |
|
|
return op - (u8 *)out_data; |
146 |
|
|
} |
147 |
|
|
|