File Coverage

HexConverter.xs
Criterion Covered Total %
statement 35 39 89.7
branch 13 18 72.2
condition n/a
subroutine n/a
pod n/a
total 48 57 84.2


line stmt bran cond sub pod time code
1             #include "EXTERN.h"
2             #include "perl.h"
3             #include "XSUB.h"
4              
5             #include "src/hexsimd.h"
6              
7             MODULE = Data::HexConverter PACKAGE = Data::HexConverter
8              
9             PROTOTYPES: ENABLE
10              
11             SV *
12             hex_to_binary(SV *hex_ref)
13             PREINIT:
14             SV *hex_sv;
15             STRLEN in_len;
16             const char *in;
17             SV *out_sv;
18             char *out_buf;
19             ptrdiff_t written;
20             CODE:
21             /* must be a ref to a scalar */
22 9 50         if (!SvROK(hex_ref)) {
23 0           croak("hex_to_binary() expects a reference to a scalar");
24             }
25              
26 9           hex_sv = SvRV(hex_ref);
27 9 50         in = SvOK(hex_sv) ? SvPVbyte(hex_sv, in_len) : "";
28              
29             /* empty input -> empty output */
30 9 100         if (in_len == 0) {
31 2           RETVAL = newSVpvn("", 0);
32             } else {
33             /* must be even length */
34 7 100         if ((in_len & 1) != 0) {
35 2           croak("hex_to_binary() input length (%" IVdf ") is not even", (IV)in_len);
36             }
37              
38             /* allocate N/2 bytes */
39 5           out_sv = newSV(in_len / 2);
40 5           SvPOK_on(out_sv);
41 5           SvCUR_set(out_sv, in_len / 2);
42 5           out_buf = SvPVX(out_sv);
43              
44 5           written = hex_to_bytes(in,
45             (size_t)in_len,
46             (uint8_t *)out_buf,
47             /* strict */ 1);
48              
49 5 100         if (written < 0) {
50 1           SvREFCNT_dec(out_sv);
51 1           croak("hex_to_binary() invalid hex input");
52             }
53              
54             /* in case C returned less */
55 4           SvCUR_set(out_sv, (STRLEN)written);
56 4           out_buf[written] = '\0';
57              
58 4           RETVAL = out_sv;
59             }
60             OUTPUT:
61             RETVAL
62              
63             SV *
64             binary_to_hex(SV *bin_ref)
65             PREINIT:
66             SV *bin_sv;
67             STRLEN in_len;
68             const unsigned char *in;
69             SV *out_sv;
70             char *out_buf;
71             ptrdiff_t written;
72             CODE:
73 7 50         if (!SvROK(bin_ref)) {
74 0           croak("binary_to_hex() expects a reference to a scalar");
75             }
76              
77 7           bin_sv = SvRV(bin_ref);
78 7           in = SvOK(bin_sv) ? (const unsigned char *)SvPVbyte(bin_sv, in_len)
79 7 50         : (const unsigned char *)"";
80              
81             /* empty input -> empty hex */
82 7 100         if (in_len == 0) {
83 2           RETVAL = newSVpvn("", 0);
84             } else {
85 5           out_sv = newSV(in_len * 2);
86 5           SvPOK_on(out_sv);
87 5           SvCUR_set(out_sv, in_len * 2);
88 5           out_buf = SvPVX(out_sv);
89              
90 5           written = bytes_to_hex(in, (size_t)in_len, out_buf);
91 5 50         if (written < 0) {
92 0           SvREFCNT_dec(out_sv);
93 0           croak("binary_to_hex() conversion failed");
94             }
95              
96 5           SvCUR_set(out_sv, (STRLEN)written);
97 5           out_buf[written] = '\0';
98              
99 5           RETVAL = out_sv;
100             }
101             OUTPUT:
102             RETVAL
103              
104             const char *
105             hex_to_binary_impl()
106             CODE:
107 2           RETVAL = hexsimd_hex2bin_impl_name();
108             OUTPUT:
109             RETVAL
110              
111             const char *
112             binary_to_hex_impl()
113             CODE:
114 2           RETVAL = hexsimd_bin2hex_impl_name();
115             OUTPUT:
116             RETVAL