summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDongHun Kwak <dh0128.kwak@samsung.com>2018-04-24 13:37:11 +0900
committerDongHun Kwak <dh0128.kwak@samsung.com>2018-04-24 13:37:14 +0900
commit3fa6014dff6063ae5134a839292c7cbbb8ba855b (patch)
tree66457131676c7c156638faff8b5aefab1fff8ea9
parent545caac6a594c26ece430c1742c1ddb9bf8ef570 (diff)
downloadlibzio-3fa6014dff6063ae5134a839292c7cbbb8ba855b.tar.gz
libzio-3fa6014dff6063ae5134a839292c7cbbb8ba855b.tar.bz2
libzio-3fa6014dff6063ae5134a839292c7cbbb8ba855b.zip
Imported Upstream version 1.06upstream/1.06
Change-Id: Iffff0686f8efff3c37c66194dab7ea27db229f49 Signed-off-by: DongHun Kwak <dh0128.kwak@samsung.com>
-rw-r--r--Makefile6
-rw-r--r--tests.c2
-rw-r--r--testx.c28
-rw-r--r--zio.c137
4 files changed, 106 insertions, 67 deletions
diff --git a/Makefile b/Makefile
index cca22c0..f8f8c43 100644
--- a/Makefile
+++ b/Makefile
@@ -8,7 +8,7 @@ LARGE = $(shell getconf LFS_CFLAGS)
CFLAGS = $(RPM_OPT_FLAGS) -pipe -Wall -D_GNU_SOURCE -D_REENTRANT -D_DEFAULT_SOURCE $(LARGE)
CC = gcc
MAJOR = 1
-MINOR = 05
+MINOR = 06
VERSION = $(MAJOR).$(MINOR)
SONAME = libzio.so.$(MAJOR)
LDMAP = -Wl,--version-script=zio.map
@@ -27,6 +27,7 @@ FILES = README \
zio.h.in \
testt.c \
tests.c \
+ testx.c \
lzw.h \
unlzw.c \
zio.map \
@@ -131,3 +132,6 @@ testt: testt.c libzio.a
tests: tests.c libzio.a
$(CC) $(CFLAGS) -o $@ $^ $(LIBS)
+
+testx: testx.c libzio.a
+ $(CC) $(CFLAGS) -o $@ $^ $(LIBS)
diff --git a/tests.c b/tests.c
index 7512573..8577a1b 100644
--- a/tests.c
+++ b/tests.c
@@ -7,7 +7,7 @@
int main(int argc, char *argv[])
{
FILE *file;
- char line[1024];
+ char line[1<<13];
size_t len;
if (!(file = fdzopen(fileno(stdin), "r", argc > 1 ? argv[1] : "g"))) {
diff --git a/testx.c b/testx.c
new file mode 100644
index 0000000..d705e4c
--- /dev/null
+++ b/testx.c
@@ -0,0 +1,28 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+#include "zio.h"
+
+int main(int argc, char *argv[])
+{
+ FILE *file;
+ char line[1<<13];
+ size_t len;
+
+ if (!(file = fdzopen(fileno(stdout), "w", argc > 1 ? argv[1] : "g"))) {
+ fprintf(stderr, "%s\n", strerror(errno));
+ return 1;
+ }
+
+ while ((len = fread(line, sizeof(char), sizeof (line), stdin))) {
+ size_t ret = fwrite(line, sizeof(char), len, file);
+ if ((ret != len) && ferror(stdout)) {
+ clearerr(stdout);
+ }
+ }
+
+ fclose(file);
+
+ return 0;
+}
diff --git a/zio.c b/zio.c
index 95453ef..be27da5 100644
--- a/zio.c
+++ b/zio.c
@@ -70,10 +70,10 @@ static int zclose(void *cookie)
__extension__
static cookie_io_functions_t ioz = {
- .read = (cookie_read_function_t*) &zread,
- .write = (cookie_write_function_t*)&zwrite,
- .seek = (cookie_seek_function_t*) &zseek,
- .close = (cookie_close_function_t*)&zclose,
+ .read = (cookie_read_function_t*) zread,
+ .write = (cookie_write_function_t*)zwrite,
+ .seek = (cookie_seek_function_t*) zseek,
+ .close = (cookie_close_function_t*)zclose,
};
#else /* !HAS_ZLIB_H */
# error No support for `.gz' nor `.z' format
@@ -234,10 +234,10 @@ out:
__extension__
static cookie_io_functions_t iobz = {
- .read = (cookie_read_function_t*) &bzread,
- .write = (cookie_write_function_t*)&bzwrite,
- .seek = (cookie_seek_function_t*) &bzseek,
- .close = (cookie_close_function_t*)&bzclose,
+ .read = (cookie_read_function_t*) bzread,
+ .write = (cookie_write_function_t*)bzwrite,
+ .seek = (cookie_seek_function_t*) bzseek,
+ .close = (cookie_close_function_t*)bzclose,
};
#else /* !HAS_BZLIB_H */
# warning No support for .bz2 format
@@ -259,8 +259,10 @@ typedef struct lzfile_s {
int eof;
} lzfile_t;
-static lzma_ret lzmaopen(lzma_stream *__restrict strm, const char mode, const char what, int level)
+static lzma_ret lzmaopen(void *cookie, const char mode, const char what, int level)
{
+ lzfile_t *lzma = (lzfile_t*)cookie;
+ lzma_stream *strm = &lzma->strm;
lzma_ret ret;
if (mode == 'w') {
if (what == 'x')
@@ -270,8 +272,13 @@ static lzma_ret lzmaopen(lzma_stream *__restrict strm, const char mode, const ch
lzma_lzma_preset(&opt, level);
ret = lzma_alone_encoder(strm, &opt);
}
- } else
+ strm->next_out = lzma->buf;
+ strm->avail_out = sizeof(lzma->buf);
+ } else {
ret = lzma_auto_decoder(strm, 100<<20, 0);
+ strm->next_in = lzma->buf;
+ strm->avail_in = 0;
+ }
return ret;
}
@@ -291,7 +298,6 @@ static ssize_t lzmaread(void *cookie, char *buf, size_t count)
while (1) {
lzma_ret lret;
if (!strm->avail_in) {
- strm->next_in = lzma->buf;
strm->avail_in = fread(lzma->buf, 1, sizeof(lzma->buf), lzma->file);
if (strm->avail_in == 0)
eof = 1;
@@ -330,20 +336,19 @@ static ssize_t lzmawrite(void *cookie, const char *buf, size_t count)
strm->avail_in = count;
while (1) {
lzma_ret lret;
- size_t len;
- strm->next_out = lzma->buf;
- strm->avail_out = sizeof(lzma->buf);
lret = lzma_code(strm, LZMA_RUN);
if (lret != LZMA_OK)
break;
- len = sizeof(lzma->buf) - strm->avail_out;
- if (len && fwrite(lzma->buf, 1, len, lzma->file) != len)
- break;
- if (strm->avail_in == 0) {
- if (len > 0 && len < count)
- len = count;
- return len;
+ if (strm->avail_out == 0) {
+ size_t len;
+ len = sizeof(lzma->buf);
+ if (len && fwrite(lzma->buf, 1, len, lzma->file) != len)
+ break;
+ strm->next_out = lzma->buf;
+ strm->avail_out = sizeof(lzma->buf);
}
+ if (strm->avail_in == 0)
+ return count;
}
return -1;
}
@@ -443,15 +448,17 @@ static int lzmaclose(void *cookie)
goto out;
strm = &lzma->strm;
while (1) {
- size_t len;
- strm->avail_out = sizeof(lzma->buf);
- strm->next_out = (uint8_t*)lzma->buf;
lret = lzma_code(strm, LZMA_FINISH);
if (lret != LZMA_OK && lret != LZMA_STREAM_END)
goto out;
- len = sizeof(lzma->buf) - strm->avail_out;
- if (len && fwrite(lzma->buf, 1, len, lzma->file) != len)
- goto out;
+ if (strm->avail_out == 0 || lret == LZMA_STREAM_END) {
+ size_t len;
+ len = sizeof(lzma->buf) - strm->avail_out;
+ if (len && fwrite(lzma->buf, 1, len, lzma->file) != len)
+ goto out;
+ strm->next_out = lzma->buf;
+ strm->avail_out = sizeof(lzma->buf);
+ }
if (lret == LZMA_STREAM_END)
break;
}
@@ -466,10 +473,10 @@ out:
__extension__
static cookie_io_functions_t iolzma = {
- .read = (cookie_read_function_t*) &lzmaread,
- .write = (cookie_write_function_t*)&lzmawrite,
- .seek = (cookie_seek_function_t*) &lzmaseek,
- .close = (cookie_close_function_t*)&lzmaclose,
+ .read = (cookie_read_function_t*) lzmaread,
+ .write = (cookie_write_function_t*)lzmawrite,
+ .seek = (cookie_seek_function_t*) lzmaseek,
+ .close = (cookie_close_function_t*)lzmaclose,
};
#else /* !HAS_LZMA_H */
# if defined(HAS_LZMADEC_H)
@@ -509,10 +516,10 @@ static int lzmaclose(void *cookie)
__extension__
static cookie_io_functions_t iolzma = {
- .read = (cookie_read_function_t*) &lzmaread,
- .write = (cookie_write_function_t*)&lzmawrite,
- .seek = (cookie_seek_function_t*) &lzmaseek,
- .close = (cookie_close_function_t*)&lzmaclose,
+ .read = (cookie_read_function_t*) lzmaread,
+ .write = (cookie_write_function_t*)lzmawrite,
+ .seek = (cookie_seek_function_t*) lzmaseek,
+ .close = (cookie_close_function_t*)lzmaclose,
};
# else /* !HAS_LZMADEC_H */
# warning No support for .lzma format
@@ -651,10 +658,10 @@ static int lzwclose(void *cookie)
__extension__
static cookie_io_functions_t iolzw = {
- .read = (cookie_read_function_t*) &lzwread,
- .write = (cookie_write_function_t*)&lzwwrite,
- .seek = (cookie_seek_function_t*) &lzwseek,
- .close = (cookie_close_function_t*)&lzwclose,
+ .read = (cookie_read_function_t*) lzwread,
+ .write = (cookie_write_function_t*)lzwwrite,
+ .seek = (cookie_seek_function_t*) lzwseek,
+ .close = (cookie_close_function_t*)lzwclose,
};
static inline char autodetect(char **__restrict path, const char *__restrict check)
@@ -799,12 +806,12 @@ FILE * fzopen(const char * path, const char * mode)
#if defined(HAS_ZLIB_H)
{
gzFile cookie;
-
- if (&gzopen == NULL) {
+# ifndef NO_WEAK
+ if ((gzopen) == 0) {
errno = ENOSYS;
goto out;
}
-
+# endif
if (!(cookie = gzopen(ext, mode))) {
if (!errno)
errno = ENOMEM;
@@ -868,12 +875,12 @@ FILE * fzopen(const char * path, const char * mode)
{
bzfile_t *__restrict cookie;
int level = 5;
-
- if (&BZ2_bzopen == NULL) {
+# ifndef NO_WEAK
+ if ((BZ2_bzopen) == 0) {
errno = ENOSYS;
goto out;
}
-
+# endif
if (posix_memalign((void*)&cookie, sizeof(void*), alignof(bzfile_t)+strsize(ext)+strsize(mode)) != 0)
goto out;
memset(cookie, 0, alignof(bzfile_t)+strsize(ext)+strsize(mode));
@@ -923,12 +930,12 @@ FILE * fzopen(const char * path, const char * mode)
int level = LZMA_PRESET_DEFAULT;
lzfile_t *__restrict cookie;
lzma_ret lret;
-
- if (&lzma_auto_decoder == NULL) {
+# ifndef NO_WEAK
+ if ((lzma_auto_decoder) == 0) {
errno = ENOSYS;
goto out;
}
-
+# endif
if (posix_memalign((void*)&cookie, sizeof(void*), alignof(lzfile_t)) != 0)
goto out;
memset(cookie, 0, alignof(lzfile_t));
@@ -950,7 +957,7 @@ FILE * fzopen(const char * path, const char * mode)
cookie->strm = (lzma_stream)LZMA_STREAM_INIT;
cookie->level = level;
cookie->encoding = (check[0] == 'w') ? 1 : 0;
- lret = lzmaopen(&cookie->strm, check[0], what, level);
+ lret = lzmaopen(cookie, check[0], what, level);
if (lret != LZMA_OK) {
fclose(cookie->file);
@@ -985,12 +992,12 @@ FILE * fzopen(const char * path, const char * mode)
errno = ENOTSUP;
goto out;
}
-
- if (&lzmadec_open == NULL) {
+# ifndef NO_WEAK
+ if ((lzmadec_open) == 0) {
errno = ENOSYS;
goto out;
}
-
+# endif
if (!(cookie = lzmadec_open(ext))) {
if (!errno)
errno = ENOMEM;
@@ -1068,12 +1075,12 @@ FILE * fdzopen(int fildes, const char * mode, const char *what)
#if defined(HAS_ZLIB_H)
{
gzFile cookie;
-
- if (&gzdopen == NULL) {
+# ifndef NO_WEAK
+ if ((gzdopen) == 0) {
errno = ENOSYS;
goto out;
}
-
+# endif
if (!(cookie = gzdopen(fildes, mode))) {
if (!errno)
errno = ENOMEM;
@@ -1135,12 +1142,12 @@ FILE * fdzopen(int fildes, const char * mode, const char *what)
{
bzfile_t *__restrict cookie;
int level = 5;
-
- if (&BZ2_bzdopen == NULL) {
+# ifndef NO_WEAK
+ if ((BZ2_bzdopen) == 0) {
errno = ENOSYS;
goto out;
}
-
+# endif
if (posix_memalign((void*)&cookie, sizeof(void*), alignof(bzfile_t)+strsize(mode)) != 0)
goto out;
memset(cookie, 0, alignof(bzfile_t)+strsize(mode));
@@ -1192,12 +1199,12 @@ FILE * fdzopen(int fildes, const char * mode, const char *what)
int level = LZMA_PRESET_DEFAULT;
lzfile_t *__restrict cookie;
lzma_ret lret;
-
- if (&lzma_auto_decoder == NULL) {
+# ifndef NO_WEAK
+ if ((lzma_auto_decoder) == 0) {
errno = ENOSYS;
goto out;
}
-
+#endif
if (posix_memalign((void*)&cookie, sizeof(void*), alignof(lzfile_t)) != 0)
goto out;
memset(cookie, 0, alignof(lzfile_t));
@@ -1219,7 +1226,7 @@ FILE * fdzopen(int fildes, const char * mode, const char *what)
cookie->strm = (lzma_stream)LZMA_STREAM_INIT;
cookie->level = level;
cookie->encoding = (check[0] == 'w') ? 1 : 0;
- lret = lzmaopen(&cookie->strm, check[0], *what, level);
+ lret = lzmaopen(cookie, check[0], *what, level);
if (lret != LZMA_OK) {
fclose(cookie->file);
@@ -1254,12 +1261,12 @@ FILE * fdzopen(int fildes, const char * mode, const char *what)
errno = ENOTSUP;
goto out;
}
-
- if (&lzmadec_open == NULL) {
+# ifndef NO_WEAK
+ if ((lzmadec_open) == 0) {
errno = ENOSYS;
goto out;
}
-
+# endif
if (!(cookie = lzmadec_dopen(fildes))) {
if (!errno)
errno = ENOMEM;