1 | /* |
1 | /* |
2 | * Copyright (c) 2000 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 <errno.h> |
39 | #include "lzfP.h" |
30 | |
40 | |
31 | #include "lzfP.h" |
41 | #if AVOID_ERRNO |
|
|
42 | # define SET_ERRNO(n) |
|
|
43 | #else |
|
|
44 | # include <errno.h> |
|
|
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)); |
|
|
53 | #endif |
32 | |
54 | |
33 | unsigned int |
55 | unsigned int |
34 | lzf_decompress (const void *const in_data, unsigned int in_len, |
56 | lzf_decompress (const void *const in_data, unsigned int in_len, |
35 | void *out_data, unsigned int out_len) |
57 | void *out_data, unsigned int out_len) |
36 | { |
58 | { |
37 | u8 const *ip = in_data; |
59 | u8 const *ip = (const u8 *)in_data; |
38 | u8 *op = out_data; |
60 | u8 *op = (u8 *)out_data; |
39 | u8 const *const in_end = ip + in_len; |
61 | u8 const *const in_end = ip + in_len; |
40 | u8 *const out_end = op + out_len; |
62 | u8 *const out_end = op + out_len; |
41 | |
63 | |
42 | do |
64 | do |
43 | { |
65 | { |
… | |
… | |
47 | { |
69 | { |
48 | ctrl++; |
70 | ctrl++; |
49 | |
71 | |
50 | if (op + ctrl > out_end) |
72 | if (op + ctrl > out_end) |
51 | { |
73 | { |
52 | errno = E2BIG; |
74 | SET_ERRNO (E2BIG); |
53 | return 0; |
75 | return 0; |
54 | } |
76 | } |
55 | |
77 | |
56 | #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 |
57 | memcpy (op, ip, ctrl); |
87 | lzf_movsb (op, ip, ctrl); |
58 | op += ctrl; |
|
|
59 | ip += ctrl; |
|
|
60 | #else |
88 | #else |
61 | do |
89 | do |
62 | *op++ = *ip++; |
90 | *op++ = *ip++; |
63 | while (--ctrl); |
91 | while (--ctrl); |
64 | #endif |
92 | #endif |
… | |
… | |
67 | { |
95 | { |
68 | unsigned int len = ctrl >> 5; |
96 | unsigned int len = ctrl >> 5; |
69 | |
97 | |
70 | u8 *ref = op - ((ctrl & 0x1f) << 8) - 1; |
98 | u8 *ref = op - ((ctrl & 0x1f) << 8) - 1; |
71 | |
99 | |
|
|
100 | #if CHECK_INPUT |
|
|
101 | if (ip >= in_end) |
|
|
102 | { |
|
|
103 | SET_ERRNO (EINVAL); |
|
|
104 | return 0; |
|
|
105 | } |
|
|
106 | #endif |
72 | if (len == 7) |
107 | if (len == 7) |
|
|
108 | { |
73 | 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 |
74 | |
117 | } |
|
|
118 | |
75 | ref -= *ip++; |
119 | ref -= *ip++; |
76 | |
120 | |
77 | if (op + len + 2 > out_end) |
121 | if (op + len + 2 > out_end) |
78 | { |
122 | { |
79 | errno = E2BIG; |
123 | SET_ERRNO (E2BIG); |
80 | return 0; |
124 | return 0; |
81 | } |
125 | } |
82 | |
126 | |
83 | if (ref < (u8 *)out_data) |
127 | if (ref < (u8 *)out_data) |
84 | { |
128 | { |
85 | errno = EINVAL; |
129 | SET_ERRNO (EINVAL); |
86 | return 0; |
130 | return 0; |
87 | } |
131 | } |
88 | |
132 | |
|
|
133 | #ifdef lzf_movsb |
|
|
134 | len += 2; |
|
|
135 | lzf_movsb (op, ref, len); |
|
|
136 | #else |
89 | *op++ = *ref++; |
137 | *op++ = *ref++; |
90 | *op++ = *ref++; |
138 | *op++ = *ref++; |
91 | |
139 | |
92 | do |
140 | do |
93 | *op++ = *ref++; |
141 | *op++ = *ref++; |
94 | while (--len); |
142 | while (--len); |
|
|
143 | #endif |
95 | } |
144 | } |
96 | } |
145 | } |
97 | while (op < out_end && ip < in_end); |
146 | while (ip < in_end); |
98 | |
147 | |
99 | return op - (u8 *)out_data; |
148 | return op - (u8 *)out_data; |
100 | } |
149 | } |
101 | |
150 | |