Repository: commons-compress
Updated Branches:
  refs/heads/master f5330f7e6 -> 430e12676


Fix writing of multibyte name entries


Project: http://git-wip-us.apache.org/repos/asf/commons-compress/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-compress/commit/1fbb16b0
Tree: http://git-wip-us.apache.org/repos/asf/commons-compress/tree/1fbb16b0
Diff: http://git-wip-us.apache.org/repos/asf/commons-compress/diff/1fbb16b0

Branch: refs/heads/master
Commit: 1fbb16b037e62c6af90ab89ab3ac8d3633c59a90
Parents: 9e80104
Author: Jens Reimann <jreim...@redhat.com>
Authored: Wed Jul 11 09:20:44 2018 +0200
Committer: Stefan Bodewig <bode...@apache.org>
Committed: Wed Jul 11 18:05:49 2018 +0200

----------------------------------------------------------------------
 .../archivers/cpio/CpioArchiveEntry.java        | 26 ++++++++++++++++++--
 .../archivers/cpio/CpioArchiveOutputStream.java |  5 ++--
 2 files changed, 27 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-compress/blob/1fbb16b0/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveEntry.java
----------------------------------------------------------------------
diff --git 
a/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveEntry.java
 
b/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveEntry.java
index 3ad7c87..0266da1 100644
--- 
a/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveEntry.java
+++ 
b/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveEntry.java
@@ -19,6 +19,7 @@
 package org.apache.commons.compress.archivers.cpio;
 
 import java.io.File;
+import java.nio.charset.Charset;
 import java.util.Date;
 
 import org.apache.commons.compress.archivers.ArchiveEntry;
@@ -466,11 +467,32 @@ public class CpioArchiveEntry implements CpioConstants, 
ArchiveEntry {
     /**
      * Get the number of bytes needed to pad the header to the alignment 
boundary.
      *
+     * @deprecated This method doesn't properly work for multi-byte encodings. 
And
+     *             creates corrupt archives. Use {@link 
#getHeaderPadCount(Charset)}
+     *             or {@link #getHeaderPadCount(long)} in any case.
      * @return the number of bytes needed to pad the header (0,1,2,3)
      */
+    @Deprecated
     public int getHeaderPadCount(){
-        long namesize = name != null ? name.length() : 0;
-        return getHeaderPadCount(namesize);
+        return getHeaderPadCount(null);
+    }
+
+    /**
+     * Get the number of bytes needed to pad the header to the alignment 
boundary.
+     *
+     * @param charset
+     *             The character set used to encode the entry name in the 
stream. 
+     * @return the number of bytes needed to pad the header (0,1,2,3)
+     * @since 1.18
+     */
+    public int getHeaderPadCount(Charset charset){
+        if (name==null) {
+            return 0;
+        }
+        if (charset==null) {
+            return getHeaderPadCount(name.length());
+        }
+        return getHeaderPadCount(name.getBytes(charset).length);
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/commons-compress/blob/1fbb16b0/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveOutputStream.java
----------------------------------------------------------------------
diff --git 
a/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveOutputStream.java
 
b/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveOutputStream.java
index c0e04c4..07fd53a 100644
--- 
a/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveOutputStream.java
+++ 
b/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveOutputStream.java
@@ -22,6 +22,7 @@ import java.io.File;
 import java.io.IOException;
 import java.io.OutputStream;
 import java.nio.ByteBuffer;
+import java.nio.charset.Charset;
 import java.util.HashMap;
 
 import org.apache.commons.compress.archivers.ArchiveEntry;
@@ -302,7 +303,7 @@ public class CpioArchiveOutputStream extends 
ArchiveOutputStream implements
         writeAsciiLong(entry.getName().length() + 1L, 8, 16);
         writeAsciiLong(entry.getChksum(), 8, 16);
         writeCString(entry.getName());
-        pad(entry.getHeaderPadCount());
+        pad(entry.getHeaderPadCount(Charset.forName(encoding)));
     }
 
     private void writeOldAsciiEntry(final CpioArchiveEntry entry)
@@ -363,7 +364,7 @@ public class CpioArchiveOutputStream extends 
ArchiveOutputStream implements
         writeBinaryLong(entry.getName().length() + 1L, 2, swapHalfWord);
         writeBinaryLong(entry.getSize(), 4, swapHalfWord);
         writeCString(entry.getName());
-        pad(entry.getHeaderPadCount());
+        pad(entry.getHeaderPadCount(Charset.forName(encoding)));
     }
 
     /*(non-Javadoc)

Reply via email to