Skip to content

Commit

Permalink
Further code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
gus33000 committed Oct 5, 2024
1 parent 8b259e7 commit 6dd87ff
Show file tree
Hide file tree
Showing 17 changed files with 104 additions and 99 deletions.
12 changes: 6 additions & 6 deletions Img2Ffu.Library/Reader/Data/ImageFlash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public ImageFlash(Stream stream)
if (ImageHeader.Size == 28u)
{
byte[] DeviceTargetingInfoCountBuffer = new byte[4];
_ = stream.Read(DeviceTargetingInfoCountBuffer, 0, 4);
_ = stream.Read(DeviceTargetingInfoCountBuffer);

DeviceTargetingInfoCount = BitConverter.ToUInt32(DeviceTargetingInfoCountBuffer);
}
Expand All @@ -63,7 +63,7 @@ public ImageFlash(Stream stream)
_ = stream.Seek(InitialStreamPosition + ImageHeader.Size, SeekOrigin.Begin);

byte[] manifestDataBuffer = new byte[ImageHeader.ManifestLength];
_ = stream.Read(manifestDataBuffer, 0, (int)ImageHeader.ManifestLength);
_ = stream.Read(manifestDataBuffer);
ASCIIEncoding asciiEncoding = new();
ManifestData = asciiEncoding.GetString(manifestDataBuffer);

Expand All @@ -77,7 +77,7 @@ public ImageFlash(Stream stream)
{
long paddingSize = (ImageHeader.ChunkSize * 1024) - (Position % (ImageHeader.ChunkSize * 1024));
Padding = new byte[paddingSize];
_ = stream.Read(Padding, 0, (int)paddingSize);
_ = stream.Read(Padding);
}

Store store = new(stream);
Expand Down Expand Up @@ -113,7 +113,7 @@ public byte[] GetImageBlock(Stream Stream, ulong dataBlockIndex)
ulong dataBlockPosition = (ulong)InitialStreamPosition + (dataBlockIndex * (ImageHeader.ChunkSize * 1024));

_ = Stream.Seek((long)dataBlockPosition, SeekOrigin.Begin);
_ = Stream.Read(dataBlock, 0, dataBlock.Length);
_ = Stream.Read(dataBlock);

return dataBlock;
}
Expand Down Expand Up @@ -200,7 +200,7 @@ public byte[] GetStoreDataBlock(Stream Stream, ulong storeIndex, ulong dataBlock
byte[] compressedDataBlock = new byte[compressedDataBlockSize];

_ = Stream.Seek((long)dataBlockPosition, SeekOrigin.Begin);
_ = Stream.Read(compressedDataBlock, 0, compressedDataBlock.Length);
_ = Stream.Read(compressedDataBlock);

switch ((CompressionAlgorithm)currentStore.CompressionAlgorithm)
{
Expand Down Expand Up @@ -231,7 +231,7 @@ public byte[] GetStoreDataBlock(Stream Stream, ulong storeIndex, ulong dataBlock
dataBlockPosition += dataBlockIndex * currentStore.StoreHeader.BlockSize;

_ = Stream.Seek((long)dataBlockPosition, SeekOrigin.Begin);
_ = Stream.Read(dataBlock, 0, dataBlock.Length);
_ = Stream.Read(dataBlock);
}

return dataBlock;
Expand Down
6 changes: 3 additions & 3 deletions Img2Ffu.Library/Reader/Data/SignedImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,18 @@ public SignedImage(Stream stream)
}

Catalog = new byte[SecurityHeader.CatalogSize];
_ = stream.Read(Catalog, 0, (int)SecurityHeader.CatalogSize);
_ = stream.Read(Catalog);

TableOfHashes = new byte[SecurityHeader.HashTableSize];
_ = stream.Read(TableOfHashes, 0, (int)SecurityHeader.HashTableSize);
_ = stream.Read(TableOfHashes);

long Position = stream.Position;
uint sizeOfBlock = SecurityHeader.ChunkSizeInKB * 1024;
if (Position % sizeOfBlock > 0)
{
long paddingSize = sizeOfBlock - (Position % sizeOfBlock);
Padding = new byte[paddingSize];
_ = stream.Read(Padding, 0, (int)paddingSize);
_ = stream.Read(Padding);
}

try
Expand Down
4 changes: 2 additions & 2 deletions Img2Ffu.Library/Reader/Data/Store.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public Store(Stream stream)
{
StoreHeaderV2 = stream.ReadStructure<StoreHeaderV2>();
byte[] stringBytes = new byte[StoreHeaderV2.DevicePathLength * 2];
_ = stream.Read(stringBytes, 0, stringBytes.Length);
_ = stream.Read(stringBytes);
UnicodeEncoding unicodeEncoding = new();
DevicePath = unicodeEncoding.GetString(stringBytes);
break;
Expand Down Expand Up @@ -94,7 +94,7 @@ public Store(Stream stream)
{
long paddingSize = StoreHeader.BlockSize - (Position % StoreHeader.BlockSize);
Padding = new byte[paddingSize];
_ = stream.Read(Padding, 0, (int)paddingSize);
_ = stream.Read(Padding);
}
}

Expand Down
2 changes: 1 addition & 1 deletion Img2Ffu.Library/Reader/Data/ValidationDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public ValidationDescriptor(Stream stream)
ValidationEntry = stream.ReadStructure<ValidationEntry>();

ValidationBytes = new byte[ValidationEntry.dwByteCount];
_ = stream.Read(ValidationBytes, 0, (int)ValidationEntry.dwByteCount);
_ = stream.Read(ValidationBytes);
}

