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.

379 lines
7.1 KiB

  1. //--------------------------------------------------------------------------;
  2. //
  3. // File: Binres.c
  4. //
  5. // Copyright (C) Microsoft Corporation, 1994 - 1996 All rights reserved
  6. //
  7. // Abstract:
  8. // This creates a binary that prepares arbitrary files as resources
  9. // for Windows binaries.
  10. //
  11. // Contents:
  12. // parse_cmdline()
  13. // create_file()
  14. // main()
  15. //
  16. // History:
  17. // 04/10/94 Fwong Created for binary resources.
  18. //
  19. //--------------------------------------------------------------------------;
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <malloc.h>
  23. #include <fcntl.h>
  24. #include <io.h>
  25. //==========================================================================;
  26. //
  27. // Constants...
  28. //
  29. //==========================================================================;
  30. #define BYTE unsigned char
  31. #define STRING_SIZE 144
  32. #define BUFFER_SIZE 2048
  33. #define ERROR 1
  34. #define NO_ERROR 0
  35. //==========================================================================;
  36. //
  37. // Globals...
  38. //
  39. //==========================================================================;
  40. char szInputFile[STRING_SIZE];
  41. char szOutputFile[STRING_SIZE];
  42. //--------------------------------------------------------------------------;
  43. //
  44. // int parse_cmdline
  45. //
  46. // Description:
  47. // Parses command line and fills in input and output file names.
  48. //
  49. // Arguments:
  50. // char *pszInputFile: Buffer for input file name.
  51. //
  52. // char *pszOutputFile: Buffer for output file name.
  53. //
  54. // char *pszArg1: First argument.
  55. //
  56. // char *pszArg2: Second argument.
  57. //
  58. // Return (int):
  59. // ERROR if error occurs, NO_ERROR otherwise.
  60. //
  61. // History:
  62. // 04/10/94 Fwong Created for binary resources.
  63. //
  64. //--------------------------------------------------------------------------;
  65. int parse_cmdline
  66. (
  67. char *pszInputFile,
  68. char *pszOutputFile,
  69. char *pszArg1,
  70. char *pszArg2
  71. )
  72. {
  73. char szScrap[STRING_SIZE];
  74. //
  75. // Setting both filenames to NULL strings.
  76. //
  77. pszInputFile[0] = 0;
  78. pszOutputFile[0] = 0;
  79. //
  80. // Checking first argument...
  81. //
  82. strcpy(szScrap,pszArg1);
  83. //
  84. // ASCII trick, allows '/' and '-' to be synonymous
  85. //
  86. szScrap[0] = (szScrap[0]|(0x02));
  87. strlwr(szScrap);
  88. if(0 == strncmp(szScrap,"/out:",5))
  89. {
  90. strcpy(pszOutputFile,((char*)(&szScrap[5])));
  91. }
  92. if(0 == strncmp(szScrap,"/in:",4))
  93. {
  94. strcpy(pszInputFile,((char*)(&szScrap[4])));
  95. }
  96. //
  97. // Checking second argument...
  98. //
  99. strcpy(szScrap,pszArg2);
  100. //
  101. // ASCII trick, allows '/' and '-' to be synonymous
  102. //
  103. szScrap[0] = (szScrap[0]|(0x02));
  104. strlwr(szScrap);
  105. if(0 == strncmp(szScrap,"/out:",5))
  106. {
  107. strcpy(pszOutputFile,((char*)(&szScrap[5])));
  108. }
  109. if(0 == strncmp(szScrap,"/in:",4))
  110. {
  111. strcpy(pszInputFile,((char*)(&szScrap[4])));
  112. }
  113. //
  114. // Did we miss one?
  115. //
  116. if(0 == pszInputFile[0])
  117. {
  118. return ERROR;
  119. }
  120. if(0 == pszOutputFile[0])
  121. {
  122. return ERROR;
  123. }
  124. //
  125. // No error.
  126. //
  127. return NO_ERROR;
  128. } // parse_cmdline()
  129. //--------------------------------------------------------------------------;
  130. //
  131. // int create_file
  132. //
  133. // Description:
  134. // Given input and output files names, creates the output file.
  135. // This function does the *real* work of the program.
  136. //
  137. // Arguments:
  138. // char *pszInputFile: Input file name.
  139. //
  140. // char *pszOutputFile: Output file name.
  141. //
  142. // Return (int):
  143. // ERROR if error occurs, NO_ERROR otherwise.
  144. //
  145. // History:
  146. // 04/10/94 Fwong Created for binary resources.
  147. //
  148. //--------------------------------------------------------------------------;
  149. int create_file
  150. (
  151. char *pszInputFile,
  152. char *pszOutputFile
  153. )
  154. {
  155. FILE *pIFile;
  156. FILE *pOFile;
  157. size_t cbCount;
  158. long cbSize;
  159. long cbWritten;
  160. BYTE *pBuffer;
  161. //
  162. // Allocating transfer buffer...
  163. //
  164. pBuffer = (BYTE*)malloc(BUFFER_SIZE);
  165. if(NULL == pBuffer)
  166. {
  167. return ERROR;
  168. }
  169. //
  170. // Opening input file...
  171. //
  172. pIFile = fopen(pszInputFile,"rb");
  173. if(NULL == pIFile)
  174. {
  175. free(pBuffer);
  176. return ERROR;
  177. }
  178. //
  179. // Getting size...
  180. //
  181. cbSize = filelength(fileno(pIFile));
  182. if((-1) == cbSize)
  183. {
  184. fclose(pIFile);
  185. free(pBuffer);
  186. return ERROR;
  187. }
  188. if(0 != fseek(pIFile,0,SEEK_SET))
  189. {
  190. fclose(pIFile);
  191. free(pBuffer);
  192. return ERROR;
  193. }
  194. //
  195. // Opening output file...
  196. //
  197. pOFile = fopen(pszOutputFile,"wb");
  198. if(NULL == pOFile)
  199. {
  200. fclose(pIFile);
  201. free(pBuffer);
  202. return ERROR;
  203. }
  204. //
  205. // First, let's write the size of file...
  206. //
  207. printf("File size (in bytes) of %s: %lu.\n",pszInputFile,cbSize);
  208. cbCount = fwrite(&cbSize,sizeof(cbSize),1,pOFile);
  209. if(1 != cbCount)
  210. {
  211. fclose(pOFile);
  212. fclose(pIFile);
  213. free(pBuffer);
  214. return ERROR;
  215. }
  216. //
  217. // Next, let's write the actual data...
  218. // Note: Counting that cbCount != 0
  219. //
  220. for(cbWritten=0;cbCount;)
  221. {
  222. //
  223. // Reading data...
  224. //
  225. cbCount = fread(pBuffer,1,BUFFER_SIZE,pIFile);
  226. if(0 == cbCount)
  227. {
  228. //
  229. // No more data?!
  230. //
  231. break;
  232. }
  233. //
  234. // Writing data...
  235. //
  236. if(cbCount != fwrite(pBuffer,1,cbCount,pOFile))
  237. {
  238. fclose(pOFile);
  239. fclose(pIFile);
  240. free(pBuffer);
  241. return ERROR;
  242. }
  243. cbWritten += cbCount;
  244. }
  245. printf("Total bytes written to %s: %lu.\n",pszOutputFile,cbWritten);
  246. //
  247. // Cleaning up...
  248. //
  249. fclose(pOFile);
  250. fclose(pIFile);
  251. free(pBuffer);
  252. return NO_ERROR;
  253. } // create_file()
  254. //--------------------------------------------------------------------------;
  255. //
  256. // int main
  257. //
  258. // Description:
  259. // Typical character based "main" function for C.
  260. //
  261. // Arguments:
  262. // int argc: Number of command line arguments.
  263. //
  264. // char *argv[]: Value of command line arguments.
  265. //
  266. // char *envp[]: Value of enviroment variables.
  267. //
  268. // Return (int):
  269. // ERROR if error occurs, NO_ERROR otherwise.
  270. //
  271. // History:
  272. // 04/10/94 Fwong Created for binary resources.
  273. //
  274. //--------------------------------------------------------------------------;
  275. int main
  276. (
  277. int argc,
  278. char *argv[],
  279. char *envp[]
  280. )
  281. {
  282. if(3 != argc)
  283. {
  284. //
  285. // Incorrect number of parameters
  286. //
  287. printf("Usage: binres /in:InputFile /out:OutputFile");
  288. return ERROR;
  289. }
  290. if(0 != parse_cmdline(szInputFile,szOutputFile,argv[1],argv[2]))
  291. {
  292. //
  293. // Something wrong with command line?
  294. //
  295. return ERROR;
  296. }
  297. if(0 != create_file(szInputFile,szOutputFile))
  298. {
  299. //
  300. // Something wrong with one of the files?
  301. //
  302. return ERROR;
  303. }
  304. return NO_ERROR;
  305. } // main()