瀏覽代碼

ncd: NCDStringIndex: add NCDStringIndex_LookupBin() and NCDStringIndex_GetBin()

ambrop7 13 年之前
父節點
當前提交
47127f8564
共有 3 個文件被更改,包括 37 次插入12 次删除
  1. 31 9
      ncd/NCDStringIndex.c
  2. 4 1
      ncd/NCDStringIndex.h
  3. 2 2
      ncd/NCDStringIndex_hash.h

+ 31 - 9
ncd/NCDStringIndex.c

@@ -48,14 +48,16 @@
 
 #include <generated/blog_channel_ncd.h>
 
-static NCD_string_id_t do_get (NCDStringIndex *o, const char *str)
+static NCD_string_id_t do_get (NCDStringIndex *o, const char *str, size_t str_len)
 {
     ASSERT(str)
+    ASSERT(!memchr(str, '\0', str_len))
     
-    NCDStringIndex__HashRef ref = NCDStringIndex__Hash_Lookup(&o->hash, o->entries, str);
+    NCDStringIndex_hash_key key = {str, str_len};
+    NCDStringIndex__HashRef ref = NCDStringIndex__Hash_Lookup(&o->hash, o->entries, key);
     ASSERT(ref.link == -1 || ref.link >= 0)
     ASSERT(ref.link == -1 || ref.link < o->entries_size)
-    ASSERT(ref.link == -1 || !strcmp(ref.ptr->str, str))
+    ASSERT(ref.link == -1 || (ref.ptr->str_len == str_len && !memcmp(ref.ptr->str, str, str_len)))
     
     if (ref.link != -1) {
         return ref.link;
@@ -70,10 +72,11 @@ static NCD_string_id_t do_get (NCDStringIndex *o, const char *str)
     
     struct NCDStringIndex__entry *entry = &o->entries[o->entries_size];
     
-    if (!(entry->str = b_strdup(str))) {
-        BLog(BLOG_ERROR, "b_strdup failed");
+    if (!(entry->str = b_strdup_bin(str, str_len))) {
+        BLog(BLOG_ERROR, "b_strdup_bin failed");
         return -1;
     }
+    entry->str_len = str_len;
     
     NCDStringIndex__HashRef newref = {entry, o->entries_size};
     int res = NCDStringIndex__Hash_Insert(&o->hash, o->entries, newref, NULL);
@@ -96,7 +99,7 @@ int NCDStringIndex_Init (NCDStringIndex *o)
         goto fail1;
     }
     
-    if (do_get(o, "") < 0) {
+    if (do_get(o, "", 0) < 0) {
         goto fail2;
     }
     
@@ -128,10 +131,20 @@ NCD_string_id_t NCDStringIndex_Lookup (NCDStringIndex *o, const char *str)
     DebugObject_Access(&o->d_obj);
     ASSERT(str)
     
-    NCDStringIndex__HashRef ref = NCDStringIndex__Hash_Lookup(&o->hash, o->entries, str);
+    return NCDStringIndex_LookupBin(o, str, strlen(str));
+}
+
+NCD_string_id_t NCDStringIndex_LookupBin (NCDStringIndex *o, const char *str, size_t str_len)
+{
+    DebugObject_Access(&o->d_obj);
+    ASSERT(str)
+    ASSERT(!memchr(str, '\0', str_len))
+    
+    NCDStringIndex_hash_key key = {str, str_len};
+    NCDStringIndex__HashRef ref = NCDStringIndex__Hash_Lookup(&o->hash, o->entries, key);
     ASSERT(ref.link == -1 || ref.link >= 0)
     ASSERT(ref.link == -1 || ref.link < o->entries_size)
-    ASSERT(ref.link == -1 || !strcmp(ref.ptr->str, str))
+    ASSERT(ref.link == -1 || (ref.ptr->str_len == str_len && !memcmp(ref.ptr->str, str, str_len)))
     
     return ref.link;
 }
@@ -141,7 +154,16 @@ NCD_string_id_t NCDStringIndex_Get (NCDStringIndex *o, const char *str)
     DebugObject_Access(&o->d_obj);
     ASSERT(str)
     
-    return do_get(o, str);
+    return NCDStringIndex_GetBin(o, str, strlen(str));
+}
+
+NCD_string_id_t NCDStringIndex_GetBin (NCDStringIndex *o, const char *str, size_t str_len)
+{
+    DebugObject_Access(&o->d_obj);
+    ASSERT(str)
+    ASSERT(!memchr(str, '\0', str_len))
+    
+    return do_get(o, str, str_len);
 }
 
 const char * NCDStringIndex_Value (NCDStringIndex *o, NCD_string_id_t id)

+ 4 - 1
ncd/NCDStringIndex.h

@@ -45,10 +45,11 @@ typedef int NCD_string_id_t;
 
 struct NCDStringIndex__entry {
     char *str;
+    size_t str_len;
     NCD_string_id_t hash_next;
 };
 
-typedef const char *NCDStringIndex_hash_key;
+typedef struct { const char *str; size_t len; } NCDStringIndex_hash_key;
 typedef struct NCDStringIndex__entry *NCDStringIndex_hash_arg;
 
 #include "NCDStringIndex_hash.h"
@@ -72,7 +73,9 @@ struct NCD_string_request {
 int NCDStringIndex_Init (NCDStringIndex *o) WARN_UNUSED;
 void NCDStringIndex_Free (NCDStringIndex *o);
 NCD_string_id_t NCDStringIndex_Lookup (NCDStringIndex *o, const char *str);
+NCD_string_id_t NCDStringIndex_LookupBin (NCDStringIndex *o, const char *str, size_t str_len);
 NCD_string_id_t NCDStringIndex_Get (NCDStringIndex *o, const char *str);
+NCD_string_id_t NCDStringIndex_GetBin (NCDStringIndex *o, const char *str, size_t str_len);
 const char * NCDStringIndex_Value (NCDStringIndex *o, NCD_string_id_t id);
 int NCDStringIndex_GetRequests (NCDStringIndex *o, struct NCD_string_request *requests) WARN_UNUSED;
 

+ 2 - 2
ncd/NCDStringIndex_hash.h

@@ -6,8 +6,8 @@
 #define CHASH_PARAM_NULL ((NCD_string_id_t)-1)
 #define CHASH_PARAM_DEREF(arg, link) (&(arg)[(link)])
 #define CHASH_PARAM_ENTRYHASH(arg, entry) badvpn_djb2_hash((const uint8_t *)(entry).ptr->str)
-#define CHASH_PARAM_KEYHASH(arg, key) badvpn_djb2_hash((const uint8_t *)(key))
+#define CHASH_PARAM_KEYHASH(arg, key) badvpn_djb2_hash_bin((const uint8_t *)(key).str, (key).len)
 #define CHASH_PARAM_ENTRYHASH_IS_CHEAP 0
 #define CHASH_PARAM_COMPARE_ENTRIES(arg, entry1, entry2) (!strcmp((entry1).ptr->str, (entry2).ptr->str))
-#define CHASH_PARAM_COMPARE_KEY_ENTRY(arg, key1, entry2) (!strcmp((key1), (entry2).ptr->str))
+#define CHASH_PARAM_COMPARE_KEY_ENTRY(arg, key1, entry2) ((key1).len == (entry2).ptr->str_len && !memcmp((key1).str, (entry2).ptr->str, (key1).len))
 #define CHASH_PARAM_ENTRY_NEXT hash_next