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_LZMA_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)
97 lzma_stream init_strm = LZMA_STREAM_INIT;
101 for (; *mode; mode++)
105 else if (*mode == 'r')
107 else if (*mode >= '1' && *mode <= '9')
111 fp = fdopen(fd, encoding ? "w" : "r");
113 fp = fopen(path, encoding ? "w" : "r");
116 lzfile = calloc(1, sizeof(*lzfile));
123 lzfile->encoding = encoding;
125 lzfile->strm = init_strm;
129 ret = lzma_easy_encoder(&lzfile->strm, level, LZMA_CHECK_SHA256);
132 lzma_options_lzma options;
133 lzma_lzma_preset(&options, level);
134 ret = lzma_alone_encoder(&lzfile->strm, &options);
139 /* lzma_easy_decoder_memusage(level) is not ready yet, use hardcoded limit for now */
140 ret = lzma_auto_decoder(&lzfile->strm, 100 << 20, 0);
151 static int lzclose(void *cookie)
153 LZFILE *lzfile = cookie;
160 if (lzfile->encoding)
164 lzfile->strm.avail_out = sizeof(lzfile->buf);
165 lzfile->strm.next_out = lzfile->buf;
166 ret = lzma_code(&lzfile->strm, LZMA_FINISH);
167 if (ret != LZMA_OK && ret != LZMA_STREAM_END)
169 n = sizeof(lzfile->buf) - lzfile->strm.avail_out;
170 if (n && fwrite(lzfile->buf, 1, n, lzfile->file) != n)
172 if (ret == LZMA_STREAM_END)
176 lzma_end(&lzfile->strm);
177 rc = fclose(lzfile->file);
182 static ssize_t lzread(void *cookie, char *buf, size_t len)
184 LZFILE *lzfile = cookie;
188 if (!lzfile || lzfile->encoding)
192 lzfile->strm.next_out = (unsigned char *)buf;
193 lzfile->strm.avail_out = len;
196 if (!lzfile->strm.avail_in)
198 lzfile->strm.next_in = lzfile->buf;
199 lzfile->strm.avail_in = fread(lzfile->buf, 1, sizeof(lzfile->buf), lzfile->file);
200 if (!lzfile->strm.avail_in)
203 ret = lzma_code(&lzfile->strm, LZMA_RUN);
204 if (ret == LZMA_STREAM_END)
207 return len - lzfile->strm.avail_out;
211 if (!lzfile->strm.avail_out)
218 static ssize_t lzwrite(void *cookie, const char *buf, size_t len)
220 LZFILE *lzfile = cookie;
223 if (!lzfile || !lzfile->encoding)
227 lzfile->strm.next_in = (unsigned char *)buf;
228 lzfile->strm.avail_in = len;
231 lzfile->strm.next_out = lzfile->buf;
232 lzfile->strm.avail_out = sizeof(lzfile->buf);
233 ret = lzma_code(&lzfile->strm, LZMA_RUN);
236 n = sizeof(lzfile->buf) - lzfile->strm.avail_out;
237 if (n && fwrite(lzfile->buf, 1, n, lzfile->file) != n)
239 if (!lzfile->strm.avail_in)
244 static inline FILE *myxzfopen(const char *fn, const char *mode)
246 LZFILE *lzf = lzopen(fn, mode, -1, 1);
247 return cookieopen(lzf, mode, lzread, lzwrite, lzclose);
250 static inline FILE *myxzfdopen(int fd, const char *mode)
252 LZFILE *lzf = lzopen(0, mode, fd, 1);
253 return cookieopen(lzf, mode, lzread, lzwrite, lzclose);
256 static inline FILE *mylzfopen(const char *fn, const char *mode)
258 LZFILE *lzf = lzopen(fn, mode, -1, 0);
259 return cookieopen(lzf, mode, lzread, lzwrite, lzclose);
262 static inline FILE *mylzfdopen(int fd, const char *mode)
264 LZFILE *lzf = lzopen(0, mode, fd, 0);
265 return cookieopen(lzf, mode, lzread, lzwrite, lzclose);
268 #endif /* ENABLE_LZMA_COMPRESSION */
272 solv_xfopen(const char *fn, const char *mode)
280 suf = strrchr(fn, '.');
281 if (suf && !strcmp(suf, ".gz"))
282 return mygzfopen(fn, mode);
283 #ifdef ENABLE_LZMA_COMPRESSION
284 if (suf && !strcmp(suf, ".xz"))
285 return myxzfopen(fn, mode);
286 if (suf && !strcmp(suf, ".lzma"))
287 return mylzfopen(fn, mode);
289 return fopen(fn, mode);
293 solv_xfopen_fd(const char *fn, int fd, const char *mode)
297 suf = fn ? strrchr(fn, '.') : 0;
300 int fl = fcntl(fd, F_GETFL, 0);
303 fl &= O_RDONLY|O_WRONLY|O_RDWR;
306 else if (fl == O_RDWR)
308 if (!suf || strcmp(suf, ".gz") != 0)
316 if (suf && !strcmp(suf, ".gz"))
317 return mygzfdopen(fd, mode);
318 #ifdef ENABLE_LZMA_COMPRESSION
319 if (suf && !strcmp(suf, ".xz"))
320 return myxzfdopen(fd, mode);
321 if (suf && !strcmp(suf, ".lzma"))
322 return mylzfdopen(fd, mode);
324 return fdopen(fd, mode);