-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
356 lines (322 loc) · 14.4 KB
/
Copy pathProgram.cs
File metadata and controls
356 lines (322 loc) · 14.4 KB
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
using BinArchiver.Version;
using ProtoBuf;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace BinArchiver
{
class Program
{
static void Main(string[] args)
{
// TODO: Add extra option so that the checksums become invalid? Or that you can overwrite them with a own value?
bool result = true;
if (args.Length < 2)
{
Console.WriteLine("bin-archiver version {0}", GitVersionRetreiver.getVersion());
Console.WriteLine("Archives one or more binary files into a single file. You can find help and usage examples on: https://github.com/Sunib/bin-archiver");
}
else
{
// Using enumerator so that we can read more parameters when we need them inside the loop.
var enumerator = args.Cast<string>().GetEnumerator();
ArchiveAppender archiveAppender = new ArchiveAppender();
string archiveFilename = null;
result = Process(enumerator, out archiveFilename, archiveAppender);
if (result)
{
if (archiveAppender.Finish())
{
Console.WriteLine("Succesfully written bytes to {0}", archiveFilename);
}
}
else
{
Console.WriteLine("Program terminating.");
}
}
}
static bool Process(IEnumerator<string> parameters, out string archiveFilename, ArchiveAppender archiveAppender)
{
bool fileExists = false;
bool result = true;
archiveFilename = null;
while (parameters.MoveNext() && result)
{
var arg = parameters.Current;
if (archiveFilename == null)
{
// Check if the file exists and then start using it!
archiveFilename = arg.ToString();
fileExists = File.Exists(archiveFilename);
}
else
{
if (arg == "-c")
{
if (parameters.MoveNext())
{
if (!fileExists)
{
if (CreateArchive(archiveFilename, parameters.Current))
{
fileExists = true;
archiveAppender.Prepare(archiveFilename);
}
else
{
Console.WriteLine("Failed to create archive for unknown reason.");
result = false;
}
}
else
{
Console.WriteLine("Cannot create archive, file already exists!");
}
}
else
{
Console.WriteLine("Failed to create archive, version string not given.");
result = false;
}
}
else if (arg == "-o")
{
if (fileExists)
{
File.Delete(archiveFilename);
fileExists = File.Exists(archiveFilename);
if (fileExists)
{
Console.WriteLine("Failed to remove file, locked?");
result = false;
}
}
}
else if (arg == "-k")
{
if (parameters.MoveNext())
{
archiveAppender.Key = Convert.FromBase64String(parameters.Current);
}
else
{
result = false;
}
}
else if (arg == "-r")
{
if (parameters.MoveNext())
{
archiveAppender.RestrictToValue = Convert.ToUInt32(parameters.Current);
}
else
{
result = false;
}
}
else if (arg == "-a")
{
if (parameters.MoveNext())
{
archiveAppender.Filename = parameters.Current;
if (parameters.MoveNext())
{
archiveAppender.Version = parameters.Current;
// Read the type values from the command line until you can't parse it anymore.
archiveAppender.TypeValues = new List<UInt32>();
bool isReading = true;
while (isReading && parameters.MoveNext())
{
UInt32 value;
if (UInt32.TryParse(parameters.Current, out value))
{
archiveAppender.TypeValues.Add(value);
}
else
{
Console.WriteLine("Multiple actions in one go not yet supported.");
archiveAppender.TypeValues.Clear();
isReading = false;
result = false;
}
}
if (archiveAppender.TypeValues.Count > 0)
{
archiveAppender.Prepare(archiveFilename);
if (archiveAppender.Append())
{
Console.WriteLine("Appended {0} to {1}", archiveAppender.Filename, archiveFilename);
}
else
{
Console.WriteLine("Failed to append {0} to {1}", archiveAppender.Filename, archiveFilename);
result = false;
}
}
else
{
Console.WriteLine("Append failed, no valid type values found");
result = false;
}
}
else
{
Console.WriteLine("Append failed, version not given");
result = false;
}
}
else
{
Console.WriteLine("Append failed, filename not given");
result = false;
}
}
else if (arg == "-d")
{
TestArchive(archiveFilename, archiveAppender.Key);
}
else
{
Console.WriteLine("Unknown/invalid input parameter {0}", arg);
}
}
}
return result;
}
public static bool CreateArchive(string filename, string version)
{
// The GeneratedPorotocolBufferClasses.cs is generated from the .proto file (should include this in the build steps).
// Copy the .proto to D:\git\protobuf-net\ProtoGen\bin\Debug
// Run this command: D:\git\protobuf-net\ProtoGen\bin\Debug>protogen -i:bin_archive.proto -ns:BinArchiver -o:GeneratedProtocolBufferClasses.cs
// Now copy output.cs to this local project. Will do for the moment.
var binArchive = new BinArchive();
binArchive.version = version;
binArchive.time = Helpers.convertToUnixTimeMilliseconds(DateTime.UtcNow);
// We can crypt the firmware file, and we cannot crypt it. Supply them both and see if you can get back the same thing twice, would be great.
using (var file = File.Create(filename))
{
Serializer.Serialize(file, binArchive);
}
return true;
}
public static void TestArchive(string filename, byte[] key = null)
{
// Prepare our encryption object, if we have a key.
BinFileCrypter binFileCrypter = null;
if (key != null)
{
binFileCrypter = new BinFileCrypter(key);
}
// Start reading the file!
using (var fileStream = File.OpenRead(filename))
{
Console.WriteLine("Loading and parsing {0} (size={1})", filename, fileStream.Length);
var binArchive = Serializer.Deserialize<BinArchive>(fileStream);
Console.WriteLine("Completed bin-archive parse, contains {0} normal files and {1} crypted files.", binArchive.binFiles.Count, binArchive.cryptedBinFiles.Count);
Console.WriteLine("Version={0}", binArchive.version);
Console.WriteLine("Time={0}", Helpers.convertUnixTimeMillisecondsToDateTime(binArchive.time).ToString());
Console.WriteLine("");
foreach (var binFile in binArchive.binFiles)
{
PrintBinFile(binFile);
}
foreach (var cryptedBinFile in binArchive.cryptedBinFiles)
{
Console.WriteLine("Found crypted file (restrictToValue={0}, IV={1}, size={2}), trying to decrypt...",
cryptedBinFile.restrictToValue,
Convert.ToBase64String(cryptedBinFile.aesInitializationVector),
cryptedBinFile.cryptedBinFile.Length);
if (binFileCrypter != null)
{
var binFile = binFileCrypter.Decrypt(cryptedBinFile);
if (binFile != null)
{
PrintBinFile(binFile);
}
else
{
Console.WriteLine("Failed to decrypt.");
}
}
else
{
Console.WriteLine("Decrypt failed: No valid -k supplied, ");
}
}
}
}
/// <summary>
/// Prints some debug info about a bin file.
/// </summary>
public static void PrintBinFile(BinFile binFile)
{
Console.WriteLine("Valid BinFile found (version={0}, size={1}, time={2})",
binFile.version,
binFile.size,
Helpers.convertUnixTimeMillisecondsToDateTime(binFile.time).ToString());
Console.WriteLine("File can be applied to type values: ");
binFile.typeValues.ForEach(v => Console.Write("{0} ", v));
Console.Write("\n");
var savedCrc = binFile.crc;
var calculatedCrc = ArchiveAppender.calculateCrc32(binFile.content);
var savedSha = Convert.ToBase64String(binFile.sha1);
var calculatedSha = Convert.ToBase64String(ArchiveAppender.calculateSha1(binFile.content));
Console.WriteLine("Hashes: CRC-32={0}, SHA-1={1}, Check={2}",
savedCrc,
savedSha,
binFile.check);
Console.WriteLine("Expected hashes: CRC-32={0}, SHA-1={1}, Check=3141592653",
calculatedCrc,
calculatedSha);
if (savedCrc != calculatedCrc)
{
Console.WriteLine("CRC ERROR!!!");
}
if (savedSha != calculatedSha)
{
Console.WriteLine("SHA1 ERROR!!!");
}
}
public static byte[] StringToByteArray(String hex)
{
int NumberChars = hex.Length / 2;
byte[] bytes = new byte[NumberChars];
using (var sr = new StringReader(hex))
{
for (int i = 0; i < NumberChars; i++)
bytes[i] =
Convert.ToByte(new string(new char[2] { (char)sr.Read(), (char)sr.Read() }), 16);
}
return bytes;
}
/// <summary>
/// Test function that reads a complete file and calculates and prints it's CRC.
/// </summary>
/// <param name="filename">Path to the filename that you want to read.</param>
/// <returns>The calculated CRC-32</returns>
public static UInt32 calculateCrc32(string filename)
{
Crc32 crc32 = new Crc32();
UInt32 result;
using (FileStream fs = File.Open(filename, FileMode.Open))
{
var hashBytes = crc32.ComputeHash(fs);
if (hashBytes.Length != 4)
throw new InvalidProgramException("Whoops, crc32.ComputeHash() did not returned 4 bytes. I thought that it always would!");
// Reverse the bytes if needed, ComputeHash() returns big endian!
if (BitConverter.IsLittleEndian)
{
hashBytes = hashBytes.Reverse().ToArray();
}
result = BitConverter.ToUInt32(hashBytes, 0);
}
Console.WriteLine("CRC-32 is {0:x}", result);
return result;
}
}
}