From e89aa4bf56e5b5c45f569eb12733519789e057da Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Thu, 28 Nov 2013 10:54:35 +0100 Subject: [PATCH] lzw: switch to bytestream2 Found-by: Mateusz "j00ru" Jurczyk and Gynvael Coldwind CC:libav-stable@libav.org --- libavcodec/lzw.c | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/libavcodec/lzw.c b/libavcodec/lzw.c index 016714021c..fae5687ab7 100644 --- a/libavcodec/lzw.c +++ b/libavcodec/lzw.c @@ -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;