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
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <assert.h>
#include "bitwriter.h"
int main(int argc, char *argv[])
{
if (argc != 2)
{
printf("Usage: %s <input file>\n", argv[0]);
exit(1);
}
char *input_file_name = argv[1];
char *output_file_name = malloc(sizeof(char) *
(strlen(input_file_name) + strlen(".deflate") + 1));
sprintf(output_file_name, "%s.deflate", input_file_name);
FILE *input_file = fopen(input_file_name, "r");
FILE *output_file = fopen(output_file_name, "w");
free(output_file_name);
BitWriter *bitWriter = BitWriter_init(output_file);
BitWriter_write_bit(bitWriter, 1); /* BFINAL */
BitWriter_write_bit(bitWriter, 1); /* BTYPE */
BitWriter_write_bit(bitWriter, 0);
uint8_t c;
while (fread(&c, 1, 1, input_file))
{
BitWriter_write_alpha(bitWriter, c);
}
for (int i = 0; i < 7; i++)
BitWriter_write_bit(bitWriter, 0);
BitWriter_flush(bitWriter);
BitWriter_free(bitWriter);
fclose(output_file);
fclose(input_file);
return 0;
}