From 997c6f8f7b3dd50444a623d041bf745cbde83268 Mon Sep 17 00:00:00 2001
From: lgb <lgb@b3059339-0415-0410-9bf9-f77b7e298cf2>
Date: Fri, 4 Jan 2002 13:08:14 +0000
Subject: [PATCH] Return of the 'Old-style-DVD-support', with dynamic loading
 (using libdl) so no more conflict with libdvdread: you can compile in both of
 libdvdread and libcss support! You can even select libcss.so to load from
 command line or configuration file, with '-csslib /usr/local/lib/libcss.so'
 or something similar. Default for this option is /usr/local/lib/libcss.so.
 Note: libcss version (ver>0.1) with newer API is not supported in this
 version! This is the first version so stay tuned :)

git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@3976 b3059339-0415-0410-9bf9-f77b7e298cf2
---
 cfg-common.h           |  18 ++---
 configure              |  47 ++++++------
 fibmap_mplayer.c       |   5 +-
 libmpdemux/demux_mpg.c |   2 +-
 libmpdemux/dvdauth.c   | 160 ++++++++++++++++++++++++-----------------
 libmpdemux/dvdauth.h   |   2 +
 6 files changed, 133 insertions(+), 101 deletions(-)

diff --git a/cfg-common.h b/cfg-common.h
index 14c6f8d5bd..c1513ff20f 100644
--- a/cfg-common.h
+++ b/cfg-common.h
@@ -22,21 +22,11 @@
 #ifdef HAVE_LIBCSS
         {"dvdauth", &dvd_auth_device, CONF_TYPE_STRING, 0, 0, 0},
         {"dvdkey", &dvdimportkey, CONF_TYPE_STRING, 0, 0, 0},
-//	{"dvd", "Option -dvd will be \"full disk\" mode, old meaning has been renamed to -dvdauth.\n", CONF_TYPE_PRINT, CONF_NOCFG, 0, 0},
+	{"csslib", &css_so, CONF_TYPE_STRING, 0, 0, 0},
 #else
-//        {"dvd", "DVD support was not compiled in. See file DOCS/DVD.\n",
-//            CONF_TYPE_PRINT, CONF_NOCFG, 0 , 0},
-#ifdef USE_DVDREAD
-        {"dvdkey", "MPlayer was compiled with libdvdread support, this option not available.\n",
-            CONF_TYPE_PRINT, CONF_NOCFG, 0 , 0},
-        {"dvdauth", "MPlayer was compiled with libdvdread support! Use option -dvd !\n",
-            CONF_TYPE_PRINT, CONF_NOCFG, 0 , 0},
-#else
-        {"dvdkey", "DVD support was not compiled in. See file DOCS/DVD.\n",
-            CONF_TYPE_PRINT, CONF_NOCFG, 0 , 0},
-        {"dvdauth", "DVD support was not compiled in. See file DOCS/DVD.\n",
-            CONF_TYPE_PRINT, CONF_NOCFG, 0 , 0},
-#endif
+        {"dvdauth", "MPlayer was compiled WITHOUT libcss support!\n", CONF_TYPE_PRINT, CONF_NOCFG, 0, 0},
+        {"dvdkey", "MPlayer was compiled WITHOUT libcss support!\n", CONF_TYPE_PRINT, CONF_NOCFG, 0, 0},
+	{"csslib", "MPlayer was compiled WITHOUT libcss support!\n", CONF_TYPE_PRINT, CONF_NOCFG, 0, 0},
 #endif
 
 // ------------------------- demuxer options --------------------
diff --git a/configure b/configure
index 92a8cf5658..12e24f0dad 100755
--- a/configure
+++ b/configure
@@ -2069,7 +2069,31 @@ else
 fi
 
 
