Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / Net / System / Net / Mail / QuotedPairReader.cs / 1305376 / QuotedPairReader.cs
//------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All rights reserved. // //----------------------------------------------------------------------------- namespace System.Net.Mail { using System; using System.Text; using System.Diagnostics; using System.Net.Mime; // RFC 2822 Section 3.2.2 - Quoted Characters // As in C# strings, characters that would otherwise have special meaning should be ignored when quoted/escaped // by a backslash "\". Quoted characters (quoted-pair) are only allowed in the following contexts: Comments, // quoted-string, domain-literal, and message-id. // // Example: A quoted-string ("hello there") cannot contain a double quote, as it must be surrounded by them. // However, the double quote can be included as a quoted-pair: ("hello\"there") means (hello"there). // // Because backslashes themselves can be quoted-pairs (\\), this class's primary function is to verify what // character is being quoted. In "hi\\\\a", 'a' is not quoted because all of the backslashes are paired together. // In "hi\\\a", 'a' is quoted. internal static class QuotedPairReader { // // Counts the number of consecutive quoted chars, including multiple preceding quoted backslashes // // Preconditions: Index should be within the bounds of the data string. // // Return value: // - 0 if the char at data[index] was not quoted // e.g. (\\a) given index=2 returns 0 because 'a' is not quoted // - The number of consecutive quoted chars, including multiple preceding quoted backslashes // e.g. (a\\\b) given index=4 returns 4, as 'b' is quoted, and so are the previous backslashes // // Throws a FormatException if the an escaped Unicode character is found but was not permitted. internal static int CountQuotedChars(string data, int index, bool permitUnicodeEscaping) { Debug.Assert(0 <= index && index < data.Length, "Index out of range: " + index + ", " + data.Length); if (index <= 0 || data[index - 1] != MailBnfHelper.Backslash) { return 0; } // Count the preceding backslashes int backslashCount = CountBackslashes(data, index - 1); // For an even number of backslashes, the original character was NOT escaped/quoted if (backslashCount % 2 == 0) { return 0; // No quoted pair to skip } else { if (!permitUnicodeEscaping && data[index] > MailBnfHelper.Ascii7bitMaxValue) { // Cannot accept quoted unicode throw new FormatException(SR.GetString(SR.MailHeaderFieldInvalidCharacter, data[index])); } // Skip the quoted char, and the odd number of backslashes preceding it return backslashCount + 1; } } // // Counts the number of consecutive backslashes // // Preconditions: // - Index should be within the bounds of the data string. // - The initial data[index] must be a backslash. // // Return value: The number of consecutive backslashes, including the initial one at data[index]. private static int CountBackslashes(string data, int index) { Debug.Assert(index >= 0 && data[index] == MailBnfHelper.Backslash, "index was not a backslash: " + index); // Find all the backslashes. It's possible that there are multiple escaped/quoted backslashes. int backslashCount = 0; do { backslashCount++; index--; } while (index >= 0 && data[index] == MailBnfHelper.Backslash); // At this point data[index] should not be a backslash Debug.Assert(index < 0 || data[index] != MailBnfHelper.Backslash, "index was a backslash: " + index); return backslashCount; } } } // 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.Text; using System.Diagnostics; using System.Net.Mime; // RFC 2822 Section 3.2.2 - Quoted Characters // As in C# strings, characters that would otherwise have special meaning should be ignored when quoted/escaped // by a backslash "\". Quoted characters (quoted-pair) are only allowed in the following contexts: Comments, // quoted-string, domain-literal, and message-id. // // Example: A quoted-string ("hello there") cannot contain a double quote, as it must be surrounded by them. // However, the double quote can be included as a quoted-pair: ("hello\"there") means (hello"there). // // Because backslashes themselves can be quoted-pairs (\\), this class's primary function is to verify what // character is being quoted. In "hi\\\\a", 'a' is not quoted because all of the backslashes are paired together. // In "hi\\\a", 'a' is quoted. internal static class QuotedPairReader { // // Counts the number of consecutive quoted chars, including multiple preceding quoted backslashes // // Preconditions: Index should be within the bounds of the data string. // // Return value: // - 0 if the char at data[index] was not quoted // e.g. (\\a) given index=2 returns 0 because 'a' is not quoted // - The number of consecutive quoted chars, including multiple preceding quoted backslashes // e.g. (a\\\b) given index=4 returns 4, as 'b' is quoted, and so are the previous backslashes // // Throws a FormatException if the an escaped Unicode character is found but was not permitted. internal static int CountQuotedChars(string data, int index, bool permitUnicodeEscaping) { Debug.Assert(0 <= index && index < data.Length, "Index out of range: " + index + ", " + data.Length); if (index <= 0 || data[index - 1] != MailBnfHelper.Backslash) { return 0; } // Count the preceding backslashes int backslashCount = CountBackslashes(data, index - 1); // For an even number of backslashes, the original character was NOT escaped/quoted if (backslashCount % 2 == 0) { return 0; // No quoted pair to skip } else { if (!permitUnicodeEscaping && data[index] > MailBnfHelper.Ascii7bitMaxValue) { // Cannot accept quoted unicode throw new FormatException(SR.GetString(SR.MailHeaderFieldInvalidCharacter, data[index])); } // Skip the quoted char, and the odd number of backslashes preceding it return backslashCount + 1; } } // // Counts the number of consecutive backslashes // // Preconditions: // - Index should be within the bounds of the data string. // - The initial data[index] must be a backslash. // // Return value: The number of consecutive backslashes, including the initial one at data[index]. private static int CountBackslashes(string data, int index) { Debug.Assert(index >= 0 && data[index] == MailBnfHelper.Backslash, "index was not a backslash: " + index); // Find all the backslashes. It's possible that there are multiple escaped/quoted backslashes. int backslashCount = 0; do { backslashCount++; index--; } while (index >= 0 && data[index] == MailBnfHelper.Backslash); // At this point data[index] should not be a backslash Debug.Assert(index < 0 || data[index] != MailBnfHelper.Backslash, "index was a backslash: " + index); return backslashCount; } } } // 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
- UInt64Storage.cs
- SimpleWebHandlerParser.cs
- DynamicDocumentPaginator.cs
- XPathMultyIterator.cs
- SchemaElementDecl.cs
- PersonalizationProvider.cs
- GotoExpression.cs
- MediaTimeline.cs
- UnicodeEncoding.cs
- LoginCancelEventArgs.cs
- CharUnicodeInfo.cs
- BitmapPalette.cs
- KeyGestureConverter.cs
- PrivateFontCollection.cs
- DropDownButton.cs
- SingleObjectCollection.cs
- WebServiceEnumData.cs
- SafeCryptoHandles.cs
- TextControl.cs
- EntryIndex.cs
- LinqDataSourceDeleteEventArgs.cs
- XmlSchemaComplexContentExtension.cs
- DataListDesigner.cs
- XamlWrapperReaders.cs
- HandlerMappingMemo.cs
- XmlSerializableWriter.cs
- ServiceEndpointElement.cs
- SqlServer2KCompatibilityCheck.cs
- TraceSection.cs
- DefaultAssemblyResolver.cs
- UshortList2.cs
- WebPartCatalogCloseVerb.cs
- MsmqChannelFactoryBase.cs
- PersonalizableTypeEntry.cs
- ArgumentException.cs
- NameValueSectionHandler.cs
- DomNameTable.cs
- MonthCalendar.cs
- AutomationPropertyInfo.cs
- ConfigUtil.cs
- AuthenticationSection.cs
- XmlCharCheckingReader.cs
- httpstaticobjectscollection.cs
- ConnectionManagementElementCollection.cs
- Italic.cs
- HtmlProps.cs
- SchemaImporterExtension.cs
- CaseInsensitiveComparer.cs
- DataKeyArray.cs
- SqlServer2KCompatibilityCheck.cs
- ImageAttributes.cs
- WebPartDescription.cs
- PropertyPath.cs
- SetStateEventArgs.cs
- CultureMapper.cs
- ConfigurationFileMap.cs
- ToolboxItemSnapLineBehavior.cs
- ConnectivityStatus.cs
- NonParentingControl.cs
- StateChangeEvent.cs
- DataKey.cs
- HtmlMeta.cs
- DbProviderFactoriesConfigurationHandler.cs
- PtsCache.cs
- ConfigurationProperty.cs
- LocalIdKeyIdentifierClause.cs
- Exceptions.cs
- XamlSerializerUtil.cs
- StandardBindingImporter.cs
- UnaryNode.cs
- autovalidator.cs
- EditorZoneDesigner.cs
- LayoutEvent.cs
- AttachedPropertyBrowsableWhenAttributePresentAttribute.cs
- IssuedTokenParametersEndpointAddressElement.cs
- InfiniteIntConverter.cs
- CommittableTransaction.cs
- DataObject.cs
- DelegatedStream.cs
- FileDialogCustomPlacesCollection.cs
- SpinLock.cs
- TransformedBitmap.cs
- XmlnsDictionary.cs
- AdornedElementPlaceholder.cs
- PageThemeBuildProvider.cs
- WindowsListViewItem.cs
- DSGeneratorProblem.cs
- DesignTimeSiteMapProvider.cs
- BooleanAnimationUsingKeyFrames.cs
- DataServiceProcessingPipeline.cs
- InputBuffer.cs
- DrawingVisualDrawingContext.cs
- ToolStripSeparator.cs
- DelegatingConfigHost.cs
- UIAgentAsyncEndRequest.cs
- DataColumn.cs
- TcpActivation.cs
- UidManager.cs
- PrivilegedConfigurationManager.cs
- TdsParserStaticMethods.cs