| 1 | package GDPR::IAB::TCFv2::BitUtils 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 | 73 | ▶ | use Math::BigInt; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 73 | ▶ | use Carp qw(croak confess); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 73 | require Exporter; | |||||||||||||||||||||||||||
| 11 | 73 | ▶ | use parent qw<Exporter>; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 73 | ▶ | use constant ASCII_OFFSET => ord('A'); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 73 | our $CAN_PACK_QUADS; | |||||||||||||||||||||||||||
| 16 | 73 | our $CAN_FORCE_BIG_ENDIAN; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 73 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 73 | $CAN_PACK_QUADS = !!eval { pack 'Q>', 1 }; | |||||||||||||||||||||||||||
| 20 | 73 | $CAN_FORCE_BIG_ENDIAN = !!eval { pack 'S>', 1 }; | |||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 73 | 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 | 545358 | ▶ | sub is_set { | ||||||||||||||||||||||||||
|
Subroutine
is_set: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 34 | 545358 | my ($data, $offset) = @_; | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 545358 | my $byte_offset = $offset >> 3; | |||||||||||||||||||||||||||
| 37 | 545358 | 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 | 545358 | my $vec_offset = ($byte_offset << 3) | (7 - $bit_in_byte); | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 545358 | ▶ | croak "index out of bounds on offset $offset: can't read 1" if ($byte_offset >= length($data)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 545358 | my $r = vec($data, $vec_offset, 1); | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 545358 | ▶ | return wantarray ? ($r, $offset + 1) : $r; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 23994 | ▶ | sub get_uint2 { | ||||||||||||||||||||||||||
|
Subroutine
get_uint2: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 51 | 23994 | my ($data, $offset) = @_; | |||||||||||||||||||||||||||
| 52 | 23994 | return _get_bits($data, $offset, 2); | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 20549 | ▶ | sub get_uint3 { | ||||||||||||||||||||||||||
|
Subroutine
get_uint3: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 56 | 20549 | my ($data, $offset) = @_; | |||||||||||||||||||||||||||
| 57 | 20549 | return _get_bits($data, $offset, 3); | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 72177 | ▶ | sub get_uint6 { | ||||||||||||||||||||||||||
|
Subroutine
get_uint6: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 61 | 72177 | my ($data, $offset) = @_; | |||||||||||||||||||||||||||
| 62 | 72177 | return _get_bits($data, $offset, 6); | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 2133 | ▶ | sub get_char6_pair { | ||||||||||||||||||||||||||
|
Subroutine
get_char6_pair: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 66 | 2133 | my ($data, $offset) = @_; | |||||||||||||||||||||||||||
| 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 | 54581 | ▶ | sub get_uint12 { | ||||||||||||||||||||||||||
|
Subroutine
get_uint12: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 82 | 54581 | my ($data, $offset) = @_; | |||||||||||||||||||||||||||
| 83 | 54581 | return _get_bits($data, $offset, 12); | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 474313 | ▶ | sub get_uint16 { | ||||||||||||||||||||||||||
|
Subroutine
get_uint16: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 87 | 474313 | my ($data, $offset) = @_; | |||||||||||||||||||||||||||
| 88 | 474313 | return _get_bits($data, $offset, 16); | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 21251 | ▶ | sub get_uint36 { | ||||||||||||||||||||||||||
|
Subroutine
get_uint36: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 92 | 21251 | my ($data, $offset) = @_; | |||||||||||||||||||||||||||
| 93 | 21251 | 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 | 666865 | ▶ | sub _get_bits { | ||||||||||||||||||||||||||
|
Subroutine
_get_bits: called CC=15 SCAR=27.1
| |||||||||||||||||||||||||||||
| 100 | 666865 | my ($data, $offset, $nbits) = @_; | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 666865 | my $byte_start = $offset >> 3; | |||||||||||||||||||||||||||
| 103 | 666865 | my $bit_start = $offset & 7; | |||||||||||||||||||||||||||
| 104 | 666865 | my $byte_end = ($offset + $nbits - 1) >> 3; | |||||||||||||||||||||||||||
| 105 | 666865 | my $num_bytes = $byte_end - $byte_start + 1; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 666865 | ▶ | croak "index out of bounds on offset $offset: can't read $nbits" if ($byte_end >= length($data)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 666865 | my $raw = substr($data, $byte_start, $num_bytes); | |||||||||||||||||||||||||||
| 110 | 666865 | 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 | 666865 | ▶ | if ($CAN_FORCE_BIG_ENDIAN) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 666831 | ▶ | if ($num_bytes == 1) { | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 116 | 55114 | $val = unpack("C", $raw); | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | elsif ($num_bytes == 2) { | ||||||||||||||||||||||||||||
| 119 | 155889 | $val = unpack("n", $raw); | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | elsif ($num_bytes == 3) { | ||||||||||||||||||||||||||||
| 122 | 434585 | $val = unpack("N", "\0" . $raw); | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | elsif ($num_bytes == 4) { | ||||||||||||||||||||||||||||
| 125 | 0 | $val = unpack("N", $raw); | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 666865 | ▶ | 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 | 21277 | ▶ | if ($CAN_PACK_QUADS) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 21243 | my $padding = "\0" x (8 - $num_bytes); | |||||||||||||||||||||||||||
| 132 | 21243 | $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 | 666865 | my $bits_in_buffer = $num_bytes << 3; | |||||||||||||||||||||||||||
| 141 | 666865 | my $right_shift = $bits_in_buffer - $bit_start - $nbits; | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 666865 | ▶ | 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 | 666831 | ▶ | $val >>= $right_shift if $right_shift > 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | 666831 | my $mask = (1 << $nbits) - 1; | |||||||||||||||||||||||||||
| 153 | 666831 | $val &= $mask; | |||||||||||||||||||||||||||
| 154 | 666831 | ▶ | return wantarray ? ($val, $offset + $nbits) : $val; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | # Remove legacy helpers that worked on bitstrings | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 73 | 1; | |||||||||||||||||||||||||||
| 161 | __END__ | ||||||||||||||||||||||||||||