[U-Boot] [PATCH v2] easylogo: add optional gzip support
Wolfgang Denk
wd at denx.de
Wed Dec 10 20:57:27 CET 2008
Dear Mike Frysinger,
In message <1228934668-20992-1-git-send-email-vapier at gentoo.org> you wrote:
> Some images can be quite large, so add an option to compress the image data
> with gzip in the U-Boot image. Then at runtime, the board can decompress it
> with the normal zlib functions.
>
> Signed-off-by: Mike Frysinger <vapier at gentoo.org>
> ---
> v2:
> - add error checking
>
> tools/easylogo/easylogo.c | 91 ++++++++++++++++++++++++++++++++++++++++++---
> 1 files changed, 85 insertions(+), 6 deletions(-)
>
> diff --git a/tools/easylogo/easylogo.c b/tools/easylogo/easylogo.c
> index 00a1e4e..c7e146f 100644
> --- a/tools/easylogo/easylogo.c
> +++ b/tools/easylogo/easylogo.c
> @@ -2,16 +2,20 @@
> ** Easylogo TGA->header converter
> ** ==============================
> ** (C) 2000 by Paolo Scaffardi (arsenio at tin.it)
> +** (C) 2007-2008 Mike Frysinger <vapier at gentoo.org>
> ** AIRVENT SAM s.p.a - RIMINI(ITALY)
This is a bad place to add your copyright - splitting Paolo's entry.
> ** This is still under construction!
> */
>
> +#include <errno.h>
> #include <getopt.h>
> #include <stdbool.h>
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
> +#include <unistd.h>
> +#include <sys/stat.h>
>
> #pragma pack(1)
>
> @@ -49,6 +53,17 @@ typedef struct {
> int width, height, pixels, bpp, pixel_size, size, palette_size, yuyv;
> } image_t;
>
> +void *xmalloc (size_t size)
> +{
> + void *ret = malloc (size);
> + if (!ret) {
> + fprintf (stderr, "\nerror: malloc(%zu) failed: %s",
> + size, strerror(errno));
> + exit(1);
> + }
> + return ret;
> +}
> +
> void StringUpperCase (char *str)
> {
> int count = strlen (str);
> @@ -171,7 +186,7 @@ int image_load_tga (image_t * image, char *filename)
> image->pixel_size = ((image->bpp - 1) / 8) + 1;
> image->pixels = image->width * image->height;
> image->size = image->pixels * image->pixel_size;
> - image->data = malloc (image->size);
> + image->data = xmalloc (image->size);
>
> if (image->bpp != 24) {
> printf ("Bpp not supported: %d!\n", image->bpp);
> @@ -192,7 +207,7 @@ int image_load_tga (image_t * image, char *filename)
> /* Swapping image */
>
> if (!(header.ImageDescriptorByte & 0x20)) {
> - unsigned char *temp = malloc (image->size);
> + unsigned char *temp = xmalloc (image->size);
> int linesize = image->pixel_size * image->width;
> void *dest = image->data,
> *source = temp + image->size - linesize;
> @@ -239,7 +254,7 @@ int image_rgb_to_yuyv (image_t * rgb_image, image_t * yuyv_image)
> yuyv_image->pixels = yuyv_image->width * yuyv_image->height;
> yuyv_image->size = yuyv_image->pixels * yuyv_image->pixel_size;
> dest = (unsigned short *) (yuyv_image->data =
> - malloc (yuyv_image->size));
> + xmalloc (yuyv_image->size));
> yuyv_image->palette = 0;
> yuyv_image->palette_size = 0;
>
> @@ -261,6 +276,8 @@ int image_rgb_to_yuyv (image_t * rgb_image, image_t * yuyv_image)
> return 0;
> }
>
> +int use_gzip = 0;
> +
> int image_save_header (image_t * image, char *filename, char *varname)
> {
> FILE *file = fopen (filename, "w");
> @@ -283,6 +300,58 @@ int image_save_header (image_t * image, char *filename, char *varname)
> fprintf (file, " *\t\t'x'\t\tis the horizontal position\n");
> fprintf (file, " *\t\t'y'\t\tis the vertical position\n */\n\n");
>
> + /* gzip compress */
> + if (use_gzip & 0x1) {
> + unsigned char *compressed;
> + struct stat st;
> + FILE *gz;
> + char *gzfilename = xmalloc(strlen (filename) + 20);
> + char *gzcmd = xmalloc(strlen (filename) + 20);
> +
> + sprintf (gzfilename, "%s.gz", filename);
> + sprintf (gzcmd, "gzip > %s", gzfilename);
> + gz = popen (gzcmd, "w");
> + if (!gz) {
> + perror ("\nerror: popen() failed");
> + return -1;
> + }
> + if (fwrite (image->data, image->size, 1, gz) != 1) {
> + perror ("\nerror: writing data to gzip failed");
> + return -1;
> + }
> + if (pclose (gz)) {
> + perror ("\nerror: gzip process failed");
> + return -1;
> + }
> +
> + gz = fopen (gzfilename, "r");
> + if (!gz) {
> + perror ("\nerror: open() on gzip data failed");
> + return -1;
> + }
> + if (stat (gzfilename, &st)) {
> + perror ("\nerror: stat() on gzip file failed");
> + return -1;
> + }
> + compressed = xmalloc (st.st_size);
> + if (fread (compressed, st.st_size, 1, gz) != 1) {
> + perror ("\nerror: reading gzip data failed");
> + return -1;
> + }
All the returns above leak the gzfilename and gzcmd memory. Not that
it really matters, but it's not clean. Also, please omit the "\n" at
the begin of the perror strings.
Best regards,
Wolfgang Denk
--
DENX Software Engineering GmbH, MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
At the source of every error which is blamed on the computer you will
find at least two human errors, including the error of blaming it on
the computer.
More information about the U-Boot
mailing list