| 1 | package Protocol::HTTP2::Huffman; | ||||||||||
| 2 | 14 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 14 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 14 | ▶ | use Protocol::HTTP2::HuffmanCodes; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 14 | ▶ | use Protocol::HTTP2::Trace qw(tracer); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 14 | our ( %hcodes, %rhcodes, $hre ); | |||||||||
| 7 | 14 | require Exporter; | |||||||||
| 8 | 14 | our @ISA = qw(Exporter); | |||||||||
| 9 | 14 | our @EXPORT = qw(huffman_encode huffman_decode); | |||||||||
| 10 | |||||||||||
| 11 | # Memory unefficient algorithm (well suited for short strings) | ||||||||||
| 12 | |||||||||||
| 13 | 103 | ▶ | sub huffman_encode { | ||||||||
|
Subroutine
huffman_encode: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 14 | 103 | my $s = shift; | |||||||||
| 15 | 103 | my $ret = my $bin = ''; | |||||||||
| 16 | 103 | for my $i ( 0 .. length($s) - 1 ) { | |||||||||
| 17 | 37284 | $bin .= $hcodes{ ord( substr $s, $i, 1 ) }; | |||||||||
| 18 | } | ||||||||||
| 19 | 103 | ▶ | $bin .= substr( $hcodes{256}, 0, 8 - length($bin) % 8 ) if length($bin) % 8; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 20 | 103 | return $ret . pack( 'B*', $bin ); | |||||||||
| 21 | } | ||||||||||
| 22 | |||||||||||
| 23 | 78 | ▶ | sub huffman_decode { | ||||||||
|
Subroutine
huffman_decode: called CC=3 SCAR=11.1
Pod
undocumented
| |||||||||||
| 24 | 78 | my $s = shift; | |||||||||
| 25 | 78 | my $bin = unpack( 'B*', $s ); | |||||||||
| 26 | |||||||||||
| 27 | 78 | my $c = 0; | |||||||||
| 28 | 78 | $s = pack 'C*', map { $c += length; $rhcodes{$_} } ( $bin =~ /$hre/g ); | |||||||||
| 29 | 78 | ▶ | tracer->warning( | ||||||||
|
Branch: 1 branch
| |||||||||||
| 30 | sprintf( | ||||||||||
| 31 | "malformed data in string at position %i, " . " length: %i", | ||||||||||
| 32 | $c, length($bin) | ||||||||||
| 33 | ) | ||||||||||
| 34 | ) if length($bin) - $c > 8; | ||||||||||
| 35 | 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 | 78 | ▶ | ) if $hcodes{256} !~ /^@{[ substr($bin, $c) ]}/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 40 | 78 | return $s; | |||||||||
| 41 | } | ||||||||||
| 42 | |||||||||||
| 43 | 14 | 1; | |||||||||