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.

450 lines
15 KiB

  1. //
  2. // patchapi.h
  3. //
  4. // Interface for creating and applying patches to files.
  5. //
  6. // Copyright (C) Microsoft, 1997-1998.
  7. //
  8. #ifndef _PATCHAPI_H_
  9. #define _PATCHAPI_H_
  10. #ifdef __cplusplus
  11. extern "C" {
  12. #endif
  13. //
  14. // The following constants can be combined and used as the OptionFlags
  15. // parameter in the patch creation apis.
  16. //
  17. #define PATCH_OPTION_USE_BEST 0x00000000 // auto choose best (slower)
  18. #define PATCH_OPTION_USE_LZX_BEST 0x00000003 // auto choose best of LZX
  19. #define PATCH_OPTION_USE_LZX_A 0x00000001 // normal
  20. #define PATCH_OPTION_USE_LZX_B 0x00000002 // better on some x86 binaries
  21. #define PATCH_OPTION_NO_BINDFIX 0x00010000 // PE bound imports
  22. #define PATCH_OPTION_NO_LOCKFIX 0x00020000 // PE smashed locks
  23. #define PATCH_OPTION_NO_REBASE 0x00040000 // PE rebased image
  24. #define PATCH_OPTION_FAIL_IF_SAME_FILE 0x00080000 // don't create if same
  25. #define PATCH_OPTION_FAIL_IF_BIGGER 0x00100000 // fail if patch is larger than simply compressing new file (slower)
  26. #define PATCH_OPTION_NO_CHECKSUM 0x00200000 // PE checksum zero
  27. #define PATCH_OPTION_NO_RESTIMEFIX 0x00400000 // PE resource timestamps
  28. #define PATCH_OPTION_NO_TIMESTAMP 0x00800000 // don't store new file timestamp in patch
  29. #define PATCH_OPTION_SIGNATURE_MD5 0x01000000 // use MD5 instead of CRC32
  30. #define PATCH_OPTION_RESERVED1 0x80000000 // (used internally)
  31. #define PATCH_OPTION_VALID_FLAGS 0x80FF0003
  32. #define PATCH_SYMBOL_NO_IMAGEHLP 0x00000001 // don't use imagehlp.dll
  33. #define PATCH_SYMBOL_NO_FAILURES 0x00000002 // don't fail patch due to imagehlp failures
  34. #define PATCH_SYMBOL_UNDECORATED_TOO 0x00000004 // after matching decorated symbols, try to match remaining by undecorated names
  35. #define PATCH_SYMBOL_RESERVED1 0x80000000 // (used internally)
  36. //
  37. // The following constants can be combined and used as the ApplyOptionFlags
  38. // parameter in the patch apply and test apis.
  39. //
  40. #define APPLY_OPTION_FAIL_IF_EXACT 0x00000001 // don't copy new file
  41. #define APPLY_OPTION_FAIL_IF_CLOSE 0x00000002 // differ by rebase, bind
  42. #define APPLY_OPTION_TEST_ONLY 0x00000004 // don't create new file
  43. #define APPLY_OPTION_VALID_FLAGS 0x00000007
  44. //
  45. // In addition to standard Win32 error codes, the following error codes may
  46. // be returned via GetLastError() when one of the patch APIs fails.
  47. //
  48. #define ERROR_PATCH_ENCODE_FAILURE 0xC00E3101 // create
  49. #define ERROR_PATCH_INVALID_OPTIONS 0xC00E3102 // create
  50. #define ERROR_PATCH_SAME_FILE 0xC00E3103 // create
  51. #define ERROR_PATCH_RETAIN_RANGES_DIFFER 0xC00E3104 // create
  52. #define ERROR_PATCH_BIGGER_THAN_COMPRESSED 0xC00E3105 // create
  53. #define ERROR_PATCH_IMAGEHLP_FAILURE 0xC00E3106 // create
  54. #define ERROR_PATCH_DECODE_FAILURE 0xC00E4101 // apply
  55. #define ERROR_PATCH_CORRUPT 0xC00E4102 // apply
  56. #define ERROR_PATCH_NEWER_FORMAT 0xC00E4103 // apply
  57. #define ERROR_PATCH_WRONG_FILE 0xC00E4104 // apply
  58. #define ERROR_PATCH_NOT_NECESSARY 0xC00E4105 // apply
  59. #define ERROR_PATCH_NOT_AVAILABLE 0xC00E4106 // apply
  60. typedef BOOL (CALLBACK *PPATCH_PROGRESS_CALLBACK)(
  61. PVOID CallbackContext,
  62. ULONG CurrentPosition,
  63. ULONG MaximumPosition
  64. );
  65. typedef BOOL (CALLBACK *PPATCH_SYMLOAD_CALLBACK)(
  66. IN ULONG WhichFile, // 0 for new file, 1 for first old file, etc
  67. IN LPCSTR SymbolFileName,
  68. IN ULONG SymType, // see SYM_TYPE in imagehlp.h
  69. IN ULONG SymbolFileCheckSum,
  70. IN ULONG SymbolFileTimeDate,
  71. IN ULONG ImageFileCheckSum,
  72. IN ULONG ImageFileTimeDate,
  73. IN PVOID CallbackContext
  74. );
  75. typedef struct _PATCH_IGNORE_RANGE {
  76. ULONG OffsetInOldFile;
  77. ULONG LengthInBytes;
  78. } PATCH_IGNORE_RANGE, *PPATCH_IGNORE_RANGE;
  79. typedef struct _PATCH_RETAIN_RANGE {
  80. ULONG OffsetInOldFile;
  81. ULONG LengthInBytes;
  82. ULONG OffsetInNewFile;
  83. } PATCH_RETAIN_RANGE, *PPATCH_RETAIN_RANGE;
  84. typedef struct _PATCH_OLD_FILE_INFO_A {
  85. ULONG SizeOfThisStruct;
  86. LPCSTR OldFileName;
  87. ULONG IgnoreRangeCount; // maximum 255
  88. PPATCH_IGNORE_RANGE IgnoreRangeArray;
  89. ULONG RetainRangeCount; // maximum 255
  90. PPATCH_RETAIN_RANGE RetainRangeArray;
  91. } PATCH_OLD_FILE_INFO_A, *PPATCH_OLD_FILE_INFO_A;
  92. typedef struct _PATCH_OLD_FILE_INFO_W {
  93. ULONG SizeOfThisStruct;
  94. LPCWSTR OldFileName;
  95. ULONG IgnoreRangeCount; // maximum 255
  96. PPATCH_IGNORE_RANGE IgnoreRangeArray;
  97. ULONG RetainRangeCount; // maximum 255
  98. PPATCH_RETAIN_RANGE RetainRangeArray;
  99. } PATCH_OLD_FILE_INFO_W, *PPATCH_OLD_FILE_INFO_W;
  100. typedef struct _PATCH_OLD_FILE_INFO_H {
  101. ULONG SizeOfThisStruct;
  102. HANDLE OldFileHandle;
  103. ULONG IgnoreRangeCount; // maximum 255
  104. PPATCH_IGNORE_RANGE IgnoreRangeArray;
  105. ULONG RetainRangeCount; // maximum 255
  106. PPATCH_RETAIN_RANGE RetainRangeArray;
  107. } PATCH_OLD_FILE_INFO_H, *PPATCH_OLD_FILE_INFO_H;
  108. typedef struct _PATCH_OLD_FILE_INFO {
  109. ULONG SizeOfThisStruct;
  110. union {
  111. LPCSTR OldFileNameA;
  112. LPCWSTR OldFileNameW;
  113. HANDLE OldFileHandle;
  114. };
  115. ULONG IgnoreRangeCount; // maximum 255
  116. PPATCH_IGNORE_RANGE IgnoreRangeArray;
  117. ULONG RetainRangeCount; // maximum 255
  118. PPATCH_RETAIN_RANGE RetainRangeArray;
  119. } PATCH_OLD_FILE_INFO, *PPATCH_OLD_FILE_INFO;
  120. typedef struct _PATCH_OPTION_DATA {
  121. ULONG SizeOfThisStruct;
  122. ULONG SymbolOptionFlags; // PATCH_SYMBOL_xxx flags
  123. LPCSTR NewFileSymbolPath; // always ANSI, never Unicode
  124. LPCSTR *OldFileSymbolPathArray; // array[ OldFileCount ]
  125. ULONG ExtendedOptionFlags;
  126. PPATCH_SYMLOAD_CALLBACK SymLoadCallback;
  127. PVOID SymLoadContext;
  128. } PATCH_OPTION_DATA, *PPATCH_OPTION_DATA;
  129. //
  130. // Note that PATCH_OPTION_DATA contains LPCSTR paths, and no LPCWSTR (Unicode)
  131. // path argument is available, even when used with one of the Unicode APIs
  132. // such as CreatePatchFileW. This is because the underlying system services
  133. // for symbol file handling (IMAGEHLP.DLL) only support ANSI file/path names.
  134. //
  135. //
  136. // A note about PATCH_RETAIN_RANGE specifiers with multiple old files:
  137. //
  138. // Each old version file must have the same RetainRangeCount, and the same
  139. // retain range LengthInBytes and OffsetInNewFile values in the same order.
  140. // Only the OffsetInOldFile values can differ between old files for retain
  141. // ranges.
  142. //
  143. #ifdef IMPORTING_PATCHAPI_DLL
  144. #define PATCHAPI WINAPI __declspec( dllimport )
  145. #else
  146. #define PATCHAPI WINAPI
  147. #endif
  148. //
  149. // The following prototypes are interface for creating patches from files.
  150. //
  151. BOOL
  152. PATCHAPI
  153. CreatePatchFileA(
  154. IN LPCSTR OldFileName,
  155. IN LPCSTR NewFileName,
  156. OUT LPCSTR PatchFileName,
  157. IN ULONG OptionFlags,
  158. IN PPATCH_OPTION_DATA OptionData // optional
  159. );
  160. BOOL
  161. PATCHAPI
  162. CreatePatchFileW(
  163. IN LPCWSTR OldFileName,
  164. IN LPCWSTR NewFileName,
  165. OUT LPCWSTR PatchFileName,
  166. IN ULONG OptionFlags,
  167. IN PPATCH_OPTION_DATA OptionData // optional
  168. );
  169. BOOL
  170. PATCHAPI
  171. CreatePatchFileByHandles(
  172. IN HANDLE OldFileHandle,
  173. IN HANDLE NewFileHandle,
  174. OUT HANDLE PatchFileHandle,
  175. IN ULONG OptionFlags,
  176. IN PPATCH_OPTION_DATA OptionData // optional
  177. );
  178. BOOL
  179. PATCHAPI
  180. CreatePatchFileExA(
  181. IN ULONG OldFileCount, // maximum 255
  182. IN PPATCH_OLD_FILE_INFO_A OldFileInfoArray,
  183. IN LPCSTR NewFileName,
  184. OUT LPCSTR PatchFileName,
  185. IN ULONG OptionFlags,
  186. IN PPATCH_OPTION_DATA OptionData, // optional
  187. IN PPATCH_PROGRESS_CALLBACK ProgressCallback,
  188. IN PVOID CallbackContext
  189. );
  190. BOOL
  191. PATCHAPI
  192. CreatePatchFileExW(
  193. IN ULONG OldFileCount, // maximum 255
  194. IN PPATCH_OLD_FILE_INFO_W OldFileInfoArray,
  195. IN LPCWSTR NewFileName,
  196. OUT LPCWSTR PatchFileName,
  197. IN ULONG OptionFlags,
  198. IN PPATCH_OPTION_DATA OptionData, // optional
  199. IN PPATCH_PROGRESS_CALLBACK ProgressCallback,
  200. IN PVOID CallbackContext
  201. );
  202. BOOL
  203. PATCHAPI
  204. CreatePatchFileByHandlesEx(
  205. IN ULONG OldFileCount, // maximum 255
  206. IN PPATCH_OLD_FILE_INFO_H OldFileInfoArray,
  207. IN HANDLE NewFileHandle,
  208. OUT HANDLE PatchFileHandle,
  209. IN ULONG OptionFlags,
  210. IN PPATCH_OPTION_DATA OptionData, // optional
  211. IN PPATCH_PROGRESS_CALLBACK ProgressCallback,
  212. IN PVOID CallbackContext
  213. );
  214. BOOL
  215. PATCHAPI
  216. ExtractPatchHeaderToFileA(
  217. IN LPCSTR PatchFileName,
  218. OUT LPCSTR PatchHeaderFileName
  219. );
  220. BOOL
  221. PATCHAPI
  222. ExtractPatchHeaderToFileW(
  223. IN LPCWSTR PatchFileName,
  224. OUT LPCWSTR PatchHeaderFileName
  225. );
  226. BOOL
  227. PATCHAPI
  228. ExtractPatchHeaderToFileByHandles(
  229. IN HANDLE PatchFileHandle,
  230. OUT HANDLE PatchHeaderFileHandle
  231. );
  232. //
  233. // The following prototypes are interface for creating new file from old file
  234. // and patch file. Note that it is possible for the TestApply API to succeed
  235. // but the actual Apply to fail since the TestApply only verifies that the
  236. // old file has the correct CRC without actually applying the patch. The
  237. // TestApply API only requires the patch header portion of the patch file,
  238. // but its CRC must be fixed up.
  239. //
  240. BOOL
  241. PATCHAPI
  242. TestApplyPatchToFileA(
  243. IN LPCSTR PatchFileName,
  244. IN LPCSTR OldFileName,
  245. IN ULONG ApplyOptionFlags
  246. );
  247. BOOL
  248. PATCHAPI
  249. TestApplyPatchToFileW(
  250. IN LPCWSTR PatchFileName,
  251. IN LPCWSTR OldFileName,
  252. IN ULONG ApplyOptionFlags
  253. );
  254. BOOL
  255. PATCHAPI
  256. TestApplyPatchToFileByHandles(
  257. IN HANDLE PatchFileHandle, // requires GENERIC_READ access
  258. IN HANDLE OldFileHandle, // requires GENERIC_READ access
  259. IN ULONG ApplyOptionFlags
  260. );
  261. BOOL
  262. PATCHAPI
  263. ApplyPatchToFileA(
  264. IN LPCSTR PatchFileName,
  265. IN LPCSTR OldFileName,
  266. OUT LPCSTR NewFileName,
  267. IN ULONG ApplyOptionFlags
  268. );
  269. BOOL
  270. PATCHAPI
  271. ApplyPatchToFileW(
  272. IN LPCWSTR PatchFileName,
  273. IN LPCWSTR OldFileName,
  274. OUT LPCWSTR NewFileName,
  275. IN ULONG ApplyOptionFlags
  276. );
  277. BOOL
  278. PATCHAPI
  279. ApplyPatchToFileByHandles(
  280. IN HANDLE PatchFileHandle, // requires GENERIC_READ access
  281. IN HANDLE OldFileHandle, // requires GENERIC_READ access
  282. OUT HANDLE NewFileHandle, // requires GENERIC_READ | GENERIC_WRITE
  283. IN ULONG ApplyOptionFlags
  284. );
  285. BOOL
  286. PATCHAPI
  287. ApplyPatchToFileExA(
  288. IN LPCSTR PatchFileName,
  289. IN LPCSTR OldFileName,
  290. OUT LPCSTR NewFileName,
  291. IN ULONG ApplyOptionFlags,
  292. IN PPATCH_PROGRESS_CALLBACK ProgressCallback,
  293. IN PVOID CallbackContext
  294. );
  295. BOOL
  296. PATCHAPI
  297. ApplyPatchToFileExW(
  298. IN LPCWSTR PatchFileName,
  299. IN LPCWSTR OldFileName,
  300. OUT LPCWSTR NewFileName,
  301. IN ULONG ApplyOptionFlags,
  302. IN PPATCH_PROGRESS_CALLBACK ProgressCallback,
  303. IN PVOID CallbackContext
  304. );
  305. BOOL
  306. PATCHAPI
  307. ApplyPatchToFileByHandlesEx(
  308. IN HANDLE PatchFileHandle,
  309. IN HANDLE OldFileHandle,
  310. OUT HANDLE NewFileHandle,
  311. IN ULONG ApplyOptionFlags,
  312. IN PPATCH_PROGRESS_CALLBACK ProgressCallback,
  313. IN PVOID CallbackContext
  314. );
  315. //
  316. // The following prototypes provide a unique patch "signature" for a given
  317. // file. Consider the case where you have a new foo.dll and the machines
  318. // to be updated with the new foo.dll may have one of three different old
  319. // foo.dll files. Rather than creating a single large patch file that can
  320. // update any of the three older foo.dll files, three separate smaller patch
  321. // files can be created and "named" according to the patch signature of the
  322. // old file. Then the patch applyer application can determine at runtime
  323. // which of the three foo.dll patch files is necessary given the specific
  324. // foo.dll to be updated. If patch files are being downloaded over a slow
  325. // network connection (Internet over a modem), this signature scheme provides
  326. // a mechanism for choosing the correct single patch file to download at
  327. // application time thus decreasing total bytes necessary to download.
  328. //
  329. BOOL
  330. GetFilePatchSignatureA(
  331. IN LPCSTR FileName,
  332. IN ULONG OptionFlags,
  333. IN PVOID OptionData,
  334. IN ULONG IgnoreRangeCount,
  335. IN PPATCH_IGNORE_RANGE IgnoreRangeArray,
  336. IN ULONG RetainRangeCount,
  337. IN PPATCH_RETAIN_RANGE RetainRangeArray,
  338. IN ULONG SignatureBufferSize,
  339. OUT PVOID SignatureBuffer
  340. );
  341. BOOL
  342. GetFilePatchSignatureW(
  343. IN LPCWSTR FileName,
  344. IN ULONG OptionFlags,
  345. IN PVOID OptionData,
  346. IN ULONG IgnoreRangeCount,
  347. IN PPATCH_IGNORE_RANGE IgnoreRangeArray,
  348. IN ULONG RetainRangeCount,
  349. IN PPATCH_RETAIN_RANGE RetainRangeArray,
  350. IN ULONG SignatureBufferSizeInBytes,
  351. OUT PVOID SignatureBuffer
  352. );
  353. BOOL
  354. GetFilePatchSignatureByHandle(
  355. IN HANDLE FileHandle,
  356. IN ULONG OptionFlags,
  357. IN PVOID OptionData,
  358. IN ULONG IgnoreRangeCount,
  359. IN PPATCH_IGNORE_RANGE IgnoreRangeArray,
  360. IN ULONG RetainRangeCount,
  361. IN PPATCH_RETAIN_RANGE RetainRangeArray,
  362. IN ULONG SignatureBufferSize,
  363. OUT PVOID SignatureBuffer
  364. );
  365. //
  366. // Depending on whether UNICODE is defined, map the generic API names to the
  367. // appropriate Unicode or Ansi APIs.
  368. //
  369. #ifdef UNICODE
  370. #define CreatePatchFile CreatePatchFileW
  371. #define CreatePatchFileEx CreatePatchFileExW
  372. #define TestApplyPatchToFile TestApplyPatchToFileW
  373. #define ApplyPatchToFile ApplyPatchToFileW
  374. #define ApplyPatchToFileEx ApplyPatchToFileExW
  375. #define ExtractPatchHeaderToFile ExtractPatchHeaderToFileW
  376. #define GetFilePatchSignature GetFilePatchSignatureW
  377. #else
  378. #define CreatePatchFile CreatePatchFileA
  379. #define CreatePatchFileEx CreatePatchFileExA
  380. #define TestApplyPatchToFile TestApplyPatchToFileA
  381. #define ApplyPatchToFile ApplyPatchToFileA
  382. #define ApplyPatchToFileEx ApplyPatchToFileExA
  383. #define ExtractPatchHeaderToFile ExtractPatchHeaderToFileA
  384. #define GetFilePatchSignature GetFilePatchSignatureA
  385. #endif // UNICODE
  386. #ifdef __cplusplus
  387. }
  388. #endif
  389. #endif // _PATCHAPI_H_