Added ndpi_print_bin() API call

This commit is contained in:
Luca Deri
2020-06-29 22:36:46 +02:00
parent 0d2d44f1b6
commit 392ce44573
3 changed files with 55 additions and 4 deletions
+9 -4
View File
@@ -3114,7 +3114,8 @@ static void binUnitTest() {
struct ndpi_bin b1, b2;
u_int8_t num_bins = 32;
u_int32_t i;
char out_buf[128];
srand(time(NULL));
ndpi_init_bin(&b1, ndpi_bin_family8, num_bins), ndpi_init_bin(&b2, ndpi_bin_family8, num_bins);
@@ -3122,9 +3123,13 @@ static void binUnitTest() {
for(i=0; i<32; i++)
ndpi_inc_bin(&b1, rand() % num_bins), ndpi_inc_bin(&b2, rand() % num_bins);
ndpi_bin_similarity(&b1, &b1, 0);
ndpi_bin_similarity(&b1, &b2, 0);
#if 0
printf("1) %s\n", ndpi_print_bin(&b1, 0, out_buf, sizeof(out_buf)));
printf("2) %s\n", ndpi_print_bin(&b2, 0, out_buf, sizeof(out_buf)));
printf("Similarity: %f\n\n", ndpi_bin_similarity(&b1, &b2, 1));
#endif
ndpi_free_bin(&b1), ndpi_free_bin(&b2);
}
+3
View File
@@ -1071,7 +1071,10 @@ extern "C" {
void ndpi_free_bin(struct ndpi_bin *b);
void ndpi_inc_bin(struct ndpi_bin *b, u_int8_t slot_id);
void ndpi_normalize_bin(struct ndpi_bin *b);
char* ndpi_print_bin(struct ndpi_bin *b, u_int8_t normalize_first, char *out_buf, u_int out_buf_len);
float ndpi_bin_similarity(struct ndpi_bin *b1, struct ndpi_bin *b2, u_int8_t normalize_first);
#ifdef __cplusplus
}
#endif
+43
View File
@@ -324,6 +324,49 @@ void ndpi_normalize_bin(struct ndpi_bin *b) {
/* ********************************************************************************* */
char* ndpi_print_bin(struct ndpi_bin *b, u_int8_t normalize_first, char *out_buf, u_int out_buf_len) {
u_int8_t i;
u_int len = 0;
if(!out_buf) return(out_buf); else out_buf[0] = '\0';
if(normalize_first)
ndpi_normalize_bin(b);
switch(b->family) {
case ndpi_bin_family8:
for(i=0; i<b->num_bins; i++) {
int rc = snprintf(&out_buf[len], out_buf_len-len, "%s%u", (i > 0) ? "," : "", b->u.bins8[i]);
if(rc < 0) break;
len += rc;
}
break;
case ndpi_bin_family16:
for(i=0; i<b->num_bins; i++) {
int rc = snprintf(&out_buf[len], out_buf_len-len, "%s%u", (i > 0) ? "," : "", b->u.bins16[i]);
if(rc < 0) break;
len += rc;
}
break;
case ndpi_bin_family32:
for(i=0; i<b->num_bins; i++) {
int rc = snprintf(&out_buf[len], out_buf_len-len, "%s%u", (i > 0) ? "," : "", b->u.bins32[i]);
if(rc < 0) break;
len += rc;
}
break;
}
return(out_buf);
}
/* ********************************************************************************* */
/*
Determines how similar are two bins