1 /****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 1998-2001 VLC authors and VideoLAN
7 * Authors: Johan Bilien <jobi@via.ecp.fr>
8 * Gildas Bazin <gbazin@netcourrier.com>
19 * This program is free software; you can redistribute it and/or modify it
20 * under the terms of the GNU Lesser General Public License as published by
21 * the Free Software Foundation; either version 2.1 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU Lesser General Public License for more details.
29 * You should have received a copy of the GNU Lesser General Public License
30 * along with this program; if not, write to the Free Software Foundation,
31 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
32 *****************************************************************************/
34 /*****************************************************************************
36 *****************************************************************************/
42 # define INCL_DOSDEVIOCTL
45 #include <sys/types.h>
51 #include <vlc_common.h>
52 #include <vlc_access.h>
53 #include <vlc_charset.h>
57 #if defined( SYS_BSDI )
59 #elif defined ( __APPLE__ )
60 # include <CoreFoundation/CFBase.h>
61 # include <IOKit/IOKitLib.h>
62 # include <IOKit/storage/IOCDTypes.h>
63 # include <IOKit/storage/IOCDMedia.h>
64 # include <IOKit/storage/IOCDMediaBSDClient.h>
65 #elif defined( HAVE_SCSIREQ_IN_SYS_SCSIIO_H )
66 # include <inttypes.h>
67 # include <sys/cdio.h>
68 # include <sys/scsiio.h>
69 #elif defined( HAVE_IOC_TOC_HEADER_IN_SYS_CDIO_H )
70 # include <sys/cdio.h>
71 # include <sys/cdrio.h>
72 #elif defined( _WIN32 )
74 # include <winioctl.h>
75 #elif defined (__linux__)
76 # include <sys/ioctl.h>
77 # include <linux/cdrom.h>
78 #elif defined( __OS2__ )
82 /*****************************************************************************
83 * vlc_DosDevIOCtl: high memory safe wrapper for DosDevIOCtl
84 *****************************************************************************
85 * Unfortunately, DosDevIOCtl() is not high memory safe API, and is not
86 * covered by os2safe.h. So define a wrapper function for it here.
87 *****************************************************************************/
89 static APIRET vlc_DosDevIOCtl( HFILE hdevice, ULONG category, ULONG function,
90 PVOID pParams, ULONG cbParamLenMax,
91 PULONG pcbParamLen, PVOID pData,
92 ULONG cbDataLenMax, PULONG pcbDataLen )
94 PVOID pParamsLow = NULL;
95 PVOID pDataLow = NULL;
101 rc = DosAllocMem( &pParamsLow, cbParamLenMax, fALLOC );
105 rc = DosAllocMem( &pDataLow, cbDataLenMax, fALLOC );
109 memcpy( pParamsLow, pParams, cbParamLenMax );
110 memcpy( pDataLow, pData, cbDataLenMax );
112 cbParamLenLow = *pcbParamLen;
113 cbDataLenLow = *pcbDataLen;
115 rc = DosDevIOCtl( hdevice, category, function, pParamsLow,
116 cbParamLenMax, &cbParamLenLow, pDataLow, cbDataLenMax,
121 memcpy( pParams, pParamsLow, cbParamLenMax );
122 memcpy( pData, pDataLow, cbDataLenMax );
124 *pcbParamLen = cbParamLenLow;
125 *pcbDataLen = cbDataLenLow;
129 DosFreeMem( pParamsLow);
130 DosFreeMem( pDataLow);
135 # define DosDevIOCtl vlc_DosDevIOCtl
140 #include "cdrom_internals.h"
143 /*****************************************************************************
144 * ioctl_Open: Opens a VCD device or file and returns an opaque handle
145 *****************************************************************************/
146 vcddev_t *ioctl_Open( vlc_object_t *p_this, const char *psz_dev )
151 #if !defined( _WIN32 ) && !defined( __OS2__ )
152 struct stat fileinfo;
155 if( !psz_dev ) return NULL;
158 * Initialize structure with default values
160 p_vcddev = malloc( sizeof(*p_vcddev) );
161 if( p_vcddev == NULL )
163 p_vcddev->i_vcdimage_handle = -1;
164 p_vcddev->psz_dev = NULL;
168 * Check if we are dealing with a device or a file (vcd image)
170 #if defined( _WIN32 ) || defined( __OS2__ )
171 if( (strlen( psz_dev ) == 2 && psz_dev[1] == ':') )
177 if( vlc_stat( psz_dev, &fileinfo ) < 0 )
183 /* Check if this is a block/char device */
184 if( S_ISBLK( fileinfo.st_mode ) || S_ISCHR( fileinfo.st_mode ) )
190 i_ret = OpenVCDImage( p_this, psz_dev, p_vcddev );
195 * open the vcd device
199 i_ret = win32_vcd_open( p_this, psz_dev, p_vcddev );
200 #elif defined( __OS2__ )
201 i_ret = os2_vcd_open( p_this, psz_dev, p_vcddev );
203 p_vcddev->i_device_handle = -1;
204 p_vcddev->i_device_handle = vlc_open( psz_dev, O_RDONLY | O_NONBLOCK );
205 i_ret = (p_vcddev->i_device_handle == -1) ? -1 : 0;
211 p_vcddev->psz_dev = (char *)strdup( psz_dev );
222 /*****************************************************************************
223 * ioctl_Close: Closes an already opened VCD device or file.
224 *****************************************************************************/
225 void ioctl_Close( vlc_object_t * p_this, vcddev_t *p_vcddev )
227 free( p_vcddev->psz_dev );
229 if( p_vcddev->i_vcdimage_handle != -1 )
235 CloseVCDImage( p_this, p_vcddev );
244 if( p_vcddev->h_device_handle )
245 CloseHandle( p_vcddev->h_device_handle );
246 #elif defined( __OS2__ )
248 DosClose( p_vcddev->hcd );
250 if( p_vcddev->i_device_handle != -1 )
251 close( p_vcddev->i_device_handle );
256 /*****************************************************************************
257 * ioctl_GetTracksMap: Read the Table of Content, fill in the pp_sectors map
258 * if pp_sectors is not null and return the number of
260 *****************************************************************************/
261 int ioctl_GetTracksMap( vlc_object_t *p_this, const vcddev_t *p_vcddev,
266 if( p_vcddev->i_vcdimage_handle != -1 )
272 i_tracks = p_vcddev->i_tracks;
276 *pp_sectors = calloc( i_tracks + 1, sizeof(**pp_sectors) );
277 if( *pp_sectors == NULL )
279 memcpy( *pp_sectors, p_vcddev->p_sectors,
280 (i_tracks + 1) * sizeof(**pp_sectors) );
292 #if defined( __APPLE__ )
297 if( ( pTOC = darwin_getTOC( p_this, p_vcddev ) ) == NULL )
299 msg_Err( p_this, "failed to get the TOC" );
303 i_descriptors = CDTOCGetDescriptorCount( pTOC );
304 i_tracks = darwin_getNumberOfTracks( pTOC, i_descriptors );
308 int i, i_leadout = -1;
309 CDTOCDescriptor *pTrackDescriptors;
312 *pp_sectors = calloc( i_tracks + 1, sizeof(**pp_sectors) );
313 if( *pp_sectors == NULL )
315 darwin_freeTOC( pTOC );
319 pTrackDescriptors = pTOC->descriptors;
321 for( i_tracks = 0, i = 0; i < i_descriptors; i++ )
323 track = pTrackDescriptors[i].point;
328 if( track > CD_MAX_TRACK_NO || track < CD_MIN_TRACK_NO )
331 (*pp_sectors)[i_tracks++] =
332 CDConvertMSFToLBA( pTrackDescriptors[i].p );
335 if( i_leadout == -1 )
337 msg_Err( p_this, "leadout not found" );
339 darwin_freeTOC( pTOC );
343 /* set leadout sector */
344 (*pp_sectors)[i_tracks] =
345 CDConvertMSFToLBA( pTrackDescriptors[i_leadout].p );
348 darwin_freeTOC( pTOC );
350 #elif defined( _WIN32 )
351 DWORD dwBytesReturned;
354 if( DeviceIoControl( p_vcddev->h_device_handle, IOCTL_CDROM_READ_TOC,
355 NULL, 0, &cdrom_toc, sizeof(CDROM_TOC),
356 &dwBytesReturned, NULL ) == 0 )
358 msg_Err( p_this, "could not read TOCHDR" );
362 i_tracks = cdrom_toc.LastTrack - cdrom_toc.FirstTrack + 1;
366 *pp_sectors = calloc( i_tracks + 1, sizeof(**pp_sectors) );
367 if( *pp_sectors == NULL )
370 for( int i = 0 ; i <= i_tracks ; i++ )
372 (*pp_sectors)[ i ] = MSF_TO_LBA2(
373 cdrom_toc.TrackData[i].Address[1],
374 cdrom_toc.TrackData[i].Address[2],
375 cdrom_toc.TrackData[i].Address[3] );
376 msg_Dbg( p_this, "p_sectors: %i, %i", i, (*pp_sectors)[i]);
380 #elif defined( __OS2__ )
381 cdrom_get_tochdr_t get_tochdr = {{'C', 'D', '0', '1'}};
382 cdrom_tochdr_t tochdr;
388 rc = DosDevIOCtl( p_vcddev->hcd, IOCTL_CDROMAUDIO,
389 CDROMAUDIO_GETAUDIODISK,
390 &get_tochdr, sizeof( get_tochdr ), ¶m_len,
391 &tochdr, sizeof( tochdr ), &data_len );
394 msg_Err( p_this, "could not read TOCHDR" );
398 i_tracks = tochdr.last_track - tochdr.first_track + 1;
402 cdrom_get_track_t get_track = {{'C', 'D', '0', '1'}, };
406 *pp_sectors = calloc( i_tracks + 1, sizeof(**pp_sectors) );
407 if( *pp_sectors == NULL )
410 for( i = 0 ; i < i_tracks ; i++ )
412 get_track.track = tochdr.first_track + i;
413 rc = DosDevIOCtl( p_vcddev->hcd, IOCTL_CDROMAUDIO,
414 CDROMAUDIO_GETAUDIOTRACK,
415 &get_track, sizeof(get_track), ¶m_len,
416 &track, sizeof(track), &data_len );
419 msg_Err( p_this, "could not read %d track",
424 (*pp_sectors)[ i ] = MSF_TO_LBA2(
428 msg_Dbg( p_this, "p_sectors: %i, %i", i, (*pp_sectors)[i]);
431 /* for lead-out track */
432 (*pp_sectors)[ i ] = MSF_TO_LBA2(
433 tochdr.lead_out.minute,
434 tochdr.lead_out.second,
435 tochdr.lead_out.frame );
436 msg_Dbg( p_this, "p_sectors: %i, %i", i, (*pp_sectors)[i]);
439 #elif defined( HAVE_IOC_TOC_HEADER_IN_SYS_CDIO_H ) \
440 || defined( HAVE_SCSIREQ_IN_SYS_SCSIIO_H )
441 struct ioc_toc_header tochdr;
442 struct ioc_read_toc_entry toc_entries;
444 if( ioctl( p_vcddev->i_device_handle, CDIOREADTOCHEADER, &tochdr )
447 msg_Err( p_this, "could not read TOCHDR" );
451 i_tracks = tochdr.ending_track - tochdr.starting_track + 1;
457 *pp_sectors = calloc( i_tracks + 1, sizeof(**pp_sectors) );
458 if( *pp_sectors == NULL )
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 )
474 if( ioctl( p_vcddev->i_device_handle, CDIOREADTOCENTRYS,
475 &toc_entries ) == -1 )
477 msg_Err( p_this, "could not read the TOC" );
479 free( toc_entries.data );
483 /* Fill the p_sectors structure with the track/sector matches */
484 for( i = 0 ; i <= i_tracks ; i++ )
486 #if defined( HAVE_SCSIREQ_IN_SYS_SCSIIO_H )
487 /* FIXME: is this ok? */
488 (*pp_sectors)[ i ] = toc_entries.data[i].addr.lba;
490 (*pp_sectors)[ i ] = ntohl( toc_entries.data[i].addr.lba );
495 struct cdrom_tochdr tochdr;
496 struct cdrom_tocentry tocent;
498 /* First we read the TOC header */
499 if( ioctl( p_vcddev->i_device_handle, CDROMREADTOCHDR, &tochdr )
502 msg_Err( p_this, "could not read TOCHDR" );
506 i_tracks = tochdr.cdth_trk1 - tochdr.cdth_trk0 + 1;
512 *pp_sectors = calloc( i_tracks + 1, sizeof(**pp_sectors) );
513 if( *pp_sectors == NULL )
516 /* Fill the p_sectors structure with the track/sector matches */
517 for( i = 0 ; i <= i_tracks ; i++ )
519 tocent.cdte_format = CDROM_LBA;
521 ( i == i_tracks ) ? CDROM_LEADOUT : tochdr.cdth_trk0 + i;
523 if( ioctl( p_vcddev->i_device_handle, CDROMREADTOCENTRY,
526 msg_Err( p_this, "could not read TOCENTRY" );
531 (*pp_sectors)[ i ] = tocent.cdte_addr.lba;
540 /****************************************************************************
541 * ioctl_ReadSector: Read VCD or CDDA sectors
542 ****************************************************************************/
543 int ioctl_ReadSectors( vlc_object_t *p_this, const vcddev_t *p_vcddev,
544 int i_sector, uint8_t *p_buffer, int i_nb, int i_type )
549 if( i_type == VCD_TYPE )
550 p_block = malloc( VCD_SECTOR_SIZE * i_nb );
554 if( p_vcddev->i_vcdimage_handle != -1 )
559 if( lseek( p_vcddev->i_vcdimage_handle, i_sector * VCD_SECTOR_SIZE,
562 msg_Err( p_this, "Could not lseek to sector %d", i_sector );
566 if( read( p_vcddev->i_vcdimage_handle, p_block, VCD_SECTOR_SIZE * i_nb)
569 msg_Err( p_this, "Could not read sector %d", i_sector );
581 #if defined( __APPLE__ )
582 dk_cd_read_t cd_read;
584 memset( &cd_read, 0, sizeof(cd_read) );
586 cd_read.offset = i_sector * VCD_SECTOR_SIZE;
587 cd_read.sectorArea = kCDSectorAreaSync | kCDSectorAreaHeader |
588 kCDSectorAreaSubHeader | kCDSectorAreaUser |
589 kCDSectorAreaAuxiliary;
590 cd_read.sectorType = kCDSectorTypeUnknown;
592 cd_read.buffer = p_block;
593 cd_read.bufferLength = VCD_SECTOR_SIZE * i_nb;
595 if( ioctl( p_vcddev->i_device_handle, DKIOCCDREAD, &cd_read ) == -1 )
597 msg_Err( p_this, "could not read block %d", i_sector );
601 #elif defined( _WIN32 )
602 DWORD dwBytesReturned;
603 RAW_READ_INFO cdrom_raw;
605 /* Initialize CDROM_RAW_READ structure */
606 cdrom_raw.DiskOffset.QuadPart = CD_SECTOR_SIZE * i_sector;
607 cdrom_raw.SectorCount = i_nb;
608 cdrom_raw.TrackMode = i_type == VCD_TYPE ? XAForm2 : CDDA;
610 if( DeviceIoControl( p_vcddev->h_device_handle, IOCTL_CDROM_RAW_READ,
611 &cdrom_raw, sizeof(RAW_READ_INFO), p_block,
612 VCD_SECTOR_SIZE * i_nb, &dwBytesReturned,
615 if( i_type == VCD_TYPE )
617 /* Retry in YellowMode2 */
618 cdrom_raw.TrackMode = YellowMode2;
619 if( DeviceIoControl( p_vcddev->h_device_handle,
620 IOCTL_CDROM_RAW_READ, &cdrom_raw,
621 sizeof(RAW_READ_INFO), p_block,
622 VCD_SECTOR_SIZE * i_nb, &dwBytesReturned,
629 #elif defined( __OS2__ )
630 cdrom_readlong_t readlong = {{'C', 'D', '0', '1'}, };
636 readlong.addr_mode = 0; /* LBA mode */
637 readlong.sectors = i_nb;
638 readlong.start = i_sector;
640 rc = DosDevIOCtl( p_vcddev->hcd, IOCTL_CDROMDISK, CDROMDISK_READLONG,
641 &readlong, sizeof( readlong ), ¶m_len,
642 p_block, VCD_SECTOR_SIZE * i_nb, &data_len );
645 msg_Err( p_this, "could not read block %d", i_sector );
649 #elif defined( HAVE_SCSIREQ_IN_SYS_SCSIIO_H )
653 memset( &sc, 0, sizeof(sc) );
655 sc.cmd[1] = i_type == VCD_TYPE ? SECTOR_TYPE_MODE2_FORM2:
657 sc.cmd[2] = (i_sector >> 24) & 0xff;
658 sc.cmd[3] = (i_sector >> 16) & 0xff;
659 sc.cmd[4] = (i_sector >> 8) & 0xff;
660 sc.cmd[5] = (i_sector >> 0) & 0xff;
661 sc.cmd[6] = (i_nb >> 16) & 0xff;
662 sc.cmd[7] = (i_nb >> 8) & 0xff;
663 sc.cmd[8] = (i_nb ) & 0xff;
664 sc.cmd[9] = i_type == VCD_TYPE ? READ_CD_RAW_MODE2 : READ_CD_USERDATA;
665 sc.cmd[10] = 0; /* sub channel */
667 sc.databuf = (caddr_t)p_block;
668 sc.datalen = VCD_SECTOR_SIZE * i_nb;
669 sc.senselen = sizeof( sc.sense );
670 sc.flags = SCCMD_READ;
673 i_ret = ioctl( p_vcddev->i_device_handle, SCIOCCOMMAND, &sc );
676 msg_Err( p_this, "SCIOCCOMMAND failed" );
679 if( sc.retsts || sc.error )
681 msg_Err( p_this, "SCSI command failed: status %d error %d",
682 sc.retsts, sc.error );
686 #elif defined( HAVE_IOC_TOC_HEADER_IN_SYS_CDIO_H )
687 int i_size = VCD_SECTOR_SIZE;
689 if( ioctl( p_vcddev->i_device_handle, CDRIOCSETBLOCKSIZE, &i_size )
692 msg_Err( p_this, "Could not set block size" );
696 if( lseek( p_vcddev->i_device_handle,
697 i_sector * VCD_SECTOR_SIZE, SEEK_SET ) == -1 )
699 msg_Err( p_this, "Could not lseek to sector %d", i_sector );
703 if( read( p_vcddev->i_device_handle,
704 p_block, VCD_SECTOR_SIZE * i_nb ) == -1 )
706 msg_Err( p_this, "Could not read sector %d", i_sector );
711 for( i = 0; i < i_nb; i++ )
713 int i_dummy = i_sector + i + 2 * CD_FRAMES;
715 #define p_msf ((struct cdrom_msf0 *)(p_block + i * VCD_SECTOR_SIZE))
716 p_msf->minute = i_dummy / (CD_FRAMES * CD_SECS);
717 p_msf->second = ( i_dummy % (CD_FRAMES * CD_SECS) ) / CD_FRAMES;
718 p_msf->frame = ( i_dummy % (CD_FRAMES * CD_SECS) ) % CD_FRAMES;
721 if( ioctl( p_vcddev->i_device_handle, CDROMREADRAW,
722 p_block + i * VCD_SECTOR_SIZE ) == -1 )
724 msg_Err( p_this, "could not read block %i from disc",
736 /* For VCDs, we don't want to keep the header and footer of the
738 if( i_type == VCD_TYPE )
740 for( i = 0; i < i_nb; i++ )
742 memcpy( p_buffer + i * VCD_DATA_SIZE,
743 p_block + i * VCD_SECTOR_SIZE + VCD_DATA_START,
752 if( i_type == VCD_TYPE )
757 /****************************************************************************
759 ****************************************************************************/
761 /****************************************************************************
762 * OpenVCDImage: try to open a vcd image from a .cue file
763 ****************************************************************************/
764 static int OpenVCDImage( vlc_object_t * p_this, const char *psz_dev,
769 char *psz_vcdfile = NULL;
770 char *psz_cuefile = NULL;
771 FILE *cuefile = NULL;
772 int *p_sectors = NULL;
774 bool b_found = false;
776 /* Check if we are dealing with a .cue file */
777 p_pos = strrchr( psz_dev, '.' );
778 if( p_pos && !strcmp( p_pos, ".cue" ) )
780 /* psz_dev must be the cue file. Let's assume there's a .bin
781 * file with the same filename */
782 if( asprintf( &psz_vcdfile, "%.*s.bin", (int)(p_pos - psz_dev),
785 psz_cuefile = strdup( psz_dev );
790 /* psz_dev must be the actual vcd file. Let's assume there's a .cue
791 * file with the same filename */
792 if( asprintf( &psz_cuefile, "%.*s.cue", (int)(p_pos - psz_dev),
795 psz_vcdfile = strdup( psz_dev );
799 if( asprintf( &psz_cuefile, "%s.cue", psz_dev ) == -1 )
801 /* If we need to look up the .cue file, then we don't have to look
803 psz_vcdfile = strdup( psz_dev );
806 if( psz_cuefile == NULL || psz_vcdfile == NULL )
809 /* Open the cue file and try to parse it */
810 msg_Dbg( p_this,"trying .cue file: %s", psz_cuefile );
811 cuefile = vlc_fopen( psz_cuefile, "rt" );
812 if( cuefile == NULL )
814 msg_Dbg( p_this, "could not find .cue file" );
818 msg_Dbg( p_this,"guessing vcd image file: %s", psz_vcdfile );
819 p_vcddev->i_vcdimage_handle = vlc_open( psz_vcdfile,
820 O_RDONLY | O_NONBLOCK | O_BINARY );
822 while( fgets( line, 1024, cuefile ) && !b_found )
824 /* We have a cue file, but no valid vcd file yet */
827 int i_temp = sscanf( line, "FILE \"%1023[^\"]\" %15s", filename, type );
831 msg_Dbg( p_this, "the cue file says the data file is %s", type );
832 if( strcasecmp( type, "BINARY" ) )
833 goto error; /* Error if not binary, otherwise treat as case 1 */
835 if( p_vcddev->i_vcdimage_handle == -1 )
837 msg_Dbg( p_this, "we could not find the data file, but we found a new path" );
839 if( *filename != '/' && ((p_pos = strrchr( psz_cuefile, '/' ))
840 || (p_pos = strrchr( psz_cuefile, '\\' ) )) )
842 psz_vcdfile = malloc( strlen(filename) +
843 (p_pos - psz_cuefile + 1) + 1 );
844 strncpy( psz_vcdfile, psz_cuefile, (p_pos - psz_cuefile + 1) );
845 strcpy( psz_vcdfile + (p_pos - psz_cuefile + 1), filename );
846 } else psz_vcdfile = strdup( filename );
847 msg_Dbg( p_this,"using vcd image file: %s", psz_vcdfile );
848 p_vcddev->i_vcdimage_handle = vlc_open( psz_vcdfile,
849 O_RDONLY | O_NONBLOCK | O_BINARY );
857 if( p_vcddev->i_vcdimage_handle == -1)
860 /* Try to parse the i_tracks and p_sectors info so we can just forget
861 * about the cuefile */
864 while( fgets( line, 1024, cuefile ) && i_tracks < INT_MAX-1 )
866 /* look for a TRACK line */
868 if( !sscanf( line, "%9s", psz_dummy ) || strcmp(psz_dummy, "TRACK") )
871 /* look for an INDEX line */
872 while( fgets( line, 1024, cuefile ) )
874 int i_num, i_min, i_sec, i_frame;
876 if( (sscanf( line, "%*9s %2u %2u:%2u:%2u", &i_num,
877 &i_min, &i_sec, &i_frame ) != 4) || (i_num != 1) )
880 int *buf = realloc (p_sectors, (i_tracks + 1) * sizeof (*buf));
884 p_sectors[i_tracks] = MSF_TO_LBA(i_min, i_sec, i_frame);
885 msg_Dbg( p_this, "vcd track %i begins at sector:%i",
886 (int)i_tracks, (int)p_sectors[i_tracks] );
892 /* fill in the last entry */
893 int *buf = realloc (p_sectors, (i_tracks + 1) * sizeof (*buf));
897 p_sectors[i_tracks] = lseek(p_vcddev->i_vcdimage_handle, 0, SEEK_END)
899 msg_Dbg( p_this, "vcd track %i, begins at sector:%i",
900 (int)i_tracks, (int)p_sectors[i_tracks] );
901 p_vcddev->i_tracks = ++i_tracks;
902 p_vcddev->p_sectors = p_sectors;
906 if( cuefile ) fclose( cuefile );
914 /****************************************************************************
915 * CloseVCDImage: closes a vcd image opened by OpenVCDImage
916 ****************************************************************************/
917 static void CloseVCDImage( vlc_object_t * p_this, vcddev_t *p_vcddev )
919 VLC_UNUSED( p_this );
920 if( p_vcddev->i_vcdimage_handle != -1 )
921 close( p_vcddev->i_vcdimage_handle );
925 free( p_vcddev->p_sectors );
928 #if defined( __APPLE__ )
929 /****************************************************************************
930 * darwin_getTOC: get the TOC
931 ****************************************************************************/
932 static CDTOC *darwin_getTOC( vlc_object_t * p_this, const vcddev_t *p_vcddev )
938 io_iterator_t iterator;
939 io_registry_entry_t service;
940 CFMutableDictionaryRef properties;
943 /* get the device name */
944 if( ( psz_devname = strrchr( p_vcddev->psz_dev, '/') ) != NULL )
947 psz_devname = p_vcddev->psz_dev;
949 /* unraw the device name */
950 if( *psz_devname == 'r' )
953 /* get port for IOKit communication */
954 if( ( ret = IOMasterPort( MACH_PORT_NULL, &port ) ) != KERN_SUCCESS )
956 msg_Err( p_this, "IOMasterPort: 0x%08x", ret );
960 /* get service iterator for the device */
961 if( ( ret = IOServiceGetMatchingServices(
962 port, IOBSDNameMatching( port, 0, psz_devname ),
963 &iterator ) ) != KERN_SUCCESS )
965 msg_Err( p_this, "IOServiceGetMatchingServices: 0x%08x", ret );
970 service = IOIteratorNext( iterator );
971 IOObjectRelease( iterator );
973 /* search for kIOCDMediaClass */
974 while( service && !IOObjectConformsTo( service, kIOCDMediaClass ) )
976 if( ( ret = IORegistryEntryGetParentIterator( service,
977 kIOServicePlane, &iterator ) ) != KERN_SUCCESS )
979 msg_Err( p_this, "IORegistryEntryGetParentIterator: 0x%08x", ret );
980 IOObjectRelease( service );
984 IOObjectRelease( service );
985 service = IOIteratorNext( iterator );
986 IOObjectRelease( iterator );
991 msg_Err( p_this, "search for kIOCDMediaClass came up empty" );
995 /* create a CF dictionary containing the TOC */
996 if( ( ret = IORegistryEntryCreateCFProperties( service, &properties,
997 kCFAllocatorDefault, kNilOptions ) ) != KERN_SUCCESS )
999 msg_Err( p_this, "IORegistryEntryCreateCFProperties: 0x%08x", ret );
1000 IOObjectRelease( service );
1004 /* get the TOC from the dictionary */
1005 if( ( data = (CFDataRef) CFDictionaryGetValue( properties,
1006 CFSTR(kIOCDMediaTOCKey) ) ) != NULL )
1011 buf_len = CFDataGetLength( data ) + 1;
1012 range = CFRangeMake( 0, buf_len );
1014 if( ( pTOC = malloc( buf_len ) ) != NULL )
1016 CFDataGetBytes( data, range, (u_char *)pTOC );
1021 msg_Err( p_this, "CFDictionaryGetValue failed" );
1024 CFRelease( properties );
1025 IOObjectRelease( service );
1030 /****************************************************************************
1031 * darwin_getNumberOfTracks: get number of tracks in TOC
1032 ****************************************************************************/
1033 static int darwin_getNumberOfTracks( CDTOC *pTOC, int i_descriptors )
1036 int i, i_tracks = 0;
1037 CDTOCDescriptor *pTrackDescriptors = NULL;
1039 pTrackDescriptors = (CDTOCDescriptor *)pTOC->descriptors;
1041 for( i = i_descriptors; i > 0; i-- )
1043 track = pTrackDescriptors[i].point;
1045 if( track > CD_MAX_TRACK_NO || track < CD_MIN_TRACK_NO )
1053 #endif /* __APPLE__ */
1055 #if defined( _WIN32 )
1056 /*****************************************************************************
1057 * win32_vcd_open: open vcd drive
1058 *****************************************************************************
1059 * Use IOCTLs on WinNT/2K/XP.
1060 *****************************************************************************/
1061 static int win32_vcd_open( vlc_object_t * p_this, const char *psz_dev,
1062 vcddev_t *p_vcddev )
1064 /* Initializations */
1065 p_vcddev->h_device_handle = NULL;
1067 char psz_win32_drive[7];
1069 msg_Dbg( p_this, "using winNT/2K/XP ioctl layer" );
1071 sprintf( psz_win32_drive, "\\\\.\\%c:", psz_dev[0] );
1073 p_vcddev->h_device_handle = CreateFileA( psz_win32_drive, GENERIC_READ,
1074 FILE_SHARE_READ | FILE_SHARE_WRITE,
1075 NULL, OPEN_EXISTING,
1076 FILE_FLAG_NO_BUFFERING |
1077 FILE_FLAG_RANDOM_ACCESS, NULL );
1078 return (p_vcddev->h_device_handle == NULL) ? -1 : 0;
1084 /*****************************************************************************
1085 * os2_vcd_open: open vcd drive
1086 *****************************************************************************/
1087 static int os2_vcd_open( vlc_object_t * p_this, const char *psz_dev,
1088 vcddev_t *p_vcddev )
1090 char device[] = "X:";
1097 device[0] = psz_dev[0];
1098 rc = DosOpen( device, &hcd, &i_action, 0, FILE_NORMAL,
1099 OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_FAIL_IF_NEW,
1100 OPEN_ACCESS_READONLY | OPEN_SHARE_DENYNONE | OPEN_FLAGS_DASD,
1104 msg_Err( p_this, "could not open the device %s", psz_dev );
1109 p_vcddev->hcd = hcd;
1116 static void astrcat( char **ppsz_dst, char *psz_src )
1118 char *psz_old = *ppsz_dst;
1122 *ppsz_dst = strdup( psz_src );
1126 if( asprintf( ppsz_dst, "%s%s", psz_old, psz_src ) < 0 )
1127 *ppsz_dst = psz_old;
1134 static int CdTextParse( vlc_meta_t ***ppp_tracks, int *pi_tracks,
1135 const uint8_t *p_buffer, int i_buffer )
1137 char *pppsz_info[128][0x10];
1138 int i_track_last = -1;
1142 memset( pppsz_info, 0, sizeof(pppsz_info) );
1144 for( int i = 0; i < (i_buffer-4)/18; i++ )
1146 const uint8_t *p_block = &p_buffer[4 + 18*i];
1147 char psz_text[12+1];
1149 const int i_pack_type = p_block[0];
1150 if( i_pack_type < 0x80 || i_pack_type > 0x8f )
1153 const int i_track_number = (p_block[1] >> 0)&0x7f;
1154 const int i_extension_flag = ( p_block[1] >> 7)& 0x01;
1155 if( i_extension_flag )
1158 //const int i_sequence_number = p_block[2];
1159 //const int i_charater_position = (p_block[3] >> 0) &0x0f;
1160 //const int i_block_number = (p_block[3] >> 4) &0x07;
1161 /* TODO unicode support
1162 * I need a sample */
1163 //const int i_unicode = ( p_block[3] >> 7)&0x01;
1164 //const int i_crc = (p_block[4+12] << 8) | (p_block[4+13] << 0);
1167 memcpy( psz_text, &p_block[4], 12 );
1168 psz_text[12] = '\0';
1171 int i_track = i_track_number;
1172 char *psz_track = &psz_text[0];
1173 while( i_track <= 127 && psz_track < &psz_text[12] )
1175 //fprintf( stderr, "t=%d psz_track=%p end=%p", i_track, psz_track, &psz_text[12] );
1178 astrcat( &pppsz_info[i_track][i_pack_type-0x80], psz_track );
1179 i_track_last = __MAX( i_track_last, i_track );
1183 psz_track += 1 + strlen(psz_track);
1187 if( i_track_last < 0 )
1190 vlc_meta_t **pp_tracks = calloc( i_track_last+1, sizeof(*pp_tracks) );
1194 for( int j = 0; j < 0x10; j++ )
1196 for( int i = 0; i <= i_track_last; i++ )
1199 if( pppsz_info[i][j] )
1200 EnsureUTF8( pppsz_info[i][j] );
1203 const char *psz_default = pppsz_info[0][j];
1204 const char *psz_value = pppsz_info[i][j];
1206 if( !psz_value && !psz_default )
1208 vlc_meta_t *p_track = pp_tracks[i];
1211 p_track = pp_tracks[i] = vlc_meta_New();
1217 case 0x00: /* Album/Title */
1220 vlc_meta_SetAlbum( p_track, psz_value );
1225 vlc_meta_SetTitle( p_track, psz_value );
1227 vlc_meta_SetAlbum( p_track, psz_default );
1230 case 0x01: /* Performer */
1231 vlc_meta_SetArtist( p_track,
1232 psz_value ? psz_value : psz_default );
1234 case 0x05: /* Messages */
1235 vlc_meta_SetDescription( p_track,
1236 psz_value ? psz_value : psz_default );
1238 case 0x07: /* Genre */
1239 vlc_meta_SetGenre( p_track,
1240 psz_value ? psz_value : psz_default );
1242 /* FIXME unsupported:
1252 for( int j = 0; j < 0x10; j++ )
1253 for( int i = 0; i <= i_track_last; i++ )
1254 free( pppsz_info[i][j] );
1256 *ppp_tracks = pp_tracks;
1257 *pi_tracks = i_track_last+1;
1258 return pp_tracks ? 0 : -1;
1261 #if defined( __APPLE__ ) || \
1262 defined( __OS2__ ) || \
1263 defined( HAVE_IOC_TOC_HEADER_IN_SYS_CDIO_H ) || \
1264 defined( HAVE_SCSIREQ_IN_SYS_SCSIIO_H )
1265 static int CdTextRead( vlc_object_t *p_object, const vcddev_t *p_vcddev,
1266 uint8_t **pp_buffer, int *pi_buffer )
1268 VLC_UNUSED( p_object );
1269 VLC_UNUSED( p_vcddev );
1270 VLC_UNUSED( pp_buffer );
1271 VLC_UNUSED( pi_buffer );
1274 #elif defined( _WIN32 )
1275 static int CdTextRead( vlc_object_t *p_object, const vcddev_t *p_vcddev,
1276 uint8_t **pp_buffer, int *pi_buffer )
1278 VLC_UNUSED( p_object );
1280 CDROM_READ_TOC_EX TOCEx;
1281 memset(&TOCEx, 0, sizeof(TOCEx));
1282 TOCEx.Format = CDROM_READ_TOC_EX_FORMAT_CDTEXT;
1284 const int i_header_size = __MAX( 4, MINIMUM_CDROM_READ_TOC_EX_SIZE );
1285 uint8_t header[i_header_size];
1287 if( !DeviceIoControl( p_vcddev->h_device_handle, IOCTL_CDROM_READ_TOC_EX,
1288 &TOCEx, sizeof(TOCEx), header, i_header_size, &i_read, 0 ) )
1291 const int i_text = 2 + (header[0] << 8) + header[1];
1295 /* Read complete CD-TEXT */
1296 uint8_t *p_text = calloc( 1, i_text );
1298 return VLC_EGENERIC;
1300 if( !DeviceIoControl( p_vcddev->h_device_handle, IOCTL_CDROM_READ_TOC_EX,
1301 &TOCEx, sizeof(TOCEx), p_text, i_text, &i_read, 0 ) )
1304 return VLC_EGENERIC;
1308 *pp_buffer = p_text;
1309 *pi_buffer = i_text;
1313 static int CdTextRead( vlc_object_t *p_object, const vcddev_t *p_vcddev,
1314 uint8_t **pp_buffer, int *pi_buffer )
1316 VLC_UNUSED( p_object );
1318 if( p_vcddev->i_device_handle == -1 )
1321 struct cdrom_generic_command gc;
1324 /* Read CD-TEXT size */
1325 memset( header, 0, sizeof(header) );
1326 memset( &gc, 0, sizeof(gc) );
1327 gc.cmd[0] = 0x43; /* Read TOC */
1328 gc.cmd[1] = 0x02; /* MSF */
1329 gc.cmd[2] = 5; /* CD-Text */
1330 gc.cmd[7] = ( sizeof(header) >> 8 ) & 0xff;
1331 gc.cmd[8] = ( sizeof(header) >> 0 ) & 0xff;
1333 gc.buflen = sizeof(header);
1335 gc.data_direction = CGC_DATA_READ;
1338 if( ioctl( p_vcddev->i_device_handle, CDROM_SEND_PACKET, &gc ) == -1 )
1339 return VLC_EGENERIC;
1341 /* If the size is less than 4 it is an error, if it 4 then
1342 * it means no text data */
1343 const int i_text = 2 + (header[0] << 8) + header[1];
1345 return VLC_EGENERIC;
1347 /* Read complete CD-TEXT */
1348 uint8_t *p_text = calloc( 1, i_text );
1350 return VLC_EGENERIC;
1352 memset( &gc, 0, sizeof(gc) );
1353 gc.cmd[0] = 0x43; /* Read TOC */
1354 gc.cmd[1] = 0x02; /* MSF */
1355 gc.cmd[2] = 5; /* CD-Text */
1356 gc.cmd[7] = ( i_text >> 8 ) & 0xff;
1357 gc.cmd[8] = ( i_text >> 0 ) & 0xff;
1361 gc.data_direction = CGC_DATA_READ;
1364 if( ioctl( p_vcddev->i_device_handle, CDROM_SEND_PACKET, &gc ) == -1 )
1367 return VLC_EGENERIC;
1371 *pp_buffer = p_text;
1372 *pi_buffer = i_text;
1377 int ioctl_GetCdText( vlc_object_t *p_object, const vcddev_t *p_vcddev,
1378 vlc_meta_t ***ppp_tracks, int *pi_tracks )
1383 if( p_vcddev->i_vcdimage_handle != -1 )
1386 if( CdTextRead( p_object, p_vcddev, &p_text, &i_text ) )
1389 CdTextParse( ppp_tracks, pi_tracks, p_text, i_text );