-echocheck "DVD support"
+
+
+echocheck "DVD support (libcss - old style)"
+if test "$_css" = auto ; then
+  cat > $TMPC <<EOF
+#include <types.h>
+#include <css.h>
+int main(void) { (void) CSSisEncrypted(0); return 0; }
+EOF
+  _css=no
+  cc_check -lcss $_ld_dl && _css=yes
+fi
+if test "$_css" = yes ; then
+  _def_css='#define HAVE_LIBCSS 1'
+  test "$_csslibdir" && _ld_css="-L${_csslibdir} $_ld_css"
+  _inputmodules="dvdcss $_inputmodules"
+  _largefiles=yes
+  echores "yes"
+else
+  _def_css='#undef HAVE_LIBCSS'
+  echores "no"
+fi
+
+
+echocheck "DVD support (libdvdread - new style)"
 if test "$_dvdread" = auto ; then
   cat > $TMPC << EOF
 #include <dvdread/dvd_reader.h>
@@ -2085,36 +2109,17 @@ EOF
      _dvdread=yes
   fi
 fi
-if test "$_css" = auto ; then
-  cat > $TMPC <<EOF
-#include <css.h>
-int main(void) { (void) CSSisEncrypted(0); return 0; }
-EOF
-  _css=no
-  cc_check -lcss && _css=yes
-fi
-# dvdread preferred to DeCSS
 if test "$_dvdread" = yes ; then
   _largefiles=yes
   _def_dvdread='#define USE_DVDREAD 1'
-  _def_css='#undef HAVE_LIBCSS'
   _ld_css='-ldvdread'
   _inputmodules="dvdread $_inputmodules"
-  echores "libdvdread"
-elif test "$_css" = yes ; then
-  _def_dvdread='#undef USE_DVDREAD'
-  _def_css='#define HAVE_LIBCSS 1'
-  _ld_css='-lcss'
-  test "$_csslibdir" && _ld_css="-L${_csslibdir} $_ld_css"
-  _inputmodules="dvdcss $_inputmodules"
-  echores "libcss"
+  echores "yes"
 else
   _def_dvdread='#undef USE_DVDREAD'
-  _def_css='#undef HAVE_LIBCSS'
   echores "no"
 fi
 
-
 echocheck "zlib"
 cat > $TMPC << EOF
 #include <zlib.h>
diff --git a/fibmap_mplayer.c b/fibmap_mplayer.c
index ead0879212..75395a90a8 100644
--- a/fibmap_mplayer.c
+++ b/fibmap_mplayer.c
@@ -1,3 +1,6 @@
+/* (C)2001,2002 by LGB (G�bor L�n�rt), lgb@lgb.hu
+   Part of MPlayer project, this source is copyrighted according to GNU/GPL.  */
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -30,6 +33,6 @@ int main ( int argc , char ** argv )
             return 1;
         }
 	close(fd);
-	fprintf(stderr,"%d\n",lba);
+	printf("%d\n",lba);
 	return 0;
 }
