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.

226 lines
6.8 KiB

  1. /****************************** Module Header ******************************\
  2. * Module Name: hsfile.c
  3. *
  4. * Copyright (c) 1985-96, Microsoft Corporation
  5. *
  6. * 09/05/96 GerardoB Created
  7. \***************************************************************************/
  8. #include "hsplit.h"
  9. /*********************************************************************
  10. * hsUnmapFile
  11. *
  12. \***************************************************************************/
  13. void hsUnmapFile (void)
  14. {
  15. LocalFree(gpmapStart);
  16. CloseHandle(ghfileInput);
  17. }
  18. /*********************************************************************
  19. * hsMapFile
  20. *
  21. \***************************************************************************/
  22. BOOL hsMapFile (void)
  23. {
  24. DWORD dwFileSize, dwBytesRead;
  25. ghfileInput = CreateFile(gpszInputFile, GENERIC_READ, FILE_SHARE_READ, NULL,
  26. OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS, NULL);
  27. if (ghfileInput == INVALID_HANDLE_VALUE) {
  28. hsLogMsg(HSLM_APIERROR | HSLM_NOLINE, "CreateFile");
  29. goto CleanupAndFail;
  30. }
  31. dwFileSize = GetFileSize(ghfileInput, NULL);
  32. if (dwFileSize == 0xFFFFFFFF) {
  33. hsLogMsg(HSLM_APIERROR | HSLM_NOLINE, "GetFileSize");
  34. goto CleanupAndFail;
  35. }
  36. gpmapStart = LocalAlloc(LPTR, dwFileSize + 1);
  37. if (!gpmapStart) {
  38. hsLogMsg(HSLM_APIERROR | HSLM_NOLINE, "LocalAlloc");
  39. goto CleanupAndFail;
  40. }
  41. if (!ReadFile(ghfileInput, gpmapStart, dwFileSize, &dwBytesRead, NULL)) {
  42. hsLogMsg(HSLM_APIERROR | HSLM_NOLINE, "ReadFile");
  43. goto CleanupAndFail;
  44. }
  45. if (dwFileSize != dwBytesRead) {
  46. hsLogMsg(HSLM_APIERROR | HSLM_NOLINE, "ReadFile");
  47. goto CleanupAndFail;
  48. }
  49. gpmapEnd = gpmapStart + dwFileSize;
  50. gpmapStart[dwFileSize] = '\0';
  51. #if 0
  52. ghmap = CreateFileMapping(ghfileInput, NULL, PAGE_READONLY, 0, 0, NULL);
  53. if (ghmap == NULL) {
  54. hsLogMsg(HSLM_APIERROR | HSLM_NOLINE, "CreateFileMapping");
  55. goto CleanupAndFail;
  56. }
  57. gpmapStart = MapViewOfFile(ghmap, FILE_MAP_READ, 0, 0, 0);
  58. if (gpmapStart == NULL) {
  59. hsLogMsg(HSLM_APIERROR | HSLM_NOLINE, "MapViewOfFile");
  60. goto CleanupAndFail;
  61. }
  62. gpmapEnd = gpmapStart + dwFileSize;
  63. #endif
  64. return TRUE;
  65. CleanupAndFail:
  66. hsLogMsg(HSLM_ERROR | HSLM_NOLINE, "hsMapFile failed. File: '%s'", gpszInputFile);
  67. return FALSE;
  68. }
  69. /*********************************************************************
  70. * hsCloseWorkingFiles
  71. *
  72. \***************************************************************************/
  73. BOOL hsCloseWorkingFiles (void)
  74. {
  75. CloseHandle(ghfilePublic);
  76. CloseHandle(ghfileInternal);
  77. hsUnmapFile();
  78. return TRUE;
  79. }
  80. /*********************************************************************
  81. * hsOpenWorkingFiles
  82. *
  83. \***************************************************************************/
  84. BOOL hsOpenWorkingFiles (void)
  85. {
  86. char * pszFileFailed;
  87. /*
  88. * Map input file to memory
  89. */
  90. if (!hsMapFile()) {
  91. pszFileFailed = gpszInputFile;
  92. goto CleanupAndFail;
  93. }
  94. /*
  95. * Open/Create public header file
  96. */
  97. ghfilePublic = CreateFile(gpszPublicFile, GENERIC_WRITE, 0, NULL,
  98. (gdwOptions & HSO_APPENDOUTPUT ? OPEN_EXISTING : CREATE_ALWAYS),
  99. FILE_ATTRIBUTE_NORMAL, NULL);
  100. if (ghfilePublic == INVALID_HANDLE_VALUE) {
  101. hsLogMsg(HSLM_APIERROR | HSLM_NOLINE, "CreateFile");
  102. pszFileFailed = gpszPublicFile;
  103. goto CleanupAndFail;
  104. }
  105. if (gdwOptions & HSO_APPENDOUTPUT) {
  106. if (0xFFFFFFFF == SetFilePointer (ghfilePublic, 0, 0, FILE_END)) {
  107. hsLogMsg(HSLM_APIERROR | HSLM_NOLINE, "SetFilePointer");
  108. pszFileFailed = gpszPublicFile;
  109. goto CleanupAndFail;
  110. }
  111. }
  112. /*
  113. * Open/Create internal header file
  114. */
  115. ghfileInternal = CreateFile(gpszInternalFile, GENERIC_WRITE, 0, NULL,
  116. (gdwOptions & HSO_APPENDOUTPUT ? OPEN_EXISTING : CREATE_ALWAYS),
  117. FILE_ATTRIBUTE_NORMAL, NULL);
  118. if (ghfileInternal == INVALID_HANDLE_VALUE) {
  119. hsLogMsg(HSLM_APIERROR | HSLM_NOLINE, "CreateFile");
  120. pszFileFailed = gpszInternalFile;
  121. goto CleanupAndFail;
  122. }
  123. if (gdwOptions & HSO_APPENDOUTPUT) {
  124. if (0xFFFFFFFF == SetFilePointer (ghfileInternal, 0, 0, FILE_END)) {
  125. hsLogMsg(HSLM_APIERROR | HSLM_NOLINE, "SetFilePointer");
  126. pszFileFailed = gpszInternalFile;
  127. goto CleanupAndFail;
  128. }
  129. }
  130. return TRUE;
  131. CleanupAndFail:
  132. hsLogMsg(HSLM_ERROR | HSLM_NOLINE, "hsOpenWorkingFiles failed. File:'%s'", pszFileFailed);
  133. return FALSE;
  134. }
  135. /***************************************************************************\
  136. * hsWriteHeaderFiles
  137. *
  138. \***************************************************************************/
  139. BOOL hsWriteHeaderFiles (char * pmap, DWORD dwSize, DWORD dwFlags)
  140. {
  141. DWORD dwWritten;
  142. /*
  143. * propagate the flags from previous blocks
  144. */
  145. if (ghsbStack < gphsbStackTop) {
  146. dwFlags |= (gphsbStackTop - 1)->dwMask;
  147. }
  148. /*
  149. * write this to the public/private files only if the
  150. * extractonly flag is not set !
  151. */
  152. if (!(dwFlags & HST_EXTRACTONLY)) {
  153. /*
  154. * If defaulting or if requested, write it to the public header
  155. */
  156. if (!(dwFlags & HST_BOTH)
  157. || (dwFlags & (HST_PUBLIC | HST_INCINTERNAL))) {
  158. if (!WriteFile(ghfilePublic, pmap, dwSize, &dwWritten, NULL)) {
  159. hsLogMsg(HSLM_APIERROR, "WriteFile");
  160. hsLogMsg(HSLM_ERROR, "Error writing public header: %s. Handle:%#lx.", gpszPublicFile, ghfilePublic);
  161. return FALSE;
  162. }
  163. }
  164. /*
  165. * Write it to internal header if requested
  166. */
  167. if ((dwFlags & HST_INTERNAL) && !(dwFlags & HST_INCINTERNAL)) {
  168. if (!WriteFile(ghfileInternal, pmap, dwSize, &dwWritten, NULL)) {
  169. hsLogMsg(HSLM_APIERROR, "WriteFile");
  170. hsLogMsg(HSLM_ERROR, "Error writing internal header: %s. Handle:%#lx.", gpszInternalFile, ghfileInternal);
  171. return FALSE;
  172. }
  173. }
  174. }
  175. /*
  176. * Write it to extract header if requested
  177. */
  178. if (!(dwFlags & HST_INTERNAL) && (dwFlags & HST_EXTRACT)) {
  179. PHSEXTRACT pe = gpExtractFile;
  180. while (pe != NULL) {
  181. if ((pe->dwMask & dwFlags) != HST_EXTRACT) {
  182. if (!WriteFile(pe->hfile, pmap, dwSize, &dwWritten, NULL)) {
  183. hsLogMsg(HSLM_APIERROR, "WriteFile");
  184. hsLogMsg(HSLM_ERROR, "Error writing extract header: %s. Handle:%#lx.",
  185. pe->pszFile, pe->hfile);
  186. return FALSE;
  187. }
  188. }
  189. pe = pe->pNext;
  190. }
  191. }
  192. return TRUE;
  193. }