-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpngcheck.c
5402 lines (4963 loc) · 181 KB
/
pngcheck.c
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
/*
* pngcheck: Authenticate the structure of a PNG file and dump info about
* it if desired.
*
* This program checks the PNG signature bytes (with tests for various forms
* of text-mode corruption), chunks (CRCs, dependencies, out-of-range values),
* and compressed image data (IDAT zlib stream). In addition, it optionally
* dumps the contents of PNG, JNG and MNG image streams in more-or-less human-
* readable form.
*
* NOTE: this program is currently NOT EBCDIC-compatible!
* (as of July 2007)
*
* Maintainer: Greg Roelofs <[email protected]>
* ChangeLog: see CHANGELOG file
*/
/*============================================================================
*
* Copyright 1995-2017 by Alexander Lehmann <[email protected]>,
* Andreas Dilger <[email protected]>,
* Glenn Randers-Pehrson <[email protected]>,
* Greg Roelofs <[email protected]>,
* John Bowler <[email protected]>,
* Tom Lane <[email protected]>
*
* 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.
*
*===========================================================================*/
#define VERSION "2.4.0-beta09 of 9 January 2017"
/*
* NOTE: current MNG support is informational; error-checking is MINIMAL!
*
*
* Currently supported chunks, in order of appearance in pngcheck() function:
*
* IHDR JHDR MHDR // PNG/JNG/MNG header chunks
*
* PLTE IDAT IEND // critical PNG chunks
*
* bKGD cHRM fRAc gAMA gIFg gIFt gIFx hIST // ancillary PNG chunks
* iCCP iTXt oFFs pCAL pHYs sBIT sCAL sPLT
* sRGB tEXt zTXt tIME tRNS
*
* cmOD cmPP cpIp mkBF mkBS mkBT mkTS pcLb // known private PNG chunks
* prVW spAL // [msOG = ??]
*
* JDAT JSEP // critical JNG chunks
*
* DHDR FRAM SAVE SEEK nEED DEFI BACK MOVE // MNG chunks
* CLON SHOW CLIP LOOP ENDL PROM fPRI eXPI
* BASI IPNG PPLT PAST TERM DISC pHYg DROP
* DBYK ORDR MAGN MEND
*
* Known unregistered, "public" chunks (i.e., invalid and now flagged as such):
*
* pRVW nULL tXMP
*
*
* GRR to do:
* - normalize error levels (mainly usage of kMinorError vs. kMajorError)
* - fix tEXt chunk: small buffers or lots of text => truncation
* (see pngcheck-1.99.4-test.c.dif)
* - fix iCCP, sPLT chunks: small buffers or large chunks => truncation?
* - update existing MNG support to version 1.0 (DHDR bug just fixed 2010!)
* - add JNG restrictions to bKGD
* - allow top-level ancillary PNGs in MNG (i.e., subsequent ones may be NULL)
* * add MNG profile report based on actual chunks found
* - split out each chunk's code into XXXX() function (e.g., IDAT(), tRNS())
* - DOS/Win32 wildcard support beyond emx+gcc, MSVC (Borland wildargs.obj?)
* - EBCDIC support (minimal?)
* - go back and make sure validation checks not dependent on verbosity level
*
*
* GRR NOTE: The MNG "top level" concept is not explicitly defined anywhere
* in the MNG 1.0 spec, but it refers to "global" chunks/values and apparently
* means before any embedded PNG or JNG images appear (i.e., before any IHDR
* or JHDR chunks are encountered). The top_level variable is set accordingly.
*/
/*
* Compilation example (GNU C, command line; replace "/zlibpath" appropriately):
*
* without zlib:
* gcc -O -o pngcheck pngcheck.c
* with zlib support (recommended):
* gcc -O -DUSE_ZLIB -I/zlibpath -o pngcheck pngcheck.c -L/zlibpath -lz
* or (static zlib):
* gcc -O -DUSE_ZLIB -I/zlibpath -o pngcheck pngcheck.c /zlibpath/libz.a
*
* Windows compilation example (MSVC, command line, assuming VCVARS32.BAT or
* whatever has been run):
*
* without zlib:
* cl -nologo -O -W3 -DWIN32 -c pngcheck.c
* link -nologo pngcheck.obj setargv.obj
* with zlib support (note that Win32 zlib is compiled as a DLL by default):
* cl -nologo -O -W3 -DWIN32 -DUSE_ZLIB -I/zlibpath -c pngcheck.c
* link -nologo pngcheck.obj setargv.obj \zlibpath\zlib.lib
* [copy pngcheck.exe and zlib.dll to installation directory]
*
* "setargv.obj" is included with MSVC and will be found if the batch file has
* been run. Either Borland or Watcom (both?) may use "wildargs.obj" instead.
* Both object files serve the same purpose: they expand wildcard arguments
* into a list of files on the local file system, just as Unix shells do by
* default ("globbing"). Note that mingw32 + gcc (Unix-like compilation
* environment for Windows) apparently expands wildcards on its own, so no
* special object files are necessary for it. emx + gcc for OS/2 (and possibly
* rsxnt + gcc for Windows NT) has a special _wildcard() function call, which
* is already included at the top of main() below.
*
* zlib info: http://www.zlib.net/
* PNG/MNG/JNG info: http://www.libpng.org/pub/png/
* http://www.libpng.org/pub/mng/ and
* ftp://ftp.simplesystems.org/pub/libpng/mng/
* pngcheck sources: http://www.libpng.org/pub/png/apps/pngcheck.html
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#ifdef __riscos
/* not sure if this will work (fragile!), but relatively clean... */
struct stat { long st_size; };
# define stat(f,s) _swix(8 /*OS_File*/, 3 | 1<<27, 17, f, s.st_size)
# define isatty(fd) (!__iob[fd].__file)
#else
# include <fcntl.h>
# if defined(__MWERKS__) && defined(macintosh) /* pxm for CodeWarrior */
# include <types.h>
# include <stat.h>
# elif defined(applec) || defined(THINK_C) /* via Mark Fleming; not tested */
# include <Types.h>
# include <ToolUtils.h>
# else
# include <sys/types.h>
# include <sys/stat.h>
# endif
#endif
#if defined(unix) || (defined(__MWERKS__) && defined(macintosh)) /* pxm */
# include <unistd.h> /* isatty() */
#endif
#ifdef WIN32
# include <io.h>
#endif
#ifdef USE_ZLIB
# include <zlib.h>
#endif
typedef unsigned char uch;
typedef unsigned short ush;
typedef unsigned long ulg;
/* printbuf state variables */
typedef struct printbuf_state {
int cr;
int lf;
int nul;
int control;
int esc;
} printbuf_state;
/* int main (int argc, char *argv[]); */
void usage (FILE *fpMsg);
#ifndef USE_ZLIB
void make_crc_table (void);
ulg update_crc (ulg crc, uch *buf, int len);
#endif
ulg getlong (FILE *fp, char *fname, char *where);
void putlong (FILE *fpOut, ulg ul);
void init_printbuf_state (printbuf_state *prbuf);
void print_buffer (printbuf_state *prbuf, uch *buffer, int size, int indent);
void report_printbuf (printbuf_state *prbuf, char *fname, char *chunkid);
int keywordlen (uch *buffer, int maxsize);
const char *getmonth (int m);
int ratio (ulg uc, ulg c);
ulg gcf (ulg a, ulg b);
int pngcheck (FILE *fp, char *_fname, int searching, FILE *fpOut);
int pnginfile (FILE *fp, char *fname, int ipng, int extracting);
void pngsearch (FILE *fp, char *fname, int extracting);
int check_magic (uch *magic, char *fname, int which);
int check_chunk_name (char *chunk_name, char *fname);
int check_keyword (uch *buffer, int maxsize, int *pKeylen,
char *keyword_name, char *chunkid, char *fname);
int check_text (uch *buffer, int maxsize, char *chunkid, char *fname);
int check_ascii_float (uch *buffer, int len, char *chunkid, char *fname);
#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))
/* for check_magic(): */
#define DO_PNG 0
#define DO_MNG 1
#define DO_JNG 2
/* GRR 20070704: borrowed from GRR from/mailx hack */
#define COLOR_NORMAL "\033[0m"
#define COLOR_RED_BOLD "\033[40;31;1m"
#define COLOR_RED "\033[40;31m"
#define COLOR_GREEN_BOLD "\033[40;32;1m"
#define COLOR_GREEN "\033[40;32m"
#define COLOR_YELLOW_BOLD "\033[40;33;1m"
#define COLOR_YELLOW "\033[40;33m" /* chunk names */
#define COLOR_BLUE_BOLD "\033[40;34;1m"
#define COLOR_BLUE "\033[40;34m"
#define COLOR_MAGENTA_BOLD "\033[40;35;1m"
#define COLOR_MAGENTA "\033[40;35m"
#define COLOR_CYAN_BOLD "\033[40;36;1m"
#define COLOR_CYAN "\033[40;36m"
#define COLOR_WHITE_BOLD "\033[40;37;1m" /* filenames, filter seps */
#define COLOR_WHITE "\033[40;37m"
#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))
#define set_err(x) global_error = ((global_error < (x))? (x) : global_error)
#define is_err(x) (global_error > (x) || (!force && global_error == (x)))
#define no_err(x) (global_error < (x) || (force && global_error == (x)))
enum {
kOK = 0,
kWarning, /* could be an error in some circumstances but not all */
kCommandLineError, /* pilot error */
kMinorError, /* minor spec errors (e.g., out-of-range values) */
kMajorError, /* file corruption, invalid chunk length/layout, etc. */
kCriticalError /* unexpected EOF or other file(system) error */
};
/* Command-line flag variables */
int verbose = 0; /* print chunk info */
int quiet = 0; /* print only error messages */
int printtext = 0; /* print tEXt chunks */
int printpal = 0; /* print PLTE/tRNS/hIST/sPLT contents */
int color = 0; /* print with ANSI colors to spice things up */
int sevenbit = 0; /* escape characters >=160 */
int force = 0; /* continue even if an error occurs (CRC error, etc) */
int check_windowbits = 1; /* more stringent zlib stream-checking */
int suppress_warnings = 0; /* don't fuss about ambiguous stuff */
int search = 0; /* hunt for PNGs in the file... */
int extract = 0; /* ...and extract them to arbitrary file names. */
int png = 0; /* it's a PNG */
int mng = 0; /* it's a MNG instead of a PNG (won't work in pipe) */
int jng = 0; /* it's a JNG */
int global_error = kOK; /* the current error status */
uch buffer[BS];
/* what the PNG, MNG and JNG magic numbers should be */
static const uch good_PNG_magic[8] = {137, 80, 78, 71, 13, 10, 26, 10};
static const uch good_MNG_magic[8] = {138, 77, 78, 71, 13, 10, 26, 10};
static const uch good_JNG_magic[8] = {139, 74, 78, 71, 13, 10, 26, 10};
/* 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
};
#ifdef USE_ZLIB
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;
const char **pass_color;
const char *color_off;
#else
ulg crc_table[256]; /* table of CRCs of all 8-bit messages */
int crc_table_computed = 0; /* flag: has the table been computed? */
#endif
static const char *inv = "INVALID";
/* PNG stuff */
static const char *png_type[] = { /* IHDR, tRNS, BASI, summary */
"grayscale",
"INVALID", /* can't use inv as initializer */
"RGB",
"palette", /* was "colormap" */
"grayscale+alpha",
"INVALID",
"RGB+alpha"
};
static const char *deflate_type[] = { /* IDAT */
"superfast",
"fast",
"default",
"maximum"
};
#ifdef USE_ZLIB
static const char *zlib_error_type[] = { /* IDAT */
"filesystem error",
"stream error",
"data error",
"memory error",
"buffering error",
"version error"
};
static const char *pass_color_enabled[] = { /* IDAT */
COLOR_NORMAL, /* color_off */
COLOR_WHITE, /* using 1-based indexing */
COLOR_BLUE,
COLOR_GREEN,
COLOR_YELLOW,
COLOR_RED,
COLOR_CYAN,
COLOR_MAGENTA
};
static const char *pass_color_disabled[] = { /* IDAT */
"", "", "", "", "", "", "", ""
};
#endif /* USE_ZLIB */
static const char *eqn_type[] = { /* pCAL */
"physical_value = p0 + p1 * original_sample / (x1-x0)",
"physical_value = p0 + p1 * exp(p2 * original_sample / (x1-x0))",
"physical_value = p0 + p1 * pow(p2, (original_sample / (x1-x0)))",
"physical_value = p0 + p1 * sinh(p2 * (original_sample - p3) / (x1-x0))"
};
static const int eqn_params[] = { 2, 3, 3, 4 }; /* pCAL */
static const char *rendering_intent[] = { /* sRGB */
"perceptual",
"relative colorimetric",
"saturation-preserving",
"absolute colorimetric"
};
/* JNG stuff */
static const char *jng_type[] = { /* JHDR, summary */
"grayscale",
"YCbCr",
"grayscale+alpha",
"YCbCr+alpha"
};
/* MNG stuff */
static const char *delta_type[] = { /* DHDR */
"full image replacement",
"block pixel addition",
"block alpha addition",
"block pixel replacement",
"block alpha replacement",
"no change"
};
static const char *termination_condition[] = { /* LOOP */
"deterministic",
"decoder discretion",
"user discretion",
"external signal"
};
static const char *termination_action[] = { /* TERM */
"show last frame indefinitely",
"cease displaying anything",
"show first frame after TERM",
"repeat sequence between TERM and MEND"
};
static const char *framing_mode[] = { /* FRAM */
"no change in framing mode",
"no background layer; interframe delay before each image displayed",
"no background layer; interframe delay before each FRAM chunk",
"interframe delay and background layer before each image displayed",
"interframe delay and background layer after each FRAM chunk"
};
static const char *change_interframe_delay[] = { /* FRAM */
"no change in interframe delay",
"change interframe delay for next subframe",
"change interframe delay and make default"
};
static const char *change_timeout_and_termination[] = { /* FRAM */
"no change in timeout and termination",
"deterministic change in timeout and termination for next subframe",
"deterministic change in timeout and termination; make default",
"decoder-discretion change in timeout and termination for next subframe",
"decoder-discretion change in timeout and termination; make default",
"user-discretion change in timeout and termination for next subframe",
"user-discretion change in timeout and termination; make default",
"change in timeout and termination for next subframe via signal",
"change in timeout and termination via signal; make default"
};
static const char *change_subframe_clipping_boundaries[] = { /* FRAM */
"no change in subframe clipping boundaries",
"change frame clipping boundaries for next subframe",
"change frame clipping boundaries and make default"
};
static const char *change_sync_id_list[] = { /* FRAM */
"no change in sync ID list",
"change sync ID list for next subframe:",
"change sync ID list and make default:"
};
static const char *clone_type[] = { /* CLON */
"full",
"partial",
"renumber"
};
static const char *do_not_show[] = { /* DEFI, CLON */
"potentially visible",
"do not show",
"same visibility as parent"
};
static const char *show_mode[] = { /* SHOW */
"make objects potentially visible and display",
"make objects invisible",
"display potentially visible objects",
"make objects potentially visible but do not display",
"toggle potentially visible and invisible objects; display visible ones",
"toggle potentially visible and invisible objects but do not display any",
"make next object potentially visible and display; make rest invisible",
"make next object potentially visible but do not display; make rest invisible"
};
static const char *entry_type[] = { /* SAVE */
"segment with full info",
"segment",
"subframe",
"exported image"
};
static const char *pplt_delta_type[] = { /* PPLT */
"replacement RGB samples",
"delta RGB samples",
"replacement alpha samples",
"delta alpha samples",
"replacement RGBA samples",
"delta RGBA samples"
};
static const char *composition_mode[] = { /* PAST */
"composite over",
"replace",
"composite under"
};
static const char *orientation[] = { /* PAST */
"same as source image",
"flipped left-right then up-down",
"flipped left-right",
"flipped up-down",
"tiled with source image"
};
static const char *order_type[] = { /* ORDR */
"anywhere",
"after IDAT and/or JDAT or JDAA",
"before IDAT and/or JDAT or JDAA",
"before IDAT but not before PLTE",
"before IDAT but not after PLTE"
};
static const char *magnification_method[] = { /* MAGN */
"no magnification",
"pixel replication of all samples",
"linear interpolation of all samples",
"replication of all samples from nearest pixel",
"linear interpolation of color, nearest-pixel replication of alpha",
"linear interpolation of alpha, nearest-pixel replication of color"
};
const char *brief_error_color = COLOR_RED_BOLD "ERROR" COLOR_NORMAL;
const char *brief_error_plain = "ERROR";
const char *brief_warn_color = COLOR_YELLOW_BOLD "WARN" COLOR_NORMAL;
const char *brief_warn_plain = "WARN";
const char *brief_OK_color = COLOR_GREEN_BOLD "OK" COLOR_NORMAL;
const char *brief_OK_plain = "OK";
const char *errors_color = COLOR_RED_BOLD "ERRORS DETECTED" COLOR_NORMAL;
const char *errors_plain = "ERRORS DETECTED";
const char *warnings_color = COLOR_YELLOW_BOLD "WARNINGS DETECTED" COLOR_NORMAL;
const char *warnings_plain = "WARNINGS DETECTED";
const char *no_err_color = COLOR_GREEN_BOLD "No errors detected" COLOR_NORMAL;
const char *no_err_plain = "No errors detected";
int main(int argc, char *argv[])
{
FILE *fp;
int i = 1;
int err = kOK;
int num_files = 0;
int num_errors = 0;
int num_warnings = 0;
const char *brief_error = color? brief_error_color : brief_error_plain;
const char *errors_detected = color? errors_color : errors_plain;
#ifdef __EMX__
_wildcard(&argc, &argv); /* Unix-like globbing for OS/2 and DOS */
#endif
while (argc > 1 && argv[1][0] == '-') {
switch (argv[1][i]) {
case '\0':
--argc;
++argv;
i = 1;
break;
case '7':
printtext = 1;
sevenbit = 1;
++i;
break;
case 'c':
color = 1;
++i;
break;
case 'f':
force = 1;
++i;
break;
case 'h':
usage(stdout);
return err;
case 'p':
printpal = 1;
++i;
break;
case 'S':
verbose = 0;
force = 1;
quiet = 2; /* summary */
++i;
break;
case 'q':
verbose = 0;
quiet = 1;
++i;
break;
case 's':
search = 1;
++i;
break;
case 't':
printtext = 1;
++i;
break;
case 'v':
++verbose; /* verbose == 2 means decode IDATs and print filter info */
quiet = 0; /* verbose == 4 means print pixel values, too */
++i;
break;
case 'w':
check_windowbits = 0;
++i;
break;
case 'x':
search = extract = 1;
++i;
break;
default:
fprintf(stderr, "error: unknown option %c\n\n", argv[1][i]);
usage(stderr);
return kCommandLineError;
}
}
if (color) {
brief_error = brief_error_color;
errors_detected = errors_color;
#ifdef USE_ZLIB
pass_color = pass_color_enabled;
color_off = pass_color_enabled[0];
#endif
} else {
brief_error = brief_error_plain;
errors_detected = errors_plain;
#ifdef USE_ZLIB
pass_color = pass_color_disabled;
color_off = pass_color_disabled[0];
#endif
}
if (argc == 1) {
if (isatty(0)) { /* if stdin not redirected, give the user help */
usage(stdout);
} else {
char *fname = "stdin";
if (search)
pngsearch(stdin, fname, extract); /* currently returns void */
else
err = pngcheck(stdin, fname, 0, NULL);
++num_files;
if (err == kWarning)
++num_warnings;
else if (err > kWarning) {
++num_errors;
if (verbose)
printf("%s in %s\n", errors_detected, fname);
else if (quiet < 2)
printf("%s: %s%s%s\n", brief_error,
color? COLOR_YELLOW:"", fname, color? COLOR_NORMAL:"");
}
}
} else {
#ifdef USE_ZLIB
/* make sure we're using the zlib version we were compiled to use */
if (zlib_version[0] != ZLIB_VERSION[0]) {
printf("zlib error: incompatible version (expected %s,"
" using %s): skipping zlib check\n\n", ZLIB_VERSION, zlib_version);
check_zlib = 0;
if (verbose > 1)
verbose = 1;
} else if (strcmp(zlib_version, ZLIB_VERSION) != 0) {
printf("zlib warning: different version (expected %s,"
" using %s)\n\n", ZLIB_VERSION, zlib_version);
}
#endif /* USE_ZLIB */
/* main loop over files listed on command line */
for (i = 1; i < argc; ++i) {
char *fname = argv[i];
err = kOK;
if (strcmp(fname, "-") == 0) {
fname = "stdin";
fp = stdin;
} else if ((fp = fopen(fname, "rb")) == NULL) {
perror(fname);
err = kCriticalError;
}
if (err == kOK) {
if (search)
pngsearch(fp, fname, extract);
else
err = pngcheck(fp, fname, 0, NULL);
if (fp != stdin)
fclose(fp);
}
++num_files;
if (err == kWarning)
++num_warnings;
else if (err > kWarning) {
++num_errors;
if (verbose)
printf("%s in %s\n", errors_detected, fname);
else if (quiet < 2)
printf("%s: %s%s%s\n", brief_error,
color? COLOR_YELLOW:"", fname, color? COLOR_NORMAL:"");
}
}
}
if (num_errors > 0)
err = (num_errors > 127)? 127 : (num_errors < 2)? 2 : num_errors;
else if (num_warnings > 0)
err = 1;
if (!quiet && num_files > 1) {
printf("\n");
if (num_errors > 0)
printf("Errors were detected in %d of the %d files tested.\n",
num_errors, num_files);
if (num_warnings > 0)
printf("Warnings were detected in %d of the %d files tested.\n",
num_warnings, num_files);
if (num_errors + num_warnings < num_files)
printf("No errors were detected in %d of the %d files tested.\n",
num_files - (num_errors + num_warnings), num_files);
}
return err;
}
/* GRR 20061203 */
void usage(FILE *fpMsg)
{
fprintf(fpMsg, "PNGcheck, version %s,\n", VERSION);
fprintf(fpMsg, " by Alexander Lehmann, Andreas Dilger and Greg Roelofs.\n");
#ifdef USE_ZLIB
fprintf(fpMsg, " Compiled with zlib %s; using zlib %s.\n",
ZLIB_VERSION, zlib_version);
#endif
fprintf(fpMsg, "\n"
"Test PNG, JNG or MNG image files for corruption, and print size/type info."
"\n\n"
"Usage: pngcheck [-7cfpqtv] file.{png|jng|mng} [file2.{png|jng|mng} [...]]\n"
" or: ... | pngcheck [-7cfpqstvx]\n"
" or: pngcheck [-7cfpqstvx] file-containing-PNGs...\n"
"\n"
"Options:\n"
" -7 print contents of tEXt chunks, escape chars >=128 (for 7-bit terminals)\n"
" -c colorize output (for ANSI terminals)\n"
" -f force continuation even after major errors\n"
" -p print contents of PLTE, tRNS, hIST, sPLT and PPLT (can be used with -q)\n"
" -q test quietly (output only errors)\n"
" -S test quietly but output a summary\n"
" -s search for PNGs within another file\n"
" -t print contents of tEXt chunks (can be used with -q)\n"
" -v test verbosely (print most chunk data)\n"
#ifdef USE_ZLIB
" -vv test very verbosely (decode & print line filters)\n"
" -w suppress windowBits test (more-stringent compression check)\n"
#endif
" -x search for PNGs within another file and extract them when found\n"
"\n"
"Note: MNG support is more informational than conformance-oriented.\n"
);
fflush(fpMsg);
}
#ifdef USE_ZLIB
# define CRCCOMPL(c) c
# define CRCINIT (0)
# define update_crc crc32
#else /* !USE_ZLIB */
/* use these instead of ~crc and -1, since that doesn't work on machines
* that have 64-bit longs */
# define CRCCOMPL(c) ((c)^0xffffffff)
# define CRCINIT (CRCCOMPL(0))
/* make the table for a fast crc */
void make_crc_table(void)
{
int n;
for (n = 0; n < 256; ++n) {
ulg c;
int k;
c = (ulg)n;
for (k = 0; k < 8; ++k)
c = c & 1 ? 0xedb88320L ^ (c >> 1):c >> 1;
crc_table[n] = c;
}
crc_table_computed = 1;
}
/* update a running crc with the bytes buf[0..len-1]--the crc should be
initialized to all 1's, and the transmitted value is the 1's complement
of the final running crc. */
ulg update_crc(ulg crc, uch *buf, int len)
{
ulg c = crc;
uch *p = buf;
int n = len;
if (!crc_table_computed) {
make_crc_table();
}
if (n > 0) do {
c = crc_table[(c ^ (*p++)) & 0xff] ^ (c >> 8);
} while (--n);
return c;
}
#endif /* ?USE_ZLIB */
ulg getlong(FILE *fp, char *fname, char *where)
{
ulg res = 0;
int j;
for (j = 0; j < 4; ++j) {
int c;
if ((c = fgetc(fp)) == EOF) {
printf("%s EOF while reading %s\n", verbose? ":":fname, where);
set_err(kCriticalError);
return 0;
}
res <<= 8;
res |= c & 0xff;
}
return res;
}
/* output a long when copying an embedded PNG out of a file. */
void putlong(FILE *fpOut, ulg ul)
{
putc(ul >> 24, fpOut);
putc(ul >> 16, fpOut);
putc(ul >> 8, fpOut);
putc(ul, fpOut);
}
/* print out "size" characters in buffer, taking care not to print control
chars other than whitespace, since this may open ways of attack by so-
called ANSI-bombs */
void init_printbuf_state(printbuf_state *prbuf)
{
prbuf->cr = 0;
prbuf->lf = 0;
prbuf->nul = 0;
prbuf->control = 0;
prbuf->esc = 0;
}
/* GRR EBCDIC WARNING */
void print_buffer(printbuf_state *prbuf, uch *buf, int size, int indent)
{
int linewidth = 79, ctg;
const char *term;
if (indent)
printf(" "), linewidth -= 4, term = "\\\n ";
else
term = "\\\n";
ctg = linewidth;
while (size--) {
uch c;
c = *buf++;
if ((c < ' ' && c != '\t' && c != '\n') ||
(sevenbit? c > 127 : (c >= 127 && c < 160))) {
if (ctg < 3) printf("%s", term), ctg = linewidth;
printf("\\%02X", c), ctg -= 3;
}
/*
else if (c == '\\')
printf("\\\\");
*/
else {
if (ctg < 1) printf("%s", term), ctg = linewidth;
putchar(c), ctg -= 1;
}
if (c < 32 || (c >= 127 && c < 160)) {
if (c == '\n') {
prbuf->lf = 1;
if (indent && size > 0)
printf(" ");
ctg = linewidth;
} else if (c == '\r')
prbuf->cr = 1;
else if (c == '\0')
prbuf->nul = 1;
else
prbuf->control = 1;
if (c == 27)
prbuf->esc = 1;
}
}
}
void report_printbuf(printbuf_state *prbuf, char *fname, char *chunkid)
{
if (prbuf->cr) {
if (prbuf->lf) {
printf("%s %s chunk contains both CR and LF as line terminators\n",
verbose? "":fname, chunkid);
set_err(kMinorError);
} else {
printf("%s %s chunk contains only CR as line terminator\n",
verbose? "":fname, chunkid);
set_err(kMinorError);
}
}
if (prbuf->nul) {
printf("%s %s chunk contains null bytes\n", verbose? "":fname, chunkid);
set_err(kMinorError);
}
if (prbuf->control) {
printf("%s %s chunk contains one or more control characters%s\n",
verbose? "":fname, chunkid, prbuf->esc? " including Escape":"");
set_err(kMinorError);
}
}
int keywordlen(uch *buf, int maxsize)
{
int j = 0;
while (j < maxsize && buf[j])
++j;
return j;
}
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)? inv : month[m-1];
}
int ratio(ulg uc, ulg c) /* GRR 19970621: swiped from UnZip 5.31 list.c */
{
ulg denom;
if (uc == 0)
return 0;
if (uc > 2000000L) { /* risk signed overflow if multiply numerator */
denom = uc / 1000L;
return ((uc >= c) ?
(int) ((uc-c + (denom>>1)) / denom) :
-((int) ((c-uc + (denom>>1)) / denom)));
} else { /* ^^^^^^^^ rounding */
denom = uc;
return ((uc >= c) ?