factor out constants
authorJoey Hess <joey@kitenet.net>
Fri, 23 Jul 2010 22:33:22 +0000 (18:33 -0400)
committerJoey Hess <joey@kitenet.net>
Fri, 23 Jul 2010 22:33:22 +0000 (18:33 -0400)
Pristine/Tar.pm
pristine-bz2
pristine-gz
pristine-tar

index 76b62eb9f254f34e0003ea33134e78005f160d97..cccdee326f7ea5e1454debf53c798d20461ff12e 100755 (executable)
@@ -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
index d45013f6f132452eecccadcdd70f7bfac79d3fdf..c368a3f3ccb1e677e0de1bef9a7278aa1e03fcb5 100755 (executable)
@@ -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");
        }
 
index 07c7d62b1b53fa355632e4246d152b2307251fe1..b73c0f8f25e17e63f62c202522e2f6d05c30ac11 100755 (executable)
@@ -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'];
        }
index 2be0d44189b7730fcdf7e4b883f9dc8bb9e8d3e0..9c63ba0b3c523d3c1259982fe8a27539821bb3b8 100755 (executable)
@@ -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: $!";