james289o9 has asked for the wisdom of the Perl Monks concerning the following question:

First off, you guys are awesome here, I have always got help with what i was trying to accomplish. I just hope that one day I can be as good as a programmer as alot of yall :) Ok to the point. I am using perl (active state perl latest build 64 bit). I have tried over and over to accomplish this. I have a file. I open the file and then binmode it. In the beginning of this file there are static references to the actual data. The references are as follows: file location, and file size. I need to setup a script that will find the file location and the filesize, then seek to that spot in the file and extract it. I can setup almost everything myself, but I just cannot get my head around how to accomplish this. here is a sort of flow chart if you will:
open file binmode file seek to static reference offset (file location) in file read a predefined byte length seek to static reference offset (file size) in file read a predefined byte length use that info (file location) to goto to the actual data. then extract (file size)
Hopefully you understand what i am trying to accomplish. Its sort of like a dynamic extractor. It extract data based of the information it finds in the static references.

EDIT:
Thank you all for helping. i finally got it to work like this:
open(my $infile, "<", "./file") or die "Cannot open file: $!"; # GET FILESIZE AND LOCATION $buffer1 = ''; $buffer2 = ''; seek $infile, 0x15, 0; read $infile, $location, 0x03; seek ($infile, 0x1D, 0); read ($infile, $filesize, 0x03); $location =~ s/(.)/sprintf("%02x",ord($1))/eg; $filesize =~ s/(.)/sprintf("%02x",ord($1))/eg; # GET FILENAME seek $infile, 0x20, 0; read $infile, $filename, 0x25; $filename =~ s/\0*$//g; print $filename, "\n"; my $directory = "extracted\\"; system "mkdir $directory"; # EXTRACT FILE open($newfile, '>', "extracted//$filename"); sysseek $infile, hex($location), 0; sysread $infile, $new, hex($filesize); syswrite $newfile, $new;

Thanks to everyone who took their time to help me :)

Replies are listed 'Best First'.
Re: dynamic extractor based off static references in file (perl)
by roboticus (Chancellor) on Dec 05, 2013 at 19:53 UTC

    james28909:

    What's the problem? It appears like you know what the task is, and the names of the primary functions you need (seek, read). Your pseudocode looks reasonable, so what's the problem?

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      I have this code so far:
      open(my $infile, '<', "./file") or die "Cannot open file: $!"; binmode($infile); open(my $outfile, '>', "./reference1") or die "Cannot create file: $!" +; binmode($outfile); my $file = $infile; my $buffer = ''; sysseek $file, 0x15, 0; sysread $file, $buffer, 0x03; syswrite $outfile, $buffer; close $outfile; system ( 'pause' );
      I can get the first reference and I should be able to get the second reference without trouble. I am having problems figuring out how to put the $buffer into $reference.
      i tried this but it didnt work to well ofcourse:
      my $reference = $buffer; print $reference;
      it prints the data from $infile, but not in binary form. i guess my question really is, how can i put $buffer (binary data) into $location? any clarifications on what to do would be very much appreciated.

        it prints the data from $infile, but not in binary form

        Sure it does, you used binmode on both filehandles, you read the data, its binary form, there is no doubt; if you need a different format you'll need to do something about it

Re: dynamic extractor based off static references in file (perl)
by GrandFather (Saint) on Dec 05, 2013 at 20:00 UTC

    So, instead of sketching why don't you draw an outline? Something like:

    use strict; use warnings; my $fileName = "sample.dat"; open my $inFile, '<:raw', $fileName or die "Can't open '$fileName': $! +\n"; my $loc = 23; my $len = 20; my $bytes; seek $inFile, $loc, 0; read $inFile, $bytes, $len; ...
    True laziness is hard work
      The main reason i cant do it like this is because i cannot put "04C0" into $loc and then call $loc in seek. This is where i have been stuck at for a while now. i want to put a hex reference "0x04C0" into $loc. but that doesnt work because it has a letter in it.
Re: dynamic extractor based off static references in file (perl)
by educated_foo (Vicar) on Dec 06, 2013 at 01:18 UTC
    ok im back lol.
    You're much more likely to get helpful answers here if you do your best to use proper grammar, capitalization, punctuation, and formatting. What you wrote looks like a random collection of text messages concatenated into a single blob. It's painful to read, and suggests that you didn't really bother to spend much time on it, so most people won't bother. I'm happy to take some of my time to help -- otherwise I wouldn't be here -- but you need to show that you've taken some of yours.

    That said, you'll probably want to read about the "unpack" and "seek" operators.

      You're much more likely to get helpful answers here if you do your best to use proper grammar, capitalization, punctuation, and formatting. What you wrote looks like a random collection of text messages concatenated into a single blob. It's painful to read, and suggests that you didn't really bother to spend much time on it, so most people won't bother. I'm happy to take some of my time to help -- otherwise I wouldn't be here -- but you need to show that you've taken some of yours.

      I find that changing line-height does wonders for legibility . Sure its still got the same grammar, but just like a paper you can read it from a podium ... not overwhelming or headache inducing from formatting :D

      I use themes for Anonymous Monk or try out themes without changing display settings to add jQuery to a page (one click) then click this lineheight3em bookmarklet to turn blobs readable

      javascript:(function(){ $('.doctext').css({'line-height':'3em'}); $('.reply-body').css({'line-height':'3em'}); $('.code').css({'line-height':'1em'}); })()