Skip to content

Commit

Permalink
Fix Decoder Error Issue #4 (#5)
Browse files Browse the repository at this point in the history
* fix: correct file delegation to resolve "corrupt stream" issue (#4)

* chore: minor fix
  • Loading branch information
pnck committed Apr 21, 2024
1 parent f3e7366 commit 0e60a43
Show file tree
Hide file tree
Showing 10 changed files with 220 additions and 107 deletions.
19 changes: 17 additions & 2 deletions foo_input_ncm.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>$(CoreLibraryDependencies);%(AdditionalDependencies);vendor/sdk/foobar2000/shared/shared-$(Platform).lib</AdditionalDependencies>
</Link>
<PostBuildEvent>
<Command>copy $(TargetPath) $(ProjectDir)dist\</Command>
</PostBuildEvent>
<PostBuildEvent>
<Message>Copy built file (x86)</Message>
</PostBuildEvent>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
Expand All @@ -122,7 +128,7 @@
<AdditionalDependencies>$(CoreLibraryDependencies);%(AdditionalDependencies);vendor/sdk/foobar2000/shared/shared-$(Platform).lib</AdditionalDependencies>
</Link>
<PostBuildEvent>
<Message>copy built files</Message>
<Message>Copy built file (x86)</Message>
<Command>copy $(TargetPath) $(ProjectDir)dist\</Command>
</PostBuildEvent>
</ItemDefinitionGroup>
Expand All @@ -143,6 +149,14 @@
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>$(CoreLibraryDependencies);%(AdditionalDependencies);vendor/sdk/foobar2000/shared/shared-$(Platform).lib</AdditionalDependencies>
</Link>
<PostBuildEvent>
<Command>mkdir $(ProjectDir)dist\$(Platform)

copy $(TargetPath) $(ProjectDir)dist\$(Platform)\</Command>
</PostBuildEvent>
<PostBuildEvent>
<Message>Copy built file (x64)</Message>
</PostBuildEvent>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
Expand All @@ -166,8 +180,9 @@
<AdditionalDependencies>$(CoreLibraryDependencies);%(AdditionalDependencies);vendor/sdk/foobar2000/shared/shared-$(Platform).lib</AdditionalDependencies>
</Link>
<PostBuildEvent>
<Message>copy built files</Message>
<Message>Copy built file (x64)</Message>
<Command>mkdir $(ProjectDir)dist\$(Platform)

copy $(TargetPath) $(ProjectDir)dist\$(Platform)\</Command>
</PostBuildEvent>
</ItemDefinitionGroup>
Expand Down
11 changes: 8 additions & 3 deletions foo_input_ncm.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,8 @@
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
COPY_PHASE_STRIP = NO;
CURRENT_PROJECT_VERSION = 0.3.0;
DEAD_CODE_STRIPPING = YES;
CURRENT_PROJECT_VERSION = 0.3.4;
DEAD_CODE_STRIPPING = NO;
DEBUG_INFORMATION_FORMAT = dwarf;
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_TESTABILITY = YES;
Expand Down Expand Up @@ -315,9 +315,11 @@
vendor/sdk/foobar2000,
);
INCLUDED_RECURSIVE_SEARCH_PATH_SUBDIRECTORIES = src;
INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.music";
LIBRARY_SEARCH_PATHS = "";
LOCALIZATION_PREFERS_STRING_CATALOGS = YES;
MACOSX_DEPLOYMENT_TARGET = 13.3;
MARKETING_VERSION = 0.3.4;
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
MTL_FAST_MATH = YES;
SDKROOT = macosx;
Expand Down Expand Up @@ -360,7 +362,7 @@
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
COPY_PHASE_STRIP = YES;
CURRENT_PROJECT_VERSION = 0.3.0;
CURRENT_PROJECT_VERSION = 0.3.4;
DEAD_CODE_STRIPPING = YES;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
ENABLE_NS_ASSERTIONS = NO;
Expand All @@ -371,6 +373,7 @@
GCC_NO_COMMON_BLOCKS = YES;
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = stdafx.h;
"GCC_PREPROCESSOR_DEFINITIONS[arch=*]" = "NDEBUG=1";
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
GCC_WARN_UNDECLARED_SELECTOR = YES;
Expand All @@ -384,9 +387,11 @@
vendor/sdk/foobar2000,
);
INCLUDED_RECURSIVE_SEARCH_PATH_SUBDIRECTORIES = src;
INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.music";
LIBRARY_SEARCH_PATHS = "";
LOCALIZATION_PREFERS_STRING_CATALOGS = YES;
MACOSX_DEPLOYMENT_TARGET = 13.3;
MARKETING_VERSION = 0.3.4;
MTL_ENABLE_DEBUG_INFO = NO;
MTL_FAST_MATH = YES;
ONLY_ACTIVE_ARCH = NO;
Expand Down
9 changes: 4 additions & 5 deletions src/album_art_extractor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,19 @@ bool ncm_album_art_extractor::is_our_path(const char *p_path, const char *p_exte
return stricmp_utf8(p_extension, "ncm") == 0;
}

