Skip to content

Commit 0e60a43

Browse files
authored
Fix Decoder Error Issue #4 (#5)
* fix: correct file delegation to resolve "corrupt stream" issue (#4) * chore: minor fix
1 parent f3e7366 commit 0e60a43

10 files changed

Lines changed: 220 additions & 107 deletions

File tree

foo_input_ncm.vcxproj

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@
9999
<GenerateDebugInformation>true</GenerateDebugInformation>
100100
<AdditionalDependencies>$(CoreLibraryDependencies);%(AdditionalDependencies);vendor/sdk/foobar2000/shared/shared-$(Platform).lib</AdditionalDependencies>
101101
</Link>
102+
<PostBuildEvent>
103+
<Command>copy $(TargetPath) $(ProjectDir)dist\</Command>
104+
</PostBuildEvent>
105+
<PostBuildEvent>
106+
<Message>Copy built file (x86)</Message>
107+
</PostBuildEvent>
102108
</ItemDefinitionGroup>
103109
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
104110
<ClCompile>
@@ -122,7 +128,7 @@
122128
<AdditionalDependencies>$(CoreLibraryDependencies);%(AdditionalDependencies);vendor/sdk/foobar2000/shared/shared-$(Platform).lib</AdditionalDependencies>
123129
</Link>
124130
<PostBuildEvent>
125-
<Message>copy built files</Message>
131+
<Message>Copy built file (x86)</Message>
126132
<Command>copy $(TargetPath) $(ProjectDir)dist\</Command>
127133
</PostBuildEvent>
128134
</ItemDefinitionGroup>
@@ -143,6 +149,14 @@
143149
<GenerateDebugInformation>true</GenerateDebugInformation>
144150
<AdditionalDependencies>$(CoreLibraryDependencies);%(AdditionalDependencies);vendor/sdk/foobar2000/shared/shared-$(Platform).lib</AdditionalDependencies>
145151
</Link>
152+
<PostBuildEvent>
153+
<Command>mkdir $(ProjectDir)dist\$(Platform)
154+
155+
copy $(TargetPath) $(ProjectDir)dist\$(Platform)\</Command>
156+
</PostBuildEvent>
157+
<PostBuildEvent>
158+
<Message>Copy built file (x64)</Message>
159+
</PostBuildEvent>
146160
</ItemDefinitionGroup>
147161
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
148162
<ClCompile>
@@ -166,8 +180,9 @@
166180
<AdditionalDependencies>$(CoreLibraryDependencies);%(AdditionalDependencies);vendor/sdk/foobar2000/shared/shared-$(Platform).lib</AdditionalDependencies>
167181
</Link>
168182
<PostBuildEvent>
169-
<Message>copy built files</Message>
183+
<Message>Copy built file (x64)</Message>
170184
<Command>mkdir $(ProjectDir)dist\$(Platform)
185+
171186
copy $(TargetPath) $(ProjectDir)dist\$(Platform)\</Command>
172187
</PostBuildEvent>
173188
</ItemDefinitionGroup>

foo_input_ncm.xcodeproj/project.pbxproj

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,8 @@
285285
CLANG_WARN_UNREACHABLE_CODE = YES;
286286
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
287287
COPY_PHASE_STRIP = NO;
288-
CURRENT_PROJECT_VERSION = 0.3.0;
289-
DEAD_CODE_STRIPPING = YES;
288+
CURRENT_PROJECT_VERSION = 0.3.4;
289+
DEAD_CODE_STRIPPING = NO;
290290
DEBUG_INFORMATION_FORMAT = dwarf;
291291
ENABLE_STRICT_OBJC_MSGSEND = YES;
292292
ENABLE_TESTABILITY = YES;
@@ -315,9 +315,11 @@
315315
vendor/sdk/foobar2000,
316316
);
317317
INCLUDED_RECURSIVE_SEARCH_PATH_SUBDIRECTORIES = src;
318+
INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.music";
318319
LIBRARY_SEARCH_PATHS = "";
319320
LOCALIZATION_PREFERS_STRING_CATALOGS = YES;
320321
MACOSX_DEPLOYMENT_TARGET = 13.3;
322+
MARKETING_VERSION = 0.3.4;
321323
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
322324
MTL_FAST_MATH = YES;
323325
SDKROOT = macosx;
@@ -360,7 +362,7 @@
360362
CLANG_WARN_UNREACHABLE_CODE = YES;
361363
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
362364
COPY_PHASE_STRIP = YES;
363-
CURRENT_PROJECT_VERSION = 0.3.0;
365+
CURRENT_PROJECT_VERSION = 0.3.4;
364366
DEAD_CODE_STRIPPING = YES;
365367
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
366368
ENABLE_NS_ASSERTIONS = NO;
@@ -371,6 +373,7 @@
371373
GCC_NO_COMMON_BLOCKS = YES;
372374
GCC_PRECOMPILE_PREFIX_HEADER = YES;
373375
GCC_PREFIX_HEADER = stdafx.h;
376+
"GCC_PREPROCESSOR_DEFINITIONS[arch=*]" = "NDEBUG=1";
374377
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
375378
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
376379
GCC_WARN_UNDECLARED_SELECTOR = YES;
@@ -384,9 +387,11 @@
384387
vendor/sdk/foobar2000,
385388
);
386389
INCLUDED_RECURSIVE_SEARCH_PATH_SUBDIRECTORIES = src;
390+
INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.music";
387391
LIBRARY_SEARCH_PATHS = "";
388392
LOCALIZATION_PREFERS_STRING_CATALOGS = YES;
389393
MACOSX_DEPLOYMENT_TARGET = 13.3;
394+
MARKETING_VERSION = 0.3.4;
390395
MTL_ENABLE_DEBUG_INFO = NO;
391396
MTL_FAST_MATH = YES;
392397
ONLY_ACTIVE_ARCH = NO;

