Skip to content

Commit ce4c1f8

Browse files
committed
Added DiscUtils.ExFat.Internal
* Formerly ExFat.Core in https://github.com/LTRData/ExFat, originally from https://github.com/picrap/ExFat
1 parent 8ba60a0 commit ce4c1f8

File tree

63 files changed

+6699
-28
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+6699
-28
lines changed

Library/DiscUtils.ExFat/DiscUtils.ExFat.csproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@
1515

1616
</PropertyGroup>
1717

18-
<ItemGroup>
19-
<PackageReference Include="LTRData.ExFat.Core" Version="*" />
20-
</ItemGroup>
21-
2218
<ItemGroup>
2319
<ProjectReference Include="..\DiscUtils.Core\DiscUtils.Core.csproj" />
2420
</ItemGroup>

Library/DiscUtils.ExFat/ExFatFileSystem.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
using System.IO;
88
using System.Linq;
99
using System.Text.RegularExpressions;
10-
using ExFat.Filesystem;
10+
using DiscUtils.ExFat.Internal.Filesystem;
1111
using DiscUtils.Streams;
12-
using ExFat.Partition;
13-
using ExFat;
12+
using DiscUtils.ExFat.Internal.Partition;
13+
using DiscUtils.ExFat.Internal;
1414
using LTRData.Extensions.Buffers;
1515

1616
namespace DiscUtils.ExFat;
@@ -89,8 +89,15 @@ protected override void Dispose(bool disposing)
8989
/// <returns></returns>
9090
public static bool Detect(Stream partitionStream)
9191
{
92-
var bootSector = ExFatPartition.ReadBootSector(partitionStream);
93-
return bootSector.IsValid;
92+
try
93+
{
94+
var bootSector = ExFatPartition.ReadBootSector(partitionStream);
95+
return bootSector.IsValid;
96+
}
97+
catch
98+
{
99+
return false;
100+
}
94101
}
95102

