|
1 | | -using System.Collections.Generic; |
| 1 | +using System.Buffers.Binary; |
| 2 | +using System.Collections.Generic; |
2 | 3 | using System.Collections.Immutable; |
3 | 4 | using System.ComponentModel.DataAnnotations; |
4 | 5 | using System.IO; |
@@ -26,26 +27,31 @@ public MbrPartitionTable(Container container, uint sectorSize = 512) : base(cont |
26 | 27 | /// <inheritdoc /> |
27 | 28 | protected override IEnumerable<PartitionEntry> LoadPartitions() |
28 | 29 | { |
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); |
31 | 31 |
|
32 | | - var signature = br.ReadUInt16(); |
| 32 | + byte[] buffer = new byte[2]; |
| 33 | + _container.Content.Read(buffer); |
| 34 | + var signature = BinaryPrimitives.ReadUInt16LittleEndian(buffer); |
33 | 35 | if (signature != 0xaa55) |
34 | 36 | { |
35 | 37 | throw new InvalidDataException(); |
36 | 38 | } |
37 | 39 |
|
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); |
39 | 44 |
|
40 | 45 | var entries = ImmutableList.CreateBuilder<PartitionEntry>(); |
41 | 46 | for (int i = 0; i < 4; i++) |
42 | 47 | { |
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]); |
49 | 55 |
|
50 | 56 | if (type == MbrPartitionType.Empty) |
51 | 57 | { |
|
0 commit comments