diff --git a/libmpdemux/demux_mpg.c b/libmpdemux/demux_mpg.c
index 4f91ad2ebf..95fa6168f0 100644
--- a/libmpdemux/demux_mpg.c
+++ b/libmpdemux/demux_mpg.c
@@ -221,7 +221,7 @@ static int demux_mpg_read_packet(demuxer_t *demux,int id){
 //    printf("packet start = 0x%X  \n",stream_tell(demux->stream)-packet_start_pos);
 #ifdef HAVE_LIBCSS
     if (css) {
-	    if (descrambling) CSSDescramble(demux->stream->buffer,key_title); else
+	    if (descrambling) dvd_css_descramble(demux->stream->buffer,key_title); else
 		    mp_msg(MSGT_DEMUX,MSGL_WARN,MSGTR_EncryptedVOBauth);
     }
 #endif
diff --git a/libmpdemux/dvdauth.c b/libmpdemux/dvdauth.c
index 248153bcef..113d2dce60 100644
--- a/libmpdemux/dvdauth.c
+++ b/libmpdemux/dvdauth.c
@@ -1,10 +1,24 @@
-/* (C)2001 by LGB (Gabor Lenart), based on example programs in libcss
-           lgb@lgb.hu                                                        */
+/* (C)2001,2002 by LGB (Gabor Lenart), based on example programs in libcss
+                lgb@lgb.hu
+		
+   This source is part of MPlayer project. This source is copyrighted by
+   the author according to rules declared in GNU/GPL license.
+
+   2001		Inital version (LGB)
+   2001		fibmap_mplayer to avoid uid=0 mplayer need (LGB)
+   2001		Support for libcss with the new API (by ???)
+   2002/Jan/04  Use dlopen to access libcss.so to avoid conflict with
+                libdvdread [now with only libcss with old API (LGB)          
+		
+   TODO:
+		support for libcss libraries with new API		     */
 
 /* don't do anything with this source if css support was not requested */
 #include "config.h"
 #ifdef HAVE_LIBCSS
 
+#warning FIXME: Dynamic loading of libcss.so with newer (ver>0.1) libcss API is not supported in this version!
+
 #include <stdio.h>
 #include <stdlib.h>
 //#include <string.h>      // FIXME: conflicts with fs.h
@@ -15,27 +29,23 @@
 #include <sys/ioctl.h>
 #include <sys/stat.h>
 #include <sys/wait.h>
-#include <css.h>
-#if CSS_MAJOR_VERSION > 0 || (CSS_MAJOR_VERSION == 0 && CSS_MINOR_VERSION > 1)
-# include <dvd.h>
-# undef	 OLD_CSS_API
-#else
-# if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
+// #include <css.h>
+
+#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
 #  include <sys/dvdio.h>
-# elif defined(__linux__)
+#elif defined(__linux__)
 #  include <linux/cdrom.h>
-# elif defined(__sun)
+#elif defined(__sun)
 #  include <sun/dvdio.h>
-# else
+#else
 #  error "Need the DVD ioctls"
-# endif
-# define OLD_CSS_API 1
 #endif
 
+#include <dlfcn.h>
 #include "dvdauth.h"
 
 
-#if	OLD_CSS_API
+// #if	OLD_CSS_API
 /*
  * provide some backward compatibiliy macros to compile this
  * code using the old libcss-0.1
@@ -46,35 +56,33 @@
 #define	DVDAuth(hdl, s)		ioctl(hdl, DVD_AUTH, s)
 #define	DVDOpenDevice(path)	open(path, O_RDONLY)
 #define	DVDCloseDevice(hdl)	close(hdl)
-#define	CSSDVDisEncrypted(hdl)	CSSisEncrypted(hdl)
-#define	CSSDVDAuthDisc		CSSAuthDisc
-/* Arghhh! Please think before you commit! You forget to check the return
-   value of path_to_lba (-1 for error) in this way ... - LGB */
-//#define	CSSDVDAuthTitlePath(hdl,key_title,path) \
-//		CSSAuthTitle(hdl,key_title,path_to_lba(path))
-
-#else	/*OLD_CSS_API*/
-
-#define DVDHandle		struct dvd_device *
-#define	DVDOpenFailed		NULL
-
-#endif	/*OLD_CSS_API*/
+// #define	CSSDVDisEncrypted(hdl)	CSSisEncrypted(hdl)
+// #define	CSSDVDAuthDisc		CSSAuthDisc
 
 
 char *dvd_auth_device=NULL;
 char *dvd_device=NULL;
 char *dvd_raw_device=NULL;
+char *css_so=NULL;
 unsigned char key_disc[2048];
 unsigned char key_title[5];
 unsigned char *dvdimportkey=NULL;
 int descrambling=0;
 
+static void *dlid;
+static int (*dl_CSSisEncrypted)(int);
+static int (*dl_CSSAuthDisc)(int,char *);
+static int (*dl_CSSAuthTitle)(int, char *,int);
+static int (*dl_CSSGetASF)(int);
+static int (*dl_CSSDecryptTitleKey)(char *, char *);
+static void (*dl_CSSDescramble)(u_char *, u_char *);
+
+dvd_css_descramble ( u_char *sec , u_char *key )
+{
+	(*dl_CSSDescramble)(sec,key);
+}
+
 
-#if OLD_CSS_API
-/*
- * With the old libcss-0.1 api, we have to find out the LBA for
- * a title for title authentication.
- */
 #ifdef __linux__
 #include <linux/fs.h>
 
@@ -82,6 +90,7 @@ int descrambling=0;
 #define FIBMAP 1
 #endif
 
+
 static int path_to_lba (char *path)
 {
     int lba = 0;
@@ -94,51 +103,56 @@ static int path_to_lba (char *path)
 	    int ret;
 	    memset(cmd,0,sizeof(cmd));
 	    fgets(cmd,99,fp);
+//	    printf("DVD: cmd: %s\n",cmd);
 	    if ((ret=pclose(fp)))
-		    fprintf(stderr,"fibmap_mplayer: %s\n",*cmd?cmd:"no error info");
-	    if(WIFEXITED(ret) && !WEXITSTATUS(ret)) 
-		lba=atoi(cmd);
-	    else
-		fp=NULL;
+		    fprintf(stderr,"DVD: fibmap_mplayer: %s\n",*cmd?cmd:"no error info");
+	    if (cmd[0]<'0'||cmd[0]>'9') fp=NULL; else {
+		if(WIFEXITED(ret) && !WEXITSTATUS(ret)) {
+		    lba=atoi(cmd);
+		    printf("DVD: fibmap_mplayer is being used\n");
+		} else
+		    fp=NULL;
+	    }
     }
     if (!fp) {
 	int fd;
-	printf("fibmap_mplayer could not run, trying with ioctl() ...\n");
+	printf("DVD: fibmap_mplayer could not run, trying with ioctl() ...\n");
 	if ((fd = open(path, O_RDONLY)) == -1) {
-    	    fprintf(stderr, "Cannot open file %s: %s",
+    	    fprintf(stderr, "DVD: Cannot open file %s: %s",
 	    path ? path : "(NULL)", strerror(errno));
     	    return -1;
 	}
         if (ioctl(fd, FIBMAP, &lba) != 0) {
-            perror ("ioctl FIBMAP");
-	    fprintf(stderr,"Hint: run mplayer as root (or better to install fibmap_mplayer as suid root)!\n");
+            perror("DVD: ioctl FIBMAP");
+	    fprintf(stderr,"  Hint: run mplayer as root (or better to install fibmap_mplayer as suid root)!\n");
             close(fd);
             return -1;
         }
 	close(fd);
     }
-    printf("LBA: %d\n",lba);
+    printf("DVD: LBA: %d\n",lba);
     return lba;
 }
 
-
-int CSSDVDAuthTitlePath(DVDHandle hdl,unsigned char *key_title,char *path)
-{
-	int lba=path_to_lba(path);
-	if (lba==-1) return -1;
-	return CSSAuthTitle(hdl,key_title,lba);
-}		
-		
-
 #else /*linux*/
+
 static int path_to_lba (char *path)
 {
 #warning translating pathname to iso9660 LBA is not supported on this platform
-    fprintf(stderr, "Translating pathname to iso9660 LBA is not supported on this platform\n");
+    fprintf(stderr, "DVD: Translating pathname to iso9660 LBA is not supported on this platform\n");
     return -1;
 }
+
 #endif /*linux*/
-#endif /*OLD_CSS_API*/
+
+
+static int CSSDVDAuthTitlePath(DVDHandle hdl,unsigned char *key_title,char *path)
+{
+	int lba=path_to_lba(path);
+	if (lba==-1) return -1;
+	return (*dl_CSSAuthTitle)(hdl,key_title,lba);
+}		
+		
 
 
 static void reset_agids ( DVDHandle dvd )
@@ -173,7 +187,7 @@ int dvd_import_key ( unsigned char *hexkey )
 		hexkey++;
 	}
 	if (*hexkey) return 1;
