/* we don't share code here with medialib because we want to use g_malloc :( */ gchar * xmms_medialib_url_encode (const gchar *path) { static gchar hex[16] = "0123456789abcdef"; gchar *res; gint i = 0, j = 0; res = g_malloc (strlen (path) * 3 + 1); if (!res) { return NULL; } while (path[i]) { guchar chr = path[i++]; if (GOODCHAR (chr)) { res[j++] = chr; } else if (chr == ' ') { res[j++] = '+'; } else { res[j++] = '%'; res[j++] = hex[((chr & 0xf0) >> 4)]; res[j++] = hex[(chr & 0x0f)]; } } res[j] = '\0'; return res; }
int _xmmsc_medialib_verify_url (const char *url) { int i; for (i = 0; url[i]; i++) { if (!(GOODCHAR (url[i]) || url[i] == '+' || url[i] == '%' || url[i] == '?' || url[i] == '=' || url[i] == '&')) return 0; } return 1; }
char * _xmmsc_medialib_encode_url_old (const char *url, int narg, const char **args) { static const char hex[16] = "0123456789abcdef"; int i = 0, j = 0, extra = 0; char *res; x_api_error_if (!url, "with a NULL url", NULL); for (i = 0; i < narg; i++) { extra += strlen (args[i]) + 2; } res = malloc (strlen (url) * 3 + 1 + extra); if (!res) return NULL; for (i = 0; url[i]; i++) { unsigned char chr = url[i]; if (GOODCHAR (chr)) { res[j++] = chr; } else if (chr == ' ') { res[j++] = '+'; } else { res[j++] = '%'; res[j++] = hex[((chr & 0xf0) >> 4)]; res[j++] = hex[(chr & 0x0f)]; } } for (i = 0; i < narg; i++) { int l; l = strlen (args[i]); res[j] = (i == 0) ? '?' : '&'; j++; memcpy (&res[j], args[i], l); j += l; } res[j] = '\0'; return res; }
/* TODO: need to add support for arguments here */ QString Medialib::encodeUrl (const QString &src, const QStringList &args) { QString ret; #if QT_VERSION >= 0x040400 QByteArray data = src.toUtf8 (); // this might have to be toLatin1() QByteArray enc = data.toPercentEncoding (excludeEnc, includeEnc); enc.replace (' ', '+'); ret = QString (enc); #else static const char hex[17] = "0123456789abcdef"; for (int i = 0; i < src.size (); i ++) { char chr = src.at (i).toLatin1 (); if (GOODCHAR (chr)) { ret += chr; } else if (chr == ' ') { ret += '+'; } else { ret += '%'; ret += hex[((chr & 0xf0) >> 4)]; ret += hex[(chr & 0x0f)]; } } #endif if (args.size () > 0) { for (int i = 0; i < args.size (); i++) { ret.append ( (i == 0) ? '?' : '&'); // Perhaps I should check for '?' and '&' here. // But the c bindings don't so I left it out ret.append (args[i]); } } return ret; }
/** * Encodes an url with arguments stored in dict args. * * The encoded url is allocated using malloc and has to be freed by the user. * * @param url The url to encode. * @param args The dict with arguments, or NULL. * @return The encoded url */ char * xmmsc_medialib_encode_url_full (const char *url, xmmsv_t *args) { static const char hex[16] = "0123456789abcdef"; int i = 0, j = 0, extra = 0, l; char *res; xmmsv_dict_iter_t *it; x_api_error_if (!url, "with a NULL url", NULL); if (args) { if (!xmmsv_dict_foreach (args, _sum_len_string_dict, (void *) &extra)) { return NULL; } } /* Provide enough room for the worst-case scenario (all characters of the URL must be encoded), the args, and a \0. */ res = malloc (strlen (url) * 3 + 1 + extra); if (!res) { return NULL; } for (i = 0; url[i]; i++) { unsigned char chr = url[i]; if (GOODCHAR (chr)) { res[j++] = chr; } else if (chr == ' ') { res[j++] = '+'; } else { res[j++] = '%'; res[j++] = hex[((chr & 0xf0) >> 4)]; res[j++] = hex[(chr & 0x0f)]; } } if (args) { for (xmmsv_get_dict_iter (args, &it), i = 0; xmmsv_dict_iter_valid (it); xmmsv_dict_iter_next (it), i++) { const char *arg, *key; xmmsv_t *val; xmmsv_dict_iter_pair (it, &key, &val); l = strlen (key); res[j] = (i == 0) ? '?' : '&'; j++; memcpy (&res[j], key, l); j += l; if (xmmsv_get_string (val, &arg)) { l = strlen (arg); res[j] = '='; j++; memcpy (&res[j], arg, l); j += l; } } } res[j] = '\0'; return res; }