0.2.0-dev Download ZIP
149 lines ·4.6 KB Raw
| 1 | /* -*- tab-width: 4; -*- */ |
| 2 | /* vi: set sw=2 ts=4 expandtab: */ |
| 3 | |
| 4 | /* |
| 5 | * Copyright 2023-2023 The Khronos Group Inc. |
| 6 | * Copyright 2023-2023 RasterGrid Kft. |
| 7 | * SPDX-License-Identifier: Apache-2.0 |
| 8 | */ |
| 9 | |
| 10 | /** |
| 11 | * @internal |
| 12 | * @file |
| 13 | * @~English |
| 14 | * |
| 15 | * @brief Wrapper functions for ZLIB compression/decompression using miniz. |
| 16 | * |
| 17 | * @author Daniel Rakos, RasterGrid |
| 18 | */ |
| 19 | |
| 20 | #include "ktx.h" |
| 21 | #include "ktxint.h" |
| 22 | |
| 23 | #include <assert.h> |
| 24 | |
| 25 | #if !KTX_FEATURE_WRITE |
| 26 | // The reader does not link with the basisu components that already include a |
| 27 | // definition of miniz so we include it here explicitly. |
| 28 | #ifdef __GNUC__ |
| 29 | #pragma GCC diagnostic push |
| 30 | #pragma GCC diagnostic ignored "-Wextra" |
| 31 | #pragma GCC diagnostic ignored "-Wmisleading-indentation" |
| 32 | #endif |
| 33 | #include "encoder/basisu_miniz.h" |
| 34 | #ifdef __GNUC__ |
| 35 | #pragma GCC diagnostic pop |
| 36 | #endif |
| 37 | #else |
| 38 | // Otherwise we only declare the interfaces and link with the basisu version. |
| 39 | // This is needed because while miniz is defined as a header in basisu it's |
| 40 | // not declaring the functions as static or inline, hence causing multiple |
| 41 | // conflicting definitions at link-time. |
| 42 | namespace buminiz { |
| 43 | typedef unsigned long mz_ulong; |
| 44 | enum { 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 }; |
| 45 | mz_ulong mz_compressBound(mz_ulong source_len); |
| 46 | int mz_compress2(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong source_len, int level); |
| 47 | int mz_uncompress(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong source_len); |
| 48 | } |
| 49 | #endif |
| 50 | |
| 51 | using namespace buminiz; |
| 52 | |
| 53 | extern "C" { |
| 54 | |
| 55 | /** |
| 56 | * @internal |
| 57 | * @~English |
| 58 | * @brief Returns upper bound for compresses data using miniz (ZLIB). |
| 59 | * |
| 60 | * @param srcLength source data length |
| 61 | * |
| 62 | * @author Daniel Rakos, RasterGrid |
| 63 | */ |
| 64 | ktx_size_t ktxCompressZLIBBounds(ktx_size_t srcLength) { |
| 65 | return mz_compressBound((mz_ulong)srcLength); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * @internal |
| 70 | * @~English |
| 71 | * @brief Compresses data using miniz (ZLIB) |
| 72 | * |
| 73 | * @param pDest destination data buffer |
| 74 | * @param pDestLength destination data buffer size |
| 75 | * (filled with written byte count on success) |
| 76 | * @param pSrc source data buffer |
| 77 | * @param srcLength source data size |
| 78 | * @param level compression level (between 1 and 9) |
| 79 | * |
| 80 | * @author Daniel Rakos, RasterGrid |
| 81 | */ |
| 82 | KTX_error_code ktxCompressZLIBInt(unsigned char* pDest, |
| 83 | ktx_size_t* pDestLength, |
| 84 | const unsigned char* pSrc, |
| 85 | ktx_size_t srcLength, |
| 86 | ktx_uint32_t level) { |
| 87 | if ((srcLength | *pDestLength) > 0xFFFFFFFFU) return KTX_INVALID_VALUE; |
| 88 | mz_ulong mzCompressedSize = (mz_ulong)*pDestLength; |
| 89 | int status = mz_compress2(pDest, &mzCompressedSize, pSrc, (mz_ulong)srcLength, level); |
| 90 | switch (status) { |
| 91 | case MZ_OK: |
| 92 | *pDestLength = mzCompressedSize; |
| 93 | return KTX_SUCCESS; |
| 94 | case MZ_PARAM_ERROR: |
| 95 | return KTX_INVALID_VALUE; |
| 96 | case MZ_BUF_ERROR: |
| 97 | #ifdef DEBUG |
| 98 | assert(false && "Deflate dstSize too small."); |
| 99 | #endif |
| 100 | return KTX_OUT_OF_MEMORY; |
| 101 | case MZ_MEM_ERROR: |
| 102 | #ifdef DEBUG |
| 103 | assert(false && "Deflate workspace too small."); |
| 104 | #endif |
| 105 | return KTX_OUT_OF_MEMORY; |
| 106 | default: |
| 107 | // The remaining errors look like they should only |
| 108 | // occur during decompression but just in case. |
| 109 | #ifdef DEBUG |
| 110 | assert(true); |
| 111 | #endif |
| 112 | return KTX_INVALID_OPERATION; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * @internal |
| 118 | * @~English |
| 119 | * @brief Uncompresses data using miniz (ZLIB) |
| 120 | * |
| 121 | * @param pDest destination data buffer |
| 122 | * @param pDestLength destination data buffer size |
| 123 | * (filled with written byte count on success) |
| 124 | * @param pSrc source data buffer |
| 125 | * @param srcLength source data size |
| 126 | * |
| 127 | * @author Daniel Rakos, RasterGrid |
| 128 | */ |
| 129 | KTX_error_code ktxUncompressZLIBInt(unsigned char* pDest, |
| 130 | ktx_size_t* pDestLength, |
| 131 | const unsigned char* pSrc, |
| 132 | ktx_size_t srcLength) { |
| 133 | if ((srcLength | *pDestLength) > 0xFFFFFFFFU) return KTX_INVALID_VALUE; |
| 134 | mz_ulong mzUncompressedSize = (mz_ulong)*pDestLength; |
| 135 | int status = mz_uncompress(pDest, &mzUncompressedSize, pSrc, (mz_ulong)srcLength); |
| 136 | switch (status) { |
| 137 | case MZ_OK: |
| 138 | *pDestLength = mzUncompressedSize; |
| 139 | return KTX_SUCCESS; |
| 140 | case MZ_BUF_ERROR: |
| 141 | return KTX_DECOMPRESS_LENGTH_ERROR; // buffer too small |
| 142 | case MZ_MEM_ERROR: |
| 143 | return KTX_OUT_OF_MEMORY; |
| 144 | default: |
| 145 | return KTX_FILE_DATA_ERROR; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | } |