java/src/org/openantivirus/util PatternOptimizer.java,NONE,1.1 PatternFinder.java,1.15,1.16

Kurt Huwig <[email protected]> Tue, 18 May 2004 09:03:33 +0000
Newsgroups gmane.comp.security.virus.openantivirus.cvs
Message-ID <[email protected]>
Update of /cvsroot/openantivirus/java/src/org/openantivirus/util
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv8196/src/org/openantivirus/util

Modified Files:
	PatternFinder.java 
Added Files:
	PatternOptimizer.java 
Log Message:
Added '?'-wildcard pattern matching

--- NEW FILE: PatternOptimizer.java ---
/*
 * $Id: PatternOptimizer.java,v 1.1 2004/05/18 09:03:31 kurti Exp $
 * 
 * ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is OAV.
 *
 * The Initial Developer of the Original Code is Kurt Huwig <[email protected]>.
 * Portions created by the Initial Developer are Copyright (C) 2001-2003
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * ***** END LICENSE BLOCK ***** */

package org.openantivirus.util;

import java.io.*;

import org.openantivirus.engine.credo.*;

/**
 * Calculates optimal search patterns
 * 
 * Pattern-Roles:
 * @author  Kurt Huwig <[email protected]>
 * @version $Revision: 1.1 $
 */
public class PatternOptimizer {
    
    public PatternOptimizer() throws Exception {
        final InputStream is = new FileInputStream("/home/kurt/test.bin");
        final byte[] buffer = new byte[16384];
        
        final int[] count = new int[1 << 24];
        
        System.err.println("Zaehle...");
        int length;
        is.read(buffer, 0, 2);
        int triple = ((buffer[0] & 0xff) << 8) | (buffer[1] & 0xff);
        while ((length = is.read(buffer)) != -1) {
            
            for (int i = 0; i < length; i++) {
                triple &= 0xffff;
                triple <<= 8;
                triple |= buffer[i] & 0xff;
                
                count[triple]++;
            }
        }
        
        is.close();
        
        int optimized = 0, patterns = 0, hits = 0;
        
        final BufferedReader br = new BufferedReader(new FileReader(
                "/home/kurt/Download/ClamAV/viruses.db"));
        String line;
        while ((line = br.readLine()) != null) {
            patterns++;
            
            final int equalPos = line.indexOf("=");
            final String sPattern = line.substring(equalPos + 1);
            final WildcardPattern wp = new WildcardPattern(sPattern);
            
            
            int min = Integer.MAX_VALUE;
            int minPos = 0;
            int pos = 0;
            for (int j = 0; j < wp.skipList.length; j++) {
                final int skipCount = wp.skipList[j];
                
                if (j % 2 == 0 && skipCount >= 3) {
                    triple = ((wp.pattern[pos] & 0xff) << 8)
                             | (wp.pattern[pos + 1] & 0xff);
                    
                    for (int i = 2; i < skipCount; i++) {
                        triple &= 0xffff;
                        triple <<= 8;
                        triple |= wp.pattern[pos + i] & 0xff;
                        
                        final int tripleCount = count[triple];
                        if (tripleCount <= min) {
                            min = tripleCount;
                            minPos = pos + i;
                        }
                    }
                }
                pos += skipCount;
            }
            
            if (min == 0) {
                optimized++;
            } else {
                hits += min;
            }
            
            System.out.println(
                    line.substring(0, equalPos) + "[" + (minPos - 2) + "]=" +
                    sPattern);
        }
        br.close();
        
        System.err.println("Optimized: " + optimized + "/" + patterns);
        System.err.println("Hits: " + hits);
    }
    
    public static void main(String[] args) {
        try {
            new PatternOptimizer();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
}
Index: PatternFinder.java
===================================================================
RCS file: /cvsroot/openantivirus/java/src/org/openantivirus/util/PatternFinder.java,v
retrieving revision 1.15
retrieving revision 1.16
diff -u -d -r1.15 -r1.16
--- PatternFinder.java	1 May 2004 14:36:11 -0000	1.15
+++ PatternFinder.java	18 May 2004 09:03:30 -0000	1.16
@@ -34,16 +34,16 @@
     
     private int workerCount, skipLines;
     
-    private String virusNamePrefix, virusNamePostfix;
+    private String virusNamePrefix, virusNameSuffix;
     
     public PatternFinder(String sScannerCommand, String sFilename,
                          int workerCount, int skipLines,
-                         String virusNamePrefix, String virusNamePostfix) {
+                         String virusNamePrefix, String virusNameSuffix) {
         this.sFilename        = sFilename;
         this.workerCount      = workerCount;
         this.skipLines        = skipLines;
         this.virusNamePrefix  = virusNamePrefix;
-        this.virusNamePostfix = virusNamePostfix;
+        this.virusNameSuffix = virusNameSuffix;
         
         StringTokenizer st = new StringTokenizer(sScannerCommand);
         asScanArgs = new String[st.countTokens() + workerCount];
@@ -382,10 +382,10 @@
                                 sLine = br.readLine();
                             }
                             int iStartPos = sLine.indexOf(virusNamePrefix);
-                            int iEndPos   = sLine.lastIndexOf(virusNamePostfix);
+                            int iEndPos   = sLine.lastIndexOf(virusNameSuffix);
                             if (iStartPos != -1 && iEndPos != -1) {
                                 final String newVirusName = sLine.substring(
-                                        iStartPos + virusNamePostfix.length(),
+                                        iStartPos + virusNameSuffix.length(),
                                         iEndPos);
                                 if (newVirusName.equals(virusName)) {
                                     result[worker] = true;
@@ -567,7 +567,7 @@
             System.err.println(
                     "Usage: " + PatternFinder.class.getName()
                     + " <scannercommand> <# of workers> <# of lines to skip> "
-                    + "<prefix> <postfix> <filename> [<filename>...]");
+                    + "<prefix> <suffix> <filename> [<filename>...]");
             System.exit(1);
         }
         
@@ -576,10 +576,10 @@
             final int workerCount       = Integer.parseInt(asParams[1]);
             final int skipLines         = Integer.parseInt(asParams[2]);
             final String prefix         = asParams[3];
-            final String postfix        = asParams[4];
+            final String suffix         = asParams[4];
             for(int i = 5; i < asParams.length; i++) {
                 new PatternFinder(scannerCommand, asParams[i], workerCount,
-                                  skipLines, prefix, postfix).parallelFind();
+                                  skipLines, prefix, suffix).parallelFind();
             }
         } catch (Exception e) {
             e.printStackTrace();



-------------------------------------------------------
This SF.Net email is sponsored by: SourceForge.net Broadband
Sign-up now for SourceForge Broadband and get the fastest
6.0/768 connection for only $19.95/mo for the first 3 months!
http://ads.osdn.com/?ad_id=2562&alloc_id=6184&op=click