Пример #1
0
 public override object Call0(Class last_class, object recv, Frame caller, Proc block)
 {
     Match md = (Match) recv;
     String str = new String(md.matched.Substring(md.value.Index + md.value.Length));
     if (md.Tainted) str.Tainted = true;
     return str;
 }
Пример #2
0
        internal static String env_str_new(Frame caller, string str)
        {
            String s = new String(str);
            s.Tainted = true;
            Object.rb_obj_freeze(caller, s);

            return s;
        }
Пример #3
0
 public override object Call1(Class last_class, object recv, Frame caller, Proc block, object param0)
 {
     String str = new String(((String)recv).value + String.StringValue(param0, caller));
     if (((recv is Basic) && (((Basic)recv).Tainted)) ||
         ((param0 is Basic) && (((Basic)param0).Tainted)))
         str.Tainted = true;
     return str;
 }
Пример #4
0
 public override object Calln(Class last_class, object recv, Frame caller, ArgList args)
 {
     if (args.Length >= 1)
     {
         string path = String.StringValue(args[0], caller);
         if (path.StartsWith("|"))
         {
             String newpath = new String(path.Remove(0, 1));
             args[0] = newpath;
             return rb_io_s_popen.singleton.Calln(last_class, Ruby.Runtime.Init.rb_cIO, caller, args);
         }
     }
     return rb_io_s_open.singleton.Calln(last_class, Ruby.Runtime.Init.rb_cFile, caller, args);
 }
Пример #5
0
        public override object Call1(Class klass, object recv, Frame caller, Proc block, object p1)
        {
            string name = String.StringValue(p1, caller);
            string val = System.Environment.GetEnvironmentVariable(name);

            if (val == null)
                return null;
            else if (Env.PATH_ENV.Equals(name, System.StringComparison.OrdinalIgnoreCase) && !Env.rb_env_path_tainted())
            {
                String s = new String(val);
                Object.rb_obj_freeze(caller, s);
                return s;
            }
            else
                return Env.env_str_new2(caller, val);
        }
Пример #6
0
        public override object Call0(Class last_class, object sym, Frame caller, Proc block)
        {
            String str;
            string name;
            uint id = ((Symbol)sym).id_new;           

            name = Symbol.rb_id2name(id);
            str = new String();
            str.value = ':' + name;            
            if (Symbol.is_junk_id(id))
            {
                str = (String)Methods.rb_str_dump.singleton.Call0(last_class, str, caller, block);
                ((String)str).value = ":\"" + ((String)str).value.Substring(2);
            }
            return str;

            //return new String(":" + ((Symbol)recv).id);
        }
Пример #7
0
        internal static int Compare(Frame caller, String x, String y)
        {
            string s1 = String.StringValue(x, caller);
            string s2 = String.StringValue(y, caller);

            for (int i = 0; i < s1.Length && i < s2.Length; i++)
            {
                if (s1[i] > s2[i])
                    return 1;
                else if (s1[i] < s2[i])
                    return -1;
            }

            if (s1.Length == s2.Length)
                return 0;
            else if (s1.Length > s2.Length)
                return 1;
            else
                return -1;
        }
Пример #8
0
        public override object Call0(Class last_class, object recv, Frame caller, Proc block)
        {
            File file = (File)recv;

            File.rb_io_check_initialized(caller, file);

            if (file._path == null)
                return null;
            else
            {
                String result = new String(file._path);
                result.Tainted = true;
                return result;
            }
        }
Пример #9
0
        public override object Call1(Class last_class, object recv, Frame caller, Proc block, object p1)
        {
            String str = (String)recv;
            String salt = String.RStringValue(p1, caller);

            string s;

            if (salt.value.Length < 2)
                throw new ArgumentError("salt too short(need >=2 bytes)").raise(caller);

            if (str.value != null)
                s = str.value;
            else
                s = "";

            String result = new String(Crypt.crypt(salt.value, s));
            result.Tainted |= str.Tainted;
            result.Tainted |= salt.Tainted;
            return result;
        }
Пример #10
0
        public override object Call0(Class last_class, object recv, Frame caller, Proc block)
        {
            ArrayList ary = ((Array)recv).value;

            if (ary.Count == 0)
                return new String("[]");
            else if (Array.IsInspecting(recv))
                return new String("[...]");
            else
            {
                try
                {
                    Array.StartInspect(recv);
                    bool tainted = ((Array)recv).Tainted;
                    StringBuilder sb = new StringBuilder();
                    sb.Append('[');

                    for (int i = 0; i < ary.Count; i++)
                    {
                        if (sb.Length > 1)
                            sb.Append(", ");

                        String s = Object.Inspect(ary[i], caller);
                        tainted |= s.Tainted;
                        sb.Append(s.value);
                    }

                    sb.Append(']');

                    String result = new String(sb.ToString());
                    result.Tainted = tainted;
                    return result;
                }
                finally
                {
                    Array.EndInspect(recv);
                }
            }
        }
Пример #11
0
        internal static Array rb_push_glob(Frame caller, Proc block, object str, int flags)
        {
            Array ary = new Array();
            string path = String.StringValue(str, caller);


            foreach (string file in glob(path))
            {
                String s = new String(file);
                s.Tainted = true;
                ary.Add(s);
            }

            if (block != null)
            {
                Methods.rb_ary_each.singleton.Call0(null, ary, caller, block);
                return null;
            }
            return ary;
        }
Пример #12
0
        private static bool proc_options(int first, string[] args)
        {
            bool do_search = false;
            bool version = false;
            bool copyright = false;
            bool verbose = false;
            String e_script = null;

            int i;
            for (i = first; i < args.Length; i++)
            {
                if (args[i][0] != '-' || args[i].Length == 1)
                    break;

                string s = args[i].Substring(1);

            reswitch:
                if (s == "")
                {
                    continue;
                }
                switch (s[0])
                {
                    case 'a':
                        do_split = true;
                        set_runtime_option("do_split", do_split);
                        s = s.Substring(1);
                        goto reswitch;

                    case 'p':
                        do_print = true;
                        set_runtime_option("do_print", true);
                        goto case 'n';

                    case 'n':
                        do_loop = true;
                        s = s.Substring(1);
                        goto reswitch;

                    case 'd':
                        set_runtime_option("ruby_debug", true);
                        set_runtime_option("ruby_verbose", true);
                        s = s.Substring(1);
                        goto reswitch;

                    case 'y':
                        set_runtime_option("ruby_yydebug", 1);
                        s = s.Substring(1);
                        goto reswitch;

                    case 'v':
                        Version.ruby_show_version();
                        verbose = true;
                        s = s.Substring(1);
                        goto reswitch;

                    case 'w':
                        set_runtime_option("ruby_verbose", true);
                        s = s.Substring(1);
                        goto reswitch;

                    case 'W':
                        {
                            int numlen;
                            int v = 2;      // -W as -W2
                            Scanner.scan_oct(s, 1, out numlen);
                            if (numlen == 0) v = 1;
                            s = s.Substring(numlen);
                            switch (v)
                            {
                                case 0:
                                    set_runtime_option("ruby_verbose", null); break;
                                case 1:
                                    set_runtime_option("ruby_verbose", false); break;
                                default:
                                    set_runtime_option("ruby_verbose", true); break;
                            }
                            goto reswitch;
                        }
                    case 'c':
                        do_check = true;
                        s = s.Substring(1);
                        goto reswitch;

                    case 's':
                        sflag = true;
                        s = s.Substring(1);
                        goto reswitch;

                    case 'h':
                        usage(compiler_name);
                        return false;

                    case 'l':
                        do_line = true;
                        set_runtime_option("do_line", do_line);
                        s = s.Substring(1);
                        goto reswitch;

                    case 'S':
                        do_search = true;
                        s = s.Substring(1);
                        goto reswitch;

                    case 'e':
                        s = s.Substring(1);
                        if (s == "")
                        {
                            if (i + 1 < args.Length)
                                s = args[++i];
                            else
                            {
                                System.Console.Error.WriteLine("{0}: no code specified for -e", compiler_name);
                                return false;
                            }
                        }
                        if (script == null) script = "-e";
                        e_script = new String(s + "\n");
                        break;

                    case 'r':
                        if (s != "")
                            set_runtime_option("require", s);
                        else if (i + 1 < args.Length)
                            set_runtime_option("require", args[++i]);
                        break;

                    case 'i':
                        s = s.Substring(1);
                        set_runtime_option("ruby_inplace_mode", s);
                        break;

                    case 'x':
                        xflag = true;
                        s = s.Substring(1);
                        if (s != "")
                            try
                            {
                                System.IO.Directory.SetCurrentDirectory(s);
                            }
                            catch (System.IO.IOException)
                            {
                                rb_fatal("Can't chdir to {0}", s);
                            }
                        break;

                    case 'C':
                    case 'X':
                        s = s.Substring(1);
                        if (s == "")
                            if (i + 1 < args.Length)
                                s = args[++i];
                            else
                                rb_fatal("Can't chdir");
                        try
                        {
                            System.IO.Directory.SetCurrentDirectory(s);
                        }
                        catch (System.IO.IOException)
                        {
                            rb_fatal("Can't chdir to {0}", s);
                        }
                        break;

                    case 'F':
                        if (s != "")
                            set_runtime_option("rb_fs", s);
                        break;

                    case 'K':
                        if (s != "")
                            set_runtime_option("kcode", s);
                        s = s.Substring(1);
                        goto reswitch;

                    case 'T':
                        {
                            int numlen;
                            uint v = 1;
                            if (s != "")
                            {
                                v = Scanner.scan_oct(s, 2, out numlen);
                                if (numlen == 0) v = 1;
                                s = s.Substring(numlen);
                            }
                            set_runtime_option("safe_level", v);
                            goto reswitch;
                        }

                    case 'I':
                        s = s.Substring(1);
                        if (s != "")
                            set_runtime_option("include", s);
                        else if (i + 1 < args.Length)
                            set_runtime_option("include", args[++i]);
                        break;

                    case '0':
                        {
                            int numlen;
                            uint v;

                            v = Scanner.scan_oct(s, 4, out numlen);
                            s = s.Substring(numlen);
                            if (v > 0377)
                                set_runtime_option("rb_rs", null);
                            else if (v == 0 && numlen >= 2)
                                set_runtime_option("rb_rs", "\n\n");
                            else
                            {
                                char c = (char)(v & 0xff);
                                set_runtime_option("rb_rs", new string(c, 1));
                            }
                            goto reswitch;
                        }

                    case '-':
                        s = s.Substring(1);
                        if (s == "")
                        {
                            i++;
                            goto switch_end;
                        }
                        if (s == "copyright")
                            copyright = true;
                        else if (s == "debug")
                        {
                            set_runtime_option("ruby_debug", true);
                            set_runtime_option("ruby_verbose", true);
                        }
                        else if (s == "version")
                            version = true;
                        else if (s == "verbose")
                        {
                            verbose = true;
                            set_runtime_option("ruby_verbose", true);
                        }
                        else if (s == "yydebug")
                            set_runtime_option("ruby_yydebug", 1);
                        else if (s == "help")
                        {
                            usage(compiler_name);
                            return false;
                        }
                        else
                        {
                            System.Console.Error.WriteLine("{0}: invalid option --{1} (-h will show valid options)", compiler_name, s);
                            return false;
                        }
                        break;

                    default:
                        System.Console.Error.WriteLine("{0}: invalid option -{1} (-h will show valid options)", compiler_name, s);
                        return false;
                }
            }

        switch_end:
            if (first == 0) return false;

            if (version)
            {
                Version.ruby_show_version();
                return false;
            }

            if (copyright)
            {
                Version.ruby_show_copyright();
                return false;
            }

            if (e_script == null)
                if (i >= args.Length)
                {
                    if (verbose) return false;
                    script = "-";
                }
                else
                {
                    script = args[i];
                    if (script == "")
                        script = "-";
                    else if (do_search)
                    {
                        string path = System.Environment.GetEnvironmentVariable("RUBYPATH");

                        script = null;
                        if (path != null)
                            script = File.dln_find(args[i], path);
                        if (script == null)
                            script = File.dln_find(args[i], System.Environment.GetEnvironmentVariable("PATH_ENV"));
                        if (script == null)
                            script = args[i];
                    }

                    script = script.Replace('/', Path.DirectorySeparatorChar);
                    i++;
                }

            if (script == "-")
                e_script = new String(System.Console.In.ReadToEnd());

            ruby_script(script);
            ruby_set_argv(args, i);
            process_sflag();

            if (e_script != null)
                tree = (AST.SOURCEFILE)Parser.ParseString(null, null, script, e_script, 1);
            else
            {
                List<string> more_options;
                try
                {
                    tree = File.load_file(null, script, xflag, out more_options);
                }
                catch (System.Exception e)
                {
                    System.Console.Error.WriteLine("{0}: {1} -- {2}\n", compiler_name, e.Message, script);
                    return false;
                }
                if (more_options != null)
                    proc_options(0, more_options.ToArray());
            }
            process_sflag();
            xflag = false;

            return true;
        }
