cdrom.c 36.9 KB
Newer Older
1 2 3
/****************************************************************************
 * cdrom.c: cdrom tools
 *****************************************************************************
4
 * Copyright (C) 1998-2001 the VideoLAN team
5
 * $Id$
6
 *
gbazin's avatar
 
gbazin committed
7 8 9
 * Authors: Johan Bilien <jobi@via.ecp.fr>
 *          Gildas Bazin <gbazin@netcourrier.com>
 *          Jon Lech Johansen <jon-vl@nanocrew.net>
10 11 12 13 14
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
15
 *
16 17 18 19 20 21 22
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
dionoea's avatar
dionoea committed
23
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 25 26 27 28
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
29 30 31 32
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

33
#include <vlc_common.h>
zorglub's avatar
zorglub committed
34
#include <vlc_access.h>
35
#include <vlc_charset.h>
36
#include <vlc_fs.h>
37
#include <limits.h>
38 39 40 41 42

#ifdef HAVE_UNISTD_H
#   include <unistd.h>
#endif

43
#include <sys/types.h>
44 45 46
#ifdef HAVE_SYS_STAT_H
#   include <sys/stat.h>
#endif
47
#ifdef HAVE_FCNTL_H
48
#   include <fcntl.h>
gbazin's avatar
 
gbazin committed
49
#endif
50 51 52
#ifdef HAVE_ARPA_INET_H
#   include <arpa/inet.h>
#endif
53 54 55

#if defined( SYS_BSDI )
#   include <dvd.h>
56
#elif defined ( __APPLE__ )
57 58 59 60 61
#   include <CoreFoundation/CFBase.h>
#   include <IOKit/IOKitLib.h>
#   include <IOKit/storage/IOCDTypes.h>
#   include <IOKit/storage/IOCDMedia.h>
#   include <IOKit/storage/IOCDMediaBSDClient.h>
62
#elif defined( HAVE_SCSIREQ_IN_SYS_SCSIIO_H )
jorrizza's avatar
jorrizza committed
63
#   include <inttypes.h>
64 65
#   include <sys/cdio.h>
#   include <sys/scsiio.h>
66 67 68
#elif defined( HAVE_IOC_TOC_HEADER_IN_SYS_CDIO_H )
#   include <sys/cdio.h>
#   include <sys/cdrio.h>
gbazin's avatar
 
gbazin committed
69 70 71
#elif defined( WIN32 )
#   include <windows.h>
#   include <winioctl.h>
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
72 73
#elif defined (__linux__)
#   include <sys/ioctl.h>
74
#   include <linux/cdrom.h>
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
75 76
#else
#   error FIXME
77 78
#endif

gbazin's avatar
 
gbazin committed
79
#include "cdrom_internals.h"
80
#include "cdrom.h"
81
#include <vlc_meta.h>
82 83

/*****************************************************************************
gbazin's avatar
 
gbazin committed
84
 * ioctl_Open: Opens a VCD device or file and returns an opaque handle
85
 *****************************************************************************/
gbazin's avatar
 
gbazin committed
86 87 88 89 90 91 92
vcddev_t *ioctl_Open( vlc_object_t *p_this, const char *psz_dev )
{
    int i_ret;
    int b_is_file;
    vcddev_t *p_vcddev;
#ifndef WIN32
    struct stat fileinfo;
93 94
#endif

gbazin's avatar
 
gbazin committed
95
    if( !psz_dev ) return NULL;
96

gbazin's avatar
 
gbazin committed
97 98 99
    /*
     *  Initialize structure with default values
     */
Laurent Aimar's avatar
Laurent Aimar committed
100
    p_vcddev = malloc( sizeof(*p_vcddev) );
gbazin's avatar
 
gbazin committed
101 102 103 104 105 106 107 108 109 110
    if( p_vcddev == NULL )
        return NULL;
    p_vcddev->i_vcdimage_handle = -1;
    p_vcddev->psz_dev = NULL;
    b_is_file = 1;

    /*
     *  Check if we are dealing with a device or a file (vcd image)
     */
#ifdef WIN32
gbazin's avatar
 
gbazin committed
111
    if( (strlen( psz_dev ) == 2 && psz_dev[1] == ':') )
gbazin's avatar
 
gbazin committed
112 113
    {
        b_is_file = 0;
114 115
    }

gbazin's avatar
 
gbazin committed
116
#else
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
117
    if( utf8_stat( psz_dev, &fileinfo ) < 0 )
gbazin's avatar
 
gbazin committed
118 119 120 121
    {
        free( p_vcddev );
        return NULL;
    }
122

gbazin's avatar
 
gbazin committed
123 124 125 126
    /* Check if this is a block/char device */
    if( S_ISBLK( fileinfo.st_mode ) || S_ISCHR( fileinfo.st_mode ) )
        b_is_file = 0;
#endif
127

gbazin's avatar
 
gbazin committed
128
    if( b_is_file )
129
    {
gbazin's avatar
 
gbazin committed
130
        i_ret = OpenVCDImage( p_this, psz_dev, p_vcddev );
131
    }
gbazin's avatar
 
gbazin committed
132 133 134 135 136
    else
    {
        /*
         *  open the vcd device
         */
137

gbazin's avatar
 
gbazin committed
138 139
#ifdef WIN32
        i_ret = win32_vcd_open( p_this, psz_dev, p_vcddev );
140
#else
gbazin's avatar
 
gbazin committed
141
        p_vcddev->i_device_handle = -1;
142
        p_vcddev->i_device_handle = utf8_open( psz_dev, O_RDONLY | O_NONBLOCK );
gbazin's avatar
 
gbazin committed
143 144 145
        i_ret = (p_vcddev->i_device_handle == -1) ? -1 : 0;
#endif
    }
146

gbazin's avatar
 
gbazin committed
147
    if( i_ret == 0 )
148
    {
gbazin's avatar
 
gbazin committed
149 150 151 152 153 154
        p_vcddev->psz_dev = (char *)strdup( psz_dev );
    }
    else
    {
        free( p_vcddev );
        p_vcddev = NULL;
155 156
    }

gbazin's avatar
 
gbazin committed
157
    return p_vcddev;
158 159 160
}

