commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject svn commit: r1296576 - in /commons/proper/compress/trunk/src: main/java/org/apache/commons/compress/archivers/tar/TarUtils.java test/java/org/apache/commons/compress/archivers/tar/TarUtilsTest.java
Date Sat, 03 Mar 2012 05:28:56 GMT
Author: bodewig
Date: Sat Mar  3 05:28:55 2012
New Revision: 1296576

URL: http://svn.apache.org/viewvc?rev=1296576&view=rev
Log:
Our parser for STAR-encoded binary values was wrong, STAR only uses 7 or 11 bytes and can
encode negative values as twos complement signaled by the first byte being 0xff rather than
0x80.  COMPRESS-182

Modified:
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarUtils.java
    commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/tar/TarUtilsTest.java

Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarUtils.java?rev=1296576&r1=1296575&r2=1296576&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarUtils.java
(original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarUtils.java
Sat Mar  3 05:28:55 2012
@@ -18,6 +18,8 @@
  */
 package org.apache.commons.compress.archivers.tar;
 
+import java.math.BigInteger;
+
 /**
  * This class provides static utility methods to work with byte streams.
  *
@@ -127,17 +129,52 @@ public class TarUtils {
         if ((buffer[offset] & 0x80) == 0) {
             return parseOctal(buffer, offset, length);
         }
-
-        long val = buffer[offset] & 0x7f;
+        final boolean negative = buffer[offset] == (byte) 0xff;
+        if (length < 9) {
+            return parseBinaryLong(buffer, offset, length, negative);
+        }
+        return parseBinaryBigInteger(buffer, offset, length, negative);
+    }
+
+    private static long parseBinaryLong(final byte[] buffer, final int offset,
+                                        final int length,
+                                        final boolean negative) {
+        if (length >= 9) {
+            throw new IllegalArgumentException("At offset " + offset + ", "
+                                               + length + " byte binary number"
+                                               + " exceeds maximum signed long"
+                                               + " value");
+        }
+        long val = 0;
         for (int i = 1; i < length; i++) {
-            if (val >= (1L << (63 - 8))) {
-                throw new IllegalArgumentException(
-                    "At offset " + offset + ", " + length + " byte " +
-                    "binary number exceeds maximum signed long value");
-            }
             val = (val << 8) + (buffer[offset + i] & 0xff);
         }
-        return val;
+        if (negative) {
+            // 2's complement
+            val--;
+            val ^= ((long) Math.pow(2, (length - 1) * 8) - 1);
+        }
+        return negative ? -val : val;
+    }
+
+    private static long parseBinaryBigInteger(final byte[] buffer,
+                                              final int offset,
+                                              final int length,
+                                              final boolean negative) {
+        byte[] remainder = new byte[length - 1];
+        System.arraycopy(buffer, offset + 1, remainder, 0, length - 1);
+        BigInteger val = new BigInteger(remainder);
+        if (negative) {
+            // 2's complement
+            val = val.add(BigInteger.valueOf(-1)).not();
+        }
+        if (val.bitLength() > 63) {
+            throw new IllegalArgumentException("At offset " + offset + ", "
+                                               + length + " byte binary number"
+                                               + " exceeds maximum signed long"
+                                               + " value");
+        }
+        return negative ? -val.longValue() : val.longValue();
     }
 
     /**
@@ -325,23 +362,54 @@ public class TarUtils {
         // Check whether we are dealing with UID/GID or SIZE field
         final long maxAsOctalChar = length == TarConstants.UIDLEN ? TarConstants.MAXID :
TarConstants.MAXSIZE;
 
-        if (value <= maxAsOctalChar) { // OK to store as octal chars
+        final boolean negative = value < 0;
+        if (!negative && value <= maxAsOctalChar) { // OK to store as octal chars
             return formatLongOctalBytes(value, buf, offset, length);
         }
 
-        long val = value;
-        for (int i = offset + length - 1; i >= offset; i--) {
-            buf[i] = (byte) val;
-            val >>= 8;
+        if (length < 9) {
+            formatLongBinary(value, buf, offset, length, negative);
         }
+        formatBigIntegerBinary(value, buf, offset, length, negative);
+
+        buf[offset] = (byte) (negative ? 0xff : 0x80);
+        return offset + length;
+    }
 
-        if (val != 0 || (buf[offset] & 0x80) != 0) {
+    private static void formatLongBinary(final long value, byte[] buf,
+                                         final int offset, final int length,
+                                         final boolean negative) {
+        final int bits = (length - 1) * 8;
+        final long max = (long) Math.pow(2, bits);
+        long val = Math.abs(value);
+        if (val >= max) {
             throw new IllegalArgumentException("Value " + value +
                 " is too large for " + length + " byte field.");
         }
+        if (negative) {
+            val ^= max - 1;
+            val |= 0xff << bits;
+            val++;
+        }
+        for (int i = offset + length - 1; i >= offset; i--) {
+            buf[i] = (byte) val;
+            val >>= 8;
+        }
+    }
 
-        buf[offset] |= 0x80;
-        return offset + length;
+    private static void formatBigIntegerBinary(final long value, byte[] buf,
+                                               final int offset,
+                                               final int length,
+                                               final boolean negative) {
+        BigInteger val = BigInteger.valueOf(value);
+        final byte[] b = val.toByteArray();
+        final int len = b.length;
+        final int off = offset + length - len;
+        System.arraycopy(b, 0, buf, off, len);
+        final byte fill = (byte) (negative ? 0xff : 0);
+        for (int i = offset + 1; i < off; i++) {
+            buf[i] = fill;
+        }
     }
 
     /**

Modified: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/tar/TarUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/tar/TarUtilsTest.java?rev=1296576&r1=1296575&r2=1296576&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/tar/TarUtilsTest.java
(original)
+++ commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/tar/TarUtilsTest.java
Sat Mar  3 05:28:55 2012
@@ -136,11 +136,18 @@ public class TarUtilsTest extends TestCa
         assertEquals(value,parseValue);
     }
 
-    public void testRoundTripOctalOrBinary() {
-        checkRoundTripOctalOrBinary(0, 8);
-        checkRoundTripOctalOrBinary(1, 8);
-        checkRoundTripOctalOrBinary(Long.MAX_VALUE, 8); // [0x7f ff ff ff ff ff ff ff
-        checkRoundTripOctalOrBinary(TarConstants.MAXSIZE, 8); // will need binary format
+    public void testRoundTripOctalOrBinary8() {
+        testRoundTripOctalOrBinary(8);
+    }
+    public void testRoundTripOctalOrBinary12() {
+        testRoundTripOctalOrBinary(12);
+    }
+    private void testRoundTripOctalOrBinary(int length) {
+        checkRoundTripOctalOrBinary(0, length);
+        checkRoundTripOctalOrBinary(1, length);
+        checkRoundTripOctalOrBinary(Long.MAX_VALUE >> 7, length); // [0x00 ff ff ff
ff ff ff ff
+        checkRoundTripOctalOrBinary(TarConstants.MAXSIZE, length); // will need binary format
+        checkRoundTripOctalOrBinary(-1, length); // will need binary format
     }
     
     // Check correct trailing bytes are generated
@@ -190,4 +197,31 @@ public class TarUtilsTest extends TestCa
         int len = TarUtils.formatNameBytes(string, buff, 0, buff.length);
         assertEquals(string, TarUtils.parseName(buff, 0, len));
     }
+
+    public void testReadNegativeBinary8Byte() {
+        byte[] b = new byte[] {
+            (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
+            (byte) 0xff, (byte) 0xff, (byte) 0xf1, (byte) 0xef,
+        };
+        assertEquals(-3601l, TarUtils.parseOctalOrBinary(b, 0, 8));
+    }
+
+    public void testReadNegativeBinary12Byte() {
+        byte[] b = new byte[] {
+            (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
+            (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
+            (byte) 0xff, (byte) 0xff, (byte) 0xf1, (byte) 0xef,
+        };
+        assertEquals(-3601l, TarUtils.parseOctalOrBinary(b, 0, 12));
+    }
+
+
+    public void testWriteNegativeBinary8Byte() {
+        byte[] b = new byte[] {
+            (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
+            (byte) 0xff, (byte) 0xff, (byte) 0xf1, (byte) 0xef,
+        };
+        assertEquals(-3601l, TarUtils.parseOctalOrBinary(b, 0, 8));
+    }
+
 }



Mime
View raw message