mirror of
https://github.com/wismna/ModernKeePassLib.git
synced 2025-10-04 08:00:18 -04:00
Setup solution
This commit is contained in:
244
ModernKeePassLib/Collections/AutoTypeConfig.cs
Normal file
244
ModernKeePassLib/Collections/AutoTypeConfig.cs
Normal file
@@ -0,0 +1,244 @@
|
||||
/*
|
||||
KeePass Password Safe - The Open-Source Password Manager
|
||||
Copyright (C) 2003-2019 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 ModernKeePassLib.Interfaces;
|
||||
|
||||
namespace ModernKeePassLib.Collections
|
||||
{
|
||||
[Flags]
|
||||
public enum AutoTypeObfuscationOptions
|
||||
{
|
||||
None = 0,
|
||||
UseClipboard = 1
|
||||
}
|
||||
|
||||
public sealed class AutoTypeAssociation : IEquatable<AutoTypeAssociation>,
|
||||
IDeepCloneable<AutoTypeAssociation>
|
||||
{
|
||||
private string m_strWindow = string.Empty;
|
||||
public string WindowName
|
||||
{
|
||||
get { return m_strWindow; }
|
||||
set
|
||||
{
|
||||
Debug.Assert(value != null); if(value == null) throw new ArgumentNullException("value");
|
||||
m_strWindow = value;
|
||||
}
|
||||
}
|
||||
|
||||
private string m_strSequence = string.Empty;
|
||||
public string Sequence
|
||||
{
|
||||
get { return m_strSequence; }
|
||||
set
|
||||
{
|
||||
Debug.Assert(value != null); if(value == null) throw new ArgumentNullException("value");
|
||||
m_strSequence = value;
|
||||
}
|
||||
}
|
||||
|
||||
public AutoTypeAssociation() { }
|
||||
|
||||
public AutoTypeAssociation(string strWindow, string strSeq)
|
||||
{
|
||||
if(strWindow == null) throw new ArgumentNullException("strWindow");
|
||||
if(strSeq == null) throw new ArgumentNullException("strSeq");
|
||||
|
||||
m_strWindow = strWindow;
|
||||
m_strSequence = strSeq;
|
||||
}
|
||||
|
||||
public bool Equals(AutoTypeAssociation other)
|
||||
{
|
||||
if(other == null) return false;
|
||||
|
||||
if(m_strWindow != other.m_strWindow) return false;
|
||||
if(m_strSequence != other.m_strSequence) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public AutoTypeAssociation CloneDeep()
|
||||
{
|
||||
return (AutoTypeAssociation)this.MemberwiseClone();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A list of auto-type associations.
|
||||
/// </summary>
|
||||
public sealed class AutoTypeConfig : IEquatable<AutoTypeConfig>,
|
||||
IDeepCloneable<AutoTypeConfig>
|
||||
{
|
||||
private bool m_bEnabled = true;
|
||||
private AutoTypeObfuscationOptions m_atooObfuscation =
|
||||
AutoTypeObfuscationOptions.None;
|
||||
private string m_strDefaultSequence = string.Empty;
|
||||
private List<AutoTypeAssociation> m_lWindowAssocs =
|
||||
new List<AutoTypeAssociation>();
|
||||
|
||||
/// <summary>
|
||||
/// Specify whether auto-type is enabled or not.
|
||||
/// </summary>
|
||||
public bool Enabled
|
||||
{
|
||||
get { return m_bEnabled; }
|
||||
set { m_bEnabled = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Specify whether the typing should be obfuscated.
|
||||
/// </summary>
|
||||
public AutoTypeObfuscationOptions ObfuscationOptions
|
||||
{
|
||||
get { return m_atooObfuscation; }
|
||||
set { m_atooObfuscation = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The default keystroke sequence that is auto-typed if
|
||||
/// no matching window is found in the <c>Associations</c>
|
||||
/// container.
|
||||
/// </summary>
|
||||
public string DefaultSequence
|
||||
{
|
||||
get { return m_strDefaultSequence; }
|
||||
set
|
||||
{
|
||||
Debug.Assert(value != null); if(value == null) throw new ArgumentNullException("value");
|
||||
m_strDefaultSequence = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get all auto-type window/keystroke sequence pairs.
|
||||
/// </summary>
|
||||
public IEnumerable<AutoTypeAssociation> Associations
|
||||
{
|
||||
get { return m_lWindowAssocs; }
|
||||
}
|
||||
|
||||
public int AssociationsCount
|
||||
{
|
||||
get { return m_lWindowAssocs.Count; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Construct a new auto-type associations list.
|
||||
/// </summary>
|
||||
public AutoTypeConfig()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove all associations.
|
||||
/// </summary>
|
||||
public void Clear()
|
||||
{
|
||||
m_lWindowAssocs.Clear();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clone the auto-type associations list.
|
||||
/// </summary>
|
||||
/// <returns>New, cloned object.</returns>
|
||||
public AutoTypeConfig CloneDeep()
|
||||
{
|
||||
AutoTypeConfig newCfg = new AutoTypeConfig();
|
||||
|
||||
newCfg.m_bEnabled = m_bEnabled;
|
||||
newCfg.m_atooObfuscation = m_atooObfuscation;
|
||||
newCfg.m_strDefaultSequence = m_strDefaultSequence;
|
||||
|
||||
foreach(AutoTypeAssociation a in m_lWindowAssocs)
|
||||
newCfg.Add(a.CloneDeep());
|
||||
|
||||
return newCfg;
|
||||
}
|
||||
|
||||
public bool Equals(AutoTypeConfig other)
|
||||
{
|
||||
if(other == null) { Debug.Assert(false); return false; }
|
||||
|
||||
if(m_bEnabled != other.m_bEnabled) return false;
|
||||
if(m_atooObfuscation != other.m_atooObfuscation) return false;
|
||||
if(m_strDefaultSequence != other.m_strDefaultSequence) return false;
|
||||
|
||||
if(m_lWindowAssocs.Count != other.m_lWindowAssocs.Count) return false;
|
||||
for(int i = 0; i < m_lWindowAssocs.Count; ++i)
|
||||
{
|
||||
if(!m_lWindowAssocs[i].Equals(other.m_lWindowAssocs[i]))
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public AutoTypeAssociation GetAt(int iIndex)
|
||||
{
|
||||
if((iIndex < 0) || (iIndex >= m_lWindowAssocs.Count))
|
||||
throw new ArgumentOutOfRangeException("iIndex");
|
||||
|
||||
return m_lWindowAssocs[iIndex];
|
||||
}
|
||||
|
||||
public void Add(AutoTypeAssociation a)
|
||||
{
|
||||
if(a == null) { Debug.Assert(false); throw new ArgumentNullException("a"); }
|
||||
|
||||
m_lWindowAssocs.Add(a);
|
||||
}
|
||||
|
||||
public void Insert(int iIndex, AutoTypeAssociation a)
|
||||
{
|
||||
if((iIndex < 0) || (iIndex > m_lWindowAssocs.Count))
|
||||
throw new ArgumentOutOfRangeException("iIndex");
|
||||
if(a == null) { Debug.Assert(false); throw new ArgumentNullException("a"); }
|
||||
|
||||
m_lWindowAssocs.Insert(iIndex, a);
|
||||
}
|
||||
|
||||
public void RemoveAt(int iIndex)
|
||||
{
|
||||
if((iIndex < 0) || (iIndex >= m_lWindowAssocs.Count))
|
||||
throw new ArgumentOutOfRangeException("iIndex");
|
||||
|
||||
m_lWindowAssocs.RemoveAt(iIndex);
|
||||
}
|
||||
|
||||
// public void Sort()
|
||||
// {
|
||||
// m_lWindowAssocs.Sort(AutoTypeConfig.AssocCompareFn);
|
||||
// }
|
||||
|
||||
// private static int AssocCompareFn(AutoTypeAssociation x,
|
||||
// AutoTypeAssociation y)
|
||||
// {
|
||||
// if(x == null) { Debug.Assert(false); return ((y == null) ? 0 : -1); }
|
||||
// if(y == null) { Debug.Assert(false); return 1; }
|
||||
// int cn = x.WindowName.CompareTo(y.WindowName);
|
||||
// if(cn != 0) return cn;
|
||||
// return x.Sequence.CompareTo(y.Sequence);
|
||||
// }
|
||||
}
|
||||
}
|
172
ModernKeePassLib/Collections/ProtectedBinaryDictionary.cs
Normal file
172
ModernKeePassLib/Collections/ProtectedBinaryDictionary.cs
Normal file
@@ -0,0 +1,172 @@
|
||||
/*
|
||||
KeePass Password Safe - The Open-Source Password Manager
|
||||
Copyright (C) 2003-2019 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.Text;
|
||||
using System.Diagnostics;
|
||||
|
||||
using ModernKeePassLib.Interfaces;
|
||||
using ModernKeePassLib.Security;
|
||||
|
||||
#if KeePassLibSD
|
||||
using KeePassLibSD;
|
||||
#endif
|
||||
|
||||
namespace ModernKeePassLib.Collections
|
||||
{
|
||||
/// <summary>
|
||||
/// A list of <c>ProtectedBinary</c> objects (dictionary).
|
||||
/// </summary>
|
||||
public sealed class ProtectedBinaryDictionary :
|
||||
IDeepCloneable<ProtectedBinaryDictionary>,
|
||||
IEnumerable<KeyValuePair<string, ProtectedBinary>>
|
||||
{
|
||||
private SortedDictionary<string, ProtectedBinary> m_vBinaries =
|
||||
new SortedDictionary<string, ProtectedBinary>();
|
||||
|
||||
/// <summary>
|
||||
/// Get the number of binaries in this entry.
|
||||
/// </summary>
|
||||
public uint UCount
|
||||
{
|
||||
get { return (uint)m_vBinaries.Count; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Construct a new list of protected binaries.
|
||||
/// </summary>
|
||||
public ProtectedBinaryDictionary()
|
||||
{
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return m_vBinaries.GetEnumerator();
|
||||
}
|
||||
|
||||
public IEnumerator<KeyValuePair<string, ProtectedBinary>> GetEnumerator()
|
||||
{
|
||||
return m_vBinaries.GetEnumerator();
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
m_vBinaries.Clear();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clone the current <c>ProtectedBinaryList</c> object, including all
|
||||
/// stored protected strings.
|
||||
/// </summary>
|
||||
/// <returns>New <c>ProtectedBinaryList</c> object.</returns>
|
||||
public ProtectedBinaryDictionary CloneDeep()
|
||||
{
|
||||
ProtectedBinaryDictionary plNew = new ProtectedBinaryDictionary();
|
||||
|
||||
foreach(KeyValuePair<string, ProtectedBinary> kvpBin in m_vBinaries)
|
||||
{
|
||||
// ProtectedBinary objects are immutable
|
||||
plNew.Set(kvpBin.Key, kvpBin.Value);
|
||||
}
|
||||
|
||||
return plNew;
|
||||
}
|
||||
|
||||
public bool EqualsDictionary(ProtectedBinaryDictionary dict)
|
||||
{
|
||||
if(dict == null) { Debug.Assert(false); return false; }
|
||||
|
||||
if(m_vBinaries.Count != dict.m_vBinaries.Count) return false;
|
||||
|
||||
foreach(KeyValuePair<string, ProtectedBinary> kvp in m_vBinaries)
|
||||
{
|
||||
ProtectedBinary pb = dict.Get(kvp.Key);
|
||||
if(pb == null) return false;
|
||||
if(!pb.Equals(kvp.Value)) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get one of the stored binaries.
|
||||
/// </summary>
|
||||
/// <param name="strName">Binary identifier.</param>
|
||||
/// <returns>Protected binary. If the binary identified by
|
||||
/// <paramref name="strName" /> cannot be found, the function
|
||||
/// returns <c>null</c>.</returns>
|
||||
/// <exception cref="System.ArgumentNullException">Thrown if the input
|
||||
/// parameter is <c>null</c>.</exception>
|
||||
public ProtectedBinary Get(string strName)
|
||||
{
|
||||
Debug.Assert(strName != null); if(strName == null) throw new ArgumentNullException("strName");
|
||||
|
||||
ProtectedBinary pb;
|
||||
if(m_vBinaries.TryGetValue(strName, out pb)) return pb;
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set a binary object.
|
||||
/// </summary>
|
||||
/// <param name="strField">Identifier of the binary field to modify.</param>
|
||||
/// <param name="pbNewValue">New value. This parameter must not be <c>null</c>.</param>
|
||||
/// <exception cref="System.ArgumentNullException">Thrown if any of the input
|
||||
/// parameters is <c>null</c>.</exception>
|
||||
public void Set(string strField, ProtectedBinary pbNewValue)
|
||||
{
|
||||
Debug.Assert(strField != null); if(strField == null) throw new ArgumentNullException("strField");
|
||||
Debug.Assert(pbNewValue != null); if(pbNewValue == null) throw new ArgumentNullException("pbNewValue");
|
||||
|
||||
m_vBinaries[strField] = pbNewValue;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove a binary object.
|
||||
/// </summary>
|
||||
/// <param name="strField">Identifier of the binary field to remove.</param>
|
||||
/// <returns>Returns <c>true</c> if the object has been successfully
|
||||
/// removed, otherwise <c>false</c>.</returns>
|
||||
/// <exception cref="System.ArgumentNullException">Thrown if the input parameter
|
||||
/// is <c>null</c>.</exception>
|
||||
public bool Remove(string strField)
|
||||
{
|
||||
Debug.Assert(strField != null); if(strField == null) throw new ArgumentNullException("strField");
|
||||
|
||||
return m_vBinaries.Remove(strField);
|
||||
}
|
||||
|
||||
public string KeysToString()
|
||||
{
|
||||
if(m_vBinaries.Count == 0) return string.Empty;
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
foreach(KeyValuePair<string, ProtectedBinary> kvp in m_vBinaries)
|
||||
{
|
||||
if(sb.Length > 0) sb.Append(", ");
|
||||
sb.Append(kvp.Key);
|
||||
}
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
}
|
||||
}
|
174
ModernKeePassLib/Collections/ProtectedBinarySet.cs
Normal file
174
ModernKeePassLib/Collections/ProtectedBinarySet.cs
Normal file
@@ -0,0 +1,174 @@
|
||||
/*
|
||||
KeePass Password Safe - The Open-Source Password Manager
|
||||
Copyright (C) 2003-2019 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 ModernKeePassLib.Delegates;
|
||||
using ModernKeePassLib.Security;
|
||||
|
||||
namespace ModernKeePassLib.Collections
|
||||
{
|
||||
internal sealed class ProtectedBinarySet : IEnumerable<KeyValuePair<int, ProtectedBinary>>
|
||||
{
|
||||
private Dictionary<int, ProtectedBinary> m_d =
|
||||
new Dictionary<int, ProtectedBinary>();
|
||||
|
||||
public ProtectedBinarySet()
|
||||
{
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return m_d.GetEnumerator();
|
||||
}
|
||||
|
||||
public IEnumerator<KeyValuePair<int, ProtectedBinary>> GetEnumerator()
|
||||
{
|
||||
return m_d.GetEnumerator();
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
m_d.Clear();
|
||||
}
|
||||
|
||||
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; }
|
||||
|
||||
int i = Find(pb);
|
||||
if(i >= 0) return; // Exists already
|
||||
|
||||
i = GetFreeID();
|
||||
m_d[i] = 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;
|
||||
}
|
||||
}
|
||||
}
|
298
ModernKeePassLib/Collections/ProtectedStringDictionary.cs
Normal file
298
ModernKeePassLib/Collections/ProtectedStringDictionary.cs
Normal file
@@ -0,0 +1,298 @@
|
||||
/*
|
||||
KeePass Password Safe - The Open-Source Password Manager
|
||||
Copyright (C) 2003-2019 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 ModernKeePassLib.Interfaces;
|
||||
using ModernKeePassLib.Security;
|
||||
using ModernKeePassLib.Utility;
|
||||
|
||||
#if KeePassLibSD
|
||||
using KeePassLibSD;
|
||||
#endif
|
||||
|
||||
namespace ModernKeePassLib.Collections
|
||||
{
|
||||
/// <summary>
|
||||
/// A list of <c>ProtectedString</c> objects (dictionary).
|
||||
/// </summary>
|
||||
public sealed class ProtectedStringDictionary :
|
||||
IDeepCloneable<ProtectedStringDictionary>,
|
||||
IEnumerable<KeyValuePair<string, ProtectedString>>
|
||||
{
|
||||
private SortedDictionary<string, ProtectedString> m_vStrings =
|
||||
new SortedDictionary<string, ProtectedString>();
|
||||
|
||||
/// <summary>
|
||||
/// Get the number of strings in this entry.
|
||||
/// </summary>
|
||||
public uint UCount
|
||||
{
|
||||
get { return (uint)m_vStrings.Count; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Construct a new list of protected strings.
|
||||
/// </summary>
|
||||
public ProtectedStringDictionary()
|
||||
{
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return m_vStrings.GetEnumerator();
|
||||
}
|
||||
|
||||
public IEnumerator<KeyValuePair<string, ProtectedString>> GetEnumerator()
|
||||
{
|
||||
return m_vStrings.GetEnumerator();
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
m_vStrings.Clear();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clone the current <c>ProtectedStringList</c> object, including all
|
||||
/// stored protected strings.
|
||||
/// </summary>
|
||||
/// <returns>New <c>ProtectedStringList</c> object.</returns>
|
||||
public ProtectedStringDictionary CloneDeep()
|
||||
{
|
||||
ProtectedStringDictionary plNew = new ProtectedStringDictionary();
|
||||
|
||||
foreach(KeyValuePair<string, ProtectedString> kvpStr in m_vStrings)
|
||||
{
|
||||
// ProtectedString objects are immutable
|
||||
plNew.Set(kvpStr.Key, kvpStr.Value);
|
||||
}
|
||||
|
||||
return plNew;
|
||||
}
|
||||
|
||||
[Obsolete]
|
||||
public bool EqualsDictionary(ProtectedStringDictionary dict)
|
||||
{
|
||||
return EqualsDictionary(dict, PwCompareOptions.None, MemProtCmpMode.None);
|
||||
}
|
||||
|
||||
[Obsolete]
|
||||
public bool EqualsDictionary(ProtectedStringDictionary dict,
|
||||
MemProtCmpMode mpCompare)
|
||||
{
|
||||
return EqualsDictionary(dict, PwCompareOptions.None, mpCompare);
|
||||
}
|
||||
|
||||
public bool EqualsDictionary(ProtectedStringDictionary dict,
|
||||
PwCompareOptions pwOpt, MemProtCmpMode mpCompare)
|
||||
{
|
||||
if(dict == null) { Debug.Assert(false); return false; }
|
||||
|
||||
bool bNeEqStd = ((pwOpt & PwCompareOptions.NullEmptyEquivStd) !=
|
||||
PwCompareOptions.None);
|
||||
if(!bNeEqStd)
|
||||
{
|
||||
if(m_vStrings.Count != dict.m_vStrings.Count) return false;
|
||||
}
|
||||
|
||||
foreach(KeyValuePair<string, ProtectedString> kvp in m_vStrings)
|
||||
{
|
||||
bool bStdField = PwDefs.IsStandardField(kvp.Key);
|
||||
ProtectedString ps = dict.Get(kvp.Key);
|
||||
|
||||
if(bNeEqStd && (ps == null) && bStdField)
|
||||
ps = ProtectedString.Empty;
|
||||
|
||||
if(ps == null) return false;
|
||||
|
||||
if(mpCompare == MemProtCmpMode.Full)
|
||||
{
|
||||
if(ps.IsProtected != kvp.Value.IsProtected) return false;
|
||||
}
|
||||
else if(mpCompare == MemProtCmpMode.CustomOnly)
|
||||
{
|
||||
if(!bStdField && (ps.IsProtected != kvp.Value.IsProtected))
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!ps.Equals(kvp.Value, false)) return false;
|
||||
}
|
||||
|
||||
if(bNeEqStd)
|
||||
{
|
||||
foreach(KeyValuePair<string, ProtectedString> kvp in dict.m_vStrings)
|
||||
{
|
||||
ProtectedString ps = Get(kvp.Key);
|
||||
|
||||
if(ps != null) continue; // Compared previously
|
||||
if(!PwDefs.IsStandardField(kvp.Key)) return false;
|
||||
if(!kvp.Value.IsEmpty) return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get one of the protected strings.
|
||||
/// </summary>
|
||||
/// <param name="strName">String identifier.</param>
|
||||
/// <returns>Protected string. If the string identified by
|
||||
/// <paramref name="strName" /> cannot be found, the function
|
||||
/// returns <c>null</c>.</returns>
|
||||
/// <exception cref="System.ArgumentNullException">Thrown if the input parameter
|
||||
/// is <c>null</c>.</exception>
|
||||
public ProtectedString Get(string strName)
|
||||
{
|
||||
Debug.Assert(strName != null); if(strName == null) throw new ArgumentNullException("strName");
|
||||
|
||||
ProtectedString ps;
|
||||
if(m_vStrings.TryGetValue(strName, out ps)) return ps;
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get one of the protected strings. The return value is never <c>null</c>.
|
||||
/// If the requested string cannot be found, an empty protected string
|
||||
/// object is returned.
|
||||
/// </summary>
|
||||
/// <param name="strName">String identifier.</param>
|
||||
/// <returns>Returns a protected string object. If the standard string
|
||||
/// has not been set yet, the return value is an empty string (<c>""</c>).</returns>
|
||||
/// <exception cref="System.ArgumentNullException">Thrown if the input
|
||||
/// parameter is <c>null</c>.</exception>
|
||||
public ProtectedString GetSafe(string strName)
|
||||
{
|
||||
Debug.Assert(strName != null); if(strName == null) throw new ArgumentNullException("strName");
|
||||
|
||||
ProtectedString ps;
|
||||
if(m_vStrings.TryGetValue(strName, out ps)) return ps;
|
||||
|
||||
return ProtectedString.Empty;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test if a named string exists.
|
||||
/// </summary>
|
||||
/// <param name="strName">Name of the string to try.</param>
|
||||
/// <returns>Returns <c>true</c> if the string exists, otherwise <c>false</c>.</returns>
|
||||
/// <exception cref="System.ArgumentNullException">Thrown if
|
||||
/// <paramref name="strName" /> is <c>null</c>.</exception>
|
||||
public bool Exists(string strName)
|
||||
{
|
||||
Debug.Assert(strName != null); if(strName == null) throw new ArgumentNullException("strName");
|
||||
|
||||
return m_vStrings.ContainsKey(strName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get one of the protected strings. If the string doesn't exist, the
|
||||
/// return value is an empty string (<c>""</c>).
|
||||
/// </summary>
|
||||
/// <param name="strName">Name of the requested string.</param>
|
||||
/// <returns>Requested string value or an empty string, if the named
|
||||
/// string doesn't exist.</returns>
|
||||
/// <exception cref="System.ArgumentNullException">Thrown if the input
|
||||
/// parameter is <c>null</c>.</exception>
|
||||
public string ReadSafe(string strName)
|
||||
{
|
||||
Debug.Assert(strName != null); if(strName == null) throw new ArgumentNullException("strName");
|
||||
|
||||
ProtectedString ps;
|
||||
if(m_vStrings.TryGetValue(strName, out ps))
|
||||
return ps.ReadString();
|
||||
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get one of the entry strings. If the string doesn't exist, the
|
||||
/// return value is an empty string (<c>""</c>). If the string is
|
||||
/// in-memory protected, the return value is <c>PwDefs.HiddenPassword</c>.
|
||||
/// </summary>
|
||||
/// <param name="strName">Name of the requested string.</param>
|
||||
/// <returns>Returns the requested string in plain-text or
|
||||
/// <c>PwDefs.HiddenPassword</c> if the string cannot be found.</returns>
|
||||
/// <exception cref="System.ArgumentNullException">Thrown if the input
|
||||
/// parameter is <c>null</c>.</exception>
|
||||
public string ReadSafeEx(string strName)
|
||||
{
|
||||
Debug.Assert(strName != null); if(strName == null) throw new ArgumentNullException("strName");
|
||||
|
||||
ProtectedString ps;
|
||||
if(m_vStrings.TryGetValue(strName, out ps))
|
||||
{
|
||||
if(ps.IsProtected) return PwDefs.HiddenPassword;
|
||||
return ps.ReadString();
|
||||
}
|
||||
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set a string.
|
||||
/// </summary>
|
||||
/// <param name="strField">Identifier of the string field to modify.</param>
|
||||
/// <param name="psNewValue">New value. This parameter must not be <c>null</c>.</param>
|
||||
/// <exception cref="System.ArgumentNullException">Thrown if one of the input
|
||||
/// parameters is <c>null</c>.</exception>
|
||||
public void Set(string strField, ProtectedString psNewValue)
|
||||
{
|
||||
Debug.Assert(strField != null); if(strField == null) throw new ArgumentNullException("strField");
|
||||
Debug.Assert(psNewValue != null); if(psNewValue == null) throw new ArgumentNullException("psNewValue");
|
||||
|
||||
m_vStrings[strField] = psNewValue;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Delete a string.
|
||||
/// </summary>
|
||||
/// <param name="strField">Name of the string field to delete.</param>
|
||||
/// <returns>Returns <c>true</c> if the field has been successfully
|
||||
/// removed, otherwise the return value is <c>false</c>.</returns>
|
||||
/// <exception cref="System.ArgumentNullException">Thrown if the input
|
||||
/// parameter is <c>null</c>.</exception>
|
||||
public bool Remove(string strField)
|
||||
{
|
||||
Debug.Assert(strField != null); if(strField == null) throw new ArgumentNullException("strField");
|
||||
|
||||
return m_vStrings.Remove(strField);
|
||||
}
|
||||
|
||||
public List<string> GetKeys()
|
||||
{
|
||||
return new List<string>(m_vStrings.Keys);
|
||||
}
|
||||
|
||||
public void EnableProtection(string strField, bool bProtect)
|
||||
{
|
||||
ProtectedString ps = Get(strField);
|
||||
if(ps == null) return; // Nothing to do, no assert
|
||||
|
||||
if(ps.IsProtected != bProtect)
|
||||
Set(strField, ps.WithProtection(bProtect));
|
||||
}
|
||||
}
|
||||
}
|
380
ModernKeePassLib/Collections/PwObjectList.cs
Normal file
380
ModernKeePassLib/Collections/PwObjectList.cs
Normal file
@@ -0,0 +1,380 @@
|
||||
/*
|
||||
KeePass Password Safe - The Open-Source Password Manager
|
||||
Copyright (C) 2003-2019 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 ModernKeePassLib.Interfaces;
|
||||
|
||||
namespace ModernKeePassLib.Collections
|
||||
{
|
||||
/// <summary>
|
||||
/// List of objects that implement <c>IDeepCloneable</c>,
|
||||
/// and cannot be <c>null</c>.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Type specifier.</typeparam>
|
||||
public sealed class PwObjectList<T> : IEnumerable<T>
|
||||
where T : class, IDeepCloneable<T>
|
||||
{
|
||||
private List<T> m_vObjects = new List<T>();
|
||||
|
||||
/// <summary>
|
||||
/// Get number of objects in this list.
|
||||
/// </summary>
|
||||
public uint UCount
|
||||
{
|
||||
get { return (uint)m_vObjects.Count; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Construct a new list of objects.
|
||||
/// </summary>
|
||||
public PwObjectList()
|
||||
{
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return m_vObjects.GetEnumerator();
|
||||
}
|
||||
|
||||
public IEnumerator<T> GetEnumerator()
|
||||
{
|
||||
return m_vObjects.GetEnumerator();
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
// Do not destroy contained objects!
|
||||
m_vObjects.Clear();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clone the current <c>PwObjectList</c>, including all
|
||||
/// stored objects (deep copy).
|
||||
/// </summary>
|
||||
/// <returns>New <c>PwObjectList</c>.</returns>
|
||||
public PwObjectList<T> CloneDeep()
|
||||
{
|
||||
PwObjectList<T> pl = new PwObjectList<T>();
|
||||
|
||||
foreach(T po in m_vObjects)
|
||||
pl.Add(po.CloneDeep());
|
||||
|
||||
return pl;
|
||||
}
|
||||
|
||||
public PwObjectList<T> CloneShallow()
|
||||
{
|
||||
PwObjectList<T> tNew = new PwObjectList<T>();
|
||||
|
||||
foreach(T po in m_vObjects) tNew.Add(po);
|
||||
|
||||
return tNew;
|
||||
}
|
||||
|
||||
public List<T> CloneShallowToList()
|
||||
{
|
||||
PwObjectList<T> tNew = CloneShallow();
|
||||
return tNew.m_vObjects;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add an object to this list.
|
||||
/// </summary>
|
||||
/// <param name="pwObject">Object to be added.</param>
|
||||
/// <exception cref="System.ArgumentNullException">Thrown if the input
|
||||
/// parameter is <c>null</c>.</exception>
|
||||
public void Add(T pwObject)
|
||||
{
|
||||
Debug.Assert(pwObject != null);
|
||||
if(pwObject == null) throw new ArgumentNullException("pwObject");
|
||||
|
||||
m_vObjects.Add(pwObject);
|
||||
}
|
||||
|
||||
public void Add(PwObjectList<T> vObjects)
|
||||
{
|
||||
Debug.Assert(vObjects != null);
|
||||
if(vObjects == null) throw new ArgumentNullException("vObjects");
|
||||
|
||||
foreach(T po in vObjects)
|
||||
{
|
||||
m_vObjects.Add(po);
|
||||
}
|
||||
}
|
||||
|
||||
public void Add(List<T> vObjects)
|
||||
{
|
||||
Debug.Assert(vObjects != null);
|
||||
if(vObjects == null) throw new ArgumentNullException("vObjects");
|
||||
|
||||
foreach(T po in vObjects)
|
||||
{
|
||||
m_vObjects.Add(po);
|
||||
}
|
||||
}
|
||||
|
||||
public void Insert(uint uIndex, T pwObject)
|
||||
{
|
||||
Debug.Assert(pwObject != null);
|
||||
if(pwObject == null) throw new ArgumentNullException("pwObject");
|
||||
|
||||
m_vObjects.Insert((int)uIndex, pwObject);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get an object of the list.
|
||||
/// </summary>
|
||||
/// <param name="uIndex">Index of the object to get. Must be valid, otherwise an
|
||||
/// exception is thrown.</param>
|
||||
/// <returns>Reference to an existing <c>T</c> object. Is never <c>null</c>.</returns>
|
||||
public T GetAt(uint uIndex)
|
||||
{
|
||||
Debug.Assert(uIndex < m_vObjects.Count);
|
||||
if(uIndex >= m_vObjects.Count) throw new ArgumentOutOfRangeException("uIndex");
|
||||
|
||||
return m_vObjects[(int)uIndex];
|
||||
}
|
||||
|
||||
public void SetAt(uint uIndex, T pwObject)
|
||||
{
|
||||
Debug.Assert(pwObject != null);
|
||||
if(pwObject == null) throw new ArgumentNullException("pwObject");
|
||||
if(uIndex >= (uint)m_vObjects.Count)
|
||||
throw new ArgumentOutOfRangeException("uIndex");
|
||||
|
||||
m_vObjects[(int)uIndex] = pwObject;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get a range of objects.
|
||||
/// </summary>
|
||||
/// <param name="uStartIndexIncl">Index of the first object to be
|
||||
/// returned (inclusive).</param>
|
||||
/// <param name="uEndIndexIncl">Index of the last object to be
|
||||
/// returned (inclusive).</param>
|
||||
/// <returns></returns>
|
||||
public List<T> GetRange(uint uStartIndexIncl, uint uEndIndexIncl)
|
||||
{
|
||||
if(uStartIndexIncl >= (uint)m_vObjects.Count)
|
||||
throw new ArgumentOutOfRangeException("uStartIndexIncl");
|
||||
if(uEndIndexIncl >= (uint)m_vObjects.Count)
|
||||
throw new ArgumentOutOfRangeException("uEndIndexIncl");
|
||||
if(uStartIndexIncl > uEndIndexIncl)
|
||||
throw new ArgumentException();
|
||||
|
||||
List<T> list = new List<T>((int)(uEndIndexIncl - uStartIndexIncl) + 1);
|
||||
for(uint u = uStartIndexIncl; u <= uEndIndexIncl; ++u)
|
||||
{
|
||||
list.Add(m_vObjects[(int)u]);
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
public int IndexOf(T pwReference)
|
||||
{
|
||||
Debug.Assert(pwReference != null); if(pwReference == null) throw new ArgumentNullException("pwReference");
|
||||
|
||||
return m_vObjects.IndexOf(pwReference);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Delete an object of this list. The object to be deleted is identified
|
||||
/// by a reference handle.
|
||||
/// </summary>
|
||||
/// <param name="pwReference">Reference of the object to be deleted.</param>
|
||||
/// <returns>Returns <c>true</c> if the object was deleted, <c>false</c> if
|
||||
/// the object wasn't found in this list.</returns>
|
||||
/// <exception cref="System.ArgumentNullException">Thrown if the input
|
||||
/// parameter is <c>null</c>.</exception>
|
||||
public bool Remove(T pwReference)
|
||||
{
|
||||
Debug.Assert(pwReference != null); if(pwReference == null) throw new ArgumentNullException("pwReference");
|
||||
|
||||
return m_vObjects.Remove(pwReference);
|
||||
}
|
||||
|
||||
public void RemoveAt(uint uIndex)
|
||||
{
|
||||
m_vObjects.RemoveAt((int)uIndex);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Move an object up or down.
|
||||
/// </summary>
|
||||
/// <param name="tObject">The object to be moved.</param>
|
||||
/// <param name="bUp">Move one up. If <c>false</c>, move one down.</param>
|
||||
public void MoveOne(T tObject, bool bUp)
|
||||
{
|
||||
Debug.Assert(tObject != null);
|
||||
if(tObject == null) throw new ArgumentNullException("tObject");
|
||||
|
||||
int nCount = m_vObjects.Count;
|
||||
if(nCount <= 1) return;
|
||||
|
||||
int nIndex = m_vObjects.IndexOf(tObject);
|
||||
if(nIndex < 0) { Debug.Assert(false); return; }
|
||||
|
||||
if(bUp && (nIndex > 0)) // No assert for top item
|
||||
{
|
||||
T tTemp = m_vObjects[nIndex - 1];
|
||||
m_vObjects[nIndex - 1] = m_vObjects[nIndex];
|
||||
m_vObjects[nIndex] = tTemp;
|
||||
}
|
||||
else if(!bUp && (nIndex != (nCount - 1))) // No assert for bottom item
|
||||
{
|
||||
T tTemp = m_vObjects[nIndex + 1];
|
||||
m_vObjects[nIndex + 1] = m_vObjects[nIndex];
|
||||
m_vObjects[nIndex] = tTemp;
|
||||
}
|
||||
}
|
||||
|
||||
public void MoveOne(T[] vObjects, bool bUp)
|
||||
{
|
||||
Debug.Assert(vObjects != null);
|
||||
if(vObjects == null) throw new ArgumentNullException("vObjects");
|
||||
|
||||
List<int> lIndices = new List<int>();
|
||||
foreach(T t in vObjects)
|
||||
{
|
||||
if(t == null) { Debug.Assert(false); continue; }
|
||||
|
||||
int p = IndexOf(t);
|
||||
if(p >= 0) lIndices.Add(p);
|
||||
else { Debug.Assert(false); }
|
||||
}
|
||||
|
||||
MoveOne(lIndices.ToArray(), bUp);
|
||||
}
|
||||
|
||||
public void MoveOne(int[] vIndices, bool bUp)
|
||||
{
|
||||
Debug.Assert(vIndices != null);
|
||||
if(vIndices == null) throw new ArgumentNullException("vIndices");
|
||||
|
||||
int n = m_vObjects.Count;
|
||||
if(n <= 1) return; // No moving possible
|
||||
|
||||
int m = vIndices.Length;
|
||||
if(m == 0) return; // Nothing to move
|
||||
|
||||
int[] v = new int[m];
|
||||
Array.Copy(vIndices, v, m);
|
||||
Array.Sort<int>(v);
|
||||
|
||||
if((bUp && (v[0] <= 0)) || (!bUp && (v[m - 1] >= (n - 1))))
|
||||
return; // Moving as a block is not possible
|
||||
|
||||
int iStart = (bUp ? 0 : (m - 1));
|
||||
int iExcl = (bUp ? m : -1);
|
||||
int iStep = (bUp ? 1 : -1);
|
||||
|
||||
for(int i = iStart; i != iExcl; i += iStep)
|
||||
{
|
||||
int p = v[i];
|
||||
if((p < 0) || (p >= n)) { Debug.Assert(false); continue; }
|
||||
|
||||
T t = m_vObjects[p];
|
||||
|
||||
if(bUp)
|
||||
{
|
||||
Debug.Assert(p > 0);
|
||||
m_vObjects.RemoveAt(p);
|
||||
m_vObjects.Insert(p - 1, t);
|
||||
}
|
||||
else // Down
|
||||
{
|
||||
Debug.Assert(p < (n - 1));
|
||||
m_vObjects.RemoveAt(p);
|
||||
m_vObjects.Insert(p + 1, t);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Move some of the objects in this list to the top/bottom.
|
||||
/// </summary>
|
||||
/// <param name="vObjects">List of objects to be moved.</param>
|
||||
/// <param name="bTop">Move to top. If <c>false</c>, move to bottom.</param>
|
||||
public void MoveTopBottom(T[] vObjects, bool bTop)
|
||||
{
|
||||
Debug.Assert(vObjects != null);
|
||||
if(vObjects == null) throw new ArgumentNullException("vObjects");
|
||||
|
||||
if(vObjects.Length == 0) return;
|
||||
|
||||
int nCount = m_vObjects.Count;
|
||||
foreach(T t in vObjects) m_vObjects.Remove(t);
|
||||
|
||||
if(bTop)
|
||||
{
|
||||
int nPos = 0;
|
||||
foreach(T t in vObjects)
|
||||
{
|
||||
m_vObjects.Insert(nPos, t);
|
||||
++nPos;
|
||||
}
|
||||
}
|
||||
else // Move to bottom
|
||||
{
|
||||
foreach(T t in vObjects) m_vObjects.Add(t);
|
||||
}
|
||||
|
||||
Debug.Assert(nCount == m_vObjects.Count);
|
||||
if(nCount != m_vObjects.Count)
|
||||
throw new ArgumentException("At least one of the T objects in the vObjects list doesn't exist!");
|
||||
}
|
||||
|
||||
public void Sort(IComparer<T> tComparer)
|
||||
{
|
||||
if(tComparer == null) throw new ArgumentNullException("tComparer");
|
||||
|
||||
m_vObjects.Sort(tComparer);
|
||||
}
|
||||
|
||||
public void Sort(Comparison<T> tComparison)
|
||||
{
|
||||
if(tComparison == null) throw new ArgumentNullException("tComparison");
|
||||
|
||||
m_vObjects.Sort(tComparison);
|
||||
}
|
||||
|
||||
public static PwObjectList<T> FromArray(T[] tArray)
|
||||
{
|
||||
if(tArray == null) throw new ArgumentNullException("tArray");
|
||||
|
||||
PwObjectList<T> l = new PwObjectList<T>();
|
||||
foreach(T t in tArray) { l.Add(t); }
|
||||
return l;
|
||||
}
|
||||
|
||||
public static PwObjectList<T> FromList(List<T> tList)
|
||||
{
|
||||
if(tList == null) throw new ArgumentNullException("tList");
|
||||
|
||||
PwObjectList<T> l = new PwObjectList<T>();
|
||||
l.Add(tList);
|
||||
return l;
|
||||
}
|
||||
}
|
||||
}
|
232
ModernKeePassLib/Collections/PwObjectPool.cs
Normal file
232
ModernKeePassLib/Collections/PwObjectPool.cs
Normal file
@@ -0,0 +1,232 @@
|
||||
/*
|
||||
KeePass Password Safe - The Open-Source Password Manager
|
||||
Copyright (C) 2003-2019 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 ModernKeePassLib.Delegates;
|
||||
using ModernKeePassLib.Interfaces;
|
||||
using ModernKeePassLib.Utility;
|
||||
|
||||
#if KeePassLibSD
|
||||
using KeePassLibSD;
|
||||
#endif
|
||||
|
||||
namespace ModernKeePassLib.Collections
|
||||
{
|
||||
public sealed class PwObjectPool
|
||||
{
|
||||
private 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 Dictionary<PwUuid, ulong> m_dUuidToId =
|
||||
new Dictionary<PwUuid, ulong>();
|
||||
private 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 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
130
ModernKeePassLib/Collections/StringDictionaryEx.cs
Normal file
130
ModernKeePassLib/Collections/StringDictionaryEx.cs
Normal file
@@ -0,0 +1,130 @@
|
||||
/*
|
||||
KeePass Password Safe - The Open-Source Password Manager
|
||||
Copyright (C) 2003-2019 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.Text;
|
||||
using System.Diagnostics;
|
||||
|
||||
using ModernKeePassLib.Interfaces;
|
||||
|
||||
#if KeePassLibSD
|
||||
using KeePassLibSD;
|
||||
#endif
|
||||
|
||||
namespace ModernKeePassLib.Collections
|
||||
{
|
||||
public sealed class StringDictionaryEx : IDeepCloneable<StringDictionaryEx>,
|
||||
IEnumerable<KeyValuePair<string, string>>, IEquatable<StringDictionaryEx>
|
||||
{
|
||||
private SortedDictionary<string, string> m_dict =
|
||||
new SortedDictionary<string, string>();
|
||||
|
||||
public int Count
|
||||
{
|
||||
get { return m_dict.Count; }
|
||||
}
|
||||
|
||||
public StringDictionaryEx()
|
||||
{
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return m_dict.GetEnumerator();
|
||||
}
|
||||
|
||||
public IEnumerator<KeyValuePair<string, string>> GetEnumerator()
|
||||
{
|
||||
return m_dict.GetEnumerator();
|
||||
}
|
||||
|
||||
public StringDictionaryEx CloneDeep()
|
||||
{
|
||||
StringDictionaryEx sdNew = new StringDictionaryEx();
|
||||
|
||||
foreach(KeyValuePair<string, string> kvp in m_dict)
|
||||
sdNew.m_dict[kvp.Key] = kvp.Value; // Strings are immutable
|
||||
|
||||
return sdNew;
|
||||
}
|
||||
|
||||
public bool Equals(StringDictionaryEx sdOther)
|
||||
{
|
||||
if(sdOther == null) { Debug.Assert(false); return false; }
|
||||
|
||||
if(m_dict.Count != sdOther.m_dict.Count) return false;
|
||||
|
||||
foreach(KeyValuePair<string, string> kvp in sdOther.m_dict)
|
||||
{
|
||||
string str = Get(kvp.Key);
|
||||
if((str == null) || (str != kvp.Value)) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public string Get(string strName)
|
||||
{
|
||||
if(strName == null) { Debug.Assert(false); throw new ArgumentNullException("strName"); }
|
||||
|
||||
string s;
|
||||
if(m_dict.TryGetValue(strName, out s)) return s;
|
||||
return null;
|
||||
}
|
||||
|
||||
public bool Exists(string strName)
|
||||
{
|
||||
if(strName == null) { Debug.Assert(false); throw new ArgumentNullException("strName"); }
|
||||
|
||||
return m_dict.ContainsKey(strName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set a string.
|
||||
/// </summary>
|
||||
/// <param name="strField">Identifier of the string field to modify.</param>
|
||||
/// <param name="strNewValue">New value. This parameter must not be <c>null</c>.</param>
|
||||
/// <exception cref="System.ArgumentNullException">Thrown if one of the input
|
||||
/// parameters is <c>null</c>.</exception>
|
||||
public void Set(string strField, string strNewValue)
|
||||
{
|
||||
if(strField == null) { Debug.Assert(false); throw new ArgumentNullException("strField"); }
|
||||
if(strNewValue == null) { Debug.Assert(false); throw new ArgumentNullException("strNewValue"); }
|
||||
|
||||
m_dict[strField] = strNewValue;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Delete a string.
|
||||
/// </summary>
|
||||
/// <param name="strField">Name of the string field to delete.</param>
|
||||
/// <returns>Returns <c>true</c>, if the field has been successfully
|
||||
/// removed. Otherwise, the return value is <c>false</c>.</returns>
|
||||
/// <exception cref="System.ArgumentNullException">Thrown if the input
|
||||
/// parameter is <c>null</c>.</exception>
|
||||
public bool Remove(string strField)
|
||||
{
|
||||
if(strField == null) { Debug.Assert(false); throw new ArgumentNullException("strField"); }
|
||||
|
||||
return m_dict.Remove(strField);
|
||||
}
|
||||
}
|
||||
}
|
415
ModernKeePassLib/Collections/VariantDictionary.cs
Normal file
415
ModernKeePassLib/Collections/VariantDictionary.cs
Normal file
@@ -0,0 +1,415 @@
|
||||
/*
|
||||
KeePass Password Safe - The Open-Source Password Manager
|
||||
Copyright (C) 2003-2019 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 ModernKeePassLib.Resources;
|
||||
using ModernKeePassLib.Utility;
|
||||
|
||||
namespace ModernKeePassLib.Collections
|
||||
{
|
||||
public class VariantDictionary
|
||||
{
|
||||
private const ushort VdVersion = 0x0100;
|
||||
private const ushort VdmCritical = 0xFF00;
|
||||
private const ushort VdmInfo = 0x00FF;
|
||||
|
||||
private 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()
|
||||
{
|
||||
Debug.Assert((VdmCritical & VdmInfo) == ushort.MinValue);
|
||||
Debug.Assert((VdmCritical | VdmInfo) == ushort.MaxValue);
|
||||
}
|
||||
|
||||
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, 0, pbName.Length);
|
||||
|
||||
int cbValue = MemUtil.BytesToInt32(MemUtil.Read(ms, 4));
|
||||
byte[] pbValue = MemUtil.Read(ms, cbValue);
|
||||
if(pbValue.Length != cbValue)
|
||||
throw new EndOfStreamException(KLRes.FileCorrupted);
|
||||
|
||||
switch(btType)
|
||||
{
|
||||
case (byte)VdType.UInt32:
|
||||
if(cbValue == 4)
|
||||
d.SetUInt32(strName, MemUtil.BytesToUInt32(pbValue));
|
||||
else { Debug.Assert(false); }
|
||||
break;
|
||||
|
||||
case (byte)VdType.UInt64:
|
||||
if(cbValue == 8)
|
||||
d.SetUInt64(strName, MemUtil.BytesToUInt64(pbValue));
|
||||
else { Debug.Assert(false); }
|
||||
break;
|
||||
|
||||
case (byte)VdType.Bool:
|
||||
if(cbValue == 1)
|
||||
d.SetBool(strName, (pbValue[0] != 0));
|
||||
else { Debug.Assert(false); }
|
||||
break;
|
||||
|
||||
case (byte)VdType.Int32:
|
||||
if(cbValue == 4)
|
||||
d.SetInt32(strName, MemUtil.BytesToInt32(pbValue));
|
||||
else { Debug.Assert(false); }
|
||||
break;
|
||||
|
||||
case (byte)VdType.Int64:
|
||||
if(cbValue == 8)
|
||||
d.SetInt64(strName, MemUtil.BytesToInt64(pbValue));
|
||||
else { Debug.Assert(false); }
|
||||
break;
|
||||
|
||||
case (byte)VdType.String:
|
||||
d.SetString(strName, StrUtil.Utf8.GetString(pbValue, 0, pbValue.Length));
|
||||
break;
|
||||
|
||||
case (byte)VdType.ByteArray:
|
||||
d.SetByteArray(strName, pbValue);
|
||||
break;
|
||||
|
||||
default:
|
||||
Debug.Assert(false); // Unknown type
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Debug.Assert(ms.ReadByte() < 0);
|
||||
}
|
||||
|
||||
return d;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user