public ValidationDescriptor(ValidationEntry validationEntry, byte[] validationBytes)
Expand Down
6 changes: 3 additions & 3 deletions Img2Ffu.Library/Writer/CatalogFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace Img2Ffu.Writer
{
internal static class CatalogFactory
{
internal static byte[] GenerateCatalogFile(byte[] hashData)
internal static Span<byte> GenerateCatalogFile(Span<byte> hashData)
{
byte[] catalog_first_part = [0x30, 0x82, 0x01, 0x44, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x02, 0xA0, 0x82, 0x01, 0x35, 0x30, 0x82, 0x01, 0x31, 0x02, 0x01, 0x01, 0x31, 0x00, 0x30, 0x82, 0x01, 0x26, 0x06, 0x09, 0x2B, 0x06, 0x01, 0x04, 0x01, 0x82, 0x37, 0x0A, 0x01, 0xA0, 0x82, 0x01, 0x17, 0x30, 0x82, 0x01, 0x13, 0x30, 0x0C, 0x06, 0x0A, 0x2B, 0x06, 0x01, 0x04, 0x01, 0x82, 0x37, 0x0C, 0x01, 0x01, 0x04, 0x10, 0xA8, 0xCA, 0xD9, 0x7D, 0xBF, 0x6D, 0x67, 0x4D, 0xB1, 0x4D, 0x62, 0xFB, 0xE6, 0x26, 0x22, 0xD4, 0x17, 0x0D, 0x32, 0x30, 0x30, 0x31, 0x31, 0x30, 0x31, 0x32, 0x31, 0x32, 0x32, 0x37, 0x5A, 0x30, 0x0E, 0x06, 0x0A, 0x2B, 0x06, 0x01, 0x04, 0x01, 0x82, 0x37, 0x0C, 0x01, 0x02, 0x05, 0x00, 0x30, 0x81, 0xD1, 0x30, 0x81, 0xCE, 0x04, 0x1E, 0x48, 0x00, 0x61, 0x00, 0x73, 0x00, 0x68, 0x00, 0x54, 0x00, 0x61, 0x00, 0x62, 0x00, 0x6C, 0x00, 0x65, 0x00, 0x2E, 0x00, 0x62, 0x00, 0x6C, 0x00, 0x6F, 0x00, 0x62, 0x00, 0x00, 0x00, 0x31, 0x81, 0xAB, 0x30, 0x45, 0x06, 0x0A, 0x2B, 0x06, 0x01, 0x04, 0x01, 0x82, 0x37, 0x02, 0x01, 0x04, 0x31, 0x37, 0x30, 0x35, 0x30, 0x10, 0x06, 0x0A, 0x2B, 0x06, 0x01, 0x04, 0x01, 0x82, 0x37, 0x02, 0x01, 0x19, 0xA2, 0x02, 0x80, 0x00, 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B, 0x0E, 0x03, 0x02, 0x1A, 0x05, 0x00, 0x04, 0x14];
byte[] catalog_second_part = [0x30, 0x62, 0x06, 0x0A, 0x2B, 0x06, 0x01, 0x04, 0x01, 0x82, 0x37, 0x0C, 0x02, 0x02, 0x31, 0x54, 0x30, 0x52, 0x1E, 0x4C, 0x00, 0x7B, 0x00, 0x44, 0x00, 0x45, 0x00, 0x33, 0x00, 0x35, 0x00, 0x31, 0x00, 0x41, 0x00, 0x34, 0x00, 0x32, 0x00, 0x2D, 0x00, 0x38, 0x00, 0x45, 0x00, 0x35, 0x00, 0x39, 0x00, 0x2D, 0x00, 0x31, 0x00, 0x31, 0x00, 0x44, 0x00, 0x30, 0x00, 0x2D, 0x00, 0x38, 0x00, 0x43, 0x00, 0x34, 0x00, 0x37, 0x00, 0x2D, 0x00, 0x30, 0x00, 0x30, 0x00, 0x43, 0x00, 0x30, 0x00, 0x34, 0x00, 0x46, 0x00, 0x43, 0x00, 0x32, 0x00, 0x39, 0x00, 0x35, 0x00, 0x45, 0x00, 0x45, 0x00, 0x7D, 0x02, 0x02, 0x02, 0x00, 0x31, 0x00];
Expand All @@ -41,7 +41,7 @@ internal static byte[] GenerateCatalogFile(byte[] hashData)
return catalog;
}

internal static byte[] GenerateCatalogFile2(byte[] hashData)
internal static Span<byte> GenerateCatalogFile2(byte[] hashData)
{
string catalog = Path.GetTempFileName();
string cdf = Path.GetTempFileName();
Expand Down Expand Up @@ -74,7 +74,7 @@ internal static byte[] GenerateCatalogFile2(byte[] hashData)
}
}

byte[] catalogBuffer = File.ReadAllBytes(catalog);
Span<byte> catalogBuffer = File.ReadAllBytes(catalog);

File.Delete(catalog);
File.Delete(hashTableBlob);
Expand Down
4 changes: 2 additions & 2 deletions Img2Ffu.Library/Writer/ChunkUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ internal static void RoundUpToChunks(FileStream stream, uint chunkSize)
if (Size % chunkSize > 0)
{
long padding = (uint)(((Size / chunkSize) + 1) * chunkSize) - Size;
stream.Write(new byte[padding], 0, (int)padding);
stream.Write(new byte[padding]);
}
}

