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


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};

const char *byte_to_binary
(
    int x
)
{
    static char b[9];
    b[0] = '\0';

    int z;
    for (z = 128; z > 0; z >>= 1)
    {
        strcat(b, ((x & z) == z) ? "1" : "0");
    }

    return b;
}


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;
}