2006-01-12 18:52:41 +01:00
|
|
|
/*
|
|
|
|
* LZO 1x decompression
|
|
|
|
* Copyright (c) 2006 Reimar Doeffinger
|
|
|
|
*
|
2006-10-07 17:30:46 +02:00
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2006-01-12 18:52:41 +01:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2006-10-07 17:30:46 +02:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2006-01-12 18:52:41 +01:00
|
|
|
*
|
2006-10-07 17:30:46 +02:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2006-01-12 18:52:41 +01:00
|
|
|
* 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
|
2006-10-07 17:30:46 +02:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2006-01-12 23:43:26 +01:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2006-01-12 18:52:41 +01:00
|
|
|
*/
|
2009-02-03 00:13:18 +01:00
|
|
|
|
2012-10-18 16:53:42 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
2009-02-03 00:13:18 +01:00
|
|
|
#include "avutil.h"
|
2014-06-23 14:45:47 +02:00
|
|
|
#include "avassert.h"
|
2006-01-12 18:52:41 +01:00
|
|
|
#include "common.h"
|
2012-10-22 19:02:20 +02:00
|
|
|
#include "intreadwrite.h"
|
2006-01-12 18:52:41 +01:00
|
|
|
#include "lzo.h"
|
|
|
|
|
2011-10-30 18:27:33 +01:00
|
|
|
/// Define if we may write up to 12 bytes beyond the output buffer.
|
2006-01-22 20:10:12 +01:00
|
|
|
#define OUTBUF_PADDED 1
|
2011-10-30 18:27:33 +01:00
|
|
|
/// Define if we may read up to 8 bytes beyond the input buffer.
|
2006-01-22 20:10:12 +01:00
|
|
|
#define INBUF_PADDED 1
|
2012-10-18 19:15:43 +02:00
|
|
|
|
2006-01-12 18:52:41 +01:00
|
|
|
typedef struct LZOContext {
|
2008-02-01 17:30:51 +01:00
|
|
|
const uint8_t *in, *in_end;
|
2006-01-22 20:10:12 +01:00
|
|
|
uint8_t *out_start, *out, *out_end;
|
2006-01-12 18:52:41 +01:00
|
|
|
int error;
|
|
|
|
} LZOContext;
|
|
|
|
|
|
|
|
/**
|
2011-06-23 22:41:54 +02:00
|
|
|
* @brief Reads one byte from the input buffer, avoiding an overrun.
|
|
|
|
* @return byte read
|
2006-01-12 18:52:41 +01:00
|
|
|
*/
|
2012-10-18 19:15:43 +02:00
|
|
|
static inline int get_byte(LZOContext *c)
|
|
|
|
{
|
2006-01-12 18:52:41 +01:00
|
|
|
if (c->in < c->in_end)
|
|
|
|
return *c->in++;
|
2009-02-02 21:16:00 +01:00
|
|
|
c->error |= AV_LZO_INPUT_DEPLETED;
|
2006-01-15 22:33:06 +01:00
|
|
|
return 1;
|
2006-01-12 18:52:41 +01:00
|
|
|
}
|
|
|
|
|
2007-01-28 17:49:51 +01:00
|
|
|
#ifdef INBUF_PADDED
|
|
|
|
#define GETB(c) (*(c).in++)
|
|
|
|
#else
|
|
|
|
#define GETB(c) get_byte(&(c))
|
|
|
|
#endif
|
|
|
|
|
2006-01-12 18:52:41 +01:00
|
|
|
/**
|
2011-06-23 22:41:54 +02:00
|
|
|
* @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
|
2006-01-12 18:52:41 +01:00
|
|
|
*/
|
2012-10-18 19:15:43 +02:00
|
|
|
static inline int get_len(LZOContext *c, int x, int mask)
|
|
|
|
{
|
2006-01-12 18:52:41 +01:00
|
|
|
int cnt = x & mask;
|
|
|
|
if (!cnt) {
|
2014-06-20 03:15:28 +02:00
|
|
|
while (!(x = get_byte(c))) {
|
|
|
|
if (cnt >= INT_MAX - 1000) {
|
|
|
|
c->error |= AV_LZO_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
2012-10-18 19:15:43 +02:00
|
|
|
cnt += 255;
|
2014-06-20 03:15:28 +02:00
|
|
|
}
|
2006-01-12 18:52:41 +01:00
|
|
|
cnt += mask + x;
|
|
|
|
}
|
|
|
|
return cnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-06-23 22:41:54 +02:00
|
|
|
* @brief Copies bytes from input to output buffer with checking.
|
|
|
|
* @param cnt number of bytes to copy, must be >= 0
|
2006-01-12 18:52:41 +01:00
|
|
|
*/
|
2012-10-18 19:15:43 +02:00
|
|
|
static inline void copy(LZOContext *c, int cnt)
|
|
|
|
{
|
2008-02-01 17:30:51 +01:00
|
|
|
register const uint8_t *src = c->in;
|
2012-10-18 19:15:43 +02:00
|
|
|
register uint8_t *dst = c->out;
|
2014-06-23 14:45:47 +02:00
|
|
|
av_assert0(cnt >= 0);
|
2007-01-31 22:00:48 +01:00
|
|
|
if (cnt > c->in_end - src) {
|
2012-10-18 19:15:43 +02:00
|
|
|
cnt = FFMAX(c->in_end - src, 0);
|
2009-02-02 21:16:00 +01:00
|
|
|
c->error |= AV_LZO_INPUT_DEPLETED;
|
2006-01-12 18:52:41 +01:00
|
|
|
}
|
2007-01-31 22:00:48 +01:00
|
|
|
if (cnt > c->out_end - dst) {
|
2012-10-18 19:15:43 +02:00
|
|
|
cnt = FFMAX(c->out_end - dst, 0);
|
2009-02-02 21:16:00 +01:00
|
|
|
c->error |= AV_LZO_OUTPUT_FULL;
|
2006-01-12 18:52:41 +01:00
|
|
|
}
|
2006-01-22 20:10:12 +01:00
|
|
|
#if defined(INBUF_PADDED) && defined(OUTBUF_PADDED)
|
2012-10-22 19:02:20 +02:00
|
|
|
AV_COPY32U(dst, src);
|
2006-01-22 20:10:12 +01:00
|
|
|
src += 4;
|
|
|
|
dst += 4;
|
|
|
|
cnt -= 4;
|
|
|
|
if (cnt > 0)
|
|
|
|
#endif
|
2012-10-18 19:15:43 +02:00
|
|
|
memcpy(dst, src, cnt);
|
|
|
|
c->in = src + cnt;
|
2006-01-22 20:10:12 +01:00
|
|
|
c->out = dst + cnt;
|
2006-01-12 18:52:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-06-23 22:41:54 +02:00
|
|
|
* @brief Copies previously decoded bytes to current position.
|
2011-12-30 10:37:33 +01:00
|
|
|
* @param back how many bytes back we start, must be > 0
|
2014-06-19 23:26:58 +02:00
|
|
|
* @param cnt number of bytes to copy, must be > 0
|
2006-01-12 18:52:41 +01:00
|
|
|
*
|
2006-01-22 20:10:12 +01:00
|
|
|
* cnt > back is valid, this will copy the bytes we just copied,
|
|
|
|
* thus creating a repeating pattern with a period length of back.
|
2006-01-12 18:52:41 +01:00
|
|
|
*/
|
2012-10-18 19:15:43 +02:00
|
|
|
static inline void copy_backptr(LZOContext *c, int back, int cnt)
|
|
|
|
{
|
|
|
|
register uint8_t *dst = c->out;
|
2014-06-23 14:45:47 +02:00
|
|
|
av_assert0(cnt > 0);
|
2013-03-15 11:59:22 +01:00
|
|
|
if (dst - c->out_start < back) {
|
2009-02-02 21:16:00 +01:00
|
|
|
c->error |= AV_LZO_INVALID_BACKPTR;
|
2006-01-12 18:52:41 +01:00
|
|
|
return;
|
|
|
|
}
|
2007-01-31 22:00:48 +01:00
|
|
|
if (cnt > c->out_end - dst) {
|
2012-10-18 19:15:43 +02:00
|
|
|
cnt = FFMAX(c->out_end - dst, 0);
|
2009-02-02 21:16:00 +01:00
|
|
|
c->error |= AV_LZO_OUTPUT_FULL;
|
2006-01-12 18:52:41 +01:00
|
|
|
}
|
2012-10-18 19:27:51 +02:00
|
|
|
av_memcpy_backptr(dst, back, cnt);
|
2008-08-06 10:17:03 +02:00
|
|
|
c->out = dst + cnt;
|
|
|
|
}
|
|
|
|
|
2012-10-18 19:15:43 +02:00
|
|
|
int av_lzo1x_decode(void *out, int *outlen, const void *in, int *inlen)
|
|
|
|
{
|
|
|
|
int state = 0;
|
2006-01-12 18:52:41 +01:00
|
|
|
int x;
|
|
|
|
LZOContext c;
|
2011-11-05 21:45:31 +01:00
|
|
|
if (*outlen <= 0 || *inlen <= 0) {
|
2011-01-24 18:51:00 +01:00
|
|
|
int res = 0;
|
2011-11-05 21:45:31 +01:00
|
|
|
if (*outlen <= 0)
|
2011-01-24 18:51:00 +01:00
|
|
|
res |= AV_LZO_OUTPUT_FULL;
|
2011-11-05 21:45:31 +01:00
|
|
|
if (*inlen <= 0)
|
2011-01-24 18:51:00 +01:00
|
|
|
res |= AV_LZO_INPUT_DEPLETED;
|
|
|
|
return res;
|
|
|
|
}
|
2012-10-18 19:15:43 +02:00
|
|
|
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);
|
2006-01-12 18:52:41 +01:00
|
|
|
if (x > 17) {
|
|
|
|
copy(&c, x - 17);
|
2007-01-28 17:49:51 +01:00
|
|
|
x = GETB(c);
|
2012-10-18 19:15:43 +02:00
|
|
|
if (x < 16)
|
|
|
|
c.error |= AV_LZO_ERROR;
|
2006-01-12 18:52:41 +01:00
|
|
|
}
|
2007-01-31 11:29:58 +01:00
|
|
|
if (c.in > c.in_end)
|
2009-02-02 21:16:00 +01:00
|
|
|
c.error |= AV_LZO_INPUT_DEPLETED;
|
2006-01-12 18:52:41 +01:00
|
|
|
while (!c.error) {
|
|
|
|
int cnt, back;
|
2007-01-31 11:07:22 +01:00
|
|
|
if (x > 15) {
|
|
|
|
if (x > 63) {
|
2012-10-18 19:15:43 +02:00
|
|
|
cnt = (x >> 5) - 1;
|
2007-01-28 17:49:51 +01:00
|
|
|
back = (GETB(c) << 3) + ((x >> 2) & 7) + 1;
|
2007-01-31 11:07:22 +01:00
|
|
|
} else if (x > 31) {
|
2012-10-18 19:15:43 +02:00
|
|
|
cnt = get_len(&c, x, 31);
|
|
|
|
x = GETB(c);
|
2007-01-28 17:49:51 +01:00
|
|
|
back = (GETB(c) << 6) + (x >> 2) + 1;
|
2006-01-12 18:52:41 +01:00
|
|
|
} else {
|
2012-10-18 19:15:43 +02:00
|
|
|
cnt = get_len(&c, x, 7);
|
|
|
|
back = (1 << 14) + ((x & 8) << 11);
|
|
|
|
x = GETB(c);
|
2007-01-28 17:49:51 +01:00
|
|
|
back += (GETB(c) << 6) + (x >> 2);
|
2006-01-12 18:52:41 +01:00
|
|
|
if (back == (1 << 14)) {
|
|
|
|
if (cnt != 1)
|
2009-02-02 21:16:00 +01:00
|
|
|
c.error |= AV_LZO_ERROR;
|
2006-01-12 18:52:41 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-10-18 19:15:43 +02:00
|
|
|
} 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;
|
2007-01-31 11:34:13 +01:00
|
|
|
} else {
|
2012-10-18 19:15:43 +02:00
|
|
|
cnt = 0;
|
|
|
|
back = (GETB(c) << 2) + (x >> 2) + 1;
|
2006-01-12 18:52:41 +01:00
|
|
|
}
|
|
|
|
copy_backptr(&c, back, cnt + 2);
|
2012-10-18 19:15:43 +02:00
|
|
|
state =
|
|
|
|
cnt = x & 3;
|
2007-01-31 21:57:30 +01:00
|
|
|
copy(&c, cnt);
|
2007-01-28 17:49:51 +01:00
|
|
|
x = GETB(c);
|
2006-01-12 18:52:41 +01:00
|
|
|
}
|
|
|
|
*inlen = c.in_end - c.in;
|
2007-01-28 17:49:51 +01:00
|
|
|
if (c.in > c.in_end)
|
|
|
|
*inlen = 0;
|
2006-01-12 18:52:41 +01:00
|
|
|
*outlen = c.out_end - c.out;
|
|
|
|
return c.error;
|
|
|
|
}
|