-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlz_pack.cpp
More file actions
536 lines (438 loc) · 11.6 KB
/
Copy pathlz_pack.cpp
File metadata and controls
536 lines (438 loc) · 11.6 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
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#define max(a,b) ((a)>(b)?(a):(b))
// ne doit jamais depasser 16 les deux !!!
//4
#define _LZ77_NB_BIT_SIZE 4
#define _LZ77_NB_BIT_OFFSET 12
#define _LZ77_MAX_SIZE (1<<_LZ77_NB_BIT_SIZE) // 1<<4 -> 16
#define _LZ77_MAX_OFFSET ((1<<_LZ77_NB_BIT_OFFSET)+1) // 1<<12 -> 4096+1 = 4097
#define _LZ77_ROOT (_LZ77_MAX_OFFSET)
#define _LZ77_SEARCH_SIZE (_LZ77_MAX_SIZE+1)
typedef struct
{
short smaller_child;
short larger_child;
short parent;
short branch; // par quel branche du parent est il venue
}LZ77_TREE;
LZ77_TREE BigTree[_LZ77_MAX_OFFSET+2];
unsigned char gLZ77_XorMask=0;
long LZ77_Compress(void* buf_src_void,void *buf_dest_void,long size_buf_src)
{
unsigned char *buf_src =(unsigned char*)buf_src_void;
unsigned char *buf_dest=(unsigned char*)buf_dest_void;
LZ77_TREE *tree,*real_tree,*current_tree,*del_tree;
unsigned char *ptr_decoding_mask;
unsigned char *ptr_search_src;
unsigned char andvalue;
long nb_rec;
long size;
long delta_value;
long offset_src;
short i,k;
short best_match;
short best_node;
short current_node;
short num_tree;
short search_index;
short branch_value;
real_tree=BigTree;
// ruse pour eviter les test kan les child sont … -1
// mon tableau commence … +1 donc ce ki sera patch‚ en 0
// rien … faire !!
tree=real_tree+1;
// init tree
memset(tree,-1,(_LZ77_MAX_OFFSET+1)*sizeof(LZ77_TREE));
offset_src=0;
nb_rec=1;
size=0;
andvalue=0;
while (offset_src+nb_rec-1<size_buf_src)
{
//
// Reload encoding type mask
//
if (!andvalue)
{
andvalue=1;
ptr_decoding_mask=buf_dest++;
*ptr_decoding_mask=0; // No match
size++;
}
// delete string de 'num_index'
// en recherchant ds tree celui ki a 'num_index+last advance'
while (nb_rec>0)
{
i=(short)(offset_src % _LZ77_MAX_OFFSET);
num_tree=i;
current_tree=&tree[i];
if (current_tree->parent>=0)
{
// supprime ca
if ( (current_tree->smaller_child<0) || (current_tree->larger_child<0) )
{ // facile a faire
branch_value=current_tree->larger_child;
if (branch_value>=0)
{
tree[branch_value].parent=current_tree->parent;
tree[branch_value].branch=current_tree->branch;
}
else
{
branch_value=current_tree->smaller_child;
if (branch_value>=0)
{
tree[branch_value].parent=current_tree->parent;
tree[branch_value].branch=current_tree->branch;
}
else // no child
{
branch_value=-1;
}
}
}
else
{
// supprime noeud ki a 2 fils
// d'ou ruse :
// au choix rechercher le plus grand du fils 'larger'
// ou recherche le plus grand du fils 'smaller'
// de toute facon ca revient au meme.... ( ce qui est logique d'ailleur)
branch_value=current_tree->smaller_child;
del_tree=&tree[branch_value];
if (del_tree->larger_child<0)
current_tree->smaller_child=del_tree->smaller_child;
else
{
while (del_tree->larger_child>=0)
{
branch_value=del_tree->larger_child;
del_tree=&tree[branch_value];
}
// supprime del
tree[del_tree->parent].larger_child=del_tree->smaller_child;
}
tree[del_tree->smaller_child].parent=del_tree->parent;
tree[del_tree->smaller_child].branch=del_tree->branch;
//remplacement
del_tree->smaller_child=current_tree->smaller_child;
del_tree->larger_child =current_tree->larger_child;
tree[del_tree->smaller_child].parent=branch_value;
tree[del_tree->larger_child].parent =branch_value;
del_tree->parent=current_tree->parent;
del_tree->branch=current_tree->branch;
}
// update chez parent de i
if (!current_tree->branch) tree[current_tree->parent].smaller_child=branch_value;
else tree[current_tree->parent].larger_child =branch_value;
}
current_tree->larger_child =-1;
current_tree->smaller_child=-1;
// donc on va ecrire ds tree[numtree]
current_node=tree[_LZ77_ROOT].smaller_child; // init with root
if (current_node<0) // remplis racine de l'arbre...
{
tree[num_tree].parent=_LZ77_ROOT;
tree[num_tree].branch=0; // weil immer smaller branch for root
tree[_LZ77_ROOT].smaller_child=num_tree;
best_match=0;
}
else
{
best_match=2;
best_node=0;
while (1)
{
i=num_tree-current_node; // offset
if (i<0) i+=(_LZ77_MAX_OFFSET);
//k= _SearchSequence(src-i,src,_LZ77_MAX_SIZE+2);
ptr_search_src=buf_src-i;
search_index=0;
while (ptr_search_src[search_index] == buf_src[search_index])
{
if (search_index==_LZ77_SEARCH_SIZE) break;
search_index++;
}
delta_value=ptr_search_src[search_index] - buf_src[search_index];
//k=_LZ77_SEARCH_SIZE-search_index; // 17-
if ( (search_index==_LZ77_SEARCH_SIZE) && (!delta_value) )
{
//
// 1) On a trouvé une chaine de la taille maximale
// possible pour l'encoding...
// On stocke cette entrée, et on se casse.
// On ne trouvera jamais mieux !
//
tree[num_tree]=tree[current_node];
// on s'occupe du parent
if (!tree[num_tree].branch) tree[tree[num_tree].parent].smaller_child=num_tree;
else tree[tree[num_tree].parent].larger_child =num_tree;
// et des fils
tree[tree[num_tree].smaller_child].parent=num_tree;
tree[tree[num_tree].larger_child].parent =num_tree;
// on efface l'ancien noeud proprement...
tree[current_node].parent=-1;
best_match=_LZ77_MAX_SIZE+2; // 18 octets à copier=maximum possible avec l'encodage 4:12
best_node =current_node;
break;
}
else
{
//
// 2) On a pas trouvé une chaine de taille maximale
//
k=_LZ77_SEARCH_SIZE-(_LZ77_SEARCH_SIZE-search_index);
if (k>best_match)
{
best_match=k;
best_node=current_node;
}
if (delta_value>=0)
{ // suivre larger
k=tree[current_node].larger_child;
if (k<0)
{
tree[num_tree].parent =current_node;
tree[current_node].larger_child =num_tree;
tree[num_tree].branch =-1; // comming from larger
break;
}
else current_node=k;
}
else // suivre smaller
{
k=tree[current_node].smaller_child;
if (k<0)
{
tree[num_tree].parent =current_node;
tree[current_node].smaller_child=num_tree;
tree[num_tree].branch =0; // comming from smaller
break;
}
else current_node=k;
}
}
}
}
nb_rec--;
if (nb_rec>0)
{
buf_src++;
offset_src++;
}
}
// bindage de taille
if (size>=size_buf_src-2)
{
return -1;
}
nb_rec=best_match;
//value>>=1;
if ( (nb_rec>2) && (offset_src+nb_rec<=size_buf_src) ) // 2 match 0 bits win ....
{
unsigned int value_short;
//
// Copy with offset
//
i=(offset_src % (_LZ77_MAX_OFFSET))-best_node;
if (i<0) i+=(_LZ77_MAX_OFFSET);
value_short=((nb_rec-3)<<(16-_LZ77_NB_BIT_SIZE)) | (i-1);
*buf_dest++=(unsigned char)(value_short & 255);
*buf_dest++=(unsigned char)(value_short>>8);
size+=2;
if (gLZ77_XorMask) *ptr_decoding_mask|=andvalue;
}
else
{
//
// Just put the byte, without compression
// and put '1' in the encoding mask
//
if (!gLZ77_XorMask) *ptr_decoding_mask|=andvalue;
*buf_dest++=*buf_src;
size++;
nb_rec=1;
}
offset_src++;
buf_src++;
andvalue<<=1;
}
return size;
//return ((size+3)&(~3));
}
void LZ77_UnCompress(void* buf_src_void,void* buf_dest_void, long size)
{
unsigned char *buf_src =(unsigned char*)buf_src_void;
unsigned char *buf_dest=(unsigned char*)buf_dest_void;
unsigned char value;
unsigned char andvalue;
long offset,nb;
andvalue=0;
while (size>0)
{
//
// Reload encoding type mask
//
if (!andvalue)
{
andvalue=1;
value=(*buf_src++);
}
if ((value^gLZ77_XorMask) & andvalue)
{
//
// Copy 1 unsigned char
//
*buf_dest++=*buf_src++;
size--;
}
else
{
//
// Copy with offset
//
// At this point, the source pointer points to a two byte
// value that actually contains a 4 bits counter, and a
// 12 bit offset to point back into the depacked stream.
// The counter is in the 4 high order bits.
//
offset = buf_src[0]; // Read 16 bits non alligned datas...
offset|=(buf_src[1]&0x0F)<<8;
offset+=1;
nb =(buf_src[1]>>4)+3;
buf_src+=2;
size-=nb;
while (nb>0)
{
*buf_dest++=*(buf_dest-offset);
nb--;
}
}
andvalue<<=1;
}
}
long LZ77_ComputeDelta(unsigned char *buf_comp,long size_uncomp,long size_comp)
{
unsigned char *buf_src =(unsigned char*)buf_comp;
unsigned char *buf_dest=(unsigned char*)buf_comp+size_comp-size_uncomp;
unsigned char value;
unsigned char andvalue;
long offset,nb;
long max_delta_space;
max_delta_space=0;
andvalue=0;
while (size_uncomp>0)
{
//
// Reload encoding type mask
//
if (!andvalue)
{
andvalue=1;
value=(*buf_src++);
}
if ((value^gLZ77_XorMask) & andvalue)
{
//
// Copy 1 unsigned char
//
//*buf_dest++=*buf_src++;
if (buf_dest>=buf_src)
{
max_delta_space=max(max_delta_space,buf_dest-buf_src);
}
buf_dest++;
buf_src++;
size_uncomp--;
}
else
{
//
// Copy with offset
//
// At this point, the source pointer points to a two byte
// value that actually contains a 4 bits counter, and a
// 12 bit offset to point back into the depacked stream.
// The counter is in the 4 high order bits.
//
offset = buf_src[0]; // Read 16 bits non alligned datas...
offset|=(buf_src[1]&0x0F)<<8;
offset+=1;
nb =(buf_src[1]>>4)+3;
buf_src+=2;
size_uncomp-=nb;
while (nb>0)
{
//*buf_dest++=*(buf_dest-offset);
if (buf_dest>=buf_src)
{
max_delta_space=max(max_delta_space,buf_dest-buf_src);
}
buf_dest++;
nb--;
}
}
andvalue<<=1;
}
return max_delta_space;
}
long LZ77_ComputeDeltaOld(unsigned char *buf_comp,long size_uncomp,long size_comp)
{
unsigned char value;
unsigned char andvalue;
long offset,nb;
long offset_comp;
long offset_uncomp;
long max_delta_space;
offset_comp=size_uncomp-size_comp;
offset_uncomp=0;
max_delta_space=0;
andvalue=0;
while (size_uncomp>0)
{
//
// Reload encoding type mask
//
if (!andvalue)
{
andvalue=1;
value=*buf_comp++;
offset_comp++;
}
if (value & andvalue)
{
//
// Copy 1 unsigned char
//
buf_comp++;
offset_uncomp++;
if (offset_comp<=offset_uncomp)
{
max_delta_space=max(offset_uncomp-offset_comp+1,max_delta_space);
}
offset_comp++;
size_uncomp--;
}
else
{
//
// Copy with offset
//
offset =*buf_comp++; // Read 16 bits non alligned datas...
offset|=(*buf_comp++)<<8;
nb=(offset & (0xff >> (8-_LZ77_NB_BIT_SIZE)) )+2+1;
offset=((offset>>_LZ77_NB_BIT_SIZE) & (0xffff >> (16-_LZ77_NB_BIT_OFFSET))) +1;
size_uncomp-=nb;
offset_uncomp+=nb;
if (offset_comp<=offset_uncomp)
{
max_delta_space=max(offset_uncomp-offset_comp+1,max_delta_space);
}
offset_comp+=2;
}
andvalue<<=1;
}
return((max_delta_space+3)&(~3)); // padd to four.
}