| 1 | package File::Type::Builder; | ||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use IO::File; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 1 | our $VERSION = "0.11"; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 10 | 1 | my $class = shift; | |||||||||||||||||||||||||||
| 11 | 1 | my $self = {}; | |||||||||||||||||||||||||||
| 12 | 1 | bless $self, $class; | |||||||||||||||||||||||||||
| 13 | 1 | return $self; | |||||||||||||||||||||||||||
| 14 | } | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 0 | ▶ | sub parse_magic { | ||||||||||||||||||||||||||
|
Subroutine
parse_magic: not called CC=11 SCAR=48.8
Pod
documented
| |||||||||||||||||||||||||||||
| 17 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 18 | 0 | my $data = shift; | |||||||||||||||||||||||||||
| 19 | 0 | my $line = shift; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | # storage | ||||||||||||||||||||||||||||
| 22 | 0 | my $parsed = {}; | |||||||||||||||||||||||||||
| 23 | 0 | my $pattern; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | # offsets | ||||||||||||||||||||||||||||
| 26 | 0 | $data =~ s/^([0-9a-fx]+)\s+//; | |||||||||||||||||||||||||||
| 27 | 0 | $parsed->{offset} = $1; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | # pattern type | ||||||||||||||||||||||||||||
| 30 | 0 | $data =~ s/(byte | short | long | string | date | beshort | | |||||||||||||||||||||||||||
| 31 | belong | bedate | leshort | lelong | ledate)(\s+)?//x; | ||||||||||||||||||||||||||||
| 32 | 0 | $parsed->{pattern_type} = $1; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 0 | ▶ | unless ($parsed->{pattern_type} =~ m/^(string|beshort|belong)/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 0 | return undef; | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | # pattern mask, if exists; buggy? | ||||||||||||||||||||||||||||
| 39 | 0 | ▶ | if ($data =~ m/^\&/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | # warn "pattern mask on line $line\n"; | ||||||||||||||||||||||||||||
| 41 | 0 | $data =~ s/\&([a-z0-9]+)\s+//; | |||||||||||||||||||||||||||
| 42 | 0 | $parsed->{pattern_mask} = $1; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | # handle pattern. Somewhat complex. | ||||||||||||||||||||||||||||
| 46 | 0 | ▶ | PARSE: while ($data =~ s/(\s*\S*\s)//) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | # add data to pattern. stop unless we've got significant whitespace. | ||||||||||||||||||||||||||||
| 48 | 0 | ▶ | $pattern .= $1 unless $1 =~ m/^\s+$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 0 | ▶ | last PARSE unless ($pattern =~ m!\\\s$!); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | # then tidy up | ||||||||||||||||||||||||||||
| 52 | 0 | ▶ | return undef unless defined($pattern); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 0 | ▶ | $pattern =~ s/\s*$// unless $pattern =~ m/\\\s$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 0 | $pattern =~ s/\\(\s)/$1/g; | |||||||||||||||||||||||||||
| 56 | 0 | $pattern =~ s/\\$//g; | |||||||||||||||||||||||||||
| 57 | 0 | $parsed->{pattern} = $pattern; | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | # what's left is the MIME type | ||||||||||||||||||||||||||||
| 60 | 0 | $data =~ s/^\s*(.*)$//; | |||||||||||||||||||||||||||
| 61 | 0 | $parsed->{mime_type} = $1; | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | # check there's nothing undigested | ||||||||||||||||||||||||||||
| 64 | 0 | ▶ | warn "On line $line, remaining '$data'. Using anyway.\n" if length($data); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | # check we've got a mime type to return | ||||||||||||||||||||||||||||
| 67 | 0 | ▶ | if (!length($parsed->{mime_type}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
length $parsed->{"mime_type"} and $parsed->{"mime_type"} =~ m[^[^/]*/[^/]*$]
MC/DC0%0/2
length $parsed->{"mime_type"} and $parsed->{"mime_type"} =~ m[^[^/]*/[^/]*$]
length $parsed->{"mime_type"}
$parsed->{"mime_type"} =~ m[^[^/]*/[^/]*$]
Truth table0%0/3
A and B
A= length $parsed->{"mime_type"}B= $parsed->{"mime_type"} =~ m[^[^/]*/[^/]*$]
| |||||||||||||||||||||||||||||
| 68 | || $parsed->{mime_type} !~ m!^[^/]*/[^/]*$!) { | ||||||||||||||||||||||||||||
| 69 | # warn "On line $line, no or improper MIME type: not used\n"; | ||||||||||||||||||||||||||||
| 70 | 0 | return undef; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 0 | return $parsed; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 0 | ▶ | sub string { | ||||||||||||||||||||||||||
|
Subroutine
string: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 77 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 78 | 0 | my $parsed = shift; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | my $escape = $self->_get_escapes(); | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | # build a code fragment. | ||||||||||||||||||||||||||||
| 83 | 0 | my $code; | |||||||||||||||||||||||||||
| 84 | 0 | my $tab = ''; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 0 | ▶ | if ($parsed->{offset}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 0 | $code = $self->_substr_handling($parsed->{offset}, 1024); | |||||||||||||||||||||||||||
| 88 | 0 | $tab = ' '; | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | # we have to use substr to advance to the anchor | ||||||||||||||||||||||||||||
| 91 | 0 | $code .= ' if (defined $substr && $substr =~ m[^'; | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | } else { | ||||||||||||||||||||||||||||
| 94 | # can just anchor normally | ||||||||||||||||||||||||||||
| 95 | 0 | $code = ' if ($data =~ m[^'; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | # manipulate regex; use File::MMagic code | ||||||||||||||||||||||||||||
| 99 | 0 | my $pattern = $parsed->{pattern}; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | # quote metacharacters | ||||||||||||||||||||||||||||
| 102 | # unless ($pattern =~ m!\\x!) { | ||||||||||||||||||||||||||||
| 103 | 0 | $pattern = quotemeta($pattern); | |||||||||||||||||||||||||||
| 104 | 0 | ▶ | $pattern =~ s/\\\\(.)/$escape->{$1}||$1/eg; | ||||||||||||||||||||||||||
|
Condition0%0/3
$escape->{$1} || $1
MC/DC0%0/2
$escape->{$1} || $1
$escape->{$1}
$1
Truth table0%0/3
A || B
A= $escape->{$1}B= $1
| |||||||||||||||||||||||||||||
| 105 | # } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 0 | $code .= $pattern; | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | # close the [] delimited regex and return mime type | ||||||||||||||||||||||||||||
| 110 | 0 | $code .= ']) {'; | |||||||||||||||||||||||||||
| 111 | 0 | $code .= "\n$tab return q{$parsed->{mime_type}};\n$tab }\n"; | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 0 | ▶ | if ($tab) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | 0 | $code .= "$tab}\n"; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 0 | return $code; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 0 | ▶ | sub be { | ||||||||||||||||||||||||||
|
Subroutine
be: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 121 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 122 | 0 | my $parsed = shift; | |||||||||||||||||||||||||||
| 123 | 0 | my $length = shift; | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | # build both sides of the conditional | ||||||||||||||||||||||||||||
| 126 | 0 | my $offset = $parsed->{offset}; | |||||||||||||||||||||||||||
| 127 | 0 | my $pattern = $parsed->{pattern}; | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | # start with substr handling | ||||||||||||||||||||||||||||
| 130 | 0 | my $code = $self->_substr_handling($offset, $length); | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | # rhs: template | ||||||||||||||||||||||||||||
| 133 | 0 | my $rhs; | |||||||||||||||||||||||||||
| 134 | 0 | ▶ | if ($pattern =~ s/^0x//) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | 0 | $rhs = "pack('H*', '$pattern')"; | |||||||||||||||||||||||||||
| 136 | } else { | ||||||||||||||||||||||||||||
| 137 | # warn "Not sure what this magic is"; | ||||||||||||||||||||||||||||
| 138 | 0 | return undef; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | # build condition | ||||||||||||||||||||||||||||
| 142 | 0 | $code .= " if ($rhs eq ".'$substr ) {'; | |||||||||||||||||||||||||||
| 143 | 0 | $code .= "\n return q{$parsed->{mime_type}};\n }\n }\n"; | |||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 0 | return $code; | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 0 | ▶ | sub _substr_handling { | ||||||||||||||||||||||||||
|
Subroutine
_substr_handling: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 149 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 150 | 0 | my ($offset, $length) = @_; | |||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 0 | my $code = ' if (length $data > '.$offset.") {\n"; | |||||||||||||||||||||||||||
| 153 | 0 | $code .= ' $substr = substr($data, '.$offset.', '.$length.");\n"; | |||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 0 | return $code; | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 0 | ▶ | sub _get_escapes { | ||||||||||||||||||||||||||
|
Subroutine
_get_escapes: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 159 | 0 | return { n => '\n', | |||||||||||||||||||||||||||
| 160 | r => '\r', | ||||||||||||||||||||||||||||
| 161 | b => '\b', | ||||||||||||||||||||||||||||
| 162 | t => '\t', | ||||||||||||||||||||||||||||
| 163 | f => '\f', | ||||||||||||||||||||||||||||
| 164 | x => '\x', | ||||||||||||||||||||||||||||
| 165 | 0 => '\0', | ||||||||||||||||||||||||||||
| 166 | 1 => '\1', | ||||||||||||||||||||||||||||
| 167 | 2 => '\2', | ||||||||||||||||||||||||||||
| 168 | 3 => '\3', | ||||||||||||||||||||||||||||
| 169 | }; | ||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 1 | 1; | |||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | __END__ | ||||||||||||||||||||||||||||