Code:
/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / Net / System / Net / Mail / MailBnfHelper.cs / 1305376 / MailBnfHelper.cs
//------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All rights reserved. // //----------------------------------------------------------------------------- namespace System.Net.Mime { using System; using System.Text; using System.Net.Mail; using System.Globalization; using System.Collections.Generic; internal static class MailBnfHelper { // characters allowed in atoms internal static bool[] Atext = new bool[128]; // characters allowed in quoted strings (not including unicode) internal static bool[] Qtext = new bool[128]; // characters allowed in domain literals internal static bool[] Dtext = new bool[128]; // characters allowed in header names internal static bool[] Ftext = new bool[128]; // characters allowed in tokens internal static bool[] Ttext = new bool[128]; // characters allowed inside of comments internal static bool[] Ctext = new bool[128]; internal static readonly int Ascii7bitMaxValue = 127; internal static readonly char Quote = '\"'; internal static readonly char Space = ' '; internal static readonly char Tab = '\t'; internal static readonly char CR = '\r'; internal static readonly char LF = '\n'; internal static readonly char StartComment = '('; internal static readonly char EndComment = ')'; internal static readonly char Backslash = '\\'; internal static readonly char At = '@'; internal static readonly char EndAngleBracket = '>'; internal static readonly char StartAngleBracket = '<'; internal static readonly char StartSquareBracket = '['; internal static readonly char EndSquareBracket = ']'; internal static readonly char Comma = ','; internal static readonly char Dot = '.'; internal static readonly IListWhitespace; static MailBnfHelper() { // NOTE: all of the specs outlined here are defined in "MailBNF.cs" as to what they are // and what they mean. They are defined in RFC 2822 in more detail. By default, every // value in the array is false and only those values which are allowed in that particular set // are then set to true. The numbers annotating each definition below are the range of ASCII // values which are allowed in that definition. // all allowed whitespace characters Whitespace = new List (); Whitespace.Add(Tab); Whitespace.Add(Space); Whitespace.Add(CR); Whitespace.Add(LF); // atext = ALPHA / DIGIT / "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "/" / "=" / "?" / "^" / "_" / "`" / "{" / "|" / "}" / "~" for (int i = '0'; i <= '9'; i++) { Atext[i] = true; } for (int i = 'A'; i <= 'Z'; i++) { Atext[i] = true; } for (int i = 'a'; i <= 'z'; i++) { Atext[i] = true; } Atext['!'] = true; Atext['#'] = true; Atext['$'] = true; Atext['%'] = true; Atext['&'] = true; Atext['\''] = true; Atext['*'] = true; Atext['+'] = true; Atext['-'] = true; Atext['/'] = true; Atext['='] = true; Atext['?'] = true; Atext['^'] = true; Atext['_'] = true; Atext['`'] = true; Atext['{'] = true; Atext['|'] = true; Atext['}'] = true; Atext['~'] = true; // fqtext = %d1-9 / %d11 / %d12 / %d14-33 / %d35-91 / %d93-127 for (int i = 1; i <= 9; i++) { Qtext[i] = true; } Qtext[11] = true; Qtext[12] = true; for (int i = 14; i <= 33; i++) { Qtext[i] = true; } for (int i = 35; i <= 91; i++) { Qtext[i] = true; } for (int i = 93; i <= 127; i++) { Qtext[i] = true; } // fdtext = %d1-8 / %d11 / %d12 / %d14-31 / %d33-90 / %d94-127 for (int i = 1; i <= 8; i++) { Dtext[i] = true; } Dtext[11] = true; Dtext[12] = true; for (int i = 14; i <= 31; i++) { Dtext[i] = true; } for (int i = 33; i <= 90; i++) { Dtext[i] = true; } for (int i = 94; i <= 127; i++) { Dtext[i] = true; } // ftext = %d33-57 / %d59-126 for (int i = 33; i <= 57; i++) { Ftext[i] = true; } for (int i = 59; i <= 126; i++) { Ftext[i] = true; } // ttext = %d33-126 except '()<>@,;:\"/[]?=' for (int i = 33; i <= 126; i++) { Ttext[i] = true; } Ttext['('] = false; Ttext[')'] = false; Ttext['<'] = false; Ttext['>'] = false; Ttext['@'] = false; Ttext[','] = false; Ttext[';'] = false; Ttext[':'] = false; Ttext['\\'] = false; Ttext['"'] = false; Ttext['/'] = false; Ttext['['] = false; Ttext[']'] = false; Ttext['?'] = false; Ttext['='] = false; // ctext- %d1-8 / %d11 / %d12 / %d14-31 / %33-39 / %42-91 / %93-127 for (int i = 1; i <= 8; i++) { Ctext[i] = true; } Ctext[11] = true; Ctext[12] = true; for (int i = 14; i <= 31; i++) { Ctext[i] = true; } for (int i = 33; i <= 39; i++) { Ctext[i] = true; } for (int i = 42; i <= 91; i++) { Ctext[i] = true; } for (int i = 93; i <= 127; i++) { Ctext[i] = true; } } internal static bool SkipCFWS(string data, ref int offset) { int comments = 0; for (;offset < data.Length ;offset++) { if (data[offset] > 127) throw new FormatException(SR.GetString(SR.MailHeaderFieldInvalidCharacter, data[offset])); else if (data[offset] == '\\' && comments > 0) offset += 2; else if (data[offset] == '(') comments++; else if (data[offset] == ')') comments--; else if (data[offset] != ' ' && data[offset] != '\t' && comments == 0) return true; if (comments < 0) { throw new FormatException(SR.GetString(SR.MailHeaderFieldInvalidCharacter, data[offset])); } } //returns false if end of string return false; } internal static void ValidateHeaderName(string data){ int offset = 0; for (; offset < data.Length; offset++) { if (data[offset] > Ftext.Length || !Ftext[data[offset]]) throw new FormatException(SR.GetString(SR.InvalidHeaderName)); } if (offset == 0) throw new FormatException(SR.GetString(SR.InvalidHeaderName)); } internal static string ReadQuotedString(string data, ref int offset, StringBuilder builder){ return ReadQuotedString(data, ref offset, builder, false, false); } internal static string ReadQuotedString(string data, ref int offset, StringBuilder builder, bool doesntRequireQuotes, bool permitUnicodeInDisplayName) { // assume first char is the opening quote if(!doesntRequireQuotes){ ++offset; } int start = offset; StringBuilder localBuilder = (builder != null ? builder : new StringBuilder()); for (; offset < data.Length; offset++) { if (data[offset] == '\\') { localBuilder.Append(data, start, offset - start); start = ++offset; } else if (data[offset] == '"') { localBuilder.Append(data, start, offset - start); offset++; return (builder != null ? null : localBuilder.ToString()); } else if (data[offset] == '=' && data.Length > offset + 3 && data[offset + 1] == '\r' && data[offset + 2] == '\n' && (data[offset + 3] == ' ' || data[offset + 3] == '\t')) { //it's a soft crlf so it's ok offset += 3; } else if (permitUnicodeInDisplayName) { //if data contains unicode and unicode is permitted, then //it is valid in a quoted string in a header. if (data[offset] <= Ascii7bitMaxValue && !Qtext[data[offset]]) throw new FormatException(SR.GetString(SR.MailHeaderFieldInvalidCharacter, data[offset])); } //not permitting unicode, in which case unicode is a formatting error else if (data[offset] > Ascii7bitMaxValue || !Qtext[data[offset]]) { throw new FormatException(SR.GetString(SR.MailHeaderFieldInvalidCharacter, data[offset])); } } if(doesntRequireQuotes){ localBuilder.Append(data, start, offset - start); return (builder != null ? null : localBuilder.ToString()); } throw new FormatException(SR.GetString(SR.MailHeaderFieldMalformedHeader)); } internal static string ReadParameterAttribute(string data, ref int offset, StringBuilder builder) { if (!SkipCFWS(data, ref offset)) return null; // return ReadToken(data, ref offset, null); } internal static string ReadToken(string data, ref int offset, StringBuilder builder) { int start = offset; for (; offset < data.Length; offset++) { if (data[offset] > Ascii7bitMaxValue) { throw new FormatException(SR.GetString(SR.MailHeaderFieldInvalidCharacter, data[offset])); } else if (!Ttext[data[offset]]) { break; } } if (start == offset) { throw new FormatException(SR.GetString(SR.MailHeaderFieldInvalidCharacter, data[offset])); } return data.Substring(start, offset - start); } static string[] s_months = new string[] { null, "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; internal static string GetDateTimeString(DateTime value, StringBuilder builder) { StringBuilder localBuilder = (builder != null ? builder : new StringBuilder()); localBuilder.Append(value.Day); localBuilder.Append(' '); localBuilder.Append(s_months[value.Month]); localBuilder.Append(' '); localBuilder.Append(value.Year); localBuilder.Append(' '); if (value.Hour <= 9) { localBuilder.Append('0'); } localBuilder.Append(value.Hour); localBuilder.Append(':'); if (value.Minute <= 9) { localBuilder.Append('0'); } localBuilder.Append(value.Minute); localBuilder.Append(':'); if (value.Second <= 9) { localBuilder.Append('0'); } localBuilder.Append(value.Second); string offset = TimeZone.CurrentTimeZone.GetUtcOffset(value).ToString(); if (offset[0] != '-') { localBuilder.Append(" +"); } else { localBuilder.Append(" "); } string[] offsetFields = offset.Split(':'); localBuilder.Append(offsetFields[0]); localBuilder.Append(offsetFields[1]); return (builder != null ? null : localBuilder.ToString()); } internal static string GetTokenOrQuotedString(string data, StringBuilder builder) { int offset = 0, start = 0; for (; offset < data.Length; offset++) { if (data[offset] > Ascii7bitMaxValue) throw new FormatException(SR.GetString(SR.MailHeaderFieldInvalidCharacter, data[offset])); if (!Ttext[data[offset]] || data[offset] == ' ') { StringBuilder localBuilder = (builder != null ? builder : new StringBuilder()); builder.Append('"'); for (; offset < data.Length; offset++) { if (data[offset] > Ascii7bitMaxValue) { throw new FormatException(SR.GetString(SR.MailHeaderFieldInvalidCharacter, data[offset])); } else if (!Qtext[data[offset]]) { builder.Append(data, start, offset - start); builder.Append('\\'); start = offset; } } builder.Append(data, start, offset - start); builder.Append('"'); return (builder != null ? null : localBuilder.ToString()); } } //always a quoted string if it was empty. if(data.Length == 0){ if (builder != null) { builder.Append("\"\""); } else{ return "\"\""; } } if (builder != null) { builder.Append(data); return null; } return data; } internal static bool HasCROrLF(string data){ for (int i=0;i
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- DataKey.cs
- _AcceptOverlappedAsyncResult.cs
- ObjectStateFormatter.cs
- StoreItemCollection.Loader.cs
- PermissionSetTriple.cs
- BitSet.cs
- PropertyGridEditorPart.cs
- InstanceLockException.cs
- NullableConverter.cs
- ImageMapEventArgs.cs
- COM2IVsPerPropertyBrowsingHandler.cs
- Margins.cs
- RegisteredArrayDeclaration.cs
- Helper.cs
- PointCollectionValueSerializer.cs
- FrameworkRichTextComposition.cs
- StyleSelector.cs
- CachedCompositeFamily.cs
- TransformedBitmap.cs
- XmlSchemaGroupRef.cs
- DataGridCellEditEndingEventArgs.cs
- IntPtr.cs
- DecimalAnimationBase.cs
- mediapermission.cs
- HtmlTextBoxAdapter.cs
- GeneratedCodeAttribute.cs
- MemberMaps.cs
- SafeSecurityHelper.cs
- DbParameterHelper.cs
- XmlNamespaceMapping.cs
- ReadOnlyHierarchicalDataSource.cs
- LeaseManager.cs
- ExpressionBindingCollection.cs
- DecoderReplacementFallback.cs
- FilterElement.cs
- XamlParser.cs
- CodeCastExpression.cs
- CodeSubDirectory.cs
- DataSourceSelectArguments.cs
- TcpAppDomainProtocolHandler.cs
- ClientSettingsProvider.cs
- CodeLinePragma.cs
- ListSortDescriptionCollection.cs
- GenericIdentity.cs
- ClassHandlersStore.cs
- CallContext.cs
- HttpCapabilitiesEvaluator.cs
- StateRuntime.cs
- ScrollViewer.cs
- ShutDownListener.cs
- WebServicesSection.cs
- Expression.DebuggerProxy.cs
- InputReferenceExpression.cs
- MailMessage.cs
- Configuration.cs
- MaskedTextBox.cs
- WorkflowApplicationUnloadedException.cs
- FontDialog.cs
- ProfileModule.cs
- ListViewItem.cs
- IncrementalReadDecoders.cs
- FocusWithinProperty.cs
- ComponentConverter.cs
- Preprocessor.cs
- ArglessEventHandlerProxy.cs
- ToolStripItemRenderEventArgs.cs
- UserControlDocumentDesigner.cs
- Baml2006ReaderContext.cs
- Link.cs
- FontUnitConverter.cs
- HttpFormatExtensions.cs
- KnownBoxes.cs
- XmlSchemaSimpleContentExtension.cs
- WebSysDefaultValueAttribute.cs
- MatrixConverter.cs
- Soap12FormatExtensions.cs
- Vector.cs
- ViewCellSlot.cs
- CalendarButtonAutomationPeer.cs
- ControlBuilder.cs
- DetailsViewUpdateEventArgs.cs
- MsmqTransportReceiveParameters.cs
- Visitors.cs
- InkSerializer.cs
- Margins.cs
- RepeatButtonAutomationPeer.cs
- OracleString.cs
- VScrollBar.cs
- StringInfo.cs
- VectorKeyFrameCollection.cs
- Errors.cs
- CommandDevice.cs
- ObjectDataSource.cs
- PathTooLongException.cs
- ObfuscationAttribute.cs
- RuntimeHelpers.cs
- NavigationService.cs
- CodeDomExtensionMethods.cs
- RemotingException.cs
- DrawListViewItemEventArgs.cs