blib/lib/GDPR/IAB/TCFv2/BitUtils.pm

filter: Statementstmt 98.8% 88 / 89 Branchbran 87.5% 28 / 32 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 18 / 18 Podpod 87.5% 7 / 8 totaltotal 94.7% 144 / 152 CC 19 SCAR 19.7
CC
19
Cov
95%
_get_bits
15.1
get_char6_pair
3.0
is_set
3.0
CRAP 7.2
« blib/lib/GDPR/IAB/TCFv2/Validator.pm ↑ Summary blib/lib/GDPR/IAB/TCFv2/BitField.pm »
1 package GDPR::IAB::TCFv2::BitUtils 0.520;
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
BranchTrueFalseTotal
if $byte_offset >= length $data 0 537105 537105
44  
45 537105 my $r = vec($data, $vec_offset, 1);
46  
47 537105 ▶ return wantarray ? ($r, $offset + 1) : $r;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 415065 122040 537105
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
BranchTrueFalseTotal
wantarray ? : 0 2133 2133
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
BranchTrueFalseTotal
if $byte_end >= length $data 0 632953 632953
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
BranchTrueFalseTotal
if ($GDPR::IAB::TCFv2::BitUtils::CAN_FORCE_BIG_ENDIAN) 632919 34 632953
115 632919 ▶ if ($num_bytes == 1) {
Branch: 4 branches
BranchTrueFalseTotal
if ($num_bytes == 1) { } 55116 577803 632919
elsif ($num_bytes == 2) { } 140058 437745 577803
elsif ($num_bytes == 3) { } 434582 3163 437745
elsif ($num_bytes == 4) { } 0 3163 3163
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
BranchTrueFalseTotal
if (not defined $val and $num_bytes <= 8) 3197 629756 632953
Condition66%2/3
not defined $val and $num_bytes <= 8
!ll&&!rl&&r
62975603197
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
ABresult
0X0
100
111
130 3197 ▶ if ($CAN_PACK_QUADS) {
Branch: 1 branch
BranchTrueFalseTotal
if ($GDPR::IAB::TCFv2::BitUtils::CAN_PACK_QUADS) { } 3163 34 3197
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
BranchTrueFalseTotal
if (ref $val) { } 34 632919 632953
144 34 ▶ $val >>= $right_shift if $right_shift > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $right_shift > 0 33 1 34
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
BranchTrueFalseTotal
wantarray ? : 13 21 34
149 }
150 else {
151 632919 ▶ $val >>= $right_shift if $right_shift > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $right_shift > 0 563931 68988 632919
152 632919 my $mask = (1 << $nbits) - 1;
153 632919 $val &= $mask;
154 632919 ▶ return wantarray ? ($val, $offset + $nbits) : $val;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 565043 67876 632919
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  
« blib/lib/GDPR/IAB/TCFv2/Validator.pm ↑ Summary blib/lib/GDPR/IAB/TCFv2/BitField.pm »