File: VariantDictionary.cs

package info (click to toggle)
keepass2 2.57%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 14,520 kB
  • sloc: cs: 120,930; xml: 6,271; cpp: 322; sh: 53; makefile: 42
file content (417 lines) | stat: -rw-r--r-- 10,309 bytes parent folder | download | duplicates (3)
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*
  KeePass Password Safe - The Open-Source Password Manager
  Copyright (C) 2003-2024 Dominik Reichl <dominik.reichl@t-online.de>

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;

using KeePassLib.Resources;
using KeePassLib.Utility;

namespace KeePassLib.Collections
{
	public class VariantDictionary : ICloneable
	{
		private const ushort VdVersion = 0x0100;
		private const ushort VdmCritical = 0xFF00;

		private readonly Dictionary<string, object> m_d = new Dictionary<string, object>();

		private enum VdType : byte
		{
			None = 0,

			// Byte = 0x02,
			// UInt16 = 0x03,
			UInt32 = 0x04,
			UInt64 = 0x05,

			// Signed mask: 0x08
			Bool = 0x08,
			// SByte = 0x0A,
			// Int16 = 0x0B,
			Int32 = 0x0C,
			Int64 = 0x0D,

			// Float = 0x10,
			// Double = 0x11,
			// Decimal = 0x12,

			// Char = 0x17, // 16-bit Unicode character
			String = 0x18,

			// Array mask: 0x40
			ByteArray = 0x42
		}

		public int Count
		{
			get { return m_d.Count; }
		}

		public VariantDictionary()
		{
		}

		private bool Get<T>(string strName, out T t)
		{
			t = default(T);

			if(string.IsNullOrEmpty(strName)) { Debug.Assert(false); return false; }

			object o;
			if(!m_d.TryGetValue(strName, out o)) return false; // No assert

			if(o == null) { Debug.Assert(false); return false; }
			if(o.GetType() != typeof(T)) { Debug.Assert(false); return false; }

			t = (T)o;
			return true;
		}

		private void SetStruct<T>(string strName, T t)
			where T : struct
		{
			if(string.IsNullOrEmpty(strName)) { Debug.Assert(false); return; }

#if DEBUG
			T tEx;
			Get<T>(strName, out tEx); // Assert same type
#endif

			m_d[strName] = t;
		}

		private void SetRef<T>(string strName, T t)
			where T : class
		{
			if(string.IsNullOrEmpty(strName)) { Debug.Assert(false); return; }
			if(t == null) { Debug.Assert(false); return; }

#if DEBUG
			T tEx;
			Get<T>(strName, out tEx); // Assert same type
#endif

			m_d[strName] = t;
		}

		public bool Remove(string strName)
		{
			if(string.IsNullOrEmpty(strName)) { Debug.Assert(false); return false; }

			return m_d.Remove(strName);
		}

		public void CopyTo(VariantDictionary d)
		{
			if(d == null) { Debug.Assert(false); return; }

			// Do not clear the target
			foreach(KeyValuePair<string, object> kvp in m_d)
			{
				d.m_d[kvp.Key] = kvp.Value;
			}
		}

		public Type GetTypeOf(string strName)
		{
			if(string.IsNullOrEmpty(strName)) { Debug.Assert(false); return null; }

			object o;
			m_d.TryGetValue(strName, out o);
			if(o == null) return null; // No assert

			return o.GetType();
		}

		public uint GetUInt32(string strName, uint uDefault)
		{
			uint u;
			if(Get<uint>(strName, out u)) return u;
			return uDefault;
		}

		public void SetUInt32(string strName, uint uValue)
		{
			SetStruct<uint>(strName, uValue);
		}

		public ulong GetUInt64(string strName, ulong uDefault)
		{
			ulong u;
			if(Get<ulong>(strName, out u)) return u;
			return uDefault;
		}

		public void SetUInt64(string strName, ulong uValue)
		{
			SetStruct<ulong>(strName, uValue);
		}

		public bool GetBool(string strName, bool bDefault)
		{
			bool b;
			if(Get<bool>(strName, out b)) return b;
			return bDefault;
		}

		public void SetBool(string strName, bool bValue)
		{
			SetStruct<bool>(strName, bValue);
		}

		public int GetInt32(string strName, int iDefault)
		{
			int i;
			if(Get<int>(strName, out i)) return i;
			return iDefault;
		}

		public void SetInt32(string strName, int iValue)
		{
			SetStruct<int>(strName, iValue);
		}

		public long GetInt64(string strName, long lDefault)
		{
			long l;
			if(Get<long>(strName, out l)) return l;
			return lDefault;
		}

		public void SetInt64(string strName, long lValue)
		{
			SetStruct<long>(strName, lValue);
		}

		public string GetString(string strName)
		{
			string str;
			Get<string>(strName, out str);
			return str;
		}

		public void SetString(string strName, string strValue)
		{
			SetRef<string>(strName, strValue);
		}

		public byte[] GetByteArray(string strName)
		{
			byte[] pb;
			Get<byte[]>(strName, out pb);
			return pb;
		}

		public void SetByteArray(string strName, byte[] pbValue)
		{
			SetRef<byte[]>(strName, pbValue);
		}

		/// <summary>
		/// Create a deep copy.
		/// </summary>
		public virtual object Clone()
		{
			VariantDictionary vdNew = new VariantDictionary();

			foreach(KeyValuePair<string, object> kvp in m_d)
			{
				object o = kvp.Value;
				if(o == null) { Debug.Assert(false); continue; }

				Type t = o.GetType();
				if(t == typeof(byte[]))
				{
					byte[] p = (byte[])o;
					byte[] pNew = new byte[p.Length];
					if(p.Length > 0) Array.Copy(p, pNew, p.Length);

					o = pNew;
				}

				vdNew.m_d[kvp.Key] = o;
			}

			return vdNew;
		}

		public static byte[] Serialize(VariantDictionary p)
		{
			if(p == null) { Debug.Assert(false); return null; }

			byte[] pbRet;
			using(MemoryStream ms = new MemoryStream())
			{
				MemUtil.Write(ms, MemUtil.UInt16ToBytes(VdVersion));

				foreach(KeyValuePair<string, object> kvp in p.m_d)
				{
					string strName = kvp.Key;
					if(string.IsNullOrEmpty(strName)) { Debug.Assert(false); continue; }
					byte[] pbName = StrUtil.Utf8.GetBytes(strName);

					object o = kvp.Value;
					if(o == null) { Debug.Assert(false); continue; }

					Type t = o.GetType();
					VdType vt = VdType.None;
					byte[] pbValue = null;
					if(t == typeof(uint))
					{
						vt = VdType.UInt32;
						pbValue = MemUtil.UInt32ToBytes((uint)o);
					}
					else if(t == typeof(ulong))
					{
						vt = VdType.UInt64;
						pbValue = MemUtil.UInt64ToBytes((ulong)o);
					}
					else if(t == typeof(bool))
					{
						vt = VdType.Bool;
						pbValue = new byte[1];
						pbValue[0] = ((bool)o ? (byte)1 : (byte)0);
					}
					else if(t == typeof(int))
					{
						vt = VdType.Int32;
						pbValue = MemUtil.Int32ToBytes((int)o);
					}
					else if(t == typeof(long))
					{
						vt = VdType.Int64;
						pbValue = MemUtil.Int64ToBytes((long)o);
					}
					else if(t == typeof(string))
					{
						vt = VdType.String;
						pbValue = StrUtil.Utf8.GetBytes((string)o);
					}
					else if(t == typeof(byte[]))
					{
						vt = VdType.ByteArray;
						pbValue = (byte[])o;
					}
					else { Debug.Assert(false); continue; } // Unknown type

					ms.WriteByte((byte)vt);
					MemUtil.Write(ms, MemUtil.Int32ToBytes(pbName.Length));
					MemUtil.Write(ms, pbName);
					MemUtil.Write(ms, MemUtil.Int32ToBytes(pbValue.Length));
					MemUtil.Write(ms, pbValue);
				}

				ms.WriteByte((byte)VdType.None);
				pbRet = ms.ToArray();
			}

			return pbRet;
		}

		public static VariantDictionary Deserialize(byte[] pb)
		{
			if(pb == null) { Debug.Assert(false); return null; }

			VariantDictionary d = new VariantDictionary();
			using(MemoryStream ms = new MemoryStream(pb, false))
			{
				ushort uVersion = MemUtil.BytesToUInt16(MemUtil.Read(ms, 2));
				if((uVersion & VdmCritical) > (VdVersion & VdmCritical))
					throw new FormatException(KLRes.FileNewVerReq);

				while(true)
				{
					int iType = ms.ReadByte();
					if(iType < 0) throw new EndOfStreamException(KLRes.FileCorrupted);
					byte btType = (byte)iType;
					if(btType == (byte)VdType.None) break;

					int cbName = MemUtil.BytesToInt32(MemUtil.Read(ms, 4));
					byte[] pbName = MemUtil.Read(ms, cbName);
					if(pbName.Length != cbName)
						throw new EndOfStreamException(KLRes.FileCorrupted);
					string strName = StrUtil.Utf8.GetString(pbName);

					int cbValue = MemUtil.BytesToInt32(MemUtil.Read(ms, 4));
					byte[] pbValue = MemUtil.Read(ms, cbValue);
					if(pbValue.Length != cbValue)
						throw new EndOfStreamException(KLRes.FileCorrupted);

					bool bError = false, bTypeUnknown = false;
					switch(btType)
					{
						case (byte)VdType.UInt32:
							if(cbValue == 4)
								d.SetUInt32(strName, MemUtil.BytesToUInt32(pbValue));
							else { Debug.Assert(false); bError = true; }
							break;

						case (byte)VdType.UInt64:
							if(cbValue == 8)
								d.SetUInt64(strName, MemUtil.BytesToUInt64(pbValue));
							else { Debug.Assert(false); bError = true; }
							break;

						case (byte)VdType.Bool:
							if(cbValue == 1)
								d.SetBool(strName, (pbValue[0] != 0));
							else { Debug.Assert(false); bError = true; }
							break;

						case (byte)VdType.Int32:
							if(cbValue == 4)
								d.SetInt32(strName, MemUtil.BytesToInt32(pbValue));
							else { Debug.Assert(false); bError = true; }
							break;

						case (byte)VdType.Int64:
							if(cbValue == 8)
								d.SetInt64(strName, MemUtil.BytesToInt64(pbValue));
							else { Debug.Assert(false); bError = true; }
							break;

						case (byte)VdType.String:
							d.SetString(strName, StrUtil.Utf8.GetString(pbValue));
							break;

						case (byte)VdType.ByteArray:
							d.SetByteArray(strName, pbValue);
							break;

						default:
							Debug.Assert(false);
							bTypeUnknown = true;
							break;
					}

					if(bError) throw new FormatException(KLRes.FileCorrupted);
					if(bTypeUnknown) throw new FormatException(KLRes.FileNewVerReq);
				}

				Debug.Assert(ms.ReadByte() < 0);
			}

			return d;
		}
	}
}