-
Notifications
You must be signed in to change notification settings - Fork 0
/
Worker.cs
166 lines (125 loc) · 4.93 KB
/
Worker.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace OpenPGPExplorer
{
public delegate void AddCompleteDelegate(string FileName, ByteBlock StartBlock);
public delegate void ProcessCompleteDelegate(ByteBlock BB, TreeNode tn);
public static class Worker
{
public static event AddCompleteDelegate AddComplete;
public static event ProcessCompleteDelegate ProcessComplete;
public static event StatusUpdates StatusUpdate;
public static event ErrorDelegate Error;
private static Thread WorkerThread;
private class BlockInfo
{
public ByteBlock BB;
public TreeNode tn;
}
public static void AddFile(string FileName)
{
if (IsProcessing())
return;
WorkerThread = new Thread(new ParameterizedThreadStart(AddFile));
WorkerThread.SetApartmentState(ApartmentState.STA);
WorkerThread.Start(FileName);
}
public static bool ProcessBlock(ByteBlock BB, TreeNode tn)
{
if (IsProcessing())
return false;
WorkerThread = new Thread(new ParameterizedThreadStart(DoProcessBlock));
WorkerThread.SetApartmentState(ApartmentState.STA);
WorkerThread.Start(new BlockInfo { BB = BB, tn = tn });
return true;
}
private static bool IsProcessing()
{
if (WorkerThread != null)
if (WorkerThread.IsAlive)
{
Error?.Invoke("Please wait for the process to complete", false);
return true;
}
return false;
}
private static void DoProcessBlock(object BlockInfo)
{
try
{
var Info = (BlockInfo)BlockInfo;
Info.BB.StatusUpdate += StatusUpdate;
Info.BB.ProcessBlock();
ProcessComplete?.Invoke(Info.BB, Info.tn);
}
catch (Exception ex)
{
Error?.Invoke(ex.Message, true);
}
}
private static void AddFile(object OFullFileName)
{
try
{
string FullFileName = OFullFileName.ToString();
var FileName = Path.GetFileName(FullFileName);
string BinaryGPGFile = GetBinaryFile(FullFileName);
ByteBlock StartBlock = OpenPGP.Process(BinaryGPGFile);
OpenPGP.Validate();
AddComplete?.Invoke(FileName, StartBlock);
}
catch (Exception ex)
{
Error?.Invoke(ex.Message, true);
}
}
private static string GetBinaryFile(string FileName)
{
using (FileStream sr = new FileStream(FileName, FileMode.Open, FileAccess.Read))
{
int FirstByte = sr.ReadByte();
if ((FirstByte & 0x80) != 0)
return FileName;
}
// Assuming ASCII Armored Text File
using (StreamReader reader = File.OpenText(FileName))
{
string line = reader.ReadLine();
if (!line.StartsWith("-----"))
throw new InvalidDataException("Not a valid Open PGP file");
FileInfo fi = new FileInfo(FileName);
if (fi.Length > Program.MAX_ARMORED_LENGTH)
throw new InvalidDataException("ASCII-Armored file too big, please convert to binary and try again");
while (!reader.EndOfStream && !string.IsNullOrWhiteSpace(reader.ReadLine())) ;
StringBuilder sb = new StringBuilder();
long CheckSum = 0;
while (!reader.EndOfStream)
{
line = reader.ReadLine();
if (line.StartsWith("="))
{
byte[] CheckSumBytes = Convert.FromBase64String(line.Substring(1));
CheckSum = Program.GetBigEndian(CheckSumBytes, 0, CheckSumBytes.Length);
break;
}
else if (string.IsNullOrWhiteSpace(line))
break;
sb.Append(line);
}
byte[] DataBytes = Convert.FromBase64String(sb.ToString());
long CRC = Program.GetCRC24(DataBytes);
if (CRC != CheckSum)
throw new InvalidDataException("ASCII-Armored checksum failed, file may be corrupt");
string TempFileName = TempFiles.GetNewTempFile();
File.WriteAllBytes(TempFileName, DataBytes);
return TempFileName;
}
}
}
}