diff options
| author | frosty <frosty@illegalfirearms.store> | 2026-01-06 23:46:24 -0500 |
|---|---|---|
| committer | frosty <frosty@illegalfirearms.store> | 2026-01-06 23:46:24 -0500 |
| commit | f3aa7ca0bc2ef7c286609e8f87d07cc2568093af (patch) | |
| tree | 269352af1238b4dd7c3e2e023f71a27b858cdb34 /src/Utility | |
rebase(d)
Diffstat (limited to 'src/Utility')
| -rw-r--r-- | src/Utility/Display.c | 46 | ||||
| -rw-r--r-- | src/Utility/Display.h | 6 | ||||
| -rw-r--r-- | src/Utility/Unescape.c | 80 | ||||
| -rw-r--r-- | src/Utility/Unescape.h | 10 | ||||
| -rw-r--r-- | src/Utility/Utility.c | 8 | ||||
| -rw-r--r-- | src/Utility/Utility.h | 6 |
6 files changed, 156 insertions, 0 deletions
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 <ctype.h> +#include <stdlib.h> +#include <string.h> +#include <strings.h> + +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 <stdlib.h> +#include <string.h> + +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 <stddef.h> + +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 |
