65 lines
1.8 KiB
C
65 lines
1.8 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include "crc_util.h"
|
|
|
|
const long png_signature[8] = { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a };
|
|
|
|
int check_header_length(unsigned char *addr, long offset) {
|
|
unsigned int res = 0;
|
|
for( int i = 0; i < 4; i++ ) {
|
|
res |= addr[offset+i];
|
|
if (i < 3) {
|
|
res <<= 8;
|
|
}
|
|
}
|
|
return res;
|
|
}
|
|
|
|
int check_file_header(char *addr) {
|
|
int signature_match = 0;
|
|
for( int i = 0; i < 8; i++ ) {
|
|
if (addr[i] != png_signature[i]) {
|
|
signature_match = 1;
|
|
}
|
|
}
|
|
printf("Sig Match: %d\n", signature_match);
|
|
return signature_match;
|
|
|
|
}
|
|
|
|
int create_cc_file(unsigned char *addr, unsigned long file_length) {
|
|
FILE *fp;
|
|
fp = fopen("png2.png", "w");
|
|
|
|
if(fp == NULL) {
|
|
return EXIT_FAILURE;
|
|
}
|
|
|
|
for(int i = 0; i < file_length; i++){
|
|
fputc(addr[i], fp);
|
|
}
|
|
fclose(fp);
|
|
return 0;
|
|
}
|
|
|
|
unsigned char* file_to_char_array(FILE *in_file, size_t* size) {
|
|
unsigned int c;
|
|
unsigned long file_data_cap = 8;
|
|
unsigned char* file_data = calloc(file_data_cap, sizeof(unsigned char));
|
|
|
|
for(size_t i = 0;(c = fgetc(in_file)) != EOF; i++) {
|
|
if(i == file_data_cap) {
|
|
file_data_cap *= 2;
|
|
file_data = reallocarray(file_data, file_data_cap, sizeof(unsigned char));
|
|
if(file_data == NULL) {
|
|
perror("FAILED ARRAY RESIZE");
|
|
return NULL;
|
|
}
|
|
}
|
|
file_data[i] = c;
|
|
*size += 1;
|
|
}
|
|
return file_data;
|
|
}
|
|
|