Skip to content
This repository has been archived by the owner on Apr 25, 2022. It is now read-only.

Add bzip2 and Umineko steam release support #153

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ find_package(ZLIB REQUIRED)
include_directories(${ZLIB_INCLUDE_DIR})
link_directories(${ZLIB_LIBRARY_DIRS})

find_package(BZip2 REQUIRED)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you make this into optional dependency?

include_directories(${BZIP2_INCLUDE_DIR})
link_directories(${BZIP2_LIBRARY_DIR})

find_package(PNG REQUIRED)
include_directories(${PNG_INCLUDE_DIR})
link_directories(${PNG_LIBRARY_DIRS})
Expand Down Expand Up @@ -183,13 +187,13 @@ endif()
add_library(libau OBJECT ${au_sources} ${au_headers})

add_executable(arc_unpacker "${CMAKE_SOURCE_DIR}/src/main.cc" $<TARGET_OBJECTS:libau>)
target_link_libraries(arc_unpacker ${unicode} ${iconv} ${Boost_LIBRARIES} ${ZLIB_LIBRARIES} ${PNG_LIBRARIES} ${JPEG_LIBRARIES} ${OPENSSL_LIBRARIES})
target_link_libraries(arc_unpacker ${unicode} ${iconv} ${Boost_LIBRARIES} ${ZLIB_LIBRARIES} ${BZIP2_LIBRARIES} ${PNG_LIBRARIES} ${JPEG_LIBRARIES} ${OPENSSL_LIBRARIES})
if(WEBP_FOUND)
target_link_libraries(arc_unpacker ${WEBP_LIBRARIES})
endif()

add_executable(run_tests ${test_sources} ${test_headers} "${CMAKE_SOURCE_DIR}/tests/main.cc" $<TARGET_OBJECTS:libau>)
target_link_libraries(run_tests ${iconv} ${Boost_LIBRARIES} ${ZLIB_LIBRARIES} ${PNG_LIBRARIES} ${JPEG_LIBRARIES} ${OPENSSL_LIBRARIES})
target_link_libraries(run_tests ${iconv} ${Boost_LIBRARIES} ${ZLIB_LIBRARIES} ${BZIP2_LIBRARIES} ${PNG_LIBRARIES} ${JPEG_LIBRARIES} ${OPENSSL_LIBRARIES})
if(WEBP_FOUND)
target_link_libraries(run_tests ${WEBP_LIBRARIES})
endif()
Expand Down
151 changes: 151 additions & 0 deletions src/algo/pack/bzip2.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
// Copyright (C) 2018 by notkyon, 2016 rr-
//
// This file is part of arc_unpacker.
//
// arc_unpacker is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or (at
// your option) any later version.
//
// arc_unpacker 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
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with arc_unpacker. If not, see <http://www.gnu.org/licenses/>.

#include "algo/pack/bzip2.h"
#include <cstring>
#include <memory>
#include <bzlib.h>
#include "algo/format.h"
#include "err.h"
#include "io/memory_byte_stream.h"

using namespace au;
using namespace au::algo::pack;

static const int buffer_size = 8192;

constexpr uint64_t combine64( uint32_t lo32, uint32_t hi32 ) {
return uint64_t(hi32)<<32 | uint64_t(lo32);
}
inline uint64_t total_out( const bz_stream &s ) {
return combine64( s.total_out_lo32, s.total_out_hi32 );
}
inline uint64_t total_in( const bz_stream &s ) {
return combine64( s.total_in_lo32, s.total_in_hi32 );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mixed tabs and spaces

}

