/// <summary> /// Encodes according to the CSS encoding rules. /// </summary> /// <param name="input">The string to encode.</param> /// <returns>The encoded string.</returns> internal static string Encode(string input) { if (string.IsNullOrEmpty(input)) { return(input); } char[][] value = characterValuesLazy.Value; StringBuilder outputStringBuilder = EncoderUtil.GetOutputStringBuilder(input.Length, 7); Utf16StringReader utf16StringReader = new Utf16StringReader(input); while (true) { int num = utf16StringReader.ReadNextScalarValue(); if (num < 0) { break; } if (num >= value.Length) { char[] value2 = SafeList.SlashThenSixDigitHexValueGenerator(num); outputStringBuilder.Append(value2); } else if (value[num] != null) { char[] value3 = value[num]; outputStringBuilder.Append(value3); } else { outputStringBuilder.Append((char)num); } } return(outputStringBuilder.ToString()); }
/// <summary> /// Encodes input strings for use in HTML. /// </summary> /// <param name="input">String to be encoded</param> /// <param name="useNamedEntities">Value indicating if the HTML 4.0 named entities should be used.</param> /// <param name="encoderTweak">A <see cref="T:Microsoft.Security.Application.MethodSpecificEncoder" /> function, if needed.</param> /// <returns> /// Encoded string for use in HTML. /// </returns> private static string HtmlEncode(string input, bool useNamedEntities, MethodSpecificEncoder encoderTweak) { if (string.IsNullOrEmpty(input)) { return(input); } if (characterValues == null) { InitialiseSafeList(); } char[][] array = null; if (useNamedEntities) { array = namedEntitiesLazy.Value; } StringBuilder outputStringBuilder = EncoderUtil.GetOutputStringBuilder(input.Length, 10); AcquireReadLock(); try { Utf16StringReader utf16StringReader = new Utf16StringReader(input); while (true) { int num = utf16StringReader.ReadNextScalarValue(); if (num < 0) { break; } if (num > 65535) { char[] value = SafeList.HashThenValueGenerator(num); outputStringBuilder.Append('&'); outputStringBuilder.Append(value); outputStringBuilder.Append(';'); } else { char c = (char)num; char[] value2; if (encoderTweak != null && encoderTweak(c, out value2)) { outputStringBuilder.Append(value2); } else if (useNamedEntities && array[num] != null) { char[] value3 = array[num]; outputStringBuilder.Append('&'); outputStringBuilder.Append(value3); outputStringBuilder.Append(';'); } else if (characterValues[num] != null) { char[] value4 = characterValues[num]; outputStringBuilder.Append('&'); outputStringBuilder.Append(value4); outputStringBuilder.Append(';'); } else { outputStringBuilder.Append(c); } } } } finally { ReleaseReadLock(); } return(outputStringBuilder.ToString()); }