public static sbyte ReadSByte(this MemoryStream stream)
        {
            var res = BStruct.ToByte(stream.GetBuffer(), (int)stream.Position);

            stream.Position += 1;
            return(res);
        }
        public static double ReadDouble(this MemoryStream stream)
        {
            var res = BStruct.ToDouble(stream.GetBuffer(), (int)stream.Position);

            stream.Position += 8;
            return(res);
        }
Пример #3
0
        private static object DecodeDouble(string x, int startIndex, out int endIndex)
        {
            startIndex += 1;
            endIndex    = startIndex + 8;

            return(BStruct.ToDouble(Util.StringBytes(x.Substring(startIndex, 8)), 0));
        }
        public static int ReadInt32(this MemoryStream stream)
        {
            var res = BStruct.ToInt32(stream.GetBuffer(), (int)stream.Position);

            stream.Position += 4;
            return(res);
        }
Пример #5
0
        private static object DecodeFloat(string x, int startIndex, out int endIndex)
        {
            startIndex += 1;
            endIndex    = startIndex + 4;

            return(BStruct.ToFloat(Util.StringBytes(x.Substring(startIndex, 4)), 0));
        }
Пример #6
0
        private static object DecodeInt2(string x, int startIndex, out int endIndex)
        {
            startIndex += 1;
            endIndex    = startIndex + 2;

            return(BStruct.ToInt2(Util.StringBytes(x.Substring(startIndex, 2)), 0));
        }
        public static long ReadInt64(this MemoryStream stream)
        {
            var res = BStruct.ToInt64(stream.GetBuffer(), (int)stream.Position);

            stream.Position += 8;
            return(res);
        }
        public static float ReadSingle(this MemoryStream stream)
        {
            var res = BStruct.ToFloat(stream.GetBuffer(), (int)stream.Position);

            stream.Position += 4;
            return(res);
        }
Пример #9
0
        /// <summary>
        /// Unpack the string 'x' (network order byte format) into object.
        /// </summary>
        public static object Unpack(string x, int n)
        {
            x = Util.StringPad(x, n);

            byte[] b = new byte[n];
            for (int i = 0; i < x.Length; i++)
            {
                b[i] = (byte)x[i];
            }

            if (b.Length == 1)
            {
                return(BStruct.ToInt1(b, 0));
            }
            if (b.Length == 2)
            {
                return(BStruct.ToInt2(b, 0));
            }
            if (b.Length == 4)
            {
                return(BStruct.ToInt4(b, 0));
            }
            if (b.Length == 8)
            {
                return(BStruct.ToInt8(b, 0));
            }
            return(null);
        }
Пример #10
0
        public float ReadSingle()
        {
            ReadTypeCode(RencodeTypeCode.Single);

            var buffer = new byte[4];

            stream.Read(buffer, 0, buffer.Length);

            return(BStruct.ToFloat(buffer, 0));
        }
Пример #11
0
        public long ReadInt64()
        {
            ReadTypeCode(RencodeTypeCode.Int64);

            var buffer = new byte[8];

            stream.Read(buffer, 0, buffer.Length);

            return(BStruct.ToInt64(buffer, 0));
        }
Пример #12
0
        public int ReadInt32()
        {
            ReadTypeCode(RencodeTypeCode.Int32);

            var buffer = new byte[4];

            stream.Read(buffer, 0, buffer.Length);

            return(BStruct.ToInt32(buffer, 0));
        }
Пример #13
0
        public short ReadInt16()
        {
            ReadTypeCode(RencodeTypeCode.Int16);

            var buffer = new byte[2];

            stream.Read(buffer, 0, buffer.Length);

            return(BStruct.ToInt16(buffer, 0));
        }
Пример #14
0
        public double ReadDouble()
        {
            ReadTypeCode(RencodeTypeCode.Double);

            var buffer = new byte[8];

            stream.Read(buffer, 0, buffer.Length);

            return(BStruct.ToDouble(buffer, 0));
        }
Пример #15
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);
            }
        }
Пример #16
0
        private static void EncodeInt(object x, List <object> dest)
        {
            // Check to determine if long type is able
            // to be packed inside an Int32 or is actually
            // an Int64 value.
            bool isLong = x is long && (((long)x) > int.MaxValue || ((long)x) < int.MinValue);

            if (!isLong && 0 <= (int)x && (int)x < RencodeConst.INT_POS_FIXED_COUNT)
            {
                dest.Add((char)(RencodeConst.INT_POS_FIXED_START + (int)x));
            }
            else if (!isLong && -RencodeConst.INT_NEG_FIXED_COUNT <= (int)x && (int)x < 0)
            {
                dest.Add((char)(RencodeConst.INT_NEG_FIXED_START - 1 - (int)x));
            }
            else if (!isLong && -128 <= (int)x && (int)x < 128)
            {
                dest.Add(RencodeConst.CHR_INT1);
                dest.Add(BStruct.Pack(x, 1));
            }
            else if (!isLong && -32768 <= (int)x && (int)x < 32768)
            {
                dest.Add(RencodeConst.CHR_INT2);
                dest.Add(BStruct.Pack(x, 2));
            }
            else if (-2147483648L <= Convert.ToInt64(x) && Convert.ToInt64(x) < 2147483648L)
            {
                dest.Add(RencodeConst.CHR_INT4);
                dest.Add(BStruct.Pack(x, 4));
            }
            else if (-9223372036854775808L < Convert.ToInt64(x) && Convert.ToInt64(x) < 9223372036854775807L)
            {
                dest.Add(RencodeConst.CHR_INT8);
                dest.Add(BStruct.Pack(x, 8));
            }
            else
            {
                string s = (string)x;
                if (s.Length >= RencodeConst.MAX_INT_LENGTH)
                {
                    throw new ArgumentOutOfRangeException();
                }
                dest.Add(RencodeConst.CHR_INT);
                dest.Add(s);
                dest.Add(RencodeConst.CHR_TERM);
            }
        }
Пример #17
0
 public void Write(double value)
 {
     stream.WriteByte((byte)RencodeTypeCode.Double);
     byte[] bytes = BStruct.Pack(value);
     stream.Write(bytes, 0, bytes.Length);
 }
Пример #18
0
 private static void EncodeDouble(object x, List <object> dest)
 {
     dest.Add(RencodeConst.CHR_FLOAT64);
     dest.Add(BStruct.Pack(x, 8));
 }
Пример #19
0
 private static void EncodeFloat(object x, List <object> dest)
 {
     dest.Add(RencodeConst.CHR_FLOAT32);
     dest.Add(BStruct.Pack(x, 4));
 }
 public static void Write(this MemoryStream stream, double x) => stream.Write(BStruct.Pack(x));