1 | /* |
1 | /* |
2 | * Copyright (c) 2000-2002 Marc Alexander Lehmann <pcg@goof.com> |
2 | * Copyright (c) 2000-2003 Marc Alexander Lehmann <pcg@goof.com> |
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, |
… | |
… | |
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 | */ |
27 | */ |
28 | |
28 | |
29 | #include <errno.h> |
29 | #include "lzfP.h" |
30 | |
30 | |
31 | #include "lzfP.h" |
31 | #if AVOID_ERRNO |
|
|
32 | # define SET_ERRNO(n) |
|
|
33 | #else |
|
|
34 | # include <errno.h> |
|
|
35 | # define SET_ERRNO(n) errno = (n) |
|
|
36 | #endif |
32 | |
37 | |
33 | unsigned int |
38 | unsigned int |
34 | lzf_decompress (const void *const in_data, unsigned int in_len, |
39 | lzf_decompress (const void *const in_data, unsigned int in_len, |
35 | void *out_data, unsigned int out_len) |
40 | void *out_data, unsigned int out_len) |
36 | { |
41 | { |
37 | u8 const *ip = in_data; |
42 | u8 const *ip = (const u8 *)in_data; |
38 | u8 *op = out_data; |
43 | u8 *op = (u8 *)out_data; |
39 | u8 const *const in_end = ip + in_len; |
44 | u8 const *const in_end = ip + in_len; |
40 | u8 *const out_end = op + out_len; |
45 | u8 *const out_end = op + out_len; |
41 | |
46 | |
42 | do |
47 | do |
43 | { |
48 | { |
… | |
… | |
47 | { |
52 | { |
48 | ctrl++; |
53 | ctrl++; |
49 | |
54 | |
50 | if (op + ctrl > out_end) |
55 | if (op + ctrl > out_end) |
51 | { |
56 | { |
52 | errno = E2BIG; |
57 | SET_ERRNO (E2BIG); |
53 | return 0; |
58 | return 0; |
54 | } |
59 | } |
55 | |
60 | |
56 | #if USE_MEMCPY |
61 | #if USE_MEMCPY |
57 | memcpy (op, ip, ctrl); |
62 | memcpy (op, ip, ctrl); |
… | |
… | |
74 | |
79 | |
75 | ref -= *ip++; |
80 | ref -= *ip++; |
76 | |
81 | |
77 | if (op + len + 2 > out_end) |
82 | if (op + len + 2 > out_end) |
78 | { |
83 | { |
79 | errno = E2BIG; |
84 | SET_ERRNO (E2BIG); |
80 | return 0; |
85 | return 0; |
81 | } |
86 | } |
82 | |
87 | |
83 | if (ref < (u8 *)out_data) |
88 | if (ref < (u8 *)out_data) |
84 | { |
89 | { |
85 | errno = EINVAL; |
90 | SET_ERRNO (EINVAL); |
86 | return 0; |
91 | return 0; |
87 | } |
92 | } |
88 | |
93 | |
89 | *op++ = *ref++; |
94 | *op++ = *ref++; |
90 | *op++ = *ref++; |
95 | *op++ = *ref++; |