Chris Lamb wrote:
> A patch attached with initial support for overriding timestamps
> in PNG files.
Another patch is attached that strips timestamps and text portions
instead.
Advantages:
* Simpler.
* More robust; no hardcoded list of "tEXt" tags to parse nor ugly
mangling of the timezone.
* Does not require Digest::CRC, preventing an (eventual) transitive
Build-Depends on any package using Debhelper.
* Stripping the data instead of rewriting it will save a few KiB of
storage space across the entire Debian archive (!)
Disadvantages:
* If any program is relying on the contents or even existence of
timestamp or text chunks then that will break. However, that
seems extremely unlikely.
Regards,
--
,''`.
: :' : Chris Lamb
`. `'` [email protected] / chris-lamb.co.uk
`-
From 77131d1dba4ccb1d762d5a05ac088506e66ffde8 Mon Sep 17 00:00:00 2001
From: Chris Lamb <[email protected]>
Date: Wed, 11 Feb 2015 16:02:47 +0000
Subject: [PATCH] Add initial support for PNG
---
lib/File/StripNondeterminism.pm | 6 ++++
lib/File/StripNondeterminism/handlers/png.pm | 54 ++++++++++++++++++++++++++++
2 files changed, 60 insertions(+)
create mode 100644 lib/File/StripNondeterminism/handlers/png.pm
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..010544b
--- /dev/null
+++ b/lib/File/StripNondeterminism/handlers/png.pm
@@ -0,0 +1,54 @@
+#
+# Copyright 2015 Chris Lamb <[email protected]>
+#
+# 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/;
+
+sub normalize {
+ my ($filename) = @_;
+
+ 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;
+
+ print $tempfile $header . $data
+ unless ($type =~ /tIME|tEXt/)
+ }
+
+ 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;
--
2.1.0