Skip to content

Commit c545e21

Browse files
committed
Migrate GPT Partition Table to Span API
1 parent 7286bd6 commit c545e21

2 files changed

Lines changed: 34 additions & 34 deletions

File tree

‎TotalImage.IO/CHSAddress.cs‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace TotalImage
1+
using System;
2+
3+
namespace TotalImage
24
{
35
/// <summary>
46
/// A structure representing a CHS address
@@ -24,7 +26,7 @@ public readonly struct CHSAddress
2426
/// Creates a CHS address from its three byte binary representation
2527
/// </summary>
2628
/// <param name="address">A three byte array containing the address</param>
27-
public CHSAddress(byte[] address)
29+
public CHSAddress(in Span<byte> address)
2830
{
2931
if (address.Length != 3)
3032
{

‎TotalImage.IO/Partitions/GptPartitionTable.cs‎

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Buffers.Binary;
23
using System.Collections.Generic;
34
using System.Collections.Immutable;
45
using System.ComponentModel.DataAnnotations;
@@ -33,37 +34,35 @@ public GptPartitionTable(Container container, uint sectorSize = 512) : base(cont
3334
/// <inheritdoc />
3435
protected override IEnumerable<PartitionEntry> LoadPartitions()
3536
{
36-
using BinaryReader br = new BinaryReader(_container.Content, Encoding.ASCII, true);
37-
br.BaseStream.Seek(_sectorSize, SeekOrigin.Begin);
37+
_container.Content.Seek(_sectorSize, SeekOrigin.Begin);
3838

39-
_header = new GptHeader(br);
39+
byte[] buffer = new byte[92];
40+
_container.Content.Read(buffer);
41+
_header = new GptHeader(buffer);
4042

4143
if (_header.Signature != "EFI PART")
4244
{
4345
throw new InvalidDataException("Not a GPT Protective Partition");
4446
}
4547

46-
br.BaseStream.Seek((long)(_sectorSize * _header.TableLBA), SeekOrigin.Begin);
48+
_container.Content.Seek((long)(_sectorSize * _header.TableLBA), SeekOrigin.Begin);
4749

4850
var entries = ImmutableList.CreateBuilder<PartitionEntry>();
51+
buffer = new byte[_header.SizeOfPartitionEntry];
52+
4953
for (int i = 0; i < _header.PartitionEntries; i++)
5054
{
51-
Guid typeId = new Guid(br.ReadBytes(16));
52-
Guid entryId = new Guid(br.ReadBytes(16));
53-
ulong firstLba = br.ReadUInt64();
54-
ulong lastLba = br.ReadUInt64();
55-
GptPartitionFlags flags = (GptPartitionFlags)br.ReadUInt64();
56-
string name = Encoding.Unicode.GetString(br.ReadBytes(72));
55+
_container.Content.Read(buffer);
56+
Guid typeId = new Guid(buffer[0..16]);
57+
Guid entryId = new Guid(buffer[16..32]);
58+
ulong firstLba = BinaryPrimitives.ReadUInt64LittleEndian(buffer[32..40]);
59+
ulong lastLba = BinaryPrimitives.ReadUInt64LittleEndian(buffer[40..48]);
60+
GptPartitionFlags flags = (GptPartitionFlags)BinaryPrimitives.ReadUInt64LittleEndian(buffer[48..56]);
61+
string name = Encoding.Unicode.GetString(buffer[56..128]);
5762

5863
long offset = (long)(firstLba * _sectorSize);
5964
long length = (long)((lastLba - firstLba + 1) * _sectorSize);
6065

61-
long additionalBytes = _header.SizeOfPartitionEntry - 0x80;
62-
if (additionalBytes > 0)
63-
{
64-
br.BaseStream.Seek(additionalBytes, SeekOrigin.Current);
65-
}
66-
6766
if (typeId != Guid.Empty)
6867
{
6968
entries.Add(new GptPartitionEntry(typeId, entryId, flags, name, offset, length, new PartialStream(_container.Content, offset, length)));
@@ -150,25 +149,24 @@ public class GptHeader
150149
/// <summary>
151150
/// Read a GUID Partition Table header from a stream
152151
/// </summary>
153-
/// <param name="reader">The binary reader for the stream</param>
154-
public GptHeader(BinaryReader reader)
152+
/// <param name="bytes">The bytes containing the header</param>
153+
public GptHeader(in Span<byte> bytes)
155154
{
156-
Signature = new string(reader.ReadChars(8));
157-
uint version = reader.ReadUInt32();
155+
Signature = Encoding.ASCII.GetString(bytes[0..8]);
156+
uint version = BinaryPrimitives.ReadUInt32LittleEndian(bytes[8..12]);
158157
VersionMajor = (ushort)(version >> 16);
159158
VersionMinor = (ushort)(version & 0xFFFF);
160-
HeaderSize = reader.ReadUInt32();
161-
HeaderHash = reader.ReadUInt32();
162-
reader.BaseStream.Seek(4, SeekOrigin.Current); // skip past reserved field
163-
CurrentLBA = reader.ReadUInt64();
164-
BackupLBA = reader.ReadUInt64();
165-
FirstPartitionLBA = reader.ReadUInt64();
166-
LastPartitionLBA = reader.ReadUInt64();
167-
DiskGuid = new Guid(reader.ReadBytes(16));
168-
TableLBA = reader.ReadUInt64();
169-
PartitionEntries = reader.ReadUInt32();
170-
SizeOfPartitionEntry = reader.ReadUInt32();
171-
TableHash = reader.ReadUInt32();
159+
HeaderSize = BinaryPrimitives.ReadUInt32LittleEndian(bytes[12..16]);
160+
HeaderHash = BinaryPrimitives.ReadUInt32LittleEndian(bytes[16..20]);
161+
CurrentLBA = BinaryPrimitives.ReadUInt64LittleEndian(bytes[24..32]);
162+
BackupLBA = BinaryPrimitives.ReadUInt64LittleEndian(bytes[32..40]);
163+
FirstPartitionLBA = BinaryPrimitives.ReadUInt64LittleEndian(bytes[40..48]);
164+
LastPartitionLBA = BinaryPrimitives.ReadUInt64LittleEndian(bytes[48..56]);
165+
DiskGuid = new Guid(bytes[56..72]);
166+
TableLBA = BinaryPrimitives.ReadUInt64LittleEndian(bytes[72..80]);
167+
PartitionEntries = BinaryPrimitives.ReadUInt32LittleEndian(bytes[80..84]);
168+
SizeOfPartitionEntry = BinaryPrimitives.ReadUInt32LittleEndian(bytes[84..88]);
169+
TableHash = BinaryPrimitives.ReadUInt32LittleEndian(bytes[88..92]);
172170
}
173171
}
174172

0 commit comments

Comments
 (0)