Perl crashes at 128KB?
am 14.09.2004 10:46:38 von Kelvin
Hi, there:
Following is my code, basically I try to simply XOR a 20MB file line by line
with a key
then save it in a file. I am using the lastest ActivePerl in Win2000.
The error I got is,
The instruction at "0x2808698b" referenced memory at "0x00000004".
The memory couldn't be "written".
Click OK to terminate...
Click CANCEL to debug...
What happened?
Thanks.
#!/usr/bin/perl
use POSIX;
open(FIL, "./line_merged.all");
@page_in = ;
close(FIL);
$key = "This is my key";
$j = 0;
open(FIL, ">./encrypted.all");
binmode FIL;
foreach $line (@page_in) {
if( $j % 100 == 0 ) {
print "\n\n\nLine $j started here: \n";
print "\n" . $line;
}
$j = $j + 1;
$mult = ceil( length( $line ) / length( $key ) ) + 1;
for( $i = 0; $i < $mult; $i = $i + 1) {
$key = $key . $key;
}
$key = substr($key, 0, length($line));
$line = $key ^ $line ;
# print "\n" . $key;
# print "\n\nAfter Encryption\n" . $line;
print FIL $line;
# $line = $key ^ $line ;
# print "\n\nAfter Decryption\n" . $line;
}
close(FIL);
Re: Perl crashes at 128KB?
am 14.09.2004 15:32:36 von someone
Kelvin wrote:
>
> Following is my code, basically I try to simply XOR a 20MB file line by line
> with a key
> then save it in a file. I am using the lastest ActivePerl in Win2000.
>
> The error I got is,
> The instruction at "0x2808698b" referenced memory at "0x00000004".
> The memory couldn't be "written".
I didn't find that message in perldiag. Are you sure that that is a perl
error message?
> #!/usr/bin/perl
use warnings;
use strict;
> use POSIX;
>
> open(FIL, "./line_merged.all");
You should *ALWAYS* verify that the file opened correctly.
open FIL, '<', 'line_merged.all' or die "Cannot open 'line_merged.all' $!";
> @page_in = ;
This is probably what is causing the error message. You are storing the
complete file in an array.
> close(FIL);
>
> $key = "This is my key";
> $j = 0;
> open(FIL, ">./encrypted.all");
You should *ALWAYS* verify that the file opened correctly.
open FIL, '>', 'encrypted.all' or die "Cannot open 'encrypted.all' $!";
> binmode FIL;
Why only binmode the output file and not the input file as well?
> foreach $line (@page_in) {
> if( $j % 100 == 0 ) {
> print "\n\n\nLine $j started here: \n";
> print "\n" . $line;
> }
> $j = $j + 1;
> $mult = ceil( length( $line ) / length( $key ) ) + 1;
> for( $i = 0; $i < $mult; $i = $i + 1) {
> $key = $key . $key;
> }
> $key = substr($key, 0, length($line));
> $line = $key ^ $line ;
> # print "\n" . $key;
> # print "\n\nAfter Encryption\n" . $line;
> print FIL $line;
>
> # $line = $key ^ $line ;
> # print "\n\nAfter Decryption\n" . $line;
> }
> close(FIL);
You should probably not read the entire file at once. Something like this
should work:
#!/usr/bin/perl
use warnings;
use strict;
my $key = 'This is my key' x 500; # make a reasonably large buffer
my $file_in = 'line_merged.all';
my $file_out = 'encrypted.all';
open FIN, '<', $file_in or die "Cannot open $file_in: $!";
binmode FIN;
open FOUT, '>', $file_out or die "Cannot open $file_out: $!";
binmode FOUT;
$/ = \length $key; # set record length same as key length
while ( ) {
$_ ^= $key;
print FOUT;
}
__END__
John
--
use Perl;
program
fulfillment
Re: Perl crashes at 128KB?
am 14.09.2004 18:59:35 von Joe Smith
John W. Krahn wrote:
> Kelvin wrote:
>> binmode FIL;
>
> Why only binmode the output file and not the input file as well?
The input file is plain text.
After the exclusive-OR, the output file has random control
characters, such as Control-Z and CR without LF, LF without CR.
-Joe
Re: Perl crashes at 128KB?
am 15.09.2004 01:17:40 von someone
Joe Smith wrote:
> John W. Krahn wrote:
>
>> Kelvin wrote:
>>
>>> binmode FIL;
>>
>> Why only binmode the output file and not the input file as well?
>
> The input file is plain text.
> After the exclusive-OR, the output file has random control
> characters, such as Control-Z and CR without LF, LF without CR.
It looked like the OP intended to use the same program to encode and decode so
using binmode on both filehandles is probably appropriate, and according to
TFM it won't hurt. :-)
John
--
use Perl;
program
fulfillment