Skip to content

Instantly share code, notes, and snippets.

@kinichiro
Created March 7, 2020 13:16
Show Gist options
  • Save kinichiro/c857af07a824e1562f2aafb2e55711c0 to your computer and use it in GitHub Desktop.
Save kinichiro/c857af07a824e1562f2aafb2e55711c0 to your computer and use it in GitHub Desktop.
convert openssl-1.1.1d/test/recipes/30-test_evp_data/evpencod.txt to base64test.c data
#!/usr/bin/perl
my $infile = $ARGV[0];
my $num, $comment, $encoding, $input, $output;
my $inlen, $outlen, $validlen;
open(IN, "< $infile") || die "open";
$num = 0;
while (<IN>) {
chop $_;
if ($_ =~ /^Title = /) {
$comment = "";
} elsif ($_ =~ /^#/) {
$comment = $_;
} elsif ($_ =~ /^Encoding = /) {
++$num;
$encoding = substr($_, 11);
$input = "";
$output = "";
} elsif ($_ =~ /^Input = /) {
$input = substr($_, 8);
adjust_data(\$input, \$inlen);
} elsif ($_ =~ /^Output = /) {
$output = substr($_, 9);
adjust_data(\$output, \$outlen);
} elsif ($_ eq "" && $num > 0 && $encoding ne "") {
if ($encoding eq "canonical") {
$validlen = $inlen;
} elsif ($encoding eq "valid") {
$validlen = $inlen;
$inlen = -1;
} elsif ($encoding eq "invalid") {
$input = "";
$inlen = -1;
$validlen = 0;
}
if ($comment eq "") {
printf "\t/* %s */\n", $encoding;
} else {
printf "\t/* %s %s */\n", $encoding, $comment;
}
printf "\t{ \"%s\", %d, \"%s\", %d, %d, },\n",
$input, $inlen, $output, $outlen, $validlen;
$comment = $encoding = $input = $output = "";
$inlen = $outlen = $validlen = 0;
}
}
close(IN);
sub adjust_data {
my ($data, $datalen) = @_;
my $i, $buf, $len, $tmp;
$buf = $$data;
if (substr($buf, 0, 1) eq '"') {
# remove heading and trailing quote
$buf = substr($buf, 1);
chop $buf;
$len = length($buf);
# check line break
$i = 0;
while(($i = index($buf, '\n', $i)) != -1) {
--$len;
$i += 2;
}
} else {
# transform from unsigned char to hex string
$len = length($buf);
for ($i = 0, $tmp = ""; $i < $len; $i += 2) {
$tmp = $tmp . "\\x" . substr($buf, $i, 2);
}
$len = length($buf) / 2;
$buf = $tmp;
}
$$data = $buf;
$$datalen = $len;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment