diff options
Diffstat (limited to 'common/dict.c')
-rw-r--r-- | common/dict.c | 168 |
1 files changed, 82 insertions, 86 deletions
diff --git a/common/dict.c b/common/dict.c index c42b8b4..c69447e 100644 --- a/common/dict.c +++ b/common/dict.c @@ -2,7 +2,7 @@ dict.c - dictionary functions This file is part of the nss-pam-ldapd library. - Copyright (C) 2007, 2008, 2009, 2010 Arthur de Jong + Copyright (C) 2007, 2008, 2009, 2010, 2012 Arthur de Jong This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -72,9 +72,9 @@ struct dictionary { /* Simple hash function that computes the hash value of a string. */ static uint32_t stringhash(const char *str) { - uint32_t hash=0; - while (*str!='\0') - hash=3*hash+*str++; + uint32_t hash = 0; + while (*str != '\0') + hash = 3 * hash + *str++; return hash; } @@ -84,34 +84,34 @@ static void growhashtable(DICT *dict) int i; int newsize; struct dict_entry **newtable; - struct dict_entry *entry,*tmp; - newsize=dict->size*3+1; + struct dict_entry *entry, *tmp; + newsize = dict->size * 3 + 1; /* allocate room for new hashtable */ - newtable=(struct dict_entry **)malloc(newsize*sizeof(struct dict_entry *)); - if (newtable==NULL) + newtable = (struct dict_entry **)malloc(newsize * sizeof(struct dict_entry *)); + if (newtable == NULL) return; /* allocating memory failed continue to fill the existing table */ /* clear new table */ - for (i=0;i<newsize;i++) - newtable[i]=NULL; + for (i = 0; i < newsize; i++) + newtable[i] = NULL; /* copy old hashtable into new table */ - for (i=0;i<dict->size;i++) + for (i = 0; i < dict->size; i++) { /* go over elements in linked list */ - entry=dict->table[i]; - while (entry!=NULL) + entry = dict->table[i]; + while (entry != NULL) { - tmp=entry; - entry=entry->next; + tmp = entry; + entry = entry->next; /* put in new position */ - tmp->next=newtable[tmp->hash%newsize]; - newtable[tmp->hash%newsize]=tmp; + tmp->next = newtable[tmp->hash % newsize]; + newtable[tmp->hash % newsize] = tmp; } } /* free the old hashtable */ free(dict->table); /* put new hashtable in place */ - dict->size=newsize; - dict->table=newtable; + dict->size = newsize; + dict->table = newtable; } DICT *dict_new(void) @@ -119,37 +119,37 @@ DICT *dict_new(void) struct dictionary *dict; int i; /* allocate room for dictionary information */ - dict=(struct dictionary *)malloc(sizeof(struct dictionary)); - if (dict==NULL) + dict = (struct dictionary *)malloc(sizeof(struct dictionary)); + if (dict == NULL) return NULL; - dict->size=DICT_INITSIZE; - dict->num=0; + dict->size = DICT_INITSIZE; + dict->num = 0; /* allocate initial hashtable */ - dict->table=(struct dict_entry **)malloc(DICT_INITSIZE*sizeof(struct dict_entry *)); - if (dict->table==NULL) + dict->table = (struct dict_entry **)malloc(DICT_INITSIZE * sizeof(struct dict_entry *)); + if (dict->table == NULL) { free(dict); return NULL; } /* clear the hashtable */ - for (i=0;i<DICT_INITSIZE;i++) - dict->table[i]=NULL; + for (i = 0; i < DICT_INITSIZE; i++) + dict->table[i] = NULL; /* we're done */ return dict; } void dict_free(DICT *dict) { - struct dict_entry *entry,*etmp; + struct dict_entry *entry, *etmp; int i; /* free hashtable entries */ - for (i=0;i<dict->size;i++) + for (i = 0; i < dict->size; i++) { - entry=dict->table[i]; - while (entry!=NULL) + entry = dict->table[i]; + while (entry != NULL) { - etmp=entry; - entry=entry->next; + etmp = entry; + entry = entry->next; free(etmp); } } @@ -159,17 +159,16 @@ void dict_free(DICT *dict) free(dict); } -void *dict_get(DICT *dict,const char *key) +void *dict_get(DICT *dict, const char *key) { uint32_t hash; struct dict_entry *entry; /* calculate the hash */ - hash=stringhash(key); + hash = stringhash(key); /* loop over the linked list in the hashtable */ - for (entry=dict->table[hash%dict->size];entry!=NULL;entry=entry->next) + for (entry = dict->table[hash % dict->size]; entry != NULL; entry = entry->next) { - if ( (entry->hash==hash) && - (strcmp(entry->key,key)==0) ) + if ((entry->hash == hash) && (strcmp(entry->key, key) == 0)) return entry->value; } /* no matches found */ @@ -180,69 +179,66 @@ const char *dict_getany(DICT *dict) { int i; /* loop over the linked list in the hashtable */ - for (i=0;i<dict->size;i++) + for (i = 0; i < dict->size; i++) if (dict->table[i]) return dict->table[i]->key; /* no matches found */ return NULL; } -int dict_put(DICT *dict,const char *key,void *value) +int dict_put(DICT *dict, const char *key, void *value) { uint32_t hash; int l; char *buf; int idx; - struct dict_entry *entry,*prev; + struct dict_entry *entry, *prev; /* check if we should grow the hashtable */ - if ( dict->num >= ((dict->size*DICT_LOADPERCENTAGE)/100) ) + if (dict->num >= ((dict->size * DICT_LOADPERCENTAGE) / 100)) growhashtable(dict); /* calculate the hash and position in the hashtable */ - hash=stringhash(key); - idx=hash%dict->size; + hash = stringhash(key); + idx = hash % dict->size; /* check if the entry is already present */ - for (entry=dict->table[idx],prev=NULL; - entry!=NULL; - prev=entry,entry=entry->next) + for (entry = dict->table[idx], prev = NULL; entry != NULL; prev = entry, entry = entry->next) { - if ( (entry->hash==hash) && - (strcmp(entry->key,key)==0) ) + if ((entry->hash == hash) && (strcmp(entry->key, key) == 0)) { /* check if we should unset the entry */ - if (value==NULL) + if (value == NULL) { /* remove from linked list */ - if (prev==NULL) - dict->table[idx]=entry->next; + if (prev == NULL) + dict->table[idx] = entry->next; else - prev->next=entry->next; + prev->next = entry->next; /* free entry memory and register removal */ free(entry); dict->num--; return 0; } /* just set the new value */ - entry->value=value; + entry->value = value; return 0; } } /* if entry should be unset we're done */ - if (value==NULL) + if (value == NULL) return 0; /* entry is not present, make new entry */ - l=strlen(key)+1; - buf=(char *)malloc(sizeof(struct dict_entry)+l); - if (buf==NULL) + l = strlen(key) + 1; + buf = (char *)malloc(sizeof(struct dict_entry) + l); + if (buf == NULL) return -1; - entry=(struct dict_entry *)(void *)buf; - buf+=sizeof(struct dict_entry); - strcpy(buf,key); - entry->hash=hash; - entry->key=buf; - entry->value=value; + entry = (struct dict_entry *)(void *)buf; + buf += sizeof(struct dict_entry); + strcpy(buf, key); + entry->hash = hash; + entry->key = buf; + entry->value = value; /* insert into hashtable/linked list */ - entry->next=dict->table[idx]; - dict->table[idx]=entry; + entry->next = dict->table[idx]; + dict->table[idx] = entry; /* increment number of stored items */ dict->num++; return 0; @@ -257,38 +253,38 @@ const char **dict_keys(DICT *dict) size_t sz; int num; /* figure out how much memory to allocate */ - num=0; - sz=0; - for (i=0;i<dict->size;i++) + num = 0; + sz = 0; + for (i = 0; i < dict->size; i++) { - entry=dict->table[i]; - while (entry!=NULL) + entry = dict->table[i]; + while (entry != NULL) { num++; - sz+=strlen(entry->key)+1; - entry=entry->next; + sz += strlen(entry->key) + 1; + entry = entry->next; } } /* allocate the needed memory */ - buf=(char *)malloc((num+1)*sizeof(char *)+sz); - if (buf==NULL) + buf = (char *)malloc((num + 1) * sizeof(char *) + sz); + if (buf == NULL) return NULL; - values=(const char **)(void *)buf; - buf+=(num+1)*sizeof(char *); + values = (const char **)(void *)buf; + buf += (num + 1) * sizeof(char *); /* fill the array with the keys */ - num=0; - for (i=0;i<dict->size;i++) + num = 0; + for (i = 0; i < dict->size; i++) { - entry=dict->table[i]; - while (entry!=NULL) + entry = dict->table[i]; + while (entry != NULL) { - strcpy(buf,entry->key); - values[num++]=buf; - buf+=strlen(buf)+1; - entry=entry->next; + strcpy(buf, entry->key); + values[num++] = buf; + buf += strlen(buf) + 1; + entry = entry->next; } } - values[num]=NULL; + values[num] = NULL; /* done */ return values; } |