This is an automated email from the ASF dual-hosted git repository.
pjfanning pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/poi.git
The following commit(s) were added to refs/heads/trunk by this push:
new dbb9c4c34e FileSharingRecord: support UTF-16LE user names (bug 70292)
(#1505)
dbb9c4c34e is described below
commit dbb9c4c34e984623e34745add332c1192dc8b1b4
Author: PJ Fanning <[email protected]>
AuthorDate: Thu Oct 29 07:25:46 2026 +0000
FileSharingRecord: support UTF-16LE user names (bug 70292) (#1505)
The user name in FILESHARING is an XLUnicodeString, but POI ignored the
fHighByte flag and always read/wrote 8-bit characters. Write-protected
.xls files saved by Excel with a non-latin user name failed to open with
a LeftoverDataException, and writeProtectWorkbook() wrote broken records
for such names.
Based on the patch by JEONGHO attached to bug 70292, reworked to use the
existing StringUtil helpers and with the sample file added as a test.
Co-authored-by: Claude Opus 5.5 <[email protected]>
---
.../apache/poi/hssf/record/FileSharingRecord.java | 16 ++---
.../poi/hssf/record/TestFileSharingRecord.java | 78 +++++++++++++++++++++
.../org/apache/poi/hssf/usermodel/TestBugs.java | 30 ++++++++
test-data/spreadsheet/70292.xls | Bin 0 -> 4096 bytes
4 files changed, 115 insertions(+), 9 deletions(-)
diff --git
a/poi/src/main/java/org/apache/poi/hssf/record/FileSharingRecord.java
b/poi/src/main/java/org/apache/poi/hssf/record/FileSharingRecord.java
index a2aee4b43b..6932209f78 100644
--- a/poi/src/main/java/org/apache/poi/hssf/record/FileSharingRecord.java
+++ b/poi/src/main/java/org/apache/poi/hssf/record/FileSharingRecord.java
@@ -33,7 +33,6 @@ public final class FileSharingRecord extends StandardRecord {
public static final short sid = 0x005B;
private short field_1_readonly;
private short field_2_password;
- private byte field_3_username_unicode_options;
private String field_3_username_value;
public FileSharingRecord() {}
@@ -42,7 +41,6 @@ public final class FileSharingRecord extends StandardRecord {
super(other);
field_1_readonly = other.field_1_readonly;
field_2_password = other.field_2_password;
- field_3_username_unicode_options =
other.field_3_username_unicode_options;
field_3_username_value = other.field_3_username_value;
}
@@ -53,9 +51,11 @@ public final class FileSharingRecord extends StandardRecord {
int nameLen = in.readShort();
if(nameLen > 0) {
- // TODO - Current examples(3) from junits only have zero length
username.
- field_3_username_unicode_options = in.readByte();
- field_3_username_value = in.readCompressedUnicode(nameLen);
+ // stUNUsername is an XLUnicodeString: bit 0 of fHighByte set
means UTF-16LE characters
+ byte unicodeOptions = in.readByte();
+ field_3_username_value = (unicodeOptions & 0x01) == 0
+ ? in.readCompressedUnicode(nameLen)
+ : in.readUnicodeLEString(nameLen);
} else {
field_3_username_value = "";
}
@@ -110,13 +110,11 @@ public final class FileSharingRecord extends
StandardRecord {
public void serialize(LittleEndianOutput out) {
- // TODO - junit
out.writeShort(getReadOnly());
out.writeShort(getPassword());
out.writeShort(field_3_username_value.length());
if(field_3_username_value.length() > 0) {
- out.writeByte(field_3_username_unicode_options);
- StringUtil.putCompressedUnicode(getUsername(), out);
+ StringUtil.writeUnicodeStringFlagAndData(out, getUsername());
}
}
@@ -125,7 +123,7 @@ public final class FileSharingRecord extends StandardRecord
{
if (nameLen < 1) {
return 6;
}
- return 7+nameLen;
+ return 4 + StringUtil.getEncodedSize(field_3_username_value);
}
public short getSid() {
diff --git
a/poi/src/test/java/org/apache/poi/hssf/record/TestFileSharingRecord.java
b/poi/src/test/java/org/apache/poi/hssf/record/TestFileSharingRecord.java
new file mode 100644
index 0000000000..cf594c1c8a
--- /dev/null
+++ b/poi/src/test/java/org/apache/poi/hssf/record/TestFileSharingRecord.java
@@ -0,0 +1,78 @@
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+
+package org.apache.poi.hssf.record;
+
+import static
org.apache.poi.hssf.record.TestcaseRecordInputStream.confirmRecordEncoding;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import org.apache.poi.util.HexRead;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests for {@link FileSharingRecord}
+ */
+final class TestFileSharingRecord {
+
+ /** user name stored as 8-bit characters (fHighByte = 0): "acns" */
+ private static final byte[] DATA_COMPRESSED = HexRead.readFromString(
+ "00 00 6F CC 04 00 00 61 63 6E 73");
+
+ /** user name stored as UTF-16LE (fHighByte = 1): Korean "홍길동" */
+ private static final byte[] DATA_UNICODE = HexRead.readFromString(
+ "00 00 6F CC 03 00 01 4D D6 38 AE D9 B3");
+
+ /** zero length user name, no fHighByte */
+ private static final byte[] DATA_EMPTY = HexRead.readFromString(
+ "01 00 6F CC 00 00");
+
+ @Test
+ void compressedUsername() {
+ RecordInputStream in =
TestcaseRecordInputStream.create(FileSharingRecord.sid, DATA_COMPRESSED);
+ FileSharingRecord rec = new FileSharingRecord(in);
+ assertEquals(0, in.remaining());
+ assertEquals("acns", rec.getUsername());
+ confirmRecordEncoding(FileSharingRecord.sid, DATA_COMPRESSED,
rec.serialize());
+ }
+
+ @Test
+ void unicodeUsername() {
+ RecordInputStream in =
TestcaseRecordInputStream.create(FileSharingRecord.sid, DATA_UNICODE);
+ FileSharingRecord rec = new FileSharingRecord(in);
+ assertEquals(0, in.remaining());
+ assertEquals("홍길동", rec.getUsername());
+ confirmRecordEncoding(FileSharingRecord.sid, DATA_UNICODE,
rec.serialize());
+ }
+
+ @Test
+ void emptyUsername() {
+ RecordInputStream in =
TestcaseRecordInputStream.create(FileSharingRecord.sid, DATA_EMPTY);
+ FileSharingRecord rec = new FileSharingRecord(in);
+ assertEquals(0, in.remaining());
+ assertEquals(1, rec.getReadOnly());
+ assertEquals("", rec.getUsername());
+ confirmRecordEncoding(FileSharingRecord.sid, DATA_EMPTY,
rec.serialize());
+ }
+
+ @Test
+ void latin1UsernameStaysCompressed() {
+ FileSharingRecord rec = new FileSharingRecord();
+ rec.setUsername("José");
+ confirmRecordEncoding(FileSharingRecord.sid,
+ HexRead.readFromString("00 00 00 00 04 00 00 4A 6F 73 E9"),
rec.serialize());
+ }
+}
diff --git a/poi/src/test/java/org/apache/poi/hssf/usermodel/TestBugs.java
b/poi/src/test/java/org/apache/poi/hssf/usermodel/TestBugs.java
index c1f8d813b2..c7c39aa117 100644
--- a/poi/src/test/java/org/apache/poi/hssf/usermodel/TestBugs.java
+++ b/poi/src/test/java/org/apache/poi/hssf/usermodel/TestBugs.java
@@ -2645,4 +2645,34 @@ final class TestBugs extends BaseTestBugzillaIssues {
workbook.write(bos);
}
}
+
+ /**
+ * Write-protected file whose FileSharingRecord stores a non-latin
+ * user name as UTF-16LE. Used to fail with a LeftoverDataException.
+ */
+ @Test
+ void bug70292() throws IOException {
+ final String username = "\uD64D\uAE38\uB3D9";
+ try (HSSFWorkbook wb1 = openSampleWorkbook("70292.xls")) {
+ assertTrue(wb1.isWriteProtected(), "sample file should be write
protected");
+ assertEquals(username,
wb1.getInternalWorkbook().getFileSharing().getUsername());
+ try (HSSFWorkbook wb2 = writeOutAndReadBack(wb1)) {
+ assertTrue(wb2.isWriteProtected(), "workbook should still be
write protected after round trip");
+ assertEquals(username,
wb2.getInternalWorkbook().getFileSharing().getUsername());
+ }
+ }
+ }
+
+ @Test
+ void bug70292WriteProtectWithUnicodeUsername() throws IOException {
+ final String username = "\uD64D\uAE38\uB3D9";
+ try (HSSFWorkbook wb1 = new HSSFWorkbook()) {
+ wb1.createSheet();
+ wb1.writeProtectWorkbook("test", username);
+ try (HSSFWorkbook wb2 = writeOutAndReadBack(wb1)) {
+ assertTrue(wb2.isWriteProtected(), "workbook should still be
write protected after round trip");
+ assertEquals(username,
wb2.getInternalWorkbook().getFileSharing().getUsername());
+ }
+ }
+ }
}
diff --git a/test-data/spreadsheet/70292.xls b/test-data/spreadsheet/70292.xls
new file mode 100644
index 0000000000..8e53fd7c5a
Binary files /dev/null and b/test-data/spreadsheet/70292.xls differ
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]