ELF::Extract::Sections::Section - An Objective reference to a section in an ELF file.


ELF-Extract-Sections documentation Contained in the ELF-Extract-Sections distribution.

Index


Code Index:

NAME

Top

ELF::Extract::Sections::Section - An Objective reference to a section in an ELF file.

VERSION

Top

version 0.02071411

SYNOPSIS

Top

  use ELF::Extract::Sections::Section;

  my $s = ELF::Extract::Sections::Section->new(
      source => '/foo/bar.pl',
      name   => '.comment',
      offset => 45670,
      size   => 1244,
  );

  # prints a human friendly description
  print $s->to_string;

  # does likewise.
  print "$s";

  # Compare with another section ( preferably in the same file, meaningless otherwise )
  if( $s->compare( $y , 'name' ) ){

  }

  # Unimplemented
  $s->write_to ( file => '/tmp/out.txt' );

  # Retuns the sections contents as a string
  print $s->contents;

DESCRIPTION

Top

Generally Intended for use by ELF::Extract::Sections as a meta-structure for tracking data, but generated objects are returned to you for you to deal with

PUBLIC ATTRIBUTES

Top

source

Str|Path::Class::File: Either a String or a Path::Class instance pointing to the file in mention.

name

Str: The ELF Section Name

offset

Int: Position in bytes relative to the start of the file.

size

Int: The ELF Section Size

PUBLIC METHODS

Top

-> new ( %ATTRIBUTES )

4 Parameters, all required.

Returns an ELF::Extract::Sections::Section object.

-> to_string

returns Str description of the object

    [ Section {name} of size {size} in {file} @ {start} to {stop} ]

-> compare ( other => $other, field => $field )

2 Parameters, both required

other

ELF::Extract::Sections::Section: Item to compare with

field

Str['name','offset','size']: Field to compare with.

returns Int of comparison result, between -1 and 1

-> write_to ( file => $file )

UNIMPLEMENTED AS OF YET

file

Str|Path::Class::File: File target to write section contents to.

-> contents

returns Str of binary data read out of file.

AUTHOR

Top

Kent Fredric <kentnl@cpan.org>

COPYRIGHT AND LICENSE

Top


ELF-Extract-Sections documentation Contained in the ELF-Extract-Sections distribution.

use strict;
use warnings;

package ELF::Extract::Sections::Section;
BEGIN {
  $ELF::Extract::Sections::Section::VERSION = '0.02071411';
}

# ABSTRACT:  An Objective reference to a section in an ELF file.

use MooseX::Declare;

class ELF::Extract::Sections::Section {



    use MooseX::Has::Sugar 0.0300;
    use MooseX::Types::Moose                ( ':all', );
    use ELF::Extract::Sections::Meta::Types ( ':all', );
    use MooseX::Types::Path::Class          ( 'File', );

    use overload '""' => \&to_string;



    has source => ( isa => File, ro, required, coerce, );


    has name   => ( isa => Str,  ro, required );


    has offset => ( isa => Int,  ro, required );


    has size   => ( isa => Int,  ro, required );




    method to_string ( Any $other?, Bool $polarity? ) {
        return sprintf
          q{[ Section %s of size %s in %s @ %x to %x ]},
          $self->name, $self->size, $self->source, $self->offset, $self->offset + $self->size,
          ;
      };


    method compare ( ELF::Extract::Sections::Section :$other! , FilterField :$field! ){
        if ( $field eq 'name' ) {
            return ( $self->name cmp $other->name );
        }
        if ( $field eq 'offset' ) {
            return ( $self->offset <=> $other->offset );
        }
        if ( $field eq 'size' ) {
            return ( $self->size <=> $other->size );
        }
        return;
    };


    method write_to( File :$file does coerce  ){
        my $fh = $self->source->openr;
        seek $fh, $self->offset, 0;
        my $output     = $file->openw;
        my $chunksize  = 1024;
        my $bytes_left = $self->size;
        my $chunk      = ( $bytes_left < $chunksize ) ? $bytes_left : $chunksize;
        while ( read $fh, my $buffer, $chunk  ) {
            print {$output} $buffer or Carp::croak("Write to $file failed");
            $bytes_left -= $chunksize;
            $chunk = ( $bytes_left < $chunksize ) ? $bytes_left : $chunksize;
        }
        return 1;
    };


    method contents {
        my $fh = $self->source->openr;
        seek $fh, $self->offset, 0;
        my $b;
        read $fh, $b, $self->size;
        return $b;
    };
};

1;




__END__