mirror of
https://github.com/wismna/ModernKeePass.git
synced 2025-10-03 23:50:18 -04:00
WIP Update lib to 2.37
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
KeePass Password Safe - The Open-Source Password Manager
|
||||
Copyright (C) 2003-2014 Dominik Reichl <dominik.reichl@t-online.de>
|
||||
Copyright (C) 2003-2017 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
|
||||
@@ -19,9 +19,9 @@
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.IO;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
#if !KeePassLibSD
|
||||
using System.IO.Compression;
|
||||
@@ -38,8 +38,7 @@ namespace ModernKeePassLib.Utility
|
||||
|
||||
public static void Open(string strPrefix)
|
||||
{
|
||||
return; // Logging is not enabled in normal builds of KeePass!
|
||||
|
||||
// Logging is not enabled in normal builds of KeePass!
|
||||
/*
|
||||
AppLogEx.Close();
|
||||
|
||||
@@ -58,7 +57,7 @@ namespace ModernKeePassLib.Utility
|
||||
string strPath = strTemp + strPrefix + "-";
|
||||
Debug.Assert(strPath.IndexOf('/') < 0);
|
||||
|
||||
DateTime dtNow = DateTime.Now;
|
||||
DateTime dtNow = DateTime.UtcNow;
|
||||
string strTime = dtNow.ToString("s");
|
||||
strTime = strTime.Replace('T', '-');
|
||||
strTime = strTime.Replace(':', '-');
|
||||
|
@@ -28,38 +28,79 @@ using System.Drawing;
|
||||
using System.Drawing.Imaging;
|
||||
#endif
|
||||
using System.Diagnostics;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ModernKeePassLib.Utility
|
||||
{
|
||||
public static class GfxUtil
|
||||
{
|
||||
#if KeePassRT
|
||||
public static Image LoadImage(byte[] pb)
|
||||
#if (!KeePassLibSD && !KeePassUAP)
|
||||
private sealed class GfxImage
|
||||
{
|
||||
MemoryStream ms = new MemoryStream(pb, false);
|
||||
try { return Image.FromStream(ms); }
|
||||
finally { ms.Close(); }
|
||||
}
|
||||
#elif ModernKeePassLib
|
||||
public static IBitmap LoadImage(byte[] pb)
|
||||
{
|
||||
using (var ms = new MemoryStream(pb, false)) {
|
||||
return BitmapLoader.Current.Load(ms, null, null).Result;
|
||||
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
|
||||
}
|
||||
#else
|
||||
#endif
|
||||
|
||||
#if KeePassUAP
|
||||
public static Image LoadImage(byte[] pb)
|
||||
{
|
||||
if(pb == null) throw new ArgumentNullException("pb");
|
||||
|
||||
MemoryStream ms = new MemoryStream(pb, false);
|
||||
try { return LoadImagePriv(ms); }
|
||||
catch(Exception)
|
||||
try { return Image.FromStream(ms); }
|
||||
finally { ms.Close(); }
|
||||
}
|
||||
#elif ModernKeePassLib
|
||||
public static async Task<IBitmap> LoadImage(byte[] pb)
|
||||
{
|
||||
return await ScaleImage(pb, null, null);
|
||||
}
|
||||
|
||||
public static async Task<IBitmap> ScaleImage(byte[] pb, int? w, int? h)
|
||||
{
|
||||
using (var ms = new MemoryStream(pb, false))
|
||||
{
|
||||
return await BitmapLoader.Current.Load(ms, w, h);
|
||||
}
|
||||
}
|
||||
#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 = TryLoadIco(pb);
|
||||
Image imgIco = ExtractBestImageFromIco(pb);
|
||||
if(imgIco != null) return imgIco;
|
||||
throw;
|
||||
}
|
||||
catch(Exception) { Debug.Assert(false); }
|
||||
#endif
|
||||
|
||||
MemoryStream ms = new MemoryStream(pb, false);
|
||||
try { return LoadImagePriv(ms); }
|
||||
finally { ms.Close(); }
|
||||
}
|
||||
|
||||
@@ -91,7 +132,12 @@ namespace ModernKeePassLib.Utility
|
||||
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;
|
||||
@@ -99,17 +145,313 @@ namespace ModernKeePassLib.Utility
|
||||
finally { if(imgSrc != null) imgSrc.Dispose(); }
|
||||
}
|
||||
|
||||
private static Image TryLoadIco(byte[] pb)
|
||||
{
|
||||
#if !KeePassLibSD
|
||||
MemoryStream ms = new MemoryStream(pb, false);
|
||||
try { return (new Icon(ms)).ToBitmap(); }
|
||||
catch(Exception) { }
|
||||
finally { ms.Close(); }
|
||||
#endif
|
||||
private static Image ExtractBestImageFromIco(byte[] pb)
|
||||
{
|
||||
List<GfxImage> l = UnpackIco(pb);
|
||||
if((l == null) || (l.Count == 0)) return null;
|
||||
|
||||
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
|
||||
#if ModernKeePassLib
|
||||
#else
|
||||
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");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
KeePass Password Safe - The Open-Source Password Manager
|
||||
Copyright (C) 2003-2014 Dominik Reichl <dominik.reichl@t-online.de>
|
||||
Copyright (C) 2003-2017 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
|
||||
@@ -19,14 +19,15 @@
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
|
||||
#if !KeePassLibSD
|
||||
using System.IO.Compression;
|
||||
#else
|
||||
#if KeePassLibSD
|
||||
using KeePassLibSD;
|
||||
#else
|
||||
using System.IO.Compression;
|
||||
#endif
|
||||
|
||||
namespace ModernKeePassLib.Utility
|
||||
@@ -36,6 +37,8 @@ namespace ModernKeePassLib.Utility
|
||||
/// </summary>
|
||||
public static class MemUtil
|
||||
{
|
||||
internal static readonly byte[] EmptyByteArray = new byte[0];
|
||||
|
||||
private static readonly uint[] m_vSBox = new uint[256] {
|
||||
0xCD2FACB3, 0xE78A7F5C, 0x6F0803FC, 0xBCF6E230,
|
||||
0x3A321712, 0x06403DB1, 0xD2F84B95, 0xDF22A6E4,
|
||||
@@ -253,66 +256,139 @@ namespace ModernKeePassLib.Utility
|
||||
Debug.Assert(pbArray != null);
|
||||
if(pbArray == null) throw new ArgumentNullException("pbArray");
|
||||
|
||||
// for(int i = 0; i < pbArray.Length; ++i)
|
||||
// pbArray[i] = 0;
|
||||
|
||||
Array.Clear(pbArray, 0, pbArray.Length);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert 2 bytes to a 16-bit unsigned integer using Little-Endian
|
||||
/// encoding.
|
||||
/// Set all elements of an array to the default value.
|
||||
/// </summary>
|
||||
/// <param name="v">Input array.</param>
|
||||
#if KeePassLibSD
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
#else
|
||||
[MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)]
|
||||
#endif
|
||||
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>
|
||||
/// <param name="pb">Input bytes. Array must contain at least 2 bytes.</param>
|
||||
/// <returns>16-bit unsigned integer.</returns>
|
||||
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 ArgumentException();
|
||||
if(pb.Length != 2) throw new ArgumentOutOfRangeException("pb");
|
||||
|
||||
return (ushort)((ushort)pb[0] | ((ushort)pb[1] << 8));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert 4 bytes to a 32-bit unsigned integer using Little-Endian
|
||||
/// encoding.
|
||||
/// 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>
|
||||
/// <param name="pb">Input bytes.</param>
|
||||
/// <returns>32-bit unsigned integer.</returns>
|
||||
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 ArgumentException("Input array must contain 4 bytes!");
|
||||
if(pb.Length != 4) throw new ArgumentOutOfRangeException("pb");
|
||||
|
||||
return (uint)pb[0] | ((uint)pb[1] << 8) | ((uint)pb[2] << 16) |
|
||||
((uint)pb[3] << 24);
|
||||
return ((uint)pb[0] | ((uint)pb[1] << 8) | ((uint)pb[2] << 16) |
|
||||
((uint)pb[3] << 24));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert 8 bytes to a 64-bit unsigned integer using Little-Endian
|
||||
/// encoding.
|
||||
/// 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>
|
||||
/// <param name="pb">Input bytes.</param>
|
||||
/// <returns>64-bit unsigned integer.</returns>
|
||||
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 ArgumentException();
|
||||
if(pb.Length != 8) throw new ArgumentOutOfRangeException("pb");
|
||||
|
||||
return (ulong)pb[0] | ((ulong)pb[1] << 8) | ((ulong)pb[2] << 16) |
|
||||
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);
|
||||
((ulong)pb[6] << 48) | ((ulong)pb[7] << 56));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert a 16-bit unsigned integer to 2 bytes using Little-Endian
|
||||
/// encoding.
|
||||
/// 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>
|
||||
/// <param name="uValue">16-bit input word.</param>
|
||||
/// <returns>Two bytes representing the 16-bit value.</returns>
|
||||
public static byte[] UInt16ToBytes(ushort uValue)
|
||||
{
|
||||
byte[] pb = new byte[2];
|
||||
@@ -327,11 +403,8 @@ namespace ModernKeePassLib.Utility
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert a 32-bit unsigned integer to 4 bytes using Little-Endian
|
||||
/// encoding.
|
||||
/// Convert a 32-bit unsigned integer to 4 bytes (little-endian).
|
||||
/// </summary>
|
||||
/// <param name="uValue">32-bit input word.</param>
|
||||
/// <returns>Four bytes representing the 32-bit value.</returns>
|
||||
public static byte[] UInt32ToBytes(uint uValue)
|
||||
{
|
||||
byte[] pb = new byte[4];
|
||||
@@ -348,11 +421,29 @@ namespace ModernKeePassLib.Utility
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert a 64-bit unsigned integer to 8 bytes using Little-Endian
|
||||
/// encoding.
|
||||
/// 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>
|
||||
/// <param name="uValue">64-bit input word.</param>
|
||||
/// <returns>Eight bytes representing the 64-bit value.</returns>
|
||||
public static byte[] UInt64ToBytes(ulong uValue)
|
||||
{
|
||||
byte[] pb = new byte[8];
|
||||
@@ -372,6 +463,61 @@ namespace ModernKeePassLib.Utility
|
||||
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 byte[] Int64ToBytes(long lValue)
|
||||
{
|
||||
return UInt64ToBytes((ulong)lValue);
|
||||
}
|
||||
|
||||
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)!
|
||||
@@ -387,19 +533,21 @@ namespace ModernKeePassLib.Utility
|
||||
return true;
|
||||
}
|
||||
|
||||
public static void XorArray(byte[] pbSource, int nSourceOffset,
|
||||
byte[] pbBuffer, int nBufferOffset, int nLength)
|
||||
public static void XorArray(byte[] pbSource, int iSourceOffset,
|
||||
byte[] pbBuffer, int iBufferOffset, int cb)
|
||||
{
|
||||
if(pbSource == null) throw new ArgumentNullException("pbSource");
|
||||
if(nSourceOffset < 0) throw new ArgumentException();
|
||||
if(iSourceOffset < 0) throw new ArgumentOutOfRangeException("iSourceOffset");
|
||||
if(pbBuffer == null) throw new ArgumentNullException("pbBuffer");
|
||||
if(nBufferOffset < 0) throw new ArgumentException();
|
||||
if(nLength < 0) throw new ArgumentException();
|
||||
if((nSourceOffset + nLength) > pbSource.Length) throw new ArgumentException();
|
||||
if((nBufferOffset + nLength) > pbBuffer.Length) throw new ArgumentException();
|
||||
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 < nLength; ++i)
|
||||
pbBuffer[nBufferOffset + i] ^= pbSource[nSourceOffset + i];
|
||||
for(int i = 0; i < cb; ++i)
|
||||
pbBuffer[iBufferOffset + i] ^= pbSource[iSourceOffset + i];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -478,7 +626,8 @@ namespace ModernKeePassLib.Utility
|
||||
if(s == null) { Debug.Assert(false); return; }
|
||||
if(pbData == null) { Debug.Assert(false); return; }
|
||||
|
||||
s.Write(pbData, 0, pbData.Length);
|
||||
Debug.Assert(pbData.Length >= 0);
|
||||
if(pbData.Length > 0) s.Write(pbData, 0, pbData.Length);
|
||||
}
|
||||
|
||||
public static byte[] Compress(byte[] pbData)
|
||||
@@ -486,15 +635,21 @@ namespace ModernKeePassLib.Utility
|
||||
if(pbData == null) throw new ArgumentNullException("pbData");
|
||||
if(pbData.Length == 0) return pbData;
|
||||
|
||||
MemoryStream msCompressed = new MemoryStream();
|
||||
GZipStream gz = new GZipStream(msCompressed, CompressionMode.Compress);
|
||||
MemoryStream msSource = new MemoryStream(pbData, false);
|
||||
MemUtil.CopyStream(msSource, gz);
|
||||
gz.Dispose();
|
||||
msSource.Dispose();
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
byte[] pbCompressed = msCompressed.ToArray();
|
||||
msCompressed.Dispose();
|
||||
return pbCompressed;
|
||||
}
|
||||
|
||||
@@ -503,15 +658,21 @@ namespace ModernKeePassLib.Utility
|
||||
if(pbCompressed == null) throw new ArgumentNullException("pbCompressed");
|
||||
if(pbCompressed.Length == 0) return pbCompressed;
|
||||
|
||||
MemoryStream msCompressed = new MemoryStream(pbCompressed, false);
|
||||
GZipStream gz = new GZipStream(msCompressed, CompressionMode.Decompress);
|
||||
MemoryStream msData = new MemoryStream();
|
||||
MemUtil.CopyStream(gz, msData);
|
||||
gz.Dispose();
|
||||
msCompressed.Dispose();
|
||||
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();
|
||||
}
|
||||
|
||||
byte[] pbData = msData.ToArray();
|
||||
msData.Dispose();
|
||||
return pbData;
|
||||
}
|
||||
|
||||
|
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
KeePass Password Safe - The Open-Source Password Manager
|
||||
Copyright (C) 2003-2014 Dominik Reichl <dominik.reichl@t-online.de>
|
||||
Copyright (C) 2003-2017 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
|
||||
@@ -20,9 +20,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Specialized;
|
||||
using System.Text;
|
||||
using System.Windows.Forms;
|
||||
using System.Diagnostics;
|
||||
using System.Text;
|
||||
|
||||
#if !KeePassUAP
|
||||
using System.Windows.Forms;
|
||||
#endif
|
||||
|
||||
using ModernKeePassLib.Resources;
|
||||
using ModernKeePassLib.Serialization;
|
||||
@@ -94,7 +97,9 @@ namespace ModernKeePassLib.Utility
|
||||
get { return m_uCurrentMessageCount; }
|
||||
}
|
||||
|
||||
#if !KeePassUAP
|
||||
public static event EventHandler<MessageServiceEventArgs> MessageShowing;
|
||||
#endif
|
||||
|
||||
private static string ObjectsToMessage(object[] vLines)
|
||||
{
|
||||
@@ -105,7 +110,7 @@ namespace ModernKeePassLib.Utility
|
||||
{
|
||||
if(vLines == null) return string.Empty;
|
||||
|
||||
string strNewPara = MessageService.NewParagraph;
|
||||
string strNewPara = Environment.NewLine;
|
||||
|
||||
StringBuilder sbText = new StringBuilder();
|
||||
bool bSeparator = false;
|
||||
@@ -168,6 +173,7 @@ namespace ModernKeePassLib.Utility
|
||||
}
|
||||
#endif
|
||||
|
||||
#if !KeePassUAP
|
||||
internal static DialogResult SafeShowMessageBox(string strText, string strTitle,
|
||||
MessageBoxButtons mb, MessageBoxIcon mi, MessageBoxDefaultButton mdb)
|
||||
{
|
||||
@@ -275,19 +281,21 @@ namespace ModernKeePassLib.Utility
|
||||
++m_uCurrentMessageCount;
|
||||
|
||||
string strTitle = PwDefs.ShortProductName + @" - " + KLRes.FatalError;
|
||||
string strText = KLRes.FatalErrorText + MessageService.NewParagraph +
|
||||
KLRes.ErrorInClipboard + MessageService.NewParagraph +
|
||||
string strText = KLRes.FatalErrorText + Environment.NewLine +
|
||||
KLRes.ErrorInClipboard + Environment.NewLine +
|
||||
// Please send it to the KeePass developers.
|
||||
// KLRes.ErrorFeedbackRequest + MessageService.NewParagraph +
|
||||
// KLRes.ErrorFeedbackRequest + Environment.NewLine +
|
||||
ObjectsToMessage(vLines);
|
||||
|
||||
try
|
||||
{
|
||||
#if !KeePassLibSD
|
||||
Clipboard.Clear();
|
||||
Clipboard.SetText(ObjectsToMessage(vLines, true));
|
||||
string strDetails = ObjectsToMessage(vLines, true);
|
||||
|
||||
#if KeePassLibSD
|
||||
Clipboard.SetDataObject(strDetails);
|
||||
#else
|
||||
Clipboard.SetDataObject(ObjectsToMessage(vLines, true));
|
||||
Clipboard.Clear();
|
||||
Clipboard.SetText(strDetails);
|
||||
#endif
|
||||
}
|
||||
catch(Exception) { Debug.Assert(false); }
|
||||
@@ -364,21 +372,7 @@ namespace ModernKeePassLib.Utility
|
||||
public static void ShowLoadWarning(string strFilePath, Exception ex,
|
||||
bool bFullException)
|
||||
{
|
||||
string str = string.Empty;
|
||||
|
||||
if((strFilePath != null) && (strFilePath.Length > 0))
|
||||
str += strFilePath + MessageService.NewParagraph;
|
||||
|
||||
str += KLRes.FileLoadFailed;
|
||||
|
||||
if((ex != null) && (ex.Message != null) && (ex.Message.Length > 0))
|
||||
{
|
||||
str += MessageService.NewParagraph;
|
||||
if(!bFullException) str += ex.Message;
|
||||
else str += ObjectsToMessage(new object[] { ex }, true);
|
||||
}
|
||||
|
||||
ShowWarning(str);
|
||||
ShowWarning(GetLoadWarningMessage(strFilePath, ex, bFullException));
|
||||
}
|
||||
|
||||
public static void ShowLoadWarning(IOConnectionInfo ioConnection, Exception ex)
|
||||
@@ -398,18 +392,7 @@ namespace ModernKeePassLib.Utility
|
||||
return;
|
||||
}
|
||||
|
||||
string str = string.Empty;
|
||||
if((strFilePath != null) && (strFilePath.Length > 0))
|
||||
str += strFilePath + MessageService.NewParagraph;
|
||||
|
||||
str += KLRes.FileSaveFailed;
|
||||
|
||||
if((ex != null) && (ex.Message != null) && (ex.Message.Length > 0))
|
||||
str += MessageService.NewParagraph + ex.Message;
|
||||
|
||||
if(bCorruptionWarning)
|
||||
str += MessageService.NewParagraph + KLRes.FileSaveCorruptionWarning;
|
||||
|
||||
string str = GetSaveWarningMessage(strFilePath, ex, bCorruptionWarning);
|
||||
ShowWarning(str);
|
||||
}
|
||||
|
||||
@@ -420,6 +403,45 @@ namespace ModernKeePassLib.Utility
|
||||
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 + Environment.NewLine;
|
||||
|
||||
str += KLRes.FileLoadFailed;
|
||||
|
||||
if((ex != null) && !string.IsNullOrEmpty(ex.Message))
|
||||
{
|
||||
str += Environment.NewLine;
|
||||
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 + Environment.NewLine;
|
||||
|
||||
str += KLRes.FileSaveFailed;
|
||||
|
||||
if((ex != null) && !string.IsNullOrEmpty(ex.Message))
|
||||
str += Environment.NewLine + ex.Message;
|
||||
|
||||
if(bCorruptionWarning)
|
||||
str += Environment.NewLine + KLRes.FileSaveCorruptionWarning;
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
public static void ExternalIncrementMessageCount()
|
||||
{
|
||||
|
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
KeePass Password Safe - The Open-Source Password Manager
|
||||
Copyright (C) 2003-2014 Dominik Reichl <dominik.reichl@t-online.de>
|
||||
Copyright (C) 2003-2017 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
|
||||
@@ -17,13 +17,23 @@
|
||||
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.Text;
|
||||
using System.Windows.Forms;
|
||||
using System.ComponentModel;
|
||||
using System.Reflection;
|
||||
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;
|
||||
|
||||
@@ -31,19 +41,72 @@ namespace ModernKeePassLib.Utility
|
||||
{
|
||||
public static class MonoWorkarounds
|
||||
{
|
||||
private static bool? m_bReq = null;
|
||||
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(!m_bReq.HasValue) m_bReq = NativeLib.IsUnix();
|
||||
return m_bReq.Value;
|
||||
if(!g_bReq.HasValue) g_bReq = NativeLib.IsUnix();
|
||||
return g_bReq.Value;
|
||||
}
|
||||
|
||||
// 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/
|
||||
// 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
|
||||
@@ -60,6 +123,12 @@ namespace ModernKeePassLib.Utility
|
||||
// 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
|
||||
@@ -71,17 +140,23 @@ namespace ModernKeePassLib.Utility
|
||||
// https://bugzilla.novell.com/show_bug.cgi?id=649266
|
||||
// 686017:
|
||||
// Minimum sizes must be enforced.
|
||||
// http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=686017
|
||||
// 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, otherwise Mono throws exceptions.
|
||||
// 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/
|
||||
// 3574233558:
|
||||
// Problems with minimizing windows, no content rendered.
|
||||
// https://sourceforge.net/p/keepass/discussion/329220/thread/d50a79d6/
|
||||
@@ -89,6 +164,9 @@ namespace ModernKeePassLib.Utility
|
||||
{
|
||||
if(!MonoWorkarounds.IsRequired()) return false;
|
||||
|
||||
bool bForce;
|
||||
if(g_dForceReq.TryGetValue(uBugID, out bForce)) return bForce;
|
||||
|
||||
ulong v = NativeLib.MonoVersion;
|
||||
if(v != 0)
|
||||
{
|
||||
@@ -99,6 +177,140 @@ namespace ModernKeePassLib.Utility
|
||||
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;
|
||||
@@ -320,5 +532,57 @@ namespace ModernKeePassLib.Utility
|
||||
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 = new XmlDocument();
|
||||
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
|
||||
}
|
||||
}
|
||||
|
@@ -360,9 +360,9 @@ namespace ModernKeePassLib.Utility
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Split up a command-line into application and argument.
|
||||
/// Split up a command line into application and argument.
|
||||
/// </summary>
|
||||
/// <param name="strCmdLine">Command-line to split.</param>
|
||||
/// <param name="strCmdLine">Command line to split.</param>
|
||||
/// <param name="strApp">Application path.</param>
|
||||
/// <param name="strArgs">Arguments.</param>
|
||||
public static void SplitCommandLine(string strCmdLine, out string strApp, out string strArgs)
|
||||
@@ -501,7 +501,7 @@ namespace ModernKeePassLib.Utility
|
||||
#if !KeePassLibSD
|
||||
#if !ModernKeePassLib && !KeePassRT
|
||||
if(excp.TargetSite != null)
|
||||
strText += excp.TargetSite.ToString() + MessageService.NewLine;
|
||||
strText += excp.TargetSite.ToString() + Environment.NewLine;
|
||||
#endif
|
||||
|
||||
if(excp.Data != null)
|
||||
@@ -671,7 +671,8 @@ namespace ModernKeePassLib.Utility
|
||||
return DateTime.TryParse(str, out dt);
|
||||
#else
|
||||
try { dt = DateTime.Parse(str); return true; }
|
||||
catch(Exception) { dt = DateTime.MinValue; return false; }
|
||||
catch(Exception) { dt = DateTime.UtcNow; }
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -720,7 +721,7 @@ namespace ModernKeePassLib.Utility
|
||||
|
||||
/// <summary>
|
||||
/// Removes all characters that are not valid XML characters,
|
||||
/// according to http://www.w3.org/TR/xml/#charsets .
|
||||
/// according to https://www.w3.org/TR/xml/#charsets .
|
||||
/// </summary>
|
||||
/// <param name="strText">Source text.</param>
|
||||
/// <returns>Text containing only valid XML characters.</returns>
|
||||
@@ -762,7 +763,7 @@ namespace ModernKeePassLib.Utility
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
private static Regex m_rxNaturalSplit = null;
|
||||
/* private static Regex g_rxNaturalSplit = null;
|
||||
public static int CompareNaturally(string strX, string strY)
|
||||
{
|
||||
Debug.Assert(strX != null);
|
||||
@@ -773,39 +774,31 @@ namespace ModernKeePassLib.Utility
|
||||
if(NativeMethods.SupportsStrCmpNaturally)
|
||||
return NativeMethods.StrCmpNaturally(strX, strY);
|
||||
|
||||
strX = strX.ToLower(); // Case-insensitive comparison
|
||||
strY = strY.ToLower();
|
||||
if(g_rxNaturalSplit == null)
|
||||
g_rxNaturalSplit = new Regex(@"([0-9]+)", RegexOptions.Compiled);
|
||||
|
||||
if(m_rxNaturalSplit == null)
|
||||
m_rxNaturalSplit = new Regex(@"([0-9]+)",
|
||||
#if ModernKeePassLib || KeePassRT
|
||||
RegexOptions.None);
|
||||
#else
|
||||
RegexOptions.Compiled);
|
||||
#endif
|
||||
string[] vPartsX = g_rxNaturalSplit.Split(strX);
|
||||
string[] vPartsY = g_rxNaturalSplit.Split(strY);
|
||||
|
||||
string[] vPartsX = m_rxNaturalSplit.Split(strX);
|
||||
string[] vPartsY = m_rxNaturalSplit.Split(strY);
|
||||
|
||||
for(int i = 0; i < Math.Min(vPartsX.Length, vPartsY.Length); ++i)
|
||||
int n = Math.Min(vPartsX.Length, vPartsY.Length);
|
||||
for(int i = 0; i < n; ++i)
|
||||
{
|
||||
string strPartX = vPartsX[i], strPartY = vPartsY[i];
|
||||
int iPartCompare;
|
||||
|
||||
#if KeePassLibSD
|
||||
ulong uX = 0, uY = 0;
|
||||
try
|
||||
{
|
||||
uX = ulong.Parse(strPartX);
|
||||
uY = ulong.Parse(strPartY);
|
||||
ulong uX = ulong.Parse(strPartX);
|
||||
ulong uY = ulong.Parse(strPartY);
|
||||
iPartCompare = uX.CompareTo(uY);
|
||||
}
|
||||
catch(Exception) { iPartCompare = strPartX.CompareTo(strPartY); }
|
||||
catch(Exception) { iPartCompare = string.Compare(strPartX, strPartY, true); }
|
||||
#else
|
||||
ulong uX, uY;
|
||||
if(ulong.TryParse(strPartX, out uX) && ulong.TryParse(strPartY, out uY))
|
||||
iPartCompare = uX.CompareTo(uY);
|
||||
else iPartCompare = strPartX.CompareTo(strPartY);
|
||||
else iPartCompare = string.Compare(strPartX, strPartY, true);
|
||||
#endif
|
||||
|
||||
if(iPartCompare != 0) return iPartCompare;
|
||||
@@ -814,6 +807,114 @@ namespace ModernKeePassLib.Utility
|
||||
if(vPartsX.Length == vPartsY.Length) return 0;
|
||||
if(vPartsX.Length < vPartsY.Length) return -1;
|
||||
return 1;
|
||||
} */
|
||||
|
||||
public static int CompareNaturally(string strX, string strY)
|
||||
{
|
||||
Debug.Assert(strX != null);
|
||||
if(strX == null) throw new ArgumentNullException("strX");
|
||||
Debug.Assert(strY != null);
|
||||
if(strY == null) throw new ArgumentNullException("strY");
|
||||
|
||||
if(NativeMethods.SupportsStrCmpNaturally)
|
||||
return NativeMethods.StrCmpNaturally(strX, strY);
|
||||
|
||||
int cX = strX.Length;
|
||||
int cY = strY.Length;
|
||||
if(cX == 0) return ((cY == 0) ? 0 : -1);
|
||||
if(cY == 0) return 1;
|
||||
|
||||
char chFirstX = strX[0];
|
||||
char chFirstY = strY[0];
|
||||
bool bExpNum = ((chFirstX >= '0') && (chFirstX <= '9'));
|
||||
bool bExpNumY = ((chFirstY >= '0') && (chFirstY <= '9'));
|
||||
#if ModernKeePassLib
|
||||
if (bExpNum != bExpNumY) return StringComparer.OrdinalIgnoreCase.Compare(strX, strY);
|
||||
#else
|
||||
if(bExpNum != bExpNumY) return string.Compare(strX, strY, true);
|
||||
#endif
|
||||
|
||||
int pX = 0;
|
||||
int pY = 0;
|
||||
while((pX < cX) && (pY < cY))
|
||||
{
|
||||
Debug.Assert(((strX[pX] >= '0') && (strX[pX] <= '9')) == bExpNum);
|
||||
Debug.Assert(((strY[pY] >= '0') && (strY[pY] <= '9')) == bExpNum);
|
||||
|
||||
int pExclX = pX + 1;
|
||||
while(pExclX < cX)
|
||||
{
|
||||
char ch = strX[pExclX];
|
||||
bool bChNum = ((ch >= '0') && (ch <= '9'));
|
||||
if(bChNum != bExpNum) break;
|
||||
++pExclX;
|
||||
}
|
||||
|
||||
int pExclY = pY + 1;
|
||||
while(pExclY < cY)
|
||||
{
|
||||
char ch = strY[pExclY];
|
||||
bool bChNum = ((ch >= '0') && (ch <= '9'));
|
||||
if(bChNum != bExpNum) break;
|
||||
++pExclY;
|
||||
}
|
||||
|
||||
string strPartX = strX.Substring(pX, pExclX - pX);
|
||||
string strPartY = strY.Substring(pY, pExclY - pY);
|
||||
|
||||
bool bStrCmp = true;
|
||||
if(bExpNum)
|
||||
{
|
||||
// 2^64 - 1 = 18446744073709551615 has length 20
|
||||
if((strPartX.Length <= 19) && (strPartY.Length <= 19))
|
||||
{
|
||||
ulong uX, uY;
|
||||
if(ulong.TryParse(strPartX, out uX) && ulong.TryParse(strPartY, out uY))
|
||||
{
|
||||
if(uX < uY) return -1;
|
||||
if(uX > uY) return 1;
|
||||
|
||||
bStrCmp = false;
|
||||
}
|
||||
else { Debug.Assert(false); }
|
||||
}
|
||||
else
|
||||
{
|
||||
double dX, dY;
|
||||
if(double.TryParse(strPartX, out dX) && double.TryParse(strPartY, out dY))
|
||||
{
|
||||
if(dX < dY) return -1;
|
||||
if(dX > dY) return 1;
|
||||
|
||||
bStrCmp = false;
|
||||
}
|
||||
else { Debug.Assert(false); }
|
||||
}
|
||||
}
|
||||
if(bStrCmp)
|
||||
{
|
||||
#if ModernKeePassLib
|
||||
int c = StringComparer.OrdinalIgnoreCase.Compare(strPartX, strPartY);
|
||||
#else
|
||||
int c = string.Compare(strPartX, strPartY, true);
|
||||
#endif
|
||||
if(c != 0) return c;
|
||||
}
|
||||
|
||||
bExpNum = !bExpNum;
|
||||
pX = pExclX;
|
||||
pY = pExclY;
|
||||
}
|
||||
|
||||
if(pX >= cX)
|
||||
{
|
||||
Debug.Assert(pX == cX);
|
||||
if(pY >= cY) { Debug.Assert(pY == cY); return 0; }
|
||||
return -1;
|
||||
}
|
||||
|
||||
Debug.Assert(pY == cY);
|
||||
return 1;
|
||||
}
|
||||
|
||||
public static string RemoveAccelerator(string strMenuText)
|
||||
@@ -888,13 +989,12 @@ namespace ModernKeePassLib.Utility
|
||||
public static bool IsHexString(string str, bool bStrict)
|
||||
{
|
||||
if(str == null) throw new ArgumentNullException("str");
|
||||
if(str.Length == 0) return true;
|
||||
|
||||
foreach(char ch in str)
|
||||
{
|
||||
if((ch >= '0') && (ch <= '9')) continue;
|
||||
if((ch >= 'a') && (ch <= 'z')) continue;
|
||||
if((ch >= 'A') && (ch <= 'Z')) continue;
|
||||
if((ch >= 'a') && (ch <= 'f')) continue;
|
||||
if((ch >= 'A') && (ch <= 'F')) continue;
|
||||
|
||||
if(bStrict) return false;
|
||||
|
||||
@@ -907,8 +1007,31 @@ namespace ModernKeePassLib.Utility
|
||||
return true;
|
||||
}
|
||||
|
||||
public static bool IsHexString(byte[] pbUtf8, bool bStrict)
|
||||
{
|
||||
if(pbUtf8 == null) throw new ArgumentNullException("pbUtf8");
|
||||
|
||||
for(int i = 0; i < pbUtf8.Length; ++i)
|
||||
{
|
||||
byte bt = pbUtf8[i];
|
||||
if((bt >= (byte)'0') && (bt <= (byte)'9')) continue;
|
||||
if((bt >= (byte)'a') && (bt <= (byte)'f')) continue;
|
||||
if((bt >= (byte)'A') && (bt <= (byte)'F')) continue;
|
||||
|
||||
if(bStrict) return false;
|
||||
|
||||
if((bt == (byte)' ') || (bt == (byte)'\t') ||
|
||||
(bt == (byte)'\r') || (bt == (byte)'\n'))
|
||||
continue;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
#if !KeePassLibSD
|
||||
private static readonly char[] m_vPatternPartsSep = new char[]{ '*' };
|
||||
private static readonly char[] m_vPatternPartsSep = new char[] { '*' };
|
||||
public static bool SimplePatternMatch(string strPattern, string strText,
|
||||
StringComparison sc)
|
||||
{
|
||||
@@ -1252,7 +1375,7 @@ namespace ModernKeePassLib.Utility
|
||||
return v;
|
||||
}
|
||||
|
||||
private static readonly char[] m_vTagSep = new char[]{ ',', ';', ':' };
|
||||
private static readonly char[] m_vTagSep = new char[] { ',', ';', ':' };
|
||||
public static string TagsToString(List<string> vTags, bool bForDisplay)
|
||||
{
|
||||
if(vTags == null) throw new ArgumentNullException("vTags");
|
||||
@@ -1625,5 +1748,16 @@ namespace ModernKeePassLib.Utility
|
||||
|
||||
return iCount;
|
||||
}
|
||||
|
||||
internal static string ReplaceNulls(string str)
|
||||
{
|
||||
if(str == null) { Debug.Assert(false); return null; }
|
||||
|
||||
if(str.IndexOf('\0') < 0) return str;
|
||||
|
||||
// Replacing null characters by spaces is the
|
||||
// behavior of Notepad (on Windows 10)
|
||||
return str.Replace('\0', ' ');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -38,20 +38,43 @@ namespace ModernKeePassLib.Utility
|
||||
/// </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>
|
||||
/// <param name="dt"></param>
|
||||
/// <returns></returns>
|
||||
[Obsolete]
|
||||
public static byte[] PackTime(DateTime dt)
|
||||
{
|
||||
dt = ToLocal(dt, true);
|
||||
|
||||
byte[] pb = new byte[5];
|
||||
|
||||
// Pack time to 5 byte structure:
|
||||
@@ -73,6 +96,7 @@ namespace ModernKeePassLib.Utility
|
||||
/// </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));
|
||||
@@ -89,7 +113,8 @@ namespace ModernKeePassLib.Utility
|
||||
int nMinute = ((n4 & 0x0000000F) << 2) | (n5 >> 6);
|
||||
int nSecond = n5 & 0x0000003F;
|
||||
|
||||
return new DateTime(nYear, nMonth, nDay, nHour, nMinute, nSecond);
|
||||
return (new DateTime(nYear, nMonth, nDay, nHour, nMinute,
|
||||
nSecond, DateTimeKind.Local)).ToUniversalTime();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -97,10 +122,13 @@ namespace ModernKeePassLib.Utility
|
||||
/// </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);
|
||||
@@ -118,6 +146,7 @@ namespace ModernKeePassLib.Utility
|
||||
/// </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);
|
||||
@@ -125,8 +154,8 @@ namespace ModernKeePassLib.Utility
|
||||
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]);
|
||||
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>
|
||||
@@ -136,23 +165,32 @@ namespace ModernKeePassLib.Utility
|
||||
/// <returns>String representing the specified <c>DateTime</c> object.</returns>
|
||||
public static string ToDisplayString(DateTime dt)
|
||||
{
|
||||
return dt.ToString();
|
||||
return ToLocal(dt, true).ToString();
|
||||
}
|
||||
|
||||
public static string ToDisplayStringDateOnly(DateTime dt)
|
||||
{
|
||||
return dt.ToString("d");
|
||||
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 = DateTime.Parse(strDisplay); return dt; }
|
||||
try { dt = ToLocal(DateTime.Parse(strDisplay), true); return true; }
|
||||
catch(Exception) { }
|
||||
#else
|
||||
if(DateTime.TryParse(strDisplay, out dt)) return dt;
|
||||
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
|
||||
@@ -160,19 +198,25 @@ namespace ModernKeePassLib.Utility
|
||||
// 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);
|
||||
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))
|
||||
return dt;
|
||||
{
|
||||
dt = ToLocal(dt, true);
|
||||
return true;
|
||||
}
|
||||
if(DateTime.TryParseExact(strDisplay, m_strDtfDate, null, dts, out dt))
|
||||
return dt;
|
||||
{
|
||||
dt = ToLocal(dt, true);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
|
||||
Debug.Assert(false);
|
||||
return DateTime.Now;
|
||||
return false;
|
||||
}
|
||||
|
||||
#if !KeePassLibSD
|
||||
@@ -262,8 +306,10 @@ namespace ModernKeePassLib.Utility
|
||||
|
||||
public static string SerializeUtc(DateTime dt)
|
||||
{
|
||||
string str = dt.ToUniversalTime().ToString("s");
|
||||
if(str.EndsWith("Z") == false) str += "Z";
|
||||
Debug.Assert(dt.Kind != DateTimeKind.Unspecified);
|
||||
|
||||
string str = ToUtc(dt, false).ToString("s");
|
||||
if(!str.EndsWith("Z")) str += "Z";
|
||||
return str;
|
||||
}
|
||||
|
||||
@@ -274,37 +320,40 @@ namespace ModernKeePassLib.Utility
|
||||
if(str.EndsWith("Z")) str = str.Substring(0, str.Length - 1);
|
||||
|
||||
bool bResult = StrUtil.TryParseDateTime(str, out dt);
|
||||
if(bResult) dt = dt.ToLocalTime();
|
||||
if(bResult) dt = ToUtc(dt, true);
|
||||
return bResult;
|
||||
}
|
||||
|
||||
private static DateTime? m_dtUnixRoot = null;
|
||||
public static double SerializeUnix(DateTime dt)
|
||||
{
|
||||
return (ToUtc(dt, false) - TimeUtil.UnixRoot).TotalSeconds;
|
||||
}
|
||||
|
||||
public static DateTime ConvertUnixTime(double dtUnix)
|
||||
{
|
||||
try
|
||||
{
|
||||
if(!m_dtUnixRoot.HasValue)
|
||||
m_dtUnixRoot = (new DateTime(1970, 1, 1, 0, 0, 0, 0,
|
||||
DateTimeKind.Utc)).ToLocalTime();
|
||||
|
||||
return m_dtUnixRoot.Value.AddSeconds(dtUnix);
|
||||
}
|
||||
try { return TimeUtil.UnixRoot.AddSeconds(dtUnix); }
|
||||
catch(Exception) { Debug.Assert(false); }
|
||||
|
||||
return DateTime.Now;
|
||||
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)
|
||||
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",
|
||||
m_vUSMonths = new string[] { "January", "February", "March",
|
||||
"April", "May", "June", "July", "August", "September",
|
||||
"October", "November", "December" };
|
||||
|
||||
@@ -314,14 +363,14 @@ namespace ModernKeePassLib.Utility
|
||||
if(str.StartsWith(m_vUSMonths[i], StrUtil.CaseIgnoreCmp))
|
||||
{
|
||||
str = str.Substring(m_vUSMonths[i].Length);
|
||||
string[] v = str.Split(new char[]{ ',', ';' });
|
||||
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);
|
||||
return new DateTime(iYear, i + 1, iDay, 0, 0, 0, k);
|
||||
else { Debug.Assert(false); return null; }
|
||||
}
|
||||
}
|
||||
@@ -331,11 +380,13 @@ namespace ModernKeePassLib.Utility
|
||||
#endif
|
||||
|
||||
private static readonly DateTime m_dtInvMin =
|
||||
new DateTime(2999, 12, 27, 23, 59, 59);
|
||||
new DateTime(2999, 12, 27, 23, 59, 59, DateTimeKind.Utc);
|
||||
private static readonly DateTime m_dtInvMax =
|
||||
new DateTime(2999, 12, 29, 23, 59, 59);
|
||||
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';
|
||||
@@ -368,5 +419,64 @@ namespace ModernKeePassLib.Utility
|
||||
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;
|
||||
} */
|
||||
}
|
||||
}
|
||||
|
@@ -23,7 +23,7 @@ using System.IO;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Diagnostics;
|
||||
|
||||
using System.Globalization;
|
||||
#if ModernKeePassLib
|
||||
//using PCLStorage;
|
||||
using Windows.Storage;
|
||||
@@ -686,10 +686,8 @@ namespace ModernKeePassLib.Utility
|
||||
if(strPath.Length == 0) { Debug.Assert(false); continue; }
|
||||
Debug.Assert(strPath == strPathRaw);
|
||||
|
||||
if(!strPath.EndsWith(strExt, StrUtil.CaseIgnoreCmp))
|
||||
continue;
|
||||
|
||||
l.Add(strPathRaw);
|
||||
if(strPath.EndsWith(strExt, StrUtil.CaseIgnoreCmp))
|
||||
l.Add(strPathRaw);
|
||||
}
|
||||
}
|
||||
else l.AddRange(v);
|
||||
@@ -725,10 +723,8 @@ namespace ModernKeePassLib.Utility
|
||||
if(strPath.Length == 0) { Debug.Assert(false); continue; }
|
||||
Debug.Assert(strPath == strPathRaw);
|
||||
|
||||
if(!strPath.EndsWith(strExt, StrUtil.CaseIgnoreCmp))
|
||||
continue;
|
||||
|
||||
l.Add(fi);
|
||||
if(strPath.EndsWith(strExt, StrUtil.CaseIgnoreCmp))
|
||||
l.Add(fi);
|
||||
}
|
||||
}
|
||||
else l.AddRange(v);
|
||||
@@ -736,5 +732,54 @@ namespace ModernKeePassLib.Utility
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user