Skip to content

Instantly share code, notes, and snippets.

@eXeC64
Created July 25, 2015 21:22
Show Gist options
  • Save eXeC64/b064f992f16166901b5d to your computer and use it in GitHub Desktop.
Save eXeC64/b064f992f16166901b5d to your computer and use it in GitHub Desktop.
Quick tool to list and dump data chunks from a PNG file
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <endian.h>
int main(int argc, char** argv)
{
if(argc < 2)
return 1;
FILE* f = fopen(argv[1], "rb");
//Move to start of first chunk
fseek(f, 8, SEEK_SET);
int index = 0;
int to_dump = -1;
if(argc == 3)
to_dump = atoi(argv[2]);
while(1) {
struct {
uint32_t len;
char type[5];
} chunk;
fread(&chunk, 4, 2, f);
chunk.len = be32toh(chunk.len);
chunk.type[4] = 0;
// Listing chunks
if(to_dump < 0)
printf("CHUNK[%d] at 0x%x type %s, length %x\n", index, ftell(f), chunk.type, chunk.len);
if(to_dump == index) {
//Dump the given chunk's data
char* buf = malloc(chunk.len);
fread(buf, chunk.len, 1, f);
fwrite(buf, chunk.len, 1, stdout);
return 0;
}
if(strcmp(chunk.type, "IEND") == 0)
break;
fseek(f, chunk.len + 4 /* checksum */, SEEK_CUR);
index += 1;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment