comparison libmpdemux/url.c @ 3496:5c8a533dfa09

Added 2 functions to escape/unescape the url as described in the RFC 2396. Code borrowed from ASFRecorder.
author bertrand
date Fri, 14 Dec 2001 23:48:47 +0000
parents fb9de639ed30
children a1522fa7728a
comparison
equal deleted inserted replaced
3495:cc1c879533ee 3496:5c8a533dfa09
1 /* 1 /*
2 * URL Helper 2 * URL Helper
3 * by Bertrand Baudet <bertrand_baudet@yahoo.com> 3 * by Bertrand Baudet <bertrand_baudet@yahoo.com>
4 * (C) 2001, MPlayer team. 4 * (C) 2001, MPlayer team.
5 * 5 *
6 * TODO: 6 * TODO:
132 if(url->file) free(url->file); 132 if(url->file) free(url->file);
133 if(url->username) free(url->username); 133 if(url->username) free(url->username);
134 if(url->password) free(url->password); 134 if(url->password) free(url->password);
135 free(url); 135 free(url);
136 } 136 }
137
138
139 /* Replace escape sequences in an URL (or a part of an URL) */
140 /* works like strcpy(), but without return argument */
141 /* unescape_url_string comes from ASFRecorder */
142 void
143 url_unescape_string(char *outbuf, char *inbuf)
144 {
145 unsigned char c;
146 do {
147 c = *inbuf++;
148 if (c == '%') {
149 unsigned char c1 = *inbuf++;
150 unsigned char c2 = *inbuf++;
151 if ( ((c1>='0' && c1<='9') || (c1>='A' && c1<='F')) &&
152 ((c2>='0' && c2<='9') || (c2>='A' && c2<='F')) ) {
153 if (c1>='0' && c1<='9') c1-='0';
154 else c1-='A';
155 if (c2>='0' && c2<='9') c2-='0';
156 else c2-='A';
157 c = (c1<<4) + c2;
158 }
159 }
160 *outbuf++ = c;
161 } while (c != '\0');
162 }
163
164 /* Replace specific characters in the URL string by an escape sequence */
165 /* works like strcpy(), but without return argument */
166 /* escape_url_string comes from ASFRecorder */
167 void
168 url_escape_string(char *outbuf, char *inbuf) {
169 unsigned char c;
170 do {
171 c = *inbuf++;
172 if( (c >= 'A' && c <= 'Z') ||
173 (c >= 'a' && c <= 'z') ||
174 (c >= '0' && c <= '9') ||
175 (c >= 0x7f) || /* fareast languages(Chinese, Korean, Japanese) */
176 c=='-' || c=='_' || c=='.' || c=='!' || c=='~' || /* mark characters */
177 c=='*' || c=='\'' || c=='(' || c==')' || c=='%' || /* do not touch escape character */
178 c==';' || c=='/' || c=='?' || c==':' || c=='@' || /* reserved characters */
179 c=='&' || c=='=' || c=='+' || c=='$' || c==',' || /* see RFC 2396 */
180 c=='\0' ) {
181 *outbuf++ = c;
182 } else {
183 /* all others will be escaped */
184 unsigned char c1 = ((c & 0xf0) >> 4);
185 unsigned char c2 = (c & 0x0f);
186 if (c1 < 10) c1+='0';
187 else c1+='A';
188 if (c2 < 10) c2+='0';
189 else c2+='A';
190 *outbuf++ = '%';
191 *outbuf++ = c1;
192 *outbuf++ = c2;
193 }
194 } while (c != '\0');
195 }
196