2 * Copyright (c) 2011, Novell Inc.
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
16 #include "solv_xfopen.h"
19 static FILE *cookieopen(void *cookie, const char *mode,
20 ssize_t (*cread)(void *, char *, size_t),
21 ssize_t (*cwrite)(void *, const char *, size_t),
22 int (*cclose)(void *))
27 return funopen(cookie,
28 (int (*)(void *, char *, int))(*mode == 'r' ? cread: NULL),/* readfn */
29 (int (*)(void *, const char *, int))(*mode == 'w' ? cwrite : NULL), /* writefn */
30 (fpos_t (*)(void *, fpos_t, int))NULL, /* seekfn */
33 #elif defined(HAVE_FOPENCOOKIE)
34 cookie_io_functions_t cio;
35 memset(&cio, 0, sizeof(cio));
38 else if (*mode == 'w')
41 return fopencookie(cookie, *mode == 'w' ? "w" : "r", cio);
43 # error Need to implement custom I/O
48 /* gzip compression */
50 static ssize_t cookie_gzread(void *cookie, char *buf, size_t nbytes)
52 return gzread((gzFile *)cookie, buf, nbytes);
55 static ssize_t cookie_gzwrite(void *cookie, const char *buf, size_t nbytes)
57 return gzwrite((gzFile *)cookie, buf, nbytes);
60 static int cookie_gzclose(void *cookie)
62 return gzclose((gzFile *)cookie);
65 static inline FILE *mygzfopen(const char *fn, const char *mode)
67 gzFile *gzf = gzopen(fn, mode);
68 return cookieopen(gzf, mode, cookie_gzread, cookie_gzwrite, cookie_gzclose);
71 static inline FILE *mygzfdopen(int fd, const char *mode)
73 gzFile *gzf = gzdopen(fd, mode);
74 return cookieopen(gzf, mode, cookie_gzread, cookie_gzwrite, cookie_gzclose);
78 #ifdef ENABLE_XZ_COMPRESSION
82 typedef struct lzfile {
83 unsigned char buf[1 << 15];
90 static LZFILE *lzopen(const char *path, const char *mode, int fd, int xz)
92 int level = 7; /* Use XZ's default compression level if unspecified */
97 lzma_stream init_strm = LZMA_STREAM_INIT;
101 for (; *mode; mode++) {
104 else if (*mode == 'r')
106 else if (*mode >= '1' && *mode <= '9')
110 fp = fdopen(fd, encoding ? "w" : "r");
112 fp = fopen(path, encoding ? "w" : "r");
115 lzfile = calloc(1, sizeof(*lzfile));
119 lzfile->encoding = encoding;
121 lzfile->strm = init_strm;
124 ret = lzma_easy_encoder(&lzfile->strm, level, LZMA_CHECK_SHA256);
126 lzma_options_lzma options;
127 lzma_lzma_preset(&options, level);
128 ret = lzma_alone_encoder(&lzfile->strm, &options);
130 } else { /* lzma_easy_decoder_memusage(level) is not ready yet, use hardcoded limit for now */
131 ret = lzma_auto_decoder(&lzfile->strm, 100<<20, 0);
133 if (ret != LZMA_OK) {
141 static int lzclose(void *cookie)
143 LZFILE *lzfile = cookie;
150 if (lzfile->encoding) {
152 lzfile->strm.avail_out = sizeof(lzfile->buf);
153 lzfile->strm.next_out = lzfile->buf;
154 ret = lzma_code(&lzfile->strm, LZMA_FINISH);
155 if (ret != LZMA_OK && ret != LZMA_STREAM_END)
157 n = sizeof(lzfile->buf) - lzfile->strm.avail_out;
158 if (n && fwrite(lzfile->buf, 1, n, lzfile->file) != n)
160 if (ret == LZMA_STREAM_END)
164 lzma_end(&lzfile->strm);
165 rc = fclose(lzfile->file);
170 static ssize_t lzread(void *cookie, char *buf, size_t len)
172 LZFILE *lzfile = cookie;
176 if (!lzfile || lzfile->encoding)
180 lzfile->strm.next_out = (unsigned char *)buf;
181 lzfile->strm.avail_out = len;
183 if (!lzfile->strm.avail_in) {
184 lzfile->strm.next_in = lzfile->buf;
185 lzfile->strm.avail_in = fread(lzfile->buf, 1, sizeof(lzfile->buf), lzfile->file);
186 if (!lzfile->strm.avail_in)
189 ret = lzma_code(&lzfile->strm, LZMA_RUN);
190 if (ret == LZMA_STREAM_END) {
192 return len - lzfile->strm.avail_out;
196 if (!lzfile->strm.avail_out)
203 static ssize_t lzwrite(void *cookie, const char *buf, size_t len)
205 LZFILE *lzfile = cookie;
208 if (!lzfile || !lzfile->encoding)
212 lzfile->strm.next_in = (unsigned char *)buf;
213 lzfile->strm.avail_in = len;
215 lzfile->strm.next_out = lzfile->buf;
216 lzfile->strm.avail_out = sizeof(lzfile->buf);
217 ret = lzma_code(&lzfile->strm, LZMA_RUN);
220 n = sizeof(lzfile->buf) - lzfile->strm.avail_out;
221 if (n && fwrite(lzfile->buf, 1, n, lzfile->file) != n)
223 if (!lzfile->strm.avail_in)
228 static inline FILE *myxzfopen(const char *fn, const char *mode)
230 LZFILE *lzf = lzopen(fn, mode, -1, 1);
231 return cookieopen(lzf, mode, lzread, lzwrite, lzclose);
234 static inline FILE *myxzfdopen(int fd, const char *mode)
236 LZFILE *lzf = lzopen(0, mode, fd, 1);
237 return cookieopen(lzf, mode, lzread, lzwrite, lzclose);
240 static inline FILE *mylzfopen(const char *fn, const char *mode)
242 LZFILE *lzf = lzopen(fn, mode, -1, 0);
243 return cookieopen(lzf, mode, lzread, lzwrite, lzclose);
246 static inline FILE *mylzfdopen(int fd, const char *mode)
248 LZFILE *lzf = lzopen(0, mode, fd, 0);
249 return cookieopen(lzf, mode, lzread, lzwrite, lzclose);
252 #endif /* ENABLE_XZ_COMPRESSION */
256 solv_xfopen(const char *fn, const char *mode)
264 suf = strrchr(fn, '.');
265 if (suf && !strcmp(suf, ".gz"))
266 return mygzfopen(fn, mode);
267 #ifdef ENABLE_XZ_COMPRESSION
268 if (suf && !strcmp(suf, ".xz"))
269 return myxzfopen(fn, mode);
270 if (suf && !strcmp(suf, ".lzma"))
271 return mylzfopen(fn, mode);
273 return fopen(fn, mode);
277 solv_xfopen_fd(const char *fn, int fd, const char *mode)
281 suf = fn ? strrchr(fn, '.') : 0;
284 int fl = fcntl(fd, F_GETFL, 0);
287 fl &= O_RDONLY|O_WRONLY|O_RDWR;
290 else if (fl == O_RDWR)
292 if (!suf || strcmp(suf, ".gz") != 0)
300 if (suf && !strcmp(suf, ".gz"))
301 return mygzfdopen(fd, mode);
302 #ifdef ENABLE_XZ_COMPRESSION
303 if (suf && !strcmp(suf, ".xz"))
304 return myxzfdopen(fd, mode);
305 if (suf && !strcmp(suf, ".lzma"))
306 return mylzfdopen(fd, mode);
308 return fdopen(fd, mode);