… | |
… | |
43 | #else |
43 | #else |
44 | # include <errno.h> |
44 | # include <errno.h> |
45 | # define SET_ERRNO(n) errno = (n) |
45 | # define SET_ERRNO(n) errno = (n) |
46 | #endif |
46 | #endif |
47 | |
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 |
|
|
54 | |
48 | unsigned int |
55 | unsigned int |
49 | lzf_decompress (const void *const in_data, unsigned int in_len, |
56 | lzf_decompress (const void *const in_data, unsigned int in_len, |
50 | void *out_data, unsigned int out_len) |
57 | void *out_data, unsigned int out_len) |
51 | { |
58 | { |
52 | u8 const *ip = (const u8 *)in_data; |
59 | u8 const *ip = (const u8 *)in_data; |
… | |
… | |
66 | { |
73 | { |
67 | SET_ERRNO (E2BIG); |
74 | SET_ERRNO (E2BIG); |
68 | return 0; |
75 | return 0; |
69 | } |
76 | } |
70 | |
77 | |
71 | #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 |
72 | memcpy (op, ip, ctrl); |
87 | lzf_movsb (op, ip, ctrl); |
73 | op += ctrl; |
|
|
74 | ip += ctrl; |
|
|
75 | #else |
88 | #else |
76 | do |
89 | do |
77 | *op++ = *ip++; |
90 | *op++ = *ip++; |
78 | while (--ctrl); |
91 | while (--ctrl); |
79 | #endif |
92 | #endif |
… | |
… | |
82 | { |
95 | { |
83 | unsigned int len = ctrl >> 5; |
96 | unsigned int len = ctrl >> 5; |
84 | |
97 | |
85 | u8 *ref = op - ((ctrl & 0x1f) << 8) - 1; |
98 | u8 *ref = op - ((ctrl & 0x1f) << 8) - 1; |
86 | |
99 | |
|
|
100 | #if CHECK_INPUT |
|
|
101 | if (ip >= in_end) |
|
|
102 | { |
|
|
103 | SET_ERRNO (EINVAL); |
|
|
104 | return 0; |
|
|
105 | } |
|
|
106 | #endif |
87 | if (len == 7) |
107 | if (len == 7) |
|
|
108 | { |
88 | 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 |
89 | |
117 | } |
|
|
118 | |
90 | ref -= *ip++; |
119 | ref -= *ip++; |
91 | |
120 | |
92 | if (op + len + 2 > out_end) |
121 | if (op + len + 2 > out_end) |
93 | { |
122 | { |
94 | SET_ERRNO (E2BIG); |
123 | SET_ERRNO (E2BIG); |
… | |
… | |
99 | { |
128 | { |
100 | SET_ERRNO (EINVAL); |
129 | SET_ERRNO (EINVAL); |
101 | return 0; |
130 | return 0; |
102 | } |
131 | } |
103 | |
132 | |
|
|
133 | #ifdef lzf_movsb |
|
|
134 | len += 2; |
|
|
135 | lzf_movsb (op, ref, len); |
|
|
136 | #else |
104 | *op++ = *ref++; |
137 | *op++ = *ref++; |
105 | *op++ = *ref++; |
138 | *op++ = *ref++; |
106 | |
139 | |
107 | do |
140 | do |
108 | *op++ = *ref++; |
141 | *op++ = *ref++; |
109 | while (--len); |
142 | while (--len); |
|
|
143 | #endif |
110 | } |
144 | } |
111 | } |
145 | } |
112 | while (op < out_end && ip < in_end); |
146 | while (ip < in_end); |
113 | |
147 | |
114 | return op - (u8 *)out_data; |
148 | return op - (u8 *)out_data; |
115 | } |
149 | } |
116 | |
150 | |