Beispiel #1
0
static void check_str_wrapper(void)
{
	int ret;
	hi_handle_t *hi_handle;
	char key[32];
	uint16_t i;
	const char *data = "data element";
	void *data_ptr;

	fputs(" o check string wrapper functions tests ...", stdout);

	ret = hi_init_str(&hi_handle, 23);
	assert(ret == 0);

	for (i = 0; i < 23; i++)
	{
		sprintf(key, "%u", i);

		ret = hi_insert_str(hi_handle, key, data);
		assert(ret == 0);

		ret = hi_get_str(hi_handle, key, &data_ptr);
		assert(ret == 0);
	}

	ret = hi_fini(hi_handle);
	assert(ret == 0);

	puts(" passed");
}
Beispiel #2
0
static void check_hi_load_factor(void)
{
	int ret;
	hi_handle_t *hi_handle;
	double load_factor;

	fputs(" o check_hi_load_factor test ...", stdout);

	/* initialize hashish handle */
	hi_init_str(&hi_handle, 3);

	ret = hi_insert_str(hi_handle, "1", NULL);
	ret = hi_insert_str(hi_handle, "2", NULL);

	load_factor = hi_table_load_factor(hi_handle);

	assert(load_factor == ((double)3)/2);

	/* free the hashish handle */
	hi_fini(hi_handle);

	fputs("passed\n", stdout);
}
int reexport (ipfixt_node_t *t, ipfix_datarecord_t *d) {
  ipfix_template_t* outtemplate;

  if (hi_get_str(templates, t->ident, (void**)&outtemplate )) {
    if ( ipfix_new_data_template( ipfixh, &outtemplate, t->ipfixt->nfields ) <0 ) {
        fprintf( stderr, "ipfix_new_template() failed in reexport: %s\n", 
                 strerror(errno) );
        exit(1);
    }
    int i;
    fprintf( stdout, "reexporting template %s as id %i.\n", t->ident, outtemplate->tid );
    for (i=0; i < t->ipfixt->nfields; i++) {
      if (ipfix_add_field( ipfixh, outtemplate, 
                           t->ipfixt->fields[i].elem->ft->eno, t->ipfixt->fields[i].elem->ft->ftype, t->ipfixt->fields[i].flength )
           <0 ) {
        fprintf( stderr, "ipfix_add_field() failed in reexport: %s - eno = %i, type = %i, length = %i\n", 
                 strerror(errno), t->ipfixt->fields[i].elem->ft->eno, t->ipfixt->fields[i].elem->ft->ftype, t->ipfixt->fields[i].flength);
        exit(1);
      }
      //fprintf( stdout, "  added field %i:%i(%i||%i,%i)\n", outtemplate->fields[i].elem->ft->eno, outtemplate->fields[i].elem->ft->ftype, outtemplate->fields[i].elem->ft->length, t->ipfixt->fields[i].flength, t->ipfixt->fields[i].elem->ft->length );
    }
    hi_insert_str(templates, t->ident, outtemplate);
 }

  if (verbose_level>1) {
    int i; 
    char tmps[256];
    fprintf( stdout, "data record, tid = %i.\n", outtemplate->tid);
    for (i=0; i < outtemplate->nfields; i++) {
       t->ipfixt->fields[i].elem->snprint(tmps, 255, d->addrs[i], d->lens[i]);
       fprintf( stdout, "%i: %s\n", i, tmps);
    }
  }

  ipfix_export_array(ipfixh, outtemplate, outtemplate->nfields, d->addrs, d->lens);

  /* TODO FIXME do a flush only every now and then instead of each time */
  ipfix_export_flush(ipfixh);

  return 0;
}
Beispiel #4
0
static void check_preambel_test(void)
{
  int ret;
  hi_handle_t *hi_handle;
  const char *key = "23";
  const char *data = "data element";
  void *data_ptr;

  /* initialize hashish handle */
  hi_init_str(&hi_handle, 23);

  /* insert an key/data pair */
  ret = hi_insert_str(hi_handle, key, data);

  /* search for a pair with a string key and store result */
  hi_get_str(hi_handle, key, &data_ptr);

  /* free the hashish handle */
  hi_fini(hi_handle);

}
PBXRESULT PbniHashStr::Add( PBCallInfo * ci )
{
	PBXRESULT	pbxr = PBX_OK;
	int hi_res;

	// check arguments
	if ( ci->pArgs->GetAt(0)->IsNull() ||
		ci->pArgs->GetAt(1)->IsNull() )
	{
		// if any of the passed arguments is null, return the null value
		ci->returnValue->SetToNull();
	}
	else
	{
		pbstring key = ci->pArgs->GetAt(0)->GetString();
		//pbstring data = ci->pArgs->GetAt(1)->GetString();
		LPCTSTR tszKey = m_pSession->GetString(key);
		//LPCTSTR tszData = m_pSession->GetString(data);

		//convert the key into ansi
		int iKeyLen = wcstombs(NULL, (LPWSTR)tszKey, 0) + 1;
		LPSTR ansiKey = (LPSTR)malloc(iKeyLen);				//ne pas oublier le free à la fermeture
		wcstombs(ansiKey, (LPWSTR)(LPWSTR)tszKey, iKeyLen);
		ReleaseSessionString(tszKey);

		////alloc a copy for the string 
		//int iDataLen = (wcslen(tszData) + 1) * sizeof(WCHAR);
		//void * memdata = malloc(iDataLen);				//ne pas oublier le free à la fermeture
		//memcpy(memdata, tszData, iDataLen);
		IPB_Value* data = m_pSession->AcquireValue(ci->pArgs->GetAt(1));

		if (HI_ERR_SUCCESS == hi_insert_str(m_hi_handle, ansiKey, data /*tszData*/))
			ci->returnValue->SetBool(true);
		else
			ci->returnValue->SetBool(false);
	}

	return pbxr;
}
int main(int argc, char **argv)
{

	int ret, verbose = 0; unsigned int i;
	char *affix = NULL;
	char *prefix = NULL;
	int opt = 0;
	int len = 0;
	int fd1, fd2;
	char *png_filename = NULL;
	unsigned int entries = DEFAULT_ENTRIES;
	unsigned int table_size = DEFAULT_HASHTABLE_SIZE;
	struct drand48_data r_d;
	hi_handle_t *hi_handle;
	uint32_t (*hashf)(const uint8_t *, uint32_t);
	const char *hashfname;

	hashf = NULL;

	srand48_r((long int)time(NULL), &r_d);

	while ((opt = getopt(argc, argv, "hqn:l:p:t:a:g:f:v")) != -1) {
		switch (opt) {
		case 'q':
			{
			int max_fd, std_fd, j;
			if ((max_fd = (int) sysconf(_SC_OPEN_MAX)) < 0) {
				max_fd = 256;
			}
			for (j = max_fd - 1; j >= 0; --j) {
				close(j);
			}
			std_fd = open("/dev/null", O_RDWR);
			fd1 = dup(std_fd);
			fd2 = dup(std_fd);
			}
			break;
		case 'f':
			if (!(strcasecmp(optarg, "elf"))) {
				hashf = lhi_hash_elf;
				hashfname = "lhi_hash_elf";
			} else if (!(strcasecmp(optarg, "torek"))) {
				hashf = lhi_hash_torek;
				hashfname = "lhi_hash_torek";
			} else if (!(strcasecmp(optarg, "dumb1"))) {
				hashf = lhi_hash_dumb1;
				hashfname = "lhi_hash_dumb1";
			} else if (!(strcasecmp(optarg, "phong"))) {
				hashf = lhi_hash_phong;
				hashfname = "lhi_hash_phong";
			} else {
				fprintf(stderr, "Hashing function not supported: %s\n",
						optarg);
				usage(EXIT_FAILURE, argv[0]);
			}
			break;
		case 'h':
			usage(EXIT_SUCCESS, argv[0]);
			break;
		case 'n':
			entries = atoi(optarg);
			break;
		case 'l':
			len = atoi(optarg);
			break;
		case 'p':
			prefix = strdup(optarg);
			break;
		case 't':
			table_size = atoi(optarg);
			break;
		case 'v':
			verbose++;
			break;
		case 'a':
			affix = strdup(optarg);
			break;
		case 'g':
#ifdef HAVE_LIBGD
			png_filename = strdup(optarg);
# else
			fprintf(stderr, "sorry - you build without gd library support\n");
			usage(EXIT_FAILURE, argv[0]);
#endif
			break;
		case '?':
			fprintf(stderr, "No such option: `%c'\n\n", optopt);
			usage(EXIT_FAILURE, argv[0]);
			break;
		}
	}

	fputs("# String Distribution Hash Test\n", stderr);

	/* initialize secure[tm] rand() seed */
	//init_seed();

	/* initialize hash table */
	ret = hi_init_str(&hi_handle, table_size);

	if (hashf != NULL) {
		lhi_sethashfunc(hi_handle, hashf);
		fprintf(stderr, "# take %s as hash function\n", hashfname);
	}

	/* fill hash table */
	for(i = 0; i < entries; i++) {

		char *key = NULL, *tmp_key;
		size_t key_len = 0;

		/* compound key */
		if (len == 0) {
			len = (rand() % (MAX_STRING_LEN - MIN_STRING_LEN + 1)) + MIN_STRING_LEN;
		}
		if (random_string(len, &tmp_key, &r_d) < 0)
			exit(EXIT_FAILURE);

		if (prefix)
			key_len += strlen(prefix);
		key_len += strlen(tmp_key);
		if (affix)
			key_len += strlen(affix);


		key = malloc(key_len + 1);
		if (key == NULL) {
			fprintf(stderr, "malloc %s\n", strerror(errno));
			exit(1);
		}

		if (prefix != NULL) {
			sprintf(&key[0], "%s%s", prefix, tmp_key);
		} else {
			sprintf(key, "%s", tmp_key);
		}

		free(tmp_key);
		tmp_key = NULL;

		if (affix)
			strcat(key, affix);

		if (verbose >= 1)
			fprintf(stdout, "key: %s\n", key);

		ret = hi_insert_str(hi_handle, (void *) key, NULL);
		if (ret < 0)
			fprintf(stderr, "# WARNING: Can't insert key (maybe a duplicated key: %s)!\n", key);
	}

	/* print statistic */
	ret = hi_size(hi_handle);
	fprintf(stderr, "# hash table entries: %d\n", ret);

	if (png_filename) { /* grapical output */

#ifdef HAVE_LIBGD
# define RECT_SIZE 5
# define RECT_BODER_SIZE 1

		int j;
		uint32_t x, y;
		gdImagePtr im;
		FILE *pngout;
		int black, white, red, max_list_len = 0;


		/* calculate maximum listsize */
		for(i = 0; i < table_size; i++) {
			int tmp_bucket_size = hi_bucket_size(hi_handle, i);
			max_list_len = max(max_list_len, tmp_bucket_size);
		}


		/* create a image with max_list_len X table_size */
		im = gdImageCreate((max_list_len * (RECT_SIZE + RECT_BODER_SIZE * 2)) + 2,
				(table_size * ((RECT_SIZE + RECT_BODER_SIZE * 2)) + 2));

		black = gdImageColorAllocate(im, 255, 231, 186);
		white = gdImageColorAllocate(im, 255, 165, 79);
		red   = gdImageColorAllocate(im, 205, 102, 29);

		x = 1;
		y = 1;

		for (i = 0; i < table_size; i++) {
			int bucket_size =  hi_bucket_size(hi_handle, i);
			for (j = 0; j < bucket_size; j++) {
				gdImageFilledRectangle(im, x + 1, y + 1, x + RECT_SIZE, y + RECT_SIZE, white);
				gdImageRectangle(im, x, y, x + RECT_SIZE + (RECT_BODER_SIZE << 1), y + RECT_SIZE + (RECT_BODER_SIZE << 1), red);
				x += RECT_SIZE + (RECT_BODER_SIZE << 1);
			}
			x = 1;
			y += RECT_SIZE + (RECT_BODER_SIZE << 1);
		}

		pngout = fopen(png_filename, "wb");
		gdImagePng(im, pngout);
		fclose(pngout);
		gdImageDestroy(im);

# undef RECT_SIZE
# undef RECT_BODER_SIZE
#endif /* HAVE_LIBGD */

	}

	if (verbose >= 1) {  /* terminal output */
		for(i = 0; i < table_size; i++) {
			fprintf(stderr, "bucket no: %d bucket size: %d\n",
					i, hi_bucket_size(hi_handle, i));
		}

	}

	/* delete table */
	hi_fini(hi_handle);

	return 0;
}