]> git.sesse.net Git - vlc/blob - extras/libdvdcss/libdvdcss.c
New features for libdvdcss: we have three ways now to decode a title key.
[vlc] / extras / libdvdcss / libdvdcss.c
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 $
6  *
7  * Authors: Stéphane Borel <stef@via.ecp.fr>
8  *          Samuel Hocevar <sam@zoy.org>
9  *          Håkan Hjort <d95hjort@dtek.chalmers.se>
10  *
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.
15  * 
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.
20  *
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  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include "defs.h"
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <fcntl.h>
37
38 #ifdef HAVE_UNISTD_H
39 #   include <unistd.h>
40 #endif
41
42 #if defined( WIN32 )
43 #   include <io.h>                                                 /* read() */
44 #else
45 #   include <sys/uio.h>                                      /* struct iovec */
46 #endif
47
48 #include "config.h"
49 #include "common.h"
50
51 #if defined( WIN32 )
52 #   include "input_iovec.h"
53 #endif
54
55 #include "videolan/dvdcss.h"
56 #include "libdvdcss.h"
57 #include "ioctl.h"
58
59 /*****************************************************************************
60  * Local prototypes
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 );
67
68 /*****************************************************************************
69  * Local prototypes, win32 specific
70  *****************************************************************************/
71 #if defined( WIN32 )
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 );
78 #endif
79
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 )
84 {
85     int i_ret;
86
87     dvdcss_handle dvdcss;
88
89     /* Allocate the library structure */
90     dvdcss = malloc( sizeof( struct dvdcss_s ) );
91     if( dvdcss == NULL )
92     {
93         if( ! (i_flags & DVDCSS_INIT_QUIET) )
94         {
95             DVDCSS_ERROR( "could not initialize library" );
96         }
97
98         return NULL;
99     }
100
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";
107
108     i_ret = _dvdcss_open( dvdcss, psz_target );
109     if( i_ret < 0 )
110     {
111         free( dvdcss );
112         return NULL;
113     }
114
115     i_ret = CSSTest( dvdcss );
116     if( i_ret < 0 )
117     {
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;
122     }
123     else
124     {
125         dvdcss->b_ioctls = 1;
126         dvdcss->b_encrypted = i_ret;
127     }
128
129     /* If disc is CSS protected and the ioctls work, authenticate the drive */
130     if( dvdcss->b_encrypted && dvdcss->b_ioctls )
131     {
132         i_ret = CSSGetDiscKey( dvdcss );
133
134         if( i_ret < 0 )
135         {
136             _dvdcss_close( dvdcss );
137             free( dvdcss );
138             return NULL;
139         }
140     }
141
142     return dvdcss;
143 }
144
145 /*****************************************************************************
146  * dvdcss_error: return the last libdvdcss error message
147  *****************************************************************************/
148 extern char * dvdcss_error ( dvdcss_handle dvdcss )
149 {
150     return dvdcss->psz_error;
151 }
152
153 /*****************************************************************************
154  * dvdcss_seek: seek into the device
155  *****************************************************************************/
156 extern int dvdcss_seek ( dvdcss_handle dvdcss, int i_blocks, int i_flags )
157 {
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 ) )
161     {
162         /* check the title key */
163         dvdcss_title( dvdcss, i_blocks );
164     }
165
166     return _dvdcss_seek( dvdcss, i_blocks );
167 }
168
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 )
176 {
177     dvd_title_t *p_title;
178     dvd_title_t *p_newtitle;
179     int          i_ret;
180
181     if( ! dvdcss->b_encrypted )
182     {
183         return 0;
184     }
185
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 )
191     {
192         p_title = p_title->p_next;
193     }
194
195     if( p_title != NULL
196          && p_title->i_startlb == i_block )
197     {
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) );
200         return 0;
201     }
202
203     /* Crack or decrypt CSS title key for current VTS */
204     i_ret = CSSGetTitleKey( dvdcss, i_block );
205
206     if( i_ret < 0 )
207     {
208         _dvdcss_error( dvdcss, "fatal error in vts css key" );
209         return i_ret;
210     }
211     else if( i_ret > 0 )
212     {
213         _dvdcss_error( dvdcss, "decryption unavailable" );
214         return -1;
215     }
216
217     /* Find our spot in the list */
218     p_newtitle = NULL;
219     p_title = dvdcss->p_titles;
220     while( ( p_title != NULL ) && ( p_title->i_startlb < i_block ) )
221     {
222         p_newtitle = p_title;
223         p_title = p_title->p_next;
224     }
225
226     /* Save the found title */
227     p_title = p_newtitle;
228
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 );
233
234     /* Link the new title, either at the beginning or inside the list */
235     if( p_title == NULL )
236     {
237         dvdcss->p_titles = p_newtitle;
238         p_newtitle->p_next = NULL;
239     }
240     else
241     {
242         p_newtitle->p_next = p_title->p_next;
243         p_title->p_next = p_newtitle;
244     }
245
246     return 0;
247 }
248
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,
254                                                int i_blocks,
255                                                int i_flags )
256 {
257     int i_ret, i_index;
258
259     i_ret = _dvdcss_read( dvdcss, p_buffer, i_blocks );
260
261     if( i_ret <= 0
262          || !dvdcss->b_encrypted
263          || !(i_flags & DVDCSS_READ_DECRYPT) )
264     {
265         return i_ret;
266     }
267
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] ) ) 
271     {
272         for( i_index = i_ret; i_index; i_index-- )
273         {
274             if( ((u8*)p_buffer)[0x14] & 0x30 )
275             {
276                 _dvdcss_error( dvdcss, "no key but found encrypted block" );
277                 /* Only return the initial range of unscrambled blocks? */
278                 i_ret = i_index;
279                 /* or fail completely? return 0; */
280             }
281             (u8*)p_buffer += DVDCSS_BLOCK_SIZE;
282         }
283     }
284
285     /* Decrypt the blocks we managed to read */
286     for( i_index = i_ret; i_index; i_index-- )
287     {
288         CSSDescrambleSector( dvdcss->css.p_title_key, p_buffer );
289         ((u8*)p_buffer)[0x14] &= 0x8f;
290         (u8*)p_buffer += DVDCSS_BLOCK_SIZE;
291     }
292
293     return i_ret;
294 }
295
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,
300                                                 int i_blocks,
301                                                 int i_flags )
302 {
303 #define P_IOVEC ((struct iovec*)p_iovec)
304     int i_ret, i_index;
305     void *iov_base;
306     size_t iov_len;
307
308     i_ret = _dvdcss_readv( dvdcss, P_IOVEC, i_blocks );
309
310     if( i_ret <= 0
311          || !dvdcss->b_encrypted
312          || !(i_flags & DVDCSS_READ_DECRYPT) )
313     {
314         return i_ret;
315     }
316
317
318     /* Initialize loop for decryption */
319     iov_base = P_IOVEC->iov_base;
320     iov_len = P_IOVEC->iov_len;
321
322     /* Decrypt the blocks we managed to read */
323     for( i_index = i_ret; i_index; i_index-- )
324     {
325         /* Check that iov_len is a multiple of 2048 */
326         if( iov_len & 0x7ff )
327         {
328             return -1;
329         }
330
331         while( iov_len == 0 )
332         {
333             P_IOVEC++;
334             iov_base = P_IOVEC->iov_base;
335             iov_len = P_IOVEC->iov_len;
336         }
337
338         CSSDescrambleSector( dvdcss->css.p_title_key, iov_base );
339         ((u8*)iov_base)[0x14] &= 0x8f;
340
341         (u8*)iov_base += DVDCSS_BLOCK_SIZE;
342         (u8*)iov_len -= DVDCSS_BLOCK_SIZE;
343     }
344
345     return i_ret;
346 #undef P_IOVEC
347 }
348 #undef Pkey
349
350 /*****************************************************************************
351  * dvdcss_close: close the DVD device and clean up the library
352  *****************************************************************************/
353 extern int dvdcss_close ( dvdcss_handle dvdcss )
354 {
355     dvd_title_t *p_title;
356     int i_ret;
357
358     /* Free our list of keys */
359     p_title = dvdcss->p_titles;
360     while( p_title )
361     {
362         dvd_title_t *p_tmptitle = p_title->p_next;
363         free( p_title );
364         p_title = p_tmptitle;
365     }
366
367     i_ret = _dvdcss_close( dvdcss );
368
369     if( i_ret < 0 )
370     {
371         return i_ret;
372     }
373
374     free( dvdcss );
375
376     return 0;
377 }
378
379 /* Following functions are local */
380
381 static int _dvdcss_open ( dvdcss_handle dvdcss, char *psz_target )
382 {
383 #if defined( WIN32 )
384     if( WIN2K )
385     {
386         char psz_dvd[7];
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 )
393         {
394             _dvdcss_error( dvdcss, "failed opening device" );
395             return -1;
396         }
397     }
398     else
399     {
400         dvdcss->i_fd = _win32_dvdcss_aopen( psz_target[0], dvdcss );
401         if( dvdcss->i_fd == -1 )
402         {
403             _dvdcss_error( dvdcss, "failed opening device" );
404             return -1;
405         }
406     }
407
408     /* initialise readv temporary buffer */
409     dvdcss->p_readv_buffer   = NULL;
410     dvdcss->i_readv_buf_size = 0;
411
412 #else
413     dvdcss->i_fd = open( psz_target, 0 );
414
415     if( dvdcss->i_fd == -1 )
416     {
417         _dvdcss_error( dvdcss, "failed opening device" );
418         return -1;
419     }
420
421 #endif
422
423     return 0;
424 }
425
426 static int _dvdcss_close ( dvdcss_handle dvdcss )
427 {
428 #if defined( WIN32 )
429     if( WIN2K )
430     {
431         CloseHandle( (HANDLE) dvdcss->i_fd );
432     }
433     else
434     {
435         _win32_dvdcss_aclose( dvdcss->i_fd );
436     }
437
438     /* Free readv temporary buffer */
439     if( dvdcss->p_readv_buffer )
440     {
441         free( dvdcss->p_readv_buffer );
442         dvdcss->p_readv_buffer   = NULL;
443         dvdcss->i_readv_buf_size = 0;
444     }
445
446 #else
447     close( dvdcss->i_fd );
448
449 #endif
450
451     return 0;
452 }
453
454 static int _dvdcss_seek ( dvdcss_handle dvdcss, int i_blocks )
455 {
456 #if defined( WIN32 )
457     dvdcss->i_seekpos = i_blocks;
458
459     if( WIN2K )
460     {
461         LARGE_INTEGER li_read;
462
463 #ifndef INVALID_SET_FILE_POINTER
464 #define INVALID_SET_FILE_POINTER ((DWORD)-1)
465 #endif
466
467         li_read.QuadPart = (LONGLONG)i_blocks * DVDCSS_BLOCK_SIZE;
468
469         li_read.LowPart = SetFilePointer( (HANDLE) dvdcss->i_fd,
470                                           li_read.LowPart,
471                                           &li_read.HighPart, FILE_BEGIN );
472         if( (li_read.LowPart == INVALID_SET_FILE_POINTER)
473             && GetLastError() != NO_ERROR)
474         {
475             li_read.QuadPart = -DVDCSS_BLOCK_SIZE;
476         }
477
478         li_read.QuadPart /= DVDCSS_BLOCK_SIZE;
479         return (int)li_read.QuadPart;
480     }
481     else
482     {
483         return ( _win32_dvdcss_aseek( dvdcss->i_fd, i_blocks, SEEK_SET ) );
484     }
485 #else
486     off_t i_read;
487
488     dvdcss->i_seekpos = i_blocks;
489
490     i_read = lseek( dvdcss->i_fd,
491                     (off_t)i_blocks * (off_t)DVDCSS_BLOCK_SIZE, SEEK_SET );
492
493     return i_read / DVDCSS_BLOCK_SIZE;
494 #endif
495
496 }
497
498 static int _dvdcss_read ( dvdcss_handle dvdcss, void *p_buffer, int i_blocks )
499 {
500 #if defined( WIN32 ) 
501     if( WIN2K )
502     {
503         int i_bytes;
504
505         if( !ReadFile( (HANDLE) dvdcss->i_fd, p_buffer,
506                   i_blocks * DVDCSS_BLOCK_SIZE,
507                   (LPDWORD)&i_bytes, NULL ) )
508         {
509             return -1;
510         }
511         return i_bytes / DVDCSS_BLOCK_SIZE;
512     }
513     else
514     {
515         return _win32_dvdcss_aread( dvdcss->i_fd, p_buffer, i_blocks );
516     }
517
518 #else
519     int i_bytes;
520
521     i_bytes = read( dvdcss->i_fd, p_buffer,
522                     (size_t)i_blocks * DVDCSS_BLOCK_SIZE );
523     return i_bytes / DVDCSS_BLOCK_SIZE;
524 #endif
525
526 }
527
528 static int _dvdcss_readv ( dvdcss_handle dvdcss, struct iovec *p_iovec,
529                            int i_blocks )
530 {
531     int i_read;
532
533 #if defined( WIN32 )
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 )
537     {
538         dvdcss->i_readv_buf_size = i_blocks * DVDCSS_BLOCK_SIZE;
539
540         if( dvdcss->p_readv_buffer ) free( dvdcss->p_readv_buffer );
541
542         /* Allocate a buffer which will be used as a temporary storage
543          * for readv */
544         dvdcss->p_readv_buffer = malloc( dvdcss->i_readv_buf_size );
545         if( !dvdcss->p_readv_buffer )
546         {
547             _dvdcss_error( dvdcss, " failed (readv)" );
548             return -1;
549         }
550     }
551
552     i_read = _win32_dvdcss_readv( dvdcss->i_fd, p_iovec, i_blocks,
553                                   dvdcss->p_readv_buffer );
554     return i_read;
555
556 #else
557     i_read = readv( dvdcss->i_fd, p_iovec, i_blocks );
558     return i_read / DVDCSS_BLOCK_SIZE;
559
560 #endif
561 }
562
563
564 #if defined( WIN32 )
565
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 )
572 {
573     int i_index;
574     int i_blocks, i_blocks_total = 0;
575
576     for( i_index = i_num_buffers; i_index; i_index-- )
577     {
578         i_blocks_total += p_iovec[i_index-1].iov_len; 
579     }
580
581     if( i_blocks_total <= 0 ) return 0;
582
583     i_blocks_total /= DVDCSS_BLOCK_SIZE;
584
585     if( WIN2K )
586     {
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 ) )
590         {
591             return -1;
592             /* The read failed... too bad.
593                As in the posix spec the file postition is left
594                unspecified after a failure */
595         }
596         i_blocks = i_bytes / DVDCSS_BLOCK_SIZE;
597     }
598     else /* Win9x */
599     {
600         i_blocks = _win32_dvdcss_aread( i_fd, p_tmp_buffer, i_blocks_total );
601         if( i_blocks < 0 )
602         {
603             return -1;  /* idem */
604         }
605     }
606
607     /* We just have to copy the content of the temp buffer into the iovecs */
608     i_index = 0;
609     i_blocks_total = i_blocks;
610     while( i_blocks_total > 0 )
611     {
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 );
619         i_index++;
620     } 
621
622     return i_blocks;
623 }
624
625 /*****************************************************************************
626  * _win32_dvdcss_aopen: open dvd drive (load aspi and init w32_aspidev
627  *                      structure)
628  *****************************************************************************/
629 static int _win32_dvdcss_aopen( char c_drive, dvdcss_handle dvdcss )
630 {
631     HMODULE hASPI;
632     DWORD dwSupportInfo;
633     struct w32_aspidev *fd;
634     int i, j, i_hostadapters;
635     long (*lpGetSupport)( void );
636     long (*lpSendCommand)( void* );
637      
638     hASPI = LoadLibrary( "wnaspi32.dll" );
639     if( hASPI == NULL )
640     {
641         _dvdcss_error( dvdcss, "unable to load wnaspi32.dll" );
642         return -1;
643     }
644
645     (FARPROC) lpGetSupport = GetProcAddress( hASPI, "GetASPI32SupportInfo" );
646     (FARPROC) lpSendCommand = GetProcAddress( hASPI, "SendASPI32Command" );
647  
648     if(lpGetSupport == NULL || lpSendCommand == NULL )
649     {
650         _dvdcss_debug( dvdcss, "unable to get aspi function pointers" );
651         FreeLibrary( hASPI );
652         return -1;
653     }
654
655     dwSupportInfo = lpGetSupport();
656
657     if( HIBYTE( LOWORD ( dwSupportInfo ) ) == SS_NO_ADAPTERS )
658     {
659         _dvdcss_debug( dvdcss, "no host adapters found (aspi)" );
660         FreeLibrary( hASPI );
661         return -1;
662     }
663
664     if( HIBYTE( LOWORD ( dwSupportInfo ) ) != SS_COMP )
665     {
666         _dvdcss_error( dvdcss, "unable to initalize aspi layer" );
667         FreeLibrary( hASPI );
668         return -1;
669     }
670
671     i_hostadapters = LOBYTE( LOWORD( dwSupportInfo ) );
672     if( i_hostadapters == 0 )
673     {
674         FreeLibrary( hASPI );
675         return -1;
676     }
677
678     fd = malloc( sizeof( struct w32_aspidev ) );
679     if( fd == NULL )
680     {
681         FreeLibrary( hASPI );
682         return -1;
683     }
684
685     fd->i_blocks = 0;
686     fd->hASPI = (long) hASPI;
687     fd->lpSendCommand = lpSendCommand;
688
689     c_drive = c_drive > 'Z' ? c_drive - 'a' : c_drive - 'A';
690
691     for( i = 0; i < i_hostadapters; i++ )
692     {
693         for( j = 0; j < 15; j++ )
694         {
695             struct SRB_GetDiskInfo srbDiskInfo;
696
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;
703
704             lpSendCommand( (void*) &srbDiskInfo );
705
706             if( (srbDiskInfo.SRB_Status == SS_COMP) &&
707                 (srbDiskInfo.SRB_Int13HDriveInfo == c_drive) )
708             {
709                 fd->i_sid = MAKEWORD( i, j );
710                 return (int) fd;
711             }
712         }
713     }
714
715     free( (void*) fd );
716     FreeLibrary( hASPI );
717     _dvdcss_debug( dvdcss, "unable to get haid and target (aspi)" );
718     return( -1 );        
719 }
720
721 /*****************************************************************************
722  * _win32_dvdcss_aclose: close dvd drive (unload aspi and free w32_aspidev
723  *                       structure)
724  *****************************************************************************/
725 static int _win32_dvdcss_aclose( int i_fd )
726 {
727     struct w32_aspidev *fd = (struct w32_aspidev *) i_fd;
728
729     FreeLibrary( (HMODULE) fd->hASPI );
730     free( (void*) i_fd );
731
732     return 0;
733 }
734
735 /*****************************************************************************
736  * _win32_dvdcss_aseek: aspi version of _dvdcss_seek
737  * 
738  * returns the number of blocks read.
739  *****************************************************************************/
740 static int _win32_dvdcss_aseek( int i_fd, int i_blocks, int i_method )
741 {
742     int i_old_blocks;
743     char sz_buf[ DVDCSS_BLOCK_SIZE ];
744     struct w32_aspidev *fd = (struct w32_aspidev *) i_fd;
745     
746     i_old_blocks = fd->i_blocks;
747     fd->i_blocks = i_blocks;
748
749     if( _win32_dvdcss_aread( i_fd, sz_buf, 1 ) == -1 )
750     {
751         fd->i_blocks = i_old_blocks;
752         return -1;
753     }
754
755     (fd->i_blocks)--;
756
757     return fd->i_blocks;
758 }
759
760 /*****************************************************************************
761  * _win32_dvdcss_aread: aspi version of _dvdcss_read
762  *
763  * returns the number of blocks read.
764  *****************************************************************************/
765 static int _win32_dvdcss_aread( int i_fd, void *p_data, int i_blocks )
766 {
767     HANDLE hEvent;
768     struct SRB_ExecSCSICmd ssc;
769     struct w32_aspidev *fd = (struct w32_aspidev *) i_fd;
770
771     /* Create the transfer completion event */
772     hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
773     if( hEvent == NULL )
774     {
775         return -1;
776     }
777
778     memset( &ssc, 0, sizeof( ssc ) );
779
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;
785     
786     ssc.SRB_PostProc = (LPVOID) hEvent;
787     ssc.SRB_BufPointer  = p_data;
788     ssc.SRB_CDBLen      = 12;
789     
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;
795     
796     /* We have to break down the reads into 64kb pieces (ASPI restriction) */
797     if( i_blocks > 32 )
798     {
799         ssc.SRB_BufLen = 32 * DVDCSS_BLOCK_SIZE;
800         ssc.CDBByte[9] = 32;
801         fd->i_blocks  += 32;
802
803         /* Initiate transfer */  
804         ResetEvent( hEvent );
805         fd->lpSendCommand( (void*) &ssc );
806
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,
810                                  i_blocks - 32) < 0 )
811         {
812             return -1;
813         }
814     }
815     else
816     {
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;
821
822         /* Initiate transfer */  
823         ResetEvent( hEvent );
824         fd->lpSendCommand( (void*) &ssc );
825
826     }
827
828     /* If the command has still not been processed, wait until it's finished */
829     if( ssc.SRB_Status == SS_PENDING )
830     {
831         WaitForSingleObject( hEvent, INFINITE );
832     }
833     CloseHandle( hEvent );
834
835     /* check that the transfer went as planned */
836     if( ssc.SRB_Status != SS_COMP )
837     {
838       return -1;
839     }
840
841     return i_blocks;
842 }
843
844 #endif
845