| 1 |
#!@PERL@ |
| 2 |
## Copyright (C) 2016-2017 Robert Krawitz |
| 3 |
## |
| 4 |
## This program is free software; you can redistribute it and/or modify |
| 5 |
## it under the terms of the GNU General Public License as published by |
| 6 |
## the Free Software Foundation; either version 2, or (at your option) |
| 7 |
## any later version. |
| 8 |
## |
| 9 |
## This program is distributed in the hope that it will be useful, |
| 10 |
## but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
## GNU General Public License for more details. |
| 13 |
## |
| 14 |
## You should have received a copy of the GNU General Public License |
| 15 |
## along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 |
|
| 17 |
# Reference compression methods and other documentation from |
| 18 |
# compress-checksums. |
| 19 |
# |
| 20 |
# This is believed back compatible with all earlier versions of the |
| 21 |
# Gutenprint test case compression utility. |
| 22 |
|
| 23 |
use strict; |
| 24 |
|
| 25 |
my ($progname) = $0; |
| 26 |
|
| 27 |
my (%old_modes, %old_printers, %new_modes, %new_printers); |
| 28 |
my ($detail1) = 0; |
| 29 |
my ($spc) = unpack("C", " "); |
| 30 |
|
| 31 |
while ($#ARGV >= 0 && $ARGV[0] eq '-d') { |
| 32 |
$detail1++; |
| 33 |
shift @ARGV; |
| 34 |
} |
| 35 |
|
| 36 |
my (%asc_table); |
| 37 |
map { $asc_table{pack("C", $_ + 128)} = pack("C", $_) . " "; } (0..127); |
| 38 |
$asc_table{pack("C", 27)} = "=+"; |
| 39 |
$asc_table{pack("C", 26)} = "!+"; |
| 40 |
$asc_table{pack("C", 25)} = '"+'; |
| 41 |
$asc_table{pack("C", 24)} = ",+"; |
| 42 |
$asc_table{pack("C", 23)} = "'+"; |
| 43 |
$asc_table{pack("C", 22)} = "%+"; |
| 44 |
$asc_table{pack("C", 21)} = ">+"; |
| 45 |
$asc_table{pack("C", 20)} = "20+"; |
| 46 |
$asc_table{pack("C", 19)} = "19+"; |
| 47 |
$asc_table{pack("C", 18)} = "18+"; |
| 48 |
$asc_table{pack("C", 17)} = "17+"; |
| 49 |
$asc_table{pack("C", 16)} = "16+"; |
| 50 |
$asc_table{pack("C", 15)} = "15+"; |
| 51 |
$asc_table{pack("C", 14)} = "14+"; |
| 52 |
$asc_table{pack("C", 13)} = "13+"; |
| 53 |
$asc_table{pack("C", 12)} = "12+"; |
| 54 |
$asc_table{pack("C", 11)} = "11+"; |
| 55 |
$asc_table{pack("C", 9)} = "10+"; # 0xa is newline |
| 56 |
$asc_table{pack("C", 8)} = "9+"; |
| 57 |
$asc_table{pack("C", 7)} = "8+"; |
| 58 |
$asc_table{pack("C", 6)} = "7+"; |
| 59 |
$asc_table{pack("C", 5)} = "6+"; |
| 60 |
$asc_table{pack("C", 4)} = "5+"; |
| 61 |
$asc_table{pack("C", 3)} = "4+"; |
| 62 |
$asc_table{pack("C", 2)} = "3+"; |
| 63 |
$asc_table{pack("C", 1)} = "2\+"; |
| 64 |
|
| 65 |
my (%respack_tbl); |
| 66 |
$respack_tbl{"d"} = "draft"; |
| 67 |
$respack_tbl{"h"} = "high"; |
| 68 |
$respack_tbl{"m"} = "mono"; |
| 69 |
$respack_tbl{"p"} = "photo"; |
| 70 |
$respack_tbl{"s"} = "std"; |
| 71 |
$respack_tbl{"t"} = "tshirt"; |
| 72 |
|
| 73 |
my (%sub_table); |
| 74 |
$sub_table{"P;"} = "PrintingMode"; |
| 75 |
$sub_table{"S;"} = "PageSize"; |
| 76 |
$sub_table{"D;"} = "DitherAlgorithm"; |
| 77 |
$sub_table{"R;"} = "Resolution"; |
| 78 |
$sub_table{"C;"} = "ColorCorrection"; |
| 79 |
$sub_table{"c;"} = "Color"; |
| 80 |
$sub_table{"M;"} = "MediaType"; |
| 81 |
$sub_table{"I;"} = "InputSlot"; |
| 82 |
|
| 83 |
my (%us_table); |
| 84 |
$us_table{'>'} = "__"; |
| 85 |
$us_table{'%'} = "___"; |
| 86 |
$us_table{"'"} = "____"; |
| 87 |
$us_table{','} = "_____"; |
| 88 |
$us_table{'"'} = "______"; |
| 89 |
$us_table{'!'} = "_______"; |
| 90 |
$us_table{'='} = "________"; |
| 91 |
|
| 92 |
if ($#ARGV <= 0) { |
| 93 |
print_file($ARGV[0]); |
| 94 |
} elsif ($#ARGV == 1) { |
| 95 |
compare_files($ARGV[0], $ARGV[1]); |
| 96 |
} else { |
| 97 |
die "Usage: $0 [old_checksum_file] [new_checksum_file]\n"; |
| 98 |
} |
| 99 |
|
| 100 |
sub get_printer($) { |
| 101 |
my ($mode) = @_; |
| 102 |
$mode =~ s/_PrintingMode.*//o; |
| 103 |
return $mode; |
| 104 |
} |
| 105 |
|
| 106 |
sub get_mode($) { |
| 107 |
my ($mode) = @_; |
| 108 |
$mode =~ s/.*_PrintingMode/PrintingMode/o; |
| 109 |
return $mode; |
| 110 |
} |
| 111 |
|
| 112 |
sub open_file($) { |
| 113 |
my ($file) = @_; |
| 114 |
my $in; |
| 115 |
# Might as well handle known compression methods directly |
| 116 |
if ($file =~ /\.bz2$/) { |
| 117 |
open($in, '-|', "@BZIP2@ -d -c $file") || die("Can't open checksum file $file: $!\n"); |
| 118 |
} elsif ($file =~ /.gz$/) { |
| 119 |
open($in, '-|', "@GZIP@ -d -c $file") || die("Can't open checksum file $file: $!\n"); |
| 120 |
} elsif ($file =~ /.xz$/) { |
| 121 |
open($in, '-|', "@XZ@ -d -c $file") || die("Can't open checksum file $file: $!\n"); |
| 122 |
} elsif ($file =~ /.lrz$/) { |
| 123 |
open($in, '-|', "cat $file | @LRZIP@ -d -q") || die("Can't open checksum file $file: $!\n"); |
| 124 |
} elsif ($file =~ /.zpaq$/) { |
| 125 |
my ($zpaq_base) = "@top_builddir@/scripts/wzpaq"; |
| 126 |
if (substr($zpaq_base, 0, 1) ne '/') { # Relative path |
| 127 |
$progname =~ s,[^/]*$,,; |
| 128 |
$zpaq_base = "$progname$zpaq_base"; |
| 129 |
} |
| 130 |
open($in, '-|', "$zpaq_base -d -c $file") || die("Can't open checksum file $file: $!\n"); |
| 131 |
} elsif ($file =~ /.rz$/) { |
| 132 |
open($in, '-|', "@RZIP@ -d -c $file") || die("Can't open checksum file $file: $!\n"); |
| 133 |
} else { |
| 134 |
open($in, '<', "$file") || die("Can't open checksum file $file: $!\n"); |
| 135 |
} |
| 136 |
return $in; |
| 137 |
} |
| 138 |
|
| 139 |
sub initialize_file($) { |
| 140 |
my ($file) = @_; |
| 141 |
my (%ctx); |
| 142 |
if ($file eq '' || $file eq '-' || ! defined $file) { |
| 143 |
$ctx{"fh"} = \*STDIN; |
| 144 |
} else { |
| 145 |
$ctx{"fh"} = open_file($file); |
| 146 |
} |
| 147 |
$ctx{"sum"} = ""; |
| 148 |
$ctx{"last_sum"} = ""; |
| 149 |
$ctx{"kvals"} = []; |
| 150 |
$ctx{"pchunks"} = []; |
| 151 |
$ctx{"active"} = 1; |
| 152 |
$ctx{"lastdup"} = 0; |
| 153 |
return \%ctx; |
| 154 |
} |
| 155 |
|
| 156 |
sub break_subgroup($$$) { |
| 157 |
my ($pref, $group, $shared) = @_; |
| 158 |
if ($group =~ /^([^_]*)_(.*)/o) { |
| 159 |
my ($option) = $1; |
| 160 |
my ($values) = $2; |
| 161 |
my (@values) = split(/&/, $values); |
| 162 |
return join(" ", map { "${pref}${option}_${_}${shared}" } @values); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
sub break_group($$) { |
| 167 |
my ($pref, $group) = @_; |
| 168 |
my (@subgroups); |
| 169 |
my ($rest); |
| 170 |
if ($group =~ /\?/) { |
| 171 |
$group =~ /(^.*\?[^?_]*_[^?_]*)(_.*)/o; |
| 172 |
$rest = $2; |
| 173 |
my ($stuff) = $1; |
| 174 |
@subgroups = split(/\?/, $stuff); |
| 175 |
} elsif ($group =~ /^([^_]*_[^_]*)(_.*$)/o) { |
| 176 |
@subgroups = ($1); |
| 177 |
$rest = $2; |
| 178 |
} |
| 179 |
return join(" ", map {break_subgroup($pref, $_, $rest)} @subgroups); |
| 180 |
} |
| 181 |
|
| 182 |
sub fetch_next_line($) { |
| 183 |
my ($ctx) = @_; |
| 184 |
my ($fh) = $$ctx{"fh"}; |
| 185 |
my ($inline); |
| 186 |
if (! ($inline = <$fh>)) { |
| 187 |
close($fh) if ($fh != \*STDIN); |
| 188 |
$$ctx{"active"} = 0; |
| 189 |
return 0; |
| 190 |
} |
| 191 |
chomp $inline; |
| 192 |
if ($inline =~ /^[0-9a-f]+ \*/o) { |
| 193 |
# Uncompressed! |
| 194 |
$$ctx{"literal"} = $inline; |
| 195 |
return 1; |
| 196 |
} |
| 197 |
delete $$ctx{"literal"}; |
| 198 |
# Level 10 |
| 199 |
# Level 9 |
| 200 |
# Whitespace optimization -- set high bit. |
| 201 |
$inline =~ s/([\x01-\x09\x0b-\x1b\x80-\xff])/$asc_table{$1}/go; |
| 202 |
# All lines start with hash followed by printing mode, so if compression |
| 203 |
# removed that, we need to restore it. |
| 204 |
# Level 8 |
| 205 |
$inline =~ s/^([0-9a-f]+ )\./$1P;_BW_/o; |
| 206 |
$inline =~ s/^([0-9a-f]+ )\,/$1P;_c;_/o; |
| 207 |
# Level 7 |
| 208 |
$inline =~ s/([=!",'%>])/$us_table{$1}/go; |
| 209 |
# Level 3 |
| 210 |
my ($tmpline) = ""; |
| 211 |
while ($inline =~ /(([^()]*) )?([^() _]*_[^() _]*_)\(([^()_]+_)([^()_]+)(_[^()]*)((\))(.*)|$)/o) { |
| 212 |
my ($head) = $2; |
| 213 |
my ($pref) = $3; |
| 214 |
my ($stuff) = "$4$5$6"; |
| 215 |
my ($tail) = $9; |
| 216 |
$tmpline .= "$head " . break_group($pref, $stuff); |
| 217 |
$inline = $tail; |
| 218 |
} |
| 219 |
$inline = "$tmpline$inline"; |
| 220 |
# Level 5 |
| 221 |
$inline =~ s/([PSDRCcMI];)/$sub_table{$1}/go; |
| 222 |
# Restore printer name |
| 223 |
$inline =~ s/(^| )\*/$1/go; |
| 224 |
$inline =~ s/\.prn( |$)/$1/go; |
| 225 |
# Level 1 |
| 226 |
my ($sum, @keys) = split(/ /o, $inline); |
| 227 |
$$ctx{"kvals"} = \@keys; |
| 228 |
$$ctx{"pchunks"} = []; |
| 229 |
|
| 230 |
# Level 4 |
| 231 |
# Level 2 is subsumed here, because the checksum lengths will always |
| 232 |
# be identical at this compression level. |
| 233 |
my $osum = $$ctx{"sum"}; |
| 234 |
if ($osum ne "" && ((length $sum) < (length $osum))) { |
| 235 |
$sum = substr($osum, 0, ((length $osum) - (length $sum))) . $sum; |
| 236 |
} |
| 237 |
$$ctx{"sum"} = $sum; |
| 238 |
return 1; |
| 239 |
} |
| 240 |
|
| 241 |
sub get_next_datum($) { |
| 242 |
my ($ctx) = @_; |
| 243 |
return undef if (! $$ctx{"active"}); |
| 244 |
if ((scalar @{$$ctx{"kvals"}}) == 0) { |
| 245 |
return undef if (! fetch_next_line($ctx)); |
| 246 |
} |
| 247 |
if (defined $$ctx{"literal"}) { |
| 248 |
my ($sum, $val) = split(/ /, $$ctx{"literal"}); |
| 249 |
$val = substr($val, 1, -4); |
| 250 |
return [$sum, get_mode($val), get_printer($val)]; |
| 251 |
} |
| 252 |
my (@chunks) = split(/_/o, shift(@{$$ctx{"kvals"}}), -1); |
| 253 |
my $pchunks = $$ctx{"pchunks"}; |
| 254 |
# Level 6 |
| 255 |
foreach my $i (0..$#chunks) { |
| 256 |
if ($chunks[$i] eq '') { |
| 257 |
$chunks[$i] = $$pchunks[$i]; |
| 258 |
} else { |
| 259 |
if (substr($chunks[$i], 0, 1) eq '+') { |
| 260 |
substr($chunks[$i], 0, 1) = |
| 261 |
substr($$pchunks[$i], 0, $$ctx{"lastdup"}); |
| 262 |
} elsif (substr($chunks[$i], 0, 1) eq '*') { |
| 263 |
substr($chunks[$i], 0, 1) = substr($$pchunks[$i], 0, 2); |
| 264 |
} elsif ($chunks[$i] =~ /^([0-9]+)\+(.*)/o) { |
| 265 |
$chunks[$i] = substr($$pchunks[$i], 0, $1) . $2; |
| 266 |
$$ctx{"lastdup"} = $1; |
| 267 |
} |
| 268 |
|
| 269 |
if (substr($chunks[$i], -1) eq "\\") { |
| 270 |
substr($chunks[$i], -1) = substr($$pchunks[$i], -2); |
| 271 |
} elsif ($chunks[$i] =~ m,(.*)/([0-9]+)$,o) { |
| 272 |
$chunks[$i] = $1 . substr($$pchunks[$i], -$2); |
| 273 |
} |
| 274 |
} |
| 275 |
} |
| 276 |
my ($val) = join('_', @chunks); |
| 277 |
# Restore printer names |
| 278 |
my ($sep) = rindex($val, ':'); |
| 279 |
my ($prn) = substr($val, $sep + 1); |
| 280 |
substr($val, $sep - 1) = ''; |
| 281 |
# Level 5 (global) |
| 282 |
$val =~ s/\{([0-9]*)([dhmpst])([^|]*)\}/$1x$1dpi_${respack_tbl{$2}}$3/go; |
| 283 |
$val =~ s/Z\;/DitherAlgorithm/go; |
| 284 |
$$ctx{"pchunks"} = \@chunks; |
| 285 |
return [$$ctx{"sum"}, $val, $prn]; |
| 286 |
} |
| 287 |
|
| 288 |
sub load_file($\%\%) { |
| 289 |
my ($file, $modes, $printers) = @_; |
| 290 |
my ($ctx) = initialize_file($file); |
| 291 |
my ($answer); |
| 292 |
while ($answer = get_next_datum($ctx)) { |
| 293 |
my ($sum) = $$answer[0]; |
| 294 |
my ($val) = $$answer[1]; |
| 295 |
my ($prn) = $$answer[2]; |
| 296 |
$$modes{"*${prn}_$val"} = $sum; |
| 297 |
$$printers{$prn} = 1; |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
sub print_changes($$@) { |
| 302 |
my ($tag, $detail, @changes) = @_; |
| 303 |
my ($prev_printer); |
| 304 |
return if ($#changes < 0); |
| 305 |
if (! $detail1) { |
| 306 |
print "$tag:\n"; |
| 307 |
} |
| 308 |
foreach my $m (@changes) { |
| 309 |
my ($printer) = get_printer($m); |
| 310 |
my ($mode) = get_mode($m); |
| 311 |
if ($detail > 1) { |
| 312 |
print "${printer}_${mode}\n"; |
| 313 |
} elsif ($detail) { |
| 314 |
if ($printer ne $prev_printer) { |
| 315 |
print " $printer:\n"; |
| 316 |
$prev_printer = $printer; |
| 317 |
} |
| 318 |
print " $mode\n"; |
| 319 |
} else { |
| 320 |
print " $mode\n"; |
| 321 |
} |
| 322 |
} |
| 323 |
print "\n"; |
| 324 |
} |
| 325 |
|
| 326 |
sub compare_files($$) { |
| 327 |
my ($file1, $file2) = @_; |
| 328 |
load_file($file1, %old_modes, %old_printers); |
| 329 |
load_file($file2, %new_modes, %new_printers); |
| 330 |
|
| 331 |
my (%only_old_printers, %only_new_printers); |
| 332 |
|
| 333 |
foreach my $new_printer (keys %new_printers) { |
| 334 |
if (! defined $old_printers{$new_printer}) { |
| 335 |
$only_new_printers{$new_printer} = 1; |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
foreach my $old_printer (keys %old_printers) { |
| 340 |
if (! defined $new_printers{$old_printer}) { |
| 341 |
$only_old_printers{$old_printer} = 1; |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
my (@only_old_modes, @only_new_modes, @changed_modes); |
| 346 |
|
| 347 |
sub mode_ne($$) { |
| 348 |
my ($new, $old) = @_; |
| 349 |
if (length $new == length $old) { |
| 350 |
return ($new ne $old); |
| 351 |
} elsif (length $new < length $old) { |
| 352 |
return ($new ne substr($old, 0, length $new)); |
| 353 |
} else { |
| 354 |
return ($old ne substr($new, 0, length $old)); |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
foreach my $old_mode (sort keys %old_modes) { |
| 359 |
next if defined $only_old_printers{get_printer($old_mode)}; |
| 360 |
if (defined $new_modes{$old_mode}) { |
| 361 |
if (mode_ne($new_modes{$old_mode}, $old_modes{$old_mode})) { |
| 362 |
push @changed_modes, $old_mode; |
| 363 |
} |
| 364 |
} else { |
| 365 |
push @only_old_modes, $old_mode; |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
foreach my $new_mode (sort keys %new_modes) { |
| 370 |
next if defined $only_new_printers{get_printer($new_mode)}; |
| 371 |
if (!defined $old_modes{$new_mode}) { |
| 372 |
push @only_new_modes, $new_mode; |
| 373 |
} |
| 374 |
} |
| 375 |
|
| 376 |
my (@only_old_printers) = sort keys %only_old_printers; |
| 377 |
my (@only_new_printers) = sort keys %only_new_printers; |
| 378 |
|
| 379 |
if (! $detail1) { |
| 380 |
print "*** Changes from $ARGV[0] to $ARGV[1] ***\n\n"; |
| 381 |
print_changes("Printers removed from $ARGV[1]", 0, @only_old_printers); |
| 382 |
print_changes("Printers added to $ARGV[1]", 0, @only_new_printers); |
| 383 |
print_changes("Modes removed from $ARGV[1]", 1, @only_old_modes); |
| 384 |
print_changes("Modes added to $ARGV[1]", 1, @only_new_modes); |
| 385 |
} |
| 386 |
print_changes("Changed printing modes", 1 + $detail1, @changed_modes); |
| 387 |
} |
| 388 |
|
| 389 |
sub print_file($) { |
| 390 |
my ($file) = @_; |
| 391 |
my ($ctx) = initialize_file($file); |
| 392 |
my ($answer); |
| 393 |
while ($answer = get_next_datum($ctx)) { |
| 394 |
my ($sum) = $$answer[0]; |
| 395 |
my ($val) = $$answer[1]; |
| 396 |
my ($prn) = $$answer[2]; |
| 397 |
print "$sum *${prn}_$val.prn\n"; |
| 398 |
} |
| 399 |
} |