Initial commit

This commit is contained in:
2026-02-02 04:50:13 +01:00
commit 5b11698731
22592 changed files with 7677434 additions and 0 deletions

View File

@@ -0,0 +1,173 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
// Allow use of stuff in <time.h> and abort()
#define FORBIDDEN_SYMBOL_EXCEPTION_time_h
#define FORBIDDEN_SYMBOL_EXCEPTION_abort
// Disable printf override in common/forbidden.h to avoid
// clashes with log.h from the Android SDK.
// That header file uses
// __attribute__ ((format(printf, 3, 4)))
// which gets messed up by our override mechanism; this could
// be avoided by either changing the Android SDK to use the equally
// legal and valid
// __attribute__ ((format(__printf__, 3, 4)))
// or by refining our printf override to use a varadic macro
// (which then wouldn't be portable, though).
// Anyway, for now we just disable the printf override globally
// for the Android port
#define FORBIDDEN_SYMBOL_EXCEPTION_printf
#include "backends/platform/android/android.h"
#include "backends/networking/basic/android/jni.h"
#ifdef USE_HTTP
#include "backends/networking/http/android/networkreadstream-android.h"
#endif
namespace Networking {
jclass NetJNI::_CLS_URL = nullptr;
jmethodID NetJNI::_MID_url_init = 0;
jmethodID NetJNI::_MID_url_getProtocol = 0;
jmethodID NetJNI::_MID_url_getHost = 0;
jmethodID NetJNI::_MID_url_getPort = 0;
jmethodID NetJNI::_MID_url_getDefaultPort = 0;
jclass NetJNI::_CLS_Socket = nullptr;
jmethodID NetJNI::_MID_socket_init = 0;
jmethodID NetJNI::_MID_socket_ready = 0;
jmethodID NetJNI::_MID_socket_send = 0;
jmethodID NetJNI::_MID_socket_recv = 0;
jmethodID NetJNI::_MID_socket_close = 0;
#ifdef USE_HTTP
jmethodID NetJNI::_MID_manager_init = 0;
jmethodID NetJNI::_MID_manager_startRequest = 0;
jmethodID NetJNI::_MID_manager_poll = 0;
jfieldID NetJNI::_FID_manager__empty = 0;
jclass NetJNI::_CLS_HTTPRequest = nullptr;
jmethodID NetJNI::_MID_request_bufinit = 0;
jmethodID NetJNI::_MID_request_forminit = 0;
jmethodID NetJNI::_MID_request_cancel = 0;
jmethodID NetJNI::_MID_request_getURL = 0;
const JNINativeMethod NetJNI::_natives_request[] = {
{ "gotHeaders", "(J[Ljava/lang/String;)V",
(void *)NetworkReadStreamAndroid::gotHeaders },
{ "gotData", "(J[BII)V",
(void *)NetworkReadStreamAndroid::gotData },
{ "finished", "(JILjava/lang/String;)V",
(void *)NetworkReadStreamAndroid::finished_ },
};
#endif
bool NetJNI::_init = false;
void NetJNI::init(JNIEnv *env) {
if (_init) {
return;
}
// We can't call error here as the backend is not built yet
#define FIND_CONSTRUCTOR(prefix, signature) do { \
_MID_ ## prefix ## init = env->GetMethodID(cls, "<init>", signature "V");\
if (_MID_ ## prefix ## init == 0) { \
LOGE("Can't find method ID <init>"); \
abort(); \
} \
} while (0)
#define FIND_METHOD(prefix, name, signature) do { \
_MID_ ## prefix ## name = env->GetMethodID(cls, #name, signature); \
if (_MID_ ## prefix ## name == 0) { \
LOGE("Can't find method ID " #name); \
abort(); \
} \
} while (0)
#define FIND_FIELD(prefix, name, signature) do { \
_FID_ ## prefix ## name = env->GetFieldID(cls, #name, signature); \
if (_FID_ ## prefix ## name == 0) { \
LOGE("Can't find field ID " #name); \
abort(); \
} \
} while (0)
jclass cls = env->FindClass("java/net/URL");
_CLS_URL = (jclass)env->NewGlobalRef(cls);
FIND_CONSTRUCTOR(url_, "(Ljava/lang/String;)");
FIND_METHOD(url_, getProtocol, "()Ljava/lang/String;");
FIND_METHOD(url_, getHost, "()Ljava/lang/String;");
FIND_METHOD(url_, getPort, "()I");
FIND_METHOD(url_, getDefaultPort, "()I");
env->DeleteLocalRef(cls);
cls = env->FindClass("org/scummvm/scummvm/net/SSocket");
_CLS_Socket = (jclass)env->NewGlobalRef(cls);
FIND_CONSTRUCTOR(socket_, "(Ljava/lang/String;)");
FIND_METHOD(socket_, ready, "()I");
FIND_METHOD(socket_, send, "([B)I");
FIND_METHOD(socket_, recv, "([B)I");
FIND_METHOD(socket_, close, "()V");
env->DeleteLocalRef(cls);
#ifdef USE_HTTP
cls = env->FindClass("org/scummvm/scummvm/net/HTTPManager");
FIND_CONSTRUCTOR(manager_, "()");
FIND_METHOD(manager_, startRequest, "(Lorg/scummvm/scummvm/net/HTTPRequest;)V");
FIND_METHOD(manager_, poll, "()V");
FIND_FIELD(manager_, _empty, "Z");
env->DeleteLocalRef(cls);
cls = env->FindClass("org/scummvm/scummvm/net/HTTPRequest");
_CLS_HTTPRequest = (jclass)env->NewGlobalRef(cls);
if (env->RegisterNatives(cls, _natives_request, ARRAYSIZE(_natives_request)) < 0) {
LOGE("Can't register natives for org/scummvm/scummvm/net/HTTPRequest");
abort();
}
FIND_CONSTRUCTOR(request_buf, "(JLjava/lang/String;[Ljava/lang/String;[BZZZ)");
FIND_CONSTRUCTOR(request_form, "(JLjava/lang/String;[Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/String;)");
FIND_METHOD(request_, cancel, "()V");
FIND_METHOD(request_, getURL, "()Ljava/lang/String;");
env->DeleteLocalRef(cls);
#endif
#undef FIND_FIELD
#undef FIND_METHOD
#undef FIND_CONSTRUCTOR
_init = true;
}
} // End of namespace Networking

