int GetCount(byte formatCode, ByteBuffer buffer) { int count; if (formatCode >= 0xA0) { //bin8 a0 10100001 //str8 a1 10100001 //sym8 a3 10100011 //list8 c0 11000000 //map8 c1 11000001 //arr8 e0 11100000 //bin32 b0 10110000 //str32 b1 10110001 //sym32 b3 10110011 //lit32 d0 11010000 //map32 d1 11010001 //arr32 f0 11110000 if ((formatCode & 0x10) == 0) { count = AmqpBitConverter.ReadUByte(buffer); } else { count = AmqpBitConverter.ReadInt(buffer); } } else { count = (1 << ((formatCode >> 4) - 4)) >> 1; } return(count); }
public static Frame Decode(ByteBuffer buffer, bool fullBody = true) { Frame frame = new Frame(); frame.RawBuffer = buffer.Array; // Header frame.size = (int)AmqpBitConverter.ReadUInt(buffer); frame.dataOffset = AmqpBitConverter.ReadUByte(buffer); frame.Type = (FrameType)AmqpBitConverter.ReadUByte(buffer); frame.Channel = AmqpBitConverter.ReadUShort(buffer); // skip extended header buffer.Complete(frame.dataOffset * 4 - Frame.HeaderSize); // Command if (buffer.Length > 0) { frame.Command = (Performative)AmqpCodec.CreateAmqpDescribed(buffer); if (fullBody) { frame.Command.DecodeValue(buffer); } } return(frame); }
static string ReadString(ByteBuffer buffer, byte formatCode, byte code8, byte code32, string type) { if (formatCode == FormatCode.Null) { return(null); } int count; if (formatCode == code8) { count = AmqpBitConverter.ReadUByte(buffer); } else if (formatCode == code32) { count = (int)AmqpBitConverter.ReadUInt(buffer); } else { throw DecodeException(formatCode, buffer.Offset); } buffer.Validate(false, count); string value = new string(Encoding.UTF8.GetChars(buffer.Buffer, buffer.Offset, count)); buffer.Complete(count); return(value); }
public static byte[] ReadBinary(ByteBuffer buffer, byte formatCode) { if (formatCode == FormatCode.Null) { return(null); } int count; if (formatCode == FormatCode.Binary8) { count = AmqpBitConverter.ReadUByte(buffer); } else if (formatCode == FormatCode.Binary32) { count = (int)AmqpBitConverter.ReadUInt(buffer); } else { throw DecodeException(formatCode, buffer.Offset); } buffer.Validate(false, count); byte[] value = new byte[count]; Array.Copy(buffer.Buffer, buffer.Offset, value, 0, count); buffer.Complete(count); return(value); }
internal override void DecodeValue(ByteBuffer buffer) { int offset = buffer.Offset; byte formatCode = Encoder.ReadFormatCode(buffer); if (formatCode == FormatCode.Binary8) { this.binaryOffset = 2; } else if (formatCode == FormatCode.Binary32) { this.binaryOffset = 5; } else { while (formatCode == FormatCode.Described) { Encoder.ReadObject(buffer); formatCode = Encoder.ReadFormatCode(buffer); } } int count; if (formatCode >= 0xA0) { //bin8 a0 10100001 //str8 a1 10100001 //sym8 a3 10100011 //list8 c0 11000000 //map8 c1 11000001 //arr8 e0 11100000 //bin32 b0 10110000 //str32 b1 10110001 //sym32 b3 10110011 //lit32 d0 11010000 //map32 d1 11010001 //arr32 f0 11110000 if ((formatCode & 0x10) == 0) { count = AmqpBitConverter.ReadUByte(buffer); } else { count = AmqpBitConverter.ReadInt(buffer); } } else { count = (1 << ((formatCode >> 4) - 4)) >> 1; } buffer.Validate(false, count); buffer.Complete(count); int size = buffer.Offset - offset; this.valueBuffer = new ByteBuffer(buffer.Buffer, offset, size, size); }
private void DecodeHeader(ByteBuffer buffer) { this.Size = (int)AmqpBitConverter.ReadUInt(buffer); this.DataOffset = AmqpBitConverter.ReadUByte(buffer); this.Type = (FrameType)AmqpBitConverter.ReadUByte(buffer); this.Channel = AmqpBitConverter.ReadUShort(buffer); buffer.Complete(this.DataOffset * 4 - 8); }
public static byte?Decode(ByteBuffer buffer, FormatCode formatCode) { if (formatCode == 0 && (formatCode = AmqpEncoding.ReadFormatCode(buffer)) == FormatCode.Null) { return(null); } return(AmqpBitConverter.ReadUByte(buffer)); }
void DecodeHeader(ByteBuffer buffer) { this.Size = (int)AmqpBitConverter.ReadUInt(buffer); this.DataOffset = AmqpBitConverter.ReadUByte(buffer); this.Type = (FrameType)AmqpBitConverter.ReadUByte(buffer); this.Channel = AmqpBitConverter.ReadUShort(buffer); // skip extended header buffer.Complete(this.DataOffset * 4 - Frame.HeaderSize); }
public static byte ReadUByte(ByteBuffer buffer, byte formatCode) { if (formatCode == FormatCode.UByte) { return(AmqpBitConverter.ReadUByte(buffer)); } else { throw DecodeException(formatCode, buffer.Offset); } }
public void Decode(ByteBuffer buffer) { if (buffer.Length < this.EncodeSize) { throw AmqpEncoding.GetEncodingException(SRAmqp.AmqpInsufficientBufferSize(this.EncodeSize, buffer.Length)); } if (AmqpBitConverter.ReadUInt(buffer) != 1095586128) { throw AmqpEncoding.GetEncodingException("ProtocolName"); } this.protocolId = (Microsoft.ServiceBus.Messaging.Amqp.ProtocolId)AmqpBitConverter.ReadUByte(buffer); this.version = new AmqpVersion(AmqpBitConverter.ReadUByte(buffer), AmqpBitConverter.ReadUByte(buffer), AmqpBitConverter.ReadUByte(buffer)); }
public static AmqpFrame DecodeFrame(ByteBuffer buffer, out ushort channelNumber) { #if DEBUG byte[] debugFrameBuffer = new byte[buffer.LengthAvailableToRead]; Buffer.BlockCopy(buffer.Buffer, buffer.ReadOffset, debugFrameBuffer, 0, buffer.LengthAvailableToRead); #endif int frameStartOffset = buffer.ReadOffset; // frame header uint frameSize = AmqpBitConverter.ReadUInt(buffer); byte dataOffset = AmqpBitConverter.ReadUByte(buffer); byte frameType = AmqpBitConverter.ReadUByte(buffer); channelNumber = AmqpBitConverter.ReadUShort(buffer); // out param if (dataOffset < 2) { throw new AmqpException(ErrorCode.FramingError, $"DOFF must be >= 2. Value is {dataOffset.ToHex()}"); } // data offset is always counted in 4-byte words. header total length is 8 bytes int bodyStartOffset = 4 * dataOffset; // forward the reader the number of bytes needed to reach the frame body buffer.CompleteRead((bodyStartOffset - 8)); if (frameSize == bodyStartOffset) { // empty frame body return(null); } // we're expecting a described list... var formatCode = DecodeFormatCode(buffer); if (formatCode != FormatCode.Described) { throw new AmqpException(ErrorCode.FramingError, $"Expected Format Code = {FormatCode.Described.ToHex()} but was {formatCode.ToHex()}"); } try { // decode return((AmqpFrame)DecodeDescribedType(buffer, formatCode)); } catch (Exception) { #if DEBUG TraceSource.FromClass().Debug(Environment.NewLine + debugFrameBuffer.ToHex()); #endif throw; } }
static Error ScanDataSection(byte formatCode, ByteBuffer buffer) { switch (formatCode) { case FormatCode.Binary8: return(AdvanceBuffer(buffer, AmqpBitConverter.ReadUByte(buffer))); case FormatCode.Binary32: return(AdvanceBuffer(buffer, AmqpBitConverter.ReadUInt(buffer))); default: return(GetDecodeError(AmqpResources.GetString(Resources.AmqpInvalidFormatCode, formatCode, buffer.Offset))); } }
public static void Decode(ByteBuffer buffer, out ushort channel, out DescribedList command) { AmqpBitConverter.ReadUInt(buffer); AmqpBitConverter.ReadUByte(buffer); AmqpBitConverter.ReadUByte(buffer); channel = AmqpBitConverter.ReadUShort(buffer); if (buffer.Length > 0) { command = (DescribedList)Codec.Decode(buffer); } else { command = null; } }
private static void SkipBinaryBuffer(ByteBuffer buffer) { var binaryFormatCode = AmqpCodec.DecodeFormatCode(buffer); int size = 0; if (binaryFormatCode == FormatCode.Binary8) { size = AmqpBitConverter.ReadUByte(buffer); } else if (binaryFormatCode == FormatCode.Binary32) { size = (int)AmqpBitConverter.ReadUInt(buffer); } buffer.CompleteRead(size); }
bool OnFrame(Stream stream, ByteBuffer buffer) { buffer.Complete(1); byte type = AmqpBitConverter.ReadUByte(buffer); ushort channel = AmqpBitConverter.ReadUShort(buffer); DescribedList command = (DescribedList)Encoder.ReadDescribed(buffer, Encoder.ReadFormatCode(buffer)); Amqp.Message message = null; if (command.Descriptor.Code == FrameCodes.TRANSFER) { message = Amqp.Message.Decode(buffer); } return(testAmqpPeer.OnFrame(stream, channel, command, message)); }
public static bool?Decode(ByteBuffer buffer, FormatCode formatCode) { if (formatCode == 0 && (formatCode = AmqpEncoding.ReadFormatCode(buffer)) == FormatCode.Null) { return(null); } VerifyFormatCode(formatCode, buffer.Offset, FormatCode.Boolean, FormatCode.BooleanFalse, FormatCode.BooleanTrue); if (formatCode == FormatCode.Boolean) { return(AmqpBitConverter.ReadUByte(buffer) != 0); } else { return(formatCode == FormatCode.BooleanTrue ? true : false); } }
protected override void DecodeValue(ByteBuffer buffer) { var formatCode = AmqpCodec.DecodeFormatCode(buffer); if (formatCode == FormatCode.Null) { return; // nothing to decode } int size; int count; if (formatCode == FormatCode.List0) { size = count = 0; } else if (formatCode == FormatCode.List8) { size = AmqpBitConverter.ReadUByte(buffer); count = AmqpBitConverter.ReadUByte(buffer); } else if (formatCode == FormatCode.List32) { size = (int)AmqpBitConverter.ReadUInt(buffer); count = (int)AmqpBitConverter.ReadUInt(buffer); } else { throw new AmqpException(ErrorCode.DecodeError, $"The format code '{formatCode}' at frame buffer offset '{buffer.ReadOffset}' is invalid."); } var thisType = GetType(); for (int i = 0; i < count; i++) { var itemFormatCode = AmqpCodec.DecodeFormatCode(buffer); if (itemFormatCode == FormatCode.Null) { continue; // null value, ignore and continue } GetDecoderDelegate(thisType, i)(this, buffer, itemFormatCode); } }
public static ulong ReadULong(ByteBuffer buffer, byte formatCode) { if (formatCode == FormatCode.ULong0) { return(0); } else if (formatCode == FormatCode.SmallULong) { return(AmqpBitConverter.ReadUByte(buffer)); } else if (formatCode == FormatCode.ULong) { return(AmqpBitConverter.ReadULong(buffer)); } else { throw DecodeException(formatCode, buffer.Offset); } }
public static uint ReadUInt(ByteBuffer buffer, byte formatCode) { if (formatCode == FormatCode.UInt0) { return(0); } else if (formatCode == FormatCode.SmallUInt) { return(AmqpBitConverter.ReadUByte(buffer)); } else if (formatCode == FormatCode.UInt) { return(AmqpBitConverter.ReadUInt(buffer)); } else { throw DecodeException(formatCode, buffer.Offset); } }
public static Array ReadArray(ByteBuffer buffer, byte formatCode) { if (formatCode == FormatCode.Null) { return(null); } int size; int count; if (formatCode == FormatCode.Array8) { size = AmqpBitConverter.ReadUByte(buffer); count = AmqpBitConverter.ReadUByte(buffer); } else if (formatCode == FormatCode.Array32) { size = (int)AmqpBitConverter.ReadUInt(buffer); count = (int)AmqpBitConverter.ReadUInt(buffer); } else { throw DecodeException(formatCode, buffer.Offset); } formatCode = Encoder.ReadFormatCode(buffer); Serializer codec = GetSerializer(formatCode); if (codec == null) { throw DecodeException(formatCode, buffer.Offset); } Array value = Array.CreateInstance(codec.Type, count); IList list = value; for (int i = 0; i < count; ++i) { list[i] = codec.Decoder(buffer, formatCode); } return(value); }
public static ulong?Decode(ByteBuffer buffer, FormatCode formatCode) { if (formatCode == 0 && (formatCode = AmqpEncoding.ReadFormatCode(buffer)) == FormatCode.Null) { return(null); } VerifyFormatCode(formatCode, buffer.Offset, FormatCode.ULong, FormatCode.SmallULong, FormatCode.ULong0); if (formatCode == FormatCode.ULong0) { return(0); } else { return(formatCode == FormatCode.SmallULong ? AmqpBitConverter.ReadUByte(buffer) : AmqpBitConverter.ReadULong(buffer)); } }
private static void SkipDescribedList(ByteBuffer buffer) { // read the list length and move forward var listFormatCode = AmqpCodec.DecodeFormatCode(buffer); int size = 0; if (listFormatCode == FormatCode.List0) { size = 0; } else if (listFormatCode == FormatCode.List8) { size = AmqpBitConverter.ReadUByte(buffer); } else if (listFormatCode == FormatCode.List32) { size = (int)AmqpBitConverter.ReadUInt(buffer); } buffer.CompleteRead(size); }
public static bool ReadBoolean(ByteBuffer buffer, byte formatCode) { if (formatCode == FormatCode.BooleanTrue) { return(true); } else if (formatCode == FormatCode.BooleanFalse) { return(false); } else if (formatCode == FormatCode.Boolean) { byte data = AmqpBitConverter.ReadUByte(buffer); return(data != 0); } else { throw DecodeException(formatCode, buffer.Offset); } }
public void Decode(ByteBuffer buffer) { if (buffer.Length < this.EncodeSize) { throw new AmqpException(AmqpErrorCode.DecodeError, AmqpResources.GetString(AmqpResources.AmqpInsufficientBufferSize, this.EncodeSize, buffer.Length)); } uint prefix = AmqpBitConverter.ReadUInt(buffer); if (prefix != ProtocolHeader.AmqpPrefix) { throw new AmqpException(AmqpErrorCode.DecodeError, "ProtocolName" + prefix.ToString("X8")); } this.protocolId = (ProtocolId)AmqpBitConverter.ReadUByte(buffer); this.version = new AmqpVersion( AmqpBitConverter.ReadUByte(buffer), AmqpBitConverter.ReadUByte(buffer), AmqpBitConverter.ReadUByte(buffer)); }
public static ProtocolHeader Decode(ByteBuffer buffer) { if (buffer.Length < ProtocolHeader.Size) { throw AmqpEncoding.GetEncodingException("BufferSize"); } uint prefix = AmqpBitConverter.ReadUInt(buffer); if (prefix != ProtocolHeader.AmqpPrefix) { throw AmqpEncoding.GetEncodingException("ProtocolName"); } ProtocolHeader header = new ProtocolHeader(); header.protocolId = (ProtocolId)AmqpBitConverter.ReadUByte(buffer); header.version = new AmqpVersion(AmqpBitConverter.ReadUByte(buffer), AmqpBitConverter.ReadUByte(buffer), AmqpBitConverter.ReadUByte(buffer)); header.Buffer = new ArraySegment <byte>(buffer.Buffer, buffer.Offset - ProtocolHeader.Size, ProtocolHeader.Size); return(header); }
public static Map ReadMap(ByteBuffer buffer, byte formatCode) { if (formatCode == FormatCode.Null) { return(null); } int size; int count; if (formatCode == FormatCode.Map8) { size = AmqpBitConverter.ReadUByte(buffer); count = AmqpBitConverter.ReadUByte(buffer); } else if (formatCode == FormatCode.Map32) { size = (int)AmqpBitConverter.ReadUInt(buffer); count = (int)AmqpBitConverter.ReadUInt(buffer); } else { throw DecodeException(formatCode, buffer.Offset); } if (count % 2 > 0) { throw new AmqpException(ErrorCode.DecodeError, Fx.Format(SRAmqp.InvalidMapCount, count)); } Map value = new Map(); for (int i = 0; i < count; i += 2) { value.Add(ReadObject(buffer), ReadObject(buffer)); } return(value); }
public static List ReadList(ByteBuffer buffer, byte formatCode) { if (formatCode == FormatCode.Null) { return(null); } int size; int count; if (formatCode == FormatCode.List0) { size = count = 0; } else if (formatCode == FormatCode.List8) { size = AmqpBitConverter.ReadUByte(buffer); count = AmqpBitConverter.ReadUByte(buffer); } else if (formatCode == FormatCode.List32) { size = (int)AmqpBitConverter.ReadUInt(buffer); count = (int)AmqpBitConverter.ReadUInt(buffer); } else { throw DecodeException(formatCode, buffer.Offset); } List value = new List(); for (int i = 0; i < count; ++i) { value.Add(ReadObject(buffer)); } return(value); }
protected static void ReadSizeAndCount(ByteBuffer buffer, byte formatCode, out int size, out int count, out int width) { if (formatCode == FormatCode.List0) { size = count = width = 0; } else if (formatCode == FormatCode.List8 || formatCode == FormatCode.Map8) { width = FixedWidth.UByte; size = AmqpBitConverter.ReadUByte(buffer); count = AmqpBitConverter.ReadUByte(buffer); } else if (formatCode == FormatCode.List32 || formatCode == FormatCode.Map32) { width = FixedWidth.UInt; size = (int)AmqpBitConverter.ReadUInt(buffer); count = (int)AmqpBitConverter.ReadUInt(buffer); } else { throw new AmqpException(ErrorCode.InvalidField, Fx.Format(SRAmqp.AmqpInvalidFormatCode, formatCode, buffer.Offset)); } }
static Error ScanValueSection(byte formatCode, ByteBuffer buffer, int depth = 0) { if (formatCode == FormatCode.Described) { if (depth > 10) { // protection for stack overflow return(GetDecodeError(AmqpResources.GetString(Resources.AmqpInvalidFormatCode, formatCode, buffer.Offset))); } Error error = ScanValueSection(AmqpEncoding.ReadFormatCode(buffer), buffer, depth + 1); if (error != null) { return(error); } formatCode = AmqpEncoding.ReadFormatCode(buffer); } uint size; if (formatCode >= FormatCode.Binary8) { // variable width size = (formatCode & 0x10) == 0 ? AmqpBitConverter.ReadUByte(buffer) : AmqpBitConverter.ReadUInt(buffer); } else { // fixed width size = (uint)((1 << ((formatCode >> 4) - 4)) >> 1); } return(AdvanceBuffer(buffer, size)); }
public void TestMethod_AmqpBitConverter() { ByteBuffer buffer = new ByteBuffer(128, true); AmqpBitConverter.WriteByte(buffer, 0x22); AmqpBitConverter.WriteByte(buffer, -0x22); AmqpBitConverter.WriteUByte(buffer, 0x22); AmqpBitConverter.WriteUByte(buffer, 0xB2); AmqpBitConverter.WriteShort(buffer, 0x22B7); AmqpBitConverter.WriteShort(buffer, -0x22B7); AmqpBitConverter.WriteUShort(buffer, 0x22B7); AmqpBitConverter.WriteUShort(buffer, 0xC2B7); AmqpBitConverter.WriteInt(buffer, 0x340da287); AmqpBitConverter.WriteInt(buffer, -0x340da287); AmqpBitConverter.WriteUInt(buffer, 0x340da287); AmqpBitConverter.WriteUInt(buffer, 0xF40da287); AmqpBitConverter.WriteLong(buffer, 0x5d00BB9A340da287); AmqpBitConverter.WriteLong(buffer, -0x5d00BB9A340da287); AmqpBitConverter.WriteULong(buffer, 0x5d00BB9A340da287); AmqpBitConverter.WriteULong(buffer, 0xad00BB9A340da287); AmqpBitConverter.WriteFloat(buffer, 12344.4434F); AmqpBitConverter.WriteFloat(buffer, -12344.4434F); AmqpBitConverter.WriteDouble(buffer, 39432123244.44352334); AmqpBitConverter.WriteDouble(buffer, -39432123244.44352334); Guid uuid = Guid.NewGuid(); AmqpBitConverter.WriteUuid(buffer, uuid); sbyte b = AmqpBitConverter.ReadByte(buffer); sbyte b2 = AmqpBitConverter.ReadByte(buffer); byte ub = AmqpBitConverter.ReadUByte(buffer); byte ub2 = AmqpBitConverter.ReadUByte(buffer); short s = AmqpBitConverter.ReadShort(buffer); short s2 = AmqpBitConverter.ReadShort(buffer); ushort us = AmqpBitConverter.ReadUShort(buffer); ushort us2 = AmqpBitConverter.ReadUShort(buffer); int i = AmqpBitConverter.ReadInt(buffer); int i2 = AmqpBitConverter.ReadInt(buffer); uint ui = AmqpBitConverter.ReadUInt(buffer); uint ui2 = AmqpBitConverter.ReadUInt(buffer); long l = AmqpBitConverter.ReadLong(buffer); long l2 = AmqpBitConverter.ReadLong(buffer); ulong ul = AmqpBitConverter.ReadULong(buffer); ulong ul2 = AmqpBitConverter.ReadULong(buffer); float f = AmqpBitConverter.ReadFloat(buffer); float f2 = AmqpBitConverter.ReadFloat(buffer); double d = AmqpBitConverter.ReadDouble(buffer); double d2 = AmqpBitConverter.ReadDouble(buffer); Guid uuid2 = AmqpBitConverter.ReadUuid(buffer); }