summaryrefslogtreecommitdiffstats
path: root/tools/msiinfo.c
blob: ac299fd5a35e336dcff33b2f08edbaa60de2c7ab (plain)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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
133
134
135
136
137
138
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
165
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
/*
 * msiinfo - MSI inspection tool
 *
 * Copyright 2012 Red Hat, Inc.
 *
 * Author: Paolo Bonzini <pbonzini@redhat.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 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
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include "config.h"
#include "libmsi.h"

#include <glib.h>
#include <stdio.h>
#include <assert.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <limits.h>

struct Command {
    const char *cmd;
    const char *desc;
    const char *usage;
    const char *opts;
    const char *help;
    int (*func)(struct Command *cmd, int argc, char **argv, GError **error);
};

static struct Command cmds[];

static void usage(FILE *out)
{
    int i;

    fprintf(out, "Usage: %s SUBCOMMAND COMMAND-OPTIONS...\n\n", g_get_prgname ());
    fprintf(out, "Options:\n");
    fprintf(out, "  -h, --help        Show program usage\n");
    fprintf(out, "  -v, --version     Display program version\n\n");
    fprintf(out, "Available subcommands:\n");
    for (i = 0; cmds[i].cmd; i++) {
        if (cmds[i].desc) {
            fprintf(out, "  %-18s%s\n", cmds[i].cmd, cmds[i].desc);
        }
    }
    exit (out == stderr);
}

static void cmd_usage(FILE *out, struct Command *cmd)
{
    fprintf(out, "%s %s %s\n\n%s.\n", g_get_prgname (), cmd->cmd, cmd->opts,
            cmd->desc);

    if (cmd->help) {
        fprintf(out, "\n%s\n", cmd->help);
    }
    exit (out == stderr);
}

static void print_libmsi_error(LibmsiResultError r)
{
    switch (r)
    {
    case LIBMSI_RESULT_SUCCESS:
        abort();
    case LIBMSI_RESULT_CONTINUE:
        fprintf(stderr, "%s: internal error (continue)\n", g_get_prgname ());
        exit(1);
    case LIBMSI_RESULT_MORE_DATA:
        fprintf(stderr, "%s: internal error (more data)\n", g_get_prgname ());
        exit(1);
    case LIBMSI_RESULT_INVALID_HANDLE:
        fprintf(stderr, "%s: internal error (invalid handle)\n", g_get_prgname ());
        exit(1);
    case LIBMSI_RESULT_NOT_ENOUGH_MEMORY:
    case LIBMSI_RESULT_OUTOFMEMORY:
        fprintf(stderr, "%s: out of memory\n", g_get_prgname ());
        exit(1);
    case LIBMSI_RESULT_INVALID_DATA:
        fprintf(stderr, "%s: invalid data\n", g_get_prgname ());
        exit(1);
    case LIBMSI_RESULT_INVALID_PARAMETER:
        fprintf(stderr, "%s: invalid parameter\n", g_get_prgname ());
        exit(1);
    case LIBMSI_RESULT_OPEN_FAILED:
        fprintf(stderr, "%s: open failed\n", g_get_prgname ());
        exit(1);
    case LIBMSI_RESULT_CALL_NOT_IMPLEMENTED:
        fprintf(stderr, "%s: not implemented\n", g_get_prgname ());
        exit(1);
    case LIBMSI_RESULT_NOT_FOUND:
        fprintf(stderr, "%s: not found\n", g_get_prgname ());
        exit(1);
    case LIBMSI_RESULT_UNKNOWN_PROPERTY:
        fprintf(stderr, "%s: unknown property\n", g_get_prgname ());
        exit(1);
    case LIBMSI_RESULT_BAD_QUERY_SYNTAX:
        fprintf(stderr, "%s: bad query syntax\n", g_get_prgname ());
        exit(1);
    case LIBMSI_RESULT_INVALID_FIELD:
        fprintf(stderr, "%s: invalid field\n", g_get_prgname ());
        exit(1);
    case LIBMSI_RESULT_FUNCTION_FAILED:
        fprintf(stderr, "%s: internal error (function failed)\n", g_get_prgname ());
        exit(1);
    case LIBMSI_RESULT_INVALID_TABLE:
        fprintf(stderr, "%s: invalid table\n", g_get_prgname ());
        exit(1);
    case LIBMSI_RESULT_DATATYPE_MISMATCH:
        fprintf(stderr, "%s: datatype mismatch\n", g_get_prgname ());
        exit(1);
    case LIBMSI_RESULT_INVALID_DATATYPE:
        fprintf(stderr, "%s: invalid datatype\n", g_get_prgname ());
        exit(1);
    default:
        g_warn_if_reached ();
    }
}

static struct Command *find_cmd(const char *s)
{
    int i;

    for (i = 0; cmds[i].cmd; i++) {
        if (!strcmp(s, cmds[i].cmd)) {
            return &cmds[i];
        }
    }

    fprintf(stderr, "%s: Unrecognized command '%s'\n", g_get_prgname (), s);
    return NULL;
}

static void print_strings_from_query(LibmsiQuery *query, GError **error)
{
    GError *err = NULL;
    LibmsiRecord *rec = NULL;
    gchar *name;

    while ((rec = libmsi_query_fetch(query, &err))) {
        name = libmsi_record_get_string(rec, 1);
        g_return_if_fail(name != NULL);

        puts(name);

        g_free(name);
        g_object_unref(rec);
    }

    if (err)
        g_propagate_error(error, err);

    g_clear_error(&err);
}

static int cmd_streams(struct Command *cmd, int argc, char **argv, GError **error)
{
    LibmsiDatabase *db = NULL;
    LibmsiQuery *query = NULL;
    int r = 1;

    if (argc != 2) {
        cmd_usage(stderr, cmd);
    }

    db = libmsi_database_new(argv[1], LIBMSI_DB_FLAGS_READONLY, NULL, error);
    if (!db)
        goto end;

    query = libmsi_query_new(db, "SELECT `Name` FROM `_Streams`", error);
    if (!query)
        goto end;

    libmsi_query_execute(query, NULL, error);
    if (*error)
        goto end;

    print_strings_from_query(query, error);
    r = 0;

end:
    if (query)
        g_object_unref(query);
    if (db)
        g_object_unref(db);

    return r;
}

static int cmd_tables(struct Command *cmd, int argc, char **argv, GError **error)
{
    LibmsiDatabase *db = NULL;
    LibmsiQuery *query = NULL;
    int r = 1;

    if (argc != 2) {
        cmd_usage(stderr, cmd);
    }

    db = libmsi_database_new(argv[1], LIBMSI_DB_FLAGS_READONLY, NULL, error);
    if (!db)
        goto end;

    query = libmsi_query_new(db, "SELECT `Name` FROM `_Tables`", error);
    if (!query)
        goto end;

    r = libmsi_query_execute(query, NULL, error);
    if (*error)
        goto end;

    puts("_SummaryInformation");
    puts("_ForceCodepage");
    print_strings_from_query(query, error);

    r = 0;

end:
    if (query)
        g_object_unref(query);
    if (db)
        g_object_unref(db);

    return r;
}

static void print_suminfo(LibmsiSummaryInfo *si, int prop, const char *name)
{
    GError *error = NULL;
    unsigned type;
    const gchar* str;
    int val;
    uint64_t valtime;
    time_t t;

    type = libmsi_summary_info_get_property_type(si, prop, &error);
    if (error)
        goto end;

    switch (type) {
    case LIBMSI_PROPERTY_TYPE_INT:
        val = libmsi_summary_info_get_int(si, prop, &error);
        if (error)
            goto end;
        printf ("%s: %d (%x)\n", name, val, val);
        break;

    case LIBMSI_PROPERTY_TYPE_STRING:
        str = libmsi_summary_info_get_string(si, prop, &error);
        if (error)
            goto end;
        printf ("%s: %s\n", name, str);
        break;

    case LIBMSI_PROPERTY_TYPE_FILETIME:
        valtime = libmsi_summary_info_get_filetime(si, prop, &error);
        if (error)
            goto end;
        /* Convert nanoseconds since 1601 to seconds since Unix epoch.  */
        t = (valtime / 10000000) - (uint64_t) 134774 * 86400;
        printf ("%s: %s", name, ctime(&t));
        break;

    case LIBMSI_PROPERTY_TYPE_EMPTY:
	break;

    default:
	abort();
    }

