HTTP2_EncDec.cc 55.4 KB
Newer Older
Elemer Lelik's avatar
Elemer Lelik committed
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
/******************************************************************************
* Copyright (c) 2017  Ericsson AB
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
*   Gabor Szalai - initial implementation and initial documentation
******************************************************************************/
//
//  File:               HTTP2_EncDec.cc
//  Description:        Encoder/decoder function for HTTP2 
//  Rev:                R1A
//  Prodnr:             CNL 113 851


#include "HTTP2_Types.hh"
#include <stdint.h>
#include "memory.h"
#include <string.h>
#include <stdio.h>
#include <deque>

// header compression context classes
class HTTP2_hdr_data{
public:
  CHARSTRING   name;
  CHARSTRING   value;
  int          size;

  HTTP2_hdr_data(){
    name="";
    value="";
    size=32;
  };
  HTTP2_hdr_data(const CHARSTRING& p_name, const CHARSTRING& p_value){
    name=p_name;
    value=p_value;
    size=32+name.lengthof()+value.lengthof();
  };
  ~HTTP2_hdr_data(){};

   void set(const CHARSTRING& p_name, const CHARSTRING& p_value){
    name=p_name;
    value=p_value;
    size=32+name.lengthof()+value.lengthof();
  }
  
};


class HTTP2_compression_ctx{
public:
  std::deque<HTTP2_hdr_data*> h_table;
  int h_table_size;
  int h_table_max_size;
  
  HTTP2_compression_ctx(){
    h_table_size=0;
    h_table_max_size=4096;
  };
  HTTP2_compression_ctx(int max_size){
    h_table_size=0;
    h_table_max_size=max_size;
  };
  
  ~HTTP2_compression_ctx(){
    std::deque<HTTP2_hdr_data*>::iterator it = h_table.begin();
    while(it!=h_table.end()){
      delete *it;
      it++;
    }
  };
  
  void set_max_size(int new_size){
    while(new_size<h_table_size){
      h_table_size-= h_table.back()->size;
      delete h_table.back();
      h_table.pop_back();
    }
    h_table_max_size=new_size;
  };
  
  void add_hdr(const CHARSTRING& p_name, const CHARSTRING& p_value){
    HTTP2_hdr_data *hdata= new HTTP2_hdr_data(p_name,p_value);
    h_table.push_front(hdata);
    h_table_size+=hdata->size;
    while(h_table_max_size<h_table_size){
      h_table_size-= h_table.back()->size;
      delete h_table.back();
      h_table.pop_back();
    };
    
  };
  
  int find(const CHARSTRING& p_name) const {
    for(size_t i=0;i<h_table.size();i++){
      if(h_table[i]->name==p_name){
        return i+1;
      }
    }
    return -1;
  };
  int find(const CHARSTRING& p_name,  const CHARSTRING& p_value) const {
    for(size_t i=0;i<h_table.size();i++){
      if( (h_table[i]->name==p_name) && (h_table[i]->value==p_value) ){
        return i+1;
      }
    }
    return -1;
  };
  
  const HTTP2_hdr_data* operator[](int idx) const{
    if( (idx>0) && (idx<=(int)h_table.size()) ){
      return h_table[idx-1];
    }
    return NULL;
  };

};

class HTTP2_compression_ctxs{
public:
  HTTP2_compression_ctx local;
  HTTP2_compression_ctx remote;
  
};

