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);
79 #ifdef ENABLE_LZMA_COMPRESSION
83 /* lzma code written by me in 2008 for rpm's rpmio.c */
85 typedef struct lzfile {
86 unsigned char buf[1 << 15];
93 static inline lzma_ret setup_alone_encoder(lzma_stream *strm, int level)
95 lzma_options_lzma options;
96 lzma_lzma_preset(&options, level);
97 return lzma_alone_encoder(strm, &options);
100 static lzma_stream stream_init = LZMA_STREAM_INIT;
102 static LZFILE *lzopen(const char *path, const char *mode, int fd, int isxz)
112 for (; *mode; mode++)
116 else if (*mode == 'r')
118 else if (*mode >= '1' && *mode <= '9')
122 fp = fdopen(fd, encoding ? "w" : "r");
124 fp = fopen(path, encoding ? "w" : "r");
127 lzfile = calloc(1, sizeof(*lzfile));
134 lzfile->encoding = encoding;
136 lzfile->strm = stream_init;
140 ret = lzma_easy_encoder(&lzfile->strm, level, LZMA_CHECK_SHA256);
142 ret = setup_alone_encoder(&lzfile->strm, level);
145 ret = lzma_auto_decoder(&lzfile->strm, 100 << 20, 0);
155 static int lzclose(void *cookie)
157 LZFILE *lzfile = cookie;
164 if (lzfile->encoding)
168 lzfile->strm.avail_out = sizeof(lzfile->buf);
169 lzfile->strm.next_out = lzfile->buf;
170 ret = lzma_code(&lzfile->strm, LZMA_FINISH);
171 if (ret != LZMA_OK && ret != LZMA_STREAM_END)
173 n = sizeof(lzfile->buf) - lzfile->strm.avail_out;
174 if (n && fwrite(lzfile->buf, 1, n, lzfile->file) != n)
176 if (ret == LZMA_STREAM_END)
180 lzma_end(&lzfile->strm);
181 rc = fclose(lzfile->file);
186 static ssize_t lzread(void *cookie, char *buf, size_t len)
188 LZFILE *lzfile = cookie;
192 if (!lzfile || lzfile->encoding)
196 lzfile->strm.next_out = (unsigned char *)buf;
197 lzfile->strm.avail_out = len;
200 if (!lzfile->strm.avail_in)
202 lzfile->strm.next_in = lzfile->buf;
203 lzfile->strm.avail_in = fread(lzfile->buf, 1, sizeof(lzfile->buf), lzfile->file);
204 if (!lzfile->strm.avail_in)
207 ret = lzma_code(&lzfile->strm, LZMA_RUN);
208 if (ret == LZMA_STREAM_END)
211 return len - lzfile->strm.avail_out;
215 if (!lzfile->strm.avail_out)
222 static ssize_t lzwrite(void *cookie, const char *buf, size_t len)
224 LZFILE *lzfile = cookie;
227 if (!lzfile || !lzfile->encoding)
231 lzfile->strm.next_in = (unsigned char *)buf;
232 lzfile->strm.avail_in = len;
235 lzfile->strm.next_out = lzfile->buf;
236 lzfile->strm.avail_out = sizeof(lzfile->buf);
237 ret = lzma_code(&lzfile->strm, LZMA_RUN);
240 n = sizeof(lzfile->buf) - lzfile->strm.avail_out;
241 if (n && fwrite(lzfile->buf, 1, n, lzfile->file) != n)
243 if (!lzfile->strm.avail_in)
248 static inline FILE *myxzfopen(const char *fn, const char *mode)
250 LZFILE *lzf = lzopen(fn, mode, -1, 1);
251 return cookieopen(lzf, mode, lzread, lzwrite, lzclose);
254 static inline FILE *myxzfdopen(int fd, const char *mode)
256 LZFILE *lzf = lzopen(0, mode, fd, 1);
257 return cookieopen(lzf, mode, lzread, lzwrite, lzclose);
260 static inline FILE *mylzfopen(const char *fn, const char *mode)
262 LZFILE *lzf = lzopen(fn, mode, -1, 0);
263 return cookieopen(lzf, mode, lzread, lzwrite, lzclose);
266 static inline FILE *mylzfdopen(int fd, const char *mode)
268 LZFILE *lzf = lzopen(0, mode, fd, 0);
269 return cookieopen(lzf, mode, lzread, lzwrite, lzclose);
272 #endif /* ENABLE_LZMA_COMPRESSION */
276 solv_xfopen(const char *fn, const char *mode)
284 suf = strrchr(fn, '.');
285 if (suf && !strcmp(suf, ".gz"))
286 return mygzfopen(fn, mode);
287 #ifdef ENABLE_LZMA_COMPRESSION
288 if (suf && !strcmp(suf, ".xz"))
289 return myxzfopen(fn, mode);
290 if (suf && !strcmp(suf, ".lzma"))
291 return mylzfopen(fn, mode);
293 return fopen(fn, mode);
297 solv_xfopen_fd(const char *fn, int fd, const char *mode)
299 const char *simplemode = mode;
302 suf = fn ? strrchr(fn, '.') : 0;
305 int fl = fcntl(fd, F_GETFL, 0);
308 fl &= O_RDONLY|O_WRONLY|O_RDWR;
310 mode = simplemode = "w";
311 else if (fl == O_RDWR)
317 mode = simplemode = "r";
319 if (suf && !strcmp(suf, ".gz"))
320 return mygzfdopen(fd, simplemode);
321 #ifdef ENABLE_LZMA_COMPRESSION
322 if (suf && !strcmp(suf, ".xz"))
323 return myxzfdopen(fd, simplemode);
324 if (suf && !strcmp(suf, ".lzma"))
325 return mylzfdopen(fd, simplemode);
327 return fdopen(fd, mode);
337 static ssize_t cookie_bufread(void *cookie, char *buf, size_t nbytes)
339 struct bufcookie *bc = cookie;
340 size_t n = *bc->buflp > nbytes ? nbytes : *bc->buflp;
343 memcpy(buf, *bc->bufp, n);
350 static ssize_t cookie_bufwrite(void *cookie, const char *buf, size_t nbytes)
352 struct bufcookie *bc = cookie;
353 int n = nbytes > 0x40000000 ? 0x40000000 : nbytes;
356 *bc->bufp = solv_extend(*bc->bufp, *bc->buflp, n + 1, 1, 4095);
357 memcpy(*bc->bufp, buf, n);
358 (*bc->bufp)[n] = 0; /* zero-terminate */
364 static int cookie_bufclose(void *cookie)
366 struct bufcookie *bc = cookie;
368 solv_free(bc->freemem);
374 solv_xfopen_buf(const char *fn, char **bufp, size_t *buflp, const char *mode)
376 struct bufcookie *bc;
378 if (*mode != 'r' && *mode != 'w')
380 bc = solv_calloc(1, sizeof(*bc));
385 bc->bufl_int = *mode == 'w' ? 0 : strlen(*bufp);
386 buflp = &bc->bufl_int;
391 *bc->bufp = solv_extend(0, 0, 1, 1, 4095); /* always zero-terminate */
395 fp = cookieopen(bc, mode, cookie_bufread, cookie_bufwrite, cookie_bufclose);
396 if (!strcmp(mode, "rf")) /* auto-free */
400 *bc->bufp = solv_free(*bc->bufp);