commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject svn commit: r1653264 - in /commons/proper/compress/trunk/src: main/java/org/apache/commons/compress/archivers/sevenz/ test/java/org/apache/commons/compress/archivers/sevenz/
Date Tue, 20 Jan 2015 14:56:41 GMT
Author: bodewig
Date: Tue Jan 20 14:56:40 2015
New Revision: 1653264

URL: http://svn.apache.org/r1653264
Log:
provide archive name if anything goes wrong extracting stuff from 7z archives

Modified:
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/AES256SHA256Decoder.java
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/CoderBase.java
    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/DeltaDecoder.java
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/LZMA2Decoder.java
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/PasswordRequiredException.java
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java
    commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java

Modified: 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=1653264&r1=1653263&r2=1653264&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/AES256SHA256Decoder.java
(original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/AES256SHA256Decoder.java
Tue Jan 20 14:56:40 2015
@@ -30,7 +30,7 @@ import javax.crypto.spec.SecretKeySpec;
 
 class AES256SHA256Decoder extends CoderBase {
     @Override
-    InputStream decode(final InputStream in, long uncompressedLength,
+    InputStream decode(final String archiveName, final InputStream in, long uncompressedLength,
             final Coder coder, final byte[] passwordBytes) throws IOException {
         return new InputStream() {
             private boolean isInitialized = false;
@@ -46,7 +46,7 @@ class AES256SHA256Decoder extends CoderB
                 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");
+                    throw new IOException("Salt size + IV size too long in " + archiveName);
                 }
                 final byte[] salt = new byte[saltSize];
                 System.arraycopy(coder.properties, 2, salt, 0, saltSize);
@@ -54,7 +54,7 @@ class AES256SHA256Decoder extends CoderB
                 System.arraycopy(coder.properties, 2 + saltSize, iv, 0, ivSize);
 
                 if (passwordBytes == null) {
-                    throw new PasswordRequiredException();
+                    throw new PasswordRequiredException(archiveName);
                 }
                 final byte[] aesKeyBytes;
                 if (numCyclesPower == 0x3f) {

Modified: 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=1653264&r1=1653263&r2=1653264&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/CoderBase.java
(original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/CoderBase.java
Tue Jan 20 14:56:40 2015
@@ -64,7 +64,8 @@ abstract class CoderBase {
     /**
      * @return a stream that reads from in using the configured coder and password.
      */
-    abstract InputStream decode(final InputStream in, long uncomressedLength,
+    abstract InputStream decode(final String archiveName,
+        final InputStream in, long uncomressedLength,
         final Coder coder, byte[] password) throws IOException;
 
     /**

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=1653264&r1=1653263&r2=1653264&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
Tue Jan 20 14:56:40 2015
@@ -67,14 +67,15 @@ class Coders {
         return CODER_MAP.get(method);
     }
 
-    static InputStream addDecoder(final InputStream is, long uncompressedLength,
+    static InputStream addDecoder(final String archiveName, final InputStream is, long uncompressedLength,
             final Coder coder, final byte[] password) throws IOException {
         CoderBase cb = findByMethod(SevenZMethod.byId(coder.decompressionMethodId));
         if (cb == null) {
             throw new IOException("Unsupported compression method " +
-                                  Arrays.toString(coder.decompressionMethodId));
+                                  Arrays.toString(coder.decompressionMethodId)
+                                  + " used in " + archiveName);
         }
-        return cb.decode(is, uncompressedLength, coder, password);
+        return cb.decode(archiveName, is, uncompressedLength, coder, password);
     }
     
     static OutputStream addEncoder(final OutputStream out, final SevenZMethod method,
@@ -88,7 +89,7 @@ class Coders {
 
     static class CopyDecoder extends CoderBase {
         @Override
-        InputStream decode(final InputStream in, long uncompressedLength,
+        InputStream decode(final String archiveName, final InputStream in, long uncompressedLength,
                 final Coder coder, byte[] password) throws IOException {
             return in; 
         }
@@ -100,7 +101,7 @@ class Coders {
 
     static class LZMADecoder extends CoderBase {
         @Override
-        InputStream decode(final InputStream in, long uncompressedLength,
+        InputStream decode(final String archiveName, final InputStream in, long uncompressedLength,
                 final Coder coder, byte[] password) throws IOException {
             byte propsByte = coder.properties[0];
             long dictSize = coder.properties[1];
@@ -108,7 +109,7 @@ class Coders {
                 dictSize |= (coder.properties[i + 1] & 0xffl) << (8 * i);
             }
             if (dictSize > LZMAInputStream.DICT_SIZE_MAX) {
-                throw new IOException("Dictionary larger than 4GiB maximum size");
+                throw new IOException("Dictionary larger than 4GiB maximum size used in "
+ archiveName);
             }
             return new LZMAInputStream(in, uncompressedLength, propsByte, (int) dictSize);
         }
@@ -121,12 +122,13 @@ class Coders {
         }
 
         @Override
-        InputStream decode(final InputStream in, long uncompressedLength,
+        InputStream decode(final String archiveName, final InputStream in, long uncompressedLength,
                 final Coder coder, byte[] password) throws IOException {
             try {
                 return opts.getInputStream(in);
             } catch (AssertionError e) {
-                IOException ex = new IOException("BCJ filter needs XZ for Java > 1.4 -
see "
+                IOException ex = new IOException("BCJ filter used in " + archiveName
+                                                 + " needs XZ for Java > 1.4 - see "
                                                  + "http://commons.apache.org/proper/commons-compress/limitations.html#7Z");
                 ex.initCause(e);
                 throw ex;
@@ -149,7 +151,7 @@ class Coders {
         }
 
         @Override
-        InputStream decode(final InputStream in, long uncompressedLength,
+        InputStream decode(final String archiveName, final InputStream in, long uncompressedLength,
                 final Coder coder, final byte[] password)
             throws IOException {
             return new InflaterInputStream(new DummyByteAddingInputStream(in),
@@ -168,7 +170,7 @@ class Coders {
         }
 
         @Override
-        InputStream decode(final InputStream in, long uncompressedLength,
+        InputStream decode(final String archiveName, final InputStream in, long uncompressedLength,
                 final Coder coder, final byte[] password)
                 throws IOException {
             return new BZip2CompressorInputStream(in);

Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/DeltaDecoder.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/DeltaDecoder.java?rev=1653264&r1=1653263&r2=1653264&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/DeltaDecoder.java
(original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/DeltaDecoder.java
Tue Jan 20 14:56:40 2015
@@ -30,7 +30,7 @@ class DeltaDecoder extends CoderBase {
     }
 
     @Override
-    InputStream decode(final InputStream in, long uncompressedLength,
+    InputStream decode(final String archiveName, final InputStream in, long uncompressedLength,
             final Coder coder, byte[] password) throws IOException {
         return new DeltaOptions(getOptionsFromCoder(coder)).getInputStream(in);
     }

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=1653264&r1=1653263&r2=1653264&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
Tue Jan 20 14:56:40 2015
@@ -32,7 +32,7 @@ class LZMA2Decoder extends CoderBase {
     }
 
     @Override
-    InputStream decode(final InputStream in, long uncompressedLength,
+    InputStream decode(final String archiveName, final InputStream in, long uncompressedLength,
             final Coder coder, byte[] password) throws IOException {
         try {
             int dictionarySize = getDictionarySize(coder);

Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/PasswordRequiredException.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/PasswordRequiredException.java?rev=1653264&r1=1653263&r2=1653264&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/PasswordRequiredException.java
(original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/PasswordRequiredException.java
Tue Jan 20 14:56:40 2015
@@ -26,7 +26,7 @@ import java.io.IOException;
  */
 public class PasswordRequiredException extends IOException {
 
-    public PasswordRequiredException() {
-        super("Cannot read encrypted files without a password");
+    public PasswordRequiredException(String archiveName) {
+        super("Cannot read encrypted archive " + archiveName + " without a password.");
     }
 }

Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java?rev=1653264&r1=1653263&r2=1653264&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java
(original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java
Tue Jan 20 14:56:40 2015
@@ -67,6 +67,7 @@ import org.apache.commons.compress.utils
 public class SevenZFile implements Closeable {
     static final int SIGNATURE_HEADER_SIZE = 32;
 
+    private final String fileName;
     private RandomAccessFile file;
     private final Archive archive;
     private int currentEntryIndex = -1;
@@ -91,6 +92,7 @@ public class SevenZFile implements Close
     public SevenZFile(final File filename, final byte[] password) throws IOException {
         boolean succeeded = false;
         this.file = new RandomAccessFile(filename, "r");
+        this.fileName = filename.getAbsolutePath();
         try {
             archive = readHeaders(password);
             if (password != null) {
@@ -276,8 +278,8 @@ public class SevenZFile implements Close
             if (coder.numInStreams != 1 || coder.numOutStreams != 1) {
                 throw new IOException("Multi input/output stream coders are not yet supported");
             }
-            inputStreamStack = Coders.addDecoder(inputStreamStack, folder.getUnpackSizeForCoder(coder),
-                    coder, password);
+            inputStreamStack = Coders.addDecoder(fileName, inputStreamStack,
+                    folder.getUnpackSizeForCoder(coder), coder, password);
         }
         if (folder.hasCrc) {
             inputStreamStack = new CRC32VerifyingInputStream(inputStreamStack,
@@ -859,8 +861,8 @@ public class SevenZFile implements Close
                 throw new IOException("Multi input/output stream coders are not yet supported");
             }
             SevenZMethod method = SevenZMethod.byId(coder.decompressionMethodId);
-            inputStreamStack = Coders.addDecoder(inputStreamStack, folder.getUnpackSizeForCoder(coder),
-                    coder, password);
+            inputStreamStack = Coders.addDecoder(fileName, inputStreamStack,
+                    folder.getUnpackSizeForCoder(coder), coder, password);
             methods.addFirst(new SevenZMethodConfiguration(method,
                      Coders.findByMethod(method).getOptionsFromCoder(coder, inputStreamStack)));
         }

Modified: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java?rev=1653264&r1=1653263&r2=1653264&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java
(original)
+++ commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java
Tue Jan 20 14:56:40 2015
@@ -60,15 +60,6 @@ public class SevenZFileTest extends Abst
         }
     }
 
-    public void test7zDecryptUnarchiveWithoutPassword() throws Exception {
-        try {
-            test7zUnarchive(getFile("bla.encrypted.7z"), SevenZMethod.LZMA);
-            fail("Expected a PasswordRequiredException");
-        } catch (PasswordRequiredException ex) {
-            // expected
-        }
-    }
-
     private void test7zUnarchive(File f, SevenZMethod m) throws Exception {
         test7zUnarchive(f, m, null);
     }
@@ -77,9 +68,14 @@ public class SevenZFileTest extends Abst
         try {
             new SevenZFile(getFile("bla.encrypted.7z"));
             fail("shouldn't decrypt without a password");
-        } catch (IOException ex) {
-            assertEquals("Cannot read encrypted files without a password",
-                         ex.getMessage());
+        } catch (PasswordRequiredException ex) {
+            String msg = ex.getMessage();
+            assertTrue("Should start with whining about being unable to decrypt",
+                       msg.startsWith("Cannot read encrypted archive "));
+            assertTrue("Should finish the sentence properly",
+                       msg.endsWith(" without a password."));
+            assertTrue("Should contain archive's name",
+                       msg.contains("bla.encrypted.7z"));
         }
     }
 



Mime
View raw message