xs/memvault.xs

filter: Statementstmt 52.3% 367 / 701 Branchbran 51.3% 227 / 442 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 51.9% 594 / 1143 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/curve25519.xs ↑ Summary xs/secretstream.xs »
1 MODULE = Crypt::Sodium::XS PACKAGE = Crypt::Sodium::XS::MemVault
2  
3 SV * new(const char * class, SV * bytes, SV * flags = &PL_sv_undef)
4  
5 PREINIT:
6 protmem *new_pm;
7 unsigned char *out_buf;
8 STRLEN out_len;
9 2053 unsigned int new_flags = g_protmem_default_flags_memvault;
10  
11 CODE:
12 2053 SvGETMAGIC(flags);
13 2053 ▶ if (SvOK(flags))
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(flags)) 1 2052 2053
14 1 new_flags = SvUV_nomg(flags);
15  
16 2053 out_buf = (unsigned char *)SvPVbyte(bytes, out_len);
17  
18 2053 new_pm = protmem_init(aTHX_ out_len, new_flags);
19 2053 ▶ if (new_pm == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (new_pm == NULL) 0 2053 2053
20 0 croak("new: Failed to allocate protmem");
21 2053 memcpy(new_pm->pm_ptr, out_buf, out_len);
22  
23 2053 ▶ if (protmem_release(aTHX_ new_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_release(aTHX_ new_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) { 0 2053 2053
24 0 protmem_free(aTHX_ new_pm);
25 0 croak("new: Failed to release protmem RW");
26 }
27  
28 2053 RETVAL = protmem_to_sv(aTHX_ new_pm, class);
29  
30 OUTPUT:
31 RETVAL
32  
33 SV * new_from_hex(const char * class, SV * hex, SV * flags = &PL_sv_undef)
34  
35 PREINIT:
36 4 protmem *hex_pm = NULL;
37 protmem *new_pm;
38 4 char *hex_buf = NULL;
39 STRLEN hex_len;
40 4 unsigned int new_flags = g_protmem_default_flags_memvault;
41  
42 CODE:
43 4 ▶ if (sv_derived_from(hex, MEMVAULT_CLASS)) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv_derived_from(hex, MEMVAULT_CLASS)) { 2 2 4
44 2 hex_pm = protmem_get(aTHX_ hex, MEMVAULT_CLASS);
45 2 hex_buf = hex_pm->pm_ptr;
46 2 hex_len = hex_pm->size;
47 }
48 else
49 2 hex_buf = SvPVbyte(hex, hex_len);
50  
51 4 SvGETMAGIC(flags);
52 4 ▶ if (SvOK(flags))
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(flags)) 0 4 4
53 0 new_flags = SvUV_nomg(flags);
54  
55 4 new_pm = protmem_init(aTHX_ hex_len / 2, new_flags);
56 4 ▶ if (new_pm == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (new_pm == NULL) 0 4 4
57 0 croak("new_from_hex: Failed to allocate protmem");
58  
59 4 ▶ if (hex_pm) {
Branch: 1 branch
BranchTrueFalseTotal
if (hex_pm) { 2 2 4
60 2 new_pm->flags = hex_pm->flags;
61 2 ▶ if (protmem_grant(aTHX_ hex_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_grant(aTHX_ hex_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 0 2 2
62 0 protmem_free(aTHX_ new_pm);
63 0 croak("new_from_hex: Failed to grant hex protmem RO");
64 }
65 }
66  
67 4 sodium_hex2bin(new_pm->pm_ptr, new_pm->size, hex_buf, hex_len,
68 NULL, &(new_pm->size), NULL);
69  
70 4 ▶ if (hex_pm)
Branch: 1 branch
BranchTrueFalseTotal
if (hex_pm) 2 2 4
71 2 ▶ if (protmem_release(aTHX_ hex_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_release(aTHX_ hex_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 0 2 2
72 0 protmem_free(aTHX_ new_pm);
73 0 croak("new_from_hex: Failed to release hex protmem RO");
74 }
75  
76 4 ▶ if (protmem_release(aTHX_ new_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_release(aTHX_ new_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) { 0 4 4
77 0 protmem_free(aTHX_ new_pm);
78 0 croak("new_from_hex: Failed to release protmem RW");
79 }
80  
81 4 RETVAL = protmem_to_sv(aTHX_ new_pm, class);
82  
83 OUTPUT:
84 RETVAL
85  
86 SV * new_from_base64( \
87 const char * class, \
88 SV * b64, \
89 int variant = sodium_base64_VARIANT_URLSAFE_NO_PADDING, \
90 SV * flags = &PL_sv_undef \
91 )
92  
93 PREINIT:
94 4 protmem *b64_pm = NULL;
95 protmem *new_pm;
96 4 char *b64_buf = NULL;
97 STRLEN b64_len;
98 STRLEN max_len;
99 4 unsigned int new_flags = g_protmem_default_flags_memvault;
100  
101 CODE:
102 4 ▶ if (sv_derived_from(b64, MEMVAULT_CLASS)) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv_derived_from(b64, MEMVAULT_CLASS)) { 2 2 4
103 2 b64_pm = protmem_get(aTHX_ b64, MEMVAULT_CLASS);
104 2 b64_buf = (char *)b64_pm->pm_ptr;
105 2 b64_len = b64_pm->size;
106 2 new_flags = b64_pm->flags;
107 }
108 else
109 2 b64_buf = SvPVbyte(b64, b64_len);
110  
111 4 SvGETMAGIC(flags);
112 4 ▶ if (SvOK(flags))
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(flags)) 0 4 4
113 0 new_flags = SvUV_nomg(flags);
114  
115 4 switch (variant) {
116 0 case 0:
117 0 variant = sodium_base64_VARIANT_URLSAFE_NO_PADDING;
118 0 break;
119 4 case sodium_base64_VARIANT_ORIGINAL: /* fallthrough */
120 case sodium_base64_VARIANT_ORIGINAL_NO_PADDING: /* fallthrough */
121 case sodium_base64_VARIANT_URLSAFE: /* fallthrough */
122 case sodium_base64_VARIANT_URLSAFE_NO_PADDING:
123 4 break;
124 0 default:
125 0 croak("new_from_base64: Invalid base64 variant");
126 }
127  
128 /* FIXME: this should check for valid b64 */
129 4 max_len = ((b64_len + 3) & ~3) / 4 * 3;
130 4 new_pm = protmem_init(aTHX_ max_len, new_flags);
131 4 ▶ if (new_pm == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (new_pm == NULL) 0 4 4
132 0 croak("new_from_base64: Failed to allocate protmem");
133  
134 4 ▶ if (b64_pm) {
Branch: 1 branch
BranchTrueFalseTotal
if (b64_pm) { 2 2 4
135 2 ▶ if (protmem_grant(aTHX_ b64_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_grant(aTHX_ b64_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 0 2 2
136 0 protmem_free(aTHX_ new_pm);
137 0 croak("new_from_base64: Failed to grant b64 protmem RO");
138 }
139 }
140  
141 4 sodium_base642bin(new_pm->pm_ptr, new_pm->size, b64_buf, b64_len,
142 NULL, &(new_pm->size), NULL, variant);
143  
144 4 ▶ if (b64_pm) {
Branch: 1 branch
BranchTrueFalseTotal
if (b64_pm) { 2 2 4
145 2 ▶ if (protmem_release(aTHX_ b64_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_release(aTHX_ b64_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 0 2 2
146 0 protmem_free(aTHX_ new_pm);
147 0 croak("new_from_base64: Failed to release b64 protmem RO");
148 }
149 }
150  
151 4 ▶ if (protmem_release(aTHX_ new_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_release(aTHX_ new_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) { 0 4 4
152 0 protmem_free(aTHX_ new_pm);
153 0 croak("new_from_base64: Failed to release protmem RW");
154 }
155  
156 4 RETVAL = protmem_to_sv(aTHX_ new_pm, class);
157  
158 OUTPUT:
159 RETVAL
160  
161 =for notes
162  
163 this is only intended for reading secrets (keys) or data in limited block
164 sizes.
165  
166 =cut
167  
168 SV * new_from_fd(const char * class, int fd, STRLEN maxlen = 0, SV * flags = &PL_sv_undef)
169  
170 PREINIT:
171 protmem *new_pm, *resize_pm;
172 10 ssize_t bytes_read = 0;
173 10 size_t bytes_total = 0, pmrem;
174 10 STRLEN pmsize = MEMVAULT_READ_BUFSIZE;
175 10 unsigned int new_flags = g_protmem_default_flags_memvault;
176  
177 CODE:
178 10 SvGETMAGIC(flags);
179 10 ▶ if (SvOK(flags))
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(flags)) 0 10 10
180 0 new_flags = SvUV_nomg(flags);
181  
182 10 ▶ if (maxlen && maxlen < pmsize)
Branch: 2 branches
BranchTrueFalseTotal
if (maxlen && maxlen < pmsize) 3 7 10
if (maxlen && maxlen < pmsize) 3 0 3
183 3 pmsize = maxlen;
184  
185 10 new_pm = protmem_init(aTHX_ pmsize, new_flags);
186 10 ▶ if (new_pm == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (new_pm == NULL) 0 10 10
187 0 croak("new_from_fd: Failed to allocate protmem");
188  
189 /* sub-optimal for perfomance, but "safer" */
190 for (;;) {
191 25 ▶ if (bytes_total == pmsize) {
Branch: 1 branch
BranchTrueFalseTotal
if (bytes_total == pmsize) { 4 21 25
192 4 ▶ if (pmsize > SIZE_MAX / 2)
Branch: 1 branch
BranchTrueFalseTotal
if (pmsize > SIZE_MAX / 2) 0 4 4
193 0 croak("new_from_fd: Out of memory");
194 4 pmsize *= 2;
195 4 ▶ if (maxlen && maxlen < pmsize)
Branch: 2 branches
BranchTrueFalseTotal
if (maxlen && maxlen < pmsize) 0 4 4
if (maxlen && maxlen < pmsize) 0 0 0
196 0 pmsize = maxlen;
197 4 resize_pm = protmem_clone(aTHX_ new_pm, pmsize);
198 4 protmem_free(aTHX_ new_pm);
199 4 ▶ if (resize_pm == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (resize_pm == NULL) 0 4 4
200 0 croak("new_from_fd: Failed to allocate resize protmem");
201 4 new_pm = resize_pm;
202 }
203  
204 25 pmrem = pmsize - bytes_total;
205 25 bytes_read = read(fd, new_pm->pm_ptr + bytes_total, CSXS_MIN(pmrem, MEMVAULT_READ_BUFSIZE));
206 25 ▶ if (bytes_read < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (bytes_read < 0) { 0 25 25
207 0 ▶ if (errno == EINTR)
Branch: 1 branch
BranchTrueFalseTotal
if (errno == EINTR) 0 0 0
208 0 continue;
209 0 protmem_free(aTHX_ new_pm);
210 0 croak("new_from_fd: read error %d", errno);
211 }
212 25 bytes_total += bytes_read;
213 25 new_pm->size = bytes_total;
214 25 ▶ if (bytes_read == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (bytes_read == 0) 7 18 25
215 7 break;
216 18 ▶ if (maxlen && bytes_total == maxlen)
Branch: 2 branches
BranchTrueFalseTotal
if (maxlen && bytes_total == maxlen) 3 15 18
if (maxlen && bytes_total == maxlen) 3 0 3
217 3 break;
218 }
219  
220 /* naive heuristic whether it's worth re-allocating */
221 10 ▶ if (pmsize - bytes_total > MEMVAULT_READ_BUFSIZE) {
Branch: 1 branch
BranchTrueFalseTotal
if (pmsize - bytes_total > MEMVAULT_READ_BUFSIZE) { 1 9 10
222 1 resize_pm = protmem_clone(aTHX_ new_pm, bytes_total);
223 1 protmem_free(aTHX_ new_pm);
224 1 ▶ if (resize_pm == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (resize_pm == NULL) 0 1 1
225 0 croak("new_from_fd: Failed to allocate final resize protmem");
226 1 new_pm = resize_pm;
227 }
228  
229 10 ▶ if (protmem_release(aTHX_ new_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_release(aTHX_ new_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) { 0 10 10
230 0 protmem_free(aTHX_ new_pm);
231 0 croak("new_from_fd: Failed to release protmem RW");
232 }
233  
234 10 RETVAL = protmem_to_sv(aTHX_ new_pm, class);
235  
236 OUTPUT:
237 RETVAL
238  
239 =for notes
240  
241 hardcoded buffer size here of 4096. should be large enough for a reasonable
242 system without trying to deal with fpathconf
243  
244 =cut
245  
246 SV * new_from_ttyno( \
247 const char * class, \
248 SV * ttyno = &PL_sv_undef, \
249 SV * prompt = &PL_sv_undef, \
250 SV * flags = &PL_sv_undef \
251 )
252  
253 PREINIT:
254 0 protmem *new_pm = NULL;
255 char *prompt_buf;
256 int fd;
257 0 unsigned int new_flags = g_protmem_default_flags_key;
258 0 STRLEN prompt_len, total = 0;
259 ssize_t r, w;
260 struct termios tattr;
261 tcflag_t lflag_prev;
262  
263 CODE:
264 0 SvGETMAGIC(ttyno);
265 0 ▶ if (SvOK(ttyno))
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(ttyno)) 0 0 0
266 0 fd = SvIV_nomg(ttyno);
267 else
268 0 fd = 0;
269  
270 0 SvGETMAGIC(prompt);
271 0 ▶ if (SvOK(prompt))
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(prompt)) 0 0 0
272 0 prompt_buf = SvPVbyte_nomg(prompt, prompt_len);
273 else {
274 0 prompt_buf = "Password: ";
275 0 prompt_len = (sizeof "Password: ") - 1;
276 }
277  
278 0 SvGETMAGIC(flags);
279 0 ▶ if (SvOK(flags))
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(flags)) 0 0 0
280 0 new_flags = SvUV(flags);
281  
282 0 ▶ if (tcgetattr(fd, &tattr) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (tcgetattr(fd, &tattr) != 0) 0 0 0
283 0 croak("new_from_ttyno: Failed tcgetattr (not a tty?)");
284 0 lflag_prev = tattr.c_lflag;
285  
286 0 new_pm = protmem_init(aTHX_ 4096, new_flags);
287 0 ▶ if (new_pm == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (new_pm == NULL) 0 0 0
288 0 croak("new_from_ttyno: Failed to allocate protmem");
289  
290 0 ▶ while (total < prompt_len) {
Branch: 1 branch
BranchTrueFalseTotal
while (total < prompt_len) { 0 0 0
291 0 w = write(fd, prompt_buf + total, prompt_len - total);
292 0 ▶ if (w == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (w == -1) { 0 0 0
293 0 ▶ if (errno == EINTR)
Branch: 1 branch
BranchTrueFalseTotal
if (errno == EINTR) 0 0 0
294 0 continue;
295 0 protmem_free(aTHX_ new_pm);
296 0 croak("new_from_ttyno: Failed to prompt");
297 }
298 0 total += w;
299 }
300  
301 0 tattr.c_lflag |= ICANON | ISIG;
302 0 tattr.c_lflag &= ~ECHO;
303 0 ▶ if (tcsetattr(fd, TCSAFLUSH, &tattr) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (tcsetattr(fd, TCSAFLUSH, &tattr) != 0) { 0 0 0
304 0 write(fd, "\n", 1);
305 0 protmem_free(aTHX_ new_pm);
306 0 croak("new_from_ttyno: Failed tcsetattr");
307 }
308 0 tattr.c_lflag = lflag_prev; /* prepared for next tcsetattr to reset */
309  
310 for (;;) {
311 0 r = read(fd, new_pm->pm_ptr, 4096);
312 0 ▶ if (r < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (r < 0) { 0 0 0
313 0 ▶ if (errno == EINTR)
Branch: 1 branch
BranchTrueFalseTotal
if (errno == EINTR) 0 0 0
314 0 continue;
315 0 write(fd, "\n", 1);
316 0 protmem_free(aTHX_ new_pm);
317 0 tcsetattr(fd, TCSAFLUSH, &tattr);
318 0 croak("new_from_ttyno: Failed read");
319 }
320 else
321 0 break;
322 }
323 0 ▶ if (r == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (r == 0) 0 0 0
324 0 write(fd, " (WARNING: empty)", (sizeof " (WARNING: empty)" - 1));
325 0 ▶ else if (*((unsigned char *)new_pm->pm_ptr + r - 1) == '\n')
Branch: 1 branch
BranchTrueFalseTotal
else if (*((unsigned char *)new_pm->pm_ptr + r - 1) == '\n') 0 0 0
326 0 r -= 1;
327 0 ▶ else if (*((unsigned char *)new_pm->pm_ptr + r - 1) == '\r')
Branch: 1 branch
BranchTrueFalseTotal
else if (*((unsigned char *)new_pm->pm_ptr + r - 1) == '\r') 0 0 0
328 0 r -= 1;
329 0 ▶ else if (r == 4096)
Branch: 1 branch
BranchTrueFalseTotal
else if (r == 4096) 0 0 0
330 0 write(fd, " (WARNING: truncated to 4096 bytes)",
331 (sizeof " (WARNING: truncated to 4096 bytes)" - 1));
332  
333 0 new_pm->size = r;
334  
335 0 write(fd, "\n", 1);
336 0 ▶ if (tcsetattr(fd, TCSAFLUSH, &tattr) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (tcsetattr(fd, TCSAFLUSH, &tattr) != 0) { 0 0 0
337 0 protmem_free(aTHX_ new_pm);
338 0 croak("new_from_ttyno: tcsetattr failed");
339 }
340  
341 0 RETVAL = protmem_to_sv(aTHX_ new_pm, class);
342  
343 OUTPUT:
344 RETVAL
345  
346 void DESTROY(SV * self)
347  
348 PREINIT:
349 protmem *self_pm;
350  
351 CODE:
352 5274 self_pm = protmem_get(aTHX_ self, MEMVAULT_CLASS);
353 5274 protmem_free(aTHX_ self_pm);
354  
355 void _overload_bool(SV * self, ...)
356  
357 PREINIT:
358 protmem *self_pm;
359  
360 PPCODE:
361 787 self_pm = protmem_get(aTHX_ self, MEMVAULT_CLASS);
362  
363 787 ▶ if (self_pm->size)
Branch: 1 branch
BranchTrueFalseTotal
if (self_pm->size) 787 0 787
364 787 XSRETURN_YES;
365 else
366 0 XSRETURN_NO;
367  
368 SV * _overload_mult(SV * self, SV * other, SV * swapped)
369  
370 PREINIT:
371 protmem *self_pm;
372 protmem *new_pm;
373 4 unsigned int count = 0, cur = 0;
374  
375 CODE:
376 PERL_UNUSED_VAR(swapped);
377  
378 4 count = SvUV(other);
379  
380 4 self_pm = protmem_get(aTHX_ self, MEMVAULT_CLASS);
381 4 ▶ if (self_pm->size > SIZE_MAX / count)
Branch: 1 branch
BranchTrueFalseTotal
if (self_pm->size > SIZE_MAX / count) 0 4 4
382 0 croak("_overload_mult: Out of memory");
383 4 new_pm = protmem_init(aTHX_ self_pm->size * count, self_pm->flags);
384 4 ▶ if (new_pm == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (new_pm == NULL) 0 4 4
385 0 croak("_overload_mult: Failed to allocate protmem");
386  
387 4 ▶ if (protmem_grant(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_grant(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) 0 4 4
388 0 croak("_overload_mult: Failed to grant self protmem RO");
389  
390 24 ▶ while(count--)
Branch: 1 branch
BranchTrueFalseTotal
while(count--) 20 4 24
391 20 memcpy(new_pm->pm_ptr + self_pm->size * cur++, self_pm->pm_ptr, self_pm->size);
392  
393 4 ▶ if (protmem_release(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_release(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 0 4 4
394 0 protmem_free(aTHX_ new_pm);
395 0 croak("_overload_mult: Failed to release self protmem RO");
396 }
397  
398 4 ▶ if (protmem_release(aTHX_ new_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_release(aTHX_ new_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) { 0 4 4
399 0 protmem_free(aTHX_ new_pm);
400 0 croak("_overload_mult: Failed to release new protmem RW");
401 }
402  
403 4 RETVAL = protmem_to_sv(aTHX_ new_pm, MEMVAULT_CLASS);
404  
405 OUTPUT:
406 RETVAL
407  
408 void _overload_nomethod(SV * self, ...)
409  
410 PREINIT:
411 char *operator;
412  
413 PPCODE:
414 PERL_UNUSED_VAR(self);
415 0 operator = SvPVbyte_nolen(ST(3));
416 0 croak("Operation \"%s\" on MemVault is not supported", operator);
417  
418 void bitwise_and(SV * self, SV * other, ...)
419  
420 ALIAS:
421 bitwise_or = 1
422 bitwise_xor = 2
423 bitwise_and_equals = 100
424 bitwise_or_equals = 101
425 bitwise_xor_equals = 102
426  
427 PREINIT:
428 protmem *self_pm;
429 16 protmem *other_pm = NULL;
430 16 protmem *new_pm = NULL;
431 unsigned char *buf;
432 unsigned char *other_buf;
433 STRLEN other_len;
434 STRLEN i;
435 unsigned int new_flags;
436  
437 PPCODE:
438 16 self_pm = protmem_get(aTHX_ self, MEMVAULT_CLASS);
439  
440 16 ▶ if (sv_derived_from(other, MEMVAULT_CLASS)) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv_derived_from(other, MEMVAULT_CLASS)) { 1 15 16
441 1 other_pm = protmem_get(aTHX_ other, MEMVAULT_CLASS);
442 1 other_buf = other_pm->pm_ptr;
443 1 other_len = other_pm->size;
444 }
445 else
446 15 other_buf = (unsigned char *)SvPVbyte(other, other_len);
447 16 ▶ if (other_len != self_pm->size)
Branch: 1 branch
BranchTrueFalseTotal
if (other_len != self_pm->size) 0 16 16
448 /* lengths MUST be identical below */
449 0 croak("Exclusive-or operands do not have equal length");
450  
451 16 ▶ if (ix >= 100) {
Branch: 1 branch
BranchTrueFalseTotal
if (ix >= 100) { 8 8 16
452 8 ▶ if (other_pm)
Branch: 1 branch
BranchTrueFalseTotal
if (other_pm) 1 7 8
453 1 ▶ if (protmem_grant(aTHX_ other_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_grant(aTHX_ other_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 0 1 1
454 0 protmem_release(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO);
455 0 croak("xor: Failed to grant other protmem RO");
456 }
457 8 ▶ if (protmem_grant(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_grant(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) { 0 8 8
458 0 ▶ if (other_pm)
Branch: 1 branch
BranchTrueFalseTotal
if (other_pm) 0 0 0
459 0 protmem_release(aTHX_ other_pm, PROTMEM_FLAG_MPROTECT_RO);
460 0 croak("xor: Failed to grant self protmem RW");
461 }
462 8 buf = self_pm->pm_ptr;
463 /* lengths identical, assured above */
464 32 ▶ for (i = 0; i < other_len; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < other_len; i++) 24 8 32
465 24 switch(ix) {
466 6 case 101:
467 6 buf[i] |= other_buf[i];
468 6 break;
469 12 case 102:
470 12 buf[i] ^= other_buf[i];
471 12 break;
472 6 default: /* 100 */
473 6 buf[i] &= other_buf[i];
474 }
475 8 ▶ if (protmem_release(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_release(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) { 0 8 8
476 0 ▶ if (other_pm)
Branch: 1 branch
BranchTrueFalseTotal
if (other_pm) 0 0 0
477 0 protmem_release(aTHX_ other_pm, PROTMEM_FLAG_MPROTECT_RO);
478 0 croak("xor: Failed to release self protmem RW");
479 }
480 }
481 else {
482 8 new_flags = self_pm->flags;
483 8 ▶ if (other_pm)
Branch: 1 branch
BranchTrueFalseTotal
if (other_pm) 0 8 8
484 0 new_flags &= other_pm->flags;
485 8 new_pm = protmem_init(aTHX_ self_pm->size, new_flags);
486 8 ▶ if (new_pm == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (new_pm == NULL) 0 8 8
487 0 croak("xor: Failed to allocate protmem");
488  
489 8 ▶ if (protmem_grant(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_grant(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) 0 8 8
490 0 croak("xor: Failed to grant self protmem RO");
491  
492 8 ▶ if (other_pm)
Branch: 1 branch
BranchTrueFalseTotal
if (other_pm) 0 8 8
493 0 ▶ if (protmem_grant(aTHX_ other_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_grant(aTHX_ other_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 0 0 0
494 0 protmem_release(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO);
495 0 croak("xor: Failed to grant other protmem RO");
496 }
497  
498 8 buf = memcpy(new_pm->pm_ptr, self_pm->pm_ptr, self_pm->size);
499 32 ▶ for (i = 0; i < other_len; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < other_len; i++) 24 8 32
500 24 switch(ix) {
501 9 case 1:
502 9 buf[i] |= other_buf[i];
503 9 break;
504 6 case 2:
505 6 buf[i] ^= other_buf[i];
506 6 break;
507 9 default:
508 9 buf[i] &= other_buf[i];
509 }
510  
511 8 ▶ if (protmem_release(aTHX_ new_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_release(aTHX_ new_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) { 0 8 8
512 0 ▶ if (other_pm)
Branch: 1 branch
BranchTrueFalseTotal
if (other_pm) 0 0 0
513 0 protmem_release(aTHX_ other_pm, PROTMEM_FLAG_MPROTECT_RO);
514 0 protmem_release(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO);
515 0 protmem_free(aTHX_ new_pm);
516 0 croak("xor: Failed to release new protmem RW");
517 }
518 8 ▶ if (protmem_release(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_release(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) { 0 8 8
519 0 ▶ if (other_pm)
Branch: 1 branch
BranchTrueFalseTotal
if (other_pm) 0 0 0
520 0 protmem_release(aTHX_ other_pm, PROTMEM_FLAG_MPROTECT_RO);
521 0 protmem_free(aTHX_ new_pm);
522 0 croak("xor: Failed to release self protmem RW");
523 }
524  
525 8 ▶ mXPUSHs(protmem_to_sv(aTHX_ new_pm, MEMVAULT_CLASS));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(protmem_to_sv(aTHX_ new_pm, MEMVAULT_CLASS)); 0 8 8
526 }
527  
528 16 ▶ if (other_pm)
Branch: 1 branch
BranchTrueFalseTotal
if (other_pm) 1 15 16
529 1 ▶ if (protmem_release(aTHX_ other_pm, PROTMEM_FLAG_MPROTECT_RO) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_release(aTHX_ other_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) 0 1 1
530 0 croak("xor: Failed to release other protmem RO");
531  
532 16 XSRETURN(1);
533  
534 SV * clone(SV * self)
535  
536 CODE:
537 2010 RETVAL = protmem_clone_sv(aTHX_ self, MEMVAULT_CLASS);
538  
539 OUTPUT:
540 RETVAL
541  
542 void compare(SV * self, SV * other, STRLEN size = 0)
543  
544 ALIAS:
545 _overload_eq = 1
546 _overload_ne = 2
547 memcmp = 3
548  
549 PREINIT:
550 8365 protmem *self_pm = NULL, *other_pm = NULL;
551 unsigned char *self_buf, *other_buf;
552 STRLEN self_size, other_size;
553 8365 ▶ int ret = 0;
Branch: 1 branch
BranchTrueFalseTotal
int ret = 0; 123 8242 8365
554  
555 PPCODE:
556 /* since used for overloads, args could be swapped. could require either self
557 * or other to be a memvault */
558 8365 ▶ if (sv_derived_from(self, MEMVAULT_CLASS)) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv_derived_from(self, MEMVAULT_CLASS)) { 8356 9 8365
559 8356 self_pm = protmem_get(aTHX_ self, MEMVAULT_CLASS);
560 8356 ▶ if (ix == 0 && !(self_pm->flags & PROTMEM_FLAG_LOCK_UNLOCKED))
Branch: 2 branches
BranchTrueFalseTotal
if (ix == 0 && !(self_pm->flags & PROTMEM_FLAG_LOCK_UNLOCKED)) 4003 4353 8356
if (ix == 0 && !(self_pm->flags & PROTMEM_FLAG_LOCK_UNLOCKED)) 3 4000 4003
561 3 croak("compare: Unlock MemVault object before comparison");
562 8353 self_buf = self_pm->pm_ptr;
563 8353 self_size = self_pm->size;
564 }
565 else
566 9 self_buf = (unsigned char *)SvPVbyte(self, self_size);
567  
568 8362 ▶ if (sv_derived_from(other, MEMVAULT_CLASS)) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv_derived_from(other, MEMVAULT_CLASS)) { 4128 4234 8362
569 4128 other_pm = protmem_get(aTHX_ other, MEMVAULT_CLASS);
570 4128 ▶ if (ix == 0 && !(other_pm->flags & PROTMEM_FLAG_LOCK_UNLOCKED))
Branch: 2 branches
BranchTrueFalseTotal
if (ix == 0 && !(other_pm->flags & PROTMEM_FLAG_LOCK_UNLOCKED)) 2008 2120 4128
if (ix == 0 && !(other_pm->flags & PROTMEM_FLAG_LOCK_UNLOCKED)) 3 2005 2008
571 3 croak("compare: Unlock MemVault object before comparison");
572 4125 other_buf = other_pm->pm_ptr;
573 4125 other_size = other_pm->size;
574 }
575 else
576 4234 other_buf = (unsigned char *)SvPVbyte(other, other_size);
577  
578 8359 ▶ if (self_size != other_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (self_size != other_size) { 12 8347 8359
579 12 ▶ switch(ix) {
Branch: 1 branch
BranchTrueFalseTotal
switch(ix) { 0 12 12
580 0 case 1: /* fallthrough */
581 case 2:
582 0 croak("compare: %s %s",
583 "Variables of unequal size cannot be automatically compared.",
584 "Please use memcmp() with the size argument provided.");
585 break;
586 12 default:
587 12 ▶ if (size == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (size == 0) { 2 10 12
588 2 croak("compare: %s %s",
589 "Variables of unequal size cannot be automatically compared.",
590 "Please provide the size argument.");
591 }
592 else {
593 10 ▶ if (size > self_size)
Branch: 1 branch
BranchTrueFalseTotal
if (size > self_size) 2 8 10
594 2 croak("compare: The argument (left) is shorter then requested size");
595 8 ▶ else if (size > other_size)
Branch: 1 branch
BranchTrueFalseTotal
else if (size > other_size) 2 6 8
596 2 croak("compare: The argument (right) is shorter then requested size");
597 }
598 }
599 }
600  
601 8353 ▶ if (self_pm)
Branch: 1 branch
BranchTrueFalseTotal
if (self_pm) 8345 8 8353
602 8345 ▶ if (protmem_grant(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_grant(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) 0 8345 8345
603 0 croak("compare: Failed to grant self protmem RO");
604  
605 8353 ▶ if (other_pm) {
Branch: 1 branch
BranchTrueFalseTotal
if (other_pm) { 4125 4228 8353
606 4125 ▶ if (protmem_grant(aTHX_ other_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_grant(aTHX_ other_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 0 4125 4125
607 0 ▶ if (self_pm)
Branch: 1 branch
BranchTrueFalseTotal
if (self_pm) 0 0 0
608 0 protmem_release(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO);
609 0 croak("compare: Failed to grant other protmem RO");
610 }
611 }
612  
613 8353 ▶ if (ix != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (ix != 0) 4350 4003 8353
614 4350 ret = sodium_memcmp(self_buf, other_buf, self_size);
615 else
616 4003 ret = sodium_compare(self_buf, other_buf, self_size);
617  
618 8353 ▶ if (other_pm && protmem_release(aTHX_ other_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (other_pm && protmem_release(aTHX_ other_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 4125 4228 8353
if (other_pm && protmem_release(aTHX_ other_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 0 4125 4125
619 0 ▶ if (self_pm)
Branch: 1 branch
BranchTrueFalseTotal
if (self_pm) 0 0 0
620 0 protmem_release(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO);
621 0 croak("compare: Failed to release other protmem RO");
622 }
623 8353 ▶ if (self_pm && protmem_release(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO) != 0)
Branch: 2 branches
BranchTrueFalseTotal
if (self_pm && protmem_release(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) 8345 8 8353
if (self_pm && protmem_release(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) 0 8345 8345
624 0 croak("compare: Failed to release self protmem RO");
625  
626 8353 ▶ if (ix == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ix == 0) { 4003 4350 8353
627 4003 XSRETURN_IV(ret);
628 }
629 4350 ▶ else if (ix == 2) {
Branch: 1 branch
BranchTrueFalseTotal
else if (ix == 2) { 32 4318 4350
630 32 ▶ if (ret == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (ret == 0) 4 28 32
631 4 XSRETURN_NO;
632 28 XSRETURN_YES;
633 }
634 else {
635 4318 ▶ if (ret == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (ret == 0) 2310 2008 4318
636 2310 XSRETURN_YES;
637 2008 XSRETURN_NO;
638 }
639  
640 =for TODO
641  
642 consider a flags argument. that cannot co-exist with overloading as it is now.
643  
644 =cut
645  
646 void concat(SV * self, SV * other, SV * swapped = &PL_sv_undef)
647  
648 ALIAS:
649 concat_inplace = 1
650  
651 PREINIT:
652 protmem *self_pm;
653 16 protmem *other_pm = NULL;
654 protmem *new_pm;
655 unsigned char *buf;
656 16 ▶ MAGIC *mg, *mg_found=NULL;
Branch: 1 branch
BranchTrueFalseTotal
MAGIC *mg, *mg_found=NULL; 4 12 16
657 STRLEN buf_len;
658 unsigned int new_flags;
659  
660 PPCODE:
661 16 ▶ if (sv_derived_from(other, MEMVAULT_CLASS)) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv_derived_from(other, MEMVAULT_CLASS)) { 6 10 16
662 6 other_pm = protmem_get(aTHX_ other, MEMVAULT_CLASS);
663 6 buf = other_pm->pm_ptr;
664 6 buf_len = other_pm->size;
665 }
666 else
667 10 buf = (unsigned char *)SvPVbyte(other, buf_len);
668 /* should probably zero buf afterwards */
669  
670 16 self_pm = protmem_get(aTHX_ self, MEMVAULT_CLASS);
671  
672 16 ▶ if (self_pm->size > SIZE_MAX - buf_len)
Branch: 1 branch
BranchTrueFalseTotal
if (self_pm->size > SIZE_MAX - buf_len) 0 16 16
673 0 croak("concat: Out of memory");
674  
675 16 new_flags = self_pm->flags;
676 16 ▶ if (other_pm)
Branch: 1 branch
BranchTrueFalseTotal
if (other_pm) 6 10 16
677 6 new_flags &= other_pm->flags;
678 16 new_pm = protmem_init(aTHX_ self_pm->size + buf_len, new_flags);
679 16 ▶ if (new_pm == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (new_pm == NULL) 0 16 16
680 0 croak("concat: Failed to allocate protmem");
681  
682 16 ▶ if (other_pm && protmem_grant(aTHX_ other_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (other_pm && protmem_grant(aTHX_ other_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 6 10 16
if (other_pm && protmem_grant(aTHX_ other_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 0 6 6
683 0 protmem_free(aTHX_ new_pm);
684 0 croak("concat: Failed to grant other protmem RO");
685 }
686  
687 16 ▶ if (ix == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ix == 0) { 12 4 16
688 12 ▶ if (protmem_grant(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_grant(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 0 12 12
689 0 ▶ if (other_pm)
Branch: 1 branch
BranchTrueFalseTotal
if (other_pm) 0 0 0
690 0 protmem_release(aTHX_ other_pm, PROTMEM_FLAG_MPROTECT_RO);
691 0 protmem_free(aTHX_ new_pm);
692 0 croak("concat: Failed to grant self protmem RO");
693 }
694  
695 12 ▶ if (SvTRUE(swapped))
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(swapped)) 2 10 12
696 2 memcpy(memcpy(new_pm->pm_ptr, buf, buf_len) + buf_len,
697 2 self_pm->pm_ptr, self_pm->size);
698 else
699 10 memcpy(memcpy(new_pm->pm_ptr, self_pm->pm_ptr, self_pm->size)
700 10 + self_pm->size, buf, buf_len);
701  
702 12 ▶ if (protmem_release(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_release(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 0 12 12
703 0 ▶ if (other_pm)
Branch: 1 branch
BranchTrueFalseTotal
if (other_pm) 0 0 0
704 0 protmem_release(aTHX_ other_pm, PROTMEM_FLAG_MPROTECT_RO);
705 0 protmem_free(aTHX_ new_pm);
706 0 croak("concat: Failed to release self protmem RO");
707 }
708  
709 12 ▶ if (protmem_release(aTHX_ new_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_release(aTHX_ new_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) { 0 12 12
710 0 ▶ if (other_pm)
Branch: 1 branch
BranchTrueFalseTotal
if (other_pm) 0 0 0
711 0 protmem_release(aTHX_ other_pm, PROTMEM_FLAG_MPROTECT_RO);
712 0 protmem_free(aTHX_ new_pm);
713 0 croak("concat: Failed to release new protmem RW");
714 }
715  
716 12 ▶ mXPUSHs(protmem_to_sv(aTHX_ new_pm, MEMVAULT_CLASS));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(protmem_to_sv(aTHX_ new_pm, MEMVAULT_CLASS)); 0 12 12
717 }
718 else {
719 4 ▶ if (protmem_grant(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_grant(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) { 0 4 4
720 0 ▶ if (other_pm)
Branch: 1 branch
BranchTrueFalseTotal
if (other_pm) 0 0 0
721 0 protmem_release(aTHX_ other_pm, PROTMEM_FLAG_MPROTECT_RO);
722 0 croak("concat: Failed to grant self protmem RW");
723 }
724  
725 4 memcpy((unsigned char *)memcpy(new_pm->pm_ptr, self_pm->pm_ptr, self_pm->size)
726 4 + self_pm->size, buf, buf_len);
727  
728 4 ▶ if (protmem_release(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RW != 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_release(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RW != 0)) { 0 4 4
729 0 ▶ if (other_pm)
Branch: 1 branch
BranchTrueFalseTotal
if (other_pm) 0 0 0
730 0 protmem_release(aTHX_ other_pm, PROTMEM_FLAG_MPROTECT_RO);
731 0 protmem_free(aTHX_ new_pm);
732 0 croak("concat: Failed to release self protmem RW");
733 }
734  
735 4 ▶ if (protmem_release(aTHX_ new_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_release(aTHX_ new_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) { 0 4 4
736 0 ▶ if (other_pm)
Branch: 1 branch
BranchTrueFalseTotal
if (other_pm) 0 0 0
737 0 protmem_release(aTHX_ other_pm, PROTMEM_FLAG_MPROTECT_RO);
738 0 protmem_free(aTHX_ new_pm);
739 0 croak("concat: Failed to release new protmem RW");
740 }
741  
742 4 ▶ for (mg = SvMAGIC(SvRV(self)); mg; mg = mg->mg_moremagic)
Branch: 1 branch
BranchTrueFalseTotal
for (mg = SvMAGIC(SvRV(self)); mg; mg = mg->mg_moremagic) 4 0 4
743 4 ▶ if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &vtbl_protmem) {
Branch: 2 branches
BranchTrueFalseTotal
if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &vtbl_protmem) { 4 0 4
if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &vtbl_protmem) { 4 0 4
744 4 mg_found = mg;
745 4 break;
746 }
747  
748 4 ▶ if (mg_found != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (mg_found != NULL) { 4 0 4
749 4 protmem_free(aTHX_ (protmem *)mg_found->mg_ptr);
750 4 mg_found->mg_ptr = (char *)new_pm;
751 4 new_pm = NULL;
752 }
753 else {
754 0 ▶ if (other_pm)
Branch: 1 branch
BranchTrueFalseTotal
if (other_pm) 0 0 0
755 0 protmem_release(aTHX_ other_pm, PROTMEM_FLAG_MPROTECT_RO);
756 0 protmem_free(aTHX_ new_pm);
757 0 croak("concat: Failed to find protmem magic");
758 }
759 }
760  
761 16 ▶ if (other_pm && protmem_release(aTHX_ other_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (other_pm && protmem_release(aTHX_ other_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 6 10 16
if (other_pm && protmem_release(aTHX_ other_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 0 6 6
762 0 protmem_free(aTHX_ new_pm);
763 0 croak("concat: Failed to release other protmem RO");
764 }
765  
766 16 XSRETURN(1);
767  
768 =for notes
769  
770 FIXME: if self is longer than LONG_MAX or abs(LONG_MIN) this is broke.
771  
772 =cut
773  
774 SV * extract( \
775 SV * self, \
776 long offset = 0, \
777 SV * length = &PL_sv_undef, \
778 SV * flags = &PL_sv_undef \
779 )
780  
781 PREINIT:
782 protmem *self_pm;
783 protmem *new_pm;
784 unsigned int new_flags;
785 STRLEN self_len;
786 long new_len;
787  
788 CODE:
789 30 self_pm = protmem_get(aTHX_ self, MEMVAULT_CLASS);
790 30 self_len = self_pm->size;
791  
792 30 ▶ if (offset && (offset > (long)self_len - 1
Branch: 2 branches
BranchTrueFalseTotal
if (offset && (offset > (long)self_len - 1 22 8 30
if (offset && (offset > (long)self_len - 1 20 2 22
793 20 ▶ || offset < -(long)self_len))
Branch: 1 branch
BranchTrueFalseTotal
|| offset < -(long)self_len)) 2 18 20
794 4 croak("extract: Invalid offset %ld", offset);
795 26 ▶ if (offset < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (offset < 0) 10 16 26
796 10 offset = self_len + offset;
797  
798 26 SvGETMAGIC(length);
799 26 ▶ if (SvOK(length)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(length)) { 22 4 26
800 22 new_len = SvIV_nomg(length);
801 22 ▶ if (new_len > (long)(self_len - offset))
Branch: 1 branch
BranchTrueFalseTotal
if (new_len > (long)(self_len - offset)) 6 16 22
802 6 new_len = self_len - offset;
803 16 ▶ else if (new_len < -(long)(self_len - offset))
Branch: 1 branch
BranchTrueFalseTotal
else if (new_len < -(long)(self_len - offset)) 6 10 16
804 6 new_len = 0;
805 10 ▶ else if (new_len < 0)
Branch: 1 branch
BranchTrueFalseTotal
else if (new_len < 0) 6 4 10
806 6 new_len = self_len - offset + new_len;
807 }
808 else
809 4 new_len = self_len - offset;
810  
811 26 SvGETMAGIC(flags);
812 26 ▶ if (SvOK(flags))
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(flags)) 0 26 26
813 0 new_flags = SvIV_nomg(flags);
814 else
815 26 new_flags = self_pm->flags;
816  
817 26 new_pm = protmem_init(aTHX_ new_len, new_flags);
818 26 ▶ if (new_pm == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (new_pm == NULL) 0 26 26
819 0 croak("extract: Failed to allocate protmem");
820  
821 26 ▶ if (protmem_grant(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_grant(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 0 26 26
822 0 protmem_free(aTHX_ new_pm);
823 0 croak("extract: Failed to grant self protmem RO");
824 }
825  
826 26 memcpy(new_pm->pm_ptr, (unsigned char *)self_pm->pm_ptr + offset, new_len);
827  
828 26 ▶ if (protmem_release(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_release(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 0 26 26
829 0 protmem_free(aTHX_ new_pm);
830 0 croak("extract: Failed to release self protmem RO");
831 }
832  
833 26 ▶ if (protmem_release(aTHX_ new_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_release(aTHX_ new_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) { 0 26 26
834 0 protmem_free(aTHX_ new_pm);
835 0 croak("extract: Failed to release new protmem RW");
836 }
837  
838 26 RETVAL = protmem_to_sv(aTHX_ new_pm, MEMVAULT_CLASS);
839  
840 OUTPUT:
841 RETVAL
842  
843 unsigned int flags(SV * self, SV * flags = &PL_sv_undef)
844  
845 PREINIT:
846 protmem *self_pm;
847  
848 CODE:
849 14 self_pm = protmem_get(aTHX_ self, MEMVAULT_CLASS);
850  
851 14 SvGETMAGIC(flags);
852 14 ▶ if (SvOK(flags)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(flags)) { 0 14 14
853 unsigned int new_flags;
854 unsigned int old_flags;
855 0 new_flags = SvUV_nomg(flags);
856 0 old_flags = self_pm->flags;
857 /* may want to allow changing malloc? likely not useful. */
858 0 if ((old_flags & PROTMEM_FLAG_MALLOC_MASK)
859 0 ▶ != (new_flags & PROTMEM_FLAG_MALLOC_MASK))
Branch: 1 branch
BranchTrueFalseTotal
!= (new_flags & PROTMEM_FLAG_MALLOC_MASK)) 0 0 0
860 0 croak("flags: cannot change malloc method on an existing MemVault");
861 0 ▶ if (self_pm->flags != new_flags) {
Branch: 1 branch
BranchTrueFalseTotal
if (self_pm->flags != new_flags) { 0 0 0
862 0 self_pm->flags = new_flags;
863 0 ▶ if (protmem_release(aTHX_ self_pm, old_flags) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_release(aTHX_ self_pm, old_flags) != 0) 0 0 0
864 0 croak("flags: Failed to release self protmem from old flags");;
865 }
866 }
867  
868 14 ▶ RETVAL = self_pm->flags;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self_pm->flags; 11 3 14
869  
870 OUTPUT:
871 RETVAL
872  
873 SV * from_base64( \
874 SV * self, \
875 int variant = sodium_base64_VARIANT_URLSAFE_NO_PADDING, \
876 SV * flags = &PL_sv_undef \
877 )
878  
879 PREINIT:
880 protmem *self_pm;
881 protmem *new_pm;
882 0 char *self_buf = NULL;
883 STRLEN self_len;
884 STRLEN max_len;
885 0 ▶ unsigned int new_flags = g_protmem_default_flags_memvault;
Branch: 1 branch
BranchTrueFalseTotal
unsigned int new_flags = g_protmem_default_flags_memvault; 0 0 0
886  
887 CODE:
888 0 self_pm = protmem_get(aTHX_ self, MEMVAULT_CLASS);
889 0 self_buf = (char *)self_pm->pm_ptr;
890 0 self_len = self_pm->size;
891 0 new_flags = self_pm->flags;
892  
893 0 SvGETMAGIC(flags);
894 0 ▶ if (SvOK(flags))
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(flags)) 0 0 0
895 0 new_flags = SvUV_nomg(flags);
896  
897 0 switch (variant) {
898 0 case 0:
899 0 variant = sodium_base64_VARIANT_URLSAFE_NO_PADDING;
900 0 break;
901 0 case sodium_base64_VARIANT_ORIGINAL: /* fallthrough */
902 case sodium_base64_VARIANT_ORIGINAL_NO_PADDING: /* fallthrough */
903 case sodium_base64_VARIANT_URLSAFE: /* fallthrough */
904 case sodium_base64_VARIANT_URLSAFE_NO_PADDING:
905 0 break;
906 0 default:
907 0 croak("from_base64: Invalid base64 variant");
908 }
909  
910 /* FIXME: this should check for valid base64 */
911 0 max_len = ((self_len + 3) & ~3) / 4 * 3;
912 0 new_pm = protmem_init(aTHX_ max_len, new_flags);
913 0 ▶ if (new_pm == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (new_pm == NULL) 0 0 0
914 0 croak("from_base64: Failed to allocate protmem");
915  
916 0 ▶ if (protmem_grant(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_grant(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 0 0 0
917 0 protmem_free(aTHX_ new_pm);
918 0 croak("from_base64: Failed to grant self protmem RO");
919 }
920  
921 0 sodium_base642bin(new_pm->pm_ptr, new_pm->size, self_buf, self_len,
922 NULL, &(new_pm->size), NULL, variant);
923  
924 0 ▶ if (protmem_release(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_release(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 0 0 0
925 0 protmem_free(aTHX_ new_pm);
926 0 croak("from_base64: Failed to release self protmem RO");
927 }
928  
929 0 ▶ if (protmem_release(aTHX_ new_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_release(aTHX_ new_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) { 0 0 0
930 0 protmem_free(aTHX_ new_pm);
931 0 croak("from_base64: Failed to release new protmem RW");
932 }
933  
934 0 RETVAL = protmem_to_sv(aTHX_ new_pm, MEMVAULT_CLASS);
935  
936 OUTPUT:
937 RETVAL
938  
939 SV * from_hex(SV * self, SV * flags = &PL_sv_undef)
940  
941 PREINIT:
942 protmem *self_pm;
943 protmem *new_pm;
944 0 char *self_buf = NULL;
945 STRLEN self_len;
946 0 ▶ unsigned int new_flags = g_protmem_default_flags_memvault;
Branch: 1 branch
BranchTrueFalseTotal
unsigned int new_flags = g_protmem_default_flags_memvault; 0 0 0
947  
948 CODE:
949 0 self_pm = protmem_get(aTHX_ self, MEMVAULT_CLASS);
950 0 self_buf = (char *)self_pm->pm_ptr;
951 0 self_len = self_pm->size;
952 0 new_flags = self_pm->flags;
953  
954 0 SvGETMAGIC(flags);
955 0 ▶ if (SvOK(flags))
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(flags)) 0 0 0
956 0 new_flags = SvUV_nomg(flags);
957  
958 0 new_pm = protmem_init(aTHX_ self_len / 2, new_flags);
959 0 ▶ if (new_pm == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (new_pm == NULL) 0 0 0
960 0 croak("from_hex: Failed to allocate protmem");
961  
962 0 ▶ if (protmem_grant(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_grant(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 0 0 0
963 0 protmem_free(aTHX_ new_pm);
964 0 croak("from_hex: Failed to grant self protmem RO");
965 }
966  
967 0 sodium_hex2bin(new_pm->pm_ptr, new_pm->size, self_buf, self_len,
968 NULL, NULL, NULL);
969  
970 0 ▶ if (protmem_release(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_release(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 0 0 0
971 0 protmem_free(aTHX_ new_pm);
972 0 croak("from_hex: Failed to release self protmem RO");
973 }
974  
975 0 ▶ if (protmem_release(aTHX_ new_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_release(aTHX_ new_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) { 0 0 0
976 0 protmem_free(aTHX_ new_pm);
977 0 croak("from_hex: Failed to release new protmem RW");
978 }
979  
980 0 RETVAL = protmem_to_sv(aTHX_ new_pm, MEMVAULT_CLASS);
981  
982 OUTPUT:
983 RETVAL
984  
985 void increment(SV * self)
986  
987 PREINIT:
988 protmem *self_pm;
989  
990 PPCODE:
991 0 self_pm = protmem_get(aTHX_ self, MEMVAULT_CLASS);
992 0 ▶ if (protmem_grant(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RW) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_grant(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) 0 0 0
993 0 croak("increment: Failed to grant self protmem RW");
994  
995 0 sodium_increment(self_pm->pm_ptr, self_pm->size);
996  
997 0 ▶ if (protmem_release(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RW) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_release(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) 0 0 0
998 0 croak("increment: Failed to release self protmem RW");
999  
1000 0 XSRETURN(1);
1001  
1002 SV * index(SV * self, SV * str, STRLEN offset = 0)
1003  
1004 PREINIT:
1005 protmem *self_pm;
1006 char *str_buf;
1007 unsigned char *self_start, *self_p, *self_stop;
1008 STRLEN str_len;
1009  
1010 CODE:
1011 16 self_pm = protmem_get(aTHX_ self, MEMVAULT_CLASS);
1012  
1013 16 ▶ if (!(self_pm->flags & PROTMEM_FLAG_LOCK_UNLOCKED))
Branch: 1 branch
BranchTrueFalseTotal
if (!(self_pm->flags & PROTMEM_FLAG_LOCK_UNLOCKED)) 2 14 16
1014 2 croak("index: Unlock MemVault object before using index");
1015  
1016 14 ▶ if (offset > self_pm->size - 1)
Branch: 1 branch
BranchTrueFalseTotal
if (offset > self_pm->size - 1) 2 12 14
1017 2 XSRETURN_IV(-1);
1018 12 str_buf = SvPVbyte(str, str_len);
1019 12 ▶ if (str_len < 1)
Branch: 1 branch
BranchTrueFalseTotal
if (str_len < 1) 0 12 12
1020 0 XSRETURN_IV(0);
1021 12 self_start = self_pm->pm_ptr;
1022 12 self_p = self_start + offset;
1023 12 self_stop = self_start + self_pm->size - str_len; /* + 1 - 1 */
1024 12 ▶ if (self_p > self_stop)
Branch: 1 branch
BranchTrueFalseTotal
if (self_p > self_stop) 0 12 12
1025 0 XSRETURN_IV(-1);
1026  
1027 12 ▶ if (protmem_grant(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_grant(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) 0 12 12
1028 0 croak("index: Failed to grant self protmem RO");
1029  
1030 /* naive implementation, good nuff for "unsafe" */
1031 12 RETVAL = NULL;
1032 101 ▶ while (self_p <= self_stop) {
Branch: 1 branch
BranchTrueFalseTotal
while (self_p <= self_stop) { 99 2 101
1033 99 ▶ if (*self_p == str_buf[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (*self_p == str_buf[0]) { 11 88 99
1034 11 ▶ if (str_len == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (str_len == 1) { 6 5 11
1035 6 RETVAL=newSVuv(self_p - self_start);
1036 6 break;
1037 }
1038 else {
1039 5 ▶ if (memcmp(self_p, str_buf, str_len) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(self_p, str_buf, str_len) == 0) { 4 1 5
1040 4 RETVAL = newSVuv(self_p - self_start);
1041 4 break;
1042 }
1043 }
1044 }
1045 89 ++self_p;
1046 }
1047 12 ▶ if (RETVAL == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (RETVAL == NULL) 2 10 12
1048 2 RETVAL = newSViv(-1);
1049  
1050 12 ▶ if (protmem_release(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_release(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) 0 12 12
1051 0 croak("index: Failed to release self protmem RO");
1052  
1053 OUTPUT:
1054 RETVAL
1055  
1056 void is_locked(SV * self)
1057  
1058 PREINIT:
1059 protmem *self_pm;
1060  
1061 PPCODE:
1062 38 self_pm = protmem_get(aTHX_ self, MEMVAULT_CLASS);
1063  
1064 38 ▶ if (self_pm->flags & PROTMEM_FLAG_LOCK_UNLOCKED)
Branch: 1 branch
BranchTrueFalseTotal
if (self_pm->flags & PROTMEM_FLAG_LOCK_UNLOCKED) 8 30 38
1065 8 XSRETURN_NO;
1066  
1067 30 XSRETURN_YES;
1068  
1069 void is_zero(SV * self)
1070  
1071 PREINIT:
1072 protmem *self_pm;
1073 int ret;
1074  
1075 PPCODE:
1076 0 self_pm = protmem_get(aTHX_ self, MEMVAULT_CLASS);
1077 0 ▶ if (protmem_grant(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_grant(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) 0 0 0
1078 0 croak("is_zero: Failed to grant self protmem RO");
1079  
1080 0 ret = sodium_is_zero(self_pm->pm_ptr, self_pm->size);
1081  
1082 0 ▶ if (protmem_release(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_release(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) 0 0 0
1083 0 croak("is_zero: Failed to release self protmem RO");
1084  
1085 0 ▶ if (ret)
Branch: 1 branch
BranchTrueFalseTotal
if (ret) 0 0 0
1086 0 XSRETURN_YES;
1087  
1088 0 XSRETURN_NO;
1089  
1090 SV * length(SV * self)
1091  
1092 ALIAS:
1093 size = 1
1094  
1095 PREINIT:
1096 protmem *self_pm;
1097  
1098 CODE:
1099 PERL_UNUSED_VAR(ix);
1100 610 self_pm = protmem_get(aTHX_ self, MEMVAULT_CLASS);
1101 610 RETVAL = newSVuv((UV)self_pm->size);
1102  
1103 OUTPUT:
1104 RETVAL
1105  
1106 void lock(SV * self)
1107  
1108 PREINIT:
1109 protmem *self_pm;
1110  
1111 PPCODE:
1112 5 self_pm = protmem_get(aTHX_ self, MEMVAULT_CLASS);
1113 5 self_pm->flags &= ~PROTMEM_FLAG_LOCK_UNLOCKED;
1114 5 XSRETURN(1);
1115  
1116 SV * pad(SV * self, STRLEN blocksize)
1117  
1118 PREINIT:
1119 protmem *self_pm, *realloc_pm;
1120 STRLEN buf_len, pad_len, padded_len;
1121  
1122 CODE:
1123 6 ▶ if (blocksize <= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (blocksize <= 0) 0 6 6
1124 0 croak("pad: Invalid blocksize <= 0");
1125  
1126 6 self_pm = protmem_get(aTHX_ self, MEMVAULT_CLASS);
1127 6 buf_len = self_pm->size;
1128  
1129 6 pad_len = blocksize - 1;
1130 6 ▶ if ((blocksize & (blocksize - 1)) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((blocksize & (blocksize - 1)) == 0) 2 4 6
1131 2 pad_len -= buf_len & (blocksize - 1);
1132 else
1133 4 pad_len -= buf_len % blocksize;
1134 6 pad_len += 1; /* for 0x80 */
1135  
1136 6 ▶ if (pad_len > SIZE_MAX - buf_len)
Branch: 1 branch
BranchTrueFalseTotal
if (pad_len > SIZE_MAX - buf_len) 0 6 6
1137 0 croak("pad: Out of memory");
1138 6 padded_len = buf_len + pad_len;
1139  
1140 6 ▶ if (protmem_grant(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_grant(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) 0 6 6
1141 0 croak("pad: Failed to grant self protmem RO");
1142  
1143 6 realloc_pm = protmem_clone(aTHX_ self_pm, padded_len);
1144 6 ▶ if (realloc_pm == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (realloc_pm == NULL) { 0 6 6
1145 0 protmem_release(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO);
1146 0 croak("pad: Failed to allocate protmem");
1147 }
1148  
1149 6 ▶ if (protmem_release(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_release(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 0 6 6
1150 0 protmem_free(aTHX_ realloc_pm);
1151 0 croak("pad: Failed to release self protmem RO");
1152 }
1153  
1154 6 ▶ if (sodium_pad(&padded_len, realloc_pm->pm_ptr,
Branch: 1 branch
BranchTrueFalseTotal
if (sodium_pad(&padded_len, realloc_pm->pm_ptr, 0 6 6
1155 buf_len, blocksize, padded_len) != 0) {
1156 /* should be impossible */
1157 0 protmem_free(aTHX_ realloc_pm);
1158 0 croak("BUG: pad: sodium_pad returned error");
1159 }
1160  
1161 6 ▶ if (protmem_release(aTHX_ realloc_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_release(aTHX_ realloc_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) { 0 6 6
1162 0 protmem_free(aTHX_ realloc_pm);
1163 0 croak("pad: Failed to release protmem RW");
1164 }
1165  
1166 6 RETVAL = protmem_to_sv(aTHX_ realloc_pm, MEMVAULT_CLASS);
1167  
1168 OUTPUT:
1169 RETVAL
1170  
1171 SV * to_base64( \
1172 SV * self, \
1173 int variant = sodium_base64_VARIANT_URLSAFE_NO_PADDING, \
1174 SV * flags = &PL_sv_undef \
1175 )
1176  
1177 PREINIT:
1178 protmem *self_pm;
1179 protmem *new_pm;
1180 STRLEN new_len;
1181 unsigned int new_flags;
1182  
1183 CODE:
1184 4 self_pm = protmem_get(aTHX_ self, MEMVAULT_CLASS);
1185 4 new_flags = self_pm->flags;
1186  
1187 4 SvGETMAGIC(flags);
1188 4 ▶ if (SvOK(flags))
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(flags)) 0 4 4
1189 0 new_flags = SvUV_nomg(flags);
1190 4 new_len = sodium_base64_encoded_len(self_pm->size, variant);
1191 4 new_pm = protmem_init(aTHX_ new_len, new_flags);
1192 4 ▶ if (new_pm == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (new_pm == NULL) 0 4 4
1193 0 croak("to_base64: Failed to allocate protmem");
1194  
1195 4 ▶ if (protmem_grant(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_grant(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 0 4 4
1196 0 protmem_free(aTHX_ new_pm);
1197 0 croak("to_base64: Failed to grant self protmem RO");
1198 }
1199  
1200 4 sodium_bin2base64((char *)new_pm->pm_ptr, new_pm->size,
1201 4 self_pm->pm_ptr, self_pm->size, variant);
1202 /* ditch the appended nul */
1203 4 new_pm->size = new_len - 1;
1204  
1205 4 ▶ if (protmem_release(aTHX_ new_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_release(aTHX_ new_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) { 0 4 4
1206 0 protmem_release(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO);
1207 0 protmem_free(aTHX_ new_pm);
1208 0 croak("to_base64: Failed to release new protmem RW");
1209 }
1210  
1211 4 ▶ if (protmem_release(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_release(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 0 4 4
1212 0 protmem_free(aTHX_ new_pm);
1213 0 croak("to_base64: Failed to release self protmem RO");
1214 }
1215  
1216 4 RETVAL = protmem_to_sv(aTHX_ new_pm, MEMVAULT_CLASS);
1217  
1218 OUTPUT:
1219 RETVAL
1220  
1221 SV * to_bytes(SV * self, ...)
1222  
1223 PREINIT:
1224 protmem *self_pm;
1225  
1226 CODE:
1227 32 self_pm = protmem_get(aTHX_ self, MEMVAULT_CLASS);
1228  
1229 32 ▶ if (!(self_pm->flags & PROTMEM_FLAG_LOCK_UNLOCKED))
Branch: 1 branch
BranchTrueFalseTotal
if (!(self_pm->flags & PROTMEM_FLAG_LOCK_UNLOCKED)) 4 28 32
1230 4 croak("to_bytes: Unlock MemVault object before stringifying");
1231  
1232 28 ▶ if (protmem_grant(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_grant(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) 0 28 28
1233 0 croak("to_bytes: Failed to grant protmem RO");
1234  
1235 28 RETVAL = newSVpvn((char *)self_pm->pm_ptr, self_pm->size);
1236  
1237 28 ▶ if (protmem_release(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_release(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) 0 28 28
1238 0 croak("to_bytes: Failed to release protmem RO");
1239  
1240 OUTPUT:
1241 RETVAL
1242  
1243 SV * to_hex(SV * self, SV * flags = &PL_sv_undef)
1244  
1245 PREINIT:
1246 protmem *self_pm;
1247 protmem *new_pm;
1248 STRLEN new_len;
1249 unsigned int new_flags;
1250  
1251 CODE:
1252 145 self_pm = protmem_get(aTHX_ self, MEMVAULT_CLASS);
1253 145 new_flags = self_pm->flags;
1254  
1255 145 SvGETMAGIC(flags);
1256 145 ▶ if (SvOK(flags))
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(flags)) 0 145 145
1257 0 new_flags = SvUV_nomg(flags);
1258 145 ▶ if (self_pm->size > SIZE_MAX / 2 - 1)
Branch: 1 branch
BranchTrueFalseTotal
if (self_pm->size > SIZE_MAX / 2 - 1) 0 145 145
1259 0 croak("to_hex: Out of memory");
1260 145 new_len = self_pm->size * 2 + 1;
1261 145 new_pm = protmem_init(aTHX_ new_len, new_flags);
1262 145 ▶ if (new_pm == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (new_pm == NULL) 0 145 145
1263 0 croak("to_hex: Failed to allocate protmem");
1264  
1265 145 ▶ if (protmem_grant(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_grant(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 0 145 145
1266 0 protmem_free(aTHX_ new_pm);
1267 0 croak("to_hex: Failed to grant self protmem RO");
1268 }
1269  
1270 145 sodium_bin2hex(new_pm->pm_ptr, new_len, self_pm->pm_ptr, self_pm->size);
1271 /* ditch the appended nul */
1272 145 new_pm->size = new_len - 1;
1273  
1274 145 ▶ if (protmem_release(aTHX_ new_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_release(aTHX_ new_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) { 0 145 145
1275 0 protmem_release(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO);
1276 0 protmem_free(aTHX_ new_pm);
1277 0 croak("to_hex: Failed to release new protmem RW");
1278 }
1279  
1280 145 ▶ if (protmem_release(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_release(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 0 145 145
1281 0 protmem_free(aTHX_ new_pm);
1282 0 croak("to_hex: Failed to release self protmem RO");
1283 }
1284  
1285 145 RETVAL = protmem_to_sv(aTHX_ new_pm, MEMVAULT_CLASS);
1286  
1287 OUTPUT:
1288 RETVAL
1289  
1290 SV * to_fd(SV * self, int fd)
1291  
1292 PREINIT:
1293 protmem *self_pm;
1294 ssize_t w;
1295 0 size_t t = 0;
1296 0 size_t r = 0;
1297  
1298 CODE:
1299 0 self_pm = protmem_get(aTHX_ self, MEMVAULT_CLASS);
1300 0 ▶ if (protmem_grant(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_grant(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO) < 0) 0 0 0
1301 0 croak("to_fd: Failed to grant self protmem RO");
1302  
1303 /* w: current iteration written bytes */
1304 /* t: total bytes written */
1305 /* r: remaining bytes to write */
1306 0 r = self_pm->size;
1307 for (;;) {
1308 0 w = write(fd, self_pm->pm_ptr + t,
1309 r > MEMVAULT_WRITE_BUFSIZE ? MEMVAULT_WRITE_BUFSIZE : r);
1310 0 ▶ if (w < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (w < 0) { 0 0 0
1311 0 ▶ if (errno == EINTR)
Branch: 1 branch
BranchTrueFalseTotal
if (errno == EINTR) 0 0 0
1312 0 continue;
1313 0 protmem_release(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO);
1314 0 croak("to_fd: (%d): write error", fd);
1315 }
1316 0 t += w;
1317 0 r -= w;
1318 0 ▶ if (r == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (r == 0) 0 0 0
1319 0 break;
1320 }
1321  
1322 0 ▶ if (protmem_release(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_release(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO) < 0) 0 0 0
1323 0 croak("to_fd: Failed to release self protmem RO");
1324  
1325 0 RETVAL = newSVuv(t);
1326  
1327 OUTPUT:
1328 RETVAL
1329  
1330 void unlock(SV * self)
1331  
1332 PREINIT:
1333 protmem *self_pm;
1334  
1335 PPCODE:
1336 2306 self_pm = protmem_get(aTHX_ self, MEMVAULT_CLASS);
1337 2306 self_pm->flags |= PROTMEM_FLAG_LOCK_UNLOCKED;
1338 2306 XSRETURN(1);
1339  
1340 SV * unpad(SV * self, STRLEN blocksize)
1341  
1342 PREINIT:
1343 protmem *self_pm, *realloc_pm;
1344 STRLEN buf_len, unpadded_len;
1345  
1346 CODE:
1347 5 ▶ if (blocksize <= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (blocksize <= 0) 0 5 5
1348 0 croak("unpad: Invalid blocksize <= 0");
1349  
1350 5 self_pm = protmem_get(aTHX_ self, MEMVAULT_CLASS);
1351 5 buf_len = self_pm->size;
1352 5 ▶ if (buf_len < blocksize)
Branch: 1 branch
BranchTrueFalseTotal
if (buf_len < blocksize) 0 5 5
1353 0 croak("unpad: Buffer is shorter than blocksize");
1354  
1355 5 ▶ if (protmem_grant(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_grant(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) 0 5 5
1356 0 croak("unpad: Failed to grant self protmem RO");
1357  
1358 5 ▶ if (sodium_unpad(&unpadded_len, self_pm->pm_ptr, self_pm->size, blocksize) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (sodium_unpad(&unpadded_len, self_pm->pm_ptr, self_pm->size, blocksize) != 0) { 0 5 5
1359 0 protmem_release(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO);
1360 0 croak("unpad: Invalid padded buffer");
1361 }
1362  
1363 5 realloc_pm = protmem_clone(aTHX_ self_pm, unpadded_len);
1364 5 ▶ if (realloc_pm == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (realloc_pm == NULL) { 0 5 5
1365 0 protmem_release(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO);
1366 0 croak("sodium_pad: Failed to allocate protmem");
1367 }
1368  
1369 5 ▶ if (protmem_release(aTHX_ realloc_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_release(aTHX_ realloc_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) { 0 5 5
1370 0 protmem_free(aTHX_ realloc_pm);
1371 0 protmem_release(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO);
1372 0 croak("sodium_pad: Failed to release protmem RW");
1373 }
1374 5 ▶ if (protmem_release(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_release(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 0 5 5
1375 0 protmem_free(aTHX_ realloc_pm);
1376 0 croak("sodium_pad: Failed to release self protmem RO");
1377 }
1378  
1379 5 RETVAL = protmem_to_sv(aTHX_ realloc_pm, MEMVAULT_CLASS);
1380  
1381 OUTPUT:
1382 RETVAL
1383  
1384 void memzero(SV * self)
1385  
1386 PREINIT:
1387 protmem *self_pm;
1388  
1389 PPCODE:
1390 0 self_pm = protmem_get(aTHX_ self, MEMVAULT_CLASS);
1391 0 ▶ if (protmem_grant(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RW) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_grant(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RW) < 0) 0 0 0
1392 0 croak("memzero: Failed to grant self protmem RW");
1393 0 sodium_memzero(self_pm->pm_ptr, self_pm->size);
1394 0 ▶ if (protmem_release(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RW) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_release(aTHX_ self_pm, PROTMEM_FLAG_MPROTECT_RW) < 0) 0 0 0
1395 0 croak("memzero: Failed to release self protmem RW");
1396  
1397 =for FIXME
1398  
1399 separate methods for xor (modify in place) from the overload (new
1400 memvault). currently no explicit method for returning new memvault, only the
1401 overload.
1402  
1403 =cut
1404  
« xs/curve25519.xs ↑ Summary xs/secretstream.xs »