summaryrefslogtreecommitdiffstats
path: root/mdk-stage1/zlibsupport.c
diff options
context:
space:
mode:
authorPascal Rigaux <pixel@mandriva.com>2007-04-25 12:26:16 +0000
committerPascal Rigaux <pixel@mandriva.com>2007-04-25 12:26:16 +0000
commit126777bc019a54afb4ec51299f2cf9d2841698aa (patch)
tree97f76e571902ead55ba138f1156a4b4f00b9b779 /mdk-stage1/zlibsupport.c
parentf1f67448efc714873378dfeb8279fae68054a90a (diff)
downloaddrakx-126777bc019a54afb4ec51299f2cf9d2841698aa.tar
drakx-126777bc019a54afb4ec51299f2cf9d2841698aa.tar.gz
drakx-126777bc019a54afb4ec51299f2cf9d2841698aa.tar.bz2
drakx-126777bc019a54afb4ec51299f2cf9d2841698aa.tar.xz
drakx-126777bc019a54afb4ec51299f2cf9d2841698aa.zip
re-sync after the big svn loss
Diffstat (limited to 'mdk-stage1/zlibsupport.c')
-rw-r--r--mdk-stage1/zlibsupport.c119
1 files changed, 119 insertions, 0 deletions
diff --git a/mdk-stage1/zlibsupport.c b/mdk-stage1/zlibsupport.c
new file mode 100644
index 000000000..ecbe7a5ef
--- /dev/null
+++ b/mdk-stage1/zlibsupport.c
@@ -0,0 +1,119 @@
+/* Support for compressed modules. Willy Tarreau <willy@meta-x.org>
+ * did the support for modutils, Andrey Borzenkov <arvidjaar@mail.ru>
+ * ported it to module-init-tools, and I said it was too ugly to live
+ * and rewrote it 8).
+ *
+ * (C) 2003 Rusty Russell, IBM Corporation.
+ */
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/mman.h>
+
+#include "zlibsupport.h"
+
+#ifdef CONFIG_USE_ZLIB
+#include <zlib.h>
+
+void *grab_contents(gzFile *gzfd, unsigned long *size)
+{
+ unsigned int max = 16384;
+ void *buffer = malloc(max);
+ int ret;
+
+ if (!buffer)
+ return NULL;
+
+ *size = 0;
+ while ((ret = gzread(gzfd, buffer + *size, max - *size)) > 0) {
+ *size += ret;
+ if (*size == max) {
+ void *p;
+
+ p = realloc(buffer, max *= 2);
+ if (!p)
+ goto out_err;
+
+ buffer = p;
+ }
+ }
+ if (ret < 0)
+ goto out_err;
+
+ return buffer;
+
+out_err:
+ free(buffer);
+ return NULL;
+}
+
+void *grab_fd(int fd, unsigned long *size)
+{
+ gzFile gzfd;
+
+ gzfd = gzdopen(fd, "rb");
+ if (!gzfd)
+ return NULL;
+
+ /* gzclose(gzfd) would close fd, which would drop locks.
+ Don't blame zlib: POSIX locking semantics are so horribly
+ broken that they should be ripped out. */
+ return grab_contents(gzfd, size);
+}
+
+/* gzopen handles uncompressed files transparently. */
+void *grab_file(const char *filename, unsigned long *size)
+{
+ gzFile gzfd;
+ void *buffer;
+
+ gzfd = gzopen(filename, "rb");
+ if (!gzfd)
+ return NULL;
+ buffer = grab_contents(gzfd, size);
+ gzclose(gzfd);
+ return buffer;
+}
+
+void release_file(void *data, unsigned long size)
+{
+ free(data);
+}
+#else /* ... !CONFIG_USE_ZLIB */
+
+void *grab_fd(int fd, unsigned long *size)
+{
+ struct stat st;
+ void *map;
+ int ret;
+
+ ret = fstat(fd, &st);
+ if (ret < 0)
+ return NULL;
+ *size = st.st_size;
+ map = mmap(0, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
+ if (map == MAP_FAILED)
+ map = NULL;
+ return map;
+}
+
+void *grab_file(const char *filename, unsigned long *size)
+{
+ int fd;
+ void *map;
+
+ fd = open(filename, O_RDONLY, 0);
+ if (fd < 0)
+ return NULL;
+ map = grab_fd(fd, size);
+ close(fd);
+ return map;
+}
+
+void release_file(void *data, unsigned long size)
+{
+ munmap(data, size);
+}
+#endif