Пример #1
0
unsigned int h_atoh(hfloat* ha, char* buf, int radix)
{
	const char ABC[]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	char *pc,c;
	bool havepoint=false;
	int i;
	hfloat r,he,hrad;
	h_init(&r);h_init(&he);h_init(&hrad);
	h_copy_d(&r, 0.0);
	h_copy_d(&he, 1.0);
	h_copy_d(&hrad, radix);

	pc=buf;
	if(*pc=='-') *pc++;
	for(;*pc!=0; pc++)
	{
		if(*pc=='.') { havepoint=true; continue; }
		if(havepoint) h_mul(&he, &hrad, &he);
		h_mul(&r, &hrad, &r);
		c=toupper(*pc);
		for(i=0; (ABC[i]!=c) && (ABC[i]!=0); i++);
		if(ABC[i]==0) return -1;
		h_add_d(&r, (double)i, &r);
	}
	if(havepoint) h_div(&r, &he, &r);
	if(buf[0]=='-') r.s=1;
	h_copy(ha, &r);
	h_clear(&r); h_clear(&he); h_clear(&hrad);
	return 0;
}
Пример #2
0
int main(int argc, char **argv) {
    heap *h = h_init(NULL);

    int i = 16;
    for (i; i > 0; i--) {
        int *ip = malloc(sizeof(int));
        *ip = i;
        h_push(h, ip);
    }

    i = 1;
    int *ip;
    while ((ip = h_pop(h)) != null) {
        printf("%d: %d @%x\n", i, *ip, ip);
        i++;
    }

    return 0;
}
Пример #3
0
threadpool *tp_init(size_t num_ts) {
    threadpool *pool;
    if ((pool = (threadpool *) malloc(sizeof(threadpool))) == null) {
        goto err;
    }

    // init the pool
    pool->num_ts = 0;
    pool->shutdown = false;
    pool->started = 0;
    pool->tasks = h_init(32, tp_taskcomp);

    pool->ts = malloc(sizeof(pthread_t) * num_ts);

    // setup mutex and conditional notification
    if (pthread_mutex_init(&pool->lock, null) != 0 ||
            pthread_cond_init(&pool->notify, null) != 0 ||
            pool->ts == null) {
        goto err;
    }

    // spin up worker threads
    size_t i;
    for (i = 0; i < num_ts; i++) {
        if (pthread_create(&pool->ts[i], null, tp_thread, (void *) pool) != 0) {
            tp_dest(pool, 0);
            return null;
        }
        pool->num_ts++;
        pool->started++;
    }

    return pool;

    err:
    // initialization has failed somewhere, cleanup and return null
    if (pool) {
        tp_free(pool);
    }
    return null;
}
Пример #4
0
int main(int argc, char **argv) {
    FILE *fd = fopen(argv[1], "r");
    heap *h = h_init(NULL);
    fraction *sum = malloc(f_size);
    sum->n = 0;
    sum->d = 1;

    bool cont = true;
    while (cont) {
        fraction *f = malloc(f_size);
        f->n = 0;
        f->d = 1;
        if (f_read(f, fd)) {
            if (f->d != 0) {
                f_add(sum, f);
                h_push(h, f);
            }
            else {
                cont = false;
            }
        }
        else {
            return -1;
        }
    }

    printf("The sum of the fractions is: ");
    f_print(sum, stdout);

    fraction *f;
    while ((f = h_pop(h)) != null) {
        f_print(f, stdout);
    }

    return 0;
}
static struct dm_cache_policy *smq_create(dm_cblock_t cache_size,
					  sector_t origin_size,
					  sector_t cache_block_size)
{
	unsigned i;
	unsigned nr_sentinels_per_queue = 2u * NR_CACHE_LEVELS;
	unsigned total_sentinels = 2u * nr_sentinels_per_queue;
	struct smq_policy *mq = kzalloc(sizeof(*mq), GFP_KERNEL);

	if (!mq)
		return NULL;

	init_policy_functions(mq);
	mq->cache_size = cache_size;
	mq->cache_block_size = cache_block_size;

	calc_hotspot_params(origin_size, cache_block_size, from_cblock(cache_size),
			    &mq->hotspot_block_size, &mq->nr_hotspot_blocks);

	mq->cache_blocks_per_hotspot_block = div64_u64(mq->hotspot_block_size, mq->cache_block_size);
	mq->hotspot_level_jump = 1u;
	if (space_init(&mq->es, total_sentinels + mq->nr_hotspot_blocks + from_cblock(cache_size))) {
		DMERR("couldn't initialize entry space");
		goto bad_pool_init;
	}

	init_allocator(&mq->writeback_sentinel_alloc, &mq->es, 0, nr_sentinels_per_queue);
        for (i = 0; i < nr_sentinels_per_queue; i++)
		get_entry(&mq->writeback_sentinel_alloc, i)->sentinel = true;

	init_allocator(&mq->demote_sentinel_alloc, &mq->es, nr_sentinels_per_queue, total_sentinels);
        for (i = 0; i < nr_sentinels_per_queue; i++)
		get_entry(&mq->demote_sentinel_alloc, i)->sentinel = true;

	init_allocator(&mq->hotspot_alloc, &mq->es, total_sentinels,
		       total_sentinels + mq->nr_hotspot_blocks);

	init_allocator(&mq->cache_alloc, &mq->es,
		       total_sentinels + mq->nr_hotspot_blocks,
		       total_sentinels + mq->nr_hotspot_blocks + from_cblock(cache_size));

	mq->hotspot_hit_bits = alloc_bitset(mq->nr_hotspot_blocks);
	if (!mq->hotspot_hit_bits) {
		DMERR("couldn't allocate hotspot hit bitset");
		goto bad_hotspot_hit_bits;
	}
	clear_bitset(mq->hotspot_hit_bits, mq->nr_hotspot_blocks);

	if (from_cblock(cache_size)) {
		mq->cache_hit_bits = alloc_bitset(from_cblock(cache_size));
		if (!mq->cache_hit_bits) {
			DMERR("couldn't allocate cache hit bitset");
			goto bad_cache_hit_bits;
		}
		clear_bitset(mq->cache_hit_bits, from_cblock(mq->cache_size));
	} else
		mq->cache_hit_bits = NULL;

	mq->tick = 0;
	spin_lock_init(&mq->lock);

	q_init(&mq->hotspot, &mq->es, NR_HOTSPOT_LEVELS);
	mq->hotspot.nr_top_levels = 8;
	mq->hotspot.nr_in_top_levels = min(mq->nr_hotspot_blocks / NR_HOTSPOT_LEVELS,
					   from_cblock(mq->cache_size) / mq->cache_blocks_per_hotspot_block);

	q_init(&mq->clean, &mq->es, NR_CACHE_LEVELS);
	q_init(&mq->dirty, &mq->es, NR_CACHE_LEVELS);

	stats_init(&mq->hotspot_stats, NR_HOTSPOT_LEVELS);
	stats_init(&mq->cache_stats, NR_CACHE_LEVELS);

	if (h_init(&mq->table, &mq->es, from_cblock(cache_size)))
		goto bad_alloc_table;

	if (h_init(&mq->hotspot_table, &mq->es, mq->nr_hotspot_blocks))
		goto bad_alloc_hotspot_table;

	sentinels_init(mq);
	mq->write_promote_level = mq->read_promote_level = NR_HOTSPOT_LEVELS;

	mq->next_hotspot_period = jiffies;
	mq->next_cache_period = jiffies;

	return &mq->policy;

bad_alloc_hotspot_table:
	h_exit(&mq->table);
bad_alloc_table:
	free_bitset(mq->cache_hit_bits);
bad_cache_hit_bits:
	free_bitset(mq->hotspot_hit_bits);
bad_hotspot_hit_bits:
	space_exit(&mq->es);
bad_pool_init:
	kfree(mq);

	return NULL;
}
// ---------------------------------------------------------------------------
// Constructeur
// ------------
bv310GridAIASCIIBaseAccessor	::bv310GridAIASCIIBaseAccessor(	const char* hpath,  
																int wanted_srid,
																double wanted_reso,
																int* status)
								:bv310BaseAccessor(hpath,wanted_srid,wanted_reso,status){
_bTrace_("bv310GridAIASCIIBaseAccessor::bv310GridAIASCIIBaseAccessor",true);
char		fpath[PATH_MAX];
char		name[FILENAME_MAX];

	_nc=_nl=_ox=_oy=_csz=_ndv=0;
	_arr=NULL;
	_dvs.sign=_2D_VX;
	_dvs.nv=1;
	_dvs.no=0;
	_dvs.offs=NULL;
	_dvs.vx.vx2[0].x=_dvs.vx.vx2[0].y=0;

	for(;;){
		load_data(hpath);
		
		sprintf(fpath,"%sContents/Datas/",hpath);
CFStringRef	cfs=CFStringCreateWithCString(kCFAllocatorDefault,fpath,kCFStringEncodingMacRoman);
		CFStringGetCString(cfs,fpath,PATH_MAX,kCFStringEncodingUTF8);
		CFRelease(cfs);
		
		sprintf(name,"header");
		*status=noErr;
		_hdr=(bStdTable*)wtbl_alloc(kTableLocalDBNoFU,fpath,name,false,_reso,-1,status);
		if(*status<0){
_te_("_hdr=(bStdTable*)wtbl_alloc failed with "+(*status));
			break;
		}
		
		if((*status=h_init())){
			break;
		}
// on ferme et on ré-ouvre pour reso et srid
		delete _hdr;
		_hdr=(bStdTable*)wtbl_alloc(kTableLocalDBNoFU,fpath,name,false,_reso,-1,status);
		if(*status){
_te_("_hdr=(bStdTable*)wtbl_alloc failed with "+(*status));
			break;
		}
		
		/*sprintf(name,"links");
		*status=_fd;
		_lnks=(bStdTable*)wtbl_alloc(kTableLocalDBNoFU,fpath,name,true,_reso,-1,status);// true pour upgrade 3.0->3.1
		if(*status<0){
_te_("_lnks=(bStdTable*)wtbl_alloc failed with "+(*status));
			break;
		}
		wtbl_free(_lnks);
		_lnks=NULL;
		
		sprintf(name,"fields");
		*status=_fd;
		_fld=(bStdTable*)wtbl_alloc(kTableLocalDBNoFU,fpath,name,false,_reso,-1,status);
		if(*status<0){
_te_("_fld=(bStdTable*)wtbl_alloc failed with "+(*status));
			break;
		}
		sprintf(name,"constraints");
		*status=_fd;
		_cnst=(bStdTable*)wtbl_alloc(kTableLocalDBNoFU,fpath,name,false,_reso,-1,status);
		if(*status<0){
_te_("_cnst=(bStdTable*)wtbl_alloc failed with "+(*status));
			break;
		}
		sprintf(name,"objects");
		*status=noErr;
		_objs=(bStdTable*)wtbl_alloc(kTableLocalDBFU,fpath,name,false,_reso,_srid,status);
		if(*status<0){
_te_("_objs=(bStdTable*)wtbl_alloc failed with "+(*status));
			break;
		}
		
		if(_fld->CountRecords()<kOBJ_Dir_){
_te_("_fld->CountRecords()<kOBJ_Dir_");
			*status=-1;
			break;
		}*/
		if((*status=load(wanted_srid))){
_te_("(*status=load()) failed with "+(*status));
			break;
		}
		return;
	}
_te_("status == "+(*status));
	_elts.reset();
	_celts.reset();
	if(_fld){	
		wtbl_free(_fld);
		_fld=NULL;
	}
	if(_objs){	
		wtbl_free(_objs);
		_objs=NULL;
	}
	if(_hdr){	
		wtbl_free(_hdr);
		_hdr=NULL;
	}
	if(_cnst){	
		wtbl_free(_cnst);
		_cnst=NULL;
	}
	if(_lnks){	
		wtbl_free(_lnks);
		_lnks=NULL;
	}
	if(_arr){
		delete _arr;
		_arr=NULL;
	}
}
Пример #7
0
int main() {

	unsigned char ordinal[4] = { 0x00, 0x00, 0x00, 0x17 };

	HMAC_CTX hmac;

	unsigned char shared_secret[20] = { 0x42, 0xAC ,0xAF, 0xF1, 0xD4 ,0x99, 0x3C, 0xCA, 0xC9, 0x00, 0x3C, 0xCA, 0xC8, 0x00, 0x3C, 0xCA, 0xC8, 0x00, 0x3C, 0xCA };
	unsigned char hashDigest[20] = { 0x6F, 0x02, 0x98, 0x86, 0x25, 0x8C, 0xAF, 0x9F, 0xC2, 0x4A, 0x70, 0x6B, 0xBD, 0x44, 0xBC, 0x5E, 0x57, 0xD8, 0x32, 0xA1 };
	unsigned char even[20] = { 0x76, 0xF4, 0x26, 0x85, 0xF4, 0x8E, 0x33, 0x3B, 0x9B, 0x8B, 0xBA, 0xCF, 0x8D, 0x12, 0x42, 0x39, 0x7F, 0x8A, 0xC3, 0x23 };
	unsigned char odd[20] = { 0xFE, 0x26, 0x68, 0x4C, 0x27, 0xB6, 0x50, 0x2A, 0xEC, 0x90, 0x85, 0xAA, 0xD9, 0x80, 0x38, 0x13, 0x9C, 0xD6, 0xE5, 0xBF };
	//unsigned char h[20] = { 0x6B, 0xB0, 0x85, 0x4C, 0xA0, 0x9C, 0xAF, 0x9C, 0x3C, 0xCC, 0xA5, 0x57, 0x30, 0x85, 0xB9, 0x5F, 0x7B, 0x85, 0xE9, 0xCB };
	unsigned char new_h[20] = { 0x00 };
	unsigned char new_h2[20] = { 0x00 };
	unsigned char xor_key[20] = { 0x00 };
	unsigned char encrypted_secret[20] = { 0x00 };
	unsigned char secret_key[20] = { 0x00 };
	unsigned char shared[20] = { 0x00 };
	unsigned char cont = 0x00;

	unsigned char osapEven[20] = { 0x03 ,0xF0 ,0x02 ,0xB6, 0xA9 ,0x2C ,0x48 ,0xAE, 0x3E ,0x0E ,0xEA ,0xA1, 0x47 ,0x5C ,0x3D ,0x21, 0xE8 ,0x06 ,0x38 ,0xD6 };

	unsigned char osapOdd[20]  = { 0x67, 0x04, 0x00, 0x4E, 0x36, 0x0C, 0x6E, 0x4A, 0xCB, 0xDB, 0xBB, 0xE6, 0xDD, 0xE2, 0xF1, 0x46, 0x2C, 0xF0, 0x77, 0x01 };

	hmac_init(secret_key, 20);
	hmac_update(osapEven, 20);
	hmac_update(osapOdd, 20);
	hmac_final(shared);

	int i;
	printf("ENC AUTH:\n");
	for(i=0;i<20;i++)
		printf("%02X ", shared[i]);

	printf("\n");

	unsigned char pcrInfoSize[4] = { 0x00, 0x00, 0x00, 0x2C };

	unsigned char pcrInfo[44] = { 0x00 };

	unsigned char data[20] = { 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x57, 0x6f, 0x72, 0x6c, 0x64,
							  0x21, 0x54, 0x68, 0x69, 0x73, 0x49, 0x73, 0x4d, 0x65, 0x0A };

	unsigned char data_len[4] = { 0x00, 0x00, 0x00, 0x14 };

	pcrInfo[1] = 0x02;
	pcrInfo[2] = 0x00;

	unsigned int hmac_len = 20;

	hash_init();
	hash_update(even, 20);
	hash_update(shared_secret, 20);
	hash_final(xor_key);

	for(i=0;i<20;i++)
		encrypted_secret[i] = xor_key[i] ^ secret_key[i];

	printf("ENC AUTH:\n");
	for(i=0;i<20;i++)
		printf("%02X ", encrypted_secret[i]);

	printf("\n");

	hash_init();
	hash_update(ordinal, 4);
	hash_update(encrypted_secret, 20);
	hash_update(pcrInfoSize, 4);
	hash_update(pcrInfo, 44);
	hash_update(data_len, 4);
	hash_update(data, 20);
	hash_final(hashDigest);

	printf("HASH DIGEST:\n");
	for(i=0;i<20;i++)
		printf("%02X ", hashDigest[i]);

	printf("\n");

	HMAC_CTX_init(&hmac);
	HMAC_Init(&hmac, shared_secret, 20, EVP_sha1());
	HMAC_Update(&hmac, hashDigest, 20);
	HMAC_Update(&hmac, even, 20);
	HMAC_Update(&hmac, odd, 20);
	HMAC_Update(&hmac, &cont, 1);
	HMAC_Final(&hmac, new_h, &hmac_len);

	printf("OPENSSL HMAC:\n");
	for(i=0;i<20;i++)
		printf("%02X ", new_h[i]);

	printf("\n");

	h_init(shared_secret, 20);
	h_update(hashDigest, 20);
	h_update(even, 20);
	h_update(odd, 20);
	h_update(&cont, 1);
	h_final(new_h2);

	printf("IAIK HMAC:\n");
	i=0;
	for(;i<20;i++)
		printf("%02X ", new_h2[i]);
	printf("\n");

	return 0;
}
// ---------------------------------------------------------------------------
// Constructeur
// ------------
bPGisBaseAccessor	::bPGisBaseAccessor(	const char* hpath,  
											int* status)
					:bv300BaseAccessor(hpath,status){
_bTrace_("bPGisBaseAccessor::bPGisBaseAccessor",false);
char		name[512];
char		_name[FILENAME_MAX];
char		path[SHRT_MAX];
char		data[1024];
	
	for(;;){		
		strcpy(path,hpath);
		make_data(data,path);
		
		_name[0]=0;
		read_name(path,_name);		
		
		sprintf(name,"%s_%s",_name,"header");
		_hdr=(bStdTable*)wtbl_alloc(kTablePostGIS,data,name,false,_reso,_srid,status);
		if(*status){
			break;
		}

		if((*status=h_init())){
			break;
		}

// on ferme et on ré-ouvre pour reso et srid
		delete _hdr;
		_hdr=(bStdTable*)wtbl_alloc(kTablePostGIS,data,name,false,_reso,_srid,status);
		if(*status){
			break;
		}
		
		sprintf(name,"%s_%s",_name,"fields");
		_fld=(bStdTable*)wtbl_alloc(kTablePostGIS,data,name,false,_reso,_srid,status);
		if(*status){
			break;
		}
		sprintf(name,"%s_%s",_name,"constraints");
		_cnst=(bStdTable*)wtbl_alloc(kTablePostGIS,data,name,false,_reso,_srid,status);
		if(*status){
			break;
		}
		sprintf(name,"%s_%s",_name,"objects");
		_objs=(bStdTable*)wtbl_alloc(kTablePostGIS,data,name,false,_reso,_srid,status);
		if(*status){
			break;
		}
		
		if(_fld->CountRecords()<kOBJ_Dir_){
			*status=-1;
			break;
		}
		if((*status=load())){
			break;
		}
		return;
	}
_err_(_strr_+"status == "+(*status));
	_elts.reset();
	_celts.reset();
	if(_fld){	
		wtbl_free(_fld);
		_fld=NULL;
	}
	if(_objs){	
		wtbl_free(_objs);
		_objs=NULL;
	}
	if(_hdr){	
		wtbl_free(_hdr);
		_hdr=NULL;
	}
	if(_cnst){	
		wtbl_free(_cnst);
		_cnst=NULL;
	}
}
Пример #9
0
void init() {
    h_init();
}
Пример #10
0
void h_htoa(hfloat* ha, char* buf, unsigned int ccmax, int radix)
{
	if(h_iszero(ha)) { buf[0]='0'; buf[1]='\0'; return; }
	const char abc[]="0123456789ABCDEF";
	hfloat t; h_init(&t); h_copy(&t, ha);

	int i,j,k;
	int exp = 0;
	bool negexp=false;
	__int8 digit=0;
	unsigned __int32 limb;
	short digitsize;
	char* pc=buf;

	memset(buf,0,ccmax);
	if(ha->s) { *pc='-'; pc++; }
	//if(t<1) { *pc='0'; pc++; }
	
	if(radix==16) digitsize=4;
	if(radix==8) digitsize=3;
	if(radix==2) digitsize=1;

	switch(radix)
	{
	case 16:
	case 8:
	case 2:
		exp = ha->e;
		if(exp<=0) 
		{
			negexp=true;
			*pc='0'; pc++; *pc='.'; pc++;
			exp=-exp;
			if(exp>=digitsize) for(i=0;i<exp;i+=digitsize) {*pc='0'; pc++; }
		}
		digit=0;
		if(negexp) k=exp%digitsize;
		else k=digitsize-exp%digitsize;
		if(k==digitsize) k=0;
		for(i=0;i<MSIZE;i++)
		{
			limb = ha->m[i];
			for(j=0;j<32;j++)
			{
				if(limb&0x80000000) digit|=1;
				k++;
				if(k>=digitsize)
				{
					k=0;
					*pc = abc[digit];
					*pc++;
					digit=0;
				}
				exp--; if((exp==0) && !negexp) { *pc='.'; pc++; }
				digit<<=1;
				limb<<=1;
			}
		}
		break;
	case 10:
		hfloat a,b; h_init(&a); h_init(&b);
		h_copy(&t, ha);
		if(t.s!=0) t.s=0;
		exp = 0;
		h_copy_d(&b, 0.1);
		if(t.e<=0) {
			strcpy(pc,"0."); pc+=2;
			while(h_cmp(&t,&b)>0) {
				h_mul_d(&t, 10.0, &t);
				*pc='0'; pc++;
			}
		} else { strcpy(pc, "0"); pc++; } // в начале - 0, для округления с переполнением (999.99999)
		while(h_cmp_d(&t, 1000000000.0)<=0) { h_div_d(&t, 1000000000.0, &t); exp+=9; }
		while(h_cmp_d(&t, 1000000.0)<=0) { h_div_d(&t, 1000000.0, &t); exp+=6; }
		while(h_cmp_d(&t, 1000.0)<=0) 
		{ 
			h_div_d(&t, 1000.0, &t); 
			exp+=3; 
		}
		while(h_cmp_d(&t, 1.0)<=0) { h_div_d(&t, 10.0, &t); exp++; }

		h_clear(&a); h_clear(&b);
		unsigned __int8 _e;
		for(i=0;i<MSIZE*32/4/5;i++)
		{
			h_mul_d(&t,100000.0, &t);
			if( (exp<=5)&&(exp>=0) ) _e=(unsigned __int8)exp; else _e=0;
			exp-=5;
			__asm {
				mov ebx, t.m
				mov eax, [ebx]
				mov ecx, 32
				sub ecx, t.e
getfrac:
				clc
				rcr eax, 1
				dec cl
				jnz getfrac

				mov esi, pc
				mov ch, _e
				or ch, 0x80
				call ascii4
				add esi, ebx
				mov pc, esi
			}
			h_clrat(&t);
		}
		
		// теперь buf содержит наше число в десятичном виде.
		// округляем его (23.01299999999 -> 23.013)
		
		pc = buf+strlen(buf)-1;
		if(*pc=='9') {
			k=1;
			do {
				if(!isdigit(*pc)) { pc--; continue; }
				*pc+=k; if(*pc>'9') { *pc='0'; k=1; } else k=0;
				pc--;
			} while(k==1);
		}
		
		break;
	}
	pc=buf+strlen(buf)-1;
	while( (*pc=='0')&&(pc!=buf) ) { *pc=0; pc--; }
	if(*pc=='.') *pc=0;
	if( (buf[0]=='0')&&(buf[1]!='.') ) memmove(buf, buf+1, strlen(buf));
	if( (buf[0]=='-')&&(buf[1]=='0')&&(buf[2]!='.') ) memmove(buf+1, buf+2, strlen(buf)-1);
	h_clear(&t);
}