}); // the byte 0-255 to be decoded is the index and the value at that position in the array is the byte that is encoded or -1 if this byte encodes nothing public static cByteList Encode(string pString) { if (pString.Length == 0) { return(new cByteList()); } var lBytes = new cByteList(); var lBuffer = new cToBase64Buffer(lBytes); bool lInBase64 = false; foreach (char lChar in pString) { if (lChar < ' ' || lChar > '~') { if (!lInBase64) { lBytes.Add(cASCII.AMPERSAND); lInBase64 = true; } byte[] lCharBytes = BitConverter.GetBytes(lChar); if (BitConverter.IsLittleEndian) { lBuffer.Add(lCharBytes[1]); lBuffer.Add(lCharBytes[0]); } else { lBuffer.Add(lCharBytes[0]); lBuffer.Add(lCharBytes[1]); } } else { if (lInBase64) { lBuffer.Flush(); lBytes.Add(cASCII.HYPEN); lInBase64 = false; } lBytes.Add((byte)lChar); if (lChar == '&') { lBytes.Add(cASCII.HYPEN); } } } if (lInBase64) { lBuffer.Flush(); lBytes.Add(cASCII.HYPEN); lInBase64 = false; } return(lBytes); }
private static bool ZTryAsQuotedASCII(string pString, bool pSecret, out cTextCommandPart rResult) { if (pString == null) { rResult = null; return(false); } var lBytes = new cByteList(pString.Length + 2); lBytes.Add(cASCII.DQUOTE); foreach (char lChar in pString) { if (lChar == cChar.NUL || lChar == cChar.CR || lChar == cChar.LF || lChar > cChar.DEL) { rResult = null; return(false); } if (lChar == '"' || lChar == '\\') { lBytes.Add(cASCII.BACKSL); } lBytes.Add((byte)lChar); } lBytes.Add(cASCII.DQUOTE); rResult = new cTextCommandPart(lBytes, pSecret); return(true); }
internal cCulturedString(IList <byte> pBytes) { if (pBytes == null) { throw new ArgumentNullException(nameof(pBytes)); } if (pBytes.Count == 0) { Parts = null; return; } cBytesCursor lCursor = new cBytesCursor(pBytes); List <cCulturedStringPart> lParts = new List <cCulturedStringPart>(); cByteList lBytes = new cByteList(); bool lPendingSpace = false; while (!lCursor.Position.AtEnd) { var lBookmark = lCursor.Position; if (lCursor.ProcessEncodedWord(out var lString, out var lLanguageTag) && (lCursor.Position.AtEnd || lCursor.SkipByte(cASCII.SPACE))) { if (lBytes.Count > 0) { lParts.Add(new cCulturedStringPart(cTools.UTF8BytesToString(lBytes), null)); lBytes = new cByteList(); } lParts.Add(new cCulturedStringPart(lString, lLanguageTag)); lPendingSpace = true; }
public bool GetNZNumber(out cByteList rBytes, out uint rNumber) { var lBookmark = Position; rNumber = 0; if (!GetToken(cCharset.Digit, null, null, out rBytes)) { return(false); } if (rBytes[0] == cASCII.ZERO) { Position = lBookmark; return(false); } checked { try { foreach (byte lByte in rBytes) { rNumber = rNumber * 10 + lByte - cASCII.ZERO; } } catch { Position = lBookmark; return(false); } } return(true); }
private static bool ZTryAsQuotedASCII(IList <byte> pBytes, bool pSecret, bool pEncoded, out cTextCommandPart rResult) { if (pBytes == null) { rResult = null; return(false); } var lBytes = new cByteList(pBytes.Count + 2); lBytes.Add(cASCII.DQUOTE); foreach (byte lByte in pBytes) { if (lByte == cASCII.NUL || lByte == cASCII.CR || lByte == cASCII.LF || lByte > cASCII.DEL) { rResult = null; return(false); } if (lByte == cASCII.DQUOTE || lByte == cASCII.BACKSL) { lBytes.Add(cASCII.BACKSL); } lBytes.Add(lByte); } lBytes.Add(cASCII.DQUOTE); rResult = new cTextCommandPart(lBytes, pSecret, pEncoded); return(true); }
private static bool ZTryAsQuotedUTF8(string pString, bool pSecret, out cTextCommandPart rResult) { if (pString == null) { rResult = null; return(false); } var lEncBytes = Encoding.UTF8.GetBytes(pString); var lBytes = new cByteList(lEncBytes.Length + 2); lBytes.Add(cASCII.DQUOTE); foreach (byte lByte in lEncBytes) { if (lByte == cASCII.NUL || lByte == cASCII.CR || lByte == cASCII.LF) { rResult = null; return(false); } if (lByte == cASCII.DQUOTE || lByte == cASCII.BACKSL) { lBytes.Add(cASCII.BACKSL); } lBytes.Add(lByte); } lBytes.Add(cASCII.DQUOTE); rResult = new cTextCommandPart(lBytes, pSecret); return(true); }
public bool GetRFC822FieldValue(out cByteList rFieldValue) { var lBookmark = Position; var lByteList = new cByteList(); while (true) { if (Position.AtEnd || Position.BytesLine.Literal) { Position = lBookmark; rFieldValue = null; return(false); } if (SkipBytes(kCRLFTAB)) { lByteList.Add(cASCII.TAB); } else if (SkipBytes(kCRLFSPACE)) { lByteList.Add(cASCII.SPACE); } else if (SkipBytes(CRLF)) { rFieldValue = lByteList; return(true); } else { lByteList.Add(Position.BytesLine[Position.Byte]); ZAdvance(ref Position); } } }
public bool GetLanguageTag(out string rLanguageTag) { // rfc 5646, 4646, 3066, 1766 // this is a crude implementation ignoring the finer details cByteList lResult = new cByteList(); cByteList lPart; if (!GetToken(cCharset.Alpha, null, null, out lPart, 1, 8)) { rLanguageTag = null; return(false); } lResult.AddRange(lPart); while (true) { var lBookmark = Position; if (!SkipByte(cASCII.HYPEN)) { break; } if (!GetToken(cCharset.AlphaNumeric, null, null, out lPart, 1, 8)) { Position = lBookmark; break; } lResult.Add(cASCII.HYPEN); lResult.AddRange(lPart); } rLanguageTag = cTools.ASCIIBytesToString(lResult); return(true); }
public bool GetRFC822QuotedString(out string rString) { var lBookmark = Position; // optional leading spaces SkipRFC822CFWS(); // open quote if (!SkipByte(cASCII.DQUOTE)) { Position = lBookmark; rString = null; return(false); } cByteList lResult = new cByteList(); while (true) { ZGetRFC822FWS(lResult); if (Position.AtEnd || Position.BytesLine.Literal) { Position = lBookmark; rString = null; return(false); } byte lByte = Position.BytesLine[Position.Byte]; if (lByte == cASCII.BACKSL) { ZAdvance(ref Position); if (Position.AtEnd || Position.BytesLine.Literal) { Position = lBookmark; rString = null; return(false); } lByte = Position.BytesLine[Position.Byte]; } else if (!cCharset.QText.Contains(lByte)) { break; } lResult.Add(lByte); ZAdvance(ref Position); } // close quote if (!SkipByte(cASCII.DQUOTE)) { Position = lBookmark; rString = null; return(false); } // optional trailing spaces SkipRFC822CFWS(); // done rString = cTools.UTF8BytesToString(lResult); return(true); }
public static bool TryEncodedMailboxPathToString(IList <byte> pEncodedMailboxPath, byte?pDelimiter, bool pUTF8Enabled, out string rString) { if (pUTF8Enabled) { rString = UTF8BytesToString(pEncodedMailboxPath); return(true); } if (pDelimiter == null) { return(cModifiedUTF7.TryDecode(pEncodedMailboxPath, out rString, out _)); } byte lDelimiterByte = pDelimiter.Value; char lDelimiterChar = (char)lDelimiterByte; List <cByteList> lSegments = new List <cByteList>(); cByteList lSegment = new cByteList(); foreach (byte lByte in pEncodedMailboxPath) { if (lByte == lDelimiterByte) { lSegments.Add(lSegment); lSegment = new cByteList(); } else { lSegment.Add(lByte); } } lSegments.Add(lSegment); StringBuilder lResult = new StringBuilder(); bool lFirst = true; foreach (var lSegmentBytes in lSegments) { if (lFirst) { lFirst = false; } else { lResult.Append(lDelimiterChar); } if (!cModifiedUTF7.TryDecode(lSegmentBytes, out var lSegmentString, out _)) { rString = null; return(false); } lResult.Append(lSegmentString); } rString = lResult.ToString(); return(true); }
public static bool TryDecode(IList <byte> pBytes, out cByteList rBytes, out string rError) { if (pBytes.Count == 0) { rBytes = new cByteList(); rError = null; return(true); } if (pBytes.Count % 4 != 0) { rBytes = null; rError = "base64 bytes must be in multiples of 4"; return(false); } int lCount; if (pBytes[pBytes.Count - 2] == cASCII.EQUALS) { if (pBytes[pBytes.Count - 1] != cASCII.EQUALS) { rBytes = null; rError = "invalid termination"; return(false); } lCount = pBytes.Count - 2; } else if (pBytes[pBytes.Count - 1] == cASCII.EQUALS) { lCount = pBytes.Count - 1; } else { lCount = pBytes.Count; } rBytes = new cByteList((lCount + 2) / 4 * 3); var lBuffer = new cFromBase64Buffer(rBytes); for (int i = 0; i < lCount; i++) { if (!lBuffer.TryAdd(pBytes[i], out rError)) { rBytes = null; return(false); } } if (!lBuffer.TryFlush(out rError)) { rBytes = null; return(false); } return(true); }
public void ProcessTextCode(eResponseTextContext pTextContext, cByteList pCode, cByteList pArguments, cTrace.cContext pParentContext) { var lContext = pParentContext.NewMethod(nameof(cActiveCommands), nameof(ProcessTextCode), pTextContext, pCode, pArguments); foreach (var lCommand in this) { lCommand.Hook.ProcessTextCode(pTextContext, pCode, pArguments, lContext); } }
public void AddLiteralHeader(bool pSecret, bool pBinary, int pLength, bool pSynchronising) { cByteList lLengthBytes = cTools.IntToBytesReverse(pLength); lLengthBytes.Reverse(); if (mTracing) { if (pBinary) { mCurrentTraceBuffer.Add(cASCII.TILDA); } mCurrentTraceBuffer.Add(cASCII.LBRACE); if (pSecret) { mCurrentTraceBuffer.Add(cASCII.NUL); } else { mCurrentTraceBuffer.AddRange(lLengthBytes); } if (!pSynchronising) { mCurrentTraceBuffer.Add(cASCII.PLUS); } mCurrentTraceBuffer.Add(cASCII.RBRACE); mCurrentTraceBuffer.Add(cASCII.CR); mCurrentTraceBuffer.Add(cASCII.LF); if (pSecret) { mContainsSecrets = true; } mLastByteWasSecret = false; } if (pBinary) { mSendBuffer.Add(cASCII.TILDA); } mSendBuffer.Add(cASCII.LBRACE); mSendBuffer.AddRange(lLengthBytes); if (!pSynchronising) { mSendBuffer.Add(cASCII.PLUS); } mSendBuffer.Add(cASCII.RBRACE); mSendBuffer.Add(cASCII.CR); mSendBuffer.Add(cASCII.LF); }
public static cTextCommandPart AsDate(DateTime pDate) { var lBytes = new cByteList(11); lBytes.AddRange(ZIntToBytes(pDate.Day, 0)); lBytes.Add(cASCII.HYPEN); lBytes.AddRange(cASCIIMonth.Name[pDate.Month - 1]); lBytes.Add(cASCII.HYPEN); lBytes.AddRange(ZIntToBytes(pDate.Year, 4)); return(new cTextCommandPart(lBytes)); }
public static cTextCommandPart AsDateTime(DateTime pDate) { var lBytes = new cByteList(26); lBytes.Add(cASCII.DQUOTE); lBytes.AddRange(ZIntToBytes(pDate.Day, 2)); lBytes.Add(cASCII.HYPEN); lBytes.AddRange(cASCIIMonth.Name[pDate.Month - 1]); lBytes.Add(cASCII.HYPEN); lBytes.AddRange(ZIntToBytes(pDate.Year, 4)); lBytes.Add(cASCII.SPACE); lBytes.AddRange(ZIntToBytes(pDate.Hour, 2)); lBytes.Add(cASCII.COLON); lBytes.AddRange(ZIntToBytes(pDate.Minute, 2)); lBytes.Add(cASCII.COLON); lBytes.AddRange(ZIntToBytes(pDate.Second, 2)); lBytes.Add(cASCII.SPACE); if (pDate.Kind == DateTimeKind.Utc) { lBytes.AddRange(UTC); } else { var lOffset = TimeZoneInfo.Local.GetUtcOffset(pDate); if (lOffset < TimeSpan.Zero) { lBytes.Add(cASCII.HYPEN); lOffset = -lOffset; } else { lBytes.Add(cASCII.PLUS); } var lOffsetChars = lOffset.ToString("hhmm"); foreach (var lChar in lOffsetChars) { lBytes.Add((byte)lChar); } } return(new cTextCommandPart(lBytes)); }
private static cByteList ZIntToBytes(int pNumber, int pMinLength) { if (pNumber < 0) { throw new ArgumentOutOfRangeException(nameof(pNumber)); } cByteList lBytes = cTools.IntToBytesReverse(pNumber); for (int i = lBytes.Count; i < pMinLength; i++) { lBytes.Add(cASCII.ZERO); } lBytes.Reverse(); return(lBytes); }
public static cByteList ULongToBytesReverse(ulong pNumber) { cByteList lBytes = new cByteList(20); ulong lNumber = pNumber; do { int lDigit = (int)(lNumber % 10); lBytes.Add((byte)(cASCII.ZERO + lDigit)); lNumber = lNumber / 10; } while (lNumber > 0); return(lBytes); }
private cByteList ZGetRestAsBytes() { cByteList lBytes = new cByteList(); while (!Position.AtEnd) { for (int i = Position.Byte; i < Position.BytesLine.Count; i++) { lBytes.Add(Position.BytesLine[i]); } Position.Byte = 0; ZAdvancePart(ref Position); } return(lBytes); }
}); // the byte 0-255 to be decoded is the index and the value at that position in the array is the byte that is encoded or -1 if this byte encodes nothing public static cByteList Encode(IList <byte> pBytes) { if (pBytes.Count == 0) { return(new cByteList()); } var lBytes = new cByteList((pBytes.Count + 2) / 3 * 4); var lBuffer = new cToBase64Buffer(lBytes); foreach (byte lByte in pBytes) { lBuffer.Add(lByte); } lBuffer.Flush(); return(lBytes); }
public bool GetQuoted(out cByteList rBytes) { var lBookmark = Position; if (!SkipByte(cASCII.DQUOTE)) { rBytes = null; return(false); } rBytes = new cByteList(); bool lInQuote = false; while (true) { byte lByte = Position.BytesLine[Position.Byte]; ZAdvance(ref Position); if (lInQuote) { rBytes.Add(lByte); lInQuote = false; } else { if (lByte == cASCII.DQUOTE) { return(true); } if (lByte == cASCII.BACKSL) { lInQuote = true; } else { rBytes.Add(lByte); } } if (Position.AtEnd || Position.BytesLine.Literal) { Position = lBookmark; rBytes = null; return(false); } } }
private bool ZGetRFC822WSP(cByteList pBytes) { bool lResult = false; while (true) { if (Position.AtEnd || Position.BytesLine.Literal) { return(lResult); } byte lByte = Position.BytesLine[Position.Byte]; if (lByte != cASCII.SPACE && lByte != cASCII.TAB) { return(lResult); } lResult = true; pBytes.Add(lByte); ZAdvance(ref Position); } }
public cTextCommandPart(string pString, bool pSecret = false) : base(pSecret, false) { if (string.IsNullOrEmpty(pString)) { throw new ArgumentOutOfRangeException(nameof(pString)); } var lBytes = new cByteList(pString.Length); foreach (char lChar in pString) { if (lChar < ' ' || lChar > '~') { throw new ArgumentOutOfRangeException(nameof(pString)); } lBytes.Add((byte)lChar); } Bytes = new cBytes(lBytes); }
public static cByteList IntToBytesReverse(int pNumber) { if (pNumber < 0) { throw new ArgumentOutOfRangeException(nameof(pNumber)); } cByteList lBytes = new cByteList(10); int lNumber = pNumber; do { int lDigit = lNumber % 10; lBytes.Add((byte)(cASCII.ZERO + lDigit)); lNumber = lNumber / 10; } while (lNumber > 0); return(lBytes); }
private static bool ZTryAsBytesInCharset(string pString, cCharset pCharset, bool pSecret, out cTextCommandPart rResult) { if (string.IsNullOrEmpty(pString)) { rResult = null; return(false); } var lBytes = new cByteList(pString.Length); foreach (char lChar in pString) { if (!pCharset.Contains(lChar)) { rResult = null; return(false); } lBytes.Add((byte)lChar); } rResult = new cTextCommandPart(lBytes, pSecret); return(true); }
public static bool TryAsASCIILiteral(string pString, bool pSecret, out cLiteralCommandPart rResult) { if (pString == null) { rResult = null; return(false); } var lBytes = new cByteList(pString.Length); foreach (char lChar in pString) { if (lChar == cChar.NUL || lChar > cChar.DEL) { rResult = null; return(false); } lBytes.Add((byte)lChar); } rResult = new cLiteralCommandPart(lBytes, false, pSecret); return(true); }
private void ZLogResponse(cResponse pResponse, cTrace.cContext pParentContext) { var lContext = pParentContext.NewMethod(nameof(cConnection), nameof(ZLogResponse)); cByteList lLogBytes = new cByteList(); foreach (var lLine in pResponse) { if (lLine.Literal) { lLogBytes.Add(cASCII.LBRACE); } else { lLogBytes.Add(cASCII.LBRACKET); } foreach (var lByte in lLine) { if (lLogBytes.Count == 60) { lContext.TraceVerbose($"received: {cTools.BytesToLoggableString(lLogBytes)}..."); return; } lLogBytes.Add(lByte); } if (lLine.Literal) { lLogBytes.Add(cASCII.RBRACE); } else { lLogBytes.Add(cASCII.RBRACKET); } } lContext.TraceVerbose($"received: {cTools.BytesToLoggableString(lLogBytes)}"); }
private bool ZGetRFC822FWS(cByteList pBytes) { bool lResult = ZGetRFC822WSP(pBytes); while (true) { var lBookmark = Position; if (!SkipBytes(CRLF)) { return(lResult); } if (!ZGetRFC822WSP(pBytes)) { Position = lBookmark; return(lResult); } lResult = true; } }
public override IList <byte> GetResponse(IList <byte> pChallenge) { if (mDone) { throw new InvalidOperationException(kInvalidOperationExceptionMessage.AlreadyChallenged); } mDone = true; if (pChallenge != null && pChallenge.Count != 0) { throw new ArgumentOutOfRangeException("non zero length challenge"); } var lBytes = new cByteList(); lBytes.Add(0); lBytes.AddRange(Encoding.UTF8.GetBytes(mAuthenticationId)); lBytes.Add(0); lBytes.AddRange(Encoding.UTF8.GetBytes(mPassword)); return(lBytes); }
public bool GetNumber(out cByteList rBytes, out uint rNumber, int pMinLength = 1, int pMaxLength = int.MaxValue) { var lBookmark = Position; rNumber = 0; if (!GetToken(cCharset.Digit, null, null, out rBytes, pMinLength, pMaxLength)) { return(false); } checked { try { foreach (byte lByte in rBytes) { rNumber = rNumber * 10 + lByte - cASCII.ZERO; } } catch { Position = lBookmark; return(false); } } return(true); }
private void ZAddWholeLine(bool pLiteral, cTrace.cContext pParentContext) { var lContext = pParentContext.NewMethod(nameof(cResponseBuilder), nameof(ZAddWholeLine), pLiteral); if (!pLiteral && mBytes.Count == 0) { if (!lContext.ContextTraceDelay) { lContext.TraceVerbose("received line an empty line"); } return; } var lLine = new cResponseLine(pLiteral, mBytes); mBytes = new cByteList(); if (!lContext.ContextTraceDelay) { lContext.TraceVerbose("received {0}", lLine); } mLines.Add(lLine); }