| | | 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 | | |
| | | 4 | | using System.Buffers; |
| | | 5 | | using System.Diagnostics; |
| | | 6 | | using System.Runtime.CompilerServices; |
| | | 7 | | using System.Runtime.InteropServices; |
| | | 8 | | using System.Text; |
| | | 9 | | |
| | | 10 | | namespace System.Globalization |
| | | 11 | | { |
| | | 12 | | internal static partial class Normalization |
| | | 13 | | { |
| | | 14 | | private static unsafe bool NlsIsNormalized(ReadOnlySpan<char> source, NormalizationForm normalizationForm) |
| | | 15 | | { |
| | 0 | 16 | | Debug.Assert(!GlobalizationMode.Invariant); |
| | 0 | 17 | | Debug.Assert(GlobalizationMode.UseNls); |
| | 0 | 18 | | Debug.Assert(!source.IsEmpty); |
| | 0 | 19 | | Debug.Assert(normalizationForm == NormalizationForm.FormC || normalizationForm == NormalizationForm.FormD || |
| | | 20 | | |
| | | 21 | | Interop.BOOL result; |
| | 0 | 22 | | fixed (char* pInput = source) |
| | | 23 | | { |
| | 0 | 24 | | result = Interop.Normaliz.IsNormalizedString(normalizationForm, pInput, source.Length); |
| | | 25 | | } |
| | | 26 | | |
| | | 27 | | // The only way to know if IsNormalizedString failed is through checking the Win32 last error |
| | | 28 | | // IsNormalizedString pinvoke has SetLastError attribute property which will set the last error |
| | | 29 | | // to 0 (ERROR_SUCCESS) before executing the calls. |
| | 0 | 30 | | CheckLastErrorAndThrowIfFailed(nameof(source)); |
| | | 31 | | |
| | 0 | 32 | | return result != Interop.BOOL.FALSE; |
| | | 33 | | } |
| | | 34 | | |
| | | 35 | | private static unsafe string NlsNormalize(string strInput, NormalizationForm normalizationForm) |
| | | 36 | | { |
| | 0 | 37 | | Debug.Assert(!GlobalizationMode.Invariant); |
| | 0 | 38 | | Debug.Assert(GlobalizationMode.UseNls); |
| | 0 | 39 | | Debug.Assert(strInput != null); |
| | 0 | 40 | | Debug.Assert(normalizationForm == NormalizationForm.FormC || normalizationForm == NormalizationForm.FormD || |
| | | 41 | | |
| | 0 | 42 | | if (strInput.Length == 0) |
| | | 43 | | { |
| | 0 | 44 | | return string.Empty; |
| | | 45 | | } |
| | | 46 | | |
| | 0 | 47 | | char[]? toReturn = null; |
| | | 48 | | try |
| | | 49 | | { |
| | | 50 | | const int StackallocThreshold = 512; |
| | | 51 | | |
| | 0 | 52 | | Span<char> buffer = strInput.Length <= StackallocThreshold |
| | 0 | 53 | | ? stackalloc char[StackallocThreshold] |
| | 0 | 54 | | : (toReturn = ArrayPool<char>.Shared.Rent(strInput.Length)); |
| | | 55 | | |
| | | 56 | | while (true) |
| | | 57 | | { |
| | | 58 | | // we depend on Win32 last error when calling NormalizeString |
| | | 59 | | // NormalizeString pinvoke has SetLastError attribute property which will set the last error |
| | | 60 | | // to 0 (ERROR_SUCCESS) before executing the calls. |
| | | 61 | | int realLength; |
| | 0 | 62 | | fixed (char* pInput = strInput) |
| | 0 | 63 | | fixed (char* pDest = &MemoryMarshal.GetReference(buffer)) |
| | | 64 | | { |
| | 0 | 65 | | realLength = Interop.Normaliz.NormalizeString(normalizationForm, pInput, strInput.Length, pDest, |
| | | 66 | | } |
| | 0 | 67 | | int lastError = Marshal.GetLastPInvokeError(); |
| | | 68 | | |
| | | 69 | | switch (lastError) |
| | | 70 | | { |
| | | 71 | | case Interop.Errors.ERROR_SUCCESS: |
| | 0 | 72 | | ReadOnlySpan<char> result = buffer.Slice(0, realLength); |
| | 0 | 73 | | return result.SequenceEqual(strInput) |
| | 0 | 74 | | ? strInput |
| | 0 | 75 | | : new string(result); |
| | | 76 | | |
| | | 77 | | // Do appropriate stuff for the individual errors: |
| | | 78 | | case Interop.Errors.ERROR_INSUFFICIENT_BUFFER: |
| | 0 | 79 | | realLength = Math.Abs(realLength); |
| | 0 | 80 | | Debug.Assert(realLength > buffer.Length, "Buffer overflow should have iLength > cBuffer.Leng |
| | 0 | 81 | | if (toReturn != null) |
| | | 82 | | { |
| | | 83 | | // Clear toReturn first to ensure we don't return the same buffer twice |
| | 0 | 84 | | char[] temp = toReturn; |
| | 0 | 85 | | toReturn = null; |
| | 0 | 86 | | ArrayPool<char>.Shared.Return(temp); |
| | | 87 | | } |
| | 0 | 88 | | Debug.Assert(realLength > StackallocThreshold); |
| | 0 | 89 | | buffer = toReturn = ArrayPool<char>.Shared.Rent(realLength); |
| | 0 | 90 | | continue; |
| | | 91 | | |
| | | 92 | | case Interop.Errors.ERROR_INVALID_PARAMETER: |
| | | 93 | | case Interop.Errors.ERROR_NO_UNICODE_TRANSLATION: |
| | | 94 | | // Illegal code point or order found. Ie: FFFE or D800 D800, etc. |
| | 0 | 95 | | throw new ArgumentException(SR.Argument_InvalidCharSequenceNoIndex, nameof(strInput)); |
| | | 96 | | |
| | | 97 | | case Interop.Errors.ERROR_NOT_ENOUGH_MEMORY: |
| | 0 | 98 | | throw new OutOfMemoryException(); |
| | | 99 | | |
| | | 100 | | default: |
| | | 101 | | // We shouldn't get here... |
| | 0 | 102 | | throw new InvalidOperationException(SR.Format(SR.UnknownError_Num, lastError)); |
| | | 103 | | } |
| | | 104 | | } |
| | | 105 | | } |
| | | 106 | | finally |
| | | 107 | | { |
| | 0 | 108 | | if (toReturn != null) |
| | | 109 | | { |
| | 0 | 110 | | ArrayPool<char>.Shared.Return(toReturn); |
| | | 111 | | } |
| | 0 | 112 | | } |
| | 0 | 113 | | } |
| | | 114 | | |
| | | 115 | | private static unsafe bool NlsTryNormalize(ReadOnlySpan<char> source, Span<char> destination, out int charsWritt |
| | | 116 | | { |
| | 0 | 117 | | Debug.Assert(!GlobalizationMode.Invariant); |
| | 0 | 118 | | Debug.Assert(GlobalizationMode.UseNls); |
| | 0 | 119 | | Debug.Assert(!source.IsEmpty); |
| | 0 | 120 | | Debug.Assert(normalizationForm == NormalizationForm.FormC || normalizationForm == NormalizationForm.FormD || |
| | | 121 | | |
| | 0 | 122 | | if (destination.IsEmpty) |
| | | 123 | | { |
| | 0 | 124 | | charsWritten = 0; |
| | 0 | 125 | | return false; |
| | | 126 | | } |
| | | 127 | | |
| | | 128 | | // we depend on Win32 last error when calling NormalizeString |
| | | 129 | | // NormalizeString pinvoke has SetLastError attribute property which will set the last error |
| | | 130 | | // to 0 (ERROR_SUCCESS) before executing the calls. |
| | | 131 | | |
| | | 132 | | int realLength; |
| | 0 | 133 | | fixed (char* pInput = source) |
| | 0 | 134 | | fixed (char* pDest = destination) |
| | | 135 | | { |
| | 0 | 136 | | realLength = Interop.Normaliz.NormalizeString(normalizationForm, pInput, source.Length, pDest, destinati |
| | | 137 | | } |
| | | 138 | | |
| | 0 | 139 | | int lastError = Marshal.GetLastPInvokeError(); |
| | | 140 | | switch (lastError) |
| | | 141 | | { |
| | | 142 | | case Interop.Errors.ERROR_SUCCESS: |
| | 0 | 143 | | charsWritten = realLength; |
| | 0 | 144 | | return true; |
| | | 145 | | |
| | | 146 | | // Do appropriate stuff for the individual errors: |
| | | 147 | | case Interop.Errors.ERROR_INSUFFICIENT_BUFFER: |
| | 0 | 148 | | charsWritten = 0; |
| | 0 | 149 | | return false; |
| | | 150 | | |
| | | 151 | | case Interop.Errors.ERROR_INVALID_PARAMETER: |
| | | 152 | | case Interop.Errors.ERROR_NO_UNICODE_TRANSLATION: |
| | | 153 | | // Illegal code point or order found. Ie: FFFE or D800 D800, etc. |
| | 0 | 154 | | throw new ArgumentException(SR.Argument_InvalidCharSequenceNoIndex, nameof(source)); |
| | | 155 | | |
| | | 156 | | case Interop.Errors.ERROR_NOT_ENOUGH_MEMORY: |
| | 0 | 157 | | throw new OutOfMemoryException(); |
| | | 158 | | |
| | | 159 | | default: |
| | | 160 | | // We shouldn't get here... |
| | 0 | 161 | | throw new InvalidOperationException(SR.Format(SR.UnknownError_Num, lastError)); |
| | | 162 | | } |
| | | 163 | | } |
| | | 164 | | |
| | | 165 | | private static unsafe int NlsGetNormalizedLength(ReadOnlySpan<char> source, NormalizationForm normalizationForm) |
| | | 166 | | { |
| | 0 | 167 | | Debug.Assert(!GlobalizationMode.Invariant); |
| | 0 | 168 | | Debug.Assert(GlobalizationMode.UseNls); |
| | 0 | 169 | | Debug.Assert(!source.IsEmpty); |
| | 0 | 170 | | Debug.Assert(normalizationForm == NormalizationForm.FormC || normalizationForm == NormalizationForm.FormD || |
| | | 171 | | |
| | | 172 | | // we depend on Win32 last error when calling NormalizeString |
| | | 173 | | // NormalizeString pinvoke has SetLastError attribute property which will set the last error |
| | | 174 | | // to 0 (ERROR_SUCCESS) before executing the calls. |
| | | 175 | | |
| | | 176 | | int realLength; |
| | 0 | 177 | | fixed (char* pInput = source) |
| | | 178 | | { |
| | 0 | 179 | | realLength = Interop.Normaliz.NormalizeString(normalizationForm, pInput, source.Length, null, 0); |
| | | 180 | | } |
| | | 181 | | |
| | 0 | 182 | | int lastError = Marshal.GetLastPInvokeError(); |
| | | 183 | | switch (lastError) |
| | | 184 | | { |
| | | 185 | | case Interop.Errors.ERROR_SUCCESS: |
| | 0 | 186 | | return realLength; |
| | | 187 | | |
| | | 188 | | case Interop.Errors.ERROR_INVALID_PARAMETER: |
| | | 189 | | case Interop.Errors.ERROR_NO_UNICODE_TRANSLATION: |
| | | 190 | | // Illegal code point or order found. Ie: FFFE or D800 D800, etc. |
| | 0 | 191 | | throw new ArgumentException(SR.Argument_InvalidCharSequenceNoIndex, nameof(source)); |
| | | 192 | | |
| | | 193 | | case Interop.Errors.ERROR_NOT_ENOUGH_MEMORY: |
| | 0 | 194 | | throw new OutOfMemoryException(); |
| | | 195 | | |
| | | 196 | | default: |
| | | 197 | | // We shouldn't get here... |
| | 0 | 198 | | throw new InvalidOperationException(SR.Format(SR.UnknownError_Num, lastError)); |
| | | 199 | | } |
| | | 200 | | } |
| | | 201 | | |
| | | 202 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 203 | | private static void CheckLastErrorAndThrowIfFailed(string inputName) |
| | | 204 | | { |
| | 0 | 205 | | int lastError = Marshal.GetLastPInvokeError(); |
| | | 206 | | switch (lastError) |
| | | 207 | | { |
| | | 208 | | case Interop.Errors.ERROR_SUCCESS: |
| | | 209 | | break; |
| | | 210 | | |
| | | 211 | | case Interop.Errors.ERROR_INVALID_PARAMETER: |
| | | 212 | | case Interop.Errors.ERROR_NO_UNICODE_TRANSLATION: |
| | 0 | 213 | | throw new ArgumentException(SR.Argument_InvalidCharSequenceNoIndex, inputName); |
| | | 214 | | |
| | | 215 | | case Interop.Errors.ERROR_NOT_ENOUGH_MEMORY: |
| | 0 | 216 | | throw new OutOfMemoryException(); |
| | | 217 | | |
| | | 218 | | default: |
| | 0 | 219 | | throw new InvalidOperationException(SR.Format(SR.UnknownError_Num, lastError)); |
| | | 220 | | } |
| | 0 | 221 | | } |
| | | 222 | | } |
| | | 223 | | } |
| | | 224 | | |