/// <summary>
 /// Acquires the session dictionary Read Lock
 /// </summary>
 private void AcquireReadLock()
 {
     #if NET20
     rwl.AcquireReaderLock(Timeout.Infinite);
     #else
     rwl.EnterReadLock();
     #endif
 }
Beispiel #2
0
        /// <summary>
        /// Encodes the input string for use in LDAP filters.
        /// </summary>
        /// <param name="input">The string to encode.</param>
        /// <returns>An encoded version of the input string suitable for use in LDAP filters.</returns>
        internal static string FilterEncode(string input)
        {
            if (string.IsNullOrEmpty(input))
            {
                return(input);
            }

            if (filterCharacterValues == null)
            {
                InitialiseFilterSafeList();
            }

            // RFC 4515 states strings must be converted to their UTF8 value before search filter encoding.
            // See http://tools.ietf.org/html/rfc4515
            // Conversion to char[] keeps null characters inline.
            byte[] utf8Bytes    = Encoding.UTF8.GetBytes(input.ToCharArray());
            char[] encodedInput = new char[utf8Bytes.Length * 3]; // Each byte can potentially be encoded as %xx
            int    outputLength = 0;

            FilterSafeListSyncLock.EnterReadLock();
            try
            {
                for (int characterPosition = 0; characterPosition < utf8Bytes.Length; characterPosition++)
                {
                    byte currentCharacter = utf8Bytes[characterPosition];

                    if (filterCharacterValues[currentCharacter] != null)
                    {
                        // Character needs encoding.
                        char[] encodedCharacter = filterCharacterValues[currentCharacter];

                        for (int j = 0; j < encodedCharacter.Length; j++)
                        {
                            encodedInput[outputLength++] = encodedCharacter[j];
                        }
                    }
                    else
                    {
                        // Character does not need encoding.
                        encodedInput[outputLength++] = (char)currentCharacter;
                    }
                }
            }
            finally
            {
                FilterSafeListSyncLock.ExitReadLock();
            }

            return(new string(encodedInput, 0, outputLength));
        }
 /// <summary>
 /// Acquires a read lock.
 /// </summary>
 private static void AcquireReadLock()
 {
     SyncLock.EnterReadLock();
 }
