File: zma.c

package info (click to toggle)
macutils 2.0b3-17.1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 1,264 kB
  • sloc: ansic: 12,737; makefile: 661
file content (390 lines) | stat: -rwxr-xr-x 9,541 bytes parent folder | download | duplicates (2)
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
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "macunpack.h"
#ifdef ZMA
#include "globals.h"
#include "zma.h"
#include "crc.h"
#include "../fileio/machdr.h"
#include "../fileio/wrfile.h"
#include "../fileio/kind.h"
#include "../util/masks.h"
#include "../util/util.h"

extern void de_lzh();

/* We do allow for possible backpointing, so we allocate the archive in core */
static char *zma_archive;
static char *zma_current;
static char *zma_filestart;
static unsigned long zma_length;
static long zma_archlength;

static int zma_filehdr();
static void zma_folder();
static void zma_mooz();
static void zma_wrfile();
static void zma_nocomp();
static void zma_lzh();

void zma(start, length)
    char *start;
    unsigned long length;
{
    struct fileHdr filehdr;
    int i, toread;

    if(length != 0) {
	if(zma_archlength < length) {
	    if(zma_archlength == 0) {
		zma_archive = malloc((unsigned)length);
	    } else {
		zma_archive = realloc(zma_archive, (unsigned)length);
	    }
	    zma_archlength = length;
	    if(zma_archive == NULL) {
		(void)fprintf(stderr, "Insufficient memory, aborting\n");
		exit(1);
	    }
	}
	if(fread(zma_archive, 1, (int)length, infp) != length) {
	    (void)fprintf(stderr, "Can't read archive.\n");
#ifdef SCAN
	    do_error("macunpack: Can't read archive");
#endif /* SCAN */
	    exit(1);
	}
	zma_length = get4(zma_archive + ZMAHDRS + 1);
	if(zma_length != length) {
	    (void)fprintf(stderr, "Archive length mismatch.\n");
#ifdef SCAN
	    do_error("macunpack: Archive length mismatch");
#endif /* SCAN */
	    exit(1);
	}
    } else {
	zma_length =  get4(start + ZMAHDRS + 1);
	if(zma_archlength < zma_length) {
	    if(zma_archlength == 0) {
		zma_archive = malloc((unsigned)zma_length);
	    } else {
		zma_archive = realloc(zma_archive, (unsigned)zma_length);
	    }
	    zma_archlength = zma_length;
	    if(zma_archive == NULL) {
		(void)fprintf(stderr, "Insufficient memory, aborting\n");
		exit(1);
	    }
	}
	if(zma_archive == NULL) {
	    (void)fprintf(stderr, "Insufficient memory, aborting\n");
	    exit(1);
	}
	for(i = 0; i <= ZMAHDRS2; i++) {
	    zma_archive[i] = start[i];
	}
	toread = zma_length - ZMAHDRS2 - 1;
	if(fread(zma_archive + ZMAHDRS2 + 1, 1, toread, infp) != toread) {
	    (void)fprintf(stderr, "Can't read archive.\n");
#ifdef SCAN
	    do_error("macunpack: Can't read archive");
#endif /* SCAN */
	    exit(1);
	}
    }
    /* Consistency checks */
    if(zma_archive[0] != 0) {
	(void)fprintf(stderr, "Not a \"Zoom\" archive after all, aborting\n");
	exit(1);
    }
    if(strncmp(zma_archive + 1, ZMAHDR, ZMAHDRS)) {
	(void)fprintf(stderr, "Not a \"Zoom\" archive after all, aborting\n");
	exit(1);
    }
    zma_current = zma_archive + 8;
    updcrc = arc_updcrc;
    crcinit = arc_crcinit;
    while(zma_current != zma_archive) {
	if(zma_filehdr(&filehdr, 0) == -1) {
	    (void)fprintf(stderr, "Can't find file header./n");
#ifdef SCAN
	    do_error("macunpack: Can't find file header");
#endif /* SCAN */
	    exit(1);
	}
	zma_filestart = zma_current + filehdr.hlen;
	if(filehdr.what == z_dir) {
	    zma_folder(filehdr);
	} else {
	    zma_mooz(filehdr);
	}
	zma_current = zma_archive + filehdr.next;
    }
}

