commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject svn commit: r911769 - in /commons/proper/compress/trunk/src: main/java/org/apache/commons/compress/archivers/zip/GeneralPurposeBit.java test/java/org/apache/commons/compress/archivers/zip/GeneralPurposeBitTest.java
Date Fri, 19 Feb 2010 10:09:03 GMT
Author: bodewig
Date: Fri Feb 19 10:09:02 2010
New Revision: 911769

URL: http://svn.apache.org/viewvc?rev=911769&view=rev
Log:
add encryption related flags.  COMPRESS-89

Modified:
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/GeneralPurposeBit.java
    commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/GeneralPurposeBitTest.java

Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/GeneralPurposeBit.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/GeneralPurposeBit.java?rev=911769&r1=911768&r2=911769&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/GeneralPurposeBit.java
(original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/GeneralPurposeBit.java
Fri Feb 19 10:09:02 2010
@@ -24,12 +24,22 @@
  */
 public class GeneralPurposeBit {
     /**
+     * Indicates that the file is encrypted.
+     */
+    private static final int ENCRYPTION_FLAG = 1 << 0;
+
+    /**
      * Indicates that a data descriptor stored after the file contents
      * will hold CRC and size information.
      */
     private static final int DATA_DESCRIPTOR_FLAG = 1 << 3;
 
     /**
+     * Indicates strong encryption.
+     */
+    private static final int STRONG_ENCRYPTION_FLAG = 1 << 6;
+
+    /**
      * Indicates that filenames are written in utf-8.
      *
      * <p>The only reason this is public is that {@link
@@ -40,6 +50,8 @@
 
     private boolean languageEncodingFlag = false;
     private boolean dataDescriptorFlag = false;
+    private boolean encryptionFlag = false;
+    private boolean strongEncryptionFlag = false;
 
     public GeneralPurposeBit() {
     }
@@ -75,6 +87,37 @@
     }
 
     /**
+     * whether the current entry is encrypted
+     */
+    public boolean usesEncryption() {
+        return encryptionFlag;
+    }
+
+    /**
+     * whether the current entry will be encrypted
+     */
+    public void useEncryption(boolean b) {
+        encryptionFlag = b;
+    }
+
+    /**
+     * whether the current entry is encrypted using strong encryption
+     */
+    public boolean usesStrongEncryption() {
+        return encryptionFlag && strongEncryptionFlag;
+    }
+
+    /**
+     * whether the current entry will be encrypted  using strong encryption
+     */
+    public void useStrongEncryption(boolean b) {
+        strongEncryptionFlag = b;
+        if (b) {
+            useEncryption(true);
+        }
+    }
+
+    /**
      * Encodes the set bits in a form suitable for ZIP archives.
      */
     public byte[] encode() {
@@ -82,6 +125,10 @@
             ZipShort.getBytes((dataDescriptorFlag ? DATA_DESCRIPTOR_FLAG : 0)
                               |
                               (languageEncodingFlag ? UFT8_NAMES_FLAG : 0)
+                              |
+                              (encryptionFlag ? ENCRYPTION_FLAG : 0)
+                              |
+                              (strongEncryptionFlag ? STRONG_ENCRYPTION_FLAG : 0)
                               );
     }
 
@@ -95,6 +142,9 @@
         GeneralPurposeBit b = new GeneralPurposeBit();
         b.useDataDescriptor((generalPurposeFlag & DATA_DESCRIPTOR_FLAG) != 0);
         b.useUTF8ForNames((generalPurposeFlag & UFT8_NAMES_FLAG) != 0);
+        b.useStrongEncryption((generalPurposeFlag & STRONG_ENCRYPTION_FLAG)
+                              != 0);
+        b.useEncryption((generalPurposeFlag & ENCRYPTION_FLAG) != 0);
         return b;
     }
 }

Modified: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/GeneralPurposeBitTest.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/GeneralPurposeBitTest.java?rev=911769&r1=911768&r2=911769&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/GeneralPurposeBitTest.java
(original)
+++ commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/GeneralPurposeBitTest.java
Fri Feb 19 10:09:02 2010
@@ -27,6 +27,8 @@
     public void testDefaults() {
         assertFalse(new GeneralPurposeBit().usesDataDescriptor());
         assertFalse(new GeneralPurposeBit().usesUTF8ForNames());
+        assertFalse(new GeneralPurposeBit().usesEncryption());
+        assertFalse(new GeneralPurposeBit().usesStrongEncryption());
         byte[] b = new byte[2];
         assertTrue(Arrays.equals(b, new GeneralPurposeBit().encode()));
     }
@@ -36,12 +38,22 @@
                     .usesDataDescriptor());
         assertFalse(GeneralPurposeBit.parse(new byte[2], 0)
                     .usesUTF8ForNames());
+        assertFalse(GeneralPurposeBit.parse(new byte[2], 0)
+                    .usesEncryption());
+        assertFalse(GeneralPurposeBit.parse(new byte[2], 0)
+                    .usesStrongEncryption());
         assertTrue(GeneralPurposeBit.parse(new byte[] {(byte) 255, (byte) 255},
                                            0)
                    .usesDataDescriptor());
         assertTrue(GeneralPurposeBit.parse(new byte[] {(byte) 255, (byte) 255},
                                            0)
                    .usesUTF8ForNames());
+        assertTrue(GeneralPurposeBit.parse(new byte[] {(byte) 255, (byte) 255},
+                                           0)
+                   .usesEncryption());
+        assertTrue(GeneralPurposeBit.parse(new byte[] {(byte) 255, (byte) 255},
+                                           0)
+                   .usesStrongEncryption());
     }
 
     public void testDataDescriptor() {
@@ -59,4 +71,25 @@
         b.useUTF8ForNames(true);
         assertTrue(Arrays.equals(flags, b.encode()));
     }
+
+    public void testEncryption() {
+        byte[] flags = new byte[] {(byte) 1, (byte) 0};
+        assertTrue(GeneralPurposeBit.parse(flags, 0).usesEncryption());
+        GeneralPurposeBit b = new GeneralPurposeBit();
+        b.useEncryption(true);
+        assertTrue(Arrays.equals(flags, b.encode()));
+    }
+
+    public void testStringEncryption() {
+        byte[] flags = new byte[] {(byte) 65, (byte) 0};
+        assertTrue(GeneralPurposeBit.parse(flags, 0).usesStrongEncryption());
+        GeneralPurposeBit b = new GeneralPurposeBit();
+        b.useStrongEncryption(true);
+        assertTrue(b.usesEncryption());
+        assertTrue(Arrays.equals(flags, b.encode()));
+
+        flags = new byte[] {(byte) 64, (byte) 0};
+        assertFalse(GeneralPurposeBit.parse(flags, 0).usesStrongEncryption());
+    }
+
 }
\ No newline at end of file



Mime
View raw message