View File

@@ -0,0 +1,79 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef BACKENDS_NETWORKING_BASIC_ANDROID_JNI_H
#define BACKENDS_NETWORKING_BASIC_ANDROID_JNI_H
#include <jni.h>
namespace Networking {
class NetJNI {
#ifdef USE_HTTP
friend class ConnectionManagerAndroid;
friend class NetworkReadStreamAndroid;
#endif
friend class AndroidSocket;
friend class AndroidURL;
public:
static void init(JNIEnv *env);
private:
static jclass _CLS_URL;
static jmethodID _MID_url_init;
static jmethodID _MID_url_getProtocol;
static jmethodID _MID_url_getHost;
static jmethodID _MID_url_getPort;
static jmethodID _MID_url_getDefaultPort;
static jclass _CLS_Socket;
static jmethodID _MID_socket_init;
static jmethodID _MID_socket_ready;
static jmethodID _MID_socket_send;
static jmethodID _MID_socket_recv;
static jmethodID _MID_socket_close;
#ifdef USE_HTTP
static jmethodID _MID_manager_init;
static jmethodID _MID_manager_startRequest;
static jmethodID _MID_manager_poll;
static jfieldID _FID_manager__empty;
static const JNINativeMethod _natives_request[];
static jclass _CLS_HTTPRequest;
static jmethodID _MID_request_bufinit;
static jmethodID _MID_request_forminit;
static jmethodID _MID_request_cancel;
static jmethodID _MID_request_getURL;
#endif
static bool _init;
};
} // End of namespace Networking
#endif

View File

