changeset 7032:fd2d4be9ed6f

applying css-key cache patch
author arpi
date Fri, 16 Aug 2002 22:46:01 +0000
parents a1ff955aa49b
children 596919e4f601
files libmpdvdkit2/css.c libmpdvdkit2/libdvdcss.c libmpdvdkit2/libdvdcss.h
diffstat 3 files changed, 79 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/libmpdvdkit2/css.c	Fri Aug 16 22:43:44 2002 +0000
+++ b/libmpdvdkit2/css.c	Fri Aug 16 22:46:01 2002 +0000
@@ -38,6 +38,9 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
 
 #include "dvdcss/dvdcss.h"
 
@@ -269,7 +272,8 @@
     dvd_title_t *p_title;
     dvd_title_t *p_newtitle;
     dvd_key_t    p_title_key;
-    int          i_ret;
+    int          i_ret=-1;
+    char* key_file=NULL;
 
     if( ! dvdcss->b_scrambled )
     {
@@ -293,8 +297,24 @@
         return 0;
     }
 
+    /* check teh CSS Key cache, if available: */
+    if(dvdcss->psz_cache){
+	int fd;
+	key_file=malloc(strlen(dvdcss->psz_cache)+12+4);
+	sprintf(key_file,"%s/%0.10x",dvdcss->psz_cache,i_block);
+	if ( (fd=open( key_file,O_RDONLY ) ) > 0 ){
+	    if(read(fd, p_title_key, 5)==5){
+		// success!
+		free(key_file); key_file=NULL;
+		i_ret=1;
+		_dvdcss_debug( dvdcss, "key found in cache" );
+	    }
+	    close(fd);
+	}
+    }
+
     /* Crack or decrypt CSS title key for current VTS */
-    i_ret = _dvdcss_titlekey( dvdcss, i_block, p_title_key );
+    if(i_ret<0) i_ret = _dvdcss_titlekey( dvdcss, i_block, p_title_key );
 
     if( i_ret < 0 )
     {
@@ -307,6 +327,16 @@
         /* Still store this in the cache, so we don't need to check again. */
     }
 
+    /* store in key-cache */
+    if(key_file){
+	int fd;
+	if ( (fd=open( key_file,O_RDWR|O_CREAT|O_EXCL,0644 ) ) > 0 ){
+	    write(fd, p_title_key, 5);
+	    close(fd);
+	}
+	free(key_file);
+    }
+
     /* Find our spot in the list */
     p_newtitle = NULL;
     p_title = dvdcss->p_titles;
@@ -1437,7 +1467,7 @@
         return 1;
     }
 
-    if( i_encrypted == 0 )
+    if( i_encrypted == 0 && i_reads>0 )
     {
         memset( p_titlekey, 0, KEY_SIZE );
         _dvdcss_debug( dvdcss, "file was unscrambled" );
--- a/libmpdvdkit2/libdvdcss.c	Fri Aug 16 22:43:44 2002 +0000
+++ b/libmpdvdkit2/libdvdcss.c	Fri Aug 16 22:46:01 2002 +0000
@@ -117,6 +117,8 @@
  */
 char * dvdcss_interface_2 = VERSION;
 
+char * dvdcss_cache_dir = NULL;
+
 /**
  * \brief Open a DVD device or directory and return a dvdcss instance.
  *
@@ -164,6 +166,7 @@
     dvdcss->i_method = DVDCSS_METHOD_KEY;
     dvdcss->b_debug = 0;
     dvdcss->b_errors = 0;
+    dvdcss->psz_cache = NULL;
 
     /*
      *  Find verbosity from DVDCSS_VERBOSE environment variable
@@ -208,6 +211,8 @@
         }
     }
 
+    if(!dvdcss_cache_dir) dvdcss_cache_dir = getenv( "DVDCSS_CACHE" );
+
     /*
      *  Open device
      */
@@ -261,6 +266,46 @@
     }
 #endif
 
+    /* if the CACHE is enabled, extract some unique disc ID */
+    if(dvdcss_cache_dir){
+	char* disc_id=NULL;
+	char title_name[64];
+	char sector[DVDCSS_BLOCK_SIZE];
+	// 32768+40  -> disc title (32 uppercase chars)
+	// 32768+813 -> disc manufacturing date + serial no (16 digit number)
+	_dvdcss_seek( dvdcss, 32768/DVDCSS_BLOCK_SIZE);
+	if(_dvdcss_read( dvdcss, sector, 1) == 1){
+	    // check disc title first:
+	    char* title_name=&sector[40];
+	    int i=31;
+	    while(i>=0 && title_name[i]<=32) i--;
+	    title_name[i+1]=0;
+	    if(strlen(title_name)>5){
+		disc_id=strdup(title_name);
+	    } else {
+		// use disc date+serial:
+		title_name=&sector[813];
+		title_name[16]=0;
+	        for ( i=0;i<16;i++ )
+		    if ( ( title_name[i] < '0' )||( title_name[i] > '9' ) ){
+		        disc_id=malloc(16+4);
+	                sprintf( disc_id,"%0.2X%0.2X%0.2X%0.2X%0.2X%0.2X%0.2X%0.2X",title_name[0],title_name[1],title_name[2],title_name[3],title_name[4],title_name[5],title_name[6],title_name[7] );
+			break;
+		    }
+		if(!disc_id) disc_id=strdup(title_name);
+	    }
+	    if(disc_id){
+		// yeah, we have a disc name/id, let's set up cache path:
+		char* dir;
+		dvdcss->psz_cache = malloc(strlen(dvdcss_cache_dir)+strlen(disc_id)+4);
+		sprintf(dvdcss->psz_cache,"%s/%s",dvdcss_cache_dir,disc_id);
+		mkdir( dvdcss->psz_cache,493 );
+		free(disc_id);
+		fprintf(stderr,"Using CSS Key-cache dir: %s\n",dvdcss->psz_cache);
+	    }
+	}
+    }
+
     return dvdcss;
 }
 
--- a/libmpdvdkit2/libdvdcss.h	Fri Aug 16 22:43:44 2002 +0000
+++ b/libmpdvdkit2/libdvdcss.h	Fri Aug 16 22:46:01 2002 +0000
@@ -38,6 +38,7 @@
     int          b_ioctls;
     int          b_scrambled;
     dvd_title_t *p_titles;
+    char *	 psz_cache;
 
     /* Error management */
     char * psz_error;