-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtl.cs
126 lines (118 loc) · 3.74 KB
/
Utl.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
using System.IO;
namespace DbfDeserializer
{
public static unsafe class Utl
{
private static readonly char[] TRIM_PATH = new char[] { '\\', '/' };
/// <summary>
/// This does not make any claims as to whether or not a string
/// IS a path, but one can confidently determine if a path is
/// NOT a path.
/// </summary>
public static bool CantBePath(this string p)
{
const int MAX_PATH_LEN = 255;
int len = p.Length;
return string.IsNullOrWhiteSpace(p) || len < 3 || len > MAX_PATH_LEN;
}
/// <summary>
///
/// Hash based on djb2 algorithm.
///
/// <para>
/// WARNING(RYAN_2023-09-22): DO NOT USE THIS FOR CRYPTOGRAPHIC PURPOSES!
///
/// WARNING(RYAN_2023-12-18): DO NOT MODIFY WITHOUT CHANGING
/// CONSTANT HASH VALUES!
/// </para>
///
/// </summary>
///
/// <param name="str">String to convert to hash.</param>
///
/// <returns>Unsigned 64 bit hash integer.</returns>
public static ulong HashUL(in string str)
{
int len = str.Length;
ulong hash = 5381ul;
fixed (char* fxd_str = str)
{
char* c = fxd_str;
while (--len > -1)
hash = ((hash << 5) + hash) + *c++; // hash * 33 + c
}
return hash;
}
/// <summary>
///
/// Determines whether or not an absolute input path is an invalid
/// Windows file path.
///
/// </summary>
///
/// <param name="p">Input path to check.</param>
///
/// <returns>
///
/// <br>True if the input path is INVALID.</br>
/// <br>False if the input path is VALID.</br>
///
/// </returns>
public static bool InvalidAbsPath(string p)
{
if (p.CantBePath()) return true;
try
{
string fullPath = Path.GetFullPath(p);
string? root = Path.GetPathRoot(p);
if (string.IsNullOrEmpty(root)) return false;
else return string.IsNullOrEmpty(root.Trim(TRIM_PATH));
}
catch { return true; }
}
/// <summary>
///
/// Determines whether or not an input directory path is invalid.
///
/// </summary>
///
/// <param name="p">Input directory path to check.</param>
///
/// <returns>
///
/// <br>True if the input path is INVALID.</br>
/// <br>False if the input path is VALID.</br>
///
/// </returns>
public static bool InvalidAbsDir(string p)
{
if (InvalidAbsPath(p)) return true;
try
{
FileAttributes fa = File.GetAttributes(p);
return (fa & FileAttributes.Directory) != FileAttributes.Directory ||
Directory.Exists(p) == false;
}
catch { return true; }
}
/// <summary>
///
/// Determines whether or not an input FILE path is invalid.
///
/// </summary>
///
/// <param name="p">Input file path to check.</param>
///
/// <returns>
///
/// <br>True if the input FILE path is INVALID.</br>
/// <br>False if the input FILE path is VALID.</br>
///
/// </returns>
public static bool InvalidAbsFileDir(string p)
{
string? dir = Path.GetDirectoryName(p);
return string.IsNullOrWhiteSpace(dir) || InvalidAbsDir(dir);
}
}
}