srl_checksum_policy.h

filter: Statementstmt 29.1% 35 / 120 Branchbran 15.1% 29 / 192 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 20.5% 64 / 312 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« srl_encoder.c ↑ Summary srl_common.h »
1 #ifndef SRL_CHECKSUM_POLICY_H_
2 #define SRL_CHECKSUM_POLICY_H_
3  
4 #define SRL_CHECKSUM_POLICY_VERIFY_IF_AVAILABLE 0
5 #define SRL_CHECKSUM_POLICY_DIE 1
6 #define SRL_CHECKSUM_POLICY_IGNORE 2
7 #define SRL_CHECKSUM_POLICY_IGNORE_WARN 3
8 #define SRL_CHECKSUM_POLICY_REQUIRE 4
9 #define SRL_CHECKSUM_POLICY_REQUIRE_V2 5
10 #define SRL_CHECKSUM_POLICY_REQUIRE_V3 6
11  
12 #define SRL_CHECKSUM_MODE_UNSET -1
13 #define SRL_CHECKSUM_MODE_NONE 0
14 #define SRL_CHECKSUM_MODE_V1 1
15 #define SRL_CHECKSUM_MODE_V2 2
16 #define SRL_CHECKSUM_MODE_V3 3
17  
18 SRL_STATIC_INLINE U8
19 21 srl_checksum_required_mode(U8 policy)
20 {
21 return policy == SRL_CHECKSUM_POLICY_REQUIRE ? 1
22 21 ▶ : policy == SRL_CHECKSUM_POLICY_REQUIRE_V2 ? 2
Branch: 3 branches
BranchTrueFalseTotal
: policy == SRL_CHECKSUM_POLICY_REQUIRE_V2 ? 2 21 0 21
: policy == SRL_CHECKSUM_POLICY_REQUIRE_V2 ? 2 21 0 21
: policy == SRL_CHECKSUM_POLICY_REQUIRE_V2 ? 2 0 21 21
23 : policy == SRL_CHECKSUM_POLICY_REQUIRE_V3 ? 3 : 0;
24 }
25  
26 SRL_STATIC_INLINE int
27 0 srl_checksum_policy_is_strict(U8 policy)
28 {
29 0 ▶ return policy == SRL_CHECKSUM_POLICY_DIE || srl_checksum_required_mode(policy);
Branch: 2 branches
BranchTrueFalseTotal
return policy == SRL_CHECKSUM_POLICY_DIE || srl_checksum_required_mode(policy); 0 0 0
return policy == SRL_CHECKSUM_POLICY_DIE || srl_checksum_required_mode(policy); 0 0 0
30 }
31  
32 SRL_STATIC_INLINE const char *
33 0 srl_checksum_policy_name(U8 policy)
34 {
35 0 switch (policy) {
36 0 case SRL_CHECKSUM_POLICY_DIE: return "die";
37 0 case SRL_CHECKSUM_POLICY_REQUIRE: return "require_key";
38 0 case SRL_CHECKSUM_POLICY_REQUIRE_V2: return "require_v2";
39 0 case SRL_CHECKSUM_POLICY_REQUIRE_V3: return "require_v3";
40 0 case SRL_CHECKSUM_POLICY_IGNORE: return "ignore";
41 0 case SRL_CHECKSUM_POLICY_IGNORE_WARN: return "ignore-warn";
42 0 default: return "verify-if-available";
43 }
44 }
45  
46 SRL_STATIC_INLINE U8
47 0 srl_parse_checksum_policy(pTHX_ const char *policy, STRLEN len, const char *source)
48 {
49 0 ▶ if (len == sizeof("verify-if-available") - 1 && memEQ(policy, "verify-if-available", len))
Branch: 2 branches
BranchTrueFalseTotal
if (len == sizeof("verify-if-available") - 1 && memEQ(policy, "verify-if-available", len)) 0 0 0
if (len == sizeof("verify-if-available") - 1 && memEQ(policy, "verify-if-available", len)) 0 0 0
50 0 return SRL_CHECKSUM_POLICY_VERIFY_IF_AVAILABLE;
51 0 ▶ if ((len == sizeof("die") - 1 && memEQ(policy, "die", len))
Branch: 2 branches
BranchTrueFalseTotal
if ((len == sizeof("die") - 1 && memEQ(policy, "die", len)) 0 0 0
if ((len == sizeof("die") - 1 && memEQ(policy, "die", len)) 0 0 0
52 0 ▶ || (len == sizeof("allow_unsigned") - 1 && memEQ(policy, "allow_unsigned", len)))
Branch: 2 branches
BranchTrueFalseTotal
|| (len == sizeof("allow_unsigned") - 1 && memEQ(policy, "allow_unsigned", len))) 0 0 0
|| (len == sizeof("allow_unsigned") - 1 && memEQ(policy, "allow_unsigned", len))) 0 0 0
53 0 return SRL_CHECKSUM_POLICY_DIE;
54 0 ▶ if ((len == sizeof("require_key") - 1 && memEQ(policy, "require_key", len))
Branch: 2 branches
BranchTrueFalseTotal
if ((len == sizeof("require_key") - 1 && memEQ(policy, "require_key", len)) 0 0 0
if ((len == sizeof("require_key") - 1 && memEQ(policy, "require_key", len)) 0 0 0
55 0 ▶ || (len == sizeof("require") - 1 && memEQ(policy, "require", len))
Branch: 2 branches
BranchTrueFalseTotal
|| (len == sizeof("require") - 1 && memEQ(policy, "require", len)) 0 0 0
|| (len == sizeof("require") - 1 && memEQ(policy, "require", len)) 0 0 0
56 0 ▶ || (len == sizeof("require_v1") - 1 && memEQ(policy, "require_v1", len)))
Branch: 2 branches
BranchTrueFalseTotal
|| (len == sizeof("require_v1") - 1 && memEQ(policy, "require_v1", len))) 0 0 0
|| (len == sizeof("require_v1") - 1 && memEQ(policy, "require_v1", len))) 0 0 0
57 0 return SRL_CHECKSUM_POLICY_REQUIRE;
58 0 ▶ if (len == sizeof("require_v2") - 1 && memEQ(policy, "require_v2", len))
Branch: 2 branches
BranchTrueFalseTotal
if (len == sizeof("require_v2") - 1 && memEQ(policy, "require_v2", len)) 0 0 0
if (len == sizeof("require_v2") - 1 && memEQ(policy, "require_v2", len)) 0 0 0
59 0 return SRL_CHECKSUM_POLICY_REQUIRE_V2;
60 0 ▶ if (len == sizeof("require_v3") - 1 && memEQ(policy, "require_v3", len))
Branch: 2 branches
BranchTrueFalseTotal
if (len == sizeof("require_v3") - 1 && memEQ(policy, "require_v3", len)) 0 0 0
if (len == sizeof("require_v3") - 1 && memEQ(policy, "require_v3", len)) 0 0 0
61 0 return SRL_CHECKSUM_POLICY_REQUIRE_V3;
62 0 ▶ if (len == sizeof("ignore") - 1 && memEQ(policy, "ignore", len))
Branch: 2 branches
BranchTrueFalseTotal
if (len == sizeof("ignore") - 1 && memEQ(policy, "ignore", len)) 0 0 0
if (len == sizeof("ignore") - 1 && memEQ(policy, "ignore", len)) 0 0 0
63 0 return SRL_CHECKSUM_POLICY_IGNORE;
64 0 ▶ if (len == sizeof("ignore-warn") - 1 && memEQ(policy, "ignore-warn", len))
Branch: 2 branches
BranchTrueFalseTotal
if (len == sizeof("ignore-warn") - 1 && memEQ(policy, "ignore-warn", len)) 0 0 0
if (len == sizeof("ignore-warn") - 1 && memEQ(policy, "ignore-warn", len)) 0 0 0
65 0 return SRL_CHECKSUM_POLICY_IGNORE_WARN;
66 0 croak("Invalid %s; expected 'verify-if-available', 'die', 'allow_unsigned', 'require_key', 'require_v1', 'require_v2', 'require_v3', 'ignore', or 'ignore-warn'", source);
67 return SRL_CHECKSUM_POLICY_VERIFY_IF_AVAILABLE; /* not reached */
68 }
69  
70 SRL_STATIC_INLINE U8
71 7 srl_get_checksum_policy(pTHX_ SV *value, const char *module_policy_var)
72 {
73 STRLEN len;
74 const char *policy;
75  
76 7 ▶ if (value && SvGMAGICAL(value))
Branch: 2 branches
BranchTrueFalseTotal
if (value && SvGMAGICAL(value)) 0 7 7
if (value && SvGMAGICAL(value)) 0 0 0
77 0 SvGETMAGIC(value);
78 7 ▶ if (value && SvOK(value)) {
Branch: 2 branches
BranchTrueFalseTotal
if (value && SvOK(value)) { 0 7 7
if (value && SvOK(value)) { 0 0 0
79 0 policy = SvPVbyte(value, len);
80 0 return srl_parse_checksum_policy(aTHX_ policy, len, "checksum_policy");
81 }
82  
83 7 value = get_sv(module_policy_var, 0);
84 7 ▶ if (value && SvGMAGICAL(value))
Branch: 2 branches
BranchTrueFalseTotal
if (value && SvGMAGICAL(value)) 7 0 7
if (value && SvGMAGICAL(value)) 0 7 7
85 0 SvGETMAGIC(value);
86 7 ▶ if (value && SvOK(value)) {
Branch: 2 branches
BranchTrueFalseTotal
if (value && SvOK(value)) { 7 0 7
if (value && SvOK(value)) { 0 7 7
87 0 policy = SvPVbyte(value, len);
88 0 return srl_parse_checksum_policy(aTHX_ policy, len, module_policy_var);
89 }
90  
91 7 value = get_sv("Sereal::ChecksumPolicy", 0);
92 7 ▶ if (value && SvGMAGICAL(value))
Branch: 2 branches
BranchTrueFalseTotal
if (value && SvGMAGICAL(value)) 7 0 7
if (value && SvGMAGICAL(value)) 0 7 7
93 0 SvGETMAGIC(value);
94 7 ▶ if (value && SvOK(value)) {
Branch: 2 branches
BranchTrueFalseTotal
if (value && SvOK(value)) { 7 0 7
if (value && SvOK(value)) { 0 7 7
95 0 policy = SvPVbyte(value, len);
96 0 return srl_parse_checksum_policy(aTHX_ policy, len, "$Sereal::ChecksumPolicy");
97 }
98  
99 7 return SRL_CHECKSUM_POLICY_VERIFY_IF_AVAILABLE;
100 }
101  
102 /* -1 means unspecified. Configured values use 0 for none and 1..3 for
103 * wire checksum types 0..2. Accept words as the readable API form. */
104 SRL_STATIC_INLINE int
105 0 srl_checksum_mode_from_sv(pTHX_ SV *value, const char *option_name)
106 {
107 STRLEN len;
108 const char *mode;
109 0 ▶ if (value && SvGMAGICAL(value))
Branch: 2 branches
BranchTrueFalseTotal
if (value && SvGMAGICAL(value)) 0 0 0
if (value && SvGMAGICAL(value)) 0 0 0
110 0 SvGETMAGIC(value);
111 0 ▶ if (!value || !SvOK(value))
Branch: 2 branches
BranchTrueFalseTotal
if (!value || !SvOK(value)) 0 0 0
if (!value || !SvOK(value)) 0 0 0
112 0 croak("The '%s' option must be 'none', 'v1', 'v2', or 'v3'", option_name);
113 0 mode = SvPVbyte(value, len);
114 0 ▶ if (memEQs(mode, len, "none") || memEQs(mode, len, "0")) return 0;
Branch: 4 branches
BranchTrueFalseTotal
if (memEQs(mode, len, "none") || memEQs(mode, len, "0")) return 0; 0 0 0
if (memEQs(mode, len, "none") || memEQs(mode, len, "0")) return 0; 0 0 0
if (memEQs(mode, len, "none") || memEQs(mode, len, "0")) return 0; 0 0 0
if (memEQs(mode, len, "none") || memEQs(mode, len, "0")) return 0; 0 0 0
115 0 ▶ if (memEQs(mode, len, "v1") || memEQs(mode, len, "1")) return 1;
Branch: 4 branches
BranchTrueFalseTotal
if (memEQs(mode, len, "v1") || memEQs(mode, len, "1")) return 1; 0 0 0
if (memEQs(mode, len, "v1") || memEQs(mode, len, "1")) return 1; 0 0 0
if (memEQs(mode, len, "v1") || memEQs(mode, len, "1")) return 1; 0 0 0
if (memEQs(mode, len, "v1") || memEQs(mode, len, "1")) return 1; 0 0 0
116 0 ▶ if (memEQs(mode, len, "v2") || memEQs(mode, len, "2")) return 2;
Branch: 4 branches
BranchTrueFalseTotal
if (memEQs(mode, len, "v2") || memEQs(mode, len, "2")) return 2; 0 0 0
if (memEQs(mode, len, "v2") || memEQs(mode, len, "2")) return 2; 0 0 0
if (memEQs(mode, len, "v2") || memEQs(mode, len, "2")) return 2; 0 0 0
if (memEQs(mode, len, "v2") || memEQs(mode, len, "2")) return 2; 0 0 0
117 0 ▶ if (memEQs(mode, len, "v3") || memEQs(mode, len, "3")) return 3;
Branch: 4 branches
BranchTrueFalseTotal
if (memEQs(mode, len, "v3") || memEQs(mode, len, "3")) return 3; 0 0 0
if (memEQs(mode, len, "v3") || memEQs(mode, len, "3")) return 3; 0 0 0
if (memEQs(mode, len, "v3") || memEQs(mode, len, "3")) return 3; 0 0 0
if (memEQs(mode, len, "v3") || memEQs(mode, len, "3")) return 3; 0 0 0
118 0 croak("The '%s' option must be 0/'none', 1/'v1', 2/'v2', or 3/'v3'", option_name);
119 return -1;
120 }
121  
122 SRL_STATIC_INLINE int
123 7 srl_get_default_checksum_mode(pTHX_ const char *module_mode_var, int default_mode)
124 {
125 SV *value;
126  
127 7 value = get_sv(module_mode_var, 0);
128 7 ▶ if (value && SvGMAGICAL(value))
Branch: 2 branches
BranchTrueFalseTotal
if (value && SvGMAGICAL(value)) 7 0 7
if (value && SvGMAGICAL(value)) 0 7 7
129 0 SvGETMAGIC(value);
130 7 ▶ if (value && SvOK(value)) {
Branch: 2 branches
BranchTrueFalseTotal
if (value && SvOK(value)) { 7 0 7
if (value && SvOK(value)) { 0 7 7
131 0 return srl_checksum_mode_from_sv(aTHX_ value, module_mode_var);
132 }
133  
134 7 value = get_sv("Sereal::ChecksumMode", 0);
135 7 ▶ if (value && SvGMAGICAL(value))
Branch: 2 branches
BranchTrueFalseTotal
if (value && SvGMAGICAL(value)) 7 0 7
if (value && SvGMAGICAL(value)) 0 7 7
136 0 SvGETMAGIC(value);
137 7 ▶ if (value && SvOK(value)) {
Branch: 2 branches
BranchTrueFalseTotal
if (value && SvOK(value)) { 7 0 7
if (value && SvOK(value)) { 0 7 7
138 0 return srl_checksum_mode_from_sv(aTHX_ value, "$Sereal::ChecksumMode");
139 }
140 7 return default_mode;
141 }
142  
143 SRL_STATIC_INLINE SV *
144 7 srl_get_default_checksum_key(pTHX_ const char *module_key_var)
145 {
146 7 SV *value = get_sv(module_key_var, 0);
147 7 ▶ if (value && SvGMAGICAL(value))
Branch: 2 branches
BranchTrueFalseTotal
if (value && SvGMAGICAL(value)) 7 0 7
if (value && SvGMAGICAL(value)) 0 7 7
148 0 SvGETMAGIC(value);
149 7 ▶ if (value && SvOK(value))
Branch: 2 branches
BranchTrueFalseTotal
if (value && SvOK(value)) 7 0 7
if (value && SvOK(value)) 0 7 7
150 0 return value;
151 7 return get_sv("Sereal::DefaultKey", 0);
152 }
153  
154 SRL_STATIC_INLINE int
155 0 srl_checksum_keys_equal(pTHX_ SV *left, SV *right)
156 {
157 STRLEN left_len, right_len;
158 const char *left_bytes, *right_bytes;
159 0 ▶ if (left == right)
Branch: 1 branch
BranchTrueFalseTotal
if (left == right) 0 0 0
160 0 return 1;
161 0 ▶ if (!left || !right || !SvOK(left) || !SvOK(right))
Branch: 4 branches
BranchTrueFalseTotal
if (!left || !right || !SvOK(left) || !SvOK(right)) 0 0 0
if (!left || !right || !SvOK(left) || !SvOK(right)) 0 0 0
if (!left || !right || !SvOK(left) || !SvOK(right)) 0 0 0
if (!left || !right || !SvOK(left) || !SvOK(right)) 0 0 0
162 0 ▶ return (!left || !SvOK(left)) && (!right || !SvOK(right));
Branch: 4 branches
BranchTrueFalseTotal
return (!left || !SvOK(left)) && (!right || !SvOK(right)); 0 0 0
return (!left || !SvOK(left)) && (!right || !SvOK(right)); 0 0 0
return (!left || !SvOK(left)) && (!right || !SvOK(right)); 0 0 0
return (!left || !SvOK(left)) && (!right || !SvOK(right)); 0 0 0
163 0 ▶ left_bytes = SvPVbyte(left, left_len);
Branch: 1 branch
BranchTrueFalseTotal
left_bytes = SvPVbyte(left, left_len); 0 0 0
164 0 right_bytes = SvPVbyte(right, right_len);
165 0 ▶ return left_len == right_len && memEQ(left_bytes, right_bytes, left_len);
Branch: 2 branches
BranchTrueFalseTotal
return left_len == right_len && memEQ(left_bytes, right_bytes, left_len); 0 0 0
return left_len == right_len && memEQ(left_bytes, right_bytes, left_len); 0 0 0
166 }
167  
168 /* Generic options and versioned aliases assign the same two values. These
169 * setters resolve constructor duplicates before package defaults are read. */
170 SRL_STATIC_INLINE void
171 0 srl_set_checksum_key(pTHX_ SV **key, SV *value)
172 {
173 0 ▶ if (*key) {
Branch: 1 branch
BranchTrueFalseTotal
if (*key) { 0 0 0
174 0 ▶ if (!srl_checksum_keys_equal(aTHX_ *key, value))
Branch: 1 branch
BranchTrueFalseTotal
if (!srl_checksum_keys_equal(aTHX_ *key, value)) 0 0 0
175 0 croak("Different keys were supplied for checksum_key: conflicting checksum key options");
176 0 warn("Attempt to set checksum_key more than once");
177 }
178 0 *key = value;
179 0 }
180  
181 SRL_STATIC_INLINE const char *
182 0 srl_checksum_mode_name(int mode)
183 {
184 return mode == SRL_CHECKSUM_MODE_NONE ? "none"
185 0 ▶ : mode == SRL_CHECKSUM_MODE_V1 ? "v1"
Branch: 3 branches
BranchTrueFalseTotal
: mode == SRL_CHECKSUM_MODE_V1 ? "v1" 0 0 0
: mode == SRL_CHECKSUM_MODE_V1 ? "v1" 0 0 0
: mode == SRL_CHECKSUM_MODE_V1 ? "v1" 0 0 0
186 : mode == SRL_CHECKSUM_MODE_V2 ? "v2" : "v3";
187 }
188  
189 SRL_STATIC_INLINE void
190 0 srl_set_checksum_mode(pTHX_ int *mode, int value)
191 {
192 0 ▶ if (*mode != SRL_CHECKSUM_MODE_UNSET) {
Branch: 1 branch
BranchTrueFalseTotal
if (*mode != SRL_CHECKSUM_MODE_UNSET) { 0 0 0
193 0 int selected = *mode > value ? *mode : value;
194 0 warn("Attempt to set checksum_mode more than once (%s and %s); using %s",
195 srl_checksum_mode_name(*mode), srl_checksum_mode_name(value),
196 srl_checksum_mode_name(selected));
197 0 *mode = selected;
198 }
199 else {
200 0 *mode = value;
201 }
202 0 }
203  
204 SRL_STATIC_INLINE void
205 7 srl_validate_checksum_key(pTHX_ SV *value, const char *option_name, U8 policy)
206 {
207 7 ▶ if (value && SvOK(value)
Branch: 2 branches
BranchTrueFalseTotal
if (value && SvOK(value) 7 0 7
if (value && SvOK(value) 0 7 7
208 0 ▶ && srl_checksum_policy_is_strict(policy)) {
Branch: 1 branch
BranchTrueFalseTotal
&& srl_checksum_policy_is_strict(policy)) { 0 0 0
209 STRLEN len;
210 (void)SvPVbyte(value, len);
211 0 ▶ if (len != 16)
Branch: 1 branch
BranchTrueFalseTotal
if (len != 16) 0 0 0
212 0 croak("The '%s' key must be exactly 16 bytes with checksum_policy '%s'",
213 option_name, srl_checksum_policy_name(policy));
214 }
215 7 }
216  
217 SRL_STATIC_INLINE void
218 7 srl_validate_checksum_required_key(pTHX_ SV *key, U8 policy)
219 {
220 7 ▶ if (srl_checksum_required_mode(policy) && (!key || !SvOK(key)))
Branch: 3 branches
BranchTrueFalseTotal
if (srl_checksum_required_mode(policy) && (!key || !SvOK(key))) 0 7 7
if (srl_checksum_required_mode(policy) && (!key || !SvOK(key))) 0 0 0
if (srl_checksum_required_mode(policy) && (!key || !SvOK(key))) 0 0 0
221 0 croak("checksum_policy '%s' requires a checksum key", srl_checksum_policy_name(policy));
222 7 }
223  
224 SRL_STATIC_INLINE void
225 7 srl_validate_checksum_required_mode(pTHX_ int mode, U8 policy)
226 {
227 7 U8 minimum = srl_checksum_required_mode(policy);
228 7 ▶ if (minimum && mode < minimum)
Branch: 2 branches
BranchTrueFalseTotal
if (minimum && mode < minimum) 0 7 7
if (minimum && mode < minimum) 0 0 0
229 0 croak("checksum_policy '%s' requires checksum mode v%u or higher",
230 srl_checksum_policy_name(policy), (unsigned int)minimum);
231 7 }
232  
233 #endif
« srl_encoder.c ↑ Summary srl_common.h »