-
Notifications
You must be signed in to change notification settings - Fork 0
/
lzw_decode.c
165 lines (111 loc) · 3.13 KB
/
lzw_decode.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <unistd.h>
#define MAX_STRING_LENGTH 256
#define VAL_TYPE short int
typedef struct entry_tag {
unsigned char *seq;
VAL_TYPE val;
int strLen;
struct entry_tag *fwd;
} entry;
entry *head = NULL;
entry *tail = NULL;
void print_list() {
entry *rover = head;
do {
printf("%d : ", rover->val);
for(int i = 0; i < rover->strLen; i++)
printf("%x", rover->seq[i]);
printf("\n");
rover = rover->fwd;
} while(rover != NULL);
}
void print_string(unsigned char *str, int strLen) {
for(int i = 0; i < strLen; i++)
printf("%x", str[i]);
printf("\n");
}
// NULL = Not Found
entry *lookup_by_val(VAL_TYPE val) {
if(head == NULL) return NULL;
entry *rover = head;
do {
if(rover->val == val) return rover;
rover = rover->fwd;
} while(rover != NULL);
return NULL;
}
void add_entry(unsigned char *seq, int strLen) {
entry *newEntry = calloc(sizeof(entry), 1);
newEntry->seq = seq;
newEntry->fwd = NULL;
newEntry->strLen = strLen;
if(head == NULL) {
newEntry->val = 0;
head = newEntry;
}
if(tail == NULL)
tail = newEntry;
else {
newEntry->val = tail->val + 1;
tail->fwd = newEntry;
tail = newEntry;
}
}
int main(int argc, char **argv) {
assert(argc == 2);
FILE *fpIn = NULL;
FILE *fpOut = NULL;
int strLen = strlen(argv[1]);
char rleFileTag[MAX_STRING_LENGTH];
strcpy(rleFileTag, argv[1] + strLen - 4);
if(strcmp(rleFileTag, ".lzw") != 0) {
printf("Invalid File Type. Must be '.lzw'\n");
exit(1);
}
char outputFile[MAX_STRING_LENGTH];
strncpy(outputFile, argv[1], strLen - 4);
fpIn = fopen(argv[1], "rb");
fpOut = fopen(outputFile, "wb");
assert(fpIn != NULL);
assert(fpOut != NULL);
// Build Initial Single Byte Table (0 - 255)
for(unsigned int i = 0; i < 256; i++) {
unsigned char *temp = calloc(sizeof(*temp), 1);
memcpy(temp, (unsigned char *)&i, 1);
add_entry(temp, 1);
}
VAL_TYPE current = -1;
VAL_TYPE previous = -1;
entry *currentEntry = NULL;
entry *previousEntry = NULL;
fread(¤t, sizeof(current), 1, fpIn);
currentEntry = lookup_by_val(current);
fwrite(currentEntry->seq, sizeof(*currentEntry->seq), currentEntry->strLen, fpOut);
previous = current;
while(fread(¤t, sizeof(current), 1, fpIn)) {
currentEntry = lookup_by_val(current);
if(currentEntry == NULL) {
previousEntry = lookup_by_val(previous);
unsigned char *temp = calloc(sizeof(*temp), previousEntry->strLen + 1);
memcpy(temp, previousEntry->seq, previousEntry->strLen);
temp[previousEntry->strLen] = previousEntry->seq[0];
fwrite(temp, sizeof(*temp), previousEntry->strLen + 1, fpOut);
add_entry(temp, previousEntry->strLen + 1);
} else {
fwrite(currentEntry->seq, sizeof(*currentEntry->seq), currentEntry->strLen, fpOut);
previousEntry = lookup_by_val(previous);
unsigned char *temp = calloc(sizeof(*temp), previousEntry->strLen + 1);
memcpy(temp, previousEntry->seq, previousEntry->strLen);
temp[previousEntry->strLen] = currentEntry->seq[0];
add_entry(temp, previousEntry->strLen + 1);
}
previous = current;
}
fclose(fpIn);
fclose(fpOut);
return 0;
}