KeePassLib finally portable (but version 2.19)

Windows Store project stub
This commit is contained in:
2017-09-11 15:13:04 +02:00
parent 9640efaa5f
commit d3a3d73491
154 changed files with 102785 additions and 787 deletions

View File

@@ -0,0 +1,108 @@
/*
KeePass Password Safe - The Open-Source Password Manager
Copyright (C) 2003-2012 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.Text;
using System.IO;
using System.Diagnostics;
#if !KeePassLibSD
using System.IO.Compression;
#endif
namespace ModernKeePassLib.Utility
{
/// <summary>
/// Application-wide logging services.
/// </summary>
public static class AppLogEx
{
private static StreamWriter m_swOut = null;
public static void Open(string strPrefix)
{
return; // Logging is not enabled in normal builds of KeePass!
/*
AppLogEx.Close();
Debug.Assert(strPrefix != null);
if(strPrefix == null) strPrefix = "Log";
try
{
string strDirSep = string.Empty;
strDirSep += Path.DirectorySeparatorChar;
string strTemp = Path.GetTempPath();
if(!strTemp.EndsWith(strDirSep))
strTemp += strDirSep;
string strPath = strTemp + strPrefix + "-";
Debug.Assert(strPath.IndexOf('/') < 0);
DateTime dtNow = DateTime.Now;
string strTime = dtNow.ToString("s");
strTime = strTime.Replace('T', '-');
strTime = strTime.Replace(':', '-');
strPath += strTime + "-" + Environment.TickCount.ToString(
CultureInfo.InvariantCulture) + ".log.gz";
FileStream fsOut = new FileStream(strPath, FileMode.Create,
FileAccess.Write, FileShare.None);
GZipStream gz = new GZipStream(fsOut, CompressionMode.Compress);
m_swOut = new StreamWriter(gz);
AppLogEx.Log("Started logging on " + dtNow.ToString("s") + ".");
}
catch(Exception) { Debug.Assert(false); }
*/
}
public static void Close()
{
if(m_swOut == null) return;
Debug.Assert(false, "not yet implemented");
return;
#if TODO
m_swOut.Close();
m_swOut = null;
#endif
}
public static void Log(string strText)
{
if(m_swOut == null) return;
if(strText == null) m_swOut.WriteLine();
else m_swOut.WriteLine(strText);
}
public static void Log(Exception ex)
{
if(m_swOut == null) return;
if(ex == null) m_swOut.WriteLine();
else m_swOut.WriteLine(ex.ToString());
}
}
}

View File