end:
    if (error)
        g_warning("Can't print summary info: %s", error->message);
    g_clear_error(&error);
}

static int cmd_suminfo(struct Command *cmd, int argc, char **argv, GError **error)
{
    LibmsiDatabase *db = NULL;
    LibmsiSummaryInfo *si = NULL;

    if (argc != 2) {
        cmd_usage(stderr, cmd);
    }

    db = libmsi_database_new(argv[1], LIBMSI_DB_FLAGS_READONLY, NULL, error);
    if (!db)
        goto end;

    si = libmsi_summary_info_new(db, 0, error);
    if (!si)
        goto end;

    print_suminfo(si, LIBMSI_PROPERTY_TITLE, "Title");
    print_suminfo(si, LIBMSI_PROPERTY_SUBJECT, "Subject");
    print_suminfo(si, LIBMSI_PROPERTY_AUTHOR, "Author");
    print_suminfo(si, LIBMSI_PROPERTY_KEYWORDS, "Keywords");
    print_suminfo(si, LIBMSI_PROPERTY_COMMENTS, "Comments");
    print_suminfo(si, LIBMSI_PROPERTY_TEMPLATE, "Template");
    print_suminfo(si, LIBMSI_PROPERTY_LASTAUTHOR, "Last author");
    print_suminfo(si, LIBMSI_PROPERTY_UUID, "Revision number (UUID)");
    print_suminfo(si, LIBMSI_PROPERTY_EDITTIME, "Edittime");
    print_suminfo(si, LIBMSI_PROPERTY_LASTPRINTED, "Last printed");
    print_suminfo(si, LIBMSI_PROPERTY_CREATED_TM, "Created");
    print_suminfo(si, LIBMSI_PROPERTY_LASTSAVED_TM, "Last saved");
    print_suminfo(si, LIBMSI_PROPERTY_VERSION, "Version");
    print_suminfo(si, LIBMSI_PROPERTY_SOURCE, "Source");
    print_suminfo(si, LIBMSI_PROPERTY_RESTRICT, "Restrict");
    print_suminfo(si, LIBMSI_PROPERTY_THUMBNAIL, "Thumbnail");
    print_suminfo(si, LIBMSI_PROPERTY_APPNAME, "Application");
    print_suminfo(si, LIBMSI_PROPERTY_SECURITY, "Security");

 end:
    if (db)
        g_object_unref(db);
    if (si)
        g_object_unref(si);

    return *error ? 1 : 0;
}

