Skip to content

Commit

Permalink
fix issue #8
Browse files Browse the repository at this point in the history
  • Loading branch information
peczenyj committed Dec 4, 2023
1 parent 45f94a6 commit 1ccc2cb
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions lib/GDPR/IAB/TCFv2/BitUtils.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use strict;
use warnings;
use integer;
use bytes;
use Math::BigInt;

use feature 'state';

Expand Down Expand Up @@ -82,20 +83,27 @@ sub get_uint16 {
sub get_uint36 {
my ( $data, $offset ) = @_;

return unpack(
"Q>",
_get_bits_with_padding( $data, 64, $offset, 36 )
);
state $can_pack_quads = !!eval { my $f = pack 'q'; 1 };

return $can_pack_quads
? unpack( "Q>", _get_bits_with_padding( $data, 64, $offset, 36 ) )
: Math::BigInt->new( "0b" . _add_padding( $data, 64, $offset, 36 ) );
}

sub _get_bits_with_padding {
my ( $data, $bits, $offset, $nbits ) = @_;

# TODO check if offset is in range of $data ?

return pack( "B${bits}", _add_padding( $data, $bits, $offset, $nbits ) );
}

sub _add_padding {
my ( $data, $bits, $offset, $nbits ) = @_;

my $padding = "0" x ( $bits - $nbits );

return pack( "B${bits}", $padding . substr( $data, $offset, $nbits ) );
return $padding . substr( $data, $offset, $nbits );
}

1;
Expand Down

0 comments on commit 1ccc2cb

Please sign in to comment.