oopsies
This commit is contained in:
46
src/Utility/Display.c
Normal file
46
src/Utility/Display.c
Normal file
@@ -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;
|
||||
}
|
||||
6
src/Utility/Display.h
Normal file
6
src/Utility/Display.h
Normal file
@@ -0,0 +1,6 @@
|
||||
#ifndef DISPLAY_H
|
||||
#define DISPLAY_H
|
||||
|
||||
char *pretty_display_url(const char *input);
|
||||
|
||||
#endif
|
||||
80
src/Utility/Unescape.c
Normal file
80
src/Utility/Unescape.c
Normal file
@@ -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;
|
||||
}
|
||||
10
src/Utility/Unescape.h
Normal file
10
src/Utility/Unescape.h
Normal file
@@ -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
|
||||
|
||||
8
src/Utility/Utility.c
Normal file
8
src/Utility/Utility.c
Normal file
@@ -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;
|
||||
}
|
||||
6
src/Utility/Utility.h
Normal file
6
src/Utility/Utility.h
Normal file
@@ -0,0 +1,6 @@
|
||||
#ifndef UTILITY_H
|
||||
#define UTILITY_H
|
||||
|
||||
int hex_to_int(char c);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user