2
* Copyright (C) 2001-2004 Billy Biggs <vektor@dumbterm.net>,
3
* Håkan Hjort <d95hjort@dtek.chalmers.se>,
4
* Björn Englund <d4bjorn@dtek.chalmers.se>
6
* This file is part of libdvdread.
8
* libdvdread is free software; you can redistribute it and/or modify
9
* it under the terms of the GNU General Public License as published by
10
* the Free Software Foundation; either version 2 of the License, or
11
* (at your option) any later version.
13
* libdvdread is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
* GNU General Public License for more details.
18
* You should have received a copy of the GNU General Public License along
19
* with libdvdread; if not, write to the Free Software Foundation, Inc.,
20
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23
#include <sys/types.h>
25
#include <sys/time.h> /* For the timing of dvdcss_title crack. */
37
/* misc win32 helpers */
40
#ifndef HAVE_GETTIMEOFDAY
41
/* replacement gettimeofday implementation */
42
#include <sys/timeb.h>
43
static inline int _private_gettimeofday( struct timeval *tv, void *tz )
48
tv->tv_usec = t.millitm * 1000;
51
#define gettimeofday(TV, TZ) _private_gettimeofday((TV), (TZ))
53
#include <io.h> /* read() */
54
#define lseek64 _lseeki64
57
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__bsdi__) || defined(__APPLE__)
62
#include <sys/mnttab.h>
63
#elif defined(__APPLE__)
64
#include <sys/param.h>
65
#include <sys/ucred.h>
66
#include <sys/mount.h>
67
#elif defined(SYS_BSD)
69
#elif defined(__linux__)
74
#include "dvdread/dvd_udf.h"
75
#include "dvd_input.h"
76
#include "dvdread/dvd_reader.h"
79
#define DEFAULT_UDF_CACHE_LEVEL 1
82
/* Basic information. */
85
/* Hack for keeping track of the css status.
86
* 0: no css, 1: perhaps (need init of keys), 2: have done init */
88
int css_title; /* Last title that we have called dvdinpute_title for. */
90
/* Information required for an image file. */
93
/* Information required for a directory path drive. */
96
/* Filesystem cache */
97
int udfcache_level; /* 0 - turned off, 1 - on */
104
/* Basic information. */
107
/* Hack for selecting the right css title. */
110
/* Information required for an image file. */
114
/* Information required for a directory path drive. */
115
size_t title_sizes[ TITLES_MAX ];
116
dvd_input_t title_devs[ TITLES_MAX ];
118
/* Calculated at open-time, size in blocks. */
122
int UDFReadBlocksRaw( dvd_reader_t *device, uint32_t lb_number,
123
size_t block_count, unsigned char *data,
127
* Set the level of caching on udf
128
* level = 0 (no caching)
129
* level = 1 (caching filesystem info)
131
int DVDUDFCacheLevel(dvd_reader_t *device, int level)
133
struct dvd_reader_s *dev = (struct dvd_reader_s *)device;
137
} else if(level < 0) {
138
return dev->udfcache_level;
141
dev->udfcache_level = level;
146
void *GetUDFCacheHandle(dvd_reader_t *device)
148
struct dvd_reader_s *dev = (struct dvd_reader_s *)device;
150
return dev->udfcache;
153
void SetUDFCacheHandle(dvd_reader_t *device, void *cache)
155
struct dvd_reader_s *dev = (struct dvd_reader_s *)device;
157
dev->udfcache = cache;
162
/* Loop over all titles and call dvdcss_title to crack the keys. */
163
static int initAllCSSKeys( dvd_reader_t *dvd )
165
struct timeval all_s, all_e;
166
struct timeval t_s, t_e;
167
char filename[ MAX_UDF_FILE_NAME_LEN ];
171
char *nokeys_str = getenv("DVDREAD_NOKEYS");
172
if(nokeys_str != NULL)
175
fprintf( stderr, "\n" );
176
fprintf( stderr, "libdvdread: Attempting to retrieve all CSS keys\n" );
177
fprintf( stderr, "libdvdread: This can take a _long_ time, "
178
"please be patient\n\n" );
179
gettimeofday(&all_s, NULL);
181
for( title = 0; title < 100; title++ ) {
182
gettimeofday( &t_s, NULL );
184
sprintf( filename, "/VIDEO_TS/VIDEO_TS.VOB" );
186
sprintf( filename, "/VIDEO_TS/VTS_%02d_%d.VOB", title, 0 );
188
start = UDFFindFile( dvd, filename, &len );
189
if( start != 0 && len != 0 ) {
190
/* Perform CSS key cracking for this title. */
191
fprintf( stderr, "libdvdread: Get key for %s at 0x%08x\n",
193
if( dvdinput_title( dvd->dev, (int)start ) < 0 ) {
194
fprintf( stderr, "libdvdread: Error cracking CSS key for %s (0x%08x)\n", filename, start);
196
gettimeofday( &t_e, NULL );
197
fprintf( stderr, "libdvdread: Elapsed time %ld\n",
198
(long int) t_e.tv_sec - t_s.tv_sec );
201
if( title == 0 ) continue;
203
gettimeofday( &t_s, NULL );
204
sprintf( filename, "/VIDEO_TS/VTS_%02d_%d.VOB", title, 1 );
205
start = UDFFindFile( dvd, filename, &len );
206
if( start == 0 || len == 0 ) break;
208
/* Perform CSS key cracking for this title. */
209
fprintf( stderr, "libdvdread: Get key for %s at 0x%08x\n",
211
if( dvdinput_title( dvd->dev, (int)start ) < 0 ) {
212
fprintf( stderr, "libdvdread: Error cracking CSS key for %s (0x%08x)!!\n", filename, start);
214
gettimeofday( &t_e, NULL );
215
fprintf( stderr, "libdvdread: Elapsed time %ld\n",
216
(long int) t_e.tv_sec - t_s.tv_sec );
220
fprintf( stderr, "libdvdread: Found %d VTS's\n", title );
221
gettimeofday(&all_e, NULL);
222
fprintf( stderr, "libdvdread: Elapsed time %ld\n",
223
(long int) all_e.tv_sec - all_s.tv_sec );
231
* Open a DVD image or block device file.
233
static dvd_reader_t *DVDOpenImageFile( const char *location, int have_css )
238
dev = dvdinput_open( location );
240
fprintf( stderr, "libdvdread: Can't open %s for reading\n", location );
244
dvd = (dvd_reader_t *) malloc( sizeof( dvd_reader_t ) );
249
dvd->isImageFile = 1;
251
dvd->path_root = NULL;
253
dvd->udfcache_level = DEFAULT_UDF_CACHE_LEVEL;
254
dvd->udfcache = NULL;
257
/* Only if DVDCSS_METHOD = title, a bit if it's disc or if
258
* DVDCSS_METHOD = key but region mismatch. Unfortunately we
259
* don't have that information. */
261
dvd->css_state = 1; /* Need key init. */
268
static dvd_reader_t *DVDOpenPath( const char *path_root )
272
dvd = (dvd_reader_t *) malloc( sizeof( dvd_reader_t ) );
273
if( !dvd ) return NULL;
274
dvd->isImageFile = 0;
276
dvd->path_root = strdup( path_root );
277
if(!dvd->path_root) {
281
dvd->udfcache_level = DEFAULT_UDF_CACHE_LEVEL;
282
dvd->udfcache = NULL;
284
dvd->css_state = 0; /* Only used in the UDF path */
285
dvd->css_title = 0; /* Only matters in the UDF path */
291
/* /dev/rdsk/c0t6d0s0 (link to /devices/...)
292
/vol/dev/rdsk/c0t6d0/??
294
static char *sun_block2char( const char *path )
298
/* Must contain "/dsk/" */
299
if( !strstr( path, "/dsk/" ) ) return (char *) strdup( path );
301
/* Replace "/dsk/" with "/rdsk/" */
302
new_path = malloc( strlen(path) + 2 );
303
strcpy( new_path, path );
304
strcpy( strstr( new_path, "/dsk/" ), "" );
305
strcat( new_path, "/rdsk/" );
306
strcat( new_path, strstr( path, "/dsk/" ) + strlen( "/dsk/" ) );
313
/* FreeBSD /dev/(r)(a)cd0c (a is for atapi), recommended to _not_ use r
314
update: FreeBSD and DragonFly no longer uses the prefix so don't add it.
315
OpenBSD /dev/rcd0c, it needs to be the raw device
316
NetBSD /dev/rcd0[d|c|..] d for x86, c (for non x86), perhaps others
317
Darwin /dev/rdisk0, it needs to be the raw device
318
BSD/OS /dev/sr0c (if not mounted) or /dev/rsr0c ('c' any letter will do)
319
returns a string allocated with strdup. It should be freed when no longer
321
static char *bsd_block2char( const char *path )
323
#if defined(__FreeBSD__) || defined(__DragonFly__)
324
return (char *) strdup( path );
328
/* If it doesn't start with "/dev/" or does start with "/dev/r" exit */
329
if( strncmp( path, "/dev/", 5 ) || !strncmp( path, "/dev/r", 6 ) )
330
return (char *) strdup( path );
332
/* Replace "/dev/" with "/dev/r" */
333
new_path = malloc( strlen(path) + 2 );
334
strcpy( new_path, "/dev/r" );
335
strcat( new_path, path + strlen( "/dev/" ) );
338
#endif /* __FreeBSD__ || __DragonFly__ */
343
dvd_reader_t *DVDOpen( const char *ppath )
345
struct stat fileinfo;
346
int ret, have_css, retval, cdir = -1;
347
dvd_reader_t *ret_val = NULL;
348
char *dev_name = NULL;
349
char *path = NULL, *new_path = NULL, *path_copy = NULL;
351
#if defined(_WIN32) || defined(__OS2__)
358
path = strdup(ppath);
362
/* Try to open libdvdcss or fall back to standard functions */
363
have_css = dvdinput_setup();
365
#if defined(_WIN32) || defined(__OS2__)
366
/* Strip off the trailing \ if it is not a drive */
369
(path[len - 1] == '\\') &&
370
(path[len - 2] != ':'))
376
ret = stat( path, &fileinfo );
380
/* maybe "host:port" url? try opening it with acCeSS library */
381
if( strchr(path,':') ) {
382
ret_val = DVDOpenImageFile( path, have_css );
387
/* If we can't stat the file, give up */
388
fprintf( stderr, "libdvdread: Can't stat %s\n", path );
393
/* First check if this is a block/char device or a file*/
394
if( S_ISBLK( fileinfo.st_mode ) ||
395
S_ISCHR( fileinfo.st_mode ) ||
396
S_ISREG( fileinfo.st_mode ) ) {
399
* Block devices and regular files are assumed to be DVD-Video images.
401
dvd_reader_t *dvd = NULL;
403
dev_name = sun_block2char( path );
404
#elif defined(SYS_BSD)
405
dev_name = bsd_block2char( path );
407
dev_name = strdup( path );
409
dvd = DVDOpenImageFile( dev_name, have_css );
413
} else if( S_ISDIR( fileinfo.st_mode ) ) {
414
dvd_reader_t *auth_drive = 0;
417
#elif defined(__sun) || defined(__linux__)
421
/* XXX: We should scream real loud here. */
422
if( !(path_copy = strdup( path ) ) )
425
#ifndef WIN32 /* don't have fchdir, and getcwd( NULL, ... ) is strange */
426
/* Also WIN32 does not have symlinks, so we don't need this bit of code. */
428
/* Resolve any symlinks and get the absolute dir name. */
430
if( ( cdir = open( ".", O_RDONLY ) ) >= 0 ) {
431
if( chdir( path_copy ) == -1 ) {
435
new_path = get_current_dir_name();
436
if(new_path == NULL) {
440
new_path = malloc(PATH_MAX+1);
444
if( getcwd( new_path, PATH_MAX ) == NULL ) {
448
retval = fchdir( cdir );
454
path_copy = new_path;
461
* If we're being asked to open a directory, check if that directory
462
* is the mount point for a DVD-ROM which we can use instead.
465
if( strlen( path_copy ) > 1 ) {
466
if( path_copy[ strlen( path_copy ) - 1 ] == '/' ) {
467
path_copy[ strlen( path_copy ) - 1 ] = '\0';
471
#if defined(_WIN32) || defined(__OS2__)
472
if(strlen(path_copy) > TITLES_MAX) {
473
if(!strcasecmp(&(path_copy[strlen( path_copy ) - TITLES_MAX]),
475
path_copy[strlen(path_copy) - (TITLES_MAX-1)] = '\0';
478
if( strlen( path_copy ) > TITLES_MAX ) {
479
if( !strcasecmp( &(path_copy[ strlen( path_copy ) - TITLES_MAX ]),
481
path_copy[ strlen( path_copy ) - TITLES_MAX ] = '\0';
485
if(path_copy[0] == '\0') {
490
#if defined(__APPLE__)
491
struct statfs s[128];
492
int r = getfsstat(NULL, 0, MNT_NOWAIT);
496
r = getfsstat(s, r * sizeof(s[0]), MNT_NOWAIT);
498
for (i=0; i<r; i++) {
499
if (!strcmp(path_copy, s[i].f_mntonname)) {
500
dev_name = bsd_block2char(s[i].f_mntfromname);
502
"libdvdread: Attempting to use device %s"
503
" mounted on %s for CSS authentication\n",
506
auth_drive = DVDOpenImageFile( dev_name, have_css );
511
#elif defined(SYS_BSD)
512
if( ( fe = getfsfile( path_copy ) ) ) {
513
dev_name = bsd_block2char( fe->fs_spec );
515
"libdvdread: Attempting to use device %s"
516
" mounted on %s for CSS authentication\n",
519
auth_drive = DVDOpenImageFile( dev_name, have_css );
522
mntfile = fopen( MNTTAB, "r" );
527
while( ( res = getmntent( mntfile, &mp ) ) != -1 ) {
528
if( res == 0 && !strcmp( mp.mnt_mountp, path_copy ) ) {
529
dev_name = sun_block2char( mp.mnt_special );
531
"libdvdread: Attempting to use device %s"
532
" mounted on %s for CSS authentication\n",
535
auth_drive = DVDOpenImageFile( dev_name, have_css );
541
#elif defined(__linux__)
542
mntfile = fopen( _PATH_MOUNTED, "r" );
546
while( ( me = getmntent( mntfile ) ) ) {
547
if( !strcmp( me->mnt_dir, path_copy ) ) {
549
"libdvdread: Attempting to use device %s"
550
" mounted on %s for CSS authentication\n",
553
auth_drive = DVDOpenImageFile( me->mnt_fsname, have_css );
554
dev_name = strdup(me->mnt_fsname);
560
#elif defined(__OS2__)
561
/* Use DVDOpenImageFile() only if it is a drive */
562
if(isalpha(path[0]) && path[1] == ':' &&
564
((path[2] == '\\' || path[2] == '/') && !path[3])))
565
auth_drive = DVDOpenImageFile( path, have_css );
566
#elif defined(_WIN32)
567
if( GetDriveType( path_copy ) == DRIVE_CDROM ) {
569
auth_drive = DVDOpenImageFile( path_copy, have_css );
573
#if !defined(_WIN32) && !defined(__OS2__)
575
fprintf( stderr, "libdvdread: Couldn't find device name.\n" );
576
} else if( !auth_drive ) {
577
fprintf( stderr, "libdvdread: Device %s inaccessible, "
578
"CSS authentication not available.\n", dev_name );
582
fprintf( stderr, "libdvdread: Device %s inaccessible, "
583
"CSS authentication not available.\n", path );
593
* If we've opened a drive, just use that.
600
* Otherwise, we now try to open the directory tree instead.
602
ret_val = DVDOpenPath( path );
608
/* If it's none of the above, screw it. */
609
fprintf( stderr, "libdvdread: Could not open %s\n", path );
612
if ( path_copy != NULL )
616
if ( new_path != NULL )
621
void DVDClose( dvd_reader_t *dvd )
624
if( dvd->dev ) dvdinput_close( dvd->dev );
625
if( dvd->path_root ) free( dvd->path_root );
626
if( dvd->udfcache ) FreeUDFCache( dvd->udfcache );
632
* Open an unencrypted file on a DVD image file.
634
static dvd_file_t *DVDOpenFileUDF( dvd_reader_t *dvd, char *filename )
637
dvd_file_t *dvd_file;
639
start = UDFFindFile( dvd, filename, &len );
641
fprintf( stderr, "libdvdnav:DVDOpenFileUDF:UDFFindFile %s failed\n", filename );
645
dvd_file = (dvd_file_t *) malloc( sizeof( dvd_file_t ) );
647
fprintf( stderr, "libdvdnav:DVDOpenFileUDF:malloc failed\n" );
651
dvd_file->lb_start = start;
652
dvd_file->seek_pos = 0;
653
memset( dvd_file->title_sizes, 0, sizeof( dvd_file->title_sizes ) );
654
memset( dvd_file->title_devs, 0, sizeof( dvd_file->title_devs ) );
655
dvd_file->filesize = len / DVD_VIDEO_LB_LEN;
661
* Searches for <file> in directory <path>, ignoring case.
662
* Returns 0 and full filename in <filename>.
663
* or -1 on file not found.
664
* or -2 on path not found.
666
static int findDirFile( const char *path, const char *file, char **filename )
672
dir = opendir( path );
673
if( !dir ) return -2;
675
while( ( ent = readdir( dir ) ) != NULL ) {
676
if( !strcasecmp( ent->d_name, file ) ) {
677
*filename = malloc( strlen( path ) + 1 + strlen( ent->d_name ) + 1 );
678
if( *filename == NULL ) {
682
sprintf( *filename, "%s%s%s", path,
683
( ( path[ strlen( path ) - 1 ] == '/' ) ? "" : "/" ),
693
static int findDVDFile( dvd_reader_t *dvd, const char *file, char **filename )
695
char *video_path = NULL;
696
const char *nodirfile;
699
/* Strip off the directory for our search */
700
if( !strncasecmp( "/VIDEO_TS/", file, 10 ) ) {
701
nodirfile = &(file[ 10 ]);
706
ret = findDirFile( dvd->path_root, nodirfile, filename );
708
/* Try also with adding the path, just in case. */
709
video_path = malloc( strlen( dvd->path_root ) + 10 + 1 );
710
if( video_path == NULL ) return 0;
711
sprintf( video_path, "%s/VIDEO_TS/", dvd->path_root );
712
ret = findDirFile( video_path, nodirfile, filename );
714
/* Try with the path, but in lower case. */
715
sprintf( video_path, "%s/video_ts/", dvd->path_root );
716
ret = findDirFile( video_path, nodirfile, filename );
729
* Open an unencrypted file from a DVD directory tree.
731
static dvd_file_t *DVDOpenFilePath( dvd_reader_t *dvd, char *filename )
733
char *full_path = NULL;
734
dvd_file_t *dvd_file;
735
struct stat fileinfo;
738
/* Get the full path of the file. */
739
if( !findDVDFile( dvd, filename, &full_path ) ) {
740
fprintf( stderr, "libdvdnav:DVDOpenFilePath:findDVDFile %s failed\n", filename );
745
dev = dvdinput_open( full_path );
747
fprintf( stderr, "libdvdnav:DVDOpenFilePath:dvdinput_open %s failed\n", full_path );
752
dvd_file = (dvd_file_t *) malloc( sizeof( dvd_file_t ) );
754
fprintf( stderr, "libdvdnav:DVDOpenFilePath:dvd_file malloc failed\n" );
760
dvd_file->lb_start = 0;
761
dvd_file->seek_pos = 0;
762
memset( dvd_file->title_sizes, 0, sizeof( dvd_file->title_sizes ) );
763
memset( dvd_file->title_devs, 0, sizeof( dvd_file->title_devs ) );
764
dvd_file->filesize = 0;
766
if( stat( full_path, &fileinfo ) < 0 ) {
767
fprintf( stderr, "libdvdread: Can't stat() %s.\n", filename );
770
dvdinput_close( dev );
773
dvd_file->title_sizes[ 0 ] = fileinfo.st_size / DVD_VIDEO_LB_LEN;
774
dvd_file->title_devs[ 0 ] = dev;
775
dvd_file->filesize = dvd_file->title_sizes[ 0 ];
781
static dvd_file_t *DVDOpenVOBUDF( dvd_reader_t *dvd, int title, int menu )
783
char filename[ MAX_UDF_FILE_NAME_LEN ];
785
dvd_file_t *dvd_file;
788
sprintf( filename, "/VIDEO_TS/VIDEO_TS.VOB" );
790
sprintf( filename, "/VIDEO_TS/VTS_%02d_%d.VOB", title, menu ? 0 : 1 );
792
start = UDFFindFile( dvd, filename, &len );
793
if( start == 0 ) return NULL;
795
dvd_file = (dvd_file_t *) malloc( sizeof( dvd_file_t ) );
796
if( !dvd_file ) return NULL;
798
/*Hack*/ dvd_file->css_title = title << 1 | menu;
799
dvd_file->lb_start = start;
800
dvd_file->seek_pos = 0;
801
memset( dvd_file->title_sizes, 0, sizeof( dvd_file->title_sizes ) );
802
memset( dvd_file->title_devs, 0, sizeof( dvd_file->title_devs ) );
803
dvd_file->filesize = len / DVD_VIDEO_LB_LEN;
805
/* Calculate the complete file size for every file in the VOBS */
809
for( cur = 2; cur < 10; cur++ ) {
810
sprintf( filename, "/VIDEO_TS/VTS_%02d_%d.VOB", title, cur );
811
if( !UDFFindFile( dvd, filename, &len ) ) break;
812
dvd_file->filesize += len / DVD_VIDEO_LB_LEN;
816
if( dvd->css_state == 1 /* Need key init */ ) {
817
initAllCSSKeys( dvd );
821
if( dvdinput_title( dvd_file->dvd->dev, (int)start ) < 0 ) {
822
fprintf( stderr, "libdvdread: Error cracking CSS key for %s\n",
830
static dvd_file_t *DVDOpenVOBPath( dvd_reader_t *dvd, int title, int menu )
832
char filename[ MAX_UDF_FILE_NAME_LEN ];
833
char *full_path = NULL;
834
struct stat fileinfo;
835
dvd_file_t *dvd_file;
838
dvd_file = (dvd_file_t *) malloc( sizeof( dvd_file_t ) );
839
if( !dvd_file ) return NULL;
841
/*Hack*/ dvd_file->css_title = title << 1 | menu;
842
dvd_file->lb_start = 0;
843
dvd_file->seek_pos = 0;
844
memset( dvd_file->title_sizes, 0, sizeof( dvd_file->title_sizes ) );
845
memset( dvd_file->title_devs, 0, sizeof( dvd_file->title_devs ) );
846
dvd_file->filesize = 0;
852
sprintf( filename, "VIDEO_TS.VOB" );
854
sprintf( filename, "VTS_%02i_0.VOB", title );
856
if( !findDVDFile( dvd, filename, &full_path ) ) {
862
dev = dvdinput_open( full_path );
869
if( stat( full_path, &fileinfo ) < 0 ) {
870
fprintf( stderr, "libdvdread: Can't stat() %s.\n", filename );
876
dvd_file->title_sizes[ 0 ] = fileinfo.st_size / DVD_VIDEO_LB_LEN;
877
dvd_file->title_devs[ 0 ] = dev;
878
dvdinput_title( dvd_file->title_devs[0], 0);
879
dvd_file->filesize = dvd_file->title_sizes[ 0 ];
882
for( i = 0; i < TITLES_MAX; ++i ) {
884
sprintf( filename, "VTS_%02i_%i.VOB", title, i + 1 );
885
if( !findDVDFile( dvd, filename, &full_path ) ) {
889
if( stat( full_path, &fileinfo ) < 0 ) {
890
fprintf( stderr, "libdvdread: Can't stat() %s.\n", filename );
894
dvd_file->title_sizes[ i ] = fileinfo.st_size / DVD_VIDEO_LB_LEN;
895
dvd_file->title_devs[ i ] = dvdinput_open( full_path );
896
dvdinput_title( dvd_file->title_devs[ i ], 0 );
897
dvd_file->filesize += dvd_file->title_sizes[ i ];
899
if( !dvd_file->title_devs[ 0 ] ) {
909
dvd_file_t *DVDOpenFile( dvd_reader_t *dvd, int titlenum,
910
dvd_read_domain_t domain )
912
char filename[ MAX_UDF_FILE_NAME_LEN ];
914
/* Check arguments. */
915
if( dvd == NULL || titlenum < 0 )
919
case DVD_READ_INFO_FILE:
920
if( titlenum == 0 ) {
921
sprintf( filename, "/VIDEO_TS/VIDEO_TS.IFO" );
923
sprintf( filename, "/VIDEO_TS/VTS_%02i_0.IFO", titlenum );
926
case DVD_READ_INFO_BACKUP_FILE:
927
if( titlenum == 0 ) {
928
sprintf( filename, "/VIDEO_TS/VIDEO_TS.BUP" );
930
sprintf( filename, "/VIDEO_TS/VTS_%02i_0.BUP", titlenum );
933
case DVD_READ_MENU_VOBS:
934
if( dvd->isImageFile ) {
935
return DVDOpenVOBUDF( dvd, titlenum, 1 );
937
return DVDOpenVOBPath( dvd, titlenum, 1 );
940
case DVD_READ_TITLE_VOBS:
941
if( titlenum == 0 ) return 0;
942
if( dvd->isImageFile ) {
943
return DVDOpenVOBUDF( dvd, titlenum, 0 );
945
return DVDOpenVOBPath( dvd, titlenum, 0 );
949
fprintf( stderr, "libdvdread: Invalid domain for file open.\n" );
953
if( dvd->isImageFile ) {
954
return DVDOpenFileUDF( dvd, filename );
956
return DVDOpenFilePath( dvd, filename );
960
void DVDCloseFile( dvd_file_t *dvd_file )
965
if( !dvd_file->dvd->isImageFile ) {
966
for( i = 0; i < TITLES_MAX; ++i ) {
967
if( dvd_file->title_devs[ i ] ) {
968
dvdinput_close( dvd_file->title_devs[i] );
978
static int DVDFileStatVOBUDF( dvd_reader_t *dvd, int title,
979
int menu, dvd_stat_t *statbuf )
981
char filename[ MAX_UDF_FILE_NAME_LEN ];
984
off_t parts_size[ 9 ];
989
sprintf( filename, "/VIDEO_TS/VIDEO_TS.VOB" );
991
sprintf( filename, "/VIDEO_TS/VTS_%02d_%d.VOB", title, menu ? 0 : 1 );
993
if( !UDFFindFile( dvd, filename, &size ) )
998
parts_size[ 0 ] = size;
1003
for( cur = 2; cur < 10; cur++ ) {
1004
sprintf( filename, "/VIDEO_TS/VTS_%02d_%d.VOB", title, cur );
1005
if( !UDFFindFile( dvd, filename, &size ) )
1008
parts_size[ nr_parts ] = size;
1014
statbuf->size = tot_size;
1015
statbuf->nr_parts = nr_parts;
1016
for( n = 0; n < nr_parts; n++ )
1017
statbuf->parts_size[ n ] = parts_size[ n ];
1023
static int DVDFileStatVOBPath( dvd_reader_t *dvd, int title,
1024
int menu, dvd_stat_t *statbuf )
1026
char filename[ MAX_UDF_FILE_NAME_LEN ];
1027
char *full_path = NULL;
1028
struct stat fileinfo;
1030
off_t parts_size[ 9 ];
1035
sprintf( filename, "VIDEO_TS.VOB" );
1037
sprintf( filename, "VTS_%02d_%d.VOB", title, menu ? 0 : 1 );
1039
if( !findDVDFile( dvd, filename, &full_path ) ) {
1044
if( stat( full_path, &fileinfo ) < 0 ) {
1045
fprintf( stderr, "libdvdread: Can't stat() %s.\n", filename );
1050
tot_size = fileinfo.st_size;
1052
parts_size[ 0 ] = fileinfo.st_size;
1056
for( cur = 2; cur < 10; cur++ ) {
1057
sprintf( filename, "VTS_%02d_%d.VOB", title, cur );
1058
if( !findDVDFile( dvd, filename, &full_path ) )
1061
if( stat( full_path, &fileinfo ) < 0 ) {
1062
fprintf( stderr, "libdvdread: Can't stat() %s.\n", filename );
1066
parts_size[ nr_parts ] = fileinfo.st_size;
1067
tot_size += parts_size[ nr_parts ];
1072
statbuf->size = tot_size;
1073
statbuf->nr_parts = nr_parts;
1074
for( n = 0; n < nr_parts; n++ )
1075
statbuf->parts_size[ n ] = parts_size[ n ];
1082
int DVDFileStat( dvd_reader_t *dvd, int titlenum,
1083
dvd_read_domain_t domain, dvd_stat_t *statbuf )
1085
char filename[ MAX_UDF_FILE_NAME_LEN ];
1086
char *full_path = NULL;
1087
struct stat fileinfo;
1090
/* Check arguments. */
1091
if( dvd == NULL || titlenum < 0 ) {
1097
case DVD_READ_INFO_FILE:
1099
sprintf( filename, "/VIDEO_TS/VIDEO_TS.IFO" );
1101
sprintf( filename, "/VIDEO_TS/VTS_%02i_0.IFO", titlenum );
1104
case DVD_READ_INFO_BACKUP_FILE:
1106
sprintf( filename, "/VIDEO_TS/VIDEO_TS.BUP" );
1108
sprintf( filename, "/VIDEO_TS/VTS_%02i_0.BUP", titlenum );
1111
case DVD_READ_MENU_VOBS:
1112
if( dvd->isImageFile )
1113
return DVDFileStatVOBUDF( dvd, titlenum, 1, statbuf );
1115
return DVDFileStatVOBPath( dvd, titlenum, 1, statbuf );
1118
case DVD_READ_TITLE_VOBS:
1122
if( dvd->isImageFile )
1123
return DVDFileStatVOBUDF( dvd, titlenum, 0, statbuf );
1125
return DVDFileStatVOBPath( dvd, titlenum, 0, statbuf );
1129
fprintf( stderr, "libdvdread: Invalid domain for file stat.\n" );
1134
if( dvd->isImageFile ) {
1135
if( UDFFindFile( dvd, filename, &size ) ) {
1136
statbuf->size = size;
1137
statbuf->nr_parts = 1;
1138
statbuf->parts_size[ 0 ] = size;
1142
if( findDVDFile( dvd, filename, &full_path ) ) {
1143
if( stat( full_path, &fileinfo ) < 0 )
1144
fprintf( stderr, "libdvdread: Can't stat() %s.\n", filename );
1146
statbuf->size = fileinfo.st_size;
1147
statbuf->nr_parts = 1;
1148
statbuf->parts_size[ 0 ] = statbuf->size;
1158
/* Internal, but used from dvd_udf.c */
1159
int UDFReadBlocksRaw( dvd_reader_t *device, uint32_t lb_number,
1160
size_t block_count, unsigned char *data,
1165
if( !device->dev ) {
1166
fprintf( stderr, "libdvdread: Fatal error in block read.\n" );
1170
ret = dvdinput_seek( device->dev, (int) lb_number );
1171
if( ret != (int) lb_number ) {
1172
fprintf( stderr, "libdvdread: Can't seek to block %u\n", lb_number );
1176
ret = dvdinput_read( device->dev, (char *) data,
1177
(int) block_count, encrypted );
1181
/* This is using a single input and starting from 'dvd_file->lb_start' offset.
1183
* Reads 'block_count' blocks from 'dvd_file' at block offset 'offset'
1184
* into the buffer located at 'data' and if 'encrypted' is set
1185
* descramble the data if it's encrypted. Returning either an
1186
* negative error or the number of blocks read. */
1187
static int DVDReadBlocksUDF( dvd_file_t *dvd_file, uint32_t offset,
1188
size_t block_count, unsigned char *data,
1191
return UDFReadBlocksRaw( dvd_file->dvd, dvd_file->lb_start + offset,
1192
block_count, data, encrypted );
1195
/* This is using possibly several inputs and starting from an offset of '0'.
1197
* Reads 'block_count' blocks from 'dvd_file' at block offset 'offset'
1198
* into the buffer located at 'data' and if 'encrypted' is set
1199
* descramble the data if it's encrypted. Returning either an
1200
* negative error or the number of blocks read. */
1201
static int DVDReadBlocksPath( dvd_file_t *dvd_file, unsigned int offset,
1202
size_t block_count, unsigned char *data,
1210
for( i = 0; i < TITLES_MAX; ++i ) {
1211
if( !dvd_file->title_sizes[ i ] ) return 0; /* Past end of file */
1213
if( offset < dvd_file->title_sizes[ i ] ) {
1214
if( ( offset + block_count ) <= dvd_file->title_sizes[ i ] ) {
1215
off = dvdinput_seek( dvd_file->title_devs[ i ], (int)offset );
1216
if( off < 0 || off != (int)offset ) {
1217
fprintf( stderr, "libdvdread: Can't seek to block %d\n",
1219
return off < 0 ? off : 0;
1221
ret = dvdinput_read( dvd_file->title_devs[ i ], data,
1222
(int)block_count, encrypted );
1225
size_t part1_size = dvd_file->title_sizes[ i ] - offset;
1226
/* FIXME: Really needs to be a while loop.
1227
* (This is only true if you try and read >1GB at a time) */
1230
off = dvdinput_seek( dvd_file->title_devs[ i ], (int)offset );
1231
if( off < 0 || off != (int)offset ) {
1232
fprintf( stderr, "libdvdread: Can't seek to block %d\n",
1234
return off < 0 ? off : 0;
1236
ret = dvdinput_read( dvd_file->title_devs[ i ], data,
1237
(int)part1_size, encrypted );
1238
if( ret < 0 ) return ret;
1239
/* FIXME: This is wrong if i is the last file in the set.
1240
* also error from this read will not show in ret. */
1242
/* Does the next part exist? If not then return now. */
1243
if( i + 1 >= TITLES_MAX || !dvd_file->title_devs[ i + 1 ] )
1247
off = dvdinput_seek( dvd_file->title_devs[ i + 1 ], 0 );
1248
if( off < 0 || off != 0 ) {
1249
fprintf( stderr, "libdvdread: Can't seek to block %d\n",
1251
return off < 0 ? off : 0;
1253
ret2 = dvdinput_read( dvd_file->title_devs[ i + 1 ],
1255
* (int64_t)DVD_VIDEO_LB_LEN ),
1256
(int)(block_count - part1_size),
1258
if( ret2 < 0 ) return ret2;
1262
offset -= dvd_file->title_sizes[ i ];
1269
/* This is broken reading more than 2Gb at a time is ssize_t is 32-bit. */
1270
ssize_t DVDReadBlocks( dvd_file_t *dvd_file, int offset,
1271
size_t block_count, unsigned char *data )
1275
/* Check arguments. */
1276
if( dvd_file == NULL || offset < 0 || data == NULL )
1279
/* Hack, and it will still fail for multiple opens in a threaded app ! */
1280
if( dvd_file->dvd->css_title != dvd_file->css_title ) {
1281
dvd_file->dvd->css_title = dvd_file->css_title;
1282
if( dvd_file->dvd->isImageFile ) {
1283
dvdinput_title( dvd_file->dvd->dev, (int)dvd_file->lb_start );
1285
/* Here each vobu has it's own dvdcss handle, so no need to update
1287
dvdinput_title( dvd_file->title_devs[ 0 ], (int)dvd_file->lb_start );
1291
if( dvd_file->dvd->isImageFile ) {
1292
ret = DVDReadBlocksUDF( dvd_file, (uint32_t)offset,
1293
block_count, data, DVDINPUT_READ_DECRYPT );
1295
ret = DVDReadBlocksPath( dvd_file, (unsigned int)offset,
1296
block_count, data, DVDINPUT_READ_DECRYPT );
1299
return (ssize_t)ret;
1302
int32_t DVDFileSeek( dvd_file_t *dvd_file, int32_t offset )
1304
/* Check arguments. */
1305
if( dvd_file == NULL || offset < 0 )
1308
if( offset > dvd_file->filesize * DVD_VIDEO_LB_LEN ) {
1311
dvd_file->seek_pos = (uint32_t) offset;
1315
int DVDFileSeekForce(dvd_file_t *dvd_file, int offset, int force_size)
1317
/* Check arguments. */
1318
if( dvd_file == NULL || offset <= 0 )
1321
if( dvd_file->dvd->isImageFile ) {
1322
if( force_size < 0 )
1323
force_size = (offset - 1) / DVD_VIDEO_LB_LEN + 1;
1324
if( dvd_file->filesize < force_size ) {
1325
dvd_file->filesize = force_size;
1326
fprintf(stderr, "libdvdread: Ignored size of file indicated in UDF.\n");
1330
if( offset > dvd_file->filesize * DVD_VIDEO_LB_LEN )
1333
dvd_file->seek_pos = (uint32_t) offset;
1337
ssize_t DVDReadBytes( dvd_file_t *dvd_file, void *data, size_t byte_size )
1339
unsigned char *secbuf_base, *secbuf;
1340
unsigned int numsec, seek_sector, seek_byte;
1343
/* Check arguments. */
1344
if( dvd_file == NULL || data == NULL )
1347
seek_sector = dvd_file->seek_pos / DVD_VIDEO_LB_LEN;
1348
seek_byte = dvd_file->seek_pos % DVD_VIDEO_LB_LEN;
1350
numsec = ( ( seek_byte + byte_size ) / DVD_VIDEO_LB_LEN ) +
1351
( ( ( seek_byte + byte_size ) % DVD_VIDEO_LB_LEN ) ? 1 : 0 );
1353
secbuf_base = (unsigned char *) malloc( numsec * DVD_VIDEO_LB_LEN + 2048 );
1354
secbuf = (unsigned char *)(((uintptr_t)secbuf_base & ~((uintptr_t)2047)) + 2048);
1355
if( !secbuf_base ) {
1356
fprintf( stderr, "libdvdread: Can't allocate memory "
1357
"for file read!\n" );
1361
if( dvd_file->dvd->isImageFile ) {
1362
ret = DVDReadBlocksUDF( dvd_file, (uint32_t) seek_sector,
1363
(size_t) numsec, secbuf, DVDINPUT_NOFLAGS );
1365
ret = DVDReadBlocksPath( dvd_file, seek_sector,
1366
(size_t) numsec, secbuf, DVDINPUT_NOFLAGS );
1369
if( ret != (int) numsec ) {
1370
free( secbuf_base );
1371
return ret < 0 ? ret : 0;
1374
memcpy( data, &(secbuf[ seek_byte ]), byte_size );
1375
free( secbuf_base );
1377
DVDFileSeekForce(dvd_file, dvd_file->seek_pos + byte_size, -1);
1381
ssize_t DVDFileSize( dvd_file_t *dvd_file )
1383
/* Check arguments. */
1384
if( dvd_file == NULL )
1387
return dvd_file->filesize;
1390
int DVDDiscID( dvd_reader_t *dvd, unsigned char *discid )
1394
int nr_of_files = 0;
1396
/* Check arguments. */
1397
if( dvd == NULL || discid == NULL )
1400
/* Go through the first 10 IFO:s, in order,
1401
* and md5sum them, i.e VIDEO_TS.IFO and VTS_0?_0.IFO */
1402
md5_init_ctx( &ctx );
1403
for( title = 0; title < 10; title++ ) {
1404
dvd_file_t *dvd_file = DVDOpenFile( dvd, title, DVD_READ_INFO_FILE );
1405
if( dvd_file != NULL ) {
1407
size_t file_size = dvd_file->filesize * DVD_VIDEO_LB_LEN;
1408
char *buffer_base = malloc( file_size + 2048 );
1409
char *buffer = (char *)(((uintptr_t)buffer_base & ~((uintptr_t)2047)) + 2048);
1411
if( buffer_base == NULL ) {
1412
DVDCloseFile( dvd_file );
1413
fprintf( stderr, "libdvdread: DVDDiscId, failed to "
1414
"allocate memory for file read!\n" );
1418
bytes_read = DVDReadBytes( dvd_file, buffer, file_size );
1419
if( bytes_read != file_size ) {
1420
fprintf( stderr, "libdvdread: DVDDiscId read returned %zd bytes"
1421
", wanted %zd\n", bytes_read, file_size );
1422
DVDCloseFile( dvd_file );
1423
free( buffer_base );
1427
md5_process_bytes( buffer, file_size, &ctx );
1429
DVDCloseFile( dvd_file );
1430
free( buffer_base );
1434
md5_finish_ctx( &ctx, discid );
1442
int DVDISOVolumeInfo( dvd_reader_t *dvd,
1443
char *volid, unsigned int volid_size,
1444
unsigned char *volsetid, unsigned int volsetid_size )
1446
unsigned char *buffer, *buffer_base;
1449
/* Check arguments. */
1453
if( dvd->dev == NULL ) {
1454
/* No block access, so no ISO... */
1458
buffer_base = malloc( DVD_VIDEO_LB_LEN + 2048 );
1459
buffer = (unsigned char *)(((uintptr_t)buffer_base & ~((uintptr_t)2047)) + 2048);
1461
if( buffer_base == NULL ) {
1462
fprintf( stderr, "libdvdread: DVDISOVolumeInfo, failed to "
1463
"allocate memory for file read!\n" );
1467
ret = UDFReadBlocksRaw( dvd, 16, 1, buffer, 0 );
1469
fprintf( stderr, "libdvdread: DVDISOVolumeInfo, failed to "
1470
"read ISO9660 Primary Volume Descriptor!\n" );
1471
free( buffer_base );
1475
if( (volid != NULL) && (volid_size > 0) ) {
1477
for(n = 0; n < 32; n++) {
1478
if(buffer[40+n] == 0x20) {
1483
if(volid_size > n+1) {
1487
memcpy(volid, &buffer[40], volid_size-1);
1488
volid[volid_size-1] = '\0';
1491
if( (volsetid != NULL) && (volsetid_size > 0) ) {
1492
if(volsetid_size > 128) {
1493
volsetid_size = 128;
1495
memcpy(volsetid, &buffer[190], volsetid_size);
1497
free( buffer_base );
1502
int DVDUDFVolumeInfo( dvd_reader_t *dvd,
1503
char *volid, unsigned int volid_size,
1504
unsigned char *volsetid, unsigned int volsetid_size )
1507
/* Check arguments. */
1511
if( dvd->dev == NULL ) {
1512
/* No block access, so no UDF VolumeSet Identifier */
1516
if( (volid != NULL) && (volid_size > 0) ) {
1517
ret = UDFGetVolumeIdentifier(dvd, volid, volid_size);
1522
if( (volsetid != NULL) && (volsetid_size > 0) ) {
1523
ret = UDFGetVolumeSetIdentifier(dvd, volsetid, volsetid_size);