⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 zopen.c

📁 早期freebsd实现
💻 C
📖 第 1 页 / 共 2 页
字号:
output(zs, ocode)	struct s_zstate *zs;	code_int ocode;{	register int bits, r_off;	register char_type *bp;	r_off = offset;	bits = n_bits;	bp = buf;	if (ocode >= 0) {		/* Get to the first byte. */		bp += (r_off >> 3);		r_off &= 7;		/*		 * Since ocode is always >= 8 bits, only need to mask the first		 * hunk on the left.		 */		*bp = (*bp & rmask[r_off]) | (ocode << r_off) & lmask[r_off];		bp++;		bits -= (8 - r_off);		ocode >>= 8 - r_off;		/* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */		if (bits >= 8) {			*bp++ = ocode;			ocode >>= 8;			bits -= 8;		}		/* Last bits. */		if (bits)			*bp = ocode;		offset += n_bits;		if (offset == (n_bits << 3)) {			bp = buf;			bits = n_bits;			bytes_out += bits;			if (fwrite(bp, sizeof(char), bits, fp) != bits)				return (-1);			bp += bits;			bits = 0;			offset = 0;		}		/*		 * If the next entry is going to be too big for the ocode size,		 * then increase it, if possible.		 */		if (free_ent > maxcode || (clear_flg > 0)) {		       /*			* Write the whole buffer, because the input side won't			* discover the size increase until after it has read it.			*/			if (offset > 0) {				if (fwrite(buf, 1, n_bits, fp) != n_bits)					return (-1);				bytes_out += n_bits;			}			offset = 0;			if (clear_flg) {				maxcode = MAXCODE(n_bits = INIT_BITS);				clear_flg = 0;			} else {				n_bits++;				if (n_bits == maxbits)					maxcode = maxmaxcode;				else					maxcode = MAXCODE(n_bits);			}		}	} else {		/* At EOF, write the rest of the buffer. */		if (offset > 0) {			offset = (offset + 7) / 8;			if (fwrite(buf, 1, offset, fp) != offset)				return (-1);			bytes_out += offset;		}		offset = 0;	}	return (0);}/* * Decompress read.  This routine adapts to the codes in the file building * the "string" table on-the-fly; requiring no table to be stored in the * compressed file.  The tables used herein are shared with those of the * compress() routine.  See the definitions above. */static intzread(cookie, rbp, num)	void *cookie;	char *rbp;	int num;{	register u_int count;	struct s_zstate *zs;	u_char *bp, header[3];	if (num == 0)		return (0);	zs = cookie;	count = num;	bp = (u_char *)rbp;	switch (state) {	case S_START:		state = S_MIDDLE;		break;	case S_MIDDLE:		goto middle;	case S_EOF:		goto eof;	}	/* Check the magic number */	if (fread(header,	    sizeof(char), sizeof(header), fp) != sizeof(header) ||	    memcmp(header, magic_header, sizeof(magic_header)) != 0) {		errno = EFTYPE;		return (-1);	}	maxbits = header[2];	/* Set -b from file. */	block_compress = maxbits & BLOCK_MASK;	maxbits &= BIT_MASK;	maxmaxcode = 1L << maxbits;	if (maxbits > BITS) {		errno = EFTYPE;		return (-1);	}	/* As above, initialize the first 256 entries in the table. */	maxcode = MAXCODE(n_bits = INIT_BITS);	for (code = 255; code >= 0; code--) {		tab_prefixof(code) = 0;		tab_suffixof(code) = (char_type) code;	}	free_ent = block_compress ? FIRST : 256;	finchar = oldcode = getcode(zs);	if (oldcode == -1)	/* EOF already? */		return (0);	/* Get out of here */	/* First code must be 8 bits = char. */	*bp++ = (u_char)finchar;	count--;	stackp = de_stack;	while ((code = getcode(zs)) > -1) {		if ((code == CLEAR) && block_compress) {			for (code = 255; code >= 0; code--)				tab_prefixof(code) = 0;			clear_flg = 1;			free_ent = FIRST - 1;			if ((code = getcode(zs)) == -1)	/* O, untimely death! */				break;		}		incode = code;		/* Special case for KwKwK string. */		if (code >= free_ent) {			*stackp++ = finchar;			code = oldcode;		}		/* Generate output characters in reverse order. */		while (code >= 256) {			*stackp++ = tab_suffixof(code);			code = tab_prefixof(code);		}		*stackp++ = finchar = tab_suffixof(code);		/* And put them out in forward order.  */middle:		do {			if (count-- == 0)				return (num);			*bp++ = *--stackp;		} while (stackp > de_stack);		/* Generate the new entry. */		if ((code = free_ent) < maxmaxcode) {			tab_prefixof(code) = (u_short) oldcode;			tab_suffixof(code) = finchar;			free_ent = code + 1;		}		/* Remember previous code. */		oldcode = incode;	}	state = S_EOF;eof:	return (num - count);}/*- * Read one code from the standard input.  If EOF, return -1. * Inputs: * 	stdin * Outputs: * 	code or -1 is returned. */static code_intgetcode(zs)	struct s_zstate *zs;{	register code_int gcode;	register int r_off, bits;	register char_type *bp;	bp = gbuf;	if (clear_flg > 0 || roffset >= size || free_ent > maxcode) {		/*		 * If the next entry will be too big for the current gcode		 * size, then we must increase the size.  This implies reading		 * a new buffer full, too.		 */		if (free_ent > maxcode) {			n_bits++;			if (n_bits == maxbits)	/* Won't get any bigger now. */				maxcode = maxmaxcode;			else				maxcode = MAXCODE(n_bits);		}		if (clear_flg > 0) {			maxcode = MAXCODE(n_bits = INIT_BITS);			clear_flg = 0;		}		size = fread(gbuf, 1, n_bits, fp);		if (size <= 0)			/* End of file. */			return (-1);		roffset = 0;		/* Round size down to integral number of codes. */		size = (size << 3) - (n_bits - 1);	}	r_off = roffset;	bits = n_bits;	/* Get to the first byte. */	bp += (r_off >> 3);	r_off &= 7;	/* Get first part (low order bits). */	gcode = (*bp++ >> r_off);	bits -= (8 - r_off);	r_off = 8 - r_off;	/* Now, roffset into gcode word. */	/* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */	if (bits >= 8) {		gcode |= *bp++ << r_off;		r_off += 8;		bits -= 8;	}	/* High order bits. */	gcode |= (*bp & rmask[bits]) << r_off;	roffset += n_bits;	return (gcode);}static intcl_block(zs)			/* Table clear for block compress. */	struct s_zstate *zs;{	register long rat;	checkpoint = in_count + CHECK_GAP;	if (in_count > 0x007fffff) {	/* Shift will overflow. */		rat = bytes_out >> 8;		if (rat == 0)		/* Don't divide by zero. */			rat = 0x7fffffff;		else			rat = in_count / rat;	} else		rat = (in_count << 8) / bytes_out;	/* 8 fractional bits. */	if (rat > ratio)		ratio = rat;	else {		ratio = 0;		cl_hash(zs, (count_int) hsize);		free_ent = FIRST;		clear_flg = 1;		if (output(zs, (code_int) CLEAR) == -1)			return (-1);	}	return (0);}static voidcl_hash(zs, cl_hsize)			/* Reset code table. */	struct s_zstate *zs;	register count_int cl_hsize;{	register count_int *htab_p;	register long i, m1;	m1 = -1;	htab_p = htab + cl_hsize;	i = cl_hsize - 16;	do {			/* Might use Sys V memset(3) here. */		*(htab_p - 16) = m1;		*(htab_p - 15) = m1;		*(htab_p - 14) = m1;		*(htab_p - 13) = m1;		*(htab_p - 12) = m1;		*(htab_p - 11) = m1;		*(htab_p - 10) = m1;		*(htab_p - 9) = m1;		*(htab_p - 8) = m1;		*(htab_p - 7) = m1;		*(htab_p - 6) = m1;		*(htab_p - 5) = m1;		*(htab_p - 4) = m1;		*(htab_p - 3) = m1;		*(htab_p - 2) = m1;		*(htab_p - 1) = m1;		htab_p -= 16;	} while ((i -= 16) >= 0);	for (i += 16; i > 0; i--)		*--htab_p = m1;}FILE *zopen(fname, mode, bits)	const char *fname, *mode;	int bits;{	struct s_zstate *zs;	if (mode[0] != 'r' && mode[0] != 'w' || mode[1] != '\0' ||	    bits < 0 || bits > BITS) {		errno = EINVAL;		return (NULL);	}	if ((zs = calloc(1, sizeof(struct s_zstate))) == NULL)		return (NULL);	maxbits = bits ? bits : BITS;	/* User settable max # bits/code. */	maxmaxcode = 1 << BITS;		/* Should NEVER generate this code. */	hsize = HSIZE;			/* For dynamic table sizing. */	free_ent = 0;			/* First unused entry. */	block_compress = BLOCK_MASK;	clear_flg = 0;	ratio = 0;	checkpoint = CHECK_GAP;	in_count = 1;			/* Length of input. */	out_count = 0;			/* # of codes output (for debugging). */	state = S_START;	roffset = 0;	size = 0;	/*	 * Layering compress on top of stdio in order to provide buffering,	 * and ensure that reads and write work with the data specified.	 */	if ((fp = fopen(fname, mode)) == NULL) {		free(zs);		return (NULL);	}	switch (*mode) {	case 'r':		zmode = 'r';		return (funopen(zs, zread, NULL, NULL, zclose));	case 'w':		zmode = 'w';		return (funopen(zs, NULL, zwrite, NULL, zclose));	}	/* NOTREACHED */}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -