comparison tools/libgfx.c @ 2257:c7495fcaffa9

Check image dimensions when loading PPM/PNG/PCX/IFF images.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 16 Jun 2019 22:33:05 +0300
parents a8b4e9c9f337
children 75216bf67fd2
comparison
equal deleted inserted replaced
2256:fe974f670d1d 2257:c7495fcaffa9
1003 } 1003 }
1004 1004
1005 dmMsg(2, "PPM: %d x %d, type=%d\n", 1005 dmMsg(2, "PPM: %d x %d, type=%d\n",
1006 width, height, itype); 1006 width, height, itype);
1007 1007
1008 // Check image dimensions
1009 if (width == 0 || height == 0)
1010 {
1011 return dmError(DMERR_INVALID_DATA,
1012 "PPM: Invalid image dimensions.\n");
1013 }
1014
1008 if ((*pimg = img = dmImageAlloc(width, height, itype, -1)) == NULL) 1015 if ((*pimg = img = dmImageAlloc(width, height, itype, -1)) == NULL)
1009 { 1016 {
1010 res = dmError(DMERR_MALLOC, 1017 res = dmError(DMERR_MALLOC,
1011 "PPM: Could not allocate image data.\n"); 1018 "PPM: Could not allocate image data.\n");
1012 goto error; 1019 goto error;
1256 png_read_info(png_ptr, info_ptr); 1263 png_read_info(png_ptr, info_ptr);
1257 1264
1258 png_get_IHDR(png_ptr, info_ptr, &width, &height, 1265 png_get_IHDR(png_ptr, info_ptr, &width, &height,
1259 &bit_depth, &color_type, NULL, NULL, NULL); 1266 &bit_depth, &color_type, NULL, NULL, NULL);
1260 1267
1268 dmMsg(2, "PNG: %d x %d, bit_depth=%d, color_type=%d\n",
1269 width, height, bit_depth, color_type);
1270
1261 if (width < 1 || height < 1) 1271 if (width < 1 || height < 1)
1262 { 1272 {
1263 res = dmError(DMERR_INVALID_DATA, 1273 res = dmError(DMERR_INVALID_DATA,
1264 "PNG: Invalid width or height (%d x %d)\n", 1274 "PNG: Invalid image dimensions.\n");
1265 width, height);
1266 goto error; 1275 goto error;
1267 } 1276 }
1268 1277
1269 switch (color_type) 1278 switch (color_type)
1270 { 1279 {
1784 1793
1785 dmMsg(2, 1794 dmMsg(2,
1786 "PCX: nplanes=%d, bpp=%d, bpl=%d, isPaletted=%s\n", 1795 "PCX: nplanes=%d, bpp=%d, bpl=%d, isPaletted=%s\n",
1787 hdr.nplanes, hdr.bitsPerPlane, hdr.bpl, isPaletted ? "yes" : "no"); 1796 hdr.nplanes, hdr.bitsPerPlane, hdr.bpl, isPaletted ? "yes" : "no");
1788 1797
1798 // Check image dimensions
1799 if (hdr.xmin > hdr.xmax || hdr.ymin > hdr.ymax)
1800 {
1801 res = dmError(DMERR_INVALID_DATA,
1802 "PCX: Invalid image dimensions.\n");
1803 goto error;
1804 }
1805
1789 if (hdr.nplanes < 1 || hdr.nplanes > 8) 1806 if (hdr.nplanes < 1 || hdr.nplanes > 8)
1790 { 1807 {
1791 res = dmError(DMERR_NOT_SUPPORTED, 1808 res = dmError(DMERR_NOT_SUPPORTED,
1792 "PCX: Unsupported number of bitplanes %d.\n", 1809 "PCX: Unsupported number of bitplanes %d.\n",
1793 hdr.nplanes); 1810 hdr.nplanes);
2449 "IFF: %s chunk before BMHD?\n", chunk.idStr); 2466 "IFF: %s chunk before BMHD?\n", chunk.idStr);
2450 } 2467 }
2451 2468
2452 dmMsg(2, "IFF: %s chunk size %d bytes\n", chunk.idStr, chunk.size); 2469 dmMsg(2, "IFF: %s chunk size %d bytes\n", chunk.idStr, chunk.size);
2453 2470
2471 // Check image dimensions
2472 if (iff.bmhd.w == 0 || iff.bmhd.h == 0)
2473 {
2474 return dmError(DMERR_INVALID_DATA,
2475 "IFF: Invalid image dimensions.\n");
2476 }
2477
2454 // Allocate image 2478 // Allocate image
2455 if ((*pimg = dmImageAlloc(iff.bmhd.w, iff.bmhd.h, 2479 if ((*pimg = dmImageAlloc(iff.bmhd.w, iff.bmhd.h,
2456 iff.bmhd.nplanes <= 8 ? DM_PIXFMT_PALETTE : DM_PIXFMT_RGBA, 2480 iff.bmhd.nplanes <= 8 ? DM_PIXFMT_PALETTE : DM_PIXFMT_RGBA,
2457 // XXX TODO? When/if we ever handle < 8bit indexed correctly, we can use the actual bpp 2481 // XXX TODO? When/if we ever handle < 8bit indexed correctly, we can use the actual bpp
2458 //iff->bmhd.nplanes <= 8 ? iff->bmhd.nplanes : -1 2482 //iff->bmhd.nplanes <= 8 ? iff->bmhd.nplanes : -1