1 | /* |
1 | /* |
2 | * Copyright (c) 2000-2005 Marc Alexander Lehmann <schmorp@schmorp.de> |
2 | * Copyright (c) 2000-2007 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, |
8 | * this list of conditions and the following disclaimer. |
8 | * this list of conditions and the following disclaimer. |
9 | * |
9 | * |
10 | * 2. Redistributions in binary form must reproduce the above copyright |
10 | * 2. Redistributions in binary form must reproduce the above copyright |
11 | * notice, this list of conditions and the following disclaimer in the |
11 | * notice, this list of conditions and the following disclaimer in the |
12 | * documentation and/or other materials provided with the distribution. |
12 | * documentation and/or other materials provided with the distribution. |
13 | * |
|
|
14 | * 3. The name of the author may not be used to endorse or promote products |
|
|
15 | * derived from this software without specific prior written permission. |
|
|
16 | * |
13 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
18 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- |
15 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- |
19 | * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
16 | * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
20 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- |
17 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- |
… | |
… | |
43 | #else |
40 | #else |
44 | # include <errno.h> |
41 | # include <errno.h> |
45 | # define SET_ERRNO(n) errno = (n) |
42 | # define SET_ERRNO(n) errno = (n) |
46 | #endif |
43 | #endif |
47 | |
44 | |
|
|
45 | #if (__i386 || __amd64) && __GNUC__ >= 3 |
|
|
46 | # define lzf_movsb(dst, src, len) \ |
|
|
47 | asm ("rep movsb" \ |
|
|
48 | : "=D" (dst), "=S" (src), "=c" (len) \ |
|
|
49 | : "0" (dst), "1" (src), "2" (len)); |
|
|
50 | #endif |
|
|
51 | |
48 | unsigned int |
52 | unsigned int |
49 | lzf_decompress (const void *const in_data, unsigned int in_len, |
53 | lzf_decompress (const void *const in_data, unsigned int in_len, |
50 | void *out_data, unsigned int out_len) |
54 | void *out_data, unsigned int out_len) |
51 | { |
55 | { |
52 | u8 const *ip = (const u8 *)in_data; |
56 | u8 const *ip = (const u8 *)in_data; |
… | |
… | |
66 | { |
70 | { |
67 | SET_ERRNO (E2BIG); |
71 | SET_ERRNO (E2BIG); |
68 | return 0; |
72 | return 0; |
69 | } |
73 | } |
70 | |
74 | |
71 | #if USE_MEMCPY |
75 | #if CHECK_INPUT |
|
|
76 | if (ip + ctrl > in_end) |
|
|
77 | { |
|
|
78 | SET_ERRNO (EINVAL); |
|
|
79 | return 0; |
|
|
80 | } |
|
|
81 | #endif |
|
|
82 | |
|
|
83 | #ifdef lzf_movsb |
72 | memcpy (op, ip, ctrl); |
84 | lzf_movsb (op, ip, ctrl); |
73 | op += ctrl; |
|
|
74 | ip += ctrl; |
|
|
75 | #else |
85 | #else |
76 | do |
86 | do |
77 | *op++ = *ip++; |
87 | *op++ = *ip++; |
78 | while (--ctrl); |
88 | while (--ctrl); |
79 | #endif |
89 | #endif |
… | |
… | |
82 | { |
92 | { |
83 | unsigned int len = ctrl >> 5; |
93 | unsigned int len = ctrl >> 5; |
84 | |
94 | |
85 | u8 *ref = op - ((ctrl & 0x1f) << 8) - 1; |
95 | u8 *ref = op - ((ctrl & 0x1f) << 8) - 1; |
86 | |
96 | |
|
|
97 | #if CHECK_INPUT |
|
|
98 | if (ip >= in_end) |
|
|
99 | { |
|
|
100 | SET_ERRNO (EINVAL); |
|
|
101 | return 0; |
|
|
102 | } |
|
|
103 | #endif |
87 | if (len == 7) |
104 | if (len == 7) |
|
|
105 | { |
88 | len += *ip++; |
106 | len += *ip++; |
|
|
107 | #if CHECK_INPUT |
|
|
108 | if (ip >= in_end) |
|
|
109 | { |
|
|
110 | SET_ERRNO (EINVAL); |
|
|
111 | return 0; |
|
|
112 | } |
|
|
113 | #endif |
89 | |
114 | } |
|
|
115 | |
90 | ref -= *ip++; |
116 | ref -= *ip++; |
91 | |
117 | |
92 | if (op + len + 2 > out_end) |
118 | if (op + len + 2 > out_end) |
93 | { |
119 | { |
94 | SET_ERRNO (E2BIG); |
120 | SET_ERRNO (E2BIG); |
… | |
… | |
99 | { |
125 | { |
100 | SET_ERRNO (EINVAL); |
126 | SET_ERRNO (EINVAL); |
101 | return 0; |
127 | return 0; |
102 | } |
128 | } |
103 | |
129 | |
|
|
130 | #ifdef lzf_movsb |
|
|
131 | len += 2; |
|
|
132 | lzf_movsb (op, ref, len); |
|
|
133 | #else |
104 | *op++ = *ref++; |
134 | *op++ = *ref++; |
105 | *op++ = *ref++; |
135 | *op++ = *ref++; |
106 | |
136 | |
107 | do |
137 | do |
108 | *op++ = *ref++; |
138 | *op++ = *ref++; |
109 | while (--len); |
139 | while (--len); |
|
|
140 | #endif |
110 | } |
141 | } |
111 | } |
142 | } |
112 | while (op < out_end && ip < in_end); |
143 | while (ip < in_end); |
113 | |
144 | |
114 | return op - (u8 *)out_data; |
145 | return op - (u8 *)out_data; |
115 | } |
146 | } |
116 | |
147 | |