class HTTP2_static_table_class{
public:
  HTTP2_compression_ctx ctx;
  HTTP2_static_table_class(){
    ctx.add_hdr("www-authenticate","");
    ctx.add_hdr("via","");
    ctx.add_hdr("vary","");
    ctx.add_hdr("user-agent","");
    ctx.add_hdr("transfer-encoding","");
    ctx.add_hdr("strict-transport-security","");
    ctx.add_hdr("set-cookie","");
    ctx.add_hdr("server","");
    ctx.add_hdr("retry-after","");
    ctx.add_hdr("refresh","");
    ctx.add_hdr("referer","");
    ctx.add_hdr("range","");
    ctx.add_hdr("proxy-authorization","");
    ctx.add_hdr("proxy-authenticate","");
    ctx.add_hdr("max-forwards","");
    ctx.add_hdr("location","");
    ctx.add_hdr("link","");
    ctx.add_hdr("last-modified","");
    ctx.add_hdr("if-unmodified-since","");
    ctx.add_hdr("if-range","");
    ctx.add_hdr("if-none-match","");
    ctx.add_hdr("if-modified-since","");
    ctx.add_hdr("if-match","");
    ctx.add_hdr("host","");
    ctx.add_hdr("from","");
    ctx.add_hdr("expires","");
    ctx.add_hdr("expect","");
    ctx.add_hdr("etag","");
    ctx.add_hdr("date","");
    ctx.add_hdr("cookie","");
    ctx.add_hdr("content-type","");
    ctx.add_hdr("content-range","");
    ctx.add_hdr("content-location","");
    ctx.add_hdr("content-length","");
    ctx.add_hdr("content-language","");
    ctx.add_hdr("content-encoding","");
    ctx.add_hdr("content-disposition","");
    ctx.add_hdr("cache-control","");
    ctx.add_hdr("authorization","");
    ctx.add_hdr("allow","");
    ctx.add_hdr("age","");
    ctx.add_hdr("access-control-allow-origin","");
    ctx.add_hdr("accept","");
    ctx.add_hdr("accept-ranges","");
    ctx.add_hdr("accept-language","");
    ctx.add_hdr("accept-encoding","gzip, deflate");
    ctx.add_hdr("accept-charset","");
    ctx.add_hdr(":status","500");
    ctx.add_hdr(":status","404");
    ctx.add_hdr(":status","400");
    ctx.add_hdr(":status","304");
    ctx.add_hdr(":status","206");
    ctx.add_hdr(":status","204");
    ctx.add_hdr(":status","200");
    ctx.add_hdr(":scheme","https");
    ctx.add_hdr(":scheme","http");
    ctx.add_hdr(":path","/index.html");
    ctx.add_hdr(":path","/");
    ctx.add_hdr(":method","POST");
    ctx.add_hdr(":method","GET");
    ctx.add_hdr(":authority","");
  }
};

static const HTTP2_static_table_class HTTP2_static_table=HTTP2_static_table_class();

////// Indexed variable int representation functions
//  Support max 32 bit unsigned val
//  it means the buffer should be at least 6 octet to safely store the max value 
//  with the minimal prefix

static const unsigned char twoN_minus_one_table[]={  // stores 2^N-1 for N=0..8
0,1,3,7,15,31,63,127,255
};

// return the number of used octets
static int encode_integer(unsigned char* buff,
                          const uint32_t  val,
                          const int prefix_len,
                          const unsigned char filler){  // only the filler bits should be set
  int ret_val=0;
  if(val< twoN_minus_one_table[prefix_len]){
     // fits in one octet
     buff[0]= filler | (val & 0xFF);
     ret_val=1;
  } else {
    uint32_t act_val=val-twoN_minus_one_table[prefix_len];
    buff[0]= filler | ( twoN_minus_one_table[prefix_len] & 0xFF);
    ret_val=1;
    while(act_val>127){
      buff[ret_val]= 0x80 | (act_val%128);
      act_val/=128;
      ret_val++;
    }
    buff[ret_val]= act_val;
    ret_val++;
  }
  return ret_val;
}

// returns the number of the used octets
static int decode_integer(const unsigned char* buff,
                          uint32_t&  val,
                          const int prefix_len,
                          unsigned char& filler){  // only the filler bits will be set
  filler = buff[0] & ~twoN_minus_one_table[prefix_len];
  
  val = buff[0] & twoN_minus_one_table[prefix_len];

  int ret_val=0;
  if(val==twoN_minus_one_table[prefix_len]){
    // The value is not fit into the prefix_len bits
    do {
      ret_val++;
      val += (  ((uint32_t)(buff[ret_val] & 0x7F)) << (7*ret_val) );
    } while(buff[ret_val] & 0x80);
    ret_val++;
  } else {
    ret_val=1;
  }
  return ret_val;
}





