summaryrefslogtreecommitdiff
path: root/setup_native/source/win32/wintools/makecab/parseddf.c
blob: cf081e3643ca268eab22777c05a731d083752be4 (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
/* -*- 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 "parseddf.h"
#include <sys/stat.h>
#include <stdarg.h>
#include <stdio.h>
#include <errno.h>

extern int errno;
static const char * VARS[] = {"cabinetname", "reservepercabinetsize", "maxdisksize", "compressiontype", "compress", "compressionmemory", "cabinet", "diskdirectorytemplate"};
static const unsigned int VARS_COUNT = 8;
static int CompMemory = 0;
static TCOMP CompType = tcompTYPE_MSZIP;
static unsigned int DdfVerb = 0;

/* Trim leading whitespace from a null terminated string */
void ltrim(char * str)
{
    unsigned int i = 0;
    while (isspace(str[i]) && str[i] != '\0') i++;
    if (i > 0) memmove(str, str+i, strlen(str)+1-i);
}

/* Trim trailing whitespace from a null terminated string */
void rtrim(char * str)
{
    unsigned int i = strlen(str) - 1;
    while (isspace(str[i]) && i > 0) i--;
    if (i) i++;
    str[i] = '\0';
}

/* Trim trailing and leading whitespace from a null terminated string */
void trim(char * str)
{
    ltrim(str);
    rtrim(str);
}

void ddfLogProgress(DDFLOGLEVEL lvl, char * desc, unsigned int progress)
{
    if (DdfVerb < lvl) return;
    if (progress == 0) printf("  %s: %3d%%", desc, progress);
    else if (progress > 0 && progress < 100) printf("\r  %s: %3d%%", desc, progress);
    else if (progress == 100) printf("\r  %s: 100%%\n", desc);
    fflush(stdout);
}

void ddfLog(DDFLOGLEVEL lvl, char * msg, ...)
{
    va_list args;
    if (DdfVerb < lvl) return;

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

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

void ddfPrintState(DDFLOGLEVEL lvl, PCCAB ccab, DDFSRCFILE * srcList)
{
    static const unsigned int SIZE = 2048;
    char msg[SIZE];
    char srcLine[DDF_MAX_CHARS];
    unsigned int len;
    DDFSRCFILE * srcListCurr;

    if (ccab)
    {
        len = sprintf(msg,
                      "=== Directive file state table ===\n"
                      "CabinetName           %s\n"
                      "ReservePerCabinetSize %d\n"
                      "MaxDiskSize           %d\n"
                      "Compress              %d\n"
                      "CompressionMemory     %d\n"
                      "Cabinet               %d\n"
                      "DiskDirectoryTemplate %s",
                      ccab->szCab,
                      ccab->cbReserveCFHeader,
                      ccab->iDisk,
                      1,
                      CompMemory,
                      1,
                      ccab->szCabPath);
    }
    else
    {
        msg[0] = '\0';
    }
    if (!srcList) printf("DEBUG: No list!\n");

    for (srcListCurr = srcList;
         srcListCurr != NULL && (len = strlen(msg)) < SIZE;
         srcListCurr = srcListCurr->next)
    {
        sprintf(srcLine, "\nsrc: %s (%s) extract? %d MSZIP? %d LZX? %d",
                srcListCurr->fileName, srcListCurr->cabName,
                srcListCurr->extract, (srcListCurr->cmpType == tcompTYPE_MSZIP),
                (srcListCurr->cmpType != tcompTYPE_MSZIP));
        strncat(msg, srcLine, SIZE-strlen(srcLine)-1);
    }
    if (msg[SIZE-1] != '\0') msg[SIZE-1] = '\0';

    ddfLog(lvl, msg);
}

/* Moves the next DDF token from the beginning of *line into *token.
 *
 * Currently supported token delimiters are:
 * whitespace (trimmed when not quoted)
 * ; demarks the start of a comment
 * " escapes whitespace, quotes and =
 * = when setting values
 * \n \r - only if file has endline incompatibility
 *
 * returns:
 * DDF_OK More tokens remain
 * DDFERR_NO_MORE_TOKENS At the end of the (usable) line.
 * DDFERR_INVALID_ENDL Token up to this point is stored, endline is chomped.
 */
DDFERR nextToken(char * token, char * line)
{
    unsigned int i = 0;
    unsigned int r = DDF_OK;
    unsigned int offset = 0;
    int c;
    BOOL esc = FALSE;

    ltrim(line);
    if (line[0] == '\0' || line[0] == ';') return DDFERR_NO_MORE_TOKENS;
    if (line[0] == '=')
    {
        memmove(line, line+1, strlen(line));
        trim(line);
    }

    for (i = 0; i < DDF_MAX_CHARS-1 && i < strlen(line); i++)
    {
        /* Chomp delimiting tokens unless they're escaped */
        c = line[i];
        if (c == '\"')
        {
            esc = !esc;
            offset++;
            /* Compress "" to ", otherwise don't add quote to token*/
            if (i > 0 && line[i-1] != '\"') continue;
        }
        else if (c == '\r' || c == '\n')
        {
            r = DDFERR_INVALID_ENDL;
            break;
        }
        else if(!esc)
        {
            if (isspace(c) || c == '=') { r = DDF_OK; break; }
            else if (c == ';') { r = DDFERR_NO_MORE_TOKENS; break; }
        }

        token[i - offset] = c;

    }
    token[i - offset] = '\0';
    memmove(line, line+i, strlen(line)+1-i);
    return r;
}

DDFERR setVar(DDFVAR key, PCCAB ccab, char * value)
{
    int i = 0;
    char * cp = NULL;

    ddfLog(DDFLOG_MSG, "Setting variable %s=%s...", VARS[key], value);

    switch (key)
    {
    case DDF_CABNAME:
        strncpy(ccab->szCab, value, 255);
        break;
    case DDF_RESERVEPERCAB:
        i = atoi(value);
        if (i < 0) i = 0;
        if (i > DDF_MAX_CABRESERVE) i = DDF_MAX_CABRESERVE;
        if (i % 4 != 0) i -= (i % 4);
        ccab->cbReserveCFHeader = i;
        break;
    case DDF_MAXDISK:
        i = atoi(value);
        ccab->cb = i;
        break;
    case DDF_COMPTYPE:
        if (strcmp(value, "LZX") == 0)
        {
/*      if (CompMemory) CompType = TCOMPfromLZXWindow(CompMemory);
        else CompType = tcompTYPE_LZX;*/
/*TODO: LZX not yet supported in Wine */
            CompType = tcompTYPE_MSZIP;
        }
        else
        {
            if (strcmp(value, "MSZIP") != 0)
                ddfLog(DDFLOG_WRN, "Invalid compression type \"%s\", reverting to MSZIP.", value);
            CompType = tcompTYPE_MSZIP;
        }
        break;
    case DDF_COMPRESS:
        break;
    case DDF_COMPMEM:
        i = atoi(value);
        if (i < 15) i = 15;
        if (i > 21) i = 21;
        CompMemory = i;
        if (CompType == tcompTYPE_LZX) CompType = TCOMPfromLZXWindow(CompMemory);
        break;
    case DDF_CABINET:
        break;
    case DDF_DISKDIRTEMPLATE:
        while ((cp = strchr(value, '*')) != '\0') *cp = '1';
        strncpy(ccab->szCabPath, value, 255);
        break;
    }

    return DDF_OK;
}

DDFERR parseCmd(char * line, PCCAB ccab, char * token)
{
    unsigned int r, i;
    char *p;
    for (p = token ; *p; ++p) *p = tolower(*p);

    /* Set command checks lower case token representing var name
       against known variables. If found, the next token is used as
       the value to assign */
    if (strcmp(token, ".set") == 0)
    {
        r = nextToken(token, line);
        if (r != DDF_OK) return DDFERR_FUNC_FAILED;

        for (p = token ; *p; ++p) *p = tolower(*p);
        if (isdigit(token[strlen(token)-1])) token[strlen(token)-1] = '\0';
        for (i = 0; i < (sizeof(VARS)/sizeof(VARS[0])); i++)
        {
            if (strcmp(token, VARS[i]) == 0)
            {
                r = nextToken(token, line);
                if (r != DDF_OK) return DDFERR_FUNC_FAILED;
                setVar(i, ccab, token);
                break;
            }
        }
    }

    return DDF_OK;
}

char * getCabName(char * name)
{
    char * cName;
    if ((cName = strrchr(name, '/')) || (cName = strrchr(name, '\\')))
        cName++;
    else
        cName = name;
    return cName;
}

DDFERR parseSrc(char * line, DDFSRCFILE ** srcFileList, char * token)
{
    unsigned int r;
    struct stat s;
    DDFSRCFILE * tmpSrcFile;

    /* Check file exists before allocating DDFSRCFILE */
    if (stat(token, &s) == -1 && errno == ENOENT)
    {
        ddfLog(DDFLOG_WRN, "Could not find file \"%s\"", token);
        return DDFERR_SRC_SKIPPED;
    }

    tmpSrcFile = (DDFSRCFILE *) malloc(sizeof(DDFSRCFILE));
    if (!tmpSrcFile) return DDFERR_FUNC_FAILED;
    if (!(*srcFileList))
    {
        *srcFileList = tmpSrcFile;
    }
    else
    {
        while ((*srcFileList)->next != NULL) *srcFileList = (*srcFileList)->next;
        (*srcFileList)->next = tmpSrcFile;
    }

    strncpy(tmpSrcFile->fileName, token, MAX_PATH);
    /* Get cabinet file name or construct from src path if unspecified */
    r = nextToken(token, line);
    if (r != DDF_OK || token[0] == '/')
    {
        strncpy(tmpSrcFile->cabName, getCabName(tmpSrcFile->fileName), DDF_MAX_CABNAME-1);
    }
    else
    {
        strncpy(tmpSrcFile->cabName, token, DDF_MAX_CABNAME-1);
    }

    tmpSrcFile->cabName[DDF_MAX_CABNAME-1] = '\0';
    tmpSrcFile->extract = FALSE;
    tmpSrcFile->cmpType = CompType;
    tmpSrcFile->next = NULL;

    return DDF_OK;
}

DDFERR parseDdfLine(char * line, PCCAB ccab, DDFSRCFILE ** srcList)
{
    unsigned int r;
    char token[DDF_MAX_CHARS];

    if (line[0] == '\0') return DDF_OK;

    ddfLog(DDFLOG_MSG, "Parsing line \"%s\"", line);

    r = nextToken(token,line);
    if (r == DDF_OK)
    {
        if (token[0] == '.')
        {
            r = parseCmd(line, ccab, token);
            if (r != DDF_OK) ddfLog(DDFLOG_ERR, "Invalid command line skipped: %s%s", token, line);
        }
        else
        {
            r = parseSrc(line, srcList, token);

            if (r == DDFERR_SRC_SKIPPED)
                ddfLog(DDFLOG_ERR, "Source file command \"%s%s\" couldn't be read - skipped", token, line);
            else if (r == DDFERR_FUNC_FAILED)
                ddfLog(DDFLOG_WRN, "Could not parse command correctly, \"%s\" remains", line);
        }
    }

    return r;
}

DDFERR ParseDdf(char* ddfFile, PCCAB ccab, DDFSRCFILE ** srcListH, unsigned int v)
{
    char fLine[DDF_MAX_CHARS];
    FILE *ddf = NULL;
    unsigned int i = 0;
    DDFSRCFILE * srcListCurr = NULL;

    ddf = fopen(ddfFile, "r");
    if (ddf == NULL) return DDFERR_UNREAD;

    DdfVerb = v;
    for (; i < DDF_MAX_LINES && fgets(fLine, sizeof fLine, ddf); i++)
    {
        fLine[strlen(fLine)-1] = '\0';
        parseDdfLine(fLine, ccab, &srcListCurr);
        if (srcListCurr && !(*srcListH))
        {
            *srcListH = srcListCurr;
        }
    }

    ddfPrintState(DDFLOG_MSG, ccab, *srcListH);
    ccab->cbFolderThresh = ccab->cb;
    ccab->iDisk = 0;

    fclose(ddf);

    return DDF_OK;
}

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