1 /*
2 * LZO 1x decompression
3 * Copyright (c) 2006 Reimar Doeffinger
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <gst/gst.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include "lzo.h"
29
30 /*! define if we may write up to 12 bytes beyond the output buffer */
31 /* #define OUTBUF_PADDED 1 */
32 /*! define if we may read up to 8 bytes beyond the input buffer */
33 /* #define INBUF_PADDED 1 */
34 typedef struct LZOContext
35 {
36 const guint8 *in, *in_end;
37 guint8 *out_start, *out, *out_end;
38 int error;
39 } LZOContext;
40
41 /*
42 * \brief read one byte from input buffer, avoiding overrun
43 * \return byte read
44 */
45 static inline int
get_byte(LZOContext * c)46 get_byte (LZOContext * c)
47 {
48 if (c->in < c->in_end)
49 return *c->in++;
50 c->error |= LZO_INPUT_DEPLETED;
51 return 1;
52 }
53
54 #ifdef INBUF_PADDED
55 #define GETB(c) (*(c).in++)
56 #else
57 #define GETB(c) get_byte(&(c))
58 #endif
59
60 /*
61 * \brief decode a length value in the coding used by lzo
62 * \param x previous byte value
63 * \param mask bits used from x
64 * \return decoded length value
65 */
66 static inline int
get_len(LZOContext * c,int x,int mask)67 get_len (LZOContext * c, int x, int mask)
68 {
69 int cnt = x & mask;
70 if (!cnt) {
71 while (!(x = get_byte (c)))
72 cnt += 255;
73 cnt += mask + x;
74 }
75 return cnt;
76 }
77
78 /*#define UNALIGNED_LOADSTORE */
79 #define BUILTIN_MEMCPY
80 #ifdef UNALIGNED_LOADSTORE
81 #define COPY2(d, s) *(guint16 *)(d) = *(guint16 *)(s);
82 #define COPY4(d, s) *(guint32 *)(d) = *(guint32 *)(s);
83 #elif defined(BUILTIN_MEMCPY)
84 #define COPY2(d, s) memcpy(d, s, 2);
85 #define COPY4(d, s) memcpy(d, s, 4);
86 #else
87 #define COPY2(d, s) (d)[0] = (s)[0]; (d)[1] = (s)[1];
88 #define COPY4(d, s) (d)[0] = (s)[0]; (d)[1] = (s)[1]; (d)[2] = (s)[2]; (d)[3] = (s)[3];
89 #endif
90
91 /*
92 * \brief copy bytes from input to output buffer with checking
93 * \param cnt number of bytes to copy, must be >= 0
94 */
95 static inline void
copy(LZOContext * c,int cnt)96 copy (LZOContext * c, int cnt)
97 {
98 register const guint8 *src = c->in;
99 register guint8 *dst = c->out;
100 if (cnt > c->in_end - src) {
101 cnt = MAX (c->in_end - src, 0);
102 c->error |= LZO_INPUT_DEPLETED;
103 }
104 if (cnt > c->out_end - dst) {
105 cnt = MAX (c->out_end - dst, 0);
106 c->error |= LZO_OUTPUT_FULL;
107 }
108 #if defined(INBUF_PADDED) && defined(OUTBUF_PADDED)
109 COPY4 (dst, src);
110 src += 4;
111 dst += 4;
112 cnt -= 4;
113 if (cnt > 0)
114 #endif
115 memcpy (dst, src, cnt);
116 c->in = src + cnt;
117 c->out = dst + cnt;
118 }
119
120 /*
121 * \brief copy previously decoded bytes to current position
122 * \param back how many bytes back we start
123 * \param cnt number of bytes to copy, must be >= 0
124 *
125 * cnt > back is valid, this will copy the bytes we just copied,
126 * thus creating a repeating pattern with a period length of back.
127 */
128 static inline void
copy_backptr(LZOContext * c,int back,int cnt)129 copy_backptr (LZOContext * c, int back, int cnt)
130 {
131 register const guint8 *src = &c->out[-back];
132 register guint8 *dst = c->out;
133 if (src < c->out_start || src > dst) {
134 c->error |= LZO_INVALID_BACKPTR;
135 return;
136 }
137 if (cnt > c->out_end - dst) {
138 cnt = MAX (c->out_end - dst, 0);
139 c->error |= LZO_OUTPUT_FULL;
140 }
141 if (back == 1) {
142 memset (dst, *src, cnt);
143 dst += cnt;
144 } else {
145 #ifdef OUTBUF_PADDED
146 COPY2 (dst, src);
147 COPY2 (dst + 2, src + 2);
148 src += 4;
149 dst += 4;
150 cnt -= 4;
151 if (cnt > 0) {
152 COPY2 (dst, src);
153 COPY2 (dst + 2, src + 2);
154 COPY2 (dst + 4, src + 4);
155 COPY2 (dst + 6, src + 6);
156 src += 8;
157 dst += 8;
158 cnt -= 8;
159 }
160 #endif
161 if (cnt > 0) {
162 int blocklen = back;
163 while (cnt > blocklen) {
164 memcpy (dst, src, blocklen);
165 dst += blocklen;
166 cnt -= blocklen;
167 blocklen <<= 1;
168 }
169 memcpy (dst, src, cnt);
170 }
171 dst += cnt;
172 }
173 c->out = dst;
174 }
175
176 /*
177 * \brief decode LZO 1x compressed data
178 * \param out output buffer
179 * \param outlen size of output buffer, number of bytes left are returned here
180 * \param in input buffer
181 * \param inlen size of input buffer, number of bytes left are returned here
182 * \return 0 on success, otherwise error flags, see lzo.h
183 *
184 * make sure all buffers are appropriately padded, in must provide
185 * LZO_INPUT_PADDING, out must provide LZO_OUTPUT_PADDING additional bytes
186 */
187 int
lzo1x_decode(void * out,int * outlen,const void * in,int * inlen)188 lzo1x_decode (void *out, int *outlen, const void *in, int *inlen)
189 {
190 int state = 0;
191 int x;
192 LZOContext c;
193 c.in = in;
194 c.in_end = (const guint8 *) in + *inlen;
195 c.out = c.out_start = out;
196 c.out_end = (guint8 *) out + *outlen;
197 c.error = 0;
198 x = GETB (c);
199 if (x > 17) {
200 copy (&c, x - 17);
201 x = GETB (c);
202 if (x < 16)
203 c.error |= LZO_ERROR;
204 }
205 if (c.in > c.in_end)
206 c.error |= LZO_INPUT_DEPLETED;
207 while (!c.error) {
208 int cnt, back;
209 if (x > 15) {
210 if (x > 63) {
211 cnt = (x >> 5) - 1;
212 back = (GETB (c) << 3) + ((x >> 2) & 7) + 1;
213 } else if (x > 31) {
214 cnt = get_len (&c, x, 31);
215 x = GETB (c);
216 back = (GETB (c) << 6) + (x >> 2) + 1;
217 } else {
218 cnt = get_len (&c, x, 7);
219 back = (1 << 14) + ((x & 8) << 11);
220 x = GETB (c);
221 back += (GETB (c) << 6) + (x >> 2);
222 if (back == (1 << 14)) {
223 if (cnt != 1)
224 c.error |= LZO_ERROR;
225 break;
226 }
227 }
228 } else if (!state) {
229 cnt = get_len (&c, x, 15);
230 copy (&c, cnt + 3);
231 x = GETB (c);
232 if (x > 15)
233 continue;
234 cnt = 1;
235 back = (1 << 11) + (GETB (c) << 2) + (x >> 2) + 1;
236 } else {
237 cnt = 0;
238 back = (GETB (c) << 2) + (x >> 2) + 1;
239 }
240 copy_backptr (&c, back, cnt + 2);
241 state = cnt = x & 3;
242 copy (&c, cnt);
243 x = GETB (c);
244 }
245 *inlen = c.in_end - c.in;
246 if (c.in > c.in_end)
247 *inlen = 0;
248 *outlen = c.out_end - c.out;
249 return c.error;
250 }
251
252 #ifdef TEST
253 #include <stdio.h>
254 #include <lzo/lzo1x.h>
255 #include "log.h"
256 #define MAXSZ (10*1024*1024)
257 int
main(int argc,char * argv[])258 main (int argc, char *argv[])
259 {
260 FILE *in = fopen (argv[1], "rb");
261 guint8 *orig = av_malloc (MAXSZ + 16);
262 guint8 *comp = av_malloc (2 * MAXSZ + 16);
263 guint8 *decomp = av_malloc (MAXSZ + 16);
264 gsize s = fread (orig, 1, MAXSZ, in);
265 lzo_uint clen = 0;
266 long tmp[LZO1X_MEM_COMPRESS];
267 int inlen, outlen;
268 int i;
269 av_log_level = AV_LOG_DEBUG;
270 lzo1x_999_compress (orig, s, comp, &clen, tmp);
271 for (i = 0; i < 300; i++) {
272 START_TIMER inlen = clen;
273 outlen = MAXSZ;
274 #ifdef LIBLZO
275 if (lzo1x_decompress_safe (comp, inlen, decomp, &outlen, NULL))
276 #elif defined(LIBLZO_UNSAFE)
277 if (lzo1x_decompress (comp, inlen, decomp, &outlen, NULL))
278 #else
279 if (lzo1x_decode (decomp, &outlen, comp, &inlen))
280 #endif
281 av_log (NULL, AV_LOG_ERROR, "decompression error\n");
282 STOP_TIMER ("lzod")
283 }
284 if (memcmp (orig, decomp, s))
285 av_log (NULL, AV_LOG_ERROR, "decompression incorrect\n");
286 else
287 av_log (NULL, AV_LOG_ERROR, "decompression ok\n");
288
289 fclose (in);
290 return 0;
291 }
292 #endif
293