1
0
mirror of https://github.com/winscp/winscp.git synced 2026-10-24 02:28:55 +03:00
Files
winscp/dotnet/internal/Tools.cs
Martin Prikryl a269e58826 Bug fix: x-name URL parameter was incorrectly decoded
Source commit: be5c17cde1f823e24389834032d88299366963e0
2026-06-26 01:02:43 +02:00

72 lines
2.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Globalization;
namespace WinSCP
{
internal static class Tools
{
public static int TimeSpanToMilliseconds(TimeSpan value)
{
if ((value.TotalMilliseconds > int.MaxValue) || (value.TotalMilliseconds < int.MinValue))
{
throw new InvalidCastException(string.Format(CultureInfo.CurrentCulture, "Cannot convert {0} to integer", value));
}
return (int)value.TotalMilliseconds;
}
public static TimeSpan MillisecondsToTimeSpan(int value)
{
return TimeSpan.FromMilliseconds(value);
}
public static string ArgumentEscape(string value)
{
int i = 0;
while (i < value.Length)
{
if (value[i] == '"')
{
value = value.Insert(i, "\"");
++i;
}
++i;
}
return value;
}
public static void AddRawParameters(
ref string arguments, Dictionary<string, string> parameters, string switchName, bool count)
{
if (parameters.Count > 0)
{
if (!string.IsNullOrEmpty(arguments))
{
arguments += " ";
}
arguments += switchName;
if (count)
{
arguments += string.Format(CultureInfo.InvariantCulture, "[{0}]", parameters.Count);
}
foreach (KeyValuePair<string, string> rawSetting in parameters)
{
// Needs MungeStr-like encoding
arguments += string.Format(CultureInfo.InvariantCulture, " {0}=\"{1}\"", rawSetting.Key, ArgumentEscape(rawSetting.Value));
}
}
}
public static int LengthTo32Bit(long length)
{
if (length < int.MinValue || length > int.MaxValue)
{
throw new OverflowException(string.Format(CultureInfo.CurrentCulture, "Size {0} cannot be represented using 32-bit value", length));
}
return (int)length;
}
}
}