mirror of
https://github.com/wismna/ModernKeePassLib.git
synced 2025-10-03 15:40:20 -04:00
Setup solution
This commit is contained in:
190
ModernKeePassLib/Native/ClipboardU.cs
Normal file
190
ModernKeePassLib/Native/ClipboardU.cs
Normal file
@@ -0,0 +1,190 @@
|
||||
/*
|
||||
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.Reflection;
|
||||
using System.Text;
|
||||
|
||||
namespace ModernKeePassLib.Native
|
||||
{
|
||||
internal static class ClipboardU
|
||||
{
|
||||
private const string XSel = "xsel";
|
||||
private const string XSelV = "--version";
|
||||
private const string XSelR = "--output --clipboard";
|
||||
private const string XSelC = "--clear --clipboard";
|
||||
private const string XSelW = "--input --clipboard";
|
||||
private const string XSelND = " --nodetach";
|
||||
private const AppRunFlags XSelWF = AppRunFlags.WaitForExit;
|
||||
|
||||
private static bool? g_obXSel = null;
|
||||
|
||||
public static string GetText()
|
||||
{
|
||||
// System.Windows.Forms.Clipboard doesn't work properly,
|
||||
// see Mono workaround 1530
|
||||
|
||||
// string str = GtkGetText();
|
||||
// if(str != null) return str;
|
||||
|
||||
return XSelGetText();
|
||||
}
|
||||
|
||||
public static bool SetText(string strText, bool bMayBlock)
|
||||
{
|
||||
string str = (strText ?? string.Empty);
|
||||
|
||||
// System.Windows.Forms.Clipboard doesn't work properly,
|
||||
// see Mono workaround 1530
|
||||
|
||||
// if(GtkSetText(str)) return true;
|
||||
|
||||
return XSelSetText(str, bMayBlock);
|
||||
}
|
||||
|
||||
// =============================================================
|
||||
// LibGTK
|
||||
|
||||
// Even though GTK+ 3 appears to be loaded already, performing a
|
||||
// P/Invoke of LibGTK's gtk_init_check function terminates the
|
||||
// process (!) with the following error message:
|
||||
// "Gtk-ERROR **: GTK+ 2.x symbols detected. Using GTK+ 2.x and
|
||||
// GTK+ 3 in the same process is not supported".
|
||||
|
||||
/* private static bool GtkInit()
|
||||
{
|
||||
try
|
||||
{
|
||||
// GTK requires GLib;
|
||||
// the following throws if and only if GLib is unavailable
|
||||
NativeMethods.g_free(IntPtr.Zero);
|
||||
|
||||
if(NativeMethods.gtk_init_check(IntPtr.Zero, IntPtr.Zero) !=
|
||||
NativeMethods.G_FALSE)
|
||||
return true;
|
||||
|
||||
Debug.Assert(false);
|
||||
}
|
||||
catch(Exception) { Debug.Assert(false); }
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static string GtkGetText()
|
||||
{
|
||||
IntPtr lpText = IntPtr.Zero;
|
||||
try
|
||||
{
|
||||
if(GtkInit())
|
||||
{
|
||||
IntPtr h = NativeMethods.gtk_clipboard_get(
|
||||
NativeMethods.GDK_SELECTION_CLIPBOARD);
|
||||
if(h != IntPtr.Zero)
|
||||
{
|
||||
lpText = NativeMethods.gtk_clipboard_wait_for_text(h);
|
||||
if(lpText != IntPtr.Zero)
|
||||
return NativeMethods.Utf8ZToString(lpText);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch(Exception) { Debug.Assert(false); }
|
||||
finally
|
||||
{
|
||||
try { NativeMethods.g_free(lpText); }
|
||||
catch(Exception) { Debug.Assert(false); }
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static bool GtkSetText(string str)
|
||||
{
|
||||
IntPtr lpText = IntPtr.Zero;
|
||||
try
|
||||
{
|
||||
if(GtkInit())
|
||||
{
|
||||
lpText = NativeMethods.Utf8ZFromString(str ?? string.Empty);
|
||||
if(lpText == IntPtr.Zero) { Debug.Assert(false); return false; }
|
||||
|
||||
bool b = false;
|
||||
for(int i = 0; i < 2; ++i)
|
||||
{
|
||||
IntPtr h = NativeMethods.gtk_clipboard_get((i == 0) ?
|
||||
NativeMethods.GDK_SELECTION_PRIMARY :
|
||||
NativeMethods.GDK_SELECTION_CLIPBOARD);
|
||||
if(h != IntPtr.Zero)
|
||||
{
|
||||
NativeMethods.gtk_clipboard_clear(h);
|
||||
NativeMethods.gtk_clipboard_set_text(h, lpText, -1);
|
||||
NativeMethods.gtk_clipboard_store(h);
|
||||
b = true;
|
||||
}
|
||||
}
|
||||
|
||||
return b;
|
||||
}
|
||||
}
|
||||
catch(Exception) { Debug.Assert(false); }
|
||||
finally { NativeMethods.Utf8ZFree(lpText); }
|
||||
|
||||
return false;
|
||||
} */
|
||||
|
||||
// =============================================================
|
||||
// XSel
|
||||
|
||||
private static bool XSelInit()
|
||||
{
|
||||
if(g_obXSel.HasValue) return g_obXSel.Value;
|
||||
|
||||
string strTest = NativeLib.RunConsoleApp(XSel, XSelV);
|
||||
|
||||
bool b = (strTest != null);
|
||||
g_obXSel = b;
|
||||
return b;
|
||||
}
|
||||
|
||||
private static string XSelGetText()
|
||||
{
|
||||
if(!XSelInit()) return null;
|
||||
|
||||
return NativeLib.RunConsoleApp(XSel, XSelR);
|
||||
}
|
||||
|
||||
private static bool XSelSetText(string str, bool bMayBlock)
|
||||
{
|
||||
if(!XSelInit()) return false;
|
||||
|
||||
string strOpt = (bMayBlock ? XSelND : string.Empty);
|
||||
|
||||
// xsel with an empty input can hang, thus use --clear
|
||||
if(str.Length == 0)
|
||||
return (NativeLib.RunConsoleApp(XSel, XSelC + strOpt,
|
||||
null, XSelWF) != null);
|
||||
|
||||
// Use --nodetach to prevent clipboard corruption;
|
||||
// https://sourceforge.net/p/keepass/bugs/1603/
|
||||
return (NativeLib.RunConsoleApp(XSel, XSelW + strOpt,
|
||||
str, XSelWF) != null);
|
||||
}
|
||||
}
|
||||
}
|
90
ModernKeePassLib/Native/Native.PCL.cs
Normal file
90
ModernKeePassLib/Native/Native.PCL.cs
Normal file
@@ -0,0 +1,90 @@
|
||||
using System;
|
||||
|
||||
namespace ModernKeePassLib.Native
|
||||
{
|
||||
internal static class NativeLib
|
||||
{
|
||||
public static ulong MonoVersion {
|
||||
get { throw new NotImplementedException(); }
|
||||
}
|
||||
|
||||
public static bool IsUnix()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public static bool TransformKey256(byte[] pbNative, byte[] pbSeed, ulong uRounds)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public static System.PlatformID GetPlatformID()
|
||||
{
|
||||
return Environment.OSVersion.Platform;
|
||||
}
|
||||
}
|
||||
|
||||
internal static class NativeMethods
|
||||
{
|
||||
public static bool SupportsStrCmpNaturally => false;
|
||||
|
||||
internal const int GCRY_CIPHER_AES256 = 9;
|
||||
internal const int GCRY_CIPHER_MODE_ECB = 1;
|
||||
|
||||
public static int StrCmpNaturally (string s1, string s2)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
internal static void gcry_check_version(IntPtr zero)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public static void gcry_cipher_open(ref IntPtr intPtr, object gcryCipherAes256, object gcryCipherModeEcb, int i)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
internal static int gcry_cipher_setkey(IntPtr h, IntPtr pSeed32, IntPtr n32)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
internal static void gcry_cipher_close(IntPtr h)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
internal static int gcry_cipher_encrypt(IntPtr h, IntPtr pData32, IntPtr n32, IntPtr zero1, IntPtr zero2)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public static string GetUserRuntimeDir()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
internal enum MemoryProtectionScope
|
||||
{
|
||||
CrossProcess,
|
||||
SameLogon,
|
||||
SameProcess
|
||||
}
|
||||
|
||||
internal static class ProtectedMemory
|
||||
{
|
||||
public static byte[] Protect(byte[] userData, MemoryProtectionScope scope)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public static byte[] Unprotect(byte[] userData, MemoryProtectionScope scope)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
460
ModernKeePassLib/Native/NativeLib.cs
Normal file
460
ModernKeePassLib/Native/NativeLib.cs
Normal file
@@ -0,0 +1,460 @@
|
||||
/*
|
||||
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.Reflection;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
#if !KeePassUAP
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
#if !ModernKeePassLib
|
||||
using System.Windows.Forms;
|
||||
#endif
|
||||
#endif
|
||||
|
||||
using ModernKeePassLib.Utility;
|
||||
|
||||
namespace ModernKeePassLib.Native
|
||||
{
|
||||
/// <summary>
|
||||
/// Interface to native library (library containing fast versions of
|
||||
/// several cryptographic functions).
|
||||
/// </summary>
|
||||
public static class NativeLib
|
||||
{
|
||||
private static bool m_bAllowNative = true;
|
||||
|
||||
/// <summary>
|
||||
/// If this property is set to <c>true</c>, the native library is used.
|
||||
/// If it is <c>false</c>, all calls to functions in this class will fail.
|
||||
/// </summary>
|
||||
public static bool AllowNative
|
||||
{
|
||||
get { return m_bAllowNative; }
|
||||
set { m_bAllowNative = value; }
|
||||
}
|
||||
|
||||
private static ulong? m_ouMonoVersion = null;
|
||||
public static ulong MonoVersion
|
||||
{
|
||||
get
|
||||
{
|
||||
if(m_ouMonoVersion.HasValue) return m_ouMonoVersion.Value;
|
||||
|
||||
ulong uVersion = 0;
|
||||
try
|
||||
{
|
||||
Type t = Type.GetType("Mono.Runtime");
|
||||
if(t != null)
|
||||
{
|
||||
MethodInfo mi = t.GetMethod("GetDisplayName",
|
||||
BindingFlags.NonPublic | BindingFlags.Static);
|
||||
if(mi != null)
|
||||
{
|
||||
string strName = (mi.Invoke(null, null) as string);
|
||||
if(!string.IsNullOrEmpty(strName))
|
||||
{
|
||||
Match m = Regex.Match(strName, "\\d+(\\.\\d+)+");
|
||||
if(m.Success)
|
||||
uVersion = StrUtil.ParseVersion(m.Value);
|
||||
else { Debug.Assert(false); }
|
||||
}
|
||||
else { Debug.Assert(false); }
|
||||
}
|
||||
else { Debug.Assert(false); }
|
||||
}
|
||||
}
|
||||
catch(Exception) { Debug.Assert(false); }
|
||||
|
||||
m_ouMonoVersion = uVersion;
|
||||
return uVersion;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determine if the native library is installed.
|
||||
/// </summary>
|
||||
/// <returns>Returns <c>true</c>, if the native library is installed.</returns>
|
||||
public static bool IsLibraryInstalled()
|
||||
{
|
||||
byte[] pDummy0 = new byte[32];
|
||||
byte[] pDummy1 = new byte[32];
|
||||
|
||||
// Save the native state
|
||||
bool bCachedNativeState = m_bAllowNative;
|
||||
|
||||
// Temporarily allow native functions and try to load the library
|
||||
m_bAllowNative = true;
|
||||
bool bResult = TransformKey256(pDummy0, pDummy1, 16);
|
||||
|
||||
// Pop native state and return result
|
||||
m_bAllowNative = bCachedNativeState;
|
||||
return bResult;
|
||||
}
|
||||
|
||||
private static bool? m_bIsUnix = null;
|
||||
public static bool IsUnix()
|
||||
{
|
||||
if(m_bIsUnix.HasValue) return m_bIsUnix.Value;
|
||||
|
||||
PlatformID p = GetPlatformID();
|
||||
|
||||
// Mono defines Unix as 128 in early .NET versions
|
||||
#if !KeePassLibSD
|
||||
m_bIsUnix = ((p == PlatformID.Unix) || (p == PlatformID.MacOSX) ||
|
||||
((int)p == 128));
|
||||
#else
|
||||
m_bIsUnix = (((int)p == 4) || ((int)p == 6) || ((int)p == 128));
|
||||
#endif
|
||||
return m_bIsUnix.Value;
|
||||
}
|
||||
|
||||
private static PlatformID? m_platID = null;
|
||||
public static PlatformID GetPlatformID()
|
||||
{
|
||||
if(m_platID.HasValue) return m_platID.Value;
|
||||
|
||||
#if KeePassUAP
|
||||
m_platID = EnvironmentExt.OSVersion.Platform;
|
||||
#else
|
||||
m_platID = Environment.OSVersion.Platform;
|
||||
#endif
|
||||
|
||||
#if (!KeePassLibSD && !KeePassUAP)
|
||||
// Mono returns PlatformID.Unix on Mac OS X, workaround this
|
||||
if(m_platID.Value == PlatformID.Unix)
|
||||
{
|
||||
if((RunConsoleApp("uname", null) ?? string.Empty).Trim().Equals(
|
||||
"Darwin", StrUtil.CaseIgnoreCmp))
|
||||
m_platID = PlatformID.MacOSX;
|
||||
}
|
||||
#endif
|
||||
|
||||
return m_platID.Value;
|
||||
}
|
||||
|
||||
private static DesktopType? m_tDesktop = null;
|
||||
public static DesktopType GetDesktopType()
|
||||
{
|
||||
if(!m_tDesktop.HasValue)
|
||||
{
|
||||
DesktopType t = DesktopType.None;
|
||||
if(!IsUnix()) t = DesktopType.Windows;
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
string strXdg = (Environment.GetEnvironmentVariable(
|
||||
"XDG_CURRENT_DESKTOP") ?? string.Empty).Trim();
|
||||
string strGdm = (Environment.GetEnvironmentVariable(
|
||||
"GDMSESSION") ?? string.Empty).Trim();
|
||||
StringComparison sc = StrUtil.CaseIgnoreCmp;
|
||||
|
||||
if(strXdg.Equals("Unity", sc))
|
||||
t = DesktopType.Unity;
|
||||
else if(strXdg.Equals("LXDE", sc))
|
||||
t = DesktopType.Lxde;
|
||||
else if(strXdg.Equals("XFCE", sc))
|
||||
t = DesktopType.Xfce;
|
||||
else if(strXdg.Equals("MATE", sc))
|
||||
t = DesktopType.Mate;
|
||||
else if(strXdg.Equals("X-Cinnamon", sc)) // Mint 18.3
|
||||
t = DesktopType.Cinnamon;
|
||||
else if(strXdg.Equals("Pantheon", sc)) // Elementary OS
|
||||
t = DesktopType.Pantheon;
|
||||
else if(strXdg.Equals("KDE", sc) || // Mint 16, Kubuntu 17.10
|
||||
strGdm.Equals("kde-plasma", sc)) // Ubuntu 12.04
|
||||
t = DesktopType.Kde;
|
||||
else if(strXdg.Equals("GNOME", sc))
|
||||
{
|
||||
if(strGdm.Equals("cinnamon", sc)) // Mint 13
|
||||
t = DesktopType.Cinnamon;
|
||||
else t = DesktopType.Gnome; // Fedora 27
|
||||
}
|
||||
else if(strXdg.Equals("ubuntu:GNOME", sc))
|
||||
t = DesktopType.Gnome;
|
||||
}
|
||||
catch(Exception) { Debug.Assert(false); }
|
||||
}
|
||||
|
||||
m_tDesktop = t;
|
||||
}
|
||||
|
||||
return m_tDesktop.Value;
|
||||
}
|
||||
|
||||
#if (!KeePassLibSD && !KeePassUAP)
|
||||
public static string RunConsoleApp(string strAppPath, string strParams)
|
||||
{
|
||||
return RunConsoleApp(strAppPath, strParams, null);
|
||||
}
|
||||
|
||||
public static string RunConsoleApp(string strAppPath, string strParams,
|
||||
string strStdInput)
|
||||
{
|
||||
return RunConsoleApp(strAppPath, strParams, strStdInput,
|
||||
(AppRunFlags.GetStdOutput | AppRunFlags.WaitForExit));
|
||||
}
|
||||
|
||||
private delegate string RunProcessDelegate();
|
||||
|
||||
public static string RunConsoleApp(string strAppPath, string strParams,
|
||||
string strStdInput, AppRunFlags f)
|
||||
{
|
||||
if(strAppPath == null) throw new ArgumentNullException("strAppPath");
|
||||
if(strAppPath.Length == 0) throw new ArgumentException("strAppPath");
|
||||
|
||||
bool bStdOut = ((f & AppRunFlags.GetStdOutput) != AppRunFlags.None);
|
||||
|
||||
RunProcessDelegate fnRun = delegate()
|
||||
{
|
||||
Process pToDispose = null;
|
||||
try
|
||||
{
|
||||
ProcessStartInfo psi = new ProcessStartInfo();
|
||||
|
||||
psi.CreateNoWindow = true;
|
||||
psi.FileName = strAppPath;
|
||||
psi.WindowStyle = ProcessWindowStyle.Hidden;
|
||||
psi.UseShellExecute = false;
|
||||
psi.RedirectStandardOutput = bStdOut;
|
||||
|
||||
if(strStdInput != null) psi.RedirectStandardInput = true;
|
||||
|
||||
if(!string.IsNullOrEmpty(strParams)) psi.Arguments = strParams;
|
||||
|
||||
Process p = Process.Start(psi);
|
||||
pToDispose = p;
|
||||
|
||||
if(strStdInput != null)
|
||||
{
|
||||
EnsureNoBom(p.StandardInput);
|
||||
|
||||
p.StandardInput.Write(strStdInput);
|
||||
p.StandardInput.Close();
|
||||
}
|
||||
|
||||
string strOutput = string.Empty;
|
||||
if(bStdOut) strOutput = p.StandardOutput.ReadToEnd();
|
||||
|
||||
if((f & AppRunFlags.WaitForExit) != AppRunFlags.None)
|
||||
p.WaitForExit();
|
||||
else if((f & AppRunFlags.GCKeepAlive) != AppRunFlags.None)
|
||||
{
|
||||
pToDispose = null; // Thread disposes it
|
||||
|
||||
Thread th = new Thread(delegate()
|
||||
{
|
||||
try { p.WaitForExit(); p.Dispose(); }
|
||||
catch(Exception) { Debug.Assert(false); }
|
||||
});
|
||||
th.Start();
|
||||
}
|
||||
|
||||
return strOutput;
|
||||
}
|
||||
#if DEBUG
|
||||
catch(Exception ex) { Debug.Assert(ex is ThreadAbortException); }
|
||||
#else
|
||||
catch(Exception) { }
|
||||
#endif
|
||||
finally
|
||||
{
|
||||
try { if(pToDispose != null) pToDispose.Dispose(); }
|
||||
catch(Exception) { Debug.Assert(false); }
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
||||
|
||||
#if !ModernKeePassLib
|
||||
if((f & AppRunFlags.DoEvents) != AppRunFlags.None)
|
||||
{
|
||||
List<Form> lDisabledForms = new List<Form>();
|
||||
if((f & AppRunFlags.DisableForms) != AppRunFlags.None)
|
||||
{
|
||||
foreach(Form form in Application.OpenForms)
|
||||
{
|
||||
if(!form.Enabled) continue;
|
||||
|
||||
lDisabledForms.Add(form);
|
||||
form.Enabled = false;
|
||||
}
|
||||
}
|
||||
|
||||
IAsyncResult ar = fnRun.BeginInvoke(null, null);
|
||||
|
||||
while(!ar.AsyncWaitHandle.WaitOne(0))
|
||||
{
|
||||
Application.DoEvents();
|
||||
Thread.Sleep(2);
|
||||
}
|
||||
|
||||
string strRet = fnRun.EndInvoke(ar);
|
||||
|
||||
for(int i = lDisabledForms.Count - 1; i >= 0; --i)
|
||||
lDisabledForms[i].Enabled = true;
|
||||
|
||||
return strRet;
|
||||
}
|
||||
#endif
|
||||
return fnRun();
|
||||
}
|
||||
|
||||
private static void EnsureNoBom(StreamWriter sw)
|
||||
{
|
||||
if(sw == null) { Debug.Assert(false); return; }
|
||||
if(!MonoWorkarounds.IsRequired(1219)) return;
|
||||
|
||||
try
|
||||
{
|
||||
Encoding enc = sw.Encoding;
|
||||
if(enc == null) { Debug.Assert(false); return; }
|
||||
byte[] pbBom = enc.GetPreamble();
|
||||
if((pbBom == null) || (pbBom.Length == 0)) return;
|
||||
|
||||
// For Mono >= 4.0 (using Microsoft's reference source)
|
||||
try
|
||||
{
|
||||
FieldInfo fi = typeof(StreamWriter).GetField("haveWrittenPreamble",
|
||||
BindingFlags.Instance | BindingFlags.NonPublic);
|
||||
if(fi != null)
|
||||
{
|
||||
fi.SetValue(sw, true);
|
||||
return;
|
||||
}
|
||||
}
|
||||
catch(Exception) { Debug.Assert(false); }
|
||||
|
||||
// For Mono < 4.0
|
||||
FieldInfo fiPD = typeof(StreamWriter).GetField("preamble_done",
|
||||
BindingFlags.Instance | BindingFlags.NonPublic);
|
||||
if(fiPD != null) fiPD.SetValue(sw, true);
|
||||
else { Debug.Assert(false); }
|
||||
}
|
||||
catch(Exception) { Debug.Assert(false); }
|
||||
}
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// Transform a key.
|
||||
/// </summary>
|
||||
/// <param name="pBuf256">Source and destination buffer.</param>
|
||||
/// <param name="pKey256">Key to use in the transformation.</param>
|
||||
/// <param name="uRounds">Number of transformation rounds.</param>
|
||||
/// <returns>Returns <c>true</c>, if the key was transformed successfully.</returns>
|
||||
public static bool TransformKey256(byte[] pBuf256, byte[] pKey256,
|
||||
ulong uRounds)
|
||||
{
|
||||
#if KeePassUAP || ModernKeePassLib
|
||||
return false;
|
||||
#else
|
||||
if(!m_bAllowNative) return false;
|
||||
|
||||
KeyValuePair<IntPtr, IntPtr> kvp = PrepareArrays256(pBuf256, pKey256);
|
||||
bool bResult = false;
|
||||
|
||||
try
|
||||
{
|
||||
bResult = NativeMethods.TransformKey(kvp.Key, kvp.Value, uRounds);
|
||||
}
|
||||
catch(Exception) { bResult = false; }
|
||||
|
||||
if(bResult) GetBuffers256(kvp, pBuf256, pKey256);
|
||||
|
||||
FreeArrays(kvp);
|
||||
return bResult;
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Benchmark key transformation.
|
||||
/// </summary>
|
||||
/// <param name="uTimeMs">Number of milliseconds to perform the benchmark.</param>
|
||||
/// <param name="puRounds">Number of transformations done.</param>
|
||||
/// <returns>Returns <c>true</c>, if the benchmark was successful.</returns>
|
||||
public static bool TransformKeyBenchmark256(uint uTimeMs, out ulong puRounds)
|
||||
{
|
||||
puRounds = 0;
|
||||
|
||||
#if KeePassUAP || ModernKeePassLib
|
||||
return false;
|
||||
#else
|
||||
if(!m_bAllowNative) return false;
|
||||
|
||||
try { puRounds = NativeMethods.TransformKeyBenchmark(uTimeMs); }
|
||||
catch(Exception) { return false; }
|
||||
|
||||
return true;
|
||||
#endif
|
||||
}
|
||||
|
||||
private static KeyValuePair<IntPtr, IntPtr> PrepareArrays256(byte[] pBuf256,
|
||||
byte[] pKey256)
|
||||
{
|
||||
Debug.Assert((pBuf256 != null) && (pBuf256.Length == 32));
|
||||
if(pBuf256 == null) throw new ArgumentNullException("pBuf256");
|
||||
if(pBuf256.Length != 32) throw new ArgumentException();
|
||||
|
||||
Debug.Assert((pKey256 != null) && (pKey256.Length == 32));
|
||||
if(pKey256 == null) throw new ArgumentNullException("pKey256");
|
||||
if(pKey256.Length != 32) throw new ArgumentException();
|
||||
|
||||
IntPtr hBuf = Marshal.AllocHGlobal(pBuf256.Length);
|
||||
Marshal.Copy(pBuf256, 0, hBuf, pBuf256.Length);
|
||||
|
||||
IntPtr hKey = Marshal.AllocHGlobal(pKey256.Length);
|
||||
Marshal.Copy(pKey256, 0, hKey, pKey256.Length);
|
||||
|
||||
return new KeyValuePair<IntPtr, IntPtr>(hBuf, hKey);
|
||||
}
|
||||
|
||||
private static void GetBuffers256(KeyValuePair<IntPtr, IntPtr> kvpSource,
|
||||
byte[] pbDestBuf, byte[] pbDestKey)
|
||||
{
|
||||
if(kvpSource.Key != IntPtr.Zero)
|
||||
Marshal.Copy(kvpSource.Key, pbDestBuf, 0, pbDestBuf.Length);
|
||||
|
||||
if(kvpSource.Value != IntPtr.Zero)
|
||||
Marshal.Copy(kvpSource.Value, pbDestKey, 0, pbDestKey.Length);
|
||||
}
|
||||
|
||||
private static void FreeArrays(KeyValuePair<IntPtr, IntPtr> kvpPointers)
|
||||
{
|
||||
if(kvpPointers.Key != IntPtr.Zero)
|
||||
Marshal.FreeHGlobal(kvpPointers.Key);
|
||||
|
||||
if(kvpPointers.Value != IntPtr.Zero)
|
||||
Marshal.FreeHGlobal(kvpPointers.Value);
|
||||
}
|
||||
|
||||
internal static Type GetUwpType(string strType)
|
||||
{
|
||||
if(string.IsNullOrEmpty(strType)) { Debug.Assert(false); return null; }
|
||||
|
||||
// https://referencesource.microsoft.com/#mscorlib/system/runtime/interopservices/windowsruntime/winrtclassactivator.cs
|
||||
return Type.GetType(strType + ", Windows, ContentType=WindowsRuntime", false);
|
||||
}
|
||||
}
|
||||
}
|
216
ModernKeePassLib/Native/NativeMethods.Unix.cs
Normal file
216
ModernKeePassLib/Native/NativeMethods.Unix.cs
Normal file
@@ -0,0 +1,216 @@
|
||||
/*
|
||||
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.Reflection;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
|
||||
#if !KeePassUAP
|
||||
using System.Windows.Forms;
|
||||
#endif
|
||||
|
||||
namespace ModernKeePassLib.Native
|
||||
{
|
||||
internal static partial class NativeMethods
|
||||
{
|
||||
#if (!KeePassLibSD && !KeePassUAP)
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
private struct XClassHint
|
||||
{
|
||||
public IntPtr res_name;
|
||||
public IntPtr res_class;
|
||||
}
|
||||
|
||||
[DllImport("libX11")]
|
||||
private static extern int XSetClassHint(IntPtr display, IntPtr window, IntPtr class_hints);
|
||||
|
||||
private static Type m_tXplatUIX11 = null;
|
||||
private static Type GetXplatUIX11Type(bool bThrowOnError)
|
||||
{
|
||||
if(m_tXplatUIX11 == null)
|
||||
{
|
||||
// CheckState is in System.Windows.Forms
|
||||
string strTypeCS = typeof(CheckState).AssemblyQualifiedName;
|
||||
string strTypeX11 = strTypeCS.Replace("CheckState", "XplatUIX11");
|
||||
m_tXplatUIX11 = Type.GetType(strTypeX11, bThrowOnError, true);
|
||||
}
|
||||
|
||||
return m_tXplatUIX11;
|
||||
}
|
||||
|
||||
private static Type m_tHwnd = null;
|
||||
private static Type GetHwndType(bool bThrowOnError)
|
||||
{
|
||||
if(m_tHwnd == null)
|
||||
{
|
||||
// CheckState is in System.Windows.Forms
|
||||
string strTypeCS = typeof(CheckState).AssemblyQualifiedName;
|
||||
string strTypeHwnd = strTypeCS.Replace("CheckState", "Hwnd");
|
||||
m_tHwnd = Type.GetType(strTypeHwnd, bThrowOnError, true);
|
||||
}
|
||||
|
||||
return m_tHwnd;
|
||||
}
|
||||
|
||||
internal static void SetWmClass(Form f, string strName, string strClass)
|
||||
{
|
||||
if(f == null) { Debug.Assert(false); return; }
|
||||
|
||||
// The following crashes under Mac OS X (SIGSEGV in native code,
|
||||
// not just an exception), thus skip it when we're on Mac OS X;
|
||||
// https://sourceforge.net/projects/keepass/forums/forum/329221/topic/5860588
|
||||
if(NativeLib.GetPlatformID() == PlatformID.MacOSX) return;
|
||||
|
||||
try
|
||||
{
|
||||
Type tXplatUIX11 = GetXplatUIX11Type(true);
|
||||
FieldInfo fiDisplayHandle = tXplatUIX11.GetField("DisplayHandle",
|
||||
BindingFlags.NonPublic | BindingFlags.Static);
|
||||
IntPtr hDisplay = (IntPtr)fiDisplayHandle.GetValue(null);
|
||||
|
||||
Type tHwnd = GetHwndType(true);
|
||||
MethodInfo miObjectFromHandle = tHwnd.GetMethod("ObjectFromHandle",
|
||||
BindingFlags.Public | BindingFlags.Static);
|
||||
object oHwnd = miObjectFromHandle.Invoke(null, new object[] { f.Handle });
|
||||
|
||||
FieldInfo fiWholeWindow = tHwnd.GetField("whole_window",
|
||||
BindingFlags.NonPublic | BindingFlags.Instance);
|
||||
IntPtr hWindow = (IntPtr)fiWholeWindow.GetValue(oHwnd);
|
||||
|
||||
XClassHint xch = new XClassHint();
|
||||
xch.res_name = Marshal.StringToCoTaskMemAnsi(strName ?? string.Empty);
|
||||
xch.res_class = Marshal.StringToCoTaskMemAnsi(strClass ?? string.Empty);
|
||||
IntPtr pXch = Marshal.AllocCoTaskMem(Marshal.SizeOf(xch));
|
||||
Marshal.StructureToPtr(xch, pXch, false);
|
||||
|
||||
XSetClassHint(hDisplay, hWindow, pXch);
|
||||
|
||||
Marshal.FreeCoTaskMem(pXch);
|
||||
Marshal.FreeCoTaskMem(xch.res_name);
|
||||
Marshal.FreeCoTaskMem(xch.res_class);
|
||||
}
|
||||
catch(Exception) { Debug.Assert(false); }
|
||||
}
|
||||
#endif
|
||||
|
||||
// =============================================================
|
||||
// LibGCrypt 1.8.1
|
||||
|
||||
private const string LibGCrypt = "libgcrypt.so.20";
|
||||
|
||||
internal const int GCRY_CIPHER_AES256 = 9;
|
||||
internal const int GCRY_CIPHER_MODE_ECB = 1;
|
||||
|
||||
[DllImport(LibGCrypt)]
|
||||
internal static extern IntPtr gcry_check_version(IntPtr lpReqVersion);
|
||||
|
||||
[DllImport(LibGCrypt)]
|
||||
internal static extern uint gcry_cipher_open(ref IntPtr ph, int nAlgo,
|
||||
int nMode, uint uFlags);
|
||||
|
||||
[DllImport(LibGCrypt)]
|
||||
internal static extern void gcry_cipher_close(IntPtr h);
|
||||
|
||||
[DllImport(LibGCrypt)]
|
||||
internal static extern uint gcry_cipher_setkey(IntPtr h, IntPtr pbKey,
|
||||
IntPtr cbKey); // cbKey is size_t
|
||||
|
||||
[DllImport(LibGCrypt)]
|
||||
internal static extern uint gcry_cipher_encrypt(IntPtr h, IntPtr pbOut,
|
||||
IntPtr cbOut, IntPtr pbIn, IntPtr cbIn); // cb* are size_t
|
||||
|
||||
/* internal static IntPtr Utf8ZFromString(string str)
|
||||
{
|
||||
byte[] pb = StrUtil.Utf8.GetBytes(str ?? string.Empty);
|
||||
|
||||
IntPtr p = Marshal.AllocCoTaskMem(pb.Length + 1);
|
||||
if(p != IntPtr.Zero)
|
||||
{
|
||||
Marshal.Copy(pb, 0, p, pb.Length);
|
||||
Marshal.WriteByte(p, pb.Length, 0);
|
||||
}
|
||||
else { Debug.Assert(false); }
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
internal static string Utf8ZToString(IntPtr p)
|
||||
{
|
||||
if(p == IntPtr.Zero) { Debug.Assert(false); return null; }
|
||||
|
||||
List<byte> l = new List<byte>();
|
||||
for(int i = 0; i < int.MaxValue; ++i)
|
||||
{
|
||||
byte bt = Marshal.ReadByte(p, i);
|
||||
if(bt == 0) break;
|
||||
|
||||
l.Add(bt);
|
||||
}
|
||||
|
||||
return StrUtil.Utf8.GetString(l.ToArray());
|
||||
}
|
||||
|
||||
internal static void Utf8ZFree(IntPtr p)
|
||||
{
|
||||
if(p != IntPtr.Zero) Marshal.FreeCoTaskMem(p);
|
||||
} */
|
||||
|
||||
/* // =============================================================
|
||||
// LibGLib 2
|
||||
|
||||
private const string LibGLib = "libglib-2.0.so.0";
|
||||
|
||||
internal const int G_FALSE = 0;
|
||||
|
||||
// https://developer.gnome.org/glib/stable/glib-Memory-Allocation.html
|
||||
[DllImport(LibGLib)]
|
||||
internal static extern void g_free(IntPtr pMem); // pMem may be null
|
||||
|
||||
// =============================================================
|
||||
// LibGTK 3 (3.22.11 / 3.22.24)
|
||||
|
||||
private const string LibGtk = "libgtk-3.so.0";
|
||||
|
||||
internal static readonly IntPtr GDK_SELECTION_PRIMARY = new IntPtr(1);
|
||||
internal static readonly IntPtr GDK_SELECTION_CLIPBOARD = new IntPtr(69);
|
||||
|
||||
[DllImport(LibGtk)]
|
||||
internal static extern int gtk_init_check(IntPtr pArgc, IntPtr pArgv);
|
||||
|
||||
[DllImport(LibGtk)]
|
||||
// The returned handle is owned by GTK and must not be freed
|
||||
internal static extern IntPtr gtk_clipboard_get(IntPtr pSelection);
|
||||
|
||||
[DllImport(LibGtk)]
|
||||
internal static extern void gtk_clipboard_clear(IntPtr hClipboard);
|
||||
|
||||
[DllImport(LibGtk)]
|
||||
internal static extern IntPtr gtk_clipboard_wait_for_text(IntPtr hClipboard);
|
||||
|
||||
[DllImport(LibGtk)]
|
||||
internal static extern void gtk_clipboard_set_text(IntPtr hClipboard,
|
||||
IntPtr lpText, int cbLen);
|
||||
|
||||
[DllImport(LibGtk)]
|
||||
internal static extern void gtk_clipboard_store(IntPtr hClipboard); */
|
||||
}
|
||||
}
|
260
ModernKeePassLib/Native/NativeMethods.cs
Normal file
260
ModernKeePassLib/Native/NativeMethods.cs
Normal file
@@ -0,0 +1,260 @@
|
||||
/*
|
||||
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.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
|
||||
using ModernKeePassLib.Utility;
|
||||
|
||||
namespace ModernKeePassLib.Native
|
||||
{
|
||||
internal static partial class NativeMethods
|
||||
{
|
||||
internal const int MAX_PATH = 260;
|
||||
|
||||
internal const long INVALID_HANDLE_VALUE = -1;
|
||||
|
||||
internal const uint MOVEFILE_REPLACE_EXISTING = 0x00000001;
|
||||
internal const uint MOVEFILE_COPY_ALLOWED = 0x00000002;
|
||||
|
||||
internal const uint FILE_SUPPORTS_TRANSACTIONS = 0x00200000;
|
||||
internal const int MAX_TRANSACTION_DESCRIPTION_LENGTH = 64;
|
||||
|
||||
// internal const uint TF_SFT_SHOWNORMAL = 0x00000001;
|
||||
// internal const uint TF_SFT_HIDDEN = 0x00000008;
|
||||
|
||||
/* [DllImport("KeePassNtv32.dll", EntryPoint = "TransformKey")]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
private static extern bool TransformKey32(IntPtr pBuf256,
|
||||
IntPtr pKey256, UInt64 uRounds);
|
||||
|
||||
[DllImport("KeePassNtv64.dll", EntryPoint = "TransformKey")]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
private static extern bool TransformKey64(IntPtr pBuf256,
|
||||
IntPtr pKey256, UInt64 uRounds);
|
||||
|
||||
internal static bool TransformKey(IntPtr pBuf256, IntPtr pKey256,
|
||||
UInt64 uRounds)
|
||||
{
|
||||
if(IntPtr.Size == 4)
|
||||
return TransformKey32(pBuf256, pKey256, uRounds);
|
||||
return TransformKey64(pBuf256, pKey256, uRounds);
|
||||
}
|
||||
|
||||
[DllImport("KeePassNtv32.dll", EntryPoint = "TransformKeyTimed")]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
private static extern bool TransformKeyTimed32(IntPtr pBuf256,
|
||||
IntPtr pKey256, ref UInt64 puRounds, UInt32 uSeconds);
|
||||
|
||||
[DllImport("KeePassNtv64.dll", EntryPoint = "TransformKeyTimed")]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
private static extern bool TransformKeyTimed64(IntPtr pBuf256,
|
||||
IntPtr pKey256, ref UInt64 puRounds, UInt32 uSeconds);
|
||||
|
||||
internal static bool TransformKeyTimed(IntPtr pBuf256, IntPtr pKey256,
|
||||
ref UInt64 puRounds, UInt32 uSeconds)
|
||||
{
|
||||
if(IntPtr.Size == 4)
|
||||
return TransformKeyTimed32(pBuf256, pKey256, ref puRounds, uSeconds);
|
||||
return TransformKeyTimed64(pBuf256, pKey256, ref puRounds, uSeconds);
|
||||
} */
|
||||
|
||||
#if !KeePassUAP
|
||||
[DllImport("KeePassLibC32.dll", EntryPoint = "TransformKey256")]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
private static extern bool TransformKey32(IntPtr pBuf256,
|
||||
IntPtr pKey256, UInt64 uRounds);
|
||||
|
||||
[DllImport("KeePassLibC64.dll", EntryPoint = "TransformKey256")]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
private static extern bool TransformKey64(IntPtr pBuf256,
|
||||
IntPtr pKey256, UInt64 uRounds);
|
||||
|
||||
internal static bool TransformKey(IntPtr pBuf256, IntPtr pKey256,
|
||||
UInt64 uRounds)
|
||||
{
|
||||
if(IntPtr.Size == 4)
|
||||
return TransformKey32(pBuf256, pKey256, uRounds);
|
||||
return TransformKey64(pBuf256, pKey256, uRounds);
|
||||
}
|
||||
|
||||
[DllImport("KeePassLibC32.dll", EntryPoint = "TransformKeyBenchmark256")]
|
||||
private static extern UInt64 TransformKeyBenchmark32(UInt32 uTimeMs);
|
||||
|
||||
[DllImport("KeePassLibC64.dll", EntryPoint = "TransformKeyBenchmark256")]
|
||||
private static extern UInt64 TransformKeyBenchmark64(UInt32 uTimeMs);
|
||||
|
||||
internal static UInt64 TransformKeyBenchmark(UInt32 uTimeMs)
|
||||
{
|
||||
if(IntPtr.Size == 4)
|
||||
return TransformKeyBenchmark32(uTimeMs);
|
||||
return TransformKeyBenchmark64(uTimeMs);
|
||||
}
|
||||
#endif
|
||||
|
||||
/* [DllImport("KeePassLibC32.dll", EntryPoint = "TF_ShowLangBar")]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
private static extern bool TF_ShowLangBar32(UInt32 dwFlags);
|
||||
|
||||
[DllImport("KeePassLibC64.dll", EntryPoint = "TF_ShowLangBar")]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
private static extern bool TF_ShowLangBar64(UInt32 dwFlags);
|
||||
|
||||
internal static bool TfShowLangBar(uint dwFlags)
|
||||
{
|
||||
if(IntPtr.Size == 4) return TF_ShowLangBar32(dwFlags);
|
||||
return TF_ShowLangBar64(dwFlags);
|
||||
} */
|
||||
|
||||
[DllImport("KeePassLibC32.dll", EntryPoint = "ProtectProcessWithDacl")]
|
||||
private static extern void ProtectProcessWithDacl32();
|
||||
|
||||
[DllImport("KeePassLibC64.dll", EntryPoint = "ProtectProcessWithDacl")]
|
||||
private static extern void ProtectProcessWithDacl64();
|
||||
|
||||
internal static void ProtectProcessWithDacl()
|
||||
{
|
||||
try
|
||||
{
|
||||
if(NativeLib.IsUnix()) return;
|
||||
|
||||
if(IntPtr.Size == 4) ProtectProcessWithDacl32();
|
||||
else ProtectProcessWithDacl64();
|
||||
}
|
||||
catch(Exception) { Debug.Assert(false); }
|
||||
}
|
||||
|
||||
[DllImport("Kernel32.dll", SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
internal static extern bool CloseHandle(IntPtr hObject);
|
||||
|
||||
[DllImport("Kernel32.dll", CharSet = CharSet.Auto, ExactSpelling = false,
|
||||
SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
internal static extern bool GetVolumeInformation(string lpRootPathName,
|
||||
StringBuilder lpVolumeNameBuffer, UInt32 nVolumeNameSize,
|
||||
ref UInt32 lpVolumeSerialNumber, ref UInt32 lpMaximumComponentLength,
|
||||
ref UInt32 lpFileSystemFlags, StringBuilder lpFileSystemNameBuffer,
|
||||
UInt32 nFileSystemNameSize);
|
||||
|
||||
[DllImport("Kernel32.dll", CharSet = CharSet.Auto, ExactSpelling = false,
|
||||
SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
internal static extern bool MoveFileEx(string lpExistingFileName,
|
||||
string lpNewFileName, UInt32 dwFlags);
|
||||
|
||||
[DllImport("KtmW32.dll", CharSet = CharSet.Unicode, ExactSpelling = true,
|
||||
SetLastError = true)]
|
||||
internal static extern IntPtr CreateTransaction(IntPtr lpTransactionAttributes,
|
||||
IntPtr lpUOW, UInt32 dwCreateOptions, UInt32 dwIsolationLevel,
|
||||
UInt32 dwIsolationFlags, UInt32 dwTimeout, string lpDescription);
|
||||
|
||||
[DllImport("KtmW32.dll", SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
internal static extern bool CommitTransaction(IntPtr hTransaction);
|
||||
|
||||
[DllImport("Kernel32.dll", CharSet = CharSet.Auto, ExactSpelling = false,
|
||||
SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
internal static extern bool MoveFileTransacted(string lpExistingFileName,
|
||||
string lpNewFileName, IntPtr lpProgressRoutine, IntPtr lpData,
|
||||
UInt32 dwFlags, IntPtr hTransaction);
|
||||
|
||||
#if (!KeePassLibSD && !KeePassUAP)
|
||||
[DllImport("ShlWApi.dll", CharSet = CharSet.Auto)]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
internal static extern bool PathRelativePathTo([Out] StringBuilder pszPath,
|
||||
[In] string pszFrom, uint dwAttrFrom, [In] string pszTo, uint dwAttrTo);
|
||||
|
||||
[DllImport("ShlWApi.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
|
||||
private static extern int StrCmpLogicalW(string x, string y);
|
||||
|
||||
private static bool? m_obSupportsLogicalCmp = null;
|
||||
|
||||
private static void TestNaturalComparisonsSupport()
|
||||
{
|
||||
try
|
||||
{
|
||||
StrCmpLogicalW("0", "0"); // Throws exception if unsupported
|
||||
m_obSupportsLogicalCmp = true;
|
||||
}
|
||||
catch(Exception) { m_obSupportsLogicalCmp = false; }
|
||||
}
|
||||
#endif
|
||||
|
||||
internal static bool SupportsStrCmpNaturally
|
||||
{
|
||||
get
|
||||
{
|
||||
#if (!KeePassLibSD && !KeePassUAP)
|
||||
if(!m_obSupportsLogicalCmp.HasValue)
|
||||
TestNaturalComparisonsSupport();
|
||||
|
||||
return m_obSupportsLogicalCmp.Value;
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
internal static int StrCmpNaturally(string x, string y)
|
||||
{
|
||||
#if (!KeePassLibSD && !KeePassUAP)
|
||||
if(!NativeMethods.SupportsStrCmpNaturally)
|
||||
{
|
||||
Debug.Assert(false);
|
||||
return string.Compare(x, y, true);
|
||||
}
|
||||
|
||||
return StrCmpLogicalW(x, y);
|
||||
#else
|
||||
Debug.Assert(false);
|
||||
return string.Compare(x, y, true);
|
||||
#endif
|
||||
}
|
||||
|
||||
internal static string GetUserRuntimeDir()
|
||||
{
|
||||
#if KeePassLibSD
|
||||
return Path.GetTempPath();
|
||||
#else
|
||||
#if KeePassUAP
|
||||
string strRtDir = EnvironmentExt.AppDataLocalFolderPath;
|
||||
#else
|
||||
string strRtDir = Environment.GetEnvironmentVariable("XDG_RUNTIME_DIR");
|
||||
if(string.IsNullOrEmpty(strRtDir))
|
||||
strRtDir = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
|
||||
if(string.IsNullOrEmpty(strRtDir))
|
||||
{
|
||||
Debug.Assert(false);
|
||||
return Path.GetTempPath(); // Not UrlUtil (otherwise cyclic)
|
||||
}
|
||||
#endif
|
||||
|
||||
strRtDir = UrlUtil.EnsureTerminatingSeparator(strRtDir, false);
|
||||
strRtDir += PwDefs.ShortProductName;
|
||||
|
||||
return strRtDir;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user