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 391 392 393 394 395 396 397 398 399 400
  
     | 
    
      /*
 * $Id: kern_gzio.c,v 1.6 2008-10-18 22:54:45 lbazinet Exp $
 *
 * core_gzip.c -- gzip routines used in compressing user process cores
 *
 * This file is derived from src/lib/libz/gzio.c in FreeBSD.
 */
/* gzio.c -- IO on .gz files
 * Copyright (C) 1995-1998 Jean-loup Gailly.
 * For conditions of distribution and use, see copyright notice in zlib.h
 *
 */
/* @(#) $FreeBSD$ */
#include <sys/param.h>
#include <sys/proc.h>
#include <sys/malloc.h>
#include <sys/vnode.h>
#include <sys/syslog.h>
#include <sys/endian.h>
#include <net/zutil.h>
#include <sys/libkern.h>
#include <sys/vnode.h>
#include <sys/mount.h>
#define GZ_HEADER_LEN	10
#ifndef Z_BUFSIZE
#  ifdef MAXSEG_64K
#    define Z_BUFSIZE 4096 /* minimize memory usage for 16-bit DOS */
#  else
#    define Z_BUFSIZE 16384
#  endif
#endif
#ifndef Z_PRINTF_BUFSIZE
#  define Z_PRINTF_BUFSIZE 4096
#endif
#define ALLOC(size) malloc(size, M_TEMP, M_WAITOK | M_ZERO)
#define TRYFREE(p) {if (p) free(p, M_TEMP);}
static int gz_magic[2] = {0x1f, 0x8b}; /* gzip magic header */
/* gzip flag byte */
#define ASCII_FLAG   0x01 /* bit 0 set: file probably ascii text */
#define HEAD_CRC     0x02 /* bit 1 set: header CRC present */
#define EXTRA_FIELD  0x04 /* bit 2 set: extra field present */
#define ORIG_NAME    0x08 /* bit 3 set: original file name present */
#define COMMENT      0x10 /* bit 4 set: file comment present */
#define RESERVED     0xE0 /* bits 5..7: reserved */
typedef struct gz_stream {
    z_stream stream;
    int      z_err;   /* error code for last stream operation */
    int      z_eof;   /* set if end of input file */
    struct vnode *file; /* vnode pointer of .gz file */
    Byte     *inbuf;  /* input buffer */
    Byte     *outbuf; /* output buffer */
    uLong    crc;     /* crc32 of uncompressed data */
    char     *msg;    /* error message */
    char     *path;   /* path name for debugging only */
    int      transparent; /* 1 if input file is not a .gz file */
    char     mode;    /* 'w' or 'r' */
    long     startpos; /* start of compressed data in file (header skipped) */
    off_t    outoff;  /* current offset in output file */
    int	     flags;
} gz_stream;
local int do_flush        OF((gzFile file, int flush));
local int    destroy      OF((gz_stream *s));
local void   putU32      OF((gz_stream *file, uint32_t x));
local void *gz_alloc      OF((void *notused, u_int items, u_int size));
local void gz_free        OF((void *notused, void *ptr));
/* ===========================================================================
     Opens a gzip (.gz) file for reading or writing. The mode parameter
   is as in fopen ("rb" or "wb"). The file is given either by file descriptor
   or path name (if fd == -1).
     gz_open return NULL if the file could not be opened or if there was
   insufficient memory to allocate the (de)compression state; errno
   can be checked to distinguish the two cases (if errno is zero, the
   zlib error is Z_MEM_ERROR).
*/
gzFile gz_open (path, mode, vp)
    const char *path;
    const char *mode;
    struct vnode *vp;
{
    int err;
    int level = Z_DEFAULT_COMPRESSION; /* compression level */
    int strategy = Z_DEFAULT_STRATEGY; /* compression strategy */
    const char *p = mode;
    gz_stream *s;
    char fmode[80]; /* copy of mode, without the compression level */
    char *m = fmode;
    ssize_t resid;
    int error;
    char buf[GZ_HEADER_LEN + 1];
    if (!path || !mode) return Z_NULL;
    s = (gz_stream *)ALLOC(sizeof(gz_stream));
    if (!s) return Z_NULL;
    s->stream.zalloc = (alloc_func)gz_alloc;
    s->stream.zfree = (free_func)gz_free;
    s->stream.opaque = (voidpf)0;
    s->stream.next_in = s->inbuf = Z_NULL;
    s->stream.next_out = s->outbuf = Z_NULL;
    s->stream.avail_in = s->stream.avail_out = 0;
    s->file = NULL;
    s->z_err = Z_OK;
    s->z_eof = 0;
    s->crc = 0;
    s->msg = NULL;
    s->transparent = 0;
    s->outoff = 0;
    s->flags = 0;
    s->path = (char*)ALLOC(strlen(path)+1);
    if (s->path == NULL) {
        return destroy(s), (gzFile)Z_NULL;
    }
    strcpy(s->path, path); /* do this early for debugging */
    s->mode = '\0';
    do {
        if (*p == 'r') s->mode = 'r';
        if (*p == 'w' || *p == 'a') s->mode = 'w';
        if (*p >= '0' && *p <= '9') {
	    level = *p - '0';
	} else if (*p == 'f') {
	  strategy = Z_FILTERED;
	} else if (*p == 'h') {
	  strategy = Z_HUFFMAN_ONLY;
	} else {
	    *m++ = *p; /* copy the mode */
	}
    } while (*p++ && m != fmode + sizeof(fmode));
    if (s->mode != 'w') {
        log(LOG_ERR, "gz_open: mode is not w (%c)\n", s->mode);
        return destroy(s), (gzFile)Z_NULL;
    }
    
    err = deflateInit2(&(s->stream), level,
                       Z_DEFLATED, -MAX_WBITS, DEF_MEM_LEVEL, strategy);
    /* windowBits is passed < 0 to suppress zlib header */
    s->stream.next_out = s->outbuf = (Byte*)ALLOC(Z_BUFSIZE);
    if (err != Z_OK || s->outbuf == Z_NULL) {
        return destroy(s), (gzFile)Z_NULL;
    }
    s->stream.avail_out = Z_BUFSIZE;
    s->file = vp;
    /* Write a very simple .gz header:
     */
    snprintf(buf, sizeof(buf), "%c%c%c%c%c%c%c%c%c%c", gz_magic[0],
             gz_magic[1], Z_DEFLATED, 0 /*flags*/, 0,0,0,0 /*time*/,
             0 /*xflags*/, OS_CODE);
    if ((error = vn_rdwr(UIO_WRITE, s->file, buf, GZ_HEADER_LEN, s->outoff,
                         UIO_SYSSPACE, IO_UNIT, curproc->p_ucred,
                         NOCRED, &resid, curthread))) {
        s->outoff += GZ_HEADER_LEN - resid;
        return destroy(s), (gzFile)Z_NULL;
    }
    s->outoff += GZ_HEADER_LEN;
    s->startpos = 10L;
    
    return (gzFile)s;
}
 /* ===========================================================================
 * Cleanup then free the given gz_stream. Return a zlib error code.
   Try freeing in the reverse order of allocations.
 */
