-
-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
147 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#pragma once | ||
|
||
#ifndef _XZ_SHARED_API | ||
#define _XZ_SHARED_API | ||
#endif | ||
|
||
_XZ_SHARED_API const char* DecompressXZ(size_t downloadedDataSize, const uint8_t* downloadedData, const char* fileName); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Define the current source locations | ||
|
||
SET(CURRENT_HEADER_DIR ${CMAKE_SOURCE_DIR}/include/utils-xz) | ||
SET(CURRENT_SOURCE_DIR ${CMAKE_SOURCE_DIR}/sources/utils-xz) | ||
|
||
FILE ( GLOB_RECURSE utils_xz_SOURCES "${CURRENT_HEADER_DIR}/*.h" "${CURRENT_SOURCE_DIR}/*.cpp" ) | ||
|
||
if (USE_SHARED_LIBS) | ||
add_library(utils-xz SHARED ${utils_xz_SOURCES}) | ||
if(WIN32) | ||
target_compile_definitions(utils-xz | ||
INTERFACE | ||
"_XZ_SHARED_API=__declspec(dllimport)" | ||
PRIVATE | ||
"_XZ_SHARED_API=__declspec(dllexport)" | ||
) | ||
else() | ||
target_compile_definitions(utils-xz | ||
INTERFACE | ||
"_XZ_SHARED_API=__attribute__((visibility(\"default\")))" | ||
) | ||
endif() | ||
install( | ||
TARGETS utils-xz | ||
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" | ||
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" | ||
) | ||
else() | ||
add_library(utils-xz STATIC ${utils_xz_SOURCES}) | ||
endif() | ||
|
||
target_link_libraries(utils-xz PRIVATE LibLZMA::LibLZMA) | ||
target_include_directories(utils-xz PRIVATE LibLZMA::LibLZMA) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
#include <iostream> | ||
#include <fstream> | ||
#include <vector> | ||
#include <lzma.h> | ||
#include <utils-xz/utils-xz.h> | ||
|
||
_XZ_SHARED_API const char* DecompressXZ(size_t downloadedDataSize, const uint8_t* downloadedData, const char* fileName) | ||
{ | ||
size_t outSize = 67174456; | ||
std::vector<uint8_t> outBuffer; | ||
const char* error = nullptr; | ||
|
||
std::ofstream file; | ||
file.open(fileName, std::ios::out | std::ios::trunc | std::ios::binary); | ||
|
||
if (!file.is_open()) | ||
{ | ||
return "Could not open file for writing"; | ||
} | ||
|
||
try | ||
{ | ||
outBuffer.resize(outSize); | ||
} | ||
catch(...) | ||
{ | ||
error = "Could not allocate buffer"; | ||
} | ||
|
||
if (error == nullptr) | ||
{ | ||
const uint32_t flags = LZMA_TELL_UNSUPPORTED_CHECK | LZMA_CONCATENATED; | ||
lzma_stream strm = LZMA_STREAM_INIT; | ||
strm.next_in = downloadedData; | ||
strm.avail_in = downloadedDataSize; | ||
lzma_ret lzmaRet = lzma_stream_decoder(&strm, outSize, flags); | ||
if (lzmaRet == LZMA_OK) | ||
{ | ||
do { | ||
strm.next_out = outBuffer.data(); | ||
strm.avail_out = outSize; | ||
lzmaRet = lzma_code(&strm, LZMA_FINISH); | ||
if (lzmaRet == LZMA_MEMLIMIT_ERROR) | ||
{ | ||
outSize = lzma_memusage(&strm); | ||
try | ||
{ | ||
outBuffer.resize(outSize); | ||
} | ||
catch (...) | ||
{ | ||
error = "Could not increase buffer size"; | ||
break; | ||
} | ||
lzma_memlimit_set(&strm, outSize); | ||
strm.avail_out = 0; | ||
} | ||
else if (lzmaRet != LZMA_OK && lzmaRet != LZMA_STREAM_END) | ||
{ | ||
error = "LZMA decoder returned error"; | ||
break; | ||
} | ||
else | ||
{ | ||
std::streamsize toWrite = static_cast<std::streamsize>(outSize - strm.avail_out); | ||
file.write(reinterpret_cast<char*>(outBuffer.data()), toWrite); | ||
} | ||
} while (strm.avail_out == 0 && lzmaRet != LZMA_STREAM_END); | ||
file.flush(); | ||
} | ||
else | ||
{ | ||
error = "Could not initialize LZMA decoder"; | ||
} | ||
|
||
lzma_end(&strm); | ||
} | ||
|
||
file.close(); | ||
|
||
if (error != nullptr) | ||
std::remove(fileName); | ||
|
||
return error; | ||
} |