summaryrefslogtreecommitdiff
path: root/bzlib-src/t/lib.pl
blob: c68dbebe6d88851bf263db0ce45825ad3322d0a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
use File::Copy;
use Cwd;
use Config;

BEGIN {
  eval { require File::Spec::Functions ; File::Spec::Functions->import( qw(catfile rel2abs) ) } ;
  *catfile = sub { return join( '/', @_ ) } if $@;
}

require VMS::Filespec if $^O eq 'VMS';

sub dump_block {
  my %block;
  ( $block{1}, $block{2} ) = @_;

  for ( my $i=0; $i<length($block{1}); $i+=16 ) {
    my %xbuf;

    foreach my $j ( 1, 2 ) {
      for ( my $k=0; $k<16 && $i+$k<length($block{$j}); $k++ ) {
	$xbuf{$j} .= ' ' if $xbuf{$j} && $k % 2 == 0;
	$xbuf{$j} .= unpack( "H2", substr($block{$j},$i+$k,1) );
      }

      printf STDERR "%03d %s\n", $i, $xbuf{$j};
    }

    print STDERR "\n";
  }
}

sub compare_binary_files {
  my ( $file1, $file2 ) = @_;
  my ( %fh, %buf, %ln, %counter );

  if ( -s $file1 != -s $file2 ) {
    print STDERR "files not the same size", " $file1 is ".(-s $file1), ", $file2 is ".(-s $file2),"\n";
    return 0;
  }

  open( $fh{1}, $file1 );
  open( $fh{2}, $file2 );

  my $same = 1;
  my $notdone;
  $counter{blocks}++;
  while ( !$notdone ) {
    $ln{1} = sysread( $fh{1}, $buf{1}, 512 );
    $ln{2} = sysread( $fh{2}, $buf{2}, 512 );
    if ( $ln{1} != $ln{2} ) {
      print STDERR "blocks not the same size\n";
      return 0;
    }
    if ($buf{1} ne $buf{2}) {
      print STDERR "block $counter{blocks} not the same\n";
      dump_block($buf{1}, $buf{2});
      return 0;
    }
    return 1 if $ln{1} == 0;
    $counter{blocks}++;
  }

  close( $fh{1} );
  close( $fh{2} );

  return 0;
}

sub display_file {
  my ( $file ) = @_;
  my $in;
  if ( !open( $in, $file ) ) {
    warn "Error: unable to open $file: $!\n";
  }
  else {
    while (<$in>) {
      print STDERR $_;
    }
    close($in);
  }
}
our $BZLIB_BIN ;
our $BZLIB_LIB ;
our $BZLIB_INCLUDE ;
our $BUILD_BZLIB ;

sub ParseCONFIG {
  my $CONFIG = shift || 'config.in' ;

  my ($k, $v) ;
  my @badkey = () ;
  my %Info = () ;
  my @Options = qw( BZLIB_INCLUDE BZLIB_LIB BUILD_BZLIB BZLIB_BIN ) ;
  my %ValidOption = map {$_, 1} @Options ;
  my %Parsed = %ValidOption ;
  my $debugf = 0;

  print STDERR "Parsing $CONFIG...\n" if $debugf;

  if (!open(F, "<$CONFIG")) {
    warn "warning: failed to open $CONFIG: $!\n";
  }
  else {
    while (<F>) {
      chomp;
      s/#.*$//;
      next if !/\S/;

      ($k, $v) = split(/\s*=\s*/, $_, 2) ;
      $k = uc $k ;

      if ($ValidOption{$k}) {
	delete $Parsed{$k} ;
	$Info{$k} = $v ;
      }
      else {
	push(@badkey, $k) ;
      }
    }
    close F ;
  }

  print STDERR "Unknown keys in $CONFIG ignored [@badkey]\n" if $debugf && scalar(@badkey) ;

  $BZLIB_INCLUDE = $ENV{'BZLIB_INCLUDE'} || $Info{'BZLIB_INCLUDE'} ;
  $BZLIB_LIB = $ENV{'BZLIB_LIB'} || $Info{'BZLIB_LIB'} ;
  $BZLIB_BIN = $ENV{'BZLIB_BIN'} || $Info{'BZLIB_BIN'} ;

  if ($^O eq 'VMS') {
    $BZLIB_INCLUDE = VMS::Filespec::vmspath($BZLIB_INCLUDE);
    $BZLIB_LIB = VMS::Filespec::vmspath($BZLIB_LIB);
    $BZLIB_BIN = VMS::Filespec::vmspath($BZLIB_BIN);
  }

  $BUILD_BZLIB = 1;

  print STDERR "Building internal libbz2 enabled\n" if $debugf ;

  print STDERR <<EOM if $debugf ;
INCLUDE	[$BZLIB_INCLUDE]
LIB	[$BZLIB_LIB]
BIN	[$BZLIB_BIN]

EOM
;

  print STDERR "Looks Good.\n" if $debugf;
}

ParseCONFIG('../config.in') ;

$::BZIP = 'bzip2'.$Config{exe_ext};
$::BZIP = $BZLIB_BIN ? catfile( $BZLIB_BIN, $::BZIP ) :
    -x $::BZIP ? rel2abs( $::BZIP ) : $::BZIP;

$ENV{PATH} .= ';' . getcwd() . '\\bzlib-src' if $^O =~ /win32/i; # just in case

$::debugf = $ENV{DEBUG};