Exemplo n.º 1
0
        /// <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>
 /// Initializes the LDAP DN safe lists.
 /// </summary>
 /// <returns>The DN safe list.</returns>
 private static char[][] InitialiseDistinguishedNameSafeList()
 {
     char[][] result = SafeList.Generate(255, new SafeList.GenerateSafeValue(SafeList.HashThenHexValueGenerator));
     SafeList.PunchSafeList(ref result, DistinguishedNameSafeList());
     EscapeDistinguisedNameCharacter(ref result, ',');
     EscapeDistinguisedNameCharacter(ref result, '+');
     EscapeDistinguisedNameCharacter(ref result, '"');
     EscapeDistinguisedNameCharacter(ref result, '\\');
     EscapeDistinguisedNameCharacter(ref result, '<');
     EscapeDistinguisedNameCharacter(ref result, '>');
     EscapeDistinguisedNameCharacter(ref result, ';');
     return(result);
 }
 /// <summary>
 /// Initializes the HTML safe list.
 /// </summary>
 private static void InitialiseSafeList()
 {
     AcquireWriteLock();
     try
     {
         if (characterValues == null)
         {
             characterValues = SafeList.Generate(65535, new SafeList.GenerateSafeValue(SafeList.HashThenValueGenerator));
             SafeList.PunchUnicodeThrough(ref characterValues, currentLowerCodeChartSettings, currentLowerMidCodeChartSettings, currentMidCodeChartSettings, currentUpperMidCodeChartSettings, currentUpperCodeChartSettings);
             ApplyHtmlSpecificValues();
         }
     }
     finally
     {
         ReleaseWriteLock();
     }
 }
 /// <summary>
 /// Marks characters from the specified languages as safe.
 /// </summary>
 /// <param name="lowerCodeCharts">The combination of lower code charts to use.</param>
 /// <param name="lowerMidCodeCharts">The combination of lower mid code charts to use.</param>
 /// <param name="midCodeCharts">The combination of mid code charts to use.</param>
 /// <param name="upperMidCodeCharts">The combination of upper mid code charts to use.</param>
 /// <param name="upperCodeCharts">The combination of upper code charts to use.</param>
 /// <remarks>The safe list affects all HTML and XML encoding functions.</remarks>
 public static void MarkAsSafe(LowerCodeCharts lowerCodeCharts, LowerMidCodeCharts lowerMidCodeCharts, MidCodeCharts midCodeCharts, UpperMidCodeCharts upperMidCodeCharts, UpperCodeCharts upperCodeCharts)
 {
     if (lowerCodeCharts == currentLowerCodeChartSettings && lowerMidCodeCharts == currentLowerMidCodeChartSettings && midCodeCharts == currentMidCodeChartSettings && upperMidCodeCharts == currentUpperMidCodeChartSettings && upperCodeCharts == currentUpperCodeChartSettings)
     {
         return;
     }
     AcquireWriteLock();
     try
     {
         characterValues = SafeList.Generate(65536, new SafeList.GenerateSafeValue(SafeList.HashThenValueGenerator));
         SafeList.PunchUnicodeThrough(ref characterValues, lowerCodeCharts, lowerMidCodeCharts, midCodeCharts, upperMidCodeCharts, upperCodeCharts);
         ApplyHtmlSpecificValues();
         currentLowerCodeChartSettings    = lowerCodeCharts;
         currentLowerMidCodeChartSettings = lowerMidCodeCharts;
         currentMidCodeChartSettings      = midCodeCharts;
         currentUpperMidCodeChartSettings = upperMidCodeCharts;
         currentUpperCodeChartSettings    = upperCodeCharts;
     }
     finally
     {
         ReleaseWriteLock();
     }
 }
 /// <summary>
 /// Initializes the LDAP filter safe list.
 /// </summary>
 /// <returns>The LDAP filter safe list.</returns>
 private static char[][] InitialiseFilterSafeList()
 {
     char[][] result = SafeList.Generate(255, new SafeList.GenerateSafeValue(SafeList.SlashThenHexValueGenerator));
     SafeList.PunchSafeList(ref result, FilterEncodingSafeList());
     return(result);
 }
        /// <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());
        }
Exemplo n.º 7
0
 /// <summary>
 /// Initializes the Url Path safe list.
 /// </summary>
 /// <returns>A list of characters and their encoded values for URL encoding.</returns>
 private static char[][] InitialisePathSafeList()
 {
     char[][] result = SafeList.Generate(255, new SafeList.GenerateSafeValue(SafeList.PercentThenHexValueGenerator));
     SafeList.PunchSafeList(ref result, UrlPathSafeList());
     return(result);
 }
Exemplo n.º 8
0
 /// <summary>
 /// Initializes the CSS safe list.
 /// </summary>
 /// <returns>
 /// The CSS safe list.
 /// </returns>
 private static char[][] InitialiseSafeList()
 {
     char[][] result = SafeList.Generate(255, new SafeList.GenerateSafeValue(SafeList.SlashThenSixDigitHexValueGenerator));
     SafeList.PunchSafeList(ref result, CssSafeList());
     return(result);
 }