aboutsummaryrefslogtreecommitdiff
path: root/plugins/libre_redirect
diff options
context:
space:
mode:
authorfenze <contact@fenze.dev>2022-12-18 16:54:07 +0000
committerfenze <contact@fenze.dev>2022-12-18 16:54:07 +0000
commit584a5d68a833c6fa58dd165ebac83cf4de1e4c0d (patch)
treec102ba69187568fddf874a1c88805489c68e887e /plugins/libre_redirect
parentd97045bf47c06c91f4afb79f09660353cc8ad9ad (diff)
Fixed formatting in some libre_redirect files
Diffstat (limited to 'plugins/libre_redirect')
-rw-r--r--plugins/libre_redirect/libre_redirect.c133
-rw-r--r--[-rwxr-xr-x]plugins/libre_redirect/libre_redirect.h5
-rw-r--r--[-rwxr-xr-x]plugins/libre_redirect/str_replace_start.c13
-rw-r--r--[-rwxr-xr-x]plugins/libre_redirect/str_replace_start.h8
-rwxr-xr-xplugins/libre_redirect/str_replace_test/examplebin17040 -> 0 bytes
-rw-r--r--plugins/libre_redirect/str_replace_test/example.c24
6 files changed, 96 insertions, 87 deletions
diff --git a/plugins/libre_redirect/libre_redirect.c b/plugins/libre_redirect/libre_redirect.c
index b9da2b1..8650326 100644
--- a/plugins/libre_redirect/libre_redirect.c
+++ b/plugins/libre_redirect/libre_redirect.c
@@ -1,69 +1,84 @@
-#include "str_replace_start.h"
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
+
+#include "str_replace_start.h"
+
#define LIBRE_N 19
-#define DEBUG false
+
+/* Uncomment for debug */
+/* #define DEBUG */
/* Inspired by https://libredirect.github.io/, but in C. */
-void str_init(char* str, int n){
- for(int i=0; i<n; i++){
- str[i] = ' ';
- }
- str[n] = '\0';
+void str_init(char* str, int n)
+{
+ for(int i = 0; i < n; i++)
+ str[i] = ' ';
+ str[n] = '\0';
} // could also use <https://manpages.ubuntu.com/manpages/impish/man3/strinit.3pub.html>
-int libre_redirect(const char* uri, char* output){
- int l1 = strlen(uri);
- int l2 = strlen(output);
-
- if((l2 - l1) < LIBRE_N){
- if(DEBUG) printf("Not enough memory\n");
- return 1; // not enough memory.
- }else{
- char tmp_uri[l2++];
- char tmp_output[l2++];
- strcpy(tmp_uri, uri); // strcpy also copies the terminating '\0'
- strcpy(tmp_output, output);
-
- char* annoying_sites[] = {
- "https://www.youtube.com",
- "https://www.reddit.com",
- "https://medium.com",
- "https://translate.google.com",
- "https://forum.effectivealtruism.org",
- "https://www.bloomberg.com",
- "https://twitter.com"
- };
- char* alternatives[] = {
- "https://yt.artemislena.eu",
- "https://teddit.nunosempere.com",
- "https://scribe.rip",
- "https://simplytranslate.org/",
- "https://ea.greaterwrong.com",
- "https://archive.is/https://www.bloomberg.com",
- "https://nitter.net"
- };
- int n = sizeof(annoying_sites)/sizeof(annoying_sites[0]);
- for(int i=0; i<n ; i++){
- int replace_check = str_replace_start(tmp_uri, annoying_sites[i], alternatives[i], output);
- if(replace_check == 2){
- if(DEBUG) printf("tmp_uri: %s\n", tmp_uri);
- if(DEBUG) printf("output: %s\n", output);
- // strcpy(output, tmp_uri);
- // break;
- return 2;
- }else if(replace_check == 1){
- if(DEBUG) printf("replace_check failed\n");
- return 1;
- }
- strcpy(tmp_uri, output);
- str_init(output, l2);
- }
- strcpy(output, tmp_uri);
- }
- if(DEBUG) printf("No match found\n\n");
- return 0;
-
+int libre_redirect(const char* uri, char* output)
+{
+ int l1 = strlen(uri);
+ int l2 = strlen(output);
+ int len;
+ char tmp_uri[l2++];
+ char tmp_output[l2++];
+
+ if ((l2 - l1) < LIBRE_N) {
+#ifdef DEBUG
+ printf("Not enough memory\n");
+#endif
+ return 1; // not enough memory.
+ } else {
+ strcpy(tmp_uri, uri); // strcpy also copies the terminating '\0'
+ strcpy(tmp_output, output);
+
+ char* annoying_sites[] = {
+ "https://www.youtube.com",
+ "https://www.reddit.com",
+ "https://medium.com",
+ "https://translate.google.com",
+ "https://forum.effectivealtruism.org",
+ "https://www.bloomberg.com",
+ "https://twitter.com"
+ };
+
+ char* alternatives[] = {
+ "https://yt.artemislena.eu",
+ "https://teddit.nunosempere.com",
+ "https://scribe.rip",
+ "https://simplytranslate.org/",
+ "https://ea.greaterwrong.com",
+ "https://archive.is/https://www.bloomberg.com",
+ "https://nitter.net"
+ };
+
+ len = sizeof(annoying_sites) / sizeof(annoying_sites[0]);
+
+ for (int i = 0; i < len; i++) {
+ int replace_check = str_replace_start(tmp_uri, annoying_sites[i],
+ alternatives[i], output);
+ if (replace_check == 2) {
+#ifdef DEBUG
+ printf("tmp_uri: %s\n", tmp_uri);
+ printf("output: %s\n", output);
+#endif
+ return 2;
+ } else if (replace_check == 1) {
+#ifdef DEBUG
+ printf("replace_check failed\n");
+#endif
+ return 1;
+ }
+ strcpy(tmp_uri, output);
+ str_init(output, l2);
+ }
+ strcpy(output, tmp_uri);
+ }
+#ifdef DEBUG
+ printf("No match found\n\n");
+#endif
+ return 0;
}
diff --git a/plugins/libre_redirect/libre_redirect.h b/plugins/libre_redirect/libre_redirect.h
index aee42f4..150c95e 100755..100644
--- a/plugins/libre_redirect/libre_redirect.h
+++ b/plugins/libre_redirect/libre_redirect.h
@@ -1,9 +1,6 @@
-#ifndef LIBRE_REDIRECT
-#define LIBRE_REDIRECT
+#pragma once
#define LIBRE_N 19
int libre_redirect(const char* uri, char* uri_filtered);
void str_init(char* str, int n);
-
-#endif
diff --git a/plugins/libre_redirect/str_replace_start.c b/plugins/libre_redirect/str_replace_start.c
index 74bff65..6bdfc69 100755..100644
--- a/plugins/libre_redirect/str_replace_start.c
+++ b/plugins/libre_redirect/str_replace_start.c
@@ -1,28 +1,29 @@
#include <string.h>
#include <stdbool.h>
#include <stdio.h>
+
#define DEBUG false
/*
-See also:
+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){
-
+int str_replace_start(const char* string, const char* target, const char* replacement, char* output)
+{
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(DEBUG) printf("%s,%s,%s,%s\n", string, target, replacement, output);
-
+
if((l4 < (l1 - l2 + l3)) || l4 < l1 ){
// Not enough memory in output string.
if(DEBUG) printf("String not long enough.\n");
- return 1;
- }
+ return 1;
+ }
/* else if(l1 < l2){
// Not even possible that there is a match.
if(DEBUG) printf("Target larger than string.\n");
diff --git a/plugins/libre_redirect/str_replace_start.h b/plugins/libre_redirect/str_replace_start.h
index d9f1235..78c79b5 100755..100644
--- a/plugins/libre_redirect/str_replace_start.h
+++ b/plugins/libre_redirect/str_replace_start.h
@@ -1,6 +1,4 @@
-#ifndef STR_REPLACE_H_
-#define STR_REPLACE_H_
+#pragma once
-int str_replace_start(const char* string, const char* target, const char* replacement, char* output);
-
-#endif
+int str_replace_start(const char* string, const char* target,
+ const char* replacement, char* output);
diff --git a/plugins/libre_redirect/str_replace_test/example b/plugins/libre_redirect/str_replace_test/example
deleted file mode 100755
index 4e80ab8..0000000
--- a/plugins/libre_redirect/str_replace_test/example
+++ /dev/null
Binary files differ
diff --git a/plugins/libre_redirect/str_replace_test/example.c b/plugins/libre_redirect/str_replace_test/example.c
index 32285bd..303665e 100644
--- a/plugins/libre_redirect/str_replace_test/example.c
+++ b/plugins/libre_redirect/str_replace_test/example.c
@@ -3,18 +3,16 @@
#include <stdio.h>
int main(){
- char uri[] = "https://reddit.com/r/blah";
-
- int l = LIBRE_N + strlen(uri) + 1;
- char uri_filtered[l];
- str_init(uri_filtered, l);
-
- if(!libre_redirect(uri, uri_filtered)){
- printf("Filtered uri: %s\n", uri_filtered);
- }else{
- printf("Uri: %s\n", uri);
- // failure; do something with the original uri.
- }
-}
+ char uri[] = "https://reddit.com/r/blah";
+ int l = LIBRE_N + strlen(uri) + 1;
+ char uri_filtered[l];
+ str_init(uri_filtered, l);
+ if (!libre_redirect(uri, uri_filtered)) {
+ printf("Filtered uri: %s\n", uri_filtered);
+ } else {
+ printf("Uri: %s\n", uri);
+ // failure; do something with the original uri.
+ }
+}