Skip to content

Commit

Permalink
Merge pull request #1354 from antony-liu/poi/v3.16-patch6
Browse files Browse the repository at this point in the history
Some patches ported from poi
  • Loading branch information
tonyqus committed Jun 18, 2024
2 parents f42c080 + 3e3c2f9 commit 75635b1
Show file tree
Hide file tree
Showing 36 changed files with 1,038 additions and 947 deletions.
85 changes: 37 additions & 48 deletions main/DDF/EscherArrayProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,57 +76,41 @@ public int NumberOfElementsInArray
{
return 0;
}
return LittleEndian.GetUShort(_complexData, 0);
return LittleEndian.GetUShort(ComplexData, 0);
}
set
{
int expectedArraySize = value * GetActualSizeOfElements(SizeOfElements) + FIXED_SIZE;
if (expectedArraySize != _complexData.Length)
{
byte[] newArray = new byte[expectedArraySize];
Array.Copy(_complexData, 0, newArray, 0, _complexData.Length);
_complexData = newArray;
}
LittleEndian.PutShort(_complexData, 0, (short)value);
int expectedArraySize = GetArraySizeInBytes(value, SizeOfElements);
ResizeComplexData(expectedArraySize, ComplexData.Length);
LittleEndian.PutShort(ComplexData, 0, (short)value);
}
}
public int NumberOfElementsInMemory
{
get
{
return LittleEndian.GetUShort(_complexData, 2);
return LittleEndian.GetUShort(ComplexData, 2);
}
set
{
int expectedArraySize = value * GetActualSizeOfElements(this.SizeOfElements) + FIXED_SIZE;
if (expectedArraySize != _complexData.Length)
{
byte[] newArray = new byte[expectedArraySize];
Array.Copy(_complexData, 0, newArray, 0, expectedArraySize);
_complexData = newArray;
}
LittleEndian.PutShort(_complexData, 2, (short)value);
int expectedArraySize = GetArraySizeInBytes(value, SizeOfElements);
ResizeComplexData(expectedArraySize, expectedArraySize);
LittleEndian.PutShort(ComplexData, 2, (short)value);
}
}

public short SizeOfElements
{
get
{
return LittleEndian.GetShort(_complexData, 4);
return (emptyComplexPart) ? (short)0 : LittleEndian.GetShort(ComplexData, 4);
}
set
{
LittleEndian.PutShort(_complexData, 4, (short)value);

int expectedArraySize = NumberOfElementsInArray * GetActualSizeOfElements(SizeOfElements) + FIXED_SIZE;
if (expectedArraySize != _complexData.Length)
{
// Keep just the first 6 bytes. The rest is no good to us anyway.
byte[] newArray = new byte[expectedArraySize];
Array.Copy(_complexData, 0, newArray, 0, 6);
_complexData = newArray;
}
LittleEndian.PutShort(ComplexData, 4, (short)value);
int expectedArraySize = GetArraySizeInBytes(NumberOfElementsInArray, value);
// Keep just the first 6 bytes. The rest is no good to us anyway.
ResizeComplexData(expectedArraySize, 6);
}
}

Expand All @@ -139,7 +123,7 @@ public byte[] GetElement(int index)
{
int actualSize = GetActualSizeOfElements(SizeOfElements);
byte[] result = new byte[actualSize];
Array.Copy(_complexData, FIXED_SIZE + index * actualSize, result, 0, result.Length);
Array.Copy(ComplexData, FIXED_SIZE + index * actualSize, result, 0, result.Length);
return result;
}

Expand All @@ -151,7 +135,7 @@ public byte[] GetElement(int index)
public void SetElement(int index, byte[] element)
{
int actualSize = GetActualSizeOfElements(SizeOfElements);
Array.Copy(element, 0, _complexData, FIXED_SIZE + index * actualSize, actualSize);
Array.Copy(element, 0, ComplexData, FIXED_SIZE + index * actualSize, actualSize);
}

