Fix StyleCop warning SA1005 (single line comment spacing)
[mono-project.git] / netcore / System.Private.CoreLib / shared / System / Convert.Base64.cs
blob36fa578eda7bdfc3e67b1ed9df8d62c09b8d035c
1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
5 using System.Diagnostics;
6 using System.Runtime.CompilerServices;
7 using System.Runtime.InteropServices;
8 using Internal.Runtime.CompilerServices;
10 namespace System
12 public static partial class Convert
14 /// <summary>
15 /// Decode the span of UTF-16 encoded text represented as base 64 into binary data.
16 /// If the input is not a multiple of 4, or contains illegal characters, it will decode as much as it can, to the largest possible multiple of 4.
17 /// This invariant allows continuation of the parse with a slower, whitespace-tolerant algorithm.
18 ///
19 /// <param name="utf16">The input span which contains UTF-16 encoded text in base 64 that needs to be decoded.</param>
20 /// <param name="bytes">The output span which contains the result of the operation, i.e. the decoded binary data.</param>
21 /// <param name="consumed">The number of input bytes consumed during the operation. This can be used to slice the input for subsequent calls, if necessary.</param>
22 /// <param name="written">The number of bytes written into the output span. This can be used to slice the output for subsequent calls, if necessary.</param>
23 /// </summary>
24 /// <returns>Returns:
25 /// - true - The entire input span was successfully parsed.
26 /// - false - Only a part of the input span was successfully parsed. Failure causes may include embedded or trailing whitespace,
27 /// other illegal Base64 characters, trailing characters after an encoding pad ('='), an input span whose length is not divisible by 4
28 /// or a destination span that's too small. <paramref name="consumed"/> and <paramref name="written"/> are set so that
29 /// parsing can continue with a slower whitespace-tolerant algorithm.
30 ///
31 /// Note: This is a cut down version of the implementation of Base64.DecodeFromUtf8(), modified the accept UTF16 chars and act as a fast-path
32 /// helper for the Convert routines when the input string contains no whitespace.
33 /// </returns>
34 private static bool TryDecodeFromUtf16(ReadOnlySpan<char> utf16, Span<byte> bytes, out int consumed, out int written)
36 ref char srcChars = ref MemoryMarshal.GetReference(utf16);
37 ref byte destBytes = ref MemoryMarshal.GetReference(bytes);
39 int srcLength = utf16.Length & ~0x3; // only decode input up to the closest multiple of 4.
40 int destLength = bytes.Length;
42 int sourceIndex = 0;
43 int destIndex = 0;
45 if (utf16.Length == 0)
46 goto DoneExit;
48 ref sbyte decodingMap = ref MemoryMarshal.GetReference(DecodingMap);
50 // Last bytes could have padding characters, so process them separately and treat them as valid.
51 const int skipLastChunk = 4;
53 int maxSrcLength;
54 if (destLength >= (srcLength >> 2) * 3)
56 maxSrcLength = srcLength - skipLastChunk;
58 else
60 // This should never overflow since destLength here is less than int.MaxValue / 4 * 3 (i.e. 1610612733)
61 // Therefore, (destLength / 3) * 4 will always be less than 2147483641
62 maxSrcLength = (destLength / 3) * 4;
65 while (sourceIndex < maxSrcLength)
67 int result = Decode(ref Unsafe.Add(ref srcChars, sourceIndex), ref decodingMap);
68 if (result < 0)
69 goto InvalidExit;
70 WriteThreeLowOrderBytes(ref Unsafe.Add(ref destBytes, destIndex), result);
71 destIndex += 3;
72 sourceIndex += 4;
75 if (maxSrcLength != srcLength - skipLastChunk)
76 goto InvalidExit;
78 // If input is less than 4 bytes, srcLength == sourceIndex == 0
79 // If input is not a multiple of 4, sourceIndex == srcLength != 0
80 if (sourceIndex == srcLength)
82 goto InvalidExit;
85 int i0 = Unsafe.Add(ref srcChars, srcLength - 4);
86 int i1 = Unsafe.Add(ref srcChars, srcLength - 3);
87 int i2 = Unsafe.Add(ref srcChars, srcLength - 2);
88 int i3 = Unsafe.Add(ref srcChars, srcLength - 1);
89 if (((i0 | i1 | i2 | i3) & 0xffffff00) != 0)
90 goto InvalidExit;
92 i0 = Unsafe.Add(ref decodingMap, i0);
93 i1 = Unsafe.Add(ref decodingMap, i1);
95 i0 <<= 18;
96 i1 <<= 12;
98 i0 |= i1;
100 if (i3 != EncodingPad)
102 i2 = Unsafe.Add(ref decodingMap, i2);
103 i3 = Unsafe.Add(ref decodingMap, i3);
105 i2 <<= 6;
107 i0 |= i3;
108 i0 |= i2;
110 if (i0 < 0)
111 goto InvalidExit;
112 if (destIndex > destLength - 3)
113 goto InvalidExit;
114 WriteThreeLowOrderBytes(ref Unsafe.Add(ref destBytes, destIndex), i0);
115 destIndex += 3;
117 else if (i2 != EncodingPad)
119 i2 = Unsafe.Add(ref decodingMap, i2);
121 i2 <<= 6;
123 i0 |= i2;
125 if (i0 < 0)
126 goto InvalidExit;
127 if (destIndex > destLength - 2)
128 goto InvalidExit;
129 Unsafe.Add(ref destBytes, destIndex) = (byte)(i0 >> 16);
130 Unsafe.Add(ref destBytes, destIndex + 1) = (byte)(i0 >> 8);
131 destIndex += 2;
133 else
135 if (i0 < 0)
136 goto InvalidExit;
137 if (destIndex > destLength - 1)
138 goto InvalidExit;
139 Unsafe.Add(ref destBytes, destIndex) = (byte)(i0 >> 16);
140 destIndex += 1;
143 sourceIndex += 4;
145 if (srcLength != utf16.Length)
146 goto InvalidExit;
148 DoneExit:
149 consumed = sourceIndex;
150 written = destIndex;
151 return true;
153 InvalidExit:
154 consumed = sourceIndex;
155 written = destIndex;
156 Debug.Assert((consumed % 4) == 0);
157 return false;
160 [MethodImpl(MethodImplOptions.AggressiveInlining)]
161 private static int Decode(ref char encodedChars, ref sbyte decodingMap)
163 int i0 = encodedChars;
164 int i1 = Unsafe.Add(ref encodedChars, 1);
165 int i2 = Unsafe.Add(ref encodedChars, 2);
166 int i3 = Unsafe.Add(ref encodedChars, 3);
168 if (((i0 | i1 | i2 | i3) & 0xffffff00) != 0)
169 return -1; // One or more chars falls outside the 00..ff range. This cannot be a valid Base64 character.
171 i0 = Unsafe.Add(ref decodingMap, i0);
172 i1 = Unsafe.Add(ref decodingMap, i1);
173 i2 = Unsafe.Add(ref decodingMap, i2);
174 i3 = Unsafe.Add(ref decodingMap, i3);
176 i0 <<= 18;
177 i1 <<= 12;
178 i2 <<= 6;
180 i0 |= i3;
181 i1 |= i2;
183 i0 |= i1;
184 return i0;
187 [MethodImpl(MethodImplOptions.AggressiveInlining)]
188 private static void WriteThreeLowOrderBytes(ref byte destination, int value)
190 destination = (byte)(value >> 16);
191 Unsafe.Add(ref destination, 1) = (byte)(value >> 8);
192 Unsafe.Add(ref destination, 2) = (byte)value;
195 // Pre-computing this table using a custom string(s_characters) and GenerateDecodingMapAndVerify (found in tests)
196 private static ReadOnlySpan<sbyte> DecodingMap => new sbyte[] // rely on C# compiler optimization to reference static data
198 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
199 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
200 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, // 62 is placed at index 43 (for +), 63 at index 47 (for /)
201 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, // 52-61 are placed at index 48-57 (for 0-9), 64 at index 61 (for =)
202 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
203 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, // 0-25 are placed at index 65-90 (for A-Z)
204 -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
205 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, // 26-51 are placed at index 97-122 (for a-z)
206 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // Bytes over 122 ('z') are invalid and cannot be decoded
207 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // Hence, padding the map with 255, which indicates invalid input
208 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
209 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
210 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
211 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
212 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
213 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
216 private const byte EncodingPad = (byte)'='; // '=', for padding