Leaked source code of windows server 2003
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.

270 lines
8.4 KiB

  1. /*++
  2. Copyright (c) 1993-1994 Microsoft Corporation
  3. Module Name:
  4. initodat.c
  5. Abstract:
  6. Routines for converting Perf???.ini to Perf???.dat files.
  7. Source INI files are localed under ..\perfini\<country> directories. Generated DAT files will
  8. be put under %SystemRoot%\System32 directory.
  9. Author:
  10. HonWah Chan (a-honwah) October, 1993
  11. Revision History:
  12. --*/
  13. #include "initodat.h"
  14. #include "strids.h"
  15. #include "common.h"
  16. BOOL
  17. MakeUpgradeFilename(
  18. LPCWSTR szDataFileName,
  19. LPWSTR szUpgradeFileName
  20. )
  21. {
  22. BOOL bReturn = FALSE;
  23. // note: assumes szUpgradeFileName buffer is large enough for result
  24. WCHAR szDrive[_MAX_DRIVE];
  25. WCHAR szDir[_MAX_DIR];
  26. WCHAR szFileName[_MAX_FNAME];
  27. WCHAR szExt[_MAX_EXT];
  28. _wsplitpath(szDataFileName, (LPWSTR) szDrive, (LPWSTR) szDir, (LPWSTR) szFileName, (LPWSTR) szExt);
  29. // see if the filename fits the "PERF[C|H]XXX" format
  30. if (szFileName[4] == L'C' || szFileName[4] == L'H' || szFileName[4] == L'c' || szFileName[4] == L'h') {
  31. // then it's the correct format so change the 4th letter up 1 letter
  32. szFileName[4] += 1;
  33. // and make a new path
  34. _wmakepath(szUpgradeFileName, (LPCWSTR) szDrive, (LPCWSTR) szDir, (LPCWSTR) szFileName, (LPCWSTR) szExt);
  35. bReturn = TRUE;
  36. }
  37. return bReturn;
  38. }
  39. BOOL
  40. GetFilesFromCommandLine(
  41. LPWSTR lpCommandLine,
  42. #ifdef FE_SB
  43. UINT * puCodePage,
  44. #endif
  45. LPWSTR lpFileNameI,
  46. DWORD dwFileNameI,
  47. LPWSTR lpFileNameD,
  48. DWORD dwFileNameD
  49. )
  50. /*++
  51. GetFilesFromCommandLine
  52. parses the command line to retrieve the ini filename that should be
  53. the first and only argument.
  54. Arguments
  55. lpCommandLine pointer to command line (returned by GetCommandLine)
  56. lpFileNameI pointer to buffer that will receive address of the
  57. validated input filename entered on the command line
  58. lpFileNameD pointer to buffer that will receive address of the
  59. optional output filename entered on the command line
  60. Return Value
  61. TRUE if a valid filename was returned
  62. FALSE if the filename is not valid or missing
  63. error is returned in GetLastError
  64. --*/
  65. {
  66. INT iNumArgs;
  67. HFILE hIniFile;
  68. OFSTRUCT ofIniFile;
  69. CHAR lpIniFileName[FILE_NAME_BUFFER_SIZE];
  70. WCHAR lpExeName[FILE_NAME_BUFFER_SIZE];
  71. WCHAR lpIniName[FILE_NAME_BUFFER_SIZE];
  72. BOOL bReturn = FALSE;
  73. // check for valid arguments
  74. if (lpCommandLine == NULL || lpFileNameI == NULL || lpFileNameD == NULL) {
  75. goto Cleanup;
  76. }
  77. // get strings from command line
  78. #ifdef FE_SB
  79. iNumArgs = swscanf(lpCommandLine, L" %s %d %s %s ", lpExeName, puCodePage, lpIniName, lpFileNameD);
  80. #else
  81. iNumArgs = swscanf(lpCommandLine, L" %s %s %s ", lpExeName, lpIniName, lpFileNameD);
  82. #endif
  83. #ifdef FE_SB
  84. if (iNumArgs < 3 || iNumArgs > 4) {
  85. #else
  86. if (iNumArgs < 2 || iNumArgs > 3) {
  87. #endif
  88. // wrong number of arguments
  89. goto Cleanup;
  90. }
  91. // see if file specified exists
  92. // file name is always an ANSI buffer
  93. WideCharToMultiByte(CP_ACP, 0, lpIniName, -1, lpIniFileName, FILE_NAME_BUFFER_SIZE, NULL, NULL);
  94. hIniFile = OpenFile(lpIniFileName, & ofIniFile, OF_PARSE);
  95. if (hIniFile != HFILE_ERROR) {
  96. _lclose(hIniFile);
  97. hIniFile = OpenFile(lpIniFileName, & ofIniFile, OF_EXIST);
  98. if ((hIniFile && hIniFile != HFILE_ERROR) || GetLastError() == ERROR_FILE_EXISTS) {
  99. // file exists, so return name and success
  100. // return full pathname if found
  101. MultiByteToWideChar(CP_ACP, 0, ofIniFile.szPathName, -1, lpFileNameI, dwFileNameI);
  102. bReturn = TRUE;
  103. _lclose(hIniFile);
  104. }
  105. else {
  106. // filename was on command line, but not valid so return
  107. // false, but send name back for error message
  108. MultiByteToWideChar(CP_ACP, 0, lpIniFileName, -1, lpFileNameI, dwFileNameI);
  109. if (hIniFile && hIniFile != HFILE_ERROR) _lclose(hIniFile);
  110. }
  111. }
  112. Cleanup:
  113. return bReturn;
  114. }
  115. BOOL
  116. VerifyIniData(
  117. PVOID pValueBuffer,
  118. ULONG ValueLength
  119. )
  120. /*++
  121. VerifyIniData
  122. This routine does some simple check to see if the ini file is good.
  123. Basically, it is looking for (ID, Text) and checking that ID is an
  124. integer. Mostly in case of missing comma or quote, the ID will be
  125. an invalid integer.
  126. --*/
  127. {
  128. INT iNumArg;
  129. INT TextID;
  130. LPWSTR lpID = NULL;
  131. LPWSTR lpText = NULL;
  132. LPWSTR lpLastID;
  133. LPWSTR lpLastText;
  134. LPWSTR lpInputBuffer = (LPWSTR) pValueBuffer;
  135. LPWSTR lpBeginBuffer = (LPWSTR) pValueBuffer;
  136. BOOL returnCode = TRUE;
  137. UINT NumOfID = 0;
  138. ULONG CurrentLength;
  139. while (TRUE) {
  140. // save up the last items for summary display later
  141. lpLastID = lpID;
  142. lpLastText = lpText;
  143. // increment to next ID and text location
  144. lpID = lpInputBuffer;
  145. CurrentLength = (ULONG) ((PBYTE) lpID - (PBYTE) lpBeginBuffer + sizeof(WCHAR));
  146. if (CurrentLength >= ValueLength) break;
  147. CurrentLength += lstrlenW(lpID) + 1;
  148. if (CurrentLength >= ValueLength) break;
  149. lpText = lpID + lstrlenW(lpID) + 1;
  150. CurrentLength += lstrlenW(lpText) + 1;
  151. if (CurrentLength >= ValueLength) break;
  152. lpInputBuffer = lpText + lstrlenW(lpText) + 1;
  153. iNumArg = swscanf(lpID, L"%d", & TextID);
  154. if (iNumArg != 1) {
  155. // bad ID
  156. returnCode = FALSE;
  157. break;
  158. }
  159. NumOfID ++;
  160. }
  161. if (returnCode == FALSE) {
  162. DisplaySummaryError(lpLastID, lpLastText, NumOfID);
  163. }
  164. else {
  165. DisplaySummary(lpLastID, lpLastText, NumOfID);
  166. }
  167. return (returnCode);
  168. }
  169. __cdecl main(
  170. )
  171. /*++
  172. main
  173. Arguments
  174. ReturnValue
  175. 0 (ERROR_SUCCESS) if command was processed
  176. Non-Zero if command error was detected.
  177. --*/
  178. {
  179. LPWSTR lpCommandLine;
  180. WCHAR lpIniFile[MAX_PATH];
  181. WCHAR lpDatFile[MAX_PATH];
  182. UNICODE_STRING IniFileName;
  183. PVOID pValueBuffer = NULL;
  184. ULONG ValueLength;
  185. BOOL bStatus;
  186. NTSTATUS NtStatus = ERROR_SUCCESS;
  187. #ifdef FE_SB
  188. UINT uCodePage = CP_ACP;
  189. #endif
  190. lpCommandLine = GetCommandLineW(); // get command line
  191. if (lpCommandLine == NULL) {
  192. NtStatus = GetLastError();
  193. goto Cleanup;
  194. }
  195. // read command line to determine what to do
  196. lpIniFile[0] = lpDatFile[0] = L'\0';
  197. #ifdef FE_SB // FE_SB
  198. if (GetFilesFromCommandLine(lpCommandLine, & uCodePage,
  199. lpIniFile, RTL_NUMBER_OF(lpIniFile), lpDatFile, RTL_NUMBER_OF(lpDatFile))) {
  200. if (! IsValidCodePage(uCodePage)) {
  201. uCodePage = CP_ACP;
  202. }
  203. #else
  204. if (GetFilesFromCommandLine(lpCommandLine,
  205. lpIniFile, RTL_NUMBER_OF(lpIniFile), lpDatFile, RTL_NUMBER_OF(lpDatFile))) {
  206. #endif // FE_SB
  207. // valid filename (i.e. ini file exists)
  208. RtlInitUnicodeString(& IniFileName, lpIniFile);
  209. #ifdef FE_SB
  210. NtStatus = DatReadMultiSzFile(uCodePage, & IniFileName, & pValueBuffer, & ValueLength);
  211. #else
  212. NtStatus = DatReadMultiSzFile(& IniFileName, & pValueBuffer, & ValueLength);
  213. #endif
  214. bStatus = NT_SUCCESS(NtStatus);
  215. if (bStatus) {
  216. bStatus = VerifyIniData(pValueBuffer, ValueLength);
  217. if (bStatus) {
  218. bStatus = OutputIniData(
  219. & IniFileName, lpDatFile, RTL_NUMBER_OF(lpDatFile), pValueBuffer, ValueLength);
  220. bStatus = MakeUpgradeFilename(lpDatFile, lpDatFile);
  221. if (bStatus) {
  222. bStatus = OutputIniData(
  223. & IniFileName, lpDatFile, RTL_NUMBER_OF(lpDatFile), pValueBuffer, ValueLength);
  224. }
  225. }
  226. }
  227. }
  228. else {
  229. if (* lpIniFile) {
  230. printf(GetFormatResource(LC_NO_INIFILE), lpIniFile);
  231. }
  232. else {
  233. //Incorrect Command Format
  234. // display command line usage
  235. DisplayCommandHelp(LC_FIRST_CMD_HELP, LC_LAST_CMD_HELP);
  236. }
  237. }
  238. Cleanup:
  239. if (pValueBuffer != NULL) FREEMEM(pValueBuffer);
  240. return (NtStatus); // success
  241. }