//////////// Huffman tables See RFC7541

// Huffman code lengths in bits

static const int huffman_code_length[]={
 13, 23, 28, 28, 28, 28, 28, 28, 28, 24, 30, 28, 28, 30, 28, 28,
 28, 28, 28, 28, 28, 28, 30, 28, 28, 28, 28, 28, 28, 28, 28, 28,
  6, 10, 10, 12, 13,  6,  8, 11, 10, 10,  8, 11,  8,  6,  6,  6,
  5,  5,  5,  6,  6,  6,  6,  6,  6,  6,  7,  8, 15,  6, 12, 10,
 13,  6,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,
  7,  7,  7,  7,  7,  7,  7,  7,  8,  7,  8, 13, 19, 13, 14,  6,
 15,  5,  6,  5,  6,  5,  6,  6,  6,  5,  7,  7,  6,  6,  6,  5,
  6,  7,  6,  5,  5,  6,  7,  7,  7,  7,  7, 15, 11, 14, 13, 28,
 20, 22, 20, 20, 22, 22, 22, 23, 22, 23, 23, 23, 23, 23, 24, 23,
 24, 24, 22, 23, 24, 23, 23, 23, 23, 21, 22, 23, 22, 23, 23, 24,
 22, 21, 20, 22, 22, 23, 23, 21, 23, 22, 22, 24, 21, 22, 23, 23,
 21, 21, 22, 21, 23, 22, 23, 23, 20, 22, 22, 22, 23, 22, 22, 23,
 26, 26, 20, 19, 22, 23, 22, 25, 26, 26, 26, 27, 27, 26, 24, 25,
 19, 21, 26, 27, 27, 26, 27, 24, 21, 21, 26, 26, 28, 27, 27, 27,
 20, 24, 20, 21, 22, 21, 21, 23, 22, 22, 25, 25, 24, 24, 26, 23,
 26, 27, 26, 26, 27, 27, 27, 27, 27, 28, 27, 27, 27, 27, 27, 26,
 30
};

