libavutil/lzo.c
517840c6
 /*
  * LZO 1x decompression
  * Copyright (c) 2006 Reimar Doeffinger
  *
b78e7197
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
517840c6
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
b78e7197
  * version 2.1 of the License, or (at your option) any later version.
517840c6
  *
b78e7197
  * FFmpeg is distributed in the hope that it will be useful,
517840c6
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
b78e7197
  * License along with FFmpeg; if not, write to the Free Software
5509bffa
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
517840c6
  */
0d08e270
 
8fb1e264
 #include <string.h>
 
0d08e270
 #include "avutil.h"
cf2b7c01
 #include "avassert.h"
517840c6
 #include "common.h"
ceb754d0
 #include "intreadwrite.h"
517840c6
 #include "lzo.h"
 
c68fafe0
 /// Define if we may write up to 12 bytes beyond the output buffer.
f53a2931
 #define OUTBUF_PADDED 1
c68fafe0
 /// Define if we may read up to 8 bytes beyond the input buffer.
f53a2931
 #define INBUF_PADDED 1
c0853697
 
517840c6
 typedef struct LZOContext {
5e038b4f
     const uint8_t *in, *in_end;
f53a2931
     uint8_t *out_start, *out, *out_end;
517840c6
     int error;
 } LZOContext;
 
 /**
adbfc605
  * @brief Reads one byte from the input buffer, avoiding an overrun.
  * @return byte read
517840c6
  */
c0853697
 static inline int get_byte(LZOContext *c)
 {
517840c6
     if (c->in < c->in_end)
         return *c->in++;
0b178e56
     c->error |= AV_LZO_INPUT_DEPLETED;
d3c71c50
     return 1;
517840c6
 }
 
a737f1df
 #ifdef INBUF_PADDED
 #define GETB(c) (*(c).in++)
 #else
 #define GETB(c) get_byte(&(c))
 #endif
 
517840c6
 /**
adbfc605
  * @brief Decodes a length value in the coding used by lzo.
  * @param x previous byte value
  * @param mask bits used from x
  * @return decoded length value
517840c6
  */
c0853697
 static inline int get_len(LZOContext *c, int x, int mask)
 {
517840c6
     int cnt = x & mask;
     if (!cnt) {
d6af26c5
         while (!(x = get_byte(c))) {
             if (cnt >= INT_MAX - 1000) {
                 c->error |= AV_LZO_ERROR;
                 break;
             }
c0853697
             cnt += 255;
d6af26c5
         }
517840c6
         cnt += mask + x;
     }
     return cnt;
 }
 
 /**
adbfc605
  * @brief Copies bytes from input to output buffer with checking.
  * @param cnt number of bytes to copy, must be >= 0
517840c6
  */
c0853697
 static inline void copy(LZOContext *c, int cnt)
 {
5e038b4f
     register const uint8_t *src = c->in;
c0853697
     register uint8_t *dst       = c->out;
cf2b7c01
     av_assert0(cnt >= 0);
c0a8b876
     if (cnt > c->in_end - src) {
c0853697
         cnt       = FFMAX(c->in_end - src, 0);
0b178e56
         c->error |= AV_LZO_INPUT_DEPLETED;
517840c6
     }
c0a8b876
     if (cnt > c->out_end - dst) {
c0853697
         cnt       = FFMAX(c->out_end - dst, 0);
0b178e56
         c->error |= AV_LZO_OUTPUT_FULL;
517840c6
     }
f53a2931
 #if defined(INBUF_PADDED) && defined(OUTBUF_PADDED)
ceb754d0
     AV_COPY32U(dst, src);
f53a2931
     src += 4;
     dst += 4;
     cnt -= 4;
     if (cnt > 0)
 #endif
c0853697
     memcpy(dst, src, cnt);
     c->in  = src + cnt;
f53a2931
     c->out = dst + cnt;
517840c6
 }
 
 /**
adbfc605
  * @brief Copies previously decoded bytes to current position.
874da652
  * @param back how many bytes back we start, must be > 0
ccda51b1
  * @param cnt number of bytes to copy, must be > 0
517840c6
  *
f53a2931
  * cnt > back is valid, this will copy the bytes we just copied,
  * thus creating a repeating pattern with a period length of back.
517840c6
  */
c0853697
 static inline void copy_backptr(LZOContext *c, int back, int cnt)
 {
     register uint8_t *dst       = c->out;
cf2b7c01
     av_assert0(cnt > 0);
ca6c3f2c
     if (dst - c->out_start < back) {
0b178e56
         c->error |= AV_LZO_INVALID_BACKPTR;
517840c6
         return;
     }
c0a8b876
     if (cnt > c->out_end - dst) {
c0853697
         cnt       = FFMAX(c->out_end - dst, 0);
0b178e56
         c->error |= AV_LZO_OUTPUT_FULL;
517840c6
     }
5bac2d0c
     av_memcpy_backptr(dst, back, cnt);
6ba10f33
     c->out = dst + cnt;
 }
 
