1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428
|
/*
* Copyright 1995-2007 by Alexander Lehmann <lehmann@usa.net>,
* Andreas Dilger <adilger@enel.ucalgary.ca>,
* Glenn Randers-Pehrson <randeg@alum.rpi.edu>,
* Greg Roelofs <newt@pobox.com>,
* John Bowler <jbowler@acm.org>,
* Tom Lane <tgl@sss.pgh.pa.us>
* Copyright (C) 2015 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
* Copyright (C) 2011 Sourcefire, Inc.
* Maintainer: Tomasz Kojm <tkojm@clamav.net>
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for any purpose and without fee is hereby granted, provided
* that the above copyright notice appear in all copies and that both that
* copyright notice and this permission notice appear in supporting
* documentation. This software is provided "as is" without express or
* implied warranty.
*
*/
#if HAVE_CONFIG_H
#include "clamav-config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <zlib.h>
#include "clamav.h"
#include "others.h"
#include "png.h"
typedef unsigned char uch;
typedef unsigned short ush;
typedef unsigned long ulg;
#define BS 32000 /* size of read block for CRC calculation (and zlib) */
/* Mark's macros to extract big-endian short and long ints: */
#define SH(p) ((ush)(uch)((p)[1]) | ((ush)(uch)((p)[0]) << 8))
#define LG(p) ((ulg)(SH((p)+2)) | ((ulg)(SH(p)) << 16))
#define isASCIIalpha(x) (ascii_alpha_table[x] & 0x1)
#define ANCILLARY(chunkID) ((chunkID)[0] & 0x20)
#define PRIVATE(chunkID) ((chunkID)[1] & 0x20)
#define RESERVED(chunkID) ((chunkID)[2] & 0x20)
#define SAFECOPY(chunkID) ((chunkID)[3] & 0x20)
#define CRITICAL(chunkID) (!ANCILLARY(chunkID))
#define PUBLIC(chunkID) (!PRIVATE(chunkID))
/* GRR FIXME: could merge all three of these into single table (bit fields) */
/* GRR 20061203: for "isalpha()" that works even on EBCDIC machines */
static const uch ascii_alpha_table[256] = {
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
};
/* GRR 20070707: list of forbidden characters in various keywords */
static const uch latin1_keyword_forbidden[256] = {
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
};
/* GRR 20070707: list of discouraged (control) characters in tEXt/zTXt text */
static const uch latin1_text_discouraged[256] = {
1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
};
/* PNG stuff */
static const char *png_type[] = { /* IHDR, tRNS, BASI, summary */
"grayscale",
"INVALID",
"RGB",
"palette",
"grayscale+alpha",
"INVALID",
"RGB+alpha"
};
#define CRCCOMPL(c) c
#define CRCINIT (0)
#define update_crc crc32
static ulg getlong(fmap_t *map, unsigned int *offset, const char *where)
{
ulg res = 0;
int j;
for (j = 0; j < 4; ++j) {
unsigned char c;
if(fmap_readn(map, &c, *offset, sizeof(c)) != sizeof(c)) {
cli_dbgmsg("PNG: EOF(?) while reading %s\n", where);
return 0;
}
(*offset)++;
res <<= 8;
res |= c & 0xff;
}
return res;
}
static int keywordlen(uch *buf, int maxsize)
{
int j = 0;
while (j < maxsize && buf[j])
++j;
return j;
}
static const char *getmonth(int m)
{
static const char *month[] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
return (m < 1 || m > 12)? "INVALID" : month[m-1];
}
/* GRR 20061203: now EBCDIC-safe */
static int check_chunk_name(char *chunk_name)
{
if (isASCIIalpha((int)chunk_name[0]) && isASCIIalpha((int)chunk_name[1]) &&
isASCIIalpha((int)chunk_name[2]) && isASCIIalpha((int)chunk_name[3]))
return 0;
cli_dbgmsg("PNG: invalid chunk name\n");
return CL_EPARSE; /* usually means we've "jumped the tracks": bail! */
}
/* GRR 20050724 */
/* caller must do return CL_EPARSE based on return value (0 == OK) */
/* keyword_name is "keyword" for most chunks, but it can instead be "name" or
* "identifier" or whatever makes sense for the chunk in question */
static int check_keyword(uch *buffer, int maxsize, int *pKeylen)
{
int j, prev_space = 0;
int keylen = keywordlen(buffer, maxsize);
if (pKeylen)
*pKeylen = keylen;
if (keylen == 0) {
cli_dbgmsg("PNG: zero length keyword\n");
return 1;
}
if (keylen > 79) {
cli_dbgmsg("PNG: keyword is longer than 79 characters\n");
return 2;
}
if (buffer[0] == ' ') {
cli_dbgmsg("PNG: keyword has leading space(s)\n");
return 3;
}
if (buffer[keylen - 1] == ' ') {
cli_dbgmsg("PNG: keyword has trailing space(s)\n");
return 4;
}
for (j = 0; j < keylen; ++j) {
if (buffer[j] == ' ') {
if (prev_space) {
cli_dbgmsg("PNG: keyword has consecutive spaces\n");
return 5;
}
prev_space = 1;
} else {
prev_space = 0;
}
}
for (j = 0; j < keylen; ++j) {
if (latin1_keyword_forbidden[buffer[j]]) { /* [0,31] || [127,160] */
cli_dbgmsg("PNG: keyword has control character(s)\n");
return 6;
}
}
return 0;
}
/* GRR 20070707 */
/* caller must do return CL_EPARSE based on return value (0 == OK) */
static int check_text(uch *buffer, int maxsize)
{
int j;
for (j = 0; j < maxsize; ++j) {
if (buffer[j] == 0) {
cli_dbgmsg("PNG: text contains NULL character(s)\n");
return 1;
} else if (latin1_text_discouraged[buffer[j]]) {
cli_dbgmsg("PNG: text has control character(s)\n");
return 1;
}
}
return 0;
}
/* GRR 20061203 (used only for sCAL) */
static int check_ascii_float(uch *buffer, int len)
{
uch *qq = buffer, *bufEnd = buffer + len;
int have_sign = 0, have_integer = 0, have_dot = 0, have_fraction = 0;
int have_E = 0, have_Esign = 0, have_exponent = 0, in_digits = 0;
int have_nonzero = 0;
int rc = 0;
for (qq = buffer; qq < bufEnd && !rc; ++qq) {
switch (*qq) {
case '+':
case '-':
if (qq == buffer) {
have_sign = 1;
in_digits = 0;
} else if (have_E && !have_Esign) {
have_Esign = 1;
in_digits = 0;
} else {
cli_dbgmsg("PNG: invalid sign character\n");
rc = 1;
}
break;
case '.':
if (!have_dot && !have_E) {
have_dot = 1;
in_digits = 0;
} else {
cli_dbgmsg("PNG: invalid decimal point\n");
rc = 2;
}
break;
case 'e':
case 'E':
if (have_integer || have_fraction) {
have_E = 1;
in_digits = 0;
} else {
cli_dbgmsg("PNG: invalid exponent before mantissa\n");
rc = 3;
}
break;
default:
if (*qq < '0' || *qq > '9') {
cli_dbgmsg("PNG: invalid character\n");
rc = 4;
} else if (in_digits) {
/* still in digits: do nothing except check for non-zero digits */
if (!have_exponent && *qq != '0')
have_nonzero = 1;
} else if (!have_integer && !have_dot) {
have_integer = 1;
in_digits = 1;
if (*qq != '0')
have_nonzero = 1;
} else if (have_dot && !have_fraction) {
have_fraction = 1;
in_digits = 1;
if (*qq != '0')
have_nonzero = 1;
} else if (have_E && !have_exponent) {
have_exponent = 1;
in_digits = 1;
} else {
/* is this case possible? */
cli_dbgmsg("PNG: invalid digits\n");
rc = 5;
}
break;
}
}
/* must have either integer part or fractional part; all else is optional */
if (rc == 0 && !have_integer && !have_fraction) {
cli_dbgmsg("PNG: missing mantissa\n");
rc = 6;
}
/* non-exponent part must be non-zero (=> must have seen a non-zero digit) */
if (rc == 0 && !have_nonzero) {
cli_dbgmsg("PNG: invalid zero value(s)\n");
rc = 7;
}
return rc;
}
int cli_parsepng(cli_ctx *ctx)
{
long sz;
uch magic[8];
char chunkid[5] = {'\0', '\0', '\0', '\0', '\0'};
int toread;
int c;
int have_IHDR = 0, have_IEND = 0;
int have_PLTE = 0;
int have_IDAT = 0, have_JDAT = 0, last_is_IDAT = 0, last_is_JDAT = 0;
int have_bKGD = 0, have_cHRM = 0, have_gAMA = 0, have_hIST = 0, have_iCCP = 0;
int have_oFFs = 0, have_pCAL = 0, have_pHYs = 0, have_sBIT = 0, have_sCAL = 0;
int have_sRGB = 0, have_sTER = 0, have_tIME = 0, have_tRNS = 0;
ulg zhead = 1; /* 0x10000 indicates both zlib header bytes read */
ulg crc, filecrc;
long num_chunks = 0L;
long w = 0L, h = 0L;
int bitdepth = 0, sampledepth = 0, lace = 0, nplte = 0;
unsigned int ityp = 1;
uch buffer[BS];
int first_idat = 1; /* flag: is this the first IDAT chunk? */
int zlib_error = 0; /* reset in IHDR section; used for IDAT */
int check_zlib = 1; /* validate zlib stream (just IDATs for now) */
unsigned zlib_windowbits = 15;
uch outbuf[BS];
z_stream zstrm;
unsigned int offset = 0;
fmap_t *map = *ctx->fmap;
cli_dbgmsg("in cli_parsepng()\n");
if(fmap_readn(map, magic, offset, 8) != 8)
return CL_SUCCESS; /* Ignore */
if(memcmp(magic, "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a", 8))
return CL_SUCCESS; /* Not a PNG file */
offset += 8;
/*-------------------- BEGINNING OF IMMENSE WHILE-LOOP --------------------*/
while(fmap_readn(map, &c, offset, sizeof(c)) == sizeof(c)) {
if (have_IEND) {
cli_dbgmsg("PNG: additional data after END chunk\n");
return CL_EPARSE;
}
sz = getlong(map, &offset, "chunk length");
if (sz < 0 || sz > 0x7fffffff) { /* FIXME: convert to ulg, lose "< 0" */
cli_dbgmsg("PNG: invalid chunk length (too large)\n");
return CL_EPARSE;
}
if(fmap_readn(map, chunkid, offset, 4) != 4) {
cli_dbgmsg("PNG: EOF while reading chunk type\n");
return CL_EPARSE;
}
offset += 4;
/* GRR: add 4-character EBCDIC conversion here (chunkid) */
chunkid[4] = '\0';
++num_chunks;
if (check_chunk_name(chunkid) != 0)
return CL_EPARSE;
if (!have_IHDR && strcmp(chunkid,"IHDR")!=0)
{
cli_dbgmsg("PNG: first chunk must be IHDR\n");
return CL_EPARSE;
}
crc = update_crc(CRCINIT, (uch *)chunkid, 4);
toread = (sz > BS)? BS:sz;
if(toread && fmap_readn(map, buffer, offset, toread) != toread) {
cli_dbgmsg("PNG: EOF while reading data\n");
return CL_EPARSE;
}
offset += toread;
crc = update_crc(crc, (uch *)buffer, toread);
/*------*
| IHDR |
*------*/
if (strcmp(chunkid, "IHDR") == 0) {
if (have_IHDR) {
cli_dbgmsg("PNG: multiple IHDR not allowed\n");
return CL_EPARSE;
} else if (sz != 13) {
cli_dbgmsg("PNG: invalid IHDR length\n");
return CL_EPARSE;
} else {
int compr, filt;
w = LG(buffer);
h = LG(buffer+4);
if (w <= 0 || h <= 0 || w > 2147483647 || h > 2147483647) {
cli_dbgmsg("PNG: invalid image dimensions\n");
return CL_EPARSE;
}
bitdepth = sampledepth = (uch)buffer[8];
ityp = (uch)buffer[9];
if (ityp == 1 || ityp == 5 || ityp > sizeof(png_type)/sizeof(char*)) {
cli_dbgmsg("PNG: invalid image type (%d)\n", ityp);
return CL_EPARSE;
}
switch (sampledepth) {
case 1:
case 2:
case 4:
if (ityp == 2 || ityp == 4 || ityp == 6) { /* RGB or GA or RGBA */
cli_dbgmsg("PNG: invalid sample depth (%d)\n", sampledepth);
return CL_EPARSE;
}
break;
case 8:
break;
case 16:
if (ityp == 3) { /* palette */
cli_dbgmsg("PNG: invalid sample depth (%d)\n", sampledepth);
return CL_EPARSE;
}
break;
default:
cli_dbgmsg("PNG: invalid sample depth (%d)\n", sampledepth);
return CL_EPARSE;
}
compr = (uch)buffer[10];
if (compr > 127) {
cli_dbgmsg("PNG: private (invalid?) compression method (%d)\n", compr);
return CL_EPARSE;
} else if (compr > 0) {
cli_dbgmsg("PNG: invalid compression method (%d)\n", compr);
return CL_EPARSE;
}
filt = (uch)buffer[11];
if (filt > 127) {
cli_dbgmsg("PNG: private (invalid?) filter method (%d)\n", filt);
return CL_EPARSE;
} else if (filt > 0)
{
cli_dbgmsg("PNG: invalid filter method (%d)\n", filt);
return CL_EPARSE;
}
lace = (uch)buffer[12];
if (lace > 127) {
cli_dbgmsg("PNG: private (invalid?) interlace method (%d)\n", lace);
return CL_EPARSE;
} else if (lace > 1) {
cli_dbgmsg("PNG: invalid interlace method (%d)\n", lace);
return CL_EPARSE;
}
switch (ityp) {
case 2:
bitdepth = sampledepth * 3; /* RGB */
break;
case 4:
bitdepth = sampledepth * 2; /* gray+alpha */
break;
case 6:
bitdepth = sampledepth * 4; /* RGBA */
break;
}
}
have_IHDR = 1;
last_is_IDAT = last_is_JDAT = 0;
first_idat = 1; /* flag: next IDAT will be the first in this subimage */
zlib_error = 0; /* flag: no zlib errors yet in this file */
/* GRR 20000304: data dump not yet compatible with interlaced images: */
/*================================================*
* PNG chunks (with the exception of IHDR, above) *
*================================================*/
/*------*
| PLTE |
*------*/
} else if (strcmp(chunkid, "PLTE") == 0) {
if (have_PLTE) {
cli_dbgmsg("PNG: multiple PLTE not allowed\n");
return CL_EPARSE;
} else if (ityp != 3 && ityp != 2 && ityp != 6) {
cli_dbgmsg("PNG: PLTE not allowed in %s image\n", png_type[ityp]);
return CL_EPARSE;
} else if (have_IDAT) {
cli_dbgmsg("PNG: PLTE must precede IDAT\n");
return CL_EPARSE;
} else if (have_bKGD) {
cli_dbgmsg("PNG: PLTE must precede bKGD\n");
return CL_EPARSE;
} else if (sz > 768 || sz % 3 != 0) {
cli_dbgmsg("PNG: invalid number of PLTE entries (%g)\n", (double)sz / 3);
return CL_EPARSE;
} else {
nplte = sz / 3;
if (((bitdepth == 1 && nplte > 2) ||
(bitdepth == 2 && nplte > 4) || (bitdepth == 4 && nplte > 16)))
{
cli_dbgmsg("PNG: invalid number of PLTE entries (%d) for %d-bit image\n", nplte, bitdepth);
return CL_EPARSE;
}
}
if (ityp == 1) /* for MNG and tRNS */
ityp = 3;
have_PLTE = 1;
last_is_IDAT = last_is_JDAT = 0;
} else if (strcmp(chunkid, "IDAT") == 0) {
/* GRR FIXME: need to check for consecutive IDATs within MNG segments */
if (have_IDAT && !last_is_IDAT) {
cli_dbgmsg("PNG: IDAT chunks must be consecutive\n");
return CL_EPARSE;
} else if (ityp == 3 && !have_PLTE) {
cli_dbgmsg("PNG: IDAT must follow PLTE in %s image\n", png_type[ityp]);
return CL_EPARSE;
}
/* We just want to check that we have read at least the minimum (10)
* IDAT bytes possible, but avoid any overflow for short ints. We
* must also take into account that 0-length IDAT chunks are legal.
*/
if (have_IDAT <= 0)
have_IDAT = (sz > 0)? sz : -1; /* -1 as marker for IDAT(s), no data */
else if (have_IDAT < 10)
have_IDAT += (sz > 10)? 10 : sz; /* FIXME? could cap at 10 always */
/* Dump the zlib header from the first two bytes. */
if (zhead < 0x10000 && sz > 0) {
zhead = (zhead << 8) + buffer[0];
if (sz > 1 && zhead < 0x10000)
zhead = (zhead << 8) + buffer[1];
if (zhead >= 0x10000) {
/* formerly print_zlibheader(zhead & 0xffff); */
/* See the code in zlib deflate.c that writes out the header when
s->status is INIT_STATE. In fact this code is based on the zlib
specification in RFC 1950 (ftp://ds.internic.net/rfc/rfc1950.txt),
with the implicit assumption that the zlib header *is* written (it
always should be inside a valid PNG file). The variable names are
taken, verbatim, from the RFC. */
unsigned int CINFO = (zhead & 0xf000) >> 12;
unsigned int CM = (zhead & 0xf00) >> 8;
zlib_windowbits = CINFO + 8;
if((zhead & 0xffff) % 31) {
cli_dbgmsg("PNG: compression header fails checksum\n");
return CL_EPARSE;
} else if (CM != 8) {
cli_dbgmsg("PNG: non-deflate compression method (%d)\n", CM);
return CL_EPARSE;
}
}
}
if (check_zlib && !zlib_error) {
static uch *p; /* always points to next filter byte */
static int cur_y, cur_pass, cur_xoff, cur_yoff, cur_xskip, cur_yskip;
static long cur_width, cur_linebytes;
static long numfilt, numfilt_this_block, numfilt_total, numfilt_pass[7];
uch *eod;
int err=Z_OK;
zstrm.next_in = buffer;
zstrm.avail_in = toread;
/* initialize zlib and bit/byte/line variables if not already done */
if (first_idat) {
zstrm.next_out = p = outbuf;
zstrm.avail_out = BS;
zstrm.zalloc = (alloc_func)Z_NULL;
zstrm.zfree = (free_func)Z_NULL;
zstrm.opaque = (voidpf)Z_NULL;
if ((err = inflateInit2(&zstrm, zlib_windowbits)) != Z_OK) {
cli_dbgmsg("PNG: zlib: can't initialize (error = %d)\n", err);
return CL_EUNPACK;
}
cur_y = 0;
cur_pass = 1; /* interlace pass: 1 through 7 */
cur_xoff = cur_yoff = 0;
cur_xskip = cur_yskip = lace? 8 : 1;
cur_width = (w - cur_xoff + cur_xskip - 1) / cur_xskip; /* round up */
cur_linebytes = ((cur_width*bitdepth + 7) >> 3) + 1; /* round, fltr */
numfilt = 0L;
first_idat = 0;
if (lace) { /* loop through passes to calculate total filters */
int passm1, yskip=0, yoff=0, xoff=0;
for (passm1 = 0; passm1 < 7; ++passm1) {
switch (passm1) { /* (see table below for full summary) */
case 0: yskip = 8; yoff = 0; xoff = 0; break;
case 1: yskip = 8; yoff = 0; xoff = 4; break;
case 2: yskip = 8; yoff = 4; xoff = 0; break;
case 3: yskip = 4; yoff = 0; xoff = 2; break;
case 4: yskip = 4; yoff = 2; xoff = 0; break;
case 5: yskip = 2; yoff = 0; xoff = 1; break;
case 6: yskip = 2; yoff = 1; xoff = 0; break;
}
/* effective height is reduced if odd pass: subtract yoff (but
* if effective width of pass is 0 => no rows and no filters) */
numfilt_pass[passm1] =
(w <= xoff)? 0 : (h - yoff + yskip - 1) / yskip;
if (passm1 > 0) /* now make it cumulative */
numfilt_pass[passm1] += numfilt_pass[passm1 - 1];
}
} else {
numfilt_pass[0] = h; /* if non-interlaced */
numfilt_pass[1] = numfilt_pass[2] = numfilt_pass[3] = h;
numfilt_pass[4] = numfilt_pass[5] = numfilt_pass[6] = h;
}
numfilt_total = numfilt_pass[6];
}
numfilt_this_block = 0L;
while (err != Z_STREAM_END && zstrm.avail_in > 0) {
/* know zstrm.avail_out > 0: get some image/filter data */
err = inflate(&zstrm, Z_SYNC_FLUSH);
if (err != Z_OK && err != Z_STREAM_END) {
cli_dbgmsg("PNG: zlib: inflate error\n");
inflateEnd(&zstrm);
return CL_EPARSE;
}
/* now have uncompressed, filtered image data in outbuf */
eod = outbuf + BS - zstrm.avail_out;
while (p < eod) {
if (cur_linebytes) { /* GRP 20000727: bugfix */
int filttype = p[0];
if (filttype > 127) {
if (lace > 1)
break; /* assume it's due to unknown interlace method */
if (numfilt_this_block == 0) {
/* warn only on first one per block; don't break */
cli_dbgmsg("PNG: private (invalid?) row-filter type (%d)\n", filttype);
inflateEnd(&zstrm);
return CL_EPARSE;
}
} else if (filttype > 4) {
if (lace <= 1) {
cli_dbgmsg("PNG: invalid row-filter type (%d)\n", filttype);
inflateEnd(&zstrm);
return CL_EPARSE;
} /* else assume it's due to unknown interlace method */
break;
}
++numfilt;
p += cur_linebytes;
}
cur_y += cur_yskip;
if (lace) {
while (cur_y >= h) { /* may loop if very short image */
/*
pass xskip yskip xoff yoff
1 8 8 0 0
2 8 8 4 0
3 4 8 0 4
4 4 4 2 0
5 2 4 0 2
6 2 2 1 0
7 1 2 0 1
*/
++cur_pass;
if (cur_pass & 1) { /* beginning an odd pass */
cur_yoff = cur_xoff;
cur_xoff = 0;
cur_xskip >>= 1;
} else { /* beginning an even pass */
if (cur_pass == 2)
cur_xoff = 4;
else {
cur_xoff = cur_yoff >> 1;
cur_yskip >>= 1;
}
cur_yoff = 0;
}
cur_y = cur_yoff;
/* effective width is reduced if even pass: subtract cur_xoff */
cur_width = (w - cur_xoff + cur_xskip - 1) / cur_xskip;
cur_linebytes = ((cur_width*bitdepth + 7) >> 3) + 1;
if (cur_linebytes == 1) /* just the filter byte? no can do */
cur_linebytes = 0; /* GRP 20000727: added fix */
}
} else if (cur_y >= h) {
inflateEnd(&zstrm);
if(eod - p > 0) {
cli_dbgmsg("PNG: %li bytes remaining in buffer before inflateEnd()", eod-p);
return CL_EPARSE;
}
err = Z_STREAM_END;
zlib_error = 1;
}
}
p -= (eod - outbuf); /* wrap p back into outbuf region */
zstrm.next_out = outbuf;
zstrm.avail_out = BS;
/* get more input (waiting until buffer empties is not necessary best
* zlib strategy, but simpler than shifting leftover data around) */
if (zstrm.avail_in == 0 && sz > toread) {
int data_read;
sz -= toread;
toread = (sz > BS)? BS:sz;
if((data_read = fmap_readn(map, buffer, offset, toread)) != toread) {
cli_dbgmsg("PNG: EOF while reading %s data\n", chunkid);
return CL_EPARSE;
}
offset += toread;
crc = update_crc(crc, buffer, toread);
zstrm.next_in = buffer;
zstrm.avail_in = toread;
}
}
}
last_is_IDAT = 1;
last_is_JDAT = 0;
/*------*
| IEND |
*------*/
} else if (strcmp(chunkid, "IEND") == 0) {
if (have_IEND) {
cli_dbgmsg("PNG: multiple IEND not allowed\n");
return CL_EPARSE;
} else if (sz != 0) {
cli_dbgmsg("PNG: invalid IEND length\n");
return CL_EPARSE;
} else if (have_IDAT <= 0) {
cli_dbgmsg("PNG: no IDAT chunks\n");
return CL_EPARSE;
} else if (have_IDAT < 10) {
cli_dbgmsg("PNG: not enough IDAT data\n");
return CL_EPARSE;
}
have_IEND = 1;
last_is_IDAT = last_is_JDAT = 0;
/*------*
| bKGD |
*------*/
} else if (strcmp(chunkid, "bKGD") == 0) {
if (have_bKGD) {
cli_dbgmsg("PNG: multiple bKGD not allowed\n");
return CL_EPARSE;
} else if ((have_IDAT || have_JDAT)) {
cli_dbgmsg("PNG: bKGD must precede IDAT\n");
return CL_EPARSE;
}
switch (ityp) {
case 0:
case 4:
if (sz != 2) {
cli_dbgmsg("PNG: invalid bKGD length\n");
return CL_EPARSE;
}
break;
case 1: /* MNG top-level chunk (default values): "as if 16-bit RGBA" */
case 2:
case 6:
if (sz != 6) {
cli_dbgmsg("PNG: invalid bKGD length\n");
return CL_EPARSE;
}
break;
case 3:
if (sz != 1) {
cli_dbgmsg("PNG: invalid bKGD length\n");
return CL_EPARSE;
} else if (buffer[0] >= nplte) {
cli_dbgmsg("PNG: bKGD index falls outside PLTE\n");
return CL_EPARSE;
}
break;
}
have_bKGD = 1;
last_is_IDAT = last_is_JDAT = 0;
/*------*
| cHRM |
*------*/
} else if (strcmp(chunkid, "cHRM") == 0) {
if (have_cHRM) {
cli_dbgmsg("PNG: multiple cHRM not allowed\n");
return CL_EPARSE;
} else if (have_PLTE) {
cli_dbgmsg("PNG: cHRM must precede PLTE\n");
return CL_EPARSE;
} else if ((have_IDAT || have_JDAT)) {
cli_dbgmsg("PNG: cHRM must precede IDAT\n");
return CL_EPARSE;
} else if (sz != 32) {
cli_dbgmsg("PNG: invalid cHRM length\n");
return CL_EPARSE;
} else {
double wx, wy, rx, ry, gx, gy, bx, by;
wx = (double)LG(buffer)/100000;
wy = (double)LG(buffer+4)/100000;
rx = (double)LG(buffer+8)/100000;
ry = (double)LG(buffer+12)/100000;
gx = (double)LG(buffer+16)/100000;
gy = (double)LG(buffer+20)/100000;
bx = (double)LG(buffer+24)/100000;
by = (double)LG(buffer+28)/100000;
if (wx < 0 || wx > 0.8 || wy < 0 || wy > 0.8 || wx + wy > 1.0) {
cli_dbgmsg("PNG: invalid cHRM white point\n");
return CL_EPARSE;
} else if (rx < 0 || rx > 0.8 || ry < 0 || ry > 0.8 || rx + ry > 1.0) {
cli_dbgmsg("PNG: invalid cHRM red point\n");
return CL_EPARSE;
} else if (gx < 0 || gx > 0.8 || gy < 0 || gy > 0.8 || gx + gy > 1.0) {
cli_dbgmsg("PNG: invalid cHRM green point\n");
return CL_EPARSE;
} else if (bx < 0 || bx > 0.8 || by < 0 || by > 0.8 || bx + by > 1.0) {
cli_dbgmsg("PNG: invalid cHRM blue point\n");
return CL_EPARSE;
}
}
have_cHRM = 1;
last_is_IDAT = last_is_JDAT = 0;
/*------*
| fRAc |
*------*/
} else if (strcmp(chunkid, "fRAc") == 0) {
last_is_IDAT = last_is_JDAT = 0;
/*------*
| gAMA |
*------*/
} else if (strcmp(chunkid, "gAMA") == 0) {
if (have_gAMA) {
cli_dbgmsg("PNG: multiple gAMA not allowed\n");
return CL_EPARSE;
} else if (have_IDAT || have_JDAT) {
cli_dbgmsg("PNG: gAMA must precede IDAT\n");
return CL_EPARSE;
} else if (have_PLTE) {
cli_dbgmsg("PNG: gAMA must precede PLTE\n");
return CL_EPARSE;
} else if (sz != 4) {
cli_dbgmsg("PNG: invalid gAMA length\n");
return CL_EPARSE;
} else if (LG(buffer) == 0) {
cli_dbgmsg("PNG: invalid gAMA value (0.0000)\n");
return CL_EPARSE;
}
have_gAMA = 1;
last_is_IDAT = last_is_JDAT = 0;
/*------*
| gIFg |
*------*/
} else if (strcmp(chunkid, "gIFg") == 0) {
if (sz != 4) {
cli_dbgmsg("PNG: invalid gIFg length\n");
return CL_EPARSE;
}
last_is_IDAT = last_is_JDAT = 0;
/*------*
| gIFt |
*------*/
} else if (strcmp(chunkid, "gIFt") == 0) {
if (sz < 24) {
cli_dbgmsg("PNG: invalid gIFt length\n");
return CL_EPARSE;
}
last_is_IDAT = last_is_JDAT = 0;
/*------*
| gIFx |
*------*/
} else if (strcmp(chunkid, "gIFx") == 0) {
if (sz < 11) {
cli_dbgmsg("PNG: invalid gIFx length\n");
return CL_EPARSE;
}
last_is_IDAT = last_is_JDAT = 0;
/*------*
| hIST |
*------*/
} else if (strcmp(chunkid, "hIST") == 0) {
if (have_hIST) {
cli_dbgmsg("PNG: multiple hIST not allowed\n");
return CL_EPARSE;
} else if (!have_PLTE) {
cli_dbgmsg("PNG: hIST must follow PLTE\n");
return CL_EPARSE;
} else if (have_IDAT) {
cli_dbgmsg("PNG: hIST must precede IDAT\n");
return CL_EPARSE;
} else if (sz != nplte * 2) {
cli_dbgmsg("PNG: invalid number of hIST entries (%g)\n", (double)sz / 2);
return CL_EPARSE;
}
have_hIST = 1;
last_is_IDAT = last_is_JDAT = 0;
/*------*
| iCCP |
*------*/
} else if (strcmp(chunkid, "iCCP") == 0) {
int name_len;
if (have_iCCP) {
cli_dbgmsg("PNG: multiple iCCP not allowed\n");
return CL_EPARSE;
} else if (have_sRGB) {
cli_dbgmsg("PNG: iCCP not allowed with sRGB\n");
return CL_EPARSE;
} else if (have_PLTE) {
cli_dbgmsg("PNG: iCCP must precede PLTE\n");
return CL_EPARSE;
} else if (have_IDAT || have_JDAT) {
cli_dbgmsg("PNG: iCCP must precede IDAT\n");
return CL_EPARSE;
} else if (check_keyword(buffer, toread, &name_len)) {
return CL_EPARSE;
} else {
int remainder = toread - name_len - 3;
uch compr = buffer[name_len+1];
if (remainder < 0) {
cli_dbgmsg("PNG: invalid iCCP length\n");
return CL_EPARSE;
} else if (buffer[name_len] != 0) {
cli_dbgmsg("PNG: missing NULL after iCCP profile name\n");
return CL_EPARSE;
} else if (compr > 0 && compr < 128) {
cli_dbgmsg("PNG: invalid iCCP compression method (%d)\n", compr);
return CL_EPARSE;
} else if (compr >= 128) {
return CL_EPARSE;
}
}
have_iCCP = 1;
last_is_IDAT = last_is_JDAT = 0;
/*------*
| iTXt |
*------*/
} else if (strcmp(chunkid, "iTXt") == 0) {
int keylen;
if (check_keyword(buffer, toread, &keylen))
return CL_EPARSE;
else {
int compressed = 0, compr = 0;
if(keylen + 1 >= BS)
return CL_EPARSE;
compressed = buffer[keylen+1];
if (compressed < 0 || compressed > 1) {
cli_dbgmsg("PNG: invalid iTXt compression flag (%d)\n", compressed);
return CL_EPARSE;
} else if ((compr = (uch)buffer[keylen+2]) > 127) {
cli_dbgmsg("PNG: private (invalid?) iTXt compression method (%d)\n", compr);
return CL_EPARSE;
} else if (compr > 0) {
cli_dbgmsg("PNG: invalid iTXt compression method (%d)\n", compr);
return CL_EPARSE;
}
}
last_is_IDAT = last_is_JDAT = 0;
/*------*
| oFFs |
*------*/
} else if (strcmp(chunkid, "oFFs") == 0) {
if (have_oFFs) {
cli_dbgmsg("PNG: multiple oFFs not allowed\n");
return CL_EPARSE;
} else if (have_IDAT || have_JDAT) {
cli_dbgmsg("PNG: oFFs must precede IDAT\n");
return CL_EPARSE;
} else if (sz != 9) {
cli_dbgmsg("PNG: invalid oFFs length\n");
return CL_EPARSE;
} else if (buffer[8] > 1) {
cli_dbgmsg("PNG: invalid oFFs unit specifier (%u)\n", buffer[8]);
return CL_EPARSE;
}
have_oFFs = 1;
last_is_IDAT = last_is_JDAT = 0;
/*------*
| pCAL |
*------*/
} else if (strcmp(chunkid, "pCAL") == 0) {
if (have_pCAL) {
cli_dbgmsg("PNG: multiple pCAL not allowed\n");
return CL_EPARSE;
} else if (have_IDAT) {
cli_dbgmsg("PNG: pCAL must precede IDAT\n");
return CL_EPARSE;
}
have_pCAL = 1;
last_is_IDAT = last_is_JDAT = 0;
/*------*
| pHYs |
*------*/
} else if (strcmp(chunkid, "pHYs") == 0) {
if (have_pHYs) {
cli_dbgmsg("PNG: multiple pHYs not allowed\n");
return CL_EPARSE;
} else if (have_IDAT || have_JDAT) {
cli_dbgmsg("PNG: pHYS must precede DAT\n");
return CL_EPARSE;
} else if (sz != 9) {
cli_dbgmsg("PNG: invalid pHYS length\n");
return CL_EPARSE;
} else if (buffer[8] > 1) {
cli_dbgmsg("PNG: invalid pHYs unit specifier (%u)\n", buffer[8]);
return CL_EPARSE;
}
have_pHYs = 1;
last_is_IDAT = last_is_JDAT = 0;
/*------*
| sBIT |
*------*/
} else if (strcmp(chunkid, "sBIT") == 0) {
int maxbits = (ityp == 3)? 8 : sampledepth;
if (have_sBIT) {
cli_dbgmsg("PNG: multiple sBIT not allowed\n");
return CL_EPARSE;
} else if (have_PLTE) {
cli_dbgmsg("PNG: sBIT must precede PLTE\n");
return CL_EPARSE;
} else if (have_IDAT) {
cli_dbgmsg("PNG: sBIT must precede IDAT\n");
return CL_EPARSE;
}
switch (ityp) {
case 0:
if (sz != 1) {
cli_dbgmsg("PNG: invalid sBIT length\n");
return CL_EPARSE;
} else if (buffer[0] == 0 || buffer[0] > maxbits) {
cli_dbgmsg("PNG: sBIT grey bits invalid for sample image\n");
return CL_EPARSE;
}
break;
case 2:
case 3:
if (sz != 3) {
cli_dbgmsg("PNG: invalid sBIT length\n");
return CL_EPARSE;
} else if (buffer[0] == 0 || buffer[0] > maxbits) {
cli_dbgmsg("PNG: sBIT red bits invalid for sample image\n");
return CL_EPARSE;
} else if (buffer[1] == 0 || buffer[1] > maxbits) {
cli_dbgmsg("PNG: sBIT green bits invalid for sample image\n");
return CL_EPARSE;
} else if (buffer[2] == 0 || buffer[2] > maxbits) {
cli_dbgmsg("PNG: sBIT blue bits invalid for sample image\n");
return CL_EPARSE;
}
break;
case 4:
if (sz != 2) {
cli_dbgmsg("PNG: invalid length\n");
return CL_EPARSE;
} else if (buffer[0] == 0 || buffer[0] > maxbits) {
cli_dbgmsg("PNG: grey bits invalid for sample image\n");
return CL_EPARSE;
} else if (buffer[1] == 0 || buffer[1] > maxbits) {
cli_dbgmsg("PNG: alpha bits invalid for sample image\n");
return CL_EPARSE;
}
break;
case 6:
if (sz != 4) {
cli_dbgmsg("PNG: invalid sBIT length\n");
return CL_EPARSE;
} else if (buffer[0] == 0 || buffer[0] > maxbits) {
cli_dbgmsg("PNG: red bits invalid for sample image\n");
return CL_EPARSE;
} else if (buffer[1] == 0 || buffer[1] > maxbits) {
cli_dbgmsg("PNG: green bits invalid for sample image\n");
return CL_EPARSE;
} else if (buffer[2] == 0 || buffer[2] > maxbits) {
cli_dbgmsg("PNG: blue bits invalid for sample image\n");
return CL_EPARSE;
} else if (buffer[3] == 0 || buffer[3] > maxbits) {
cli_dbgmsg("PNG: alpha bits invalid for sample image\n");
return CL_EPARSE;
}
break;
}
have_sBIT = 1;
last_is_IDAT = last_is_JDAT = 0;
/*------*
| sCAL |
*------*/
} else if (strcmp(chunkid, "sCAL") == 0) {
int unittype = buffer[0];
uch *pPixwidth = buffer+1, *pPixheight=NULL;
if (have_sCAL) {
cli_dbgmsg("PNG: multiple sCAL not allowed\n");
return CL_EPARSE;
} else if (have_IDAT || have_JDAT) {
cli_dbgmsg("PNG: sCAL must precede IDAT\n");
return CL_EPARSE;
} else if (sz < 4) {
cli_dbgmsg("PNG: invalid sCAL length\n");
return CL_EPARSE;
} else if (unittype < 1 || unittype > 2) {
cli_dbgmsg("PNG: invalid sCAL unit specifier (%d)\n", unittype);
return CL_EPARSE;
} else {
uch *qq;
for (qq = pPixwidth; qq < buffer+sz; ++qq) {
if (*qq == 0)
break;
}
if (qq == buffer+sz) {
cli_dbgmsg("PNG: missing sCAL null separator\n");
return CL_EPARSE;
} else {
pPixheight = qq + 1;
if (pPixheight == buffer+sz || *pPixheight == 0) {
cli_dbgmsg("PNG: missing sCAL pixel height\n");
return CL_EPARSE;
}
}
for (qq = pPixheight; qq < buffer+sz; ++qq) {
if (*qq == 0)
break;
}
if (qq != buffer+sz) {
cli_dbgmsg("PNG: extra sCAL null separator\n");
return CL_EPARSE;
}
if (*pPixwidth == '-' || *pPixheight == '-') {
cli_dbgmsg("PNG: invalid negative sCAL value(s)\n");
return CL_EPARSE;
} else if (check_ascii_float(pPixwidth, pPixheight-pPixwidth-1) ||
check_ascii_float(pPixheight, buffer+sz-pPixheight))
{
return CL_EPARSE;
}
}
have_sCAL = 1;
last_is_IDAT = last_is_JDAT = 0;
/*------*
| sPLT |
*------*/
} else if (strcmp(chunkid, "sPLT") == 0) {
int name_len;
if (have_IDAT) {
cli_dbgmsg("PNG: sPLT must precede IDAT\n");
return CL_EPARSE;
} else if (check_keyword(buffer, toread, &name_len)) {
return CL_EPARSE;
} else {
uch bps = buffer[name_len+1];
int remainder = toread - name_len - 2;
int bytes = (bps >> 3);
int entry_sz = 4*bytes + 2;
if (remainder < 0) {
cli_dbgmsg("PNG: invalid sPLT length\n");
return CL_EPARSE;
} else if (buffer[name_len] != 0) {
cli_dbgmsg("PNG: missing NULL after sPLT palette name\n");
return CL_EPARSE;
} else if (bps != 8 && bps != 16) {
cli_dbgmsg("PNG: invalid sPLT sample depth\n");
return CL_EPARSE;
} else if (remainder % entry_sz != 0) {
cli_dbgmsg("PNG: invalid number of sPLT entries\n");
return CL_EPARSE;
}
}
last_is_IDAT = last_is_JDAT = 0;
/*------*
| sRGB |
*------*/
} else if (strcmp(chunkid, "sRGB") == 0) {
if (have_sRGB) {
cli_dbgmsg("PNG: multiple sRGB not allowed\n");
return CL_EPARSE;
} else if (have_iCCP) {
cli_dbgmsg("PNG: sRGB not allowed with iCCP\n");
return CL_EPARSE;
} else if (have_PLTE) {
cli_dbgmsg("PNG: sRGB must precede PLTE\n");
return CL_EPARSE;
} else if (have_IDAT || have_JDAT) {
cli_dbgmsg("PNG: sRGB must precede IDAT\n");
return CL_EPARSE;
} else if (sz != 1) {
cli_dbgmsg("PNG: invalid sRGB length\n");
return CL_EPARSE;
} else if (buffer[0] > 3) {
cli_dbgmsg("PNG: sRGB invalid rendering intent\n");
return CL_EPARSE;
}
have_sRGB = 1;
last_is_IDAT = last_is_JDAT = 0;
/*------*
| sTER |
*------*/
} else if (strcmp(chunkid, "sTER") == 0) {
if (have_sTER) {
cli_dbgmsg("PNG: multiple sTER not allowed\n");
return CL_EPARSE;
} else if (have_IDAT || have_JDAT) {
cli_dbgmsg("PNG: sTER must precede IDAT\n");
return CL_EPARSE;
} else if (sz != 1) {
cli_dbgmsg("PNG: invalid sTER length\n");
return CL_EPARSE;
} else if (buffer[0] > 1) {
cli_dbgmsg("PNG: invalid sTER layout mode\n");
return CL_EPARSE;
}
have_sTER = 1;
last_is_IDAT = last_is_JDAT = 0;
/*------* *------*
| tEXt | | zTXt |
*------* *------*/
} else if (strcmp(chunkid, "tEXt") == 0 || strcmp(chunkid, "zTXt") == 0) {
int ztxt = (chunkid[0] == 'z');
int keylen;
if (check_keyword(buffer, toread, &keylen))
return CL_EPARSE;
else if (ztxt) {
int compr = (uch)buffer[keylen+1];
if (compr > 127) {
cli_dbgmsg("PNG: private (possibly invalid) compression method\n");
return CL_EPARSE;
} else if (compr > 0) {
cli_dbgmsg("PNG: invalid compression method\n");
return CL_EPARSE;
}
}
else if (check_text(buffer + keylen + 1, toread - keylen - 1)) {
return CL_EPARSE;
}
last_is_IDAT = last_is_JDAT = 0;
/*------*
| tIME |
*------*/
} else if (strcmp(chunkid, "tIME") == 0) {
if (have_tIME) {
cli_dbgmsg("PNG: multiple tIME not allowed\n");
return CL_EPARSE;
} else if (sz != 7) {
cli_dbgmsg("PNG: invalid tIME length\n");
return CL_EPARSE;
} else {
int yr = SH(buffer);
int mo = buffer[2];
int dy = buffer[3];
int hh = buffer[4];
int mm = buffer[5];
int ss = buffer[6];
if (yr < 1995) {
/* conversion to PNG format counts as modification... */
/* FIXME: also test for future dates? (may allow current year + 1) */
cli_dbgmsg("PNG: invalid year\n");
return CL_EPARSE;
} else if (mo < 1 || mo > 12) {
cli_dbgmsg("PNG: invalid month\n");
return CL_EPARSE;
} else if (dy < 1 || dy > 31) {
/* FIXME: also validate day given specified month? */
cli_dbgmsg("PNG: invalid day\n");
return CL_EPARSE;
} else if (hh < 0 || hh > 23) {
cli_dbgmsg("PNG: invalid hour\n");
return CL_EPARSE;
} else if (mm < 0 || mm > 59) {
cli_dbgmsg("PNG: invalid minute\n");
return CL_EPARSE;
} else if (ss < 0 || ss > 60) {
cli_dbgmsg("PNG: invalid second\n");
return CL_EPARSE;
}
cli_dbgmsg("PNG: Time: %2d %s %4d %02d:%02d:%02d UTC\n", dy, getmonth(mo), yr, hh, mm, ss);
}
have_tIME = 1;
last_is_IDAT = last_is_JDAT = 0;
/*------*
| tRNS |
*------*/
} else if (strcmp(chunkid, "tRNS") == 0) {
if (have_tRNS) {
cli_dbgmsg("PNG: multiple tRNS not allowed\n");
return CL_EPARSE;
} else if (ityp == 3 && !have_PLTE) {
cli_dbgmsg("PNG: tRNS must follow PLTE\n");
return CL_EPARSE;
} else if (have_IDAT) {
cli_dbgmsg("PNG: tRNS must precede IDAT\n");
return CL_EPARSE;
} else {
switch (ityp) {
case 0:
if (sz != 2) {
cli_dbgmsg("PNG: invalid tRNS length for %s image\n", png_type[ityp]);
return CL_EPARSE;
}
break;
case 2:
if (sz != 6) {
cli_dbgmsg("PNG: invalid tRNS length for %s image\n", png_type[ityp]);
return CL_EPARSE;
}
break;
case 3:
if (sz > nplte) {
cli_dbgmsg("PNG: invalid tRNS length for %s image\n", png_type[ityp]);
return CL_EPARSE;
}
break;
default:
cli_dbgmsg("PNG: tRNS not allowed in %s image\n", png_type[ityp]);
return CL_EPARSE;
break;
}
}
have_tRNS = 1;
last_is_IDAT = last_is_JDAT = 0;
/*===============*
* unknown chunk *
*===============*/
} else {
if (CRITICAL(chunkid) && SAFECOPY(chunkid)) {
/* a critical, safe-to-copy chunk is an error */
cli_dbgmsg("PNG: illegal critical, safe-to-copy chunk\n");
return CL_EPARSE;
} else if (RESERVED(chunkid)) {
/* a chunk with the reserved bit set is an error (or spec updated) */
cli_dbgmsg("PNG: illegal reserved-bit-set chunk\n");
return CL_EPARSE;
} else if (PUBLIC(chunkid)) {
/* GRR 20050725: all registered (public) PNG/MNG/JNG chunks are now
* known to pngcheck, so any unknown public ones are invalid (or have
* been proposed and approved since the last release of pngcheck) */
cli_dbgmsg("PNG: illegal (unless recently approved) unknown, public\n");
return CL_EPARSE;
} else if (/* !PUBLIC(chunkid) && */ CRITICAL(chunkid)) {
cli_dbgmsg("PNG: private, critical chunk (warning)\n");
return CL_EPARSE; /* not an error if used only internally */
}
last_is_IDAT = last_is_JDAT = 0;
}
while (sz > toread) {
int data_read;
sz -= toread;
toread = (sz > BS)? BS:sz;
data_read = fmap_readn(map, buffer, offset, toread);
if (data_read != toread) {
cli_dbgmsg("PNG: EOF while reading final data\n");
return CL_EPARSE;
}
offset += toread;
crc = update_crc(crc, (uch *)buffer, toread);
}
filecrc = getlong(map, &offset, "CRC value");
if (filecrc != CRCCOMPL(crc)) {
cli_dbgmsg("PNG: CRC error in chunk %s (computed %08lx, expected %08lx)\n",
chunkid, CRCCOMPL(crc), filecrc);
return CL_EPARSE;
}
}
/*----------------------- END OF IMMENSE WHILE-LOOP -----------------------*/
if (!have_IEND) {
cli_dbgmsg("PNG: file doesn't end with a IEND chunk\n");
return CL_EPARSE;
}
return CL_SUCCESS;
}
|