-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathScoopReadWriter.cs
302 lines (274 loc) · 10.6 KB
/
ScoopReadWriter.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
using Microsoft.Win32.SafeHandles;
namespace poc1poc2Conv
{
/// <summary>
/// Summary description for ScoopReadWriter.
/// </summary>
public class ScoopReadWriter
{
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool SetFileValidData(SafeFileHandle hFile, long ValidDataLength);
protected string _sFileName;
protected string _tFileName;
private long _lLength = -1;
protected bool _bOpen = false;
protected FileStream _fs;
protected FileStream _ft;
bool _inline;
private long _lPosition = 0;
//inline constructor
public ScoopReadWriter(string stFileName)
{
_sFileName = stFileName;
_tFileName = stFileName;
_inline = true;
}
//separate file constructor
public ScoopReadWriter(string sFileName, string tFileName)
{
_sFileName = sFileName;
_tFileName = tFileName;
_inline = false;
}
public Boolean PreAlloc(long totalNonces)
{
try
{
_ft.SetLength(totalNonces * (2 << 17));
}
catch (Exception e)
{
MessageBox.Show(e.Message, "Error Preallocating File", MessageBoxButtons.OK);
return false;
}
bool test = SetFileValidData(_ft.SafeFileHandle, totalNonces * (2 << 17));
if (!test)
{
DialogResult dialogResult = MessageBox.Show("Elevated file creation failed. File creation will be very slow. Continue?", "Freeze Warning", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.No)
{
return false;
}
}
return true;
}
public void Close()
{
if (_bOpen) {
if (!_inline) _fs.Close();
_ft.Close();
_fs = null;
_ft = null;
_bOpen = false;
}
}
public bool EOF
{
get
{
return (!_bOpen || (_lPosition >= _lLength));
}
}
public Boolean Open(bool directIO)
{
FileOptions FileFlagNoBuffering = (FileOptions)0x20000000;
try
{
if (_inline)
{
if (directIO) {
_fs = new FileStream(_sFileName, FileMode.Open, FileAccess.ReadWrite, FileShare.None, 1048576, FileFlagNoBuffering);
}
else {
_fs = new FileStream(_sFileName, FileMode.Open, FileAccess.ReadWrite, FileShare.None, 1048576, FileOptions.SequentialScan | FileOptions.WriteThrough);
}
_ft = _fs;
}
else
{
if (!Privileges.HasAdminPrivileges)
{
DialogResult dialogResult = MessageBox.Show("No elevated file creation possible. File creation will be very slow. Continue?", "Freeze Warning", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.No)
{
Application.Exit();
}
}
if (directIO)
{
_fs = new FileStream(_sFileName, FileMode.Open, FileAccess.Read, FileShare.Read, 1048576, FileFlagNoBuffering);
_ft = new FileStream(_tFileName, FileMode.Create, FileAccess.Write, FileShare.None, 1048576, FileFlagNoBuffering);
}
else
{
_fs = new FileStream(_sFileName, FileMode.Open, FileAccess.Read, FileShare.Read, 1048576, FileOptions.SequentialScan);
_ft = new FileStream(_tFileName, FileMode.Create, FileAccess.Write, FileShare.None, 1048576, FileOptions.WriteThrough);
}
}
}
catch (Exception e)
{
//MessageBox.Show(e.Message, "Error Opening File", MessageBoxButtons.OK);
if(_fs != null) _fs.Close();
if(_ft != null) _ft.Close();
return false;
}
_lLength = _fs.Length;
_lPosition = 0;
_bOpen = true;
return true;
}
public Boolean isOpen()
{
return _bOpen;
}
public Boolean ReadScoop(int scoop, long totalNonces, long startNonce, Scoop target, int limit)
{
_lPosition = scoop * (64 * totalNonces) + startNonce * 64;
try
{
_fs.Seek(_lPosition, SeekOrigin.Begin);
}
catch (Exception e)
{
MessageBox.Show("Scoop: " + scoop.ToString() + " " + e.Message, "I/O Error - Seek to read", MessageBoxButtons.OK);
return false;
}
try
{
//interrupt avoider 1mb read 64*16384
for (int i = 0;i< limit * 64;i+=(64*16384))
_fs.Read(target.byteArrayField, i, Math.Min(64 * 16384,limit * 64-i));
}
catch (Exception e)
{
MessageBox.Show("Scoop: " + scoop.ToString() + " " + e.Message, "I/O Error - Read", MessageBoxButtons.OK);
return false;
}
_lPosition += limit * 64;
return true;
}
public Boolean WriteScoop(int scoop, long totalNonces, long startNonce, Scoop source, int limit)
{
_lPosition = scoop * (64 * totalNonces) + startNonce * 64;
try
{
_ft.Seek(_lPosition, SeekOrigin.Begin);
}
catch (Exception e)
{
MessageBox.Show("Scoop: "+scoop.ToString()+" "+e.Message, "I/O Error - Seek to write", MessageBoxButtons.OK);
return false;
}
try {
//interrupt avoider 1mb read 64*16384
for (int i = 0; i < limit * 64; i += (64 * 16384))
_ft.Write(source.byteArrayField, i, Math.Min(64 * 16384, limit * 64 - i));
}
catch (Exception e)
{
MessageBox.Show("Scoop: " + scoop.ToString() + " " + e.Message, "I/O Error - Write", MessageBoxButtons.OK);
return false;
}
_lPosition += limit * 64;
return true;
}
}
public static class Privileges
{
private static int asserted = 0;
private static bool hasBackupPrivileges = false;
public static bool HasAdminPrivileges
{
get { return AssertPriveleges(); }
}
private static bool AssertPriveleges()
{
bool success = false;
var wasAsserted = Interlocked.CompareExchange(ref asserted, 1, 0);
if (wasAsserted == 0)
{
success = AssertPrivelege(NativeMethods.SE_MANAGE_VOLUME_NAME);
hasBackupPrivileges = success;
}
return hasBackupPrivileges;
}
private static bool AssertPrivelege(string privelege)
{
IntPtr token;
var tokenPrivileges = new NativeMethods.TOKEN_PRIVILEGES();
tokenPrivileges.Privileges = new NativeMethods.LUID_AND_ATTRIBUTES[1];
var success =
NativeMethods.OpenProcessToken(NativeMethods.GetCurrentProcess(), NativeMethods.TOKEN_ADJUST_PRIVILEGES, out token)
&&
NativeMethods.LookupPrivilegeValue(null, privelege, out tokenPrivileges.Privileges[0].Luid);
try
{
if (success)
{
tokenPrivileges.PrivilegeCount = 1;
tokenPrivileges.Privileges[0].Attributes = NativeMethods.SE_PRIVILEGE_ENABLED;
success =
NativeMethods.AdjustTokenPrivileges(token, false, ref tokenPrivileges, Marshal.SizeOf(tokenPrivileges), IntPtr.Zero, IntPtr.Zero)
&&
(Marshal.GetLastWin32Error() == 0);
}
if (!success)
{
Console.WriteLine("Could not assert privilege: " + privelege);
}
}
finally
{
NativeMethods.CloseHandle(token);
}
return success;
}
}
internal class NativeMethods
{
internal const int ERROR_HANDLE_EOF = 38;
//--> Privilege constants....
internal const UInt32 SE_PRIVILEGE_ENABLED = 0x00000002;
internal const string SE_MANAGE_VOLUME_NAME = "SeManageVolumePrivilege";
//--> For starting a process in session 1 from session 0...
internal const uint TOKEN_ADJUST_PRIVILEGES = 0x0020;
[DllImport("advapi32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool OpenProcessToken(IntPtr ProcessHandle, UInt32 DesiredAccess, out IntPtr TokenHandle);
[DllImport("kernel32.dll")]
internal static extern IntPtr GetCurrentProcess();
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool LookupPrivilegeValue(string lpSystemName, string lpName, out LUID lpLuid);
[DllImport("advapi32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool AdjustTokenPrivileges(IntPtr TokenHandle, [MarshalAs(UnmanagedType.Bool)]bool DisableAllPrivileges, ref TOKEN_PRIVILEGES NewState, Int32 BufferLength, IntPtr PreviousState, IntPtr ReturnLength);
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool CloseHandle(IntPtr hObject);
[StructLayout(LayoutKind.Sequential)]
internal struct LUID
{
public UInt32 LowPart;
public Int32 HighPart;
}
[StructLayout(LayoutKind.Sequential)]
internal struct LUID_AND_ATTRIBUTES
{
public LUID Luid;
public UInt32 Attributes;
}
internal struct TOKEN_PRIVILEGES
{
public UInt32 PrivilegeCount;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]
public LUID_AND_ATTRIBUTES[] Privileges;
}
}
}