diff options
author | Evan Huus <eapache@gmail.com> | 2013-06-18 17:39:15 +0000 |
---|---|---|
committer | Evan Huus <eapache@gmail.com> | 2013-06-18 17:39:15 +0000 |
commit | d0c1d89cd63f522443a7e5345e5c51bff00849d5 (patch) | |
tree | f6caf7013544872195f0b7ea2fd275f26a63322c /epan/wmem | |
parent | 99e1ad5443468134de4062b53e05764a03c713c0 (diff) |
Factor string-packing code into a common function to use in both insert_string
and lookup_string.
svn path=/trunk/; revision=50006
Diffstat (limited to 'epan/wmem')
-rw-r--r-- | epan/wmem/wmem_tree.c | 73 |
1 files changed, 25 insertions, 48 deletions
diff --git a/epan/wmem/wmem_tree.c b/epan/wmem/wmem_tree.c index 6379a51261..f86e0912a8 100644 --- a/epan/wmem/wmem_tree.c +++ b/epan/wmem/wmem_tree.c @@ -515,26 +515,26 @@ wmem_tree_lookup32_le(wmem_tree_t *tree, guint32 key) After the uint32's containing the string, there is one final terminator uint32 with the value 0x00000001 */ -void -wmem_tree_insert_string(wmem_tree_t* tree, const gchar* k, void* v, guint32 flags) +static guint32 * +pack_string(const gchar *key, guint32 *divx, guint32 flags) { - wmem_tree_key_t key[2]; - guint32 *aligned=NULL; - guint32 len = (guint32) strlen(k); - guint32 divx = (len+3)/4+1; + guint32 *aligned = NULL; + guint32 len = (guint32) strlen(key); guint32 i; guint32 tmp; - aligned = (guint32 *)g_malloc(divx * sizeof (guint32)); + *divx = (len+3)/4 + 1; + + aligned = (guint32 *)g_malloc(*divx * sizeof (guint32)); /* pack the bytes one one by one into guint32s */ tmp = 0; for (i = 0;i < len;i++) { unsigned char ch; - ch = (unsigned char)k[i]; + ch = (unsigned char)key[i]; if (flags & WMEM_TREE_STRING_NOCASE) { - if(isupper(ch)) { + if (isupper(ch)) { ch = tolower(ch); } } @@ -555,14 +555,25 @@ wmem_tree_insert_string(wmem_tree_t* tree, const gchar* k, void* v, guint32 flag } /* add the terminator */ - aligned[divx-1] = 0x00000001; + aligned[*divx-1] = 0x00000001; + + return aligned; +} + +void +wmem_tree_insert_string(wmem_tree_t* tree, const gchar* k, void* v, guint32 flags) +{ + wmem_tree_key_t key[2]; + guint32 *aligned; + guint32 divx; + + aligned = pack_string(k, &divx, flags); key[0].length = divx; key[0].key = aligned; key[1].length = 0; key[1].key = NULL; - wmem_tree_insert32_array(tree, key, v); g_free(aligned); } @@ -571,51 +582,17 @@ void * wmem_tree_lookup_string(wmem_tree_t* tree, const gchar* k, guint32 flags) { wmem_tree_key_t key[2]; - guint32 *aligned=NULL; - guint32 len = (guint) strlen(k); - guint32 divx = (len+3)/4+1; - guint32 i; - guint32 tmp; + guint32 *aligned; + guint32 divx; void *ret; - aligned = (guint32 *)g_malloc(divx * sizeof (guint32)); - - /* pack the bytes one one by one into guint32s */ - tmp = 0; - for (i = 0;i < len;i++) { - unsigned char ch; - - ch = (unsigned char)k[i]; - if (flags & WMEM_TREE_STRING_NOCASE) { - if(isupper(ch)) { - ch = tolower(ch); - } - } - tmp <<= 8; - tmp |= ch; - if (i%4 == 3) { - aligned[i/4] = tmp; - tmp = 0; - } - } - /* add required padding to the last uint32 */ - if (i%4 != 0) { - while (i%4 != 0) { - i++; - tmp <<= 8; - } - aligned[i/4-1] = tmp; - } - - /* add the terminator */ - aligned[divx-1] = 0x00000001; + aligned = pack_string(k, &divx, flags); key[0].length = divx; key[0].key = aligned; key[1].length = 0; key[1].key = NULL; - ret = wmem_tree_lookup32_array(tree, key); g_free(aligned); return ret; |