aboutsummaryrefslogtreecommitdiff
path: root/decompress.c
blob: dae888dfd11b636dc0c0fbb98ebc30d52c033f8c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*

Not implemented yet

Currently using zlib's decompress (sane)





uint8_t getCM(uint8_t b){
	uint8_t r = b<<4;
	return r>>4;
}

uint8_t getCINFO(uint8_t b){
	return b>>4;
}

bool validateCMFFLG(uint8_t CMF, uint8_t FLG){
	uint16_t CMFFLGcheck = (CMF<<8) + FLG;
	return getCM(CMF) == 8 && CMFFLGcheck%31 == 0;
}

typedef bool bit;


bool getBit(int n, int bit){
	return n & (1 << bit);
}

void printBinary(uint8_t n){
    for(int i = 7; i >= 0; --i)
        printf("%d", getBit(n, i));
}

uint8_t reverseBits(uint8_t n){
	uint8_t r = 0;
	for(int i = 0; i < 8; ++i)
		r |= ((bool)(n&(1<<(7-i))))<<i;
	return r;
}

uint8_t read8(bit *data, uint32_t *i){
	// This function is used for decompression
	// The first bit is the LSB
	uint8_t r = 0;
	for(int j = 0; j < 8; ++j){
		r += data[(*i)+j]<<j;
	}
	(*i) += 8;
	return r;
}

uint16_t read16(bit *data, uint32_t *i){
	// This function is used for decompression
	// The first bit is the LSB
	uint16_t r = 0;
	for(int j = 0; j < 16; ++j){
		r += data[(*i)+j]<<j;
	}
	(*i) += 16;
	return r;
}

typedef enum compressionType{
	NONE, FIXED, DYNAMIC, ERROR,
} compressionType;

bit *decompressData(bit *data, uint32_t len){
	// we have to decompress with zlib
				// in zlib the first bit is the least significant
				// (b0 b1 b2 b3)
				// ftp://ftp.isi.edu/in-notes/rfc1950.txt
				// https://www.ietf.org/rfc/rfc1950.txt
				// https://stackoverflow.com/questions/42934720/understanding-the-zlib-header-cmf-cm-cinfo-flg-fdict-dictid-flevel-rfc
				
				/*uint8_t CMF = data[0], FLG = data[1], FDICT, FLEVEL;
				if(!validateCMFFLG(CMF, FLG)){
					fprintf(stderr, "Invalid CMF+FLG check\n");
					fclose(fp);
					return 0;
				}
				uint32_t LZ77size = (1<<getCINFO(CMF))<<8;
				FDICT = getBit(FLG, 5);
				FLEVEL = getBit(FLG, 6) + (getBit(FLG, 7)<<1);
				if(FDICT){
					fprintf(stderr, "I dont know how to deal with DICT\n");
					fclose(fp);
					return 0;
				}
				
				// Deflate data
				// https://www.ietf.org/rfc/rfc1951.txt
				/*
				Data elements are packed into bytes in order of
					increasing bit number within the byte, i.e., starting
					with the least-significant bit of the byte.
				Data elements other than Huffman codes are packed
					starting with the least-significant bit of the data
					element.
				Huffman codes are packed starting with the most-
					significant bit of the code.
				*
				/

				// Adler32
	bit *out = malloc(len);
	bit BFINAL;
	uint8_t BTYPE;
	uint32_t ii = 0, oi = 0;
	while(!BFINAL){
		BFINAL = data[ii++];
		if(BFINAL){
			// TODO: exit
		}
		BTYPE = (data[ii]<<1) + data[ii+1];
		ii += 2;
		if(BTYPE == NONE){
			ii += 8-(ii%8);
		}
		uint16_t LEN, NLEN;
		LEN = read16(data, &ii);
		NLEN = read16(data, &ii);

		for(uint16_t j = 0; j < LEN; ++j){

		}
	}
}
*/