summaryrefslogtreecommitdiff
path: root/setup_native/source/win32/wintools/makecab/makecab.c
blob: a5c7acda1a8eb83209920896f450fb41227d3989 (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
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#include <zlib.h>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

#include "parseddf.h"

#define _O_RDONLY 0
#define _O_WRONLY 1
#define _O_RDWR 2
#define _O_CREAT 0x0100

#define _A_RDONLY 1
#define _A_HIDDEN 2
#define _A_SYSTEM 4
#define _A_ARCH 0x20

typedef enum CABLOGLEVEL { CABLOG_ERR = 1, CABLOG_WRN, CABLOG_MSG } CABLOGLEVEL;
typedef enum CABERR { CAB_OK, CABERR_DDF_UNOPENED } CABERR;
static unsigned int CabVerb = 0;
static const char * FCI_ERRS[] = {"None", "Error opening source file", "Error reading source file", "Error allocating memory", "Error creating temporary file", "Unknown compression type specified", "Error creating cabinet file", "Compression was manually aborted", "Error compressing data", "Cabinet data size or file count limits exceeded"};

void cabLog(CABLOGLEVEL lvl, char * msg, ...)
{
    va_list args;
    if (CabVerb < lvl) return;

    switch (lvl)
    {
    case CABLOG_WRN: printf("[Warning] "); break;
    case CABLOG_ERR: printf("[Error] "); break;
    }

    va_start(args, msg);
    vprintf(msg, args);
    va_end(args);
    printf("\n");
}

void cabLogErr(PERF erf, char * msg)
{
    if (!erf)
    {
        cabLog(CABLOG_ERR, "%s: An unknown problem occurred");
        return;
    }

    if (erf->erfOper >= 0)
    {
        if (erf->erfOper < SAL_N_ELEMENTS(FCI_ERRS))
            cabLog(CABLOG_ERR, "%s: %s", msg, FCI_ERRS[erf->erfOper]);
        else
            cabLog(CABLOG_ERR, "%s: Unknown error", msg);
    }
}

void cabLogCCAB(PCCAB cc)
{
    cabLog(CABLOG_MSG,
           "cb                 %ld\n"
           "cbFolderThresh     %ld\n"
           "cbReserveCFHeader  %d\n"
           "cbReserveCFFolder  %d\n"
           "cbReserveCFData    %d\n"
           "iCab               %d\n"
           "iDisk              %d\n"
           "setID              %d\n"
           "szDisk             %s\n"
           "szCab              %s\n"
           "szCabPath          %s\n",
           cc->cb, cc->cbFolderThresh, cc->cbReserveCFHeader,
           cc->cbReserveCFFolder, cc->cbReserveCFData, cc->iCab,
           cc->iDisk, cc->setID, cc->szDisk, cc->szCab, cc->szCabPath);
}

/***********************************************************************
 * Define FCI callbacks
 **********************************************************************/
FNFCIALLOC (fnMemAlloc) /*(ULONG cb)*/
{
    return malloc(cb);
}

FNFCIFREE (fnMemFree) /*(void HUGE *memory)*/
{
    free(memory);
}

FNFCIOPEN (fnOpen) /*(char *pszFile, int oflag, int pmode, int *err, void *pv)*/
{
    FILE * f = NULL;
    char * mode = "r+";
    printf("DEBUG: fnOpen file %s\n", pszFile);
    if (oflag & _O_WRONLY) mode = "w";
    else if (oflag & _O_RDWR) mode = "r+";
    else if (oflag & _O_RDONLY) mode = "r";

    if (oflag & _O_CREAT && oflag & _O_RDWR) mode = "w+";

    f = fopen(pszFile, mode);
    if (f == NULL)
    {
        cabLog(CABLOG_ERR, "Could not get handle to file %s", pszFile);
        *err = -1;
    }

    return (INT_PTR) f;
}

FNFCIREAD (fnRead) /*(INT_PTR hf, void FAR *memory, UINT cb, int FAR *err, void FAR *pv)*/
{
    FILE * f = (FILE *)hf;
    size_t r = fread(memory, 1, cb, f);
    printf("DEBUG: fnRead\n");
    if (r < cb)
    {
        if (feof(f))
        {
            cabLog(CABLOG_WRN, "Reached EOF while reading file (%d chars remain of %d requested)", r, cb);
            return r;
        }
        else if (ferror(f))
        {
            cabLog(CABLOG_ERR, "Error while reading file");
        }
        *err = -1;
        return -1;
    }
    return r;
}

FNFCIWRITE (fnWrite) /*(INT_PTR hf, void FAR *memory, UINT cb, int FAR *err, void FAR *pv)*/
{
    unsigned int c;
    printf("DEBUG: fnWrite\n");
    if ( !(hf && (c = fwrite(memory, 1, sizeof(memory), (FILE *)hf))) )
    {
        if(c < cb)
        {
            *err = -1;
            return -1;
        }
    }
    return cb;
}

FNFCICLOSE (fnClose) /*(INT_PTR hf, int FAR *err, void FAR *pv)*/
{
    printf("DEBUG: fnClose\n");
    if ( !(hf && fclose((FILE *)hf) != EOF) )
    {
        *err = -1;
        return -1;
    }
    return 0;
}

FNFCISEEK (fnSeek) /*(INT_PTR hf, long dist, int seektype, int FAR *err, void FAR *pv)*/
{
    printf("DEBUG: fnSeek\n");
    if (fseek((FILE *)hf, dist, seektype) != 0)
    {
        *err = -1;
        return -1;
    }
    return 0;
}

FNFCIDELETE (fnDelete) /*(LPSTR pszFile, int FAR *err, void FAR *pv)*/
{
    printf("DEBUG: fnDelete\n");
    if (remove(pszFile) != 0)
    {
        *err = -1;
        return -1;
    }
    return 0;
}

FNFCIGETTEMPFILE (fnGetTempFile) /*(char *pszTempName[bcount(cbTempName)], int cbTempName[range(>=, MAX_PATH)], void FAR *pv)*/
{
    BOOL success = FALSE;
    CHAR tempPath[L_tmpnam];
    char * r;
    printf("DEBUG: fnGetTempFile\n");
    r = tmpnam(tempPath);
    if (r != NULL)
    {
        success = TRUE;
        strcpy(pszTempName, tempPath);
        cbTempName = strlen(pszTempName);
    }
    return success;
}

FNFCIFILEPLACED (fnFilePlaced) /*(PCCAB *pccab, LPSTR pszFile, long cbFile, BOOL fContinuation, void FAR *pv)*/
{
    printf("DEBUG: fnFilePlaced\n");
    if (fContinuation == FALSE)
        cabLog(CABLOG_MSG, "%s (%d b) has been added to %s", pszFile, cbFile, pccab->szCab);

    return 0;
}

FNFCIGETNEXTCABINET(fnGetNextCab) /*(PCCAB pccab, ULONG cbPrevCab, void FAR *pv)*/
{
    printf("DEBUG: fnGetNextCab\n");
    return TRUE;
}

FNFCISTATUS(fnStatus) /*(UINT typeStatus, ULONG cb1, ULONG cb2, void FAR *pv)*/
{
    switch (typeStatus)
    {
    case statusFile:
        printf("\rCompressing source file (%d bytes compressed from %d)", cb1, cb2);
        break;
    case statusFolder:
        printf("\rCopying data into cabinet (%d of %d)", cb1, cb2);
        break;
    case statusCabinet:
        printf("\rWriting cabinet file (%d of approx. %d bytes)", cb1, cb2);
        return cb2;
    }

    return 0;
}

FNFCIGETOPENINFO(fnGetOpenInfo) /*(LPSTR pszName, USHORT *pdate, USHORT *ptime, USHORT *pattribs, int FAR *err, void FAR *pv)*/
{
    /* pdate: dddddmmmmyyyyyyy - d [1-31] m [1-12] y offset from 1980 */
    /* ptime: sssssmmmmhhhhhhh = s [second/2] m [0-59] h [0-23] */
    struct stat s;
    struct tm * time;
    FILE * f = NULL;
    if (stat(pszName, &s) != -1)
    {
        time = gmtime(&s.st_mtime);
        *pdate = 0;
        *ptime = 0;

        /* Note: tm_year is years since 1900 */
        *pdate = (time->tm_mday << 11) | ((time->tm_mon+1) << 7) | (time->tm_year-80);
        *ptime = ((time->tm_sec / 2) << 11) | (time->tm_min << 6) | (time->tm_hour);
        f = (FILE *) fnOpen(pszName, _O_RDONLY, 0, err, pv);
    }

    if (!f)
    {
        cabLog(CABLOG_ERR, "Could not access file information: %s", pszName);
        return -1;
    }
    return (INT_PTR) f;
}

/*
  MAKECAB [/V[n]] [/D var=value ...] [/L dir] source [destination]
  MAKECAB [/V[n]] [/D var=value ...] /F directive_file [...]
  source  - File to compress.
  destination  - File name to give compressed file. If omitted, the
  last character of the source file name is replaced
  with an underscore (_) and used as the destination.
  /F directives - A file with MakeCAB directives (may be repeated).
  /D var=value - Defines variable with specified value.
  /L dir - Location to place destination (default is current directory).
  /V[n] - Verbosity level (1..3)
*/
void usage(void)
{
    printf(
        "Usage: makecab [/V[n]] /F directive_file\n"
        "\nOptions:\n"
        "/F directives - A file with MakeCAB directives.\n"
        "/V[n] - Verbosity level (1..3)\n");
}

int main(int argc, char *argv[])
{
    int v = 0;
    char * ddfFile = NULL;
    CCAB ddfVars;
    DDFSRCFILE *srcList = NULL;
    DDFSRCFILE *srcListCurr = NULL;
    char * cmd = NULL;
    unsigned int cmdSize = 0;

    while (argv[1] && (argv[1][0] == '-' || argv[1][0] == '/'))
    {
        switch(tolower(argv[1][1]))
        {
        case 'f':              /* Directive file specified */
            argv++; argc--;
            ddfFile = argv[1];
            break;
        case 'v':              /* Verbosity [0-3] */
            switch(argv[1][2])
            {
            case '0': v = 0; break;
            case '1': v = 1; break;
            case '2': v = 2; break;
            case '3': v = 3; break;
            }
            break;
        case '?':
            usage();
            return 0;
        }
        argv++; argc--;
    }
    CabVerb = v;

    if (ddfFile == NULL)
    {
        cabLog(CABLOG_ERR, "No DDF file specified.");
        usage();
        return 0;
    }

    cabLog(CABLOG_MSG, "=== Parsing directive file \"%s\"===", ddfFile);
    switch(ParseDdf(ddfFile, &ddfVars, &srcListCurr, v))
    {
    case DDFERR_UNREAD: cabLog(CABLOG_ERR, "Could not open directive file."); break;
    }
    getcwd(ddfVars.szCabPath, MAX_PATH-1);
    strcat(ddfVars.szCabPath, "/");

    srcList = srcListCurr;
    if (srcList == NULL)
    {
        cabLog(CABLOG_ERR, "No input files were specified.");
        return 2;
    }

    /* Construct system call to lcab */
    for(srcListCurr = srcList; srcListCurr != NULL; srcListCurr = srcListCurr->next)
        cmdSize += strlen(srcListCurr->fileName) + 1;
    cmdSize += strlen(ddfVars.szCabPath) + strlen(ddfVars.szCab);
    cmdSize += 6; /* room for "lcab " and \0 */
    cmd = malloc(cmdSize);
    strcpy(cmd, "lcab ");
    for (srcListCurr = srcList; srcListCurr != NULL; srcListCurr = srcListCurr->next)
    {
        strcat(cmd, srcListCurr->fileName);
        strcat(cmd, " ");
    }
    strcat(cmd, ddfVars.szCabPath);
    strcat(cmd, ddfVars.szCab);

    cabLog(CABLOG_MSG, "syscall: %s\n", cmd);
    system(cmd);
    free(cmd);

    cabLog(CABLOG_MSG, "=== Cleaning up resources ===");
    /* Free list of cab source files */
    for (srcListCurr = srcList; srcListCurr != NULL; )
    {
        struct DDFSRCFILE *const next = srcListCurr->next;
        free(srcListCurr);
        srcListCurr = next;
    }
    cabLog(CABLOG_MSG, "Cabinet file %s/%s created.", ddfVars.szCabPath, ddfVars.szCab);
    return 0;
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */