1 /* Support for compressed modules. Willy Tarreau <willy@meta-x.org>
2 * did the support for modutils, Andrey Borzenkov <arvidjaar@mail.ru>
3 * ported it to module-init-tools, and I said it was too ugly to live
6 * (C) 2003 Rusty Russell, IBM Corporation.
15 #include "zlibsupport.h"
18 #ifdef CONFIG_USE_ZLIB
21 void *grab_contents(gzFile
*gzfd
, unsigned long *size
)
23 unsigned int max
= 16384;
24 void *buffer
= malloc(max
);
31 while ((ret
= gzread(gzfd
, buffer
+ *size
, max
- *size
)) > 0) {
36 p
= realloc(buffer
, max
*= 2);
53 void *grab_fd(int fd
, unsigned long *size
)
57 gzfd
= gzdopen(fd
, "rb");
61 /* gzclose(gzfd) would close fd, which would drop locks.
62 Don't blame zlib: POSIX locking semantics are so horribly
63 broken that they should be ripped out. */
64 return grab_contents(gzfd
, size
);
67 /* gzopen handles uncompressed files transparently. */
68 void *grab_file(const char *filename
, unsigned long *size
)
73 gzfd
= gzopen(filename
, "rb");
76 buffer
= grab_contents(gzfd
, size
);
81 void release_file(void *data
, unsigned long size
)
85 #else /* ... !CONFIG_USE_ZLIB */
87 void *grab_fd(int fd
, unsigned long *size
)
97 map
= mmap(0, *size
, PROT_READ
|PROT_WRITE
, MAP_PRIVATE
, fd
, 0);
98 if (map
== MAP_FAILED
)
103 void *grab_file(const char *filename
, unsigned long *size
)
108 fd
= open(filename
, O_RDONLY
, 0);
111 map
= grab_fd(fd
, size
);
116 void release_file(void *data
, unsigned long size
)