-
-
Save severak/8b5c92c198eda15f4cdff1884881e3db to your computer and use it in GitHub Desktop.
Quick tool to list and dump data chunks from a PNG file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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
TODO - port to Go