-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
161 lines (148 loc) · 5.83 KB
/
Program.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
using System.Text;
using System.IO.Compression;
namespace pradosh_arduino
{
class Chiefwood
{
string version = "v1.0";
string simple_version = "1";
static int compressLevel = 2; // Defaults to none
static void Main(string[] args)
{
Chiefwood cw = new Chiefwood();
if (args.Length == 0)
{
Console.WriteLine("Please pass some arguments.");
return;
}
for (int i = 0; i < args.Length; i++)
{
if (args[i].Contains("-create"))
{
Console.WriteLine("Creating " + args[i + 1] + ".cw" + cw.simple_version + " file.");
cw.CreateCWF(args[i + 1]);
}
else if (args[i].Contains("-load"))
{
Console.WriteLine("Reading " + args[i + 1] + ".cw" + cw.simple_version + " file.");
cw.ReadCWF(args[i + 1]);
}
else if (args[0].Contains("-compression"))
{
if (args[1] == "none")
{
compressLevel = 2;
}
else if (args[1] == "fast")
{
compressLevel = 1;
}
else if (args[1] == "optimal")
{
compressLevel = 0;
}
else if (args[1] == "best")
{
compressLevel = 3;
}
else
{
Console.WriteLine("Only 'none, fast, optimal, best' is allowed for compression.");
Environment.Exit(-1);
}
}
else if (args[i].Contains("-help"))
{
Console.WriteLine("Welcome to Chiefwood Compressor - Here is the help\n\t-create <name>\t This argument is used to create a Chiefwood file (no file extension)\n\t-load <name>\t Loads a Chiefwood file (no file extension)\n\t-compression <level>\tself explainable, \'none, fast, optimal, best\'");
}
}
}
public void CreateCWF(string name)
{
try
{
// Create Chiefwood file
using (BinaryWriter binaryWriter = new BinaryWriter(File.Open(name + ".cw" + simple_version, FileMode.Create)))
{
binaryWriter.Write("cw " + version);
binaryWriter.Write(Directory.GetFiles("./contents").Length);
foreach (string fileName in Directory.GetFiles("./contents"))
{
binaryWriter.Write(EncryptString(fileName));
binaryWriter.Write(EncryptString(File.ReadAllText(fileName)));
}
}
}
catch (IOException)
{
Console.WriteLine("File is being used by another process. (I/O Exception)");
}
}
public void ReadCWF(string name)
{
using (BinaryReader binaryReader = new BinaryReader(File.Open(name + ".cw" + simple_version, FileMode.Open)))
{
string header = binaryReader.ReadString();
if (header.Contains("cw"))
{
if (header.Contains(version))
{
int totalFiles = binaryReader.ReadInt32();
for (int i = 0; i < totalFiles; i++)
{
string dec_filename = DecryptString(binaryReader.ReadString()).Replace("./contents\\", "");
Console.WriteLine("Found file " + dec_filename + " Decrypting...");
File.Create(dec_filename).Close();
File.WriteAllText(dec_filename, DecryptString(binaryReader.ReadString()));
}
}
else
{
Console.WriteLine("Invalid Version!");
return;
}
}
else
{
Console.WriteLine("Invalid Signature!");
return;
}
}
}
public string DecryptString(string base64EncodedData)
{
var base64EncodedBytes = Decompress(System.Convert.FromBase64String(base64EncodedData));
return Encoding.UTF8.GetString(base64EncodedBytes);
}
public string EncryptString(string plainText)
{
var plainTextBytes = Compress(Encoding.UTF8.GetBytes(plainText));
return System.Convert.ToBase64String(plainTextBytes);
}
public byte[] Compress(byte[] bytes)
{
using (var memoryStream = new MemoryStream())
{
using (var brotliStream = new BrotliStream(memoryStream, (CompressionLevel)Enum.ToObject(typeof(CompressionLevel), compressLevel)))
{
brotliStream.Write(bytes, 0, bytes.Length);
}
return memoryStream.ToArray();
}
}
public byte[] Decompress(byte[] bytes)
{
using (var memoryStream = new MemoryStream(bytes))
{
using (var outputStream = new MemoryStream())
{
using (var decompressStream = new BrotliStream(memoryStream, CompressionMode.Decompress))
{
decompressStream.CopyTo(outputStream);
}
return outputStream.ToArray();
}
}
}
}
}