-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathScoopReadWriterX.cs
176 lines (164 loc) · 5.92 KB
/
ScoopReadWriterX.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
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 ScoopReadWriterX
{
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool SetFileValidData(SafeFileHandle hFile, long ValidDataLength);
protected string _FileName;
private long _lLength = -1;
protected bool _bOpen = false;
protected FileStream _fs;
private long _lPosition = 0;
FileOptions FileFlagNoBuffering = (FileOptions)0x20000000;
//inline constructor
public ScoopReadWriterX(string stFileName)
{
_FileName = stFileName;
}
public void Close()
{
_bOpen = false;
_fs.Close();
}
public Boolean OpenR(bool directIO)
{
try
{
if (directIO)
{
_fs = new FileStream(_FileName, FileMode.Open, FileAccess.Read, FileShare.Read, 1048576, FileFlagNoBuffering);
}
else
{
_fs = new FileStream(_FileName, FileMode.Open, FileAccess.Read, FileShare.Read, 1048576, FileOptions.SequentialScan);
}
}
catch (Exception e)
{
//MessageBox.Show(e.Message, "Error Opening File", MessageBoxButtons.OK);
if (_fs != null) _fs.Close();
return false;
}
_lPosition = 0;
_bOpen = true;
return true;
}
public Boolean OpenW(bool directIO)
{
try
{
//assert priviliges
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(_FileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None, 1048576, FileFlagNoBuffering);
}
else
{
_fs = new FileStream(_FileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None, 1048576, FileOptions.WriteThrough);
}
}catch (Exception e)
{
//MessageBox.Show(e.Message, "Error Opening File", MessageBoxButtons.OK);
if (_fs != null) _fs.Close();
return false;
}
_lPosition = 0;
_lLength = _fs.Length;
_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
{
_fs.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))
_fs.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 Boolean PreAlloc(long totalNonces)
{
try
{
_fs.SetLength(totalNonces * (2 << 17));
}
catch (Exception e)
{
MessageBox.Show(e.Message, "Error Preallocating File", MessageBoxButtons.OK);
return false;
}
bool test = SetFileValidData(_fs.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;
}
}
}