ia64/xen-unstable

view tools/libxutil/gzip_stream.c @ 2621:9402048e2325

bitkeeper revision 1.1159.1.218 (416a8128OiHXHyk_Sy8FsA0YUQcEnA)

Merge freefall.cl.cam.ac.uk:/auto/groups/xeno/users/cl349/BK/xeno.bk-26dom0
into freefall.cl.cam.ac.uk:/local/scratch/cl349/xeno.bk-26dom0
author cl349@freefall.cl.cam.ac.uk
date Mon Oct 11 12:48:40 2004 +0000 (2004-10-11)
parents 0e23f01219c6
children b5bf5415d400 d56c3246d889
line source
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 */
19 /** @file
20 * An IOStream implementation using zlib gzFile to provide
21 * compression and decompression.
22 */
23 #ifndef __KERNEL__
25 #include <stdio.h>
26 #include <stdlib.h>
28 #include "zlib.h"
30 #include "allocate.h"
31 #include "gzip_stream.h"
33 static int gzip_read(IOStream *s, void *buf, size_t n);
34 static int gzip_write(IOStream *s, const void *buf, size_t n);
35 static int gzip_error(IOStream *s);
36 static int gzip_close(IOStream *s);
37 static void gzip_free(IOStream *s);
38 static int gzip_flush(IOStream *s);
40 /** Methods used by a gzFile* IOStream. */
41 static 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 };
50 /** Get the underlying gzFile*.
51 *
52 * @param s gzip stream
53 * @return the stream s wraps
54 */
55 static inline gzFile get_gzfile(IOStream *s){
56 return (gzFile)s->data;
57 }
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 */
66 static int gzip_write(IOStream *s, const void *buf, size_t n){
67 return gzwrite(get_gzfile(s), (void*)buf, n);
68 }
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 */
77 static int gzip_read(IOStream *s, void *buf, size_t n){
78 return gzread(get_gzfile(s), buf, n);
79 }
81 /** Flush the underlying stream.
82 *
83 * @param s gzip stream
84 * @return 0 on success, error code otherwise
85 */
86 static 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 }
92 /** Check if a stream has an error.
93 *
94 * @param s gzip stream
95 * @return 1 if has an error, 0 otherwise
96 */
97 static 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 }
104 /** Close a gzip stream.
105 *
106 * @param s gzip stream to close
107 * @return result of the close
108 */
109 static int gzip_close(IOStream *s){
110 return gzclose(get_gzfile(s));
111 }
113 /** Free a gzip stream.
114 *
115 * @param s gzip stream
116 */
117 static void gzip_free(IOStream *s){
118 // Do nothing - fclose does it all?
119 }
121 /** Create an IOStream for a gzip stream.
122 *
123 * @param f stream to wrap
124 * @return new IOStream using f for i/o
125 */
126 IOStream *gzip_stream_new(gzFile *f){
127 IOStream *io = ALLOCATE(IOStream);
128 if(io){
129 io->methods = &gzip_methods;
130 io->data = (void*)f;
131 }
132 return io;
133 }
135 /** IOStream version of fopen().
136 *
137 * @param file name of the file to open
138 * @param flags giving the mode to open in (as for fopen())
139 * @return new stream for the open file, or NULL if failed
140 */
141 IOStream *gzip_stream_fopen(const char *file, const char *flags){
142 IOStream *io = NULL;
143 gzFile *fgz;
144 fgz = gzopen(file, flags);
145 if(fgz){
146 io = gzip_stream_new(fgz);
147 if(!io){
148 gzclose(fgz);
149 //free(fgz); // gzclose frees ?
150 }
151 }
152 return io;
153 }
155 /** IOStream version of fdopen().
156 *
157 * @param fd file descriptor
158 * @param flags giving the mode to open in (as for fdopen())
159 * @return new stream for the open file, or NULL if failed
160 */
161 IOStream *gzip_stream_fdopen(int fd, const char *flags){
162 IOStream *io = NULL;
163 gzFile *fgz;
164 fgz = gzdopen(fd, flags);
165 if(fgz){
166 io = gzip_stream_new(fgz);
167 }
168 return io;
169 }
170 #endif