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 |
… | |
… | |
56 | { |
66 | { |
57 | SET_ERRNO (E2BIG); |
67 | SET_ERRNO (E2BIG); |
58 | return 0; |
68 | return 0; |
59 | } |
69 | } |
60 | |
70 | |
|
|
71 | #if CHECK_INPUT |
|
|
72 | if (ip + ctrl > in_end) |
|
|
73 | { |
|
|
74 | SET_ERRNO (EINVAL); |
|
|
75 | return 0; |
|
|
76 | } |
|
|
77 | #endif |
|
|
78 | |
61 | #if USE_MEMCPY |
79 | #if USE_MEMCPY |
62 | memcpy (op, ip, ctrl); |
80 | memcpy (op, ip, ctrl); |
63 | op += ctrl; |
81 | op += ctrl; |
64 | ip += ctrl; |
82 | ip += ctrl; |
65 | #else |
83 | #else |
… | |
… | |
72 | { |
90 | { |
73 | unsigned int len = ctrl >> 5; |
91 | unsigned int len = ctrl >> 5; |
74 | |
92 | |
75 | u8 *ref = op - ((ctrl & 0x1f) << 8) - 1; |
93 | u8 *ref = op - ((ctrl & 0x1f) << 8) - 1; |
76 | |
94 | |
|
|
95 | #if CHECK_INPUT |
|
|
96 | if (ip >= in_end) |
|
|
97 | { |
|
|
98 | SET_ERRNO (EINVAL); |
|
|
99 | return 0; |
|
|
100 | } |
|
|
101 | #endif |
77 | if (len == 7) |
102 | if (len == 7) |
|
|
103 | { |
78 | 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 |
79 | |
112 | } |
|
|
113 | |
80 | ref -= *ip++; |
114 | ref -= *ip++; |
81 | |
115 | |
82 | if (op + len + 2 > out_end) |
116 | if (op + len + 2 > out_end) |
83 | { |
117 | { |
84 | SET_ERRNO (E2BIG); |
118 | SET_ERRNO (E2BIG); |
… | |
… | |
97 | do |
131 | do |
98 | *op++ = *ref++; |
132 | *op++ = *ref++; |
99 | while (--len); |
133 | while (--len); |
100 | } |
134 | } |
101 | } |
135 | } |
102 | while (op < out_end && ip < in_end); |
136 | while (ip < in_end); |
103 | |
137 | |
104 | return op - (u8 *)out_data; |
138 | return op - (u8 *)out_data; |
105 | } |
139 | } |
106 | |
140 | |