main.c 7.18 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
/*
 * winemsibuilder - tool to build MSI packages
 *
 * Copyright 2010 Hans Leidekker for CodeWeavers
 *
 * 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
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#define WIN32_LEAN_AND_MEAN
#define COBJMACROS

#include <stdio.h>
#include <windows.h>
#include <msi.h>
#include <msiquery.h>
#include <objbase.h>

#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(winemsibuilder);

static UINT open_database( const WCHAR *msifile, MSIHANDLE *handle )
{
    UINT r;
    MSIHANDLE hdb;

    if (GetFileAttributesW( msifile ) == INVALID_FILE_ATTRIBUTES)
    {
        r = MsiOpenDatabaseW( msifile, MSIDBOPEN_CREATE, &hdb );
        if (r != ERROR_SUCCESS)
        {
            WINE_ERR( "failed to create package database %s (%u)\n", wine_dbgstr_w(msifile), r );
            return r;
        }
        r = MsiDatabaseCommit( hdb );
        if (r != ERROR_SUCCESS)
        {
            WINE_ERR( "failed to commit database (%u)\n", r );
            MsiCloseHandle( hdb );
            return r;
        }
    }
    else
    {
        r = MsiOpenDatabaseW( msifile, MSIDBOPEN_TRANSACT, &hdb );
        if (r != ERROR_SUCCESS)
        {
            WINE_ERR( "failed to open package database %s (%u)\n", wine_dbgstr_w(msifile), r );
            return r;
        }
    }

    *handle = hdb;
    return ERROR_SUCCESS;
}

static int import_tables( const WCHAR *msifile, WCHAR **tables )
{
    UINT r;
    MSIHANDLE hdb;
    WCHAR *dir;
    DWORD len;

    r = open_database( msifile, &hdb );
    if (r != ERROR_SUCCESS) return 1;

    len = GetCurrentDirectoryW( 0, NULL );
80
    if (!(dir = malloc( (len + 1) * sizeof(WCHAR) )))
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
    {
        MsiCloseHandle( hdb );
        return 1;
    }
    GetCurrentDirectoryW( len + 1, dir );

    while (*tables)
    {
        r = MsiDatabaseImportW( hdb, dir, *tables );
        if (r != ERROR_SUCCESS)
        {
            WINE_ERR( "failed to import table %s (%u)\n", wine_dbgstr_w(*tables), r );
            break;
        }
        tables++;
    }

    if (r == ERROR_SUCCESS)
    {
        r = MsiDatabaseCommit( hdb );
        if (r != ERROR_SUCCESS)
            WINE_ERR( "failed to commit changes (%u)\n", r );
    }

105
    free( dir );
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
    MsiCloseHandle( hdb );
    return (r != ERROR_SUCCESS);
}

/* taken from dlls/msi/table.c */
static int utf2mime( int x )
{
    if (x >= '0' && x <= '9')
        return x - '0';
    if (x >= 'A' && x <= 'Z')
        return x - 'A' + 10;
    if (x >= 'a' && x <= 'z')
        return x - 'a' + 10 + 26;
    if (x == '.')
        return 10 + 26 + 26;
    if (x == '_')
        return 10 + 26 + 26 + 1;
    return -1;
}

#define MAX_STREAM_NAME 0x1f

static WCHAR *encode_stream( const WCHAR *in )
{
    DWORD c, next, count;
    WCHAR *out, *p;

133
    count = lstrlenW( in );
134 135 136 137
    if (count > MAX_STREAM_NAME)
        return NULL;

    count += 2;
138
    if (!(out = malloc( count * sizeof(WCHAR) ))) return NULL;
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
    p = out;
    while (count--)
    {
        c = *in++;
        if (!c)
        {
            *p = c;
            return out;
        }
        if (c < 0x80 && utf2mime( c ) >= 0)
        {
            c = utf2mime( c ) + 0x4800;
            next = *in;
            if (next && next < 0x80)
            {
                next = utf2mime( next );
                if (next != -1)
                {
                     next += 0x3ffffc0;
                     c += next << 6;
                     in++;
                }
            }
        }
        *p++ = c;
    }
165
    free( out );
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
    return NULL;
}

