How to use file as a negative mask for reading another file, in Perl?

135 views Asked by At

I want to extract only the junk data from the free space of a raw partition image (EXT4). So I got this idea, to zero out the free space and then to use the result as a mask.

I have raw partition image (14GB) containing data and free space and the same raw partition image, with free space zeroed.

I want to do the following operation between these two files in Perl, for each byte of them in order to obtain the raw partition image processed, will contain only junk data from free space.

RPM  - raw partition image
RPMz - raw partition image with free space zeroed
RPMp - raw partition image processed, will contain only junk data from free space

for each byte: RPM & !RPMz => RPMp

Can someone help me out with a Perl script or a starting point for this?

1

There are 1 answers

1
Nick On BEST ANSWER

This is what I wrote for inverting the bytes, in order to obtain !RPMz. But it's slow, and with 100MB chunks I'm out of memory. I need some help.

use strict;
use warnings;
use bignum;

my $buffer = "";

my $path1="F:/data-lost-workspace/partition-for-zerofree/mmcblk0p12.raw";
my $path2="F:/data-lost-workspace/partition-for-zerofree/mmcblk0p12_invert.raw";

open(FILE_IN, "<$path1");
binmode(FILE_IN); 

my $offset=0;
my $remaining = -s $path1;
my $length=1024*1024*100;
my $index=1;

unlink $path2;

while($remaining>0)
{
my $line=read(FILE_IN, $buffer, $length);  
print $index." ".$line."\r\n";
$index++;
$remaining=$remaining-$length;

my $buffer_invert=();

my @c = split('', $buffer);

for(my $i=0;$i<$length;$i++)
{
    if(ord($c[$i])==0x0)
    {
        $c[$i]=chr(0xFF);
    }
    else
    {
        $c[$i]=chr(0x00);
    }
}

$buffer_invert=join('', @c);

open(FILE_OUT, ">>$path2");
binmode(FILE_OUT); 
print FILE_OUT $buffer_invert;
close(FILE_OUT);
}
close(FILE_IN);