Skip to content

Commit

Permalink
Directory reader, a start
Browse files Browse the repository at this point in the history
  • Loading branch information
picrap committed Sep 1, 2017
1 parent 0c1de02 commit 5e20714
Show file tree
Hide file tree
Showing 37 changed files with 1,093 additions and 167 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@

*.cs text eol=crlf diff=csharp
*.csproj text eol=crlf diff=csharp

*.gz binary
52 changes: 0 additions & 52 deletions ExFat.Core/Buffer/BufferData.cs

This file was deleted.

19 changes: 0 additions & 19 deletions ExFat.Core/Buffer/BufferInt64.cs

This file was deleted.

25 changes: 0 additions & 25 deletions ExFat.Core/Buffer/BufferUInt32.cs

This file was deleted.

25 changes: 0 additions & 25 deletions ExFat.Core/Buffer/BufferUInt64.cs

This file was deleted.

108 changes: 108 additions & 0 deletions ExFat.Core/Buffers/Buffer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
namespace ExFat.Core.Buffers
{
using System;
using System.Collections.Generic;

public class Buffer
{
private readonly int _offset;
private readonly byte[] _bytes;

/// <summary>
/// Gets the length.
/// </summary>
/// <value>
/// The length.
/// </value>
public int Length { get; }

/// <summary>
/// Gets or sets the <see cref="System.Byte"/> at the specified index.
/// </summary>
/// <value>
/// The <see cref="System.Byte"/>.
/// </value>
/// <param name="index">The index.</param>
/// <returns></returns>
/// <exception cref="System.ArgumentOutOfRangeException">
/// index
/// or
/// index
/// </exception>
public byte this[int index]
{
get
{
if (index < 0 || index >= Length)
throw new ArgumentOutOfRangeException(nameof(index));
return _bytes[_offset + index];
}
set
{
if (index < 0 || index >= Length)
throw new ArgumentOutOfRangeException(nameof(index));
_bytes[_offset + index] = value;
}
}

/// <summary>
/// Gets the bytes.
/// </summary>
/// <returns></returns>
public byte[] GetBytes()
{
var bytes = new byte[Length];
Array.Copy(_bytes, _offset, bytes, 0, Length);
return bytes;
}

public void Set(IList<byte> bytes)
{
if (bytes.Count > Length)
throw new ArgumentException(nameof(bytes));
for (int index = 0; index < bytes.Count; index++)
_bytes[_offset + index] = bytes[index];
}

/// <summary>
/// Initializes a new instance of the <see cref="Buffer"/> class given a raw array of bytes.
/// </summary>
/// <param name="bytes">The bytes.</param>
public Buffer(byte[] bytes)
{
_bytes = bytes;
Length = _bytes.Length;
}

/// <summary>
/// Initializes a new instance of the <see cref="Buffer"/> class given another <see cref="Buffer"/>.
/// </summary>
/// <param name="buffer">The buffer.</param>
/// <param name="offset">The offset.</param>
/// <param name="length">The length.</param>
/// <exception cref="System.ArgumentOutOfRangeException">
/// offset
/// or
/// length
/// </exception>
public Buffer(Buffer buffer, int offset, int length)
{
if (offset < 0 || offset >= buffer.Length)
throw new ArgumentOutOfRangeException(nameof(offset));
if (offset + length > buffer.Length)
throw new ArgumentOutOfRangeException(nameof(length));
_bytes = buffer._bytes;
_offset = buffer._offset + offset;
Length = length;
}

/// <summary>
/// Initializes a new instance of the <see cref="Buffer"/> class given another <see cref="Buffer"/>.
/// </summary>
/// <param name="buffer">The buffer.</param>
public Buffer(Buffer buffer)
: this(buffer, 0, buffer.Length)
{
}
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
namespace ExFat.Core.Buffer
namespace ExFat.Core.Buffers
{
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;

[DebuggerDisplay("{Value}")]
public class BufferByteString : BufferData
[DebuggerDisplay("{" + nameof(Value) + "}")]
public class BufferByteString : IValueProvider<string>
{
private readonly Encoding _encoding;
private readonly Buffer _buffer;

private IEnumerable<byte> GetZero()
private IEnumerable<byte> GetZeroBytes()
{
foreach (var b in GetAll())
foreach (var b in _buffer.GetBytes())
{
if (b == 0)
break;
Expand All @@ -28,20 +29,28 @@ private IEnumerable<byte> GetZero()
/// </value>
public string Value
{
get { return _encoding.GetString(GetZero().ToArray()); }
set { Set(_encoding.GetBytes(value)); }
get { return _encoding.GetString(GetZeroBytes().ToArray()); }
set
{
var stringBytes = _encoding.GetBytes(value);
// first of all, inject bytes
_buffer.Set(stringBytes);
// then pad
for (int index = stringBytes.Length; index < _buffer.Length; index++)
_buffer[index] = 0;
}
}

/// <summary>
/// Initializes a new instance of the <see cref="BufferByteString"/> class.
/// Initializes a new instance of the <see cref="BufferByteString" /> class.
/// </summary>
/// <param name="buffer">The buffer.</param>
/// <param name="offset">The offset.</param>
/// <param name="length">The length.</param>
/// <param name="encoding">The encoding (defaults to ASCII).</param>
public BufferByteString(byte[] buffer, int offset, int length, Encoding encoding = null)
: base(buffer, offset, length)
public BufferByteString(Buffer buffer, int offset, int length, Encoding encoding = null)
{
_buffer = new Buffer(buffer, offset, length);
_encoding = encoding ?? Encoding.ASCII;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace ExFat.Core.Buffer
namespace ExFat.Core.Buffers
{
using System;
using System.Collections;
Expand All @@ -9,10 +9,11 @@
/// <summary>
/// Represents bytes in the buffer
/// </summary>
/// <seealso cref="BufferData" />
[DebuggerDisplay("{" + nameof(DebugLiteral) + "}")]
public class BufferBytes : BufferData, IEnumerable<byte>
public class BufferBytes : IEnumerable<byte>
{
private readonly Buffers.Buffer _buffer;

/// <summary>
/// Gets or sets the <see cref="System.Byte"/> at the specified index.
/// </summary>
Expand All @@ -25,15 +26,15 @@ public class BufferBytes : BufferData, IEnumerable<byte>
/// </exception>
public byte this[int index]
{
get { return GetAt(index); }
set { SetAt(index, value); }
get { return _buffer[index]; }
set { _buffer[index] = value; }
}

private string DebugLiteral
{
get
{
var bytes = GetAll();
var bytes = _buffer.GetBytes();
var s = string.Join(", ", bytes.Take(10).Select(b => $"0x{b:X2}"));
if (bytes.Length > 10)
s += " ...";
Expand All @@ -47,14 +48,14 @@ private string DebugLiteral
/// <param name="buffer">The buffer.</param>
/// <param name="offset">The offset.</param>
/// <param name="length">The length.</param>
public BufferBytes(byte[] buffer, int offset, int length)
: base(buffer, offset, length)
public BufferBytes(Buffers.Buffer buffer, int offset, int length)
{
_buffer = new Buffers.Buffer(buffer, offset, length);
}

public IEnumerator<byte> GetEnumerator()
{
return ((IEnumerable<byte>)GetAll()).GetEnumerator();
return ((IEnumerable<byte>)_buffer.GetBytes()).GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator()
Expand Down
28 changes: 28 additions & 0 deletions ExFat.Core/Buffers/BufferUInt16.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace ExFat.Core.Buffers
{
using System;
using System.Diagnostics;

[DebuggerDisplay("{" + nameof(Value) + "}")]
public class BufferUInt16 : IValueProvider<UInt16>
{
private readonly Buffer _buffer;

/// <summary>
/// Gets or sets the value.
/// </summary>
/// <value>
/// The value.
/// </value>
public UInt16 Value
{
get { return BitConverter.ToUInt16(_buffer.GetBytes().FromLittleEndian(), 0); }
set { _buffer.Set(BitConverter.GetBytes(value).ToLittleEndian()); }
}

public BufferUInt16(Buffers.Buffer buffer, int offset)
{
_buffer = new Buffers.Buffer(buffer, offset, sizeof(UInt16));
}
}
}
Loading

0 comments on commit 5e20714

Please sign in to comment.