-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodec.c
More file actions
177 lines (152 loc) · 2.97 KB
/
Copy pathcodec.c
File metadata and controls
177 lines (152 loc) · 2.97 KB
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
//
// encoder.c
//
//
// Created by Richard Fine on 13/01/2015.
//
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int isDecoding = 0;
char* dbFile = 0;
long dbSize = 0;
char* dbData = 0;
unsigned int dbEntries = 0;
char** dbIndices;
void readDbFile()
{
FILE* fp = fopen(dbFile, "r");
if(!fp) { fprintf(stderr, "Could not open dbfile %s\n", dbFile); exit(-2); }
fseek(fp, 0, SEEK_END);
dbSize = ftell(fp);
fseek(fp, 0, SEEK_SET);
dbData = malloc(dbSize + 1);
fread(dbData, dbSize, 1, fp);
dbData[dbSize + 1] = 0;
fclose(fp);
}
int isClauseEnder(char ch)
{
return (ch == '.') || (ch == ',') || (ch == '?') || (ch == ':') || (ch == ';');
}
int isWhitespace(char ch)
{
return (ch == ' ') || (ch == '\t') || (ch == '\n');
}
void parseDb()
{
int e; char* ptr; long i;
dbEntries = 0;
for(i = 0; i < dbSize; ++i)
{
if(isClauseEnder(dbData[i]) && ((i + 1 >= dbSize) || isWhitespace(dbData[i+1])))
{
dbData[i+1] = 0;
++dbEntries;
}
}
dbIndices = malloc(dbEntries * sizeof(char*));
ptr = dbData;
for(e = 0; e < dbEntries; ++e)
{
dbIndices[e] = ptr;
ptr += strlen(ptr) + 1;
}
}
void compressDb()
{
for(int i = 0; i < dbEntries; ++i)
{
// Trim beginning
while(isWhitespace(*dbIndices[i]) && *dbIndices[i] != 0)
dbIndices[i]++;
// Trim end
int len = strlen(dbIndices[i]);
while(len > 0 && isWhitespace(dbIndices[i][len - 1]))
{
dbIndices[i][--len] = 0;
}
// Check that we've not already seen it - this is O(n^2) - oh well
for(int j = 0; j < i; ++j)
{
if(strcmp(dbIndices[i], dbIndices[j]) == 0)
{
*dbIndices[i] = 0;
len = 0;
break;
}
}
// Eliminate zero-length strings
if(len == 0)
{
dbIndices[i] = dbIndices[--dbEntries];
--i;
continue;
}
}
}
void encode(FILE* input, FILE* output, int wordSize)
{
unsigned int buf = 0;
unsigned char bitsQueued = 0;
unsigned int bitsMask = (1 << wordSize) - 1;
while(!feof(input))
{
unsigned int c = 0;
fread(&c, 1, 1, input);
c <<= bitsQueued;
buf |= c;
bitsQueued += 8;
while(bitsQueued > wordSize)
{
int bits = buf & bitsMask;
fprintf(output, "%s ", dbIndices[bits]);
buf >>= wordSize;
bitsQueued -= wordSize;
}
}
if(bitsQueued > 0)
{
fprintf(output, "%s\n", dbIndices[buf]);
}
}
int main(int argc, char* argv[])
{
for(int i = 1; i < argc; ++i)
{
if (strcmp(argv[i], "-decode") == 0)
{
isDecoding = 1;
continue;
}
if (strcmp(argv[i], "-db") == 0)
{
if(i+1 < argc) dbFile = argv[i+1];
continue;
}
}
if(dbFile == 0)
{
dbFile = "nottingham.txt";
}
readDbFile();
parseDb();
compressDb();
unsigned int wordSize = 32;
while(((1ul << wordSize) - 1) > dbEntries && wordSize > 0) wordSize--;
if(wordSize == 0) { fprintf(stderr, "DB is too small\n"); exit(-3); }
if(isDecoding)
{
fprintf(stderr, "Decoding is not implemented for security reasons\n");
exit(-5);
}
else
{
encode(stdin, stdout, wordSize);
}
free(dbIndices);
free(dbData);
return 0;
}