Author: bodewig
Date: Wed Feb 26 20:00:21 2014
New Revision: 1572222

URL: http://svn.apache.org/r1572222
Log:
re-shuffle som code

Added:
    
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/AES256SHA256Decoder.java
   (with props)
    
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/CoderBase.java
   (with props)
Modified:
    
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/Coders.java
    
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/LZMA2Decoder.java

Added: 
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/AES256SHA256Decoder.java
URL: 
http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/AES256SHA256Decoder.java?rev=1572222&view=auto
==============================================================================
--- 
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/AES256SHA256Decoder.java
 (added)
+++ 
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/AES256SHA256Decoder.java
 Wed Feb 26 20:00:21 2014
@@ -0,0 +1,125 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (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.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ */
+package org.apache.commons.compress.archivers.sevenz;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.security.GeneralSecurityException;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import javax.crypto.Cipher;
+import javax.crypto.CipherInputStream;
+import javax.crypto.SecretKey;
+import javax.crypto.spec.IvParameterSpec;
+import javax.crypto.spec.SecretKeySpec;
+
+class AES256SHA256Decoder extends CoderBase {
+    @Override
+    InputStream decode(final InputStream in, final Coder coder, final byte[] 
passwordBytes) throws IOException {
+        return new InputStream() {
+            private boolean isInitialized = false;
+            private CipherInputStream cipherInputStream = null;
+                
+            private CipherInputStream init() throws IOException {
+                if (isInitialized) {
+                    return cipherInputStream;
+                }
+                final int byte0 = 0xff & coder.properties[0];
+                final int numCyclesPower = byte0 & 0x3f;
+                final int byte1 = 0xff & coder.properties[1];
+                final int ivSize = ((byte0 >> 6) & 1) + (byte1 & 0x0f);
+                final int saltSize = ((byte0 >> 7) & 1) + (byte1 >> 4);
+                if (2 + saltSize + ivSize > coder.properties.length) {
+                    throw new IOException("Salt size + IV size too long");
+                }
+                final byte[] salt = new byte[saltSize];
+                System.arraycopy(coder.properties, 2, salt, 0, saltSize);
+                final byte[] iv = new byte[16];
+                System.arraycopy(coder.properties, 2 + saltSize, iv, 0, 
ivSize);
+
+                if (passwordBytes == null) {
+                    throw new IOException("Cannot read encrypted files without 
a password");
+                }
+                final byte[] aesKeyBytes;
+                if (numCyclesPower == 0x3f) {
+                    aesKeyBytes = new byte[32];
+                    System.arraycopy(salt, 0, aesKeyBytes, 0, saltSize);
+                    System.arraycopy(passwordBytes, 0, aesKeyBytes, saltSize,
+                                     Math.min(passwordBytes.length, 
aesKeyBytes.length - saltSize));
+                } else {
+                    final MessageDigest digest;
+                    try {
+                        digest = MessageDigest.getInstance("SHA-256");
+                    } catch (NoSuchAlgorithmException 
noSuchAlgorithmException) {
+                        IOException ioe = new IOException("SHA-256 is 
unsupported by your Java implementation");
+                        ioe.initCause(noSuchAlgorithmException);
+                        throw ioe;
+                        // TODO: simplify when Compress requires Java 1.6      
          
+//                      throw new IOException("SHA-256 is unsupported by your 
Java implementation",
+//                              noSuchAlgorithmException);
+                    }
+                    final byte[] extra = new byte[8];
+                    for (long j = 0; j < (1L << numCyclesPower); j++) {
+                        digest.update(salt);
+                        digest.update(passwordBytes);
+                        digest.update(extra);
+                        for (int k = 0; k < extra.length; k++) {
+                            ++extra[k];
+                            if (extra[k] != 0) {
+                                break;
+                            }
+                        }
+                    }
+                    aesKeyBytes = digest.digest();
+                }
+
+                final SecretKey aesKey = new SecretKeySpec(aesKeyBytes, "AES");
+                try {
+                    final Cipher cipher = 
Cipher.getInstance("AES/CBC/NoPadding");
+                    cipher.init(Cipher.DECRYPT_MODE, aesKey, new 
IvParameterSpec(iv));
+                    cipherInputStream = new CipherInputStream(in, cipher);
+                    isInitialized = true;
+                    return cipherInputStream;
+                } catch (GeneralSecurityException generalSecurityException) {
+                    IOException ioe = new IOException("Decryption error " +
+                                                      "(do you have the JCE 
Unlimited Strength Jurisdiction Policy Files installed?)");
+                    ioe.initCause(generalSecurityException);
+                    throw ioe;
+                    // TODO: simplify when Compress requires Java 1.6          
      
+//                  throw new IOException("Decryption error " +
+//                          "(do you have the JCE Unlimited Strength 
Jurisdiction Policy Files installed?)",
+//                          generalSecurityException);
+                    }
+            }
+                
+            @Override
+            public int read() throws IOException {
+                return init().read();
+            }
+                
+            @Override
+            public int read(byte[] b, int off, int len) throws IOException {
+                return init().read(b, off, len);
+            }
+                
+            @Override
+            public void close() {
+            }
+        };
+    }
+}

Propchange: 
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/AES256SHA256Decoder.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/CoderBase.java
URL: 
http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/CoderBase.java?rev=1572222&view=auto
==============================================================================
--- 
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/CoderBase.java
 (added)
+++ 
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/CoderBase.java
 Wed Feb 26 20:00:21 2014
@@ -0,0 +1,76 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (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.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ */
+package org.apache.commons.compress.archivers.sevenz;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+/**
+ * Base Codec class.
+ */
+abstract class CoderBase {
+    private final Class<?>[] acceptableOptions;
+    private static final byte[] NONE = new byte[0];
+
+    /**
+     * @param acceptableOptions types that can be used as options for this 
codec.
+     */
+    protected CoderBase(Class<?>... acceptableOptions) {
+        this.acceptableOptions = acceptableOptions;
+    }
+
+    /**
+     * @return whether this method can extract options from the given object.
+     */
+    boolean canAcceptOptions(Object opts) {
+        for (Class<?> c : acceptableOptions) {
+            if (c.isInstance(opts)) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    /**
+     * @return property-bytes to write in a Folder block
+     */
+    byte[] getOptionsAsProperties(Object options) {
+        return NONE;
+    }
+
+    /**
+     * @return a stream that reads from in using the configured coder and 
password.
+     */
+    abstract InputStream decode(final InputStream in, final Coder coder, 
byte[] password) throws IOException;
+
+    /**
+     * @return a stream that writes to out using the given configuration.
+     */
+    OutputStream encode(final OutputStream out, final Object options) throws 
IOException {
+        throw new UnsupportedOperationException("method doesn't support 
writing");
+    }
+
+    /**
+     * If the option represents a number, return its integer
+     * value, otherwise return the given default value.
+     */
+    protected static int numberOptionOrDefault(Object options, int 
defaultValue) {
+        return options instanceof Number ? ((Number) options).intValue() : 
defaultValue;
+    }
+}

Propchange: 
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/CoderBase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: 
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/Coders.java
URL: 
http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/Coders.java?rev=1572222&r1=1572221&r2=1572222&view=diff
==============================================================================
--- 
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/Coders.java
 (original)
+++ 
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/Coders.java
 Wed Feb 26 20:00:21 2014
@@ -21,33 +21,30 @@ import java.io.FilterInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
-import java.security.GeneralSecurityException;
-import java.security.MessageDigest;
-import java.security.NoSuchAlgorithmException;
 import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
 import java.util.zip.Deflater;
 import java.util.zip.DeflaterOutputStream;
 import java.util.zip.Inflater;
 import java.util.zip.InflaterInputStream;
 
-import javax.crypto.Cipher;
-import javax.crypto.CipherInputStream;
-import javax.crypto.SecretKey;
-import javax.crypto.spec.IvParameterSpec;
-import javax.crypto.spec.SecretKeySpec;
-
 import 
org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
 import 
org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream;
 import org.tukaani.xz.LZMAInputStream;
 
 class Coders {
+    private static final Map<SevenZMethod, CoderBase> CODER_MAP = new 
HashMap<SevenZMethod, CoderBase>() {{
+            put(SevenZMethod.COPY, new CopyDecoder());
+            put(SevenZMethod.LZMA, new LZMADecoder());
+            put(SevenZMethod.LZMA2, new LZMA2Decoder());
+            put(SevenZMethod.DEFLATE, new DeflateDecoder());
+            put(SevenZMethod.BZIP2, new BZIP2Decoder());
+            put(SevenZMethod.AES256SHA256, new AES256SHA256Decoder());
+        }};
+
     static CoderBase findByMethod(SevenZMethod method) {
-        for (final CoderId coderId : coderTable) {
-            if (coderId.method.equals(method)) {
-                return coderId.coder;
-            }
-        }
-        return null;
+        return CODER_MAP.get(method);
     }
 
     static InputStream addDecoder(final InputStream is,
@@ -69,68 +66,6 @@ class Coders {
         return cb.encode(out, options);
     }
 
-    static CoderId[] coderTable = new CoderId[] {
-        new CoderId(SevenZMethod.COPY, new CopyDecoder()),
-        new CoderId(SevenZMethod.LZMA, new LZMADecoder()),
-        new CoderId(SevenZMethod.LZMA2, new LZMA2Decoder()),
-        new CoderId(SevenZMethod.DEFLATE, new DeflateDecoder()),
-        new CoderId(SevenZMethod.BZIP2, new BZIP2Decoder()),
-        new CoderId(SevenZMethod.AES256SHA256, new AES256SHA256Decoder())
-    };
-    
-    static class CoderId {
-        CoderId(SevenZMethod method, final CoderBase coder) {
-            this.method = method;
-            this.coder = coder;
-        }
-
-        final SevenZMethod method;
-        final CoderBase coder;
-    }
-    
-    static abstract class CoderBase {
-        private final Class<?>[] acceptableOptions;
-        private static final byte[] NONE = new byte[0];
-
-        protected CoderBase(Class<?>... acceptableOptions) {
-            this.acceptableOptions = acceptableOptions;
-        }
-
-        /**
-         * @return whether this method can extract options from the given 
object.
-         */
-        boolean canAcceptOptions(Object opts) {
-            for (Class<?> c : acceptableOptions) {
-                if (c.isInstance(opts)) {
-                    return true;
-                }
-            }
-            return false;
-        }
-
-        /**
-         * @return property-bytes to write in a Folder block
-         */
-        byte[] getOptionsAsProperties(Object options) {
-            return NONE;
-        }
-
-        abstract InputStream decode(final InputStream in, final Coder coder,
-                byte[] password) throws IOException;
-        OutputStream encode(final OutputStream out, final Object options)
-            throws IOException {
-            throw new UnsupportedOperationException("method doesn't support 
writing");
-        }
-
-        /**
-         * If the option represents a number, return its integer
-         * value, otherwise return the given default value.
-         */
-        protected int numberOptionOrDefault(Object options, int defaultValue) {
-            return options instanceof Number ? ((Number) options).intValue() : 
defaultValue;
-        }
-    }
-    
     static class CopyDecoder extends CoderBase {
         @Override
         InputStream decode(final InputStream in, final Coder coder,
@@ -195,103 +130,6 @@ class Coders {
         }
     }
 
-    static class AES256SHA256Decoder extends CoderBase {
-        @Override
-        InputStream decode(final InputStream in, final Coder coder,
-                final byte[] passwordBytes) throws IOException {
-            return new InputStream() {
-                private boolean isInitialized = false;
-                private CipherInputStream cipherInputStream = null;
-                
-                private CipherInputStream init() throws IOException {
-                    if (isInitialized) {
-                        return cipherInputStream;
-                    }
-                    final int byte0 = 0xff & coder.properties[0];
-                    final int numCyclesPower = byte0 & 0x3f;
-                    final int byte1 = 0xff & coder.properties[1];
-                    final int ivSize = ((byte0 >> 6) & 1) + (byte1 & 0x0f);
-                    final int saltSize = ((byte0 >> 7) & 1) + (byte1 >> 4);
-                    if (2 + saltSize + ivSize > coder.properties.length) {
-                        throw new IOException("Salt size + IV size too long");
-                    }
-                    final byte[] salt = new byte[saltSize];
-                    System.arraycopy(coder.properties, 2, salt, 0, saltSize);
-                    final byte[] iv = new byte[16];
-                    System.arraycopy(coder.properties, 2 + saltSize, iv, 0, 
ivSize);
-                    
-                    if (passwordBytes == null) {
-                        throw new IOException("Cannot read encrypted files 
without a password");
-                    }
-                    final byte[] aesKeyBytes;
-                    if (numCyclesPower == 0x3f) {
-                        aesKeyBytes = new byte[32];
-                        System.arraycopy(salt, 0, aesKeyBytes, 0, saltSize);
-                        System.arraycopy(passwordBytes, 0, aesKeyBytes, 
saltSize,
-                                Math.min(passwordBytes.length, 
aesKeyBytes.length - saltSize));
-                    } else {
-                        final MessageDigest digest;
-                        try {
-                            digest = MessageDigest.getInstance("SHA-256");
-                        } catch (NoSuchAlgorithmException 
noSuchAlgorithmException) {
-                            IOException ioe = new IOException("SHA-256 is 
unsupported by your Java implementation");
-                            ioe.initCause(noSuchAlgorithmException);
-                            throw ioe;
-        // TODO: simplify when Compress requires Java 1.6                
-//                            throw new IOException("SHA-256 is unsupported by 
your Java implementation",
-//                                    noSuchAlgorithmException);
-                        }
-                        final byte[] extra = new byte[8];
-                        for (long j = 0; j < (1L << numCyclesPower); j++) {
-                            digest.update(salt);
-                            digest.update(passwordBytes);
-                            digest.update(extra);
-                            for (int k = 0; k < extra.length; k++) {
-                                ++extra[k];
-                                if (extra[k] != 0) {
-                                    break;
-                                }
-                            }
-                        }
-                        aesKeyBytes = digest.digest();
-                    }
-                    
-                    final SecretKey aesKey = new SecretKeySpec(aesKeyBytes, 
"AES");
-                    try {
-                        final Cipher cipher = 
Cipher.getInstance("AES/CBC/NoPadding");
-                        cipher.init(Cipher.DECRYPT_MODE, aesKey, new 
IvParameterSpec(iv));
-                        cipherInputStream = new CipherInputStream(in, cipher);
-                        isInitialized = true;
-                        return cipherInputStream;
-                    } catch (GeneralSecurityException 
generalSecurityException) {
-                        IOException ioe = new IOException("Decryption error " +
-                                "(do you have the JCE Unlimited Strength 
Jurisdiction Policy Files installed?)");
-                        ioe.initCause(generalSecurityException);
-                        throw ioe;
-        // TODO: simplify when Compress requires Java 1.6                
-//                        throw new IOException("Decryption error " +
-//                                "(do you have the JCE Unlimited Strength 
Jurisdiction Policy Files installed?)",
-//                                generalSecurityException);
-                    }
-                }
-                
-                @Override
-                public int read() throws IOException {
-                    return init().read();
-                }
-                
-                @Override
-                public int read(byte[] b, int off, int len) throws IOException 
{
-                    return init().read(b, off, len);
-                }
-                
-                @Override
-                public void close() {
-                }
-            };
-        }
-    }
-
     /**
      * ZLIB requires an extra dummy byte.
      *

Modified: 
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/LZMA2Decoder.java
URL: 
http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/LZMA2Decoder.java?rev=1572222&r1=1572221&r2=1572222&view=diff
==============================================================================
--- 
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/LZMA2Decoder.java
 (original)
+++ 
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/LZMA2Decoder.java
 Wed Feb 26 20:00:21 2014
@@ -25,7 +25,7 @@ import org.tukaani.xz.FinishableOutputSt
 import org.tukaani.xz.LZMA2InputStream;
 import org.tukaani.xz.LZMA2Options;
 
-class LZMA2Decoder extends Coders.CoderBase {
+class LZMA2Decoder extends CoderBase {
     LZMA2Decoder() {
         super(LZMA2Options.class, Number.class);
     }


Reply via email to