Created
June 7, 2014 07:48
-
-
Save mschmitt/4ba6abc6e949c5e37caf to your computer and use it in GitHub Desktop.
Deliberately corrupt a JPEG file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl -w | |
use strict; | |
use Getopt::Std; | |
# Deliberately corrupt a JPEG file. | |
# | |
# ./j0rrupt0r.pl -i nice.jpg -o corrupt.jpg | |
# Initial fraction of the image that won't be considered | |
# for corruption (1/fraction) | |
my $fraction = 3; | |
# Every how many bytes to consider corruption | |
my $consider = 757; | |
# With a probability of 1/$probability, skip a byte | |
my $probability = 3; | |
my %opts; | |
my $fh_in; | |
my $fh_out; | |
getopts('i:o:', \%opts); | |
# Open input and output file | |
die "Specify input file with -i.\n" unless ($opts{'i'} and (open $fh_in, "<$opts{'i'}")); | |
die "Specify output file with -o.\n" unless ($opts{'o'} and (open $fh_out, ">$opts{'o'}")); | |
# Leave 1/$fraction of the image intact before vandalizing | |
my $size = (stat($opts{'i'}))[7]; | |
my $offset = int($size / $fraction); | |
my $data; | |
my $bytes; | |
for (0..$offset){ | |
$bytes++; | |
read ($fh_in, $data, 1); | |
print $fh_out $data; | |
} | |
while(read($fh_in, $data, 1)){ | |
# consider every nth byte for deliberate corruption | |
if (0 == $bytes++ % $consider){ | |
if (0 == int(rand($probability))){ | |
# Skip this byte | |
next; | |
} | |
} | |
print $fh_out $data; | |
} | |
close $fh_in; | |
close $fh_out; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment