mirror of
https://github.com/wismna/ModernKeePass.git
synced 2025-10-03 23:50:18 -04:00
ModernKeePassLib custom PCL version
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
KeePass Password Safe - The Open-Source Password Manager
|
||||
Copyright (C) 2003-2012 Dominik Reichl <dominik.reichl@t-online.de>
|
||||
Copyright (C) 2003-2014 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
|
||||
@@ -18,22 +18,34 @@
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
//using System.Drawing;
|
||||
using ModernKeePassLib.WinRTAdaptors;
|
||||
using System.IO;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
#if PCL
|
||||
using Windows.Security.Cryptography;
|
||||
#else
|
||||
using System.Security.Cryptography;
|
||||
#endif
|
||||
using System.Globalization;
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.InteropServices.WindowsRuntime;
|
||||
using Windows.Security.Cryptography.DataProtection;
|
||||
using Windows.Storage.Streams;
|
||||
using Windows.UI;
|
||||
using ModernKeePassLib.Collections;
|
||||
using ModernKeePassLib.Cryptography.PasswordGenerator;
|
||||
using ModernKeePassLib.Security;
|
||||
using ModernKeePassLib.Resources;
|
||||
using UnicodeEncoding = System.Text.UnicodeEncoding;
|
||||
|
||||
namespace ModernKeePassLib.Utility
|
||||
{
|
||||
/// <summary>
|
||||
/// Character stream class.
|
||||
/// </summary>
|
||||
public sealed class CharStream
|
||||
/// <summary>
|
||||
/// Character stream class.
|
||||
/// </summary>
|
||||
public sealed class CharStream
|
||||
{
|
||||
private string m_strString = string.Empty;
|
||||
private int m_nPos = 0;
|
||||
@@ -208,53 +220,47 @@ namespace ModernKeePassLib.Utility
|
||||
{
|
||||
get
|
||||
{
|
||||
if(m_lEncs == null)
|
||||
{
|
||||
m_lEncs = new List<StrEncodingInfo>();
|
||||
#if !KeePassWinRT
|
||||
m_lEncs.Add(new StrEncodingInfo(StrEncodingType.Default,
|
||||
if(m_lEncs != null) return m_lEncs;
|
||||
|
||||
List<StrEncodingInfo> l = new List<StrEncodingInfo>();
|
||||
|
||||
#if !KeePassLibSD
|
||||
Encoding.Default.EncodingName,
|
||||
l.Add(new StrEncodingInfo(StrEncodingType.Default,
|
||||
#if PCL || KeePassRT
|
||||
StrUtil.Utf8.WebName, StrUtil.Utf8, 1, null));
|
||||
#else
|
||||
Encoding.Default.WebName,
|
||||
#endif
|
||||
Encoding.Default,
|
||||
(uint)Encoding.Default.GetBytes("a").Length, null));
|
||||
m_lEncs.Add(new StrEncodingInfo(StrEncodingType.Ascii,
|
||||
"ASCII", Encoding.ASCII, 1, null));
|
||||
m_lEncs.Add(new StrEncodingInfo(StrEncodingType.Utf7,
|
||||
"Unicode (UTF-7)", Encoding.UTF7, 1, null));
|
||||
m_lEncs.Add(new StrEncodingInfo(StrEncodingType.Utf8,
|
||||
"Unicode (UTF-8)", StrUtil.Utf8, 1, new byte[] { 0xEF, 0xBB, 0xBF }));
|
||||
m_lEncs.Add(new StrEncodingInfo(StrEncodingType.Utf16LE,
|
||||
"Unicode (UTF-16 LE)", new UnicodeEncoding(false, false),
|
||||
2, new byte[] { 0xFF, 0xFE }));
|
||||
m_lEncs.Add(new StrEncodingInfo(StrEncodingType.Utf16BE,
|
||||
"Unicode (UTF-16 BE)", new UnicodeEncoding(true, false),
|
||||
2, new byte[] { 0xFE, 0xFF }));
|
||||
#if !KeePassLibSD
|
||||
m_lEncs.Add(new StrEncodingInfo(StrEncodingType.Utf32LE,
|
||||
"Unicode (UTF-32 LE)", new UTF32Encoding(false, false),
|
||||
4, new byte[] { 0xFF, 0xFE, 0x0, 0x0 }));
|
||||
m_lEncs.Add(new StrEncodingInfo(StrEncodingType.Utf32BE,
|
||||
"Unicode (UTF-32 BE)", new UTF32Encoding(true, false),
|
||||
4, new byte[] { 0x0, 0x0, 0xFE, 0xFF }));
|
||||
Encoding.Default.EncodingName,
|
||||
#else
|
||||
Encoding.Default.WebName,
|
||||
#endif
|
||||
#else // KeePassWinRT
|
||||
m_lEncs.Add(new StrEncodingInfo(StrEncodingType.Utf8,
|
||||
"Unicode (UTF-8)", StrUtil.Utf8, 1, new byte[] { 0xEF, 0xBB, 0xBF }));
|
||||
m_lEncs.Add(new StrEncodingInfo(StrEncodingType.Utf16LE,
|
||||
"Unicode (UTF-16 LE)", new UnicodeEncoding(false, false),
|
||||
2, new byte[] { 0xFF, 0xFE }));
|
||||
m_lEncs.Add(new StrEncodingInfo(StrEncodingType.Utf16BE,
|
||||
"Unicode (UTF-16 BE)", new UnicodeEncoding(true, false),
|
||||
2, new byte[] { 0xFE, 0xFF }));
|
||||
Encoding.Default,
|
||||
(uint)Encoding.Default.GetBytes("a").Length, null));
|
||||
#endif
|
||||
#if !PCL && !KeePassRT
|
||||
l.Add(new StrEncodingInfo(StrEncodingType.Ascii,
|
||||
"ASCII", Encoding.ASCII, 1, null));
|
||||
l.Add(new StrEncodingInfo(StrEncodingType.Utf7,
|
||||
"Unicode (UTF-7)", Encoding.UTF7, 1, null));
|
||||
#endif
|
||||
l.Add(new StrEncodingInfo(StrEncodingType.Utf8,
|
||||
"Unicode (UTF-8)", StrUtil.Utf8, 1, new byte[] { 0xEF, 0xBB, 0xBF }));
|
||||
l.Add(new StrEncodingInfo(StrEncodingType.Utf16LE,
|
||||
"Unicode (UTF-16 LE)", new UnicodeEncoding(false, false),
|
||||
2, new byte[] { 0xFF, 0xFE }));
|
||||
l.Add(new StrEncodingInfo(StrEncodingType.Utf16BE,
|
||||
"Unicode (UTF-16 BE)", new UnicodeEncoding(true, false),
|
||||
2, new byte[] { 0xFE, 0xFF }));
|
||||
#if (!PCL && !KeePassLibSD && !KeePassRT)
|
||||
l.Add(new StrEncodingInfo(StrEncodingType.Utf32LE,
|
||||
"Unicode (UTF-32 LE)", new UTF32Encoding(false, false),
|
||||
4, new byte[] { 0xFF, 0xFE, 0x0, 0x0 }));
|
||||
l.Add(new StrEncodingInfo(StrEncodingType.Utf32BE,
|
||||
"Unicode (UTF-32 BE)", new UTF32Encoding(true, false),
|
||||
4, new byte[] { 0x0, 0x0, 0xFE, 0xFF }));
|
||||
#endif
|
||||
}
|
||||
|
||||
return m_lEncs;
|
||||
m_lEncs = l;
|
||||
return l;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -282,16 +288,21 @@ namespace ModernKeePassLib.Utility
|
||||
// {
|
||||
// char ch = str[i];
|
||||
// if((int)ch >= 256)
|
||||
// {
|
||||
// sbEncoded.Append("\\u");
|
||||
// sbEncoded.Append((int)ch);
|
||||
// sbEncoded.Append('?');
|
||||
// }
|
||||
// sbEncoded.Append(StrUtil.RtfEncodeChar(ch));
|
||||
// else sbEncoded.Append(ch);
|
||||
// }
|
||||
// return sbEncoded.ToString();
|
||||
// }
|
||||
|
||||
public static string RtfEncodeChar(char ch)
|
||||
{
|
||||
// Unicode character values must be encoded using
|
||||
// 16-bit numbers (decimal); Unicode values greater
|
||||
// than 32767 must be expressed as negative numbers
|
||||
short sh = (short)ch;
|
||||
return ("\\u" + sh.ToString(NumberFormatInfo.InvariantInfo) + "?");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert a string into a valid HTML sequence representing that string.
|
||||
/// </summary>
|
||||
@@ -308,7 +319,7 @@ namespace ModernKeePassLib.Utility
|
||||
str = str.Replace("\'", @"'");
|
||||
|
||||
str = NormalizeNewLines(str, false);
|
||||
str = str.Replace("\n", @"<br />");
|
||||
str = str.Replace("\n", @"<br />" + Environment.NewLine);
|
||||
|
||||
return str;
|
||||
}
|
||||
@@ -479,59 +490,59 @@ namespace ModernKeePassLib.Utility
|
||||
/// <returns>String representing the exception.</returns>
|
||||
public static string FormatException(Exception excp)
|
||||
{
|
||||
Debug.Assert(false, "not yet implemented");
|
||||
return "";
|
||||
#if TODO
|
||||
string strText = string.Empty;
|
||||
|
||||
if(excp.Message != null)
|
||||
strText += excp.Message + MessageService.NewLine;
|
||||
strText += excp.Message + Environment.NewLine;
|
||||
#if !KeePassLibSD
|
||||
if(excp.Source != null)
|
||||
strText += excp.Source + MessageService.NewLine;
|
||||
strText += excp.Source + Environment.NewLine;
|
||||
#endif
|
||||
if(excp.StackTrace != null)
|
||||
strText += excp.StackTrace + MessageService.NewLine;
|
||||
strText += excp.StackTrace + Environment.NewLine;
|
||||
#if !KeePassLibSD
|
||||
#if !PCL && !KeePassRT
|
||||
if(excp.TargetSite != null)
|
||||
strText += excp.TargetSite.ToString() + MessageService.NewLine;
|
||||
#endif
|
||||
|
||||
if(excp.Data != null)
|
||||
{
|
||||
strText += MessageService.NewLine;
|
||||
strText += Environment.NewLine;
|
||||
foreach(DictionaryEntry de in excp.Data)
|
||||
strText += @"'" + de.Key + @"' -> '" + de.Value + @"'" +
|
||||
MessageService.NewLine;
|
||||
Environment.NewLine;
|
||||
}
|
||||
#endif
|
||||
|
||||
if(excp.InnerException != null)
|
||||
{
|
||||
strText += MessageService.NewLine + "Inner:" + MessageService.NewLine;
|
||||
strText += Environment.NewLine + "Inner:" + Environment.NewLine;
|
||||
if(excp.InnerException.Message != null)
|
||||
strText += excp.InnerException.Message + MessageService.NewLine;
|
||||
strText += excp.InnerException.Message + Environment.NewLine;
|
||||
#if !KeePassLibSD
|
||||
if(excp.InnerException.Source != null)
|
||||
strText += excp.InnerException.Source + MessageService.NewLine;
|
||||
strText += excp.InnerException.Source + Environment.NewLine;
|
||||
#endif
|
||||
if(excp.InnerException.StackTrace != null)
|
||||
strText += excp.InnerException.StackTrace + MessageService.NewLine;
|
||||
strText += excp.InnerException.StackTrace + Environment.NewLine;
|
||||
#if !KeePassLibSD
|
||||
#if !PCL && !KeePassRT
|
||||
if(excp.InnerException.TargetSite != null)
|
||||
strText += excp.InnerException.TargetSite.ToString();
|
||||
#endif
|
||||
|
||||
if(excp.InnerException.Data != null)
|
||||
{
|
||||
strText += MessageService.NewLine;
|
||||
strText += Environment.NewLine;
|
||||
foreach(DictionaryEntry de in excp.InnerException.Data)
|
||||
strText += @"'" + de.Key + @"' -> '" + de.Value + @"'" +
|
||||
MessageService.NewLine;
|
||||
Environment.NewLine;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
return strText;
|
||||
#endif
|
||||
}
|
||||
|
||||
public static bool TryParseUShort(string str, out ushort u)
|
||||
@@ -550,7 +561,25 @@ namespace ModernKeePassLib.Utility
|
||||
return int.TryParse(str, out n);
|
||||
#else
|
||||
try { n = int.Parse(str); return true; }
|
||||
catch(Exception) { n = 0; return false; }
|
||||
catch(Exception) { n = 0; }
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
public static bool TryParseIntInvariant(string str, out int n)
|
||||
{
|
||||
#if !KeePassLibSD
|
||||
return int.TryParse(str, NumberStyles.Integer,
|
||||
NumberFormatInfo.InvariantInfo, out n);
|
||||
#else
|
||||
try
|
||||
{
|
||||
n = int.Parse(str, NumberStyles.Integer,
|
||||
NumberFormatInfo.InvariantInfo);
|
||||
return true;
|
||||
}
|
||||
catch(Exception) { n = 0; }
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -560,7 +589,25 @@ namespace ModernKeePassLib.Utility
|
||||
return uint.TryParse(str, out u);
|
||||
#else
|
||||
try { u = uint.Parse(str); return true; }
|
||||
catch(Exception) { u = 0; return false; }
|
||||
catch(Exception) { u = 0; }
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
public static bool TryParseUIntInvariant(string str, out uint u)
|
||||
{
|
||||
#if !KeePassLibSD
|
||||
return uint.TryParse(str, NumberStyles.Integer,
|
||||
NumberFormatInfo.InvariantInfo, out u);
|
||||
#else
|
||||
try
|
||||
{
|
||||
u = uint.Parse(str, NumberStyles.Integer,
|
||||
NumberFormatInfo.InvariantInfo);
|
||||
return true;
|
||||
}
|
||||
catch(Exception) { u = 0; }
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -570,7 +617,25 @@ namespace ModernKeePassLib.Utility
|
||||
return long.TryParse(str, out n);
|
||||
#else
|
||||
try { n = long.Parse(str); return true; }
|
||||
catch(Exception) { n = 0; return false; }
|
||||
catch(Exception) { n = 0; }
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
public static bool TryParseLongInvariant(string str, out long n)
|
||||
{
|
||||
#if !KeePassLibSD
|
||||
return long.TryParse(str, NumberStyles.Integer,
|
||||
NumberFormatInfo.InvariantInfo, out n);
|
||||
#else
|
||||
try
|
||||
{
|
||||
n = long.Parse(str, NumberStyles.Integer,
|
||||
NumberFormatInfo.InvariantInfo);
|
||||
return true;
|
||||
}
|
||||
catch(Exception) { n = 0; }
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -580,7 +645,25 @@ namespace ModernKeePassLib.Utility
|
||||
return ulong.TryParse(str, out u);
|
||||
#else
|
||||
try { u = ulong.Parse(str); return true; }
|
||||
catch(Exception) { u = 0; return false; }
|
||||
catch(Exception) { u = 0; }
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
public static bool TryParseULongInvariant(string str, out ulong u)
|
||||
{
|
||||
#if !KeePassLibSD
|
||||
return ulong.TryParse(str, NumberStyles.Integer,
|
||||
NumberFormatInfo.InvariantInfo, out u);
|
||||
#else
|
||||
try
|
||||
{
|
||||
u = ulong.Parse(str, NumberStyles.Integer,
|
||||
NumberFormatInfo.InvariantInfo);
|
||||
return true;
|
||||
}
|
||||
catch(Exception) { u = 0; }
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -648,19 +731,34 @@ namespace ModernKeePassLib.Utility
|
||||
Debug.Assert(strText != null); // No throw
|
||||
if(string.IsNullOrEmpty(strText)) return strText;
|
||||
|
||||
char[] vChars = strText.ToCharArray();
|
||||
StringBuilder sb = new StringBuilder(strText.Length, strText.Length);
|
||||
char ch;
|
||||
int nLength = strText.Length;
|
||||
StringBuilder sb = new StringBuilder(nLength);
|
||||
|
||||
for(int i = 0; i < vChars.Length; ++i)
|
||||
for(int i = 0; i < nLength; ++i)
|
||||
{
|
||||
ch = vChars[i];
|
||||
char ch = strText[i];
|
||||
|
||||
if(((ch >= 0x20) && (ch <= 0xD7FF)) ||
|
||||
(ch == 0x9) || (ch == 0xA) || (ch == 0xD) ||
|
||||
((ch >= 0xE000) && (ch <= 0xFFFD)))
|
||||
if(((ch >= '\u0020') && (ch <= '\uD7FF')) ||
|
||||
(ch == '\u0009') || (ch == '\u000A') || (ch == '\u000D') ||
|
||||
((ch >= '\uE000') && (ch <= '\uFFFD')))
|
||||
sb.Append(ch);
|
||||
// Range ((ch >= 0x10000) && (ch <= 0x10FFFF)) excluded
|
||||
else if((ch >= '\uD800') && (ch <= '\uDBFF')) // High surrogate
|
||||
{
|
||||
if((i + 1) < nLength)
|
||||
{
|
||||
char chLow = strText[i + 1];
|
||||
if((chLow >= '\uDC00') && (chLow <= '\uDFFF')) // Low sur.
|
||||
{
|
||||
sb.Append(ch);
|
||||
sb.Append(chLow);
|
||||
++i;
|
||||
}
|
||||
else { Debug.Assert(false); } // Low sur. invalid
|
||||
}
|
||||
else { Debug.Assert(false); } // Low sur. missing
|
||||
}
|
||||
|
||||
Debug.Assert((ch < '\uDC00') || (ch > '\uDFFF')); // Lonely low sur.
|
||||
}
|
||||
|
||||
return sb.ToString();
|
||||
@@ -669,22 +767,24 @@ namespace ModernKeePassLib.Utility
|
||||
private static Regex m_rxNaturalSplit = null;
|
||||
public static int CompareNaturally(string strX, string strY)
|
||||
{
|
||||
Debug.Assert(false, "not yet implemented");
|
||||
return 0;
|
||||
#if TODO
|
||||
Debug.Assert(strX != null);
|
||||
if(strX == null) throw new ArgumentNullException("strX");
|
||||
Debug.Assert(strY != null);
|
||||
if(strY == null) throw new ArgumentNullException("strY");
|
||||
|
||||
if(NativeMethods.SupportsStrCmpNaturally)
|
||||
return NativeMethods.StrCmpNaturally(strX, strY);
|
||||
/*if(NativeMethods.SupportsStrCmpNaturally)
|
||||
return NativeMethods.StrCmpNaturally(strX, strY);*/
|
||||
|
||||
strX = strX.ToLower(); // Case-insensitive comparison
|
||||
strY = strY.ToLower();
|
||||
|
||||
if(m_rxNaturalSplit == null)
|
||||
m_rxNaturalSplit = new Regex(@"([0-9]+)", RegexOptions.Compiled);
|
||||
m_rxNaturalSplit = new Regex(@"([0-9]+)",
|
||||
#if PCL || KeePassRT
|
||||
RegexOptions.None);
|
||||
#else
|
||||
RegexOptions.Compiled);
|
||||
#endif
|
||||
|
||||
string[] vPartsX = m_rxNaturalSplit.Split(strX);
|
||||
string[] vPartsY = m_rxNaturalSplit.Split(strY);
|
||||
@@ -716,7 +816,6 @@ namespace ModernKeePassLib.Utility
|
||||
if(vPartsX.Length == vPartsY.Length) return 0;
|
||||
if(vPartsX.Length < vPartsY.Length) return -1;
|
||||
return 1;
|
||||
#endif
|
||||
}
|
||||
|
||||
public static string RemoveAccelerator(string strMenuText)
|
||||
@@ -740,6 +839,54 @@ namespace ModernKeePassLib.Utility
|
||||
return str;
|
||||
}
|
||||
|
||||
public static string AddAccelerator(string strMenuText,
|
||||
List<char> lAvailKeys)
|
||||
{
|
||||
if(strMenuText == null) { Debug.Assert(false); return null; }
|
||||
if(lAvailKeys == null) { Debug.Assert(false); return strMenuText; }
|
||||
|
||||
int xa = -1, xs = 0;
|
||||
for(int i = 0; i < strMenuText.Length; ++i)
|
||||
{
|
||||
char ch = strMenuText[i];
|
||||
|
||||
#if KeePassLibSD
|
||||
char chUpper = char.ToUpper(ch);
|
||||
#else
|
||||
char chUpper = char.ToUpperInvariant(ch);
|
||||
#endif
|
||||
xa = lAvailKeys.IndexOf(chUpper);
|
||||
if(xa >= 0) { xs = i; break; }
|
||||
|
||||
#if KeePassLibSD
|
||||
char chLower = char.ToLower(ch);
|
||||
#else
|
||||
char chLower = char.ToLowerInvariant(ch);
|
||||
#endif
|
||||
xa = lAvailKeys.IndexOf(chLower);
|
||||
if(xa >= 0) { xs = i; break; }
|
||||
}
|
||||
|
||||
if(xa < 0) return strMenuText;
|
||||
|
||||
lAvailKeys.RemoveAt(xa);
|
||||
return strMenuText.Insert(xs, @"&");
|
||||
}
|
||||
|
||||
public static string EncodeMenuText(string strText)
|
||||
{
|
||||
if(strText == null) throw new ArgumentNullException("strText");
|
||||
|
||||
return strText.Replace(@"&", @"&&");
|
||||
}
|
||||
|
||||
public static string EncodeToolTipText(string strText)
|
||||
{
|
||||
if(strText == null) throw new ArgumentNullException("strText");
|
||||
|
||||
return strText.Replace(@"&", @"&&&");
|
||||
}
|
||||
|
||||
public static bool IsHexString(string str, bool bStrict)
|
||||
{
|
||||
if(str == null) throw new ArgumentNullException("str");
|
||||
@@ -888,6 +1035,36 @@ namespace ModernKeePassLib.Utility
|
||||
}
|
||||
}
|
||||
|
||||
public static string GetNewLineSeq(string str)
|
||||
{
|
||||
if(str == null) { Debug.Assert(false); return Environment.NewLine; }
|
||||
|
||||
int n = str.Length, nLf = 0, nCr = 0, nCrLf = 0;
|
||||
char chLast = char.MinValue;
|
||||
for(int i = 0; i < n; ++i)
|
||||
{
|
||||
char ch = str[i];
|
||||
|
||||
if(ch == '\r') ++nCr;
|
||||
else if(ch == '\n')
|
||||
{
|
||||
++nLf;
|
||||
if(chLast == '\r') ++nCrLf;
|
||||
}
|
||||
|
||||
chLast = ch;
|
||||
}
|
||||
|
||||
nCr -= nCrLf;
|
||||
nLf -= nCrLf;
|
||||
|
||||
int nMax = Math.Max(nCrLf, Math.Max(nCr, nLf));
|
||||
if(nMax == 0) return Environment.NewLine;
|
||||
|
||||
if(nCrLf == nMax) return "\r\n";
|
||||
return ((nLf == nMax) ? "\n" : "\r");
|
||||
}
|
||||
|
||||
public static string AlphaNumericOnly(string str)
|
||||
{
|
||||
if(string.IsNullOrEmpty(str)) return str;
|
||||
@@ -965,85 +1142,85 @@ namespace ModernKeePassLib.Utility
|
||||
|
||||
public static string VersionToString(ulong uVersion)
|
||||
{
|
||||
return VersionToString(uVersion, false);
|
||||
return VersionToString(uVersion, 1U);
|
||||
}
|
||||
|
||||
[Obsolete]
|
||||
public static string VersionToString(ulong uVersion,
|
||||
bool bEnsureAtLeastTwoComp)
|
||||
{
|
||||
string str = string.Empty;
|
||||
bool bMultiComp = false;
|
||||
return VersionToString(uVersion, (bEnsureAtLeastTwoComp ? 2U : 1U));
|
||||
}
|
||||
|
||||
public static string VersionToString(ulong uVersion, uint uMinComp)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
uint uComp = 0;
|
||||
|
||||
for(int i = 0; i < 4; ++i)
|
||||
{
|
||||
ushort us = (ushort)(uVersion & 0xFFFFUL);
|
||||
if(uVersion == 0UL) break;
|
||||
|
||||
if((us != 0) || (str.Length > 0))
|
||||
{
|
||||
if(str.Length > 0)
|
||||
{
|
||||
str = "." + str;
|
||||
bMultiComp = true;
|
||||
}
|
||||
ushort us = (ushort)(uVersion >> 48);
|
||||
|
||||
str = us.ToString() + str;
|
||||
}
|
||||
if(sb.Length > 0) sb.Append('.');
|
||||
|
||||
uVersion >>= 16;
|
||||
sb.Append(us.ToString(NumberFormatInfo.InvariantInfo));
|
||||
++uComp;
|
||||
|
||||
uVersion <<= 16;
|
||||
}
|
||||
|
||||
if(bEnsureAtLeastTwoComp && !bMultiComp && (str.Length > 0))
|
||||
str += ".0";
|
||||
while(uComp < uMinComp)
|
||||
{
|
||||
if(sb.Length > 0) sb.Append('.');
|
||||
|
||||
return str;
|
||||
sb.Append('0');
|
||||
++uComp;
|
||||
}
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
private static readonly byte[] m_pbOptEnt = { 0xA5, 0x74, 0x2E, 0xEC };
|
||||
|
||||
public static string EncryptString(string strPlainText)
|
||||
{
|
||||
Debug.Assert(false, "not yet implemented");
|
||||
return null;
|
||||
#if TODO
|
||||
if(string.IsNullOrEmpty(strPlainText)) return string.Empty;
|
||||
|
||||
try
|
||||
{
|
||||
byte[] pbPlain = StrUtil.Utf8.GetBytes(strPlainText);
|
||||
byte[] pbEnc = ProtectedData.Protect(pbPlain, m_pbOptEnt,
|
||||
DataProtectionScope.CurrentUser);
|
||||
|
||||
#if !KeePassLibSD
|
||||
var provider = new DataProtectionProvider("Local=user");
|
||||
var pbEnc = provider.ProtectAsync(pbPlain.AsBuffer()).GetResults();
|
||||
|
||||
#if (!PCL && !KeePassLibSD && !KeePassRT)
|
||||
return Convert.ToBase64String(pbEnc, Base64FormattingOptions.None);
|
||||
#else
|
||||
return Convert.ToBase64String(pbEnc);
|
||||
return Convert.ToBase64String(pbEnc.ToArray());
|
||||
#endif
|
||||
}
|
||||
catch(Exception) { Debug.Assert(false); }
|
||||
|
||||
return strPlainText;
|
||||
#endif
|
||||
}
|
||||
|
||||
public static string DecryptString(string strCipherText)
|
||||
{
|
||||
Debug.Assert(false, "not yet implemented");
|
||||
return null;
|
||||
#if TODO
|
||||
if(string.IsNullOrEmpty(strCipherText)) return string.Empty;
|
||||
|
||||
try
|
||||
{
|
||||
byte[] pbEnc = Convert.FromBase64String(strCipherText);
|
||||
byte[] pbPlain = ProtectedData.Unprotect(pbEnc, m_pbOptEnt,
|
||||
DataProtectionScope.CurrentUser);
|
||||
|
||||
var provider = new DataProtectionProvider("Local=user");
|
||||
var pbPlain = provider.UnprotectAsync(pbEnc.AsBuffer()).GetResults().ToArray();
|
||||
|
||||
return StrUtil.Utf8.GetString(pbPlain, 0, pbPlain.Length);
|
||||
}
|
||||
catch(Exception) { Debug.Assert(false); }
|
||||
|
||||
return strCipherText;
|
||||
#endif
|
||||
}
|
||||
|
||||
public static string SerializeIntArray(int[] vNumbers)
|
||||
@@ -1054,7 +1231,7 @@ namespace ModernKeePassLib.Utility
|
||||
for(int i = 0; i < vNumbers.Length; ++i)
|
||||
{
|
||||
if(i > 0) sb.Append(' ');
|
||||
sb.Append(vNumbers[i]);
|
||||
sb.Append(vNumbers[i].ToString(NumberFormatInfo.InvariantInfo));
|
||||
}
|
||||
|
||||
return sb.ToString();
|
||||
@@ -1071,7 +1248,7 @@ namespace ModernKeePassLib.Utility
|
||||
for(int i = 0; i < vParts.Length; ++i)
|
||||
{
|
||||
int n;
|
||||
if(!TryParseInt(vParts[i], out n)) { Debug.Assert(false); }
|
||||
if(!TryParseIntInvariant(vParts[i], out n)) { Debug.Assert(false); }
|
||||
v[i] = n;
|
||||
}
|
||||
|
||||
@@ -1131,7 +1308,7 @@ namespace ModernKeePassLib.Utility
|
||||
Array.Reverse(pb);
|
||||
for(int i = 0; i < pb.Length; ++i) pb[i] = (byte)(pb[i] ^ 0x65);
|
||||
|
||||
#if !KeePassLibSD && TODO
|
||||
#if (!PCL && !KeePassLibSD && !KeePassRT)
|
||||
return Convert.ToBase64String(pb, Base64FormattingOptions.None);
|
||||
#else
|
||||
return Convert.ToBase64String(pb);
|
||||
@@ -1249,9 +1426,33 @@ namespace ModernKeePassLib.Utility
|
||||
|
||||
public static bool IsDataUri(string strUri)
|
||||
{
|
||||
if(strUri == null) { Debug.Assert(false); return false; }
|
||||
return IsDataUri(strUri, null);
|
||||
}
|
||||
|
||||
return strUri.StartsWith("data:", StrUtil.CaseIgnoreCmp);
|
||||
public static bool IsDataUri(string strUri, string strReqMimeType)
|
||||
{
|
||||
if(strUri == null) { Debug.Assert(false); return false; }
|
||||
// strReqMimeType may be null
|
||||
|
||||
const string strPrefix = "data:";
|
||||
if(!strUri.StartsWith(strPrefix, StrUtil.CaseIgnoreCmp))
|
||||
return false;
|
||||
|
||||
int iC = strUri.IndexOf(',');
|
||||
if(iC < 0) return false;
|
||||
|
||||
if(!string.IsNullOrEmpty(strReqMimeType))
|
||||
{
|
||||
int iS = strUri.IndexOf(';', 0, iC);
|
||||
int iTerm = ((iS >= 0) ? iS : iC);
|
||||
|
||||
string strMime = strUri.Substring(strPrefix.Length,
|
||||
iTerm - strPrefix.Length);
|
||||
if(!strMime.Equals(strReqMimeType, StrUtil.CaseIgnoreCmp))
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -1267,7 +1468,7 @@ namespace ModernKeePassLib.Utility
|
||||
|
||||
if(strMimeType == null) strMimeType = "application/octet-stream";
|
||||
|
||||
#if !KeePassLibSD && TODO
|
||||
#if (!PCL && !KeePassLibSD && !KeePassRT)
|
||||
return ("data:" + strMimeType + ";base64," + Convert.ToBase64String(
|
||||
pbData, Base64FormattingOptions.None));
|
||||
#else
|
||||
@@ -1283,9 +1484,6 @@ namespace ModernKeePassLib.Utility
|
||||
/// <returns>Decoded binary data.</returns>
|
||||
public static byte[] DataUriToData(string strDataUri)
|
||||
{
|
||||
Debug.Assert(false, "not yet implemented");
|
||||
return null;
|
||||
#if TODO
|
||||
if(strDataUri == null) throw new ArgumentNullException("strDataUri");
|
||||
if(!strDataUri.StartsWith("data:", StrUtil.CaseIgnoreCmp)) return null;
|
||||
|
||||
@@ -1301,20 +1499,25 @@ namespace ModernKeePassLib.Utility
|
||||
|
||||
MemoryStream ms = new MemoryStream();
|
||||
|
||||
#if PCL || KeePassRT
|
||||
Encoding enc = StrUtil.Utf8;
|
||||
#else
|
||||
Encoding enc = Encoding.ASCII;
|
||||
#endif
|
||||
|
||||
string[] v = strData.Split('%');
|
||||
byte[] pb = Encoding.ASCII.GetBytes(v[0]);
|
||||
byte[] pb = enc.GetBytes(v[0]);
|
||||
ms.Write(pb, 0, pb.Length);
|
||||
for(int i = 1; i < v.Length; ++i)
|
||||
{
|
||||
ms.WriteByte(Convert.ToByte(v[i].Substring(0, 2), 16));
|
||||
pb = Encoding.ASCII.GetBytes(v[i].Substring(2));
|
||||
pb = enc.GetBytes(v[i].Substring(2));
|
||||
ms.Write(pb, 0, pb.Length);
|
||||
}
|
||||
|
||||
pb = ms.ToArray();
|
||||
ms.Close();
|
||||
ms.Dispose();
|
||||
return pb;
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -1358,5 +1561,72 @@ namespace ModernKeePassLib.Utility
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static string[] m_vPrefSepChars = null;
|
||||
/// <summary>
|
||||
/// Find a character that does not occur within a given text.
|
||||
/// </summary>
|
||||
public static char GetUnusedChar(string strText)
|
||||
{
|
||||
if(strText == null) { Debug.Assert(false); return '@'; }
|
||||
|
||||
if(m_vPrefSepChars == null)
|
||||
m_vPrefSepChars = new string[5] {
|
||||
"@!$%#/\\:;,.*-_?",
|
||||
PwCharSet.UpperCase, PwCharSet.LowerCase,
|
||||
PwCharSet.Digits, PwCharSet.PrintableAsciiSpecial
|
||||
};
|
||||
|
||||
for(int i = 0; i < m_vPrefSepChars.Length; ++i)
|
||||
{
|
||||
foreach(char ch in m_vPrefSepChars[i])
|
||||
{
|
||||
if(strText.IndexOf(ch) < 0) return ch;
|
||||
}
|
||||
}
|
||||
|
||||
for(char ch = '\u00C0'; ch < char.MaxValue; ++ch)
|
||||
{
|
||||
if(strText.IndexOf(ch) < 0) return ch;
|
||||
}
|
||||
|
||||
return char.MinValue;
|
||||
}
|
||||
|
||||
public static char ByteToSafeChar(byte bt)
|
||||
{
|
||||
const char chDefault = '.';
|
||||
|
||||
// 00-1F are C0 control chars
|
||||
if(bt < 0x20) return chDefault;
|
||||
|
||||
// 20-7F are basic Latin; 7F is DEL
|
||||
if(bt < 0x7F) return (char)bt;
|
||||
|
||||
// 80-9F are C1 control chars
|
||||
if(bt < 0xA0) return chDefault;
|
||||
|
||||
// A0-FF are Latin-1 supplement; AD is soft hyphen
|
||||
if(bt == 0xAD) return '-';
|
||||
return (char)bt;
|
||||
}
|
||||
|
||||
public static int Count(string str, string strNeedle)
|
||||
{
|
||||
if(str == null) { Debug.Assert(false); return 0; }
|
||||
if(string.IsNullOrEmpty(strNeedle)) { Debug.Assert(false); return 0; }
|
||||
|
||||
int iOffset = 0, iCount = 0;
|
||||
while(iOffset < str.Length)
|
||||
{
|
||||
int p = str.IndexOf(strNeedle, iOffset);
|
||||
if(p < 0) break;
|
||||
|
||||
++iCount;
|
||||
iOffset = p + 1;
|
||||
}
|
||||
|
||||
return iCount;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user