bintools/evalid/EvalidCompare.pm.bak
author kelvzhu
Thu, 11 Nov 2010 17:31:39 +0800
changeset 680 e4701f5f5159
parent 676 b5e6747818a9
permissions -rw-r--r--
update feature database
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
655
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
     1
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
     2
# Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
     3
# All rights reserved.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
     4
# This component and the accompanying materials are made available
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
     5
# under the terms of the License "Eclipse Public License v1.0"
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
     6
# which accompanies this distribution, and is available
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
     7
# at the URL "http://www.eclipse.org/legal/epl-v10.html".
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
     8
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
     9
# Initial Contributors:
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    10
# Nokia Corporation - initial contribution.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    11
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    12
# Contributors:
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    13
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    14
# Description: 
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    15
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    16
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    17
package EvalidCompare;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    18
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    19
use strict;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    20
our $VERSION = '1.00';
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    21
use IO::Handle;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    22
use IO::File;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    23
use Cwd;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    24
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    25
use File::Temp qw/ tempfile tempdir /;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    26
use File::Find;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    27
use File::Path;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    28
use File::Basename;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    29
use File::Copy;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    30
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    31
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    32
# Constants.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    33
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    34
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    35
my %typeLookup = (
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    36
      'ARM PE-COFF executable' => 'ignore',
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    37
      'E32 EXE' => 'e32',
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    38
      'E32 DLL' => 'e32',
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    39
      'Uncompressed E32 EXE' => 'e32',
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    40
      'Uncompressed E32 DLL' => 'e32',
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    41
      'Compressed E32 EXE' => 'e32',
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    42
      'Compressed E32 DLL' => 'e32',
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    43
      'Intel DLL' => 'intel_pe',
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    44
      'Intel EXE' => 'intel_pe',
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    45
      'MSDOS EXE' => 'intel_pe',
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    46
      'Intel object' => 'intel',
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    47
      'Intel library' => 'intel',
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    48
      'ELF library' => 'elf',
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    49
      'ARM object' => 'arm',
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    50
      'ARM library' => 'arm',
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    51
      'unknown format' => 'identical',
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    52
      'Java class' => 'identical',
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    53
      'ZIP file' => 'zip',
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    54
      'Permanent File Store' => 'permanent_file_store',
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    55
      'SIS file' => 'identical',
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    56
      'MSVC database' => 'ignore',
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    57
      'MAP file' => 'map',
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    58
      'SGML file' => 'sgml',
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    59
      'Preprocessed text' => 'preprocessed_text',
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    60
      'ELF file' => 'elf',
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    61
      'Unknown COFF object' => 'identical',
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    62
      'Unknown library' => 'identical',
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    63
      'chm file' => 'chm_file',
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    64
	  'Header file' => 'header',
676
b5e6747818a9 add rofs image compare
lorewang
parents: 655
diff changeset
    65
	  'Distribution Policy' => 'distpol',
b5e6747818a9 add rofs image compare
lorewang
parents: 655
diff changeset
    66
	  'Rofs image' => 'img'
655
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    67
     );
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    68
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    69
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    70
# %TEMPDIR% and %FILE% are magic words for the expandor
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    71
# they will be replaced with suitable values when used
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    72
# they also enabled an order of expandor arguments where the filename is not last
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    73
my %typeHandler = (
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    74
      e32 => {reader => 'elf2e32 --dump --e32input=', filter => \&Elf2E32Filter},
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    75
      arm => {reader => 'nm --no-sort', filter => \&NmFilter, retry => 1, relative_paths => 1},
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    76
      elf => {reader => 'elfdump -i', filter => \&ElfDumpFilter, rawretry => 1},
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    77
      intel => {reader => '%EPOCROOT%epoc32\gcc_mingw\bin\nm --no-sort', filter => \&NmFilter, rawretry => 1, relative_paths => 1, skipstderr => 1},
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    78
      intel_pe => {reader => 'pe_dump', filter => \&FilterNone, rawretry => 1},
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    79
	  zip => {reader => '"'.$FindBin::Bin.'/unzip" -l -v', filter => \&UnzipFilter, rawretry => 1},
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    80
      map => {filter => \&MapFilter, skipblanks => 1},
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    81
      sgml => {filter => \&SgmlFilter},
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    82
      preprocessed_text => {filter => \&PreprocessedTextFilter},
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    83
      permanent_file_store => {reader => 'pfsdump -c -v', filter => \&PermanentFileStoreFilter, rawretry => 1, relative_paths => 1},
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    84
      ignore => {filter => \&FilterAll},
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    85
      chm_file => {expandor => 'hh -decompile %TEMPDIR% %FILE%', rawretry => 1},
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    86
	  header => {filter => \&FilterCVSTags},
676
b5e6747818a9 add rofs image compare
lorewang
parents: 655
diff changeset
    87
	  distpol => {filter => \&DistributionPolicyFilter},
b5e6747818a9 add rofs image compare
lorewang
parents: 655
diff changeset
    88
      img => {expandor => 'readimage -z %TEMPDIR% %FILE%', rawretry => 1}
655
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    89
     );
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    90
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    91
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    92
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    93
# Globals.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    94
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    95
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    96
my $log;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    97
my $verbose;
676
b5e6747818a9 add rofs image compare
lorewang
parents: 655
diff changeset
    98
my $keepgoing;
655
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
    99
