| 1 | package GDPR::IAB::TCFv2::RangeSection 0.530; | ||||||||||||||||||||||||||||
| 2 | 73 | ▶ | use v5.12; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 73 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 73 | ▶ | use integer; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 73 | ▶ | use bytes; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 73 | ▶ | use GDPR::IAB::TCFv2::BitUtils qw<is_set get_uint12 get_uint16>; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 73 | ▶ | use Carp qw<croak>; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 26475 | ▶ | sub Parse { | ||||||||||||||||||||||||||
|
Subroutine
Parse: called CC=14 SCAR=26.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 12 | 26475 | my ($klass, %args) = @_; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 26475 | ▶ | croak "missing 'data'" unless defined $args{data}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 15 | 26475 | ▶ | croak "missing 'data_size'" unless defined $args{data_size}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 16 | 26475 | ▶ | croak "missing 'offset'" unless defined $args{offset}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 17 | 26475 | ▶ | croak "missing 'max_id'" unless defined $args{max_id}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 26475 | ▶ | croak "missing 'options'" unless defined $args{options}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 20 | 26475 | ▶ | croak "missing 'options.json'" unless defined $args{options}->{json}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 26475 | my $data = $args{data}; | |||||||||||||||||||||||||||
| 23 | 26475 | my $data_size = $args{data_size}; | |||||||||||||||||||||||||||
| 24 | 26475 | my $offset = $args{offset}; | |||||||||||||||||||||||||||
| 25 | 26475 | my $max_id = $args{max_id}; | |||||||||||||||||||||||||||
| 26 | 26475 | my $options = $args{options}; | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 26475 | my %prefetch; | |||||||||||||||||||||||||||
| 29 | 26475 | my $cache = ""; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 26475 | ▶ | if (exists $options->{prefetch}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | 26475 | my $self = {ranges => [], cache => $cache, max_id => $max_id, options => $options, offset => $offset,}; | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 26475 | bless $self, $klass; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 26475 | 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 | 26475 | ▶ | if (scalar @{$self->{ranges}} > 10) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | 19709 | my %hcache; | |||||||||||||||||||||||||||
| 57 | 19709 | foreach my $vendor_id (keys %prefetch) { | |||||||||||||||||||||||||||
| 58 | 18 | ▶ | $hcache{$vendor_id} = $prefetch{$vendor_id} == 2 ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | 19709 | $self->{hcache} = \%hcache; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 26475 | return ($self, $next_offset); | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 26475 | ▶ | sub _parse { | ||||||||||||||||||||||||||
|
Subroutine
_parse: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 67 | 26475 | my ($self, $data, $data_size, $offset, $prefetch) = @_; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 26475 | my ($num_entries, $next_offset) = get_uint12($data, $offset); | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 26475 | foreach my $i (1 .. $num_entries) { | |||||||||||||||||||||||||||
| 72 | 391634 | $next_offset = $self->_parse_range($data, $data_size, $next_offset, $prefetch,); | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 26475 | return $next_offset; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 391634 | ▶ | sub _parse_range { | ||||||||||||||||||||||||||
|
Subroutine
_parse_range: called CC=12 SCAR=25.9
| |||||||||||||||||||||||||||||
| 79 | 391634 | my ($self, $data, $data_size, $offset, $prefetch) = @_; | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 391634 | ▶ | croak "bit $offset was supposed to start a new range entry, but the consent string was only $data_size bytes long" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | if ($offset >> 3) >= $data_size; | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 391634 | my $max_id = $self->{max_id}; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | # If the first bit is set, it's a Range of IDs | ||||||||||||||||||||||||||||
| 87 | 391634 | my ($is_range, $next_offset) = is_set $data, $offset; | |||||||||||||||||||||||||||
| 88 | 391634 | ▶ | if ($is_range) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 59244 | my ($start, $end); | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 59244 | ($start, $next_offset) = get_uint16($data, $next_offset); | |||||||||||||||||||||||||||
| 92 | 59244 | ($end, $next_offset) = get_uint16($data, $next_offset); | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 59244 | ▶ | croak "bit $offset range entry starts at $start, but the min vendor ID is 1" if 1 > $start; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 59244 | ▶ | croak "bit $offset range entry ends at $end, but the max vendor ID is $max_id" if $end > $max_id; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 59244 | ▶ | croak "start $start can't be bigger than end $end" if $start > $end; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 59244 | push @{$self->{ranges}}, [$start, $end]; | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 59244 | foreach my $vendor_id (keys %{$prefetch}) { | |||||||||||||||||||||||||||
| 103 | 18 | ▶ | if ($start <= $vendor_id && $vendor_id <= $end) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$start <= $vendor_id and $vendor_id <= $end
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
| |||||||||||||||||||||||||||||
| 104 | 0 | $prefetch->{$vendor_id} = 2; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 59244 | return $next_offset; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 332390 | my $vendor_id; | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 332390 | ($vendor_id, $next_offset) = get_uint16($data, $next_offset); | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 332390 | ▶ | croak "bit $offset range entry vendor $vendor_id, but only vendors [1, $max_id] are valid" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
1 > $vendor_id or $vendor_id > $max_id
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
| |||||||||||||||||||||||||||||
| 116 | if 1 > $vendor_id || $vendor_id > $max_id; | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 332390 | push @{$self->{ranges}}, [$vendor_id, $vendor_id]; | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 332390 | ▶ | if (exists $prefetch->{$vendor_id}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 3 | $prefetch->{$vendor_id} = 2; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 332390 | return $next_offset; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 2 | ▶ | sub max_id { | ||||||||||||||||||||||||||
|
Subroutine
max_id: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 128 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 2 | return $self->{max_id}; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 4196 | ▶ | sub contains { | ||||||||||||||||||||||||||
|
Subroutine
contains: called CC=10 SCAR=23.1
Pod
documented
| |||||||||||||||||||||||||||||
| 134 | 4196 | my ($self, $id) = @_; | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 4196 | ▶ | croak "invalid vendor id $id: must be positive integer bigger than 0" if $id < 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 4196 | ▶ | if ($self->{cache}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | 668 | ▶ | return if $id > $self->{max_id}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | 473 | my $vec_offset = (($id - 1) >> 3 << 3) | (7 - (($id - 1) & 7)); | |||||||||||||||||||||||||||
| 141 | 473 | return vec($self->{cache}, $vec_offset, 1); | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 3528 | ▶ | if ($self->{hcache} && exists $self->{hcache}->{$id}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->{"hcache"} and exists $self->{"hcache"}{$id}
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}
| |||||||||||||||||||||||||||||
| 145 | 18 | return $self->{hcache}->{$id}; | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 3510 | ▶ | return if $id > $self->{max_id}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 3482 | foreach my $range (@{$self->{ranges}}) { | |||||||||||||||||||||||||||
| 151 | 16266 | ▶ | return 1 if $range->[0] <= $id && $id <= $range->[1]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$range->[0] <= $id and $id <= $range->[1]
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]
| |||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 2671 | return 0; | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 2186 | ▶ | sub all { | ||||||||||||||||||||||||||
|
Subroutine
all: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 158 | 2186 | my $self = shift; | |||||||||||||||||||||||||||
| 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 | 236 | ▶ | sub TO_JSON { | ||||||||||||||||||||||||||
|
Subroutine
TO_JSON: called CC=11 SCAR=33.1
Pod
documented
| |||||||||||||||||||||||||||||
| 169 | 236 | my ($self, $filter_id) = @_; | |||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 236 | ▶ | if (defined $filter_id) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | 0 | my $val = $self->contains($filter_id); | |||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 0 | ▶ | if (!!$self->{options}->{json}->{compact}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | 0 | ▶ | return $val ? [$filter_id] : []; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 0 | my ($false, $true) = @{$self->{options}->{json}->{boolean_values}}; | |||||||||||||||||||||||||||
| 179 | 0 | ▶ | my $bool_val = $val ? $true : $false; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 0 | ▶ | if (!!$self->{options}->{json}->{verbose}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 182 | 0 | return {$filter_id => $bool_val}; | |||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 0 | ▶ | return $val ? {$filter_id => $true} : {}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 236 | ▶ | return $self->all if !!$self->{options}->{json}->{compact}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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 | 73 | 1; | |||||||||||||||||||||||||||
| 211 | __END__ | ||||||||||||||||||||||||||||