]> git.sesse.net Git - vlc/blob - extras/libdvdcss/libdvdcss.c
30b037f43ae8b41fb2bb9428a7eedde76b0f36bf
[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.13 2001/07/30 18:56:35 gbazin Exp $
6  *
7  * Authors: Stéphane Borel <stef@via.ecp.fr>
8  *          Samuel Hocevar <sam@zoy.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  * 
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include "defs.h"
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <fcntl.h>
35
36 #ifdef HAVE_UNISTD_H
37 #   include <unistd.h>
38 #endif
39
40 #if defined( WIN32 )
41 #   include <io.h>                                                 /* read() */
42 #else
43 #   include <sys/uio.h>                                      /* struct iovec */
44 #endif
45
46 #include "config.h"
47 #include "common.h"
48
49 #if defined( WIN32 )
50 #   include "input_iovec.h"
51 #endif
52
53 #include "videolan/dvdcss.h"
54 #include "libdvdcss.h"
55 #include "ioctl.h"
56
57 /*****************************************************************************
58  * Local prototypes
59  *****************************************************************************/
60 static int _dvdcss_open  ( dvdcss_handle, char *psz_target );
61 static int _dvdcss_close ( dvdcss_handle );
62 static int _dvdcss_seek  ( dvdcss_handle, int i_blocks );
63 static int _dvdcss_read  ( dvdcss_handle, void *p_buffer, int i_blocks );
64 static int _dvdcss_readv ( dvdcss_handle, struct iovec *p_iovec, int i_blocks );
65
66 /*****************************************************************************
67  * Local prototypes, win32 specific
68  *****************************************************************************/
69 #if defined( WIN32 )
70 static int _win32_dvdcss_readv  ( int i_fd, struct iovec *p_iovec,
71                                   int i_num_buffers, char *p_tmp_buffer );
72 static int _win32_dvdcss_aopen  ( char c_drive, dvdcss_handle dvdcss );
73 static int _win32_dvdcss_aclose ( int i_fd );
74 static int _win32_dvdcss_aseek  ( int i_fd, int i_blocks, int i_method );
75 static int _win32_dvdcss_aread  ( int i_fd, void *p_data, int i_blocks );
76 #endif
77
78 /*****************************************************************************
79  * dvdcss_open: initialize library, open a DVD device, crack CSS key
80  *****************************************************************************/
81 extern dvdcss_handle dvdcss_open ( char *psz_target, int i_flags )
82 {
83     int i_ret;
84
85     dvdcss_handle dvdcss;
86
87     /* Allocate the library structure */
88     dvdcss = malloc( sizeof( struct dvdcss_s ) );
89     if( dvdcss == NULL )
90     {
91         if( ! (i_flags & DVDCSS_INIT_QUIET) )
92         {
93             DVDCSS_ERROR( "could not initialize library" );
94         }
95
96         return NULL;
97     }
98
99     /* Initialize structure */
100     dvdcss->p_titles = NULL;
101     dvdcss->b_debug = i_flags & DVDCSS_INIT_DEBUG;
102     dvdcss->b_errors = !(i_flags & DVDCSS_INIT_QUIET);
103     dvdcss->psz_error = "no error";
104
105     i_ret = _dvdcss_open( dvdcss, psz_target );
106     if( i_ret < 0 )
107     {
108         free( dvdcss );
109         return NULL;
110     }
111
112     i_ret = CSSTest( dvdcss );
113     if( i_ret < 0 )
114     {
115         _dvdcss_error( dvdcss, "css test failed" );
116         _dvdcss_close( dvdcss );
117         free( dvdcss );
118         return NULL;
119     }
120
121     dvdcss->b_encrypted = i_ret;
122
123     /* If drive is encrypted, crack its key */
124     if( dvdcss->b_encrypted )
125     {
126         i_ret = CSSInit( dvdcss );
127
128         if( i_ret < 0 )
129         {
130             _dvdcss_close( dvdcss );
131             free( dvdcss );
132             return NULL;
133         }
134     }
135
136     return dvdcss;
137 }
138
139 /*****************************************************************************
140  * dvdcss_error: return the last libdvdcss error message
141  *****************************************************************************/
142 extern char * dvdcss_error ( dvdcss_handle dvdcss )
143 {
144     return dvdcss->psz_error;
145 }
146
147 /*****************************************************************************
148  * dvdcss_seek: seek into the device
149  *****************************************************************************/
150 extern int dvdcss_seek ( dvdcss_handle dvdcss, int i_blocks )
151 {
152     return _dvdcss_seek( dvdcss, i_blocks );
153 }
154
155 /*****************************************************************************
156  * dvdcss_title: crack the current title key if needed
157  *****************************************************************************/
158 extern int dvdcss_title ( dvdcss_handle dvdcss, int i_block )
159 {
160     dvd_title_t *p_title;
161     dvd_key_t p_key;
162     int i_ret;
163
164     if( ! dvdcss->b_encrypted )
165     {
166         return 0;
167     }
168
169     //fprintf( stderr, "looking for a key for offset %i\n", i_block );
170
171     /* Check if we've already cracked this key */
172     p_title = dvdcss->p_titles;
173     while( p_title != NULL
174             && p_title->p_next != NULL
175             && p_title->p_next->i_startlb <= i_block )
176     {
177         p_title = p_title->p_next;
178     }
179
180     if( p_title != NULL
181          && p_title->i_startlb == i_block )
182     {
183         /* We've already cracked this key, nothing to do */
184         return 0;
185     }
186
187     /* Crack CSS title key for current VTS */
188     i_ret = CSSGetKey( dvdcss, i_block, p_key );
189
190     if( i_ret < 0 )
191     {
192         _dvdcss_error( dvdcss, "fatal error in vts css key" );
193         return i_ret;
194     }
195     else if( i_ret > 0 )
196     {
197         _dvdcss_error( dvdcss, "decryption unavailable" );
198         return -1;
199     }
200
201     //fprintf( stderr, "cracked key is %.2x %.2x %.2x %.2x %.2x\n",
202     //         p_key[0], p_key[1], p_key[2], p_key[3], p_key[4] );
203
204     /* Add key to keytable if it isn't empty */
205     if( p_key[0] | p_key[1] | p_key[2] | p_key[3] | p_key[4] )
206     {
207         dvd_title_t *p_newtitle;
208
209         /* Find our spot in the list */
210         p_newtitle = NULL;
211         p_title = dvdcss->p_titles;
212         while( p_title != NULL
213                 && p_title->i_startlb < i_block )
214         {
215             p_newtitle = p_title;
216             p_title = p_title->p_next;
217         }
218
219         /* Save the found title */
220         p_title = p_newtitle;
221
222         /* Write in the new title and its key */
223         p_newtitle = malloc( sizeof( dvd_title_t ) );
224         p_newtitle->i_startlb = i_block;
225         memcpy( p_newtitle->p_key, p_key, KEY_SIZE );
226
227         /* Link the new title, either at the beginning or inside the list */
228         if( p_title == NULL )
229         {
230             dvdcss->p_titles = p_newtitle;
231             p_newtitle->p_next = NULL;
232         }
233         else
234         {
235             p_newtitle->p_next = p_title->p_next;
236             p_title->p_next = p_newtitle;
237         }
238     }
239
240     return 0;
241 }
242
243 /*****************************************************************************
244  * dvdcss_read: read data from the device, decrypt if requested
245  *****************************************************************************/
246 extern int dvdcss_read ( dvdcss_handle dvdcss, void *p_buffer,
247                                                int i_blocks,
248                                                int i_flags )
249 {
250     dvd_title_t *p_title;
251     int i_ret, i_index;
252
253     i_ret = _dvdcss_read( dvdcss, p_buffer, i_blocks );
254
255     if( i_ret <= 0
256          || !dvdcss->b_encrypted
257          || !(i_flags & DVDCSS_READ_DECRYPT) )
258     {
259         return i_ret;
260     }
261
262     /* find our key */
263     p_title = dvdcss->p_titles;
264     while( p_title != NULL
265             && p_title->p_next
266             && p_title->p_next->i_startlb <= dvdcss->i_seekpos )
267     {
268         p_title = p_title->p_next;
269     }
270
271     if( p_title == NULL )
272     {
273         /* no css key found to use, so no decryption to do */
274         return 0;
275     }
276
277     /* Decrypt the blocks we managed to read */
278     for( i_index = i_ret; i_index; i_index-- )
279     {
280         CSSDescrambleSector( p_title->p_key, p_buffer );
281         ((u8*)p_buffer)[0x14] &= 0x8f;
282         (u8*)p_buffer += DVDCSS_BLOCK_SIZE;
283     }
284
285     return i_ret;
286 }
287
288 /*****************************************************************************
289  * dvdcss_readv: read data to an iovec structure, decrypt if reaquested
290  *****************************************************************************/
291 extern int dvdcss_readv ( dvdcss_handle dvdcss, void *p_iovec,
292                                                 int i_blocks,
293                                                 int i_flags )
294 {
295 #define P_IOVEC ((struct iovec*)p_iovec)
296     dvd_title_t *p_title;
297     int i_ret, i_index;
298     void *iov_base;
299     size_t iov_len;
300
301     i_ret = _dvdcss_readv( dvdcss, P_IOVEC, i_blocks );
302
303     if( i_ret <= 0
304          || !dvdcss->b_encrypted
305          || !(i_flags & DVDCSS_READ_DECRYPT) )
306     {
307         return i_ret;
308     }
309
310     /* Find our key */
311     p_title = dvdcss->p_titles;
312     while( p_title != NULL
313             && p_title->p_next != NULL
314             && p_title->p_next->i_startlb <= dvdcss->i_seekpos )
315     {
316         p_title = p_title->p_next;
317     }
318
319     if( p_title == NULL )
320     {
321         /* no css key found to use, so no decryption to do */
322         return 0;
323     }
324
325     /* Initialize loop for decryption */
326     iov_base = P_IOVEC->iov_base;
327     iov_len = P_IOVEC->iov_len;
328
329     /* Decrypt the blocks we managed to read */
330     for( i_index = i_ret; i_index; i_index-- )
331     {
332         /* Check that iov_len is a multiple of 2048 */
333         if( iov_len & 0x7ff )
334         {
335             return -1;
336         }
337
338         while( iov_len == 0 )
339         {
340             P_IOVEC++;
341             iov_base = P_IOVEC->iov_base;
342             iov_len = P_IOVEC->iov_len;
343         }
344
345         CSSDescrambleSector( p_title->p_key, iov_base );
346         ((u8*)iov_base)[0x14] &= 0x8f;
347
348         (u8*)iov_base += DVDCSS_BLOCK_SIZE;
349         (u8*)iov_len -= DVDCSS_BLOCK_SIZE;
350     }
351
352     return i_ret;
353 #undef P_IOVEC
354 }
355
356 /*****************************************************************************
357  * dvdcss_close: close the DVD device and clean up the library
358  *****************************************************************************/
359 extern int dvdcss_close ( dvdcss_handle dvdcss )
360 {
361     dvd_title_t *p_title;
362     int i_ret;
363
364     /* Free our list of keys */
365     p_title = dvdcss->p_titles;
366     while( p_title )
367     {
368         dvd_title_t *p_tmptitle = p_title->p_next;
369         free( p_title );
370         p_title = p_tmptitle;
371     }
372
373     i_ret = _dvdcss_close( dvdcss );
374
375     if( i_ret < 0 )
376     {
377         return i_ret;
378     }
379
380     free( dvdcss );
381
382     return 0;
383 }
384
385 /* Following functions are local */
386
387 static int _dvdcss_open ( dvdcss_handle dvdcss, char *psz_target )
388 {
389 #if defined( WIN32 )
390     if( WIN2K )
391     {
392         char psz_dvd[7];
393         _snprintf( psz_dvd, 7, "\\\\.\\%c:", psz_target[0] );
394         (HANDLE) dvdcss->i_fd =
395                 CreateFile( psz_dvd, GENERIC_READ | GENERIC_WRITE,
396                                 FILE_SHARE_READ | FILE_SHARE_WRITE,
397                                 NULL, OPEN_EXISTING, 0, NULL );
398         if( (HANDLE) dvdcss->i_fd == INVALID_HANDLE_VALUE )
399         {
400             _dvdcss_error( dvdcss, "failed opening device" );
401             return -1;
402         }
403     }
404     else
405     {
406         dvdcss->i_fd = _win32_dvdcss_aopen( psz_target[0], dvdcss );
407         if( dvdcss->i_fd == -1 )
408         {
409             _dvdcss_error( dvdcss, "failed opening device" );
410             return -1;
411         }
412     }
413
414     /* initialise readv temporary buffer */
415     dvdcss->p_readv_buffer   = NULL;
416     dvdcss->i_readv_buf_size = 0;
417
418 #else
419     dvdcss->i_fd = open( psz_target, 0 );
420
421     if( dvdcss->i_fd == -1 )
422     {
423         _dvdcss_error( dvdcss, "failed opening device" );
424         return -1;
425     }
426
427 #endif
428
429     return 0;
430 }
431
432 static int _dvdcss_close ( dvdcss_handle dvdcss )
433 {
434 #if defined( WIN32 )
435     if( WIN2K )
436     {
437         CloseHandle( (HANDLE) dvdcss->i_fd );
438     }
439     else
440     {
441         _win32_dvdcss_aclose( dvdcss->i_fd );
442     }
443
444     /* Free readv temporary buffer */
445     if( dvdcss->p_readv_buffer )
446     {
447         free( dvdcss->p_readv_buffer );
448         dvdcss->p_readv_buffer   = NULL;
449         dvdcss->i_readv_buf_size = 0;
450     }
451
452 #else
453     close( dvdcss->i_fd );
454
455 #endif
456
457     return 0;
458 }
459
460 static int _dvdcss_seek ( dvdcss_handle dvdcss, int i_blocks )
461 {
462 #if defined( WIN32 )
463     dvdcss->i_seekpos = i_blocks;
464
465     if( WIN2K )
466     {
467         LARGE_INTEGER li_read;
468
469 #ifndef INVALID_SET_FILE_POINTER
470 #define INVALID_SET_FILE_POINTER ((DWORD)-1)
471 #endif
472
473         li_read.QuadPart = (LONGLONG)i_blocks * DVDCSS_BLOCK_SIZE;
474
475         li_read.LowPart = SetFilePointer( (HANDLE) dvdcss->i_fd,
476                                           li_read.LowPart,
477                                           &li_read.HighPart, FILE_BEGIN );
478         if( (li_read.LowPart == INVALID_SET_FILE_POINTER)
479             && GetLastError() != NO_ERROR)
480         {
481             li_read.QuadPart = -DVDCSS_BLOCK_SIZE;
482         }
483
484         li_read.QuadPart /= DVDCSS_BLOCK_SIZE;
485         return (int)li_read.QuadPart;
486     }
487     else
488     {
489         return ( _win32_dvdcss_aseek( dvdcss->i_fd, i_blocks, SEEK_SET ) );
490     }
491 #else
492     off_t i_read;
493
494     dvdcss->i_seekpos = i_blocks;
495
496     i_read = lseek( dvdcss->i_fd,
497                     (off_t)i_blocks * (off_t)DVDCSS_BLOCK_SIZE, SEEK_SET );
498
499     return i_read / DVDCSS_BLOCK_SIZE;
500 #endif
501
502 }
503
504 static int _dvdcss_read ( dvdcss_handle dvdcss, void *p_buffer, int i_blocks )
505 {
506 #if defined( WIN32 ) 
507     if( WIN2K )
508     {
509         int i_bytes;
510
511         if( !ReadFile( (HANDLE) dvdcss->i_fd, p_buffer,
512                   i_blocks * DVDCSS_BLOCK_SIZE,
513                   (LPDWORD)&i_bytes, NULL ) )
514         {
515             return -1;
516         }
517         return i_bytes / DVDCSS_BLOCK_SIZE;
518     }
519     else
520     {
521         return _win32_dvdcss_aread( dvdcss->i_fd, p_buffer, i_blocks );
522     }
523
524 #else
525     int i_bytes;
526
527     i_bytes = read( dvdcss->i_fd, p_buffer,
528                     (size_t)i_blocks * DVDCSS_BLOCK_SIZE );
529     return i_bytes / DVDCSS_BLOCK_SIZE;
530 #endif
531
532 }
533
534 static int _dvdcss_readv ( dvdcss_handle dvdcss, struct iovec *p_iovec,
535                            int i_blocks )
536 {
537     int i_read;
538
539 #if defined( WIN32 )
540     /* Check the size of the readv temp buffer, just in case we need to
541      * realloc something bigger */
542     if( dvdcss->i_readv_buf_size < i_blocks * DVDCSS_BLOCK_SIZE )
543     {
544         dvdcss->i_readv_buf_size = i_blocks * DVDCSS_BLOCK_SIZE;
545
546         if( dvdcss->p_readv_buffer ) free( dvdcss->p_readv_buffer );
547
548         /* Allocate a buffer which will be used as a temporary storage
549          * for readv */
550         dvdcss->p_readv_buffer = malloc( dvdcss->i_readv_buf_size );
551         if( !dvdcss->p_readv_buffer )
552         {
553             _dvdcss_error( dvdcss, " failed (readv)" );
554             return -1;
555         }
556     }
557
558     i_read = _win32_dvdcss_readv( dvdcss->i_fd, p_iovec, i_blocks,
559                                   dvdcss->p_readv_buffer );
560     return i_read;
561
562 #else
563     i_read = readv( dvdcss->i_fd, p_iovec, i_blocks );
564     return i_read / DVDCSS_BLOCK_SIZE;
565
566 #endif
567 }
568
569
570 #if defined( WIN32 )
571
572 /*****************************************************************************
573  * _win32_dvdcss_readv: vectored read using ReadFile for Win2K and
574  *                      _win32_dvdcss_aread for win9x
575  *****************************************************************************/
576 static int _win32_dvdcss_readv( int i_fd, struct iovec *p_iovec,
577                                 int i_num_buffers, char *p_tmp_buffer )
578 {
579     int i_index;
580     int i_blocks, i_blocks_total = 0;
581
582     for( i_index = i_num_buffers; i_index; i_index-- )
583     {
584         i_blocks_total += p_iovec[i_index-1].iov_len; 
585     }
586
587     if( i_blocks_total <= 0 ) return 0;
588
589     i_blocks_total /= DVDCSS_BLOCK_SIZE;
590
591     if( WIN2K )
592     {
593         unsigned long int i_bytes;
594         if( !ReadFile( (HANDLE)i_fd, p_tmp_buffer,
595                        i_blocks_total * DVDCSS_BLOCK_SIZE, &i_bytes, NULL ) )
596         {
597             return -1;
598             /* The read failed... too bad.
599                As in the posix spec the file postition is left
600                unspecified after a failure */
601         }
602         i_blocks = i_bytes / DVDCSS_BLOCK_SIZE;
603     }
604     else /* Win9x */
605     {
606         i_blocks = _win32_dvdcss_aread( i_fd, p_tmp_buffer, i_blocks_total );
607         if( i_blocks < 0 )
608         {
609             return -1;  /* idem */
610         }
611     }
612
613     /* We just have to copy the content of the temp buffer into the iovecs */
614     i_index = 0;
615     i_blocks_total = i_blocks;
616     while( i_blocks_total > 0 )
617     {
618         memcpy( p_iovec[i_index].iov_base,
619                 &p_tmp_buffer[(i_blocks - i_blocks_total) * DVDCSS_BLOCK_SIZE],
620                 p_iovec[i_index].iov_len );
621         /* if we read less blocks than asked, we'll just end up copying
622            garbage, this isn't an issue as we return the number of
623            blocks actually read */
624         i_blocks_total -= ( p_iovec[i_index].iov_len / DVDCSS_BLOCK_SIZE );
625         i_index++;
626     } 
627
628     return i_blocks;
629 }
630
631 /*****************************************************************************
632  * _win32_dvdcss_aopen: open dvd drive (load aspi and init w32_aspidev
633  *                      structure)
634  *****************************************************************************/
635 static int _win32_dvdcss_aopen( char c_drive, dvdcss_handle dvdcss )
636 {
637     HMODULE hASPI;
638     DWORD dwSupportInfo;
639     struct w32_aspidev *fd;
640     int i, j, i_hostadapters;
641     long (*lpGetSupport)( void );
642     long (*lpSendCommand)( void* );
643      
644     hASPI = LoadLibrary( "wnaspi32.dll" );
645     if( hASPI == NULL )
646     {
647         _dvdcss_error( dvdcss, "unable to load wnaspi32.dll" );
648         return -1;
649     }
650
651     (FARPROC) lpGetSupport = GetProcAddress( hASPI, "GetASPI32SupportInfo" );
652     (FARPROC) lpSendCommand = GetProcAddress( hASPI, "SendASPI32Command" );
653  
654     if(lpGetSupport == NULL || lpSendCommand == NULL )
655     {
656         _dvdcss_debug( dvdcss, "unable to get aspi function pointers" );
657         FreeLibrary( hASPI );
658         return -1;
659     }
660
661     dwSupportInfo = lpGetSupport();
662
663     if( HIBYTE( LOWORD ( dwSupportInfo ) ) == SS_NO_ADAPTERS )
664     {
665         _dvdcss_debug( dvdcss, "no host adapters found (aspi)" );
666         FreeLibrary( hASPI );
667         return -1;
668     }
669
670     if( HIBYTE( LOWORD ( dwSupportInfo ) ) != SS_COMP )
671     {
672         _dvdcss_error( dvdcss, "unable to initalize aspi layer" );
673         FreeLibrary( hASPI );
674         return -1;
675     }
676
677     i_hostadapters = LOBYTE( LOWORD( dwSupportInfo ) );
678     if( i_hostadapters == 0 )
679     {
680         FreeLibrary( hASPI );
681         return -1;
682     }
683
684     fd = malloc( sizeof( struct w32_aspidev ) );
685     if( fd == NULL )
686     {
687         FreeLibrary( hASPI );
688         return -1;
689     }
690
691     fd->i_blocks = 0;
692     fd->hASPI = (long) hASPI;
693     fd->lpSendCommand = lpSendCommand;
694
695     c_drive = c_drive > 'Z' ? c_drive - 'a' : c_drive - 'A';
696
697     for( i = 0; i < i_hostadapters; i++ )
698     {
699         for( j = 0; j < 15; j++ )
700         {
701             struct SRB_GetDiskInfo srbDiskInfo;
702
703             srbDiskInfo.SRB_Cmd         = SC_GET_DISK_INFO;
704             srbDiskInfo.SRB_HaId        = i;
705             srbDiskInfo.SRB_Flags       = 0;
706             srbDiskInfo.SRB_Hdr_Rsvd    = 0;
707             srbDiskInfo.SRB_Target      = j;
708             srbDiskInfo.SRB_Lun         = 0;
709
710             lpSendCommand( (void*) &srbDiskInfo );
711
712             if( (srbDiskInfo.SRB_Status == SS_COMP) &&
713                 (srbDiskInfo.SRB_Int13HDriveInfo == c_drive) )
714             {
715                 fd->i_sid = MAKEWORD( i, j );
716                 return (int) fd;
717             }
718         }
719     }
720
721     free( (void*) fd );
722     FreeLibrary( hASPI );
723     _dvdcss_debug( dvdcss, "unable to get haid and target (aspi)" );
724     return( -1 );        
725 }
726
727 /*****************************************************************************
728  * _win32_dvdcss_aclose: close dvd drive (unload aspi and free w32_aspidev
729  *                       structure)
730  *****************************************************************************/
731 static int _win32_dvdcss_aclose( int i_fd )
732 {
733     struct w32_aspidev *fd = (struct w32_aspidev *) i_fd;
734
735     FreeLibrary( (HMODULE) fd->hASPI );
736     free( (void*) i_fd );
737
738     return 0;
739 }
740
741 /*****************************************************************************
742  * _win32_dvdcss_aseek: aspi version of _dvdcss_seek
743  * 
744  * returns the number of blocks read.
745  *****************************************************************************/
746 static int _win32_dvdcss_aseek( int i_fd, int i_blocks, int i_method )
747 {
748     int i_old_blocks;
749     char sz_buf[ DVDCSS_BLOCK_SIZE ];
750     struct w32_aspidev *fd = (struct w32_aspidev *) i_fd;
751     
752     i_old_blocks = fd->i_blocks;
753     fd->i_blocks = i_blocks;
754
755     if( _win32_dvdcss_aread( i_fd, sz_buf, 1 ) == -1 )
756     {
757         fd->i_blocks = i_old_blocks;
758         return -1;
759     }
760
761     (fd->i_blocks)--;
762
763     return fd->i_blocks;
764 }
765
766 /*****************************************************************************
767  * _win32_dvdcss_aread: aspi version of _dvdcss_read
768  *
769  * returns the number of blocks read.
770  *****************************************************************************/
771 static int _win32_dvdcss_aread( int i_fd, void *p_data, int i_blocks )
772 {
773     HANDLE hEvent;
774     struct SRB_ExecSCSICmd ssc;
775     struct w32_aspidev *fd = (struct w32_aspidev *) i_fd;
776
777     /* Create the transfer completion event */
778     hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
779     if( hEvent == NULL )
780     {
781         return -1;
782     }
783
784     memset( &ssc, 0, sizeof( ssc ) );
785
786     ssc.SRB_Cmd         = SC_EXEC_SCSI_CMD;
787     ssc.SRB_Flags       = SRB_DIR_IN | SRB_EVENT_NOTIFY;
788     ssc.SRB_HaId        = LOBYTE( fd->i_sid );
789     ssc.SRB_Target      = HIBYTE( fd->i_sid );
790     ssc.SRB_SenseLen    = SENSE_LEN;
791     
792     ssc.SRB_PostProc = (LPVOID) hEvent;
793     ssc.SRB_BufPointer  = p_data;
794     ssc.SRB_CDBLen      = 12;
795     
796     ssc.CDBByte[0]      = 0xA8; /* RAW */
797     ssc.CDBByte[2]      = (UCHAR) (fd->i_blocks >> 24);
798     ssc.CDBByte[3]      = (UCHAR) (fd->i_blocks >> 16) & 0xff;
799     ssc.CDBByte[4]      = (UCHAR) (fd->i_blocks >> 8) & 0xff;
800     ssc.CDBByte[5]      = (UCHAR) (fd->i_blocks) & 0xff;
801     
802     /* We have to break down the reads into 64kb pieces (ASPI restriction) */
803     if( i_blocks > 32 )
804     {
805         ssc.SRB_BufLen = 32 * DVDCSS_BLOCK_SIZE;
806         ssc.CDBByte[9] = 32;
807         fd->i_blocks  += 32;
808
809         /* Initiate transfer */  
810         ResetEvent( hEvent );
811         fd->lpSendCommand( (void*) &ssc );
812
813         /* transfer the next 64kb (_win32_dvdcss_aread is called recursively)
814          * We need to check the status of the read on return */
815         if( _win32_dvdcss_aread( i_fd, (u8*) p_data + 32 * DVDCSS_BLOCK_SIZE,
816                                  i_blocks - 32) < 0 )
817         {
818             return -1;
819         }
820     }
821     else
822     {
823         /* This is the last transfer */
824         ssc.SRB_BufLen   = i_blocks * DVDCSS_BLOCK_SIZE;
825         ssc.CDBByte[9]   = (UCHAR) i_blocks;
826         fd->i_blocks += i_blocks;
827
828         /* Initiate transfer */  
829         ResetEvent( hEvent );
830         fd->lpSendCommand( (void*) &ssc );
831
832     }
833
834     /* If the command has still not been processed, wait until it's finished */
835     if( ssc.SRB_Status == SS_PENDING )
836     {
837         WaitForSingleObject( hEvent, INFINITE );
838     }
839     CloseHandle( hEvent );
840
841     /* check that the transfer went as planned */
842     if( ssc.SRB_Status != SS_COMP )
843     {
844       return -1;
845     }
846
847     return i_blocks;
848 }
849
850 #endif
851