Skip to content

Commit 52e7241

Browse files
committed
Rewrite MBR to use spans
1 parent c545e21 commit 52e7241

1 file changed

Lines changed: 17 additions & 11 deletions

File tree

‎TotalImage.IO/Partitions/MbrPartitionTable.cs‎

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System.Buffers.Binary;
2+
using System.Collections.Generic;
23
using System.Collections.Immutable;
34
using System.ComponentModel.DataAnnotations;
45
using System.IO;
@@ -26,26 +27,31 @@ public MbrPartitionTable(Container container, uint sectorSize = 512) : base(cont
2627
/// <inheritdoc />
2728
protected override IEnumerable<PartitionEntry> LoadPartitions()
2829
{
29-
using BinaryReader br = new BinaryReader(_container.Content, Encoding.ASCII, true);
30-
br.BaseStream.Seek(0x1FE, SeekOrigin.Begin);
30+
_container.Content.Seek(0x1FE, SeekOrigin.Begin);
3131

32-
var signature = br.ReadUInt16();
32+
byte[] buffer = new byte[2];
33+
_container.Content.Read(buffer);
34+
var signature = BinaryPrimitives.ReadUInt16LittleEndian(buffer);
3335
if (signature != 0xaa55)
3436
{
3537
throw new InvalidDataException();
3638
}
3739

38-
br.BaseStream.Seek(0x1BE, SeekOrigin.Begin);
40+
_container.Content.Seek(0x1BE, SeekOrigin.Begin);
41+
42+
buffer = new byte[64];
43+
_container.Content.Read(buffer);
3944

4045
var entries = ImmutableList.CreateBuilder<PartitionEntry>();
4146
for (int i = 0; i < 4; i++)
4247
{
43-
byte status = br.ReadByte();
44-
CHSAddress chsStart = new CHSAddress(br.ReadBytes(3));
45-
MbrPartitionType type = (MbrPartitionType)br.ReadByte();
46-
CHSAddress chsEnd = new CHSAddress(br.ReadBytes(3));
47-
uint lbaStart = br.ReadUInt32();
48-
uint lbaLength = br.ReadUInt32();
48+
var record = buffer[(i * 16)..((i + 1) * 16)];
49+
byte status = record[0];
50+
CHSAddress chsStart = new CHSAddress(record[1..4]);
51+
MbrPartitionType type = (MbrPartitionType)record[4];
52+
CHSAddress chsEnd = new CHSAddress(record[5..8]);
53+
uint lbaStart = BinaryPrimitives.ReadUInt32LittleEndian(record[8..12]);
54+
uint lbaLength = BinaryPrimitives.ReadUInt32LittleEndian(record[12..16]);
4955

5056
if (type == MbrPartitionType.Empty)
5157
{

0 commit comments

Comments
 (0)