File: arcpack.c

package info (click to toggle)
arc 5.21q-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 412 kB
  • ctags: 311
  • sloc: ansic: 4,124; makefile: 86
file content (299 lines) | stat: -rw-r--r-- 6,652 bytes parent folder | download | duplicates (3)
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
/*  ARC - Archive utility - ARCPACK

    Version 3.49, created on 04/21/87 at 11:26:51

    (C) COPYRIGHT 1985-87 by System Enhancement Associates.
    You may copy and distribute this program freely,
    under the terms of the General Public License.

    By:	 Thom Henderson

    Description:
	 This file contains the routines used to compress a file
	 when placing it in an archive.

    Language:
	 Computer Innovations Optimizing C86
*/
#include <stdio.h>
#include "arc.h"
#if	_MTS
#include <ctype.h>
#endif

#include "proto.h"

VOID		setcode(), init_cm(), codebuf();
VOID		arcdie(), init_sq(), flsh_cm();
int		crcbuf();
u_int		ncr_buf();

int		lastc;

/* stuff for non-repeat packing */

#define DLE 0x90		/* repeat sequence marker */

u_char	state;		/* current packing state */

/* non-repeat packing states */

#define NOHIST	0		/* don't consider previous input */
#define SENTCHAR 1		/* lastchar set, no lookahead yet */

extern	u_char	*pinbuf;
extern	u_char	*pakbuf;	/* worst case, 2*inbuf */

/* packing results */

long		stdlen;		/* length for standard packing */
short		crcval;		/* CRC check value */

VOID
pack(f, t, hdr)			/* pack file into an archive */
	FILE	       *f, *t;	/* source, destination */
	struct heads   *hdr;	/* pointer to header data */
{
	long		ncrlen; /* length after packing */
	long		huflen; /* length after squeezing */
	long		lzwlen; /* length after crunching */
	long		pred_sq(), head_sq(), huf_buf();	/* stuff for squeezing */
	long		pred_cm();	/* dynamic crunching cleanup */
	long		tloc, ftell();	/* start of output */
	u_int		inbytes = 0, pakbytes = 0;

	/* first pass - see which method is best */

	tloc = ftell(t);	/* note start of output */

	if (!nocomp) {		/* if storage kludge not active */
		if (note) {
			printf(" analyzing, ");
			fflush(stdout);
		}
		state = NOHIST; /* initialize ncr packing */
		stdlen = ncrlen = 0;	/* reset size counters */
		huflen = lzwlen = 0;
		crcval = 0;	/* initialize CRC check value */
		setcode();	/* initialize encryption */
		init_sq();	/* initialize for squeeze scan */

		inbytes = getbuf(f);
		if (inbytes) {

			init_cm(pinbuf);

			for (;; inbytes = getbuf(f)) {
				pakbytes = ncr_buf(inbytes);
				ncrlen += pakbytes;
				hufb_tab(pakbuf, pakbytes);
				if (dosquash)
					lzw_buf(pinbuf, inbytes, t);
				else
					lzw_buf(pakbuf, pakbytes, t);
				if (inbytes < MYBUF)
					break;
			}
			lzwlen = pred_cm(t);
			huflen = pred_sq();
		}
	} else {		/* else kludge the method */
		stdlen = 0;	/* make standard look best */
		ncrlen = huflen = lzwlen = 1;
	}

	/* standard set-ups common to all methods */

	hdr->crc = crcval;	/* note CRC check value */
	hdr->length = stdlen;	/* set actual file length */
	if (stdlen > MYBUF) {
		fseek(f, 0L, 0);/* rewind input */
		state = NOHIST; /* reinitialize ncr packing */
		setcode();	/* reinitialize encryption */
	} else
		inbytes = stdlen;

	/* choose and use the shortest method */

	if (kludge && note)
		printf("\n\tS:%ld  P:%ld  S:%ld	 C:%ld,\t ",
		       stdlen, ncrlen, huflen, lzwlen);

