1 /*****************************************************************************
2 * libdvdcss.c: DVD reading library.
3 *****************************************************************************
4 * Copyright (C) 1998-2001 VideoLAN
5 * $Id: libdvdcss.c,v 1.30 2002/01/23 03:15:31 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 *****************************************************************************/
32 #include <sys/types.h>
36 #include <videolan/vlc.h>
43 # include <io.h> /* read() */
45 # include <sys/uio.h> /* struct iovec */
49 # include "input_iovec.h"
52 #include "videolan/dvdcss.h"
53 #include "libdvdcss.h"
56 /*****************************************************************************
58 *****************************************************************************/
59 static int _dvdcss_open ( dvdcss_handle, char *psz_target );
60 static int _dvdcss_close ( dvdcss_handle );
61 static int _dvdcss_readv ( dvdcss_handle, struct iovec *p_iovec, int i_blocks );
63 /*****************************************************************************
64 * Local prototypes, win32 specific
65 *****************************************************************************/
67 static int _win32_dvdcss_readv ( int i_fd, struct iovec *p_iovec,
68 int i_num_buffers, char *p_tmp_buffer );
69 static int _win32_dvdcss_aopen ( char c_drive, dvdcss_handle dvdcss );
70 static int _win32_dvdcss_aclose ( int i_fd );
71 static int _win32_dvdcss_aseek ( int i_fd, int i_blocks, int i_method );
72 static int _win32_dvdcss_aread ( int i_fd, void *p_data, int i_blocks );
74 static int _dvdcss_raw_open ( dvdcss_handle, char *psz_target );
77 /*****************************************************************************
78 * dvdcss_open: initialize library, open a DVD device, crack CSS key
79 *****************************************************************************/
80 extern dvdcss_handle dvdcss_open ( char *psz_target )
85 char *psz_method = getenv( "DVDCSS_METHOD" );
86 char *psz_verbose = getenv( "DVDCSS_VERBOSE" );
88 char *psz_raw_device = getenv( "DVDCSS_RAW_DEVICE" );
93 /* Allocate the library structure */
94 dvdcss = malloc( sizeof( struct dvdcss_s ) );
100 /* Initialize structure with default values */
101 dvdcss->p_titles = NULL;
102 dvdcss->psz_error = "no error";
103 dvdcss->i_method = DVDCSS_METHOD_TITLE;
105 dvdcss->b_errors = 1;
107 /* Find method from DVDCSS_METHOD environment variable */
108 if( psz_method != NULL )
110 if( !strncmp( psz_method, "key", 4 ) )
112 dvdcss->i_method = DVDCSS_METHOD_KEY;
114 else if( !strncmp( psz_method, "disc", 5 ) )
116 dvdcss->i_method = DVDCSS_METHOD_DISC;
118 else if( !strncmp( psz_method, "title", 5 ) )
120 dvdcss->i_method = DVDCSS_METHOD_TITLE;
124 _dvdcss_error( dvdcss, "unknown decrypt method, please choose "
125 "from 'title', 'key' or 'disc'" );
131 /* Find verbosity from DVDCSS_VERBOSE environment variable */
132 if( psz_verbose != NULL )
134 switch( atoi( psz_verbose ) )
137 dvdcss->b_errors = 0;
145 _dvdcss_error( dvdcss, "unknown verbose level, please choose "
146 "from '0', '1' or '2'" );
154 i_ret = _dvdcss_open( dvdcss, psz_target );
162 /* it's not possible to stat a drive letter. Fake a block device */
163 fileinfo.st_mode = S_IFBLK;
165 if( stat( psz_target, &fileinfo ) < 0 )
167 _dvdcss_error( dvdcss, "dvdcss: can't stat target" );
171 if( S_ISBLK( fileinfo.st_mode ) ||
172 S_ISCHR( fileinfo.st_mode ) )
174 i_ret = CSSTest( dvdcss );
177 _dvdcss_error( dvdcss, "CSS test failed" );
178 /* Disable the CSS ioctls and hope that it works? */
179 dvdcss->b_ioctls = 0;
180 dvdcss->b_encrypted = 1;
184 dvdcss->b_ioctls = 1;
185 dvdcss->b_encrypted = i_ret;
190 _dvdcss_debug( dvdcss, "dvdcss: file mode, using title method" );
191 dvdcss->i_method = DVDCSS_METHOD_TITLE;
192 dvdcss->b_ioctls = 0;
193 dvdcss->b_encrypted = 1;
196 /* If disc is CSS protected and the ioctls work, authenticate the drive */
197 if( dvdcss->b_encrypted && dvdcss->b_ioctls )
199 i_ret = CSSGetDiscKey( dvdcss );
203 _dvdcss_close( dvdcss );
208 memset( dvdcss->css.p_unenc_key, 0, KEY_SIZE );
210 memset( dvdcss->css.p_title_key, 0, KEY_SIZE );
213 if( psz_raw_device != NULL )
215 _dvdcss_raw_open( dvdcss, psz_raw_device );
222 /*****************************************************************************
223 * dvdcss_error: return the last libdvdcss error message
224 *****************************************************************************/
225 extern char * dvdcss_error ( dvdcss_handle dvdcss )
227 return dvdcss->psz_error;
230 /*****************************************************************************
231 * dvdcss_seek: seek into the device
232 *****************************************************************************/
233 extern int dvdcss_seek ( dvdcss_handle dvdcss, int i_blocks, int i_flags )
235 /* title cracking method is too slow to be used at each seek */
236 if( ( ( i_flags & DVDCSS_SEEK_MPEG )
237 && ( dvdcss->i_method != DVDCSS_METHOD_TITLE ) )
238 && dvdcss->b_encrypted )
242 /* Crack or decrypt CSS title key for current VTS */
243 i_ret = CSSGetTitleKey( dvdcss, i_blocks );
247 _dvdcss_error( dvdcss, "fatal error in vts css key" );
252 _dvdcss_error( dvdcss, "decryption unavailable" );
256 else if( i_flags & DVDCSS_SEEK_KEY )
258 /* check the title key */
259 if( dvdcss_title( dvdcss, i_blocks ) )
265 return _dvdcss_seek( dvdcss, i_blocks );
268 /*****************************************************************************
269 * dvdcss_title: crack or decrypt the current title key if needed
270 *****************************************************************************
271 * This function should only be called by dvdcss_seek and should eventually
272 * not be external if possible.
273 *****************************************************************************/
274 extern int dvdcss_title ( dvdcss_handle dvdcss, int i_block )
276 dvd_title_t *p_title;
277 dvd_title_t *p_newtitle;
280 if( ! dvdcss->b_encrypted )
285 /* Check if we've already cracked this key */
286 p_title = dvdcss->p_titles;
287 while( p_title != NULL
288 && p_title->p_next != NULL
289 && p_title->p_next->i_startlb <= i_block )
291 p_title = p_title->p_next;
295 && p_title->i_startlb == i_block )
297 /* We've already cracked this key, nothing to do */
298 memcpy( dvdcss->css.p_title_key, p_title->p_key, sizeof(dvd_key_t) );
302 /* Crack or decrypt CSS title key for current VTS */
303 i_ret = CSSGetTitleKey( dvdcss, i_block );
307 _dvdcss_error( dvdcss, "fatal error in vts css key" );
312 _dvdcss_error( dvdcss, "decryption unavailable" );
316 /* Find our spot in the list */
318 p_title = dvdcss->p_titles;
319 while( ( p_title != NULL ) && ( p_title->i_startlb < i_block ) )
321 p_newtitle = p_title;
322 p_title = p_title->p_next;
325 /* Save the found title */
326 p_title = p_newtitle;
328 /* Write in the new title and its key */
329 p_newtitle = malloc( sizeof( dvd_title_t ) );
330 p_newtitle->i_startlb = i_block;
331 memcpy( p_newtitle->p_key, dvdcss->css.p_title_key, KEY_SIZE );
333 /* Link the new title, either at the beginning or inside the list */
334 if( p_title == NULL )
336 dvdcss->p_titles = p_newtitle;
337 p_newtitle->p_next = NULL;
341 p_newtitle->p_next = p_title->p_next;
342 p_title->p_next = p_newtitle;
348 #define Pkey dvdcss->css.p_title_key
349 /*****************************************************************************
350 * dvdcss_read: read data from the device, decrypt if requested
351 *****************************************************************************/
352 extern int dvdcss_read ( dvdcss_handle dvdcss, void *p_buffer,
358 i_ret = _dvdcss_read( dvdcss, p_buffer, i_blocks );
361 || !dvdcss->b_encrypted
362 || !(i_flags & DVDCSS_READ_DECRYPT) )
367 /* For what we believe is an unencrypted title,
368 check that there are no encrypted blocks */
369 if( !( Pkey[0] | Pkey[1] | Pkey[2] | Pkey[3] | Pkey[4] ) )
371 for( i_index = i_ret; i_index; i_index-- )
373 if( ((u8*)p_buffer)[0x14] & 0x30 )
375 _dvdcss_error( dvdcss, "no key but found encrypted block" );
376 /* Only return the initial range of unscrambled blocks? */
378 /* or fail completely? return 0; */
380 (u8*)p_buffer += DVDCSS_BLOCK_SIZE;
384 /* Decrypt the blocks we managed to read */
385 for( i_index = i_ret; i_index; i_index-- )
387 CSSDescrambleSector( dvdcss->css.p_title_key, p_buffer );
388 ((u8*)p_buffer)[0x14] &= 0x8f;
389 (u8*)p_buffer += DVDCSS_BLOCK_SIZE;
395 /*****************************************************************************
396 * dvdcss_readv: read data to an iovec structure, decrypt if requested
397 *****************************************************************************/
398 extern int dvdcss_readv ( dvdcss_handle dvdcss, void *p_iovec,
402 #define P_IOVEC ((struct iovec*)p_iovec)
407 i_ret = _dvdcss_readv( dvdcss, P_IOVEC, i_blocks );
410 || !dvdcss->b_encrypted
411 || !(i_flags & DVDCSS_READ_DECRYPT) )
417 /* Initialize loop for decryption */
418 iov_base = P_IOVEC->iov_base;
419 iov_len = P_IOVEC->iov_len;
421 /* Decrypt the blocks we managed to read */
422 for( i_index = i_ret; i_index; i_index-- )
424 /* Check that iov_len is a multiple of 2048 */
425 if( iov_len & 0x7ff )
430 while( iov_len == 0 )
433 iov_base = P_IOVEC->iov_base;
434 iov_len = P_IOVEC->iov_len;
437 CSSDescrambleSector( dvdcss->css.p_title_key, iov_base );
438 ((u8*)iov_base)[0x14] &= 0x8f;
440 (u8*)iov_base += DVDCSS_BLOCK_SIZE;
441 (u8*)iov_len -= DVDCSS_BLOCK_SIZE;
449 /*****************************************************************************
450 * dvdcss_close: close the DVD device and clean up the library
451 *****************************************************************************/
452 extern int dvdcss_close ( dvdcss_handle dvdcss )
454 dvd_title_t *p_title;
457 /* Free our list of keys */
458 p_title = dvdcss->p_titles;
461 dvd_title_t *p_tmptitle = p_title->p_next;
463 p_title = p_tmptitle;
466 i_ret = _dvdcss_close( dvdcss );
478 /* Following functions are local */
480 static int _dvdcss_open ( dvdcss_handle dvdcss, char *psz_target )
486 _snprintf( psz_dvd, 7, "\\\\.\\%c:", psz_target[0] );
488 /* To have access to ioctls, we need read and write access to the
489 * device. This is only allowed if you have administrator priviledges
490 * so we allow for a fallback method where ioctls are not available but
491 * we at least have read access to the device.
492 * (See Microsoft Q241374: Read and Write Access Required for SCSI
493 * Pass Through Requests) */
494 (HANDLE) dvdcss->i_fd =
495 CreateFile( psz_dvd, GENERIC_READ | GENERIC_WRITE,
496 FILE_SHARE_READ | FILE_SHARE_WRITE,
498 FILE_FLAG_RANDOM_ACCESS, NULL );
500 if( (HANDLE) dvdcss->i_fd == INVALID_HANDLE_VALUE )
501 (HANDLE) dvdcss->i_fd =
502 CreateFile( psz_dvd, GENERIC_READ, FILE_SHARE_READ,
504 FILE_FLAG_RANDOM_ACCESS, NULL );
506 if( (HANDLE) dvdcss->i_fd == INVALID_HANDLE_VALUE )
508 _dvdcss_error( dvdcss, "failed opening device" );
514 dvdcss->i_fd = _win32_dvdcss_aopen( psz_target[0], dvdcss );
515 if( dvdcss->i_fd == -1 )
517 _dvdcss_error( dvdcss, "failed opening device" );
522 /* initialise readv temporary buffer */
523 dvdcss->p_readv_buffer = NULL;
524 dvdcss->i_readv_buf_size = 0;
527 dvdcss->i_fd = dvdcss->i_read_fd = open( psz_target, 0 );
529 if( dvdcss->i_fd == -1 )
531 _dvdcss_error( dvdcss, "failed opening device" );
541 static int _dvdcss_raw_open ( dvdcss_handle dvdcss, char *psz_target )
543 dvdcss->i_raw_fd = open( psz_target, 0 );
545 if( dvdcss->i_raw_fd == -1 )
547 _dvdcss_error( dvdcss, "failed opening raw device, continuing" );
552 dvdcss->i_read_fd = dvdcss->i_raw_fd;
559 static int _dvdcss_close ( dvdcss_handle dvdcss )
564 CloseHandle( (HANDLE) dvdcss->i_fd );
568 _win32_dvdcss_aclose( dvdcss->i_fd );
571 /* Free readv temporary buffer */
572 if( dvdcss->p_readv_buffer )
574 free( dvdcss->p_readv_buffer );
575 dvdcss->p_readv_buffer = NULL;
576 dvdcss->i_readv_buf_size = 0;
580 close( dvdcss->i_fd );
582 if( dvdcss->i_raw_fd >= 0 )
584 close( dvdcss->i_raw_fd );
585 dvdcss->i_raw_fd = -1;
593 int _dvdcss_seek ( dvdcss_handle dvdcss, int i_blocks )
596 dvdcss->i_seekpos = i_blocks;
600 LARGE_INTEGER li_read;
602 #ifndef INVALID_SET_FILE_POINTER
603 #define INVALID_SET_FILE_POINTER ((DWORD)-1)
606 li_read.QuadPart = (LONGLONG)i_blocks * DVDCSS_BLOCK_SIZE;
608 li_read.LowPart = SetFilePointer( (HANDLE) dvdcss->i_fd,
610 &li_read.HighPart, FILE_BEGIN );
611 if( (li_read.LowPart == INVALID_SET_FILE_POINTER)
612 && GetLastError() != NO_ERROR)
614 li_read.QuadPart = -DVDCSS_BLOCK_SIZE;
617 li_read.QuadPart /= DVDCSS_BLOCK_SIZE;
618 return (int)li_read.QuadPart;
622 return ( _win32_dvdcss_aseek( dvdcss->i_fd, i_blocks, SEEK_SET ) );
627 dvdcss->i_seekpos = i_blocks;
629 i_read = lseek( dvdcss->i_read_fd,
630 (off_t)i_blocks * (off_t)DVDCSS_BLOCK_SIZE, SEEK_SET );
632 return i_read / DVDCSS_BLOCK_SIZE;
637 int _dvdcss_read ( dvdcss_handle dvdcss, void *p_buffer, int i_blocks )
644 if( !ReadFile( (HANDLE) dvdcss->i_fd, p_buffer,
645 i_blocks * DVDCSS_BLOCK_SIZE,
646 (LPDWORD)&i_bytes, NULL ) )
650 return i_bytes / DVDCSS_BLOCK_SIZE;
654 return _win32_dvdcss_aread( dvdcss->i_fd, p_buffer, i_blocks );
660 i_bytes = read( dvdcss->i_read_fd, p_buffer,
661 (size_t)i_blocks * DVDCSS_BLOCK_SIZE );
662 return i_bytes / DVDCSS_BLOCK_SIZE;
667 static int _dvdcss_readv ( dvdcss_handle dvdcss, struct iovec *p_iovec,
673 /* Check the size of the readv temp buffer, just in case we need to
674 * realloc something bigger */
675 if( dvdcss->i_readv_buf_size < i_blocks * DVDCSS_BLOCK_SIZE )
677 dvdcss->i_readv_buf_size = i_blocks * DVDCSS_BLOCK_SIZE;
679 if( dvdcss->p_readv_buffer ) free( dvdcss->p_readv_buffer );
681 /* Allocate a buffer which will be used as a temporary storage
683 dvdcss->p_readv_buffer = malloc( dvdcss->i_readv_buf_size );
684 if( !dvdcss->p_readv_buffer )
686 _dvdcss_error( dvdcss, " failed (readv)" );
691 i_read = _win32_dvdcss_readv( dvdcss->i_fd, p_iovec, i_blocks,
692 dvdcss->p_readv_buffer );
696 i_read = readv( dvdcss->i_read_fd, p_iovec, i_blocks );
697 return i_read / DVDCSS_BLOCK_SIZE;
705 /*****************************************************************************
706 * _win32_dvdcss_readv: vectored read using ReadFile for Win2K and
707 * _win32_dvdcss_aread for win9x
708 *****************************************************************************/
709 static int _win32_dvdcss_readv( int i_fd, struct iovec *p_iovec,
710 int i_num_buffers, char *p_tmp_buffer )
713 int i_blocks, i_blocks_total = 0;
715 for( i_index = i_num_buffers; i_index; i_index-- )
717 i_blocks_total += p_iovec[i_index-1].iov_len;
720 if( i_blocks_total <= 0 ) return 0;
722 i_blocks_total /= DVDCSS_BLOCK_SIZE;
726 unsigned long int i_bytes;
727 if( !ReadFile( (HANDLE)i_fd, p_tmp_buffer,
728 i_blocks_total * DVDCSS_BLOCK_SIZE, &i_bytes, NULL ) )
731 /* The read failed... too bad.
732 As in the posix spec the file postition is left
733 unspecified after a failure */
735 i_blocks = i_bytes / DVDCSS_BLOCK_SIZE;
739 i_blocks = _win32_dvdcss_aread( i_fd, p_tmp_buffer, i_blocks_total );
742 return -1; /* idem */
746 /* We just have to copy the content of the temp buffer into the iovecs */
748 i_blocks_total = i_blocks;
749 while( i_blocks_total > 0 )
751 memcpy( p_iovec[i_index].iov_base,
752 &p_tmp_buffer[(i_blocks - i_blocks_total) * DVDCSS_BLOCK_SIZE],
753 p_iovec[i_index].iov_len );
754 /* if we read less blocks than asked, we'll just end up copying
755 garbage, this isn't an issue as we return the number of
756 blocks actually read */
757 i_blocks_total -= ( p_iovec[i_index].iov_len / DVDCSS_BLOCK_SIZE );
764 /*****************************************************************************
765 * _win32_dvdcss_aopen: open dvd drive (load aspi and init w32_aspidev
767 *****************************************************************************/
768 static int _win32_dvdcss_aopen( char c_drive, dvdcss_handle dvdcss )
772 struct w32_aspidev *fd;
773 int i, j, i_hostadapters;
774 long (*lpGetSupport)( void );
775 long (*lpSendCommand)( void* );
777 hASPI = LoadLibrary( "wnaspi32.dll" );
780 _dvdcss_error( dvdcss, "unable to load wnaspi32.dll" );
784 (FARPROC) lpGetSupport = GetProcAddress( hASPI, "GetASPI32SupportInfo" );
785 (FARPROC) lpSendCommand = GetProcAddress( hASPI, "SendASPI32Command" );
787 if(lpGetSupport == NULL || lpSendCommand == NULL )
789 _dvdcss_debug( dvdcss, "unable to get aspi function pointers" );
790 FreeLibrary( hASPI );
794 dwSupportInfo = lpGetSupport();
796 if( HIBYTE( LOWORD ( dwSupportInfo ) ) == SS_NO_ADAPTERS )
798 _dvdcss_debug( dvdcss, "no host adapters found (aspi)" );
799 FreeLibrary( hASPI );
803 if( HIBYTE( LOWORD ( dwSupportInfo ) ) != SS_COMP )
805 _dvdcss_error( dvdcss, "unable to initalize aspi layer" );
806 FreeLibrary( hASPI );
810 i_hostadapters = LOBYTE( LOWORD( dwSupportInfo ) );
811 if( i_hostadapters == 0 )
813 FreeLibrary( hASPI );
817 fd = malloc( sizeof( struct w32_aspidev ) );
820 FreeLibrary( hASPI );
825 fd->hASPI = (long) hASPI;
826 fd->lpSendCommand = lpSendCommand;
828 c_drive = c_drive > 'Z' ? c_drive - 'a' : c_drive - 'A';
830 for( i = 0; i < i_hostadapters; i++ )
832 for( j = 0; j < 15; j++ )
834 struct SRB_GetDiskInfo srbDiskInfo;
836 srbDiskInfo.SRB_Cmd = SC_GET_DISK_INFO;
837 srbDiskInfo.SRB_HaId = i;
838 srbDiskInfo.SRB_Flags = 0;
839 srbDiskInfo.SRB_Hdr_Rsvd = 0;
840 srbDiskInfo.SRB_Target = j;
841 srbDiskInfo.SRB_Lun = 0;
843 lpSendCommand( (void*) &srbDiskInfo );
845 if( (srbDiskInfo.SRB_Status == SS_COMP) &&
846 (srbDiskInfo.SRB_Int13HDriveInfo == c_drive) )
848 fd->i_sid = MAKEWORD( i, j );
855 FreeLibrary( hASPI );
856 _dvdcss_debug( dvdcss, "unable to get haid and target (aspi)" );
860 /*****************************************************************************
861 * _win32_dvdcss_aclose: close dvd drive (unload aspi and free w32_aspidev
863 *****************************************************************************/
864 static int _win32_dvdcss_aclose( int i_fd )
866 struct w32_aspidev *fd = (struct w32_aspidev *) i_fd;
868 FreeLibrary( (HMODULE) fd->hASPI );
869 free( (void*) i_fd );
874 /*****************************************************************************
875 * _win32_dvdcss_aseek: aspi version of _dvdcss_seek
877 * returns the number of blocks read.
878 *****************************************************************************/
879 static int _win32_dvdcss_aseek( int i_fd, int i_blocks, int i_method )
882 char sz_buf[ DVDCSS_BLOCK_SIZE ];
883 struct w32_aspidev *fd = (struct w32_aspidev *) i_fd;
885 i_old_blocks = fd->i_blocks;
886 fd->i_blocks = i_blocks;
888 if( _win32_dvdcss_aread( i_fd, sz_buf, 1 ) == -1 )
890 fd->i_blocks = i_old_blocks;
899 /*****************************************************************************
900 * _win32_dvdcss_aread: aspi version of _dvdcss_read
902 * returns the number of blocks read.
903 *****************************************************************************/
904 static int _win32_dvdcss_aread( int i_fd, void *p_data, int i_blocks )
907 struct SRB_ExecSCSICmd ssc;
908 struct w32_aspidev *fd = (struct w32_aspidev *) i_fd;
910 /* Create the transfer completion event */
911 hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
917 memset( &ssc, 0, sizeof( ssc ) );
919 ssc.SRB_Cmd = SC_EXEC_SCSI_CMD;
920 ssc.SRB_Flags = SRB_DIR_IN | SRB_EVENT_NOTIFY;
921 ssc.SRB_HaId = LOBYTE( fd->i_sid );
922 ssc.SRB_Target = HIBYTE( fd->i_sid );
923 ssc.SRB_SenseLen = SENSE_LEN;
925 ssc.SRB_PostProc = (LPVOID) hEvent;
926 ssc.SRB_BufPointer = p_data;
929 ssc.CDBByte[0] = 0xA8; /* RAW */
930 ssc.CDBByte[2] = (UCHAR) (fd->i_blocks >> 24);
931 ssc.CDBByte[3] = (UCHAR) (fd->i_blocks >> 16) & 0xff;
932 ssc.CDBByte[4] = (UCHAR) (fd->i_blocks >> 8) & 0xff;
933 ssc.CDBByte[5] = (UCHAR) (fd->i_blocks) & 0xff;
935 /* We have to break down the reads into 64kb pieces (ASPI restriction) */
938 ssc.SRB_BufLen = 32 * DVDCSS_BLOCK_SIZE;
942 /* Initiate transfer */
943 ResetEvent( hEvent );
944 fd->lpSendCommand( (void*) &ssc );
946 /* transfer the next 64kb (_win32_dvdcss_aread is called recursively)
947 * We need to check the status of the read on return */
948 if( _win32_dvdcss_aread( i_fd, (u8*) p_data + 32 * DVDCSS_BLOCK_SIZE,
956 /* This is the last transfer */
957 ssc.SRB_BufLen = i_blocks * DVDCSS_BLOCK_SIZE;
958 ssc.CDBByte[9] = (UCHAR) i_blocks;
959 fd->i_blocks += i_blocks;
961 /* Initiate transfer */
962 ResetEvent( hEvent );
963 fd->lpSendCommand( (void*) &ssc );
967 /* If the command has still not been processed, wait until it's finished */
968 if( ssc.SRB_Status == SS_PENDING )
970 WaitForSingleObject( hEvent, INFINITE );
972 CloseHandle( hEvent );
974 /* check that the transfer went as planned */
975 if( ssc.SRB_Status != SS_COMP )