local int destroy (s)
    gz_stream *s;
{
    int err = Z_OK;
    if (!s) return Z_STREAM_ERROR;
    TRYFREE(s->msg);
    if (s->stream.state != NULL) {
	if (s->mode == 'w') {
	    err = deflateEnd(&(s->stream));
	}
    }
    if (s->z_err < 0) err = s->z_err;
    TRYFREE(s->inbuf);
    TRYFREE(s->outbuf);
    TRYFREE(s->path);
    TRYFREE(s);
    return err;
}
/* ===========================================================================
     Writes the given number of uncompressed bytes into the compressed file.
   gzwrite returns the number of bytes actually written (0 in case of error).
*/
int ZEXPORT gzwrite (file, buf, len)
    gzFile file;
    const voidp buf;
    unsigned len;
{
    gz_stream *s = (gz_stream*)file;
    off_t curoff;
    size_t resid;
    int error;
    if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR;
    s->stream.next_in = (Bytef*)buf;
    s->stream.avail_in = len;
    curoff = s->outoff;
    while (s->stream.avail_in != 0) {
        if (s->stream.avail_out == 0) {
            s->stream.next_out = s->outbuf;
            error = vn_rdwr_inchunks(UIO_WRITE, s->file, s->outbuf, Z_BUFSIZE,
                        curoff, UIO_SYSSPACE, IO_UNIT,
                        curproc->p_ucred, NOCRED, &resid, curthread);
            if (error) {
                log(LOG_ERR, "gzwrite: vn_rdwr return %d\n", error);
                curoff += Z_BUFSIZE - resid;
                s->z_err = Z_ERRNO;
                break;
            }
            curoff += Z_BUFSIZE;
            s->stream.avail_out = Z_BUFSIZE;
        }
        s->z_err = deflate(&(s->stream), Z_NO_FLUSH);
        if (s->z_err != Z_OK) {
            log(LOG_ERR,
                "gzwrite: deflate returned error %d\n", s->z_err);
            break;
        }
    }
    s->crc = ~crc32_raw(buf, len, ~s->crc);
    s->outoff = curoff;
    return (int)(len - s->stream.avail_in);
}
/* ===========================================================================
     Flushes all pending output into the compressed file. The parameter
   flush is as in the deflate() function.
*/
local int do_flush (file, flush)
    gzFile file;
    int flush;
{
    uInt len;
    int done = 0;
    gz_stream *s = (gz_stream*)file;
    off_t curoff = s->outoff;
    size_t resid;
    int error;
    if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR;
    if (s->stream.avail_in) {
        log(LOG_WARNING, "do_flush: avail_in non-zero on entry\n");
    } 
    s->stream.avail_in = 0; /* should be zero already anyway */
    for (;;) {
        len = Z_BUFSIZE - s->stream.avail_out;
        if (len != 0) {
            error = vn_rdwr_inchunks(UIO_WRITE, s->file, s->outbuf, len, curoff,
                        UIO_SYSSPACE, IO_UNIT, curproc->p_ucred,
                        NOCRED, &resid, curthread);
	    if (error) {
                s->z_err = Z_ERRNO;
                s->outoff = curoff + len - resid;
                return Z_ERRNO;
            }
            s->stream.next_out = s->outbuf;
            s->stream.avail_out = Z_BUFSIZE;
            curoff += len;
        }
        if (done) break;
        s->z_err = deflate(&(s->stream), flush);
	/* Ignore the second of two consecutive flushes: */
	if (len == 0 && s->z_err == Z_BUF_ERROR) s->z_err = Z_OK;
        /* deflate has finished flushing only when it hasn't used up
         * all the available space in the output buffer: 
         */
        done = (s->stream.avail_out != 0 || s->z_err == Z_STREAM_END);
 
        if (s->z_err != Z_OK && s->z_err != Z_STREAM_END) break;
    }
    s->outoff = curoff;
    return  s->z_err == Z_STREAM_END ? Z_OK : s->z_err;
}
int ZEXPORT gzflush (file, flush)
     gzFile file;
     int flush;
{
    gz_stream *s = (gz_stream*)file;
    int err = do_flush (file, flush);
    if (err) return err;
    return  s->z_err == Z_STREAM_END ? Z_OK : s->z_err;
}
/* ===========================================================================
   Outputs a long in LSB order to the given file
*/
local void putU32 (s, x)
    gz_stream *s;
    uint32_t x;
{
    uint32_t xx;
    off_t curoff = s->outoff;
    ssize_t resid;
#if BYTE_ORDER == BIG_ENDIAN
    xx = bswap32(x);
#else
    xx = x;
#endif
    vn_rdwr(UIO_WRITE, s->file, (caddr_t)&xx, sizeof(xx), curoff,
            UIO_SYSSPACE, IO_UNIT, curproc->p_ucred,
            NOCRED, &resid, curthread);
    s->outoff += sizeof(xx) - resid;
}
/* ===========================================================================
     Flushes all pending output if necessary, closes the compressed file
   and deallocates all the (de)compression state.
*/
int ZEXPORT gzclose (file)
    gzFile file;
{
    int err;
    gz_stream *s = (gz_stream*)file;
    if (s == NULL) return Z_STREAM_ERROR;
    if (s->mode == 'w') {
        err = do_flush (file, Z_FINISH);
        if (err != Z_OK) {
            log(LOG_ERR, "gzclose: do_flush failed (err %d)\n", err);
            return destroy((gz_stream*)file);
        }
#if 0
	printf("gzclose: putting crc: %lld total: %lld\n",
	    (long long)s->crc, (long long)s->stream.total_in);
	printf("sizeof uLong = %d\n", (int)sizeof(uLong));
#endif
        putU32 (s, s->crc);
        putU32 (s, (uint32_t) s->stream.total_in);
    }
    return destroy((gz_stream*)file);
}
/*
 * Space allocation and freeing routines for use by zlib routines when called
 * from gzip modules.
 */
static void *
gz_alloc(void *notused __unused, u_int items, u_int size)
{
    void *ptr;
    MALLOC(ptr, void *, items * size, M_TEMP, M_NOWAIT | M_ZERO);
    return ptr;
}
                                     
static void
gz_free(void *opaque __unused, void *ptr)
{
    FREE(ptr, M_TEMP);
}
 
     |