fukasawa e60969
/* checksum-icc.c
fukasawa e60969
 *
fukasawa e60969
 * Copyright (c) 2013 John Cunningham Bowler
fukasawa e60969
 *
fukasawa e60969
 * Last changed in libpng 1.6.0 [February 14, 2013]
fukasawa e60969
 *
fukasawa e60969
 * This code is released under the libpng license.
fukasawa e60969
 * For conditions of distribution and use, see the disclaimer
fukasawa e60969
 * and license in png.h
fukasawa e60969
 *
fukasawa e60969
 * Generate crc32 and adler32 checksums of the given input files, used to
fukasawa e60969
 * generate check-codes for use when matching ICC profiles within libpng.
fukasawa e60969
 */
fukasawa e60969
#include <stdio.h></stdio.h>
fukasawa e60969
fukasawa e60969
#include <zlib.h></zlib.h>
fukasawa e60969
fukasawa e60969
static int
fukasawa e60969
read_one_file(FILE *ip, const char *name)
fukasawa e60969
{
fukasawa e60969
   uLong length = 0;
fukasawa e60969
   uLong a32 = adler32(0, NULL, 0);
fukasawa e60969
   uLong c32 = crc32(0, NULL, 0);
fukasawa e60969
   Byte header[132];
fukasawa e60969
fukasawa e60969
   for (;;)
fukasawa e60969
   {
fukasawa e60969
      int ch = getc(ip);
fukasawa e60969
      Byte b;
fukasawa e60969
fukasawa e60969
      if (ch == EOF) break;
fukasawa e60969
fukasawa e60969
      b = (Byte)ch;
fukasawa e60969
fukasawa e60969
      if (length < sizeof header)
fukasawa e60969
         header[length] = b;
fukasawa e60969
fukasawa e60969
      ++length;
fukasawa e60969
      a32 = adler32(a32, &b, 1);
fukasawa e60969
      c32 = crc32(c32, &b, 1);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   if (ferror(ip))
fukasawa e60969
      return 0;
fukasawa e60969
fukasawa e60969
   /* Success */
fukasawa e60969
   printf("PNG_ICC_CHECKSUM(0x%8.8lx, 0x%8.8lx,\n   PNG_MD5("
fukasawa e60969
      "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,"
fukasawa e60969
      " 0x%2.2x%2.2x%2.2x%2.2x), %d,\n"
fukasawa e60969
      "   \"%4.4d/%2.2d/%2.2d %2.2d:%2.2d:%2.2d\", %lu, \"%s\")\n",
fukasawa e60969
      (unsigned long)a32, (unsigned long)c32,
fukasawa e60969
      header[84], header[85], header[86], header[87],
fukasawa e60969
      header[88], header[89], header[90], header[91],
fukasawa e60969
      header[92], header[93], header[94], header[95],
fukasawa e60969
      header[96], header[97], header[98], header[99],
fukasawa e60969
#     define u16(x) (header[x] * 256 + header[x+1])
fukasawa e60969
#     define u32(x) (u16(x) * 65536 + u16(x+2))
fukasawa e60969
      u32(64), u16(24), u16(26), u16(28), u16(30), u16(32), u16(34),
fukasawa e60969
      (unsigned long)length, name);
fukasawa e60969
fukasawa e60969
   return 1;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
int main(int argc, char **argv)
fukasawa e60969
{
fukasawa e60969
   int err = 0;
fukasawa e60969
fukasawa e60969
   printf("/* adler32, crc32, MD5[16], intent, date, length, file-name */\n");
fukasawa e60969
fukasawa e60969
   if (argc > 1)
fukasawa e60969
   {
fukasawa e60969
      int i;
fukasawa e60969
fukasawa e60969
      for (i=1; i
fukasawa e60969
      {
fukasawa e60969
         FILE *ip = fopen(argv[i], "rb");
fukasawa e60969
fukasawa e60969
         if (ip == NULL || !read_one_file(ip, argv[i]))
fukasawa e60969
         {
fukasawa e60969
            err = 1;
fukasawa e60969
            perror(argv[i]);
fukasawa e60969
            fprintf(stderr, "%s: read error\n", argv[i]);
fukasawa e60969
            printf("/* ERROR: %s */\n", argv[i]);
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         (void)fclose(ip);
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   else
fukasawa e60969
   {
fukasawa e60969
      if (!read_one_file(stdin, "-"))
fukasawa e60969
      {
fukasawa e60969
         err = 1;
fukasawa e60969
         perror("stdin");
fukasawa e60969
         fprintf(stderr, "stdin: read error\n");
fukasawa e60969
         printf("/* ERROR: stdin */\n");
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   return err;
fukasawa e60969
}