static void full_write(int fd, char *buf, size_t sz)
{
    while (sz > 0) {
        ssize_t rc = write(fd, buf, sz);
        if (rc < 0) {
            perror("write");
            exit(1);
        }
        buf += rc;
        sz -= rc;
    }
}

static int cmd_extract(struct Command *cmd, int argc, char **argv, GError **error)
{
    LibmsiDatabase *db = NULL;
    LibmsiQuery *query = NULL;
    LibmsiRecord *rec = NULL;
    GInputStream *in = NULL;
    int r = 1;
    char buffer[4096];
    size_t n_read;

    if (argc != 3) {
        cmd_usage(stderr, cmd);
    }

    db = libmsi_database_new(argv[1], LIBMSI_DB_FLAGS_READONLY, NULL, error);
    if (!db)
        goto end;

    query = libmsi_query_new(db, "SELECT `Data` FROM `_Streams` WHERE `Name` = ?", error);
    if (*error)
        goto end;

    rec = libmsi_record_new(1);
    libmsi_record_set_string(rec, 1, argv[2]);
    r = libmsi_query_execute(query, rec, error);
    if (*error)
        goto end;
    g_object_unref(rec);

    rec = libmsi_query_fetch(query, error);
    if (*error)
        goto end;

#if O_BINARY
    _setmode(STDOUT_FILENO, O_BINARY);
#endif

    in = G_INPUT_STREAM (libmsi_record_get_stream(rec, 1));
    for (;;) {
        n_read = g_input_stream_read (in, buffer, sizeof (buffer), NULL, error);
        if (n_read == -1)
            goto end;
        if (n_read == 0)
            break;

	full_write (STDOUT_FILENO, buffer, n_read);
    }

    if (!*error)
        r = 0;

end:
    if (in)
        g_object_unref(in);
    if (rec)
        g_object_unref(rec);
    if (query)
        g_object_unref(query);
    if (db)
        g_object_unref(db);

    return r;
}

