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 | } |
55 | |
70 | |
56 | #if USE_MEMCPY |
71 | #if USE_MEMCPY |
57 | memcpy (op, ip, ctrl); |
72 | memcpy (op, ip, ctrl); |
… | |
… | |
74 | |
89 | |
75 | ref -= *ip++; |
90 | ref -= *ip++; |
76 | |
91 | |
77 | if (op + len + 2 > out_end) |
92 | if (op + len + 2 > out_end) |
78 | { |
93 | { |
79 | errno = E2BIG; |
94 | SET_ERRNO (E2BIG); |
80 | return 0; |
95 | return 0; |
81 | } |
96 | } |
82 | |
97 | |
83 | if (ref < (u8 *)out_data) |
98 | if (ref < (u8 *)out_data) |
84 | { |
99 | { |
85 | errno = EINVAL; |
100 | SET_ERRNO (EINVAL); |
86 | return 0; |
101 | return 0; |
87 | } |
102 | } |
88 | |
103 | |
89 | *op++ = *ref++; |
104 | *op++ = *ref++; |
90 | *op++ = *ref++; |
105 | *op++ = *ref++; |