static const unsigned int huffman_code_values[]={
     0x1ff8,   0x7fffd8,  0xfffffe2,  0xfffffe3,  0xfffffe4,  0xfffffe5,  0xfffffe6,  0xfffffe7,  0xfffffe8,   0xffffea, 0x3ffffffc,  0xfffffe9,  0xfffffea, 0x3ffffffd,  0xfffffeb,  0xfffffec,
  0xfffffed,  0xfffffee,  0xfffffef,  0xffffff0,  0xffffff1,  0xffffff2, 0x3ffffffe,  0xffffff3,  0xffffff4,  0xffffff5,  0xffffff6,  0xffffff7,  0xffffff8,  0xffffff9,  0xffffffa,  0xffffffb,
       0x14,      0x3f8,      0x3f9,      0xffa,     0x1ff9,       0x15,       0xf8,      0x7fa,      0x3fa,      0x3fb,       0xf9,      0x7fb,       0xfa,       0x16,       0x17,       0x18,
        0x0,        0x1,        0x2,       0x19,       0x1a,       0x1b,       0x1c,       0x1d,       0x1e,       0x1f,       0x5c,       0xfb,     0x7ffc,       0x20,      0xffb,      0x3fc,
     0x1ffa,       0x21,       0x5d,       0x5e,       0x5f,       0x60,       0x61,       0x62,       0x63,       0x64,       0x65,       0x66,       0x67,       0x68,       0x69,       0x6a,
       0x6b,       0x6c,       0x6d,       0x6e,       0x6f,       0x70,       0x71,       0x72,       0xfc,       0x73,       0xfd,     0x1ffb,    0x7fff0,     0x1ffc,     0x3ffc,       0x22,
     0x7ffd,        0x3,       0x23,        0x4,       0x24,        0x5,       0x25,       0x26,       0x27,        0x6,       0x74,       0x75,       0x28,       0x29,       0x2a,        0x7,
       0x2b,       0x76,       0x2c,        0x8,        0x9,       0x2d,       0x77,       0x78,       0x79,       0x7a,       0x7b,     0x7ffe,      0x7fc,     0x3ffd,     0x1ffd,  0xffffffc,
    0xfffe6,   0x3fffd2,    0xfffe7,    0xfffe8,   0x3fffd3,   0x3fffd4,   0x3fffd5,   0x7fffd9,   0x3fffd6,   0x7fffda,   0x7fffdb,   0x7fffdc,   0x7fffdd,   0x7fffde,   0xffffeb,   0x7fffdf,
   0xffffec,   0xffffed,   0x3fffd7,   0x7fffe0,   0xffffee,   0x7fffe1,   0x7fffe2,   0x7fffe3,   0x7fffe4,   0x1fffdc,   0x3fffd8,   0x7fffe5,   0x3fffd9,   0x7fffe6,   0x7fffe7,   0xffffef,
   0x3fffda,   0x1fffdd,    0xfffe9,   0x3fffdb,   0x3fffdc,   0x7fffe8,   0x7fffe9,   0x1fffde,   0x7fffea,   0x3fffdd,   0x3fffde,   0xfffff0,   0x1fffdf,   0x3fffdf,   0x7fffeb,   0x7fffec,
   0x1fffe0,   0x1fffe1,   0x3fffe0,   0x1fffe2,   0x7fffed,   0x3fffe1,   0x7fffee,   0x7fffef,    0xfffea,   0x3fffe2,   0x3fffe3,   0x3fffe4,   0x7ffff0,   0x3fffe5,   0x3fffe6,   0x7ffff1,
  0x3ffffe0,  0x3ffffe1,    0xfffeb,    0x7fff1,   0x3fffe7,   0x7ffff2,   0x3fffe8,  0x1ffffec,  0x3ffffe2,  0x3ffffe3,  0x3ffffe4,  0x7ffffde,  0x7ffffdf,  0x3ffffe5,   0xfffff1,  0x1ffffed,
    0x7fff2,   0x1fffe3,  0x3ffffe6,  0x7ffffe0,  0x7ffffe1,  0x3ffffe7,  0x7ffffe2,   0xfffff2,   0x1fffe4,   0x1fffe5,  0x3ffffe8,  0x3ffffe9,  0xffffffd,  0x7ffffe3,  0x7ffffe4,  0x7ffffe5,
    0xfffec,   0xfffff3,    0xfffed,   0x1fffe6,   0x3fffe9,   0x1fffe7,   0x1fffe8,   0x7ffff3,   0x3fffea,   0x3fffeb,  0x1ffffee,  0x1ffffef,   0xfffff4,   0xfffff5,  0x3ffffea,   0x7ffff4,
  0x3ffffeb,  0x7ffffe6,  0x3ffffec,  0x3ffffed,  0x7ffffe7,  0x7ffffe8,  0x7ffffe9,  0x7ffffea,  0x7ffffeb,  0xffffffe,  0x7ffffec,  0x7ffffed,  0x7ffffee,  0x7ffffef,  0x7fffff0,  0x3ffffee,
 0x3fffffff
};


// inserts the huffman code of one character into teh buffer
static void put_code2buff(unsigned char* buff, int& bitpos, unsigned char code){
  int code_len = huffman_code_length[code];
  int bitpos_in_octet = bitpos%8;
  
  // moves to the first bit of the huffman code to the insertion point
  int shift=  (16-bitpos_in_octet-(code_len%8))%8;
  uint64_t ins_val= ((uint64_t)huffman_code_values[code]) << shift;
  
  
  buff+= bitpos/8;
  for(int i=((code_len+shift+7)/8)-1;i>=0;i--)  {
    if(i==0){
      buff[i] |= (ins_val & 0xFF);
    } else {
      buff[i] = (ins_val & 0xFF);
    }
    ins_val >>= 8;
  }
  bitpos+=code_len;
}

