Sunday, 15 January 2012

perl seek and remove at varying offsets in binmode -



perl seek and remove at varying offsets in binmode -

this script writing.

#usr/bin/perl utilize warnings; open(my $infile, '<', "./file1.bin") or die "cannot open file1.bin: $!"; binmode($infile); open(my $outfile, '>', "./extracted info without 00's.bin") or die "cannot create extracted info without 00's.bin: $!"; binmode($outfile); local $/; $infile = <stdin>; print substr($infile, 0, 0x840, ''); $infile =~ s/\0{16}//; print $outfile;

i'm loading binary file in perl. have been able seek , patch @ offsets, is, able find instance of "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00" (16 bytes?) , remove file, no less 16 bytes. less want leave. in of files offset 00's start @ different offsets, if thinking correctly, if can search 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 , remove instance of it, won't matter offset 00's at. extract info first specific offsets, search file , prune 00's it. can extract specific offsets need, need open extracted file , shave off 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

ef 39 77 5b 14 9d e9 1e 94 a9 97 f2 6d e3 68 05 6f 7b 77 bb c4 99 67 b5 c9 71 12 30 9d ed 31 b6 ab 1f 81 66 e1 dd 29 4e 71 8d 54 f5 6c c8 86 0d 5b 72 af a8 1f 26 dd 05 af 78 13 ef a5 e0 76 bb 8a 59 9b 20 c5 58 95 7c e0 db 44 6a ec 7e d0 10 09 42 b1 12 65 80 b3 ec 58 1a 2f 92 b9 32 d9 07 96 de 32 51 4b 5f 3b 50 9a d1 09 37 f4 6d 7c 01 01 4a a4 24 04 dc 83 08 17 cb 34 2c e5 87 26 c1 35 38 f4 c4 e4 78 fe fc a2 99 48 c9 ca 69 90 33 87 09 a8 27 ba 91 fc 4b 77 fa ab f5 1e 4e c0 want leave f2 78 6e 31 7d 16 3b 53 04 8a c1 a8 4b 70 39 22 <----- here 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 <----- want prune 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 here on 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00<---- end of file, , need prune these few rows of 00's

say "f2 78 6e" illustration above, @ offset 0x45000 but in file 00 00's start @ different offset, how code 00 00's pruned. in file opening? if need more specific, ask. seems peekk far file until nail long 00 00 string, prune remaining lines. create sense @ all? want search file instances of 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 , delete/prune/truncate it. want save 00's

edit #2 did it:

open($infile, '<', './file1') or die "cannot open file1: $!"; binmode $infile; open($outfile, '>', './file2') or die "cannot open file2: $!"; binmode $outfile; local $/; $file = <$infile>; $file =~ s/\0{16}//g; print $outfile $file; close ($infile); close ($outfile);

thank ikegami help , patience :)

no such thing removing file. have either

copy file without undesired bits, or read rest of file, seek back, print on undesired bits, truncate file.

i went alternative 1.

$ perl -e' binmode stdin; binmode stdout; local $/; $file = <stdin>; $file =~ s/\0{16}//; print $file; ' <file.in >file.out

i'm loading entire file memory. either alternative can done in chunks, complicates things because nuls span 2 chunks.

in poorly phrased update, seem have asked avoid changes in first 0x840 bytes. 2 solutions:

$ perl -e' binmode stdin; binmode stdout; local $/; $file = <stdin>; substr($file, 0x840) =~ s/\0{16}//; print $file; ' <file.in >file.out $ perl -e' binmode stdin; binmode stdout; local $/; $file = <stdin>; print substr($file, 0, 0x840, ''); $file =~ s/\0{16}//; print $file; ' <file.in >file.out

perl seek vary

No comments:

Post a Comment