| 1 | package AWS::Signature::V4::Checksum; | ||||||||||||||||||||||||||||
| 2 | 9 | ▶ | use v5.24; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 9 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 9 | ▶ | use Compress::Raw::Zlib (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 9 | ▶ | use Digest::SHA (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 9 | ▶ | use MIME::Base64 qw< encode_base64 >; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 9 | ▶ | use AWS::Signature::V4::Error qw< fail >; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 9 | ▶ | use experimental qw< signatures >; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 9 | ▶ | use constant { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | # name => size of the raw checksum, in bytes | ||||||||||||||||||||||||||||
| 12 | SIZE_FOR => +{ crc32 => 4, crc32c => 4, sha1 => 20, sha256 => 32 }, | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | # crc32c comes from String::CRC32C when it is installed (it is much | ||||||||||||||||||||||||||||
| 15 | # faster), otherwise from the slicing-by-4 code below: $T0[$b] is the | ||||||||||||||||||||||||||||
| 16 | # effect of byte $b on the register, $T1 to $T3 that of the same byte | ||||||||||||||||||||||||||||
| 17 | # followed by 1 to 3 zeros | ||||||||||||||||||||||||||||
| 18 | 9 | CRC32C_XS => eval {; require String::CRC32C; \&String::CRC32C::crc32c }, | |||||||||||||||||||||||||||
| 19 | 9 | }; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 9 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 9 | has algorithm => (is => 'ro', required => 1); | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | # running state: the CRCs keep a number, the SHA ones a Digest::SHA object | ||||||||||||||||||||||||||||
| 26 | 9 | has _crc => (is => 'rw', lazy => 1, builder => 1); | |||||||||||||||||||||||||||
| 27 | 9 | has _sha => (is => 'lazy'); | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | # the algorithm is given as the only, positional, argument: new('crc32') | ||||||||||||||||||||||||||||
| 30 | 127 | ▶ | around BUILDARGS => sub ($orig, $class, @args) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=15.4
| |||||||||||||||||||||||||||||
| 31 | 127 | ▶ | return {algorithm => $args[0]} if @args == 1 && !ref $args[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@args == 1 and not ref $args[0]
MC/DC0%0/2
@args == 1 and not ref $args[0]
@args == 1
not ref $args[0]
Truth table33%1/3
A and not(B)
A= @args == 1B= not ref $args[0]
| |||||||||||||||||||||||||||||
| 32 | 0 | return $class->$orig(@args); | |||||||||||||||||||||||||||
| 33 | 9 | }; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 65 | ▶ | sub _build__crc ($self) { $self->algorithm eq 'crc32c' ? 0xFFFFFFFF : 0 } | ||||||||||||||||||||||||||
|
Subroutine
_build__crc: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 62 | ▶ | sub _build__sha ($self) { Digest::SHA->new($self->algorithm eq 'sha1' ? 1 : 256) } | ||||||||||||||||||||||||||
|
Subroutine
_build__sha: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 84 | ▶ | sub encoded_size ($class, $algo) { 4 * int((SIZE_FOR->{$algo} + 2) / 3) } | ||||||||||||||||||||||||||
|
Subroutine
encoded_size: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 1306 | ▶ | sub add ($self, $ref) { # $ref: reference to a byte string | ||||||||||||||||||||||||||
|
Subroutine
add: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 41 | 1306 | ▶ | fail 400, 'checksum data must be a reference to a byte string' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $ref eq "SCALAR" and defined $$ref
MC/DC100%2/2
ref $ref eq "SCALAR" and defined $$ref
ref $ref eq "SCALAR"
defined $$ref
Truth table100%3/3
A and B
A= ref $ref eq "SCALAR"B= defined $$ref
| |||||||||||||||||||||||||||||
| 42 | unless ref $ref eq 'SCALAR' && defined $$ref; | ||||||||||||||||||||||||||||
| 43 | 1298 | ▶ | utf8::is_utf8($$ref) && !utf8::downgrade(my $copy = $$ref, 1) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
utf8::is_utf8($$ref) and not utf8::downgrade(my $copy = $$ref, 1)
MC/DC100%2/2
utf8::is_utf8($$ref) and not utf8::downgrade(my $copy = $$ref, 1)
utf8::is_utf8($$ref)
not utf8::downgrade(my $copy = $$ref, 1)
Truth table100%3/3
A and not(B)
A= utf8::is_utf8($$ref)B= not utf8::downgrade(my $copy = $$ref, 1)
| |||||||||||||||||||||||||||||
| 44 | and fail 400, 'checksum data must be a byte string: encode characters first'; | ||||||||||||||||||||||||||||
| 45 | 1294 | my $algo = $self->algorithm; | |||||||||||||||||||||||||||
| 46 | 1294 | ▶ | if ($algo eq 'crc32') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 47 | 21 | $self->_crc(Compress::Raw::Zlib::crc32($$ref, $self->_crc)); | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | elsif ($algo eq 'crc32c') { | ||||||||||||||||||||||||||||
| 50 | 445 | $self->_crc(_crc32c($self->_crc, $ref)); | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | 828 | else { $self->_sha->add($$ref) } | |||||||||||||||||||||||||||
| 53 | 1294 | return $self; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | # register after the data; String::CRC32C wants and gives it inverted | ||||||||||||||||||||||||||||
| 57 | 445 | ▶ | sub _crc32c ($crc, $ref) { | ||||||||||||||||||||||||||
|
Subroutine
_crc32c: called CC=7 SCAR=39.2
| |||||||||||||||||||||||||||||
| 58 | 445 | return CRC32C_XS->($$ref, $crc ^ 0xFFFFFFFF) ^ 0xFFFFFFFF if CRC32C_XS; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | state $T0 = [ | ||||||||||||||||||||||||||||
| 61 | map { | ||||||||||||||||||||||||||||
| 62 | 0 | my $c = $_; | |||||||||||||||||||||||||||
| 63 | 0 | ▶ | $c = ($c & 1) ? (($c >> 1) ^ 0x82F63B78) : ($c >> 1) for 1 .. 8; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 0 | $c; | |||||||||||||||||||||||||||
| 65 | } 0 .. 255 | ||||||||||||||||||||||||||||
| 66 | ]; | ||||||||||||||||||||||||||||
| 67 | 0 | state $T1 = [ map { $T0->[$T0->[$_] & 0xFF] ^ ($T0->[$_] >> 8) } 0 .. 255 ]; | |||||||||||||||||||||||||||
| 68 | 0 | state $T2 = [ map { $T0->[$T1->[$_] & 0xFF] ^ ($T1->[$_] >> 8) } 0 .. 255 ]; | |||||||||||||||||||||||||||
| 69 | 0 | state $T3 = [ map { $T0->[$T2->[$_] & 0xFF] ^ ($T2->[$_] >> 8) } 0 .. 255 ]; | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 0 | my $words = length($$ref) & ~3; | |||||||||||||||||||||||||||
| 73 | 0 | ▶ | for (my $offset = 0; $offset < $words; $offset += 65536) { # bounded lists | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 0 | ▶ | my $size = $words - $offset < 65536 ? $words - $offset : 65536; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 0 | for my $word (unpack 'V*', substr $$ref, $offset, $size) { | |||||||||||||||||||||||||||
| 76 | 0 | my $c = $crc ^ $word; | |||||||||||||||||||||||||||
| 77 | 0 | $crc = $T3->[$c & 0xFF] | |||||||||||||||||||||||||||
| 78 | ^ $T2->[($c >> 8) & 0xFF] | ||||||||||||||||||||||||||||
| 79 | ^ $T1->[($c >> 16) & 0xFF] | ||||||||||||||||||||||||||||
| 80 | ^ $T0->[$c >> 24]; | ||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | 0 | $crc = $T0->[($crc ^ $_) & 0xFF] ^ ($crc >> 8) for unpack 'C*', substr $$ref, $words; | |||||||||||||||||||||||||||
| 84 | 0 | return $crc; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 123 | ▶ | sub base64 ($self) { | ||||||||||||||||||||||||||
|
Subroutine
base64: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 88 | 123 | my $algo = $self->algorithm; | |||||||||||||||||||||||||||
| 89 | 123 | ▶ | my $raw = | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 90 | $algo eq 'crc32' ? pack('N', $self->_crc) | ||||||||||||||||||||||||||||
| 91 | : $algo eq 'crc32c' ? pack('N', $self->_crc ^ 0xFFFFFFFF) | ||||||||||||||||||||||||||||
| 92 | : $self->_sha->digest; | ||||||||||||||||||||||||||||
| 93 | 123 | return encode_base64($raw, ''); | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 199 | ▶ | sub is_supported_algorithm ($co, $name) { exists(SIZE_FOR->{$name}) } | ||||||||||||||||||||||||||
|
Subroutine
is_supported_algorithm: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 4 | ▶ | sub supported_algorithms { sort { $a cmp $b } keys(SIZE_FOR->%*) } | ||||||||||||||||||||||||||
|
Subroutine
supported_algorithms: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 9 | 1; | |||||||||||||||||||||||||||