Path: blob/main/filesystems/curlftpfs/files/patch-path__utils.c
16464 views
--- path_utils.c.orig 2007-11-20 19:27:58 UTC1+++ path_utils.c2@@ -92,3 +92,72 @@ char* get_dir_path(const char* path) {34return ret;5}6+7+/*8+ * the chars not needed to be escaped:9+ * unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"10+ */11+static inline int is_unreserved_rfc3986(char c)12+{13+ int is_locase_alpha = (c >= 'a' && c <= 'z');14+ int is_upcase_alpha = (c >= 'A' && c <= 'Z');15+ int is_digit = (c >= '0' && c <= '9');16+ int is_special = c == '-'17+ || c == '.'18+ || c == '_'19+ || c == '~';20+ int is_unreserved = is_locase_alpha21+ || is_upcase_alpha22+ || is_digit23+ || is_special;24+25+ return is_unreserved;26+}27+28+static inline int is_unreserved(char c)29+{30+ return is_unreserved_rfc3986(c) || c == '/';31+}32+33+char* path_to_uri(const char* path)34+{35+ static const char hex[] = "0123456789ABCDEF";36+ size_t path_len = strlen(path);37+ size_t host_len = strlen(ftpfs.host);38+ /* at worst: c -> %XX */39+ char * encoded_path = malloc (3 * path_len + 1);40+ const char * s = path;41+ char * d = encoded_path;42+43+ /*44+ * 'path' is always prefixed with 'ftpfs.host'45+ */46+ memcpy (d, ftpfs.host, host_len);47+ s += host_len;48+ d += host_len;49+50+ for (; *s; ++s)51+ {52+ char c = *s;53+ if (is_unreserved (c))54+ {55+ *d++ = c;56+ }57+ else58+ {59+ unsigned int hi = ((unsigned)c >> 4) & 0xF;60+ unsigned int lo = ((unsigned)c >> 0) & 0xF;61+ *d++ = '%';62+ *d++ = hex[hi];63+ *d++ = hex[lo];64+ }65+ }66+ *d = '\0';67+68+ return encoded_path;69+}70+71+void free_uri(char* path)72+{73+ free(path);74+}757677