Skip to content

Commit

Permalink
Merge pull request #1463 from antony-liu/poi/v3.16-patch11
Browse files Browse the repository at this point in the history
Some patches from poi 3.16
  • Loading branch information
tonyqus authored Jan 15, 2025
2 parents 14e8133 + 6db15f2 commit 48dd6ca
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 71 deletions.
11 changes: 4 additions & 7 deletions main/HPSF/PropertySet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -336,9 +336,7 @@ public static bool IsPropertySetStream(Stream stream)
/// <c>true</c> if the byte array is a property Set
/// stream; otherwise, <c>false</c>.
/// </returns>
public static bool IsPropertySetStream(byte[] src,
int offset,
int Length)
public static bool IsPropertySetStream(byte[] src, int offset, int Length)
{
/* FIXME (3): Ensure that at most "Length" bytes are Read. */

Expand All @@ -364,10 +362,9 @@ public static bool IsPropertySetStream(byte[] src,
ClassID classID = new ClassID(src, offset);
o += ClassID.LENGTH;
long sectionCount = LittleEndian.GetUInt(src, o);
o += LittleEndianConsts.INT_SIZE;
if (sectionCount < 0)
return false;
return true;
//o += LittleEndianConsts.INT_SIZE;

return sectionCount >= 0;
}


Expand Down
6 changes: 4 additions & 2 deletions main/HSSF/Record/CFRule12Record.cs
Original file line number Diff line number Diff line change
Expand Up @@ -445,9 +445,11 @@ public override Object Clone() {

base.CopyTo(rec);

rec.ext_formatting_length = ext_formatting_length;
// use min() to gracefully handle cases where the length-property and the array-lenght do not match
// we saw some such files in circulation
rec.ext_formatting_length = Math.Min(ext_formatting_length, ext_formatting_data.Length);
rec.ext_formatting_data = new byte[ext_formatting_length];
Array.Copy(ext_formatting_data, 0, rec.ext_formatting_data, 0, ext_formatting_length);
Array.Copy(ext_formatting_data, 0, rec.ext_formatting_data, 0, rec.ext_formatting_length);

rec.formula_scale = formula_scale.Copy();

Expand Down
32 changes: 16 additions & 16 deletions main/HSSF/Record/ObjRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ namespace NPOI.HSSF.Record
public class ObjRecord : Record, ICloneable
{
private const int NORMAL_PAD_ALIGNMENT = 2;
private const int MAX_PAD_ALIGNMENT = 4;
private const int MAX_PAD_ALIGNMENT = 4;

public const short sid = 0x5D;
private List<SubRecord> subrecords;
/** used when POI has no idea what is going on */
private byte[] _uninterpretedData;
/**
* Excel seems to tolerate padding to quad or double byte length
*/
/** used when POI has no idea what is going on */
private byte[] _uninterpretedData;
/**
* Excel seems to tolerate padding to quad or double byte length
*/
private bool _isPaddedToQuadByteMultiple;

//00000000 15 00 12 00 01 00 01 00 11 60 00 00 00 00 00 0D .........`......
Expand Down Expand Up @@ -130,14 +130,14 @@ public ObjRecord(RecordInputStream in1)
}
}
/**
* Some XLS files have ObjRecords with nearly 8Kb of excessive padding. These were probably
* written by a version of POI (around 3.1) which incorrectly interpreted the second short of
* the ftLbs subrecord (0x1FEE) as a length, and read that many bytes as padding (other bugs
* helped allow this to occur).
*
* Excel reads files with this excessive padding OK, truncating the over-sized ObjRecord back
* to the its proper size. POI does the same.
*/
* Some XLS files have ObjRecords with nearly 8Kb of excessive padding. These were probably
* written by a version of POI (around 3.1) which incorrectly interpreted the second short of
* the ftLbs subrecord (0x1FEE) as a length, and read that many bytes as padding (other bugs
* helped allow this to occur).
*
* Excel reads files with this excessive padding OK, truncating the over-sized ObjRecord back
* to the its proper size. POI does the same.
*/
private static bool CanPaddingBeDiscarded(byte[] data, int nRemainingBytes)
{
// make sure none of the padding looks important
Expand Down Expand Up @@ -172,8 +172,8 @@ public override int Serialize(int offset, byte [] data)

LittleEndianByteArrayOutputStream out1 = new LittleEndianByteArrayOutputStream(data, offset, recSize);

out1.WriteShort(sid);
out1.WriteShort(dataSize);
out1.WriteShort(sid);
out1.WriteShort(dataSize);

if (_uninterpretedData == null)
{
Expand Down
14 changes: 7 additions & 7 deletions main/HSSF/Record/SubRecord/SubRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ public byte[] Serialize()
}
}
/**
* Wether this record terminates the sub-record stream.
* There are two cases when this method must be overridden and return <c>true</c>
* - EndSubRecord (sid = 0x00)
* - LbsDataSubRecord (sid = 0x12)
*
* @return whether this record is the last in the sub-record stream
*/
* Wether this record terminates the sub-record stream.
* There are two cases when this method must be overridden and return <c>true</c>
* - EndSubRecord (sid = 0x00)
* - LbsDataSubRecord (sid = 0x12)
*
* @return whether this record is the last in the sub-record stream
*/
public virtual bool IsTerminating
{
get
Expand Down
8 changes: 3 additions & 5 deletions main/HSSF/UserModel/HSSFCell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1052,14 +1052,12 @@ public byte ErrorCellValue
case CellType.Error:
return ((BoolErrRecord)_record).ErrorValue;
case CellType.Formula:
break;
FormulaRecord fr = ((FormulaRecordAggregate)_record).FormulaRecord;
CheckFormulaCachedValueType(CellType.Error, fr);
return (byte) fr.CachedErrorValue;
default:
throw TypeMismatch(CellType.Error, cellType, false);

}
FormulaRecord fr = ((FormulaRecordAggregate)_record).FormulaRecord;
CheckFormulaCachedValueType(CellType.Error, fr);
return (byte)fr.CachedErrorValue;
}
}

