From 304b4b274c69251dce75ab50e7abb6372c2a3772 Mon Sep 17 00:00:00 2001 From: NunoSempere Date: Tue, 13 Dec 2022 20:04:18 +0000 Subject: [PATCH] feat: minimal redirection functionality. --- plugins/libre_redirect/build-example.sh | 11 ++++ plugins/libre_redirect/example | Bin 0 -> 17008 bytes plugins/libre_redirect/example.c | 25 +++++++++ plugins/libre_redirect/libre_redirect.c | 47 +++++++++++++++++ plugins/libre_redirect/libre_redirect.h | 8 +++ plugins/libre_redirect/str_replace_start.c | 58 +++++++++++++++++++++ plugins/libre_redirect/str_replace_start.h | 6 +++ 7 files changed, 155 insertions(+) create mode 100644 plugins/libre_redirect/build-example.sh create mode 100755 plugins/libre_redirect/example create mode 100644 plugins/libre_redirect/example.c create mode 100644 plugins/libre_redirect/libre_redirect.c create mode 100755 plugins/libre_redirect/libre_redirect.h create mode 100755 plugins/libre_redirect/str_replace_start.c create mode 100755 plugins/libre_redirect/str_replace_start.h 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 0000000000000000000000000000000000000000..d46dfae22428a6a3c376a3b9935e8da1f5356b2e GIT binary patch literal 17008 zcmeHOZEzGvdhXRC5H_p?hMbJgX3sVkb3}{8VJx7qcLf6@m#`I(&QAHb)uLUYE$@f2 zJDZcPz$b2zOIwTlQ7I?-ksr!eg;Xjj<;$gW;)*b5q_`3nqf(P-7M}AE5Wajm?igH1X3%#T~$hJ6z&EkyL!r$pYTNoD7x2b4ZjDudF|4~+2tq`Y=zSMW+V z!bv+USTN=CejRqIYn8^W33u}mRo-&_(52$fRE1Y;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8dgxCrM1Bboe)yJHN+usyG1nR+3!C*F@RwhI}%{gY~4%aq;^*%RMq8EZwxZqMstre0Q5)M@_& zaJ;@sa{YN<$CS(GeVw!ZA=uNNj_rA!&2&zcOLe3;vpw&>qQI!hY|ra)ro3;2icI9g zo_-Sy?aA1l*X>Nz<4m2ETr7w6pMgE?@tEiJJ<~>2n6v&|hY@5brfkpq0H%{l(HTE( z|L-dM7Ny7gSf*Vzd$MuH@ArXGOxQmjnpRTOxdL|f|L-~My~>VhyK=|G;a>cS!=Cpk zOj&<9`*DZ;pfX_UXG2BVAJgwU>?f6g=@#lZTVgwg=*wk${CI!FbS*0?>Wts(%AWgQ zc$K1}A1am!_aF2Bj0}~}_64scVcKD_#b0OnKZe9^&-*f_y(F|HXZasMMtdCIPV>5) z&qq7;nP>VbY-?@yyw5w=01%1wEN+o!{BsCs>~i_MALR4ye6F3lk?pu2{|vU&M7HOB zAEPGm~%2(<|5!-Jo zB36d&?UcukUDdA~PO2Nj-4GJQ_2K)^YG7Pmf_{>U)*hJY;qVl GSn=P=g2EvH literal 0 HcmV?d00001 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