Skip to content

Instantly share code, notes, and snippets.

@ynaoto
Created December 9, 2013 05:31
Show Gist options
  • Select an option

  • Save ynaoto/7867727 to your computer and use it in GitHub Desktop.

Select an option

Save ynaoto/7867727 to your computer and use it in GitHub Desktop.
PNGファイルの構造をざざっと見る
#include <stdio.h>
#include <ctype.h>
typedef unsigned char BYTE;
typedef int INT4;
int main()
{
BYTE pngHdr[8];
BYTE buf4[4];
int addr = 0;
int i;
fread(pngHdr, 8, 1, stdin);
printf("PNG HDR: ");
for (i = 0; i < 8; i++) {
printf("%02x ", pngHdr[i]);
}
printf("\n");
addr += 8;
while (fread(buf4, 4, 1, stdin)) {
INT4 chunkSize = 0;
for (i = 0; i < 4; i++) {
chunkSize |= buf4[i] << ((3 - i) * 8);
}
printf("%08o chunkSize = %d\n", addr, chunkSize);
addr += 4;
fread(buf4, 4, 1, stdin);
printf("chunk ");
for (i = 0; i < 4; i++) {
printf("[%c]", buf4[i]);
}
printf("\n");
addr += 4;
for (i = 0; i < chunkSize; i++) {
int c = getchar();
/*
printf("%02x", c);
if (isprint(c)) {
printf("(%c)", c);
}
printf(" ");
*/
printf("%c", isprint(c) ? c : '.');
}
printf("\n");
addr += chunkSize;
fread(buf4, 4, 1, stdin);
printf("crc: ");
for (i = 0; i < 4; i++) {
printf("%02x ", buf4[i]);
}
printf("\n");
addr += 4;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment