File: XmlSerializerEx.cs

package info (click to toggle)
keepass2 2.60%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,892 kB
  • sloc: cs: 119,878; xml: 6,087; ansic: 2,033; cpp: 738; sh: 50; makefile: 42
file content (558 lines) | stat: -rw-r--r-- 18,106 bytes parent folder | download
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
/*
  KeePass Password Safe - The Open-Source Password Manager
  Copyright (C) 2003-2025 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.Reflection;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

using KeePass.App.Configuration;

using KeePassLib.Interfaces;
using KeePassLib.Translation;
using KeePassLib.Utility;

namespace KeePass.Util.XmlSerialization
{
	public sealed partial class XmlSerializerEx : IXmlSerializerEx
	{
		private readonly Type m_t;

		public XmlSerializerEx(Type t)
		{
			m_t = t;
		}

		// AppConfigEx with KeePass.XmlSerializers.dll: 811 ms
		// AppConfigEx with own deserializer: 312 ms
		/* public object Deserialize(Stream s)
		{
			int tStart = Environment.TickCount;
			object o = Deserialize_(s);
			MessageService.ShowInfo(Environment.TickCount - tStart);
			return o;
		} */

		public object Deserialize(Stream s)
		{
			object oResult = null;
			using(XmlReader xr = XmlUtilEx.CreateXmlReader(s))
			{
				if((m_t == typeof(AppConfigEx)) || (m_t == typeof(KPTranslation)))
				{
					string strRootName = GetXmlName(m_t);
					bool bRootFound = SkipToRoot(xr, strRootName);

					if(!bRootFound) { Debug.Assert(false); }
					else if(m_t == typeof(AppConfigEx))
						oResult = ReadAppConfigEx(xr);
					else if(m_t == typeof(KPTranslation))
						oResult = ReadKPTranslation(xr);
					else { Debug.Assert(false); } // See top-level 'if'
				}
				else
				{
					XmlSerializer xs = new XmlSerializer(m_t);
					oResult = xs.Deserialize(xr);
				}
			}

			return oResult;
		}

		private static bool SkipToRoot(XmlReader xr, string strRootName)
		{
			xr.Read(); // Initialize reader

			bool bRootFound = false;
			while(true)
			{
				if((xr.NodeType == XmlNodeType.Document) ||
					(xr.NodeType == XmlNodeType.DocumentFragment) ||
					(xr.NodeType == XmlNodeType.Element))
				{
					if(xr.Name == strRootName) { bRootFound = true; break; }
					xr.Skip();
				}
				else if(!xr.Read()) { Debug.Assert(false); break; }
			}

			return bRootFound;
		}

		public void Serialize(XmlWriter xmlWriter, object o)
		{
			XmlSerializer xs = new XmlSerializer(m_t);
			xs.Serialize(xmlWriter, o);
		}

		internal static T GetAttribute<T>(object[] vAttribs)
			where T : Attribute
		{
			if(vAttribs == null) { Debug.Assert(false); return null; }

			foreach(object o in vAttribs)
			{
				if(o == null) { Debug.Assert(false); continue; }
				if(o.GetType() == typeof(T)) return (o as T);
			}

			return null;
		}

		internal static string GetXmlName(MemberInfo mi)
		{
			object[] vAttribs = mi.GetCustomAttributes(true);

			XmlTypeAttribute xt = GetAttribute<XmlTypeAttribute>(vAttribs);
			if(xt != null) return xt.TypeName;
			XmlRootAttribute xr = GetAttribute<XmlRootAttribute>(vAttribs);
			if(xr != null) return xr.ElementName;
			XmlArrayAttribute xa = GetAttribute<XmlArrayAttribute>(vAttribs);
			if(xa != null) return xa.ElementName;
			XmlElementAttribute xe = GetAttribute<XmlElementAttribute>(vAttribs);
			if(xe != null) return xe.ElementName;

			return mi.Name;
		}

		private sealed class XmlsTypeInfo
		{
			public bool HasInfo { get { return (m_strReadCode.Length > 0); } }

			private readonly Type m_t;
			public Type Type { get { return m_t; } }

			private readonly string m_strReadCode;
			public string ReadCode { get { return m_strReadCode; } }

			// private readonly string m_strWriteCode;
			// public string WriteCode { get { return m_strWriteCode; } }

			public XmlsTypeInfo(Type t)
			{
				m_t = t;
				m_strReadCode = string.Empty;
				// m_strWriteCode = string.Empty;
			}

			public XmlsTypeInfo(Type t, string strReadCode, string _)
			{
				m_t = t;
				m_strReadCode = (strReadCode ?? string.Empty);
				// m_strWriteCode = (strWriteCode ?? string.Empty);
			}
		}

		internal static void GenerateSerializers(CommandLineArgs cl)
		{
			StringBuilder sb = new StringBuilder();
			int t = 0;

			AppendLine(sb, "// This is a generated file!", ref t);
			AppendLine(sb, "// Do not edit manually, changes will be overwritten.", ref t);
			AppendLine(sb);
			AppendLine(sb, "using System;", ref t);
			AppendLine(sb, "using System.Collections.Generic;", ref t);
			AppendLine(sb, "using System.Diagnostics;", ref t);
			AppendLine(sb, "using System.Xml;", ref t);
			AppendLine(sb);
			AppendLine(sb, "using KeePassLib.Interfaces;", ref t);
			AppendLine(sb);
			AppendLine(sb, "namespace KeePass.Util.XmlSerialization", ref t);
			AppendLine(sb, "{", ref t, 0, 1);
			AppendLine(sb, "public sealed partial class XmlSerializerEx : IXmlSerializerEx", ref t);
			AppendLine(sb, "{", ref t, 0, 1);
			AppendLine(sb, "private static readonly char[] g_vEnumSeps = new char[] {", ref t, 0, 1);
			AppendLine(sb, "' ', '\\t', '\\r', '\\n', '|', ',', ';', ':'", ref t);
			AppendLine(sb, "};", ref t, -1, 0);

			Dictionary<string, XmlsTypeInfo> d = new Dictionary<string, XmlsTypeInfo>
			{
				{ typeof(AppConfigEx).FullName, new XmlsTypeInfo(typeof(AppConfigEx)) },
				{ typeof(KPTranslation).FullName, new XmlsTypeInfo(typeof(KPTranslation)) }
			};

			bool bTypeCreated = true;
			while(bTypeCreated)
			{
				bTypeCreated = false;
				foreach(KeyValuePair<string, XmlsTypeInfo> kvp in d)
				{
					if(!kvp.Value.HasInfo)
					{
						d[kvp.Key] = GenerateSerializer(kvp.Value.Type, d, t);
						bTypeCreated = true;
						break; // Iterator might be invalid
					}
				}
			}

			foreach(KeyValuePair<string, XmlsTypeInfo> kvp in d)
			{
				AppendLine(sb);
				sb.Append(kvp.Value.ReadCode);
			}

			AppendLine(sb, "}", ref t, -1, 0);
			AppendLine(sb, "}", ref t, -1, 0);
			Debug.Assert(t == 0);

			string strFileData = StrUtil.NormalizeNewLines(sb.ToString(), true);

			string strFile = cl["out"];
			if(!string.IsNullOrEmpty(strFile))
			{
				strFile = UrlUtil.MakeAbsolutePath(WinUtil.GetExecutable(), strFile);
				File.WriteAllText(strFile, strFileData, StrUtil.Utf8);
				MessageService.ShowInfo("Saved XmlSerializerEx to:", strFile);
			}
		}

		private static bool IsXmlConvertibleType(Type t)
		{
			return ((t == typeof(Boolean)) || (t == typeof(Byte)) ||
				(t == typeof(Char)) || (t == typeof(DateTime)) ||
				(t == typeof(DateTimeOffset)) || (t == typeof(Decimal)) ||
				(t == typeof(Double)) || (t == typeof(Guid)) ||
				(t == typeof(Int16)) || (t == typeof(Int32)) ||
				(t == typeof(Int64)) || (t == typeof(SByte)) ||
				(t == typeof(Single)) || (t == typeof(TimeSpan)) ||
				(t == typeof(UInt16)) || (t == typeof(UInt32)) ||
				(t == typeof(UInt64)));
		}

		private static void Append(StringBuilder sb, string strAppend,
			ref int iIndent, int iIndentChangePre, int iIndentChangePost)
		{
			iIndent += iIndentChangePre;
			Debug.Assert(iIndent >= 0);
			sb.Append(new string('\t', Math.Max(iIndent, 0)));
			sb.Append(strAppend);
			iIndent += iIndentChangePost;
		}

		private static void AppendLine(StringBuilder sb)
		{
			sb.AppendLine();
		}

		private static void AppendLine(StringBuilder sb, string strAppend,
			ref int iIndent)
		{
			AppendLine(sb, strAppend, ref iIndent, 0, 0);
		}

		private static void AppendLine(StringBuilder sb, string strAppend,
			ref int iIndent, int iIndentChangePre, int iIndentChangePost)
		{
			Append(sb, strAppend + MessageService.NewLine, ref iIndent,
				iIndentChangePre, iIndentChangePost);
		}

		internal static string GetFullTypeNameCS(Type t, out string strPrimarySubType)
		{
			string str = t.FullName;

			if(str.StartsWith(@"System.Collections.Generic.List`1"))
			{
				int iElemTypeOffset = str.IndexOf("[[");
				int iElemTypeEnd = str.IndexOfAny(new char[] {
					',', ' ', '\t', '\r', '\n', ';', ':', ']' }, iElemTypeOffset);
				strPrimarySubType = str.Substring(iElemTypeOffset + 2,
					iElemTypeEnd - iElemTypeOffset - 2);

				str = "System.Collections.Generic.List<" + strPrimarySubType + ">";
			}
			else if(str.EndsWith("[]"))
				strPrimarySubType = str.Substring(0, str.Length - 2);
			else strPrimarySubType = null;

			return str;
		}

		internal static string GetTypeDesc(string strFullTypeNameCS)
		{
			string str = strFullTypeNameCS;

			int iBackOffset = str.IndexOf('<');
			if(iBackOffset < 0) iBackOffset = str.Length - 1;

			int i = str.LastIndexOf('.', iBackOffset);
			if(i >= 0) str = str.Substring(i + 1);

			if(str.StartsWith("List<"))
			{
				string strSubType = str.Substring(5, str.Length - 6);
				return "ListOf" + GetTypeDesc(strSubType);
			}
			if(str.EndsWith("[]"))
			{
				string strSubType = str.Substring(0, str.Length - 2);
				return "ArrayOf" + GetTypeDesc(strSubType);
			}

			Debug.Assert(str.IndexOfAny(new char[] { '<', '>', '[', ']',
				'`', ':', ' ', '\t', '\r', '\n' }) < 0);
			return str;
		}

		internal static bool TypeIsList(string strTypeFullCS)
		{
			return strTypeFullCS.StartsWith("System.Collections.Generic.List<");
		}

		internal static bool TypeIsArray(string strTypeFullCS)
		{
			return strTypeFullCS.EndsWith("[]");
		}

		private static XmlsTypeInfo GenerateSerializer(Type t,
			Dictionary<string, XmlsTypeInfo> dTypes, int iIndent)
		{
			StringBuilder sbr = new StringBuilder();
			StringBuilder sbw = new StringBuilder();

			string strSubTypeFull;
			string strTypeFull = GetFullTypeNameCS(t, out strSubTypeFull);
			string strTypeDesc = GetTypeDesc(strTypeFull);

			string strSubTypeDesc = null;
			if(strSubTypeFull != null)
				strSubTypeDesc = GetTypeDesc(strSubTypeFull);

			bool bIsList = TypeIsList(strTypeFull);
			bool bIsArray = TypeIsArray(strTypeFull);
			int ir = iIndent;

			if(t.IsEnum)
				AppendLine(sbr, "private static Dictionary<string, " + strTypeFull +
					"> m_dict" + strTypeDesc + " = null;", ref ir);

			AppendLine(sbr, "private static " + strTypeFull + " Read" +
				strTypeDesc + "(XmlReader xr" +
				// ((bIsList || bIsArray) ? ", string strItemName" : string.Empty) +
				")", ref ir);
			AppendLine(sbr, "{", ref ir, 0, 1);

			if(t == typeof(string))
			{
				AppendLine(sbr, "return xr.ReadElementString();", ref ir);
			}
			else if(IsXmlConvertibleType(t))
			{
				AppendLine(sbr, "string strValue = xr.ReadElementString();", ref ir);
				AppendLine(sbr, "return XmlConvert.To" + strTypeDesc + "(strValue);", ref ir);
			}
			else if(t.IsEnum)
			{
				AppendLine(sbr, "if(m_dict" + strTypeDesc + " == null)", ref ir);
				AppendLine(sbr, "{", ref ir, 0, 1);
				AppendLine(sbr, "m_dict" + strTypeDesc + " = new Dictionary<string, " +
					strTypeFull + ">();", ref ir);

				string[] vEnumNames = Enum.GetNames(t);
				foreach(string strEnumName in vEnumNames)
				{
					AppendLine(sbr, "m_dict" + strTypeDesc + "[\"" + strEnumName +
						"\"] = " + strTypeFull + "." + strEnumName + ";", ref ir);
				}

				AppendLine(sbr, "}", ref ir, -1, 0);
				AppendLine(sbr);

				AppendLine(sbr, "string strValue = xr.ReadElementString();", ref ir);

				// AppendLine(sbr, "return Enum.Parse(typeof(" + strTypeFull + "), strValue);", ref ir);
				object[] vAttribs = t.GetCustomAttributes(true);
				if(GetAttribute<FlagsAttribute>(vAttribs) != null)
				{
					AppendLine(sbr, strTypeFull + " eResult = (" + strTypeFull + ")0;", ref ir);
					AppendLine(sbr, "string[] vValues = strValue.Split(g_vEnumSeps, StringSplitOptions.RemoveEmptyEntries);", ref ir);
					AppendLine(sbr, "foreach(string strPart in vValues)", ref ir);
					AppendLine(sbr, "{", ref ir, 0, 1);
					AppendLine(sbr, strTypeFull + " ePart;", ref ir);
					AppendLine(sbr, "if(m_dict" + strTypeDesc + ".TryGetValue(strPart, out ePart))", ref ir);
					AppendLine(sbr, "eResult |= ePart;", ref ir, 1, -1);
					AppendLine(sbr, "else { Debug.Assert(false); }", ref ir);
					AppendLine(sbr, "}", ref ir, -1, 0);
				}
				else
				{
					AppendLine(sbr, strTypeFull + " eResult;", ref ir);
					AppendLine(sbr, "if(!m_dict" + strTypeDesc + ".TryGetValue(strValue, out eResult))", ref ir);
					AppendLine(sbr, "{ Debug.Assert(false); }", ref ir, 1, -1);
				}

				AppendLine(sbr, "return eResult;", ref ir);
			}
			else
			{
				if(!bIsArray)
					AppendLine(sbr, strTypeFull + " o = new " + strTypeFull + "();", ref ir);
				else
					AppendLine(sbr, "List<" + strSubTypeFull + "> l = new List<" +
						strSubTypeFull + ">();", ref ir);
				AppendLine(sbr);

				byte[] pbInsAttribs = new byte[16];
				Program.GlobalRandom.NextBytes(pbInsAttribs);
				string strInsAttribs = Convert.ToBase64String(pbInsAttribs,
					Base64FormattingOptions.None);
				sbr.Append(strInsAttribs);
				StringBuilder sbAttribs = new StringBuilder();

				AppendLine(sbr, "if(SkipEmptyElement(xr)) return " +
					(bIsArray ? "l.ToArray();" : "o;"), ref ir);
				AppendLine(sbr);

				AppendLine(sbr, "Debug.Assert(xr.NodeType == XmlNodeType.Element);", ref ir);
				AppendLine(sbr, "xr.ReadStartElement();", ref ir);
				AppendLine(sbr, "xr.MoveToContent();", ref ir);
				AppendLine(sbr);
				AppendLine(sbr, "while(true)", ref ir);
				AppendLine(sbr, "{", ref ir, 0, 1);
				AppendLine(sbr, "XmlNodeType nt = xr.NodeType;", ref ir);
				AppendLine(sbr, "if((nt == XmlNodeType.EndElement) || (nt == XmlNodeType.None)) break;", ref ir);
				AppendLine(sbr, "if(nt != XmlNodeType.Element) { Debug.Assert(false); xr.Skip(); continue; }", ref ir);
				AppendLine(sbr);

				if(bIsList || bIsArray)
				{
					AppendLine(sbr, strSubTypeFull + " oElem = Read" + strSubTypeDesc +
						"(xr);", ref ir);
					AppendLine(sbr, (bIsArray ? "l" : "o") + ".Add(oElem);", ref ir);

					if(!dTypes.ContainsKey(strSubTypeFull))
						dTypes[strSubTypeFull] = new XmlsTypeInfo(Type.GetType(strSubTypeFull));
				}
				else
				{
					StringBuilder sbs = new StringBuilder();
					uint uElements = 0;

					AppendLine(sbs, "switch(xr.LocalName)", ref ir);
					AppendLine(sbs, "{", ref ir, 0, 1);

					PropertyInfo[] vProps = t.GetProperties();
					foreach(PropertyInfo pi in vProps)
					{
						object[] vAttribs = pi.GetCustomAttributes(true);
						if(GetAttribute<XmlIgnoreAttribute>(vAttribs) != null) continue;

						if(!pi.CanRead || !pi.CanWrite) { Debug.Assert(false); continue; }

						Type tProp = pi.PropertyType;
						string strPropSubTypeFull;
						string strPropTypeFull = GetFullTypeNameCS(tProp, out strPropSubTypeFull);
						string strPropTypeDesc = GetTypeDesc(strPropTypeFull);

						string strXmlName = GetXmlName(pi);

						if(!dTypes.ContainsKey(strPropTypeFull))
							dTypes[strPropTypeFull] = new XmlsTypeInfo(tProp);

						if(GetAttribute<XmlAttributeAttribute>(vAttribs) == null)
						{
							AppendLine(sbs, "case \"" + strXmlName + "\":", ref ir, 0, 1);
							AppendLine(sbs, "o." + pi.Name + " = Read" + strPropTypeDesc +
								"(xr);", ref ir);
							AppendLine(sbs, "break;", ref ir, 0, -1);

							++uElements;
						}
						else
						{
							Debug.Assert(tProp == typeof(string));

							AppendLine(sbAttribs, "case \"" + strXmlName + "\":", ref ir, 0, 1);
							AppendLine(sbAttribs, "o." + pi.Name + " = xr.Value;", ref ir);
							AppendLine(sbAttribs, "break;", ref ir, 0, -1);
						}
					}

					AppendLine(sbs, "default:", ref ir, 0, 1);
					AppendLine(sbs, "Debug.Assert(false);", ref ir);
					AppendLine(sbs, "xr.Skip();", ref ir);
					AppendLine(sbs, "break;", ref ir, 0, -1);
					AppendLine(sbs, "}", ref ir, -1, 0); // switch

					if(uElements > 0) sbr.Append(sbs.ToString());
					else
					{
						AppendLine(sbr, "Debug.Assert(false);", ref ir);
						AppendLine(sbr, "xr.Skip();", ref ir);
					}
				}

				AppendLine(sbr);
				AppendLine(sbr, "xr.MoveToContent();", ref ir);
				AppendLine(sbr, "}", ref ir, -1, 0);
				AppendLine(sbr);
				AppendLine(sbr, "Debug.Assert(xr.NodeType == XmlNodeType.EndElement);", ref ir);
				AppendLine(sbr, "xr.ReadEndElement();", ref ir);

				AppendLine(sbr, (bIsArray ? "return l.ToArray();" : "return o;"), ref ir);

				if(sbAttribs.Length == 0)
					sbr.Replace(strInsAttribs, string.Empty);
				else
				{
					StringBuilder sba = new StringBuilder();
					AppendLine(sba, "while(xr.MoveToNextAttribute())", ref ir);
					AppendLine(sba, "{", ref ir, 0, 1);
					AppendLine(sba, "switch(xr.LocalName)", ref ir);
					AppendLine(sba, "{", ref ir, 0, 1);
					sba.Append(sbAttribs.ToString());
					AppendLine(sba, "default:", ref ir, 0, 1);
					AppendLine(sba, "Debug.Assert(false);", ref ir);
					AppendLine(sba, "break;", ref ir, 0, -1);
					AppendLine(sba, "}", ref ir, -1, 0); // switch
					AppendLine(sba, "}", ref ir, -1, 0); // while
					sba.AppendLine();

					sbr.Replace(strInsAttribs, sba.ToString());
				}
			}

			AppendLine(sbr, "}", ref ir, -1, 0);

			Debug.Assert(ir == iIndent);

			XmlsTypeInfo xti = new XmlsTypeInfo(t, sbr.ToString(), sbw.ToString());
			return xti;
		}

		private static bool SkipEmptyElement(XmlReader xr)
		{
			xr.MoveToElement();
			if(xr.IsEmptyElement)
			{
				xr.Skip();
				return true;
			}

			return false;
		}
	}
}