Skip to content

Commit

Permalink
refactor offsets
Browse files Browse the repository at this point in the history
  • Loading branch information
peczenyj committed Dec 12, 2023
1 parent 6a4ca72 commit 7fafe0e
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 44 deletions.
40 changes: 18 additions & 22 deletions lib/GDPR/IAB/TCFv2.pm
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,6 @@ sub Parse {

croak 'invalid vendor list version' if $self->vendor_list_version == 0;

# TODO parse special feature opt in

#_parse_bitfield()

# TODO parse purpose section
# TODO parse purpose consent

# TODO parse purpose legitimate interest

# parse vendor section
# parse vendor consent

Expand Down Expand Up @@ -495,16 +486,18 @@ sub _parse_vendor_legitimate_interests {
sub _parse_publisher_restrictions {
my ( $self, $pub_restrict_offset ) = @_;

my ($publisher_restrictions, $next_offset) = GDPR::IAB::TCFv2::PublisherRestrictions->Parse(
data => $self->{data},
offset => $pub_restrict_offset,
max_id =>ASSUMED_MAX_VENDOR_ID,
my $data = substr($self->{data}, $pub_restrict_offset, ASSUMED_MAX_VENDOR_ID);

my ($publisher_restrictions, $relative_next_offset) = GDPR::IAB::TCFv2::PublisherRestrictions->Parse(
data => $data,
data_size => length($self->{data}),
max_id => ASSUMED_MAX_VENDOR_ID,
options => $self->{options},
);

$self->{publisher_restrictions} = $publisher_restrictions;

return $next_offset;
return $pub_restrict_offset + $relative_next_offset;
}

sub _get_core_tc_string {
Expand Down Expand Up @@ -550,30 +543,33 @@ sub _is_vendor_consent_range_encoding {
}

sub _parse_range_section {
my ( $self, $max_id, $offset ) = @_;
my ( $self, $max_id, $range_section_start_offset ) = @_;

my $data = substr($self->{data}, $range_section_start_offset, $max_id);

my ( $range_section, $next_offset ) =
GDPR::IAB::TCFv2::RangeSection->Parse(
data => $self->{data},
offset => $offset,
data => $data,
data_size => length($self->{data}),
max_id => $max_id,
options => $self->{options},
);

return ( $range_section, $next_offset );
return wantarray ? ( $range_section, $range_section_start_offset +$next_offset ) : $range_section;
}

sub _parse_bitfield {
my ( $self, $max_id, $offset ) = @_;
my ( $self, $max_id, $bitfield_start_offset ) = @_;

my $data = substr($self->{data}, $bitfield_start_offset, $max_id);

my ( $bitfield, $next_offset ) = GDPR::IAB::TCFv2::BitField->Parse(
data => $self->{data},
offset => $offset,
data => $data,
max_id => $max_id,
options => $self->{options},
);

return ( $bitfield, $next_offset );
return wantarray ? ( $bitfield, $bitfield_start_offset + $next_offset ) : $bitfield;
}

sub looksLikeIsConsentVersion2 {
Expand Down
5 changes: 2 additions & 3 deletions lib/GDPR/IAB/TCFv2/BitField.pm
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@ use Carp qw<croak>;
sub Parse {
my ( $klass, %args ) = @_;

croak "missing 'data'" unless defined $args{data};
croak "missing 'offset'" unless defined $args{offset};
croak "missing 'data'" unless defined $args{data};
croak "missing 'max_id'"
unless defined $args{max_id};

croak "missing 'options'" unless defined $args{options};
croak "missing 'options.json'" unless defined $args{options}->{json};

my $data = $args{data};
my $offset = $args{offset};
my $offset = $args{offset} || 0;
my $max_id = $args{max_id};
my $options = $args{options};

Expand Down
24 changes: 13 additions & 11 deletions lib/GDPR/IAB/TCFv2/PublisherRestrictions.pm
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,19 @@ use GDPR::IAB::TCFv2::BitUtils qw<is_set
sub Parse {
my ( $klass, %args ) = @_;

croak "missing 'data'" unless defined $args{data};
croak "missing 'offset'" unless defined $args{offset};
croak "missing 'data'" unless defined $args{data};
croak "missing 'data_size'" unless defined $args{data_size};
croak "missing 'max_id'"
unless defined $args{max_id};

croak "missing 'options'" unless defined $args{options};
croak "missing 'options.json'" unless defined $args{options}->{json};

my $data = $args{data};
my $offset = $args{offset};
my $max_id = $args{max_id};
my $options = $args{options};
my $data = $args{data};
my $data_size = $args{data_size};
my $offset = $args{offset} || 0;
my $max_id = $args{max_id};
my $options = $args{options};

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

Expand All @@ -42,10 +43,11 @@ sub Parse {

( $vendor_restrictions, $next_offset ) =
GDPR::IAB::TCFv2::RangeSection->Parse(
data => $data,
offset => $next_offset,
max_id => $max_id,
options => $options,
data => $data,
data_size => $data_size,
offset => $next_offset,
max_id => $max_id,
options => $options,
);

$restrictions{$purpose_id} ||= {};
Expand Down Expand Up @@ -111,7 +113,7 @@ GDPR::IAB::TCFv2::PublisherRestrictions - Transparency & Consent String version
max_id =>ASSUMED_MAX_VENDOR_ID,
options => $self->{options},
);
die "there is publisher restriction on purpose id 1, type 0 on vendor 284"
if $range->contains(1, 0, 284);
Expand Down
14 changes: 7 additions & 7 deletions lib/GDPR/IAB/TCFv2/RangeSection.pm
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ use Carp qw<croak>;
sub Parse {
my ( $klass, %args ) = @_;

croak "missing 'data'" unless defined $args{data};
croak "missing 'offset'" unless defined $args{offset};
croak "missing 'data'" unless defined $args{data};
croak "missing 'data_size'" unless defined $args{data_size};
croak "missing 'max_id'"
unless defined $args{max_id};

croak "missing 'options'" unless defined $args{options};
croak "missing 'options.json'" unless defined $args{options}->{json};

my $data = $args{data};
my $offset = $args{offset};
my $data = $args{data};
my $data_size = $args{data_size};

my $offset = $args{offset} || 0;
my $max_id = $args{max_id};
my $options = $args{options};

my $data_size = length($data);

croak
Carp::confess
"a BitField for vendor consent strings using RangeSections require at least 31 bytes. Got $data_size"
if $data_size < 31;

Expand Down
2 changes: 1 addition & 1 deletion t/01-parse.t
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ subtest "invalid tcf consent string candidates" => sub {
GDPR::IAB::TCFv2->Parse(
'COvcSpYOvcSpYC9AAAENAPCAAAAAAAAAAAAAAFQBgAAgABAACAAEAAQAAgAA')
}
qr/index out of bounds on offset 360/,
qr/index out of bounds on offset 0: can't read 12, only has: 10/,
'this test uses a crafted consent uses range section, declares 10 vendors, 6 exceptions and legitimate interest without require';


Expand Down

0 comments on commit 7fafe0e

Please sign in to comment.