Expand All @@ -39,7 +39,7 @@ internal static void RoundUpToChunks(MemoryStream stream, uint chunkSize)
if (Size % chunkSize > 0)
{
long padding = (uint)(((Size / chunkSize) + 1) * chunkSize) - Size;
stream.Write(new byte[padding], 0, (int)padding);
stream.Write(new byte[padding]);
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions Img2Ffu.Library/Writer/Data/DeviceTargetInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class DeviceTargetInfo(string manufacturer, string family, string product

public string BaseboardProduct { get; } = baseboardProduct;

internal byte[] GetResultingBuffer()
internal Span<byte> GetResultingBuffer()
{
using MemoryStream DeviceTargetInfoStream = new();
using BinaryWriter binaryWriter = new(DeviceTargetInfoStream);
Expand Down Expand Up @@ -82,11 +82,12 @@ internal byte[] GetResultingBuffer()
binaryWriter.Write(Encoding.ASCII.GetBytes(BaseboardManufacturer));
binaryWriter.Write(Encoding.ASCII.GetBytes(BaseboardProduct));

byte[] DeviceTargetInfoBuffer = new byte[DeviceTargetInfoStream.Length];
Memory<byte> DeviceTargetInfoBuffer = new byte[DeviceTargetInfoStream.Length];
Span<byte> span = DeviceTargetInfoBuffer.Span;
_ = DeviceTargetInfoStream.Seek(0, SeekOrigin.Begin);
DeviceTargetInfoStream.ReadExactly(DeviceTargetInfoBuffer, 0, DeviceTargetInfoBuffer.Length);
DeviceTargetInfoStream.ReadExactly(span);

return DeviceTargetInfoBuffer;
return span;
}
}
}
9 changes: 5 additions & 4 deletions Img2Ffu.Library/Writer/Data/ImageHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public uint ManifestLength
get; set;
}

