expand.c 5.51 KB
Newer Older
1 2
/*
 * Copyright 1997 Victor Schneider
3
 * Copyright 2002 Alexandre Julliard
4
 * Copyright 2007 Hans Leidekker
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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 22
#define WIN32_LEAN_AND_MEAN

Alexandre Julliard's avatar
Alexandre Julliard committed
23 24 25 26
#include <stdio.h>
#include <string.h>
#include <windows.h>
#include <lzexpand.h>
27 28
#include <setupapi.h>

29 30 31 32
static int myprintf(const char* format, ...)
{
    va_list     va;
    char        tmp[8192];
33
    DWORD       w = 0;
34 35 36 37 38 39 40 41 42 43
    int         len;

    va_start(va, format);
    len = vsnprintf(tmp, sizeof(tmp), format, va);
    if (len > 0)
        WriteFile(GetStdHandle(STD_ERROR_HANDLE), tmp, len, &w, NULL);
    va_end(va);
    return w;
}

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
static UINT CALLBACK set_outfile( PVOID context, UINT notification, UINT_PTR param1, UINT_PTR param2 )
{
    FILE_IN_CABINET_INFO_A *info = (FILE_IN_CABINET_INFO_A *)param1;
    char buffer[MAX_PATH];
    char* basename;

    switch (notification)
    {
    case SPFILENOTIFY_FILEINCABINET:
    {
        LPSTR outfile = context;
        if (outfile[0] != 0)
        {
            SetLastError( ERROR_NOT_SUPPORTED );
            return FILEOP_ABORT;
        }
        GetFullPathNameA( info->NameInCabinet, sizeof(buffer), buffer, &basename );
        strcpy( outfile, basename );
        return FILEOP_SKIP;
    }
    default: return NO_ERROR;
    }
}

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
static UINT CALLBACK extract_callback( PVOID context, UINT notification, UINT_PTR param1, UINT_PTR param2 )
{
    FILE_IN_CABINET_INFO_A *info = (FILE_IN_CABINET_INFO_A *)param1;

    switch (notification)
    {
    case SPFILENOTIFY_FILEINCABINET:
    {
        LPCSTR targetname = context;

        strcpy( info->FullTargetName, targetname );
        return FILEOP_DOIT;
    }
    default: return NO_ERROR;
    }
}
Alexandre Julliard's avatar
Alexandre Julliard committed
84

85 86 87 88 89 90 91
static BOOL option_equal(LPCSTR str1, LPCSTR str2)
{
    if (str1[0] != '/' && str1[0] != '-')
        return FALSE;
    return !lstrcmpA( str1 + 1, str2 );
}

92
int main(int argc, char *argv[])
Alexandre Julliard's avatar
Alexandre Julliard committed
93
{
94 95
    int ret = 0;
    char infile[MAX_PATH], outfile[MAX_PATH], actual_name[MAX_PATH];
96
    char outfile_basename[MAX_PATH], *basename_index;
97 98 99 100
    UINT comp;

    if (argc < 3)
    {
101 102 103
        myprintf( "Usage:\n" );
        myprintf( "\t%s infile outfile\n", argv[0] );
        myprintf( "\t%s /r infile\n", argv[0] );
104 105 106
        return 1;
    }

107 108 109 110
    if (argc == 3 && (option_equal(argv[1], "R") || option_equal(argv[1], "r")))
        GetFullPathNameA( argv[2], sizeof(infile), infile, NULL );
    else
        GetFullPathNameA( argv[1], sizeof(infile), infile, NULL );
111

112
    if (!SetupGetFileCompressionInfoExA( infile, actual_name, sizeof(actual_name), NULL, NULL, NULL, &comp ))
113
    {
114
        myprintf( "%s: can't open input file %s\n", argv[0], infile );
115 116
        return 1;
    }
117 118

    if (argc == 3 && (option_equal(argv[1], "R") || option_equal(argv[1], "r")))
119
    {
120 121 122 123
        switch (comp)
        {
        case FILE_COMPRESSION_MSZIP:
            outfile_basename[0] = 0;
124
            if (!SetupIterateCabinetA( infile, 0, set_outfile, outfile_basename ))
125
            {
126
                myprintf( "%s: can't determine original name\n", argv[0] );
127 128 129 130 131 132 133 134 135 136
                return 1;
            }
            GetFullPathNameA( infile, sizeof(outfile), outfile, &basename_index );
            *basename_index = 0;
            strcat( outfile, outfile_basename );
            break;
        case FILE_COMPRESSION_WINLZA:
            GetExpandedNameA( infile, outfile_basename );
            break;
        default:
137
            myprintf( "%s: can't determine original\n", argv[0] );
138 139 140 141 142 143 144 145
            return 1;
        }
    }
    else
        GetFullPathNameA( argv[2], sizeof(outfile), outfile, NULL );

    if (!lstrcmpiA( infile, outfile ))
    {
146
        myprintf( "%s: can't expand file to itself\n", argv[0] );
147 148 149 150 151 152
        return 1;
    }

    switch (comp)
    {
    case FILE_COMPRESSION_MSZIP:
153
        if (!SetupIterateCabinetA( infile, 0, extract_callback, outfile ))
154
        {
155
            myprintf( "%s: cabinet extraction failed\n", argv[0] );
156 157 158 159 160 161 162 163 164
            return 1;
        }
        break;
    case FILE_COMPRESSION_WINLZA:
    {
        INT hin, hout;
        OFSTRUCT ofin, ofout;
        LONG error;

165
        if ((hin = LZOpenFileA( infile, &ofin, OF_READ )) < 0)
166
        {
167
            myprintf( "%s: can't open input file %s\n", argv[0], infile );
168 169
            return 1;
        }
170
        if ((hout = LZOpenFileA( outfile, &ofout, OF_CREATE | OF_WRITE )) < 0)
171 172
        {
            LZClose( hin );
173
            myprintf( "%s: can't open output file %s\n", argv[0], outfile );
174 175 176 177 178 179 180 181 182
            return 1;
        }
        error = LZCopy( hin, hout );

        LZClose( hin );
        LZClose( hout );

        if (error < 0)
        {
183
            myprintf( "%s: LZCopy failed, error is %d\n", argv[0], error );
184 185 186 187 188 189 190
            return 1;
        }
        break;
    }
    default:
        if (!CopyFileA( infile, outfile, FALSE ))
        {
191
            myprintf( "%s: CopyFileA failed\n", argv[0] );
192 193 194 195 196
            return 1;
        }
        break;
    }
    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
197
}