#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#define BIOSBUFSIZE 2048
#define HASHSIZE 128
#define INSERTHASHFAIL -1
#define INSERTHASHSUCCESS -1
char smbiosBuf[BIOSBUFSIZE];
typedef struct smbiosTable smbiosTable_T;
struct smbiosTable{
char type;
char length;
short handle;
};
typedef struct node node_t;
struct node{
void *pData;
node_t *next;
};
typedef struct hash hast_t;
struct hash{
node_t **ppContainer;
int pCapacity;
int nodeCount;
};
hast_t *globalHashTable;
/**************************/
/************************/
int createHash(void)
{
globalHashTable = malloc(sizeof(hast_t));
if(NULL == globalHashTable)
{
return -1;
}
globalHashTable->ppContainer = malloc(HASHSIZE * sizeof(node_t *));
if(NULL == globalHashTable->ppContainer)
{
free(globalHashTable);
return -1;
}
memset(globalHashTable->ppContainer,0,HASHSIZE * sizeof(node_t *));
globalHashTable->pCapacity = HASHSIZE;
globalHashTable->nodeCount = 0;
return 0;
}
/*
函数功能:获取哈希关键数值
*/
static int getHashKey(int pdata)
{
return (pdata)%HASHSIZE;
}
/*
函数功能,将每个元素插入到哈系表中
*/
int insert(void *pData)
{
if(NULL == pData)
{
return -1;
}
node_t *pNode = malloc(sizeof(node_t));
node_t *pTemp =NULL;
int index;
if(NULL == pNode)
return INSERTHASHFAIL;
index = getHashKey(*(char *)pData);
pNode->pData = pData;
pNode->next = NULL;
pTemp = globalHashTable->ppContainer[index];
if(pTemp == NULL)
{
globalHashTable->ppContainer[index] = pNode;
return INSERTHASHSUCCESS;
}
while( NULL != pTemp->next)
{
pTemp=pTemp->next;
}
pTemp ->next =pNode;
return INSERTHASHSUCCESS;
}
static int cmpFunc(short data1,short data2)
{
if(data1 == data2 )
return 0;
return -1;
}
/*
函数功能:查询hash表内的元素
*/
void *search(int data)
{
if(data <0 || data > 128)
{
return NULL;
}
return globalHashTable->ppContainer[data];
}
#define OPENBIOSFAIL -1
int openBios(void)
{
if(smbiosBuf == NULL)
return OPENBIOSFAIL;
char *ptmp,*ptmp2;
ptmp = smbiosBuf ;
int i=0;
if( (0==((smbiosTable_T *)ptmp)->type) && (0 == ((smbiosTable_T *)ptmp)->handle))
{
i++;
insert((void *)ptmp);
while(i < 45)
{
ptmp2=NULL;
ptmp2=ptmp+((smbiosTable_T *)ptmp)->length;
while((*ptmp2 != 0) || *(ptmp2+1) !=0)
{
ptmp2++;
}
ptmp2+=2;
ptmp=ptmp2;
i++;
insert((void *)ptmp);
}
if(45 != i)
{
return OPENBIOSFAIL;
}
}else{
return OPENBIOSFAIL;
}
return 0;
}
int main(void)
{
int fd;
ssize_t ret;
fd = open("/dev/mem",O_RDONLY);
if(-1 == fd)
{
perror("OPEN");
return -1;
}
ret = lseek(fd,0xE0010,SEEK_SET);
if(-1 == ret)
{
perror("LSEEK");
return -1;
}
ret = read(fd,smbiosBuf,BIOSBUFSIZE);
createHash();
smbiosTable_T *temp;
temp = (smbiosTable_T *)smbiosBuf;
printf("%d %d %d\n",temp->type,temp->length,temp->handle);
ret =openBios();
printf("%d\n",ret);
printf("------%d",((smbiosTable_T *)((globalHashTable->ppContainer[127])->pData))->type);
return 0;
}