public static IList <RLPItem> Decode(byte[] input) { var message = new RLPMessage(input); while (message.Remainder.Offset < input.Length) { Decode(message); } return(message.Data); }
private static void Decode(RLPMessage msg) { var firstByte = Convert.ToInt16(msg.Remainder.Array[msg.Remainder.Offset]); // single byte if (firstByte <= 0x7f) { msg.Data.Add(new RLPItem(new[] { msg.Remainder.Array[msg.Remainder.Offset] })); msg.Remainder = msg.Remainder.Slice(1); return; } // string 0-55 bytes if (firstByte <= 0xb7) { var itemLength = Math.Abs(128 - firstByte); var data = firstByte == 0x80 ? new ArraySegment <byte>(new byte[0]) : msg.Remainder.Slice(1, itemLength); msg.Data.Add(new RLPItem(data.ToArray())); msg.Remainder = msg.Remainder.Slice(data.Count + 1); return; } // string 56-255 bytes if (firstByte <= 0xbf) { var listLength = Math.Abs(183 - firstByte); var itemLength = Convert.ToInt16(msg.Remainder.Array[msg.Remainder.Offset + 1]); var data = msg.Remainder.Slice(listLength + 1, itemLength); msg.Data.Add(new RLPItem(data.ToArray())); msg.Remainder = msg.Remainder.Slice(data.Offset + data.Count); return; } // collection 0-55 bytes if (firstByte <= 0xf7) { var itemLength = Math.Abs(192 - firstByte); var data = msg.Remainder.Slice(1, itemLength).ToArray(); while (msg.Remainder.Offset < msg.Remainder.Array.Length) { var decoded = Decode(data); msg.Data.AddRange(decoded); msg.Remainder = msg.Remainder.Slice(msg.Remainder.Count); } return; } // collection 56-255 bytes if (firstByte <= 0xff) { var listLength = Math.Abs(247 - firstByte); var itemLength = Convert.ToInt16(msg.Remainder.Array[msg.Remainder.Offset + 1]); var data = msg.Remainder.Slice(listLength + 1, itemLength).ToArray(); while (msg.Remainder.Offset < msg.Remainder.Array.Length) { var decoded = Decode(data); msg.Data.AddRange(decoded); msg.Remainder = msg.Remainder.Slice(msg.Remainder.Count); } return; } throw new ArgumentOutOfRangeException("msg", "msg is too long"); }
private static void Decode(RLPMessage msg) { var firstByte = Convert.ToInt16(msg.Remainder.Array[msg.Remainder.Offset]); // single byte if (firstByte <= 0x7f) { msg.Decoded.Add(firstByte.ToString(CultureInfo.InvariantCulture)); msg.Remainder = msg.Remainder.Slice(1); return; } // string <55 bytes if (firstByte <= 0xb7) { var itemLength = Math.Abs(128 - firstByte); var data = firstByte == 0x80 ? new ArraySegment <byte>(new byte[0]) : msg.Remainder.Slice(1, itemLength); msg.Decoded.Add(System.Text.Encoding.ASCII.GetString(data.Array, data.Offset, data.Count)); msg.Remainder = msg.Remainder.Slice(data.Count + 1); return; } // string >55 bytes if (firstByte <= 0xbf) { var listLength = Math.Abs(183 - firstByte); var itemLength = Convert.ToInt16(msg.Remainder.Array[msg.Remainder.Offset + 1]); var data = msg.Remainder.Slice(listLength + 1, itemLength); msg.Decoded.Add(System.Text.Encoding.ASCII.GetString(msg.Remainder.Array, data.Offset, data.Count)); msg.Remainder = msg.Remainder.Slice(data.Offset + data.Count); return; } // collection <55 bytes if (firstByte <= 0xf7) { var itemLength = Math.Abs(192 - firstByte); var data = msg.Remainder.Slice(1, itemLength).ToArray(); while (msg.Remainder.Offset < msg.Remainder.Array.Length) { var decoded = Decode(data); msg.Decoded.AddRange(decoded); msg.Remainder = msg.Remainder.Slice(msg.Remainder.Count); } return; } // collection >55 bytes if (firstByte <= 0xff) { var listLength = Math.Abs(247 - firstByte); var itemLength = Convert.ToInt16(msg.Remainder.Array[msg.Remainder.Offset + 1]); var data = msg.Remainder.Slice(listLength + 1, itemLength).ToArray(); while (msg.Remainder.Offset < msg.Remainder.Array.Length) { var decoded = Decode(data); msg.Decoded.AddRange(decoded); msg.Remainder = msg.Remainder.Slice(msg.Remainder.Count); } return; } throw new ArgumentOutOfRangeException("msg", "msg is too long"); }