| 1 | package Linux::Event::WebSocket::_UTF8; | ||||||||||||||||||||||||||||
| 2 | 11 | ▶ | use v5.36; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 11 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 11 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 11 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 11 | ▶ | use utf8 (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 62 | ▶ | sub _byte_copy ($bytes) { | ||||||||||||||||||||||||||
|
Subroutine
_byte_copy: called CC=4 SCAR=15.6
| |||||||||||||||||||||||||||||
| 10 | 62 | ▶ | croak 'UTF-8 input must be a defined scalar' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $bytes or ref $bytes
MC/DC0%0/2
not defined $bytes or ref $bytes
not defined $bytes
ref $bytes
Truth table33%1/3
not(A) or B
A= not defined $bytesB= ref $bytes
| |||||||||||||||||||||||||||||
| 11 | if !defined($bytes) || ref($bytes); | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 62 | my $copy = "$bytes"; | |||||||||||||||||||||||||||
| 14 | 62 | ▶ | croak 'UTF-8 input must contain bytes' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 15 | if !utf8::downgrade($copy, 1); | ||||||||||||||||||||||||||||
| 16 | 62 | return $copy; | |||||||||||||||||||||||||||
| 17 | } | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 69 | ▶ | sub _decode_copy ($copy) { | ||||||||||||||||||||||||||
|
Subroutine
_decode_copy: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 20 | 69 | my $byte_length = length $copy; | |||||||||||||||||||||||||||
| 21 | 69 | my $decoded = $copy; | |||||||||||||||||||||||||||
| 22 | 69 | ▶ | croak 'invalid RFC 3629 UTF-8' if !utf8::decode($decoded); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | # A valid UTF-8 byte string can keep the original byte scalar when every | ||||||||||||||||||||||||||||
| 25 | # code point was ASCII. Comparing decoded character count with input byte | ||||||||||||||||||||||||||||
| 26 | # count is much cheaper here than scanning the entire payload with a regex. | ||||||||||||||||||||||||||||
| 27 | 61 | ▶ | return $copy if length($decoded) == $byte_length; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | # Perl accepts surrogate and out-of-range code points in its internal UTF-8 | ||||||||||||||||||||||||||||
| 30 | # representation, while RFC 3629 forbids them. | ||||||||||||||||||||||||||||
| 31 | 14 | ▶ | croak 'invalid RFC 3629 UTF-8' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | if $decoded =~ /[\x{d800}-\x{dfff}]|[^\x{0}-\x{10ffff}]/; | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 12 | return $decoded; | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 62 | ▶ | sub _decode_bytes ($bytes) { | ||||||||||||||||||||||||||
|
Subroutine
_decode_bytes: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | 62 | return _decode_copy(_byte_copy($bytes)); | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 45 | ▶ | sub validate_bytes ($class, $bytes) { | ||||||||||||||||||||||||||
|
Subroutine
validate_bytes: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 42 | 45 | _decode_bytes($bytes); | |||||||||||||||||||||||||||
| 43 | 35 | return 1; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 17 | ▶ | sub decode ($class, $bytes) { | ||||||||||||||||||||||||||
|
Subroutine
decode: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 47 | 17 | return _decode_bytes($bytes); | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 16 | ▶ | sub encode ($class, $text) { | ||||||||||||||||||||||||||
|
Subroutine
encode: called CC=5 SCAR=16.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 51 | 16 | ▶ | croak 'UTF-8 text must be a defined scalar' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $text or ref $text
MC/DC0%0/2
not defined $text or ref $text
not defined $text
ref $text
Truth table33%1/3
not(A) or B
A= not defined $textB= ref $text
| |||||||||||||||||||||||||||||
| 52 | if !defined($text) || ref($text); | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 16 | my $copy = "$text"; | |||||||||||||||||||||||||||
| 55 | 16 | ▶ | if (!utf8::is_utf8($copy)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 7 | _decode_copy($copy); | |||||||||||||||||||||||||||
| 57 | 7 | return $copy; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 9 | ▶ | croak 'UTF-8 text contains a non-Unicode scalar value' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | if $copy =~ /[\x{d800}-\x{dfff}]|[^\x{0}-\x{10ffff}]/; | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 6 | utf8::encode($copy); | |||||||||||||||||||||||||||
| 64 | 6 | return $copy; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 11 | 1; | |||||||||||||||||||||||||||