From: Sergey Vladimirov Date: Tue, 11 Sep 2012 19:49:44 +0000 (+0000) Subject: Fixed bug 53380 -- ArrayIndexOutOfBounds Excetion parsing word 97 document X-Git-Tag: 3.10-beta1~146 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=861bccdf3c31ad9fae6e235ba96910823d3c28e8;p=poi.git Fixed bug 53380 -- ArrayIndexOutOfBounds Excetion parsing word 97 document We had incorrect implementation for sprmCShd80 (0x4866) 0x66 processing, Shd was used instead of Shd80 git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1383584 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index d1896df4e6..5ea601b4c1 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + 53380 - ArrayIndexOutOfBounds Excetion parsing word 97 document. 53434 - Subtotal is not return correct value. 53642 - [PATCH] XLS formula evaluation logging 53561 - Unexpected adding of drawings into a workbook diff --git a/src/scratchpad/src/org/apache/poi/hwpf/model/types/SHD80AbstractType.java b/src/scratchpad/src/org/apache/poi/hwpf/model/types/SHD80AbstractType.java index 2a91c4b34f..1a617073d0 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/model/types/SHD80AbstractType.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/model/types/SHD80AbstractType.java @@ -14,17 +14,18 @@ See the License for the specific language governing permissions and limitations under the License. ==================================================================== */ -package org.apache.poi.hwpf.model.types; +package org.apache.poi.hwpf.model.types; import org.apache.poi.util.BitField; import org.apache.poi.util.Internal; import org.apache.poi.util.LittleEndian; /** - * The SHD80 is a substructure of the CHP and PAP, and TC for Word 97.

Class - and fields descriptions are quoted from - Microsoft Office Word 97-2007 Binary File Format + * The Shd80 structure specifies the colors and pattern that are used for background + shading. As an exception to the constraints that are specified by Ico and Ipat, a Shd80 can + be set to Shd80Nil and specifies that no shading is applied.

Class and fields + descriptions are quoted from Word (.doc) Binary File Format by Microsoft Corporation *

* NOTE: This source is automatically generated please do not modify this file. Either subclass or @@ -33,8 +34,7 @@ import org.apache.poi.util.LittleEndian; * This class is internal. It content or properties may change without notice * due to changes in our knowledge of internal Microsoft Word binary structures. - * @author Sergey Vladimirov; according to Microsoft Office Word 97-2007 Binary File Format - Specification [*.doc] + * @author Sergey Vladimirov; according to Word (.doc) Binary File Format by Microsoft Corporation. */ @Internal @@ -42,9 +42,9 @@ public abstract class SHD80AbstractType { protected short field_1_value; - /**/private static BitField icoFore = new BitField(0x001F); - /**/private static BitField icoBack = new BitField(0x03E0); - /**/private static BitField ipat = new BitField(0xFC00); + /**/private static final BitField icoFore = new BitField(0x001F); + /**/private static final BitField icoBack = new BitField(0x03E0); + /**/private static final BitField ipat = new BitField(0xFC00); protected SHD80AbstractType() { @@ -52,12 +52,19 @@ public abstract class SHD80AbstractType protected void fillFields( byte[] data, int offset ) { - field_1_value = LittleEndian.getShort(data, 0x0 + offset); + field_1_value = LittleEndian.getShort( data, 0x0 + offset ); } public void serialize( byte[] data, int offset ) { - LittleEndian.putShort(data, 0x0 + offset, (short)field_1_value); + LittleEndian.putShort( data, 0x0 + offset, field_1_value ); + } + + public byte[] serialize() + { + final byte[] result = new byte[ getSize() ]; + serialize( result, 0 ); + return result; } /** @@ -68,6 +75,30 @@ public abstract class SHD80AbstractType return 0 + 2; } + @Override + public boolean equals( Object obj ) + { + if ( this == obj ) + return true; + if ( obj == null ) + return false; + if ( getClass() != obj.getClass() ) + return false; + SHD80AbstractType other = (SHD80AbstractType) obj; + if ( field_1_value != other.field_1_value ) + return false; + return true; + } + + @Override + public int hashCode() + { + final int prime = 31; + int result = 1; + result = prime * result + field_1_value; + return result; + } + public String toString() { StringBuilder builder = new StringBuilder(); diff --git a/src/scratchpad/src/org/apache/poi/hwpf/model/types/SHDAbstractType.java b/src/scratchpad/src/org/apache/poi/hwpf/model/types/SHDAbstractType.java index 16b99e1c33..13603ae7b2 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/model/types/SHDAbstractType.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/model/types/SHDAbstractType.java @@ -14,6 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. ==================================================================== */ + package org.apache.poi.hwpf.model.types; @@ -22,9 +23,9 @@ import org.apache.poi.util.Internal; import org.apache.poi.util.LittleEndian; /** - * The SHD is a substructure of the CHP, PAP, and TC for Word 2000.

Class + * The Shd structure specifies the colors and pattern that are used for background shading.

Class and - fields descriptions are quoted from Microsoft Office Word 97-2007 Binary File Format + fields descriptions are quoted from Word (.doc) Binary File Format by Microsoft Corporation *

* NOTE: This source is automatically generated please do not modify this file. Either subclass or @@ -33,8 +34,7 @@ import org.apache.poi.util.LittleEndian; * This class is internal. It content or properties may change without notice * due to changes in our knowledge of internal Microsoft Word binary structures. - * @author Sergey Vladimirov; according to Microsoft Office Word 97-2007 Binary File Format - Specification [*.doc] + * @author Sergey Vladimirov; according to Word (.doc) Binary File Format by Microsoft Corporation. */ @Internal @@ -53,16 +53,23 @@ public abstract class SHDAbstractType protected void fillFields( byte[] data, int offset ) { - field_1_cvFore = new Colorref(data, 0x0 + offset); - field_2_cvBack = new Colorref(data, 0x4 + offset); - field_3_ipat = LittleEndian.getShort(data, 0x8 + offset); + field_1_cvFore = new Colorref( data, 0x0 + offset ); + field_2_cvBack = new Colorref( data, 0x4 + offset ); + field_3_ipat = LittleEndian.getShort( data, 0x8 + offset ); } public void serialize( byte[] data, int offset ) { - field_1_cvFore.serialize(data, 0x0 + offset); - field_2_cvBack.serialize(data, 0x4 + offset); - LittleEndian.putShort(data, 0x8 + offset, (short)field_3_ipat); + field_1_cvFore.serialize( data, 0x0 + offset ); + field_2_cvBack.serialize( data, 0x4 + offset ); + LittleEndian.putUShort( data, 0x8 + offset, field_3_ipat ); + } + + public byte[] serialize() + { + final byte[] result = new byte[ getSize() ]; + serialize( result, 0 ); + return result; } /** @@ -73,6 +80,36 @@ public abstract class SHDAbstractType return 0 + 4 + 4 + 2; } + @Override + public boolean equals( Object obj ) + { + if ( this == obj ) + return true; + if ( obj == null ) + return false; + if ( getClass() != obj.getClass() ) + return false; + SHDAbstractType other = (SHDAbstractType) obj; + if ( field_1_cvFore != other.field_1_cvFore ) + return false; + if ( field_2_cvBack != other.field_2_cvBack ) + return false; + if ( field_3_ipat != other.field_3_ipat ) + return false; + return true; + } + + @Override + public int hashCode() + { + final int prime = 31; + int result = 1; + result = prime * result + field_1_cvFore.hashCode(); + result = prime * result + field_2_cvBack.hashCode(); + result = prime * result + field_3_ipat; + return result; + } + public String toString() { StringBuilder builder = new StringBuilder(); @@ -89,7 +126,7 @@ public abstract class SHDAbstractType } /** - * 24-bit foreground color. + * A COLORREF that specifies the foreground color of ipat. */ @Internal public Colorref getCvFore() @@ -98,7 +135,7 @@ public abstract class SHDAbstractType } /** - * 24-bit foreground color. + * A COLORREF that specifies the foreground color of ipat. */ @Internal public void setCvFore( Colorref field_1_cvFore ) @@ -107,7 +144,7 @@ public abstract class SHDAbstractType } /** - * 24-bit background color. + * A COLORREF that specifies the background color of ipat. */ @Internal public Colorref getCvBack() @@ -116,7 +153,7 @@ public abstract class SHDAbstractType } /** - * 24-bit background color. + * A COLORREF that specifies the background color of ipat. */ @Internal public void setCvBack( Colorref field_2_cvBack ) @@ -125,7 +162,7 @@ public abstract class SHDAbstractType } /** - * Shading pattern. + * An Ipat that specifies the pattern used for shading. */ @Internal public int getIpat() @@ -134,7 +171,7 @@ public abstract class SHDAbstractType } /** - * Shading pattern. + * An Ipat that specifies the pattern used for shading. */ @Internal public void setIpat( int field_3_ipat ) diff --git a/src/scratchpad/src/org/apache/poi/hwpf/sprm/CharacterSprmUncompressor.java b/src/scratchpad/src/org/apache/poi/hwpf/sprm/CharacterSprmUncompressor.java index 0af5b79848..bc3b798d45 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/sprm/CharacterSprmUncompressor.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/sprm/CharacterSprmUncompressor.java @@ -17,6 +17,8 @@ package org.apache.poi.hwpf.sprm; +import org.apache.poi.hwpf.usermodel.ShadingDescriptor80; + import org.apache.poi.hwpf.model.Colorref; import org.apache.poi.hwpf.model.Hyphenation; import org.apache.poi.hwpf.usermodel.BorderCode; @@ -578,9 +580,20 @@ public final class CharacterSprmUncompressor extends SprmUncompressor case 0x65: newCHP.setBrc (new BorderCode(sprm.getGrpprl(), sprm.getGrpprlOffset())); break; - case 0x66: - newCHP.setShd (new ShadingDescriptor(sprm.getGrpprl(), sprm.getGrpprlOffset())); - break; + case 0x66: + // sprmCShd80 + /* + * "A Shd80 structure that specifies the background shading for the text. By default, text is not shaded." + * + * Word (.doc) Binary File Format. Copyright (c) 2011 Microsoft + * Corporation. Release: Tuesday, March 15, 2011 + */ + ShadingDescriptor80 oldDescriptor = new ShadingDescriptor80( + sprm.getGrpprl(), sprm.getGrpprlOffset() ); + ShadingDescriptor newDescriptor = oldDescriptor + .toShadingDescriptor(); + newCHP.setShd( newDescriptor ); + break; case 0x67: // Obsolete break; diff --git a/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestBugs.java b/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestBugs.java index 6d69f68b9d..29638fd837 100644 --- a/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestBugs.java +++ b/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestBugs.java @@ -765,4 +765,20 @@ public class TestBugs extends TestCase { HWPFTestDataSamples.openSampleFile( "Bug52032_3.doc" ); } + + /** + * Bug 53380 - ArrayIndexOutOfBounds Excetion parsing word 97 document + */ + public void testBug53380_1() throws Exception + { + HWPFTestDataSamples.openSampleFile( "Bug53380_1.doc" ); + } + + /** + * Bug 53380 - ArrayIndexOutOfBounds Excetion parsing word 97 document + */ + public void testBug53380_2() throws Exception + { + HWPFTestDataSamples.openSampleFile( "Bug53380_2.doc" ); + } } diff --git a/src/types/definitions/shd80_type.xml b/src/types/definitions/shd80_type.xml index ab3b45e4f4..22210ce7b0 100644 --- a/src/types/definitions/shd80_type.xml +++ b/src/types/definitions/shd80_type.xml @@ -19,17 +19,17 @@ --> AbstractType - The SHD80 is a substructure of the CHP and PAP, and TC for Word 97. <p>Class - and fields descriptions are quoted from - Microsoft Office Word 97-2007 Binary File Format + The Shd80 structure specifies the colors and pattern that are used for background + shading. As an exception to the constraints that are specified by Ico and Ipat, a Shd80 can + be set to Shd80Nil and specifies that no shading is applied. <p>Class and fields + descriptions are quoted from Word (.doc) Binary File Format by Microsoft Corporation - Sergey Vladimirov; according to Microsoft Office Word 97-2007 Binary File Format - Specification [*.doc] + Sergey Vladimirov; according to Word (.doc) Binary File Format by Microsoft Corporation. - - + + diff --git a/src/types/definitions/shd_type.xml b/src/types/definitions/shd_type.xml index 940eaa99ab..a2c1ba6247 100644 --- a/src/types/definitions/shd_type.xml +++ b/src/types/definitions/shd_type.xml @@ -19,16 +19,15 @@ --> AbstractType - The SHD is a substructure of the CHP, PAP, and TC for Word 2000. <p>Class + The Shd structure specifies the colors and pattern that are used for background shading. <p>Class and - fields descriptions are quoted from Microsoft Office Word 97-2007 Binary File Format + fields descriptions are quoted from Word (.doc) Binary File Format by Microsoft Corporation - Sergey Vladimirov; according to Microsoft Office Word 97-2007 Binary File Format - Specification [*.doc] + Sergey Vladimirov; according to Word (.doc) Binary File Format by Microsoft Corporation. - - - + + + diff --git a/test-data/document/Bug53380_1.doc b/test-data/document/Bug53380_1.doc new file mode 100644 index 0000000000..4233ac1e86 Binary files /dev/null and b/test-data/document/Bug53380_1.doc differ diff --git a/test-data/document/Bug53380_2.doc b/test-data/document/Bug53380_2.doc new file mode 100644 index 0000000000..1dd3daa184 Binary files /dev/null and b/test-data/document/Bug53380_2.doc differ