public override void EncodeObject(object value, bool arrayEncoding, ByteBuffer buffer) { if (arrayEncoding) { ArraySegment<byte> binaryValue = (ArraySegment<byte>)value; AmqpBitConverter.WriteUInt(buffer, (uint)binaryValue.Count); buffer.WriteBytes(binaryValue.Array, binaryValue.Offset, binaryValue.Count); } else { BinaryEncoding.Encode((ArraySegment<byte>)value, buffer); } }
public static void Encode(ArraySegment<byte> value, ByteBuffer buffer) { if (value.Array == null) { AmqpEncoding.EncodeNull(buffer); } else { int width = AmqpEncoding.GetEncodeWidthBySize(value.Count); if (width == FixedWidth.UByte) { AmqpBitConverter.WriteUByte(buffer, (byte)FormatCode.Binary8); AmqpBitConverter.WriteUByte(buffer, (byte)value.Count); } else { AmqpBitConverter.WriteUByte(buffer, (byte)FormatCode.Binary32); AmqpBitConverter.WriteUInt(buffer, (uint)value.Count); } buffer.WriteBytes(value.Array, value.Offset, value.Count); } }
unsafe static void EncodeValue(decimal value, ByteBuffer buffer) { int[] bits = Decimal.GetBits(value); int lowSignificant = bits[0]; int middleSignificant = bits[1]; int highSignificant = bits[2]; int signAndExponent = bits[3]; byte[] bytes = new byte[FixedWidth.Decimal128]; byte *p = (byte*)&signAndExponent; int exponent = Decimal128Bias - p[2]; bytes[0] = p[3]; // sign bytes[0] |= (byte)(exponent >> 9); // 7 bits in msb bytes[1] = (byte)((exponent & 0x7F) << 1); // 7 bits in 2nd msb bytes[2] = 0; bytes[3] = 0; p = (byte*)&highSignificant; bytes[4] = p[3]; bytes[5] = p[2]; bytes[6] = p[1]; bytes[7] = p[0]; p = (byte*)&middleSignificant; bytes[8] = p[3]; bytes[9] = p[2]; bytes[10] = p[1]; bytes[11] = p[0]; p = (byte*)&lowSignificant; bytes[12] = p[3]; bytes[13] = p[2]; bytes[14] = p[1]; bytes[15] = p[0]; buffer.WriteBytes(bytes, 0, bytes.Length); }
void PatchRawSection(ByteBuffer buffer, MetadataBuilder metadata) { var position = base.position; Align(4); buffer.WriteBytes(base.position - position); const byte fat_format = 0x40; const byte more_sects = 0x80; var flags = ReadByte(); if ((flags & fat_format) == 0) { buffer.WriteByte(flags); PatchRawSmallSection(buffer, metadata); } else PatchRawFatSection(buffer, metadata); if ((flags & more_sects) != 0) PatchRawSection(buffer, metadata); }
void PatchRawExceptionHandlers(ByteBuffer buffer, MetadataBuilder metadata, int count, bool fat_entry) { const int fat_entry_size = 16; const int small_entry_size = 6; for (int i = 0; i < count; i++) { ExceptionHandlerType handler_type; if (fat_entry) { var type = ReadUInt32(); handler_type = (ExceptionHandlerType)(type & 0x7); buffer.WriteUInt32(type); } else { var type = ReadUInt16(); handler_type = (ExceptionHandlerType)(type & 0x7); buffer.WriteUInt16(type); } buffer.WriteBytes(ReadBytes(fat_entry ? fat_entry_size : small_entry_size)); switch (handler_type) { case ExceptionHandlerType.Catch: var exception = reader.LookupToken(ReadToken()); buffer.WriteUInt32(metadata.LookupToken(exception).ToUInt32()); break; default: buffer.WriteUInt32(ReadUInt32()); break; } } }
void PatchRawCode(ByteBuffer buffer, int code_size, CodeWriter writer) { var metadata = writer.metadata; buffer.WriteBytes(ReadBytes(code_size)); var end = buffer.position; buffer.position -= code_size; while (buffer.position < end) { OpCode opcode; var il_opcode = buffer.ReadByte(); if (il_opcode != 0xfe) { opcode = OpCodes.OneByteOpCode[il_opcode]; } else { var il_opcode2 = buffer.ReadByte(); opcode = OpCodes.TwoBytesOpCode[il_opcode2]; } switch (opcode.OperandType) { case OperandType.ShortInlineI: case OperandType.ShortInlineBrTarget: case OperandType.ShortInlineVar: case OperandType.ShortInlineArg: buffer.position += 1; break; case OperandType.InlineVar: case OperandType.InlineArg: buffer.position += 2; break; case OperandType.InlineBrTarget: case OperandType.ShortInlineR: case OperandType.InlineI: buffer.position += 4; break; case OperandType.InlineI8: case OperandType.InlineR: buffer.position += 8; break; case OperandType.InlineSwitch: var length = buffer.ReadInt32(); buffer.position += length * 4; break; case OperandType.InlineString: var @string = GetString(new MetadataToken(buffer.ReadUInt32())); buffer.position -= 4; buffer.WriteUInt32( new MetadataToken( TokenType.String, metadata.user_string_heap.GetStringIndex(@string)).ToUInt32()); break; case OperandType.InlineSig: var call_site = GetCallSite(new MetadataToken(buffer.ReadUInt32())); buffer.position -= 4; buffer.WriteUInt32(writer.GetStandAloneSignature(call_site).ToUInt32()); break; case OperandType.InlineTok: case OperandType.InlineType: case OperandType.InlineMethod: case OperandType.InlineField: var provider = reader.LookupToken(new MetadataToken(buffer.ReadUInt32())); buffer.position -= 4; buffer.WriteUInt32(metadata.LookupToken(provider).ToUInt32()); break; } } }