static gboolean export_create_table(const char *table,
                                    LibmsiRecord *names,
                                    LibmsiRecord *types,
                                    LibmsiRecord *keys)
{
    guint num_columns = libmsi_record_get_field_count(names);
    guint num_keys = libmsi_record_get_field_count(keys);
    guint i, len;
    char size[20], extra[30];
    gchar *name, *type;

    if (!strcmp(table, "_Tables") ||
        !strcmp(table, "_Columns") ||
        !strcmp(table, "_Streams") ||
        !strcmp(table, "_Storages")) {
        return 0;
    }

    printf("CREATE TABLE `%s` (", table);
    for (i = 1; i <= num_columns; i++)
    {
        name = libmsi_record_get_string(names, i);
        g_return_val_if_fail(name != NULL, FALSE);

        type = libmsi_record_get_string(types, i);
        g_return_val_if_fail(type != NULL, FALSE);

        if (i > 1) {
            printf(", ");
        }

        extra[0] = '\0';
        if (g_ascii_islower(type[0])) {
            strcat(extra, " NOT NULL");
        }

        switch (type[0])
        {
            case 'l': case 'L':
                strcat(extra, " LOCALIZABLE");
                /* fall through */
            case 's': case 'S':
                strcpy(size, type+1);
                sprintf(type, "CHAR(%s)", size);
                break;
            case 'i': case 'I':
                len = atol(type + 1);
                if (len <= 2)
                    strcpy(type, "INT");
                else if (len == 4)
                    strcpy(type, "LONG");
                else
                    abort();
                break;
            case 'v': case 'V':
                strcpy(type, "OBJECT");
                break;
            default:
                abort();
        }

        printf("`%s` %s%s", name, type, extra);
        g_free(name);
        g_free(type);
    }

    for (i = 1; i <= num_keys; i++)
    {
        name = libmsi_record_get_string(names, i);
        g_return_val_if_fail(name != NULL, FALSE);

        printf("%s `%s`", (i > 1 ? "," : " PRIMARY KEY"), name);
        g_free(name);
    }
    printf(")\n");
    return TRUE;
}

static void print_quoted_string(const char *s)
{
    putchar('\'');
    for (; *s; s++) {
        switch (*s) {
        case '\n': putchar('\\'); putchar('n'); break;
        case '\r': putchar('\\'); putchar('r'); break;
        case '\\': putchar('\\'); putchar('\\'); break;
        case '\'': putchar('\\'); putchar('\''); break;
        default: putchar(*s);
        }
    }
    putchar('\'');
}

static gboolean export_insert(const char *table,
                              LibmsiRecord *names,
                              LibmsiRecord *types,
                              LibmsiRecord *vals)
{
    guint num_columns = libmsi_record_get_field_count(names);
    gchar *name, *type;
    guint i;
    char *s;

    printf("INSERT INTO `%s` (", table);
    for (i = 1; i <= num_columns; i++)
    {
        if (libmsi_record_is_null(vals, i)) {
            continue;
        }

        name = libmsi_record_get_string(names, i);
        g_return_val_if_fail(name != NULL, FALSE);

        if (i > 1) {
            printf(", ");
        }

        printf("`%s`", name);
        g_free(name);
    }
    printf(") VALUES (");
    for (i = 1; i <= num_columns; i++)
    {
        if (libmsi_record_is_null(vals, i)) {
            continue;
        }

        if (i > 1) {
            printf(", ");
        }

        type = libmsi_record_get_string(types, i);
        g_return_val_if_fail(type != NULL, FALSE);

        switch (type[0])
        {
            case 'l': case 'L':
            case 's': case 'S':
                s = libmsi_record_get_string(vals, i);
                print_quoted_string(s);
                g_free(s);
                break;

            case 'i': case 'I':
                printf("%d", libmsi_record_get_int(vals, i));
                break;
            case 'v': case 'V':
                printf("''");
                break;
            default:
                abort();
        }

        g_free(type);
    }
    printf(")\n");
    return TRUE;
}

