summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/extractors/bilibili.c16
-rw-r--r--src/extractors/extractor.c9
-rw-r--r--src/extractors/haokan.c4
-rw-r--r--src/process_url.c54
-rw-r--r--src/process_url.h2
-rw-r--r--src/utils/types.c6
-rw-r--r--src/utils/utils.c8
-rw-r--r--src/utils/utils.h3
8 files changed, 53 insertions, 49 deletions
diff --git a/src/extractors/bilibili.c b/src/extractors/bilibili.c
index b088e95..89ae1fc 100644
--- a/src/extractors/bilibili.c
+++ b/src/extractors/bilibili.c
@@ -365,9 +365,9 @@ static int bilibili_merge(void *v) {
callback_struct_t *cb_struct = (callback_struct_t *)v;
LOG("Bilibili", "Using ffmpeg to merge downloaded files ...\n");
int r = merge_av(cb_struct->videofn, cb_struct->audiofn, cb_struct->filename);
- free_and_nullify(cb_struct->videofn);
- free_and_nullify(cb_struct->audiofn);
- free_and_nullify(cb_struct->filename);
+ free_and_nullify((void **)&cb_struct->videofn);
+ free_and_nullify((void **)&cb_struct->audiofn);
+ free_and_nullify((void **)&cb_struct->filename);
LOG("Bilibili", "All done!\n");
return r;
}
@@ -378,7 +378,7 @@ static int download(Bilibili_options *bilibili_options) {
get(bilibili_options->api, &resp);
if (get_dash(resp, &dash)) {
LOG("Bilibili", "Get dash failed.");
- free_and_nullify(resp);
+ free_and_nullify((void **)&resp);
dash_cleanup(&dash);
return 1;
};
@@ -427,7 +427,7 @@ static int download(Bilibili_options *bilibili_options) {
add_url(audio->baseUrl, NULL, callback_struct.audiofn, &bilibili_merge,
&callback_struct);
}
- free_and_nullify(resp);
+ free_and_nullify((void **)&resp);
dash_cleanup(&dash);
return 0;
}
@@ -475,18 +475,18 @@ void bilibili_extract(struct options *options) {
DEBUG_PRINT("title: %s\n", bilibili_options.title);
if (generate_api(&bilibili_options, 127)) {
- free_and_nullify(bilibili_options.api);
+ free_and_nullify((void **)&bilibili_options.api);
multipage_cleanup(&multipage_struct);
return;
}
DEBUG_PRINT("Generated API: %s\n", bilibili_options.api);
if (download(&bilibili_options)) {
- free_and_nullify(bilibili_options.api);
+ free_and_nullify((void **)&bilibili_options.api);
multipage_cleanup(&multipage_struct);
return;
}
- free_and_nullify(bilibili_options.api);
+ free_and_nullify((void **)&bilibili_options.api);
multipage_cleanup(&multipage_struct);
}
diff --git a/src/extractors/extractor.c b/src/extractors/extractor.c
index ccc1ec6..4bfc7cb 100644
--- a/src/extractors/extractor.c
+++ b/src/extractors/extractor.c
@@ -4,16 +4,17 @@
#include "bilibili.h"
#include "extractor.h"
#include "haokan.h"
+#include "youku.h"
Site_map site_map = {
{{"www.bilibili.com", SITE_BILIBILI}, {"haokan.baidu.com", SITE_HAOKAN}},
2};
void options_cleanup(Options *options) {
- free_and_nullify(options->URL);
- free_and_nullify(options->path);
- free_and_nullify(options->query);
- free_and_nullify(options->pagedata);
+ free_and_nullify((void **)&options->URL);
+ free_and_nullify((void **)&options->path);
+ free_and_nullify((void **)&options->query);
+ free_and_nullify((void **)&options->pagedata);
}
int extract(void *v) {
diff --git a/src/extractors/haokan.c b/src/extractors/haokan.c
index f289c3e..e2a8aef 100644
--- a/src/extractors/haokan.c
+++ b/src/extractors/haokan.c
@@ -38,8 +38,8 @@ void haokan_extract(Options *options) {
end:
free_str_array(&results);
- free_and_nullify(filename);
- free_and_nullify(videoURL);
+ free_and_nullify((void **)&filename);
+ free_and_nullify((void **)&videoURL);
return;
}
free_str_array(&results);
diff --git a/src/process_url.c b/src/process_url.c
index 8d64cda..328fc4f 100644
--- a/src/process_url.c
+++ b/src/process_url.c
@@ -145,7 +145,7 @@ static void gen_fullpathfn(char *fullpathfn, const char *outdir,
outdir[strlen(outdir) - 1] == SPLITTER_CHAR ? "" : SPLITTER_STR, fn);
}
-static int parse_url(const char *URL, const char *outdir, char *fn) {
+static int parse_url(const char *URL, const char *outdir, char **p_fn) {
CURLUcode ue = logerr(curl_url_set(h, CURLUPART_URL, URL, 0));
if (ue && ue != CURLUE_NO_QUERY) {
return 1;
@@ -204,7 +204,7 @@ static int parse_url(const char *URL, const char *outdir, char *fn) {
/* filename */
- if (fn == NULL) {
+ if (p_fn == NULL) {
const char *patterns_str[1] = {"(?:.+\\/)([^#/?]+)"};
str_array_t results = create_str_array(0);
const str_array_t patterns = {(char **)patterns_str, 1};
@@ -228,9 +228,9 @@ static int parse_url(const char *URL, const char *outdir, char *fn) {
return 1;
}
} else {
- curl_c->outfn = malloc(strlen(outdir) + strlen(fn) + 2);
- gen_fullpathfn(curl_c->outfn, outdir, fn);
- free_and_nullify(fn);
+ curl_c->outfn = malloc(strlen(outdir) + strlen(*p_fn) + 2);
+ gen_fullpathfn(curl_c->outfn, outdir, *p_fn);
+ free_and_nullify((void **)p_fn);
}
DEBUG_PRINT("File will be saved as: %s\n", curl_c->outfn);
DEBUG_PRINT("Got regular URL: %s\n", curl_c->URL);
@@ -275,7 +275,7 @@ bool get_info(const char *URL, long *psize, char **p_content_type,
goto end;
}
DEBUG_PRINT("Set-Cookie: %s\n", pch->value);
- if (p_cookie){
+ if (p_cookie) {
*p_cookie = malloc(strlen(pch->value) + 1);
strcpy(*p_cookie, pch->value);
}
@@ -348,7 +348,8 @@ static int pull_part(void *a) {
return (int)res;
}
-static int merge_and_cleanup(curl_conf_t *curl_c) {
+static int merge_and_cleanup(curl_conf_t **p_curl_c) {
+ curl_conf_t *curl_c = *p_curl_c;
if (corrupted) {
append_log("Cancelling...\n");
} else {
@@ -389,8 +390,9 @@ static int merge_and_cleanup(curl_conf_t *curl_c) {
corrupted = false;
curl_c->success_thrd = 0;
curl_c->total_thrd = 0;
- free_and_nullify(curl_c->URL);
- free_and_nullify(curl_c->outfn);
+ free_and_nullify((void **)&curl_c->URL);
+ free_and_nullify((void **)&curl_c->outfn);
+ free_and_nullify((void **)p_curl_c);
return 0;
}
@@ -455,19 +457,20 @@ static void replace_illegal_char(char *str) {
}
}
-static char *callback_struct_convert_fullpath(char *filename) {
- char *tmp = malloc(strlen(outdir_g) + strlen(filename) + 2);
- replace_illegal_char(filename);
- gen_fullpathfn(tmp, outdir_g, filename);
- free_and_nullify(filename);
+static char *callback_struct_convert_fullpath(char **p_filename) {
+ char *tmp = malloc(strlen(outdir_g) + strlen(*p_filename) + 2);
+ replace_illegal_char(*p_filename);
+ gen_fullpathfn(tmp, outdir_g, *p_filename);
+ free_and_nullify((void **)p_filename);
return tmp;
}
-void add_cookie(char *cookie) {
+void add_cookie(char **p_cookie) {
+ char *cookie = *p_cookie;
if (cookie_g) {
char *tmp = malloc(strlen(cookie_g) + strlen(cookie) + 3);
sprintf(tmp, "%s; %s", cookie_g, cookie);
- free_and_nullify(cookie_g);
+ free_and_nullify((void **)&cookie_g);
cookie_g = tmp;
} else {
cookie_g = cookie;
@@ -479,7 +482,7 @@ void set_referer(char *referer) { referer_g = referer; }
void curl_init(char *cookie) {
curl_global_init(CURL_GLOBAL_ALL);
h = curl_url();
- add_cookie(cookie);
+ add_cookie(&cookie);
dl_queue = create_queue();
mtx_init(&mtx, mtx_plain);
cnd_init(&cnd);
@@ -498,13 +501,13 @@ void curl_cleanup(status_t *stat) {
}
mtx_unlock(&mtx);
if (!stat->is_done) {
- merge_and_cleanup(curl_conf);
+ merge_and_cleanup(&curl_conf);
}
mtx_destroy(&mtx);
cnd_destroy(&cnd);
}
free_queue(&dl_queue);
- free_and_nullify(cookie_g);
+ free_and_nullify((void **)&cookie_g);
curl_url_cleanup(h);
curl_global_cleanup();
}
@@ -538,8 +541,7 @@ void poll_status(status_t *stat) {
int r;
thrd_join(tid[i], &r);
}
- merge_and_cleanup(curl_conf);
- free_and_nullify(curl_conf);
+ merge_and_cleanup(&curl_conf);
// Perform the callback
if (is_empty_queue(&dl_queue) && callback_g) {
thrd_t cb_thrd;
@@ -563,7 +565,7 @@ int get(const char *URL, char **pdstr) {
CURLcode res = logerr(curl_easy_perform(curl));
*pdstr = malloc(pagedata.len + 1);
strcpy(*pdstr, pagedata.string);
- free_and_nullify(pagedata.string);
+ free_and_nullify((void **)&pagedata.string);
curl_easy_cleanup(curl);
return res;
}
@@ -594,16 +596,16 @@ void add_url(const char *URL, const char *outdir, const char *fn,
callback_g = callback;
if (p_callback_struct) {
p_callback_struct->videofn =
- callback_struct_convert_fullpath(p_callback_struct->videofn);
+ callback_struct_convert_fullpath(&p_callback_struct->videofn);
p_callback_struct->audiofn =
- callback_struct_convert_fullpath(p_callback_struct->audiofn);
+ callback_struct_convert_fullpath(&p_callback_struct->audiofn);
p_callback_struct->filename =
- callback_struct_convert_fullpath(p_callback_struct->filename);
+ callback_struct_convert_fullpath(&p_callback_struct->filename);
p_callback_struct_g = p_callback_struct;
}
// Pass our cache (outdir_g) to parse_url()
- if (parse_url(URL, outdir_g, filename)) {
+ if (parse_url(URL, outdir_g, &filename)) {
DEBUG_PRINT("parse_url() failed with error.\n");
return; // Parse failed, quit the task directly
};
diff --git a/src/process_url.h b/src/process_url.h
index 005cfcf..e4b513d 100644
--- a/src/process_url.h
+++ b/src/process_url.h
@@ -58,7 +58,7 @@ void curl_cleanup(status_t *);
void poll_status(status_t *);
-void add_cookie(char *cookie);
+void add_cookie(char **p_cookie);
void set_referer(char *referer);
diff --git a/src/utils/types.c b/src/utils/types.c
index 390d34c..f986ba3 100644
--- a/src/utils/types.c
+++ b/src/utils/types.c
@@ -15,7 +15,7 @@ generic_array_t create_array(size_t elem_size, size_t n) {
}
void free_array(generic_array_t *array) {
- free_and_nullify(array->data);
+ free_and_nullify((void **)&array->data);
array->n = 0;
}
@@ -53,7 +53,7 @@ void free_str_array(str_array_t *array) {
for (size_t i = 0; i < array->n; i++) {
free(array->str[i]);
}
- free_and_nullify(array->str);
+ free_and_nullify((void **)&array->str);
array->n = 0;
}
@@ -115,7 +115,7 @@ data_t dequeue(queue_t *queue) {
node_t *temp = queue->front;
data_t data = temp->data;
queue->front = temp->next;
- free_and_nullify(temp);
+ free_and_nullify((void **)&temp);
if (queue->front == NULL) {
queue->rear = NULL;
diff --git a/src/utils/utils.c b/src/utils/utils.c
index ae60eee..f7de635 100644
--- a/src/utils/utils.c
+++ b/src/utils/utils.c
@@ -170,9 +170,9 @@ int repchr(char *str, char t, char r) {
return c;
}
-void free_and_nullify(void *p) {
- if (p) {
- free(p);
- p = NULL;
+void free_and_nullify(void **p) {
+ if (*p) {
+ free(*p);
+ *p = NULL;
}
}
diff --git a/src/utils/utils.h b/src/utils/utils.h
index f6bbad5..a96ab70 100644
--- a/src/utils/utils.h
+++ b/src/utils/utils.h
@@ -13,6 +13,7 @@ const char *mimeType2ext(const char *mimeType);
int repchr(char *str, char t, char r);
-void free_and_nullify(void *p);
+/* NOTICE: pass a pointer-to-pointer to free the original pointer. */
+void free_and_nullify(void **p);
#endif