static int zma_filehdr(f, skip)
struct fileHdr *f;
int skip;
{
    register int i;
    int n;
    char ftype[5], fauth[5];

    if(zma_current - zma_archive + Z_HDRSIZE > zma_length) {
	return -1;
    }
    for(i = 0; i < INFOBYTES; i++) {
	info[i] = '\0';
    }

    n = zma_current[Z_FNAME] & BYTEMASK;
    if(n > F_NAMELEN) {
	n = F_NAMELEN;
    }
    info[I_NAMEOFF] = n;
    copy(info + I_NAMEOFF + 1, zma_current + Z_FNAME + 1, n);
    transname(zma_current + Z_FNAME + 1, text, n);

    f->what = zma_current[Z_WHAT];
    f->rsrcLength = get4(zma_current + Z_URLEN);
    f->dataLength = get4(zma_current + Z_UDLEN);
    f->compRLength = get4(zma_current + Z_CRLEN);
    f->compDLength = get4(zma_current + Z_CDLEN);
    f->rsrcCRC = get2(zma_current + Z_RCRC);
    f->dataCRC = get2(zma_current + Z_DCRC);
    f->hlen = zma_current[Z_HLEN];
    f->next = get4(zma_current + Z_NEXT);
    if(f->what == z_dir) { /* A hack */
	f->conts = get4(zma_current + Z_AUTH);
    }
    /* Set rsrc fork sizes correctly */
    f->rsrcLength -= f->dataLength;
    f->compRLength -= f->compDLength;

    write_it = !skip;
    if(f->what & 0x80) {
	write_it = 0;
	f->what = -f->what;
	f->deleted = 1;
	return 0;
    }
    f->deleted = 0;
    if(list && !skip) {
	do_indent(indent);
	if(f->what == z_dir) {
	    (void)fprintf(stderr, "folder=\"%s\"", text);
	} else {
	    transname(zma_current + Z_TYPE, ftype, 4);
	    transname(zma_current + Z_AUTH, fauth, 4);
	    (void)fprintf(stderr,
		    "name=\"%s\", type=%4.4s, author=%4.4s, data=%ld, rsrc=%ld",
		    text, ftype, fauth,
		    (long)f->dataLength, (long)f->rsrcLength);
	}
	switch(f->what) {
	case z_plug:
	    (void)fputc('\n', stderr);
	    (void)fprintf(stderr,
		    "\tFile uses custom processing, cannot handle.\n");
	    write_it = 0;
	    return 0;
	case z_dir:
	case z_file:
	case z_plain:
	    break;
	default:
	    (void)fputc('\n', stderr);
	    (void)fprintf(stderr,
		    "\tEh, do not understand this (%d); skipped.\n", f->what);
	    write_it = 0;
	    return 0;
	}

	if(info_only) {
	    write_it = 0;
	}
	if(query) {
	    write_it = do_query();
	} else {
	    (void)fputc('\n', stderr);
	}
    }


    if(write_it) {
	define_name(text);

	if(f->what != z_dir) {
	    copy(info + I_TYPEOFF, zma_current + Z_TYPE, 4);
	    copy(info + I_AUTHOFF, zma_current + Z_AUTH, 4);
	    copy(info + I_FLAGOFF, zma_current + Z_FLAGS, 2);
	    copy(info + I_DLENOFF, zma_current + Z_UDLEN, 4);
	    put4(zma_current + Z_URLEN, f->rsrcLength);
	    copy(info + I_RLENOFF, zma_current + Z_URLEN, 4);
	    copy(info + I_CTIMOFF, zma_current + Z_MDATE, 4);
	    copy(info + I_MTIMOFF, zma_current + Z_MDATE, 4);
	}
    }
    return 1;
}

static void zma_folder(fhdr)
struct fileHdr fhdr;
{
    int i;
    char loc_name[64];
    struct fileHdr filehdr;