c0853697
 int av_lzo1x_decode(void *out, int *outlen, const void *in, int *inlen)
 {
     int state = 0;
517840c6
     int x;
     LZOContext c;
b9242fd1
     if (*outlen <= 0 || *inlen <= 0) {
7d508260
         int res = 0;
b9242fd1
         if (*outlen <= 0)
7d508260
             res |= AV_LZO_OUTPUT_FULL;
b9242fd1
         if (*inlen <= 0)
7d508260
             res |= AV_LZO_INPUT_DEPLETED;
         return res;
     }
c0853697
     c.in      = in;
     c.in_end  = (const uint8_t *)in + *inlen;
     c.out     = c.out_start = out;
     c.out_end = (uint8_t *)out + *outlen;
     c.error   = 0;
     x         = GETB(c);
517840c6
     if (x > 17) {
         copy(&c, x - 17);
a737f1df
         x = GETB(c);
c0853697
         if (x < 16)
             c.error |= AV_LZO_ERROR;
517840c6
     }
5fe9c42c
     if (c.in > c.in_end)
0b178e56
         c.error |= AV_LZO_INPUT_DEPLETED;
517840c6
     while (!c.error) {
         int cnt, back;
801778bc
         if (x > 15) {
             if (x > 63) {
c0853697
                 cnt  = (x >> 5) - 1;
a737f1df
                 back = (GETB(c) << 3) + ((x >> 2) & 7) + 1;
801778bc
             } else if (x > 31) {
c0853697
                 cnt  = get_len(&c, x, 31);
                 x    = GETB(c);
a737f1df
                 back = (GETB(c) << 6) + (x >> 2) + 1;
517840c6
             } else {
c0853697
                 cnt   = get_len(&c, x, 7);
                 back  = (1 << 14) + ((x & 8) << 11);
                 x     = GETB(c);
a737f1df
                 back += (GETB(c) << 6) + (x >> 2);
517840c6
                 if (back == (1 << 14)) {
                     if (cnt != 1)
0b178e56
                         c.error |= AV_LZO_ERROR;
517840c6
                     break;
                 }
             }
c0853697
         } else if (!state) {
             cnt = get_len(&c, x, 15);
             copy(&c, cnt + 3);
             x = GETB(c);
             if (x > 15)
                 continue;
             cnt  = 1;
             back = (1 << 11) + (GETB(c) << 2) + (x >> 2) + 1;
bf47272f
         } else {
c0853697
             cnt  = 0;
             back = (GETB(c) << 2) + (x >> 2) + 1;
517840c6
         }
         copy_backptr(&c, back, cnt + 2);
c0853697
         state =
         cnt   = x & 3;
56f8647a
         copy(&c, cnt);
a737f1df
         x = GETB(c);
517840c6
     }
     *inlen = c.in_end - c.in;
a737f1df
     if (c.in > c.in_end)
         *inlen = 0;
517840c6
     *outlen = c.out_end - c.out;
     return c.error;
 }
266aa26c
 
 #ifdef TEST
 #include <stdio.h>
 #include <lzo/lzo1x.h>
 #include "log.h"
 #define MAXSZ (10*1024*1024)
985fdd53
 
 /* Define one of these to 1 if you wish to benchmark liblzo
  * instead of our native implementation. */
 #define BENCHMARK_LIBLZO_SAFE   0
 #define BENCHMARK_LIBLZO_UNSAFE 0
 
266aa26c
 int main(int argc, char *argv[]) {
     FILE *in = fopen(argv[1], "rb");
9326caa5
     int comp_level = argc > 2 ? atoi(argv[2]) : 0;
266aa26c
     uint8_t *orig = av_malloc(MAXSZ + 16);
     uint8_t *comp = av_malloc(2*MAXSZ + 16);
     uint8_t *decomp = av_malloc(MAXSZ + 16);
     size_t s = fread(orig, 1, MAXSZ, in);
     lzo_uint clen = 0;
     long tmp[LZO1X_MEM_COMPRESS];
     int inlen, outlen;
     int i;
82a6ef20
     av_log_set_level(AV_LOG_DEBUG);
9326caa5
     if (comp_level == 0) {
         lzo1x_1_compress(orig, s, comp, &clen, tmp);
     } else if (comp_level == 11) {
         lzo1x_1_11_compress(orig, s, comp, &clen, tmp);
     } else if (comp_level == 12) {
         lzo1x_1_12_compress(orig, s, comp, &clen, tmp);
     } else if (comp_level == 15) {
         lzo1x_1_15_compress(orig, s, comp, &clen, tmp);
     } else
         lzo1x_999_compress(orig, s, comp, &clen, tmp);
266aa26c
     for (i = 0; i < 300; i++) {
 START_TIMER
         inlen = clen; outlen = MAXSZ;
985fdd53
 #if BENCHMARK_LIBLZO_SAFE
d62a0c1e
         if (lzo1x_decompress_safe(comp, inlen, decomp, &outlen, NULL))
985fdd53
 #elif BENCHMARK_LIBLZO_UNSAFE
d62a0c1e
         if (lzo1x_decompress(comp, inlen, decomp, &outlen, NULL))
 #else
0b178e56
         if (av_lzo1x_decode(decomp, &outlen, comp, &inlen))
d62a0c1e
 #endif
266aa26c
             av_log(NULL, AV_LOG_ERROR, "decompression error\n");
 STOP_TIMER("lzod")
     }
     if (memcmp(orig, decomp, s))
         av_log(NULL, AV_LOG_ERROR, "decompression incorrect\n");
     else
89c9ff50
         av_log(NULL, AV_LOG_ERROR, "decompression OK\n");
c44d8d95
     fclose(in);
266aa26c
     return 0;
 }
 #endif