comparison libmpdvdkit2/device.c @ 16630:954cdf2171f6

upgrade to libdvdcss 1.2.9
author diego
date Sat, 01 Oct 2005 17:19:33 +0000
parents 80494fefb7ea
children 0af14c5fee82
comparison
equal deleted inserted replaced
16629:045e93202e4a 16630:954cdf2171f6
1 /***************************************************************************** 1 /*****************************************************************************
2 * device.h: DVD device access 2 * device.h: DVD device access
3 ***************************************************************************** 3 *****************************************************************************
4 * Copyright (C) 1998-2002 VideoLAN 4 * Copyright (C) 1998-2002 VideoLAN
5 *
6 * Modified for use with MPlayer, changes contained in libdvdcss_changes.diff.
7 * detailed CVS changelog at http://www.mplayerhq.hu/cgi-bin/cvsweb.cgi/main/
8 * $Id$ 5 * $Id$
9 * 6 *
10 * Authors: Stéphane Borel <stef@via.ecp.fr> 7 * Authors: Stéphane Borel <stef@via.ecp.fr>
11 * Samuel Hocevar <sam@zoy.org> 8 * Samuel Hocevar <sam@zoy.org>
12 * Håkan Hjort <d95hjort@dtek.chalmers.se> 9 * Håkan Hjort <d95hjort@dtek.chalmers.se>
32 #include "config.h" 29 #include "config.h"
33 30
34 #include <stdio.h> 31 #include <stdio.h>
35 #include <stdlib.h> 32 #include <stdlib.h>
36 #include <string.h> 33 #include <string.h>
34 # include <errno.h>
37 #include <sys/types.h> 35 #include <sys/types.h>
38 #include <sys/stat.h> 36 #include <sys/stat.h>
39 #include <fcntl.h> 37 #include <fcntl.h>
40 #include <unistd.h> 38 # include <unistd.h>
41 #include <limits.h> 39 # include <limits.h>
42 40
43 #if defined( WIN32 ) && !defined( SYS_CYGWIN ) 41 #if defined( WIN32 ) && !defined( SYS_CYGWIN )
44 # include <io.h> /* read() */ 42 # include <io.h> /* read() */
45 #else 43 #else
46 # include <sys/uio.h> /* struct iovec */ 44 # include <sys/uio.h> /* struct iovec */
127 #endif 125 #endif
128 } 126 }
129 127
130 int _dvdcss_open ( dvdcss_t dvdcss ) 128 int _dvdcss_open ( dvdcss_t dvdcss )
131 { 129 {
132 char psz_debug[200];
133 char const *psz_device = dvdcss->psz_device; 130 char const *psz_device = dvdcss->psz_device;
134 131
135 snprintf( psz_debug, 199, "opening target `%s'", psz_device ); 132 print_debug( dvdcss, "opening target `%s'", psz_device );
136 psz_debug[199] = '\0';
137 _dvdcss_debug( dvdcss, psz_debug );
138 133
139 #if defined( WIN32 ) 134 #if defined( WIN32 )
140 dvdcss->b_file = 1; 135 dvdcss->b_file = 1;
141 /* If device is "X:" or "X:\", we are not actually opening a file. */ 136 /* If device is "X:" or "X:\", we are not actually opening a file. */
142 if (psz_device[0] && psz_device[1] == ':' && 137 if (psz_device[0] && psz_device[1] == ':' &&
147 dvdcss->p_readv_buffer = NULL; 142 dvdcss->p_readv_buffer = NULL;
148 dvdcss->i_readv_buf_size = 0; 143 dvdcss->i_readv_buf_size = 0;
149 144
150 if( !dvdcss->b_file && WIN2K ) 145 if( !dvdcss->b_file && WIN2K )
151 { 146 {
152 _dvdcss_debug( dvdcss, "using Win2K API for access" ); 147 print_debug( dvdcss, "using Win2K API for access" );
153 dvdcss->pf_seek = win2k_seek; 148 dvdcss->pf_seek = win2k_seek;
154 dvdcss->pf_read = win2k_read; 149 dvdcss->pf_read = win2k_read;
155 dvdcss->pf_readv = win_readv; 150 dvdcss->pf_readv = win_readv;
156 return win2k_open( dvdcss, psz_device ); 151 return win2k_open( dvdcss, psz_device );
157 } 152 }
158 else if( !dvdcss->b_file ) 153 else if( !dvdcss->b_file )
159 { 154 {
160 _dvdcss_debug( dvdcss, "using ASPI for access" ); 155 print_debug( dvdcss, "using ASPI for access" );
161 dvdcss->pf_seek = aspi_seek; 156 dvdcss->pf_seek = aspi_seek;
162 dvdcss->pf_read = aspi_read; 157 dvdcss->pf_read = aspi_read;
163 dvdcss->pf_readv = win_readv; 158 dvdcss->pf_readv = win_readv;
164 return aspi_open( dvdcss, psz_device ); 159 return aspi_open( dvdcss, psz_device );
165 } 160 }
166 else 161 else
167 #endif 162 #endif
168 { 163 {
169 _dvdcss_debug( dvdcss, "using libc for access" ); 164 print_debug( dvdcss, "using libc for access" );
170 dvdcss->pf_seek = libc_seek; 165 dvdcss->pf_seek = libc_seek;
171 dvdcss->pf_read = libc_read; 166 dvdcss->pf_read = libc_read;
172 dvdcss->pf_readv = libc_readv; 167 dvdcss->pf_readv = libc_readv;
173 return libc_open( dvdcss, psz_device ); 168 return libc_open( dvdcss, psz_device );
174 } 169 }
179 { 174 {
180 dvdcss->i_raw_fd = open( psz_device, 0 ); 175 dvdcss->i_raw_fd = open( psz_device, 0 );
181 176
182 if( dvdcss->i_raw_fd == -1 ) 177 if( dvdcss->i_raw_fd == -1 )
183 { 178 {
184 _dvdcss_error( dvdcss, "failed opening raw device, continuing" ); 179 print_debug( dvdcss, "cannot open %s (%s)",
180 psz_device, strerror(errno) );
181 print_error( dvdcss, "failed to open raw device, but continuing" );
185 return -1; 182 return -1;
186 } 183 }
187 else 184 else
188 { 185 {
189 dvdcss->i_read_fd = dvdcss->i_raw_fd; 186 dvdcss->i_read_fd = dvdcss->i_raw_fd;
248 dvdcss->i_fd = dvdcss->i_read_fd = open( psz_device, O_BINARY ); 245 dvdcss->i_fd = dvdcss->i_read_fd = open( psz_device, O_BINARY );
249 #endif 246 #endif
250 247
251 if( dvdcss->i_fd == -1 ) 248 if( dvdcss->i_fd == -1 )
252 { 249 {
253 _dvdcss_error( dvdcss, "failed opening device" ); 250 print_debug( dvdcss, "cannot open %s (%s)",
251 psz_device, strerror(errno) );
252 print_error( dvdcss, "failed to open device" );
254 return -1; 253 return -1;
255 } 254 }
256 255
257 dvdcss->i_pos = 0; 256 dvdcss->i_pos = 0;
258 257
285 NULL, OPEN_EXISTING, 284 NULL, OPEN_EXISTING,
286 FILE_FLAG_RANDOM_ACCESS, NULL ); 285 FILE_FLAG_RANDOM_ACCESS, NULL );
287 286
288 if( (HANDLE) dvdcss->i_fd == INVALID_HANDLE_VALUE ) 287 if( (HANDLE) dvdcss->i_fd == INVALID_HANDLE_VALUE )
289 { 288 {
290 _dvdcss_error( dvdcss, "failed opening device" ); 289 print_error( dvdcss, "failed opening device" );
291 return -1; 290 return -1;
292 } 291 }
293 292
294 dvdcss->i_pos = 0; 293 dvdcss->i_pos = 0;
295 294
308 307
309 /* load aspi and init w32_aspidev structure */ 308 /* load aspi and init w32_aspidev structure */
310 hASPI = LoadLibrary( "wnaspi32.dll" ); 309 hASPI = LoadLibrary( "wnaspi32.dll" );
311 if( hASPI == NULL ) 310 if( hASPI == NULL )
312 { 311 {
313 _dvdcss_error( dvdcss, "unable to load wnaspi32.dll" ); 312 print_error( dvdcss, "unable to load wnaspi32.dll" );
314 return -1; 313 return -1;
315 } 314 }
316 315
317 lpGetSupport = (GETASPI32SUPPORTINFO) GetProcAddress( hASPI, "GetASPI32SupportInfo" ); 316 lpGetSupport = (GETASPI32SUPPORTINFO) GetProcAddress( hASPI, "GetASPI32SupportInfo" );
318 lpSendCommand = (SENDASPI32COMMAND) GetProcAddress( hASPI, "SendASPI32Command" ); 317 lpSendCommand = (SENDASPI32COMMAND) GetProcAddress( hASPI, "SendASPI32Command" );
319 318
320 if(lpGetSupport == NULL || lpSendCommand == NULL ) 319 if(lpGetSupport == NULL || lpSendCommand == NULL )
321 { 320 {
322 _dvdcss_error( dvdcss, "unable to get aspi function pointers" ); 321 print_error( dvdcss, "unable to get aspi function pointers" );
323 FreeLibrary( hASPI ); 322 FreeLibrary( hASPI );
324 return -1; 323 return -1;
325 } 324 }
326 325
327 dwSupportInfo = lpGetSupport(); 326 dwSupportInfo = lpGetSupport();
328 327
329 if( HIBYTE( LOWORD ( dwSupportInfo ) ) == SS_NO_ADAPTERS ) 328 if( HIBYTE( LOWORD ( dwSupportInfo ) ) == SS_NO_ADAPTERS )
330 { 329 {
331 _dvdcss_error( dvdcss, "no ASPI adapters found" ); 330 print_error( dvdcss, "no ASPI adapters found" );
332 FreeLibrary( hASPI ); 331 FreeLibrary( hASPI );
333 return -1; 332 return -1;
334 } 333 }
335 334
336 if( HIBYTE( LOWORD ( dwSupportInfo ) ) != SS_COMP ) 335 if( HIBYTE( LOWORD ( dwSupportInfo ) ) != SS_COMP )
337 { 336 {
338 _dvdcss_error( dvdcss, "unable to initalize aspi layer" ); 337 print_error( dvdcss, "unable to initalize aspi layer" );
339 FreeLibrary( hASPI ); 338 FreeLibrary( hASPI );
340 return -1; 339 return -1;
341 } 340 }
342 341
343 i_hostadapters = LOBYTE( LOWORD( dwSupportInfo ) ); 342 i_hostadapters = LOBYTE( LOWORD( dwSupportInfo ) );
344 if( i_hostadapters == 0 ) 343 if( i_hostadapters == 0 )
345 { 344 {
346 _dvdcss_error( dvdcss, "no ASPI adapters ready" ); 345 print_error( dvdcss, "no ASPI adapters ready" );
347 FreeLibrary( hASPI ); 346 FreeLibrary( hASPI );
348 return -1; 347 return -1;
349 } 348 }
350 349
351 fd = malloc( sizeof( struct w32_aspidev ) ); 350 fd = malloc( sizeof( struct w32_aspidev ) );
352 if( fd == NULL ) 351 if( fd == NULL )
353 { 352 {
354 _dvdcss_error( dvdcss, "not enough memory" ); 353 print_error( dvdcss, "not enough memory" );
355 FreeLibrary( hASPI ); 354 FreeLibrary( hASPI );
356 return -1; 355 return -1;
357 } 356 }
358 357
359 fd->i_blocks = 0; 358 fd->i_blocks = 0;
400 } 399 }
401 else 400 else
402 { 401 {
403 free( (void*) fd ); 402 free( (void*) fd );
404 FreeLibrary( hASPI ); 403 FreeLibrary( hASPI );
405 _dvdcss_error( dvdcss,"this is not a cdrom drive" ); 404 print_error( dvdcss,"this is not a cdrom drive" );
406 return -1; 405 return -1;
407 } 406 }
408 } 407 }
409 } 408 }
410 } 409 }
411 410
412 free( (void*) fd ); 411 free( (void*) fd );
413 FreeLibrary( hASPI ); 412 FreeLibrary( hASPI );
414 _dvdcss_error( dvdcss, "unable to get haid and target (aspi)" ); 413 print_error( dvdcss, "unable to get haid and target (aspi)" );
415 return -1; 414 return -1;
416 } 415 }
417 #endif 416 #endif
418 417
419 /***************************************************************************** 418 /*****************************************************************************
427 { 426 {
428 /* We are already in position */ 427 /* We are already in position */
429 return i_blocks; 428 return i_blocks;
430 } 429 }
431 430
432 i_seek = lseek( dvdcss->i_read_fd, 431 i_seek = (off_t)i_blocks * (off_t)DVDCSS_BLOCK_SIZE;
433 (off_t)i_blocks * (off_t)DVDCSS_BLOCK_SIZE, SEEK_SET ); 432 i_seek = lseek( dvdcss->i_read_fd, i_seek, SEEK_SET );
434 433
435 if( i_seek < 0 ) 434 if( i_seek < 0 )
436 { 435 {
437 _dvdcss_error( dvdcss, "seek error" ); 436 print_error( dvdcss, "seek error" );
438 dvdcss->i_pos = -1; 437 dvdcss->i_pos = -1;
439 return i_seek; 438 return i_seek;
440 } 439 }
441 440
442 dvdcss->i_pos = i_seek / DVDCSS_BLOCK_SIZE; 441 dvdcss->i_pos = i_seek / DVDCSS_BLOCK_SIZE;
509 /***************************************************************************** 508 /*****************************************************************************
510 * Read commands. 509 * Read commands.
511 *****************************************************************************/ 510 *****************************************************************************/
512 static int libc_read ( dvdcss_t dvdcss, void *p_buffer, int i_blocks ) 511 static int libc_read ( dvdcss_t dvdcss, void *p_buffer, int i_blocks )
513 { 512 {
514 off_t i_ret; 513 off_t i_size, i_ret;
515 514
516 i_ret = read( dvdcss->i_read_fd, p_buffer, 515 i_size = (off_t)i_blocks * (off_t)DVDCSS_BLOCK_SIZE;
517 (off_t)i_blocks * DVDCSS_BLOCK_SIZE ); 516 i_ret = read( dvdcss->i_read_fd, p_buffer, i_size );
518 517
519 if( i_ret < 0 ) 518 if( i_ret < 0 )
520 { 519 {
521 _dvdcss_error( dvdcss, "read error" ); 520 print_error( dvdcss, "read error" );
522 dvdcss->i_pos = -1; 521 dvdcss->i_pos = -1;
523 return i_ret; 522 return i_ret;
524 } 523 }
525 524
526 /* Handle partial reads */ 525 /* Handle partial reads */
527 if( i_ret != (off_t)i_blocks * DVDCSS_BLOCK_SIZE ) 526 if( i_ret != i_size )
528 { 527 {
529 int i_seek; 528 int i_seek;
530 529
531 dvdcss->i_pos = -1; 530 dvdcss->i_pos = -1;
532 i_seek = libc_seek( dvdcss, i_ret / DVDCSS_BLOCK_SIZE ); 531 i_seek = libc_seek( dvdcss, i_ret / DVDCSS_BLOCK_SIZE );
665 /* Allocate a buffer which will be used as a temporary storage 664 /* Allocate a buffer which will be used as a temporary storage
666 * for readv */ 665 * for readv */
667 dvdcss->p_readv_buffer = malloc( dvdcss->i_readv_buf_size ); 666 dvdcss->p_readv_buffer = malloc( dvdcss->i_readv_buf_size );
668 if( !dvdcss->p_readv_buffer ) 667 if( !dvdcss->p_readv_buffer )
669 { 668 {
670 _dvdcss_error( dvdcss, " failed (readv)" ); 669 print_error( dvdcss, " failed (readv)" );
671 dvdcss->i_pos = -1; 670 dvdcss->i_pos = -1;
672 return -1; 671 return -1;
673 } 672 }
674 } 673 }
675 674