Mercurial > cgi-bin > hgwebdir.cgi > VMS > C_Libraries > Hash_impl
diff PrivateHash.c @ 9:7c4d2bf121a9
Bugfix: Strings were used to handle keys. So 0 byte in key will terminate all functions.
| author | Merten Sach <msach@mailbox.tu-berlin.de> |
|---|---|
| date | Wed, 28 Sep 2011 13:38:15 +0200 |
| parents | 8b225987970d |
| children | 5b89d57e5d10 093cad17d992 |
line diff
1.1 --- a/PrivateHash.c Mon Jul 11 18:10:14 2011 +0200 1.2 +++ b/PrivateHash.c Wed Sep 28 13:38:15 2011 +0200 1.3 @@ -71,11 +71,13 @@ 1.4 } 1.5 1.6 unsigned int 1.7 -hashThisKey( char *s, int hashSz ) 1.8 +hashThisKey( char* s, int hashSz ) 1.9 { unsigned int h = 0; 1.10 + unsigned int i; 1.11 + hashkey_t* key = (hashkey_t*)s; 1.12 1.13 - for( ; *s != 0; s++ ) 1.14 - h = *s + h*31; 1.15 + for(i=0 ; i<sizeof(hashkey_t); i++ ) 1.16 + h = key->hashable[i] + h*31; 1.17 return h % hashSz; 1.18 } 1.19 1.20 @@ -88,7 +90,9 @@ 1.21 HashEntry* 1.22 hashEntry = table->entries[ hashIndex ]; 1.23 for( ; hashEntry != NULL; hashEntry = hashEntry->next ) 1.24 - { if( strcmp( hashEntry->key, key ) == 0 ) return hashEntry; 1.25 + { 1.26 + if( memcmp( hashEntry->key, key, sizeof(hashkey_t) ) == 0 ) 1.27 + return hashEntry; 1.28 } 1.29 return NULL; 1.30 } 1.31 @@ -115,9 +119,9 @@ 1.32 { hashIdx = hashThisKey( key, table->tableSz ); 1.33 hashEntry = (HashEntry*) VMS__malloc( sizeof( HashEntry ) ); 1.34 if( hashEntry == NULL ) return 0; 1.35 - hashEntry->key = VMS__malloc( strlen(key) ); 1.36 + hashEntry->key = VMS__malloc( sizeof(hashkey_t) ); 1.37 if( hashEntry->key == NULL ) return 0; 1.38 - strcpy( hashEntry->key, key ); 1.39 + memcpy( hashEntry->key, key, sizeof(hashkey_t) ); 1.40 hashEntry->next = (table->entries)[hashIdx]; 1.41 (table->entries)[hashIdx] = hashEntry; 1.42 table->numEntries += 1; 1.43 @@ -164,7 +168,7 @@ 1.44 1.45 testEntry = table->entries[ hashIndex ]; 1.46 for( ; testEntry != NULL; testEntry = testEntry->next ) 1.47 - { if( strcmp( testEntry->key, entry->key ) == 0 ) 1.48 + { if( memcmp( testEntry->key, entry->key, sizeof(hashkey_t)) == 0 ) 1.49 { if( prevEntry == NULL ) 1.50 { table->entries[hashIndex] = entry; 1.51 entry->next = testEntry->next; 1.52 @@ -194,7 +198,7 @@ 1.53 addrOfHashEntryPtr = &( table->entries[ hashIndex ] ); 1.54 hashEntry = *addrOfHashEntryPtr; 1.55 while( hashEntry != NULL ) 1.56 - { if( strcmp( hashEntry->key, key ) == 0 ) 1.57 + { if( memcmp( hashEntry->key, key, sizeof(hashkey_t) ) == 0 ) 1.58 { 1.59 *addrOfHashEntryPtr = hashEntry->next; 1.60 //TODO: Free the contents of entry? 1.61 @@ -207,40 +211,7 @@ 1.62 } 1.63 return FALSE; 1.64 } 1.65 - 1.66 1.67 -/* debugging function displays the hashtable in (key.value) pairs 1.68 -*/ 1.69 -/*void hashTableToString( HashTable * table ) 1.70 - { int i; 1.71 - HashEntry *t; 1.72 - for( i = 0; i < table->tableSz; i++ ) 1.73 - { t = entries[i]; 1.74 - if( t == NULL ) 1.75 - strcat_m( retStr, &"()" ); 1.76 - else 1.77 - { strcat_m( retStr, &"(" ); 1.78 - for( ; t != NULL; t = t->next ) 1.79 - { strcat_m( retStr, &" " ); 1.80 - strcat_m( retStr, t->key ); 1.81 - strcat_m( retStr, &"." ); 1.82 - strcat_m( retStr, paramToString( t->param ) ); 1.83 - strcat_m( retStr, &" " ); 1.84 - } 1.85 - strcat_m( retStr, &")" ); 1.86 - } 1.87 - } 1.88 - } 1.89 -*/ 1.90 - 1.91 -/* 1.92 - void 1.93 -setFnToFreeEntryContent( HashTable *table, FreeEntryContentFnPtr fnPtr ) 1.94 - { 1.95 - table->freeEntryContentFn = fnPtr; 1.96 - } 1.97 -*/ 1.98 - 1.99 void 1.100 freeHashTable( HashTable *table ) 1.101 { int i;
