u_int lst_string_items_common(LST_String *s1, u_int off1, LST_String *s2, u_int off2, u_int max_len) { if (!s1 || !s2 || off1 >= s1->num_items || off2 >= s2->num_items) return 0; const u_int len = MIN(s1->num_items-off1-1, MIN(s2->num_items-off2-1, max_len)); u_int item1= off1; u_int item2= off2; for (u_int i = 0; i < len; ++i, ++item1, ++item2) { if (!(*((char*)lst_string_get_item(s1, item1)) == *((char*)lst_string_get_item(s2, item2)))) { return i; } } if (len==max_len) return len; else { if ((s1==s2) && (s1->num_items-1==item1) && (s2->num_items-1==item2)) return len+1; else return len; } }
int lst_string_eq(LST_String *s1, u_int item1, LST_String *s2, u_int item2) { if (!s1 || !s2 || item1 >= s1->num_items || item2 >= s2->num_items) return 0; /* Treat the end-of-string markers separately: */ if (item1 == s1->num_items - 1 || item2 == s2->num_items - 1) { if (item1 == s1->num_items - 1 && item2 == s2->num_items - 1) { if (s1 == s2) { D(("Comparing end of identical strings\n")); return 1; } else { D(("Comparing end of different strings\n")); return 0; } } else { D(("Comparing end and non-end\n")); return 0; } } return !(s1->sclass->cmp_func(lst_string_get_item(s1, item1), lst_string_get_item(s2, item2))); }
void lst_string_item_copy(LST_String *src, u_int src_index, LST_String *dst, u_int dst_index) { void *src_item, *dst_item; if (!src || !dst || src_index >= src->num_items || dst_index >= dst->num_items) return; src_item = lst_string_get_item(src, src_index); dst_item = lst_string_get_item(dst, dst_index); src->sclass->copy_func(src_item, dst_item); }
void byte_distribution_add(byte_distribution_t *bd, LST_String *nbytes, int offset){ if(!bd || !nbytes) { return; } if(offset < 0 || offset >= lst_string_get_length(nbytes)) { return; } char *value = (char *) lst_string_get_item(nbytes, offset); HashTableValue frequency; char * key = (char *) malloc(sizeof(char)); *key = value[0]; if((frequency = hash_table_lookup(bd->value_frequency, key)) != HASH_TABLE_NULL){ (*(int *)frequency)++; } else { int *freq = (int *) malloc (sizeof(int)); *freq = 1; hash_table_insert(bd->value_frequency, key, freq); } return; }
byte_distribution_t * byte_distribution_new(LST_String *nbytes, int offset) { char * item = (char *)lst_string_get_item(nbytes, offset); byte_distribution_t * bd = (byte_distribution_t *) malloc (sizeof(byte_distribution_t)); bd->value_frequency = hash_table_new(int_hash, int_equal); char * value = (char *) malloc (sizeof(char)); *value = item[0]; int * frequency = (int *) malloc (sizeof(int)); *frequency = 1; hash_table_insert(bd->value_frequency, value, frequency); return bd; }