A
A. Farber
How could I replace 1500 bytes in a file, please?
They are not located at some fixed offset and also
the amount of replacing bytes could be bigger or
smaller than 1500. Here's what I am trying currently:
my $SEARCH = pack 'C*', map { hex } qw(
98 e6 52 ca db ce db 19 62 00 00 00
69 b5 40 0c 91 dc 6b 43 cf 00 00 00
5b 7d 92 c2 e1 ec f1 45 11 00 00 00
92 45 a3 64 0b ff e4 d9 a6 00 00 00
2e 0d cb 22 50 0e 92 5a 0f 00 00 00
4e d5 78 59 ee 1c 8b c6 cc 00 00 00
.... );
my $REPLACE = pack 'C*', map { hex } qw(
ff ff ff ff ff ff ff ff ff ff ff ff
69 b5 40 0c 91 dc 6b 43 cf 00 00 00
5b 7d 92 c2 e1 ec f1 45 11 00 00 00
92 45 a3 64 0b ff e4 d9 a6 00 00 00
2e 0d cb 22 50 0e 92 5a 0f 00 00 00
.... );
use constant KEYSIZE => length $SEARCH;
while (sysread $upload, $chunk, KEYSIZE) {
$both = $prev . $chunk;
if ($both =~ s/$SEARCH/$REPLACE/o) {
die "syswrite failed: $!"
unless length $both == syswrite $fh, $both;
undef $prev;
} else {
die "syswrite failed: $!"
unless length $prev == syswrite $fh, $prev;
$prev = $chunk;
}
}
# don't forget to print the last line
die "syswrite failed: $!"
unless length $prev == syswrite $fh, $prev;
Unfortunately the regex doesn't compile, probably
because it doesn't like the binary data I feed to it!
Regards
Alex
They are not located at some fixed offset and also
the amount of replacing bytes could be bigger or
smaller than 1500. Here's what I am trying currently:
my $SEARCH = pack 'C*', map { hex } qw(
98 e6 52 ca db ce db 19 62 00 00 00
69 b5 40 0c 91 dc 6b 43 cf 00 00 00
5b 7d 92 c2 e1 ec f1 45 11 00 00 00
92 45 a3 64 0b ff e4 d9 a6 00 00 00
2e 0d cb 22 50 0e 92 5a 0f 00 00 00
4e d5 78 59 ee 1c 8b c6 cc 00 00 00
.... );
my $REPLACE = pack 'C*', map { hex } qw(
ff ff ff ff ff ff ff ff ff ff ff ff
69 b5 40 0c 91 dc 6b 43 cf 00 00 00
5b 7d 92 c2 e1 ec f1 45 11 00 00 00
92 45 a3 64 0b ff e4 d9 a6 00 00 00
2e 0d cb 22 50 0e 92 5a 0f 00 00 00
.... );
use constant KEYSIZE => length $SEARCH;
while (sysread $upload, $chunk, KEYSIZE) {
$both = $prev . $chunk;
if ($both =~ s/$SEARCH/$REPLACE/o) {
die "syswrite failed: $!"
unless length $both == syswrite $fh, $both;
undef $prev;
} else {
die "syswrite failed: $!"
unless length $prev == syswrite $fh, $prev;
$prev = $chunk;
}
}
# don't forget to print the last line
die "syswrite failed: $!"
unless length $prev == syswrite $fh, $prev;
Unfortunately the regex doesn't compile, probably
because it doesn't like the binary data I feed to it!
Regards
Alex