| | | 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.Diagnostics; |
| | | 5 | | using System.Runtime.CompilerServices; |
| | | 6 | | using System.Text; |
| | | 7 | | |
| | | 8 | | namespace System |
| | | 9 | | { |
| | | 10 | | internal static class IriHelper |
| | | 11 | | { |
| | | 12 | | // |
| | | 13 | | // Checks if provided non surrogate char lies in iri range |
| | | 14 | | // This method implements the ABNF checks per https://tools.ietf.org/html/rfc3987#section-2.2 |
| | | 15 | | // |
| | | 16 | | internal static bool CheckIriUnicodeRange(char unicode, bool isQuery) |
| | 0 | 17 | | { |
| | 0 | 18 | | return IsInInclusiveRange(unicode, '\u00A0', '\uD7FF') |
| | 0 | 19 | | || IsInInclusiveRange(unicode, '\uF900', '\uFDCF') |
| | 0 | 20 | | || IsInInclusiveRange(unicode, '\uFDF0', '\uFFEF') |
| | 0 | 21 | | || (isQuery && IsInInclusiveRange(unicode, '\uE000', '\uF8FF')); |
| | 0 | 22 | | } |
| | | 23 | | |
| | | 24 | | // |
| | | 25 | | // Check if highSurr and lowSurr are a surrogate pair then |
| | | 26 | | // it checks if the combined char is in the range |
| | | 27 | | // Takes in isQuery because iri restrictions for query are different |
| | | 28 | | // This method implements the ABNF checks per https://tools.ietf.org/html/rfc3987#section-2.2 |
| | | 29 | | // |
| | | 30 | | internal static bool CheckIriUnicodeRange(char highSurr, char lowSurr, out bool isSurrogatePair, bool isQuery) |
| | 0 | 31 | | { |
| | 0 | 32 | | Debug.Assert(char.IsHighSurrogate(highSurr)); |
| | | 33 | | |
| | 0 | 34 | | if (Rune.TryCreate(highSurr, lowSurr, out Rune rune)) |
| | 0 | 35 | | { |
| | 0 | 36 | | isSurrogatePair = true; |
| | | 37 | | |
| | | 38 | | // U+xxFFFE..U+xxFFFF is always private use for all planes, so we exclude it. |
| | | 39 | | // U+E0000..U+E0FFF is disallowed per the 'ucschar' definition in the ABNF. |
| | | 40 | | // U+F0000 and above are only allowed for 'iprivate' per the ABNF (isQuery = true). |
| | | 41 | | |
| | 0 | 42 | | return ((rune.Value & 0xFFFF) < 0xFFFE) |
| | 0 | 43 | | && ((uint)(rune.Value - 0xE0000) >= (0xE1000 - 0xE0000)) |
| | 0 | 44 | | && (isQuery || rune.Value < 0xF0000); |
| | | 45 | | } |
| | | 46 | | |
| | 0 | 47 | | isSurrogatePair = false; |
| | 0 | 48 | | return false; |
| | 0 | 49 | | } |
| | | 50 | | |
| | | 51 | | internal static bool CheckIriUnicodeRange(uint value, bool isQuery) |
| | 0 | 52 | | { |
| | 0 | 53 | | if (value <= 0xFFFF) |
| | 0 | 54 | | { |
| | 0 | 55 | | return IsInInclusiveRange(value, '\u00A0', '\uD7FF') |
| | 0 | 56 | | || IsInInclusiveRange(value, '\uF900', '\uFDCF') |
| | 0 | 57 | | || IsInInclusiveRange(value, '\uFDF0', '\uFFEF') |
| | 0 | 58 | | || (isQuery && IsInInclusiveRange(value, '\uE000', '\uF8FF')); |
| | | 59 | | } |
| | | 60 | | else |
| | 0 | 61 | | { |
| | 0 | 62 | | return ((value & 0xFFFF) < 0xFFFE) |
| | 0 | 63 | | && !IsInInclusiveRange(value, 0xE0000, 0xE0FFF) |
| | 0 | 64 | | && (isQuery || value < 0xF0000); |
| | | 65 | | } |
| | 0 | 66 | | } |
| | | 67 | | |
| | | 68 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 69 | | public static bool IsInInclusiveRange(uint value, uint min, uint max) |
| | 0 | 70 | | => (value - min) <= (max - min); |
| | | 71 | | |
| | | 72 | | // IRI normalization for strings containing characters that are not allowed or |
| | | 73 | | // escaped characters that should be unescaped in the context of the specified Uri component. |
| | | 74 | | public static void EscapeUnescapeIri(ref ValueStringBuilder dest, scoped ReadOnlySpan<char> span, bool isQuery) |
| | 0 | 75 | | { |
| | 0 | 76 | | Span<byte> maxUtf8EncodedSpan = [0, 0, 0, 0]; |
| | | 77 | | |
| | 0 | 78 | | for (int i = 0; (uint)i < (uint)span.Length; i++) |
| | 0 | 79 | | { |
| | 0 | 80 | | char ch = span[i]; |
| | | 81 | | |
| | 0 | 82 | | if (ch == '%') |
| | 0 | 83 | | { |
| | 0 | 84 | | if ((uint)(i + 2) < (uint)span.Length) |
| | 0 | 85 | | { |
| | 0 | 86 | | ch = UriHelper.DecodeHexChars(span[i + 1], span[i + 2]); |
| | | 87 | | |
| | | 88 | | // Do not unescape a reserved char |
| | 0 | 89 | | if (ch == Uri.c_DummyChar || UriHelper.IsNotSafeForUnescape(ch)) |
| | 0 | 90 | | { |
| | | 91 | | // keep as is |
| | 0 | 92 | | dest.Append(span[i]); |
| | 0 | 93 | | dest.Append(span[i + 1]); |
| | 0 | 94 | | dest.Append(span[i + 2]); |
| | 0 | 95 | | i += 2; |
| | 0 | 96 | | } |
| | 0 | 97 | | else if (ch <= '\x7F') |
| | 0 | 98 | | { |
| | | 99 | | // ASCII |
| | 0 | 100 | | dest.Append(ch); |
| | 0 | 101 | | i += 2; |
| | 0 | 102 | | } |
| | | 103 | | else |
| | 0 | 104 | | { |
| | | 105 | | // possibly utf8 encoded sequence of unicode |
| | 0 | 106 | | int charactersRead = PercentEncodingHelper.UnescapePercentEncodedUTF8Sequence( |
| | 0 | 107 | | span.Slice(i), |
| | 0 | 108 | | ref dest, |
| | 0 | 109 | | isQuery, |
| | 0 | 110 | | iriParsing: true); |
| | | 111 | | |
| | 0 | 112 | | Debug.Assert(charactersRead > 0); |
| | 0 | 113 | | i += charactersRead - 1; // -1 as i will be incremented in the loop |
| | 0 | 114 | | } |
| | | 115 | | |
| | 0 | 116 | | continue; |
| | | 117 | | } |
| | 0 | 118 | | } |
| | 0 | 119 | | else if (ch > '\x7f') |
| | 0 | 120 | | { |
| | | 121 | | // unicode |
| | | 122 | | |
| | | 123 | | bool isInIriUnicodeRange; |
| | 0 | 124 | | bool surrogatePair = false; |
| | | 125 | | |
| | 0 | 126 | | char ch2 = '\0'; |
| | | 127 | | |
| | 0 | 128 | | if (char.IsHighSurrogate(ch) && (uint)(i + 1) < (uint)span.Length) |
| | 0 | 129 | | { |
| | 0 | 130 | | ch2 = span[i + 1]; |
| | 0 | 131 | | isInIriUnicodeRange = CheckIriUnicodeRange(ch, ch2, out surrogatePair, isQuery); |
| | 0 | 132 | | } |
| | | 133 | | else |
| | 0 | 134 | | { |
| | 0 | 135 | | isInIriUnicodeRange = CheckIriUnicodeRange(ch, isQuery); |
| | 0 | 136 | | } |
| | | 137 | | |
| | 0 | 138 | | if (isInIriUnicodeRange) |
| | 0 | 139 | | { |
| | 0 | 140 | | dest.Append(ch); |
| | 0 | 141 | | if (surrogatePair) |
| | 0 | 142 | | { |
| | 0 | 143 | | dest.Append(ch2); |
| | 0 | 144 | | } |
| | 0 | 145 | | } |
| | | 146 | | else |
| | 0 | 147 | | { |
| | | 148 | | Rune rune; |
| | 0 | 149 | | if (surrogatePair) |
| | 0 | 150 | | { |
| | 0 | 151 | | rune = new Rune(ch, ch2); |
| | 0 | 152 | | } |
| | 0 | 153 | | else if (!Rune.TryCreate(ch, out rune)) |
| | 0 | 154 | | { |
| | 0 | 155 | | rune = Rune.ReplacementChar; |
| | 0 | 156 | | } |
| | | 157 | | |
| | 0 | 158 | | int bytesWritten = rune.EncodeToUtf8(maxUtf8EncodedSpan); |
| | 0 | 159 | | ReadOnlySpan<byte> encodedBytes = maxUtf8EncodedSpan.Slice(0, bytesWritten); |
| | | 160 | | |
| | 0 | 161 | | foreach (byte b in encodedBytes) |
| | 0 | 162 | | { |
| | 0 | 163 | | UriHelper.PercentEncodeByte(b, ref dest); |
| | 0 | 164 | | } |
| | 0 | 165 | | } |
| | | 166 | | |
| | 0 | 167 | | if (surrogatePair) |
| | 0 | 168 | | { |
| | 0 | 169 | | i++; |
| | 0 | 170 | | } |
| | | 171 | | |
| | 0 | 172 | | continue; |
| | | 173 | | } |
| | | 174 | | |
| | | 175 | | // ASCII, just copy the character |
| | 0 | 176 | | dest.Append(ch); |
| | 0 | 177 | | } |
| | 0 | 178 | | } |
| | | 179 | | } |
| | | 180 | | } |
| | | 181 | | |