Path: blob/master/thirdparty/libktx/lib/miniz_wrapper.cpp
9902 views
/* -*- tab-width: 4; -*- */1/* vi: set sw=2 ts=4 expandtab: */23/*4* Copyright 2023-2023 The Khronos Group Inc.5* Copyright 2023-2023 RasterGrid Kft.6* SPDX-License-Identifier: Apache-2.07*/89/**10* @internal11* @file12* @~English13*14* @brief Wrapper functions for ZLIB compression/decompression using miniz.15*16* @author Daniel Rakos, RasterGrid17*/1819#include "ktx.h"20#include "ktxint.h"2122#include <assert.h>2324#if !KTX_FEATURE_WRITE25// The reader does not link with the basisu components that already include a26// definition of miniz so we include it here explicitly.27#ifdef __GNUC__28#pragma GCC diagnostic push29#pragma GCC diagnostic ignored "-Wextra"30#pragma GCC diagnostic ignored "-Wmisleading-indentation"31#endif32#include "encoder/basisu_miniz.h"33#ifdef __GNUC__34#pragma GCC diagnostic pop35#endif36#else37// Otherwise we only declare the interfaces and link with the basisu version.38// This is needed because while miniz is defined as a header in basisu it's39// not declaring the functions as static or inline, hence causing multiple40// conflicting definitions at link-time.41namespace buminiz {42typedef unsigned long mz_ulong;43enum { MZ_OK = 0, MZ_STREAM_END = 1, MZ_NEED_DICT = 2, MZ_ERRNO = -1, MZ_STREAM_ERROR = -2, MZ_DATA_ERROR = -3, MZ_MEM_ERROR = -4, MZ_BUF_ERROR = -5, MZ_VERSION_ERROR = -6, MZ_PARAM_ERROR = -10000 };44mz_ulong mz_compressBound(mz_ulong source_len);45int mz_compress2(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong source_len, int level);46int mz_uncompress(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong source_len);47}48#endif4950using namespace buminiz;5152extern "C" {5354/**55* @internal56* @~English57* @brief Returns upper bound for compresses data using miniz (ZLIB).58*59* @param srcLength source data length60*61* @author Daniel Rakos, RasterGrid62*/63ktx_size_t ktxCompressZLIBBounds(ktx_size_t srcLength) {64return mz_compressBound((mz_ulong)srcLength);65}6667/**68* @internal69* @~English70* @brief Compresses data using miniz (ZLIB)71*72* @param pDest destination data buffer73* @param pDestLength destination data buffer size74* (filled with written byte count on success)75* @param pSrc source data buffer76* @param srcLength source data size77* @param level compression level (between 1 and 9)78*79* @author Daniel Rakos, RasterGrid80*/81KTX_error_code ktxCompressZLIBInt(unsigned char* pDest,82ktx_size_t* pDestLength,83const unsigned char* pSrc,84ktx_size_t srcLength,85ktx_uint32_t level) {86if ((srcLength | *pDestLength) > 0xFFFFFFFFU) return KTX_INVALID_VALUE;87mz_ulong mzCompressedSize = (mz_ulong)*pDestLength;88int status = mz_compress2(pDest, &mzCompressedSize, pSrc, (mz_ulong)srcLength, level);89switch (status) {90case MZ_OK:91*pDestLength = mzCompressedSize;92return KTX_SUCCESS;93case MZ_PARAM_ERROR:94return KTX_INVALID_VALUE;95case MZ_BUF_ERROR:96#ifdef DEBUG97assert(false && "Deflate dstSize too small.");98#endif99return KTX_OUT_OF_MEMORY;100case MZ_MEM_ERROR:101#ifdef DEBUG102assert(false && "Deflate workspace too small.");103#endif104return KTX_OUT_OF_MEMORY;105default:106// The remaining errors look like they should only107// occur during decompression but just in case.108#ifdef DEBUG109assert(true);110#endif111return KTX_INVALID_OPERATION;112}113}114115/**116* @internal117* @~English118* @brief Uncompresses data using miniz (ZLIB)119*120* @param pDest destination data buffer121* @param pDestLength destination data buffer size122* (filled with written byte count on success)123* @param pSrc source data buffer124* @param srcLength source data size125*126* @author Daniel Rakos, RasterGrid127*/128KTX_error_code ktxUncompressZLIBInt(unsigned char* pDest,129ktx_size_t* pDestLength,130const unsigned char* pSrc,131ktx_size_t srcLength) {132if ((srcLength | *pDestLength) > 0xFFFFFFFFU) return KTX_INVALID_VALUE;133mz_ulong mzUncompressedSize = (mz_ulong)*pDestLength;134int status = mz_uncompress(pDest, &mzUncompressedSize, pSrc, (mz_ulong)srcLength);135switch (status) {136case MZ_OK:137*pDestLength = mzUncompressedSize;138return KTX_SUCCESS;139case MZ_BUF_ERROR:140return KTX_DECOMPRESS_LENGTH_ERROR; // buffer too small141case MZ_MEM_ERROR:142return KTX_OUT_OF_MEMORY;143default:144return KTX_FILE_DATA_ERROR;145}146}147148}149150151