forked from ttgzs/iedexplorer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIniFileManager.cs
More file actions
306 lines (268 loc) · 11.7 KB
/
IniFileManager.cs
File metadata and controls
306 lines (268 loc) · 11.7 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
/* Mkulu Ini File Manager
* Copyright (c) 2010 Chad Ullman
*
* This software is provided 'as-is', without any express or implied warranty. In
* no event will the authors be held liable for any damages arising from the use
* of this software.
*
* Permission is granted to anyone to use this software for any purpose, including
* commercial applications, and to alter it and redistribute it freely, subject to
* the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not claim
* that you wrote the original software. If you use this software in a product,
* an acknowledgment in the product documentation would be appreciated but is
* not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source distribution.
*
*
* This class provides a simple interface to config data stored in the now classic Microsoft .ini file
* format. Originally the class used the Win32 Get/WritePrivateProfileString functions, but that was later
* changed to the current implementation, where the class parses the config file itself. Blank lines are
* ignored, as are any lines starting with a semi-colon (;) or REM. The class supports encrypting the config file.
*
* Any comments in the config file will be lost once it has been processed by this class.
*
* The config file is saved to disk every time you use one of the writeXXX functions. While this is somewhat
* inefficient, realistically we are not going to be writing out thosands of config entries all the time, and in
* real world use the benefits of never having to worry about flushing the updates to disk outweigh any nano-second
* performance benefits we might get by implemeting a transaction/commit system.
*
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Diagnostics;
namespace org.mkulu.config {
#region IniFileManager
public class IniFileManager {
#region Data Members
private string configFileName;
private byte[] cryptoKey;
private byte[] cryptoInitializationVector;
private bool fileIsEncrypted;
private AesManaged aes;
private SortedDictionary<string, StringDictionary> cache = new SortedDictionary<string, StringDictionary>(new IniCacheSorter<string>());
#endregion
#region Constructors
public IniFileManager(string fileToManage) {
configFileName = fileToManage;
fileIsEncrypted = false;
cacheIniFile();
}
public IniFileManager(string fileToManage, string key) {
configFileName = fileToManage;
fileIsEncrypted = true;
aes = new AesManaged();
aes.BlockSize = 128;
aes.KeySize = 256;
cryptoKey = ASCIIEncoding.ASCII.GetBytes(key);
cryptoInitializationVector = cryptoKey;
if (cryptoInitializationVector.Length != (aes.BlockSize / 8)) {
Array.Resize(ref cryptoInitializationVector, (aes.BlockSize / 8));
}
if (cryptoKey.Length != (aes.KeySize / 8)) {
Array.Resize(ref cryptoKey, (aes.KeySize / 8));
}
cacheIniFile();
}
#endregion
#region Encryption Utilities (from http://remy.supertext.ch/2011/01/simple-c-encryption-and-decryption/)
private string Encrypt(string source) {
if (String.IsNullOrEmpty(source)) {
return "";
}
ICryptoTransform encryptor = aes.CreateEncryptor(cryptoKey, cryptoInitializationVector);
try {
byte[] sourceBytes = ASCIIEncoding.ASCII.GetBytes(source);
byte[] encryptedSource = encryptor.TransformFinalBlock(sourceBytes, 0, sourceBytes.Length);
return Convert.ToBase64String(encryptedSource);
} catch (CryptographicException cex) {
throw new Exception("Unable to encrypt ini file: " + cex.Message, cex);
} catch (Exception) {
throw;
}
}
private string Decrypt(string source) {
if (String.IsNullOrEmpty(source)) {
return "";
}
ICryptoTransform decryptor = aes.CreateDecryptor(cryptoKey, cryptoInitializationVector);
try {
byte[] encryptedSourceBytes = Convert.FromBase64String(source);
byte[] sourceBytes = decryptor.TransformFinalBlock(encryptedSourceBytes, 0, encryptedSourceBytes.Length);
return ASCIIEncoding.ASCII.GetString(sourceBytes);
} catch (CryptographicException cex) {
throw new Exception("Unable to decrypt ini file: " + cex.Message, cex);
} catch (Exception) {
throw;
}
}
#endregion
#region Cache Handling
private void cacheIniFile() {
string configData;
if (!File.Exists(configFileName)) {
return;
}
configData = File.ReadAllText(configFileName);
//It is always possible to pass in an unencrypted ini file which we want to encrypt, so we need to check for that
int encryptionIndicator = configData.IndexOf(Environment.NewLine);
if (encryptionIndicator == -1 && !fileIsEncrypted) {
throw new Exception("Config file is encrypted, but no key provided");
}
if (encryptionIndicator == -1 && fileIsEncrypted) {
configData = Decrypt(configData);
}
string[] lines = configData.Split(new string[] {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries);
StringDictionary sectionData = null;
string sectionName = "";
Match res;
foreach (string line in lines) {
Debug.WriteLine("Processing line " + line);
if (Regex.IsMatch(line, @"^\s*;") || Regex.IsMatch(line, @"^\s*rem\s+", RegexOptions.IgnoreCase)) { //comment line, skip
Debug.WriteLine(" line is a comment, skipping");
continue;
} else if (Regex.IsMatch(line, @"^\[.*\]")) { //section header
Debug.WriteLine(" line is a section header, processing");
if (sectionData != null) {
cache.Add(sectionName, sectionData);
}
res = Regex.Match(line, @"\[(.*)\]");
sectionName = res.Groups[1].Value;
//sectionName = sectionName.ToLower();
Debug.WriteLine(" sectionName = " + sectionName);
sectionData = new StringDictionary();
} else {
Debug.WriteLine(" line may be section data");
Match parts = Regex.Match(line, @"^([^=]*)=(.*)");
if (parts.Groups.Count != 3 || sectionData == null) {
Debug.WriteLine(" nope, this is in fact not section data");
continue;
}
Debug.WriteLine(" " + parts.Groups[1].Value.Trim() + "=" + parts.Groups[2].Value.Trim());
sectionData.Add(parts.Groups[1].Value.Trim(), parts.Groups[2].Value.Trim());
}
}
// the last section we processed won't have been saved yet, so do that now
cache.Add(sectionName, sectionData);
}
private void dumpCache() {
StringBuilder configData = new StringBuilder();
foreach (KeyValuePair<string, StringDictionary> section in cache) {
configData.AppendLine("[" + section.Key + "]");
foreach (DictionaryEntry data in section.Value) {
configData.AppendLine(String.Format("{0}={1}", data.Key, data.Value));
}
configData.AppendLine();
}
string config = configData.ToString();
//dump cache in to configData
if (fileIsEncrypted) {
config = Encrypt(config);
}
File.WriteAllText(configFileName, config);
}
#endregion
#region Data Access - Read
public string getString(string section, string key, string defaultValue) {
key = key.ToLower();
if (!cache.ContainsKey(section)) {
return defaultValue;
}
StringDictionary sectionData = (StringDictionary)cache[section];
if (!sectionData.ContainsKey(key)) {
return defaultValue;
}
return sectionData[key];
}
public int getInt(string section, string key, int defaultValue) {
string value = getString(section, key, defaultValue.ToString());
int result;
try {
result = Convert.ToInt32(value);
return result;
} catch {
return defaultValue;
}
}
public bool getBool(string section, string key, bool defaultValue) {
string value = getString(section, key, defaultValue.ToString());
bool result;
if (Boolean.TryParse(value, out result)) {
return result;
} else {
return defaultValue;
}
}
public List<string> getSections() {
return cache.Keys.ToList<string>();
}
public StringDictionary getSection(string section) {
if (cache.Keys.Contains(section)) {
return cache[section];
} else {
return null;
}
}
#endregion
#region Data Access - Write
public void writeString(string section, string key, string value) {
if (!cache.ContainsKey(section)) {
cache.Add(section, new StringDictionary());
}
StringDictionary sectionData = (StringDictionary)cache[section];
if (!sectionData.ContainsKey(key)) {
sectionData.Add(key, value);
} else {
sectionData[key] = value;
}
dumpCache();
}
public void writeInt(string section, string key, int value) {
writeString(section, key, value.ToString());
}
public void writeBool(string section, string key, bool value) {
writeString(section, key, value.ToString());
}
#endregion
#region Data Access - Delete
public void deleteSection(string section) {
if (cache.ContainsKey(section)) {
cache.Remove(section);
dumpCache();
}
}
public void deleteKey(string section, string key) {
if (cache.ContainsKey(section) && ((StringDictionary)cache[section]).ContainsKey(key)) {
((StringDictionary)(cache[section])).Remove(key);
dumpCache();
}
}
#endregion
}
#endregion
#region IniCacheSorter
public class IniCacheSorter<T> : IComparer<T> where T: IComparable<T> {
// Return values
// Less than zero: x is less than y.
// Zero: x equals y.
// Greater than zero: x is greater than y.
public int Compare(T x, T y) {
string first = x as string;
string second = y as string;
return string.Compare(first, second, true); //case insensitive comparison
}
}
#endregion
}