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 <michaelni@gmx.at>
This commit is contained in:
Michael Niedermayer 2014-03-29 03:18:45 +01:00
parent 657cee1aef
commit 067ada04d1

View File

@ -37,6 +37,27 @@ static int convert(uint8_t x)
return 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 - strlen(key); p++) {
if (!memcmp(p, key, strlen(key)))
break;
}
p += strlen(key);
if (p >= end)
return INT_MIN;
for(; p<end; p++) {
char **eptr;
int64_t ret = strtol(p, &eptr, 10);
if (eptr != p)
return ret;
}
return INT_MIN;
}
static int xbm_decode_frame(AVCodecContext *avctx, void *data, static int xbm_decode_frame(AVCodecContext *avctx, void *data,
int *got_frame, AVPacket *avpkt) int *got_frame, AVPacket *avpkt)
{ {
@ -45,34 +66,14 @@ static int xbm_decode_frame(AVCodecContext *avctx, void *data,
int width = 0; int width = 0;
int height = 0; int height = 0;
const uint8_t *end, *ptr = avpkt->data; const uint8_t *end, *ptr = avpkt->data;
const uint8_t *next;
uint8_t *dst; uint8_t *dst;
avctx->pix_fmt = AV_PIX_FMT_MONOWHITE; avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
end = avpkt->data + avpkt->size; end = avpkt->data + avpkt->size;
while (!width || !height) {
char name[256];
int number, len;
ptr += strcspn(ptr, "#"); width = parse_str_int(avpkt->data, avpkt->size, "_width");
if (ptr >= avpkt->data + avpkt->size) { height = parse_str_int(avpkt->data, avpkt->size, "_height");
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;
}
if ((ret = ff_set_dimensions(avctx, width, height)) < 0) if ((ret = ff_set_dimensions(avctx, width, height)) < 0)
return ret; return ret;
@ -81,7 +82,12 @@ static int xbm_decode_frame(AVCodecContext *avctx, void *data,
return ret; return ret;
// goto start of image data // 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; linesize = (avctx->width + 7) / 8;
for (i = 0; i < avctx->height; i++) { 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++) { for (j = 0; j < linesize; j++) {
uint8_t val; uint8_t val;
ptr += strcspn(ptr, "x") + 1; ptr += strcspn(ptr, "x$") + 1;
if (ptr < end && av_isxdigit(*ptr)) { if (ptr < end && av_isxdigit(*ptr)) {
val = convert(*ptr++); val = convert(*ptr++);
if (av_isxdigit(*ptr)) if (av_isxdigit(*ptr))