aboutsummaryrefslogtreecommitdiffstats
path: root/src/launcherutil.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/launcherutil.c')
-rw-r--r--src/launcherutil.c77
1 files changed, 77 insertions, 0 deletions
diff --git a/src/launcherutil.c b/src/launcherutil.c
index 8df4a71..f73428a 100644
--- a/src/launcherutil.c
+++ b/src/launcherutil.c
@@ -1,6 +1,7 @@
#include "macros.h"
#include "l2su.h"
+#include <curl/easy.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
@@ -155,3 +156,79 @@ mdcleanup:
return 0;
}
+
+char *l2_launcher_parse_iso_time(const char *str, struct tm *ts)
+{
+ return strptime(str, "%FT%T%z", ts); /* TODO: replace with something portable */
+}
+
+void l2_launcher_download_init(struct l2_dlbuf *buf)
+{
+ buf->data = NULL;
+ buf->size = 0;
+ buf->capacity = 0;
+}
+
+size_t l2_launcher__download_callback(char *data, size_t size, size_t nmemb, void *user)
+{
+ struct l2_dlbuf *buf = user;
+ size_t realsz = size * nmemb;
+
+ if (buf->size + realsz > buf->capacity) {
+ size_t newcap = 16;
+ while (newcap && buf->size + realsz < newcap)
+ newcap <<= 1;
+
+ if (!newcap) return CURLE_WRITE_ERROR;
+
+ void *newbuf = realloc(buf->data, newcap);
+ if (!newbuf) return CURLE_WRITE_ERROR;
+
+ buf->data = newbuf;
+ buf->capacity = newcap;
+ }
+
+ memcpy((char *)buf->data + buf->size, data, realsz);
+ buf->size += realsz;
+ return realsz;
+}
+
+void *l2_launcher_download_finalize(struct l2_dlbuf *buf, size_t *psz)
+{
+ void *smaller = realloc(buf->data, buf->size);
+ if (smaller) buf->data = smaller;
+
+ void *data = buf->data;
+ buf->data = NULL;
+
+ *psz = buf->size;
+ return data;
+}
+
+void l2_launcher_download_cleanup(struct l2_dlbuf *buf)
+{
+ free(buf->data);
+}
+
+const curl_write_callback l2_dlcb = &l2_launcher__download_callback;
+
+int l2_launcher_download(CURL *cd, const char *url, void **odata, size_t *osize)
+{
+ struct l2_dlbuf db;
+
+ l2_launcher_download_init(&db);
+
+ curl_easy_setopt(cd, CURLOPT_URL, url);
+ curl_easy_setopt(cd, CURLOPT_WRITEDATA, &db);
+ curl_easy_setopt(cd, CURLOPT_WRITEFUNCTION, l2_dlcb);
+
+ if (curl_easy_perform(cd) != CURLE_OK) {
+ l2_launcher_download_cleanup(&db);
+ return -1;
+ }
+
+ *odata = l2_launcher_download_finalize(&db, osize);
+ l2_launcher_download_cleanup(&db); /* not strictly necessary but ok */
+
+ return 0;
+}