fileshare/util.c

42 lines
813 B
C

#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include "util.h"
void remove_trailing_slash(char *str) {
size_t offset = strlen(str)-1;
if((offset != 0) && (str[offset] == '/')) {
str[offset] = '\0';
}
}
void urlencode(const char *str, char *result) {
uint8_t c;
while(*str) {
c = *str;
if((c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9') ||
c == '-' ||
c == '_' ||
c == '!' ||
c == '(' ||
c == ')' ||
c == '$' ||
c == '*' ||
c == '\'' ||
c == ',' ||
c == '.') {
// simply print safe characters
result += sprintf(result, "%c", c);
} else {
// encode all others
result += sprintf(result, "%%%02X", c);
}
str++;
}
}