11 #if defined( INC_ALL )
23 #if defined( __AMX__ )
29 int threadPriority(
void )
33 cjtkpradjust( cjtkid(), &priority );
48 #elif defined( __BEOS__ ) || defined( __SYMBIAN32__ )
50 int strnicmp(
const char *src,
const char *
dest,
int length )
56 char srcCh = *src++, destCh = *dest++;
63 return( srcCh - destCh );
69 int stricmp(
const char *src,
const char *dest )
71 const int length = strlen( src );
73 if( length != strlen( dest ) )
75 return( strnicmp( src, dest, length ) );
84 #elif defined( __UCOS__ )
94 INT8U threadSelf(
void )
98 OSTaskQuery( OS_PRIO_SELF, &osTCB );
99 return( osTCB.OSTCBPrio );
108 #elif defined( __ITRON__ )
117 ID threadSelf(
void )
136 #elif defined( __MVS__ ) && defined( EBCDIC_CHARS )
144 static const BYTE asciiToEbcdicTbl[] = {
145 0x00, 0x01, 0x02, 0x03, 0x37, 0x2D, 0x2E, 0x2F,
146 0x16, 0x05, 0x15, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
147 0x10, 0x11, 0x12, 0x13, 0x3C, 0x3D, 0x32, 0x26,
148 0x18, 0x19, 0x3F, 0x27, 0x1C, 0x1D, 0x1E, 0x1F,
149 0x40, 0x5A, 0x7F, 0x7B, 0x5B, 0x6C, 0x50, 0x7D,
150 0x4D, 0x5D, 0x5C, 0x4E, 0x6B, 0x60, 0x4B, 0x61,
151 0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7,
152 0xF8, 0xF9, 0x7A, 0x5E, 0x4C, 0x7E, 0x6E, 0x6F,
153 0x7C, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7,
154 0xC8, 0xC9, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6,
155 0xD7, 0xD8, 0xD9, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6,
156 0xE7, 0xE8, 0xE9, 0xAD, 0xE0, 0xBD, 0x5F, 0x6D,
157 0x79, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
158 0x88, 0x89, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
159 0x97, 0x98, 0x99, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6,
160 0xA7, 0xA8, 0xA9, 0xC0, 0x4F, 0xD0, 0xA1, 0x07,
161 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x06, 0x17,
162 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x09, 0x0A, 0x1B,
163 0x30, 0x31, 0x1A, 0x33, 0x34, 0x35, 0x36, 0x08,
164 0x38, 0x39, 0x3A, 0x3B, 0x04, 0x14, 0x3E, 0xFF,
165 0x41, 0xAA, 0x4A, 0xB1, 0x9F, 0xB2, 0x6A, 0xB5,
166 0xBB, 0xB4, 0x9A, 0x8A, 0xB0, 0xCA, 0xAF, 0xBC,
167 0x90, 0x8F, 0xEA, 0xFA, 0xBE, 0xA0, 0xB6, 0xB3,
168 0x9D, 0xDA, 0x9B, 0x8B, 0xB7, 0xB8, 0xB9, 0xAB,
169 0x64, 0x65, 0x62, 0x66, 0x63, 0x67, 0x9E, 0x68,
170 0x74, 0x71, 0x72, 0x73, 0x78, 0x75, 0x76, 0x77,
171 0xAC, 0x69, 0xED, 0xEE, 0xEB, 0xEF, 0xEC, 0xBF,
172 0x80, 0xFD, 0xFE, 0xFB, 0xFC, 0xBA, 0xAE, 0x59,
173 0x44, 0x45, 0x42, 0x46, 0x43, 0x47, 0x9C, 0x48,
174 0x54, 0x51, 0x52, 0x53, 0x58, 0x55, 0x56, 0x57,
175 0x8C, 0x49, 0xCD, 0xCE, 0xCB, 0xCF, 0xCC, 0xE1,
176 0x70, 0xDD, 0xDE, 0xDB, 0xDC, 0x8D, 0x8E, 0xDF
181 static const BYTE ebcdicToAsciiTbl[] = {
182 0x00, 0x01, 0x02, 0x03, 0x9C, 0x09, 0x86, 0x7F,
183 0x97, 0x8D, 0x8E, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
184 0x10, 0x11, 0x12, 0x13, 0x9D, 0x0A, 0x08, 0x87,
185 0x18, 0x19, 0x92, 0x8F, 0x1C, 0x1D, 0x1E, 0x1F,
186 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x17, 0x1B,
187 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x05, 0x06, 0x07,
188 0x90, 0x91, 0x16, 0x93, 0x94, 0x95, 0x96, 0x04,
189 0x98, 0x99, 0x9A, 0x9B, 0x14, 0x15, 0x9E, 0x1A,
190 0x20, 0xA0, 0xE2, 0xE4, 0xE0, 0xE1, 0xE3, 0xE5,
191 0xE7, 0xF1, 0xA2, 0x2E, 0x3C, 0x28, 0x2B, 0x7C,
192 0x26, 0xE9, 0xEA, 0xEB, 0xE8, 0xED, 0xEE, 0xEF,
193 0xEC, 0xDF, 0x21, 0x24, 0x2A, 0x29, 0x3B, 0x5E,
194 0x2D, 0x2F, 0xC2, 0xC4, 0xC0, 0xC1, 0xC3, 0xC5,
195 0xC7, 0xD1, 0xA6, 0x2C, 0x25, 0x5F, 0x3E, 0x3F,
196 0xF8, 0xC9, 0xCA, 0xCB, 0xC8, 0xCD, 0xCE, 0xCF,
197 0xCC, 0x60, 0x3A, 0x23, 0x40, 0x27, 0x3D, 0x22,
198 0xD8, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
199 0x68, 0x69, 0xAB, 0xBB, 0xF0, 0xFD, 0xFE, 0xB1,
200 0xB0, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70,
201 0x71, 0x72, 0xAA, 0xBA, 0xE6, 0xB8, 0xC6, 0xA4,
202 0xB5, 0x7E, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
203 0x79, 0x7A, 0xA1, 0xBF, 0xD0, 0x5B, 0xDE, 0xAE,
204 0xAC, 0xA3, 0xA5, 0xB7, 0xA9, 0xA7, 0xB6, 0xBC,
205 0xBD, 0xBE, 0xDD, 0xA8, 0xAF, 0x5D, 0xB4, 0xD7,
206 0x7B, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
207 0x48, 0x49, 0xAD, 0xF4, 0xF6, 0xF2, 0xF3, 0xF5,
208 0x7D, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50,
209 0x51, 0x52, 0xB9, 0xFB, 0xFC, 0xF9, 0xFA, 0xFF,
210 0x5C, 0xF7, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
211 0x59, 0x5A, 0xB2, 0xD4, 0xD6, 0xD2, 0xD3, 0xD5,
212 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
213 0x38, 0x39, 0xB3, 0xDB, 0xDC, 0xD9, 0xDA, 0x9F
218 int asciiToEbcdic(
char *dest,
const char *src,
const int length )
225 for( i = 0; i <
length; i++ )
226 dest[ i ] = asciiToEbcdicTbl[ (
unsigned int ) src[ i ] ];
230 int ebcdicToAscii(
char *dest,
const char *src,
const int length )
237 for( i = 0; i <
length; i++ )
238 dest[ i ] = ebcdicToAsciiTbl[ (
unsigned int ) src[ i ] ];
243 int asciiToEbcdic(
char *dest,
const char *src,
const int length )
249 memcpy( dest, src, length );
250 return(
return( __atoe_l( dest, length ) < 0 ? \
254 int ebcdicToAscii(
char *dest,
const char *src,
const int length )
260 memcpy( dest, src, length );
261 return(
return( __etoa_l( dest, length ) < 0 ? \
269 char *bufferToEbcdic(
char *
buffer,
const char *
string )
271 strcpy( buffer,
string );
272 asciiToEbcdic( buffer, buffer, strlen(
string ) );
278 #define A ASCII_ALPHA
279 #define L ASCII_LOWER
280 #define N ASCII_NUMERIC
281 #define S ASCII_SPACE
282 #define U ASCII_UPPER
286 #define ANX ( A | N | X )
287 #define AUX ( A | U | X )
289 const BYTE asciiCtypeTbl[ 256 ] = {
291 0, 0, 0, 0, 0, 0, 0, 0,
293 0, 0, 0, 0, 0, 0, 0, 0,
295 0, 0, 0, 0, 0, 0, 0, 0,
297 0, 0, 0, 0, 0, 0, 0, 0,
299 A, A, A, A, A, A, A, A,
301 A, A, A, A, A, A, A, A,
303 ANX, ANX, ANX, ANX, ANX, ANX, ANX, ANX,
305 ANX, ANX, A, A, A, A, A, A,
307 A, AUX, AUX, AUX, AUX, AUX, AUX, AU,
309 AU, AU, AU, AU, AU, AU, AU, AU,
311 AU, AU, AU, AU, AU, AU, AU, AU,
313 AU, AU, AU, A, A, A, A, A,
315 A,
AL, AL, AL, AL, AL, AL, AL,
317 AL, AL, AL, AL, AL, AL, AL, AL,
319 AL, AL, AL, AL, AL, AL, AL, AL,
321 AL, AL, AL, A, A, A, A, A,
330 int strCompare(
const char *src,
const char *dest,
int length )
343 if( *src < 0x80 && *dest < 0x80 && \
348 src = bufferToEbcdic( buffer1, src );
349 dest = bufferToEbcdic( buffer2, dest );
350 return( strncasecmp( src, dest, length ) );
353 int strCompareZ(
const char *src,
const char *dest )
355 const int length = strlen( src );
357 if( length != strlen( dest ) )
368 int sPrintf_s(
char *buffer,
const int bufSize,
const char *format, ... )
372 const int formatLen = strlen( format ) - 1;
381 for( i = 0; i < formatLen; i++ )
383 if( format[ i ] ==
'%' && format[ i + 1 ] ==
's' )
387 "<<<Unable to format output string>>>" );
392 format = bufferToEbcdic( formatBuffer, format );
393 va_start( argPtr, format );
394 status = vsprintf( buffer, format, argPtr );
396 ebcdicToAscii( buffer, buffer, status );
407 #elif defined( __PALMOS__ )
409 #include <CmnErrors.h>
410 #include <CmnLaunchCodes.h>
414 uint32_t cryptlibMain( uint16_t cmd,
void *cmdPBP, uint16_t launchFlags )
421 case sysLaunchCmdInitialize:
426 case sysLaunchCmdFinalize:
441 #elif defined( __RTEMS__ )
451 rtems_id threadSelf(
void )
455 rtems_task_ident( RTEMS_SELF, RTEMS_SEARCH_ALL_NODES, &taskID );
469 #elif defined( __TANDEM_NSK__ ) || defined( __TANDEM_OSS__ )
473 time_t my_mktime(
struct tm *timeptr )
477 theTime = mktime( timeptr );
478 if( theTime < 0 && timeptr->tm_year > 122 && timeptr->tm_year <= 138 )
480 timeptr->tm_year = 122;
481 theTime = mktime( timeptr );
492 #elif defined( __UNIX__ ) && \
493 !( defined( __MVS__ ) || defined( __TANDEM_NSK__ ) || \
494 defined( __TANDEM_OSS__ ) )
496 #include <sys/time.h>
504 long timeLSB, timeDifference;
507 gettimeofday( &tv, NULL );
508 timeLSB = tv.tv_usec;
515 if( startTime < timeLSB )
516 timeDifference = timeLSB - startTime;
519 timeDifference = ( 1000000L - startTime ) + timeLSB;
520 if( timeDifference <= 0 )
522 printf(
"Error: Time difference = %lX, startTime = %lX, "
523 "endTime = %lX.\n", timeDifference, startTime, timeLSB );
526 return( timeDifference );
540 #if defined( sun ) && ( OSVERSION <= 5 )
544 int fixedSprintf(
char *buffer,
const int bufSize,
const char *format, ... )
549 va_start( argPtr, format );
550 length = vsprintf( buffer, format, argPtr );
563 #elif defined( __WIN32__ )
617 void threadYield(
void )
619 static int sleepTime = -1;
624 SYSTEM_INFO systemInfo;
626 GetSystemInfo( &systemInfo );
627 sleepTime = ( systemInfo.dwNumberOfProcessors > 1 ) ? 10 : 1;
642 long timeLSB, timeDifference;
645 LARGE_INTEGER performanceCount;
648 QueryPerformanceCounter( &performanceCount );
649 timeLSB = performanceCount.LowPart;
651 FILETIME dummyTime, kernelTime, userTime;
654 GetThreadTimes( GetCurrentThread(), &dummyTime, &dummyTime,
655 &kernelTime, &userTime );
656 timeLSB = userTime.dwLowDateTime;
664 if( startTime < timeLSB )
665 timeDifference = timeLSB - startTime;
669 timeDifference = ( 0xFFFFFFFFUL - startTime ) + 1 + timeLSB;
671 if( timeDifference <= 0 )
673 printf(
"Error: Time difference = %X, startTime = %X, endTime = %X.\n",
674 timeDifference, startTime, timeLSB );
677 return( timeDifference );
686 #if defined( __BORLANDC__ ) && ( __BORLANDC__ < 0x0550 )
688 int bcSnprintf(
char *buffer,
const int bufSize,
const char *format, ... )
693 va_start( argPtr, format );
694 length = vsprintf( buffer, format, argPtr );
700 int bcVsnprintf(
char *buffer,
const int bufSize,
const char *format, va_list argPtr )
702 return( vsprintf( buffer, format, argPtr ) );
726 #define CSIDL_SYSTEM 0x25
728 #ifndef SHGFP_TYPE_CURRENT
729 #define SHGFP_TYPE_CURRENT 0
732 HMODULE WINAPI loadExistingLibrary(
IN_STRING LPCTSTR lpFileName )
737 hFile = CreateFile( lpFileName, GENERIC_READ, 0, NULL, OPEN_EXISTING,
738 FILE_ATTRIBUTE_NORMAL, NULL );
739 if( hFile == INVALID_HANDLE_VALUE )
741 CloseHandle( hFile );
743 return( LoadLibrary( lpFileName ) );
746 HMODULE WINAPI loadFromSystemDirectory(
IN_STRING LPCTSTR lpFileName )
748 char path[ MAX_PATH + 8 ];
749 const int fileNameLength = strlen( lpFileName ) + 1;
754 GetSystemDirectory( path, MAX_PATH - ( fileNameLength + 8 ) );
755 if( pathLength < 1 || pathLength > MAX_PATH - ( fileNameLength + 8 ) )
757 path[ pathLength++ ] =
'\\';
758 memcpy( path + pathLength, lpFileName, fileNameLength );
760 return( loadExistingLibrary( path ) );
763 HMODULE WINAPI SafeLoadLibrary(
IN_STRING LPCTSTR lpFileName )
765 typedef HRESULT ( WINAPI *SHGETFOLDERPATH )( HWND hwndOwner,
766 int nFolder,
HANDLE hToken,
769 const char *dllName;
const int dllNameLen;
771 static const DLL_NAME_INFO dllNameInfoTbl[] = {
772 {
"Crypt32.dll", 11 }, {
"ComCtl32.dll", 12 },
773 {
"dnsapi.dll", 10 }, {
"Mpr.dll", 7 },
774 {
"NetAPI32.dll", 12 }, {
"ODBC32.dll", 10 },
775 {
"SetupAPI.dll", 12 }, {
"SHFolder.dll", 12 },
776 {
"Shell32.dll", 11 }, {
"WinHTTP.dll", 11 },
777 {
"wldap32.dll", 11 }, {
"ws2_32.dll", 10 },
778 {
"wsock32.dll", 11 },
779 { NULL, 0 }, { NULL, 0 }
781 SHGETFOLDERPATH pSHGetFolderPath;
783 char path[ MAX_PATH + 8 ];
784 const int fileNameLength = strlen( lpFileName ) + 1;
794 return( LoadLibrary( lpFileName ) );
797 if( lpFileName[ 0 ] ==
'/' || \
798 ( fileNameLength > 3 &&
isAlpha( lpFileName[ 0 ] ) && \
799 lpFileName[ 1 ] ==
':' && lpFileName[ 2 ] ==
'/' ) )
800 return( loadExistingLibrary( lpFileName ) );
803 for( i = 0; dllNameInfoTbl[ i ].dllName != NULL && \
806 if( dllNameInfoTbl[ i ].dllNameLen == fileNameLength - 1 && \
807 !
strCompare( dllNameInfoTbl[ i ].dllName, lpFileName,
808 fileNameLength - 1 ) )
812 return( loadFromSystemDirectory( lpFileName ) );
830 hShell32 = loadFromSystemDirectory(
"Shell32.dll" );
831 if( hShell32 != NULL )
833 pSHGetFolderPath = ( SHGETFOLDERPATH ) \
834 GetProcAddress( hShell32,
"SHGetFolderPathA" );
835 if( pSHGetFolderPath != NULL && \
836 pSHGetFolderPath( NULL, CSIDL_SYSTEM, NULL, SHGFP_TYPE_CURRENT,
839 FreeLibrary( hShell32 );
848 pathLength = strlen( path );
849 if( pathLength < 3 || pathLength + 1 + fileNameLength > MAX_PATH )
862 return( LoadLibrary( lpFileName ) );
864 path[ pathLength++ ] =
'\\';
865 memcpy( path + pathLength, lpFileName, fileNameLength );
867 return( loadExistingLibrary( path ) );
880 #define ACL_BUFFER_SIZE 1024
881 #define TOKEN_BUFFER_SIZE 256
900 SECURITY_ATTRIBUTES sa;
901 SECURITY_DESCRIPTOR pSecurityDescriptor;
903 PTOKEN_USER pTokenUser;
904 BYTE aclBuffer[ ACL_BUFFER_SIZE + 8 ];
905 BYTE tokenBuffer[ TOKEN_BUFFER_SIZE + 8 ];
912 void *initACLInfo(
const int access )
914 SECURITY_INFO *securityInfo;
915 HANDLE hToken = INVALID_HANDLE_VALUE;
923 if( ( securityInfo = \
924 clAlloc(
"initACLInfo",
sizeof( SECURITY_INFO ) ) ) == NULL )
926 memset( securityInfo, 0,
sizeof( SECURITY_INFO ) );
927 securityInfo->pAcl = ( PACL ) securityInfo->aclBuffer;
928 securityInfo->pTokenUser = ( PTOKEN_USER ) securityInfo->tokenBuffer;
936 if( OpenThreadToken( GetCurrentThread(), TOKEN_QUERY,
FALSE, &hToken ) || \
937 OpenProcessToken( GetCurrentProcess(), TOKEN_QUERY, &hToken ) )
941 tokenOK = GetTokenInformation( hToken, TokenUser,
942 securityInfo->pTokenUser,
943 TOKEN_BUFFER_SIZE, &cbTokenUser );
944 CloseHandle( hToken );
948 clFree(
"initACLInfo", securityInfo );
953 if( !InitializeSecurityDescriptor( &securityInfo->pSecurityDescriptor,
954 SECURITY_DESCRIPTOR_REVISION ) || \
955 !SetSecurityDescriptorOwner( &securityInfo->pSecurityDescriptor,
956 securityInfo->pTokenUser->User.Sid,
959 clFree(
"initACLInfo", securityInfo );
965 if( !InitializeAcl( securityInfo->pAcl, ACL_BUFFER_SIZE,
967 !AddAccessAllowedAce( securityInfo->pAcl, ACL_REVISION, access,
968 securityInfo->pTokenUser->User.Sid ) )
970 clFree(
"initACLInfo", securityInfo );
975 if( !SetSecurityDescriptorDacl( &securityInfo->pSecurityDescriptor,
TRUE,
976 securityInfo->pAcl,
FALSE ) )
978 clFree(
"initACLInfo", securityInfo );
982 assert( IsValidSecurityDescriptor( &securityInfo->pSecurityDescriptor ) );
985 securityInfo->sa.nLength =
sizeof( SECURITY_ATTRIBUTES );
986 securityInfo->sa.bInheritHandle =
FALSE;
987 securityInfo->sa.lpSecurityDescriptor = &securityInfo->pSecurityDescriptor;
989 return( securityInfo );
993 void freeACLInfo(
INOUT TYPECAST( SECURITY_INFO * )
void *securityInfoPtr )
995 SECURITY_INFO *securityInfo = ( SECURITY_INFO * ) securityInfoPtr;
997 assert( securityInfoPtr == NULL || \
998 isWritePtr( securityInfoPtr,
sizeof( SECURITY_INFO ) ) );
1000 if( securityInfo == NULL )
1002 clFree(
"freeACLInfo", securityInfo );
1009 void *getACLInfo(
INOUT TYPECAST( SECURITY_INFO * )
void *securityInfoPtr )
1011 SECURITY_INFO *securityInfo = ( SECURITY_INFO * ) securityInfoPtr;
1013 assert( securityInfo == NULL || \
1014 isWritePtr( securityInfo,
sizeof( SECURITY_INFO ) ) );
1016 return( ( securityInfo == NULL ) ? NULL : &securityInfo->sa );
1034 #if !( defined( NT_DRIVER ) || defined( STATIC_LIB ) )
1036 BOOL WINAPI DllMain( HINSTANCE hinstDLL,
DWORD fdwReason, LPVOID lpvReserved )
1045 case DLL_PROCESS_ATTACH:
1048 DisableThreadLibraryCalls( hinstDLL );
1054 case DLL_PROCESS_DETACH:
1059 case DLL_THREAD_ATTACH:
1060 case DLL_THREAD_DETACH:
1070 #define MB_OK 0x00000000L
1071 #define MB_ICONQUESTION 0x00000020L
1073 int WINAPI MessageBoxA( HWND hWnd, LPCSTR lpText, LPCSTR lpCaption,
1077 #pragma comment( linker, "/export:DllRegisterServer=_DllRegisterServer@0,PRIVATE" )
1080 STDAPI DllRegisterServer(
void )
1082 MessageBoxA( NULL,
"Why are you trying to register the cryptlib DLL?\n"
1083 "It's just a standard Windows DLL, there's nothing\nto be "
1084 "registered.",
"ESO Error",
1085 MB_ICONQUESTION | MB_OK );
1086 return( E_NOINTERFACE );
1094 #if defined( __BORLANDC__ ) && ( __BORLANDC__ < 0x550 )
1096 BOOL WINAPI DllEntryPoint( HINSTANCE hinstDLL,
DWORD fdwReason, \
1097 LPVOID lpvReserved )
1099 return( DllMain( hinstDLL, fdwReason, lpvReserved ) );
1103 #elif defined( __WIN16__ )
1124 int CALLBACK LibMain( HINSTANCE hInstance, WORD wDataSeg, WORD wHeapSize, \
1133 int CALLBACK WEP(
int nSystemExit )
1135 switch( nSystemExit )
1137 case WEP_SYSTEM_EXIT:
1153 #if defined( __WIN32__ ) && !defined( NO_ASM )
1157 unsigned int magicValue, version;
1181 } __except (EXCEPTION_EXECUTE_HANDLER) {}
1183 return( magicValue ==
'VMXh' ) ?
TRUE :
FALSE );
1199 #elif defined( __WINCE__ )
1206 static LARGE_INTEGER *getTimeOffset(
void )
1208 static LARGE_INTEGER timeOffset = { 0 };
1213 if( timeOffset.QuadPart == 0 )
1215 SYSTEMTIME ofsSystemTime;
1216 FILETIME ofsFileTime;
1218 memset( &ofsSystemTime, 0,
sizeof( SYSTEMTIME ) );
1219 ofsSystemTime.wYear = 1970;
1220 ofsSystemTime.wMonth = 1;
1221 ofsSystemTime.wDay = 1;
1222 SystemTimeToFileTime( &ofsSystemTime, &ofsFileTime );
1223 timeOffset.HighPart = ofsFileTime.dwHighDateTime;
1224 timeOffset.LowPart = ofsFileTime.dwLowDateTime;
1227 return( &timeOffset );
1231 static time_t fileTimeToTimeT(
const FILETIME *fileTime )
1233 const LARGE_INTEGER *timeOffset = getTimeOffset();
1234 LARGE_INTEGER largeInteger;
1238 largeInteger.HighPart = fileTime->dwHighDateTime;
1239 largeInteger.LowPart = fileTime->dwLowDateTime;
1240 largeInteger.QuadPart = ( largeInteger.QuadPart - \
1241 timeOffset->QuadPart ) / 10000000L;
1242 if(
sizeof( time_t ) == 4 && \
1243 largeInteger.QuadPart > 0x80000000UL )
1253 return( ( time_t ) largeInteger.QuadPart );
1257 static
void timeTToFileTime( FILETIME *fileTime,
const time_t timeT )
1259 const LARGE_INTEGER *timeOffset = getTimeOffset();
1260 LARGE_INTEGER largeInteger = { timeT };
1264 largeInteger.QuadPart = ( largeInteger.QuadPart * 10000000L ) + \
1265 timeOffset->QuadPart;
1266 fileTime->dwHighDateTime = largeInteger.HighPart;
1267 fileTime->dwLowDateTime = largeInteger.LowPart;
1270 time_t time( time_t *timePtr )
1274 SYSTEMTIME systemTime;
1277 assert( timePtr == NULL );
1282 GetSystemTime( &systemTime );
1283 SystemTimeToFileTime( &systemTime, &fileTime );
1285 GetSystemTimeAsFileTime( &fileTime );
1288 return( fileTimeToTimeT( &fileTime ) );
1291 time_t mktime(
struct tm *tmStruct )
1293 SYSTEMTIME systemTime;
1296 assert(
isWritePtr( tmStruct,
sizeof(
struct tm ) ) );
1301 memset( &systemTime, 0,
sizeof( SYSTEMTIME ) );
1302 systemTime.wYear = tmStruct->tm_year + 1900;
1303 systemTime.wMonth = tmStruct->tm_mon + 1;
1304 systemTime.wDay = tmStruct->tm_mday;
1305 systemTime.wHour = tmStruct->tm_hour;
1306 systemTime.wMinute = tmStruct->tm_min;
1307 systemTime.wSecond = tmStruct->tm_sec;
1308 SystemTimeToFileTime( &systemTime, &fileTime );
1309 LocalFileTimeToFileTime( &fileTime, &fileTime );
1311 return( fileTimeToTimeT( &fileTime ) );
1314 struct tm *gmtime(
const time_t *timePtr )
1316 static struct tm tmStruct;
1317 SYSTEMTIME systemTime;
1320 assert(
isReadPtr( timePtr,
sizeof( time_t ) ) );
1325 timeTToFileTime( &fileTime, *timePtr );
1326 FileTimeToSystemTime( &fileTime, &systemTime );
1327 memset( &tmStruct, 0,
sizeof(
struct tm ) );
1328 tmStruct.tm_year = systemTime.wYear - 1900;
1329 tmStruct.tm_mon = systemTime.wMonth - 1;
1330 tmStruct.tm_mday = systemTime.wDay;
1331 tmStruct.tm_hour = systemTime.wHour;
1332 tmStruct.tm_min = systemTime.wMinute;
1333 tmStruct.tm_sec = systemTime.wSecond;
1335 return( &tmStruct );
1345 #if !defined( NDEBUG )
1347 int debugPrintf(
const char *format, ... )
1350 char buffer[ 1024 ];
1351 wchar_t wcBuffer[ 1024 ];
1354 va_start( argPtr, format );
1355 length = vsprintf( buffer, format, argPtr );
1357 status = asciiToUnicode( wcBuffer, 1024, buffer, length );
1359 NKDbgPrintfW( L
"%s", wcBuffer );
1377 int asciiToUnicode(
wchar_t *dest,
const int destMaxLen,
1378 const char *src,
const int length )
1388 status = MultiByteToWideChar( GetACP(), 0, src, destMaxLen, dest,
1393 int unicodeToAscii(
char *dest,
const int destMaxLen,
1394 const wchar_t *src,
const int length )
1413 status = WideCharToMultiByte( GetACP(), 0, src, length, dest,
1414 length *
sizeof(
wchar_t ),
"_", NULL );
1417 status =
wcstombs_s( &destLen, dest, destMaxLen, src,
1418 length *
sizeof(
wchar_t ) );
1423 BOOL WINAPI DllMain(
HANDLE hinstDLL,
DWORD dwReason, LPVOID lpvReserved )
1430 case DLL_PROCESS_ATTACH:
1433 DisableThreadLibraryCalls( hinstDLL );
1439 case DLL_PROCESS_DETACH:
1444 case DLL_THREAD_ATTACH:
1445 case DLL_THREAD_DETACH:
1459 #ifdef NO_NATIVE_STRLCPY
1474 int strlcpy_s(
char *dest,
const int destLen,
const char *src )
1483 for( i = 0; i < destLen - 1 && *src !=
'\0'; i++ )
1490 int strlcat_s(
char *dest,
const int destLen,
const char *src )
1497 for( i = 0; i < destLen && dest[ i ] !=
'\0'; i++ );
1502 dest[ destLen - 1 ] =
'\0';
1509 while( i < destLen - 1 && *src !=
'\0' )
1510 dest[ i++ ] = *src++;
1523 #if defined( __WIN32__ ) && !defined( _M_X64 ) && !defined( NO_ASM )
1526 static
int getHWInfo(
void )
1529 char vendorID[ 12 + 8 ];
1530 unsigned long processorID, featureFlags;
1554 mov [hasAdvFeatures], 1
1562 mov dword ptr [vendorID], ebx
1563 mov dword ptr [vendorID+4], edx
1564 mov dword ptr [vendorID+8], ecx
1567 mov [processorID], eax
1568 mov [featureFlags], ecx
1574 if( !hasAdvFeatures )
1579 if( !memcmp( vendorID,
"CentaurHauls", 12 ) )
1603 and eax, 0110000000000b
1604 cmp eax, 0110000000000b
1609 and eax, 011000000000000b
1610 cmp eax, 011000000000000b
1616 if( !memcmp( vendorID,
"AuthenticAMD", 12 ) )
1619 if( ( processorID & 0x05A0 ) == 0x05A0 )
1622 if( !memcmp( vendorID,
"GenuineIntel", 12 ) )
1625 if( featureFlags & ( 1 << 25 ) )
1629 if( featureFlags & ( 1 << 30 ) )
1636 #elif defined( __WIN32__ ) && defined( _M_X64 )
1644 #pragma intrinsic( __cpuid )
1646 typedef struct {
unsigned int eax, ebx, ecx, edx; } CPUID_INFO;
1649 static
void cpuID(
OUT CPUID_INFO *result,
const int type )
1654 memset( result, 0,
sizeof( CPUID_INFO ) );
1657 __cpuid( intResult, type );
1658 result->eax = intResult[ 0 ];
1659 result->ebx = intResult[ 1 ];
1660 result->ecx = intResult[ 2 ];
1661 result->edx = intResult[ 3 ];
1665 static
int getHWInfo(
void )
1667 CPUID_INFO cpuidInfo;
1668 char vendorID[ 12 + 8 ];
1669 int *vendorIDptr = (
int * ) vendorID;
1670 unsigned long processorID, featureFlags;
1679 cpuID( &cpuidInfo, 0 );
1680 vendorIDptr[ 0 ] = cpuidInfo.ebx;
1681 vendorIDptr[ 1 ] = cpuidInfo.edx;
1682 vendorIDptr[ 2 ] = cpuidInfo.ecx;
1683 cpuID( &cpuidInfo, 1 );
1684 processorID = cpuidInfo.eax;
1685 featureFlags = cpuidInfo.ecx;
1688 if( !memcmp( vendorID,
"CentaurHauls", 12 ) )
1695 cpuID( &cpuidInfo, 0xC0000000 );
1696 if( cpuidInfo.eax >= 0xC0000001 )
1699 cpuID( &cpuidInfo, 0xC0000000 );
1700 if( ( cpuidInfo.edx & 0x000C ) == 0x000C )
1702 if( ( cpuidInfo.edx & 0x00C0 ) == 0x00C0 )
1704 if( ( cpuidInfo.edx & 0x0C00 ) == 0x0C00 )
1706 if( ( cpuidInfo.edx & 0x3000 ) == 0x3000 )
1710 if( !memcmp( vendorID,
"AuthenticAMD", 12 ) )
1713 if( ( processorID & 0x05A0 ) == 0x05A0 )
1716 if( !memcmp( vendorID,
"GenuineIntel", 12 ) )
1719 if( featureFlags & ( 1 << 25 ) )
1723 if( featureFlags & ( 1 << 30 ) )
1730 #elif defined( __GNUC__ ) && defined( __i386__ ) && !defined( NO_ASM )
1732 #if HWCAP_FLAG_RDTSC != 0x01
1733 #error Need to sync HWCAP_FLAG_RDTSC with equivalent asm definition
1737 static
int getHWInfo(
void )
1739 char vendorID[ 12 + 8 ];
1740 unsigned long processorID, featureFlags;
1741 int hasAdvFeatures = 0, sysCaps = 0;
1753 asm volatile(
"pushf\n\t"
1755 "movl %%eax, %%ecx\n\t"
1756 "xorl $0x200000, %%eax\n\t"
1763 "xorl %%ecx, %%eax\n\t"
1765 "movl $1, %[hasAdvFeatures]\n\t"
1766 "movl %[HW_FLAG_RDTSC], %[sysCaps]\n\t"
1768 "xorl %%eax, %%eax\n\t"
1770 "leal %2, %%eax\n\t"
1771 "movl %%ebx, (%%eax)\n\t"
1772 "movl %%edx, 4(%%eax)\n\t"
1773 "movl %%ecx, 8(%%eax)\n\t"
1774 "movl $1, %%eax\n\t"
1776 "leal %3, %%ebx\n\t"
1777 "movl %%eax, (%%ebx)\n\t"
1778 "leal %4, %%ebx\n\t"
1779 "movl %%ecx, (%%ebx)\n\t"
1783 ".section .note.GNU-stack, \"\", @progbits; .previous\n"
1790 : [hasAdvFeatures]
"=m"(hasAdvFeatures),
1791 [sysCaps]
"=m"(sysCaps),
1792 [vendorID]
"=m"(vendorID),
1793 [processorID]
"=m"(processorID),
1794 [featureFlags]
"=m"(featureFlags)
1796 :
"%eax",
"%ecx",
"%edx"
1801 if( !hasAdvFeatures )
1806 if( !memcmp( vendorID,
"CentaurHauls", 12 ) )
1808 asm volatile(
"pushl %%ebx\n\t"
1809 "movl $0xC0000000, %%eax\n\t"
1812 "cmpl $0xC0000001, %%eax\n\t"
1815 "movl $0xC0000001, %%eax\n\t"
1818 "movl %%edx, %%eax\n\t"
1819 "andl $0xC, %%edx\n\t"
1820 "cmpl $0xC, %%edx\n\t"
1822 "orl %[HW_FLAG_XSTORE], %[sysCaps]\n"
1824 "movl %%edx, %%eax\n\t"
1825 "andl $0xC0, %%eax\n\t"
1826 "cmpl $0xC0, %%eax\n\t"
1828 "orl %[HW_FLAG_XCRYPT], %[sysCaps]\n"
1830 "movl %%edx, %%eax\n\t"
1831 "andl $0xC00, %%eax\n\t"
1832 "cmpl $0xC00, %%eax\n\t"
1834 "orl %[HW_FLAG_XSHA], %[sysCaps]\n"
1836 "movl %%edx, %%eax\n\t"
1837 "andl $0x3000, %%eax\n\t"
1838 "cmpl $0x3000, %%eax\n\t"
1840 "orl %[HW_FLAG_MONTMUL], %[sysCaps]\n"
1842 : [sysCaps]
"=m"(sysCaps)
1847 :
"%eax",
"%ecx",
"%edx"
1850 if( !memcmp( vendorID,
"AuthenticAMD", 12 ) )
1853 if( ( processorID & 0x05A0 ) == 0x05A0 )
1856 if( !memcmp( vendorID,
"GenuineIntel", 12 ) )
1859 if( featureFlags & ( 1 << 25 ) )
1863 if( featureFlags & ( 1 << 30 ) )
1870 #elif defined( __GNUC__ ) && ( defined( __arm ) || defined( __arm__ ) ) && \
1871 !defined( NO_ASM ) && 0
1874 static
int getHWInfo(
void )
1886 "mrc p15, 0, r0, c0, c0, 0\n\t"
1897 static
int getHWInfo(
void )
1908 #define MAX_SYSVARS 8
1912 #if ( defined( __WIN32__ ) || defined( __WINCE__ ) )
1916 OSVERSIONINFO osvi = {
sizeof( OSVERSIONINFO ) };
1917 SYSTEM_INFO systemInfo;
1925 if( !GetVersionEx( &osvi ) )
1929 osvi.dwMajorVersion = 5;
1930 osvi.dwPlatformId = VER_PLATFORM_WIN32_NT;
1932 sysVars[ SYSVAR_OSMAJOR ] = osvi.dwMajorVersion;
1933 sysVars[ SYSVAR_OSMINOR ] = osvi.dwMinorVersion;
1934 sysVars[ SYSVAR_ISWIN95 ] = \
1935 ( osvi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS ) ? \
1940 if( osvi.dwPlatformId == VER_PLATFORM_WIN32s )
1948 GetSystemInfo( &systemInfo );
1949 sysVars[ SYSVAR_PAGESIZE ] = systemInfo.dwPageSize;
1957 #elif defined( __UNIX__ )
1969 #if defined( _CRAY ) || defined( __hpux ) || defined( _M_XENIX ) || \
1971 #if defined( _SC_PAGESIZE )
1972 sysVars[ SYSVAR_PAGESIZE ] = sysconf( _SC_PAGESIZE );
1973 #elif defined( _SC_PAGE_SIZE )
1974 sysVars[ SYSVAR_PAGESIZE ] = sysconf( _SC_PAGE_SIZE );
1976 sysVars[ SYSVAR_PAGESIZE ] = 4096;
1979 sysVars[ SYSVAR_PAGESIZE ] = getpagesize();
1981 if( sysVars[ SYSVAR_PAGESIZE ] < 1024 )
1983 DEBUG_DIAG((
"System reports page size < 1024" ));
1988 sysVars[ SYSVAR_PAGESIZE ] = 4096;
1994 #if defined( __IBMC__ ) || defined( __IBMCPP__ )
2020 return( sysVars[ type ] );
2038 #if defined( __WIN32__ ) || defined( __WIN64__ )
2039 #define intptr_t INT_PTR
2040 #elif defined( __ECOS__ )
2041 #define intptr_t unsigned int
2042 #elif defined( __GNUC__ ) && ( __GNUC__ >= 3 )
2044 #elif defined( SYSTEM_64BIT )
2045 #define intptr_t long long
2047 #define intptr_t int
2055 return( (
void * ) ( (
char * ) ptr + ( -( (
intptr_t )( ptr ) ) & ( units - 1 ) ) ) );
2069 assert( ptr1 >= ptr2 );
2071 diff = (
const BYTE * ) ptr1 - (
const BYTE * ) ptr2;
2077 return( (
int ) diff );