Ejemplo n.º 1
0
Archivo: RLP.cs Proyecto: zutobg/Meadow
        private static RLPList DecodeList(byte[] data, int start, out int end)
        {
            // Create a new list representation.
            RLPList rlpList = new RLPList();

            int current = 0;

            // If it's less than 0xf7 than it's a list constituting 0-55 bytes total.
            if (data[start] < 0xf7)
            {
                // Obtain the length of our data.
                int length = data[start] - 0xc0;

                // Calculate the end of our RLP item.
                end = start + 1 + length;

                // Keep adding items until we reach the end.
                current = start + 1;
                while (current < end)
                {
                    rlpList.Items.Add(DecodeAt(data, current, out current));
                }
            }
            else
            {
                // Obtain the length of the length
                int lengthLength = data[start] - 0xf7;

                // Obtain our length
                int length = DecodeLength(data, start + 1, lengthLength);

                // Calculate the end of our RLP item.
                end = start + 1 + lengthLength + length;

                // Keep adding items until we reach the end.
                current = start + 1 + lengthLength;
                while (current < end)
                {
                    rlpList.Items.Add(DecodeAt(data, current, out current));
                }
            }

            // Verify our decoding added at our predicted end.
            if (current != end)
            {
                throw new ArgumentException("RLP deserialization encountered an error where travering an encoded list does not add up to the stored length.");
            }

            return(rlpList);
        }
Ejemplo n.º 2
0
Archivo: RLP.cs Proyecto: zutobg/Meadow
        private static byte[] EncodeList(RLPList rlpList)
        {
            // Obtain all the encoded items and total length.
            int length = 0;

            byte[][] data = new byte[rlpList.Items.Count][];
            for (int i = 0; i < data.Length; i++)
            {
                data[i] = Encode(rlpList.Items[i]);
                length += data[i].Length;
            }

            // If it's between 0 and 55 bytes, we add a prefix that denotes length.
            if (length < 55)
            {
                return(new byte[] { (byte)(0xC0 + length) }.Concat(data));
            }

            // If it's more, we'll want to get the length of the data (as a possibly large integer)
            byte[] lengthData = EncodeLength(length);

            // Return an array with our data
            return(new[] { (byte)(0xf7 + lengthData.Length) }.Concat(lengthData).Concat(data));
        }