XS.xs

filter: Statementstmt 96.4% 27 / 28 Branchbran 83.3% 10 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 92.5% 37 / 40 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« des.c ↑ Summary md5.c »
1 /*-
2 * Copyright (c) 2011 cPanel, Inc.
3 * All rights reserved.
4 *
5 * This library is free software; you can redistribute it and/or modify
6 * it under the same terms as Perl itself, either Perl version 5.10.1 or,
7 * at your option, any later version of Perl 5 you may have available.
8 */
9  
10 #include "EXTERN.h"
11 #include "perl.h"
12 #include "XSUB.h"
13 #include "ppport.h"
14  
15 #include "md5crypt.h"
16 #include "des.h"
17 #include "sha256crypt.h"
18 #include "sha512crypt.h"
19 #include "yescrypt_crypt.h"
20  
21  
22 typedef char *(*crypt_function_t)(const char*, const char*);
23  
24 /* The enum and map are in the same order for easy lookup */
25 typedef enum { MD5 = 0, APR1, DES, SHA256, SHA512, YESCRYPT } crypt_scheme_t;
26  
27 crypt_function_t crypt_function_map[] = {
28 cpx_crypt_md5,
29 cpx_crypt_apr1,
30 cpx_crypt_des,
31 cpx_sha256_crypt,
32 cpx_sha512_crypt,
33 NULL /* YESCRYPT: reentrant, called directly in _multi_crypt() */
34 };
35  
36 /* This function performs all cleanup of input and calls the correct C crypt function */
37 124 SV* _multi_crypt(crypt_scheme_t scheme, SV *pw, SV *salt) {
38 124 char *cryptpw_cstr = NULL;
39 124 char *pw_cstr = NULL;
40 124 char *salt_cstr = NULL;
41 124 SV* RETVAL = &PL_sv_undef;
42 124 ▶ if (SvPOK(pw)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvPOK(pw)) { 113 11 124
43 113 pw_cstr = SvPVX(pw);
44 } else {
45 11 pw_cstr = "";
46 }
47 124 ▶ if (SvPOK(salt)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvPOK(salt)) { 116 8 124
48 116 salt_cstr = SvPVX(salt);
49 } else {
50 8 salt_cstr = "\0";
51 }
52 124 ▶ if (scheme == YESCRYPT) {
Branch: 1 branch
BranchTrueFalseTotal
if (scheme == YESCRYPT) { 39 85 124
53 /* A per-call buffer, so concurrent ithreads never share the result. */
54 char buf[CPX_YESCRYPT_BUFLEN];
55 39 cryptpw_cstr = cpx_yescrypt_crypt( pw_cstr, salt_cstr, buf, sizeof(buf) );
56 /* Copy out before buf goes out of scope. */
57 39 ▶ if (cryptpw_cstr != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (cryptpw_cstr != NULL) { 39 0 39
58 39 RETVAL = newSVpv(cryptpw_cstr,0);
59 }
60 39 return RETVAL;
61 }
62 85 ▶ if (crypt_function_map[scheme] == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (crypt_function_map[scheme] == NULL) { 0 85 85
63 0 return RETVAL;
64 }
65 85 cryptpw_cstr = crypt_function_map[scheme]( pw_cstr, salt_cstr );
66 85 ▶ if (cryptpw_cstr != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (cryptpw_cstr != NULL) { 82 3 85
67 82 RETVAL = newSVpv(cryptpw_cstr,0);
68 }
69 85 return RETVAL;
70 }
71  
72 MODULE = Crypt::Passwd::XS PACKAGE = Crypt::Passwd::XS
73  
74 PROTOTYPES: ENABLE
75  
76 SV*
77 unix_md5_crypt(pw,salt)
78 SV *pw;
79 SV *salt;
80  
81 CODE:
82 9 RETVAL = _multi_crypt(MD5, pw, salt);
83  
84 OUTPUT:
85 RETVAL
86  
87 SV*
88 apache_md5_crypt(pw,salt)
89 SV *pw;
90 SV *salt;
91  
92 CODE:
93 8 RETVAL = _multi_crypt(APR1, pw, salt);
94  
95 OUTPUT:
96 RETVAL
97  
98 SV*
99 unix_des_crypt(pw,salt)
100 SV *pw;
101 SV *salt;
102  
103 CODE:
104 28 RETVAL = _multi_crypt(DES, pw, salt);
105  
106 OUTPUT:
107 RETVAL
108  
109 SV*
110 unix_sha256_crypt(pw,salt)
111 SV *pw;
112 SV *salt;
113  
114 CODE:
115 20 RETVAL = _multi_crypt(SHA256, pw, salt);
116  
117 OUTPUT:
118 RETVAL
119  
120 SV*
121 unix_sha512_crypt(pw,salt)
122 SV *pw;
123 SV *salt;
124  
125 CODE:
126 20 RETVAL = _multi_crypt(SHA512, pw, salt);
127  
128 OUTPUT:
129 RETVAL
130  
131 SV*
132 unix_yescrypt_crypt(pw,salt)
133 SV *pw;
134 SV *salt;
135  
136 CODE:
137 39 RETVAL = _multi_crypt(YESCRYPT, pw, salt);
138  
139 OUTPUT:
140 RETVAL
« des.c ↑ Summary md5.c »