Пример #13
0
        public override object Call(Class last_class, object recv, Frame caller, Proc block, Array argv)
        {
            int argc = argv.Count;
            if (argc > 0)
            {
                throw new ArgumentError(string.Format(CultureInfo.InvariantCulture, "wrong number of arguments ({0} for 0)", argc)).raise(caller);
            }
            else
            {
                Match match = (Match)recv;
                Array result = new Array();
                bool taint = match.Tainted;

                System.Text.RegularExpressions.Match m = match.value;

                while (m.Success)
                {
                    String str = new String(m.Value);
                    if (taint)
                        str.Tainted = true;
                    if (Eval.Test(Proc.rb_yield(block, caller, new object[] { str })))
                    {
                        result.Add(str);
                    }
                    m = m.NextMatch();
                }
                return result;
            }
        }
Пример #14
0
 internal static void rb_lastline_set(Frame caller, String line) // author: cjs, status: done
 {
     caller.Uscore = line;
 }
Пример #15
0
        internal static object r_bytes0(Frame caller, int len, load_arg arg)
        {
            object str;

            if (len == 0) return new String();

            if (arg.src is String)
            {
                if (((String)arg.src).value.Length > arg.offset)
                {
                    str = new String(((String)arg.src).value.Substring(arg.offset, len));
                    arg.offset += len;
                }
                else
                {
                    goto too_short;
                }
            }
            else
            {
                object src = arg.src;
                object n = len;
                //TEST: the address of n is passed in the ruby code, these calls probably arn't equivalent 
                //        str = rb_funcall2(src, s_read, 1, &n);
                str = Eval.CallPrivate(src, caller, s_read, null, n);
                if (str == null) goto too_short;
                if (!(str is String))
                {
                    str = String.StringValue(str, caller);
                }
                if (((String)str).value.Length != len) goto too_short;
                if (((Basic)str).Tainted)
                {
                    arg.taint = true;
                }
                if(((Basic)str).Tainted)
                {
                    arg.taint = true;
                }
            }

            return str;

        too_short:
            throw new ArgumentError("marshal data too short").raise(caller);
        }
