source: trunk/packages/xen-3.1/xen-3.1/tools/vnet/libxutil/gzip_stream.c @ 34

Last change on this file since 34 was 34, checked in by hartmans, 19 years ago

Add xen and xen-common

File size: 4.3 KB
Line 
1/*
2 * Copyright (C) 2003 Hewlett-Packard Company.
3 *
4 * This library is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; either version 2.1 of the License, or
7 * (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17 */
18
19/** @file
20 * An IOStream implementation using zlib gzFile to provide
21 * compression and decompression.
22 */
23#ifndef __KERNEL__
24
25#include <stdio.h>
26#include <stdlib.h>
27
28#include "zlib.h"
29
30#include "allocate.h"
31#include "gzip_stream.h"
32
33static int gzip_read(IOStream *s, void *buf, size_t n);
34static int gzip_write(IOStream *s, const void *buf, size_t n);
35static int gzip_error(IOStream *s);
36static int gzip_close(IOStream *s);
37static void gzip_free(IOStream *s);
38static int gzip_flush(IOStream *s);
39
40/** Methods used by a gzFile* IOStream. */
41static const IOMethods gzip_methods = {
42    read:  gzip_read,
43    write: gzip_write,
44    error: gzip_error,
45    close: gzip_close,
46    free:  gzip_free,
47    flush: gzip_flush,
48};
49
50/** Get the underlying gzFile*.
51 *
52 * @param s gzip stream
53 * @return the stream s wraps
54 */
55static inline gzFile get_gzfile(IOStream *s){
56    return (gzFile)s->data;
57}
58
59/** Write to the underlying stream.
60 *
61 * @param stream destination
62 * @param buf data
63 * @param n number of bytes to write
64 * @return number of bytes written
65 */
66static int gzip_write(IOStream *s, const void *buf, size_t n){
67    return gzwrite(get_gzfile(s), (void*)buf, n);
68}
69
70/** Read from the underlying stream.
71 *
72 * @param stream input
73 * @param buf where to put input
74 * @param n number of bytes to read
75 * @return number of bytes read
76 */
77static int gzip_read(IOStream *s, void *buf, size_t n){
78    return gzread(get_gzfile(s), buf, n);
79}
80
81/** Flush the underlying stream.
82 *
83 * @param s gzip stream
84 * @return 0 on success, error code otherwise
85 */
86static int gzip_flush(IOStream *s){
87    //return gzflush(get_gzfile(s), Z_NO_FLUSH);
88    return gzflush(get_gzfile(s), Z_SYNC_FLUSH);
89    //return gzflush(get_gzfile(s), Z_FULL_FLUSH);
90}
91
92/** Check if a stream has an error.
93 *
94 * @param s gzip stream
95 * @return 1 if has an error, 0 otherwise
96 */
97static int gzip_error(IOStream *s){
98    int err;
99    gzFile *gz = get_gzfile(s);
100    gzerror(gz, &err);
101    return (err == Z_ERRNO ? 1 /* ferror(gzfile(gz)) */ : err);
102}
103
104/** Close a gzip stream.
105 *
106 * @param s gzip stream to close
107 * @return result of the close
108 */
109static int gzip_close(IOStream *s){
110    int result = 0;
111    result = gzclose(get_gzfile(s));
112    return result;
113}
114
115/** Free a gzip stream.
116 *
117 * @param s gzip stream
118 */
119static void gzip_free(IOStream *s){
120    // Nothing to do - close did it all.
121}
122
123/** Create an IOStream for a gzip stream.
124 *
125 * @param f stream to wrap
126 * @return new IOStream using f for i/o
127 */
128IOStream *gzip_stream_new(gzFile *f){
129    IOStream *io = ALLOCATE(IOStream);
130    if(io){
131        io->methods = &gzip_methods;
132        io->data = (void*)f;
133    }
134    return io;
135}
136
137/** IOStream version of fopen().
138 *
139 * @param file name of the file to open
140 * @param flags giving the mode to open in (as for fopen())
141 * @return new stream for the open file, or NULL if failed
142 */
143IOStream *gzip_stream_fopen(const char *file, const char *flags){
144    IOStream *io = NULL;
145    gzFile *fgz;
146    fgz = gzopen(file, flags);
147    if(fgz){
148        io = gzip_stream_new(fgz);
149        if(!io){
150            gzclose(fgz);
151        }
152    }
153    return io;
154}
155
156/** IOStream version of fdopen().
157 *
158 * @param fd file descriptor
159 * @param flags giving the mode to open in (as for fdopen())
160 * @return new stream for the open file, or NULL if failed.  Always takes
161 *         ownership of fd.
162 */
163IOStream *gzip_stream_fdopen(int fd, const char *flags){
164    IOStream *io = NULL;
165    gzFile *fgz;
166    fgz = gzdopen(fd, flags);
167    if(fgz){
168        io = gzip_stream_new(fgz);
169        if(!io)
170            gzclose(fgz);
171    }
172    return io;
173}
174#endif
Note: See TracBrowser for help on using the repository browser.