-
Notifications
You must be signed in to change notification settings - Fork 4
/
EPartitionStream.cs
196 lines (169 loc) · 7.09 KB
/
EPartitionStream.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Copyright (c) 2018, Gustave M. - gus33000.me - @gus33000
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace DumpIt
{
internal class EPartitionStream : Stream, IDisposable
{
private Stream innerstream;
private readonly string[] excluded;
private readonly List<GPTPartition> partitions;
private readonly bool IS_UNLOCKED = false;
private readonly uint SectorSize;
private bool disposed;
public EPartitionStream(Stream stream, uint SectorSize, string[] partitionstoexclude)
{
this.SectorSize = SectorSize;
innerstream = stream;
excluded = partitionstoexclude;
partitions = GetPartsFromGPT(this, SectorSize);
if (partitions.Any(x => x.Name == "IS_UNLOCKED"))
{
IS_UNLOCKED = true;
}
}
public override bool CanRead => innerstream.CanRead;
public override bool CanSeek => innerstream.CanSeek;
public override bool CanWrite => innerstream.CanWrite;
public override long Length => innerstream.Length;
public override long Position
{
get => innerstream.Position; set => innerstream.Position = value;
}
public override void Flush()
{
innerstream.Flush();
}
public override int Read(byte[] buffer, int offset, int count)
{
ulong readingstart = Convert.ToUInt64(Position);
ulong readingend = readingstart + Convert.ToUInt64(count);
int read = innerstream.Read(buffer, offset, count);
foreach (GPTPartition partition in partitions)
{
if (IS_UNLOCKED && partition.Name == "UEFI_BS_NV")
{
continue;
}
// The partition is excluded.
if (excluded.Any(x => x.ToLower() == partition.Name.ToLower()))
{
if (readingend < (partition.FirstSector * SectorSize))
{
continue;
}
if (readingstart > (partition.LastSector * SectorSize))
{
continue;
}
// We read inside the partition
if (readingstart >= (partition.FirstSector * SectorSize) && readingend <= (partition.LastSector * SectorSize))
{
for (int i = offset; i < count; i++)
{
buffer[i] = 0;
}
return read;
}
// We read beyond the partition in every way
if (readingstart < (partition.FirstSector * SectorSize) && readingend > (partition.LastSector * SectorSize))
{
for (int i = (int)(partition.FirstSector * SectorSize) - (int)readingstart + offset;
i < (int)(readingend - readingstart);
i++)
{
buffer[i] = 0;
}
}
// We read from inside the partition to beyond the partition.
if (readingstart >= (partition.FirstSector * SectorSize) && readingstart <= (partition.LastSector * SectorSize) && readingend > (partition.LastSector * SectorSize))
{
int bytecounttoremoveatthestart = (int)((partition.LastSector * SectorSize) - readingstart);
for (int i = offset; i < offset + bytecounttoremoveatthestart; i++)
{
buffer[i] = 0;
}
}
// We read from outside the partition to inside the partition and no partition before is excluded.
if (readingstart < (partition.FirstSector * SectorSize) && readingend <= (partition.LastSector * SectorSize) && readingend >= (partition.FirstSector * SectorSize))
{
int bytecounttoremoveattheend = (int)(readingend - (partition.FirstSector * SectorSize));
for (int i = count - bytecounttoremoveattheend; i < count; i++)
{
buffer[i] = 0;
}
}
}
}
return read;
}
public override long Seek(long offset, SeekOrigin origin)
{
return innerstream.Seek(offset, origin);
}
public override void SetLength(long value)
{
innerstream.SetLength(value);
}
public override void Write(byte[] buffer, int offset, int count)
{
innerstream.Write(buffer, offset, count);
}
public static List<GPTPartition> GetPartsFromGPT(Stream stream, uint SectorSize)
{
byte[] GPTBuffer = new byte[0x100 * SectorSize];
_ = stream.Read(GPTBuffer, 0, 0x100 * (int)SectorSize);
GPT GPT = new(GPTBuffer, SectorSize);
return GPT.Partitions;
}
public override void Close()
{
innerstream.Dispose();
innerstream = null;
base.Close();
}
public new void Dispose()
{
Dispose(true);
base.Dispose();
GC.SuppressFinalize(this);
}
protected new void Dispose(bool disposing)
{
// Check to see if Dispose has already been called.
if (!disposed)
{
if (disposing)
{
if (innerstream != null)
{
innerstream.Dispose();
innerstream = null;
}
}
// Note disposing has been done.
disposed = true;
}
}
}
}