Пример #16
0
        public override object Call(Class last_class, object recv, Frame caller, Proc block, Array args)
        {
            object fmt;
            string fmtString;
            int p, end;
            System.Text.StringBuilder result = new System.Text.StringBuilder();

            int width, prec, flags = Sprintf.FNONE;
            int nextArg = 1;
            int posArg = 0;
            //int tainted = 0;
            object nextValue;
            object tmp = null;
            object str;

            fmt = args[0];
            //if (OBJ_TAINTED(fmt)) tainted = 1;
            fmtString = String.StringValue(fmt, caller);
            //    fmt = rb_str_new4(fmt); - tainted work being done in this method
            p = 0; //p = RSTRING(fmt)->ptr;
            end = fmtString.Length;

            for (; p < end; p++)
            {
                int t;
                int n;

                for (t = p; t < end && fmtString[t] != '%'; t++) ; //skip over preceding chars
                result.Append(fmtString.Substring(p, t - p));
                if (t >= end)
                {
                    /* end of fmt string */
                    goto sprint_exit;
                }
                p = t + 1;  /* skip '%' */

                width = prec = -1;
                nextValue = null;

            retry:
                switch (fmtString[p])
                {
                    case ' ':
                        flags |= Sprintf.FSPACE; //leave a space at the start of positive numbers.
                        p++;
                        goto retry;

                    case '#':
                        flags |= Sprintf.FSHARP;
                        p++;
                        goto retry;

                    case '+':
                        flags |= Sprintf.FPLUS;
                        p++;
                        goto retry;

                    case '-':
                        flags |= Sprintf.FMINUS;
                        p++;
                        goto retry;

                    case '0':
                        flags |= Sprintf.FZERO;
                        p++;
                        goto retry;

                    case '1':
                    case '2':
                    case '3':
                    case '4':
                    case '5':
                    case '6':
                    case '7':
                    case '8':
                    case '9':
                        n = 0;
                        for (; p < end && char.IsDigit(fmtString[p]); p++)
                        {
                            n = 10 * n + (fmtString[p] - '0');
                        }
                        if (p >= end)
                        {
                            throw new ArgumentError("malformed format string - %[0-9]").raise(caller);
                        }
                        if (fmtString[p] == '$')
                        {
                            if (nextValue != null)
                            {
                                throw new ArgumentError(string.Format(CultureInfo.InvariantCulture, "value given twice - {0}$", n)).raise(caller);
                            }
                            nextValue = Sprintf.GETPOSARG(caller, n, ref posArg, args);
                            p++;
                            goto retry;
                        }
                        width = n;
                        flags |= Sprintf.FWIDTH;
                        goto retry;

                    case '*':
                        if ((flags & Sprintf.FWIDTH) > 0)
                        {
                            throw new ArgumentError("width given twice").raise(caller);
                        }
                        flags |= Sprintf.FWIDTH;
                        width = Sprintf.GETASTER(ref t, ref p, out n, end, fmtString,
                            ref posArg, ref nextArg, nextValue, tmp, args, caller);
                        if (width < 0)
                        {
                            flags |= Sprintf.FMINUS;
                            width = -width;
                        }
                        p++;
                        goto retry;

                    case '.':
                        if ((flags & Sprintf.FPREC) > 0)
                        {
                            throw new ArgumentError("precision given twice").raise(caller);
                        }
                        flags |= Sprintf.FPREC;

                        prec = 0;
                        p++;
                        if (fmtString[p] == '*')
                        {
                            prec = Sprintf.GETASTER(ref t, ref p, out n, end, fmtString,
                            ref posArg, ref nextArg, nextValue, tmp, args, caller);
                            if (prec < 0) /* ignore negative precision */
                            {
                                flags &= ~Sprintf.FPREC;
                            }
                            p++;
                            goto retry;
                        }
                        for (; p < end && char.IsDigit(fmtString[p]); p++)
                        {
                            prec = 10 * prec + (fmtString[p] - '0');
                        }
                        if (p >= end)
                        {
                            throw new ArgumentError("malformed format string - %%.[0-9]").raise(caller);
                        }
                        goto retry;
                    case '\n':
                        p--;
                        goto case '%';
                    case '\0':
                        goto case '%';
                    case '%':
                        if (flags != Sprintf.FNONE)
                        {
                            throw new ArgumentError("illegal format character - %").raise(caller);
                        }
                        result.Append("%");
                        break;
                    case 'c':
                        {
                            object val = Sprintf.GETARG(caller, ref posArg, ref nextArg, nextValue, args);
                            int c;
                            if (!((flags & Sprintf.FMINUS) > 0))
                            {
                                while (--width > 0)
                                {
                                    result.Append(" ");
                                }
                            }
                            c = Numeric.rb_num2long(val, caller) & 0xff;
                            result.Append((char)(byte)c);
                            while (--width > 0)
                            {
                                result.Append(" ");
                            }
                        }
                        break;
                    case 's':
                        goto case 'p';
                    case 'p':
                        {
                            object arg = Sprintf.GETARG(caller, ref posArg, ref nextArg, nextValue, args);
                            int len;

                            if (fmtString[p] == 'p') arg = Object.Inspect(arg, caller);
                            str = String.ObjectAsString(arg, caller);
                            //if (OBJ_TAINTED(str)) tainted = 1;
                            len = ((String)str).value.Length;
                            if ((flags & Sprintf.FPREC) > 0)
                            {
                                if (prec < len)
                                {
                                    len = prec;
                                }
                            }
                            if ((flags & Sprintf.FWIDTH) > 0)
                            {
                                if (width > len)
                                {
                                    width -= len;
                                    if (!((flags & Sprintf.FMINUS) > 0))
                                    {
                                        while (width-- > 0)
                                        {
                                            result.Append(' ');
                                        }
                                    }
                                    result.Append(((String)str).value.Substring(0, len));
                                    if ((flags & Sprintf.FMINUS) > 0)
                                    {
                                        while (width-- > 0)
                                        {
                                            result.Append(' ');
                                        }
                                    }
                                    break;
                                }
                            }
                            result.Append(((String)str).value.Substring(0, len));
                        }
                        break;

                    case 'd':
                    case 'i':
                    case 'o':
                    case 'x':
                    case 'X':
                    case 'b':
                    case 'B':
                    case 'u':
                        {
                            object val = Sprintf.GETARG(caller, ref posArg, ref nextArg, nextValue, args);
                            string prefix = "";
                            int sign = 0;
                            int v = 0;
                            int bignum = 0;
                            int numBase = 0;
                            char sc = '\0';
                            string fbuf = "";
                            string nbuf = "";
                            int s = 0;
                            string sString = "";
                            int tt = 0;
                            string ttString = "";
                            object tmp2;
                            //int pos;
                            int len;

                            switch (fmtString[p])
                            {
                                case 'd':
                                    goto case 'i';
                                case 'i':
                                    sign = 1; break;
                                case 'o':
                                case 'x':
                                case 'X':
                                case 'b':
                                case 'B':
                                case 'u':
                                default:
                                    if ((flags & (Sprintf.FPLUS | Sprintf.FSPACE)) > 0) sign = 1;
                                    break;
                            }
                            if ((flags & Sprintf.FSHARP) > 0)
                            {
                                switch (fmtString[p])
                                {
                                    case 'o':
                                        prefix = "0"; break;
                                    case 'x':
                                        prefix = "0x"; break;
                                    case 'X':
                                        prefix = "0X"; break;
                                    case 'b':
                                        prefix = "0b"; break;
                                    case 'B':
                                        prefix = "0B"; break;
                                }
                                if (prefix.Length > 0)
                                {
                                    width -= prefix.Length;
                                }
                            }
                        bin_retry:
                            if (val is Float)
                            {
                                val = new Bignum(((Float)val).value);
                                if (val is int) goto bin_retry;
                                bignum = 1;
                            }
                            else if (val is String)
                            {
                                val = String.rb_str_to_inum(val, caller, 0, true); //TEST - should base be 0?
                                goto bin_retry;
                            }
                            else if (val is Bignum)
                            {
                                bignum = 1;
                            }
                            else if (val is int)
                            {
                                v = (int)val;
                            }
                            else
                            {
                                val = Integer.rb_Integer(val, caller);
                                goto bin_retry;
                            }
                            switch (fmtString[p])
                            {
                                case 'o':
                                    numBase = 8; break;
                                case 'x':
                                case 'X':
                                    numBase = 16; break;
                                case 'b':
                                case 'B':
                                    numBase = 2; break;
                                case 'u':
                                case 'd':
                                case 'i':
                                default:
                                    numBase = 10; break;
                            }
                            if (bignum == 0)
                            {
                                if (numBase == 2)
                                {
                                    val = new Bignum(v);
                                    goto bin_retry;
                                }
                                if (sign != 0)
                                {
                                    char c = fmtString[p];
                                    if (c == 'i') c = 'd'; /* %d and %i are identical */
                                    if (v < 0)
                                    {
                                        v = -v;
                                        sc = '-';
                                        width--;
                                    }
                                    else if ((flags & Sprintf.FPLUS) > 0)
                                    {
                                        sc = '+';
                                        width--;
                                    }
                                    else if ((flags & Sprintf.FSPACE) > 0)
                                    {
                                        sc = ' ';
                                        width--;
                                    }
                                    fbuf = Sprintf.sprintf("%%l%c", c);
                                    nbuf = Sprintf.sprintf(fbuf, v);
                                    s = 0;//s = nbuf;
                                    sString = nbuf;
                                    goto format_integer;
                                }
                                s = 0;//s = nbuf;
                                if (v < 0)
                                {
                                    if (numBase == 10)
                                    {
                                        Errors.rb_warning("negative number for %%u specifier");
                                    }
                                    if (!((flags & (Sprintf.FPREC | Sprintf.FZERO)) > 0))
                                    {
                                        nbuf = nbuf.Insert(s, "..");
                                        s += 2;
                                    }
                                }
                                fbuf = Sprintf.sprintf("%%l%c", fmtString[p] == 'X' ? 'x' : fmtString[p]);
                                nbuf = nbuf.Substring(0, s) + Sprintf.sprintf(fbuf, v);
                                if (v < 0)
                                {
                                    char d = '\0';
                                    Sprintf.remove_sign_bits(nbuf.Substring(s), numBase);
                                    switch (numBase)
                                    {
                                        case 16:
                                            d = 'f'; break;
                                        case 8:
                                            d = '7'; break;
                                    }
                                    if (d != '\0' && (nbuf[s] != d))
                                    {
                                        nbuf.Insert(s, d.ToString());
                                    }
                                }
                                s = 0;//s = nbuf;
                                sString = nbuf;
                                goto format_integer;
                            }
                            //bignums between here and format_integer
                            if (sign > 0)
                            {
                                tmp2 = ((Bignum)val).value.ToString((uint)numBase); //tmp2 = rb_big2str(val, numBase);
                                s = 0;
                                sString = (string)tmp2;
                                if (sString[s] == '-')
                                {
                                    s++;
                                    sc = '-';
                                    width--;
                                }
                                else if ((flags & Sprintf.FPLUS) > 0)
                                {
                                    sc = '+';
                                    width--;
                                }
                                else if ((flags & Sprintf.FSPACE) > 0)
                                {
                                    sc = ' ';
                                    width--;
                                }
                                goto format_integer;
                            }
                            //negative bignum from now on
                            if (((Bignum)val).value < (IronMath.integer.make(0)))
                            {
                                val = new Bignum(((Bignum)val).value);
                                //must be equivalent to rb_big_2comp(val); TEST
                                new Bignum(~((Bignum)val).value);
                            }
                            tmp2 = new String(((Bignum)val).value.ToString((uint)numBase)); //tmp2 = rb_big2str(val, numBase);
                            s = 0;//              s = RSTRING(tmp2)->ptr;
                            sString = ((String)tmp2).value;
                            if (sString[s] == '-')
                            {
                                if (numBase == 10)
                                {
                                    Errors.rb_warning("negative number for %%u specifier");
                                    s++;
                                }
                                else
                                {
                                    sString = Sprintf.remove_sign_bits(sString.Substring(++s), numBase);
                                    tmp2 = new String();
                                    tt = 0;
                                    ttString = ((String)tmp2).value;
                                    if (!((flags & (Sprintf.FPREC | Sprintf.FZERO)) > 0))
                                    {
                                        ttString.Insert(tt, "..");
                                        tt += 2;
                                    }
                                    switch (numBase)
                                    {
                                        case 16:
                                            if (sString[0] != 'f')
                                            {
                                                ttString += "f"; //strcpy(t++, "f");
                                                tt++;
                                            }
                                            break;
                                        case 8:
                                            if (sString[0] != 'f')
                                            {
                                                ttString += "7"; //strcpy(t++, "7");
                                                tt++;
                                            }
                                            break;
                                        case 2:
                                            if (sString[0] != 'f')
                                            {
                                                ttString += "1"; //strcpy(t++, "1");
                                                tt++;
                                            }
                                            break;
                                    }
                                    ttString += sString; //check //strcpy(t, s);
                                    bignum = 2;
                                }
                            }
                            s = 0;
                            sString = ((String)tmp2).value;
                        format_integer:
                            //pos = -1;
                            len = sString.Length;
                            if (fmtString[p] == 'X')
                            {
                                sString = sString.ToUpperInvariant();
                            }
                            if ((flags & (Sprintf.FZERO | Sprintf.FPREC)) == Sprintf.FZERO)
                            {
                                prec = width;
                                width = 0;
                            }
                            else
                            {
                                if (prec < len) prec = len;
                                width -= prec;
                            }

                            if (!((flags & Sprintf.FMINUS) > 0))
                            {
                                while (width-- > 0)
                                {
                                    result.Append(' ');
                                }
                            }
                            if (sc != '\0')
                            {
                                result.Append(sc);
                            }
                            if (prefix.Length > 0)
                            {
                                result.Append(prefix);
                            }
                            if (bignum == 0 && v < 0)
                            {
                                char c = Sprintf.sign_bits(numBase, fmtString[p]);
                                while (len < prec--)
                                {
                                    result.Append(c);
                                }
                            }
                            else
                            {
                                char c;

                                if ((bignum != 0) && ((Bignum)val).value < (IronMath.integer.make(0)))
                                {
                                    c = Sprintf.sign_bits(numBase, fmtString[p]);
                                }
                                else
                                {
                                    c = '0';
                                }
                                while (len < prec--)
                                {
                                    result.Append(c);
                                }
                            }
                            result.Append(sString.Substring(s, len));
                            while (width-- > 0)
                            {
                                result.Append(' ');
                            }
                        }
                        break;
                    case 'f':
                    case 'g':
                    case 'G':
                    case 'e':
                    case 'E':
                        {
                            object val = Sprintf.GETARG(caller, ref posArg, ref nextArg, nextValue, args);
                            double fval;
                            int i = 0;
                            int need = 6;
                            string fbuf;

                            fval = Float.rb_Float(val, caller).value;
                            if (double.IsNaN(fval) || double.IsInfinity(fval))
                            {
                                string expr;

                                if (double.IsNaN(fval))
                                {
                                    expr = "NaN";
                                }
                                else
                                {
                                    expr = "Inf";
                                }
                                need = expr.Length;
                                if ((!double.IsNaN(fval) && fval < 0.0) || ((flags & Sprintf.FPLUS) != 0))
                                {
                                    need++;
                                }
                                if (((flags & Sprintf.FWIDTH) != 0) && need < width)
                                {
                                    need = width;
                                }
                                //TODO - may need a blen count. 
                                //                  sprintf(&buf[blen], "%*s", need, "");
                                if ((flags & Sprintf.FMINUS) != 0)
                                {
                                    if (!double.IsNaN(fval) && fval < 0.0)
                                    {
                                        result.Append('-'); //buf[blen++] = '-';
                                    }
                                    else if ((flags & Sprintf.FPLUS) != 0)
                                    {
                                        result.Append('+'); //buf[blen++] = '+';
                                    }
                                    else if ((flags & Sprintf.FSPACE) != 0)
                                    {
                                        result.Append(' '); //blen++;
                                    }
                                    result.Append(expr); //strncpy(&buf[blen], expr, strlen(expr));
                                }
                                else if ((flags & Sprintf.FZERO) != 0)
                                {
                                    if (!double.IsNaN(fval) && fval < 0.0)
                                    {
                                        result.Append('-');//buf[blen++] = '-';
                                        need--;
                                    }
                                    else if ((flags & Sprintf.FPLUS) != 0)
                                    {
                                        result.Append('+');    //buf[blen++] = '+';
                                        need--;
                                    }
                                    else if ((flags & Sprintf.FSPACE) != 0)
                                    {
                                        result.Append(' '); //blen++;
                                        need--;
                                    }
                                    while (need-- - expr.Length > 0)
                                    {
                                        result.Append('0');
                                    }
                                    result.Append(expr); //strncpy(&buf[blen], expr, strlen(expr));                                    
                                }
                                else
                                {
                                    if (!double.IsNaN(fval) && fval < 0.0)
                                        //run this - may need a blen count. 
                                        //could buffer overflow - check this out
                                        result[need - expr.Length - 1] = '-';  //buf[blen + need - strlen(expr) - 1] = '-';
                                    else if ((flags & Sprintf.FPLUS) != 0)
                                        result[need - expr.Length - 1] = '-';  //buf[blen + need - strlen(expr) - 1] = '+';
                                    //TODO - may need a blen count
                                    //strncpy(&buf[blen + need - strlen(expr)], expr, strlen(expr));
                                }
                                //blen += strlen(&buf[blen]);
                                break;
                            }

                            fbuf = Sprintf.fmt_setup(fmtString[p], flags, width, prec);
                            need = 0;
                            if (fmtString[p] != 'e' && fmtString[p] != 'E')
                            {
                                i = int.MinValue;
                                object frexpResult = Ruby.Methods.math_frexp.singleton.Call1(last_class, null, caller, block, new Float(fval));
                                i = (int)((Array)frexpResult).value[1]; //get the exponent
                                if (i > 0)
                                {
                                    need = Sprintf.BIT_DIGITS(i);
                                }
                            }
                            need += ((flags & Sprintf.FPREC) > 0) ? prec : 6;
                            if (((flags & Sprintf.FWIDTH) != 0) && need < width)
                            {
                                need = width;
                            }

                            need += 20;
                            result.Append(Sprintf.sprintf(fbuf, fval));  //              sprintf(&buf[blen], fbuf, fval);  
                            //may need to include belen calcs?
                            //              blen += strlen(&buf[blen]); 
                        }
                        break;
                    default:
                        if (char.IsLetterOrDigit(fmtString[p]) || fmtString[p] == ' ')
                            throw new ArgumentError(string.Format(CultureInfo.InvariantCulture, "malformed format string - %{0}", fmtString)).raise(caller);
                        else
                            throw new ArgumentError("malformed format string").raise(caller);
                }
                flags = Sprintf.FNONE;
            }
            
        sprint_exit:
            //    /* XXX - We cannot validiate the number of arguments because
            //    *       the format string may contain `n$'-style argument selector.
            //    */

            if (Eval.Test(Options.ruby_verbose.value) && posArg >= 0 && nextArg < args.Count) {
                throw new ArgumentError("too many arguments for format string").raise(caller);
            }

            String res = new String(result.ToString());
            //TODO:
            //    if (tainted) OBJ_TAINT(result);
            return res;
        }