Beispiel #4
0
        /// <summary>
        /// Looks up the actual type of an object to be deserialized.
        /// </summary>
        /// <param name="nominalType">The nominal type of the object.</param>
        /// <param name="discriminator">The discriminator.</param>
        /// <returns>The actual type of the object.</returns>
        public static Type LookupActualType(Type nominalType, BsonValue discriminator)
        {
            if (discriminator == null)
            {
                return(nominalType);
            }

            // note: EnsureKnownTypesAreRegistered handles its own locking so call from outside any lock
            EnsureKnownTypesAreRegistered(nominalType);

            __configLock.EnterReadLock();
            try
            {
                Type actualType = null;

                HashSet <Type> hashSet;
                if (__discriminators.TryGetValue(discriminator, out hashSet))
                {
                    foreach (var type in hashSet)
                    {
                        if (nominalType.IsAssignableFrom(type))
                        {
                            if (actualType == null)
                            {
                                actualType = type;
                            }
                            else
                            {
                                string message = string.Format("Ambiguous discriminator '{0}'.", discriminator);
                                throw new BsonSerializationException(message);
                            }
                        }
                    }
                }

                if (actualType == null && discriminator.IsString)
                {
                    actualType = TypeNameDiscriminator.GetActualType(discriminator.AsString); // see if it's a Type name
                }

                if (actualType == null)
                {
                    string message = string.Format("Unknown discriminator value '{0}'.", discriminator);
                    throw new BsonSerializationException(message);
                }

                if (!nominalType.IsAssignableFrom(actualType))
                {
                    string message = string.Format(
                        "Actual type {0} is not assignable to expected type {1}.",
                        actualType.FullName, nominalType.FullName);
                    throw new BsonSerializationException(message);
                }

                return(actualType);
            }
            finally
            {
                __configLock.ExitReadLock();
            }
        }
        /// <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="MethodSpecificEncoder"/> function, if needed.</param>
        /// <returns>
        /// Encoded string for use in HTML.
        /// </returns>
        /// <exception cref="InvalidUnicodeValueException">Thrown if a character with an invalid Unicode value is encountered within the input string.</exception>
        /// <exception cref="InvalidSurrogatePairException">Thrown if a high surrogate code point is encoded without a following low surrogate code point, or a
        /// low surrogate code point is encounter without having been preceded by a high surrogate code point.</exception>
        private static string HtmlEncode(string input, bool useNamedEntities, MethodSpecificEncoder encoderTweak)
        {
            if (string.IsNullOrEmpty(input))
            {
                return(input);
            }

            if (characterValues == null)
            {
                InitialiseSafeList();
            }

            if (useNamedEntities && namedEntities == null)
            {
                InitialiseNamedEntityList();
            }

            // Setup a new character array for output.
            char[] inputAsArray = input.ToCharArray();
            int    outputLength = 0;
            int    inputLength  = inputAsArray.Length;

            char[] encodedInput = new char[inputLength * 10]; // Worse case scenario - the longest entity name, thetasym is 10 characters, including the & and ;.

            SyncLock.EnterReadLock();
            try
            {
                for (int i = 0; i < inputLength; i++)
                {
                    char   currentCharacter = inputAsArray[i];
                    int    currentCodePoint = inputAsArray[i];
                    char[] tweekedValue;

                    // Check for invalid values
                    if (currentCodePoint == 0xFFFE ||
                        currentCodePoint == 0xFFFF)
                    {
                        throw new InvalidUnicodeValueException(currentCodePoint);
                    }
                    else if (currentCharacter.IsHighSurrogate())
                    {
                        if (i + 1 == inputLength)
                        {
                            throw new InvalidSurrogatePairException(currentCharacter, '\0');
                        }

                        // Now peak ahead and check if the following character is a low surrogate.
                        char nextCharacter = inputAsArray[i + 1];
                        char nextCodePoint = inputAsArray[i + 1];
                        if (!nextCharacter.IsLowSurrogate())
                        {
                            throw new InvalidSurrogatePairException(currentCharacter, nextCharacter);
                        }

                        // Look-ahead was good, so skip.
                        i++;

                        // Calculate the combined code point
                        long combinedCodePoint =
                            0x10000 + ((currentCodePoint - 0xD800) * 0x400) + (nextCodePoint - 0xDC00);
                        char[] encodedCharacter = SafeList.HashThenValueGenerator(combinedCodePoint);
                        encodedInput[outputLength++] = '&';

                        for (int j = 0; j < encodedCharacter.Length; j++)
                        {
                            encodedInput[outputLength++] = encodedCharacter[j];
                        }

                        encodedInput[outputLength++] = ';';
                    }
                    else if (currentCharacter.IsLowSurrogate())
                    {
                        throw new InvalidSurrogatePairException('\0', currentCharacter);
                    }
                    else if (encoderTweak != null && encoderTweak(currentCharacter, out tweekedValue))
                    {
                        for (int j = 0; j < tweekedValue.Length; j++)
                        {
                            encodedInput[outputLength++] = tweekedValue[j];
                        }
                    }
                    else if (useNamedEntities && namedEntities[currentCodePoint] != null)
                    {
                        char[] encodedCharacter = namedEntities[currentCodePoint];
                        encodedInput[outputLength++] = '&';

                        for (int j = 0; j < encodedCharacter.Length; j++)
                        {
                            encodedInput[outputLength++] = encodedCharacter[j];
                        }

                        encodedInput[outputLength++] = ';';
                    }
                    else if (characterValues[currentCodePoint] != null)
                    {
                        // character needs to be encoded
                        char[] encodedCharacter = characterValues[currentCodePoint];
                        encodedInput[outputLength++] = '&';

                        for (int j = 0; j < encodedCharacter.Length; j++)
                        {
                            encodedInput[outputLength++] = encodedCharacter[j];
                        }

                        encodedInput[outputLength++] = ';';
                    }
                    else
                    {
                        // character does not need encoding
                        encodedInput[outputLength++] = currentCharacter;
                    }
                }
            }
            finally
            {
                SyncLock.ExitReadLock();
            }

            return(new string(encodedInput, 0, outputLength));
        }
Beispiel #6
0
        /// <summary>
        /// Encodes the input string for use in LDAP DNs.
        /// </summary>
        /// <param name="input">The string to encode.</param>
        /// <param name="useInitialCharacterRules">Value indicating whether the special case rules for encoding of spaces and octothorpes at the start of a string are used.</param>
        /// <param name="useFinalCharacterRule">Value indicating whether the special case for encoding of final character spaces is used.</param>
        /// <returns>An encoded version of the input string suitable for use in LDAP DNs.</returns>
        internal static string DistinguishedNameEncode(string input, bool useInitialCharacterRules, bool useFinalCharacterRule)
        {
            if (string.IsNullOrEmpty(input))
            {
                return(input);
            }

            if (distinguishedNameCharacterValues == null)
            {
                InitialiseDistinguishedNameSafeList();
            }

            byte[] utf8Bytes    = Encoding.UTF8.GetBytes(input.ToCharArray());
            char[] encodedInput = new char[utf8Bytes.Length * 3]; // Each byte can potentially be encoded as #xx
            int    outputLength = 0;

            DistinguishedNameSafeListSyncLock.EnterReadLock();
            try
            {
                for (int characterPosition = 0; characterPosition < utf8Bytes.Length; characterPosition++)
                {
                    byte currentCharacter = utf8Bytes[characterPosition];

                    if (characterPosition == 0 && currentCharacter == ' ' && useInitialCharacterRules)
                    {
                        // rfc2253 states spaces at the start of a string must be escaped
                        encodedInput[outputLength++] = '\\';
                        encodedInput[outputLength++] = ' ';
                    }
                    else if (characterPosition == 0 && currentCharacter == '#' && useInitialCharacterRules)
                    {
                        // rfc2253 states hashes at the start of a string must be escaped
                        encodedInput[outputLength++] = '\\';
                        encodedInput[outputLength++] = '#';
                    }
                    else if (characterPosition == (utf8Bytes.Length - 1) && currentCharacter == ' ' &&
                             useFinalCharacterRule)
                    {
                        // rfc2253 states spaces at the end of a string must be escaped
                        encodedInput[outputLength++] = '\\';
                        encodedInput[outputLength++] = ' ';
                    }
                    else if (distinguishedNameCharacterValues[currentCharacter] != null)
                    {
                        // Character needs encoding.
                        char[] encodedCharacter = distinguishedNameCharacterValues[currentCharacter];

                        for (int j = 0; j < encodedCharacter.Length; j++)
                        {
                            encodedInput[outputLength++] = encodedCharacter[j];
                        }
                    }
                    else
                    {
                        // Character does not need encoding.
                        encodedInput[outputLength++] = (char)currentCharacter;
                    }
                }
            }
            finally
            {
                DistinguishedNameSafeListSyncLock.ExitReadLock();
            }

            return(new string(encodedInput, 0, outputLength));
        }
