Exemplo n.º 1
0
static VALUE
rg_initialize_copy(VALUE self, VALUE orig)
{
    boxed_holder *holder1;
    boxed_holder *holder2;

    if (self == orig) return self;

    if (!rb_obj_is_instance_of(orig, rb_obj_class(self))) {
        rb_raise(rb_eTypeError, "wrong argument class");
    }

    Data_Get_Struct(self, boxed_holder, holder1);
    Data_Get_Struct(orig, boxed_holder, holder2);

    holder1->boxed = g_boxed_copy(holder2->type, holder2->boxed);
    holder1->own   = TRUE;

    if (!holder1->boxed)
      rb_raise(rb_eRuntimeError, "g_boxed_copy() failed");

    return self;
}
Exemplo n.º 2
0
/*
 *  call-seq:
 *     ctx.flush_sessions(time | nil) -> self
 *
 */
static VALUE
ossl_sslctx_flush_sessions(int argc, VALUE *argv, VALUE self)
{
    VALUE arg1;
    SSL_CTX *ctx;
    time_t tm = 0;
    int cb_state;

    rb_scan_args(argc, argv, "01", &arg1);

    Data_Get_Struct(self, SSL_CTX, ctx);

    if (NIL_P(arg1)) {
        tm = time(0);
    } else if (rb_obj_is_instance_of(arg1, rb_cTime)) {
        tm = NUM2LONG(rb_funcall(arg1, rb_intern("to_i"), 0));
    } else {
        rb_raise(rb_eArgError, "arg must be Time or nil");
    }

    SSL_CTX_flush_sessions(ctx, (long)tm);

    return self;
}
Exemplo n.º 3
0
/*
 * call-seq: put(pointer, value)
 * @param [AbstractMemory] pointer pointer on a {Struct}
 * @param [String, Array] value +value+ may be a String only if array's type is a kind of +int8+
 * @return [value]
 * Set an array in a {Struct}.
 */