Пример #17
0
        // ------------------------------------------------------------------------------


        internal static object eval(object self, String src, IContext scope, string file, int line, Frame caller) {
            //System.Console.WriteLine("eval({0})", src.value);
            Frame frame = scope.Frame();
            //frame.caller = caller;      // BBTAG

            Compiler.AST.EVAL tree = (Compiler.AST.EVAL)Compiler.Parser.ParseString(caller, frame, file, src, line);
            PERWAPI.FieldRef surroundingClass = Compiler.CodeGenContext.FindParentClassField(frame.GetType());
            PERWAPI.PEFile assembly = tree.GenerateCode(surroundingClass);

            //assembly.WritePEFile(false);
            return tree.ExecuteInit(assembly, null, scope.Self(), caller, frame);
            //throw new System.Exception("testing");
        }
Пример #18
0
        internal static object class2path(object klass, Frame caller)
        {

            object path = new String(rb_class_path(klass, caller));

            string n = ((String)path).value;

            if (n[0] == '#')
            {
                string type;

                if (((Class)klass)._type == Class.Type.Class)
                {
                    type = "class";
                }
                else
                {
                    type = "module";
                }

                throw new TypeError(string.Format(CultureInfo.InvariantCulture, "can't dump anonymous {0} {1}", type, n)).raise(caller);
            }

            return path;
        }
Пример #19
0
        //variable.c
        //MOVE: this doesn't belong here, move to variable.cs
        internal static object rb_path2class(Frame caller, string path)
        {
            int pbeg = 0;
            int p = 0;
            //int id;
            object c = Ruby.Runtime.Init.rb_cObject;

            if (path[0] == '#')
            {
                throw new ArgumentError(string.Format(CultureInfo.InvariantCulture, "can't retrieve anonymous class {0}", path)).raise(caller);
            }
            while (p < path.Length)
            {

                object str;
                while (p < path.Length && path[p] != ':') p++;
                str = new String(path.Substring(pbeg, p - pbeg));
                if (p < path.Length && path[p] == ':')
                {
                    if (p + 1 >= path.Length || path[p + 1] != ':') 
                        goto undefined_class;
                    p += 2;
                    pbeg = p;
                }
                if (!((Class)c).const_defined(((String)str).value))
                {
                    goto undefined_class;
                }
                c = ((Class)c).const_get(((String)str).value, caller);
                Class.Type type = ((Class)c)._type;
                switch (type)
                {
                    case Class.Type.Class:
                    case Class.Type.Module:
                        break;
                    default:
                        throw new TypeError(string.Format(CultureInfo.InvariantCulture, "{0} does not refer class/module", path)).raise(caller);
                }
            }
            return c;

        undefined_class:
            throw new ArgumentError(string.Format(CultureInfo.InvariantCulture, "undefined class/module {0}", path.Substring(0, p))).raise(caller);
        }
Пример #20
0
        public override object Call(Class last_class, object recv, Frame caller, Proc block, Array rest)
        {
            String str = (String)recv;

            Class.rb_scan_args(caller, rest, 0, 2, false);

            object limit = null;
            int lim = 0;
            int i = 0;
            if (rest.Count == 2)
            {
                limit = rest[1];
                lim = Numeric.rb_num2long(limit, caller);
                if (lim <= 0)
                    limit = null;
                else if (lim == 1)
                {
                    if (str.value.Length == 0)
                        return new Array();
                    return new Array(str);
                }
                i = 1;
            }

            bool awk_split = false;
            object spat = null;
            if (rest.Count > 0)
                spat = rest[0];
            if (spat == null)
            {
                if (String.rb_fs != null && String.rb_fs.value != null)
                    spat = String.rb_fs.value;
                else
                    awk_split = true;
            }
            if (!awk_split)
            {
                if (spat is String && ((String)spat).value.Length == 1)
                {
                    if (((String)spat).value[0] == ' ')
                        awk_split = true;
                    else
                        spat = new Regexp(Regexp.rb_reg_quote((String)spat).value, 0);
                }
                else
                    spat = String.get_pat(spat, true, caller);
            }

            Array result = new Array();
            int beg = 0;
            if (awk_split)
            {
                string[] strings;
                if (limit != null)
                    strings = str.value.Split(new char[] { ' ' }, lim, System.StringSplitOptions.RemoveEmptyEntries);
                else
                    strings = str.value.Split(new char[] { ' ' }, System.StringSplitOptions.RemoveEmptyEntries);

                foreach (string s in strings)
                {
                    String s1 = new String(s);
                    Object.obj_infect(s1, str);
                    result.Add(s1);

                    if (limit != null)
                        i++;
                }

                beg = str.value.Length;
            }
            else
            {
                Regexp pat = (Regexp)spat;

                int start = beg;
                int end;
                bool last_null = false;

                while ((end = pat.rb_reg_search(str, start, false, caller)) >= 0)
                {
                    Match regs = Regexp.rb_backref_get(caller);
                    if (start == end && regs.value.Length == 0)
                    {
                        if (str.value.Length == 0)
                        {
                            result.Add(new String());
                            break;
                        }
                        else if (last_null)
                        {
                            String s = new String(str.value.Substring(beg, 1));
                            Object.obj_infect(s, str);
                            result.Add(s);
                            beg = start;
                        }
                        else
                        {
                            start += 1;
                            last_null = true;
                            continue;
                        }
                    }
                    else if (start == end && last_null && pat.value.ToString().Equals(regs.value.Value))
                    {
                        beg = start = regs.value.Index + regs.value.Length;
                    }
                    else
                    {
                        String s = new String(str.value.Substring(beg, end - beg));
                        Object.obj_infect(s, str);
                        result.Add(s);
                        beg = start = regs.value.Index + regs.value.Length;
                    }
                    last_null = false;

                    for (int idx = 1; idx < regs.value.Groups.Count; idx++)
                    {
                        String tmp;
                        if (regs.value.Groups[idx].Length == 0)
                            tmp = new String();
                        else
                            tmp = new String(regs.value.Groups[idx].Value);
                        Object.obj_infect(tmp, str);
                        result.Add(tmp);
                    }

                    if (limit != null && lim <= ++i)
                        break;
                }
            }

            if (str.value.Length > 0 && (limit != null || str.value.Length > beg || lim < 0))
            {
                String tmp;
                if (str.value.Length == beg)
                    tmp = new String();
                else
                    tmp = new String(str.value.Substring(beg));
                Object.obj_infect(tmp, str);
                result.Add(tmp);
            }

            if (limit == null && lim == 0)
                while (result.Count > 0 && ((String)result.value[result.Count - 1]).value.Length == 0)
                    result.value.RemoveAt(result.value.Count - 1);

            return result;
        }
Пример #21
0
        public override object Call0(Class last_class, object recv, Frame caller, Proc block)
        {
            string value = ((String)recv).value;

            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            
            sb.Append('"');
            for (int i = 0; i < value.Length; i++)
            {
                char c = value[i];
                if (c == '"' || c == '\\' || (c == '#' && (i + 1 < value.Length) && (value[i+1] == '$' || value[i+1] == '@' || value[i+1] == '{')))
                {
                    sb.Append('\\');
                    sb.Append(c);
                }
                else if (c < 256 && String.ISPRINT((byte)c)) // ISPRINT()
                {
                    sb.Append(c);
                }
                else
                    switch (c)
                    {
                        case '\n':
                            sb.Append(@"\n");
                            break;
                        case '\r':
                            sb.Append(@"\r");
                            break;
                        case '\t':
                            sb.Append(@"\t");
                            break;
                        case '\f':
                            sb.Append(@"\f");
                            break;
                        case '\v':
                            sb.Append(@"\v");
                            break;
                        case '\a':
                            sb.Append(@"\a");
                            break;
                        case (char)33:
                            sb.Append(@"e");
                            break;
                        default:
                            sb.Append("\\"+String.DecimalToBase(c & 0xFF, 8, 3));
                            break;
                    }
            }
            sb.Append('"');
            
            String result = new String(sb.ToString());
            Object.obj_infect(result, recv);
            return result;
        }
Пример #22
0
        public override object Call0(Class last_class, object recv, Frame caller, Proc block)
        {
            Hash hash = (Hash)recv;
            if (hash.value.Count == 0)
                return new String("{}");
            else if (Hash.IsInspecting(recv))
                return new String("{...}");
            else
            {
                try
                {
                    bool taint = hash.Tainted;
                    Hash.StartInspect(recv);
                    System.Text.StringBuilder sb = new System.Text.StringBuilder();
                    sb.Append('{');

                    foreach (KeyValuePair pair in hash.value)
                    {
                        if (sb.Length > 1)
                            sb.Append(", ");

                        String key = Object.Inspect(pair.Key.key, caller);
                        taint |= key.Tainted;
                        sb.Append(key.value);
                        sb.Append("=>");
                        String val = Object.Inspect(pair.Value, caller);
                        taint |= val.Tainted;
                        sb.Append(val.value);
                    }

                    sb.Append('}');

                    String result = new String(sb.ToString());
                    result.Tainted = taint;
                    return result;
                }
                finally
                {
                    Hash.EndInspect(recv);
                }
            }
        }
Пример #23
0
        public override object Call0(Class klass, object recv, Frame caller, Proc block)
        {
            System.Text.StringBuilder str = new System.Text.StringBuilder("{");

            bool first = true;
            foreach (System.Collections.DictionaryEntry pair in System.Environment.GetEnvironmentVariables())
            {
                if (first)
                    first = false;
                else
                    str.Append(", ");
                str.Append("\"");
                str.Append(pair.Key);
                str.Append("\"=>");
                str.Append(pair.Value);
            }

            str.Append("}");

            String s = new String(str.ToString());
            s.Tainted = true;

            return s;

            //return rb_hash_inspect.singleton.Call0(klass, env_to_hash.singleton.Call0(klass, recv, caller, block), caller, block);
        }
Пример #24
0
        private object scan_once(String str, Regexp pat, ref int start, Frame caller)
        {
            Match match;
            if (pat.rb_reg_search(str, start, false, caller) >= 0)
            {
                match = Regexp.rb_backref_get(caller);

                if (match.value.Length == 0)
                    /*
                     * Always consume at least one character of the input string
                     */
                    start = match.value.Index + 1;
                else
                    start = match.value.Index + match.value.Length;

                if (match.value.Groups.Count == 1)
                    return Regexp.rb_reg_nth_match(0, match);
                else
                {
                    Array result = new Array();
                    for (int i = 1; i < match.value.Groups.Count; i++)
                        result = result.Add(Regexp.rb_reg_nth_match(i, match));
                    return result;
                }
            }
            else
                return null;
        }
Пример #25
0
        public override object Call0(Class last_class, object recv, Frame caller, Proc block)
        {
            FileStat self = (FileStat)recv;

            System.Text.StringBuilder str = new System.Text.StringBuilder();
            str.Append("#<");

            str.Append(((Class)recv)._name + " ");
            str.Append("dev=0x" + self.stat.st_dev.ToString("1x", CultureInfo.InvariantCulture) + " ");
            str.Append("ino=" + self.stat.st_ino.ToString(CultureInfo.InvariantCulture) + " ");
            str.Append("mode=0" + self.stat.st_mode.ToString("1o", CultureInfo.InvariantCulture) + " ");
            str.Append("nlink=" + self.stat.st_nlink.ToString(CultureInfo.InvariantCulture) + " ");
            str.Append("uid=" + self.stat.st_uid.ToString(CultureInfo.InvariantCulture) + " ");
            str.Append("gid=" + self.stat.st_gid.ToString(CultureInfo.InvariantCulture) + " ");
            str.Append("rdev=nil ");
            str.Append("size=" + self.stat.st_size.ToString(CultureInfo.InvariantCulture) + " ");
            str.Append("blocks=nil ");
            str.Append("blksize=nil ");
            str.Append("atime=" + self.stat.st_atime.ToString() + " ");
            str.Append("mtime=" + self.stat.st_mtime.ToString() + " ");
            str.Append("ctime=" + self.stat.st_ctime.ToString() + " ");
            
            str.Append(">");

            String result = new String(str.ToString());
            result.Tainted |= self.Tainted;

            return result;
        }
