Skip to content

Commit b6404aa

Browse files
committed
use std::span for compression utils
1 parent 9e66e94 commit b6404aa

7 files changed

Lines changed: 19 additions & 40 deletions

File tree

libsave/src/GameTypes/Save/ChunkHelper.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ std::vector<std::byte> SatisfactorySave::decompressChunks(IStreamArchive& inAr)
2222
for (int64_t i = 0; i < size; i++) {
2323
const ChunkInfo& chunk = chunk_list[i];
2424
std::byte* decompressed_buffer_ptr = file_data_blob.data() + chunk.decompressed_offset;
25-
zlibUncompress(decompressed_buffer_ptr, chunk.header.uncompressedSize(), chunk.compressed_chunk.data(),
26-
chunk.compressed_chunk.size());
25+
zlibUncompress({decompressed_buffer_ptr, static_cast<std::size_t>(chunk.header.uncompressedSize())},
26+
chunk.compressed_chunk);
2727
}
2828

2929
return file_data_blob;

libsave/src/IO/OodleUtils.cpp

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,11 @@ void SatisfactorySave::checkOodleLibraryIsLoaded() {
5656
#endif
5757
}
5858

59-
std::vector<std::byte> SatisfactorySave::oodleDecompress(const std::vector<std::byte>& buffer,
60-
std::size_t decompressed_size) {
61-
std::vector<std::byte> buffer_uncompressed(decompressed_size);
62-
oodleDecompress(buffer_uncompressed.data(), decompressed_size, buffer.data(), buffer.size());
63-
return buffer_uncompressed;
64-
}
65-
66-
void SatisfactorySave::oodleDecompress(std::byte* dest, std::size_t destLen, const std::byte* source,
67-
std::size_t sourceLen) {
59+
void SatisfactorySave::oodleDecompress(std::span<std::byte> dest, std::span<const std::byte> source) {
6860
#ifdef _WIN32
69-
auto len = dispatch().OodleLZ_Decompress(source, static_cast<int64_t>(sourceLen), dest,
70-
static_cast<int64_t>(destLen), 1, 0, 0, nullptr, 0, nullptr, nullptr, nullptr, 0, 3);
71-
if (len != destLen) {
61+
auto len = dispatch().OodleLZ_Decompress(source.data(), static_cast<int64_t>(source.size()), dest.data(),
62+
static_cast<int64_t>(dest.size()), 1, 0, 0, nullptr, 0, nullptr, nullptr, nullptr, 0, 3);
63+
if (len != dest.size()) {
7264
throw std::runtime_error("Error decompressing Oodle!");
7365
}
7466
#else

libsave/src/IO/OodleUtils.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
#pragma once
22

33
#include <cstddef>
4-
#include <vector>
4+
#include <span>
55

66
namespace SatisfactorySave {
77
void checkOodleLibraryIsLoaded();
88

9-
std::vector<std::byte> oodleDecompress(const std::vector<std::byte>& buffer, std::size_t decompressed_size);
10-
11-
void oodleDecompress(std::byte* dest, std::size_t destLen, const std::byte* source, std::size_t sourceLen);
9+
void oodleDecompress(std::span<std::byte> dest, std::span<const std::byte> source);
1210
} // namespace SatisfactorySave

libsave/src/IO/ZlibUtils.cpp

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
#include <zlib.h>
66

7-
std::vector<std::byte> SatisfactorySave::zlibCompress(const std::vector<std::byte>& buffer) {
7+
std::vector<std::byte> SatisfactorySave::zlibCompress(std::span<const std::byte> buffer) {
88
uLong size = compressBound(static_cast<uLong>(buffer.size()));
99
std::vector<std::byte> buffer_compressed(static_cast<std::size_t>(size));
1010
int state = ::compress(reinterpret_cast<Bytef*>(buffer_compressed.data()), &size,
@@ -16,19 +16,11 @@ std::vector<std::byte> SatisfactorySave::zlibCompress(const std::vector<std::byt
1616
return buffer_compressed;
1717
}
1818

19-
std::vector<std::byte> SatisfactorySave::zlibUncompress(const std::vector<std::byte>& buffer,
20-
std::size_t uncompressed_size) {
21-
std::vector<std::byte> buffer_uncompressed(uncompressed_size);
22-
zlibUncompress(buffer_uncompressed.data(), uncompressed_size, buffer.data(), buffer.size());
23-
return buffer_uncompressed;
24-
}
25-
26-
void SatisfactorySave::zlibUncompress(std::byte* dest, std::size_t destLen, const std::byte* source,
27-
std::size_t sourceLen) {
28-
auto size = static_cast<uLongf>(destLen);
29-
int state = ::uncompress(reinterpret_cast<Bytef*>(dest), &size, reinterpret_cast<const Bytef*>(source),
30-
static_cast<uLong>(sourceLen));
31-
if (state != Z_OK || size != destLen) {
19+
void SatisfactorySave::zlibUncompress(std::span<std::byte> dest, std::span<const std::byte> source) {
20+
auto size = static_cast<uLongf>(dest.size());
21+
int state = ::uncompress(reinterpret_cast<Bytef*>(dest.data()), &size,
22+
reinterpret_cast<const Bytef*>(source.data()), static_cast<uLong>(source.size()));
23+
if (state != Z_OK || size != dest.size()) {
3224
throw std::runtime_error("Error decompressing buffer with zlib!");
3325
}
3426
}

libsave/src/IO/ZlibUtils.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
#pragma once
22

33
#include <cstddef>
4+
#include <span>
45
#include <vector>
56

67
namespace SatisfactorySave {
8+
std::vector<std::byte> zlibCompress(std::span<const std::byte> buffer);
79

8-
std::vector<std::byte> zlibCompress(const std::vector<std::byte>& buffer);
9-
10-
std::vector<std::byte> zlibUncompress(const std::vector<std::byte>& buffer, std::size_t uncompressed_size);
11-
12-
void zlibUncompress(std::byte* dest, std::size_t destLen, const std::byte* source, std::size_t sourceLen);
13-
10+
void zlibUncompress(std::span<std::byte> dest, std::span<const std::byte> source);
1411
} // namespace SatisfactorySave

libsave/src/Pak/IoStoreFile.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ void SatisfactorySave::IoStoreFile::readCompressionBlock(uint64_t blockIdx, std:
150150
ar.serializeRaw(dst, compBlock.GetCompressedSize());
151151
} else if (compMethod == "Oodle") {
152152
const auto comp_buf = ar.read_buffer(compBlock.GetCompressedSize());
153-
oodleDecompress(dst, compBlock.GetUncompressedSize(), comp_buf.data(), compBlock.GetCompressedSize());
153+
oodleDecompress({dst, compBlock.GetUncompressedSize()}, comp_buf);
154154
} else {
155155
throw std::runtime_error("Unknown compression method: " + compMethod);
156156
}

libsave/src/Pak/PakFile.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ std::vector<std::byte> SatisfactorySave::PakFile::readAssetFileContent(const std
142142
const auto& compressedBuffer = ar.read_buffer(blockSize);
143143
int64_t destLen =
144144
std::min(entry.UncompressedSize - bufferPos, static_cast<int64_t>(entry.CompressionBlockSize));
145-
zlibUncompress(buffer.data() + bufferPos, destLen, compressedBuffer.data(), compressedBuffer.size());
145+
zlibUncompress({buffer.data() + bufferPos, static_cast<std::size_t>(destLen)}, compressedBuffer);
146146
bufferPos += destLen;
147147
}
148148
return buffer;

0 commit comments

Comments
 (0)