/* Rotate substring left by one bit. */ static void bitarray_rotate_left_one(bitarray_t *ba, size_t bit_off, size_t bit_len) { size_t i; bool first_bit = bitarray_get(ba, bit_off); for (i = bit_off; i + 1 < bit_off + bit_len; i++) bitarray_set(ba, i, bitarray_get(ba, i + 1)); bitarray_set(ba, i, first_bit); }
static void bitarray_reverse(bitarray_t *const bitarray, const size_t bit_offset, const size_t bit_length) { bool temp_bit; for (size_t i = bit_offset; i < (bit_offset + bit_length) / 2; i++) { bitarray_set(bitarray, i, bitarray_get(bitarray,i) ^ bitarray_get(bitarray, bit_offset + bit_length - (i - bit_offset) - 1)); bitarray_set(bitarray, bit_offset + bit_length - (i - bit_offset) - 1, bitarray_get(bitarray,i) ^ bitarray_get(bitarray, bit_offset + bit_length - (i - bit_offset) - 1)); bitarray_set(bitarray, i, bitarray_get(bitarray,i) ^ bitarray_get(bitarray, bit_offset + bit_length - (i - bit_offset) - 1) ); } }
static void bitarray_rotate_left_one(bitarray_t *const bitarray, const size_t bit_offset, const size_t bit_length) { // Grab the first bit in the range, shift everything left by one, and // then stick the first bit at the end. const bool first_bit = bitarray_get(bitarray, bit_offset); size_t i; for (i = bit_offset; i + 1 < bit_offset + bit_length; i++) { bitarray_set(bitarray, i, bitarray_get(bitarray, i + 1)); } bitarray_set(bitarray, i, first_bit); }
static void testutil_newrand(const size_t bit_sz, const unsigned int seed) { // If we somehow managed to avoid freeing test_bitarray after a previous // test, go free it now. if (test_bitarray != NULL) { bitarray_free(test_bitarray); } test_bitarray = bitarray_new(bit_sz); assert(test_bitarray != NULL); // Reseed the RNG with whatever we were passed; this ensures that we can // repeat the test deterministically by specifying the same seed. srand(seed); for (size_t i = 0; i < bit_sz; i++) { bitarray_set(test_bitarray, i, randbit()); } // If we were asked to be verbose, go ahead and show the bit array and // the random seed. if (test_verbose) { bitarray_fprint(stdout, test_bitarray); fprintf(stdout, " newrand sz=%zu, seed=%u\n", bit_sz, seed); } }
static void test_pick_circid(void *arg) { bitarray_t *ba = NULL; channel_t *chan1, *chan2; circid_t circid; int i; (void) arg; chan1 = tor_malloc_zero(sizeof(channel_t)); chan2 = tor_malloc_zero(sizeof(channel_t)); chan2->wide_circ_ids = 1; chan1->circ_id_type = CIRC_ID_TYPE_NEITHER; tt_int_op(0, ==, get_unique_circ_id_by_chan(chan1)); /* Basic tests, with no collisions */ chan1->circ_id_type = CIRC_ID_TYPE_LOWER; for (i = 0; i < 50; ++i) { circid = get_unique_circ_id_by_chan(chan1); tt_uint_op(0, <, circid); tt_uint_op(circid, <, (1<<15)); } chan1->circ_id_type = CIRC_ID_TYPE_HIGHER; for (i = 0; i < 50; ++i) { circid = get_unique_circ_id_by_chan(chan1); tt_uint_op((1<<15), <, circid); tt_uint_op(circid, <, (1<<16)); } chan2->circ_id_type = CIRC_ID_TYPE_LOWER; for (i = 0; i < 50; ++i) { circid = get_unique_circ_id_by_chan(chan2); tt_uint_op(0, <, circid); tt_uint_op(circid, <, (1u<<31)); } chan2->circ_id_type = CIRC_ID_TYPE_HIGHER; for (i = 0; i < 50; ++i) { circid = get_unique_circ_id_by_chan(chan2); tt_uint_op((1u<<31), <, circid); } /* Now make sure that we can behave well when we are full up on circuits */ chan1->circ_id_type = CIRC_ID_TYPE_LOWER; chan2->circ_id_type = CIRC_ID_TYPE_LOWER; chan1->wide_circ_ids = chan2->wide_circ_ids = 0; ba = bitarray_init_zero((1<<15)); for (i = 0; i < (1<<15); ++i) { circid = get_unique_circ_id_by_chan(chan1); if (circid == 0) { tt_int_op(i, >, (1<<14)); break; } tt_uint_op(circid, <, (1<<15)); tt_assert(! bitarray_is_set(ba, circid)); bitarray_set(ba, circid); channel_mark_circid_unusable(chan1, circid); }
static void s6rc_graph_closure_rec (recinfo_t *recinfo, unsigned int i) { if (!bitarray_peek(recinfo->mark, i)) { unsigned int j = recinfo->db->services[i].ndeps[recinfo->h] ; bitarray_set(recinfo->mark, i) ; while (j--) s6rc_graph_closure_rec(recinfo, recinfo->db->deps[recinfo->h * recinfo->db->ndeps + recinfo->db->services[i].deps[recinfo->h] + j]) ; recinfo->bits[i] |= recinfo->mask ; } }
/** Run unit tests for bitarray code */ static void test_container_bitarray(void *arg) { bitarray_t *ba = NULL; int i, j, ok=1; (void)arg; ba = bitarray_init_zero(1); tt_assert(ba); tt_assert(! bitarray_is_set(ba, 0)); bitarray_set(ba, 0); tt_assert(bitarray_is_set(ba, 0)); bitarray_clear(ba, 0); tt_assert(! bitarray_is_set(ba, 0)); bitarray_free(ba); ba = bitarray_init_zero(1023); for (i = 1; i < 64; ) { for (j = 0; j < 1023; ++j) { if (j % i) bitarray_set(ba, j); else bitarray_clear(ba, j); } for (j = 0; j < 1023; ++j) { if (!bool_eq(bitarray_is_set(ba, j), j%i)) ok = 0; } tt_assert(ok); if (i < 7) ++i; else if (i == 28) i = 32; else i += 7; } done: if (ba) bitarray_free(ba); }
static void handle_reply(void *arg) { #ifdef TRACK_RESPONSES rsa_work_t *rw = arg; /* Naughty cast, but only looking at serial. */ tor_assert(! bitarray_is_set(received, rw->serial)); bitarray_set(received,rw->serial); #endif tor_free(arg); ++n_received; }
/** Run unit tests for bitarray code */ static void test_container_bitarray(void *unused) { bitarray_t *ba = NULL; int i, j; ba = bitarray_init_zero(1); tt_assert(ba); tt_assert(! bitarray_is_set(ba, 0)); bitarray_set(ba, 0); tt_assert(bitarray_is_set(ba, 0)); bitarray_clear(ba, 0); tt_assert(! bitarray_is_set(ba, 0)); bitarray_free(ba); ba = bitarray_init_zero(1023); for (i = 1; i < 64; ) { for (j = 0; j < 1023; ++j) { if (j % i) bitarray_set(ba, j); else bitarray_clear(ba, j); } for (j = 0; j < 1023; ++j) { tt_bool_op(bitarray_is_set(ba, j), ==, j%i); } if (i < 7) ++i; else if (i == 28) i = 32; else i += 7; } end: if (ba) bitarray_free(ba); }
static void mark_handled(int serial) { #ifdef TRACK_RESPONSES tor_mutex_acquire(&bitmap_mutex); tor_assert(serial < handled_len); tor_assert(! bitarray_is_set(handled, serial)); bitarray_set(handled, serial); tor_mutex_release(&bitmap_mutex); #else (void)serial; #endif }
int gensetdyn_new (gensetdyn_ref g, unsigned int *i) { register unsigned int r ; if (g->bits.len < g->storage.len) r = bitarray_firstclear((unsigned char *)g->bits.s, g->storage.len) ; else { if (!gensetdyn_readyplus(g, 1)) return 0 ; r = g->storage.len++ ; } g->bits.len++ ; bitarray_set((unsigned char *)g->bits.s, r) ; *i = r ; return 1 ; }
/* Create a new bitarray in test_ba of the specified size and fill it with random data based on the seed given. For a given seed number, the pseudorandom data will be the same (at least on the same glibc implementation). */ static void testutil_newrand(size_t bit_sz, unsigned int seed) { size_t i; if (test_ba != NULL) bitarray_free(test_ba); test_ba = bitarray_new(bit_sz); assert(test_ba != NULL); srand(seed); for (i = 0; i < bit_sz; i++) { bool temp = randbit() ; bitarray_set(test_ba, i, temp); } if (test_verbose) { bitarray_fprint(stdout, test_ba); fprintf(stdout, "\n"); fprintf(stdout, " newrand sz=%llu, seed=%u\n", (unsigned long long) bit_sz, seed); } }
/* Create a new bitarray in test_ba by parsing a string of 0s and 1s, e.g. "0101011011". */ static void testutil_frmstr(const char *bitstr) { size_t sl = strlen(bitstr), i; if (test_ba != NULL) bitarray_free(test_ba); test_ba = bitarray_new(sl); assert(test_ba != NULL); size_t myflipcount = 0; bool curbit, prevbit = false; for (i = 0; i < sl; i++) { curbit = boolfromchar(bitstr[i]); if (i != 0 && curbit != prevbit) myflipcount++; bitarray_set(test_ba, i, curbit); prevbit = curbit; } bitarray_fprint(stdout, test_ba); if (test_verbose) { fprintf(stdout, " newstr lit=%s\n", bitstr); testutil_expect(bitstr, myflipcount); } }
static void NS(test_main)(void *arg) { routerset_t *set = routerset_new(); int contains = 1; (void)arg; NS_MOCK(compare_tor_addr_to_addr_policy); NS_MOCK(geoip_get_country_by_addr); set->countries = bitarray_init_zero(1); bitarray_set(set->countries, 1); contains = routerset_contains(set, MOCK_TOR_ADDR_PTR, 0, NULL, NULL, -1); routerset_free(set); tt_int_op(contains, ==, 0); tt_int_op(CALLED(compare_tor_addr_to_addr_policy), ==, 1); tt_int_op(CALLED(geoip_get_country_by_addr), ==, 1); done: ; }
void testutil_frmstr(const char *const bitstring) { const size_t bitstring_length = strlen(bitstring); // If we somehow managed to avoid freeing test_bitarray after a previous // test, go free it now. if (test_bitarray != NULL) { bitarray_free(test_bitarray); } test_bitarray = bitarray_new(bitstring_length); assert(test_bitarray != NULL); bool current_bit; for (size_t i = 0; i < bitstring_length; i++) { current_bit = boolfromchar(bitstring[i]); bitarray_set(test_bitarray, i, current_bit); } bitarray_fprint(stdout, test_bitarray); if (test_verbose) { fprintf(stdout, " newstr lit=%s\n", bitstring); testutil_expect(bitstring); } }
/** Update the routerset's <b>countries</b> bitarray_t. Called whenever * the GeoIP IPv4 database is reloaded. */ void routerset_refresh_countries(routerset_t *target) { int cc; bitarray_free(target->countries); if (!geoip_is_loaded(AF_INET)) { target->countries = NULL; target->n_countries = 0; return; } target->n_countries = geoip_get_n_countries(); target->countries = bitarray_init_zero(target->n_countries); SMARTLIST_FOREACH_BEGIN(target->country_names, const char *, country) { cc = geoip_get_country(country); if (cc >= 0) { tor_assert(cc < target->n_countries); bitarray_set(target->countries, cc); } else { log_warn(LD_CONFIG, "Country code '%s' is not recognized.", country); } } SMARTLIST_FOREACH_END(country); }
static size_t lpc2_flash_writeDirect(struct KBlock *blk, block_idx_t idx, const void *_buf, size_t offset, size_t size) { ASSERT(offset == 0); ASSERT(FLASH_PAGE_SIZE_BYTES == size); Flash *fls = FLASH_CAST(blk); if (!(fls->blk.priv.flags & KB_WRITE_ONCE)) ASSERT(sector_size(idx) <= FLASH_PAGE_SIZE_BYTES); const uint8_t *buf = (const uint8_t *)_buf; cpu_flags_t flags; //Compute page address of current page. uint32_t addr = idx * blk->blk_size; uint32_t sector = addr_to_sector(addr); // Compute the first page index in the sector to manage the status int idx_sector = sector_addr(sector) / blk->blk_size; LOG_INFO("Writing page[%ld]sector[%ld]idx[%d]\n", idx, sector, idx_sector); IRQ_SAVE_DISABLE(flags); IapCmd cmd; IapRes res; cmd.cmd = PREPARE_SECTOR_FOR_WRITE; cmd.param[0] = cmd.param[1] = sector; iap(&cmd, &res); if (res.status != CMD_SUCCESS) goto flash_error; if ((fls->blk.priv.flags & KB_WRITE_ONCE) && bitarray_isRangeFull(&lpc2_bitx, idx_sector, erase_group[sector])) { kputs("blocchi pieni\n"); ASSERT(0); goto flash_error; } bool erase = false; if ((fls->blk.priv.flags & KB_WRITE_ONCE) && bitarray_isRangeEmpty(&lpc2_bitx, idx_sector, erase_group[sector])) erase = true; if (!(fls->blk.priv.flags & KB_WRITE_ONCE)) erase = true; if (erase) { cmd.cmd = ERASE_SECTOR; cmd.param[0] = cmd.param[1] = sector; cmd.param[2] = CPU_FREQ / 1000; iap(&cmd, &res); if (res.status != CMD_SUCCESS) goto flash_error; } LOG_INFO("Writing page [%ld], addr [%ld] in sector[%ld]\n", idx, addr, sector); cmd.cmd = PREPARE_SECTOR_FOR_WRITE; cmd.param[0] = cmd.param[1] = sector; iap(&cmd, &res); if (res.status != CMD_SUCCESS) goto flash_error; if (fls->blk.priv.flags & KB_WRITE_ONCE) { if (bitarray_test(&lpc2_bitx, idx)) { ASSERT(0); goto flash_error; } else bitarray_set(&lpc2_bitx, idx); } cmd.cmd = COPY_RAM_TO_FLASH; cmd.param[0] = addr; cmd.param[1] = (uint32_t)buf; cmd.param[2] = FLASH_PAGE_SIZE_BYTES; cmd.param[3] = CPU_FREQ / 1000; iap(&cmd, &res); if (res.status != CMD_SUCCESS) goto flash_error; IRQ_RESTORE(flags); LOG_INFO("Done\n"); return blk->blk_size; flash_error: LOG_ERR("%ld\n", res.status); fls->hw->status |= FLASH_WR_ERR; return 0; }
void bit_on (bitset matrix, U8 bit) { bitarray_set (matrix, bit); }