mirror of
https://github.com/openmoh/openmohaa.git
synced 2025-04-28 21:57:57 +03:00
Replace tabs by spaces
This commit is contained in:
parent
c1a66bf9e7
commit
f89bfba5dc
12 changed files with 1085 additions and 1052 deletions
|
@ -20,30 +20,30 @@
|
|||
#define DEF_GROWBY 8
|
||||
|
||||
#ifdef _NO_NOPORT_H_
|
||||
#define gsimalloc malloc
|
||||
#define gsifree free
|
||||
#define gsirealloc realloc
|
||||
#include "common/gsAssert.h"
|
||||
#define gsimalloc malloc
|
||||
#define gsifree free
|
||||
#define gsirealloc realloc
|
||||
#include "common/gsAssert.h"
|
||||
#else
|
||||
#include "nonport.h" //for gsimalloc/realloc/free/GS_ASSERT
|
||||
#include "nonport.h" //for gsimalloc/realloc/free/GS_ASSERT
|
||||
#endif
|
||||
|
||||
|
||||
// STRUCTURES
|
||||
struct DArrayImplementation
|
||||
{
|
||||
int count, capacity;
|
||||
int elemsize;
|
||||
int growby;
|
||||
ArrayElementFreeFn elemfreefn;
|
||||
void *list; //array of elements
|
||||
int count, capacity;
|
||||
int elemsize;
|
||||
int growby;
|
||||
ArrayElementFreeFn elemfreefn;
|
||||
void *list; //array of elements
|
||||
};
|
||||
|
||||
// PROTOTYPES
|
||||
static void *mylsearch(const void *key, void *base, int count, int size,
|
||||
ArrayCompareFn comparator);
|
||||
ArrayCompareFn comparator);
|
||||
static void *mybsearch(const void *elem, void *base, int num, int elemsize,
|
||||
ArrayCompareFn comparator, int *found);
|
||||
ArrayCompareFn comparator, int *found);
|
||||
// FUNCTIONS
|
||||
|
||||
/* FreeElement
|
||||
|
@ -51,8 +51,8 @@ static void *mybsearch(const void *elem, void *base, int num, int elemsize,
|
|||
*/
|
||||
static void FreeElement(DArray array, int n)
|
||||
{
|
||||
if (array->elemfreefn != NULL)
|
||||
array->elemfreefn(ArrayNth(array,n));
|
||||
if (array->elemfreefn != NULL)
|
||||
array->elemfreefn(ArrayNth(array,n));
|
||||
}
|
||||
|
||||
/* ArrayGrow
|
||||
|
@ -60,10 +60,10 @@ static void FreeElement(DArray array, int n)
|
|||
*/
|
||||
static void ArrayGrow(DArray array)
|
||||
{
|
||||
GS_ASSERT(array->elemsize) // sanity check -mj Oct 31st
|
||||
array->capacity += array->growby;
|
||||
array->list = gsirealloc(array->list, (size_t) array->capacity * array->elemsize);
|
||||
GS_ASSERT(array->list);
|
||||
GS_ASSERT(array->elemsize) // sanity check -mj Oct 31st
|
||||
array->capacity += array->growby;
|
||||
array->list = gsirealloc(array->list, (size_t) array->capacity * array->elemsize);
|
||||
GS_ASSERT(array->list);
|
||||
}
|
||||
|
||||
/* SetElement
|
||||
|
@ -71,94 +71,94 @@ static void ArrayGrow(DArray array)
|
|||
*/
|
||||
static void SetElement(DArray array, const void *elem, int pos)
|
||||
{
|
||||
GS_ASSERT(array) // safety check -mj Oct 31st
|
||||
GS_ASSERT(elem)
|
||||
GS_ASSERT(array->elemsize)
|
||||
GS_ASSERT(array) // safety check -mj Oct 31st
|
||||
GS_ASSERT(elem)
|
||||
GS_ASSERT(array->elemsize)
|
||||
|
||||
memcpy(ArrayNth(array,pos), elem, (size_t)array->elemsize);
|
||||
memcpy(ArrayNth(array,pos), elem, (size_t)array->elemsize);
|
||||
}
|
||||
|
||||
DArray ArrayNew(int elemSize, int numElemsToAllocate,
|
||||
ArrayElementFreeFn elemFreeFn)
|
||||
{
|
||||
DArray array;
|
||||
DArray array;
|
||||
|
||||
array = (DArray) gsimalloc(sizeof(struct DArrayImplementation));
|
||||
GS_ASSERT(array);
|
||||
GS_ASSERT(elemSize);
|
||||
if (numElemsToAllocate == 0)
|
||||
numElemsToAllocate = DEF_GROWBY;
|
||||
array->count = 0;
|
||||
array->capacity = numElemsToAllocate;;
|
||||
array->elemsize = elemSize;
|
||||
array->growby = numElemsToAllocate;
|
||||
array->elemfreefn = elemFreeFn;
|
||||
if (array->capacity != 0)
|
||||
{
|
||||
array->list = gsimalloc((size_t)array->capacity * array->elemsize);
|
||||
GS_ASSERT(array->list);
|
||||
} else
|
||||
array->list = NULL;
|
||||
array = (DArray) gsimalloc(sizeof(struct DArrayImplementation));
|
||||
GS_ASSERT(array);
|
||||
GS_ASSERT(elemSize);
|
||||
if (numElemsToAllocate == 0)
|
||||
numElemsToAllocate = DEF_GROWBY;
|
||||
array->count = 0;
|
||||
array->capacity = numElemsToAllocate;;
|
||||
array->elemsize = elemSize;
|
||||
array->growby = numElemsToAllocate;
|
||||
array->elemfreefn = elemFreeFn;
|
||||
if (array->capacity != 0)
|
||||
{
|
||||
array->list = gsimalloc((size_t)array->capacity * array->elemsize);
|
||||
GS_ASSERT(array->list);
|
||||
} else
|
||||
array->list = NULL;
|
||||
|
||||
return array;
|
||||
return array;
|
||||
}
|
||||
|
||||
void ArrayFree(DArray array)
|
||||
{
|
||||
int i;
|
||||
int i;
|
||||
|
||||
GS_ASSERT(array);
|
||||
for (i = 0; i < array->count; i++)
|
||||
{
|
||||
FreeElement(array, i);
|
||||
}
|
||||
// mj to do: move these asserts into gsi_free. maybe, depends on whether user overloads them
|
||||
GS_ASSERT(array->list)
|
||||
GS_ASSERT(array)
|
||||
gsifree(array->list);
|
||||
gsifree(array);
|
||||
GS_ASSERT(array);
|
||||
for (i = 0; i < array->count; i++)
|
||||
{
|
||||
FreeElement(array, i);
|
||||
}
|
||||
// mj to do: move these asserts into gsi_free. maybe, depends on whether user overloads them
|
||||
GS_ASSERT(array->list)
|
||||
GS_ASSERT(array)
|
||||
gsifree(array->list);
|
||||
gsifree(array);
|
||||
}
|
||||
|
||||
void *ArrayGetDataPtr(DArray array)
|
||||
{
|
||||
GS_ASSERT(array);
|
||||
return array->list;
|
||||
GS_ASSERT(array);
|
||||
return array->list;
|
||||
}
|
||||
|
||||
void ArraySetDataPtr(DArray array, void *ptr, int count, int capacity)
|
||||
{
|
||||
int i;
|
||||
int i;
|
||||
|
||||
GS_ASSERT(array);
|
||||
if (array->list != NULL)
|
||||
{
|
||||
for (i = 0; i < array->count; i++)
|
||||
{
|
||||
FreeElement(array, i);
|
||||
}
|
||||
gsifree(array->list);
|
||||
}
|
||||
array->list = ptr;
|
||||
array->count = count;
|
||||
array->capacity = capacity;
|
||||
GS_ASSERT(array);
|
||||
if (array->list != NULL)
|
||||
{
|
||||
for (i = 0; i < array->count; i++)
|
||||
{
|
||||
FreeElement(array, i);
|
||||
}
|
||||
gsifree(array->list);
|
||||
}
|
||||
array->list = ptr;
|
||||
array->count = count;
|
||||
array->capacity = capacity;
|
||||
|
||||
}
|
||||
|
||||
|
||||
int ArrayLength(const DArray array)
|
||||
{
|
||||
GS_ASSERT(array)
|
||||
return array->count;
|
||||
GS_ASSERT(array)
|
||||
return array->count;
|
||||
}
|
||||
|
||||
void *ArrayNth(DArray array, int n)
|
||||
{
|
||||
// 2004.Nov.16.JED - modified GS_ASSERT to include "if" to add robustness
|
||||
GS_ASSERT( (n >= 0) && (n < array->count));
|
||||
if( ! ((n >= 0) && (n < array->count)) )
|
||||
return NULL;
|
||||
// 2004.Nov.16.JED - modified GS_ASSERT to include "if" to add robustness
|
||||
GS_ASSERT( (n >= 0) && (n < array->count));
|
||||
if( ! ((n >= 0) && (n < array->count)) )
|
||||
return NULL;
|
||||
|
||||
return (char *)array->list + array->elemsize*n;
|
||||
return (char *)array->list + array->elemsize*n;
|
||||
}
|
||||
|
||||
/* ArrayAppend
|
||||
|
@ -166,166 +166,166 @@ void *ArrayNth(DArray array, int n)
|
|||
*/
|
||||
void ArrayAppend(DArray array, const void *newElem)
|
||||
{
|
||||
GS_ASSERT(array);
|
||||
if(array)
|
||||
ArrayInsertAt(array, newElem, array->count);
|
||||
GS_ASSERT(array);
|
||||
if(array)
|
||||
ArrayInsertAt(array, newElem, array->count);
|
||||
}
|
||||
|
||||
void ArrayInsertAt(DArray array, const void *newElem, int n)
|
||||
{
|
||||
GS_ASSERT (array)
|
||||
GS_ASSERT ( (n >= 0) && (n <= array->count));
|
||||
GS_ASSERT (array)
|
||||
GS_ASSERT ( (n >= 0) && (n <= array->count));
|
||||
|
||||
if (array->count == array->capacity)
|
||||
ArrayGrow(array);
|
||||
array->count++;
|
||||
if (n < array->count - 1) //if we aren't appending
|
||||
memmove(ArrayNth(array, n+1), ArrayNth(array,n),
|
||||
(size_t)(array->count - 1 - n) * array->elemsize);
|
||||
SetElement(array, newElem, n);
|
||||
if (array->count == array->capacity)
|
||||
ArrayGrow(array);
|
||||
array->count++;
|
||||
if (n < array->count - 1) //if we aren't appending
|
||||
memmove(ArrayNth(array, n+1), ArrayNth(array,n),
|
||||
(size_t)(array->count - 1 - n) * array->elemsize);
|
||||
SetElement(array, newElem, n);
|
||||
}
|
||||
|
||||
void ArrayInsertSorted(DArray array, const void *newElem, ArrayCompareFn comparator)
|
||||
{
|
||||
int n;
|
||||
void *res;
|
||||
int found;
|
||||
int n;
|
||||
void *res;
|
||||
int found;
|
||||
|
||||
GS_ASSERT (array)
|
||||
GS_ASSERT (comparator);
|
||||
GS_ASSERT (array)
|
||||
GS_ASSERT (comparator);
|
||||
|
||||
res=mybsearch(newElem, array->list, array->count, array->elemsize, comparator, &found);
|
||||
n = (((char *)res - (char *)array->list) / array->elemsize);
|
||||
ArrayInsertAt(array, newElem, n);
|
||||
res=mybsearch(newElem, array->list, array->count, array->elemsize, comparator, &found);
|
||||
n = (((char *)res - (char *)array->list) / array->elemsize);
|
||||
ArrayInsertAt(array, newElem, n);
|
||||
}
|
||||
|
||||
|
||||
void ArrayRemoveAt(DArray array, int n)
|
||||
{
|
||||
GS_ASSERT (array)
|
||||
GS_ASSERT( (n >= 0) && (n < array->count));
|
||||
GS_ASSERT (array)
|
||||
GS_ASSERT( (n >= 0) && (n < array->count));
|
||||
|
||||
if (n < array->count - 1) //if not last element
|
||||
memmove(ArrayNth(array,n),ArrayNth(array,n+1),
|
||||
(size_t)(array->count - 1 - n) * array->elemsize);
|
||||
array->count--;
|
||||
if (n < array->count - 1) //if not last element
|
||||
memmove(ArrayNth(array,n),ArrayNth(array,n+1),
|
||||
(size_t)(array->count - 1 - n) * array->elemsize);
|
||||
array->count--;
|
||||
}
|
||||
|
||||
void ArrayDeleteAt(DArray array, int n)
|
||||
{
|
||||
GS_ASSERT (array)
|
||||
GS_ASSERT ( (n >= 0) && (n < array->count));
|
||||
GS_ASSERT (array)
|
||||
GS_ASSERT ( (n >= 0) && (n < array->count));
|
||||
|
||||
FreeElement(array,n);
|
||||
ArrayRemoveAt(array, n);
|
||||
FreeElement(array,n);
|
||||
ArrayRemoveAt(array, n);
|
||||
}
|
||||
|
||||
|
||||
void ArrayReplaceAt(DArray array, const void *newElem, int n)
|
||||
{
|
||||
GS_ASSERT (array)
|
||||
GS_ASSERT ( (n >= 0) && (n < array->count));
|
||||
GS_ASSERT (array)
|
||||
GS_ASSERT ( (n >= 0) && (n < array->count));
|
||||
|
||||
FreeElement(array, n);
|
||||
SetElement(array, newElem,n);
|
||||
FreeElement(array, n);
|
||||
SetElement(array, newElem,n);
|
||||
}
|
||||
|
||||
|
||||
void ArraySort(DArray array, ArrayCompareFn comparator)
|
||||
{
|
||||
GS_ASSERT (array)
|
||||
qsort(array->list, (size_t)array->count, (size_t)array->elemsize, comparator);
|
||||
GS_ASSERT (array)
|
||||
qsort(array->list, (size_t)array->count, (size_t)array->elemsize, comparator);
|
||||
}
|
||||
|
||||
//GS_ASSERT will be raised by ArrayNth if fromindex out of range
|
||||
int ArraySearch(DArray array, const void *key, ArrayCompareFn comparator,
|
||||
int fromIndex, int isSorted)
|
||||
{
|
||||
void *res;
|
||||
int found = 1;
|
||||
if (!array || array->count == 0)
|
||||
return NOT_FOUND;
|
||||
void *res;
|
||||
int found = 1;
|
||||
if (!array || array->count == 0)
|
||||
return NOT_FOUND;
|
||||
|
||||
if (isSorted)
|
||||
res=mybsearch(key, ArrayNth(array,fromIndex),
|
||||
array->count - fromIndex, array->elemsize, comparator, &found);
|
||||
else
|
||||
res=mylsearch(key, ArrayNth(array, fromIndex),
|
||||
array->count - fromIndex, array->elemsize, comparator);
|
||||
if (res != NULL && found)
|
||||
return (((char *)res - (char *)array->list) / array->elemsize);
|
||||
else
|
||||
return NOT_FOUND;
|
||||
if (isSorted)
|
||||
res=mybsearch(key, ArrayNth(array,fromIndex),
|
||||
array->count - fromIndex, array->elemsize, comparator, &found);
|
||||
else
|
||||
res=mylsearch(key, ArrayNth(array, fromIndex),
|
||||
array->count - fromIndex, array->elemsize, comparator);
|
||||
if (res != NULL && found)
|
||||
return (((char *)res - (char *)array->list) / array->elemsize);
|
||||
else
|
||||
return NOT_FOUND;
|
||||
}
|
||||
|
||||
|
||||
void ArrayMap(DArray array, ArrayMapFn fn, void *clientData)
|
||||
{
|
||||
int i;
|
||||
int i;
|
||||
|
||||
GS_ASSERT (array)
|
||||
GS_ASSERT(fn);
|
||||
GS_ASSERT (array)
|
||||
GS_ASSERT(fn);
|
||||
|
||||
for (i = 0; i < array->count; i++)
|
||||
fn(ArrayNth(array,i), clientData);
|
||||
for (i = 0; i < array->count; i++)
|
||||
fn(ArrayNth(array,i), clientData);
|
||||
|
||||
}
|
||||
|
||||
void ArrayMapBackwards(DArray array, ArrayMapFn fn, void *clientData)
|
||||
{
|
||||
int i;
|
||||
int i;
|
||||
|
||||
GS_ASSERT(fn);
|
||||
GS_ASSERT(fn);
|
||||
|
||||
for (i = (array->count - 1) ; i >= 0 ; i--)
|
||||
fn(ArrayNth(array,i), clientData);
|
||||
for (i = (array->count - 1) ; i >= 0 ; i--)
|
||||
fn(ArrayNth(array,i), clientData);
|
||||
|
||||
}
|
||||
|
||||
void * ArrayMap2(DArray array, ArrayMapFn2 fn, void *clientData)
|
||||
{
|
||||
int i;
|
||||
void * pcurr;
|
||||
int i;
|
||||
void * pcurr;
|
||||
|
||||
GS_ASSERT(fn);
|
||||
GS_ASSERT(clientData);
|
||||
GS_ASSERT(fn);
|
||||
GS_ASSERT(clientData);
|
||||
|
||||
for (i = 0; i < array->count; i++)
|
||||
{
|
||||
pcurr = ArrayNth(array,i);
|
||||
if(!fn(pcurr, clientData))
|
||||
return pcurr;
|
||||
}
|
||||
for (i = 0; i < array->count; i++)
|
||||
{
|
||||
pcurr = ArrayNth(array,i);
|
||||
if(!fn(pcurr, clientData))
|
||||
return pcurr;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void * ArrayMapBackwards2(DArray array, ArrayMapFn2 fn, void *clientData)
|
||||
{
|
||||
int i;
|
||||
void * pcurr;
|
||||
int i;
|
||||
void * pcurr;
|
||||
|
||||
GS_ASSERT(fn);
|
||||
GS_ASSERT(clientData);
|
||||
GS_ASSERT(fn);
|
||||
GS_ASSERT(clientData);
|
||||
|
||||
for (i = (array->count - 1) ; i >= 0 ; i--)
|
||||
{
|
||||
pcurr = ArrayNth(array,i);
|
||||
if(!fn(pcurr, clientData))
|
||||
return pcurr;
|
||||
}
|
||||
for (i = (array->count - 1) ; i >= 0 ; i--)
|
||||
{
|
||||
pcurr = ArrayNth(array,i);
|
||||
if(!fn(pcurr, clientData))
|
||||
return pcurr;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void ArrayClear(DArray array)
|
||||
{
|
||||
int i;
|
||||
int i;
|
||||
|
||||
// This could be more optimal!
|
||||
//////////////////////////////
|
||||
for(i = (ArrayLength(array) - 1) ; i >= 0 ; i--)
|
||||
ArrayDeleteAt(array, i);
|
||||
// This could be more optimal!
|
||||
//////////////////////////////
|
||||
for(i = (ArrayLength(array) - 1) ; i >= 0 ; i--)
|
||||
ArrayDeleteAt(array, i);
|
||||
}
|
||||
|
||||
/* mylsearch
|
||||
|
@ -333,17 +333,17 @@ void ArrayClear(DArray array)
|
|||
* couldn't use lfind
|
||||
*/
|
||||
static void *mylsearch(const void *key, void *base, int count, int size,
|
||||
ArrayCompareFn comparator)
|
||||
ArrayCompareFn comparator)
|
||||
{
|
||||
int i;
|
||||
GS_ASSERT(key);
|
||||
GS_ASSERT(base);
|
||||
for (i = 0; i < count; i++)
|
||||
{
|
||||
if (comparator(key, (char *)base + size*i) == 0)
|
||||
return (char *)base + size*i;
|
||||
}
|
||||
return NULL;
|
||||
int i;
|
||||
GS_ASSERT(key);
|
||||
GS_ASSERT(base);
|
||||
for (i = 0; i < count; i++)
|
||||
{
|
||||
if (comparator(key, (char *)base + size*i) == 0)
|
||||
return (char *)base + size*i;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* mybsearch
|
||||
|
@ -351,27 +351,27 @@ static void *mylsearch(const void *key, void *base, int count, int size,
|
|||
*/
|
||||
static void *mybsearch(const void *elem, void *base, int num, int elemsize, ArrayCompareFn comparator, int *found)
|
||||
{
|
||||
int L, H, I, C;
|
||||
int L, H, I, C;
|
||||
|
||||
GS_ASSERT(elem);
|
||||
GS_ASSERT(base);
|
||||
GS_ASSERT(found);
|
||||
GS_ASSERT(elem);
|
||||
GS_ASSERT(base);
|
||||
GS_ASSERT(found);
|
||||
|
||||
L = 0;
|
||||
H = num - 1;
|
||||
*found = 0;
|
||||
while (L <= H)
|
||||
{
|
||||
I = (L + H) >> 1;
|
||||
C = comparator(((char *)base) + I * elemsize,elem);
|
||||
if (C == 0)
|
||||
*found = 1;
|
||||
if (C < 0)
|
||||
L = I + 1;
|
||||
else
|
||||
{
|
||||
H = I - 1;
|
||||
}
|
||||
}
|
||||
return ((char *)base) + L * elemsize;
|
||||
L = 0;
|
||||
H = num - 1;
|
||||
*found = 0;
|
||||
while (L <= H)
|
||||
{
|
||||
I = (L + H) >> 1;
|
||||
C = comparator(((char *)base) + I * elemsize,elem);
|
||||
if (C == 0)
|
||||
*found = 1;
|
||||
if (C < 0)
|
||||
L = I + 1;
|
||||
else
|
||||
{
|
||||
H = I - 1;
|
||||
}
|
||||
}
|
||||
return ((char *)base) + L * elemsize;
|
||||
}
|
||||
|
|
|
@ -43,9 +43,9 @@ typedef struct DArrayImplementation *DArray;
|
|||
* If the two elements are "equal", return 0.
|
||||
*/
|
||||
#if defined(WIN32)
|
||||
typedef int (__cdecl *ArrayCompareFn)(const void *elem1, const void *elem2);
|
||||
typedef int (__cdecl *ArrayCompareFn)(const void *elem1, const void *elem2);
|
||||
#else
|
||||
typedef int (*ArrayCompareFn)(const void *elem1, const void *elem2);
|
||||
typedef int (*ArrayCompareFn)(const void *elem1, const void *elem2);
|
||||
#endif
|
||||
|
||||
|
||||
|
|
|
@ -188,3 +188,36 @@ void crypt_docrypt(GCryptInfo *info, unsigned char *out, int len)
|
|||
info->wordPtr++;
|
||||
}
|
||||
}
|
||||
|
||||
int enctype2_encoder(unsigned char *secKey, unsigned char *data, int size)
|
||||
{
|
||||
GCryptInfo info = {0};
|
||||
int i;
|
||||
int header_size = 8;
|
||||
|
||||
// move the data and put it after the header
|
||||
for (i = size - 1; i >= 0; i--) {
|
||||
data[1 + header_size + i] = data[i];
|
||||
}
|
||||
|
||||
// initialize the header
|
||||
for (i = 0; i < header_size; i++) {
|
||||
data[i + 1] = 0;
|
||||
}
|
||||
|
||||
// initialize the crypt key
|
||||
init_crypt_key(data + 1, header_size, &info);
|
||||
|
||||
for (i = 0; i < 6; i++) {
|
||||
data[1 + header_size + size + i] = 0;
|
||||
}
|
||||
crypt_docrypt(&info, data + 1 + header_size, size + 6);
|
||||
|
||||
for (i = 0; secKey[i]; i++) {
|
||||
data[i + 1] ^= secKey[i];
|
||||
}
|
||||
|
||||
size += 1 + header_size + 6;
|
||||
*data = header_size ^ 0xec;
|
||||
return size;
|
||||
}
|
||||
|
|
|
@ -13,17 +13,17 @@ Fax(714)549-0757
|
|||
******
|
||||
|
||||
Updated 10-15-99 (BGW)
|
||||
Modified ServerParseKeyVals to actually parse and store empty
|
||||
values for keys (i.e. "\delete\\" adds key="delete" and value="")
|
||||
Modified ServerParseKeyVals to actually parse and store empty
|
||||
values for keys (i.e. "\delete\\" adds key="delete" and value="")
|
||||
Updated 6-17-99 (DDW)
|
||||
Added new tokenize function to handle empty values for keys
|
||||
Added new tokenize function to handle empty values for keys
|
||||
|
||||
|
||||
*******/
|
||||
#if defined(applec) || defined(THINK_C) || defined(__MWERKS__) && !defined(__KATANA__)
|
||||
#include "::nonport.h"
|
||||
#include "::nonport.h"
|
||||
#else
|
||||
#include "../nonport.h"
|
||||
#include "../nonport.h"
|
||||
#endif
|
||||
#include "goaceng.h"
|
||||
#include "gserver.h"
|
||||
|
@ -43,11 +43,11 @@ static char *LookupKey(GServer server, char *k);
|
|||
|
||||
void ServerFree(void *elem)
|
||||
{
|
||||
//free a server!
|
||||
GServer server = *(GServer *)elem;
|
||||
//free a server!
|
||||
GServer server = *(GServer *)elem;
|
||||
|
||||
TableFree(server->keyvals);
|
||||
free(server);
|
||||
TableFree(server->keyvals);
|
||||
free(server);
|
||||
}
|
||||
|
||||
static void ServerSetAddressFromString (GServer server, char *address)
|
||||
|
@ -106,107 +106,107 @@ GServer ServerNewData(char **fieldlist, int fieldcount, char *serverdata, GQuery
|
|||
|
||||
GServer ServerNew(unsigned long ip, unsigned short port, GQueryType qtype, HashTable keylist)
|
||||
{
|
||||
GServer server;
|
||||
int nBuckets, nChains;
|
||||
GServer server;
|
||||
int nBuckets, nChains;
|
||||
|
||||
server = malloc(sizeof(struct GServerImplementation));
|
||||
server->ip = ip;
|
||||
server->port = port;
|
||||
server->ping = 9999;
|
||||
server->querytype = qtype;
|
||||
/* optimize the number of buckets / chains based on query type */
|
||||
switch (qtype)
|
||||
{
|
||||
case qt_basic:
|
||||
nBuckets = 4;
|
||||
nChains = 2;
|
||||
break;
|
||||
case qt_info:
|
||||
nBuckets = 6;
|
||||
nChains = 2;
|
||||
break;
|
||||
case qt_players:
|
||||
case qt_rules:
|
||||
nBuckets = 8;
|
||||
nChains = 2;
|
||||
break;
|
||||
case qt_info_rules:
|
||||
case qt_status:
|
||||
default:
|
||||
nBuckets = 8;
|
||||
nChains= 4;
|
||||
break;
|
||||
}
|
||||
server->keyvals = TableNew2(sizeof(GKeyValuePair),nBuckets,nChains, KeyValHashKeyP, KeyValCompareKeyP, NULL);
|
||||
server->keylist = keylist;
|
||||
return server;
|
||||
server = malloc(sizeof(struct GServerImplementation));
|
||||
server->ip = ip;
|
||||
server->port = port;
|
||||
server->ping = 9999;
|
||||
server->querytype = qtype;
|
||||
/* optimize the number of buckets / chains based on query type */
|
||||
switch (qtype)
|
||||
{
|
||||
case qt_basic:
|
||||
nBuckets = 4;
|
||||
nChains = 2;
|
||||
break;
|
||||
case qt_info:
|
||||
nBuckets = 6;
|
||||
nChains = 2;
|
||||
break;
|
||||
case qt_players:
|
||||
case qt_rules:
|
||||
nBuckets = 8;
|
||||
nChains = 2;
|
||||
break;
|
||||
case qt_info_rules:
|
||||
case qt_status:
|
||||
default:
|
||||
nBuckets = 8;
|
||||
nChains= 4;
|
||||
break;
|
||||
}
|
||||
server->keyvals = TableNew2(sizeof(GKeyValuePair),nBuckets,nChains, KeyValHashKeyP, KeyValCompareKeyP, NULL);
|
||||
server->keylist = keylist;
|
||||
return server;
|
||||
}
|
||||
|
||||
|
||||
static char *mytok(char *instr, char delim)
|
||||
{
|
||||
char *result;
|
||||
static char *thestr;
|
||||
char *result;
|
||||
static char *thestr;
|
||||
|
||||
if (instr)
|
||||
thestr = instr;
|
||||
result=thestr;
|
||||
while (*thestr && *thestr != delim)
|
||||
{
|
||||
thestr++;
|
||||
}
|
||||
if (thestr == result)
|
||||
result = NULL;
|
||||
if (*thestr) //not the null term
|
||||
*thestr++ = '\0';
|
||||
return result;
|
||||
if (instr)
|
||||
thestr = instr;
|
||||
result=thestr;
|
||||
while (*thestr && *thestr != delim)
|
||||
{
|
||||
thestr++;
|
||||
}
|
||||
if (thestr == result)
|
||||
result = NULL;
|
||||
if (*thestr) //not the null term
|
||||
*thestr++ = '\0';
|
||||
return result;
|
||||
}
|
||||
|
||||
static int CheckValidKey(char *key)
|
||||
{
|
||||
const char *InvalidKeys[]={"queryid","final"};
|
||||
int i;
|
||||
for (i = 0; i < sizeof(InvalidKeys)/sizeof(InvalidKeys[0]); i++)
|
||||
{
|
||||
if (strcmp(key,InvalidKeys[i]) == 0)
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
const char *InvalidKeys[]={"queryid","final"};
|
||||
int i;
|
||||
for (i = 0; i < sizeof(InvalidKeys)/sizeof(InvalidKeys[0]); i++)
|
||||
{
|
||||
if (strcmp(key,InvalidKeys[i]) == 0)
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static char *LookupKey(GServer server, char *k)
|
||||
{
|
||||
char **keyindex;
|
||||
char **keyindex;
|
||||
|
||||
keyindex = (char **)TableLookup(server->keylist,&k);
|
||||
if (keyindex != NULL)
|
||||
return *keyindex;
|
||||
k = strdup(k);
|
||||
TableEnter(server->keylist,&k);
|
||||
return k;
|
||||
keyindex = (char **)TableLookup(server->keylist,&k);
|
||||
if (keyindex != NULL)
|
||||
return *keyindex;
|
||||
k = strdup(k);
|
||||
TableEnter(server->keylist,&k);
|
||||
return k;
|
||||
}
|
||||
|
||||
void ServerParseKeyVals(GServer server, char *keyvals)
|
||||
{
|
||||
char *k, *v;
|
||||
GKeyValuePair kvpair;
|
||||
char *k, *v;
|
||||
GKeyValuePair kvpair;
|
||||
|
||||
k = mytok(++keyvals,'\\'); //skip over starting backslash
|
||||
while (k != NULL)
|
||||
{
|
||||
v = mytok(NULL,'\\');
|
||||
if (v == NULL)
|
||||
v = "";
|
||||
if (CheckValidKey(k))
|
||||
{
|
||||
kvpair.key = LookupKey(server, k);
|
||||
kvpair.value = LookupKey(server, v);
|
||||
TableEnter(server->keyvals, &kvpair);
|
||||
}
|
||||
k = mytok(NULL,'\\');
|
||||
k = mytok(++keyvals,'\\'); //skip over starting backslash
|
||||
while (k != NULL)
|
||||
{
|
||||
v = mytok(NULL,'\\');
|
||||
if (v == NULL)
|
||||
v = "";
|
||||
if (CheckValidKey(k))
|
||||
{
|
||||
kvpair.key = LookupKey(server, k);
|
||||
kvpair.value = LookupKey(server, v);
|
||||
TableEnter(server->keyvals, &kvpair);
|
||||
}
|
||||
k = mytok(NULL,'\\');
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -215,7 +215,7 @@ void ServerParseKeyVals(GServer server, char *keyvals)
|
|||
Returns the ping for the specified server. */
|
||||
int ServerGetPing(GServer server)
|
||||
{
|
||||
return server->ping;
|
||||
return server->ping;
|
||||
}
|
||||
|
||||
/* ServerGetAddress
|
||||
|
@ -223,7 +223,7 @@ int ServerGetPing(GServer server)
|
|||
Returns the string, dotted IP address for the specified server */
|
||||
char *ServerGetAddress(GServer server)
|
||||
{
|
||||
return (char *)inet_ntoa(*(struct in_addr*)&server->ip);
|
||||
return (char *)inet_ntoa(*(struct in_addr*)&server->ip);
|
||||
}
|
||||
|
||||
/* ServerGetInetAddress
|
||||
|
@ -231,7 +231,7 @@ char *ServerGetAddress(GServer server)
|
|||
Returns the IP address for the specified server */
|
||||
unsigned int ServerGetInetAddress(GServer server)
|
||||
{
|
||||
return server->ip;
|
||||
return server->ip;
|
||||
}
|
||||
|
||||
|
||||
|
@ -240,18 +240,18 @@ unsigned int ServerGetInetAddress(GServer server)
|
|||
Returns the "query" port for the specified server. */
|
||||
int ServerGetQueryPort(GServer server)
|
||||
{
|
||||
return server->port;
|
||||
return server->port;
|
||||
}
|
||||
|
||||
static GKeyValuePair *ServerRuleLookup(GServer server, char *key)
|
||||
{
|
||||
GKeyValuePair kvp;
|
||||
char **keyindex;
|
||||
keyindex = (char **)TableLookup(server->keylist, &key);
|
||||
if (keyindex == NULL)
|
||||
return NULL; //otherwise, the keyindex->keyindex is valid, so use it to lookup
|
||||
kvp.key = *keyindex;
|
||||
return (GKeyValuePair *)TableLookup(server->keyvals, &kvp);
|
||||
GKeyValuePair kvp;
|
||||
char **keyindex;
|
||||
keyindex = (char **)TableLookup(server->keylist, &key);
|
||||
if (keyindex == NULL)
|
||||
return NULL; //otherwise, the keyindex->keyindex is valid, so use it to lookup
|
||||
kvp.key = *keyindex;
|
||||
return (GKeyValuePair *)TableLookup(server->keyvals, &kvp);
|
||||
}
|
||||
|
||||
/* ServerGet[]Value
|
||||
|
@ -259,60 +259,60 @@ static GKeyValuePair *ServerRuleLookup(GServer server, char *key)
|
|||
Returns the value for the specified key. */
|
||||
char *ServerGetStringValue(GServer server, char *key, char *sdefault)
|
||||
{
|
||||
GKeyValuePair *kv;
|
||||
GKeyValuePair *kv;
|
||||
|
||||
if (strcmp(key,"hostaddr") == 0) //ooh! they want the hostaddr!
|
||||
return ServerGetAddress(server);
|
||||
kv = ServerRuleLookup(server,key);
|
||||
if (!kv)
|
||||
return sdefault;
|
||||
return kv->value;
|
||||
if (strcmp(key,"hostaddr") == 0) //ooh! they want the hostaddr!
|
||||
return ServerGetAddress(server);
|
||||
kv = ServerRuleLookup(server,key);
|
||||
if (!kv)
|
||||
return sdefault;
|
||||
return kv->value;
|
||||
}
|
||||
|
||||
int ServerGetIntValue(GServer server, char *key, int idefault)
|
||||
{
|
||||
GKeyValuePair *kv;
|
||||
GKeyValuePair *kv;
|
||||
|
||||
if (strcmp(key,"ping") == 0) //ooh! they want the ping!
|
||||
return ServerGetPing(server);
|
||||
kv = ServerRuleLookup(server,key);
|
||||
if (!kv)
|
||||
return idefault;
|
||||
return atoi(kv->value);
|
||||
if (strcmp(key,"ping") == 0) //ooh! they want the ping!
|
||||
return ServerGetPing(server);
|
||||
kv = ServerRuleLookup(server,key);
|
||||
if (!kv)
|
||||
return idefault;
|
||||
return atoi(kv->value);
|
||||
|
||||
}
|
||||
|
||||
double ServerGetFloatValue(GServer server, char *key, double fdefault)
|
||||
{
|
||||
GKeyValuePair *kv;
|
||||
GKeyValuePair *kv;
|
||||
|
||||
kv = ServerRuleLookup(server,key);
|
||||
if (!kv)
|
||||
return fdefault;
|
||||
return atof(kv->value);
|
||||
kv = ServerRuleLookup(server,key);
|
||||
if (!kv)
|
||||
return fdefault;
|
||||
return atof(kv->value);
|
||||
}
|
||||
|
||||
char *ServerGetPlayerStringValue(GServer server, int playernum, char *key, char *sdefault)
|
||||
{
|
||||
char newkey[32];
|
||||
char newkey[32];
|
||||
|
||||
sprintf(newkey,"%s_%d",key,playernum);
|
||||
return ServerGetStringValue(server, newkey, sdefault);
|
||||
sprintf(newkey,"%s_%d",key,playernum);
|
||||
return ServerGetStringValue(server, newkey, sdefault);
|
||||
}
|
||||
int ServerGetPlayerIntValue(GServer server, int playernum, char *key, int idefault)
|
||||
{
|
||||
char newkey[32];
|
||||
char newkey[32];
|
||||
|
||||
sprintf(newkey,"%s_%d",key,playernum);
|
||||
return ServerGetIntValue(server, newkey, idefault);
|
||||
sprintf(newkey,"%s_%d",key,playernum);
|
||||
return ServerGetIntValue(server, newkey, idefault);
|
||||
|
||||
}
|
||||
double ServerGetPlayerFloatValue(GServer server, int playernum, char *key, double fdefault)
|
||||
{
|
||||
char newkey[32];
|
||||
char newkey[32];
|
||||
|
||||
sprintf(newkey,"%s_%d",key,playernum);
|
||||
return ServerGetFloatValue(server, newkey, fdefault);
|
||||
sprintf(newkey,"%s_%d",key,playernum);
|
||||
return ServerGetFloatValue(server, newkey, fdefault);
|
||||
|
||||
}
|
||||
|
||||
|
@ -324,20 +324,20 @@ key/value. The user-defined instance data will be passed to the KeyFn callback *
|
|||
|
||||
static void KeyMapF(void *elem, void *clientData)
|
||||
{
|
||||
GKeyValuePair *kv = (GKeyValuePair *)elem;
|
||||
GEnumData *ped = (GEnumData *)clientData;
|
||||
ped->EnumFn(kv->key, kv->value, ped->instance);
|
||||
GKeyValuePair *kv = (GKeyValuePair *)elem;
|
||||
GEnumData *ped = (GEnumData *)clientData;
|
||||
ped->EnumFn(kv->key, kv->value, ped->instance);
|
||||
}
|
||||
|
||||
|
||||
void ServerEnumKeys(GServer server, KeyEnumFn KeyFn, void *instance)
|
||||
{
|
||||
GEnumData ed;
|
||||
GEnumData ed;
|
||||
|
||||
ed.EnumFn = KeyFn;
|
||||
ed.instance = instance;
|
||||
ed.keylist = server->keylist;
|
||||
TableMap(server->keyvals, KeyMapF, &ed);
|
||||
ed.EnumFn = KeyFn;
|
||||
ed.instance = instance;
|
||||
ed.keylist = server->keylist;
|
||||
TableMap(server->keyvals, KeyMapF, &ed);
|
||||
}
|
||||
|
||||
|
||||
|
@ -349,21 +349,21 @@ void ServerEnumKeys(GServer server, KeyEnumFn KeyFn, void *instance)
|
|||
#define MULTIPLIER -1664117991
|
||||
static int StringHash(char *s, int numbuckets)
|
||||
{
|
||||
unsigned long hashcode = 0;
|
||||
while (*s != 0)
|
||||
hashcode = hashcode * MULTIPLIER + tolower(*s++);
|
||||
unsigned long hashcode = 0;
|
||||
while (*s != 0)
|
||||
hashcode = hashcode * MULTIPLIER + tolower(*s++);
|
||||
return (hashcode % numbuckets);
|
||||
|
||||
}
|
||||
|
||||
int GStringHash(const void *elem, int numbuckets)
|
||||
{
|
||||
return StringHash(*(char **)elem, numbuckets);
|
||||
return StringHash(*(char **)elem, numbuckets);
|
||||
}
|
||||
|
||||
static int KeyValHashKeyP(const void *elem, int numbuckets)
|
||||
{
|
||||
return StringHash(((GKeyValuePair *)elem)->key, numbuckets);
|
||||
return StringHash(((GKeyValuePair *)elem)->key, numbuckets);
|
||||
}
|
||||
|
||||
|
||||
|
@ -384,12 +384,12 @@ int GCaseInsensitiveCompare(const void *entry1, const void *entry2)
|
|||
*/
|
||||
static int KeyValCompareKeyP(const void *entry1, const void *entry2)
|
||||
{
|
||||
return ((GKeyValuePair *)entry1)->key - ((GKeyValuePair *)entry2)->key;
|
||||
return ((GKeyValuePair *)entry1)->key - ((GKeyValuePair *)entry2)->key;
|
||||
}
|
||||
|
||||
void GStringFree(void *elem)
|
||||
{
|
||||
free(*(char **)elem);
|
||||
free(*(char **)elem);
|
||||
}
|
||||
|
||||
/* keyval
|
||||
|
@ -397,8 +397,8 @@ void GStringFree(void *elem)
|
|||
*
|
||||
static int KeyValCompareKeyA(const void *entry1, const void *entry2)
|
||||
{
|
||||
return CaseInsensitiveCompare(&((GKeyValuePair *)entry1)->key,
|
||||
&((GKeyValuePair *)entry2)->key);
|
||||
return CaseInsensitiveCompare(&((GKeyValuePair *)entry1)->key,
|
||||
&((GKeyValuePair *)entry2)->key);
|
||||
}
|
||||
|
||||
*/
|
|
@ -27,38 +27,38 @@ extern "C" {
|
|||
#endif
|
||||
|
||||
#if defined(applec) || defined(THINK_C) || defined(__MWERKS__) && !defined(__KATANA__)
|
||||
#include "::hashtable.h"
|
||||
#include "::hashtable.h"
|
||||
#else
|
||||
#include "../hashtable.h"
|
||||
#include "../hashtable.h"
|
||||
#endif
|
||||
|
||||
struct GServerImplementation
|
||||
{
|
||||
unsigned long ip;
|
||||
unsigned short port;
|
||||
short ping;
|
||||
GQueryType querytype;
|
||||
HashTable keyvals;
|
||||
HashTable keylist;
|
||||
unsigned long ip;
|
||||
unsigned short port;
|
||||
short ping;
|
||||
GQueryType querytype;
|
||||
HashTable keyvals;
|
||||
HashTable keylist;
|
||||
|
||||
};
|
||||
|
||||
typedef struct
|
||||
{
|
||||
char *key;
|
||||
char *value;
|
||||
char *key;
|
||||
char *value;
|
||||
} GKeyValuePair;
|
||||
/*
|
||||
typedef struct
|
||||
{
|
||||
char *key, *value;
|
||||
char *key, *value;
|
||||
} GKeyValuePair;
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
KeyEnumFn EnumFn;
|
||||
void *instance;
|
||||
HashTable keylist;
|
||||
KeyEnumFn EnumFn;
|
||||
void *instance;
|
||||
HashTable keylist;
|
||||
} GEnumData;
|
||||
|
||||
void ServerFree(void *elem);
|
||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -18,68 +18,68 @@ typedef unsigned char uchar;
|
|||
|
||||
static void swap_byte(uchar* a, uchar* b)
|
||||
{
|
||||
uchar swapByte;
|
||||
uchar swapByte;
|
||||
|
||||
swapByte = *a;
|
||||
*a = *b;
|
||||
*b = swapByte;
|
||||
swapByte = *a;
|
||||
*a = *b;
|
||||
*b = swapByte;
|
||||
}
|
||||
|
||||
static uchar encode_ct(uchar c)
|
||||
{
|
||||
if (c < 26) return (uchar)('A' + c);
|
||||
if (c < 52) return (uchar)('a' + c - 26);
|
||||
if (c < 62) return (uchar)('0' + c - 52);
|
||||
if (c == 62) return (uchar)('+');
|
||||
if (c == 63) return (uchar)('/');
|
||||
if (c < 26) return (uchar)('A' + c);
|
||||
if (c < 52) return (uchar)('a' + c - 26);
|
||||
if (c < 62) return (uchar)('0' + c - 52);
|
||||
if (c == 62) return (uchar)('+');
|
||||
if (c == 63) return (uchar)('/');
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void gs_encode(uchar* ins, int size, uchar* result)
|
||||
{
|
||||
int i, pos;
|
||||
uchar trip[3];
|
||||
uchar kwart[4];
|
||||
int i, pos;
|
||||
uchar trip[3];
|
||||
uchar kwart[4];
|
||||
|
||||
i = 0;
|
||||
while (i < size)
|
||||
{
|
||||
for (pos = 0; pos <= 2; pos++, i++)
|
||||
if (i < size) trip[pos] = *ins++;
|
||||
else trip[pos] = '\0';
|
||||
kwart[0] = (unsigned char)((trip[0]) >> 2);
|
||||
kwart[1] = (unsigned char)((((trip[0]) & 3) << 4) + ((trip[1]) >> 4));
|
||||
kwart[2] = (unsigned char)((((trip[1]) & 15) << 2) + ((trip[2]) >> 6));
|
||||
kwart[3] = (unsigned char)((trip[2]) & 63);
|
||||
for (pos = 0; pos <= 3; pos++) *result++ = encode_ct(kwart[pos]);
|
||||
}
|
||||
*result = '\0';
|
||||
i = 0;
|
||||
while (i < size)
|
||||
{
|
||||
for (pos = 0; pos <= 2; pos++, i++)
|
||||
if (i < size) trip[pos] = *ins++;
|
||||
else trip[pos] = '\0';
|
||||
kwart[0] = (unsigned char)((trip[0]) >> 2);
|
||||
kwart[1] = (unsigned char)((((trip[0]) & 3) << 4) + ((trip[1]) >> 4));
|
||||
kwart[2] = (unsigned char)((((trip[1]) & 15) << 2) + ((trip[2]) >> 6));
|
||||
kwart[3] = (unsigned char)((trip[2]) & 63);
|
||||
for (pos = 0; pos <= 3; pos++) *result++ = encode_ct(kwart[pos]);
|
||||
}
|
||||
*result = '\0';
|
||||
}
|
||||
|
||||
void gs_encrypt(uchar* key, int key_len, uchar* buffer_ptr, int buffer_len)
|
||||
{
|
||||
int counter;
|
||||
uchar x, y, xorIndex;
|
||||
uchar state[256];
|
||||
int counter;
|
||||
uchar x, y, xorIndex;
|
||||
uchar state[256];
|
||||
|
||||
for (counter = 0; counter < 256; counter++) state[counter] = (uchar)counter;
|
||||
for (counter = 0; counter < 256; counter++) state[counter] = (uchar)counter;
|
||||
|
||||
x = 0; y = 0;
|
||||
for (counter = 0; counter < 256; counter++)
|
||||
{
|
||||
y = (uchar)((key[x] + state[counter] + y) % 256);
|
||||
x = (uchar)((x + 1) % key_len);
|
||||
swap_byte(&state[counter], &state[y]);
|
||||
}
|
||||
x = 0; y = 0;
|
||||
for (counter = 0; counter < 256; counter++)
|
||||
{
|
||||
y = (uchar)((key[x] + state[counter] + y) % 256);
|
||||
x = (uchar)((x + 1) % key_len);
|
||||
swap_byte(&state[counter], &state[y]);
|
||||
}
|
||||
|
||||
x = 0; y = 0;
|
||||
for (counter = 0; counter < buffer_len; counter++)
|
||||
{
|
||||
x = (uchar)((x + buffer_ptr[counter] + 1) % 256);
|
||||
y = (uchar)((state[x] + y) % 256);
|
||||
swap_byte(&state[x], &state[y]);
|
||||
xorIndex = (uchar)((state[x] + state[y]) % 256);
|
||||
buffer_ptr[counter] ^= state[xorIndex];
|
||||
}
|
||||
x = 0; y = 0;
|
||||
for (counter = 0; counter < buffer_len; counter++)
|
||||
{
|
||||
x = (uchar)((x + buffer_ptr[counter] + 1) % 256);
|
||||
y = (uchar)((state[x] + y) % 256);
|
||||
swap_byte(&state[x], &state[y]);
|
||||
xorIndex = (uchar)((state[x] + state[y]) % 256);
|
||||
buffer_ptr[counter] ^= state[xorIndex];
|
||||
}
|
||||
}
|
||||
|
|
|
@ -27,7 +27,7 @@ size: size of the buffer
|
|||
OUT
|
||||
---
|
||||
result: pointer to buffer to store result. Size should be (4 * size) / 3 + 1
|
||||
result will be null terminated.
|
||||
result will be null terminated.
|
||||
**********/
|
||||
void gs_encode(uchar* ins, int size, uchar* result);
|
||||
|
||||
|
|
|
@ -22,208 +22,208 @@
|
|||
|
||||
|
||||
#ifdef _NO_NOPORT_H_
|
||||
#define gsimalloc malloc
|
||||
#define gsifree free
|
||||
#define gsirealloc realloc
|
||||
#include <assert.h>
|
||||
#define gsimalloc malloc
|
||||
#define gsifree free
|
||||
#define gsirealloc realloc
|
||||
#include <assert.h>
|
||||
#else
|
||||
#include "nonport.h" //for gsimalloc/realloc/free/assert
|
||||
#include "nonport.h" //for gsimalloc/realloc/free/assert
|
||||
#endif
|
||||
|
||||
|
||||
struct HashImplementation
|
||||
{
|
||||
DArray *buckets;
|
||||
int nbuckets;
|
||||
TableElementFreeFn freefn;
|
||||
TableHashFn hashfn;
|
||||
TableCompareFn compfn;
|
||||
DArray *buckets;
|
||||
int nbuckets;
|
||||
TableElementFreeFn freefn;
|
||||
TableHashFn hashfn;
|
||||
TableCompareFn compfn;
|
||||
};
|
||||
|
||||
HashTable TableNew(int elemSize, int nBuckets,
|
||||
TableHashFn hashFn, TableCompareFn compFn,
|
||||
TableElementFreeFn freeFn)
|
||||
TableElementFreeFn freeFn)
|
||||
{
|
||||
return TableNew2(elemSize, nBuckets, 4, hashFn, compFn, freeFn);
|
||||
return TableNew2(elemSize, nBuckets, 4, hashFn, compFn, freeFn);
|
||||
}
|
||||
HashTable TableNew2(int elemSize, int nBuckets, int nChains,
|
||||
TableHashFn hashFn, TableCompareFn compFn,
|
||||
TableElementFreeFn freeFn)
|
||||
TableElementFreeFn freeFn)
|
||||
{
|
||||
HashTable table;
|
||||
int i;
|
||||
HashTable table;
|
||||
int i;
|
||||
|
||||
assert(hashFn);
|
||||
assert(compFn);
|
||||
assert(elemSize);
|
||||
assert(nBuckets);
|
||||
assert(hashFn);
|
||||
assert(compFn);
|
||||
assert(elemSize);
|
||||
assert(nBuckets);
|
||||
|
||||
table = (HashTable)gsimalloc(sizeof(struct HashImplementation));
|
||||
assert(table);
|
||||
table = (HashTable)gsimalloc(sizeof(struct HashImplementation));
|
||||
assert(table);
|
||||
|
||||
table->buckets = (DArray *)gsimalloc(nBuckets * sizeof(DArray));
|
||||
assert(table->buckets);
|
||||
for (i = 0; i < nBuckets; i++) //ArrayNew will assert if allocation fails
|
||||
table->buckets[i] = ArrayNew(elemSize, nChains, freeFn);
|
||||
table->nbuckets = nBuckets;
|
||||
table->freefn = freeFn;
|
||||
table->compfn = compFn;
|
||||
table->hashfn = hashFn;
|
||||
table->buckets = (DArray *)gsimalloc(nBuckets * sizeof(DArray));
|
||||
assert(table->buckets);
|
||||
for (i = 0; i < nBuckets; i++) //ArrayNew will assert if allocation fails
|
||||
table->buckets[i] = ArrayNew(elemSize, nChains, freeFn);
|
||||
table->nbuckets = nBuckets;
|
||||
table->freefn = freeFn;
|
||||
table->compfn = compFn;
|
||||
table->hashfn = hashFn;
|
||||
|
||||
return table;
|
||||
return table;
|
||||
}
|
||||
|
||||
|
||||
void TableFree(HashTable table)
|
||||
{
|
||||
int i;
|
||||
int i;
|
||||
|
||||
assert(table);
|
||||
assert(table);
|
||||
|
||||
if (NULL == table )
|
||||
return;
|
||||
if (NULL == table )
|
||||
return;
|
||||
|
||||
for (i = 0 ; i < table->nbuckets ; i++)
|
||||
ArrayFree(table->buckets[i]);
|
||||
gsifree(table->buckets);
|
||||
gsifree(table);
|
||||
for (i = 0 ; i < table->nbuckets ; i++)
|
||||
ArrayFree(table->buckets[i]);
|
||||
gsifree(table->buckets);
|
||||
gsifree(table);
|
||||
}
|
||||
|
||||
|
||||
int TableCount(HashTable table)
|
||||
{
|
||||
int i, count = 0;
|
||||
int i, count = 0;
|
||||
|
||||
assert(table);
|
||||
assert(table);
|
||||
|
||||
if (NULL == table )
|
||||
return count;
|
||||
if (NULL == table )
|
||||
return count;
|
||||
|
||||
for (i = 0 ; i < table->nbuckets ; i++)
|
||||
count += ArrayLength(table->buckets[i]);
|
||||
for (i = 0 ; i < table->nbuckets ; i++)
|
||||
count += ArrayLength(table->buckets[i]);
|
||||
|
||||
return count;
|
||||
return count;
|
||||
}
|
||||
|
||||
|
||||
void TableEnter(HashTable table, const void *newElem)
|
||||
{
|
||||
int hash, itempos;
|
||||
int hash, itempos;
|
||||
|
||||
assert(table);
|
||||
assert(table);
|
||||
|
||||
if (NULL == table )
|
||||
return;
|
||||
if (NULL == table )
|
||||
return;
|
||||
|
||||
hash = table->hashfn(newElem, table->nbuckets);
|
||||
itempos = ArraySearch(table->buckets[hash], newElem, table->compfn, 0,0);
|
||||
if (itempos == NOT_FOUND)
|
||||
ArrayAppend(table->buckets[hash], newElem);
|
||||
else
|
||||
ArrayReplaceAt(table->buckets[hash], newElem, itempos);
|
||||
hash = table->hashfn(newElem, table->nbuckets);
|
||||
itempos = ArraySearch(table->buckets[hash], newElem, table->compfn, 0,0);
|
||||
if (itempos == NOT_FOUND)
|
||||
ArrayAppend(table->buckets[hash], newElem);
|
||||
else
|
||||
ArrayReplaceAt(table->buckets[hash], newElem, itempos);
|
||||
}
|
||||
|
||||
int TableRemove(HashTable table, const void *delElem)
|
||||
{
|
||||
int hash, itempos;
|
||||
int hash, itempos;
|
||||
|
||||
assert(table);
|
||||
assert(table);
|
||||
|
||||
if (NULL == table )
|
||||
return 0;
|
||||
if (NULL == table )
|
||||
return 0;
|
||||
|
||||
hash = table->hashfn(delElem, table->nbuckets);
|
||||
itempos = ArraySearch(table->buckets[hash], delElem, table->compfn, 0,0);
|
||||
if (itempos == NOT_FOUND)
|
||||
return 0;
|
||||
else
|
||||
ArrayDeleteAt(table->buckets[hash], itempos);
|
||||
return 1;
|
||||
hash = table->hashfn(delElem, table->nbuckets);
|
||||
itempos = ArraySearch(table->buckets[hash], delElem, table->compfn, 0,0);
|
||||
if (itempos == NOT_FOUND)
|
||||
return 0;
|
||||
else
|
||||
ArrayDeleteAt(table->buckets[hash], itempos);
|
||||
return 1;
|
||||
}
|
||||
|
||||
void *TableLookup(HashTable table, const void *elemKey)
|
||||
{
|
||||
int hash, itempos;
|
||||
int hash, itempos;
|
||||
|
||||
assert(table);
|
||||
assert(table);
|
||||
|
||||
if (NULL == table )
|
||||
return NULL;
|
||||
if (NULL == table )
|
||||
return NULL;
|
||||
|
||||
hash = table->hashfn(elemKey, table->nbuckets);
|
||||
itempos = ArraySearch(table->buckets[hash], elemKey, table->compfn, 0,
|
||||
0);
|
||||
if (itempos == NOT_FOUND)
|
||||
return NULL;
|
||||
else
|
||||
return ArrayNth(table->buckets[hash], itempos);
|
||||
hash = table->hashfn(elemKey, table->nbuckets);
|
||||
itempos = ArraySearch(table->buckets[hash], elemKey, table->compfn, 0,
|
||||
0);
|
||||
if (itempos == NOT_FOUND)
|
||||
return NULL;
|
||||
else
|
||||
return ArrayNth(table->buckets[hash], itempos);
|
||||
}
|
||||
|
||||
|
||||
void TableMap(HashTable table, TableMapFn fn, void *clientData)
|
||||
{
|
||||
int i;
|
||||
int i;
|
||||
|
||||
assert(table);
|
||||
assert(fn);
|
||||
assert(table);
|
||||
assert(fn);
|
||||
|
||||
if (NULL == table || NULL == fn)
|
||||
return;
|
||||
if (NULL == table || NULL == fn)
|
||||
return;
|
||||
|
||||
for (i = 0 ; i < table->nbuckets ; i++)
|
||||
ArrayMap(table->buckets[i], fn, clientData);
|
||||
for (i = 0 ; i < table->nbuckets ; i++)
|
||||
ArrayMap(table->buckets[i], fn, clientData);
|
||||
|
||||
}
|
||||
|
||||
void TableMapSafe(HashTable table, TableMapFn fn, void *clientData)
|
||||
{
|
||||
int i;
|
||||
int i;
|
||||
|
||||
assert(fn);
|
||||
assert(fn);
|
||||
|
||||
for (i = 0 ; i < table->nbuckets ; i++)
|
||||
ArrayMapBackwards(table->buckets[i], fn, clientData);
|
||||
for (i = 0 ; i < table->nbuckets ; i++)
|
||||
ArrayMapBackwards(table->buckets[i], fn, clientData);
|
||||
|
||||
}
|
||||
|
||||
void * TableMap2(HashTable table, TableMapFn2 fn, void *clientData)
|
||||
{
|
||||
int i;
|
||||
void * pcurr;
|
||||
int i;
|
||||
void * pcurr;
|
||||
|
||||
assert(fn);
|
||||
assert(fn);
|
||||
|
||||
for (i = 0 ; i < table->nbuckets ; i++)
|
||||
{
|
||||
pcurr = ArrayMap2(table->buckets[i], fn, clientData);
|
||||
if(pcurr)
|
||||
return pcurr;
|
||||
}
|
||||
for (i = 0 ; i < table->nbuckets ; i++)
|
||||
{
|
||||
pcurr = ArrayMap2(table->buckets[i], fn, clientData);
|
||||
if(pcurr)
|
||||
return pcurr;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void * TableMapSafe2(HashTable table, TableMapFn2 fn, void *clientData)
|
||||
{
|
||||
int i;
|
||||
void * pcurr;
|
||||
int i;
|
||||
void * pcurr;
|
||||
|
||||
assert(fn);
|
||||
assert(fn);
|
||||
|
||||
for (i = 0 ; i < table->nbuckets ; i++)
|
||||
{
|
||||
pcurr = ArrayMapBackwards2(table->buckets[i], fn, clientData);
|
||||
if(pcurr)
|
||||
return pcurr;
|
||||
}
|
||||
for (i = 0 ; i < table->nbuckets ; i++)
|
||||
{
|
||||
pcurr = ArrayMapBackwards2(table->buckets[i], fn, clientData);
|
||||
if(pcurr)
|
||||
return pcurr;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void TableClear(HashTable table)
|
||||
{
|
||||
int i;
|
||||
int i;
|
||||
|
||||
for (i = 0 ; i < table->nbuckets ; i++)
|
||||
ArrayClear(table->buckets[i]);
|
||||
for (i = 0 ; i < table->nbuckets ; i++)
|
||||
ArrayClear(table->buckets[i]);
|
||||
}
|
||||
|
|
|
@ -126,11 +126,11 @@ extern "C" {
|
|||
|
||||
HashTable TableNew(int elemSize, int nBuckets,
|
||||
TableHashFn hashFn, TableCompareFn compFn,
|
||||
TableElementFreeFn freeFn);
|
||||
TableElementFreeFn freeFn);
|
||||
|
||||
HashTable TableNew2(int elemSize, int nBuckets, int nChains,
|
||||
TableHashFn hashFn, TableCompareFn compFn,
|
||||
TableElementFreeFn freeFn);
|
||||
TableElementFreeFn freeFn);
|
||||
|
||||
|
||||
/* TableFree
|
||||
|
|
|
@ -305,7 +305,7 @@ static void Decode (UINT4 *output, unsigned char *input, unsigned int len)
|
|||
*/
|
||||
static void MD5_memcpy (POINTER output, POINTER input, unsigned int len)
|
||||
{
|
||||
memcpy(output, input, len);
|
||||
memcpy(output, input, len);
|
||||
/* unsigned int i;
|
||||
|
||||
for (i = 0; i < len; i++)
|
||||
|
@ -316,7 +316,7 @@ static void MD5_memcpy (POINTER output, POINTER input, unsigned int len)
|
|||
*/
|
||||
static void MD5_memset (POINTER output, int value, unsigned int len)
|
||||
{
|
||||
memset(output, value, len);
|
||||
memset(output, value, len);
|
||||
/* unsigned int i;
|
||||
|
||||
for (i = 0; i < len; i++)
|
||||
|
@ -327,26 +327,26 @@ static void MD5_memset (POINTER output, int value, unsigned int len)
|
|||
|
||||
void MD5Print (unsigned char digest[16], char output[33])
|
||||
{
|
||||
static const char hex_digits[] = "0123456789abcdef";
|
||||
unsigned int i;
|
||||
static const char hex_digits[] = "0123456789abcdef";
|
||||
unsigned int i;
|
||||
|
||||
for (i = 0; i < 16; i++)
|
||||
{
|
||||
output[i*2 ] = hex_digits[digest[i] / 16];
|
||||
output[i*2+1] = hex_digits[digest[i] % 16];
|
||||
}
|
||||
output[32] = '\0';
|
||||
for (i = 0; i < 16; i++)
|
||||
{
|
||||
output[i*2 ] = hex_digits[digest[i] / 16];
|
||||
output[i*2+1] = hex_digits[digest[i] % 16];
|
||||
}
|
||||
output[32] = '\0';
|
||||
}
|
||||
|
||||
void MD5Digest (unsigned char *input, unsigned int len, char output[33])
|
||||
{
|
||||
MD5_CTX ctx;
|
||||
unsigned char digest[16];
|
||||
MD5_CTX ctx;
|
||||
unsigned char digest[16];
|
||||
|
||||
MD5Init(&ctx);
|
||||
MD5Update(&ctx, input, len);
|
||||
MD5Final(digest, &ctx);
|
||||
MD5Print(digest, output);
|
||||
MD5Init(&ctx);
|
||||
MD5Update(&ctx, input, len);
|
||||
MD5Final(digest, &ctx);
|
||||
MD5Print(digest, output);
|
||||
|
||||
}
|
||||
#ifdef __cplusplus
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue