-
Notifications
You must be signed in to change notification settings - Fork 2
/
Helpers.cs
78 lines (72 loc) · 2.34 KB
/
Helpers.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright (c) Thomas Gossler. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Security;
namespace WebPageHost;
/// <summary>
/// Static helper functions used across commands.
/// </summary>
internal static class Helpers
{
public static T Scale<T>(T value, float scaleFactor) where T : struct
{
if (value is int) {
int val = (int)(object)value;
val = (int)(val * scaleFactor);
return (T)(object)val;
}
else if (typeof(T) == typeof(Point)) {
var val = (Point)(object)value;
val.X = (int)(val.X * scaleFactor);
val.Y = (int)(val.Y * scaleFactor);
return (T)(object)val;
}
else if (typeof(T) == typeof(Size)) {
var val = (Size)(object)value;
val.Width = (int)(val.Width * scaleFactor);
val.Height = (int)(val.Height * scaleFactor);
return (T)(object)val;
}
else if (typeof(T) == typeof(Rectangle)) {
var val = (Rectangle)(object)value;
val.X = (int)(val.X * scaleFactor);
val.Y = (int)(val.Y * scaleFactor);
val.Width = (int)(val.Width * scaleFactor);
val.Height = (int)(val.Height * scaleFactor);
return (T)(object)val;
}
return default;
}
public static SecureString ToSecureString(string str)
{
var secureStr = new SecureString();
if (str.Length > 0) {
foreach (char c in str) {
secureStr.AppendChar(c);
}
}
return secureStr;
}
public static string ToUnSecureString(SecureString secstr)
{
if (null == secstr) {
return string.Empty;
}
IntPtr unmanagedString = IntPtr.Zero;
try {
unmanagedString = Marshal.SecureStringToGlobalAllocUnicode(secstr);
string? result = Marshal.PtrToStringUni(unmanagedString);
if (result == null) {
Trace.TraceWarning("SecureString could not be converted to an unsecure string");
return string.Empty;
}
return result;
}
finally {
Marshal.ZeroFreeGlobalAllocUnicode(unmanagedString);
}
}
}