Hash.xs

filter: Statementstmt 86.2% 350 / 406 Branchbran 71.3% 227 / 318 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 79.7% 577 / 724 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/arch/File/Raw/Hash/Install/Files.pm ↑ Summary hashx.c »
1 /*
2 * Hash.xs - File::Raw plugin bindings for File::Raw::Hash.
3 *
4 * file_slurp($p, plugin => 'hash', algo => 'sha256', into => \my $d);
5 * file_slurp($p, plugin => 'hash', algos => [qw(sha256 md5)],
6 * into => \my %digests);
7 */
8  
9 #define PERL_NO_GET_CONTEXT
10 #include "EXTERN.h"
11 #include "perl.h"
12 #include "XSUB.h"
13  
14 /* Under PERL_IMPLICIT_SYS (every Strawberry perl) XSUB.h rewrites the
15 * plain names malloc/calloc/realloc/free into PerlMem_* macros that
16 * dereference my_perl. The frh_abi_* entry points below are plain C
17 * with no interpreter to hand, so they fail to compile there; and the
18 * runner they allocate is fed to hashx.c, which allocates with the
19 * libc names, so mixing the two pools would free to the wrong one.
20 * Undef the four so this file matches hashx.c. No-op on Unix. */
21 #undef malloc
22 #undef calloc
23 #undef realloc
24 #undef free
25  
26 #include "file_plugin.h"
27 #include "hashx.h"
28  
29 #include <string.h>
30 #include <stdlib.h>
31  
32 /* ============================================================
33 * Resolved options for a single plugin call.
34 * ============================================================ */
35  
36 #define MAX_ALGOS 8
37  
38 typedef struct {
39 hash_algo_id_t ids[MAX_ALGOS];
40 int n_ids;
41 int multi; /* 0 = `algo`, 1 = `algos` */
42 hash_format_t format;
43 SV *into; /* the user's ref SV; not refcount-bumped here */
44 SV *hmac_key_sv; /* NULL if not set */
45 int xxh64_seed_set;
46 uint64_t xxh64_seed;
47 } hash_opts_t;
48  
49 /* Phase context for `into` validation. RECORD wants an arrayref
50 * because we push one entry per record; READ/WRITE/STREAM want a
51 * scalar (single algo) or hash (multi-algo) ref. */
52 typedef enum {
53 PHASE_ONE_SHOT = 0,
54 PHASE_RECORD = 1
55 } decode_phase_t;
56  
57 static int
58 4135 str_eq(const char *a, STRLEN alen, const char *b)
59 {
60 4135 ▶ return alen == strlen(b) && memcmp(a, b, alen) == 0;
Branch: 2 branches
BranchTrueFalseTotal
return alen == strlen(b) && memcmp(a, b, alen) == 0; 1606 2529 4135
return alen == strlen(b) && memcmp(a, b, alen) == 0; 966 640 1606
61 }
62  
63 static const char *VALID_OPT_KEYS[] = {
64 "algo", "algos", "into", "format",
65 "hmac_key", "xxh64_seed",
66 "plugin", /* always present from the dispatcher */
67 NULL
68 };
69  
70 static int
71 564 known_opt(const char *key, STRLEN klen)
72 {
73 const char *const *p;
74 2152 ▶ for (p = VALID_OPT_KEYS; *p; p++) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = VALID_OPT_KEYS; *p; p++) { 2151 1 2152
75 2151 ▶ if (str_eq(key, klen, *p)) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (str_eq(key, klen, *p)) return 1; 563 1588 2151
76 }
77 1 return 0;
78 }
79  
80 /* Parse one algo name SV, croak on unknown. */
81 static hash_algo_id_t
82 177 parse_algo_sv(pTHX_ SV *sv)
83 {
84 STRLEN alen;
85 const char *ap;
86 const hash_algo_info_t *info;
87  
88 177 ▶ if (!SvOK(sv))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(sv)) 0 177 177
89 0 croak("File::Raw::Hash: algo name must not be undef");
90 177 ▶ if (SvROK(sv))
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(sv)) 0 177 177
91 0 croak("File::Raw::Hash: algo name must be a string, not a reference");
92 177 ap = SvPV(sv, alen);
93 177 info = hash_algo_lookup(ap, alen);
94 177 ▶ if (!info)
Branch: 1 branch
BranchTrueFalseTotal
if (!info) 1 176 177
95 1 croak("File::Raw::Hash: unknown algo '%.*s' "
96 "(known: sha256 sha512 sha1 md5 crc32 xxh64 blake3)",
97 (int)alen, ap);
98 176 return info->id;
99 }
100  
101 /* Decode the per-call options HV into a hash_opts_t. Croaks on any
102 * validation failure. Caller passes a zeroed struct. */
103 static void
104 160 decode_opts(pTHX_ HV *opts_hv, hash_opts_t *opts, decode_phase_t phase)
105 {
106 HE *he;
107 160 SV *algo_sv = NULL;
108 160 SV *algos_sv = NULL;
109 160 SV *fmt_sv = NULL;
110 160 SV *seed_sv = NULL;
111  
112 160 ▶ if (!opts_hv) croak("File::Raw::Hash: missing options");
Branch: 1 branch
BranchTrueFalseTotal
if (!opts_hv) croak("File::Raw::Hash: missing options"); 0 160 160
113  
114 /* First pass: validate keys + grab the ones we care about. */
115 160 hv_iterinit(opts_hv);
116 723 ▶ while ((he = hv_iternext(opts_hv))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(opts_hv))) { 564 159 723
117 I32 klen_i;
118 564 const char *key = hv_iterkey(he, &klen_i);
119 564 STRLEN klen = (STRLEN)klen_i;
120 564 SV *val = hv_iterval(opts_hv, he);
121  
122 564 ▶ if (!known_opt(key, klen)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!known_opt(key, klen)) { 1 563 564
123 1 croak("File::Raw::Hash: unknown option '%.*s' (known: algo, "
124 "algos, into, format, hmac_key, xxh64_seed)",
125 (int)klen, key);
126 }
127 563 ▶ if (str_eq(key, klen, "algo")) algo_sv = val;
Branch: 1 branch
BranchTrueFalseTotal
if (str_eq(key, klen, "algo")) algo_sv = val; 145 418 563
128 418 ▶ else if (str_eq(key, klen, "algos")) algos_sv = val;
Branch: 1 branch
BranchTrueFalseTotal
else if (str_eq(key, klen, "algos")) algos_sv = val; 16 402 418
129 402 ▶ else if (str_eq(key, klen, "into")) opts->into = val;
Branch: 1 branch
BranchTrueFalseTotal
else if (str_eq(key, klen, "into")) opts->into = val; 159 243 402
130 243 ▶ else if (str_eq(key, klen, "format")) fmt_sv = val;
Branch: 1 branch
BranchTrueFalseTotal
else if (str_eq(key, klen, "format")) fmt_sv = val; 52 191 243
131 191 ▶ else if (str_eq(key, klen, "hmac_key")) opts->hmac_key_sv = val;
Branch: 1 branch
BranchTrueFalseTotal
else if (str_eq(key, klen, "hmac_key")) opts->hmac_key_sv = val; 24 167 191
132 167 ▶ else if (str_eq(key, klen, "xxh64_seed")) seed_sv = val;
Branch: 1 branch
BranchTrueFalseTotal
else if (str_eq(key, klen, "xxh64_seed")) seed_sv = val; 7 160 167
133 /* "plugin" key: silently ignored. */
134 }
135  
136 /* Mutual exclusion. */
137 159 ▶ if (algo_sv && SvOK(algo_sv) && algos_sv && SvOK(algos_sv))
Branch: 4 branches
BranchTrueFalseTotal
if (algo_sv && SvOK(algo_sv) && algos_sv && SvOK(algos_sv)) 144 15 159
if (algo_sv && SvOK(algo_sv) && algos_sv && SvOK(algos_sv)) 144 0 144
if (algo_sv && SvOK(algo_sv) && algos_sv && SvOK(algos_sv)) 1 143 144
if (algo_sv && SvOK(algo_sv) && algos_sv && SvOK(algos_sv)) 1 0 1
138 1 croak("File::Raw::Hash: 'algo' and 'algos' are mutually exclusive");
139  
140 /* Resolve algorithm list. */
141 158 ▶ if (algos_sv && SvOK(algos_sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (algos_sv && SvOK(algos_sv)) { 15 143 158
if (algos_sv && SvOK(algos_sv)) { 15 0 15
142 AV *av;
143 SSize_t n, i;
144 15 ▶ if (!SvROK(algos_sv) || SvTYPE(SvRV(algos_sv)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(algos_sv) || SvTYPE(SvRV(algos_sv)) != SVt_PVAV) 14 1 15
if (!SvROK(algos_sv) || SvTYPE(SvRV(algos_sv)) != SVt_PVAV) 0 14 14
145 1 croak("File::Raw::Hash: 'algos' must be an arrayref");
146 14 av = (AV *)SvRV(algos_sv);
147 14 n = av_len(av) + 1;
148 14 ▶ if (n < 1)
Branch: 1 branch
BranchTrueFalseTotal
if (n < 1) 1 13 14
149 1 croak("File::Raw::Hash: 'algos' arrayref is empty");
150 13 ▶ if (n > MAX_ALGOS)
Branch: 1 branch
BranchTrueFalseTotal
if (n > MAX_ALGOS) 0 13 13
151 0 croak("File::Raw::Hash: too many algos (%ld); max %d",
152 (long)n, MAX_ALGOS);
153 47 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 34 13 47
154 34 SV **slot = av_fetch(av, i, 0);
155 34 ▶ if (!slot || !*slot)
Branch: 2 branches
BranchTrueFalseTotal
if (!slot || !*slot) 34 0 34
if (!slot || !*slot) 0 34 34
156 0 croak("File::Raw::Hash: undef entry in 'algos' at index %ld",
157 (long)i);
158 34 opts->ids[i] = parse_algo_sv(aTHX_ *slot);
159 }
160 13 opts->n_ids = (int)n;
161 13 opts->multi = 1;
162 143 ▶ } else if (algo_sv && SvOK(algo_sv)) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (algo_sv && SvOK(algo_sv)) { 143 0 143
} else if (algo_sv && SvOK(algo_sv)) { 143 0 143
163 143 opts->ids[0] = parse_algo_sv(aTHX_ algo_sv);
164 142 opts->n_ids = 1;
165 142 opts->multi = 0;
166 } else {
167 /* Default: single sha256. */
168 0 opts->ids[0] = HA_SHA256;
169 0 opts->n_ids = 1;
170 0 opts->multi = 0;
171 }
172  
173 /* Resolve format. */
174 206 ▶ if (fmt_sv && SvOK(fmt_sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (fmt_sv && SvOK(fmt_sv)) { 52 103 155
if (fmt_sv && SvOK(fmt_sv)) { 52 0 52
175 STRLEN flen;
176 const char *fp;
177 52 ▶ if (SvROK(fmt_sv))
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(fmt_sv)) 0 52 52
178 0 croak("File::Raw::Hash: 'format' must be a string");
179 52 fp = SvPV(fmt_sv, flen);
180 52 ▶ if (hash_format_parse(fp, flen, &opts->format) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (hash_format_parse(fp, flen, &opts->format) != 0) 1 51 52
181 1 croak("File::Raw::Hash: unknown format '%.*s' "
182 "(known: hex, HEX, base64, base64url, raw)",
183 (int)flen, fp);
184 } else {
185 103 opts->format = HF_HEX;
186 }
187  
188 /* Resolve xxh64_seed. */
189 154 ▶ if (seed_sv && SvOK(seed_sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (seed_sv && SvOK(seed_sv)) { 7 147 154
if (seed_sv && SvOK(seed_sv)) { 7 0 7
190 7 ▶ if (SvROK(seed_sv))
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(seed_sv)) 1 6 7
191 1 croak("File::Raw::Hash: 'xxh64_seed' must be an integer");
192 6 opts->xxh64_seed = (uint64_t)SvUV(seed_sv);
193 6 opts->xxh64_seed_set = 1;
194 }
195  
196 /* HMAC key validation. The key itself can be any byte string
197 * including binary / empty. Only the value's *type* is checked
198 * here; per-algo HMAC-able-ness is checked when set_hmac runs. */
199 171 ▶ if (opts->hmac_key_sv && SvOK(opts->hmac_key_sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (opts->hmac_key_sv && SvOK(opts->hmac_key_sv)) { 24 129 153
if (opts->hmac_key_sv && SvOK(opts->hmac_key_sv)) { 24 0 24
200 int j;
201 24 ▶ if (SvROK(opts->hmac_key_sv))
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(opts->hmac_key_sv)) 1 23 24
202 1 croak("File::Raw::Hash: 'hmac_key' must be a byte string, "
203 "not a reference");
204 44 ▶ for (j = 0; j < opts->n_ids; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < opts->n_ids; j++) { 26 18 44
205 26 const hash_algo_info_t *info = hash_algo_by_id(opts->ids[j]);
206 26 ▶ if (!info->hmac_able)
Branch: 1 branch
BranchTrueFalseTotal
if (!info->hmac_able) 5 21 26
207 5 croak("File::Raw::Hash: HMAC is not defined for algo "
208 "'%s' (HMAC-able: sha256, sha512, sha1, md5)",
209 info->name);
210 }
211 } else {
212 129 opts->hmac_key_sv = NULL;
213 }
214  
215 /* Validate `into`. Required; shape depends on phase. */
216 147 ▶ if (!opts->into || !SvOK(opts->into))
Branch: 2 branches
BranchTrueFalseTotal
if (!opts->into || !SvOK(opts->into)) 146 1 147
if (!opts->into || !SvOK(opts->into)) 1 145 146
217 2 croak("File::Raw::Hash: 'into' is required");
218 145 ▶ if (!SvROK(opts->into))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(opts->into)) 1 144 145
219 1 croak("File::Raw::Hash: 'into' must be a reference");
220  
221 144 ▶ if (phase == PHASE_RECORD) {
Branch: 1 branch
BranchTrueFalseTotal
if (phase == PHASE_RECORD) { 10 134 144
222 10 ▶ if (SvTYPE(SvRV(opts->into)) != SVt_PVAV)
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(SvRV(opts->into)) != SVt_PVAV) 2 8 10
223 2 croak("File::Raw::Hash: in record phase, 'into' must be an "
224 "ARRAY ref (one entry pushed per record)");
225 8 return;
226 }
227  
228 134 ▶ if (opts->multi) {
Branch: 1 branch
BranchTrueFalseTotal
if (opts->multi) { 9 125 134
229 9 ▶ if (SvTYPE(SvRV(opts->into)) != SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(SvRV(opts->into)) != SVt_PVHV) 1 8 9
230 1 croak("File::Raw::Hash: 'into' must be a hash ref when "
231 "'algos' is used");
232 } else {
233 125 SV *referent = SvRV(opts->into);
234 125 svtype t = SvTYPE(referent);
235 125 ▶ if (t == SVt_PVAV || t == SVt_PVHV || t == SVt_PVCV
Branch: 3 branches
BranchTrueFalseTotal
if (t == SVt_PVAV || t == SVt_PVHV || t == SVt_PVCV 125 0 125
if (t == SVt_PVAV || t == SVt_PVHV || t == SVt_PVCV 124 1 125
if (t == SVt_PVAV || t == SVt_PVHV || t == SVt_PVCV 124 0 124
236 124 ▶ || t == SVt_PVGV || t == SVt_PVFM || t == SVt_PVIO)
Branch: 3 branches
BranchTrueFalseTotal
|| t == SVt_PVGV || t == SVt_PVFM || t == SVt_PVIO) 124 0 124
|| t == SVt_PVGV || t == SVt_PVFM || t == SVt_PVIO) 124 0 124
|| t == SVt_PVGV || t == SVt_PVFM || t == SVt_PVIO) 0 124 124
237 1 croak("File::Raw::Hash: 'into' must be a SCALAR ref for "
238 "single-algo (got %s ref)", sv_reftype(referent, 0));
239 }
240 }
241  
242 /* Helper: build, run and finalise a runner over the given bytes,
243 * applying HMAC if a key is present. Returns 0 on success, croaks on
244 * setup error. results[*] is owned by the runner and lives until
245 * hash_runner_free. */
246 static void
247 133 run_full(pTHX_ const hash_opts_t *opts,
248 const char *data, size_t len,
249 hash_runner_t *runner, const hash_result_t **out_results)
250 {
251 133 ▶ if (hash_runner_init(runner, opts->ids, opts->n_ids, opts->format,
Branch: 1 branch
BranchTrueFalseTotal
if (hash_runner_init(runner, opts->ids, opts->n_ids, opts->format, 0 133 133
252 133 opts->xxh64_seed) != 0)
253 0 croak("File::Raw::Hash: out of memory initialising runner");
254  
255 133 ▶ if (opts->hmac_key_sv) {
Branch: 1 branch
BranchTrueFalseTotal
if (opts->hmac_key_sv) { 17 116 133
256 STRLEN klen;
257 const unsigned char *kp =
258 17 (const unsigned char *)SvPV(opts->hmac_key_sv, klen);
259 17 ▶ if (hash_runner_set_hmac(runner, kp, (size_t)klen) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (hash_runner_set_hmac(runner, kp, (size_t)klen) != 0) { 0 17 17
260 0 hash_runner_free(runner);
261 /* Only reachable if a non-HMAC-able algo slipped past
262 * decode_opts; defensive. */
263 0 croak("File::Raw::Hash: HMAC mode rejected for the requested "
264 "algorithm set");
265 }
266 }
267  
268 133 ▶ if (data && len) hash_runner_update(runner, data, len);
Branch: 2 branches
BranchTrueFalseTotal
if (data && len) hash_runner_update(runner, data, len); 133 0 133
if (data && len) hash_runner_update(runner, data, len); 97 36 133
269  
270 133 ▶ if (hash_runner_finish(runner, out_results) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (hash_runner_finish(runner, out_results) != 0) { 0 133 133
271 0 hash_runner_free(runner);
272 0 croak("File::Raw::Hash: out of memory finalising runner");
273 }
274 133 }
275  
276 /* Write digest results into the user's `into` target (READ/WRITE/STREAM
277 * shape). For RECORD phase use append_record_results. */
278 static void
279 132 emit_results(pTHX_ const hash_opts_t *opts, const hash_result_t *results)
280 {
281 int i;
282 132 ▶ if (opts->multi) {
Branch: 1 branch
BranchTrueFalseTotal
if (opts->multi) { 8 124 132
283 8 HV *h = (HV *)SvRV(opts->into);
284 30 ▶ for (i = 0; i < opts->n_ids; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < opts->n_ids; i++) { 22 8 30
285 22 const hash_result_t *r = &results[i];
286 22 SV *val = newSVpvn(r->out, r->out_len);
287 22 ▶ if (opts->format != HF_RAW) SvUTF8_off(val);
Branch: 1 branch
BranchTrueFalseTotal
if (opts->format != HF_RAW) SvUTF8_off(val); 22 0 22
288 22 (void)hv_store(h, r->name, (I32)strlen(r->name), val, 0);
289 }
290 } else {
291 124 SV *target = SvRV(opts->into);
292 124 const hash_result_t *r = &results[0];
293 124 sv_setpvn(target, r->out, r->out_len);
294 124 ▶ if (opts->format != HF_RAW) SvUTF8_off(target);
Branch: 1 branch
BranchTrueFalseTotal
if (opts->format != HF_RAW) SvUTF8_off(target); 104 20 124
295 }
296 132 }
297  
298 /* RECORD-phase emission: push one element into the user's arrayref.
299 * Element shape mirrors the READ/WRITE convention:
300 * single algo -> a scalar (the digest)
301 * multi algos -> a hashref (algo => digest, ...)
302 */
303 static void
304 8 append_record_results(pTHX_ const hash_opts_t *opts,
305 const hash_result_t *results)
306 {
307 8 AV *av = (AV *)SvRV(opts->into);
308 int i;
309 8 ▶ if (opts->multi) {
Branch: 1 branch
BranchTrueFalseTotal
if (opts->multi) { 2 6 8
310 2 HV *h = newHV();
311 8 ▶ for (i = 0; i < opts->n_ids; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < opts->n_ids; i++) { 6 2 8
312 6 const hash_result_t *r = &results[i];
313 6 SV *val = newSVpvn(r->out, r->out_len);
314 6 ▶ if (opts->format != HF_RAW) SvUTF8_off(val);
Branch: 1 branch
BranchTrueFalseTotal
if (opts->format != HF_RAW) SvUTF8_off(val); 6 0 6
315 6 (void)hv_store(h, r->name, (I32)strlen(r->name), val, 0);
316 }
317 2 av_push(av, newRV_noinc((SV *)h));
318 } else {
319 6 const hash_result_t *r = &results[0];
320 6 SV *val = newSVpvn(r->out, r->out_len);
321 6 ▶ if (opts->format != HF_RAW) SvUTF8_off(val);
Branch: 1 branch
BranchTrueFalseTotal
if (opts->format != HF_RAW) SvUTF8_off(val); 5 1 6
322 6 av_push(av, val);
323 }
324 8 }
325  
326 /* ============================================================
327 * READ / WRITE callbacks (passthrough + side-channel digest).
328 * ============================================================ */
329  
330 static SV *
331 143 hash_one_shot(pTHX_ FilePluginContext *ctx)
332 {
333 hash_opts_t opts;
334 hash_runner_t runner;
335 143 const hash_result_t *results = NULL;
336 143 STRLEN dlen = 0;
337 143 const char *dp = NULL;
338  
339 143 memset(&opts, 0, sizeof opts);
340 143 memset(&runner, 0, sizeof runner);
341  
342 143 decode_opts(aTHX_ ctx->options, &opts, PHASE_ONE_SHOT);
343  
344 250 ▶ if (ctx->data && SvOK(ctx->data)) dp = SvPV(ctx->data, dlen);
Branch: 2 branches
BranchTrueFalseTotal
if (ctx->data && SvOK(ctx->data)) dp = SvPV(ctx->data, dlen); 125 0 125
if (ctx->data && SvOK(ctx->data)) dp = SvPV(ctx->data, dlen); 125 0 125
345  
346 125 run_full(aTHX_ &opts, dp, (size_t)dlen, &runner, &results);
347 125 emit_results(aTHX_ &opts, results);
348 125 hash_runner_free(&runner);
349  
350 /* Passthrough. */
351 125 ▶ if (!ctx->data) return newSVpvn("", 0);
Branch: 1 branch
BranchTrueFalseTotal
if (!ctx->data) return newSVpvn("", 0); 0 125 125
352 125 return SvREFCNT_inc_simple_NN(ctx->data);
353 }
354  
355 static SV *
356 137 hash_read_cb(pTHX_ FilePluginContext *ctx)
357 {
358 137 return hash_one_shot(aTHX_ ctx);
359 }
360  
361 static SV *
362 6 hash_write_cb(pTHX_ FilePluginContext *ctx)
363 {
364 6 return hash_one_shot(aTHX_ ctx);
365 }
366  
367 /* ============================================================
368 * RECORD callback (one digest per record, pushed into arrayref).
369 * ============================================================ */
370  
371 static SV *
372 10 hash_record_cb(pTHX_ FilePluginContext *ctx, SV *record)
373 {
374 hash_opts_t opts;
375 hash_runner_t runner;
376 10 const hash_result_t *results = NULL;
377 10 STRLEN dlen = 0;
378 10 const char *dp = NULL;
379  
380 10 memset(&opts, 0, sizeof opts);
381 10 memset(&runner, 0, sizeof runner);
382  
383 10 decode_opts(aTHX_ ctx->options, &opts, PHASE_RECORD);
384  
385 16 ▶ if (record && SvOK(record)) dp = SvPV(record, dlen);
Branch: 2 branches
BranchTrueFalseTotal
if (record && SvOK(record)) dp = SvPV(record, dlen); 8 0 8
if (record && SvOK(record)) dp = SvPV(record, dlen); 8 0 8
386  
387 8 run_full(aTHX_ &opts, dp, (size_t)dlen, &runner, &results);
388 8 append_record_results(aTHX_ &opts, results);
389 8 hash_runner_free(&runner);
390  
391 /* Passthrough the record so downstream filters / map_lines see it
392 * unchanged. The dispatcher mortalises on its way out. */
393 8 ▶ if (!record) return &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
if (!record) return &PL_sv_undef; 0 8 8
394 8 return SvREFCNT_inc_simple_NN(record);
395 }
396  
397 /* ============================================================
398 * STREAM callback.
399 * ============================================================ */
400  
401 typedef struct {
402 hash_runner_t runner;
403 hash_opts_t opts;
404 SV *into_ref; /* +1 refcount */
405 } hash_stream_state_t;
406  
407 static int
408 102 hash_stream_cb(pTHX_ FilePluginContext *ctx,
409 const char *chunk, size_t len, int eof)
410 {
411 102 hash_stream_state_t *st = (hash_stream_state_t *)ctx->call_state;
412  
413 102 ▶ if (!st) {
Branch: 1 branch
BranchTrueFalseTotal
if (!st) { 7 95 102
414 7 st = (hash_stream_state_t *)calloc(1, sizeof *st);
415 7 ▶ if (!st) {
Branch: 1 branch
BranchTrueFalseTotal
if (!st) { 0 7 7
416 0 warn("File::Raw::Hash: stream alloc failed");
417 0 ctx->cancel = 1;
418 0 return 1;
419 }
420 7 decode_opts(aTHX_ ctx->options, &st->opts, PHASE_ONE_SHOT);
421 7 ▶ if (hash_runner_init(&st->runner, st->opts.ids, st->opts.n_ids,
Branch: 1 branch
BranchTrueFalseTotal
if (hash_runner_init(&st->runner, st->opts.ids, st->opts.n_ids, 0 7 7
422 st->opts.format, st->opts.xxh64_seed) != 0) {
423 0 free(st);
424 0 warn("File::Raw::Hash: stream runner init failed");
425 0 ctx->cancel = 1;
426 0 return 1;
427 }
428 7 ▶ if (st->opts.hmac_key_sv) {
Branch: 1 branch
BranchTrueFalseTotal
if (st->opts.hmac_key_sv) { 1 6 7
429 STRLEN klen;
430 const unsigned char *kp =
431 1 (const unsigned char *)SvPV(st->opts.hmac_key_sv, klen);
432 1 ▶ if (hash_runner_set_hmac(&st->runner, kp, (size_t)klen) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (hash_runner_set_hmac(&st->runner, kp, (size_t)klen) != 0) { 0 1 1
433 0 hash_runner_free(&st->runner);
434 0 free(st);
435 0 warn("File::Raw::Hash: stream HMAC setup failed");
436 0 ctx->cancel = 1;
437 0 return 1;
438 }
439 }
440 7 st->into_ref = SvREFCNT_inc_simple_NN(st->opts.into);
441 7 ctx->call_state = st;
442 }
443  
444 102 ▶ if (chunk && len) {
Branch: 2 branches
BranchTrueFalseTotal
if (chunk && len) { 95 7 102
if (chunk && len) { 95 0 95
445 95 hash_runner_update(&st->runner, chunk, len);
446 }
447  
448 102 ▶ if (eof) {
Branch: 1 branch
BranchTrueFalseTotal
if (eof) { 7 95 102
449 7 const hash_result_t *results = NULL;
450 7 ▶ if (hash_runner_finish(&st->runner, &results) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (hash_runner_finish(&st->runner, &results) != 0) { 0 7 7
451 0 hash_runner_free(&st->runner);
452 0 SvREFCNT_dec(st->into_ref);
453 0 free(st);
454 0 ctx->call_state = NULL;
455 0 warn("File::Raw::Hash: stream finish failed");
456 0 ctx->cancel = 1;
457 0 return 1;
458 }
459 7 emit_results(aTHX_ &st->opts, results);
460 7 hash_runner_free(&st->runner);
461 7 SvREFCNT_dec(st->into_ref);
462 7 free(st);
463 7 ctx->call_state = NULL;
464 }
465  
466 102 return 0; /* continue */
467 }
468  
469 /* ============================================================ */
470  
471 static FilePlugin hash_plugin;
472  
473 /* ============================================================
474 * The C ABI - include/frh_abi.h. Perl-free thunks over the runner,
475 * every runner initialised with HF_RAW so raw digest bytes come out
476 * of the existing finish path (t/14-raw-parity.t proves that path
477 * equals the formatted one, HMAC included).
478 * ============================================================ */
479  
480 #include "frh_abi.h"
481  
482 /* The registry rows the ABI hands out. Filled once from the internal
483 * table rather than aliased to it, so frh_algo_t owes nothing to
484 * hash_algo_info_t's layout. */
485 static frh_algo_t FRH_ALGOS[HA_COUNT];
486 static int frh_algos_ready = 0;
487  
488 27 static void frh_algos_fill(void)
489 {
490 int i;
491 27 ▶ if (frh_algos_ready)
Branch: 1 branch
BranchTrueFalseTotal
if (frh_algos_ready) 26 1 27
492 26 return;
493 8 ▶ for (i = 0; i < HA_COUNT; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < HA_COUNT; i++) { 7 1 8
494 7 const hash_algo_info_t *a = hash_algo_by_id((hash_algo_id_t)i);
495 7 FRH_ALGOS[i].name = a->name;
496 7 FRH_ALGOS[i].id = i;
497 7 FRH_ALGOS[i].digest_size = a->digest_size;
498 7 FRH_ALGOS[i].hmac_able = a->hmac_able;
499 7 FRH_ALGOS[i].block_size = a->hmac_block_size;
500 }
501 1 frh_algos_ready = 1;
502 }
503  
504 13 static const frh_algo_t *frh_abi_algo_by_name(const char *name, size_t len)
505 {
506 const hash_algo_info_t *a;
507 13 ▶ if (!name)
Branch: 1 branch
BranchTrueFalseTotal
if (!name) 0 13 13
508 0 return NULL;
509 13 frh_algos_fill();
510 13 a = hash_algo_lookup(name, len);
511 13 ▶ return a ? &FRH_ALGOS[a->id] : NULL;
Branch: 1 branch
BranchTrueFalseTotal
return a ? &FRH_ALGOS[a->id] : NULL; 12 1 13
512 }
513  
514 16 static const frh_algo_t *frh_abi_algo_by_id(int id)
515 {
516 16 ▶ if (id < 0 || id >= HA_COUNT)
Branch: 2 branches
BranchTrueFalseTotal
if (id < 0 || id >= HA_COUNT) 15 1 16
if (id < 0 || id >= HA_COUNT) 1 14 15
517 2 return NULL;
518 14 frh_algos_fill();
519 14 return &FRH_ALGOS[id];
520 }
521  
522 1 static int frh_abi_algo_count(void)
523 {
524 1 return HA_COUNT;
525 }
526  
527 /* The handle behind the opaque pointer. `finished` enforces the
528 * exactly-once rule the internal finish does not: hash_runner_finish
529 * tolerates a second call for leak safety but would re-finalise dead
530 * contexts and hand back garbage. */
531 typedef struct {
532 hash_runner_t r;
533 int finished;
534 } frh_runner_t;
535  
536 15 static void *frh_abi_new_runner(const int *ids, int n,
537 unsigned long long seed)
538 {
539 frh_runner_t *w;
540 hash_algo_id_t *hids;
541 int i;
542  
543 15 ▶ if (!ids || n < 1)
Branch: 2 branches
BranchTrueFalseTotal
if (!ids || n < 1) 14 1 15
if (!ids || n < 1) 0 14 14
544 1 return NULL;
545 28 ▶ for (i = 0; i < n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) 16 12 28
546 16 ▶ if (ids[i] < 0 || ids[i] >= HA_COUNT)
Branch: 2 branches
BranchTrueFalseTotal
if (ids[i] < 0 || ids[i] >= HA_COUNT) 15 1 16
if (ids[i] < 0 || ids[i] >= HA_COUNT) 1 14 15
547 2 return NULL;
548  
549 12 hids = (hash_algo_id_t *)malloc((size_t)n * sizeof *hids);
550 12 ▶ if (!hids)
Branch: 1 branch
BranchTrueFalseTotal
if (!hids) 0 12 12
551 0 return NULL;
552 26 ▶ for (i = 0; i < n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) 14 12 26
553 14 hids[i] = (hash_algo_id_t)ids[i];
554  
555 12 w = (frh_runner_t *)malloc(sizeof *w);
556 12 ▶ if (!w) {
Branch: 1 branch
BranchTrueFalseTotal
if (!w) { 0 12 12
557 0 free(hids);
558 0 return NULL;
559 }
560 12 ▶ if (hash_runner_init(&w->r, hids, n, HF_RAW, (uint64_t)seed) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (hash_runner_init(&w->r, hids, n, HF_RAW, (uint64_t)seed) != 0) { 0 12 12
561 0 free(hids);
562 0 free(w);
563 0 return NULL;
564 }
565 12 free(hids);
566 12 w->finished = 0;
567 12 return w;
568 }
569  
570 2 static int frh_abi_runner_set_hmac(void *vr, const unsigned char *key,
571 size_t klen)
572 {
573 2 frh_runner_t *w = (frh_runner_t *)vr;
574 2 ▶ if (!w || w->finished)
Branch: 2 branches
BranchTrueFalseTotal
if (!w || w->finished) 2 0 2
if (!w || w->finished) 0 2 2
575 0 return -1;
576 2 return hash_runner_set_hmac(&w->r, key, klen);
577 }
578  
579 14 static void frh_abi_runner_update(void *vr, const void *data, size_t len)
580 {
581 14 frh_runner_t *w = (frh_runner_t *)vr;
582 14 ▶ if (!w || w->finished)
Branch: 2 branches
BranchTrueFalseTotal
if (!w || w->finished) 14 0 14
if (!w || w->finished) 0 14 14
583 0 return;
584 14 hash_runner_update(&w->r, data, len);
585 }
586  
587 13 static int frh_abi_runner_finish(void *vr, unsigned char **outs)
588 {
589 13 frh_runner_t *w = (frh_runner_t *)vr;
590 const hash_result_t *res;
591 int i;
592  
593 13 ▶ if (!w || w->finished || !outs)
Branch: 3 branches
BranchTrueFalseTotal
if (!w || w->finished || !outs) 13 0 13
if (!w || w->finished || !outs) 12 1 13
if (!w || w->finished || !outs) 0 12 12
594 1 return -1;
595 /* validate every destination BEFORE finalising: a NULL discovered
596 * mid-copy would leave the runner finished and the output partial,
597 * the worst of both */
598 26 ▶ for (i = 0; i < w->r.count; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < w->r.count; i++) 14 12 26
599 14 ▶ if (!outs[i])
Branch: 1 branch
BranchTrueFalseTotal
if (!outs[i]) 0 14 14
600 0 return -1;
601 12 ▶ if (hash_runner_finish(&w->r, &res) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (hash_runner_finish(&w->r, &res) != 0) 0 12 12
602 0 return -1;
603 12 w->finished = 1;
604 26 ▶ for (i = 0; i < w->r.count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < w->r.count; i++) { 14 12 26
605 14 const hash_algo_info_t *a = hash_algo_by_id(res[i].id);
606 14 memcpy(outs[i], res[i].out, a->digest_size);
607 }
608 12 return 0;
609 }
610  
611 13 static void frh_abi_runner_free(void *vr)
612 {
613 13 frh_runner_t *w = (frh_runner_t *)vr;
614 13 ▶ if (!w)
Branch: 1 branch
BranchTrueFalseTotal
if (!w) 1 12 13
615 1 return;
616 12 hash_runner_free(&w->r);
617 12 free(w);
618 }
619  
620 /* One-shots: a runner born, fed and finished locally, so there is no
621 * second finalisation code path to drift. */
622 12 static int frh_abi_digest_common(int id, const unsigned char *key,
623 size_t klen,
624 const unsigned char *in, size_t n,
625 unsigned char *out)
626 {
627 void *w;
628 unsigned char *outs[1];
629 int rc;
630  
631 12 ▶ if (!out)
Branch: 1 branch
BranchTrueFalseTotal
if (!out) 0 12 12
632 0 return -1;
633 12 w = frh_abi_new_runner(&id, 1, 0);
634 12 ▶ if (!w)
Branch: 1 branch
BranchTrueFalseTotal
if (!w) 2 10 12
635 2 return -1;
636 10 ▶ if (key && frh_abi_runner_set_hmac(w, key, klen) != 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (key && frh_abi_runner_set_hmac(w, key, klen) != 0) { 1 9 10
if (key && frh_abi_runner_set_hmac(w, key, klen) != 0) { 0 1 1
637 0 frh_abi_runner_free(w);
638 0 return -1;
639 }
640 10 frh_abi_runner_update(w, in, n);
641 10 outs[0] = out;
642 10 rc = frh_abi_runner_finish(w, outs);
643 10 frh_abi_runner_free(w);
644 10 return rc;
645 }
646  
647 11 static int frh_abi_digest(int id, const unsigned char *in, size_t n,
648 unsigned char *out)
649 {
650 11 return frh_abi_digest_common(id, NULL, 0, in, n, out);
651 }
652  
653 1 static int frh_abi_hmac(int id, const unsigned char *key, size_t klen,
654 const unsigned char *in, size_t n,
655 unsigned char *out)
656 {
657 1 ▶ if (!key)
Branch: 1 branch
BranchTrueFalseTotal
if (!key) 0 1 1
658 0 return -1;
659 1 return frh_abi_digest_common(id, key, klen, in, n, out);
660 }
661  
662 static const frh_abi FRH_ABI = {
663 FRH_ABI_VERSION,
664 frh_abi_algo_by_name,
665 frh_abi_algo_by_id,
666 frh_abi_algo_count,
667 frh_abi_digest,
668 frh_abi_hmac,
669 frh_abi_new_runner,
670 frh_abi_runner_set_hmac,
671 frh_abi_runner_update,
672 frh_abi_runner_finish,
673 frh_abi_runner_free
674 };
675  
676 MODULE = File::Raw::Hash PACKAGE = File::Raw::Hash
677  
678 PROTOTYPES: DISABLE
679  
680 BOOT:
681 16 memset(&hash_plugin, 0, sizeof hash_plugin);
682 16 hash_plugin.name = "hash";
683 16 hash_plugin.read_fn = hash_read_cb;
684 16 hash_plugin.write_fn = hash_write_cb;
685 16 hash_plugin.record_fn = hash_record_cb;
686 16 hash_plugin.stream_fn = hash_stream_cb;
687 16 file_register_plugin(aTHX_ &hash_plugin);
688  
689 IV
690 _abi_ptr()
691 CODE:
692 2 ▶ RETVAL = PTR2IV(&FRH_ABI);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = PTR2IV(&FRH_ABI); 0 2 2
693 OUTPUT:
694 RETVAL
695  
696 SV*
697 _abi_selftest()
698 CODE:
699 {
700 /* Drive the table the way a C consumer would and report one
701 * named flag per member group, plus the raw "abc" digest of
702 * every algorithm as hex so the Perl tests can compare this
703 * path against the plugin path byte for byte. */
704 1 const frh_abi *H = &FRH_ABI;
705 1 HV *hv = newHV();
706 1 HV *digests = newHV();
707 1 int registry_ok = 1, digest_ok = 1, hmac_ok = 0;
708 1 int runner_ok = 0, edges_ok = 1;
709 1 int i, n = H->algo_count();
710 unsigned char buf[HASH_MAX_DIGEST_SIZE];
711 char hex[HASH_MAX_DIGEST_SIZE * 2 + 1];
712  
713 /* registry: dense ids, names round-trip, sane sizes */
714 8 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 7 1 8
715 7 const frh_algo_t *a = H->algo_by_id(i);
716 const frh_algo_t *b;
717 7 ▶ if (!a || a->id != i || !a->name ||
Branch: 3 branches
BranchTrueFalseTotal
if (!a || a->id != i || !a->name || 7 0 7
if (!a || a->id != i || !a->name || 7 0 7
if (!a || a->id != i || !a->name || 7 0 7
718 7 ▶ a->digest_size == 0 ||
Branch: 1 branch
BranchTrueFalseTotal
a->digest_size == 0 || 7 0 7
719 7 ▶ a->digest_size > HASH_MAX_DIGEST_SIZE) {
Branch: 1 branch
BranchTrueFalseTotal
a->digest_size > HASH_MAX_DIGEST_SIZE) { 0 7 7
720 0 registry_ok = 0;
721 0 break;
722 }
723 7 b = H->algo_by_name(a->name, strlen(a->name));
724 7 ▶ if (b != a) { registry_ok = 0; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (b != a) { registry_ok = 0; break; } 0 7 7
725 7 ▶ if (a->hmac_able && a->block_size == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (a->hmac_able && a->block_size == 0) { 4 3 7
if (a->hmac_able && a->block_size == 0) { 0 4 4
726 0 registry_ok = 0;
727 0 break;
728 }
729 }
730 1 registry_ok = registry_ok
731 1 ▶ && !H->algo_by_id(-1) && !H->algo_by_id(n)
Branch: 2 branches
BranchTrueFalseTotal
&& !H->algo_by_id(-1) && !H->algo_by_id(n) 1 0 1
&& !H->algo_by_id(-1) && !H->algo_by_id(n) 1 0 1
732 2 ▶ && !H->algo_by_name("nonesuch", 8);
Branch: 2 branches
BranchTrueFalseTotal
&& !H->algo_by_name("nonesuch", 8); 1 0 1
&& !H->algo_by_name("nonesuch", 8); 1 0 1
733  
734 /* one-shot digest of "abc" per algorithm, reported as hex */
735 8 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 7 1 8
736 7 const frh_algo_t *a = H->algo_by_id(i);
737 size_t j;
738 7 ▶ if (H->digest(i, (const unsigned char *)"abc", 3, buf) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (H->digest(i, (const unsigned char *)"abc", 3, buf) != 0) { 0 7 7
739 0 digest_ok = 0;
740 0 continue;
741 }
742 183 ▶ for (j = 0; j < a->digest_size; j++)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < a->digest_size; j++) 176 7 183
743 176 sprintf(hex + 2 * j, "%02x", buf[j]);
744 7 (void)hv_store(digests, a->name, (I32)strlen(a->name),
745 newSVpvn(hex, a->digest_size * 2), 0);
746 }
747  
748 /* HMAC: RFC 2202 case 1, checked here in C so the vector
749 * guards the table even if the Perl tests thin out */
750 {
751 const frh_algo_t *sha1 =
752 1 H->algo_by_name("sha1", 4);
753 static const unsigned char want[20] = {
754 0xb6,0x17,0x31,0x86,0x55,0x05,0x72,0x64,0xe2,0x8b,
755 0xc0,0xb6,0xfb,0x37,0x8c,0x8e,0xf1,0x46,0xbe,0x00
756 };
757 unsigned char key[20];
758 1 memset(key, 0x0b, sizeof key);
759 2 if (sha1 &&
760 1 H->hmac(sha1->id, key, sizeof key,
761 (const unsigned char *)"Hi There", 8, buf) == 0)
762 1 hmac_ok = memcmp(buf, want, 20) == 0;
763 }
764  
765 /* lockstep: sha256+blake3 over a split stream equals the
766 * one-shots over the joined stream */
767 {
768 1 const frh_algo_t *s = H->algo_by_name("sha256", 6);
769 1 const frh_algo_t *b = H->algo_by_name("blake3", 6);
770 1 ▶ if (s && b) {
Branch: 2 branches
BranchTrueFalseTotal
if (s && b) { 1 0 1
if (s && b) { 1 0 1
771 int ids[2];
772 unsigned char o1[HASH_MAX_DIGEST_SIZE];
773 unsigned char o2[HASH_MAX_DIGEST_SIZE];
774 unsigned char *outs[2];
775 void *r;
776 1 ids[0] = s->id; ids[1] = b->id;
777 1 outs[0] = o1; outs[1] = o2;
778 1 r = H->new_runner(ids, 2, 0);
779 1 ▶ if (r) {
Branch: 1 branch
BranchTrueFalseTotal
if (r) { 1 0 1
780 unsigned char w1[HASH_MAX_DIGEST_SIZE];
781 unsigned char w2[HASH_MAX_DIGEST_SIZE];
782 1 H->runner_update(r, "lock", 4);
783 1 H->runner_update(r, "", 0);
784 1 H->runner_update(r, "step", 4);
785 1 runner_ok = H->runner_finish(r, outs) == 0
786 1 ▶ && H->digest(s->id, (const unsigned char *)
Branch: 1 branch
BranchTrueFalseTotal
&& H->digest(s->id, (const unsigned char *) 1 0 1
787 "lockstep", 8, w1) == 0
788 1 ▶ && H->digest(b->id, (const unsigned char *)
Branch: 1 branch
BranchTrueFalseTotal
&& H->digest(b->id, (const unsigned char *) 1 0 1
789 "lockstep", 8, w2) == 0
790 1 ▶ && memcmp(o1, w1, s->digest_size) == 0
Branch: 1 branch
BranchTrueFalseTotal
&& memcmp(o1, w1, s->digest_size) == 0 1 0 1
791 2 ▶ && memcmp(o2, w2, b->digest_size) == 0;
Branch: 2 branches
BranchTrueFalseTotal
&& memcmp(o2, w2, b->digest_size) == 0; 1 0 1
&& memcmp(o2, w2, b->digest_size) == 0; 1 0 1
792 /* edge: a second finish refuses */
793 1 edges_ok = edges_ok
794 1 ▶ && H->runner_finish(r, outs) == -1;
Branch: 2 branches
BranchTrueFalseTotal
&& H->runner_finish(r, outs) == -1; 1 0 1
&& H->runner_finish(r, outs) == -1; 1 0 1
795 1 H->runner_free(r);
796 }
797 }
798 }
799  
800 /* edges: free(NULL); set_hmac including a non-hmac_able algo
801 * fails atomically and the runner stays usable plain */
802 1 H->runner_free(NULL);
803 {
804 1 const frh_algo_t *s = H->algo_by_name("sha256", 6);
805 1 const frh_algo_t *c = H->algo_by_name("crc32", 5);
806 1 ▶ if (s && c) {
Branch: 2 branches
BranchTrueFalseTotal
if (s && c) { 1 0 1
if (s && c) { 1 0 1
807 int ids[2];
808 unsigned char o1[HASH_MAX_DIGEST_SIZE];
809 unsigned char o2[HASH_MAX_DIGEST_SIZE];
810 unsigned char *outs[2];
811 void *r;
812 1 ids[0] = s->id; ids[1] = c->id;
813 1 outs[0] = o1; outs[1] = o2;
814 1 r = H->new_runner(ids, 2, 0);
815 1 ▶ edges_ok = edges_ok && r
Branch: 1 branch
BranchTrueFalseTotal
edges_ok = edges_ok && r 1 0 1
816 2 ▶ && H->runner_set_hmac(r,
Branch: 2 branches
BranchTrueFalseTotal
&& H->runner_set_hmac(r, 1 0 1
&& H->runner_set_hmac(r, 1 0 1
817 (const unsigned char *)"k", 1) == -1;
818 1 ▶ if (r) {
Branch: 1 branch
BranchTrueFalseTotal
if (r) { 1 0 1
819 1 H->runner_update(r, "abc", 3);
820 1 edges_ok = edges_ok
821 1 ▶ && H->runner_finish(r, outs) == 0;
Branch: 2 branches
BranchTrueFalseTotal
&& H->runner_finish(r, outs) == 0; 1 0 1
&& H->runner_finish(r, outs) == 0; 1 0 1
822 1 H->runner_free(r);
823 }
824 }
825 /* bad ids refuse */
826 1 edges_ok = edges_ok
827 1 ▶ && H->digest(-1, (const unsigned char *)"x", 1, buf) == -1
Branch: 1 branch
BranchTrueFalseTotal
&& H->digest(-1, (const unsigned char *)"x", 1, buf) == -1 1 0 1
828 1 ▶ && H->digest(n, (const unsigned char *)"x", 1, buf) == -1
Branch: 1 branch
BranchTrueFalseTotal
&& H->digest(n, (const unsigned char *)"x", 1, buf) == -1 1 0 1
829 2 ▶ && !H->new_runner(NULL, 1, 0);
Branch: 2 branches
BranchTrueFalseTotal
&& !H->new_runner(NULL, 1, 0); 1 0 1
&& !H->new_runner(NULL, 1, 0); 1 0 1
830 }
831  
832 1 (void)hv_stores(hv, "version", newSViv(H->version));
833 1 (void)hv_stores(hv, "registry_ok", newSViv(registry_ok));
834 1 (void)hv_stores(hv, "digest_ok", newSViv(digest_ok));
835 1 (void)hv_stores(hv, "hmac_ok", newSViv(hmac_ok));
836 1 (void)hv_stores(hv, "runner_ok", newSViv(runner_ok));
837 1 (void)hv_stores(hv, "edges_ok", newSViv(edges_ok));
838 1 (void)hv_stores(hv, "digests", newRV_noinc((SV *)digests));
839 1 RETVAL = newRV_noinc((SV *)hv);
840 }
841 OUTPUT:
842 RETVAL
843  
844 # ============================================================
845 # Test helper: invoke the hash plugin's record_fn through File::Raw's
846 # dispatch_record entry point. Public name has a leading underscore to
847 # signal "not part of the supported API" - it exists so the test suite
848 # can exercise RECORD phase end-to-end before File::Raw exposes a
849 # user-facing per-record iterator.
850 # ============================================================
851  
852 SV*
853 _test_record_one(record_sv, ...)
854 SV *record_sv
855 PREINIT:
856 HV *opts;
857 SV *result;
858 int i;
859 CODE:
860 10 ▶ if ((items - 1) % 2 != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((items - 1) % 2 != 0) 0 10 10
861 0 croak("File::Raw::Hash::_test_record_one: odd number of "
862 "key/value option args");
863 10 opts = newHV();
864 /* Default plugin to "hash" so the caller can omit it. */
865 10 (void)hv_stores(opts, "plugin", newSVpvs("hash"));
866 32 ▶ for (i = 1; i < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i += 2) { 22 10 32
867 STRLEN klen;
868 22 const char *kp = SvPV(ST(i), klen);
869 22 SV *vp = SvREFCNT_inc(ST(i + 1));
870 22 (void)hv_store(opts, kp, (I32)klen, vp, 0);
871 }
872 10 result = file_plugin_dispatch_record(aTHX_ opts, NULL, record_sv);
873 8 SvREFCNT_dec((SV *)opts);
874 8 ▶ if (!result) {
Branch: 1 branch
BranchTrueFalseTotal
if (!result) { 0 8 8
875 0 RETVAL = &PL_sv_undef;
876 0 SvREFCNT_inc(RETVAL);
877 } else {
878 8 RETVAL = result;
879 }
880 OUTPUT:
881 RETVAL
« blib/arch/File/Raw/Hash/Install/Files.pm ↑ Summary hashx.c »