java/src/org/openantivirus/engine/vfs/container SingleFileContainer.java,NONE,1.1 ZipContainer.java,NONE,1.1 DirectoryContainer.java,NONE,1.1 CompressedContainerFactory.java,NONE,1.1 TarContainer.java,NONE,1.1 GzipContainer.java,NONE,1.1 DirectoryContainerFactory.java,NONE,1.1 ArchiveContainer.java,NONE,1.1 UpxContainer.java,NONE,1.1 Bzip2Container.java,NONE,1.1

[email protected]
Newsgroups gmane.comp.security.virus.openantivirus.cvs
Message-ID <[email protected]>
Update of /cvsroot/openantivirus/java/src/org/openantivirus/engine/vfs/container
In directory sc8-pr-cvs1:/tmp/cvs-serv4777/java/src/org/openantivirus/engine/vfs/container

Added Files:
	SingleFileContainer.java ZipContainer.java 
	DirectoryContainer.java CompressedContainerFactory.java 
	TarContainer.java GzipContainer.java 
	DirectoryContainerFactory.java ArchiveContainer.java 
	UpxContainer.java Bzip2Container.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: SingleFileContainer.java ---
/*
 * $Id: SingleFileContainer.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.vfs.container;

import java.io.*;

import org.openantivirus.engine.*;
import org.openantivirus.engine.vfs.*;
import org.openantivirus.engine.vfs.entry.*;

/**
 * Container for a single file, e.g. a gzip-compressed file
 * 
 * Pattern-Roles:
 * @author  Kurt Huwig <[email protected]>
 * @version $Revision: 1.1 $
 */
public abstract class SingleFileContainer implements VfsContainer, VfsEntry {

    private final VfsEntry fileEntry;
    private final TemporaryFile tempFile;
    private final VfsEntry entry;
    private final String type;
    private boolean read = false;
    private boolean initialized = false;
    
    protected SingleFileContainer(VfsEntry entry,
                                  String type,
                                  ScanConfiguration scanConf)
    throws IOException{
        this.entry = entry;
        this.type  = type;
        tempFile = new TemporaryFile(scanConf);
        fileEntry = new FileVfsEntry(tempFile.getFile());
    }
    
    protected void init() throws IOException {
        try {
            extractFile(entry, tempFile.getFile());
        } catch (Exception e) {
            tempFile.delete();
            throw new IOException("error while extracting: " + e.getMessage());
        }
        
        initialized = true;
    }
    
    public abstract void extractFile(VfsEntry entry, File tempFile)
    throws IOException;
    
    public void dispose() throws IOException {
        tempFile.delete();
    }

    public File getFile() throws IOException {
        return tempFile.getFile();
    }

    public byte[] getStart() throws IOException {
        return fileEntry.getStart();
    }

    public boolean hasNext() {
        return !read;
    }

    public VfsEntry next() throws IOException {
        if (!initialized) {
            throw new IllegalStateException("not initialized");
        }
        read = true;
        return this;
    }

    protected void copyStream(final InputStream is, final OutputStream os) throws IOException {
        final byte[] buffer = new byte[32768];
        int length;
        while ((length = is.read(buffer)) != -1) {
            os.write(buffer, 0, length);
        }
        is.close();
        os.close();
    }

    public String getName() {
        return entry.getName() + " >> " + type;
    }

    protected void runCommand(final String[] command, File tempFile) throws IOException, FileNotFoundException {
        Process process = null;
        try {
            process = Runtime.getRuntime().exec(command);
            copyStream(process.getInputStream(),
                       new FileOutputStream(tempFile));
            
        } finally {
            if (process != null) {
                process.destroy();
            }
        }
    }

}

--- NEW FILE: ZipContainer.java ---
/*
 * $Id: ZipContainer.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.vfs.container;

import java.io.*;
import java.util.*;
import java.util.zip.*;

import org.openantivirus.engine.*;
import org.openantivirus.engine.vfs.*;
import org.openantivirus.engine.vfs.entry.*;

/**
 * VfsContainer for ZIP files
 *
 * Pattern-Roles:
 * @author  Kurt Huwig <[email protected]>
 * @version $Revision: 1.1 $
 */
public class ZipContainer implements VfsContainer {
    
    private final ScanConfiguration configuration;
    private final VfsEntry entry;
    private final ZipFile zipFile;
    private final Enumeration entries;
    
    private ZipEntry nextEntry;
    
    public ZipContainer(VfsEntry entry, ScanConfiguration configuration)
    throws IOException {
        this.configuration = configuration;
        this.entry = entry;
        
        zipFile = new ZipFile(entry.getFile());
        entries = zipFile.entries();
        
        determineNext();
    }
        
    public boolean hasNext() {
        return nextEntry != null;
    }
    
