|
1 | 1 | using System; |
| 2 | +using System.Buffers.Binary; |
2 | 3 | using System.Collections.Generic; |
3 | 4 | using System.Collections.Immutable; |
4 | 5 | using System.ComponentModel.DataAnnotations; |
@@ -33,37 +34,35 @@ public GptPartitionTable(Container container, uint sectorSize = 512) : base(cont |
33 | 34 | /// <inheritdoc /> |
34 | 35 | protected override IEnumerable<PartitionEntry> LoadPartitions() |
35 | 36 | { |
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); |
38 | 38 |
|
39 | | - _header = new GptHeader(br); |
| 39 | + byte[] buffer = new byte[92]; |
| 40 | + _container.Content.Read(buffer); |
| 41 | + _header = new GptHeader(buffer); |
40 | 42 |
|
41 | 43 | if (_header.Signature != "EFI PART") |
42 | 44 | { |
43 | 45 | throw new InvalidDataException("Not a GPT Protective Partition"); |
44 | 46 | } |
45 | 47 |
|
46 | | - br.BaseStream.Seek((long)(_sectorSize * _header.TableLBA), SeekOrigin.Begin); |
| 48 | + _container.Content.Seek((long)(_sectorSize * _header.TableLBA), SeekOrigin.Begin); |
47 | 49 |
|
48 | 50 | var entries = ImmutableList.CreateBuilder<PartitionEntry>(); |
| 51 | + buffer = new byte[_header.SizeOfPartitionEntry]; |
| 52 | + |
49 | 53 | for (int i = 0; i < _header.PartitionEntries; i++) |
50 | 54 | { |
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]); |
57 | 62 |
|
58 | 63 | long offset = (long)(firstLba * _sectorSize); |
59 | 64 | long length = (long)((lastLba - firstLba + 1) * _sectorSize); |
60 | 65 |
|
61 | | - long additionalBytes = _header.SizeOfPartitionEntry - 0x80; |
62 | | - if (additionalBytes > 0) |
63 | | - { |
64 | | - br.BaseStream.Seek(additionalBytes, SeekOrigin.Current); |
65 | | - } |
66 | | - |
67 | 66 | if (typeId != Guid.Empty) |
68 | 67 | { |
69 | 68 | entries.Add(new GptPartitionEntry(typeId, entryId, flags, name, offset, length, new PartialStream(_container.Content, offset, length))); |
@@ -150,25 +149,24 @@ public class GptHeader |
150 | 149 | /// <summary> |
151 | 150 | /// Read a GUID Partition Table header from a stream |
152 | 151 | /// </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) |
155 | 154 | { |
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]); |
158 | 157 | VersionMajor = (ushort)(version >> 16); |
159 | 158 | 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]); |
172 | 170 | } |
173 | 171 | } |
174 | 172 |
|
|
0 commit comments