/*****************************************************************************
gbazin's avatar
 
gbazin committed
161
 * ioctl_Close: Closes an already opened VCD device or file.
162
 *****************************************************************************/
gbazin's avatar
 
gbazin committed
163
void ioctl_Close( vlc_object_t * p_this, vcddev_t *p_vcddev )
164
{
165
    free( p_vcddev->psz_dev );
166

gbazin's avatar
 
gbazin committed
167
    if( p_vcddev->i_vcdimage_handle != -1 )
168
    {
gbazin's avatar
 
gbazin committed
169 170 171 172 173 174
        /*
         *  vcd image mode
         */

        CloseVCDImage( p_this, p_vcddev );
        return;
175 176
    }

gbazin's avatar
 
gbazin committed
177 178 179
    /*
     *  vcd device mode
     */
180

gbazin's avatar
 
gbazin committed
181 182 183 184 185 186 187
#ifdef WIN32
    if( p_vcddev->h_device_handle )
        CloseHandle( p_vcddev->h_device_handle );
#else
    if( p_vcddev->i_device_handle != -1 )
        close( p_vcddev->i_device_handle );
#endif
188
    free( p_vcddev );
gbazin's avatar
 
gbazin committed
189
}
190

gbazin's avatar
 
gbazin committed
191 192 193 194 195 196 197 198 199 200 201
/*****************************************************************************
 * ioctl_GetTracksMap: Read the Table of Content, fill in the pp_sectors map
 *                     if pp_sectors is not null and return the number of
 *                     tracks available.
 *****************************************************************************/
