diff --git a/.gitignore b/.gitignore index 69238b6..1ac048c 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ compile_commands.json CTestTestfile.cmake _deps .vs +out \ No newline at end of file diff --git a/minlzdec/CMakeLists.txt b/minlzdec/CMakeLists.txt new file mode 100644 index 0000000..27324a8 --- /dev/null +++ b/minlzdec/CMakeLists.txt @@ -0,0 +1,9 @@ +add_executable (minlzdec "minlzdec.c") + +target_include_directories(minlzdec PUBLIC ${PROJECT_SOURCE_DIR}) +target_link_libraries(minlzdec LINK_PUBLIC minlzlib) + +if(MSVC) + set(CMAKE_C_FLAGS_RELWITHDEBINFO "/Ox /Ob2 /Oi /Ot /Oy /GF /GL /Gy /MD /Zi /DNDEBUG") + set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "/LTCG /INCREMENTAL:NO /OPT:ICF /OPT:REF") +endif() diff --git a/minlzdec/minlzdec.c b/minlzdec/minlzdec.c new file mode 100644 index 0000000..9e27c78 --- /dev/null +++ b/minlzdec/minlzdec.c @@ -0,0 +1,116 @@ +#define _CRT_SECURE_NO_WARNINGS +#define _CRT_NONSTDC_NO_WARNINGS +#include +#include +#include +#include +#include +#include +#include +#include + +int32_t +main ( + int32_t ArgumentCount, + char* Arguments[] + ) +{ + FILE* inputFile; + FILE* outputFile; + size_t fileSize; + size_t sizeRead; + uint32_t size; + uint8_t* inputBuffer; + uint8_t* outputBuffer; + struct stat stat; + bool decodeResult; + + inputFile = NULL; + outputFile = NULL; + inputBuffer = NULL; + outputBuffer = NULL; + + printf("minlzdec v.1.0.0 -- http://ionescu007.github.io/minlzma\n"); + printf("Copyright(c) 2020 Alex Ionescu (@aionescu)\n\n"); + if (ArgumentCount != 3) + { + printf("Usage: minlzdec [INPUT FILE] [OUTPUT FILE]\n"); + printf("Decompress INPUT FILE in the .xz format into OUTPUT FILE.\n"); + _set_errno(EINVAL); + goto Cleanup; + } + + inputFile = fopen(Arguments[1], "rb"); + if (inputFile == 0) + { + goto Cleanup; + } + + fstat(fileno(inputFile), &stat); + fileSize = stat.st_size; + printf("Input file size: %zd\n", fileSize); + + inputBuffer = malloc(fileSize); + if (inputBuffer == NULL) + { + goto Cleanup; + } + + sizeRead = fread(inputBuffer, 1, fileSize, inputFile); + if (sizeRead != fileSize) + { + goto Cleanup; + } + + size = (uint32_t)fileSize * 16; + + outputBuffer = malloc(size); + if (outputBuffer == NULL) + { + goto Cleanup; + } + + decodeResult = XzDecode(inputBuffer, size, outputBuffer, &size); + if (decodeResult == false) + { + _set_errno(ENOTSUP); + goto Cleanup; + } + printf("decode result: %d %d\n", decodeResult, size); + + outputFile = fopen(Arguments[2], "wb"); + if (outputFile == 0) + { + goto Cleanup; + } + + fileSize = fwrite(outputBuffer, 1, size, outputFile); + if (fileSize != size) + { + goto Cleanup; + } + _set_errno(0); + +Cleanup: + if (outputBuffer != NULL) + { + free(outputBuffer); + } + if (inputBuffer != NULL) + { + free(inputBuffer); + } + + if (outputFile != 0) + { + fflush(outputFile); + fclose(outputFile); + } + + if (inputFile != 0) + { + fclose(inputFile); + } + + return errno; +} diff --git a/minlzlib/CMakeLists.txt b/minlzlib/CMakeLists.txt index 59876b7..ce2371d 100644 --- a/minlzlib/CMakeLists.txt +++ b/minlzlib/CMakeLists.txt @@ -1,13 +1,15 @@ cmake_minimum_required (VERSION 3.8) -if(!MSVC) - add_compile_options(-Wno-undefined-inline) -endif() +set(MINLZLIB_SOURCES "inputbuf.c" "dictbuf.c" "lzma2dec.c" "lzmadec.c" "rangedec.c" "xzstream.c" "lzmadec.h" "xzstream.h" "minlzlib.h") +add_library(minlz_obj OBJECT ${MINLZLIB_SOURCES}) +set_target_properties(minlz_obj PROPERTIES C_STANDARD 11 C_STANDARD_REQUIRED YES C_EXTENSIONS NO) + +add_library (minlzlib STATIC ${MINLZLIB_SOURCES}) +add_library (minlz SHARED ${MINLZLIB_SOURCES}) -add_library (minlzlib STATIC "inputbuf.c" "dictbuf.c" "lzma2dec.c" "lzmadec.c" "rangedec.c" "xzstream.c" "lzmadec.h" "xzstream.h" "minlzlib.h") -set_target_properties(minlzlib PROPERTIES C_STANDARD 11 C_STANDARD_REQUIRED YES C_EXTENSIONS NO) -target_include_directories (minlzlib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) +target_include_directories (minlz_obj PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) if(MSVC) set(CMAKE_C_FLAGS_RELWITHDEBINFO "/Ox /Ob2 /Oi /Ot /Oy /GF /GL /Gy /MD /Zi /DNDEBUG") + set(CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "/LTCG /INCREMENTAL:NO /OPT:ICF /OPT:REF") endif() diff --git a/minlzma.h b/minlzma.h new file mode 100644 index 0000000..3692a58 --- /dev/null +++ b/minlzma.h @@ -0,0 +1,24 @@ +#pragma once + +/** + * @brief Decompresses an XZ stream from InputBuffer into OutputBuffer. + * @description The XZ stream must contain a single block with an LZMA2 filter + * and no BJC2 filters, using default LZMA properties, and using + * either CRC32 or None as the checksum type. + * + * @param[in] InputBuffer - A fully formed buffer containing the XZ stream. + * @param[in] InputSize - The size of the input buffer. + * @param[in] OutputBuffer - A fully allocated buffer to receive the outout. + * @param[inout] OutputSize - On input, the size of the buffer. On output, the + * size of the decompressed result. + * + * @return true - The input buffer was fully decompressed in OutputBuffer + * false - A failure occured during the decompression process + */ +bool +XzDecode ( + uint8_t* InputBuffer, + uint32_t InputSize, + uint8_t* OutputBuffer, + uint32_t* OutputSize + ); \ No newline at end of file