[LT2013]
[reactos.git] / base / system / expand / expand.c
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
21 #define WIN32_NO_STATUS
22 #include <stdarg.h>
23 #include <windef.h>
24 #include <winbase.h>
25 #include <winreg.h>
26 #include <winuser.h>
27 #include <stdio.h>
28 #include <lzexpand.h>
29 #include <setupapi.h>
30
31 static UINT CALLBACK set_outfile( PVOID context, UINT notification, UINT_PTR param1, UINT_PTR param2 )
32 {
33 FILE_IN_CABINET_INFO_A *info = (FILE_IN_CABINET_INFO_A *)param1;
34 char buffer[MAX_PATH];
35 char* basename;
36
37 switch (notification)
38 {
39 case SPFILENOTIFY_FILEINCABINET:
40 {
41 LPSTR outfile = context;
42 if (outfile[0] != 0)
43 {
44 SetLastError( ERROR_NOT_SUPPORTED );
45 return FILEOP_ABORT;
46 }
47 GetFullPathNameA( info->NameInCabinet, sizeof(buffer), buffer, &basename );
48 strcpy( outfile, basename );
49 return FILEOP_SKIP;
50 }
51 default: return NO_ERROR;
52 }
53 }
54
55 static UINT CALLBACK extract_callback( PVOID context, UINT notification, UINT_PTR param1, UINT_PTR param2 )
56 {
57 FILE_IN_CABINET_INFO_A *info = (FILE_IN_CABINET_INFO_A *)param1;
58
59 switch (notification)
60 {
61 case SPFILENOTIFY_FILEINCABINET:
62 {
63 LPCSTR targetname = context;
64
65 strcpy( info->FullTargetName, targetname );
66 return FILEOP_DOIT;
67 }
68 default: return NO_ERROR;
69 }
70 }
71
72 static BOOL option_equal(LPCSTR str1, LPCSTR str2)
73 {
74 if (str1[0] != '/' && str1[0] != '-')
75 return FALSE;
76 return !lstrcmpA( str1 + 1, str2 );
77 }
78
79 int main(int argc, char *argv[])
80 {
81 int ret = 0;
82 char infile[MAX_PATH], outfile[MAX_PATH], actual_name[MAX_PATH];
83 char outfile_basename[MAX_PATH], *basename_index;
84 UINT comp;
85
86 if (argc < 3)
87 {
88 fprintf( stderr, "Usage:\n" );
89 fprintf( stderr, "\t%s infile outfile\n", argv[0] );
90 fprintf( stderr, "\t%s /r infile\n", argv[0] );
91 return 1;
92 }
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
99 if (!SetupGetFileCompressionInfoExA( infile, actual_name, sizeof(actual_name), NULL, NULL, NULL, &comp ))
100 {
101 fprintf( stderr, "%s: can't open input file %s\n", argv[0], infile );
102 return 1;
103 }
104
105 if (argc == 3 && (option_equal(argv[1], "R") || option_equal(argv[1], "r")))
106 {
107 switch (comp)
108 {
109 case FILE_COMPRESSION_MSZIP:
110 {
111 outfile_basename[0] = 0;
112 if (!SetupIterateCabinetA( infile, 0, set_outfile, outfile_basename ))
113 {
114 fprintf( stderr, "%s: can't determine original name\n", argv[0] );
115 return 1;
116 }
117 GetFullPathNameA( infile, sizeof(outfile), outfile, &basename_index );
118 *basename_index = 0;
119 strcat( outfile, outfile_basename );
120 break;
121 }
122 case FILE_COMPRESSION_WINLZA:
123 {
124 GetExpandedNameA( infile, outfile_basename );
125 break;
126 }
127 default:
128 {
129 fprintf( stderr, "%s: can't determine original\n", argv[0] );
130 return 1;
131 }
132 }
133 }
134 else
135 GetFullPathNameA( argv[2], sizeof(outfile), outfile, NULL );
136
137 if (!lstrcmpiA( infile, outfile ))
138 {
139 fprintf( stderr, "%s: can't expand file to itself\n", argv[0] );
140 return 1;
141 }
142
143 switch (comp)
144 {
145 case FILE_COMPRESSION_MSZIP:
146 {
147 if (!SetupIterateCabinetA( infile, 0, extract_callback, outfile ))
148 {
149 fprintf( stderr, "%s: cabinet extraction failed\n", argv[0] );
150 return 1;
151 }
152 break;
153 }
154 case FILE_COMPRESSION_WINLZA:
155 {
156 INT hin, hout;
157 OFSTRUCT ofin, ofout;
158 LONG error;
159
160 if ((hin = LZOpenFileA( infile, &ofin, OF_READ )) < 0)
161 {
162 fprintf( stderr, "%s: can't open input file %s\n", argv[0], infile );
163 return 1;
164 }
165 if ((hout = LZOpenFileA( outfile, &ofout, OF_CREATE | OF_WRITE )) < 0)
166 {
167 LZClose( hin );
168 fprintf( stderr, "%s: can't open output file %s\n", argv[0], outfile );
169 return 1;
170 }
171 error = LZCopy( hin, hout );
172
173 LZClose( hin );
174 LZClose( hout );
175
176 if (error < 0)
177 {
178 fprintf( stderr, "%s: LZCopy failed, error is %ld\n", argv[0], error );
179 return 1;
180 }
181 break;
182 }
183 default:
184 {
185 if (!CopyFileA( infile, outfile, FALSE ))
186 {
187 fprintf( stderr, "%s: CopyFileA failed\n", argv[0] );
188 return 1;
189 }
190 break;
191 }
192 }
193 return ret;
194 }