my $toRoot;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   100
my $dumpDir;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   101
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   102
undef $dumpDir;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   103
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   104
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   105
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   106
# Public.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   107
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   108
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   109
sub CompareFiles {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   110
  my $file1 = shift;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   111
  my $file2 = shift;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   112
  $verbose = defined($_[0]) ? shift : 0;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   113
  $log = defined($_[0]) ? shift : *STDOUT;
676
b5e6747818a9 add rofs image compare
lorewang
parents: 655
diff changeset
   114
  $keepgoing = defined($_[0]) ? shift : 0;
b5e6747818a9 add rofs image compare
lorewang
parents: 655
diff changeset
   115
  
655
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   116
  # Try binary compare first (to keep semantics the same as evalid)...
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   117
  if (DoCompareFiles($file1, $file2, 'unknown format')) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   118
    return 1,'identical';
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   119
  }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   120
  my $type = IdentifyFileType($file1);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   121
  if ($typeLookup{$type} eq 'identical') {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   122
    return 0,$type; # We already know a binary compare is going to return false.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   123
  }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   124
  return DoCompareFiles($file1, $file2, $type),$type;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   125
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   126
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   127
sub GenerateSignature {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   128
  my $file = shift;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   129
  $dumpDir = shift;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   130
  $verbose = defined($_[0]) ? shift : 0;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   131
  $log = defined($_[0]) ? shift : *STDOUT;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   132
  my $md5;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   133
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   134
  if (eval "require Digest::MD5") { # Prefer Digest::MD5, if available.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   135
    $md5 = Digest::MD5->new();
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   136
  } elsif (eval "require MD5") { # Try old version of MD5, if available.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   137
    $md5 = new MD5;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   138
  } elsif (eval "require Digest::Perl::MD5") { # Try Perl (Slow) version of MD5, if available.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   139
    $md5 = Digest::Perl::MD5->new();
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   140
  } else {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   141
    die "Error: Cannot load any MD5 Modules";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   142
  }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   143
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   144
  my $type = IdentifyFileType($file);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   145
  WriteFilteredData($file, $type, $md5);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   146
  return $md5->hexdigest(), $type;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   147
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   148
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   149
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   150
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   151
# Private.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   152
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   153
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   154
sub IdentifyFileType {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   155
  my $file = shift;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   156
  open (FILE, $file) or die "Error: Couldn't open \"$file\" for reading: $!\n";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   157
  binmode (FILE);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   158
  my $typeBuf;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   159
  read (FILE, $typeBuf, 512);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   160
  close (FILE);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   161
  my ($uid1, $uid2, $uid3, $checksum) = unpack "V4", $typeBuf;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   162
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   163
  # NB. Need to use the s modifier so that '.' will match \x0A
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   164
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   165
  if ($typeBuf =~ /^.\x00\x00\x10.{12}EPOC.{8}(....).{12}(.)..(.)/s) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   166
    # E32 Image file with a 0x100000?? UID1
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   167
    # $2 is the flag field indicating an EXE or a DLL
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   168
    # $3 is the flag byte indicating compressable executables
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   169
    # $1 is the format field indicating compression type
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   170
    # See e32tools\inc\e32image.h
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   171
    #
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   172
    my $typename = "E32 EXE";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   173
    if ((ord $2) & 0x1) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   174
      $typename = "E32 DLL";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   175
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   176
    if ((ord $3) >= 0x1) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   177
    if ((ord $1) != 0) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   178
        $typename = "Compressed $typename";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   179
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   180
    else {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   181
        $typename = "Uncompressed $typename";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   182
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   183
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   184
    return $typename;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   185
  }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   186
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   187
  if ($typeBuf =~ /^\x4D\x5A.{38}\x00{20}(....)/s) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   188
    # A standard 64-byte MS-DOS header with e_magic == IMAGE_DOS_SIGNATURE
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   189
    # $1 is e_lfanew, which we expect to point to a COFF header
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   190
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   191
    my $offset = unpack "V",$1;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   192
    if ($offset + 24 <= length $typeBuf) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   193
      $typeBuf = substr $typeBuf, $offset;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   194
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   195
    else {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   196
      open FILE, $file or die "Error: Couldn't open \"$file\" for reading: $!\n";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   197
      binmode FILE;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   198
      seek FILE, $offset, 0;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   199
      read FILE, $typeBuf, 512;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   200
      close FILE;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   201
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   202
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   203
    if ($typeBuf =~ /^PE\0\0\x4c\x01.{16}(..)/s) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   204
      # A PE signature "PE\0\0" followed by a COFF header with
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   205
      # machine type IMAGE_FILE_MACHINE_I386
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   206
      # $1 is the characteristics field
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   207
      #
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   208
      if ((unpack "v",$1) & 0x2000) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   209
    return "Intel DLL";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   210
      }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   211
      else {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   212
    return "Intel EXE";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   213
      }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   214
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   215
  elsif($typeBuf =~ /^PE\0\0\0\x0a/) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   216
  # A PE signature "PE\0\0" followed by ARM COFF file magic value 0xA00
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   217
    return "ARM PE-COFF executable";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   218
  }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   219
    else {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   220
      return "MSDOS EXE";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   221
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   222
  }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   223
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   224
  if ($typeBuf =~ /^(\x4c\x01|\x00\x0A).(\x00|\x01).{4}...\x00/s) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   225
    # COFF header with less than 512 sections and a symbol table
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   226
    # at an offset no greater than 0x00ffffff
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   227
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   228
    if ($1 eq "\x4c\x01") {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   229
      return "Intel object";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   230
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   231
    elsif ($1 eq "\x00\x0A") {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   232
      return "ARM object";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   233
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   234
    else {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   235
      return "Unknown COFF object";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   236
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   237
  }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   238
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   239
  if ($typeBuf =~ /^!<arch>\x0A(.{48}([0-9 ]{9})\x60\x0A(......))/s) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   240
    # library - could be MARM or WINS
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   241
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   242
    $typeBuf = $1;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   243
    my $member_start = 8;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   244
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   245
    open (FILE, $file) or die "Error: Couldn't open \"$file\" for reading: $!\n";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   246
    binmode (FILE);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   247
    
676
b5e6747818a9 add rofs image compare
lorewang
parents: 655
diff changeset
   248
    while ($typeBuf =~ /^.{48}([0-9 ]{9}).\x60\x0A(......)/s) {
655
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   249
      # $1 is the size of the archive member, $2 is first 6 bytes of the file
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   250
      # There may be several different sorts of file in the archive, and we
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   251
      # need to scan through until we find a type we recognize:
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   252
      # $2 == 0x0A00 would be ARM COFF, 0x014C would be Intel COFF
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   253
      if ($2 =~ /^\x00\x0A/) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   254
  close FILE;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   255
  return "ARM library";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   256
      }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   257
      if ($2 =~ /^\x4C\x01/) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   258
  close FILE;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   259
  return "Intel library";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   260
      }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   261
	  my $elfBuf =  $2;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   262
      if ($2 =~ /^\x7F\x45\x4C\x46/) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   263
  close FILE;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   264
		my $dataEncodingLib = substr($elfBuf, 5, 6);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   265
		if ( $dataEncodingLib =~ /^\x02/) {	
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   266
			# e_ident[EI_DATA] == 2 (Data Encoding ELFDATA2MSB - big endian)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   267
			# this is not supported by Elfdump hence it is treated as 'unknown format'
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   268
		return 'unknown library';
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   269
		}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   270
		else {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   271
		return "ELF library";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   272
		}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   273
	 }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   274
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   275
      $member_start += 60 + $1;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   276
      if ($member_start & 0x1) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   277
        $member_start += 1;  # align to multiple of 2 bytes
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   278
      }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   279
      seek FILE, $member_start, 0;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   280
      read FILE, $typeBuf, 512;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   281
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   282
    close FILE;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   283
    return "Unknown library";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   284
  }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   285
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   286
  if ($typeBuf =~ /^\xCA\xFE\xBA\xBE/) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   287
    # Java class file - should have match as a straight binary comparison
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   288
    return "Java class";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   289
  }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   290
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   291
  if ($typeBuf =~ /^PK\x03\x04/) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   292
    # ZIP file
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   293
    return "ZIP file";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   294
  }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   295
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   296
  if ($uid1 && $uid1==0x10000050) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   297
    # Permanent File Store
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   298
    return "Permanent File Store";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   299
  }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   300
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   301
  if ($uid1 && $uid2 && $uid3 && $checksum && $uid3==0x10000419) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   302
    if (($uid1==0x100002c3 && $uid2==0x1000006d && $checksum==0x128ca96f)  # narrow
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   303
  ||  ($uid1==0x10003b0b && $uid2==0x1000006d && $checksum==0x75e21a1d)  # unicode
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   304
  ||  ($uid1==0x10009205 && $uid2==0x10003a12 && $checksum==0x986a0c25)) # new format
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   305
      {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   306
      # SIS file
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   307
      return "SIS file";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   308
      }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   309
  }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   310
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   311
  if ($typeBuf =~ /^Microsoft [^\x0A]+ [Dd]atabase/s) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   312
    return "MSVC database";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   313
  }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   314
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   315
  if ($typeBuf =~ /^\S.+ needed due to / || $typeBuf =~ /^Archive member included.*because of file/) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   316
    # GCC MAP file
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   317
    return "MAP file";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   318
  }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   319
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   320
  if ($typeBuf =~ /Preferred load address is/) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   321
    # Developer Studio MAP file
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   322
    return "MAP file";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   323
  }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   324
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   325
  if ($typeBuf =~ /^Address\s+Size\s+Name\s+Subname\s+Module/) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   326
    # CodeWarrior MAP file
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   327
    return "MAP file";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   328
  }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   329
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   330
  if ($typeBuf =~ /^ARM Linker,/) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   331
    # RVCT MAP file
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   332
    return "MAP file";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   333
  }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   334
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   335
  if ($typeBuf =~ /<!DOCTYPE/i) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   336
    # XML or HTML file - need to ignore javadoc generation dates
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   337
    return "SGML file";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   338
  }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   339
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   340
  if ($typeBuf =~ /^# 1 ".*"(\x0D|\x0A)/s) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   341
    # Output of CPP
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   342
    return "Preprocessed text";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   343
  }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   344
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   345
  if ($typeBuf =~ /^\x7F\x45\x4C\x46/) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   346
	my $dataEncoding = substr($typeBuf, 5, 6);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   347
	if ( $dataEncoding =~ /^\x02/) {	
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   348
	  # e_ident[EI_DATA] == 2 (Data Encoding ELFDATA2MSB - big endian)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   349
	  # this is not supported by Elfdump hence it is treated as 'unknown format'
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   350
	   return 'unknown format';
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   351
	}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   352
	else {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   353
		return "ELF file";;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   354
	}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   355
   }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   356
  
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   357
  if ($typeBuf =~/^ITSF/) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   358
    # chm file
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   359
    return "chm file";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   360
  }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   361
