URandom.xs

filter: Statementstmt 100% 13 / 13 Branchbran 100% 6 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 100% 19 / 19 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Crypt/URandom.pm ↑ Summary
1 #define PERL_NO_GET_CONTEXT
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5 #include <sys/types.h>
6 #ifdef HAVE_CRYPT_URANDOM_NATIVE_GETRANDOM
7 #include <sys/random.h>
8 #else
9 #ifdef HAVE_CRYPT_URANDOM_SYSCALL_GETRANDOM
10 #include <sys/syscall.h>
11 #else
12 #ifdef HAVE_CRYPT_URANDOM_NATIVE_ARC4RANDOM_BUF
13 #include <sys/random.h>
14 #else
15 #ifdef HAVE_CRYPT_URANDOM_UNISTD_ARC4RANDOM_BUF
16 #include <unistd.h>
17 #else
18 #ifdef HAVE_CRYPT_URANDOM_STDLIB_ARC4RANDOM_BUF
19 #include <stdlib.h>
20 #endif
21 #endif
22 #endif
23 #endif
24 #endif
25 #ifdef GRND_NONBLOCK
26 #else
27 #define GRND_NONBLOCK 0x0001
28 #endif
29 #include <errno.h>
30  
31 MODULE = Crypt::URandom PACKAGE = Crypt::URandom PREFIX = crypt_urandom_
32 PROTOTYPES: ENABLE
33  
34 SV *
35 crypt_urandom_getrandom(length)
36 ssize_t length
37 PREINIT:
38 char *data;
39 int result;
40 CODE:
41 35 ▶ if (length < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length < 0) { 1 34 35
42 1 croak("The length argument cannot be less than 0");
43 }
44 34 Newx(data, length + 1u, char);
45 36 GETRANDOM:
46 #ifdef HAVE_CRYPT_URANDOM_NATIVE_GETRANDOM
47 36 result = getrandom(data, length, GRND_NONBLOCK);
48 #else
49 #ifdef HAVE_CRYPT_URANDOM_SYSCALL_GETRANDOM
50 result = syscall(SYS_getrandom, data, length, GRND_NONBLOCK);
51 #else
52 #ifdef HAVE_CRYPT_URANDOM_NATIVE_ARC4RANDOM_BUF
53 arc4random_buf(data, length);
54 result = length;
55 #else
56 #ifdef HAVE_CRYPT_URANDOM_UNISTD_ARC4RANDOM_BUF
57 arc4random_buf(data, length);
58 result = length;
59 #else
60 #ifdef HAVE_CRYPT_URANDOM_STDLIB_ARC4RANDOM_BUF
61 arc4random_buf(data, length);
62 result = length;
63 #else
64 croak("Unable to find getrandom or an alternative");
65 #endif
66 #endif
67 #endif
68 #endif
69 #endif
70 36 ▶ if (result != length) {
Branch: 1 branch
BranchTrueFalseTotal
if (result != length) { 4 32 36
71 4 ▶ if (errno == EINTR) {
Branch: 1 branch
BranchTrueFalseTotal
if (errno == EINTR) { 2 2 4
72 2 goto GETRANDOM;
73 } else {
74 2 Safefree(data);
75 2 croak("Failed to getrandom:%s", strerror(errno));
76 }
77 }
78 32 data[result] = '\0';
79 32 RETVAL = newSVpv(data, result);
80 32 Safefree(data);
81 OUTPUT:
82 RETVAL
« blib/lib/Crypt/URandom.pm ↑ Summary