Source code of Windows XP (NT5)
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.

440 lines
9.0 KiB

  1. #ifdef RLWIN32
  2. #include <windows.h>
  3. #else
  4. #ifdef RLWIN16
  5. #include <windows.h>
  6. //#include <ntimage.h>
  7. //#else // DOS BUILD
  8. //#include <ntimage.h>
  9. #endif
  10. #endif
  11. // CRT includes
  12. #include <stdio.h>
  13. #include <stddef.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <tchar.h>
  17. #include <io.h>
  18. #include <time.h>
  19. //#include <sys\types.h>
  20. //#include <sys\stat.h>
  21. #ifdef RLDOS
  22. #include "dosdefs.h"
  23. #else
  24. #include "windefs.h"
  25. #endif
  26. #include "commbase.h"
  27. #include "restok.h"
  28. #include "tokenapi.h"
  29. extern UCHAR szDHW[];
  30. /**
  31. * Function: StripNewLine
  32. * Replaces new line characters with nulls
  33. *
  34. * Arguments:
  35. * sz, string to be stripped
  36. *
  37. * Returns:
  38. * nothing
  39. *
  40. * Error Codes:
  41. * none
  42. *
  43. * History:
  44. * 2/92, Implemented SteveBl
  45. * 10/92, Simplified to only check last char in non-empty string - DaveWi
  46. */
  47. void StripNewLineA( CHAR *sz)
  48. {
  49. int i;
  50. if ( sz && (i = lstrlenA( sz)) > 0 )
  51. {
  52. if ( sz[ --i] == '\n' )
  53. {
  54. sz[i] = 0;
  55. }
  56. }
  57. }
  58. //.....................................................
  59. //...
  60. //... A Unicode NewLine is TEXT("\r\n") - two separate characters
  61. void StripNewLineW( LPWSTR sz)
  62. {
  63. int i = lstrlenW( sz);
  64. if ( i > 0 && sz[ --i] == TEXT('\n') )
  65. {
  66. sz[i] = TEXT('\0');
  67. if ( i > 0 && sz[ --i] == TEXT('\r') )
  68. {
  69. sz[i] = TEXT('\0');
  70. }
  71. }
  72. }
  73. //+-------------------------------------------------------------------------
  74. //
  75. // Function: IsExe, Public
  76. //
  77. // Synopsis: Determines if the specified file is an executable image file
  78. //
  79. //
  80. // Arguments: [szFileName] The name of the file to determine whether it is an exe
  81. //
  82. //
  83. // Effects:
  84. //
  85. // Returns: -1 Error Condition
  86. // NOTEXE File is not an exe
  87. // WIN16EXE File is a Win 16 exe
  88. // NTEXE File is a win 32 exe
  89. // UNKNOWEXE File is not a valid exe
  90. //
  91. // Modifies:
  92. //
  93. // History:
  94. // 10-Oct-92 Created TerryRu
  95. //
  96. //
  97. // Notes:
  98. //
  99. //--------------------------------------------------------------------------
  100. int IsExe( CHAR *szFileName )
  101. {
  102. static IMAGE_DOS_HEADER DosHeader;
  103. static IMAGE_OS2_HEADER ImageNeFileHdr;
  104. static IMAGE_FILE_HEADER ImageFileHdr;
  105. static IMAGE_OPTIONAL_HEADER ImageOptionalHdr;
  106. DWORD neSignature;
  107. FILE *fIn = NULL;
  108. WORD rc;
  109. if ( (fIn = FOPEN( szFileName, "rb")) == NULL )
  110. {
  111. return ( -1 );
  112. }
  113. if ( ResReadBytes( fIn,
  114. (char *)&DosHeader,
  115. sizeof( IMAGE_DOS_HEADER),
  116. NULL) == FALSE
  117. || (DosHeader.e_magic != IMAGE_DOS_SIGNATURE ))
  118. {
  119. FCLOSE( fIn);
  120. return( NOTEXE);
  121. }
  122. // 1st byte was a valid signature, and we were able to read a DOS Hdr
  123. // now seek to address of new exe header
  124. if ( fseek( fIn, DosHeader.e_lfanew, SEEK_SET))
  125. {
  126. FCLOSE( fIn);
  127. return( NOTEXE);
  128. }
  129. // assume file is a Win 16 file,
  130. // Read the NT signature
  131. neSignature = (WORD) GetWord( fIn, NULL );
  132. if ( neSignature == IMAGE_OS2_SIGNATURE )
  133. {
  134. // return signature into input stream,
  135. // and read ne header as a whole
  136. UnGetWord( fIn, (WORD) neSignature, NULL );
  137. if ( ResReadBytes( fIn,
  138. (char *)&ImageNeFileHdr,
  139. sizeof( IMAGE_OS2_HEADER),
  140. NULL) == FALSE )
  141. {
  142. FCLOSE( fIn);
  143. return( NOTEXE);
  144. }
  145. // determine if file is a WIN 16 Image File
  146. if ( ImageNeFileHdr.ne_ver >= 4 && ImageNeFileHdr.ne_exetyp == 2 )
  147. {
  148. FCLOSE( fIn);
  149. return( WIN16EXE);
  150. }
  151. }
  152. // not a win 16 exe, check for a NT exe.
  153. UnGetWord( fIn, (WORD) neSignature, NULL );
  154. neSignature = GetdWord( fIn, NULL );
  155. if ( neSignature == IMAGE_NT_SIGNATURE )
  156. {
  157. if ( ResReadBytes( fIn,
  158. (char *)&ImageFileHdr,
  159. sizeof( IMAGE_FILE_HEADER),
  160. NULL) == FALSE )
  161. {
  162. FCLOSE( fIn);
  163. return( NOTEXE);
  164. }
  165. if ( ImageFileHdr.SizeOfOptionalHeader )
  166. {
  167. // read the optional header only to validate the ImageFileHeader
  168. // we currently don\'t use any info in the Optional Header
  169. if ( ImageFileHdr.SizeOfOptionalHeader
  170. > sizeof( IMAGE_OPTIONAL_HEADER ) )
  171. {
  172. FCLOSE( fIn);
  173. return( NOTEXE);
  174. }
  175. if ( ResReadBytes( fIn,
  176. (char *)&ImageOptionalHdr,
  177. (size_t)min( sizeof( IMAGE_OPTIONAL_HEADER),
  178. ImageFileHdr.SizeOfOptionalHeader),
  179. NULL) == FALSE )
  180. {
  181. FCLOSE( fIn);
  182. return( NOTEXE);
  183. }
  184. }
  185. // determine if file is an executable image file
  186. if ( (ImageFileHdr.Characteristics & IMAGE_FILE_EXECUTABLE_IMAGE) ||
  187. (ImageFileHdr.Characteristics & IMAGE_FILE_DLL) )
  188. {
  189. FCLOSE( fIn);
  190. return( NTEXE);
  191. }
  192. else
  193. {
  194. FCLOSE( fIn);
  195. return( NOTEXE);
  196. }
  197. }
  198. FCLOSE( fIn);
  199. // did not regonize signature type
  200. return( NOTEXE);
  201. }
  202. BOOL IsWin32Res( CHAR * szFileName)
  203. {
  204. BOOL fRC = FALSE;
  205. if ( IsRes( szFileName) )
  206. {
  207. FILE *pF = fopen( szFileName, "rb");
  208. if ( pF )
  209. {
  210. DWORD dwSize = GetdWord( pF, NULL);
  211. fclose( pF);
  212. fRC = (dwSize == 0L) ? TRUE : FALSE;
  213. }
  214. else
  215. {
  216. fRC = FALSE;
  217. }
  218. }
  219. else
  220. {
  221. fRC = FALSE;
  222. }
  223. return( fRC);
  224. }
  225. //+-------------------------------------------------------------------------
  226. //
  227. // Function: IsRes, Public
  228. //
  229. // Synopsis: Determines if the specified file has a .RES extention.
  230. //
  231. //
  232. // Arguments: [szFileName] The name of the file to determine whether it is a res
  233. //
  234. //
  235. // Effects:
  236. //
  237. // Returns: TRUE, File has a .RES extention
  238. // FALSE, File does not have a .RES extention
  239. //
  240. // Modifies:
  241. //
  242. // History:
  243. // 16-Oct-92 Created TerryRu
  244. //
  245. //
  246. // Notes:
  247. //
  248. //--------------------------------------------------------------------------
  249. BOOL IsRes( CHAR *szFileName)
  250. {
  251. int i = lstrlenA( szFileName);
  252. return( (i > 4 && lstrcmpiA( szFileName + i - 4, ".RES") == 0) ? TRUE : FALSE );
  253. }
  254. /**
  255. * Function TranslateFileTime
  256. * Translates a Win32 filetime structure into a useful string
  257. * representation.
  258. *
  259. * Arguments:
  260. * sz, destination buffer (ANSI string)
  261. * ft, file time structure
  262. *
  263. * Returns:
  264. * sring representation of date/time in sz
  265. *
  266. * History:
  267. * 7/92 implemented SteveBl
  268. */
  269. #ifdef RLWIN32
  270. void TranslateFileTime(CHAR *sz, FILETIME ft)
  271. {
  272. sprintf(sz,"FILETIME STRUCTURE: %Lu:%Lu",ft.dwHighDateTime,ft.dwLowDateTime);
  273. }
  274. #endif
  275. /**
  276. * Function: SzDateFromFileName
  277. * Returns a string containing the time and date stamp on a file.
  278. *
  279. * Arguments:
  280. * sz, destination buffer
  281. * szFile, path to file
  282. *
  283. * Returns:
  284. * date and time in sz
  285. *
  286. * Error Codes:
  287. * none (but leaves sz empty)
  288. *
  289. * Comments:
  290. * Assumes sz is large enough for date string.
  291. *
  292. * History:
  293. * 2/92, Implemented SteveBl
  294. */
  295. void SzDateFromFileName(CHAR *sz,CHAR *szFile)
  296. {
  297. #ifdef RLWIN32
  298. HANDLE hFile;
  299. WCHAR szt[MAXFILENAME];
  300. _MBSTOWCS( szt,
  301. szFile,
  302. WCHARSIN( sizeof( szt)),
  303. ACHARSIN( lstrlenA( szFile) + 1));
  304. hFile = CreateFile( szt,
  305. GENERIC_READ,
  306. FILE_SHARE_READ,
  307. NULL,
  308. OPEN_EXISTING,
  309. 0,
  310. NULL);
  311. if ( hFile != (HANDLE)-1 )
  312. {
  313. FILETIME ft;
  314. GetFileTime( hFile, NULL, NULL, &ft);
  315. TranslateFileTime( sz, ft);
  316. CloseHandle( hFile);
  317. }
  318. #else //RLWIN32
  319. struct _stat s;
  320. if (!_stat(szFile,&s))
  321. {
  322. sprintf(sz,"%s",ctime(&s.st_atime));
  323. StripNewLine(sz);
  324. }
  325. else
  326. {
  327. sz[0] = 0;
  328. }
  329. #endif
  330. }
  331. //..........................................................................
  332. #ifdef _DEBUG
  333. FILE * MyFopen( char * pszFileName, char * pszMode, char * pszFile, int nLine)
  334. #else
  335. FILE * MyFopen( char * pszFileName, char * pszMode)
  336. #endif
  337. {
  338. FILE *pfRC = NULL;
  339. //#ifdef _DEBUG
  340. // fprintf( stderr, "fopening \"%s\" at %d in %s",
  341. // pszFileName,
  342. // nLine,
  343. // pszFile);
  344. //#endif
  345. pfRC = fopen( pszFileName, pszMode);
  346. //#ifdef _DEBUG
  347. // fprintf( stderr, ": FILE ptr = %p\n", pfRC);
  348. //#endif
  349. return( pfRC);
  350. }
  351. //..........................................................................
  352. #ifdef _DEBUG
  353. int MyClose( FILE **pf, char * pszFile, int nLine)
  354. #else
  355. int MyClose( FILE **pf)
  356. #endif
  357. {
  358. int nRC = 0;
  359. //#ifdef _DEBUG
  360. // fprintf( stderr, "\tclosing %p at %d in %s\n", *pf, nLine, pszFile);
  361. //#endif
  362. if ( *pf )
  363. {
  364. nRC = fclose( *pf);
  365. *pf = NULL;
  366. }
  367. return( nRC);
  368. }