-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhencode.c
177 lines (156 loc) · 3.92 KB
/
hencode.c
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "ordered_list.h"
#include "huffman.h"
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdint.h>
#include <fcntl.h>
#include <stdio.h>
#define SIZE_ASCII 256
#define BUFFER_SIZE 4096
#define ONE_BYTE 1
#define FOUR_BYTE 4
#define EIGHT_BITS 8
#define BIT_INDEX 7
#define SHIFT3 24
#define SHIFT2 16
#define SHIFT1 8
/*Function that develops the header from the
* frequency list*/
void writeHeader(int fd, int *freqlist)
{
uint8_t count = 0;
uint8_t character;
uint32_t freq;
uint8_t freqBytes[4];
int i;
for(i = 0; i < SIZE_ASCII; i++)
{
if(freqlist[i] != 0)
{
count++;
}
}
count--;
write(fd, &count, ONE_BYTE);
for(i = 0; i < SIZE_ASCII; i++)
{
if(freqlist[i] != 0)
{
character = (uint8_t)i;
freq = (uint32_t)freqlist[i];
freqBytes[0] = (uint8_t)((freq >> SHIFT3) & 0xFF);
freqBytes[1] = (uint8_t)((freq >> SHIFT2) & 0xFF);
freqBytes[2] = (uint8_t)((freq >> SHIFT1) & 0xFF);
freqBytes[3] = (uint8_t)(freq & 0xFF);
write(fd, &character, ONE_BYTE);
write(fd, freqBytes, FOUR_BYTE);
}
}
}
/* Actually encodes the file.
* Iterates through the file in chunks of 4096 bytes
* and writes out the codes*/
void encode(int infd, int outfd, int *freqlist, char **codes)
{
unsigned char inbuf[BUFFER_SIZE];
unsigned char outbuf[BUFFER_SIZE];
int inbufsize;
int outbufsize = 0;
int remainingbits = 0;
uint8_t currbyte = 0;
int i, j;
char *code;
char bit;
while ((inbufsize = read(infd, inbuf, BUFFER_SIZE)) > 0)
{
for(i = 0; i < inbufsize; i++)
{
code = codes[(int)inbuf[i]];
for(j = 0; j < strlen(code); j++)
{
bit = code[j];
if (bit == '1')
{
currbyte |= (1 << (BIT_INDEX - remainingbits));
}
remainingbits++;
if (remainingbits == EIGHT_BITS)
{
outbuf[outbufsize++] = currbyte;
currbyte = 0;
remainingbits = 0;
if (outbufsize == BUFFER_SIZE)
{
write(outfd, outbuf, outbufsize);
outbufsize = 0;
}
}
}
}
}
/*Extra checks were needed to make sure that all the bits
* were written to the file*/
if (remainingbits > 0)
{
outbuf[outbufsize++] = currbyte;
}
if (outbufsize > 0)
{
write(outfd, outbuf, outbufsize);
}
}
/*Executes the huffman encoding files, controls
* the input and output through command line arguments
* and stdin/stdout */
int main(int argc, char *argv[])
{
int input_fd;
int output_fd;
int *freq_list;
HuffmanNode *head;
char **codeTable;
LinkedList *l;
input_fd = open(argv[1], O_RDONLY);
if(input_fd == -1)
{
perror("FILE NOT FOUND");
return 0;
}
if(argc == 3)
{
output_fd = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0644);
if(output_fd == -1)
{
perror("FILE NOT FOUND");
return 0;
}
}
else
{
output_fd = STDOUT_FILENO;
}
l = createList();
freq_list = readFromFile(input_fd);
lseek(input_fd, 0, SEEK_SET);
head = createHuffTree(l, freq_list);
if(l -> size == 0)
{
free(freq_list);
free(l);
close(input_fd);
close(output_fd);
return 0;
}
codeTable = createCodeArray(head);
writeHeader(output_fd, freq_list);
encode(input_fd, output_fd, freq_list, codeTable);
free(freq_list);
freeCodeTable(codeTable);
freeEverythingHuffman(head);
free(l -> head);
free(l);
close(output_fd);
close(input_fd);
return 0;
}