| 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 | use strict; | ||
| 19 | use warnings; | ||
| 20 | |||
| 21 | use bytes; | ||
| 22 | use if (1 << 32 == 1), 'bigint'; # support native uint64 on 32-bit platforms | ||
| 23 | use Encode 'decode'; | ||
| 24 | |||
| 25 | sub new ($$) { | ||
| 26 | my $class = shift; | ||
| 27 | my $data = shift // ""; | ||
| 28 | |||
| 29 | my $self = bless {}, $class; | ||
| 30 | |||
| 31 | return $self->set($data); | ||
| 32 | } | ||
| 33 | |||
| 34 | sub reset ($;$) { | ||
| 35 | my $self = shift; | ||
| 36 | my $offset = shift || 0; | ||
| 37 | |||
| 38 | $self->{offset} = $offset; | ||
| 39 | |||
| 40 | return $self; | ||
| 41 | } | ||
| 42 | |||
| 43 | sub set ($$;$) { | ||
| 44 | my $self = shift; | ||
| 45 | |||
| 46 | $self->{data} = $_[0]; | ||
| 47 | $self->{size} = length($_[0]); | ||
| 48 | |||
| 49 | return $self->reset($_[1]); | ||
| 50 | } | ||
| 51 | |||
| 52 | sub data { $_[0]->{data} } | ||
| 53 | sub size { $_[0]->{size} } | ||
| 54 | sub offset { $_[0]->{offset} } | ||
| 55 | |||
| 56 | 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 | my $self = shift; | ||
| 67 | my $n_bytes = shift; | ||
| 68 | my $be_factor = shift() ? -1 : 1; | ||
| 69 | |||
| 70 | return unpack($UINT_MODS{$be_factor * $n_bytes}, $self->bytes($n_bytes)); | ||
| 71 | } | ||
| 72 | |||
| 73 | sub str ($$;$) { | ||
| 74 | my $self = shift; | ||
| 75 | my $n_bytes = shift; | ||
| 76 | my $enc = shift || 'UTF-16LE'; | ||
| 77 | |||
| 78 | return decode($enc, $self->bytes($n_bytes)); | ||
| 79 | } | ||
| 80 | |||
| 81 | sub bytes ($$) { | ||
| 82 | my $self = shift; | ||
| 83 | my $n_bytes = shift; | ||
| 84 | |||
| 85 | my $n_avail = $self->{offset} + $n_bytes > $self->{size} | ||
| 86 | ? $self->{size} - $self->{offset} : $n_bytes; | ||
| 87 | |||
| 88 | my $bytes = $self->{offset} > $self->{size} ? '' : substr($self->{data}, $self->{offset}, $n_avail); | ||
| 89 | $self->{offset} += $n_bytes; | ||
| 90 | |||
| 91 | return $bytes; | ||
| 92 | } | ||
| 93 | |||
| 94 | sub skip ($) { | ||
| 95 | my $self = shift; | ||
| 96 | my $n_bytes = shift; | ||
| 97 | |||
| 98 | $self->{offset} += $n_bytes; | ||
| 99 | |||
| 100 | return $self; | ||
| 101 | } | ||
| 102 | |||
| 103 | sub uint8 { uint($_[0], 1 ); } | ||
| 104 | sub uint16 { uint($_[0], 2 ); } | ||
| 105 | sub uint32 { uint($_[0], 4 ); } | ||
| 106 | sub uint16_be { uint($_[0], 2, 1); } | ||
| 107 | sub uint32_be { uint($_[0], 4, 1); } | ||
| 108 | sub uint64 { uint32($_[0]) + (uint32($_[0]) << 32); } | ||
| 109 | sub utf16 { str($_[0], $_[1]); } | ||
| 110 | sub utf16_be { str($_[0], $_[1], 'UTF-16BE'); } | ||
| 111 | sub fid1 { uint16($_[0]); } | ||
| 112 | sub fid2 { [ uint64($_[0]), uint64($_[0]) ]; } | ||
| 113 | |||
| 114 | 1; | ||
| 115 | |||
| 116 | __END__ |