Changelog: - re-enable use in release build. - remove ftpsrv and untitled from builtin ghdl options, as both packages are available in the appstore. - add image viewer (png, jpg, bmp) - add music player (bfstm, bfwav, mp3, wav, ogg) - add idv3 tag parsing support for mp3. - add "decyption" of GTA Vice City mp3. - add usbdvd support for music playback and file browsing. - add nsz export support (solid, block, ldm). - add xcz export support (same as above). - add nro fs proper mount support (romfs, nacp, icon). - add program nca fs support. - add bfsar fs support. - re-write the usb protocol, still wip. replaces tinfoil protocol. - all threads are now create with pre-emptive support with the proper affinity mask set. - fix oob crash in libpulsar when a bfwav was opened that had more than 2 channels. - bump yyjson version. - bump usbhsfs version. - disable nvjpg. - add support for theme music of any supported playback type (bfstm, bfwav, mp3, wav, ogg). - add support for setting background music. - add async exit to blocking threads (download, nxlink, ftpsrv) to reduce exit time. - add support for dumping to pc via usb. - add null, deflate, zstd hash options, mainly used for benchmarking. - add sidebar slider (currently unused). - file_viwer can now be used with any filesystem. - filebrowser will only ever stat file once. previously it would keep stat'ing until it succeeded. - disabled themezer due to the api breaking and i am not willing to keep maintaining it. - disable zlt handling in usbds as it's not needed for my api's because the size is always known. - remove usbds enums and GetSpeed() as i pr'd it to libnx. - added support for mounting nca's from any source, including files, memory, nsps, xcis etc. - split the lru cache into it's own header as it's now used in multiple places (nsz, all mounted options). - add support for fetching and decrypting es personalised tickets. - fix es common ticket converting where i forgot to also convert the cert chain as well. - remove the download default music option. - improve performance of libpulsar when opening a bfsar by remove the large setvbuf option. instead, use the default 1k buffer and handle large buffers manually in sphaira by using a lru cache (todo: just write my own bfsar parser). - during app init and exit, load times have been halved as i now load/exit async. timestamps have also been added to measure how long everything takes. - download now async loads / exits the etag json file to improve init times. - add custom zip io to dumper to support writing a zip to any dest (such as usb). - dumper now returns a proper error if the transfer was cancelled by the user. - fatfs mount now sets the timestamp for files. - fatfs mount handles folders with the archive bit by reporting them as a file. - ftpsrv config is async loaded to speed up load times. - nxlink now tries attempt to connect/accept by handling blocking rather than just bailing out. - added support for minini floats. - thread_file_transfer now spawns 3 threads rather than 2, to have the middle thread be a optional processor (mainly used for compressing/decompressing). - added spinner to progress box, taken from nvg demo. - progress box disables sleep mode on init. - add gamecard detection to game menu to detect a refresh. - handle xci that have the key area prepended. - change gamecard mount fs to use the xci mount code instead of native fs, that way we can see all the partitions rather than just secure. - reformat the ghdl entries to show the timestamp first. - support for exporting saves to pc via usb. - zip fs now uses lru cache.
359 lines
12 KiB
C++
359 lines
12 KiB
C++
#pragma once
|
|
|
|
#include "fs.hpp"
|
|
#include <vector>
|
|
#include <string>
|
|
#include <functional>
|
|
#include <unordered_map>
|
|
#include <algorithm>
|
|
#include <stop_token>
|
|
#include <switch.h>
|
|
|
|
namespace sphaira::curl {
|
|
|
|
enum {
|
|
Flag_None = 0,
|
|
|
|
// requests to download send etag in the header.
|
|
// the received etag is then saved on success.
|
|
// this api is only available on downloading to file.
|
|
Flag_Cache = 1 << 0,
|
|
|
|
// sets CURLOPT_NOBODY.
|
|
Flag_NoBody = 1 << 1,
|
|
};
|
|
|
|
enum class Priority {
|
|
Normal, // gets pushed to the back of the queue
|
|
High, // gets pushed to the front of the queue
|
|
};
|
|
|
|
struct Api;
|
|
struct ApiResult;
|
|
|
|
using Path = fs::FsPath;
|
|
using OnComplete = std::function<void(ApiResult& result)>;
|
|
using OnProgress = std::function<bool(s64 dltotal, s64 dlnow, s64 ultotal, s64 ulnow)>;
|
|
using OnUploadCallback = std::function<size_t(void *ptr, size_t size)>;
|
|
using OnUploadSeek = std::function<bool(s64 offset)>;
|
|
using StopToken = std::stop_token;
|
|
|
|
struct Url {
|
|
Url() = default;
|
|
Url(const std::string& str) : m_str{str} {}
|
|
std::string m_str;
|
|
};
|
|
|
|
struct Fields {
|
|
Fields() = default;
|
|
Fields(const std::string& str) : m_str{str} {}
|
|
std::string m_str;
|
|
};
|
|
|
|
struct Header {
|
|
Header() = default;
|
|
Header(std::initializer_list<std::pair<const std::string, std::string>>&& p) : m_map{std::forward<decltype(p)>(p)} {}
|
|
std::unordered_map<std::string, std::string> m_map;
|
|
|
|
auto Find(const std::string& key) const {
|
|
return std::find_if(m_map.cbegin(), m_map.cend(), [&key](auto& e) {
|
|
return !strcasecmp(key.c_str(), e.first.c_str());
|
|
});
|
|
}
|
|
};
|
|
|
|
struct Flags {
|
|
Flags() = default;
|
|
Flags(u32 flags) : m_flags{flags} {}
|
|
u32 m_flags{Flag_None};
|
|
};
|
|
|
|
struct Port {
|
|
Port() = default;
|
|
Port(u16 port) : m_port{port} {}
|
|
u16 m_port{};
|
|
};
|
|
|
|
struct CustomRequest {
|
|
CustomRequest() = default;
|
|
CustomRequest(const std::string& str) : m_str{str} {}
|
|
std::string m_str;
|
|
};
|
|
|
|
struct UserPass {
|
|
UserPass() = default;
|
|
UserPass(const std::string& user) : m_user{user} {}
|
|
UserPass(const std::string& user, const std::string& pass) : m_user{user}, m_pass{pass} {}
|
|
std::string m_user;
|
|
std::string m_pass;
|
|
};
|
|
|
|
struct UploadInfo {
|
|
UploadInfo() = default;
|
|
UploadInfo(const std::string& name) : m_name{name} {}
|
|
UploadInfo(const std::string& name, s64 size, const OnUploadCallback& cb) : m_name{name}, m_size{size}, m_callback{cb} {}
|
|
UploadInfo(const std::string& name, const std::vector<u8>& data) : m_name{name}, m_data{data} {}
|
|
std::string m_name{};
|
|
std::vector<u8> m_data{};
|
|
s64 m_size{};
|
|
OnUploadCallback m_callback{};
|
|
};
|
|
|
|
struct Bearer {
|
|
Bearer() = default;
|
|
Bearer(const std::string& str) : m_str{str} {}
|
|
std::string m_str;
|
|
};
|
|
|
|
struct PubKey {
|
|
PubKey() = default;
|
|
PubKey(const std::string& str) : m_str{str} {}
|
|
std::string m_str;
|
|
};
|
|
|
|
struct PrivKey {
|
|
PrivKey() = default;
|
|
PrivKey(const std::string& str) : m_str{str} {}
|
|
std::string m_str;
|
|
};
|
|
|
|
struct ApiResult {
|
|
bool success;
|
|
long code;
|
|
Header header; // returned headers in request
|
|
std::vector<u8> data; // empty if downloaded a file
|
|
fs::FsPath path; // empty if downloaded memory
|
|
};
|
|
|
|
struct DownloadEventData {
|
|
OnComplete callback;
|
|
ApiResult result;
|
|
StopToken stoken;
|
|
};
|
|
|
|
// helper that generates the api using an location.
|
|
#define CURL_LOCATION_TO_API(loc) \
|
|
curl::Url{loc.url}, \
|
|
curl::UserPass{loc.user, loc.pass}, \
|
|
curl::Bearer{loc.bearer}, \
|
|
curl::PubKey{loc.pub_key}, \
|
|
curl::PrivKey{loc.priv_key}, \
|
|
curl::Port(loc.port)
|
|
|
|
auto Init() -> bool;
|
|
void Exit();
|
|
void ExitSignal();
|
|
|
|
// sync functions
|
|
auto ToMemory(const Api& e) -> ApiResult;
|
|
auto ToFile(const Api& e) -> ApiResult;
|
|
auto FromMemory(const Api& e) -> ApiResult;
|
|
auto FromFile(const Api& e) -> ApiResult;
|
|
|
|
// async functions
|
|
auto ToMemoryAsync(const Api& e) -> bool;
|
|
auto ToFileAsync(const Api& e) -> bool;
|
|
auto FromMemoryAsync(const Api& e) -> bool;
|
|
auto FromFileAsync(const Api& e) -> bool;
|
|
|
|
// uses curl to convert string to their %XX
|
|
auto EscapeString(const std::string& str) -> std::string;
|
|
|
|
struct Api {
|
|
Api() = default;
|
|
|
|
template <typename... Ts>
|
|
Api(Ts&&... ts) {
|
|
Api::set_option(std::forward<Ts>(ts)...);
|
|
}
|
|
|
|
template <typename... Ts>
|
|
auto To(Ts&&... ts) {
|
|
if constexpr(std::disjunction_v<std::is_same<Path, Ts>...>) {
|
|
return ToFile(std::forward<Ts>(ts)...);
|
|
} else {
|
|
return ToMemory(std::forward<Ts>(ts)...);
|
|
}
|
|
}
|
|
|
|
template <typename... Ts>
|
|
auto From(Ts&&... ts) {
|
|
if constexpr(std::disjunction_v<std::is_same<Path, Ts>...>) {
|
|
return FromFile(std::forward<Ts>(ts)...);
|
|
} else {
|
|
return FromMemory(std::forward<Ts>(ts)...);
|
|
}
|
|
}
|
|
|
|
template <typename... Ts>
|
|
auto ToAsync(Ts&&... ts) {
|
|
if constexpr(std::disjunction_v<std::is_same<Path, Ts>...>) {
|
|
return ToFileAsync(std::forward<Ts>(ts)...);
|
|
} else {
|
|
return ToMemoryAsync(std::forward<Ts>(ts)...);
|
|
}
|
|
}
|
|
|
|
template <typename... Ts>
|
|
auto FromAsync(Ts&&... ts) {
|
|
if constexpr(std::disjunction_v<std::is_same<Path, Ts>...>) {
|
|
return FromFileAsync(std::forward<Ts>(ts)...);
|
|
} else {
|
|
return FromMemoryAsync(std::forward<Ts>(ts)...);
|
|
}
|
|
}
|
|
|
|
template <typename... Ts>
|
|
auto ToMemory(Ts&&... ts) {
|
|
static_assert(std::disjunction_v<std::is_same<Url, Ts>...>, "Url must be specified");
|
|
static_assert(!std::disjunction_v<std::is_same<Path, Ts>...>, "Path must not valid for memory");
|
|
static_assert(!std::disjunction_v<std::is_same<OnComplete, Ts>...>, "OnComplete must not be specified");
|
|
Api::set_option(std::forward<Ts>(ts)...);
|
|
return curl::ToMemory(*this);
|
|
}
|
|
|
|
template <typename... Ts>
|
|
auto FromMemory(Ts&&... ts) {
|
|
static_assert(std::disjunction_v<std::is_same<Url, Ts>...>, "Url must be specified");
|
|
static_assert(std::disjunction_v<std::is_same<UploadInfo, Ts>...>, "UploadInfo must be specified");
|
|
static_assert(!std::disjunction_v<std::is_same<Path, Ts>...>, "Path must not valid for memory");
|
|
static_assert(!std::disjunction_v<std::is_same<OnComplete, Ts>...>, "OnComplete must not be specified");
|
|
Api::set_option(std::forward<Ts>(ts)...);
|
|
return curl::FromMemory(*this);
|
|
}
|
|
|
|
template <typename... Ts>
|
|
auto ToFile(Ts&&... ts) {
|
|
static_assert(std::disjunction_v<std::is_same<Url, Ts>...>, "Url must be specified");
|
|
static_assert(std::disjunction_v<std::is_same<Path, Ts>...>, "Path must be specified");
|
|
static_assert(!std::disjunction_v<std::is_same<OnComplete, Ts>...>, "OnComplete must not be specified");
|
|
Api::set_option(std::forward<Ts>(ts)...);
|
|
return curl::ToFile(*this);
|
|
}
|
|
|
|
template <typename... Ts>
|
|
auto FromFile(Ts&&... ts) {
|
|
static_assert(std::disjunction_v<std::is_same<Url, Ts>...>, "Url must be specified");
|
|
static_assert(std::disjunction_v<std::is_same<Path, Ts>...>, "Path must be specified");
|
|
static_assert(std::disjunction_v<std::is_same<UploadInfo, Ts>...>, "UploadInfo must be specified");
|
|
static_assert(!std::disjunction_v<std::is_same<OnComplete, Ts>...>, "OnComplete must not be specified");
|
|
Api::set_option(std::forward<Ts>(ts)...);
|
|
return curl::FromFile(*this);
|
|
}
|
|
|
|
template <typename... Ts>
|
|
auto ToMemoryAsync(Ts&&... ts) {
|
|
static_assert(std::disjunction_v<std::is_same<Url, Ts>...>, "Url must be specified");
|
|
static_assert(std::disjunction_v<std::is_same<OnComplete, Ts>...>, "OnComplete must be specified");
|
|
static_assert(!std::disjunction_v<std::is_same<Path, Ts>...>, "Path must not valid for memory");
|
|
static_assert(std::disjunction_v<std::is_same<StopToken, Ts>...>, "StopToken must be specified");
|
|
Api::set_option(std::forward<Ts>(ts)...);
|
|
return curl::ToMemoryAsync(*this);
|
|
}
|
|
|
|
template <typename... Ts>
|
|
auto FromMemoryAsync(Ts&&... ts) {
|
|
static_assert(std::disjunction_v<std::is_same<Url, Ts>...>, "Url must be specified");
|
|
static_assert(std::disjunction_v<std::is_same<UploadInfo, Ts>...>, "UploadInfo must be specified");
|
|
static_assert(std::disjunction_v<std::is_same<OnComplete, Ts>...>, "OnComplete must be specified");
|
|
static_assert(!std::disjunction_v<std::is_same<Path, Ts>...>, "Path must not valid for memory");
|
|
static_assert(std::disjunction_v<std::is_same<StopToken, Ts>...>, "StopToken must be specified");
|
|
Api::set_option(std::forward<Ts>(ts)...);
|
|
return curl::FromMemoryAsync(*this);
|
|
}
|
|
|
|
template <typename... Ts>
|
|
auto ToFileAsync(Ts&&... ts) {
|
|
static_assert(std::disjunction_v<std::is_same<Url, Ts>...>, "Url must be specified");
|
|
static_assert(std::disjunction_v<std::is_same<Path, Ts>...>, "Path must be specified");
|
|
static_assert(std::disjunction_v<std::is_same<OnComplete, Ts>...>, "OnComplete must be specified");
|
|
static_assert(std::disjunction_v<std::is_same<StopToken, Ts>...>, "StopToken must be specified");
|
|
Api::set_option(std::forward<Ts>(ts)...);
|
|
return curl::ToFileAsync(*this);
|
|
}
|
|
|
|
template <typename... Ts>
|
|
auto FromFileAsync(Ts&&... ts) {
|
|
static_assert(std::disjunction_v<std::is_same<Url, Ts>...>, "Url must be specified");
|
|
static_assert(std::disjunction_v<std::is_same<Path, Ts>...>, "Path must be specified");
|
|
static_assert(std::disjunction_v<std::is_same<UploadInfo, Ts>...>, "UploadInfo must be specified");
|
|
static_assert(std::disjunction_v<std::is_same<OnComplete, Ts>...>, "OnComplete must be specified");
|
|
static_assert(std::disjunction_v<std::is_same<StopToken, Ts>...>, "StopToken must be specified");
|
|
Api::set_option(std::forward<Ts>(ts)...);
|
|
return curl::FromFileAsync(*this);
|
|
}
|
|
|
|
void SetUpload(bool enable) { m_is_upload = enable; }
|
|
|
|
auto IsUpload() const { return m_is_upload; }
|
|
auto& GetUrl() const { return m_url.m_str; }
|
|
auto& GetFields() const { return m_fields.m_str; }
|
|
auto& GetHeader() const { return m_header; }
|
|
auto& GetFlags() const { return m_flags.m_flags; }
|
|
auto& GetPath() const { return m_path; }
|
|
auto& GetPort() const { return m_port.m_port; }
|
|
auto& GetCustomRequest() const { return m_custom_request.m_str; }
|
|
auto& GetUserPass() const { return m_userpass; }
|
|
auto& GetBearer() const { return m_bearer.m_str; }
|
|
auto& GetPubKey() const { return m_pub_key.m_str; }
|
|
auto& GetPrivKey() const { return m_priv_key.m_str; }
|
|
auto& GetUploadInfo() const { return m_info; }
|
|
auto& GetOnComplete() const { return m_on_complete; }
|
|
auto& GetOnProgress() const { return m_on_progress; }
|
|
auto& GetOnUploadSeek() const { return m_on_upload_seek; }
|
|
auto& GetPriority() const { return m_prio; }
|
|
auto& GetToken() const { return m_stoken; }
|
|
|
|
void SetOption(Url&& v) { m_url = v; }
|
|
void SetOption(Fields&& v) { m_fields = v; }
|
|
void SetOption(Header&& v) { m_header = v; }
|
|
void SetOption(Flags&& v) { m_flags = v; }
|
|
void SetOption(Path&& v) { m_path = v; }
|
|
void SetOption(Port&& v) { m_port = v; }
|
|
void SetOption(CustomRequest&& v) { m_custom_request = v; }
|
|
void SetOption(UserPass&& v) { m_userpass = v; }
|
|
void SetOption(Bearer&& v) { m_bearer = v; }
|
|
void SetOption(PubKey&& v) { m_pub_key = v; }
|
|
void SetOption(PrivKey&& v) { m_priv_key = v; }
|
|
void SetOption(UploadInfo&& v) { m_info = v; }
|
|
void SetOption(OnComplete&& v) { m_on_complete = v; }
|
|
void SetOption(OnProgress&& v) { m_on_progress = v; }
|
|
void SetOption(OnUploadSeek&& v) { m_on_upload_seek = v; }
|
|
void SetOption(Priority&& v) { m_prio = v; }
|
|
void SetOption(StopToken&& v) { m_stoken = v; }
|
|
|
|
template <typename T>
|
|
void set_option(T&& t) {
|
|
SetOption(std::forward<T>(t));
|
|
}
|
|
|
|
template <typename T, typename... Ts>
|
|
void set_option(T&& t, Ts&&... ts) {
|
|
set_option(std::forward<T>(t));
|
|
set_option(std::forward<Ts>(ts)...);
|
|
}
|
|
|
|
private:
|
|
Url m_url{};
|
|
Fields m_fields{};
|
|
Header m_header{};
|
|
Flags m_flags{};
|
|
Path m_path{};
|
|
Port m_port{};
|
|
CustomRequest m_custom_request{};
|
|
UserPass m_userpass{};
|
|
Bearer m_bearer{};
|
|
PubKey m_pub_key{};
|
|
PrivKey m_priv_key{};
|
|
UploadInfo m_info{};
|
|
OnComplete m_on_complete{};
|
|
OnProgress m_on_progress{};
|
|
OnUploadSeek m_on_upload_seek{};
|
|
Priority m_prio{Priority::High};
|
|
std::stop_source m_stop_source{};
|
|
StopToken m_stoken{m_stop_source.get_token()};
|
|
bool m_is_upload{};
|
|
};
|
|
|
|
} // namespace sphaira::curl
|