| 1 | package Unblock::HTTP3::Capsule; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use Unblock::HTTP3 (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 5 | ▶ | use Unblock::HTTP3::_Bytes (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 5 | our $VERSION = '0.14'; | |||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 5 | my $MAX_VARINT = '4611686018427387903'; | |||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | 59 | ▶ | sub _normalize_varint { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_normalize_varint: called CC=8 SCAR=26.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 59 | my ($value, $name) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | 59 | ▶ | $name ||= 'capsule integer'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$name ||= "capsule integer"
MC/DC0%0/1
$name ||= "capsule integer"
$name
Truth table50%1/2
A
A= $name
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | 59 | ▶ | croak "$name must be a non-negative integer" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
not defined $value or ref $value or not "$value" =~ /\A[0-9]+\z/
not defined $value or ref $value
MC/DC0%0/3
not defined $value or ref $value or not "$value" =~ /\A[0-9]+\z/
not defined $value
ref $value
not "$value" =~ /\A[0-9]+\z/
Truth table25%1/4
not(A) or B or not(C)
A= not defined $valueB= ref $valueC= not "$value" =~ /\A[0-9]+\z/
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | if !defined($value) | ||||||||||||||||||||||||||||||||||||||||||
| 22 | || ref($value) | ||||||||||||||||||||||||||||||||||||||||||
| 23 | || "$value" !~ /\A[0-9]+\z/; | ||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | 59 | my $text = "$value"; | |||||||||||||||||||||||||||||||||||||||||
| 26 | 59 | $text =~ s/\A0+(?=[0-9])//; | |||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 59 | ▶ | croak "$name exceeds the HTTP/3 varint maximum" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
CORE::length $text > CORE::length $MAX_VARINT or CORE::length $text == CORE::length $MAX_VARINT and $text gt $MAX_VARINT
CORE::length $text == CORE::length $MAX_VARINT and $text gt $MAX_VARINT
MC/DC0%0/3
CORE::length $text > CORE::length $MAX_VARINT or CORE::length $text == CORE::length $MAX_VARINT and $text gt $MAX_VARINT
CORE::length $text > CORE::length $MAX_VARINT
CORE::length $text == CORE::length $MAX_VARINT
$text gt $MAX_VARINT
Truth table25%1/4
A or B and C
A= CORE::length $text > CORE::length $MAX_VARINTB= CORE::length $text == CORE::length $MAX_VARINTC= $text gt $MAX_VARINT
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | if length($text) > length($MAX_VARINT) | ||||||||||||||||||||||||||||||||||||||||||
| 30 | || ( | ||||||||||||||||||||||||||||||||||||||||||
| 31 | length($text) == length($MAX_VARINT) | ||||||||||||||||||||||||||||||||||||||||||
| 32 | && $text gt $MAX_VARINT | ||||||||||||||||||||||||||||||||||||||||||
| 33 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | 59 | return $text; | |||||||||||||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | 32 | ▶ | sub _encode_varint { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_encode_varint: called CC=4 SCAR=14.6
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | 32 | my ($value) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | 32 | $value = _normalize_varint($value, 'capsule integer'); | |||||||||||||||||||||||||||||||||||||||||
| 42 | 32 | my $number = 0 + $value; | |||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | 32 | ▶ | return pack('C', $number) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | if $number <= 63; | ||||||||||||||||||||||||||||||||||||||||||
| 46 | 5 | ▶ | return pack('n', $number | 0x4000) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | if $number <= 16_383; | ||||||||||||||||||||||||||||||||||||||||||
| 48 | 1 | ▶ | return pack('N', $number | 0x80000000) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | if $number <= 1_073_741_823; | ||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | my $high = (($number >> 32) & 0x3fffffff) | 0xc0000000; | |||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | my $low = $number & 0xffffffff; | |||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | return pack('NN', $high, $low); | |||||||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | 44 | ▶ | sub _decode_varint { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_decode_varint: called CC=7 SCAR=19.9
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | 44 | my ($bytes, $offset) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 59 | 44 | ▶ | $offset ||= 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$offset ||= 0
MC/DC0%0/1
$offset ||= 0
$offset
Truth table50%1/2
A
A= $offset
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | 44 | ▶ | return if $offset >= length($bytes); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | 32 | my $first = ord(substr($bytes, $offset, 1)); | |||||||||||||||||||||||||||||||||||||||||
| 64 | 32 | my $length = (1, 2, 4, 8)[ $first >> 6 ]; | |||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | 32 | ▶ | return if length($bytes) - $offset < $length; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | 30 | my $value; | |||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | 30 | ▶ | if ($length == 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | 26 | $value = $first & 0x3f; | |||||||||||||||||||||||||||||||||||||||||
| 72 | } elsif ($length == 2) { | ||||||||||||||||||||||||||||||||||||||||||
| 73 | 3 | $value = unpack('n', substr($bytes, $offset, 2)) & 0x3fff; | |||||||||||||||||||||||||||||||||||||||||
| 74 | } elsif ($length == 4) { | ||||||||||||||||||||||||||||||||||||||||||
| 75 | 1 | $value = unpack('N', substr($bytes, $offset, 4)) & 0x3fffffff; | |||||||||||||||||||||||||||||||||||||||||
| 76 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | my ($high, $low) = unpack('NN', substr($bytes, $offset, 8)); | |||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | $high &= 0x3fffffff; | |||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | $value = ($high << 32) | $low; | |||||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | 30 | return ("$value", $length); | |||||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | 23 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=14.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | 23 | my ($class, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | 23 | my $type = delete $args{type}; | |||||||||||||||||||||||||||||||||||||||||
| 89 | 23 | ▶ | my $value = exists($args{value}) ? delete($args{value}) : ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | 23 | ▶ | croak 'Capsule type is required' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | unless defined $type; | ||||||||||||||||||||||||||||||||||||||||||
| 93 | 23 | ▶ | croak 'unknown Capsule option: ' . join(', ', sort keys %args) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | if %args; | ||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | 23 | $type = _normalize_varint($type, 'Capsule type'); | |||||||||||||||||||||||||||||||||||||||||
| 97 | 23 | $value = Unblock::HTTP3::_Bytes::byte_string('Capsule value', $value); | |||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | 23 | return bless { | |||||||||||||||||||||||||||||||||||||||||
| 100 | type => $type, | ||||||||||||||||||||||||||||||||||||||||||
| 101 | value => $value, | ||||||||||||||||||||||||||||||||||||||||||
| 102 | }, $class; | ||||||||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | 9 | ▶ | sub type { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
type: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | 9 | my ($self, @args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 107 | 9 | ▶ | croak 'type() does not accept arguments' if @args; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | 9 | return $self->{type}; | |||||||||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | 9 | ▶ | sub value { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
value: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | 9 | my ($self, @args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 113 | 9 | ▶ | croak 'value() does not accept arguments' if @args; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | 9 | return $self->{value}; | |||||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | 1 | ▶ | sub length { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
length: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | 1 | my ($self, @args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 119 | 1 | ▶ | croak 'length() does not accept arguments' if @args; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | 1 | return CORE::length($self->{value}); | |||||||||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||
| 123 | 16 | ▶ | sub encode { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
encode: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | 16 | my ($self, @args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 125 | 16 | ▶ | croak 'encode() does not accept arguments' if @args; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | return _encode_varint($self->{type}) | ||||||||||||||||||||||||||||||||||||||||||
| 128 | . _encode_varint(CORE::length($self->{value})) | ||||||||||||||||||||||||||||||||||||||||||
| 129 | 16 | . $self->{value}; | |||||||||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | 5 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||
| 134 | __END__ | ||||||||||||||||||||||||||||||||||||||||||