1 /*****************************************************************************
2 * libdvdcss.c: DVD reading library.
3 *****************************************************************************
4 * Copyright (C) 1998-2001 VideoLAN
5 * $Id: libdvdcss.c,v 1.16 2001/10/13 15:34:21 stef Exp $
7 * Authors: Stéphane Borel <stef@via.ecp.fr>
8 * Samuel Hocevar <sam@zoy.org>
9 * Håkan Hjort <d95hjort@dtek.chalmers.se>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
34 #include <sys/types.h>
43 # include <io.h> /* read() */
45 # include <sys/uio.h> /* struct iovec */
52 # include "input_iovec.h"
55 #include "videolan/dvdcss.h"
56 #include "libdvdcss.h"
59 /*****************************************************************************
61 *****************************************************************************/
62 static int _dvdcss_open ( dvdcss_handle, char *psz_target );
63 static int _dvdcss_close ( dvdcss_handle );
64 static int _dvdcss_seek ( dvdcss_handle, int i_blocks );
65 static int _dvdcss_read ( dvdcss_handle, void *p_buffer, int i_blocks );
66 static int _dvdcss_readv ( dvdcss_handle, struct iovec *p_iovec, int i_blocks );
68 /*****************************************************************************
69 * Local prototypes, win32 specific
70 *****************************************************************************/
72 static int _win32_dvdcss_readv ( int i_fd, struct iovec *p_iovec,
73 int i_num_buffers, char *p_tmp_buffer );
74 static int _win32_dvdcss_aopen ( char c_drive, dvdcss_handle dvdcss );
75 static int _win32_dvdcss_aclose ( int i_fd );
76 static int _win32_dvdcss_aseek ( int i_fd, int i_blocks, int i_method );
77 static int _win32_dvdcss_aread ( int i_fd, void *p_data, int i_blocks );
80 /*****************************************************************************
81 * dvdcss_open: initialize library, open a DVD device, crack CSS key
82 *****************************************************************************/
83 extern dvdcss_handle dvdcss_open ( char *psz_target, int i_method, int i_flags )
89 /* Allocate the library structure */
90 dvdcss = malloc( sizeof( struct dvdcss_s ) );
93 if( ! (i_flags & DVDCSS_INIT_QUIET) )
95 DVDCSS_ERROR( "could not initialize library" );
101 /* Initialize structure */
102 dvdcss->p_titles = NULL;
103 dvdcss->b_debug = i_flags & DVDCSS_INIT_DEBUG;
104 dvdcss->b_errors = !(i_flags & DVDCSS_INIT_QUIET);
105 dvdcss->i_method = i_method;
106 dvdcss->psz_error = "no error";
108 i_ret = _dvdcss_open( dvdcss, psz_target );
115 i_ret = CSSTest( dvdcss );
118 _dvdcss_error( dvdcss, "CSS test failed" );
119 /* Disable the CSS ioctls and hope that it works? */
120 dvdcss->b_ioctls = 0;
121 dvdcss->b_encrypted = 1;
125 dvdcss->b_ioctls = 1;
126 dvdcss->b_encrypted = i_ret;
129 /* If disc is CSS protected and the ioctls work, authenticate the drive */
130 if( dvdcss->b_encrypted && dvdcss->b_ioctls )
132 i_ret = CSSGetDiscKey( dvdcss );
136 _dvdcss_close( dvdcss );
145 /*****************************************************************************
146 * dvdcss_error: return the last libdvdcss error message
147 *****************************************************************************/
148 extern char * dvdcss_error ( dvdcss_handle dvdcss )
150 return dvdcss->psz_error;
153 /*****************************************************************************
154 * dvdcss_seek: seek into the device
155 *****************************************************************************/
156 extern int dvdcss_seek ( dvdcss_handle dvdcss, int i_blocks, int i_flags )
158 /* title cracking method is too slow to be used at each seek */
159 if( ( ( i_flags & DVDCSS_SEEK_MPEG ) && ( dvdcss->i_method != DVDCSS_TITLE ) )
160 || ( i_flags & DVDCSS_SEEK_INI ) )
162 /* check the title key */
163 dvdcss_title( dvdcss, i_blocks );
166 return _dvdcss_seek( dvdcss, i_blocks );
169 /*****************************************************************************
170 * dvdcss_title: crack or decrypt the current title key if needed
171 *****************************************************************************
172 * This function should only be called by dvdcss_seek and should eventually
173 * not be external if possible.
174 *****************************************************************************/
175 extern int dvdcss_title ( dvdcss_handle dvdcss, int i_block )
177 dvd_title_t *p_title;
178 dvd_title_t *p_newtitle;
181 if( ! dvdcss->b_encrypted )
186 /* Check if we've already cracked this key */
187 p_title = dvdcss->p_titles;
188 while( p_title != NULL
189 && p_title->p_next != NULL
190 && p_title->p_next->i_startlb <= i_block )
192 p_title = p_title->p_next;
196 && p_title->i_startlb == i_block )
198 /* We've already cracked this key, nothing to do */
199 memcpy( dvdcss->css.p_title_key, p_title->p_key, sizeof(dvd_key_t) );
203 /* Crack or decrypt CSS title key for current VTS */
204 i_ret = CSSGetTitleKey( dvdcss, i_block );
208 _dvdcss_error( dvdcss, "fatal error in vts css key" );
213 _dvdcss_error( dvdcss, "decryption unavailable" );
217 /* Find our spot in the list */
219 p_title = dvdcss->p_titles;
220 while( ( p_title != NULL ) && ( p_title->i_startlb < i_block ) )
222 p_newtitle = p_title;
223 p_title = p_title->p_next;
226 /* Save the found title */
227 p_title = p_newtitle;
229 /* Write in the new title and its key */
230 p_newtitle = malloc( sizeof( dvd_title_t ) );
231 p_newtitle->i_startlb = i_block;
232 memcpy( p_newtitle->p_key, dvdcss->css.p_title_key, KEY_SIZE );
234 /* Link the new title, either at the beginning or inside the list */
235 if( p_title == NULL )
237 dvdcss->p_titles = p_newtitle;
238 p_newtitle->p_next = NULL;
242 p_newtitle->p_next = p_title->p_next;
243 p_title->p_next = p_newtitle;
249 #define Pkey dvdcss->css.p_title_key
250 /*****************************************************************************
251 * dvdcss_read: read data from the device, decrypt if requested
252 *****************************************************************************/
253 extern int dvdcss_read ( dvdcss_handle dvdcss, void *p_buffer,
259 i_ret = _dvdcss_read( dvdcss, p_buffer, i_blocks );
262 || !dvdcss->b_encrypted
263 || !(i_flags & DVDCSS_READ_DECRYPT) )
268 /* For what we believe is an unencrypted title,
269 check that there are no encrypted blocks */
270 if( !( Pkey[0] | Pkey[1] | Pkey[2] | Pkey[3] | Pkey[4] ) )
272 for( i_index = i_ret; i_index; i_index-- )
274 if( ((u8*)p_buffer)[0x14] & 0x30 )
276 _dvdcss_error( dvdcss, "no key but found encrypted block" );
277 /* Only return the initial range of unscrambled blocks? */
279 /* or fail completely? return 0; */
281 (u8*)p_buffer += DVDCSS_BLOCK_SIZE;
285 /* Decrypt the blocks we managed to read */
286 for( i_index = i_ret; i_index; i_index-- )
288 CSSDescrambleSector( dvdcss->css.p_title_key, p_buffer );
289 ((u8*)p_buffer)[0x14] &= 0x8f;
290 (u8*)p_buffer += DVDCSS_BLOCK_SIZE;
296 /*****************************************************************************
297 * dvdcss_readv: read data to an iovec structure, decrypt if requested
298 *****************************************************************************/
299 extern int dvdcss_readv ( dvdcss_handle dvdcss, void *p_iovec,
303 #define P_IOVEC ((struct iovec*)p_iovec)
308 i_ret = _dvdcss_readv( dvdcss, P_IOVEC, i_blocks );
311 || !dvdcss->b_encrypted
312 || !(i_flags & DVDCSS_READ_DECRYPT) )
318 /* Initialize loop for decryption */
319 iov_base = P_IOVEC->iov_base;
320 iov_len = P_IOVEC->iov_len;
322 /* Decrypt the blocks we managed to read */
323 for( i_index = i_ret; i_index; i_index-- )
325 /* Check that iov_len is a multiple of 2048 */
326 if( iov_len & 0x7ff )
331 while( iov_len == 0 )
334 iov_base = P_IOVEC->iov_base;
335 iov_len = P_IOVEC->iov_len;
338 CSSDescrambleSector( dvdcss->css.p_title_key, iov_base );
339 ((u8*)iov_base)[0x14] &= 0x8f;
341 (u8*)iov_base += DVDCSS_BLOCK_SIZE;
342 (u8*)iov_len -= DVDCSS_BLOCK_SIZE;
350 /*****************************************************************************
351 * dvdcss_close: close the DVD device and clean up the library
352 *****************************************************************************/
353 extern int dvdcss_close ( dvdcss_handle dvdcss )
355 dvd_title_t *p_title;
358 /* Free our list of keys */
359 p_title = dvdcss->p_titles;
362 dvd_title_t *p_tmptitle = p_title->p_next;
364 p_title = p_tmptitle;
367 i_ret = _dvdcss_close( dvdcss );
379 /* Following functions are local */
381 static int _dvdcss_open ( dvdcss_handle dvdcss, char *psz_target )
387 _snprintf( psz_dvd, 7, "\\\\.\\%c:", psz_target[0] );
388 (HANDLE) dvdcss->i_fd =
389 CreateFile( psz_dvd, GENERIC_READ | GENERIC_WRITE,
390 FILE_SHARE_READ | FILE_SHARE_WRITE,
391 NULL, OPEN_EXISTING, 0, NULL );
392 if( (HANDLE) dvdcss->i_fd == INVALID_HANDLE_VALUE )
394 _dvdcss_error( dvdcss, "failed opening device" );
400 dvdcss->i_fd = _win32_dvdcss_aopen( psz_target[0], dvdcss );
401 if( dvdcss->i_fd == -1 )
403 _dvdcss_error( dvdcss, "failed opening device" );
408 /* initialise readv temporary buffer */
409 dvdcss->p_readv_buffer = NULL;
410 dvdcss->i_readv_buf_size = 0;
413 dvdcss->i_fd = open( psz_target, 0 );
415 if( dvdcss->i_fd == -1 )
417 _dvdcss_error( dvdcss, "failed opening device" );
426 static int _dvdcss_close ( dvdcss_handle dvdcss )
431 CloseHandle( (HANDLE) dvdcss->i_fd );
435 _win32_dvdcss_aclose( dvdcss->i_fd );
438 /* Free readv temporary buffer */
439 if( dvdcss->p_readv_buffer )
441 free( dvdcss->p_readv_buffer );
442 dvdcss->p_readv_buffer = NULL;
443 dvdcss->i_readv_buf_size = 0;
447 close( dvdcss->i_fd );
454 static int _dvdcss_seek ( dvdcss_handle dvdcss, int i_blocks )
457 dvdcss->i_seekpos = i_blocks;
461 LARGE_INTEGER li_read;
463 #ifndef INVALID_SET_FILE_POINTER
464 #define INVALID_SET_FILE_POINTER ((DWORD)-1)
467 li_read.QuadPart = (LONGLONG)i_blocks * DVDCSS_BLOCK_SIZE;
469 li_read.LowPart = SetFilePointer( (HANDLE) dvdcss->i_fd,
471 &li_read.HighPart, FILE_BEGIN );
472 if( (li_read.LowPart == INVALID_SET_FILE_POINTER)
473 && GetLastError() != NO_ERROR)
475 li_read.QuadPart = -DVDCSS_BLOCK_SIZE;
478 li_read.QuadPart /= DVDCSS_BLOCK_SIZE;
479 return (int)li_read.QuadPart;
483 return ( _win32_dvdcss_aseek( dvdcss->i_fd, i_blocks, SEEK_SET ) );
488 dvdcss->i_seekpos = i_blocks;
490 i_read = lseek( dvdcss->i_fd,
491 (off_t)i_blocks * (off_t)DVDCSS_BLOCK_SIZE, SEEK_SET );
493 return i_read / DVDCSS_BLOCK_SIZE;
498 static int _dvdcss_read ( dvdcss_handle dvdcss, void *p_buffer, int i_blocks )
505 if( !ReadFile( (HANDLE) dvdcss->i_fd, p_buffer,
506 i_blocks * DVDCSS_BLOCK_SIZE,
507 (LPDWORD)&i_bytes, NULL ) )
511 return i_bytes / DVDCSS_BLOCK_SIZE;
515 return _win32_dvdcss_aread( dvdcss->i_fd, p_buffer, i_blocks );
521 i_bytes = read( dvdcss->i_fd, p_buffer,
522 (size_t)i_blocks * DVDCSS_BLOCK_SIZE );
523 return i_bytes / DVDCSS_BLOCK_SIZE;
528 static int _dvdcss_readv ( dvdcss_handle dvdcss, struct iovec *p_iovec,
534 /* Check the size of the readv temp buffer, just in case we need to
535 * realloc something bigger */
536 if( dvdcss->i_readv_buf_size < i_blocks * DVDCSS_BLOCK_SIZE )
538 dvdcss->i_readv_buf_size = i_blocks * DVDCSS_BLOCK_SIZE;
540 if( dvdcss->p_readv_buffer ) free( dvdcss->p_readv_buffer );
542 /* Allocate a buffer which will be used as a temporary storage
544 dvdcss->p_readv_buffer = malloc( dvdcss->i_readv_buf_size );
545 if( !dvdcss->p_readv_buffer )
547 _dvdcss_error( dvdcss, " failed (readv)" );
552 i_read = _win32_dvdcss_readv( dvdcss->i_fd, p_iovec, i_blocks,
553 dvdcss->p_readv_buffer );
557 i_read = readv( dvdcss->i_fd, p_iovec, i_blocks );
558 return i_read / DVDCSS_BLOCK_SIZE;
566 /*****************************************************************************
567 * _win32_dvdcss_readv: vectored read using ReadFile for Win2K and
568 * _win32_dvdcss_aread for win9x
569 *****************************************************************************/
570 static int _win32_dvdcss_readv( int i_fd, struct iovec *p_iovec,
571 int i_num_buffers, char *p_tmp_buffer )
574 int i_blocks, i_blocks_total = 0;
576 for( i_index = i_num_buffers; i_index; i_index-- )
578 i_blocks_total += p_iovec[i_index-1].iov_len;
581 if( i_blocks_total <= 0 ) return 0;
583 i_blocks_total /= DVDCSS_BLOCK_SIZE;
587 unsigned long int i_bytes;
588 if( !ReadFile( (HANDLE)i_fd, p_tmp_buffer,
589 i_blocks_total * DVDCSS_BLOCK_SIZE, &i_bytes, NULL ) )
592 /* The read failed... too bad.
593 As in the posix spec the file postition is left
594 unspecified after a failure */
596 i_blocks = i_bytes / DVDCSS_BLOCK_SIZE;
600 i_blocks = _win32_dvdcss_aread( i_fd, p_tmp_buffer, i_blocks_total );
603 return -1; /* idem */
607 /* We just have to copy the content of the temp buffer into the iovecs */
609 i_blocks_total = i_blocks;
610 while( i_blocks_total > 0 )
612 memcpy( p_iovec[i_index].iov_base,
613 &p_tmp_buffer[(i_blocks - i_blocks_total) * DVDCSS_BLOCK_SIZE],
614 p_iovec[i_index].iov_len );
615 /* if we read less blocks than asked, we'll just end up copying
616 garbage, this isn't an issue as we return the number of
617 blocks actually read */
618 i_blocks_total -= ( p_iovec[i_index].iov_len / DVDCSS_BLOCK_SIZE );
625 /*****************************************************************************
626 * _win32_dvdcss_aopen: open dvd drive (load aspi and init w32_aspidev
628 *****************************************************************************/
629 static int _win32_dvdcss_aopen( char c_drive, dvdcss_handle dvdcss )
633 struct w32_aspidev *fd;
634 int i, j, i_hostadapters;
635 long (*lpGetSupport)( void );
636 long (*lpSendCommand)( void* );
638 hASPI = LoadLibrary( "wnaspi32.dll" );
641 _dvdcss_error( dvdcss, "unable to load wnaspi32.dll" );
645 (FARPROC) lpGetSupport = GetProcAddress( hASPI, "GetASPI32SupportInfo" );
646 (FARPROC) lpSendCommand = GetProcAddress( hASPI, "SendASPI32Command" );
648 if(lpGetSupport == NULL || lpSendCommand == NULL )
650 _dvdcss_debug( dvdcss, "unable to get aspi function pointers" );
651 FreeLibrary( hASPI );
655 dwSupportInfo = lpGetSupport();
657 if( HIBYTE( LOWORD ( dwSupportInfo ) ) == SS_NO_ADAPTERS )
659 _dvdcss_debug( dvdcss, "no host adapters found (aspi)" );
660 FreeLibrary( hASPI );
664 if( HIBYTE( LOWORD ( dwSupportInfo ) ) != SS_COMP )
666 _dvdcss_error( dvdcss, "unable to initalize aspi layer" );
667 FreeLibrary( hASPI );
671 i_hostadapters = LOBYTE( LOWORD( dwSupportInfo ) );
672 if( i_hostadapters == 0 )
674 FreeLibrary( hASPI );
678 fd = malloc( sizeof( struct w32_aspidev ) );
681 FreeLibrary( hASPI );
686 fd->hASPI = (long) hASPI;
687 fd->lpSendCommand = lpSendCommand;
689 c_drive = c_drive > 'Z' ? c_drive - 'a' : c_drive - 'A';
691 for( i = 0; i < i_hostadapters; i++ )
693 for( j = 0; j < 15; j++ )
695 struct SRB_GetDiskInfo srbDiskInfo;
697 srbDiskInfo.SRB_Cmd = SC_GET_DISK_INFO;
698 srbDiskInfo.SRB_HaId = i;
699 srbDiskInfo.SRB_Flags = 0;
700 srbDiskInfo.SRB_Hdr_Rsvd = 0;
701 srbDiskInfo.SRB_Target = j;
702 srbDiskInfo.SRB_Lun = 0;
704 lpSendCommand( (void*) &srbDiskInfo );
706 if( (srbDiskInfo.SRB_Status == SS_COMP) &&
707 (srbDiskInfo.SRB_Int13HDriveInfo == c_drive) )
709 fd->i_sid = MAKEWORD( i, j );
716 FreeLibrary( hASPI );
717 _dvdcss_debug( dvdcss, "unable to get haid and target (aspi)" );
721 /*****************************************************************************
722 * _win32_dvdcss_aclose: close dvd drive (unload aspi and free w32_aspidev
724 *****************************************************************************/
725 static int _win32_dvdcss_aclose( int i_fd )
727 struct w32_aspidev *fd = (struct w32_aspidev *) i_fd;
729 FreeLibrary( (HMODULE) fd->hASPI );
730 free( (void*) i_fd );
735 /*****************************************************************************
736 * _win32_dvdcss_aseek: aspi version of _dvdcss_seek
738 * returns the number of blocks read.
739 *****************************************************************************/
740 static int _win32_dvdcss_aseek( int i_fd, int i_blocks, int i_method )
743 char sz_buf[ DVDCSS_BLOCK_SIZE ];
744 struct w32_aspidev *fd = (struct w32_aspidev *) i_fd;
746 i_old_blocks = fd->i_blocks;
747 fd->i_blocks = i_blocks;
749 if( _win32_dvdcss_aread( i_fd, sz_buf, 1 ) == -1 )
751 fd->i_blocks = i_old_blocks;
760 /*****************************************************************************
761 * _win32_dvdcss_aread: aspi version of _dvdcss_read
763 * returns the number of blocks read.
764 *****************************************************************************/
765 static int _win32_dvdcss_aread( int i_fd, void *p_data, int i_blocks )
768 struct SRB_ExecSCSICmd ssc;
769 struct w32_aspidev *fd = (struct w32_aspidev *) i_fd;
771 /* Create the transfer completion event */
772 hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
778 memset( &ssc, 0, sizeof( ssc ) );
780 ssc.SRB_Cmd = SC_EXEC_SCSI_CMD;
781 ssc.SRB_Flags = SRB_DIR_IN | SRB_EVENT_NOTIFY;
782 ssc.SRB_HaId = LOBYTE( fd->i_sid );
783 ssc.SRB_Target = HIBYTE( fd->i_sid );
784 ssc.SRB_SenseLen = SENSE_LEN;
786 ssc.SRB_PostProc = (LPVOID) hEvent;
787 ssc.SRB_BufPointer = p_data;
790 ssc.CDBByte[0] = 0xA8; /* RAW */
791 ssc.CDBByte[2] = (UCHAR) (fd->i_blocks >> 24);
792 ssc.CDBByte[3] = (UCHAR) (fd->i_blocks >> 16) & 0xff;
793 ssc.CDBByte[4] = (UCHAR) (fd->i_blocks >> 8) & 0xff;
794 ssc.CDBByte[5] = (UCHAR) (fd->i_blocks) & 0xff;
796 /* We have to break down the reads into 64kb pieces (ASPI restriction) */
799 ssc.SRB_BufLen = 32 * DVDCSS_BLOCK_SIZE;
803 /* Initiate transfer */
804 ResetEvent( hEvent );
805 fd->lpSendCommand( (void*) &ssc );
807 /* transfer the next 64kb (_win32_dvdcss_aread is called recursively)
808 * We need to check the status of the read on return */
809 if( _win32_dvdcss_aread( i_fd, (u8*) p_data + 32 * DVDCSS_BLOCK_SIZE,
817 /* This is the last transfer */
818 ssc.SRB_BufLen = i_blocks * DVDCSS_BLOCK_SIZE;
819 ssc.CDBByte[9] = (UCHAR) i_blocks;
820 fd->i_blocks += i_blocks;
822 /* Initiate transfer */
823 ResetEvent( hEvent );
824 fd->lpSendCommand( (void*) &ssc );
828 /* If the command has still not been processed, wait until it's finished */
829 if( ssc.SRB_Status == SS_PENDING )
831 WaitForSingleObject( hEvent, INFINITE );
833 CloseHandle( hEvent );
835 /* check that the transfer went as planned */
836 if( ssc.SRB_Status != SS_COMP )