All of lore.kernel.org
 help / color / mirror / Atom feed
From: Lei Wen <adrian.wenl@gmail.com>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH V2 5/6] lib: add gzip lib function callback
Date: Fri, 28 Sep 2012 07:26:46 -0700	[thread overview]
Message-ID: <1348842407-20355-6-git-send-email-leiwen@marvell.com> (raw)
In-Reply-To: <1332690817-31759-1-git-send-email-adrian.wenl@gmail.com>

Signed-off-by: Lei Wen <leiwen@marvell.com>
---
Changelog:
No change

 include/common.h |    7 +++
 lib/Makefile     |    1 +
 lib/gzip.c       |  142 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 150 insertions(+), 0 deletions(-)
 create mode 100644 lib/gzip.c

diff --git a/include/common.h b/include/common.h
index 55025c0..a7fb05e 100644
--- a/include/common.h
+++ b/include/common.h
@@ -829,6 +829,13 @@ void	fputc(int file, const char c);
 int	ftstc(int file);
 int	fgetc(int file);
 
+/* lib/gzip.c */
+int gzip(void *dst, unsigned long *lenp,
+		unsigned char *src, unsigned long srclen);
+int zzip(void *dst, unsigned long *lenp, unsigned char *src,
+		unsigned long srclen, int stoponerr,
+		int (*func)(unsigned long, unsigned long));
+
 /* lib/net_utils.c */
 #include <net.h>
 static inline IPaddr_t getenv_IPaddr(char *var)
diff --git a/lib/Makefile b/lib/Makefile
index c60c380..45798de 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -42,6 +42,7 @@ COBJS-y += errno.o
 COBJS-$(CONFIG_OF_CONTROL) += fdtdec.o
 COBJS-$(CONFIG_TEST_FDTDEC) += fdtdec_test.o
 COBJS-$(CONFIG_GZIP) += gunzip.o
+COBJS-$(CONFIG_GZIP_COMPRESSED) += gzip.o
 COBJS-y += hashtable.o
 COBJS-$(CONFIG_LMB) += lmb.o
 COBJS-y += ldiv.o
