Hello my friends,

I'd like to extract a png-file out of a huge file like a memory dump. In this example the png-file sits an the very beginning, so I just have to search for the end-string ("IEND"-string).

My idea is to read the file char-by-char, then read 4 bytes ahead and check these 4 bytes for the matching "IEND"-string, then put the pointer back to the old position and write the char into the new file.

Unfortunately it works only when the matching string is located not far from the beginning of the file. My test-png sizes 5.5kb and the code exits after 193 bytes in the middle of nowhere.

What am I doing wrong?

#!/usr/bin/env perl use strict; use warnings; my $dateiread = "/home/ni/OUTPUT/test.png"; my $dateiwrite = "/home/ni/OUTPUT/test2.png"; my $buffer; my $zeichen; my $bytes = 4; open (my $handle1, "<", $dateiread); open (my $handle2, ">", $dateiwrite); while (my $zeichen = getc($handle1)) { seek($handle1, -1, 1); read($handle1, $buffer, $bytes); if ($buffer =~ /IEND/g) { print $handle2 $buffer; exit; } seek($handle1, -3, 1); print $handle2 $zeichen; } close ($handle1); close ($handle2);

Another question:
is my method suitable for handling large dumpfiles (2gb for example) or is there a faster one?

Sincerely,
Ni

In reply to File extraction 2nd try by knight.of.ni

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.