WIP KeePassLibPCL - problem with awaitables

This commit is contained in:
2017-09-22 18:48:09 +02:00
parent a43bc20eb3
commit 668afbe817
108 changed files with 1205 additions and 4203 deletions

View File

@@ -27,7 +27,7 @@ using System.Diagnostics;
using System.IO.Compression;
#endif
namespace ModernKeePassLib.Utility
namespace ModernKeePassLibPCL.Utility
{
/// <summary>
/// Application-wide logging services.

View File

@@ -3,7 +3,7 @@ using System.Drawing;
using System.Globalization;
using System.Text.RegularExpressions;
namespace KeePass2PCL.Utility
namespace ModernKeePassLibPCL.Utility
{
/// <summary>
/// Replacement for System.Drawing.ColorTranslator.

View File

@@ -21,7 +21,7 @@ using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
#if KeePass2PCL
#if ModernKeePassLibPCL
using Splat;
#else
using System.Drawing;
@@ -29,7 +29,7 @@ using System.Drawing.Imaging;
#endif
using System.Diagnostics;
namespace KeePass2PCL.Utility
namespace ModernKeePassLibPCL.Utility
{
public static class GfxUtil
{
@@ -40,7 +40,7 @@ namespace KeePass2PCL.Utility
try { return Image.FromStream(ms); }
finally { ms.Close(); }
}
#elif KeePass2PCL
#elif ModernKeePassLibPCL
public static IBitmap LoadImage(byte[] pb)
{
using (var ms = new MemoryStream(pb, false)) {

View File

@@ -29,7 +29,7 @@ using System.IO.Compression;
using KeePassLibSD;
#endif
namespace ModernKeePassLib.Utility
namespace ModernKeePassLibPCL.Utility
{
/// <summary>
/// Contains static buffer manipulation and string conversion routines.

View File

@@ -24,10 +24,10 @@ using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using ModernKeePassLib.Resources;
using ModernKeePassLib.Serialization;
using ModernKeePassLibPCL.Resources;
using ModernKeePassLibPCL.Serialization;
namespace ModernKeePassLib.Utility
namespace ModernKeePassLibPCL.Utility
{
public sealed class MessageServiceEventArgs : EventArgs
{

View File

@@ -25,9 +25,9 @@ using System.ComponentModel;
using System.Reflection;
using System.Diagnostics;
using KeePass2PCL.Native;
using ModernKeePassLibPCL.Native;
namespace KeePass2PCL.Utility
namespace ModernKeePassLibPCL.Utility
{
public static class MonoWorkarounds
{

View File

@@ -21,26 +21,24 @@ using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.IO;
using System.Text.RegularExpressions;
#if PCL
using Windows.Security.Cryptography;
#if ModernKeePassLibPCL
using PCLCrypto;
#else
using System.Security.Cryptography;
#endif
using System.Globalization;
using System.Diagnostics;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Security.Cryptography.DataProtection;
using Windows.Storage.Streams;
using Windows.UI;
using ModernKeePassLib.Collections;
using ModernKeePassLib.Cryptography.PasswordGenerator;
using ModernKeePassLib.Security;
using ModernKeePassLib.Resources;
using UnicodeEncoding = System.Text.UnicodeEncoding;
namespace ModernKeePassLib.Utility
using ModernKeePassLibPCL.Collections;
using ModernKeePassLibPCL.Cryptography.PasswordGenerator;
using ModernKeePassLibPCL.Native;
using ModernKeePassLibPCL.Security;
using ModernKeePassLibPCL.Resources;
namespace ModernKeePassLibPCL.Utility
{
/// <summary>
/// Character stream class.
@@ -225,7 +223,7 @@ namespace ModernKeePassLib.Utility
List<StrEncodingInfo> l = new List<StrEncodingInfo>();
l.Add(new StrEncodingInfo(StrEncodingType.Default,
#if PCL || KeePassRT
#if ModernKeePassLibPCL || KeePassRT
StrUtil.Utf8.WebName, StrUtil.Utf8, 1, null));
#else
#if !KeePassLibSD
@@ -236,7 +234,7 @@ namespace ModernKeePassLib.Utility
Encoding.Default,
(uint)Encoding.Default.GetBytes("a").Length, null));
#endif
#if !PCL && !KeePassRT
#if !ModernKeePassLibPCL && !KeePassRT
l.Add(new StrEncodingInfo(StrEncodingType.Ascii,
"ASCII", Encoding.ASCII, 1, null));
l.Add(new StrEncodingInfo(StrEncodingType.Utf7,
@@ -250,7 +248,7 @@ namespace ModernKeePassLib.Utility
l.Add(new StrEncodingInfo(StrEncodingType.Utf16BE,
"Unicode (UTF-16 BE)", new UnicodeEncoding(true, false),
2, new byte[] { 0xFE, 0xFF }));
#if (!PCL && !KeePassLibSD && !KeePassRT)
#if (!ModernKeePassLibPCL && !KeePassLibSD && !KeePassRT)
l.Add(new StrEncodingInfo(StrEncodingType.Utf32LE,
"Unicode (UTF-32 LE)", new UTF32Encoding(false, false),
4, new byte[] { 0xFF, 0xFE, 0x0, 0x0 }));
@@ -501,7 +499,7 @@ namespace ModernKeePassLib.Utility
if(excp.StackTrace != null)
strText += excp.StackTrace + Environment.NewLine;
#if !KeePassLibSD
#if !PCL && !KeePassRT
#if !ModernKeePassLibPCL && !KeePassRT
if(excp.TargetSite != null)
strText += excp.TargetSite.ToString() + MessageService.NewLine;
#endif
@@ -527,7 +525,7 @@ namespace ModernKeePassLib.Utility
if(excp.InnerException.StackTrace != null)
strText += excp.InnerException.StackTrace + Environment.NewLine;
#if !KeePassLibSD
#if !PCL && !KeePassRT
#if !ModernKeePassLibPCL && !KeePassRT
if(excp.InnerException.TargetSite != null)
strText += excp.InnerException.TargetSite.ToString();
#endif
@@ -772,15 +770,15 @@ namespace ModernKeePassLib.Utility
Debug.Assert(strY != null);
if(strY == null) throw new ArgumentNullException("strY");
/*if(NativeMethods.SupportsStrCmpNaturally)
return NativeMethods.StrCmpNaturally(strX, strY);*/
if(NativeMethods.SupportsStrCmpNaturally)
return NativeMethods.StrCmpNaturally(strX, strY);
strX = strX.ToLower(); // Case-insensitive comparison
strY = strY.ToLower();
if(m_rxNaturalSplit == null)
m_rxNaturalSplit = new Regex(@"([0-9]+)",
#if PCL || KeePassRT
#if ModernKeePassLibPCL || KeePassRT
RegexOptions.None);
#else
RegexOptions.Compiled);
@@ -1191,14 +1189,13 @@ namespace ModernKeePassLib.Utility
try
{
byte[] pbPlain = StrUtil.Utf8.GetBytes(strPlainText);
byte[] pbEnc = ProtectedData.Protect(pbPlain, m_pbOptEnt,
DataProtectionScope.CurrentUser);
var provider = new DataProtectionProvider("Local=user");
var pbEnc = provider.ProtectAsync(pbPlain.AsBuffer()).GetResults();
#if (!PCL && !KeePassLibSD && !KeePassRT)
#if (!ModernKeePassLibPCL && !KeePassLibSD && !KeePassRT)
return Convert.ToBase64String(pbEnc, Base64FormattingOptions.None);
#else
return Convert.ToBase64String(pbEnc.ToArray());
return Convert.ToBase64String(pbEnc);
#endif
}
catch(Exception) { Debug.Assert(false); }
@@ -1213,9 +1210,9 @@ namespace ModernKeePassLib.Utility
try
{
byte[] pbEnc = Convert.FromBase64String(strCipherText);
var provider = new DataProtectionProvider("Local=user");
var pbPlain = provider.UnprotectAsync(pbEnc.AsBuffer()).GetResults().ToArray();
byte[] pbPlain = ProtectedData.Unprotect(pbEnc, m_pbOptEnt,
DataProtectionScope.CurrentUser);
return StrUtil.Utf8.GetString(pbPlain, 0, pbPlain.Length);
}
catch(Exception) { Debug.Assert(false); }
@@ -1308,7 +1305,7 @@ namespace ModernKeePassLib.Utility
Array.Reverse(pb);
for(int i = 0; i < pb.Length; ++i) pb[i] = (byte)(pb[i] ^ 0x65);
#if (!PCL && !KeePassLibSD && !KeePassRT)
#if (!ModernKeePassLibPCL && !KeePassLibSD && !KeePassRT)
return Convert.ToBase64String(pb, Base64FormattingOptions.None);
#else
return Convert.ToBase64String(pb);
@@ -1468,7 +1465,7 @@ namespace ModernKeePassLib.Utility
if(strMimeType == null) strMimeType = "application/octet-stream";
#if (!PCL && !KeePassLibSD && !KeePassRT)
#if (!ModernKeePassLibPCL && !KeePassLibSD && !KeePassRT)
return ("data:" + strMimeType + ";base64," + Convert.ToBase64String(
pbData, Base64FormattingOptions.None));
#else
@@ -1499,7 +1496,7 @@ namespace ModernKeePassLib.Utility
MemoryStream ms = new MemoryStream();
#if PCL || KeePassRT
#if ModernKeePassLibPCL || KeePassRT
Encoding enc = StrUtil.Utf8;
#else
Encoding enc = Encoding.ASCII;

View File

@@ -0,0 +1,17 @@
using System.IO;
using Windows.Storage.Streams;
namespace ModernKeePassLibPCL.Utility
{
public static class StreamExtensions
{
public static Stream AsStream(this IRandomAccessStream inputStream)
{
var reader = new DataReader(inputStream.GetInputStreamAt(0));
var bytes = new byte[inputStream.Size];
reader.LoadAsync((uint)inputStream.Size).GetResults();
reader.ReadBytes(bytes);
return new MemoryStream(bytes);
}
}
}

View File

@@ -23,9 +23,9 @@ using System.Text;
using System.Globalization;
using System.Diagnostics;
using ModernKeePassLib.Interfaces;
using ModernKeePassLibPCL.Interfaces;
namespace ModernKeePassLib.Utility
namespace ModernKeePassLibPCL.Utility
{
/// <summary>
/// Contains various static time structure manipulation and conversion

View File

@@ -24,11 +24,13 @@ using System.Runtime.InteropServices;
using System.Text;
using System.Diagnostics;
#if PCL
using Windows.Storage;
#if ModernKeePassLibPCL
using PCLStorage;
#endif
namespace ModernKeePassLib.Utility
using ModernKeePassLibPCL.Native;
namespace ModernKeePassLibPCL.Utility
{
/// <summary>
/// A class containing various static path utility helper methods (like
@@ -38,7 +40,7 @@ namespace ModernKeePassLib.Utility
{
private static readonly char[] m_vDirSeps = new char[] {
'\\', '/', UrlUtil.LocalDirSepChar };
#if !PCL
#if !ModernKeePassLibPCL
private static readonly char[] m_vPathTrimCharsWs = new char[] {
'\"', ' ', '\t', '\r', '\n' };
#endif
@@ -47,9 +49,8 @@ namespace ModernKeePassLib.Utility
{
#if KeePassRT
get { return '\\'; }
#elif PCL
//get { return PortablePath.DirectorySeparatorChar; }
get { return '\\'; }
#elif ModernKeePassLibPCL
get { return PortablePath.DirectorySeparatorChar; }
#else
get { return Path.DirectorySeparatorChar; }
#endif
@@ -264,7 +265,7 @@ namespace ModernKeePassLib.Utility
public static bool UnhideFile(string strFile)
{
#if (PCL || KeePassLibSD || KeePassRT)
#if (ModernKeePassLibPCL || KeePassLibSD || KeePassRT)
return false;
#else
if(strFile == null) throw new ArgumentNullException("strFile");
@@ -284,7 +285,7 @@ namespace ModernKeePassLib.Utility
public static bool HideFile(string strFile, bool bHide)
{
#if (PCL || KeePassLibSD || KeePassRT)
#if (ModernKeePassLibPCL || KeePassLibSD || KeePassRT)
return false;
#else
if(strFile == null) throw new ArgumentNullException("strFile");
@@ -325,7 +326,7 @@ namespace ModernKeePassLib.Utility
return strTargetFile;
}
#if (!PCL && !KeePassLibSD && !KeePassRT)
#if (!ModernKeePassLibPCL && !KeePassLibSD && !KeePassRT)
if(NativeLib.IsUnix())
#endif
{
@@ -358,7 +359,7 @@ namespace ModernKeePassLib.Utility
return sbRel.ToString();
}
#if (!PCL && !KeePassLibSD && !KeePassRT)
#if (!ModernKeePassLibPCL && !KeePassLibSD && !KeePassRT)
try // Windows
{
const int nMaxPath = NativeMethods.MAX_PATH * 2;
@@ -452,13 +453,12 @@ namespace ModernKeePassLib.Utility
try
{
#if KeePassRT
var dirT = StorageFolder.GetFolderFromPathAsync(
strPath).GetResults();
var dirT = Windows.Storage.StorageFolder.GetFolderFromPathAsync(
strPath).AwaitEx();
str = dirT.Path;
#elif ModernKeePassLibPCL
var dirT = FileSystem.Current.GetFolderFromPathAsync(strPath).Result;
str = dirT.Path;
#elif PCL
//var dirT = FileSystem.Current.GetFolderFromPathAsync(strPath).Result;
str = ApplicationData.Current.RoamingFolder.Path;
//str = dirT.Path;
#else
str = Path.GetFullPath(strPath);
#endif
@@ -631,7 +631,7 @@ namespace ModernKeePassLib.Utility
return false;
}
#if !PCL
#if !ModernKeePassLibPCL
public static string GetTempPath()
{
string strDir;
@@ -654,7 +654,7 @@ namespace ModernKeePassLib.Utility
}
#endif
#if !PCL && !KeePassLibSD
#if !ModernKeePassLibPCL && !KeePassLibSD
// Structurally mostly equivalent to UrlUtil.GetFileInfos
public static List<string> GetFilePaths(string strDir, string strPattern,
SearchOption opt)