| 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 |
4415 |
| str_eq(const char *a, STRLEN alen, const char *b) |
| 59 |
|
| { |
| 60 |
4415 |
▶ | return alen == strlen(b) && memcmp(a, b, alen) == 0; |
Branch: 2 branches
| Branch | True | False | Total |
| return alen == strlen(b) && memcmp(a, b, alen) == 0; |
1724 |
2691 |
4415 |
| return alen == strlen(b) && memcmp(a, b, alen) == 0; |
1034 |
690 |
1724 |
|
| 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 |
604 |
| known_opt(const char *key, STRLEN klen) |
| 72 |
|
| { |
| 73 |
|
| const char *const *p; |
| 74 |
2298 |
▶ | for (p = VALID_OPT_KEYS; *p; p++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (p = VALID_OPT_KEYS; *p; p++) { |
2297 |
1 |
2298 |
|
| 75 |
2297 |
▶ | if (str_eq(key, klen, *p)) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (str_eq(key, klen, *p)) return 1; |
603 |
1694 |
2297 |
|
| 76 |
|
| } |
| 77 |
1 |
| return 0; |
| 78 |
|
| } |
| 79 |
|
| |
| 80 |
|
| /* Parse one algo name SV, croak on unknown. */ |
| 81 |
|
| static hash_algo_id_t |
| 82 |
190 |
| parse_algo_sv(pTHX_ SV *sv) |
| 83 |
|
| { |
| 84 |
|
| STRLEN alen; |
| 85 |
|
| const char *ap; |
| 86 |
|
| const hash_algo_info_t *info; |
| 87 |
|
| |
| 88 |
190 |
▶ | if (!SvOK(sv)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvOK(sv)) |
0 |
190 |
190 |
|
| 89 |
0 |
| croak("File::Raw::Hash: algo name must not be undef"); |
| 90 |
190 |
▶ | if (SvROK(sv)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvROK(sv)) |
0 |
190 |
190 |
|
| 91 |
0 |
| croak("File::Raw::Hash: algo name must be a string, not a reference"); |
| 92 |
190 |
| ap = SvPV(sv, alen); |
| 93 |
190 |
| info = hash_algo_lookup(ap, alen); |
| 94 |
190 |
▶ | if (!info) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!info) |
1 |
189 |
190 |
|
| 95 |
1 |
| croak("File::Raw::Hash: unknown algo '%.*s' " |
| 96 |
|
| "(known: sha256 sha512 sha1 md5 crc32 xxh64 blake3)", |
| 97 |
|
| (int)alen, ap); |
| 98 |
189 |
| 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 |
173 |
| decode_opts(pTHX_ HV *opts_hv, hash_opts_t *opts, decode_phase_t phase) |
| 105 |
|
| { |
| 106 |
|
| HE *he; |
| 107 |
173 |
| SV *algo_sv = NULL; |
| 108 |
173 |
| SV *algos_sv = NULL; |
| 109 |
173 |
| SV *fmt_sv = NULL; |
| 110 |
173 |
| SV *seed_sv = NULL; |
| 111 |
|
| |
| 112 |
173 |
▶ | if (!opts_hv) croak("File::Raw::Hash: missing options"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!opts_hv) croak("File::Raw::Hash: missing options"); |
0 |
173 |
173 |
|
| 113 |
|
| |
| 114 |
|
| /* First pass: validate keys + grab the ones we care about. */ |
| 115 |
173 |
| hv_iterinit(opts_hv); |
| 116 |
776 |
▶ | while ((he = hv_iternext(opts_hv))) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((he = hv_iternext(opts_hv))) { |
604 |
172 |
776 |
|
| 117 |
|
| I32 klen_i; |
| 118 |
604 |
| const char *key = hv_iterkey(he, &klen_i); |
| 119 |
604 |
| STRLEN klen = (STRLEN)klen_i; |
| 120 |
604 |
| SV *val = hv_iterval(opts_hv, he); |
| 121 |
|
| |
| 122 |
604 |
▶ | if (!known_opt(key, klen)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!known_opt(key, klen)) { |
1 |
603 |
604 |
|
| 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 |
603 |
▶ | if (str_eq(key, klen, "algo")) algo_sv = val; |
Branch: 1 branch
| Branch | True | False | Total |
| if (str_eq(key, klen, "algo")) algo_sv = val; |
158 |
445 |
603 |
|
| 128 |
445 |
▶ | else if (str_eq(key, klen, "algos")) algos_sv = val; |
Branch: 1 branch
| Branch | True | False | Total |
| else if (str_eq(key, klen, "algos")) algos_sv = val; |
16 |
429 |
445 |
|
| 129 |
429 |
▶ | else if (str_eq(key, klen, "into")) opts->into = val; |
Branch: 1 branch
| Branch | True | False | Total |
| else if (str_eq(key, klen, "into")) opts->into = val; |
172 |
257 |
429 |
|
| 130 |
257 |
▶ | else if (str_eq(key, klen, "format")) fmt_sv = val; |
Branch: 1 branch
| Branch | True | False | Total |
| else if (str_eq(key, klen, "format")) fmt_sv = val; |
52 |
205 |
257 |
|
| 131 |
205 |
▶ | else if (str_eq(key, klen, "hmac_key")) opts->hmac_key_sv = val; |
Branch: 1 branch
| Branch | True | False | Total |
| else if (str_eq(key, klen, "hmac_key")) opts->hmac_key_sv = val; |
26 |
179 |
205 |
|
| 132 |
179 |
▶ | else if (str_eq(key, klen, "xxh64_seed")) seed_sv = val; |
Branch: 1 branch
| Branch | True | False | Total |
| else if (str_eq(key, klen, "xxh64_seed")) seed_sv = val; |
7 |
172 |
179 |
|
| 133 |
|
| /* "plugin" key: silently ignored. */ |
| 134 |
|
| } |
| 135 |
|
| |
| 136 |
|
| /* Mutual exclusion. */ |
| 137 |
172 |
▶ | if (algo_sv && SvOK(algo_sv) && algos_sv && SvOK(algos_sv)) |
Branch: 4 branches
| Branch | True | False | Total |
| if (algo_sv && SvOK(algo_sv) && algos_sv && SvOK(algos_sv)) |
157 |
15 |
172 |
| if (algo_sv && SvOK(algo_sv) && algos_sv && SvOK(algos_sv)) |
157 |
0 |
157 |
| if (algo_sv && SvOK(algo_sv) && algos_sv && SvOK(algos_sv)) |
1 |
156 |
157 |
| 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 |
171 |
▶ | if (algos_sv && SvOK(algos_sv)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (algos_sv && SvOK(algos_sv)) { |
15 |
156 |
171 |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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 |
156 |
▶ | } else if (algo_sv && SvOK(algo_sv)) { |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (algo_sv && SvOK(algo_sv)) { |
156 |
0 |
156 |
| } else if (algo_sv && SvOK(algo_sv)) { |
156 |
0 |
156 |
|
| 163 |
156 |
| opts->ids[0] = parse_algo_sv(aTHX_ algo_sv); |
| 164 |
155 |
| opts->n_ids = 1; |
| 165 |
155 |
| 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 |
219 |
▶ | if (fmt_sv && SvOK(fmt_sv)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (fmt_sv && SvOK(fmt_sv)) { |
52 |
116 |
168 |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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 |
116 |
| opts->format = HF_HEX; |
| 186 |
|
| } |
| 187 |
|
| |
| 188 |
|
| /* Resolve xxh64_seed. */ |
| 189 |
167 |
▶ | if (seed_sv && SvOK(seed_sv)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (seed_sv && SvOK(seed_sv)) { |
7 |
160 |
167 |
| if (seed_sv && SvOK(seed_sv)) { |
7 |
0 |
7 |
|
| 190 |
7 |
▶ | if (SvROK(seed_sv)) |
Branch: 1 branch
| Branch | True | False | Total |
| 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 |
186 |
▶ | if (opts->hmac_key_sv && SvOK(opts->hmac_key_sv)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (opts->hmac_key_sv && SvOK(opts->hmac_key_sv)) { |
26 |
140 |
166 |
| if (opts->hmac_key_sv && SvOK(opts->hmac_key_sv)) { |
26 |
0 |
26 |
|
| 200 |
|
| int j; |
| 201 |
26 |
▶ | if (SvROK(opts->hmac_key_sv)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvROK(opts->hmac_key_sv)) |
1 |
25 |
26 |
|
| 202 |
1 |
| croak("File::Raw::Hash: 'hmac_key' must be a byte string, " |
| 203 |
|
| "not a reference"); |
| 204 |
48 |
▶ | for (j = 0; j < opts->n_ids; j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < opts->n_ids; j++) { |
28 |
20 |
48 |
|
| 205 |
28 |
| const hash_algo_info_t *info = hash_algo_by_id(opts->ids[j]); |
| 206 |
28 |
▶ | if (!info->hmac_able) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!info->hmac_able) |
5 |
23 |
28 |
|
| 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 |
140 |
| opts->hmac_key_sv = NULL; |
| 213 |
|
| } |
| 214 |
|
| |
| 215 |
|
| /* Validate `into`. Required; shape depends on phase. */ |
| 216 |
160 |
▶ | if (!opts->into || !SvOK(opts->into)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!opts->into || !SvOK(opts->into)) |
159 |
1 |
160 |
| if (!opts->into || !SvOK(opts->into)) |
1 |
158 |
159 |
|
| 217 |
2 |
| croak("File::Raw::Hash: 'into' is required"); |
| 218 |
158 |
▶ | if (!SvROK(opts->into)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvROK(opts->into)) |
1 |
157 |
158 |
|
| 219 |
1 |
| croak("File::Raw::Hash: 'into' must be a reference"); |
| 220 |
|
| |
| 221 |
157 |
▶ | if (phase == PHASE_RECORD) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (phase == PHASE_RECORD) { |
10 |
147 |
157 |
|
| 222 |
10 |
▶ | if (SvTYPE(SvRV(opts->into)) != SVt_PVAV) |
Branch: 1 branch
| Branch | True | False | Total |
| 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 |
147 |
▶ | if (opts->multi) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (opts->multi) { |
9 |
138 |
147 |
|
| 229 |
9 |
▶ | if (SvTYPE(SvRV(opts->into)) != SVt_PVHV) |
Branch: 1 branch
| Branch | True | False | Total |
| 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 |
138 |
| SV *referent = SvRV(opts->into); |
| 234 |
138 |
| svtype t = SvTYPE(referent); |
| 235 |
138 |
▶ | if (t == SVt_PVAV || t == SVt_PVHV || t == SVt_PVCV |
Branch: 3 branches
| Branch | True | False | Total |
| if (t == SVt_PVAV || t == SVt_PVHV || t == SVt_PVCV |
138 |
0 |
138 |
| if (t == SVt_PVAV || t == SVt_PVHV || t == SVt_PVCV |
137 |
1 |
138 |
| if (t == SVt_PVAV || t == SVt_PVHV || t == SVt_PVCV |
137 |
0 |
137 |
|
| 236 |
137 |
▶ | || t == SVt_PVGV || t == SVt_PVFM || t == SVt_PVIO) |
Branch: 3 branches
| Branch | True | False | Total |
| || t == SVt_PVGV || t == SVt_PVFM || t == SVt_PVIO) |
137 |
0 |
137 |
| || t == SVt_PVGV || t == SVt_PVFM || t == SVt_PVIO) |
137 |
0 |
137 |
| || t == SVt_PVGV || t == SVt_PVFM || t == SVt_PVIO) |
0 |
137 |
137 |
|
| 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 |
146 |
| 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 |
146 |
▶ | if (hash_runner_init(runner, opts->ids, opts->n_ids, opts->format, |
Branch: 1 branch
| Branch | True | False | Total |
| if (hash_runner_init(runner, opts->ids, opts->n_ids, opts->format, |
0 |
146 |
146 |
|
| 252 |
146 |
| opts->xxh64_seed) != 0) |
| 253 |
0 |
| croak("File::Raw::Hash: out of memory initialising runner"); |
| 254 |
|
| |
| 255 |
146 |
▶ | if (opts->hmac_key_sv) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (opts->hmac_key_sv) { |
19 |
127 |
146 |
|
| 256 |
|
| STRLEN klen; |
| 257 |
|
| const unsigned char *kp = |
| 258 |
19 |
| (const unsigned char *)SvPV(opts->hmac_key_sv, klen); |
| 259 |
19 |
▶ | if (hash_runner_set_hmac(runner, kp, (size_t)klen) != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (hash_runner_set_hmac(runner, kp, (size_t)klen) != 0) { |
0 |
19 |
19 |
|
| 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 |
146 |
▶ | if (data && len) hash_runner_update(runner, data, len); |
Branch: 2 branches
| Branch | True | False | Total |
| if (data && len) hash_runner_update(runner, data, len); |
146 |
0 |
146 |
| if (data && len) hash_runner_update(runner, data, len); |
110 |
36 |
146 |
|
| 269 |
|
| |
| 270 |
146 |
▶ | if (hash_runner_finish(runner, out_results) != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (hash_runner_finish(runner, out_results) != 0) { |
0 |
146 |
146 |
|
| 271 |
0 |
| hash_runner_free(runner); |
| 272 |
0 |
| croak("File::Raw::Hash: out of memory finalising runner"); |
| 273 |
|
| } |
| 274 |
146 |
| } |
| 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 |
145 |
| emit_results(pTHX_ const hash_opts_t *opts, const hash_result_t *results) |
| 280 |
|
| { |
| 281 |
|
| int i; |
| 282 |
145 |
▶ | if (opts->multi) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (opts->multi) { |
8 |
137 |
145 |
|
| 283 |
8 |
| HV *h = (HV *)SvRV(opts->into); |
| 284 |
30 |
▶ | for (i = 0; i < opts->n_ids; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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 |
137 |
| SV *target = SvRV(opts->into); |
| 292 |
137 |
| const hash_result_t *r = &results[0]; |
| 293 |
137 |
| sv_setpvn(target, r->out, r->out_len); |
| 294 |
137 |
▶ | if (opts->format != HF_RAW) SvUTF8_off(target); |
Branch: 1 branch
| Branch | True | False | Total |
| if (opts->format != HF_RAW) SvUTF8_off(target); |
117 |
20 |
137 |
|
| 295 |
|
| } |
| 296 |
145 |
| } |
| 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
| Branch | True | False | Total |
| if (opts->multi) { |
2 |
6 |
8 |
|
| 310 |
2 |
| HV *h = newHV(); |
| 311 |
8 |
▶ | for (i = 0; i < opts->n_ids; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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 |
156 |
| hash_one_shot(pTHX_ FilePluginContext *ctx) |
| 332 |
|
| { |
| 333 |
|
| hash_opts_t opts; |
| 334 |
|
| hash_runner_t runner; |
| 335 |
156 |
| const hash_result_t *results = NULL; |
| 336 |
156 |
| STRLEN dlen = 0; |
| 337 |
156 |
| const char *dp = NULL; |
| 338 |
|
| |
| 339 |
156 |
| memset(&opts, 0, sizeof opts); |
| 340 |
156 |
| memset(&runner, 0, sizeof runner); |
| 341 |
|
| |
| 342 |
156 |
| decode_opts(aTHX_ ctx->options, &opts, PHASE_ONE_SHOT); |
| 343 |
|
| |
| 344 |
276 |
▶ | if (ctx->data && SvOK(ctx->data)) dp = SvPV(ctx->data, dlen); |
Branch: 2 branches
| Branch | True | False | Total |
| if (ctx->data && SvOK(ctx->data)) dp = SvPV(ctx->data, dlen); |
138 |
0 |
138 |
| if (ctx->data && SvOK(ctx->data)) dp = SvPV(ctx->data, dlen); |
138 |
0 |
138 |
|
| 345 |
|
| |
| 346 |
138 |
| run_full(aTHX_ &opts, dp, (size_t)dlen, &runner, &results); |
| 347 |
138 |
| emit_results(aTHX_ &opts, results); |
| 348 |
138 |
| hash_runner_free(&runner); |
| 349 |
|
| |
| 350 |
|
| /* Passthrough. */ |
| 351 |
138 |
▶ | if (!ctx->data) return newSVpvn("", 0); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ctx->data) return newSVpvn("", 0); |
0 |
138 |
138 |
|
| 352 |
138 |
| return SvREFCNT_inc_simple_NN(ctx->data); |
| 353 |
|
| } |
| 354 |
|
| |
| 355 |
|
| static SV * |
| 356 |
150 |
| hash_read_cb(pTHX_ FilePluginContext *ctx) |
| 357 |
|
| { |
| 358 |
150 |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| if (!st) { |
7 |
95 |
102 |
|
| 414 |
7 |
| st = (hash_stream_state_t *)calloc(1, sizeof *st); |
| 415 |
7 |
▶ | if (!st) { |
Branch: 1 branch
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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 |
121 |
| static void frh_algos_fill(void) |
| 489 |
|
| { |
| 490 |
|
| int i; |
| 491 |
121 |
▶ | if (frh_algos_ready) |
Branch: 1 branch
| Branch | True | False | Total |
| if (frh_algos_ready) |
117 |
4 |
121 |
|
| 492 |
117 |
| return; |
| 493 |
32 |
▶ | for (i = 0; i < HA_COUNT; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < HA_COUNT; i++) { |
28 |
4 |
32 |
|
| 494 |
28 |
| const hash_algo_info_t *a = hash_algo_by_id((hash_algo_id_t)i); |
| 495 |
28 |
| FRH_ALGOS[i].name = a->name; |
| 496 |
28 |
| FRH_ALGOS[i].id = i; |
| 497 |
28 |
| FRH_ALGOS[i].digest_size = a->digest_size; |
| 498 |
28 |
| FRH_ALGOS[i].hmac_able = a->hmac_able; |
| 499 |
28 |
| FRH_ALGOS[i].block_size = a->hmac_block_size; |
| 500 |
|
| } |
| 501 |
4 |
| frh_algos_ready = 1; |
| 502 |
|
| } |
| 503 |
|
| |
| 504 |
93 |
| 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 |
93 |
▶ | if (!name) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!name) |
0 |
93 |
93 |
|
| 508 |
0 |
| return NULL; |
| 509 |
93 |
| frh_algos_fill(); |
| 510 |
93 |
| a = hash_algo_lookup(name, len); |
| 511 |
93 |
▶ | return a ? &FRH_ALGOS[a->id] : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| return a ? &FRH_ALGOS[a->id] : NULL; |
92 |
1 |
93 |
|
| 512 |
|
| } |
| 513 |
|
| |
| 514 |
30 |
| static const frh_algo_t *frh_abi_algo_by_id(int id) |
| 515 |
|
| { |
| 516 |
30 |
▶ | if (id < 0 || id >= HA_COUNT) |
Branch: 2 branches
| Branch | True | False | Total |
| if (id < 0 || id >= HA_COUNT) |
29 |
1 |
30 |
| if (id < 0 || id >= HA_COUNT) |
1 |
28 |
29 |
|
| 517 |
2 |
| return NULL; |
| 518 |
28 |
| frh_algos_fill(); |
| 519 |
28 |
| return &FRH_ALGOS[id]; |
| 520 |
|
| } |
| 521 |
|
| |
| 522 |
3 |
| static int frh_abi_algo_count(void) |
| 523 |
|
| { |
| 524 |
3 |
| 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 |
95 |
| 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 |
95 |
▶ | if (!ids || n < 1) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!ids || n < 1) |
94 |
1 |
95 |
| if (!ids || n < 1) |
0 |
94 |
94 |
|
| 544 |
1 |
| return NULL; |
| 545 |
188 |
▶ | for (i = 0; i < n; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) |
96 |
92 |
188 |
|
| 546 |
96 |
▶ | if (ids[i] < 0 || ids[i] >= HA_COUNT) |
Branch: 2 branches
| Branch | True | False | Total |
| if (ids[i] < 0 || ids[i] >= HA_COUNT) |
95 |
1 |
96 |
| if (ids[i] < 0 || ids[i] >= HA_COUNT) |
1 |
94 |
95 |
|
| 547 |
2 |
| return NULL; |
| 548 |
|
| |
| 549 |
92 |
| hids = (hash_algo_id_t *)malloc((size_t)n * sizeof *hids); |
| 550 |
92 |
▶ | if (!hids) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!hids) |
0 |
92 |
92 |
|
| 551 |
0 |
| return NULL; |
| 552 |
186 |
▶ | for (i = 0; i < n; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) |
94 |
92 |
186 |
|
| 553 |
94 |
| hids[i] = (hash_algo_id_t)ids[i]; |
| 554 |
|
| |
| 555 |
92 |
| w = (frh_runner_t *)malloc(sizeof *w); |
| 556 |
92 |
▶ | if (!w) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!w) { |
0 |
92 |
92 |
|
| 557 |
0 |
| free(hids); |
| 558 |
0 |
| return NULL; |
| 559 |
|
| } |
| 560 |
92 |
▶ | if (hash_runner_init(&w->r, hids, n, HF_RAW, (uint64_t)seed) != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (hash_runner_init(&w->r, hids, n, HF_RAW, (uint64_t)seed) != 0) { |
0 |
92 |
92 |
|
| 561 |
0 |
| free(hids); |
| 562 |
0 |
| free(w); |
| 563 |
0 |
| return NULL; |
| 564 |
|
| } |
| 565 |
92 |
| free(hids); |
| 566 |
92 |
| w->finished = 0; |
| 567 |
92 |
| 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
| Branch | True | False | Total |
| 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 |
76815 |
| static void frh_abi_runner_update(void *vr, const void *data, size_t len) |
| 580 |
|
| { |
| 581 |
76815 |
| frh_runner_t *w = (frh_runner_t *)vr; |
| 582 |
76815 |
▶ | if (!w || w->finished) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!w || w->finished) |
76815 |
0 |
76815 |
| if (!w || w->finished) |
0 |
76815 |
76815 |
|
| 583 |
0 |
| return; |
| 584 |
76815 |
| hash_runner_update(&w->r, data, len); |
| 585 |
|
| } |
| 586 |
|
| |
| 587 |
93 |
| static int frh_abi_runner_finish(void *vr, unsigned char **outs) |
| 588 |
|
| { |
| 589 |
93 |
| frh_runner_t *w = (frh_runner_t *)vr; |
| 590 |
|
| const hash_result_t *res; |
| 591 |
|
| int i; |
| 592 |
|
| |
| 593 |
93 |
▶ | if (!w || w->finished || !outs) |
Branch: 3 branches
| Branch | True | False | Total |
| if (!w || w->finished || !outs) |
93 |
0 |
93 |
| if (!w || w->finished || !outs) |
92 |
1 |
93 |
| if (!w || w->finished || !outs) |
0 |
92 |
92 |
|
| 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 |
186 |
▶ | for (i = 0; i < w->r.count; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < w->r.count; i++) |
94 |
92 |
186 |
|
| 599 |
94 |
▶ | if (!outs[i]) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!outs[i]) |
0 |
94 |
94 |
|
| 600 |
0 |
| return -1; |
| 601 |
92 |
▶ | if (hash_runner_finish(&w->r, &res) != 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (hash_runner_finish(&w->r, &res) != 0) |
0 |
92 |
92 |
|
| 602 |
0 |
| return -1; |
| 603 |
92 |
| w->finished = 1; |
| 604 |
186 |
▶ | for (i = 0; i < w->r.count; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < w->r.count; i++) { |
94 |
92 |
186 |
|
| 605 |
94 |
| const hash_algo_info_t *a = hash_algo_by_id(res[i].id); |
| 606 |
94 |
| memcpy(outs[i], res[i].out, a->digest_size); |
| 607 |
|
| } |
| 608 |
92 |
| return 0; |
| 609 |
|
| } |
| 610 |
|
| |
| 611 |
93 |
| static void frh_abi_runner_free(void *vr) |
| 612 |
|
| { |
| 613 |
93 |
| frh_runner_t *w = (frh_runner_t *)vr; |
| 614 |
93 |
▶ | if (!w) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!w) |
1 |
92 |
93 |
|
| 615 |
1 |
| return; |
| 616 |
92 |
| hash_runner_free(&w->r); |
| 617 |
92 |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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 |
19 |
| memset(&hash_plugin, 0, sizeof hash_plugin); |
| 682 |
19 |
| hash_plugin.name = "hash"; |
| 683 |
19 |
| hash_plugin.read_fn = hash_read_cb; |
| 684 |
19 |
| hash_plugin.write_fn = hash_write_cb; |
| 685 |
19 |
| hash_plugin.record_fn = hash_record_cb; |
| 686 |
19 |
| hash_plugin.stream_fn = hash_stream_cb; |
| 687 |
19 |
| file_register_plugin(aTHX_ &hash_plugin); |
| 688 |
|
| { |
| 689 |
|
| /* The CPU's SHA instructions when it has them; FRH_PORTABLE set true |
| 690 |
|
| * in the environment keeps the C. Read from %ENV, not getenv(), which |
| 691 |
|
| * does not see %ENV everywhere. */ |
| 692 |
19 |
▶ | SV **svp = hv_fetchs(GvHVn(PL_envgv), "FRH_PORTABLE", 0); |
Branch: 1 branch
| Branch | True | False | Total |
| SV **svp = hv_fetchs(GvHVn(PL_envgv), "FRH_PORTABLE", 0); |
19 |
0 |
19 |
|
| 693 |
19 |
▶ | int portable = svp && SvTRUE(*svp); |
Branch: 2 branches
| Branch | True | False | Total |
| int portable = svp && SvTRUE(*svp); |
1 |
18 |
19 |
| int portable = svp && SvTRUE(*svp); |
1 |
0 |
1 |
|
| 694 |
19 |
| sha256_select(portable); |
| 695 |
19 |
| sha1_select(portable); |
| 696 |
|
| } |
| 697 |
|
| |
| 698 |
|
| SV * |
| 699 |
|
| implementation() |
| 700 |
|
| CODE: |
| 701 |
|
| { |
| 702 |
|
| /* Which code computes each algorithm: "portable" (C), or the name of |
| 703 |
|
| * the CPU instructions in use. */ |
| 704 |
2 |
| HV *hv = newHV(); |
| 705 |
2 |
| int i, n = FRH_ABI.algo_count(); |
| 706 |
16 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
14 |
2 |
16 |
|
| 707 |
14 |
| const frh_algo_t *a = FRH_ABI.algo_by_id(i); |
| 708 |
2 |
| const char *impl = !strcmp(a->name, "sha256") ? sha256_implementation() |
| 709 |
26 |
▶ | : !strcmp(a->name, "sha1") ? sha1_implementation() |
Branch: 1 branch
| Branch | True | False | Total |
| : !strcmp(a->name, "sha1") ? sha1_implementation() |
2 |
12 |
14 |
|
| 710 |
12 |
▶ | : "portable"; |
Branch: 1 branch
| Branch | True | False | Total |
| : "portable"; |
2 |
10 |
12 |
|
| 711 |
14 |
| (void)hv_store(hv, a->name, (I32)strlen(a->name), newSVpv(impl, 0), 0); |
| 712 |
|
| } |
| 713 |
2 |
| RETVAL = newRV_noinc((SV *)hv); |
| 714 |
|
| } |
| 715 |
|
| OUTPUT: |
| 716 |
|
| RETVAL |
| 717 |
|
| |
| 718 |
|
| IV |
| 719 |
|
| _abi_ptr() |
| 720 |
|
| CODE: |
| 721 |
2 |
▶ | RETVAL = PTR2IV(&FRH_ABI); |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = PTR2IV(&FRH_ABI); |
0 |
2 |
2 |
|
| 722 |
|
| OUTPUT: |
| 723 |
|
| RETVAL |
| 724 |
|
| |
| 725 |
|
| SV* |
| 726 |
|
| _abi_selftest() |
| 727 |
|
| CODE: |
| 728 |
|
| { |
| 729 |
|
| /* Drive the table the way a C consumer would and report one |
| 730 |
|
| * named flag per member group, plus the raw "abc" digest of |
| 731 |
|
| * every algorithm as hex so the Perl tests can compare this |
| 732 |
|
| * path against the plugin path byte for byte. */ |
| 733 |
1 |
| const frh_abi *H = &FRH_ABI; |
| 734 |
1 |
| HV *hv = newHV(); |
| 735 |
1 |
| HV *digests = newHV(); |
| 736 |
1 |
| int registry_ok = 1, digest_ok = 1, hmac_ok = 0; |
| 737 |
1 |
| int runner_ok = 0, edges_ok = 1; |
| 738 |
1 |
| int i, n = H->algo_count(); |
| 739 |
|
| unsigned char buf[HASH_MAX_DIGEST_SIZE]; |
| 740 |
|
| char hex[HASH_MAX_DIGEST_SIZE * 2 + 1]; |
| 741 |
|
| |
| 742 |
|
| /* registry: dense ids, names round-trip, sane sizes */ |
| 743 |
8 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
7 |
1 |
8 |
|
| 744 |
7 |
| const frh_algo_t *a = H->algo_by_id(i); |
| 745 |
|
| const frh_algo_t *b; |
| 746 |
7 |
▶ | if (!a || a->id != i || !a->name || |
Branch: 3 branches
| Branch | True | False | Total |
| 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 |
|
| 747 |
7 |
▶ | a->digest_size == 0 || |
Branch: 1 branch
| Branch | True | False | Total |
| a->digest_size == 0 || |
7 |
0 |
7 |
|
| 748 |
7 |
▶ | a->digest_size > HASH_MAX_DIGEST_SIZE) { |
Branch: 1 branch
| Branch | True | False | Total |
| a->digest_size > HASH_MAX_DIGEST_SIZE) { |
0 |
7 |
7 |
|
| 749 |
0 |
| registry_ok = 0; |
| 750 |
0 |
| break; |
| 751 |
|
| } |
| 752 |
7 |
| b = H->algo_by_name(a->name, strlen(a->name)); |
| 753 |
7 |
▶ | if (b != a) { registry_ok = 0; break; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (b != a) { registry_ok = 0; break; } |
0 |
7 |
7 |
|
| 754 |
7 |
▶ | if (a->hmac_able && a->block_size == 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (a->hmac_able && a->block_size == 0) { |
4 |
3 |
7 |
| if (a->hmac_able && a->block_size == 0) { |
0 |
4 |
4 |
|
| 755 |
0 |
| registry_ok = 0; |
| 756 |
0 |
| break; |
| 757 |
|
| } |
| 758 |
|
| } |
| 759 |
1 |
| registry_ok = registry_ok |
| 760 |
1 |
▶ | && !H->algo_by_id(-1) && !H->algo_by_id(n) |
Branch: 2 branches
| Branch | True | False | Total |
| && !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 |
|
| 761 |
2 |
▶ | && !H->algo_by_name("nonesuch", 8); |
Branch: 2 branches
| Branch | True | False | Total |
| && !H->algo_by_name("nonesuch", 8); |
1 |
0 |
1 |
| && !H->algo_by_name("nonesuch", 8); |
1 |
0 |
1 |
|
| 762 |
|
| |
| 763 |
|
| /* one-shot digest of "abc" per algorithm, reported as hex */ |
| 764 |
8 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
7 |
1 |
8 |
|
| 765 |
7 |
| const frh_algo_t *a = H->algo_by_id(i); |
| 766 |
|
| size_t j; |
| 767 |
7 |
▶ | if (H->digest(i, (const unsigned char *)"abc", 3, buf) != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (H->digest(i, (const unsigned char *)"abc", 3, buf) != 0) { |
0 |
7 |
7 |
|
| 768 |
0 |
| digest_ok = 0; |
| 769 |
0 |
| continue; |
| 770 |
|
| } |
| 771 |
183 |
▶ | for (j = 0; j < a->digest_size; j++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < a->digest_size; j++) |
176 |
7 |
183 |
|
| 772 |
176 |
| sprintf(hex + 2 * j, "%02x", buf[j]); |
| 773 |
7 |
| (void)hv_store(digests, a->name, (I32)strlen(a->name), |
| 774 |
|
| newSVpvn(hex, a->digest_size * 2), 0); |
| 775 |
|
| } |
| 776 |
|
| |
| 777 |
|
| /* HMAC: RFC 2202 case 1, checked here in C so the vector |
| 778 |
|
| * guards the table even if the Perl tests thin out */ |
| 779 |
|
| { |
| 780 |
|
| const frh_algo_t *sha1 = |
| 781 |
1 |
| H->algo_by_name("sha1", 4); |
| 782 |
|
| static const unsigned char want[20] = { |
| 783 |
|
| 0xb6,0x17,0x31,0x86,0x55,0x05,0x72,0x64,0xe2,0x8b, |
| 784 |
|
| 0xc0,0xb6,0xfb,0x37,0x8c,0x8e,0xf1,0x46,0xbe,0x00 |
| 785 |
|
| }; |
| 786 |
|
| unsigned char key[20]; |
| 787 |
1 |
| memset(key, 0x0b, sizeof key); |
| 788 |
2 |
| if (sha1 && |
| 789 |
1 |
| H->hmac(sha1->id, key, sizeof key, |
| 790 |
|
| (const unsigned char *)"Hi There", 8, buf) == 0) |
| 791 |
1 |
| hmac_ok = memcmp(buf, want, 20) == 0; |
| 792 |
|
| } |
| 793 |
|
| |
| 794 |
|
| /* lockstep: sha256+blake3 over a split stream equals the |
| 795 |
|
| * one-shots over the joined stream */ |
| 796 |
|
| { |
| 797 |
1 |
| const frh_algo_t *s = H->algo_by_name("sha256", 6); |
| 798 |
1 |
| const frh_algo_t *b = H->algo_by_name("blake3", 6); |
| 799 |
1 |
▶ | if (s && b) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (s && b) { |
1 |
0 |
1 |
| if (s && b) { |
1 |
0 |
1 |
|
| 800 |
|
| int ids[2]; |
| 801 |
|
| unsigned char o1[HASH_MAX_DIGEST_SIZE]; |
| 802 |
|
| unsigned char o2[HASH_MAX_DIGEST_SIZE]; |
| 803 |
|
| unsigned char *outs[2]; |
| 804 |
|
| void *r; |
| 805 |
1 |
| ids[0] = s->id; ids[1] = b->id; |
| 806 |
1 |
| outs[0] = o1; outs[1] = o2; |
| 807 |
1 |
| r = H->new_runner(ids, 2, 0); |
| 808 |
1 |
▶ | if (r) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (r) { |
1 |
0 |
1 |
|
| 809 |
|
| unsigned char w1[HASH_MAX_DIGEST_SIZE]; |
| 810 |
|
| unsigned char w2[HASH_MAX_DIGEST_SIZE]; |
| 811 |
1 |
| H->runner_update(r, "lock", 4); |
| 812 |
1 |
| H->runner_update(r, "", 0); |
| 813 |
1 |
| H->runner_update(r, "step", 4); |
| 814 |
1 |
| runner_ok = H->runner_finish(r, outs) == 0 |
| 815 |
1 |
▶ | && H->digest(s->id, (const unsigned char *) |
Branch: 1 branch
| Branch | True | False | Total |
| && H->digest(s->id, (const unsigned char *) |
1 |
0 |
1 |
|
| 816 |
|
| "lockstep", 8, w1) == 0 |
| 817 |
1 |
▶ | && H->digest(b->id, (const unsigned char *) |
Branch: 1 branch
| Branch | True | False | Total |
| && H->digest(b->id, (const unsigned char *) |
1 |
0 |
1 |
|
| 818 |
|
| "lockstep", 8, w2) == 0 |
| 819 |
1 |
▶ | && memcmp(o1, w1, s->digest_size) == 0 |
Branch: 1 branch
| Branch | True | False | Total |
| && memcmp(o1, w1, s->digest_size) == 0 |
1 |
0 |
1 |
|
| 820 |
2 |
▶ | && memcmp(o2, w2, b->digest_size) == 0; |
Branch: 2 branches
| Branch | True | False | Total |
| && memcmp(o2, w2, b->digest_size) == 0; |
1 |
0 |
1 |
| && memcmp(o2, w2, b->digest_size) == 0; |
1 |
0 |
1 |
|
| 821 |
|
| /* edge: a second finish refuses */ |
| 822 |
1 |
| edges_ok = edges_ok |
| 823 |
1 |
▶ | && H->runner_finish(r, outs) == -1; |
Branch: 2 branches
| Branch | True | False | Total |
| && H->runner_finish(r, outs) == -1; |
1 |
0 |
1 |
| && H->runner_finish(r, outs) == -1; |
1 |
0 |
1 |
|
| 824 |
1 |
| H->runner_free(r); |
| 825 |
|
| } |
| 826 |
|
| } |
| 827 |
|
| } |
| 828 |
|
| |
| 829 |
|
| /* edges: free(NULL); set_hmac including a non-hmac_able algo |
| 830 |
|
| * fails atomically and the runner stays usable plain */ |
| 831 |
1 |
| H->runner_free(NULL); |
| 832 |
|
| { |
| 833 |
1 |
| const frh_algo_t *s = H->algo_by_name("sha256", 6); |
| 834 |
1 |
| const frh_algo_t *c = H->algo_by_name("crc32", 5); |
| 835 |
1 |
▶ | if (s && c) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (s && c) { |
1 |
0 |
1 |
| if (s && c) { |
1 |
0 |
1 |
|
| 836 |
|
| int ids[2]; |
| 837 |
|
| unsigned char o1[HASH_MAX_DIGEST_SIZE]; |
| 838 |
|
| unsigned char o2[HASH_MAX_DIGEST_SIZE]; |
| 839 |
|
| unsigned char *outs[2]; |
| 840 |
|
| void *r; |
| 841 |
1 |
| ids[0] = s->id; ids[1] = c->id; |
| 842 |
1 |
| outs[0] = o1; outs[1] = o2; |
| 843 |
1 |
| r = H->new_runner(ids, 2, 0); |
| 844 |
1 |
▶ | edges_ok = edges_ok && r |
Branch: 1 branch
| Branch | True | False | Total |
| edges_ok = edges_ok && r |
1 |
0 |
1 |
|
| 845 |
2 |
▶ | && H->runner_set_hmac(r, |
Branch: 2 branches
| Branch | True | False | Total |
| && H->runner_set_hmac(r, |
1 |
0 |
1 |
| && H->runner_set_hmac(r, |
1 |
0 |
1 |
|
| 846 |
|
| (const unsigned char *)"k", 1) == -1; |
| 847 |
1 |
▶ | if (r) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (r) { |
1 |
0 |
1 |
|
| 848 |
1 |
| H->runner_update(r, "abc", 3); |
| 849 |
1 |
| edges_ok = edges_ok |
| 850 |
1 |
▶ | && H->runner_finish(r, outs) == 0; |
Branch: 2 branches
| Branch | True | False | Total |
| && H->runner_finish(r, outs) == 0; |
1 |
0 |
1 |
| && H->runner_finish(r, outs) == 0; |
1 |
0 |
1 |
|
| 851 |
1 |
| H->runner_free(r); |
| 852 |
|
| } |
| 853 |
|
| } |
| 854 |
|
| /* bad ids refuse */ |
| 855 |
1 |
| edges_ok = edges_ok |
| 856 |
1 |
▶ | && H->digest(-1, (const unsigned char *)"x", 1, buf) == -1 |
Branch: 1 branch
| Branch | True | False | Total |
| && H->digest(-1, (const unsigned char *)"x", 1, buf) == -1 |
1 |
0 |
1 |
|
| 857 |
1 |
▶ | && H->digest(n, (const unsigned char *)"x", 1, buf) == -1 |
Branch: 1 branch
| Branch | True | False | Total |
| && H->digest(n, (const unsigned char *)"x", 1, buf) == -1 |
1 |
0 |
1 |
|
| 858 |
2 |
▶ | && !H->new_runner(NULL, 1, 0); |
Branch: 2 branches
| Branch | True | False | Total |
| && !H->new_runner(NULL, 1, 0); |
1 |
0 |
1 |
| && !H->new_runner(NULL, 1, 0); |
1 |
0 |
1 |
|
| 859 |
|
| } |
| 860 |
|
| |
| 861 |
1 |
| (void)hv_stores(hv, "version", newSViv(H->version)); |
| 862 |
1 |
| (void)hv_stores(hv, "registry_ok", newSViv(registry_ok)); |
| 863 |
1 |
| (void)hv_stores(hv, "digest_ok", newSViv(digest_ok)); |
| 864 |
1 |
| (void)hv_stores(hv, "hmac_ok", newSViv(hmac_ok)); |
| 865 |
1 |
| (void)hv_stores(hv, "runner_ok", newSViv(runner_ok)); |
| 866 |
1 |
| (void)hv_stores(hv, "edges_ok", newSViv(edges_ok)); |
| 867 |
1 |
| (void)hv_stores(hv, "digests", newRV_noinc((SV *)digests)); |
| 868 |
1 |
| RETVAL = newRV_noinc((SV *)hv); |
| 869 |
|
| } |
| 870 |
|
| OUTPUT: |
| 871 |
|
| RETVAL |
| 872 |
|
| |
| 873 |
|
| # ============================================================ |
| 874 |
|
| # Test helper: invoke the hash plugin's record_fn through File::Raw's |
| 875 |
|
| # dispatch_record entry point. Public name has a leading underscore to |
| 876 |
|
| # signal "not part of the supported API" - it exists so the test suite |
| 877 |
|
| # can exercise RECORD phase end-to-end before File::Raw exposes a |
| 878 |
|
| # user-facing per-record iterator. |
| 879 |
|
| # ============================================================ |
| 880 |
|
| |
| 881 |
|
| SV* |
| 882 |
|
| _test_record_one(record_sv, ...) |
| 883 |
|
| SV *record_sv |
| 884 |
|
| PREINIT: |
| 885 |
|
| HV *opts; |
| 886 |
|
| SV *result; |
| 887 |
|
| int i; |
| 888 |
|
| CODE: |
| 889 |
10 |
▶ | if ((items - 1) % 2 != 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((items - 1) % 2 != 0) |
0 |
10 |
10 |
|
| 890 |
0 |
| croak("File::Raw::Hash::_test_record_one: odd number of " |
| 891 |
|
| "key/value option args"); |
| 892 |
10 |
| opts = newHV(); |
| 893 |
|
| /* Default plugin to "hash" so the caller can omit it. */ |
| 894 |
10 |
| (void)hv_stores(opts, "plugin", newSVpvs("hash")); |
| 895 |
32 |
▶ | for (i = 1; i < items; i += 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 1; i < items; i += 2) { |
22 |
10 |
32 |
|
| 896 |
|
| STRLEN klen; |
| 897 |
22 |
| const char *kp = SvPV(ST(i), klen); |
| 898 |
22 |
| SV *vp = SvREFCNT_inc(ST(i + 1)); |
| 899 |
22 |
| (void)hv_store(opts, kp, (I32)klen, vp, 0); |
| 900 |
|
| } |
| 901 |
10 |
| result = file_plugin_dispatch_record(aTHX_ opts, NULL, record_sv); |
| 902 |
8 |
| SvREFCNT_dec((SV *)opts); |
| 903 |
8 |
▶ | if (!result) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!result) { |
0 |
8 |
8 |
|
| 904 |
0 |
| RETVAL = &PL_sv_undef; |
| 905 |
0 |
| SvREFCNT_inc(RETVAL); |
| 906 |
|
| } else { |
| 907 |
8 |
| RETVAL = result; |
| 908 |
|
| } |
| 909 |
|
| OUTPUT: |
| 910 |
|
| RETVAL |
| 911 |
|
| |
| 912 |
|
| SV * |
| 913 |
|
| _test_feed(algo_sv, data_sv, offset, ...) |
| 914 |
|
| SV *algo_sv |
| 915 |
|
| SV *data_sv |
| 916 |
|
| IV offset |
| 917 |
|
| PREINIT: |
| 918 |
80 |
| STRLEN nlen, len, done = 0; |
| 919 |
|
| const char *name, *data; |
| 920 |
|
| const frh_algo_t *a; |
| 921 |
|
| void *r; |
| 922 |
|
| unsigned char *mem, *buf, out[HASH_MAX_DIGEST_SIZE], *outs[1]; |
| 923 |
|
| char hex[HASH_MAX_DIGEST_SIZE * 2 + 1]; |
| 924 |
80 |
| int id, k = 3; |
| 925 |
|
| size_t j; |
| 926 |
|
| CODE: |
| 927 |
|
| /* For the oracle tests: digest data_sv through the C ABI's |
| 928 |
|
| * runner, from a buffer offset bytes past an aligned start, |
| 929 |
|
| * fed in the chunk sizes given after the offset (cycled; the |
| 930 |
|
| * rest in one call when none are given). Returns hex. */ |
| 931 |
80 |
| name = SvPV(algo_sv, nlen); |
| 932 |
80 |
| a = FRH_ABI.algo_by_name(name, nlen); |
| 933 |
80 |
▶ | if (!a) croak("File::Raw::Hash::_test_feed: unknown algorithm"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!a) croak("File::Raw::Hash::_test_feed: unknown algorithm"); |
0 |
80 |
80 |
|
| 934 |
80 |
▶ | if (offset < 0 || offset > 63) croak("File::Raw::Hash::_test_feed: offset 0-63"); |
Branch: 2 branches
| Branch | True | False | Total |
| if (offset < 0 || offset > 63) croak("File::Raw::Hash::_test_feed: offset 0-63"); |
80 |
0 |
80 |
| if (offset < 0 || offset > 63) croak("File::Raw::Hash::_test_feed: offset 0-63"); |
0 |
80 |
80 |
|
| 935 |
80 |
| data = SvPVbyte(data_sv, len); |
| 936 |
80 |
| Newx(mem, len + 64 + 1, unsigned char); |
| 937 |
80 |
| buf = mem + offset; |
| 938 |
80 |
▶ | if (len) Copy(data, buf, len, unsigned char); |
Branch: 1 branch
| Branch | True | False | Total |
| if (len) Copy(data, buf, len, unsigned char); |
76 |
4 |
80 |
|
| 939 |
80 |
| id = a->id; |
| 940 |
80 |
| r = FRH_ABI.new_runner(&id, 1, 0); |
| 941 |
80 |
▶ | if (!r) { Safefree(mem); croak("File::Raw::Hash::_test_feed: no runner"); } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!r) { Safefree(mem); croak("File::Raw::Hash::_test_feed: no runner"); } |
0 |
80 |
80 |
|
| 942 |
76881 |
▶ | while (done < len) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (done < len) { |
76801 |
80 |
76881 |
|
| 943 |
76801 |
| STRLEN take = len - done; |
| 944 |
76801 |
▶ | if (items > 3) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (items > 3) { |
76785 |
16 |
76801 |
|
| 945 |
76785 |
| IV want = SvIV(ST(k)); |
| 946 |
76785 |
▶ | if (++k >= items) k = 3; |
Branch: 1 branch
| Branch | True | False | Total |
| if (++k >= items) k = 3; |
14102 |
62683 |
76785 |
|
| 947 |
76785 |
▶ | if (want > 0 && (STRLEN)want < take) take = (STRLEN)want; |
Branch: 2 branches
| Branch | True | False | Total |
| if (want > 0 && (STRLEN)want < take) take = (STRLEN)want; |
76785 |
0 |
76785 |
| if (want > 0 && (STRLEN)want < take) take = (STRLEN)want; |
76725 |
60 |
76785 |
|
| 948 |
|
| } |
| 949 |
76801 |
| FRH_ABI.runner_update(r, buf + done, take); |
| 950 |
76801 |
| done += take; |
| 951 |
|
| } |
| 952 |
80 |
| outs[0] = out; |
| 953 |
80 |
▶ | if (FRH_ABI.runner_finish(r, outs) != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (FRH_ABI.runner_finish(r, outs) != 0) { |
0 |
80 |
80 |
|
| 954 |
0 |
| FRH_ABI.runner_free(r); |
| 955 |
0 |
| Safefree(mem); |
| 956 |
0 |
| croak("File::Raw::Hash::_test_feed: finish failed"); |
| 957 |
|
| } |
| 958 |
80 |
| FRH_ABI.runner_free(r); |
| 959 |
80 |
| Safefree(mem); |
| 960 |
2200 |
▶ | for (j = 0; j < a->digest_size; j++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < a->digest_size; j++) |
2120 |
80 |
2200 |
|
| 961 |
2120 |
| sprintf(hex + 2 * j, "%02x", out[j]); |
| 962 |
80 |
| RETVAL = newSVpvn(hex, a->digest_size * 2); |
| 963 |
|
| OUTPUT: |
| 964 |
|
| RETVAL |