Skip to content

Commit

Permalink
Further improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
gus33000 committed Oct 5, 2024
1 parent 6dd87ff commit ccc3e53
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 10 deletions.
14 changes: 7 additions & 7 deletions Img2Ffu.Library/Reader/Data/ImageFlash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public ImageFlash(Stream stream)

if (ImageHeader.Size == 28u)
{
byte[] DeviceTargetingInfoCountBuffer = new byte[4];
Span<byte> DeviceTargetingInfoCountBuffer = new byte[4];
_ = stream.Read(DeviceTargetingInfoCountBuffer);

DeviceTargetingInfoCount = BitConverter.ToUInt32(DeviceTargetingInfoCountBuffer);
Expand All @@ -62,7 +62,7 @@ public ImageFlash(Stream stream)
// Account for FFUs with Device Target Info Count and Device Target Infos and potentially other things...
_ = stream.Seek(InitialStreamPosition + ImageHeader.Size, SeekOrigin.Begin);

byte[] manifestDataBuffer = new byte[ImageHeader.ManifestLength];
Span<byte> manifestDataBuffer = new byte[ImageHeader.ManifestLength];
_ = stream.Read(manifestDataBuffer);
ASCIIEncoding asciiEncoding = new();
ManifestData = asciiEncoding.GetString(manifestDataBuffer);
Expand Down Expand Up @@ -102,14 +102,14 @@ public ulong GetImageBlockCount()
return imageBlockCount + storeBlockCount;
}

public byte[] GetImageBlock(Stream Stream, ulong dataBlockIndex)
public Span<byte> GetImageBlock(Stream Stream, ulong dataBlockIndex)
{
ulong imageBlockCount = (ulong)(DataBlocksPosition - InitialStreamPosition) / (ImageHeader.ChunkSize * 1024);

// The data block is within the image headers
if (dataBlockIndex < imageBlockCount)
{
byte[] dataBlock = new byte[(ImageHeader.ChunkSize * 1024)];
Span<byte> dataBlock = new byte[(ImageHeader.ChunkSize * 1024)];
ulong dataBlockPosition = (ulong)InitialStreamPosition + (dataBlockIndex * (ImageHeader.ChunkSize * 1024));

_ = Stream.Seek((long)dataBlockPosition, SeekOrigin.Begin);
Expand All @@ -130,12 +130,12 @@ public ulong GetDataBlockCount()
ulong dataBlockCount = 0;
foreach (Store store in Stores)
{
dataBlockCount += (ulong)store.WriteDescriptors.LongCount();
dataBlockCount += (ulong)store.WriteDescriptors.Count;
}
return dataBlockCount;
}

public byte[] GetDataBlock(Stream Stream, ulong dataBlockIndex)
public Span<byte> GetDataBlock(Stream Stream, ulong dataBlockIndex)
{
ulong dataBlockIndexOffset = 0;
for (ulong storeIndex = 0; storeIndex < (ulong)Stores.Count; storeIndex++)
Expand All @@ -155,7 +155,7 @@ public byte[] GetDataBlock(Stream Stream, ulong dataBlockIndex)

public ulong GetStoreDataBlockCount(ulong storeIndex)
{
return (ulong)Stores[(int)storeIndex].WriteDescriptors.LongCount();
return (ulong)Stores[(int)storeIndex].WriteDescriptors.Count;
}

public byte[] GetStoreDataBlock(Stream Stream, ulong storeIndex, ulong dataBlockIndex)
Expand Down
2 changes: 1 addition & 1 deletion Img2Ffu.Library/Reader/Data/SignedImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public void VerifyFFU()

if (SecurityHeader.AlgorithmId == 0x0000800c) // SHA256 Algorithm ID
{
byte[] block = Image.GetImageBlock(Stream, i);
Span<byte> block = Image.GetImageBlock(Stream, i);
byte[] hash = SHA256.HashData(block);
byte[] hashTableHash = BlockHashes.ElementAt((int)i);

Expand Down
2 changes: 1 addition & 1 deletion Img2Ffu.Library/Reader/Data/Store.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public Store(Stream stream)
case FFUVersion.V2:
{
StoreHeaderV2 = stream.ReadStructure<StoreHeaderV2>();
byte[] stringBytes = new byte[StoreHeaderV2.DevicePathLength * 2];
Span<byte> stringBytes = new byte[StoreHeaderV2.DevicePathLength * 2];
_ = stream.Read(stringBytes);
UnicodeEncoding unicodeEncoding = new();
DevicePath = unicodeEncoding.GetString(stringBytes);
Expand Down
2 changes: 1 addition & 1 deletion Img2Ffu.Library/Reader/FullFlashUpdateReaderStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ public void CopyTo(Stream DestinationStream, Action<ulong, ulong> ProgressCallBa

_ = DestinationStream.Seek(OriginalDestinationStreamPosition + virtualPosition, SeekOrigin.Begin);

byte[] buffer = image.GetStoreDataBlock(Stream, storeIndex, (ulong)physicalDiskBlockNumber);
Span<byte> buffer = image.GetStoreDataBlock(Stream, storeIndex, (ulong)physicalDiskBlockNumber);
currentWrittenBytes += (ulong)blockSize;
ProgressCallBack?.Invoke(currentWrittenBytes, totalBytes);

Expand Down

0 comments on commit ccc3e53

Please sign in to comment.