| 1 | # SMB Perl library, Copyright (C) 2014 Mikhael Goikhman, migo@cpan.org | ||||||||||||
| 2 | # | ||||||||||||
| 3 | # This program is free software: you can redistribute it and/or modify | ||||||||||||
| 4 | # it under the terms of the GNU General Public License as published by | ||||||||||||
| 5 | # the Free Software Foundation, either version 3 of the License, or | ||||||||||||
| 6 | # (at your option) any later version. | ||||||||||||
| 7 | # | ||||||||||||
| 8 | # This program is distributed in the hope that it will be useful, | ||||||||||||
| 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||||
| 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||||||||||
| 11 | # GNU General Public License for more details. | ||||||||||||
| 12 | # | ||||||||||||
| 13 | # You should have received a copy of the GNU General Public License | ||||||||||||
| 14 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||||||||||
| 15 | |||||||||||||
| 16 | package SMB::Parser; | ||||||||||||
| 17 | |||||||||||||
| 18 | 3 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 19 | 3 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 20 | |||||||||||||
| 21 | 3 | ▶ | use bytes; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 22 | 3 | ▶ | use if (1 << 32 == 1), 'bigint'; # support native uint64 on 32-bit platforms | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 23 | 3 | ▶ | use Encode 'decode'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 24 | |||||||||||||
| 25 | sub new ($$) { | ||||||||||||
| 26 | 3 | ▶ | my $class = shift; | ||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 27 | 3 | ▶ | my $data = shift // ""; | ||||||||||
|
Condition100%2/2
shift() // ""
MC/DC100%1/1
shift() // ""
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||
| 28 | |||||||||||||
| 29 | 3 | my $self = bless {}, $class; | |||||||||||
| 30 | |||||||||||||
| 31 | 3 | return $self->set($data); | |||||||||||
| 32 | } | ||||||||||||
| 33 | |||||||||||||
| 34 | sub reset ($;$) { | ||||||||||||
| 35 | 23 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
reset: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 36 | 23 | ▶ | my $offset = shift || 0; | ||||||||||
|
Condition100%2/2
shift() || 0
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||
| 37 | |||||||||||||
| 38 | 23 | $self->{offset} = $offset; | |||||||||||
| 39 | |||||||||||||
| 40 | 23 | return $self; | |||||||||||
| 41 | } | ||||||||||||
| 42 | |||||||||||||
| 43 | sub set ($$;$) { | ||||||||||||
| 44 | 7 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
set: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 45 | |||||||||||||
| 46 | 7 | $self->{data} = $_[0]; | |||||||||||
| 47 | 7 | $self->{size} = length($_[0]); | |||||||||||
| 48 | |||||||||||||
| 49 | 7 | return $self->reset($_[1]); | |||||||||||
| 50 | } | ||||||||||||
| 51 | |||||||||||||
| 52 | 1 | ▶ | sub data { $_[0]->{data} } | ||||||||||
|
Subroutine
data: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 53 | 2 | ▶ | sub size { $_[0]->{size} } | ||||||||||
|
Subroutine
size: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 54 | 10 | ▶ | sub offset { $_[0]->{offset} } | ||||||||||
|
Subroutine
offset: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 55 | |||||||||||||
| 56 | 3 | my %UINT_MODS = ( | |||||||||||
| 57 | +1 => 'C', | ||||||||||||
| 58 | +2 => 'v', | ||||||||||||
| 59 | +4 => 'V', | ||||||||||||
| 60 | -1 => 'C', | ||||||||||||
| 61 | -2 => 'n', | ||||||||||||
| 62 | -4 => 'N', | ||||||||||||
| 63 | ); | ||||||||||||
| 64 | |||||||||||||
| 65 | sub uint ($$;$) { | ||||||||||||
| 66 | 66 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
uint: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 67 | 66 | my $n_bytes = shift; | |||||||||||
| 68 | 66 | ▶ | my $be_factor = shift() ? -1 : 1; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 69 | |||||||||||||
| 70 | 66 | return unpack($UINT_MODS{$be_factor * $n_bytes}, $self->bytes($n_bytes)); | |||||||||||
| 71 | } | ||||||||||||
| 72 | |||||||||||||
| 73 | sub str ($$;$) { | ||||||||||||
| 74 | 9 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
str: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||
| 75 | 9 | my $n_bytes = shift; | |||||||||||
| 76 | 9 | ▶ | my $enc = shift || 'UTF-16LE'; | ||||||||||
|
Condition50%1/2
shift() || 'UTF-16LE'
MC/DC0%0/1
shift() || 'UTF-16LE'
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||
| 77 | |||||||||||||
| 78 | 9 | return decode($enc, $self->bytes($n_bytes)); | |||||||||||
| 79 | } | ||||||||||||
| 80 | |||||||||||||
| 81 | sub bytes ($$) { | ||||||||||||
| 82 | 90 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
bytes: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 83 | 90 | my $n_bytes = shift; | |||||||||||
| 84 | |||||||||||||
| 85 | my $n_avail = $self->{offset} + $n_bytes > $self->{size} | ||||||||||||
| 86 | 90 | ▶ | ? $self->{size} - $self->{offset} : $n_bytes; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 87 | |||||||||||||
| 88 | 90 | ▶ | my $bytes = $self->{offset} > $self->{size} ? '' : substr($self->{data}, $self->{offset}, $n_avail); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 89 | 90 | $self->{offset} += $n_bytes; | |||||||||||
| 90 | |||||||||||||
| 91 | 90 | return $bytes; | |||||||||||
| 92 | } | ||||||||||||
| 93 | |||||||||||||
| 94 | sub skip ($) { | ||||||||||||
| 95 | 9 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
skip: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 96 | 9 | my $n_bytes = shift; | |||||||||||
| 97 | |||||||||||||
| 98 | 9 | $self->{offset} += $n_bytes; | |||||||||||
| 99 | |||||||||||||
| 100 | 9 | return $self; | |||||||||||
| 101 | } | ||||||||||||
| 102 | |||||||||||||
| 103 | 14 | ▶ | sub uint8 { uint($_[0], 1 ); } | ||||||||||
|
Subroutine
uint8: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 104 | 26 | ▶ | sub uint16 { uint($_[0], 2 ); } | ||||||||||
|
Subroutine
uint16: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 105 | 17 | ▶ | sub uint32 { uint($_[0], 4 ); } | ||||||||||
|
Subroutine
uint32: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 106 | 6 | ▶ | sub uint16_be { uint($_[0], 2, 1); } | ||||||||||
|
Subroutine
uint16_be: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 107 | 3 | ▶ | sub uint32_be { uint($_[0], 4, 1); } | ||||||||||
|
Subroutine
uint32_be: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 108 | 1 | ▶ | sub uint64 { uint32($_[0]) + (uint32($_[0]) << 32); } | ||||||||||
|
Subroutine
uint64: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 109 | 0 | ▶ | sub utf16 { str($_[0], $_[1]); } | ||||||||||
|
Subroutine
utf16: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 110 | 0 | ▶ | sub utf16_be { str($_[0], $_[1], 'UTF-16BE'); } | ||||||||||
|
Subroutine
utf16_be: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 111 | 0 | ▶ | sub fid1 { uint16($_[0]); } | ||||||||||
|
Subroutine
fid1: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 112 | 0 | ▶ | sub fid2 { [ uint64($_[0]), uint64($_[0]) ]; } | ||||||||||
|
Subroutine
fid2: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 113 | |||||||||||||
| 114 | 3 | 1; | |||||||||||
| 115 | |||||||||||||
| 116 | __END__ | ||||||||||||