static VALUE
array_field_put(VALUE self, VALUE pointer, VALUE value)
{
    StructField* f;
    ArrayType* array;
    

    Data_Get_Struct(self, StructField, f);
    Data_Get_Struct(f->rbType, ArrayType, array);
    
    if (isCharArray(array) && rb_obj_is_instance_of(value, rb_cString)) {
        VALUE argv[2];

        argv[0] = INT2FIX(f->offset);
        argv[1] = value;

        rb_funcall2(pointer, rb_intern("put_string"), 2, argv);

    } else {
#ifdef notyet
        MemoryOp* op;
        int count = RARRAY_LEN(value);
        int i;
        AbstractMemory* memory = MEMORY(pointer);

        if (count > array->length) {
            rb_raise(rb_eIndexError, "array too large");
        }

        /* clear the contents in case of a short write */
        checkWrite(memory);
        checkBounds(memory, f->offset, f->type->ffiType->size);
        if (count < array->length) {
            memset(memory->address + f->offset + (count * array->componentType->ffiType->size),
                    0, (array->length - count) * array->componentType->ffiType->size);
        }

        /* now copy each element in */
        if ((op = get_memory_op(array->componentType)) != NULL) {

            for (i = 0; i < count; ++i) {
                (*op->put)(memory, f->offset + (i * array->componentType->ffiType->size), rb_ary_entry(value, i));
            }

        } else if (array->componentType->nativeType == NATIVE_STRUCT) {

            for (i = 0; i < count; ++i) {
                VALUE entry = rb_ary_entry(value, i);
                Struct* s;

                if (!rb_obj_is_kind_of(entry, rbffi_StructClass)) {
                    rb_raise(rb_eTypeError, "array element not an instance of FFI::Struct");
                    break;
                }

                Data_Get_Struct(entry, Struct, s);
                checkRead(s->pointer);
                checkBounds(s->pointer, 0, array->componentType->ffiType->size);

                memcpy(memory->address + f->offset + (i * array->componentType->ffiType->size),
                        s->pointer->address, array->componentType->ffiType->size);
            }

        } else {
            rb_raise(rb_eNotImpError, "put not supported for arrays of type %s", rb_obj_classname(array->rbComponentType));
        }
#else
        rb_raise(rb_eNotImpError, "cannot set array field");
#endif
    }

    return value;
}
Exemplo n.º 4
0
static int
error_handle(int ex)
{
    int status = EXIT_FAILURE;
    rb_thread_t *th = GET_THREAD();

    if (rb_threadptr_set_raised(th))
	return EXIT_FAILURE;
    switch (ex & TAG_MASK) {
      case 0:
	status = EXIT_SUCCESS;
	break;

      case TAG_RETURN:
	error_pos();
	warn_print("unexpected return\n");
	break;
      case TAG_NEXT:
	error_pos();
	warn_print("unexpected next\n");
	break;
      case TAG_BREAK:
	error_pos();
	warn_print("unexpected break\n");
	break;
      case TAG_REDO:
	error_pos();
	warn_print("unexpected redo\n");
	break;
      case TAG_RETRY:
	error_pos();
	warn_print("retry outside of rescue clause\n");
	break;
      case TAG_THROW:
	/* TODO: fix me */
	error_pos();
	warn_print("unexpected throw\n");
	break;
      case TAG_RAISE: {
	VALUE errinfo = th->errinfo;
	if (rb_obj_is_kind_of(errinfo, rb_eSystemExit)) {
	    status = sysexit_status(errinfo);
	}
	else if (rb_obj_is_instance_of(errinfo, rb_eSignal) &&
		 rb_ivar_get(errinfo, id_signo) != INT2FIX(SIGSEGV)) {
	    /* no message when exiting by signal */
	}
	else {
	    error_print(th);
	}
	break;
      }
      case TAG_FATAL:
	error_print(th);
	break;
      default:
	unknown_longjmp_status(ex);
	break;
    }
    rb_threadptr_reset_raised(th);
    return status;
}
Exemplo n.º 5
0
Arquivo: icon.c Projeto: guns/subtle
VALUE
subIconCopyArea(int argc,
  VALUE *argv,
  VALUE self)
{
  VALUE data[7] = { Qnil };

  rb_scan_args(argc, argv, "16", &data[0], &data[1], &data[2], &data[3],
    &data[4], &data[5], &data[6]);

  /* Check object type */
  if(rb_obj_is_instance_of(data[0],
      rb_const_get(mod, rb_intern("Icon"))))
    {
      SubtlextIcon *src = NULL, *dest = NULL;

      Data_Get_Struct(data[0], SubtlextIcon, src);
      Data_Get_Struct(self,    SubtlextIcon, dest);

      if(src && dest)
        {
          int src_x = 0, src_y = 0, dest_x = 0, dest_y = 0;
          int iwidth = 0, iheight = 0, area_w = 0, area_h = 0;
          VALUE width = Qnil, height = Qnil;

          /* Get icon dimesions */
          GET_ATTR(self, "@width",  width);
          GET_ATTR(self, "@height", height);

          iwidth  = FIX2INT(width);
          iheight = FIX2INT(height);

          /* Check args */
          if(!NIL_P(data[1])) src_x  = FIX2INT(data[1]);
          if(!NIL_P(data[2])) src_y  = FIX2INT(data[2]);
          if(!NIL_P(data[3])) area_w = FIX2INT(data[3]);
          if(!NIL_P(data[4])) area_h = FIX2INT(data[4]);
          if(!NIL_P(data[5])) dest_x = FIX2INT(data[5]);
          if(!NIL_P(data[6])) dest_y = FIX2INT(data[6]);

          /* Sanitize args */
          if(0 == area_w) area_w = iwidth;
          if(0 == area_h) area_h = iheight;

          if(area_w > dest_x + iwidth)  area_w = iwidth  - dest_x;
          if(area_h > dest_y + iheight) area_h = iheight - dest_y;

          if(0 > src_x  || src_x  > iwidth)  src_x  = 0;
          if(0 > src_y  || src_y  > iheight) src_y  = 0;
          if(0 > dest_x || dest_x > iwidth)  dest_x = 0;
          if(0 > dest_y || dest_y > iheight) dest_y = 0;

          /* Create on demand */
          if(0 == dest->gc)
            dest->gc = XCreateGC(display, dest->pixmap, 0, NULL);

          /* Copy area */
          if(src->flags & ICON_PIXMAP && dest->flags & ICON_PIXMAP)
            {
              XCopyPlane(display, src->pixmap, dest->pixmap, dest->gc,
                src_x, src_y, area_w, area_h, dest_x, dest_y, 1);
            }
          else XCopyArea(display, src->pixmap, dest->pixmap, dest->gc,
            src_x, src_y, area_w, area_h, dest_x, dest_y);

          XFlush(display);
        }
    }
  else rb_raise(rb_eArgError, "Unexpected value-types");

  return Qnil;
} /* }}} */
Exemplo n.º 6
0
static VALUE zipruby_archive_update(int argc, VALUE *argv, VALUE self) {
  struct zipruby_archive *p_archive, *p_srcarchive;
  VALUE srcarchive, flags;
  int i, num_files, i_flags = 0;

  rb_scan_args(argc, argv, "11", &srcarchive, &flags);

  if (!rb_obj_is_instance_of(srcarchive, Archive)) {
    rb_raise(rb_eTypeError, "wrong argument type %s (expected Zip::Archive)", rb_class2name(CLASS_OF(srcarchive)));
  }

  if (!NIL_P(flags)) {
    i_flags = NUM2INT(flags);
  }

  Data_Get_Struct(self, struct zipruby_archive, p_archive);
  Check_Archive(p_archive);
  Data_Get_Struct(srcarchive, struct zipruby_archive, p_srcarchive);
  Check_Archive(p_srcarchive);

  num_files = zip_get_num_files(p_srcarchive->archive);

  for (i = 0; i < num_files; i++) {
    struct zip_source *zsource;
    struct zip_file *fzip;
    struct zip_stat sb;
    char *buf;
    const char *name;
    int index, error;

    zip_stat_init(&sb);

    if (zip_stat_index(p_srcarchive->archive, i, 0, &sb)) {
      zip_unchange_all(p_archive->archive);
      zip_unchange_archive(p_archive->archive);
      rb_raise(Error, "Update archive failed: %s", zip_strerror(p_srcarchive->archive));
    }

    if ((buf = malloc(sb.size)) == NULL) {
      zip_unchange_all(p_archive->archive);
      zip_unchange_archive(p_archive->archive);
      rb_raise(rb_eRuntimeError, "Update archive failed: Cannot allocate memory");
    }

    fzip = zip_fopen_index(p_srcarchive->archive, i, 0);

    if (fzip == NULL) {
      free(buf);
      zip_unchange_all(p_archive->archive);
      zip_unchange_archive(p_archive->archive);
      rb_raise(Error, "Update archive failed: %s", zip_strerror(p_srcarchive->archive));
    }

    if (zip_fread(fzip, buf, sb.size) == -1) {
      free(buf);
      zip_fclose(fzip);
      zip_unchange_all(p_archive->archive);
      zip_unchange_archive(p_archive->archive);
      rb_raise(Error, "Update archive failed: %s", zip_file_strerror(fzip));
    }

    if ((error = zip_fclose(fzip)) != 0) {
      char errstr[ERRSTR_BUFSIZE];
      free(buf);
      zip_unchange_all(p_archive->archive);
      zip_unchange_archive(p_archive->archive);
      zip_error_to_str(errstr, ERRSTR_BUFSIZE, error, errno);
      rb_raise(Error, "Update archive failed: %s", errstr);
    }

    if ((zsource = zip_source_buffer(p_archive->archive, buf, sb.size, 1)) == NULL) {
      free(buf);
      zip_unchange_all(p_archive->archive);
      zip_unchange_archive(p_archive->archive);
      rb_raise(Error, "Update archive failed: %s", zip_strerror(p_archive->archive));
    }

    if ((name = zip_get_name(p_srcarchive->archive, i, 0)) == NULL) {
      zip_source_free(zsource);
      zip_unchange_all(p_archive->archive);
      zip_unchange_archive(p_archive->archive);
      rb_raise(Error, "Update archive failed: %s", zip_strerror(p_srcarchive->archive));
    }

    index = zip_name_locate(p_archive->archive, name, i_flags);

    if (index >= 0) {
      if (zip_replace(p_archive->archive, i, zsource) == -1) {
        zip_source_free(zsource);
        zip_unchange_all(p_archive->archive);
        zip_unchange_archive(p_archive->archive);
        rb_raise(Error, "Update archive failed: %s", zip_strerror(p_archive->archive));
      }
    } else {
      if (zip_add(p_archive->archive, name, zsource) == -1) {
        zip_source_free(zsource);
        zip_unchange_all(p_archive->archive);
        zip_unchange_archive(p_archive->archive);
        rb_raise(Error, "Update archive failed: %s", zip_strerror(p_archive->archive));
      }
    }
  }

  return Qnil;
}
Exemplo n.º 7
0
static VALUE zipruby_archive_s_open_buffer(int argc, VALUE *argv, VALUE self) {
  VALUE buffer, flags, comp_level;
  VALUE archive;
  struct zipruby_archive *p_archive;
  void *data = NULL;
  int len = 0, i_flags = 0;
  int errorp;
  int i_comp_level = Z_BEST_COMPRESSION;
  int buffer_is_temporary = 0;

  rb_scan_args(argc, argv, "03", &buffer, &flags, &comp_level);

  if (FIXNUM_P(buffer) && NIL_P(comp_level)) {
    comp_level = flags;
    flags = buffer;
    buffer = Qnil;
  }

  if (!NIL_P(flags)) {
    i_flags = NUM2INT(flags);
  }

  if (!NIL_P(comp_level)) {
    i_comp_level = NUM2INT(comp_level);

    if (i_comp_level != Z_DEFAULT_COMPRESSION && i_comp_level != Z_NO_COMPRESSION && (i_comp_level < Z_BEST_SPEED || Z_BEST_COMPRESSION < i_comp_level)) {
      rb_raise(rb_eArgError, "Wrong compression level %d", i_comp_level);
    }
  }

  if (i_flags & ZIP_CREATE) {
    if (!NIL_P(buffer)) {
      Check_Type(buffer, T_STRING);
    } else {
      buffer = rb_str_new("", 0);
      buffer_is_temporary = 1;
    }

    i_flags = (i_flags | ZIP_TRUNCATE);
  } else if (TYPE(buffer) == T_STRING) {
    data = RSTRING_PTR(buffer);
    len = RSTRING_LEN(buffer);
  } else if (rb_obj_is_instance_of(buffer, rb_cProc)) {
    data = (void *) buffer;
    len = -1;
  } else {
    rb_raise(rb_eTypeError, "wrong argument type %s (expected String or Proc)", rb_class2name(CLASS_OF(buffer)));
  }

  archive = rb_funcall(Archive, rb_intern("new"), 0);
  Data_Get_Struct(archive, struct zipruby_archive, p_archive);

  if ((p_archive->tmpfilnam = zipruby_tmpnam(data, len)) == NULL) {
    rb_raise(Error, "Open archive failed: Failed to create temporary file");
  }

  if ((p_archive->archive = zip_open(p_archive->tmpfilnam, i_flags, &errorp)) == NULL) {
    char errstr[ERRSTR_BUFSIZE];
    zip_error_to_str(errstr, ERRSTR_BUFSIZE, errorp, errno);
    rb_raise(Error, "Open archive failed: %s", errstr);
  }

  //  p_archive->archive->comp_level = i_comp_level;
  p_archive->path = rb_str_new2(p_archive->tmpfilnam);
  p_archive->flags = i_flags;
  p_archive->buffer = buffer;
  p_archive->sources = rb_ary_new();

  if (rb_block_given_p()) {
    VALUE retval;
    int status;

    retval = rb_protect(rb_yield, archive, &status);
    zipruby_archive_close(archive);

    if (status != 0) {
      rb_jump_tag(status);
    }

    return buffer_is_temporary ? buffer : retval;
  } else {
    return archive;
  }
}
Exemplo n.º 8
0
static VALUE so_instance_of(VALUE self, VALUE obj, VALUE klass) {
  return rb_obj_is_instance_of(obj, klass);
}
Exemplo n.º 9
0
/* subColorPixel {{{ */
unsigned long
subColorPixel(VALUE red,
  VALUE green,
  VALUE blue,
  XColor *xcolor)
{
  XColor xcol = { 0 };

  /* Check object type */
  switch(rb_type(red))
    {
      case T_FIXNUM:
      case T_BIGNUM:
        if(NIL_P(green) && NIL_P(blue))
          {
            xcol.pixel = NUM2LONG(red);

            ColorPixelToRGB(&xcol);
          }
        else
          {
            xcol.red   = NUM2INT(red);
            xcol.green = NUM2INT(green);
            xcol.blue  = NUM2INT(blue);

            ColorRGBToPixel(&xcol);
          }
        break;
      case T_STRING:
        xcol.pixel = subSharedParseColor(display, RSTRING_PTR(red));

        ColorPixelToRGB(&xcol);
        break;
      case T_ARRAY:
        if(3 == FIX2INT(rb_funcall(red, rb_intern("size"), 0, NULL)))
          {
            xcol.red   = NUM2INT(rb_ary_entry(red, 0));
            xcol.green = NUM2INT(rb_ary_entry(red, 1));
            xcol.blue  = NUM2INT(rb_ary_entry(red, 2));

            ColorRGBToPixel(&xcol);
          }
        break;
      case T_HASH:
          {
            xcol.red   = NUM2INT(rb_hash_lookup(red, CHAR2SYM("red")));
            xcol.green = NUM2INT(rb_hash_lookup(red, CHAR2SYM("green")));
            xcol.blue  = NUM2INT(rb_hash_lookup(red, CHAR2SYM("blue")));

            ColorRGBToPixel(&xcol);
          }
        break;
      case T_OBJECT:
          {
            VALUE klass = rb_const_get(mod, rb_intern("Color"));

            /* Check object instance */
            if(rb_obj_is_instance_of(red, klass))
              {
                xcol.red   = NUM2INT(rb_iv_get(red, "@red"));
                xcol.green = NUM2INT(rb_iv_get(red, "@green"));
                xcol.blue  = NUM2INT(rb_iv_get(red, "@blue"));
                xcol.pixel = NUM2LONG(rb_iv_get(red, "@pixel"));
              }
          }
        break;
      default:
        rb_raise(rb_eArgError, "Unexpected value-type `%s'",
          rb_obj_classname(red));
    }

  if(xcolor)
    {
      xcolor->red   = xcol.red;
      xcolor->green = xcol.green;
      xcolor->blue  = xcol.blue;
      xcolor->pixel = xcol.pixel;
    }


  return xcol.pixel;
} /* }}} */
Exemplo n.º 10
0
/* 1 - success, 0 - error */
static int set_oci_number_from_num(OCINumber *result, VALUE num, int force, OCIError *errhp)
{
    signed long sl;

    if (!RTEST(rb_obj_is_kind_of(num, rb_cNumeric)))
        rb_raise(rb_eTypeError, "expect Numeric but %s", rb_class2name(CLASS_OF(num)));
    if (rb_respond_to(num, id_finite_p) && !RTEST(rb_funcall(num, id_finite_p, 0))) {
        rb_raise(rb_eTypeError, "cannot accept number which isn't finite.");
    }
    switch (rb_type(num)) {
    case T_FIXNUM:
        /* set from long. */
        sl = NUM2LONG(num);
        chkerr(OCINumberFromInt(errhp, &sl, sizeof(sl), OCI_NUMBER_SIGNED, result));
        return 1;
    case T_FLOAT:
        /* set from double. */
        oci8_dbl_to_onum(result, NUM2DBL(num), errhp);
        return 1;
    case T_BIGNUM:
        /* change via string. */
        num = rb_big2str(num, 10);
        set_oci_number_from_str(result, num, Qnil, Qnil, errhp);
        return 1;
    }
    if (RTEST(rb_obj_is_instance_of(num, cOCINumber))) {
        /* OCI::Number */
        chkerr(OCINumberAssign(errhp, DATA_PTR(num), result));
        return 1;
    }
    if (rb_respond_to(num, id_split)) {
        /* BigDecimal */
        VALUE split = rb_funcall(num, id_split, 0);

        if (TYPE(split) == T_ARRAY && RARRAY_LEN(split) == 4) {
            /*
             * sign, significant_digits, base, exponent = num.split
             * onum = sign * "0.#{significant_digits}".to_f * (base ** exponent)
             */
            VALUE *ary = RARRAY_PTR(split);
            int sign;
            OCINumber digits;
            int exponent;
            int digits_len;
            OCINumber work;

            /* check sign */
            if (TYPE(ary[0]) != T_FIXNUM) {
                goto is_not_big_decimal;
            }
            sign = FIX2INT(ary[0]);
            /* check digits */
            StringValue(ary[1]);
            digits_len = RSTRING_LEN(ary[1]);
            set_oci_number_from_str(&digits, ary[1], Qnil, Qnil, errhp);
            /* check base */
            if (TYPE(ary[2]) != T_FIXNUM || FIX2LONG(ary[2]) != 10) {
                goto is_not_big_decimal;
            }
            /* check exponent */
            if (TYPE(ary[3]) != T_FIXNUM) {
                goto is_not_big_decimal;
            }
            exponent = FIX2INT(ary[3]);

            chkerr(OCINumberShift(errhp, &digits, exponent - digits_len, &work));
            if (sign >= 0) {
                chkerr(OCINumberAssign(errhp, &work, result));
            } else {
                chkerr(OCINumberNeg(errhp, &work, result));
            }
            return 1;
        }
    }
is_not_big_decimal:
    if (rb_respond_to(num, id_numerator) && rb_respond_to(num, id_denominator)) {
        /* Rational */
        OCINumber numerator;
        OCINumber denominator;

        if (set_oci_number_from_num(&numerator, rb_funcall(num, id_numerator, 0), 0, errhp) &&
            set_oci_number_from_num(&denominator, rb_funcall(num, id_denominator, 0), 0, errhp)) {
            chkerr(OCINumberDiv(errhp, &numerator, &denominator, result));
            return 1;
        }
    }
    if (force) {
        /* change via string as a last resort. */
        /* TODO: if error, raise TypeError instead of OCI::Error */
        set_oci_number_from_str(result, num, Qnil, Qnil, errhp);
        return 1;
    }
    return 0;
}
Exemplo n.º 11
0
void check_type_cowboy_data(VALUE in){
  if (rb_obj_is_instance_of(in, cCowboyData) != Qtrue)
    rb_raise(rb_eTypeError, "Expected a CowboyData instance");
}
Exemplo n.º 12
0
static VALUE ov_http_client_submit(VALUE self, VALUE request) {
    CURL* handle;
    VALUE response;
    VALUE transfer;
    ov_http_client_object* ptr;
    ov_http_request_object* request_ptr;
    ov_http_transfer_object* transfer_ptr;
    struct curl_slist* headers;

    /* Get the pointer to the native object and check that it isn't closed: */
    ov_http_client_ptr(self, ptr);
    ov_http_client_check_closed(ptr);

    /* Check the type of request and get the pointer to the native object: */
    if (NIL_P(request)) {
        rb_raise(ov_error_class, "The 'request' parameter can't be nil");
    }
    if (!rb_obj_is_instance_of(request, ov_http_request_class)) {
        rb_raise(ov_error_class, "The 'request' parameter isn't an instance of class 'HttpRequest'");
    }
    ov_http_request_ptr(request, request_ptr);

    /* Create the libcurl easy handle: */
    handle = curl_easy_init();
    if (ptr->handle == NULL) {
        rb_raise(ov_error_class, "Can't create libcurl object");
    }

    /* The headers used by the libcurl easy handle can't be released till the handle is released itself, so we need
       to initialize here, and add it to the context so that we can release it later: */
    headers = NULL;

    /* Configure the libcurl easy handle with the data from the client and from the request: */
    ov_http_client_prepare_handle(ptr, request_ptr, &headers, handle);

    /* Allocate a ne empty response: */
    response = rb_class_new_instance(0, NULL, ov_http_response_class);

    /* Allocate a new empty transfer: */
    transfer = rb_class_new_instance(0, NULL, ov_http_transfer_class);
    ov_http_transfer_ptr(transfer, transfer_ptr);
    transfer_ptr->client = self;
    transfer_ptr->request = request;
    transfer_ptr->response = response;
    transfer_ptr->headers = headers;
    transfer_ptr->cancel = false;
    if (NIL_P(request_ptr->body)) {
        transfer_ptr->in = rb_class_new_instance(0, NULL, STRING_IO_CLASS);
    }
    else {
        transfer_ptr->in = rb_class_new_instance(1, &request_ptr->body, STRING_IO_CLASS);
    }
    transfer_ptr->out = rb_class_new_instance(0, NULL, STRING_IO_CLASS);

    /* Put the request and the transfer in the hash of pending transfers: */
    rb_hash_aset(ptr->pending, request, transfer);

    /* Set the transfer as the data for all the callbacks, so we can access it from any place where it is needed: */
    curl_easy_setopt(handle, CURLOPT_PRIVATE, transfer);
    curl_easy_setopt(handle, CURLOPT_READDATA, transfer);
    curl_easy_setopt(handle, CURLOPT_WRITEDATA, transfer);
    curl_easy_setopt(handle, CURLOPT_HEADERDATA, transfer);
    curl_easy_setopt(handle, CURLOPT_DEBUGDATA, transfer);

    /* Add the easy handle to the multi handle: */
    curl_multi_add_handle(ptr->handle, handle);

    return Qnil;
}
Exemplo n.º 13
0
static VALUE generate_privkey(int argc, VALUE* argv, VALUE self) {
  VALUE accountname, protocol, file;
  OtrlUserState us;
  void *newkey;
  void *args_cancel[2];
  int fd;
  FILE* fp;
  gcry_error_t err;

  if (argc < 2 || argc > 3) {
    rb_raise(rb_eArgError, "wrong number of arguments");
  }

  accountname = argv[0];
  protocol = argv[1];

  Check_Type(accountname, T_STRING);
  Check_Type(protocol, T_STRING);

  if (argc == 3) {
    file = argv[2];
  } else {
    file = rb_funcall(self, rb_intern("keyfile"), 0);
  }

  if (!rb_obj_is_instance_of(file, rb_cFile) && !rb_obj_is_instance_of(file, rb_cString)) {
    rb_raise(rb_eTypeError, "file argument must be File or String");
  }

  Data_Get_Struct(self, struct s_OtrlUserState, us);

  err = otrl_privkey_generate_start(us,
    RSTRING_PTR(accountname),
    RSTRING_PTR(protocol),
    &newkey);
  if (err) {
    RAISE_GPG_ERROR(err);
  }

  args_cancel[0] = us;
  args_cancel[1] = newkey;

  err = rb_thread_call_without_gvl(generate_privkey_background,
    newkey,
    generate_privkey_cancel,
    args_cancel);
  if (err) {
    RAISE_GPG_ERROR(err);
  }

  if (rb_obj_is_instance_of(file, rb_cFile)) {
    fd = FIX2INT(rb_funcall(file, rb_intern("fileno"), 0));
    fp = fdopen(dup(fd), "wb+");
    err = otrl_privkey_generate_finish_FILEp(us,
      newkey,
      fp);
    fclose(fp);
  } else if (rb_obj_is_instance_of(file, rb_cString)) {
    err = otrl_privkey_generate_finish(us,
      newkey,
      RSTRING_PTR(file));
  }
  if (err) {
    RAISE_GPG_ERROR(err);
  }

  return Qnil;
}
Exemplo n.º 14
0
int main( int argc, char** argv ) 
{
  int state  = 0;
  int rc     = 0;
  int opt_mv = 0;

  crate_app ca;

  /** startup items from ruby's original main.c */
#ifdef _WIN32
  NtInitialize(&argc, &argv);
#endif
#if defined(__MACOS__) && defined(__MWERKS__)
  argc = ccommand(&argv);
#endif

  /* setup ruby */
  ruby_init();
  ruby_script( argv[0] );
  ruby_init_loadpath();

  /* strip out the crate specific arguments from argv using --crate- */
  opt_mv = crate_init_from_options( &ca, argc, argv );
  argc -= opt_mv;
  argv += opt_mv;
 
  /* printf("crate file  : %s\n", ca.file_name);   */
  /* printf("crate class : %s\n", ca.class_name);  */
  /* printf("crate method: %s\n", ca.method_name); */

  /* make ARGV available */
  ruby_set_argv( argc, argv );

  /* initialize all extensions */
  Init_ext();

  /* load up the amalgalite libs */
  am_bootstrap_lift( cARB, Qnil );
 
  /* remove the current LOAD_PATH */
  rb_ary_clear( rb_gv_get( "$LOAD_PATH" ) );

  /* invoke the class / method passing in ARGV and ENV */
  rb_protect( crate_wrap_app, (VALUE)&ca, &state );

  /* check the results */
  if ( state ) {

    /* exception was raised, check the $! var */
    VALUE lasterr  = rb_gv_get("$!");
   
    /* system exit was called so just propogate that up to our exit */
    if ( rb_obj_is_instance_of( lasterr, rb_eSystemExit ) ) {

      rc = NUM2INT( rb_attr_get( lasterr, rb_intern("status") ) );
      /*printf(" Caught SystemExit -> $? will be %d\n", rc ); */

    } else {

      /* some other exception was raised so dump that out */
      VALUE klass     = rb_class_path( CLASS_OF( lasterr ) );
      VALUE message   = rb_obj_as_string( lasterr );
      VALUE backtrace = rb_funcall( lasterr, rb_intern("backtrace"), 0 );

      fprintf( stderr, "%s: %s\n", RSTRING( klass )->ptr, RSTRING( message )->ptr );
      rb_iterate( rb_each, backtrace, dump_backtrace, Qnil );

      rc = state;
    }
  } 

  free( ca.file_name );
  free( ca.class_name );
  free( ca.method_name );

  /* shut down ruby */
  ruby_finalize();

  /* exit the program */
  exit( rc );
}