java/src/org/openantivirus/util PatternOptimizer.java,1.4,1.5

Kurt Huwig <[email protected]> Sat, 22 May 2004 12:22:29 +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-serv9747/src/org/openantivirus/util

Modified Files:
	PatternOptimizer.java 
Log Message:
Bugfix: some patterns were not detected due to bad optimizing
Speed improvements on multipart patterns

Index: PatternOptimizer.java
===================================================================
RCS file: /cvsroot/openantivirus/java/src/org/openantivirus/util/PatternOptimizer.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- PatternOptimizer.java	20 May 2004 13:34:32 -0000	1.4
+++ PatternOptimizer.java	22 May 2004 12:22:27 -0000	1.5
@@ -40,8 +40,9 @@
  */
 public class PatternOptimizer {
     
-    public PatternOptimizer() throws Exception {
-        final InputStream is = new FileInputStream("/home/kurt/test.bin");
+    public PatternOptimizer(String testFile, String patternFile)
+    throws Exception {
+        final InputStream is = new FileInputStream(testFile);
         final byte[] buffer = new byte[16384];
         
         final int[] count = new int[1 << 16];
@@ -53,7 +54,7 @@
         while ((length = is.read(buffer)) != -1) {
             
             for (int i = 0; i < length; i++) {
-                match = ((match << 8) | buffer[i]) & 0xffff;
+                match = ((match << 8) | (buffer[i] & 0xff)) & 0xffff;
                 
                 count[match]++;
             }
@@ -61,10 +62,11 @@
         
         is.close();
         
+        System.err.println("Optimizing...");
         int optimized = 0, patterns = 0, hits = 0;
         
-        final BufferedReader br = new BufferedReader(new FileReader(
-                "/home/kurt/Download/ClamAV/viruses.db2"));
+        final BufferedReader br =
+            new BufferedReader(new FileReader(patternFile));
         String line;
         while ((line = br.readLine()) != null) {
             patterns++;
@@ -99,7 +101,8 @@
                         match = wp.pattern[pos] & 0xff;
                         
                         for (int i = 1; i < skipCount; i++) {
-                            match = ((match << 8) | wp.pattern[i]) & 0xffff;
+                            match = ((match << 8)
+                                    | (wp.pattern[i] & 0xff)) & 0xffff;
                             
                             final int tripleCount = count[match];
                             if (tripleCount <= min) {
@@ -155,8 +158,13 @@
     }
     
     public static void main(String[] args) {
+        if (args.length != 2) {
+            System.out.println("Usage: " + PatternOptimizer.class.getName()
+                               + " <test-file> <pattern-db>");
+            System.exit(1);
+        }
         try {
-            new PatternOptimizer();
+            new PatternOptimizer(args[0], args[1]);
         } catch (Exception e) {
             e.printStackTrace();
         }



-------------------------------------------------------
This SF.Net email is sponsored by: Oracle 10g
Get certified on the hottest thing ever to hit the market... Oracle 10g. 
Take an Oracle 10g class now, and we'll give you the exam FREE.
http://ads.osdn.com/?ad_id=3149&alloc_id=8166&op=click