int ioctl_GetTracksMap( vlc_object_t *p_this, const vcddev_t *p_vcddev,
                        int **pp_sectors )
{
    int i_tracks = 0;

    if( p_vcddev->i_vcdimage_handle != -1 )
202
    {
gbazin's avatar
 
gbazin committed
203 204 205
        /*
         *  vcd image mode
         */
206

gbazin's avatar
 
gbazin committed
207
        i_tracks = p_vcddev->i_tracks;
208

gbazin's avatar
 
gbazin committed
209 210
        if( pp_sectors )
        {
211
            *pp_sectors = calloc( i_tracks + 1, sizeof(**pp_sectors) );
gbazin's avatar
 
gbazin committed
212 213 214
            if( *pp_sectors == NULL )
                return 0;
            memcpy( *pp_sectors, p_vcddev->p_sectors,
Laurent Aimar's avatar
Laurent Aimar committed
215
                    (i_tracks + 1) * sizeof(**pp_sectors) );
gbazin's avatar
 
gbazin committed
216
        }
217

gbazin's avatar
 
gbazin committed
218
        return i_tracks;
219
    }
gbazin's avatar
 
gbazin committed
220
    else
221 222
    {

gbazin's avatar
 
gbazin committed
223 224 225
        /*
         *  vcd device mode
         */
226

227
#if defined( __APPLE__ )
gbazin's avatar
 
gbazin committed
228 229 230 231

        CDTOC *pTOC;
        int i_descriptors;

232
        if( ( pTOC = darwin_getTOC( p_this, p_vcddev ) ) == NULL )
gbazin's avatar
 
gbazin committed
233 234 235 236 237
        {
            msg_Err( p_this, "failed to get the TOC" );
            return 0;
        }

238
        i_descriptors = CDTOCGetDescriptorCount( pTOC );
gbazin's avatar
 
gbazin committed
239 240 241 242 243 244 245 246
        i_tracks = darwin_getNumberOfTracks( pTOC, i_descriptors );

        if( pp_sectors )
        {
            int i, i_leadout = -1;
            CDTOCDescriptor *pTrackDescriptors;
            u_char track;

247
            *pp_sectors = calloc( i_tracks + 1, sizeof(**pp_sectors) );
gbazin's avatar
 
gbazin committed
248 249 250 251 252 253 254 255
            if( *pp_sectors == NULL )
            {
                darwin_freeTOC( pTOC );
                return 0;
            }

            pTrackDescriptors = pTOC->descriptors;

256
            for( i_tracks = 0, i = 0; i < i_descriptors; i++ )
gbazin's avatar
 
gbazin committed
257 258 259 260 261 262 263 264 265
            {
                track = pTrackDescriptors[i].point;

                if( track == 0xA2 )
                    i_leadout = i;

                if( track > CD_MAX_TRACK_NO || track < CD_MIN_TRACK_NO )
                    continue;

266
                (*pp_sectors)[i_tracks++] =
gbazin's avatar
 
gbazin committed
267 268 269 270 271 272 273 274 275 276 277 278
                    CDConvertMSFToLBA( pTrackDescriptors[i].p );
            }

            if( i_leadout == -1 )
            {
                msg_Err( p_this, "leadout not found" );
                free( *pp_sectors );
                darwin_freeTOC( pTOC );
                return 0;
            }

            /* set leadout sector */
279
            (*pp_sectors)[i_tracks] =
gbazin's avatar
 
gbazin committed
280 281 282 283 284 285
                CDConvertMSFToLBA( pTrackDescriptors[i_leadout].p );
        }

        darwin_freeTOC( pTOC );

#elif defined( WIN32 )
286 287
        DWORD dwBytesReturned;
        CDROM_TOC cdrom_toc;
gbazin's avatar
 
gbazin committed
288

289 290 291 292 293 294 295
        if( DeviceIoControl( p_vcddev->h_device_handle, IOCTL_CDROM_READ_TOC,
                             NULL, 0, &cdrom_toc, sizeof(CDROM_TOC),
                             &dwBytesReturned, NULL ) == 0 )
        {
            msg_Err( p_this, "could not read TOCHDR" );
            return 0;
        }
gbazin's avatar
 
gbazin committed
296

297
        i_tracks = cdrom_toc.LastTrack - cdrom_toc.FirstTrack + 1;
gbazin's avatar
 
gbazin committed
298

299
        if( pp_sectors )
gbazin's avatar
 
gbazin committed
300
        {
301 302
            *pp_sectors = calloc( i_tracks + 1, sizeof(**pp_sectors) );
            if( *pp_sectors == NULL )
gbazin's avatar
 
gbazin committed
303 304
                return 0;

305
            for( int i = 0 ; i <= i_tracks ; i++ )
gbazin's avatar
 
gbazin committed
306
            {
307
                (*pp_sectors)[ i ] = MSF_TO_LBA2(
gbazin's avatar
 
gbazin committed
308 309 310
                                           cdrom_toc.TrackData[i].Address[1],
                                           cdrom_toc.TrackData[i].Address[2],
                                           cdrom_toc.TrackData[i].Address[3] );
311 312
                msg_Dbg( p_this, "p_sectors: %i, %i", i, (*pp_sectors)[i]);
             }
gbazin's avatar
 
gbazin committed
313
        }
314

315 316
#elif defined( HAVE_IOC_TOC_HEADER_IN_SYS_CDIO_H ) \
       || defined( HAVE_SCSIREQ_IN_SYS_SCSIIO_H )
gbazin's avatar
 
gbazin committed
317 318
        struct ioc_toc_header tochdr;
        struct ioc_read_toc_entry toc_entries;
319

320
        if( ioctl( p_vcddev->i_device_handle, CDIOREADTOCHEADER, &tochdr )
gbazin's avatar
 
gbazin committed
321 322 323 324 325
            == -1 )
        {
            msg_Err( p_this, "could not read TOCHDR" );
            return 0;
        }
326

gbazin's avatar
 
gbazin committed
327 328 329 330 331 332
        i_tracks = tochdr.ending_track - tochdr.starting_track + 1;

        if( pp_sectors )
        {
             int i;

333
             *pp_sectors = calloc( i_tracks + 1, sizeof(**pp_sectors) );
gbazin's avatar
 
gbazin committed
334
             if( *pp_sectors == NULL )
Rafaël Carré's avatar
Rafaël Carré committed
335
                 return 0;
gbazin's avatar
 
gbazin committed
336 337 338 339

             toc_entries.address_format = CD_LBA_FORMAT;
             toc_entries.starting_track = 0;
             toc_entries.data_len = ( i_tracks + 1 ) *
340
                                        sizeof( struct cd_toc_entry );
gbazin's avatar
 
gbazin committed
341 342 343 344 345 346 347
             toc_entries.data = (struct cd_toc_entry *)
                                    malloc( toc_entries.data_len );
             if( toc_entries.data == NULL )
             {
                 free( *pp_sectors );
                 return 0;
             }
348

gbazin's avatar
 
gbazin committed
349 350 351 352 353 354 355 356 357
             /* Read the TOC */
             if( ioctl( p_vcddev->i_device_handle, CDIOREADTOCENTRYS,
                        &toc_entries ) == -1 )
             {
                 msg_Err( p_this, "could not read the TOC" );
                 free( *pp_sectors );
                 free( toc_entries.data );
                 return 0;
             }
358

gbazin's avatar
 
gbazin committed
359 360 361
             /* Fill the p_sectors structure with the track/sector matches */
             for( i = 0 ; i <= i_tracks ; i++ )
             {
362 363 364 365
#if defined( HAVE_SCSIREQ_IN_SYS_SCSIIO_H )
                 /* FIXME: is this ok? */
                 (*pp_sectors)[ i ] = toc_entries.data[i].addr.lba;
#else
gbazin's avatar
 
gbazin committed
366
                 (*pp_sectors)[ i ] = ntohl( toc_entries.data[i].addr.lba );
367
#endif
gbazin's avatar
 
gbazin committed
368 369
             }
        }
370
#else
gbazin's avatar
 
gbazin committed
371 372
        struct cdrom_tochdr   tochdr;
        struct cdrom_tocentry tocent;
373

gbazin's avatar
 
gbazin committed
374 375 376 377 378 379 380
        /* First we read the TOC header */
        if( ioctl( p_vcddev->i_device_handle, CDROMREADTOCHDR, &tochdr )
            == -1 )
        {
            msg_Err( p_this, "could not read TOCHDR" );
            return 0;
        }
381

gbazin's avatar
 
gbazin committed
382
        i_tracks = tochdr.cdth_trk1 - tochdr.cdth_trk0 + 1;
383

gbazin's avatar
 
gbazin committed
384
        if( pp_sectors )
385
        {
gbazin's avatar
 
gbazin committed
386 387
            int i;

388
            *pp_sectors = calloc( i_tracks + 1, sizeof(**pp_sectors) );
gbazin's avatar
 
gbazin committed
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
            if( *pp_sectors == NULL )
                return 0;

            /* Fill the p_sectors structure with the track/sector matches */
            for( i = 0 ; i <= i_tracks ; i++ )
            {
                tocent.cdte_format = CDROM_LBA;
                tocent.cdte_track =
                    ( i == i_tracks ) ? CDROM_LEADOUT : tochdr.cdth_trk0 + i;

                if( ioctl( p_vcddev->i_device_handle, CDROMREADTOCENTRY,
                           &tocent ) == -1 )
                {
                    msg_Err( p_this, "could not read TOCENTRY" );
                    free( *pp_sectors );
                    return 0;
                }

                (*pp_sectors)[ i ] = tocent.cdte_addr.lba;
            }
409 410 411
        }
#endif

gbazin's avatar
 
gbazin committed
412 413
        return i_tracks;
    }
414 415 416
}

