File: extract_huff_codes.pl

package info (click to toggle)
libprotocol-http2-perl 1.11-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 448 kB
  • sloc: perl: 3,370; makefile: 7
file content (53 lines) | stat: -rw-r--r-- 1,006 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/perl
#
use strict;
use warnings;

use XML::LibXML;

sub usage {
    "Usage: $0 draft-ietf-httpbis-header-compression.xml\n";
}

my $file = $ARGV[0] or die usage;
die usage unless -f $file;

open my $fh, '<', $file or die $!;
my $doc = XML::LibXML->load_xml( IO => $fh );
my $hufftable =
  XML::LibXML::XPathExpression->new(
    '//section[@title="Huffman Code"]//artwork');
my $value = $doc->findvalue($hufftable);
die "cant find Huffman Codes section" unless $value;

print << 'EOF';
package Protocol::HTTP2::HuffmanCodes;
use strict;
use warnings;
require Exporter;
our @ISA = qw(Exporter);
our ( %hcodes, %rhcodes, $hre );
our @EXPORT = qw(%hcodes %rhcodes $hre);

%hcodes = (
EOF

for ( split /\n/, $value ) {
    my ( $code, $hex, $bit ) = (/\((.{3})\).+\s([0-9a-f]+)\s+\[\s*(\d+)\]/)
      or next;
    printf "    %3d => '%0${bit}b',\n", $code, hex($hex);
}

print << 'EOF';
);

%rhcodes = reverse %hcodes;

{
    local $" = '|';
    $hre = qr/(?:^|\G)(@{[ keys %rhcodes ]})/;
}

1;
EOF