Beispiel #7
0
 public override void Enter()
 {
     ReaderWriterLock.EnterReadLock();
 }
Beispiel #8
0
        /// <summary>
        /// Encodes according to the CSS encoding rules.
        /// </summary>
        /// <param name="input">The string to encode.</param>
        /// <returns>The encoded string.</returns>
        /// <exception cref="InvalidUnicodeValueException">Thrown if a character with an invalid Unicode value is encountered within the input string.</exception>
        /// <exception cref="InvalidSurrogatePairException">Thrown if a high surrogate code point is encoded without a following low surrogate code point, or a
        /// low surrogate code point is encounter without having been preceded by a high surrogate code point.</exception>
        internal static string Encode(string input)
        {
            if (string.IsNullOrEmpty(input))
            {
                return(input);
            }

            if (characterValues == null)
            {
                InitialiseSafeList();
            }

            // Setup a new character array for output.
            char[] inputAsArray = input.ToCharArray();
            int    outputLength = 0;
            int    inputLength  = inputAsArray.Length;

            char[] encodedInput = new char[inputLength * 7]; // Worse case scenario - CSS encoding wants \XXXXXX for encoded characters.

            syncLock.EnterReadLock();
            try
            {
                for (int i = 0; i < inputLength; i++)
                {
                    char currentCharacter = inputAsArray[i];
                    int  currentCodePoint = inputAsArray[i];

                    // Check for invalid values
                    if (currentCodePoint == 0xFFFE ||
                        currentCodePoint == 0xFFFF)
                    {
                        throw new InvalidUnicodeValueException(currentCodePoint);
                    }
                    else if (currentCharacter.IsHighSurrogate())
                    {
                        if (i + 1 == inputLength)
                        {
                            throw new InvalidSurrogatePairException(currentCharacter, '\0');
                        }

                        // Now peak ahead and check if the following character is a low surrogate.
                        char nextCharacter = inputAsArray[i + 1];
                        char nextCodePoint = inputAsArray[i + 1];
                        if (!nextCharacter.IsLowSurrogate())
                        {
                            throw new InvalidSurrogatePairException(currentCharacter, nextCharacter);
                        }

                        // Look-ahead was good, so skip.
                        i++;

                        // Calculate the combined code point
                        long combinedCodePoint =
                            0x10000 + ((currentCodePoint - 0xD800) * 0x400) + (nextCodePoint - 0xDC00);
                        char[] encodedCharacter = SafeList.SlashThenSixDigitHexValueGenerator(combinedCodePoint);

                        for (int j = 0; j < encodedCharacter.Length; j++)
                        {
                            encodedInput[outputLength++] = encodedCharacter[j];
                        }
                    }
                    else if (currentCharacter.IsLowSurrogate())
                    {
                        throw new InvalidSurrogatePairException('\0', currentCharacter);
                    }
                    else if (currentCodePoint > characterValues.Length - 1)
                    {
                        char[] encodedCharacter = SafeList.SlashThenSixDigitHexValueGenerator(currentCodePoint);

                        for (int j = 0; j < encodedCharacter.Length; j++)
                        {
                            encodedInput[outputLength++] = encodedCharacter[j];
                        }
                    }
                    else if (characterValues[currentCodePoint] != null)
                    {
                        // character needs to be encoded
                        char[] encodedCharacter = characterValues[currentCodePoint];
                        for (int j = 0; j < encodedCharacter.Length; j++)
                        {
                            encodedInput[outputLength++] = encodedCharacter[j];
                        }
                    }
                    else
                    {
                        // character does not need encoding
                        encodedInput[outputLength++] = currentCharacter;
                    }
                }
            }
            finally
            {
                syncLock.ExitReadLock();
            }

            return(new string(encodedInput, 0, outputLength));
        }