From 067ada04d19629fb0afefffda27bcd5ebdffc0e8 Mon Sep 17 00:00:00 2001 From: Michael Niedermayer Date: Sat, 29 Mar 2014 03:18:45 +0100 Subject: avcodec/xbmdec: redesign parser to handle more cases The new code is more tolerant on the syntax Fixes decoding of bm1.xbm Signed-off-by: Michael Niedermayer --- libavcodec/xbmdec.c | 56 +++++++++++++++++++++++++++++------------------------ 1 file changed, 31 insertions(+), 25 deletions(-) (limited to 'libavcodec') diff --git a/libavcodec/xbmdec.c b/libavcodec/xbmdec.c index 203c2f9cbe..a2c36595e9 100644 --- a/libavcodec/xbmdec.c +++ b/libavcodec/xbmdec.c @@ -37,6 +37,27 @@ static int convert(uint8_t x) return x; } +static int parse_str_int(const uint8_t *p, int len, const uint8_t *key) +{ + const uint8_t *end = p + len; + + for(; p= end) + return INT_MIN; + + for(; pdata; + const uint8_t *next; uint8_t *dst; avctx->pix_fmt = AV_PIX_FMT_MONOWHITE; end = avpkt->data + avpkt->size; - while (!width || !height) { - char name[256]; - int number, len; - - ptr += strcspn(ptr, "#"); - if (ptr >= avpkt->data + avpkt->size) { - av_log(avctx, AV_LOG_ERROR, "End of file reached.\n"); - return AVERROR_INVALIDDATA; - } - if (sscanf(ptr, "#define %255s %u", name, &number) != 2) { - av_log(avctx, AV_LOG_ERROR, "Unexpected preprocessor directive\n"); - return AVERROR_INVALIDDATA; - } - len = strlen(name); - if ((len > 6) && !height && !memcmp(name + len - 7, "_height", 7)) { - height = number; - } else if ((len > 5) && !width && !memcmp(name + len - 6, "_width", 6)) { - width = number; - } else { - av_log(avctx, AV_LOG_WARNING, "Unknown define '%s'\n", name); - } - ptr += strcspn(ptr, "\n\r") + 1; - } + width = parse_str_int(avpkt->data, avpkt->size, "_width"); + height = parse_str_int(avpkt->data, avpkt->size, "_height"); if ((ret = ff_set_dimensions(avctx, width, height)) < 0) return ret; @@ -81,7 +82,12 @@ static int xbm_decode_frame(AVCodecContext *avctx, void *data, return ret; // goto start of image data - ptr += strcspn(ptr, "{") + 1; + next = ptr + strcspn(ptr, "{"); + if (!*next) + next = ptr + strcspn(ptr, "("); + if (!*next) + return AVERROR_INVALIDDATA; + ptr = next + 1; linesize = (avctx->width + 7) / 8; for (i = 0; i < avctx->height; i++) { @@ -89,7 +95,7 @@ static int xbm_decode_frame(AVCodecContext *avctx, void *data, for (j = 0; j < linesize; j++) { uint8_t val; - ptr += strcspn(ptr, "x") + 1; + ptr += strcspn(ptr, "x$") + 1; if (ptr < end && av_isxdigit(*ptr)) { val = convert(*ptr++); if (av_isxdigit(*ptr)) -- cgit v1.2.3