mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: barebox@lists.infradead.org
Subject: [PATCH 12/16] Add generic uncompress function
Date: Mon, 28 Nov 2011 23:10:05 +0100	[thread overview]
Message-ID: <1322518209-2965-13-git-send-email-s.hauer@pengutronix.de> (raw)
In-Reply-To: <1322518209-2965-1-git-send-email-s.hauer@pengutronix.de>

uncompress() has the same prototype as the various kernel decompress
functions. It automatically detects the compression type and selects
the correct uncompress function.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 include/uncompress.h |   19 ++++++
 lib/Makefile         |    1 +
 lib/uncompress.c     |  159 ++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 179 insertions(+), 0 deletions(-)
 create mode 100644 include/uncompress.h
 create mode 100644 lib/uncompress.c

diff --git a/include/uncompress.h b/include/uncompress.h
new file mode 100644
index 0000000..d146c90
--- /dev/null
+++ b/include/uncompress.h
@@ -0,0 +1,19 @@
+#ifndef __UNCOMPRESS_H
+#define __UNCOMPRESS_H
+
+int uncompress(unsigned char *inbuf, int len,
+	   int(*fill)(void*, unsigned int),
+	   int(*flush)(void*, unsigned int),
+	   unsigned char *output,
+	   int *pos,
+	   void(*error_fn)(char *x));
+
+int uncompress_fd_to_fd(int infd, int outfd,
+	   void(*error_fn)(char *x));
+
+int uncompress_fd_to_buf(int infd, void *output,
+	   void(*error_fn)(char *x));
+
+void uncompress_err_stdout(char *);
+
+#endif /* __UNCOMPRESS_H */
diff --git a/lib/Makefile b/lib/Makefile
index f0abc21..e35d4b1 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -33,3 +33,4 @@ obj-$(CONFIG_MD5)	+= md5.o
 obj-$(CONFIG_SHA1)	+= sha1.o
 obj-$(CONFIG_SHA256)	+= sha256.o
 obj-$(CONFIG_FDT)	+= fdt/
+obj-y			+= uncompress.o
diff --git a/lib/uncompress.c b/lib/uncompress.c
new file mode 100644
index 0000000..80982f3
--- /dev/null
+++ b/lib/uncompress.c
@@ -0,0 +1,159 @@
+/*
+ * uncompress.c - uncompress files
+ *
+ * Copyright (c) 2011 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+#include <common.h>
+#include <uncompress.h>
+#include <bunzip2.h>
+#include <gunzip.h>
+#include <lzo.h>
+#include <errno.h>
+#include <filetype.h>
+#include <malloc.h>
+#include <fs.h>
+
+static void *uncompress_buf;
+static unsigned int uncompress_size;
+
+void uncompress_err_stdout(char *x)
+{
+	printf("%s\n", x);
+}
+
+static int (*uncompress_fill_fn)(void*, unsigned int);
+
+static int uncompress_fill(void *buf, unsigned int len)
+{
+	int total = 0;
+
+	if (uncompress_size) {
+		int now = min(len, uncompress_size);
+
+		memcpy(buf, uncompress_buf, now);
+		uncompress_size -= now;
+		len -= now;
+		total = now;
+		buf += now;
+	}
+
+	if (len) {
+		int ret = uncompress_fill_fn(buf, len);
+		if (ret < 0)
+			return ret;
+		total += ret;
+	}
+
+	return total;
+}
+
+int uncompress(unsigned char *inbuf, int len,
+	   int(*fill)(void*, unsigned int),
+	   int(*flush)(void*, unsigned int),
+	   unsigned char *output,
+	   int *pos,
+	   void(*error_fn)(char *x))
+{
+	enum filetype ft;
+	int (*compfn)(unsigned char *inbuf, int len,
+            int(*fill)(void*, unsigned int),
+            int(*flush)(void*, unsigned int),
+            unsigned char *output,
+            int *pos,
+            void(*error)(char *x));
+	int ret;
+	char *err;
+
+	BUG_ON(uncompress_size);
+
+	if (inbuf) {
+		ft = file_detect_type(inbuf);
+	} else {
+		if (!fill)
+			return -EINVAL;
+
+		uncompress_fill_fn = fill;
+		uncompress_buf = xzalloc(32);
+		uncompress_size = 32;
+
+		ret = fill(uncompress_buf, 32);
+		if (ret < 0)
+			goto err;
+
+		ft = file_detect_type(uncompress_buf);
+	}
+
+	switch (ft) {
+#ifdef CONFIG_BZLIB
+	case filetype_bzip2:
+		compfn = bunzip2;
+		break;
+#endif
+#ifdef CONFIG_ZLIB
+	case filetype_gzip:
+		compfn = gunzip;
+		break;
+#endif
+#ifdef CONFIG_LZO_DECOMPRESS
+	case filetype_lzo_compressed:
+		compfn = decompress_unlzo;
+		break;
+#endif
+	default:
+		err = asprintf("cannot handle filetype %s", file_type_to_string(ft));
+		error_fn(err);
+		free(err);
+		ret = -ENOSYS;
+		goto err;
+	}
+
+	ret = compfn(inbuf, len, fill ? uncompress_fill : NULL,
+			flush, output, pos, error_fn);
+err:
+	free(uncompress_buf);
+	uncompress_size = 0;
+
+	return ret;
+}
+
+static int uncompress_infd, uncompress_outfd;
+
+static int fill_fd(void *buf, unsigned int len)
+{
+	return read(uncompress_infd, buf, len);
+}
+
+static int flush_fd(void *buf, unsigned int len)
+{
+	return write(uncompress_outfd, buf, len);
+}
+
+int uncompress_fd_to_fd(int infd, int outfd,
+	   void(*error_fn)(char *x))
+{
+	uncompress_infd = infd;
+	uncompress_outfd = outfd;
+
+	return uncompress(NULL, 0,
+	   fill_fd,
+	   flush_fd,
+	   NULL,
+	   NULL,
+	   error_fn);
+}
-- 
1.7.7.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  parent reply	other threads:[~2011-11-28 22:10 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-11-28 22:09 uncompress work Sascha Hauer
2011-11-28 22:09 ` [PATCH 01/16] armlinux: remove unnecessary include Sascha Hauer
2011-11-28 22:09 ` [PATCH 02/16] arm bootm: " Sascha Hauer
2011-11-28 22:09 ` [PATCH 03/16] scripts/mkimage.c: " Sascha Hauer
2011-11-28 22:09 ` [PATCH 04/16] use kernel bunzip implementation Sascha Hauer
2011-11-28 22:09 ` [PATCH 05/16] remove old bzlib Sascha Hauer
2011-11-28 22:09 ` [PATCH 06/16] add kernel gunzip implementation Sascha Hauer
2011-11-28 22:10 ` [PATCH 07/16] remove old zlib Sascha Hauer
2011-12-03 18:47   ` Jean-Christophe PLAGNIOL-VILLARD
2011-11-28 22:10 ` [PATCH 08/16] lib: prompt for uncompression functions Sascha Hauer
2011-11-28 22:10 ` [PATCH 09/16] bootm: do not select uncompression methods Sascha Hauer
2011-11-28 22:10 ` [PATCH 10/16] add file detection support Sascha Hauer
2011-11-28 23:10   ` Marc Kleine-Budde
2011-11-29 19:41     ` Sascha Hauer
2011-11-29 19:40   ` Sascha Hauer
2011-11-28 22:10 ` [PATCH 11/16] lzo: export decompress_unlzo function Sascha Hauer
2011-11-28 22:10 ` Sascha Hauer [this message]
2011-11-28 22:10 ` [PATCH 13/16] add generic uncompress command Sascha Hauer
2011-11-28 22:10 ` [PATCH 14/16] update configs and default envs for uncompress Sascha Hauer
2011-11-28 22:10 ` [PATCH 15/16] remove now unused unlzo function Sascha Hauer
2011-11-28 22:10 ` [PATCH 16/16] bootm: use generic uncompress function Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1322518209-2965-13-git-send-email-s.hauer@pengutronix.de \
    --to=s.hauer@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox