Removed dependencies to UWP SDK

Added ImageSharp for image processing
This commit is contained in:
Geoffroy BONNEVILLE
2019-07-31 11:48:42 +02:00
parent fdba7c182d
commit 84adf3785b
15 changed files with 92 additions and 200 deletions

View File

@@ -1,9 +1,11 @@
using System; using System;
using System.IO; using System.IO;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks; using System.Threading.Tasks;
using ModernKeePassLib.Keys; using ModernKeePassLib.Keys;
using ModernKeePassLib.Utility; using ModernKeePassLib.Utility;
using Windows.Storage; using Windows.Storage;
using Windows.Storage.Streams;
using NUnit.Framework; using NUnit.Framework;
namespace ModernKeePassLib.Test.Keys namespace ModernKeePassLib.Test.Keys
@@ -58,15 +60,19 @@ namespace ModernKeePassLib.Test.Keys
sw.Write(ExpectedFileEnd); sw.Write(ExpectedFileEnd);
} }
var keyFile = new KcpKeyFile(_file); var fileBytes = (await FileIO.ReadBufferAsync(_file)).ToArray();
var keyFile = new KcpKeyFile(fileBytes);
var keyData = keyFile.KeyData.ReadData(); var keyData = keyFile.KeyData.ReadData();
Assert.That(MemUtil.ArraysEqual(keyData, expectedKeyData), Is.True); Assert.That(MemUtil.ArraysEqual(keyData, expectedKeyData), Is.True);
} }
[Test] [Test]
public async Task TestCreate() public async Task TestCreate()
{ {
KcpKeyFile.Create(_file, null); var fileBytes = KcpKeyFile.Create(null);
await FileIO.WriteBytesAsync(_file, fileBytes);
var fileContents = await FileIO.ReadTextAsync(_file); var fileContents = await FileIO.ReadTextAsync(_file);
Assert.That(fileContents.Length, Is.EqualTo(185)); Assert.That(fileContents.Length, Is.EqualTo(185));

View File

@@ -1,4 +1,6 @@
using NUnit.Framework; using System;
using ModernKeePassLib.Utility;
using NUnit.Framework;
namespace ModernKeePassLib.Test.Utility namespace ModernKeePassLib.Test.Utility
{ {
@@ -6,20 +8,20 @@ namespace ModernKeePassLib.Test.Utility
public class GfxUtilTests public class GfxUtilTests
{ {
// 16x16 all white PNG file, base64 encoded // 16x16 all white PNG file, base64 encoded
const string testImageData = private const string TestImageData =
"iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAAACXBIWXMAAAsTAAA" + "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAAACXBIWXMAAAsTAAA" +
"LEwEAmpwYAAAAB3RJTUUH3wMOFgIgmTCUMQAAABl0RVh0Q29tbWVudABDcmVhdG" + "LEwEAmpwYAAAAB3RJTUUH3wMOFgIgmTCUMQAAABl0RVh0Q29tbWVudABDcmVhdG" +
"VkIHdpdGggR0lNUFeBDhcAAAAaSURBVCjPY/z//z8DKYCJgUQwqmFUw9DRAABVb" + "VkIHdpdGggR0lNUFeBDhcAAAAaSURBVCjPY/z//z8DKYCJgUQwqmFUw9DRAABVb" +
"QMdny4VogAAAABJRU5ErkJggg=="; "QMdny4VogAAAABJRU5ErkJggg==";
//[Test] [Test]
//public void TestLoadImage () public void TestLoadImage()
//{ {
// var testData = Convert.FromBase64String (testImageData); var testData = Convert.FromBase64String(TestImageData);
// var image = GfxUtil.ScaleImage(testData, 16, 16, ScaleTransformFlags.UIIcon); //var image = GfxUtil.ScaleImage(testData, 16, 16, ScaleTransformFlags.UIIcon);
// //var image = GfxUtil.LoadImage(testData); var image = GfxUtil.LoadImage(testData);
// Assert.Equal(image.Width, 16); Assert.That(image.Width, Is.EqualTo(16));
// Assert.Equal(image.Height, 16); Assert.That(image.Height, Is.EqualTo(16));
//} }
} }
} }

View File

@@ -27,9 +27,6 @@ using System.Xml;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Xml.Linq; using System.Xml.Linq;
using Windows.Security.Cryptography;
using Windows.Security.Cryptography.Core;
using Windows.Storage;
#else #else
using System.Security.Cryptography; using System.Security.Cryptography;
#endif #endif
@@ -68,9 +65,9 @@ namespace ModernKeePassLib.Keys
get { return m_pbKeyData; } get { return m_pbKeyData; }
} }
#if ModernKeePassLib #if ModernKeePassLib
public KcpKeyFile(StorageFile keyFile) public KcpKeyFile(byte[] keyFile)
{ {
Construct(IOConnectionInfo.FromStorageFile(keyFile), false); Construct(IOConnectionInfo.FromByteArray(keyFile), false);
} }
#else #else
public KcpKeyFile(string strKeyFile) public KcpKeyFile(string strKeyFile)
@@ -192,7 +189,7 @@ namespace ModernKeePassLib.Keys
/// random number generator is used).</param> /// random number generator is used).</param>
/// <returns>Returns a <c>FileSaveResult</c> error code.</returns> /// <returns>Returns a <c>FileSaveResult</c> error code.</returns>
#if ModernKeePassLib #if ModernKeePassLib
public static void Create(StorageFile file, byte[] pbAdditionalEntropy) public static byte[] Create(byte[] pbAdditionalEntropy)
#else #else
public static void Create(string strFilePath, byte[] pbAdditionalEntropy) public static void Create(string strFilePath, byte[] pbAdditionalEntropy)
#endif #endif
@@ -215,7 +212,7 @@ namespace ModernKeePassLib.Keys
} }
#if ModernKeePassLib #if ModernKeePassLib
CreateXmlKeyFile(file, pbFinalKey32); return CreateXmlKeyFile(pbFinalKey32);
#else #else
CreateXmlKeyFile(strFilePath, pbFinalKey32); CreateXmlKeyFile(strFilePath, pbFinalKey32);
#endif #endif
@@ -281,10 +278,8 @@ namespace ModernKeePassLib.Keys
} }
#if ModernKeePassLib #if ModernKeePassLib
private static void CreateXmlKeyFile(StorageFile file, byte[] pbKeyData) private static byte[] CreateXmlKeyFile(byte[] pbKeyData)
{ {
Debug.Assert(file != null);
if (file == null) throw new ArgumentNullException(nameof(file));
#else #else
private static void CreateXmlKeyFile(string strFile, byte[] pbKeyData) private static void CreateXmlKeyFile(string strFile, byte[] pbKeyData)
{ {
@@ -295,7 +290,8 @@ namespace ModernKeePassLib.Keys
if(pbKeyData == null) throw new ArgumentNullException("pbKeyData"); if(pbKeyData == null) throw new ArgumentNullException("pbKeyData");
#if ModernKeePassLib #if ModernKeePassLib
var ioc = IOConnectionInfo.FromStorageFile(file); var fileContents = new byte[0];
var ioc = IOConnectionInfo.FromByteArray(fileContents);
#else #else
IOConnectionInfo ioc = IOConnectionInfo.FromPath(strFile); IOConnectionInfo ioc = IOConnectionInfo.FromPath(strFile);
#endif #endif
@@ -323,7 +319,10 @@ namespace ModernKeePassLib.Keys
xw.WriteEndElement(); // </KeyFile> xw.WriteEndElement(); // </KeyFile>
xw.WriteEndDocument(); xw.WriteEndDocument();
} }
#if ModernKeePassLib
return ((MemoryStream) s).ToArray();
#endif
} }
} }
} }
} }