    public VfsEntry next() throws IOException {
        final VfsEntry result = new TemporaryIsVfsEntry(
                entry.getName() + " >> zip:" + nextEntry.getName(),
                configuration,
                zipFile.getInputStream(nextEntry));
        determineNext();
        return result;
    }
    
    protected void determineNext() {
        do {
            if (!entries.hasMoreElements()) {
                nextEntry = null;
                break;
            }
            
            nextEntry = (ZipEntry) entries.nextElement();
        } while (nextEntry.isDirectory());
    }
    
    public void dispose() throws IOException {
        zipFile.close();
        nextEntry = null;
    }
}

--- NEW FILE: DirectoryContainer.java ---
/*
 * $Id: DirectoryContainer.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.vfs.container;

import java.io.*;
import java.util.*;
import org.openantivirus.engine.*;
import org.openantivirus.engine.vfs.*;
import org.openantivirus.engine.vfs.entry.*;

/**
 * Container for plain directories; can strip parts of the path, e.g. for
 * temporary directories
 *
 * Pattern-Roles:
 * @author  Kurt Huwig <[email protected]>
 * @version $Revision: 1.1 $
 */
public class DirectoryContainer implements VfsContainer {
    
    /** to do list */
    private final LinkedList directories = new LinkedList();
    
    private final VfsEntry entry;
    private final ScanConfiguration scanConf;
    private final int removeLength;
    
    /** files in the current directory */
    private File[] currentFiles;
    
    /** position within the current directory */
    private int currentIndex;
    
    /** the next file (not directory) */
    private File nextFile;
    
    /** @param directory directory for this container */
    public DirectoryContainer(VfsEntry entry, ScanConfiguration scanConf)
    throws IOException {
        this(entry, scanConf, null);
    }
    /** @param directory directory for this container */
    public DirectoryContainer(VfsEntry entry,
                              ScanConfiguration scanConf,
                              String pathRemove)
                              throws IOException {
        this.entry        = entry;
        this.scanConf     = scanConf;
        this.removeLength = (pathRemove != null ? pathRemove.length() : 0);
        
        final File directory = entry.getFile();
        if (!directory.isDirectory()) {
            throw new IllegalArgumentException(
                    "not a directory: " + directory.getAbsolutePath());
        }
        
        handleDirectory(directory);
        determineNext();
    }
    
    /** @return if there are more VfsEntrys in this container */
    public boolean hasNext() {
        return nextFile != null;
    }
    
    /**
     * @return the next VfsEntry in "first files than directories"
     *         in depth search order
     * @throws IOException if the file cannot be accessed
     */
    public VfsEntry next() throws IOException {
        final VfsEntry result;
        if (removeLength == 0) {
            result = new FileVfsEntry(nextFile);
        } else {
            result = new FileVfsEntry(
                    nextFile,
                    entry.getName()
                    + FileVfsEntry.getRelativeName(nextFile)
                            .substring(removeLength));
        }
        
        determineNext();
        return result;
    }
    
    /** determines the next file (not directory) */
    protected void determineNext() {
        File next;
        while ((next = getNext()) != null) {
            if (next.isFile()) {
                // we want a plain file, nothing else
                break;
            } else if (next.isDirectory()) {
                // we will get to you later
                directories.add(next);
            }
        }
        
        // this is either the next file or null if there is none
        nextFile = next;
    }

    /** @return the next file or directory */
    protected File getNext() {
        if (currentIndex < currentFiles.length) {
            return currentFiles[currentIndex++];
        } else {
            if (directories.isEmpty()) {
                return null;
            } else {
                handleDirectory((File) directories.removeFirst());
                return getNext();
            }
        }
    }
    
    /**
     * sets the file-array to the contents of this directory
     * and resets the index
     */
    protected void handleDirectory(File directory) {
        assert directory.isDirectory();
        
        currentFiles = directory.listFiles();
        currentIndex = 0;
    }
    
    public void dispose() {
        currentFiles = null;
        nextFile = null;
    }
    
}

--- NEW FILE: CompressedContainerFactory.java ---
/*
 * $Id: CompressedContainerFactory.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.vfs.container;

import java.io.*;

import org.openantivirus.engine.*;
import org.openantivirus.engine.vfs.*;
import org.openantivirus.engine.vfs.container.ucl.*;

/**
 * Detects different compression formats and returns the appropriate container
 *
 * Pattern-Roles: Factory
 * @author  Kurt Huwig <[email protected]>
 * @version $Revision: 1.1 $
 */
public class CompressedContainerFactory implements VfsContainerFactory {
    private final byte[]
            ZIP_MAGIC   = {'P', 'K', 3, 4},
            CAB_MAGIC   = {'M', 'S', 'C', 'F', 0, 0, 0, 0},
            EXE_MAGIC   = {'M', 'Z'},
            BZIP2_MAGIC = {'B', 'Z', 'h'},
            GZIP_MAGIC  = {(byte)0x1f, (byte)0x8b},
            TAR_MAGIC   = {'u', 's', 't', 'a', 'r'};
    
