Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose some protected members #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 46 additions & 10 deletions src/Wiry.Base32/Base32Encoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,27 @@ namespace Wiry.Base32
/// </summary>
public abstract class Base32Encoding : IBase32Encoding
{
/// <summary>
/// Reverse alphabet lookup table
/// </summary>
protected sealed class LookupTable
{
/// <summary>
/// Low code
/// </summary>
public int LowCode { get; }
/// <summary>
/// Values
/// </summary>
public int[] Values { get; }

internal LookupTable(int lowCode, int[] values)
{
LowCode = lowCode;
Values = values;
}
}

private const string ErrorMessageInvalidLength = "Invalid length";
private const string ErrorMessageInvalidPadding = "Invalid padding";
private const string ErrorMessageInvalidCharacter = "Invalid character";
Expand Down Expand Up @@ -82,7 +103,7 @@ public virtual byte[] ToBytes(string encoded)
/// </summary>
public virtual byte[] ToBytes(string encoded, int index, int length)
{
return ToBytes(encoded, index, length, PadSymbol, GetOrCreateLookupTable(Alphabet));
return ToBytes(encoded, index, length, PadSymbol, GetOrCreateLookupTable());
}

/// <summary>
Expand All @@ -101,12 +122,15 @@ public virtual ValidationResult Validate(string encoded)
/// </summary>
public virtual ValidationResult Validate(string encoded, int index, int length)
{
return Validate(encoded, index, length, PadSymbol, GetOrCreateLookupTable(Alphabet));
return Validate(encoded, index, length, PadSymbol, GetOrCreateLookupTable());
}

internal LookupTable GetOrCreateLookupTable(string alphabet)
/// <summary>
/// Return lookup table after building if needed
/// </summary>
protected LookupTable GetOrCreateLookupTable()
{
return _lookupTable ?? (_lookupTable = BuildLookupTable(alphabet));
return _lookupTable ?? (_lookupTable = BuildLookupTable(Alphabet));
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down Expand Up @@ -140,7 +164,10 @@ private static LookupTable BuildLookupTable(string alphabet)
return new LookupTable(min, table);
}

private static unsafe void ToBase32GroupsUnsafe(byte* pInput, char* pOutput, char* pAlphabet,
/// <summary>
/// Core algorithm for encoding full groups
/// </summary>
protected static unsafe void ToBase32GroupsUnsafe(byte* pInput, char* pOutput, char* pAlphabet,
int inputGroupsCount)
{
for (int i = 0; i < inputGroupsCount; i++)
Expand All @@ -165,7 +192,10 @@ private static unsafe void ToBase32GroupsUnsafe(byte* pInput, char* pOutput, cha
}
}

private static unsafe int ToBase32RemainderUnsafe(byte* pInput, char* pOutput, char* pAlphabet, int remainder)
/// <summary>
/// Core algorithm for encoding remainder after groups
/// </summary>
protected static unsafe int ToBase32RemainderUnsafe(byte* pInput, char* pOutput, char* pAlphabet, int remainder)
{
ulong value = *pInput++;
for (int j = 1; j < remainder; j++)
Expand Down Expand Up @@ -217,7 +247,10 @@ private static unsafe void ToBase32Unsafe(byte[] input, int inputOffset, char[]
}
}

private static unsafe void ToBytesGroupsUnsafe(char* pEncoded, byte* pOutput, int encodedGroupsCount,
/// <summary>
/// Core algorithm for decoding full groups
/// </summary>
protected static unsafe void ToBytesGroupsUnsafe(char* pEncoded, byte* pOutput, int encodedGroupsCount,
int* pLookup, int lookupSize, int lowCode)
{
ulong value = 0;
Expand Down Expand Up @@ -251,7 +284,10 @@ private static unsafe void ToBytesGroupsUnsafe(char* pEncoded, byte* pOutput, in
}
}

private static unsafe void ToBytesRemainderUnsafe(char* pEncoded, byte* pOutput, int remainder,
/// <summary>
/// Core algorithm for decoding remainder after groups
/// </summary>
protected static unsafe void ToBytesRemainderUnsafe(char* pEncoded, byte* pOutput, int remainder,
int* pLookup, int lookupSize, int lowCode)
{
ulong value = 0;
Expand Down Expand Up @@ -385,7 +421,7 @@ private static int GetRemainderWithChecks(string encoded, int index, int length,
return remainder;
}

internal static byte[] ToBytes(string encoded, int index, int length, char? padSymbol, LookupTable lookupTable)
private static byte[] ToBytes(string encoded, int index, int length, char? padSymbol, LookupTable lookupTable)
{
CheckToBytesArguments(encoded, index, length, lookupTable);

Expand Down Expand Up @@ -419,7 +455,7 @@ internal static byte[] ToBytes(string encoded, int index, int length, char? padS
return bytes;
}

internal static ValidationResult Validate(string encoded, int index, int length, char? padSymbol,
private static ValidationResult Validate(string encoded, int index, int length, char? padSymbol,
LookupTable lookupTable)
{
try
Expand Down
18 changes: 0 additions & 18 deletions src/Wiry.Base32/LookupTable.cs

This file was deleted.