History

pngdec: add ability to check chunk CRC

By default now, if AV_EF_CRCCHECK or AV_EF_IGNORE_ERR are enabled the decoder will skip the chunk and carry on with the next one. This should make the decoder able to decode more corrupt files because the functions which decode individual chunks will very likely error out if fed invalid data and stop the decoding of the entire image.

Lynne committed 7 years ago

Commit b8d9bc21 parent e69f4071 file · +17 −0Browse files

Unified · Split

libavcodec/pngdec.c+17−0
@@ -23,6 +23,7 @@ · 22 unmodified lines
2323
2424 #include "libavutil/avassert.h"
2525 #include "libavutil/bprint.h"
26+#include "libavutil/crc.h"
2627 #include "libavutil/imgutils.h"
2728 #include "libavutil/intreadwrite.h"
2829 #include "libavutil/stereo3d.h"
@@ -1179,6 +1180,7 @@ static int handle_p_frame_apng(AVCodecContext *avctx, PNGDecContext *s, · 1150 unmodified lines
11791180 static int decode_frame_common(AVCodecContext *avctx, PNGDecContext *s,
11801181 AVFrame *p, AVPacket *avpkt)
11811182 {
1183+ const AVCRC *crc_tab = av_crc_get_table(AV_CRC_32_IEEE_LE);
11821184 AVDictionary **metadatap = NULL;
11831185 uint32_t tag, length;
11841186 int decode_next_dat = 0;
@@ -1213,6 +1215,21 @@ static int decode_frame_common(AVCodecContext *avctx, PNGDecContext *s, · 28 unmodified lines
12131215 ret = AVERROR_INVALIDDATA;
12141216 goto fail;
12151217 }
1218+ if (avctx->err_recognition & (AV_EF_CRCCHECK | AV_EF_IGNORE_ERR)) {
1219+ uint32_t crc_sig = AV_RB32(s->gb.buffer + length + 4);
1220+ uint32_t crc_cal = ~av_crc(crc_tab, UINT32_MAX, s->gb.buffer, length + 4);
1221+ if (crc_sig ^ crc_cal) {
1222+ av_log(avctx, AV_LOG_ERROR, "CRC mismatch in chunk");
1223+ if (avctx->err_recognition & AV_EF_EXPLODE) {
1224+ av_log(avctx, AV_LOG_ERROR, ", quitting\n");
1225+ ret = AVERROR_INVALIDDATA;
1226+ goto fail;
1227+ }
1228+ av_log(avctx, AV_LOG_ERROR, ", skipping\n");
1229+ bytestream2_skip(&s->gb, 4); /* tag */
1230+ goto skip_tag;
1231+ }
1232+ }
12161233 tag = bytestream2_get_le32(&s->gb);
12171234 if (avctx->debug & FF_DEBUG_STARTCODE)
12181235 av_log(avctx, AV_LOG_DEBUG, "png: tag=%s length=%u\n",

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t