mirror of
https://github.com/wismna/ModernKeePassLib.git
synced 2025-10-03 23:50:20 -04:00
Setup solution
This commit is contained in:
103
ModernKeePassLib/Utility/AppLogEx.cs
Normal file
103
ModernKeePassLib/Utility/AppLogEx.cs
Normal file
@@ -0,0 +1,103 @@
|
||||
/*
|
||||
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;
|
||||
|
||||
#if !KeePassLibSD
|
||||
using System.IO.Compression;
|
||||
#endif
|
||||
|
||||
namespace ModernKeePassLib.Utility
|
||||
{
|
||||
/// <summary>
|
||||
/// Application-wide logging services.
|
||||
/// </summary>
|
||||
public static class AppLogEx
|
||||
{
|
||||
private static StreamWriter m_swOut = null;
|
||||
|
||||
public static void Open(string strPrefix)
|
||||
{
|
||||
// Logging is not enabled in normal builds of KeePass!
|
||||
/*
|
||||
AppLogEx.Close();
|
||||
|
||||
Debug.Assert(strPrefix != null);
|
||||
if(strPrefix == null) strPrefix = "Log";
|
||||
|
||||
try
|
||||
{
|
||||
string strDirSep = string.Empty;
|
||||
strDirSep += UrlUtil.LocalDirSepChar;
|
||||
|
||||
string strTemp = UrlUtil.GetTempPath();
|
||||
if(!strTemp.EndsWith(strDirSep))
|
||||
strTemp += strDirSep;
|
||||
|
||||
string strPath = strTemp + strPrefix + "-";
|
||||
Debug.Assert(strPath.IndexOf('/') < 0);
|
||||
|
||||
DateTime dtNow = DateTime.UtcNow;
|
||||
string strTime = dtNow.ToString("s");
|
||||
strTime = strTime.Replace('T', '-');
|
||||
strTime = strTime.Replace(':', '-');
|
||||
|
||||
strPath += strTime + "-" + Environment.TickCount.ToString(
|
||||
NumberFormatInfo.InvariantInfo) + ".log.gz";
|
||||
|
||||
FileStream fsOut = new FileStream(strPath, FileMode.Create,
|
||||
FileAccess.Write, FileShare.None);
|
||||
GZipStream gz = new GZipStream(fsOut, CompressionMode.Compress);
|
||||
m_swOut = new StreamWriter(gz);
|
||||
|
||||
AppLogEx.Log("Started logging on " + dtNow.ToString("s") + ".");
|
||||
}
|
||||
catch(Exception) { Debug.Assert(false); }
|
||||
*/
|
||||
}
|
||||
|
||||
public static void Close()
|
||||
{
|
||||
if(m_swOut == null) return;
|
||||
|
||||
m_swOut.Dispose();
|
||||
m_swOut = null;
|
||||
}
|
||||
|
||||
public static void Log(string strText)
|
||||
{
|
||||
if(m_swOut == null) return;
|
||||
|
||||
if(strText == null) m_swOut.WriteLine();
|
||||
else m_swOut.WriteLine(strText);
|
||||
}
|
||||
|
||||
public static void Log(Exception ex)
|
||||
{
|
||||
if(m_swOut == null) return;
|
||||
|
||||
if(ex == null) m_swOut.WriteLine();
|
||||
else m_swOut.WriteLine(ex.ToString());
|
||||
}
|
||||
}
|
||||
}
|
57
ModernKeePassLib/Utility/ColorTranslator.cs
Normal file
57
ModernKeePassLib/Utility/ColorTranslator.cs
Normal file
@@ -0,0 +1,57 @@
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.Globalization;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace ModernKeePassLib.Utility
|
||||
{
|
||||
/// <summary>
|
||||
/// Replacement for System.Drawing.ColorTranslator.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Colors are stored in the kdbx database file in HTML format (#XXXXXX).
|
||||
/// </remarks>
|
||||
public static class ColorTranslator
|
||||
{
|
||||
static Regex longForm = new Regex("^#([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})$");
|
||||
|
||||
/// <summary>
|
||||
/// Converts an HTML color value to a Color.
|
||||
/// </summary>
|
||||
/// <returns>The Color.</returns>
|
||||
/// <param name="htmlColor">HTML color code.</param>
|
||||
/// <exception cref="ArgumentNullException">If htmlColor is null.</exception>
|
||||
/// <exception cref="ArgumentException">If htmlColor did not match the pattern "#XXXXXX".</exception>
|
||||
/// <remarks>
|
||||
/// Currently only understands "#XXXXXX". "#XXX" or named colors will
|
||||
/// throw and exception.
|
||||
/// </remarks>
|
||||
public static Color FromHtml(string htmlColor)
|
||||
{
|
||||
if (htmlColor == null)
|
||||
throw new ArgumentNullException("htmlColor");
|
||||
Match match = longForm.Match(htmlColor);
|
||||
if (match.Success) {
|
||||
var r = int.Parse(match.Groups[1].Value, NumberStyles.HexNumber);
|
||||
var g = int.Parse(match.Groups[2].Value, NumberStyles.HexNumber);
|
||||
var b = int.Parse(match.Groups[3].Value, NumberStyles.HexNumber);
|
||||
return Color.FromArgb(r, g, b);
|
||||
}
|
||||
throw new ArgumentException(string.Format("Could not parse HTML color '{0}'.", htmlColor), "htmlColor");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a color to an HTML color code.
|
||||
/// </summary>
|
||||
/// <returns>String containing the color code.</returns>
|
||||
/// <param name="htmlColor">The Color to convert</param>
|
||||
/// <remarks>
|
||||
/// The string is in the format "#XXXXXX"
|
||||
/// </remarks>
|
||||
public static string ToHtml(Color htmlColor)
|
||||
{
|
||||
return string.Format("#{0:x2}{1:x2}{2:x2}", htmlColor.R, htmlColor.G, htmlColor.B);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
17
ModernKeePassLib/Utility/GfxUtil.PCL.cs
Normal file
17
ModernKeePassLib/Utility/GfxUtil.PCL.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using Windows.UI.Xaml.Controls;
|
||||
|
||||
namespace ModernKeePassLib.Utility
|
||||
{
|
||||
public class GfxUtil
|
||||
{
|
||||
public static Image LoadImage(byte[] pb)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Image ScaleImage(Image m_imgOrg, int? w, int? h, ScaleTransformFlags flags)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
441
ModernKeePassLib/Utility/GfxUtil.cs
Normal file
441
ModernKeePassLib/Utility/GfxUtil.cs
Normal file
@@ -0,0 +1,441 @@
|
||||
/*
|
||||
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;
|
||||
|
||||
#if !KeePassUAP
|
||||
using System.Drawing;
|
||||
using System.Drawing.Drawing2D;
|
||||
using System.Drawing.Imaging;
|
||||
#endif
|
||||
|
||||
namespace ModernKeePassLib.Utility
|
||||
{
|
||||
public static class GfxUtil
|
||||
{
|
||||
#if (!KeePassLibSD && !KeePassUAP)
|
||||
private sealed class GfxImage
|
||||
{
|
||||
public byte[] Data;
|
||||
|
||||
public int Width;
|
||||
public int Height;
|
||||
|
||||
public GfxImage(byte[] pbData, int w, int h)
|
||||
{
|
||||
this.Data = pbData;
|
||||
this.Width = w;
|
||||
this.Height = h;
|
||||
}
|
||||
|
||||
#if DEBUG
|
||||
// For debugger display
|
||||
public override string ToString()
|
||||
{
|
||||
return (this.Width.ToString() + "x" + this.Height.ToString());
|
||||
}
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
#if KeePassUAP
|
||||
public static Image LoadImage(byte[] pb)
|
||||
{
|
||||
if(pb == null) throw new ArgumentNullException("pb");
|
||||
|
||||
MemoryStream ms = new MemoryStream(pb, false);
|
||||
try { return Image.FromStream(ms); }
|
||||
finally { ms.Close(); }
|
||||
}
|
||||
#else
|
||||
public static Image LoadImage(byte[] pb)
|
||||
{
|
||||
if(pb == null) throw new ArgumentNullException("pb");
|
||||
|
||||
#if !KeePassLibSD
|
||||
// First try to load the data as ICO and afterwards as
|
||||
// normal image, because trying to load an ICO using
|
||||
// the normal image loading methods can result in a
|
||||
// low resolution image
|
||||
try
|
||||
{
|
||||
Image imgIco = ExtractBestImageFromIco(pb);
|
||||
if(imgIco != null) return imgIco;
|
||||
}
|
||||
catch(Exception) { Debug.Assert(false); }
|
||||
#endif
|
||||
|
||||
MemoryStream ms = new MemoryStream(pb, false);
|
||||
try { return LoadImagePriv(ms); }
|
||||
finally { ms.Close(); }
|
||||
}
|
||||
|
||||
private static Image LoadImagePriv(Stream s)
|
||||
{
|
||||
// Image.FromStream wants the stream to be open during
|
||||
// the whole lifetime of the image; as we can't guarantee
|
||||
// this, we make a copy of the image
|
||||
Image imgSrc = null;
|
||||
try
|
||||
{
|
||||
#if !KeePassLibSD
|
||||
imgSrc = Image.FromStream(s);
|
||||
Bitmap bmp = new Bitmap(imgSrc.Width, imgSrc.Height,
|
||||
PixelFormat.Format32bppArgb);
|
||||
|
||||
try
|
||||
{
|
||||
bmp.SetResolution(imgSrc.HorizontalResolution,
|
||||
imgSrc.VerticalResolution);
|
||||
Debug.Assert(bmp.Size == imgSrc.Size);
|
||||
}
|
||||
catch(Exception) { Debug.Assert(false); }
|
||||
#else
|
||||
imgSrc = new Bitmap(s);
|
||||
Bitmap bmp = new Bitmap(imgSrc.Width, imgSrc.Height);
|
||||
#endif
|
||||
|
||||
using(Graphics g = Graphics.FromImage(bmp))
|
||||
{
|
||||
g.Clear(Color.Transparent);
|
||||
|
||||
#if !KeePassLibSD
|
||||
g.DrawImageUnscaled(imgSrc, 0, 0);
|
||||
#else
|
||||
g.DrawImage(imgSrc, 0, 0);
|
||||
#endif
|
||||
}
|
||||
|
||||
return bmp;
|
||||
}
|
||||
finally { if(imgSrc != null) imgSrc.Dispose(); }
|
||||
}
|
||||
|
||||
#if !KeePassLibSD
|
||||
private static Image ExtractBestImageFromIco(byte[] pb)
|
||||
{
|
||||
List<GfxImage> l = UnpackIco(pb);
|
||||
if((l == null) || (l.Count == 0)) return null;
|
||||
|
||||
long qMax = 0;
|
||||
foreach(GfxImage gi in l)
|
||||
{
|
||||
if(gi.Width == 0) gi.Width = 256;
|
||||
if(gi.Height == 0) gi.Height = 256;
|
||||
|
||||
qMax = Math.Max(qMax, (long)gi.Width * (long)gi.Height);
|
||||
}
|
||||
|
||||
byte[] pbHdrPng = new byte[] {
|
||||
0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A
|
||||
};
|
||||
byte[] pbHdrJpeg = new byte[] { 0xFF, 0xD8, 0xFF };
|
||||
|
||||
Image imgBest = null;
|
||||
int bppBest = -1;
|
||||
|
||||
foreach(GfxImage gi in l)
|
||||
{
|
||||
if(((long)gi.Width * (long)gi.Height) < qMax) continue;
|
||||
|
||||
byte[] pbImg = gi.Data;
|
||||
Image img = null;
|
||||
try
|
||||
{
|
||||
if((pbImg.Length > pbHdrPng.Length) &&
|
||||
MemUtil.ArraysEqual(pbHdrPng,
|
||||
MemUtil.Mid<byte>(pbImg, 0, pbHdrPng.Length)))
|
||||
img = GfxUtil.LoadImage(pbImg);
|
||||
else if((pbImg.Length > pbHdrJpeg.Length) &&
|
||||
MemUtil.ArraysEqual(pbHdrJpeg,
|
||||
MemUtil.Mid<byte>(pbImg, 0, pbHdrJpeg.Length)))
|
||||
img = GfxUtil.LoadImage(pbImg);
|
||||
else
|
||||
{
|
||||
using(MemoryStream ms = new MemoryStream(pb, false))
|
||||
{
|
||||
using(Icon ico = new Icon(ms, gi.Width, gi.Height))
|
||||
{
|
||||
img = ico.ToBitmap();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch(Exception) { Debug.Assert(false); }
|
||||
|
||||
if(img == null) continue;
|
||||
|
||||
if((img.Width < gi.Width) || (img.Height < gi.Height))
|
||||
{
|
||||
Debug.Assert(false);
|
||||
img.Dispose();
|
||||
continue;
|
||||
}
|
||||
|
||||
int bpp = GetBitsPerPixel(img.PixelFormat);
|
||||
if(bpp > bppBest)
|
||||
{
|
||||
if(imgBest != null) imgBest.Dispose();
|
||||
|
||||
imgBest = img;
|
||||
bppBest = bpp;
|
||||
}
|
||||
else img.Dispose();
|
||||
}
|
||||
|
||||
return imgBest;
|
||||
}
|
||||
|
||||
private static List<GfxImage> UnpackIco(byte[] pb)
|
||||
{
|
||||
if(pb == null) { Debug.Assert(false); return null; }
|
||||
|
||||
const int SizeICONDIR = 6;
|
||||
const int SizeICONDIRENTRY = 16;
|
||||
|
||||
if(pb.Length < SizeICONDIR) return null;
|
||||
if(MemUtil.BytesToUInt16(pb, 0) != 0) return null; // Reserved, 0
|
||||
if(MemUtil.BytesToUInt16(pb, 2) != 1) return null; // ICO type, 1
|
||||
|
||||
int n = MemUtil.BytesToUInt16(pb, 4);
|
||||
if(n < 0) { Debug.Assert(false); return null; }
|
||||
|
||||
int cbDir = SizeICONDIR + (n * SizeICONDIRENTRY);
|
||||
if(pb.Length < cbDir) return null;
|
||||
|
||||
List<GfxImage> l = new List<GfxImage>();
|
||||
int iOffset = SizeICONDIR;
|
||||
for(int i = 0; i < n; ++i)
|
||||
{
|
||||
int w = pb[iOffset];
|
||||
int h = pb[iOffset + 1];
|
||||
if((w < 0) || (h < 0)) { Debug.Assert(false); return null; }
|
||||
|
||||
int cb = MemUtil.BytesToInt32(pb, iOffset + 8);
|
||||
if(cb <= 0) return null; // Data must have header (even BMP)
|
||||
|
||||
int p = MemUtil.BytesToInt32(pb, iOffset + 12);
|
||||
if(p < cbDir) return null;
|
||||
if((p + cb) > pb.Length) return null;
|
||||
|
||||
try
|
||||
{
|
||||
byte[] pbImage = MemUtil.Mid<byte>(pb, p, cb);
|
||||
GfxImage img = new GfxImage(pbImage, w, h);
|
||||
l.Add(img);
|
||||
}
|
||||
catch(Exception) { Debug.Assert(false); return null; }
|
||||
|
||||
iOffset += SizeICONDIRENTRY;
|
||||
}
|
||||
|
||||
return l;
|
||||
}
|
||||
|
||||
private static int GetBitsPerPixel(PixelFormat f)
|
||||
{
|
||||
int bpp = 0;
|
||||
switch(f)
|
||||
{
|
||||
case PixelFormat.Format1bppIndexed:
|
||||
bpp = 1;
|
||||
break;
|
||||
|
||||
case PixelFormat.Format4bppIndexed:
|
||||
bpp = 4;
|
||||
break;
|
||||
|
||||
case PixelFormat.Format8bppIndexed:
|
||||
bpp = 8;
|
||||
break;
|
||||
|
||||
case PixelFormat.Format16bppArgb1555:
|
||||
case PixelFormat.Format16bppGrayScale:
|
||||
case PixelFormat.Format16bppRgb555:
|
||||
case PixelFormat.Format16bppRgb565:
|
||||
bpp = 16;
|
||||
break;
|
||||
|
||||
case PixelFormat.Format24bppRgb:
|
||||
bpp = 24;
|
||||
break;
|
||||
|
||||
case PixelFormat.Format32bppArgb:
|
||||
case PixelFormat.Format32bppPArgb:
|
||||
case PixelFormat.Format32bppRgb:
|
||||
bpp = 32;
|
||||
break;
|
||||
|
||||
case PixelFormat.Format48bppRgb:
|
||||
bpp = 48;
|
||||
break;
|
||||
|
||||
case PixelFormat.Format64bppArgb:
|
||||
case PixelFormat.Format64bppPArgb:
|
||||
bpp = 64;
|
||||
break;
|
||||
|
||||
default:
|
||||
Debug.Assert(false);
|
||||
break;
|
||||
}
|
||||
|
||||
return bpp;
|
||||
}
|
||||
|
||||
public static Image ScaleImage(Image img, int w, int h)
|
||||
{
|
||||
return ScaleImage(img, w, h, ScaleTransformFlags.None);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resize an image.
|
||||
/// </summary>
|
||||
/// <param name="img">Image to resize.</param>
|
||||
/// <param name="w">Width of the returned image.</param>
|
||||
/// <param name="h">Height of the returned image.</param>
|
||||
/// <param name="f">Flags to customize scaling behavior.</param>
|
||||
/// <returns>Resized image. This object is always different
|
||||
/// from <paramref name="img" /> (i.e. they can be
|
||||
/// disposed separately).</returns>
|
||||
public static Image ScaleImage(Image img, int w, int h,
|
||||
ScaleTransformFlags f)
|
||||
{
|
||||
if(img == null) throw new ArgumentNullException("img");
|
||||
if(w < 0) throw new ArgumentOutOfRangeException("w");
|
||||
if(h < 0) throw new ArgumentOutOfRangeException("h");
|
||||
|
||||
bool bUIIcon = ((f & ScaleTransformFlags.UIIcon) !=
|
||||
ScaleTransformFlags.None);
|
||||
|
||||
// We must return a Bitmap object for UIUtil.CreateScaledImage
|
||||
Bitmap bmp = new Bitmap(w, h, PixelFormat.Format32bppArgb);
|
||||
using(Graphics g = Graphics.FromImage(bmp))
|
||||
{
|
||||
g.Clear(Color.Transparent);
|
||||
|
||||
g.SmoothingMode = SmoothingMode.HighQuality;
|
||||
g.CompositingQuality = CompositingQuality.HighQuality;
|
||||
|
||||
int wSrc = img.Width;
|
||||
int hSrc = img.Height;
|
||||
|
||||
InterpolationMode im = InterpolationMode.HighQualityBicubic;
|
||||
if((wSrc > 0) && (hSrc > 0))
|
||||
{
|
||||
if(bUIIcon && ((w % wSrc) == 0) && ((h % hSrc) == 0))
|
||||
im = InterpolationMode.NearestNeighbor;
|
||||
// else if((w < wSrc) && (h < hSrc))
|
||||
// im = InterpolationMode.HighQualityBilinear;
|
||||
}
|
||||
else { Debug.Assert(false); }
|
||||
g.InterpolationMode = im;
|
||||
|
||||
RectangleF rSource = new RectangleF(0.0f, 0.0f, wSrc, hSrc);
|
||||
RectangleF rDest = new RectangleF(0.0f, 0.0f, w, h);
|
||||
AdjustScaleRects(ref rSource, ref rDest);
|
||||
|
||||
g.DrawImage(img, rDest, rSource, GraphicsUnit.Pixel);
|
||||
}
|
||||
|
||||
return bmp;
|
||||
}
|
||||
|
||||
internal static void AdjustScaleRects(ref RectangleF rSource,
|
||||
ref RectangleF rDest)
|
||||
{
|
||||
// When enlarging images, apply a -0.5 offset to avoid
|
||||
// the scaled image being cropped on the top/left side;
|
||||
// when shrinking images, do not apply a -0.5 offset,
|
||||
// otherwise the image is cropped on the bottom/right
|
||||
// side; this applies to all interpolation modes
|
||||
if(rDest.Width > rSource.Width)
|
||||
rSource.X = rSource.X - 0.5f;
|
||||
if(rDest.Height > rSource.Height)
|
||||
rSource.Y = rSource.Y - 0.5f;
|
||||
|
||||
// When shrinking, apply a +0.5 offset, such that the
|
||||
// scaled image is less cropped on the bottom/right side
|
||||
if(rDest.Width < rSource.Width)
|
||||
rSource.X = rSource.X + 0.5f;
|
||||
if(rDest.Height < rSource.Height)
|
||||
rSource.Y = rSource.Y + 0.5f;
|
||||
}
|
||||
|
||||
#if DEBUG
|
||||
public static Image ScaleTest(Image[] vIcons)
|
||||
{
|
||||
Bitmap bmp = new Bitmap(1024, vIcons.Length * (256 + 12),
|
||||
PixelFormat.Format32bppArgb);
|
||||
|
||||
using(Graphics g = Graphics.FromImage(bmp))
|
||||
{
|
||||
g.Clear(Color.White);
|
||||
|
||||
int[] v = new int[] { 16, 24, 32, 48, 64, 128, 256 };
|
||||
|
||||
int x;
|
||||
int y = 8;
|
||||
|
||||
foreach(Image imgIcon in vIcons)
|
||||
{
|
||||
if(imgIcon == null) { Debug.Assert(false); continue; }
|
||||
|
||||
x = 128;
|
||||
|
||||
foreach(int q in v)
|
||||
{
|
||||
using(Image img = ScaleImage(imgIcon, q, q,
|
||||
ScaleTransformFlags.UIIcon))
|
||||
{
|
||||
g.DrawImageUnscaled(img, x, y);
|
||||
}
|
||||
|
||||
x += q + 8;
|
||||
}
|
||||
|
||||
y += v[v.Length - 1] + 8;
|
||||
}
|
||||
}
|
||||
|
||||
return bmp;
|
||||
}
|
||||
#endif // DEBUG
|
||||
#endif // !KeePassLibSD
|
||||
#endif // KeePassUAP
|
||||
|
||||
internal static string ImageToDataUri(Image img)
|
||||
{
|
||||
if(img == null) { Debug.Assert(false); return string.Empty; }
|
||||
|
||||
byte[] pb = null;
|
||||
using(MemoryStream ms = new MemoryStream())
|
||||
{
|
||||
img.Save(ms, ImageFormat.Png);
|
||||
pb = ms.ToArray();
|
||||
}
|
||||
|
||||
return StrUtil.DataToDataUri(pb, "image/png");
|
||||
}
|
||||
}
|
||||
}
|
885
ModernKeePassLib/Utility/MemUtil.cs
Normal file
885
ModernKeePassLib/Utility/MemUtil.cs
Normal file
@@ -0,0 +1,885 @@
|
||||
/*
|
||||
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.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
|
||||
#if KeePassLibSD
|
||||
using KeePassLibSD;
|
||||
#else
|
||||
using System.IO.Compression;
|
||||
#endif
|
||||
|
||||
namespace ModernKeePassLib.Utility
|
||||
{
|
||||
/// <summary>
|
||||
/// Buffer manipulation and conversion routines.
|
||||
/// </summary>
|
||||
public static class MemUtil
|
||||
{
|
||||
public static readonly byte[] EmptyByteArray = new byte[0];
|
||||
|
||||
internal static readonly ArrayHelperEx<char> ArrayHelperExOfChar =
|
||||
new ArrayHelperEx<char>();
|
||||
|
||||
private const MethodImplOptions MioNoOptimize =
|
||||
#if KeePassLibSD
|
||||
MethodImplOptions.NoInlining;
|
||||
#else
|
||||
(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining);
|
||||
#endif
|
||||
|
||||
private static readonly uint[] m_vSBox = new uint[256] {
|
||||
0xCD2FACB3, 0xE78A7F5C, 0x6F0803FC, 0xBCF6E230,
|
||||
0x3A321712, 0x06403DB1, 0xD2F84B95, 0xDF22A6E4,
|
||||
0x07CE9E5B, 0x31788A0C, 0xF683F6F4, 0xEA061F49,
|
||||
0xFA5C2ACA, 0x4B9E494E, 0xB0AB25BA, 0x767731FC,
|
||||
0x261893A7, 0x2B09F2CE, 0x046261E4, 0x41367B4B,
|
||||
0x18A7F225, 0x8F923C0E, 0x5EF3A325, 0x28D0435E,
|
||||
0x84C22919, 0xED66873C, 0x8CEDE444, 0x7FC47C24,
|
||||
0xFCFC6BA3, 0x676F928D, 0xB4147187, 0xD8FB126E,
|
||||
0x7D798D17, 0xFF82E424, 0x1712FA5B, 0xABB09DD5,
|
||||
0x8156BA63, 0x84E4D969, 0xC937FB9A, 0x2F1E5BFC,
|
||||
0x178ECA11, 0x0E71CD5F, 0x52AAC6F4, 0x71EEFC8F,
|
||||
0x7090D749, 0x21CACA31, 0x92996378, 0x0939A8A8,
|
||||
0xE9EE1934, 0xD2718616, 0xF2500543, 0xB911873C,
|
||||
0xD3CB3EEC, 0x2BA0DBEB, 0xB42D0A27, 0xECE67C0F,
|
||||
0x302925F0, 0x6114F839, 0xD39E6307, 0xE28970D6,
|
||||
0xEB982F99, 0x941B4CDF, 0xC540E550, 0x8124FC45,
|
||||
0x98B025C7, 0xE2BF90EA, 0x4F57C976, 0xCF546FE4,
|
||||
0x59566DC8, 0xE3F4360D, 0xF5F9D231, 0xD6180B22,
|
||||
0xB54E088A, 0xB5DFE6A6, 0x3637A36F, 0x056E9284,
|
||||
0xAFF8FBC5, 0x19E01648, 0x8611F043, 0xDAE44337,
|
||||
0xF61B6A1C, 0x257ACD9E, 0xDD35F507, 0xEF05CAFA,
|
||||
0x05EB4A83, 0xFC25CA92, 0x0A4728E6, 0x9CF150EF,
|
||||
0xAEEF67DE, 0xA9472337, 0x57C81EFE, 0x3E5E009F,
|
||||
0x02CB03BB, 0x2BA85674, 0xF21DC251, 0x78C34A34,
|
||||
0xABB1F5BF, 0xB95A2FBD, 0x1FB47777, 0x9A96E8AC,
|
||||
0x5D2D2838, 0x55AAC92A, 0x99EE324E, 0x10F6214B,
|
||||
0x58ABDFB1, 0x2008794D, 0xBEC880F0, 0xE75E5341,
|
||||
0x88015C34, 0x352D8FBF, 0x622B7F6C, 0xF5C59EA2,
|
||||
0x1F759D8E, 0xADE56159, 0xCC7B4C25, 0x5B8BC48C,
|
||||
0xB6BD15AF, 0x3C5B5110, 0xE74A7C3D, 0xEE613161,
|
||||
0x156A1C67, 0x72C06817, 0xEA0A6F69, 0x4CECF993,
|
||||
0xCA9D554C, 0x8E20361F, 0x42D396B9, 0x595DE578,
|
||||
0x749D7955, 0xFD1BA5FD, 0x81FC160E, 0xDB97E28C,
|
||||
0x7CF148F7, 0x0B0B3CF5, 0x534DE605, 0x46421066,
|
||||
0xD4B68DD1, 0x9E479CE6, 0xAE667A9D, 0xBC082082,
|
||||
0xB06DD6EF, 0x20F0F23F, 0xB99E1551, 0xF47A2E3A,
|
||||
0x71DA50C6, 0x67B65779, 0x2A8CB376, 0x1EA71EEE,
|
||||
0x29ABCD50, 0xB6EB0C6B, 0x23C10511, 0x6F3F2144,
|
||||
0x6AF23012, 0xF696BD9E, 0xB94099D8, 0xAD5A9C81,
|
||||
0x7A0794FA, 0x7EDF59D6, 0x1E72E574, 0x8561913C,
|
||||
0x4E4D568F, 0xEECB9928, 0x9C124D2E, 0x0848B82C,
|
||||
0xF1CA395F, 0x9DAF43DC, 0xF77EC323, 0x394E9B59,
|
||||
0x7E200946, 0x8B811D68, 0x16DA3305, 0xAB8DE2C3,
|
||||
0xE6C53B64, 0x98C2D321, 0x88A97D81, 0xA7106419,
|
||||
0x8E52F7BF, 0x8ED262AF, 0x7CCA974E, 0xF0933241,
|
||||
0x040DD437, 0xE143B3D4, 0x3019F56F, 0xB741521D,
|
||||
0xF1745362, 0x4C435F9F, 0xB4214D0D, 0x0B0C348B,
|
||||
0x5051D189, 0x4C30447E, 0x7393D722, 0x95CEDD0B,
|
||||
0xDD994E80, 0xC3D22ED9, 0x739CD900, 0x131EB9C4,
|
||||
0xEF1062B2, 0x4F0DE436, 0x52920073, 0x9A7F3D80,
|
||||
0x896E7B1B, 0x2C8BBE5A, 0xBD304F8A, 0xA993E22C,
|
||||
0x134C41A0, 0xFA989E00, 0x39CE9726, 0xFB89FCCF,
|
||||
0xE8FBAC97, 0xD4063FFC, 0x935A2B5A, 0x44C8EE83,
|
||||
0xCB2BC7B6, 0x02989E92, 0x75478BEA, 0x144378D0,
|
||||
0xD853C087, 0x8897A34E, 0xDD23629D, 0xBDE2A2A2,
|
||||
0x581D8ECC, 0x5DA8AEE8, 0xFF8AAFD0, 0xBA2BCF6E,
|
||||
0x4BD98DAC, 0xF2EDB9E4, 0xFA2DC868, 0x47E84661,
|
||||
0xECEB1C7D, 0x41705CA4, 0x5982E4D4, 0xEB5204A1,
|
||||
0xD196CAFB, 0x6414804D, 0x3ABD4B46, 0x8B494C26,
|
||||
0xB432D52B, 0x39C5356B, 0x6EC80BF7, 0x71BE5483,
|
||||
0xCEC4A509, 0xE9411D61, 0x52F341E5, 0xD2E6197B,
|
||||
0x4F02826C, 0xA9E48838, 0xD1F8F247, 0xE4957FB3,
|
||||
0x586CCA99, 0x9A8B6A5B, 0x4998FBEA, 0xF762BE4C,
|
||||
0x90DFE33C, 0x9731511E, 0x88C6A82F, 0xDD65A4D4
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Convert a hexadecimal string to a byte array. The input string must be
|
||||
/// even (i.e. its length is a multiple of 2).
|
||||
/// </summary>
|
||||
/// <param name="strHex">String containing hexadecimal characters.</param>
|
||||
/// <returns>Returns a byte array. Returns <c>null</c> if the string parameter
|
||||
/// was <c>null</c> or is an uneven string (i.e. if its length isn't a
|
||||
/// multiple of 2).</returns>
|
||||
/// <exception cref="System.ArgumentNullException">Thrown if <paramref name="strHex" />
|
||||
/// is <c>null</c>.</exception>
|
||||
public static byte[] HexStringToByteArray(string strHex)
|
||||
{
|
||||
if(strHex == null) { Debug.Assert(false); throw new ArgumentNullException("strHex"); }
|
||||
|
||||
int nStrLen = strHex.Length;
|
||||
if((nStrLen & 1) != 0) { Debug.Assert(false); return null; }
|
||||
|
||||
byte[] pb = new byte[nStrLen / 2];
|
||||
byte bt;
|
||||
char ch;
|
||||
|
||||
for(int i = 0; i < nStrLen; i += 2)
|
||||
{
|
||||
ch = strHex[i];
|
||||
|
||||
if((ch >= '0') && (ch <= '9'))
|
||||
bt = (byte)(ch - '0');
|
||||
else if((ch >= 'a') && (ch <= 'f'))
|
||||
bt = (byte)(ch - 'a' + 10);
|
||||
else if((ch >= 'A') && (ch <= 'F'))
|
||||
bt = (byte)(ch - 'A' + 10);
|
||||
else { Debug.Assert(false); bt = 0; }
|
||||
|
||||
bt <<= 4;
|
||||
|
||||
ch = strHex[i + 1];
|
||||
if((ch >= '0') && (ch <= '9'))
|
||||
bt += (byte)(ch - '0');
|
||||
else if((ch >= 'a') && (ch <= 'f'))
|
||||
bt += (byte)(ch - 'a' + 10);
|
||||
else if((ch >= 'A') && (ch <= 'F'))
|
||||
bt += (byte)(ch - 'A' + 10);
|
||||
else { Debug.Assert(false); }
|
||||
|
||||
pb[i >> 1] = bt;
|
||||
}
|
||||
|
||||
return pb;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert a byte array to a hexadecimal string.
|
||||
/// </summary>
|
||||
/// <param name="pbArray">Input byte array.</param>
|
||||
/// <returns>Returns the hexadecimal string representing the byte
|
||||
/// array. Returns <c>null</c>, if the input byte array was <c>null</c>. Returns
|
||||
/// an empty string, if the input byte array has length 0.</returns>
|
||||
public static string ByteArrayToHexString(byte[] pbArray)
|
||||
{
|
||||
if(pbArray == null) return null;
|
||||
|
||||
int nLen = pbArray.Length;
|
||||
if(nLen == 0) return string.Empty;
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
byte bt, btHigh, btLow;
|
||||
for(int i = 0; i < nLen; ++i)
|
||||
{
|
||||
bt = pbArray[i];
|
||||
btHigh = bt; btHigh >>= 4;
|
||||
btLow = (byte)(bt & 0x0F);
|
||||
|
||||
if(btHigh >= 10) sb.Append((char)('A' + btHigh - 10));
|
||||
else sb.Append((char)('0' + btHigh));
|
||||
|
||||
if(btLow >= 10) sb.Append((char)('A' + btLow - 10));
|
||||
else sb.Append((char)('0' + btLow));
|
||||
}
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decode Base32 strings according to RFC 4648.
|
||||
/// </summary>
|
||||
public static byte[] ParseBase32(string str)
|
||||
{
|
||||
if((str == null) || ((str.Length % 8) != 0))
|
||||
{
|
||||
Debug.Assert(false);
|
||||
return null;
|
||||
}
|
||||
|
||||
ulong uMaxBits = (ulong)str.Length * 5UL;
|
||||
List<byte> l = new List<byte>((int)(uMaxBits / 8UL) + 1);
|
||||
Debug.Assert(l.Count == 0);
|
||||
|
||||
for(int i = 0; i < str.Length; i += 8)
|
||||
{
|
||||
ulong u = 0;
|
||||
int nBits = 0;
|
||||
|
||||
for(int j = 0; j < 8; ++j)
|
||||
{
|
||||
char ch = str[i + j];
|
||||
if(ch == '=') break;
|
||||
|
||||
ulong uValue;
|
||||
if((ch >= 'A') && (ch <= 'Z'))
|
||||
uValue = (ulong)(ch - 'A');
|
||||
else if((ch >= 'a') && (ch <= 'z'))
|
||||
uValue = (ulong)(ch - 'a');
|
||||
else if((ch >= '2') && (ch <= '7'))
|
||||
uValue = (ulong)(ch - '2') + 26UL;
|
||||
else { Debug.Assert(false); return null; }
|
||||
|
||||
u <<= 5;
|
||||
u += uValue;
|
||||
nBits += 5;
|
||||
}
|
||||
|
||||
int nBitsTooMany = (nBits % 8);
|
||||
u >>= nBitsTooMany;
|
||||
nBits -= nBitsTooMany;
|
||||
Debug.Assert((nBits % 8) == 0);
|
||||
|
||||
int idxNewBytes = l.Count;
|
||||
while(nBits > 0)
|
||||
{
|
||||
l.Add((byte)(u & 0xFF));
|
||||
u >>= 8;
|
||||
nBits -= 8;
|
||||
}
|
||||
l.Reverse(idxNewBytes, l.Count - idxNewBytes);
|
||||
}
|
||||
|
||||
return l.ToArray();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set all bytes in a byte array to zero.
|
||||
/// </summary>
|
||||
/// <param name="pbArray">Input array. All bytes of this array
|
||||
/// will be set to zero.</param>
|
||||
[MethodImpl(MioNoOptimize)]
|
||||
public static void ZeroByteArray(byte[] pbArray)
|
||||
{
|
||||
Debug.Assert(pbArray != null);
|
||||
if(pbArray == null) throw new ArgumentNullException("pbArray");
|
||||
|
||||
Array.Clear(pbArray, 0, pbArray.Length);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set all elements of an array to the default value.
|
||||
/// </summary>
|
||||
/// <param name="v">Input array.</param>
|
||||
[MethodImpl(MioNoOptimize)]
|
||||
public static void ZeroArray<T>(T[] v)
|
||||
{
|
||||
if(v == null) { Debug.Assert(false); throw new ArgumentNullException("v"); }
|
||||
|
||||
Array.Clear(v, 0, v.Length);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert 2 bytes to a 16-bit unsigned integer (little-endian).
|
||||
/// </summary>
|
||||
public static ushort BytesToUInt16(byte[] pb)
|
||||
{
|
||||
Debug.Assert((pb != null) && (pb.Length == 2));
|
||||
if(pb == null) throw new ArgumentNullException("pb");
|
||||
if(pb.Length != 2) throw new ArgumentOutOfRangeException("pb");
|
||||
|
||||
return (ushort)((ushort)pb[0] | ((ushort)pb[1] << 8));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert 2 bytes to a 16-bit unsigned integer (little-endian).
|
||||
/// </summary>
|
||||
public static ushort BytesToUInt16(byte[] pb, int iOffset)
|
||||
{
|
||||
if(pb == null) { Debug.Assert(false); throw new ArgumentNullException("pb"); }
|
||||
if((iOffset < 0) || ((iOffset + 1) >= pb.Length))
|
||||
{
|
||||
Debug.Assert(false);
|
||||
throw new ArgumentOutOfRangeException("iOffset");
|
||||
}
|
||||
|
||||
return (ushort)((ushort)pb[iOffset] | ((ushort)pb[iOffset + 1] << 8));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert 4 bytes to a 32-bit unsigned integer (little-endian).
|
||||
/// </summary>
|
||||
public static uint BytesToUInt32(byte[] pb)
|
||||
{
|
||||
Debug.Assert((pb != null) && (pb.Length == 4));
|
||||
if(pb == null) throw new ArgumentNullException("pb");
|
||||
if(pb.Length != 4) throw new ArgumentOutOfRangeException("pb");
|
||||
|
||||
return ((uint)pb[0] | ((uint)pb[1] << 8) | ((uint)pb[2] << 16) |
|
||||
((uint)pb[3] << 24));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert 4 bytes to a 32-bit unsigned integer (little-endian).
|
||||
/// </summary>
|
||||
public static uint BytesToUInt32(byte[] pb, int iOffset)
|
||||
{
|
||||
if(pb == null) { Debug.Assert(false); throw new ArgumentNullException("pb"); }
|
||||
if((iOffset < 0) || ((iOffset + 3) >= pb.Length))
|
||||
{
|
||||
Debug.Assert(false);
|
||||
throw new ArgumentOutOfRangeException("iOffset");
|
||||
}
|
||||
|
||||
return ((uint)pb[iOffset] | ((uint)pb[iOffset + 1] << 8) |
|
||||
((uint)pb[iOffset + 2] << 16) | ((uint)pb[iOffset + 3] << 24));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert 8 bytes to a 64-bit unsigned integer (little-endian).
|
||||
/// </summary>
|
||||
public static ulong BytesToUInt64(byte[] pb)
|
||||
{
|
||||
Debug.Assert((pb != null) && (pb.Length == 8));
|
||||
if(pb == null) throw new ArgumentNullException("pb");
|
||||
if(pb.Length != 8) throw new ArgumentOutOfRangeException("pb");
|
||||
|
||||
return ((ulong)pb[0] | ((ulong)pb[1] << 8) | ((ulong)pb[2] << 16) |
|
||||
((ulong)pb[3] << 24) | ((ulong)pb[4] << 32) | ((ulong)pb[5] << 40) |
|
||||
((ulong)pb[6] << 48) | ((ulong)pb[7] << 56));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert 8 bytes to a 64-bit unsigned integer (little-endian).
|
||||
/// </summary>
|
||||
public static ulong BytesToUInt64(byte[] pb, int iOffset)
|
||||
{
|
||||
if(pb == null) { Debug.Assert(false); throw new ArgumentNullException("pb"); }
|
||||
if((iOffset < 0) || ((iOffset + 7) >= pb.Length))
|
||||
{
|
||||
Debug.Assert(false);
|
||||
throw new ArgumentOutOfRangeException("iOffset");
|
||||
}
|
||||
|
||||
// if(BitConverter.IsLittleEndian)
|
||||
// return BitConverter.ToUInt64(pb, iOffset);
|
||||
|
||||
return ((ulong)pb[iOffset] | ((ulong)pb[iOffset + 1] << 8) |
|
||||
((ulong)pb[iOffset + 2] << 16) | ((ulong)pb[iOffset + 3] << 24) |
|
||||
((ulong)pb[iOffset + 4] << 32) | ((ulong)pb[iOffset + 5] << 40) |
|
||||
((ulong)pb[iOffset + 6] << 48) | ((ulong)pb[iOffset + 7] << 56));
|
||||
}
|
||||
|
||||
public static int BytesToInt32(byte[] pb)
|
||||
{
|
||||
return (int)BytesToUInt32(pb);
|
||||
}
|
||||
|
||||
public static int BytesToInt32(byte[] pb, int iOffset)
|
||||
{
|
||||
return (int)BytesToUInt32(pb, iOffset);
|
||||
}
|
||||
|
||||
public static long BytesToInt64(byte[] pb)
|
||||
{
|
||||
return (long)BytesToUInt64(pb);
|
||||
}
|
||||
|
||||
public static long BytesToInt64(byte[] pb, int iOffset)
|
||||
{
|
||||
return (long)BytesToUInt64(pb, iOffset);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert a 16-bit unsigned integer to 2 bytes (little-endian).
|
||||
/// </summary>
|
||||
public static byte[] UInt16ToBytes(ushort uValue)
|
||||
{
|
||||
byte[] pb = new byte[2];
|
||||
|
||||
unchecked
|
||||
{
|
||||
pb[0] = (byte)uValue;
|
||||
pb[1] = (byte)(uValue >> 8);
|
||||
}
|
||||
|
||||
return pb;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert a 32-bit unsigned integer to 4 bytes (little-endian).
|
||||
/// </summary>
|
||||
public static byte[] UInt32ToBytes(uint uValue)
|
||||
{
|
||||
byte[] pb = new byte[4];
|
||||
|
||||
unchecked
|
||||
{
|
||||
pb[0] = (byte)uValue;
|
||||
pb[1] = (byte)(uValue >> 8);
|
||||
pb[2] = (byte)(uValue >> 16);
|
||||
pb[3] = (byte)(uValue >> 24);
|
||||
}
|
||||
|
||||
return pb;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert a 32-bit unsigned integer to 4 bytes (little-endian).
|
||||
/// </summary>
|
||||
public static void UInt32ToBytesEx(uint uValue, byte[] pb, int iOffset)
|
||||
{
|
||||
if(pb == null) { Debug.Assert(false); throw new ArgumentNullException("pb"); }
|
||||
if((iOffset < 0) || ((iOffset + 3) >= pb.Length))
|
||||
{
|
||||
Debug.Assert(false);
|
||||
throw new ArgumentOutOfRangeException("iOffset");
|
||||
}
|
||||
|
||||
unchecked
|
||||
{
|
||||
pb[iOffset] = (byte)uValue;
|
||||
pb[iOffset + 1] = (byte)(uValue >> 8);
|
||||
pb[iOffset + 2] = (byte)(uValue >> 16);
|
||||
pb[iOffset + 3] = (byte)(uValue >> 24);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert a 64-bit unsigned integer to 8 bytes (little-endian).
|
||||
/// </summary>
|
||||
public static byte[] UInt64ToBytes(ulong uValue)
|
||||
{
|
||||
byte[] pb = new byte[8];
|
||||
|
||||
unchecked
|
||||
{
|
||||
pb[0] = (byte)uValue;
|
||||
pb[1] = (byte)(uValue >> 8);
|
||||
pb[2] = (byte)(uValue >> 16);
|
||||
pb[3] = (byte)(uValue >> 24);
|
||||
pb[4] = (byte)(uValue >> 32);
|
||||
pb[5] = (byte)(uValue >> 40);
|
||||
pb[6] = (byte)(uValue >> 48);
|
||||
pb[7] = (byte)(uValue >> 56);
|
||||
}
|
||||
|
||||
return pb;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert a 64-bit unsigned integer to 8 bytes (little-endian).
|
||||
/// </summary>
|
||||
public static void UInt64ToBytesEx(ulong uValue, byte[] pb, int iOffset)
|
||||
{
|
||||
if(pb == null) { Debug.Assert(false); throw new ArgumentNullException("pb"); }
|
||||
if((iOffset < 0) || ((iOffset + 7) >= pb.Length))
|
||||
{
|
||||
Debug.Assert(false);
|
||||
throw new ArgumentOutOfRangeException("iOffset");
|
||||
}
|
||||
|
||||
unchecked
|
||||
{
|
||||
pb[iOffset] = (byte)uValue;
|
||||
pb[iOffset + 1] = (byte)(uValue >> 8);
|
||||
pb[iOffset + 2] = (byte)(uValue >> 16);
|
||||
pb[iOffset + 3] = (byte)(uValue >> 24);
|
||||
pb[iOffset + 4] = (byte)(uValue >> 32);
|
||||
pb[iOffset + 5] = (byte)(uValue >> 40);
|
||||
pb[iOffset + 6] = (byte)(uValue >> 48);
|
||||
pb[iOffset + 7] = (byte)(uValue >> 56);
|
||||
}
|
||||
}
|
||||
|
||||
public static byte[] Int32ToBytes(int iValue)
|
||||
{
|
||||
return UInt32ToBytes((uint)iValue);
|
||||
}
|
||||
|
||||
public static void Int32ToBytesEx(int iValue, byte[] pb, int iOffset)
|
||||
{
|
||||
UInt32ToBytesEx((uint)iValue, pb, iOffset);
|
||||
}
|
||||
|
||||
public static byte[] Int64ToBytes(long lValue)
|
||||
{
|
||||
return UInt64ToBytes((ulong)lValue);
|
||||
}
|
||||
|
||||
public static void Int64ToBytesEx(long lValue, byte[] pb, int iOffset)
|
||||
{
|
||||
UInt64ToBytesEx((ulong)lValue, pb, iOffset);
|
||||
}
|
||||
|
||||
public static uint RotateLeft32(uint u, int nBits)
|
||||
{
|
||||
return ((u << nBits) | (u >> (32 - nBits)));
|
||||
}
|
||||
|
||||
public static uint RotateRight32(uint u, int nBits)
|
||||
{
|
||||
return ((u >> nBits) | (u << (32 - nBits)));
|
||||
}
|
||||
|
||||
public static ulong RotateLeft64(ulong u, int nBits)
|
||||
{
|
||||
return ((u << nBits) | (u >> (64 - nBits)));
|
||||
}
|
||||
|
||||
public static ulong RotateRight64(ulong u, int nBits)
|
||||
{
|
||||
return ((u >> nBits) | (u << (64 - nBits)));
|
||||
}
|
||||
|
||||
public static bool ArraysEqual(byte[] x, byte[] y)
|
||||
{
|
||||
// Return false if one of them is null (not comparable)!
|
||||
if((x == null) || (y == null)) { Debug.Assert(false); return false; }
|
||||
|
||||
if(x.Length != y.Length) return false;
|
||||
|
||||
for(int i = 0; i < x.Length; ++i)
|
||||
{
|
||||
if(x[i] != y[i]) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static void XorArray(byte[] pbSource, int iSourceOffset,
|
||||
byte[] pbBuffer, int iBufferOffset, int cb)
|
||||
{
|
||||
if(pbSource == null) throw new ArgumentNullException("pbSource");
|
||||
if(iSourceOffset < 0) throw new ArgumentOutOfRangeException("iSourceOffset");
|
||||
if(pbBuffer == null) throw new ArgumentNullException("pbBuffer");
|
||||
if(iBufferOffset < 0) throw new ArgumentOutOfRangeException("iBufferOffset");
|
||||
if(cb < 0) throw new ArgumentOutOfRangeException("cb");
|
||||
if(iSourceOffset > (pbSource.Length - cb))
|
||||
throw new ArgumentOutOfRangeException("cb");
|
||||
if(iBufferOffset > (pbBuffer.Length - cb))
|
||||
throw new ArgumentOutOfRangeException("cb");
|
||||
|
||||
for(int i = 0; i < cb; ++i)
|
||||
pbBuffer[iBufferOffset + i] ^= pbSource[iSourceOffset + i];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fast hash that can be used e.g. for hash tables.
|
||||
/// The algorithm might change in the future; do not store
|
||||
/// the hashes for later use.
|
||||
/// </summary>
|
||||
public static uint Hash32(byte[] v, int iStart, int iLength)
|
||||
{
|
||||
uint u = 0x326F637B;
|
||||
|
||||
if(v == null) { Debug.Assert(false); return u; }
|
||||
if(iStart < 0) { Debug.Assert(false); return u; }
|
||||
if(iLength < 0) { Debug.Assert(false); return u; }
|
||||
|
||||
int m = iStart + iLength;
|
||||
if(m > v.Length) { Debug.Assert(false); return u; }
|
||||
|
||||
for(int i = iStart; i < m; ++i)
|
||||
{
|
||||
u ^= m_vSBox[v[i]];
|
||||
u *= 3;
|
||||
}
|
||||
|
||||
return u;
|
||||
}
|
||||
|
||||
public static void CopyStream(Stream sSource, Stream sTarget)
|
||||
{
|
||||
Debug.Assert((sSource != null) && (sTarget != null));
|
||||
if(sSource == null) throw new ArgumentNullException("sSource");
|
||||
if(sTarget == null) throw new ArgumentNullException("sTarget");
|
||||
|
||||
const int nBufSize = 4096;
|
||||
byte[] pbBuf = new byte[nBufSize];
|
||||
|
||||
while(true)
|
||||
{
|
||||
int nRead = sSource.Read(pbBuf, 0, nBufSize);
|
||||
if(nRead == 0) break;
|
||||
|
||||
sTarget.Write(pbBuf, 0, nRead);
|
||||
}
|
||||
|
||||
// Do not close any of the streams
|
||||
}
|
||||
|
||||
public static byte[] Read(Stream s, int nCount)
|
||||
{
|
||||
if(s == null) throw new ArgumentNullException("s");
|
||||
if(nCount < 0) throw new ArgumentOutOfRangeException("nCount");
|
||||
|
||||
byte[] pb = new byte[nCount];
|
||||
int iOffset = 0;
|
||||
while(nCount > 0)
|
||||
{
|
||||
int iRead = s.Read(pb, iOffset, nCount);
|
||||
if(iRead == 0) break;
|
||||
|
||||
iOffset += iRead;
|
||||
nCount -= iRead;
|
||||
}
|
||||
|
||||
if(iOffset != pb.Length)
|
||||
{
|
||||
byte[] pbPart = new byte[iOffset];
|
||||
Array.Copy(pb, pbPart, iOffset);
|
||||
return pbPart;
|
||||
}
|
||||
|
||||
return pb;
|
||||
}
|
||||
|
||||
public static void Write(Stream s, byte[] pbData)
|
||||
{
|
||||
if(s == null) { Debug.Assert(false); return; }
|
||||
if(pbData == null) { Debug.Assert(false); return; }
|
||||
|
||||
Debug.Assert(pbData.Length >= 0);
|
||||
if(pbData.Length > 0) s.Write(pbData, 0, pbData.Length);
|
||||
}
|
||||
|
||||
public static byte[] Compress(byte[] pbData)
|
||||
{
|
||||
if(pbData == null) throw new ArgumentNullException("pbData");
|
||||
if(pbData.Length == 0) return pbData;
|
||||
|
||||
byte[] pbCompressed;
|
||||
using(MemoryStream msSource = new MemoryStream(pbData, false))
|
||||
{
|
||||
using(MemoryStream msCompressed = new MemoryStream())
|
||||
{
|
||||
using(GZipStream gz = new GZipStream(msCompressed,
|
||||
CompressionMode.Compress))
|
||||
{
|
||||
MemUtil.CopyStream(msSource, gz);
|
||||
}
|
||||
|
||||
pbCompressed = msCompressed.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
return pbCompressed;
|
||||
}
|
||||
|
||||
public static byte[] Decompress(byte[] pbCompressed)
|
||||
{
|
||||
if(pbCompressed == null) throw new ArgumentNullException("pbCompressed");
|
||||
if(pbCompressed.Length == 0) return pbCompressed;
|
||||
|
||||
byte[] pbData;
|
||||
using(MemoryStream msData = new MemoryStream())
|
||||
{
|
||||
using(MemoryStream msCompressed = new MemoryStream(pbCompressed, false))
|
||||
{
|
||||
using(GZipStream gz = new GZipStream(msCompressed,
|
||||
CompressionMode.Decompress))
|
||||
{
|
||||
MemUtil.CopyStream(gz, msData);
|
||||
}
|
||||
}
|
||||
|
||||
pbData = msData.ToArray();
|
||||
}
|
||||
|
||||
return pbData;
|
||||
}
|
||||
|
||||
public static int IndexOf<T>(T[] vHaystack, T[] vNeedle)
|
||||
where T : IEquatable<T>
|
||||
{
|
||||
if(vHaystack == null) throw new ArgumentNullException("vHaystack");
|
||||
if(vNeedle == null) throw new ArgumentNullException("vNeedle");
|
||||
if(vNeedle.Length == 0) return 0;
|
||||
|
||||
for(int i = 0; i <= (vHaystack.Length - vNeedle.Length); ++i)
|
||||
{
|
||||
bool bFound = true;
|
||||
for(int m = 0; m < vNeedle.Length; ++m)
|
||||
{
|
||||
if(!vHaystack[i + m].Equals(vNeedle[m]))
|
||||
{
|
||||
bFound = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(bFound) return i;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
public static T[] Mid<T>(T[] v, int iOffset, int iLength)
|
||||
{
|
||||
if(v == null) throw new ArgumentNullException("v");
|
||||
if(iOffset < 0) throw new ArgumentOutOfRangeException("iOffset");
|
||||
if(iLength < 0) throw new ArgumentOutOfRangeException("iLength");
|
||||
if((iOffset + iLength) > v.Length) throw new ArgumentException();
|
||||
|
||||
T[] r = new T[iLength];
|
||||
Array.Copy(v, iOffset, r, 0, iLength);
|
||||
return r;
|
||||
}
|
||||
|
||||
public static IEnumerable<T> Union<T>(IEnumerable<T> a, IEnumerable<T> b,
|
||||
IEqualityComparer<T> cmp)
|
||||
{
|
||||
if(a == null) throw new ArgumentNullException("a");
|
||||
if(b == null) throw new ArgumentNullException("b");
|
||||
|
||||
Dictionary<T, bool> d = ((cmp != null) ?
|
||||
(new Dictionary<T, bool>(cmp)) : (new Dictionary<T, bool>()));
|
||||
|
||||
foreach(T ta in a)
|
||||
{
|
||||
if(d.ContainsKey(ta)) continue; // Prevent duplicates
|
||||
|
||||
d[ta] = true;
|
||||
yield return ta;
|
||||
}
|
||||
|
||||
foreach(T tb in b)
|
||||
{
|
||||
if(d.ContainsKey(tb)) continue; // Prevent duplicates
|
||||
|
||||
d[tb] = true;
|
||||
yield return tb;
|
||||
}
|
||||
|
||||
yield break;
|
||||
}
|
||||
|
||||
public static IEnumerable<T> Intersect<T>(IEnumerable<T> a, IEnumerable<T> b,
|
||||
IEqualityComparer<T> cmp)
|
||||
{
|
||||
if(a == null) throw new ArgumentNullException("a");
|
||||
if(b == null) throw new ArgumentNullException("b");
|
||||
|
||||
Dictionary<T, bool> d = ((cmp != null) ?
|
||||
(new Dictionary<T, bool>(cmp)) : (new Dictionary<T, bool>()));
|
||||
|
||||
foreach(T tb in b) { d[tb] = true; }
|
||||
|
||||
foreach(T ta in a)
|
||||
{
|
||||
if(d.Remove(ta)) // Prevent duplicates
|
||||
yield return ta;
|
||||
}
|
||||
|
||||
yield break;
|
||||
}
|
||||
|
||||
public static IEnumerable<T> Except<T>(IEnumerable<T> a, IEnumerable<T> b,
|
||||
IEqualityComparer<T> cmp)
|
||||
{
|
||||
if(a == null) throw new ArgumentNullException("a");
|
||||
if(b == null) throw new ArgumentNullException("b");
|
||||
|
||||
Dictionary<T, bool> d = ((cmp != null) ?
|
||||
(new Dictionary<T, bool>(cmp)) : (new Dictionary<T, bool>()));
|
||||
|
||||
foreach(T tb in b) { d[tb] = true; }
|
||||
|
||||
foreach(T ta in a)
|
||||
{
|
||||
if(d.ContainsKey(ta)) continue;
|
||||
|
||||
d[ta] = true; // Prevent duplicates
|
||||
yield return ta;
|
||||
}
|
||||
|
||||
yield break;
|
||||
}
|
||||
|
||||
[MethodImpl(MioNoOptimize)]
|
||||
internal static void DisposeIfPossible(object o)
|
||||
{
|
||||
if(o == null) { Debug.Assert(false); return; }
|
||||
|
||||
IDisposable d = (o as IDisposable);
|
||||
if(d != null) d.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class ArrayHelperEx<T> : IEqualityComparer<T[]>, IComparer<T[]>
|
||||
where T : IEquatable<T>, IComparable<T>
|
||||
{
|
||||
public int GetHashCode(T[] obj)
|
||||
{
|
||||
if(obj == null) throw new ArgumentNullException("obj");
|
||||
|
||||
uint h = 0xC17962B7U;
|
||||
unchecked
|
||||
{
|
||||
int n = obj.Length;
|
||||
for(int i = 0; i < n; ++i)
|
||||
{
|
||||
h += (uint)obj[i].GetHashCode();
|
||||
h = MemUtil.RotateLeft32(h * 0x5FC34C67U, 13);
|
||||
}
|
||||
}
|
||||
|
||||
return (int)h;
|
||||
}
|
||||
|
||||
/* internal ulong GetHashCodeEx(T[] obj)
|
||||
{
|
||||
if(obj == null) throw new ArgumentNullException("obj");
|
||||
|
||||
ulong h = 0x207CAC8E509A3FC9UL;
|
||||
unchecked
|
||||
{
|
||||
int n = obj.Length;
|
||||
for(int i = 0; i < n; ++i)
|
||||
{
|
||||
h += (uint)obj[i].GetHashCode();
|
||||
h = MemUtil.RotateLeft64(h * 0x54724D3EA2860CBBUL, 29);
|
||||
}
|
||||
}
|
||||
|
||||
return h;
|
||||
} */
|
||||
|
||||
public bool Equals(T[] x, T[] y)
|
||||
{
|
||||
if(object.ReferenceEquals(x, y)) return true;
|
||||
if((x == null) || (y == null)) return false;
|
||||
|
||||
int n = x.Length;
|
||||
if(n != y.Length) return false;
|
||||
|
||||
for(int i = 0; i < n; ++i)
|
||||
{
|
||||
if(!x[i].Equals(y[i])) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public int Compare(T[] x, T[] y)
|
||||
{
|
||||
if(object.ReferenceEquals(x, y)) return 0;
|
||||
if(x == null) return -1;
|
||||
if(y == null) return 1;
|
||||
|
||||
int n = x.Length, m = y.Length;
|
||||
if(n != m) return ((n < m) ? -1 : 1);
|
||||
|
||||
for(int i = 0; i < n; ++i)
|
||||
{
|
||||
T tX = x[i], tY = y[i];
|
||||
if(!tX.Equals(tY)) return tX.CompareTo(tY);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
459
ModernKeePassLib/Utility/MessageService.cs
Normal file
459
ModernKeePassLib/Utility/MessageService.cs
Normal file
@@ -0,0 +1,459 @@
|
||||
/*
|
||||
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.Collections.Specialized;
|
||||
using System.Diagnostics;
|
||||
using System.Text;
|
||||
|
||||
#if !ModernKeePassLib
|
||||
using System.Windows.Forms;
|
||||
#endif
|
||||
|
||||
using ModernKeePassLib.Resources;
|
||||
using ModernKeePassLib.Serialization;
|
||||
|
||||
namespace ModernKeePassLib.Utility
|
||||
{
|
||||
public sealed class MessageServiceEventArgs : EventArgs
|
||||
{
|
||||
#if !ModernKeePassLib
|
||||
private string m_strTitle = string.Empty;
|
||||
private string m_strText = string.Empty;
|
||||
private MessageBoxButtons m_msgButtons = MessageBoxButtons.OK;
|
||||
private MessageBoxIcon m_msgIcon = MessageBoxIcon.None;
|
||||
|
||||
public string Title { get { return m_strTitle; } }
|
||||
public string Text { get { return m_strText; } }
|
||||
public MessageBoxButtons Buttons { get { return m_msgButtons; } }
|
||||
public MessageBoxIcon Icon { get { return m_msgIcon; } }
|
||||
|
||||
public MessageServiceEventArgs() { }
|
||||
|
||||
public MessageServiceEventArgs(string strTitle, string strText,
|
||||
MessageBoxButtons msgButtons, MessageBoxIcon msgIcon)
|
||||
{
|
||||
m_strTitle = (strTitle ?? string.Empty);
|
||||
m_strText = (strText ?? string.Empty);
|
||||
m_msgButtons = msgButtons;
|
||||
m_msgIcon = msgIcon;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
public static class MessageService
|
||||
{
|
||||
private static volatile uint m_uCurrentMessageCount = 0;
|
||||
|
||||
#if !ModernKeePassLib
|
||||
#if !KeePassLibSD
|
||||
private const MessageBoxIcon m_mbiInfo = MessageBoxIcon.Information;
|
||||
private const MessageBoxIcon m_mbiWarning = MessageBoxIcon.Warning;
|
||||
private const MessageBoxIcon m_mbiFatal = MessageBoxIcon.Error;
|
||||
|
||||
private const MessageBoxOptions m_mboRtl = (MessageBoxOptions.RtlReading |
|
||||
MessageBoxOptions.RightAlign);
|
||||
#else
|
||||
private const MessageBoxIcon m_mbiInfo = MessageBoxIcon.Asterisk;
|
||||
private const MessageBoxIcon m_mbiWarning = MessageBoxIcon.Exclamation;
|
||||
private const MessageBoxIcon m_mbiFatal = MessageBoxIcon.Hand;
|
||||
#endif
|
||||
private const MessageBoxIcon m_mbiQuestion = MessageBoxIcon.Question;
|
||||
#endif
|
||||
public static string NewLine
|
||||
{
|
||||
#if !KeePassLibSD
|
||||
get { return Environment.NewLine; }
|
||||
#else
|
||||
get { return "\r\n"; }
|
||||
#endif
|
||||
}
|
||||
|
||||
public static string NewParagraph
|
||||
{
|
||||
#if !KeePassLibSD
|
||||
get { return Environment.NewLine + Environment.NewLine; }
|
||||
#else
|
||||
get { return "\r\n\r\n"; }
|
||||
#endif
|
||||
}
|
||||
|
||||
public static uint CurrentMessageCount
|
||||
{
|
||||
get { return m_uCurrentMessageCount; }
|
||||
}
|
||||
|
||||
#if !KeePassUAP
|
||||
public static event EventHandler<MessageServiceEventArgs> MessageShowing;
|
||||
#endif
|
||||
|
||||
private static string ObjectsToMessage(object[] vLines)
|
||||
{
|
||||
return ObjectsToMessage(vLines, false);
|
||||
}
|
||||
|
||||
private static string ObjectsToMessage(object[] vLines, bool bFullExceptions)
|
||||
{
|
||||
if(vLines == null) return string.Empty;
|
||||
|
||||
string strNewPara = MessageService.NewParagraph;
|
||||
|
||||
StringBuilder sbText = new StringBuilder();
|
||||
bool bSeparator = false;
|
||||
|
||||
foreach(object obj in vLines)
|
||||
{
|
||||
if(obj == null) continue;
|
||||
|
||||
string strAppend = null;
|
||||
|
||||
Exception exObj = (obj as Exception);
|
||||
string strObj = (obj as string);
|
||||
#if (!KeePassLibSD && !ModernKeePassLib)
|
||||
StringCollection scObj = (obj as StringCollection);
|
||||
#endif
|
||||
|
||||
if(exObj != null)
|
||||
{
|
||||
if(bFullExceptions)
|
||||
strAppend = StrUtil.FormatException(exObj);
|
||||
else if(!string.IsNullOrEmpty(exObj.Message))
|
||||
strAppend = exObj.Message;
|
||||
}
|
||||
#if (!KeePassLibSD && !ModernKeePassLib)
|
||||
else if(scObj != null)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
foreach(string strCollLine in scObj)
|
||||
{
|
||||
if(sb.Length > 0) sb.AppendLine();
|
||||
sb.Append(strCollLine.TrimEnd());
|
||||
}
|
||||
strAppend = sb.ToString();
|
||||
}
|
||||
#endif
|
||||
else if(strObj != null)
|
||||
strAppend = strObj;
|
||||
else
|
||||
strAppend = obj.ToString();
|
||||
|
||||
if(!string.IsNullOrEmpty(strAppend))
|
||||
{
|
||||
if(bSeparator) sbText.Append(strNewPara);
|
||||
else bSeparator = true;
|
||||
|
||||
sbText.Append(strAppend);
|
||||
}
|
||||
}
|
||||
|
||||
return sbText.ToString();
|
||||
}
|
||||
|
||||
#if (!KeePassLibSD && !ModernKeePassLib)
|
||||
internal static Form GetTopForm()
|
||||
{
|
||||
FormCollection fc = Application.OpenForms;
|
||||
if((fc == null) || (fc.Count == 0)) return null;
|
||||
|
||||
return fc[fc.Count - 1];
|
||||
}
|
||||
#endif
|
||||
|
||||
#if !ModernKeePassLib
|
||||
internal static DialogResult SafeShowMessageBox(string strText, string strTitle,
|
||||
MessageBoxButtons mb, MessageBoxIcon mi, MessageBoxDefaultButton mdb)
|
||||
{
|
||||
#if (KeePassLibSD || KeePassRT)
|
||||
return MessageBox.Show(strText, strTitle, mb, mi, mdb);
|
||||
#else
|
||||
IWin32Window wnd = null;
|
||||
try
|
||||
{
|
||||
Form f = GetTopForm();
|
||||
if((f != null) && f.InvokeRequired)
|
||||
return (DialogResult)f.Invoke(new SafeShowMessageBoxInternalDelegate(
|
||||
SafeShowMessageBoxInternal), f, strText, strTitle, mb, mi, mdb);
|
||||
else wnd = f;
|
||||
}
|
||||
catch(Exception) { Debug.Assert(false); }
|
||||
|
||||
if(wnd == null)
|
||||
{
|
||||
if(StrUtil.RightToLeft)
|
||||
return MessageBox.Show(strText, strTitle, mb, mi, mdb, m_mboRtl);
|
||||
return MessageBox.Show(strText, strTitle, mb, mi, mdb);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if(StrUtil.RightToLeft)
|
||||
return MessageBox.Show(wnd, strText, strTitle, mb, mi, mdb, m_mboRtl);
|
||||
return MessageBox.Show(wnd, strText, strTitle, mb, mi, mdb);
|
||||
}
|
||||
catch(Exception) { Debug.Assert(false); }
|
||||
|
||||
if(StrUtil.RightToLeft)
|
||||
return MessageBox.Show(strText, strTitle, mb, mi, mdb, m_mboRtl);
|
||||
return MessageBox.Show(strText, strTitle, mb, mi, mdb);
|
||||
#endif
|
||||
}
|
||||
|
||||
#if (!KeePassLibSD && !KeePassRT)
|
||||
internal delegate DialogResult SafeShowMessageBoxInternalDelegate(IWin32Window iParent,
|
||||
string strText, string strTitle, MessageBoxButtons mb, MessageBoxIcon mi,
|
||||
MessageBoxDefaultButton mdb);
|
||||
|
||||
internal static DialogResult SafeShowMessageBoxInternal(IWin32Window iParent,
|
||||
string strText, string strTitle, MessageBoxButtons mb, MessageBoxIcon mi,
|
||||
MessageBoxDefaultButton mdb)
|
||||
{
|
||||
if(StrUtil.RightToLeft)
|
||||
return MessageBox.Show(iParent, strText, strTitle, mb, mi, mdb, m_mboRtl);
|
||||
return MessageBox.Show(iParent, strText, strTitle, mb, mi, mdb);
|
||||
}
|
||||
#endif
|
||||
|
||||
public static void ShowInfo(params object[] vLines)
|
||||
{
|
||||
ShowInfoEx(null, vLines);
|
||||
}
|
||||
|
||||
public static void ShowInfoEx(string strTitle, params object[] vLines)
|
||||
{
|
||||
++m_uCurrentMessageCount;
|
||||
|
||||
strTitle = (strTitle ?? PwDefs.ShortProductName);
|
||||
string strText = ObjectsToMessage(vLines);
|
||||
|
||||
if(MessageService.MessageShowing != null)
|
||||
MessageService.MessageShowing(null, new MessageServiceEventArgs(
|
||||
strTitle, strText, MessageBoxButtons.OK, m_mbiInfo));
|
||||
|
||||
SafeShowMessageBox(strText, strTitle, MessageBoxButtons.OK, m_mbiInfo,
|
||||
MessageBoxDefaultButton.Button1);
|
||||
|
||||
--m_uCurrentMessageCount;
|
||||
}
|
||||
|
||||
public static void ShowWarning(params object[] vLines)
|
||||
{
|
||||
ShowWarningPriv(vLines, false);
|
||||
}
|
||||
|
||||
internal static void ShowWarningExcp(params object[] vLines)
|
||||
{
|
||||
ShowWarningPriv(vLines, true);
|
||||
}
|
||||
|
||||
private static void ShowWarningPriv(object[] vLines, bool bFullExceptions)
|
||||
{
|
||||
++m_uCurrentMessageCount;
|
||||
|
||||
string strTitle = PwDefs.ShortProductName;
|
||||
string strText = ObjectsToMessage(vLines, bFullExceptions);
|
||||
|
||||
if(MessageService.MessageShowing != null)
|
||||
MessageService.MessageShowing(null, new MessageServiceEventArgs(
|
||||
strTitle, strText, MessageBoxButtons.OK, m_mbiWarning));
|
||||
|
||||
SafeShowMessageBox(strText, strTitle, MessageBoxButtons.OK, m_mbiWarning,
|
||||
MessageBoxDefaultButton.Button1);
|
||||
|
||||
--m_uCurrentMessageCount;
|
||||
}
|
||||
|
||||
public static void ShowFatal(params object[] vLines)
|
||||
{
|
||||
++m_uCurrentMessageCount;
|
||||
|
||||
string strTitle = PwDefs.ShortProductName + " - " + KLRes.FatalError;
|
||||
string strText = KLRes.FatalErrorText + MessageService.NewParagraph +
|
||||
KLRes.ErrorInClipboard + MessageService.NewParagraph +
|
||||
// Please send it to the KeePass developers.
|
||||
// KLRes.ErrorFeedbackRequest + MessageService.NewParagraph +
|
||||
ObjectsToMessage(vLines);
|
||||
|
||||
try
|
||||
{
|
||||
string strDetails = ObjectsToMessage(vLines, true);
|
||||
|
||||
#if KeePassLibSD
|
||||
Clipboard.SetDataObject(strDetails);
|
||||
#else
|
||||
Clipboard.Clear();
|
||||
Clipboard.SetText(strDetails);
|
||||
#endif
|
||||
}
|
||||
catch(Exception) { Debug.Assert(false); }
|
||||
|
||||
if(MessageService.MessageShowing != null)
|
||||
MessageService.MessageShowing(null, new MessageServiceEventArgs(
|
||||
strTitle, strText, MessageBoxButtons.OK, m_mbiFatal));
|
||||
|
||||
SafeShowMessageBox(strText, strTitle, MessageBoxButtons.OK, m_mbiFatal,
|
||||
MessageBoxDefaultButton.Button1);
|
||||
|
||||
--m_uCurrentMessageCount;
|
||||
}
|
||||
|
||||
public static DialogResult Ask(string strText, string strTitle,
|
||||
MessageBoxButtons mbb)
|
||||
{
|
||||
++m_uCurrentMessageCount;
|
||||
|
||||
string strTextEx = (strText ?? string.Empty);
|
||||
string strTitleEx = (strTitle ?? PwDefs.ShortProductName);
|
||||
|
||||
if(MessageService.MessageShowing != null)
|
||||
MessageService.MessageShowing(null, new MessageServiceEventArgs(
|
||||
strTitleEx, strTextEx, mbb, m_mbiQuestion));
|
||||
|
||||
DialogResult dr = SafeShowMessageBox(strTextEx, strTitleEx, mbb,
|
||||
m_mbiQuestion, MessageBoxDefaultButton.Button1);
|
||||
|
||||
--m_uCurrentMessageCount;
|
||||
return dr;
|
||||
}
|
||||
|
||||
public static bool AskYesNo(string strText, string strTitle, bool bDefaultToYes,
|
||||
MessageBoxIcon mbi)
|
||||
{
|
||||
++m_uCurrentMessageCount;
|
||||
|
||||
string strTextEx = (strText ?? string.Empty);
|
||||
string strTitleEx = (strTitle ?? PwDefs.ShortProductName);
|
||||
|
||||
if(MessageService.MessageShowing != null)
|
||||
MessageService.MessageShowing(null, new MessageServiceEventArgs(
|
||||
strTitleEx, strTextEx, MessageBoxButtons.YesNo, mbi));
|
||||
|
||||
DialogResult dr = SafeShowMessageBox(strTextEx, strTitleEx,
|
||||
MessageBoxButtons.YesNo, mbi, bDefaultToYes ?
|
||||
MessageBoxDefaultButton.Button1 : MessageBoxDefaultButton.Button2);
|
||||
|
||||
--m_uCurrentMessageCount;
|
||||
return (dr == DialogResult.Yes);
|
||||
}
|
||||
|
||||
public static bool AskYesNo(string strText, string strTitle, bool bDefaultToYes)
|
||||
{
|
||||
return AskYesNo(strText, strTitle, bDefaultToYes, m_mbiQuestion);
|
||||
}
|
||||
|
||||
public static bool AskYesNo(string strText, string strTitle)
|
||||
{
|
||||
return AskYesNo(strText, strTitle, true, m_mbiQuestion);
|
||||
}
|
||||
|
||||
public static bool AskYesNo(string strText)
|
||||
{
|
||||
return AskYesNo(strText, null, true, m_mbiQuestion);
|
||||
}
|
||||
|
||||
public static void ShowLoadWarning(string strFilePath, Exception ex)
|
||||
{
|
||||
ShowLoadWarning(strFilePath, ex, false);
|
||||
}
|
||||
|
||||
public static void ShowLoadWarning(string strFilePath, Exception ex,
|
||||
bool bFullException)
|
||||
{
|
||||
ShowWarning(GetLoadWarningMessage(strFilePath, ex, bFullException));
|
||||
}
|
||||
|
||||
public static void ShowLoadWarning(IOConnectionInfo ioConnection, Exception ex)
|
||||
{
|
||||
if(ioConnection != null)
|
||||
ShowLoadWarning(ioConnection.GetDisplayName(), ex, false);
|
||||
else ShowWarning(ex);
|
||||
}
|
||||
|
||||
public static void ShowSaveWarning(string strFilePath, Exception ex,
|
||||
bool bCorruptionWarning)
|
||||
{
|
||||
FileLockException fl = (ex as FileLockException);
|
||||
if(fl != null)
|
||||
{
|
||||
ShowWarning(fl.Message);
|
||||
return;
|
||||
}
|
||||
|
||||
string str = GetSaveWarningMessage(strFilePath, ex, bCorruptionWarning);
|
||||
ShowWarning(str);
|
||||
}
|
||||
|
||||
public static void ShowSaveWarning(IOConnectionInfo ioConnection, Exception ex,
|
||||
bool bCorruptionWarning)
|
||||
{
|
||||
if(ioConnection != null)
|
||||
ShowSaveWarning(ioConnection.GetDisplayName(), ex, bCorruptionWarning);
|
||||
else ShowWarning(ex);
|
||||
}
|
||||
#endif // !KeePassUAP
|
||||
|
||||
internal static string GetLoadWarningMessage(string strFilePath,
|
||||
Exception ex, bool bFullException)
|
||||
{
|
||||
string str = string.Empty;
|
||||
|
||||
if(!string.IsNullOrEmpty(strFilePath))
|
||||
str += strFilePath + MessageService.NewParagraph;
|
||||
|
||||
str += KLRes.FileLoadFailed;
|
||||
|
||||
if((ex != null) && !string.IsNullOrEmpty(ex.Message))
|
||||
{
|
||||
str += MessageService.NewParagraph;
|
||||
if(!bFullException) str += ex.Message;
|
||||
else str += ObjectsToMessage(new object[] { ex }, true);
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
internal static string GetSaveWarningMessage(string strFilePath,
|
||||
Exception ex, bool bCorruptionWarning)
|
||||
{
|
||||
string str = string.Empty;
|
||||
if(!string.IsNullOrEmpty(strFilePath))
|
||||
str += strFilePath + MessageService.NewParagraph;
|
||||
|
||||
str += KLRes.FileSaveFailed;
|
||||
|
||||
if((ex != null) && !string.IsNullOrEmpty(ex.Message))
|
||||
str += MessageService.NewParagraph + ex.Message;
|
||||
|
||||
if(bCorruptionWarning)
|
||||
str += MessageService.NewParagraph + KLRes.FileSaveCorruptionWarning;
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
public static void ExternalIncrementMessageCount()
|
||||
{
|
||||
++m_uCurrentMessageCount;
|
||||
}
|
||||
|
||||
public static void ExternalDecrementMessageCount()
|
||||
{
|
||||
--m_uCurrentMessageCount;
|
||||
}
|
||||
}
|
||||
}
|
10
ModernKeePassLib/Utility/MonoWorkaround.PCL.cs
Normal file
10
ModernKeePassLib/Utility/MonoWorkaround.PCL.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace ModernKeePassLib.Utility
|
||||
{
|
||||
public static class MonoWorkarounds
|
||||
{
|
||||
public static bool IsRequired(int i)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
609
ModernKeePassLib/Utility/MonoWorkarounds.cs
Normal file
609
ModernKeePassLib/Utility/MonoWorkarounds.cs
Normal file
@@ -0,0 +1,609 @@
|
||||
/*
|
||||
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
|
||||
*/
|
||||
|
||||
#if DEBUG
|
||||
// #define DEBUG_BREAKONFAIL
|
||||
#endif
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Xml;
|
||||
|
||||
#if !KeePassUAP
|
||||
using System.Windows.Forms;
|
||||
#endif
|
||||
|
||||
using ModernKeePassLib.Native;
|
||||
|
||||
namespace ModernKeePassLib.Utility
|
||||
{
|
||||
public static class MonoWorkarounds
|
||||
{
|
||||
private const string AppXDoTool = "xdotool";
|
||||
|
||||
private static Dictionary<uint, bool> g_dForceReq = new Dictionary<uint, bool>();
|
||||
private static Thread g_thFixClip = null;
|
||||
// private static Predicate<IntPtr> g_fOwnWindow = null;
|
||||
|
||||
#if DEBUG_BREAKONFAIL
|
||||
private static DebugBreakTraceListener g_tlBreak = null;
|
||||
#endif
|
||||
|
||||
private static bool? g_bReq = null;
|
||||
public static bool IsRequired()
|
||||
{
|
||||
if(!g_bReq.HasValue) g_bReq = NativeLib.IsUnix();
|
||||
return g_bReq.Value;
|
||||
}
|
||||
|
||||
// 106:
|
||||
// Mono throws exceptions when no X server is running.
|
||||
// https://sourceforge.net/p/keepass/patches/106/
|
||||
// 1219:
|
||||
// Mono prepends byte order mark (BOM) to StdIn.
|
||||
// https://sourceforge.net/p/keepass/bugs/1219/
|
||||
// 1245:
|
||||
// Key events not raised while Alt is down, and nav keys out of order.
|
||||
// https://sourceforge.net/p/keepass/bugs/1245/
|
||||
// 1254:
|
||||
// NumericUpDown bug: text is drawn below up/down buttons.
|
||||
// https://sourceforge.net/p/keepass/bugs/1254/
|
||||
// 1354:
|
||||
// Finalizer of NotifyIcon throws on Unity.
|
||||
// See also 1574.
|
||||
// https://sourceforge.net/p/keepass/bugs/1354/
|
||||
// 1358:
|
||||
// FileDialog crashes when ~/.recently-used is invalid.
|
||||
// https://sourceforge.net/p/keepass/bugs/1358/
|
||||
// 1366:
|
||||
// Drawing bug when scrolling a RichTextBox.
|
||||
// https://sourceforge.net/p/keepass/bugs/1366/
|
||||
// 1378:
|
||||
// Mono doesn't implement Microsoft.Win32.SystemEvents events.
|
||||
// https://sourceforge.net/p/keepass/bugs/1378/
|
||||
// https://github.com/mono/mono/blob/master/mcs/class/System/Microsoft.Win32/SystemEvents.cs
|
||||
// 1418:
|
||||
// Minimizing a form while loading it doesn't work.
|
||||
// https://sourceforge.net/p/keepass/bugs/1418/
|
||||
// 1468:
|
||||
// Use LibGCrypt for AES-KDF, because Mono's implementations
|
||||
// of RijndaelManaged and AesCryptoServiceProvider are slow.
|
||||
// https://sourceforge.net/p/keepass/bugs/1468/
|
||||
// 1527:
|
||||
// Timer causes 100% CPU load.
|
||||
// https://sourceforge.net/p/keepass/bugs/1527/
|
||||
// 1530:
|
||||
// Mono's clipboard functions don't work properly.
|
||||
// https://sourceforge.net/p/keepass/bugs/1530/
|
||||
// 1574:
|
||||
// Finalizer of NotifyIcon throws on Mac OS X.
|
||||
// See also 1354.
|
||||
// https://sourceforge.net/p/keepass/bugs/1574/
|
||||
// 1632:
|
||||
// RichTextBox rendering bug for bold/italic text.
|
||||
// https://sourceforge.net/p/keepass/bugs/1632/
|
||||
// 1690:
|
||||
// Removing items from a list view doesn't work properly.
|
||||
// https://sourceforge.net/p/keepass/bugs/1690/
|
||||
// 1710:
|
||||
// Mono doesn't always raise the FormClosed event properly.
|
||||
// https://sourceforge.net/p/keepass/bugs/1710/
|
||||
// 1716:
|
||||
// 'Always on Top' doesn't work properly on the Cinnamon desktop.
|
||||
// https://sourceforge.net/p/keepass/bugs/1716/
|
||||
// 1760:
|
||||
// Input focus is not restored when activating a form.
|
||||
// https://sourceforge.net/p/keepass/bugs/1760/
|
||||
// 2139:
|
||||
// Shortcut keys are ignored.
|
||||
// https://sourceforge.net/p/keepass/feature-requests/2139/
|
||||
// 2140:
|
||||
// Explicit control focusing is ignored.
|
||||
// https://sourceforge.net/p/keepass/feature-requests/2140/
|
||||
// 5795:
|
||||
// Text in input field is incomplete.
|
||||
// https://bugzilla.xamarin.com/show_bug.cgi?id=5795
|
||||
// https://sourceforge.net/p/keepass/discussion/329220/thread/d23dc88b/
|
||||
// 9604:
|
||||
// Trying to resolve a non-existing metadata token crashes Mono.
|
||||
// https://github.com/mono/mono/issues/9604
|
||||
// 10163:
|
||||
// WebRequest GetResponse call missing, breaks WebDAV due to no PUT.
|
||||
// https://bugzilla.xamarin.com/show_bug.cgi?id=10163
|
||||
// https://sourceforge.net/p/keepass/bugs/1117/
|
||||
// https://sourceforge.net/p/keepass/discussion/329221/thread/9422258c/
|
||||
// https://github.com/mono/mono/commit/8e67b8c2fc7cb66bff7816ebf7c1039fb8cfc43b
|
||||
// https://bugzilla.xamarin.com/show_bug.cgi?id=1512
|
||||
// https://sourceforge.net/p/keepass/patches/89/
|
||||
// 12525:
|
||||
// PictureBox not rendered when bitmap height >= control height.
|
||||
// https://bugzilla.xamarin.com/show_bug.cgi?id=12525
|
||||
// https://sourceforge.net/p/keepass/discussion/329220/thread/54f61e9a/
|
||||
// 100001:
|
||||
// Control locations/sizes are invalid/unexpected.
|
||||
// [NoRef]
|
||||
// 373134:
|
||||
// Control.InvokeRequired doesn't always return the correct value.
|
||||
// https://bugzilla.novell.com/show_bug.cgi?id=373134
|
||||
// 586901:
|
||||
// RichTextBox doesn't handle Unicode string correctly.
|
||||
// https://bugzilla.novell.com/show_bug.cgi?id=586901
|
||||
// 620618:
|
||||
// ListView column headers not drawn.
|
||||
// https://bugzilla.novell.com/show_bug.cgi?id=620618
|
||||
// 649266:
|
||||
// Calling Control.Hide doesn't remove the application from taskbar.
|
||||
// https://bugzilla.novell.com/show_bug.cgi?id=649266
|
||||
// 686017:
|
||||
// Minimum sizes must be enforced.
|
||||
// https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=686017
|
||||
// 801414:
|
||||
// Mono recreates the main window incorrectly.
|
||||
// https://bugs.launchpad.net/ubuntu/+source/keepass2/+bug/801414
|
||||
// 891029:
|
||||
// Increase tab control height and don't use images on tabs.
|
||||
// https://sourceforge.net/projects/keepass/forums/forum/329221/topic/4519750
|
||||
// https://bugs.launchpad.net/ubuntu/+source/keepass2/+bug/891029
|
||||
// https://sourceforge.net/p/keepass/bugs/1256/
|
||||
// https://sourceforge.net/p/keepass/bugs/1566/
|
||||
// https://sourceforge.net/p/keepass/bugs/1634/
|
||||
// 836428016:
|
||||
// ListView group header selection unsupported.
|
||||
// https://sourceforge.net/p/keepass/discussion/329221/thread/31dae0f0/
|
||||
// 2449941153:
|
||||
// RichTextBox doesn't properly escape '}' when generating RTF data.
|
||||
// https://sourceforge.net/p/keepass/discussion/329221/thread/920722a1/
|
||||
// 3471228285:
|
||||
// Mono requires command line arguments to be encoded differently.
|
||||
// https://sourceforge.net/p/keepass/discussion/329221/thread/cee6bd7d/
|
||||
// 3574233558:
|
||||
// Problems with minimizing windows, no content rendered.
|
||||
// https://sourceforge.net/p/keepass/discussion/329220/thread/d50a79d6/
|
||||
public static bool IsRequired(uint uBugID)
|
||||
{
|
||||
if(!MonoWorkarounds.IsRequired()) return false;
|
||||
|
||||
bool bForce;
|
||||
if(g_dForceReq.TryGetValue(uBugID, out bForce)) return bForce;
|
||||
|
||||
ulong v = NativeLib.MonoVersion;
|
||||
if(v != 0)
|
||||
{
|
||||
if(uBugID == 10163)
|
||||
return (v >= 0x0002000B00000000UL); // >= 2.11
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
internal static void SetEnabled(string strIDs, bool bEnabled)
|
||||
{
|
||||
if(string.IsNullOrEmpty(strIDs)) return;
|
||||
|
||||
string[] vIDs = strIDs.Split(new char[] { ',' });
|
||||
foreach(string strID in vIDs)
|
||||
{
|
||||
if(string.IsNullOrEmpty(strID)) continue;
|
||||
|
||||
uint uID;
|
||||
if(StrUtil.TryParseUInt(strID.Trim(), out uID))
|
||||
g_dForceReq[uID] = bEnabled;
|
||||
}
|
||||
}
|
||||
|
||||
internal static void Initialize()
|
||||
{
|
||||
Terminate();
|
||||
|
||||
// g_fOwnWindow = fOwnWindow;
|
||||
|
||||
if(IsRequired(1530))
|
||||
{
|
||||
try
|
||||
{
|
||||
ThreadStart ts = new ThreadStart(MonoWorkarounds.FixClipThread);
|
||||
g_thFixClip = new Thread(ts);
|
||||
g_thFixClip.Start();
|
||||
}
|
||||
catch(Exception) { Debug.Assert(false); }
|
||||
}
|
||||
|
||||
#if DEBUG_BREAKONFAIL
|
||||
if(IsRequired() && (g_tlBreak == null))
|
||||
{
|
||||
g_tlBreak = new DebugBreakTraceListener();
|
||||
Debug.Listeners.Add(g_tlBreak);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
internal static void Terminate()
|
||||
{
|
||||
if(g_thFixClip != null)
|
||||
{
|
||||
try { g_thFixClip.Abort(); }
|
||||
catch(Exception) { Debug.Assert(false); }
|
||||
|
||||
g_thFixClip = null;
|
||||
}
|
||||
}
|
||||
|
||||
private static void FixClipThread()
|
||||
{
|
||||
try
|
||||
{
|
||||
#if !KeePassUAP
|
||||
const int msDelay = 250;
|
||||
|
||||
string strTest = ClipboardU.GetText();
|
||||
if(strTest == null) return; // No clipboard support
|
||||
|
||||
// Without XDoTool, the workaround would be applied to
|
||||
// all applications, which may corrupt the clipboard
|
||||
// when it doesn't contain simple text only;
|
||||
// https://sourceforge.net/p/keepass/bugs/1603/#a113
|
||||
strTest = (NativeLib.RunConsoleApp(AppXDoTool,
|
||||
"help") ?? string.Empty).Trim();
|
||||
if(strTest.Length == 0) return;
|
||||
|
||||
Thread.Sleep(msDelay);
|
||||
|
||||
string strLast = null;
|
||||
while(true)
|
||||
{
|
||||
string str = ClipboardU.GetText();
|
||||
if(str == null) { Debug.Assert(false); }
|
||||
else if(str != strLast)
|
||||
{
|
||||
if(NeedClipboardWorkaround())
|
||||
ClipboardU.SetText(str, true);
|
||||
|
||||
strLast = str;
|
||||
}
|
||||
|
||||
Thread.Sleep(msDelay);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
catch(ThreadAbortException)
|
||||
{
|
||||
try { Thread.ResetAbort(); }
|
||||
catch(Exception) { Debug.Assert(false); }
|
||||
}
|
||||
catch(Exception) { Debug.Assert(false); }
|
||||
finally { g_thFixClip = null; }
|
||||
}
|
||||
|
||||
#if !KeePassUAP
|
||||
private static bool NeedClipboardWorkaround()
|
||||
{
|
||||
try
|
||||
{
|
||||
string strHandle = (NativeLib.RunConsoleApp(AppXDoTool,
|
||||
"getactivewindow") ?? string.Empty).Trim();
|
||||
if(strHandle.Length == 0) { Debug.Assert(false); return false; }
|
||||
|
||||
// IntPtr h = new IntPtr(long.Parse(strHandle));
|
||||
long.Parse(strHandle); // Validate
|
||||
|
||||
// Detection of own windows based on Form.Handle
|
||||
// comparisons doesn't work reliably (Mono's handles
|
||||
// are usually off by 1)
|
||||
// Predicate<IntPtr> fOwnWindow = g_fOwnWindow;
|
||||
// if(fOwnWindow != null)
|
||||
// {
|
||||
// if(fOwnWindow(h)) return true;
|
||||
// }
|
||||
// else { Debug.Assert(false); }
|
||||
|
||||
string strWmClass = (NativeLib.RunConsoleApp("xprop",
|
||||
"-id " + strHandle + " WM_CLASS") ?? string.Empty);
|
||||
|
||||
if(strWmClass.IndexOf("\"" + PwDefs.ResClass + "\"",
|
||||
StrUtil.CaseIgnoreCmp) >= 0) return true;
|
||||
if(strWmClass.IndexOf("\"Remmina\"",
|
||||
StrUtil.CaseIgnoreCmp) >= 0) return true;
|
||||
}
|
||||
catch(ThreadAbortException) { throw; }
|
||||
catch(Exception) { Debug.Assert(false); }
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static void ApplyTo(Form f)
|
||||
{
|
||||
if(!MonoWorkarounds.IsRequired()) return;
|
||||
if(f == null) { Debug.Assert(false); return; }
|
||||
|
||||
#if (!KeePassLibSD && !KeePassRT)
|
||||
f.HandleCreated += MonoWorkarounds.OnFormHandleCreated;
|
||||
SetWmClass(f);
|
||||
|
||||
ApplyToControlsRec(f.Controls, f, MonoWorkarounds.ApplyToControl);
|
||||
#endif
|
||||
}
|
||||
|
||||
public static void Release(Form f)
|
||||
{
|
||||
if(!MonoWorkarounds.IsRequired()) return;
|
||||
if(f == null) { Debug.Assert(false); return; }
|
||||
|
||||
#if (!KeePassLibSD && !KeePassRT)
|
||||
f.HandleCreated -= MonoWorkarounds.OnFormHandleCreated;
|
||||
|
||||
ApplyToControlsRec(f.Controls, f, MonoWorkarounds.ReleaseControl);
|
||||
#endif
|
||||
}
|
||||
|
||||
#if (!KeePassLibSD && !KeePassRT)
|
||||
private delegate void MwaControlHandler(Control c, Form fContext);
|
||||
|
||||
private static void ApplyToControlsRec(Control.ControlCollection cc,
|
||||
Form fContext, MwaControlHandler fn)
|
||||
{
|
||||
if(cc == null) { Debug.Assert(false); return; }
|
||||
|
||||
foreach(Control c in cc)
|
||||
{
|
||||
fn(c, fContext);
|
||||
ApplyToControlsRec(c.Controls, fContext, fn);
|
||||
}
|
||||
}
|
||||
|
||||
private static void ApplyToControl(Control c, Form fContext)
|
||||
{
|
||||
Button btn = (c as Button);
|
||||
if(btn != null) ApplyToButton(btn, fContext);
|
||||
|
||||
NumericUpDown nc = (c as NumericUpDown);
|
||||
if((nc != null) && MonoWorkarounds.IsRequired(1254))
|
||||
{
|
||||
if(nc.TextAlign == HorizontalAlignment.Right)
|
||||
nc.TextAlign = HorizontalAlignment.Left;
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class MwaHandlerInfo
|
||||
{
|
||||
private readonly Delegate m_fnOrg; // May be null
|
||||
public Delegate FunctionOriginal
|
||||
{
|
||||
get { return m_fnOrg; }
|
||||
}
|
||||
|
||||
private readonly Delegate m_fnOvr;
|
||||
public Delegate FunctionOverride
|
||||
{
|
||||
get { return m_fnOvr; }
|
||||
}
|
||||
|
||||
private readonly DialogResult m_dr;
|
||||
public DialogResult Result
|
||||
{
|
||||
get { return m_dr; }
|
||||
}
|
||||
|
||||
private readonly Form m_fContext;
|
||||
public Form FormContext
|
||||
{
|
||||
get { return m_fContext; }
|
||||
}
|
||||
|
||||
public MwaHandlerInfo(Delegate fnOrg, Delegate fnOvr, DialogResult dr,
|
||||
Form fContext)
|
||||
{
|
||||
m_fnOrg = fnOrg;
|
||||
m_fnOvr = fnOvr;
|
||||
m_dr = dr;
|
||||
m_fContext = fContext;
|
||||
}
|
||||
}
|
||||
|
||||
private static EventHandlerList GetEventHandlers(Component c,
|
||||
out object objClickEvent)
|
||||
{
|
||||
FieldInfo fi = typeof(Control).GetField("ClickEvent", // Mono
|
||||
BindingFlags.Static | BindingFlags.NonPublic);
|
||||
if(fi == null)
|
||||
fi = typeof(Control).GetField("EventClick", // .NET
|
||||
BindingFlags.Static | BindingFlags.NonPublic);
|
||||
if(fi == null) { Debug.Assert(false); objClickEvent = null; return null; }
|
||||
|
||||
objClickEvent = fi.GetValue(null);
|
||||
if(objClickEvent == null) { Debug.Assert(false); return null; }
|
||||
|
||||
PropertyInfo pi = typeof(Component).GetProperty("Events",
|
||||
BindingFlags.Instance | BindingFlags.NonPublic);
|
||||
return (pi.GetValue(c, null) as EventHandlerList);
|
||||
}
|
||||
|
||||
private static Dictionary<object, MwaHandlerInfo> m_dictHandlers =
|
||||
new Dictionary<object, MwaHandlerInfo>();
|
||||
private static void ApplyToButton(Button btn, Form fContext)
|
||||
{
|
||||
DialogResult dr = btn.DialogResult;
|
||||
if(dr == DialogResult.None) return; // No workaround required
|
||||
|
||||
object objClickEvent;
|
||||
EventHandlerList ehl = GetEventHandlers(btn, out objClickEvent);
|
||||
if(ehl == null) { Debug.Assert(false); return; }
|
||||
Delegate fnClick = ehl[objClickEvent]; // May be null
|
||||
|
||||
EventHandler fnOvr = new EventHandler(MonoWorkarounds.OnButtonClick);
|
||||
m_dictHandlers[btn] = new MwaHandlerInfo(fnClick, fnOvr, dr, fContext);
|
||||
|
||||
btn.DialogResult = DialogResult.None;
|
||||
if(fnClick != null) ehl.RemoveHandler(objClickEvent, fnClick);
|
||||
ehl[objClickEvent] = fnOvr;
|
||||
}
|
||||
|
||||
private static void ReleaseControl(Control c, Form fContext)
|
||||
{
|
||||
Button btn = (c as Button);
|
||||
if(btn != null) ReleaseButton(btn, fContext);
|
||||
}
|
||||
|
||||
private static void ReleaseButton(Button btn, Form fContext)
|
||||
{
|
||||
MwaHandlerInfo hi;
|
||||
m_dictHandlers.TryGetValue(btn, out hi);
|
||||
if(hi == null) return;
|
||||
|
||||
object objClickEvent;
|
||||
EventHandlerList ehl = GetEventHandlers(btn, out objClickEvent);
|
||||
if(ehl == null) { Debug.Assert(false); return; }
|
||||
|
||||
ehl.RemoveHandler(objClickEvent, hi.FunctionOverride);
|
||||
if(hi.FunctionOriginal != null)
|
||||
ehl[objClickEvent] = hi.FunctionOriginal;
|
||||
|
||||
btn.DialogResult = hi.Result;
|
||||
m_dictHandlers.Remove(btn);
|
||||
}
|
||||
|
||||
private static void OnButtonClick(object sender, EventArgs e)
|
||||
{
|
||||
Button btn = (sender as Button);
|
||||
if(btn == null) { Debug.Assert(false); return; }
|
||||
|
||||
MwaHandlerInfo hi;
|
||||
m_dictHandlers.TryGetValue(btn, out hi);
|
||||
if(hi == null) { Debug.Assert(false); return; }
|
||||
|
||||
Form f = hi.FormContext;
|
||||
|
||||
// Set current dialog result by setting the form's private
|
||||
// variable; the DialogResult property can't be used,
|
||||
// because it raises close events
|
||||
FieldInfo fiRes = typeof(Form).GetField("dialog_result",
|
||||
BindingFlags.Instance | BindingFlags.NonPublic);
|
||||
if(fiRes == null) { Debug.Assert(false); return; }
|
||||
if(f != null) fiRes.SetValue(f, hi.Result);
|
||||
|
||||
if(hi.FunctionOriginal != null)
|
||||
hi.FunctionOriginal.Method.Invoke(hi.FunctionOriginal.Target,
|
||||
new object[] { btn, e });
|
||||
|
||||
// Raise close events, if the click event handler hasn't
|
||||
// reset the dialog result
|
||||
if((f != null) && (f.DialogResult == hi.Result))
|
||||
f.DialogResult = hi.Result; // Raises close events
|
||||
}
|
||||
|
||||
private static void SetWmClass(Form f)
|
||||
{
|
||||
NativeMethods.SetWmClass(f, PwDefs.UnixName, PwDefs.ResClass);
|
||||
}
|
||||
|
||||
private static void OnFormHandleCreated(object sender, EventArgs e)
|
||||
{
|
||||
Form f = (sender as Form);
|
||||
if(f == null) { Debug.Assert(false); return; }
|
||||
|
||||
if(!f.IsHandleCreated) return; // Prevent infinite loop
|
||||
|
||||
SetWmClass(f);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set the value of the private <c>shown_raised</c> member
|
||||
/// variable of a form.
|
||||
/// </summary>
|
||||
/// <returns>Previous <c>shown_raised</c> value.</returns>
|
||||
internal static bool ExchangeFormShownRaised(Form f, bool bNewValue)
|
||||
{
|
||||
if(f == null) { Debug.Assert(false); return true; }
|
||||
|
||||
try
|
||||
{
|
||||
FieldInfo fi = typeof(Form).GetField("shown_raised",
|
||||
BindingFlags.Instance | BindingFlags.NonPublic);
|
||||
if(fi == null) { Debug.Assert(false); return true; }
|
||||
|
||||
bool bPrevious = (bool)fi.GetValue(f);
|
||||
|
||||
fi.SetValue(f, bNewValue);
|
||||
|
||||
return bPrevious;
|
||||
}
|
||||
catch(Exception) { Debug.Assert(false); }
|
||||
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// Ensure that the file ~/.recently-used is valid (in order to
|
||||
/// prevent Mono's FileDialog from crashing).
|
||||
/// </summary>
|
||||
internal static void EnsureRecentlyUsedValid()
|
||||
{
|
||||
if(!MonoWorkarounds.IsRequired(1358)) return;
|
||||
|
||||
try
|
||||
{
|
||||
string strFile = Environment.GetFolderPath(
|
||||
Environment.SpecialFolder.Personal);
|
||||
strFile = UrlUtil.EnsureTerminatingSeparator(strFile, false);
|
||||
strFile += ".recently-used";
|
||||
|
||||
if(File.Exists(strFile))
|
||||
{
|
||||
try
|
||||
{
|
||||
// Mono's WriteRecentlyUsedFiles method also loads the
|
||||
// XML file using XmlDocument
|
||||
XmlDocument xd = XmlUtilEx.CreateXmlDocument();
|
||||
xd.Load(strFile);
|
||||
}
|
||||
catch(Exception) // The XML file is invalid
|
||||
{
|
||||
File.Delete(strFile);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch(Exception) { Debug.Assert(false); }
|
||||
}
|
||||
#endif // !KeePassUAP
|
||||
|
||||
#if DEBUG_BREAKONFAIL
|
||||
private sealed class DebugBreakTraceListener : TraceListener
|
||||
{
|
||||
public override void Fail(string message)
|
||||
{
|
||||
Debugger.Break();
|
||||
}
|
||||
|
||||
public override void Fail(string message, string detailMessage)
|
||||
{
|
||||
Debugger.Break();
|
||||
}
|
||||
|
||||
public override void Write(string message) { }
|
||||
public override void WriteLine(string message) { }
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
1900
ModernKeePassLib/Utility/StrUtil.cs
Normal file
1900
ModernKeePassLib/Utility/StrUtil.cs
Normal file
File diff suppressed because it is too large
Load Diff
17
ModernKeePassLib/Utility/StreamExtensions.cs
Normal file
17
ModernKeePassLib/Utility/StreamExtensions.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using System.IO;
|
||||
using Windows.Storage.Streams;
|
||||
|
||||
namespace ModernKeePassLibPCL.Utility
|
||||
{
|
||||
public static class StreamExtensions
|
||||
{
|
||||
public static Stream AsStream(this IRandomAccessStream inputStream)
|
||||
{
|
||||
var reader = new DataReader(inputStream.GetInputStreamAt(0));
|
||||
var bytes = new byte[inputStream.Size];
|
||||
reader.LoadAsync((uint)inputStream.Size).GetResults();
|
||||
reader.ReadBytes(bytes);
|
||||
return new MemoryStream(bytes);
|
||||
}
|
||||
}
|
||||
}
|
482
ModernKeePassLib/Utility/TimeUtil.cs
Normal file
482
ModernKeePassLib/Utility/TimeUtil.cs
Normal file
@@ -0,0 +1,482 @@
|
||||
/*
|
||||
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.Globalization;
|
||||
using System.Text;
|
||||
|
||||
using ModernKeePassLib.Interfaces;
|
||||
|
||||
namespace ModernKeePassLib.Utility
|
||||
{
|
||||
/// <summary>
|
||||
/// Contains various static time structure manipulation and conversion
|
||||
/// routines.
|
||||
/// </summary>
|
||||
public static class TimeUtil
|
||||
{
|
||||
/// <summary>
|
||||
/// Length of a compressed <c>PW_TIME</c> structure in bytes.
|
||||
/// </summary>
|
||||
public const int PwTimeLength = 7;
|
||||
|
||||
public static readonly DateTime SafeMinValueUtc = new DateTime(
|
||||
DateTime.MinValue.Ticks + TimeSpan.TicksPerDay, DateTimeKind.Utc);
|
||||
public static readonly DateTime SafeMaxValueUtc = new DateTime(
|
||||
DateTime.MaxValue.Ticks - TimeSpan.TicksPerDay, DateTimeKind.Utc);
|
||||
|
||||
#if !KeePassLibSD
|
||||
private static string m_strDtfStd = null;
|
||||
private static string m_strDtfDate = null;
|
||||
#endif
|
||||
|
||||
// private static long m_lTicks2PowLess1s = 0;
|
||||
|
||||
private static DateTime? m_odtUnixRoot = null;
|
||||
public static DateTime UnixRoot
|
||||
{
|
||||
get
|
||||
{
|
||||
if(m_odtUnixRoot.HasValue) return m_odtUnixRoot.Value;
|
||||
|
||||
DateTime dtRoot = new DateTime(1970, 1, 1, 0, 0, 0, 0,
|
||||
DateTimeKind.Utc);
|
||||
|
||||
m_odtUnixRoot = dtRoot;
|
||||
return dtRoot;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Pack a <c>DateTime</c> object into 5 bytes. Layout: 2 zero bits,
|
||||
/// year 12 bits, month 4 bits, day 5 bits, hour 5 bits, minute 6
|
||||
/// bits, second 6 bits.
|
||||
/// </summary>
|
||||
[Obsolete]
|
||||
public static byte[] PackTime(DateTime dt)
|
||||
{
|
||||
dt = ToLocal(dt, true);
|
||||
|
||||
byte[] pb = new byte[5];
|
||||
|
||||
// Pack time to 5 byte structure:
|
||||
// Byte bits: 11111111 22222222 33333333 44444444 55555555
|
||||
// Contents : 00YYYYYY YYYYYYMM MMDDDDDH HHHHMMMM MMSSSSSS
|
||||
pb[0] = (byte)((dt.Year >> 6) & 0x3F);
|
||||
pb[1] = (byte)(((dt.Year & 0x3F) << 2) | ((dt.Month >> 2) & 0x03));
|
||||
pb[2] = (byte)(((dt.Month & 0x03) << 6) | ((dt.Day & 0x1F) << 1) |
|
||||
((dt.Hour >> 4) & 0x01));
|
||||
pb[3] = (byte)(((dt.Hour & 0x0F) << 4) | ((dt.Minute >> 2) & 0x0F));
|
||||
pb[4] = (byte)(((dt.Minute & 0x03) << 6) | (dt.Second & 0x3F));
|
||||
|
||||
return pb;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unpack a packed time (5 bytes, packed by the <c>PackTime</c>
|
||||
/// member function) to a <c>DateTime</c> object.
|
||||
/// </summary>
|
||||
/// <param name="pb">Packed time, 5 bytes.</param>
|
||||
/// <returns>Unpacked <c>DateTime</c> object.</returns>
|
||||
[Obsolete]
|
||||
public static DateTime UnpackTime(byte[] pb)
|
||||
{
|
||||
Debug.Assert((pb != null) && (pb.Length == 5));
|
||||
if(pb == null) throw new ArgumentNullException("pb");
|
||||
if(pb.Length != 5) throw new ArgumentException();
|
||||
|
||||
int n1 = pb[0], n2 = pb[1], n3 = pb[2], n4 = pb[3], n5 = pb[4];
|
||||
|
||||
// Unpack 5 byte structure to date and time
|
||||
int nYear = (n1 << 6) | (n2 >> 2);
|
||||
int nMonth = ((n2 & 0x00000003) << 2) | (n3 >> 6);
|
||||
int nDay = (n3 >> 1) & 0x0000001F;
|
||||
int nHour = ((n3 & 0x00000001) << 4) | (n4 >> 4);
|
||||
int nMinute = ((n4 & 0x0000000F) << 2) | (n5 >> 6);
|
||||
int nSecond = n5 & 0x0000003F;
|
||||
|
||||
return (new DateTime(nYear, nMonth, nDay, nHour, nMinute,
|
||||
nSecond, DateTimeKind.Local)).ToUniversalTime();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Pack a <c>DateTime</c> object into 7 bytes (<c>PW_TIME</c>).
|
||||
/// </summary>
|
||||
/// <param name="dt">Object to be encoded.</param>
|
||||
/// <returns>Packed time, 7 bytes (<c>PW_TIME</c>).</returns>
|
||||
[Obsolete]
|
||||
public static byte[] PackPwTime(DateTime dt)
|
||||
{
|
||||
Debug.Assert(PwTimeLength == 7);
|
||||
|
||||
dt = ToLocal(dt, true);
|
||||
|
||||
byte[] pb = new byte[7];
|
||||
pb[0] = (byte)(dt.Year & 0xFF);
|
||||
pb[1] = (byte)(dt.Year >> 8);
|
||||
pb[2] = (byte)dt.Month;
|
||||
pb[3] = (byte)dt.Day;
|
||||
pb[4] = (byte)dt.Hour;
|
||||
pb[5] = (byte)dt.Minute;
|
||||
pb[6] = (byte)dt.Second;
|
||||
|
||||
return pb;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unpack a packed time (7 bytes, <c>PW_TIME</c>) to a <c>DateTime</c> object.
|
||||
/// </summary>
|
||||
/// <param name="pb">Packed time, 7 bytes.</param>
|
||||
/// <returns>Unpacked <c>DateTime</c> object.</returns>
|
||||
[Obsolete]
|
||||
public static DateTime UnpackPwTime(byte[] pb)
|
||||
{
|
||||
Debug.Assert(PwTimeLength == 7);
|
||||
|
||||
Debug.Assert(pb != null); if(pb == null) throw new ArgumentNullException("pb");
|
||||
Debug.Assert(pb.Length == 7); if(pb.Length != 7) throw new ArgumentException();
|
||||
|
||||
return (new DateTime(((int)pb[1] << 8) | (int)pb[0], (int)pb[2], (int)pb[3],
|
||||
(int)pb[4], (int)pb[5], (int)pb[6], DateTimeKind.Local)).ToUniversalTime();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert a <c>DateTime</c> object to a displayable string.
|
||||
/// </summary>
|
||||
/// <param name="dt"><c>DateTime</c> object to convert to a string.</param>
|
||||
/// <returns>String representing the specified <c>DateTime</c> object.</returns>
|
||||
public static string ToDisplayString(DateTime dt)
|
||||
{
|
||||
return ToLocal(dt, true).ToString();
|
||||
}
|
||||
|
||||
public static string ToDisplayStringDateOnly(DateTime dt)
|
||||
{
|
||||
return ToLocal(dt, true).ToString("d");
|
||||
}
|
||||
|
||||
public static DateTime FromDisplayString(string strDisplay)
|
||||
{
|
||||
DateTime dt;
|
||||
if(FromDisplayStringEx(strDisplay, out dt)) return dt;
|
||||
return DateTime.Now;
|
||||
}
|
||||
|
||||
public static bool FromDisplayStringEx(string strDisplay, out DateTime dt)
|
||||
{
|
||||
#if KeePassLibSD
|
||||
try { dt = ToLocal(DateTime.Parse(strDisplay), true); return true; }
|
||||
catch(Exception) { }
|
||||
#else
|
||||
if(DateTime.TryParse(strDisplay, out dt))
|
||||
{
|
||||
dt = ToLocal(dt, true);
|
||||
return true;
|
||||
}
|
||||
|
||||
// For some custom formats specified using the Control Panel,
|
||||
// DateTime.ToString returns the correct string, but
|
||||
// DateTime.TryParse fails (e.g. for "//dd/MMM/yyyy");
|
||||
// https://sourceforge.net/p/keepass/discussion/329221/thread/3a225b29/?limit=25&page=1#c6ae
|
||||
if((m_strDtfStd == null) || (m_strDtfDate == null))
|
||||
{
|
||||
DateTime dtUni = new DateTime(2111, 3, 4, 5, 6, 7, DateTimeKind.Local);
|
||||
m_strDtfStd = DeriveCustomFormat(ToDisplayString(dtUni), dtUni);
|
||||
m_strDtfDate = DeriveCustomFormat(ToDisplayStringDateOnly(dtUni), dtUni);
|
||||
}
|
||||
const DateTimeStyles dts = DateTimeStyles.AllowWhiteSpaces;
|
||||
if(DateTime.TryParseExact(strDisplay, m_strDtfStd, null, dts, out dt))
|
||||
{
|
||||
dt = ToLocal(dt, true);
|
||||
return true;
|
||||
}
|
||||
if(DateTime.TryParseExact(strDisplay, m_strDtfDate, null, dts, out dt))
|
||||
{
|
||||
dt = ToLocal(dt, true);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
|
||||
Debug.Assert(false);
|
||||
return false;
|
||||
}
|
||||
|
||||
#if !KeePassLibSD
|
||||
private static string DeriveCustomFormat(string strDT, DateTime dt)
|
||||
{
|
||||
string[] vPlh = new string[] {
|
||||
// Names, sorted by length
|
||||
"MMMM", "dddd",
|
||||
"MMM", "ddd",
|
||||
"gg", "g",
|
||||
|
||||
// Numbers, the ones with prefix '0' first
|
||||
"yyyy", "yyy", "yy", "y",
|
||||
"MM", "M",
|
||||
"dd", "d",
|
||||
"HH", "hh", "H", "h",
|
||||
"mm", "m",
|
||||
"ss", "s",
|
||||
|
||||
"tt", "t"
|
||||
};
|
||||
|
||||
List<string> lValues = new List<string>();
|
||||
foreach(string strPlh in vPlh)
|
||||
{
|
||||
string strEval = strPlh;
|
||||
if(strEval.Length == 1) strEval = @"%" + strPlh; // Make custom
|
||||
|
||||
lValues.Add(dt.ToString(strEval));
|
||||
}
|
||||
|
||||
StringBuilder sbAll = new StringBuilder();
|
||||
sbAll.Append("dfFghHKmMstyz:/\"\'\\%");
|
||||
sbAll.Append(strDT);
|
||||
foreach(string strVEnum in lValues) { sbAll.Append(strVEnum); }
|
||||
|
||||
List<char> lCodes = new List<char>();
|
||||
for(int i = 0; i < vPlh.Length; ++i)
|
||||
{
|
||||
char ch = StrUtil.GetUnusedChar(sbAll.ToString());
|
||||
lCodes.Add(ch);
|
||||
sbAll.Append(ch);
|
||||
}
|
||||
|
||||
string str = strDT;
|
||||
for(int i = 0; i < vPlh.Length; ++i)
|
||||
{
|
||||
string strValue = lValues[i];
|
||||
if(string.IsNullOrEmpty(strValue)) continue;
|
||||
|
||||
str = str.Replace(strValue, new string(lCodes[i], 1));
|
||||
}
|
||||
|
||||
StringBuilder sbFmt = new StringBuilder();
|
||||
bool bInLiteral = false;
|
||||
foreach(char ch in str)
|
||||
{
|
||||
int iCode = lCodes.IndexOf(ch);
|
||||
|
||||
// The escape character doesn't work correctly (e.g.
|
||||
// "dd\\.MM\\.yyyy\\ HH\\:mm\\:ss" doesn't work, but
|
||||
// "dd'.'MM'.'yyyy' 'HH':'mm':'ss" does); use '' instead
|
||||
|
||||
// if(iCode >= 0) sbFmt.Append(vPlh[iCode]);
|
||||
// else // Literal
|
||||
// {
|
||||
// sbFmt.Append('\\');
|
||||
// sbFmt.Append(ch);
|
||||
// }
|
||||
|
||||
if(iCode >= 0)
|
||||
{
|
||||
if(bInLiteral) { sbFmt.Append('\''); bInLiteral = false; }
|
||||
sbFmt.Append(vPlh[iCode]);
|
||||
}
|
||||
else // Literal
|
||||
{
|
||||
if(!bInLiteral) { sbFmt.Append('\''); bInLiteral = true; }
|
||||
sbFmt.Append(ch);
|
||||
}
|
||||
}
|
||||
if(bInLiteral) sbFmt.Append('\'');
|
||||
|
||||
return sbFmt.ToString();
|
||||
}
|
||||
#endif
|
||||
|
||||
public static string SerializeUtc(DateTime dt)
|
||||
{
|
||||
Debug.Assert(dt.Kind != DateTimeKind.Unspecified);
|
||||
|
||||
string str = ToUtc(dt, false).ToString("s");
|
||||
if(!str.EndsWith("Z")) str += "Z";
|
||||
return str;
|
||||
}
|
||||
|
||||
public static bool TryDeserializeUtc(string str, out DateTime dt)
|
||||
{
|
||||
if(str == null) throw new ArgumentNullException("str");
|
||||
|
||||
if(str.EndsWith("Z")) str = str.Substring(0, str.Length - 1);
|
||||
|
||||
bool bResult = StrUtil.TryParseDateTime(str, out dt);
|
||||
if(bResult) dt = ToUtc(dt, true);
|
||||
return bResult;
|
||||
}
|
||||
|
||||
public static double SerializeUnix(DateTime dt)
|
||||
{
|
||||
return (ToUtc(dt, false) - TimeUtil.UnixRoot).TotalSeconds;
|
||||
}
|
||||
|
||||
public static DateTime ConvertUnixTime(double dtUnix)
|
||||
{
|
||||
try { return TimeUtil.UnixRoot.AddSeconds(dtUnix); }
|
||||
catch(Exception) { Debug.Assert(false); }
|
||||
|
||||
return DateTime.UtcNow;
|
||||
}
|
||||
|
||||
#if !KeePassLibSD
|
||||
[Obsolete]
|
||||
public static DateTime? ParseUSTextDate(string strDate)
|
||||
{
|
||||
return ParseUSTextDate(strDate, DateTimeKind.Unspecified);
|
||||
}
|
||||
|
||||
private static string[] m_vUSMonths = null;
|
||||
/// <summary>
|
||||
/// Parse a US textual date string, like e.g. "January 02, 2012".
|
||||
/// </summary>
|
||||
public static DateTime? ParseUSTextDate(string strDate, DateTimeKind k)
|
||||
{
|
||||
if(strDate == null) { Debug.Assert(false); return null; }
|
||||
|
||||
if(m_vUSMonths == null)
|
||||
m_vUSMonths = new string[] { "January", "February", "March",
|
||||
"April", "May", "June", "July", "August", "September",
|
||||
"October", "November", "December" };
|
||||
|
||||
string str = strDate.Trim();
|
||||
for(int i = 0; i < m_vUSMonths.Length; ++i)
|
||||
{
|
||||
if(str.StartsWith(m_vUSMonths[i], StrUtil.CaseIgnoreCmp))
|
||||
{
|
||||
str = str.Substring(m_vUSMonths[i].Length);
|
||||
string[] v = str.Split(new char[] { ',', ';' });
|
||||
if((v == null) || (v.Length != 2)) return null;
|
||||
|
||||
string strDay = v[0].Trim().TrimStart('0');
|
||||
int iDay, iYear;
|
||||
if(int.TryParse(strDay, out iDay) &&
|
||||
int.TryParse(v[1].Trim(), out iYear))
|
||||
return new DateTime(iYear, i + 1, iDay, 0, 0, 0, k);
|
||||
else { Debug.Assert(false); return null; }
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
#endif
|
||||
|
||||
private static readonly DateTime m_dtInvMin =
|
||||
new DateTime(2999, 12, 27, 23, 59, 59, DateTimeKind.Utc);
|
||||
private static readonly DateTime m_dtInvMax =
|
||||
new DateTime(2999, 12, 29, 23, 59, 59, DateTimeKind.Utc);
|
||||
public static int Compare(DateTime dtA, DateTime dtB, bool bUnkIsPast)
|
||||
{
|
||||
Debug.Assert(dtA.Kind == dtB.Kind);
|
||||
|
||||
if(bUnkIsPast)
|
||||
{
|
||||
// 2999-12-28 23:59:59 in KeePass 1.x means 'unknown';
|
||||
// expect time zone corruption (twice)
|
||||
// bool bInvA = ((dtA.Year == 2999) && (dtA.Month == 12) &&
|
||||
// (dtA.Day >= 27) && (dtA.Day <= 29) && (dtA.Minute == 59) &&
|
||||
// (dtA.Second == 59));
|
||||
// bool bInvB = ((dtB.Year == 2999) && (dtB.Month == 12) &&
|
||||
// (dtB.Day >= 27) && (dtB.Day <= 29) && (dtB.Minute == 59) &&
|
||||
// (dtB.Second == 59));
|
||||
// Faster due to internal implementation of DateTime:
|
||||
bool bInvA = ((dtA >= m_dtInvMin) && (dtA <= m_dtInvMax) &&
|
||||
(dtA.Minute == 59) && (dtA.Second == 59));
|
||||
bool bInvB = ((dtB >= m_dtInvMin) && (dtB <= m_dtInvMax) &&
|
||||
(dtB.Minute == 59) && (dtB.Second == 59));
|
||||
|
||||
if(bInvA) return (bInvB ? 0 : -1);
|
||||
if(bInvB) return 1;
|
||||
}
|
||||
|
||||
return dtA.CompareTo(dtB);
|
||||
}
|
||||
|
||||
internal static int CompareLastMod(ITimeLogger tlA, ITimeLogger tlB,
|
||||
bool bUnkIsPast)
|
||||
{
|
||||
if(tlA == null) { Debug.Assert(false); return ((tlB == null) ? 0 : -1); }
|
||||
if(tlB == null) { Debug.Assert(false); return 1; }
|
||||
|
||||
return Compare(tlA.LastModificationTime, tlB.LastModificationTime,
|
||||
bUnkIsPast);
|
||||
}
|
||||
|
||||
public static DateTime ToUtc(DateTime dt, bool bUnspecifiedIsUtc)
|
||||
{
|
||||
DateTimeKind k = dt.Kind;
|
||||
if(k == DateTimeKind.Utc) return dt;
|
||||
if(k == DateTimeKind.Local) return dt.ToUniversalTime();
|
||||
|
||||
Debug.Assert(k == DateTimeKind.Unspecified);
|
||||
if(bUnspecifiedIsUtc)
|
||||
return new DateTime(dt.Ticks, DateTimeKind.Utc);
|
||||
return dt.ToUniversalTime(); // Unspecified = local
|
||||
}
|
||||
|
||||
public static DateTime ToLocal(DateTime dt, bool bUnspecifiedIsLocal)
|
||||
{
|
||||
DateTimeKind k = dt.Kind;
|
||||
if(k == DateTimeKind.Local) return dt;
|
||||
if(k == DateTimeKind.Utc) return dt.ToLocalTime();
|
||||
|
||||
Debug.Assert(k == DateTimeKind.Unspecified);
|
||||
if(bUnspecifiedIsLocal)
|
||||
return new DateTime(dt.Ticks, DateTimeKind.Local);
|
||||
return dt.ToLocalTime(); // Unspecified = UTC
|
||||
}
|
||||
|
||||
/* internal static DateTime RoundToMultOf2PowLess1s(DateTime dt)
|
||||
{
|
||||
long l2Pow = m_lTicks2PowLess1s;
|
||||
if(l2Pow == 0)
|
||||
{
|
||||
l2Pow = 1;
|
||||
while(true)
|
||||
{
|
||||
l2Pow <<= 1;
|
||||
if(l2Pow >= TimeSpan.TicksPerSecond) break;
|
||||
}
|
||||
l2Pow >>= 1;
|
||||
m_lTicks2PowLess1s = l2Pow;
|
||||
|
||||
Debug.Assert(TimeSpan.TicksPerSecond == 10000000L); // .NET
|
||||
Debug.Assert(l2Pow == (1L << 23)); // .NET
|
||||
}
|
||||
|
||||
long l = dt.Ticks;
|
||||
if((l % l2Pow) == 0L) return dt;
|
||||
|
||||
// Round down to full second
|
||||
l /= TimeSpan.TicksPerSecond;
|
||||
l *= TimeSpan.TicksPerSecond;
|
||||
|
||||
// Round up to multiple of l2Pow
|
||||
long l2PowM1 = l2Pow - 1L;
|
||||
l = (l + l2PowM1) & ~l2PowM1;
|
||||
DateTime dtRnd = new DateTime(l, dt.Kind);
|
||||
|
||||
Debug.Assert((dtRnd.Ticks % l2Pow) == 0L);
|
||||
Debug.Assert(dtRnd.ToString("u") == dt.ToString("u"));
|
||||
return dtRnd;
|
||||
} */
|
||||
}
|
||||
}
|
65
ModernKeePassLib/Utility/TypeOverridePool.cs
Normal file
65
ModernKeePassLib/Utility/TypeOverridePool.cs
Normal file
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
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.Text;
|
||||
|
||||
using ModernKeePassLib.Delegates;
|
||||
|
||||
namespace KeePassLib.Utility
|
||||
{
|
||||
public static class TypeOverridePool
|
||||
{
|
||||
private static Dictionary<Type, GFunc<object>> g_d =
|
||||
new Dictionary<Type, GFunc<object>>();
|
||||
|
||||
public static void Register(Type t, GFunc<object> f)
|
||||
{
|
||||
if(t == null) throw new ArgumentNullException("t");
|
||||
if(f == null) throw new ArgumentNullException("f");
|
||||
|
||||
g_d[t] = f;
|
||||
}
|
||||
|
||||
public static void Unregister(Type t)
|
||||
{
|
||||
if(t == null) throw new ArgumentNullException("t");
|
||||
|
||||
g_d.Remove(t);
|
||||
}
|
||||
|
||||
public static bool IsRegistered(Type t)
|
||||
{
|
||||
if(t == null) throw new ArgumentNullException("t");
|
||||
|
||||
return g_d.ContainsKey(t);
|
||||
}
|
||||
|
||||
public static T CreateInstance<T>()
|
||||
where T : new()
|
||||
{
|
||||
GFunc<object> f;
|
||||
if(g_d.TryGetValue(typeof(T), out f))
|
||||
return (T)(f());
|
||||
|
||||
return new T();
|
||||
}
|
||||
}
|
||||
}
|
787
ModernKeePassLib/Utility/UrlUtil.cs
Normal file
787
ModernKeePassLib/Utility/UrlUtil.cs
Normal file
@@ -0,0 +1,787 @@
|
||||
/*
|
||||
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.Globalization;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
#if ModernKeePassLib
|
||||
using Windows.Storage;
|
||||
#endif
|
||||
|
||||
using ModernKeePassLib.Native;
|
||||
|
||||
namespace ModernKeePassLib.Utility
|
||||
{
|
||||
/// <summary>
|
||||
/// A class containing various static path utility helper methods (like
|
||||
/// stripping extension from a file, etc.).
|
||||
/// </summary>
|
||||
public static class UrlUtil
|
||||
{
|
||||
private static readonly char[] m_vDirSeps = new char[] {
|
||||
'\\', '/', UrlUtil.LocalDirSepChar };
|
||||
#if !ModernKeePassLib
|
||||
private static readonly char[] m_vPathTrimCharsWs = new char[] {
|
||||
'\"', ' ', '\t', '\r', '\n' };
|
||||
#endif
|
||||
|
||||
public static char LocalDirSepChar
|
||||
{
|
||||
#if KeePassRT
|
||||
get { return '\\'; }
|
||||
#elif ModernKeePassLib
|
||||
//get { return PortablePath.DirectorySeparatorChar; }
|
||||
get { return '\\'; }
|
||||
#else
|
||||
get { return Path.DirectorySeparatorChar; }
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the directory (path) of a file name. The returned string may be
|
||||
/// terminated by a directory separator character. Example:
|
||||
/// passing <c>C:\\My Documents\\My File.kdb</c> in <paramref name="strFile" />
|
||||
/// and <c>true</c> to <paramref name="bAppendTerminatingChar"/>
|
||||
/// would produce this string: <c>C:\\My Documents\\</c>.
|
||||
/// </summary>
|
||||
/// <param name="strFile">Full path of a file.</param>
|
||||
/// <param name="bAppendTerminatingChar">Append a terminating directory separator
|
||||
/// character to the returned path.</param>
|
||||
/// <param name="bEnsureValidDirSpec">If <c>true</c>, the returned path
|
||||
/// is guaranteed to be a valid directory path (for example <c>X:\\</c> instead
|
||||
/// of <c>X:</c>, overriding <paramref name="bAppendTerminatingChar" />).
|
||||
/// This should only be set to <c>true</c>, if the returned path is directly
|
||||
/// passed to some directory API.</param>
|
||||
/// <returns>Directory of the file.</returns>
|
||||
public static string GetFileDirectory(string strFile, bool bAppendTerminatingChar,
|
||||
bool bEnsureValidDirSpec)
|
||||
{
|
||||
Debug.Assert(strFile != null);
|
||||
if(strFile == null) throw new ArgumentNullException("strFile");
|
||||
|
||||
int nLastSep = strFile.LastIndexOfAny(m_vDirSeps);
|
||||
if(nLastSep < 0) return string.Empty; // No directory
|
||||
|
||||
if(bEnsureValidDirSpec && (nLastSep == 2) && (strFile[1] == ':') &&
|
||||
(strFile[2] == '\\')) // Length >= 3 and Windows root directory
|
||||
bAppendTerminatingChar = true;
|
||||
|
||||
if(!bAppendTerminatingChar) return strFile.Substring(0, nLastSep);
|
||||
return EnsureTerminatingSeparator(strFile.Substring(0, nLastSep),
|
||||
(strFile[nLastSep] == '/'));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the file name of the specified file (full path). Example:
|
||||
/// if <paramref name="strPath" /> is <c>C:\\My Documents\\My File.kdb</c>
|
||||
/// the returned string is <c>My File.kdb</c>.
|
||||
/// </summary>
|
||||
/// <param name="strPath">Full path of a file.</param>
|
||||
/// <returns>File name of the specified file. The return value is
|
||||
/// an empty string (<c>""</c>) if the input parameter is <c>null</c>.</returns>
|
||||
public static string GetFileName(string strPath)
|
||||
{
|
||||
Debug.Assert(strPath != null); if(strPath == null) throw new ArgumentNullException("strPath");
|
||||
|
||||
int nLastSep = strPath.LastIndexOfAny(m_vDirSeps);
|
||||
|
||||
if(nLastSep < 0) return strPath;
|
||||
if(nLastSep >= (strPath.Length - 1)) return string.Empty;
|
||||
|
||||
return strPath.Substring(nLastSep + 1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Strip the extension of a file.
|
||||
/// </summary>
|
||||
/// <param name="strPath">Full path of a file with extension.</param>
|
||||
/// <returns>File name without extension.</returns>
|
||||
public static string StripExtension(string strPath)
|
||||
{
|
||||
Debug.Assert(strPath != null); if(strPath == null) throw new ArgumentNullException("strPath");
|
||||
|
||||
int nLastDirSep = strPath.LastIndexOfAny(m_vDirSeps);
|
||||
int nLastExtDot = strPath.LastIndexOf('.');
|
||||
|
||||
if(nLastExtDot <= nLastDirSep) return strPath;
|
||||
|
||||
return strPath.Substring(0, nLastExtDot);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the extension of a file.
|
||||
/// </summary>
|
||||
/// <param name="strPath">Full path of a file with extension.</param>
|
||||
/// <returns>Extension without prepending dot.</returns>
|
||||
public static string GetExtension(string strPath)
|
||||
{
|
||||
Debug.Assert(strPath != null); if(strPath == null) throw new ArgumentNullException("strPath");
|
||||
|
||||
int nLastDirSep = strPath.LastIndexOfAny(m_vDirSeps);
|
||||
int nLastExtDot = strPath.LastIndexOf('.');
|
||||
|
||||
if(nLastExtDot <= nLastDirSep) return string.Empty;
|
||||
if(nLastExtDot == (strPath.Length - 1)) return string.Empty;
|
||||
|
||||
return strPath.Substring(nLastExtDot + 1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ensure that a path is terminated with a directory separator character.
|
||||
/// </summary>
|
||||
/// <param name="strPath">Input path.</param>
|
||||
/// <param name="bUrl">If <c>true</c>, a slash (<c>/</c>) is appended to
|
||||
/// the string if it's not terminated already. If <c>false</c>, the
|
||||
/// default system directory separator character is used.</param>
|
||||
/// <returns>Path having a directory separator as last character.</returns>
|
||||
public static string EnsureTerminatingSeparator(string strPath, bool bUrl)
|
||||
{
|
||||
Debug.Assert(strPath != null); if(strPath == null) throw new ArgumentNullException("strPath");
|
||||
|
||||
int nLength = strPath.Length;
|
||||
if(nLength <= 0) return string.Empty;
|
||||
|
||||
char chLast = strPath[nLength - 1];
|
||||
|
||||
for(int i = 0; i < m_vDirSeps.Length; ++i)
|
||||
{
|
||||
if(chLast == m_vDirSeps[i]) return strPath;
|
||||
}
|
||||
|
||||
if(bUrl) return (strPath + '/');
|
||||
return (strPath + UrlUtil.LocalDirSepChar);
|
||||
}
|
||||
|
||||
/* /// <summary>
|
||||
/// File access mode enumeration. Used by the <c>FileAccessible</c>
|
||||
/// method.
|
||||
/// </summary>
|
||||
public enum FileAccessMode
|
||||
{
|
||||
/// <summary>
|
||||
/// Opening a file in read mode. The specified file must exist.
|
||||
/// </summary>
|
||||
Read = 0,
|
||||
|
||||
/// <summary>
|
||||
/// Opening a file in create mode. If the file exists already, it
|
||||
/// will be overwritten. If it doesn't exist, it will be created.
|
||||
/// The return value is <c>true</c>, if data can be written to the
|
||||
/// file.
|
||||
/// </summary>
|
||||
Create
|
||||
} */
|
||||
|
||||
/* /// <summary>
|
||||
/// Test if a specified path is accessible, either in read or write mode.
|
||||
/// </summary>
|
||||
/// <param name="strFilePath">Path to test.</param>
|
||||
/// <param name="fMode">Requested file access mode.</param>
|
||||
/// <returns>Returns <c>true</c> if the specified path is accessible in
|
||||
/// the requested mode, otherwise the return value is <c>false</c>.</returns>
|
||||
public static bool FileAccessible(string strFilePath, FileAccessMode fMode)
|
||||
{
|
||||
Debug.Assert(strFilePath != null);
|
||||
if(strFilePath == null) throw new ArgumentNullException("strFilePath");
|
||||
|
||||
if(fMode == FileAccessMode.Read)
|
||||
{
|
||||
FileStream fs;
|
||||
|
||||
try { fs = File.OpenRead(strFilePath); }
|
||||
catch(Exception) { return false; }
|
||||
if(fs == null) return false;
|
||||
|
||||
fs.Close();
|
||||
return true;
|
||||
}
|
||||
else if(fMode == FileAccessMode.Create)
|
||||
{
|
||||
FileStream fs;
|
||||
|
||||
try { fs = File.Create(strFilePath); }
|
||||
catch(Exception) { return false; }
|
||||
if(fs == null) return false;
|
||||
|
||||
fs.Close();
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
} */
|
||||
|
||||
public static string GetQuotedAppPath(string strPath)
|
||||
{
|
||||
if(strPath == null) { Debug.Assert(false); return string.Empty; }
|
||||
|
||||
// int nFirst = strPath.IndexOf('\"');
|
||||
// int nSecond = strPath.IndexOf('\"', nFirst + 1);
|
||||
// if((nFirst >= 0) && (nSecond >= 0))
|
||||
// return strPath.Substring(nFirst + 1, nSecond - nFirst - 1);
|
||||
// return strPath;
|
||||
|
||||
string str = strPath.Trim();
|
||||
if(str.Length <= 1) return str;
|
||||
if(str[0] != '\"') return str;
|
||||
|
||||
int iSecond = str.IndexOf('\"', 1);
|
||||
if(iSecond <= 0) return str;
|
||||
|
||||
return str.Substring(1, iSecond - 1);
|
||||
}
|
||||
|
||||
public static string FileUrlToPath(string strUrl)
|
||||
{
|
||||
Debug.Assert(strUrl != null);
|
||||
if(strUrl == null) throw new ArgumentNullException("strUrl");
|
||||
|
||||
string str = strUrl;
|
||||
if(str.StartsWith(@"file:///", StrUtil.CaseIgnoreCmp))
|
||||
str = str.Substring(8, str.Length - 8);
|
||||
|
||||
str = str.Replace('/', UrlUtil.LocalDirSepChar);
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
public static bool UnhideFile(string strFile)
|
||||
{
|
||||
#if (ModernKeePassLib || KeePassLibSD || KeePassRT)
|
||||
return false;
|
||||
#else
|
||||
if(strFile == null) throw new ArgumentNullException("strFile");
|
||||
|
||||
try
|
||||
{
|
||||
FileAttributes fa = File.GetAttributes(strFile);
|
||||
if((long)(fa & FileAttributes.Hidden) == 0) return false;
|
||||
|
||||
return HideFile(strFile, false);
|
||||
}
|
||||
catch(Exception) { }
|
||||
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
public static bool HideFile(string strFile, bool bHide)
|
||||
{
|
||||
#if (ModernKeePassLib || KeePassLibSD || KeePassRT)
|
||||
return false;
|
||||
#else
|
||||
if(strFile == null) throw new ArgumentNullException("strFile");
|
||||
|
||||
try
|
||||
{
|
||||
FileAttributes fa = File.GetAttributes(strFile);
|
||||
|
||||
if(bHide) fa = ((fa & ~FileAttributes.Normal) | FileAttributes.Hidden);
|
||||
else // Unhide
|
||||
{
|
||||
fa &= ~FileAttributes.Hidden;
|
||||
if((long)fa == 0) fa = FileAttributes.Normal;
|
||||
}
|
||||
|
||||
File.SetAttributes(strFile, fa);
|
||||
return true;
|
||||
}
|
||||
catch(Exception) { }
|
||||
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
public static string MakeRelativePath(string strBaseFile, string strTargetFile)
|
||||
{
|
||||
if(strBaseFile == null) throw new ArgumentNullException("strBasePath");
|
||||
if(strTargetFile == null) throw new ArgumentNullException("strTargetPath");
|
||||
if(strBaseFile.Length == 0) return strTargetFile;
|
||||
if(strTargetFile.Length == 0) return string.Empty;
|
||||
|
||||
// Test whether on different Windows drives
|
||||
if((strBaseFile.Length >= 3) && (strTargetFile.Length >= 3))
|
||||
{
|
||||
if((strBaseFile[1] == ':') && (strTargetFile[1] == ':') &&
|
||||
(strBaseFile[2] == '\\') && (strTargetFile[2] == '\\') &&
|
||||
(strBaseFile[0] != strTargetFile[0]))
|
||||
return strTargetFile;
|
||||
}
|
||||
|
||||
#if (!KeePassLibSD && !KeePassUAP && !ModernKeePassLib)
|
||||
if(NativeLib.IsUnix())
|
||||
{
|
||||
#endif
|
||||
bool bBaseUnc = IsUncPath(strBaseFile);
|
||||
bool bTargetUnc = IsUncPath(strTargetFile);
|
||||
if((!bBaseUnc && bTargetUnc) || (bBaseUnc && !bTargetUnc))
|
||||
return strTargetFile;
|
||||
|
||||
string strBase = GetShortestAbsolutePath(strBaseFile);
|
||||
string strTarget = GetShortestAbsolutePath(strTargetFile);
|
||||
string[] vBase = strBase.Split(m_vDirSeps);
|
||||
string[] vTarget = strTarget.Split(m_vDirSeps);
|
||||
|
||||
int i = 0;
|
||||
while((i < (vBase.Length - 1)) && (i < (vTarget.Length - 1)) &&
|
||||
(vBase[i] == vTarget[i])) { ++i; }
|
||||
|
||||
StringBuilder sbRel = new StringBuilder();
|
||||
for(int j = i; j < (vBase.Length - 1); ++j)
|
||||
{
|
||||
if(sbRel.Length > 0) sbRel.Append(UrlUtil.LocalDirSepChar);
|
||||
sbRel.Append("..");
|
||||
}
|
||||
for(int k = i; k < vTarget.Length; ++k)
|
||||
{
|
||||
if(sbRel.Length > 0) sbRel.Append(UrlUtil.LocalDirSepChar);
|
||||
sbRel.Append(vTarget[k]);
|
||||
}
|
||||
|
||||
return sbRel.ToString();
|
||||
#if (!KeePassLibSD && !KeePassUAP && !ModernKeePassLib)
|
||||
}
|
||||
|
||||
try // Windows
|
||||
{
|
||||
const int nMaxPath = NativeMethods.MAX_PATH * 2;
|
||||
StringBuilder sb = new StringBuilder(nMaxPath + 2);
|
||||
if(!NativeMethods.PathRelativePathTo(sb, strBaseFile, 0,
|
||||
strTargetFile, 0))
|
||||
return strTargetFile;
|
||||
|
||||
string str = sb.ToString();
|
||||
while(str.StartsWith(".\\")) str = str.Substring(2, str.Length - 2);
|
||||
|
||||
return str;
|
||||
}
|
||||
catch(Exception) { Debug.Assert(false); }
|
||||
return strTargetFile;
|
||||
#endif
|
||||
}
|
||||
|
||||
public static string MakeAbsolutePath(string strBaseFile, string strTargetFile)
|
||||
{
|
||||
if(strBaseFile == null) throw new ArgumentNullException("strBasePath");
|
||||
if(strTargetFile == null) throw new ArgumentNullException("strTargetPath");
|
||||
if(strBaseFile.Length == 0) return strTargetFile;
|
||||
if(strTargetFile.Length == 0) return string.Empty;
|
||||
|
||||
if(IsAbsolutePath(strTargetFile)) return strTargetFile;
|
||||
|
||||
string strBaseDir = GetFileDirectory(strBaseFile, true, false);
|
||||
return GetShortestAbsolutePath(strBaseDir + strTargetFile);
|
||||
}
|
||||
|
||||
public static bool IsAbsolutePath(string strPath)
|
||||
{
|
||||
if(strPath == null) throw new ArgumentNullException("strPath");
|
||||
if(strPath.Length == 0) return false;
|
||||
|
||||
if(IsUncPath(strPath)) return true;
|
||||
|
||||
try { return Path.IsPathRooted(strPath); }
|
||||
catch(Exception) { Debug.Assert(false); }
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static string GetShortestAbsolutePath(string strPath)
|
||||
{
|
||||
if(strPath == null) throw new ArgumentNullException("strPath");
|
||||
if(strPath.Length == 0) return string.Empty;
|
||||
|
||||
// Path.GetFullPath is incompatible with UNC paths traversing over
|
||||
// different server shares (which are created by PathRelativePathTo);
|
||||
// we need to build the absolute path on our own...
|
||||
if(IsUncPath(strPath))
|
||||
{
|
||||
char chSep = strPath[0];
|
||||
Debug.Assert(Array.IndexOf<char>(m_vDirSeps, chSep) >= 0);
|
||||
|
||||
List<string> l = new List<string>();
|
||||
#if !KeePassLibSD
|
||||
string[] v = strPath.Split(m_vDirSeps, StringSplitOptions.None);
|
||||
#else
|
||||
string[] v = strPath.Split(m_vDirSeps);
|
||||
#endif
|
||||
Debug.Assert((v.Length >= 3) && (v[0].Length == 0) &&
|
||||
(v[1].Length == 0));
|
||||
|
||||
foreach(string strPart in v)
|
||||
{
|
||||
if(strPart.Equals(".")) continue;
|
||||
else if(strPart.Equals(".."))
|
||||
{
|
||||
if(l.Count > 0) l.RemoveAt(l.Count - 1);
|
||||
else { Debug.Assert(false); }
|
||||
}
|
||||
else l.Add(strPart); // Do not ignore zero length parts
|
||||
}
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for(int i = 0; i < l.Count; ++i)
|
||||
{
|
||||
// Don't test length of sb, might be 0 due to initial UNC seps
|
||||
if(i > 0) sb.Append(chSep);
|
||||
|
||||
sb.Append(l[i]);
|
||||
}
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
string str;
|
||||
try
|
||||
{
|
||||
#if ModernKeePassLib
|
||||
var dirT = StorageFolder.GetFolderFromPathAsync(
|
||||
strPath).GetResults();
|
||||
str = dirT.Path;
|
||||
#else
|
||||
str = Path.GetFullPath(strPath);
|
||||
#endif
|
||||
}
|
||||
catch(Exception) { Debug.Assert(false); return strPath; }
|
||||
|
||||
Debug.Assert(str.IndexOf("\\..\\") < 0);
|
||||
foreach(char ch in m_vDirSeps)
|
||||
{
|
||||
string strSep = new string(ch, 1);
|
||||
str = str.Replace(strSep + "." + strSep, strSep);
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
public static int GetUrlLength(string strText, int nOffset)
|
||||
{
|
||||
if(strText == null) throw new ArgumentNullException("strText");
|
||||
if(nOffset > strText.Length) throw new ArgumentException(); // Not >= (0 len)
|
||||
|
||||
int iPosition = nOffset, nLength = 0, nStrLen = strText.Length;
|
||||
|
||||
while(iPosition < nStrLen)
|
||||
{
|
||||
char ch = strText[iPosition];
|
||||
++iPosition;
|
||||
|
||||
if((ch == ' ') || (ch == '\t') || (ch == '\r') || (ch == '\n'))
|
||||
break;
|
||||
|
||||
++nLength;
|
||||
}
|
||||
|
||||
return nLength;
|
||||
}
|
||||
|
||||
public static string RemoveScheme(string strUrl)
|
||||
{
|
||||
if(string.IsNullOrEmpty(strUrl)) return string.Empty;
|
||||
|
||||
int nNetScheme = strUrl.IndexOf(@"://", StrUtil.CaseIgnoreCmp);
|
||||
int nShScheme = strUrl.IndexOf(@":/", StrUtil.CaseIgnoreCmp);
|
||||
int nSmpScheme = strUrl.IndexOf(@":", StrUtil.CaseIgnoreCmp);
|
||||
|
||||
if((nNetScheme < 0) && (nShScheme < 0) && (nSmpScheme < 0))
|
||||
return strUrl; // No scheme
|
||||
|
||||
int nMin = Math.Min(Math.Min((nNetScheme >= 0) ? nNetScheme : int.MaxValue,
|
||||
(nShScheme >= 0) ? nShScheme : int.MaxValue),
|
||||
(nSmpScheme >= 0) ? nSmpScheme : int.MaxValue);
|
||||
|
||||
if(nMin == nNetScheme) return strUrl.Substring(nMin + 3);
|
||||
if(nMin == nShScheme) return strUrl.Substring(nMin + 2);
|
||||
return strUrl.Substring(nMin + 1);
|
||||
}
|
||||
|
||||
public static string ConvertSeparators(string strPath)
|
||||
{
|
||||
return ConvertSeparators(strPath, UrlUtil.LocalDirSepChar);
|
||||
}
|
||||
|
||||
public static string ConvertSeparators(string strPath, char chSeparator)
|
||||
{
|
||||
if(string.IsNullOrEmpty(strPath)) return string.Empty;
|
||||
|
||||
strPath = strPath.Replace('/', chSeparator);
|
||||
strPath = strPath.Replace('\\', chSeparator);
|
||||
|
||||
return strPath;
|
||||
}
|
||||
|
||||
public static bool IsUncPath(string strPath)
|
||||
{
|
||||
if(strPath == null) throw new ArgumentNullException("strPath");
|
||||
|
||||
return (strPath.StartsWith("\\\\") || strPath.StartsWith("//"));
|
||||
}
|
||||
|
||||
public static string FilterFileName(string strName)
|
||||
{
|
||||
if(strName == null) { Debug.Assert(false); return string.Empty; }
|
||||
|
||||
string str = strName;
|
||||
|
||||
str = str.Replace('/', '-');
|
||||
str = str.Replace('\\', '-');
|
||||
str = str.Replace(":", string.Empty);
|
||||
str = str.Replace("*", string.Empty);
|
||||
str = str.Replace("?", string.Empty);
|
||||
str = str.Replace("\"", string.Empty);
|
||||
str = str.Replace(@"'", string.Empty);
|
||||
str = str.Replace('<', '(');
|
||||
str = str.Replace('>', ')');
|
||||
str = str.Replace('|', '-');
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the host component of an URL.
|
||||
/// This method is faster and more fault-tolerant than creating
|
||||
/// an <code>Uri</code> object and querying its <code>Host</code>
|
||||
/// property.
|
||||
/// </summary>
|
||||
/// <example>
|
||||
/// For the input <code>s://u:p@d.tld:p/p?q#f</code> the return
|
||||
/// value is <code>d.tld</code>.
|
||||
/// </example>
|
||||
public static string GetHost(string strUrl)
|
||||
{
|
||||
if(strUrl == null) { Debug.Assert(false); return string.Empty; }
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
bool bInExtHost = false;
|
||||
for(int i = 0; i < strUrl.Length; ++i)
|
||||
{
|
||||
char ch = strUrl[i];
|
||||
if(bInExtHost)
|
||||
{
|
||||
if(ch == '/')
|
||||
{
|
||||
if(sb.Length == 0) { } // Ignore leading '/'s
|
||||
else break;
|
||||
}
|
||||
else sb.Append(ch);
|
||||
}
|
||||
else // !bInExtHost
|
||||
{
|
||||
if(ch == ':') bInExtHost = true;
|
||||
}
|
||||
}
|
||||
|
||||
string str = sb.ToString();
|
||||
if(str.Length == 0) str = strUrl;
|
||||
|
||||
// Remove the login part
|
||||
int nLoginLen = str.IndexOf('@');
|
||||
if(nLoginLen >= 0) str = str.Substring(nLoginLen + 1);
|
||||
|
||||
// Remove the port
|
||||
int iPort = str.LastIndexOf(':');
|
||||
if(iPort >= 0) str = str.Substring(0, iPort);
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
public static bool AssemblyEquals(string strExt, string strShort)
|
||||
{
|
||||
if((strExt == null) || (strShort == null)) { Debug.Assert(false); return false; }
|
||||
|
||||
if(strExt.Equals(strShort, StrUtil.CaseIgnoreCmp) ||
|
||||
strExt.StartsWith(strShort + ",", StrUtil.CaseIgnoreCmp))
|
||||
return true;
|
||||
|
||||
if(!strShort.EndsWith(".dll", StrUtil.CaseIgnoreCmp))
|
||||
{
|
||||
if(strExt.Equals(strShort + ".dll", StrUtil.CaseIgnoreCmp) ||
|
||||
strExt.StartsWith(strShort + ".dll,", StrUtil.CaseIgnoreCmp))
|
||||
return true;
|
||||
}
|
||||
|
||||
if(!strShort.EndsWith(".exe", StrUtil.CaseIgnoreCmp))
|
||||
{
|
||||
if(strExt.Equals(strShort + ".exe", StrUtil.CaseIgnoreCmp) ||
|
||||
strExt.StartsWith(strShort + ".exe,", StrUtil.CaseIgnoreCmp))
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static string GetTempPath()
|
||||
{
|
||||
string strDir;
|
||||
if(NativeLib.IsUnix())
|
||||
strDir = NativeMethods.GetUserRuntimeDir();
|
||||
#if KeePassUAP || ModernKeePassLib
|
||||
else strDir = Windows.Storage.ApplicationData.Current.TemporaryFolder.Path;
|
||||
#else
|
||||
else strDir = Path.GetTempPath();
|
||||
|
||||
try
|
||||
{
|
||||
if(!Directory.Exists(strDir)) Directory.CreateDirectory(strDir);
|
||||
}
|
||||
catch(Exception) { Debug.Assert(false); }
|
||||
|
||||
#endif
|
||||
return strDir;
|
||||
}
|
||||
|
||||
#if !ModernKeePassLib && !KeePassLibSD
|
||||
// Structurally mostly equivalent to UrlUtil.GetFileInfos
|
||||
public static List<string> GetFilePaths(string strDir, string strPattern,
|
||||
SearchOption opt)
|
||||
{
|
||||
List<string> l = new List<string>();
|
||||
if(strDir == null) { Debug.Assert(false); return l; }
|
||||
if(strPattern == null) { Debug.Assert(false); return l; }
|
||||
|
||||
string[] v = Directory.GetFiles(strDir, strPattern, opt);
|
||||
if(v == null) { Debug.Assert(false); return l; }
|
||||
|
||||
// Only accept files with the correct extension; GetFiles may
|
||||
// return additional files, see GetFiles documentation
|
||||
string strExt = GetExtension(strPattern);
|
||||
if(!string.IsNullOrEmpty(strExt) && (strExt.IndexOf('*') < 0) &&
|
||||
(strExt.IndexOf('?') < 0))
|
||||
{
|
||||
strExt = "." + strExt;
|
||||
|
||||
foreach(string strPathRaw in v)
|
||||
{
|
||||
if(strPathRaw == null) { Debug.Assert(false); continue; }
|
||||
string strPath = strPathRaw.Trim(m_vPathTrimCharsWs);
|
||||
if(strPath.Length == 0) { Debug.Assert(false); continue; }
|
||||
Debug.Assert(strPath == strPathRaw);
|
||||
|
||||
if(strPath.EndsWith(strExt, StrUtil.CaseIgnoreCmp))
|
||||
l.Add(strPathRaw);
|
||||
}
|
||||
}
|
||||
else l.AddRange(v);
|
||||
|
||||
return l;
|
||||
}
|
||||
|
||||
// Structurally mostly equivalent to UrlUtil.GetFilePaths
|
||||
public static List<FileInfo> GetFileInfos(DirectoryInfo di, string strPattern,
|
||||
SearchOption opt)
|
||||
{
|
||||
List<FileInfo> l = new List<FileInfo>();
|
||||
if(di == null) { Debug.Assert(false); return l; }
|
||||
if(strPattern == null) { Debug.Assert(false); return l; }
|
||||
|
||||
FileInfo[] v = di.GetFiles(strPattern, opt);
|
||||
if(v == null) { Debug.Assert(false); return l; }
|
||||
|
||||
// Only accept files with the correct extension; GetFiles may
|
||||
// return additional files, see GetFiles documentation
|
||||
string strExt = GetExtension(strPattern);
|
||||
if(!string.IsNullOrEmpty(strExt) && (strExt.IndexOf('*') < 0) &&
|
||||
(strExt.IndexOf('?') < 0))
|
||||
{
|
||||
strExt = "." + strExt;
|
||||
|
||||
foreach(FileInfo fi in v)
|
||||
{
|
||||
if(fi == null) { Debug.Assert(false); continue; }
|
||||
string strPathRaw = fi.FullName;
|
||||
if(strPathRaw == null) { Debug.Assert(false); continue; }
|
||||
string strPath = strPathRaw.Trim(m_vPathTrimCharsWs);
|
||||
if(strPath.Length == 0) { Debug.Assert(false); continue; }
|
||||
Debug.Assert(strPath == strPathRaw);
|
||||
|
||||
if(strPath.EndsWith(strExt, StrUtil.CaseIgnoreCmp))
|
||||
l.Add(fi);
|
||||
}
|
||||
}
|
||||
else l.AddRange(v);
|
||||
|
||||
return l;
|
||||
}
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// Expand shell variables in a string.
|
||||
/// <paramref name="vParams" />[0] is the value of <c>%1</c>, etc.
|
||||
/// </summary>
|
||||
public static string ExpandShellVariables(string strText, string[] vParams)
|
||||
{
|
||||
if(strText == null) { Debug.Assert(false); return string.Empty; }
|
||||
if(vParams == null) { Debug.Assert(false); vParams = new string[0]; }
|
||||
|
||||
string str = strText;
|
||||
NumberFormatInfo nfi = NumberFormatInfo.InvariantInfo;
|
||||
|
||||
for(int i = 0; i <= 9; ++i)
|
||||
{
|
||||
string strPlh = "%" + i.ToString(nfi);
|
||||
|
||||
string strValue = string.Empty;
|
||||
if((i > 0) && ((i - 1) < vParams.Length))
|
||||
strValue = (vParams[i - 1] ?? string.Empty);
|
||||
|
||||
str = str.Replace(strPlh, strValue);
|
||||
|
||||
if(i == 1)
|
||||
{
|
||||
// %L is replaced by the long version of %1; e.g.
|
||||
// HKEY_CLASSES_ROOT\\IE.AssocFile.URL\\Shell\\Open\\Command
|
||||
str = str.Replace("%L", strValue);
|
||||
str = str.Replace("%l", strValue);
|
||||
}
|
||||
}
|
||||
|
||||
if(str.IndexOf("%*") >= 0)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
foreach(string strValue in vParams)
|
||||
{
|
||||
if(!string.IsNullOrEmpty(strValue))
|
||||
{
|
||||
if(sb.Length > 0) sb.Append(' ');
|
||||
sb.Append(strValue);
|
||||
}
|
||||
}
|
||||
|
||||
str = str.Replace("%*", sb.ToString());
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
public static char GetDriveLetter(string strPath)
|
||||
{
|
||||
if(strPath == null) throw new ArgumentNullException("strPath");
|
||||
|
||||
Debug.Assert(default(char) == '\0');
|
||||
if(strPath.Length < 3) return '\0';
|
||||
if((strPath[1] != ':') || (strPath[2] != '\\')) return '\0';
|
||||
|
||||
char ch = char.ToUpperInvariant(strPath[0]);
|
||||
return (((ch >= 'A') && (ch <= 'Z')) ? ch : '\0');
|
||||
}
|
||||
}
|
||||
}
|
120
ModernKeePassLib/Utility/XmlUtilEx.cs
Normal file
120
ModernKeePassLib/Utility/XmlUtilEx.cs
Normal file
@@ -0,0 +1,120 @@
|
||||
/*
|
||||
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 System.Xml;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
namespace ModernKeePassLib.Utility
|
||||
{
|
||||
public static class XmlUtilEx
|
||||
{
|
||||
public static XmlDocument CreateXmlDocument()
|
||||
{
|
||||
XmlDocument d = new XmlDocument();
|
||||
|
||||
// .NET 4.5.2 and newer do not resolve external XML resources
|
||||
// by default; for older .NET versions, we explicitly
|
||||
// prevent resolving
|
||||
d.XmlResolver = null; // Default in old .NET: XmlUrlResolver object
|
||||
|
||||
return d;
|
||||
}
|
||||
|
||||
public static XmlReaderSettings CreateXmlReaderSettings()
|
||||
{
|
||||
XmlReaderSettings xrs = new XmlReaderSettings();
|
||||
|
||||
xrs.CloseInput = false;
|
||||
xrs.IgnoreComments = true;
|
||||
xrs.IgnoreProcessingInstructions = true;
|
||||
xrs.IgnoreWhitespace = true;
|
||||
|
||||
#if KeePassUAP
|
||||
xrs.DtdProcessing = DtdProcessing.Prohibit;
|
||||
#else
|
||||
// Also see PrepMonoDev.sh script
|
||||
xrs.ProhibitDtd = true; // Obsolete in .NET 4, but still there
|
||||
// xrs.DtdProcessing = DtdProcessing.Prohibit; // .NET 4 only
|
||||
#endif
|
||||
|
||||
xrs.ValidationType = ValidationType.None;
|
||||
xrs.XmlResolver = null;
|
||||
|
||||
return xrs;
|
||||
}
|
||||
|
||||
public static XmlReader CreateXmlReader(Stream s)
|
||||
{
|
||||
if(s == null) { Debug.Assert(false); throw new ArgumentNullException("s"); }
|
||||
|
||||
return XmlReader.Create(s, CreateXmlReaderSettings());
|
||||
}
|
||||
|
||||
public static XmlWriterSettings CreateXmlWriterSettings()
|
||||
{
|
||||
XmlWriterSettings xws = new XmlWriterSettings();
|
||||
|
||||
xws.CloseOutput = false;
|
||||
xws.Encoding = StrUtil.Utf8;
|
||||
xws.Indent = true;
|
||||
xws.IndentChars = "\t";
|
||||
xws.NewLineOnAttributes = false;
|
||||
|
||||
return xws;
|
||||
}
|
||||
|
||||
public static XmlWriter CreateXmlWriter(Stream s)
|
||||
{
|
||||
if(s == null) { Debug.Assert(false); throw new ArgumentNullException("s"); }
|
||||
|
||||
return XmlWriter.Create(s, CreateXmlWriterSettings());
|
||||
}
|
||||
|
||||
public static void Serialize<T>(Stream s, T t)
|
||||
{
|
||||
if(s == null) { Debug.Assert(false); throw new ArgumentNullException("s"); }
|
||||
|
||||
XmlSerializer xs = new XmlSerializer(typeof(T));
|
||||
using(XmlWriter xw = CreateXmlWriter(s))
|
||||
{
|
||||
xs.Serialize(xw, t);
|
||||
}
|
||||
}
|
||||
|
||||
public static T Deserialize<T>(Stream s)
|
||||
{
|
||||
if(s == null) { Debug.Assert(false); throw new ArgumentNullException("s"); }
|
||||
|
||||
XmlSerializer xs = new XmlSerializer(typeof(T));
|
||||
|
||||
T t = default(T);
|
||||
using(XmlReader xr = CreateXmlReader(s))
|
||||
{
|
||||
t = (T)xs.Deserialize(xr);
|
||||
}
|
||||
|
||||
return t;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user