/****************************************************************************
gbazin's avatar
 
gbazin committed
417
 * ioctl_ReadSector: Read VCD or CDDA sectors
418
 ****************************************************************************/
gbazin's avatar
 
gbazin committed
419
int ioctl_ReadSectors( vlc_object_t *p_this, const vcddev_t *p_vcddev,
420
                       int i_sector, uint8_t *p_buffer, int i_nb, int i_type )
421
{
422
    uint8_t *p_block;
gbazin's avatar
 
gbazin committed
423 424
    int i;

Laurent Aimar's avatar
Laurent Aimar committed
425 426 427 428
    if( i_type == VCD_TYPE )
        p_block = malloc( VCD_SECTOR_SIZE * i_nb );
    else
        p_block = p_buffer;
429

gbazin's avatar
 
gbazin committed
430 431 432 433 434 435 436 437 438
    if( p_vcddev->i_vcdimage_handle != -1 )
    {
        /*
         *  vcd image mode
         */
        if( lseek( p_vcddev->i_vcdimage_handle, i_sector * VCD_SECTOR_SIZE,
                   SEEK_SET ) == -1 )
        {
            msg_Err( p_this, "Could not lseek to sector %d", i_sector );
gbazin's avatar
 
gbazin committed
439
            if( i_type == VCD_TYPE ) free( p_block );
gbazin's avatar
 
gbazin committed
440 441 442
            return -1;
        }

gbazin's avatar
 
gbazin committed
443
        if( read( p_vcddev->i_vcdimage_handle, p_block, VCD_SECTOR_SIZE * i_nb)
gbazin's avatar
 
gbazin committed
444 445
            == -1 )
        {
gbazin's avatar
 
gbazin committed
446 447
            msg_Err( p_this, "Could not read sector %d", i_sector );
            if( i_type == VCD_TYPE ) free( p_block );
gbazin's avatar
 
gbazin committed
448 449 450 451 452 453 454 455 456 457 458
            return -1;
        }

    }
    else
    {

        /*
         *  vcd device mode
         */

459
#if defined( __APPLE__ )
gbazin's avatar
 
gbazin committed
460
        dk_cd_read_t cd_read;
461

gbazin's avatar
 
gbazin committed
462
        memset( &cd_read, 0, sizeof(cd_read) );
463

gbazin's avatar
 
gbazin committed
464 465 466 467 468
        cd_read.offset = i_sector * VCD_SECTOR_SIZE;
        cd_read.sectorArea = kCDSectorAreaSync | kCDSectorAreaHeader |
                             kCDSectorAreaSubHeader | kCDSectorAreaUser |
                             kCDSectorAreaAuxiliary;
        cd_read.sectorType = kCDSectorTypeUnknown;
469

gbazin's avatar
 
gbazin committed
470
        cd_read.buffer = p_block;
gbazin's avatar
 
gbazin committed
471
        cd_read.bufferLength = VCD_SECTOR_SIZE * i_nb;
472

gbazin's avatar
 
gbazin committed
473 474 475
        if( ioctl( p_vcddev->i_device_handle, DKIOCCDREAD, &cd_read ) == -1 )
        {
            msg_Err( p_this, "could not read block %d", i_sector );
gbazin's avatar
 
gbazin committed
476
            if( i_type == VCD_TYPE ) free( p_block );
gbazin's avatar
 
gbazin committed
477 478 479 480
            return -1;
        }

#elif defined( WIN32 )
481 482 483 484 485 486 487 488 489 490 491 492
        DWORD dwBytesReturned;
        RAW_READ_INFO cdrom_raw;

        /* Initialize CDROM_RAW_READ structure */
        cdrom_raw.DiskOffset.QuadPart = CD_SECTOR_SIZE * i_sector;
        cdrom_raw.SectorCount = i_nb;
        cdrom_raw.TrackMode =  i_type == VCD_TYPE ? XAForm2 : CDDA;

        if( DeviceIoControl( p_vcddev->h_device_handle, IOCTL_CDROM_RAW_READ,
                             &cdrom_raw, sizeof(RAW_READ_INFO), p_block,
                             VCD_SECTOR_SIZE * i_nb, &dwBytesReturned,
                             NULL ) == 0 )
gbazin's avatar
 
gbazin committed
493
        {
494
            if( i_type == VCD_TYPE )
gbazin's avatar
 
gbazin committed
495
            {
496 497 498 499 500 501 502
                /* Retry in YellowMode2 */
                cdrom_raw.TrackMode = YellowMode2;
                if( DeviceIoControl( p_vcddev->h_device_handle,
                                     IOCTL_CDROM_RAW_READ, &cdrom_raw,
                                     sizeof(RAW_READ_INFO), p_block,
                                     VCD_SECTOR_SIZE * i_nb, &dwBytesReturned,
                                     NULL ) == 0 )
gbazin's avatar
 
gbazin committed
503
                {
504 505
                    free( p_block );
                    return -1;
gbazin's avatar
 
gbazin committed
506
                }
gbazin's avatar
 
gbazin committed
507
            }
508
            else return -1;
gbazin's avatar
 
gbazin committed
509
        }
510

511 512 513 514 515 516
#elif defined( HAVE_SCSIREQ_IN_SYS_SCSIIO_H )
        struct scsireq  sc;
        int i_ret;

        memset( &sc, 0, sizeof(sc) );
        sc.cmd[0] = 0xBE;
gbazin's avatar
 
gbazin committed
517 518
        sc.cmd[1] = i_type == VCD_TYPE ? SECTOR_TYPE_MODE2_FORM2:
                                         SECTOR_TYPE_CDDA;
519 520 521 522
        sc.cmd[2] = (i_sector >> 24) & 0xff;
        sc.cmd[3] = (i_sector >> 16) & 0xff;
        sc.cmd[4] = (i_sector >>  8) & 0xff;
        sc.cmd[5] = (i_sector >>  0) & 0xff;
gbazin's avatar
 
gbazin committed
523 524 525 526
        sc.cmd[6] = (i_nb >> 16) & 0xff;
        sc.cmd[7] = (i_nb >>  8) & 0xff;
        sc.cmd[8] = (i_nb      ) & 0xff;
        sc.cmd[9] = i_type == VCD_TYPE ? READ_CD_RAW_MODE2 : READ_CD_USERDATA;
gbazin's avatar
 
gbazin committed
527
        sc.cmd[10] = 0; /* sub channel */
528 529
        sc.cmdlen = 12;
        sc.databuf = (caddr_t)p_block;
gbazin's avatar
 
gbazin committed
530
        sc.datalen = VCD_SECTOR_SIZE * i_nb;
531 532 533 534
        sc.senselen = sizeof( sc.sense );
        sc.flags = SCCMD_READ;
        sc.timeout = 10000;

jorrizza's avatar
jorrizza committed
535
        i_ret = ioctl( p_vcddev->i_device_handle, SCIOCCOMMAND, &sc );
536 537 538
        if( i_ret == -1 )
        {
            msg_Err( p_this, "SCIOCCOMMAND failed" );
gbazin's avatar
 
gbazin committed
539
            if( i_type == VCD_TYPE ) free( p_block );
540 541 542 543
            return -1;
        }
        if( sc.retsts || sc.error )
        {
544
            msg_Err( p_this, "SCSI command failed: status %d error %d",
545
                             sc.retsts, sc.error );
gbazin's avatar
 
gbazin committed
546
            if( i_type == VCD_TYPE ) free( p_block );
547 548 549
           return -1;
        }

550
#elif defined( HAVE_IOC_TOC_HEADER_IN_SYS_CDIO_H )
gbazin's avatar
 
gbazin committed
551 552 553 554 555 556
        int i_size = VCD_SECTOR_SIZE;

        if( ioctl( p_vcddev->i_device_handle, CDRIOCSETBLOCKSIZE, &i_size )
            == -1 )
        {
            msg_Err( p_this, "Could not set block size" );
gbazin's avatar
 
gbazin committed
557
            if( i_type == VCD_TYPE ) free( p_block );
gbazin's avatar
 
gbazin committed
558 559 560 561 562 563 564
            return( -1 );
        }

        if( lseek( p_vcddev->i_device_handle,
                   i_sector * VCD_SECTOR_SIZE, SEEK_SET ) == -1 )
        {
            msg_Err( p_this, "Could not lseek to sector %d", i_sector );
gbazin's avatar
 
gbazin committed
565
            if( i_type == VCD_TYPE ) free( p_block );
gbazin's avatar
 
gbazin committed
566 567 568
            return( -1 );
        }

gbazin's avatar
 
gbazin committed
569 570
        if( read( p_vcddev->i_device_handle,
                  p_block, VCD_SECTOR_SIZE * i_nb ) == -1 )
gbazin's avatar
 
gbazin committed
571 572
        {
            msg_Err( p_this, "Could not read sector %d", i_sector );
gbazin's avatar
 
gbazin committed
573
            if( i_type == VCD_TYPE ) free( p_block );
gbazin's avatar
 
gbazin committed
574 575 576 577
            return( -1 );
        }

#else
gbazin's avatar
 
gbazin committed
578 579 580
        for( i = 0; i < i_nb; i++ )
        {
            int i_dummy = i_sector + i + 2 * CD_FRAMES;
gbazin's avatar
 
gbazin committed
581

gbazin's avatar
 
gbazin committed
582 583 584 585
#define p_msf ((struct cdrom_msf0 *)(p_block + i * VCD_SECTOR_SIZE))
            p_msf->minute =   i_dummy / (CD_FRAMES * CD_SECS);
            p_msf->second = ( i_dummy % (CD_FRAMES * CD_SECS) ) / CD_FRAMES;
            p_msf->frame =  ( i_dummy % (CD_FRAMES * CD_SECS) ) % CD_FRAMES;
gbazin's avatar
 
gbazin committed
586 587
#undef p_msf

gbazin's avatar
 
gbazin committed
588 589 590 591 592 593 594 595 596 597 598 599 600
            if( ioctl( p_vcddev->i_device_handle, CDROMREADRAW,
                       p_block + i * VCD_SECTOR_SIZE ) == -1 )
            {
                msg_Err( p_this, "could not read block %i from disc",
                         i_sector );

                if( i == 0 )
                {
                    if( i_type == VCD_TYPE ) free( p_block );
                    return( -1 );
                }
                else break;
            }
gbazin's avatar
 
gbazin committed
601 602
        }
#endif
gbazin's avatar
 
gbazin committed
603
    }
gbazin's avatar
 
gbazin committed
604

gbazin's avatar
 
gbazin committed
605 606 607 608 609 610 611 612 613 614 615
    /* For VCDs, we don't want to keep the header and footer of the
     * sectors read */
    if( i_type == VCD_TYPE )
    {
        for( i = 0; i < i_nb; i++ )
        {
            memcpy( p_buffer + i * VCD_DATA_SIZE,
                    p_block + i * VCD_SECTOR_SIZE + VCD_DATA_START,
                    VCD_DATA_SIZE );
        }
        free( p_block );
gbazin's avatar
 
gbazin committed
616
    }
gbazin's avatar
 
gbazin committed
617 618

    return( 0 );
gbazin's avatar
 
gbazin committed
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
}

