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;
} |