lzo.c
Go to the documentation of this file.
1 /*
2  * LZO 1x decompression
3  * Copyright (c) 2006 Reimar Doeffinger
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "avutil.h"
23 #include "common.h"
25 #undef memcpy
26 #include <string.h>
27 #include "lzo.h"
28 
30 #define OUTBUF_PADDED 1
31 
32 #define INBUF_PADDED 1
33 typedef struct LZOContext {
34  const uint8_t *in, *in_end;
35  uint8_t *out_start, *out, *out_end;
36  int error;
37 } LZOContext;
38 
43 static inline int get_byte(LZOContext *c) {
44  if (c->in < c->in_end)
45  return *c->in++;
47  return 1;
48 }
49 
50 #ifdef INBUF_PADDED
51 #define GETB(c) (*(c).in++)
52 #else
53 #define GETB(c) get_byte(&(c))
54 #endif
55 
62 static inline int get_len(LZOContext *c, int x, int mask) {
63  int cnt = x & mask;
64  if (!cnt) {
65  while (!(x = get_byte(c))) cnt += 255;
66  cnt += mask + x;
67  }
68  return cnt;
69 }
70 
71 //#define UNALIGNED_LOADSTORE
72 #define BUILTIN_MEMCPY
73 #ifdef UNALIGNED_LOADSTORE
74 #define COPY2(d, s) *(uint16_t *)(d) = *(uint16_t *)(s);
75 #define COPY4(d, s) *(uint32_t *)(d) = *(uint32_t *)(s);
76 #elif defined(BUILTIN_MEMCPY)
77 #define COPY2(d, s) memcpy(d, s, 2);
78 #define COPY4(d, s) memcpy(d, s, 4);
79 #else
80 #define COPY2(d, s) (d)[0] = (s)[0]; (d)[1] = (s)[1];
81 #define COPY4(d, s) (d)[0] = (s)[0]; (d)[1] = (s)[1]; (d)[2] = (s)[2]; (d)[3] = (s)[3];
82 #endif
83 
88 static inline void copy(LZOContext *c, int cnt) {
89  register const uint8_t *src = c->in;
90  register uint8_t *dst = c->out;
91  if (cnt < 0) {
92  c->error |= AV_LZO_ERROR;
93  return;
94  }
95  if (cnt > c->in_end - src) {
96  cnt = FFMAX(c->in_end - src, 0);
98  }
99  if (cnt > c->out_end - dst) {
100  cnt = FFMAX(c->out_end - dst, 0);
102  }
103 #if defined(INBUF_PADDED) && defined(OUTBUF_PADDED)
104  COPY4(dst, src);
105  src += 4;
106  dst += 4;
107  cnt -= 4;
108  if (cnt > 0)
109 #endif
110  memcpy(dst, src, cnt);
111  c->in = src + cnt;
112  c->out = dst + cnt;
113 }
114 
115 static inline void memcpy_backptr(uint8_t *dst, int back, int cnt);
116 
125 static inline void copy_backptr(LZOContext *c, int back, int cnt) {
126  register uint8_t *dst = c->out;
127  if (cnt <= 0) {
128  c->error |= AV_LZO_ERROR;
129  return;
130  }
131  if (dst - c->out_start < back) {
133  return;
134  }
135  if (cnt > c->out_end - dst) {
136  cnt = FFMAX(c->out_end - dst, 0);
138  }
139  memcpy_backptr(dst, back, cnt);
140  c->out = dst + cnt;
141 }
142 
143 static inline void memcpy_backptr(uint8_t *dst, int back, int cnt) {
144  const uint8_t *src = &dst[-back];
145  if (back == 1) {
146  memset(dst, *src, cnt);
147  } else {
148 #ifdef OUTBUF_PADDED
149  COPY2(dst, src);
150  COPY2(dst + 2, src + 2);
151  src += 4;
152  dst += 4;
153  cnt -= 4;
154  if (cnt > 0) {
155  COPY2(dst, src);
156  COPY2(dst + 2, src + 2);
157  COPY2(dst + 4, src + 4);
158  COPY2(dst + 6, src + 6);
159  src += 8;
160  dst += 8;
161  cnt -= 8;
162  }
163 #endif
164  if (cnt > 0) {
165  int blocklen = back;
166  while (cnt > blocklen) {
167  memcpy(dst, src, blocklen);
168  dst += blocklen;
169  cnt -= blocklen;
170  blocklen <<= 1;
171  }
172  memcpy(dst, src, cnt);
173  }
174  }
175 }
176 
177 void av_memcpy_backptr(uint8_t *dst, int back, int cnt) {
178  memcpy_backptr(dst, back, cnt);
179 }
180 
181 int av_lzo1x_decode(void *out, int *outlen, const void *in, int *inlen) {
182  int state= 0;
183  int x;
184  LZOContext c;
185  if (!*outlen || !*inlen) {
186  int res = 0;
187  if (!*outlen)
188  res |= AV_LZO_OUTPUT_FULL;
189  if (!*inlen)
190  res |= AV_LZO_INPUT_DEPLETED;
191  return res;
192  }
193  c.in = in;
194  c.in_end = (const uint8_t *)in + *inlen;
195  c.out = c.out_start = out;
196  c.out_end = (uint8_t *)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) c.error |= AV_LZO_ERROR;
203  }
204  if (c.in > c.in_end)
206  while (!c.error) {
207  int cnt, back;
208  if (x > 15) {
209  if (x > 63) {
210  cnt = (x >> 5) - 1;
211  back = (GETB(c) << 3) + ((x >> 2) & 7) + 1;
212  } else if (x > 31) {
213  cnt = get_len(&c, x, 31);
214  x = GETB(c);
215  back = (GETB(c) << 6) + (x >> 2) + 1;
216  } else {
217  cnt = get_len(&c, x, 7);
218  back = (1 << 14) + ((x & 8) << 11);
219  x = GETB(c);
220  back += (GETB(c) << 6) + (x >> 2);
221  if (back == (1 << 14)) {
222  if (cnt != 1)
223  c.error |= AV_LZO_ERROR;
224  break;
225  }
226  }
227  } else if(!state){
228  cnt = get_len(&c, x, 15);
229  copy(&c, cnt + 3);
230  x = GETB(c);
231  if (x > 15)
232  continue;
233  cnt = 1;
234  back = (1 << 11) + (GETB(c) << 2) + (x >> 2) + 1;
235  } else {
236  cnt = 0;
237  back = (GETB(c) << 2) + (x >> 2) + 1;
238  }
239  copy_backptr(&c, back, cnt + 2);
240  state=
241  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 
258 /* Define one of these to 1 if you wish to benchmark liblzo
259  * instead of our native implementation. */
260 #define BENCHMARK_LIBLZO_SAFE 0
261 #define BENCHMARK_LIBLZO_UNSAFE 0
262 
263 int main(int argc, char *argv[]) {
264  FILE *in = fopen(argv[1], "rb");
265  uint8_t *orig = av_malloc(MAXSZ + 16);
266  uint8_t *comp = av_malloc(2*MAXSZ + 16);
267  uint8_t *decomp = av_malloc(MAXSZ + 16);
268  size_t s = fread(orig, 1, MAXSZ, in);
269  lzo_uint clen = 0;
270  long tmp[LZO1X_MEM_COMPRESS];
271  int inlen, outlen;
272  int i;
274  lzo1x_999_compress(orig, s, comp, &clen, tmp);
275  for (i = 0; i < 300; i++) {
277  inlen = clen; outlen = MAXSZ;
278 #if BENCHMARK_LIBLZO_SAFE
279  if (lzo1x_decompress_safe(comp, inlen, decomp, &outlen, NULL))
280 #elif BENCHMARK_LIBLZO_UNSAFE
281  if (lzo1x_decompress(comp, inlen, decomp, &outlen, NULL))
282 #else
283  if (av_lzo1x_decode(decomp, &outlen, comp, &inlen))
284 #endif
285  av_log(NULL, AV_LOG_ERROR, "decompression error\n");
286 STOP_TIMER("lzod")
287  }
288  if (memcmp(orig, decomp, s))
289  av_log(NULL, AV_LOG_ERROR, "decompression incorrect\n");
290  else
291  av_log(NULL, AV_LOG_ERROR, "decompression OK\n");
292  return 0;
293 }
294 #endif