@@ -0,0 +1,156 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
// Allow use of stuff in <time.h> and abort()
#define FORBIDDEN_SYMBOL_EXCEPTION_time_h
#define FORBIDDEN_SYMBOL_EXCEPTION_abort
// Disable printf override in common/forbidden.h to avoid
// clashes with log.h from the Android SDK.
// That header file uses
// __attribute__ ((format(printf, 3, 4)))
// which gets messed up by our override mechanism; this could
// be avoided by either changing the Android SDK to use the equally
// legal and valid
// __attribute__ ((format(__printf__, 3, 4)))
// or by refining our printf override to use a varadic macro
// (which then wouldn't be portable, though).
// Anyway, for now we just disable the printf override globally
// for the Android port
#define FORBIDDEN_SYMBOL_EXCEPTION_printf
#include "backends/platform/android/android.h"
#include "backends/platform/android/jni-android.h"
#include "backends/networking/basic/android/jni.h"
#include "backends/networking/basic/android/socket.h"
#include "common/debug.h"
#include "common/textconsole.h"
namespace Networking {
Socket *Socket::connect(const Common::String &url) {
return AndroidSocket::connect(url);
}
Socket *AndroidSocket::connect(const Common::String &url) {
JNIEnv *env = JNI::getEnv();
NetJNI::init(env);
jstring url_obj = env->NewStringUTF(url.c_str());
jobject socket_obj = env->NewObject(NetJNI::_CLS_Socket, NetJNI::_MID_socket_init, url_obj);
env->DeleteLocalRef(url_obj);
if (env->ExceptionCheck()) {
LOGE("Socket::<init> failed");
env->ExceptionDescribe();
env->ExceptionClear();
return nullptr;
}
Socket *socket = new AndroidSocket(env, socket_obj);
env->DeleteLocalRef(socket_obj);
return socket;
}
AndroidSocket::AndroidSocket(JNIEnv *env, jobject socket) {
_socket = env->NewGlobalRef(socket);
}
AndroidSocket::~AndroidSocket() {
JNIEnv *env = JNI::getEnv();
env->CallVoidMethod(_socket, NetJNI::_MID_socket_close);
if (env->ExceptionCheck()) {
LOGE("Socket::close failed");
env->ExceptionDescribe();
env->ExceptionClear();
}
env->DeleteGlobalRef(_socket);
}
int AndroidSocket::ready() {
JNIEnv *env = JNI::getEnv();
jint ret = env->CallIntMethod(_socket, NetJNI::_MID_socket_ready);
if (env->ExceptionCheck()) {
LOGE("Socket::ready failed");
env->ExceptionDescribe();
env->ExceptionClear();
// In doubt make it ready
return 1;
}
return ret;
}
size_t AndroidSocket::send(const char *data, int len) {
JNIEnv *env = JNI::getEnv();
jbyteArray buffer_obj = env->NewByteArray(len);
env->SetByteArrayRegion(buffer_obj, 0, len, (const jbyte *)data);
jint sent = env->CallIntMethod(_socket, NetJNI::_MID_socket_send, buffer_obj);
env->DeleteLocalRef(buffer_obj);
if (env->ExceptionCheck()) {
LOGE("Socket::send failed");
env->ExceptionDescribe();
env->ExceptionClear();
return 0;
}
return sent;
}
size_t AndroidSocket::recv(void *data, int maxLen) {
JNIEnv *env = JNI::getEnv();
jbyteArray buffer_obj = env->NewByteArray(maxLen);
jint recvd = env->CallIntMethod(_socket, NetJNI::_MID_socket_recv, buffer_obj);
assert(recvd <= maxLen);
if (env->ExceptionCheck()) {
LOGE("Socket::send failed");
env->ExceptionDescribe();
env->ExceptionClear();
env->DeleteLocalRef(buffer_obj);
return 0;
}
env->GetByteArrayRegion(buffer_obj, 0, recvd, (jbyte *)data);
return recvd;
}
} // End of namespace Networking

View File

@@ -0,0 +1,47 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef BACKENDS_NETWORKING_BASIC_CURL_SOCKET_H
#define BACKENDS_NETWORKING_BASIC_CURL_SOCKET_H
#include <jni.h>
#include "backends/networking/basic/socket.h"
namespace Networking {
class AndroidSocket : public Socket {
public:
static Socket *connect(const Common::String &url);
AndroidSocket(JNIEnv *env, jobject socket);
~AndroidSocket() override;
int ready() override;
size_t send(const char *data, int len) override;
size_t recv(void *data, int maxLen) override;
private:
jobject _socket;
};
} // End of namespace Networking
#endif

View File

