]> git.sesse.net Git - vlc/blob - modules/access/vcd/cdrom.c
cdrom.c: code cosmetic and includes removal
[vlc] / modules / access / vcd / cdrom.c
1 /****************************************************************************
2  * cdrom.c: cdrom tools
3  *****************************************************************************
4  * Copyright (C) 1998-2001 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Johan Bilien <jobi@via.ecp.fr>
8  *          Gildas Bazin <gbazin@netcourrier.com>
9  *          Rémi Denis-Courmont
10  *          Laurent Aimar
11  *          Rémi Duraffort
12  *          Derk-Jan Hartman
13  *          Samuel Hocevar
14  *          Rafaël Carré
15  *          Christophe Massiot
16  *          Jean-Baptiste Kempf
17  *
18  *
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.
23  *
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.
28  *
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  *****************************************************************************/
33
34 /*****************************************************************************
35  * Preamble
36  *****************************************************************************/
37 #ifdef HAVE_CONFIG_H
38 # include "config.h"
39 #endif
40
41 #ifdef __OS2__
42 #   define INCL_DOSDEVIOCTL
43 #endif
44
45 #include <sys/types.h>
46 #include <unistd.h>
47 #include <sys/stat.h>
48 #include <fcntl.h>
49 #include <limits.h>
50
51 #include <vlc_common.h>
52 #include <vlc_access.h>
53 #include <vlc_charset.h>
54 #include <vlc_fs.h>
55 #include <vlc_meta.h>
56
57 #if defined( SYS_BSDI )
58 #   include <dvd.h>
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 )
73 #   include <windows.h>
74 #   include <winioctl.h>
75 #elif defined (__linux__)
76 #   include <sys/ioctl.h>
77 #   include <linux/cdrom.h>
78 #elif defined( __OS2__ )
79 #   include <os2safe.h>
80 #   include <os2.h>
81
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  *****************************************************************************/
88
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 )
93 {
94     PVOID pParamsLow = NULL;
95     PVOID pDataLow = NULL;
96     ULONG cbParamLenLow;
97     ULONG cbDataLenLow;
98
99     APIRET rc;
100
101     rc = DosAllocMem( &pParamsLow, cbParamLenMax, fALLOC );
102     if( rc )
103         goto exit_free;
104
105     rc = DosAllocMem( &pDataLow, cbDataLenMax, fALLOC );
106     if( rc )
107         goto exit_free;
108
109     memcpy( pParamsLow, pParams, cbParamLenMax );
110     memcpy( pDataLow, pData, cbDataLenMax );
111
112     cbParamLenLow = *pcbParamLen;
113     cbDataLenLow  = *pcbDataLen;
114
115     rc = DosDevIOCtl( hdevice, category, function, pParamsLow,
116                       cbParamLenMax, &cbParamLenLow, pDataLow, cbDataLenMax,
117                       &cbDataLenLow );
118
119     if( !rc )
120     {
121         memcpy( pParams, pParamsLow, cbParamLenMax );
122         memcpy( pData, pDataLow, cbDataLenMax );
123
124         *pcbParamLen = cbParamLenLow;
125         *pcbDataLen  = cbDataLenLow;
126     }
127
128 exit_free:
129     DosFreeMem( pParamsLow);
130     DosFreeMem( pDataLow);
131
132     return rc;
133 }
134
135 #   define DosDevIOCtl vlc_DosDevIOCtl
136 #else
137 #   error FIXME
138 #endif
139
140 #include "cdrom_internals.h"
141 #include "cdrom.h"
142
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 )
147 {
148     int i_ret;
149     int b_is_file;
150     vcddev_t *p_vcddev;
151 #if !defined( _WIN32 ) && !defined( __OS2__ )
152     struct stat fileinfo;
153 #endif
154
155     if( !psz_dev ) return NULL;
156
157     /*
158      *  Initialize structure with default values
159      */
160     p_vcddev = malloc( sizeof(*p_vcddev) );
161     if( p_vcddev == NULL )
162         return NULL;
163     p_vcddev->i_vcdimage_handle = -1;
164     p_vcddev->psz_dev = NULL;
165     b_is_file = 1;
166
167     /*
168      *  Check if we are dealing with a device or a file (vcd image)
169      */
170 #if defined( _WIN32 ) || defined( __OS2__ )
171     if( (strlen( psz_dev ) == 2 && psz_dev[1] == ':') )
172     {
173         b_is_file = 0;
174     }
175
176 #else
177     if( vlc_stat( psz_dev, &fileinfo ) < 0 )
178     {
179         free( p_vcddev );
180         return NULL;
181     }
182
183     /* Check if this is a block/char device */
184     if( S_ISBLK( fileinfo.st_mode ) || S_ISCHR( fileinfo.st_mode ) )
185         b_is_file = 0;
186 #endif
187
188     if( b_is_file )
189     {
190         i_ret = OpenVCDImage( p_this, psz_dev, p_vcddev );
191     }
192     else
193     {
194         /*
195          *  open the vcd device
196          */
197
198 #ifdef _WIN32
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 );
202 #else
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;
206 #endif
207     }
208
209     if( i_ret == 0 )
210     {
211         p_vcddev->psz_dev = (char *)strdup( psz_dev );
212     }
213     else
214     {
215         free( p_vcddev );
216         p_vcddev = NULL;
217     }
218
219     return p_vcddev;
220 }
221
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 )
226 {
227     free( p_vcddev->psz_dev );
228
229     if( p_vcddev->i_vcdimage_handle != -1 )
230     {
231         /*
232          *  vcd image mode
233          */
234
235         CloseVCDImage( p_this, p_vcddev );
236         return;
237     }
238
239     /*
240      *  vcd device mode
241      */
242
243 #ifdef _WIN32
244     if( p_vcddev->h_device_handle )
245         CloseHandle( p_vcddev->h_device_handle );
246 #elif defined( __OS2__ )
247     if( p_vcddev->hcd )
248         DosClose( p_vcddev->hcd );
249 #else
250     if( p_vcddev->i_device_handle != -1 )
251         close( p_vcddev->i_device_handle );
252 #endif
253     free( p_vcddev );
254 }
255
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
259  *                     tracks available.
260  *****************************************************************************/
261 int ioctl_GetTracksMap( vlc_object_t *p_this, const vcddev_t *p_vcddev,
262                         int **pp_sectors )
263 {
264     int i_tracks = 0;
265
266     if( p_vcddev->i_vcdimage_handle != -1 )
267     {
268         /*
269          *  vcd image mode
270          */
271
272         i_tracks = p_vcddev->i_tracks;
273
274         if( pp_sectors )
275         {
276             *pp_sectors = calloc( i_tracks + 1, sizeof(**pp_sectors) );
277             if( *pp_sectors == NULL )
278                 return 0;
279             memcpy( *pp_sectors, p_vcddev->p_sectors,
280                     (i_tracks + 1) * sizeof(**pp_sectors) );
281         }
282
283         return i_tracks;
284     }
285     else
286     {
287
288         /*
289          *  vcd device mode
290          */
291
292 #if defined( __APPLE__ )
293
294         CDTOC *pTOC;
295         int i_descriptors;
296
297         if( ( pTOC = darwin_getTOC( p_this, p_vcddev ) ) == NULL )
298         {
299             msg_Err( p_this, "failed to get the TOC" );
300             return 0;
301         }
302
303         i_descriptors = CDTOCGetDescriptorCount( pTOC );
304         i_tracks = darwin_getNumberOfTracks( pTOC, i_descriptors );
305
306         if( pp_sectors )
307         {
308             int i, i_leadout = -1;
309             CDTOCDescriptor *pTrackDescriptors;
310             u_char track;
311
312             *pp_sectors = calloc( i_tracks + 1, sizeof(**pp_sectors) );
313             if( *pp_sectors == NULL )
314             {
315                 darwin_freeTOC( pTOC );
316                 return 0;
317             }
318
319             pTrackDescriptors = pTOC->descriptors;
320
321             for( i_tracks = 0, i = 0; i < i_descriptors; i++ )
322             {
323                 track = pTrackDescriptors[i].point;
324
325                 if( track == 0xA2 )
326                     i_leadout = i;
327
328                 if( track > CD_MAX_TRACK_NO || track < CD_MIN_TRACK_NO )
329                     continue;
330
331                 (*pp_sectors)[i_tracks++] =
332                     CDConvertMSFToLBA( pTrackDescriptors[i].p );
333             }
334
335             if( i_leadout == -1 )
336             {
337                 msg_Err( p_this, "leadout not found" );
338                 free( *pp_sectors );
339                 darwin_freeTOC( pTOC );
340                 return 0;
341             }
342
343             /* set leadout sector */
344             (*pp_sectors)[i_tracks] =
345                 CDConvertMSFToLBA( pTrackDescriptors[i_leadout].p );
346         }
347
348         darwin_freeTOC( pTOC );
349
350 #elif defined( _WIN32 )
351         DWORD dwBytesReturned;
352         CDROM_TOC cdrom_toc;
353
354         if( DeviceIoControl( p_vcddev->h_device_handle, IOCTL_CDROM_READ_TOC,
355                              NULL, 0, &cdrom_toc, sizeof(CDROM_TOC),
356                              &dwBytesReturned, NULL ) == 0 )
357         {
358             msg_Err( p_this, "could not read TOCHDR" );
359             return 0;
360         }
361
362         i_tracks = cdrom_toc.LastTrack - cdrom_toc.FirstTrack + 1;
363
364         if( pp_sectors )
365         {
366             *pp_sectors = calloc( i_tracks + 1, sizeof(**pp_sectors) );
367             if( *pp_sectors == NULL )
368                 return 0;
369
370             for( int i = 0 ; i <= i_tracks ; i++ )
371             {
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]);
377              }
378         }
379
380 #elif defined( __OS2__ )
381         cdrom_get_tochdr_t get_tochdr = {{'C', 'D', '0', '1'}};
382         cdrom_tochdr_t     tochdr;
383
384         ULONG param_len;
385         ULONG data_len;
386         ULONG rc;
387
388         rc = DosDevIOCtl( p_vcddev->hcd, IOCTL_CDROMAUDIO,
389                           CDROMAUDIO_GETAUDIODISK,
390                           &get_tochdr, sizeof( get_tochdr ), &param_len,
391                           &tochdr, sizeof( tochdr ), &data_len );
392         if( rc )
393         {
394             msg_Err( p_this, "could not read TOCHDR" );
395             return 0;
396         }
397
398         i_tracks = tochdr.last_track - tochdr.first_track + 1;
399
400         if( pp_sectors )
401         {
402             cdrom_get_track_t get_track = {{'C', 'D', '0', '1'}, };
403             cdrom_track_t track;
404             int i;
405
406             *pp_sectors = calloc( i_tracks + 1, sizeof(**pp_sectors) );
407             if( *pp_sectors == NULL )
408                 return 0;
409
410             for( i = 0 ; i < i_tracks ; i++ )
411             {
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), &param_len,
416                                   &track, sizeof(track), &data_len );
417                 if (rc)
418                 {
419                     msg_Err( p_this, "could not read %d track",
420                              get_track.track );
421                     return 0;
422                 }
423
424                 (*pp_sectors)[ i ] = MSF_TO_LBA2(
425                                        track.start.minute,
426                                        track.start.second,
427                                        track.start.frame );
428                 msg_Dbg( p_this, "p_sectors: %i, %i", i, (*pp_sectors)[i]);
429             }
430
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]);
437         }
438
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;
443
444         if( ioctl( p_vcddev->i_device_handle, CDIOREADTOCHEADER, &tochdr )
445             == -1 )
446         {
447             msg_Err( p_this, "could not read TOCHDR" );
448             return 0;
449         }
450
451         i_tracks = tochdr.ending_track - tochdr.starting_track + 1;
452
453         if( pp_sectors )
454         {
455              int i;
456
457              *pp_sectors = calloc( i_tracks + 1, sizeof(**pp_sectors) );
458              if( *pp_sectors == NULL )
459                  return 0;
460
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 )
468              {
469                  free( *pp_sectors );
470                  return 0;
471              }
472
473              /* Read the TOC */
474              if( ioctl( p_vcddev->i_device_handle, CDIOREADTOCENTRYS,
475                         &toc_entries ) == -1 )
476              {
477                  msg_Err( p_this, "could not read the TOC" );
478                  free( *pp_sectors );
479                  free( toc_entries.data );
480                  return 0;
481              }
482
483              /* Fill the p_sectors structure with the track/sector matches */
484              for( i = 0 ; i <= i_tracks ; i++ )
485              {
486 #if defined( HAVE_SCSIREQ_IN_SYS_SCSIIO_H )
487                  /* FIXME: is this ok? */
488                  (*pp_sectors)[ i ] = toc_entries.data[i].addr.lba;
489 #else
490                  (*pp_sectors)[ i ] = ntohl( toc_entries.data[i].addr.lba );
491 #endif
492              }
493         }
494 #else
495         struct cdrom_tochdr   tochdr;
496         struct cdrom_tocentry tocent;
497
498         /* First we read the TOC header */
499         if( ioctl( p_vcddev->i_device_handle, CDROMREADTOCHDR, &tochdr )
500             == -1 )
501         {
502             msg_Err( p_this, "could not read TOCHDR" );
503             return 0;
504         }
505
506         i_tracks = tochdr.cdth_trk1 - tochdr.cdth_trk0 + 1;
507
508         if( pp_sectors )
509         {
510             int i;
511
512             *pp_sectors = calloc( i_tracks + 1, sizeof(**pp_sectors) );
513             if( *pp_sectors == NULL )
514                 return 0;
515
516             /* Fill the p_sectors structure with the track/sector matches */
517             for( i = 0 ; i <= i_tracks ; i++ )
518             {
519                 tocent.cdte_format = CDROM_LBA;
520                 tocent.cdte_track =
521                     ( i == i_tracks ) ? CDROM_LEADOUT : tochdr.cdth_trk0 + i;
522
523                 if( ioctl( p_vcddev->i_device_handle, CDROMREADTOCENTRY,
524                            &tocent ) == -1 )
525                 {
526                     msg_Err( p_this, "could not read TOCENTRY" );
527                     free( *pp_sectors );
528                     return 0;
529                 }
530
531                 (*pp_sectors)[ i ] = tocent.cdte_addr.lba;
532             }
533         }
534 #endif
535
536         return i_tracks;
537     }
538 }
539
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 )
545 {
546     uint8_t *p_block;
547     int i;
548
549     if( i_type == VCD_TYPE )
550         p_block = malloc( VCD_SECTOR_SIZE * i_nb );
551     else
552         p_block = p_buffer;
553
554     if( p_vcddev->i_vcdimage_handle != -1 )
555     {
556         /*
557          *  vcd image mode
558          */
559         if( lseek( p_vcddev->i_vcdimage_handle, i_sector * VCD_SECTOR_SIZE,
560                    SEEK_SET ) == -1 )
561         {
562             msg_Err( p_this, "Could not lseek to sector %d", i_sector );
563             goto error;
564         }
565
566         if( read( p_vcddev->i_vcdimage_handle, p_block, VCD_SECTOR_SIZE * i_nb)
567             == -1 )
568         {
569             msg_Err( p_this, "Could not read sector %d", i_sector );
570             goto error;
571         }
572
573     }
574     else
575     {
576
577         /*
578          *  vcd device mode
579          */
580
581 #if defined( __APPLE__ )
582         dk_cd_read_t cd_read;
583
584         memset( &cd_read, 0, sizeof(cd_read) );
585
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;
591
592         cd_read.buffer = p_block;
593         cd_read.bufferLength = VCD_SECTOR_SIZE * i_nb;
594
595         if( ioctl( p_vcddev->i_device_handle, DKIOCCDREAD, &cd_read ) == -1 )
596         {
597             msg_Err( p_this, "could not read block %d", i_sector );
598             goto error;
599         }
600
601 #elif defined( _WIN32 )
602         DWORD dwBytesReturned;
603         RAW_READ_INFO cdrom_raw;
604
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;
609
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,
613                              NULL ) == 0 )
614         {
615             if( i_type == VCD_TYPE )
616             {
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,
623                                      NULL ) == 0 )
624                     goto error;
625             }
626             else return -1;
627         }
628
629 #elif defined( __OS2__ )
630         cdrom_readlong_t readlong = {{'C', 'D', '0', '1'}, };
631
632         ULONG param_len;
633         ULONG data_len;
634         ULONG rc;
635
636         readlong.addr_mode = 0;         /* LBA mode */
637         readlong.sectors   = i_nb;
638         readlong.start     = i_sector;
639
640         rc = DosDevIOCtl( p_vcddev->hcd, IOCTL_CDROMDISK, CDROMDISK_READLONG,
641                           &readlong, sizeof( readlong ), &param_len,
642                           p_block, VCD_SECTOR_SIZE * i_nb, &data_len );
643         if( rc )
644         {
645             msg_Err( p_this, "could not read block %d", i_sector );
646             goto error;
647         }
648
649 #elif defined( HAVE_SCSIREQ_IN_SYS_SCSIIO_H )
650         struct scsireq  sc;
651         int i_ret;
652
653         memset( &sc, 0, sizeof(sc) );
654         sc.cmd[0] = 0xBE;
655         sc.cmd[1] = i_type == VCD_TYPE ? SECTOR_TYPE_MODE2_FORM2:
656                                          SECTOR_TYPE_CDDA;
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 */
666         sc.cmdlen = 12;
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;
671         sc.timeout = 10000;
672
673         i_ret = ioctl( p_vcddev->i_device_handle, SCIOCCOMMAND, &sc );
674         if( i_ret == -1 )
675         {
676             msg_Err( p_this, "SCIOCCOMMAND failed" );
677             goto error;
678         }
679         if( sc.retsts || sc.error )
680         {
681             msg_Err( p_this, "SCSI command failed: status %d error %d",
682                              sc.retsts, sc.error );
683             goto error;
684         }
685
686 #elif defined( HAVE_IOC_TOC_HEADER_IN_SYS_CDIO_H )
687         int i_size = VCD_SECTOR_SIZE;
688
689         if( ioctl( p_vcddev->i_device_handle, CDRIOCSETBLOCKSIZE, &i_size )
690             == -1 )
691         {
692             msg_Err( p_this, "Could not set block size" );
693             goto error;
694         }
695
696         if( lseek( p_vcddev->i_device_handle,
697                    i_sector * VCD_SECTOR_SIZE, SEEK_SET ) == -1 )
698         {
699             msg_Err( p_this, "Could not lseek to sector %d", i_sector );
700             goto error;
701         }
702
703         if( read( p_vcddev->i_device_handle,
704                   p_block, VCD_SECTOR_SIZE * i_nb ) == -1 )
705         {
706             msg_Err( p_this, "Could not read sector %d", i_sector );
707             goto error;
708         }
709
710 #else
711         for( i = 0; i < i_nb; i++ )
712         {
713             int i_dummy = i_sector + i + 2 * CD_FRAMES;
714
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;
719 #undef p_msf
720
721             if( ioctl( p_vcddev->i_device_handle, CDROMREADRAW,
722                        p_block + i * VCD_SECTOR_SIZE ) == -1 )
723             {
724                 msg_Err( p_this, "could not read block %i from disc",
725                          i_sector );
726
727                 if( i == 0 )
728                     goto error;
729                 else
730                     break;
731             }
732         }
733 #endif
734     }
735
736     /* For VCDs, we don't want to keep the header and footer of the
737      * sectors read */
738     if( i_type == VCD_TYPE )
739     {
740         for( i = 0; i < i_nb; i++ )
741         {
742             memcpy( p_buffer + i * VCD_DATA_SIZE,
743                     p_block + i * VCD_SECTOR_SIZE + VCD_DATA_START,
744                     VCD_DATA_SIZE );
745         }
746         free( p_block );
747     }
748
749     return( 0 );
750
751 error:
752     if( i_type == VCD_TYPE )
753         free( p_block );
754     return( -1 );
755 }
756
757 /****************************************************************************
758  * Private functions
759  ****************************************************************************/
760
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,
765                          vcddev_t *p_vcddev )
766 {
767     int i_ret = -1;
768     char *p_pos;
769     char *psz_vcdfile = NULL;
770     char *psz_cuefile = NULL;
771     FILE *cuefile     = NULL;
772     int *p_sectors    = NULL;
773     char line[1024];
774     bool b_found      = false;
775
776     /* Check if we are dealing with a .cue file */
777     p_pos = strrchr( psz_dev, '.' );
778     if( p_pos && !strcmp( p_pos, ".cue" ) )
779     {
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),
783                       psz_dev ) < 0 )
784             psz_vcdfile = NULL;
785         psz_cuefile = strdup( psz_dev );
786     }
787     else
788     if( p_pos )
789     {
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),
793                       psz_dev ) < 0 )
794             psz_cuefile = NULL;
795         psz_vcdfile = strdup( psz_dev );
796     }
797     else
798     {
799         if( asprintf( &psz_cuefile, "%s.cue", psz_dev ) == -1 )
800             psz_cuefile = NULL;
801          /* If we need to look up the .cue file, then we don't have to look
802           * for the vcd */
803         psz_vcdfile = strdup( psz_dev );
804     }
805
806     if( psz_cuefile == NULL || psz_vcdfile == NULL )
807         goto error;
808
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 )
813     {
814         msg_Dbg( p_this, "could not find .cue file" );
815         goto error;
816     }
817
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 );
821
822     while( fgets( line, 1024, cuefile ) && !b_found )
823     {
824         /* We have a cue file, but no valid vcd file yet */
825         char filename[1024];
826         char type[16];
827         int i_temp = sscanf( line, "FILE \"%1023[^\"]\" %15s", filename, type );
828         switch( i_temp )
829         {
830             case 2:
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 */
834             case 1:
835                 if( p_vcddev->i_vcdimage_handle == -1 )
836                 {
837                     msg_Dbg( p_this, "we could not find the data file, but we found a new path" );
838                     free( psz_vcdfile);
839                     if( *filename != '/' && ((p_pos = strrchr( psz_cuefile, '/' ))
840                         || (p_pos = strrchr( psz_cuefile, '\\' ) )) )
841                     {
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 );
850                 }
851                 b_found = true;
852             default:
853                 break;
854         }
855     }
856
857     if( p_vcddev->i_vcdimage_handle == -1)
858         goto error;
859
860     /* Try to parse the i_tracks and p_sectors info so we can just forget
861      * about the cuefile */
862     size_t i_tracks = 0;
863
864     while( fgets( line, 1024, cuefile ) && i_tracks < INT_MAX-1 )
865     {
866         /* look for a TRACK line */
867         char psz_dummy[10];
868         if( !sscanf( line, "%9s", psz_dummy ) || strcmp(psz_dummy, "TRACK") )
869             continue;
870
871         /* look for an INDEX line */
872         while( fgets( line, 1024, cuefile ) )
873         {
874             int i_num, i_min, i_sec, i_frame;
875
876             if( (sscanf( line, "%*9s %2u %2u:%2u:%2u", &i_num,
877                          &i_min, &i_sec, &i_frame ) != 4) || (i_num != 1) )
878                 continue;
879
880             int *buf = realloc (p_sectors, (i_tracks + 1) * sizeof (*buf));
881             if (buf == NULL)
882                 goto error;
883             p_sectors = 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] );
887             i_tracks++;
888             break;
889         }
890     }
891
892     /* fill in the last entry */
893     int *buf = realloc (p_sectors, (i_tracks + 1) * sizeof (*buf));
894     if (buf == NULL)
895         goto error;
896     p_sectors = buf;
897     p_sectors[i_tracks] = lseek(p_vcddev->i_vcdimage_handle, 0, SEEK_END)
898                                  / VCD_SECTOR_SIZE;
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;
903     i_ret = 0;
904
905 error:
906     if( cuefile ) fclose( cuefile );
907     free( p_sectors );
908     free( psz_cuefile );
909     free( psz_vcdfile );
910
911     return i_ret;
912 }
913
914 /****************************************************************************
915  * CloseVCDImage: closes a vcd image opened by OpenVCDImage
916  ****************************************************************************/
917 static void CloseVCDImage( vlc_object_t * p_this, vcddev_t *p_vcddev )
918 {
919     VLC_UNUSED( p_this );
920     if( p_vcddev->i_vcdimage_handle != -1 )
921         close( p_vcddev->i_vcdimage_handle );
922     else
923         return;
924
925     free( p_vcddev->p_sectors );
926 }
927
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 )
933 {
934     mach_port_t port;
935     char *psz_devname;
936     kern_return_t ret;
937     CDTOC *pTOC = NULL;
938     io_iterator_t iterator;
939     io_registry_entry_t service;
940     CFMutableDictionaryRef properties;
941     CFDataRef data;
942
943     /* get the device name */
944     if( ( psz_devname = strrchr( p_vcddev->psz_dev, '/') ) != NULL )
945         ++psz_devname;
946     else
947         psz_devname = p_vcddev->psz_dev;
948
949     /* unraw the device name */
950     if( *psz_devname == 'r' )
951         ++psz_devname;
952
953     /* get port for IOKit communication */
954     if( ( ret = IOMasterPort( MACH_PORT_NULL, &port ) ) != KERN_SUCCESS )
955     {
956         msg_Err( p_this, "IOMasterPort: 0x%08x", ret );
957         return( NULL );
958     }
959
960     /* get service iterator for the device */
961     if( ( ret = IOServiceGetMatchingServices(
962                     port, IOBSDNameMatching( port, 0, psz_devname ),
963                     &iterator ) ) != KERN_SUCCESS )
964     {
965         msg_Err( p_this, "IOServiceGetMatchingServices: 0x%08x", ret );
966         return( NULL );
967     }
968
969     /* first service */
970     service = IOIteratorNext( iterator );
971     IOObjectRelease( iterator );
972
973     /* search for kIOCDMediaClass */
974     while( service && !IOObjectConformsTo( service, kIOCDMediaClass ) )
975     {
976         if( ( ret = IORegistryEntryGetParentIterator( service,
977                         kIOServicePlane, &iterator ) ) != KERN_SUCCESS )
978         {
979             msg_Err( p_this, "IORegistryEntryGetParentIterator: 0x%08x", ret );
980             IOObjectRelease( service );
981             return( NULL );
982         }
983
984         IOObjectRelease( service );
985         service = IOIteratorNext( iterator );
986         IOObjectRelease( iterator );
987     }
988
989     if( !service )
990     {
991         msg_Err( p_this, "search for kIOCDMediaClass came up empty" );
992         return( NULL );
993     }
994
995     /* create a CF dictionary containing the TOC */
996     if( ( ret = IORegistryEntryCreateCFProperties( service, &properties,
997                     kCFAllocatorDefault, kNilOptions ) ) != KERN_SUCCESS )
998     {
999         msg_Err( p_this, "IORegistryEntryCreateCFProperties: 0x%08x", ret );
1000         IOObjectRelease( service );
1001         return( NULL );
1002     }
1003
1004     /* get the TOC from the dictionary */
1005     if( ( data = (CFDataRef) CFDictionaryGetValue( properties,
1006                                     CFSTR(kIOCDMediaTOCKey) ) ) != NULL )
1007     {
1008         CFRange range;
1009         CFIndex buf_len;
1010
1011         buf_len = CFDataGetLength( data ) + 1;
1012         range = CFRangeMake( 0, buf_len );
1013
1014         if( ( pTOC = malloc( buf_len ) ) != NULL )
1015         {
1016             CFDataGetBytes( data, range, (u_char *)pTOC );
1017         }
1018     }
1019     else
1020     {
1021         msg_Err( p_this, "CFDictionaryGetValue failed" );
1022     }
1023
1024     CFRelease( properties );
1025     IOObjectRelease( service );
1026
1027     return( pTOC );
1028 }
1029
1030 /****************************************************************************
1031  * darwin_getNumberOfTracks: get number of tracks in TOC
1032  ****************************************************************************/
1033 static int darwin_getNumberOfTracks( CDTOC *pTOC, int i_descriptors )
1034 {
1035     u_char track;
1036     int i, i_tracks = 0;
1037     CDTOCDescriptor *pTrackDescriptors = NULL;
1038
1039     pTrackDescriptors = (CDTOCDescriptor *)pTOC->descriptors;
1040
1041     for( i = i_descriptors; i > 0; i-- )
1042     {
1043         track = pTrackDescriptors[i].point;
1044
1045         if( track > CD_MAX_TRACK_NO || track < CD_MIN_TRACK_NO )
1046             continue;
1047
1048         i_tracks++;
1049     }
1050
1051     return( i_tracks );
1052 }
1053 #endif /* __APPLE__ */
1054
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 )
1063 {
1064     /* Initializations */
1065     p_vcddev->h_device_handle = NULL;
1066
1067     char psz_win32_drive[7];
1068
1069     msg_Dbg( p_this, "using winNT/2K/XP ioctl layer" );
1070
1071     sprintf( psz_win32_drive, "\\\\.\\%c:", psz_dev[0] );
1072
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;
1079 }
1080
1081 #endif /* _WIN32 */
1082
1083 #ifdef __OS2__
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 )
1089 {
1090     char device[] = "X:";
1091     HFILE hcd;
1092     ULONG i_action;
1093     ULONG rc;
1094
1095     p_vcddev->hcd = 0;
1096
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,
1101                   NULL);
1102     if( rc )
1103     {
1104         msg_Err( p_this, "could not open the device %s", psz_dev );
1105
1106         return -1;
1107     }
1108
1109     p_vcddev->hcd = hcd;
1110     return 0;
1111 }
1112
1113 #endif
1114
1115 /* */
1116 static void astrcat( char **ppsz_dst, char *psz_src )
1117 {
1118     char *psz_old = *ppsz_dst;
1119
1120     if( !psz_old )
1121     {
1122         *ppsz_dst = strdup( psz_src );
1123     }
1124     else if( psz_src )
1125     {
1126         if( asprintf( ppsz_dst, "%s%s", psz_old, psz_src ) < 0 )
1127             *ppsz_dst = psz_old;
1128         else
1129             free( psz_old );
1130     }
1131 }
1132
1133 /* */
1134 static int CdTextParse( vlc_meta_t ***ppp_tracks, int *pi_tracks,
1135                         const uint8_t *p_buffer, int i_buffer )
1136 {
1137     char *pppsz_info[128][0x10];
1138     int i_track_last = -1;
1139     if( i_buffer < 4 )
1140         return -1;
1141
1142     memset( pppsz_info, 0, sizeof(pppsz_info) );
1143
1144     for( int i = 0; i < (i_buffer-4)/18; i++ )
1145     {
1146         const uint8_t *p_block = &p_buffer[4 + 18*i];
1147         char psz_text[12+1];
1148
1149         const int i_pack_type = p_block[0];
1150         if( i_pack_type < 0x80 || i_pack_type > 0x8f )
1151             continue;
1152
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 )
1156             continue;
1157
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);
1165
1166         /* */
1167         memcpy( psz_text, &p_block[4], 12 );
1168         psz_text[12] = '\0';
1169
1170         /* */
1171         int i_track =  i_track_number;
1172         char *psz_track = &psz_text[0];
1173         while( i_track <= 127 && psz_track < &psz_text[12] )
1174         {
1175             //fprintf( stderr, "t=%d psz_track=%p end=%p", i_track, psz_track, &psz_text[12] );
1176             if( *psz_track )
1177             {
1178                 astrcat( &pppsz_info[i_track][i_pack_type-0x80], psz_track );
1179                 i_track_last = __MAX( i_track_last, i_track );
1180             }
1181
1182             i_track++;
1183             psz_track += 1 + strlen(psz_track);
1184         }
1185     }
1186
1187     if( i_track_last < 0 )
1188         return -1;
1189
1190     vlc_meta_t **pp_tracks = calloc( i_track_last+1, sizeof(*pp_tracks) );
1191     if( !pp_tracks )
1192         goto exit;
1193
1194     for( int j = 0; j < 0x10; j++ )
1195     {
1196         for( int i = 0; i <= i_track_last; i++ )
1197         {
1198             /* */
1199             if( pppsz_info[i][j] )
1200                 EnsureUTF8( pppsz_info[i][j] );
1201
1202             /* */
1203             const char *psz_default = pppsz_info[0][j];
1204             const char *psz_value = pppsz_info[i][j];
1205
1206             if( !psz_value && !psz_default )
1207                 continue;
1208             vlc_meta_t *p_track = pp_tracks[i];
1209             if( !p_track )
1210             {
1211                 p_track = pp_tracks[i] = vlc_meta_New();
1212                 if( !p_track )
1213                     continue;
1214             }
1215             switch( j )
1216             {
1217             case 0x00: /* Album/Title */
1218                 if( i == 0 )
1219                 {
1220                     vlc_meta_SetAlbum( p_track, psz_value );
1221                 }
1222                 else
1223                 {
1224                     if( psz_value )
1225                         vlc_meta_SetTitle( p_track, psz_value );
1226                     if( psz_default )
1227                         vlc_meta_SetAlbum( p_track, psz_default );
1228                 }
1229                 break;
1230             case 0x01: /* Performer */
1231                 vlc_meta_SetArtist( p_track,
1232                                     psz_value ? psz_value : psz_default );
1233                 break;
1234             case 0x05: /* Messages */
1235                 vlc_meta_SetDescription( p_track,
1236                                          psz_value ? psz_value : psz_default );
1237                 break;
1238             case 0x07: /* Genre */
1239                 vlc_meta_SetGenre( p_track,
1240                                    psz_value ? psz_value : psz_default );
1241                 break;
1242             /* FIXME unsupported:
1243              * 0x02: songwriter
1244              * 0x03: composer
1245              * 0x04: arrenger
1246              * 0x06: disc id */
1247             }
1248         }
1249     }
1250     /* */
1251 exit:
1252     for( int j = 0; j < 0x10; j++ )
1253         for( int i = 0; i <= i_track_last; i++ )
1254             free( pppsz_info[i][j] );
1255
1256     *ppp_tracks = pp_tracks;
1257     *pi_tracks = i_track_last+1;
1258     return pp_tracks ? 0 : -1;
1259 }
1260
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 )
1267 {
1268     VLC_UNUSED( p_object );
1269     VLC_UNUSED( p_vcddev );
1270     VLC_UNUSED( pp_buffer );
1271     VLC_UNUSED( pi_buffer );
1272     return -1;
1273 }
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 )
1277 {
1278     VLC_UNUSED( p_object );
1279
1280     CDROM_READ_TOC_EX TOCEx;
1281     memset(&TOCEx, 0, sizeof(TOCEx));
1282     TOCEx.Format = CDROM_READ_TOC_EX_FORMAT_CDTEXT;
1283
1284     const int i_header_size = __MAX( 4, MINIMUM_CDROM_READ_TOC_EX_SIZE );
1285     uint8_t header[i_header_size];
1286     DWORD i_read;
1287     if( !DeviceIoControl( p_vcddev->h_device_handle, IOCTL_CDROM_READ_TOC_EX,
1288                           &TOCEx, sizeof(TOCEx), header, i_header_size, &i_read, 0 ) )
1289         return -1;
1290
1291     const int i_text = 2 + (header[0] << 8) + header[1];
1292     if( i_text <= 4 )
1293         return -1;
1294
1295     /* Read complete CD-TEXT */
1296     uint8_t *p_text = calloc( 1, i_text );
1297     if( !p_text )
1298         return VLC_EGENERIC;
1299
1300     if( !DeviceIoControl( p_vcddev->h_device_handle, IOCTL_CDROM_READ_TOC_EX,
1301                           &TOCEx, sizeof(TOCEx), p_text, i_text, &i_read, 0 ) )
1302     {
1303         free( p_text );
1304         return VLC_EGENERIC;
1305     }
1306
1307     /* */
1308     *pp_buffer = p_text;
1309     *pi_buffer = i_text;
1310     return VLC_SUCCESS;
1311 }
1312 #else
1313 static int CdTextRead( vlc_object_t *p_object, const vcddev_t *p_vcddev,
1314                        uint8_t **pp_buffer, int *pi_buffer )
1315 {
1316     VLC_UNUSED( p_object );
1317
1318     if( p_vcddev->i_device_handle == -1 )
1319         return -1;
1320
1321     struct cdrom_generic_command gc;
1322     uint8_t header[4];
1323
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;
1332
1333     gc.buflen = sizeof(header);
1334     gc.buffer = header;
1335     gc.data_direction = CGC_DATA_READ;
1336     gc.timeout = 1000;
1337
1338     if( ioctl( p_vcddev->i_device_handle, CDROM_SEND_PACKET, &gc ) == -1 )
1339         return VLC_EGENERIC;
1340
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];
1344     if( i_text <= 4 )
1345         return VLC_EGENERIC;
1346
1347     /* Read complete CD-TEXT */
1348     uint8_t *p_text = calloc( 1, i_text );
1349     if( !p_text )
1350         return VLC_EGENERIC;
1351
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;
1358
1359     gc.buflen = i_text;
1360     gc.buffer = p_text;
1361     gc.data_direction = CGC_DATA_READ;
1362     gc.timeout = 1000;
1363
1364     if( ioctl( p_vcddev->i_device_handle, CDROM_SEND_PACKET, &gc ) == -1 )
1365     {
1366         free( p_text );
1367         return VLC_EGENERIC;
1368     }
1369
1370     /* */
1371     *pp_buffer = p_text;
1372     *pi_buffer = i_text;
1373     return VLC_SUCCESS;
1374 }
1375 #endif
1376
1377 int ioctl_GetCdText( vlc_object_t *p_object, const vcddev_t *p_vcddev,
1378                      vlc_meta_t ***ppp_tracks, int *pi_tracks )
1379 {
1380     uint8_t *p_text;
1381     int i_text;
1382
1383     if( p_vcddev->i_vcdimage_handle != -1 )
1384         return -1;
1385
1386     if( CdTextRead( p_object, p_vcddev, &p_text, &i_text ) )
1387         return -1;
1388
1389     CdTextParse( ppp_tracks, pi_tracks, p_text, i_text );
1390     free( p_text );
1391     return 0;
1392 }
1393