inc/CryptX_Digest.xs.inc

filter: Statementstmt 97.5% 79 / 81 Branchbran 63.1% 53 / 84 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 80.0% 132 / 165 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« inc/CryptX_Digest_KangarooTwelve.xs.inc ↑ Summary inc/CryptX_PK_ECC.xs.inc »
1 MODULE = CryptX PACKAGE = Crypt::Digest
2  
3 PROTOTYPES: DISABLE
4  
5 Crypt::Digest
6 new(char * cname, char * pname = NULL)
7 CODE:
8 {
9 int rv;
10 int id;
11 3494 ▶ char *digest_name = strcmp(cname, "Crypt::Digest") == 0 ? pname : cname;
Branch: 1 branch
BranchTrueFalseTotal
char *digest_name = strcmp(cname, "Crypt::Digest") == 0 ? pname : cname; 3494 0 3494
12  
13 3494 id = cryptx_internal_find_hash(digest_name);
14 3494 ▶ if (id == -1) croak("FATAL: find_hash failed for '%s'", digest_name);
Branch: 1 branch
BranchTrueFalseTotal
if (id == -1) croak("FATAL: find_hash failed for '%s'", digest_name); 0 3494 3494
15  
16 3494 Newz(0, RETVAL, 1, struct digest_struct);
17 3494 ▶ if (!RETVAL) croak("FATAL: Newz failed");
Branch: 1 branch
BranchTrueFalseTotal
if (!RETVAL) croak("FATAL: Newz failed"); 0 3494 3494
18  
19 3494 RETVAL->desc = &hash_descriptor[id];
20 3494 rv = RETVAL->desc->init(&RETVAL->state);
21 3494 ▶ if (rv != CRYPT_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != CRYPT_OK) { 0 3494 3494
22 0 Safefree(RETVAL);
23 0 croak("FATAL: digest setup failed: %s", error_to_string(rv));
24 }
25 }
26 OUTPUT:
27 RETVAL
28  
29 void
30 DESTROY(Crypt::Digest self)
31 CODE:
32 {
33 3599 zeromem(self, sizeof(*self));
34 3599 Safefree(self);
35 }
36  
37 void
38 reset(Crypt::Digest self)
39 PPCODE:
40 {
41 int rv;
42 35 rv = self->desc->init(&self->state);
43 35 ▶ if (rv != CRYPT_OK) croak("FATAL: digest init failed: %s", error_to_string(rv));
Branch: 1 branch
BranchTrueFalseTotal
if (rv != CRYPT_OK) croak("FATAL: digest init failed: %s", error_to_string(rv)); 0 35 35
44 35 self->finalized = 0;
45 35 ▶ XPUSHs(ST(0)); /* return self */
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(ST(0)); /* return self */ 0 35 35
46 }
47  
48 Crypt::Digest
49 clone(Crypt::Digest self)
50 CODE:
51 105 Newz(0, RETVAL, 1, struct digest_struct);
52 105 ▶ if (!RETVAL) croak("FATAL: Newz failed");
Branch: 1 branch
BranchTrueFalseTotal
if (!RETVAL) croak("FATAL: Newz failed"); 0 105 105
53 105 Copy(self, RETVAL, 1, struct digest_struct);
54 OUTPUT:
55 RETVAL
56  
57 void
58 add(Crypt::Digest self, ...)
59 PPCODE:
60 {
61 STRLEN inlen;
62 int rv, i;
63 unsigned char *in;
64  
65 3528 ▶ if (self->finalized) croak("FATAL: digest object already finalized");
Branch: 1 branch
BranchTrueFalseTotal
if (self->finalized) croak("FATAL: digest object already finalized"); 35 3493 3528
66 7091 ▶ for(i = 1; i < items; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 1; i < items; i++) { 3598 3493 7091
67 3598 in = (unsigned char *)SvPVbyte(ST(i), inlen);
68 3598 ▶ if (inlen > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (inlen > 0) { 3550 48 3598
69 3550 rv = self->desc->process(&self->state, in, (unsigned long)inlen);
70 3550 ▶ if (rv != CRYPT_OK) croak("FATAL: digest process failed: %s", error_to_string(rv));
Branch: 1 branch
BranchTrueFalseTotal
if (rv != CRYPT_OK) croak("FATAL: digest process failed: %s", error_to_string(rv)); 0 3550 3550
71 }
72 }
73 3493 ▶ XPUSHs(ST(0)); /* return self */
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(ST(0)); /* return self */ 0 3493 3493
74 }
75  
76 SV *
77 digest(Crypt::Digest self)
78 ALIAS:
79 hexdigest = 1
80 b64digest = 2
81 b64udigest = 3
82 CODE:
83 {
84 int rv;
85 unsigned long outlen;
86 unsigned char hash[MAXBLOCKSIZE];
87 char out[MAXBLOCKSIZE*2+1];
88  
89 3563 ▶ if (self->finalized) croak("FATAL: digest object already finalized");
Branch: 1 branch
BranchTrueFalseTotal
if (self->finalized) croak("FATAL: digest object already finalized"); 70 3493 3563
90 3493 rv = self->desc->done(&self->state, hash);
91 3493 ▶ if (rv != CRYPT_OK) croak("FATAL: digest done failed: %s", error_to_string(rv));
Branch: 1 branch
BranchTrueFalseTotal
if (rv != CRYPT_OK) croak("FATAL: digest done failed: %s", error_to_string(rv)); 0 3493 3493
92 3493 self->finalized = 1;
93  
94 3493 outlen = sizeof(out);
95 3493 ▶ if (ix == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (ix == 3) { 175 3318 3493
96 175 rv = base64url_encode(hash, self->desc->hashsize, out, &outlen);
97 175 ▶ if (rv != CRYPT_OK) croak("FATAL: base64url_encode failed: %s", error_to_string(rv));
Branch: 1 branch
BranchTrueFalseTotal
if (rv != CRYPT_OK) croak("FATAL: base64url_encode failed: %s", error_to_string(rv)); 0 175 175
98 175 RETVAL = newSVpvn(out, outlen);
99 }
100 3318 ▶ else if (ix == 2) {
Branch: 1 branch
BranchTrueFalseTotal
else if (ix == 2) { 315 3003 3318
101 315 rv = base64_encode(hash, self->desc->hashsize, out, &outlen);
102 315 ▶ if (rv != CRYPT_OK) croak("FATAL: base64_encode failed: %s", error_to_string(rv));
Branch: 1 branch
BranchTrueFalseTotal
if (rv != CRYPT_OK) croak("FATAL: base64_encode failed: %s", error_to_string(rv)); 0 315 315
103 315 RETVAL = newSVpvn(out, outlen);
104 }
105 3003 ▶ else if (ix == 1) {
Branch: 1 branch
BranchTrueFalseTotal
else if (ix == 1) { 2688 315 3003
106 2688 rv = base16_encode(hash, self->desc->hashsize, out, &outlen, 0);
107 2688 ▶ if (rv != CRYPT_OK) croak("FATAL: base16_encode failed: %s", error_to_string(rv));
Branch: 1 branch
BranchTrueFalseTotal
if (rv != CRYPT_OK) croak("FATAL: base16_encode failed: %s", error_to_string(rv)); 0 2688 2688
108 2688 RETVAL = newSVpvn(out, outlen);
109 }
110 else {
111 315 RETVAL = newSVpvn((char *) hash, self->desc->hashsize);
112 }
113 }
114 OUTPUT:
115 RETVAL
116  
117 SV *
118 digest_data(char * digest_name, ...)
119 ALIAS:
120 digest_data_hex = 1
121 digest_data_b64 = 2
122 digest_data_b64u = 3
123 CODE:
124 {
125 STRLEN inlen;
126 int rv, id, i;
127 unsigned char *in, hash[MAXBLOCKSIZE];
128 1049 unsigned long len = sizeof(hash), outlen;
129 char out[MAXBLOCKSIZE*2+1];
130 hash_state md;
131  
132 1049 id = cryptx_internal_find_hash(digest_name);
133 1049 ▶ if (id == -1) croak("FATAL: find_digest failed for '%s'", digest_name);
Branch: 1 branch
BranchTrueFalseTotal
if (id == -1) croak("FATAL: find_digest failed for '%s'", digest_name); 0 1049 1049
134  
135 /* digest_data("SHA1", $data1, $data2, $data3); */
136 1049 len = hash_descriptor[id].hashsize;
137 1049 rv = hash_descriptor[id].init(&md);
138 1049 ▶ if (rv != CRYPT_OK) croak("FATAL: digest init failed: %s", error_to_string(rv));
Branch: 1 branch
BranchTrueFalseTotal
if (rv != CRYPT_OK) croak("FATAL: digest init failed: %s", error_to_string(rv)); 0 1049 1049
139 2658 ▶ for (i = 1; i < items; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i++) { 1609 1049 2658
140 1609 in = (unsigned char *)SvPVbyte(ST(i), inlen);
141 1609 ▶ if (inlen > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (inlen > 0) { 1364 245 1609
142 1364 rv = hash_descriptor[id].process(&md, in, (unsigned long)inlen);
143 1364 ▶ if (rv != CRYPT_OK) croak("FATAL: digest process failed: %s", error_to_string(rv));
Branch: 1 branch
BranchTrueFalseTotal
if (rv != CRYPT_OK) croak("FATAL: digest process failed: %s", error_to_string(rv)); 0 1364 1364
144 }
145 }
146 1049 rv = hash_descriptor[id].done(&md, hash);
147 1049 ▶ if (rv != CRYPT_OK) croak("FATAL: digest done failed: %s", error_to_string(rv));
Branch: 1 branch
BranchTrueFalseTotal
if (rv != CRYPT_OK) croak("FATAL: digest done failed: %s", error_to_string(rv)); 0 1049 1049
148  
149 1049 outlen = sizeof(out);
150 1049 ▶ if (ix == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (ix == 3) { 175 874 1049
151 175 rv = base64url_encode(hash, len, out, &outlen);
152 175 ▶ if (rv != CRYPT_OK) croak("FATAL: base64url_encode failed: %s", error_to_string(rv));
Branch: 1 branch
BranchTrueFalseTotal
if (rv != CRYPT_OK) croak("FATAL: base64url_encode failed: %s", error_to_string(rv)); 0 175 175
153 175 RETVAL = newSVpvn(out, outlen);
154 }
155 874 ▶ else if (ix == 2) {
Branch: 1 branch
BranchTrueFalseTotal
else if (ix == 2) { 280 594 874
156 280 rv = base64_encode(hash, len, out, &outlen);
157 280 ▶ if (rv != CRYPT_OK) croak("FATAL: base64_encode failed: %s", error_to_string(rv));
Branch: 1 branch
BranchTrueFalseTotal
if (rv != CRYPT_OK) croak("FATAL: base64_encode failed: %s", error_to_string(rv)); 0 280 280
158 280 RETVAL = newSVpvn(out, outlen);
159 }
160 594 ▶ else if (ix == 1) {
Branch: 1 branch
BranchTrueFalseTotal
else if (ix == 1) { 282 312 594
161 282 rv = base16_encode(hash, len, out, &outlen, 0);
162 282 ▶ if (rv != CRYPT_OK) croak("FATAL: base16_encode failed: %s", error_to_string(rv));
Branch: 1 branch
BranchTrueFalseTotal
if (rv != CRYPT_OK) croak("FATAL: base16_encode failed: %s", error_to_string(rv)); 0 282 282
163 282 RETVAL = newSVpvn(out, outlen);
164 }
165 else {
166 312 RETVAL = newSVpvn((char *) hash, len);
167 }
168 }
169 OUTPUT:
170 RETVAL
171  
172 int
173 hashsize(SV * param, char * extra = NULL)
174 CODE:
175 {
176 1962 ▶ if (sv_isobject(param) && sv_derived_from(param, "Crypt::Digest")) {
Branch: 2 branches
BranchTrueFalseTotal
if (sv_isobject(param) && sv_derived_from(param, "Crypt::Digest")) { 35 1927 1962
if (sv_isobject(param) && sv_derived_from(param, "Crypt::Digest")) { 35 0 35
177 35 IV tmp = SvIV((SV*)SvRV(param));
178 35 Crypt__Digest obj = INT2PTR(Crypt__Digest, tmp);
179 35 RETVAL = obj->desc->hashsize;
180 }
181 else {
182 char *digest_name;
183 STRLEN plen;
184 int rv, id;
185 3994 ▶ digest_name = SvPOK_spec(param) && strcmp(SvPVbyte(param, plen), "Crypt::Digest") ? SvPVbyte(param, plen) : extra;
Branch: 6 branches
BranchTrueFalseTotal
digest_name = SvPOK_spec(param) && strcmp(SvPVbyte(param, plen), "Crypt::Digest") ? SvPVbyte(param, plen) : extra; 1927 0 1927
digest_name = SvPOK_spec(param) && strcmp(SvPVbyte(param, plen), "Crypt::Digest") ? SvPVbyte(param, plen) : extra; 0 1927 1927
digest_name = SvPOK_spec(param) && strcmp(SvPVbyte(param, plen), "Crypt::Digest") ? SvPVbyte(param, plen) : extra; 0 0 0
digest_name = SvPOK_spec(param) && strcmp(SvPVbyte(param, plen), "Crypt::Digest") ? SvPVbyte(param, plen) : extra; 0 0 0
digest_name = SvPOK_spec(param) && strcmp(SvPVbyte(param, plen), "Crypt::Digest") ? SvPVbyte(param, plen) : extra; 0 0 0
digest_name = SvPOK_spec(param) && strcmp(SvPVbyte(param, plen), "Crypt::Digest") ? SvPVbyte(param, plen) : extra; 140 1787 1927
186 1927 id = cryptx_internal_find_hash(digest_name);
187 1927 ▶ if (id == -1) croak("FATAL: find_hash failed for '%s'", digest_name);
Branch: 1 branch
BranchTrueFalseTotal
if (id == -1) croak("FATAL: find_hash failed for '%s'", digest_name); 0 1927 1927
188 1927 rv = hash_descriptor[id].hashsize;
189 1927 ▶ if (!rv) croak("FATAL: invalid hashsize for '%s'", digest_name);;
Branch: 1 branch
BranchTrueFalseTotal
if (!rv) croak("FATAL: invalid hashsize for '%s'", digest_name);; 0 1927 1927
190 1927 RETVAL = rv;
191 }
192 }
193 OUTPUT:
194 RETVAL
« inc/CryptX_Digest_KangarooTwelve.xs.inc ↑ Summary inc/CryptX_PK_ECC.xs.inc »