#if !defined(__STRLIB_H__)
#define __STRLIB_H__

#include "acp_base.h"

#if defined(USE_STDSTRLIB)
#include <string.h>

#define StringCopy          strcpy
#define StringLength        strlen
#define StringCat           strcat
#define StringNCopy         strncpy
#define StringRChar         strrchr
#define StringChar          strchr
#define StringCompare		strcmp
#define StringICompare      stricmp
#define StringNICompare     strnicmp
#define StringNCompare      strncmp
#define StringString        strstr
#define StringUpper         strupr
#define StringLower         strlwr

#if defined(USE_CRYPTNAME)
	#define StringCompareToCryptKey		strcmp
#else
	#define StringCompareToCryptKey		strcmp
#endif

#elif defined(INLINE_STDSTRLIB)

INLINE
const char * __cdecl StringCopy(const char * dst,const char * src)
{	
	const char *savedst=dst;
	while( (*(unsigned char *)dst++=*(unsigned char *)src++) );	
	return(savedst);
}

INLINE
int __cdecl StringCompare(const char * src,const char * dst)
{
	int ret = 0 ;
	
	while( ! (ret = *(unsigned char *)src - *(unsigned char *)dst) && *dst)
		++src, ++dst;
	
	if ( ret < 0 ) ret = -1 ;
	else if ( ret > 0 ) ret = 1 ;
	return( ret );
}

INLINE
int __cdecl StringICompare(const char * dst, const char * src)
{
	int f,l;
	
	do 
	{
		if ( ((f = (unsigned char)(*(dst++))) >= 'A') && (f <= 'Z') )
			f -= ('A' - 'a');
		
		if ( ((l = (unsigned char)(*(src++))) >= 'A') && (l <= 'Z') )
			l -= ('A' - 'a');
	} 
	while ( f && (f == l) );
	
	return(f - l);
}

#if defined(USE_CRYPTNAME)
INLINE
int __cdecl StringCompareToCryptKey(const char * src,const char * dst)
{
	int ret = 0 ;
	long lKey = *(long*)(dst);
	unsigned char *p_ucCryptKey = (unsigned char*) &lKey;
	unsigned char ucSize = (unsigned char)dst [4];
	unsigned char ucIndex;
	unsigned char ucUncoded;

	dst += 5;
	for (ucIndex = 0 ; ucIndex < ucSize ; ucIndex++)
	{
		// uncode dst char
		ucUncoded = (*(unsigned char *)dst++ - p_ucCryptKey[0]) ^ p_ucCryptKey[1];
		p_ucCryptKey[0] += p_ucCryptKey[2];
		p_ucCryptKey[1] += p_ucCryptKey[3];

		// compare
		ret = *(unsigned char *)src++ - ucUncoded;
		if(ret)	break;
	}

	if ( ret < 0 ) ret = -1 ;
	else if ( ret > 0 ) ret = 1 ;
	return( ret );
}
#else
	#define StringCompareToCryptKey		StringCompare
#endif

#else

#include "deflib.h"

#if defined(__cplusplus)
extern "C" {
#endif

char *StringCopy(char *StrDestination,const char *StrSource);
size_t StringLength(const char *String);
char *StringCat(char *StrDestination,const char *StrSource);
char *StringNCopy(char *StrDestination,const char *StrSource,size_t count);
char *StringRChar(const char *String,int c);
char *StringChar(const char *String,int c);
int StringNICompare(const char *String1,const char *String2,size_t count);
int StringNCompare(const char *String1,const char *String2,size_t count);
int StringICompare(const char *String1,const char *String2);
int StringCompare(const char *String1,const char *String2);
char *StringString(const char *string,const char *strCharSet);
char *StringUpper(char *string);
char *StringLower(char *string);

#if defined(USE_CRYPTNAME)
	int StringCompareToCryptKey(const char *String1,const char *String2);
#else
	#define StringCompareToCryptKey		strcmp
#endif
#if defined(__cplusplus)
}
#endif

#endif // USE_STDSTRLIB

#endif // __STRLIB_H__
