示例#1
0
VALUE t_do(VALUE self, VALUE in, VALUE out, VALUE size)
{
	rb_check_type(in, T_FILE);
	rb_check_type(out, T_FILE);
	FILE *source;
	if (rb_funcall(rb_eval_string("Socket"), rb_intern("=="), 1, rb_class_of(in)) == Qtrue)
		source= RFILE(in)->fptr->f2;
	else
		source= RFILE(in)->fptr->f;
	FILE *destination;
	if (rb_funcall(rb_eval_string("Socket"), rb_intern("=="), 1, rb_class_of(out)) == Qtrue)
		destination= RFILE(out)->fptr->f2;
	else
		destination= RFILE(out)->fptr->f;
	VALUE result= INT2NUM(0);
	char buffer[1024];
	while ((size == Qnil) || (rb_funcall(result, rb_intern("<"), 1, size) == Qtrue)) {
		size_t done= fread(buffer, 1, sizeof(buffer), source);
		if (ferror(source)) {
			rb_sys_fail("io2io_do(read)");
		}
		fwrite(buffer, 1, done, destination);
		if (ferror(destination)) {
			rb_sys_fail("io2io_do(write)");
		}
		result= rb_funcall(result, rb_intern("+"), 1, INT2NUM(done));
		if (done < sizeof(buffer)) {
			return result;
		}
	}
	return result;
}
示例#2
0
/*
 * call-seq:
 *     session.userauth_password("username", "password")
 *
 * Attempts to authenticate using a username and password.
 *
 * */
static VALUE
userauth_password(VALUE self, VALUE username, VALUE password) {
    int result;
    rb_check_type(username, T_STRING);
    rb_check_type(password, T_STRING);

    result = libssh2_userauth_password(
            get_session(self),
            StringValuePtr(username),
            StringValuePtr(password));
    HANDLE_LIBSSH2_RESULT(result);
}
示例#3
0
static VALUE geographiclib_local_cartesian_reverse(int argc, VALUE *argv, VALUE self)
{
  GeographicLib::Math::real lat, lon, h, x, y, z = 0.0;
  switch (argc) {
  default:
    rb_raise(rb_eArgError, "Wrong number of arguments. Expecting 1 or 3.");
  case 3:
    x = rb_num2dbl(argv[0]);
    y = rb_num2dbl(argv[1]);
    z = rb_num2dbl(argv[2]);
    break;
  case 1:
    rb_check_type(argv[0], T_HASH);
    x = rb_num2dbl(rb_hash_lookup(argv[0], rb_symbol("x")));
    y = rb_num2dbl(rb_hash_lookup(argv[0], rb_symbol("y")));
    z = rb_num2dbl(rb_hash_lookup(argv[0], rb_symbol("z")));
    break;
  }
  GeographicLib::LocalCartesian *lc = geographiclib_local_cartesian_get(self);
  lc->Reverse(x, y, z, lat, lon, h);
  VALUE r = rb_hash_new();
  rb_hash_aset(r, rb_symbol("lat"), rb_float_new(lat));
  rb_hash_aset(r, rb_symbol("lon"), rb_float_new(lon));
  rb_hash_aset(r, rb_symbol("h"), rb_float_new(h));
  return r;
}
示例#4
0
static VALUE geographiclib_local_cartesian_forward(int argc, VALUE *argv, VALUE self)
{
  GeographicLib::Math::real lat = 0.0, lon = 0.0, h = 0.0, x, y, z;
  switch (argc) {
  default:
    rb_raise(rb_eArgError, "Wrong number of arguments. Expecting 1, 2 or 3.");
  case 3:
    h = rb_num2dbl(argv[2]);
  case 2:
    decode_lat_lon(argv[0], argv[1], lat, lon);
    break;
  case 1:
    rb_check_type(argv[0], T_HASH);
    decode_lat_lon(rb_hash_lookup(argv[0], rb_symbol("lat")), rb_hash_lookup(argv[0], rb_symbol("lon")), lat, lon);
    VALUE vh = rb_hash_lookup(argv[0], rb_symbol("h"));
    if (vh != Qnil) {
      h = rb_num2dbl(vh);
    }
    break;
  }
  GeographicLib::LocalCartesian *lc = geographiclib_local_cartesian_get(self);
  lc->Forward(lat, lon, h, x, y, z);
  VALUE r = rb_hash_new();
  rb_hash_aset(r, rb_symbol("x"), rb_float_new(x));
  rb_hash_aset(r, rb_symbol("y"), rb_float_new(y));
  rb_hash_aset(r, rb_symbol("z"), rb_float_new(z));
  return r;
}
示例#5
0
static VALUE geographiclib_local_cartesian_initialize(int argc, VALUE *argv, VALUE self)
{
  GeographicLib::Math::real lat = 0.0, lon = 0.0, h = 0.0;
  switch (argc) {
  default:
    rb_raise(rb_eArgError, "Wrong number of arguments. Expecting 0, 1, 2 or 3.");
  case 3:
    h = rb_num2dbl(argv[2]);
  case 2:
    decode_lat_lon(argv[0], argv[1], lat, lon);
  case 0:
    break;
  case 1:
    rb_check_type(argv[0], T_HASH);
    decode_lat_lon(rb_hash_lookup(argv[0], rb_symbol("lat")), rb_hash_lookup(argv[0], rb_symbol("lon")), lat, lon);
    VALUE vh = rb_hash_lookup(argv[0], rb_symbol("h"));
    if (vh != Qnil) {
      h = rb_num2dbl(vh);
    }
    break;
  }
  geographiclib_local_cartesian_free(DATA_PTR(self));
  DATA_PTR(self) = new GeographicLib::LocalCartesian(lat, lon, h);
  return self;
}
示例#6
0
文件: rcovrt.c 项目: CoralineAda/rcov
static struct cov_array * coverage_increase_counter_uncached(char *sourcefile, unsigned int sourceline, char mark_only) {
  struct cov_array *carray = NULL;
 
  if(sourcefile == NULL) {
    /* "can't happen", just ignore and avoid segfault */
    return NULL;
  } 
  else if(!st_lookup(coverinfo, (st_data_t)sourcefile, (st_data_t*)&carray)) {
    VALUE arr;
    
    arr = rb_hash_aref(oSCRIPT_LINES__, rb_str_new2(sourcefile));
    if(NIL_P(arr)) 
      return 0;
    rb_check_type(arr, T_ARRAY);
    carray = calloc(1, sizeof(struct cov_array));
    carray->ptr = calloc(RARRAY(arr)->len, sizeof(unsigned int));
    carray->len = RARRAY(arr)->len;
    st_insert(coverinfo, (st_data_t)strdup(sourcefile), (st_data_t) carray);
  } 
  else {
    /* recovered carray, sanity check */
    assert(carray && "failed to create valid carray");
  }

  if(mark_only) {
    if(!carray->ptr[sourceline])
      carray->ptr[sourceline] = 1;
  } else {
    if (carray && carray->len > sourceline) {
      carray->ptr[sourceline]++;
    }
  }

  return carray;
}
示例#7
0
static char* proxenet_ruby_execute_function(interpreter_t* interpreter, ID rFunc, request_t* request)
{
	char *buf, *data;
	int buflen, i;
	VALUE rRet;
	char *uri;

        struct proxenet_ruby_args args;

	uri = request->http_infos.uri;
	if (!uri)
		return NULL;

	/* build args */
	args.rVM = (VALUE)interpreter->vm;

        args.rFunc = rFunc;

	args.rArgs[0] = INT2NUM(request->id);
	args.rArgs[1] = rb_str_new(request->data, request->size);
	args.rArgs[2] = rb_str_new2(uri);

        for(i=0; i<3; i++) {
                rb_gc_register_address(&args.rArgs[i]);
	}

	/* safe function call */
        rRet = (VALUE)_safe_call_func(&args);
	if (!rRet) {
		xlog(LOG_ERROR, "%s\n", "[ruby] funcall2() failed");
		data = NULL;
		goto call_end;
	}

	rb_check_type(rRet, T_STRING);

        for(i=0; i<3; i++) {
                rb_gc_unregister_address(&args.rArgs[i]);
	}


	/* copy result to exploitable buffer */
	buf = RSTRING_PTR(rRet);
	buflen = RSTRING_LEN(rRet);

	data = proxenet_xmalloc(buflen + 1);
	data = memcpy(data, buf, buflen);

	request->data = data;
	request->size = buflen;

call_end:

	return data;
}
示例#8
0
/*
 * call-seq:
 *     session.userauth_publickey_fromfile("username", "/etc/key.pub", "/etc/key", "foo")
 *
 * Attempts to authenticate using public and private keys from files.
 *
 * */
static VALUE
userauth_publickey_fromfile(VALUE self,
        VALUE username,
        VALUE publickey_path,
        VALUE privatekey_path,
        VALUE passphrase) {
    int result;
    rb_check_type(username, T_STRING);
    rb_check_type(publickey_path, T_STRING);
    rb_check_type(privatekey_path, T_STRING);
    rb_check_type(passphrase, T_STRING);

    result = libssh2_userauth_publickey_fromfile(
            get_session(self),
            StringValuePtr(username),
            StringValuePtr(publickey_path),
            StringValuePtr(privatekey_path),
            StringValuePtr(passphrase));
    HANDLE_LIBSSH2_RESULT(result);
}
示例#9
0
VALUE rb_Fonts_load_font(VALUE self, VALUE id, VALUE str)
{
    unsigned long position = normalize_long(rb_num2long(id), 0, 255);
    rb_check_type(str, T_STRING);
    while(rb_Fonts_font_tbl.size() <= position)
    {
        rb_Fonts_font_tbl.push_back(sf::Font());
    }
    rb_Fonts_font_tbl[position].loadFromFile(RSTRING_PTR(str));
    return self;
}
示例#10
0
static void add_class_variables(VALUE module, VALUE class_variables)
{
  rb_check_type(class_variables, T_HASH);
#ifdef RUBY_VM
  if(RHASH(class_variables)->ntbl)
  {
    st_foreach(RHASH(class_variables)->ntbl, set_cvar_from_hash, module);
  }
#else
  st_foreach(RHASH(class_variables)->tbl, set_cvar_from_hash, module);
#endif
}
示例#11
0
static void add_methods(VALUE module, VALUE methods)
{
  rb_check_type(methods, T_HASH);
#ifdef RUBY_VM
  if(RHASH(methods)->ntbl)
  {
    st_foreach(RHASH(methods)->ntbl, add_method_iter, module);
  }
#else
  st_foreach(RHASH(methods)->tbl, add_method_iter, module);
#endif
}
示例#12
0
static void include_modules(VALUE module, VALUE included_modules)
{
  long j;
  VALUE v;
  VALUE name;

  rb_check_type(included_modules, T_ARRAY);
  for(j = 0; j < RARRAY_LEN(included_modules); ++j)
  {
    name = RARRAY_PTR(included_modules)[j];
    v = lookup_module(name);
    rb_funcall(module, rb_intern("include"), 1, v);
  }
}
示例#13
0
static int add_method_iter(VALUE name, VALUE value, VALUE module)
{
  NODE * n;
  rb_check_type(name, T_SYMBOL);
  if(!rb_obj_is_kind_of(value, rb_cNode))
  {
    rb_raise(
        rb_eTypeError,
        "Expected Node, but got %s",
        rb_class2name(CLASS_OF(value)));
  }
  Data_Get_Struct(value, NODE, n);
#if RUBY_VERSION_CODE >= 192
  rb_raise(rb_eRuntimeError, "NOT SUPPORTED");
#else
  rb_add_method(module, SYM2ID(name), n->nd_body, n->nd_noex);
#endif
  return ST_CONTINUE;
}
示例#14
0
static struct cov_array * coverage_increase_counter_uncached(char *sourcefile, unsigned int sourceline, char mark_only) {
    struct cov_array *carray = NULL;