96103
/// <inheritdoc />
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// This is ExFat, an exFAT accessor written in pure C#
2+
// Released under MIT license
3+
// https://github.com/picrap/ExFat
4+
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Diagnostics;
8+
using System.Linq;
9+
using System.Runtime.InteropServices;
10+
using System.Text;
11+
12+
namespace DiscUtils.ExFat.Internal.Buffers;
13+
/// <summary>
14+
/// 8-bit char string
15+
/// </summary>
16+
/// <seealso cref="string" />
17+
[DebuggerDisplay("{" + nameof(Value) + "}")]
18+
public readonly struct BufferByteString : IValueProvider<string>
19+
{
20+
private readonly Encoding encoding;
21+
private readonly Memory<byte> buffer;
22+
23+
private IEnumerable<byte> GetZeroBytes()
24+
{
25+
foreach (var b in MemoryMarshal.ToEnumerable<byte>(buffer))
26+
{
27+
if (b == 0)
28+
{
29+
break;
30+
}
31+
32+
yield return b;
33+
}
34+
}
35+
36+
/// <inheritdoc />
37+
/// <summary>
38+
/// Gets or sets the value.
39+
/// </summary>
40+
/// <value>
41+
/// The value.
42+
/// </value>
43+
public string Value
44+
{
45+
get => encoding.GetString(GetZeroBytes().ToArray());
46+
set
47+
{
48+
var stringBytes = encoding.GetBytes(value);
49+
// first of all, inject bytes
50+
buffer.Set(stringBytes);
51+
// then pad
52+
for (var index = stringBytes.Length; index < buffer.Length; index++)
53+
{
54+
buffer.Span[index] = 0;
55+
}
56+
}
57+
}
58+
59+
/// <summary>
60+
/// Initializes a new instance of the <see cref="BufferByteString" /> class.
61+
/// </summary>
62+
/// <param name="buffer">The buffer.</param>
63+
/// <param name="encoding">The encoding (defaults to ASCII).</param>
64+
public BufferByteString(Memory<byte> buffer, Encoding encoding = null)
65+
{
66+
this.buffer = buffer;
67+
this.encoding = encoding ?? Encoding.ASCII;
68+
}
69+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// This is ExFat, an exFAT accessor written in pure C#
2+
// Released under MIT license
3+
// https://github.com/picrap/ExFat
4+
5+
using System;
6+
using System.Collections;
7+
using System.Collections.Generic;
8+
using System.Diagnostics;
9+
using System.Linq;
10+
using System.Runtime.InteropServices;
11+
12+
namespace DiscUtils.ExFat.Internal.Buffers;
13+
/// <summary>
14+
/// Represents bytes in the buffer
15+
/// </summary>
16+
[DebuggerDisplay("{" + nameof(DebugLiteral) + "}")]
17+
public readonly struct BufferBytes : IEnumerable<byte>
18+
{
19+
private readonly Memory<byte> buffer;
20+
21+
/// <summary>
22+
/// Gets or sets the <see cref="byte"/> at the specified index.
23+
/// </summary>
24+
/// <value>
25+
/// The <see cref="byte"/>.
26+
/// </value>
27+
/// <param name="index">The index.</param>
28+
/// <returns></returns>
29+
/// <exception cref="ArgumentOutOfRangeException">
30+
/// </exception>
31+
public byte this[int index]
32+
{
33+
get => buffer.Span[index];
34+
set => buffer.Span[index] = value;
35+
}
36+
37+
private string DebugLiteral
38+
{
39+
get
40+
{
41+
var s = string.Join(", ", this.Take(10).Select(b => $"0x{b:X2}"));
42+
if (buffer.Length > 10)
43+
{
44+
s += " ...";
45+
}
46+
47+
return s;
48+
}
49+
}
50+
51+
/// <summary>
52+
/// Initializes a new instance of the <see cref="BufferBytes"/> class.
53+
/// </summary>
54+
/// <param name="buffer">The buffer.</param>
55+
public BufferBytes(Memory<byte> buffer)
56+
{
57+
this.buffer = buffer;
58+
}
59+
60+
/// <summary>
61+
/// Sets the specified bytes.
62+
/// </summary>
63+
/// <param name="bytes">The bytes.</param>
64+
public void Set(ReadOnlySpan<byte> bytes)
65+
{
66+
for (var offset = 0; offset < buffer.Length; offset++)
67+
{
68+
buffer.Span[offset] = bytes[offset];
69+
}
70+
}
71+
72+
/// <summary>
73+
/// Returns an enumerator that iterates through the collection.
74+
/// </summary>
75+
/// <returns>
76+
/// A <see cref="T:System.Collections.Generic.IEnumerator`1" /> that can be used to iterate through the collection.
77+
/// </returns>
78+
public IEnumerator<byte> GetEnumerator() => MemoryMarshal.ToEnumerable<byte>(buffer).GetEnumerator();
79+
80+
/// <summary>
81+
/// Returns an enumerator that iterates through a collection.
82+
/// </summary>
83+
/// <returns>
84+
/// An <see cref="T:System.Collections.IEnumerator" /> object that can be used to iterate through the collection.
85+
/// </returns>
86+
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
87+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// This is ExFat, an exFAT accessor written in pure C#
2+
// Released under MIT license
3+
// https://github.com/picrap/ExFat
4+
5+
using DiscUtils.Streams;
6+
using System;
7+
using System.Diagnostics;
8+
9+
namespace DiscUtils.ExFat.Internal.Buffers;
10+
/// <summary>
11+
/// 16-bits unsigned int buffer
12+
/// </summary>
13+
/// <seealso cref="ushort" />
14+
[DebuggerDisplay("{" + nameof(Value) + "}")]
15+
public readonly struct BufferUInt16 : IValueProvider<ushort>
16+
{
17+
private readonly Memory<byte> buffer;
18+
19+
/// <summary>
20+
/// Gets or sets the value.
21+
/// </summary>
22+
/// <value>
23+
/// The value.
24+
/// </value>
25+
public ushort Value
26+
{
27+
get => EndianUtilities.ToUInt16LittleEndian(buffer.Span);
28+
set => EndianUtilities.WriteBytesLittleEndian(value, buffer.Span);
29+
}
30+
31+
/// <summary>
32+
/// Initializes a new instance of the <see cref="BufferUInt16"/> class.
33+
/// </summary>
34+
/// <param name="buffer">The buffer.</param>
35+
public BufferUInt16(Memory<byte> buffer)
36+
{
37+
this.buffer = buffer.Slice(0, sizeof(ushort));
38+
}
39+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// This is ExFat, an exFAT accessor written in pure C#
2+
// Released under MIT license
3+
// https://github.com/picrap/ExFat
4+
5+
using DiscUtils.Streams;
6+
using System;
7+
using System.Diagnostics;
8+
9+
namespace DiscUtils.ExFat.Internal.Buffers;
10+
/// <summary>
11+
/// 32-bits unsigned int buffer
12+
/// </summary>
13+
/// <seealso cref="uint" />
14+
[DebuggerDisplay("{" + nameof(Value) + "}")]
15+
public readonly struct BufferUInt32 : IValueProvider<uint>
16+
{
17+
private readonly Memory<byte> _buffer;
18+
19+
/// <summary>
20+
/// Gets or sets the value.
21+
/// </summary>
22+
/// <value>
23+
/// The value.
24+
/// </value>
25+
public uint Value
26+
{
27+
get => EndianUtilities.ToUInt32LittleEndian(_buffer.Span);
28+
set => EndianUtilities.WriteBytesLittleEndian(value, _buffer.Span);
29+
}
30+
31+
/// <summary>
32+
/// Initializes a new instance of the <see cref="BufferUInt32"/> class.
33+
/// </summary>
34+
/// <param name="buffer">The buffer.</param>
35+
public BufferUInt32(Memory<byte> buffer)
36+
{
37+
_buffer = buffer.Slice(0, sizeof(uint));
38+
}
39+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// This is ExFat, an exFAT accessor written in pure C#
2+
// Released under MIT license
3+
// https://github.com/picrap/ExFat
4+
5+
using DiscUtils.Streams;
6+
using System;
7+
using System.Diagnostics;
8+
9+
namespace DiscUtils.ExFat.Internal.Buffers;
10+
/// <summary>
11+
/// 64-bits unsigned int buffer
12+
/// </summary>
13+
/// <seealso cref="ulong" />
14+
[DebuggerDisplay("{" + nameof(Value) + "}")]
15+
public readonly struct BufferUInt64 : IValueProvider<ulong>
16+
{
17+
private readonly Memory<byte> buffer;
18+
19+
/// <summary>
20+
/// Gets or sets the value.
21+
/// </summary>
22+
/// <value>
23+
/// The value.
24+
/// </value>
25+
public ulong Value
26+
{
27+
get => EndianUtilities.ToUInt64LittleEndian(buffer.Span);
28+
set => EndianUtilities.WriteBytesLittleEndian(value, buffer.Span);
29+
}
30+
31+
/// <summary>
32+
/// Initializes a new instance of the <see cref="BufferUInt64"/> class.
33+
/// </summary>
34+
/// <param name="buffer">The buffer.</param>
35+
public BufferUInt64(Memory<byte> buffer)
36+
{
37+
this.buffer = buffer.Slice(0, sizeof(ulong));
38+
}
39+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// This is ExFat, an exFAT accessor written in pure C#
2+
// Released under MIT license
3+
// https://github.com/picrap/ExFat
4+
5+
using System;
6+
using System.Diagnostics;
7+
8+
namespace DiscUtils.ExFat.Internal.Buffers;
9+
/// <summary>
10+
/// 8-bit unsigned int buffer (in other words, a byte)
11+
/// </summary>
12+
/// <seealso cref="byte" />
13+
[DebuggerDisplay("{" + nameof(Value) + "}")]
14+
public readonly struct BufferUInt8 : IValueProvider<byte>
15+
{
16+
private readonly Memory<byte> buffer;
17+
18+
/// <inheritdoc />
19+
/// <summary>
20+
/// Gets or sets the value.
21+
/// </summary>
22+
/// <value>
23+
/// The value.
24+
/// </value>
25+
public byte Value
26+
{
27+
get => buffer.Span[0];
28+
set => buffer.Span[0] = value;
29+
}
30+
31+
/// <summary>
32+
/// Initializes a new instance of the <see cref="BufferUInt8"/> class.
33+
/// </summary>
34+
/// <param name="buffer">The buffer.</param>
35+
public BufferUInt8(Memory<byte> buffer)
36+
{
37+
this.buffer = buffer.Slice(0, sizeof(byte));
38+
}
39+
}

0 commit comments

Comments
 (0)