File: makesfx.c

package info (click to toggle)
unzip 6.0-16%2Bdeb8u3
  • links: PTS
  • area: main
  • in suites: jessie
  • size: 8,768 kB
  • ctags: 10,194
  • sloc: ansic: 55,133; cpp: 4,084; makefile: 2,517; asm: 1,789; cs: 1,012; sh: 119
file content (255 lines) | stat: -rw-r--r-- 6,935 bytes parent folder | download | duplicates (15)
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
/*
  Copyright (c) 1990-2000 Info-ZIP.  All rights reserved.

  See the accompanying file LICENSE, version 2000-Apr-09 or later
  (the contents of which are also included in unzip.h) for terms of use.
  If, for some reason, all these files are missing, the Info-ZIP license
  also may be found at:  ftp://ftp.info-zip.org/pub/infozip/license.html
*/
/*
 * makesfx - Makes a QDOS sfx zip file
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * created by Jonathan Hudson, 04/09/95
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>

#define SFXFLAG "??Special Flag for unzipsfx hack  ??"

#ifdef QDOS
# include <qdos.h>
# define ZMODE (X_OK|R_OK)
# define rev_long(x) (x)
# define XFLAG 0x4afb
#else
# define ZMODE (R_OK)
# define getchid(p1) p1
# include <sys/stat.h>
  long rev_long(long l);
# define XFLAG 0xfb4a

typedef struct
{
    long id;
    long dlen;
} NTC;

struct qdirect  {
    long            d_length __attribute__ ((packed));  /* file length */
    unsigned char   d_access __attribute__ ((packed));  /* file access type */
    unsigned char   d_type __attribute__ ((packed));    /* file type */
    long            d_datalen __attribute__ ((packed)); /* data length */
    long            d_reserved __attribute__ ((packed));/* Unused */
    short           d_szname __attribute__ ((packed));  /* size of name */
    char            d_name[36] __attribute__ ((packed));/* name area */
    long            d_update __attribute__ ((packed));  /* last update */
    long            d_refdate __attribute__ ((packed));
    long            d_backup __attribute__ ((packed));   /* EOD */
} ;

int fs_headr (int fd, long t, struct qdirect *qs, short size)
{
    NTC ntc;
    int r = -1;
    struct stat s;

    fstat(fd, &s);
    qs->d_length = s.st_size;
    lseek(fd, -8, SEEK_END);
    read(fd, &ntc, 8);
    if(ntc.id == *(long *)"XTcc")
    {
        qs->d_datalen = ntc.dlen;    /* This is big endian */
        qs->d_type = 1;
        r = 0;
    }
    lseek(fd, 0, 0);
    return 42;                       /* why not ??? */
}

typedef unsigned char uch;

long rev_long (long l)
{
    uch cc[4];
    cc[0] = (uch)(l >> 24);
    cc[1] = (uch)((l >> 16) & 0xff);
    cc[2] = (uch)((l >> 8) & 0xff);
    cc[3] = (uch)(l & 0xff);
    return *(long *)cc;
}

#endif

#define RBUFSIZ 4096

void usage(void)
{
    fputs("makesfx -o outfile -z zipfile -xunzipsfx -sstubfile\n", stderr);
    exit(0);
}

int main (int ac, char **av)
{
    int fd, fo;
    static char local_sig[4] = "PK\003\004";
    char *p, tmp[4];
    short ok = 0;
    char *of = NULL;
    char *xf = NULL;
    char *zf = NULL;
    char *sf = NULL;
    int c;

    while((c = getopt(ac, av, "o:z:x:s:h")) != EOF)
    {
        switch(c)
        {
            case 'o':
                of = optarg;
                break;
            case 'z':
                zf = optarg;
                break;
            case 'x':
                xf = optarg;
                break;
            case 's':
                sf = optarg;
                break;
            case 'h':
                usage();
                break;
        }
    }


    if(zf && xf && of && sf)
    {
        if((fd = open(zf, O_RDONLY)) > 0)
        {
            if((read(fd, tmp, 4) == 4))
            {
                if(*(long *)tmp == *(long *)local_sig)
                {
                    ok = 1;
                }
            }
            close(fd);
        }
        if(!ok)
        {
            fprintf(stderr,
                    "Huum, %s doesn't look like a ZIP file to me\n", zf);
            exit(0);
        }

        if(strstr(xf, "unzipsfx"))
        {
            if(access(xf, ZMODE))
            {
                fprintf(stderr, "Sorry, don't like the look of %s\n", xf);
                exit(0);
            }
        }

        if((fo = open(of, O_CREAT|O_TRUNC|O_RDWR, 0666)) != -1)
        {
            struct qdirect sd,xd;
            int n;
            int dsoff = 0;
            int nfoff = 0;
            int zlen = 0;

            if((fd = open(sf, O_RDONLY)) != -1)
            {
                if(fs_headr(getchid(fd), -1, &sd, sizeof(sd)) > 0)
                {
                    unsigned short *q;
                    p = malloc(sd.d_length);
                    n = read(fd, p, sd.d_length);
                    for(q = (unsigned short *)p;
                        q != (unsigned short *)(p+sd.d_length); q++)
                    {
                        if(*q == XFLAG && *(q+1) == XFLAG)
                        {
                            dsoff = (int)q-(int)p;
                            break;
                        }
                    }
                    write(fo, p, n);
                    close(fd);
                }
            }

            if(dsoff == 0)
            {
                puts("Fails");

                exit(0);
            }

            if((fd = open(xf, O_RDONLY)) != -1)
            {
                char *q;
                if(fs_headr(getchid(fd), -1, &xd, sizeof(xd)) > 0)
                {
                    p = realloc(p, xd.d_length);
                    n = read(fd, p, xd.d_length);
                    {
                        for(q = p; q < p+xd.d_length ; q++)
                        {
                            if(*q == '?')
                            {
                                if(memcmp(q, SFXFLAG, sizeof(SFXFLAG)-1) == 0)
                                {
                                    nfoff = (int)(q-p);
                                    break;
                                }
                            }
                        }
                    }
                    write(fo, p, n);
                    close(fd);

                    if((fd = open(zf, O_RDONLY)) > 0)
                    {
                        p = realloc(p, RBUFSIZ);
                        while((n = read(fd, p, RBUFSIZ)) > 0)
                        {
                            write(fo, p, n);
                            zlen += n;
                        }
                        close(fd);
                    }
                    lseek(fo, dsoff+4, SEEK_SET);
                    n = rev_long((sd.d_length-dsoff));
                    write(fo, &n, sizeof(long));
                    n = rev_long(xd.d_length);
                    write(fo, &n, sizeof(long));
                    write(fo, &xd.d_datalen, sizeof(long));
                    n = rev_long(nfoff);
                    write(fo, &n, sizeof(long));
                    n = rev_long(zlen);
                    write(fo, &n, sizeof(long));
                    close(fo);
                }
                else
                {
                    close(fd);
                    fputs("Can't read unzipsfx header", stderr);
                    exit(0);
                }
            }
            free(p);
        }
    }
    else
        usage();

    return 0;
}