Windows NT 4.0 source code leak
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

527 lines
13 KiB

4 years ago
  1. #include "detect.h"
  2. #include <crtapi.h>
  3. static union _REGS inregs;
  4. int IFS_Present( void )
  5. {
  6. static int fDone=-1;
  7. static int fResult;
  8. if (-1 == fDone)
  9. {
  10. fResult = IFSFUNC_Present();
  11. fDone = 0;
  12. }
  13. return fResult;
  14. } /* end IFS_Present() */
  15. #if DZECK
  16. char _far * WinGetEnv(char _far *var)
  17. {
  18. extern char _far * _pascal GetDosEnvironment(void);
  19. char _far *Env = GetDosEnvironment();
  20. while (*Env)
  21. {
  22. if (_fstrncmp(Env, var, _fstrlen(var)) == 0)
  23. return(Env + _fstrlen(var)+1);
  24. Env += _fstrlen(Env)+1;
  25. }
  26. return(0);
  27. }
  28. #endif
  29. /***************************************************************************/
  30. /* Copyright (c) 1989 - Microsoft Corp. */
  31. /* All rights reserved. */
  32. /* */
  33. /* Gets the PATH variable from the enviroment and then copies it to a */
  34. /* specified buffer and seperates the string into individual paths and */
  35. /* fills in an array of pointers to the beginning of each string. */
  36. /* */
  37. /* void GetPathStrings( char **apszPaths, char *chBuffer, int BufSize ) */
  38. /* */
  39. /* ARGUMENTS: apszPaths - Array of pointers to be filled in*/
  40. /* chBuffer - Buffer to copy the string into */
  41. /* BufSize - Size of passed buffer in bytes */
  42. /* RETURNS: void */
  43. /* */
  44. /* johnhe - 01/13/89 */
  45. /***************************************************************************/
  46. void GetPathStrings( char **apszPaths, char *chBuffer, int BufSize )
  47. {
  48. register i; /* Index for array apszPaths */
  49. char _far *szEnvironment; /* Pointer to eviro PATH string */
  50. char *pchEnd; /* Pointer to end of PATH string*/
  51. /* Make sure there is a path enviro variable */
  52. if ( (szEnvironment = getenv( "PATH" )) != NULL )
  53. //if ( (szEnvironment = WinGetEnv( "PATH" )) != NULL )
  54. {
  55. /* Copy string to work buffer */
  56. i = _fstrlen( szEnvironment );
  57. ++i;
  58. if ( i < BufSize )
  59. BufSize = i;
  60. _fstrncpy( chBuffer, szEnvironment, BufSize - 1 );
  61. *(chBuffer + BufSize - 1) = EOL;
  62. RemoveSpaces( chBuffer ); /* Clean up the string */
  63. pchEnd = strchr( chBuffer, EOL ); /* Find end of string */
  64. ReplaceChar( chBuffer, ';', EOL );/*
  65. * Convert to individ string
  66. */
  67. for ( i = 0; chBuffer < pchEnd; i++ )
  68. {
  69. apszPaths[i] = chBuffer; /* Save pointer to this path */
  70. chBuffer = strchr( chBuffer, EOL ) + 1; /*
  71. * Find end of
  72. * this path
  73. */
  74. }
  75. }
  76. apszPaths[i] = NULL; /* Mark end of array */
  77. } /* end GetPathStrings */
  78. /***************************************************************************/
  79. /* */
  80. /* int FindPath( char *szPathname ) */
  81. /* */
  82. /* ARGUMENTS: szPathname - PATH name to be found */
  83. /* RETURNS: TRUE if the szPathname was found in the PATH */
  84. /* environment variable */
  85. /* FALSE otherwise` */
  86. /* */
  87. /***************************************************************************/
  88. int FindPath( char *szPathname )
  89. {
  90. char **apszPaths;
  91. char *chBuffer;
  92. int i;
  93. if ( ( apszPaths = malloc( sizeof( char * ) * 100 ) ) == NULL )
  94. DetectExit( NO_MEMORY );
  95. if ( ( chBuffer = malloc( 200 ) ) == NULL )
  96. DetectExit( NO_MEMORY );
  97. GetPathStrings( apszPaths, chBuffer, 200 );/*
  98. * Get pointer to dir paths
  99. */
  100. for ( i = 0; apszPaths[i] != NULL; i++ )
  101. {
  102. if ( ScanPath( apszPaths[i], szPathname ) )
  103. return( 1 ); /* Found the desired path */
  104. }
  105. return( 0 ); /* Did not find the desired path */
  106. } /* end FindPath () */
  107. /***************************************************************************/
  108. /* */
  109. /* int ScanPath( char *szFullpathm, char *szSubpath ) */
  110. /* */
  111. /* ARGUMENTS: szFullpathm - string of the PATH env variable */
  112. /* szSubpath - path name to be found */
  113. /* */
  114. /* RETURNS: TRUE if the szSubpath was found in the szFullpathm*/
  115. /* FALSE otherwise` */
  116. /* */
  117. /***************************************************************************/
  118. int ScanPath( char *szFullpath, char *szSubpath )
  119. {
  120. char *szSubStart, *szSubEnd, *szPtr;
  121. int iStat;
  122. szPtr = strchr( szFullpath, ':' );
  123. if ( szPtr != NULL )
  124. szPtr++;
  125. else
  126. szPtr = szFullpath;
  127. while ( 1 )
  128. {
  129. szSubStart = strchr( szPtr, '\\' );
  130. if ( szSubStart != NULL )
  131. szSubStart++;
  132. else
  133. return ( RpcStrcmpi( szPtr, szSubpath ) == 0 );
  134. szSubEnd = strchr( szSubStart, '\\' );
  135. if ( szSubEnd != NULL )
  136. {
  137. *szSubEnd = EOL;
  138. iStat = ( RpcStrcmpi( szSubStart, szSubpath ) == 0 );
  139. *szSubEnd = '\\';
  140. if ( iStat )
  141. return( iStat ); /* Found match, return */
  142. }
  143. else
  144. return( RpcStrcmpi( szSubStart, szSubpath ) == 0 );
  145. szPtr = ++szSubEnd;
  146. }
  147. } /* end ScanPath() */
  148. /***************************************************************************/
  149. /* Scans a buffer for a matching string from a list of strings. If the */
  150. /* global iIgnoreCase == FALSE the buffer and string will be converted to */
  151. /* uppercase before before the search is done. */
  152. /* */
  153. /* int MultScanBuf( char *Buf, char **apszText, unsigned uSize ) */
  154. /* */
  155. /* ARGUMENTS: Buf - Ptr to the search buffer */
  156. /* apszText - Array of ptrs to the strings to search for */
  157. /* uSize - The size of the buffer in bytes */
  158. /* RETURNS: int - First matched string's index value or -1 if no */
  159. /* no match was found */
  160. /* */
  161. /***************************************************************************/
  162. int MultScanBuf( char far *Buf, char _far * _far *apszText, unsigned uSize )
  163. {
  164. char *szString;
  165. char *szStringBuf;
  166. register iMatch;
  167. unsigned uCount;
  168. int iIgnoreCase = 1;
  169. szString = szStringBuf = malloc( MAX_PATH_LEN );
  170. if ( szString == NULL )
  171. DetectExit( NO_MEMORY );
  172. /* Convert the buffer to upper case */
  173. if ( iIgnoreCase != FALSE )
  174. {
  175. for ( uCount = 0; uCount < uSize; uCount++ )
  176. Buf[ uCount ] = (char)toupper( Buf[ uCount ] );
  177. }
  178. /* Loop once for each string in the search list */
  179. for ( uCount = 0, iMatch = -1;
  180. apszText[ uCount ] != NULL && iMatch == -1;
  181. uCount++ )
  182. {
  183. /* This is a hard coded test for IBM PC-DOS */
  184. if ( _fstrcmp( apszText[ uCount ], "IBM PC-DOS" ) == OK )
  185. szString = "IBM PERSONAL COMPUTER";
  186. /* If ! case sensitive need to copy string to a */
  187. /* tmp buffer and convert it to upper case */
  188. else if ( iIgnoreCase != FALSE )
  189. {
  190. _fstrcpy( szString, apszText[ uCount ] );
  191. RpcStrupr( szString );
  192. }
  193. else
  194. szString = apszText[ uCount ];
  195. /* Call quick buffer search function */
  196. if ( FindString( Buf, (char far *)szString, uSize ) == OK )
  197. iMatch = (int)(uCount);
  198. }
  199. free( szStringBuf );
  200. return( iMatch );
  201. } /* end MultScanBuf */
  202. int SearchRedir( char *szRedirname, char _far *szRedirPath )
  203. {
  204. char **apszPaths;
  205. char szPath[100];
  206. char *Buffer;
  207. char _far *szEnd, *chBuffer;
  208. static struct find_t Dtr;
  209. int i;
  210. static int fResult;
  211. static char szSaveName[128]="", szSavePath[128]="";
  212. if (
  213. (0 == _fstrcmp(szSaveName, szRedirname)) &&
  214. (0 == _fstrcmp(szSavePath, szRedirPath))
  215. )
  216. return fResult;
  217. _fstrcpy(szSaveName, szRedirname);
  218. _fstrcpy(szSavePath, szRedirPath);
  219. if ( ( apszPaths = malloc( sizeof( char * ) * 100 ) ) == NULL )
  220. DetectExit( NO_MEMORY );
  221. if ( ( chBuffer = malloc( 200 ) ) == NULL )
  222. DetectExit( NO_MEMORY );
  223. GetPathStrings( apszPaths, chBuffer, 200 );/*
  224. * Get pointer to dir paths */
  225. Buffer = malloc(64);
  226. for ( i = 0; apszPaths[i] != NULL; i++ )
  227. {
  228. _fstrcpy( szPath, apszPaths[i] );
  229. szEnd = _fstrchr( szPath, '\0' );
  230. if ( *(szEnd - 1) != '\\' )
  231. *(szEnd++) = '\\';
  232. _fstrcpy( szEnd, szRedirname );
  233. _fstrcpy(Buffer, szPath);
  234. if ( _dos_findfirst( Buffer, _A_FILE, &Dtr ) == 0 )
  235. {
  236. free(Buffer);
  237. _fstrcpy( szRedirPath, szPath );
  238. return( fResult = 1 ); /* Found it */
  239. }
  240. }
  241. free(Buffer);
  242. return( fResult = 0 ); /* Return failure */
  243. } /* end SearchRedir */
  244. int ScanHimemStr( char _far *szRedirPath )
  245. {
  246. static int fDone=-1;
  247. static int fResult;
  248. static char _far *apszSrchStr[] = { "HIMEM:", NULL };
  249. if (-1 == fDone)
  250. {
  251. fResult = ( MultStrMatch(szRedirPath, apszSrchStr ) != -1 );
  252. fDone = 0;
  253. }
  254. return fResult;
  255. } /* end ScanHimemStr */
  256. int MultStrMatch( char _far *szPath, char _far * _far *apszText )
  257. {
  258. register iMatch;
  259. char far *Buf;
  260. static int iFile;
  261. long lBufSize;
  262. char *Buffer;
  263. /* Allocate max possible buffer < 64K */
  264. lBufSize = (GetMaxHugeSize() - 5000L);
  265. if ( lBufSize > 0x7000L )
  266. lBufSize = 0x7000L;
  267. if ( lBufSize < (long)MIN_BUF_SIZE ||
  268. (Buf = (char far *)_fmalloc( (int) lBufSize)) == NULL )
  269. DetectExit( NO_MEMORY );
  270. Buffer = malloc(64);
  271. _fstrcpy(Buffer, szPath);
  272. if ( _dos_open( Buffer, O_RDONLY, &iFile ) == OK )
  273. iMatch = ScanFile( iFile, apszText, Buf,
  274. (unsigned int)lBufSize );
  275. else
  276. iMatch = -1;
  277. RpcClose( iFile );
  278. free(Buffer);
  279. _ffree( Buf );
  280. return( iMatch );
  281. } /* end MultStrMatch */
  282. unsigned RemoveSpaces( char *szString )
  283. {
  284. char *szPtr, *szStart;
  285. szStart = szPtr = szString;
  286. while( *szPtr != EOL )
  287. {
  288. if ( *szPtr != ' ' )
  289. *(szString++) = *(szPtr++);
  290. else
  291. szPtr++;
  292. }
  293. *szString = EOL;
  294. return( (unsigned)(szStart - szString) );
  295. } /* RemoveSpaces () */
  296. void ReplaceChar( char *szString, char chOldChar, char chNewChar )
  297. {
  298. while ( (szString = strchr( szString, chOldChar )) != NULL )
  299. *(szString++) = chNewChar;
  300. } /* ReplaceChar */
  301. void DetectExit( int iErr )
  302. {
  303. switch ( iErr )
  304. {
  305. case NO_MEMORY: // printf( "\nError: Insufficient memory" );
  306. break;
  307. default: break;
  308. }
  309. // RpcExit( -1 );
  310. } /* DetectExit () */
  311. long GetMaxHugeSize( void )
  312. {
  313. return(0xff00);
  314. }
  315. unsigned MaxStrLen( char _far * _far *Strings )
  316. {
  317. register i;
  318. unsigned Len;
  319. unsigned MaxLen;
  320. for ( i = MaxLen = 0; Strings[i] != NULL; i++ )
  321. {
  322. Len = _fstrlen( Strings[i] );
  323. if ( Len > MaxLen )
  324. MaxLen = Len;
  325. }
  326. return( MaxLen );
  327. }
  328. /***************************************************************************/
  329. /* Reads in sections of a file into a specified buffer and then scans the */
  330. /* the buffer for the first match in a list of strings. The scan will */
  331. /* continue until the entire file has been scanned or a matching string */
  332. /* has been found. */
  333. /* */
  334. /* int ScanFile( int iFile, char **apszText, char *Buf, unsigned BufSize ) */
  335. /* */
  336. /* ARGUMENTS: iFile - Open dos file handle from _dos_open() */
  337. /* apszText- Array of ptrs to string to search for */
  338. /* Buf - Ptr to disk read buffer */
  339. /* BufSize - Size of the disk read buffer in bytes */
  340. /* RETURNS: int - First matched string's index value or -1 if no */
  341. /* no match was found */
  342. /* */
  343. /***************************************************************************/
  344. int ScanFile( int iFile, char _far * _far *apszText, char far *Buf, unsigned BufSize )
  345. {
  346. register iMatch;
  347. register iCount;
  348. unsigned uToRead;
  349. static unsigned uRead;
  350. unsigned uOffset;
  351. char far *Ptr;
  352. uToRead = BufSize; /* Read as much as buffer will hold on first read */
  353. for ( iMatch = -1, iCount = 0, uOffset = 0, Ptr = Buf; /* Loop Init */
  354. iMatch == -1 && /* Condition 1 */
  355. _dos_read( iFile, Ptr, uToRead, &uRead ) == OK && /* Condition 2 */
  356. uRead != 0; /* Condition 3 */
  357. iCount++ ) /* Re-init */
  358. {
  359. if ((iMatch = MultScanBuf(Buf,apszText,uRead+uOffset) ) == -1 )
  360. {
  361. /* Need to move the last portion of the buffer */
  362. /* scanned to the begining of the buffer and do the */
  363. /* next read the end of this to avoid missing */
  364. /* a match if it was split by the last read */
  365. if (iCount == 0) /* See if this was the first read */
  366. {
  367. uOffset = MaxStrLen( apszText );
  368. Ptr += uOffset;
  369. uToRead -= uOffset;
  370. }
  371. if ( uRead > uOffset )/* Make sure not at end of file */
  372. _fmemmove( Buf, Buf + (uRead-uOffset), uOffset );
  373. else
  374. Ptr = Buf, uOffset = 0; /*
  375. * Only a small peice
  376. * left to do
  377. */
  378. }
  379. }
  380. return( iMatch );
  381. }
  382. int IsUbnet()
  383. {
  384. static int fDone = -1;
  385. static int fResult;
  386. if (-1 == fDone)
  387. {
  388. fDone = 0;
  389. inregs.x.ax = 0xdd05;
  390. inregs.x.bx = 0;
  391. RpcInt86( 0x2f, &inregs, &inregs );
  392. fResult = ( inregs.x.ax != 0xdd05 );
  393. }
  394. return fResult;
  395. } /* end IsUbnet() */
  396. int IsIBMLan()
  397. {
  398. static int fDone = -1;
  399. static int fResult;
  400. if (-1 == fDone)
  401. {
  402. fDone = 0;
  403. inregs.x.ax = 0xb800;
  404. RpcInt86( 0x2f, &inregs, &inregs );
  405. fResult = ( inregs.h.al == 0xff );
  406. }
  407. return fResult;
  408. } /* end IsIBMLan() */
  409. unsigned long Bcd ( unsigned long lInput, unsigned iFrom, unsigned iTo )
  410. {
  411. unsigned long lResult=0L;
  412. unsigned iBase=1;
  413. do
  414. {
  415. lResult += (lInput % 10) * iBase;
  416. lInput /= iFrom;
  417. iBase *= iTo;
  418. } while (0 != lInput);
  419. return lResult;
  420. } /* end Bcd */