/// <summary> /// Safely clear the clipboard. The clipboard clearing method of the /// .NET framework sets the clipboard to an empty <c>DataObject</c> when /// invoking the clearing method -- this might cause incompatibilities /// with other applications. Therefore, the <c>Clear</c> method of /// <c>ClipboardUtil</c> first tries to clear the clipboard using /// native Windows functions (which *really* clear the clipboard). /// </summary> public static void Clear() { bool bNativeSuccess = false; try { if (!NativeLib.IsUnix()) // Windows { if (OpenW(IntPtr.Zero, true)) // Clears the clipboard { CloseW(); bNativeSuccess = true; } } else if (NativeLib.GetPlatformID() == PlatformID.MacOSX) { SetStringM(string.Empty); bNativeSuccess = true; } else if (NativeLib.IsUnix()) { SetStringU(string.Empty); bNativeSuccess = true; } } catch (Exception) { Debug.Assert(false); } if (bNativeSuccess) { return; } try { Clipboard.Clear(); } // Fallback to .NET framework method catch (Exception) { Debug.Assert(false); } }
/// <summary> /// Safely clear the clipboard. The clipboard clearing method /// of the .NET Framework stores an empty <c>DataObject</c> /// in the clipboard; this can cause incompatibilities with /// other applications. Therefore, the <c>Clear</c> method of /// <c>ClipboardUtil</c> first tries to clear the clipboard using /// native Windows functions (which *really* clear the clipboard). /// </summary> public static void Clear() { // Ensure that there's no infinite recursion if (!g_csClearing.TryEnter()) { Debug.Assert(false); return; } // In some situations (e.g. when running in a VM, when using // a clipboard extension utility, ...) the clipboard cannot // be cleared; for this case we first overwrite the clipboard // with a non-sensitive text try { Copy("--", false, false, null, null, IntPtr.Zero); } catch (Exception) { Debug.Assert(false); } bool bNativeSuccess = false; try { if (!NativeLib.IsUnix()) // Windows { if (OpenW(IntPtr.Zero, true)) // Clears the clipboard { CloseW(); bNativeSuccess = true; } } else if (NativeLib.GetPlatformID() == PlatformID.MacOSX) { SetStringM(string.Empty); bNativeSuccess = true; } else if (NativeLib.IsUnix()) { SetStringU(string.Empty); bNativeSuccess = true; } } catch (Exception) { Debug.Assert(false); } g_pbDataHash = null; g_csClearing.Exit(); if (bNativeSuccess) { return; } Debug.Assert(false); try { Clipboard.Clear(); } // Fallback; empty data object catch (Exception) { Debug.Assert(false); } }
private static byte[] HashClipboard() { try { SHA256Managed sha256 = new SHA256Managed(); if (m_strFormat != null) { if (ContainsData(m_strFormat)) { byte[] pbData; if (m_bEncoded) { pbData = GetEncodedData(m_strFormat, IntPtr.Zero); } else { pbData = GetData(m_strFormat); } if (pbData == null) { Debug.Assert(false); return(null); } return(sha256.ComputeHash(pbData)); } } else if (NativeLib.GetPlatformID() == PlatformID.MacOSX) { string strData = GetStringM(); byte[] pbUtf8 = StrUtil.Utf8.GetBytes(strData); return(sha256.ComputeHash(pbUtf8)); } else if (NativeLib.IsUnix()) { string strData = GetStringU(); byte[] pbUtf8 = StrUtil.Utf8.GetBytes(strData); return(sha256.ComputeHash(pbUtf8)); } else if (Clipboard.ContainsText()) { string strData = Clipboard.GetText(); byte[] pbUtf8 = StrUtil.Utf8.GetBytes(strData); return(sha256.ComputeHash(pbUtf8)); } } catch (Exception) { Debug.Assert(false); } return(null); }
public static string GetText() { if (!NativeLib.IsUnix()) // Windows { return(Clipboard.GetText()); } if (NativeLib.GetPlatformID() == PlatformID.MacOSX) { return(GetStringM()); } if (NativeLib.IsUnix()) { return(GetStringU()); } Debug.Assert(false); return(Clipboard.GetText()); }
public static byte[] GetEncodedData(string strFormat, IntPtr hOwner) { try { if (!NativeLib.IsUnix()) // Windows { if (!OpenW(hOwner, false)) { throw new InvalidOperationException(); } string str = GetStringW(strFormat, false); CloseW(); if (str == null) { return(null); } if (str.Length == 0) { return(new byte[0]); } return(StrUtil.DataUriToData(str)); } else if (NativeLib.GetPlatformID() == PlatformID.MacOSX) { return(StrUtil.DataUriToData(GetStringM())); } else if (NativeLib.IsUnix()) { return(StrUtil.DataUriToData(GetStringU())); } // else // Managed, no encoding // { // return GetData(strFormat); // } } catch (Exception) { Debug.Assert(false); } return(null); }
public static bool Copy(string strToCopy, bool bSprCompile, bool bIsEntryInfo, PwEntry peEntryInfo, PwDatabase pwReferenceSource, IntPtr hOwner) { if (strToCopy == null) { throw new ArgumentNullException("strToCopy"); } if (bIsEntryInfo && !AppPolicy.Try(AppPolicyId.CopyToClipboard)) { return(false); } string strData = (bSprCompile ? SprEngine.Compile(strToCopy, new SprContext(peEntryInfo, pwReferenceSource, SprCompileFlags.All)) : strToCopy); try { if (!NativeLib.IsUnix()) // Windows { if (!OpenW(hOwner, true)) { throw new InvalidOperationException(); } bool bFailed = false; if (!AttachIgnoreFormatW()) { bFailed = true; } if (!SetDataW(null, strData, null)) { bFailed = true; } CloseW(); if (bFailed) { return(false); } } else if (NativeLib.GetPlatformID() == PlatformID.MacOSX) { SetStringM(strData); } else if (NativeLib.IsUnix()) { SetStringU(strData); } // else // Managed // { // Clear(); // DataObject doData = CreateProtectedDataObject(strData); // Clipboard.SetDataObject(doData); // } } catch (Exception) { Debug.Assert(false); return(false); } m_strFormat = null; byte[] pbUtf8 = StrUtil.Utf8.GetBytes(strData); SHA256Managed sha256 = new SHA256Managed(); m_pbDataHash32 = sha256.ComputeHash(pbUtf8); RaiseCopyEvent(bIsEntryInfo, strData); if (peEntryInfo != null) { peEntryInfo.Touch(false); } // SprEngine.Compile might have modified the database Program.MainForm.UpdateUI(false, null, false, null, false, null, false); return(true); }
public static byte[] ComputeHash() { try // This works always or never { bool bOpened = OpenW(IntPtr.Zero, false); // The following seems to work even without opening the // clipboard, but opening maybe is safer uint u = NativeMethods.GetClipboardSequenceNumber(); if (bOpened) { CloseW(); } if (u == 0) { throw new UnauthorizedAccessException(); } SHA256Managed sha256 = new SHA256Managed(); return(sha256.ComputeHash(MemUtil.UInt32ToBytes(u))); } catch (Exception) { Debug.Assert(false); } if (NativeLib.GetPlatformID() == PlatformID.MacOSX) { string str = GetStringM(); byte[] pbText = StrUtil.Utf8.GetBytes("pb" + str); return((new SHA256Managed()).ComputeHash(pbText)); } if (NativeLib.IsUnix()) { string str = GetStringU(); byte[] pbText = StrUtil.Utf8.GetBytes("pb" + str); return((new SHA256Managed()).ComputeHash(pbText)); } try { MemoryStream ms = new MemoryStream(); byte[] pbPre = StrUtil.Utf8.GetBytes("pb"); ms.Write(pbPre, 0, pbPre.Length); // Prevent empty buffer if (Clipboard.ContainsAudio()) { Stream sAudio = Clipboard.GetAudioStream(); MemUtil.CopyStream(sAudio, ms); sAudio.Close(); } if (Clipboard.ContainsFileDropList()) { StringCollection sc = Clipboard.GetFileDropList(); foreach (string str in sc) { byte[] pbStr = StrUtil.Utf8.GetBytes(str); ms.Write(pbStr, 0, pbStr.Length); } } if (Clipboard.ContainsImage()) { using (Image img = Clipboard.GetImage()) { MemoryStream msImage = new MemoryStream(); img.Save(msImage, ImageFormat.Bmp); byte[] pbImg = msImage.ToArray(); ms.Write(pbImg, 0, pbImg.Length); msImage.Close(); } } if (Clipboard.ContainsText()) { string str = Clipboard.GetText(); byte[] pbText = StrUtil.Utf8.GetBytes(str); ms.Write(pbText, 0, pbText.Length); } byte[] pbData = ms.ToArray(); SHA256Managed sha256 = new SHA256Managed(); byte[] pbHash = sha256.ComputeHash(pbData); ms.Close(); return(pbHash); } catch (Exception) { Debug.Assert(false); } return(null); }
public static bool Copy(byte[] pbToCopy, string strFormat, bool bEncode, bool bIsEntryInfo, IntPtr hOwner) { Debug.Assert(pbToCopy != null); if (pbToCopy == null) { throw new ArgumentNullException("pbToCopy"); } if (bIsEntryInfo && !AppPolicy.Try(AppPolicyId.CopyToClipboard)) { return(false); } string strEnc = null; try { if (bEncode || NativeLib.IsUnix()) { strEnc = StrUtil.DataToDataUri(pbToCopy, ClipFmtToMimeType(strFormat)); } if (!NativeLib.IsUnix()) // Windows { if (!OpenW(hOwner, true)) { throw new InvalidOperationException(); } uint uFormat = NativeMethods.RegisterClipboardFormat(strFormat); bool bFailed = false; if (!AttachIgnoreFormatW()) { bFailed = true; } if (!bEncode) { if (!SetDataW(uFormat, pbToCopy)) { bFailed = true; } } else // Encode { if (!SetDataW(uFormat, strEnc, false)) { bFailed = true; } } CloseW(); if (bFailed) { return(false); } } else if (NativeLib.GetPlatformID() == PlatformID.MacOSX) { SetStringM(strEnc); } else if (NativeLib.IsUnix()) { SetStringU(strEnc); } // else // Managed, no encoding // { // Clear(); // DataObject doData = CreateProtectedDataObject(strFormat, pbToCopy); // Clipboard.SetDataObject(doData); // } } catch (Exception) { Debug.Assert(false); return(false); } m_strFormat = strFormat; m_bEncoded = (strEnc != null); SHA256Managed sha256 = new SHA256Managed(); // if(strEnc != null) // m_pbDataHash32 = sha256.ComputeHash(StrUtil.Utf8.GetBytes(strEnc)); // else m_pbDataHash32 = sha256.ComputeHash(pbToCopy); RaiseCopyEvent(bIsEntryInfo, string.Empty); return(true); }
public static bool Copy(string strToCopy, bool bSprCompile, bool bIsEntryInfo, PwEntry peEntryInfo, PwDatabase pwReferenceSource, IntPtr hOwner) { if (strToCopy == null) { throw new ArgumentNullException("strToCopy"); } if (strToCopy.Length == 0) { Clear(); return(true); } if (bIsEntryInfo && !AppPolicy.Try(AppPolicyId.CopyToClipboard)) { return(false); } string strData = strToCopy; if (bSprCompile) { strData = SprEngine.Compile(strData, new SprContext( peEntryInfo, pwReferenceSource, SprCompileFlags.All)); } try { // if(SetStringUwp(strData)) { } else if (!NativeLib.IsUnix()) // Windows { if (!OpenW(hOwner, true)) { throw new InvalidOperationException(); } bool bFailed = false; if (!AttachIgnoreFormatsW()) { bFailed = true; } if (!SetDataW(null, strData, null)) { bFailed = true; } CloseW(); if (bFailed) { return(false); } } else if (NativeLib.GetPlatformID() == PlatformID.MacOSX) { SetStringM(strData); } else if (NativeLib.IsUnix()) { SetStringU(strData); } else { Debug.Assert(false); Clipboard.SetText(strData); } } catch (Exception) { Debug.Assert(false); return(false); } g_pbDataHash = HashString(strData); if (peEntryInfo != null) { peEntryInfo.Touch(false); } if (bIsEntryInfo) { Program.TriggerSystem.RaiseEvent(EcasEventIDs.CopiedEntryInfo, EcasProperty.Text, strData); } // SprEngine.Compile might have modified the database MainForm mf = Program.MainForm; if ((mf != null) && bSprCompile) { mf.RefreshEntriesList(); mf.UpdateUI(false, null, false, null, false, null, false); } return(true); }