Expand Down
11 changes: 10 additions & 1 deletion main/POIFS/FileSystem/NDocumentInputStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,15 @@ public NDocumentInputStream(NPOIFSDocument document)


public override int Available()
{
return RemainingBytes();
}
/**
* Helper methods for forbidden api calls
*
* @return the bytes remaining until the end of the stream
*/
private int RemainingBytes()
{
if (_closed)
{
Expand Down Expand Up @@ -159,7 +168,7 @@ public override int Read(byte[] b, int off, int len)
{
return EOF;
}
int limit = Math.Min(Available(), len);
int limit = Math.Min(RemainingBytes(), len);
ReadFully(b, off, limit);
return limit;
}
Expand Down
4 changes: 2 additions & 2 deletions main/SS/Formula/Functions/WeekNum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class WeekNum : Fixed2ArgFunction, FreeRefFunction

public override ValueEval Evaluate(int srcRowIndex, int srcColumnIndex, ValueEval serialNumVE, ValueEval returnTypeVE)
{
double serialNum = 0.0;
double serialNum;
try
{
serialNum = NumericFunction.SingleOperandEvaluate(serialNumVE, srcRowIndex, srcColumnIndex);
Expand All @@ -71,7 +71,7 @@ public override ValueEval Evaluate(int srcRowIndex, int srcColumnIndex, ValueEva
{
return ErrorEval.NUM_ERROR;
}
int returnType = 0;
int returnType;
try
{
ValueEval ve = OperandResolver.GetSingleValue(returnTypeVE, srcRowIndex, srcColumnIndex);
Expand Down
10 changes: 4 additions & 6 deletions openxml4Net/OPC/PackageRelationshipCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public class PackageRelationshipCollection : IEnumerator<PackageRelationship>
private SortedList<String, PackageRelationship> relationshipsByID;

/**
* A lookup of internal relationships to avoid
*/
* A lookup of internal relationships to avoid
*/
private SortedList<String, PackageRelationship> internalRelationshipsByTargetName;

/**
Expand Down Expand Up @@ -146,7 +146,7 @@ public PackageRelationshipCollection(OPCPackage container, PackagePart part)


if (container == null)
throw new ArgumentException("container");
throw new ArgumentException("container needs to be specified");

// Check if the specified part is not a relationship part
if (part != null && part.IsRelationshipPart)
Expand Down Expand Up @@ -420,9 +420,7 @@ private void ParseRelationshipsPart(PackagePart relPart)
*/
public PackageRelationshipCollection GetRelationships(String typeFilter)
{
PackageRelationshipCollection coll = new PackageRelationshipCollection(
this, typeFilter);
return coll;
return new PackageRelationshipCollection(this, typeFilter);
}

/**
Expand Down
7 changes: 0 additions & 7 deletions testcases/main/Util/TestLittleEndian.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,6 @@ public void TestGetUShort()
Assert.AreEqual(testdata3[ 2 ], unchecked((byte)0xFF));
Assert.AreEqual(expected[ 2 ], LittleEndian.GetUShort(testdata3));
Assert.AreEqual(expected[ 3 ], LittleEndian.GetUShort(testdata3, 1));
//System.out.println("TD[1][0]: "+LittleEndian.GetUShort(testdata)+" expecting 65281");
//System.out.println("TD[1][1]: "+LittleEndian.GetUShort(testdata, 1)+" expecting 767");
//System.out.println("TD[2][0]: "+LittleEndian.GetUShort(testdata2)+" expecting 37645");
//System.out.println("TD[2][1]: "+LittleEndian.GetUShort(testdata2, 1)+" expecting 65427");
//System.out.println("TD[3][0]: "+LittleEndian.GetUShort(testdata3)+" expecting 37645");
//System.out.println("TD[3][1]: "+LittleEndian.GetUShort(testdata3, 1)+" expecting 65427");

}

private static byte[] _DOUBLE_array =
Expand Down
11 changes: 0 additions & 11 deletions testcases/ooxml/XSSF/TestXSSFCloneSheet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,6 @@ public void Test60512()
IWorkbook wbBack = XSSFTestDataSamples.WriteOutAndReadBack(wb);
Assert.IsNotNull(wbBack);
wbBack.Close();
FileInfo fi = TempFile.CreateTempFile("60512", ".xlsx");
//Stream str = new FileStream("/tmp/60512.xlsx", FileMode.Create, FileAccess.Write);
Stream str = new FileStream(fi.FullName, FileMode.Create, FileAccess.Write);
try
{
wb.Write(str);
}
finally
{
str.Close();
}

wb.Close();
}
Expand Down
2 changes: 2 additions & 0 deletions testcases/ooxml/XSSF/UserModel/TestXSSFDrawing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,7 @@ public void TestAddBulletParagraphs()
String paraString10 = "Fifth Bullet";

XSSFTextParagraph para = shape.AddNewTextParagraph(paraString1);
Assert.IsNotNull(para);
para = shape.AddNewTextParagraph(paraString2);
para.SetBullet(true);

Expand All @@ -658,6 +659,7 @@ public void TestAddBulletParagraphs()
para.SetBullet(true);

para = shape.AddNewTextParagraph(paraString5);
Assert.IsNotNull(para);
para = shape.AddNewTextParagraph(paraString6);
para.SetBullet(ListAutoNumber.ARABIC_PERIOD);

Expand Down
16 changes: 9 additions & 7 deletions testcases/openxml4net/TestZipPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,23 @@ public void TestBug56479()

// Check we found the contents of it
bool foundCoreProps = false, foundDocument = false, foundTheme1 = false;
foreach (PackagePart part in p.GetParts())
foreach(PackagePart part in p.GetParts())
{
if (part.PartName.ToString().Equals("/docProps/core.xml"))
String partName = part.PartName.ToString();
String contentType = part.ContentType;
if("/docProps/core.xml".Equals(partName))
{
Assert.AreEqual(ContentTypes.CORE_PROPERTIES_PART, part.ContentType);
Assert.AreEqual(ContentTypes.CORE_PROPERTIES_PART, contentType);
foundCoreProps = true;
}
if (part.PartName.ToString().Equals("/word/document.xml"))
if("/word/document.xml".Equals(partName))
{
Assert.AreEqual(XWPFRelation.DOCUMENT.ContentType, part.ContentType);
Assert.AreEqual(XWPFRelation.DOCUMENT.ContentType, contentType);
foundDocument = true;
}
if (part.PartName.ToString().Equals("/word/theme/theme1.xml"))
if("/word/theme/theme1.xml".Equals(partName))
{
Assert.AreEqual(XWPFRelation.THEME.ContentType, part.ContentType);
Assert.AreEqual(XWPFRelation.THEME.ContentType, contentType);
foundTheme1 = true;
}
}
Expand Down

0 comments on commit 48dd6ca

Please sign in to comment.