void wi_test_timer(void) { #ifdef WI_PTHREADS wi_timer_t *timer; _wi_test_timer_lock = wi_autorelease(wi_condition_lock_init_with_condition(wi_condition_lock_alloc(), 0)); timer = wi_autorelease(wi_timer_init_with_function(wi_timer_alloc(), _wi_test_timer_function, 0.001, false)); wi_timer_schedule(timer); if(wi_condition_lock_lock_when_condition(_wi_test_timer_lock, 1, 1.0)) { WI_TEST_ASSERT_EQUALS(_wi_test_timer_hits, 5U, ""); wi_condition_lock_unlock(_wi_test_timer_lock); } else { WI_TEST_FAIL("Timed out waiting for timer, currently at %u %s", _wi_test_timer_hits, _wi_test_timer_hits == 1 ? "hit" : "hits"); } #endif }
static wi_timer_t * _wi_timer_first_timer(void) { wi_timer_t *timer; wi_array_rdlock(_wi_timers); timer = wi_autorelease(wi_retain(wi_array_first_data(_wi_timers))); wi_array_unlock(_wi_timers); return timer; }
wd_chat_t * wd_chats_chat_with_cid(wd_cid_t cid) { wd_chat_t *chat; wi_dictionary_rdlock(wd_chats); chat = wi_autorelease(wi_retain(wi_dictionary_data_for_key(wd_chats, wi_number_with_int32(cid)))); wi_dictionary_unlock(wd_chats); return chat; }
wd_topic_t * wd_chat_topic(wd_chat_t *chat) { wd_topic_t *topic; wi_recursive_lock_lock(chat->lock); topic = wi_autorelease(wi_retain(chat->topic)); wi_recursive_lock_unlock(chat->lock); return topic; }
wi_uinteger_t wi_config_port_for_name(wi_config_t *config, wi_string_t *name) { wi_number_t *value; wi_lock_lock(config->lock); value = wi_autorelease(wi_retain(wi_dictionary_data_for_key(config->values, name))); wi_lock_unlock(config->lock); return value ? wi_number_integer(value) : 0; }
wi_time_interval_t wi_config_time_interval_for_name(wi_config_t *config, wi_string_t *name) { wi_number_t *value; wi_lock_lock(config->lock); value = wi_autorelease(wi_retain(wi_dictionary_data_for_key(config->values, name))); wi_lock_unlock(config->lock); return value ? wi_number_double(value) : 0.0; }
wi_regexp_t * wi_config_regexp_for_name(wi_config_t *config, wi_string_t *name) { wi_regexp_t *value; wi_lock_lock(config->lock); value = wi_autorelease(wi_retain(wi_dictionary_data_for_key(config->values, name))); wi_lock_unlock(config->lock); return value; }
wt_server_t * wt_servers_server_with_key(wi_string_t *key) { wt_server_t *server; wi_dictionary_rdlock(wt_servers); server = wi_autorelease(wi_retain(wi_dictionary_data_for_key(wt_servers, key))); wi_dictionary_unlock(wt_servers); return server; }
wd_user_t * wd_users_user_with_uid(wd_uid_t uid) { wd_user_t *user; wi_hash_rdlock(wd_users); user = wi_autorelease(wi_retain(wi_hash_data_for_key(wd_users, wi_number_with_int32(uid)))); wi_hash_unlock(wd_users); return user; }
wd_user_t * wd_users_user_with_id(wd_uid_t id) { wd_user_t *user; wi_dictionary_rdlock(wd_users); user = wi_autorelease(wi_retain(wi_dictionary_data_for_key(wd_users, wi_number_with_int32(id)))); wi_dictionary_unlock(wd_users); return user; }
wi_data_t * wi_data_by_appending_bytes(wi_data_t *data, const void *bytes, wi_uinteger_t length) { wi_mutable_data_t *newdata; newdata = wi_copy(data); wi_mutable_data_append_bytes(newdata, bytes, length); wi_runtime_make_immutable(data); return wi_autorelease(newdata); }
wi_mutable_string_t * wi_mutable_string_with_format(wi_string_t *fmt, ...) { wi_mutable_string_t *string; va_list ap; va_start(ap, fmt); string = wi_string_init_with_format_and_arguments(wi_mutable_string_alloc(), fmt, ap); va_end(ap); return wi_autorelease(string); }
wi_data_t * wi_data_by_appending_data(wi_data_t *data, wi_data_t *append_data) { wi_mutable_data_t *newdata; newdata = wi_mutable_copy(data); wi_mutable_data_append_bytes(newdata, append_data->bytes, append_data->length); wi_runtime_make_immutable(data); return wi_autorelease(newdata); }
void wi_test_dsa_accessors(void) { #ifdef WI_DSA wi_dsa_t *dsa; dsa = wi_autorelease(wi_dsa_init_with_bits(wi_dsa_alloc(), 512)); WI_TEST_ASSERT_TRUE(wi_data_length(wi_dsa_public_key(dsa)) > 0, ""); WI_TEST_ASSERT_TRUE(wi_data_length(wi_dsa_private_key(dsa)) > 0, ""); WI_TEST_ASSERT_EQUALS(wi_dsa_bits(dsa), 384U, ""); #endif }
wi_string_t * wi_time_interval_rfc3339_string(wi_time_interval_t interval) { wi_mutable_string_t *string; string = wi_mutable_copy(wi_time_interval_string_with_format(interval, WI_STR("%Y-%m-%dT%H:%M:%S%z"))); wi_mutable_string_insert_string_at_index(string, WI_STR(":"), 22); wi_runtime_make_immutable(string); return wi_autorelease(string); }
void wi_test_readwrite_lock_runtime_functions(void) { #ifdef WI_PTHREADS wi_readwrite_lock_t *lock; lock = wi_autorelease(wi_readwrite_lock_init(wi_readwrite_lock_alloc())); WI_TEST_ASSERT_EQUALS(wi_runtime_id(lock), wi_readwrite_lock_runtime_id(), ""); WI_TEST_ASSERT_NOT_EQUALS(wi_string_index_of_string(wi_description(lock), WI_STR("wi_readwrite_lock_t"), 0), WI_NOT_FOUND, ""); #endif }
void wr_transfers_download(wi_string_t *path) { wr_transfer_t *transfer; transfer = wi_autorelease(wr_transfer_init_download(wr_transfer_alloc())); transfer->name = wi_retain(wi_string_last_path_component(path)); transfer->master_path = wi_retain(path); wi_mutable_array_add_data(wr_transfers, transfer); if(transfer->tid == 1) wr_transfer_start(transfer); }
wi_string_t * wi_file_read_to_end_of_file(wi_file_t *file) { wi_string_t *string; char buffer[WI_FILE_BUFFER_SIZE]; wi_integer_t bytes; string = wi_string_init(wi_string_alloc()); while((bytes = wi_file_read_buffer(file, buffer, sizeof(buffer)))) wi_string_append_bytes(string, buffer, bytes); return wi_autorelease(string); }
void wi_test_host_runtime_functions(void) { wi_host_t *host1, *host2, *host3, *host4, *host5, *host6; host1 = wi_host(); host2 = wi_autorelease(wi_copy(host1)); host3 = wi_host_with_string(WI_STR("localhost")); host4 = wi_autorelease(wi_copy(host3)); host5 = wi_host_with_string(WI_STR("aab119a592b9e23779b66649677b436d24b35aaa5ad5beadf4c2a945b70577e5.com")); host6 = wi_autorelease(wi_copy(host5)); WI_TEST_ASSERT_EQUAL_INSTANCES(host1, host2, ""); WI_TEST_ASSERT_EQUAL_INSTANCES(host3, host4, ""); WI_TEST_ASSERT_EQUAL_INSTANCES(host5, host6, ""); WI_TEST_ASSERT_NOT_EQUAL_INSTANCES(host1, host3, ""); WI_TEST_ASSERT_NOT_EQUAL_INSTANCES(host2, host3, ""); WI_TEST_ASSERT_NOT_EQUAL_INSTANCES(host1, host4, ""); WI_TEST_ASSERT_NOT_EQUAL_INSTANCES(host2, host4, ""); WI_TEST_ASSERT_NOT_EQUAL_INSTANCES(host1, host5, ""); WI_TEST_ASSERT_NOT_EQUAL_INSTANCES(host2, host5, ""); WI_TEST_ASSERT_NOT_EQUAL_INSTANCES(host1, host6, ""); WI_TEST_ASSERT_NOT_EQUAL_INSTANCES(host2, host6, ""); WI_TEST_ASSERT_EQUALS(wi_hash(host1), wi_hash(host2), ""); WI_TEST_ASSERT_EQUALS(wi_hash(host3), wi_hash(host4), ""); WI_TEST_ASSERT_EQUALS(wi_hash(host5), wi_hash(host6), ""); WI_TEST_ASSERT_EQUALS(wi_runtime_id(host1), wi_host_runtime_id(), ""); WI_TEST_ASSERT_EQUALS(wi_runtime_id(host2), wi_host_runtime_id(), ""); WI_TEST_ASSERT_EQUALS(wi_runtime_id(host3), wi_host_runtime_id(), ""); WI_TEST_ASSERT_EQUALS(wi_runtime_id(host4), wi_host_runtime_id(), ""); WI_TEST_ASSERT_EQUALS(wi_runtime_id(host5), wi_host_runtime_id(), ""); WI_TEST_ASSERT_EQUALS(wi_runtime_id(host6), wi_host_runtime_id(), ""); #ifdef HAVE_GETIFADDRS WI_TEST_ASSERT_NOT_EQUALS(wi_string_index_of_string(wi_description(host1), WI_STR("127.0.0.1"), 0), WI_NOT_FOUND, ""); #else WI_TEST_ASSERT_NOT_EQUALS(wi_string_index_of_string(wi_description(host1), WI_STR("0.0.0.0"), 0), WI_NOT_FOUND, ""); #endif WI_TEST_ASSERT_NOT_EQUALS(wi_string_index_of_string(wi_description(host3), WI_STR("127.0.0.1"), 0), WI_NOT_FOUND, ""); }
void wi_test_timer(void) { #ifdef WI_PTHREADS wi_timer_t *timer; timer = wi_autorelease(wi_timer_init_with_function(wi_timer_alloc(), _wi_test_timer_function, 0.01, false)); wi_timer_set_data(timer, &_wi_test_timer_hits); wi_timer_schedule(timer); wi_thread_sleep(0.1); WI_TEST_ASSERT_EQUALS(_wi_test_timer_hits, 5U, ""); #endif }
void wi_test_regexp_runtime_functions(void) { wi_regexp_t *regexp1, *regexp2; regexp1 = wi_regexp_with_pattern(WI_STR("foobar"), 0); regexp2 = wi_autorelease(wi_copy(regexp1)); WI_TEST_ASSERT_EQUAL_INSTANCES(regexp1, regexp2, ""); WI_TEST_ASSERT_EQUALS(wi_hash(regexp1), wi_hash(regexp2), ""); WI_TEST_ASSERT_EQUALS(wi_runtime_id(regexp1), wi_regexp_runtime_id(), ""); WI_TEST_ASSERT_EQUALS(wi_runtime_id(regexp2), wi_regexp_runtime_id(), ""); WI_TEST_ASSERT_NOT_EQUALS(wi_string_index_of_string(wi_description(regexp1), wi_regexp_pattern(regexp1), 0), WI_NOT_FOUND, ""); }
wi_hash_t * wi_p7_message_fields(wi_p7_message_t *p7_message) { wi_hash_t *fields; wi_string_t *field_name, *type_name, *field_value; xmlNodePtr node; unsigned char *buffer, *start; wi_p7_type_t type_id; uint32_t message_size, field_id, field_size; fields = wi_hash_init(wi_hash_alloc()); if(p7_message->serialization == WI_P7_BINARY) { message_size = p7_message->binary_size - _WI_P7_MESSAGE_BINARY_HEADER_SIZE; buffer = start = p7_message->binary_buffer + _WI_P7_MESSAGE_BINARY_HEADER_SIZE; while((uint32_t) (buffer - start) < message_size) { field_id = wi_read_swap_big_to_host_int32(buffer, 0); buffer += sizeof(field_id); field_size = wi_p7_spec_field_size(p7_message->spec, field_id); if(field_size == 0) { field_size = wi_read_swap_big_to_host_int32(buffer, 0); buffer += sizeof(field_size); } field_name = wi_p7_spec_field_name(p7_message->spec, field_id); type_id = wi_p7_spec_field_type(p7_message->spec, field_id); field_value = _wi_p7_message_field_string_value(p7_message, field_name, type_id); wi_hash_set_data_for_key(fields, field_value, field_name); buffer += field_size; } } else { if(p7_message->xml_root_node) { for(node = p7_message->xml_root_node->children; node != NULL; node = node->next) { if(node->type == XML_ELEMENT_NODE) { field_name = wi_p7_xml_string_for_attribute(node, WI_STR("name")); type_name = wi_p7_xml_string_for_attribute(node, WI_STR("type")); type_id = wi_p7_spec_type_id(p7_message->spec, type_name); field_value = _wi_p7_message_field_string_value(p7_message, field_name, type_id); wi_hash_set_data_for_key(fields, field_value, field_name); } } } } return wi_autorelease(fields); }
void wd_transfers_queue_upload(wi_string_t *path, wi_file_offset_t size, wi_string_t *checksum) { wd_user_t *user = wd_users_user_for_thread(); wi_string_t *realpath, *filechecksum; wd_transfer_t *transfer; wi_file_offset_t offset; wi_fs_stat_t sb; realpath = wi_string_by_resolving_aliases_in_path(wd_files_real_path(path)); if(wi_fs_stat_path(realpath, &sb)) { wd_reply(521, WI_STR("File or Directory Exists")); return; } if(!wi_string_has_suffix(realpath, WI_STR(WD_TRANSFERS_PARTIAL_EXTENSION))) realpath = wi_string_by_appending_path_extension(realpath, WI_STR(WD_TRANSFERS_PARTIAL_EXTENSION)); if(!wi_fs_stat_path(realpath, &sb)) { offset = 0; } else { offset = sb.size; if(sb.size >= WD_FILES_CHECKSUM_SIZE) { filechecksum = wi_fs_sha1_for_path(realpath, WD_FILES_CHECKSUM_SIZE); if(!wi_is_equal(filechecksum, checksum)) { wd_reply(522, WI_STR("Checksum Mismatch")); return; } } } transfer = wi_autorelease(wd_transfer_init_upload_with_user(wd_transfer_alloc(), user)); transfer->path = wi_retain(path); transfer->realpath = wi_retain(realpath); transfer->size = size; transfer->offset = offset; transfer->transferred = offset; wi_lock_lock(wd_transfers_update_queue_lock); wi_array_wrlock(wd_transfers); wi_mutable_array_add_data(wd_transfers, transfer); wi_array_unlock(wd_transfers); wd_transfers_update_queue(); wi_lock_unlock(wd_transfers_update_queue_lock); }
wi_array_t * wi_array_subarray_with_range(wi_array_t *array, wi_range_t range) { wi_array_t *newarray; wi_uinteger_t i; _WI_ARRAY_INDEX_ASSERT(array, range.location); _WI_ARRAY_INDEX_ASSERT(array, range.location + range.length - 1); newarray = wi_array_init_with_capacity(wi_array_alloc(), range.length); for(i = range.location; i < range.location + range.length; i++) wi_array_add_data(newarray, WI_ARRAY(array, i)); return wi_autorelease(newarray); }
int32_t wi_socket_recvfrom(wi_socket_t *socket, wi_socket_context_t *context, char *buffer, size_t length, wi_address_t **address) { struct sockaddr_storage ss; char *inbuffer = NULL; socklen_t sslength; int bytes; sslength = sizeof(ss); #ifdef WI_SSL if(context && context->priv_rsa) { inbuffer = wi_malloc(length); bytes = recvfrom(socket->sd, inbuffer, length, 0, (struct sockaddr *) &ss, &sslength); if(bytes < 0) { wi_error_set_errno(errno); goto end; } bytes = RSA_private_decrypt(bytes, (unsigned char *) inbuffer, (unsigned char *) buffer, context->priv_rsa, RSA_PKCS1_OAEP_PADDING); if(bytes < 0) { wi_error_set_ssl_error(); goto end; } } else { #endif bytes = recvfrom(socket->sd, buffer, length, 0, (struct sockaddr *) &ss, &sslength); if(bytes < 0) { wi_error_set_errno(errno); goto end; } #ifdef WI_SSL } #endif end: *address = (sslength > 0) ? wi_autorelease(wi_address_init_with_sa(wi_address_alloc(), (struct sockaddr *) &ss)) : NULL; if(inbuffer) wi_free(inbuffer); return bytes; }
wi_set_t * wi_set_with_data(void *data0, ...) { wi_set_t *set; void *data; va_list ap; set = wi_set_init_with_capacity(wi_set_alloc(), 0, false); _wi_set_add_data(set, data0); va_start(ap, data0); while((data = va_arg(ap, void *))) _wi_set_add_data(set, data); va_end(ap); return wi_autorelease(set); }
wi_array_t * wi_array_with_data(void *data0, ...) { wi_array_t *array; void *data; va_list ap; array = wi_array_init_with_capacity(wi_array_alloc(), 0); wi_array_add_data(array, data0); va_start(ap, data0); while((data = va_arg(ap, void *))) wi_array_add_data(array, data); va_end(ap); return wi_autorelease(array); }
void wi_parse_wire_command(wi_string_t *buffer, wi_string_t **out_command, wi_string_t **out_arguments) { wi_uinteger_t index; index = wi_string_index_of_string(buffer, WI_STR(" "), 0); if(index != WI_NOT_FOUND) { *out_command = wi_string_substring_to_index(buffer, index); *out_arguments = wi_string_substring_from_index(buffer, index + 1); } else { *out_command = wi_autorelease(wi_copy(buffer)); *out_arguments = wi_string(); } if(wi_string_has_prefix(*out_command, WI_STR("/"))) *out_command = wi_string_by_deleting_characters_to_index(*out_command, 1); }
wi_integer_t wi_socket_recvfrom(wi_socket_t *socket, char *buffer, size_t length, wi_address_t **address) { struct sockaddr_storage ss; socklen_t sslength; wi_integer_t bytes; sslength = sizeof(ss); bytes = recvfrom(socket->sd, buffer, length, 0, (struct sockaddr *) &ss, &sslength); *address = (sslength > 0) ? wi_autorelease(wi_address_init_with_sa(wi_address_alloc(), (struct sockaddr *) &ss)) : NULL; if(bytes < 0) { wi_error_set_errno(errno); return -1; } return bytes; }
wi_string_t * wi_socket_read_string(wi_socket_t *socket, wi_time_interval_t timeout) { wi_mutable_string_t *string; char buffer[WI_SOCKET_BUFFER_SIZE]; int bytes = -1; string = wi_autorelease(wi_string_init_with_capacity(wi_mutable_string_alloc(), WI_SOCKET_BUFFER_SIZE)); bytes = _wi_socket_read_buffer(socket, timeout, buffer, WI_SOCKET_BUFFER_SIZE); if(bytes <= 0) return NULL; wi_mutable_string_append_bytes(string, buffer, bytes); wi_runtime_make_immutable(string); return string; }