checksum-icc.c 2.4 KB

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