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.

239 lines
6.2 KiB

  1. #include "precomp.h"
  2. #pragma hdrstop
  3. /* File: progcm.c */
  4. /**************************************************************************/
  5. /* Install: Resource stamping
  6. /**************************************************************************/
  7. extern HWND hwndFrame;
  8. #define wExeSignature 0x5A4D
  9. #define lNewExeOffset 0x3CL
  10. #define wNewExeSignature 0x454E
  11. #define lResourceOffset 0x24L
  12. #define FTypeNumeric(wType) (wType & 0x8000)
  13. #define FNameNumeric(wName) (wName & 0x8000)
  14. #define WTypeActual(wType) (WORD)(wType & 0x7FFF)
  15. #define WNameActual(wName) (WORD)(wName & 0x7FFF)
  16. /* ReSource Group */
  17. typedef struct _rsg
  18. {
  19. WORD wType;
  20. WORD crsd;
  21. LONG lReserved;
  22. } RSG;
  23. #define cbRsg (sizeof(RSG))
  24. /* ReSource Descriptor */
  25. typedef struct _rsd
  26. {
  27. WORD wOffset;
  28. WORD wLength;
  29. WORD wFlags;
  30. WORD wName;
  31. LONG lReserved;
  32. } RSD;
  33. #define cbRsd (sizeof(RSD))
  34. /*
  35. ** Purpose:
  36. ** Write a string into an EXE resource
  37. ** Arguments:
  38. ** szSection INF section containing EXE file descriptor
  39. ** szKey INF key of EXE file descriptor
  40. ** szDst Directory where the EXE file lives
  41. ** wResType Resource type
  42. ** wResId Resource ID
  43. ** szData String to write into the resource
  44. ** cbData Number of bytes to write
  45. ** Notes:
  46. ** Only numeric resource types and resource IDs are supported.
  47. ** szData must contain the entire resource data, and must
  48. ** be formatted correctly as a resource, including all tags,
  49. ** byte counts, flags, etc, expected of the resource type.
  50. ** FStampResource knows nothing of individual resource formats.
  51. ** cbData must be less than or equal to the size of the
  52. ** resource in the file. If it is smaller than the actual
  53. ** resource, the remainder of the resource is left intact.
  54. ** Returns:
  55. ** Returns fTrue if successful, fFalse otherwise.
  56. **
  57. **************************************************************************/
  58. BOOL APIENTRY FStampResource(SZ szSection, SZ szKey, SZ szDst,
  59. WORD wResType, WORD wResId, SZ szData, CB cbData)
  60. /* REVIEW need fVital? */
  61. {
  62. #if defined(WIN16)
  63. PSFD psfd = (PSFD)NULL;
  64. OER oer;
  65. CHP szExe[cchpFullPathBuf];
  66. PFH pfh;
  67. LONG lData, lNewHeader;
  68. WORD wResShift;
  69. USHORT date, time;
  70. GRC grc;
  71. INT Line;
  72. while ((grc = GrcFillPoerFromSymTab(&oer)) != grcOkay)
  73. if (EercErrorHandler(hwndFrame, grc, fTrue, 0, 0, 0) != eercRetry)
  74. return(fFalse);
  75. if ((Line = FindLineFromInfSectionKey(szSection, szKey)) == -1)
  76. {
  77. EvalAssert(EercErrorHandler(hwndFrame, grcINFMissingLine, fTrue, szSection, pLocalInfPermInfo()->szName,
  78. 0) == eercAbort);
  79. return(fFalse);
  80. }
  81. while ((grc = GrcGetSectionFileLine(&psfd, &oer)) != grcOkay) {
  82. SZ szParam1 = NULL, szParam2 = NULL;
  83. switch ( grc ) {
  84. case grcINFBadFDLine:
  85. szParam1 = pLocalInfPermInfo()->szName;
  86. szParam2 = szSection;
  87. break;
  88. default:
  89. break;
  90. }
  91. if (EercErrorHandler(hwndFrame, grc, fTrue, szParam1, szParam2, 0) != eercRetry) {
  92. return(fFalse);
  93. }
  94. }
  95. Assert(psfd != NULL);
  96. if (!FBuildFullDstPath(szExe, szDst, psfd, fFalse))
  97. {
  98. EvalAssert(FFreePsfd(psfd));
  99. EvalAssert(EercErrorHandler(hwndFrame, grcInvalidPathErr, fTrue,
  100. szDst, psfd->szFile, 0) == eercAbort);
  101. return(fFalse);
  102. }
  103. EvalAssert(FFreePsfd(psfd));
  104. while ((pfh = PfhOpenFile(szExe, ofmReadWrite)) == (PFH)NULL)
  105. if (EercErrorHandler(hwndFrame, grcOpenFileErr, fTrue, szExe, 0, 0)
  106. != eercRetry)
  107. return(fFalse);
  108. while (_dos_getftime(pfh->iDosfh, &date, &time))
  109. if (EercErrorHandler(hwndFrame, grcReadFileErr, fTrue, szExe, 0, 0)
  110. != eercRetry)
  111. goto LCloseExit;
  112. while (CbReadFile(pfh, (PB)&lData, 2) != 2)
  113. if (EercErrorHandler(hwndFrame, grcReadFileErr, fTrue, szExe, 0, 0)
  114. != eercRetry)
  115. goto LCloseExit;
  116. if (LOWORD(lData) != 0x5A4D) //'MZ'
  117. goto LBadExe;
  118. if (LfaSeekFile(pfh, 0x3CL, sfmSet) == lfaSeekError)
  119. goto LBadExe;
  120. if (CbReadFile(pfh, (PB)&lNewHeader, 4) != 4)
  121. goto LBadExe;
  122. if (LfaSeekFile(pfh, lNewHeader, sfmSet) == lfaSeekError)
  123. goto LBadExe;
  124. if (CbReadFile(pfh, (PB)&lData, 2) != 2)
  125. goto LBadExe;
  126. if (LOWORD(lData) != 0x454E) //'NE'
  127. goto LBadExe;
  128. if (LfaSeekFile(pfh, lNewHeader + 0x24, sfmSet) == lfaSeekError)
  129. goto LBadExe;
  130. if (CbReadFile(pfh, (PB)&lData, 2) != 2)
  131. goto LBadExe;
  132. if (LfaSeekFile(pfh, lNewHeader + LOWORD(lData), sfmSet) == lfaSeekError)
  133. goto LBadExe;
  134. if (CbReadFile(pfh, (PB)&wResShift, 2) != 2)
  135. goto LBadExe;
  136. for (;;)
  137. {
  138. RSG rsg;
  139. if (CbReadFile(pfh, (PB)&rsg, cbRsg) != cbRsg)
  140. goto LBadExe;
  141. if (rsg.wType == 0)
  142. goto LMissingResource;
  143. while (rsg.crsd)
  144. {
  145. RSD rsd;
  146. if (CbReadFile(pfh, (PB)&rsd, cbRsd) != cbRsd)
  147. goto LBadExe;
  148. if (FTypeNumeric(rsg.wType) &&
  149. WTypeActual(rsg.wType) == wResType &&
  150. FNameNumeric(rsd.wName) &&
  151. WNameActual(rsd.wName) == wResId)
  152. {
  153. LONG lOffset = ((LONG)rsd.wOffset) << wResShift;
  154. LONG lLength = ((LONG)rsd.wLength) << wResShift;
  155. if (LfaSeekFile(pfh, lOffset, sfmSet) == lfaSeekError)
  156. goto LMissingResource;
  157. if ((LONG)cbData > lLength)
  158. {
  159. EvalAssert(EercErrorHandler(hwndFrame,
  160. grcResourceTooLongErr, fTrue, 0, 0,0) == eercAbort);
  161. goto LCloseExit;
  162. }
  163. if (CbWriteFile(pfh, szData, cbData) != cbData)
  164. {
  165. EvalAssert(EercErrorHandler(hwndFrame, grcWriteFileErr,
  166. fTrue, szExe, 0,0) == eercAbort);
  167. goto LCloseExit;
  168. }
  169. while (_dos_setftime(pfh->iDosfh, date, time))
  170. if (EercErrorHandler(hwndFrame, grcWriteFileErr, fTrue,
  171. szExe, 0, 0) != eercRetry)
  172. goto LCloseExit;
  173. EvalAssert(FCloseFile(pfh));
  174. return(fTrue);
  175. }
  176. rsg.crsd--;
  177. }
  178. }
  179. LMissingResource:
  180. EvalAssert(EercErrorHandler(hwndFrame, grcMissingResourceErr, fTrue, szExe,
  181. 0, 0) == eercAbort);
  182. goto LCloseExit;
  183. LBadExe:
  184. EvalAssert(EercErrorHandler(hwndFrame, grcBadWinExeFileFormatErr, fTrue,
  185. szExe, 0, 0) == eercAbort);
  186. LCloseExit:
  187. EvalAssert(FCloseFile(pfh));
  188. return(fFalse);
  189. #else // 1632BUG -- eliminate this func altogether?
  190. Unused(szSection);
  191. Unused(szKey);
  192. Unused(szDst);
  193. Unused(wResType);
  194. Unused(wResId);
  195. Unused(szData);
  196. Unused(cbData);
  197. MessBoxSzSz("FStampResource","IGNORED (Unsupported in 32-bit version)");
  198. return(fTrue);
  199. #endif
  200. }