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"
20 static FILE *cookieopen(void *cookie, const char *mode,
21 ssize_t (*cread)(void *, char *, size_t),
22 ssize_t (*cwrite)(void *, const char *, size_t),
23 int (*cclose)(void *))
28 return funopen(cookie,
29 (int (*)(void *, char *, int))(*mode == 'r' ? cread: NULL),/* readfn */
30 (int (*)(void *, const char *, int))(*mode == 'w' ? cwrite : NULL), /* writefn */
31 (fpos_t (*)(void *, fpos_t, int))NULL, /* seekfn */
34 #elif defined(HAVE_FOPENCOOKIE)
35 cookie_io_functions_t cio;
36 memset(&cio, 0, sizeof(cio));
39 else if (*mode == 'w')
42 return fopencookie(cookie, *mode == 'w' ? "w" : "r", cio);
44 # error Need to implement custom I/O
49 /* gzip compression */
51 static ssize_t cookie_gzread(void *cookie, char *buf, size_t nbytes)
53 return gzread((gzFile)cookie, buf, nbytes);
56 static ssize_t cookie_gzwrite(void *cookie, const char *buf, size_t nbytes)
58 return gzwrite((gzFile)cookie, buf, nbytes);
61 static int cookie_gzclose(void *cookie)
63 return gzclose((gzFile)cookie);
66 static inline FILE *mygzfopen(const char *fn, const char *mode)
68 gzFile gzf = gzopen(fn, mode);
69 return cookieopen(gzf, mode, cookie_gzread, cookie_gzwrite, cookie_gzclose);
72 static inline FILE *mygzfdopen(int fd, const char *mode)
74 gzFile gzf = gzdopen(fd, mode);
75 return cookieopen(gzf, mode, cookie_gzread, cookie_gzwrite, cookie_gzclose);
78 #ifdef ENABLE_BZIP2_COMPRESSION
82 /* bzip2 compression */
84 static ssize_t cookie_bzread(void *cookie, char *buf, size_t nbytes)
86 return BZ2_bzread((BZFILE *)cookie, buf, nbytes);
89 static ssize_t cookie_bzwrite(void *cookie, const char *buf, size_t nbytes)
91 return BZ2_bzwrite((BZFILE *)cookie, (char *)buf, nbytes);
94 static int cookie_bzclose(void *cookie)
96 BZ2_bzclose((BZFILE *)cookie);
100 static inline FILE *mybzfopen(const char *fn, const char *mode)
102 BZFILE *bzf = BZ2_bzopen(fn, mode);
103 return cookieopen(bzf, mode, cookie_bzread, cookie_bzwrite, cookie_bzclose);
106 static inline FILE *mybzfdopen(int fd, const char *mode)
108 BZFILE *bzf = BZ2_bzdopen(fd, mode);
109 return cookieopen(bzf, mode, cookie_bzread, cookie_bzwrite, cookie_bzclose);
115 #ifdef ENABLE_LZMA_COMPRESSION
119 /* lzma code written by me in 2008 for rpm's rpmio.c */
121 typedef struct lzfile {
122 unsigned char buf[1 << 15];
129 static inline lzma_ret setup_alone_encoder(lzma_stream *strm, int level)
131 lzma_options_lzma options;
132 lzma_lzma_preset(&options, level);
133 return lzma_alone_encoder(strm, &options);
136 static lzma_stream stream_init = LZMA_STREAM_INIT;
138 static LZFILE *lzopen(const char *path, const char *mode, int fd, int isxz)
148 for (; *mode; mode++)
152 else if (*mode == 'r')
154 else if (*mode >= '1' && *mode <= '9')
158 fp = fdopen(fd, encoding ? "w" : "r");
160 fp = fopen(path, encoding ? "w" : "r");
163 lzfile = calloc(1, sizeof(*lzfile));
170 lzfile->encoding = encoding;
172 lzfile->strm = stream_init;
176 ret = lzma_easy_encoder(&lzfile->strm, level, LZMA_CHECK_SHA256);
178 ret = setup_alone_encoder(&lzfile->strm, level);
181 ret = lzma_auto_decoder(&lzfile->strm, 100 << 20, 0);
191 static int lzclose(void *cookie)
193 LZFILE *lzfile = cookie;
200 if (lzfile->encoding)
204 lzfile->strm.avail_out = sizeof(lzfile->buf);
205 lzfile->strm.next_out = lzfile->buf;
206 ret = lzma_code(&lzfile->strm, LZMA_FINISH);
207 if (ret != LZMA_OK && ret != LZMA_STREAM_END)
209 n = sizeof(lzfile->buf) - lzfile->strm.avail_out;
210 if (n && fwrite(lzfile->buf, 1, n, lzfile->file) != n)
212 if (ret == LZMA_STREAM_END)
216 lzma_end(&lzfile->strm);
217 rc = fclose(lzfile->file);
222 static ssize_t lzread(void *cookie, char *buf, size_t len)
224 LZFILE *lzfile = cookie;
228 if (!lzfile || lzfile->encoding)
232 lzfile->strm.next_out = (unsigned char *)buf;
233 lzfile->strm.avail_out = len;
236 if (!lzfile->strm.avail_in)
238 lzfile->strm.next_in = lzfile->buf;
239 lzfile->strm.avail_in = fread(lzfile->buf, 1, sizeof(lzfile->buf), lzfile->file);
240 if (!lzfile->strm.avail_in)
243 ret = lzma_code(&lzfile->strm, LZMA_RUN);
244 if (ret == LZMA_STREAM_END)
247 return len - lzfile->strm.avail_out;
251 if (!lzfile->strm.avail_out)
258 static ssize_t lzwrite(void *cookie, const char *buf, size_t len)
260 LZFILE *lzfile = cookie;
263 if (!lzfile || !lzfile->encoding)
267 lzfile->strm.next_in = (unsigned char *)buf;
268 lzfile->strm.avail_in = len;
271 lzfile->strm.next_out = lzfile->buf;
272 lzfile->strm.avail_out = sizeof(lzfile->buf);
273 ret = lzma_code(&lzfile->strm, LZMA_RUN);
276 n = sizeof(lzfile->buf) - lzfile->strm.avail_out;
277 if (n && fwrite(lzfile->buf, 1, n, lzfile->file) != n)
279 if (!lzfile->strm.avail_in)
284 static inline FILE *myxzfopen(const char *fn, const char *mode)
286 LZFILE *lzf = lzopen(fn, mode, -1, 1);
287 return cookieopen(lzf, mode, lzread, lzwrite, lzclose);
290 static inline FILE *myxzfdopen(int fd, const char *mode)
292 LZFILE *lzf = lzopen(0, mode, fd, 1);
293 return cookieopen(lzf, mode, lzread, lzwrite, lzclose);
296 static inline FILE *mylzfopen(const char *fn, const char *mode)
298 LZFILE *lzf = lzopen(fn, mode, -1, 0);
299 return cookieopen(lzf, mode, lzread, lzwrite, lzclose);
302 static inline FILE *mylzfdopen(int fd, const char *mode)
304 LZFILE *lzf = lzopen(0, mode, fd, 0);
305 return cookieopen(lzf, mode, lzread, lzwrite, lzclose);
308 #endif /* ENABLE_LZMA_COMPRESSION */
312 solv_xfopen(const char *fn, const char *mode)
320 suf = strrchr(fn, '.');
321 if (suf && !strcmp(suf, ".gz"))
322 return mygzfopen(fn, mode);
323 #ifdef ENABLE_LZMA_COMPRESSION
324 if (suf && !strcmp(suf, ".xz"))
325 return myxzfopen(fn, mode);
326 if (suf && !strcmp(suf, ".lzma"))
327 return mylzfopen(fn, mode);
329 #ifdef ENABLE_BZIP2_COMPRESSION
330 if (suf && !strcmp(suf, ".bz2"))
331 return mybzfopen(fn, mode);
333 return fopen(fn, mode);
337 solv_xfopen_fd(const char *fn, int fd, const char *mode)
339 const char *simplemode = mode;
342 suf = fn ? strrchr(fn, '.') : 0;
345 int fl = fcntl(fd, F_GETFL, 0);
348 fl &= O_RDONLY|O_WRONLY|O_RDWR;
350 mode = simplemode = "w";
351 else if (fl == O_RDWR)
357 mode = simplemode = "r";
359 if (suf && !strcmp(suf, ".gz"))
360 return mygzfdopen(fd, simplemode);
361 #ifdef ENABLE_LZMA_COMPRESSION
362 if (suf && !strcmp(suf, ".xz"))
363 return myxzfdopen(fd, simplemode);
364 if (suf && !strcmp(suf, ".lzma"))
365 return mylzfdopen(fd, simplemode);
367 #ifdef ENABLE_BZIP2_COMPRESSION
368 if (suf && !strcmp(suf, ".bz2"))
369 return mybzfdopen(fd, simplemode);
371 return fdopen(fd, mode);
381 static ssize_t cookie_bufread(void *cookie, char *buf, size_t nbytes)
383 struct bufcookie *bc = cookie;
384 size_t n = *bc->buflp > nbytes ? nbytes : *bc->buflp;
387 memcpy(buf, *bc->bufp, n);
394 static ssize_t cookie_bufwrite(void *cookie, const char *buf, size_t nbytes)
396 struct bufcookie *bc = cookie;
397 int n = nbytes > 0x40000000 ? 0x40000000 : nbytes;
400 *bc->bufp = solv_extend(*bc->bufp, *bc->buflp, n + 1, 1, 4095);
401 memcpy(*bc->bufp, buf, n);
402 (*bc->bufp)[n] = 0; /* zero-terminate */
408 static int cookie_bufclose(void *cookie)
410 struct bufcookie *bc = cookie;
412 solv_free(bc->freemem);
418 solv_xfopen_buf(const char *fn, char **bufp, size_t *buflp, const char *mode)
420 struct bufcookie *bc;
422 if (*mode != 'r' && *mode != 'w')
424 bc = solv_calloc(1, sizeof(*bc));
429 bc->bufl_int = *mode == 'w' ? 0 : strlen(*bufp);
430 buflp = &bc->bufl_int;
435 *bc->bufp = solv_extend(0, 0, 1, 1, 4095); /* always zero-terminate */
439 fp = cookieopen(bc, mode, cookie_bufread, cookie_bufwrite, cookie_bufclose);
440 if (!strcmp(mode, "rf")) /* auto-free */
444 *bc->bufp = solv_free(*bc->bufp);