expand.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * Copyright 1997 Victor Schneider
  3. * Copyright 2002 Alexandre Julliard
  4. * Copyright 2007 Hans Leidekker
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  19. */
  20. #define WIN32_LEAN_AND_MEAN
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <windows.h>
  24. #include <lzexpand.h>
  25. #include <setupapi.h>
  26. static int WINAPIV myprintf(const char* format, ...)
  27. {
  28. va_list va;
  29. char tmp[8192];
  30. DWORD w = 0;
  31. int len;
  32. va_start(va, format);
  33. len = vsnprintf(tmp, sizeof(tmp), format, va);
  34. if (len > 0)
  35. WriteFile(GetStdHandle(STD_ERROR_HANDLE), tmp, len, &w, NULL);
  36. va_end(va);
  37. return w;
  38. }
  39. static UINT CALLBACK set_outfile( PVOID context, UINT notification, UINT_PTR param1, UINT_PTR param2 )
  40. {
  41. FILE_IN_CABINET_INFO_A *info = (FILE_IN_CABINET_INFO_A *)param1;
  42. char buffer[MAX_PATH];
  43. char* basename;
  44. switch (notification)
  45. {
  46. case SPFILENOTIFY_FILEINCABINET:
  47. {
  48. LPSTR outfile = context;
  49. if (outfile[0] != 0)
  50. {
  51. SetLastError( ERROR_NOT_SUPPORTED );
  52. return FILEOP_ABORT;
  53. }
  54. GetFullPathNameA( info->NameInCabinet, sizeof(buffer), buffer, &basename );
  55. strcpy( outfile, basename );
  56. return FILEOP_SKIP;
  57. }
  58. default: return NO_ERROR;
  59. }
  60. }
  61. static UINT CALLBACK extract_callback( PVOID context, UINT notification, UINT_PTR param1, UINT_PTR param2 )
  62. {
  63. FILE_IN_CABINET_INFO_A *info = (FILE_IN_CABINET_INFO_A *)param1;
  64. switch (notification)
  65. {
  66. case SPFILENOTIFY_FILEINCABINET:
  67. {
  68. LPCSTR targetname = context;
  69. strcpy( info->FullTargetName, targetname );
  70. return FILEOP_DOIT;
  71. }
  72. default: return NO_ERROR;
  73. }
  74. }
  75. static BOOL option_equal(LPCSTR str1, LPCSTR str2)
  76. {
  77. if (str1[0] != '/' && str1[0] != '-')
  78. return FALSE;
  79. return !lstrcmpA( str1 + 1, str2 );
  80. }
  81. int __cdecl main(int argc, char *argv[])
  82. {
  83. int ret = 0;
  84. char infile[MAX_PATH], outfile[MAX_PATH], actual_name[MAX_PATH];
  85. char outfile_basename[MAX_PATH], *basename_index;
  86. UINT comp;
  87. if (argc < 3)
  88. {
  89. myprintf( "Usage:\n" );
  90. myprintf( "\t%s infile outfile\n", argv[0] );
  91. myprintf( "\t%s /r infile\n", argv[0] );
  92. return 1;
  93. }
  94. if (argc == 3 && (option_equal(argv[1], "R") || option_equal(argv[1], "r")))
  95. GetFullPathNameA( argv[2], sizeof(infile), infile, NULL );
  96. else
  97. GetFullPathNameA( argv[1], sizeof(infile), infile, NULL );
  98. if (!SetupGetFileCompressionInfoExA( infile, actual_name, sizeof(actual_name), NULL, NULL, NULL, &comp ))
  99. {
  100. myprintf( "%s: can't open input file %s\n", argv[0], infile );
  101. return 1;
  102. }
  103. if (argc == 3 && (option_equal(argv[1], "R") || option_equal(argv[1], "r")))
  104. {
  105. switch (comp)
  106. {
  107. case FILE_COMPRESSION_MSZIP:
  108. outfile_basename[0] = 0;
  109. if (!SetupIterateCabinetA( infile, 0, set_outfile, outfile_basename ))
  110. {
  111. myprintf( "%s: can't determine original name\n", argv[0] );
  112. return 1;
  113. }
  114. GetFullPathNameA( infile, sizeof(outfile), outfile, &basename_index );
  115. *basename_index = 0;
  116. strcat( outfile, outfile_basename );
  117. break;
  118. case FILE_COMPRESSION_WINLZA:
  119. GetExpandedNameA( infile, outfile_basename );
  120. break;
  121. default:
  122. myprintf( "%s: can't determine original\n", argv[0] );
  123. return 1;
  124. }
  125. }
  126. else
  127. GetFullPathNameA( argv[2], sizeof(outfile), outfile, NULL );
  128. if (!lstrcmpiA( infile, outfile ))
  129. {
  130. myprintf( "%s: can't expand file to itself\n", argv[0] );
  131. return 1;
  132. }
  133. switch (comp)
  134. {
  135. case FILE_COMPRESSION_MSZIP:
  136. if (!SetupIterateCabinetA( infile, 0, extract_callback, outfile ))
  137. {
  138. myprintf( "%s: cabinet extraction failed\n", argv[0] );
  139. return 1;
  140. }
  141. break;
  142. case FILE_COMPRESSION_WINLZA:
  143. {
  144. INT hin, hout;
  145. OFSTRUCT ofin, ofout;
  146. LONG error;
  147. if ((hin = LZOpenFileA( infile, &ofin, OF_READ )) < 0)
  148. {
  149. myprintf( "%s: can't open input file %s\n", argv[0], infile );
  150. return 1;
  151. }
  152. if ((hout = LZOpenFileA( outfile, &ofout, OF_CREATE | OF_WRITE )) < 0)
  153. {
  154. LZClose( hin );
  155. myprintf( "%s: can't open output file %s\n", argv[0], outfile );
  156. return 1;
  157. }
  158. error = LZCopy( hin, hout );
  159. LZClose( hin );
  160. LZClose( hout );
  161. if (error < 0)
  162. {
  163. myprintf( "%s: LZCopy failed, error is %d\n", argv[0], error );
  164. return 1;
  165. }
  166. break;
  167. }
  168. default:
  169. if (!CopyFileA( infile, outfile, FALSE ))
  170. {
  171. myprintf( "%s: CopyFileA failed\n", argv[0] );
  172. return 1;
  173. }
  174. break;
  175. }
  176. return ret;
  177. }