fakepng.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /* Fake a PNG - just write it out directly.
  2. *
  3. * COPYRIGHT: Written by John Cunningham Bowler, 2014.
  4. * To the extent possible under law, the author has waived all copyright and
  5. * related or neighboring rights to this work. This work is published from:
  6. * United States.
  7. *
  8. */
  9. #include <stdio.h>
  10. #include <zlib.h> /* for crc32 */
  11. void
  12. put_uLong(uLong val)
  13. {
  14. putchar(val >> 24);
  15. putchar(val >> 16);
  16. putchar(val >> 8);
  17. putchar(val >> 0);
  18. }
  19. void
  20. put_chunk(const unsigned char *chunk, uInt length)
  21. {
  22. uLong crc;
  23. put_uLong(length-4); /* Exclude the tag */
  24. fwrite(chunk, length, 1, stdout);
  25. crc = crc32(0, Z_NULL, 0);
  26. put_uLong(crc32(crc, chunk, length));
  27. }
  28. const unsigned char signature[] =
  29. {
  30. 137, 80, 78, 71, 13, 10, 26, 10
  31. };
  32. const unsigned char IHDR[] =
  33. {
  34. 73, 72, 68, 82, /* IHDR */
  35. 0, 0, 0, 1, /* width */
  36. 0, 0, 0, 1, /* height */
  37. 1, /* bit depth */
  38. 0, /* color type: greyscale */
  39. 0, /* compression method */
  40. 0, /* filter method */
  41. 0 /* interlace method: none */
  42. };
  43. const unsigned char unknown[] =
  44. {
  45. 'u', 'n', 'K', 'n' /* "unKn" - private safe to copy */
  46. };
  47. int
  48. main(void)
  49. {
  50. fwrite(signature, sizeof signature, 1, stdout);
  51. put_chunk(IHDR, sizeof IHDR);
  52. for (;;)
  53. put_chunk(unknown, sizeof unknown);
  54. }