forked from Wargus/stargus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhuff.cpp
1120 lines (955 loc) · 42.6 KB
/
huff.cpp
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
/*****************************************************************************/
/* huffman.cpp Copyright (c) Ladislav Zezula 1998-2003 */
/*---------------------------------------------------------------------------*/
/* This module contains Huffmann (de)compression methods */
/* */
/* Authors : Ladislav Zezula ([email protected]) */
/* ShadowFlare ([email protected]) */
/* */
/*---------------------------------------------------------------------------*/
/* Date Ver Who Comment */
/* -------- ---- --- ------- */
/* xx.xx.xx 1.00 Lad The first version of dcmp.cpp */
/* 03.05.03 1.00 Lad Added compression methods */
/* 19.11.03 1.01 Dan Big endian handling */
/* 08.12.03 2.01 Dan High-memory handling (> 0x80000000) */
/*****************************************************************************/
#include <assert.h>
#include <string.h>
#include "huff.h"
THTreeItem * gcpFirst, * gpFirst, * gcpItem3054, * gpItem3054;
#define PTR_VALID(ptr) ((ptr) != gcpFirst && (ptr) != gcpItem3054)
#define PTR_INVALID(ptr) (!PTR_VALID(ptr))
#define PTR_INVALID_OR_NULL(ptr) (0 == (ptr) || PTR_INVALID(ptr))
//-----------------------------------------------------------------------------
// Methods of the THTreeItem struct
// 1501DB70
THTreeItem * THTreeItem::Call1501DB70(THTreeItem * pLast)
{
if(pLast == NULL)
pLast = this + 1;
return pLast;
}
// Gets previous Huffman tree item (?)
THTreeItem * THTreeItem::GetPrevItem(SIntPtr value)
{
if(PTR_INVALID(prev))
return PTR_NOT(prev);
if(value == -1 || PTR_INVALID((THTreeItem *) value))
value = (SIntPtr)(this - next->prev);
return prev + value;
}
// 1500F5E0
void THTreeItem::ClearItemLinks()
{
next = prev = NULL;
}
// 1500BC90
void THTreeItem::RemoveItem()
{
THTreeItem * pTemp; // EDX
if(next != NULL)
{
pTemp = prev;
if(PTR_INVALID(pTemp))
pTemp = PTR_NOT(pTemp);
else
pTemp += (this - next->prev);
pTemp->next = next;
next->prev = prev;
next = prev = NULL;
}
}
//-----------------------------------------------------------------------------
// TOutputStream functions
void TOutputStream::PutBits(unsigned long dwBuff, unsigned int nPutBits)
{
dwBitBuff |= (dwBuff << nBits);
nBits += nPutBits;
// Flush completed bytes
while(nBits >= 8)
{
if(cbOutSize != 0)
{
*pbOutPos++ = (unsigned char)dwBitBuff;
cbOutSize--;
}
dwBitBuff >>= 8;
nBits -= 8;
}
}
//-----------------------------------------------------------------------------
// TInputStream functions
// Gets one bit from input stream
unsigned long TInputStream::GetBit()
{
unsigned long dwOneBit = 0;
// Ensure that the input stream is reloaded, if there are no bits left
if(BitCount == 0)
{
// Refill the bit buffer
BitBuffer = *pbInBuffer++;
BitCount = 8;
}
// Copy the bit from bit buffer to the variable
dwOneBit = (BitBuffer & 0x01);
BitBuffer >>= 1;
BitCount--;
return dwOneBit;
}
// Gets 7 bits from the stream. DOES NOT remove the bits from input stream
unsigned long TInputStream::Get7Bits()
{
unsigned long dwReloadByte = 0;
// If there is not enough bits to get the value,
// we have to add 8 more bits from the input buffer
if(BitCount < 7)
{
dwReloadByte = *pbInBuffer++;
BitBuffer |= dwReloadByte << BitCount;
BitCount += 8;
}
// Return the first available 7 bits. DO NOT remove them from the input stream
return (BitBuffer & 0x7F);
}
// Gets the whole byte from the input stream.
unsigned long TInputStream::Get8Bits()
{
unsigned long dwReloadByte = 0;
unsigned long dwOneByte = 0;
// If there is not enough bits to get the value,
// we have to add 8 more bits from the input buffer
if(BitCount < 8)
{
dwReloadByte = *pbInBuffer++;
BitBuffer |= dwReloadByte << BitCount;
BitCount += 8;
}
// Return the lowest 8 its
dwOneByte = (BitBuffer & 0xFF);
BitBuffer >>= 8;
BitCount -= 8;
return dwOneByte;
}
void TInputStream::SkipBits(unsigned int dwBitsToSkip)
{
unsigned long dwReloadByte = 0;
// If there is not enough bits in the buffer,
// we have to add 8 more bits from the input buffer
if(BitCount < dwBitsToSkip)
{
dwReloadByte = *pbInBuffer++;
BitBuffer |= dwReloadByte << BitCount;
BitCount += 8;
}
// Skip the remaining bits
BitBuffer >>= dwBitsToSkip;
BitCount -= dwBitsToSkip;
}
//-----------------------------------------------------------------------------
// Functions for huffmann tree items
// Inserts item into the tree (?)
static void InsertItem(THTreeItem ** itemPtr, THTreeItem * item, unsigned long where, THTreeItem * item2)
{
THTreeItem * next = item->next; // EDI - next to the first item
THTreeItem * prev = item->prev; // ESI - prev to the first item
THTreeItem * prev2; // Pointer to previous item
THTreeItem * next2; // Pointer to the next item
// The same code like in RemoveItem(item);
if(next != 0) // If the first item already has next one
{
if(PTR_INVALID(prev))
prev = PTR_NOT(prev);
else
prev += (item - next->prev);
// 150083C1
// Remove the item from the tree
prev->next = next;
next->prev = prev;
// Invalidate 'prev' and 'next' pointer
item->next = 0;
item->prev = 0;
}
if(item2 == NULL) // EDX - If the second item is not entered,
item2 = PTR_PTR(&itemPtr[1]); // take the first tree item
switch(where)
{
case SWITCH_ITEMS : // Switch the two items
item->next = item2->next; // item2->next (Pointer to pointer to first)
item->prev = item2->next->prev;
item2->next->prev = item;
item2->next = item; // Set the first item
return;
case INSERT_ITEM: // Insert as the last item
item->next = item2; // Set next item (or pointer to pointer to first item)
item->prev = item2->prev; // Set prev item (or last item in the tree)
next2 = itemPtr[0];// Usually NULL
prev2 = item2->prev; // Prev item to the second (or last tree item)
if(PTR_INVALID(prev2))
{
prev2 = PTR_NOT(prev);
prev2->next = item;
item2->prev = item; // Next after last item
return;
}
if(PTR_INVALID(next2))
next2 = (THTreeItem *)(item2 - item2->next->prev);
prev2 += (uintptr_t) next2;
prev2->next = item;
item2->prev = item; // Set the next/last item
return;
default:
return;
}
}
//-----------------------------------------------------------------------------
// THuffmannTree class functions
THuffmannTree::THuffmannTree()
{
}
void THuffmannTree::InitTree(bool bCompression)
{
THTreeItem * pItem;
unsigned int nCount;
// Clear links for all the items in the tree
for(pItem = items0008, nCount = 0x203; nCount != 0; pItem++, nCount--)
pItem->ClearItemLinks();
gcpItem3054 = (THTreeItem *) &gcpItem3054;
pItem3050 = NULL;
pItem3054 = PTR_PTR(&pItem3054);
pItem3058 = gcpItem3054;
gpItem3054 = pItem3054;
gcpFirst = (THTreeItem *) &gcpFirst;
pItem305C = NULL;
pFirst = PTR_PTR(&pFirst);
pLast = gcpFirst;
gpFirst = pFirst;
offs0004 = 1;
nItems = 0;
// Clear all TQDecompress items. Do this only if preparing for decompression
if(bCompression == false)
{
for(nCount = 0; nCount < sizeof(qd3474) / sizeof(TQDecompress); nCount++)
qd3474[nCount].offs00 = 0;
}
}
// Builds Huffman tree. Called with the first 8 bits loaded from input stream
void THuffmannTree::BuildTree(unsigned int nCmpType)
{
unsigned long maxByte; // [ESP+10] - The greatest character found in table
THTreeItem ** itemPtr; // [ESP+14] - Pointer to Huffman tree item pointer array
unsigned char * byteArray; // [ESP+1C] - Pointer to unsigned char in Table1502A630
THTreeItem * child1;
unsigned long i; // egcs in linux doesn't like multiple for loops without an explicit i
// Loop while pointer has a valid value
while(PTR_VALID(pLast)) // ESI - Last entry
{
THTreeItem * temp; // EAX
if(pLast->next != NULL) // ESI->next
pLast->RemoveItem();
// EDI = &offs3054
pItem3058 = PTR_PTR(&pItem3054); // [EDI+4]
pLast->prev = pItem3058; // EAX
temp = PTR_PTR(&pItem3054)->GetPrevItem((SIntPtr)(&pItem3050));
temp->next = pLast;
pItem3054 = pLast;
}
// Clear all pointers in HTree item array
memset(items306C, 0, sizeof(items306C));
maxByte = 0; // Greatest character found init to zero.
itemPtr = (THTreeItem **)&items306C; // Pointer to current entry in HTree item pointer array
// Ensure we have low 8 bits only
nCmpType &= 0xFF;
byteArray = Table1502A630 + nCmpType * 258; // EDI also
for(i = 0; i < 0x100; i++, itemPtr++)
{
THTreeItem * item = pItem3058; // Item to be created
THTreeItem * pItem3 = pItem3058;
unsigned char oneByte = byteArray[i];
// Skip all the bytes which are zero.
if(byteArray[i] == 0)
continue;
// If not valid pointer, take the first available item in the array
if(PTR_INVALID_OR_NULL(item))
item = &items0008[nItems++];
// Insert this item as the top of the tree
InsertItem(&pItem305C, item, SWITCH_ITEMS, NULL);
item->parent = NULL; // Invalidate child and parent
item->child = NULL;
*itemPtr = item; // Store pointer into pointer array
item->dcmpByte = i; // Store counter
item->byteValue = oneByte; // Store byte value
if(oneByte >= maxByte)
{
maxByte = oneByte;
continue;
}
// Find the first item which has byte value greater than current one byte
if(PTR_VALID(pItem3 = pLast)) // EDI - Pointer to the last item
{
// 15006AF7
if(pItem3 != NULL)
{
do // 15006AFB
{
if(pItem3->byteValue >= oneByte)
goto _15006B09;
pItem3 = pItem3->prev;
}
while(PTR_VALID(pItem3));
}
}
pItem3 = NULL;
// 15006B09
_15006B09:
if(item->next != NULL)
item->RemoveItem();
// 15006B15
if(pItem3 == NULL)
pItem3 = PTR_PTR(&pFirst);
// 15006B1F
item->next = pItem3->next;
item->prev = pItem3->next->prev;
pItem3->next->prev = item;
pItem3->next = item;
}
// 15006B4A
for(; i < 0x102; i++)
{
THTreeItem ** itemPtr = &items306C[i]; // EDI
// 15006B59
THTreeItem * item = pItem3058; // ESI
if(PTR_INVALID_OR_NULL(item))
item = &items0008[nItems++];
InsertItem(&pItem305C, item, INSERT_ITEM, NULL);
// 15006B89
item->dcmpByte = i;
item->byteValue = 1;
item->parent = NULL;
item->child = NULL;
*itemPtr++ = item;
}
// 15006BAA
if(PTR_VALID(child1 = pLast)) // EDI - last item (first child to item
{
THTreeItem * child2; // EBP
THTreeItem * item; // ESI
// 15006BB8
while(PTR_VALID(child2 = child1->prev))
{
if(PTR_INVALID_OR_NULL(item = pItem3058))
item = &items0008[nItems++];
// 15006BE3
InsertItem(&pItem305C, item, SWITCH_ITEMS, NULL);
// 15006BF3
item->parent = NULL;
item->child = NULL;
//EDX = child2->byteValue + child1->byteValue;
//EAX = child1->byteValue;
//ECX = maxByte; // The greatest character (0xFF usually)
item->byteValue = child1->byteValue + child2->byteValue; // 0x02
item->child = child1; // Prev item in the
child1->parent = item;
child2->parent = item;
// EAX = item->byteValue;
if(item->byteValue >= maxByte)
maxByte = item->byteValue;
else
{
THTreeItem * pItem2 = child2->prev; // EDI
// 15006C2D
while(PTR_VALID(pItem2))
{
if(pItem2->byteValue >= item->byteValue)
goto _15006C3B;
pItem2 = pItem2->prev;
}
pItem2 = NULL;
_15006C3B:
if(item->next != 0)
{
THTreeItem * temp4 = item->GetPrevItem(-1);
temp4->next = item->next; // The first item changed
item->next->prev = item->prev; // First->prev changed to negative value
item->next = NULL;
item->prev = NULL;
}
// 15006C62
if(pItem2 == NULL)
pItem2 = PTR_PTR(&pFirst);
item->next = pItem2->next; // Set item with 0x100 byte value
item->prev = pItem2->next->prev; // Set item with 0x17 byte value
pItem2->next->prev = item; // Changed prev of item with
pItem2->next = item;
}
// 15006C7B
if(PTR_INVALID_OR_NULL(child1 = child2->prev))
break;
}
}
// 15006C88
offs0004 = 1;
}
THTreeItem * THuffmannTree::Call1500E740(unsigned int nValue)
{
THTreeItem * pItem1 = pItem3058; // EDX
THTreeItem * pItem2; // EAX
THTreeItem * pNext;
THTreeItem * pPrev;
THTreeItem ** ppItem;
if(PTR_INVALID_OR_NULL(pItem1) || (pItem2 = pItem1) == NULL)
{
if((pItem2 = &items0008[nItems++]) != NULL)
pItem1 = pItem2;
else
pItem1 = pFirst;
}
else
pItem1 = pItem2;
pNext = pItem1->next;
if(pNext != NULL)
{
pPrev = pItem1->prev;
if(PTR_INVALID(pPrev))
pPrev = PTR_NOT(pPrev);
else
pPrev += (pItem1 - pItem1->next->prev);
pPrev->next = pNext;
pNext->prev = pPrev;
pItem1->next = NULL;
pItem1->prev = NULL;
}
ppItem = &pFirst; // esi
if(nValue > 1)
{
// ecx = pFirst->next;
pItem1->next = *ppItem;
pItem1->prev = (*ppItem)->prev;
(*ppItem)->prev = pItem2;
*ppItem = pItem1;
pItem2->parent = NULL;
pItem2->child = NULL;
}
else
{
pItem1->next = (THTreeItem *)ppItem;
pItem1->prev = ppItem[1];
// edi = pItem305C;
pPrev = ppItem[1]; // ecx
if(PTR_INVALID(pPrev))
{
pPrev = PTR_NOT(pPrev);
pPrev->next = pItem1;
pPrev->prev = pItem2;
pItem2->parent = NULL;
pItem2->child = NULL;
}
else
{
if(PTR_INVALID(pItem305C))
pPrev += (THTreeItem *)ppItem - (*ppItem)->prev;
else
pPrev += (uintptr_t)pItem305C;
pPrev->next = pItem1;
ppItem[1] = pItem2;
pItem2->parent = NULL;
pItem2->child = NULL;
}
}
return pItem2;
}
void THuffmannTree::Call1500E820(THTreeItem * pItem)
{
THTreeItem * pItem1; // edi
THTreeItem * pItem2 = NULL; // eax
THTreeItem * pItem3; // edx
THTreeItem * pPrev; // ebx
for(; pItem != NULL; pItem = pItem->parent)
{
pItem->byteValue++;
for(pItem1 = pItem; ; pItem1 = pPrev)
{
pPrev = pItem1->prev;
if(PTR_INVALID_OR_NULL(pPrev))
{
pPrev = NULL;
break;
}
if(pPrev->byteValue >= pItem->byteValue)
break;
}
if(pItem1 == pItem)
continue;
if(pItem1->next != NULL)
{
pItem2 = pItem1->GetPrevItem(-1);
pItem2->next = pItem1->next;
pItem1->next->prev = pItem1->prev;
pItem1->next = NULL;
pItem1->prev = NULL;
}
pItem2 = pItem->next;
pItem1->next = pItem2;
pItem1->prev = pItem2->prev;
pItem2->prev = pItem1;
pItem->next = pItem1;
if((pItem2 = pItem1) != NULL)
{
pItem2 = pItem->GetPrevItem(-1);
pItem2->next = pItem->next;
pItem->next->prev = pItem->prev;
pItem->next = NULL;
pItem->prev = NULL;
}
if(pPrev == NULL)
pPrev = PTR_PTR(&pFirst);
pItem2 = pPrev->next;
pItem->next = pItem2;
pItem->prev = pItem2->prev;
pItem2->prev = pItem;
pPrev->next = pItem;
pItem3 = pItem1->parent->child;
pItem2 = pItem->parent;
if(pItem2->child == pItem)
pItem2->child = pItem1;
if(pItem3 == pItem1)
pItem1->parent->child = pItem;
pItem2 = pItem->parent;
pItem->parent = pItem1->parent;
pItem1->parent = pItem2;
offs0004++;
}
}
// 1500E920
unsigned int THuffmannTree::DoCompression(TOutputStream * os, unsigned char * pbInBuffer, int nInLength, int nCmpType)
{
THTreeItem * pItem1;
THTreeItem * pItem2;
THTreeItem * pItem3;
THTreeItem * pTemp;
unsigned long dwBitBuff;
unsigned int nBits;
unsigned int nBit;
BuildTree(nCmpType);
bIsCmp0 = (nCmpType == 0);
// Store the compression type into output buffer
os->dwBitBuff |= (nCmpType << os->nBits);
os->nBits += 8;
// Flush completed bytes
while(os->nBits >= 8)
{
if(os->cbOutSize != 0)
{
*os->pbOutPos++ = (unsigned char)os->dwBitBuff;
os->cbOutSize--;
}
os->dwBitBuff >>= 8;
os->nBits -= 8;
}
for(; nInLength != 0; nInLength--)
{
unsigned char bOneByte = *pbInBuffer++;
if((pItem1 = items306C[bOneByte]) == NULL)
{
pItem2 = items306C[0x101]; // ecx
pItem3 = pItem2->parent; // eax
dwBitBuff = 0;
nBits = 0;
for(; pItem3 != NULL; pItem3 = pItem3->parent)
{
nBit = (pItem3->child != pItem2) ? 1 : 0;
dwBitBuff = (dwBitBuff << 1) | nBit;
nBits++;
pItem2 = pItem3;
}
os->PutBits(dwBitBuff, nBits);
// Store the loaded byte into output stream
os->dwBitBuff |= (bOneByte << os->nBits);
os->nBits += 8;
// Flush the whole byte(s)
while(os->nBits >= 8)
{
if(os->cbOutSize != 0)
{
*os->pbOutPos++ = (unsigned char)os->dwBitBuff;
os->cbOutSize--;
}
os->dwBitBuff >>= 8;
os->nBits -= 8;
}
pItem1 = (PTR_INVALID_OR_NULL(pLast)) ? NULL : pLast;
pItem2 = Call1500E740(1);
pItem2->dcmpByte = pItem1->dcmpByte;
pItem2->byteValue = pItem1->byteValue;
pItem2->parent = pItem1;
items306C[pItem2->dcmpByte] = pItem2;
pItem2 = Call1500E740(1);
pItem2->dcmpByte = bOneByte;
pItem2->byteValue = 0;
pItem2->parent = pItem1;
items306C[pItem2->dcmpByte] = pItem2;
pItem1->child = pItem2;
Call1500E820(pItem2);
if(bIsCmp0 != 0)
{
Call1500E820(items306C[bOneByte]);
continue;
}
for(pItem1 = items306C[bOneByte]; pItem1 != NULL; pItem1 = pItem1->parent)
{
pItem1->byteValue++;
pItem2 = pItem1;
for(;;)
{
pItem3 = pItem2->prev;
if(PTR_INVALID_OR_NULL(pItem3))
{
pItem3 = NULL;
break;
}
if(pItem3->byteValue >= pItem1->byteValue)
break;
pItem2 = pItem3;
}
if(pItem2 != pItem1)
{
InsertItem(&pItem305C, pItem2, SWITCH_ITEMS, pItem1);
InsertItem(&pItem305C, pItem1, SWITCH_ITEMS, pItem3);
pItem3 = pItem2->parent->child;
if(pItem1->parent->child == pItem1)
pItem1->parent->child = pItem2;
if(pItem3 == pItem2)
pItem2->parent->child = pItem1;
pTemp = pItem1->parent;
pItem1->parent = pItem2->parent;
pItem2->parent = pTemp;
offs0004++;
}
}
}
// 1500EB62
else
{
dwBitBuff = 0;
nBits = 0;
for(pItem2 = pItem1->parent; pItem2 != NULL; pItem2 = pItem2->parent)
{
nBit = (pItem2->child != pItem1) ? 1 : 0;
dwBitBuff = (dwBitBuff << 1) | nBit;
nBits++;
pItem1 = pItem2;
}
os->PutBits(dwBitBuff, nBits);
}
// 1500EB98
if(bIsCmp0 != 0)
Call1500E820(items306C[bOneByte]); // 1500EB9D
// 1500EBAF
} // for(; nInLength != 0; nInLength--)
// 1500EBB8
pItem1 = items306C[0x100];
dwBitBuff = 0;
nBits = 0;
for(pItem2 = pItem1->parent; pItem2 != NULL; pItem2 = pItem2->parent)
{
nBit = (pItem2->child != pItem1) ? 1 : 0;
dwBitBuff = (dwBitBuff << 1) | nBit;
nBits++;
pItem1 = pItem2;
}
// 1500EBE6
os->PutBits(dwBitBuff, nBits);
// 1500EBEF
// Flush the remaining bits
while(os->nBits != 0)
{
if(os->cbOutSize != 0)
{
*os->pbOutPos++ = (unsigned char)os->dwBitBuff;
os->cbOutSize--;
}
os->dwBitBuff >>= 8;
os->nBits -= ((os->nBits > 8) ? 8 : os->nBits);
}
return (unsigned int)(os->pbOutPos - os->pbOutBuffer);
}
// Decompression using Huffman tree (1500E450)
unsigned int THuffmannTree::DoDecompression(unsigned char * pbOutBuffer, unsigned int dwOutLength, TInputStream * is)
{
TQDecompress * qd;
THTreeItem * pItem1;
THTreeItem * pItem2;
unsigned char * pbOutPos = pbOutBuffer;
unsigned long nBitCount;
unsigned int nDcmpByte = 0;
unsigned int n8Bits; // 8 bits loaded from input stream
unsigned int n7Bits; // 7 bits loaded from input stream
bool bHasQdEntry;
// Test the output length. Must not be NULL.
if(dwOutLength == 0)
return 0;
// Get the compression type from the input stream
n8Bits = is->Get8Bits();
// Build the Huffman tree
BuildTree(n8Bits);
bIsCmp0 = (n8Bits == 0) ? 1 : 0;
for(;;)
{
// Security check: If we are at the end of the input buffer,
// it means that the data are corrupt.
if(is->pbInBuffer > is->pbInBufferEnd)
return 0;
// Get 7 bits from input stream
n7Bits = is->Get7Bits();
// Try to use quick decompression. Check TQDecompress array for corresponding item.
// If found, ise the result byte instead.
qd = &qd3474[n7Bits];
// If there is a quick-pass possible (ebx)
bHasQdEntry = (qd->offs00 >= offs0004) ? true : false;
// If we can use quick decompress, use it.
if(bHasQdEntry)
{
if(qd->nBits > 7)
{
is->SkipBits(7);
pItem1 = qd->pItem;
goto _1500E549;
}
is->SkipBits(qd->nBits);
nDcmpByte = qd->dcmpByte;
}
else
{
pItem1 = pFirst->next->prev;
if(PTR_INVALID_OR_NULL(pItem1))
pItem1 = NULL;
_1500E549:
nBitCount = 0;
pItem2 = NULL;
do
{
pItem1 = pItem1->child; // Move down by one level
if(is->GetBit()) // If current bit is set, move to previous
pItem1 = pItem1->prev;
if(++nBitCount == 7) // If we are at 7th bit, save current HTree item.
pItem2 = pItem1;
}
while(pItem1->child != NULL); // Walk until tree has no deeper level
if(bHasQdEntry == false)
{
if(nBitCount > 7)
{
qd->offs00 = offs0004;
qd->nBits = nBitCount;
qd->pItem = pItem2;
}
else
{
unsigned long nIndex = n7Bits & (0xFFFFFFFF >> (32 - nBitCount));
unsigned long nAdd = (1 << nBitCount);
for(qd = &qd3474[nIndex]; nIndex <= 0x7F; nIndex += nAdd, qd += nAdd)
{
qd->offs00 = offs0004;
qd->nBits = nBitCount;
qd->dcmpByte = pItem1->dcmpByte;
}
}
}
nDcmpByte = pItem1->dcmpByte;
}
if(nDcmpByte == 0x101) // Huffman tree needs to be modified
{
n8Bits = is->Get8Bits();
pItem1 = (PTR_INVALID_OR_NULL(pLast)) ? NULL : pLast;
pItem2 = Call1500E740(1);
pItem2->parent = pItem1;
pItem2->dcmpByte = pItem1->dcmpByte;
pItem2->byteValue = pItem1->byteValue;
items306C[pItem2->dcmpByte] = pItem2;
pItem2 = Call1500E740(1);
pItem2->parent = pItem1;
pItem2->dcmpByte = n8Bits;
pItem2->byteValue = 0;
items306C[pItem2->dcmpByte] = pItem2;
pItem1->child = pItem2;
Call1500E820(pItem2);
if(bIsCmp0 == 0)
Call1500E820(items306C[n8Bits]);
nDcmpByte = n8Bits;
}
if(nDcmpByte == 0x100)
break;
*pbOutPos++ = (unsigned char)nDcmpByte;
if(--dwOutLength == 0)
break;
if(bIsCmp0)
Call1500E820(items306C[nDcmpByte]);
}
return (unsigned int)(pbOutPos - pbOutBuffer);
}
// Table for (de)compression. Every compression type has 258 entries
unsigned char THuffmannTree::Table1502A630[] =
{
// Data for compression type 0x00
0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
0x00, 0x00,
// Data for compression type 0x01
0x54, 0x16, 0x16, 0x0D, 0x0C, 0x08, 0x06, 0x05, 0x06, 0x05, 0x06, 0x03, 0x04, 0x04, 0x03, 0x05,
0x0E, 0x0B, 0x14, 0x13, 0x13, 0x09, 0x0B, 0x06, 0x05, 0x04, 0x03, 0x02, 0x03, 0x02, 0x02, 0x02,
0x0D, 0x07, 0x09, 0x06, 0x06, 0x04, 0x03, 0x02, 0x04, 0x03, 0x03, 0x03, 0x03, 0x03, 0x02, 0x02,
0x09, 0x06, 0x04, 0x04, 0x04, 0x04, 0x03, 0x02, 0x03, 0x02, 0x02, 0x02, 0x02, 0x03, 0x02, 0x04,
0x08, 0x03, 0x04, 0x07, 0x09, 0x05, 0x03, 0x03, 0x03, 0x03, 0x02, 0x02, 0x02, 0x03, 0x02, 0x02,
0x03, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x01, 0x01, 0x01, 0x02, 0x01, 0x02, 0x02,
0x06, 0x0A, 0x08, 0x08, 0x06, 0x07, 0x04, 0x03, 0x04, 0x04, 0x02, 0x02, 0x04, 0x02, 0x03, 0x03,
0x04, 0x03, 0x07, 0x07, 0x09, 0x06, 0x04, 0x03, 0x03, 0x02, 0x01, 0x02, 0x02, 0x02, 0x02, 0x02,
0x0A, 0x02, 0x02, 0x03, 0x02, 0x02, 0x01, 0x01, 0x02, 0x02, 0x02, 0x06, 0x03, 0x05, 0x02, 0x03,
0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x03, 0x01, 0x01, 0x01,
0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x04, 0x04, 0x04, 0x07, 0x09, 0x08, 0x0C, 0x02,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x01, 0x01, 0x03,
0x04, 0x01, 0x02, 0x04, 0x05, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x01, 0x01, 0x01,
0x04, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x01, 0x01, 0x02, 0x02, 0x02, 0x06, 0x4B,
0x00, 0x00,
// Data for compression type 0x02
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x27, 0x00, 0x00, 0x23, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xFF, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x01, 0x01, 0x06, 0x0E, 0x10, 0x04,
0x06, 0x08, 0x05, 0x04, 0x04, 0x03, 0x03, 0x02, 0x02, 0x03, 0x03, 0x01, 0x01, 0x02, 0x01, 0x01,
0x01, 0x04, 0x02, 0x04, 0x02, 0x02, 0x02, 0x01, 0x01, 0x04, 0x01, 0x01, 0x02, 0x03, 0x03, 0x02,
0x03, 0x01, 0x03, 0x06, 0x04, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x01, 0x02, 0x01, 0x01,
0x01, 0x29, 0x07, 0x16, 0x12, 0x40, 0x0A, 0x0A, 0x11, 0x25, 0x01, 0x03, 0x17, 0x10, 0x26, 0x2A,
0x10, 0x01, 0x23, 0x23, 0x2F, 0x10, 0x06, 0x07, 0x02, 0x09, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,