Exemplo n.º 1
0
        public static object Decode(ReadOnlySpan <char> x)
        {
            int         byteCount = Util.GetByteCount(x);
            Span <byte> buffer    = new byte[byteCount];

            Util.GetBytes(x, buffer);
            return(Decode(buffer));
        }
Exemplo n.º 2
0
        public void Write(BigInteger value)
        {
            // Check to determine if long type is able
            // to be packed inside an Int32 or is actually
            // an Int64 value.
            bool isLong = (value > int.MaxValue || value < int.MinValue);

            if (!isLong && value >= 0 && value < RencodeConst.EncodedPositiveIntegerUpperBound)
            {
                stream.WriteByte((byte)((int)RencodeTypeCode.EncodedPositiveInteger + value));
            }
            else if (!isLong && value >= -RencodeConst.EncodedNegativeIntegerUpperBound && value < 0)
            {
                stream.WriteByte((byte)((int)RencodeTypeCode.EncodedNegativeInteger - 1 - value));
            }
            else if (!isLong && value >= sbyte.MinValue && value < sbyte.MaxValue)
            {
                stream.WriteByte((byte)RencodeTypeCode.SByte);
                stream.WriteByte((byte)(sbyte)value);
            }
            else if (!isLong && value >= short.MinValue && value < short.MaxValue)
            {
                stream.WriteByte((byte)RencodeTypeCode.Int16);
                byte[] bytes = BStruct.Pack((short)value);
                stream.Write(bytes, 0, bytes.Length);
            }
            else if (value >= int.MinValue && value < int.MaxValue)
            {
                stream.WriteByte((byte)RencodeTypeCode.Int32);
                byte[] bytes = BStruct.Pack((int)value);
                stream.Write(bytes, 0, bytes.Length);
            }
            else if (value >= long.MinValue && value <= long.MaxValue)
            {
                stream.WriteByte((byte)RencodeTypeCode.Int64);
                byte[] bytes = BStruct.Pack((long)value);
                stream.Write(bytes, 0, bytes.Length);
            }
            else
            {
                string s = value.ToString(System.Globalization.CultureInfo.InvariantCulture);
                if (s.Length >= RencodeConst.MaxBase10IntegerStringLength)
                {
                    throw new ArgumentOutOfRangeException();
                }

                stream.WriteByte((byte)RencodeTypeCode.BigInteger);

                byte[] encoded = Util.GetBytes(s);
                stream.Write(encoded, 0, encoded.Length);

                stream.WriteByte((byte)RencodeTypeCode.Termination);
            }
        }
Exemplo n.º 3
0
        public void Write(string str)
        {
            if (str.Length < RencodeConst.EncodedLengthStringMaxLength)
            {
                stream.WriteByte((byte)(RencodeTypeCode.EncodedLengthString + str.Length));

                byte[] bytes = Util.GetBytes(str);

                stream.Write(bytes, 0, bytes.Length);
            }
            else
            {
                byte[] bytes = Util.GetBytes(str.Length.ToString());
                stream.Write(bytes, 0, bytes.Length);

                stream.WriteByte((byte)':');

                bytes = Util.GetBytes(str);
                stream.Write(bytes, 0, bytes.Length);
            }
        }
 public static void Write(this MemoryStream stream, string x) => stream.Write(Util.GetBytes(x));
Exemplo n.º 5
0
        /// <summary>
        /// Decode rencode string 'x' into object.
        /// </summary>
        public static object Decode(string x)
        {
            var bytes = Util.GetBytes(x);

            return(Decode(bytes));
        }