File: SprRefCache.cs

package info (click to toggle)
keepass2 2.47%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 13,992 kB
  • sloc: cs: 111,053; xml: 5,956; cpp: 308; makefile: 48; sh: 48
file content (123 lines) | stat: -rw-r--r-- 3,171 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
/*
  KeePass Password Safe - The Open-Source Password Manager
  Copyright (C) 2003-2021 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.Text;

using KeePassLib.Utility;

namespace KeePass.Util.Spr
{
	internal sealed class SprRefCache
	{
		private sealed class SprRefCacheItem
		{
			public readonly string Ref;
			public readonly string Value;
			public readonly uint Context;

			public SprRefCacheItem(string strRef, string strValue, uint uContext)
			{
				this.Ref = strRef;
				this.Value = strValue;
				this.Context = uContext;
			}
		}

		private List<SprRefCacheItem> m_l = new List<SprRefCacheItem>();

		public SprRefCache()
		{
		}

		/// <summary>
		/// Hash all settings of <paramref name="ctx" /> that may
		/// affect the encoded value of a field reference.
		/// </summary>
		private static uint HashContext(SprContext ctx)
		{
			if(ctx == null) { Debug.Assert(false); return 0; }

			uint u = 0;

			if(ctx.ForcePlainTextPasswords) u |= 1;
			if(ctx.EncodeForCommandLine) u |= 2;
			if(ctx.EncodeAsAutoTypeSequence) u |= 4;

			return u;
		}

		public void Clear()
		{
			m_l.Clear();
		}

		private string Get(string strRef, uint uCtx)
		{
			if(strRef == null) { Debug.Assert(false); return null; }

			foreach(SprRefCacheItem ci in m_l)
			{
				if(ci.Context != uCtx) continue;

				if(string.Equals(strRef, ci.Ref, StrUtil.CaseIgnoreCmp))
					return ci.Value;
			}

			return null;
		}

		public bool Add(string strRef, string strValue, SprContext ctx)
		{
			if(strRef == null) throw new ArgumentNullException("strRef");
			if(strValue == null) throw new ArgumentNullException("strValue");

			uint uCtx = HashContext(ctx);

			if(Get(strRef, uCtx) != null)
			{
				Debug.Assert(false);
				return false; // Exists already, do not overwrite
			}

			m_l.Add(new SprRefCacheItem(strRef, strValue, uCtx));
			return true;
		}

		public string Fill(string strText, SprContext ctx)
		{
			if(strText == null) { Debug.Assert(false); return string.Empty; }

			string str = strText;
			uint uCtx = HashContext(ctx);

			foreach(SprRefCacheItem ci in m_l)
			{
				if(ci.Context != uCtx) continue;

				// str = str.Replace(ci.Ref, ci.Value);
				str = StrUtil.ReplaceCaseInsensitive(str, ci.Ref, ci.Value);
			}

			return str;
		}
	}
}