Initial PNG CC Implementation

Signed-off-by: Pin <wf6DJd8a3xSSCZbn@protonmail.com>
This commit is contained in:
Pin
2021-09-21 01:15:03 -04:00
parent fafe5a4520
commit c86b56286c
9 changed files with 668 additions and 0 deletions

132
src/compress_util.c Normal file
View File

@@ -0,0 +1,132 @@
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <errno.h>
#include "compress_util.h"
#define CHUNK 1024
/*
* Built from exmaple provided in: https://zlib.net/zlib_how.html
*/
void zlib_decompress_data(unsigned char *data_chunk, size_t file_length, unsigned char **buff, size_t *sz) {
int ret;
unsigned int have;
z_stream strm;
// Adding one to zero here
unsigned char out[CHUNK];
unsigned char in[CHUNK];
errno=0;
FILE *data_stream = fmemopen(data_chunk, file_length, "r");
FILE *of = open_memstream((char**)buff, sz);
if(data_stream == NULL) {
perror("F MEM OPEN");
}
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.avail_in = 0;
strm.next_in = Z_NULL;
ret = inflateInit(&strm);
if(ret != Z_OK) {
return;
}
do {
strm.avail_in = fread(in, 1, CHUNK, data_stream);
if(ferror(data_stream)) {
(void)inflateEnd(&strm);
return;
}
if(strm.avail_in == 0) {
break;
}
strm.next_in = in;
do {
strm.avail_out = CHUNK;
strm.next_out = out;
ret = inflate(&strm, Z_NO_FLUSH);
assert(ret != Z_STREAM_ERROR);
switch(ret) {
case Z_NEED_DICT:
ret = Z_DATA_ERROR;
case Z_DATA_ERROR:
case Z_MEM_ERROR:
(void)inflateEnd(&strm);
printf("Error: %d\n", ret);
printf("MSG: %s\n", (char*)strm.msg);
return;
}
have = CHUNK - strm.avail_out;
fwrite(out, 1, have, of);
} while(strm.avail_out == 0);
} while(ret != Z_STREAM_END);
(void)inflateEnd(&strm);
fclose(data_stream);
fclose(of);
}
/*
* Built from exmaple provided in: https://zlib.net/zlib_how.html
*/
void zlib_compress_data(unsigned char *data_chunk, size_t file_length, unsigned char **buff, size_t *sz) {
int ret, flush;
unsigned int have;
z_stream strm;
unsigned char in[CHUNK];
unsigned char out[CHUNK];
int level = 9;
int method = Z_DEFLATED;
int windowBits = 10;
int memLevel = 9;
//int strategy = Z_DEFAULT_STRATEGY;
int strategy = Z_FILTERED;
FILE *data_stream = fmemopen(data_chunk, file_length, "r");
FILE *out_data_stream = NULL;
out_data_stream = open_memstream((char**)buff, sz);
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
ret = deflateInit2(&strm, level, method, windowBits, memLevel, strategy);
if (ret != Z_OK) {
return;
}
do {
strm.avail_in = fread(in, 1, CHUNK, data_stream);
if (ferror(data_stream)) {
(void)deflateEnd(&strm);
return;
}
flush = feof(data_stream) ? Z_FINISH : Z_NO_FLUSH;
strm.next_in = in;
do {
strm.avail_out = CHUNK;
strm.next_out = out;
ret = deflate(&strm, flush);
assert(ret != Z_STREAM_ERROR);
have = CHUNK - strm.avail_out;
if(fwrite(out, 1, have, out_data_stream) != have || ferror(out_data_stream)) {
(void)deflateEnd(&strm);
return;
}
} while(strm.avail_out == 0);
assert(strm.avail_in == 0);
} while(flush != Z_FINISH);
assert(ret == Z_STREAM_END);
fclose(data_stream);
fclose(out_data_stream);
(void)deflateEnd(&strm);
}

92
src/crc_util.c Normal file
View File

@@ -0,0 +1,92 @@
#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;
}
void populate_idat_png(unsigned char *addr, struct PNG_IDAT_FILE_STRUCT *png_data, unsigned long offset) {
unsigned long cur_idat_length = 0;
for(int i = 0; i < 4; i++){
png_data->idat_length[i] = addr[i+offset];
}
for(int i = 0; i < 4; i++){
cur_idat_length += (png_data->idat_length[i] << (24-(8*i)));
}
for(int i = 0; i < 4; i++){
png_data->idat_header[i] = addr[i+offset+4];
}
}
void populate_start_png(unsigned char *addr, struct PNG_START_FILE_STRUCT *png_data) {
union{
unsigned char *data;
struct PNG_START_FILE_STRUCT *png_data;
}png;
png.png_data = png_data;
for(int i = 0; i < sizeof(struct PNG_START_FILE_STRUCT); i++) {
png.data[i] = addr[i];
}
}
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;
}