File: ProtectedBinarySet.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 (170 lines) | stat: -rw-r--r-- 4,116 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
/*
  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.Security;

namespace KeePassLib.Collections
{
	internal sealed class ProtectedBinarySet : IEnumerable<KeyValuePair<int, ProtectedBinary>>
	{
		private readonly Dictionary<int, ProtectedBinary> m_d =
			new Dictionary<int, ProtectedBinary>();

		private readonly bool m_bDedupAdd;

		public ProtectedBinarySet(bool bDedupAdd)
		{
			m_bDedupAdd = bDedupAdd;
		}

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

		public IEnumerator<KeyValuePair<int, ProtectedBinary>> GetEnumerator()
		{
			return m_d.GetEnumerator();
		}

		private int GetFreeID()
		{
			int i = m_d.Count;
			while(m_d.ContainsKey(i)) { ++i; }
			Debug.Assert(i == m_d.Count); // m_d.Count should be free
			return i;
		}

		public ProtectedBinary Get(int iID)
		{
			ProtectedBinary pb;
			if(m_d.TryGetValue(iID, out pb)) return pb;

			// Debug.Assert(false); // No assert
			return null;
		}

		public int Find(ProtectedBinary pb)
		{
			if(pb == null) { Debug.Assert(false); return -1; }

			// Fast search by reference
			foreach(KeyValuePair<int, ProtectedBinary> kvp in m_d)
			{
				if(object.ReferenceEquals(pb, kvp.Value))
				{
					Debug.Assert(pb.Equals(kvp.Value));
					return kvp.Key;
				}
			}

			// Slow search by content
			foreach(KeyValuePair<int, ProtectedBinary> kvp in m_d)
			{
				if(pb.Equals(kvp.Value)) return kvp.Key;
			}

			// Debug.Assert(false); // No assert
			return -1;
		}

		public void Set(int iID, ProtectedBinary pb)
		{
			if(iID < 0) { Debug.Assert(false); return; }
			if(pb == null) { Debug.Assert(false); return; }

			m_d[iID] = pb;
		}

		public void Add(ProtectedBinary pb)
		{
			if(pb == null) { Debug.Assert(false); return; }

			if(m_bDedupAdd && (Find(pb) >= 0)) return; // Exists already

			m_d[GetFreeID()] = pb;
		}

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

			foreach(KeyValuePair<string, ProtectedBinary> kvp in d)
			{
				Add(kvp.Value);
			}
		}

		public void AddFrom(PwGroup pg)
		{
			if(pg == null) { Debug.Assert(false); return; }

			EntryHandler eh = delegate(PwEntry pe)
			{
				if(pe == null) { Debug.Assert(false); return true; }

				AddFrom(pe.Binaries);
				foreach(PwEntry peHistory in pe.History)
				{
					if(peHistory == null) { Debug.Assert(false); continue; }
					AddFrom(peHistory.Binaries);
				}

				return true;
			};

			pg.TraverseTree(TraversalMethod.PreOrder, null, eh);
		}

		public ProtectedBinary[] ToArray()
		{
			int n = m_d.Count;
			ProtectedBinary[] v = new ProtectedBinary[n];

			foreach(KeyValuePair<int, ProtectedBinary> kvp in m_d)
			{
				if((kvp.Key < 0) || (kvp.Key >= n))
				{
					Debug.Assert(false);
					throw new InvalidOperationException();
				}

				v[kvp.Key] = kvp.Value;
			}

			for(int i = 0; i < n; ++i)
			{
				if(v[i] == null)
				{
					Debug.Assert(false);
					throw new InvalidOperationException();
				}
			}

			return v;
		}
	}
}