static int add_stream( const WCHAR *msifile, const WCHAR *stream, const WCHAR *file )
{
    UINT r;
    HRESULT hr;
    MSIHANDLE hdb;
    IStorage *stg;
    IStream *stm = NULL;
    HANDLE handle;
    char buffer[4096];
    ULARGE_INTEGER size;
    DWORD low, high, read;
    WCHAR *encname;
    int ret = 1;

    /* make sure we have the right type of file  */
    r = open_database( msifile, &hdb );
    if (r != ERROR_SUCCESS) return 1;
    MsiCloseHandle( hdb );

    hr = StgOpenStorage( msifile, NULL, STGM_TRANSACTED|STGM_READWRITE|STGM_SHARE_EXCLUSIVE, NULL, 0, &stg );
    if (hr != S_OK)
    {
191
        WINE_WARN( "failed to open storage %s (0x%08lx)\n", wine_dbgstr_w(msifile), hr );
192 193 194 195 196 197 198 199 200 201 202
        return 1;
    }
    encname = encode_stream( stream );
    if (!encname)
    {
        WINE_WARN( "failed to encode stream name %s\n", wine_dbgstr_w(stream) );
        goto done;
    }
    hr = IStorage_CreateStream( stg, encname, STGM_CREATE|STGM_WRITE|STGM_SHARE_EXCLUSIVE, 0, 0, &stm );
    if (hr != S_OK)
    {
203
        WINE_WARN( "failed to create stream %s (0x%08lx)\n", wine_dbgstr_w(encname), hr );
204 205 206 207 208
        goto done;
    }
    handle = CreateFileW( file, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL );
    if (handle == INVALID_HANDLE_VALUE)
    {
209
        WINE_WARN( "failed to open file %s (%lu)\n", wine_dbgstr_w(file), GetLastError() );
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
        goto done;
    }
    low = GetFileSize( handle, &high );
    if (low == INVALID_FILE_SIZE || high)
    {
        WINE_WARN( "file %s too big\n", wine_dbgstr_w(file) );
        CloseHandle( handle );
        goto done;
    }
    size.QuadPart = low;
    hr = IStream_SetSize( stm, size );
    if (hr != S_OK) goto done;

    while (ReadFile( handle, buffer, sizeof(buffer), &read, NULL ) && read)
    {
        hr = IStream_Write( stm, buffer, read, NULL );
        if (hr != S_OK) break;
        size.QuadPart -= read;
    }
    CloseHandle( handle );
    if (size.QuadPart)
    {
        WINE_WARN( "failed to write stream contents\n" );
        goto done;
    }
    IStorage_Commit( stg, 0 );
    ret = 0;

done:
239
    free( encname );
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
    if (stm) IStream_Release( stm );
    IStorage_Release( stg );
    return ret;
}

static void show_usage( void )
{
    WINE_MESSAGE(
        "Usage: winemsibuilder [OPTION] [MSIFILE] ...\n"
        "Options:\n"
        "  -i package.msi table1.idt [table2.idt ...]    Import one or more tables into the database.\n"
        "  -a package.msi stream file                    Add 'stream' to storage with contents of 'file'.\n"
        "\nExisting tables or streams will be overwritten. If package.msi does not exist a new file\n"
        "will be created with an empty database.\n"
    );
}

257
int __cdecl wmain( int argc, WCHAR *argv[] )
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
{
    if (argc < 3 || argv[1][0] != '-')
    {
        show_usage();
        return 1;
    }

    switch (argv[1][1])
    {
    case 'i':
        if (argc < 4) break;
        return import_tables( argv[2], argv + 3 );
    case 'a':
        if (argc < 5) break;
        return add_stream( argv[2], argv[3], argv[4] );
    default:
        WINE_WARN( "unknown option\n" );
        break;
    }

    show_usage();
    return 1;
}