    public VfsContainer getContainer(VfsEntry entry,
                                     ScanConfiguration configuration)
                                     throws IOException {
        final byte[] start = entry.getStart();
        
        // ZIP
        if (startsWithMagic(start, ZIP_MAGIC)) {
            return new ZipContainer(entry, configuration);
        }
        
        // BZIP2
        if (startsWithMagic(start, BZIP2_MAGIC)) {
            return new Bzip2Container(entry, configuration);
        }
        
        // GZIP
        if (startsWithMagic(start, GZIP_MAGIC)) {
            return new GzipContainer(entry, configuration);
        }
        
        // TAR
        if (containsMagic(start, TAR_MAGIC, 257)) {
            return new TarContainer(entry, configuration);
        }
        
        // Microsoft Cabinet
        if (startsWithMagic(start, CAB_MAGIC)) {
            System.err.println("Cannot scan Microsoft Cabinet files");
        }
        
        if (startsWithMagic(start, EXE_MAGIC)) {
            final File file = entry.getFile();
            final RandomAccessFile raf = new RandomAccessFile(file, "r");
            final UPXDecompress upxDecompress =
                    new UPXDecompress(raf, file.length());
            
            boolean canUnpack;
            try {
                canUnpack = upxDecompress.canUnpack();
            } catch (Exception e) {
                // if anything goes wrong, we cannot unpack!
                e.printStackTrace();
                canUnpack = false;
            }
            if (canUnpack) {
                return new UpxContainer(entry, upxDecompress, configuration);
            } else {
                raf.close();
            }
        }        
        return null;
    }
    
    protected boolean startsWithMagic(byte[] start, byte[] magic) {
        return containsMagic(start, magic, 0);
    }
    
    protected boolean containsMagic(byte[] start, byte[] magic, int offset) {
        if (start.length < magic.length + offset) {
            return false;
        }
        
        for (int i = 0; i < magic.length; i++) {
            if (start[i + offset] != magic[i]) {
                return false;
            }
        }
        
        return true;
    }
}
--- NEW FILE: TarContainer.java ---
/*
 * $Id: TarContainer.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.vfs.container;

import java.io.*;

import org.openantivirus.engine.*;
import org.openantivirus.engine.vfs.*;

/**
 * Decompresses a tar file using the 'tar' commandline tool
 * 
 * Pattern-Roles:
 * @author  Kurt Huwig <[email protected]>
 * @version $Revision: 1.1 $
 */
public class TarContainer extends ArchiveContainer {
    public TarContainer(VfsEntry entry, ScanConfiguration scanConf)
    throws IOException {
        super(entry, " >> tar:", scanConf);
        init();
    }
    
    public void extractArchive(VfsEntry entry, File tempDir)
    throws IOException {
        runCommand(new String[] {
                "tar",
                "xfC",
                entry.getFile().getCanonicalPath(),
                tempDir.getCanonicalPath()});
    }
    
}
--- NEW FILE: GzipContainer.java ---
/*
 * $Id: GzipContainer.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.vfs.container;

import java.io.*;
import java.util.zip.*;

import org.openantivirus.engine.*;
import org.openantivirus.engine.vfs.*;

/**
 * Decompresses gzip compressed files using the Java internal gzip decompressor
 * 
 * Pattern-Roles:
 * @author  Kurt Huwig <[email protected]>
 * @version $Revision: 1.1 $
 */
public class GzipContainer extends SingleFileContainer {
    
    public GzipContainer(VfsEntry entry, ScanConfiguration scanConf)
    throws IOException {
        super(entry, "gzip", scanConf);
        init();
    }
    
    public void extractFile(VfsEntry entry, File tempFile)
    throws IOException {
        copyStream(new GZIPInputStream(new FileInputStream(entry.getFile())),
                   new FileOutputStream(tempFile));
    }
    
}

--- NEW FILE: DirectoryContainerFactory.java ---
/*
 * $Id: DirectoryContainerFactory.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.vfs.container;

import java.io.*;

import org.openantivirus.engine.*;
import org.openantivirus.engine.vfs.*;

/**
 * Factory for "normal" directories
 *  
 * Pattern-Roles: Factory
 * @author  Kurt Huwig <[email protected]>
 * @version $Revision: 1.1 $
 */
public class DirectoryContainerFactory implements VfsContainerFactory {

    public VfsContainer getContainer(VfsEntry entry,
                                     ScanConfiguration configuration)
    throws IOException {
        return entry.getFile().isDirectory()
               ? new DirectoryContainer(entry, configuration)
               : null;
    }

}