/****************************************************************************
 * Private functions
 ****************************************************************************/

/****************************************************************************
 * OpenVCDImage: try to open a vcd image from a .cue file
 ****************************************************************************/
static int OpenVCDImage( vlc_object_t * p_this, const char *psz_dev,
                         vcddev_t *p_vcddev )
{
    int i_ret = -1;
    char *p_pos;
    char *psz_vcdfile = NULL;
    char *psz_cuefile = NULL;
635
    FILE *cuefile     = NULL;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
636
    int *p_sectors    = NULL;
gbazin's avatar
 
gbazin committed
637
    char line[1024];
638
    bool b_found      = false;
gbazin's avatar
 
gbazin committed
639 640 641 642

    /* Check if we are dealing with a .cue file */
    p_pos = strrchr( psz_dev, '.' );
    if( p_pos && !strcmp( p_pos, ".cue" ) )
643
    {
644 645
        /* psz_dev must be the cue file. Let's assume there's a .bin
         * file with the same filename */
646 647 648
        psz_vcdfile = malloc( p_pos - psz_dev + 5 /* ".bin" */ );
        strncpy( psz_vcdfile, psz_dev, p_pos - psz_dev );
        strcpy( psz_vcdfile + (p_pos - psz_dev), ".bin");
gbazin's avatar
 
gbazin committed
649 650 651 652 653 654 655 656 657 658 659 660 661 662
        psz_cuefile = strdup( psz_dev );
    }
    else
    {
        /* psz_dev must be the actual vcd file. Let's assume there's a .cue
         * file with the same filename */
        if( p_pos )
        {
            psz_cuefile = malloc( p_pos - psz_dev + 5 /* ".cue" */ );
            strncpy( psz_cuefile, psz_dev, p_pos - psz_dev );
            strcpy( psz_cuefile + (p_pos - psz_dev), ".cue");
        }
        else
        {
663 664
            if( asprintf( &psz_cuefile, "%s.cue", psz_dev ) == -1 )
                psz_cuefile = NULL;
gbazin's avatar
 
gbazin committed
665
        }
666 667
        /* If we need to look up the .cue file, then we don't have to look for the vcd */
        psz_vcdfile = strdup( psz_dev );
668 669
    }

gbazin's avatar
 
gbazin committed
670
    /* Open the cue file and try to parse it */
gbazin's avatar
 
gbazin committed
671
    msg_Dbg( p_this,"trying .cue file: %s", psz_cuefile );
672
    cuefile = utf8_fopen( psz_cuefile, "rt" );
673 674 675 676 677 678
    if( cuefile == NULL )
    {
        msg_Dbg( p_this, "could not find .cue file" );
        goto error;
    }

679
    msg_Dbg( p_this,"guessing vcd image file: %s", psz_vcdfile );
680
    p_vcddev->i_vcdimage_handle = utf8_open( psz_vcdfile,
681
                                    O_RDONLY | O_NONBLOCK | O_BINARY );
682
 
683
    while( fgets( line, 1024, cuefile ) && !b_found )
684
    {
685
        /* We have a cue file, but no valid vcd file yet */
686 687 688 689 690
        char filename[1024];
        char type[16];
        int i_temp = sscanf( line, "FILE \"%1023[^\"]\" %15s", filename, type );
        *p_pos = 0;
        switch( i_temp )
gbazin's avatar
 
gbazin committed
691
        {
692 693 694 695 696 697 698 699 700 701 702 703 704
            case 2:
                msg_Dbg( p_this, "the cue file says the data file is %s", type );
                if( strcasecmp( type, "BINARY" ) )
                    goto error; /* Error if not binary, otherwise treat as case 1 */
            case 1:
                if( p_vcddev->i_vcdimage_handle == -1 )
                {
                    msg_Dbg( p_this, "we could not find the data file, but we found a new path" );
                    free( psz_vcdfile);
                    if( *filename != '/' && ((p_pos = strrchr( psz_cuefile, '/' ))
                        || (p_pos = strrchr( psz_cuefile, '\\' ) )) )
                    {
                        psz_vcdfile = malloc( strlen(filename) +
gbazin's avatar
 
gbazin committed
705
                                      (p_pos - psz_cuefile + 1) + 1 );
706 707 708 709 710
                        strncpy( psz_vcdfile, psz_cuefile, (p_pos - psz_cuefile + 1) );
                        strcpy( psz_vcdfile + (p_pos - psz_cuefile + 1), filename );
                    } else psz_vcdfile = strdup( filename );
                    msg_Dbg( p_this,"using vcd image file: %s", psz_vcdfile );
                    p_vcddev->i_vcdimage_handle = utf8_open( psz_vcdfile,
711
                                        O_RDONLY | O_NONBLOCK | O_BINARY );
712 713 714 715 716
                }
                b_found = true;
            default:
                break;
        }
717 718
    }

719 720
    if( p_vcddev->i_vcdimage_handle == -1)
        goto error;
721

gbazin's avatar
 
gbazin committed
722 723
    /* Try to parse the i_tracks and p_sectors info so we can just forget
     * about the cuefile */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
724 725
    size_t i_tracks = 0;

726
    while( fgets( line, 1024, cuefile ) && i_tracks < INT_MAX-1 )
gbazin's avatar
 
gbazin committed
727
    {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
728 729 730 731
        /* look for a TRACK line */
        char psz_dummy[9];
        if( !sscanf( line, "%9s", psz_dummy ) || strcmp(psz_dummy, "TRACK") )
            continue;
732

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
733
        /* look for an INDEX line */
gbazin's avatar
 
gbazin committed
734 735
        while( fgets( line, 1024, cuefile ) )
        {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
736
            int i_num, i_min, i_sec, i_frame;
gbazin's avatar
 
gbazin committed
737

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
738 739 740
            if( (sscanf( line, "%*9s %2u %2u:%2u:%2u", &i_num,
                         &i_min, &i_sec, &i_frame ) != 4) || (i_num != 1) )
                continue;
gbazin's avatar
 
gbazin committed
741

742
            int *buf = realloc (p_sectors, (i_tracks + 1) * sizeof (*buf));
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
743 744 745 746 747
            if (buf == NULL)
                goto error;
            p_sectors = buf;
            p_sectors[i_tracks] = MSF_TO_LBA(i_min, i_sec, i_frame);
            msg_Dbg( p_this, "vcd track %i begins at sector:%i",
Laurent Aimar's avatar
Laurent Aimar committed
748
                     (int)i_tracks, (int)p_sectors[i_tracks] );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
749 750
            i_tracks++;
            break;
gbazin's avatar
 
gbazin committed
751
        }
752 753
    }

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
754
    /* fill in the last entry */
755
    int *buf = realloc (p_sectors, (i_tracks + 1) * sizeof (*buf));
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
756 757 758 759 760 761
    if (buf == NULL)
        goto error;
    p_sectors = buf;
    p_sectors[i_tracks] = lseek(p_vcddev->i_vcdimage_handle, 0, SEEK_END)
                                 / VCD_SECTOR_SIZE;
    msg_Dbg( p_this, "vcd track %i, begins at sector:%i",
Laurent Aimar's avatar
Laurent Aimar committed
762
             (int)i_tracks, (int)p_sectors[i_tracks] );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
763 764 765 766
    p_vcddev->i_tracks = ++i_tracks;
    p_vcddev->p_sectors = p_sectors;
    i_ret = 0;

767
error:
gbazin's avatar
 
gbazin committed
768
    if( cuefile ) fclose( cuefile );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
769
    free( p_sectors );
770 771
    free( psz_cuefile );
    free( psz_vcdfile );
772

gbazin's avatar
 
gbazin committed
773 774 775 776 777 778 779 780
    return i_ret;
}

