-
Notifications
You must be signed in to change notification settings - Fork 1
/
Helpers.cs
88 lines (77 loc) · 2.12 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
79
80
81
82
83
84
85
86
87
88
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace Edge.Patcher
{
internal static class Helpers
{
internal static string GetRelativePath(this string fromPath, string toPath)
{
if (string.IsNullOrEmpty(fromPath))
{
throw new ArgumentNullException("fromPath");
}
if (string.IsNullOrEmpty(toPath))
{
throw new ArgumentNullException("toPath");
}
Uri fromUri = new Uri(AppendDirectorySeparatorChar(fromPath));
Uri toUri = new Uri(AppendDirectorySeparatorChar(toPath));
if (fromUri.Scheme != toUri.Scheme)
{
return toPath;
}
Uri relativeUri = fromUri.MakeRelativeUri(toUri);
string relativePath = Uri.UnescapeDataString(relativeUri.ToString());
if (string.Equals(toUri.Scheme, Uri.UriSchemeFile, StringComparison.OrdinalIgnoreCase))
{
relativePath = relativePath.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
}
return relativePath;
}
internal static string AppendDirectorySeparatorChar(string path)
{
// Append a slash only if the path is a directory and does not have a slash.
if (!Path.HasExtension(path) &&
!path.EndsWith(Path.DirectorySeparatorChar.ToString()))
{
return path + Path.DirectorySeparatorChar;
}
return path;
}
internal static bool ContainsFile(this Dictionary<string, string> data, string file)
{
if (data == null || data.Count == 0)
{
return false;
}
return data.Keys.Contains(file, StringComparer.OrdinalIgnoreCase) || data.Values.Contains(file, StringComparer.OrdinalIgnoreCase);
}
internal static int SearchBytes(this byte[] haystack, int startIndex, byte[] needle, int haystackSize)
{
var len = needle.Length;
var limit = haystackSize - len;
for (var i = startIndex; i <= limit; i++)
{
var k = 0;
for (; k < len; k++)
{
if (needle[k] != haystack[i + k])
{
if (needle[k] != 0xFF)
{
break;
}
}
}
if (k == len)
{
return i;
}
}
return -1;
}
}
}