History

lavc/pngdec: use a separate bytestream reader for each chunk

This makes sure that reading a truncated chunk will never overflow into the following chunk. It also allows to remove many repeated lines skipping over the trailing crc checksum.

Anton Khirnov committed 5 years ago

Commit 19e81031 parent ae08eec1 file · +72 −94Browse files

Showing the changes to libavcodec (1 of 1 file). Show all

Unified · Split

libavcodec/pngdec.c+72−94
@@ -421,13 +421,12 @@ the_end:; · 420 unmodified lines
421421 }
422422 }
423423
424−static int png_decode_idat(PNGDecContext *s, int length,
424+static int png_decode_idat(PNGDecContext *s, GetByteContext *gb,
425425 uint8_t *dst, ptrdiff_t dst_stride)
426426 {
427427 int ret;
428− s->zstream.avail_in = FFMIN(length, bytestream2_get_bytes_left(&s->gb));
429− s->zstream.next_in = s->gb.buffer;
430− bytestream2_skip(&s->gb, length);
428+ s->zstream.avail_in = bytestream2_get_bytes_left(gb);
429+ s->zstream.next_in = gb->buffer;
431430
432431 /* decode one line if possible */
433432 while (s->zstream.avail_in > 0) {
@@ -520,11 +519,11 @@ static uint8_t *iso88591_to_utf8(const uint8_t *in, size_t size_in) · 86 unmodified lines
520519 return out;
521520 }
522521
523−static int decode_text_chunk(PNGDecContext *s, uint32_t length, int compressed)
522+static int decode_text_chunk(PNGDecContext *s, GetByteContext *gb, int compressed)
524523 {
525524 int ret, method;
526− const uint8_t *data = s->gb.buffer;
527− const uint8_t *data_end = data + length;
525+ const uint8_t *data = gb->buffer;
526+ const uint8_t *data_end = gb->buffer_end;
528527 const uint8_t *keyword = data;
529528 const uint8_t *keyword_end = memchr(keyword, 0, data_end - keyword);
530529 uint8_t *kw_utf8 = NULL, *text, *txt_utf8 = NULL;
@@ -568,9 +567,9 @@ static int decode_text_chunk(PNGDecContext *s, uint32_t length, int compressed) · 37 unmodified lines
568567 }
569568
570569 static int decode_ihdr_chunk(AVCodecContext *avctx, PNGDecContext *s,
571− uint32_t length)
570+ GetByteContext *gb)
572571 {
573− if (length != 13)
572+ if (bytestream2_get_bytes_left(gb) != 13)
574573 return AVERROR_INVALIDDATA;
575574
576575 if (s->pic_state & PNG_IDAT) {
@@ -583,28 +582,27 @@ static int decode_ihdr_chunk(AVCodecContext *avctx, PNGDecContext *s, · 6 unmodified lines
583582 return AVERROR_INVALIDDATA;
584583 }
585584
586− s->width = s->cur_w = bytestream2_get_be32(&s->gb);
587− s->height = s->cur_h = bytestream2_get_be32(&s->gb);
585+ s->width = s->cur_w = bytestream2_get_be32(gb);
586+ s->height = s->cur_h = bytestream2_get_be32(gb);
588587 if (av_image_check_size(s->width, s->height, 0, avctx)) {
589588 s->cur_w = s->cur_h = s->width = s->height = 0;
590589 av_log(avctx, AV_LOG_ERROR, "Invalid image size\n");
591590 return AVERROR_INVALIDDATA;
592591 }
593− s->bit_depth = bytestream2_get_byte(&s->gb);
592+ s->bit_depth = bytestream2_get_byte(gb);
594593 if (s->bit_depth != 1 && s->bit_depth != 2 && s->bit_depth != 4 &&
595594 s->bit_depth != 8 && s->bit_depth != 16) {
596595 av_log(avctx, AV_LOG_ERROR, "Invalid bit depth\n");
597596 goto error;
598597 }
599− s->color_type = bytestream2_get_byte(&s->gb);
600− s->compression_type = bytestream2_get_byte(&s->gb);
598+ s->color_type = bytestream2_get_byte(gb);
599+ s->compression_type = bytestream2_get_byte(gb);
601600 if (s->compression_type) {
602601 av_log(avctx, AV_LOG_ERROR, "Invalid compression method %d\n", s->compression_type);
603602 goto error;
604603 }
605− s->filter_type = bytestream2_get_byte(&s->gb);
606− s->interlace_type = bytestream2_get_byte(&s->gb);
607− bytestream2_skip(&s->gb, 4); /* crc */
604+ s->filter_type = bytestream2_get_byte(gb);
605+ s->interlace_type = bytestream2_get_byte(gb);
608606 s->hdr_state |= PNG_IHDR;
609607 if (avctx->debug & FF_DEBUG_PICT_INFO)
610608 av_log(avctx, AV_LOG_DEBUG, "width=%d height=%d depth=%d color_type=%d "
@@ -619,24 +617,24 @@ error: · 8 unmodified lines
619617 return AVERROR_INVALIDDATA;
620618 }
621619
622−static int decode_phys_chunk(AVCodecContext *avctx, PNGDecContext *s)
620+static int decode_phys_chunk(AVCodecContext *avctx, PNGDecContext *s,
621+ GetByteContext *gb)
623622 {
624623 if (s->pic_state & PNG_IDAT) {
625624 av_log(avctx, AV_LOG_ERROR, "pHYs after IDAT\n");
626625 return AVERROR_INVALIDDATA;
627626 }
628− avctx->sample_aspect_ratio.num = bytestream2_get_be32(&s->gb);
629− avctx->sample_aspect_ratio.den = bytestream2_get_be32(&s->gb);
627+ avctx->sample_aspect_ratio.num = bytestream2_get_be32(gb);
628+ avctx->sample_aspect_ratio.den = bytestream2_get_be32(gb);
630629 if (avctx->sample_aspect_ratio.num < 0 || avctx->sample_aspect_ratio.den < 0)
631630 avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
632− bytestream2_skip(&s->gb, 1); /* unit specifier */
633− bytestream2_skip(&s->gb, 4); /* crc */
631+ bytestream2_skip(gb, 1); /* unit specifier */
634632
635633 return 0;
636634 }
637635
638636 static int decode_idat_chunk(AVCodecContext *avctx, PNGDecContext *s,
639− uint32_t length, AVFrame *p)
637+ GetByteContext *gb, AVFrame *p)
640638 {
641639 int ret;
642640 size_t byte_depth = s->bit_depth > 8 ? 2 : 1;
@@ -773,7 +771,7 @@ static int decode_idat_chunk(AVCodecContext *avctx, PNGDecContext *s, · 130 unmodified lines
773771 if (s->has_trns && s->color_type != PNG_COLOR_TYPE_PALETTE)
774772 s->bpp -= byte_depth;
775773
776− ret = png_decode_idat(s, length, p->data[0], p->linesize[0]);
774+ ret = png_decode_idat(s, gb, p->data[0], p->linesize[0]);
777775
778776 if (s->has_trns && s->color_type != PNG_COLOR_TYPE_PALETTE)
779777 s->bpp += byte_depth;
@@ -781,14 +779,13 @@ static int decode_idat_chunk(AVCodecContext *avctx, PNGDecContext *s, · 1 unmodified line
781779 if (ret < 0)
782780 return ret;
783781
784− bytestream2_skip(&s->gb, 4); /* crc */
785−
786782 return 0;
787783 }
788784
789785 static int decode_plte_chunk(AVCodecContext *avctx, PNGDecContext *s,
790− uint32_t length)
786+ GetByteContext *gb)
791787 {
788+ int length = bytestream2_get_bytes_left(gb);
792789 int n, i, r, g, b;
793790
794791 if ((length % 3) != 0 || length > 256 * 3)
@@ -796,22 +793,22 @@ static int decode_plte_chunk(AVCodecContext *avctx, PNGDecContext *s, · 1 unmodified line
796793 /* read the palette */
797794 n = length / 3;
798795 for (i = 0; i < n; i++) {
799− r = bytestream2_get_byte(&s->gb);
800− g = bytestream2_get_byte(&s->gb);
801− b = bytestream2_get_byte(&s->gb);
796+ r = bytestream2_get_byte(gb);
797+ g = bytestream2_get_byte(gb);
798+ b = bytestream2_get_byte(gb);
802799 s->palette[i] = (0xFFU << 24) | (r << 16) | (g << 8) | b;
803800 }
804801 for (; i < 256; i++)
805802 s->palette[i] = (0xFFU << 24);
806803 s->hdr_state |= PNG_PLTE;
807− bytestream2_skip(&s->gb, 4); /* crc */
808804
809805 return 0;
810806 }
811807
812808 static int decode_trns_chunk(AVCodecContext *avctx, PNGDecContext *s,
813− uint32_t length)
809+ GetByteContext *gb)
814810 {
811+ int length = bytestream2_get_bytes_left(gb);
815812 int v, i;
816813
817814 if (!(s->hdr_state & PNG_IHDR)) {
@@ -829,7 +826,7 @@ static int decode_trns_chunk(AVCodecContext *avctx, PNGDecContext *s, · 11 unmodified lines
829826 return AVERROR_INVALIDDATA;
830827
831828 for (i = 0; i < length; i++) {
832− unsigned v = bytestream2_get_byte(&s->gb);
829+ unsigned v = bytestream2_get_byte(gb);
833830 s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
834831 }
835832 } else if (s->color_type == PNG_COLOR_TYPE_GRAY || s->color_type == PNG_COLOR_TYPE_RGB) {
@@ -840,7 +837,7 @@ static int decode_trns_chunk(AVCodecContext *avctx, PNGDecContext *s, · 4 unmodified lines
840837
841838 for (i = 0; i < length / 2; i++) {
842839 /* only use the least significant bits */
843− v = av_mod_uintp2(bytestream2_get_be16(&s->gb), s->bit_depth);
840+ v = av_mod_uintp2(bytestream2_get_be16(gb), s->bit_depth);
844841
845842 if (s->bit_depth > 8)
846843 AV_WB16(&s->transparent_color_be[2 * i], v);
@@ -851,35 +848,30 @@ static int decode_trns_chunk(AVCodecContext *avctx, PNGDecContext *s, · 4 unmodified lines
851848 return AVERROR_INVALIDDATA;
852849 }
853850
854− bytestream2_skip(&s->gb, 4); /* crc */
855851 s->has_trns = 1;
856852
857853 return 0;
858854 }
859855
860−static int decode_iccp_chunk(PNGDecContext *s, int length, AVFrame *f)
856+static int decode_iccp_chunk(PNGDecContext *s, GetByteContext *gb, AVFrame *f)
861857 {
862858 int ret, cnt = 0;
863859 AVBPrint bp;
864860
865− while ((s->iccp_name[cnt++] = bytestream2_get_byte(&s->gb)) && cnt < 81);
861+ while ((s->iccp_name[cnt++] = bytestream2_get_byte(gb)) && cnt < 81);
866862 if (cnt > 80) {
867863 av_log(s->avctx, AV_LOG_ERROR, "iCCP with invalid name!\n");
868864 ret = AVERROR_INVALIDDATA;
869865 goto fail;
870866 }
871867
872− length = FFMAX(length - cnt, 0);
873−
874− if (bytestream2_get_byte(&s->gb) != 0) {
868+ if (bytestream2_get_byte(gb) != 0) {
875869 av_log(s->avctx, AV_LOG_ERROR, "iCCP with invalid compression!\n");
876870 ret = AVERROR_INVALIDDATA;
877871 goto fail;
878872 }
879873
880− length = FFMAX(length - 1, 0);
881−
882− if ((ret = decode_zbuf(&bp, s->gb.buffer, s->gb.buffer + length)) < 0)
874+ if ((ret = decode_zbuf(&bp, gb->buffer, gb->buffer_end)) < 0)
883875 return ret;
884876
885877 av_freep(&s->iccp_data);
@@ -888,9 +880,6 @@ static int decode_iccp_chunk(PNGDecContext *s, int length, AVFrame *f) · 2 unmodified lines
888880 return ret;
889881 s->iccp_data_len = bp.len;
890882
891− /* ICC compressed data and CRC */
892− bytestream2_skip(&s->gb, length + 4);
893−
894883 return 0;
895884 fail:
896885 s->iccp_name[0] = 0;
@@ -971,12 +960,12 @@ static void handle_small_bpp(PNGDecContext *s, AVFrame *p) · 74 unmodified lines
971960 }
972961
973962 static int decode_fctl_chunk(AVCodecContext *avctx, PNGDecContext *s,
974− uint32_t length)
963+ GetByteContext *gb)
975964 {
976965 uint32_t sequence_number;
977966 int cur_w, cur_h, x_offset, y_offset, dispose_op, blend_op;
978967
979− if (length != 26)
968+ if (bytestream2_get_bytes_left(gb) != 26)
980969 return AVERROR_INVALIDDATA;
981970
982971 if (!(s->hdr_state & PNG_IHDR)) {
@@ -995,15 +984,14 @@ static int decode_fctl_chunk(AVCodecContext *avctx, PNGDecContext *s, · 12 unmodified lines
995984 s->last_y_offset = s->y_offset;
996985 s->last_dispose_op = s->dispose_op;
997986
998− sequence_number = bytestream2_get_be32(&s->gb);
999− cur_w = bytestream2_get_be32(&s->gb);
1000− cur_h = bytestream2_get_be32(&s->gb);
1001− x_offset = bytestream2_get_be32(&s->gb);
1002− y_offset = bytestream2_get_be32(&s->gb);
1003− bytestream2_skip(&s->gb, 4); /* delay_num (2), delay_den (2) */
1004− dispose_op = bytestream2_get_byte(&s->gb);
1005− blend_op = bytestream2_get_byte(&s->gb);
1006− bytestream2_skip(&s->gb, 4); /* crc */
987+ sequence_number = bytestream2_get_be32(gb);
988+ cur_w = bytestream2_get_be32(gb);
989+ cur_h = bytestream2_get_be32(gb);
990+ x_offset = bytestream2_get_be32(gb);
991+ y_offset = bytestream2_get_be32(gb);
992+ bytestream2_skip(gb, 4); /* delay_num (2), delay_den (2) */
993+ dispose_op = bytestream2_get_byte(gb);
994+ blend_op = bytestream2_get_byte(gb);
1007995
1008996 if (sequence_number == 0 &&
1009997 (cur_w != s->width ||
@@ -1194,6 +1182,8 @@ static int decode_frame_common(AVCodecContext *avctx, PNGDecContext *s, · 184 unmodified lines
11941182 int i, ret;
11951183
11961184 for (;;) {
1185+ GetByteContext gb_chunk;
1186+
11971187 length = bytestream2_get_bytes_left(&s->gb);
11981188 if (length <= 0) {
11991189
@@ -1233,8 +1223,7 @@ static int decode_frame_common(AVCodecContext *avctx, PNGDecContext *s, · 33 unmodified lines
12331223 goto fail;
12341224 }
12351225 av_log(avctx, AV_LOG_ERROR, ", skipping\n");
1236− bytestream2_skip(&s->gb, 4); /* tag */
1237− goto skip_tag;
1226+ bytestream2_skip(&s->gb, length + 8); /* tag */
12381227 }
12391228 }
12401229 tag = bytestream2_get_le32(&s->gb);
@@ -1242,6 +1231,9 @@ static int decode_frame_common(AVCodecContext *avctx, PNGDecContext *s, · 1 unmodified line
12421231 av_log(avctx, AV_LOG_DEBUG, "png: tag=%s length=%u\n",
12431232 av_fourcc2str(tag), length);
12441233
1234+ bytestream2_init(&gb_chunk, s->gb.buffer, length);
1235+ bytestream2_skip(&s->gb, length + 4);
1236+
12451237 if (avctx->codec_id == AV_CODEC_ID_PNG &&
12461238 avctx->skip_frame == AVDISCARD_ALL) {
12471239 switch(tag) {
@@ -1252,62 +1244,57 @@ static int decode_frame_common(AVCodecContext *avctx, PNGDecContext *s, · 4 unmodified lines
12521244 case MKTAG('t', 'R', 'N', 'S'):
12531245 break;
12541246 default:
1255− goto skip_tag;
1247+ continue;
12561248 }
12571249 }
12581250
12591251 switch (tag) {
12601252 case MKTAG('I', 'H', 'D', 'R'):
1261− if ((ret = decode_ihdr_chunk(avctx, s, length)) < 0)
1253+ if ((ret = decode_ihdr_chunk(avctx, s, &gb_chunk)) < 0)
12621254 goto fail;
12631255 break;
12641256 case MKTAG('p', 'H', 'Y', 's'):
1265− if ((ret = decode_phys_chunk(avctx, s)) < 0)
1257+ if ((ret = decode_phys_chunk(avctx, s, &gb_chunk)) < 0)
12661258 goto fail;
12671259 break;
12681260 case MKTAG('f', 'c', 'T', 'L'):
12691261 if (!CONFIG_APNG_DECODER || avctx->codec_id != AV_CODEC_ID_APNG)
1270− goto skip_tag;
1271− if ((ret = decode_fctl_chunk(avctx, s, length)) < 0)
1262+ continue;
1263+ if ((ret = decode_fctl_chunk(avctx, s, &gb_chunk)) < 0)
12721264 goto fail;
12731265 decode_next_dat = 1;
12741266 break;
12751267 case MKTAG('f', 'd', 'A', 'T'):
12761268 if (!CONFIG_APNG_DECODER || avctx->codec_id != AV_CODEC_ID_APNG)
1277− goto skip_tag;
1278− if (!decode_next_dat || length < 4) {
1269+ continue;
1270+ if (!decode_next_dat || bytestream2_get_bytes_left(&gb_chunk) < 4) {
12791271 ret = AVERROR_INVALIDDATA;
12801272 goto fail;
12811273 }
1282− bytestream2_get_be32(&s->gb);
1283− length -= 4;
1274+ bytestream2_get_be32(&gb_chunk);
12841275 /* fallthrough */
12851276 case MKTAG('I', 'D', 'A', 'T'):
12861277 if (CONFIG_APNG_DECODER && avctx->codec_id == AV_CODEC_ID_APNG && !decode_next_dat)
1287− goto skip_tag;
1288− if ((ret = decode_idat_chunk(avctx, s, length, p)) < 0)
1278+ continue;
1279+ if ((ret = decode_idat_chunk(avctx, s, &gb_chunk, p)) < 0)
12891280 goto fail;
12901281 break;
12911282 case MKTAG('P', 'L', 'T', 'E'):
1292− if (decode_plte_chunk(avctx, s, length) < 0)
1293− goto skip_tag;
1283+ decode_plte_chunk(avctx, s, &gb_chunk);
12941284 break;
12951285 case MKTAG('t', 'R', 'N', 'S'):
1296− if (decode_trns_chunk(avctx, s, length) < 0)
1297− goto skip_tag;
1286+ decode_trns_chunk(avctx, s, &gb_chunk);
12981287 break;
12991288 case MKTAG('t', 'E', 'X', 't'):
1300− if (decode_text_chunk(s, length, 0) < 0)
1289+ if (decode_text_chunk(s, &gb_chunk, 0) < 0)
13011290 av_log(avctx, AV_LOG_WARNING, "Broken tEXt chunk\n");
1302− bytestream2_skip(&s->gb, length + 4);
13031291 break;
13041292 case MKTAG('z', 'T', 'X', 't'):
1305− if (decode_text_chunk(s, length, 1) < 0)
1293+ if (decode_text_chunk(s, &gb_chunk, 1) < 0)
13061294 av_log(avctx, AV_LOG_WARNING, "Broken zTXt chunk\n");
1307− bytestream2_skip(&s->gb, length + 4);
13081295 break;
13091296 case MKTAG('s', 'T', 'E', 'R'): {
1310− int mode = bytestream2_get_byte(&s->gb);
1297+ int mode = bytestream2_get_byte(&gb_chunk);
13111298
13121299 if (mode == 0 || mode == 1) {
13131300 s->stereo_mode = mode;
@@ -1315,33 +1302,31 @@ static int decode_frame_common(AVCodecContext *avctx, PNGDecContext *s, · 1 unmodified line
13151302 av_log(avctx, AV_LOG_WARNING,
13161303 "Unknown value in sTER chunk (%d)\n", mode);
13171304 }
1318− bytestream2_skip(&s->gb, 4); /* crc */
13191305 break;
13201306 }
13211307 case MKTAG('i', 'C', 'C', 'P'): {
1322− if ((ret = decode_iccp_chunk(s, length, p)) < 0)
1308+ if ((ret = decode_iccp_chunk(s, &gb_chunk, p)) < 0)
13231309 goto fail;
13241310 break;
13251311 }
13261312 case MKTAG('c', 'H', 'R', 'M'): {
13271313 s->have_chrm = 1;
13281314
1329− s->white_point[0] = bytestream2_get_be32(&s->gb);
1330− s->white_point[1] = bytestream2_get_be32(&s->gb);
1315+ s->white_point[0] = bytestream2_get_be32(&gb_chunk);
1316+ s->white_point[1] = bytestream2_get_be32(&gb_chunk);
13311317
13321318 /* RGB Primaries */
13331319 for (i = 0; i < 3; i++) {
1334− s->display_primaries[i][0] = bytestream2_get_be32(&s->gb);
1335− s->display_primaries[i][1] = bytestream2_get_be32(&s->gb);
1320+ s->display_primaries[i][0] = bytestream2_get_be32(&gb_chunk);
1321+ s->display_primaries[i][1] = bytestream2_get_be32(&gb_chunk);
13361322 }
13371323
1338− bytestream2_skip(&s->gb, 4); /* crc */
13391324 break;
13401325 }
13411326 case MKTAG('g', 'A', 'M', 'A'): {
13421327 AVBPrint bp;
13431328 char *gamma_str;
1344− int num = bytestream2_get_be32(&s->gb);
1329+ int num = bytestream2_get_be32(&gb_chunk);
13451330
13461331 av_bprint_init(&bp, 0, AV_BPRINT_SIZE_UNLIMITED);
13471332 av_bprintf(&bp, "%i/%i", num, 100000);
@@ -1351,7 +1336,6 @@ static int decode_frame_common(AVCodecContext *avctx, PNGDecContext *s, · 3 unmodified lines
13511336
13521337 av_dict_set(&s->frame_metadata, "gamma", gamma_str, AV_DICT_DONT_STRDUP_VAL);
13531338
1354− bytestream2_skip(&s->gb, 4); /* crc */
13551339 break;
13561340 }
13571341 case MKTAG('I', 'E', 'N', 'D'):
@@ -1361,13 +1345,7 @@ static int decode_frame_common(AVCodecContext *avctx, PNGDecContext *s, · 3 unmodified lines
13611345 ret = AVERROR_INVALIDDATA;
13621346 goto fail;
13631347 }
1364− bytestream2_skip(&s->gb, 4); /* crc */
13651348 goto exit_loop;
1366− default:
1367− /* skip tag */
1368−skip_tag:
1369− bytestream2_skip(&s->gb, length + 4);
1370− break;
13711349 }
13721350 }
13731351 exit_loop:

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