1 | /* |
1 | /* |
2 | * Copyright (c) 2000-2003 Marc Alexander Lehmann <pcg@goof.com> |
2 | * Copyright (c) 2000-2005 Marc Alexander Lehmann <schmorp@schmorp.de> |
3 | * |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without modifica- |
4 | * Redistribution and use in source and binary forms, with or without modifica- |
5 | * tion, are permitted provided that the following conditions are met: |
5 | * tion, are permitted provided that the following conditions are met: |
6 | * |
6 | * |
7 | * 1. Redistributions of source code must retain the above copyright notice, |
7 | * 1. Redistributions of source code must retain the above copyright notice, |
… | |
… | |
22 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
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, |
23 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
24 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- |
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 |
25 | * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
26 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
|
|
27 | * |
|
|
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. |
27 | */ |
37 | */ |
28 | |
38 | |
29 | #include "lzfP.h" |
39 | #include "lzfP.h" |
30 | |
40 | |
31 | #if AVOID_ERRNO |
41 | #if AVOID_ERRNO |
32 | # define SET_ERRNO(n) |
42 | # define SET_ERRNO(n) |
33 | #else |
43 | #else |
34 | # include <errno.h> |
44 | # include <errno.h> |
35 | # define SET_ERRNO(n) errno = (n) |
45 | # define SET_ERRNO(n) errno = (n) |
|
|
46 | #endif |
|
|
47 | |
|
|
48 | #if (__i386 || __amd64) && __GNUC__ >= 3 |
|
|
49 | # define lzf_movsb(dst, src, len) \ |
|
|
50 | asm ("rep movsb" \ |
|
|
51 | : "=D" (dst), "=S" (src), "=c" (len) \ |
|
|
52 | : "0" (dst), "1" (src), "2" (len)); |
36 | #endif |
53 | #endif |
37 | |
54 | |
38 | unsigned int |
55 | unsigned int |
39 | lzf_decompress (const void *const in_data, unsigned int in_len, |
56 | lzf_decompress (const void *const in_data, unsigned int in_len, |
40 | void *out_data, unsigned int out_len) |
57 | void *out_data, unsigned int out_len) |
… | |
… | |
56 | { |
73 | { |
57 | SET_ERRNO (E2BIG); |
74 | SET_ERRNO (E2BIG); |
58 | return 0; |
75 | return 0; |
59 | } |
76 | } |
60 | |
77 | |
61 | #if USE_MEMCPY |
78 | #if CHECK_INPUT |
|
|
79 | if (ip + ctrl > in_end) |
|
|
80 | { |
|
|
81 | SET_ERRNO (EINVAL); |
|
|
82 | return 0; |
|
|
83 | } |
|
|
84 | #endif |
|
|
85 | |
|
|
86 | #ifdef lzf_movsb |
62 | memcpy (op, ip, ctrl); |
87 | lzf_movsb (op, ip, ctrl); |
63 | op += ctrl; |
|
|
64 | ip += ctrl; |
|
|
65 | #else |
88 | #else |
66 | do |
89 | do |
67 | *op++ = *ip++; |
90 | *op++ = *ip++; |
68 | while (--ctrl); |
91 | while (--ctrl); |
69 | #endif |
92 | #endif |
… | |
… | |
72 | { |
95 | { |
73 | unsigned int len = ctrl >> 5; |
96 | unsigned int len = ctrl >> 5; |
74 | |
97 | |
75 | u8 *ref = op - ((ctrl & 0x1f) << 8) - 1; |
98 | u8 *ref = op - ((ctrl & 0x1f) << 8) - 1; |
76 | |
99 | |
|
|
100 | #if CHECK_INPUT |
|
|
101 | if (ip >= in_end) |
|
|
102 | { |
|
|
103 | SET_ERRNO (EINVAL); |
|
|
104 | return 0; |
|
|
105 | } |
|
|
106 | #endif |
77 | if (len == 7) |
107 | if (len == 7) |
|
|
108 | { |
78 | len += *ip++; |
109 | len += *ip++; |
|
|
110 | #if CHECK_INPUT |
|
|
111 | if (ip >= in_end) |
|
|
112 | { |
|
|
113 | SET_ERRNO (EINVAL); |
|
|
114 | return 0; |
|
|
115 | } |
|
|
116 | #endif |
79 | |
117 | } |
|
|
118 | |
80 | ref -= *ip++; |
119 | ref -= *ip++; |
81 | |
120 | |
82 | if (op + len + 2 > out_end) |
121 | if (op + len + 2 > out_end) |
83 | { |
122 | { |
84 | SET_ERRNO (E2BIG); |
123 | SET_ERRNO (E2BIG); |
… | |
… | |
89 | { |
128 | { |
90 | SET_ERRNO (EINVAL); |
129 | SET_ERRNO (EINVAL); |
91 | return 0; |
130 | return 0; |
92 | } |
131 | } |
93 | |
132 | |
|
|
133 | #ifdef lzf_movsb |
|
|
134 | len += 2; |
|
|
135 | lzf_movsb (op, ref, len); |
|
|
136 | #else |
94 | *op++ = *ref++; |
137 | *op++ = *ref++; |
95 | *op++ = *ref++; |
138 | *op++ = *ref++; |
96 | |
139 | |
97 | do |
140 | do |
98 | *op++ = *ref++; |
141 | *op++ = *ref++; |
99 | while (--len); |
142 | while (--len); |
|
|
143 | #endif |
100 | } |
144 | } |
101 | } |
145 | } |
102 | while (op < out_end && ip < in_end); |
146 | while (ip < in_end); |
103 | |
147 | |
104 | return op - (u8 *)out_data; |
148 | return op - (u8 *)out_data; |
105 | } |
149 | } |
106 | |
150 | |