dictionary hashing algorithm


product: microsoft exemod, exepack, or lib utility
title: dictionary hashing algorithm used by the lib utility

updated: 16-may-1991
operating system versions: 3.0x 3.10 3.11 3.14 3.15 3.17 3.18 | 3.1
operating systems: ms-dos | os/2

summary:

the last part of each library produced by the microsoft library
manager (lib) contains a dictionary that holds all the public symbols
in the library. the hashing algorithm mentioned on page 63 of the
"microsoft c developer's toolkit reference" is used to place data in
the dictionary. the code required to implement the hashing algorithm
is shown at the end of this article.

more information:

the library dictionary is divided into pages that are 512 bytes long.
each page starts with a 37-byte bucket table, which contains 37
separate offsets to the symbols in the rest of the page. the values in
the buckets are multiplied by 2 to get the actual offset (since 1 byte
can contain only 256 different values).

the hashing algorithm analyzes a symbol's name and produces two
indexes (page index and bucket index) and two deltas (page index delta
and bucket index delta). using the offset contained in the bucket at
bucket index in the page at page index, you must compare the symbol at
that location with the one you are looking for.

if (due to symbol collision) you have not found the correct symbol,
add the bucket index delta to the current bucket index, modulo 37, and
try again. continue until all the buckets in the current page are
tried. then, add the page index delta to the current page, modulo by
the page count, and try all the buckets in that page starting at
bucket index. continue this process until all of the possible page and
offset combinations have been tried.

for more information on the actual format of the symbols in the
dictionary, and information on the format for the rest of the library,
see the "microsoft c developer's toolkit reference."

sample code
-----------

/* this code illustrates the hashing algorithm used by lib */

/* compile options needed: none
*/

#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <stdlib.h>

#define xor ^
#define modulo %

char *symbol; /* symbol to find (or to place) */
int dictlength; /* dictionary length in pages */
int buckets; /* number of buckets on one page */

char *pb; /* a pointer to the beginning of the symbol */
char *pe; /* a pointer to the end of the symbol */
int slength; /* length of the symbol's name */

int page_index; /* page index */
int page_index_delta; /* page index delta */
int bucket_index; /* bucket index */
int bucket_index_delta; /* bucket index delta */

unsigned c;

void hash(void)
{
page_index = 0;
page_index_delta = 0;
bucket_index = 0;
bucket_index_delta = 0;

while( slength--)
{
c = *(pb++) | 32; /* convert character to lower case */
page_index = (page_index<<2) xor c; /* hash */
bucket_index_delta = (bucket_index_delta>>2) xor c; /* hash */
c = *(pe--) | 32;
bucket_index = (bucket_index>>2) xor c; /* hash */
page_index_delta = (page_index_delta<<2) xor c; /* hash */
}
/* calculate page index */
page_index = page_index modulo dictlength;

/* calculate page index delta */
if( (page_index_delta = page_index_delta modulo dictlength) == 0)
page_index_delta = 1;

/* calculate bucket offset */
bucket_index = bucket_index modulo buckets;

/* calculate bucket offset delta */
if( (bucket_index_delta = bucket_index_delta modulo buckets) == 0)
bucket_index_delta = 1;
}

void main(void)
{
int i;
dictlength = 3;
buckets = 37;

if ( (symbol = (char *) malloc( sizeof(char) * 4 )) == null )
exit(1);

strcpy( symbol, "one");

for( i = 0; i < 2; i++ ) {
slength = strlen(symbol);
pb = symbol;
pe = symbol + slength ;
hash();
printf("\npage_index: %2d page_index_delta: %d",
page_index, page_index_delta);
printf("\nbucket_index: %2d bucket_index_delta: %d",
bucket_index, bucket_index_delta);
strcpy( symbol, "two");
}