public unsafe void DecoderMultipleMisalignedCallTest() { string str = "\u0135\u0136\u0137\u0138\u0139"; var decodeBuffer = new ArraySegment<char>(new char[3]); StringDecoder decoder = new StringDecoder(decodeBuffer); byte[] bytes = Encoding.UTF8.GetBytes(str); decoder.Decode(new ArraySegment<byte>(bytes.Take(3).ToArray())); decoder.Decode(new ArraySegment<byte>(bytes.Skip(3).ToArray())); var decodedString = decoder.ToString(); Console.WriteLine(decodedString); Assert.AreEqual(str, decodedString); }
private Envelope ReadEnvelope() { var date = ReadString(); var subject = ReadString(); var from = ReadMailAddressCollection(); var sender = ReadMailAddressCollection(); var replyTo = ReadMailAddressCollection(); var to = ReadMailAddressCollection(); var cc = ReadMailAddressCollection(); var bcc = ReadMailAddressCollection(); var inReplyTo = ReadString(); var messageId = ReadString(); return(new Envelope { Bcc = bcc, Cc = cc, Date = HeaderFieldParser.ParseDate(date), From = from.FirstOrDefault(), InReplyTo = StringDecoder.Decode(inReplyTo, true), MessageId = messageId, ReplyTo = replyTo, Sender = sender.FirstOrDefault(), Subject = StringDecoder.Decode(subject, true), To = to }); }
public static MailAddress ParseMailAddress(string value) { int num = value.LastIndexOf("<", StringComparison.Ordinal); if (num < 0) { return(new MailAddress(string.Empty, value.Trim())); } string address = value.Substring(num).Trim().TrimStart('<').TrimEnd('>'); if (string.IsNullOrEmpty(address)) { return(null); } string displayName = ""; if (num >= 1) { displayName = value.Substring(0, num - 1).Trim(); } return(new MailAddress(StringDecoder.Decode(displayName, true).Trim().Trim(new[] { ' ', '<', '>', '\r', '\n' }), address)); }
private ContentDisposition ReadDisposition() { SkipSpaces(); var sb = new StringBuilder(); char currentChar; while ((currentChar = (char)_reader.Read()) != '(') { sb.Append(currentChar); if (sb.ToString() == "NIL") { return(null); } } var type = ReadString().ToLower(); var paramaters = ReadParameterList(); var disposition = new ContentDisposition(type); foreach (var paramater in paramaters) { switch (paramater.Key) { case "filename": case "name": disposition.FileName = StringDecoder.Decode(paramater.Value, true); break; } } SkipSpaces(); _reader.Read(); // read ')' SkipSpaces(); return(disposition); }
void UpdateAssemblyPanel() { lstView_Dissassembly.Items.Clear(); int rows = 5; int PC = Ada.PC; for (int i = PC - 4 * rows; i < PC + 4 * rows; i += 4) { if (i < 0 || i > Ada.RAM.MemoryArray.Length) { LstStruct empty = new LstStruct { Address = "0x" + i.ToString("X8"), Values = "", Instruction = "" }; continue; } int instCode = Ada.RAM.ReadWord(i); LstStruct row = new LstStruct { Address = "0x" + i.ToString("X8"), Values = StringDecoder.Decode(instCode, Ada.Registers, Ada.RAM, Ada.Flags, i), Instruction = instCode.ToString("X8") }; lstView_Dissassembly.Items.Add(row); } lstView_Dissassembly.SelectedIndex = rows; }
public void ShouldDecodeAnASCIIStringFromACompleteBuffer() { StringDecoder Decoder = new StringDecoder(1024, bufPool); // 0 Characters var buf = new Buffer(); buf.WriteByte(0x00); var consumed = Decoder.Decode(buf.View); Assert.True(Decoder.Done); Assert.Equal("", Decoder.Result); Assert.Equal(0, Decoder.StringLength); Assert.Equal(1, consumed); buf = new Buffer(); buf.WriteByte(0x04); // 4 Characters, non huffman buf.WriteByte('a'); buf.WriteByte('s'); buf.WriteByte('d'); buf.WriteByte('f'); consumed = Decoder.Decode(buf.View); Assert.True(Decoder.Done); Assert.Equal("asdf", Decoder.Result); Assert.Equal(4, Decoder.StringLength); Assert.Equal(5, consumed); // Multi-byte prefix buf = new Buffer(); buf.WriteByte(0x7F); // Prefix filled, non huffman, I = 127 buf.WriteByte(0xFF); // I = 0x7F + 0x7F * 2^0 = 0xFE buf.WriteByte(0x03); // I = 0xFE + 0x03 * 2^7 = 0xFE + 0x180 = 0x27E = 638 var expectedLength = 638; var expectedString = ""; for (var i = 0; i < expectedLength; i++) { buf.WriteByte(' '); expectedString += ' '; } consumed = Decoder.Decode(buf.View); Assert.True(Decoder.Done); Assert.Equal(expectedString, Decoder.Result); Assert.Equal(expectedLength, Decoder.StringLength); Assert.Equal(3 + expectedLength, consumed); }
public void ShouldCheckTheMaximumStringLength() { StringDecoder Decoder = new StringDecoder(2, bufPool); // 2 Characters are ok var buf = new Buffer(); buf.WriteByte(0x02); buf.WriteByte('a'); buf.WriteByte('b'); var consumed = Decoder.Decode(buf.View); Assert.True(Decoder.Done); Assert.Equal("ab", Decoder.Result); Assert.Equal(2, Decoder.StringLength); Assert.Equal(3, consumed); // 3 should fail buf = new Buffer(); buf.WriteByte(0x03); buf.WriteByte('a'); buf.WriteByte('b'); buf.WriteByte('c'); var ex = Assert.Throws <Exception>(() => Decoder.Decode(buf.View)); Assert.Equal("Maximum string length exceeded", ex.Message); // Things were the length is stored in a continuation byte should also fail buf = new Buffer(); buf.WriteByte(0x7F); // More than 127 bytes consumed = Decoder.Decode(buf.View); Assert.False(Decoder.Done); Assert.Equal(1, consumed); buf.WriteByte(1); var view = new ArraySegment <byte>(buf.Bytes, 1, 1); ex = Assert.Throws <Exception>(() => Decoder.DecodeCont(view)); Assert.Equal("Maximum string length exceeded", ex.Message); }
private ContentDisposition ReadDisposition() { SkipSpaces(); var sb = new StringBuilder(); char currentChar; while ((currentChar = (char)_reader.Read()) != '(') { sb.Append(currentChar); if (sb.ToString() == "NIL") { return(null); } if (currentChar == Convert.ToChar(65535)) { return(null); } } var type = ReadString().ToLower(); var paramaters = ReadParameterList(); ContentDisposition disposition = null; try { disposition = new ContentDisposition(type); foreach (var paramater in paramaters) { switch (paramater.Key) { case "filename": case "name": disposition.FileName = StringDecoder.Decode(paramater.Value, true); break; } } SkipSpaces(); _reader.Read(); // read ')' SkipSpaces(); } catch (Exception ex) { //EventLog.WriteEntry("purgeSource", ex.ToString()); //throw new Exception("Failed on: " + type, ex); } return(disposition); }
public unsafe void DecoderTest() { //string str = "abcdefghijklmnop\u0135"; string str = "\u0135\u0135\u0135\u0135\u0135"; var decodeBuffer = new ArraySegment<char>(new char[3]); StringDecoder decoder = new StringDecoder(decodeBuffer); decoder.Decode(new ArraySegment<byte>(Encoding.UTF8.GetBytes(str))); var decodedString = decoder.ToString(); Console.WriteLine(decodedString); Assert.AreEqual(str, decodedString); }
public void ShouldDecodeAHuffmanEncodedStringIfLengthAndPayloadAreInMultipleBuffers() { StringDecoder Decoder = new StringDecoder(1024, bufPool); // Only put the prefix in the first byte var buf = new Buffer(); buf.WriteByte(0xFF); // Prefix filled, non huffman, I = 127 var consumed = Decoder.Decode(buf.View); Assert.False(Decoder.Done); Assert.Equal(1, consumed); // Remaining part of the length plus first content byte buf = new Buffer(); buf.WriteByte(0x02); // I = 0x7F + 0x02 * 2^0 = 129 byte payload buf.WriteByte(0xf9); // first byte of the payload var expectedResult = "*"; consumed = Decoder.DecodeCont(buf.View); Assert.False(Decoder.Done); Assert.Equal(2, consumed); // Half of other content bytes buf = new Buffer(); for (var i = 0; i < 64; i = i + 2) { expectedResult += ")-"; buf.WriteByte(0xfe); buf.WriteByte(0xd6); } consumed = Decoder.DecodeCont(buf.View); Assert.False(Decoder.Done); Assert.Equal(64, consumed); // Last part of content bytes buf = new Buffer(); for (var i = 0; i < 64; i = i + 2) { expectedResult += "0+"; buf.WriteByte(0x07); buf.WriteByte(0xfb); } consumed = Decoder.DecodeCont(buf.View); Assert.True(Decoder.Done); Assert.Equal(expectedResult, Decoder.Result); Assert.Equal(129, Decoder.StringLength); Assert.Equal(64, consumed); }
public void ShouldDecodeAnASCIIStringIfPayloadIsInMultipleBuffers() { StringDecoder Decoder = new StringDecoder(1024, bufPool); // Only put the prefix in the first byte var buf = new Buffer(); buf.WriteByte(0x04); // 4 Characters, non huffman var consumed = Decoder.Decode(buf.View); Assert.False(Decoder.Done); Assert.Equal(1, consumed); // Next chunk with part of data buf = new Buffer(); buf.WriteByte('a'); buf.WriteByte('s'); consumed = Decoder.DecodeCont(buf.View); Assert.False(Decoder.Done); Assert.Equal(2, consumed); // Give the thing a depleted buffer consumed = Decoder.DecodeCont(new ArraySegment <byte>(buf.Bytes, 2, 0)); Assert.False(Decoder.Done); Assert.Equal(0, consumed); // Final chunk buf = new Buffer(); buf.WriteByte('d'); buf.WriteByte('f'); consumed = Decoder.DecodeCont(buf.View); Assert.True(Decoder.Done); Assert.Equal("asdf", Decoder.Result); Assert.Equal(4, Decoder.StringLength); Assert.Equal(2, consumed); }
private void BindHeadersToFields() { foreach (var header in Headers) { switch (header.Key) { case MessageHeader.MimeVersion: MimeVersion = header.Value; break; case MessageHeader.Sender: Sender = HeaderFieldParser.ParseMailAddress(header.Value); break; case MessageHeader.Subject: Subject = StringDecoder.Decode(header.Value, true); break; case MessageHeader.To: if (To.Count == 0) { To = HeaderFieldParser.ParseMailAddressCollection(header.Value); } else { foreach (MailAddress addr in HeaderFieldParser.ParseMailAddressCollection(header.Value)) { To.Add(addr); } } break; case MessageHeader.DeliveredTo: To.Add(HeaderFieldParser.ParseMailAddress(header.Value)); break; case MessageHeader.From: From = HeaderFieldParser.ParseMailAddress(header.Value); break; case MessageHeader.Cc: Cc = HeaderFieldParser.ParseMailAddressCollection(header.Value); break; case MessageHeader.Bcc: Bcc = HeaderFieldParser.ParseMailAddressCollection(header.Value); break; case MessageHeader.Organisation: case MessageHeader.Organization: Organization = StringDecoder.Decode(header.Value, true); break; case MessageHeader.Date: Date = HeaderFieldParser.ParseDate(header.Value); break; case MessageHeader.Importance: Importance = header.Value.ToMessageImportance(); break; case MessageHeader.ContentType: ContentType = HeaderFieldParser.ParseContentType(header.Value); break; case MessageHeader.ContentTransferEncoding: ContentTransferEncoding = header.Value; break; case MessageHeader.MessageId: MessageId = header.Value; break; case MessageHeader.Mailer: case MessageHeader.XMailer: Mailer = header.Value; break; case MessageHeader.ReplyTo: ReplyTo = HeaderFieldParser.ParseMailAddressCollection(header.Value); break; case MessageHeader.Sensitivity: Sensitivity = header.Value.ToMessageSensitivity(); break; case MessageHeader.ReturnPath: ReturnPath = HeaderFieldParser.ParseMailAddress( header.Value.Split(new[] { '\r', '\n' }, StringSplitOptions.None) .Distinct() .FirstOrDefault()); break; case MessageHeader.ContentLanguage: case MessageHeader.Language: Language = header.Value; break; case MessageHeader.InReplyTo: InReplyTo = header.Value; break; case MessageHeader.Comments: Comments = header.Value; break; } } }
public MessageContent ParsePart(int number, int level) { var part = new MessageContent(_client, _message); var block = level > 2 ? level - 1 : 0; if (block != 0) { level = level - (level - 2); } part.ContentNumber = level < 2 ? number.ToString() : level - 1 + "." + (block == 0 ? "" : block + ".") + number; var contentType = ReadString().ToLower(); var contentSubType = ReadString().ToLower(); if (string.IsNullOrEmpty(contentType)) { // Set content type to application/octet-stream in case server returned NIL contentType = "application"; contentSubType = "octet-stream"; } part.ContentType = HeaderFieldParser.ParseContentType(contentType + "/" + contentSubType); part.Parameters = ReadParameterList(); if (part.Parameters.ContainsKey("content-type")) { try { if (part.ContentType == null) { part.ContentType = HeaderFieldParser.ParseContentType(part.Parameters["content-type"]); } } catch (Exception ex) { ex.ToString(); } } if (part.Parameters.ContainsKey("charset")) { if (part.ContentType == null) { part.ContentType = new ContentType(); } part.ContentType.CharSet = part.Parameters["charset"]; } if (part.Parameters.ContainsKey("name") || part.Parameters.ContainsKey("filename")) { var value = StringDecoder.Decode(part.Parameters.ContainsKey("name") ? part.Parameters["name"] : part.Parameters["filename"], true); if (part.ContentType == null) { part.ContentType = new ContentType(); } if (part.ContentDisposition == null) { part.ContentDisposition = new ContentDisposition(); } part.ContentDisposition.FileName = value; if (string.IsNullOrEmpty(part.ContentDisposition.DispositionType)) { part.ContentDisposition.DispositionType = DispositionTypeNames.Attachment; } part.ContentType.Name = value; } if (part.Parameters.ContainsKey("content-id")) { if (part.ContentDisposition == null) { part.ContentDisposition = new ContentDisposition(); } part.ContentDisposition.DispositionType = DispositionTypeNames.Inline; part.ContentId = part.Parameters["content-id"].Trim(' ', '<', '>'); } part.ContentId = ReadString(); part.Description = ReadString(); part.ContentTransferEncoding = ReadString().ToContentTransferEncoding(); part.Size = ReadLong(); if (contentType == "text") { part.Lines = ReadLong(); } else if (contentType == "message" && contentSubType == "rfc822") { SkipSpaces(); if (_reader.Peek() == '(') { part.Envelope = ReadEnvelope(); _reader.Read(); // Read ')' of envelope DropExtensionData(); part.Lines = ReadLong(); } else { // Some servers, e.g GMail, don't return // envelope & body strcuture for message/rfc822 attachments if (part.ContentDisposition == null) { part.ContentDisposition = new ContentDisposition(); } part.ContentDisposition.FileName = "unnamed.eml"; if (string.IsNullOrEmpty(part.ContentDisposition.DispositionType)) { part.ContentDisposition.DispositionType = DispositionTypeNames.Attachment; } } } SkipSpaces(); if (_reader.Peek() == ')') { _reader.Read(); return(part); } part.Md5 = ReadString(); if (_reader.Peek() == ')') { _reader.Read(); return(part); } part.ContentDisposition = ReadDisposition() ?? part.ContentDisposition; if (!string.IsNullOrEmpty(part.ContentId) && part.ContentDisposition != null) { part.ContentDisposition.DispositionType = DispositionTypeNames.Inline; } if (part.ContentDisposition != null && string.IsNullOrEmpty(part.ContentDisposition.FileName)) { part.ContentDisposition.FileName = string.Format("unnamed-{0}.{1}", part.ContentNumber, contentType == "message" ? "eml" : "dat"); } if (_reader.Peek() == ')') { _reader.Read(); return(part); } part.Language = ReadLanguage(); if (_reader.Peek() == ')') { _reader.Read(); return(part); } DropExtensionData(); return(part); }
static void Main(string[] args) { Console.WriteLine(StringDecoder.Decode("Hello World")); Console.Read(); }
public override void ProcessCommandResult(string data) { int index, index2; if ((index = data.IndexOf("BODY[" + ContentNumber + ".MIME]", StringComparison.Ordinal)) != -1) { if ((index2 = data.IndexOf("BODY[" + ContentNumber + ".MIME] NIL", StringComparison.Ordinal)) != -1) { data = data.Replace("BODY[" + ContentNumber + ".MIME] NIL", ""); } else { data = CleanData(data, index).Trim(); if (data.StartsWith("*") && data.Contains("FETCH")) { data = CommandStartRex.Replace(data, ""); } if (!string.IsNullOrEmpty(data)) { AppendDataToContentStream(data); } _fetchState = MessageFetchState.Headers; _fetchProgress = _fetchProgress | MessageFetchState.Headers; return; } } if ((index = data.IndexOf("BODY[" + ContentNumber + "]", StringComparison.Ordinal)) != -1) { data = CleanData(data, index).Trim(); if (data.StartsWith("*") && (data.Contains("UID") || data.Contains("FETCH"))) { data = CommandStartRex.Replace(data, ""); } if (!string.IsNullOrEmpty(data)) { AppendDataToContentStream(data); } _fetchState = MessageFetchState.Body; _fetchProgress = _fetchProgress | MessageFetchState.Body; return; } if (_fetchState == MessageFetchState.Headers) { try { Match headerMatch = Expressions.HeaderParseRex.Match(data); if (!headerMatch.Success) { return; } string key = headerMatch.Groups[1].Value.ToLower(); string value = headerMatch.Groups[2].Value; if (this.ContentType != null && this.ContentType.MediaType == "message/rfc822") { _contentBuilder.AppendLine(data); } if (Parameters.ContainsKey(key)) { Parameters[key] = value; } else { Parameters.Add(key, value); } switch (key) { case "content-type": if (ContentType == null) { ContentType = HeaderFieldParser.ParseContentType(value); } if (!string.IsNullOrEmpty(ContentType.Name)) { ContentType.Name = StringDecoder.Decode(ContentType.Name, true); if (ContentDisposition == null) { ContentDisposition = new ContentDisposition { DispositionType = DispositionTypeNames.Attachment } } ; ContentDisposition.FileName = ContentType.Name; } break; case "charset": if (ContentType == null) { ContentType = new ContentType(); } ContentType.CharSet = value; break; case "filename": case "name": value = StringDecoder.Decode(value, true); if (ContentType == null) { ContentType = new ContentType(); } if (ContentDisposition == null) { ContentDisposition = new ContentDisposition(); } ContentDisposition.FileName = value; if (string.IsNullOrEmpty(ContentDisposition.DispositionType) && string.IsNullOrEmpty(ContentId)) { ContentDisposition.DispositionType = DispositionTypeNames.Attachment; } ContentType.Name = value; break; case "content-id": if (ContentDisposition == null) { ContentDisposition = new ContentDisposition(); } ContentDisposition.DispositionType = DispositionTypeNames.Inline; ContentId = value.Trim(' ', '<', '>'); break; case "content-disposition": if (ContentDisposition == null) { ContentDisposition = new ContentDisposition(value); } if (!string.IsNullOrEmpty(ContentId)) { ContentDisposition.DispositionType = DispositionTypeNames.Inline; } break; case "content-transfer-encoding": ContentTransferEncoding = value.ToContentTransferEncoding(); break; } } catch { } } else if (CommandEndRex.IsMatch(data)) { for (var i = _contentBuilder.Length - 1; i >= 0; i--) { if (_contentBuilder[i] == ')') { _contentBuilder.Remove(i, _contentBuilder.Length - i); return; } } } else if ((index = data.IndexOf("UID")) != -1) { data = CommandJunkUID.Replace(data, ""); AppendDataToContentStream(data); return; } else if ((index = data.IndexOf("UID")) != -1) { data = CommandJunkUID.Replace(data, ""); AppendDataToContentStream(data); return; } else { AppendDataToContentStream(data); } }