	if (stdlen <= ncrlen && stdlen <= huflen && stdlen <= lzwlen) {
		if (note) {
			printf("storing, ");	/* store without compression */
			fflush(stdout);
		}
		hdrver = 2;	/* note packing method */
		fseek(t, tloc, 0);	/* reset output for new method */
		if (nocomp || (stdlen > MYBUF)) {
			stdlen = crcval = 0;
			while ((inbytes = getbuf(f)) != 0)
				putb_pak(pinbuf, inbytes, t); /* store it straight */
		} else
			putb_pak(pinbuf, inbytes, t);
		hdr->crc = crcval;
		hdr->length = hdr->size = stdlen;
	} else if (ncrlen < lzwlen && ncrlen < huflen) {
		if (note) {
			printf("packing, ");	/* pack with repeat */
			fflush(stdout); /* suppression */
		}
		hdrver = 3;	/* note packing method */
		hdr->size = ncrlen;	/* set data length */
		fseek(t, tloc, 0);	/* reset output for new method */
		if (stdlen > MYBUF) {
			do {
				inbytes = getbuf(f);
				pakbytes = ncr_buf(inbytes);
				putb_pak(pakbuf, pakbytes, t);
			} while (inbytes != 0);
		} else
			putb_pak(pakbuf, pakbytes, t);
	} else if (huflen < lzwlen) {
		if (note) {
			printf("squeezing, ");
			fflush(stdout);
		}
		hdrver = 4;	/* note packing method */
		fseek(t, tloc, 0);	/* reset output for new method */
		huflen = head_sq();
		if (stdlen > MYBUF) {
			do {
				inbytes = getbuf(f);
				pakbytes = ncr_buf(inbytes);
				huflen += huf_buf(pakbuf, pakbytes, inbytes, t);
			} while (inbytes != 0);
		} else
			huflen += huf_buf(pakbuf, pakbytes, 0, t);

		hdr->size = huflen;	/* note final size */
	} else {
		if (note)
			printf(dosquash ? "squashed, " : "crunched, ");
		flsh_cm(t);
		hdrver = dosquash ? 9 : 8;
		hdr->size = lzwlen;	/* size should not change */
	}

	/* standard cleanups common to all methods */

	if (note)
		printf("done. (%ld%%)\n", hdr->length == 0 ?
		       0L : 100L - (100L * hdr->size) / hdr->length);
}

/*
 * Non-repeat compression - text is passed through normally, except that a
 * run of more than two is encoded as:
 *
 * <char> <DLE> <count>
 *
 * Special case: a count of zero indicates that the DLE is really a DLE, not a
 * repeat marker.
 */

u_int
ncr_buf(inbytes)
	u_int		inbytes;	/* number of bytes in inbuf */
{
	u_int		i;
	int		c;
	reg u_char     *inptr, *pakptr;
	static int	cnt;

	inptr = pinbuf;
	pakptr = pakbuf;

	if (state == NOHIST) {
		lastc = (-1);
		cnt = 1;
		state = SENTCHAR;
	}
	for (i = 0; i < inbytes; i++) {
		c = *inptr++;
		if (c == lastc && cnt < 255)
			cnt++;
		else {
			if (cnt == 2) {
				*pakptr++ = lastc;
			} else if (cnt > 2) {
				*pakptr++ = DLE;
				*pakptr++ = cnt;
			}
			*pakptr++ = c;
			lastc = c;
			cnt = 1;
		}
		if (c == DLE) {
			*pakptr++ = '\0';
			lastc = (-1);
		}
	}
	if (inbytes < MYBUF && cnt > 1) {	/* trailing stuff */
		if (cnt == 2)
			*pakptr++ = lastc;
		else {
			*pakptr++ = DLE;
			*pakptr++ = cnt;
		}
	}
	return (pakptr - pakbuf);
}

u_int
getbuf(f)
	FILE	       *f;
{
	u_int		i;
#if	!DOS
	int		c;
	static int	cr = 0;
	register u_char *ptr;
	if (image) {
#endif
		i = fread(pinbuf, 1, MYBUF, f);
#if	!DOS
	} else {
		ptr = pinbuf;
		for (i = 0, c = 0; (c != EOF) && (i < MYBUF); i++) {
			if (cr) {
				c = '\n';
				cr = 0;
			} else {
				c = fgetc(f);
				if (c == EOF)
					break;
				else if (c == '\n') {
					c = '\r';
					cr = 1;
				}
			}
			*ptr++ = c;
		}
#if	_MTS
		etoa(pinbuf, i);
#endif				/* _MTS */
	}
#endif				/* !DOS */
	crcval = crcbuf(crcval, i, pinbuf);
	stdlen += i;
	return (i);
}

VOID
putb_pak(buf, len, f)
	u_char	       *buf;
	u_int		len;
	FILE	       *f;
{
	u_int		i;

	if (f && len) {
		if (password)
			codebuf(buf, len);
		i = fwrite(buf, 1, len, f);
		if (i != len)
			arcdie("Write failed");
	}
}