[PATCH 3/5] zlibsupport: {grab,release}_file are wrappers for functions that are specific to each type of compression

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Signed-off-by: Alexey Gladkov <gladkov.alexey@xxxxxxxxx>
---
 zlibsupport.c |   61 +++++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 55 insertions(+), 6 deletions(-)

diff --git a/zlibsupport.c b/zlibsupport.c
index 8936c7f..bff371a 100644
--- a/zlibsupport.c
+++ b/zlibsupport.c
@@ -43,7 +43,7 @@ static int grab_contents(gzFile *gzfd, struct grab_data *fdata)
 }
 
 /* gzopen handles uncompressed files transparently. */
-int grab_file(const char *filename, struct grab_data *fdata)
+static int gzip_grab_file(const char *filename, struct grab_data *fdata)
 {
 	gzFile gzfd;
 
@@ -63,13 +63,13 @@ int grab_file(const char *filename, struct grab_data *fdata)
 	return 0;
 }
 
-void release_file(struct grab_data *fdata)
+static void gzip_release_file(struct grab_data *fdata)
 {
 	free(fdata->data);
 }
-#else /* ... !CONFIG_USE_ZLIB */
+#endif
 
-static void *grab_fd(int fd, struct grab_data *fdata)
+static int grab_fd(int fd, struct grab_data *fdata)
 {
 	struct stat st;
 	int ret;
@@ -84,7 +84,7 @@ static void *grab_fd(int fd, struct grab_data *fdata)
 	return 0;
 }
 
-int grab_file(const char *filename, struct grab_data *fdata)
+static int plain_grab_file(const char *filename, struct grab_data *fdata)
 {
 	int fd;
 
@@ -98,8 +98,57 @@ int grab_file(const char *filename, struct grab_data *fdata)
 	return 0;
 }
 
-void release_file(struct grab_data *fdata)
+static void plain_release_file(struct grab_data *fdata)
 {
 	munmap(fdata->data, fdata->size);
 }
+
+static int compress_type(const char *filename)
+{
+	int fd;
+	unsigned char buf[6];
+
+	if ((fd = open(filename, O_RDONLY)) == -1)
+		return -1;
+
+	if (read(fd, buf, sizeof(buf)) == -1) {
+		close(fd);
+		return -1;
+	}
+
+	close(fd);
+
+	if (buf[0] == 0x1F && buf[1] == 0x8B)
+		return GZIP_FILE;
+
+	return PLAIN_FILE;
+}
+
+int grab_file(const char *filename, struct grab_data *fdata)
+{
+	switch (compress_type(filename)) {
+#ifdef CONFIG_USE_ZLIB
+		case GZIP_FILE:
+			return gzip_grab_file(filename, fdata);
 #endif
+		case PLAIN_FILE:
+			return plain_grab_file(filename, fdata);
+	}
+	fatal("Unknown compression type\n");
+	return -1;
+}
+
+void release_file(struct grab_data *fdata)
+{
+	switch (fdata->type) {
+#ifdef CONFIG_USE_ZLIB
+		case GZIP_FILE:
+			gzip_release_file(fdata);
+			return;
+#endif
+		case PLAIN_FILE:
+			plain_release_file(fdata);
+			return;
+	}
+	fatal("Unknown compression type\n");
+}
-- 
1.7.3.5

--
To unsubscribe from this list: send the line "unsubscribe linux-modules" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [Big List of Linux Books]

  Powered by Linux