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.

242 lines
4.8 KiB

  1. /*++
  2. Copyright (c) 1998 Microsoft Corporation
  3. Module Name:
  4. ntcab.c
  5. Abstract:
  6. This is the source module for the nt
  7. cab file tool. This tool allows for the
  8. creation and modification of nt cab files.
  9. Author:
  10. Wesley Witt (wesw) 29-Sept-1998
  11. Revision History:
  12. --*/
  13. #include <windows.h>
  14. #include <stdio.h>
  15. #include "ntcabapi.h"
  16. void
  17. Usage(
  18. void
  19. )
  20. {
  21. printf( "\nMicrosoft (R) NT Cabinet Tool\n" );
  22. printf( "Copyright (c) Microsoft Corp 1998. All rights reserved.\n\n" );
  23. printf( "Usage: NTCAB [options] cabfile [@list] [files]\n\n" );
  24. printf( "Options:\n" );
  25. printf( " -c Create a new cab file\n" );
  26. printf( " -a Add a file to the cabinet\n" );
  27. printf( " -x Extract a file from the cabinet\n" );
  28. }
  29. BOOL
  30. UserEnumFunc(
  31. const PNTCAB_ENUM_DATA EnumData,
  32. ULONG_PTR Context
  33. )
  34. {
  35. printf( "%ws\n", EnumData->FileName );
  36. return TRUE;
  37. }
  38. int __cdecl wmain( int argc, WCHAR *argv[] )
  39. {
  40. PVOID hCab;
  41. ULONG i;
  42. HANDLE hFile;
  43. HANDLE hMap;
  44. PCHAR FileList;
  45. PCHAR s,e;
  46. WCHAR FileName[MAX_PATH];
  47. BOOL CreateNewCab = FALSE;
  48. BOOL ExtractFile = FALSE;
  49. BOOL AddFile = FALSE;
  50. BOOL ListFile = FALSE;
  51. //
  52. // process any options
  53. //
  54. for (i=1; i<(ULONG)argc; i++) {
  55. if (argv[i][0] == L'/' || argv[i][0] == L'-') {
  56. switch (towlower(argv[i][1])) {
  57. case L'c':
  58. CreateNewCab = TRUE;
  59. break;
  60. case L'x':
  61. ExtractFile = TRUE;
  62. break;
  63. case L'a':
  64. AddFile = TRUE;
  65. break;
  66. case L'l':
  67. ListFile = TRUE;
  68. break;
  69. case L'?':
  70. Usage();
  71. return 0;
  72. default:
  73. return -1;
  74. }
  75. }
  76. }
  77. if (ListFile) {
  78. hCab = NtCabInitialize();
  79. if (hCab == NULL) {
  80. return -1;
  81. }
  82. if (!NtCabOpenCabFile( hCab, argv[2] )) {
  83. return -1;
  84. }
  85. NtCabEnumerateFiles( hCab, UserEnumFunc, 0 );
  86. NtCabClose( hCab );
  87. return 0;
  88. }
  89. if (ExtractFile) {
  90. hCab = NtCabInitialize();
  91. if (hCab == NULL) {
  92. return -1;
  93. }
  94. if (!NtCabOpenCabFile( hCab, argv[2] )) {
  95. return -1;
  96. }
  97. if (!NtCabExtractOneFile( hCab, argv[3], NULL )) {
  98. return -1;
  99. }
  100. NtCabClose( hCab );
  101. return 0;
  102. }
  103. if (AddFile) {
  104. hCab = NtCabInitialize();
  105. if (hCab == NULL) {
  106. return -1;
  107. }
  108. if (!NtCabOpenCabFile( hCab, argv[2] )) {
  109. return -1;
  110. }
  111. if (!NtCabReplaceOneFile( hCab, argv[3] )) {
  112. return -1;
  113. }
  114. NtCabClose( hCab );
  115. return 0;
  116. }
  117. if (CreateNewCab) {
  118. hCab = NtCabInitialize();
  119. if (hCab == NULL) {
  120. return -1;
  121. }
  122. if (!NtCabCreateNewCabFile( hCab, argv[2] )) {
  123. return -1;
  124. }
  125. if (argv[3][0] == L'@') {
  126. //
  127. // use a response file for the file list
  128. //
  129. hFile = CreateFile(
  130. &argv[3][1],
  131. GENERIC_READ,
  132. FILE_SHARE_READ,
  133. NULL,
  134. OPEN_EXISTING,
  135. FILE_ATTRIBUTE_NORMAL,
  136. NULL
  137. );
  138. if (hFile == INVALID_HANDLE_VALUE) {
  139. return -1;
  140. }
  141. hMap = CreateFileMapping(
  142. hFile,
  143. NULL,
  144. PAGE_WRITECOPY,
  145. 0,
  146. 0,
  147. NULL
  148. );
  149. if (hMap == NULL) {
  150. return -1;
  151. }
  152. FileList = MapViewOfFile(
  153. hMap,
  154. FILE_MAP_COPY,
  155. 0,
  156. 0,
  157. 0
  158. );
  159. if (FileList == NULL) {
  160. return -1;
  161. }
  162. s = FileList;
  163. while(1) {
  164. e = strchr(s,'\r');
  165. if (e == NULL) {
  166. break;
  167. }
  168. *e = 0;
  169. MultiByteToWideChar(
  170. CP_ACP,
  171. MB_PRECOMPOSED,
  172. s,
  173. -1,
  174. FileName,
  175. sizeof(FileName)/sizeof(WCHAR)
  176. );
  177. NtCabCompressOneFile( hCab, FileName );
  178. s = e + 2;
  179. }
  180. } else {
  181. //
  182. // use a file list off the command line
  183. //
  184. for (i=3; i<(ULONG)argc; i++) {
  185. NtCabCompressOneFile( hCab, argv[i] );
  186. }
  187. }
  188. NtCabClose( hCab );
  189. return 0;
  190. }
  191. return 0;
  192. }