public static int GetUpperBound(this RencodeTypeCode @this)
        {
            switch (@this)
            {
            case RencodeTypeCode.String:
                return(RencodeConst.StringTypeCount);

            case RencodeTypeCode.EncodedLengthString:
                return(RencodeConst.EncodedLengthStringMaxLength);

            case RencodeTypeCode.EncodedPositiveInteger:
                return(RencodeConst.EncodedPositiveIntegerUpperBound);

            case RencodeTypeCode.EncodedNegativeInteger:
                return(RencodeConst.EncodedNegativeIntegerUpperBound);

            case RencodeTypeCode.EncodedLengthList:
                return(RencodeConst.EncodedLengthListMaxLength);

            case RencodeTypeCode.EncodedLengthDictionary:
                return(RencodeConst.EncodedLengthDictionaryMaxLength);
            }

            throw new InvalidOperationException($"{@this} does not have an upper bound");
        }
Esempio n. 2
0
        public void ReaderPeekTypecode()
        {
            using var stringStream = new MemoryStream(Util.GetBytes("0:1: 2:  3:   4:    5:     6:      7:       8:        9:         "));
            using var stringReader = new RencodeReader(stringStream);
            RencodeTypeCode typeCode;

            while ((typeCode = stringReader.PeekTypeCode()) != RencodeTypeCode.EndOfStream)
            {
                Assert.AreEqual(RencodeTypeCode.String, typeCode);
                stringReader.ReadString();
            }

            var strings = new string[] {
                (char)RencodeTypeCode.SByte + "\x78",
                (char)RencodeTypeCode.Int16 + "\x06\x04",
                (char)RencodeTypeCode.Int32 + "\x7f\xff\xff\xd0",
                (char)RencodeTypeCode.Int64 + "\x7F\xFF\xFF\xFF\xFF\xFE\xD7\xE0"
            };

            var s = string.Concat(strings);

            using var intStream = new MemoryStream(Util.GetBytes(s));
            using var intReader = new RencodeReader(intStream);
            int i = 0;

            while ((typeCode = intReader.PeekTypeCode()) != RencodeTypeCode.EndOfStream)
            {
                RencodeTypeCode expectedTypeCode = (RencodeTypeCode)strings[i++][0];
                Assert.AreEqual(expectedTypeCode, typeCode);
                intReader.Read();
            }
        }
Esempio n. 3
0
        private RencodeTypeCode ReadTypeCode(RencodeTypeCode expected)
        {
            RencodeTypeCode typeCode = ReadTypeCode();

            if (typeCode == expected)
            {
                return(typeCode);
            }

            if (typeCode == RencodeTypeCode.EndOfStream)
            {
                throw new EndOfStreamException();
            }

            throw new FormatException("The stream is corrupted");
        }
        public static bool IsWithinBounds(this RencodeTypeCode @this, int value)
        {
            int start = (int)@this;

            return(value >= start && value < start + @this.GetUpperBound());
        }