| 1 | package GDPR::IAB::TCFv2::BitUtils 0.512; | ||||||||||||||||||||||||||||
| 2 | 72 | ▶ | use v5.12; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 72 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 72 | ▶ | use integer; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 72 | ▶ | use bytes; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 72 | ▶ | use Math::BigInt; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 72 | ▶ | use Carp qw(croak confess); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 72 | require Exporter; | |||||||||||||||||||||||||||
| 11 | 72 | ▶ | use parent qw<Exporter>; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 72 | ▶ | use constant ASCII_OFFSET => ord('A'); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 72 | our $CAN_PACK_QUADS; | |||||||||||||||||||||||||||
| 16 | 72 | our $CAN_FORCE_BIG_ENDIAN; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | BEGIN { | ||||||||||||||||||||||||||||
| 19 | 72 | ▶ | $CAN_PACK_QUADS = !!eval { pack 'Q>', 1 }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 72 | $CAN_FORCE_BIG_ENDIAN = !!eval { pack 'S>', 1 }; | |||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 72 | our @EXPORT_OK = qw<is_set | |||||||||||||||||||||||||||
| 24 | get_uint2 | ||||||||||||||||||||||||||||
| 25 | get_uint3 | ||||||||||||||||||||||||||||
| 26 | get_uint6 | ||||||||||||||||||||||||||||
| 27 | get_uint12 | ||||||||||||||||||||||||||||
| 28 | get_uint16 | ||||||||||||||||||||||||||||
| 29 | get_uint36 | ||||||||||||||||||||||||||||
| 30 | get_char6_pair | ||||||||||||||||||||||||||||
| 31 | >; | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | sub is_set { | ||||||||||||||||||||||||||||
| 34 | 537105 | ▶ | my ($data, $offset) = @_; | ||||||||||||||||||||||||||
|
Subroutine
is_set: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 537105 | my $byte_offset = $offset >> 3; | |||||||||||||||||||||||||||
| 37 | 537105 | my $bit_in_byte = $offset & 7; | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | # TCF is MSB-first. vec(..., 1) addresses bits from LSB to MSB. | ||||||||||||||||||||||||||||
| 40 | # So bit 0 of the spec is bit 7 of the byte for vec(). | ||||||||||||||||||||||||||||
| 41 | 537105 | my $vec_offset = ($byte_offset << 3) | (7 - $bit_in_byte); | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 537105 | ▶ | croak "index out of bounds on offset $offset: can't read 1" if ($byte_offset >= length($data)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 537105 | my $r = vec($data, $vec_offset, 1); | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 537105 | ▶ | return wantarray ? ($r, $offset + 1) : $r; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | sub get_uint2 { | ||||||||||||||||||||||||||||
| 51 | 23996 | ▶ | my ($data, $offset) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_uint2: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 52 | 23996 | return _get_bits($data, $offset, 2); | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | sub get_uint3 { | ||||||||||||||||||||||||||||
| 56 | 20549 | ▶ | my ($data, $offset) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_uint3: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 57 | 20549 | return _get_bits($data, $offset, 3); | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | sub get_uint6 { | ||||||||||||||||||||||||||||
| 61 | 56347 | ▶ | my ($data, $offset) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_uint6: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 62 | 56347 | return _get_bits($data, $offset, 6); | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | sub get_char6_pair { | ||||||||||||||||||||||||||||
| 66 | 2133 | ▶ | my ($data, $offset) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_char6_pair: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 2133 | my $pair; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 2133 | for (1 .. 2) { | |||||||||||||||||||||||||||
| 71 | 4266 | my ($byte, $next_offset) = get_uint6($data, $offset); | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 4266 | $pair .= chr(ASCII_OFFSET + $byte); | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 4266 | $offset = $next_offset; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 2133 | ▶ | return wantarray ? ($pair, $offset) : $pair; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | sub get_uint12 { | ||||||||||||||||||||||||||||
| 82 | 54580 | ▶ | my ($data, $offset) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_uint12: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 83 | 54580 | return _get_bits($data, $offset, 12); | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | sub get_uint16 { | ||||||||||||||||||||||||||||
| 87 | 474310 | ▶ | my ($data, $offset) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_uint16: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 88 | 474310 | return _get_bits($data, $offset, 16); | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | sub get_uint36 { | ||||||||||||||||||||||||||||
| 92 | 3171 | ▶ | my ($data, $offset) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_uint36: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 93 | 3171 | return _get_bits($data, $offset, 36); | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | # General bit extractor that handles cross-byte boundaries efficiently. | ||||||||||||||||||||||||||||
| 97 | # Since TCF is MSB-first, we can extract bytes, join them into a large | ||||||||||||||||||||||||||||
| 98 | # integer, and then shift/mask. | ||||||||||||||||||||||||||||
| 99 | sub _get_bits { | ||||||||||||||||||||||||||||
| 100 | 632953 | ▶ | my ($data, $offset, $nbits) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_get_bits: called CC=15 SCAR=27.1
| |||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 632953 | my $byte_start = $offset >> 3; | |||||||||||||||||||||||||||
| 103 | 632953 | my $bit_start = $offset & 7; | |||||||||||||||||||||||||||
| 104 | 632953 | my $byte_end = ($offset + $nbits - 1) >> 3; | |||||||||||||||||||||||||||
| 105 | 632953 | my $num_bytes = $byte_end - $byte_start + 1; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 632953 | ▶ | croak "index out of bounds on offset $offset: can't read $nbits" if ($byte_end >= length($data)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 632953 | my $raw = substr($data, $byte_start, $num_bytes); | |||||||||||||||||||||||||||
| 110 | 632953 | my $val; | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | # Unpack into a native integer and shift. | ||||||||||||||||||||||||||||
| 113 | # For up to 36 bits, we might need up to 6 bytes if it spans boundaries. | ||||||||||||||||||||||||||||
| 114 | 632953 | ▶ | if ($CAN_FORCE_BIG_ENDIAN) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 632919 | ▶ | if ($num_bytes == 1) { | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 116 | 55116 | $val = unpack("C", $raw); | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | elsif ($num_bytes == 2) { | ||||||||||||||||||||||||||||
| 119 | 140058 | $val = unpack("n", $raw); | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | elsif ($num_bytes == 3) { | ||||||||||||||||||||||||||||
| 122 | 434582 | $val = unpack("N", "\0" . $raw); | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | elsif ($num_bytes == 4) { | ||||||||||||||||||||||||||||
| 125 | 0 | $val = unpack("N", $raw); | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 632953 | ▶ | if (!defined($val) && $num_bytes <= 8) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $val and $num_bytes <= 8
MC/DC50%1/2
not defined $val and $num_bytes <= 8
not defined $val
$num_bytes <= 8
Truth table66%2/3
not(A) and B
A= not defined $valB= $num_bytes <= 8
| |||||||||||||||||||||||||||||
| 130 | 3197 | ▶ | if ($CAN_PACK_QUADS) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 3163 | my $padding = "\0" x (8 - $num_bytes); | |||||||||||||||||||||||||||
| 132 | 3163 | $val = unpack("Q>", $padding . $raw); | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | else { | ||||||||||||||||||||||||||||
| 135 | 34 | $val = Math::BigInt->new("0x" . unpack("H*", $raw)); | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | # Shift right to remove trailing bits of the last byte | ||||||||||||||||||||||||||||
| 140 | 632953 | my $bits_in_buffer = $num_bytes << 3; | |||||||||||||||||||||||||||
| 141 | 632953 | my $right_shift = $bits_in_buffer - $bit_start - $nbits; | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 632953 | ▶ | if (ref $val) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 34 | ▶ | $val >>= $right_shift if $right_shift > 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | 34 | my $mask = Math::BigInt->new(1) << $nbits; | |||||||||||||||||||||||||||
| 146 | 34 | $mask -= 1; | |||||||||||||||||||||||||||
| 147 | 34 | $val &= $mask; | |||||||||||||||||||||||||||
| 148 | 34 | ▶ | return wantarray ? ($val->numify, $offset + $nbits) : $val->numify; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | else { | ||||||||||||||||||||||||||||
| 151 | 632919 | ▶ | $val >>= $right_shift if $right_shift > 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | 632919 | my $mask = (1 << $nbits) - 1; | |||||||||||||||||||||||||||
| 153 | 632919 | $val &= $mask; | |||||||||||||||||||||||||||
| 154 | 632919 | ▶ | return wantarray ? ($val, $offset + $nbits) : $val; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | # Remove legacy helpers that worked on bitstrings | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 72 | 1; | |||||||||||||||||||||||||||
| 161 | __END__ | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | =head1 NAME | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | GDPR::IAB::TCFv2::BitUtils - TCF v2.3 bit-level decoding utilities | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 168 | use GDPR::IAB::TCFv2::BitUtils qw<is_set get_uint16>; | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | my $data = '...'; # raw binary data | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | my $max_vendor_id_consent = get_uint16($data, 213); | ||||||||||||||||||||||||||||
| 173 | my $is_service_specific = is_set( $data, 138 ); | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | =head2 get_uint2 | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | Receive two parameters: data and bit offset. | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | Will fetch 2 bits from data since bit offset and convert it to an unsigned int. | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | my $value = get_uint2( $data, $offset ); | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | =head2 get_uint3 | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | Receive two parameters: data and bit offset. | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | Will fetch 3 bits from data since bit offset and convert it to an unsigned int. | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | my $segment_type = get_uint3( $data, 0 ); | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | =head2 get_uint6 | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | Receive two parameters: data and bit offset. | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | Will fetch 6 bits from data since bit offset and convert it to an unsigned int. | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | my $version = get_uint6( $data, 0 ); | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | =head2 get_char6_pair | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | Receives the data and bit offset. | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | Reads two consecutive 6-bit values starting at C<$offset>, increments each | ||||||||||||||||||||||||||||
| 204 | by the ASCII value of the letter C<A>, and returns the resulting two-letter | ||||||||||||||||||||||||||||
| 205 | string. Used to decode the C<consent_language> and C<publisher_country_code> | ||||||||||||||||||||||||||||
| 206 | fields of the TCF v2 core string. | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | my $consent_language = get_char6_pair($data, 108); # returns two letter country encoded as ISO_639-1 | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | =head2 get_uint12 | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | Receives the data and bit offset. | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | Will fetch 12 bits from data since bit offset and convert it to an unsigned int (short). | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | my $cmp_id = get_uint12( $data, 78 ); | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | =head2 get_uint16 | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | Receives the data and bit offset. | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | Will fetch 16 bits from data since bit offset and convert it to an unsigned int (short). | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | my $max_vendor_id_consent = get_uint16( $data, 213 ); | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | =head2 get_uint36 | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | Receives the data and bit offset. | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | Will fetch 36 bits from data since bit offset and convert it to an unsigned int (long). | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | my $deciseconds = get_uint36( $data, 6 ); | ||||||||||||||||||||||||||||
| 233 | my $created = $deciseconds/10; | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||