forked from haithun/CoD4X17a_testing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
huffman.c
743 lines (662 loc) · 15.5 KB
/
huffman.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
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
/*
===========================================================================
Copyright (C) 1999-2005 Id Software, Inc.
This file is part of CoD4X17a-Server source code.
CoD4X17a-Server source code is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
CoD4X17a-Server source code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
===========================================================================
*/
#include <string.h>
#include "huffman.h"
/* This is based on the Adaptive Huffman algorithm described in Sayood's Data
* Compression book. The ranks are not actually stored, but implicitly defined
* by the location of a node within a doubly-linked list */
static int bloc = 0;
void Huff_putBit( int bit, byte *fout, int *offset ) {
bloc = *offset;
if ( ( bloc & 7 ) == 0 ) {
fout[( bloc >> 3 )] = 0;
}
fout[( bloc >> 3 )] |= bit << ( bloc & 7 );
bloc++;
*offset = bloc;
}
int Huff_getBit( byte *fin, int *offset ) {
int t;
bloc = *offset;
t = ( fin[( bloc >> 3 )] >> ( bloc & 7 ) ) & 0x1;
bloc++;
*offset = bloc;
return t;
}
/* Add a bit to the output file (buffered) */
static void add_bit( char bit, byte *fout ) {
if ( ( bloc & 7 ) == 0 ) {
fout[( bloc >> 3 )] = 0;
}
fout[( bloc >> 3 )] |= bit << ( bloc & 7 );
bloc++;
}
/* Receive one bit from the input file (buffered) */
static int get_bit( byte *fin ) {
int t;
t = ( fin[( bloc >> 3 )] >> ( bloc & 7 ) ) & 0x1;
bloc++;
return t;
}
static node_t **get_ppnode( huff_t* huff ) {
node_t **tppnode;
if ( !huff->freelist ) {
return &( huff->nodePtrs[huff->blocPtrs++] );
} else {
tppnode = huff->freelist;
huff->freelist = (node_t **)*tppnode;
return tppnode;
}
}
static void free_ppnode( huff_t* huff, node_t **ppnode ) {
*ppnode = (node_t *)huff->freelist;
huff->freelist = ppnode;
}
/* Swap the location of these two nodes in the tree */
static void swap( huff_t* huff, node_t *node1, node_t *node2 ) {
node_t *par1, *par2;
par1 = node1->parent;
par2 = node2->parent;
if ( par1 ) {
if ( par1->left == node1 ) {
par1->left = node2;
} else {
par1->right = node2;
}
} else {
huff->tree = node2;
}
if ( par2 ) {
if ( par2->left == node2 ) {
par2->left = node1;
} else {
par2->right = node1;
}
} else {
huff->tree = node1;
}
node1->parent = par2;
node2->parent = par1;
}
/* Swap these two nodes in the linked list (update ranks) */
static void swaplist( node_t *node1, node_t *node2 ) {
node_t *par1;
par1 = node1->next;
node1->next = node2->next;
node2->next = par1;
par1 = node1->prev;
node1->prev = node2->prev;
node2->prev = par1;
if ( node1->next == node1 ) {
node1->next = node2;
}
if ( node2->next == node2 ) {
node2->next = node1;
}
if ( node1->next ) {
node1->next->prev = node1;
}
if ( node2->next ) {
node2->next->prev = node2;
}
if ( node1->prev ) {
node1->prev->next = node1;
}
if ( node2->prev ) {
node2->prev->next = node2;
}
}
/* Do the increments */
static void increment( huff_t* huff, node_t *node ) {
node_t *lnode;
if ( !node ) {
return;
}
if ( node->next != NULL && node->next->weight == node->weight ) {
lnode = *node->head;
if ( lnode != node->parent ) {
swap( huff, lnode, node );
}
swaplist( lnode, node );
}
if ( node->prev && node->prev->weight == node->weight ) {
*node->head = node->prev;
} else {
*node->head = NULL;
free_ppnode( huff, node->head );
}
node->weight++;
if ( node->next && node->next->weight == node->weight ) {
node->head = node->next->head;
} else {
node->head = get_ppnode( huff );
*node->head = node;
}
if ( node->parent ) {
increment( huff, node->parent );
if ( node->prev == node->parent ) {
swaplist( node, node->parent );
if ( *node->head == node ) {
*node->head = node->parent;
}
}
}
}
void Huff_addRef( huff_t* huff, byte ch ) {
node_t *tnode, *tnode2;
if ( huff->loc[ch] == NULL ) { /* if this is the first transmission of this node */
tnode = &( huff->nodeList[huff->blocNode++] );
tnode2 = &( huff->nodeList[huff->blocNode++] );
tnode2->symbol = INTERNAL_NODE;
tnode2->weight = 1;
tnode2->next = huff->lhead->next;
if ( huff->lhead->next ) {
huff->lhead->next->prev = tnode2;
if ( huff->lhead->next->weight == 1 ) {
tnode2->head = huff->lhead->next->head;
} else {
tnode2->head = get_ppnode( huff );
*tnode2->head = tnode2;
}
} else {
tnode2->head = get_ppnode( huff );
*tnode2->head = tnode2;
}
huff->lhead->next = tnode2;
tnode2->prev = huff->lhead;
tnode->symbol = ch;
tnode->weight = 1;
tnode->next = huff->lhead->next;
if ( huff->lhead->next ) {
huff->lhead->next->prev = tnode;
if ( huff->lhead->next->weight == 1 ) {
tnode->head = huff->lhead->next->head;
} else {
/* this should never happen */
tnode->head = get_ppnode( huff );
*tnode->head = tnode2;
}
} else {
/* this should never happen */
tnode->head = get_ppnode( huff );
*tnode->head = tnode;
}
huff->lhead->next = tnode;
tnode->prev = huff->lhead;
tnode->left = tnode->right = NULL;
if ( huff->lhead->parent ) {
if ( huff->lhead->parent->left == huff->lhead ) { /* lhead is guaranteed to by the NYT */
huff->lhead->parent->left = tnode2;
} else {
huff->lhead->parent->right = tnode2;
}
} else {
huff->tree = tnode2;
}
tnode2->right = tnode;
tnode2->left = huff->lhead;
tnode2->parent = huff->lhead->parent;
huff->lhead->parent = tnode->parent = tnode2;
huff->loc[ch] = tnode;
increment( huff, tnode2->parent );
} else {
increment( huff, huff->loc[ch] );
}
}
/* Get a symbol */
int Huff_Receive( node_t *node, int *ch, byte *fin ) {
while ( node && node->symbol == INTERNAL_NODE ) {
if ( get_bit( fin ) ) {
node = node->right;
} else {
node = node->left;
}
}
if ( !node ) {
return 0;
// Com_Error(ERR_DROP, "Illegal tree!\n");
}
return ( *ch = node->symbol );
}
/* Get a symbol */
void Huff_offsetReceive( node_t *node, int *ch, byte *fin, int *offset ) {
bloc = *offset;
while ( node && node->symbol == INTERNAL_NODE ) {
if ( get_bit( fin ) ) {
node = node->right;
} else {
node = node->left;
}
}
if ( !node ) {
*ch = 0;
return;
// Com_Error(ERR_DROP, "Illegal tree!\n");
}
*ch = node->symbol;
*offset = bloc;
}
/* Send the prefix code for this node */
static void Huff_send( node_t *node, node_t *child, byte *fout ) {
if ( node->parent ) {
Huff_send( node->parent, node, fout );
}
if ( child ) {
if ( node->right == child ) {
add_bit( 1, fout );
} else {
add_bit( 0, fout );
}
}
}
/* Send a symbol */
void Huff_transmit( huff_t *huff, int ch, byte *fout ) {
int i;
if ( huff->loc[ch] == NULL ) {
/* node_t hasn't been transmitted, send a NYT, then the symbol */
Huff_transmit( huff, NYT, fout );
for ( i = 7; i >= 0; i-- ) {
add_bit( (char)( ( ch >> i ) & 0x1 ), fout );
}
} else {
Huff_send( huff->loc[ch], NULL, fout );
}
}
void Huff_offsetTransmit( huff_t *huff, int ch, byte *fout, int *offset ) {
bloc = *offset;
Huff_send( huff->loc[ch], NULL, fout );
*offset = bloc;
}
void Huff_Decompress( msg_t *mbuf, int offset ) {
int ch, cch, i, j, size;
byte seq[65536];
byte* buffer;
huff_t huff;
size = mbuf->cursize - offset;
buffer = mbuf->data + offset;
if ( size <= 0 ) {
return;
}
Com_Memset( &huff, 0, sizeof( huff_t ) );
// Initialize the tree & list with the NYT node
huff.tree = huff.lhead = huff.ltail = huff.loc[NYT] = &( huff.nodeList[huff.blocNode++] );
huff.tree->symbol = NYT;
huff.tree->weight = 0;
huff.lhead->next = huff.lhead->prev = NULL;
huff.tree->parent = huff.tree->left = huff.tree->right = NULL;
cch = buffer[0] * 256 + buffer[1];
// don't overflow with bad messages
if ( cch > mbuf->maxsize - offset ) {
cch = mbuf->maxsize - offset;
}
bloc = 16;
for ( j = 0; j < cch; j++ ) {
ch = 0;
// don't overflow reading from the messages
// FIXME: would it be better to have a overflow check in get_bit ?
if ( ( bloc >> 3 ) > size ) {
seq[j] = 0;
break;
}
Huff_Receive( huff.tree, &ch, buffer ); /* Get a character */
if ( ch == NYT ) { /* We got a NYT, get the symbol associated with it */
ch = 0;
for ( i = 0; i < 8; i++ ) {
ch = ( ch << 1 ) + get_bit( buffer );
}
}
seq[j] = ch; /* Write symbol */
Huff_addRef( &huff, (byte)ch ); /* Increment node */
}
mbuf->cursize = cch + offset;
Com_Memcpy( mbuf->data + offset, seq, cch );
}
extern int oldsize;
void Huff_Compress( msg_t *mbuf, int offset ) {
int i, ch, size;
byte seq[65536];
byte* buffer;
huff_t huff;
size = mbuf->cursize - offset;
buffer = mbuf->data + + offset;
if ( size <= 0 ) {
return;
}
Com_Memset( &huff, 0, sizeof( huff_t ) );
// Add the NYT (not yet transmitted) node into the tree/list */
huff.tree = huff.lhead = huff.loc[NYT] = &( huff.nodeList[huff.blocNode++] );
huff.tree->symbol = NYT;
huff.tree->weight = 0;
huff.lhead->next = huff.lhead->prev = NULL;
huff.tree->parent = huff.tree->left = huff.tree->right = NULL;
huff.loc[NYT] = huff.tree;
seq[0] = ( size >> 8 );
seq[1] = size & 0xff;
bloc = 16;
for ( i = 0; i < size; i++ ) {
ch = buffer[i];
Huff_transmit( &huff, ch, seq ); /* Transmit symbol */
Huff_addRef( &huff, (byte)ch ); /* Do update */
}
bloc += 8; // next byte
mbuf->cursize = ( bloc >> 3 ) + offset;
Com_Memcpy( mbuf->data + offset, seq, ( bloc >> 3 ) );
}
void Huff_Init( huffman_t *huff ) {
Com_Memset( &huff->compressor, 0, sizeof( huff_t ) );
Com_Memset( &huff->decompressor, 0, sizeof( huff_t ) );
// Initialize the tree & list with the NYT node
huff->decompressor.tree = huff->decompressor.lhead = huff->decompressor.ltail = huff->decompressor.loc[NYT] = &( huff->decompressor.nodeList[huff->decompressor.blocNode++] );
huff->decompressor.tree->symbol = NYT;
huff->decompressor.tree->weight = 0;
huff->decompressor.lhead->next = huff->decompressor.lhead->prev = NULL;
huff->decompressor.tree->parent = huff->decompressor.tree->left = huff->decompressor.tree->right = NULL;
// Add the NYT (not yet transmitted) node into the tree/list */
huff->compressor.tree = huff->compressor.lhead = huff->compressor.loc[NYT] = &( huff->compressor.nodeList[huff->compressor.blocNode++] );
huff->compressor.tree->symbol = NYT;
huff->compressor.tree->weight = 0;
huff->compressor.lhead->next = huff->compressor.lhead->prev = NULL;
huff->compressor.tree->parent = huff->compressor.tree->left = huff->compressor.tree->right = NULL;
huff->compressor.loc[NYT] = huff->compressor.tree;
}
int msg_hData[256] = {
250315, // 0
41193, // 1
6292, // 2
7106, // 3
3730, // 4
3750, // 5
6110, // 6
23283, // 7
33317, // 8
6950, // 9
7838, // 10
9714, // 11
9257, // 12
17259, // 13
3949, // 14
1778, // 15
8288, // 16
1604, // 17
1590, // 18
1663, // 19
1100, // 20
1213, // 21
1238, // 22
1134, // 23
1749, // 24
1059, // 25
1246, // 26
1149, // 27
1273, // 28
4486, // 29
2805, // 30
3472, // 31
21819, // 32
1159, // 33
1670, // 34
1066, // 35
1043, // 36
1012, // 37
1053, // 38
1070, // 39
1726, // 40
888, // 41
1180, // 42
850, // 43
960, // 44
780, // 45
1752, // 46
3296, // 47
10630, // 48
4514, // 49
5881, // 50
2685, // 51
4650, // 52
3837, // 53
2093, // 54
1867, // 55
2584, // 56
1949, // 57
1972, // 58
940, // 59
1134, // 60
1788, // 61
1670, // 62
1206, // 63
5719, // 64
6128, // 65
7222, // 66
6654, // 67
3710, // 68
3795, // 69
1492, // 70
1524, // 71
2215, // 72
1140, // 73
1355, // 74
971, // 75
2180, // 76
1248, // 77
1328, // 78
1195, // 79
1770, // 80
1078, // 81
1264, // 82
1266, // 83
1168, // 84
965, // 85
1155, // 86
1186, // 87
1347, // 88
1228, // 89
1529, // 90
1600, // 91
2617, // 92
2048, // 93
2546, // 94
3275, // 95
2410, // 96
3585, // 97
2504, // 98
2800, // 99
2675, // 100
6146, // 101
3663, // 102
2840, // 103
14253, // 104
3164, // 105
2221, // 106
1687, // 107
3208, // 108
2739, // 109
3512, // 110
4796, // 111
4091, // 112
3515, // 113
5288, // 114
4016, // 115
7937, // 116
6031, // 117
5360, // 118
3924, // 119
4892, // 120
3743, // 121
4566, // 122
4807, // 123
5852, // 124
6400, // 125
6225, // 126
8291, // 127
23243, // 128
7838, // 129
7073, // 130
8935, // 131
5437, // 132
4483, // 133
3641, // 134
5256, // 135
5312, // 136
5328, // 137
5370, // 138
3492, // 139
2458, // 140
1694, // 141
1821, // 142
2121, // 143
1916, // 144
1149, // 145
1516, // 146
1367, // 147
1236, // 148
1029, // 149
1258, // 150
1104, // 151
1245, // 152
1006, // 153
1149, // 154
1025, // 155
1241, // 156
952, // 157
1287, // 158
997, // 159
1713, // 160
1009, // 161
1187, // 162
879, // 163
1099, // 164
929, // 165
1078, // 166
951, // 167
1656, // 168
930, // 169
1153, // 170
1030, // 171
1262, // 172
1062, // 173
1214, // 174
1060, // 175
1621, // 176
930, // 177
1106, // 178
912, // 179
1034, // 180
892, // 181
1158, // 182
990, // 183
1175, // 184
850, // 185
1121, // 186
903, // 187
1087, // 188
920, // 189
1144, // 190
1056, // 191
3462, // 192
2240, // 193
4397, // 194
12136, // 195
7758, // 196
1345, // 197
1307, // 198
3278, // 199
1950, // 200
886, // 201
1023, // 202
1112, // 203
1077, // 204
1042, // 205
1061, // 206
1071, // 207
1484, // 208
1001, // 209
1096, // 210
915, // 211
1052, // 212
995, // 213
1070, // 214
876, // 215
1111, // 216
851, // 217
1059, // 218
805, // 219
1112, // 220
923, // 221
1103, // 222
817, // 223
1899, // 224
1872, // 225
976, // 226
841, // 227
1127, // 228
956, // 229
1159, // 230
950, // 231
7791, // 232
954, // 233
1289, // 234
933, // 235
1127, // 236
3207, // 237
1020, // 238
927, // 239
1355, // 240
768, // 241
1040, // 242
745, // 243
952, // 244
805, // 245
1073, // 246
740, // 247
1013, // 248
805, // 249
1008, // 250
796, // 251
996, // 252
1057, // 253
11457, // 254
13504, // 255
};
static huffman_t msgHuff;
static qboolean huffInit = qfalse;
int MSG_ReadBitsCompress(const byte* input, byte* outputBuf, int readsize){
readsize = readsize * 8;
byte *outptr = outputBuf;
int get;
int offset;
int i;
if(readsize <= 0){
return 0;
}
for(offset = 0, i = 0; readsize > offset; i++){
Huff_offsetReceive(/*msgHuff.decompressor.tree*/ *((node_t**)(0x89297e8)), &get, (byte*)input, &offset);
*outptr = (byte)get;
outptr++;
}
return i;
}
void MSG_initHuffman() {
int i,j;
huffInit = qtrue;
Huff_Init(&msgHuff);
for(i=0;i<256;i++) {
for (j=0;j<msg_hData[i];j++) {
Huff_addRef(&msgHuff.compressor, (byte)i); // Do update
Huff_addRef(&msgHuff.decompressor, (byte)i); // Do update
}
}
}