VALUE do_postgres_cConnection_quote_byte_array(VALUE self, VALUE string) { PGconn *db = DATA_PTR(rb_iv_get(self, "@connection")); const unsigned char *source = (unsigned char *)rb_str_ptr_readonly(string); size_t source_len = rb_str_len(string); unsigned char *escaped; unsigned char *escaped_quotes; size_t quoted_length = 0; VALUE result; // Allocate space for the escaped version of 'string' // http://www.postgresql.org/docs/8.3/static/libpq-exec.html#LIBPQ-EXEC-ESCAPE-STRING escaped = PQescapeByteaConn(db, source, source_len, "ed_length); if(!escaped) { rb_memerror(); } if (!(escaped_quotes = calloc(quoted_length + 1, sizeof(unsigned char)))) { rb_memerror(); } memcpy(escaped_quotes + 1, escaped, quoted_length * sizeof(unsigned char)); // Wrap the escaped string in single-quotes, this is DO's convention (replace trailing \0) escaped_quotes[0] = escaped_quotes[quoted_length] = '\''; result = rb_str_new((const char *)escaped_quotes, quoted_length + 1); PQfreemem(escaped); free(escaped_quotes); return result; }
VALUE rb_readlink(VALUE path) { ssize_t len; WCHAR *wpath, wbuf[MAX_PATH]; rb_encoding *enc; UINT cp, path_cp; FilePathValue(path); enc = rb_enc_get(path); cp = path_cp = code_page(enc); if (cp == INVALID_CODE_PAGE) { path = fix_string_encoding(path, enc); cp = CP_UTF8; } wpath = mbstr_to_wstr(cp, RSTRING_PTR(path), RSTRING_LEN(path)+rb_enc_mbminlen(enc), NULL); if (!wpath) rb_memerror(); len = rb_w32_wreadlink(wpath, wbuf, numberof(wbuf)); free(wpath); if (len < 0) rb_sys_fail_path(path); enc = rb_filesystem_encoding(); cp = path_cp = code_page(enc); if (cp == INVALID_CODE_PAGE) cp = CP_UTF8; return append_wstr(rb_enc_str_new(0, 0, enc), wbuf, len, cp, path_cp, enc); }
void ruby_libvirt_raise_error_if(const int condition, VALUE error, const char *method, virConnectPtr conn) { VALUE ruby_errinfo; virErrorPtr err; char *msg; int rc; struct rb_exc_new2_arg arg; int exception = 0; if (!condition) { return; } if (conn == NULL) { err = virGetLastError(); } else { err = virConnGetLastError(conn); } if (err != NULL && err->message != NULL) { rc = asprintf(&msg, "Call to %s failed: %s", method, err->message); } else { rc = asprintf(&msg, "Call to %s failed", method); } if (rc < 0) { /* there's not a whole lot we can do here; try to raise an * out-of-memory message */ rb_memerror(); } arg.error = error; arg.msg = msg; ruby_errinfo = rb_protect(ruby_libvirt_exc_new2_wrap, (VALUE)&arg, &exception); free(msg); if (exception) { rb_jump_tag(exception); } rb_iv_set(ruby_errinfo, "@libvirt_function_name", rb_str_new2(method)); if (err != NULL) { rb_iv_set(ruby_errinfo, "@libvirt_code", INT2NUM(err->code)); rb_iv_set(ruby_errinfo, "@libvirt_component", INT2NUM(err->domain)); rb_iv_set(ruby_errinfo, "@libvirt_level", INT2NUM(err->level)); if (err->message != NULL) { rb_iv_set(ruby_errinfo, "@libvirt_message", rb_str_new2(err->message)); } } rb_exc_raise(ruby_errinfo); };
static VALUE spgd_compute_name(VALUE self, VALUE split_rule, VALUE values) { VALUE res = 0; int encoding = -1; char *result = (char*) xmalloc(INITIAL_CAPA); int pos = 0, capa = INITIAL_CAPA; long i, rule_len = RARRAY_LEN(split_rule); if (!result) { rb_memerror(); } for (i = 0; i < rule_len; i++) { VALUE rule = rb_ary_entry(split_rule, i); if (rb_class_of(rule) == rb_cArray) { long fieldnum = NUM2LONG(rb_ary_entry(rule, 0)); VALUE actions = rb_ary_entry(rule, 1); rule = rb_ary_entry(values, fieldnum); encoding = ENCODING_GET(rule); if (RTEST(actions) && RARRAY_LEN(actions)) { rule = apply_actions(rule, actions); } } if (rb_class_of(rule) == rb_cString) { long size = RSTRING_LEN(rule); if (capa < pos + size + 1) { char *tmp; capa = pos + size + 1; if (i + 1 != rule_len) capa = (capa * 3) >> 1; tmp = (char*) xrealloc(result, capa); if (!tmp) { xfree(result); rb_memerror(); } result = tmp; } if (encoding == -1) encoding = ENCODING_GET(rule); strncpy(result + pos, RSTRING_PTR(rule), size + 1); pos += size; }
void * ruby_xrealloc(void *ptr, size_t size) { void *mem; if (size < 0) { rb_raise(rb_eArgError, "negative re-allocation size"); } if (ptr == NULL) { return ruby_xmalloc(size); } if (size == 0) { size = 1; } #if MAC_OS_X_VERSION_MAX_ALLOWED < 1060 { size_t old_size = malloc_size(ptr); if (old_size >= size) { return ptr; } mem = ruby_xmalloc(size); if (mem == NULL) { rb_memerror(); } auto_zone_write_barrier_memmove(__auto_zone, mem, ptr, old_size); xfree(ptr); } #else mem = malloc_zone_realloc(__auto_zone, ptr, size); if (mem == NULL) { rb_memerror(); } #endif return mem; }
static inline void * ruby_xmalloc_memory(size_t size, int type) { assert(size > 0); if (__auto_zone == NULL) { rb_objc_no_gc_error(); } void *mem = auto_zone_allocate_object(__auto_zone, size, type, 0, 0); if (mem == NULL) { rb_memerror(); } return mem; }
void * ruby_xrealloc(void *ptr, size_t size) { if (ptr == NULL) { return ruby_xmalloc(size); } if (size == 0) { size = 1; } void *mem = malloc_zone_realloc(__auto_zone, ptr, size); if (mem == NULL) { rb_memerror(); } return mem; }
static VALUE rb_czmq_frame_dup(VALUE obj) { VALUE dup; zframe_t *dup_fr = NULL; errno = 0; ZmqGetFrame(obj); dup_fr = zframe_dup(frame); if (dup_fr == NULL) { ZmqAssertSysError(); rb_memerror(); } ZmqRegisterFrame(dup_fr); dup = Data_Wrap_Struct(rb_cZmqFrame, 0, rb_czmq_free_frame_gc, dup_fr); rb_obj_call_init(dup, 0, NULL); return dup; }
VALUE do_sqlite3_cConnection_quote_string(VALUE self, VALUE string) { const char *source = rb_str_ptr_readonly(string); // Wrap the escaped string in single-quotes, this is DO's convention char *escaped_with_quotes = sqlite3_mprintf("%Q", source); if(!escaped_with_quotes) { rb_memerror(); } VALUE result = rb_str_new2(escaped_with_quotes); #ifdef HAVE_RUBY_ENCODING_H rb_enc_associate_index(result, FIX2INT(rb_iv_get(self, "@encoding_id"))); #endif sqlite3_free(escaped_with_quotes); return result; }
static VALUE rb_czmq_frame_s_new(int argc, VALUE *argv, VALUE frame) { VALUE data; errno = 0; zframe_t *fr; rb_scan_args(argc, argv, "01", &data); if (NIL_P(data)) { fr = zframe_new(NULL, 0); } else { Check_Type(data, T_STRING); fr = zframe_new(RSTRING_PTR(data), (size_t)RSTRING_LEN(data)); } if (fr == NULL) { ZmqAssertSysError(); rb_memerror(); } ZmqRegisterFrame(fr); frame = Data_Wrap_Struct(rb_cZmqFrame, 0, rb_czmq_free_frame_gc, fr); rb_obj_call_init(frame, 0, NULL); return frame; }
VALUE do_postgres_cConnection_quote_string(VALUE self, VALUE string) { PGconn *db = DATA_PTR(rb_iv_get(self, "@connection")); const char *source = rb_str_ptr_readonly(string); int error = 0; long source_len = rb_str_len(string); long buffer_len = source_len * 2 + 3; // Overflow check if(buffer_len <= source_len) { rb_raise(rb_eArgError, "Input string is too large to be safely quoted"); } char *escaped; // Allocate space for the escaped version of 'string' // http://www.postgresql.org/docs/8.3/static/libpq-exec.html#LIBPQ-EXEC-ESCAPE-STRING if (!(escaped = calloc(buffer_len, sizeof(char)))) { rb_memerror(); } long quoted_length; VALUE result; // Escape 'source' using the current charset in use on the conection 'db' quoted_length = PQescapeStringConn(db, escaped + 1, source, source_len, &error); if(error) { rb_raise(eDO_DataError, "%s", PQerrorMessage(db)); } // Wrap the escaped string in single-quotes, this is DO's convention escaped[0] = escaped[quoted_length + 1] = '\''; result = DATA_OBJECTS_STR_NEW(escaped, quoted_length + 2, FIX2INT(rb_iv_get(self, "@encoding_id")), NULL); free(escaped); return result; }
VALUE do_mysql_cConnection_quote_string(VALUE self, VALUE string) { MYSQL *db = DATA_PTR(rb_iv_get(self, "@connection")); const char *source = rb_str_ptr_readonly(string); long source_len = rb_str_len(string); long buffer_len = source_len * 2 + 3; // Overflow check if(buffer_len <= source_len) { rb_raise(rb_eArgError, "Input string is too large to be safely quoted"); } // Allocate space for the escaped version of 'string'. Use + 3 allocate space for null term. // and the leading and trailing single-quotes. // Thanks to http://www.browardphp.com/mysql_manual_en/manual_MySQL_APIs.html#mysql_real_escape_string char *escaped = calloc(buffer_len, sizeof(char)); if (!escaped) { rb_memerror(); } unsigned long quoted_length; VALUE result; // Escape 'source' using the current encoding in use on the conection 'db' quoted_length = mysql_real_escape_string(db, escaped + 1, source, source_len); // Wrap the escaped string in single-quotes, this is DO's convention escaped[0] = escaped[quoted_length + 1] = '\''; // We don't want to use the internal encoding, because this needs // to go into the database in the connection encoding result = DATA_OBJECTS_STR_NEW(escaped, quoted_length + 2, FIX2INT(rb_iv_get(self, "@encoding_id")), NULL); free(escaped); return result; }
void * ruby_xrealloc(void *ptr, size_t size) { if ((ssize_t)size < 0) { negative_size_allocation_error("negative re-allocation size"); } if (ptr == NULL) { return ruby_xmalloc(size); } if (size == 0) { size = 1; } if (stress_gc && !dont_gc) { garbage_collect(); } void *mem = malloc_zone_realloc(__auto_zone, ptr, size); if (mem == NULL) { rb_memerror(); } return mem; }
void * ruby_xmalloc(size_t size) { void *mem; if (size < 0) { rb_raise(rb_eNoMemError, "negative allocation size (or too big)"); } if (size == 0) { size = 1; } if (__auto_zone == NULL) { rb_objc_no_gc_error(); } mem = auto_zone_allocate_object(__auto_zone, size, AUTO_MEMORY_SCANNED, 0, 0); if (mem == NULL) { rb_memerror(); } return mem; }
static inline void * ruby_xmalloc_memory(size_t size, int type) { if ((ssize_t)size < 0) { negative_size_allocation_error("negative allocation size (or too big)"); } if (size == 0) { size = 1; } if (__auto_zone == NULL) { rb_objc_no_gc_error(); } if (stress_gc && !dont_gc) { garbage_collect(); } void *mem = auto_zone_allocate_object(__auto_zone, size, type, 0, 0); if (mem == NULL) { rb_memerror(); } return mem; }
void do_postgres_full_connect(VALUE self, PGconn *db) { VALUE r_host; char *host = NULL; if ((r_host = rb_iv_get(self, "@host")) != Qnil) { host = StringValuePtr(r_host); } VALUE r_user; char *user = NULL; if ((r_user = rb_iv_get(self, "@user")) != Qnil) { user = StringValuePtr(r_user); } VALUE r_password; char *password = NULL; if ((r_password = rb_iv_get(self, "@password")) != Qnil) { password = StringValuePtr(r_password); } VALUE r_port; const char *port = "5432"; if ((r_port = rb_iv_get(self, "@port")) != Qnil) { port = StringValuePtr(r_port); } VALUE r_path; char *path = NULL; char *database = NULL; if ((r_path = rb_iv_get(self, "@path")) != Qnil) { path = StringValuePtr(r_path); database = strtok(path, "/"); } if (!database || !*database) { database = NULL; } VALUE r_query = rb_iv_get(self, "@query"); const char *search_path = data_objects_get_uri_option(r_query, "search_path"); db = PQsetdbLogin( host, port, NULL, NULL, database, user, password ); if (PQstatus(db) == CONNECTION_BAD) { rb_raise(eDO_ConnectionError, "%s", PQerrorMessage(db)); } PGresult *result; if (search_path) { char *search_path_query; if (!(search_path_query = calloc(256, sizeof(char)))) { rb_memerror(); } snprintf(search_path_query, 256, "set search_path to %s;", search_path); r_query = rb_str_new2(search_path_query); result = do_postgres_cCommand_execute(Qnil, self, db, r_query); if (PQresultStatus(result) != PGRES_COMMAND_OK) { free(search_path_query); do_postgres_raise_error(self, result, r_query); } free(search_path_query); } const char *backslash_off = "SET backslash_quote = off"; const char *standard_strings_on = "SET standard_conforming_strings = on"; const char *warning_messages = "SET client_min_messages = warning"; const char *date_format = "SET datestyle = ISO"; VALUE r_options; r_options = rb_str_new2(backslash_off); result = do_postgres_cCommand_execute(Qnil, self, db, r_options); if (PQresultStatus(result) != PGRES_COMMAND_OK) { rb_warn("%s", PQresultErrorMessage(result)); } r_options = rb_str_new2(standard_strings_on); result = do_postgres_cCommand_execute(Qnil, self, db, r_options); if (PQresultStatus(result) != PGRES_COMMAND_OK) { rb_warn("%s", PQresultErrorMessage(result)); } r_options = rb_str_new2(warning_messages); result = do_postgres_cCommand_execute(Qnil, self, db, r_options); if (PQresultStatus(result) != PGRES_COMMAND_OK) { rb_warn("%s", PQresultErrorMessage(result)); } r_options = rb_str_new2(date_format); result = do_postgres_cCommand_execute(Qnil, self, db, r_options); if (PQresultStatus(result) != PGRES_COMMAND_OK) { rb_warn("%s", PQresultErrorMessage(result)); } VALUE encoding = rb_iv_get(self, "@encoding"); #ifdef HAVE_PQSETCLIENTENCODING VALUE pg_encoding = rb_hash_aref(data_objects_const_get(mDO_PostgresEncoding, "MAP"), encoding); if (pg_encoding != Qnil) { if (PQsetClientEncoding(db, rb_str_ptr_readonly(pg_encoding))) { rb_raise(eDO_ConnectionError, "Couldn't set encoding: %s", rb_str_ptr_readonly(encoding)); } else { #ifdef HAVE_RUBY_ENCODING_H rb_iv_set(self, "@encoding_id", INT2FIX(rb_enc_find_index(rb_str_ptr_readonly(encoding)))); #endif rb_iv_set(self, "@pg_encoding", pg_encoding); } } else { rb_warn("Encoding %s is not a known Ruby encoding for PostgreSQL\n", rb_str_ptr_readonly(encoding)); rb_iv_set(self, "@encoding", rb_str_new2("UTF-8")); #ifdef HAVE_RUBY_ENCODING_H rb_iv_set(self, "@encoding_id", INT2FIX(rb_enc_find_index("UTF-8"))); #endif rb_iv_set(self, "@pg_encoding", rb_str_new2("UTF8")); } #endif rb_iv_set(self, "@connection", Data_Wrap_Struct(rb_cObject, 0, 0, db)); }