summaryrefslogtreecommitdiff
path: root/src/genpubsuffix.pl
blob: 30b6689a3c6d4a30947903a0516743d98a82bcc3 (plain)
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
#
# Public suffix C include generator
#
# Copyright 2016 Vincent Sanders <vince@kyllikki.og>
#
# Permission to use, copy, modify, and/or distribute this software for
# any purpose with or without fee is hereby granted, provided that the
# above copyright notice and this permission notice appear in all
# copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
# WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
# AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
# DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
# OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
# TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.


# This program converts the public suffix list data [1] into a C
#  program with static data representation and acessor function.
#
# The actual data list [2] should be placed in a file effective_tld_names.dat
#
# The C program is written to stdout, the typical 160K input file
#  generates 500K of program and compiles down to a 100K object file
#
# There is a single exported function
#
# const char *getpublicsuffix(const char *hostname)
#
# This returns the public suffix of the passed hostname or NULL if
#  there was an error processing the hostname. The returned pointer is
#  within the passed hostname so if the returned pointer is the same as
#  hostname the whole hostname is a public suffix otherwise the passed
#  hostname has a private part.
#
# The resulting C file is mearly a conversion of the input data (the
#  added c code is from this source and licenced under the same terms)
#  and imposes no additional copyright above that of the source data
#  file.
#
# Note: The pnode structure is built assuming there will never be more
#  label nodes than can fit in an unsigned 16 bit value (65535) but as
#  there are currently around 8000 nodes there is space for another
#  58,000 before this becomes an issue.
#
# [1] https://publicsuffix.org/
# [2] https://publicsuffix.org/list/effective_tld_names.dat


# debian package for ordered hashes: libtie-ixhash-perl
# debian package for punycode encode: libidna-punycode-perl

use strict;
use warnings;
use utf8;
use File::Basename;
use Tie::IxHash;
use IDNA::Punycode;

