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.

253 lines
5.8 KiB

  1. /*++
  2. Copyright (c) 2001 Microsoft Corporation
  3. Module Name:
  4. dmpobj.c
  5. Abstract:
  6. This program reads the contents of a specific section from an OBJ
  7. and emits its contents as a C-compatible UCHAR array.
  8. Author:
  9. Forrest Foltz (forrestf) 06-Mar-2001
  10. Revision History:
  11. --*/
  12. #include <nt.h>
  13. #include <ntrtl.h>
  14. #include <nturtl.h>
  15. #include <windows.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. ULONG FileSize;
  19. VOID
  20. Usage(VOID) {
  21. fprintf(stderr,"\n"
  22. "DMPOBJ reads the contents of a specific section from\n"
  23. "an OBJ image and emits its contents as a c-compatible\n"
  24. "UCHAR array.\n\n"
  25. "Usage: DMPOBJ <source> <secnam> <varnam>\n"
  26. "where: <source> is the full path of the .obj file\n\n"
  27. " <secnam> is the name of the section to emit\n"
  28. " Use ENTIRE_FILE to dump the whole thing\n\n"
  29. " <varnam> is the name to assign to the array\n");
  30. exit(1);
  31. }
  32. BOOLEAN
  33. OpenFileImage(
  34. IN PCHAR FilePath,
  35. OUT HANDLE *FileHandle,
  36. OUT PCHAR *FileImage
  37. )
  38. {
  39. HANDLE fileHandle;
  40. HANDLE mapHandle;
  41. PVOID view;
  42. fileHandle = CreateFile( FilePath,
  43. GENERIC_READ,
  44. 0,
  45. NULL,
  46. OPEN_EXISTING,
  47. FILE_ATTRIBUTE_NORMAL,
  48. NULL );
  49. if (fileHandle == INVALID_HANDLE_VALUE) {
  50. return FALSE;
  51. }
  52. FileSize = GetFileSize( fileHandle, NULL );
  53. mapHandle = CreateFileMapping( fileHandle,
  54. NULL,
  55. PAGE_READONLY,
  56. 0,
  57. 0,
  58. NULL );
  59. CloseHandle(fileHandle);
  60. if (mapHandle == NULL) {
  61. return FALSE;
  62. }
  63. view = MapViewOfFile( mapHandle,
  64. FILE_MAP_READ,
  65. 0,
  66. 0,
  67. 0 );
  68. if (view == NULL) {
  69. CloseHandle(mapHandle);
  70. return FALSE;
  71. }
  72. *FileHandle = mapHandle;
  73. *FileImage = view;
  74. return TRUE;
  75. }
  76. int
  77. __cdecl
  78. main (
  79. int argc,
  80. char *argv[]
  81. )
  82. {
  83. HANDLE mapHandle;
  84. PCHAR view;
  85. BOOLEAN result;
  86. PCHAR inputPath;
  87. PCHAR sectionName;
  88. PCHAR arrayName;
  89. ULONG numberOfSections;
  90. ULONG section;
  91. PIMAGE_FILE_HEADER imageHeader;
  92. PIMAGE_SECTION_HEADER sectionHeader;
  93. PIMAGE_SECTION_HEADER indexHeader;
  94. PUCHAR data;
  95. PUCHAR dataEnd;
  96. ULONG column;
  97. BOOLEAN entireFile;
  98. ULONG dumpSize;
  99. if (argc != 4) {
  100. Usage();
  101. }
  102. //
  103. // Get the user-supplied parameters and open the input image
  104. //
  105. inputPath = argv[1];
  106. sectionName = argv[2];
  107. arrayName = argv[3];
  108. result = OpenFileImage( inputPath, &mapHandle, &view );
  109. if (result == FALSE) {
  110. fprintf(stderr, "DMPOBJ: could not open file %s for reading.\n");
  111. exit(1);
  112. }
  113. //
  114. // Find the desired section. It is is a fatal error to specify a
  115. // section name that appears in more than one section.
  116. //
  117. if (strcmp("ENTIRE_FILE", sectionName) == 0) {
  118. data = view;
  119. dumpSize = FileSize;
  120. } else {
  121. imageHeader = (PIMAGE_FILE_HEADER)view;
  122. numberOfSections = imageHeader->NumberOfSections;
  123. indexHeader = (PIMAGE_SECTION_HEADER)((PUCHAR)(imageHeader + 1) +
  124. imageHeader->SizeOfOptionalHeader);
  125. sectionHeader = NULL;
  126. for (section = 0; section < numberOfSections; section += 1) {
  127. if (strncmp(indexHeader->Name,
  128. sectionName,
  129. IMAGE_SIZEOF_SHORT_NAME) == 0) {
  130. if (sectionHeader != NULL) {
  131. fprintf(stderr,
  132. "DMPOBJ: multiple instances of section %s "
  133. "found in image %s\n",
  134. sectionName,
  135. inputPath);
  136. exit(1);
  137. }
  138. sectionHeader = indexHeader;
  139. }
  140. indexHeader += 1;
  141. }
  142. if (sectionHeader == NULL) {
  143. fprintf(stderr,
  144. "DMPOBJ: could not find section %s in image %s\n",
  145. sectionName,
  146. inputPath);
  147. exit(1);
  148. }
  149. data = view + sectionHeader->PointerToRawData;
  150. dumpSize = sectionHeader->SizeOfRawData;
  151. }
  152. //
  153. // Dump the contents of the section in a format compatible with a
  154. // C language compiler.
  155. //
  156. fprintf(stdout, "//\n");
  157. fprintf(stdout, "// DMPOBJ generated file, DO NOT EDIT\n");
  158. fprintf(stdout, "//\n");
  159. fprintf(stdout, "// Source: %s\n", inputPath);
  160. fprintf(stdout, "// Section: %s\n", sectionName);
  161. fprintf(stdout, "//\n");
  162. fprintf(stdout, "\n");
  163. fprintf(stdout, "const char %s[] = {\n ",arrayName);
  164. column = 0;
  165. dataEnd = data + dumpSize;
  166. while (data < dataEnd) {
  167. if (column == 12) {
  168. fprintf(stdout, "\n ");
  169. column = 0;
  170. }
  171. fprintf(stdout, "0x%02x", *data);
  172. if (data < (dataEnd - 1)) {
  173. fprintf(stdout, ", ");
  174. }
  175. column += 1;
  176. data += 1;
  177. }
  178. fprintf(stdout, "\n};\n\n");
  179. fprintf(stdout,
  180. "#define %sSize 0x%0x\n\n",
  181. arrayName,
  182. dumpSize);
  183. CloseHandle(mapHandle);
  184. return 0;
  185. }