public void TestByteStreamReading() { // Test bytes using (ByteStream stream = new ByteStream(testData)) { for (int i = 0; i < testData.Length; i++) { Assert.AreEqual(testData[i], stream.ReadByte(), "ReadByte returned incorrect value"); } } // Test ushorts using (ByteStream stream = new ByteStream(testData)) { Assert.AreEqual(0x0841, stream.ReadUshort()); Assert.AreEqual(0x6472, stream.ReadUshort()); Assert.AreEqual(0x6965, stream.ReadUshort()); Assert.AreEqual(0x6e6e, stream.ReadUshort()); } // Test uints using (ByteStream stream = new ByteStream(testData)) { Assert.AreEqual(0x08416472, stream.ReadUint()); Assert.AreEqual(0x69656e6e, stream.ReadUint()); } // Test string using (ByteStream stream = new ByteStream(testData)) { Assert.AreEqual("Adrienne", stream.ReadString(stream.ReadByte(), Encoding.ASCII)); } }
public void TestByteStreamNumberWriting() { ByteStream stream = new ByteStream(); stream.WriteByte(0x08); stream.WriteUshort(0x4164); stream.WriteUint(0x7269656e); stream.WriteByteArray(new byte[] {0x6e, 0x65}); Assert.AreEqual(testData.Length, stream.GetByteCount()); ByteStream testStream = new ByteStream(stream.GetBytes()); Assert.AreEqual("Adrienne", testStream.ReadString(testStream.ReadByte(), Encoding.ASCII)); }
/// <summary> /// Parses a byte buffer into an <see cref="BartID"/> object /// </summary> private void ReadIconInfo(byte[] buffer, UserInfo userinfo) { using (ByteStream iconStream = new ByteStream(buffer)) { int iconStreamSize = iconStream.GetByteCount(); while (iconStream.CurrentPosition + 4 <= iconStreamSize) { BartID item = new BartID(iconStream); // Find the end of the current data item in the stream int endDataPosition = iconStream.CurrentPosition + item.Data.Length; switch (item.Type) { case BartTypeId.BuddyIcon: if (!GraphicsManager.IsBlankIcon(item.Data)) { userinfo.Icon = item; } break; case BartTypeId.StatusString: // Available message using (ByteStream messageStream = new ByteStream(item.Data)) { Encoding encoding = Encoding.UTF8; byte[] amessage = new byte[0]; if (messageStream.HasMoreData) { // Pull the message to a byte array, assume at first that the encoding // is UTF-8. If existing encoding information exists, use that instead amessage = messageStream.ReadByteArray(messageStream.ReadByte()); // Check if there's encoding information available if (messageStream.HasMoreData) { // Check to see if the encoding's been specified if (messageStream.ReadUshort() == 0x0001) { messageStream.AdvanceOffset(2); string encodingStr = messageStream.ReadString(messageStream.ReadUshort(), Encoding.ASCII); // Try to use the encoding from the byte stream try { encoding = Encoding.GetEncoding(encodingStr); } catch (ArgumentException) { Logging.WriteString( "ReadIconInfo: Got unknown encoding for available message (" + encodingStr + "), falling back to UTF-8"); encoding = Encoding.UTF8; } } } } userinfo.AvailableMessage = Encoding.Unicode.GetString( Encoding.Convert(encoding, Encoding.Unicode, amessage)); } break; default: break; } } } }
/// <summary> /// Initializes a new BartID from a byte stream /// </summary> internal BartID(ByteStream stream) { type = (BartTypeId)stream.ReadUshort(); flags = (BartFlags)stream.ReadByte(); byte dataLength = stream.ReadByte(); data = stream.ReadByteArray(dataLength); }
/// <summary> /// Processes a newly received offline message for the specified UIN /// </summary> /// <remarks>This is called from the ICQ manager, which receives the incoming /// offline message packet and sends it here for processing</remarks> internal void ReadIcqOfflineMessage(String uin, ByteStream stream) { String sender = stream.ReadUintLE().ToString(); // Read in the date information (GMT) ushort year = stream.ReadUshortLE(); byte month = stream.ReadByte(); byte day = stream.ReadByte(); byte hourGmt = stream.ReadByte(); byte minute = stream.ReadByte(); DateTime received = new DateTime(year, month, day, hourGmt, minute, 0, DateTimeKind.Utc); // Read in message type information byte messageType = stream.ReadByte(); byte messageFlags = stream.ReadByte(); Encoding messageEncoding = GetOfflineMessageEncoding(messageType); OfflineIM im = new OfflineIM(sender); im.ReceivedOn = received; im.Message = Encoding.Unicode.GetString( Encoding.Convert(messageEncoding, Encoding.Unicode, stream.ReadByteArray(stream.ReadUshortLE() - 1))); im.IsAutoResponse = (messageFlags == 0x03) || (messageType == 0xE8); // Store it for delivery to the client AcceptIcbmOIM(im); }
/// <summary> /// Retrieves the message text from SNAC(04,07) TLV 02 /// </summary> /// <param name="stream">A received <see cref="ByteStream"/></param> /// <param name="message">An <see cref="IM"/> object to be populated</param> private void GetChannelOneMessage(ByteStream stream, ref IM message) { while (stream.HasMoreData) { byte identifier = stream.ReadByte(); byte version = stream.ReadByte(); ushort length = stream.ReadUshort(); switch (identifier) { case 0x01: // Message text ushort charset = stream.ReadUshort(); ushort language = stream.ReadUshort(); Encoding incoming = IM.GetEncodingFromCharset(charset, language); message.Message = Encoding.Unicode.GetString( Encoding.Convert(incoming, Encoding.Unicode, stream.ReadByteArray(length - 4))); break; default: // Unhandled case stream.AdvanceOffset(length); break; } } }
/// <summary> /// Retrieves the message text from SNAC(04,07) TLV 05 /// </summary> /// <param name="stream">A received <see cref="ByteStream"/></param> /// <param name="message">An <see cref="IM"/> object to be populated</param> private void GetChannelfourMessage(ByteStream stream, ref IM message) { stream.ReadByteArray(46); //fixed unknon part ushort length = stream.ReadUshort(); ushort charset = stream.ReadUshort(); stream.ReadByte(); ushort charsubset = (ushort)0;//stream.ReadUshort(); Encoding incoming = IM.GetEncodingFromCharset(charset, charsubset); string xml = Encoding.Unicode.GetString( Encoding.Convert(incoming, Encoding.Unicode, stream.ReadByteArray(length))); // parse xml System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); doc.LoadXml(xml); System.Xml.XmlNode node = doc.SelectSingleNode("/sms_message/text"); if (node != null) { message.Message = node.InnerText; } node = doc.SelectSingleNode("/sms_message/sender"); if (node != null) { message.ScreenName = node.InnerText; } }
/// <summary> /// Parses a byte buffer into an <see cref="BartID"/> object /// </summary> private void ReadIconInfo(byte[] buffer, UserInfo userinfo) { using (ByteStream iconStream = new ByteStream(buffer)) { int iconStreamSize = iconStream.GetByteCount(); while (iconStream.CurrentPosition + 4 <= iconStreamSize) { BartID item = new BartID(iconStream); // Find the end of the current data item in the stream int endDataPosition = iconStream.CurrentPosition + item.Data.Length; switch (item.Type) { case BartTypeId.BuddyIcon: if (!GraphicsManager.IsBlankIcon(item.Data)) { userinfo.Icon = item; } break; case BartTypeId.StatusString: // Available message using (ByteStream messageStream = new ByteStream(item.Data)) { Encoding encoding = Encoding.UTF8; byte[] amessage = new byte[0]; if (messageStream.HasMoreData) { // Pull the message to a byte array, assume at first that the encoding // is UTF-8. If existing encoding information exists, use that instead amessage = messageStream.ReadByteArray(messageStream.ReadByte()); // Check if there's encoding information available if (messageStream.HasMoreData) { // Check to see if the encoding's been specified if (messageStream.ReadUshort() == 0x0001) { messageStream.AdvanceOffset(2); string encodingStr = messageStream.ReadString(messageStream.ReadUshort(), Encoding.ASCII); // Try to use the encoding from the byte stream try { encoding = Encoding.GetEncoding(encodingStr); } catch (ArgumentException) { Logging.WriteString( "ReadIconInfo: Got unknown encoding for available message (" + encodingStr + "), falling back to UTF-8"); encoding = Encoding.UTF8; } } } } userinfo.AvailableMessage = Encoding.Unicode.GetString( Encoding.Convert(encoding, Encoding.Unicode, amessage)); } break; default: break; } } } }