u-boot/lib/gzip.c
Michal Simek 26beded3d0 zlib: Remove incorrect ZLIB_VERSION
Get rid of zlib version which is not correct because of U-Boot related
changes and various CVE backports.

The change in inspired by Linux kernel commit 4f3865fb57a0 ("[PATCH]
zlib_inflate: Upgrade library code to a recent version") which described
ZLIB_VERSION removal as

"This patch also removes ZLIB_VERSION as it no longer has a correct value.
We don't need version checks anyway as the kernel's module handling will
take care of that for us.  This removal is also more in keeping with the
zlib author's wishes (http://www.zlib.net/zlib_faq.html#faq24) and I've
added something to the zlib.h header to note its a modified version."

Author describes wish to follow this guidance at
https://www.zlib.net/zlib_faq.html#faq24:
"The license says that altered source versions must be "plainly marked". So
what exactly do I need to do to meet that requirement?

You need to change the ZLIB_VERSION and ZLIB_VERNUM #defines in zlib.h. In
particular, the final version number needs to be changed to f, and an
identification string should be appended to ZLIB_VERSION. Version numbers
x.x.x.f are reserved for modifications to zlib by others than the zlib
maintainers. For example, if the version of the base zlib you are altering
is 1.2.3.4, then in zlib.h you should change ZLIB_VERNUM to 0x123f, and
ZLIB_VERSION to something like 1.2.3.f-zachary-mods-v3. You can also update
the version strings in deflate.c and inftrees.c."

But U-Boot is not exact version that's why following the same style which
has been used by Linux kernel where ZLIB_VERSION is completely removed.

Signed-off-by: Michal Simek <michal.simek@amd.com>
2024-04-12 12:57:07 -06:00

127 lines
2.6 KiB
C

// SPDX-License-Identifier: GPL-2.0+
/*
* (C) Copyright 2012
* Lei Wen <leiwen@marvell.com>, Marvell Inc.
*/
#include <watchdog.h>
#include <command.h>
#include <gzip.h>
#include <image.h>
#include <malloc.h>
#include <memalign.h>
#include <u-boot/zlib.h>
#include "zlib/zutil.h"
#ifndef CFG_GZIP_COMPRESS_DEF_SZ
#define CFG_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_cache_aligned(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,
sizeof(z_stream));
if (r != Z_OK) {
printf ("Error: deflateInit2_() returned %d\n", r);
return -1;
}
while (srclen > 0) {
comp_len = (srclen > CFG_GZIP_COMPRESS_DEF_SZ) ?
CFG_GZIP_COMPRESS_DEF_SZ : srclen;
s.next_in = src;
s.avail_in = comp_len;
flush = (srclen > CFG_GZIP_COMPRESS_DEF_SZ)?
Z_NO_FLUSH : Z_FINISH;
do {
left_len = (*lenp > CFG_GZIP_COMPRESS_DEF_SZ) ?
CFG_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;
}