File: unpacker.c

package info (click to toggle)
nyquist 3.12%2Bds-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 58,036 kB
  • sloc: ansic: 74,355; lisp: 20,485; java: 9,390; cpp: 6,695; sh: 207; xml: 58; makefile: 40
file content (328 lines) | stat: -rw-r--r-- 8,087 bytes parent folder | download | duplicates (12)
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
/* unpacker -- a program to unpack a set of files */
/* See packer.c for a description of the input file format. */

#include "switches.h"
#ifdef MACINTOSH
#include <Files.h>
#include <Script.h>
#endif

#include "cext.h"
#include "convert.h"
#include "string.h"

/* since we aren't using the cleanup package, expose exit(): */
#undef exit

#include "stdio.h"
#ifdef THINK_C
#include "console.h"
#endif

#define string_max 500
#ifndef EOS
#define EOS 0
#endif

void escape();
void unpack_ascii();
void unpack_binary();
void put_run();

#ifdef UNIX
#define FILESEP '/'
#include <sys/types.h>
#include <dirent.h>

int dir_isvaliddir(char *path)
{
    DIR *dir = opendir(path);
    if (!dir) return false;
    closedir(dir);
    return true;
}


#include <sys/stat.h>
int _mkdir(char *p)
{
    int rslt = mkdir(p, S_IRUSR | S_IWUSR | S_IXUSR | 
                        S_IXGRP | S_IWGRP | S_IROTH | S_IXOTH);
    printf("_mkdir: %s, rslt %d\n", p, rslt);
    if (rslt == -1) {
        printf("mkdir error\n");
        perror("unpacker mkdir");
    }
    return rslt;
}

#endif

#ifdef WINDOWS
#define FILESEP '\\'

// deal with some incompatible definitions
#undef byte
#undef boolean
#include "windows.h"
#include <direct.h>
/*
 * Function: dir_isvaliddir
 *
 * Purpose:
 *
 * Is this a valid directory ? 
 */
BOOL dir_isvaliddir(LPSTR path)
{
    DWORD dwAttrib;
    dwAttrib = GetFileAttributes(path);
    if (dwAttrib == -1) {
        return(FALSE);         }
    if (dwAttrib & FILE_ATTRIBUTE_DIRECTORY) {
        return(TRUE);
    }
    return(FALSE);
} /* dir_isvaliddir */ 
#endif

/* early_eof -- print error message and exit */
void early_eof()
{
    fprintf(stderr, "Unexpected end of file while unpacking\n");
    exit(1);
}
#ifdef MACINTOSH

#define FILESEP ':'

int dir_isvaliddir(char *path)
{
    char filename[256];
    OSErr err;
    FSSpec spec;
    strcpy(filename, path);
    c2pstr(filename);
    err = FSMakeFSSpec(0, 0, (unsigned char *) filename, &spec);
    if (err == noErr) {
        /* if we can open this as a file, it's not a directory */
        SInt16 refnum;
        err = FSpOpenDF(&spec, fsCurPerm, &refnum);
        if (err == noErr) {
            FSClose(refnum);
            return false;
        }
        return true;
    }  
    return false;
}


int _mkdir(char *p)
{
    OSErr err;
    FSSpec spec;
    SInt32 dirid;
    spec.vRefNum = 0;
    spec.parID = 0;
    strcpy((char *) spec.name, p);
    c2pstr(spec.name);
    err = FSpDirCreate(&spec, smSystemScript, &dirid);
    if (err == noErr) {
        return 0;
    }

    if (err != noErr) {
        printf("mkdir error %d\n", err);
    }
    return -1;
}
#endif


void make_path(char *full_name)
// make directories as needed
{
    char directory[256];
    char *ptr = full_name;
    while (ptr = strchr(ptr, FILESEP)) {
        strcpy(directory, full_name);
        directory[ptr - full_name] = 0;
        if (!dir_isvaliddir(directory)) {
            if (_mkdir(directory) != 0) {
                printf("Could not create %s\n", directory);
                return;
            } else {
                printf("Created directory %s\n", directory);
            }
        }
        // now directory is valid, so move to next one
        ptr++;
    }
}


