sysperfana/perfinvestigator/com.nokia.carbide.cpp.pi.instr/src/com/nokia/carbide/cpp/pi/instr/BinaryReader.java
changeset 2 b9ab3b238396
child 5 844b047e260d
equal deleted inserted replaced
1:1050670c6980 2:b9ab3b238396
       
     1 /*
       
     2  * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3  * All rights reserved.
       
     4  * This component and the accompanying materials are made available
       
     5  * under the terms of the License "Eclipse Public License v1.0"
       
     6  * which accompanies this distribution, and is available
       
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8  *
       
     9  * Initial Contributors:
       
    10  * Nokia Corporation - initial contribution.
       
    11  *
       
    12  * Contributors:
       
    13  *
       
    14  * Description: 
       
    15  *
       
    16  */
       
    17 
       
    18 package com.nokia.carbide.cpp.pi.instr;
       
    19 
       
    20 import java.io.BufferedReader;
       
    21 import java.io.File;
       
    22 import java.io.FileReader;
       
    23 import java.io.IOException;
       
    24 import java.util.Enumeration;
       
    25 import java.util.Hashtable;
       
    26 import java.util.Vector;
       
    27 
       
    28 import com.nokia.carbide.cpp.internal.pi.model.Binary;
       
    29 import com.nokia.carbide.cpp.internal.pi.model.Function;
       
    30 import com.nokia.carbide.cpp.pi.importer.SampleImporter;
       
    31 import com.nokia.carbide.cpp.pi.importer.SampleImporter.PkgObyFile;
       
    32 import com.nokia.carbide.cpp.pi.util.GeneralMessages;
       
    33 import com.nokia.carbide.cpp.pi.util.GuessAndFixPath;
       
    34 
       
    35 
       
    36 public class BinaryReader
       
    37 {
       
    38   private boolean debug = false;
       
    39   private Vector<File> files;
       
    40   private Hashtable<String, ProcessedBinary> processedFiles;
       
    41 
       
    42   public BinaryReader() throws Exception
       
    43   {
       
    44     files = new Vector<File>();
       
    45     processedFiles = new Hashtable<String, ProcessedBinary>();
       
    46 
       
    47     for (PkgObyFile currentFile : SampleImporter.getInstance().getPkgObyFilesList())
       
    48     {    	
       
    49     	{
       
    50     		String epocRoot = null;
       
    51     	    File epoc32;
       
    52     		
       
    53     	    if (currentFile.epocRoot.endsWith("\\")) //$NON-NLS-1$
       
    54     	    {
       
    55     	    	epocRoot = currentFile.epocRoot.substring(0, currentFile.epocRoot.length() - 1);
       
    56     	    }
       
    57     	    else
       
    58     	    {
       
    59     	    	epocRoot = currentFile.epocRoot;
       
    60     	    }
       
    61     	    epoc32 = new File(epocRoot + "\\epoc32"); //$NON-NLS-1$
       
    62 
       
    63     	    if (!epoc32.exists() || !epoc32.isDirectory()) {
       
    64     	    	GeneralMessages.showErrorMessage(Messages.getString("BinaryReader.cannotFindEpoc32FromRoot")+epoc32.toString()); //$NON-NLS-1$
       
    65     	    	throw new Exception (Messages.getString("BinaryReader.cannotFindEpoc32FromRoot")+epoc32.toString()); //$NON-NLS-1$
       
    66     	    }
       
    67     	}
       
    68     	
       
    69     	if (currentFile.fileAbsolutePath.toLowerCase().endsWith(".oby") || currentFile.fileAbsolutePath.toLowerCase().endsWith(".iby")) //$NON-NLS-1$ //$NON-NLS-2$
       
    70     		addFilesInOby(currentFile.fileAbsolutePath, currentFile.epocRoot);
       
    71     	else if (currentFile.fileAbsolutePath.toLowerCase().endsWith(".pkg")) //$NON-NLS-1$
       
    72     		addFilesInPkg(currentFile.fileAbsolutePath, currentFile.epocRoot);
       
    73     	else
       
    74     		this.addSingleFile(currentFile.fileAbsolutePath);
       
    75     }
       
    76     refreshFileTable();
       
    77   }
       
    78   public BinaryReaderResult findSequence(IttSample sample)
       
    79   {
       
    80     return findSequence(sample,(ProcessedBinary)null);
       
    81   }
       
    82 
       
    83   public BinaryReaderResult findSequence(IttSample sample, Binary b)
       
    84   {
       
    85     if (this.processedFiles.containsKey(b.binaryName))
       
    86     {
       
    87       ProcessedBinary pf = (ProcessedBinary)this.processedFiles.get(b.binaryName);
       
    88       return this.findSequence(sample,pf);
       
    89     }
       
    90     return null;
       
    91   }
       
    92   
       
    93   public ProcessedBinary getProcessedBinaryForName(String name)
       
    94   {
       
    95   	ProcessedBinary pb = (ProcessedBinary)this.processedFiles.get(name);
       
    96   	
       
    97   	if (pb != null)
       
    98   	{
       
    99   		return pb;
       
   100   	}
       
   101   	return null;
       
   102   }
       
   103   
       
   104   public boolean checkSampleInBinary(IttSample sample,Binary binary,int differencesAllowed)
       
   105   {
       
   106   	ProcessedBinary pb = this.getProcessedBinaryForName(binary.binaryName);
       
   107   	if (pb == null)
       
   108   	{
       
   109   		System.out.println(Messages.getString("BinaryReader.cannotFindBinary")+binary.binaryName); //$NON-NLS-1$
       
   110 		return false;
       
   111   	}
       
   112   	
       
   113   	if (binary.startAddress <= sample.programCounter &&
       
   114   	   binary.startAddress+binary.length >= sample.programCounter)
       
   115   	{
       
   116   		// the program counter value indicates that the sample
       
   117   		// is within the binary
       
   118   		long[] reversedInstructions = sample.reversedInstructions();
       
   119   		int matches = 0;
       
   120   		for (int i=0;i<reversedInstructions.length;i++)
       
   121   		{
       
   122   			
       
   123   			long instruction = reversedInstructions[i];
       
   124   			long offset = (long)(sample.programCounter-binary.startAddress);
       
   125 
       
   126   			if (offset < binary.length)
       
   127   			{
       
   128   				// add the amount of bytes that match in the next 4 bytes
       
   129   				matches += this.testNextFourBytes((int)offset+(i*4),instruction,pb);
       
   130   			}
       
   131   		}	
       
   132   		
       
   133   		if (matches >= (reversedInstructions.length*4 - differencesAllowed))
       
   134   		{
       
   135   			// binary matches with the sample
       
   136   			// within the binary
       
   137   			return true;
       
   138   		}
       
   139   	}
       
   140   	
       
   141   	// the sample is not within the binary memory area
       
   142   	return false;
       
   143   }
       
   144 
       
   145   public BinaryReaderResult findSequence(IttSample sample,ProcessedBinary processedFile)
       
   146   {
       
   147     long[] sequence = sample.reversedInstructions();
       
   148 
       
   149     Vector possibleBinaries  = new Vector();
       
   150     Vector checksumValues = new Vector();
       
   151 
       
   152     if (sequence.length != 0)
       
   153     {
       
   154       int first = (int) ((sequence[0] & (long)0xff000000) >> 24);
       
   155       int second = (int) ((sequence[0] & (long)0x00ff0000) >> 16);
       
   156       int third = (int) ((sequence[0] & (long)0x0000ff00) >> 8);
       
   157       int fourth = (int) ((sequence[0] & (long)0x000000ff));
       
   158 
       
   159       // this is for searching only one binary
       
   160       Vector tempVec = null;
       
   161       Enumeration enumer = null;
       
   162 
       
   163       if (processedFile != null)
       
   164       {
       
   165         tempVec = new Vector();
       
   166         tempVec.add(processedFile);
       
   167         enumer = tempVec.elements();
       
   168       }
       
   169       else
       
   170       {
       
   171         enumer = this.processedFiles.elements();
       
   172       }
       
   173 
       
   174       while (enumer.hasMoreElements())
       
   175       {
       
   176         ProcessedBinary pf = (ProcessedBinary) enumer.nextElement();
       
   177 
       
   178         int[] indicesToCheck = pf.getIndicesForSequence(first);
       
   179 
       
   180         if (indicesToCheck != null)
       
   181         {
       
   182           for ( int k = 0;k<indicesToCheck.length;k++)
       
   183           {
       
   184             int i = indicesToCheck[k];
       
   185 
       
   186             int match = 0;
       
   187             
       
   188             if (i < pf.length - 5)
       
   189             {
       
   190               if (pf.data[i] == (byte) first) match++;
       
   191               
       
   192               if (pf.data[i + 1] == (byte) second) match++;
       
   193               
       
   194               if (pf.data[i + 2] == (byte) third) match++;
       
   195               
       
   196               if (pf.data[i + 3] == (byte) fourth) match++;
       
   197                     
       
   198               // if at least three of the previous four bytes did match
       
   199               if (match >= 3)
       
   200                     {
       
   201                       for (int s = 1; s < sequence.length; s++)
       
   202                       {
       
   203 
       
   204                       	// add the match value with one if three of the four bytes
       
   205                       	// in the next 4 bytes match
       
   206                       	match += testNextFourBytes(i + s * 4, sequence[s], pf);
       
   207                       }
       
   208                       
       
   209                       if (match >= (sequence.length*4)-2)
       
   210                       {
       
   211                         // check the checksum
       
   212                         // go back from the current position another 16 bytes
       
   213                         // and calculate the xor checksum for 11 instructions
       
   214                         long checksum = pf.calculateXorChecksum(i+12,8);
       
   215 
       
   216                         // this value should match with the last value in the
       
   217                         // search pattern
       
   218                         if ( (checksum & 0xffffffff) == (sample.checksum & 0xffffffff) )
       
   219                         {
       
   220 
       
   221                           // add this location to possible binaries
       
   222                           Binary b = new Binary(pf.binaryName);
       
   223                           b.length = pf.length;
       
   224                           b.offsetToCodeStart = pf.offsetToCodeStart;
       
   225                           b.type = pf.type;
       
   226 
       
   227                           // binary start address is the this address - this offset
       
   228                           b.startAddress = sample.programCounter-i;
       
   229                           possibleBinaries.add(b);
       
   230 
       
   231                           //System.out.println (	"CHECKSUM MATCH at "+pf.binaryName+"@0x"+Long.toHexString(b.startAddress-b.offsetToCodeStart)+
       
   232                           //						" pos:"+i+"!! Checksum:"+checksum+" "+pf.file.getAbsolutePath());
       
   233 
       
   234                           
       
   235                           // this is a checksum match
       
   236                           checksumValues.add(Boolean.valueOf(true));
       
   237                         }
       
   238                         else
       
   239                         {
       
   240 
       
   241                           // add this location to possible binaries
       
   242 
       
   243                           Binary b = new Binary(pf.binaryName);
       
   244                           b.length = pf.length;
       
   245                           b.offsetToCodeStart = pf.offsetToCodeStart;
       
   246                           b.type = pf.type;
       
   247 
       
   248                           // binary start address is this address - this offset
       
   249                           b.startAddress = sample.programCounter-i;
       
   250                           possibleBinaries.add(b);
       
   251 
       
   252                           //System.out.println(	"MATCH at "+pf.binaryName+"@0x"+Long.toHexString(b.startAddress-b.offsetToCodeStart)+
       
   253                           //						" pos:"+i+"!! "+pf.file.getAbsolutePath());
       
   254                           
       
   255                           // this one is not a checksum match
       
   256                           checksumValues.add(Boolean.valueOf(false));
       
   257                         }
       
   258                       }
       
   259                     }
       
   260             }
       
   261           }
       
   262         }
       
   263       }
       
   264     }
       
   265     BinaryReaderResult brr = new BinaryReaderResult();
       
   266 
       
   267     brr.checksumValues = (Boolean[])checksumValues.toArray(new Boolean[checksumValues.size()]);
       
   268     brr.possibleBinaries = (Binary[])possibleBinaries.toArray(new Binary[possibleBinaries.size()]);
       
   269 
       
   270     return brr;
       
   271     //System.out.println("Finished!");
       
   272   }
       
   273 
       
   274 
       
   275   public void printBinaryFromOffset(ProcessedBinary pf, int offset, int length)
       
   276   {
       
   277     if (pf.length < offset+length) return;
       
   278     int c = 0;
       
   279 
       
   280     for (int i=offset;i<offset+length;i++)
       
   281     {
       
   282       String hex = Integer.toHexString((int)pf.data[i] & 0xff);
       
   283       if (hex.length() == 1) hex = "0"+hex; //$NON-NLS-1$
       
   284 
       
   285       if (c%4 == 0) System.out.print(" "); //$NON-NLS-1$
       
   286       if (c%40 == 0) System.out.print("\n"); //$NON-NLS-1$
       
   287 
       
   288       System.out.print(hex);
       
   289       c++;
       
   290     }
       
   291   }
       
   292   
       
   293   private int testNextFourBytes(int offset,long data, ProcessedBinary pf)
       
   294   {
       
   295       if (data == -1) return 4;
       
   296 
       
   297       int first = (int) ((data & (long)0xff000000) >> 24);
       
   298       int second = (int) ((data & (long)0x00ff0000) >> 16);
       
   299       int third = (int) ((data & (long)0x0000ff00) >> 8);
       
   300       int fourth = (int) ((data & (long)0x000000ff));
       
   301 
       
   302       int matches = 0;
       
   303       
       
   304       if (pf.length > offset+4)
       
   305       {
       
   306         if (pf.data[offset] == (byte)first) matches++; 
       
   307         
       
   308         if (pf.data[offset+1] == (byte)second) matches++;
       
   309         
       
   310         if (pf.data[offset+2] == (byte)third) matches++;
       
   311         
       
   312         if (pf.data[offset+3] == (byte)fourth) matches++;
       
   313       }
       
   314       
       
   315       return matches;
       
   316   }
       
   317 
       
   318   public void printBinary(String fileName)
       
   319   {
       
   320     this.printBinary(fileName,0,-1);
       
   321   }
       
   322 
       
   323   public void printBinary(String fileName,int startOffset,int length)
       
   324   {
       
   325   	System.out.println(binaryToString(fileName,startOffset,length));
       
   326   }
       
   327 
       
   328   public String binaryToString(String fileName,int startOffset,int length)
       
   329   {
       
   330   	String resultString = ""; //$NON-NLS-1$
       
   331   	
       
   332     int counter = 1;
       
   333     if (this.processedFiles.containsKey(fileName))
       
   334     {
       
   335       ProcessedBinary pf = (ProcessedBinary)this.processedFiles.get(fileName);
       
   336       if (length == -1)
       
   337         {
       
   338           startOffset = 0;
       
   339           length = pf.length;
       
   340         }
       
   341 
       
   342       for (int i=startOffset;i<startOffset+length;i++)
       
   343       {
       
   344         String hex = Integer.toHexString((int)pf.data[i] & 0xff);
       
   345         if (hex.length() == 1) hex = "0"+hex; //$NON-NLS-1$
       
   346 
       
   347         resultString+=hex;
       
   348 
       
   349         if (counter%4 == 0) resultString+=" "; //$NON-NLS-1$
       
   350         if (counter%40 == 0) resultString+="\n"; //$NON-NLS-1$
       
   351 
       
   352         counter++;
       
   353       }
       
   354     }
       
   355     
       
   356     return resultString;
       
   357   }
       
   358 
       
   359   
       
   360   public String getFunctionName(String binaryName, long offset)
       
   361   {
       
   362   	ProcessedBinary pb = (ProcessedBinary)this.processedFiles.get(binaryName);
       
   363   	if (pb != null)
       
   364   	{
       
   365   		return pb.getFunctionNameForOffset(offset);
       
   366   	}
       
   367   	else
       
   368   	{
       
   369   		return Messages.getString("BinaryReader.binaryNotFound1")+binaryName+Messages.getString("BinaryReader.binaryNotFound2"); //$NON-NLS-1$ //$NON-NLS-2$
       
   370   	}
       
   371   }
       
   372   
       
   373   public Function getFunction(String binaryName,long offset)
       
   374   {
       
   375   	ProcessedBinary pb = (ProcessedBinary)this.processedFiles.get(binaryName);
       
   376   	if (pb != null)
       
   377   	{
       
   378   		return pb.getFunctionForOffset(offset);
       
   379    	}
       
   380   	else
       
   381   	{
       
   382   		return null;
       
   383   	}
       
   384   }
       
   385   
       
   386   public long getFunctionStartOffsetFromBinaryStart(String binaryName,String functionName)
       
   387   {
       
   388   	ProcessedBinary pb = (ProcessedBinary)this.processedFiles.get(binaryName);
       
   389   	if (pb != null)
       
   390   	{
       
   391   		return pb.getOffsetFromBinaryStartForFunction(functionName);
       
   392   	}
       
   393   	else
       
   394   	{
       
   395   		return -1;
       
   396   	}
       
   397   }
       
   398 
       
   399   public void refreshFileTable()
       
   400   {
       
   401     long length = 0;
       
   402     int addedFiles = 0;
       
   403 
       
   404     if (debug) System.out.println(this.files.size()+Messages.getString("BinaryReader.filesFound")); //$NON-NLS-1$
       
   405     if (this.files.size() > 0)
       
   406     {
       
   407       Enumeration enumer = files.elements();
       
   408       while (enumer.hasMoreElements())
       
   409       {
       
   410         File f = (File) enumer.nextElement();
       
   411         String name = f.getAbsolutePath();
       
   412         name = name.substring(name.indexOf(File.separator),name.length());
       
   413 
       
   414         if (!(this.processedFiles.containsKey(name) &&
       
   415               (((ProcessedBinary)this.processedFiles.get(name)).length == f.length())))
       
   416         {
       
   417           try
       
   418           {
       
   419             ProcessedBinary pf = processFile(f);
       
   420             //System.out.println(f.getName());
       
   421             this.processedFiles.put(pf.binaryName, pf);
       
   422             length += pf.length;
       
   423             addedFiles++;
       
   424           }
       
   425           catch (Exception e)
       
   426           {
       
   427             e.printStackTrace();
       
   428           }
       
   429         }
       
   430         else
       
   431         {
       
   432           System.out.println(Messages.getString("BinaryReader.fileAlreadyPresent1")+f.getName()+Messages.getString("BinaryReader.fileAlreadyPresent2")); //$NON-NLS-1$ //$NON-NLS-2$
       
   433         }
       
   434       }
       
   435     }
       
   436 
       
   437     this.files.clear();
       
   438 
       
   439     if (debug) System.out.println(Messages.getString("BinaryReader.bytesProcessed1")+length+Messages.getString("BinaryReader.bytesProcessed2")+addedFiles+Messages.getString("BinaryReader.bytesProcessed3")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
       
   440   }
       
   441 
       
   442   private ProcessedBinary processFile(File f) throws Exception
       
   443   {
       
   444     return new ProcessedBinary(f);
       
   445   }
       
   446 
       
   447   public void addFilesInOby(String obyFile, String epocroot) throws Exception
       
   448   {
       
   449     File file = new File(obyFile);
       
   450     if (!file.exists())
       
   451     {
       
   452       throw new Exception(Messages.getString("BinaryReader.cannotFindObyFile")); //$NON-NLS-1$
       
   453     }
       
   454 
       
   455     BufferedReader br = new BufferedReader(new FileReader(file));
       
   456 
       
   457     String line = br.readLine();
       
   458     
       
   459     while(line != null)
       
   460     {
       
   461         line = line.trim(); 
       
   462         
       
   463         if (line.endsWith("\""))  //$NON-NLS-1$
       
   464         {        
       
   465         	line = line.replaceAll("\"",""); //$NON-NLS-1$ //$NON-NLS-2$
       
   466         	line.trim();
       
   467         }
       
   468     	
       
   469         int start = line.indexOf("="); //$NON-NLS-1$
       
   470 //        start = line.indexOf("\\", start);
       
   471         start = line.indexOf(System.getProperty(Messages.getString("BinaryReader.fileSeparatorProperty")), start); //$NON-NLS-1$
       
   472 //        System.getProperty("file.separator")
       
   473 
       
   474         int space = line.indexOf(" ",start+2); //$NON-NLS-1$
       
   475         int tab = line.indexOf("\t",start+2); //$NON-NLS-1$
       
   476         int end = 0;
       
   477 
       
   478         if (space < 0 && tab < 0)
       
   479           end = line.length();
       
   480         else if (space > 0 && tab > 0 && space > tab)
       
   481           end = tab;
       
   482         else if (space > 0 && tab > 0 && tab > space)
       
   483           end = space;
       
   484         else if (space < 0 && tab > 0)
       
   485           end = tab;
       
   486         else if (space > 0)
       
   487           end = space;
       
   488         else
       
   489           start = -1;
       
   490 
       
   491         if (start >= 0)
       
   492         {
       
   493           String lowCase = line.toLowerCase();
       
   494 		  
       
   495           if (checkFileExtension(lowCase) == false) start = -1;
       
   496 		                    	
       
   497           if (line.indexOf("rem") >= 0) //$NON-NLS-1$
       
   498           {
       
   499             if (line.indexOf("rem") <= start) //$NON-NLS-1$
       
   500               start = -1;
       
   501           }
       
   502           else if (line.indexOf("romname") >= 0) //$NON-NLS-1$
       
   503           {
       
   504             if (line.indexOf("romname") <= start) //$NON-NLS-1$
       
   505               start = -1;
       
   506           }
       
   507         }
       
   508 
       
   509         if (start >= 0 && end > start)
       
   510         {
       
   511           String fileNameLine = line.substring(start, end);
       
   512           if (fileNameLine.endsWith("\"")) //$NON-NLS-1$
       
   513             fileNameLine = fileNameLine.substring(0, fileNameLine.length() - 1);
       
   514 
       
   515           addFile(fileNameLine, epocroot);
       
   516           //System.out.println("ADDED "+fileNameLine);
       
   517         }
       
   518         else
       
   519         {
       
   520         	//if (line.indexOf(".rsc") == -1 && !line.startsWith("rem") && line.length() > 3)
       
   521         	//System.out.println("SKIPPED "+line);
       
   522         }
       
   523 
       
   524         line = br.readLine();
       
   525         //System.out.println("READ "+line);
       
   526       }
       
   527     br.close();
       
   528 
       
   529   }
       
   530   
       
   531   public static boolean checkFileExtension(String lowCase)
       
   532   {
       
   533       if (lowCase.endsWith(".rsc")) return false; //$NON-NLS-1$
       
   534       else if (lowCase.endsWith(".aif")) return false; //$NON-NLS-1$
       
   535       else if (lowCase.endsWith(".dll55l")) return false; //$NON-NLS-1$
       
   536       else if (lowCase.endsWith(".dof")) return false; //$NON-NLS-1$
       
   537       else if (lowCase.endsWith(".mid")) return false; //$NON-NLS-1$
       
   538       else if (lowCase.endsWith(".rng")) return false; //$NON-NLS-1$
       
   539       else if (lowCase.endsWith(".ini")) return false; //$NON-NLS-1$
       
   540       else if (lowCase.endsWith(".awb")) return false; //$NON-NLS-1$
       
   541       else if (lowCase.endsWith(".dat")) return false; //$NON-NLS-1$
       
   542       else if (lowCase.endsWith(".txt")) return false; //$NON-NLS-1$
       
   543       else if (lowCase.endsWith(".mbm_rom")) return false; //$NON-NLS-1$
       
   544       else if (lowCase.endsWith(".xml")) return false; //$NON-NLS-1$
       
   545 	  else if (lowCase.endsWith(".dtd")) return false; //$NON-NLS-1$
       
   546       else if (lowCase.endsWith(".snm")) return false; //$NON-NLS-1$
       
   547       else if (lowCase.endsWith(".smil")) return false; //$NON-NLS-1$
       
   548       else if (lowCase.endsWith(".sis")) return false; //$NON-NLS-1$
       
   549 	  else if (lowCase.endsWith(".skn")) return false; //$NON-NLS-1$
       
   550 	  else if (lowCase.endsWith(".lnk")) return false; //$NON-NLS-1$
       
   551 	  else if (lowCase.endsWith(".gdr")) return false; //$NON-NLS-1$
       
   552 	  else if (lowCase.endsWith(".esk")) return false;	   //$NON-NLS-1$
       
   553       else if (lowCase.endsWith(".ota")) return false;	  //$NON-NLS-1$
       
   554 	  else if (lowCase.endsWith(".cfg")) return false;	  //$NON-NLS-1$
       
   555 	  else if (lowCase.endsWith(".wav")) return false; //$NON-NLS-1$
       
   556 	  else if (lowCase.endsWith(".mp3")) return false; //$NON-NLS-1$
       
   557 	  else if (lowCase.endsWith(".amr")) return false; //$NON-NLS-1$
       
   558 	  else if (lowCase.endsWith(".pcm")) return false; //$NON-NLS-1$
       
   559 	  else if (lowCase.endsWith(".mbm")) return false; //$NON-NLS-1$
       
   560 	  else if (lowCase.endsWith(".bmp")) return false; //$NON-NLS-1$
       
   561 	  else if (lowCase.endsWith(".aac")) return false; //$NON-NLS-1$
       
   562 	  else if (lowCase.endsWith(".png")) return false; //$NON-NLS-1$
       
   563 	  else if (lowCase.endsWith(".hlp")) return false; //$NON-NLS-1$
       
   564 	  else if (lowCase.endsWith(".jpg")) return false; //$NON-NLS-1$
       
   565 	  else if (lowCase.endsWith(".dic")) return false; //$NON-NLS-1$
       
   566 	  else if (lowCase.endsWith(".mask")) return false; //$NON-NLS-1$
       
   567 	  else if (lowCase.endsWith(".db")) return false; //$NON-NLS-1$
       
   568 	  else if (lowCase.endsWith(".mxmf")) return false; //$NON-NLS-1$
       
   569 	  else if (lowCase.endsWith(".gif")) return false;          //$NON-NLS-1$
       
   570 
       
   571 	  else return true;
       
   572   }
       
   573   
       
   574   public void addFilesInPkg(String pkgName, String epocroot) throws IOException
       
   575   {
       
   576 	  File pkgFile = new File(pkgName);
       
   577 	  if (pkgFile.exists() && !pkgFile.isDirectory())
       
   578 	  {
       
   579 		  try
       
   580 		  	{
       
   581 			  BufferedReader br = new BufferedReader(new FileReader(pkgFile));
       
   582 			  
       
   583 			  while(true)
       
   584 			  {
       
   585 				  String line = br.readLine();
       
   586 				  //System.out.println(line);
       
   587 				  int first = line.indexOf("\""); //$NON-NLS-1$
       
   588 				  int second = -1;
       
   589 				  int third = -1;
       
   590 				  int fourth = -1;
       
   591 				  
       
   592 				  if (first != -1)
       
   593 					  second = line.indexOf("\"",first+1); //$NON-NLS-1$
       
   594 				  
       
   595 				  if (second != -1)
       
   596 					  third = line.indexOf("\"!:",second+1); //$NON-NLS-1$
       
   597 				  
       
   598 				  if (third != -1)
       
   599 					  fourth = line.indexOf("\"",third+1); //$NON-NLS-1$
       
   600 				  
       
   601 				  //System.out.println(first+" "+second+" "+third+" "+fourth);
       
   602 				  if (fourth != -1)
       
   603 				  {
       
   604 					  String localFile = line.substring(first+1,second);
       
   605 //					  String remoteFile = line.substring(third+3,fourth);
       
   606 					  
       
   607 					  // .PKG is referring everything as root relative,
       
   608 					  // sometime there is reference to $(EPOCROOT) too
       
   609 					  // let's try to guess what it was. I hate windows drive letter
       
   610 					  if (localFile.charAt(0) == '\\') {
       
   611 						  localFile = GuessAndFixPath.fixPath(localFile, epocroot, pkgName);
       
   612 					  }
       
   613 					  
       
   614 					  String lowerLocal = localFile.toLowerCase();
       
   615 					  if (checkFileExtension(lowerLocal))
       
   616 					  {
       
   617 						  System.out.println(Messages.getString("BinaryReader.addedPkgFile")+lowerLocal); //$NON-NLS-1$
       
   618 						  this.addFile(lowerLocal, epocroot);
       
   619 					  }
       
   620 				  }
       
   621 			  }
       
   622 		  	}
       
   623 		  catch (Exception e)
       
   624 		  {
       
   625 			  
       
   626 		  }
       
   627 	  }
       
   628   }
       
   629   
       
   630   public void addSingleFile(String fileName)
       
   631   {
       
   632   	File file = new File(fileName);
       
   633   	this.addFile(file); 
       
   634   	this.refreshFileTable();
       
   635   }
       
   636   
       
   637   private void addFile(String fileName, String epocroot)
       
   638   {
       
   639 	  File f = new File(epocroot + fileName);
       
   640     if (!f.exists()) System.out.println(Messages.getString("BinaryReader.epocRootFileNonexistent1")+fileName+Messages.getString("BinaryReader.epocRootFileNonexistent2")); //$NON-NLS-1$ //$NON-NLS-2$
       
   641     else
       
   642       {
       
   643         addFile(f);
       
   644         //System.out.println("Added file "+fileName+" length "+f.length());
       
   645       }
       
   646 
       
   647   }
       
   648 
       
   649   private void addFile(File file)
       
   650   {
       
   651     if (file.exists() && !file.isDirectory())
       
   652     {
       
   653       //System.out.println("ADDED"+file.getName());
       
   654       this.files.add(file);
       
   655     }
       
   656     else
       
   657     {
       
   658       System.out.println(Messages.getString("BinaryReader.fileNonexistent1")+file.getAbsolutePath()+Messages.getString("BinaryReader.fileNonexistent2")); //$NON-NLS-1$ //$NON-NLS-2$
       
   659     }
       
   660   }
       
   661 
       
   662   public static void main(String a[]) throws Exception
       
   663   {
       
   664 	  System.out.println("Starting"); //$NON-NLS-1$
       
   665 	  try
       
   666 	  {
       
   667 	    	GeneralMessages.showErrorMessage("BinaryReader Internal Error"); //$NON-NLS-1$
       
   668 	    	throw new Exception ("BinaryReader Internal Error"); //$NON-NLS-1$
       
   669 	  }
       
   670 	  catch (Exception e)
       
   671 	  {
       
   672 		  e.printStackTrace();
       
   673 	  }
       
   674   }
       
   675   
       
   676 }