[syslinux:slzm] slzm: don't use floadfile()

syslinux-bot for H. Peter Anvin hpa at linux.intel.com
Fri Jan 18 09:18:03 PST 2013


Commit-ID:  3c0af7322087a19a2b62bb96204afc7bf93172a6
Gitweb:     http://www.syslinux.org/commit/3c0af7322087a19a2b62bb96204afc7bf93172a6
Author:     H. Peter Anvin <hpa at linux.intel.com>
AuthorDate: Fri, 18 Jan 2013 09:06:26 -0800
Committer:  H. Peter Anvin <hpa at linux.intel.com>
CommitDate: Fri, 18 Jan 2013 09:06:26 -0800

slzm: don't use floadfile()

Don't use floadfile() for loading module data.  The SLZM header
contains both the compressed and uncompressed sizes, so we might as
well just use that information to know ahead of time how much memory
to allocate.

Signed-off-by: H. Peter Anvin <hpa at linux.intel.com>

---
 com32/lib/Makefile            |  4 ++--
 com32/lib/sys/module/common.c | 46 +++++++++++++++++++++++--------------------
 2 files changed, 27 insertions(+), 23 deletions(-)

diff --git a/com32/lib/Makefile b/com32/lib/Makefile
index 7bedc22..d979ab4 100644
--- a/com32/lib/Makefile
+++ b/com32/lib/Makefile
@@ -60,7 +60,7 @@ LIBLOAD_OBJS = \
 	syslinux/run_default.o syslinux/run_command.o			\
 	syslinux/cleanup.o syslinux/localboot.o	syslinux/runimage.o	\
 	\
-	syslinux/loadfile.o syslinux/zloadfile.o	\
+	syslinux/loadfile.o syslinux/floadfile.o syslinux/zloadfile.o	\
 	\
 	syslinux/load_linux.o syslinux/initramfs.o			\
 	syslinux/initramfs_file.o syslinux/initramfs_loadfile.o		\
@@ -81,10 +81,10 @@ LIBENTRY_OBJS = \
 	dprintf.o vdprintf.o						\
 	\
 	syslinux/idle.o							\
+	\
 	exit.o
 
 LIBMODULE_OBJS = \
-	syslinux/floadfile.o 						\
 	sys/module/common.o sys/module/elf_module.o		\
 	sys/module/elfutils.o					\
 	sys/module/exec.o
diff --git a/com32/lib/sys/module/common.c b/com32/lib/sys/module/common.c
index 1daa533..7625ab7 100644
--- a/com32/lib/sys/module/common.c
+++ b/com32/lib/sys/module/common.c
@@ -110,8 +110,8 @@ again:
 int image_load(struct elf_module *module)
 {
 	void *zdata, *mdata;
-	size_t zlen, mlen;
-	const struct slzm_header *hdr;
+	size_t mlen;
+	struct slzm_header hdr;
 	FILE *f;
 	int zr;
 	int rv = -1;
@@ -124,34 +124,39 @@ int image_load(struct elf_module *module)
 	f = findpath(module->name);
 
 	if (!f) {
-		DBG_PRINT("Could not open module file '%s'\n", module->name);
+		dprintf("%s: could not open module file\n", module->name);
 		goto error;
 	}
 
-	if (floadfile(f, &zdata, &zlen, NULL, 0)) {
-		DBG_PRINT("Could not read module file '%s'\n", module->name);
+	if (_fread(&hdr, sizeof hdr, f) != sizeof hdr) {
+		dprintf("%s: could not read module header\n",
+			module->name);
 		goto error;
 	}
 
-	fclose(f);
-	f = NULL;
-
-	hdr = zdata;
-	if (zlen < sizeof *hdr) {
-		dprintf("%s: file too short\n", module->name);
+	if (hdr.magic[0] != SLZM_MAGIC1 || hdr.magic[1] != SLZM_MAGIC2 ||
+	    hdr.platform != SLZM_PLATFORM || hdr.arch != SLZM_ARCH) {
+		dprintf("%s: bad header\n", module->name);
 		goto error;
 	}
 
-	zlen -= sizeof *hdr;
+	zdata = malloc(hdr.zsize);
+	if (!zdata) {
+		dprintf("%s: failed to allocate zdata buffer\n",
+			module->name);
+		goto error;
+	}
 
-	if (hdr->magic[0] != SLZM_MAGIC1 || hdr->magic[1] != SLZM_MAGIC2 ||
-	    hdr->platform != SLZM_PLATFORM || hdr->arch != SLZM_ARCH ||
-	    zlen < hdr->zsize) {
-		dprintf("%s: bad header\n", module->name);
+	if (_fread(zdata, hdr.zsize, f) != hdr.zsize) {
+		dprintf("%s: failed to read module data\n",
+			module->name);
 		goto error;
 	}
 
-	mlen = hdr->usize + 15;
+	fclose(f);
+	f = NULL;
+
+	mlen = hdr.usize + 15;
 	mdata = malloc(mlen);
 	if (!mdata) {
 		dprintf("%s: failed to allocate mdata buffer\n",
@@ -159,17 +164,16 @@ int image_load(struct elf_module *module)
 		goto error;
 	}
 	
-	zr = lzo1x_decompress_fast_safe((const char *)zdata + sizeof *hdr,
-					hdr->zsize, mdata, &mlen, NULL);
+	zr = lzo1x_decompress_fast_safe(zdata, hdr.zsize, mdata, &mlen, NULL);
 	if (zr) {
 		dprintf("%s: decompression returned error %d\n",
 			module->name, zr);
 		goto error;
 	}
 
-	if (mlen != hdr->usize) {
+	if (mlen != hdr.usize) {
 		dprintf("%s: decompression returned %zu bytes expected %u\n",
-			module->name, mlen, hdr->usize);
+			module->name, mlen, hdr.usize);
 		goto error;
 	}
 


More information about the Syslinux-commits mailing list