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

/*
	dynamic bit compressor

	initial header:
		int32 version
		int32 error check method
			no error check
			per set crc
			md5sum end result
			other gcrypt options
		int32 set size (bytes uncompressed)

	setX header:
		char bits (bits per member)
	setX data:
		optional crc/error
		members

1 byte
	256 combinations
8 - 64 bits


1

2 - 16

3

4

5

6

7

8



baseline offset
find median of set values
find max bits needed for offset value
store base, bit size, array of offsets


*/

void Usage(void){
	printf("dbc [-v/-h] [-c] [-d] [-i] [filenames ... ]\n");

}

//const uint8_t byte_limits[8] = {2, 4, 8, 16, 32, 64, 128, 256};
const uint16_t byte_limits[8] = {3, 5, 9, 17, 33, 65, 129, 257};

int main(int argc, char **argv){
	FILE *fp1;
	uint8_t eight_b[8];
	uint8_t eight_t[8];
	size_t tbr = 0;
	double tbs = 0;
	uint8_t sbr = 0;
	uint8_t n1 = 0;
	uint8_t n2 = 0;

	fp1 = fopen(argv[1], "rb");
	if(!fp1){
		printf("Error opening file: %s\n", argv[1]);
		return 1;
	}
	while((sbr = fread((void *)eight_b, 1, 8, fp1)) > 0){
		
		tbr+=sbr;
		for(n1=0; n1<sbr; n1++){
			// find highest out of all 8
			for(n2=0; n2<8; n2++){
				if((uint16_t)eight_b[n1] < byte_limits[n2]){
					eight_t[n1] = n2;
					break;
				}
			}
		}
		n1 = 0;
		for(n2=0; n2<sbr; n2++){
			if(eight_t[n2] > n1)
				n1 = eight_t[n2];
		}
		//n1 == biggest 
		tbs += ((double)n1+1)*sbr;

		if(sbr<8)
			break;
	}
	
	

	fclose(fp1);

	printf("\tread %zu bytes\n", tbr);
	printf("\tpotential %.0lf bytes\n", tbs/8);

	return 0;
}