-	printf("DVD key (requested): %02X%02X%02X%02X%02X\n",key_title[0],key_title[1],key_title[2],key_title[3],key_title[4]);
+	printf("DVD: DVD key (requested): %02X%02X%02X%02X%02X\n",key_title[0],key_title[1],key_title[2],key_title[3],key_title[4]);
 	descrambling=1;
 	return 0;
 }
@@ -184,28 +198,46 @@ int dvd_auth ( char *dev , char *filename )
 {
     	DVDHandle dvd;  /* DVD device handle */
 
+	if (!css_so) css_so=strdup("/usr/local/lib/libcss.so");
+	printf("DVD: opening libcss.so as %s ...\n",css_so);
+	dlid=dlopen(css_so,RTLD_NOW);
+	if (!dlid) {
+		printf("DVD: dlopen: %s\n",dlerror());
+		return 1;
+	} printf("DVD: dlopen OK!\n");
+
+#define CSS_DLSYM(v,s) if (!(v=dlsym(dlid,s))) {\
+fprintf(stderr,"DVD: %s\n  Hint: use libcss version 0.1!\n",dlerror());\
+return 1; }
+
+	CSS_DLSYM(dl_CSSisEncrypted,"CSSisEncrypted");
+	CSS_DLSYM(dl_CSSAuthDisc,"CSSAuthDisc");
+	CSS_DLSYM(dl_CSSAuthTitle,"CSSAuthTitle");
+	CSS_DLSYM(dl_CSSGetASF,"CSSGetASF");
+	CSS_DLSYM(dl_CSSDecryptTitleKey,"CSSDecryptTitleKey");
+	CSS_DLSYM(dl_CSSDescramble,"CSSDescramble");
+
+#undef CSS_DLSYM
+
 	if ((dvd=DVDOpenDevice(dev)) == DVDOpenFailed) {
 		fprintf(stderr,"DVD: cannot open DVD device \"%s\": %s.\n",
 			dev, strerror(errno));
 		return 1;
 	}
