[MPlayer-dev-eng] http stream basic auth base64_encode() fix

Jeff D'Angelo jcd+mplayer at psu.edu
Wed Mar 15 06:02:24 CET 2006


On Tue, 14 Mar 2006, Reimar Döffinger wrote:

> Hi,
> On Tue, Mar 14, 2006 at 02:39:20PM -0500, Jeff D'Angelo wrote:
> > Let me know if you want anything else from me for the libmpdemux/url.c
> > patch from March 6 (updated patch from root folder attached).

Thanks for getting back to me so fast.

> Hmm... Somehow didn't even see that one...

Ya, sorry.  The bug this fixes and the patch got lost earlier in the
thread.

> > @@ -86,12 +86,15 @@
> >  		// We got something, at least a username...
> >  		int len = ptr2-ptr1;
> >  		Curl->username = (char*)malloc(len+1);
> > -		if( Curl->username==NULL ) {
> > +		char *usernametmp = (char*)malloc(len+1);
> > +		if( Curl->username==NULL || usernametmp==NULL ) {
> >  			mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
> >  			goto err_out;
> >  		}
> > -		strncpy(Curl->username, ptr1, len);
> > -		Curl->username[len] = '\0';
> > +		strncpy(usernametmp, ptr1, len);
> > +		usernametmp[len] = '\0';
> > +		url_unescape_string( Curl->username, usernametmp );
> > +		free(usernametmp);
>
> IMHO Ugly.

Honestly, I agree with you (see my note on the 6th).  I knew there were a
few different ways to go about it.

I figured doing it the simplest way first could elicit a helpful response
on which way to go.

> Still ugly, but maybe better:
> Just do url_unescape_string( Curl->username, Curl->username );

Interesting.  I guess that would work.  Hadn't thought of it.

> But maybe best (if it works, I think it should):
> *ptr2 = 0;
> and replace the strncpy by url_unescape_string.

I'm attracted to a possibly more elagant solution like this, too.

Unfortunately, I think you break the rest of the parsing if you drop a
null char in the middle of the string while you are still parsing it.
Isn't ptr2 still used to find things like the optional port value?  Maybe
there's another trick here than can be used.

Another way is to make an "n" version of url_unescape_string() to replace
the strncpy() like I do in the enclosed patch.  Let me know what you
think.

Thanks again.

-- 
Jeff

> Greetings,
> Reimar Döffinger
>
> _______________________________________________
> MPlayer-dev-eng mailing list
> MPlayer-dev-eng at mplayerhq.hu
> http://mplayerhq.hu/mailman/listinfo/mplayer-dev-eng
>
-------------- next part --------------
Index: libmpdemux/url.c
===================================================================
RCS file: /cvsroot/mplayer/main/libmpdemux/url.c,v
retrieving revision 1.30
diff -u -r1.30 url.c
--- libmpdemux/url.c	26 Nov 2005 15:57:39 -0000	1.30
+++ libmpdemux/url.c	15 Mar 2006 04:23:31 -0000
@@ -90,8 +90,7 @@
 			mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
 			goto err_out;
 		}
-		strncpy(Curl->username, ptr1, len);
-		Curl->username[len] = '\0';
+		url_unescape_nstring(Curl->username, ptr1, len);
 
 		ptr3 = strstr(ptr1, ":");
 		if( ptr3!=NULL && ptr3<ptr2 ) {
@@ -103,8 +102,7 @@
 				mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
 				goto err_out;
 			}
-			strncpy( Curl->password, ptr3+1, len2);
-			Curl->password[len2]='\0';
+			url_unescape_nstring( Curl->password, ptr3+1, len2);
 		}
 		ptr1 = ptr2+1;
 		pos1 = ptr1-escfilename;
@@ -208,8 +206,14 @@
 void
 url_unescape_string(char *outbuf, const char *inbuf)
 {
+	url_unescape_nstring(outbuf,inbuf,strlen(inbuf));
+}
+/* the "n" version of url_unescape_string(), parallels strncpy() */
+void
+url_unescape_nstring(char *outbuf, const char *inbuf, int len)
+{
 	unsigned char c,c1,c2;
-        int i,len=strlen(inbuf);
+        int i;
         for (i=0;i<len;i++){
 		c = inbuf[i];
 		if (c == '%' && i<len-2) { //must have 2 more chars
Index: libmpdemux/url.h
===================================================================
RCS file: /cvsroot/mplayer/main/libmpdemux/url.h,v
retrieving revision 1.9
diff -u -r1.9 url.h
--- libmpdemux/url.h	26 Mar 2003 11:27:48 -0000	1.9
+++ libmpdemux/url.h	15 Mar 2006 04:23:31 -0000
@@ -23,6 +23,7 @@
 void   url_free(URL_t* url);
 
 void url_unescape_string(char *outbuf, const char *inbuf);
+void url_unescape_nstring(char *outbuf, const char *inbuf, int len);
 void url_escape_string(char *outbuf, const char *inbuf);
 
 #ifdef __URL_DEBUG


More information about the MPlayer-dev-eng mailing list