Skip to content

Commit

Permalink
Toolchain update
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamFull committed Nov 25, 2023
1 parent b30d90f commit b10f707
Show file tree
Hide file tree
Showing 9 changed files with 12 additions and 174 deletions.
6 changes: 0 additions & 6 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,6 +0,0 @@
[submodule "external/zstd"]
path = external/zstd
url = https://github.com/facebook/zstd.git
[submodule "external/lz4"]
path = external/lz4
url = https://github.com/lz4/lz4.git
13 changes: 6 additions & 7 deletions external/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
cmake_minimum_required(VERSION 3.8)
project(gpak_externals)

find_package(zstd CONFIG REQUIRED)

add_library(${PROJECT_NAME} STATIC gpakext.cpp)

if(WIN32)
Expand All @@ -9,10 +11,7 @@ target_link_libraries(${PROJECT_NAME} PUBLIC libfmemopen)
endif()

find_package(ZLIB REQUIRED)
target_link_libraries(${PROJECT_NAME} PUBLIC ZLIB::ZLIB)

add_subdirectory("lz4/build/cmake/")
target_link_libraries(${PROJECT_NAME} PUBLIC lz4_static)

add_subdirectory("zstd/build/cmake/")
target_link_libraries(${PROJECT_NAME} PUBLIC libzstd_static)
target_link_libraries(${PROJECT_NAME}
PUBLIC ZLIB::ZLIB
PRIVATE $<IF:$<TARGET_EXISTS:zstd::libzstd_shared>,zstd::libzstd_shared,zstd::libzstd_static>
)
1 change: 0 additions & 1 deletion external/lz4
Submodule lz4 deleted from 9efa7e
1 change: 0 additions & 1 deletion external/zstd
Submodule zstd deleted from 7806d8
15 changes: 4 additions & 11 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,14 @@ option(BUILD_SHARED "Build as a shared library" OFF)
set(EXTERNALS_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../external/")
add_subdirectory(${EXTERNALS_PATH} external)

file(GLOB_RECURSE SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/*.c")
file(GLOB_RECURSE HEADERS "${CMAKE_CURRENT_SOURCE_DIR}/*.h")
set(SOURCES
${SOURCES}
"${EXTERNALS_PATH}lz4/lib/lz4file.c"
"${EXTERNALS_PATH}lz4/lib/lz4frame.c"
)
file(GLOB_RECURSE GPAK_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/*.c")
file(GLOB_RECURSE GPAK_HEADERS "${CMAKE_CURRENT_SOURCE_DIR}/*.h")

if(BUILD_SHARED)
add_library(${PROJECT_NAME} SHARED ${SOURCES} ${HEADERS})
add_library(${PROJECT_NAME} SHARED ${GPAK_SOURCES} ${GPAK_HEADERS})
target_compile_definitions(${PROJECT_NAME} PRIVATE -DGPAK_EXPORTS -DGPAK_VERSION=1000)
else()
add_library(${PROJECT_NAME} STATIC ${SOURCES} ${HEADERS})
add_library(${PROJECT_NAME} STATIC ${GPAK_SOURCES} ${GPAK_HEADERS})
endif()

target_compile_features(${PROJECT_NAME} PRIVATE c_std_11)
Expand All @@ -29,8 +24,6 @@ set_target_properties(${PROJECT_NAME} PROPERTIES

target_include_directories(${PROJECT_NAME} PUBLIC
"${CMAKE_CURRENT_SOURCE_DIR}"
"${EXTERNALS_PATH}lz4/lib"
"${EXTERNALS_PATH}zstd/lib"
"${EXTERNALS_PATH}libfmemopen"
)
target_link_libraries(${PROJECT_NAME} PRIVATE gpak_externals)
6 changes: 1 addition & 5 deletions lib/gpak.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,6 @@ int _gpak_archivate_file_tree(gpak_t* _pak)
uint32_t _crc32 = 0u;
if (_pak->header_.compression_ & GPAK_HEADER_COMPRESSION_DEFLATE)
_crc32 = _gpak_compressor_deflate(_pak, _infile, _pak->stream_);
else if (_pak->header_.compression_ & GPAK_HEADER_COMPRESSION_LZ4)
_crc32 = _gpak_compressor_lz4(_pak, _infile, _pak->stream_);
else if (_pak->header_.compression_ & GPAK_HEADER_COMPRESSION_ZST)
_crc32 = _gpak_compressor_zstd(_pak, _infile, _pak->stream_);
else
Expand Down Expand Up @@ -301,7 +299,7 @@ int gpak_close(gpak_t* _pak)
{
if (_pak->mode_ & GPAK_MODE_CREATE)
{
if ((_pak->header_.compression_ & GPAK_HEADER_COMPRESSION_LZ4) || (_pak->header_.compression_ & GPAK_HEADER_COMPRESSION_ZST))
if (_pak->header_.compression_ & GPAK_HEADER_COMPRESSION_ZST)
_gpak_compressor_generate_dictionary(_pak);
}

Expand Down Expand Up @@ -413,8 +411,6 @@ gpak_file_t* gpak_fopen(gpak_t* _pak, const char* _path)

if (_pak->header_.compression_ & GPAK_HEADER_COMPRESSION_DEFLATE)
mfile->crc32_ = _gpak_decompressor_inflate(_pak, _pak->stream_, mfile->stream_, compressed_size);
else if (_pak->header_.compression_ & GPAK_HEADER_COMPRESSION_LZ4)
mfile->crc32_ = _gpak_decompressor_lz4(_pak, _pak->stream_, mfile->stream_, compressed_size);
else if (_pak->header_.compression_ & GPAK_HEADER_COMPRESSION_ZST)
mfile->crc32_ = _gpak_decompressor_zstd(_pak, _pak->stream_, mfile->stream_, compressed_size);
else
Expand Down
116 changes: 0 additions & 116 deletions lib/gpak_compressors.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@
// Zlib
#include <zlib.h>

// LZ4
#include <lz4.h>
#include <lz4file.h>
#include <lz4hc.h>

// Z-standard
#include <zstd.h>
#include <zdict.h>
Expand Down Expand Up @@ -228,117 +223,6 @@ uint32_t _gpak_decompressor_inflate(gpak_t* _pak, FILE* _infile, FILE* _outfile,
return _crc32;
}

// TODO: add dictionary
uint32_t _gpak_compressor_lz4(gpak_t* _pak, FILE* _infile, FILE* _outfile)
{
LZ4F_errorCode_t ret = LZ4F_OK_NoError;
size_t len;
LZ4_writeFile_t* lz4fWrite;
char* _bufferIn = (char*)malloc(_DEFAULT_BLOCK_SIZE);

fseek(_infile, 0, SEEK_END);
size_t _total_size = ftell(_infile);
fseek(_infile, 0, SEEK_SET);

uint32_t _crc32 = crc32(0L, Z_NULL, 0);

LZ4F_preferences_t _preferences;
_preferences.frameInfo.blockSizeID = LZ4F_max4MB;
_preferences.frameInfo.blockMode = LZ4F_blockLinked;
_preferences.frameInfo.contentChecksumFlag = 0;
_preferences.frameInfo.frameType = LZ4F_frame;
_preferences.frameInfo.contentSize = 0;
_preferences.frameInfo.dictID = 0;
_preferences.frameInfo.blockChecksumFlag = 0;
_preferences.compressionLevel = _pak->header_.compression_level_;
_preferences.autoFlush = 1;
_preferences.favorDecSpeed = 1;

ret = LZ4F_writeOpen(&lz4fWrite, _outfile, &_preferences);
if (LZ4F_isError(ret))
return _gpak_make_error(_pak, GPAK_ERROR_LZ4_WRITE_OPEN);

size_t _total_readed = 0ull;
while (1)
{
len = _freadb(_bufferIn, 1, _DEFAULT_BLOCK_SIZE, _infile);
_total_readed += len;
_crc32 = crc32(_crc32, _bufferIn, len);
_gpak_pass_progress(_pak, _total_readed, _total_size, GPAK_STAGE_COMPRESSION);

if (ferror(_infile))
{
_gpak_make_error(_pak, GPAK_ERROR_READ);
goto end;
}

if (len == 0)
break;

ret = LZ4F_write(lz4fWrite, _bufferIn, len);
if (LZ4F_isError(ret))
{
_gpak_make_error(_pak, GPAK_ERROR_LZ4_WRITE);
goto end;
}
}

end:
free(_bufferIn);
if (LZ4F_isError(LZ4F_writeClose(lz4fWrite)))
return _gpak_make_error(_pak, GPAK_ERROR_LZ4_WRITE_CLOSE);
return _crc32;
}

uint32_t _gpak_decompressor_lz4(gpak_t* _pak, FILE* _infile, FILE* _outfile, size_t _read_size)
{
LZ4F_errorCode_t ret = LZ4F_OK_NoError;
LZ4_readFile_t* lz4fRead;
char* _bufferIn = (char*)malloc(_DEFAULT_BLOCK_SIZE);

uint32_t _crc32 = crc32(0L, Z_NULL, 0);

ret = LZ4F_readOpen(&lz4fRead, _infile);
if (LZ4F_isError(ret))
return _gpak_make_error(_pak, GPAK_ERROR_LZ4_READ_OPEN);

size_t bytesReaded = 0;
size_t nextBlockSize = _DEFAULT_BLOCK_SIZE;

while (bytesReaded < _read_size)
{
size_t bytes_to_read = (_read_size - bytesReaded) < _DEFAULT_BLOCK_SIZE ? (_read_size - bytesReaded) : _DEFAULT_BLOCK_SIZE;

ret = LZ4F_read(lz4fRead, _bufferIn, _DEFAULT_BLOCK_SIZE);
if (LZ4F_isError(ret))
{
_gpak_make_error(_pak, GPAK_ERROR_LZ4_READ);
goto out;
}

if (ret == 0)
break;

bytesReaded += ret;
_gpak_pass_progress(_pak, bytesReaded, _read_size, GPAK_STAGE_DECOMPRESSION);

_crc32 = crc32(_crc32, _bufferIn, ret);

if (_fwriteb(_bufferIn, 1, ret, _outfile) != ret)
{
_gpak_make_error(_pak, GPAK_ERROR_WRITE);
goto out;
}
}

out:
free(_bufferIn);
if (LZ4F_isError(LZ4F_readClose(lz4fRead)))
return _gpak_make_error(_pak, GPAK_ERROR_LZ4_READ_CLOSE);

return _crc32;
}


uint32_t _gpak_compressor_zstd(gpak_t* _pak, FILE* _infile, FILE* _outfile)
{
Expand Down
25 changes: 0 additions & 25 deletions lib/gpak_compressors.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,31 +83,6 @@ extern "C" {
*/
GPAK_API uint32_t _gpak_decompressor_inflate(gpak_t* _pak, FILE* _infile, FILE* _outfile, size_t _read_size);

/**
* @brief Compresses the input file using the LZ4 algorithm.
*
* This function compresses the input file using the LZ4 algorithm and writes the compressed data to the output file.
*
* @param _pak A pointer to the gpak_t.
* @param _infile A pointer to the input FILE.
* @param _outfile A pointer to the output FILE.
* @return The number of bytes written to the output file.
*/
GPAK_API uint32_t _gpak_compressor_lz4(gpak_t* _pak, FILE* _infile, FILE* _outfile);

/**
* @brief Decompresses the input file using the LZ4 algorithm.
*
* This function decompresses the input file using the LZ4 algorithm and writes the decompressed data to the output file.
*
* @param _pak A pointer to the gpak_t.
* @param _infile A pointer to the input FILE.
* @param _outfile A pointer to the output FILE.
* @param _read_size The number of bytes to read from the input file.
* @return The number of bytes written to the output file.
*/
GPAK_API uint32_t _gpak_decompressor_lz4(gpak_t* _pak, FILE* _infile, FILE* _outfile, size_t _read_size);

/**
* @brief Compresses the input file using the Zstandard (zstd) algorithm.
*
Expand Down
3 changes: 1 addition & 2 deletions lib/gpak_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ enum gpak_header_compression_algorithm
{
GPAK_HEADER_COMPRESSION_NONE = 0, /**< No compression. */
GPAK_HEADER_COMPRESSION_DEFLATE = 1 << 0, /**< Deflate compression algorithm. */
GPAK_HEADER_COMPRESSION_LZ4 = 1 << 1, /**< LZ4 compression algorithm. */
GPAK_HEADER_COMPRESSION_ZST = 1 << 2 /**< Zstandard (ZST) compression algorithm. */
GPAK_HEADER_COMPRESSION_ZST = 1 << 1 /**< Zstandard (ZST) compression algorithm. */
};

/**
Expand Down

0 comments on commit b10f707

Please sign in to comment.