    for(i = 0; i < 64; i++) {
	loc_name[i] = text[i];
    }
    zma_current = zma_archive + fhdr.conts;
    if(write_it || info_only) {
	if(write_it) {
	    do_mkdir(text, info);
	}
	indent++;
	while(zma_current != zma_archive) {
	    if(zma_filehdr(&filehdr, 0) == -1) {
		(void)fprintf(stderr, "Can't find file header.\n");
#ifdef SCAN
		do_error("macunpack: Can't find file header");
#endif /* SCAN */
		exit(1);
	    }
	    zma_filestart = zma_current + filehdr.hlen;
	    if(filehdr.what == z_dir) {
		zma_folder(filehdr);
	    } else {
		zma_mooz(filehdr);
	    }
	    zma_current = zma_archive + filehdr.next;
	}
	if(write_it) {
	    enddir();
	}
	indent--;
	if(list) {
	    do_indent(indent);
	    (void)fprintf(stderr, "leaving folder \"%s\"\n", loc_name);
	}
    }
}

static void zma_mooz(filehdr)
struct fileHdr filehdr;
{
    unsigned long crc;

    if(write_it) {
	start_info(info, filehdr.rsrcLength, filehdr.dataLength);
    }
    if(verbose) {
	(void)fprintf(stderr, "\tData: ");
    }
    if(write_it) {
	start_data();
    }
    zma_wrfile(filehdr.compDLength, filehdr.dataLength, filehdr.what);
    if(write_it) {
	crc = (*updcrc)(INIT_CRC, out_buffer, filehdr.dataLength);
	if(filehdr.dataCRC != crc) {
	    (void)fprintf(stderr,
		"CRC error on data fork: need 0x%04x, got 0x%04x\n",
		(int)filehdr.dataCRC, (int)crc);
#ifdef SCAN
	    do_error("macunpack: CRC error on data fork");
#endif /* SCAN */
	    exit(1);
	}
    }
    if(verbose) {
	(void)fprintf(stderr, ", Rsrc: ");
    }
    if(write_it) {
	start_rsrc();
    }
    zma_wrfile(filehdr.compRLength, filehdr.rsrcLength, filehdr.what);
    if(write_it) {
	crc = (*updcrc)(INIT_CRC, out_buffer, filehdr.rsrcLength);
	if(filehdr.rsrcCRC != crc) {
	    (void)fprintf(stderr,
		"CRC error on resource fork: need 0x%04x, got 0x%04x\n",
		(int)filehdr.rsrcCRC, (int)crc);
#ifdef SCAN
	    do_error("macunpack: CRC error on resource fork");
#endif /* SCAN */
	    exit(1);
	}
	end_file();
    }
    if(verbose) {
	(void)fprintf(stderr, ".\n");
    }
}

static void zma_wrfile(ibytes, obytes, type)
unsigned long ibytes, obytes;
char type;
{
    if(ibytes == 0) {
	if(verbose) {
	    (void)fprintf(stderr, "empty");
	}
	return;
    }
    switch(type) {
    case z_plain:		/* no compression */
	if(verbose) {
	    (void)fprintf(stderr, "No compression");
	}
	if(write_it) {
	    zma_nocomp(ibytes);
	}
	break;
    case z_file:		/* lzh compression */
	if(verbose) {
	    (void)fprintf(stderr,
		    "LZH compressed (%4.1f%%)", 100.0 * ibytes / obytes);
	}
	if(write_it) {
	    zma_lzh(ibytes);
	}
	break;
    default:
	(void)fprintf(stderr, "Unknown compression method %2x\n", type);
#ifdef SCAN
	do_idf("", UNKNOWN);
#endif /* SCAN */
	exit(1);
    }
}

/*---------------------------------------------------------------------------*/
/*	No compression							     */
/*---------------------------------------------------------------------------*/
static void zma_nocomp(ibytes)
unsigned long ibytes;
{
    int n = ibytes;
    char *ptr = out_buffer;

    while(n-- > 0) {
	*ptr++ = *zma_filestart++;
    }
}

/*---------------------------------------------------------------------------*/
/*	LZ compression plus Huffman encoding				     */
/*---------------------------------------------------------------------------*/
static void zma_lzh(ibytes)
unsigned long ibytes;
{
    /* Controlled by ibutes only */
    de_lzh((long)ibytes, (long)(-1), &zma_filestart, 13);
}
#else /* ZMA */
int zma; /* keep lint and some compilers happy */
#endif /* ZMA */