summaryrefslogtreecommitdiff
path: root/ext/solv_xfopen.c
diff options
context:
space:
mode:
authorMichael Schroeder <mls@suse.de>2012-03-27 11:54:21 +0200
committerMichael Schroeder <mls@suse.de>2012-03-27 11:54:21 +0200
commit68f6030cf48cd4dce0535f6e100f39be2b6d9dec (patch)
tree8f5cc8fa5288b399e85d7ef41800456f3a79c82c /ext/solv_xfopen.c
parent28d0d857fec4cc719d7418f94e250b570228ae84 (diff)
downloadlibsolv-68f6030cf48cd4dce0535f6e100f39be2b6d9dec.tar.gz
libsolv-68f6030cf48cd4dce0535f6e100f39be2b6d9dec.tar.bz2
libsolv-68f6030cf48cd4dce0535f6e100f39be2b6d9dec.zip
- clean up lzma code a bit
Diffstat (limited to 'ext/solv_xfopen.c')
-rw-r--r--ext/solv_xfopen.c46
1 files changed, 24 insertions, 22 deletions
diff --git a/ext/solv_xfopen.c b/ext/solv_xfopen.c
index 2223da7..83e6809 100644
--- a/ext/solv_xfopen.c
+++ b/ext/solv_xfopen.c
@@ -79,6 +79,8 @@ static inline FILE *mygzfdopen(int fd, const char *mode)
#include <lzma.h>
+/* lzma code written by me in 2008 for rpm's rpmio.c */
+
typedef struct lzfile {
unsigned char buf[1 << 15];
lzma_stream strm;
@@ -87,14 +89,22 @@ typedef struct lzfile {
int eof;
} LZFILE;
-static LZFILE *lzopen(const char *path, const char *mode, int fd, int xz)
+static inline lzma_ret setup_alone_encoder(lzma_stream *strm, int level)
+{
+ lzma_options_lzma options;
+ lzma_lzma_preset(&options, level);
+ return lzma_alone_encoder(strm, &options);
+}
+
+static lzma_stream stream_init = LZMA_STREAM_INIT;
+
+static LZFILE *lzopen(const char *path, const char *mode, int fd, int isxz)
{
int level = 7;
int encoding = 0;
FILE *fp;
LZFILE *lzfile;
lzma_ret ret;
- lzma_stream init_strm = LZMA_STREAM_INIT;
if (!path && fd < 0)
return 0;
@@ -122,23 +132,16 @@ static LZFILE *lzopen(const char *path, const char *mode, int fd, int xz)
lzfile->file = fp;
lzfile->encoding = encoding;
lzfile->eof = 0;
- lzfile->strm = init_strm;
+ lzfile->strm = stream_init;
if (encoding)
{
- if (xz)
+ if (isxz)
ret = lzma_easy_encoder(&lzfile->strm, level, LZMA_CHECK_SHA256);
else
- {
- lzma_options_lzma options;
- lzma_lzma_preset(&options, level);
- ret = lzma_alone_encoder(&lzfile->strm, &options);
- }
+ ret = setup_alone_encoder(&lzfile->strm, level);
}
else
- {
- /* lzma_easy_decoder_memusage(level) is not ready yet, use hardcoded limit for now */
- ret = lzma_auto_decoder(&lzfile->strm, 100 << 20, 0);
- }
+ ret = lzma_auto_decoder(&lzfile->strm, 100 << 20, 0);
if (ret != LZMA_OK)
{
fclose(fp);
@@ -292,6 +295,7 @@ solv_xfopen(const char *fn, const char *mode)
FILE *
solv_xfopen_fd(const char *fn, int fd, const char *mode)
{
+ const char *simplemode = mode;
char *suf;
suf = fn ? strrchr(fn, '.') : 0;
@@ -302,24 +306,22 @@ solv_xfopen_fd(const char *fn, int fd, const char *mode)
return 0;
fl &= O_RDONLY|O_WRONLY|O_RDWR;
if (fl == O_WRONLY)
- mode = "w";
+ mode = simplemode = "w";
else if (fl == O_RDWR)
{
- if (!suf || strcmp(suf, ".gz") != 0)
- mode = "r+";
- else
- mode = "r";
+ mode = "r+";
+ simplemode = "r";
}
else
- mode = "r";
+ mode = simplemode = "r";
}
if (suf && !strcmp(suf, ".gz"))
- return mygzfdopen(fd, mode);
+ return mygzfdopen(fd, simplemode);
#ifdef ENABLE_LZMA_COMPRESSION
if (suf && !strcmp(suf, ".xz"))
- return myxzfdopen(fd, mode);
+ return myxzfdopen(fd, simplemode);
if (suf && !strcmp(suf, ".lzma"))
- return mylzfdopen(fd, mode);
+ return mylzfdopen(fd, simplemode);
#endif
return fdopen(fd, mode);
}