Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/nissl-lab/npoi
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyqus committed Jul 1, 2024
2 parents 2a7e1b2 + 196b262 commit c36964d
Show file tree
Hide file tree
Showing 79 changed files with 2,266 additions and 1,179 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1">
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
Expand Down
2 changes: 1 addition & 1 deletion OpenXmlFormats/Wordprocessing/Numbering.cs
Original file line number Diff line number Diff line change
Expand Up @@ -971,7 +971,7 @@ public CT_AbstractNum()
this.multiLevelTypeField = new CT_MultiLevelType();
this.nsidField = new CT_LongHexNumber();
this.nsidField.val = new byte[4];
Array.Copy(BitConverter.GetBytes(DateTime.Now.Ticks), 4, this.nsidField.val, 0, 4);
Array.Copy(BitConverter.GetBytes(DateTime.Now.Ticks), 0, this.nsidField.val, 0, 4);
}

[XmlElement(Order = 0)]
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/NPOI.Benchmarks/NPOI.Benchmarks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.5" />
<PackageReference Include="BenchmarkDotNet" Version="0.13.12" />
</ItemGroup>

</Project>
5 changes: 4 additions & 1 deletion build/_build.csproj.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/PLACE_SIMPLE_ANONYMOUSMETHOD_ON_SINGLE_LINE/@EntryValue">False</s:Boolean>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateInstanceFields/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateStaticFields/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/UserRules/=4a98fdf6_002D7d98_002D4f5a_002Dafeb_002Dea44ad98c70c/@EntryIndexedValue">&lt;Policy&gt;&lt;Descriptor Staticness="Instance" AccessRightKinds="Private" Description="Instance fields (private)"&gt;&lt;ElementKinds&gt;&lt;Kind Name="FIELD" /&gt;&lt;Kind Name="READONLY_FIELD" /&gt;&lt;/ElementKinds&gt;&lt;/Descriptor&gt;&lt;Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /&gt;&lt;/Policy&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/UserRules/=f9fce829_002De6f4_002D4cb2_002D80f1_002D5497c44f51df/@EntryIndexedValue">&lt;Policy&gt;&lt;Descriptor Staticness="Static" AccessRightKinds="Private" Description="Static fields (private)"&gt;&lt;ElementKinds&gt;&lt;Kind Name="FIELD" /&gt;&lt;/ElementKinds&gt;&lt;/Descriptor&gt;&lt;Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /&gt;&lt;/Policy&gt;</s:String>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpAttributeForSingleLineMethodUpgrade/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpKeepExistingMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpPlaceEmbeddedOnSameLineMigration/@EntryIndexedValue">True</s:Boolean>
Expand All @@ -24,4 +26,5 @@
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EAddAccessorOwnerDeclarationBracesMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002ECSharpPlaceAttributeOnSameLineMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateBlankLinesAroundFieldToBlankLinesAroundProperty/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateThisQualifierSettings/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateThisQualifierSettings/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EPredefinedNamingRulesToUserRulesUpgrade/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
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
Loading

0 comments on commit c36964d

Please sign in to comment.