|
// Misc.cpp: implementation of the CMisc class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Misc.h"
#include "syslog.h"
#include "assert.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
int CMisc::hex2dec_32(char *shex)
{
int idec,i,mid;
int len;
len = strlen( shex );
if( len > 8 )
return 0;
mid = 0; idec = 0;
for( i=0; i<len; i++ )
{
if( shex[i]>='0'&&shex[i]<='9' )
mid = shex[i]-'0';
else if( shex[i]>='a'&&shex[i]<='f' )
mid = shex[i] -'a' +10;
else if( shex[i]>='A'&&shex[i]<='F' )
mid = shex[i] -'A' +10;
else
return 0;
mid = mid << ((len-i-1)<<2);
idec = idec | mid;
}
return idec;
}
int CMisc::wild_cmp(char *wild,char *string)
{
char *cp = NULL, *mp = NULL;
while ((*string) && (*wild != '*'))
{
if ((*wild != *string) && (*wild != '?'))
{
return 0;
}
wild++;
string++;
}
while (*string)
{
if (*wild == '*')
{
if (!*++wild)
{
return 1;
}
mp = wild;
cp = string+1;
}
else if ((*wild == *string) || (*wild == '?'))
{
wild++;
string++;
}
else
{
wild = mp;
string = cp++;
}
}
while (*wild == '*')
{
wild++;
}
return !*wild;
}
QWORD CMisc::hex2dec_64(char *shex)
{
QWORD idec,mid;
int i,len;
while(shex[0] == '0')
shex ++;
len = strlen( shex );
ASSERT(len <= 16);
mid = 0; idec = 0;
for( i=0;i<len;i++ )
{
if( shex[i]>='0'&&shex[i]<='9' )
mid = shex[i]-'0';
else if( shex[i]>='a'&&shex[i]<='f' )
mid = shex[i] -'a' +10;
else if( shex[i]>='A'&&shex[i]<='F' )
mid = shex[i] -'A' +10;
else
return 0;
mid = mid << ((len-i-1)<<2);
idec = idec | mid;
}
return idec;
}
int CMisc::add_overflow(int a, int b)
{
if(a >= 0 && b >= 0)
{
if(a > MAX_INT - b)
return TRUE;
}
else if( a < 0 && b < 0)
{
if(a < MIN_INT - b)
return TRUE;
}
return FALSE;
}
int CMisc::sub_overflow(int a, int b)
{
if(a >= 0 && b < 0)
{
if( a > MAX_INT + b)
return TRUE;
}
else if(a < 0 && b >= 0)
{
if( a < MIN_INT + b)
return TRUE;
}
return FALSE;
}
int CMisc::add_overflow(DWORD a, DWORD b)
{
DWORD r;
r = a + b;
return (r < a || r < b);
}
int CMisc::sub_overflow(DWORD a, DWORD b)
{
return a < b;
}
int CMisc::multi_overflow(int a, int b)
{
if(a == 0 || b == 0) return FALSE;
if(b == -1)
{
if(a == MIN_INT)
return TRUE;
else
return FALSE;
}
if( a < MIN_INT / b || a > MAX_INT / b)
return TRUE;
return FALSE;
}
int CMisc::multi_overflow(DWORD a, DWORD b)
{
if(b == 0) return FALSE;
if( a > 0xFFFFFFF / b)
return TRUE;
return FALSE;
}
int CMisc::sub_overflow(char a, char b)
{
if(a >= 0 && b < 0)
{
if( a > MAX_CHAR + b)
return TRUE;
}
else if(a < 0 && b >= 0)
{
if( a < MIN_CHAR + b)
return TRUE;
}
return FALSE;
}
int CMisc::sub_overflow(short a, short b)
{
if(a >= 0 && b < 0)
{
if( a > MAX_SHORT + b)
return TRUE;
}
else if(a < 0 && b >= 0)
{
if( a < MIN_SHORT + b)
return TRUE;
}
return FALSE;
}
int CMisc::sub_overflow(BYTE a, BYTE b)
{
return a < b;
}
int CMisc::sub_overflow(WORD a, WORD b)
{
return a < b;
}
int CMisc::mult(long op1, long op2, long *prod_hi, long *prod_lo)
{
long _hi,_low;
__asm
{
pushad
mov eax,op1
mov ecx,op2
imul ecx
mov _hi,edx
mov _low,eax
popad
}
*prod_hi = _hi;
*prod_lo = _low;
return OK;
}
int CMisc::div_overflow(int a, int b)
{
if( b == 0)
return TRUE;
if(a == MIN_INT && b == -1)
return TRUE;
return FALSE;
}
int CMisc::div_overflow(DWORD a, DWORD b)
{
if( b == 0)
return TRUE;
return FALSE;
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.