java/src/org/openantivirus/engine/credo WildcardPattern.java,NONE,1.1 StringsParser.java,1.2,1.3

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/engine/credo
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv8196/src/org/openantivirus/engine/credo

Modified Files:
	StringsParser.java 
Added Files:
	WildcardPattern.java 
Log Message:
Added '?'-wildcard pattern matching

--- NEW FILE: WildcardPattern.java ---
/*
 * $Id: WildcardPattern.java,v 1.1 2004/05/18 09:03:30 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.engine.credo;

import java.util.*;

/**
 * Contains information about patterns with '?'-wildcards
 * 
 * Pattern-Roles:
 * @author  Kurt Huwig <[email protected]>
 * @version $Revision: 1.1 $
 */
public class WildcardPattern {
    public final byte[] pattern;
    public final int[] skipList;
    
    public WildcardPattern(String hex) {
        if (hex.length() % 2 != 0) {
            throw new IllegalArgumentException("Malformed hexstring: " + hex);
        }
        
        pattern = new byte[hex.length() / 2];
        
        final Collection colSkipList = new LinkedList();
        
        int skipCount = 0;
        boolean skip = false;
        for (int i = 0; i < hex.length(); i += 2) {
            final String character = hex.substring(i, i + 2);
            if (character.equals("??")) {
                if (skip) {
                    skipCount++;
                } else {
                    skip = true;
                    colSkipList.add(new Integer(skipCount));
                    skipCount = 1;
                }
            } else {
                if (!skip) {
                    skipCount++;
                } else {
                    skip = false;
                    colSkipList.add(new Integer(skipCount));
                    skipCount = 1;
                }
                pattern[i / 2] = (byte) Integer.parseInt(character, 16);
            }
        }
        
        if (skipCount > 0) {
            colSkipList.add(new Integer(skipCount));
        }
        
        skipList = new int[colSkipList.size()];
        final Iterator it = colSkipList.iterator();
        for (int i = 0; i < skipList.length; i++) {
            skipList[i] = ((Integer) it.next()).intValue();
        }
    }
}

Index: StringsParser.java
===================================================================
RCS file: /cvsroot/openantivirus/java/src/org/openantivirus/engine/credo/StringsParser.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- StringsParser.java	1 May 2004 14:36:11 -0000	1.2
+++ StringsParser.java	18 May 2004 09:03:30 -0000	1.3
@@ -49,49 +49,39 @@
     throws IOException {
 
         final BufferedReader br = new BufferedReader(patternReader);
-        try {
-            String sLine;
-            while ((sLine = br.readLine()) != null) {
-                int iPos = sLine.indexOf('=');
-                if (iPos == -1) {
-                    System.err.println("Malformed pattern line: " + sLine);
-                    continue;
-                }
-                
-                String sVirusName = sLine.substring(0, iPos);
-                String sPattern   = sLine.substring(iPos + 1);
-                
-                try {
-                    stringFinder.addString(hexToString(sPattern),
-                            new StringVirusFoundListener(sVirusName));
-                } catch (Exception e) {
-                    System.err.println(sLine);
-                    e.printStackTrace();
-                }
+        String sLine;
+        while ((sLine = br.readLine()) != null) {
+            int iPos = sLine.indexOf('=');
+            if (iPos == -1) {
+                System.err.println("Malformed pattern line: " + sLine);
+                continue;
+            }
+            
+            String sVirusName = sLine.substring(0, iPos);
+            String sPattern   = sLine.substring(iPos + 1);
+            
+            try {
+                final int offsetStart = sLine.lastIndexOf('[');
+                final int[] skipList = new int[1];
+                skipList[0] = sPattern.length() / 2;
+                final WildcardPattern wp = new WildcardPattern(sPattern);
+                stringFinder.addString(
+                        wp.pattern,
+                        wp.skipList,
+                        offsetStart != -1
+                            ? Integer.parseInt(sLine.substring(
+                                    offsetStart + 1, iPos - 1))
+                            : 0,
+                        new StringVirusFoundListener(sVirusName));
+            } catch (Exception e) {
+                System.err.println(sLine);
+                e.printStackTrace();
             }
-        } finally {
-            br.close();
-        }
-    }
-    
-    protected byte[] hexToString(String hex) {
-        if (hex.length() % 2 != 0) {
-            System.err.println("Malformed hexstring: " + hex);
-            return new byte[0];
-        }
-        
-        final byte[] result = new byte[hex.length() / 2];
-        for (int i = 0; i < hex.length(); i += 2) {
-            result[i / 2] = (byte) Integer.parseInt(hex.substring(i, i + 2),
-                    16);
         }
-        
-        return result;
+        // br may not be closed!
     }
     
-
-    
-    private class StringVirusFoundListener implements PositionFoundListener {
+    public static class StringVirusFoundListener implements PositionFoundListener {
         private String virusName, pattern;
         
         public StringVirusFoundListener(String virusName) {



-------------------------------------------------------
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