/* * common.c: Web video plugin for the Video Disk Recorder * * See the README file for copyright information and how to reach the author. * * $Id$ */ #include #include #include #include #include #include #include #include "common.h" char *extensionFromUrl(const char *url) { if (!url) return NULL; // Find the possible query ("?query=foo") or fragment ("#bar"). The // extension is located right before them. size_t extendpos = strcspn(url, "?#"); size_t extstartpos = extendpos-1; while ((extstartpos > 0) && (url[extstartpos] != '.') && (url[extstartpos] != '/')) extstartpos--; if ((extstartpos > 0) && (url[extstartpos] == '.')) { // We found the extension. Copy it to a buffer, and return it. char *ext = (char *)malloc(sizeof(char)*(extendpos-extstartpos+1)); memcpy(ext, &url[extstartpos], extendpos-extstartpos); ext[extendpos-extstartpos] = '\0'; return ext; } return NULL; } char *validateFileName(const char *filename) { if (!filename) return NULL; char *validated = (char *)malloc(strlen(filename)+1); int j=0; for (unsigned int i=0; i', '#', '%', '{', '}', '|', '\\', '^', '~', '[', ']', '`', '\0' }; char *buf = (char *)malloc((3*strlen(s)+1)*sizeof(char)); if (!buf) return NULL; unsigned char *out; const unsigned char *in; for (out=(unsigned char *)buf, in=(const unsigned char *)s; *in != '\0'; in++) { if ((*in < 32) // control chracters || (strchr(reserved_and_unsafe, *in)) // reserved and unsafe || (*in > 127)) // non-ASCII { snprintf((char *)out, 4, "%%%02hhX", *in); out += 3; } else { *out = *in; out++; } } *out = '\0'; return buf; } char *URLdecode(const char *s) { char *res = (char *)malloc(strlen(s)+1); const char *in = s; char *out = res; const char *hex = "0123456789ABCDEF"; const char *h1, *h2; while (*in) { if ((*in == '%') && (in[1] != '\0') && (in[2] != '\0')) { h1 = strchr(hex, toupper(in[1])); h2 = strchr(hex, toupper(in[2])); if (h1 && h2) { *out = ((h1-hex) << 4) + (h2-hex); in += 3; } else { *out = *in; in++; } } else { *out = *in; in++; } out++; } *out = '\0'; return res; } char *safeFilename(char *filename) { if (filename) { strreplace(filename, '/', '!'); char *p = filename; while ((*p == '.') || isspace(*p)) { p++; } if (p != filename) { memmove(filename, p, strlen(p)+1); } } return filename; }