blib/lib/GDPR/IAB/TCFv2/RangeSection.pm

filter: Statementstmt 92.1% 118 / 128 Branchbran 61.2% 38 / 62 Conditioncond 66.6% 8 / 12 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 100% 13 / 13 Podpod 80.0% 4 / 5 totaltotal 80.7% 184 / 228 CC 46 SCAR 25.9
CC
46
Cov
81%
TO_JSON
27.3
Parse
14.3
_parse_range
13.3
CRAP 13.4
« blib/lib/GDPR/IAB/TCFv2/CMPValidator.pm ↑ Summary bin/iabtcfv2 »
1 package GDPR::IAB::TCFv2::RangeSection 0.512;
2 72 ▶use v5.12;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 72 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 72 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 72 ▶use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 72 ▶use GDPR::IAB::TCFv2::BitUtils qw<is_set get_uint12 get_uint16>;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 72 ▶use Carp qw<croak>;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10  
11 sub Parse {
12 26477 ▶ my ($klass, %args) = @_;
Subroutine Parse: called CC=14 SCAR=26.6
Pod undocumented
13  
14 26477 ▶ croak "missing 'data'" unless defined $args{data};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"data"} 0 26477 26477
15 26477 ▶ croak "missing 'data_size'" unless defined $args{data_size};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"data_size"} 0 26477 26477
16 26477 ▶ croak "missing 'offset'" unless defined $args{offset};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"offset"} 0 26477 26477
17 26477 ▶ croak "missing 'max_id'" unless defined $args{max_id};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"max_id"} 0 26477 26477
18  
19 26477 ▶ croak "missing 'options'" unless defined $args{options};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"options"} 0 26477 26477
20 26477 ▶ croak "missing 'options.json'" unless defined $args{options}->{json};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"options"}{"json"} 0 26477 26477
21  
22 26477 my $data = $args{data};
23 26477 my $data_size = $args{data_size};
24 26477 my $offset = $args{offset};
25 26477 my $max_id = $args{max_id};
26 26477 my $options = $args{options};
27  
28 26477 my %prefetch;
29 26477 my $cache = "";
30  
31 26477 ▶ if (exists $options->{prefetch}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options->{"prefetch"}) 2 26475 26477
32 2 my $vendor_ids = $options->{prefetch};
33 2 foreach my $vendor_id (@{$vendor_ids}) {
34 18 $prefetch{$vendor_id} = 1;
35 }
36 }
37  
38 26477 my $self = {ranges => [], cache => $cache, max_id => $max_id, options => $options, offset => $offset,};
39  
40 26477 bless $self, $klass;
41  
42 26477 my $next_offset = $self->_parse($data, $data_size, $offset, \%prefetch);
43  
44 # If many ranges, build a bit-vector cache for O(1) lookups
45 26477 ▶ if (scalar @{$self->{ranges}} > 10) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$self->{'ranges'};} > 10) { } 6766 19711 26477
46 6766 $self->{cache} = "\0" x (($max_id >> 3) + 1);
47 6766 foreach my $range (@{$self->{ranges}}) {
48 364524 for my $id ($range->[0] .. $range->[1]) {
49 462854 my $vec_offset = (($id - 1) >> 3 << 3) | (7 - (($id - 1) & 7));
50 462854 vec($self->{cache}, $vec_offset, 1) = 1;
51 }
52 }
53 }
54 else {
55 # Small number of ranges: pre-populate hash cache from prefetch
56 19711 my %hcache;
57 19711 foreach my $vendor_id (keys %prefetch) {
58 18 ▶ $hcache{$vendor_id} = $prefetch{$vendor_id} == 2 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$prefetch{$vendor_id} == 2 ? : 3 15 18
59 }
60 19711 $self->{hcache} = \%hcache;
61 }
62  
63 26477 return ($self, $next_offset);
64 }
65  
66 sub _parse {
67 26477 ▶ my ($self, $data, $data_size, $offset, $prefetch) = @_;
Subroutine _parse: called CC=2 SCAR=6.9
68  
69 26477 my ($num_entries, $next_offset) = get_uint12($data, $offset);
70  
71 26477 foreach my $i (1 .. $num_entries) {
72 391636 $next_offset = $self->_parse_range($data, $data_size, $next_offset, $prefetch,);
73 }
74  
75 26477 return $next_offset;
76 }
77  
78 sub _parse_range {
79 391636 ▶ my ($self, $data, $data_size, $offset, $prefetch) = @_;
Subroutine _parse_range: called CC=12 SCAR=25.9
80  
81 391636 ▶ croak "bit $offset was supposed to start a new range entry, but the consent string was only $data_size bytes long"
Branch: 1 branch
BranchTrueFalseTotal
if $offset >> 3 >= $data_size 0 391636 391636
82 if ($offset >> 3) >= $data_size;
83  
84 391636 my $max_id = $self->{max_id};
85  
86 # If the first bit is set, it's a Range of IDs
87 391636 my ($is_range, $next_offset) = is_set $data, $offset;
88 391636 ▶ if ($is_range) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_range) 59241 332395 391636
89 59241 my ($start, $end);
90  
91 59241 ($start, $next_offset) = get_uint16($data, $next_offset);
92 59241 ($end, $next_offset) = get_uint16($data, $next_offset);
93  
94 59241 ▶ croak "bit $offset range entry starts at $start, but the min vendor ID is 1" if 1 > $start;
Branch: 1 branch
BranchTrueFalseTotal
if 1 > $start 0 59241 59241
95  
96 59241 ▶ croak "bit $offset range entry ends at $end, but the max vendor ID is $max_id" if $end > $max_id;
Branch: 1 branch
BranchTrueFalseTotal
if $end > $max_id 0 59241 59241
97  
98 59241 ▶ croak "start $start can't be bigger than end $end" if $start > $end;
Branch: 1 branch
BranchTrueFalseTotal
if $start > $end 0 59241 59241
99  
100 59241 push @{$self->{ranges}}, [$start, $end];
101  
102 59241 foreach my $vendor_id (keys %{$prefetch}) {
103 18 ▶ if ($start <= $vendor_id && $vendor_id <= $end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start <= $vendor_id and $vendor_id <= $end) 0 18 18
Condition66%2/3
$start <= $vendor_id and $vendor_id <= $end
!ll&&!rl&&r
1260
MC/DC0%0/2
$start <= $vendor_id and $vendor_id <= $end
$start <= $vendor_id $vendor_id <= $end
Truth table66%2/3
A and B
A= $start <= $vendor_idB= $vendor_id <= $end
ABresult
0X0
100
111
104 0 $prefetch->{$vendor_id} = 2;
105 }
106 }
107  
108 59241 return $next_offset;
109 }
110  
111 332395 my $vendor_id;
112  
113 332395 ($vendor_id, $next_offset) = get_uint16($data, $next_offset);
114  
115 332395 ▶ croak "bit $offset range entry vendor $vendor_id, but only vendors [1, $max_id] are valid"
Branch: 1 branch
BranchTrueFalseTotal
if 1 > $vendor_id or $vendor_id > $max_id 0 332395 332395
Condition33%1/3
1 > $vendor_id or $vendor_id > $max_id
l!l&&r!l&&!r
00332395
MC/DC0%0/2
1 > $vendor_id or $vendor_id > $max_id
1 > $vendor_id $vendor_id > $max_id
Truth table33%1/3
A or B
A= 1 > $vendor_idB= $vendor_id > $max_id
ABresult
1X1
011
000
116 if 1 > $vendor_id || $vendor_id > $max_id;
117  
118 332395 push @{$self->{ranges}}, [$vendor_id, $vendor_id];
119  
120 332395 ▶ if (exists $prefetch->{$vendor_id}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $prefetch->{$vendor_id}) 3 332392 332395
121 3 $prefetch->{$vendor_id} = 2;
122 }
123  
124 332395 return $next_offset;
125 }
126  
127 sub max_id {
128 2 ▶ my $self = shift;
Subroutine max_id: called CC=1 SCAR=0.0
Pod documented
129  
130 2 return $self->{max_id};
131 }
132  
133 sub contains {
134 3891 ▶ my ($self, $id) = @_;
Subroutine contains: called CC=10 SCAR=23.1
Pod documented
135  
136 3891 ▶ croak "invalid vendor id $id: must be positive integer bigger than 0" if $id < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $id < 1 0 3891 3891
137  
138 3891 ▶ if ($self->{cache}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"cache"}) 396 3495 3891
139 396 ▶ return if $id > $self->{max_id};
Branch: 1 branch
BranchTrueFalseTotal
if $id > $self->{"max_id"} 8 388 396
140 388 my $vec_offset = (($id - 1) >> 3 << 3) | (7 - (($id - 1) & 7));
141 388 return vec($self->{cache}, $vec_offset, 1);
142 }
143  
144 3495 ▶ if ($self->{hcache} && exists $self->{hcache}->{$id}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"hcache"} and exists $self->{"hcache"}{$id}) 18 3477 3495
Condition66%2/3
$self->{"hcache"} and exists $self->{"hcache"}{$id}
!ll&&!rl&&r
0347718
MC/DC50%1/2
$self->{"hcache"} and exists $self->{"hcache"}{$id}
$self->{"hcache"} exists $self->{"hcache"}{$id}
Truth table66%2/3
A and B
A= $self->{"hcache"}B= exists $self->{"hcache"}{$id}
ABresult
0X0
100
111
145 18 return $self->{hcache}->{$id};
146 }
147  
148 3477 ▶ return if $id > $self->{max_id};
Branch: 1 branch
BranchTrueFalseTotal
if $id > $self->{"max_id"} 1 3476 3477
149  
150 3476 foreach my $range (@{$self->{ranges}}) {
151 16121 ▶ return 1 if $range->[0] <= $id && $id <= $range->[1];
Branch: 1 branch
BranchTrueFalseTotal
if $range->[0] <= $id and $id <= $range->[1] 815 15306 16121
Condition100%3/3
$range->[0] <= $id and $id <= $range->[1]
!ll&&!rl&&r
80867220815
MC/DC100%2/2
$range->[0] <= $id and $id <= $range->[1]
$range->[0] <= $id $id <= $range->[1]
Truth table100%3/3
A and B
A= $range->[0] <= $idB= $id <= $range->[1]
ABresult
0X0
100
111
152 }
153  
154 2661 return 0;
155 }
156  
157 sub all {
158 2186 ▶ my $self = shift;
Subroutine all: called CC=2 SCAR=6.9
Pod documented
159  
160 2186 my @vendors;
161 2186 foreach my $range (@{$self->{ranges}}) {
162 27635 push @vendors, $range->[0] .. $range->[1];
163 }
164  
165 2186 return \@vendors;
166 }
167  
168 sub TO_JSON {
169 236 ▶ my ($self, $filter_id) = @_;
Subroutine TO_JSON: called CC=11 SCAR=33.1
Pod documented
170  
171 236 ▶ if (defined $filter_id) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filter_id) 0 236 236
172 0 my $val = $self->contains($filter_id);
173  
174 0 ▶ if (!!$self->{options}->{json}->{compact}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (not $self->{"options"}{"json"}{"compact"}) 0 0 0
175 0 ▶ return $val ? [$filter_id] : [];
Branch: 1 branch
BranchTrueFalseTotal
$val ? : 0 0 0
176 }
177  
178 0 my ($false, $true) = @{$self->{options}->{json}->{boolean_values}};
179 0 ▶ my $bool_val = $val ? $true : $false;
Branch: 1 branch
BranchTrueFalseTotal
$val ? : 0 0 0
180  
181 0 ▶ if (!!$self->{options}->{json}->{verbose}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (not $self->{"options"}{"json"}{"verbose"}) 0 0 0
182 0 return {$filter_id => $bool_val};
183 }
184  
185 0 ▶ return $val ? {$filter_id => $true} : {};
Branch: 1 branch
BranchTrueFalseTotal
$val ? : 0 0 0
186 }
187  
188 236 ▶ return $self->all if !!$self->{options}->{json}->{compact};
Branch: 1 branch
BranchTrueFalseTotal
unless not $self->{"options"}{"json"}{"compact"} 8 228 236
189  
190 228 my ($false, $true) = @{$self->{options}->{json}->{boolean_values}};
191  
192 228 my %map;
193 228 ▶ if (!!$self->{options}->{json}->{verbose}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (not $self->{"options"}{"json"}{"verbose"}) 2 226 228
194 2 %map = map { $_ => $false } 1 .. $self->{max_id};
195 }
196  
197 # Direct hash-element assignment per range; the previous form
198 # `%map = ( %map, map { $_ => $true } ... )` re-copied the running
199 # hash into a list every iteration, accidentally O(n^2) over the
200 # number of vendor IDs already accumulated.
201 228 foreach my $range (@{$self->{ranges}}) {
202 8010 for my $id ($range->[0] .. $range->[1]) {
203 8573 $map{$id} = $true;
204 }
205 }
206  
207 228 return \%map;
208 }
209  
210 72 1;
211 __END__
212  
213 =head1 NAME
214  
215 GDPR::IAB::TCFv2::RangeSection - TCF v2.3 range section parser
216  
217 =head1 SYNOPSIS
218  
219 use GDPR::IAB::TCFv2::RangeSection;
220 my $data = '...'; # raw binary data
221
222 my $max_id_consent = 100;
223  
224 my ($range_section, $next_offset) = GDPR::IAB::TCFv2::RangeSection->Parse(
225 data => $data,
226 data_size => length($data),
227 offset => 230, # offset for vendor ranges
228 max_id => $max_id_consent,
229 options => { json => {} },
230 );
231  
232 if ($range_section->contains(284)) {
233 # ...
234 }
235  
236 =head1 CONSTRUCTOR
237  
238 Constructor C<Parse> receives a hash of 6 parameters:
239  
240 =over
241  
242 =item *
243  
244 Key C<data> is the binary data
245  
246 =item *
247  
248 Key C<data_size> is the original binary data size (in bits)
249  
250 =item *
251  
252 Key C<offset> is the binary data offset. Can be 0.
253  
254 =item *
255  
256 Key C<max_id> is the max id (used to validate the ranges if all data is between 1 and C<max_id>)
257  
258 =item *
259  
260 Key C<options> is the L<GDPR::IAB::TCFv2> options (includes the C<json> field to modify the L</TO_JSON> method output.
261  
262 =item *
263  
264 Key C<prefetch> is an arrayref of IDs to pre-calculate bit vector presence. Optional.
265  
266 =back
267  
268 Will die if any parameter is missing.
269  
270 Will die if data does not contain all bits required.
271  
272 Will die if the range sections are malformed.
273  
274 Will return an array of two elements: the object itself and the next offset.
275  
276 =head1 METHODS
277  
278 =head2 contains
279  
280 Return the vendor id bit status (if enable or not) from one of the range sections.
281  
282 Will return false if id is bigger than max vendor id.
283  
284 my $ok = $range_section->contains(284);
285  
286 =head2 max_id
287  
288 Returns the max vendor id.
289  
290 =head2 all
291  
292 Returns an arrayref of all vendors mapped with the bit enabled.
293  
294 =head2 TO_JSON
295  
296 By default it returns an hashref mapping id to a boolean, that represent if the id is present or not in all ranges sections.
297  
298 The json option C<verbose> controls if all ids between 1 to L</max_id> will be present on the C<json> or only the ones that are true.
299  
300 The json option C<compact> change the response, will return an arrayref of all ids present on the ranges section.
« blib/lib/GDPR/IAB/TCFv2/CMPValidator.pm ↑ Summary bin/iabtcfv2 »