From 24c6f11c8b8d08cb98648b324098f7f8c3f0bd03 Mon Sep 17 00:00:00 2001 From: bertrand Date: Fri, 14 Dec 2001 23:48:47 +0000 Subject: [PATCH] Added 2 functions to escape/unescape the url as described in the RFC 2396. Code borrowed from ASFRecorder. git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@3497 b3059339-0415-0410-9bf9-f77b7e298cf2 --- libmpdemux/url.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++- libmpdemux/url.h | 3 +++ 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/libmpdemux/url.c b/libmpdemux/url.c index 410664368c..8633133319 100644 --- a/libmpdemux/url.c +++ b/libmpdemux/url.c @@ -1,4 +1,4 @@ -/* +/* * URL Helper * by Bertrand Baudet * (C) 2001, MPlayer team. @@ -134,3 +134,63 @@ url_free(URL_t* url) { if(url->password) free(url->password); free(url); } + + +/* Replace escape sequences in an URL (or a part of an URL) */ +/* works like strcpy(), but without return argument */ +/* unescape_url_string comes from ASFRecorder */ +void +url_unescape_string(char *outbuf, char *inbuf) +{ + unsigned char c; + do { + c = *inbuf++; + if (c == '%') { + unsigned char c1 = *inbuf++; + unsigned char c2 = *inbuf++; + if ( ((c1>='0' && c1<='9') || (c1>='A' && c1<='F')) && + ((c2>='0' && c2<='9') || (c2>='A' && c2<='F')) ) { + if (c1>='0' && c1<='9') c1-='0'; + else c1-='A'; + if (c2>='0' && c2<='9') c2-='0'; + else c2-='A'; + c = (c1<<4) + c2; + } + } + *outbuf++ = c; + } while (c != '\0'); +} + +/* Replace specific characters in the URL string by an escape sequence */ +/* works like strcpy(), but without return argument */ +/* escape_url_string comes from ASFRecorder */ +void +url_escape_string(char *outbuf, char *inbuf) { + unsigned char c; + do { + c = *inbuf++; + if( (c >= 'A' && c <= 'Z') || + (c >= 'a' && c <= 'z') || + (c >= '0' && c <= '9') || + (c >= 0x7f) || /* fareast languages(Chinese, Korean, Japanese) */ + c=='-' || c=='_' || c=='.' || c=='!' || c=='~' || /* mark characters */ + c=='*' || c=='\'' || c=='(' || c==')' || c=='%' || /* do not touch escape character */ + c==';' || c=='/' || c=='?' || c==':' || c=='@' || /* reserved characters */ + c=='&' || c=='=' || c=='+' || c=='$' || c==',' || /* see RFC 2396 */ + c=='\0' ) { + *outbuf++ = c; + } else { + /* all others will be escaped */ + unsigned char c1 = ((c & 0xf0) >> 4); + unsigned char c2 = (c & 0x0f); + if (c1 < 10) c1+='0'; + else c1+='A'; + if (c2 < 10) c2+='0'; + else c2+='A'; + *outbuf++ = '%'; + *outbuf++ = c1; + *outbuf++ = c2; + } + } while (c != '\0'); +} + diff --git a/libmpdemux/url.h b/libmpdemux/url.h index 0fe5cba491..a7a9e1f545 100644 --- a/libmpdemux/url.h +++ b/libmpdemux/url.h @@ -21,4 +21,7 @@ URL_t* url_new(char* url); URL_t* url_copy(URL_t* url); void url_free(URL_t* url); +void url_unescape_string(char *outbuf, char *inbuf); +void url_escape_string(char *outbuf, char *inbuf); + #endif