feat: minimal redirection functionality.

This commit is contained in:
NunoSempere 2022-12-13 20:04:18 +00:00
parent 9ca14fb3d4
commit 304b4b274c
7 changed files with 155 additions and 0 deletions

View File

@ -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

BIN
plugins/libre_redirect/example Executable file

Binary file not shown.

View File

@ -0,0 +1,25 @@
#include "libre_redirect.h"
#include <string.h>
#include <stdio.h>
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<l; i++){
uri_filtered[i] = ' ';
}
uri_filtered[l] = '\0';
int libre_check = libre_redirect(uri, uri_filtered);
if(!libre_check){
printf("Filtered url: %s", uri_filtered);
}else{
printf("Uri: %s", uri);
// failure; do something with the original uri.
}
}

View File

@ -0,0 +1,47 @@
#include "str_replace_start.h"
#include <string.h>
#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;
}

View File

@ -0,0 +1,8 @@
#ifndef LIBRE_REDIRECT
#define LIBRE_REDIRECT
#define LIBRE_N 12
int libre_redirect(const char* uri, char* uri_filtered);
#endif

View File

@ -0,0 +1,58 @@
#include <string.h>
#include <stdbool.h>
#include <stdio.h>
#define DEBUG false
/*
See also:
* <https://web.archive.org/web/20160201212501/coding.debuntu.org/c-implementing-str_replace-replace-all-occurrences-substring>
* 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<l2; i++){
if(string[i] != target[i]){
match = false;
break;
}
}
if(match){
if(DEBUG) printf("Case 3.a.\n");
for(int i=0; i<l3; i++){
output[i] = replacement[i];
}
int counter = l3;
for(int i=l2; i<l1; i++){
output[counter] = string[i];
counter++;
}
output[counter] = '\0';
}
else {
if(DEBUG) printf("Case 3.b.\n");
strcpy(output, string);
}
}
return 0;
}

View File

@ -0,0 +1,6 @@
#ifndef STR_REPLACE_H_
#define STR_REPLACE_H_
int str_replace_start(const char* string, const char* target, const char* replacement, char* output);
#endif