?? util.c
字號(hào):
/* util.c -- utility functions for gzip support * Copyright (C) 1992-1993 Jean-loup Gailly * This is free software; you can redistribute it and/or modify it under the * terms of the GNU General Public License, see the file COPYING. */#ifdef RCSIDstatic char rcsid[] = "$Id: util.c,v 0.15 1993/06/15 09:04:13 jloup Exp $";#endif#include <ctype.h>#include <errno.h>#include <sys/types.h>#include "tailor.h"#ifdef HAVE_UNISTD_H# include <unistd.h>#endif#ifndef NO_FCNTL_H# include <fcntl.h>#endif#if defined(STDC_HEADERS) || !defined(NO_STDLIB_H)# include <stdlib.h>#else extern int errno;#endif#include "gzip.h"#include "crypt.h"#include "bsdebug.h"//#define _DEBUG_UTIL_C_#ifdef DBGPrintfi#undef DBGPrintfi#endif#ifdef DBGPrintfo#undef DBGPrintfo#endif#if defined(_TRACEHELPER_DEBUG_ALL_) #define DBGPrintfi(a) DbgPrintfi a; #define DBGPrintfo(a) DbgPrintfo a;#elif defined(_DEBUG_UTIL_C_) && defined(_TRACEHELPER_DEBUG_EACH_OF_FILE_) #define DBGPrintfi(a) DbgPrintfi a; #define DBGPrintfo(a) DbgPrintfo a;#else #define DBGPrintfi(a) #define DBGPrintfo(a)#endifextern ulg crc_32_tab[]; /* crc table, defined below *//* =========================================================================== * Copy input to output unchanged: zcat == cat with --force. * IN assertion: insize bytes have already been read in inbuf. */int copy(in, out) int in, out; /* input and output file descriptors */{ DBGPrintfi(("copy(In)\r\n")); { errno = 0; while (insize != 0 && (int)insize != EOF) { write_buf(out, (char*)inbuf, insize); bytes_out += insize; insize = read(in, (char*)inbuf, INBUFSIZ); } if ((int)insize == EOF && errno != 0) { read_error(); DBGPrintfo(("copy(out10)\r\n")); return -1; } bytes_in = bytes_out; DBGPrintfo(("copy(out1)\r\n")); return OK; }}/* =========================================================================== * Run a set of bytes through the crc shift register. If s is a NULL * pointer, then initialize the crc shift register contents instead. * Return the current crc in either case. */ulg updcrc(s, n) uch *s; /* pointer to bytes to pump through */ unsigned n; /* number of bytes in s[] */{ DBGPrintfi(("updcrc(In)\r\n")); { register ulg c; /* temporary variable */ static ulg crc = (ulg)0xffffffffL; /* shift register contents */ if (s == NULL) { c = 0xffffffffL; } else { c = crc; if (n) do { c = crc_32_tab[((int)c ^ (*s++)) & 0xff] ^ (c >> 8); } while (--n); } crc = c; DBGPrintfo(("updcrc(out1)\r\n")); return c ^ 0xffffffffL; /* (instead of ~c for 64-bit machines) */ }}/* =========================================================================== * Clear input and output buffers */void clear_bufs(){ DBGPrintfi(("clear_bufs(In)\r\n")); { outcnt = 0; insize = inptr = 0; bytes_in = bytes_out = 0L; } DBGPrintfo(("clear_bufs(out)\r\n"));}extern char *in_buf;extern int in_size;extern int in_rd_count;/* =========================================================================== * Fill the input buffer. This is called only when the buffer is empty. */// by bskimint fill_inbuf(eof_ok) int eof_ok; /* set if EOF acceptable as a result */{ DBGPrintfi(("fill_inbuf(In)\r\n")); { int len; /* Read as much as possible */ insize = 0; errno = 0; if ( in_size > in_rd_count) len = in_size - in_rd_count; else len = 0; if ( len > INBUFSIZ) { len = INBUFSIZ; } memcpy(inbuf, &in_buf[in_rd_count], len); insize = len; if (insize == 0) { DBGPrintfo(("fill_inbuf(out1)\r\n")); return EOF; } in_rd_count += insize; bytes_in += (ulg)insize; inptr = 1; DBGPrintfo(("fill_inbuf(out2)\r\n")); return inbuf[0]; }}#if 0int fill_inbuf(eof_ok) int eof_ok; /* set if EOF acceptable as a result */{ DBGPrintfi(("fill_inbuf(In)\r\n")); { int len; /* Read as much as possible */ insize = 0; errno = 0; do { len = read(ifd, (char*)inbuf+insize, INBUFSIZ-insize); if (len == 0 || len == EOF) break; insize += len; } while (insize < INBUFSIZ); if (insize == 0) { if (eof_ok) { DBGPrintfo(("fill_inbuf(out1)\r\n")); return EOF; } read_error(); } bytes_in += (ulg)insize; inptr = 1; DBGPrintfo(("fill_inbuf(out2)\r\n")); return inbuf[0]; }}#endifextern char *out_buf;extern int out_buf_size;extern int *out_size;extern int *outUnitSize;extern char **pout;/* =========================================================================== * Write the output buffer outbuf[0..outcnt-1] and update bytes_out. * (used for the compressed data only) */void flush_outbuf(){ DBGPrintfi(("flush_outbuf(In)\r\n")); { char *tbuf; int size1, size2; if ( !*pout ) { *pout = out_buf = (char *)xmalloc(OUTBUFSIZ+sizeof(char*)); if (out_buf == 0) return; *((char **)&out_buf[OUTBUFSIZ]) = 0; *out_size = 0; out_buf_size = OUTBUFSIZ; *outUnitSize = OUTBUFSIZ; } if (outcnt == 0) { DBGPrintfo(("flush_outbuf(out1)\r\n")); return ; } if ( (*out_size+outcnt) > out_buf_size ) { while(outcnt>0) { size1 = out_buf_size - *out_size; memcpy(&out_buf[*out_size - (out_buf_size - OUTBUFSIZ)], outbuf, size1); tbuf = (char *)xmalloc(OUTBUFSIZ+sizeof(char*)); if (tbuf == 0) return; *((char **)&tbuf[OUTBUFSIZ]) = 0; *((char **)&out_buf[OUTBUFSIZ]) = tbuf; out_buf = tbuf; size2 = outcnt - size1; if ( size2 > OUTBUFSIZ ) size2 = OUTBUFSIZ; memcpy(&out_buf[0], &outbuf[size1], size2); out_buf_size += OUTBUFSIZ; *out_size += (size1+size2); outcnt -= (size1+size2); } } else { memcpy(&out_buf[*out_size], outbuf, outcnt); *out_size += outcnt; } bytes_out += (ulg)outcnt; outcnt = 0; } DBGPrintfo(("flush_outbuf(out)\r\n"));}#if 0void flush_outbuf(){ DBGPrintfi(("flush_outbuf(In)\r\n")); { if (outcnt == 0) { DBGPrintfo(("flush_outbuf(out1)\r\n")); return ; } write_buf(ofd, (char *)outbuf, outcnt); bytes_out += (ulg)outcnt; outcnt = 0; } DBGPrintfo(("flush_outbuf(out)\r\n"));}#endif/* =========================================================================== * Write the output window window[0..outcnt-1] and update crc and bytes_out. * (Used for the decompressed data only.) */// by bskimvoid flush_window(){ DBGPrintfi(("flush_window(In)\r\n")); { char *tbuf; int size1, size2; if ( !*pout ) { *pout = out_buf = (char *)xmalloc(WSIZE+sizeof(char*)); if (out_buf==0) return; *((char **)&out_buf[WSIZE]) = 0; *out_size = 0; out_buf_size = WSIZE; *outUnitSize = WSIZE; } if (outcnt == 0) { DBGPrintfo(("flush_window(out1)\r\n")); return ; } updcrc(window, outcnt); if ( (*out_size+outcnt) > out_buf_size) { while(outcnt>0) { size1 = out_buf_size - *out_size; memcpy(&out_buf[*out_size - (out_buf_size - WSIZE)], window, size1); tbuf = (char *)xmalloc(WSIZE+sizeof(char*)); if (tbuf ==0) return; *((char **)&tbuf[WSIZE]) = 0; *((char **)&out_buf[WSIZE]) = tbuf; out_buf = tbuf; size2 = outcnt - size1; if ( size2 > WSIZE ) size2 = WSIZE; memcpy(&out_buf[0], &outbuf[size1], size2); out_buf_size += WSIZE; *out_size += (size1+size2); outcnt -= (size1+size2); } } else { memcpy(&out_buf[*out_size], window, outcnt); *out_size += outcnt; } bytes_out += (ulg)outcnt; outcnt = 0; } DBGPrintfo(("flush_window(out)\r\n"));}#if 0void flush_window(){ DBGPrintfi(("flush_window(In)\r\n")); { if (outcnt == 0) { DBGPrintfo(("flush_window(out1)\r\n")); return ; } updcrc(window, outcnt); if (!test) { write_buf(ofd, (char *)window, outcnt); } bytes_out += (ulg)outcnt; outcnt = 0; } DBGPrintfo(("flush_window(out)\r\n"));}#endif/* =========================================================================== * Does the same as write(), but also handles partial pipe writes and checks * for error return. */void write_buf(fd, buf, cnt) int fd; voidp buf; unsigned cnt;{ DBGPrintfi(("write_buf(In)\r\n")); { } DBGPrintfo(("write_buf(out)\r\n"));}#if 0void write_buf(fd, buf, cnt) int fd; voidp buf; unsigned cnt;{ DBGPrintfi(("write_buf(In)\r\n")); { unsigned n; while ((n = write(fd, buf, cnt)) != cnt) { if (n == (unsigned)(-1)) { write_error(); } cnt -= n;
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -