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