arm-trusted-firmware/lib/zlib/tf_gunzip.c
Antonio Nino Diaz 09d40e0e08 Sanitise includes across codebase
Enforce full include path for includes. Deprecate old paths.

The following folders inside include/lib have been left unchanged:

- include/lib/cpus/${ARCH}
- include/lib/el3_runtime/${ARCH}

The reason for this change is that having a global namespace for
includes isn't a good idea. It defeats one of the advantages of having
folders and it introduces problems that are sometimes subtle (because
you may not know the header you are actually including if there are two
of them).

For example, this patch had to be created because two headers were
called the same way: e0ea0928d5 ("Fix gpio includes of mt8173 platform
to avoid collision."). More recently, this patch has had similar
problems: 46f9b2c3a2 ("drivers: add tzc380 support").

This problem was introduced in commit 4ecca33988 ("Move include and
source files to logical locations"). At that time, there weren't too
many headers so it wasn't a real issue. However, time has shown that
this creates problems.

Platforms that want to preserve the way they include headers may add the
removed paths to PLAT_INCLUDES, but this is discouraged.

Change-Id: I39dc53ed98f9e297a5966e723d1936d6ccf2fc8f
Signed-off-by: Antonio Nino Diaz <antonio.ninodiaz@arm.com>
2019-01-04 10:43:17 +00:00

102 lines
2.3 KiB
C

/*
* Copyright (c) 2018, ARM Limited and Contributors. All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <assert.h>
#include <errno.h>
#include <string.h>
#include <common/debug.h>
#include <lib/utils.h>
#include <tf_gunzip.h>
#include "zutil.h"
/*
* memory allocated by malloc() is supposed to be aligned for any built-in type
*/
#define ZALLOC_ALIGNMENT sizeof(void *)
static uintptr_t zalloc_start;
static uintptr_t zalloc_end;
static uintptr_t zalloc_current;
static void * ZLIB_INTERNAL zcalloc(void *opaque, unsigned int items,
unsigned int size)
{
uintptr_t p, p_end;
size *= items;
p = round_up(zalloc_current, ZALLOC_ALIGNMENT);
p_end = p + size;
if (p_end > zalloc_end)
return NULL;
memset((void *)p, 0, size);
zalloc_current = p_end;
return (void *)p;
}
static void ZLIB_INTERNAL zfree(void *opaque, void *ptr)
{
}
/*
* gunzip - decompress gzip data
* @in_buf: source of compressed input. Upon exit, the end of input.
* @in_len: length of in_buf
* @out_buf: destination of decompressed output. Upon exit, the end of output.
* @out_len: length of out_buf
* @work_buf: workspace
* @work_len: length of workspace
*/
int gunzip(uintptr_t *in_buf, size_t in_len, uintptr_t *out_buf,
size_t out_len, uintptr_t work_buf, size_t work_len)
{
z_stream stream;
int zret, ret;
zalloc_start = work_buf;
zalloc_end = work_buf + work_len;
zalloc_current = zalloc_start;
stream.next_in = (typeof(stream.next_in))*in_buf;
stream.avail_in = in_len;
stream.next_out = (typeof(stream.next_out))*out_buf;
stream.avail_out = out_len;
stream.zalloc = zcalloc;
stream.zfree = zfree;
stream.opaque = (voidpf)0;
zret = inflateInit(&stream);
if (zret != Z_OK) {
ERROR("zlib: inflate init failed (ret = %d)\n", zret);
return (zret == Z_MEM_ERROR) ? -ENOMEM : -EIO;
}
zret = inflate(&stream, Z_NO_FLUSH);
if (zret == Z_STREAM_END) {
ret = 0;
} else {
if (stream.msg)
ERROR("%s\n", stream.msg);
ERROR("zlib: inflate failed (ret = %d)\n", zret);
ret = (zret == Z_MEM_ERROR) ? -ENOMEM : -EIO;
}
VERBOSE("zlib: %lu byte input\n", stream.total_in);
VERBOSE("zlib: %lu byte output\n", stream.total_out);
*in_buf = (uintptr_t)stream.next_in;
*out_buf = (uintptr_t)stream.next_out;
inflateEnd(&stream);
return ret;
}