line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Protocol::HTTP2::Huffman; |
2
|
11
|
|
|
11
|
|
23366
|
use strict; |
|
11
|
|
|
|
|
19
|
|
|
11
|
|
|
|
|
347
|
|
3
|
11
|
|
|
11
|
|
46
|
use warnings; |
|
11
|
|
|
|
|
14
|
|
|
11
|
|
|
|
|
249
|
|
4
|
11
|
|
|
11
|
|
4444
|
use Protocol::HTTP2::HuffmanCodes; |
|
11
|
|
|
|
|
39
|
|
|
11
|
|
|
|
|
1749
|
|
5
|
11
|
|
|
11
|
|
2101
|
use Protocol::HTTP2::Trace qw(tracer); |
|
11
|
|
|
|
|
16
|
|
|
11
|
|
|
|
|
4171
|
|
6
|
|
|
|
|
|
|
our ( %hcodes, %rhcodes, $hre ); |
7
|
|
|
|
|
|
|
require Exporter; |
8
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
9
|
|
|
|
|
|
|
our @EXPORT = qw(huffman_encode huffman_decode); |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
# Memory unefficient algorithm (well suited for short strings) |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub huffman_encode { |
14
|
72
|
|
|
72
|
0
|
10373
|
my $s = shift; |
15
|
72
|
|
|
|
|
10458
|
my $ret = my $bin = ''; |
16
|
72
|
|
|
|
|
10547
|
for my $i ( 0 .. length($s) - 1 ) { |
17
|
1310
|
|
|
|
|
22096
|
$bin .= $hcodes{ ord( substr $s, $i, 1 ) }; |
18
|
|
|
|
|
|
|
} |
19
|
72
|
100
|
|
|
|
10150
|
$bin .= substr( $hcodes{256}, 0, 8 - length($bin) % 8 ) if length($bin) % 8; |
20
|
72
|
|
|
|
|
20780
|
return $ret . pack( 'B*', $bin ); |
21
|
|
|
|
|
|
|
} |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
sub huffman_decode { |
24
|
61
|
|
|
61
|
0
|
9057
|
my $s = shift; |
25
|
61
|
|
|
|
|
8991
|
my $bin = unpack( 'B*', $s ); |
26
|
|
|
|
|
|
|
|
27
|
61
|
|
|
|
|
9021
|
my $c = 0; |
28
|
61
|
|
|
|
|
10180
|
$s = pack 'C*', map { $c += length; $rhcodes{$_} } ( $bin =~ /$hre/g ); |
|
1266
|
|
|
|
|
195708
|
|
|
1266
|
|
|
|
|
392577
|
|
29
|
61
|
50
|
|
|
|
9411
|
tracer->warning( |
30
|
|
|
|
|
|
|
sprintf( |
31
|
|
|
|
|
|
|
"malformed data in string at position %i, " . " length: %i", |
32
|
|
|
|
|
|
|
$c, length($bin) |
33
|
|
|
|
|
|
|
) |
34
|
|
|
|
|
|
|
) if length($bin) - $c > 8; |
35
|
61
|
50
|
|
|
|
9343
|
tracer->warning( |
36
|
|
|
|
|
|
|
sprintf "no huffman code 256 at the end of encoded string '%s': %s\n", |
37
|
|
|
|
|
|
|
substr( $s, 0, 30 ), |
38
|
|
|
|
|
|
|
substr( $bin, $c ) |
39
|
61
|
|
|
|
|
19044
|
) if $hcodes{256} !~ /^@{[ substr($bin, $c) ]}/; |
40
|
61
|
|
|
|
|
18412
|
return $s; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
1; |