src/album_art_extractor.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,19 @@ bool ncm_album_art_extractor::is_our_path(const char *p_path, const char *p_exte
77
return stricmp_utf8(p_extension, "ncm") == 0;
88
}
99

10-
album_art_extractor_instance_ptr ncm_album_art_extractor::open(file_ptr p_filehint, const char *p_path,
11-
abort_callback &p_abort) {
10+
album_art_extractor_instance_ptr ncm_album_art_extractor::open(file_ptr p_filehint, const char *p_path, abort_callback &p_abort) {
1211
if (p_filehint.is_empty()) {
1312
if (is_our_path(p_path, pfc::string_extension(p_path).toString())) {
1413
filesystem::g_open_read(p_filehint, p_path, p_abort);
1514
}
1615
}
17-
auto _ncm_file = fb2k::service_new<ncm_file>(p_filehint, p_path);
16+
auto _ncm_file = fb2k::service_new<ncm_file>(p_path);
1817
_ncm_file->parse(ncm_file::parse_contents::NCM_PARSE_ALBUM);
1918
auto album_arts = fb2k::service_new<album_art_extractor_instance_simple>();
20-
auto image = album_art_data_impl::g_create(_ncm_file->image_data.data(), _ncm_file->image_data.size());
19+
auto image = album_art_data_impl::g_create(_ncm_file->image_data().data(), _ncm_file->image_data().size());
2120
album_arts->set(album_art_ids::cover_front, image);
2221
// album_arts->set(album_art_ids::disc, image);
2322
return album_arts;
2423
}
2524

26-
static service_factory_single_t<ncm_album_art_extractor> g_ncm_album_art_extractor;
25+
static service_factory_single_t<ncm_album_art_extractor> g_ncm_album_art_extractor;

src/cipher/abnormal_RC4.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,29 @@ fb2k_ncm::cipher::abnormal_RC4::abnormal_RC4(const uint8_t *seed, size_t len) :
2323
for (int i = 0; i < 256; i++) {
2424
auto k1 = (i + 1) & 0xff;
2525
auto k2 = (k1 + key_box[k1]) & 0xff;
26+
// no exchange comparing to RC4 PRGA
27+
// std::swap(key_box[k1],key_box[k2]); <- this step is missing
2628
auto k = key_box[(key_box[k1] + key_box[k2]) & 0xff];
2729
key_box_[i] = k;
2830
}
2931
}
3032

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

35-
bool fb2k_ncm::cipher::abnormal_RC4::is_valid() const {
36-
return key_seed_.size() && key_box_;
37-
}
38-
39-
abnormal_RC4::abnormal_RC4(abnormal_RC4 &c) : key_seed(key_seed_), key_seed_(c.key_seed_), key_box_(c.key_box_) {}
37+
abnormal_RC4::abnormal_RC4(abnormal_RC4 &c) : key_seed(key_seed_), key_seed_(c.key_seed_), key_box_(c.key_box_), counter_(c.counter_) {}
4038
abnormal_RC4 &abnormal_RC4::operator=(abnormal_RC4 &c) {
4139
key_seed_ = c.key_seed_;
4240
key_box_ = c.key_box_;
41+
counter_ = c.counter_;
4342
return *this;
4443
}
4544
abnormal_RC4::abnormal_RC4(abnormal_RC4 &&c)
46-
: key_seed(key_seed_), key_seed_(std::move(c.key_seed_)), key_box_(std::move(c.key_box_)) {}
45+
: key_seed(key_seed_), key_seed_(std::move(c.key_seed_)), key_box_(std::move(c.key_box_)), counter_(c.counter_) {}
4746
abnormal_RC4 &abnormal_RC4::operator=(abnormal_RC4 &&c) {
4847
key_seed_ = std::move(c.key_seed_);
4948
key_box_ = std::move(c.key_box_);
49+
counter_ = c.counter_;
5050
return *this;
51-
}
51+
}

src/cipher/abnormal_RC4.hpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
#include <memory>
55
#include <functional>
66

7+
#include <ranges>
8+
#include <algorithm>
9+
710
namespace fb2k_ncm::cipher
811
{
912
// special rc4-like BLOCK CIPHER used for ncm files
@@ -18,15 +21,26 @@ namespace fb2k_ncm::cipher
1821
abnormal_RC4 &operator=(abnormal_RC4 &&c);
1922

2023
public:
21-
bool is_valid() const;
24+
inline bool is_valid() const { return key_seed_.size() && key_box_; }
25+
inline auto &reset_counter(size_t offset = 0) {
26+
counter_ = offset;
27+
return *this;
28+
}
2229
std::function<uint8_t(uint8_t, size_t)> get_transform() const;
2330

31+
// c++20 ranges version, returns a transform view
32+
template <std::ranges::range R>
33+
auto transform(const R &r) {
34+
return r | std::views::all | std::views::transform([tf = get_transform(), this](auto b) { return tf(b, counter_++); });
35+
}
36+
2437
public:
2538
const std::vector<uint8_t> &key_seed;
2639

2740
private:
2841
std::vector<uint8_t> key_seed_;
2942
std::shared_ptr<uint8_t[]> key_box_;
43+
size_t counter_ = 0; // to keep decrypt indices on track
3044
};
3145

32-
} // namespace fb2k_ncm::cipher
46+
} // namespace fb2k_ncm::cipher

0 commit comments

Comments
 (0)