#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <zlib.h>


#define CRC_OFFSET_S 0x21


int main(int argc, char **argv){
	//02 75
//	union{ uint8_t a[4]; uint32_t b;}x;

//	x.a[0] = 198;
//	x.a[1] = 182;
//	x.a[2] = 138;
//	x.a[3] = 0;


//	printf("%u\n", x.b);



	FILE *in = NULL;
	int8_t *read_buffer;
	unsigned long crc = crc32(0L, Z_NULL, 0);
	long filelen;
//	uint32_t i;

        if(argc != 2){
		printf("Error!\n");
		return 0;
	}else{
                //file_data_in  = (char *)malloc( sizeof(char) * strlen(argv[1]) + 1 );
                //(void)strcpy(file_data_in, (const char *)argv[options[1]]);
		 in = fopen( argv[1], "rb" );

printf("%s opened\n", argv[1]);
fseek(in, 0, SEEK_END);
filelen = ftell(in);
rewind(in);
	}
	//read_buffer[1] = '\0';

	//while(feof(in)

//	for(i = 0; i < filelen; i++){
//		fread(&read_buffer, 1, 1, in);
//
//		crc = crc32(crc, (const Bytef *)&read_buffer, 1);
//	}


	read_buffer = (int8_t *)malloc(sizeof(int8_t) * filelen + 1);
	(void)memset((void *)read_buffer, '\0', sizeof(int8_t) * filelen + 1 );
	fread(read_buffer, 1, filelen, in);
	crc = crc32(crc, (const Bytef *)read_buffer, strlen((const char *)read_buffer));

//	while(fgets((char *)read_buffer, 1, in)){
//		if( fgets((char *)read_buffer, 1, in) == NULL)
//			break;
//
//		crc = crc32(crc, (const Bytef *)&read_buffer, 1);
//
//	}

	fclose(in);
	free(read_buffer);
	printf("crc %lu\n", crc);
	printf("%lu bytes read\n", filelen);

    return 0;
}












//     while (read_buffer(buffer, length) != EOF) {
//       crc = crc32(crc, buffer, length);
//     }
//     if (crc != original_crc) error();

