Update of /cvsroot/openantivirus/java/src/org/openantivirus/engine
In directory sc8-pr-cvs1:/tmp/cvs-serv4777/java/src/org/openantivirus/engine
Added Files:
ScanConfiguration.java DefaultScanConfiguration.java
WriteableScanConfiguration.java ScanEngine.java
Log Message:
Rewrite of the engine to use a virtual file system
Move to 'engine' subdirectory
Added bzip2 and tar decompressors
Switch from GPL to MPL
--- NEW FILE: ScanConfiguration.java ---
/*
* $Id: ScanConfiguration.java,v 1.1 2003/12/14 11:08:26 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;
/**
* Holds configuration data for scanning
*
* Pattern-Roles:
* @author Kurt Huwig <[email protected]>
* @version $Revision: 1.1 $
*/
public interface ScanConfiguration {
String HALT_ON_VIRUS_FOUND = "haltOnVirusFound";
boolean getBoolean(String key);
String getString(String key);
int getInt(String key);
Object getObject(String key);
}
--- NEW FILE: DefaultScanConfiguration.java ---
/*
* $Id: DefaultScanConfiguration.java,v 1.1 2003/12/14 11:08:26 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;
import org.openantivirus.engine.censor.trie.*;
/**
* The default configuration used by the scanner
*
* Pattern-Roles:
* @author Kurt Huwig <[email protected]>
* @version $Revision: 1.1 $
*/
public class DefaultScanConfiguration extends WriteableScanConfiguration {
public DefaultScanConfiguration() {
putBoolean(HALT_ON_VIRUS_FOUND, true);
putString("credo.directory", "credo");
putInt("credo.level", 3);
putObject("trie.instance", new Trie());
putString("tempdirectory", "temp");
}
}
--- NEW FILE: WriteableScanConfiguration.java ---
/*
* $Id: WriteableScanConfiguration.java,v 1.1 2003/12/14 11:08:26 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;
import java.util.*;
/**
* A ScanConfiguration that can be changed; can have a parent configuration to
* look up entries that it does not have.
*
* Pattern-Roles:
* @author Kurt Huwig <[email protected]>
* @version $Revision: 1.1 $
*/
public class WriteableScanConfiguration implements ScanConfiguration {
private Map properties = new HashMap();
private final ScanConfiguration parent;
protected WriteableScanConfiguration() {
parent = null;
}
public WriteableScanConfiguration(ScanConfiguration parent) {
if (parent == null) {
throw new IllegalArgumentException("Parent may not be null");
}
this.parent = parent;
}
public boolean getBoolean(String key) {
return ((Boolean) getObject(key)).booleanValue();
}
public String getString(String key) {
return (String) getObject(key);
}
public int getInt(String key) {
return ((Integer) getObject(key)).intValue();
}
public Object getObject(String key) {
final Object result = properties.get(key);
if (result != null) {
return result;
} else {
if (parent != null) {
return parent.getObject(key);
} else {
throw new IllegalArgumentException("unknown key '" + key + "'");
}
}
}
public void putObject(String key, Object value) {
properties.put(key, value);
}
public void putBoolean(String key, boolean value) {
putObject(key, value ? Boolean.TRUE : Boolean.FALSE);
}
public void putString(String key, String value) {
putObject(key, value);
}
public void putInt(String key, int value) {
putObject(key, new Integer(value));
}
}
--- NEW FILE: ScanEngine.java ---
/*
* $Id: ScanEngine.java,v 1.1 2003/12/14 11:08:26 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;
import java.io.*;
import org.openantivirus.engine.censor.*;
import org.openantivirus.engine.censor.trie.*;
import org.openantivirus.engine.credo.*;
import org.openantivirus.engine.vfs.*;
import org.openantivirus.engine.vfs.container.*;
import org.openantivirus.engine.vfs.entry.*;
/**
* You need this class to scan files
*
* Pattern-Roles:
* @author Kurt Huwig <[email protected]>
* @version $Revision: 1.1 $
*/
public class ScanEngine {
private final VfsContainerFactory[] containerFactories = {
new DirectoryContainerFactory(),
new CompressedContainerFactory()};
private final Censor[] censors;
private final ScanConfiguration scanConfiguration;
public ScanEngine() throws IOException, CredoException {
this(new DefaultScanConfiguration());
}
public ScanEngine(ScanConfiguration scanConfiguration)
throws IOException, CredoException {
this.scanConfiguration = scanConfiguration;
final Trie trie = (Trie) scanConfiguration.getObject("trie.instance");
final TrieCensor trieCensor = new TrieCensor(trie);
censors = new Censor[] {trieCensor};
final CredoParser credoParser =
new CredoParser(scanConfiguration, trie);
credoParser.parse(new File(
scanConfiguration.getString("credo.directory")));
trie.prepare();
}
/**
* scans the file using the default configuration
*
* @throws IOException
* if the file cannot be accessed
* @throws MalwareFoundException
* if malware has been found within this file
*/
public void scan(File file) throws MalwareFoundException, IOException {
scan(file, scanConfiguration);
}
/**
* scans the file using the given configuration
*
* @throws IOException
* if the file cannot be accessed
* @throws MalwareFoundException
* if malware has been found within this file
*/
public void scan(File file, ScanConfiguration conf)
throws MalwareFoundException, IOException {
scan(new FileVfsEntry(file), conf);
}
protected void scan(VfsEntry entry, ScanConfiguration conf)
throws MalwareFoundException {
try {
if (scanContainer(entry, conf)) {
return;
}
for (int i = 0; i < censors.length; i++) {
try {
final int result = censors[i].censor(entry);
if (result == Censor.ENTRY_CLEAN) {
return;
}
} catch (MalwareFoundException mfe) {
notifyMalwareFound(mfe);
if (conf.getBoolean(
DefaultScanConfiguration.HALT_ON_VIRUS_FOUND)) {
throw mfe;
}
} catch (Exception e) {
// this does not keep us from scanning
notifyException(e);
}
}
} finally {
try {
entry.dispose();
} catch (IOException ioe) {
notifyException(ioe);
}
}
}
protected boolean scanContainer(VfsEntry entry, ScanConfiguration conf)
throws MalwareFoundException {
boolean containerFound = false;
VfsContainer container = null;
try {
for (int i = 0;
i < containerFactories.length && !containerFound;
i++) {
try {
container = containerFactories[i].getContainer(entry, conf);
if (container == null) {
continue;
} else {
containerFound = true;
}
while (container.hasNext()) {
try {
scan(container.next(), conf);
} catch (MalwareFoundException mfe) {
throw mfe;
} catch (Exception e) {
// ok, try the next one
notifyException(e);
}
}
container.dispose();
} catch (MalwareFoundException mfe) {
throw mfe;
} catch (Exception e) {
notifyException(e);
}
}
} finally {
if (containerFound) {
try {
container.dispose();
} catch (IOException ioe) {
notifyException(ioe);
}
}
}
return containerFound;
}
protected void notifyException(Exception exception) {
exception.printStackTrace();
}
protected void notifyMalwareFound(MalwareFoundException mfe) {
System.out.println("FOUND: " + mfe.getName());
System.out.println(" in '" + mfe.getEntry().getName() + "'");
}
}
-------------------------------------------------------
This SF.net email is sponsored by: SF.net Giveback Program.
Does SourceForge.net help you be more productive? Does it
help you create better code? SHARE THE LOVE, and help us help
YOU! Click Here: http://sourceforge.net/donate/
lmpx.com only provides a reader for public news (NNTP) servers. It is not
affiliated with the servers or forums shown here and is not responsible for
the content of articles, which is written by their respective authors.