| 1 | /*************************************************************************** |
| 2 | * fheroes2: https://github.com/ihhub/fheroes2 * |
| 3 | * Copyright (C) 2019 - 2026 * |
| 4 | * * |
| 5 | * Free Heroes2 Engine: http://sourceforge.net/projects/fheroes2 * |
| 6 | * Copyright (C) 2009 by Andrey Afletdinov <[email protected]> * |
| 7 | * * |
| 8 | * This program is free software; you can redistribute it and/or modify * |
| 9 | * it under the terms of the GNU General Public License as published by * |
| 10 | * the Free Software Foundation; either version 2 of the License, or * |
| 11 | * (at your option) any later version. * |
| 12 | * * |
| 13 | * This program is distributed in the hope that it will be useful, * |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 16 | * GNU General Public License for more details. * |
| 17 | * * |
| 18 | * You should have received a copy of the GNU General Public License * |
| 19 | * along with this program; if not, write to the * |
| 20 | * Free Software Foundation, Inc., * |
| 21 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
| 22 | ***************************************************************************/ |
| 23 | |
| 24 | #include "zzlib.h" |
| 25 | |
| 26 | #include <cstring> |
| 27 | #include <ostream> |
| 28 | |
| 29 | #include <zconf.h> |
| 30 | #include <zlib.h> |
| 31 | |
| 32 | #include "logging.h" |
| 33 | #include "serialize.h" |
| 34 | |
| 35 | namespace |
| 36 | { |
| 37 | constexpr uint16_t FORMAT_VERSION_0 = 0; |
| 38 | } |
| 39 | |
| 40 | std::vector<uint8_t> Compression::unzipData( const uint8_t * src, const size_t srcSize, size_t realSize /* = 0 */ ) |
| 41 | { |
| 42 | if ( src == nullptr || srcSize == 0 ) { |
| 43 | return {}; |
| 44 | } |
| 45 | |
| 46 | const uLong srcSizeULong = static_cast<uLong>( srcSize ); |
| 47 | if ( srcSizeULong != srcSize ) { |
| 48 | ERROR_LOG( "The size of the compressed data is too large" ) |
| 49 | return {}; |
| 50 | } |
| 51 | |
| 52 | std::vector<uint8_t> res( realSize ); |
| 53 | |
| 54 | if ( realSize == 0 ) { |
| 55 | constexpr size_t sizeMultiplier = 7; |
| 56 | |
| 57 | if ( srcSize > res.max_size() / sizeMultiplier ) { |
| 58 | // If the multiplicated size is too large, let's start with the original size and see how it goes |
| 59 | realSize = srcSize; |
| 60 | } |
| 61 | else { |
| 62 | realSize = srcSize * sizeMultiplier; |
| 63 | } |
| 64 | |
| 65 | res.resize( realSize ); |
| 66 | } |
| 67 | |
| 68 | uLong dstSizeULong = static_cast<uLong>( res.size() ); |
| 69 | if ( dstSizeULong != res.size() ) { |
| 70 | ERROR_LOG( "The size of the decompressed data is too large" ) |
| 71 | return {}; |
| 72 | } |
| 73 | |
| 74 | int ret = Z_BUF_ERROR; |
| 75 | while ( Z_BUF_ERROR == ( ret = uncompress( res.data(), &dstSizeULong, src, srcSizeULong ) ) ) { |
| 76 | constexpr size_t sizeMultiplier = 2; |
| 77 | |
| 78 | // Avoid infinite loop due to unsigned overflow on multiplication |
| 79 | if ( res.size() > res.max_size() / sizeMultiplier ) { |
| 80 | ERROR_LOG( "The size of the decompressed data is too large" ) |
| 81 | return {}; |
| 82 | } |
| 83 | |
| 84 | res.resize( res.size() * sizeMultiplier ); |
| 85 | |
| 86 | dstSizeULong = static_cast<uLong>( res.size() ); |
| 87 | if ( dstSizeULong != res.size() ) { |
| 88 | ERROR_LOG( "The size of the decompressed data is too large" ) |
| 89 | return {}; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | if ( ret != Z_OK ) { |
| 94 | ERROR_LOG( "zlib error: " << ret ) |
| 95 | return {}; |
| 96 | } |
| 97 | |
| 98 | res.resize( dstSizeULong ); |
| 99 | |
| 100 | return res; |
| 101 | } |
| 102 | |
| 103 | bool Compression::unzipData( const uint8_t * src, const size_t srcSize, uint8_t * dst, const size_t dstSize ) |
| 104 | { |
| 105 | if ( src == nullptr || srcSize == 0 || dst == nullptr || dstSize == 0 ) { |
| 106 | return false; |
| 107 | } |
| 108 | |
| 109 | const uLong srcSizeULong = static_cast<uLong>( srcSize ); |
| 110 | if ( srcSizeULong != srcSize ) { |
| 111 | ERROR_LOG( "The size of the compressed data is too large" ) |
| 112 | return false; |
| 113 | } |
| 114 | |
| 115 | uLong dstSizeULong = static_cast<uLong>( dstSize ); |
| 116 | if ( dstSizeULong != dstSize ) { |
| 117 | ERROR_LOG( "The size of the decompressed data is too large" ) |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | const int ret = uncompress( dst, &dstSizeULong, src, srcSizeULong ); |
| 122 | if ( ret != Z_OK ) { |
| 123 | ERROR_LOG( "zlib error: " << ret ) |
| 124 | return false; |
| 125 | } |
| 126 | |
| 127 | return true; |
| 128 | } |
| 129 | |
| 130 | std::vector<uint8_t> Compression::zipData( const uint8_t * src, const size_t srcSize, const bool isMaximumCompression ) |
| 131 | { |
| 132 | if ( src == nullptr || srcSize == 0 ) { |
| 133 | return {}; |
| 134 | } |
| 135 | |
| 136 | const uLong srcSizeULong = static_cast<uLong>( srcSize ); |
| 137 | if ( srcSizeULong != srcSize ) { |
| 138 | ERROR_LOG( "The size of the source data is too large" ) |
| 139 | return {}; |
| 140 | } |
| 141 | |
| 142 | std::vector<uint8_t> res( compressBound( srcSizeULong ) ); |
| 143 | |
| 144 | uLong dstSizeULong = static_cast<uLong>( res.size() ); |
| 145 | if ( dstSizeULong != res.size() ) { |
| 146 | ERROR_LOG( "The size of the compressed data is too large" ) |
| 147 | return {}; |
| 148 | } |
| 149 | |
| 150 | const int ret = compress2( res.data(), &dstSizeULong, src, srcSizeULong, isMaximumCompression ? Z_BEST_COMPRESSION : Z_DEFAULT_COMPRESSION ); |
| 151 | |
| 152 | if ( ret != Z_OK ) { |
| 153 | ERROR_LOG( "zlib error: " << ret ) |
| 154 | return {}; |
| 155 | } |
| 156 | |
| 157 | res.resize( dstSizeULong ); |
| 158 | |
| 159 | return res; |
| 160 | } |
| 161 | |
| 162 | bool Compression::unzipStream( IStreamBase & inputStream, OStreamBase & outputStream ) |
| 163 | { |
| 164 | const uint32_t rawSize = inputStream.get32(); |
| 165 | const uint32_t zipSize = inputStream.get32(); |
| 166 | if ( zipSize == 0 ) { |
| 167 | return false; |
| 168 | } |
| 169 | |
| 170 | const uint16_t version = inputStream.get16(); |
| 171 | if ( version != FORMAT_VERSION_0 ) { |
| 172 | return false; |
| 173 | } |
| 174 | |
| 175 | inputStream.skip( 2 ); // Unused bytes |
| 176 | |
| 177 | const std::vector<uint8_t> zip = inputStream.getRaw( zipSize ); |
| 178 | const std::vector<uint8_t> raw = unzipData( zip.data(), zip.size(), rawSize ); |
| 179 | if ( raw.size() != rawSize ) { |
| 180 | return false; |
| 181 | } |
| 182 | |
| 183 | outputStream.putRaw( raw.data(), raw.size() ); |
| 184 | |
| 185 | return !outputStream.fail(); |
| 186 | } |
| 187 | |
| 188 | bool Compression::zipStreamBuf( const IStreamBuf & inputStream, OStreamBase & outputStream ) |
| 189 | { |
| 190 | const std::vector<uint8_t> zip = zipData( inputStream.data(), inputStream.size(), false ); |
| 191 | if ( zip.empty() ) { |
| 192 | return false; |
| 193 | } |
| 194 | |
| 195 | outputStream.put32( static_cast<uint32_t>( inputStream.size() ) ); |
| 196 | outputStream.put32( static_cast<uint32_t>( zip.size() ) ); |
| 197 | outputStream.put16( FORMAT_VERSION_0 ); |
| 198 | outputStream.put16( 0 ); // Unused bytes |
| 199 | outputStream.putRaw( zip.data(), zip.size() ); |
| 200 | |
| 201 | return !outputStream.fail(); |
| 202 | } |
| 203 | |
| 204 | fheroes2::Image Compression::CreateImageFromZlib( int32_t width, int32_t height, const uint8_t * imageData, size_t imageSize, bool doubleLayer ) |
| 205 | { |
| 206 | if ( imageData == nullptr || imageSize == 0 || width <= 0 || height <= 0 ) { |
| 207 | return {}; |
| 208 | } |
| 209 | |
| 210 | const std::vector<uint8_t> & uncompressedData = unzipData( imageData, imageSize ); |
| 211 | if ( doubleLayer && ( uncompressedData.size() & 1 ) == 1 ) { |
| 212 | return {}; |
| 213 | } |
| 214 | |
| 215 | const size_t uncompressedSize = doubleLayer ? uncompressedData.size() / 2 : uncompressedData.size(); |
| 216 | |
| 217 | if ( static_cast<size_t>( width ) * height != uncompressedSize ) { |
| 218 | return {}; |
| 219 | } |
| 220 | |
| 221 | fheroes2::Image out; |
| 222 | if ( !doubleLayer ) { |
| 223 | out._disableTransformLayer(); |
| 224 | } |
| 225 | out.resize( width, height ); |
| 226 | |
| 227 | std::memcpy( out.image(), uncompressedData.data(), uncompressedSize ); |
| 228 | if ( doubleLayer ) { |
| 229 | std::memcpy( out.transform(), uncompressedData.data() + uncompressedSize, uncompressedSize ); |
| 230 | } |
| 231 | return out; |
| 232 | } |