Files
pcp/bindings/py/gencffi.pl

95 lines
2.5 KiB
Perl
Raw Normal View History

2014-12-14 14:38:30 +01:00
#!/usr/bin/perl
use Data::Dumper;
my %sobytes = (
'crypto_box_NONCEBYTES' => 24,
'crypto_box_PUBLICKEYBYTES' => 32,
'crypto_box_SECRETKEYBYTES' => 32,
'crypto_box_ZEROBYTES' => 32,
'crypto_box_BOXZEROBYTES' => 16,
'crypto_box_MACBYTES' => 16,
'crypto_secretbox_KEYBYTES' => 32,
'crypto_secretbox_NONCEBYTES' => 24,
'crypto_secretbox_ZEROBYTES' => 32,
'crypto_secretbox_BOXZEROBYTES' => 16,
'crypto_secretbox_MACBYTES' => 16,
'crypto_sign_PUBLICKEYBYTES' => 32,
'crypto_sign_SECRETKEYBYTES' => 64,
'crypto_sign_SEEDBYTES' => 32,
'crypto_sign_BYTES' => 64,
'crypto_stream_KEYBYTES' => 32,
'crypto_stream_NONCEBYTES' => 24,
'crypto_generichash_BYTES' => 32,
'crypto_scalarmult_curve25519_BYTES' => 32,
'crypto_scalarmult_BYTES' => 32,
'crypto_generichash_BYTES_MAX' => 64,
);
2015-04-18 20:24:24 +02:00
my @ignore = qw(digital_crc32.h base85.h uthash.h);
2014-12-14 14:38:30 +01:00
my @code;
2015-04-18 20:24:24 +02:00
my @structs;
my @typedefs;
my %defs;
2014-12-14 14:38:30 +01:00
foreach my $head (@ARGV) {
2015-04-18 20:24:24 +02:00
next if grep { $head =~ $_} @ignore;
2014-12-14 14:38:30 +01:00
open HEAD, "<$head" or die "Could not open $head: $!\n";
my $raw = join '', <HEAD>;
# resolve sodium constants
foreach my $sobyte (sort { length($b) <=> length($a) } keys %sobytes) {
$raw =~ s/$sobyte/$sobytes{$sobyte}/g;
}
# some sizes are calculated, cffi doesn't so do we
$raw =~ s/(\d+) \+ (\d+)/$1 + $2/ge;
# 1line type
while ($raw =~ /^(typedef .*;)/gm) {
2015-04-18 20:24:24 +02:00
push @typedefs, ('', "/*** $0: from $head:$. */");
push @typedefs, $1;
2014-12-14 14:38:30 +01:00
}
# a struct
# the uthash handle doesn't resolve, so we
# use a placeholder
while ($raw =~ /(struct [^\s]* \{[^\}]*\};)/gs) {
my $code = $1;
$code =~ s/UT_hash_handle hh/byte hh[56]/g;
2015-04-18 20:24:24 +02:00
push @structs, ('', "/*** $0: from $head:$. */");
push @structs, $code;
2014-12-14 14:38:30 +01:00
}
# a function
while ($raw =~ /^([a-zA-Z].*\(.*\);)/gm) {
my $c = $1;
push @code, ('', "/*** $0: from $head:$. */");
push @code, $c;
}
# a definition
while ($raw =~ /^\s*#define ((EXP|PCP|PBP).*)$/gm) {
my ($name, $def) = split /\s\s*/, $1, 2;
$def =~ s/\/\*.*//;
2015-04-18 20:24:24 +02:00
next if ($name =~ /_VERSION/);
if (!exists $defs{$name} && $def !~ /(sizeof| \+ )/) {
$defs{$name} = "\n# $0: from $head:$.\n$name = $def\n";
}
}
2014-12-14 14:38:30 +01:00
close $head;
2015-04-18 20:24:24 +02:00
$. = 0;
2014-12-14 14:38:30 +01:00
}
print "PCP_RAW_CODE = '''\n";
2015-04-18 20:24:24 +02:00
print join "\n", @typedefs;
print join "\n", @structs;
2014-12-14 14:38:30 +01:00
print join "\n", @code;
print "'''\n";
print join "\n", values %defs;