From f3aa7ca0bc2ef7c286609e8f87d07cc2568093af Mon Sep 17 00:00:00 2001 From: frosty Date: Tue, 6 Jan 2026 23:46:24 -0500 Subject: rebase(d) --- src/Utility/Display.c | 46 +++++++++++++++++++++++++++++ src/Utility/Display.h | 6 ++++ src/Utility/Unescape.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/Utility/Unescape.h | 10 +++++++ src/Utility/Utility.c | 8 +++++ src/Utility/Utility.h | 6 ++++ 6 files changed, 156 insertions(+) create mode 100644 src/Utility/Display.c create mode 100644 src/Utility/Display.h create mode 100644 src/Utility/Unescape.c create mode 100644 src/Utility/Unescape.h create mode 100644 src/Utility/Utility.c create mode 100644 src/Utility/Utility.h (limited to 'src/Utility') diff --git a/src/Utility/Display.c b/src/Utility/Display.c new file mode 100644 index 0000000..492e998 --- /dev/null +++ b/src/Utility/Display.c @@ -0,0 +1,46 @@ +#include "Display.h" +#include +#include +#include +#include + +char *pretty_display_url(const char *input) { + if (!input) return NULL; + + const char *start = input; + + const char *protocol_pos = strstr(input, "://"); + if (protocol_pos) { + start = protocol_pos + 3; + } + + if (strncasecmp(start, "www.", 4) == 0) { + start += 4; + } + + size_t input_len = strlen(start); + char temp[512]; + strncpy(temp, start, sizeof(temp) - 1); + temp[sizeof(temp) - 1] = '\0'; + + if (input_len > 0 && temp[input_len - 1] == '/') { + temp[input_len - 1] = '\0'; + } + + char *output = (char *)malloc(strlen(temp) * 3 + 1); + if (!output) return NULL; + + size_t j = 0; + for (size_t i = 0; temp[i] != '\0'; i++) { + if (temp[i] == '/') { + output[j++] = ' '; + output[j++] = '>'; + output[j++] = ' '; + } else { + output[j++] = (char)tolower((unsigned char)temp[i]); + } + } + output[j] = '\0'; + + return output; +} diff --git a/src/Utility/Display.h b/src/Utility/Display.h new file mode 100644 index 0000000..bbaf421 --- /dev/null +++ b/src/Utility/Display.h @@ -0,0 +1,6 @@ +#ifndef DISPLAY_H +#define DISPLAY_H + +char *pretty_display_url(const char *input); + +#endif diff --git a/src/Utility/Unescape.c b/src/Utility/Unescape.c new file mode 100644 index 0000000..e2968b2 --- /dev/null +++ b/src/Utility/Unescape.c @@ -0,0 +1,80 @@ +#include "Unescape.h" +#include "Utility.h" +#include +#include + +char *unescape_search_url(const char *input) { + if (!input) return NULL; + + const char *key = NULL; + const char *start = NULL; + const char *end = NULL; + size_t len = 0; + + if (strstr(input, "uddg=")) { + key = "uddg="; + start = strstr(input, key); + if (!start) return NULL; + start += strlen(key); + end = strchr(start, '&'); + len = end ? (size_t)(end - start) : strlen(start); + } + + else if (strstr(input, "RU=")) { + key = "RU="; + start = strstr(input, key); + if (!start) return strdup(input); + start += strlen(key); + end = strchr(start, '/'); + len = end ? (size_t)(end - start) : strlen(start); + } + + else { + return strdup(input); + } + + char *output = (char *)malloc(len * 3 + 1); + if (!output) return NULL; + + size_t i = 0, j = 0; + while (i < len) { + if (start[i] == '%' && i + 2 < len) { + int high = hex_to_int(start[i + 1]); + int low = hex_to_int(start[i + 2]); + if (high != -1 && low != -1) { + output[j++] = (char)((high << 4) | low); + i += 3; + } else { + output[j++] = start[i++]; + } + } else if (start[i] == '+') { + output[j++] = ' '; + i++; + } else { + output[j++] = start[i++]; + } + } + output[j] = '\0'; + + return output; +} + +char *url_decode_query(const char *src) { + if (!src) return NULL; + char *res = strdup(src); + char *p = res; + while (*src) { + if (*src == '+') { + *p++ = ' '; + } else if (*src == '%' && src[1] && src[2]) { + char hex[3] = {src[1], src[2], '\0'}; + *p++ = (char)strtol(hex, NULL, 16); + src += 2; + } else { + *p++ = *src; + } + src++; + } + *p = '\0'; + return res; +} diff --git a/src/Utility/Unescape.h b/src/Utility/Unescape.h new file mode 100644 index 0000000..0adb228 --- /dev/null +++ b/src/Utility/Unescape.h @@ -0,0 +1,10 @@ +#ifndef UNESCAPE_H +#define UNESCAPE_H + +#include + +char *unescape_search_url(const char *input); +char *url_decode_query(const char *src); + +#endif + diff --git a/src/Utility/Utility.c b/src/Utility/Utility.c new file mode 100644 index 0000000..8e5af92 --- /dev/null +++ b/src/Utility/Utility.c @@ -0,0 +1,8 @@ +#include "Utility.h" + +int hex_to_int(char c) { + if (c >= '0' && c <= '9') return c - '0'; + if (c >= 'a' && c <= 'f') return c - 'a' + 10; + if (c >= 'A' && c <= 'F') return c - 'A' + 10; + return -1; +} diff --git a/src/Utility/Utility.h b/src/Utility/Utility.h new file mode 100644 index 0000000..3b0181c --- /dev/null +++ b/src/Utility/Utility.h @@ -0,0 +1,6 @@ +#ifndef UTILITY_H +#define UTILITY_H + +int hex_to_int(char c); + +#endif -- cgit v1.2.3