$OpenBSD: patch-src_css_c,v 1.6 2020/03/21 18:58:07 kmos Exp $

Index: src/css.c
--- src/css.c.orig
+++ src/css.c
@@ -231,10 +231,13 @@ int dvdcss_title ( dvdcss_t dvdcss, int i_block )
         {
             char psz_key[PSZ_KEY_SIZE];
             unsigned int k0, k1, k2, k3, k4;
+            int n;
 
-            psz_key[PSZ_KEY_SIZE - 1] = '\0';
+            memset(psz_key, 0, sizeof psz_key);
+            n = read( i_fd, psz_key, PSZ_KEY_SIZE - 1 );
+            close( i_fd );
 
-            if( read( i_fd, psz_key, PSZ_KEY_SIZE - 1 ) == PSZ_KEY_SIZE - 1
+            if( n == PSZ_KEY_SIZE - 1
                  && sscanf( psz_key, "%x:%x:%x:%x:%x",
                             &k0, &k1, &k2, &k3, &k4 ) == 5 )
             {
@@ -248,9 +251,12 @@ int dvdcss_title ( dvdcss_t dvdcss, int i_block )
                 /* Don't try to save it again */
                 b_cache = 0;
                 i_ret = 1;
+            } 
+            else if (n > 0 && strcmp(psz_key, PACKAGE_VERSION "\n") == 0) 
+            {
+                /* didn't crack it, negative caching */
+                return -1;
             }
-
-            close( i_fd );
         }
     }
 
@@ -264,6 +270,15 @@ int dvdcss_title ( dvdcss_t dvdcss, int i_block )
         {
             print_error( dvdcss, "fatal error in Video Title Set (VTS) "
                                  "Content Scrambling System (CSS) key" );
+            i_fd = open( dvdcss->psz_cachefile, O_RDWR|O_CREAT, 0644 );
+            if( i_fd >= 0 )
+            {
+                char message[sizeof(PACKAGE_VERSION) + 3];
+
+                sprintf( message, "%s\n", PACKAGE_VERSION);
+                write( i_fd, message, strlen(message) );
+                close( i_fd );
+            }
             return i_ret;
         }
 
