diff --git a/.gitmodules b/.gitmodules
index cbffb5c..6505679 100644
--- a/.gitmodules
+++ b/.gitmodules
@@ -1,3 +1,6 @@
[submodule "tests/lib"]
path = tests/lib
url = https://github.com/google/googletest
+[submodule "3rdparty/bzip2"]
+ path = 3rdparty/bzip2
+ url = https://gitlab.com/bzip2/bzip2.git
diff --git a/3rdparty/CMakeLists.txt b/3rdparty/CMakeLists.txt
new file mode 100644
index 0000000..3503875
--- /dev/null
+++ b/3rdparty/CMakeLists.txt
@@ -0,0 +1,7 @@
+message("Hello")
+
+
+SET(ENABLE_LIB_ONLY ON)
+SET(ENABLE_STATIC_LIB ON)
+
+add_subdirectory(bzip2)
\ No newline at end of file
diff --git a/3rdparty/bzip2 b/3rdparty/bzip2
new file mode 160000
index 0000000..2221ed3
--- /dev/null
+++ b/3rdparty/bzip2
@@ -0,0 +1 @@
+Subproject commit 2221ed3debadd1b2607207b68f0dbb423b8f9a5b
diff --git a/CMakeLists.txt b/CMakeLists.txt
index faea6c0..b191b3a 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -71,6 +71,29 @@ if("${CMAKE_BUILD_TYPE}" MATCHES "Release")
endif()
endif()
+add_subdirectory(3rdparty)
+
add_library("Aaru.Compression.Native" SHARED library.c apple_rle.c apple_rle.h adc.c adc.h)
+MACRO (TARGET_LINK_LIBRARIES_WHOLE_ARCHIVE target)
+ if("${CMAKE_C_COMPILER_ID}" MATCHES "MSVC")
+ FOREACH (arg IN LISTS ARGN)
+ SET_TARGET_PROPERTIES(
+ ${target} PROPERTIES LINK_FLAGS "/WHOLEARCHIVE:${lib}"
+ )
+ ENDFOREACH ()
+ ELSE ()
+ if("${CMAKE_C_COMPILER_ID}" MATCHES "AppleClang")
+ SET(LINK_FLAGS "-Wl,-all_load")
+ SET(UNDO_FLAGS "-Wl,-noall_load")
+ ELSE ()
+ SET(LINK_FLAGS "-Wl,--whole-archive")
+ SET(UNDO_FLAGS "-Wl,--no-whole-archive")
+ ENDIF ()
+ TARGET_LINK_LIBRARIES(${target} ${LINK_FLAGS} ${ARGN} ${UNDO_FLAGS})
+ ENDIF ()
+ENDMACRO ()
+
+TARGET_LINK_LIBRARIES_WHOLE_ARCHIVE("Aaru.Compression.Native" bz2_static)
+
add_subdirectory(tests)
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 5f41b5a..574a315 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -15,7 +15,10 @@ file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/data/apple_rle.bin
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/data/adc.bin
DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/data/)
+file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/data/bzip2.bz2
+ DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/data/)
+
# 'Google_Tests_run' is the target name
# 'test1.cpp tests2.cpp' are source files with tests
-add_executable(tests_run apple_rle.cpp crc32.c crc32.h adc.cpp)
+add_executable(tests_run apple_rle.cpp crc32.c crc32.h adc.cpp bzip2.cpp)
target_link_libraries(tests_run gtest gtest_main "Aaru.Compression.Native")
diff --git a/tests/bzip2.cpp b/tests/bzip2.cpp
new file mode 100644
index 0000000..ff9ed6f
--- /dev/null
+++ b/tests/bzip2.cpp
@@ -0,0 +1,80 @@
+/*
+ * This file is part of the Aaru Data Preservation Suite.
+ * Copyright (c) 2019-2021 Natalia Portillo.
+ *
+ * This library is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of the
+ * License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see .
+ */
+
+#include
+#include
+
+#include "../3rdparty/bzip2/bzlib.h"
+#include "crc32.h"
+#include "gtest/gtest.h"
+
+#define EXPECTED_CRC32 0xc64059c0
+
+static const uint8_t* buffer;
+
+class bzip2Fixture : public ::testing::Test
+{
+ public:
+ bzip2Fixture()
+ {
+ // initialization;
+ // can also be done in SetUp()
+ }
+
+ protected:
+ void SetUp()
+ {
+ char path[PATH_MAX];
+ char filename[PATH_MAX];
+
+ getcwd(path, PATH_MAX);
+ snprintf(filename, PATH_MAX, "%s/data/bzip2.bz2", path);
+
+ FILE* file = fopen(filename, "rb");
+ buffer = (const uint8_t*)malloc(1053934);
+ fread((void*)buffer, 1, 1053934, file);
+ fclose(file);
+ }
+
+ void TearDown() { free((void*)buffer); }
+
+ ~bzip2Fixture()
+ {
+ // resources cleanup, no exceptions allowed
+ }
+
+ // shared user data
+};
+
+TEST_F(bzip2Fixture, bzip2)
+{
+ uint real_size = 1048576;
+ auto* outBuf = (uint8_t*)malloc(1048576);
+
+ auto bz_err = BZ2_bzBuffToBuffDecompress((char*)outBuf, &real_size, (char*)buffer, 1053934, 0, 0);
+
+ EXPECT_EQ(bz_err, BZ_OK);
+
+ EXPECT_EQ(real_size, 1048576);
+
+ auto crc = crc32_data(outBuf, 1048576);
+
+ free(outBuf);
+
+ EXPECT_EQ(crc, EXPECTED_CRC32);
+}
diff --git a/tests/data/bzip2.bz2 b/tests/data/bzip2.bz2
new file mode 100644
index 0000000..ce91b41
Binary files /dev/null and b/tests/data/bzip2.bz2 differ