/// <summary>
Expand Down Expand Up @@ -205,24 +189,26 @@ public int SetArrayData(byte[] data, int offset)
{
if (emptyComplexPart)
{
_complexData = new byte[0];
ResizeComplexData(0, 0);
return 0;
}
else
{
short numElements = LittleEndian.GetShort(data, offset);
//short numReserved = LittleEndian.GetShort(data, offset + 2); // numReserved
short sizeOfElements = LittleEndian.GetShort(data, offset + 4);

int arraySize = GetActualSizeOfElements(sizeOfElements) * numElements;
if (arraySize == _complexData.Length)
{
// The stored data size in the simple block excludes the header size
_complexData = new byte[arraySize + 6];
sizeIncludesHeaderSize = false;
}
Array.Copy(data, offset, _complexData, 0, _complexData.Length);
short numElements = LittleEndian.GetShort(data, offset);
// LittleEndian.getShort(data, offset + 2); // numReserved
short sizeOfElements = LittleEndian.GetShort(data, offset + 4);

// TODO: this part is strange - it doesn't make sense to compare
// the size of the existing data when setting a new data array ...
int arraySize = GetArraySizeInBytes(numElements, sizeOfElements);
if (arraySize - FIXED_SIZE == ComplexData.Length)
{
// The stored data size in the simple block excludes the header size
sizeIncludesHeaderSize = false;
}
return _complexData.Length;
int cpySize = Math.Min(arraySize, data.Length - offset);
ResizeComplexData(cpySize, 0);
Array.Copy(data, offset, ComplexData, 0, cpySize);
return cpySize;
}

/// <summary>
Expand All @@ -236,7 +222,7 @@ public int SetArrayData(byte[] data, int offset)
public override int SerializeSimplePart(byte[] data, int pos)
{
LittleEndian.PutShort(data, pos, Id);
int recordSize = _complexData.Length;
int recordSize = ComplexData.Length;
if (!sizeIncludesHeaderSize)
{
recordSize -= 6;
Expand All @@ -258,7 +244,10 @@ public static int GetActualSizeOfElements(short sizeOfElements)
else
return sizeOfElements;
}

private static int GetArraySizeInBytes(int numberOfElements, int sizeOfElements)
{
return numberOfElements * GetActualSizeOfElements((short)(sizeOfElements & 0xFFFF)) + FIXED_SIZE;
}

public IEnumerator<byte[]> GetEnumerator()
{
Expand Down
14 changes: 7 additions & 7 deletions main/DDF/EscherBitmapBlip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ public override int FillFields(byte[] data, int offset, IEscherRecordFactory rec
Array.Copy(data, pos, field_1_UID, 0, 16); pos += 16;
field_2_marker = data[pos]; pos++;

field_pictureData = new byte[bytesAfterHeader - 17];
Array.Copy(data, pos, field_pictureData, 0, field_pictureData.Length);
SetPictureData(data, pos, bytesAfterHeader - 17);

return bytesAfterHeader + HEADER_SIZE;
}
Expand All @@ -79,10 +78,11 @@ public override int Serialize(int offset, byte[] data, EscherSerializationListen

Array.Copy(field_1_UID, 0, data, pos, 16);
data[pos + 16] = field_2_marker;
Array.Copy(field_pictureData, 0, data, pos + 17, field_pictureData.Length);
byte[] pd = PictureData;
Array.Copy(pd, 0, data, pos + 17, pd.Length);

listener.AfterRecordSerialize(offset + RecordSize, RecordId, RecordSize, this);
return HEADER_SIZE + 16 + 1 + field_pictureData.Length;
return HEADER_SIZE + 16 + 1 + pd.Length;
}

/// <summary>
Expand All @@ -91,7 +91,7 @@ public override int Serialize(int offset, byte[] data, EscherSerializationListen
/// <value> Number of bytes</value>
public override int RecordSize
{
get { return 8 + 16 + 1 + field_pictureData.Length; }
get { return 8 + 16 + 1 + PictureData.Length; }
}

/// <summary>
Expand Down Expand Up @@ -129,7 +129,7 @@ public override String ToString()
{
String nl = Environment.NewLine;

String extraData = HexDump.Dump(this.field_pictureData, 0, 0);
String extraData = HexDump.Dump(this.PictureData, 0, 0);
return this.GetType().Name + ":" + nl +
" RecordId: 0x" + HexDump.ToHex(RecordId) + nl +
" Version: 0x" + HexDump.ToHex(Version) + nl +
Expand All @@ -141,7 +141,7 @@ public override String ToString()

public override String ToXml(String tab)
{
String extraData = HexDump.ToHex(this.field_pictureData);
String extraData = HexDump.ToHex(this.PictureData);

StringBuilder builder = new StringBuilder();
builder.Append(tab).Append(FormatXmlRecordHeader(GetType().Name, HexDump.ToHex(RecordId), HexDump.ToHex(Version), HexDump.ToHex(Instance)))
Expand Down
35 changes: 23 additions & 12 deletions main/DDF/EscherBlipRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class EscherBlipRecord : EscherRecord

private const int HEADER_SIZE = 8;

protected byte[] field_pictureData;
private byte[] field_pictureData;

public EscherBlipRecord()
{
Expand Down Expand Up @@ -106,20 +106,31 @@ public byte[] PictureData
get { return field_pictureData; }
set
{
if (value == null)
{
throw new ArgumentNullException("picture data can't be null");
}
else
{
field_pictureData = new byte[value.Length];
if (value.Length > 0)
Array.Copy(value, field_pictureData, value.Length);
}
SetPictureData(value, 0, (value == null ? 0 : value.Length));
}
}


/// <summary>
/// Sets the picture data bytes
/// </summary>
/// <param name="pictureData">the picture data</param>
/// <param name="offset">the offset into the picture data</param>
/// <param name="length">the amount of bytes to be used</param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentOutOfRangeException"></exception>
public void SetPictureData(byte[] pictureData, int offset, int length)
{
if (pictureData == null)
{
throw new ArgumentNullException("picture data can't be null");
}
if (offset < 0 || length < 0 || pictureData.Length < offset + length)
{
throw new ArgumentOutOfRangeException("picture data, offset, length were out of range");
}
field_pictureData = new byte[length];
Array.Copy(pictureData, offset, field_pictureData, 0, length);
}
/// <summary>
/// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions main/DDF/EscherBoolProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public EscherBoolProperty(short propertyNumber, int value)
/// <value><c>true</c> if this instance is true; otherwise, <c>false</c>.</value>
public bool IsTrue
{
get { return propertyValue != 0; }
get { return PropertyValue != 0; }
}

/// <summary>
Expand All @@ -56,7 +56,7 @@ public bool IsTrue
/// <value><c>true</c> if this instance is false; otherwise, <c>false</c>.</value>
public bool IsFalse
{
get { return propertyValue == 0; }
get { return !IsTrue; }
}

//public override String ToString()
Expand Down
2 changes: 2 additions & 0 deletions main/DDF/EscherColorRef.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ namespace NPOI.DDF
using System.Diagnostics;
public class SysIndexSource
{
private int opid = -1;
private int colorRef = 0;
internal static SysIndexSource[] Values()
{
return new SysIndexSource[] {
Expand Down
13 changes: 12 additions & 1 deletion main/DDF/EscherComplexProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace NPOI.DDF
/// </summary>
public class EscherComplexProperty : EscherProperty
{
protected byte[] _complexData = new byte[0];
private byte[] _complexData = new byte[0];

/// <summary>
/// Create a complex property using the property id and a byte array containing the complex
Expand Down Expand Up @@ -102,6 +102,17 @@ public byte[] ComplexData
get { return _complexData; }
}

protected void ResizeComplexData(int newSize, int bytesToKeep)
{
if (newSize == _complexData.Length)
{
return;
}
byte[] newArray = new byte[newSize];
Array.Copy(_complexData, 0, newArray, 0, Math.Min(bytesToKeep, newSize));
_complexData = newArray;
}

/// <summary>
/// Determine whether this property is equal to another property.
/// </summary>
Expand Down
49 changes: 19 additions & 30 deletions main/DDF/EscherMetafileBlip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ public override int FillFields( byte[] data, int offset, IEscherRecordFactory re
// 0xFE means no compression
if (field_6_fCompression == 0)
{
field_pictureData = InflatePictureData(raw_pictureData);
PictureData = InflatePictureData(raw_pictureData);
}
else
{
field_pictureData = raw_pictureData;
PictureData = raw_pictureData;
}
int remaining = bytesAfterHeader - pos + offset + HEADER_SIZE;
if (remaining > 0)
Expand Down Expand Up @@ -333,34 +333,23 @@ public override String ToString()
{
String nl = Environment.NewLine;

String extraData;
using (MemoryStream b = new MemoryStream())
{
try
{
HexDump.Dump(this.field_pictureData, 0, b, 0);
extraData = b.ToString();
}
catch (Exception e)
{
extraData = e.ToString();
}
return GetType().Name + ":" + nl +
" RecordId: 0x" + HexDump.ToHex(RecordId) + nl +
" Version: 0x" + HexDump.ToHex(Version) + '\n' +
" Instance: 0x" + HexDump.ToHex(Instance) + '\n' +
" UID: 0x" + HexDump.ToHex(field_1_UID) + nl +
(field_2_UID == null ? "" : (" UID2: 0x" + HexDump.ToHex(field_2_UID) + nl)) +
" Uncompressed Size: " + HexDump.ToHex(field_2_cb) + nl +
" Bounds: " + Bounds + nl +
" Size in EMU: " + SizeEMU + nl +
" Compressed Size: " + HexDump.ToHex(field_5_cbSave) + nl +
" Compression: " + HexDump.ToHex(field_6_fCompression) + nl +
" Filter: " + HexDump.ToHex(field_7_fFilter) + nl +
" Extra Data:" + nl + extraData +
(remainingData == null ? null : ("\n" +
" Remaining Data: " + HexDump.ToHex(remainingData, 32)));
}
String extraData = "";//HexDump.toHex(field_pictureData, 32);
return GetType().Name + ":" + nl +
" RecordId: 0x" + HexDump.ToHex(RecordId) + nl +
" Version: 0x" + HexDump.ToHex(Version) + '\n' +
" Instance: 0x" + HexDump.ToHex(Instance) + '\n' +
" UID: 0x" + HexDump.ToHex(field_1_UID) + nl +
(field_2_UID == null ? "" : (" UID2: 0x" + HexDump.ToHex(field_2_UID) + nl)) +
" Uncompressed Size: " + HexDump.ToHex(field_2_cb) + nl +
" Bounds: " + Bounds + nl +
" Size in EMU: " + SizeEMU + nl +
" Compressed Size: " + HexDump.ToHex(field_5_cbSave) + nl +
" Compression: " + HexDump.ToHex(field_6_fCompression) + nl +
" Filter: " + HexDump.ToHex(field_7_fFilter) + nl +
" Extra Data:" + nl + extraData +
(remainingData == null ? null : ("\n" +
" Remaining Data: " + HexDump.ToHex(remainingData, 32)));

}
public override String ToXml(String tab)
{
Expand Down
2 changes: 1 addition & 1 deletion main/DDF/EscherOptRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public override short Instance
{
get
{
Instance = ((short)properties.Count);
Instance = ((short)EscherProperties.Count);
return base.Instance;
}
}
Expand Down
Loading

0 comments on commit 75635b1

Please sign in to comment.