Пример #26
0
 private static String smart_chomp(String str, int len)
 {
     if (str.value[len - 1] == '\n')
     {
         str.value = str.value.Remove(--len);
         if (str.value.Length > 0 &&
             str.value[str.value.Length - 1] == '\r')
         {
             str.value = str.value.Remove(--len);
         }
     }
     else if (str.value[len - 1] == '\r')
     {
         str.value = str.value.Remove(--len);
     }
     else
     {
         return null;
     }
     return str;
 }
Пример #27
0
        internal static object eval_under(Class klass, object self, String src, IContext scope, string file, int line, Frame caller) {
            //System.Console.WriteLine("eval({0})", src.value);
            Frame frame = scope.Frame();
            //frame.caller = caller;      // BBTAG

            Compiler.AST.EVAL tree = (Compiler.AST.EVAL)Compiler.Parser.ParseString(caller, frame, file, src, line);
            PERWAPI.PEFile assembly = tree.GenerateCode(null);

            //Compiler.CodeGenContext.WriteToFile(assembly);
            return tree.ExecuteInit(assembly, klass, scope.Self(), caller, frame);
            //throw new System.Exception("testing");
        }
Пример #28
0
 public override object Call2(Class last_class, object str, Frame caller, Proc block, object src, object repl)
 {
     string newValue = ((String)str).value;
     str = new String(newValue);
     String.tr_trans(str, caller, block, src, repl, true);
     return str;
 }
