xref: /aosp_15_r20/external/libpng/contrib/tools/checksum-icc.c (revision a67afe4df73cf47866eedc69947994b8ff839aba)
1 /* checksum-icc.c
2  *
3  * Copyright (c) 2013 John Cunningham Bowler
4  *
5  * This code is released under the libpng license.
6  * For conditions of distribution and use, see the disclaimer
7  * and license in png.h
8  *
9  * Generate crc32 and adler32 checksums of the given input files, used to
10  * generate check-codes for use when matching ICC profiles within libpng.
11  */
12 
13 #include <stdio.h>
14 #include <zlib.h>
15 
16 static int
read_one_file(FILE * ip,const char * name)17 read_one_file(FILE *ip, const char *name)
18 {
19    uLong length = 0;
20    uLong a32 = adler32(0, NULL, 0);
21    uLong c32 = crc32(0, NULL, 0);
22    Byte header[132];
23 
24    for (;;)
25    {
26       int ch = getc(ip);
27       Byte b;
28 
29       if (ch == EOF) break;
30 
31       b = (Byte)ch;
32 
33       if (length < sizeof header)
34          header[length] = b;
35 
36       ++length;
37       a32 = adler32(a32, &b, 1);
38       c32 = crc32(c32, &b, 1);
39    }
40 
41    if (ferror(ip))
42       return 0;
43 
44    /* Success */
45    printf("PNG_ICC_CHECKSUM(0x%8.8lx, 0x%8.8lx,\n   PNG_MD5("
46       "0x%2.2x%2.2x%2.2x%2.2x, 0x%2.2x%2.2x%2.2x%2.2x, 0x%2.2x%2.2x%2.2x%2.2x,"
47       " 0x%2.2x%2.2x%2.2x%2.2x), %d,\n"
48       "   \"%4.4d/%2.2d/%2.2d %2.2d:%2.2d:%2.2d\", %lu, \"%s\")\n",
49       (unsigned long)a32, (unsigned long)c32,
50       header[84], header[85], header[86], header[87],
51       header[88], header[89], header[90], header[91],
52       header[92], header[93], header[94], header[95],
53       header[96], header[97], header[98], header[99],
54 #     define u16(x) (header[x] * 256 + header[x+1])
55 #     define u32(x) (u16(x) * 65536 + u16(x+2))
56       u32(64), u16(24), u16(26), u16(28), u16(30), u16(32), u16(34),
57       (unsigned long)length, name);
58 
59    return 1;
60 }
61 
main(int argc,char ** argv)62 int main(int argc, char **argv)
63 {
64    int err = 0;
65 
66    printf("/* adler32, crc32, MD5[16], intent, date, length, file-name */\n");
67 
68    if (argc > 1)
69    {
70       int i;
71 
72       for (i=1; i<argc; ++i)
73       {
74          FILE *ip = fopen(argv[i], "rb");
75 
76          if (ip == NULL || !read_one_file(ip, argv[i]))
77          {
78             err = 1;
79             perror(argv[i]);
80             fprintf(stderr, "%s: read error\n", argv[i]);
81             printf("/* ERROR: %s */\n", argv[i]);
82          }
83 
84          (void)fclose(ip);
85       }
86    }
87 
88    else
89    {
90       if (!read_one_file(stdin, "-"))
91       {
92          err = 1;
93          perror("stdin");
94          fprintf(stderr, "stdin: read error\n");
95          printf("/* ERROR: stdin */\n");
96       }
97    }
98 
99    return err;
100 }
101