album_art_extractor_instance_ptr ncm_album_art_extractor::open(file_ptr p_filehint, const char *p_path,
abort_callback &p_abort) {
album_art_extractor_instance_ptr ncm_album_art_extractor::open(file_ptr p_filehint, const char *p_path, abort_callback &p_abort) {
if (p_filehint.is_empty()) {
if (is_our_path(p_path, pfc::string_extension(p_path).toString())) {
filesystem::g_open_read(p_filehint, p_path, p_abort);
}
}
auto _ncm_file = fb2k::service_new<ncm_file>(p_filehint, p_path);
auto _ncm_file = fb2k::service_new<ncm_file>(p_path);
_ncm_file->parse(ncm_file::parse_contents::NCM_PARSE_ALBUM);
auto album_arts = fb2k::service_new<album_art_extractor_instance_simple>();
auto image = album_art_data_impl::g_create(_ncm_file->image_data.data(), _ncm_file->image_data.size());
auto image = album_art_data_impl::g_create(_ncm_file->image_data().data(), _ncm_file->image_data().size());
album_arts->set(album_art_ids::cover_front, image);
// album_arts->set(album_art_ids::disc, image);
return album_arts;
}

static service_factory_single_t<ncm_album_art_extractor> g_ncm_album_art_extractor;
static service_factory_single_t<ncm_album_art_extractor> g_ncm_album_art_extractor;
16 changes: 8 additions & 8 deletions src/cipher/abnormal_RC4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,29 @@ fb2k_ncm::cipher::abnormal_RC4::abnormal_RC4(const uint8_t *seed, size_t len) :
for (int i = 0; i < 256; i++) {
auto k1 = (i + 1) & 0xff;
auto k2 = (k1 + key_box[k1]) & 0xff;
// no exchange comparing to RC4 PRGA
// std::swap(key_box[k1],key_box[k2]); <- this step is missing
auto k = key_box[(key_box[k1] + key_box[k2]) & 0xff];
key_box_[i] = k;
}
}

std::function<uint8_t(uint8_t, size_t)> fb2k_ncm::cipher::abnormal_RC4::get_transform() const {
return [key_box = this->key_box_](uint8_t _b, size_t offset) -> uint8_t { return _b ^ key_box[offset & 0xff]; };
return [this](uint8_t b, size_t offset) -> uint8_t { return b ^ key_box_[offset & 0xff]; };
}

bool fb2k_ncm::cipher::abnormal_RC4::is_valid() const {
return key_seed_.size() && key_box_;
}

abnormal_RC4::abnormal_RC4(abnormal_RC4 &c) : key_seed(key_seed_), key_seed_(c.key_seed_), key_box_(c.key_box_) {}
abnormal_RC4::abnormal_RC4(abnormal_RC4 &c) : key_seed(key_seed_), key_seed_(c.key_seed_), key_box_(c.key_box_), counter_(c.counter_) {}
abnormal_RC4 &abnormal_RC4::operator=(abnormal_RC4 &c) {
key_seed_ = c.key_seed_;
key_box_ = c.key_box_;
counter_ = c.counter_;
return *this;
}
abnormal_RC4::abnormal_RC4(abnormal_RC4 &&c)
: key_seed(key_seed_), key_seed_(std::move(c.key_seed_)), key_box_(std::move(c.key_box_)) {}
: key_seed(key_seed_), key_seed_(std::move(c.key_seed_)), key_box_(std::move(c.key_box_)), counter_(c.counter_) {}
abnormal_RC4 &abnormal_RC4::operator=(abnormal_RC4 &&c) {
key_seed_ = std::move(c.key_seed_);
key_box_ = std::move(c.key_box_);
counter_ = c.counter_;
return *this;
}
}
18 changes: 16 additions & 2 deletions src/cipher/abnormal_RC4.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#include <memory>
#include <functional>

#include <ranges>
#include <algorithm>

namespace fb2k_ncm::cipher
{
// special rc4-like BLOCK CIPHER used for ncm files
Expand All @@ -18,15 +21,26 @@ namespace fb2k_ncm::cipher
abnormal_RC4 &operator=(abnormal_RC4 &&c);

public:
bool is_valid() const;
inline bool is_valid() const { return key_seed_.size() && key_box_; }
inline auto &reset_counter(size_t offset = 0) {
counter_ = offset;
return *this;
}
std::function<uint8_t(uint8_t, size_t)> get_transform() const;

// c++20 ranges version, returns a transform view
template <std::ranges::range R>
auto transform(const R &r) {
return r | std::views::all | std::views::transform([tf = get_transform(), this](auto b) { return tf(b, counter_++); });
}

public:
const std::vector<uint8_t> &key_seed;

private:
std::vector<uint8_t> key_seed_;
std::shared_ptr<uint8_t[]> key_box_;
size_t counter_ = 0; // to keep decrypt indices on track
};

} // namespace fb2k_ncm::cipher
} // namespace fb2k_ncm::cipher
Loading

0 comments on commit 0e60a43

Please sign in to comment.