| 1 | package GDPR::IAB::TCFv2::BitField 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>; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 73 | ▶ | use Carp qw<croak>; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | 20952 | ▶ | sub Parse { | ||||||||||
|
Subroutine
Parse: called CC=8 SCAR=21.4
Pod
undocumented
| |||||||||||||
| 11 | 20952 | my ($klass, %args) = @_; | |||||||||||
| 12 | |||||||||||||
| 13 | 20952 | ▶ | croak "missing 'data'" unless defined $args{data}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 14 | 20952 | ▶ | croak "missing 'data_size'" unless defined $args{data_size}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 15 | 20952 | ▶ | croak "missing 'max_id'" unless defined $args{max_id}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 16 | |||||||||||||
| 17 | 20952 | ▶ | croak "missing 'options'" unless defined $args{options}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 18 | 20952 | ▶ | croak "missing 'options.json'" unless defined $args{options}->{json}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 19 | |||||||||||||
| 20 | 20952 | my $data = $args{data}; | |||||||||||
| 21 | 20952 | my $data_size = $args{data_size}; | |||||||||||
| 22 | 20952 | ▶ | my $offset = $args{offset} // 0; | ||||||||||
|
Condition50%1/2
$args{"offset"} // 0
MC/DC0%0/1
$args{"offset"} // 0
$args{"offset"}
Truth table50%1/2
A
A= $args{"offset"}
| |||||||||||||
| 23 | 20952 | my $max_id = $args{max_id}; | |||||||||||
| 24 | 20952 | my $options = $args{options}; | |||||||||||
| 25 | |||||||||||||
| 26 | 20952 | ▶ | croak | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 27 | 2 | "a BitField for $max_id bits requires a consent string of at least @{[ $offset + $max_id ]} bits. This consent string only had @{[ $data_size << 3 ]} bits" | |||||||||||
| 28 | if $offset + $max_id > ($data_size << 3); | ||||||||||||
| 29 | |||||||||||||
| 30 | 20950 | my $self = {data => $data, offset => $offset, max_id => $max_id, options => $options,}; | |||||||||||
| 31 | |||||||||||||
| 32 | 20950 | bless $self, $klass; | |||||||||||
| 33 | |||||||||||||
| 34 | 20950 | return ($self, $offset + $max_id); | |||||||||||
| 35 | } | ||||||||||||
| 36 | |||||||||||||
| 37 | 5 | ▶ | sub max_id { | ||||||||||
|
Subroutine
max_id: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 38 | 5 | my $self = shift; | |||||||||||
| 39 | |||||||||||||
| 40 | 5 | return $self->{max_id}; | |||||||||||
| 41 | } | ||||||||||||
| 42 | |||||||||||||
| 43 | 16780 | ▶ | sub contains { | ||||||||||
|
Subroutine
contains: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 44 | 16780 | my ($self, $id) = @_; | |||||||||||
| 45 | |||||||||||||
| 46 | 16780 | ▶ | croak "invalid vendor id $id: must be positive integer bigger than 0" if $id < 1; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 47 | |||||||||||||
| 48 | 16780 | ▶ | return if $id > $self->{max_id}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 49 | |||||||||||||
| 50 | 14805 | return is_set($self->{data}, $self->{offset} + $id - 1); | |||||||||||
| 51 | } | ||||||||||||
| 52 | |||||||||||||
| 53 | 30 | ▶ | sub all { | ||||||||||
|
Subroutine
all: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 54 | 30 | my $self = shift; | |||||||||||
| 55 | |||||||||||||
| 56 | 30 | my $bitstring = unpack("B*", substr($self->{data}, $self->{offset} >> 3)); | |||||||||||
| 57 | 30 | $bitstring = substr($bitstring, $self->{offset} & 7, $self->{max_id}); | |||||||||||
| 58 | |||||||||||||
| 59 | 30 | my @set_bits; | |||||||||||
| 60 | 30 | ▶ | while ($bitstring =~ /1/g) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 61 | 732 | push @set_bits, pos($bitstring); | |||||||||||
| 62 | } | ||||||||||||
| 63 | |||||||||||||
| 64 | 30 | return \@set_bits; | |||||||||||
| 65 | } | ||||||||||||
| 66 | |||||||||||||
| 67 | 1949 | ▶ | sub TO_JSON { | ||||||||||
|
Subroutine
TO_JSON: called CC=11 SCAR=24.2
Pod
documented
| |||||||||||||
| 68 | 1949 | my ($self, $filter_id) = @_; | |||||||||||
| 69 | |||||||||||||
| 70 | 1949 | ▶ | if (defined $filter_id) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 71 | 20 | my $val = $self->contains($filter_id); | |||||||||||
| 72 | |||||||||||||
| 73 | 20 | ▶ | if (!!$self->{options}->{json}->{compact}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 74 | 0 | ▶ | return $val ? [$filter_id] : []; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 75 | } | ||||||||||||
| 76 | |||||||||||||
| 77 | 20 | my ($false, $true) = @{$self->{options}->{json}->{boolean_values}}; | |||||||||||
| 78 | 20 | ▶ | my $bool_val = $val ? $true : $false; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 79 | |||||||||||||
| 80 | 20 | ▶ | if (!!$self->{options}->{json}->{verbose}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 81 | 0 | return {$filter_id => $bool_val}; | |||||||||||
| 82 | } | ||||||||||||
| 83 | |||||||||||||
| 84 | 20 | ▶ | return $val ? {$filter_id => $true} : {}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 85 | } | ||||||||||||
| 86 | |||||||||||||
| 87 | 1929 | ▶ | if (!!$self->{options}->{json}->{compact}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 88 | 30 | return $self->all; | |||||||||||
| 89 | } | ||||||||||||
| 90 | |||||||||||||
| 91 | 1899 | my $bitstring = unpack("B*", substr($self->{data}, $self->{offset} >> 3)); | |||||||||||
| 92 | 1899 | $bitstring = substr($bitstring, $self->{offset} & 7, $self->{max_id}); | |||||||||||
| 93 | |||||||||||||
| 94 | 1899 | my ($false, $true) = @{$self->{options}->{json}->{boolean_values}}; | |||||||||||
| 95 | |||||||||||||
| 96 | 1899 | ▶ | if (!!$self->{options}->{json}->{verbose}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 97 | 2 | my %map = map { $_ => $false } 1 .. $self->{max_id}; | |||||||||||
| 98 | 2 | ▶ | while ($bitstring =~ /1/g) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 99 | 68 | $map{pos($bitstring)} = $true; | |||||||||||
| 100 | } | ||||||||||||
| 101 | 2 | return \%map; | |||||||||||
| 102 | } | ||||||||||||
| 103 | |||||||||||||
| 104 | 1897 | my %map; | |||||||||||
| 105 | 1897 | ▶ | while ($bitstring =~ /1/g) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 106 | 978402 | $map{pos($bitstring)} = $true; | |||||||||||
| 107 | } | ||||||||||||
| 108 | 1897 | return \%map; | |||||||||||
| 109 | } | ||||||||||||
| 110 | |||||||||||||
| 111 | 73 | 1; | |||||||||||
| 112 | __END__ | ||||||||||||