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)
36 cookie_io_functions_t cio;
37 memset(&cio, 0, sizeof(cio));
40 else if (*mode == 'w')
43 return fopencookie(cookie, *mode == 'w' ? "w" : "r", cio);
46 # error Need to implement custom I/O
51 /* gzip compression */
53 static ssize_t cookie_gzread(void *cookie, char *buf, size_t nbytes)
55 return gzread((gzFile)cookie, buf, nbytes);
58 static ssize_t cookie_gzwrite(void *cookie, const char *buf, size_t nbytes)
60 return gzwrite((gzFile)cookie, buf, nbytes);
63 static int cookie_gzclose(void *cookie)
65 return gzclose((gzFile)cookie);
68 static inline FILE *mygzfopen(const char *fn, const char *mode)
70 gzFile gzf = gzopen(fn, mode);
71 return cookieopen(gzf, mode, cookie_gzread, cookie_gzwrite, cookie_gzclose);
74 static inline FILE *mygzfdopen(int fd, const char *mode)
76 gzFile gzf = gzdopen(fd, mode);
77 return cookieopen(gzf, mode, cookie_gzread, cookie_gzwrite, cookie_gzclose);
80 #ifdef ENABLE_BZIP2_COMPRESSION
84 /* bzip2 compression */
86 static ssize_t cookie_bzread(void *cookie, char *buf, size_t nbytes)
88 return BZ2_bzread((BZFILE *)cookie, buf, nbytes);
91 static ssize_t cookie_bzwrite(void *cookie, const char *buf, size_t nbytes)
93 return BZ2_bzwrite((BZFILE *)cookie, (char *)buf, nbytes);
96 static int cookie_bzclose(void *cookie)
98 BZ2_bzclose((BZFILE *)cookie);
102 static inline FILE *mybzfopen(const char *fn, const char *mode)
104 BZFILE *bzf = BZ2_bzopen(fn, mode);
105 return cookieopen(bzf, mode, cookie_bzread, cookie_bzwrite, cookie_bzclose);
108 static inline FILE *mybzfdopen(int fd, const char *mode)
110 BZFILE *bzf = BZ2_bzdopen(fd, mode);
111 return cookieopen(bzf, mode, cookie_bzread, cookie_bzwrite, cookie_bzclose);
117 #ifdef ENABLE_LZMA_COMPRESSION
121 /* lzma code written by me in 2008 for rpm's rpmio.c */
123 typedef struct lzfile {
124 unsigned char buf[1 << 15];
131 static inline lzma_ret setup_alone_encoder(lzma_stream *strm, int level)
133 lzma_options_lzma options;
134 lzma_lzma_preset(&options, level);
135 return lzma_alone_encoder(strm, &options);
138 static lzma_stream stream_init = LZMA_STREAM_INIT;
140 static LZFILE *lzopen(const char *path, const char *mode, int fd, int isxz)
150 for (; *mode; mode++)
154 else if (*mode == 'r')
156 else if (*mode >= '1' && *mode <= '9')
160 fp = fdopen(fd, encoding ? "w" : "r");
162 fp = fopen(path, encoding ? "w" : "r");
165 lzfile = calloc(1, sizeof(*lzfile));
172 lzfile->encoding = encoding;
174 lzfile->strm = stream_init;
178 ret = lzma_easy_encoder(&lzfile->strm, level, LZMA_CHECK_SHA256);
180 ret = setup_alone_encoder(&lzfile->strm, level);
183 ret = lzma_auto_decoder(&lzfile->strm, 100 << 20, 0);
193 static int lzclose(void *cookie)
195 LZFILE *lzfile = cookie;
202 if (lzfile->encoding)
206 lzfile->strm.avail_out = sizeof(lzfile->buf);
207 lzfile->strm.next_out = lzfile->buf;
208 ret = lzma_code(&lzfile->strm, LZMA_FINISH);
209 if (ret != LZMA_OK && ret != LZMA_STREAM_END)
211 n = sizeof(lzfile->buf) - lzfile->strm.avail_out;
212 if (n && fwrite(lzfile->buf, 1, n, lzfile->file) != n)
214 if (ret == LZMA_STREAM_END)
218 lzma_end(&lzfile->strm);
219 rc = fclose(lzfile->file);
224 static ssize_t lzread(void *cookie, char *buf, size_t len)
226 LZFILE *lzfile = cookie;
230 if (!lzfile || lzfile->encoding)
234 lzfile->strm.next_out = (unsigned char *)buf;
235 lzfile->strm.avail_out = len;
238 if (!lzfile->strm.avail_in)
240 lzfile->strm.next_in = lzfile->buf;
241 lzfile->strm.avail_in = fread(lzfile->buf, 1, sizeof(lzfile->buf), lzfile->file);
242 if (!lzfile->strm.avail_in)
245 ret = lzma_code(&lzfile->strm, LZMA_RUN);
246 if (ret == LZMA_STREAM_END)
249 return len - lzfile->strm.avail_out;
253 if (!lzfile->strm.avail_out)
260 static ssize_t lzwrite(void *cookie, const char *buf, size_t len)
262 LZFILE *lzfile = cookie;
265 if (!lzfile || !lzfile->encoding)
269 lzfile->strm.next_in = (unsigned char *)buf;
270 lzfile->strm.avail_in = len;
273 lzfile->strm.next_out = lzfile->buf;
274 lzfile->strm.avail_out = sizeof(lzfile->buf);
275 ret = lzma_code(&lzfile->strm, LZMA_RUN);
278 n = sizeof(lzfile->buf) - lzfile->strm.avail_out;
279 if (n && fwrite(lzfile->buf, 1, n, lzfile->file) != n)
281 if (!lzfile->strm.avail_in)
286 static inline FILE *myxzfopen(const char *fn, const char *mode)
288 LZFILE *lzf = lzopen(fn, mode, -1, 1);
289 return cookieopen(lzf, mode, lzread, lzwrite, lzclose);
292 static inline FILE *myxzfdopen(int fd, const char *mode)
294 LZFILE *lzf = lzopen(0, mode, fd, 1);
295 return cookieopen(lzf, mode, lzread, lzwrite, lzclose);
298 static inline FILE *mylzfopen(const char *fn, const char *mode)
300 LZFILE *lzf = lzopen(fn, mode, -1, 0);
301 return cookieopen(lzf, mode, lzread, lzwrite, lzclose);
304 static inline FILE *mylzfdopen(int fd, const char *mode)
306 LZFILE *lzf = lzopen(0, mode, fd, 0);
307 return cookieopen(lzf, mode, lzread, lzwrite, lzclose);
310 #endif /* ENABLE_LZMA_COMPRESSION */
314 solv_xfopen(const char *fn, const char *mode)
322 suf = strrchr(fn, '.');
323 if (suf && !strcmp(suf, ".gz"))
324 return mygzfopen(fn, mode);
325 #ifdef ENABLE_LZMA_COMPRESSION
326 if (suf && !strcmp(suf, ".xz"))
327 return myxzfopen(fn, mode);
328 if (suf && !strcmp(suf, ".lzma"))
329 return mylzfopen(fn, mode);
331 if (suf && !strcmp(suf, ".xz"))
333 if (suf && !strcmp(suf, ".lzma"))
336 #ifdef ENABLE_BZIP2_COMPRESSION
337 if (suf && !strcmp(suf, ".bz2"))
338 return mybzfopen(fn, mode);
340 if (suf && !strcmp(suf, ".bz2"))
343 return fopen(fn, mode);
347 solv_xfopen_fd(const char *fn, int fd, const char *mode)
349 const char *simplemode = mode;
352 suf = fn ? strrchr(fn, '.') : 0;
355 int fl = fcntl(fd, F_GETFL, 0);
358 fl &= O_RDONLY|O_WRONLY|O_RDWR;
360 mode = simplemode = "w";
361 else if (fl == O_RDWR)
367 mode = simplemode = "r";
369 if (suf && !strcmp(suf, ".gz"))
370 return mygzfdopen(fd, simplemode);
371 #ifdef ENABLE_LZMA_COMPRESSION
372 if (suf && !strcmp(suf, ".xz"))
373 return myxzfdopen(fd, simplemode);
374 if (suf && !strcmp(suf, ".lzma"))
375 return mylzfdopen(fd, simplemode);
377 if (suf && !strcmp(suf, ".xz"))
379 if (suf && !strcmp(suf, ".lzma"))
382 #ifdef ENABLE_BZIP2_COMPRESSION
383 if (suf && !strcmp(suf, ".bz2"))
384 return mybzfdopen(fd, simplemode);
386 if (suf && !strcmp(suf, ".bz2"))
389 return fdopen(fd, mode);
393 solv_xfopen_iscompressed(const char *fn)
395 const char *suf = fn ? strrchr(fn, '.') : 0;
398 if (!strcmp(suf, ".gz"))
400 if (!strcmp(suf, ".xz") || !strcmp(suf, ".lzma"))
401 #ifdef ENABLE_LZMA_COMPRESSION
406 if (!strcmp(suf, ".bz2"))
407 #ifdef ENABLE_BZIP2_COMPRESSION
422 static ssize_t cookie_bufread(void *cookie, char *buf, size_t nbytes)
424 struct bufcookie *bc = cookie;
425 size_t n = *bc->buflp > nbytes ? nbytes : *bc->buflp;
428 memcpy(buf, *bc->bufp, n);
435 static ssize_t cookie_bufwrite(void *cookie, const char *buf, size_t nbytes)
437 struct bufcookie *bc = cookie;
438 int n = nbytes > 0x40000000 ? 0x40000000 : nbytes;
441 *bc->bufp = solv_extend(*bc->bufp, *bc->buflp, n + 1, 1, 4095);
442 memcpy(*bc->bufp, buf, n);
443 (*bc->bufp)[n] = 0; /* zero-terminate */
449 static int cookie_bufclose(void *cookie)
451 struct bufcookie *bc = cookie;
453 solv_free(bc->freemem);
459 solv_xfopen_buf(const char *fn, char **bufp, size_t *buflp, const char *mode)
461 struct bufcookie *bc;
463 if (*mode != 'r' && *mode != 'w')
465 bc = solv_calloc(1, sizeof(*bc));
470 bc->bufl_int = *mode == 'w' ? 0 : strlen(*bufp);
471 buflp = &bc->bufl_int;
476 *bc->bufp = solv_extend(0, 0, 1, 1, 4095); /* always zero-terminate */
480 fp = cookieopen(bc, mode, cookie_bufread, cookie_bufwrite, cookie_bufclose);
481 if (!strcmp(mode, "rf")) /* auto-free */
486 *bc->bufp = solv_free(*bc->bufp);