[Reproducible-commits] [strip-nondeterminism] 01/04: Add a PNG handler
Andrew Ayer
agwa at andrewayer.name
Sun Mar 22 18:41:51 UTC 2015
This is an automated email from the git hooks/post-receive script.
agwa-guest pushed a commit to branch debian
in repository strip-nondeterminism.
commit 83188636cd4286d82b214b57f9772283afbeb4fa
Author: Andrew Ayer <agwa at andrewayer.name>
Date: Sun Mar 22 11:12:40 2015 -0700
Add a PNG handler
Based heavily on patch by Chris Lamb <lamby at debian.org>.
Closes: #777679
---
lib/File/StripNondeterminism.pm | 6 ++
lib/File/StripNondeterminism/handlers/png.pm | 86 ++++++++++++++++++++++++++++
2 files changed, 92 insertions(+)
diff --git a/lib/File/StripNondeterminism.pm b/lib/File/StripNondeterminism.pm
index 8f4d4b7..8d17640 100644
--- a/lib/File/StripNondeterminism.pm
+++ b/lib/File/StripNondeterminism.pm
@@ -27,6 +27,7 @@ use File::StripNondeterminism::handlers::jar;
use File::StripNondeterminism::handlers::javadoc;
use File::StripNondeterminism::handlers::pe;
use File::StripNondeterminism::handlers::pearregistry;
+use File::StripNondeterminism::handlers::png;
use File::StripNondeterminism::handlers::javaproperties;
use File::StripNondeterminism::handlers::zip;
@@ -73,6 +74,10 @@ sub get_normalizer_for_file {
if (m/\.(exe|dll|cpl|ocx|sys|scr|drv|efi|fon)/ && _get_file_type($_) =~ m/PE32.? executable/) {
return \&File::StripNondeterminism::handlers::pe::normalize;
}
+ # PNG
+ if (m/\.png$/ && _get_file_type($_) =~ m/PNG image data/) {
+ return \&File::StripNondeterminism::handlers::png::normalize;
+ }
# pom.properties, version.properties
if (m/(pom|version)\.properties$/ && File::StripNondeterminism::handlers::javaproperties::is_java_properties_file($_)) {
return \&File::StripNondeterminism::handlers::javaproperties::normalize;
@@ -92,6 +97,7 @@ sub get_normalizer_by_name {
return \&File::StripNondeterminism::handlers::javadoc::normalize if $_ eq 'javadoc';
return \&File::StripNondeterminism::handlers::pe::normalize if $_ eq 'pe';
return \&File::StripNondeterminism::handlers::pearregistry::normalize if $_ eq 'pearregistry';
+ return \&File::StripNondeterminism::handlers::png::normalize if $_ eq 'png';
return \&File::StripNondeterminism::handlers::javaproperties::normalize if $_ eq 'javaproperties';
return \&File::StripNondeterminism::handlers::zip::normalize if $_ eq 'zip';
return undef;
diff --git a/lib/File/StripNondeterminism/handlers/png.pm b/lib/File/StripNondeterminism/handlers/png.pm
new file mode 100644
index 0000000..f0f806e
--- /dev/null
+++ b/lib/File/StripNondeterminism/handlers/png.pm
@@ -0,0 +1,86 @@
+#
+# Copyright 2015 Chris Lamb <lamby at debian.org>
+# Copyright 2015 Andrew Ayer <agwa at andrewayer.name>
+#
+# 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::png;
+
+use strict;
+use warnings;
+
+use File::Basename qw/dirname/;
+use POSIX qw/strftime/;
+use Archive::Zip;
+
+sub crc {
+ my ($data) = @_;
+ return Archive::Zip::computeCRC32($data);
+}
+
+sub chunk {
+ my ($type, $data) = @_;
+ return pack('Na4a*N', length($data), $type, $data, crc($type . $data));
+}
+
+sub time_chunk {
+ my ($seconds) = @_;
+ my ($sec, $min, $hour, $mday, $mon, $year) = gmtime($seconds);
+ return chunk('tIME', pack('nCCCCC', 1900+$year, $mon+1, $mday, $hour, $min, $sec));
+}
+
+sub text_chunk {
+ my ($keyword, $data) = @_;
+ return chunk('tEXt', pack('Z*a*', $keyword, $data));
+}
+
+sub normalize {
+ my ($filename) = @_;
+
+ my $canonical_time = $File::StripNondeterminism::canonical_time;
+
+ my $tempfile = File::Temp->new(DIR => dirname($filename));
+
+ open(my $fh, '+<', $filename) or die "$filename: open: $!";
+ read($fh, my $magic, 8); $magic eq "\x89PNG\r\n\x1a\n"
+ or die "$filename: does not appear to be a PNG";
+ print $tempfile $magic;
+
+ while (read($fh, my $header, 8) == 8) {
+ my ($len, $type) = unpack('Na4', $header);
+
+ # Always read(2) (including the CRC) even if we're going to skip
+ read $fh, my $data, $len + 4;
+
+ if ($type eq "tIME") {
+ print $tempfile time_chunk($canonical_time) if defined($canonical_time);
+ } elsif ($type =~ /[tiz]EXt/ && $data =~ /^(date:[^\0]+|Creation Time)\0/) {
+ print $tempfile text_chunk($1, strftime("%Y-%m-%dT%H:%M:%S-00:00",
+ gmtime($canonical_time))) if defined($canonical_time);
+ } else {
+ print $tempfile $header . $data;
+ }
+ }
+
+ chmod((stat($fh))[2] & 07777, $tempfile->filename);
+ rename($tempfile->filename, $filename)
+ or die "$filename: unable to overwrite: rename: $!";
+ $tempfile->unlink_on_destroy(0);
+
+ close $fh;
+}
+
+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