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