in reply to File extraction 2nd try

If the PNG starts at the start of the dump then Re: Weird file extraction problem still applies - parse the PNG. It'll be much faster than chewing your way through the dump a byte at a time and more reliable than searching a large buffer for a string that may exist in multiple places if you are unlucky with the image.

Sometimes biting the bullet, learning some new stuff and doing the hard yards is what you have to do!

Premature optimization is the root of all job security

Replies are listed 'Best First'.
Re^2: File extraction 2nd try
by knight.of.ni (Initiate) on Jan 19, 2016 at 19:10 UTC
    You are right, chewing bytes is time consuming. But my code is to be applied on files that are larger than my system memory. Maybe reading larger chunks into memory would be faster. I'll test different solutions since that is the best way to get experience.

      Parsing a .PNG file really isn't hard, especially if you don't really care about the content. Here's something to get you started:

      use strict; use warnings; my $file = '...'; open my $pngIn, '<:raw', $file or die "Can't open '$file': $!\n"; read $pngIn, my $header, 8; my ($prefix, $png, $crlf, $ctrlZ, $lf) = unpack "aa3a2aa", $header; die "Bad header prefix\n" if ord $prefix != 0x89; die "Bad header type\n" if $png ne 'PNG'; die "Bad header crlf\n" if $crlf ne "\r\n"; die "Bad header Ctrl-Z\n" if ord $ctrlZ != 0x1a; die "Bad header newline\n" if $lf ne "\n"; while (!eof $pngIn) { read $pngIn, my $chunkHeader, 8 or last; my ($length, $type) = unpack "Na4", $chunkHeader; read $pngIn, my $body, $length + 4 or die "Truncated chunk\n"; my ($payload, $crc) = unpack "a$length N", $body; last if $type eq 'IEND'; }

      Tested on Windows. Probably OK on a big endien machine but those are harder to come by now that Apple have gone Intel. It doesn't do anything with the chunk data other than skipping through the chunk headers. It also doesn't try to be smart about excessively large chunks.

      Premature optimization is the root of all job security