static int random_next_int(long *seed, int n) { int bits, val; if (n <= 0) { /* invalid */ return 0; } if ((n & -n) == n) { /* n is a power of two */ return (int)((n * (long)random_next(seed, 31)) >> 31); } do { bits = random_next(seed, 31); val = bits % n; } while (bits - val + (n - 1) < 0); return val; }
int simple_rng::get_next_random() { random_next(); ++random_calls_; DBG_RND << "pulled user random " << random_pool_ << " for call " << random_calls_ << " with seed " << random_seed_ << '\n'; return (random_pool_ / 65536) % 32768; }
void simple_rng::seed_random(const int seed, const unsigned call_count) { random_pool_ = seed; random_seed_ = seed; for(random_calls_ = 0; random_calls_ < call_count; ++random_calls_) { random_next(); } DBG_RND << "Seeded random with " << random_seed_ << " with " << random_calls_ << " calls, pool is now at " << random_pool_ << '\n'; }
u16 dns_new_id(void) { if(id_random_ctx == NULL) { id_random_ctx = random_init_auto(); } u16 id = (u16)random_next(id_random_ctx); return id; }
static void fragment_memory(void) { #if FRAGMENT_MEMORY // Pre-fragment memory. We allocate a bunch of random-sized blobs, // shuffle them and free half of them. This creates a more realistic // memory layout, testing how well the library deals with real-world // memory usage rather than a nice flat empty malloc(). // With this random seed, it peaks at about 22 megs before freeing half. random_t random; random_seed(&random, 34986); for (int i = 0; i < MEMORY_COUNT; ++i) { size_t bytes = (1 << (random_next(&random) % 12)) + random_next(&random) % 8; memory[i] = malloc(bytes); } for (int i = 0; i < MEMORY_COUNT; ++i) { int j = random_next(&random) % (MEMORY_COUNT - i) + i; void* l = memory[i]; memory[i] = memory[j]; memory[j] = l; } for (int i = 0; i < MEMORY_COUNT / 2; ++i) free(memory[i]); #endif }
/** * Creates a pointer list pointing to the coeffitient * @param *context a context-structure for F5 * @return LSTG_ERROR if error or LSTG_OK if success */ uint32_t f5_create_coeff_list(f5_context *context) { uint32_t i, j, k, l, num_coeff = 0; // count non-zero coefficients // iterate components for (i = 0; i < context->stego_data->comp_num; i++) { num_coeff += (context->stego_data->comp[i].nblocks * 63); } // initiate coeff list context->coeff_list = (int16_t**)malloc(num_coeff*sizeof(int16_t*)); l = 0; for (i = 0; i < context->stego_data->comp_num; i++) { // iterate blocks for (j = 0; j < context->stego_data->comp[i].nblocks; j++) { // iterate coefficient, skip DC for (k = 1; k < 64; k++) { context->coeff_list[l] = &context->stego_data->comp[i].blocks[j].values[k]; l++; } } } context->list_len = num_coeff; uint32_t rand_num; int16_t *temp; // shuffle coeff_list for( i = 0; i < num_coeff; i++) { rand_num = random_next(context->random) % num_coeff; temp = context->coeff_list[0]; context->coeff_list[0] = context->coeff_list[rand_num]; context->coeff_list[rand_num] = temp; } return LSTG_OK; }
static ya_result xfr_query_callback(void* data) { xfr_query_schedule_param *xqsp = (xfr_query_schedule_param*)data; ya_result return_value; ya_result scheduler_status; u32 refreshed_time = 0; u32 retried_time = time(NULL); /* * Zone refresh will be enabled (again) here. * * xfr_query_mark_zone_loaded * */ if(ISOK(xqsp->return_value)) { /* success but type == 0 => transfer done */ if(xqsp->type != 0) { log_info("slave: %{dnstype}: proceeding with transfer of %{dnsname}", &xqsp->type, xqsp->origin); } } else { log_err("slave: %{dnstype}: transfer of %{dnsname} failed: %r", &xqsp->type, xqsp->origin, xqsp->return_value); /* * Here (?) put the invalid zone placeholder if it is not there yet */ } switch(xqsp->type) { case TYPE_AXFR: { /* * Load the axfr */ if(ISOK(xqsp->return_value)) { axfr_query_axfr_load_param *aqalp; MALLOC_OR_DIE(axfr_query_axfr_load_param*, aqalp, sizeof(axfr_query_axfr_load_param), GENERIC_TAG); log_info("slave: opening AXFR for %{dnsname} %d", xqsp->origin, xqsp->loaded_serial); if(ISOK(return_value = zone_axfr_reader_open_with_serial(g_config->xfr_path, xqsp->origin, xqsp->loaded_serial, &aqalp->zr))) { /* * The system is ready to load an AXFR * A zone is already in place (by design), there may be an old zone that is now irrelevant. * At this point ... * * If the serial are not the same * MT the old zone must be destroyed (if it exist) * then * MT The new zone has to be created (loaded from the AXFR file) * Else * ST The new zone is actually the old zone, and the old zone is non-existent * EndIf * * then * * ST The new zone and the placeholder zone have to be swapped, then the placeholder has to be destroyed * */ zdb_zone *zone = zdb_zone_find_from_dnsname((zdb*)xqsp->db, xqsp->origin, CLASS_IN); if(zone != NULL) { zdb_zone_truncate_invalidate(zone); } /** * schedule the axfr load */ aqalp->old_zone = NULL; /* old zone */ aqalp->new_zone = NULL; aqalp->db = xqsp->db; aqalp->origin = dnsname_dup(xqsp->origin); scheduler_status = SCHEDULER_TASK_PROGRESS; thread_pool_schedule_job(xfr_query_axfr_load_thread, aqalp, NULL, "axfr load"); break; } } /** * @todo If the "old_zone" exists, then ... (destroy ? swap back ? expired ?) * */ /* * An issue occurred (AXFR transfer, load) */ log_err("slave: unable to load the axfr (retry set in %d seconds)", g_config->axfr_retry_delay); alarm_event_node *event = alarm_event_alloc(); event->epoch = time(NULL) + g_config->axfr_retry_delay; if(g_config->axfr_retry_jitter > 0) { random_ctx rndctx = thread_pool_get_random_ctx(); u32 jitter = random_next(rndctx) % g_config->axfr_retry_jitter; event->epoch += jitter; } event->function = scheduler_axfr_query_alarm; event->args = xqsp; event->key = ALARM_KEY_ZONE_AXFR_QUERY; event->flags = ALARM_DUP_NOP; event->text = "scheduler_axfr_query_alarm"; zdb *db = (zdb*)xqsp->db; alarm_set(db->alarm_handle, event); /* DO NOT FREE xqsp, SO DO NOT BREAK : return now */ return SCHEDULER_TASK_FINISHED; } case TYPE_IXFR: { /** * Load the ixfr (single thread, zone locked) */ scheduler_status = SCHEDULER_TASK_FINISHED; if(ISOK(xqsp->return_value)) { zdb_zone *dbzone = zdb_zone_find_from_dnsname((zdb*)xqsp->db, xqsp->origin, CLASS_IN); if(dbzone == NULL) { log_err("slave: zone %{dnsname} journal has vanished", xqsp->origin); break; } log_info("slave: opening journal for '%{dnsname}'", xqsp->origin); zdb_zone_lock(dbzone, ZDB_ZONE_MUTEX_XFR); if(ISOK(return_value = zdb_icmtl_replay(dbzone, g_config->xfr_path, xqsp->serial_start_offset, xqsp->loaded_serial, ZDB_ICMTL_REPLAY_SERIAL_OFFSET|ZDB_ICMTL_REPLAY_SERIAL_LIMIT))) { log_info("slave: replayed %d records changes", return_value); dbzone->apex->flags &= ~ZDB_RR_LABEL_INVALID_ZONE; refreshed_time = time(NULL); } else { log_err("slave: replay error: %r this is bad: invalidate the zone and ask for an AXFR", return_value); /** @todo INVALIDATE AND AXFR */ } /** @todo invalitate if retried_time > expired_time */ zdb_zone_unlock(dbzone, ZDB_ZONE_MUTEX_XFR); } log_info("slave: journal transfer done"); xfr_query_mark_zone_loaded((zdb*)g_config->database, xqsp->origin, refreshed_time, retried_time); scheduler_status = SCHEDULER_TASK_FINISHED; break; } default: { refreshed_time = time(NULL); log_info("slave: transfer done"); xfr_query_mark_zone_loaded((zdb*)g_config->database, xqsp->origin, refreshed_time, retried_time); scheduler_status = SCHEDULER_TASK_FINISHED; break; } } /* switch return_value */
static inline jsdouble random_nextDouble(JSContext *cx) { return jsdouble((random_next(cx, 26) << 27) + random_next(cx, 27)) / RNG_DSCALE; }
uint32_t random_uint32(void) { random_init(); return random_next(); }
static inline jsdouble random_nextDouble(JSThreadData *data) { return jsdouble((random_next(data, 26) << 27) + random_next(data, 27)) / RNG_DSCALE; }