diff --git a/lib/gzip.c b/lib/gzip.c
new file mode 100644
index 0000000..a83f4af
--- /dev/null
+++ b/lib/gzip.c
@@ -0,0 +1,142 @@
+/*
+ * (C) Copyright 2012
+ * Lei Wen <leiwen@marvell.com>, Marvell Inc.
+ *
+ * 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 as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * 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 <watchdog.h>
+#include <command.h>
+#include <image.h>
+#include <malloc.h>
+#include <u-boot/zlib.h>
+#include "zlib/zutil.h"
+
+#ifndef CONFIG_GZIP_COMPRESS_DEF_SZ
+#define CONFIG_GZIP_COMPRESS_DEF_SZ	0x200
+#endif
+#define ZALLOC_ALIGNMENT		16
+
+static void *zalloc(void *x, unsigned items, unsigned size)
+{
+	void *p;
+
+	size *= items;
+	size = (size + ZALLOC_ALIGNMENT - 1) & ~(ZALLOC_ALIGNMENT - 1);
+
+	p = malloc (size);
+
+	return (p);
+}
+
+static void zfree(void *x, void *addr, unsigned nb)
+{
+	free (addr);
+}
+
+int gzip(void *dst, unsigned long *lenp,
+		unsigned char *src, unsigned long srclen)
+{
+	return zzip(dst, lenp, src, srclen, 1, NULL);
+}
+
+/*
+ * Compress blocks with zlib
+ */
+int zzip(void *dst, unsigned long *lenp, unsigned char *src,
+		unsigned long srclen, int stoponerr,
+		int (*func)(unsigned long, unsigned long))
+{
+	z_stream s;
+	int r, flush, orig, window;
+	unsigned long comp_len, left_len;
+
+	if (!srclen)
+		return 0;
+
+#ifndef CONFIG_GZIP
+	window = MAX_WBITS;
+#else
+	window = 2 * MAX_WBITS;
+#endif
+	orig = *lenp;
+	s.zalloc = zalloc;
+	s.zfree = zfree;
+	s.opaque = Z_NULL;
+
+	r = deflateInit2_(&s, Z_BEST_SPEED, Z_DEFLATED,	window,
+			DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
+			ZLIB_VERSION, sizeof(z_stream));
+	if (r != Z_OK) {
+		printf ("Error: deflateInit2_() returned %d\n", r);
+		return -1;
+	}
+
+	while (srclen > 0) {
+		comp_len = (srclen > CONFIG_GZIP_COMPRESS_DEF_SZ) ?
+				CONFIG_GZIP_COMPRESS_DEF_SZ : srclen;
+
+		s.next_in = src;
+		s.avail_in = comp_len;
+		flush = (srclen > CONFIG_GZIP_COMPRESS_DEF_SZ)?
+			Z_NO_FLUSH : Z_FINISH;
+
+		do {
+			left_len = (*lenp > CONFIG_GZIP_COMPRESS_DEF_SZ) ?
+					CONFIG_GZIP_COMPRESS_DEF_SZ : *lenp;
+			s.next_out = dst;
+			s.avail_out = left_len;
+			r = deflate(&s, flush);
+			if (r == Z_STREAM_ERROR && stoponerr == 1) {
+				printf("Error: deflate() returned %d\n", r);
+				r = -1;
+				goto bail;
+			}
+			if (!func) {
+				dst += (left_len - s.avail_out);
+				*lenp -= (left_len - s.avail_out);
+			} else if (left_len - s.avail_out > 0) {
+				r = func((unsigned long)dst,
+					left_len - s.avail_out);
+				if (r < 0)
+					goto bail;
+			}
+		} while (s.avail_out == 0 && (*lenp > 0));
+		if (s.avail_in) {
+			printf("Deflate failed to consume %u bytes", s.avail_in);
+			r = -1;
+			goto bail;
+		}
+		if (*lenp == 0) {
+			printf("Deflate need more space to compress "
+				"left %lu bytes\n", srclen);
+			r = -1;
+			goto bail;
+		}
+		srclen -= comp_len;
+		src += comp_len;
+	}
+
+	r = 0;
+bail:
+	deflateEnd(&s);
+	*lenp = orig - *lenp;
+	return r;
+}
-- 
1.7.5.4

  parent reply	other threads:[~2012-09-28 14:26 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-03-25 15:53 [U-Boot] [PATCH 0/6] add zip command support for uboot adrian.wenl at gmail.com
2012-03-25 15:53 ` [U-Boot] [PATCH 1/6] lib: zlib: import deflate source file from 1.2.5 adrian.wenl at gmail.com
2012-03-25 15:53 ` [U-Boot] [PATCH 2/6] lib: zlib: import trees " adrian.wenl at gmail.com
2012-03-25 15:53 ` [U-Boot] [PATCH 3/6] lib: zlib: include deflate into zlib build adrian.wenl at gmail.com
2012-03-25 15:53 ` [U-Boot] [PATCH 4/6] lib: zlib: remove the limitation for cannot using 0 as start adrian.wenl at gmail.com
2012-03-25 15:53 ` [U-Boot] [PATCH 5/6] lib: add gzip lib function callback adrian.wenl at gmail.com
2012-03-25 15:53 ` [U-Boot] [PATCH 6/6] common: add zip command support adrian.wenl at gmail.com
2012-03-27  8:04 ` [U-Boot] [PATCH 0/6] add zip command support for uboot Lei Wen
2012-03-27 18:12   ` Tom Rini
2012-03-28  2:23     ` Lei Wen
2012-04-02 14:16       ` Lei Wen
2012-04-02 19:17   ` Mike Frysinger
2012-04-03  9:31     ` Lei Wen
2012-04-10  4:37       ` Mike Frysinger
2012-04-10  5:05         ` Lei Wen
2012-04-10 22:11           ` Mike Frysinger
2012-04-11  1:24             ` Lei Wen
2012-05-08 12:57               ` Lei Wen
2012-06-21 19:12               ` Wolfgang Denk
2012-09-06  4:18 ` Marek Vasut
2012-09-06  4:49   ` Lei Wen
2012-09-06  6:23     ` Marek Vasut
2012-09-06  8:18       ` Lukasz Majewski
2012-09-06  8:49         ` Marek Vasut
2012-09-06 10:41           ` Lukasz Majewski
2012-09-06 10:49             ` Marek Vasut
2012-09-26  6:41               ` Lei Wen
2012-09-26 15:34                 ` Marek Vasut
2012-09-26 16:20                   ` Tom Rini
2012-09-27  2:05                     ` Lei Wen
2012-09-27 15:22                       ` Tom Rini
2012-09-28 14:29                         ` Lei Wen
2012-09-27  2:12                   ` Lei Wen
2012-09-28 14:26 ` [U-Boot] [PATCH V2 " Lei Wen
2012-09-29 14:51   ` Tom Rini
2012-09-28 14:26 ` [U-Boot] [PATCH V2 1/6] lib: zlib: import deflate source file from 1.2.5 Lei Wen
2012-09-28 14:26 ` [U-Boot] [PATCH V2 2/6] lib: zlib: import trees " Lei Wen
2012-09-28 14:26 ` [U-Boot] [PATCH V2 3/6] lib: zlib: include deflate into zlib build Lei Wen
2012-09-28 14:26 ` [U-Boot] [PATCH V2 4/6] lib: zlib: remove the limitation for cannot using 0 as start Lei Wen
2012-09-28 14:26 ` Lei Wen [this message]
2012-09-28 14:26 ` [U-Boot] [PATCH V2 6/6] common: add zip command support Lei Wen

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=1348842407-20355-6-git-send-email-leiwen@marvell.com \
    --to=adrian.wenl@gmail.com \
    --cc=u-boot@lists.denx.de \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.