File Coverage

secret_buffer_span.c
Criterion Covered Total %
statement 23 37 62.1
branch 1 16 6.2
condition n/a
subroutine n/a
pod n/a
total 24 53 45.2


line stmt bran cond sub pod time code
1             /*
2             * SecretBuffer Span Magic
3             */
4              
5 797           void *secret_buffer_span_auto_ctor(pTHX_ SV *owner) {
6 797           secret_buffer_span *span= NULL;
7 797           Newxz(span, 1, secret_buffer_span);
8 797           return span;
9             }
10 5612           secret_buffer_span* secret_buffer_span_from_magic(SV *objref, int flags) {
11             dTHX;
12 5612           return (secret_buffer_span*) secret_buffer_X_from_magic(aTHX_
13             objref, flags,
14             &secret_buffer_span_magic_vtbl, "secret_buffer_span",
15             secret_buffer_span_auto_ctor);
16             }
17              
18 797           int secret_buffer_span_magic_free(pTHX_ SV *sv, MAGIC *mg) {
19 797 50         if (mg->mg_ptr) {
20 797           Safefree(mg->mg_ptr);
21 797           mg->mg_ptr = NULL;
22             }
23 797           return 0;
24             }
25              
26             #ifdef USE_ITHREADS
27             int secret_buffer_span_magic_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *param) {
28             secret_buffer_span *clone= NULL;
29             PERL_UNUSED_VAR(param);
30             if (mg->mg_ptr) {
31             Newx(clone, 1, secret_buffer_span);
32             memcpy(clone, mg->mg_ptr, sizeof(secret_buffer_span));
33             mg->mg_ptr = (char *)clone;
34             }
35             return 0;
36             }
37             #endif
38              
39 797           static SV *new_mortal_span_obj(pTHX_ secret_buffer *buf, UV pos, UV lim, int encoding) {
40 797           HV *hv= newHV();
41 797           SV *ref= sv_2mortal(newRV_noinc((SV*) hv));
42 797           sv_bless(ref, gv_stashpv("Crypt::SecretBuffer::Span", GV_ADD));
43              
44 797           secret_buffer_span *span= secret_buffer_span_from_magic(ref, SECRET_BUFFER_MAGIC_AUTOCREATE);
45 797           hv_stores(hv, "buf", newRV_inc(buf->wrapper));
46 797           span->pos= pos;
47 797           span->lim= lim;
48 797           span->encoding= encoding;
49 797           return ref;
50             }
51              
52             /* Public API: Return a mortal ref to a new SecretBuffer::Span */
53 9           extern SV *secret_buffer_span_new_obj(secret_buffer *buf, size_t pos, size_t lim, int encoding) {
54             dTHX;
55 9           return new_mortal_span_obj(aTHX_ buf, pos, lim, encoding);
56             }
57              
58             /* Public API: Return a mortal ref to a new SecretBuffer::Span using fields of a parse */
59 0           extern SV *secret_buffer_span_new_obj_from_parse(secret_buffer_parse *src) {
60             dTHX;
61             U8 *sbuf_start, *sbuf_lim;
62 0 0         if (!src->sbuf)
63 0           croak("parse struct lacks secret_buffer reference");
64             /* sanity check on pos and lim since this is about to subtract pointers */
65 0           sbuf_start= (U8*) src->sbuf->data;
66 0           sbuf_lim= sbuf_start + src->sbuf->len;
67 0 0         if (src->pos < sbuf_start || src->pos > sbuf_lim)
    0          
68 0           croak("parse->pos out of bounds");
69 0 0         if (src->pos_bit)
70 0           croak("parse->pos is not on a byte boundary");
71 0 0         if (src->lim < src->pos || src->lim > sbuf_lim)
    0          
72 0           croak("parse->lim out of bounds");
73 0 0         if (src->lim_bit)
74 0           croak("parse->lim is not on a byte boundary");
75 0           return new_mortal_span_obj(aTHX_ src->sbuf, src->pos - sbuf_start, src->lim - sbuf_start, src->encoding);
76             }