コード例 #1
0
ファイル: RdnComponent.cs プロジェクト: pykarun/njupiter
        private static string EscapeValue(string s, EscapeChars escapeChars)
        {
            var returnValue = new StringBuilder();

            for (var i = 0; i < s.Length; i++)
            {
                if (Rdn.IsSpecialChar(s[i]) || ((i == 0 || i == s.Length - 1) && s[i] == ' '))
                {
                    if ((escapeChars & EscapeChars.SpecialChars) != EscapeChars.None)
                    {
                        returnValue.Append('\\');
                    }

                    returnValue.Append(s[i]);
                }
                else if (s[i] < 32 && ((escapeChars & EscapeChars.ControlChars) != EscapeChars.None))
                {
                    returnValue.AppendFormat("\\{0:X2}", (int)s[i]);
                }
                else if (s[i] >= 128 && ((escapeChars & EscapeChars.MultibyteChars) != EscapeChars.None))
                {
                    var bytes = Encoding.UTF8.GetBytes(new[] { s[i] });

                    foreach (var b in bytes)
                    {
                        returnValue.AppendFormat("\\{0:X2}", b);
                    }
                }
                else
                {
                    returnValue.Append(s[i]);
                }
            }

            return(returnValue.ToString());
        }
コード例 #2
0
        private static List <IRdn> ParseDn(string dnString)
        {
            if (string.IsNullOrEmpty(dnString))
            {
                return(new List <IRdn>());
            }
            var rdns   = new List <IRdn>();
            var state  = ParserState.LookingForSeparator;
            var rawRdn = new StringBuilder();


            for (var position = 0; position < dnString.Length; ++position)
            {
                switch (state)
                {
                case ParserState.LookingForSeparator:
                    // If we find a separator character, we've hit the end of an Rdn.
                    // We'll store the Rdn, and we'll check to see if the Rdn is actually
                    // valid later.
                    if (dnString[position] == ',' || dnString[position] == ';')
                    {
                        var rdn = new Rdn(rawRdn.ToString());
                        rdns.Add(rdn);                         // Add the string to the list of raw Rdns
                        rawRdn.Length = 0;                     // Clear the StringBuilder to prepare for the next Rdn
                    }
                    else
                    {
                        // Add the character to our temporary Rdn string
                        rawRdn.Append(dnString[position]);

                        // If we find an escape character, store character that follows it,
                        // but don't consider it as a possible separator character.  If the
                        // string ends with an escape character, that's bad, and we should
                        // throw an exception
                        if (dnString[position] == '\\')
                        {
                            try {
                                rawRdn.Append(dnString[++position]);
                            } catch (IndexOutOfRangeException) {
                                throw new ArgumentException("Invalid Dn: DNs aren't allowed to end with an escape character.", dnString);
                            }
                        }
                        // If we find a quote, we'll change state so that we look for the closing quote
                        // and ignore any separator characters within.
                        else if (dnString[position] == '"')
                        {
                            state = ParserState.InQuotedString;
                        }
                    }

                    break;


                case ParserState.InQuotedString:
                    // Store the character
                    rawRdn.Append(dnString[position]);

                    // You're allowed to escape special characters in a quoted string, but not required
                    // to.  But if there's an escaped quote, we need to take special care to make sure
                    // that we don't mistake that for the end of the quoted string.
                    if (dnString[position] == '\\')
                    {
                        try {
                            rawRdn.Append(dnString[++position]);
                        } catch (IndexOutOfRangeException) {
                            throw new ArgumentException("Invalid Dn: DNs aren't allowed to end with an escape character.", dnString);
                        }
                    }
                    else if (dnString[position] == '"')
                    {
                        state = ParserState.LookingForSeparator;
                    }
                    break;
                }
            }

            // Take the last Rdn and add it to the list
            var lastRdn = new Rdn(rawRdn.ToString());

            rdns.Add(lastRdn);

            // Check parser's end state
            if (state == ParserState.InQuotedString)
            {
                throw new ArgumentException("Invalid Dn: Unterminated quoted string.", dnString);
            }

            return(rdns);
        }
コード例 #3
0
ファイル: Dn.cs プロジェクト: aelveborn/njupiter
		private static List<IRdn> ParseDn(string dnString) {
			if(string.IsNullOrEmpty(dnString)) {
				return new List<IRdn>();
			}
			var rdns = new List<IRdn>();
			var state = ParserState.LookingForSeparator;
			var rawRdn = new StringBuilder();


			for(var position = 0; position < dnString.Length; ++position) {
				switch(state) {
					case ParserState.LookingForSeparator:
					// If we find a separator character, we've hit the end of an Rdn.
					// We'll store the Rdn, and we'll check to see if the Rdn is actually
					// valid later.
					if(dnString[position] == ',' || dnString[position] == ';') {
						var rdn = new Rdn(rawRdn.ToString());
						rdns.Add(rdn); // Add the string to the list of raw Rdns
						rawRdn.Length = 0;              // Clear the StringBuilder to prepare for the next Rdn
					} else {
						// Add the character to our temporary Rdn string
						rawRdn.Append(dnString[position]);

						// If we find an escape character, store character that follows it,
						// but don't consider it as a possible separator character.  If the
						// string ends with an escape character, that's bad, and we should
						// throw an exception
						if(dnString[position] == '\\') {
							try {
								rawRdn.Append(dnString[++position]);
							} catch(IndexOutOfRangeException) {
								throw new ArgumentException("Invalid Dn: DNs aren't allowed to end with an escape character.", dnString);
							}

						}
							// If we find a quote, we'll change state so that we look for the closing quote
							// and ignore any separator characters within.
						else if(dnString[position] == '"') {
							state = ParserState.InQuotedString;
						}
					}

					break;


					case ParserState.InQuotedString:
					// Store the character
					rawRdn.Append(dnString[position]);

					// You're allowed to escape special characters in a quoted string, but not required
					// to.  But if there's an escaped quote, we need to take special care to make sure
					// that we don't mistake that for the end of the quoted string.
					if(dnString[position] == '\\') {
						try {
							rawRdn.Append(dnString[++position]);
						} catch(IndexOutOfRangeException) {
							throw new ArgumentException("Invalid Dn: DNs aren't allowed to end with an escape character.", dnString);
						}
					} else if(dnString[position] == '"')
						state = ParserState.LookingForSeparator;
					break;

				}
			}

			// Take the last Rdn and add it to the list
			var lastRdn = new Rdn(rawRdn.ToString());
			rdns.Add(lastRdn);

			// Check parser's end state
			if(state == ParserState.InQuotedString)
				throw new ArgumentException("Invalid Dn: Unterminated quoted string.", dnString);

			return rdns;
		}