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.
16 #include "zlibsupport.h"
20 #ifdef CONFIG_USE_ZLIB
23 void *grab_contents(gzFile
*gzfd
, unsigned long *size
)
25 unsigned int max
= 16384;
26 void *buffer
= NOFAIL(malloc(max
));
30 while ((ret
= gzread(gzfd
, buffer
+ *size
, max
- *size
)) > 0) {
33 buffer
= NOFAIL(realloc(buffer
, max
*= 2));
43 void *grab_fd(int fd
, unsigned long *size
)
47 gzfd
= gzdopen(fd
, "rb");
50 fatal("Memory allocation failure in gzdopen\n");
54 /* gzclose(gzfd) would close fd, which would drop locks.
55 Don't blame zlib: POSIX locking semantics are so horribly
56 broken that they should be ripped out. */
57 return grab_contents(gzfd
, size
);
60 /* gzopen handles uncompressed files transparently. */
61 void *grab_file(const char *filename
, unsigned long *size
)
67 gzfd
= gzopen(filename
, "rb");
70 fatal("Memory allocation failure in gzopen\n");
73 buffer
= grab_contents(gzfd
, size
);
78 void release_file(void *data
, unsigned long size
)
82 #else /* ... !CONFIG_USE_ZLIB */
84 void *grab_fd(int fd
, unsigned long *size
)
94 map
= mmap(0, *size
, PROT_READ
|PROT_WRITE
, MAP_PRIVATE
, fd
, 0);
95 if (map
== MAP_FAILED
)
100 void *grab_file(const char *filename
, unsigned long *size
)
105 fd
= open(filename
, O_RDONLY
, 0);
108 map
= grab_fd(fd
, size
);
113 void release_file(void *data
, unsigned long size
)