static gboolean export_sql( LibmsiDatabase *db, const char *table, GError **error)
{
    GError *err = NULL;
    LibmsiRecord *name = NULL;
    LibmsiRecord *type = NULL;
    LibmsiRecord *keys = NULL;
    LibmsiRecord *rec = NULL;
    LibmsiQuery *query = NULL;
    gboolean success = FALSE;
    char *sql;

    sql = g_strdup_printf("select * from `%s`", table);
    query = libmsi_query_new(db, sql, error);
    if (*error) {
        goto done;
    }

    /* write out row 1, the column names */
    name = libmsi_query_get_column_info(query, LIBMSI_COL_INFO_NAMES, error);
    if (*error) {
        goto done;
    }

    /* write out row 2, the column types */
    type = libmsi_query_get_column_info(query, LIBMSI_COL_INFO_TYPES, error);
    if (*error) {
        goto done;
    }

    /* write out row 3, the table name + keys */
    keys = libmsi_database_get_primary_keys(db, table, error);
    if (!keys) {
        goto done;
    }

    if (!export_create_table(table, name, type, keys))
        goto done;

    /* write out row 4 onwards, the data */
    while ((rec = libmsi_query_fetch(query, &err))) {
        success = export_insert(table, name, type, rec);
        g_object_unref(rec);
        if (!success) {
            break;
        }
    }

    if (err) {
        g_propagate_error(error, err);
        success = FALSE;
    }

    g_clear_error(&err);

done:
    g_object_unref(name);
    g_object_unref(type);
    g_object_unref(keys);
    g_object_unref(query);
    return success;
}

static int cmd_export(struct Command *cmd, int argc, char **argv, GError **error)
{
    LibmsiDatabase *db = NULL;
    gboolean sql = FALSE;

    if (argc > 1 && !strcmp(argv[1], "-s")) {
        sql = TRUE;
        argc--;
        argv++;
    }

    if (argc != 3) {
        cmd_usage(stderr, cmd);
    }

    db = libmsi_database_new(argv[1], LIBMSI_DB_FLAGS_READONLY, NULL, error);
    if (!db)
        return 1;

    if (sql) {
        if (!export_sql(db, argv[2], error))
            return 1;
    } else {
#if O_BINARY
        _setmode(STDOUT_FILENO, O_BINARY);
#endif
        if (!libmsi_database_export(db, argv[2], STDOUT_FILENO, error))
            goto end;
    }

end:
    if (db)
        g_object_unref(db);

    return *error ? 1 : 0;
}

static int cmd_version(struct Command *cmd, int argc, char **argv, GError **error)
{
    printf("%s (%s) version %s\n", g_get_prgname (), PACKAGE, VERSION);
    return 0;
}

static int cmd_help(struct Command *cmd, int argc, char **argv, GError **error)
{
    if (argc > 1) {
        cmd = find_cmd(argv[1]);
        if (cmd && cmd->desc) {
            cmd_usage(stdout, cmd);
        }
    }

    usage(stdout);

    return 0;
}

static struct Command cmds[] = {
    {
        .cmd = "help",
        .opts = "[SUBCOMMAND]",
        .desc = "Show program or subcommand usage",
        .func = cmd_help,
    },
    {
        .cmd = "streams",
        .opts = "FILE",
        .desc = "List streams in a .msi file",
        .func = cmd_streams,
    },
    {
        .cmd = "tables",
        .opts = "FILE",
        .desc = "List tables in a .msi file",
        .func = cmd_tables,
    },
    {
        .cmd = "extract",
        .opts = "FILE STREAM",
        .desc = "Extract a binary stream from an .msi file",
        .func = cmd_extract,
    },
    {
        .cmd = "export",
        .opts = "[-s] FILE TABLE\n\nOptions:\n"
                "  -s                Format output as an SQL query",
        .desc = "Export a table in text form from an .msi file",
        .func = cmd_export,
    },
    {
        .cmd = "suminfo",
        .opts = "FILE",
        .desc = "Print summary information",
        .func = cmd_suminfo,
    },
    {
        .cmd = "-h",
        .func = cmd_help,
    },
    {
        .cmd = "--help",
        .func = cmd_help,
    },
    {
        .cmd = "-v",
        .func = cmd_version
    },
    {
        .cmd = "--version",
        .func = cmd_version
    },
    { NULL },
};

int main(int argc, char **argv)
{
    GError *error = NULL;
    struct Command *cmd = NULL;

#if !GLIB_CHECK_VERSION(2,35,1)
    g_type_init ();
#endif
    g_set_prgname ("msiinfo");

    if (argc == 1) {
        usage(stderr);
    }

    cmd = find_cmd(argv[1]);
    if (!cmd) {
        fprintf(stderr, "%s: Unrecognized command\n", g_get_prgname ());
        usage(stderr);
    }

    int result = cmd->func(cmd, argc - 1, argv + 1, &error);
    if (error != NULL) {
        g_printerr("error: %s\n", error->message);
        print_libmsi_error(error->code);
        g_clear_error(&error);
    }

    return result;
}