-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcompression_algos_test.cpp
More file actions
1078 lines (838 loc) · 50.9 KB
/
Copy pathcompression_algos_test.cpp
File metadata and controls
1078 lines (838 loc) · 50.9 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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Compressors testing
//
#include "stdafx.h"
//#include <cstdlib>
//#include <algorithm>
#include <Windows.h>
#include <vector>
#pragma warning( disable: 4996 )
//Defines
typedef unsigned int UINT;
typedef unsigned char UCHAR;
typedef unsigned long long int ULLINT;
typedef unsigned long long int QWORD;
typedef int(*__compar_d_fn_t) (const void *, const void *, void *);
//Macro
#define swap_uint(a, b) {UINT tmp = (a) ; (a) = (b); (b) = tmp;}
#define SFPS(pos) SetFilePointer(f, pos, 0, FILE_BEGIN)
#define SFPC(pos) SetFilePointer(f, pos, 0, FILE_CURRENT)
#define READ(v) ReadFile(f, &(v), sizeof(v), &a, NULL)
#define READP(p, n) ReadFile(f, p, n, &a, NULL)
#define WRITE(v) WriteFile(f2, &(v), sizeof(v), &a, NULL)
#define WRITEP(p, n) WriteFile(f2, p, n, &a, NULL)
//Structs
//Globals
UCHAR* gimme_random_array(UCHAR count)
{
UCHAR* mem = (UCHAR*)malloc(count * sizeof(UCHAR));
for (int i = 0; i < count; i++)
{
mem[i] = (UCHAR)rand();
}
return mem;
}
UCHAR* gimme_inverse_array(UCHAR count)
{
UCHAR* mem = (UCHAR*)malloc(count * sizeof(UCHAR));
for (int i = 0; i < count; i++)
{
mem[i] = count - i;
}
return mem;
}
UCHAR* gimme_sorted_array(UCHAR count)
{
UCHAR* mem = (UCHAR*)malloc(count * sizeof(UCHAR));
for (int i = 0; i < count; i++)
{
mem[i] = count;
}
return mem;
}
UCHAR* gimme_bunch_of_zeros_array(UCHAR count)
{
UCHAR* mem = (UCHAR*)malloc(count * sizeof(UCHAR));
memset(mem, 0, count * sizeof(UCHAR));
return mem;
}
void randomize_array(UCHAR* arr, UCHAR count)
{
for (int i = 0; i < count; i++)
{
arr[i] = rand();
}
}
void read_cycles(ULLINT* cycles)
{
UINT tmp_v1, tmp_v2;
__asm {
xor eax, eax
cpuid
rdtsc
mov tmp_v1, eax
mov tmp_v2, edx
}
*cycles = tmp_v2;
*cycles <<= 32;
*cycles += tmp_v1;
}
// Compressing\Decompressing algorithms
// Template
// out_array* Compressor(input_array*, UINT size_of_data, UINT* size_of_compressed_data)
// out_array* Decompressor(input_array*, UINT size_of_compressed_data, UINT size_of_decompressed_data)
// Delta encoding is
// [val1][val2] -> [val1][val2-val1]
// it helps to compress linear sequences
UCHAR* Delta_encode(UCHAR* input, UINT size_of_data, UINT* size_of_compressed_data)
{
UCHAR* input_ptr = input;
UCHAR saved_val = *input_ptr;
input_ptr++;
while (input_ptr <= (input + size_of_data))
{
UCHAR tmp = *input_ptr;
*input_ptr = *input_ptr - saved_val;
input_ptr++;
saved_val = tmp;
}
return input;
}
// [val1][val2-val1]
//
UCHAR* Delta_decode(UCHAR* input, UINT size_of_compressed_data, UINT size_of_decompressed_data)
{
UCHAR* input_ptr = input;
UCHAR saved_val = *input_ptr;
input_ptr++;
while (input_ptr <= (input + size_of_compressed_data))
{
*input_ptr = *input_ptr + saved_val;
saved_val = *input_ptr;
input_ptr++;
}
return input;
}
// Run Lenght Encode
// Scheme
// [amt] [val] [amt] [val] [val]
// amt < 254
// [amt] byte [value]
// if amt == 255 next byte is size of data bytes following next
// [255] byte [size] [data_bytes][][][]....
UCHAR* RLE_Compress(UCHAR* input, UINT size_of_data, UINT* size_of_compressed_data)
{
UCHAR* output = (UCHAR*)malloc(size_of_data);
UCHAR* output_ptr = output;
UCHAR* input_ptr = input;
while (input_ptr <= (input + size_of_data))
{
UINT b1 = *input_ptr;
input_ptr++;
UINT b2 = *input_ptr;
input_ptr++;
UCHAR run_lenght = 0;
if (b1 == b2)
{
run_lenght = 2;
while (*input_ptr == b1 && run_lenght < 254)
{
run_lenght++;
input_ptr++;
}
*output_ptr = run_lenght;
output_ptr++;
*output_ptr = b1;
output_ptr++;
}
else
{
run_lenght = 2;
UINT saved_val = b2;
UCHAR* saved_input_ptr = input_ptr - 2;
while (*input_ptr != saved_val && run_lenght < 254)
{
run_lenght++;
saved_val = *input_ptr;
input_ptr++;
}
*output_ptr = 255;
output_ptr++;
*output_ptr = run_lenght;
output_ptr++;
while (saved_input_ptr != input_ptr)
{
*output_ptr = *saved_input_ptr;
output_ptr++;
saved_input_ptr++;
}
}
}
*output_ptr = 0;
*size_of_compressed_data = output_ptr - output;
printf("RLE Compression done: %d : %d\n", size_of_data, *size_of_compressed_data);
return output;
}
UCHAR* RLE_Decompress(UCHAR* input, UINT size_of_compressed_data, UINT size_of_decompressed_data)
{
UCHAR* output = (UCHAR*)malloc(size_of_decompressed_data);
UCHAR* output_ptr = output;
UCHAR* input_ptr = input;
while (output_ptr <= (output + size_of_decompressed_data))
{
UINT amt = *input_ptr;
input_ptr++;
UINT val = *input_ptr;
input_ptr++;
if (amt == 255)
{
for (int i = 0; i < val; i++)
{
*output_ptr = *input_ptr;
input_ptr++;
output_ptr++;
}
continue;
}
if (amt != 0)
{
for (int i = 0; i < amt; i++)
{
*output_ptr = val;
output_ptr++;
}
continue;
}
if (size_of_decompressed_data != (output_ptr - output))
{
printf("Error in decompression, incorrect decompression size\n");
return output;
}
}
return output;
}
//RLE + Delta-encoding
UCHAR* DeltaRLE_Compress(UCHAR* input, UINT size_of_data, UINT* size_of_compressed_data)
{
Delta_encode(input, size_of_data, size_of_compressed_data);
return RLE_Compress(input, size_of_data, size_of_compressed_data);
}
UCHAR* DeltaRLE_Decompress(UCHAR* input, UINT size_of_compressed_data, UINT size_of_decompressed_data)
{
UCHAR* decompressed_data = RLE_Decompress(input, size_of_compressed_data, size_of_decompressed_data);
Delta_decode(decompressed_data, size_of_decompressed_data, size_of_decompressed_data);
return decompressed_data;
}
#define HUFFMAN_MAX_SYMBOLS_COUNT 256
// Huffman code compression
struct Huffman_Tree_node {
UINT weight;
// contain symbols
UINT count;
UCHAR symbols[HUFFMAN_MAX_SYMBOLS_COUNT];
Huffman_Tree_node* p_left = nullptr;
Huffman_Tree_node* p_right = nullptr;
};
void Dump_node(Huffman_Tree_node* p_node, UINT hops)
{
for (int i = 0; i < hops; i++)
{
printf("\t");
}
printf("%08x count - %d left %08x right %08x \n", p_node, p_node->count, p_node->p_left, p_node->p_right);
}
void Dump_bit_string(UINT val, UINT len) {
for (int i = 0; i < len; i++) {
printf(val & (1 << i) ? "1" : "0");
}
}
void Dump_bit_table(UINT* bit_codes, UINT* bit_counts) {
printf("Symbol_table: \n");
for (int i = 0; i < HUFFMAN_MAX_SYMBOLS_COUNT; i++) {
printf("%02x : len %d (", i, bit_counts[i]);
Dump_bit_string(bit_codes[i], bit_counts[i]);
printf(") \n");
}
}
int quick_sort_pairs_partition(std::vector<std::pair<BYTE, BYTE>>& p_v, int low, int high)
{
BYTE pivot = p_v[high].first; // pivot
int i = (low - 1); // Index of smaller element and indicates the right position of pivot found so far
for (int j = low; j <= high - 1; j++)
{
// If current element is smaller than the pivot
if (p_v[j].first < pivot)
{
i++; // increment index of smaller element
std::pair<BYTE, BYTE> tmp = p_v[i];
p_v[i] = p_v[j];
p_v[j] = tmp;
}
}
std::pair<BYTE, BYTE> tmp = p_v[i + 1];
p_v[i + 1] = p_v[high];
p_v[high] = tmp;
return (i + 1);
}
void quick_sort_pairs(std::vector<std::pair<BYTE, BYTE>>& p_v, int low, int high) {
if (low < high)
{
/* pi is partitioning index, arr[p] is now
at right place */
int pi = quick_sort_pairs_partition(p_v, low, high);
// Separately sort elements before
// partition and after partition
quick_sort_pairs(p_v, low, pi - 1);
quick_sort_pairs(p_v, pi + 1, high);
}
}
UCHAR* Huffman_Compress(UCHAR* input, UINT size_of_data, UINT* size_of_compressed_data)
{
UCHAR* input_ptr = input;
//Fill frequency table
static UINT g_frequency_table[HUFFMAN_MAX_SYMBOLS_COUNT] = { 0 };
static UINT g_symbol_bits_table[HUFFMAN_MAX_SYMBOLS_COUNT] = { 0 };
static UINT g_bit_count_table[HUFFMAN_MAX_SYMBOLS_COUNT] = { 0 };
static bool g_used_symbols[HUFFMAN_MAX_SYMBOLS_COUNT] = { 0 };
while (input_ptr <= (input + size_of_data))
{
g_frequency_table[*input_ptr]++;
g_used_symbols[*input_ptr] = true;
input_ptr++;
}
printf("g_frequency_table built\n");
std::vector <Huffman_Tree_node*> ht_nodes;
for (int i = 0; i < HUFFMAN_MAX_SYMBOLS_COUNT; i++)
{
if (!g_used_symbols[i]) continue;
Huffman_Tree_node* p_node = new Huffman_Tree_node;
p_node->weight = g_frequency_table[i];
p_node->count = 1;
p_node->symbols[0] = i;
ht_nodes.push_back(p_node);
}
// building tree
while (ht_nodes.size() > 1)
{
int id_least1 = 0;
int id_least2 = 0;
UINT least_weight1 = -1;
UINT least_weight2 = -1;
// find 2 least weight nodes
for (int i = 0; i < ht_nodes.size(); i++)
{
if (least_weight1 >= ht_nodes[i]->weight)
{
least_weight1 = ht_nodes[i]->weight;
id_least1 = i;
}
if (least_weight2 >= ht_nodes[i]->weight && i != id_least1)
{
least_weight2 = ht_nodes[i]->weight;
id_least2 = i;
}
}
//Construct new node
Huffman_Tree_node* p_node = new Huffman_Tree_node;
p_node->weight = least_weight1 + least_weight2;
p_node->count = ht_nodes[id_least1]->count + ht_nodes[id_least2]->count;
p_node->p_right = ht_nodes[id_least1]; // the least one is right branch
p_node->p_left = ht_nodes[id_least2];
//printf("p_node->weight: %d \n", p_node->weight);
//printf("p_node->count: %d \n", p_node->count);
UINT cnt = 0;
// Filling symbols
while (cnt < p_node->count)
{
if (cnt < ht_nodes[id_least1]->count)
p_node->symbols[cnt] = ht_nodes[id_least1]->symbols[cnt];
else
p_node->symbols[cnt] = ht_nodes[id_least2]->symbols[cnt - ht_nodes[id_least1]->count];
cnt++;
}
// changes to new node at old one and removes second old node.
ht_nodes[id_least2] = p_node;
ht_nodes.erase(ht_nodes.begin() + id_least1);
//printf("Nodes remaining %d\n", ht_nodes.size());
}
printf("Huffman Tree built\n");
// builds code table
for (UINT symbol = 0; symbol < HUFFMAN_MAX_SYMBOLS_COUNT; symbol++)
{
if (!g_used_symbols[symbol]) continue;
UINT hops = 0;
UINT symbol_code = 0;
Huffman_Tree_node* p_node = ht_nodes[0];
while (p_node)
{
//Dump_node(p_node, hops);
// node contain symbol ?
UCHAR cnt = 0;
bool is_left = false;
bool is_contain = false;
if (p_node->p_left)
{
while (cnt < p_node->p_left->count)
{
if (p_node->p_left->symbols[cnt] == symbol)
{
is_left = true;
is_contain = true;
p_node = p_node->p_left;
break;
}
cnt++;
}
}
if (is_left)
{
hops += is_contain;
continue;
}
if (p_node->p_right)
{
cnt = 0;
while (cnt < p_node->p_right->count)
{
if (p_node->p_right->symbols[cnt] == symbol)
{
p_node = p_node->p_right;
is_contain = true;
break;
}
cnt++;
}
}
if (!is_contain)
break;
// [d] [a] [b] |
// d=00000000 \ \0 / 1 } 2 - hop
// a=10000000 0| [c] |
// b=11000000 \ / 1 } 1 - hop
// [a] |
symbol_code |= ((!is_left) << hops);
hops += is_contain;
}
g_symbol_bits_table[symbol] = symbol_code;
g_bit_count_table[symbol] = hops;
}
printf("code tables built\n");
DWORD symbol_used_amt = 0;
for (auto used : g_used_symbols) { if (used) symbol_used_amt++; }
// 3 tables [symb][sybol codes][symbol len] - [byte][word][byte]
DWORD additional_info_size = symbol_used_amt * 4 + sizeof(WORD);
UCHAR* output = (UCHAR*)malloc(size_of_data + additional_info_size);
memset(output, 0, size_of_data + additional_info_size);
UCHAR* output_ptr = output;
input_ptr = input;
// Store tables
// there need some kind of sorting for tables
// from lowest bit count to higher.
std::vector<std::pair<BYTE, BYTE>> increasing_order; // number of bits and ID
for (int i = 0; i < HUFFMAN_MAX_SYMBOLS_COUNT; i++) {
if (!g_used_symbols[i]) continue;
increasing_order.push_back({ g_bit_count_table[i], i });
}
quick_sort_pairs(increasing_order, 0, increasing_order.size() - 1);
// storing amount of symbols used
*(WORD*)output_ptr = symbol_used_amt;
output_ptr += 2;
// symbols table
for (int i = 0; i < symbol_used_amt; i++) {
*(BYTE*)output_ptr = increasing_order[i].second;
output_ptr++;
}
// symbol code table
for (int i = 0; i < symbol_used_amt; i++)
{
*((DWORD*)output_ptr) = g_symbol_bits_table[increasing_order[i].second];
output_ptr += 4;
}
// bit amount table
for (int i = 0; i < symbol_used_amt; i++)
{
*((BYTE*)output_ptr) = g_bit_count_table[increasing_order[i].second];
output_ptr += 1;
}
int bit_counter = 0;
// bit hell
while (input_ptr <= (input + size_of_data))
{
QWORD val = *((QWORD*)output_ptr);
//val = 0;
while (bit_counter < 32)
{
UCHAR symbol = *input_ptr;
input_ptr++;
val |= ((QWORD)g_symbol_bits_table[symbol] << bit_counter);
bit_counter += g_bit_count_table[symbol];
}
*((QWORD*)output_ptr) = val;
if (bit_counter >= 32)
{
output_ptr += 4;
bit_counter -= 32;
}
}
*size_of_compressed_data = output_ptr - output + 1;
printf("Huffman Compression done: %d : %d\n", size_of_data, *size_of_compressed_data);
printf("Huffman Compression tables space coeff: %f %%\n", 100.0f * (float)additional_info_size
/ (float) (*size_of_compressed_data - additional_info_size));
return output;
}
UCHAR* Huffman_Decompress(UCHAR* input, UINT size_of_compressed_data, UINT size_of_decompressed_data)
{
UCHAR* input_end = input + size_of_compressed_data;
static UINT g_symbol_bits_table[HUFFMAN_MAX_SYMBOLS_COUNT] = { 0 };
static UINT g_bit_count_table[HUFFMAN_MAX_SYMBOLS_COUNT] = { 0 };
static BYTE g_char_table[HUFFMAN_MAX_SYMBOLS_COUNT] = { 0 };
static DWORD g_bitmask_table[HUFFMAN_MAX_SYMBOLS_COUNT] = { 0 };
DWORD symbol_used_amt = 0;
symbol_used_amt = *(WORD*)input;
input += sizeof(WORD);
for (int i = 0; i < symbol_used_amt; i++) {
g_char_table[i] = *(BYTE*)input;
input++;
}
// read immportant tables
for (int i = 0; i < symbol_used_amt; i++) {
g_symbol_bits_table[i] = *(DWORD*)input;
input += 4;
}
for (int i = 0; i < symbol_used_amt; i++) {
g_bit_count_table[i] = *(BYTE*)input;
input++;
}
int symb_id = 0;
while (symb_id < symbol_used_amt) {
DWORD bit_mask = 0;
for (int i = 0; i < g_bit_count_table[symb_id]; i++) bit_mask |= 1 << i;
g_bitmask_table[symb_id] = bit_mask;
symb_id++;
}
//Dump_bit_table(g_symbol_bits_table, g_bit_count_table);
//return input;
UCHAR* decompressed_data = (UCHAR*)malloc(size_of_decompressed_data);
memset(decompressed_data, 0, size_of_decompressed_data);
UCHAR* output_ptr = decompressed_data;
UCHAR* output_end = decompressed_data + size_of_decompressed_data;
int bit_counter = 0;
DWORD tmp_val;
// Decoder :(
while (output_ptr <= output_end && input <= input_end)
{
while (bit_counter < 32) {
DWORD val = *((QWORD*)input) >> bit_counter;
//val = 0;
int symb_id = 0;
// probably should use something tree like structures
// instead of iterating through every symbol ??
// probably will be better in perf but anyway not speed testing this algos
while (symb_id < symbol_used_amt) {
DWORD tmp_val = val ^ g_symbol_bits_table[symb_id];
if ((tmp_val & g_bitmask_table[symb_id]) == 0) {
*output_ptr = g_char_table[symb_id];
output_ptr++;
break;
}
symb_id++;
}
bit_counter += g_bit_count_table[symb_id];
}
if (bit_counter >= 32) {
input += 4;
bit_counter -= 32;
}
}
return decompressed_data;
}
// Liv-Zempel? like algorithm
// just dictionary algorithm of some sort
// with sliding window
// !!! NOT WORKING NOW
#define LZ_SLIDING_WINDOW_SIZE 0x8000
#define LZ_MAX_SEQUENCE_LENGHT 0xFF
#define LZ_MAX_COPY_DATA_LENGHT 0x7F
struct lz_op_code {
//USHORT distance; // if distance < LZ_SLIDING_WINDOW_SIZE this correct
UCHAR dist1; //higner if > 0x80 then copy
UCHAR dist0; //lower part
UCHAR lenght;
UCHAR symbol;
};
// unused
/*
struct lz_op_code_data {
UCHAR lenght; // len = this byte - 0x80 + 1
UCHAR data; // data bytes ahead...
};*/
// Fills lps[] for given patttern pat[0..M-1]
void lz_computeLPSArray(UCHAR* pat, int M, UCHAR* lps)
{
// length of the previous longest prefix suffix
UCHAR len = 0;
lps[0] = 0; // lps[0] is always 0
// the loop calculates lps[i] for i = 1 to M-1
int i = 1;
while (i < M) {
if (pat[i] == pat[len]) {
len++;
lps[i] = len;
i++;
}
else // (pat[i] != pat[len])
{
// This is tricky. Consider the example.
// AAACAAAA and i = 7. The idea is similar
// to search step.
if (len != 0) {
len = lps[len - 1];
// Also, note that we do not increment
// i here
}
else // if (len == 0)
{
lps[i] = 0;
i++;
}
}
}
}
// Uses Knuth–Morris–Pratt algorithm
USHORT lz_find_best_match(UCHAR* buf, USHORT buf_len, UCHAR* seq, USHORT seq_len, USHORT* max_len_found) {
int M = seq_len;
int N = buf_len;
*max_len_found = 0;
// create lps[] that will hold the longest prefix suffix
// values for pattern
//int* lps = (int*)malloc(sizeof(int)*M);
static UCHAR lps[LZ_MAX_SEQUENCE_LENGHT] = { 0 };
//int lps[M];
// Preprocess the pattern (calculate lps[] array)
lz_computeLPSArray(seq, M, lps);
int i = 0; // index for txt[]
int j = 0; // index for pat[]
while (i < N) {
*max_len_found = j;
if (seq[j] == buf[i]) {
j++;
i++;
}
if (j == M) {
//free(lps);
return i - j;
//printf("Found pattern at index %d ", i - j);
//j = lps[j - 1];
}
// mismatch after j matches
else if (i < N && seq[j] != buf[i]) {
// Do not match lps[0..lps[j-1]] characters,
// they will match anyway
if (j != 0)
j = lps[j - 1];
else
i = i + 1;
}
}
//free(lps);
return -1;
}
UCHAR* lz_compress(UCHAR* input, UINT size_of_data, UINT* size_of_compressed_data) {
// first fill buffer with known information
UCHAR* output = (UCHAR*)malloc(size_of_data);
UCHAR* output_ptr = output;
static_assert(LZ_MAX_COPY_DATA_LENGHT <= 0x7F, "LZ_MAX_COPY_DATA_LENGHT must be <= 0x7F");
*output_ptr = 0x80 + LZ_MAX_COPY_DATA_LENGHT;
output_ptr++;
// Optimize this code for small data ?
if (size_of_data < LZ_MAX_COPY_DATA_LENGHT) {
memcpy(output_ptr, input, size_of_data);
output_ptr += size_of_data;
*size_of_compressed_data = output_ptr - output;
return output_ptr;
}
else {
memcpy(output_ptr, input, LZ_MAX_COPY_DATA_LENGHT);
output_ptr += LZ_MAX_COPY_DATA_LENGHT;
}
UCHAR* dict_ptr = input;
UCHAR* input_ptr = input + LZ_MAX_COPY_DATA_LENGHT;
UCHAR* end_ptr = input + size_of_data;
while (input_ptr < end_ptr) {
// find best match!
// IMPORTANT!!!
// this is bad way to do it!
// There should be way to optimize best matches \n
if (input_ptr + LZ_MAX_SEQUENCE_LENGHT + 1 < end_ptr) {
UINT seq_lenght = LZ_MAX_SEQUENCE_LENGHT;
USHORT max_len_found;
USHORT distance = 0xFFFF;
// find lower limit first
while (distance == 0xFFFF) {
distance = lz_find_best_match(dict_ptr, input_ptr - dict_ptr, input_ptr, seq_lenght, &max_len_found);
//printf("dist %d seq_len %d dict: %08x seq: %08x\n", distance, seq_lenght, dict_ptr, input_ptr);
seq_lenght = max_len_found;
if (seq_lenght == 0) break;
}
// now match found
if (distance == 0xFFFF)
{
*output_ptr = 0x80 + LZ_MAX_COPY_DATA_LENGHT;
output_ptr++;
memcpy(output_ptr, input_ptr, LZ_MAX_COPY_DATA_LENGHT);
output_ptr += LZ_MAX_COPY_DATA_LENGHT;
// updata pointers
input_ptr += LZ_MAX_COPY_DATA_LENGHT;
if (input_ptr - dict_ptr >= LZ_SLIDING_WINDOW_SIZE)
dict_ptr = input_ptr - LZ_SLIDING_WINDOW_SIZE;
}
// contruct lz opcode
lz_op_code opcode;
opcode.dist0 = distance & 0xFF;
opcode.dist1 = (distance & 0xFF00) >> 8;
//opcode.distance = distance;
opcode.lenght = seq_lenght;
opcode.symbol = *(input_ptr + seq_lenght);
*(lz_op_code*)output_ptr = opcode;
output_ptr += sizeof(lz_op_code);
// update pointers
input_ptr += seq_lenght;
if (input_ptr - dict_ptr >= LZ_SLIDING_WINDOW_SIZE)
dict_ptr = input_ptr - LZ_SLIDING_WINDOW_SIZE;
}
else {
// end data should be copied? or better other way?
//
//
if (end_ptr - input_ptr <= LZ_MAX_COPY_DATA_LENGHT) {
*output_ptr = 0x80 + end_ptr - input_ptr;
output_ptr++;
memcpy(output_ptr, input_ptr, end_ptr - input_ptr);
output_ptr += end_ptr - input_ptr;
}
else {
*output_ptr = 0x80 + LZ_MAX_COPY_DATA_LENGHT;
output_ptr++;
memcpy(output_ptr, input_ptr, LZ_MAX_COPY_DATA_LENGHT);
output_ptr += LZ_MAX_COPY_DATA_LENGHT;
*output_ptr = 0x80 + end_ptr - input_ptr - LZ_MAX_COPY_DATA_LENGHT;
output_ptr++;
memcpy(output_ptr, input_ptr, end_ptr - input_ptr - LZ_MAX_COPY_DATA_LENGHT);
output_ptr += end_ptr - input_ptr - LZ_MAX_COPY_DATA_LENGHT;
}
*size_of_compressed_data = output_ptr - output;
return output;
}
}
*size_of_compressed_data = output_ptr - output;
return output;
}
UCHAR* lz_decompress(UCHAR* input, UINT size_of_compressed_data, UINT size_of_decompressed_data) {
UCHAR* output = (UCHAR*)malloc(size_of_decompressed_data);
UCHAR* output_ptr = output;
UCHAR* end_ptr = output + size_of_decompressed_data;
UCHAR* input_ptr = input;
UCHAR* dict_ptr = output_ptr;
// Optimize this code for small data ?
if (size_of_compressed_data < LZ_MAX_COPY_DATA_LENGHT) {
memcpy(output_ptr, input, size_of_compressed_data);
return output_ptr;
}
else {
memcpy(output_ptr, input_ptr, LZ_MAX_COPY_DATA_LENGHT);
output_ptr += LZ_MAX_COPY_DATA_LENGHT;
input_ptr += LZ_MAX_COPY_DATA_LENGHT;
}
while (output_ptr < end_ptr) {
// is data
if (*(BYTE*)input_ptr < 0) {
UINT data_len = *(UCHAR*)input_ptr - 0x80;
input_ptr++;
memcpy(output_ptr, input_ptr, data_len);
output_ptr += data_len;
input_ptr += data_len;
}
else { // is reference
lz_op_code* opcode_p = (lz_op_code*)input_ptr;
input_ptr += sizeof(lz_op_code);
UINT distance = opcode_p->dist0 + (opcode_p->dist1 << 8);
// check if memcpy can be broken in case of (output_ptr + opcode_p->lenght > output_ptr - opcode_p->distance)
memcpy(output_ptr, dict_ptr + distance, opcode_p->lenght);
output_ptr += opcode_p->lenght;
*output_ptr = opcode_p->symbol;
output_ptr++;
}
if (output_ptr - dict_ptr >= LZ_SLIDING_WINDOW_SIZE)
dict_ptr = output_ptr - LZ_SLIDING_WINDOW_SIZE;
}
return output;
}
void Test_decompression_algorithm(
UCHAR* (*compressor_fn)(UCHAR* input, UINT size_of_data, UINT* size_of_compressed_data),
UCHAR* (*decompressor_fn)(UCHAR* input, UINT size_of_compressed_data, UINT size_of_decompressed_data),
char* data_file_name,
char* compressed_file_name
)
{
DWORD a = 0;
HANDLE f = CreateFileA(data_file_name, GENERIC_READ, NULL, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (GetLastError())
{
printf("File open Error: %d\n", GetLastError());
return;
}
DWORD file_size = GetFileSize(f, NULL);