Bug 7213

Summary: [Patch] Header and Footer Records fix?
Product: POI Reporter: Shawn Laubach <shawn.laubach>
Component: HSSFAssignee: POI Developers List <dev>
Status: CLOSED FIXED    
Severity: normal    
Priority: P3    
Version: unspecified   
Target Milestone: ---   
Hardware: Other   
OS: other   

Description Shawn Laubach 2002-03-18 17:02:47 UTC
Index: src/java/org/apache/poi/hssf/record/FooterRecord.java
===================================================================
RCS file: /home/cvspublic/jakarta-
poi/src/java/org/apache/poi/hssf/record/FooterRecord.java,v
retrieving revision 1.4
diff -u -r1.4 FooterRecord.java
--- src/java/org/apache/poi/hssf/record/FooterRecord.java	1 Mar 2002 
13:10:15 -0000	1.4
+++ src/java/org/apache/poi/hssf/record/FooterRecord.java	18 Mar 2002 
17:00:17 -0000
@@ -63,6 +63,7 @@
  * Description:  Specifies the footer for a sheet<P>
  * REFERENCE:  PG 317 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)
<P>
  * @author Andrew C. Oliver (acoliver at apache dot org)
+ * @author Shawn Laubach (laubach@acm.org) Modified 3/14/02
  * @version 2.0-pre
  */
 
@@ -117,7 +118,7 @@
         if (size > 0)
         {
             field_1_footer_len = data[ 0 + offset ];
-            field_2_footer     = new String(data, 1 + offset,
+            field_2_footer     = new String(data, 3 + offset, // [Shawn] 
Changed 1 to 3 for offset of string
                                             LittleEndian.ubyteToInt( 
field_1_footer_len) );
         }
     }
@@ -153,9 +154,9 @@
      * @see #getFooter()
      */
 
-    public byte getFooterLength()
+    public short getFooterLength()
     {
-        return field_1_footer_len;
+        return (short)(0xFF & field_1_footer_len); // [Shawn] Fixed needing 
unsigned byte
     }
 
     /**
@@ -189,15 +190,15 @@
 
         if (getFooterLength() > 0)
         {
-            len++;
+            len+=3; // [Shawn] Fixed for two null bytes in the length
         }
         LittleEndian.putShort(data, 0 + offset, sid);
         LittleEndian.putShort(data, 2 + offset,
                               ( short ) ((len - 4) + getFooterLength()));
         if (getFooterLength() > 0)
         {
-            data[ 4 + offset ] = getFooterLength();
-            StringUtil.putCompressedUnicode(getFooter(), data, 5 + offset);
+            data[ 4 + offset ] = (byte)getFooterLength();
+            StringUtil.putCompressedUnicode(getFooter(), data, 7 + offset); // 
[Shawn] Place the string in the correct offset
         }
         return getRecordSize();
     }
@@ -208,7 +209,7 @@
 
         if (getFooterLength() > 0)
         {
-            retval++;
+            retval+=3; // [Shawn] Fixed for two null bytes in the length
         }
         return retval + getFooterLength();
     }
Index: src/java/org/apache/poi/hssf/record/HeaderRecord.java
===================================================================
RCS file: /home/cvspublic/jakarta-
poi/src/java/org/apache/poi/hssf/record/HeaderRecord.java,v
retrieving revision 1.4
diff -u -r1.4 HeaderRecord.java
--- src/java/org/apache/poi/hssf/record/HeaderRecord.java	1 Mar 2002 
13:10:15 -0000	1.4
+++ src/java/org/apache/poi/hssf/record/HeaderRecord.java	18 Mar 2002 
17:00:17 -0000
@@ -63,6 +63,7 @@
  * Description:  Specifies a header for a sheet<P>
  * REFERENCE:  PG 321 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)
<P>
  * @author Andrew C. Oliver (acoliver at apache dot org)
+ * @author Shawn Laubach (laubach@acm.org) Modified 3/14/02
  * @version 2.0-pre
  */
 
@@ -117,7 +118,7 @@
         if (size > 0)
         {
             field_1_header_len = data[ 0 + offset ];
-            field_2_header     = new String(data, 1 + offset,
+            field_2_header     = new String(data, 3 + offset, // [Shawn] 
Changed 1 to 3 for offset of string
                                             LittleEndian.ubyteToInt
(field_1_header_len));
         }
     }
@@ -153,9 +154,9 @@
      * @see #getHeader()
      */
 
-    public byte getHeaderLength()
+    public short getHeaderLength()
     {
-        return field_1_header_len;
+        return (short)(0xFF & field_1_header_len); // [Shawn] Fixed needing 
unsigned byte
     }
 
     /**
@@ -189,15 +190,16 @@
 
         if (getHeaderLength() != 0)
         {
-            len++;
+            len+=3; // [Shawn] Fixed for two null bytes in the length
         }
         LittleEndian.putShort(data, 0 + offset, sid);
         LittleEndian.putShort(data, 2 + offset,
                               ( short ) ((len - 4) + getHeaderLength()));
+
         if (getHeaderLength() > 0)
         {
-            data[ 4 + offset ] = getHeaderLength();
-            StringUtil.putCompressedUnicode(getHeader(), data, 5 + offset);
+            data[ 4 + offset ] = (byte)getHeaderLength();
+            StringUtil.putCompressedUnicode(getHeader(), data, 7 + offset); // 
[Shawn] Place the string in the correct offset
         }
         return getRecordSize();
     }
@@ -208,7 +210,7 @@
 
         if (getHeaderLength() != 0)
         {
-            retval++;
+            retval+=3; // [Shawn] Fixed for two null bytes in the length
         }
         retval += getHeaderLength();
         return retval;
Comment 1 Glen Stampoultzis 2002-04-06 11:17:58 UTC
Some context from poi mailing list:

Are you sure that the header and footer records are 100% accurate.  Whenever I 
run Biff Viewer and look at the records, it cuts the last two characters off.  
Also, the length for the strings seem to be 3 less than the length for the 
record.  1 byte for the length of the string, x number of bytes for the string, 
and 2 bytes for what.  In all the Excel files I looked at, the two bytes after 
the string length are nulls.  I'm needing to insert headers and footers so I 
wrote some code that updated the record and got some trailing gibberish.  After 
playing around, I made the record insert the two null bytes after the string 
length.  Updated all the places then can happen and everything seemed to work.  

If you think that this understanding of the record is valid, I'll send the 
patches for the code. 

BTW, the unsigned byte problem occurs in the headers and footers.  I fixed this 
also. 
Comment 2 Glen Stampoultzis 2002-04-08 14:06:38 UTC
Hrrm this bug seems to have been applied.

BTW, when summitting patches.  Attach them.  Avoids wrapping problems.
Comment 3 Glen Stampoultzis 2002-04-08 14:14:30 UTC
Confirmed that this patch has been applied, however no one has any memory of 
the event.  Closing.