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 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_access.h>
41 #ifdef HAVE_SYS_TYPES_H
42 # include <sys/types.h>
44 #ifdef HAVE_SYS_STAT_H
45 # include <sys/stat.h>
51 #if defined( SYS_BSDI )
53 #elif defined ( __APPLE__ )
54 # include <CoreFoundation/CFBase.h>
55 # include <IOKit/IOKitLib.h>
56 # include <IOKit/storage/IOCDTypes.h>
57 # include <IOKit/storage/IOCDMedia.h>
58 # include <IOKit/storage/IOCDMediaBSDClient.h>
59 #elif defined( HAVE_SCSIREQ_IN_SYS_SCSIIO_H )
60 # include <sys/inttypes.h>
61 # include <sys/cdio.h>
62 # include <sys/scsiio.h>
63 #elif defined( HAVE_IOC_TOC_HEADER_IN_SYS_CDIO_H )
64 # include <sys/cdio.h>
65 # include <sys/cdrio.h>
66 #elif defined( WIN32 )
68 # include <winioctl.h>
69 #elif defined (__linux__)
70 # include <sys/ioctl.h>
71 # include <linux/cdrom.h>
76 #include "cdrom_internals.h"
78 #include <vlc_charset.h>
80 /*****************************************************************************
81 * ioctl_Open: Opens a VCD device or file and returns an opaque handle
82 *****************************************************************************/
83 vcddev_t *ioctl_Open( vlc_object_t *p_this, const char *psz_dev )
92 if( !psz_dev ) return NULL;
95 * Initialize structure with default values
97 p_vcddev = (vcddev_t *)malloc( sizeof(vcddev_t) );
98 if( p_vcddev == NULL )
100 p_vcddev->i_vcdimage_handle = -1;
101 p_vcddev->psz_dev = NULL;
105 * Check if we are dealing with a device or a file (vcd image)
108 if( (strlen( psz_dev ) == 2 && psz_dev[1] == ':') )
114 if( stat( psz_dev, &fileinfo ) < 0 )
120 /* Check if this is a block/char device */
121 if( S_ISBLK( fileinfo.st_mode ) || S_ISCHR( fileinfo.st_mode ) )
127 i_ret = OpenVCDImage( p_this, psz_dev, p_vcddev );
132 * open the vcd device
136 i_ret = win32_vcd_open( p_this, psz_dev, p_vcddev );
138 p_vcddev->i_device_handle = -1;
139 p_vcddev->i_device_handle = open( psz_dev, O_RDONLY | O_NONBLOCK );
140 i_ret = (p_vcddev->i_device_handle == -1) ? -1 : 0;
146 p_vcddev->psz_dev = (char *)strdup( psz_dev );
157 /*****************************************************************************
158 * ioctl_Close: Closes an already opened VCD device or file.
159 *****************************************************************************/
160 void ioctl_Close( vlc_object_t * p_this, vcddev_t *p_vcddev )
162 free( p_vcddev->psz_dev );
164 if( p_vcddev->i_vcdimage_handle != -1 )
170 CloseVCDImage( p_this, p_vcddev );
179 if( p_vcddev->h_device_handle )
180 CloseHandle( p_vcddev->h_device_handle );
181 if( p_vcddev->hASPI )
182 FreeLibrary( (HMODULE)p_vcddev->hASPI );
184 if( p_vcddev->i_device_handle != -1 )
185 close( p_vcddev->i_device_handle );
189 /*****************************************************************************
190 * ioctl_GetTracksMap: Read the Table of Content, fill in the pp_sectors map
191 * if pp_sectors is not null and return the number of
193 *****************************************************************************/
194 int ioctl_GetTracksMap( vlc_object_t *p_this, const vcddev_t *p_vcddev,
199 if( p_vcddev->i_vcdimage_handle != -1 )
205 i_tracks = p_vcddev->i_tracks;
209 *pp_sectors = malloc( (i_tracks + 1) * sizeof(int) );
210 if( *pp_sectors == NULL )
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 = CDTOCGetDescriptorCount( 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 darwin_freeTOC( pTOC );
252 pTrackDescriptors = pTOC->descriptors;
254 for( i_tracks = 0, i = 0; i < i_descriptors; i++ )
256 track = pTrackDescriptors[i].point;
261 if( track > CD_MAX_TRACK_NO || track < CD_MIN_TRACK_NO )
264 (*pp_sectors)[i_tracks++] =
265 CDConvertMSFToLBA( pTrackDescriptors[i].p );
268 if( i_leadout == -1 )
270 msg_Err( p_this, "leadout not found" );
272 darwin_freeTOC( pTOC );
276 /* set leadout sector */
277 (*pp_sectors)[i_tracks] =
278 CDConvertMSFToLBA( pTrackDescriptors[i_leadout].p );
281 darwin_freeTOC( pTOC );
283 #elif defined( WIN32 )
284 if( p_vcddev->hASPI )
287 struct SRB_ExecSCSICmd ssc;
288 uint8_t p_tocheader[ 4 ];
290 /* Create the transfer completion event */
291 hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
297 memset( &ssc, 0, sizeof( ssc ) );
299 ssc.SRB_Cmd = SC_EXEC_SCSI_CMD;
300 ssc.SRB_Flags = SRB_DIR_IN | SRB_EVENT_NOTIFY;
301 ssc.SRB_HaId = LOBYTE( p_vcddev->i_sid );
302 ssc.SRB_Target = HIBYTE( p_vcddev->i_sid );
303 ssc.SRB_SenseLen = SENSE_LEN;
305 ssc.SRB_PostProc = (LPVOID) hEvent;
309 ssc.CDBByte[ 0 ] = READ_TOC;
312 ssc.CDBByte[ 2 ] = READ_TOC_FORMAT_TOC;
315 ssc.CDBByte[ 6 ] = 0;
317 /* Allocation length and buffer */
318 ssc.SRB_BufLen = sizeof( p_tocheader );
319 ssc.SRB_BufPointer = p_tocheader;
320 ssc.CDBByte[ 7 ] = ( ssc.SRB_BufLen >> 8 ) & 0xff;
321 ssc.CDBByte[ 8 ] = ( ssc.SRB_BufLen ) & 0xff;
323 /* Initiate transfer */
324 ResetEvent( hEvent );
325 p_vcddev->lpSendCommand( (void*) &ssc );
327 /* If the command has still not been processed, wait until it's
329 if( ssc.SRB_Status == SS_PENDING )
330 WaitForSingleObject( hEvent, INFINITE );
332 /* check that the transfer went as planned */
333 if( ssc.SRB_Status != SS_COMP )
335 CloseHandle( hEvent );
339 i_tracks = p_tocheader[3] - p_tocheader[2] + 1;
346 i_toclength = 4 /* header */ + p_tocheader[0] +
347 ((unsigned int)p_tocheader[1] << 8);
349 p_fulltoc = malloc( i_toclength );
350 *pp_sectors = malloc( (i_tracks + 1) * sizeof(int) );
352 if( *pp_sectors == NULL || p_fulltoc == NULL )
356 CloseHandle( hEvent );
360 /* Allocation length and buffer */
361 ssc.SRB_BufLen = i_toclength;
362 ssc.SRB_BufPointer = p_fulltoc;
363 ssc.CDBByte[ 7 ] = ( ssc.SRB_BufLen >> 8 ) & 0xff;
364 ssc.CDBByte[ 8 ] = ( ssc.SRB_BufLen ) & 0xff;
366 /* Initiate transfer */
367 ResetEvent( hEvent );
368 p_vcddev->lpSendCommand( (void*) &ssc );
370 /* If the command has still not been processed, wait until it's
372 if( ssc.SRB_Status == SS_PENDING )
373 WaitForSingleObject( hEvent, INFINITE );
375 /* check that the transfer went as planned */
376 if( ssc.SRB_Status != SS_COMP )
379 for( i = 0 ; i <= i_tracks ; i++ )
381 int i_index = 8 + 8 * i;
382 (*pp_sectors)[ i ] = ((int)p_fulltoc[ i_index ] << 24) +
383 ((int)p_fulltoc[ i_index+1 ] << 16) +
384 ((int)p_fulltoc[ i_index+2 ] << 8) +
385 (int)p_fulltoc[ i_index+3 ];
387 msg_Dbg( p_this, "p_sectors: %i, %i", i, (*pp_sectors)[i]);
393 CloseHandle( hEvent );
398 DWORD dwBytesReturned;
401 if( DeviceIoControl( p_vcddev->h_device_handle,
402 IOCTL_CDROM_READ_TOC,
403 NULL, 0, &cdrom_toc, sizeof(CDROM_TOC),
404 &dwBytesReturned, NULL ) == 0 )
406 msg_Err( p_this, "could not read TOCHDR" );
410 i_tracks = cdrom_toc.LastTrack - cdrom_toc.FirstTrack + 1;
416 *pp_sectors = malloc( (i_tracks + 1) * sizeof(int) );
417 if( *pp_sectors == NULL )
420 for( i = 0 ; i <= i_tracks ; i++ )
422 (*pp_sectors)[ i ] = MSF_TO_LBA2(
423 cdrom_toc.TrackData[i].Address[1],
424 cdrom_toc.TrackData[i].Address[2],
425 cdrom_toc.TrackData[i].Address[3] );
426 msg_Dbg( p_this, "p_sectors: %i, %i", i, (*pp_sectors)[i]);
431 #elif defined( HAVE_IOC_TOC_HEADER_IN_SYS_CDIO_H ) \
432 || defined( HAVE_SCSIREQ_IN_SYS_SCSIIO_H )
433 struct ioc_toc_header tochdr;
434 struct ioc_read_toc_entry toc_entries;
436 if( ioctl( p_vcddev->i_device_handle, CDIOREADTOCHEADER, &tochdr )
439 msg_Err( p_this, "could not read TOCHDR" );
443 i_tracks = tochdr.ending_track - tochdr.starting_track + 1;
449 *pp_sectors = malloc( (i_tracks + 1) * sizeof(int) );
450 if( *pp_sectors == NULL )
453 toc_entries.address_format = CD_LBA_FORMAT;
454 toc_entries.starting_track = 0;
455 toc_entries.data_len = ( i_tracks + 1 ) *
456 sizeof( struct cd_toc_entry );
457 toc_entries.data = (struct cd_toc_entry *)
458 malloc( toc_entries.data_len );
459 if( toc_entries.data == NULL )
466 if( ioctl( p_vcddev->i_device_handle, CDIOREADTOCENTRYS,
467 &toc_entries ) == -1 )
469 msg_Err( p_this, "could not read the TOC" );
471 free( toc_entries.data );
475 /* Fill the p_sectors structure with the track/sector matches */
476 for( i = 0 ; i <= i_tracks ; i++ )
478 #if defined( HAVE_SCSIREQ_IN_SYS_SCSIIO_H )
479 /* FIXME: is this ok? */
480 (*pp_sectors)[ i ] = toc_entries.data[i].addr.lba;
482 (*pp_sectors)[ i ] = ntohl( toc_entries.data[i].addr.lba );
487 struct cdrom_tochdr tochdr;
488 struct cdrom_tocentry tocent;
490 /* First we read the TOC header */
491 if( ioctl( p_vcddev->i_device_handle, CDROMREADTOCHDR, &tochdr )
494 msg_Err( p_this, "could not read TOCHDR" );
498 i_tracks = tochdr.cdth_trk1 - tochdr.cdth_trk0 + 1;
504 *pp_sectors = malloc( (i_tracks + 1) * sizeof(int) );
505 if( *pp_sectors == NULL )
508 /* Fill the p_sectors structure with the track/sector matches */
509 for( i = 0 ; i <= i_tracks ; i++ )
511 tocent.cdte_format = CDROM_LBA;
513 ( i == i_tracks ) ? CDROM_LEADOUT : tochdr.cdth_trk0 + i;
515 if( ioctl( p_vcddev->i_device_handle, CDROMREADTOCENTRY,
518 msg_Err( p_this, "could not read TOCENTRY" );
523 (*pp_sectors)[ i ] = tocent.cdte_addr.lba;
532 /****************************************************************************
533 * ioctl_ReadSector: Read VCD or CDDA sectors
534 ****************************************************************************/
535 int ioctl_ReadSectors( vlc_object_t *p_this, const vcddev_t *p_vcddev,
536 int i_sector, uint8_t *p_buffer, int i_nb, int i_type )
541 if( i_type == VCD_TYPE ) p_block = malloc( VCD_SECTOR_SIZE * i_nb );
542 else p_block = p_buffer;
544 if( p_vcddev->i_vcdimage_handle != -1 )
549 if( lseek( p_vcddev->i_vcdimage_handle, i_sector * VCD_SECTOR_SIZE,
552 msg_Err( p_this, "Could not lseek to sector %d", i_sector );
553 if( i_type == VCD_TYPE ) free( p_block );
557 if( read( p_vcddev->i_vcdimage_handle, p_block, VCD_SECTOR_SIZE * i_nb)
560 msg_Err( p_this, "Could not read sector %d", i_sector );
561 if( i_type == VCD_TYPE ) free( p_block );
573 #if defined( __APPLE__ )
574 dk_cd_read_t cd_read;
576 memset( &cd_read, 0, sizeof(cd_read) );
578 cd_read.offset = i_sector * VCD_SECTOR_SIZE;
579 cd_read.sectorArea = kCDSectorAreaSync | kCDSectorAreaHeader |
580 kCDSectorAreaSubHeader | kCDSectorAreaUser |
581 kCDSectorAreaAuxiliary;
582 cd_read.sectorType = kCDSectorTypeUnknown;
584 cd_read.buffer = p_block;
585 cd_read.bufferLength = VCD_SECTOR_SIZE * i_nb;
587 if( ioctl( p_vcddev->i_device_handle, DKIOCCDREAD, &cd_read ) == -1 )
589 msg_Err( p_this, "could not read block %d", i_sector );
590 if( i_type == VCD_TYPE ) free( p_block );
594 #elif defined( WIN32 )
595 if( p_vcddev->hASPI )
598 struct SRB_ExecSCSICmd ssc;
600 /* Create the transfer completion event */
601 hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
604 if( i_type == VCD_TYPE ) free( p_block );
608 memset( &ssc, 0, sizeof( ssc ) );
610 ssc.SRB_Cmd = SC_EXEC_SCSI_CMD;
611 ssc.SRB_Flags = SRB_DIR_IN | SRB_EVENT_NOTIFY;
612 ssc.SRB_HaId = LOBYTE( p_vcddev->i_sid );
613 ssc.SRB_Target = HIBYTE( p_vcddev->i_sid );
614 ssc.SRB_SenseLen = SENSE_LEN;
616 ssc.SRB_PostProc = (LPVOID) hEvent;
620 ssc.CDBByte[ 0 ] = READ_CD;
623 ssc.CDBByte[ 1 ] = i_type == VCD_TYPE ? SECTOR_TYPE_MODE2_FORM2 :
627 ssc.CDBByte[ 2 ] = ( i_sector >> 24 ) & 0xff;
628 ssc.CDBByte[ 3 ] = ( i_sector >> 16 ) & 0xff;
629 ssc.CDBByte[ 4 ] = ( i_sector >> 8 ) & 0xff;
630 ssc.CDBByte[ 5 ] = ( i_sector ) & 0xff;
632 /* Transfer length */
633 ssc.CDBByte[ 6 ] = ( i_nb >> 16 ) & 0xff;
634 ssc.CDBByte[ 7 ] = ( i_nb >> 8 ) & 0xff;
635 ssc.CDBByte[ 8 ] = ( i_nb ) & 0xff;
638 ssc.CDBByte[ 9 ] = i_type == VCD_TYPE ? READ_CD_RAW_MODE2 :
642 ssc.SRB_BufPointer = p_block;
643 ssc.SRB_BufLen = VCD_SECTOR_SIZE * i_nb;
645 /* Initiate transfer */
646 ResetEvent( hEvent );
647 p_vcddev->lpSendCommand( (void*) &ssc );
649 /* If the command has still not been processed, wait until it's
651 if( ssc.SRB_Status == SS_PENDING )
653 WaitForSingleObject( hEvent, INFINITE );
655 CloseHandle( hEvent );
657 /* check that the transfer went as planned */
658 if( ssc.SRB_Status != SS_COMP )
660 if( i_type == VCD_TYPE ) free( p_block );
666 DWORD dwBytesReturned;
667 RAW_READ_INFO cdrom_raw;
669 /* Initialize CDROM_RAW_READ structure */
670 cdrom_raw.DiskOffset.QuadPart = CD_SECTOR_SIZE * i_sector;
671 cdrom_raw.SectorCount = i_nb;
672 cdrom_raw.TrackMode = i_type == VCD_TYPE ? XAForm2 : CDDA;
674 if( DeviceIoControl( p_vcddev->h_device_handle,
675 IOCTL_CDROM_RAW_READ, &cdrom_raw,
676 sizeof(RAW_READ_INFO), p_block,
677 VCD_SECTOR_SIZE * i_nb, &dwBytesReturned,
680 if( i_type == VCD_TYPE )
682 /* Retry in YellowMode2 */
683 cdrom_raw.TrackMode = YellowMode2;
684 if( DeviceIoControl( p_vcddev->h_device_handle,
685 IOCTL_CDROM_RAW_READ, &cdrom_raw,
686 sizeof(RAW_READ_INFO), p_block,
687 VCD_SECTOR_SIZE * i_nb, &dwBytesReturned,
698 #elif defined( HAVE_SCSIREQ_IN_SYS_SCSIIO_H )
702 memset( &sc, 0, sizeof(sc) );
704 sc.cmd[1] = i_type == VCD_TYPE ? SECTOR_TYPE_MODE2_FORM2:
706 sc.cmd[2] = (i_sector >> 24) & 0xff;
707 sc.cmd[3] = (i_sector >> 16) & 0xff;
708 sc.cmd[4] = (i_sector >> 8) & 0xff;
709 sc.cmd[5] = (i_sector >> 0) & 0xff;
710 sc.cmd[6] = (i_nb >> 16) & 0xff;
711 sc.cmd[7] = (i_nb >> 8) & 0xff;
712 sc.cmd[8] = (i_nb ) & 0xff;
713 sc.cmd[9] = i_type == VCD_TYPE ? READ_CD_RAW_MODE2 : READ_CD_USERDATA;
714 sc.cmd[10] = 0; /* sub channel */
716 sc.databuf = (caddr_t)p_block;
717 sc.datalen = VCD_SECTOR_SIZE * i_nb;
718 sc.senselen = sizeof( sc.sense );
719 sc.flags = SCCMD_READ;
722 i_ret = ioctl( i_fd, SCIOCCOMMAND, &sc );
725 msg_Err( p_this, "SCIOCCOMMAND failed" );
726 if( i_type == VCD_TYPE ) free( p_block );
729 if( sc.retsts || sc.error )
731 msg_Err( p_this, "SCSI command failed: status %d error %d\n",
732 sc.retsts, sc.error );
733 if( i_type == VCD_TYPE ) free( p_block );
737 #elif defined( HAVE_IOC_TOC_HEADER_IN_SYS_CDIO_H )
738 int i_size = VCD_SECTOR_SIZE;
740 if( ioctl( p_vcddev->i_device_handle, CDRIOCSETBLOCKSIZE, &i_size )
743 msg_Err( p_this, "Could not set block size" );
744 if( i_type == VCD_TYPE ) free( p_block );
748 if( lseek( p_vcddev->i_device_handle,
749 i_sector * VCD_SECTOR_SIZE, SEEK_SET ) == -1 )
751 msg_Err( p_this, "Could not lseek to sector %d", i_sector );
752 if( i_type == VCD_TYPE ) free( p_block );
756 if( read( p_vcddev->i_device_handle,
757 p_block, VCD_SECTOR_SIZE * i_nb ) == -1 )
759 msg_Err( p_this, "Could not read sector %d", i_sector );
760 if( i_type == VCD_TYPE ) free( p_block );
765 for( i = 0; i < i_nb; i++ )
767 int i_dummy = i_sector + i + 2 * CD_FRAMES;
769 #define p_msf ((struct cdrom_msf0 *)(p_block + i * VCD_SECTOR_SIZE))
770 p_msf->minute = i_dummy / (CD_FRAMES * CD_SECS);
771 p_msf->second = ( i_dummy % (CD_FRAMES * CD_SECS) ) / CD_FRAMES;
772 p_msf->frame = ( i_dummy % (CD_FRAMES * CD_SECS) ) % CD_FRAMES;
775 if( ioctl( p_vcddev->i_device_handle, CDROMREADRAW,
776 p_block + i * VCD_SECTOR_SIZE ) == -1 )
778 msg_Err( p_this, "could not read block %i from disc",
783 if( i_type == VCD_TYPE ) free( p_block );
792 /* For VCDs, we don't want to keep the header and footer of the
794 if( i_type == VCD_TYPE )
796 for( i = 0; i < i_nb; i++ )
798 memcpy( p_buffer + i * VCD_DATA_SIZE,
799 p_block + i * VCD_SECTOR_SIZE + VCD_DATA_START,
808 /****************************************************************************
810 ****************************************************************************/
812 /****************************************************************************
813 * OpenVCDImage: try to open a vcd image from a .cue file
814 ****************************************************************************/
815 static int OpenVCDImage( vlc_object_t * p_this, const char *psz_dev,
820 char *psz_vcdfile = NULL;
821 char *psz_cuefile = NULL;
822 FILE *cuefile = NULL;
824 bool b_found = false;
826 /* Check if we are dealing with a .cue file */
827 p_pos = strrchr( psz_dev, '.' );
828 if( p_pos && !strcmp( p_pos, ".cue" ) )
830 /* psz_dev must be the cue file. Let's assume there's a .bin
831 * file with the same filename */
832 psz_vcdfile = malloc( p_pos - psz_dev + 5 /* ".bin" */ );
833 strncpy( psz_vcdfile, psz_dev, p_pos - psz_dev );
834 strcpy( psz_vcdfile + (p_pos - psz_dev), ".bin");
835 psz_cuefile = strdup( psz_dev );
839 /* psz_dev must be the actual vcd file. Let's assume there's a .cue
840 * file with the same filename */
843 psz_cuefile = malloc( p_pos - psz_dev + 5 /* ".cue" */ );
844 strncpy( psz_cuefile, psz_dev, p_pos - psz_dev );
845 strcpy( psz_cuefile + (p_pos - psz_dev), ".cue");
849 psz_cuefile = malloc( strlen(psz_dev) + 5 /* ".cue" */ );
850 sprintf( psz_cuefile, "%s.cue", psz_dev );
852 /* If we need to look up the .cue file, then we don't have to look for the vcd */
853 psz_vcdfile = strdup( psz_dev );
856 /* Open the cue file and try to parse it */
857 msg_Dbg( p_this,"trying .cue file: %s", psz_cuefile );
858 cuefile = utf8_fopen( psz_cuefile, "rt" );
859 if( cuefile == NULL )
862 msg_Dbg( p_this, "could not find .cue file" );
866 msg_Dbg( p_this,"guessing vcd image file: %s", psz_vcdfile );
867 p_vcddev->i_vcdimage_handle = utf8_open( psz_vcdfile,
868 O_RDONLY | O_NONBLOCK | O_BINARY, 0666 );
870 while( fgets( line, 1024, cuefile ) && !b_found )
872 /* We have a cue file, but no valid vcd file yet */
875 int i_temp = sscanf( line, "FILE \"%1023[^\"]\" %15s", filename, type );
880 msg_Dbg( p_this, "the cue file says the data file is %s", type );
881 if( strcasecmp( type, "BINARY" ) )
882 goto error; /* Error if not binary, otherwise treat as case 1 */
884 if( p_vcddev->i_vcdimage_handle == -1 )
886 msg_Dbg( p_this, "we could not find the data file, but we found a new path" );
888 if( *filename != '/' && ((p_pos = strrchr( psz_cuefile, '/' ))
889 || (p_pos = strrchr( psz_cuefile, '\\' ) )) )
891 psz_vcdfile = malloc( strlen(filename) +
892 (p_pos - psz_cuefile + 1) + 1 );
893 strncpy( psz_vcdfile, psz_cuefile, (p_pos - psz_cuefile + 1) );
894 strcpy( psz_vcdfile + (p_pos - psz_cuefile + 1), filename );
895 } else psz_vcdfile = strdup( filename );
896 msg_Dbg( p_this,"using vcd image file: %s", psz_vcdfile );
897 p_vcddev->i_vcdimage_handle = utf8_open( psz_vcdfile,
898 O_RDONLY | O_NONBLOCK | O_BINARY, 0666 );
906 if( p_vcddev->i_vcdimage_handle == -1)
913 /* Try to parse the i_tracks and p_sectors info so we can just forget
914 * about the cuefile */
922 while( fgets( line, 1024, cuefile ) )
924 /* look for a TRACK line */
925 if( !sscanf( line, "%9s", psz_dummy ) ||
926 strcmp(psz_dummy, "TRACK") )
929 /* look for an INDEX line */
930 while( fgets( line, 1024, cuefile ) )
932 int i_min, i_sec, i_frame;
934 if( (sscanf( line, "%9s %2u %2u:%2u:%2u", psz_dummy, &i_num,
935 &i_min, &i_sec, &i_frame ) != 5) || (i_num != 1) )
939 p_sectors[i_tracks - 1] = MSF_TO_LBA(i_min, i_sec, i_frame);
940 msg_Dbg( p_this, "vcd track %i begins at sector:%i",
941 i_tracks - 1, p_sectors[i_tracks - 1] );
946 /* fill in the last entry */
947 p_sectors[i_tracks] = lseek(p_vcddev->i_vcdimage_handle, 0, SEEK_END)
949 msg_Dbg( p_this, "vcd track %i, begins at sector:%i",
950 i_tracks, p_sectors[i_tracks] );
951 p_vcddev->i_tracks = i_tracks;
952 p_vcddev->p_sectors = malloc( (i_tracks + 1) * sizeof(int) );
953 memcpy( p_vcddev->p_sectors, p_sectors, (i_tracks + 1) * sizeof(int) );
958 if( cuefile ) fclose( cuefile );
965 /****************************************************************************
966 * CloseVCDImage: closes a vcd image opened by OpenVCDImage
967 ****************************************************************************/
968 static void CloseVCDImage( vlc_object_t * p_this, vcddev_t *p_vcddev )
970 if( p_vcddev->i_vcdimage_handle != -1 )
971 close( p_vcddev->i_vcdimage_handle );
975 free( p_vcddev->p_sectors );
978 #if defined( __APPLE__ )
979 /****************************************************************************
980 * darwin_getTOC: get the TOC
981 ****************************************************************************/
982 static CDTOC *darwin_getTOC( vlc_object_t * p_this, const vcddev_t *p_vcddev )
988 io_iterator_t iterator;
989 io_registry_entry_t service;
990 CFMutableDictionaryRef properties;
993 /* get the device name */
994 if( ( psz_devname = strrchr( p_vcddev->psz_dev, '/') ) != NULL )
997 psz_devname = p_vcddev->psz_dev;
999 /* unraw the device name */
1000 if( *psz_devname == 'r' )
1003 /* get port for IOKit communication */
1004 if( ( ret = IOMasterPort( MACH_PORT_NULL, &port ) ) != KERN_SUCCESS )
1006 msg_Err( p_this, "IOMasterPort: 0x%08x", ret );
1010 /* get service iterator for the device */
1011 if( ( ret = IOServiceGetMatchingServices(
1012 port, IOBSDNameMatching( port, 0, psz_devname ),
1013 &iterator ) ) != KERN_SUCCESS )
1015 msg_Err( p_this, "IOServiceGetMatchingServices: 0x%08x", ret );
1020 service = IOIteratorNext( iterator );
1021 IOObjectRelease( iterator );
1023 /* search for kIOCDMediaClass */
1024 while( service && !IOObjectConformsTo( service, kIOCDMediaClass ) )
1026 if( ( ret = IORegistryEntryGetParentIterator( service,
1027 kIOServicePlane, &iterator ) ) != KERN_SUCCESS )
1029 msg_Err( p_this, "IORegistryEntryGetParentIterator: 0x%08x", ret );
1030 IOObjectRelease( service );
1034 IOObjectRelease( service );
1035 service = IOIteratorNext( iterator );
1036 IOObjectRelease( iterator );
1039 if( service == NULL )
1041 msg_Err( p_this, "search for kIOCDMediaClass came up empty" );
1045 /* create a CF dictionary containing the TOC */
1046 if( ( ret = IORegistryEntryCreateCFProperties( service, &properties,
1047 kCFAllocatorDefault, kNilOptions ) ) != KERN_SUCCESS )
1049 msg_Err( p_this, "IORegistryEntryCreateCFProperties: 0x%08x", ret );
1050 IOObjectRelease( service );
1054 /* get the TOC from the dictionary */
1055 if( ( data = (CFDataRef) CFDictionaryGetValue( properties,
1056 CFSTR(kIOCDMediaTOCKey) ) ) != NULL )
1061 buf_len = CFDataGetLength( data ) + 1;
1062 range = CFRangeMake( 0, buf_len );
1064 if( ( pTOC = (CDTOC *)malloc( buf_len ) ) != NULL )
1066 CFDataGetBytes( data, range, (u_char *)pTOC );
1071 msg_Err( p_this, "CFDictionaryGetValue failed" );
1074 CFRelease( properties );
1075 IOObjectRelease( service );
1080 /****************************************************************************
1081 * darwin_getNumberOfTracks: get number of tracks in TOC
1082 ****************************************************************************/
1083 static int darwin_getNumberOfTracks( CDTOC *pTOC, int i_descriptors )
1086 int i, i_tracks = 0;
1087 CDTOCDescriptor *pTrackDescriptors = NULL;
1089 pTrackDescriptors = (CDTOCDescriptor *)pTOC->descriptors;
1091 for( i = i_descriptors; i > 0; i-- )
1093 track = pTrackDescriptors[i].point;
1095 if( track > CD_MAX_TRACK_NO || track < CD_MIN_TRACK_NO )
1103 #endif /* __APPLE__ */
1105 #if defined( WIN32 )
1106 /*****************************************************************************
1107 * win32_vcd_open: open vcd drive
1108 *****************************************************************************
1109 * Load and use aspi if it is available, otherwise use IOCTLs on WinNT/2K/XP.
1110 *****************************************************************************/
1111 static int win32_vcd_open( vlc_object_t * p_this, const char *psz_dev,
1112 vcddev_t *p_vcddev )
1114 /* Initializations */
1115 p_vcddev->h_device_handle = NULL;
1116 p_vcddev->i_sid = 0;
1117 p_vcddev->hASPI = 0;
1118 p_vcddev->lpSendCommand = 0;
1122 char psz_win32_drive[7];
1124 msg_Dbg( p_this, "using winNT/2K/XP ioctl layer" );
1126 sprintf( psz_win32_drive, "\\\\.\\%c:", psz_dev[0] );
1128 p_vcddev->h_device_handle = CreateFile( psz_win32_drive, GENERIC_READ,
1129 FILE_SHARE_READ | FILE_SHARE_WRITE,
1130 NULL, OPEN_EXISTING,
1131 FILE_FLAG_NO_BUFFERING |
1132 FILE_FLAG_RANDOM_ACCESS, NULL );
1133 return (p_vcddev->h_device_handle == NULL) ? -1 : 0;
1137 HMODULE hASPI = NULL;
1138 long (*lpGetSupport)( void ) = NULL;
1139 long (*lpSendCommand)( void* ) = NULL;
1140 DWORD dwSupportInfo;
1141 int i, j, i_hostadapters;
1142 char c_drive = psz_dev[0];
1144 hASPI = LoadLibrary( "wnaspi32.dll" );
1147 lpGetSupport = (void *)GetProcAddress( hASPI, "GetASPI32SupportInfo" );
1148 lpSendCommand = (void *)GetProcAddress( hASPI, "SendASPI32Command" );
1151 if( hASPI == NULL || lpGetSupport == NULL || lpSendCommand == NULL )
1154 "unable to load aspi or get aspi function pointers" );
1155 if( hASPI ) FreeLibrary( hASPI );
1159 /* ASPI support seems to be there */
1161 dwSupportInfo = lpGetSupport();
1163 if( HIBYTE( LOWORD ( dwSupportInfo ) ) == SS_NO_ADAPTERS )
1165 msg_Dbg( p_this, "no host adapters found (aspi)" );
1166 FreeLibrary( hASPI );
1170 if( HIBYTE( LOWORD ( dwSupportInfo ) ) != SS_COMP )
1172 msg_Dbg( p_this, "unable to initalize aspi layer" );
1173 FreeLibrary( hASPI );
1177 i_hostadapters = LOBYTE( LOWORD( dwSupportInfo ) );
1178 if( i_hostadapters == 0 )
1180 FreeLibrary( hASPI );
1184 c_drive = c_drive > 'Z' ? c_drive - 'a' : c_drive - 'A';
1186 for( i = 0; i < i_hostadapters; i++ )
1188 for( j = 0; j < 15; j++ )
1190 struct SRB_GetDiskInfo srbDiskInfo;
1192 srbDiskInfo.SRB_Cmd = SC_GET_DISK_INFO;
1193 srbDiskInfo.SRB_HaId = i;
1194 srbDiskInfo.SRB_Flags = 0;
1195 srbDiskInfo.SRB_Hdr_Rsvd = 0;
1196 srbDiskInfo.SRB_Target = j;
1197 srbDiskInfo.SRB_Lun = 0;
1199 lpSendCommand( (void*) &srbDiskInfo );
1201 if( (srbDiskInfo.SRB_Status == SS_COMP) &&
1202 (srbDiskInfo.SRB_Int13HDriveInfo == c_drive) )
1204 /* Make sure this is a cdrom device */
1205 struct SRB_GDEVBlock srbGDEVBlock;
1207 memset( &srbGDEVBlock, 0, sizeof(struct SRB_GDEVBlock) );
1208 srbGDEVBlock.SRB_Cmd = SC_GET_DEV_TYPE;
1209 srbGDEVBlock.SRB_HaId = i;
1210 srbGDEVBlock.SRB_Target = j;
1212 lpSendCommand( (void*) &srbGDEVBlock );
1214 if( ( srbGDEVBlock.SRB_Status == SS_COMP ) &&
1215 ( srbGDEVBlock.SRB_DeviceType == DTYPE_CDROM ) )
1217 p_vcddev->i_sid = MAKEWORD( i, j );
1218 p_vcddev->hASPI = (long)hASPI;
1219 p_vcddev->lpSendCommand = lpSendCommand;
1220 msg_Dbg( p_this, "using aspi layer" );
1226 FreeLibrary( hASPI );
1227 msg_Dbg( p_this, "%c: is not a cdrom drive",
1235 FreeLibrary( hASPI );
1236 msg_Dbg( p_this, "unable to get haid and target (aspi)" );