mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH] uncompress: simplify prototype of uncompress()
@ 2023-11-13 19:00 Uwe Kleine-König
  2023-11-15 13:10 ` Sascha Hauer
  2023-11-16  7:22 ` Sascha Hauer
  0 siblings, 2 replies; 4+ messages in thread
From: Uwe Kleine-König @ 2023-11-13 19:00 UTC (permalink / raw)
  To: barebox

All callers apart from lib/uncompress.c itself only use memory-to-memory
decompression. Simplify the calls accordingly.

Note that two of three callers passed error_fn=NULL. As the uncompress
function calls error_fn() unconditionally on error, this might yield
undefined behaviour and so the new uncompress function uses
uncompress_err_stdout() as error function which isn't worse for sure.

Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
 arch/arm/cpu/start.c    |  2 +-
 arch/riscv/boot/start.c |  2 +-
 defaultenv/defaultenv.c |  4 +---
 include/uncompress.h    |  7 +------
 lib/uncompress.c        | 24 +++++++++++++++---------
 5 files changed, 19 insertions(+), 20 deletions(-)

diff --git a/arch/arm/cpu/start.c b/arch/arm/cpu/start.c
index 15f5b2937227..65e1a0cce53b 100644
--- a/arch/arm/cpu/start.c
+++ b/arch/arm/cpu/start.c
@@ -84,7 +84,7 @@ void *barebox_arm_boot_dtb(void)
 		       compressed_dtb->datalen_uncompressed);
 	else
 		ret = uncompress(compressed_dtb->data, compressed_dtb->datalen,
-				 NULL, NULL, dtb, NULL, NULL);
+				 dtb);
 
 	if (ret) {
 		pr_err("uncompressing dtb failed\n");
diff --git a/arch/riscv/boot/start.c b/arch/riscv/boot/start.c
index 92991d0f6a84..526012b9a43b 100644
--- a/arch/riscv/boot/start.c
+++ b/arch/riscv/boot/start.c
@@ -59,7 +59,7 @@ void *barebox_riscv_boot_dtb(void)
 		return NULL;
 
 	ret = uncompress(compressed_dtb->data, compressed_dtb->datalen,
-			 NULL, NULL, dtb, NULL, NULL);
+			 dtb);
 	if (ret) {
 		pr_err("uncompressing dtb failed\n");
 		free(dtb);
diff --git a/defaultenv/defaultenv.c b/defaultenv/defaultenv.c
index 055475eb4756..b1b042d724d7 100644
--- a/defaultenv/defaultenv.c
+++ b/defaultenv/defaultenv.c
@@ -111,9 +111,7 @@ static int defaultenv_load_one(struct defaultenv *df, const char *dir,
 		if (!freep)
 			return -ENOMEM;
 
-		ret = uncompress(df->buf, df->size,
-				NULL, NULL,
-				freep, NULL, uncompress_err_stdout);
+		ret = uncompress(df->buf, df->size, freep)
 		if (ret) {
 			free(freep);
 			pr_err("Failed to uncompress: %s\n", strerror(-ret));
diff --git a/include/uncompress.h b/include/uncompress.h
index 72ba1dfda607..c6ab2abde983 100644
--- a/include/uncompress.h
+++ b/include/uncompress.h
@@ -2,12 +2,7 @@
 #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(unsigned char *inbuf, int len, unsigned char *output);
 
 int uncompress_fd_to_fd(int infd, int outfd,
 	   void(*error_fn)(char *x));
diff --git a/lib/uncompress.c b/lib/uncompress.c
index 71ac882b87fe..8f7284ba68c0 100644
--- a/lib/uncompress.c
+++ b/lib/uncompress.c
@@ -60,12 +60,12 @@ static int uncompress_fill(void *buf, unsigned int len)
 	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))
+static 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,
@@ -139,6 +139,12 @@ int uncompress(unsigned char *inbuf, int len,
 	return ret;
 }
 
+int uncompress(unsigned char *inbuf, int len,
+	       unsigned char *output)
+{
+	return __uncompress(inbuf, len, NULL, NULL, output, NULL, uncompress_err_stdout);
+}
+
 static int uncompress_infd, uncompress_outfd;
 
 static int fill_fd(void *buf, unsigned int len)
@@ -157,7 +163,7 @@ int uncompress_fd_to_fd(int infd, int outfd,
 	uncompress_infd = infd;
 	uncompress_outfd = outfd;
 
-	return uncompress(NULL, 0,
+	return __uncompress(NULL, 0,
 	   fill_fd,
 	   flush_fd,
 	   NULL,
@@ -170,7 +176,7 @@ int uncompress_fd_to_buf(int infd, void *output,
 {
 	uncompress_infd = infd;
 
-	return uncompress(NULL, 0, fill_fd, NULL, output, NULL, error_fn);
+	return __uncompress(NULL, 0, fill_fd, NULL, output, NULL, error_fn);
 }
 
 int uncompress_buf_to_fd(const void *input, size_t input_len,
@@ -178,7 +184,7 @@ int uncompress_buf_to_fd(const void *input, size_t input_len,
 {
 	uncompress_outfd = outfd;
 
-	return uncompress((void *)input, input_len, NULL, flush_fd,
+	return __uncompress((void *)input, input_len, NULL, flush_fd,
 			  NULL, NULL, error_fn);
 }
 

base-commit: a9120f147631785fec30eb1e18615d8eabd3d087
-- 
2.42.0




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] uncompress: simplify prototype of uncompress()
  2023-11-13 19:00 [PATCH] uncompress: simplify prototype of uncompress() Uwe Kleine-König
@ 2023-11-15 13:10 ` Sascha Hauer
  2023-11-16  7:22 ` Sascha Hauer
  1 sibling, 0 replies; 4+ messages in thread
From: Sascha Hauer @ 2023-11-15 13:10 UTC (permalink / raw)
  To: Uwe Kleine-König; +Cc: barebox

On Mon, Nov 13, 2023 at 08:00:35PM +0100, Uwe Kleine-König wrote:
> All callers apart from lib/uncompress.c itself only use memory-to-memory
> decompression. Simplify the calls accordingly.
> 
> Note that two of three callers passed error_fn=NULL. As the uncompress
> function calls error_fn() unconditionally on error, this might yield
> undefined behaviour and so the new uncompress function uses
> uncompress_err_stdout() as error function which isn't worse for sure.
> 
> Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
> ---
>  arch/arm/cpu/start.c    |  2 +-
>  arch/riscv/boot/start.c |  2 +-
>  defaultenv/defaultenv.c |  4 +---
>  include/uncompress.h    |  7 +------
>  lib/uncompress.c        | 24 +++++++++++++++---------
>  5 files changed, 19 insertions(+), 20 deletions(-)

Applied, thanks

Sascha

> 
> diff --git a/arch/arm/cpu/start.c b/arch/arm/cpu/start.c
> index 15f5b2937227..65e1a0cce53b 100644
> --- a/arch/arm/cpu/start.c
> +++ b/arch/arm/cpu/start.c
> @@ -84,7 +84,7 @@ void *barebox_arm_boot_dtb(void)
>  		       compressed_dtb->datalen_uncompressed);
>  	else
>  		ret = uncompress(compressed_dtb->data, compressed_dtb->datalen,
> -				 NULL, NULL, dtb, NULL, NULL);
> +				 dtb);
>  
>  	if (ret) {
>  		pr_err("uncompressing dtb failed\n");
> diff --git a/arch/riscv/boot/start.c b/arch/riscv/boot/start.c
> index 92991d0f6a84..526012b9a43b 100644
> --- a/arch/riscv/boot/start.c
> +++ b/arch/riscv/boot/start.c
> @@ -59,7 +59,7 @@ void *barebox_riscv_boot_dtb(void)
>  		return NULL;
>  
>  	ret = uncompress(compressed_dtb->data, compressed_dtb->datalen,
> -			 NULL, NULL, dtb, NULL, NULL);
> +			 dtb);
>  	if (ret) {
>  		pr_err("uncompressing dtb failed\n");
>  		free(dtb);
> diff --git a/defaultenv/defaultenv.c b/defaultenv/defaultenv.c
> index 055475eb4756..b1b042d724d7 100644
> --- a/defaultenv/defaultenv.c
> +++ b/defaultenv/defaultenv.c
> @@ -111,9 +111,7 @@ static int defaultenv_load_one(struct defaultenv *df, const char *dir,
>  		if (!freep)
>  			return -ENOMEM;
>  
> -		ret = uncompress(df->buf, df->size,
> -				NULL, NULL,
> -				freep, NULL, uncompress_err_stdout);
> +		ret = uncompress(df->buf, df->size, freep)
>  		if (ret) {
>  			free(freep);
>  			pr_err("Failed to uncompress: %s\n", strerror(-ret));
> diff --git a/include/uncompress.h b/include/uncompress.h
> index 72ba1dfda607..c6ab2abde983 100644
> --- a/include/uncompress.h
> +++ b/include/uncompress.h
> @@ -2,12 +2,7 @@
>  #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(unsigned char *inbuf, int len, unsigned char *output);
>  
>  int uncompress_fd_to_fd(int infd, int outfd,
>  	   void(*error_fn)(char *x));
> diff --git a/lib/uncompress.c b/lib/uncompress.c
> index 71ac882b87fe..8f7284ba68c0 100644
> --- a/lib/uncompress.c
> +++ b/lib/uncompress.c
> @@ -60,12 +60,12 @@ static int uncompress_fill(void *buf, unsigned int len)
>  	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))
> +static 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,
> @@ -139,6 +139,12 @@ int uncompress(unsigned char *inbuf, int len,
>  	return ret;
>  }
>  
> +int uncompress(unsigned char *inbuf, int len,
> +	       unsigned char *output)
> +{
> +	return __uncompress(inbuf, len, NULL, NULL, output, NULL, uncompress_err_stdout);
> +}
> +
>  static int uncompress_infd, uncompress_outfd;
>  
>  static int fill_fd(void *buf, unsigned int len)
> @@ -157,7 +163,7 @@ int uncompress_fd_to_fd(int infd, int outfd,
>  	uncompress_infd = infd;
>  	uncompress_outfd = outfd;
>  
> -	return uncompress(NULL, 0,
> +	return __uncompress(NULL, 0,
>  	   fill_fd,
>  	   flush_fd,
>  	   NULL,
> @@ -170,7 +176,7 @@ int uncompress_fd_to_buf(int infd, void *output,
>  {
>  	uncompress_infd = infd;
>  
> -	return uncompress(NULL, 0, fill_fd, NULL, output, NULL, error_fn);
> +	return __uncompress(NULL, 0, fill_fd, NULL, output, NULL, error_fn);
>  }
>  
>  int uncompress_buf_to_fd(const void *input, size_t input_len,
> @@ -178,7 +184,7 @@ int uncompress_buf_to_fd(const void *input, size_t input_len,
>  {
>  	uncompress_outfd = outfd;
>  
> -	return uncompress((void *)input, input_len, NULL, flush_fd,
> +	return __uncompress((void *)input, input_len, NULL, flush_fd,
>  			  NULL, NULL, error_fn);
>  }
>  
> 
> base-commit: a9120f147631785fec30eb1e18615d8eabd3d087
> -- 
> 2.42.0
> 
> 
> 

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] uncompress: simplify prototype of uncompress()
  2023-11-13 19:00 [PATCH] uncompress: simplify prototype of uncompress() Uwe Kleine-König
  2023-11-15 13:10 ` Sascha Hauer
@ 2023-11-16  7:22 ` Sascha Hauer
  2023-11-16  8:01   ` Uwe Kleine-König
  1 sibling, 1 reply; 4+ messages in thread
From: Sascha Hauer @ 2023-11-16  7:22 UTC (permalink / raw)
  To: Uwe Kleine-König; +Cc: barebox

On Mon, Nov 13, 2023 at 08:00:35PM +0100, Uwe Kleine-König wrote:
> All callers apart from lib/uncompress.c itself only use memory-to-memory
> decompression. Simplify the calls accordingly.
> 
> Note that two of three callers passed error_fn=NULL. As the uncompress
> function calls error_fn() unconditionally on error, this might yield
> undefined behaviour and so the new uncompress function uses
> uncompress_err_stdout() as error function which isn't worse for sure.
> 
> Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
> ---
>  arch/arm/cpu/start.c    |  2 +-
>  arch/riscv/boot/start.c |  2 +-
>  defaultenv/defaultenv.c |  4 +---
>  include/uncompress.h    |  7 +------
>  lib/uncompress.c        | 24 +++++++++++++++---------
>  5 files changed, 19 insertions(+), 20 deletions(-)

I had to revert this one. You missed one call site of uncompress() in
uimage_load(). That one can't be converted as it uses the fill argument
to uncompress().

Sascha


> 
> diff --git a/arch/arm/cpu/start.c b/arch/arm/cpu/start.c
> index 15f5b2937227..65e1a0cce53b 100644
> --- a/arch/arm/cpu/start.c
> +++ b/arch/arm/cpu/start.c
> @@ -84,7 +84,7 @@ void *barebox_arm_boot_dtb(void)
>  		       compressed_dtb->datalen_uncompressed);
>  	else
>  		ret = uncompress(compressed_dtb->data, compressed_dtb->datalen,
> -				 NULL, NULL, dtb, NULL, NULL);
> +				 dtb);
>  
>  	if (ret) {
>  		pr_err("uncompressing dtb failed\n");
> diff --git a/arch/riscv/boot/start.c b/arch/riscv/boot/start.c
> index 92991d0f6a84..526012b9a43b 100644
> --- a/arch/riscv/boot/start.c
> +++ b/arch/riscv/boot/start.c
> @@ -59,7 +59,7 @@ void *barebox_riscv_boot_dtb(void)
>  		return NULL;
>  
>  	ret = uncompress(compressed_dtb->data, compressed_dtb->datalen,
> -			 NULL, NULL, dtb, NULL, NULL);
> +			 dtb);
>  	if (ret) {
>  		pr_err("uncompressing dtb failed\n");
>  		free(dtb);
> diff --git a/defaultenv/defaultenv.c b/defaultenv/defaultenv.c
> index 055475eb4756..b1b042d724d7 100644
> --- a/defaultenv/defaultenv.c
> +++ b/defaultenv/defaultenv.c
> @@ -111,9 +111,7 @@ static int defaultenv_load_one(struct defaultenv *df, const char *dir,
>  		if (!freep)
>  			return -ENOMEM;
>  
> -		ret = uncompress(df->buf, df->size,
> -				NULL, NULL,
> -				freep, NULL, uncompress_err_stdout);
> +		ret = uncompress(df->buf, df->size, freep)
>  		if (ret) {
>  			free(freep);
>  			pr_err("Failed to uncompress: %s\n", strerror(-ret));
> diff --git a/include/uncompress.h b/include/uncompress.h
> index 72ba1dfda607..c6ab2abde983 100644
> --- a/include/uncompress.h
> +++ b/include/uncompress.h
> @@ -2,12 +2,7 @@
>  #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(unsigned char *inbuf, int len, unsigned char *output);
>  
>  int uncompress_fd_to_fd(int infd, int outfd,
>  	   void(*error_fn)(char *x));
> diff --git a/lib/uncompress.c b/lib/uncompress.c
> index 71ac882b87fe..8f7284ba68c0 100644
> --- a/lib/uncompress.c
> +++ b/lib/uncompress.c
> @@ -60,12 +60,12 @@ static int uncompress_fill(void *buf, unsigned int len)
>  	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))
> +static 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,
> @@ -139,6 +139,12 @@ int uncompress(unsigned char *inbuf, int len,
>  	return ret;
>  }
>  
> +int uncompress(unsigned char *inbuf, int len,
> +	       unsigned char *output)
> +{
> +	return __uncompress(inbuf, len, NULL, NULL, output, NULL, uncompress_err_stdout);
> +}
> +
>  static int uncompress_infd, uncompress_outfd;
>  
>  static int fill_fd(void *buf, unsigned int len)
> @@ -157,7 +163,7 @@ int uncompress_fd_to_fd(int infd, int outfd,
>  	uncompress_infd = infd;
>  	uncompress_outfd = outfd;
>  
> -	return uncompress(NULL, 0,
> +	return __uncompress(NULL, 0,
>  	   fill_fd,
>  	   flush_fd,
>  	   NULL,
> @@ -170,7 +176,7 @@ int uncompress_fd_to_buf(int infd, void *output,
>  {
>  	uncompress_infd = infd;
>  
> -	return uncompress(NULL, 0, fill_fd, NULL, output, NULL, error_fn);
> +	return __uncompress(NULL, 0, fill_fd, NULL, output, NULL, error_fn);
>  }
>  
>  int uncompress_buf_to_fd(const void *input, size_t input_len,
> @@ -178,7 +184,7 @@ int uncompress_buf_to_fd(const void *input, size_t input_len,
>  {
>  	uncompress_outfd = outfd;
>  
> -	return uncompress((void *)input, input_len, NULL, flush_fd,
> +	return __uncompress((void *)input, input_len, NULL, flush_fd,
>  			  NULL, NULL, error_fn);
>  }
>  
> 
> base-commit: a9120f147631785fec30eb1e18615d8eabd3d087
> -- 
> 2.42.0
> 
> 
> 

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] uncompress: simplify prototype of uncompress()
  2023-11-16  7:22 ` Sascha Hauer
@ 2023-11-16  8:01   ` Uwe Kleine-König
  0 siblings, 0 replies; 4+ messages in thread
From: Uwe Kleine-König @ 2023-11-16  8:01 UTC (permalink / raw)
  To: Sascha Hauer; +Cc: barebox

[-- Attachment #1: Type: text/plain, Size: 1457 bytes --]

On Thu, Nov 16, 2023 at 08:22:44AM +0100, Sascha Hauer wrote:
> On Mon, Nov 13, 2023 at 08:00:35PM +0100, Uwe Kleine-König wrote:
> > All callers apart from lib/uncompress.c itself only use memory-to-memory
> > decompression. Simplify the calls accordingly.
> > 
> > Note that two of three callers passed error_fn=NULL. As the uncompress
> > function calls error_fn() unconditionally on error, this might yield
> > undefined behaviour and so the new uncompress function uses
> > uncompress_err_stdout() as error function which isn't worse for sure.
> > 
> > Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
> > ---
> >  arch/arm/cpu/start.c    |  2 +-
> >  arch/riscv/boot/start.c |  2 +-
> >  defaultenv/defaultenv.c |  4 +---
> >  include/uncompress.h    |  7 +------
> >  lib/uncompress.c        | 24 +++++++++++++++---------
> >  5 files changed, 19 insertions(+), 20 deletions(-)
> 
> I had to revert this one. You missed one call site of uncompress() in
> uimage_load(). That one can't be converted as it uses the fill argument
> to uncompress().

Yes indeed. Missed that because I grepped for "uncompress(", sorry!

That instance might be converted to uncompress_fd_to_buf, but that's not
completely trivial. I'll take a look.

Best regards
Uwe

-- 
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-11-16  8:03 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-13 19:00 [PATCH] uncompress: simplify prototype of uncompress() Uwe Kleine-König
2023-11-15 13:10 ` Sascha Hauer
2023-11-16  7:22 ` Sascha Hauer
2023-11-16  8:01   ` Uwe Kleine-König

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox