-
Notifications
You must be signed in to change notification settings - Fork 1
/
WinlinkSupport.vb
1205 lines (1072 loc) · 35.2 KB
/
WinlinkSupport.vb
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
Option Strict Off
Imports System.Collections.Generic
Imports System.Text
Imports System.Security.Cryptography
'
' This class implements the Compression, and CRC functions previously provided
' in a no managed .DLL
'
' The Class names are:
' Compression
' Crc
Public Class Compression
'
' lzh .net Class library v1.0
' Ported to C# by Peter Woods 08/22/2008 from the original
' Pascal version.
' Ported to VB by Peter Woods 10/20/2010 from the C# code
'
'--------------Original Header Below-----------------
'
' LZHUF.C English version 1.0
' Based on Japanese version 29-NOV-1988
' LZSS coded by Haruhiko OKUMURA
' Adaptive Huffman Coding coded by Haruyasu YOSHIZAKI
' Edited and translated to English by Kenji RIKITAKE
' Converted to Turbo Pascal 5.0
' by Peter Sawatzki with assistance of Wayne Sullivan
'
'
' Constants
'
'
' Synchonization object to support multi threading
'
Shared LZSync As New Object()
'
' Define the string buffer size. Note, was 4096 in the original pascal version.
'
Const N As Int32 = 2048
'
' Define the size of the look-ahead buffer
'
Const F As Int32 = 60
'
' Define the Threshold
'
Const Threshold As Int32 = 2
'
' Set the NULL value to the size of the string buffer
'
Const NodeNIL As Int32 = N
'
' Huffman coding parameters
'
Const NChar As Int32 = (256 - Threshold) + F
'
' Define the table size
' character code = 0..N_CHAR-1
'
Const T As Int32 = (NChar * 2) - 1
'
' Define the Root position
'
Const R As Int32 = T - 1
'
' MAx frequency is 32678
'
Const MaxFreq As Int32 = &H8000
'
' CRC polynomial
'
Const CRCPoly As Int32 = &H18408
'
' Allocate fixed size buffers
'
'
' textBuf [0..N+F-2] Byte->Byte
'
Const TBSize As Int32 = N + F - 2
Shared textBufMask As Int32 = &HFF
Shared textBuf As Byte() = New Byte(TBSize + 1) {}
'
' lSon [0..N] Word->UInt16
'
Shared lSon As Int32() = New Int32(N + 1) {}
'
' dad [0..N] Word->UInt16
'
Shared dad As Int32() = New Int32(N + 1) {}
'
' rSon [0..N+256] Word->UInt16
'
Shared rSon As Int32() = New Int32(N + 256 + 1) {}
'
' Cumulative freq table [0..T] Word->UInt16
'
Shared freq As Int32() = New Int32(T + 1) {}
'
' Pointing children nodes (son[], son[] + 1) [0..T-1] Word->UInt16
'
Shared son As Int32() = New Int32(T) {}
'
' Pointing parent nodes use [0..T-1]. Area [T..(T + N_CHAR - 1)] are pointers for leaves
' Word->UInt16
'
Shared parent As Int32() = New Int32(T + NChar) {}
'
' Tables for encoding/decoding upper 6 bits of sliding dictionary pointer
' encoder table}
'
' Position Encode length
'
Shared p_len As Byte() = {&H3, &H4, &H4, &H4, &H5, &H5, _
&H5, &H5, &H5, &H5, &H5, &H5, _
&H6, &H6, &H6, &H6, &H6, &H6, _
&H6, &H6, &H6, &H6, &H6, &H6, _
&H7, &H7, &H7, &H7, &H7, &H7, _
&H7, &H7, &H7, &H7, &H7, &H7, _
&H7, &H7, &H7, &H7, &H7, &H7, _
&H7, &H7, &H7, &H7, &H7, &H7, _
&H8, &H8, &H8, &H8, &H8, &H8, _
&H8, &H8, &H8, &H8, &H8, &H8, _
&H8, &H8, &H8, &H8}
'
' Position Encode Table
'
Shared p_code As Int32() = {&H0, &H20, &H30, &H40, &H50, &H58, _
&H60, &H68, &H70, &H78, &H80, &H88, _
&H90, &H94, &H98, &H9C, &HA0, &HA4, _
&HA8, &HAC, &HB0, &HB4, &HB8, &HBC, _
&HC0, &HC2, &HC4, &HC6, &HC8, &HCA, _
&HCC, &HCE, &HD0, &HD2, &HD4, &HD6, _
&HD8, &HDA, &HDC, &HDE, &HE0, &HE2, _
&HE4, &HE6, &HE8, &HEA, &HEC, &HEE, _
&HF0, &HF1, &HF2, &HF3, &HF4, &HF5, _
&HF6, &HF7, &HF8, &HF9, &HFA, &HFB, _
&HFC, &HFD, &HFE, &HFF}
'
' Position decode table
'
Shared d_code As Int32() = {&H0, &H0, &H0, &H0, &H0, &H0, _
&H0, &H0, &H0, &H0, &H0, &H0, _
&H0, &H0, &H0, &H0, &H0, &H0, _
&H0, &H0, &H0, &H0, &H0, &H0, _
&H0, &H0, &H0, &H0, &H0, &H0, _
&H0, &H0, &H1, &H1, &H1, &H1, _
&H1, &H1, &H1, &H1, &H1, &H1, _
&H1, &H1, &H1, &H1, &H1, &H1, _
&H2, &H2, &H2, &H2, &H2, &H2, _
&H2, &H2, &H2, &H2, &H2, &H2, _
&H2, &H2, &H2, &H2, &H3, &H3, _
&H3, &H3, &H3, &H3, &H3, &H3, _
&H3, &H3, &H3, &H3, &H3, &H3, _
&H3, &H3, &H4, &H4, &H4, &H4, _
&H4, &H4, &H4, &H4, &H5, &H5, _
&H5, &H5, &H5, &H5, &H5, &H5, _
&H6, &H6, &H6, &H6, &H6, &H6, _
&H6, &H6, &H7, &H7, &H7, &H7, _
&H7, &H7, &H7, &H7, &H8, &H8, _
&H8, &H8, &H8, &H8, &H8, &H8, _
&H9, &H9, &H9, &H9, &H9, &H9, _
&H9, &H9, &HA, &HA, &HA, &HA, _
&HA, &HA, &HA, &HA, &HB, &HB, _
&HB, &HB, &HB, &HB, &HB, &HB, _
&HC, &HC, &HC, &HC, &HD, &HD, _
&HD, &HD, &HE, &HE, &HE, &HE, _
&HF, &HF, &HF, &HF, &H10, &H10, _
&H10, &H10, &H11, &H11, &H11, &H11, _
&H12, &H12, &H12, &H12, &H13, &H13, _
&H13, &H13, &H14, &H14, &H14, &H14, _
&H15, &H15, &H15, &H15, &H16, &H16, _
&H16, &H16, &H17, &H17, &H17, &H17, _
&H18, &H18, &H19, &H19, &H1A, &H1A, _
&H1B, &H1B, &H1C, &H1C, &H1D, &H1D, _
&H1E, &H1E, &H1F, &H1F, &H20, &H20, _
&H21, &H21, &H22, &H22, &H23, &H23, _
&H24, &H24, &H25, &H25, &H26, &H26, _
&H27, &H27, &H28, &H28, &H29, &H29, _
&H2A, &H2A, &H2B, &H2B, &H2C, &H2C, _
&H2D, &H2D, &H2E, &H2E, &H2F, &H2F, _
&H30, &H31, &H32, &H33, &H34, &H35, _
&H36, &H37, &H38, &H39, &H3A, &H3B, _
&H3C, &H3D, &H3E, &H3F}
'
' Position decode length
'
Shared d_len As Int32() = {&H3, &H3, &H3, &H3, &H3, &H3, _
&H3, &H3, &H3, &H3, &H3, &H3, _
&H3, &H3, &H3, &H3, &H3, &H3, _
&H3, &H3, &H3, &H3, &H3, &H3, _
&H3, &H3, &H3, &H3, &H3, &H3, _
&H3, &H3, &H4, &H4, &H4, &H4, _
&H4, &H4, &H4, &H4, &H4, &H4, _
&H4, &H4, &H4, &H4, &H4, &H4, _
&H4, &H4, &H4, &H4, &H4, &H4, _
&H4, &H4, &H4, &H4, &H4, &H4, _
&H4, &H4, &H4, &H4, &H4, &H4, _
&H4, &H4, &H4, &H4, &H4, &H4, _
&H4, &H4, &H4, &H4, &H4, &H4, _
&H4, &H4, &H5, &H5, &H5, &H5, _
&H5, &H5, &H5, &H5, &H5, &H5, _
&H5, &H5, &H5, &H5, &H5, &H5, _
&H5, &H5, &H5, &H5, &H5, &H5, _
&H5, &H5, &H5, &H5, &H5, &H5, _
&H5, &H5, &H5, &H5, &H5, &H5, _
&H5, &H5, &H5, &H5, &H5, &H5, _
&H5, &H5, &H5, &H5, &H5, &H5, _
&H5, &H5, &H5, &H5, &H5, &H5, _
&H5, &H5, &H5, &H5, &H5, &H5, _
&H5, &H5, &H5, &H5, &H5, &H5, _
&H6, &H6, &H6, &H6, &H6, &H6, _
&H6, &H6, &H6, &H6, &H6, &H6, _
&H6, &H6, &H6, &H6, &H6, &H6, _
&H6, &H6, &H6, &H6, &H6, &H6, _
&H6, &H6, &H6, &H6, &H6, &H6, _
&H6, &H6, &H6, &H6, &H6, &H6, _
&H6, &H6, &H6, &H6, &H6, &H6, _
&H6, &H6, &H6, &H6, &H6, &H6, _
&H7, &H7, &H7, &H7, &H7, &H7, _
&H7, &H7, &H7, &H7, &H7, &H7, _
&H7, &H7, &H7, &H7, &H7, &H7, _
&H7, &H7, &H7, &H7, &H7, &H7, _
&H7, &H7, &H7, &H7, &H7, &H7, _
&H7, &H7, &H7, &H7, &H7, &H7, _
&H7, &H7, &H7, &H7, &H7, &H7, _
&H7, &H7, &H7, &H7, &H7, &H7, _
&H8, &H8, &H8, &H8, &H8, &H8, _
&H8, &H8, &H8, &H8, &H8, &H8, _
&H8, &H8, &H8, &H8}
'
' CRC Table
' Word->UInt16
'
Shared CRCMask As Int32 = &HFFFF
Shared CRCTable As Int32() = {&H0, &H1021, &H2042, &H3063, &H4084, &H50A5, _
&H60C6, &H70E7, &H8108, &H9129, &HA14A, &HB16B, _
&HC18C, &HD1AD, &HE1CE, &HF1EF, &H1231, &H210, _
&H3273, &H2252, &H52B5, &H4294, &H72F7, &H62D6, _
&H9339, &H8318, &HB37B, &HA35A, &HD3BD, &HC39C, _
&HF3FF, &HE3DE, &H2462, &H3443, &H420, &H1401, _
&H64E6, &H74C7, &H44A4, &H5485, &HA56A, &HB54B, _
&H8528, &H9509, &HE5EE, &HF5CF, &HC5AC, &HD58D, _
&H3653, &H2672, &H1611, &H630, &H76D7, &H66F6, _
&H5695, &H46B4, &HB75B, &HA77A, &H9719, &H8738, _
&HF7DF, &HE7FE, &HD79D, &HC7BC, &H48C4, &H58E5, _
&H6886, &H78A7, &H840, &H1861, &H2802, &H3823, _
&HC9CC, &HD9ED, &HE98E, &HF9AF, &H8948, &H9969, _
&HA90A, &HB92B, &H5AF5, &H4AD4, &H7AB7, &H6A96, _
&H1A71, &HA50, &H3A33, &H2A12, &HDBFD, &HCBDC, _
&HFBBF, &HEB9E, &H9B79, &H8B58, &HBB3B, &HAB1A, _
&H6CA6, &H7C87, &H4CE4, &H5CC5, &H2C22, &H3C03, _
&HC60, &H1C41, &HEDAE, &HFD8F, &HCDEC, &HDDCD, _
&HAD2A, &HBD0B, &H8D68, &H9D49, &H7E97, &H6EB6, _
&H5ED5, &H4EF4, &H3E13, &H2E32, &H1E51, &HE70, _
&HFF9F, &HEFBE, &HDFDD, &HCFFC, &HBF1B, &HAF3A, _
&H9F59, &H8F78, &H9188, &H81A9, &HB1CA, &HA1EB, _
&HD10C, &HC12D, &HF14E, &HE16F, &H1080, &HA1, _
&H30C2, &H20E3, &H5004, &H4025, &H7046, &H6067, _
&H83B9, &H9398, &HA3FB, &HB3DA, &HC33D, &HD31C, _
&HE37F, &HF35E, &H2B1, &H1290, &H22F3, &H32D2, _
&H4235, &H5214, &H6277, &H7256, &HB5EA, &HA5CB, _
&H95A8, &H8589, &HF56E, &HE54F, &HD52C, &HC50D, _
&H34E2, &H24C3, &H14A0, &H481, &H7466, &H6447, _
&H5424, &H4405, &HA7DB, &HB7FA, &H8799, &H97B8, _
&HE75F, &HF77E, &HC71D, &HD73C, &H26D3, &H36F2, _
&H691, &H16B0, &H6657, &H7676, &H4615, &H5634, _
&HD94C, &HC96D, &HF90E, &HE92F, &H99C8, &H89E9, _
&HB98A, &HA9AB, &H5844, &H4865, &H7806, &H6827, _
&H18C0, &H8E1, &H3882, &H28A3, &HCB7D, &HDB5C, _
&HEB3F, &HFB1E, &H8BF9, &H9BD8, &HABBB, &HBB9A, _
&H4A75, &H5A54, &H6A37, &H7A16, &HAF1, &H1AD0, _
&H2AB3, &H3A92, &HFD2E, &HED0F, &HDD6C, &HCD4D, _
&HBDAA, &HAD8B, &H9DE8, &H8DC9, &H7C26, &H6C07, _
&H5C64, &H4C45, &H3CA2, &H2C83, &H1CE0, &HCC1, _
&HEF1F, &HFF3E, &HCF5D, &HDF7C, &HAF9B, &HBFBA, _
&H8FD9, &H9FF8, &H6E17, &H7E36, &H4E55, &H5E74, _
&H2E93, &H3EB2, &HED1, &H1EF0}
'
' Processing buffers
'
Shared inBuf As Byte() = Nothing
Shared outBuf As Byte() = Nothing
Shared inPtr As Int32 = 0
Shared inEnd As Int32 = 0
Shared outPtr As Int32 = 0
Shared CRC As Int32
Shared EncDec As Boolean = False ' true for Encode, false for Decode
Shared getBuf As Int32 = 0
Shared getLen As Int32 = 0
Shared putBuf As Int32 = 0
Shared putLen As Int32 = 0
Shared textSize As Int32 = 0
Shared codeSize As Int32 = 0
Shared matchPosition As Int32 = 0
Shared matchLength As Int32 = 0
'
' Main entry points
'
Public Shared Function Encode(ByVal iBuf As Byte(), ByRef oBuf As Byte(), ByVal prependCRC As Boolean) As Int32
Dim tmp As Int32
Encode = Encode(iBuf, oBuf, tmp, prependCRC)
End Function
Public Shared Function Encode(ByVal iBuf As Byte(), ByRef oBuf As Byte(), ByVal retCRC As Int32) As Int32
Encode = Encode(iBuf, oBuf, retCRC, False)
End Function
Public Shared Function Encode(ByVal iBuf As Byte(), ByRef oBuf As Byte(), ByVal retCRC As Int32, ByVal prependCRC As Boolean) As Int32
'
' Encoding/Compressing
'
Dim i As Int32
Dim c As Int32
Dim len As Int32
Dim r As Int32
Dim s As Int32
Dim last_match_length As Int32
Dim j As Int32 = 0
SyncLock LZSync
'
' The lock makes the code thread-safe
'
' Allocate work buffers to hold the incoming message.
inBuf = New Byte(iBuf.Length + 100) {}
outBuf = New Byte(iBuf.Length * 2 + 10000) {}
Init()
EncDec = True
For i = 0 To iBuf.Length - 1
inBuf(inEnd) = iBuf(i)
inEnd += 1
Next
putc(CType(inEnd And &HFF, Byte))
putc(CType((inEnd >> 8) And &HFF, Byte))
putc(CType((inEnd >> 16) And &HFF, Byte))
putc(CType((inEnd >> 24) And &HFF, Byte))
codeSize += 4
If inEnd = 0 Then
oBuf = New Byte(-1) {}
retCRC = 0
' Free the work buffers
inBuf = Nothing
outBuf = Nothing
Return codeSize
End If
textSize = 0
StartHuff()
InitTree()
s = 0
r = N - F
For i = 0 To r - 1
'fillchar(text_buf[0],r,' ');
textBuf(i) = CType(&H20, Byte)
Next
len = 0
While (len < F) AndAlso (inPtr < inEnd)
textBuf(r + len) = CType(getc(), Byte) And &HFF
len += 1
End While
textSize = len
For i = 1 To F
InsertNode(r - i)
Next
InsertNode(r)
Do
If matchLength > len Then
matchLength = len
End If
If matchLength <= Threshold Then
matchLength = 1
EncodeChar(textBuf(r))
Else
EncodeChar((255 - Threshold) + matchLength)
EncodePosition(matchPosition)
End If
last_match_length = matchLength
i = 0
While (i < last_match_length) AndAlso (inPtr < inEnd)
i += 1
DeleteNode(s)
c = getc()
textBuf(s) = CType(c, Byte) And &HFF
If s < F - 1 Then
textBuf(s + N) = CType(c, Byte)
End If
s = (s + 1) And (N - 1)
r = (r + 1) And (N - 1)
InsertNode(r)
End While
textSize += i
While i < last_match_length
i += 1
DeleteNode(s)
s = (s + 1) And (N - 1)
r = (r + 1) And (N - 1)
len -= 1
If len > 0 Then
InsertNode(r)
End If
End While
Loop While len > 0
EncodeEnd()
retCRC = GetCRC()
'
' Create a buffer to hold the results
'
If prependCRC Then
oBuf = New Byte(codeSize + 1) {}
oBuf(0) = CType((retCRC >> 8) And &HFF, Byte)
oBuf(1) = CType(retCRC And &HFF, Byte)
j = 2
Else
oBuf = New Byte(codeSize - 1) {}
j = 0
End If
For i = 0 To codeSize - 1
oBuf(j) = outBuf(i)
j += 1
Next
If prependCRC Then
codeSize += 2
End If
Encode = codeSize
' Free the work buffers
inBuf = Nothing
outBuf = Nothing
End SyncLock
End Function
Public Shared Function Decode(ByVal iBuf As Byte(), ByRef oBuf As Byte(), ByVal checkCRC As Boolean, ByVal intExpectedUncompressedSize As Integer) As Int32
Dim tmp As UInt16 = 0
Return DecodeWork(iBuf, oBuf, tmp, checkCRC, intExpectedUncompressedSize)
End Function
Public Shared Function Decode(ByVal iBuf As Byte(), ByRef oBuf As Byte(), ByVal retCRC As UInt16, ByVal intExpectedUncompressedSize As Integer) As Int32
Return DecodeWork(iBuf, oBuf, retCRC, False, intExpectedUncompressedSize)
End Function
Public Shared Function DecodeWork(ByVal iBuf As Byte(), ByRef oBuf As Byte(), ByVal retCRC As UInt16, ByVal checkCRC As Boolean, ByVal intExpectedUncompressedSize As Integer) As Int32
'
' Decoding/Uncompressing
'
Dim i As Int32
Dim j As Int32
Dim k As Int32
Dim r As Int32
Dim c As Int32
Dim count As Int32
Dim iBufStart As Int32 = 0
Dim suppliedCRC As Int32 = 0
SyncLock LZSync
'
' The lock makes the code thread-safe
'
' Allocate work buffers to hold the incoming message.
inBuf = New Byte(iBuf.Length + 100) {}
outBuf = New Byte(intExpectedUncompressedSize + 10000) {}
EncDec = False
Init()
If checkCRC Then
iBufStart = 2
suppliedCRC = CType(iBuf(1), Int32) And &HFF
suppliedCRC = suppliedCRC Or (CType(iBuf(0), Int32) << 8)
End If
For i = iBufStart To iBuf.Length - 1
'
' Load the user supplied buffer into the internal processing buffer
'
inBuf(inEnd) = iBuf(i)
inEnd += 1
Next
'
' Read size of original text
'
textSize = getc()
textSize = textSize Or (getc() << 8)
textSize = textSize Or (getc() << 16)
textSize = textSize Or (getc() << 24)
If textSize = 0 Then
oBuf = New Byte(-1) {}
retCRC = 0
DecodeWork = textSize
' Free the work buffers
inBuf = Nothing
outBuf = Nothing
Return textSize
End If
StartHuff()
For i = 0 To (N - F) - 1
'fillchar(text_buf[0],N-F,' ');
textBuf(i) = CType(&H20, Byte)
Next
r = N - F
count = 0
While count < textSize
c = DecodeChar()
If c < 256 Then
putc(CType(c And &HFF, Byte))
textBuf(r) = CType(c And &HFF, Byte)
r = (r + 1) And (N - 1)
count += 1
Else
i = ((r - DecodePosition()) - 1) And (N - 1)
j = (c - 255) + Threshold
For k = 0 To j - 1
c = CType(textBuf((i + k) And (N - 1)), Int32)
putc(CType(c And &HFF, Byte))
textBuf(r) = CType(c And &HFF, Byte)
r = (r + 1) And (N - 1)
count += 1
Next
End If
End While
oBuf = New Byte(count - 1) {}
retCRC = GetCRC()
For i = 0 To count - 1
oBuf(i) = outBuf(i)
Next
If checkCRC AndAlso (retCRC <> suppliedCRC) Then
'
' Check the CRC. Return 0 if mismatch
'
count = 0
End If
' Free the work buffers
inBuf = Nothing
outBuf = Nothing
Return count
End SyncLock
End Function
Public Shared Function GetCRC() As Int32
Return CType(Swap(CRC And &HFFFF), Int32)
End Function
Private Shared Sub Init()
'
' Initialize all structures pointers and counters
'
inPtr = 0
inEnd = 0
outPtr = 0
'outEnd = 0;
getBuf = 0
getLen = 0
putBuf = 0
putLen = 0
textSize = 0
codeSize = 0
matchPosition = 0
matchLength = 0
InitArrayB(inBuf)
InitArrayB(outBuf)
InitArrayB(textBuf)
InitArrayI(lSon)
InitArrayI(dad)
InitArrayI(rSon)
InitArrayI(freq)
InitArrayI(parent)
InitArrayI(son)
CRC = 0
End Sub
Private Shared Sub DoCRC(ByVal c As Int32)
'
' Update running tally of CRC
'
CRC = ((CRC << 8) Xor CRCTable((CRC >> 8) Xor c)) And CRCMask
End Sub
Private Shared Function getc() As Integer
'
' Get a character from the input buffer
'
Dim c As Int32 = 0
If inPtr < inEnd Then
c = CType(inBuf(inPtr), Int32) And &HFF
inPtr += 1
If Not EncDec Then
'
' Do CRC on input for Decode
'
DoCRC(c)
End If
End If
Return c
End Function
Private Shared Sub putc(ByVal c As Int32)
'
' Write a character from the output buffer
'
outBuf(outPtr) = CType(c And &HFF, Byte)
outPtr += 1
If EncDec Then
'
' Do CRC on output for Encode
'
DoCRC(c)
End If
End Sub
Private Shared Sub InitTree()
'
' Initializing tree
'
Dim i As Int32
For i = N + 1 To N + 256
' {root
rSon(i) = NodeNIL
Next
For i = 0 To N - 1
'{node}
dad(i) = NodeNIL
Next
End Sub
Private Shared Sub InsertNode(ByVal r As Int32)
'
' Insert nodes to the tree
'
Dim i As Int32
Dim p As Int32
Dim geq As Boolean
Dim c As Int32
geq = True
p = N + 1 + textBuf(r)
rSon(r) = NodeNIL
lSon(r) = NodeNIL
matchLength = 0
While True
If geq Then
If rSon(p) = NodeNIL Then
rSon(p) = r
dad(r) = p
Return
Else
p = rSon(p)
End If
Else
If lSon(p) = NodeNIL Then
lSon(p) = r
dad(r) = p
Return
Else
p = lSon(p)
End If
End If
i = 1
While (i < F) AndAlso (textBuf(r + i) = textBuf(p + i))
i += 1
End While
geq = (textBuf(r + i) >= textBuf(p + i)) OrElse (i = F)
If i > Threshold Then
If i > matchLength Then
matchPosition = ((r - p) And (N - 1)) - 1
matchLength = i
If matchLength >= F Then
Exit While
End If
End If
If i = matchLength Then
c = ((r - p) And (N - 1)) - 1
If c < matchPosition Then
matchPosition = c
End If
End If
End If
End While
dad(r) = dad(p)
lSon(r) = lSon(p)
rSon(r) = rSon(p)
dad(lSon(p)) = r
dad(rSon(p)) = r
If rSon(dad(p)) = p Then
rSon(dad(p)) = r
Else
lSon(dad(p)) = r
End If
dad(p) = NodeNIL
' remove p
End Sub
Private Shared Sub DeleteNode(ByVal p As Int32)
'
' Delete node from the tree
'
Dim q As Int32
If dad(p) = NodeNIL Then
' unregistered
Return
End If
If rSon(p) = NodeNIL Then
q = lSon(p)
Else
If lSon(p) = NodeNIL Then
q = rSon(p)
Else
q = lSon(p)
If rSon(q) <> NodeNIL Then
Do
q = rSon(q)
Loop While rSon(q) <> NodeNIL
rSon(dad(q)) = lSon(q)
dad(lSon(q)) = dad(q)
lSon(q) = lSon(p)
dad(lSon(p)) = q
End If
rSon(q) = rSon(p)
dad(rSon(p)) = q
End If
End If
dad(q) = dad(p)
If rSon(dad(p)) = p Then
rSon(dad(p)) = q
Else
lSon(dad(p)) = q
End If
dad(p) = NodeNIL
End Sub
Private Shared Function GetBit() As Int32
'
' Get one bit
'
Dim retVal As Int32
While getLen <= 8
getBuf = (getBuf Or (getc() << (8 - getLen))) And &HFFFF
getLen += 8
End While
retVal = (getBuf >> 15) And &H1
getBuf = (getBuf << 1) And &HFFFF
getLen -= 1
Return retVal
End Function
Private Shared Function GetByte() As Int32
'
' Get one byte
'
Dim retVal As Int32
While getLen <= 8
getBuf = (getBuf Or (getc() << (8 - getLen))) And &HFFFF
getLen += 8
End While
retVal = Hi(getBuf) And &HFF
getBuf = (getBuf << 8) And &HFFFF
getLen -= 8
Return retVal
End Function
Private Shared Sub Putcode(ByVal n As Int32, ByVal c As Int32)
'
' Output 'n' bits
'
putBuf = (putBuf Or (c >> putLen)) And &HFFFF
putLen += n
If putLen >= 8 Then
putc(Hi(putBuf) And &HFF)
putLen -= 8
If putLen >= 8 Then
putc(Lo(putBuf) And &HFF)
codeSize += 2
putLen -= 8
putBuf = (c << (n - putLen)) And &HFFFF
Else
putBuf = Swap(putBuf And &HFF)
codeSize += 1
End If
End If
End Sub
Private Shared Sub StartHuff()
'
' Initialize freq tree
'
Dim i As Int32
Dim j As Int32
For i = 0 To NChar - 1
freq(i) = 1
son(i) = i + T
parent(i + T) = i
Next
i = 0
j = NChar
While j <= R
freq(j) = (freq(i) + freq(i + 1)) And &HFFFF
son(j) = i
parent(i) = j
parent(i + 1) = j
i += 2
j += 1
End While
freq(T) = &HFFFF
parent(R) = 0
End Sub
Private Shared Sub reconst()
'
' Reconstruct freq tree
'
Dim i As Int32
Dim j As Int32
Dim k As Int32
Dim f As Int32
Dim n As Int32
'
' Halven cumulative freq for leaf nodes
'
j = 0
For i = 0 To T - 1
If son(i) >= T Then
freq(j) = (freq(i) + 1) >> 1
son(j) = son(i)
j += 1
End If
Next
'
' Make a tree : first, connect children nodes
'
i = 0
j = NChar
While j < T
k = i + 1
f = (freq(i) + freq(k)) And &HFFFF
freq(j) = f
k = j - 1
While f < freq(k)
k -= 1
End While
k += 1
'
' Original code segment
' l:= (j-k)*2;
' move(freq[k],freq[k+1],l);
' freq[k]:= f;
' move(son[k],son[k+1],l);
' son[k]:= i;
'
' New code segment.
'
For n = j To k + 1 Step -1
freq(n) = freq(n - 1)
son(n) = son(n - 1)
Next
freq(k) = f
son(k) = i
i += 2
j += 1
End While
'
' Connect parent nodes
'
For i = 0 To T - 1
k = son(i)
parent(k) = i
If k < T Then
parent(k + 1) = i
End If
Next
End Sub
Private Shared Sub update(ByVal c As Int32)
'
' Update freq tree
'
Dim i As Int32
Dim j As Int32
Dim k As Int32
Dim n As Int32
If freq(R) = MaxFreq Then
reconst()
End If
c = parent(c + T)
Do
freq(c) += 1
k = freq(c)
'
' Swap nodes to keep the tree freq-ordered}
'
n = c + 1
If k > freq(n) Then
While k > freq(n + 1)
n += 1
End While
freq(c) = freq(n)
freq(n) = k
i = son(c)
parent(i) = n
If i < T Then
parent(i + 1) = n
End If
j = son(n)
son(n) = i
parent(j) = c
If j < T Then
parent(j + 1) = c
End If
son(c) = j
c = n
End If
c = parent(c)
Loop While c <> 0
' do it until reaching the root
End Sub
Private Shared Sub EncodeChar(ByVal c As Int32)
Dim code As Int32
Dim len As Byte
Dim k As Int32
code = 0
len = 0
k = parent(c + T)
'