View File

@@ -23,12 +23,10 @@ using System.IO;
using System.Security; using System.Security;
#if ModernKeePassLib #if ModernKeePassLib
using Windows.Storage;
using System.Security.Cryptography; using System.Security.Cryptography;
#endif #endif
using ModernKeePassLib.Cryptography; using ModernKeePassLib.Cryptography;
using ModernKeePassLib.Native;
using ModernKeePassLib.Resources; using ModernKeePassLib.Resources;
using ModernKeePassLib.Security; using ModernKeePassLib.Security;
using ModernKeePassLib.Utility; using ModernKeePassLib.Utility;
@@ -87,8 +85,8 @@ namespace ModernKeePassLib.Keys
private static string GetUserKeyFilePath(bool bCreate) private static string GetUserKeyFilePath(bool bCreate)
{ {
#if ModernKeePassLib #if KeePassUAP
string strUserDir = Windows.Storage.ApplicationData.Current.RoamingFolder.Path; string strUserDir = EnvironmentExt.AppDataRoamingFolderPath;
#else #else
string strUserDir = Environment.GetFolderPath( string strUserDir = Environment.GetFolderPath(
Environment.SpecialFolder.ApplicationData); Environment.SpecialFolder.ApplicationData);
@@ -97,11 +95,8 @@ namespace ModernKeePassLib.Keys
strUserDir = UrlUtil.EnsureTerminatingSeparator(strUserDir, false); strUserDir = UrlUtil.EnsureTerminatingSeparator(strUserDir, false);
strUserDir += PwDefs.ShortProductName; strUserDir += PwDefs.ShortProductName;
#if !ModernKeePassLib
if(bCreate && !Directory.Exists(strUserDir)) if(bCreate && !Directory.Exists(strUserDir))
Directory.CreateDirectory(strUserDir); Directory.CreateDirectory(strUserDir);
#endif
strUserDir = UrlUtil.EnsureTerminatingSeparator(strUserDir, false); strUserDir = UrlUtil.EnsureTerminatingSeparator(strUserDir, false);
return (strUserDir + UserKeyFileName); return (strUserDir + UserKeyFileName);
@@ -115,15 +110,7 @@ namespace ModernKeePassLib.Keys
try try
{ {
string strFilePath = GetUserKeyFilePath(false); string strFilePath = GetUserKeyFilePath(false);
#if ModernKeePassLib
var fileStream = StorageFile.GetFileFromPathAsync(strFilePath).GetAwaiter().GetResult().OpenStreamForReadAsync().GetAwaiter().GetResult();
var pbProtectedKey = new byte[(int)fileStream.Length];
fileStream.Read(pbProtectedKey, 0, (int)fileStream.Length);
fileStream.Dispose();
#else
byte[] pbProtectedKey = File.ReadAllBytes(strFilePath); byte[] pbProtectedKey = File.ReadAllBytes(strFilePath);
#endif
pbKey = CryptoUtil.UnprotectData(pbProtectedKey, m_pbEntropy, pbKey = CryptoUtil.UnprotectData(pbProtectedKey, m_pbEntropy,
DataProtectionScope.CurrentUser); DataProtectionScope.CurrentUser);
@@ -148,13 +135,8 @@ namespace ModernKeePassLib.Keys
byte[] pbRandomKey = CryptoRandom.Instance.GetRandomBytes(64); byte[] pbRandomKey = CryptoRandom.Instance.GetRandomBytes(64);
byte[] pbProtectedKey = CryptoUtil.ProtectData(pbRandomKey, byte[] pbProtectedKey = CryptoUtil.ProtectData(pbRandomKey,
m_pbEntropy, DataProtectionScope.CurrentUser); m_pbEntropy, DataProtectionScope.CurrentUser);
#if ModernKeePassLib
var fileStream = StorageFile.GetFileFromPathAsync(strFilePath).GetAwaiter().GetResult().OpenStreamForWriteAsync().GetAwaiter().GetResult();
fileStream.Write(pbProtectedKey, 0, (int)fileStream.Length);
fileStream.Dispose();
#else
File.WriteAllBytes(strFilePath, pbProtectedKey); File.WriteAllBytes(strFilePath, pbProtectedKey);
#endif
byte[] pbKey = LoadUserKey(true); byte[] pbKey = LoadUserKey(true);
Debug.Assert(MemUtil.ArraysEqual(pbKey, pbRandomKey)); Debug.Assert(MemUtil.ArraysEqual(pbKey, pbRandomKey));

View File

@@ -3,14 +3,14 @@
<PropertyGroup> <PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework> <TargetFramework>netstandard2.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>2.42.1</Version> <Version>2.42.1.1</Version>
<Authors>Geoffroy Bonneville</Authors> <Authors>Geoffroy Bonneville</Authors>
<PackageLicenseUrl>https://www.gnu.org/licenses/gpl-3.0.en.html</PackageLicenseUrl> <PackageLicense>https://www.gnu.org/licenses/gpl-3.0.en.html</PackageLicense>
<PackageProjectUrl>https://github.com/wismna/ModernKeePass</PackageProjectUrl> <PackageProjectUrl>https://github.com/wismna/ModernKeePassLib</PackageProjectUrl>
<Description>Portable KeePass Password Management Library that targets .Net Standard and WinRT. Allows reading, editing and writing to KeePass 2.x databases.</Description> <Description>Portable KeePass Password Management Library that targets .Net Standard. Allows reading, editing and writing to KeePass 2.x databases.</Description>
<Company>wismna</Company> <Company>wismna</Company>
<Product>ModernKeePassLib</Product> <Product>ModernKeePassLib</Product>
<PackageReleaseNotes>Update to version 2.42.1</PackageReleaseNotes> <PackageReleaseNotes>Removed dependency to UWP SDK, use ImageSharp for image processing</PackageReleaseNotes>
<PackageTags>KeePass KeePassLib Portable PCL NetStandard ModernKeePass</PackageTags> <PackageTags>KeePass KeePassLib Portable PCL NetStandard ModernKeePass</PackageTags>
<Copyright>Copyright © 2019 Geoffroy Bonneville</Copyright> <Copyright>Copyright © 2019 Geoffroy Bonneville</Copyright>
</PropertyGroup> </PropertyGroup>
@@ -54,15 +54,9 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="SixLabors.ImageSharp" Version="1.0.0-beta0006" />
<PackageReference Include="System.Runtime.WindowsRuntime" Version="4.3.0" /> <PackageReference Include="System.Runtime.WindowsRuntime" Version="4.3.0" />
<PackageReference Include="System.Security.Cryptography.ProtectedData" Version="4.5.0" /> <PackageReference Include="System.Security.Cryptography.ProtectedData" Version="4.5.0" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Reference Include="Windows">
<HintPath>..\..\..\..\..\..\Program Files (x86)\Windows Kits\10\UnionMetadata\10.0.18362.0\Windows.winmd</HintPath>
<IsWinMDFile>true</IsWinMDFile>
</Reference>
</ItemGroup>
</Project> </Project>

View File

@@ -20,13 +20,14 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
#if !ModernKeePassLib #if ModernKeePassLib
using System.Drawing; using SixLabors.ImageSharp;
#else #else
using Windows.UI.Xaml.Controls; using System.Drawing;
#endif #endif
using ModernKeePassLib.Utility; using ModernKeePassLib.Utility;
using SixLabors.ImageSharp.PixelFormats;
namespace ModernKeePassLib namespace ModernKeePassLib
{ {
@@ -38,8 +39,8 @@ namespace ModernKeePassLib
private readonly PwUuid m_pwUuid; private readonly PwUuid m_pwUuid;
private readonly byte[] m_pbImageDataPng; private readonly byte[] m_pbImageDataPng;
private readonly Image m_imgOrg; private readonly Image<Rgba32> m_imgOrg;
private Dictionary<long, Image> m_dImageCache = new Dictionary<long, Image>(); private Dictionary<long, Image<Rgba32>> m_dImageCache = new Dictionary<long, Image<Rgba32>>();
// Recommended maximum sizes, not obligatory // Recommended maximum sizes, not obligatory
internal const int MaxWidth = 128; internal const int MaxWidth = 128;
@@ -56,7 +57,7 @@ namespace ModernKeePassLib
} }
[Obsolete("Use GetImage instead.")] [Obsolete("Use GetImage instead.")]
public Image Image public Image<Rgba32> Image
{ {
#if (!KeePassLibSD && !KeePassUAP) #if (!KeePassLibSD && !KeePassUAP)
get { return GetImage(16, 16); } // Backward compatibility get { return GetImage(16, 16); } // Backward compatibility
@@ -96,7 +97,7 @@ namespace ModernKeePassLib
/// <summary> /// <summary>
/// Get the icon as an <c>Image</c> (original size). /// Get the icon as an <c>Image</c> (original size).
/// </summary> /// </summary>
public Image GetImage() public Image<Rgba32> GetImage()
{ {
return m_imgOrg; return m_imgOrg;
} }
@@ -107,7 +108,7 @@ namespace ModernKeePassLib
/// </summary> /// </summary>
/// <param name="w">Width of the returned image.</param> /// <param name="w">Width of the returned image.</param>
/// <param name="h">Height of the returned image.</param> /// <param name="h">Height of the returned image.</param>
public Image GetImage(int w, int h) public Image<Rgba32> GetImage(int w, int h)
{ {
if(w < 0) { Debug.Assert(false); return m_imgOrg; } if(w < 0) { Debug.Assert(false); return m_imgOrg; }
if(h < 0) { Debug.Assert(false); return m_imgOrg; } if(h < 0) { Debug.Assert(false); return m_imgOrg; }
@@ -115,7 +116,7 @@ namespace ModernKeePassLib
long lID = GetID(w, h); long lID = GetID(w, h);
Image img; Image<Rgba32> img;
if(m_dImageCache.TryGetValue(lID, out img)) return img; if(m_dImageCache.TryGetValue(lID, out img)) return img;
img = GfxUtil.ScaleImage(m_imgOrg, w, h, ScaleTransformFlags.UIIcon); img = GfxUtil.ScaleImage(m_imgOrg, w, h, ScaleTransformFlags.UIIcon);

View File

@@ -24,9 +24,7 @@ using System.IO;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
#if ModernKeePassLib #if ModernKeePassLib
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks; using System.Threading.Tasks;
using Windows.Storage.Streams;
#endif #endif
using ModernKeePassLib.Cryptography; using ModernKeePassLib.Cryptography;
using ModernKeePassLib.Resources; using ModernKeePassLib.Resources;

View File

@@ -23,13 +23,9 @@ using System.ComponentModel;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Text; using System.Text;
using Windows.Storage.AccessCache;
#if (!ModernKeePassLib && !KeePassLibSD && !KeePassRT) #if (!ModernKeePassLib && !KeePassLibSD && !KeePassRT)
using System.Security.AccessControl; using System.Security.AccessControl;
#endif #endif
#if ModernKeePassLib
using Windows.Storage;
#endif
using ModernKeePassLib.Cryptography; using ModernKeePassLib.Cryptography;
using ModernKeePassLib.Delegates; using ModernKeePassLib.Delegates;
@@ -237,7 +233,7 @@ namespace ModernKeePassLib.Serialization
FileSecurity sec = File.GetAccessControl(m_iocBase.Path, acs); FileSecurity sec = File.GetAccessControl(m_iocBase.Path, acs);
if(sec != null) pbSec = sec.GetSecurityDescriptorBinaryForm(); if(sec != null) pbSec = sec.GetSecurityDescriptorBinaryForm();
#endif #endif
} }
catch(Exception) { Debug.Assert(NativeLib.IsUnix()); } catch(Exception) { Debug.Assert(NativeLib.IsUnix()); }
// if((long)(faBase & FileAttributes.ReadOnly) != 0) // if((long)(faBase & FileAttributes.ReadOnly) != 0)
@@ -351,13 +347,7 @@ namespace ModernKeePassLib.Serialization
if((chT != chB) && !TxfIsSupported(chT)) return; if((chT != chB) && !TxfIsSupported(chT)) return;
m_iocTxfMidFallback = m_iocTemp; m_iocTxfMidFallback = m_iocTemp;
#if ModernKeePassLib
var tempFile = ApplicationData.Current.TemporaryFolder.CreateFileAsync(m_iocTemp.Path).GetAwaiter()
.GetResult();
m_iocTemp = IOConnectionInfo.FromStorageFile(tempFile);
#else
m_iocTemp = IOConnectionInfo.FromPath(strTemp); m_iocTemp = IOConnectionInfo.FromPath(strTemp);
#endif
m_lToDelete.Add(m_iocTemp); m_lToDelete.Add(m_iocTemp);
} }
@@ -370,9 +360,9 @@ namespace ModernKeePassLib.Serialization
if(TxfMoveWithTx()) return true; if(TxfMoveWithTx()) return true;
// Move the temporary file onto the base file's drive first, // Move the temporary file onto the base file's drive first,
// such that it cannot happen that both the base file and // such that it cannot happen that both the base file and
// the temporary file are deleted/corrupted // the temporary file are deleted/corrupted
#if !ModernKeePassLib #if !ModernKeePassLib
const uint f = (NativeMethods.MOVEFILE_COPY_ALLOWED | const uint f = (NativeMethods.MOVEFILE_COPY_ALLOWED |
NativeMethods.MOVEFILE_REPLACE_EXISTING); NativeMethods.MOVEFILE_REPLACE_EXISTING);
@@ -441,10 +431,6 @@ namespace ModernKeePassLib.Serialization
{ {
try try
{ {
#if ModernKeePassLib
ApplicationData.Current.TemporaryFolder.GetFileAsync(UrlUtil.GetTempPath()).GetAwaiter()
.GetResult().DeleteAsync().GetAwaiter().GetResult();
#else
// See also TxfPrepare method // See also TxfPrepare method
DirectoryInfo di = new DirectoryInfo(UrlUtil.GetTempPath()); DirectoryInfo di = new DirectoryInfo(UrlUtil.GetTempPath());
List<FileInfo> l = UrlUtil.GetFileInfos(di, StrTxfTempPrefix + List<FileInfo> l = UrlUtil.GetFileInfos(di, StrTxfTempPrefix +
@@ -460,7 +446,6 @@ namespace ModernKeePassLib.Serialization
if((DateTime.UtcNow - fi.LastWriteTimeUtc).TotalDays > 1.0) if((DateTime.UtcNow - fi.LastWriteTimeUtc).TotalDays > 1.0)
fi.Delete(); fi.Delete();
} }
#endif
} }
catch(Exception) { Debug.Assert(false); } catch(Exception) { Debug.Assert(false); }
} }

View File

@@ -33,10 +33,6 @@ using System.Net.Security;
using System.Security.Cryptography.X509Certificates; using System.Security.Cryptography.X509Certificates;
#endif #endif
#if ModernKeePassLib
using Windows.Storage;
using Windows.Storage.Streams;
#endif
using ModernKeePassLib.Native; using ModernKeePassLib.Native;
using ModernKeePassLib.Utility; using ModernKeePassLib.Utility;
@@ -600,7 +596,7 @@ namespace ModernKeePassLib.Serialization
private static Stream OpenReadLocal(IOConnectionInfo ioc) private static Stream OpenReadLocal(IOConnectionInfo ioc)
{ {
#if ModernKeePassLib #if ModernKeePassLib
return ioc.StorageFile.OpenAsync(FileAccessMode.Read).GetAwaiter().GetResult().AsStream(); return new MemoryStream(ioc.Bytes);
#else #else
return new FileStream(ioc.Path, FileMode.Open, FileAccess.Read, return new FileStream(ioc.Path, FileMode.Open, FileAccess.Read,
FileShare.Read); FileShare.Read);
@@ -639,12 +635,12 @@ namespace ModernKeePassLib.Serialization
private static Stream OpenWriteLocal(IOConnectionInfo ioc) private static Stream OpenWriteLocal(IOConnectionInfo ioc)
{ {
#if ModernKeePassLib #if ModernKeePassLib
return ioc.StorageFile.OpenAsync(FileAccessMode.ReadWrite).GetAwaiter().GetResult().AsStream(); return new MemoryStream();
#else #else
return new FileStream(ioc.Path, FileMode.Create, FileAccess.Write, return new FileStream(ioc.Path, FileMode.Create, FileAccess.Write,
FileShare.None); FileShare.None);
#endif #endif
} }
public static bool FileExists(IOConnectionInfo ioc) public static bool FileExists(IOConnectionInfo ioc)
{ {
@@ -658,7 +654,7 @@ namespace ModernKeePassLib.Serialization
RaiseIOAccessPreEvent(ioc, IOAccessType.Exists); RaiseIOAccessPreEvent(ioc, IOAccessType.Exists);
#if ModernKeePassLib #if ModernKeePassLib
return ioc.StorageFile != null; return ioc.Bytes != null;
#else #else
if(ioc.IsLocalFile()) return File.Exists(ioc.Path); if(ioc.IsLocalFile()) return File.Exists(ioc.Path);
@@ -700,7 +696,7 @@ namespace ModernKeePassLib.Serialization
#if ModernKeePassLib #if ModernKeePassLib
if (!ioc.IsLocalFile()) return; if (!ioc.IsLocalFile()) return;
ioc.StorageFile?.DeleteAsync().GetAwaiter().GetResult(); MemUtil.ZeroByteArray(ioc.Bytes);
#else #else
if(ioc.IsLocalFile()) { File.Delete(ioc.Path); return; } if(ioc.IsLocalFile()) { File.Delete(ioc.Path); return; }
@@ -737,10 +733,7 @@ namespace ModernKeePassLib.Serialization
{ {
RaiseIOAccessPreEvent(iocFrom, iocTo, IOAccessType.Move); RaiseIOAccessPreEvent(iocFrom, iocTo, IOAccessType.Move);
#if ModernKeePassLib #if !ModernKeePassLib
if (!iocFrom.IsLocalFile()) return;
iocFrom.StorageFile?.RenameAsync(iocTo.Path).GetAwaiter().GetResult();
#else
if(iocFrom.IsLocalFile()) { File.Move(iocFrom.Path, iocTo.Path); return; } if(iocFrom.IsLocalFile()) { File.Move(iocFrom.Path, iocTo.Path); return; }
#if !KeePassLibSD #if !KeePassLibSD

View File

@@ -24,9 +24,6 @@ using System.Diagnostics;
using System.IO; using System.IO;
using System.Text; using System.Text;
using System.Xml.Serialization; using System.Xml.Serialization;
#if ModernKeePassLib
using Windows.Storage;
#endif
using ModernKeePassLib.Interfaces; using ModernKeePassLib.Interfaces;
using ModernKeePassLib.Utility; using ModernKeePassLib.Utility;
@@ -67,9 +64,9 @@ namespace ModernKeePassLib.Serialization
{ {
// private IOFileFormatHint m_ioHint = IOFileFormatHint.None; // private IOFileFormatHint m_ioHint = IOFileFormatHint.None;
public StorageFile StorageFile { get; set; } public byte[] Bytes { get; private set; }
private string m_strUrl = string.Empty; private string m_strUrl = string.Empty;
public string Path public string Path
{ {
get { return m_strUrl; } get { return m_strUrl; }
@@ -118,7 +115,8 @@ namespace ModernKeePassLib.Serialization
} }
private IOCredSaveMode m_ioCredSaveMode = IOCredSaveMode.NoSave; private IOCredSaveMode m_ioCredSaveMode = IOCredSaveMode.NoSave;
public IOCredSaveMode CredSaveMode
public IOCredSaveMode CredSaveMode
{ {
get { return m_ioCredSaveMode; } get { return m_ioCredSaveMode; }
set { m_ioCredSaveMode = value; } set { m_ioCredSaveMode = value; }
@@ -315,16 +313,16 @@ namespace ModernKeePassLib.Serialization
} }
#if ModernKeePassLib #if ModernKeePassLib
public static IOConnectionInfo FromStorageFile(StorageFile file) public static IOConnectionInfo FromByteArray(byte[] bytes)
{ {
IOConnectionInfo ioc = new IOConnectionInfo(); IOConnectionInfo ioc = new IOConnectionInfo();
ioc.StorageFile = file; ioc.Bytes = bytes;
ioc.CredSaveMode = IOCredSaveMode.NoSave; ioc.CredSaveMode = IOCredSaveMode.NoSave;
return ioc; return ioc;
} }
#else #endif
public static IOConnectionInfo FromPath(string strPath) public static IOConnectionInfo FromPath(string strPath)
{ {
IOConnectionInfo ioc = new IOConnectionInfo(); IOConnectionInfo ioc = new IOConnectionInfo();
@@ -334,16 +332,11 @@ namespace ModernKeePassLib.Serialization
return ioc; return ioc;
} }
#endif
public bool CanProbablyAccess() public bool CanProbablyAccess()
{ {
#if ModernKeePassLib #if ModernKeePassLib
if (IsLocalFile()) if (IsLocalFile()) return Bytes != null;
{
//return (FileSystem.Current.GetFileFromPathAsync(m_strUrl).Result != null);
var file = StorageFile.GetFileFromPathAsync(m_strUrl).GetAwaiter().GetResult();
return file != null;
}
#else #else
if(IsLocalFile()) return File.Exists(m_strUrl); if(IsLocalFile()) return File.Exists(m_strUrl);
#endif #endif

View File

@@ -28,8 +28,6 @@ using System.Text;
using System.Xml; using System.Xml;
#if !ModernKeePassLib && !KeePassUAP #if !ModernKeePassLib && !KeePassUAP
using System.Security.Cryptography; using System.Security.Cryptography;
#else
using Windows.Storage;
#endif #endif
#if !KeePassLibSD #if !KeePassLibSD
@@ -62,9 +60,9 @@ namespace ModernKeePassLib.Serialization
/// <param name="fmt">Format.</param> /// <param name="fmt">Format.</param>
/// <param name="slLogger">Status logger (optional).</param> /// <param name="slLogger">Status logger (optional).</param>
#if ModernKeePassLib #if ModernKeePassLib
public void Load(StorageFile file, KdbxFormat fmt, IStatusLogger slLogger) public void Load(byte[] fileContents, KdbxFormat fmt, IStatusLogger slLogger)
{ {
IOConnectionInfo ioc = IOConnectionInfo.FromStorageFile(file); IOConnectionInfo ioc = IOConnectionInfo.FromByteArray(fileContents);
#else #else
public void Load(string strFilePath, KdbxFormat fmt, IStatusLogger slLogger) public void Load(string strFilePath, KdbxFormat fmt, IStatusLogger slLogger)
{ {

View File

@@ -30,10 +30,6 @@ using System.Xml;
using System.Security.Cryptography; using System.Security.Cryptography;
#endif #endif
#if ModernKeePassLib
using Windows.Storage;
#endif
using ModernKeePassLib.Collections; using ModernKeePassLib.Collections;
using ModernKeePassLib.Cryptography; using ModernKeePassLib.Cryptography;
using ModernKeePassLib.Cryptography.Cipher; using ModernKeePassLib.Cryptography.Cipher;
@@ -512,33 +508,11 @@ namespace ModernKeePassLib.Serialization
++iTry; ++iTry;
} }
#if ModernKeePassLib
while (StorageFile.GetFileFromPathAsync(strPath).GetResults() != null);
#else
while(File.Exists(strPath)); while(File.Exists(strPath));
#endif
#if ModernKeePassLib
byte[] pbData = pb.ReadData();
/*var file = FileSystem.Current.GetFileFromPathAsync(strPath).Result;
using (var stream = file.OpenAsync(FileAccess.ReadAndWrite).Result) {*/
var file = StorageFile.GetFileFromPathAsync(strPath).GetAwaiter().GetResult();
using (var stream = file.OpenAsync(FileAccessMode.ReadWrite).GetAwaiter().GetResult().AsStream())
{
stream.Write (pbData, 0, pbData.Length);
}
MemUtil.ZeroByteArray(pbData);
#elif !KeePassLibSD
byte[] pbData = pb.ReadData();
File.WriteAllBytes(strPath, pbData);
MemUtil.ZeroByteArray(pbData);
#else
FileStream fs = new FileStream(strPath, FileMode.Create,
FileAccess.Write, FileShare.None);
byte[] pbData = pb.ReadData(); byte[] pbData = pb.ReadData();
try { File.WriteAllBytes(strPath, pbData); } try { File.WriteAllBytes(strPath, pbData); }
finally { if(pb.IsProtected) MemUtil.ZeroByteArray(pbData); } finally { if(pb.IsProtected) MemUtil.ZeroByteArray(pbData); }
#endif
} }
} }
} }

View File

@@ -1,17 +1,21 @@
using Windows.UI.Xaml.Controls; 
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
namespace ModernKeePassLib.Utility namespace ModernKeePassLib.Utility
{ {
public class GfxUtil public class GfxUtil
{ {
public static Image LoadImage(byte[] pb) public static Image<Rgba32> LoadImage(byte[] pb)
{ {
return null; return Image.Load<Rgba32>(pb);
} }
public static Image ScaleImage(Image m_imgOrg, int? w, int? h, ScaleTransformFlags flags) public static Image<Rgba32> ScaleImage(Image<Rgba32> m_imgOrg, int? w, int? h, ScaleTransformFlags flags)
{ {
return null; m_imgOrg.Mutate(i => i.Resize(w.GetValueOrDefault(), h.GetValueOrDefault()));
return m_imgOrg;
} }
} }
} }

View File

@@ -1,17 +0,0 @@
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

@@ -25,10 +25,6 @@ using System.IO;
using System.Text; using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
#if ModernKeePassLib
using Windows.Storage;
#endif
using ModernKeePassLib.Native; using ModernKeePassLib.Native;
namespace ModernKeePassLib.Utility namespace ModernKeePassLib.Utility
@@ -44,14 +40,7 @@ namespace ModernKeePassLib.Utility
public static char LocalDirSepChar public static char LocalDirSepChar
{ {
#if KeePassRT
get { return '\\'; }
#elif ModernKeePassLib
//get { return PortablePath.DirectorySeparatorChar; }
get { return '\\'; }
#else
get { return Path.DirectorySeparatorChar; } get { return Path.DirectorySeparatorChar; }
#endif
} }
private static char[] g_vDirSepChars = null; private static char[] g_vDirSepChars = null;
@@ -486,16 +475,7 @@ namespace ModernKeePassLib.Utility
} }
string str; string str;
try try { str = Path.GetFullPath(strPath); }
{
#if ModernKeePassLib
var dirT = StorageFolder.GetFolderFromPathAsync(
strPath).GetResults();
str = dirT.Path;
#else
str = Path.GetFullPath(strPath);
#endif
}
catch(Exception) { Debug.Assert(false); return strPath; } catch(Exception) { Debug.Assert(false); return strPath; }
Debug.Assert((str.IndexOf("\\..\\") < 0) || NativeLib.IsUnix()); Debug.Assert((str.IndexOf("\\..\\") < 0) || NativeLib.IsUnix());
@@ -703,10 +683,11 @@ namespace ModernKeePassLib.Utility
string strDir; string strDir;
if(NativeLib.IsUnix()) if(NativeLib.IsUnix())
strDir = NativeMethods.GetUserRuntimeDir(); strDir = NativeMethods.GetUserRuntimeDir();
#if KeePassUAP || ModernKeePassLib #if KeePassUAP
else strDir = Windows.Storage.ApplicationData.Current.TemporaryFolder.Path; else strDir = Windows.Storage.ApplicationData.Current.TemporaryFolder.Path;
#else #else
else strDir = Path.GetTempPath(); else strDir = Path.GetTempPath();
#endif
try try
{ {
@@ -714,11 +695,10 @@ namespace ModernKeePassLib.Utility
} }
catch(Exception) { Debug.Assert(false); } catch(Exception) { Debug.Assert(false); }
#endif
return strDir; return strDir;
} }
#if !ModernKeePassLib && !KeePassLibSD #if !KeePassLibSD
// Structurally mostly equivalent to UrlUtil.GetFileInfos // Structurally mostly equivalent to UrlUtil.GetFileInfos
public static List<string> GetFilePaths(string strDir, string strPattern, public static List<string> GetFilePaths(string strDir, string strPattern,
SearchOption opt) SearchOption opt)