diff --git a/plugins/libre_redirect/build-example.sh b/plugins/libre_redirect/build-example.sh new file mode 100644 index 0000000..ac7cf4a --- /dev/null +++ b/plugins/libre_redirect/build-example.sh @@ -0,0 +1,11 @@ +#!/bin/bash +CC=gcc + +FLAGS="-std=c99 -Wall -lm" + +SRC=example.c +REQS="str_replace_start.c libre_redirect.c" + +echo -e "\n\n\n" +$CC $FLAGS $SRC $REQS -o example + diff --git a/plugins/libre_redirect/example b/plugins/libre_redirect/example new file mode 100755 index 0000000..d46dfae Binary files /dev/null and b/plugins/libre_redirect/example differ diff --git a/plugins/libre_redirect/example.c b/plugins/libre_redirect/example.c new file mode 100644 index 0000000..b21cad1 --- /dev/null +++ b/plugins/libre_redirect/example.c @@ -0,0 +1,25 @@ +#include "libre_redirect.h" +#include +#include + +int main(){ + char uri[] = "https://reddit.com/r/blah"; + + int uri_length = strlen(uri); + int l = LIBRE_N+uri_length; + char uri_filtered[l++]; + for(int i=0; i +#define LIBRE_N 12 + +/* Inspired by https://libredirect.github.io/, but in C. */ + +// int str_replace_start(const char* string, const char* target, const char* replacement, char* output){ + +int libre_redirect(const char* uri, char* output){ + /* inv.riverside.rocks */ + // max length + int l1 = strlen(uri); + int l2 = strlen(output); + + if((l2 - l1) < LIBRE_N){ + return 1; // not enough memory. + }else{ + char tmp_uri[l2++]; + strcpy(tmp_uri, uri); + + char* sites[] = { + "https://youtube.com", + "https://reddit.com", + "https://medium.com", + "https://translate.google.com" + }; + char* alternatives[] = { + "https://yt.artemislena.eu", + "https://teddit.nunosempere.com", + "https://scribe.rip", + "https://simplytranslate.org/" + }; + + for(int i=1; i<4; i++){ + int replace_check = str_replace_start(tmp_uri, sites[i], alternatives[i], output); + if(replace_check){ + return 1; + } + strcpy(tmp_uri, output); + } + strcpy(output, tmp_uri); + + } + + return 0; + +} diff --git a/plugins/libre_redirect/libre_redirect.h b/plugins/libre_redirect/libre_redirect.h new file mode 100755 index 0000000..ef5cf77 --- /dev/null +++ b/plugins/libre_redirect/libre_redirect.h @@ -0,0 +1,8 @@ +#ifndef LIBRE_REDIRECT +#define LIBRE_REDIRECT + +#define LIBRE_N 12 + +int libre_redirect(const char* uri, char* uri_filtered); + +#endif diff --git a/plugins/libre_redirect/str_replace_start.c b/plugins/libre_redirect/str_replace_start.c new file mode 100755 index 0000000..70f9567 --- /dev/null +++ b/plugins/libre_redirect/str_replace_start.c @@ -0,0 +1,58 @@ +#include +#include +#include +#define DEBUG false + +/* +See also: +* +* https://github.com/irl/la-cucina/blob/master/str_replace.c +*/ + +int str_replace_start(const char* string, const char* target, const char* replacement, char* output){ + /* Checks */ + int l1 = strlen(string); + int l2 = strlen(target); + int l3 = strlen(replacement); + int l4 = strlen(output); + if(DEBUG) printf("%d,%d,%d,%d\n", l1, l2, l3, l4); + + if((l4 < (l1 - l2 + l3)) || l4 < l1 ){ + // Not enough memory in output string. + if(DEBUG) printf("Case 1.\n"); + return 1; + } + else if(l1 < l2){ + // Not even possible that there is a match. + if(DEBUG) printf("Case 2.\n"); + strcpy(output, string); + } + else { + if(DEBUG) printf("Case 3.\n"); + int match = true; + for(int i=0; i