public byte[] GetResultingBuffer(uint BlockSize, bool HasDeviceTargetInfo, uint DeviceTargetInfosCount)
public Span<byte> GetResultingBuffer(uint BlockSize, bool HasDeviceTargetInfo, uint DeviceTargetInfosCount)
{
using MemoryStream ImageHeaderStream = new();
using BinaryWriter binaryWriter = new(ImageHeaderStream);
Expand All @@ -45,11 +45,12 @@ public byte[] GetResultingBuffer(uint BlockSize, bool HasDeviceTargetInfo, uint
binaryWriter.Write(DeviceTargetInfosCount); // Device Target Infos Count
}

byte[] ImageHeaderBuffer = new byte[ImageHeaderStream.Length];
Memory<byte> ImageHeaderBuffer = new byte[ImageHeaderStream.Length];
Span<byte> span = ImageHeaderBuffer.Span;
_ = ImageHeaderStream.Seek(0, SeekOrigin.Begin);
ImageHeaderStream.ReadExactly(ImageHeaderBuffer, 0, ImageHeaderBuffer.Length);
ImageHeaderStream.ReadExactly(span);

return ImageHeaderBuffer;
return span;
}
}
}
9 changes: 5 additions & 4 deletions Img2Ffu.Library/Writer/Data/SecurityHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class SecurityHeader
public uint CatalogSize;
public uint HashTableSize;

public byte[] GetResultingBuffer(uint BlockSize)
public Span<byte> GetResultingBuffer(uint BlockSize)
{
using MemoryStream SecurityHeaderStream = new();
using BinaryWriter binaryWriter = new(SecurityHeaderStream);
Expand All @@ -46,11 +46,12 @@ public byte[] GetResultingBuffer(uint BlockSize)
binaryWriter.Write(CatalogSize);
binaryWriter.Write(HashTableSize);

byte[] SecurityHeaderBuffer = new byte[SecurityHeaderStream.Length];
Memory<byte> SecurityHeaderBuffer = new byte[SecurityHeaderStream.Length];
Span<byte> span = SecurityHeaderBuffer.Span;
_ = SecurityHeaderStream.Seek(0, SeekOrigin.Begin);
SecurityHeaderStream.ReadExactly(SecurityHeaderBuffer, 0, SecurityHeaderBuffer.Length);
SecurityHeaderStream.ReadExactly(span);

return SecurityHeaderBuffer;
return span;
}
}
}
6 changes: 3 additions & 3 deletions Img2Ffu.Library/Writer/Data/StoreHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public string? DevicePath
get; set;
}

public byte[] GetResultingBuffer(FlashUpdateVersion storeHeaderVersion, FlashUpdateType storeHeaderUpdateType, CompressionAlgorithm storeHeaderCompressionAlgorithm)
public Memory<byte> GetResultingBuffer(FlashUpdateVersion storeHeaderVersion, FlashUpdateType storeHeaderUpdateType, CompressionAlgorithm storeHeaderCompressionAlgorithm)
{
switch (storeHeaderVersion)
{
Expand Down Expand Up @@ -183,9 +183,9 @@ public byte[] GetResultingBuffer(FlashUpdateVersion storeHeaderVersion, FlashUpd
break;
}

byte[] StoreHeaderBuffer = new byte[StoreHeaderStream.Length];
Memory<byte> StoreHeaderBuffer = new byte[StoreHeaderStream.Length];
_ = StoreHeaderStream.Seek(0, SeekOrigin.Begin);
StoreHeaderStream.ReadExactly(StoreHeaderBuffer, 0, StoreHeaderBuffer.Length);
StoreHeaderStream.ReadExactly(StoreHeaderBuffer.Span);

return StoreHeaderBuffer;
}
Expand Down
9 changes: 5 additions & 4 deletions Img2Ffu.Library/Writer/Data/WriteDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public required DiskLocation[] DiskLocations
get; set;
}

public byte[] GetResultingBuffer(FlashUpdateVersion storeHeaderVersion, uint CompressedDataBlockSize = 0)
public Span<byte> GetResultingBuffer(FlashUpdateVersion storeHeaderVersion, uint CompressedDataBlockSize = 0)
{
using MemoryStream WriteDescriptorStream = new();
using BinaryWriter binaryWriter = new(WriteDescriptorStream);
Expand All @@ -55,11 +55,12 @@ public byte[] GetResultingBuffer(FlashUpdateVersion storeHeaderVersion, uint Com
binaryWriter.Write(DiskLocation.BlockIndex);
}

byte[] WriteDescriptorBuffer = new byte[WriteDescriptorStream.Length];
Memory<byte> WriteDescriptorBuffer = new byte[WriteDescriptorStream.Length];
Span<byte> span = WriteDescriptorBuffer.Span;
_ = WriteDescriptorStream.Seek(0, SeekOrigin.Begin);
WriteDescriptorStream.ReadExactly(WriteDescriptorBuffer, 0, WriteDescriptorBuffer.Length);
WriteDescriptorStream.ReadExactly(span);

return WriteDescriptorBuffer;
return span;
}
}
}
Loading

0 comments on commit 6dd87ff

Please sign in to comment.