| line | stmt | bran | cond | sub | pod | time | code | 
| 1 | 3 |  |  | 3 |  | 1140 | use 5.006; | 
|  | 3 |  |  |  |  | 13 |  | 
| 2 | 3 |  |  | 3 |  | 16 | use strict; | 
|  | 3 |  |  |  |  | 6 |  | 
|  | 3 |  |  |  |  | 69 |  | 
| 3 | 3 |  |  | 3 |  | 15 | use warnings; | 
|  | 3 |  |  |  |  | 5 |  | 
|  | 3 |  |  |  |  | 200 |  | 
| 4 |  |  |  |  |  |  |  | 
| 5 |  |  |  |  |  |  | package ELF::Extract::Sections::Section; | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | # ABSTRACT:  An Objective reference to a section in an ELF file. | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | our $VERSION = '1.001002'; # TRIAL | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | our $AUTHORITY = 'cpan:KENTNL'; # AUTHORITY | 
| 12 |  |  |  |  |  |  |  | 
| 13 | 3 |  |  | 3 |  | 731 | use Moose; | 
|  | 3 |  |  |  |  | 420355 |  | 
|  | 3 |  |  |  |  | 24 |  | 
| 14 |  |  |  |  |  |  |  | 
| 15 | 3 |  |  | 3 |  | 18321 | use Carp qw( croak ); | 
|  | 3 |  |  |  |  | 6 |  | 
|  | 3 |  |  |  |  | 200 |  | 
| 16 | 3 |  |  | 3 |  | 870 | use MooseX::Has::Sugar 0.0300; | 
|  | 3 |  |  |  |  | 706 |  | 
|  | 3 |  |  |  |  | 22 |  | 
| 17 | 3 |  |  | 3 |  | 905 | use MooseX::Types::Moose                ( ':all', ); | 
|  | 3 |  |  |  |  | 59031 |  | 
|  | 3 |  |  |  |  | 30 |  | 
| 18 | 3 |  |  | 3 |  | 24415 | use ELF::Extract::Sections::Meta::Types ( ':all', ); | 
|  | 3 |  |  |  |  | 8 |  | 
|  | 3 |  |  |  |  | 27 |  | 
| 19 | 3 |  |  | 3 |  | 4282 | use MooseX::Types::Path::Tiny           ( 'File', ); | 
|  | 3 |  |  |  |  | 145898 |  | 
|  | 3 |  |  |  |  | 24 |  | 
| 20 | 3 |  |  | 3 |  | 8380 | use MooseX::Params::Validate            (qw( validated_list )); | 
|  | 3 |  |  |  |  | 71735 |  | 
|  | 3 |  |  |  |  | 26 |  | 
| 21 |  |  |  |  |  |  |  | 
| 22 | 3 |  |  | 3 |  | 569 | use overload '""' => \&to_string; | 
|  | 3 |  |  |  |  | 31 |  | 
|  | 3 |  |  |  |  | 35 |  | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  |  | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  | has source => ( isa => File, ro, required, coerce, ); | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  |  | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  |  | 
| 36 |  |  |  |  |  |  |  | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | has name => ( isa => Str, ro, required ); | 
| 39 |  |  |  |  |  |  |  | 
| 40 |  |  |  |  |  |  |  | 
| 41 |  |  |  |  |  |  |  | 
| 42 |  |  |  |  |  |  |  | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  |  | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | has offset => ( isa => Int, ro, required ); | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  |  | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  |  | 
| 52 |  |  |  |  |  |  |  | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  | has size => ( isa => Int, ro, required ); | 
| 55 |  |  |  |  |  |  |  | 
| 56 |  |  |  |  |  |  | __PACKAGE__->meta->make_immutable; | 
| 57 | 3 |  |  | 3 |  | 428 | no Moose; | 
|  | 3 |  |  |  |  | 5 |  | 
|  | 3 |  |  |  |  | 20 |  | 
| 58 |  |  |  |  |  |  |  | 
| 59 |  |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  |  | 
| 63 |  |  |  |  |  |  |  | 
| 64 |  |  |  |  |  |  |  | 
| 65 |  |  |  |  |  |  |  | 
| 66 |  |  |  |  |  |  |  | 
| 67 |  |  |  |  |  |  |  | 
| 68 |  |  |  |  |  |  |  | 
| 69 |  |  |  |  |  |  |  | 
| 70 |  |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  |  | 
| 72 |  |  |  |  |  |  |  | 
| 73 |  |  |  |  |  |  |  | 
| 74 |  |  |  |  |  |  |  | 
| 75 |  |  |  |  |  |  |  | 
| 76 |  |  |  |  |  |  |  | 
| 77 |  |  |  |  |  |  | sub to_string { | 
| 78 | 0 |  |  | 0 | 1 |  | my ( $self, ) = @_; | 
| 79 | 0 |  |  |  |  |  | return sprintf | 
| 80 |  |  |  |  |  |  | q{[ Section %s of size %s in %s @ %x to %x ]}, | 
| 81 |  |  |  |  |  |  | $self->name, $self->size, $self->source, $self->offset, | 
| 82 |  |  |  |  |  |  | $self->offset + $self->size, | 
| 83 |  |  |  |  |  |  | ; | 
| 84 |  |  |  |  |  |  | } | 
| 85 |  |  |  |  |  |  |  | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  |  | 
| 88 |  |  |  |  |  |  |  | 
| 89 |  |  |  |  |  |  |  | 
| 90 |  |  |  |  |  |  |  | 
| 91 |  |  |  |  |  |  |  | 
| 92 |  |  |  |  |  |  |  | 
| 93 |  |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  |  | 
| 95 |  |  |  |  |  |  |  | 
| 96 |  |  |  |  |  |  |  | 
| 97 |  |  |  |  |  |  |  | 
| 98 |  |  |  |  |  |  |  | 
| 99 |  |  |  |  |  |  |  | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  |  | 
| 102 |  |  |  |  |  |  |  | 
| 103 |  |  |  |  |  |  |  | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  |  | 
| 106 |  |  |  |  |  |  |  | 
| 107 |  |  |  |  |  |  |  | 
| 108 |  |  |  |  |  |  | sub compare { | 
| 109 | 0 |  |  | 0 | 1 |  | my ( $self, $other, $field ) = validated_list( | 
| 110 |  |  |  |  |  |  | \@_, | 
| 111 |  |  |  |  |  |  | other => { isa => class_type('ELF::Extract::Sections::Section') }, | 
| 112 |  |  |  |  |  |  | field => { isa => FilterField, }, | 
| 113 |  |  |  |  |  |  | ); | 
| 114 | 0 | 0 |  |  |  |  | if ( 'name' eq $field ) { | 
| 115 | 0 |  |  |  |  |  | return ( $self->name cmp $other->name ); | 
| 116 |  |  |  |  |  |  | } | 
| 117 | 0 | 0 |  |  |  |  | if ( 'offset' eq $field ) { | 
| 118 | 0 |  |  |  |  |  | return ( $self->offset <=> $other->offset ); | 
| 119 |  |  |  |  |  |  | } | 
| 120 | 0 | 0 |  |  |  |  | if ( 'size' eq $field ) { | 
| 121 | 0 |  |  |  |  |  | return ( $self->size <=> $other->size ); | 
| 122 |  |  |  |  |  |  | } | 
| 123 | 0 |  |  |  |  |  | return; | 
| 124 |  |  |  |  |  |  | } | 
| 125 |  |  |  |  |  |  |  | 
| 126 |  |  |  |  |  |  |  | 
| 127 |  |  |  |  |  |  |  | 
| 128 |  |  |  |  |  |  |  | 
| 129 |  |  |  |  |  |  |  | 
| 130 |  |  |  |  |  |  |  | 
| 131 |  |  |  |  |  |  |  | 
| 132 |  |  |  |  |  |  |  | 
| 133 |  |  |  |  |  |  |  | 
| 134 |  |  |  |  |  |  |  | 
| 135 |  |  |  |  |  |  |  | 
| 136 |  |  |  |  |  |  |  | 
| 137 |  |  |  |  |  |  |  | 
| 138 |  |  |  |  |  |  |  | 
| 139 |  |  |  |  |  |  |  | 
| 140 |  |  |  |  |  |  |  | 
| 141 |  |  |  |  |  |  |  | 
| 142 |  |  |  |  |  |  | sub write_to { | 
| 143 | 0 |  |  | 0 | 1 |  | my ( $self, $file ) = validated_list( | 
| 144 |  |  |  |  |  |  | \@_,    # | 
| 145 |  |  |  |  |  |  | file => { isa => File, optional => 0, coerce => 1 }, | 
| 146 |  |  |  |  |  |  | ); | 
| 147 | 0 |  |  |  |  |  | my $fh = $self->source->openr; | 
| 148 | 0 |  |  |  |  |  | seek $fh, $self->offset, 0; | 
| 149 | 0 |  |  |  |  |  | my $output     = $file->openw; | 
| 150 | 0 |  |  |  |  |  | my $chunksize  = 1024; | 
| 151 | 0 |  |  |  |  |  | my $bytes_left = $self->size; | 
| 152 | 0 | 0 |  |  |  |  | my $chunk      = ( $bytes_left < $chunksize ) ? $bytes_left : $chunksize; | 
| 153 | 0 |  |  |  |  |  | while ( read $fh, my $buffer, $chunk ) { | 
| 154 | 0 | 0 |  |  |  |  | print {$output} $buffer or Carp::croak("Write to $file failed"); | 
|  | 0 |  |  |  |  |  |  | 
| 155 | 0 |  |  |  |  |  | $bytes_left -= $chunksize; | 
| 156 | 0 | 0 |  |  |  |  | $chunk = ( $bytes_left < $chunksize ) ? $bytes_left : $chunksize; | 
| 157 |  |  |  |  |  |  | } | 
| 158 | 0 |  |  |  |  |  | return 1; | 
| 159 |  |  |  |  |  |  | } | 
| 160 |  |  |  |  |  |  |  | 
| 161 |  |  |  |  |  |  |  | 
| 162 |  |  |  |  |  |  |  | 
| 163 |  |  |  |  |  |  |  | 
| 164 |  |  |  |  |  |  |  | 
| 165 |  |  |  |  |  |  |  | 
| 166 |  |  |  |  |  |  |  | 
| 167 |  |  |  |  |  |  |  | 
| 168 |  |  |  |  |  |  |  | 
| 169 |  |  |  |  |  |  | sub contents { | 
| 170 | 0 |  |  | 0 | 1 |  | my ($self) = @_; | 
| 171 | 0 |  |  |  |  |  | my $fh = $self->source->openr; | 
| 172 | 0 |  |  |  |  |  | seek $fh, $self->offset, 0; | 
| 173 | 0 |  |  |  |  |  | my $b; | 
| 174 | 0 |  |  |  |  |  | read $fh, $b, $self->size; | 
| 175 | 0 |  |  |  |  |  | return $b; | 
| 176 |  |  |  |  |  |  | } | 
| 177 |  |  |  |  |  |  |  | 
| 178 |  |  |  |  |  |  | 1; | 
| 179 |  |  |  |  |  |  |  | 
| 180 |  |  |  |  |  |  | __END__ | 
| 181 |  |  |  |  |  |  |  | 
| 182 |  |  |  |  |  |  | =pod | 
| 183 |  |  |  |  |  |  |  | 
| 184 |  |  |  |  |  |  | =encoding UTF-8 | 
| 185 |  |  |  |  |  |  |  | 
| 186 |  |  |  |  |  |  | =head1 NAME | 
| 187 |  |  |  |  |  |  |  | 
| 188 |  |  |  |  |  |  | ELF::Extract::Sections::Section - An Objective reference to a section in an ELF file. | 
| 189 |  |  |  |  |  |  |  | 
| 190 |  |  |  |  |  |  | =head1 VERSION | 
| 191 |  |  |  |  |  |  |  | 
| 192 |  |  |  |  |  |  | version 1.001002 | 
| 193 |  |  |  |  |  |  |  | 
| 194 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 195 |  |  |  |  |  |  |  | 
| 196 |  |  |  |  |  |  | use ELF::Extract::Sections::Section; | 
| 197 |  |  |  |  |  |  |  | 
| 198 |  |  |  |  |  |  | my $s = ELF::Extract::Sections::Section->new( | 
| 199 |  |  |  |  |  |  | source => '/foo/bar.pl', | 
| 200 |  |  |  |  |  |  | name   => '.comment', | 
| 201 |  |  |  |  |  |  | offset => 45670, | 
| 202 |  |  |  |  |  |  | size   => 1244, | 
| 203 |  |  |  |  |  |  | ); | 
| 204 |  |  |  |  |  |  |  | 
| 205 |  |  |  |  |  |  | # prints a human friendly description | 
| 206 |  |  |  |  |  |  | print $s->to_string; | 
| 207 |  |  |  |  |  |  |  | 
| 208 |  |  |  |  |  |  | # does likewise. | 
| 209 |  |  |  |  |  |  | print "$s"; | 
| 210 |  |  |  |  |  |  |  | 
| 211 |  |  |  |  |  |  | # Compare with another section ( preferably in the same file, meaningless otherwise ) | 
| 212 |  |  |  |  |  |  | if( $s->compare( $y , 'name' ) ){ | 
| 213 |  |  |  |  |  |  |  | 
| 214 |  |  |  |  |  |  | } | 
| 215 |  |  |  |  |  |  |  | 
| 216 |  |  |  |  |  |  | # Unimplemented | 
| 217 |  |  |  |  |  |  | $s->write_to ( file => '/tmp/out.txt' ); | 
| 218 |  |  |  |  |  |  |  | 
| 219 |  |  |  |  |  |  | # Retuns the sections contents as a string | 
| 220 |  |  |  |  |  |  | print $s->contents; | 
| 221 |  |  |  |  |  |  |  | 
| 222 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 223 |  |  |  |  |  |  |  | 
| 224 |  |  |  |  |  |  | Generally Intended for use by L<ELF::Extract::Sections> as a meta-structure for tracking data, | 
| 225 |  |  |  |  |  |  | but generated objects are returned to you for you to  deal with | 
| 226 |  |  |  |  |  |  |  | 
| 227 |  |  |  |  |  |  | =head1 METHODS | 
| 228 |  |  |  |  |  |  |  | 
| 229 |  |  |  |  |  |  | =head2 C<new> | 
| 230 |  |  |  |  |  |  |  | 
| 231 |  |  |  |  |  |  | my $section = ELF::Extract::Sections::Section->new( %ATTRIBUTES ); | 
| 232 |  |  |  |  |  |  |  | 
| 233 |  |  |  |  |  |  | 4 Parameters, all required. | 
| 234 |  |  |  |  |  |  |  | 
| 235 |  |  |  |  |  |  | Returns an C<ELF::Extract::Sections::Section> object. | 
| 236 |  |  |  |  |  |  |  | 
| 237 |  |  |  |  |  |  | =head2 C<to_string> | 
| 238 |  |  |  |  |  |  |  | 
| 239 |  |  |  |  |  |  | my $string = $section->to_string; | 
| 240 |  |  |  |  |  |  |  | 
| 241 |  |  |  |  |  |  | returns C<Str> description of the object | 
| 242 |  |  |  |  |  |  |  | 
| 243 |  |  |  |  |  |  | [ Section {name} of size {size} in {file} @ {start} to {stop} ] | 
| 244 |  |  |  |  |  |  |  | 
| 245 |  |  |  |  |  |  | =head2 C<compare> | 
| 246 |  |  |  |  |  |  |  | 
| 247 |  |  |  |  |  |  | my $cmp_result = $section->compare( other => $other, field => $field ); | 
| 248 |  |  |  |  |  |  |  | 
| 249 |  |  |  |  |  |  | 2 Parameters, both required | 
| 250 |  |  |  |  |  |  |  | 
| 251 |  |  |  |  |  |  | =over 4 | 
| 252 |  |  |  |  |  |  |  | 
| 253 |  |  |  |  |  |  | =item other | 
| 254 |  |  |  |  |  |  |  | 
| 255 |  |  |  |  |  |  | C<ELF::Extract::Sections::Section>: Item to compare with | 
| 256 |  |  |  |  |  |  |  | 
| 257 |  |  |  |  |  |  | =item field | 
| 258 |  |  |  |  |  |  |  | 
| 259 |  |  |  |  |  |  | C<Str['name','offset','size']>: Field to compare with. | 
| 260 |  |  |  |  |  |  |  | 
| 261 |  |  |  |  |  |  | =back | 
| 262 |  |  |  |  |  |  |  | 
| 263 |  |  |  |  |  |  | returns C<Int> of comparison result, between -1 and 1 | 
| 264 |  |  |  |  |  |  |  | 
| 265 |  |  |  |  |  |  | =head2 C<write_to> | 
| 266 |  |  |  |  |  |  |  | 
| 267 |  |  |  |  |  |  | my $boolean = $section->write_to( file => $file ); | 
| 268 |  |  |  |  |  |  |  | 
| 269 |  |  |  |  |  |  | B<UNIMPLEMENTED AS OF YET> | 
| 270 |  |  |  |  |  |  |  | 
| 271 |  |  |  |  |  |  | =over 4 | 
| 272 |  |  |  |  |  |  |  | 
| 273 |  |  |  |  |  |  | =item file | 
| 274 |  |  |  |  |  |  |  | 
| 275 |  |  |  |  |  |  | C<Str>|C<Path::Tiny>: File target to write section contents to. | 
| 276 |  |  |  |  |  |  |  | 
| 277 |  |  |  |  |  |  | =back | 
| 278 |  |  |  |  |  |  |  | 
| 279 |  |  |  |  |  |  | =head2 C<contents> | 
| 280 |  |  |  |  |  |  |  | 
| 281 |  |  |  |  |  |  | my $string = $section->contents; | 
| 282 |  |  |  |  |  |  |  | 
| 283 |  |  |  |  |  |  | returns C<Str> of binary data read out of file. | 
| 284 |  |  |  |  |  |  |  | 
| 285 |  |  |  |  |  |  | =head1 ATTRIBUTES | 
| 286 |  |  |  |  |  |  |  | 
| 287 |  |  |  |  |  |  | =head2 C<source> | 
| 288 |  |  |  |  |  |  |  | 
| 289 |  |  |  |  |  |  | C<Str>|C<Path::Tiny>: Either a String or a Path::Tiny instance pointing to the file in mention. | 
| 290 |  |  |  |  |  |  |  | 
| 291 |  |  |  |  |  |  | =head2 C<name> | 
| 292 |  |  |  |  |  |  |  | 
| 293 |  |  |  |  |  |  | C<Str>: The ELF Section Name | 
| 294 |  |  |  |  |  |  |  | 
| 295 |  |  |  |  |  |  | =head2 C<offset> | 
| 296 |  |  |  |  |  |  |  | 
| 297 |  |  |  |  |  |  | C<Int>: Position in bytes relative to the start of the file. | 
| 298 |  |  |  |  |  |  |  | 
| 299 |  |  |  |  |  |  | =head2 C<size> | 
| 300 |  |  |  |  |  |  |  | 
| 301 |  |  |  |  |  |  | C<Int>: The ELF Section Size | 
| 302 |  |  |  |  |  |  |  | 
| 303 |  |  |  |  |  |  | =head1 AUTHOR | 
| 304 |  |  |  |  |  |  |  | 
| 305 |  |  |  |  |  |  | Kent Fredric <kentnl@cpan.org> | 
| 306 |  |  |  |  |  |  |  | 
| 307 |  |  |  |  |  |  | =head1 COPYRIGHT AND LICENSE | 
| 308 |  |  |  |  |  |  |  | 
| 309 |  |  |  |  |  |  | This software is copyright (c) 2015 by Kent Fredric <kentfredric@gmail.com>. | 
| 310 |  |  |  |  |  |  |  | 
| 311 |  |  |  |  |  |  | This is free software; you can redistribute it and/or modify it under | 
| 312 |  |  |  |  |  |  | the same terms as the Perl 5 programming language system itself. | 
| 313 |  |  |  |  |  |  |  | 
| 314 |  |  |  |  |  |  | =cut |