[PATCH] uncompress: simplify prototype of uncompress()

Sascha Hauer sha at pengutronix.de
Wed Nov 15 23:22:44 PST 2023


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 at 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 |



More information about the barebox mailing list