@@ -0,0 +1,180 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
// Allow use of stuff in <time.h> and abort()
#define FORBIDDEN_SYMBOL_EXCEPTION_time_h
#define FORBIDDEN_SYMBOL_EXCEPTION_abort
// Disable printf override in common/forbidden.h to avoid
// clashes with log.h from the Android SDK.
// That header file uses
// __attribute__ ((format(printf, 3, 4)))
// which gets messed up by our override mechanism; this could
// be avoided by either changing the Android SDK to use the equally
// legal and valid
// __attribute__ ((format(__printf__, 3, 4)))
// or by refining our printf override to use a varadic macro
// (which then wouldn't be portable, though).
// Anyway, for now we just disable the printf override globally
// for the Android port
#define FORBIDDEN_SYMBOL_EXCEPTION_printf
#include "backends/platform/android/android.h"
#include "backends/platform/android/jni-android.h"
#include "backends/networking/basic/android/jni.h"
#include "backends/networking/basic/android/url.h"
#include "common/debug.h"
#include "common/textconsole.h"
namespace Networking {
URL *URL::parseURL(const Common::String &url) {
return AndroidURL::parseURL(url);
}
URL *AndroidURL::parseURL(const Common::String &url) {
JNIEnv *env = JNI::getEnv();
NetJNI::init(env);
jstring url_sobj = env->NewStringUTF(url.c_str());
jobject url_obj = env->NewObject(NetJNI::_CLS_URL, NetJNI::_MID_url_init, url_sobj);
env->DeleteLocalRef(url_sobj);
if (env->ExceptionCheck()) {
LOGE("URL::<init> failed");
env->ExceptionDescribe();
env->ExceptionClear();
return nullptr;
}
URL *url_ = new AndroidURL(env, url_obj);
env->DeleteLocalRef(url_obj);
return url_;
}
AndroidURL::AndroidURL(JNIEnv *env, jobject url) {
_url = env->NewGlobalRef(url);
}
AndroidURL::~AndroidURL() {
JNIEnv *env = JNI::getEnv();
env->DeleteGlobalRef(_url);
}
Common::String AndroidURL::getScheme() const {
JNIEnv *env = JNI::getEnv();
jstring protocol_obj = (jstring)env->CallObjectMethod(_url, NetJNI::_MID_url_getProtocol);
if (env->ExceptionCheck()) {
LOGE("URL::getProtocol failed");
env->ExceptionDescribe();
env->ExceptionClear();
return Common::String();
}
uint length = env->GetStringLength(protocol_obj);
if (!length) {
env->DeleteLocalRef(protocol_obj);
return Common::String();
}
const char *protocol_ptr = env->GetStringUTFChars(protocol_obj, 0);
if (!protocol_ptr) {
env->DeleteLocalRef(protocol_obj);
return Common::String();
}
Common::String result(protocol_ptr, length);
env->ReleaseStringUTFChars(protocol_obj, protocol_ptr);
env->DeleteLocalRef(protocol_obj);
return result;
}
Common::String AndroidURL::getHost() const {
JNIEnv *env = JNI::getEnv();
jstring protocol_obj = (jstring)env->CallObjectMethod(_url, NetJNI::_MID_url_getHost);
if (env->ExceptionCheck()) {
LOGE("URL::getHost failed");
env->ExceptionDescribe();
env->ExceptionClear();
return Common::String();
}
uint length = env->GetStringLength(protocol_obj);
if (!length) {
env->DeleteLocalRef(protocol_obj);
return Common::String();
}
const char *protocol_ptr = env->GetStringUTFChars(protocol_obj, 0);
if (!protocol_ptr) {
env->DeleteLocalRef(protocol_obj);
return Common::String();
}
Common::String result(protocol_ptr, length);
env->ReleaseStringUTFChars(protocol_obj, protocol_ptr);
env->DeleteLocalRef(protocol_obj);
return result;
}
int AndroidURL::getPort(bool defaultPort) const {
JNIEnv *env = JNI::getEnv();
jint port = env->CallIntMethod(_url, NetJNI::_MID_url_getPort);
if (env->ExceptionCheck()) {
LOGE("URL::getPort failed");
env->ExceptionDescribe();
env->ExceptionClear();
return -1;
}
if (port == -1 && defaultPort) {
port = env->CallIntMethod(_url, NetJNI::_MID_url_getDefaultPort);
if (env->ExceptionCheck()) {
LOGE("URL::getDefaultPort failed");
env->ExceptionDescribe();
env->ExceptionClear();
return -1;
}
}
if (port == -1) {
port = 0;
}
return port;
}
} // End of namespace Networking

View File

@@ -0,0 +1,47 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef BACKENDS_NETWORKING_BASIC_ANDROID_URL_H
#define BACKENDS_NETWORKING_BASIC_ANDROID_URL_H
#include <jni.h>
#include "backends/networking/basic/url.h"
namespace Networking {
class AndroidURL : public URL {
public:
static URL *parseURL(const Common::String &url);
AndroidURL(JNIEnv *env, jobject url);
~AndroidURL() override;
Common::String getScheme() const override;
Common::String getHost() const override;
int getPort(bool returnDefault = false) const override;
private:
jobject _url;
};
} // End of Namespace Networking
#endif

View File

@@ -0,0 +1,52 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "backends/networking/basic/curl/cacert.h"
#include "common/fs.h"
namespace Networking {
Common::String getCaCertPath() {
#if defined(DATA_PATH)
static enum {
kNotInitialized,
kFileNotFound,
kFileExists
} state = kNotInitialized;
if (state == kNotInitialized) {
Common::FSNode node(DATA_PATH "/cacert.pem");
state = node.exists() ? kFileExists : kFileNotFound;
}
if (state == kFileExists) {
return DATA_PATH "/cacert.pem";
} else {
return "";
}
#else
return "";
#endif
}
} // End of namespace Networking

View File

@@ -0,0 +1,33 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef BACKENDS_NETWORKING_BASIC_CURL_CACERT_H
#define BACKENDS_NETWORKING_BASIC_CURL_CACERT_H
#include "common/str.h"
namespace Networking {
/** Return the path to the CA certificates bundle. */
Common::String getCaCertPath();
}
#endif

View File

