mirror of
https://github.com/wismna/ModernKeePass.git
synced 2025-10-03 23:50:18 -04:00
WIP Lib version 2.39.1
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-2018 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);
|
||||
}
|
||||
}
|
||||
}
|
@@ -53,24 +53,6 @@ namespace ModernKeePassLib.Native
|
||||
set { m_bAllowNative = value; }
|
||||
}
|
||||
|
||||
private static int? g_oiPointerSize = null;
|
||||
/// <summary>
|
||||
/// Size of a native pointer (in bytes).
|
||||
/// </summary>
|
||||
public static int PointerSize
|
||||
{
|
||||
get
|
||||
{
|
||||
if(!g_oiPointerSize.HasValue)
|
||||
#if KeePassUAP
|
||||
g_oiPointerSize = Marshal.SizeOf<IntPtr>();
|
||||
#else
|
||||
g_oiPointerSize = Marshal.SizeOf(typeof(IntPtr));
|
||||
#endif
|
||||
return g_oiPointerSize.Value;
|
||||
}
|
||||
}
|
||||
|
||||
private static ulong? m_ouMonoVersion = null;
|
||||
public static ulong MonoVersion
|
||||
{
|
||||
@@ -195,19 +177,21 @@ namespace ModernKeePassLib.Native
|
||||
t = DesktopType.Xfce;
|
||||
else if(strXdg.Equals("MATE", sc))
|
||||
t = DesktopType.Mate;
|
||||
else if(strXdg.Equals("X-Cinnamon", sc))
|
||||
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
|
||||
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;
|
||||
else t = DesktopType.Gnome; // Fedora 27
|
||||
}
|
||||
else if(strXdg.Equals("ubuntu:GNOME", sc))
|
||||
t = DesktopType.Gnome;
|
||||
}
|
||||
catch(Exception) { Debug.Assert(false); }
|
||||
}
|
||||
@@ -243,6 +227,7 @@ namespace ModernKeePassLib.Native
|
||||
|
||||
RunProcessDelegate fnRun = delegate()
|
||||
{
|
||||
Process pToDispose = null;
|
||||
try
|
||||
{
|
||||
ProcessStartInfo psi = new ProcessStartInfo();
|
||||
@@ -258,6 +243,7 @@ namespace ModernKeePassLib.Native
|
||||
if(!string.IsNullOrEmpty(strParams)) psi.Arguments = strParams;
|
||||
|
||||
Process p = Process.Start(psi);
|
||||
pToDispose = p;
|
||||
|
||||
if(strStdInput != null)
|
||||
{
|
||||
@@ -274,9 +260,11 @@ namespace ModernKeePassLib.Native
|
||||
p.WaitForExit();
|
||||
else if((f & AppRunFlags.GCKeepAlive) != AppRunFlags.None)
|
||||
{
|
||||
pToDispose = null; // Thread disposes it
|
||||
|
||||
Thread th = new Thread(delegate()
|
||||
{
|
||||
try { p.WaitForExit(); }
|
||||
try { p.WaitForExit(); p.Dispose(); }
|
||||
catch(Exception) { Debug.Assert(false); }
|
||||
});
|
||||
th.Start();
|
||||
@@ -289,6 +277,11 @@ namespace ModernKeePassLib.Native
|
||||
#else
|
||||
catch(Exception) { }
|
||||
#endif
|
||||
finally
|
||||
{
|
||||
try { if(pToDispose != null) pToDispose.Dispose(); }
|
||||
catch(Exception) { Debug.Assert(false); }
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
||||
|
@@ -31,6 +31,14 @@ namespace ModernKeePassLib.Native
|
||||
{
|
||||
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;
|
||||
|
||||
@@ -47,10 +55,9 @@ namespace ModernKeePassLib.Native
|
||||
internal static bool TransformKey(IntPtr pBuf256, IntPtr pKey256,
|
||||
UInt64 uRounds)
|
||||
{
|
||||
if(Marshal.SizeOf(typeof(IntPtr)) == 8)
|
||||
return TransformKey64(pBuf256, pKey256, uRounds);
|
||||
else
|
||||
if(IntPtr.Size == 4)
|
||||
return TransformKey32(pBuf256, pKey256, uRounds);
|
||||
return TransformKey64(pBuf256, pKey256, uRounds);
|
||||
}
|
||||
|
||||
[DllImport("KeePassNtv32.dll", EntryPoint = "TransformKeyTimed")]
|
||||
@@ -66,10 +73,9 @@ namespace ModernKeePassLib.Native
|
||||
internal static bool TransformKeyTimed(IntPtr pBuf256, IntPtr pKey256,
|
||||
ref UInt64 puRounds, UInt32 uSeconds)
|
||||
{
|
||||
if(Marshal.SizeOf(typeof(IntPtr)) == 8)
|
||||
return TransformKeyTimed64(pBuf256, pKey256, ref puRounds, uSeconds);
|
||||
else
|
||||
if(IntPtr.Size == 4)
|
||||
return TransformKeyTimed32(pBuf256, pKey256, ref puRounds, uSeconds);
|
||||
return TransformKeyTimed64(pBuf256, pKey256, ref puRounds, uSeconds);
|
||||
} */
|
||||
|
||||
#if !KeePassUAP
|
||||
@@ -86,10 +92,9 @@ namespace ModernKeePassLib.Native
|
||||
internal static bool TransformKey(IntPtr pBuf256, IntPtr pKey256,
|
||||
UInt64 uRounds)
|
||||
{
|
||||
if(NativeLib.PointerSize == 8)
|
||||
return TransformKey64(pBuf256, pKey256, uRounds);
|
||||
else
|
||||
if(IntPtr.Size == 4)
|
||||
return TransformKey32(pBuf256, pKey256, uRounds);
|
||||
return TransformKey64(pBuf256, pKey256, uRounds);
|
||||
}
|
||||
|
||||
[DllImport("KeePassLibC32.dll", EntryPoint = "TransformKeyBenchmark256")]
|
||||
@@ -100,9 +105,9 @@ namespace ModernKeePassLib.Native
|
||||
|
||||
internal static UInt64 TransformKeyBenchmark(UInt32 uTimeMs)
|
||||
{
|
||||
if(NativeLib.PointerSize == 8)
|
||||
return TransformKeyBenchmark64(uTimeMs);
|
||||
return TransformKeyBenchmark32(uTimeMs);
|
||||
if(IntPtr.Size == 4)
|
||||
return TransformKeyBenchmark32(uTimeMs);
|
||||
return TransformKeyBenchmark64(uTimeMs);
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -116,11 +121,64 @@ namespace ModernKeePassLib.Native
|
||||
|
||||
internal static bool TfShowLangBar(uint dwFlags)
|
||||
{
|
||||
if(Marshal.SizeOf(typeof(IntPtr)) == 8)
|
||||
return TF_ShowLangBar64(dwFlags);
|
||||
return TF_ShowLangBar32(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)]
|
||||
|
Reference in New Issue
Block a user