-
Notifications
You must be signed in to change notification settings - Fork 1
/
my_compress.c
302 lines (273 loc) · 8.07 KB
/
my_compress.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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
*
* Copyright (c) 2000-2006 Alberto Reggiori <areggiori@webweaving.org>
* Dirk-Willem van Gulik <dirkx@webweaving.org>
*
* NOTICE
*
* This product is distributed under a BSD/ASF like license as described in the 'LICENSE'
* file you should have received together with this source code. If you did not get a
* a copy of such a license agreement you can pick up one at:
*
* http://rdfstore.sourceforge.net/LICENSE
*
* $Id: my_compress.c,v 1.5 2006/06/19 10:10:21 areggiori Exp $
*/
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <string.h>
#include "rdfstore_compress.h"
#include "my_compress.h"
/* RLE+VLE like de/encoder
*
* DISCLAIMER: I have no idea whether the following algorithm is under any existing patent
*
*
* First byte
* bit value meaning
* 0..3 ... lenght of run (len)
* 4-5 00 len is between 0 .. 15, no other bytes
* 10 len is continued in next byte, next is LSB (short int)
* 01 len is continued in next 3 bytes (long int)
* * first is upper nibble of MSB
* * next is lower nibble of MSB
* * next+1 is upper nibble of LSB
* * next+2 is lower nibble of LSB
* 11 len is continued in next 7 bytes (64 bit int, not implemented yet...)
* 6 set the byte just *after* the run len (see bit 4 and 5) gives the variable-length (up to 127)
* unset no var len
* 7 set run of len bytes set to the byte value after the run len
* unset no run, next len bytes are to be copied as-is i.e. "literal bytes"; such bytes could be repeated for a
* a given variable-length given in the next byte (up to 127) if bit 6 is set
*/
/*
*
* (compress|expand)_mine (
* len in buffer
* buff unsigned char buffer
*
* len-ptr will contain out size
* outbuff will contain compressed/decompressed data
* must be big enough ! if set to 'NULL' just
* the size will be calculated... (deleted as
* it takes (some) cycles.
* )
*/
unsigned int
compress_mine (
unsigned char * in,
unsigned char * out,
unsigned int insize
) {
register unsigned int i,j,vlen,l,len,comp,best_match;
unsigned int matches[MAXVARLENGTH];
double ratio;
for(i=0,j=0; i<insize; ) {
comp=0;
best_match=0;
len=1;
vlen=1;
/* look for the "best" repeated code; the loop can be repeated up to 2*vlen */
for( vlen=1;
( (i+(vlen*2) < insize) &&
(vlen<=MAXVARLENGTH) );
vlen++ ) {
matches[ vlen-1 ]=0;
/* check if there is at least *two* matches for vlen sequence to compress */
if(memcmp( in+i, in+i+vlen, vlen ) == 0 ) {
matches[ vlen-1 ]=1;
/* look for more matches to compress */
for( len=2, matches[ vlen-1 ]=2;
( i+(len*vlen)+vlen < insize ) &&
(len < MAXRUNLENGTH) &&
( memcmp( in+i, in+i+(len*vlen), vlen ) == 0 );
matches[ vlen-1 ]++, len++ ) { };
/* we should check if the compression is good enough vlen/len << 1/100 (or take the best one) */
ratio= (double)vlen / (double)len;
if ( ratio <= OPTIMAL ) {
/*
printf("GOT OPTIMAL i=%u j=%u value %u/%u: %f << %f\n",i,j,vlen,len,ratio,OPTIMAL);
*/
comp=1;
break;
};
if( (best_match == 0) ||
(ratio < ( (double)best_match / (double)matches[ best_match-1 ] ) ) )
best_match = vlen;
};
};
if(comp == 0) {
/* is there any good/best match? */
if ( ( best_match > 0 ) &&
( matches[ best_match-1 ] > 1 ) ) { /* do compress len>1 */
vlen = best_match;
len = matches[ best_match-1 ];
ratio= (double)vlen / (double)len;
comp=1;
} else {
/* the following is for "literal bytes"; we do store dict entries also for literals to refer them if the are at least 1 byte long */
vlen=1; /* force this */
for( len=1;
(len+i < insize) &&
(len < MAXVARLENGTH) &&
(len < MAXRUNLENGTH);
len++) {
if(len==1) {
if(in[i]==in[i+len]) {
break;
};
} else if( memchr( in+i, in[i+len], len ) != NULL ) {
break;
};
};
/*
{
unsigned int w=0;
printf("LITERAL BYTES insize=%u i=%u j=%u len=%u \n",insize,i,j,len);
printf("LITERAL BYTES insize=%u i=%u j=%u len=%u : ",insize,i,j,len); w=0; while (w<len) { printf("%x,", in[ i+w ]); w++; }; printf("\n");
};
*/
};
};
if (len == 0)
fprintf(stderr,"Compressing: RLE len=0\n");
if ( vlen > MAXVARLENGTH)
fprintf(stderr,"Var length too high!!!\n");
l = j;
if (len > 4095 ) {
out[ l ] = 32 + ((len >> 24) & 15);
out[ l+1 ] = (len>>16) & 0xffff;
out[ l+2 ] = (len>>8) & 0xffff;
out[ l+3 ] = len & 0xffff;
j+=3;
if(vlen>1) {
out[ l+4 ] = vlen & 127; /* see MAXVARLENGTH */
j++;
out[ l ] |= 64; /* having vlen>1 */
};
} else if (len > 15 ) {
out[ l ] = 16 + ((len >> 8) & 15);
out[ l+1 ] = len & 0xff;
j++;
if(vlen>1) {
out[ l+2 ] = vlen & 127;
j++;
out[ l ] |= 64;
};
} else {
out[ l ] = len & 15;
if(vlen>1) {
out[ l+1 ] = vlen & 127;
j++;
out[ l ] |= 64;
};
};
j++;
if ( comp ) {
out[ l ] |= 128;
/*
printf("COMPRESSING for i=%u j=%u insize=%u vlen=%u len=%u comp=%u ratio=%f\n",i,j,insize,vlen,len,comp,ratio);
*/
/* just the repeated bytes */
bcopy(in+i,out+j,vlen);
j+=vlen;
} else {
/*
printf("NOT COMPRESSING for i=%u j=%u insize=%u vlen=%u len=%u comp=%u\n",i,j,insize,vlen,len,comp);
*/
if (len==1) {
out[j] = in[i];
} else {
bcopy(in+i,out+j,len);
};
j+=len;
}; /* non-compressed else */
i+=(len*vlen); /* hop on for the next ones... */
/*
{
unsigned int w=0;
printf(">>>COMPRESSED STRING so far insize=%u i=%u j=%u : ",insize,i,j); w=0; while (w<j) { printf("%x,", out[ w ]); w++; }; printf("\n");
};
*/
}; /* for loop */
/*
printf("compressed string is %u bytes\n",j);
*/
/*
* pad to sensible length (in the hope it makes
* the db storage a bit faster .... if we do not
* have to increase the size of the stored block
* for each and every change..
*
* for(;j % 32;j++) out[j]='\0';
*/
return j;
};
unsigned int
expand_mine (
unsigned char * in,
unsigned char * out,
unsigned int insize
) {
register unsigned int i,j,c,len,vlen,k;
for(i=0,j=0; i<insize;) {
/* work out run length */
c = in[i];
if (!c)
break; /* no compress, no length */
if ( c & 32 ) {
len = c & 31;
len = ( len << 8 ) + in[ ++i ];
len = ( len << 8 ) + in[ ++i ];
len = ( len << 8 ) + in[ ++i ];
} else {
len = c & 15;
if ( c & 16 )
len = ( len << 8 ) + in[ ++i ];
};
if ( c & 64 ) {
vlen=in[ ++i ];
} else {
vlen=1;
};
if (len == 0) {
fprintf(stderr,"Bug: RLE len=0\n");
break;
};
i++;
if (c & 128) {
if (vlen>1) {
/*
printf("DECOMPRESSING for i=%u j=%u insize=%u vlen=%u len=%u\n",i,j,insize,vlen,len);
*/
for(k=0; k<len;k++) {
bcopy(in+i,out+(j+(k*vlen)),vlen);
};
i+=vlen;
} else {
/*
printf("DECOMPRESSING for i=%u j=%u insize=%u vlen=%u len=%u\n",i,j,insize,vlen,len);
*/
if(in[i] == 0) {
bzero(out+j, len);
} else {
memset(out+j, in[i], len);
};
i+=vlen;
};
} else {
/*
printf("EXPANDING literal bytes for i=%u j=%u vlen=%u len=%u insize=%u\n",i,j,vlen,len,insize);
*/
bcopy(in+i,out+j,len);
i+=len;
};
j+=(len*vlen);
}; /* for */
/*
printf("decompressed string is %u bytes\n",j);
*/
return j;
};