183 lines
5.4 KiB
C++
Executable File
183 lines
5.4 KiB
C++
Executable File
// hash.cpp
|
|
|
|
#include "hash.h"
|
|
|
|
//===========================================================================
|
|
HashItem::HashItem(char *s, int v) {
|
|
if (strlen(s) > STR_LEN) {
|
|
cout<<endl<<"Hash item string too long";
|
|
exit(-1);
|
|
}
|
|
strcpy(str, s); value = v;
|
|
}
|
|
//===========================================================================
|
|
void HashItem::Set(char *s, int v) {
|
|
if (strlen(s) > STR_LEN) {
|
|
cout<<endl<<"Hash item string too long";
|
|
exit(-1);
|
|
}
|
|
strcpy(str, s); value = v;
|
|
}
|
|
//===========================================================================
|
|
void HashTable::Insert(HashItem &h_item) {
|
|
int hash_index = HashFunc(h_item.str);
|
|
#ifdef DBG
|
|
cout<<hash_index;cout.flush();
|
|
#endif
|
|
data[hash_index].Insert(h_item);
|
|
}
|
|
//===========================================================================
|
|
int HashTable::Retrieve(HashItem &h_item) {
|
|
int hash_index = HashFunc(h_item.str);
|
|
HashItem *temp_item_ptr = data[hash_index].Search(h_item);
|
|
if (!temp_item_ptr) return 0;
|
|
else {
|
|
h_item = *temp_item_ptr;
|
|
return 1;
|
|
}
|
|
}
|
|
//===========================================================================
|
|
unsigned HashTable::HashFunc(char *str) {
|
|
unsigned k = 0;
|
|
for (int i = 0; i < strlen(str); i++) {
|
|
k += (unsigned)str[i] << (i * 8);
|
|
}
|
|
return (k % data.Size());
|
|
}
|
|
//=============================================================================
|
|
ostream &operator<<(ostream &s, HashTable &ht) {
|
|
for (int i = 0; i < ht.data.Size(); i++) {
|
|
if (!ht.data[i].Empty()) {
|
|
ht.data[i].Write(s);
|
|
s<<endl;
|
|
}
|
|
}
|
|
return s;
|
|
}
|
|
//===========================================================================
|
|
// for int hash tables
|
|
//===========================================================================
|
|
HashItemInt &HashItemInt::operator = (const HashItemInt &h_item) {
|
|
key = h_item.key;
|
|
value = h_item.value;
|
|
return *this;
|
|
}
|
|
//===========================================================================
|
|
void HashTableInt::Insert(HashItemInt &h_item) {
|
|
int hash_index = HashFunc(h_item.key);
|
|
data[hash_index].Insert(h_item);
|
|
}
|
|
//===========================================================================
|
|
int HashTableInt::Retrieve(HashItemInt &h_item) {
|
|
int hash_index = HashFunc(h_item.key);
|
|
HashItemInt *temp_item_ptr;
|
|
temp_item_ptr = data[hash_index].Search(h_item);
|
|
if (!temp_item_ptr) return 0;
|
|
else {
|
|
h_item = *temp_item_ptr;
|
|
return 1;
|
|
}
|
|
}
|
|
//===========================================================================
|
|
unsigned HashTableInt::HashFunc(int key) {
|
|
return (key % data.Size());
|
|
}
|
|
//=============================================================================
|
|
ostream &operator<<(ostream &s, HashTableInt &ht) {
|
|
for (int i = 0; i < ht.data.Size(); i++) {
|
|
if (!ht.data[i].Empty()) {
|
|
ht.data[i].Write(s);
|
|
s<<endl;
|
|
}
|
|
}
|
|
return s;
|
|
}
|
|
//===========================================================================
|
|
void HashTableInt::PutInArray(Array<HashItemInt> &h_array, int &num_items) {
|
|
num_items = 0;
|
|
HashItemInt h_item;
|
|
for (int i = 0; i < data.Size(); i++) {
|
|
if (!data[i].Empty()) { // now iterate through the linked list
|
|
int start = 1;
|
|
while (data[i].GetNext(h_item, start)) {
|
|
h_array[num_items].Set(h_item.key, h_item.value);
|
|
start = 0;
|
|
num_items++;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
//===========================================================================
|
|
int HashTableInt::ExtToInt(int key, int next_value)
|
|
{
|
|
HashItemInt h_item(key, 0);
|
|
|
|
// Check to see if we know this one. 0 matches any number. If we
|
|
// do know this one, h_item.value gets set to what we knew it to be.
|
|
if (!Retrieve(h_item)) {
|
|
h_item.Set(key, next_value);
|
|
Insert(h_item);
|
|
}
|
|
return (h_item.value);
|
|
}
|
|
|
|
/*
|
|
// to test out the hash table
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <fstream.h>
|
|
|
|
#define MAX_SYS_CALLS 255
|
|
//============================================================================
|
|
int GetCalls(HashTable &ht) {
|
|
ifstream calls_file("calls.txt");
|
|
char buff[255];
|
|
int buff_len;
|
|
int num_sys_calls = 0;
|
|
HashItem h_item;
|
|
while (!calls_file.eof() && num_sys_calls < MAX_SYS_CALLS) {
|
|
calls_file.getline(buff, 254);
|
|
buff_len = strlen(buff);
|
|
if (buff_len) {
|
|
// cat on a parenth to make sure only calls are matched
|
|
strcat(buff, "(");
|
|
#ifdef DBG
|
|
cout<<endl<<buff; cout.flush();
|
|
#endif
|
|
h_item.Set(buff, num_sys_calls);
|
|
ht.Insert(h_item);
|
|
num_sys_calls++;
|
|
}
|
|
}
|
|
calls_file.close();
|
|
if (num_sys_calls == MAX_SYS_CALLS) return 0;
|
|
else return 1;
|
|
}
|
|
//============================================================================
|
|
void main(void) {
|
|
HashTable hashtable(701);
|
|
HashItem h_item("unlink(", 0);
|
|
if (GetCalls(hashtable)) {
|
|
cout<<endl<<hashtable;
|
|
h_item.Set("unlink(", 0);
|
|
if (hashtable.Retrieve(h_item))
|
|
cout<<endl<<" unlink found, index = "<<h_item.value;
|
|
else cout<<endl<<" unlink not found";
|
|
h_item.Set("get_kernel_syms(", 0);
|
|
if (hashtable.Retrieve(h_item))
|
|
cout<<endl<<" get_kernel_syms found, index = "<<h_item.value;
|
|
else cout<<endl<<" get_kernel_syms not found";
|
|
h_item.Set("hello(", 0);
|
|
if (hashtable.Retrieve(h_item))
|
|
cout<<endl<<" hello found, index = "<<h_item.value;
|
|
else cout<<endl<<" hello not found";
|
|
h_item.Set("setsockopt(", 0);
|
|
if (hashtable.Retrieve(h_item))
|
|
cout<<endl<<" setsockopt found, index = "<<h_item.value;
|
|
else cout<<endl<<" setsockopt not found";
|
|
}
|
|
}
|
|
*/
|
|
|
|
|