| 1 | // Copyright 2020 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DiscIO/WIACompression.h" |
| 5 | |
| 6 | #include <algorithm> |
| 7 | #include <cstddef> |
| 8 | #include <cstring> |
| 9 | #include <memory> |
| 10 | #include <optional> |
| 11 | #include <vector> |
| 12 | |
| 13 | #include <bzlib.h> |
| 14 | #include <lzma.h> |
| 15 | #include <zstd.h> |
| 16 | |
| 17 | #include "Common/Assert.h" |
| 18 | #include "Common/CommonTypes.h" |
| 19 | #include "Common/MathUtil.h" |
| 20 | #include "Common/Swap.h" |
| 21 | #include "DiscIO/LaggedFibonacciGenerator.h" |
| 22 | |
| 23 | namespace DiscIO |
| 24 | { |
| 25 | static u32 LZMA2DictionarySize(u8 p) |
| 26 | { |
| 27 | return (static_cast<u32>(2) | (p & 1)) << (p / 2 + 11); |
| 28 | } |
| 29 | |
| 30 | Decompressor::~Decompressor() = default; |
| 31 | |
| 32 | bool NoneDecompressor::Decompress(const DecompressionBuffer& in, DecompressionBuffer* out, |
| 33 | size_t* in_bytes_read) |
| 34 | { |
| 35 | const size_t length = |
| 36 | std::min(in.bytes_written - *in_bytes_read, out->data.size() - out->bytes_written); |
| 37 | |
| 38 | std::memcpy(out->data.data() + out->bytes_written, in.data.data() + *in_bytes_read, length); |
| 39 | |
| 40 | *in_bytes_read += length; |
| 41 | out->bytes_written += length; |
| 42 | |
| 43 | m_done = in.data.size() == *in_bytes_read; |
| 44 | return true; |
| 45 | } |
| 46 | |
| 47 | PurgeDecompressor::PurgeDecompressor(u64 decompressed_size) : m_decompressed_size(decompressed_size) |
| 48 | { |
| 49 | } |
| 50 | |
| 51 | bool PurgeDecompressor::Decompress(const DecompressionBuffer& in, DecompressionBuffer* out, |
| 52 | size_t* in_bytes_read) |
| 53 | { |
| 54 | if (!m_started) |
| 55 | { |
| 56 | m_sha1_context = Common::SHA1::CreateContext(); |
| 57 | |
| 58 | // Include the exception lists in the SHA-1 calculation (but not in the compression...) |
| 59 | m_sha1_context->Update(in.data.data(), *in_bytes_read); |
| 60 | |
| 61 | m_started = true; |
| 62 | } |
| 63 | |
| 64 | while (!m_done && in.bytes_written != *in_bytes_read && |
| 65 | (m_segment_bytes_written < sizeof(m_segment) || out->data.size() != out->bytes_written)) |
| 66 | { |
| 67 | if (m_segment_bytes_written == 0 && *in_bytes_read == in.data.size() - Common::SHA1::DIGEST_LEN) |
| 68 | { |
| 69 | const size_t zeroes_to_write = std::min<size_t>(m_decompressed_size - m_out_bytes_written, |
| 70 | out->data.size() - out->bytes_written); |
| 71 | |
| 72 | std::memset(out->data.data() + out->bytes_written, 0, zeroes_to_write); |
| 73 | |
| 74 | out->bytes_written += zeroes_to_write; |
| 75 | m_out_bytes_written += zeroes_to_write; |
| 76 | |
| 77 | if (m_out_bytes_written == m_decompressed_size && in.bytes_written == in.data.size()) |
| 78 | { |
| 79 | const auto actual_hash = m_sha1_context->Finish(); |
| 80 | |
| 81 | Common::SHA1::Digest expected_hash; |
| 82 | std::memcpy(expected_hash.data(), in.data.data() + *in_bytes_read, expected_hash.size()); |
| 83 | |
| 84 | *in_bytes_read += expected_hash.size(); |
| 85 | m_done = true; |
| 86 | |
| 87 | if (actual_hash != expected_hash) |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | return true; |
| 92 | } |
| 93 | |
| 94 | if (m_segment_bytes_written < sizeof(m_segment)) |
| 95 | { |
| 96 | const size_t bytes_to_copy = |
| 97 | std::min(in.bytes_written - *in_bytes_read, sizeof(m_segment) - m_segment_bytes_written); |
| 98 | |
| 99 | std::memcpy(reinterpret_cast<u8*>(&m_segment) + m_segment_bytes_written, |
| 100 | in.data.data() + *in_bytes_read, bytes_to_copy); |
| 101 | m_sha1_context->Update(in.data.data() + *in_bytes_read, bytes_to_copy); |
| 102 | |
| 103 | *in_bytes_read += bytes_to_copy; |
| 104 | m_bytes_read += bytes_to_copy; |
| 105 | m_segment_bytes_written += bytes_to_copy; |
| 106 | } |
| 107 | |
| 108 | if (m_segment_bytes_written < sizeof(m_segment)) |
| 109 | return true; |
| 110 | |
| 111 | const size_t offset = Common::swap32(m_segment.offset); |
| 112 | const size_t size = Common::swap32(m_segment.size); |
| 113 | |
| 114 | if (m_out_bytes_written < offset) |
| 115 | { |
| 116 | const size_t zeroes_to_write = |
| 117 | std::min(offset - m_out_bytes_written, out->data.size() - out->bytes_written); |
| 118 | |
| 119 | std::memset(out->data.data() + out->bytes_written, 0, zeroes_to_write); |
| 120 | |
| 121 | out->bytes_written += zeroes_to_write; |
| 122 | m_out_bytes_written += zeroes_to_write; |
| 123 | } |
| 124 | |
| 125 | if (m_out_bytes_written >= offset && m_out_bytes_written < offset + size) |
| 126 | { |
| 127 | const size_t bytes_to_copy = std::min( |
| 128 | std::min(offset + size - m_out_bytes_written, out->data.size() - out->bytes_written), |
| 129 | in.bytes_written - *in_bytes_read); |
| 130 | |
| 131 | std::memcpy(out->data.data() + out->bytes_written, in.data.data() + *in_bytes_read, |
| 132 | bytes_to_copy); |
| 133 | m_sha1_context->Update(in.data.data() + *in_bytes_read, bytes_to_copy); |
| 134 | |
| 135 | *in_bytes_read += bytes_to_copy; |
| 136 | m_bytes_read += bytes_to_copy; |
| 137 | out->bytes_written += bytes_to_copy; |
| 138 | m_out_bytes_written += bytes_to_copy; |
| 139 | } |
| 140 | |
| 141 | if (m_out_bytes_written >= offset + size) |
| 142 | m_segment_bytes_written = 0; |
| 143 | } |
| 144 | |
| 145 | return true; |
| 146 | } |
| 147 | |
| 148 | Bzip2Decompressor::~Bzip2Decompressor() |
| 149 | { |
| 150 | if (m_started) |
| 151 | BZ2_bzDecompressEnd(&m_stream); |
| 152 | } |
| 153 | |
| 154 | bool Bzip2Decompressor::Decompress(const DecompressionBuffer& in, DecompressionBuffer* out, |
| 155 | size_t* in_bytes_read) |
| 156 | { |
| 157 | if (!m_started) |
| 158 | { |
| 159 | if (BZ2_bzDecompressInit(&m_stream, 0, 0) != BZ_OK) |
| 160 | return false; |
| 161 | |
| 162 | m_started = true; |
| 163 | } |
| 164 | |
| 165 | char* const in_ptr = reinterpret_cast<char*>(const_cast<u8*>(in.data.data() + *in_bytes_read)); |
| 166 | m_stream.next_in = in_ptr; |
| 167 | m_stream.avail_in = MathUtil::SaturatingCast<u32>(in.bytes_written - *in_bytes_read); |
| 168 | |
| 169 | char* const out_ptr = reinterpret_cast<char*>(out->data.data() + out->bytes_written); |
| 170 | m_stream.next_out = out_ptr; |
| 171 | m_stream.avail_out = MathUtil::SaturatingCast<u32>(out->data.size() - out->bytes_written); |
| 172 | |
| 173 | const int result = BZ2_bzDecompress(&m_stream); |
| 174 | |
| 175 | *in_bytes_read += m_stream.next_in - in_ptr; |
| 176 | out->bytes_written += m_stream.next_out - out_ptr; |
| 177 | |
| 178 | m_done = result == BZ_STREAM_END; |
| 179 | return result == BZ_OK || result == BZ_STREAM_END; |
| 180 | } |
| 181 | |
| 182 | LZMADecompressor::LZMADecompressor(bool lzma2, const u8* filter_options, size_t filter_options_size) |
| 183 | { |
| 184 | m_options.preset_dict = nullptr; |
| 185 | |
| 186 | if (!lzma2 && filter_options_size == 5) |
| 187 | { |
| 188 | // The dictionary size is stored as a 32-bit little endian unsigned integer |
| 189 | static_assert(sizeof(m_options.dict_size) == sizeof(u32)); |
| 190 | std::memcpy(&m_options.dict_size, filter_options + 1, sizeof(u32)); |
| 191 | |
| 192 | const u8 d = filter_options[0]; |
| 193 | if (d >= (9 * 5 * 5)) |
| 194 | { |
| 195 | m_error_occurred = true; |
| 196 | } |
| 197 | else |
| 198 | { |
| 199 | m_options.lc = d % 9; |
| 200 | const u8 e = d / 9; |
| 201 | m_options.pb = e / 5; |
| 202 | m_options.lp = e % 5; |
| 203 | } |
| 204 | } |
| 205 | else if (lzma2 && filter_options_size == 1) |
| 206 | { |
| 207 | const u8 d = filter_options[0]; |
| 208 | if (d > 40) |
| 209 | m_error_occurred = true; |
| 210 | else |
| 211 | m_options.dict_size = d == 40 ? 0xFFFFFFFF : LZMA2DictionarySize(d); |
| 212 | } |
| 213 | else |
| 214 | { |
| 215 | m_error_occurred = true; |
| 216 | } |
| 217 | |
| 218 | m_filters[0].id = lzma2 ? LZMA_FILTER_LZMA2 : LZMA_FILTER_LZMA1; |
| 219 | m_filters[0].options = &m_options; |
| 220 | m_filters[1].id = LZMA_VLI_UNKNOWN; |
| 221 | m_filters[1].options = nullptr; |
| 222 | } |
| 223 | |
| 224 | LZMADecompressor::~LZMADecompressor() |
| 225 | { |
| 226 | if (m_started) |
| 227 | lzma_end(&m_stream); |
| 228 | } |
| 229 | |
| 230 | bool LZMADecompressor::Decompress(const DecompressionBuffer& in, DecompressionBuffer* out, |
| 231 | size_t* in_bytes_read) |
| 232 | { |
| 233 | if (!m_started) |
| 234 | { |
| 235 | if (m_error_occurred || lzma_raw_decoder(&m_stream, m_filters) != LZMA_OK) |
| 236 | return false; |
| 237 | |
| 238 | m_started = true; |
| 239 | } |
| 240 | |
| 241 | const u8* const in_ptr = in.data.data() + *in_bytes_read; |
| 242 | m_stream.next_in = in_ptr; |
| 243 | m_stream.avail_in = in.bytes_written - *in_bytes_read; |
| 244 | |
| 245 | u8* const out_ptr = out->data.data() + out->bytes_written; |
| 246 | m_stream.next_out = out_ptr; |
| 247 | m_stream.avail_out = out->data.size() - out->bytes_written; |
| 248 | |
| 249 | const lzma_ret result = lzma_code(&m_stream, LZMA_RUN); |
| 250 | |
| 251 | *in_bytes_read += m_stream.next_in - in_ptr; |
| 252 | out->bytes_written += m_stream.next_out - out_ptr; |
| 253 | |
| 254 | m_done = result == LZMA_STREAM_END; |
| 255 | return result == LZMA_OK || result == LZMA_STREAM_END; |
| 256 | } |
| 257 | |
| 258 | ZstdDecompressor::ZstdDecompressor() |
| 259 | { |
| 260 | m_stream = ZSTD_createDStream(); |
| 261 | } |
| 262 | |
| 263 | ZstdDecompressor::~ZstdDecompressor() |
| 264 | { |
| 265 | ZSTD_freeDStream(m_stream); |
| 266 | } |
| 267 | |
| 268 | bool ZstdDecompressor::Decompress(const DecompressionBuffer& in, DecompressionBuffer* out, |
| 269 | size_t* in_bytes_read) |
| 270 | { |
| 271 | if (!m_stream) |
| 272 | return false; |
| 273 | |
| 274 | ZSTD_inBuffer in_buffer{in.data.data(), in.bytes_written, *in_bytes_read}; |
| 275 | ZSTD_outBuffer out_buffer{out->data.data(), out->data.size(), out->bytes_written}; |
| 276 | |
| 277 | const size_t result = ZSTD_decompressStream(m_stream, &out_buffer, &in_buffer); |
| 278 | |
| 279 | *in_bytes_read = in_buffer.pos; |
| 280 | out->bytes_written = out_buffer.pos; |
| 281 | |
| 282 | m_done = result == 0; |
| 283 | return !ZSTD_isError(result); |
| 284 | } |
| 285 | |
| 286 | RVZPackDecompressor::RVZPackDecompressor(std::unique_ptr<Decompressor> decompressor, |
| 287 | DecompressionBuffer decompressed, u64 data_offset, |
| 288 | u32 rvz_packed_size) |
| 289 | : m_decompressor(std::move(decompressor)), m_decompressed(std::move(decompressed)), |
| 290 | m_data_offset(data_offset), m_rvz_packed_size(rvz_packed_size) |
| 291 | { |
| 292 | m_bytes_read = m_decompressed.bytes_written; |
| 293 | } |
| 294 | |
| 295 | bool RVZPackDecompressor::IncrementBytesRead(size_t x) |
| 296 | { |
| 297 | m_bytes_read += x; |
| 298 | return m_bytes_read <= m_rvz_packed_size; |
| 299 | } |
| 300 | |
| 301 | std::optional<bool> RVZPackDecompressor::ReadToDecompressed(const DecompressionBuffer& in, |
| 302 | size_t* in_bytes_read, |
| 303 | size_t decompressed_bytes_read, |
| 304 | size_t bytes_to_read) |
| 305 | { |
| 306 | if (m_decompressed.data.size() < decompressed_bytes_read + bytes_to_read) |
| 307 | m_decompressed.data.resize(decompressed_bytes_read + bytes_to_read); |
| 308 | |
| 309 | if (m_decompressed.bytes_written < decompressed_bytes_read + bytes_to_read) |
| 310 | { |
| 311 | const size_t prev_bytes_written = m_decompressed.bytes_written; |
| 312 | |
| 313 | if (!m_decompressor->Decompress(in, &m_decompressed, in_bytes_read)) |
| 314 | return false; |
| 315 | |
| 316 | if (!IncrementBytesRead(m_decompressed.bytes_written - prev_bytes_written)) |
| 317 | return false; |
| 318 | |
| 319 | if (m_decompressed.bytes_written < decompressed_bytes_read + bytes_to_read) |
| 320 | return true; |
| 321 | } |
| 322 | |
| 323 | return std::nullopt; |
| 324 | } |
| 325 | |
| 326 | bool RVZPackDecompressor::Decompress(const DecompressionBuffer& in, DecompressionBuffer* out, |
| 327 | size_t* in_bytes_read) |
| 328 | { |
| 329 | while (out->data.size() != out->bytes_written && !Done()) |
| 330 | { |
| 331 | if (m_size == 0) |
| 332 | { |
| 333 | if (m_decompressed.bytes_written == m_decompressed_bytes_read) |
| 334 | { |
| 335 | m_decompressed.data.resize(sizeof(u32)); |
| 336 | m_decompressed.bytes_written = 0; |
| 337 | m_decompressed_bytes_read = 0; |
| 338 | } |
| 339 | |
| 340 | std::optional<bool> result = |
| 341 | ReadToDecompressed(in, in_bytes_read, m_decompressed_bytes_read, sizeof(u32)); |
| 342 | if (result) |
| 343 | return *result; |
| 344 | |
| 345 | const u32 size = Common::swap32(m_decompressed.data.data() + m_decompressed_bytes_read); |
| 346 | |
| 347 | m_junk = size & 0x80000000; |
| 348 | if (m_junk) |
| 349 | { |
| 350 | constexpr size_t SEED_SIZE = LaggedFibonacciGenerator::SEED_SIZE * sizeof(u32); |
| 351 | constexpr size_t BLOCK_SIZE = 0x8000; |
| 352 | |
| 353 | result = ReadToDecompressed(in, in_bytes_read, m_decompressed_bytes_read + sizeof(u32), |
| 354 | SEED_SIZE); |
| 355 | if (result) |
| 356 | return *result; |
| 357 | |
| 358 | m_lfg.SetSeed(m_decompressed.data.data() + m_decompressed_bytes_read + sizeof(u32)); |
| 359 | m_lfg.Forward(m_data_offset % BLOCK_SIZE); |
| 360 | |
| 361 | m_decompressed_bytes_read += SEED_SIZE; |
| 362 | } |
| 363 | |
| 364 | m_decompressed_bytes_read += sizeof(u32); |
| 365 | m_size = size & 0x7FFFFFFF; |
| 366 | } |
| 367 | |
| 368 | size_t bytes_to_write = std::min<size_t>(m_size, out->data.size() - out->bytes_written); |
| 369 | if (m_junk) |
| 370 | { |
| 371 | m_lfg.GetBytes(bytes_to_write, out->data.data() + out->bytes_written); |
| 372 | out->bytes_written += bytes_to_write; |
| 373 | } |
| 374 | else |
| 375 | { |
| 376 | if (m_decompressed.bytes_written != m_decompressed_bytes_read) |
| 377 | { |
| 378 | bytes_to_write = |
| 379 | std::min(bytes_to_write, m_decompressed.bytes_written - m_decompressed_bytes_read); |
| 380 | |
| 381 | std::memcpy(out->data.data() + out->bytes_written, |
| 382 | m_decompressed.data.data() + m_decompressed_bytes_read, bytes_to_write); |
| 383 | |
| 384 | m_decompressed_bytes_read += bytes_to_write; |
| 385 | out->bytes_written += bytes_to_write; |
| 386 | } |
| 387 | else |
| 388 | { |
| 389 | const size_t prev_out_bytes_written = out->bytes_written; |
| 390 | const size_t old_out_size = out->data.size(); |
| 391 | const size_t new_out_size = out->bytes_written + bytes_to_write; |
| 392 | |
| 393 | if (new_out_size < old_out_size) |
| 394 | out->data.resize(new_out_size); |
| 395 | |
| 396 | if (!m_decompressor->Decompress(in, out, in_bytes_read)) |
| 397 | return false; |
| 398 | |
| 399 | out->data.resize(old_out_size); |
| 400 | |
| 401 | bytes_to_write = out->bytes_written - prev_out_bytes_written; |
| 402 | |
| 403 | if (!IncrementBytesRead(bytes_to_write)) |
| 404 | return false; |
| 405 | |
| 406 | if (bytes_to_write == 0) |
| 407 | return true; |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | m_data_offset += bytes_to_write; |
| 412 | m_size -= static_cast<u32>(bytes_to_write); |
| 413 | } |
| 414 | |
| 415 | // If out is full but not all data has been read from in, give the decompressor a chance to read |
| 416 | // from in anyway. This is needed for the case where zstd has read everything except the checksum. |
| 417 | if (out->data.size() == out->bytes_written && in.bytes_written != *in_bytes_read) |
| 418 | { |
| 419 | if (!m_decompressor->Decompress(in, out, in_bytes_read)) |
| 420 | return false; |
| 421 | } |
| 422 | |
| 423 | return true; |
| 424 | } |
| 425 | |
| 426 | bool RVZPackDecompressor::Done() const |
| 427 | { |
| 428 | return m_size == 0 && m_rvz_packed_size == m_bytes_read && |
| 429 | m_decompressed.bytes_written == m_decompressed_bytes_read && m_decompressor->Done(); |
| 430 | } |
| 431 | |
| 432 | Compressor::~Compressor() = default; |
| 433 | |
| 434 | PurgeCompressor::PurgeCompressor() = default; |
| 435 | |
| 436 | PurgeCompressor::~PurgeCompressor() = default; |
| 437 | |
| 438 | bool PurgeCompressor::Start(std::optional<u64> size) |
| 439 | { |
| 440 | m_buffer.clear(); |
| 441 | m_bytes_written = 0; |
| 442 | |
| 443 | m_sha1_context = Common::SHA1::CreateContext(); |
| 444 | |
| 445 | return true; |
| 446 | } |
| 447 | |
| 448 | bool PurgeCompressor::AddPrecedingDataOnlyForPurgeHashing(const u8* data, size_t size) |
| 449 | { |
| 450 | m_sha1_context->Update(data, size); |
| 451 | return true; |
| 452 | } |
| 453 | |
| 454 | bool PurgeCompressor::Compress(const u8* data, size_t size) |
| 455 | { |
| 456 | // We could add support for calling this twice if we're fine with |
| 457 | // making the code more complicated, but there's no need to support it |
| 458 | ASSERT_MSG(DISCIO, m_bytes_written == 0, |
| 459 | "Calling PurgeCompressor::Compress() twice is not supported"); |
| 460 | |
| 461 | m_buffer.resize(size + sizeof(PurgeSegment) + Common::SHA1::DIGEST_LEN); |
| 462 | |
| 463 | size_t bytes_read = 0; |
| 464 | |
| 465 | while (true) |
| 466 | { |
| 467 | const auto first_non_zero = |
| 468 | std::find_if(data + bytes_read, data + size, [](u8 x) { return x != 0; }); |
| 469 | |
| 470 | const u32 non_zero_data_start = static_cast<u32>(first_non_zero - data); |
| 471 | if (non_zero_data_start == size) |
| 472 | break; |
| 473 | |
| 474 | size_t non_zero_data_end = non_zero_data_start; |
| 475 | size_t sequence_length = 0; |
| 476 | for (size_t i = non_zero_data_start; i < size; ++i) |
| 477 | { |
| 478 | if (data[i] == 0) |
| 479 | { |
| 480 | ++sequence_length; |
| 481 | } |
| 482 | else |
| 483 | { |
| 484 | sequence_length = 0; |
| 485 | non_zero_data_end = i + 1; |
| 486 | } |
| 487 | |
| 488 | // To avoid wasting space, only count runs of zeroes that are of a certain length |
| 489 | // (unless there is nothing after the run of zeroes, then we might as well always count it) |
| 490 | if (sequence_length > sizeof(PurgeSegment)) |
| 491 | break; |
| 492 | } |
| 493 | |
| 494 | const u32 non_zero_data_length = static_cast<u32>(non_zero_data_end - non_zero_data_start); |
| 495 | |
| 496 | const PurgeSegment segment{Common::swap32(non_zero_data_start), |
| 497 | Common::swap32(non_zero_data_length)}; |
| 498 | std::memcpy(m_buffer.data() + m_bytes_written, &segment, sizeof(segment)); |
| 499 | m_bytes_written += sizeof(segment); |
| 500 | |
| 501 | std::memcpy(m_buffer.data() + m_bytes_written, data + non_zero_data_start, |
| 502 | non_zero_data_length); |
| 503 | m_bytes_written += non_zero_data_length; |
| 504 | |
| 505 | bytes_read = non_zero_data_end; |
| 506 | } |
| 507 | |
| 508 | return true; |
| 509 | } |
| 510 | |
| 511 | bool PurgeCompressor::End() |
| 512 | { |
| 513 | m_sha1_context->Update(m_buffer.data(), m_bytes_written); |
| 514 | |
| 515 | const auto digest = m_sha1_context->Finish(); |
| 516 | std::memcpy(m_buffer.data() + m_bytes_written, digest.data(), sizeof(digest)); |
| 517 | |
| 518 | m_bytes_written += sizeof(digest); |
| 519 | |
| 520 | ASSERT(m_bytes_written <= m_buffer.size()); |
| 521 | |
| 522 | return true; |
| 523 | } |
| 524 | |
| 525 | const u8* PurgeCompressor::GetData() const |
| 526 | { |
| 527 | return m_buffer.data(); |
| 528 | } |
| 529 | |
| 530 | size_t PurgeCompressor::GetSize() const |
| 531 | { |
| 532 | return m_bytes_written; |
| 533 | } |
| 534 | |
| 535 | Bzip2Compressor::Bzip2Compressor(int compression_level) : m_compression_level(compression_level) |
| 536 | { |
| 537 | } |
| 538 | |
| 539 | Bzip2Compressor::~Bzip2Compressor() |
| 540 | { |
| 541 | BZ2_bzCompressEnd(&m_stream); |
| 542 | } |
| 543 | |
| 544 | bool Bzip2Compressor::Start(std::optional<u64> size) |
| 545 | { |
| 546 | ASSERT_MSG(DISCIO, m_stream.state == nullptr, |
| 547 | "Called Bzip2Compressor::Start() twice without calling Bzip2Compressor::End()"); |
| 548 | |
| 549 | m_buffer.clear(); |
| 550 | m_stream.next_out = reinterpret_cast<char*>(m_buffer.data()); |
| 551 | |
| 552 | return BZ2_bzCompressInit(&m_stream, m_compression_level, 0, 0) == BZ_OK; |
| 553 | } |
| 554 | |
| 555 | bool Bzip2Compressor::Compress(const u8* data, size_t size) |
| 556 | { |
| 557 | m_stream.next_in = reinterpret_cast<char*>(const_cast<u8*>(data)); |
| 558 | m_stream.avail_in = static_cast<unsigned int>(size); |
| 559 | |
| 560 | ExpandBuffer(size); |
| 561 | |
| 562 | while (m_stream.avail_in != 0) |
| 563 | { |
| 564 | if (m_stream.avail_out == 0) |
| 565 | ExpandBuffer(0x100); |
| 566 | |
| 567 | if (BZ2_bzCompress(&m_stream, BZ_RUN) != BZ_RUN_OK) |
| 568 | return false; |
| 569 | } |
| 570 | |
| 571 | return true; |
| 572 | } |
| 573 | |
| 574 | bool Bzip2Compressor::End() |
| 575 | { |
| 576 | bool success = true; |
| 577 | |
| 578 | while (true) |
| 579 | { |
| 580 | if (m_stream.avail_out == 0) |
| 581 | ExpandBuffer(0x100); |
| 582 | |
| 583 | const int result = BZ2_bzCompress(&m_stream, BZ_FINISH); |
| 584 | if (result != BZ_FINISH_OK && result != BZ_STREAM_END) |
| 585 | success = false; |
| 586 | if (result != BZ_FINISH_OK) |
| 587 | break; |
| 588 | } |
| 589 | |
| 590 | if (BZ2_bzCompressEnd(&m_stream) != BZ_OK) |
| 591 | success = false; |
| 592 | |
| 593 | return success; |
| 594 | } |
| 595 | |
| 596 | void Bzip2Compressor::ExpandBuffer(size_t bytes_to_add) |
| 597 | { |
| 598 | const size_t bytes_written = GetSize(); |
| 599 | m_buffer.resize(m_buffer.size() + bytes_to_add); |
| 600 | m_stream.next_out = reinterpret_cast<char*>(m_buffer.data()) + bytes_written; |
| 601 | m_stream.avail_out = static_cast<unsigned int>(m_buffer.size() - bytes_written); |
| 602 | } |
| 603 | |
| 604 | const u8* Bzip2Compressor::GetData() const |
| 605 | { |
| 606 | return m_buffer.data(); |
| 607 | } |
| 608 | |
| 609 | size_t Bzip2Compressor::GetSize() const |
| 610 | { |
| 611 | return static_cast<size_t>(reinterpret_cast<u8*>(m_stream.next_out) - m_buffer.data()); |
| 612 | } |
| 613 | |
| 614 | LZMACompressor::LZMACompressor(bool lzma2, int compression_level, u8 compressor_data_out[7], |
| 615 | u8* compressor_data_size_out) |
| 616 | { |
| 617 | // lzma_lzma_preset returns false on success for some reason |
| 618 | if (lzma_lzma_preset(&m_options, static_cast<uint32_t>(compression_level))) |
| 619 | { |
| 620 | m_initialization_failed = true; |
| 621 | return; |
| 622 | } |
| 623 | |
| 624 | if (!lzma2) |
| 625 | { |
| 626 | if (compressor_data_size_out) |
| 627 | *compressor_data_size_out = 5; |
| 628 | |
| 629 | if (compressor_data_out) |
| 630 | { |
| 631 | ASSERT(m_options.lc < 9); |
| 632 | ASSERT(m_options.lp < 5); |
| 633 | ASSERT(m_options.pb < 5); |
| 634 | compressor_data_out[0] = |
| 635 | static_cast<u8>((m_options.pb * 5 + m_options.lp) * 9 + m_options.lc); |
| 636 | |
| 637 | // The dictionary size is stored as a 32-bit little endian unsigned integer |
| 638 | static_assert(sizeof(m_options.dict_size) == sizeof(u32)); |
| 639 | std::memcpy(compressor_data_out + 1, &m_options.dict_size, sizeof(u32)); |
| 640 | } |
| 641 | } |
| 642 | else |
| 643 | { |
| 644 | if (compressor_data_size_out) |
| 645 | *compressor_data_size_out = 1; |
| 646 | |
| 647 | if (compressor_data_out) |
| 648 | { |
| 649 | u8 encoded_dict_size = 0; |
| 650 | while (encoded_dict_size < 40 && m_options.dict_size > LZMA2DictionarySize(encoded_dict_size)) |
| 651 | ++encoded_dict_size; |
| 652 | |
| 653 | compressor_data_out[0] = encoded_dict_size; |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | m_filters[0].id = lzma2 ? LZMA_FILTER_LZMA2 : LZMA_FILTER_LZMA1; |
| 658 | m_filters[0].options = &m_options; |
| 659 | m_filters[1].id = LZMA_VLI_UNKNOWN; |
| 660 | m_filters[1].options = nullptr; |
| 661 | } |
| 662 | |
| 663 | LZMACompressor::~LZMACompressor() |
| 664 | { |
| 665 | lzma_end(&m_stream); |
| 666 | } |
| 667 | |
| 668 | bool LZMACompressor::Start(std::optional<u64> size) |
| 669 | { |
| 670 | if (m_initialization_failed) |
| 671 | return false; |
| 672 | |
| 673 | m_buffer.clear(); |
| 674 | m_stream.next_out = m_buffer.data(); |
| 675 | |
| 676 | return lzma_raw_encoder(&m_stream, m_filters) == LZMA_OK; |
| 677 | } |
| 678 | |
| 679 | bool LZMACompressor::Compress(const u8* data, size_t size) |
| 680 | { |
| 681 | m_stream.next_in = data; |
| 682 | m_stream.avail_in = size; |
| 683 | |
| 684 | ExpandBuffer(size); |
| 685 | |
| 686 | while (m_stream.avail_in != 0) |
| 687 | { |
| 688 | if (m_stream.avail_out == 0) |
| 689 | ExpandBuffer(0x100); |
| 690 | |
| 691 | if (lzma_code(&m_stream, LZMA_RUN) != LZMA_OK) |
| 692 | return false; |
| 693 | } |
| 694 | |
| 695 | return true; |
| 696 | } |
| 697 | |
| 698 | bool LZMACompressor::End() |
| 699 | { |
| 700 | while (true) |
| 701 | { |
| 702 | if (m_stream.avail_out == 0) |
| 703 | ExpandBuffer(0x100); |
| 704 | |
| 705 | switch (lzma_code(&m_stream, LZMA_FINISH)) |
| 706 | { |
| 707 | case LZMA_OK: |
| 708 | break; |
| 709 | case LZMA_STREAM_END: |
| 710 | return true; |
| 711 | default: |
| 712 | return false; |
| 713 | } |
| 714 | } |
| 715 | } |
| 716 | |
| 717 | void LZMACompressor::ExpandBuffer(size_t bytes_to_add) |
| 718 | { |
| 719 | const size_t bytes_written = GetSize(); |
| 720 | m_buffer.resize(m_buffer.size() + bytes_to_add); |
| 721 | m_stream.next_out = m_buffer.data() + bytes_written; |
| 722 | m_stream.avail_out = m_buffer.size() - bytes_written; |
| 723 | } |
| 724 | |
| 725 | const u8* LZMACompressor::GetData() const |
| 726 | { |
| 727 | return m_buffer.data(); |
| 728 | } |
| 729 | |
| 730 | size_t LZMACompressor::GetSize() const |
| 731 | { |
| 732 | return static_cast<size_t>(m_stream.next_out - m_buffer.data()); |
| 733 | } |
| 734 | |
| 735 | ZstdCompressor::ZstdCompressor(int compression_level) |
| 736 | { |
| 737 | m_stream = ZSTD_createCStream(); |
| 738 | |
| 739 | if (ZSTD_isError(ZSTD_CCtx_setParameter(m_stream, ZSTD_c_compressionLevel, compression_level)) || |
| 740 | ZSTD_isError(ZSTD_CCtx_setParameter(m_stream, ZSTD_c_contentSizeFlag, 0))) |
| 741 | { |
| 742 | m_stream = nullptr; |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | ZstdCompressor::~ZstdCompressor() |
| 747 | { |
| 748 | ZSTD_freeCStream(m_stream); |
| 749 | } |
| 750 | |
| 751 | bool ZstdCompressor::Start(std::optional<u64> size) |
| 752 | { |
| 753 | if (!m_stream) |
| 754 | return false; |
| 755 | |
| 756 | m_buffer.clear(); |
| 757 | m_out_buffer = {}; |
| 758 | |
| 759 | if (ZSTD_isError(ZSTD_CCtx_reset(m_stream, ZSTD_reset_session_only))) |
| 760 | return false; |
| 761 | |
| 762 | if (size) |
| 763 | { |
| 764 | if (ZSTD_isError(ZSTD_CCtx_setPledgedSrcSize(m_stream, *size))) |
| 765 | return false; |
| 766 | } |
| 767 | |
| 768 | return true; |
| 769 | } |
| 770 | |
| 771 | bool ZstdCompressor::Compress(const u8* data, size_t size) |
| 772 | { |
| 773 | ZSTD_inBuffer in_buffer{data, size, 0}; |
| 774 | |
| 775 | ExpandBuffer(size); |
| 776 | |
| 777 | while (in_buffer.size != in_buffer.pos) |
| 778 | { |
| 779 | if (m_out_buffer.size == m_out_buffer.pos) |
| 780 | ExpandBuffer(0x100); |
| 781 | |
| 782 | if (ZSTD_isError(ZSTD_compressStream(m_stream, &m_out_buffer, &in_buffer))) |
| 783 | return false; |
| 784 | } |
| 785 | |
| 786 | return true; |
| 787 | } |
| 788 | |
| 789 | bool ZstdCompressor::End() |
| 790 | { |
| 791 | while (true) |
| 792 | { |
| 793 | if (m_out_buffer.size == m_out_buffer.pos) |
| 794 | ExpandBuffer(0x100); |
| 795 | |
| 796 | const size_t result = ZSTD_endStream(m_stream, &m_out_buffer); |
| 797 | if (ZSTD_isError(result)) |
| 798 | return false; |
| 799 | if (result == 0) |
| 800 | return true; |
| 801 | } |
| 802 | } |
| 803 | |
| 804 | void ZstdCompressor::ExpandBuffer(size_t bytes_to_add) |
| 805 | { |
| 806 | m_buffer.resize(m_buffer.size() + bytes_to_add); |
| 807 | |
| 808 | m_out_buffer.dst = m_buffer.data(); |
| 809 | m_out_buffer.size = m_buffer.size(); |
| 810 | } |
| 811 | |
| 812 | } // namespace DiscIO |