1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| #!/usr/bin/perl
use strict;
use warnings;
use feature qw(:5.14);
my ($in) = @ARGV;
my $bak = "$in.bak";
die "$bak already exists\n" if -f "$bak";
rename $in, $bak;
open my $IN, "<", $bak or die "Can't open file $bak for reading: $!";
open my $OUT, ">", $in or die "Can't open file $in for writing: $!";
my (@kept_lines, @decoded);
while (my $line = <$IN>) {
chomp($line);
my ($code) = $line =~ /,([\da-f]+)$/i;
next if !$code || $code !~ /^8[18]/;
if ($code =~ /^81/) {
warn "Line to decode found at $. in $bak\n" if !@decoded;
warn "New line to decode at $. in $bak\n" if @decoded;
my $bitcode = unpack("B*", pack("H*", $code));
my $bit75 = substr($bitcode, 74, 8);
my $bitpos;
if ($bit75 eq "00000000") {
my $bit171 = substr($bitcode, 170, 2);
if ($bit171 eq "00" || $bit171 eq "11") {
$bitpos = substr($bitcode, 185, 1) eq "1" ? 217 : 209;
}
else {
$bitpos = substr($bitcode, 200, 1) eq "1" ? 232 : 224;
}
}
elsif ($bit75 eq "00000001") {
my $bit194 = substr($bitcode, 193, 2);
if ($bit194 eq "00" || $bit194 eq "11") {
$bitpos = substr($bitcode, 209, 1) eq "1" ? 241 : 232;
}
else {
$bitpos = substr($bitcode, 222, 1) eq "1" ? 254 : 246;
}
}
@decoded = map oct("0b$_"), substr($bitcode, $bitpos, 32) =~ /(.{4})/g if $bitpos;
if (@kept_lines) {
map { say { $OUT } "$_ @decoded" } splice @kept_lines;
}
}
elsif (@decoded) {
say { $OUT } "$line @decoded";
}
else {
push @kept_lines, $line;
}
} |
Partager