Skip to content

Commit

Permalink
add initial code
Browse files Browse the repository at this point in the history
  • Loading branch information
peczenyj committed Dec 16, 2023
1 parent f63f7b4 commit a166b2f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/GDPR/IAB/TCFv2.pm
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ sub Parse {

$options{json}->{date_format} ||= DATE_FORMAT_ISO_8601;
$options{json}->{boolean_values} ||= [ _json_false(), _json_true() ];
$options{prefetch} = $opts{prefetch} if exists $opts{prefetch};

my $self = {
core_data => $segments->{core_data},
Expand Down
22 changes: 21 additions & 1 deletion lib/GDPR/IAB/TCFv2/RangeSection.pm
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ sub Parse {
"a BitField for vendor consent strings using RangeSections require at least 31 bytes. Got $data_size"
if $data_size < 31;

my %cache;

if ( exists $options->{prefetch} ) {
my $vendor_id = $options->{prefetch};

$cache{$vendor_id} = 0;
}

my ( $num_entries, $next_offset ) = get_uint12( $data, $offset );

my @ranges;
Expand All @@ -41,6 +49,7 @@ sub Parse {
$next_offset,
$max_id,
$options,
\%cache,
);

push @ranges, $range;
Expand All @@ -50,6 +59,7 @@ sub Parse {
ranges => \@ranges,
max_id => $max_id,
options => $options,
cache => \%cache,
};

bless $self, $klass;
Expand All @@ -58,7 +68,7 @@ sub Parse {
}

sub _parse_range {
my ( $data, $data_size, $offset, $max_id, $options ) = @_;
my ( $data, $data_size, $offset, $max_id, $options, $cache ) = @_;

croak
"bit $offset was suppose to start a new range entry, but the consent string was only $data_size bytes long"
Expand All @@ -82,6 +92,10 @@ sub _parse_range {

croak "start $start can't be bigger than end $end" if $start > $end;

foreach my $id ( keys %{$cache} ) {
$cache->{$id} = 1 if $start <= $id && $id <= $end;
}

return [ $start, $end ],
$next_offset;
}
Expand All @@ -94,6 +108,10 @@ sub _parse_range {
"bit $offset range entry exclusion vendor $vendor_id, but only vendors [1, $max_id] are valid"
if 1 > $vendor_id || $vendor_id > $max_id;

foreach my $id ( keys %{$cache} ) {
$cache->{$id} = 1 if $id == $vendor_id;
}

return [ $vendor_id, $vendor_id ], $next_offset;
}

Expand All @@ -109,6 +127,8 @@ sub contains {
croak "invalid vendor id $id: must be positive integer bigger than 0"
if $id < 1;

return $self->{cache}->{$id} if exists $self->{cache}->{$id};

return if $id > $self->{max_id};

foreach my $range ( @{ $self->{ranges} } ) {
Expand Down

0 comments on commit a166b2f

Please sign in to comment.