From fec3c23f04ce0ca97790f16ec74d53610aadbf2f Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Fri, 23 Jul 2010 18:33:22 -0400 Subject: [PATCH] factor out constants --- Pristine/Tar.pm | 48 ++++++++++++++++++++++++++++++++++++++++++++ pristine-bz2 | 14 ++++--------- pristine-gz | 62 ++++++++++----------------------------------------------- pristine-tar | 22 ++++++-------------- 4 files changed, 68 insertions(+), 78 deletions(-) diff --git a/Pristine/Tar.pm b/Pristine/Tar.pm index 76b62eb..cccdee3 100755 --- a/Pristine/Tar.pm +++ b/Pristine/Tar.pm @@ -95,4 +95,52 @@ sub dispatch { $run->(@ARGV); } + +# magic identification +use constant GZIP_ID1 => 0x1F; +use constant GZIP_ID2 => 0x8B; +use constant BZIP2_ID1 => 0x42; +use constant BZIP2_ID2 => 0x5a; + +# compression methods +# 0x00-0x07 are reserved +use constant GZIP_METHOD_DEFLATE => 0x08; +# 'h' for Bzip2 ('H'uffman coding) +use constant BZIP2_METHOD_HUFFMAN => 0x68; + +# flags +use constant { + GZIP_FLAG_FTEXT => 0, + GZIP_FLAG_FHCRC => 1, + GZIP_FLAG_FEXTRA => 2, + GZIP_FLAG_FNAME => 3, + GZIP_FLAG_FCOMMENT => 4, + # the rest are reserved +}; +# compression level +use constant { + GZIP_COMPRESSION_NORMAL => 0, + GZIP_COMPRESSION_BEST => 2, + GZIP_COMPRESSION_FAST => 4, +}; +# operating systems +use constant { + GZIP_OS_MSDOS => 0, + GZIP_OS_AMIGA => 1, + GZIP_OS_VMS => 2, + GZIP_OS_UNIX => 3, + GZIP_OS_VMCMS => 4, + GZIP_OS_ATARI => 5, + GZIP_OS_HPFS => 6, + GZIP_OS_MACINTOSH => 7, + GZIP_OS_ZSYSTEM => 8, + GZIP_OS_CPM => 9, + GZIP_OS_TOPS => 10, + GZIP_OS_NTFS => 11, + GZIP_OS_QDOS => 12, + GZIP_OS_RISCOS => 13, + GZIP_OS_VFAT => 14, + GZIP_OS_UNKNOWN => 255, +}; + 1 diff --git a/pristine-bz2 b/pristine-bz2 index d45013f..c368a3f 100755 --- a/pristine-bz2 +++ b/pristine-bz2 @@ -88,15 +88,6 @@ use File::Basename qw/basename/; use IPC::Open2; use IO::Handle; -use constant BZIP2_DEBUG => 1; - -# magic identification -use constant BZIP2_ID1 => 0x42; -use constant BZIP2_ID2 => 0x5a; - -# compression methods, 'h' for Bzip2 ('H'uffman coding), '0' for Bzip1 (deprecated) -use constant BZIP2_METHOD_HUFFMAN => 0x68; - my @supported_bzip2_programs = qw(bzip2 pbzip2 zgz); my $try=0; @@ -133,7 +124,10 @@ sub readbzip2 { # we actually want the value, not the ascii position $level-=48; - if ($id1 != BZIP2_ID1 || $id2 != BZIP2_ID2 || $method != BZIP2_METHOD_HUFFMAN || $level !~ /^[1-9]$/) { + if ($id1 != Pristine::Tar::BZIP2_ID1 || + $id2 != Pristine::Tar::BZIP2_ID2 || + $method != Pristine::Tar::BZIP2_METHOD_HUFFMAN || + $level !~ /^[1-9]$/) { die("This is not a valid BZip2 archive.\n"); } diff --git a/pristine-gz b/pristine-gz index 07c7d62..b73c0f8 100755 --- a/pristine-gz +++ b/pristine-gz @@ -94,50 +94,6 @@ use Pristine::Delta; use File::Basename qw/basename/; use IPC::Open2; -use constant GZIP_DEBUG => 1; - -# magic identification -use constant GZIP_ID1 => 0x1F; -use constant GZIP_ID2 => 0x8B; - -# compression methods, 0x00-0x07 are reserved -use constant GZIP_METHOD_DEFLATE => 0x08; - -# flags -use constant { - GZIP_FLAG_FTEXT => 0, - GZIP_FLAG_FHCRC => 1, - GZIP_FLAG_FEXTRA => 2, - GZIP_FLAG_FNAME => 3, - GZIP_FLAG_FCOMMENT => 4, - # the rest are reserved -}; -# compression level -use constant { - GZIP_COMPRESSION_NORMAL => 0, - GZIP_COMPRESSION_BEST => 2, - GZIP_COMPRESSION_FAST => 4, -}; -# operating systems -use constant { - GZIP_OS_MSDOS => 0, - GZIP_OS_AMIGA => 1, - GZIP_OS_VMS => 2, - GZIP_OS_UNIX => 3, - GZIP_OS_VMCMS => 4, - GZIP_OS_ATARI => 5, - GZIP_OS_HPFS => 6, - GZIP_OS_MACINTOSH => 7, - GZIP_OS_ZSYSTEM => 8, - GZIP_OS_CPM => 9, - GZIP_OS_TOPS => 10, - GZIP_OS_NTFS => 11, - GZIP_OS_QDOS => 12, - GZIP_OS_RISCOS => 13, - GZIP_OS_VFAT => 14, - GZIP_OS_UNKNOWN => 255, -}; - dispatch( commands => { usage => [\&usage], @@ -165,12 +121,14 @@ sub readgzip { my ($id1, $id2, $method, $flags, $timestamp, $level, $os, $name) = (unpack("CCCb8VCC", $chars), ''); - if ($id1 != GZIP_ID1 || $id2 != GZIP_ID2 || $method != GZIP_METHOD_DEFLATE) { + if ($id1 != Pristine::Tar::GZIP_ID1 || + $id2 != Pristine::Tar::GZIP_ID2 || + $method != Pristine::Tar::GZIP_METHOD_DEFLATE) { die("This is not a valid GZip archive.\n"); } my @flags = split(//, $flags); - if ($flags[GZIP_FLAG_FNAME]) { + if ($flags[Pristine::Tar::GZIP_FLAG_FNAME]) { # read a null-terminated string $name .= $chars while (read(GZIP, $chars, 1) == 1 && ord($chars) != 0); @@ -185,15 +143,15 @@ sub predictgzipargs { my @flags = @$flags; my @args; - unless ($flags[GZIP_FLAG_FNAME]) { + unless ($flags[Pristine::Tar::GZIP_FLAG_FNAME]) { push @args, '-n'; push @args, '-M' if $timestamp; } - if ($level == GZIP_COMPRESSION_BEST) { + if ($level == Pristine::Tar::GZIP_COMPRESSION_BEST) { push @args, '-9' } - elsif ($level == GZIP_COMPRESSION_FAST) { + elsif ($level == Pristine::Tar::GZIP_COMPRESSION_FAST) { push @args, '-1' } @@ -228,7 +186,7 @@ sub reproducegz { my @try; - if ($os == GZIP_OS_UNIX) { + if ($os == Pristine::Tar::GZIP_OS_UNIX) { # for 98% of the cases the simple heuristic above works # and it was produced by gnu gzip. push @try, ['--gnu', @args]; @@ -243,7 +201,7 @@ sub reproducegz { # set the Operating System flag to the one found in the original # archive - push @args, ("--osflag", $os) if $os != GZIP_OS_UNIX; + push @args, ("--osflag", $os) if $os != Pristine::Tar::GZIP_OS_UNIX; # many of the .gz out there are created using the BSD version of # gzip which is using the zlib library; try with our version of @@ -259,7 +217,7 @@ sub reproducegz { # Windows' NTFS gzip implementation; quirk is really really evil # it should be the last test: it can result in a corrupted archive! - if ($os == GZIP_OS_NTFS) { + if ($os == Pristine::Tar::GZIP_OS_NTFS) { pop @args; pop @args; # ntfs quirk implies NTFS osflag push @try, [@args, '--quirk', 'ntfs']; } diff --git a/pristine-tar b/pristine-tar index 2be0d44..9c63ba0 100755 --- a/pristine-tar +++ b/pristine-tar @@ -171,18 +171,6 @@ use File::Path; use File::Basename; use Cwd qw{getcwd abs_path}; -# magic identification -use constant GZIP_ID1 => 0x1F; -use constant GZIP_ID2 => 0x8B; -use constant BZIP2_ID1 => 0x42; -use constant BZIP2_ID2 => 0x5a; - -# compression methods, 0x00-0x07 are reserved -use constant GZIP_METHOD_DEFLATE => 0x08; - -# compression methods, 'h' for Bzip2 ('H'uffman coding), '0' for Bzip1 (deprecated) -use constant BZIP2_METHOD_HUFFMAN => 0x68; - # Force locale to C since tar may output utf-8 filenames differently # depending on the locale. $ENV{LANG}='C'; @@ -435,8 +423,9 @@ sub gendelta { my ($chars, $id1, $id2, $method); if (read(IN, $chars, 10) == 10 && (($id1, $id2, $method) = unpack("CCC", $chars)) && - $id1 == GZIP_ID1 && $id2 == GZIP_ID2 && - $method == GZIP_METHOD_DEFLATE) { + $id1 == Pristine::Tar::GZIP_ID1 && + $id2 == Pristine::Tar::GZIP_ID2 && + $method == Pristine::Tar::GZIP_METHOD_DEFLATE) { $compression='gz'; open(IN, "-|", "zcat", $tarball) || die "zcat: $!"; open(OUT, ">", "$tempdir/origtarball") || die "$tempdir/origtarball: $!"; @@ -448,8 +437,9 @@ sub gendelta { seek(IN, 0, 0) || die "seek: $!"; if (read(IN, $chars, 3) == 3 && (($id1, $id2, $method) = unpack("CCC", $chars)) && - $id1 == BZIP2_ID1 && $id2 == BZIP2_ID2 && - $method == BZIP2_METHOD_HUFFMAN) { + $id1 == Pristine::Tar::BZIP2_ID1 && + $id2 == Pristine::Tar::BZIP2_ID2 && + $method == Pristine::Tar::BZIP2_METHOD_HUFFMAN) { $compression='bz2'; open(IN, "-|", "bzcat", $tarball) || die "bzcat: $!"; open(OUT, ">", "$tempdir/origtarball") || die "$tempdir/origtarball: $!"; -- 2.7.4