676
b5e6747818a9 add rofs image compare
lorewang
parents: 655
diff changeset
   362
  if ($typeBuf =~/^(ROFS|ROFx)/) {
b5e6747818a9 add rofs image compare
lorewang
parents: 655
diff changeset
   363
    # img file
b5e6747818a9 add rofs image compare
lorewang
parents: 655
diff changeset
   364
    return "Rofs image";
b5e6747818a9 add rofs image compare
lorewang
parents: 655
diff changeset
   365
  }
b5e6747818a9 add rofs image compare
lorewang
parents: 655
diff changeset
   366
655
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   367
  if ($file =~ m/\.(iby|h|hby|hrh|oby|rsg|cpp)$/i) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   368
    return "Header file";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   369
  }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   370
  
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   371
  if ($file =~ /distribution\.policy$/i) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   372
	return "Distribution Policy"
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   373
  }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   374
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   375
  return 'unknown format';
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   376
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   377
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   378
sub WriteFilteredData {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   379
  my $file = shift;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   380
  my $type = shift;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   381
  my $md5 = shift;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   382
  my $dumpDirExpandedFile = shift;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   383
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   384
  my (@dumpDirBuffer);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   385
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   386
  unless (exists $typeLookup{$type}) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   387
    die "Invalid file type \"$type\"";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   388
  }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   389
  $type = $typeLookup{$type};
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   390
  
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   391
  # Check to see if this file type requires expanding first
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   392
  if (exists $typeHandler{$type}->{expandor})
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   393
  {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   394
    my $expandor = $typeHandler{$type}->{expandor};
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   395
    # Create two temporary directories
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   396
    my $tempdir = tempdir ( "EvalidExpand_XXXXXX", DIR => File::Spec->tmpdir, CLEANUP => 1);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   397
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   398
    # Build the Expandor commandline
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   399
    $expandor =~ s/%TEMPDIR%/$tempdir/g;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   400
    $expandor =~ s/%FILE%/$file/g;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   401
    
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   402
    # Expand files
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   403
    my $output = `$expandor 2>&1`;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   404
    print($log "Expanding using $expandor output was:-\n$output") if ($verbose);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   405
    if ($? > 0)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   406
    {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   407
      print ($log "$expandor exited with $?") if ($verbose);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   408
      # set type to be identical for retry if raw
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   409
      if ($typeHandler{$type}->{rawretry} == 1)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   410
      {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   411
        $type = 'identical';
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   412
      } else {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   413
        print "ERROR: failed to start $expandor (" .($?). ") - reporting failure\n";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   414
      }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   415
    } else {    
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   416
      # Process all files in $tempdir
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   417
      my @FileList;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   418
      find(sub { push @FileList, $File::Find::name if (! -d);}, $tempdir);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   419
      foreach my $expandfile (@FileList)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   420
      {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   421
	  my $dumpDirExpandedFilename = "";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   422
      	
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   423
      if ($dumpDir)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   424
      	{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   425
	  	$dumpDirExpandedFilename = $expandfile;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   426
		$dumpDirExpandedFilename =~ s/^.*EvalidExpand_\w+//;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   427
	  	$dumpDirExpandedFilename = $file.$dumpDirExpandedFilename;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   428
      	}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   429
      	
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   430
      my $type = IdentifyFileType($expandfile);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   431
      
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   432
      &WriteFilteredData($expandfile, $type, $md5, $dumpDirExpandedFilename);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   433
      }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   434
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   435
  }  elsif ($type ne 'identical') {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   436
    unless (exists $typeHandler{$type}) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   437
      die "Invalid comparison type \"$type\"";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   438
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   439
    my $reader = $typeHandler{$type}->{reader};
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   440
    my $filter = $typeHandler{$type}->{filter};
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   441
    my $retry = $typeHandler{$type}->{retry} || 0;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   442
    my $rawretry = $typeHandler{$type}->{rawretry} || 0;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   443
	my $skipblanks = $typeHandler{$type}->{skipblanks} || 0;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   444
    my $relativePaths = $typeHandler{$type}->{relative_paths} || 0;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   445
    my $dosPaths = $typeHandler{$type}->{dos_paths} || 0;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   446
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   447
	my $skipstderr = $typeHandler{$type}->{skipstderr} || 0;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   448
	my $redirectstd = "2>&1";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   449
	
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   450
	if ($skipstderr) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   451
		$redirectstd = "2>NUL";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   452
	}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   453
	  
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   454
    if ($relativePaths) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   455
      $file = RelativePath($file);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   456
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   457
    if ($dosPaths) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   458
      $file =~ s/\//\\/g;       # convert to DOS-style backslash separators
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   459
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   460
    
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   461
    my $raw;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   462
    if ($reader) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   463
      $raw = IO::File->new("$reader \"$file\" $redirectstd |") or die "Error: Couldn't run \"$reader $file\": $!\n";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   464
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   465
    else {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   466
      $raw = IO::File->new("$file") or die "Error: Couldn't open \"$file\": $!\n";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   467
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   468
    while (my $line = <$raw>) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   469
      &$filter(\$line);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   470
	  next if $skipblanks and $line =~ /^\s*$/;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   471
      $md5->add($line);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   472
      push @dumpDirBuffer, $line if ($dumpDir);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   473
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   474
    Drain($raw);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   475
    $raw->close();
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   476
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   477
    # Retry once if reader failed and reader has retry specified
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   478
    if ((($?>>8) != 0) && ($retry == 1))
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   479
    {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   480
      print "Warning: $reader failed (" .($?>>8). ") on $file - retrying\n";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   481
      # Reset MD5
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   482
      $md5->reset;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   483
      undef @dumpDirBuffer if ($dumpDir);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   484
      $raw = IO::File->new("$reader \"$file\" $redirectstd |") or die "Error: Couldn't run \"$reader $file\": $!\n";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   485
      while (my $line = <$raw>)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   486
      {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   487
        &$filter(\$line);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   488
		next if $skipblanks and $line =~ /^\s*$/;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   489
        $md5->add($line);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   490
        push @dumpDirBuffer, $line if ($dumpDir);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   491
      }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   492
      Drain($raw);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   493
      $raw->close();
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   494
      if (($?>>8) != 0)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   495
      {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   496
        print "Error: $reader failed again (" .($?>>8) .") on $file - reporting failure\n";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   497
      }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   498
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   499
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   500
    # Retry as raw if specified
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   501
    if (($?>>8) != 0) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   502
      if ($rawretry)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   503
      {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   504
          if ($reader =~ /^pfsdump/) { 
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   505
              print "Warning: $reader failed (". ($?>>8) .") on file $file - retrying as raw binary\n";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   506
          }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   507
          else {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   508
              print "Info: something wrong to execute $reader (". ($?>>8) .") on file $file - retrying as raw binary\n";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   509
          }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   510
          # Set type to be identical so it will try it as a raw binary stream
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   511
          $type = 'identical';
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   512
      } else {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   513
        print "Error: $reader failed (". ($?>>8) .") on file $file - not retrying as raw binary\n";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   514
      }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   515
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   516
  }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   517
  if ($type eq 'identical') {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   518
    # Reset md5 as it might have been used in reader section
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   519
    $md5->reset;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   520
	undef @dumpDirBuffer if ($dumpDir);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   521
    # Treat 'identical' as a special case - no filtering, just write raw binary stream.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   522
    my $raw = IO::File->new($file) or die "Error: Couldn't open \"$file\" for reading: $!\n";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   523
    binmode($raw);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   524
    my $buf;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   525
    while ($raw->read($buf, 4096)) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   526
      $md5->add($buf);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   527
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   528
    $raw->close();
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   529
  }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   530
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   531
  my $dumpDirFilename = $file;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   532
  $dumpDirFilename = $dumpDirExpandedFile if ($dumpDirExpandedFile);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   533
  dumpDescriptiveOutput ($file, $dumpDirFilename, @dumpDirBuffer) if ($dumpDir);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   534
  
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   535
  # Make sure the $? is reset for the next file otherwise it will report errors
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   536
  $? = 0;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   537
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   538
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   539
sub DoCompareFiles {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   540
  my $file1 = shift;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   541
  my $file2 = shift;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   542
  my $type = shift;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   543
  my $same = 0;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   544
  unless (exists $typeLookup{$type}) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   545
    die "Invalid file type \"$type\"";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   546
  }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   547
  
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   548
  $type = $typeLookup{$type};
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   549
  
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   550
  # Check to see if this file type requires expanding first
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   551
  if (exists $typeHandler{$type}->{expandor})
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   552
  {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   553
    $same = &ExpandAndCompareFiles($file1, $file2, $typeHandler{$type}->{expandor});
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   554
    # Check for Expanding error
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   555
    if ($same == -1)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   556
    {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   557
      if ($typeHandler{$type}->{rawretry} == 1)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   558
      {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   559
        # Set type to be identical if rawrety is set
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   560
        $type = 'identical';
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   561
        print($log "Warning: Expandor $typeHandler{$type}->{expandor} failed for $file1 or $file2 : retrying as raw\n") if ($verbose);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   562
      } else {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   563
        die "Error: Expandor $typeHandler{$type}->{expandor} failed for $file1 or $file2\n";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   564
      }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   565
    } else {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   566
      return $same;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   567
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   568
  }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   569
    
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   570
  if ($type ne 'identical')
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   571
  {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   572
    unless (exists $typeHandler{$type}) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   573
      die "Invalid comparison type \"$type\"";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   574
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   575
    
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   576
    my $reader = $typeHandler{$type}->{reader};
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   577
    my $filter = $typeHandler{$type}->{filter};
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   578
    my $retry = $typeHandler{$type}->{retry} || 0;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   579
	my $skipblanks= $typeHandler{$type}->{skipblanks} || 0;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   580
    my $rawretry = $typeHandler{$type}->{rawretry} || 0;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   581
    my $relativePaths = $typeHandler{$type}->{relative_paths} || 0;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   582
	my $skipstderr = $typeHandler{$type}->{skipstderr} || 0;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   583
	my $redirectstd = "2>&1";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   584
	
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   585
	if ($skipstderr) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   586
		$redirectstd = "2>NUL";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   587
	}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   588
    
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   589
    if ($relativePaths) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   590
      $file1 = RelativePath($file1);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   591
      $file2 = RelativePath($file2);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   592
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   593
    my $fileHandle1;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   594
    my $fileHandle2;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   595
    if ($reader) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   596
      $fileHandle1 = IO::File->new("$reader \"$file1\" $redirectstd |") or die "Error: Couldn't run \"$reader $file1\": $!\n";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   597
      $fileHandle2 = IO::File->new("$reader \"$file2\" $redirectstd |") or die "Error: Couldn't run \"$reader $file2\": $!\n";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   598
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   599
    else {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   600
      $fileHandle1 = IO::File->new("$file1") or die "Error: Couldn't open \"$file1\": $!\n";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   601
      $fileHandle2 = IO::File->new("$file2") or die "Error: Couldn't open \"$file2\": $!\n";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   602
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   603
	$same = CompareTexts($fileHandle1, $fileHandle2, $filter, $file1, $skipblanks);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   604
    Drain($fileHandle1, $fileHandle2);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   605
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   606
    $fileHandle1->close();
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   607
    my $status1 = $?>>8;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   608
    $fileHandle2->close();
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   609
    my $status2 = $?>>8;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   610
    if (($retry) && ($status1 != 0 or $status2 != 0))
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   611
    {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   612
      print ($log "Warning: $reader failed ($status1, $status2) - retrying\n");
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   613
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   614
      # Repeat previous code by hand, rather than calling DoCompareFiles
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   615
      # again: if it's a systematic failure that would be a never ending loop...
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   616
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   617
      $fileHandle1 = IO::File->new("$reader \"$file1\" $redirectstd |") or die "Error: Couldn't run \"$reader $file1\": $!\n";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   618
      $fileHandle2 = IO::File->new("$reader \"$file2\" $redirectstd |") or die "Error: Couldn't run \"$reader $file2\": $!\n";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   619
	  $same = CompareTexts($fileHandle1, $fileHandle2, $filter, $file1, $skipblanks);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   620
      Drain($fileHandle1, $fileHandle2);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   621
      $fileHandle1->close();
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   622
      $status1 = $?>>8;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   623
      $fileHandle2->close();
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   624
      $status2 = $?>>8;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   625
      if ($status1 != 0 or $status2 != 0)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   626
      {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   627
        print ($log "Warning: $reader failed again ($status1, $status2) - reporting failure\n");
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   628
        $same = 0;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   629
      }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   630
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   631
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   632
    # Retry as raw if specified
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   633
    if (($rawretry)&& ($status1 != 0 or $status2 != 0))
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   634
    {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   635
      if ($rawretry)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   636
      {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   637
        print ($log "Warning: $reader failed (" .($?>>8). ") on a file retrying as raw binary\n");
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   638
        # Set type to be identical so it will try it as a raw binary stream
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   639
        $type = 'identical';
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   640
      } else {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   641
        print ($log "Error: $reader failed (" .($?>>8). ") on a file not retrying as raw binary\n");
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   642
      }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   643
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   644
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   645
  }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   646
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   647
  if ($type eq 'identical') {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   648
    # Treat 'identical' as a special case - no filtering, just do raw binary stream comparison.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   649
    my $fileHandle1 = IO::File->new($file1) or die "Error: Couldn't open \"$file1\" for reading: $!\n";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   650
    my $fileHandle2 = IO::File->new($file2) or die "Error: Couldn't open \"$file2\" for reading: $!\n";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   651
    binmode($fileHandle1);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   652
    binmode($fileHandle2);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   653
    $same = CompareStreams($fileHandle1, $fileHandle2, $file1);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   654
  }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   655
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   656
  # Make sure the $? is reset for the next file otherwise it will report errors
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   657
  $? = 0;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   658
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   659
  return $same;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   660
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   661
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   662
sub CompareStreams {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   663
  my $fileHandle1 = shift;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   664
  my $fileHandle2 = shift;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   665
  my $filename = shift;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   666
  my $same = 1;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   667
  my $offset = -4096;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   668
  my $buf1;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   669
  my $buf2;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   670
  while ($same) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   671
    my $len1 = $fileHandle1->read($buf1, 4096);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   672
    my $len2 = $fileHandle2->read($buf2, 4096);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   673
    if ($len1 == 0 and $len2 == 0) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   674
      return 1;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   675
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   676
    $same = $buf1 eq $buf2;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   677
    $offset += 4096;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   678
  }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   679
  if ($verbose) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   680
    my @bytes1 = unpack "C*", $buf1;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   681
    my @bytes2 = unpack "C*", $buf2;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   682
    foreach my $thisByte (@bytes1) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   683
      if ($thisByte != $bytes2[0]) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   684
	printf $log "Binary comparison: %s failed at byte %d: %02x != %02x\n", $filename, $offset, $thisByte, $bytes2[0];
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   685
	last;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   686
      }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   687
      shift @bytes2;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   688
      $offset+=1;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   689
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   690
  }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   691
  return 0;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   692
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   693
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   694
sub NextSignificantLine {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   695
	my $filehandle = shift;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   696
	my $linenumber = shift;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   697
	my $cleanersub = shift;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   698
	my $skipblanks = shift;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   699
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   700
	while (!eof($filehandle)) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   701
		my $line = <$filehandle>;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   702
		$$linenumber++;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   703
		$cleanersub->(\$line);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   704
		return $line if !$skipblanks or $line !~ /^\s*$/;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   705
	}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   706
	return undef; # on eof
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   707
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   708
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   709
sub CompareTexts {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   710
	my $filehandle1 = shift;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   711
	my $filehandle2 = shift;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   712
	my $cleaner = shift;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   713
	my $filename = shift;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   714
	my $skipblanks = shift;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   715
	my $lineNum1 = 0;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   716
	my $lineNum2 = 0;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   717
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   718
	while (1) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   719
		my $line1 = NextSignificantLine($filehandle1, \$lineNum1, $cleaner, $skipblanks);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   720
		my $line2 = NextSignificantLine($filehandle2, \$lineNum2, $cleaner, $skipblanks);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   721
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   722
		return 0 if defined($line1) != defined($line2); # eof vs. significant content
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   723
		return 1 if !defined($line1) and !defined($line2); # eof on both files
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   724
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   725
		if ($line1 ne $line2) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   726
			printf($log "Text comparison: %s failed at lines %d/%d\n< %s> %s\n",
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   727
			$filename, $lineNum1, $lineNum2, $line1, $line2) if $verbose;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   728
			return 0;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   729
		}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   730
	}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   731
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   732
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   733
sub Drain {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   734
  foreach my $handle (@_) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   735
    while (my $line = <$handle>) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   736
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   737
  }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   738
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   739
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   740
sub RelativePath {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   741
  my $name = shift;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   742
  if (($name =~ /^\\[^\\]/) || ($name =~ /^\//)) {  # abs path (unix or windows), not UNC
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   743
    unless ($toRoot) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   744
      $toRoot = getcwd();
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   745
      $toRoot =~ s/\//\\/g;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   746
      $toRoot =~ s/^[a-zA-Z]:\\(.*)$/$1/;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   747
      $toRoot =~ s/[^\\]+/../g;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   748
      if ($toRoot =~ /^$/) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   749
  $toRoot = '.';    # because we are starting in the root
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   750
      }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   751
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   752
    return $toRoot.$name;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   753
  }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   754
  return $name;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   755
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   756
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   757
# Function to expand compressed formats and recompare expanded files
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   758
# This is the file against file implementation
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   759
# It returns one identical / non indentical result based on all files in the
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   760
# expanded content. i.e one non identical expanded file will cause the non
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   761
# expanded file to be reported as non identical.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   762
sub ExpandAndCompareFiles
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   763
{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   764
  my $file1 = shift;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   765
  my $file2 = shift;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   766
  my $expandor = shift;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   767
  
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   768
  # Create two temporary directories
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   769
  my $tempdir1 = tempdir ( "EvalidExpand_XXXXXX", DIR => File::Spec->tmpdir, CLEANUP => 1);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   770
  my $tempdir2 = tempdir ( "EvalidExpand_XXXXXX", DIR => File::Spec->tmpdir, CLEANUP => 1);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   771
  
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   772
  # Build the Expandor commandline
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   773
  my $cmd1 = $expandor;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   774
  $cmd1 =~ s/%TEMPDIR%/$tempdir1/g;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   775
  $cmd1 =~ s/%FILE%/$file1/g;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   776
  
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   777
  my $cmd2 = $expandor;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   778
  $cmd2 =~ s/%TEMPDIR%/$tempdir2/g;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   779
  $cmd2 =~ s/%FILE%/$file2/g;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   780
  
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   781
  # Expand files
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   782
  my $output = `$cmd1 2>&1`;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   783
  print($log "Expanding using $cmd1 output was:-\n$output") if ($verbose);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   784
  if ($? > 0)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   785
  {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   786
    print ($log "$cmd1 exited with $?") if ($verbose);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   787
    return -1;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   788
  }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   789
  
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   790
  $output = `$cmd2 2>&1`;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   791
  print($log "Expanding using $cmd2 output was:-\n$output") if ($verbose);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   792
  if ($? > 0)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   793
  {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   794
    print ($log "$cmd2 exited with $?") if ($verbose);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   795
    return -1;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   796
  }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   797
  
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   798
  # Produce full filelist of expanded files without directory names
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   799
  my %iFileList1;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   800
  $tempdir1 =~ s#\\#/#g; # Make sure the dir seperators are / for consistent and easier matching.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   801
  find sub {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   802
            if (!-d)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   803
            {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   804
              my ($fixedpath) = $File::Find::name;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   805
              $fixedpath =~ s#\\#/#g;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   806
              my ($relpath) = $File::Find::name =~ /$tempdir1(.*)/i;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   807
              $iFileList1{$relpath} = "left";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   808
            }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   809
          }, $tempdir1;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   810
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   811
  my %iFileList2;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   812
  $tempdir2 =~ s#\\#/#g; # Make sure the dir seperators are / for consistent and easier matching.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   813
  find sub {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   814
            if (!-d)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   815
            {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   816
              my ($fixedpath) = $File::Find::name;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   817
              $fixedpath =~ s#\\#/#g;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   818
              my ($relpath) = $File::Find::name =~ /$tempdir2(.*)/i;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   819
              $iFileList2{$relpath} = "right";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   820
            }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   821
          }, $tempdir2;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   822
  
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   823
  #Work out the if the two file lists are different
676
b5e6747818a9 add rofs image compare
lorewang
parents: 655
diff changeset
   824
  my @tmpfiles;
655
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   825
  foreach my $file (sort keys %iFileList1)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   826
  {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   827
    if (! defined $iFileList2{$file})
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   828
    {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   829
      # If the filename does not exist in the second filelist the compressed files cannot be the same.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   830
      print ($log "Did not find $file in $file2\n") if ($verbose);
676
b5e6747818a9 add rofs image compare
lorewang
parents: 655
diff changeset
   831
      if(!$keepgoing){
b5e6747818a9 add rofs image compare
lorewang
parents: 655
diff changeset
   832
      	return 0;
b5e6747818a9 add rofs image compare
lorewang
parents: 655
diff changeset
   833
      }else{
b5e6747818a9 add rofs image compare
lorewang
parents: 655
diff changeset
   834
      	push @tmpfiles, $file;
b5e6747818a9 add rofs image compare
lorewang
parents: 655
diff changeset
   835
      }
655
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   836
    } else {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   837
      delete $iFileList2{$file}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   838
    }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   839
  }
676
b5e6747818a9 add rofs image compare
lorewang
parents: 655
diff changeset
   840
  foreach my $file (@tmpfiles)
b5e6747818a9 add rofs image compare
lorewang
parents: 655
diff changeset
   841
  {
b5e6747818a9 add rofs image compare
lorewang
parents: 655
diff changeset
   842
   	delete $iFileList1{$file};
b5e6747818a9 add rofs image compare
lorewang
parents: 655
diff changeset
   843
  }
655
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   844
  
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   845
  # There are extra files in the second compressed file therefore the compressed files cannot be the same.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   846
  if (scalar(keys %iFileList2) > 0)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   847
  {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   848
    print ($log "$file2 contained more files than $file1\n") if ($verbose);
676
b5e6747818a9 add rofs image compare
lorewang
parents: 655
diff changeset
   849
    if (!$keepgoing){
b5e6747818a9 add rofs image compare
lorewang
parents: 655
diff changeset
   850
	    return 0;
b5e6747818a9 add rofs image compare
lorewang
parents: 655
diff changeset
   851
	  }else{
b5e6747818a9 add rofs image compare
lorewang
parents: 655
diff changeset
   852
	  	foreach my $file (sort keys %iFileList2){
b5e6747818a9 add rofs image compare
lorewang
parents: 655
diff changeset
   853
	  		print ($log "Dig not find $file in $file1\n") if ($verbose);
b5e6747818a9 add rofs image compare
lorewang
parents: 655
diff changeset
   854
	  	}
b5e6747818a9 add rofs image compare
lorewang
parents: 655
diff changeset
   855
	  }
655
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   856
  }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   857
  
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   858
  print($log "Comparing content\n") if ($verbose);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   859
  #filelist1 and filelist2 contain all the same filenames, now compare the contents of each file
676
b5e6747818a9 add rofs image compare
lorewang
parents: 655
diff changeset
   860
  my $same = 1; # Variable to store collated result of comparison, assume an error
655
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   861
  foreach my $file (keys %iFileList1)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   862
  {
676
b5e6747818a9 add rofs image compare
lorewang
parents: 655
diff changeset
   863
  	my $tmpsame;
b5e6747818a9 add rofs image compare
lorewang
parents: 655
diff changeset
   864
  	my $type;
b5e6747818a9 add rofs image compare
lorewang
parents: 655
diff changeset
   865
    ($tmpsame, $type) = CompareFiles($tempdir1.$file,$tempdir2.$file, $verbose, $log, $keepgoing);
b5e6747818a9 add rofs image compare
lorewang
parents: 655
diff changeset
   866
    print ($log "Comparing $tempdir1.$file against $tempdir2.$file, $tmpsame, $keepgoing\n") if ($verbose);
b5e6747818a9 add rofs image compare
lorewang
parents: 655
diff changeset
   867
    if (!$keepgoing){
b5e6747818a9 add rofs image compare
lorewang
parents: 655
diff changeset
   868
	    if ($tmpsame == 0){ # do not bother comparing more files if one of the expanded files is different.
b5e6747818a9 add rofs image compare
lorewang
parents: 655
diff changeset
   869
	    	$same = 0;
b5e6747818a9 add rofs image compare
lorewang
parents: 655
diff changeset
   870
	    	last;
b5e6747818a9 add rofs image compare
lorewang
parents: 655
diff changeset
   871
	    }
b5e6747818a9 add rofs image compare
lorewang
parents: 655
diff changeset
   872
	  }else{
b5e6747818a9 add rofs image compare
lorewang
parents: 655
diff changeset
   873
	  	if ($tmpsame == 0){
b5e6747818a9 add rofs image compare
lorewang
parents: 655
diff changeset
   874
	  		print ($log "Failed\n\n") if ($verbose);
b5e6747818a9 add rofs image compare
lorewang
parents: 655
diff changeset
   875
	  		$same = 0;
b5e6747818a9 add rofs image compare
lorewang
parents: 655
diff changeset
   876
	  	}else{
b5e6747818a9 add rofs image compare
lorewang
parents: 655
diff changeset
   877
	  		print ($log "OK\n\n") if ($verbose);
b5e6747818a9 add rofs image compare
lorewang
parents: 655
diff changeset
   878
	  	}
b5e6747818a9 add rofs image compare
lorewang
parents: 655
diff changeset
   879
	  }
655
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   880
  }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   881
  
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   882
  #Cleanup the temporary directories
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   883
  rmtree([$tempdir1,$tempdir2]);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   884
  
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   885
  return $same;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   886
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   887
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   888
# Create descriptive versions of input files in response to the -d option to MD5 generation
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   889
sub dumpDescriptiveOutput ($$@)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   890
	{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   891
	my ($originalFile, $dumpDirFile, @content) = @_;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   892
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   893
	my $currentDir = cwd;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   894
	my $drive = "";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   895
	$dumpDirFile =~ s/^.://;  # Remove drive letter 
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   896
	
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   897
	$drive = $1 if ($currentDir =~ /^(\w{1}:)\//);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   898
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   899
	my $DUMPFILE = $dumpDir;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   900
	$DUMPFILE = cwd."\\$dumpDir" if ($dumpDir !~ /^(\\|\/|\w{1}:\\)/);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   901
	$DUMPFILE = $drive.$dumpDir if ($dumpDir =~ /^\\/);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   902
	$DUMPFILE .= "\\" if ($DUMPFILE !~ /(\\|\/)$/);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   903
	$DUMPFILE .= $dumpDirFile;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   904
	$DUMPFILE =~ s/\//\\/g;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   905
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   906
	# This is most likely to come about due to maintaining path structures in expanded archives e.g. .chm files
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   907
	if (length ($DUMPFILE) > 255)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   908
		{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   909
		print ("Warning: Not attempting to create \"$DUMPFILE\" as it exceeds Windows MAX_PATH limit.\n");
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   910
		return;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   911
		}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   912
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   913
	mkpath (dirname ($DUMPFILE));
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   914
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   915
	my $success = 0;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   916
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   917
	if (@content)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   918
		{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   919
		if (open DUMPFILE, "> $DUMPFILE")
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   920
			{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   921
			print DUMPFILE $_ foreach (@content);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   922
			close DUMPFILE;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   923
			$success = 1;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   924
			}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   925
		}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   926
	else
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   927
		{
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   928
		$success = 1 if (copy ($originalFile, $DUMPFILE));
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   929
		}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   930
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   931
	print ("Warning: Cannot create \"$DUMPFILE\".\n") if (!$success);
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   932
	}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   933
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   934
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   935
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   936
# Filters.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   937
#
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   938
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   939
sub Elf2E32Filter {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   940
  my $line = shift;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   941
  if ($$line =~ /Time Stamp:|E32ImageFile|Header CRC:/) { # Ignore time stamps, file name and Header CRC which uses the timestamp.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   942
    $$line = '';
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   943
  }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   944
  if ($$line =~ /imports from /) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   945
  	$$line = lc $$line;	# DLL names are not case-sensitive in the Symbian platform loader
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   946
  }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   947
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   948
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   949
sub ElfDumpFilter {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   950
  my $line = shift;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   951
  $$line  =~ s/^\tProgram header offset.*$/Program header offset/;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   952
  $$line  =~ s/^\tSection header offset.*$/Section header offset/;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   953
  $$line  =~ s/#<DLL>(\S+\.\S+)#<\\DLL>/#<DLL>\L$1\E#<\\DLL>/; # DLL names are not case-sensitive in the Symbian platform loader
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   954
  if ($$line =~ /^\.(rel\.)?debug_/) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   955
	$$line = ''; # additional debug-related information - not considered significant
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   956
	}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   957
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   958
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   959
sub NmFilter {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   960
  my $line = shift;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   961
  $$line =~ s/^.*:$//;                # ignore the filenames
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   962
  $$line =~ s/\.\.\\[^(]*\\//g;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   963
  $$line =~ s/\.\.\/[^(]*\///g;  # ignore pathnames of object files
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   964
  $$line =~ s/^BFD: (.*)$//;		# ignore the Binary File Descriptor(BFD) warning messages
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   965
  if ($$line =~ /^(.+ (_head|_))\w+_(EPOC32_\w+(_LIB|_iname))$/i) {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   966
    # dlltool uses the "-o" argument string as the basis for a "unique symbol", but
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   967
    # doesn't turn the name into a canonical form first.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   968
    # dh.o:
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   969
    #          U ________EPOC32_RELEASE_ARM4_UREL_EIKCOCTL_LIB_iname
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   970
    # 00000000 ? _head_______EPOC32_RELEASE_ARM4_UREL_EIKCOCTL_LIB
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   971
    $$line = uc "$1_..._$3\n";
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   972
  }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   973
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   974
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   975
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   976
sub MapFilter {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   977
  my $line = shift;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   978
  $$line =~ s/([d-z])\d*s_?\d+\.o/$1s999.o/;                     # ignore the names of intermediate files in .LIB
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   979
  $$line =~ s/([d-z])\d*([ht])\.o/$1$2.o/;                       # ignore the names of intermediate files in .LIB
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   980
  $$line =~ s-/-\\-go;                                           # convert / into \
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   981
  $$line =~ s/(\.\.\\|.:\\)[^(]*\\//g;                           # ignore pathnames of object files
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   982
  $$line =~ s/\.stab.*$//;                                       # ignore .stab and .stabstr lines
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   983
  $$line =~ s/0x.*size before relaxing//;                        # ignore additional comments about .stab and .stabstr
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   984
  $$line =~ s/(_head|_)\w+_(EPOC32_\w+(_LIB|_iname))/$1_,,,_$3/; # dlltool-generated unique symbols
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   985
  $$line =~ s/Timestamp is .*$//;                                # ignore timestamps in DevStudio map files
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   986
  if ($$line =~ /^ARM Linker,/) {      
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   987
	$$line = '';
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   988
  }																 # ignore the message that armlink's license will expire. (in RVCT MAP file)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   989
  if ($$line =~ /^Your license/) {								 
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   990
	$$line = '';
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   991
  }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   992
  $$line =~ s/\s__T\d{8}\s/ __Tnnnnnnnn /;                       # ignore RVCT generated internal symbols
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   993
  if ($$line =~ /0x00000000   Number         0 /) {              # ignore filenames in RVCT link maps
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   994
    $$line = '';
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   995
  }
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   996
  
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   997
  # Ignore various case differences:
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   998
  
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
   999
  ## RVCT
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1000
  
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1001
  # source filenames turning up in mangled symbols e.g.:
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1002
  #     __sti___13_BALServer_cpp                 0x000087c9   Thumb Code    52  BALServer.o(.text)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1003
  $$line =~ s/^(\s+__sti___\d+_)(\w+)(.*\(\.text\))$/$1\L$2\E$3/;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1004
  
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1005
  # object filenames e.g.:
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1006
  #     .text                                    0x0000a01c   Section      164  AssertE.o(.text)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1007
  $$line =~ s/^(\s+\.text\s+0x[0-9A-Fa-f]{8}\s+Section\s+\d+\s+)(.+)(\(\.text\))$/$1\L$2\E$3/;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1008
  
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1009
  ## WINSCW
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1010
  
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1011
  # import/static libraries processed listed in the last section e.g.:
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1012
  #1      EDLL.LIB
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1013
  #99     EDLL.LIB (not used)
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1014
  $$line =~ s/^(\d{1,2} {5,6})(\w+\.lib)( \(not used\)|)$/$1\L$2\E$3/i;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1015
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1016
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1017
sub UnzipFilter {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1018
  my $line = shift;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1019
  $$line =~ s/^Archive:.*$/Archive/;                 # ignore the archive names
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1020
  # Line format of unzip -l -v
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1021
  # Length   Method    Size  Ratio   Date   Time   CRC-32    Name, Date can be dd-mm-yy or mm/dd/yy
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1022
  $$line =~ s/ (\d+).*? ..-..-..\s+..:.. / ($1) 99-99-99 99:99 /;  # ignore (Method Size Ratio Date Time) on contained files
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1023
  $$line =~ s^ (\d+).*? ..\/..\/..\s+..:.. ^ ($1) 99-99-99 99:99 ^;  # ignore (Method Size Ratio Date Time) on contained files
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1024
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1025
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1026
sub SgmlFilter {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1027
  my $line = shift;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1028
  $$line =~ s/<!--.*-->//;  # ignore comments such as "generated by javadoc"
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1029
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1030
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1031
sub PreprocessedTextFilter {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1032
  my $line = shift;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1033
  $$line =~ s/^# \d+ ".*"( \d)?$//;  # ignore #include history
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1034
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1035
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1036
sub FilterCVSTags {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1037
  my $line = shift;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1038
  $$line =~ s#//\s+\$(?:Id|Name|Header|Date|DateTime|Change|File|Revision|Author):.*\$$##m;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1039
  # Remove tags like:
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1040
  # // $Id: //my/perforce/here $
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1041
  # which may be inserted into source code by some licensees
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1042
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1043
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1044
sub PermanentFileStoreFilter {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1045
  my $line = shift;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1046
  $$line =~ s/^Dumping .*$/Dumping (file)/;  # ignore the source file names
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1047
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1048
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1049
sub DistributionPolicyFilter {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1050
  my $line = shift;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1051
  $$line =~ s/# DistPolGen.*//;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1052
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1053
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1054
sub FilterAll {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1055
  my $line = shift;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1056
  $$line = '';
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1057
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1058
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1059
sub FilterNone {
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1060
}
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1061
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1062
1;
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1063
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1064
__END__
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1065
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1066
=head1 NAME
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1067
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1068
EvalidCompare.pm - Utilities for comparing the contents of files.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1069
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1070
=head1 DESCRIPTION
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1071
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1072
This package has been largely factored out of the C<e32toolp> tool C<evalid>. The main pieces of borrowed functionality are the ability to identify file types by examining their content, and the ability to filter irrelevant data out of files to allow comparisons to be performed. This refactoring was done in order to allow both direct and indirect comparisons of files to be supported. Direct comparisions are done by reading a pair of files (in the same way the C<evalid> does). Indirect comparisons are done by generating MD5 signatures of the files to be compared. The later method allows comparisons to be performed much more efficiently, because only one file need be present provided the signature of the other is known.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1073
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1074
=head1 INTERFACE
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1075
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1076
=head2 CompareFiles
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1077
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1078
Expects to be passed a pair of file names. May optionally also be passed a verbosity level (defaults to 0) and a file handle for logging purposes (defaults to *STDIN). Returns 1 if the files match, 0 if not. Firstly does a raw binary compare of the two files. If they match, no further processing is done and 1 is returned. If not, the type of the first file is found and the files are re-compared, this time ignoring data known to be irrelevant for the file type. The result of this compare is then returned.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1079
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1080
=head2 GenerateSignature
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1081
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1082
Expects to be passed a file name. May optionally also be passed a verbosity level (defaults to 0) and a file handle for logging purposes (defaults to *STDIN). Returns an MD5 signature of the specified file contents, having ignored irrelevant data associted with its type. This signature may subsequently be used to verify that the contents of the file has not been altered in a significant way.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1083
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1084
=head1 KNOWN BUGS
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1085
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1086
None.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1087
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1088
=head1 COPYRIGHT
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1089
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1090
 Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1091
 All rights reserved.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1092
 This component and the accompanying materials are made available
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1093
 under the terms of the License "Eclipse Public License v1.0"
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1094
 which accompanies this distribution, and is available
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1095
 at the URL "http://www.eclipse.org/legal/epl-v10.html".
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1096
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1097
 Initial Contributors:
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1098
 Nokia Corporation - initial contribution.
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1099
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1100
 Contributors:
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1101
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1102
 Description: 
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1103
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1104
=cut
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1105
3f65fd25dfd4 sync up SVN codes
kelvzhu
parents:
diff changeset
  1106
__END__