-	
-	if (!CSSDVDisEncrypted(dvd)) {
-		printf("DVD is unencrypted! Skipping authentication!\n(note: you should not use -dvd switch for unencrypted discs!)\n");
+
+	if (!(*dl_CSSisEncrypted)(dvd)) {
+		printf("DVD: DVD is unencrypted! Skipping authentication!\n(note: you should not use -dvd switch for unencrypted discs!)\n");
 		DVDCloseDevice(dvd);
 		return 0;
-	} else printf("DVD is encrypted, issuing authentication ...\n");
-
+	} else printf("DVD: DVD is encrypted, issuing authentication ...\n");
 	/* reset AGIDs */
 	reset_agids(dvd);
-
 	/* authenticate disc */
-	if (CSSDVDAuthDisc(dvd,key_disc)) {
+	if ((*dl_CSSAuthDisc)(dvd,key_disc)) {
 		fprintf(stderr,"DVD: CSSDVDAuthDisc() failed.\n");
 		DVDCloseDevice(dvd);
 		return 1;
 	}
-
         if (CSSDVDAuthTitlePath(dvd,key_title,filename)) {
 		fprintf(stderr,"DVD: CSSDVDAuthTitle() failed.\n");
 		DVDCloseDevice(dvd);
@@ -213,14 +245,14 @@ int dvd_auth ( char *dev , char *filename )
 	}
 
 	/* decrypting title */
-        if (CSSDecryptTitleKey (key_title, key_disc) < 0) {
+        if ((*dl_CSSDecryptTitleKey)(key_title, key_disc) < 0) {
                 fprintf(stderr,"DVD: CSSDecryptTitleKey() failed.\n");
 		DVDCloseDevice(dvd);
 		return 1;
 	}
 
 	DVDCloseDevice(dvd);
-	printf("DVD title key is: %02X%02X%02X%02X%02X\n",key_title[0],key_title[1],key_title[2],key_title[3],key_title[4]);
+	printf("DVD: DVD title key is: %02X%02X%02X%02X%02X\n",key_title[0],key_title[1],key_title[2],key_title[3],key_title[4]);
 	descrambling=1;
 	return 0;
 }
diff --git a/libmpdemux/dvdauth.h b/libmpdemux/dvdauth.h
index 8672ce17af..e6da3a1dc5 100644
--- a/libmpdemux/dvdauth.h
+++ b/libmpdemux/dvdauth.h
@@ -7,9 +7,11 @@ extern unsigned char key_disc[];
 extern unsigned char key_title[];
 extern unsigned char *dvdimportkey;
 extern int descrambling;
+extern char *css_so;
 
 int dvd_auth ( char *, char * );
 int dvd_import_key ( unsigned char * );
+int dvd_css_descramble ( u_char *, u_char * );
 
 #endif
 #endif