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.

379 lines
8.3 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1996 - 1999
  6. //
  7. // File: prsparse.cpp
  8. //
  9. // Contents: Microsoft Internet Security Internal Utility
  10. //
  11. // Functions: main
  12. //
  13. // *** local functions ***
  14. // GetLine
  15. // EOLOut
  16. // ReformatLine
  17. // ParseAndReformatLine2
  18. // AddDefaultsForLine2
  19. // AddPRSNumber
  20. //
  21. // History: 20-Aug-1997 pberkman created
  22. //
  23. //--------------------------------------------------------------------------
  24. #include <stdio.h>
  25. #include <windows.h>
  26. #include <io.h>
  27. #define MAX_PRS_LINE 1024
  28. #define PRS_LINE1_NUMPARAMS 4
  29. #define PRS_FILE "PRS.TXT"
  30. DWORD GetLine(HANDLE hFile, char *pszBuf, DWORD cbMaxRead);
  31. void EOLOut(char *psz, DWORD ccLen);
  32. void ReformatLine(char *pszIn, char *pszOut, DWORD cbMax);
  33. void ParseAndReformatLine2(char *pszIn, char *pszOut, DWORD cbMax);
  34. void AddDefaultsForLine2(char *pszOut, DWORD cbMax);
  35. void AddPRSNumber(char *pszOut, DWORD cbMax);
  36. HANDLE hPRSFile;
  37. extern "C" int __cdecl main(int argc, char **argv)
  38. {
  39. if (argc < 3)
  40. {
  41. printf("\nUsage: %s infile outfile\n", argv[0]);
  42. return(0);
  43. }
  44. HANDLE hFileIn;
  45. HANDLE hFileOut;
  46. char szBufIn[MAX_PRS_LINE];
  47. char szBufOut[MAX_PRS_LINE];
  48. char *psz;
  49. DWORD cbWritten;
  50. if ((hFileIn = CreateFile(argv[1], GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
  51. FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE)
  52. {
  53. return(0);
  54. }
  55. if ((hFileOut = CreateFile(argv[2], GENERIC_WRITE | GENERIC_READ, 0, NULL, CREATE_ALWAYS,
  56. FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE)
  57. {
  58. CloseHandle(hFileIn);
  59. return(0);
  60. }
  61. strcpy(&szBufIn[0], argv[1]);
  62. if (psz = strrchr(&szBufIn[0], '\\'))
  63. {
  64. psz++;
  65. }
  66. else if (psz = strrchr(&szBufIn[0], ':'))
  67. {
  68. psz++;
  69. }
  70. else
  71. {
  72. psz = &szBufIn[0];
  73. }
  74. strcpy(psz, PRS_FILE);
  75. hPRSFile = CreateFile(&szBufIn[0], GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
  76. FILE_ATTRIBUTE_NORMAL, NULL);
  77. szBufOut[0] = NULL;
  78. while (GetLine(hFileIn, &szBufIn[0], MAX_PRS_LINE) > 0)
  79. {
  80. EOLOut(&szBufIn[0], strlen(&szBufIn[0]) + 4);
  81. if ((szBufOut[0]) && (szBufIn[0] == '-'))
  82. {
  83. //
  84. // line continues... the second line needs to be parsed...
  85. //
  86. ParseAndReformatLine2(&szBufIn[0], &szBufOut[0], MAX_PRS_LINE);
  87. }
  88. else if (szBufOut[0])
  89. {
  90. AddDefaultsForLine2(&szBufOut[0], MAX_PRS_LINE);
  91. }
  92. if (szBufOut[0])
  93. {
  94. cbWritten = 0;
  95. WriteFile(hFileOut, &szBufOut[0], strlen(&szBufOut[0]), &cbWritten, NULL);
  96. szBufOut[0] = NULL;
  97. continue;
  98. }
  99. if ((szBufIn[0] == ';') || (szBufIn[0] == '#') || !(szBufIn[0]) || (szBufIn[0] == ' '))
  100. {
  101. continue;
  102. }
  103. ReformatLine(&szBufIn[0], &szBufOut[0], MAX_PRS_LINE);
  104. }
  105. CloseHandle(hFileIn);
  106. CloseHandle(hFileOut);
  107. if (hPRSFile != INVALID_HANDLE_VALUE)
  108. {
  109. CloseHandle(hPRSFile);
  110. }
  111. return(1);
  112. }
  113. void ReformatLine(char *pszIn, char *pszOut, DWORD cbMax)
  114. {
  115. int idxIn;
  116. int idxOut;
  117. int len;
  118. int params;
  119. params = 1;
  120. idxOut = 0;
  121. pszOut[0] = NULL;
  122. len = strlen(pszIn);
  123. if (len > 0)
  124. {
  125. pszOut[idxOut++] = '\"';
  126. pszOut[idxOut] = NULL;
  127. }
  128. for (idxIn = 0; idxIn < len; idxIn++)
  129. {
  130. if (pszIn[idxIn] == ',')
  131. {
  132. if (pszIn[idxIn - 1] != '\"')
  133. {
  134. pszOut[idxOut++] = '\"';
  135. pszOut[idxOut] = NULL;
  136. }
  137. pszOut[idxOut++] = ',';
  138. pszOut[idxOut] = NULL;
  139. if (pszIn[idxIn + 1] != '\"')
  140. {
  141. pszOut[idxOut++] = '\"';
  142. pszOut[idxOut] = NULL;
  143. }
  144. params++;
  145. }
  146. else
  147. {
  148. pszOut[idxOut++] = pszIn[idxIn];
  149. }
  150. }
  151. if (params < PRS_LINE1_NUMPARAMS)
  152. {
  153. for (idxIn = params; idxIn < PRS_LINE1_NUMPARAMS; idxIn++)
  154. {
  155. pszOut[idxOut++] = ',';
  156. pszOut[idxOut++] = '\"';
  157. pszOut[idxOut++] = '\"';
  158. }
  159. }
  160. if (len > 0)
  161. {
  162. if (pszOut[idxOut - 1] != '\"')
  163. {
  164. pszOut[idxOut++] = '\"';
  165. }
  166. pszOut[idxOut] = NULL;
  167. }
  168. }
  169. void ParseAndReformatLine2(char *pszIn, char *pszOut, DWORD cbMax)
  170. {
  171. int idxIn;
  172. int idxOut;
  173. int len;
  174. int params;
  175. params = PRS_LINE1_NUMPARAMS;
  176. idxOut = strlen(pszOut);
  177. pszOut[idxOut] = NULL;
  178. len = strlen(pszIn);
  179. if (len > 0)
  180. {
  181. pszOut[idxOut++] = ',';
  182. if (pszIn[1] != '\"')
  183. {
  184. pszOut[idxOut++] = '\"';
  185. }
  186. pszOut[idxOut] = NULL;
  187. }
  188. for (idxIn = 1; idxIn < len; idxIn++) // idxIn = 1: pass over '-'
  189. {
  190. if (pszIn[idxIn] == ',')
  191. {
  192. if (pszIn[idxIn - 1] != '\"')
  193. {
  194. pszOut[idxOut++] = '\"';
  195. pszOut[idxOut] = NULL;
  196. }
  197. pszOut[idxOut++] = ',';
  198. pszOut[idxOut] = NULL;
  199. if (pszIn[idxIn + 1] != '\"')
  200. {
  201. pszOut[idxOut++] = '\"';
  202. pszOut[idxOut] = NULL;
  203. }
  204. params++;
  205. }
  206. else
  207. {
  208. pszOut[idxOut++] = pszIn[idxIn];
  209. }
  210. }
  211. if (len > 0)
  212. {
  213. if (pszIn[idxIn - 1] != '\"')
  214. {
  215. pszOut[idxOut++] = '\"';
  216. }
  217. pszOut[idxOut] = NULL;
  218. }
  219. AddPRSNumber(pszOut, cbMax);
  220. strcat(pszOut, "\r\n");
  221. }
  222. void AddDefaultsForLine2(char *pszOut, DWORD cbMax)
  223. {
  224. strcat(&pszOut[0], ",\"PN:\",\"SET:\",\"VN:\",\"MV:\"");
  225. AddPRSNumber(pszOut, cbMax);
  226. strcat(pszOut, "\r\n");
  227. }
  228. void AddPRSNumber(char *pszOut, DWORD cbMax)
  229. {
  230. if (hPRSFile != INVALID_HANDLE_VALUE)
  231. {
  232. char szRead[MAX_PATH];
  233. while (GetLine(hPRSFile, &szRead[0], MAX_PATH) > 0)
  234. {
  235. EOLOut(&szRead[0], strlen(&szRead[0]) + 4);
  236. if ((szRead[0] == ';') || (szRead[0] == '#') || !(szRead[0]) || (szRead[0] == ' '))
  237. {
  238. continue;
  239. }
  240. strcat(pszOut, ",\"JOBNO:");
  241. strcat(pszOut, &szRead[0]);
  242. strcat(pszOut, "\"");
  243. break;
  244. }
  245. }
  246. }
  247. DWORD GetLine(HANDLE hFile, char *pszBuf, DWORD cbMaxRead)
  248. {
  249. DWORD dwHold;
  250. DWORD cbRead;
  251. DWORD dw;
  252. int iAmt;
  253. pszBuf[0] = NULL;
  254. if ((dwHold = SetFilePointer(hFile, 0, NULL, FILE_CURRENT)) == 0xFFFFFFFF)
  255. {
  256. return(0);
  257. }
  258. cbRead = 0;
  259. if (ReadFile(hFile, pszBuf, cbMaxRead, &cbRead, NULL))
  260. {
  261. if (cbRead == 0)
  262. {
  263. return(0);
  264. }
  265. pszBuf[cbRead] = 0x00;
  266. if (cbRead > 0)
  267. {
  268. iAmt = 0;
  269. for (dw = 0; dw < (cbRead - 1); dw++)
  270. {
  271. if ((pszBuf[dw] == 0x0d) ||
  272. (pszBuf[dw] == 0x0a))
  273. {
  274. iAmt++;
  275. if (pszBuf[dw + 1] == 0x0a)
  276. {
  277. dw++;
  278. iAmt++;
  279. }
  280. SetFilePointer(hFile, dwHold + (dw + 1), NULL, FILE_BEGIN);
  281. pszBuf[dw + 1] = 0x00;
  282. return(cbRead + 1);
  283. }
  284. }
  285. }
  286. }
  287. else
  288. {
  289. return(0);
  290. }
  291. if (pszBuf[cbRead - 1] == 0x1a) /* EOF */
  292. {
  293. cbRead--;
  294. }
  295. return(cbRead);
  296. }
  297. void EOLOut(char *psz, DWORD ccLen)
  298. {
  299. DWORD i;
  300. for (i = 0; i < ccLen; i++)
  301. {
  302. if ((psz[i] == 0x0a) || (psz[i] == 0x0d))
  303. {
  304. psz[i] = NULL;
  305. return;
  306. }
  307. }
  308. psz[ccLen] = NULL;
  309. }