Improved support for read-only recommended workbooks, fixing bug #44536

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@634619 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2008-03-07 11:36:14 +00:00
parent e2dd40f66c
commit 96198ae3d1
8 changed files with 64 additions and 2 deletions

View File

@ -36,6 +36,7 @@
<!-- Don't forget to update status.xml too! -->
<release version="3.1-beta1" date="2008-??-??">
<action dev="POI-DEVELOPERS" type="add">44536 - Improved support for detecting read-only recommended files</action>
<action dev="POI-DEVELOPERS" type="fix">43901 - Correctly update the internal last cell number when adding and removing cells (previously sometimes off-by-one)</action>
<action dev="POI-DEVELOPERS" type="fix">28231 - For apparently truncated files, which are somehow still valid, now issue a truncation warning but carry on, rather than giving an exception as before</action>
<action dev="POI-DEVELOPERS" type="fix">44504 - Added initial support for recognising external functions like YEARFRAC and ISEVEN (using NameXPtg), via LinkTable support</action>

View File

@ -33,6 +33,7 @@
<!-- Don't forget to update changes.xml too! -->
<changes>
<release version="3.1-beta1" date="2008-??-??">
<action dev="POI-DEVELOPERS" type="add">44536 - Improved support for detecting read-only recommended files</action>
<action dev="POI-DEVELOPERS" type="fix">43901 - Correctly update the internal last cell number when adding and removing cells (previously sometimes off-by-one)</action>
<action dev="POI-DEVELOPERS" type="fix">44504 - Added initial support for recognising external functions like YEARFRAC and ISEVEN (using NameXPtg), via LinkTable support</action>
<action dev="POI-DEVELOPERS" type="fix">44504 - Improvements to FormulaParser - operators, precedence, error literals, quotes in string literals, range checking on IntPtg, formulas with extra un-parsed stuff at the end, improved parse error handling</action>

View File

@ -2300,6 +2300,17 @@ public class Workbook implements Model
}
return this.fileShare;
}
/**
* is the workbook protected with a password (not encrypted)?
*/
public boolean isWriteProtected() {
if (this.fileShare == null) {
return false;
}
FileSharingRecord frec = getFileSharing();
return (frec.getReadOnly() == 1);
}
/**
* protect a workbook with a password (not encypted, just sets writeprotect

View File

@ -19,6 +19,8 @@
package org.apache.poi.hssf.record;
import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
import org.apache.poi.util.StringUtil;
/**
@ -29,6 +31,8 @@ import org.apache.poi.util.StringUtil;
*/
public class FileSharingRecord extends Record {
private static POILogger logger = POILogFactory.getLogger(FileSharingRecord.class);
public final static short sid = 0x5b;
private short field_1_readonly;
private short field_2_password;
@ -58,8 +62,23 @@ public class FileSharingRecord extends Record {
field_1_readonly = in.readShort();
field_2_password = in.readShort();
field_3_username_length = in.readByte();
// Is this really correct? The latest docs
// seem to hint there's nothing between the
// username length and the username string
field_4_unknown = in.readShort();
field_5_username = in.readCompressedUnicode(field_3_username_length);
// Ensure we don't try to read more data than
// there actually is
if(field_3_username_length > in.remaining()) {
logger.log(POILogger.WARN, "FileSharingRecord defined a username of length " + field_3_username_length + ", but only " + in.remaining() + " bytes were left, truncating");
field_3_username_length = (byte)in.remaining();
}
if(field_3_username_length > 0) {
field_5_username = in.readCompressedUnicode(field_3_username_length);
} else {
field_5_username = "";
}
}
//this is the world's lamest "security". thanks to Wouter van Vugt for making me

View File

@ -267,7 +267,7 @@ public class RecordInputStream extends InputStream
public String readCompressedUnicode(int length) {
if ((length < 0) || ((remaining() < length) && !isContinueNext())) {
throw new IllegalArgumentException("Illegal length");
throw new IllegalArgumentException("Illegal length " + length);
}
StringBuffer buf = new StringBuffer(length);

View File

@ -1379,6 +1379,13 @@ public class HSSFWorkbook extends POIDocument
}
}
/**
* Is the workbook protected with a password (not encrypted)?
*/
public boolean isWriteProtected() {
return this.workbook.isWriteProtected();
}
/**
* protect a workbook with a password (not encypted, just sets writeprotect
* flags and the password.

View File

@ -1104,6 +1104,29 @@ extends TestCase {
assertEquals(1, wb.getNumberOfSheets());
}
/**
* Files with "read only recommended" were giving
* grief on the FileSharingRecord
*/
public void test44536() throws Exception {
FileInputStream in = new FileInputStream(new File(cwd, "ReadOnlyRecommended.xls"));
// Used to blow up with an IllegalArgumentException
// when creating a FileSharingRecord
HSSFWorkbook wb = new HSSFWorkbook(in);
in.close();
// Check read only advised
assertEquals(3, wb.getNumberOfSheets());
assertTrue(wb.isWriteProtected());
// But also check that another wb isn't
in = new FileInputStream(new File(cwd, "SimpleWithChoose.xls"));
wb = new HSSFWorkbook(in);
in.close();
assertFalse(wb.isWriteProtected());
}
}