Exemple #1
0
 public void ReadWrite(BitcoinStream stream)
 {
     stream.ReadWrite(ref _value);
 }
 public void ReadWrite(BitcoinStream stream)
 {
     stream.ReadWriteAsVarString(ref _Data);
 }
        internal byte[] ReadData(Stream stream)
        {
            uint          len       = 0;
            BitcoinStream bitStream = new BitcoinStream(stream, false);

            if (Code == 0)
            {
                return(new byte[0]);
            }

            if ((byte)OpcodeType.OP_1 <= (byte)Code && (byte)Code <= (byte)OpcodeType.OP_16)
            {
                return(new byte[] { (byte)(Code - OpcodeType.OP_1 + 1) });
            }

            if (Code == OpcodeType.OP_1NEGATE)
            {
                return(new byte[] { 0x81 });
            }

            try
            {
                if (0x01 <= (byte)Code && (byte)Code <= 0x4b)
                {
                    len = (uint)Code;
                }
                else if (Code == OpcodeType.OP_PUSHDATA1)
                {
                    len = bitStream.ReadWrite((byte)0);
                }
                else if (Code == OpcodeType.OP_PUSHDATA2)
                {
                    len = bitStream.ReadWrite((ushort)0);
                }
                else if (Code == OpcodeType.OP_PUSHDATA4)
                {
                    len = bitStream.ReadWrite((uint)0);
                }
                else
                {
                    IsInvalid = true;
                    return(new byte[0]);
                }


                byte[] data = null;

                if (len <= MAX_SCRIPT_ELEMENT_SIZE)                //Most of the time
                {
                    data = new byte[len];
                    var readen = stream.Read(data, 0, data.Length);
                    if (readen != data.Length)
                    {
                        IsInvalid = true;
                        return(new byte[0]);
                    }
                }
                else                 //Mitigate against a big array allocation
                {
                    List <byte> bytes = new List <byte>();
                    for (int i = 0; i < len; i++)
                    {
                        var b = stream.ReadByte();
                        if (b < 0)
                        {
                            IsInvalid = true;
                            return(new byte[0]);
                        }
                        bytes.Add((byte)b);
                    }
                    data = bytes.ToArray();
                }
                return(data);
            }
            catch (EndOfStreamException)
            {
                IsInvalid = true;
                return(new byte[0]);
            }
        }
 public void ReadWrite(BitcoinStream stream)
 {
     stream.ReadWrite(ref vHave);
 }