    if (sourcefile == NULL) {
        return NULL;
    } else if (!st_lookup(coverinfo, (st_data_t)sourcefile, (st_data_t*)&carray)) {
        VALUE arr;
        arr = rb_hash_aref(oSCRIPT_LINES__, rb_str_new2(sourcefile));

        if (NIL_P(arr)) return 0;

        rb_check_type(arr, T_ARRAY);
        carray = calloc(1, sizeof(struct cov_array));
        carray->ptr = calloc(RARRAY(arr)->len, sizeof(unsigned int));
        carray->len = RARRAY(arr)->len;
        st_insert(coverinfo, (st_data_t)strdup(sourcefile), (st_data_t) carray);
    } else {
        assert(carray && "failed to create valid carray");
    }

    if (mark_only) {
        if (carray && carray->len > sourceline) {
            if (!carray->ptr[sourceline])
                carray->ptr[sourceline] = 1;
        } else {
#if COVERAGE_DEBUG_EVENTS
            printf("DEBUG: %s carray->len:%d sourceline:%d\n", sourcefile, carray->len, sourceline);
#endif
        }
    } else {
        if (carray && carray->len > sourceline) {
            carray->ptr[sourceline]++;
        }
    }

    return carray;
}
示例#15
0
/*
 * call-seq:
 *   Module.load(String) => Module
 *
 * Load a module from a string.
 */
static VALUE module_load(VALUE klass, VALUE str)
{
  VALUE arr, class_name, metaclass_str, metaclass, superclass_name,
        included_modules, class_variables_str, class_variables,
        instance_methods_str, instance_methods, flags, module;

  if(   rb_safe_level() >= 4
     || (rb_safe_level() >= 1 && OBJ_TAINTED(str)))
  {
    /* no playing with knives in the sandbox */
    rb_raise(rb_eSecurityError, "Insecure: can't load module");
  }

  arr = marshal_load(str);
  class_name = rb_ary_pop(arr);
  metaclass_str = rb_ary_pop(arr);
  superclass_name = rb_ary_pop(arr);
  included_modules = rb_ary_pop(arr);
  class_variables_str = rb_ary_pop(arr);
  instance_methods_str = rb_ary_pop(arr);
  flags = rb_ary_pop(arr);

  if(RTEST(superclass_name))
  {
    VALUE superclass;
    rb_check_type(superclass_name, T_STRING);
    superclass = rb_funcall(
        lookup_module_proc,
        rb_intern("call"),
        1,
        superclass_name);
#if RUBY_VERSION_CODE >= 180
    /* Can't make subclass of Class on 1.8.x */
    module = rb_class_boot(superclass);
    rb_define_alloc_func(module, module_instance_allocate);
#else
    module = rb_class_new(superclass);
#endif
  }
  else
  {
    module = rb_module_new();
  }

  if(!NIL_P(class_name))
  {
    VALUE outer_module = rb_funcall(outer_module_proc, rb_intern("call"), 1, class_name);
    VALUE module_name = rb_funcall(module_name_proc, rb_intern("call"), 1, class_name);
    rb_const_set(outer_module, SYM2ID(module_name), module);
  }

  RBASIC(module)->flags = NUM2INT(flags);
  include_modules(module, included_modules);
  class_variables = marshal_load(class_variables_str);
  add_class_variables(module, class_variables);
  instance_methods = marshal_load(instance_methods_str);
  add_methods(module, instance_methods);

  metaclass = marshal_load(metaclass_str);
  if(RTEST(metaclass))
  {
    rb_singleton_class_attached(metaclass, module);
    RBASIC(module)->klass = metaclass;
  }

  return module;
}
示例#16
0
static VALUE m_initialize(VALUE self, VALUE string) {
	rb_check_type(string, T_STRING);
	rb_iv_set(self, "@expression", string);
	rb_iv_set(self, "@ast", Qnil);
	return self;
}