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
| #include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char file_name[5000];
printf("Codebook: ");
fgets(file_name, 5000, stdin);
file_name[strlen(file_name) - 1] = '\0';
FILE *codebook = fopen(file_name, "r");
if (codebook == NULL)
{
printf("No file.\n");
return -1;
}
printf("Input File: ");
fgets(file_name, 5000, stdin);
file_name[strlen(file_name) - 1] = '\0';
FILE *input = fopen(file_name, "rb");
if (input == NULL)
{
printf("No file.\n");
return -1;
}
printf("Output File: ");
fgets(file_name, 5000, stdin);
file_name[strlen(file_name) - 1] = '\0';
FILE *output = fopen(file_name, "wb");
if (output == NULL)
{
printf("No file.\n");
return -1;
}
int32_t encode[29];
int32_t encode_len[29] = {0};
for (int32_t i = 0; i < 29; i++)
{
encode[i] = -1;
}
char code[100];
while (!feof(codebook))
{
fgets(code, 100, codebook);
if (code[0] == '\n')
{
continue;
;
}
if (code[1] == ':')
{
if (code[0] >= 'a' && code[0] <= 'z')
{
encode[code[0] - 'a'] = strtol(code + 3, NULL, 2);
encode_len[code[0] - 'a'] = strlen(code + 3) - 1;
}
else
{
printf("Invalid codebook.\n");
goto terminate;
}
}
else
{
if (strstr(code, "space:"))
{
encode[26] = strtol(strchr(code, ':') + 2, NULL, 2);
encode_len[26] = strlen(strchr(code, ':') + 2) - 1;
}
else if (strstr(code, "comma:"))
{
encode[27] = strtol(strchr(code, ':') + 2, NULL, 2);
encode_len[27] = strlen(strchr(code, ':') + 2) - 1;
}
else if (strstr(code, "period:"))
{
encode[28] = strtol(strchr(code, ':') + 2, NULL, 2);
encode_len[28] = strlen(strchr(code, ':') + 2) - 1;
}
else
{
printf("Invalid codebook.\n");
goto terminate;
}
}
}
fclose(codebook);
int8_t temp = 0;
int8_t temp_len = 0;
while (!feof(input))
{
char c = fgetc(input);
if (c == EOF)
{
break;
}
int32_t code_now = 0;
int32_t code_now_len = 0;
if (c >= 'a' && c <= 'z')
{
code_now = encode[c - 'a'];
code_now_len = encode_len[c - 'a'];
}
else if (c == ' ')
{
code_now = encode[26];
code_now_len = encode_len[26];
}
else if (c == ',')
{
code_now = encode[27];
code_now_len = encode_len[27];
}
else if (c == '.')
{
code_now = encode[28];
code_now_len = encode_len[28];
}
else
{
printf("Invalid input.\n");
goto terminate;
}
if (code_now == -1)
{
printf("Invalid codebook.\n");
goto terminate;
}
// fprintf(stderr, "*%d %d\n", code_now, code_now_len);
while (temp_len + code_now_len > 8)
{
temp = (temp << (8 - temp_len)) | (code_now >> (code_now_len - (8 - temp_len)));
// fprintf(stderr, "%d ", temp);
fwrite(&temp, 1, 1, output);
code_now = code_now & ((1 << (code_now_len - (8 - temp_len))) - 1);
code_now_len = code_now_len - (8 - temp_len);
temp = 0;
temp_len = 0;
}
temp = temp << code_now_len | code_now;
temp_len += code_now_len;
// fprintf(stderr, "(%d %d\n", temp, temp_len);
if (temp_len == 8)
{
// fprintf(stderr, "a%d ", temp);
fwrite(&temp, 1, 1, output);
temp = 0;
temp_len = 0;
}
}
if (temp_len != 0)
{
temp = temp << (8 - temp_len);
temp += 1 << (8 - temp_len - 1);
fwrite(&temp, 1, 1, output);
}
else
{
temp = 0b10000000;
fwrite(&temp, 1, 1, output);
}
fclose(input);
fclose(output);
return 0;
terminate:
fclose(codebook);
fclose(input);
fclose(output);
return -1;
}
|