[Reproducible-commits] [strip-nondeterminism] 08/10: Rewrite the first timestamp in a PE (.exe) file

Andrew Ayer agwa at andrewayer.name
Sun Nov 16 23:16:06 UTC 2014


This is an automated email from the git hooks/post-receive script.

agwa-guest pushed a commit to branch master
in repository strip-nondeterminism.

commit e5d67a49862e5d52d9280bca0724f1081dac6b75
Author: Chris West (Faux) <git at goeswhere.com>
Date:   Thu Nov 6 13:19:08 2014 -0800

    Rewrite the first timestamp in a PE (.exe) file
    
    Modified-by: Andrew Ayer <agwa at andrewayer.name>
     * Add copyright notice
     * Use $File::StripNondeterminism::canonical_time for timestamp instead
       of parsing the debian changelog in the handler itself.
---
 lib/File/StripNondeterminism.pm             |  6 ++++
 lib/File/StripNondeterminism/handlers/pe.pm | 52 +++++++++++++++++++++++++++++
 2 files changed, 58 insertions(+)

diff --git a/lib/File/StripNondeterminism.pm b/lib/File/StripNondeterminism.pm
index 55eebbe..02d656c 100644
--- a/lib/File/StripNondeterminism.pm
+++ b/lib/File/StripNondeterminism.pm
@@ -25,6 +25,7 @@ use File::StripNondeterminism::handlers::ar;
 use File::StripNondeterminism::handlers::gzip;
 use File::StripNondeterminism::handlers::jar;
 use File::StripNondeterminism::handlers::javadoc;
+use File::StripNondeterminism::handlers::pe;
 use File::StripNondeterminism::handlers::pomproperties;
 use File::StripNondeterminism::handlers::zip;
 
@@ -63,6 +64,10 @@ sub get_normalizer_for_file {
 	if (m/\.html$/ && File::StripNondeterminism::handlers::javadoc::is_javadoc_file($_)) {
 		return \&File::StripNondeterminism::handlers::javadoc::normalize;
 	}
+	# PE executables
+	if (m/\.(exe|dll|cpl|ocx|sys|scr|drv|efi|fon)/ && _get_file_type($_) =~ m/PE32.? executable/) {
+		return \&File::StripNondeterminism::handlers::pe::normalize;
+	}
 	# pomproperties
 	if (m/pom\.properties$/ && File::StripNondeterminism::handlers::pomproperties::is_pom_properties_file($_)) {
 		return \&File::StripNondeterminism::handlers::pomproperties::normalize;
@@ -80,6 +85,7 @@ sub get_normalizer_by_name {
 	return \&File::StripNondeterminism::handlers::gzip::normalize if $_ eq 'gzip';
 	return \&File::StripNondeterminism::handlers::jar::normalize if $_ eq 'jar';
 	return \&File::StripNondeterminism::handlers::javadoc::normalize if $_ eq 'javadoc';
+	return \&File::StripNondeterminism::handlers::pe::normalize if $_ eq 'pe';
 	return \&File::StripNondeterminism::handlers::pomproperties::normalize if $_ eq 'pomproperties';
 	return \&File::StripNondeterminism::handlers::zip::normalize if $_ eq 'zip';
 	return undef;
diff --git a/lib/File/StripNondeterminism/handlers/pe.pm b/lib/File/StripNondeterminism/handlers/pe.pm
new file mode 100644
index 0000000..789c740
--- /dev/null
+++ b/lib/File/StripNondeterminism/handlers/pe.pm
@@ -0,0 +1,52 @@
+#
+# Copyright 2014 Chris West (Faux)
+#
+# This file is part of strip-nondeterminism.
+#
+# strip-nondeterminism is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# strip-nondeterminism is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with strip-nondeterminism.  If not, see <http://www.gnu.org/licenses/>.
+#
+package File::StripNondeterminism::handlers::pe;
+
+use strict;
+use warnings;
+
+use Fcntl ":seek";
+
+sub normalize {
+	my ($filename) = @_;
+	open(my $f, "+<", $filename) or die("couldn't open $filename: $!");
+	binmode($f);
+	read($f, my $mx, 2) or die("couldn't try to read initial header: $!");
+	return if ($mx ne 'MZ');
+
+	seek($f, 0x3A, SEEK_CUR) or die("couldn't jump to e_lfanew location: $!");
+	read($f, my $encoded_off, 4) or die("couldn't read e_lfanew field: $!");
+	my $off = unpack("V", $encoded_off);
+	seek($f, $off, SEEK_SET) or die("couldn't seek to start of PE section: $!");
+
+	read($f, my $pe, 2) or die("couldn't read PE header: $!");
+	return if $pe ne 'PE';
+
+	seek($f, 2+2+2, SEEK_CUR) or die("couldn't skip mMachine and mNumberOfSections: $!");
+	read($f, my $encoded_time, 4) or die("couldn't read timestamp: $!");
+	my $time = unpack("V", $encoded_time);
+
+	$encoded_time = pack("V", $File::StripNondeterminism::canonical_time // 0);
+
+	seek($f, -4, SEEK_CUR) or die("impossibly couldn't seek to where we were before: $!");
+	print $f $encoded_time;
+	close($f) or die("couldn't close file: $!");
+}
+
+1;

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/reproducible/strip-nondeterminism.git



More information about the Reproducible-commits mailing list