Esempio n. 1
0
        internal static bool TryParseInternal(ReadOnlySpan <byte> bEncodedObject, ref int offset, ref BObject bInteger)
        {
            int availableBytes = bEncodedObject.Length - offset;

            int  length   = 0;
            bool negative = false;

            while (true)
            {
                if (availableBytes-- == 0)
                {
                    return(false);
                }
                byte b = bEncodedObject[offset++];

                if (b == NegativeByte)
                {
                    if (negative)
                    {
                        return(false);
                    }
                    if (length != 0)
                    {
                        return(false);
                    }
                    negative = true;
                }
                else if (b == EndByte)
                {
                    break;
                }
                else if (b >= ZeroByte && b <= NineByte)
                {
                    if (length++ >= MaxLength)
                    {
                        return(false);
                    }
                }
                else
                {
                    return(false);
                }
            }

            long value = 0;

            for (int i = 0; i < length; i++)
            {
                value += (bEncodedObject[offset - 2 - i] - ZeroByte) * PowersOfTen[i];
            }
            if (negative)
            {
                value = -value;
            }

            if (negative && value == 0)
            {
                return(false);
            }
            bInteger = new BInteger(value);
            return(true);
        }
Esempio n. 2
0
        internal static bool TryParseInternal(ReadOnlySpan <byte> bEncodedObject, ref int offset, ref BObject bString)
        {
            int availableBytes = bEncodedObject.Length - offset;

            int lengthBytes = 0;

            while (true)
            {
                if (availableBytes-- == 0)
                {
                    return(false);
                }
                byte b = bEncodedObject[offset++];

                if (b >= ZeroByte && b <= NineByte)
                {
                    if (lengthBytes++ >= MaxLengthBytes)
                    {
                        return(false);
                    }
                }
                else if (b == LengthSeparatorByte)
                {
                    break;
                }
                else
                {
                    return(false);
                }
            }

            int length = 0;

            for (int i = 0; i < lengthBytes; i++)
            {
                length += (bEncodedObject[offset - 2 - i] - ZeroByte) * PowersOfTen[i];
            }

            if (availableBytes < length)
            {
                return(false);
            }

            byte[] bytes = bEncodedObject.Slice(offset, length).ToArray();
            offset += length;
            bString = new BString(bytes);
            return(true);
        }