1 /****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 1998-2001 the VideoLAN team
7 * Authors: Johan Bilien <jobi@via.ecp.fr>
8 * Gildas Bazin <gbazin@netcourrier.com>
9 * Jon Lech Johansen <jon-vl@nanocrew.net>
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
40 #include <sys/types.h>
45 #ifdef HAVE_SYS_IOCTL_H
46 # include <sys/ioctl.h>
49 #if defined( SYS_BSDI )
51 #elif defined ( __APPLE__ )
52 # include <CoreFoundation/CFBase.h>
53 # include <IOKit/IOKitLib.h>
54 # include <IOKit/storage/IOCDTypes.h>
55 # include <IOKit/storage/IOCDMedia.h>
56 # include <IOKit/storage/IOCDMediaBSDClient.h>
57 #elif defined( HAVE_SCSIREQ_IN_SYS_SCSIIO_H )
58 # include <sys/inttypes.h>
59 # include <sys/cdio.h>
60 # include <sys/scsiio.h>
61 #elif defined( HAVE_IOC_TOC_HEADER_IN_SYS_CDIO_H )
62 # include <sys/cdio.h>
63 # include <sys/cdrio.h>
64 #elif defined( WIN32 )
66 # include <winioctl.h>
68 # include <linux/cdrom.h>
71 #include "cdrom_internals.h"
74 /*****************************************************************************
75 * ioctl_Open: Opens a VCD device or file and returns an opaque handle
76 *****************************************************************************/
77 vcddev_t *ioctl_Open( vlc_object_t *p_this, const char *psz_dev )
86 if( !psz_dev ) return NULL;
89 * Initialize structure with default values
91 p_vcddev = (vcddev_t *)malloc( sizeof(vcddev_t) );
92 if( p_vcddev == NULL )
94 msg_Err( p_this, "out of memory" );
97 p_vcddev->i_vcdimage_handle = -1;
98 p_vcddev->psz_dev = NULL;
102 * Check if we are dealing with a device or a file (vcd image)
105 if( (strlen( psz_dev ) == 2 && psz_dev[1] == ':') )
111 if( stat( psz_dev, &fileinfo ) < 0 )
117 /* Check if this is a block/char device */
118 if( S_ISBLK( fileinfo.st_mode ) || S_ISCHR( fileinfo.st_mode ) )
124 i_ret = OpenVCDImage( p_this, psz_dev, p_vcddev );
129 * open the vcd device
133 i_ret = win32_vcd_open( p_this, psz_dev, p_vcddev );
135 p_vcddev->i_device_handle = -1;
136 p_vcddev->i_device_handle = open( psz_dev, O_RDONLY | O_NONBLOCK );
137 i_ret = (p_vcddev->i_device_handle == -1) ? -1 : 0;
143 p_vcddev->psz_dev = (char *)strdup( psz_dev );
154 /*****************************************************************************
155 * ioctl_Close: Closes an already opened VCD device or file.
156 *****************************************************************************/
157 void ioctl_Close( vlc_object_t * p_this, vcddev_t *p_vcddev )
159 if( p_vcddev->psz_dev ) free( p_vcddev->psz_dev );
161 if( p_vcddev->i_vcdimage_handle != -1 )
167 CloseVCDImage( p_this, p_vcddev );
176 if( p_vcddev->h_device_handle )
177 CloseHandle( p_vcddev->h_device_handle );
178 if( p_vcddev->hASPI )
179 FreeLibrary( (HMODULE)p_vcddev->hASPI );
181 if( p_vcddev->i_device_handle != -1 )
182 close( p_vcddev->i_device_handle );
186 /*****************************************************************************
187 * ioctl_GetTracksMap: Read the Table of Content, fill in the pp_sectors map
188 * if pp_sectors is not null and return the number of
190 *****************************************************************************/
191 int ioctl_GetTracksMap( vlc_object_t *p_this, const vcddev_t *p_vcddev,
196 if( p_vcddev->i_vcdimage_handle != -1 )
202 i_tracks = p_vcddev->i_tracks;
206 *pp_sectors = malloc( (i_tracks + 1) * sizeof(int) );
207 if( *pp_sectors == NULL )
209 msg_Err( p_this, "out of memory" );
212 memcpy( *pp_sectors, p_vcddev->p_sectors,
213 (i_tracks + 1) * sizeof(int) );
225 #if defined( __APPLE__ )
230 if( ( pTOC = darwin_getTOC( p_this, p_vcddev ) ) == NULL )
232 msg_Err( p_this, "failed to get the TOC" );
236 i_descriptors = darwin_getNumberOfDescriptors( pTOC );
237 i_tracks = darwin_getNumberOfTracks( pTOC, i_descriptors );
241 int i, i_leadout = -1;
242 CDTOCDescriptor *pTrackDescriptors;
245 *pp_sectors = malloc( (i_tracks + 1) * sizeof(int) );
246 if( *pp_sectors == NULL )
248 msg_Err( p_this, "out of memory" );
249 darwin_freeTOC( pTOC );
253 pTrackDescriptors = pTOC->descriptors;
255 for( i_tracks = 0, i = 0; i <= i_descriptors; i++ )
257 track = pTrackDescriptors[i].point;
262 if( track > CD_MAX_TRACK_NO || track < CD_MIN_TRACK_NO )
265 (*pp_sectors)[i_tracks++] =
266 CDConvertMSFToLBA( pTrackDescriptors[i].p );
269 if( i_leadout == -1 )
271 msg_Err( p_this, "leadout not found" );
273 darwin_freeTOC( pTOC );
277 /* set leadout sector */
278 (*pp_sectors)[i_tracks] =
279 CDConvertMSFToLBA( pTrackDescriptors[i_leadout].p );
282 darwin_freeTOC( pTOC );
284 #elif defined( WIN32 )
285 if( p_vcddev->hASPI )
288 struct SRB_ExecSCSICmd ssc;
289 byte_t p_tocheader[ 4 ];
291 /* Create the transfer completion event */
292 hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
298 memset( &ssc, 0, sizeof( ssc ) );
300 ssc.SRB_Cmd = SC_EXEC_SCSI_CMD;
301 ssc.SRB_Flags = SRB_DIR_IN | SRB_EVENT_NOTIFY;
302 ssc.SRB_HaId = LOBYTE( p_vcddev->i_sid );
303 ssc.SRB_Target = HIBYTE( p_vcddev->i_sid );
304 ssc.SRB_SenseLen = SENSE_LEN;
306 ssc.SRB_PostProc = (LPVOID) hEvent;
310 ssc.CDBByte[ 0 ] = READ_TOC;
313 ssc.CDBByte[ 2 ] = READ_TOC_FORMAT_TOC;
316 ssc.CDBByte[ 6 ] = 0;
318 /* Allocation length and buffer */
319 ssc.SRB_BufLen = sizeof( p_tocheader );
320 ssc.SRB_BufPointer = p_tocheader;
321 ssc.CDBByte[ 7 ] = ( ssc.SRB_BufLen >> 8 ) & 0xff;
322 ssc.CDBByte[ 8 ] = ( ssc.SRB_BufLen ) & 0xff;
324 /* Initiate transfer */
325 ResetEvent( hEvent );
326 p_vcddev->lpSendCommand( (void*) &ssc );
328 /* If the command has still not been processed, wait until it's
330 if( ssc.SRB_Status == SS_PENDING )
331 WaitForSingleObject( hEvent, INFINITE );
333 /* check that the transfer went as planned */
334 if( ssc.SRB_Status != SS_COMP )
336 CloseHandle( hEvent );
340 i_tracks = p_tocheader[3] - p_tocheader[2] + 1;
347 i_toclength = 4 /* header */ + p_tocheader[0] +
348 ((unsigned int)p_tocheader[1] << 8);
350 p_fulltoc = malloc( i_toclength );
351 *pp_sectors = malloc( (i_tracks + 1) * sizeof(int) );
353 if( *pp_sectors == NULL || p_fulltoc == NULL )
355 if( *pp_sectors ) free( *pp_sectors );
356 if( p_fulltoc ) free( p_fulltoc );
357 msg_Err( p_this, "out of memory" );
358 CloseHandle( hEvent );
362 /* Allocation length and buffer */
363 ssc.SRB_BufLen = i_toclength;
364 ssc.SRB_BufPointer = p_fulltoc;
365 ssc.CDBByte[ 7 ] = ( ssc.SRB_BufLen >> 8 ) & 0xff;
366 ssc.CDBByte[ 8 ] = ( ssc.SRB_BufLen ) & 0xff;
368 /* Initiate transfer */
369 ResetEvent( hEvent );
370 p_vcddev->lpSendCommand( (void*) &ssc );
372 /* If the command has still not been processed, wait until it's
374 if( ssc.SRB_Status == SS_PENDING )
375 WaitForSingleObject( hEvent, INFINITE );
377 /* check that the transfer went as planned */
378 if( ssc.SRB_Status != SS_COMP )
381 for( i = 0 ; i <= i_tracks ; i++ )
383 int i_index = 8 + 8 * i;
384 (*pp_sectors)[ i ] = ((int)p_fulltoc[ i_index ] << 24) +
385 ((int)p_fulltoc[ i_index+1 ] << 16) +
386 ((int)p_fulltoc[ i_index+2 ] << 8) +
387 (int)p_fulltoc[ i_index+3 ];
389 msg_Dbg( p_this, "p_sectors: %i, %i", i, (*pp_sectors)[i]);
395 CloseHandle( hEvent );
400 DWORD dwBytesReturned;
403 if( DeviceIoControl( p_vcddev->h_device_handle,
404 IOCTL_CDROM_READ_TOC,
405 NULL, 0, &cdrom_toc, sizeof(CDROM_TOC),
406 &dwBytesReturned, NULL ) == 0 )
408 msg_Err( p_this, "could not read TOCHDR" );
412 i_tracks = cdrom_toc.LastTrack - cdrom_toc.FirstTrack + 1;
418 *pp_sectors = malloc( (i_tracks + 1) * sizeof(int) );
419 if( *pp_sectors == NULL )
421 msg_Err( p_this, "out of memory" );
425 for( i = 0 ; i <= i_tracks ; i++ )
427 (*pp_sectors)[ i ] = MSF_TO_LBA2(
428 cdrom_toc.TrackData[i].Address[1],
429 cdrom_toc.TrackData[i].Address[2],
430 cdrom_toc.TrackData[i].Address[3] );
431 msg_Dbg( p_this, "p_sectors: %i, %i", i, (*pp_sectors)[i]);
436 #elif defined( HAVE_IOC_TOC_HEADER_IN_SYS_CDIO_H ) \
437 || defined( HAVE_SCSIREQ_IN_SYS_SCSIIO_H )
438 struct ioc_toc_header tochdr;
439 struct ioc_read_toc_entry toc_entries;
441 if( ioctl( p_vcddev->i_device_handle, CDIOREADTOCHEADER, &tochdr )
444 msg_Err( p_this, "could not read TOCHDR" );
448 i_tracks = tochdr.ending_track - tochdr.starting_track + 1;
454 *pp_sectors = malloc( (i_tracks + 1) * sizeof(int) );
455 if( *pp_sectors == NULL )
457 msg_Err( p_this, "out of memory" );
461 toc_entries.address_format = CD_LBA_FORMAT;
462 toc_entries.starting_track = 0;
463 toc_entries.data_len = ( i_tracks + 1 ) *
464 sizeof( struct cd_toc_entry );
465 toc_entries.data = (struct cd_toc_entry *)
466 malloc( toc_entries.data_len );
467 if( toc_entries.data == NULL )
469 msg_Err( p_this, "out of memory" );
475 if( ioctl( p_vcddev->i_device_handle, CDIOREADTOCENTRYS,
476 &toc_entries ) == -1 )
478 msg_Err( p_this, "could not read the TOC" );
480 free( toc_entries.data );
484 /* Fill the p_sectors structure with the track/sector matches */
485 for( i = 0 ; i <= i_tracks ; i++ )
487 #if defined( HAVE_SCSIREQ_IN_SYS_SCSIIO_H )
488 /* FIXME: is this ok? */
489 (*pp_sectors)[ i ] = toc_entries.data[i].addr.lba;
491 (*pp_sectors)[ i ] = ntohl( toc_entries.data[i].addr.lba );
496 struct cdrom_tochdr tochdr;
497 struct cdrom_tocentry tocent;
499 /* First we read the TOC header */
500 if( ioctl( p_vcddev->i_device_handle, CDROMREADTOCHDR, &tochdr )
503 msg_Err( p_this, "could not read TOCHDR" );
507 i_tracks = tochdr.cdth_trk1 - tochdr.cdth_trk0 + 1;
513 *pp_sectors = malloc( (i_tracks + 1) * sizeof(int) );
514 if( *pp_sectors == NULL )
516 msg_Err( p_this, "out of memory" );
520 /* Fill the p_sectors structure with the track/sector matches */
521 for( i = 0 ; i <= i_tracks ; i++ )
523 tocent.cdte_format = CDROM_LBA;
525 ( i == i_tracks ) ? CDROM_LEADOUT : tochdr.cdth_trk0 + i;
527 if( ioctl( p_vcddev->i_device_handle, CDROMREADTOCENTRY,
530 msg_Err( p_this, "could not read TOCENTRY" );
535 (*pp_sectors)[ i ] = tocent.cdte_addr.lba;
544 /****************************************************************************
545 * ioctl_ReadSector: Read VCD or CDDA sectors
546 ****************************************************************************/
547 int ioctl_ReadSectors( vlc_object_t *p_this, const vcddev_t *p_vcddev,
548 int i_sector, byte_t * p_buffer, int i_nb, int i_type )
553 if( i_type == VCD_TYPE ) p_block = malloc( VCD_SECTOR_SIZE * i_nb );
554 else p_block = p_buffer;
556 if( p_vcddev->i_vcdimage_handle != -1 )
561 if( lseek( p_vcddev->i_vcdimage_handle, i_sector * VCD_SECTOR_SIZE,
564 msg_Err( p_this, "Could not lseek to sector %d", i_sector );
565 if( i_type == VCD_TYPE ) free( p_block );
569 if( read( p_vcddev->i_vcdimage_handle, p_block, VCD_SECTOR_SIZE * i_nb)
572 msg_Err( p_this, "Could not read sector %d", i_sector );
573 if( i_type == VCD_TYPE ) free( p_block );
585 #if defined( __APPLE__ )
586 dk_cd_read_t cd_read;
588 memset( &cd_read, 0, sizeof(cd_read) );
590 cd_read.offset = i_sector * VCD_SECTOR_SIZE;
591 cd_read.sectorArea = kCDSectorAreaSync | kCDSectorAreaHeader |
592 kCDSectorAreaSubHeader | kCDSectorAreaUser |
593 kCDSectorAreaAuxiliary;
594 cd_read.sectorType = kCDSectorTypeUnknown;
596 cd_read.buffer = p_block;
597 cd_read.bufferLength = VCD_SECTOR_SIZE * i_nb;
599 if( ioctl( p_vcddev->i_device_handle, DKIOCCDREAD, &cd_read ) == -1 )
601 msg_Err( p_this, "could not read block %d", i_sector );
602 if( i_type == VCD_TYPE ) free( p_block );
606 #elif defined( WIN32 )
607 if( p_vcddev->hASPI )
610 struct SRB_ExecSCSICmd ssc;
612 /* Create the transfer completion event */
613 hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
616 if( i_type == VCD_TYPE ) free( p_block );
620 memset( &ssc, 0, sizeof( ssc ) );
622 ssc.SRB_Cmd = SC_EXEC_SCSI_CMD;
623 ssc.SRB_Flags = SRB_DIR_IN | SRB_EVENT_NOTIFY;
624 ssc.SRB_HaId = LOBYTE( p_vcddev->i_sid );
625 ssc.SRB_Target = HIBYTE( p_vcddev->i_sid );
626 ssc.SRB_SenseLen = SENSE_LEN;
628 ssc.SRB_PostProc = (LPVOID) hEvent;
632 ssc.CDBByte[ 0 ] = READ_CD;
635 ssc.CDBByte[ 1 ] = i_type == VCD_TYPE ? SECTOR_TYPE_MODE2_FORM2 :
639 ssc.CDBByte[ 2 ] = ( i_sector >> 24 ) & 0xff;
640 ssc.CDBByte[ 3 ] = ( i_sector >> 16 ) & 0xff;
641 ssc.CDBByte[ 4 ] = ( i_sector >> 8 ) & 0xff;
642 ssc.CDBByte[ 5 ] = ( i_sector ) & 0xff;
644 /* Transfer length */
645 ssc.CDBByte[ 6 ] = ( i_nb >> 16 ) & 0xff;
646 ssc.CDBByte[ 7 ] = ( i_nb >> 8 ) & 0xff;
647 ssc.CDBByte[ 8 ] = ( i_nb ) & 0xff;
650 ssc.CDBByte[ 9 ] = i_type == VCD_TYPE ? READ_CD_RAW_MODE2 :
654 ssc.SRB_BufPointer = p_block;
655 ssc.SRB_BufLen = VCD_SECTOR_SIZE * i_nb;
657 /* Initiate transfer */
658 ResetEvent( hEvent );
659 p_vcddev->lpSendCommand( (void*) &ssc );
661 /* If the command has still not been processed, wait until it's
663 if( ssc.SRB_Status == SS_PENDING )
665 WaitForSingleObject( hEvent, INFINITE );
667 CloseHandle( hEvent );
669 /* check that the transfer went as planned */
670 if( ssc.SRB_Status != SS_COMP )
672 if( i_type == VCD_TYPE ) free( p_block );
678 DWORD dwBytesReturned;
679 RAW_READ_INFO cdrom_raw;
681 /* Initialize CDROM_RAW_READ structure */
682 cdrom_raw.DiskOffset.QuadPart = CD_SECTOR_SIZE * i_sector;
683 cdrom_raw.SectorCount = i_nb;
684 cdrom_raw.TrackMode = i_type == VCD_TYPE ? XAForm2 : CDDA;
686 if( DeviceIoControl( p_vcddev->h_device_handle,
687 IOCTL_CDROM_RAW_READ, &cdrom_raw,
688 sizeof(RAW_READ_INFO), p_block,
689 VCD_SECTOR_SIZE * i_nb, &dwBytesReturned,
692 if( i_type == VCD_TYPE )
694 /* Retry in YellowMode2 */
695 cdrom_raw.TrackMode = YellowMode2;
696 if( DeviceIoControl( p_vcddev->h_device_handle,
697 IOCTL_CDROM_RAW_READ, &cdrom_raw,
698 sizeof(RAW_READ_INFO), p_block,
699 VCD_SECTOR_SIZE * i_nb, &dwBytesReturned,
710 #elif defined( HAVE_SCSIREQ_IN_SYS_SCSIIO_H )
714 memset( &sc, 0, sizeof(sc) );
716 sc.cmd[1] = i_type == VCD_TYPE ? SECTOR_TYPE_MODE2_FORM2:
718 sc.cmd[2] = (i_sector >> 24) & 0xff;
719 sc.cmd[3] = (i_sector >> 16) & 0xff;
720 sc.cmd[4] = (i_sector >> 8) & 0xff;
721 sc.cmd[5] = (i_sector >> 0) & 0xff;
722 sc.cmd[6] = (i_nb >> 16) & 0xff;
723 sc.cmd[7] = (i_nb >> 8) & 0xff;
724 sc.cmd[8] = (i_nb ) & 0xff;
725 sc.cmd[9] = i_type == VCD_TYPE ? READ_CD_RAW_MODE2 : READ_CD_USERDATA;
726 sc.cmd[10] = 0; /* sub channel */
728 sc.databuf = (caddr_t)p_block;
729 sc.datalen = VCD_SECTOR_SIZE * i_nb;
730 sc.senselen = sizeof( sc.sense );
731 sc.flags = SCCMD_READ;
734 i_ret = ioctl( i_fd, SCIOCCOMMAND, &sc );
737 msg_Err( p_this, "SCIOCCOMMAND failed" );
738 if( i_type == VCD_TYPE ) free( p_block );
741 if( sc.retsts || sc.error )
743 msg_Err( p_this, "SCSI command failed: status %d error %d\n",
744 sc.retsts, sc.error );
745 if( i_type == VCD_TYPE ) free( p_block );
749 #elif defined( HAVE_IOC_TOC_HEADER_IN_SYS_CDIO_H )
750 int i_size = VCD_SECTOR_SIZE;
752 if( ioctl( p_vcddev->i_device_handle, CDRIOCSETBLOCKSIZE, &i_size )
755 msg_Err( p_this, "Could not set block size" );
756 if( i_type == VCD_TYPE ) free( p_block );
760 if( lseek( p_vcddev->i_device_handle,
761 i_sector * VCD_SECTOR_SIZE, SEEK_SET ) == -1 )
763 msg_Err( p_this, "Could not lseek to sector %d", i_sector );
764 if( i_type == VCD_TYPE ) free( p_block );
768 if( read( p_vcddev->i_device_handle,
769 p_block, VCD_SECTOR_SIZE * i_nb ) == -1 )
771 msg_Err( p_this, "Could not read sector %d", i_sector );
772 if( i_type == VCD_TYPE ) free( p_block );
777 for( i = 0; i < i_nb; i++ )
779 int i_dummy = i_sector + i + 2 * CD_FRAMES;
781 #define p_msf ((struct cdrom_msf0 *)(p_block + i * VCD_SECTOR_SIZE))
782 p_msf->minute = i_dummy / (CD_FRAMES * CD_SECS);
783 p_msf->second = ( i_dummy % (CD_FRAMES * CD_SECS) ) / CD_FRAMES;
784 p_msf->frame = ( i_dummy % (CD_FRAMES * CD_SECS) ) % CD_FRAMES;
787 if( ioctl( p_vcddev->i_device_handle, CDROMREADRAW,
788 p_block + i * VCD_SECTOR_SIZE ) == -1 )
790 msg_Err( p_this, "could not read block %i from disc",
795 if( i_type == VCD_TYPE ) free( p_block );
804 /* For VCDs, we don't want to keep the header and footer of the
806 if( i_type == VCD_TYPE )
808 for( i = 0; i < i_nb; i++ )
810 memcpy( p_buffer + i * VCD_DATA_SIZE,
811 p_block + i * VCD_SECTOR_SIZE + VCD_DATA_START,
820 /****************************************************************************
822 ****************************************************************************/
824 /****************************************************************************
825 * OpenVCDImage: try to open a vcd image from a .cue file
826 ****************************************************************************/
827 static int OpenVCDImage( vlc_object_t * p_this, const char *psz_dev,
832 char *psz_vcdfile = NULL;
833 char *psz_cuefile = NULL;
837 /* Check if we are dealing with a .cue file */
838 p_pos = strrchr( psz_dev, '.' );
839 if( p_pos && !strcmp( p_pos, ".cue" ) )
841 psz_cuefile = strdup( psz_dev );
845 /* psz_dev must be the actual vcd file. Let's assume there's a .cue
846 * file with the same filename */
849 psz_cuefile = malloc( p_pos - psz_dev + 5 /* ".cue" */ );
850 strncpy( psz_cuefile, psz_dev, p_pos - psz_dev );
851 strcpy( psz_cuefile + (p_pos - psz_dev), ".cue");
855 psz_cuefile = malloc( strlen(psz_dev) + 5 /* ".cue" */ );
856 sprintf( psz_cuefile, "%s.cue", psz_dev );
860 /* Open the cue file and try to parse it */
861 msg_Dbg( p_this,"trying .cue file: %s", psz_cuefile );
862 cuefile = utf8_fopen( psz_cuefile, "rt" );
863 if( cuefile && fscanf( cuefile, "FILE %c", line ) &&
864 fgets( line, 1024, cuefile ) )
866 p_pos = strchr( line, '"' );
871 /* Take care of path standardization */
872 if( *line != '/' && ((p_pos = strrchr( psz_cuefile, '/' ))
873 || (p_pos = strrchr( psz_cuefile, '\\' ) )) )
875 psz_vcdfile = malloc( strlen(line) +
876 (p_pos - psz_cuefile + 1) + 1 );
877 strncpy( psz_vcdfile, psz_cuefile, (p_pos - psz_cuefile + 1) );
878 strcpy( psz_vcdfile + (p_pos - psz_cuefile + 1), line );
880 else psz_vcdfile = strdup( line );
886 msg_Dbg( p_this,"using vcd image file: %s", psz_vcdfile );
887 p_vcddev->i_vcdimage_handle = open( psz_vcdfile,
888 O_RDONLY | O_NONBLOCK | O_BINARY );
889 i_ret = (p_vcddev->i_vcdimage_handle == -1) ? -1 : 0;
892 /* Try to parse the i_tracks and p_sectors info so we can just forget
893 * about the cuefile */
901 while( fgets( line, 1024, cuefile ) )
903 /* look for a TRACK line */
904 if( !sscanf( line, "%9s", psz_dummy ) ||
905 strcmp(psz_dummy, "TRACK") )
908 /* look for an INDEX line */
909 while( fgets( line, 1024, cuefile ) )
911 int i_min, i_sec, i_frame;
913 if( (sscanf( line, "%9s %2u %2u:%2u:%2u", psz_dummy, &i_num,
914 &i_min, &i_sec, &i_frame ) != 5) || (i_num != 1) )
918 p_sectors[i_tracks - 1] = MSF_TO_LBA(i_min, i_sec, i_frame);
919 msg_Dbg( p_this, "vcd track %i begins at sector:%i",
920 i_tracks - 1, p_sectors[i_tracks - 1] );
925 /* fill in the last entry */
926 p_sectors[i_tracks] = lseek(p_vcddev->i_vcdimage_handle, 0, SEEK_END)
928 msg_Dbg( p_this, "vcd track %i, begins at sector:%i",
929 i_tracks, p_sectors[i_tracks] );
930 p_vcddev->i_tracks = i_tracks;
931 p_vcddev->p_sectors = malloc( (i_tracks + 1) * sizeof(int) );
932 memcpy( p_vcddev->p_sectors, p_sectors, (i_tracks + 1) * sizeof(int) );
936 if( cuefile ) fclose( cuefile );
937 if( psz_cuefile ) free( psz_cuefile );
938 if( psz_vcdfile ) free( psz_vcdfile );
943 /****************************************************************************
944 * CloseVCDImage: closes a vcd image opened by OpenVCDImage
945 ****************************************************************************/
946 static void CloseVCDImage( vlc_object_t * p_this, vcddev_t *p_vcddev )
948 if( p_vcddev->i_vcdimage_handle != -1 )
949 close( p_vcddev->i_vcdimage_handle );
953 if( p_vcddev->p_sectors )
954 free( p_vcddev->p_sectors );
957 #if defined( __APPLE__ )
958 /****************************************************************************
959 * darwin_getTOC: get the TOC
960 ****************************************************************************/
961 static CDTOC *darwin_getTOC( vlc_object_t * p_this, const vcddev_t *p_vcddev )
967 io_iterator_t iterator;
968 io_registry_entry_t service;
969 CFMutableDictionaryRef properties;
972 /* get the device name */
973 if( ( psz_devname = strrchr( p_vcddev->psz_dev, '/') ) != NULL )
976 psz_devname = p_vcddev->psz_dev;
978 /* unraw the device name */
979 if( *psz_devname == 'r' )
982 /* get port for IOKit communication */
983 if( ( ret = IOMasterPort( MACH_PORT_NULL, &port ) ) != KERN_SUCCESS )
985 msg_Err( p_this, "IOMasterPort: 0x%08x", ret );
989 /* get service iterator for the device */
990 if( ( ret = IOServiceGetMatchingServices(
991 port, IOBSDNameMatching( port, 0, psz_devname ),
992 &iterator ) ) != KERN_SUCCESS )
994 msg_Err( p_this, "IOServiceGetMatchingServices: 0x%08x", ret );
999 service = IOIteratorNext( iterator );
1000 IOObjectRelease( iterator );
1002 /* search for kIOCDMediaClass */
1003 while( service && !IOObjectConformsTo( service, kIOCDMediaClass ) )
1005 if( ( ret = IORegistryEntryGetParentIterator( service,
1006 kIOServicePlane, &iterator ) ) != KERN_SUCCESS )
1008 msg_Err( p_this, "IORegistryEntryGetParentIterator: 0x%08x", ret );
1009 IOObjectRelease( service );
1013 IOObjectRelease( service );
1014 service = IOIteratorNext( iterator );
1015 IOObjectRelease( iterator );
1018 if( service == NULL )
1020 msg_Err( p_this, "search for kIOCDMediaClass came up empty" );
1024 /* create a CF dictionary containing the TOC */
1025 if( ( ret = IORegistryEntryCreateCFProperties( service, &properties,
1026 kCFAllocatorDefault, kNilOptions ) ) != KERN_SUCCESS )
1028 msg_Err( p_this, "IORegistryEntryCreateCFProperties: 0x%08x", ret );
1029 IOObjectRelease( service );
1033 /* get the TOC from the dictionary */
1034 if( ( data = (CFDataRef) CFDictionaryGetValue( properties,
1035 CFSTR(kIOCDMediaTOCKey) ) ) != NULL )
1040 buf_len = CFDataGetLength( data ) + 1;
1041 range = CFRangeMake( 0, buf_len );
1043 if( ( pTOC = (CDTOC *)malloc( buf_len ) ) != NULL )
1045 CFDataGetBytes( data, range, (u_char *)pTOC );
1050 msg_Err( p_this, "CFDictionaryGetValue failed" );
1053 CFRelease( properties );
1054 IOObjectRelease( service );
1059 /****************************************************************************
1060 * darwin_getNumberOfDescriptors: get number of descriptors in TOC
1061 ****************************************************************************/
1062 static int darwin_getNumberOfDescriptors( CDTOC *pTOC )
1066 /* get TOC length */
1067 i_descriptors = pTOC->length;
1069 /* remove the first and last session */
1070 i_descriptors -= ( sizeof(pTOC->sessionFirst) +
1071 sizeof(pTOC->sessionLast) );
1073 /* divide the length by the size of a single descriptor */
1074 i_descriptors /= sizeof(CDTOCDescriptor);
1076 return( i_descriptors );
1079 /****************************************************************************
1080 * darwin_getNumberOfTracks: get number of tracks in TOC
1081 ****************************************************************************/
1082 static int darwin_getNumberOfTracks( CDTOC *pTOC, int i_descriptors )
1085 int i, i_tracks = 0;
1086 CDTOCDescriptor *pTrackDescriptors;
1088 pTrackDescriptors = pTOC->descriptors;
1090 for( i = i_descriptors; i >= 0; i-- )
1092 track = pTrackDescriptors[i].point;
1094 if( track > CD_MAX_TRACK_NO || track < CD_MIN_TRACK_NO )
1102 #endif /* __APPLE__ */
1104 #if defined( WIN32 )
1105 /*****************************************************************************
1106 * win32_vcd_open: open vcd drive
1107 *****************************************************************************
1108 * Load and use aspi if it is available, otherwise use IOCTLs on WinNT/2K/XP.
1109 *****************************************************************************/
1110 static int win32_vcd_open( vlc_object_t * p_this, const char *psz_dev,
1111 vcddev_t *p_vcddev )
1113 /* Initializations */
1114 p_vcddev->h_device_handle = NULL;
1115 p_vcddev->i_sid = 0;
1116 p_vcddev->hASPI = 0;
1117 p_vcddev->lpSendCommand = 0;
1121 char psz_win32_drive[7];
1123 msg_Dbg( p_this, "using winNT/2K/XP ioctl layer" );
1125 sprintf( psz_win32_drive, "\\\\.\\%c:", psz_dev[0] );
1127 p_vcddev->h_device_handle = CreateFile( psz_win32_drive, GENERIC_READ,
1128 FILE_SHARE_READ | FILE_SHARE_WRITE,
1129 NULL, OPEN_EXISTING,
1130 FILE_FLAG_NO_BUFFERING |
1131 FILE_FLAG_RANDOM_ACCESS, NULL );
1132 return (p_vcddev->h_device_handle == NULL) ? -1 : 0;
1136 HMODULE hASPI = NULL;
1137 long (*lpGetSupport)( void ) = NULL;
1138 long (*lpSendCommand)( void* ) = NULL;
1139 DWORD dwSupportInfo;
1140 int i, j, i_hostadapters;
1141 char c_drive = psz_dev[0];
1143 hASPI = LoadLibrary( "wnaspi32.dll" );
1146 (FARPROC) lpGetSupport = GetProcAddress( hASPI,
1147 "GetASPI32SupportInfo" );
1148 (FARPROC) lpSendCommand = GetProcAddress( hASPI,
1149 "SendASPI32Command" );
1152 if( hASPI == NULL || lpGetSupport == NULL || lpSendCommand == NULL )
1155 "unable to load aspi or get aspi function pointers" );
1156 if( hASPI ) FreeLibrary( hASPI );
1160 /* ASPI support seems to be there */
1162 dwSupportInfo = lpGetSupport();
1164 if( HIBYTE( LOWORD ( dwSupportInfo ) ) == SS_NO_ADAPTERS )
1166 msg_Dbg( p_this, "no host adapters found (aspi)" );
1167 FreeLibrary( hASPI );
1171 if( HIBYTE( LOWORD ( dwSupportInfo ) ) != SS_COMP )
1173 msg_Dbg( p_this, "unable to initalize aspi layer" );
1174 FreeLibrary( hASPI );
1178 i_hostadapters = LOBYTE( LOWORD( dwSupportInfo ) );
1179 if( i_hostadapters == 0 )
1181 FreeLibrary( hASPI );
1185 c_drive = c_drive > 'Z' ? c_drive - 'a' : c_drive - 'A';
1187 for( i = 0; i < i_hostadapters; i++ )
1189 for( j = 0; j < 15; j++ )
1191 struct SRB_GetDiskInfo srbDiskInfo;
1193 srbDiskInfo.SRB_Cmd = SC_GET_DISK_INFO;
1194 srbDiskInfo.SRB_HaId = i;
1195 srbDiskInfo.SRB_Flags = 0;
1196 srbDiskInfo.SRB_Hdr_Rsvd = 0;
1197 srbDiskInfo.SRB_Target = j;
1198 srbDiskInfo.SRB_Lun = 0;
1200 lpSendCommand( (void*) &srbDiskInfo );
1202 if( (srbDiskInfo.SRB_Status == SS_COMP) &&
1203 (srbDiskInfo.SRB_Int13HDriveInfo == c_drive) )
1205 /* Make sure this is a cdrom device */
1206 struct SRB_GDEVBlock srbGDEVBlock;
1208 memset( &srbGDEVBlock, 0, sizeof(struct SRB_GDEVBlock) );
1209 srbGDEVBlock.SRB_Cmd = SC_GET_DEV_TYPE;
1210 srbGDEVBlock.SRB_HaId = i;
1211 srbGDEVBlock.SRB_Target = j;
1213 lpSendCommand( (void*) &srbGDEVBlock );
1215 if( ( srbGDEVBlock.SRB_Status == SS_COMP ) &&
1216 ( srbGDEVBlock.SRB_DeviceType == DTYPE_CDROM ) )
1218 p_vcddev->i_sid = MAKEWORD( i, j );
1219 p_vcddev->hASPI = (long)hASPI;
1220 p_vcddev->lpSendCommand = lpSendCommand;
1221 msg_Dbg( p_this, "using aspi layer" );
1227 FreeLibrary( hASPI );
1228 msg_Dbg( p_this, "%c: is not a cdrom drive",
1236 FreeLibrary( hASPI );
1237 msg_Dbg( p_this, "unable to get haid and target (aspi)" );