From e7d974049a3b5ebae0bbf8c9d3ea0bace5a084c4 Mon Sep 17 00:00:00 2001 From: Robert Schmidt Date: Thu, 23 Apr 2020 20:41:24 +0200 Subject: [PATCH] Make internal algorithms protected so they can be used from derived classes. Move LookupTable inside Base32Encoding to make this work without making it public. --- src/Wiry.Base32/Base32Encoding.cs | 56 +++++++++++++++++++++++++------ src/Wiry.Base32/LookupTable.cs | 18 ---------- 2 files changed, 46 insertions(+), 28 deletions(-) delete mode 100644 src/Wiry.Base32/LookupTable.cs diff --git a/src/Wiry.Base32/Base32Encoding.cs b/src/Wiry.Base32/Base32Encoding.cs index 8fbb645..45fd6a3 100644 --- a/src/Wiry.Base32/Base32Encoding.cs +++ b/src/Wiry.Base32/Base32Encoding.cs @@ -13,6 +13,27 @@ namespace Wiry.Base32 /// public abstract class Base32Encoding : IBase32Encoding { + /// + /// Reverse alphabet lookup table + /// + protected sealed class LookupTable + { + /// + /// Low code + /// + public int LowCode { get; } + /// + /// Values + /// + 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"; @@ -82,7 +103,7 @@ public virtual byte[] ToBytes(string encoded) /// 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()); } /// @@ -101,12 +122,15 @@ public virtual ValidationResult Validate(string encoded) /// 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) + /// + /// Return lookup table after building if needed + /// + protected LookupTable GetOrCreateLookupTable() { - return _lookupTable ?? (_lookupTable = BuildLookupTable(alphabet)); + return _lookupTable ?? (_lookupTable = BuildLookupTable(Alphabet)); } [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -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, + /// + /// Core algorithm for encoding full groups + /// + protected static unsafe void ToBase32GroupsUnsafe(byte* pInput, char* pOutput, char* pAlphabet, int inputGroupsCount) { for (int i = 0; i < inputGroupsCount; i++) @@ -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) + /// + /// Core algorithm for encoding remainder after groups + /// + protected static unsafe int ToBase32RemainderUnsafe(byte* pInput, char* pOutput, char* pAlphabet, int remainder) { ulong value = *pInput++; for (int j = 1; j < remainder; j++) @@ -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, + /// + /// Core algorithm for decoding full groups + /// + protected static unsafe void ToBytesGroupsUnsafe(char* pEncoded, byte* pOutput, int encodedGroupsCount, int* pLookup, int lookupSize, int lowCode) { ulong value = 0; @@ -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, + /// + /// Core algorithm for decoding remainder after groups + /// + protected static unsafe void ToBytesRemainderUnsafe(char* pEncoded, byte* pOutput, int remainder, int* pLookup, int lookupSize, int lowCode) { ulong value = 0; @@ -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); @@ -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 diff --git a/src/Wiry.Base32/LookupTable.cs b/src/Wiry.Base32/LookupTable.cs deleted file mode 100644 index 2e41f54..0000000 --- a/src/Wiry.Base32/LookupTable.cs +++ /dev/null @@ -1,18 +0,0 @@ -// Copyright (c) Dmitry Razumikhin, 2016-2018. -// Licensed under the MIT License. -// See LICENSE in the project root for license information. - -namespace Wiry.Base32 -{ - internal sealed class LookupTable - { - public int LowCode { get; } - public int[] Values { get; } - - public LookupTable(int lowCode, int[] values) - { - LowCode = lowCode; - Values = values; - } - } -} \ No newline at end of file