@@ -0,0 +1,371 @@
/*
KeePass Password Safe - The Open-Source Password Manager
Copyright (C) 2003-2012 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.Text;
using System.IO;
#if !KeePassLibSD
#else
using ModernKeePassLibSD;
#endif
namespace ModernKeePassLib.Utility
{
/// <summary>
/// Contains static buffer manipulation and string conversion routines.
/// </summary>
public static class MemUtil
{
/// <summary>
/// Convert a hexadecimal string to a byte array. The input string must be
/// even (i.e. its length is a multiple of 2).
/// </summary>
/// <param name="strHex">String containing hexadecimal characters.</param>
/// <returns>Returns a byte array. Returns <c>null</c> if the string parameter
/// was <c>null</c> or is an uneven string (i.e. if its length isn't a
/// multiple of 2).</returns>
/// <exception cref="System.ArgumentNullException">Thrown if <paramref name="strHex" />
/// is <c>null</c>.</exception>
public static byte[] HexStringToByteArray(string strHex)
{
if(strHex == null) { Debug.Assert(false); throw new ArgumentNullException("strHex"); }
int nStrLen = strHex.Length;
if((nStrLen & 1) != 0) { Debug.Assert(false); return null; }
byte[] pb = new byte[nStrLen / 2];
byte bt;
char ch;
for(int i = 0; i < nStrLen; i += 2)
{
ch = strHex[i];
if((ch >= '0') && (ch <= '9'))
bt = (byte)(ch - '0');
else if((ch >= 'a') && (ch <= 'f'))
bt = (byte)(ch - 'a' + 10);
else if((ch >= 'A') && (ch <= 'F'))
bt = (byte)(ch - 'A' + 10);
else { Debug.Assert(false); bt = 0; }
bt <<= 4;
ch = strHex[i + 1];
if((ch >= '0') && (ch <= '9'))
bt += (byte)(ch - '0');
else if((ch >= 'a') && (ch <= 'f'))
bt += (byte)(ch - 'a' + 10);
else if((ch >= 'A') && (ch <= 'F'))
bt += (byte)(ch - 'A' + 10);
else { Debug.Assert(false); }
pb[i >> 1] = bt;
}
return pb;
}
/// <summary>
/// Convert a byte array to a hexadecimal string.
/// </summary>
/// <param name="pbArray">Input byte array.</param>
/// <returns>Returns the hexadecimal string representing the byte
/// array. Returns <c>null</c>, if the input byte array was <c>null</c>. Returns
/// an empty string, if the input byte array has length 0.</returns>
public static string ByteArrayToHexString(byte[] pbArray)
{
if(pbArray == null) return null;
int nLen = pbArray.Length;
if(nLen == 0) return string.Empty;
StringBuilder sb = new StringBuilder();
byte bt, btHigh, btLow;
for(int i = 0; i < nLen; ++i)
{
bt = pbArray[i];
btHigh = bt; btHigh >>= 4;
btLow = (byte)(bt & 0x0F);
if(btHigh >= 10) sb.Append((char)('A' + btHigh - 10));
else sb.Append((char)('0' + btHigh));
if(btLow >= 10) sb.Append((char)('A' + btLow - 10));
else sb.Append((char)('0' + btLow));
}
return sb.ToString();
}
/// <summary>
/// Set all bytes in a byte array to zero.
/// </summary>
/// <param name="pbArray">Input array. All bytes of this array will be set
/// to zero.</param>
public static void ZeroByteArray(byte[] pbArray)
{
Debug.Assert(pbArray != null); if(pbArray == null) throw new ArgumentNullException("pbArray");
// for(int i = 0; i < pbArray.Length; ++i)
// pbArray[i] = 0;
Array.Clear(pbArray, 0, pbArray.Length);
}
/// <summary>
/// Convert 2 bytes to a 16-bit unsigned integer using Little-Endian
/// encoding.
/// </summary>
/// <param name="pb">Input bytes. Array must contain at least 2 bytes.</param>
/// <returns>16-bit unsigned integer.</returns>
public static ushort BytesToUInt16(byte[] pb)
{
Debug.Assert((pb != null) && (pb.Length == 2));
if(pb == null) throw new ArgumentNullException("pb");
if(pb.Length != 2) throw new ArgumentException();
return (ushort)((ushort)pb[0] | ((ushort)pb[1] << 8));
}
/// <summary>
/// Convert 4 bytes to a 32-bit unsigned integer using Little-Endian
/// encoding.
/// </summary>
/// <param name="pb">Input bytes.</param>
/// <returns>32-bit unsigned integer.</returns>
public static uint BytesToUInt32(byte[] pb)
{
Debug.Assert((pb != null) && (pb.Length == 4));
if(pb == null) throw new ArgumentNullException("pb");
if(pb.Length != 4) throw new ArgumentException("Input array must contain 4 bytes!");
return (uint)pb[0] | ((uint)pb[1] << 8) | ((uint)pb[2] << 16) |
((uint)pb[3] << 24);
}
/// <summary>
/// Convert 8 bytes to a 64-bit unsigned integer using Little-Endian
/// encoding.
/// </summary>
/// <param name="pb">Input bytes.</param>
/// <returns>64-bit unsigned integer.</returns>
public static ulong BytesToUInt64(byte[] pb)
{
Debug.Assert((pb != null) && (pb.Length == 8));
if(pb == null) throw new ArgumentNullException("pb");
if(pb.Length != 8) throw new ArgumentException();
return (ulong)pb[0] | ((ulong)pb[1] << 8) | ((ulong)pb[2] << 16) |
((ulong)pb[3] << 24) | ((ulong)pb[4] << 32) | ((ulong)pb[5] << 40) |
((ulong)pb[6] << 48) | ((ulong)pb[7] << 56);
}
/// <summary>
/// Convert a 16-bit unsigned integer to 2 bytes using Little-Endian
/// encoding.
/// </summary>
/// <param name="uValue">16-bit input word.</param>
/// <returns>Two bytes representing the 16-bit value.</returns>
public static byte[] UInt16ToBytes(ushort uValue)
{
byte[] pb = new byte[2];
unchecked
{
pb[0] = (byte)uValue;
pb[1] = (byte)(uValue >> 8);
}
return pb;
}
/// <summary>
/// Convert a 32-bit unsigned integer to 4 bytes using Little-Endian
/// encoding.
/// </summary>
/// <param name="uValue">32-bit input word.</param>
/// <returns>Four bytes representing the 32-bit value.</returns>
public static byte[] UInt32ToBytes(uint uValue)
{
byte[] pb = new byte[4];
unchecked
{
pb[0] = (byte)uValue;
pb[1] = (byte)(uValue >> 8);
pb[2] = (byte)(uValue >> 16);
pb[3] = (byte)(uValue >> 24);
}
return pb;
}
/// <summary>
/// Convert a 64-bit unsigned integer to 8 bytes using Little-Endian
/// encoding.
/// </summary>
/// <param name="uValue">64-bit input word.</param>
/// <returns>Eight bytes representing the 64-bit value.</returns>
public static byte[] UInt64ToBytes(ulong uValue)
{
byte[] pb = new byte[8];
unchecked
{
pb[0] = (byte)uValue;
pb[1] = (byte)(uValue >> 8);
pb[2] = (byte)(uValue >> 16);
pb[3] = (byte)(uValue >> 24);
pb[4] = (byte)(uValue >> 32);
pb[5] = (byte)(uValue >> 40);
pb[6] = (byte)(uValue >> 48);
pb[7] = (byte)(uValue >> 56);
}
return pb;
}
public static bool ArraysEqual(byte[] x, byte[] y)
{
// Return false if one of them is null (not comparable)!
if((x == null) || (y == null)) { Debug.Assert(false); return false; }
if(x.Length != y.Length) return false;
for(int i = 0; i < x.Length; ++i)
{
if(x[i] != y[i]) return false;
}
return true;
}
public static void XorArray(byte[] pbSource, int nSourceOffset,
byte[] pbBuffer, int nBufferOffset, int nLength)
{
if(pbSource == null) throw new ArgumentNullException("pbSource");
if(nSourceOffset < 0) throw new ArgumentException();
if(pbBuffer == null) throw new ArgumentNullException("pbBuffer");
if(nBufferOffset < 0) throw new ArgumentException();
if(nLength < 0) throw new ArgumentException();
if((nSourceOffset + nLength) > pbSource.Length) throw new ArgumentException();
if((nBufferOffset + nLength) > pbBuffer.Length) throw new ArgumentException();
for(int i = 0; i < nLength; ++i)
pbBuffer[nBufferOffset + i] ^= pbSource[nSourceOffset + i];
}
public static void CopyStream(Stream sSource, Stream sTarget)
{
Debug.Assert((sSource != null) && (sTarget != null));
if(sSource == null) throw new ArgumentNullException("sSource");
if(sTarget == null) throw new ArgumentNullException("sTarget");
const int nBufSize = 4096;
byte[] pbBuf = new byte[nBufSize];
int nRead;
while((nRead = sSource.Read(pbBuf, 0, nBufSize)) > 0)
sTarget.Write(pbBuf, 0, nRead);
// Do not close any of the streams
}
public static byte[] Compress(byte[] pbData)
{
Debug.Assert(false, "not yet implemented");
return null;
#if TODO
if(pbData == null) throw new ArgumentNullException("pbData");
if(pbData.Length == 0) return pbData;
MemoryStream msCompressed = new MemoryStream();
GZipStream gz = new GZipStream(msCompressed, CompressionMode.Compress);
MemoryStream msSource = new MemoryStream(pbData, false);
MemUtil.CopyStream(msSource, gz);
gz.Close();
msSource.Close();
byte[] pbCompressed = msCompressed.ToArray();
msCompressed.Close();
return pbCompressed;
#endif
}
public static byte[] Decompress(byte[] pbCompressed)
{
Debug.Assert(false, "not yet implemented");
return null;
#if TODO
if(pbCompressed == null) throw new ArgumentNullException("pbCompressed");
if(pbCompressed.Length == 0) return pbCompressed;
MemoryStream msCompressed = new MemoryStream(pbCompressed, false);
GZipStream gz = new GZipStream(msCompressed, CompressionMode.Decompress);
MemoryStream msData = new MemoryStream();
MemUtil.CopyStream(gz, msData);
gz.Close();
msCompressed.Close();
byte[] pbData = msData.ToArray();
msData.Close();
return pbData;
#endif
}
public static int IndexOf<T>(T[] vHaystack, T[] vNeedle)
where T : IEquatable<T>
{
if(vHaystack == null) throw new ArgumentNullException("vHaystack");
if(vNeedle == null) throw new ArgumentNullException("vNeedle");
if(vNeedle.Length == 0) return 0;
for(int i = 0; i <= (vHaystack.Length - vNeedle.Length); ++i)
{
bool bFound = true;
for(int m = 0; m < vNeedle.Length; ++m)
{
if(!vHaystack[i + m].Equals(vNeedle[m]))
{
bFound = false;
break;
}
}
if(bFound) return i;
}
return -1;
}
public static T[] Mid<T>(T[] v, int iOffset, int iLength)
{
if(v == null) throw new ArgumentNullException("v");
if(iOffset < 0) throw new ArgumentOutOfRangeException("iOffset");
if(iLength < 0) throw new ArgumentOutOfRangeException("iLength");
if(iOffset + iLength > v.Length) throw new ArgumentException();
T[] r = new T[iLength];
Array.Copy(v, iOffset, r, 0, iLength);
return r;
}
}
}

View File

@@ -0,0 +1,427 @@
/*
KeePass Password Safe - The Open-Source Password Manager
Copyright (C) 2003-2012 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
*/
/* BERT TODO: All removed for the time being */
#if false
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using ModernKeePassLib.Resources;
using ModernKeePassLib.Serialization;
namespace ModernKeePassLib.Utility
{
public sealed class MessageServiceEventArgs : EventArgs
{
private string m_strTitle = string.Empty;
private string m_strText = string.Empty;
private MessageBoxButtons m_msgButtons = MessageBoxButtons.OK;
private MessageBoxIcon m_msgIcon = MessageBoxIcon.None;
public string Title { get { return m_strTitle; } }
public string Text { get { return m_strText; } }
public MessageBoxButtons Buttons { get { return m_msgButtons; } }
public MessageBoxIcon Icon { get { return m_msgIcon; } }
public MessageServiceEventArgs() { }
public MessageServiceEventArgs(string strTitle, string strText,
MessageBoxButtons msgButtons, MessageBoxIcon msgIcon)
{
m_strTitle = (strTitle ?? string.Empty);
m_strText = (strText ?? string.Empty);
m_msgButtons = msgButtons;
m_msgIcon = msgIcon;
}
}
public static class MessageService
{
private static volatile uint m_uCurrentMessageCount = 0;
#if !KeePassLibSD
private const MessageBoxIcon m_mbiInfo = MessageBoxIcon.Information;
private const MessageBoxIcon m_mbiWarning = MessageBoxIcon.Warning;
private const MessageBoxIcon m_mbiFatal = MessageBoxIcon.Error;
private const MessageBoxOptions m_mboRtl = (MessageBoxOptions.RtlReading |
MessageBoxOptions.RightAlign);
#else
private const MessageBoxIcon m_mbiInfo = MessageBoxIcon.Asterisk;
private const MessageBoxIcon m_mbiWarning = MessageBoxIcon.Exclamation;
private const MessageBoxIcon m_mbiFatal = MessageBoxIcon.Hand;
#endif
private const MessageBoxIcon m_mbiQuestion = MessageBoxIcon.Question;
public static string NewLine
{
#if !KeePassLibSD
get { return Environment.NewLine; }
#else
get { return "\r\n"; }
#endif
}
public static string NewParagraph
{
#if !KeePassLibSD
get { return Environment.NewLine + Environment.NewLine; }
#else
get { return "\r\n\r\n"; }
#endif
}
public static uint CurrentMessageCount
{
get { return m_uCurrentMessageCount; }
}
public static event EventHandler<MessageServiceEventArgs> MessageShowing;
private static string ObjectsToMessage(object[] vLines)
{
return ObjectsToMessage(vLines, false);
}
private static string ObjectsToMessage(object[] vLines, bool bFullExceptions)
{
if(vLines == null) return string.Empty;
string strNewPara = MessageService.NewParagraph;
StringBuilder sbText = new StringBuilder();
bool bSeparator = false;
foreach(object obj in vLines)
{
if(obj == null) continue;
string strAppend = null;
Exception exObj = (obj as Exception);
string strObj = (obj as string);
#if !KeePassLibSD
StringCollection scObj = (obj as StringCollection);
#endif
if(exObj != null)
{
if(bFullExceptions)
strAppend = StrUtil.FormatException(exObj);
else if((exObj.Message != null) && (exObj.Message.Length > 0))
strAppend = exObj.Message;
}
#if !KeePassLibSD
else if(scObj != null)
{
StringBuilder sb = new StringBuilder();
foreach(string strCollLine in scObj)
{
if(sb.Length > 0) sb.AppendLine();
sb.Append(strCollLine.TrimEnd());
}
strAppend = sb.ToString();
}
#endif
else if(strObj != null)
strAppend = strObj;
else
strAppend = obj.ToString();
if(!string.IsNullOrEmpty(strAppend))
{
if(bSeparator) sbText.Append(strNewPara);
else bSeparator = true;
sbText.Append(strAppend);
}
}
return sbText.ToString();
}
private static DialogResult SafeShowMessageBox(string strText, string strTitle,
MessageBoxButtons mb, MessageBoxIcon mi, MessageBoxDefaultButton mdb)
{
#if KeePassLibSD
return MessageBox.Show(strText, strTitle, mb, mi, mdb);
#else
IWin32Window wnd = null;
try
{
FormCollection fc = Application.OpenForms;
if((fc != null) && (fc.Count > 0))
{
Form f = fc[fc.Count - 1];
if((f != null) && f.InvokeRequired)
return (DialogResult)f.Invoke(new SafeShowMessageBoxInternalDelegate(
SafeShowMessageBoxInternal), f, strText, strTitle, mb, mi, mdb);
else wnd = f;
}
}
catch(Exception) { Debug.Assert(false); }
if(wnd == null)
{
if(StrUtil.RightToLeft)
return MessageBox.Show(strText, strTitle, mb, mi, mdb, m_mboRtl);
return MessageBox.Show(strText, strTitle, mb, mi, mdb);
}
try
{
if(StrUtil.RightToLeft)
return MessageBox.Show(wnd, strText, strTitle, mb, mi, mdb, m_mboRtl);
return MessageBox.Show(wnd, strText, strTitle, mb, mi, mdb);
}
catch(Exception) { Debug.Assert(false); }
if(StrUtil.RightToLeft)
return MessageBox.Show(strText, strTitle, mb, mi, mdb, m_mboRtl);
return MessageBox.Show(strText, strTitle, mb, mi, mdb);
#endif
}
#if !KeePassLibSD
internal delegate DialogResult SafeShowMessageBoxInternalDelegate(IWin32Window iParent,
string strText, string strTitle, MessageBoxButtons mb, MessageBoxIcon mi,
MessageBoxDefaultButton mdb);
internal static DialogResult SafeShowMessageBoxInternal(IWin32Window iParent,
string strText, string strTitle, MessageBoxButtons mb, MessageBoxIcon mi,
MessageBoxDefaultButton mdb)
{
if(StrUtil.RightToLeft)
return MessageBox.Show(iParent, strText, strTitle, mb, mi, mdb, m_mboRtl);
return MessageBox.Show(iParent, strText, strTitle, mb, mi, mdb);
}
#endif
public static void ShowInfo(params object[] vLines)
{
ShowInfoEx(null, vLines);
}
public static void ShowInfoEx(string strTitle, params object[] vLines)
{
++m_uCurrentMessageCount;
strTitle = (strTitle ?? PwDefs.ShortProductName);
string strText = ObjectsToMessage(vLines);
if(MessageService.MessageShowing != null)
MessageService.MessageShowing(null, new MessageServiceEventArgs(
strTitle, strText, MessageBoxButtons.OK, m_mbiInfo));
SafeShowMessageBox(strText, strTitle, MessageBoxButtons.OK, m_mbiInfo,
MessageBoxDefaultButton.Button1);
--m_uCurrentMessageCount;
}
public static void ShowWarning(params object[] vLines)
{
ShowWarningPriv(vLines, false);
}
internal static void ShowWarningExcp(params object[] vLines)
{
ShowWarningPriv(vLines, true);
}
private static void ShowWarningPriv(object[] vLines, bool bFullExceptions)
{
++m_uCurrentMessageCount;
string strTitle = PwDefs.ShortProductName;
string strText = ObjectsToMessage(vLines, bFullExceptions);
if(MessageService.MessageShowing != null)
MessageService.MessageShowing(null, new MessageServiceEventArgs(
strTitle, strText, MessageBoxButtons.OK, m_mbiWarning));
SafeShowMessageBox(strText, strTitle, MessageBoxButtons.OK, m_mbiWarning,
MessageBoxDefaultButton.Button1);
--m_uCurrentMessageCount;
}
public static void ShowFatal(params object[] vLines)
{
++m_uCurrentMessageCount;
string strTitle = PwDefs.ShortProductName + @" - " + KLRes.FatalError;
string strText = KLRes.FatalErrorText + MessageService.NewParagraph +
KLRes.ErrorInClipboard + MessageService.NewParagraph +
// Please send it to the KeePass developers.
// KLRes.ErrorFeedbackRequest + MessageService.NewParagraph +
ObjectsToMessage(vLines);
try
{
#if !KeePassLibSD
Clipboard.Clear();
Clipboard.SetText(ObjectsToMessage(vLines, true));
#else
Clipboard.SetDataObject(ObjectsToMessage(vLines, true));
#endif
}
catch(Exception) { Debug.Assert(false); }
if(MessageService.MessageShowing != null)
MessageService.MessageShowing(null, new MessageServiceEventArgs(
strTitle, strText, MessageBoxButtons.OK, m_mbiFatal));
SafeShowMessageBox(strText, strTitle, MessageBoxButtons.OK, m_mbiFatal,
MessageBoxDefaultButton.Button1);
--m_uCurrentMessageCount;
}
public static DialogResult Ask(string strText, string strTitle,
MessageBoxButtons mbb)
{
++m_uCurrentMessageCount;
string strTextEx = (strText ?? string.Empty);
string strTitleEx = (strTitle ?? PwDefs.ShortProductName);
if(MessageService.MessageShowing != null)
MessageService.MessageShowing(null, new MessageServiceEventArgs(
strTitleEx, strTextEx, mbb, m_mbiQuestion));
DialogResult dr = SafeShowMessageBox(strTextEx, strTitleEx, mbb,
m_mbiQuestion, MessageBoxDefaultButton.Button1);
--m_uCurrentMessageCount;
return dr;
}
public static bool AskYesNo(string strText, string strTitle, bool bDefaultToYes)
{
++m_uCurrentMessageCount;
string strTextEx = (strText ?? string.Empty);
string strTitleEx = (strTitle ?? PwDefs.ShortProductName);
if(MessageService.MessageShowing != null)
MessageService.MessageShowing(null, new MessageServiceEventArgs(
strTitleEx, strTextEx, MessageBoxButtons.YesNo, m_mbiQuestion));
DialogResult dr = SafeShowMessageBox(strTextEx, strTitleEx,
MessageBoxButtons.YesNo, m_mbiQuestion, bDefaultToYes ?
MessageBoxDefaultButton.Button1 : MessageBoxDefaultButton.Button2);
--m_uCurrentMessageCount;
return (dr == DialogResult.Yes);
}
public static bool AskYesNo(string strText, string strTitle)
{
return AskYesNo(strText, strTitle, true);
}
public static bool AskYesNo(string strText)
{
return AskYesNo(strText, null, true);
}
public static void ShowLoadWarning(string strFilePath, Exception ex)
{
ShowLoadWarning(strFilePath, ex, false);
}
public static void ShowLoadWarning(string strFilePath, Exception ex,
bool bFullException)
{
string str = string.Empty;
if((strFilePath != null) && (strFilePath.Length > 0))
str += strFilePath + MessageService.NewParagraph;
str += KLRes.FileLoadFailed;
if((ex != null) && (ex.Message != null) && (ex.Message.Length > 0))
{
str += MessageService.NewParagraph;
if(!bFullException) str += ex.Message;
else str += ObjectsToMessage(new object[] { ex }, true);
}
ShowWarning(str);
}
public static void ShowLoadWarning(IOConnectionInfo ioConnection, Exception ex)
{
if(ioConnection != null)
ShowLoadWarning(ioConnection.GetDisplayName(), ex, false);
else ShowWarning(ex);
}
public static void ShowSaveWarning(string strFilePath, Exception ex,
bool bCorruptionWarning)
{
FileLockException fl = (ex as FileLockException);
if(fl != null)
{
ShowWarning(fl.Message);
return;
}
string str = string.Empty;
if((strFilePath != null) && (strFilePath.Length > 0))
str += strFilePath + MessageService.NewParagraph;
str += KLRes.FileSaveFailed;
if((ex != null) && (ex.Message != null) && (ex.Message.Length > 0))
str += MessageService.NewParagraph + ex.Message;
if(bCorruptionWarning)
str += MessageService.NewParagraph + KLRes.FileSaveCorruptionWarning;
ShowWarning(str);
}
public static void ShowSaveWarning(IOConnectionInfo ioConnection, Exception ex,
bool bCorruptionWarning)
{
if(ioConnection != null)
ShowSaveWarning(ioConnection.GetDisplayName(), ex, bCorruptionWarning);
else ShowWarning(ex);
}
public static void ExternalIncrementMessageCount()
{
++m_uCurrentMessageCount;
}
public static void ExternalDecrementMessageCount()
{
--m_uCurrentMessageCount;
}
}
}
#endif // false

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,222 @@
/*
KeePass Password Safe - The Open-Source Password Manager
Copyright (C) 2003-2012 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;
namespace ModernKeePassLib.Utility
{
/// <summary>
/// Contains various static time structure manipulation and conversion
/// routines.
/// </summary>
public static class TimeUtil
{
/// <summary>
/// Length of a compressed <c>PW_TIME</c> structure in bytes.
/// </summary>
public const int PwTimeLength = 7;
/// <summary>
/// Pack a <c>DateTime</c> object into 5 bytes. Layout: 2 zero bits,
/// year 12 bits, month 4 bits, day 5 bits, hour 5 bits, minute 6
/// bits, second 6 bits.
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public static byte[] PackTime(DateTime dt)
{
byte[] pb = new byte[5];
// Pack time to 5 byte structure:
// Byte bits: 11111111 22222222 33333333 44444444 55555555
// Contents : 00YYYYYY YYYYYYMM MMDDDDDH HHHHMMMM MMSSSSSS
pb[0] = (byte)((dt.Year >> 6) & 0x3F);
pb[1] = (byte)(((dt.Year & 0x3F) << 2) | ((dt.Month >> 2) & 0x03));
pb[2] = (byte)(((dt.Month & 0x03) << 6) | ((dt.Day & 0x1F) << 1) |
((dt.Hour >> 4) & 0x01));
pb[3] = (byte)(((dt.Hour & 0x0F) << 4) | ((dt.Minute >> 2) & 0x0F));
pb[4] = (byte)(((dt.Minute & 0x03) << 6) | (dt.Second & 0x3F));
return pb;
}
/// <summary>
/// Unpack a packed time (5 bytes, packed by the <c>PackTime</c>
/// member function) to a <c>DateTime</c> object.
/// </summary>
/// <param name="pb">Packed time, 5 bytes.</param>
/// <returns>Unpacked <c>DateTime</c> object.</returns>
public static DateTime UnpackTime(byte[] pb)
{
Debug.Assert((pb != null) && (pb.Length == 5));
if(pb == null) throw new ArgumentNullException("pb");
if(pb.Length != 5) throw new ArgumentException();
int n1 = pb[0], n2 = pb[1], n3 = pb[2], n4 = pb[3], n5 = pb[4];
// Unpack 5 byte structure to date and time
int nYear = (n1 << 6) | (n2 >> 2);
int nMonth = ((n2 & 0x00000003) << 2) | (n3 >> 6);
int nDay = (n3 >> 1) & 0x0000001F;
int nHour = ((n3 & 0x00000001) << 4) | (n4 >> 4);
int nMinute = ((n4 & 0x0000000F) << 2) | (n5 >> 6);
int nSecond = n5 & 0x0000003F;
return new DateTime(nYear, nMonth, nDay, nHour, nMinute, nSecond);
}
/// <summary>
/// Pack a <c>DateTime</c> object into 7 bytes (<c>PW_TIME</c>).
/// </summary>
/// <param name="dt">Object to be encoded.</param>
/// <returns>Packed time, 7 bytes (<c>PW_TIME</c>).</returns>
public static byte[] PackPwTime(DateTime dt)
{
Debug.Assert(PwTimeLength == 7);
byte[] pb = new byte[7];
pb[0] = (byte)(dt.Year & 0xFF);
pb[1] = (byte)(dt.Year >> 8);
pb[2] = (byte)dt.Month;
pb[3] = (byte)dt.Day;
pb[4] = (byte)dt.Hour;
pb[5] = (byte)dt.Minute;
pb[6] = (byte)dt.Second;
return pb;
}
/// <summary>
/// Unpack a packed time (7 bytes, <c>PW_TIME</c>) to a <c>DateTime</c> object.
/// </summary>
/// <param name="pb">Packed time, 7 bytes.</param>
/// <returns>Unpacked <c>DateTime</c> object.</returns>
public static DateTime UnpackPwTime(byte[] pb)
{
Debug.Assert(PwTimeLength == 7);
Debug.Assert(pb != null); if(pb == null) throw new ArgumentNullException("pb");
Debug.Assert(pb.Length == 7); if(pb.Length != 7) throw new ArgumentException();
return new DateTime(((int)pb[1] << 8) | (int)pb[0], (int)pb[2], (int)pb[3],
(int)pb[4], (int)pb[5], (int)pb[6]);
}
/// <summary>
/// Convert a <c>DateTime</c> object to a displayable string.
/// </summary>
/// <param name="dt"><c>DateTime</c> object to convert to a string.</param>
/// <returns>String representing the specified <c>DateTime</c> object.</returns>
public static string ToDisplayString(DateTime dt)
{
return dt.ToString();
}
public static string ToDisplayStringDateOnly(DateTime dt)
{
return dt.ToString("d");
}
public static DateTime FromDisplayString(string strDisplay)
{
DateTime dt;
#if !KeePassLibSD
if(DateTime.TryParse(strDisplay, out dt)) return dt;
#else
try { dt = DateTime.Parse(strDisplay); return dt; }
catch(Exception) { }
#endif
Debug.Assert(false);
return DateTime.Now;
}
public static string SerializeUtc(DateTime dt)
{
string str = dt.ToUniversalTime().ToString("s");
if(str.EndsWith("Z") == false) str += "Z";
return str;
}
public static bool TryDeserializeUtc(string str, out DateTime dt)
{
if(str == null) throw new ArgumentNullException("str");
if(str.EndsWith("Z")) str = str.Substring(0, str.Length - 1);
bool bResult = StrUtil.TryParseDateTime(str, out dt);
if(bResult) dt = dt.ToLocalTime();
return bResult;
}
private static DateTime? m_dtUnixRoot = null;
public static DateTime ConvertUnixTime(double dtUnix)
{
try
{
if(!m_dtUnixRoot.HasValue)
m_dtUnixRoot = (new DateTime(1970, 1, 1, 0, 0, 0, 0,
DateTimeKind.Utc)).ToLocalTime();
return m_dtUnixRoot.Value.AddSeconds(dtUnix);
}
catch(Exception) { Debug.Assert(false); }
return DateTime.Now;
}
#if !KeePassLibSD
private static string[] m_vUSMonths = null;
/// <summary>
/// Parse a US textual date string, like e.g. "January 02, 2012".
/// </summary>
public static DateTime? ParseUSTextDate(string strDate)
{
if(strDate == null) { Debug.Assert(false); return null; }
if(m_vUSMonths == null)
m_vUSMonths = new string[]{ "January", "February", "March",
"April", "May", "June", "July", "August", "September",
"October", "November", "December" };
string str = strDate.Trim();
for(int i = 0; i < m_vUSMonths.Length; ++i)
{
if(str.StartsWith(m_vUSMonths[i], StrUtil.CaseIgnoreCmp))
{
str = str.Substring(m_vUSMonths[i].Length);
string[] v = str.Split(new char[]{ ',', ';' });
if((v == null) || (v.Length != 2)) return null;
string strDay = v[0].Trim().TrimStart('0');
int iDay, iYear;
if(int.TryParse(strDay, out iDay) &&
int.TryParse(v[1].Trim(), out iYear))
return new DateTime(iYear, i + 1, iDay);
else { Debug.Assert(false); return null; }
}
}
return null;
}
#endif
}
}

View File

@@ -0,0 +1,599 @@
/*
KeePass Password Safe - The Open-Source Password Manager
Copyright (C) 2003-2012 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.IO;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
using ModernKeePassLib.Native;
namespace ModernKeePassLib.Utility
{
/// <summary>
/// A class containing various static path utility helper methods (like
/// stripping extension from a file, etc.).
/// </summary>
public static class UrlUtil
{
// Bert TODO: Temporary fix, most of this class is not needed in WINRT.
private static readonly char DirectorySeparatorChar = '/';
private static readonly char[] m_vDirSeps = new char[] { '\\', '/',
DirectorySeparatorChar };
/// <summary>
/// Get the directory (path) of a file name. The returned string is
/// terminated by a directory separator character. Example:
/// passing <c>C:\\My Documents\\My File.kdb</c> in <paramref name="strFile" />
/// would produce this string: <c>C:\\My Documents\\</c>.
/// </summary>
/// <param name="strFile">Full path of a file.</param>
/// <param name="bAppendTerminatingChar">Append a terminating directory separator
/// character to the returned path.</param>
/// <param name="bEnsureValidDirSpec">If <c>true</c>, the returned path
/// is guaranteed to be a valid directory path (for example <c>X:\\</c> instead
/// of <c>X:</c>, overriding <paramref name="bAppendTerminatingChar" />).
/// This should only be set to <c>true</c>, if the returned path is directly
/// passed to some directory API.</param>
/// <returns>Directory of the file. The return value is an empty string
/// (<c>""</c>) if the input parameter is <c>null</c>.</returns>
public static string GetFileDirectory(string strFile, bool bAppendTerminatingChar,
bool bEnsureValidDirSpec)
{
Debug.Assert(strFile != null);
if(strFile == null) throw new ArgumentNullException("strFile");
int nLastSep = strFile.LastIndexOfAny(m_vDirSeps);
if(nLastSep < 0) return strFile; // None
if(bEnsureValidDirSpec && (nLastSep == 2) && (strFile[1] == ':') &&
(strFile[2] == '\\')) // Length >= 3 and Windows root directory
bAppendTerminatingChar = true;
if(!bAppendTerminatingChar) return strFile.Substring(0, nLastSep);
return EnsureTerminatingSeparator(strFile.Substring(0, nLastSep), false);
}
/// <summary>
/// Gets the file name of the specified file (full path). Example:
/// if <paramref name="strPath" /> is <c>C:\\My Documents\\My File.kdb</c>
/// the returned string is <c>My File.kdb</c>.
/// </summary>
/// <param name="strPath">Full path of a file.</param>
/// <returns>File name of the specified file. The return value is
/// an empty string (<c>""</c>) if the input parameter is <c>null</c>.</returns>
public static string GetFileName(string strPath)
{
Debug.Assert(strPath != null); if(strPath == null) throw new ArgumentNullException("strPath");
int nLastSep = strPath.LastIndexOfAny(m_vDirSeps);
if(nLastSep < 0) return strPath;
if(nLastSep >= (strPath.Length - 1)) return string.Empty;
return strPath.Substring(nLastSep + 1);
}
/// <summary>
/// Strip the extension of a file.
/// </summary>
/// <param name="strPath">Full path of a file with extension.</param>
/// <returns>File name without extension.</returns>
public static string StripExtension(string strPath)
{
Debug.Assert(strPath != null); if(strPath == null) throw new ArgumentNullException("strPath");
int nLastDirSep = strPath.LastIndexOfAny(m_vDirSeps);
int nLastExtDot = strPath.LastIndexOf('.');
if(nLastExtDot <= nLastDirSep) return strPath;
return strPath.Substring(0, nLastExtDot);
}
/// <summary>
/// Get the extension of a file.
/// </summary>
/// <param name="strPath">Full path of a file with extension.</param>
/// <returns>Extension without prepending dot.</returns>
public static string GetExtension(string strPath)
{
Debug.Assert(strPath != null); if(strPath == null) throw new ArgumentNullException("strPath");
int nLastDirSep = strPath.LastIndexOfAny(m_vDirSeps);
int nLastExtDot = strPath.LastIndexOf('.');
if(nLastExtDot <= nLastDirSep) return string.Empty;
if(nLastExtDot == (strPath.Length - 1)) return string.Empty;
return strPath.Substring(nLastExtDot + 1);
}
/// <summary>
/// Ensure that a path is terminated with a directory separator character.
/// </summary>
/// <param name="strPath">Input path.</param>
/// <param name="bUrl">If <c>true</c>, a slash (<c>/</c>) is appended to
/// the string if it's not terminated already. If <c>false</c>, the
/// default system directory separator character is used.</param>
/// <returns>Path having a directory separator as last character.</returns>
public static string EnsureTerminatingSeparator(string strPath, bool bUrl)
{
Debug.Assert(strPath != null); if(strPath == null) throw new ArgumentNullException("strPath");
int nLength = strPath.Length;
if(nLength <= 0) return string.Empty;
char chLast = strPath[nLength - 1];
for(int i = 0; i < m_vDirSeps.Length; ++i)
{
if(chLast == m_vDirSeps[i]) return strPath;
}
if(bUrl) return (strPath + '/');
return (strPath + DirectorySeparatorChar);
}
/* /// <summary>
/// File access mode enumeration. Used by the <c>FileAccessible</c>
/// method.
/// </summary>
public enum FileAccessMode
{
/// <summary>
/// Opening a file in read mode. The specified file must exist.
/// </summary>
Read = 0,
/// <summary>
/// Opening a file in create mode. If the file exists already, it
/// will be overwritten. If it doesn't exist, it will be created.
/// The return value is <c>true</c>, if data can be written to the
/// file.
/// </summary>
Create
} */
/* /// <summary>
/// Test if a specified path is accessible, either in read or write mode.
/// </summary>
/// <param name="strFilePath">Path to test.</param>
/// <param name="fMode">Requested file access mode.</param>
/// <returns>Returns <c>true</c> if the specified path is accessible in
/// the requested mode, otherwise the return value is <c>false</c>.</returns>
public static bool FileAccessible(string strFilePath, FileAccessMode fMode)
{
Debug.Assert(strFilePath != null);
if(strFilePath == null) throw new ArgumentNullException("strFilePath");
if(fMode == FileAccessMode.Read)
{
FileStream fs;
try { fs = File.OpenRead(strFilePath); }
catch(Exception) { return false; }
if(fs == null) return false;
fs.Close();
return true;
}
else if(fMode == FileAccessMode.Create)
{
FileStream fs;
try { fs = File.Create(strFilePath); }
catch(Exception) { return false; }
if(fs == null) return false;
fs.Close();
return true;
}
return false;
} */
public static string GetQuotedAppPath(string strPath)
{
int nFirst = strPath.IndexOf('\"');
int nSecond = strPath.IndexOf('\"', nFirst + 1);
if((nFirst >= 0) && (nSecond >= 0))
return strPath.Substring(nFirst + 1, nSecond - nFirst - 1);
return strPath;
}
public static string FileUrlToPath(string strUrl)
{
Debug.Assert(strUrl != null);
if(strUrl == null) throw new ArgumentNullException("strUrl");
string str = strUrl;
if(str.StartsWith(@"file:///", StrUtil.CaseIgnoreCmp))
str = str.Substring(8, str.Length - 8);
str = str.Replace('/', DirectorySeparatorChar);
return str;
}
public static bool UnhideFile(string strFile)
{
#if KeePassLibSD || !TODO
return false;
#else
if(strFile == null) throw new ArgumentNullException("strFile");
try
{
FileAttributes fa = File.GetAttributes(strFile);
if((long)(fa & FileAttributes.Hidden) == 0) return false;
return HideFile(strFile, false);
}
catch(Exception) { }
return false;
#endif
}
public static bool HideFile(string strFile, bool bHide)
{
#if KeePassLibSD || !TODO
return false;
#else
if(strFile == null) throw new ArgumentNullException("strFile");
try
{
FileAttributes fa = File.GetAttributes(strFile);
if(bHide) fa = ((fa & ~FileAttributes.Normal) | FileAttributes.Hidden);
else // Unhide
{
fa &= ~FileAttributes.Hidden;
if((long)fa == 0) fa |= FileAttributes.Normal;
}
File.SetAttributes(strFile, fa);
return true;
}
catch(Exception) { }
return false;
#endif
}
public static string MakeRelativePath(string strBaseFile, string strTargetFile)
{
if(strBaseFile == null) throw new ArgumentNullException("strBasePath");
if(strTargetFile == null) throw new ArgumentNullException("strTargetPath");
if(strBaseFile.Length == 0) return strTargetFile;
if(strTargetFile.Length == 0) return string.Empty;
// Test whether on different Windows drives
if((strBaseFile.Length >= 3) && (strTargetFile.Length >= 3))
{
if((strBaseFile[1] == ':') && (strTargetFile[1] == ':') &&
(strBaseFile[2] == '\\') && (strTargetFile[2] == '\\') &&
(strBaseFile[0] != strTargetFile[0]))
return strTargetFile;
}
if(NativeLib.IsUnix())
{
bool bBaseUnc = IsUncPath(strBaseFile);
bool bTargetUnc = IsUncPath(strTargetFile);
if((!bBaseUnc && bTargetUnc) || (bBaseUnc && !bTargetUnc))
return strTargetFile;
string strBase = GetShortestAbsolutePath(strBaseFile);
string strTarget = GetShortestAbsolutePath(strTargetFile);
string[] vBase = strBase.Split(m_vDirSeps);
string[] vTarget = strTarget.Split(m_vDirSeps);
int i = 0;
while((i < (vBase.Length - 1)) && (i < (vTarget.Length - 1)) &&
(vBase[i] == vTarget[i])) { ++i; }
StringBuilder sbRel = new StringBuilder();
for(int j = i; j < (vBase.Length - 1); ++j)
{
if(sbRel.Length > 0) sbRel.Append(DirectorySeparatorChar);
sbRel.Append("..");
}
for(int k = i; k < vTarget.Length; ++k)
{
if(sbRel.Length > 0) sbRel.Append(DirectorySeparatorChar);
sbRel.Append(vTarget[k]);
}
return sbRel.ToString();
}
#if KeePassLibSD || !TODO
return strTargetFile;
#else
try // Windows
{
const int nMaxPath = NativeMethods.MAX_PATH * 2;
StringBuilder sb = new StringBuilder(nMaxPath + 2);
if(NativeMethods.PathRelativePathTo(sb, strBaseFile, 0,
strTargetFile, 0) == false)
return strTargetFile;
string str = sb.ToString();
while(str.StartsWith(".\\")) str = str.Substring(2, str.Length - 2);
return str;
}
catch(Exception) { Debug.Assert(false); return strTargetFile; }
#endif
}
public static string MakeAbsolutePath(string strBaseFile, string strTargetFile)
{
if(strBaseFile == null) throw new ArgumentNullException("strBasePath");
if(strTargetFile == null) throw new ArgumentNullException("strTargetPath");
if(strBaseFile.Length == 0) return strTargetFile;
if(strTargetFile.Length == 0) return string.Empty;
if(IsAbsolutePath(strTargetFile)) return strTargetFile;
string strBaseDir = GetFileDirectory(strBaseFile, true, false);
return GetShortestAbsolutePath(strBaseDir + strTargetFile);
}
public static bool IsAbsolutePath(string strPath)
{
if(strPath == null) throw new ArgumentNullException("strPath");
if(strPath.Length == 0) return false;
if(IsUncPath(strPath)) return true;
try { return Path.IsPathRooted(strPath); }
catch(Exception) { Debug.Assert(false); }
return true;
}
public static string GetShortestAbsolutePath(string strPath)
{
Debug.Assert(false, "not yet implemented");
return null;
#if TODO
if(strPath == null) throw new ArgumentNullException("strPath");
if(strPath.Length == 0) return string.Empty;
// Path.GetFullPath is incompatible with UNC paths traversing over
// different server shares (which are created by PathRelativePathTo);
// we need to build the absolute path on our own...
if(IsUncPath(strPath))
{
char chSep = strPath[0];
Debug.Assert(Array.IndexOf<char>(m_vDirSeps, chSep) >= 0);
List<string> l = new List<string>();
#if !KeePassLibSD
string[] v = strPath.Split(m_vDirSeps, StringSplitOptions.None);
#else
string[] v = strPath.Split(m_vDirSeps);
#endif
Debug.Assert((v.Length >= 3) && (v[0].Length == 0) &&
(v[1].Length == 0));
foreach(string strPart in v)
{
if(strPart.Equals(".")) continue;
else if(strPart.Equals(".."))
{
if(l.Count > 0) l.RemoveAt(l.Count - 1);
else { Debug.Assert(false); }
}
else l.Add(strPart); // Do not ignore zero length parts
}
StringBuilder sb = new StringBuilder();
for(int i = 0; i < l.Count; ++i)
{
// Don't test length of sb, might be 0 due to initial UNC seps
if(i > 0) sb.Append(chSep);
sb.Append(l[i]);
}
return sb.ToString();
}
string str;
try { str = Path.GetFullPath(strPath); }
catch(Exception) { Debug.Assert(false); return strPath; }
Debug.Assert(str.IndexOf("\\..\\") < 0);
foreach(char ch in m_vDirSeps)
{
string strSep = new string(ch, 1);
str = str.Replace(strSep + "." + strSep, strSep);
}
return str;
#endif
}
public static int GetUrlLength(string strText, int nOffset)
{
if(strText == null) throw new ArgumentNullException("strText");
if(nOffset > strText.Length) throw new ArgumentException(); // Not >= (0 len)
int iPosition = nOffset, nLength = 0, nStrLen = strText.Length;
while(iPosition < nStrLen)
{
char ch = strText[iPosition];
++iPosition;
if((ch == ' ') || (ch == '\t') || (ch == '\r') || (ch == '\n'))
break;
++nLength;
}
return nLength;
}
public static string RemoveScheme(string strUrl)
{
if(string.IsNullOrEmpty(strUrl)) return string.Empty;
int nNetScheme = strUrl.IndexOf(@"://", StrUtil.CaseIgnoreCmp);
int nShScheme = strUrl.IndexOf(@":/", StrUtil.CaseIgnoreCmp);
int nSmpScheme = strUrl.IndexOf(@":", StrUtil.CaseIgnoreCmp);
if((nNetScheme < 0) && (nShScheme < 0) && (nSmpScheme < 0))
return strUrl; // No scheme
int nMin = Math.Min(Math.Min((nNetScheme >= 0) ? nNetScheme : int.MaxValue,
(nShScheme >= 0) ? nShScheme : int.MaxValue),
(nSmpScheme >= 0) ? nSmpScheme : int.MaxValue);
if(nMin == nNetScheme) return strUrl.Substring(nMin + 3);
if(nMin == nShScheme) return strUrl.Substring(nMin + 2);
return strUrl.Substring(nMin + 1);
}
public static string ConvertSeparators(string strPath)
{
return ConvertSeparators(strPath, DirectorySeparatorChar);
}
public static string ConvertSeparators(string strPath, char chSeparator)
{
if(string.IsNullOrEmpty(strPath)) return string.Empty;
strPath = strPath.Replace('/', chSeparator);
strPath = strPath.Replace('\\', chSeparator);
return strPath;
}
public static bool IsUncPath(string strPath)
{
if(strPath == null) throw new ArgumentNullException("strPath");
return (strPath.StartsWith("\\\\") || strPath.StartsWith("//"));
}
public static string FilterFileName(string strName)
{
if(strName == null) { Debug.Assert(false); return string.Empty; }
string str = strName;
str = str.Replace('/', '-');
str = str.Replace('\\', '-');
str = str.Replace(":", string.Empty);
str = str.Replace("*", string.Empty);
str = str.Replace("?", string.Empty);
str = str.Replace("\"", string.Empty);
str = str.Replace(@"'", string.Empty);
str = str.Replace('<', '(');
str = str.Replace('>', ')');
str = str.Replace('|', '-');
return str;
}
/// <summary>
/// Get the host component of an URL.
/// This method is faster and more fault-tolerant than creating
/// an <code>Uri</code> object and querying its <code>Host</code>
/// property.
/// </summary>
/// <example>
/// For the input <code>s://u:p@d.tld:p/p?q#f</code> the return
/// value is <code>d.tld</code>.
/// </example>
public static string GetHost(string strUrl)
{
if(strUrl == null) { Debug.Assert(false); return string.Empty; }
StringBuilder sb = new StringBuilder();
bool bInExtHost = false;
for(int i = 0; i < strUrl.Length; ++i)
{
char ch = strUrl[i];
if(bInExtHost)
{
if(ch == '/')
{
if(sb.Length == 0) { } // Ignore leading '/'s
else break;
}
else sb.Append(ch);
}
else // !bInExtHost
{
if(ch == ':') bInExtHost = true;
}
}
string str = sb.ToString();
if(str.Length == 0) str = strUrl;
// Remove the login part
int nLoginLen = str.IndexOf('@');
if(nLoginLen >= 0) str = str.Substring(nLoginLen + 1);
// Remove the port
int iPort = str.LastIndexOf(':');
if(iPort >= 0) str = str.Substring(0, iPort);
return str;
}
public static bool AssemblyEquals(string strExt, string strShort)
{
if((strExt == null) || (strShort == null)) { Debug.Assert(false); return false; }
if(strExt.Equals(strShort, StrUtil.CaseIgnoreCmp) ||
strExt.StartsWith(strShort + ",", StrUtil.CaseIgnoreCmp))
return true;
if(!strShort.EndsWith(".dll", StrUtil.CaseIgnoreCmp))
{
if(strExt.Equals(strShort + ".dll", StrUtil.CaseIgnoreCmp) ||
strExt.StartsWith(strShort + ".dll,", StrUtil.CaseIgnoreCmp))
return true;
}
if(!strShort.EndsWith(".exe", StrUtil.CaseIgnoreCmp))
{
if(strExt.Equals(strShort + ".exe", StrUtil.CaseIgnoreCmp) ||
strExt.StartsWith(strShort + ".exe,", StrUtil.CaseIgnoreCmp))
return true;
}
return false;
}
}
}