// // This method reads a standard quoted string. Departing from the RFC, Unicode is permitted for display-name. // // Preconditions: // - Index must be within the bounds of the data string. // - The char at the given index is the initial quote. (data[index] == Quote) // // Return value: The next index past the terminating-quote (data[index + 1] == Quote). // e.g. In (bob "user name"@domain), starting at index=14 (") returns index=3 (space). // // A FormatException will be thrown if: // - A non-escaped character is encountered that is not valid in a quoted string. // - A Unicode character is encountered and Unicode has not been allowed. // - The final double quote is not found. // internal static int ReadReverseQuoted(string data, int index, bool permitUnicode) { Debug.Assert(0 <= index && index < data.Length, "Index out of range: " + index + ", " + data.Length); // Check for the first bounding quote Debug.Assert(data[index] == MailBnfHelper.Quote, "Initial char at index " + index + " was not a quote."); // Skip the bounding quote index--; do { // Check for valid whitespace index = WhitespaceReader.ReadFwsReverse(data, index); if (index < 0) { break; } // Check for escaped characters int quotedCharCount = QuotedPairReader.CountQuotedChars(data, index, permitUnicode); if (quotedCharCount > 0) { // Skip quoted pairs index = index - quotedCharCount; } // Check for the terminating quote else if (data[index] == MailBnfHelper.Quote) { // Skip the final bounding quote return(index - 1); } // Check invalid characters else if (!IsValidQtext(permitUnicode, data[index])) { throw new FormatException(string.Format(Strings.MailHeaderFieldInvalidCharacter, data[index])); } // Valid char else { index--; } }while (index >= 0); // We started with a quote, but did not end with one throw new FormatException(string.Format(Strings.MailHeaderFieldInvalidCharacter, MailBnfHelper.Quote)); }
// // Reads a domain literal in reverse // // Preconditions: // - Index must be within the bounds of the data string. // - The char at the given index is the initial bracket. (data[index] == EndSquareBracket) // // Return value: // - The next index past the terminating bracket (data[index + 1] == StartSquareBracket). // e.g. In (user@[domain]), starting at index=12 (]) returns index=4 (@). // // A FormatException will be thrown if: // - A non-escaped character is encountered that is not valid in a domain literal, including Unicode. // - The final bracket is not found. // internal static int ReadReverse(string data, int index) { Debug.Assert(0 <= index && index < data.Length, "index was outside the bounds of the string: " + index); Debug.Assert(data[index] == MailBnfHelper.EndSquareBracket, "data did not end with a square bracket"); // Skip the end bracket index--; do { // Check for valid whitespace index = WhitespaceReader.ReadFwsReverse(data, index); if (index < 0) { break; } // Check for escaped characters int quotedCharCount = QuotedPairReader.CountQuotedChars(data, index, false); if (quotedCharCount > 0) { // Skip quoted pairs index = index - quotedCharCount; } // Check for the terminating bracket else if (data[index] == MailBnfHelper.StartSquareBracket) { // We're done parsing return(index - 1); } // Check for invalid characters else if (data[index] > MailBnfHelper.Ascii7bitMaxValue || !MailBnfHelper.Dtext[data[index]]) { throw new FormatException(string.Format(Strings.MailHeaderFieldInvalidCharacter, data[index])); } // Valid char else { index--; } }while (index >= 0); // We didn't find a matching '[', throw. throw new FormatException(string.Format(Strings.MailHeaderFieldInvalidCharacter, MailBnfHelper.EndSquareBracket)); }
// // This method attempts reading quoted-string formatted data when the bounding quotes were omitted. // This is common for e-mail display-names. // // Precondition: The index must be within the bounds of the data string. // // Return value: // - The index of the special delimiter provided. // e.g. In ([email protected], billy box [email protected]), starting at index=19 (x) returns index=9 (,). // - -1 if the terminating character was not found. // e.g. In (my name username@domain), starting at index=5 (e) returns index=-1. // // A FormatException will be thrown if: // - A non-escaped character is encountered that is not valid in a quoted string. This includes double quotes. // - A Unicode character is encountered and Unicode has not been allowed. // internal static int ReadReverseUnQuoted(string data, int index, bool permitUnicode, bool expectCommaDelimiter) { Debug.Assert(0 <= index && index < data.Length, "Index out of range: " + index + ", " + data.Length); do { // Check for valid whitespace index = WhitespaceReader.ReadFwsReverse(data, index); if (index < 0) { break; } // Check for escaped characters int quotedCharCount = QuotedPairReader.CountQuotedChars(data, index, permitUnicode); if (quotedCharCount > 0) { index = index - quotedCharCount; } // Check for the terminating char else if (expectCommaDelimiter && data[index] == MailBnfHelper.Comma) { break; } // Check invalid characters else if (!IsValidQtext(permitUnicode, data[index])) { throw new FormatException(string.Format(Strings.MailHeaderFieldInvalidCharacter, data[index])); } // Valid char else { index--; } }while (index >= 0); return(index); }