| 1 | package Mail::DKIM::MessageParser; | ||||||||||||||||||||||||||||
| 2 | 20 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 20 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 20 | our $VERSION = '1.20261116'; # VERSION | |||||||||||||||||||||||||||
| 5 | # ABSTRACT: Signs/verifies Internet mail with DKIM/DomainKey signatures | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | # Copyright 2005 Messiah College. All rights reserved. | ||||||||||||||||||||||||||||
| 8 | # Jason Long <jlong@messiah.edu> | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | # Copyright (c) 2004 Anthony D. Urso. All rights reserved. | ||||||||||||||||||||||||||||
| 11 | # This program is free software; you can redistribute it and/or | ||||||||||||||||||||||||||||
| 12 | # modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 20 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 1637 | ▶ | sub new_object { | ||||||||||||||||||||||||||
|
Subroutine
new_object: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 17 | 1637 | my $class = shift; | |||||||||||||||||||||||||||
| 18 | 1637 | return $class->TIEHANDLE(@_); | |||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 0 | ▶ | sub new_handle { | ||||||||||||||||||||||||||
|
Subroutine
new_handle: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 22 | 0 | my $class = shift; | |||||||||||||||||||||||||||
| 23 | 0 | local *TMP; | |||||||||||||||||||||||||||
| 24 | 0 | tie *TMP, $class, @_; | |||||||||||||||||||||||||||
| 25 | 0 | return *TMP; | |||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 1637 | ▶ | sub TIEHANDLE { | ||||||||||||||||||||||||||
|
Subroutine
TIEHANDLE: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 1637 | my $class = shift; | |||||||||||||||||||||||||||
| 30 | 1637 | my %args = @_; | |||||||||||||||||||||||||||
| 31 | 1637 | my $self = bless \%args, $class; | |||||||||||||||||||||||||||
| 32 | 1637 | $self->init; | |||||||||||||||||||||||||||
| 33 | 1635 | return $self; | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 1637 | ▶ | sub init { | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 37 | 1637 | my $self = shift; | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 1637 | my $buf = ''; | |||||||||||||||||||||||||||
| 40 | 1637 | $self->{buf_ref} = \$buf; | |||||||||||||||||||||||||||
| 41 | 1637 | $self->{in_header} = 1; | |||||||||||||||||||||||||||
| 42 | 1637 | $self->{header_scan} = 0; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 30476 | ▶ | sub PRINT { | ||||||||||||||||||||||||||
|
Subroutine
PRINT: called CC=16 SCAR=27.7
| |||||||||||||||||||||||||||||
| 46 | 30476 | my $self = shift; | |||||||||||||||||||||||||||
| 47 | 30476 | my $buf_ref = $self->{buf_ref}; | |||||||||||||||||||||||||||
| 48 | 30476 | ▶ | $$buf_ref .= @_ == 1 ? $_[0] : join( '', @_ ) if @_; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 30476 | ▶ | if ( $self->{in_header} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | # Walk the buffer with an offset, remove the parsed fields once, | ||||||||||||||||||||||||||||
| 53 | # and resume the search for the end of an incomplete field where | ||||||||||||||||||||||||||||
| 54 | # the last call stopped: removing each field from the front of | ||||||||||||||||||||||||||||
| 55 | # the buffer, or rescanning an incomplete field on every call, | ||||||||||||||||||||||||||||
| 56 | # is quadratic. | ||||||||||||||||||||||||||||
| 57 | 30451 | my $off = 0; | |||||||||||||||||||||||||||
| 58 | 30451 | my $scan = $self->{header_scan}; | |||||||||||||||||||||||||||
| 59 | 30451 | ▶ | while ( $off < length($$buf_ref) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 436433 | ▶ | if ( substr( $$buf_ref, $off, 2 ) eq "\015\012" ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 348 | substr( $$buf_ref, 0, $off + 2 ) = ''; | |||||||||||||||||||||||||||
| 62 | 348 | $off = $scan = 0; | |||||||||||||||||||||||||||
| 63 | 348 | $self->finish_header(); | |||||||||||||||||||||||||||
| 64 | 348 | $self->{in_header} = 0; | |||||||||||||||||||||||||||
| 65 | 348 | last; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | # the field ends at the first CRLF not followed by SP or HTAB | ||||||||||||||||||||||||||||
| 69 | 436085 | my $end; | |||||||||||||||||||||||||||
| 70 | 436085 | ▶ | my $i = $scan > $off ? $scan : $off; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 436085 | while (1) { | |||||||||||||||||||||||||||
| 72 | 470229 | my $j = index( $$buf_ref, "\015\012", $i ); | |||||||||||||||||||||||||||
| 73 | 470229 | ▶ | if ( $j < 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 79 | $i = length($$buf_ref) - 1; # a final CR may start a CRLF | |||||||||||||||||||||||||||
| 75 | 79 | last; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | 470150 | ▶ | if ( $j + 2 >= length($$buf_ref) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 30023 | $i = $j; # the next character isn't here yet | |||||||||||||||||||||||||||
| 79 | 30023 | last; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | 440127 | my $c = substr( $$buf_ref, $j + 2, 1 ); | |||||||||||||||||||||||||||
| 82 | 440127 | ▶ | if ( $c ne ' ' && $c ne "\t" ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$c ne " " and $c ne "\t"
MC/DC100%2/2
$c ne " " and $c ne "\t"
$c ne " "
$c ne "\t"
Truth table100%3/3
A and B
A= $c ne " "B= $c ne "\t"
| |||||||||||||||||||||||||||||
| 83 | 405983 | $end = $j + 2; | |||||||||||||||||||||||||||
| 84 | 405983 | last; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | 34144 | $i = $j + 2; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | 436085 | ▶ | if ( !defined $end ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 30102 | $scan = $i; | |||||||||||||||||||||||||||
| 90 | 30102 | last; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | 405983 | $self->add_header( substr( $$buf_ref, $off, $end - $off ) ); | |||||||||||||||||||||||||||
| 93 | 405983 | $off = $end; | |||||||||||||||||||||||||||
| 94 | 405983 | $scan = 0; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | 30451 | ▶ | substr( $$buf_ref, 0, $off ) = '' if $off; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 30451 | ▶ | $self->{header_scan} = $scan > $off ? $scan - $off : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 30476 | ▶ | if ( !$self->{in_header} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 373 | my $j = rindex( $$buf_ref, "\015\012" ); | |||||||||||||||||||||||||||
| 102 | 373 | ▶ | if ( $j >= 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | # avoid copying a large buffer: the unterminated | ||||||||||||||||||||||||||||
| 105 | # last line is typically short compared to the rest | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 351 | my $carry = substr( $$buf_ref, $j + 2 ); | |||||||||||||||||||||||||||
| 108 | 351 | substr( $$buf_ref, $j + 2 ) = ''; # shrink to last CRLF | |||||||||||||||||||||||||||
| 109 | 351 | $self->add_body($$buf_ref); # must end on CRLF | |||||||||||||||||||||||||||
| 110 | 351 | $$buf_ref = $carry; # restore unterminated last line | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | 30476 | return 1; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 354 | ▶ | sub CLOSE { | ||||||||||||||||||||||||||
|
Subroutine
CLOSE: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 117 | 354 | my $self = shift; | |||||||||||||||||||||||||||
| 118 | 354 | my $buf_ref = $self->{buf_ref}; | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 354 | ▶ | if ( $self->{in_header} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 6 | ▶ | if ( $$buf_ref ne '' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | # A line of header text ending CRLF would not have been | ||||||||||||||||||||||||||||
| 124 | # processed yet since before we couldn't tell if it was | ||||||||||||||||||||||||||||
| 125 | # the complete header. Now that we're in CLOSE, we can | ||||||||||||||||||||||||||||
| 126 | # finish the header... | ||||||||||||||||||||||||||||
| 127 | 5 | $$buf_ref =~ s/\015\012\z//s; | |||||||||||||||||||||||||||
| 128 | 5 | $self->add_header("$$buf_ref\015\012"); | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | 6 | $self->finish_header; | |||||||||||||||||||||||||||
| 131 | 6 | $self->{in_header} = 0; | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | else { | ||||||||||||||||||||||||||||
| 134 | 348 | ▶ | if ( $$buf_ref ne '' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | 0 | $self->add_body($$buf_ref); | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | 354 | $$buf_ref = ''; | |||||||||||||||||||||||||||
| 139 | 354 | $self->finish_body; | |||||||||||||||||||||||||||
| 140 | 354 | return 1; | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 0 | ▶ | sub add_header { | ||||||||||||||||||||||||||
|
Subroutine
add_header: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 144 | 0 | die 'add_header not implemented'; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 0 | ▶ | sub finish_header { | ||||||||||||||||||||||||||
|
Subroutine
finish_header: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 148 | 0 | die 'finish_header not implemented'; | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 0 | ▶ | sub add_body { | ||||||||||||||||||||||||||
|
Subroutine
add_body: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 152 | 0 | die 'add_body not implemented'; | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 0 | ▶ | sub finish_body { | ||||||||||||||||||||||||||
|
Subroutine
finish_body: not called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | # do nothing by default | ||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 0 | ▶ | sub reset { | ||||||||||||||||||||||||||
|
Subroutine
reset: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 161 | 0 | carp 'reset not implemented'; | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 20 | 1; | |||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | __END__ | ||||||||||||||||||||||||||||