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.

378 lines
7.5 KiB

  1. #include <windows.h>
  2. #include <fcntl.h>
  3. #include <io.h>
  4. #include <sys\types.h>
  5. #include <sys\stat.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <stdio.h>
  9. int
  10. dump(
  11. char *buffer,
  12. unsigned buflen,
  13. FILE *Out,
  14. char *VarName
  15. );
  16. int
  17. DumpAscii(
  18. LPSTR FileNameIn,
  19. LPSTR FileNameOut,
  20. LPSTR VarName
  21. );
  22. // argv:
  23. //
  24. // 1 - in filename
  25. // 2 - required length
  26. // 3 - offset of region in question
  27. // 4 - length of region in question
  28. // 5 - out filename
  29. // 6 - name of variable
  30. int
  31. __cdecl
  32. main(
  33. int argc,
  34. char *argv[]
  35. )
  36. {
  37. int In,rc;
  38. FILE *Out;
  39. void *buffer;
  40. unsigned ReqLen,RegStart,RegLen,FileLen;
  41. if (argc == 5 &&
  42. (argv[1][0] == '-' || argv[1][0] == '/') &&
  43. tolower(argv[1][1]) == 'a') {
  44. return DumpAscii( argv[2], argv[3], argv[4] );
  45. }
  46. if(argc != 7) {
  47. printf("Usage: %s <src file> <src file len> <region offset>\n",argv[0]);
  48. printf(" <region length> <dst file> <var name>\n");
  49. return(2);
  50. }
  51. ReqLen = atoi(argv[2]);
  52. RegStart = atoi(argv[3]);
  53. RegLen = atoi(argv[4]);
  54. In = _open(argv[1],O_RDONLY | O_BINARY);
  55. if(In == -1) {
  56. printf("%s: Unable to open file %s\n",argv[0],argv[1]);
  57. return(2);
  58. }
  59. FileLen = _lseek(In,0,SEEK_END);
  60. if(RegStart > FileLen) {
  61. _close(In);
  62. printf("%s: Desired region is out of range\n",argv[0]);
  63. return(2);
  64. }
  65. if((unsigned)_lseek(In,RegStart,SEEK_SET) != RegStart) {
  66. _close(In);
  67. printf("%s: Unable to seek in file %s\n",argv[0],argv[1]);
  68. return(2);
  69. }
  70. if((buffer = malloc(RegLen)) == NULL) {
  71. _close(In);
  72. printf("%s: Out of memory\n",argv[0]);
  73. return(2);
  74. }
  75. memset(buffer, 0, RegLen);
  76. if((unsigned)_read(In,buffer,RegLen) > RegLen) {
  77. _close(In);
  78. printf("%s: Unable to read file %s\n",argv[0],argv[1]);
  79. return(2);
  80. }
  81. _close(In);
  82. Out = fopen(argv[5],"wb");
  83. if(Out == NULL) {
  84. printf("%s: Unable to open file %s for writing\n",argv[0],argv[5]);
  85. free(buffer);
  86. return(2);
  87. }
  88. rc = dump(buffer,RegLen,Out,argv[6]);
  89. if(rc) {
  90. printf("%s: Unable to write file %s\n",argv[0],argv[5]);
  91. }
  92. fclose(Out);
  93. free(buffer);
  94. return(rc);
  95. }
  96. int
  97. dump(
  98. char *buffer,
  99. unsigned buflen,
  100. FILE *Out,
  101. char *VarName
  102. )
  103. {
  104. unsigned major,minor;
  105. unsigned i;
  106. unsigned char *bufptr = buffer;
  107. int bw;
  108. char *DefName;
  109. DefName = malloc(strlen(VarName) + 1 + 5);
  110. if(DefName == NULL) {
  111. return(2);
  112. }
  113. strcpy(DefName,VarName);
  114. _strupr(DefName);
  115. strcat(DefName,"_SIZE");
  116. bw = fprintf(Out,"#define %s %u\n\n\n",DefName,buflen);
  117. bw = fprintf(Out,"unsigned char %s[] = {\n",VarName);
  118. if(bw <= 0) {
  119. return(2);
  120. }
  121. major = buflen/16;
  122. minor = buflen%16;
  123. for(i=0; i<major; i++) {
  124. bw = fprintf(Out,
  125. "%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u",
  126. bufptr[ 0],
  127. bufptr[ 1],
  128. bufptr[ 2],
  129. bufptr[ 3],
  130. bufptr[ 4],
  131. bufptr[ 5],
  132. bufptr[ 6],
  133. bufptr[ 7],
  134. bufptr[ 8],
  135. bufptr[ 9],
  136. bufptr[10],
  137. bufptr[11],
  138. bufptr[12],
  139. bufptr[13],
  140. bufptr[14],
  141. bufptr[15]
  142. );
  143. if(bw <= 0) {
  144. return(2);
  145. }
  146. if((i == major-1) && !minor) {
  147. bw = fprintf(Out,"\n");
  148. } else {
  149. bw = fprintf(Out,",\n");
  150. }
  151. if(bw <= 0) {
  152. return(2);
  153. }
  154. bufptr += 16;
  155. }
  156. if(minor) {
  157. for(i=0; i<minor-1; i++) {
  158. bw = fprintf(Out,"%u,",*bufptr++);
  159. if(bw <= 0) {
  160. return(2);
  161. }
  162. }
  163. bw = fprintf(Out,"%u\n",*bufptr);
  164. }
  165. bw = fprintf(Out,"};\n");
  166. if(bw <= 0) {
  167. return(2);
  168. }
  169. return(0);
  170. }
  171. int
  172. DumpAscii(
  173. LPSTR FileNameIn,
  174. LPSTR FileNameOut,
  175. LPSTR VarName
  176. )
  177. {
  178. HANDLE hFileIn;
  179. HANDLE hFileOut;
  180. HANDLE hMapIn;
  181. HANDLE hMapOut;
  182. LPVOID DataIn;
  183. LPVOID DataOut;
  184. LPSTR din;
  185. LPSTR dout;
  186. DWORD FileSize;
  187. DWORD Bytes;
  188. BOOL LineBegin = TRUE;
  189. //
  190. // open the input file
  191. //
  192. hFileIn = CreateFile(
  193. FileNameIn,
  194. GENERIC_READ,
  195. FILE_SHARE_READ,
  196. NULL,
  197. OPEN_EXISTING,
  198. 0,
  199. NULL
  200. );
  201. if (hFileIn == INVALID_HANDLE_VALUE) {
  202. return 1;
  203. }
  204. FileSize = GetFileSize( hFileIn, NULL );
  205. hMapIn = CreateFileMapping(
  206. hFileIn,
  207. NULL,
  208. PAGE_READONLY,
  209. 0,
  210. 0,
  211. NULL
  212. );
  213. if (!hMapIn) {
  214. return 1;
  215. }
  216. DataIn = (LPSTR) MapViewOfFile(
  217. hMapIn,
  218. FILE_MAP_READ,
  219. 0,
  220. 0,
  221. 0
  222. );
  223. if (!DataIn) {
  224. return 1;
  225. }
  226. //
  227. // open the output file
  228. //
  229. hFileOut = CreateFile(
  230. FileNameOut,
  231. GENERIC_READ | GENERIC_WRITE,
  232. 0,
  233. NULL,
  234. CREATE_ALWAYS,
  235. 0,
  236. NULL
  237. );
  238. if (hFileOut == INVALID_HANDLE_VALUE) {
  239. return 1;
  240. }
  241. hMapOut = CreateFileMapping(
  242. hFileOut,
  243. NULL,
  244. PAGE_READWRITE,
  245. 0,
  246. FileSize * 2,
  247. NULL
  248. );
  249. if (!hMapOut) {
  250. return 1;
  251. }
  252. DataOut = (LPSTR) MapViewOfFile(
  253. hMapOut,
  254. FILE_MAP_WRITE,
  255. 0,
  256. 0,
  257. 0
  258. );
  259. if (!DataOut) {
  260. return 1;
  261. }
  262. din = DataIn;
  263. dout = DataOut;
  264. sprintf( dout, "char %s[] =\r\n", VarName );
  265. Bytes = strlen(dout);
  266. dout += Bytes;
  267. while( FileSize ) {
  268. if (LineBegin) {
  269. if (*din == ';') {
  270. while (*din != '\n') {
  271. FileSize -= 1;
  272. din += 1;
  273. }
  274. FileSize -= 1;
  275. din += 1;
  276. continue;
  277. }
  278. *dout++ = '\"';
  279. Bytes += 1;
  280. LineBegin = FALSE;
  281. }
  282. FileSize -= 1;
  283. if (*din == '\r') {
  284. din += 1;
  285. *dout++ = '\\';
  286. *dout++ = 'r';
  287. Bytes += 2;
  288. continue;
  289. }
  290. if (*din == '\n') {
  291. din += 1;
  292. *dout++ = '\\';
  293. *dout++ = 'n';
  294. *dout++ = '\"';
  295. *dout++ = '\r';
  296. *dout++ = '\n';
  297. Bytes += 5;
  298. LineBegin = TRUE;
  299. continue;
  300. }
  301. *dout++ = *din++;
  302. Bytes += 1;
  303. }
  304. *dout++ = ';';
  305. *dout++ = '\r';
  306. *dout++ = '\n';
  307. Bytes += 3;
  308. UnmapViewOfFile( DataIn );
  309. CloseHandle( hMapIn );
  310. CloseHandle( hFileIn );
  311. UnmapViewOfFile( DataOut );
  312. CloseHandle( hMapOut );
  313. SetFilePointer( hFileOut, Bytes, NULL, FILE_BEGIN );
  314. SetEndOfFile( hFileOut );
  315. CloseHandle( hFileOut );
  316. return 0;
  317. }