static const unsigned char get_mask_table[]={
0x80, 0x40, 0x20, 0x10,
0x08, 0x04, 0x02, 0x01
};

static int get_onebit(const unsigned char* buff, int& bitpos){
  if(buff[bitpos/8] & get_mask_table[bitpos%8]){
    bitpos++;
    return 1;
  }
  bitpos++;
  return 0;
}

// return -1 or the char code or the EOF (256)
static int decompress_one_char(const unsigned char* buff, int& bitpos, int length /*in bits*/ ){
  unsigned int huffman_val=0;
  
  if((bitpos+5)>length){
    while(bitpos<length){
      if(get_onebit(buff,bitpos)!=1) {return -1;} // incomplete code
    }
    return 256; //EOF
  }

  // get the first 5 bit, the minimum code length is 5
  for(int i=0;i<5;i++){
    huffman_val<<=1;
    huffman_val+=get_onebit(buff,bitpos);
  }
  // Check the 5 bit codes
  // See RFC7541 for magic values
  switch(huffman_val){
    case 0x0:
      return 48;
    case 0x1:
      return 49;
    case 0x2:
      return 50;
    case 0x3:
      return 97;
    case 0x4:
      return 99;
    case 0x5:
      return 101;
    case 0x6:
      return 105;
    case 0x7:
      return 111;
    case 0x8:
      return 115;
    case 0x9:
      return 116;
  }
  
  if(bitpos==length){ // no more bits
    if(huffman_val == 0x1f){ // all bit is 1
      return 256; //EOF
    } else {
      return -1; // invalid code
    }
  }
  
  // 6 bit codes
  huffman_val<<=1;
  huffman_val+=get_onebit(buff,bitpos);

  switch(huffman_val){
    case 0x14:
      return 32;
    case 0x15:
      return 37;
    case 0x16:
      return 45;
    case 0x17:
      return 46;
    case 0x18:
      return 47;
    case 0x19:
      return 51;
    case 0x1a:
      return 52;
    case 0x1b:
      return 53;
    case 0x1c:
      return 54;
    case 0x1d:
      return 55;
    case 0x1e:
      return 56;
    case 0x1f:
      return 57;
    case 0x20:
      return 61;
    case 0x21:
      return 65;
    case 0x22:
      return 95;
    case 0x23:
      return 98;
    case 0x24:
      return 100;
    case 0x25:
      return 102;
    case 0x26:
      return 103;
    case 0x27:
      return 104;
    case 0x28:
      return 108;
    case 0x29:
      return 109;
    case 0x2a:
      return 110;
    case 0x2b:
      return 112;
    case 0x2c:
      return 114;
    case 0x2d:
      return 117;
  }

  if(bitpos==length){ // no more bits
    if(huffman_val == 0x3f){ // all bit is 1
      return 256; //EOF
    } else {
      return -1; // invalid code
    }
  }
  
  // 7 bit codes
  huffman_val<<=1;
  huffman_val+=get_onebit(buff,bitpos);
  switch(huffman_val){
    case 0x5c:
      return 58;
    case 0x5d:
      return 66;
    case 0x5e:
      return 67;
    case 0x5f:
      return 68;
    case 0x60:
      return 69;
    case 0x61:
      return 70;
    case 0x62:
      return 71;
    case 0x63:
      return 72;
    case 0x64:
      return 73;
    case 0x65:
      return 74;
    case 0x66:
      return 75;
    case 0x67:
      return 76;
    case 0x68:
      return 77;
    case 0x69:
      return 78;
    case 0x6a:
      return 79;
    case 0x6b:
      return 80;
    case 0x6c:
      return 81;
    case 0x6d:
      return 82;
    case 0x6e:
      return 83;
    case 0x6f:
      return 84;
    case 0x70:
      return 85;
    case 0x71:
      return 86;
    case 0x72:
      return 87;
    case 0x73:
      return 89;
    case 0x74:
      return 106;
    case 0x75:
      return 107;
    case 0x76:
      return 113;
    case 0x77:
      return 118;
    case 0x78:
      return 119;
    case 0x79:
      return 120;
    case 0x7a:
      return 121;
    case 0x7b:
      return 122;
  }

  if(bitpos==length){ // no more bits
    if(huffman_val == 0x7f){ // all bit is 1
      return 256; //EOF
    } else {
      return -1; // invalid code
    }
  }
  
  // 8bit codes
  huffman_val<<=1;
  huffman_val+=get_onebit(buff,bitpos);
  switch(huffman_val){
    case 0xf8:
      return 38;
    case 0xf9:
      return 42;
    case 0xfa:
      return 44;
    case 0xfb:
      return 59;
    case 0xfc:
      return 88;
    case 0xfd:
      return 90;
  }

  if(bitpos==length){ // no more bits
      return -1; // invalid code
  }
  huffman_val<<=1;
  huffman_val+=get_onebit(buff,bitpos);
  if(bitpos==length){ // no more bits
      return -1; // invalid code
  }
  huffman_val<<=1;
  huffman_val+=get_onebit(buff,bitpos);
  // 10bit codes
  switch(huffman_val){
    case 0x3f8:
      return 33;
    case 0x3f9:
      return 34;
    case 0x3fa:
      return 40;
    case 0x3fb:
      return 41;
    case 0x3fc:
      return 63;
  }

  if(bitpos==length){ // no more bits
      return -1; // invalid code
  }
  huffman_val<<=1;
  huffman_val+=get_onebit(buff,bitpos);
  // 11bit codes
  switch(huffman_val){
    case 0x7fa:
      return 39;
    case 0x7fb:
      return 43;
    case 0x7fc:
      return 124;
  }  

  if(bitpos==length){ // no more bits
      return -1; // invalid code
  }
  huffman_val<<=1;
  huffman_val+=get_onebit(buff,bitpos);
  // 12bit codes

  switch(huffman_val){
    case 0xffa:
      return 35;
    case 0xffb:
      return 62;
  }  

  if(bitpos==length){ // no more bits
      return -1; // invalid code
  }
  huffman_val<<=1;
  huffman_val+=get_onebit(buff,bitpos);
  // 13bit codes

  switch(huffman_val){
    case 0x1ff8:
      return 0;
    case 0x1ff9:
      return 36;
    case 0x1ffa:
      return 64;
    case 0x1ffb:
      return 91;
    case 0x1ffc:
      return 93;
    case 0x1ffd:
      return 126;
  }  

  if(bitpos==length){ // no more bits
      return -1; // invalid code
  }
  huffman_val<<=1;
  huffman_val+=get_onebit(buff,bitpos);
  // 14bit codes
  switch(huffman_val){
    case 0x3ffc:
      return 94;
    case 0x3ffd:
      return 125;
  }  

  if(bitpos==length){ // no more bits
      return -1; // invalid code
  }
  huffman_val<<=1;
  huffman_val+=get_onebit(buff,bitpos);
  // 15bit codes
  switch(huffman_val){
    case 0x7ffc:
      return 60;
    case 0x7ffd:
      return 96;
    case 0x7ffe:
      return 123;
  }  
  if(bitpos==length){ // no more bits
      return -1; // invalid code
  }
  huffman_val<<=1;
  huffman_val+=get_onebit(buff,bitpos);
  if(bitpos==length){ // no more bits
      return -1; // invalid code
  }
  huffman_val<<=1;
  huffman_val+=get_onebit(buff,bitpos);
  if(bitpos==length){ // no more bits
      return -1; // invalid code
  }
  huffman_val<<=1;
  huffman_val+=get_onebit(buff,bitpos);
  if(bitpos==length){ // no more bits
      return -1; // invalid code
  }
  huffman_val<<=1;
  huffman_val+=get_onebit(buff,bitpos);
  // 19bit codes
  switch(huffman_val){
    case 0x7fff0:
      return 92;
    case 0x7fff1:
      return 195;
    case 0x7ff2:
      return 208;
  }  

  if(bitpos==length){ // no more bits
      return -1; // invalid code
  }
  huffman_val<<=1;
  huffman_val+=get_onebit(buff,bitpos);
  // 20bit codes
  switch(huffman_val){
    case 0xfffe6:
      return 128;
    case 0xfffe7:
      return 130;
    case 0xfffe8:
      return 131;
    case 0xfffe9:
      return 162;
    case 0xfffea:
      return 184;
    case 0xfffeb:
      return 194;
    case 0xfffec:
      return 224;
    case 0xfffed:
      return 226;
  }  

  if(bitpos==length){ // no more bits
      return -1; // invalid code
  }
  huffman_val<<=1;
  huffman_val+=get_onebit(buff,bitpos);
  // 21bit codes
  switch(huffman_val){
    case 0x1fffdc:
      return 153;
    case 0x1fffdd:
      return 161;
    case 0x1fffde:
      return 167;
    case 0x1fffdf:
      return 172;
    case 0x1fffe0:
      return 176;
    case 0x1fffe1:
      return 177;
    case 0x1fffe2:
      return 179;
    case 0x1fffe3:
      return 209;
    case 0x1fffe4:
      return 216;
    case 0x1fffe5:
      return 217;
    case 0x1fffe6:
      return 227;
    case 0x1fffe7:
      return 229;
    case 0x1fffe8:
      return 230;
  }  

  if(bitpos==length){ // no more bits
      return -1; // invalid code
  }
  huffman_val<<=1;
  huffman_val+=get_onebit(buff,bitpos);
  // 22bit codes
  switch(huffman_val){
    case 0x3fffd2:
      return 129;
    case 0x3fffd3:
      return 132;
    case 0x3fffd4:
      return 133;
    case 0x3fffd5:
      return 134;
    case 0x3fffd6:
      return 136;
    case 0x3fffd7:
      return 146;
    case 0x3fffd8:
      return 154;
    case 0x3fffd9:
      return 156;
    case 0x3fffda:
      return 160;
    case 0x3fffdb:
      return 163;
    case 0x3fffdc:
      return 164;
    case 0x3fffdd:
      return 169;
    case 0x3fffde:
      return 170;
    case 0x3fffdf:
      return 173;
    case 0x3fffe0:
      return 178;
    case 0x3fffe1:
      return 181;
    case 0x3fffe2:
      return 185;
    case 0x3fffe3:
      return 186;
    case 0x3fffe4:
      return 187;
    case 0x3fffe5:
      return 189;
    case 0x3fffe6:
      return 190;
    case 0x3fffe7:
      return 196;
    case 0x3fffe8:
      return 198;
    case 0x3fffe9:
      return 228;
    case 0x3fffea:
      return 232;
    case 0x3fffeb:
      return 233;
  }  

  if(bitpos==length){ // no more bits
      return -1; // invalid code
  }
  huffman_val<<=1;
  huffman_val+=get_onebit(buff,bitpos);
  // 23bit codes
  switch(huffman_val){
    case 0x7fffd8:
      return 1;
    case 0x7fffd9:
      return 135;
    case 0x7fffda:
      return 137;
    case 0x7fffdb:
      return 138;
    case 0x7fffdc:
      return 139;
    case 0x7fffdd:
      return 140;
    case 0x7fffde:
      return 141;
    case 0x7fffdf:
      return 143;
    case 0x7fffe0:
      return 147;
    case 0x7fffe1:
      return 149;
    case 0x7fffe2:
      return 150;
    case 0x7fffe3:
      return 151;
    case 0x7fffe4:
      return 152;
    case 0x7fffe5:
      return 155;
    case 0x7fffe6:
      return 157;
    case 0x7fffe7:
      return 158;
    case 0x7fffe8:
      return 165;
    case 0x7fffe9:
      return 166;
    case 0x7fffea:
      return 168;
    case 0x7fffeb:
      return 174;
    case 0x7fffec:
      return 175;
    case 0x7fffed:
      return 180;
    case 0x7fffee:
      return 182;
    case 0x7fffef:
      return 183;
    case 0x7ffff0:
      return 188;
    case 0x7ffff1:
      return 191;
    case 0x7ffff2:
      return 197;
    case 0x7ffff3:
      return 231;
    case 0x7ffff4:
      return 239;
  }  

  if(bitpos==length){ // no more bits
      return -1; // invalid code
  }
  huffman_val<<=1;
  huffman_val+=get_onebit(buff,bitpos);
  // 24bit codes
  switch(huffman_val){
    case 0xffffea:
      return 9;
    case 0xffffeb:
      return 142;
    case 0xffffec:
      return 144;
    case 0xffffed:
      return 145;
    case 0xffffee:
      return 148;
    case 0xffffef:
      return 159;
    case 0xfffff0:
      return 171;
    case 0xfffff1:
      return 206;
    case 0xfffff2:
      return 215;
    case 0xfffff3:
      return 225;
    case 0xfffff4:
      return 236;
    case 0xfffff5:
      return 237;
  }  

  if(bitpos==length){ // no more bits
      return -1; // invalid code
  }
  huffman_val<<=1;
  huffman_val+=get_onebit(buff,bitpos);
  // 25bit codes
  switch(huffman_val){
    case 0x1ffffec:
      return 199;
    case 0x1ffffed:
      return 207;
    case 0x1ffffee:
      return 234;
    case 0x1ffffef:
      return 235;
  }  

  if(bitpos==length){ // no more bits
      return -1; // invalid code
  }
  huffman_val<<=1;
  huffman_val+=get_onebit(buff,bitpos);
  // 26bit codes
  switch(huffman_val){
    case 0x3ffffe0:
      return 192;
    case 0x3ffffe1:
      return 193;
    case 0x3ffffe2:
      return 200;
    case 0x3ffffe3:
      return 201;
    case 0x3ffffe4:
      return 202;
    case 0x3ffffe5:
      return 205;
    case 0x3ffffe6:
      return 210;
    case 0x3ffffe7:
      return 213;
    case 0x3ffffe8:
      return 218;
    case 0x3ffffe9:
      return 219;
    case 0x3ffffea:
      return 238;
    case 0x3ffffeb:
      return 240;
    case 0x3ffffec:
      return 242;
    case 0x3ffffed:
      return 243;
    case 0x3ffffee:
      return 255;
  }  

  if(bitpos==length){ // no more bits
      return -1; // invalid code
  }
  huffman_val<<=1;
  huffman_val+=get_onebit(buff,bitpos);
  // 27bit codes
  switch(huffman_val){
    case 0x3ffffde:
      return 203;
    case 0x3ffffdf:
      return 204;
    case 0x3ffffe0:
      return 211;
    case 0x3ffffe1:
      return 212;
    case 0x3ffffe2:
      return 214;
    case 0x3ffffe3:
      return 221;
    case 0x3ffffe4:
      return 222;
    case 0x3ffffe5:
      return 223;
    case 0x3ffffe6:
      return 241;
    case 0x3ffffe7:
      return 244;
    case 0x3ffffe8:
      return 245;
    case 0x3ffffe9:
      return 246;
    case 0x3ffffea:
      return 247;
    case 0x3ffffeb:
      return 248;
    case 0x3ffffec:
      return 250;
    case 0x3ffffed:
For faster browsing, not all history is shown. View entire blame