fileshare/src/util.c

52 lines
1.1 KiB
C

/*
* vim: sw=2 ts=2 expandtab
*
* "THE PIZZA-WARE LICENSE" (derived from "THE BEER-WARE LICENCE"):
* Thomas Kolb <cfr34k@tkolb.de> wrote this file. As long as you retain this
* notice you can do whatever you want with this stuff. If we meet some day,
* and you think this stuff is worth it, you can buy me a pizza in return.
* - Thomas Kolb
*/
#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++;
}
}