File: PwObjectPool.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 (232 lines) | stat: -rw-r--r-- 5,551 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
/*
  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;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

using KeePassLib.Delegates;
using KeePassLib.Interfaces;
using KeePassLib.Utility;

#if KeePassLibSD
using KeePassLibSD;
#endif

namespace KeePassLib.Collections
{
	public sealed class PwObjectPool
	{
		private readonly SortedDictionary<PwUuid, IStructureItem> m_dict =
			new SortedDictionary<PwUuid, IStructureItem>();

		public static PwObjectPool FromGroupRecursive(PwGroup pgRoot, bool bEntries)
		{
			if(pgRoot == null) throw new ArgumentNullException("pgRoot");

			PwObjectPool p = new PwObjectPool();

			if(!bEntries) p.m_dict[pgRoot.Uuid] = pgRoot;
			GroupHandler gh = delegate(PwGroup pg)
			{
				p.m_dict[pg.Uuid] = pg;
				return true;
			};

			EntryHandler eh = delegate(PwEntry pe)
			{
				p.m_dict[pe.Uuid] = pe;
				return true;
			};

			pgRoot.TraverseTree(TraversalMethod.PreOrder, bEntries ? null : gh,
				bEntries ? eh : null);
			return p;
		}

		public IStructureItem Get(PwUuid pwUuid)
		{
			IStructureItem pItem;
			m_dict.TryGetValue(pwUuid, out pItem);
			return pItem;
		}

		public bool ContainsOnlyType(Type t)
		{
			foreach(KeyValuePair<PwUuid, IStructureItem> kvp in m_dict)
			{
				if(kvp.Value.GetType() != t) return false;
			}

			return true;
		}
	}

	internal sealed class PwObjectPoolEx
	{
		private readonly Dictionary<PwUuid, ulong> m_dUuidToId =
			new Dictionary<PwUuid, ulong>();
		private readonly Dictionary<ulong, IStructureItem> m_dIdToItem =
			new Dictionary<ulong, IStructureItem>();

		private PwObjectPoolEx()
		{
		}

		public static PwObjectPoolEx FromGroup(PwGroup pg)
		{
			PwObjectPoolEx p = new PwObjectPoolEx();

			if(pg == null) { Debug.Assert(false); return p; }

			ulong uFreeId = 2; // 0 = "not found", 1 is a hole

			p.m_dUuidToId[pg.Uuid] = uFreeId;
			p.m_dIdToItem[uFreeId] = pg;
			uFreeId += 2; // Make hole

			p.AddGroupRec(pg, ref uFreeId);
			return p;
		}

		private void AddGroupRec(PwGroup pg, ref ulong uFreeId)
		{
			if(pg == null) { Debug.Assert(false); return; }

			ulong uId = uFreeId;

			// Consecutive entries must have consecutive IDs
			foreach(PwEntry pe in pg.Entries)
			{
				Debug.Assert(!m_dUuidToId.ContainsKey(pe.Uuid));
				Debug.Assert(!m_dIdToItem.ContainsValue(pe));

				m_dUuidToId[pe.Uuid] = uId;
				m_dIdToItem[uId] = pe;
				++uId;
			}
			++uId; // Make hole

			// Consecutive groups must have consecutive IDs
			foreach(PwGroup pgSub in pg.Groups)
			{
				Debug.Assert(!m_dUuidToId.ContainsKey(pgSub.Uuid));
				Debug.Assert(!m_dIdToItem.ContainsValue(pgSub));

				m_dUuidToId[pgSub.Uuid] = uId;
				m_dIdToItem[uId] = pgSub;
				++uId;
			}
			++uId; // Make hole

			foreach(PwGroup pgSub in pg.Groups)
			{
				AddGroupRec(pgSub, ref uId);
			}

			uFreeId = uId;
		}

		public ulong GetIdByUuid(PwUuid pwUuid)
		{
			if(pwUuid == null) { Debug.Assert(false); return 0; }

			ulong uId;
			m_dUuidToId.TryGetValue(pwUuid, out uId);
			return uId;
		}

		public IStructureItem GetItemByUuid(PwUuid pwUuid)
		{
			if(pwUuid == null) { Debug.Assert(false); return null; }

			ulong uId;
			if(!m_dUuidToId.TryGetValue(pwUuid, out uId)) return null;
			Debug.Assert(uId != 0);

			return GetItemById(uId);
		}

		public IStructureItem GetItemById(ulong uId)
		{
			IStructureItem p;
			m_dIdToItem.TryGetValue(uId, out p);
			return p;
		}
	}

	internal sealed class PwObjectBlock<T> : IEnumerable<T>
		where T : class, ITimeLogger, IStructureItem, IDeepCloneable<T>
	{
		private readonly List<T> m_l = new List<T>();

		public T PrimaryItem
		{
			get { return ((m_l.Count > 0) ? m_l[0] : null); }
		}

		private DateTime m_dtLocationChanged = TimeUtil.SafeMinValueUtc;
		public DateTime LocationChanged
		{
			get { return m_dtLocationChanged; }
		}

		private PwObjectPoolEx m_poolAssoc = null;
		public PwObjectPoolEx PoolAssoc
		{
			get { return m_poolAssoc; }
		}

		public PwObjectBlock()
		{
		}

#if DEBUG
		public override string ToString()
		{
			return ("PwObjectBlock, Count = " + m_l.Count.ToString());
		}
#endif

		IEnumerator IEnumerable.GetEnumerator()
		{
			return m_l.GetEnumerator();
		}

		public IEnumerator<T> GetEnumerator()
		{
			return m_l.GetEnumerator();
		}

		public void Add(T t, DateTime dtLoc, PwObjectPoolEx pool)
		{
			if(t == null) { Debug.Assert(false); return; }

			m_l.Add(t);

			if(dtLoc > m_dtLocationChanged)
			{
				m_dtLocationChanged = dtLoc;
				m_poolAssoc = pool;
			}
		}
	}
}