|
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | +using System.Numerics; |
| 4 | +using System.Text; |
| 5 | + |
| 6 | +namespace Ipfs |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// A codec for Base-36. |
| 10 | + /// </summary> |
| 11 | + /// <remarks> |
| 12 | + /// <para> |
| 13 | + /// Provides encoding and decoding functionality for Base-36, with methods <see cref="EncodeToStringUc"/> and <see cref="EncodeToStringLc"/> for encoding, |
| 14 | + /// and <see cref="DecodeString"/> for decoding. The encoding methods offer both uppercase and lowercase options. |
| 15 | + /// </para> |
| 16 | + /// <para> |
| 17 | + /// The implementation is case-insensitive for decoding and allows for efficient conversion between byte arrays and Base-36 strings. |
| 18 | + /// </para> |
| 19 | + /// <para> |
| 20 | + /// Ported from https://github.com/multiformats/go-base36/blob/v0.2.0/base36.go |
| 21 | + /// </para> |
| 22 | + /// </remarks> |
| 23 | + public static class Base36 |
| 24 | + { |
| 25 | + // Constants for the encoding alphabets |
| 26 | + private const string UcAlphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
| 27 | + private const string LcAlphabet = "0123456789abcdefghijklmnopqrstuvwxyz"; |
| 28 | + private const int MaxDigitOrdinal = 'z'; |
| 29 | + private const byte MaxDigitValueB36 = 35; |
| 30 | + |
| 31 | + // Reverse lookup table for decoding |
| 32 | + private static readonly byte[] RevAlphabet = new byte[MaxDigitOrdinal + 1]; |
| 33 | + |
| 34 | + // Static constructor to initialize the reverse lookup table |
| 35 | + static Base36() |
| 36 | + { |
| 37 | + // Initialize the reverse alphabet array with default values |
| 38 | + for (int i = 0; i < RevAlphabet.Length; i++) |
| 39 | + { |
| 40 | + RevAlphabet[i] = MaxDigitValueB36 + 1; |
| 41 | + } |
| 42 | + |
| 43 | + // Populate the reverse alphabet array for decoding |
| 44 | + for (int i = 0; i < UcAlphabet.Length; i++) |
| 45 | + { |
| 46 | + char c = UcAlphabet[i]; |
| 47 | + RevAlphabet[c] = (byte)i; |
| 48 | + if (c > '9') |
| 49 | + { |
| 50 | + RevAlphabet[char.ToLower(c)] = (byte)i; |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + /// <summary> |
| 56 | + /// Encodes a byte array to a Base-36 string using uppercase characters. |
| 57 | + /// </summary> |
| 58 | + /// <param name="bytes"> |
| 59 | + /// The byte array to encode. |
| 60 | + /// </param> |
| 61 | + /// <returns> |
| 62 | + /// The encoded Base-36 string in uppercase. |
| 63 | + /// </returns> |
| 64 | + public static string EncodeToStringUc(byte[] bytes) => Encode(bytes, UcAlphabet); |
| 65 | + |
| 66 | + /// <summary> |
| 67 | + /// Encodes a byte array to a Base-36 string using lowercase characters. |
| 68 | + /// </summary> |
| 69 | + /// <param name="bytes"> |
| 70 | + /// The byte array to encode. |
| 71 | + /// </param> |
| 72 | + /// <returns> |
| 73 | + /// The encoded Base-36 string in lowercase. |
| 74 | + /// </returns> |
| 75 | + public static string EncodeToStringLc(byte[] bytes) => Encode(bytes, LcAlphabet); |
| 76 | + |
| 77 | + // Core encoding logic for Base-36 conversion |
| 78 | + private static string Encode(byte[] input, string alphabet) |
| 79 | + { |
| 80 | + int zeroCount = 0; |
| 81 | + while (zeroCount < input.Length && input[zeroCount] == 0) |
| 82 | + { |
| 83 | + zeroCount++; |
| 84 | + } |
| 85 | + |
| 86 | + int size = zeroCount + (input.Length - zeroCount) * 277 / 179 + 1; |
| 87 | + byte[] buffer = new byte[size]; |
| 88 | + int index, stopIndex; |
| 89 | + uint carry; |
| 90 | + |
| 91 | + stopIndex = size - 1; |
| 92 | + for (int i = zeroCount; i < input.Length; i++) |
| 93 | + { |
| 94 | + index = size - 1; |
| 95 | + carry = input[i]; |
| 96 | + while (index > stopIndex || carry != 0) |
| 97 | + { |
| 98 | + carry += (uint)(buffer[index]) * 256; |
| 99 | + buffer[index] = (byte)(carry % 36); |
| 100 | + carry /= 36; |
| 101 | + index--; |
| 102 | + } |
| 103 | + stopIndex = index; |
| 104 | + } |
| 105 | + |
| 106 | + // The purpose of this loop is to skip over the portion of the buffer that contains only zeros (after accounting for any leading zeros in the original input). |
| 107 | + // This is important for the encoding process, as these leading zeros are not represented in the base-36 encoded string. |
| 108 | + for (stopIndex = zeroCount; stopIndex < size && buffer[stopIndex] == 0; stopIndex++) |
| 109 | + { |
| 110 | + } |
| 111 | + |
| 112 | + // Once the first non-zero byte is found, the actual encoding of the non-zero part of the buffer can begin. |
| 113 | + byte[] valueBuffer = new byte[buffer.Length - (stopIndex - zeroCount)]; |
| 114 | + for (int i = 0; i < valueBuffer.Length; i++) |
| 115 | + { |
| 116 | + valueBuffer[i] = (byte)alphabet[buffer[stopIndex - zeroCount + i]]; |
| 117 | + } |
| 118 | + |
| 119 | + return Encoding.ASCII.GetString(valueBuffer); |
| 120 | + } |
| 121 | + |
| 122 | + /// <summary> |
| 123 | + /// Decodes a Base-36 encoded string to a byte array. |
| 124 | + /// </summary> |
| 125 | + /// <param name="s"> |
| 126 | + /// The Base-36 encoded string to decode. |
| 127 | + /// </param> |
| 128 | + /// <returns> |
| 129 | + /// The decoded byte array. |
| 130 | + /// </returns> |
| 131 | + /// <exception cref="ArgumentException"> |
| 132 | + /// Thrown if the input string is null or empty. |
| 133 | + /// </exception> |
| 134 | + /// <exception cref="FormatException"> |
| 135 | + /// Thrown if the input string contains characters not valid in Base-36. |
| 136 | + /// </exception> |
| 137 | + public static byte[] DecodeString(string s) |
| 138 | + { |
| 139 | + if (string.IsNullOrEmpty(s)) |
| 140 | + { |
| 141 | + return Array.Empty<byte>(); |
| 142 | + } |
| 143 | + |
| 144 | + int zeroCount = 0; |
| 145 | + while (zeroCount < s.Length && s[zeroCount] == '0') |
| 146 | + { |
| 147 | + zeroCount++; |
| 148 | + } |
| 149 | + |
| 150 | + byte[] binu = new byte[2 * ((s.Length) * 179 / 277 + 1)]; |
| 151 | + uint[] outi = new uint[(s.Length + 3) / 4]; |
| 152 | + |
| 153 | + foreach (char r in s) |
| 154 | + { |
| 155 | + if (r > MaxDigitOrdinal || RevAlphabet[r] > MaxDigitValueB36) |
| 156 | + { |
| 157 | + throw new FormatException($"Invalid base36 character ({r})."); |
| 158 | + } |
| 159 | + |
| 160 | + ulong c = RevAlphabet[r]; |
| 161 | + |
| 162 | + for (int j = outi.Length - 1; j >= 0; j--) |
| 163 | + { |
| 164 | + ulong t = (ulong)outi[j] * 36 + c; |
| 165 | + c = (t >> 32); |
| 166 | + outi[j] = (uint)(t & 0xFFFFFFFF); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + uint mask = (uint)((s.Length % 4) * 8); |
| 171 | + if (mask == 0) |
| 172 | + { |
| 173 | + mask = 32; |
| 174 | + } |
| 175 | + mask -= 8; |
| 176 | + |
| 177 | + int outIndex = 0; |
| 178 | + for (int j = 0; j < outi.Length; j++) |
| 179 | + { |
| 180 | + for (; mask < 32; mask -= 8) |
| 181 | + { |
| 182 | + binu[outIndex] = (byte)(outi[j] >> (int)mask); |
| 183 | + outIndex++; |
| 184 | + } |
| 185 | + mask = 24; |
| 186 | + } |
| 187 | + |
| 188 | + for (int msb = zeroCount; msb < outIndex; msb++) |
| 189 | + { |
| 190 | + if (binu[msb] > 0) |
| 191 | + { |
| 192 | + int lengthToCopy = outIndex - msb; |
| 193 | + byte[] result = new byte[lengthToCopy]; |
| 194 | + Array.Copy(binu, msb, result, 0, lengthToCopy); |
| 195 | + return result; |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + return new byte[outIndex - zeroCount]; |
| 200 | + } |
| 201 | + } |
| 202 | +} |
0 commit comments