Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // by petter wahlman, http://www.twitter.com/badeip
- // display and verify the crc of PNG chunk data
- // hex.h - available here: http://pastebin.com/yj3xsZLW
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <string.h>
- #include <sys/mman.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <arpa/inet.h>
- #include "hex.h"
- unsigned long crc_table[256];
- int crc_table_computed = 0;
- void make_crc_table(void)
- {
- unsigned long c;
- int n, k;
- for (n = 0; n < 256; n++) {
- c = (unsigned long) n;
- for (k = 0; k < 8; k++) {
- if (c & 1)
- c = 0xedb88320L ^ (c >> 1);
- else
- c = c >> 1;
- }
- crc_table[n] = c;
- }
- crc_table_computed = 1;
- }
- unsigned long update_crc(unsigned long crc, unsigned char *buf, int len)
- {
- unsigned long c = crc;
- int n;
- if (!crc_table_computed)
- make_crc_table();
- for (n = 0; n < len; n++) {
- c = crc_table[(c ^ buf[n]) & 0xff] ^ (c >> 8);
- }
- return c;
- }
- unsigned long crc(unsigned char *buf, int len)
- {
- return update_crc(0xffffffffL, buf, len) ^ 0xffffffffL;
- }
- int print_sections(char *mem, size_t size)
- {
- char *ptr = mem;
- int i;
- i = 0;
- if ((*(unsigned int *)ptr != 0x474e5089) ||
- (*(unsigned int *)(ptr + sizeof(int)) != 0x0a1a0a0d)) {
- fprintf(stderr, "error, not a valid png-image\n");
- return 1;
- }
- i += 8;
- do {
- unsigned int section_len;
- unsigned int org_crc, calc_crc;
- printf("file size: 0x%08zx\n", size);
- section_len = ntohl(*(unsigned int *)&ptr[i]);
- printf("section offset: 0x%08x\n", i);
- printf("section length: 0x%08x\n", section_len);
- i += 4;
- printf("section name: %c%c%c%c\n",
- isalpha(*(ptr + i + 0)) ? *(ptr + i + 0) : '?',
- isalpha(*(ptr + i + 1)) ? *(ptr + i + 1) : '?',
- isalpha(*(ptr + i + 2)) ? *(ptr + i + 2) : '?',
- isalpha(*(ptr + i + 3)) ? *(ptr + i + 3) : '?');
- i += 4;
- calc_crc = ntohl(crc((unsigned char *)&mem[i - 4], section_len + 4));
- printf("calculated crc: 0x%08x\n", calc_crc);
- org_crc = *(unsigned int *)&ptr[i + section_len];
- printf("crc: 0x%08x\n", org_crc);
- if (org_crc != calc_crc) {
- fprintf(stderr, "error, incorrect CRC\n");
- return 1;
- }
- printf("data:\n");
- print_hex(mem + i, section_len, 0);
- i += section_len;
- i += 4; // skip past crc (printed above)
- printf("\n");
- } while(i < size);
- return 0;
- }
- int main(int argc, char **argv)
- {
- int fd;
- char *map;
- char *png;
- struct stat st;
- int i;
- map = NULL;
- if (argc < 2) {
- argc = 2;
- argv[1] = "challenge.png";
- }
- for (i = 1; i < argc; i++) {
- png = argv[i];
- printf("filename: %s\n", png);
- fd = open(png, O_RDONLY);
- if (-1 == fd) {
- perror(png);
- return 1;
- }
- fstat(fd, &st);
- map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
- close(fd);
- print_sections(map, st.st_size);
- }
- if (isatty(fileno(stdin)))
- munmap(map, st.st_size);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement