]> source.dussan.org Git - poi.git/commitdiff
always pad properties to 4 bytes
authorSergey Vladimirov <sergey@apache.org>
Sun, 2 Oct 2011 01:06:22 +0000 (01:06 +0000)
committerSergey Vladimirov <sergey@apache.org>
Sun, 2 Oct 2011 01:06:22 +0000 (01:06 +0000)
fix 51834 - Opening and Writing .doc file results in corrupt document

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1178113 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/status.xml
src/java/org/apache/poi/hpsf/VariantSupport.java
src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestBugs.java
test-data/document/Bug51834.doc [new file with mode: 0644]

index ef8c2d1b711bf4db793c698f3ff828c71d088673..dcadc73a8deefaa2d340d760dd2db3ebf4b2b115 100644 (file)
@@ -34,6 +34,7 @@
 
     <changes>
         <release version="3.8-beta5" date="2011-??-??">
+           <action dev="poi-developers" type="fix">51834 - Opening and Writing .doc file results in corrupt document</action>
            <action dev="poi-developers" type="fix">51902 - Picture.fillRawImageContent - ArrayIndexOutOfBoundsException (duplicate)</action>
            <action dev="poi-developers" type="fix">51890 - ArrayIndexOutOfBounds ExceptionPicture.fillRawImageContent</action>
            <action dev="poi-developers" type="add">Allow the passing of a File object to WorkbookFactory.create, which permits lower memory processing than the InputStream version</action>
index 1075a9cf8b395e8b88033b7272c0ba718259028c..af7347b7140dfddf95ee768fcd26899dccc41eb2 100644 (file)
@@ -482,8 +482,7 @@ public class VariantSupport extends Variant
                 else
                     trueOrFalse = (short) 0x0000;
                 TypeWriter.writeUShortToStream( out, trueOrFalse );
-                TypeWriter.writeUShortToStream( out, (short) 0x0000 );
-                length += 4;
+                length += 2;
                 break;
             }
             case Variant.VT_LPSTR:
@@ -515,9 +514,6 @@ public class VariantSupport extends Variant
                     out.write(highb);
                     length += 2;
                 }
-                out.write(0x00);
-                out.write(0x00);
-                length += 2;
                 break;
             }
             case Variant.VT_CF:
@@ -536,13 +532,7 @@ public class VariantSupport extends Variant
             case Variant.VT_I2:
             {
                 TypeWriter.writeToStream(out, ((Integer) value).shortValue());
-                // length = LittleEndianConsts.SHORT_SIZE;
-                TypeWriter.writeToStream( out, (short) 0x0000 );
-                /*
-                 * MUST be a 16-bit signed integer, followed by zero padding to 4
-                 * bytes -- http://msdn.microsoft.com/en-us/library/dd942532(v=PROT.13).aspx
-                 */
-                length = LittleEndianConsts.INT_SIZE;
+                length = LittleEndianConsts.SHORT_SIZE;
                 break;
             }
             case Variant.VT_I4:
@@ -599,6 +589,13 @@ public class VariantSupport extends Variant
             }
         }
 
+        /* pad values to 4-bytes */
+        while ( ( length & 0x3 ) != 0 )
+        {
+            out.write( 0x00 );
+            length++;
+        }
+
         return length;
     }
 }
index 50b6e527982db8532e1b2e217f2c049b59c581ee..686f29706c4f5e761c4d6429185e82a3f537c634 100644 (file)
@@ -17,7 +17,9 @@
 package org.apache.poi.hwpf.usermodel;
 
 import java.io.ByteArrayOutputStream;
+import java.io.File;
 import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.Arrays;
@@ -25,6 +27,7 @@ import java.util.Collection;
 import java.util.List;
 
 import junit.framework.TestCase;
+
 import org.apache.commons.codec.digest.DigestUtils;
 import org.apache.poi.POIDataSamples;
 import org.apache.poi.hwpf.HWPFDocument;
@@ -38,6 +41,7 @@ import org.apache.poi.hwpf.model.PlexOfField;
 import org.apache.poi.hwpf.model.SubdocumentType;
 import org.apache.poi.hwpf.model.io.HWPFOutputStream;
 import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
+import org.apache.poi.poifs.filesystem.POIFSFileSystem;
 import org.apache.poi.util.IOUtils;
 import org.apache.poi.util.POILogFactory;
 import org.apache.poi.util.POILogger;
@@ -700,4 +704,19 @@ public class TestBugs extends TestCase
                             + " has type " + pictureType );
         }
     }
+
+    /**
+     * [RESOLVED FIXED] Bug 51834 - Opening and Writing .doc file results in
+     * corrupt document
+     */
+    public void testBug51834() throws Exception
+    {
+        /*
+         * we don't have Java test for this file - it should be checked using
+         * Microsoft BFF Validator. But check read-write-read anyway. -- sergey
+         */
+        HWPFTestDataSamples.openSampleFile( "Bug51834.doc" );
+        HWPFTestDataSamples.writeOutAndReadBack( HWPFTestDataSamples
+                .openSampleFile( "Bug51834.doc" ) );
+    }
 }
diff --git a/test-data/document/Bug51834.doc b/test-data/document/Bug51834.doc
new file mode 100644 (file)
index 0000000..c162825
Binary files /dev/null and b/test-data/document/Bug51834.doc differ