1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2024-12-23 12:43:46 +02:00

lzw: switch to bytestream2

Found-by: Mateusz "j00ru" Jurczyk and Gynvael Coldwind
CC:libav-stable@libav.org
This commit is contained in:
Anton Khirnov 2013-11-28 10:54:35 +01:00
parent 1b5d065ca7
commit e89aa4bf56

View File

@ -28,6 +28,7 @@
*/
#include "avcodec.h"
#include "bytestream.h"
#include "lzw.h"
#include "libavutil/mem.h"
@ -43,7 +44,7 @@ static const uint16_t mask[17] =
};
struct LZWState {
const uint8_t *buf_start, *pbuf, *ebuf;
GetByteContext gb;
int bbits;
unsigned int bbuf;
@ -73,9 +74,9 @@ static int lzw_get_code(struct LZWState * s)
if(s->mode == FF_LZW_GIF) {
while (s->bbits < s->cursize) {
if (!s->bs) {
s->bs = *s->pbuf++;
s->bs = bytestream2_get_byte(&s->gb);
}
s->bbuf |= (*s->pbuf++) << s->bbits;
s->bbuf |= bytestream2_get_byte(&s->gb) << s->bbits;
s->bbits += 8;
s->bs--;
}
@ -83,7 +84,7 @@ static int lzw_get_code(struct LZWState * s)
s->bbuf >>= s->cursize;
} else { // TIFF
while (s->bbits < s->cursize) {
s->bbuf = (s->bbuf << 8) | (*s->pbuf++);
s->bbuf = (s->bbuf << 8) | bytestream2_get_byte(&s->gb);
s->bbits += 8;
}
c = s->bbuf >> (s->bbits - s->cursize);
@ -95,7 +96,7 @@ static int lzw_get_code(struct LZWState * s)
int ff_lzw_size_read(LZWState *p)
{
struct LZWState *s = p;
return s->pbuf - s->buf_start;
return bytestream2_tell(&s->gb);
}
void ff_lzw_decode_tail(LZWState *p)
@ -103,17 +104,12 @@ void ff_lzw_decode_tail(LZWState *p)
struct LZWState *s = (struct LZWState *)p;
if(s->mode == FF_LZW_GIF) {
while (s->bs > 0) {
if (s->bs >= s->ebuf - s->pbuf) {
s->pbuf = s->ebuf;
break;
} else {
s->pbuf += s->bs;
s->bs = *s->pbuf++;
}
while (s->bs > 0 && bytestream2_get_bytes_left(&s->gb)) {
bytestream2_skip(&s->gb, s->bs);
s->bs = bytestream2_get_byte(&s->gb);
}
}else
s->pbuf= s->ebuf;
bytestream2_skip(&s->gb, bytestream2_get_bytes_left(&s->gb));
}
av_cold void ff_lzw_decode_open(LZWState **p)
@ -141,8 +137,7 @@ int ff_lzw_decode_init(LZWState *p, int csize, const uint8_t *buf, int buf_size,
if(csize < 1 || csize >= LZW_MAXBITS)
return -1;
/* read buffer */
s->pbuf = buf;
s->ebuf = s->pbuf + buf_size;
bytestream2_init(&s->gb, buf, buf_size);
s->bbuf = 0;
s->bbits = 0;
s->bs = 0;