/* main -- unpack a set of files */
/**/
int main(argc, argv)
  int argc;
  char *argv[];
{
    FILE *inf;  /* input file: a packed set of files */
    FILE *outf; /* a file to unpack */
    char filename[string_max];  /* holds names of inptu files */
#ifdef MACINTOSH
    argc = ccommand(&argv);
#endif
    if (argc != 2) {
        fprintf(stderr, "Usage: unpack input-file\n");
        exit(1);
    }
    inf = fopen(argv[1], "r");
    if (!inf) {
        fprintf(stderr, "Couldn't open |%s|\n", argv[1]);
        exit(1);
    }
    while (fgets(filename, string_max, inf)) {
        char *filetype = "w";
        filename[strlen(filename) - 1] = EOS;   /* remove newline at end */
        puts(filename + 1); /* don't print the leading ! or # */
        convert(filename + 1);  /* convert to local filename conventions */
        if (filename[0] == '#') filetype = "wb";
        outf = fopen(filename + 1, filetype);
        if (!outf) {
            make_path(filename + 1);
            outf = fopen(filename + 1, filetype);
            if (!outf) {
                fprintf(stderr, "Couldn't open |%s|\n", filename + 1);
                exit(1);
            }
        }
        if (filename[0] == '!') {
            unpack_ascii(inf, outf, filename + 1);
        } else if (filename[0] == '#') {
            unpack_binary(inf, outf);
        }
        if (outf) fclose(outf);
    }
    fclose(inf);
    return 0;
}


/* put_run -- output a run of characters */
/**/
void put_run(f, c, n)
    FILE *f;
    int c;
    int n;
{
    while (n--) putc(c, f);
}


/* unpack_ascii -- from inf to outf */
/**/
void unpack_ascii(inf, outf, filename)
    FILE *inf;
    FILE *outf;
    char *filename;
{
    for (;;) {
        int c = getc(inf);
        if (c == EOF) return;
        else if (c > 127) {
            fprintf(stderr, "Non-ascii char 0x%x found while unpacking %s.\n", c, filename);
            return;
        } else if (c >= 'A' && c <= '~') {
            int n = (c - 'A');
            //DO NOT OUTPUT LEADING TABS -- USE SPACES INSTEAD
            // int tabs = (n / TAB_WIDTH);
            // n -= tabs * TAB_WIDTH;
            // put_run(outf, '\t', tabs);
            put_run(outf, ' ', n);
        } else if (c >= '0' && c <= '9') {
            put_run(outf, '\n', c - '0');
        } else if (c == '!' || c == '#') {
            ungetc(c, inf);
            return;
        } else {
            fprintf(stderr, "Unexpected char in col 1 (%c) while unpacking %s.\n",
                                                        c, filename);
            return;
        }
        
        /* now get rest of the line */
        while ((c = getc(inf)) != EOF) {
            if (c == '$') {
                c = getc(inf);
                if (c == EOF) {
                    early_eof();
                } else if (c == '$') {
                    putc('$', outf);
                } else if (c >= '@' && c <= '_') {
                    putc(c - '@', outf);
                } else if (c == '\n') {
                    ; /* do nothing */
                } else {
                    fprintf(stderr, "Bad char (%c) after '$' while unpacking %s.\n",
                                                c, filename);
                }
            } else {
                putc(c, outf);
                if (c == '\n') break;   /* go up and process col. 1 char */
            }
        }
    }
}


/* unpack_binary -- from inf to outf */
/**/
void unpack_binary(inf, outf)
    FILE *inf;
    FILE *outf;
{
    for (;;) {
        long l;
        int c = getc(inf);
        if (c == EOF) {
            early_eof();
        } else if (c == '.') {
            break;
        } else if (c == '\n') {
            ; /* do nothing */
        } else {
            l = c - '0';
            c = getc(inf);
            if (c == EOF) {
                    early_eof();
            } else {
                l = (l << 6) + (c - '0');
                c = getc(inf);
                if (c == EOF) {
                    early_eof();
                } else if (c == '.') {
                    putc(l >> 4, outf);
                    break;
                } else {
                    l = (l << 6) + (c - '0');
                    c = getc(inf);
                    if (c == EOF) {
                            early_eof();
                    } else if (c == '.') {
                        putc((l >> 10) & 0xFF, outf);
                        putc((l >> 2) & 0xFF, outf);
                        break;
                    } else {
                        l = (l << 6) + (c - '0');
                        putc((l >> 16) & 0xFF, outf);
                        putc((l >> 8) & 0xFF, outf);            
                        putc(l & 0xFF, outf);
                    }
                }
            }
        }
    }
    getc(inf);  /* read the final newline */
}