-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathByteArrayValue.cs
More file actions
305 lines (252 loc) · 8.3 KB
/
Copy pathByteArrayValue.cs
File metadata and controls
305 lines (252 loc) · 8.3 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
using System.Diagnostics.CodeAnalysis;
using System.Net;
using System.Text;
using static DuckSQL.Types.IByteArrayValue;
namespace DuckSQL.Types
{
internal class ByteArrayValue : IByteArrayValue
{
public byte[] Data { get; }
private DisplayFormat[]? _possibleDisplayFormats;
public DisplayFormat[] PossibleDisplayFormats =>
_possibleDisplayFormats ??= CalculatePossibleDisplayFormats();
public ByteArrayValue(byte[] data)
{
Data = data;
}
public override string ToString() => BitConverter.ToString(Data);
public int CompareTo(IByteArrayValue? other)
{
if (other?.Data is null)
return 1;
else if (Data is null)
return -1;
else
return Data.SequenceCompareTo(other.Data);
}
public int CompareTo(object? obj)
{
if (obj is IByteArrayValue byteArray)
return CompareTo(byteArray);
else
return 1;
}
/// <summary>
/// Returns possible display formats for the binary data in <see cref="Data"/>
/// </summary>
/// <returns></returns>
private DisplayFormat[] CalculatePossibleDisplayFormats()
{
var possibleDisplayFormats = new List<DisplayFormat>();
if (ToIPv4(out var _))
possibleDisplayFormats.Add(DisplayFormat.IPv4);
if (ToIPv6(out var _))
possibleDisplayFormats.Add(DisplayFormat.IPv6);
if (ToGuid(out var _))
possibleDisplayFormats.Add(DisplayFormat.Guid);
if (ToShort(out var _))
possibleDisplayFormats.Add(DisplayFormat.Short);
if (ToInteger(out var _))
possibleDisplayFormats.Add(DisplayFormat.Integer);
if (ToLong(out var _))
possibleDisplayFormats.Add(DisplayFormat.Long);
if (ToFloat(out var _))
possibleDisplayFormats.Add(DisplayFormat.Float);
if (ToDouble(out var _))
possibleDisplayFormats.Add(DisplayFormat.Double);
if (ToASCII(out var _))
possibleDisplayFormats.Add(DisplayFormat.ASCII);
//Always supported formats
possibleDisplayFormats.Add(DisplayFormat.Hex);
possibleDisplayFormats.Add(DisplayFormat.Base64);
possibleDisplayFormats.Add(DisplayFormat.Size);
return possibleDisplayFormats.ToArray();
}
#region Type Conversions
public bool ToIPv6([NotNullWhen(true)] out IPAddress? ipAddress)
{
ipAddress = null;
if (Data.Length != 16)
return false;
try
{
// Try to create an IPAddress - if it succeeds, it's at least valid binary for IPv6
ipAddress = new IPAddress(Data);
return true;
}
catch
{
return false;
}
}
public bool ToIPv4([NotNullWhen(true)] out IPAddress? ipAddress)
{
ipAddress = null;
if (Data.Length != 4)
return false;
try
{
// Try to create an IPAddress - if it succeeds, it's at least valid binary for IPv4
ipAddress = new IPAddress(Data);
return true;
}
catch
{
return false;
}
}
public bool ToGuid([NotNullWhen(true)] out Guid? guid)
{
guid = null;
if (Data.Length != 16)
return false;
try
{
// Try to create a Guid - if it succeeds, it's at least valid binary for a GUID
guid = new Guid(Data);
return true;
}
catch
{
return false;
}
}
public bool ToASCII([NotNullWhen(true)] out string? ascii)
{
ascii = null;
if (Data.Length == 0)
return false;
var printableCount = Data.Sum(@byte =>
@byte >= ' ' /*32*/ && @byte <= '~' /*126*/ //Printable ASCII range
? 1 : 0);
//If the length is longer than 8 make sure at least 75% of bytes are printable as ASCII
if (Data.Length < 8 || (double)printableCount / Data.Length >= 0.75)
{
try
{
ascii = Encoding.ASCII.GetString(Data);
return true;
}
catch
{
return false;
}
}
return false;
}
public bool ToShort([NotNullWhen(true)] out short? @short)
{
@short = null;
if (Data.Length != 2)
return false;
try
{
@short = BitConverter.ToInt16(Data);
return true;
}
catch
{
return false;
}
}
public bool ToInteger([NotNullWhen(true)] out int? @int)
{
@int = null;
if (Data.Length != 4)
return false;
try
{
@int = BitConverter.ToInt32(Data);
return true;
}
catch
{
return false;
}
}
public bool ToLong([NotNullWhen(true)] out long? @long)
{
@long = null;
if (Data.Length != 8)
return false;
try
{
@long = BitConverter.ToInt64(Data);
return true;
}
catch
{
return false;
}
}
public bool ToFloat([NotNullWhen(true)] out float? @float)
{
@float = null;
if (Data.Length != 4)
return false;
try
{
@float = BitConverter.ToSingle(Data);
return true;
}
catch
{
return false;
}
}
public bool ToDouble([NotNullWhen(true)] out double? @double)
{
@double = null;
if (Data.Length != 8)
return false;
try
{
@double = BitConverter.ToDouble(Data);
return true;
}
catch
{
return false;
}
}
public void ToBase64(out string base64)
{
base64 = Convert.ToBase64String(Data);
}
#endregion
/// <summary>
/// Calculates how many bytes are needed to generate a string of the given length in hexadecimal format.
/// </summary>
private static int HexStringLengthToByteCount(int stringLength)
=> stringLength != int.MaxValue
? (stringLength + 1) / 3 //One byte = 3 chars. E.g. AA- (-1 for the last byte which won't have a dash)
: int.MaxValue;
/// <summary>
/// Returns the binary data in hex with an ellipsis in the middle if required to reach <paramref name="desiredLength"/>
/// </summary>
/// <param name="desiredLength">Positive integer length to target (NOT GUARANTEED)</param>
/// <returns>Binary data in hexadecimal representation</returns>
public string ToStringTruncated(int desiredLength)
{
var maxBytesToRender = Math.Max(2, HexStringLengthToByteCount(desiredLength));
if (Data.Length <= maxBytesToRender)
return BitConverter.ToString(Data);
return BitConverter.ToString(Data, 0, maxBytesToRender / 2) + "[...]"
+ BitConverter.ToString(Data, Data.Length - (maxBytesToRender / 2));
}
public bool ToImage([NotNullWhen(true)] out Image? image)
{
try
{
using var ms = new MemoryStream(Data);
image = Image.FromStream(ms);
return true;
}
catch
{
image = null;
return false;
}
}
}
}