--- NEW FILE: ArchiveContainer.java ---
/*
 * $Id: ArchiveContainer.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.vfs.container;

import java.io.*;

import org.openantivirus.engine.*;
import org.openantivirus.engine.vfs.*;
import org.openantivirus.engine.vfs.entry.*;

/**
 * Container for archives, e.g. zip compressed files
 * 
 * Pattern-Roles:
 * @author  Kurt Huwig <[email protected]>
 * @version $Revision: 1.1 $
 */
public abstract class ArchiveContainer implements VfsContainer {

    private final VfsEntry entry;
    private final String type;
    private final TemporaryDirectory tempDir;
    private DirectoryContainer directory;
    private final ScanConfiguration scanConf;
    private boolean initialized = false;
    
    protected ArchiveContainer(VfsEntry entry,
                               String type,
                               ScanConfiguration scanConf)
    throws IOException{
        this.entry    = entry;
        this.type     = type;
        this.scanConf = scanConf;
        
        tempDir = new TemporaryDirectory(scanConf);
    }
    
    protected void init() throws IOException {
        try {
            extractArchive(entry, tempDir.getDirectory());
            directory = new DirectoryContainer(
                    new FileVfsEntry(tempDir.getDirectory(),
                            entry.getName() + type),
                    scanConf,
                    FileVfsEntry.getRelativeName(tempDir.getDirectory())
                        + File.separatorChar);
        } catch (Exception e) {
            tempDir.delete();
            throw new IOException("error while extracting: " + e.getMessage());
        }
        
        initialized = true;
    }
    
    public abstract void extractArchive(VfsEntry entry, File tempDir)
    throws IOException;
    
    public boolean hasNext() {
        return directory.hasNext();
    }

    public VfsEntry next() throws IOException {
        if (!initialized) {
            throw new IllegalStateException("not initialized");
        }
        return directory.next();
    }

    public void dispose() throws IOException {
        directory.dispose();
        tempDir.delete();
    }

    protected void runCommand(final String[] command) throws IOException {
        Process process = null;
        try {
            process = Runtime.getRuntime().exec(command);
            process.waitFor();
        } catch (InterruptedException ie) {
            // should not happen
            ie.printStackTrace();
        } finally {
            if (process != null) {
                process.destroy();
            }
        }
    }

}

--- NEW FILE: UpxContainer.java ---
/*
 * $Id: UpxContainer.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.vfs.container;

import java.io.*;

import org.openantivirus.engine.*;
import org.openantivirus.engine.vfs.*;
import org.openantivirus.engine.vfs.container.ucl.*;

/**
 * Handles UPX compressed files; first tries Java decompression code and if it
 * fails tries to decompress using the 'upx' command line tool.
 *
 * Pattern-Roles:
 * @author  Kurt Huwig <[email protected]>
 * @version $Revision: 1.1 $
 */
public class UpxContainer extends SingleFileContainer {
    private final UPXDecompress upxDecompress;
    
    public UpxContainer(VfsEntry entry,
                           UPXDecompress upxDecompress,
                           ScanConfiguration scanConf)
    throws IOException {
        super(entry, "upx", scanConf);
        this.upxDecompress = upxDecompress;
        init();
    }
    
    public void extractFile(VfsEntry entry, File tempFile)
    throws IOException {
        try {
            final OutputStream os = new FileOutputStream(tempFile);
            
            try {
                upxDecompress.decompress(os);
            } finally {
                os.close();
                upxDecompress.close();
            }
            
        } catch (Exception e) {
            // broken UPX files may break decompression
            e.printStackTrace();
            tempFile.delete();
            
            try {
                if (Runtime.getRuntime().exec(new String[] {
                            "upx",
                            "-dq",
                            "-o" + tempFile.getCanonicalPath(),
                            entry.getFile().getCanonicalPath()}).waitFor()
                        != 0) {
                    throw new IOException("Broken UPX file");
                }
            } catch (InterruptedException io) {
                throw new IOException("UPX decompress interrupted");
            }
        }
    }
    
}

--- NEW FILE: Bzip2Container.java ---
/*
 * $Id: Bzip2Container.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.vfs.container;

import java.io.*;

import org.openantivirus.engine.*;
import org.openantivirus.engine.vfs.*;

/**
 * Decompresses bzip2 compressed files using the bzip2 commandline tool
 * 
 * Pattern-Roles:
 * @author  Kurt Huwig <[email protected]>
 * @version $Revision: 1.1 $
 */
public class Bzip2Container extends SingleFileContainer {
    
    public Bzip2Container(VfsEntry entry, ScanConfiguration scanConf)
    throws IOException {
        super(entry, "bzip2", scanConf);
        init();
    }

    public void extractFile(VfsEntry entry, File tempFile)
    throws IOException {
        runCommand(new String[] {
                              "bunzip2",
                              "-c",
							  entry.getFile().getCanonicalPath()},
                   tempFile);
    }
    
}




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