static bstr process_stream(
io::BaseByteStream &input_stream,
const std::function<int(bz_stream &s)> &init_func,
const std::function<int(bz_stream &s)> &process_func,
const std::function<int(bz_stream &s)> &end_func,
const std::string &error_message)
{
bz_stream s;
std::memset(&s, 0, sizeof(s));
if (init_func(s) != BZ_OK)
throw std::logic_error("Failed to initialize zlib stream");

bstr output, input_chunk, output_chunk(buffer_size);
size_t written = 0;
int ret;
const auto initial_pos = input_stream.pos();
do
{
if (s.avail_in == 0)
{
input_chunk = input_stream.read(
std::min<size_t>(input_stream.left(), buffer_size));
s.next_in = const_cast<char*>(input_chunk.get<const char>());
s.avail_in = input_chunk.size();
}

s.next_out = output_chunk.get<char>();
s.avail_out = output_chunk.size();

ret = process_func(s);
output += output_chunk.substr(0, total_out(s) - written);
written = total_out(s);
if (ret == BZ_PARAM_ERROR) // zlib equivalent: Z_BUF_ERROR
{
input_chunk += input_stream.read(
std::min<size_t>(input_stream.left(), buffer_size));
s.next_in = const_cast<char*>(input_chunk.get<const char>());
s.avail_in = input_chunk.size();
}
}
while (ret == BZ_OK);

input_stream.seek(initial_pos + total_in(s));
const auto pos = s.next_in - input_chunk.get<const char>();
end_func(s);
if (ret != BZ_STREAM_END)
{
throw err::CorruptDataError(algo::format(
"%s (%s near %x)",
error_message.c_str(),
"unknown error",
pos));
}
return output;
}

bstr algo::pack::bz2_inflate(
io::BaseByteStream &input_stream)
{
return process_stream(
input_stream,
[](bz_stream &s)
{
return BZ2_bzDecompressInit(&s, /*verbosity=*/0, /*small=*/0);
},
[](bz_stream &s)
{
return BZ2_bzDecompress(&s);
},
[](bz_stream &s)
{
return BZ2_bzDecompressEnd(&s);
},
"Failed to inflate bzip2 stream");
}

bstr algo::pack::bz2_inflate(const bstr &input)
{
io::MemoryByteStream input_stream(input);
return ::bz2_inflate(input_stream);
}

bstr algo::pack::bz2_deflate(
const bstr &input,
const CompressionLevel compression_level)
{
io::MemoryByteStream input_stream(input);
return process_stream(
input_stream,
[compression_level](bz_stream &s)
{
/* http://bzip.org/1.0.5/bzip2-manual-1.0.5.html#bzcompress-init */

const std::array<int,4> levels { 9, 6, 1, 0 };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More whitespace issues


return BZ2_bzCompressInit(
&s,
levels.at(static_cast<int>(compression_level)),
/*verbosity=*/0,
/*workFactory=*/200);
},
[&input_stream](bz_stream &s)
{
return BZ2_bzCompress(&s, input_stream.left() ? BZ_RUN : BZ_FINISH);
},
[](bz_stream &s)
{
return BZ2_bzCompressEnd(&s);
},
"Failed to deflate stream");
}
39 changes: 39 additions & 0 deletions src/algo/pack/bzip2.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (C) 2018 by notkyon, 2016 rr-
//
// This file is part of arc_unpacker.
//
// arc_unpacker is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or (at
// your option) any later version.
//
// arc_unpacker 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
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with arc_unpacker. If not, see <http://www.gnu.org/licenses/>.

#pragma once

#include "algo/pack/compression_level.h"
#include "io/base_byte_stream.h"
#include "types.h"

namespace au {
namespace algo {
namespace pack {

bstr bz2_inflate(
io::BaseByteStream &input_stream);

bstr bz2_inflate(
const bstr &input);

bstr bz2_deflate(
const bstr &input,
const CompressionLevel = CompressionLevel::Best);


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Double blank line

} } }
11 changes: 10 additions & 1 deletion src/dec/nscripter/nsa_archive_decoder.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (C) 2016 by rr-
// Copyright (C) 2016 by rr-, 2018 by notkyon
//
// This file is part of arc_unpacker.
//
Expand All @@ -17,6 +17,7 @@

#include "dec/nscripter/nsa_archive_decoder.h"
#include "algo/pack/lzss.h"
#include "algo/pack/bzip2.h"
#include "algo/range.h"
#include "dec/nscripter/nsa_encrypted_stream.h"
#include "dec/nscripter/spb_image_decoder.h"
Expand All @@ -33,6 +34,7 @@ namespace
None = 0,
Spb = 1,
Lzss = 2,
Nbz = 4
};

struct CustomArchiveEntry final : dec::CompressedArchiveEntry
Expand Down Expand Up @@ -122,6 +124,13 @@ std::unique_ptr<io::File> NsaArchiveDecoder::read_file_impl(
logger, decoder.decode(logger, spb_file), entry->path);
}

if (entry->compression_type == CompressionType::Nbz)
{
return std::make_unique<io::File>(
entry->path,
algo::pack::bz2_inflate(data.substr(4)));
}

throw err::NotSupportedError("Unknown compression type");
}

Expand Down