Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / Net / System / Net / Mail / QuotedStringFormatReader.cs / 1305376 / QuotedStringFormatReader.cs
//------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All rights reserved. // //----------------------------------------------------------------------------- namespace System.Net.Mail { using System; using System.Diagnostics; using System.Net.Mime; // RFC 2822 Section 3.2.5 - Quoted strings // When a string of characters does not conform to an atom string (Section 3.2.4), it must be enclosed in double // quotes. This allows for white spaces, quoted/escaped characters, etc. ("Say hello. \"hello!\" ") // // For robustness, we allow the bounding double quotes to be omitted when we have another clear delineator such as // a comma: (abc@x.com, billy bob bob@bob.com), where the display name 'billy bob' should have been quoted. // // Quoted strings are allowed as MailAddress components local-part and display-name. // e.g. "display name" <"user name"@domain> internal static class QuotedStringFormatReader { // // 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(SR.GetString(SR.MailHeaderFieldInvalidCharacter, data[index])); } // Valid char else { index--; } } while (index >= 0); // We started with a quote, but did not end with one throw new FormatException(SR.GetString(SR.MailHeaderFieldInvalidCharacter, MailBnfHelper.Quote)); } // // 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 (abc@x.com, billy box bob@bob.com), 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(SR.GetString(SR.MailHeaderFieldInvalidCharacter, data[index])); } // Valid char else { index--; } } while (index >= 0); return index; } // Checks for Unicode characters and characters not allowed in quoted-strings. A quoted string may contain // non-whitespace control characters as well as all remaining ASCII chars except backslash and double quote. private static bool IsValidQtext(bool allowUnicode, char ch) { if (ch > MailBnfHelper.Ascii7bitMaxValue) { return allowUnicode; } else { return MailBnfHelper.Qtext[ch]; } } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. //------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All rights reserved. // //----------------------------------------------------------------------------- namespace System.Net.Mail { using System; using System.Diagnostics; using System.Net.Mime; // RFC 2822 Section 3.2.5 - Quoted strings // When a string of characters does not conform to an atom string (Section 3.2.4), it must be enclosed in double // quotes. This allows for white spaces, quoted/escaped characters, etc. ("Say hello. \"hello!\" ") // // For robustness, we allow the bounding double quotes to be omitted when we have another clear delineator such as // a comma: (abc@x.com, billy bob bob@bob.com), where the display name 'billy bob' should have been quoted. // // Quoted strings are allowed as MailAddress components local-part and display-name. // e.g. "display name" <"user name"@domain> internal static class QuotedStringFormatReader { // // 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(SR.GetString(SR.MailHeaderFieldInvalidCharacter, data[index])); } // Valid char else { index--; } } while (index >= 0); // We started with a quote, but did not end with one throw new FormatException(SR.GetString(SR.MailHeaderFieldInvalidCharacter, MailBnfHelper.Quote)); } // // 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 (abc@x.com, billy box bob@bob.com), 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(SR.GetString(SR.MailHeaderFieldInvalidCharacter, data[index])); } // Valid char else { index--; } } while (index >= 0); return index; } // Checks for Unicode characters and characters not allowed in quoted-strings. A quoted string may contain // non-whitespace control characters as well as all remaining ASCII chars except backslash and double quote. private static bool IsValidQtext(bool allowUnicode, char ch) { if (ch > MailBnfHelper.Ascii7bitMaxValue) { return allowUnicode; } else { return MailBnfHelper.Qtext[ch]; } } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- WindowsEditBoxRange.cs
- QueryRewriter.cs
- CodePageUtils.cs
- FontNamesConverter.cs
- SqlUnionizer.cs
- TextProperties.cs
- _NegoStream.cs
- DoWorkEventArgs.cs
- XmlWrappingReader.cs
- UIInitializationException.cs
- ServicesExceptionNotHandledEventArgs.cs
- LabelLiteral.cs
- InternalEnumValidator.cs
- XamlStream.cs
- GridViewHeaderRowPresenter.cs
- TextSegment.cs
- Encoding.cs
- TemplateGroupCollection.cs
- ColumnHeader.cs
- CompleteWizardStep.cs
- DataControlFieldsEditor.cs
- HttpRequestCacheValidator.cs
- DataContractSerializerMessageContractImporter.cs
- Tuple.cs
- PageTheme.cs
- StackSpiller.cs
- CommandTreeTypeHelper.cs
- EntryPointNotFoundException.cs
- XmlElementList.cs
- PngBitmapDecoder.cs
- SHA384Managed.cs
- AttachedPropertyBrowsableWhenAttributePresentAttribute.cs
- OneToOneMappingSerializer.cs
- ValueOfAction.cs
- AnnouncementDispatcherAsyncResult.cs
- RelOps.cs
- SqlDataSourceFilteringEventArgs.cs
- DataSourceControl.cs
- WebSysDisplayNameAttribute.cs
- LocalFileSettingsProvider.cs
- HMACRIPEMD160.cs
- InsufficientMemoryException.cs
- TypeConverter.cs
- SchemaNotation.cs
- RequestNavigateEventArgs.cs
- ListViewGroupConverter.cs
- ReceiveContext.cs
- CheckBoxRenderer.cs
- DataContractSerializerSection.cs
- RectIndependentAnimationStorage.cs
- ConfigurationValidatorBase.cs
- Msec.cs
- BrowserCapabilitiesCodeGenerator.cs
- InstalledFontCollection.cs
- IndexedGlyphRun.cs
- CssStyleCollection.cs
- StringDictionary.cs
- NullRuntimeConfig.cs
- EditingCoordinator.cs
- XmlCharCheckingWriter.cs
- DataSetFieldSchema.cs
- ToolStripHighContrastRenderer.cs
- GenericPrincipal.cs
- PointIndependentAnimationStorage.cs
- DetailsViewCommandEventArgs.cs
- CatalogPartCollection.cs
- DNS.cs
- ReaderWriterLockSlim.cs
- CodePageUtils.cs
- TextModifier.cs
- PeerPresenceInfo.cs
- DBCSCodePageEncoding.cs
- PolyLineSegment.cs
- ServiceDocument.cs
- DbModificationCommandTree.cs
- TreeNodeConverter.cs
- WorkflowCommandExtensionItem.cs
- DBAsyncResult.cs
- AttributeCollection.cs
- PenThreadWorker.cs
- ProviderUtil.cs
- ClassicBorderDecorator.cs
- HttpsChannelFactory.cs
- SqlDelegatedTransaction.cs
- XmlFormatWriterGenerator.cs
- __FastResourceComparer.cs
- PermissionAttributes.cs
- ArrayList.cs
- ProxySimple.cs
- FileCodeGroup.cs
- New.cs
- XmlObjectSerializerReadContextComplex.cs
- TextDocumentView.cs
- XmlAnyElementAttributes.cs
- PropertyCondition.cs
- SqlParameterizer.cs
- ControlParameter.cs
- XmlCharCheckingWriter.cs
- WebPartManagerInternals.cs
- DesignerMetadata.cs