Пример #29
0
        public override object Call1(Class last_class, object str, Frame caller, Proc block, object fmt)
        {
            string hexdigits = "0123456789abcdef0123456789ABCDEFx";
            string fmtString, strString;
            int s, send; //str
            int p, pend; //fmt
            object ary;
            char type;
            int len;
            bool star = false;
            int tmp;

            fmtString = String.StringValue(fmt, caller);
            strString = String.StringValue(str, caller);
            s = 0;
            send = strString.Length;
            p = 0;
            pend = fmtString.Length;

            ary = new Array();
            while (p < pend)
            {
                type = fmtString[p++];

                if (char.IsWhiteSpace(type)) continue;
                if (type == '#') //eat comments
                {
                    while ((p < pend) && (fmtString[p] != '\n'))
                    {
                        p++;
                    }
                    continue;
                }
                if (p < pend && (fmtString[p] == '_' || fmtString[p] == '!')) //'_' and '!' can only come after "sSiIlL"
                {
                    const string natstr = "sSiIlL";

                    if (natstr.IndexOf(type) != -1)
                    {
                        p++;
                    }
                    else
                    {
                        throw new ArgumentError(string.Format(CultureInfo.InvariantCulture, "'{0}' allowed only after types {1}", fmtString[p], natstr)).raise(caller);
                    }
                }
                if (p >= pend)
                {
                    len = 1;
                }
                else if (fmtString[p] == '*')
                {
                    star = true;
                    len = send - s;
                    p++;
                }
                else if (p < pend && char.IsDigit(fmtString[p]))
                {
                    int numStart = p;
                    int numEnd = p + 1;
                    while (numEnd < pend)
                    {
                        if (char.IsDigit(fmtString[numEnd]))
                        {
                            numEnd++;
                        }
                        else
                        {
                            break;
                        }
                    }
                    len = int.Parse(fmtString.Substring(numStart, numEnd - numStart), CultureInfo.InvariantCulture);
                }
                else
                {
                    len = (type != '@') ? 1 : 0;
                }
                int t;
                switch (type)
                {
                    case '%':
                        throw new ArgumentError("% is not supported").raise(caller);
                    case 'A':
                        if (len > send - s)
                        {
                            len = send - s;
                        }
                        int end = len;
                        t = s + len - 1;
                        while (t >= s)
                        {
                            if (strString[t] != ' ' && strString[t] != '\0') break;
                            t--; len--;
                        }
                        ((Array)ary).value.Add(String.infected_str_new(s, len, strString));
                        s += end;
                        break;
                    case 'Z':
                        {
                            t = s;

                            if (len > send - s) len = send - s;
                            while (t < s + len && t <= send && strString[t] != '\0') t++;
                            ((Array)ary).value.Add(String.infected_str_new(s, t - s, strString));
                            if (t < send) t++;
                            s = star ? t : s + len;
                        }
                        break;
                    case 'a':
                        if (len > send - s) len = send - s;
                        ((Array)ary).value.Add(String.infected_str_new(s, len, strString));
                        s += len;
                        break;
                    case 'b':
                        {
                            System.Text.StringBuilder bitStrBuilder = new System.Text.StringBuilder();
                            object bitstr;
                            int bits;
                            int i;

                            if ((p > 0 && fmtString[p - 1] == '*') || len > (send - s) * 8)
                            {
                                len = (send - s) * 8;
                            }
                            bits = 0;
                            for (i = 0; i < len; i++)
                            {
                                if ((i & 7) > 0)
                                {
                                    bits >>= 1;
                                }
                                else
                                {
                                    bits = (byte)strString[s++];
                                }
                                bitStrBuilder.Append(((bits & 1) > 0) ? '1' : '0');
                            }
                            bitstr = new String(bitStrBuilder.ToString());
                            ((Array)ary).value.Add(bitstr);
                        }
                        break;
                    case 'B':
                        {
                            System.Text.StringBuilder bitStrBuilder = new System.Text.StringBuilder();
                            object bitstr;
                            int bits;
                            int i;

                            if ((p > 0 && fmtString[p - 1] == '*') || len > (send - s) * 8)
                            {
                                len = (send - s) * 8;
                            }
                            bits = 0;
                            for (i = 0; i < len; i++)
                            {
                                if ((i & 7) > 0)
                                {
                                    bits <<= 1;
                                }
                                else
                                {
                                    bits = strString[s++];
                                }
                                bitStrBuilder.Append(((bits & 128) > 0) ? '1' : '0');
                            }
                            bitstr = new String(bitStrBuilder.ToString());
                            ((Array)ary).value.Add(bitstr);
                        }
                        break;
                    case 'h':
                        {
                            System.Text.StringBuilder bitStrBuilder = new System.Text.StringBuilder();
                            object bitstr;
                            int bits;
                            int i;

                            if ((p > 0 && fmtString[p - 1] == '*') || len > (send - s) * 8)
                            {
                                len = (send - s) * 2;
                            }
                            bits = 0;

                            for (i = 0; i < len; i++)
                            {
                                if ((i & 1) > 0)
                                {
                                    bits >>= 4;
                                }
                                else
                                {
                                    bits = strString[s++];
                                }
                                bitStrBuilder.Append(hexdigits[bits & 15]);
                            }
                            bitstr = new String(bitStrBuilder.ToString());
                            ((Array)ary).value.Add(bitstr);
                        }
                        break;
                    case 'H':
                        {
                            System.Text.StringBuilder bitStrBuilder = new System.Text.StringBuilder();
                            object bitstr;
                            int bits;
                            int i;

                            if ((p > 0 && fmtString[p - 1] == '*') || len > (send - s) * 8)
                            {
                                len = (send - s) * 2;
                            }
                            bits = 0;
                            for (i = 0; i < len; i++)
                            {
                                if ((i & 1) > 0)
                                {
                                    bits <<= 4;
                                }
                                else
                                {
                                    bits = strString[s++];
                                }
                                bitStrBuilder.Append(hexdigits[(bits >> 4) & 15]);
                            }
                            bitstr = new String(bitStrBuilder.ToString());
                            ((Array)ary).value.Add(bitstr);
                        }
                        break;
                    case 'c':
                        tmp = Pack.PACK_LENGTH_ADJUST(s, send, ref len, star, 1);
                        while (len-- > 0)
                        {
                            int c = (byte)strString[s++];
                            if (c > 127) c -= 256; //integer is signed
                            ((Array)ary).value.Add(c);
                        }
                        tmp = Pack.PACK_ITEM_ADJUST(ary, tmp);
                        break;
                    case 'C':
                        tmp = Pack.PACK_LENGTH_ADJUST(s, send, ref len, star, 1);
                        while (len-- > 0)
                        {
                            int c = (byte)strString[s++];
                            ((Array)ary).value.Add(c);
                        }
                        tmp = Pack.PACK_ITEM_ADJUST(ary, tmp);
                        break;
                    case 's':
                        tmp = Pack.PACK_LENGTH_ADJUST(s, send, ref len, star, 2);
                        while (len-- > 0)
                        {
                            short sTmp;
                            sTmp = System.BitConverter.ToInt16(new byte[] { (byte)strString[s], (byte)strString[s + 1] }, 0);
                            s += 2;
                            ((Array)ary).value.Add((int)sTmp);
                        }
                        tmp = Pack.PACK_ITEM_ADJUST(ary, tmp);
                        break;
                    case 'S':
                        tmp = Pack.PACK_LENGTH_ADJUST(s, send, ref len, star, 2);
                        while (len-- > 0)
                        {
                            ushort sTmp;
                            sTmp = System.BitConverter.ToUInt16(new byte[] { (byte)strString[s], (byte)strString[s + 1] }, 0);
                            s += 2;
                            ((Array)ary).value.Add((int)sTmp);
                        }
                        tmp = Pack.PACK_ITEM_ADJUST(ary, tmp);
                        break;
                    case 'i':
                        tmp = Pack.PACK_LENGTH_ADJUST(s, send, ref len, star, 4);
                        while (len-- > 0)
                        {
                            int sTmp;
                            sTmp = System.BitConverter.ToInt32(new byte[] { (byte)strString[s], (byte)strString[s + 1], 
                                (byte)strString[s + 2], (byte)strString[s + 3]}, 0);
                            s += 4;
                            ((Array)ary).value.Add(sTmp);
                        }
                        tmp = Pack.PACK_ITEM_ADJUST(ary, tmp);
                        break;
                    case 'I':
                        tmp = Pack.PACK_LENGTH_ADJUST(s, send, ref len, star, 4);
                        while (len-- > 0)
                        {
                            uint sTmp;
                            sTmp = System.BitConverter.ToUInt32(new byte[] { (byte)strString[s], (byte)strString[s + 1], 
                                (byte)strString[s + 2], (byte)strString[s + 3]}, 0);
                            s += 4;
                            ((Array)ary).value.Add(Bignum.rb_uint2inum(sTmp, caller));
                        }
                        tmp = Pack.PACK_ITEM_ADJUST(ary, tmp);
                        break;
                    case 'l':
                        goto case 'i';
                    case 'L':
                        goto case 'I';
                    case 'q': //signed quadword 
                        tmp = Pack.PACK_LENGTH_ADJUST(s, send, ref len, star, 8);
                        while (len-- > 0)
                        {
                            long qTmp = System.BitConverter.ToInt64(new byte[]{(byte)strString[s],
                                (byte)strString[s + 1], (byte)strString[s + 2], (byte)strString[s + 3], (byte)strString[s + 4],
                                (byte)strString[s + 5], (byte)strString[s + 6], (byte)strString[s + 7]}, 0);
                            s += 8;
                            ((Array)ary).value.Add(Pack.i64_2_num(qTmp, caller));
                        }
                        tmp = Pack.PACK_ITEM_ADJUST(ary, tmp);
                        break;
                    case 'Q': //unsigned quadword 
                        tmp = Pack.PACK_LENGTH_ADJUST(s, send, ref len, star, 8);
                        while (len-- > 0)
                        {
                            ulong QTmp = System.BitConverter.ToUInt64(new byte[]{(byte)strString[s],
                                (byte)strString[s + 1], (byte)strString[s + 2], (byte)strString[s + 3], (byte)strString[s + 4],
                                (byte)strString[s + 5], (byte)strString[s + 6], (byte)strString[s + 7]}, 0);
                            s += 8;
                            ((Array)ary).value.Add(Pack.ui64_2_num(QTmp, caller));
                        }
                        break;
                    case 'n':
                        tmp = Pack.PACK_LENGTH_ADJUST(s, send, ref len, star, 2);
                        while (len-- > 0)
                        {
                            ushort nTmp = 0;
                            byte[] nTmpArray = new byte[] { (byte)strString[s], (byte)strString[s + 1] };
                            if (!Pack.isBigEndian())
                            {
                                Pack.swap(nTmpArray);
                            }
                            nTmp = System.BitConverter.ToUInt16(nTmpArray, 0);
                            s += 2;
                            ((Array)ary).value.Add((int)nTmp);
                        }
                        tmp = Pack.PACK_ITEM_ADJUST(ary, (int)tmp);
                        break;
                    case 'N':
                        tmp = Pack.PACK_LENGTH_ADJUST(s, send, ref len, star, 4);
                        while (len-- > 0)
                        {
                            uint NTmp = 0;
                            byte[] NTmpArray = new byte[] { (byte)strString[s], (byte)strString[s + 1],
                                (byte)strString[2], (byte)strString[3]};
                            if (!Pack.isBigEndian())
                            {
                                Pack.swap(NTmpArray);
                            }
                            NTmp = System.BitConverter.ToUInt32(NTmpArray, 0);
                            s += 4;
                            ((Array)ary).value.Add(Bignum.rb_uint2inum(NTmp, caller));
                        }
                        tmp = Pack.PACK_ITEM_ADJUST(ary, (int)tmp);
                        break;
                    case 'v':
                        tmp = Pack.PACK_LENGTH_ADJUST(s, send, ref len, star, 2);
                        while (len-- > 0)
                        {
                            ushort vTmp = 0;
                            byte[] vTmpArray = new byte[] { (byte)strString[s], (byte)strString[s + 1] };
                            if (Pack.isBigEndian())
                            {
                                Pack.swap(vTmpArray);
                            }
                            vTmp = System.BitConverter.ToUInt16(vTmpArray, 0);
                            s += 2;
                            ((Array)ary).value.Add((int)vTmp);
                        }
                        tmp = Pack.PACK_ITEM_ADJUST(ary, (int)tmp);
                        break;
                    case 'V':
                        tmp = Pack.PACK_LENGTH_ADJUST(s, send, ref len, star, 4);
                        while (len-- > 0)
                        {
                            uint VTmp = 0;
                            byte[] VTmpArray = new byte[] { (byte)strString[s], (byte)strString[s + 1], 
                                (byte)strString[s + 2], (byte)strString[s + 3]};
                            if (Pack.isBigEndian())
                            {
                                Pack.swap(VTmpArray);
                            }

                            VTmp = System.BitConverter.ToUInt32(VTmpArray, 0);
                            s += 4;
                            ((Array)ary).value.Add(Bignum.rb_uint2inum(VTmp, caller));
                        }
                        tmp = Pack.PACK_ITEM_ADJUST(ary, (int)tmp);
                        break;
                    case 'f':
                        goto case 'F';
                    case 'F':
                        tmp = Pack.PACK_LENGTH_ADJUST(s, send, ref len, star, 4);
                        while (len-- > 0)
                        {
                            float FTmp;
                            FTmp = System.BitConverter.ToSingle(new byte[] { (byte)strString[s], (byte)strString[s + 1], 
                                (byte)strString[s + 2], (byte)strString[s + 3] }, 0);
                            s += 4;
                            ((Array)ary).value.Add(new Float(FTmp)); 
                        }
                        tmp = Pack.PACK_ITEM_ADJUST(ary, (int)tmp);
                        break;
                    case 'e':
                        tmp = Pack.PACK_LENGTH_ADJUST(s, send, ref len, star, 4);
                        while (len-- > 0)
                        {
                            float eTmp;
                            byte[] eTmpArray = new byte[]{ (byte)strString[s], (byte)strString[s + 1],
                                (byte)strString[s + 2], (byte)strString[s + 3]};
                            if (Pack.isBigEndian())
                            {
                                Pack.swap(eTmpArray);
                            }
                            s += 4;
                            eTmp = System.BitConverter.ToSingle(eTmpArray, 0);
                            ((Array)ary).value.Add(new Float(eTmp));
                        }
                        tmp = Pack.PACK_ITEM_ADJUST(ary, (int)tmp);
                        break;
                    case 'E':
                        tmp = Pack.PACK_LENGTH_ADJUST(s, send, ref len, star, 8);
                        while (len-- > 0)
                        {
                            double ETmp;
                            byte[] ETmpArray = new byte[]{(byte)strString[s],
                                (byte)strString[s + 1], (byte)strString[s + 2], (byte)strString[s + 3], (byte)strString[s + 4],
                                (byte)strString[s + 5], (byte)strString[s + 6], (byte)strString[s + 7]};
                            if (Pack.isBigEndian())
                            {
                                Pack.swap(ETmpArray);
                            }
                            s += 8;
                            ETmp = System.BitConverter.ToDouble(ETmpArray, 0);
                            ((Array)ary).value.Add(new Float(ETmp));
                        }
                        tmp = Pack.PACK_ITEM_ADJUST(ary, (int)tmp);
                        break;
                    case 'D':
                        goto case 'd';
                    case 'd':
                        tmp = Pack.PACK_LENGTH_ADJUST(s, send, ref len, star, 8);
                        while (len-- > 0)
                        {
                            double dTmp;
                            byte[] dTmpArray = new byte[]{(byte)strString[s],
                                (byte)strString[s + 1], (byte)strString[s + 2], (byte)strString[s + 3], (byte)strString[s + 4],
                                (byte)strString[s + 5], (byte)strString[s + 6], (byte)strString[s + 7]};
                            s += 8;
                            dTmp = System.BitConverter.ToDouble(dTmpArray, 0);
                            ((Array)ary).value.Add(new Float(dTmp));
                        }
                        tmp = Pack.PACK_ITEM_ADJUST(ary, (int)tmp);
                        break;
                    case 'g':
                        tmp = Pack.PACK_LENGTH_ADJUST(s, send, ref len, star, 4);
                        while (len-- > 0)
                        {
                            float gTmp;
                            byte[] gTmpArray = new byte[]{ (byte)strString[s], (byte)strString[s + 1],
                                (byte)strString[s + 2], (byte)strString[s + 3]};
                            if (!Pack.isBigEndian())
                            {
                                Pack.swap(gTmpArray);
                            }
                            s += 4;
                            gTmp = System.BitConverter.ToSingle(gTmpArray, 0);
                            ((Array)ary).value.Add(new Float(gTmp));
                        }
                        tmp = Pack.PACK_ITEM_ADJUST(ary, (int)tmp);
                        break;
                    case 'G':
                        {
                            tmp = Pack.PACK_LENGTH_ADJUST(s, send, ref len, star, 8);
                            while (len-- > 0)
                            {
                                double GTmp;
                                byte[] GTmpArray = new byte[]{(byte)strString[s],
                                (byte)strString[s + 1], (byte)strString[s + 2], (byte)strString[s + 3], (byte)strString[s + 4],
                                (byte)strString[s + 5], (byte)strString[s + 6], (byte)strString[s + 7]};
                                if (!Pack.isBigEndian())
                                {
                                    Pack.swap(GTmpArray);
                                }
                                s += 8;
                                GTmp = System.BitConverter.ToDouble(GTmpArray, 0);
                                ((Array)ary).value.Add(new Float(GTmp));
                            }
                            tmp = Pack.PACK_ITEM_ADJUST(ary, (int)tmp);
                            break;
                        }
                    case 'U': 
                        if (len > send - s) len = send - s;
                        while (len > 0 && s < send)
                        {
                            int alen = send - s;
                            uint l;

                            l = (uint)Pack.utf8_to_uv(caller, strString,  s, ref alen);
                            s += alen; len--;
                            ((Array)ary).value.Add(Bignum.rb_uint2inum(l, caller));                           
                        }
                        break;
                    case 'u':
                        {


                            object buf = String.infected_str_new(0, (send - s) * 3/4, strString);
                            string ptrString = ((String)buf).value;
                            System.Text.StringBuilder ptrStringBuilder = new System.Text.StringBuilder(ptrString);
                            int ptr = 0;
                            int total = 0;

                            while (s < send && strString[s] > ' ' && strString[s] < 'a')
                            {

                                int a, b, c, d;
                                char[] hunk = new char[4];
                                
                                hunk[3] = '\0';

                                len = (strString[s++] - ' ') & 0x3f;
                                total += len;
                                if (total > ((String)buf).value.Length)
                                {
                                    len -= total - ((String)buf).value.Length;
                                    total = ((String)buf).value.Length;
                                }
                                while (len > 0)
                                {
                                    int mlen = len > 3 ? 3 : len;

                                    if (s < send && strString[s] >= ' ')
                                        a = (strString[s++] - ' ') & 0x3f;
                                    else
                                        a = 0;
                                    if (s < send && strString[s] >= ' ')
                                        b = (strString[s++] - ' ') & 0x3f;
                                    else
                                        b = 0;
                                    if (s < send && strString[s] >= ' ')
                                        c = (strString[s++] - ' ') & 0x3f;
                                    else
                                        c = 0;
                                    if (s < send && strString[s] >= ' ')
                                        d = (strString[s++] - ' ') & 0x3f;
                                    else
                                        d = 0;
                                    hunk[0] = (char)(byte)(a << 2 | b >> 4);
                                    hunk[1] = (char)(byte)(b << 4 | c >> 2);
                                    hunk[2] = (char)(byte)(c << 6 | d);
                                    for(int j = 0; j < mlen; j++){
                                        ptrStringBuilder[ptr + j] = hunk[j];
                                    }
                                    ptr += mlen;
                                    len -= mlen;                                    
                                }
                                if (s < send && strString[s] == '\r') s++;
                                if (s < send && strString[s] == '\n') s++;
                                else if(s < send && (s + 1 == send || strString[s + 1] == '\n')){
                                    s += 2; /* possible checksum byte */
                                }
                            }

                            ptrStringBuilder.Length = total;
                            ((String)buf).value = ptrStringBuilder.ToString();
                            ((Array)ary).value.Add(buf);
                        }
                        break;
                    case 'm':
                        {
                            object buf = String.infected_str_new(0, (send - s) * 3 / 4, strString);
                            System.Text.StringBuilder ptrString = new System.Text.StringBuilder(((String)buf).value);
                            int ptr = 0;
                            int a = -1, b = -1, c = 0, d = 0;

                            if (first > 0)
                            {
                                int i;
                                first = 0;

                                for (i = 0; i < 256; i++)
                                {
                                    b64_xtable[i] = -1;
                                }
                                for (i = 0; i < 64; i++)
                                {
                                    b64_xtable[(int)Pack.b64_table[i]] = i;
                                }
                            }
                            while (s < send)
                            {
                                while (strString[s] == '\r' || strString[s] == '\n') { s++; }
                                if ((a = b64_xtable[(int)strString[s]]) == -1) break;
                                if (!(s+1 < send) || (b = b64_xtable[(int)strString[s + 1]]) == -1) break;
                                if (!(s+2 < send) || (c = b64_xtable[(int)strString[s + 2]]) == -1) break;
                                if (!(s+3 < send) || (d = b64_xtable[(int)strString[s + 3]]) == -1) break;
                                ptrString[ptr++] = (char)(byte)(a << 2 | b >> 4);
                                ptrString[ptr++] = (char)(byte)(b << 4 | c >> 2);
                                ptrString[ptr++] = (char)(byte)(c << 6 | d);
                                s += 4;
                            }
                            if (a != -1 && b != -1)
                            {
                                if (s + 2 < send && strString[ptr++] == '=')
                                    ptrString[ptr++] = (char)(byte)(a << 2 | b >> 4);
                                if (c != -1 && s + 3 < send && strString[s + 3] == '=')
                                {
                                    ptrString[ptr++] = (char)(byte)(a << 2 | b >> 4);
                                    ptrString[ptr++] = (char)(byte)(b << 4 | c >> 2);
                                }
                            }
                            ptrString.Length = ptr;
                            ((String)buf).value = ptrString.ToString();
                            ((Array)ary).value.Add(buf);
                        }
                        break;
                    case 'M':
                        {
                            object buf = String.infected_str_new(0, send - s, strString);
                            System.Text.StringBuilder ptrString = new System.Text.StringBuilder(((String)buf).value);
                            int ptr = 0;
                            int c1, c2;

                            while(s < send){
                                if (strString[s] == '=')
                                {
                                    if (++s == send) break;
                                    if (strString[s] != '\n')
                                    {
                                        if ((c1 = Pack.hex2num(strString[s])) == -1) break;
                                        if (++s == send) break;
                                        if ((c2 = Pack.hex2num(strString[s])) == -1) break;
                                        ptrString[ptr++] = (char)(byte)(c1 << 4 | c2);
                                    }
                                }
                                else
                                {
                                    ptrString[ptr++] = strString[s];
                                }
                                s++;
                            }
                            ptrString.Length = ptr;
                            ((String)buf).value = ptrString.ToString();
                            ((Array)ary).value.Add(buf);
                        }
                        break;
                    case 'X':
                        if (len > s)
                            throw new ArgumentError("X outside of string").raise(caller);
                        s -= len;
                        break;
                    case 'x':
                        if (len > send - s)
                            throw new ArgumentError("X outside of string").raise(caller);
                        s += len;
                        break;
                    case 'P':        /* pointer to packed byte string */
                        throw new NotImplementedError("The pack(\"P\") method is not implemented on this platform").raise(caller);
                    case 'p':        /* pointer to string */
                        throw new NotImplementedError("The pack(\"p\") method is not implemented on this platform").raise(caller);
                    case 'w':
                        {
                            uint ul = 0;
                            uint ulmask = ((uint)0xfe) << 4 - 1 * 8;

                            while (len > 0 && s < send)
                            {
                                ul <<= 7;
                                ul |= (byte)(strString[s] & 0x7f);

                                if (!((strString[s++] & 0x80) > 0))
                                {
                                    ((Array)ary).value.Add(Bignum.rb_uint2inum(ul, caller));
                                    len--;
                                    ul = 0;
                                }
                                else if ((ul & ulmask) > 0) {
                                    object big = new Bignum(ul);
                                    object big128 = new Bignum(128);
                                    while (s < send)
                                    {
                                        big = Ruby.Methods.rb_big_mul.singleton.Call1(last_class, big, caller, null, big128);
                                        big = Ruby.Methods.rb_big_plus.singleton.Call1(last_class, big, caller, null, Bignum.rb_uint2inum((byte)(strString[s] & 0x7f), caller));
                                    }
                                    if (!((strString[s++] & 0x80) > 0))
                                    {
                                        ((Array)ary).value.Add(big);
                                        len--;
                                        ul = 0;
                                        break;
                                    }
                                }
                            }
                        }
                        break;
                    default:
                        break;
                }
            }
            return ary;
        }
Пример #30
0
 public override object Call(Class last_class, object str, Frame caller, Proc block, Array rest)
 {
     string newValue = ((String)str).value;
     str = new String(newValue);
     rb_str_delete_bang.singleton.Call(last_class, str, caller, block, rest);
     return str;
 }