This repository has been archived by the owner on Apr 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 83
Add bzip2 and Umineko steam release support #153
Open
NotKyon
wants to merge
3
commits into
vn-tools:master
Choose a base branch
from
NotKyon:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"); | ||
} |
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,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); | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Double blank line |
||
} } } |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?