sub treesubdom
{
    my ($tldtree_ref, $nodeidx_ref, $strtab_ref, $stridx_ref, $parts_ref) = @_;

    my $domelem = pop @{$parts_ref}; # Doamin element
    my $isexception = 0;
    tie my %node, 'Tie::IxHash'; # this nodes hash

    # deal with explicit domain exceptions
    $isexception = ($domelem =~ s/\A!//);
    if ($isexception != 0) {
	$node{"!"} = {};
	$$nodeidx_ref += 1;
    }
    my $domelem_puny = encode_punycode($domelem);

    # Update string table
    if (! exists $strtab_ref->{$domelem_puny}) {
	# add to string table
	$strtab_ref->{$domelem_puny} = $$stridx_ref;
	{
	    use bytes;
	    # update the character count index
	    $$stridx_ref += length($domelem_puny);
	}

    }

    # link new node list into tree
    if (! exists $tldtree_ref->{$domelem_puny}) {
	$tldtree_ref->{$domelem_puny} = \%node;
	$$nodeidx_ref += 1;
    }

    # recurse down if there are more parts to the domain
    if (($isexception == 0) && (scalar(@{$parts_ref}) > 0)) {
	treesubdom($tldtree_ref->{$domelem_puny}, $nodeidx_ref, $strtab_ref, $stridx_ref, $parts_ref);
    }
}

# output an array of bytes in binary
sub phexbits
{
    use bytes;
    my %btoh = (
	"0000" => "0",
	"1000" => "1",
	"0100" => "2",
	"1100" => "3",
	"0010" => "4",
	"1010" => "5",
	"0110" => "6",
	"1110" => "7",
	"0001" => "8",
	"1001" => "9",
	"0101" => "A",
	"1101" => "B",
	"0011" => "C",
	"1011" => "D",
	"0111" => "E",
	"1111" => "F",
	);
    my ($str) = @_;
    my @nyb = unpack "a4" x ((length($str)/4)-1) . "a*", $str;
    my $ret = "";
    my $count = 0;

    do {
	my $lo = shift @nyb;
	my $hi = shift @nyb;
	$ret = $ret . "0x" . $btoh{$hi} . $btoh{$lo} . ",";
	$count += 1;
	if ($count == 16) {
	    $ret = $ret . "\n";
	    $count = 0;
	}

    } while (@nyb > 0);

    if ($count != 0) {
	$ret = $ret . "\n";
    }

    return $ret;
}


# produce encode and decode dictionary from a tree
sub walk_huffman_tree {
    my ($node, $code, $h, $rev_h) = @_;

    my $c = $node->[0];
    if (ref $c) {
	walk_huffman_tree($c->[$_], $code.$_, $h, $rev_h) for 0,1;
    } else {
	$h->{$c} = $code; $rev_h->{$code} = $c;
    }

    $h, $rev_h;
}

# use huffman encoding to generate a dictionary from a string
#
# returns a pair of hashes for encode and decode
sub mk_huffman_tree {
    my ($domelem_array_ref) = @_;

    # generate string table to get source of huffman frequencies
    my $stringtable = "*!";	# table being generated
    for my $domelem (@$domelem_array_ref) {
	my $substridx = index($stringtable, $domelem);
	if ($substridx == -1) {
	    # no existing string match to put complete label into table
	    #use bytes;
	    $stringtable .= $domelem;
	}
    }

    my (%freq, @nodes);
    for my $c (split //, $stringtable) {
	$freq{$c}++;
    }

    @nodes = map([$_, $freq{$_}], keys %freq);

    # build priority queue
    do {
	@nodes = sort {$a->[1] <=> $b->[1]} @nodes;
	my ($x, $y) = splice @nodes, 0, 2;
	push @nodes, [[$x, $y], $x->[1] + $y->[1]]
    } while (@nodes > 1);

    return @nodes;
}

sub encode {
	my ($str, $dict) = @_;
	join '', map $dict->{$_}//die("bad char $_"), split '', $str
}

# recursively walk the tree and generate nodes
#
# each huffman graph element is made up from three nodes
# [
#   [
#     [ l ],
#     [ r ]
#   ],
#   count
# ]
#
# the l and r nodes may either be additional graph elements or terminal elements
#
sub generate_huffman_node
{
    my ($dict, $node, $htableidx) = @_;

    if (! ref $node->[0]) {
	die("bad huffman tree");
    }

    my $htable = "";

    my $l = $node->[0]->[0];
    my $r = $node->[0]->[1];

    my $lhtable = "";
    my $rhtable = "";
    my $lhtableidx = 0;
    my $rhtableidx = 0;
    $htableidx += 2;

    if (ref $l->[0]) {
	($lhtable, $lhtableidx) = generate_huffman_node($dict, $l, $htableidx);
	$htable .= "    { 0, " . $htableidx. " },\n";
    } else {
	$htable .= "    { 1, '" . $l->[0] . "' }, /* '" . $l->[0] . "': " . $dict->{$l->[0]} . " */\n";
	$lhtableidx = $htableidx;
    }

    if (ref $r->[0]) {
	($rhtable, $rhtableidx) = generate_huffman_node($dict, $r, $lhtableidx);
	$htable .= "    { 0, " . $lhtableidx . " },\n" . $lhtable . $rhtable;
    } else {
	$htable .= "    { 1, '" . $r->[0] . "' }, /* '" . $r->[0] . "': " . $dict->{$r->[0]} . " */\n" . $lhtable;
	$rhtableidx = $lhtableidx;
    }

    $htable, $rhtableidx;

}

# generate c code to represent encoding used
sub generate_huffman_table
{
    my ($hnodes_ref, $dict) = @_;
    my ($htable, $htablesize) = generate_huffman_node($dict, $hnodes_ref->[0], 0);
    my $ret;

    $ret .= "/**\n";
    $ret .= " * Huffman coding node\n";
    $ret .= " */\n";
    $ret .= "struct hnode {\n";
    $ret .= "    unsigned int term:1; /**< non zero if the node terminates a code */\n";
    $ret .= "    unsigned int value:7; /**< value in node */\n";
    $ret .= "};\n\n";

    $ret .= "/**\n";
    $ret .= " * Huffman decoding table\n";
    $ret .= " *\n";
    $ret .= " * nodes are in pairs even nodes are for 0bit odd for 1\n";
    $ret .= " */\n";
    $ret .= "static const struct hnode htable[" . $htablesize . "] = {\n";
    $ret .= $htable;
    $ret .= "};\n\n";

    return $ret;
}

# output string table
#
# array of characters the node table below directly indexes entries.
sub generate_string_table
{
    my ($tldtree_ref, $nodeidx_ref, $strtab_ref, $stridx_ref) = @_;

    # obtain sorted array of domain label strings
    my $labcount = 0; # total number of labels
    my $labsize = 0; # total size of labels
    my @tmp_array;
    foreach my $key (keys %$strtab_ref) {
	use bytes;
	push(@tmp_array, $key);
	$labcount += 1;
	$labsize += length($key);
    }
    my @domelem_array = sort { length($b) <=> length($a) } @tmp_array;

    # build huffman tree
    my @hnodes = mk_huffman_tree(\@domelem_array);

    # generate huffman encoding dictionary
    my ($h, $rev_h) = walk_huffman_tree($hnodes[0], '', {}, {});

    # generate bit table using huffman encoding
    my $bittable = encode("*", $h); # table being generated
    my $exceptidx = length($bittable);
    $bittable .= encode("!", $h);
    my $bittablesize = length($bittable);
    my $labfullcount = 2; # labels inserted into the table in full
    for my $domelem (@domelem_array) {
	my $domelembits = encode($domelem, $h);
	my $substridx = index($bittable, $domelembits);
	if ($substridx != -1) {
	    # found existing string match so use it
	    $strtab_ref->{$domelem} = $substridx;
	} else {
	    # no existing string match to put complete label into table
	    use bytes;
	    $strtab_ref->{$domelem} = $bittablesize;
	    $bittable .= $domelembits;
	    $bittablesize += length($domelembits);
	    $labfullcount += 1;
	}
    }
    my $srounding = $bittablesize % 8;
    if ($srounding > 0) {
	# ensure bittable is a multiple of 8 bits long for byte array generation
	$bittable .= '0' x (8 - $srounding);
	$bittablesize += (8 - $srounding);
    }

    print "enum stab_entities {\n";
    print "    STAB_WILDCARD = 0,\n";
    print "    STAB_EXCEPTION = " . $exceptidx . "\n";
    print "};\n\n";

    # output the node table to allow decoding
    print generate_huffman_table(\@hnodes, $h);


    print "/**\n";
    print " * Domain label string table huffman encoded.\n";
    print " * " . $labcount . " labels(" . $labsize * 8 . " bits) reduced to " . $labfullcount . " labels(" . $bittablesize . " bits)\n";
    print " */\n";
    print "static const uint8_t stab[" . ($bittablesize / 8) . "] = {\n";
    print phexbits($bittable);
    print "};\n\n";

}


# Output the length of the string
sub pstr_len
{
    use bytes;

    my ($str) = @_;
    my $ret;

    my @bytes = unpack('C*', $str);

    $ret = $ret . sprintf("%d", scalar(@bytes));

    return $ret;
}

# generate all the children of a parent node and recurse into each of
#  those updating optidx to point to the next free node
sub calc_pnode
{
    my ($parent_ref, $strtab_ref, $opidx_ref, $nodecount_ref) = @_;
    my $our_dat;
    my $child_dat = "";
    my $startidx = $$opidx_ref;
    my $lineidx = -1;

    # update the output index to after this node
    # need to allow for an additional node for each entry with children

    # iterate over each child element domain/ref pair
    while ( my ($cdom, $cref) = each(%$parent_ref) ) {
	if (scalar keys (%$cref) != 0) {
	    $$opidx_ref += 2;
	} else {
	    $$opidx_ref += 1;
	}
    }

    # entry block
    if ($startidx == ($$opidx_ref - 1)) {
	$our_dat = "\n    /* entry " . $startidx . " */\n";
    } else {
	$our_dat = "\n    /* entries " . $startidx . " to " . ($$opidx_ref - 1) . " */\n";
    }

    # iterate over each child element domain/ref pair
    while ( my ($cdom, $cref) = each(%$parent_ref) ) {
	my $child_count = scalar keys (%$cref);

	$$nodecount_ref += 1; # keep count of number of nodes in tree

	$our_dat .= "    { .label = {" . sprintf("% 7d", $strtab_ref->{$cdom}) . ",". sprintf("% 3d", pstr_len($cdom));
	if ($child_count == 0) {
	    # complete label for no children
	    $our_dat .= ", 0 } },";
	} else {
	    # complete label with children
	    $our_dat .= ", 1 } }, ";
	    $our_dat .= "{ .child = { " . $$opidx_ref . ", " . $child_count . " } },";
	    $child_dat .= calc_pnode($cref, $strtab_ref, $opidx_ref, $nodecount_ref);
	}
	$our_dat .= " /* " . $cdom . " */\n";
    }

    return $our_dat . $child_dat;
}

# main
binmode(STDOUT, ":utf8");

my ($filename) = @ARGV;

if (not defined $filename) {
    die "need filename\n";
}

open(my $fh, '<:encoding(UTF-8)', $filename)
    or die "Could not open file '$filename' $!";

tie my %tldtree, 'Tie::IxHash'; # node tree
my $nodeidx = 1; # count of nodes allowing for the root node

tie my %strtab, 'Tie::IxHash'; # string table
my $stridx = 0;

# put the wildcard match at 0 in the string table
$strtab{'*'} = $stridx;
$stridx += 1;

# put the invert match at 1 in the string table
$strtab{'!'} = $stridx;
$stridx += 1;

# read each line from prefix data and inject into hash tree
while (my $line = <$fh>) {
    chomp $line;

    if (($line ne "") && ($line !~ /\/\/.*$/)) {

	# print "$line\n";
	my @parts=split("\\.", $line);

	# recusrsive call to build tree from root

	treesubdom(\%tldtree, \$nodeidx, \%strtab, \$stridx, \@parts);
    }
}


# C program header
print "/*\n";
print " * Generated with the genpubsuffix tool.\n";
print " *  From file " . basename($filename) . "\n";
print " *  Converted on " . localtime() . "\n";
print " */\n\n";

print "/**\n";
print " * Public suffix list graph node\n";
print " */\n";
print "union pnode {\n";
print "    struct {\n";
print "        unsigned int idx:24; /**< index of domain element in string table */\n";
print "        unsigned int len:6; /**< length of domain element in string table */\n";
print "        unsigned int children:1; /**< has children */\n";
print "    } label;\n";
print "    struct {\n";
print "        uint16_t index; /**< index of first child node */\n";
print "        uint16_t count; /**< number of children of this node */\n";
print "    } child;\n";
print "};\n\n";

generate_string_table(\%tldtree, \$nodeidx, \%strtab, \$stridx);

# output static node array
#
# The constructed array of nodes has all siblings sequentialy and an
# index/count to its children. This yeilds a very compact data
# structure easily traversable.
#
# Additional flags for * (match all) and ! (exception) are omitted as
# they can be infered by having a node with a label of 0 (*) or 1 (!)
# as the string table has those values explicitly created.
#
# As labels cannot be more than 63 characters a byte length is more
# than sufficient.

my $opidx = 2; # output index of node
my $opnodes = ""; # output pnode initialisers
my $opnodecount = 1; # output domain label nodes

# root node initialiser
$opnodes .= "    /* root entry */\n";
$opnodes .= "    { .label = { 0, 0, 1 } }, { .child = { " . $opidx . ", " . scalar keys(%tldtree) . " } },";

# generate node initialiser
$opnodes .= calc_pnode(\%tldtree, \%strtab, \$opidx, \$opnodecount);

print "/**\n";
print " * PSL represented as a directed acyclic graph\n";
print " * There are " . $opnodecount . " labels in " . $opidx . " nodes\n";
print " */\n";
print "static const union pnode pnodes[" . $opidx . "] = {\n";
print $opnodes; # output node initialisors
print "\n};\n\n";