-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlz.c
More file actions
207 lines (193 loc) · 7.17 KB
/
lz.c
File metadata and controls
207 lines (193 loc) · 7.17 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#include "scanner.h"
struct word {
char constant;
int bitcount;
int alignment;
int offset;
};
struct bit {
int constantVal;
char wordType;
int bitNo;
};
//Module for lz
static int sigLength = -1;
enum wordtype {WT_LENGTH, WT_BACKREF, WT_END};
static struct word wordlist[] = {
{
.constant = 'l',
.bitcount = 0,
.alignment = 0,
.offset = 0
},
{
.constant = 'b',
.bitcount = 0,
.alignment = 0,
.offset = 0
},
{
.constant = '\0'
}
};
static struct bit *bitlist = NULL;
bool lz_Spec()
{
if (isconstnext())
return false;
char signature = getbit();
match : ;
//Find this in the wordlist
int i = 0;
for (; wordlist[i].constant != signature; i++) {
if (wordlist[i].constant == '\0') {
die(-1, "No specifier for literal matching '%c'\n", signature);
};
}
wordlist[i].bitcount = getnum();
while (!isconstnext()) {
switch (signature = getbit()) {
case '-' :
wordlist[i].offset = - getnum();
break;
case '+' :
wordlist[i].offset = getnum();
break;
case '*' :
wordlist[i].alignment = getnum() - 1;
break;
default :
goto match;
}
}
return true;
}
void lz_Init()
{
//Already identified correctly; specifier awaits us!
//Set all specifiers
while (lz_Spec())
;
//Bytecount is next
sigLength = getnum();
//A check would go here to make sure the sig is long enough
//to hold all the bits with the given alignment
//Fill in what every bit needs to be
int bitcount = sigLength * 8;
bitlist = malloc(sizeof(*bitlist) * bitcount);
for (int i = 0; i < bitcount; i++) {
if (isconstnext()) {
//Constant
bitlist[i].constantVal = getconst();
} else {
//Bit specifier
bitlist[i].constantVal = -1;
bitlist[i].wordType = getbit();
bitlist[i].bitNo = getnum();
}
}
}
//Hack to use Query to get the data to encode
static long queryDistance;
long lz_Query(char *input, long offset, long *count) {
//Perform a maximal search, and edit count appropriately
if (!*count)
return 0;
char *source = input + offset;
if (*count < wordlist[WT_LENGTH].offset)
return 0; //We can't encode this little
if (offset < wordlist[WT_BACKREF].offset)
return 0; //We can't encode this soon
//Get a pointer the farthest possible backreference can make in the given space
long distance = wordlist[WT_BACKREF].offset;
distance += ((1L << (wordlist[WT_BACKREF].bitcount)) - 1);
distance &= ~((1L << (wordlist[WT_BACKREF].alignment)) - 1);
if (offset - distance > 0)
input += offset - distance;
//Keep running track of how long the best match is
//Compare at each position, to fill its spot
//Don't use strcmp or strncmp. Zeros are valid input
//Modify count to contain length of longest match
//Consider alignment and offset
long length = 0;
long max_length = (1L << wordlist[WT_LENGTH].bitcount) - 1;
while (input < source) {
for (long i = 0; source[i] == input[i]; i++) {
if (i > length) {
length = i & ~((1L << (wordlist[WT_LENGTH].alignment)) - 1);
queryDistance = source - input;
};
//Don't exceed maximum run length
if (length > max_length) {
length = max_length;
break;
}
}
input += (1L << (wordlist[WT_BACKREF].alignment));
}
length++; //Indicies are zero-based
*count = length;
//Since LZ is only the size of its headers, we simply return bytes of headers
//We still report valid if count becomes something stupidly small
return sigLength;
}
long lz_Header(char **output, long count, long distance)
{
unsigned char outchar = 0;
int bitcount = sigLength * 8;
long byteCount = 0;
//Lz specific values
long length = 0;
long backref = 0;
//Find the value of these... values
//In order: offset, bitwidth, alignment
//Get the word data
count -= wordlist[WT_LENGTH].offset;
if (count < 0)
count = 0;
length = count & ((1L << (wordlist[WT_LENGTH].bitcount)) - 1);
length &= ~((1L << (wordlist[WT_LENGTH].alignment)) - 1);
distance -= wordlist[WT_BACKREF].offset;
if (distance < 0)
distance = 0;
backref = distance & ((1L << (wordlist[WT_BACKREF].bitcount)) - 1);
backref &= ~((1L << (wordlist[WT_BACKREF].alignment)) - 1);
byteCount = length + wordlist[WT_LENGTH].offset;
for (int i = 0; i < bitcount; i++) {
outchar <<= 1;
if (bitlist[i].constantVal != -1) {
outchar |= bitlist[i].constantVal;
} else {
//find this word in the wordlist
//lz has 'l' and 'b'
switch (bitlist[i].wordType) {
case 'l' :
outchar |= !!(length & (1L << bitlist[i].bitNo));
break;
case 'b' :
outchar |= !!(backref & (1L << bitlist[i].bitNo));
break;
}
}
if (i % 8 == 7) {
**output = outchar;
(*output)++;
outchar = 0;
};
}
return byteCount;
}
long lz_Encode(char *input, long offset, char **output, long count)
{
//This one assumes a lot.
if (count < wordlist[WT_LENGTH].offset)
return 0; //We can't encode this little
if (offset < wordlist[WT_BACKREF].offset)
return 0; //We can't encode this soon
lz_Query(input, offset, &count);
return count - lz_Header(output, count, queryDistance);
}