| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Sys::GetRandom::PP; |
|
2
|
1
|
|
|
1
|
|
215925
|
use strict; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
38
|
|
|
3
|
1
|
|
|
1
|
|
7
|
use warnings; |
|
|
1
|
|
|
|
|
7
|
|
|
|
1
|
|
|
|
|
85
|
|
|
4
|
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
6
|
use Exporter qw(import); |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
40
|
|
|
6
|
1
|
|
|
1
|
|
6
|
use Carp qw(croak); |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
74
|
|
|
7
|
1
|
|
|
1
|
|
584
|
use Sys::GetRandom::PP::_Bits (); |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
44
|
|
|
8
|
|
|
|
|
|
|
BEGIN { |
|
9
|
1
|
|
|
1
|
|
7
|
my $pkg = do { no strict 'refs'; \%{ __PACKAGE__ . '::' } }; |
|
|
1
|
|
|
1
|
|
1
|
|
|
|
1
|
|
|
|
|
98
|
|
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
4
|
|
|
10
|
1
|
|
|
|
|
2
|
my $bits = \%Sys::GetRandom::PP::_Bits::; |
|
11
|
1
|
|
|
|
|
3
|
for my $sym (qw(GRND_RANDOM GRND_NONBLOCK _SYS_getrandom)) { |
|
12
|
3
|
|
|
|
|
501
|
$pkg->{$sym} = $bits->{$sym}; |
|
13
|
|
|
|
|
|
|
} |
|
14
|
|
|
|
|
|
|
} |
|
15
|
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
our $VERSION = '0.05'; |
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
our @EXPORT_OK = qw( |
|
19
|
|
|
|
|
|
|
GRND_RANDOM |
|
20
|
|
|
|
|
|
|
GRND_NONBLOCK |
|
21
|
|
|
|
|
|
|
getrandom |
|
22
|
|
|
|
|
|
|
random_bytes |
|
23
|
|
|
|
|
|
|
); |
|
24
|
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub getrandom ($$;$) { |
|
26
|
4
|
50
|
33
|
4
|
1
|
248034
|
if (@_ < 2 || @_ > 3) { |
|
27
|
0
|
|
|
|
|
0
|
croak 'Usage: ' . __PACKAGE__ . '::getrandom($buffer, $length, $flags = 0)'; |
|
28
|
|
|
|
|
|
|
} |
|
29
|
4
|
|
|
|
|
9
|
my $bufref = \$_[0]; |
|
30
|
4
|
|
|
|
|
12
|
my (undef, $length, $flags) = @_; |
|
31
|
4
|
|
|
|
|
10
|
$length |= 0; |
|
32
|
4
|
|
|
|
|
6
|
$flags |= 0; |
|
33
|
4
|
|
|
|
|
15
|
$$bufref .= ''; |
|
34
|
4
|
|
|
|
|
20
|
utf8::downgrade $$bufref; |
|
35
|
4
|
|
|
|
|
8
|
my $dlen = $length - length $$bufref; |
|
36
|
4
|
100
|
|
|
|
17
|
$$bufref .= $dlen > 0 ? "\0" x $dlen : ''; |
|
37
|
4
|
|
|
|
|
52
|
my $r = syscall _SYS_getrandom, $$bufref, $length, $flags; |
|
38
|
4
|
50
|
|
|
|
14
|
if ($r == -1) { |
|
39
|
0
|
0
|
|
|
|
0
|
substr($$bufref, -$dlen) = '' if $dlen > 0; |
|
40
|
0
|
|
|
|
|
0
|
return undef; |
|
41
|
|
|
|
|
|
|
} |
|
42
|
4
|
|
|
|
|
11
|
substr($$bufref, $r) = ''; |
|
43
|
4
|
|
|
|
|
38
|
$r |
|
44
|
|
|
|
|
|
|
} |
|
45
|
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub random_bytes { |
|
47
|
1
|
|
|
1
|
1
|
3
|
my ($n) = @_; |
|
48
|
1
|
|
|
|
|
3
|
$n |= 0; |
|
49
|
1
|
50
|
33
|
|
|
8
|
$n >= 0 && $n <= 256 |
|
50
|
|
|
|
|
|
|
or croak "Argument to random_bytes() must be an integer between 0 and 256, not $n"; |
|
51
|
1
|
50
|
|
|
|
5
|
return '' if $n == 0; |
|
52
|
1
|
50
|
|
|
|
3
|
defined(my $r = getrandom(my $buf, $n)) |
|
53
|
|
|
|
|
|
|
or die "Internal error: getrandom(\$buf, $n) failed: $!"; |
|
54
|
1
|
50
|
|
|
|
4
|
$r == $n |
|
55
|
|
|
|
|
|
|
or die "Internal error: getrandom(\$buf, $n) returned $r"; |
|
56
|
1
|
|
|
|
|
7
|
$buf |
|
57
|
|
|
|
|
|
|
} |
|
58
|
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
1 |
|
60
|
|
|
|
|
|
|
__END__ |