コード例 #1
0
ファイル: MDer.cs プロジェクト: modulexcite/DDer
    static AsnElt BuildInteger(string iv)
    {
        /*
         * If the string can be parsed as a 64-bit integer (signed
         * or unsigned) then we can encoded it right away.
         */
        long v;

        if (Int64.TryParse(iv, out v))
        {
            return(AsnElt.MakeInteger(v));
        }
        ulong uv;

        if (UInt64.TryParse(iv, out uv))
        {
            return(AsnElt.MakeInteger(uv));
        }

        /*
         * For longer values we need ZInt.
         */
        try {
            ZInt z = ZInt.Parse(iv);
            return(AsnElt.MakePrimitive(
                       AsnElt.INTEGER, z.ToBytesBE()));
        } catch {
            throw new IOException(
                      "could not convert value to integer: " + iv);
        }
    }
コード例 #2
0
    /*
     * Interpret a token as a constant value (numerical constant,
     * boolean, literal string). If the token is not such a constant,
     * the returned value is uninitialized.
     */
    internal static XValue ParseConst(string t)
    {
        if (t.Length == 0)
        {
            return(new XValue((XObject)null));
        }
        if (t == "true")
        {
            return(new XValue(XType.BOOL, 1));
        }
        if (t == "false")
        {
            return(new XValue(XType.BOOL, 0));
        }
        if (t[0] == '"')
        {
            return(new XValue(t.Substring(1)));
        }
        if (t[0] == '`')
        {
            int cp = t[1];
            if (cp > 0x7F)
            {
                throw new Exception("non-ASCII character constant");
            }
            return((byte)cp);
        }
        bool neg = false;

        if (t[0] == '+')
        {
            t = t.Substring(1);
        }
        else if (t[0] == '-')
        {
            neg = true;
            t   = t.Substring(1);
        }
        if (t.Length == 0 || t[0] < '0' || t[0] > '9')
        {
            return(new XValue((XObject)null));
        }

        XType bt  = XType.INT;
        ZInt  min = Int32.MinValue;
        ZInt  max = Int32.MaxValue;

        if (t.EndsWith("u8") || t.EndsWith("U8"))
        {
            t   = t.Substring(0, t.Length - 2);
            bt  = XType.U8;
            min = 0;
            max = Byte.MaxValue;
        }
        else if (t.EndsWith("u16") || t.EndsWith("U16"))
        {
            t   = t.Substring(0, t.Length - 3);
            bt  = XType.U16;
            min = 0;
            max = UInt16.MaxValue;
        }
        else if (t.EndsWith("u32") || t.EndsWith("U32"))
        {
            t   = t.Substring(0, t.Length - 3);
            bt  = XType.U32;
            min = 0;
            max = UInt32.MaxValue;
        }
        else if (t.EndsWith("u64") || t.EndsWith("U64"))
        {
            t   = t.Substring(0, t.Length - 3);
            bt  = XType.U64;
            min = 0;
            max = UInt64.MaxValue;
        }
        else if (t.EndsWith("i8") || t.EndsWith("I8"))
        {
            t   = t.Substring(0, t.Length - 2);
            bt  = XType.I8;
            min = SByte.MinValue;
            max = SByte.MaxValue;
        }
        else if (t.EndsWith("i16") || t.EndsWith("I16"))
        {
            t   = t.Substring(0, t.Length - 3);
            bt  = XType.I16;
            min = Int16.MinValue;
            max = Int16.MaxValue;
        }
        else if (t.EndsWith("i32") || t.EndsWith("I32"))
        {
            t   = t.Substring(0, t.Length - 3);
            bt  = XType.I32;
            min = Int32.MinValue;
            max = Int32.MaxValue;
        }
        else if (t.EndsWith("i64") || t.EndsWith("I64"))
        {
            t   = t.Substring(0, t.Length - 3);
            bt  = XType.I64;
            min = Int64.MinValue;
            max = Int64.MaxValue;
        }

        ZInt x = ZInt.Parse(t);

        if (neg)
        {
            x = -x;
        }
        if (x < min || x > max)
        {
            throw new Exception(string.Format("value {0} is out of allowed range for type {1}", x, bt.Name));
        }

        return(new XValue(bt, x.ToULong));
    }