@@ -0,0 +1,177 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#define FORBIDDEN_SYMBOL_ALLOW_ALL
#include <curl/curl.h>
#include "backends/networking/basic/curl/socket.h"
#include "backends/networking/basic/curl/cacert.h"
#include "common/debug.h"
#include "common/system.h"
// Auxiliary function that waits on the socket.
// From https://github.com/curl/curl/blob/master/docs/examples/sendrecv.c
static int waitOnSocket(curl_socket_t sockfd, int for_recv, long timeout_ms) {
struct timeval tv {};
fd_set infd {}, outfd {}, errfd {};
int res;
tv.tv_sec = timeout_ms / 1000;
tv.tv_usec = (timeout_ms % 1000) * 1000;
FD_ZERO(&infd);
FD_ZERO(&outfd);
FD_ZERO(&errfd);
FD_SET(sockfd, &errfd); /* always check for error */
if(for_recv) {
FD_SET(sockfd, &infd);
} else {
FD_SET(sockfd, &outfd);
}
/* select() returns the number of signalled sockets or -1 */
res = select((int)sockfd + 1, &infd, &outfd, &errfd, &tv);
return res;
}
namespace Networking {
Socket *Socket::connect(const Common::String &url) {
CURL *easy = curl_easy_init();
if (!easy) {
return nullptr;
}
curl_easy_setopt(easy, CURLOPT_URL, url.c_str());
// Just connect to the host, do not do any transfers.
curl_easy_setopt(easy, CURLOPT_CONNECT_ONLY, 1L);
// libcurl won't connect to SSL connections
// with VERIFYPEER enabled because we do not ship
// with a CA bundle in these platforms.
// So let's disable it.
#if defined NINTENDO_SWITCH || defined PSP2
curl_easy_setopt(easy, CURLOPT_SSL_VERIFYPEER, 0);
#endif
Common::String caCertPath = getCaCertPath();
if (!caCertPath.empty()) {
curl_easy_setopt(easy, CURLOPT_CAINFO, caCertPath.c_str());
}
CURLcode res = curl_easy_perform(easy);
if (res != CURLE_OK) {
warning("libcurl: Failed to connect: %s", curl_easy_strerror(res));
curl_easy_cleanup(easy);
return nullptr;
}
// Get the socket, we'll need it for waiting.
// NB: Do not use CURL_AT_LEAST_VERSION(x,y,z) or CURL_VERSION_BITS(x,y,z) for version check
// These were only added around v7.45.0 release so break build with earlier libcurl versions
#if LIBCURL_VERSION_NUM >= 0x072d00 // 7.45.0
// Use new CURLINFO_ACTIVESOCKET API for libcurl v7.45.0 or greater
curl_socket_t socket;
res = curl_easy_getinfo(easy, CURLINFO_ACTIVESOCKET, &socket);
if (res == CURLE_OK) {
return new CurlSocket(easy, socket);
}
#else
// Fallback on old deprecated CURLINFO_LASTSOCKET API for libcurl older than v7.45.0 (October 2015)
long socket;
res = curl_easy_getinfo(easy, CURLINFO_LASTSOCKET, &socket);
if (res == CURLE_OK) {
// curl_socket_t is an int or a SOCKET (Win32) which is a UINT_PTR
// A cast should be safe enough as long fits in it
return new CurlSocket(easy, (curl_socket_t)socket);
}
#endif
warning("libcurl: Failed to extract socket: %s", curl_easy_strerror(res));
curl_easy_cleanup(easy);
return nullptr;
}
CurlSocket::CurlSocket(CURL *easy, curl_socket_t socket) : _easy(easy), _socket(socket) {
}
CurlSocket::~CurlSocket() {
// Always clean up.
curl_easy_cleanup(_easy);
}
int CurlSocket::ready() {
return waitOnSocket(_socket, 1, 0);
}
size_t CurlSocket::send(const char *data, int len) {
size_t nsent_total = 0, left = len;
CURLcode res = CURLE_AGAIN;
// Keep looping until the whole thing is sent, errors,
// or times out.
while (((left > 0) && (len > 0))) {
size_t nsent = 0;
uint32 tickCount = g_system->getMillis() + 5000;
while (res == CURLE_AGAIN) {
res = curl_easy_send(_easy, data + nsent_total, left - nsent_total, &nsent);
if (g_system->getMillis() >= tickCount) {
warning("libcurl: Took too long attempting to send data to socket");
return nsent;
}
}
if (res == CURLE_OK) {
nsent_total += nsent;
left -= nsent;
} else if (res != CURLE_AGAIN) {
warning("libcurl: Error when sending to socket: %s", curl_easy_strerror(res));
return nsent_total;
}
}
return nsent_total;
}
size_t CurlSocket::recv(void *data, int maxLen) {
size_t nread = 0;
CURLcode res = CURLE_AGAIN;
uint32 tickCount = g_system->getMillis() + 5000;
while (res == CURLE_AGAIN) {
res = curl_easy_recv(_easy, data, maxLen, &nread);
if (g_system->getMillis() >= tickCount) {
warning("libcurl: Took too long attempting to read data from socket");
return nread;
}
}
if(res != CURLE_OK) {
warning("libcurl Error on receiving data: %s\n", curl_easy_strerror(res));
return nread;
}
debug(3, "libcurl: Received %llu bytes", (unsigned long long)nread);
return nread;
}
} // End of namespace Networking

View File

@@ -0,0 +1,56 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef BACKENDS_NETWORKING_BASIC_CURL_SOCKET_H
#define BACKENDS_NETWORKING_BASIC_CURL_SOCKET_H
typedef void CURL;
#ifdef WIN32
// Including winsock2.h will result in errors, we have to define
// SOCKET ourselves.
#include <basetsd.h>
typedef UINT_PTR SOCKET;
typedef SOCKET curl_socket_t;
#else
typedef int curl_socket_t;
#endif
#include "backends/networking/basic/socket.h"
namespace Networking {
class CurlSocket : public Socket {
public:
CurlSocket(CURL *easy, curl_socket_t socket);
~CurlSocket() override;
int ready() override;
size_t send(const char *data, int len) override;
size_t recv(void *data, int maxLen) override;
private:
CURL *_easy;
curl_socket_t _socket;
};
} // End of namespace Networking
#endif

View File

@@ -0,0 +1,104 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#define FORBIDDEN_SYMBOL_ALLOW_ALL
#include <curl/curl.h>
#include "backends/networking/basic/curl/url.h"
#include "common/debug.h"
#include "common/textconsole.h"
namespace Networking {
// This requires libcurl version 7.62.0, If we're using
// a lower version, stub all of this.
#if LIBCURL_VERSION_NUM < 0x073E00
URL *URL::parseURL(const Common::String &url) {
warning("libcurl: curl_url requires curl 7.62.0 or later");
return nullptr;
}
#else
URL *URL::parseURL(const Common::String &url) {
CURLU *curlu = curl_url();
if (!curlu) {
warning("libcurl: Could not create curl_url handle");
return nullptr;
}
CURLUcode rc = curl_url_set(curlu, CURLUPART_URL, url.c_str(), 0);
if (rc) {
warning("libcurl: Unable to parse URL: \"%s\"", url.c_str());
curl_url_cleanup(curlu);
return nullptr;
}
return new CurlURL(curlu);
}
CurlURL::CurlURL(CURLU *curlu) : _url(curlu) {
}
CurlURL::~CurlURL() {
curl_url_cleanup(_url);
}
Common::String CurlURL::getScheme() const {
char *scheme;
CURLUcode rc = curl_url_get(_url, CURLUPART_SCHEME, &scheme, 0);
if (rc) {
warning("libcurl: Unable to get scheme");
return "";
}
Common::String schemeString(scheme);
curl_free(scheme);
return schemeString;
}
Common::String CurlURL::getHost() const {
char *host;
CURLUcode rc = curl_url_get(_url, CURLUPART_HOST, &host, 0);
if (rc) {
warning("libcurl: Unable to get host");
return "";
}
Common::String hostString(host);
curl_free(host);
return hostString;
}
int CurlURL::getPort(bool defaultPort) const {
char *portChr;
CURLUcode rc = curl_url_get(_url, CURLUPART_PORT, &portChr, (defaultPort) ? CURLU_DEFAULT_PORT : CURLU_NO_DEFAULT_PORT);
if (rc) {
if (rc == CURLUE_NO_PORT)
return 0;
warning("libcurl: Unable to get port");
return -1;
}
int port = atoi(portChr);
curl_free(portChr);
return port;
}
#endif
} // End of namespace Networking

View File

@@ -0,0 +1,45 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef BACKENDS_NETWORKING_BASIC_CURL_URL_H
#define BACKENDS_NETWORKING_BASIC_CURL_URL_H
typedef struct Curl_URL CURLU;
#include "backends/networking/basic/url.h"
namespace Networking {
class CurlURL : public URL {
public:
CurlURL(CURLU *curlu);
~CurlURL() override;
Common::String getScheme() const override;
Common::String getHost() const override;
int getPort(bool returnDefault = false) const override;
private:
CURLU *_url;
};
} // End of Namespace Networking
#endif

View File

@@ -0,0 +1,42 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef BACKENDS_NETWORKING_BASIC_SOCKET_H
#define BACKENDS_NETWORKING_BASIC_SOCKET_H
#include "common/str.h"
namespace Networking {
class Socket {
public:
static Socket *connect(const Common::String &url);
virtual ~Socket() {}
virtual int ready() = 0;
virtual size_t send(const char *data, int len) = 0;
virtual size_t recv(void *data, int maxLen) = 0;
};
} // End of namespace Networking
#endif

View File

@@ -0,0 +1,67 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef BACKENDS_NETWORKING_BASIC_URL_H
#define BACKENDS_NETWORKING_BASIC_URL_H
#include "common/str.h"
namespace Networking {
class URL {
public:
/**
* Parses an URL string and creates a new URL object.
* @param url is a string containing the URL. e.g. "https://scummvm.org".
* @retval An URL object from the url provided
*/
static URL *parseURL(const Common::String &url);
virtual ~URL() {}
/**
* Extracts the scheme of an URL parsed previously by parseURL.
* @retval String of the URL's scheme. e.g. "https".
* @retval Empty string on failure.
*/
virtual Common::String getScheme() const = 0;
/**
* Extracts the host name of an URL parsed previously by parseURL.
* @retval String of the URL's host name. e.g. "scummvm.org".
* @retval Empty string on failure.
*/
virtual Common::String getHost() const = 0;
/**
* Extracts the port of an URL parsed previously by parseURL.
* @param returnDefault tells libcurl to return the default port according to the URL's scheme if not explicitly defined
* @retval The URL's port number if one exists.
* @retval 0 if no port found.
* @retval default port if returnDefault is true.
* @retval -1 on failure.
*/
virtual int getPort(bool returnDefault = false) const = 0;
};
} // End of Namespace Networking
#endif