/* * Make a Ruby array out of the encoded values from the specified * column in the given result. */ static VALUE make_column_result_array( VALUE self, int col ) { PGresult *result = pgresult_get( self ); int rows = PQntuples( result ); int i; VALUE val = Qnil; VALUE results = rb_ary_new2( rows ); if ( col >= PQnfields(result) ) rb_raise( rb_eIndexError, "no column %d in result", col ); for ( i=0; i < rows; i++ ) { val = rb_tainted_str_new( PQgetvalue(result, i, col), PQgetlength(result, i, col) ); #ifdef M17N_SUPPORTED /* associate client encoding for text format only */ if ( 0 == PQfformat(result, col) ) { ASSOCIATE_INDEX( val, self ); } else { rb_enc_associate( val, rb_ascii8bit_encoding() ); } #endif rb_ary_store( results, i, val ); } return results; }
/* * Convert a Verse host id to a String with ASCII8BIT encoding. */ inline VALUE rbverse_host_id2str( const uint8 *hostid ) { VALUE idstring = rb_str_new( (const char *)hostid, V_HOST_ID_SIZE ); rb_enc_associate( idstring, rb_ascii8bit_encoding() ); return idstring; }
/* * call-seq: * res.getvalue( tup_num, field_num ) * * Returns the value in tuple number _tup_num_, field _field_num_, * or +nil+ if the field is +NULL+. */ static VALUE pgresult_getvalue(VALUE self, VALUE tup_num, VALUE field_num) { VALUE val; PGresult *result; int i = NUM2INT(tup_num); int j = NUM2INT(field_num); result = pgresult_get(self); if(i < 0 || i >= PQntuples(result)) { rb_raise(rb_eArgError,"invalid tuple number %d", i); } if(j < 0 || j >= PQnfields(result)) { rb_raise(rb_eArgError,"invalid field number %d", j); } if(PQgetisnull(result, i, j)) return Qnil; val = rb_tainted_str_new(PQgetvalue(result, i, j), PQgetlength(result, i, j)); #ifdef M17N_SUPPORTED /* associate client encoding for text format only */ if ( 0 == PQfformat(result, j) ) { ASSOCIATE_INDEX( val, self ); } else { rb_enc_associate( val, rb_ascii8bit_encoding() ); } #endif return val; }
/* Return code page number of the encoding. Cache code page into a hash for performance since finding the code page in Encoding#names is slow. */ static UINT code_page(rb_encoding *enc) { VALUE code_page_value, name_key; VALUE encoding, names_ary = Qundef, name; char *enc_name; struct RString fake_str; ID names; long i; if (!enc) return system_code_page(); enc_name = (char *)rb_enc_name(enc); fake_str.basic.flags = T_STRING|RSTRING_NOEMBED; fake_str.basic.klass = rb_cString; fake_str.as.heap.len = strlen(enc_name); fake_str.as.heap.ptr = enc_name; fake_str.as.heap.aux.capa = fake_str.as.heap.len; name_key = (VALUE)&fake_str; ENCODING_CODERANGE_SET(name_key, rb_usascii_encindex(), ENC_CODERANGE_7BIT); code_page_value = rb_hash_lookup(rb_code_page, name_key); if (code_page_value != Qnil) return (UINT)FIX2INT(code_page_value); name_key = rb_usascii_str_new2(enc_name); encoding = rb_enc_from_encoding(enc); if (!NIL_P(encoding)) { CONST_ID(names, "names"); names_ary = rb_funcall(encoding, names, 0); } /* map US-ASCII and ASCII-8bit as code page 1252 (us-ascii) */ if (enc == rb_usascii_encoding() || enc == rb_ascii8bit_encoding()) { UINT code_page = 1252; rb_hash_aset(rb_code_page, name_key, INT2FIX(code_page)); return code_page; } if (names_ary != Qundef) { for (i = 0; i < RARRAY_LEN(names_ary); i++) { name = RARRAY_PTR(names_ary)[i]; if (strncmp("CP", RSTRING_PTR(name), 2) == 0) { int code_page = atoi(RSTRING_PTR(name) + 2); if (code_page != 0) { rb_hash_aset(rb_code_page, name_key, INT2FIX(code_page)); return (UINT)code_page; } } } } rb_hash_aset(rb_code_page, name_key, INT2FIX(INVALID_CODE_PAGE)); return INVALID_CODE_PAGE; }
int rb_define_dummy_encoding(const char *name) { int index = rb_enc_replicate(name, rb_ascii8bit_encoding()); rb_encoding *enc = enc_table.list[index].enc; ENC_SET_DUMMY(enc); return index; }
int rb_encdb_dummy(const char *name) { int index = enc_replicate_with_index(name, rb_ascii8bit_encoding(), rb_enc_registered(name)); rb_encoding *enc = enc_table.list[index].enc; ENC_SET_DUMMY(enc); return index; }
/* @return [String] The String representation of the receiver, inheriting any * taint and untrust, encoded as ASCII-8BIT. */ VALUE rb_u_string_b(VALUE self) { const struct rb_u_string *string = RVAL2USTRING(self); VALUE result = rb_str_new(USTRING_STR(string), USTRING_LENGTH(string)); #ifdef HAVE_RUBY_ENCODING_H rb_enc_associate(result, rb_ascii8bit_encoding()); #endif OBJ_INFECT(result, self); return result; }
VALUE wkhtml_toimage_converter_get_output(VALUE self) { wkhtmltoimage_converter* converter; const unsigned char* data_cstr; long length; VALUE data; Data_Get_Struct(self, wkhtmltoimage_converter, converter); length = wkhtmltoimage_get_output(converter, &data_cstr); data = rb_str_new((char*)data_cstr, length); rb_enc_associate(data, rb_ascii8bit_encoding()); return data; }
static void check_utf8_encoding(VALUE str) { static rb_encoding *_cached[3] = {NULL, NULL, NULL}; rb_encoding *enc; if (_cached[0] == NULL) { _cached[0] = rb_utf8_encoding(); _cached[1] = rb_usascii_encoding(); _cached[2] = rb_ascii8bit_encoding(); } enc = rb_enc_get(str); if (enc != _cached[0] && enc != _cached[1] && enc != _cached[2]) { rb_raise(rb_eEncodingCompatibilityError, "Input must be UTF-8 or US-ASCII, %s given", rb_enc_name(enc)); } }
static VALUE file_path_convert(VALUE name) { #ifndef _WIN32 /* non Windows == Unix */ rb_encoding *fname_encoding = rb_enc_from_index(ENCODING_GET(name)); rb_encoding *fs_encoding; if (rb_default_internal_encoding() != NULL && rb_usascii_encoding() != fname_encoding && rb_ascii8bit_encoding() != fname_encoding && (fs_encoding = rb_filesystem_encoding()) != fname_encoding && !rb_enc_str_asciionly_p(name)) { /* Don't call rb_filesystem_encoding() before US-ASCII and ASCII-8BIT */ /* fs_encoding should be ascii compatible */ name = rb_str_conv_enc(name, fname_encoding, fs_encoding); } #endif return name; }
/* * Return the given PostgreSQL encoding name as an rb_encoding. */ rb_encoding * pg_get_pg_encname_as_rb_encoding( const char *pg_encname ) { size_t i; /* Trying looking it up in the conversion table */ for ( i = 0; i < sizeof(pg_enc_pg2ruby_mapping)/sizeof(pg_enc_pg2ruby_mapping[0]); ++i ) { if ( strcmp(pg_encname, pg_enc_pg2ruby_mapping[i][0]) == 0 ) return rb_enc_find( pg_enc_pg2ruby_mapping[i][1] ); } /* JOHAB isn't a builtin encoding, so make up a dummy encoding if it's seen */ if ( strncmp(pg_encname, "JOHAB", 5) == 0 ) return pg_find_or_create_johab(); /* Fallthrough to ASCII-8BIT */ return rb_ascii8bit_encoding(); }
/* * call-seq: * res.values -> Array * * Returns all tuples as an array of arrays. */ static VALUE pgresult_values(VALUE self) { PGresult* result = (PGresult*) pgresult_get(self); int row; int field; int num_rows = PQntuples(result); int num_fields = PQnfields(result); VALUE ary = rb_ary_new2(num_rows); for ( row = 0; row < num_rows; row++ ) { VALUE new_row = rb_ary_new2(num_fields); /* populate the row */ for ( field = 0; field < num_fields; field++ ) { if ( PQgetisnull(result, row, field) ) { rb_ary_store( new_row, field, Qnil ); } else { VALUE val = rb_tainted_str_new( PQgetvalue(result, row, field), PQgetlength(result, row, field) ); #ifdef M17N_SUPPORTED /* associate client encoding for text format only */ if ( 0 == PQfformat(result, field) ) { ASSOCIATE_INDEX( val, self ); } else { rb_enc_associate( val, rb_ascii8bit_encoding() ); } #endif rb_ary_store( new_row, field, val ); } } rb_ary_store( ary, row, new_row ); } return ary; }
static VALUE rb_keychain_item_copy_password(VALUE self){ void *data; SecKeychainItemRef keychainItem=NULL; UInt32 dataLength; Data_Get_Struct(self, struct OpaqueSecKeychainItemRef, keychainItem); VALUE unsaved = rb_ivar_get(self, rb_intern("unsaved_password")); if(!NIL_P(unsaved)){ return unsaved; } else{ OSStatus result = SecKeychainItemCopyAttributesAndData(keychainItem, NULL , NULL, NULL, &dataLength, &data); CheckOSStatusOrRaise(result); VALUE rb_data = rb_enc_str_new(data, dataLength, rb_ascii8bit_encoding()); SecKeychainItemFreeAttributesAndData(NULL,data); return rb_data; } }
static VALUE pgresult_value(VALUE self, PGresult *result, int tuple_num, int field_num) { VALUE val; if ( PQgetisnull(result, tuple_num, field_num) ) { return Qnil; } else { val = rb_tainted_str_new( PQgetvalue(result, tuple_num, field_num ), PQgetlength(result, tuple_num, field_num) ); #ifdef M17N_SUPPORTED /* associate client encoding for text format only */ if ( 0 == PQfformat(result, field_num) ) { ASSOCIATE_INDEX( val, self ); } else { rb_enc_associate( val, rb_ascii8bit_encoding() ); } #endif return val; } }
/* * call-seq: * res[ n ] -> Hash * * Returns tuple _n_ as a hash. */ static VALUE pgresult_aref(VALUE self, VALUE index) { PGresult *result = pgresult_get(self); int tuple_num = NUM2INT(index); int field_num; VALUE fname,val; VALUE tuple; if ( tuple_num < 0 || tuple_num >= PQntuples(result) ) rb_raise( rb_eIndexError, "Index %d is out of range", tuple_num ); tuple = rb_hash_new(); for ( field_num = 0; field_num < PQnfields(result); field_num++ ) { fname = rb_tainted_str_new2( PQfname(result,field_num) ); ASSOCIATE_INDEX(fname, self); if ( PQgetisnull(result, tuple_num, field_num) ) { rb_hash_aset( tuple, fname, Qnil ); } else { val = rb_tainted_str_new( PQgetvalue(result, tuple_num, field_num ), PQgetlength(result, tuple_num, field_num) ); #ifdef M17N_SUPPORTED /* associate client encoding for text format only */ if ( 0 == PQfformat(result, field_num) ) { ASSOCIATE_INDEX( val, self ); } else { rb_enc_associate( val, rb_ascii8bit_encoding() ); } #endif rb_hash_aset( tuple, fname, val ); } } return tuple; }
static VALUE encoding_spec_rb_ascii8bit_encoding(VALUE self) { return rb_str_new2(rb_ascii8bit_encoding()->name); }
static void cf_hash_to_rb_hash(const void *raw_key, const void * raw_value, void *ctx){ CFTypeRef value = (CFTypeRef)raw_value; CFStringRef key = (CFStringRef)raw_key; VALUE rubyValue = Qnil; VALUE hash = (VALUE)ctx; if(CFStringGetTypeID() == CFGetTypeID(value)){ rubyValue = cfstring_to_rb_string((CFStringRef)value); } else if(CFDataGetTypeID() == CFGetTypeID(value)){ CFDataRef data = (CFDataRef)value; rubyValue = rb_enc_str_new((const char*)CFDataGetBytePtr(data),CFDataGetLength(data), rb_ascii8bit_encoding()); } else if(CFBooleanGetTypeID() == CFGetTypeID(value)){ Boolean booleanValue = CFBooleanGetValue(value); rubyValue = booleanValue ? Qtrue : Qfalse; } else if(CFNumberGetTypeID() == CFGetTypeID(value)){ if(CFNumberIsFloatType(value)) { double doubleValue; CFNumberGetValue(value, kCFNumberDoubleType, &doubleValue); rubyValue = rb_float_new(doubleValue); }else{ long long longValue; CFNumberGetValue(value, kCFNumberLongLongType, &longValue); rubyValue = LL2NUM(longValue); } } else if (CFDateGetTypeID() == CFGetTypeID(value)){ CFDateRef date = (CFDateRef) value; CFAbsoluteTime abs_time = CFDateGetAbsoluteTime(date); double secondsSinceUnixEpoch = abs_time + kCFAbsoluteTimeIntervalSince1970; time_t seconds = (time_t)secondsSinceUnixEpoch; long usec = (secondsSinceUnixEpoch - seconds) * 1000000; rubyValue = rb_time_new((time_t)secondsSinceUnixEpoch, usec); } if(!NIL_P(rubyValue)){ rb_hash_aset(hash, cfstring_to_rb_string(key), rubyValue); } }
/** * Convert a ruby string into a utf-8 compatible binary string. * * @example Convert the string to utf-8 binary. * rb_bson_to_utf8_binary("test"); * * @param [ String ] string The ruby string. * * @return [ String ] The encoded string. * * @since 2.0.0 */ static VALUE rb_bson_to_utf8_binary(VALUE string) { VALUE utf8 = rb_str_encode(string, rb_bson_utf8_string, 0, Qnil); return rb_enc_associate(utf8, rb_ascii8bit_encoding()); }
/** * Provide default new string with binary encoding. * * @example Check encoded and provide default new binary encoded string. * if (NIL_P(encoded)) encoded = rb_str_new_encoded_binary(); * * @return [ String ] The new string with binary encoding. * * @since 2.0.0 */ static VALUE rb_str_new_encoded_binary(void) { return rb_enc_str_new("", 0, rb_ascii8bit_encoding()); }
/* * call-seq: * Dir.new( string ) -> aDir * * Returns a new directory object for the named directory. */ static VALUE dir_initialize(int argc, VALUE *argv, VALUE dir) { struct dir_data *dp; static rb_encoding *fs_encoding; rb_encoding *intencoding, *extencoding; VALUE dirname, opt; static VALUE sym_intenc, sym_extenc; if (!sym_intenc) { sym_intenc = ID2SYM(rb_intern("internal_encoding")); sym_extenc = ID2SYM(rb_intern("external_encoding")); fs_encoding = rb_filesystem_encoding(); } intencoding = NULL; extencoding = fs_encoding; rb_scan_args(argc, argv, "11", &dirname, &opt); if (!NIL_P(opt)) { VALUE v, extenc=Qnil, intenc=Qnil; opt = rb_check_convert_type(opt, T_HASH, "Hash", "to_hash"); v = rb_hash_aref(opt, sym_intenc); if (!NIL_P(v)) intenc = v; v = rb_hash_aref(opt, sym_extenc); if (!NIL_P(v)) extenc = v; if (!NIL_P(extenc)) { extencoding = rb_to_encoding(extenc); if (!NIL_P(intenc)) { intencoding = rb_to_encoding(intenc); if (extencoding == intencoding) { rb_warn("Ignoring internal encoding '%s': it is identical to external encoding '%s'", RSTRING_PTR(rb_inspect(intenc)), RSTRING_PTR(rb_inspect(extenc))); intencoding = NULL; } } } else if (!NIL_P(intenc)) { rb_raise(rb_eArgError, "External encoding must be specified when internal encoding is given"); } } { rb_encoding *dirname_encoding = rb_enc_get(dirname); if (rb_usascii_encoding() != dirname_encoding && rb_ascii8bit_encoding() != dirname_encoding #if defined __APPLE__ && rb_utf8_encoding() != dirname_encoding #endif && extencoding != dirname_encoding) { if (!intencoding) intencoding = dirname_encoding; dirname = rb_str_transcode(dirname, rb_enc_from_encoding(extencoding)); } } FilePathValue(dirname); Data_Get_Struct(dir, struct dir_data, dp); if (dp->dir) closedir(dp->dir); if (dp->path) xfree(dp->path); dp->dir = NULL; dp->path = NULL; dp->intenc = intencoding; dp->extenc = extencoding; dp->dir = opendir(RSTRING_PTR(dirname)); if (dp->dir == NULL) { if (errno == EMFILE || errno == ENFILE) { rb_gc(); dp->dir = opendir(RSTRING_PTR(dirname)); } if (dp->dir == NULL) { rb_sys_fail(RSTRING_PTR(dirname)); } } dp->path = strdup(RSTRING_PTR(dirname)); return dir; }