/* * call-seq: * Xmms.decode_url(url) -> String * * Decodes a url-encoded string _url_ and returns it in UNKNOWN ENCODING. * Use with caution. */ static VALUE m_decode_url (VALUE self, VALUE str) { const unsigned char *burl; unsigned int blen; xmmsv_t *strv, *decoded; VALUE url = Qnil; strv = xmmsv_new_string (StringValuePtr (str)); decoded = xmmsv_decode_url (strv); if (!decoded) goto out; if (!xmmsv_get_bin (decoded, &burl, &blen)) goto out; url = rb_str_new ((char *) burl, blen); out: if (decoded) xmmsv_unref (decoded); xmmsv_unref (strv); return url; }
static void print_entry_string (xmmsv_t *v, const gchar *key, const gchar *source) { const gchar *value; xmmsv_get_string (v, &value); /* Ok it's a string, if it's the URL property from the * server source we need to decode it since it's * encoded in the server */ if (strcmp (key, "url") == 0 && strcmp (source, "server") == 0) { /* First decode the URL encoding */ xmmsv_t *tmp; gchar *url = NULL; const unsigned char *burl; unsigned int blen; tmp = xmmsv_decode_url (v); if (tmp && xmmsv_get_bin (tmp, &burl, &blen)) { url = g_malloc (blen + 1); memcpy (url, burl, blen); url[blen] = 0; xmmsv_unref (tmp); } /* Let's see if the result is valid utf-8. This must be done * since we don't know the charset of the binary string */ if (url && g_utf8_validate (url, -1, NULL)) { /* If it's valid utf-8 we don't have any problem just * printing it to the screen */ print_info ("[%s] %s = %s", source, key, url); } else if (url) { /* Not valid utf-8 :-( We make a valid guess here that * the string when it was encoded with URL it was in the * same charset as we have on the terminal now. * * THIS MIGHT BE WRONG since different clients can have * different charsets and DIFFERENT computers most likely * have it. */ gchar *tmp2 = g_locale_to_utf8 (url, -1, NULL, NULL, NULL); /* Lets add a disclaimer */ print_info ("[%s] %s = %s (charset guessed)", source, key, tmp2); g_free (tmp2); } else { /* Decoding the URL failed for some reason. That's not good. */ print_info ("[%s] %s = (invalid encoding)", source, key); } g_free (url); } else { /* Normal strings is ALWAYS utf-8 no problem */ print_info ("[%s] %s = %s", source, key, value); } }
/** Decode an URL-encoded string. * * Some strings (currently only the url of media) have no known * encoding, and must be encoded in an UTF-8 clean way. This is done * similarly to the url encoding web browsers do. This functions decodes * a string encoded in that way. OBSERVE that the decoded string HAS * NO KNOWN ENCODING and you cannot display it on screen in a 100% * guaranteed correct way (a good heuristic is to try to validate the * decoded string as UTF-8, and if it validates assume that it is an * UTF-8 encoded string, and otherwise fall back to some other * encoding). * * Do not use this function if you don't understand the * implications. The best thing is not to try to display the url at * all. * * Note that the fact that the string has NO KNOWN ENCODING and CAN * NOT BE DISPLAYED does not stop you from open the file if it is a * local file (if it starts with "file://"). * * @param encoded_url the url-encoded string * @return the decoded string in an unknown encoding * */ std::string decodeUrl( const std::string& encoded_url ) { xmmsv_t *encoded, *decoded; const unsigned char *url; unsigned int len; std::string dec_str; encoded = xmmsv_new_string( encoded_url.c_str() ); decoded = xmmsv_decode_url( encoded ); if( !xmmsv_get_bin( decoded, &url, &len ) ) { throw invalid_url( "The given URL cannot be decoded." ); } dec_str = std::string( reinterpret_cast< const char* >( url ), len ); xmmsv_unref( encoded ); xmmsv_unref( decoded ); return dec_str; }
char *decode_url(const char *url) { xmmsv_t *url_str; xmmsv_t *decoded_url; char *new_url = NULL; const unsigned char *url_tmp; unsigned int url_len; url_str = xmmsv_new_string(url); if (url_str) { decoded_url = xmmsv_decode_url(url_str); if (decoded_url) { if (xmmsv_get_bin(decoded_url, &url_tmp, &url_len)) { new_url = (char*) malloc( sizeof(char)*(url_len+1) ); snprintf(new_url, url_len+1, "%s", url_tmp); } } xmmsv_unref(decoded_url); } xmmsv_unref(url_str); return new_url; }