/****************************************************************************
 * CloseVCDImage: closes a vcd image opened by OpenVCDImage
 ****************************************************************************/
static void CloseVCDImage( vlc_object_t * p_this, vcddev_t *p_vcddev )
{
Laurent Aimar's avatar
Laurent Aimar committed
781
    VLC_UNUSED( p_this );
gbazin's avatar
 
gbazin committed
782 783 784 785 786
    if( p_vcddev->i_vcdimage_handle != -1 )
        close( p_vcddev->i_vcdimage_handle );
    else
        return;

787
    free( p_vcddev->p_sectors );
788 789
}

790
#if defined( __APPLE__ )
791
/****************************************************************************
gbazin's avatar
 
gbazin committed
792
 * darwin_getTOC: get the TOC
793
 ****************************************************************************/
794
static CDTOC *darwin_getTOC( vlc_object_t * p_this, const vcddev_t *p_vcddev )
795 796 797 798 799 800 801
{
    mach_port_t port;
    char *psz_devname;
    kern_return_t ret;
    CDTOC *pTOC = NULL;
    io_iterator_t iterator;
    io_registry_entry_t service;
802
    CFMutableDictionaryRef properties;
803 804 805
    CFDataRef data;

    /* get the device name */
gbazin's avatar
 
gbazin committed
806
    if( ( psz_devname = strrchr( p_vcddev->psz_dev, '/') ) != NULL )
807 808
        ++psz_devname;
    else
gbazin's avatar
 
gbazin committed
809
        psz_devname = p_vcddev->psz_dev;
810 811 812 813 814 815 816 817

    /* unraw the device name */
    if( *psz_devname == 'r' )
        ++psz_devname;

    /* get port for IOKit communication */
    if( ( ret = IOMasterPort( MACH_PORT_NULL, &port ) ) != KERN_SUCCESS )
    {
818
        msg_Err( p_this, "IOMasterPort: 0x%08x", ret );
819 820 821 822
        return( NULL );
    }

    /* get service iterator for the device */
823
    if( ( ret = IOServiceGetMatchingServices(
824 825 826
                    port, IOBSDNameMatching( port, 0, psz_devname ),
                    &iterator ) ) != KERN_SUCCESS )
    {
827
        msg_Err( p_this, "IOServiceGetMatchingServices: 0x%08x", ret );
828 829 830 831 832 833 834
        return( NULL );
    }

    /* first service */
    service = IOIteratorNext( iterator );
    IOObjectRelease( iterator );

835
    /* search for kIOCDMediaClass */
836 837
    while( service && !IOObjectConformsTo( service, kIOCDMediaClass ) )
    {
838
        if( ( ret = IORegistryEntryGetParentIterator( service,
839 840
                        kIOServicePlane, &iterator ) ) != KERN_SUCCESS )
        {
841
            msg_Err( p_this, "IORegistryEntryGetParentIterator: 0x%08x", ret );
842 843 844 845 846 847 848 849 850
            IOObjectRelease( service );
            return( NULL );
        }

        IOObjectRelease( service );
        service = IOIteratorNext( iterator );
        IOObjectRelease( iterator );
    }

851
    if( !service )
852
    {
853
        msg_Err( p_this, "search for kIOCDMediaClass came up empty" );
854 855 856 857 858 859 860
        return( NULL );
    }

    /* create a CF dictionary containing the TOC */
    if( ( ret = IORegistryEntryCreateCFProperties( service, &properties,
                    kCFAllocatorDefault, kNilOptions ) ) != KERN_SUCCESS )
    {
861
        msg_Err( p_this, "IORegistryEntryCreateCFProperties: 0x%08x", ret );
862 863 864 865 866 867 868 869 870 871 872 873 874 875
        IOObjectRelease( service );
        return( NULL );
    }

    /* get the TOC from the dictionary */
    if( ( data = (CFDataRef) CFDictionaryGetValue( properties,
                                    CFSTR(kIOCDMediaTOCKey) ) ) != NULL )
    {
        CFRange range;
        CFIndex buf_len;

        buf_len = CFDataGetLength( data ) + 1;
        range = CFRangeMake( 0, buf_len );

Laurent Aimar's avatar
Laurent Aimar committed
876
        if( ( pTOC = malloc( buf_len ) ) != NULL )
877 878 879 880 881 882
        {
            CFDataGetBytes( data, range, (u_char *)pTOC );
        }
    }
    else
    {
883
        msg_Err( p_this, "CFDictionaryGetValue failed" );
884 885 886
    }

    CFRelease( properties );
887
    IOObjectRelease( service );
888

889
    return( pTOC );
890 891 892
}

/****************************************************************************
893
 * darwin_getNumberOfTracks: get number of tracks in TOC
894
 ****************************************************************************/
gbazin's avatar
 
gbazin committed
895
static int darwin_getNumberOfTracks( CDTOC *pTOC, int i_descriptors )
896 897
{
    u_char track;
898
    int i, i_tracks = 0;
899
    CDTOCDescriptor *pTrackDescriptors = NULL;
900

901
    pTrackDescriptors = (CDTOCDescriptor *)pTOC->descriptors;
902

903
    for( i = i_descriptors; i > 0; i-- )
904 905 906 907 908 909
    {
        track = pTrackDescriptors[i].point;

        if( track > CD_MAX_TRACK_NO || track < CD_MIN_TRACK_NO )
            continue;

910
        i_tracks++;
Sam Hocevar's avatar