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 */
39
#ifndef HAVE_GETTIMEOFDAY
40
/* replacement gettimeofday implementation */
41
#include <sys/timeb.h>
42
static inline int _private_gettimeofday( struct timeval *tv, void *tz )
47
tv->tv_usec = t.millitm * 1000;
50
#define gettimeofday(TV, TZ) _private_gettimeofday((TV), (TZ))
52
#include <io.h> /* read() */
53
#define lseek64 _lseeki64
56
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__bsdi__) || defined(__APPLE__)
61
#include <sys/mnttab.h>
62
#elif defined(__APPLE__)
63
#include <sys/param.h>
64
#include <sys/ucred.h>
65
#include <sys/mount.h>
66
#elif defined(SYS_BSD)
68
#elif defined(__linux__)
73
#include "dvdread/dvd_udf.h"
74
#include "dvd_input.h"
75
#include "dvdread/dvd_reader.h"
78
#define DEFAULT_UDF_CACHE_LEVEL 1
81
/* Basic information. */
84
/* Hack for keeping track of the css status.
85
* 0: no css, 1: perhaps (need init of keys), 2: have done init */
87
int css_title; /* Last title that we have called dvdinpute_title for. */
89
/* Information required for an image file. */
92
/* Information required for a directory path drive. */
95
/* Filesystem cache */
96
int udfcache_level; /* 0 - turned off, 1 - on */
103
/* Basic information. */
106
/* Hack for selecting the right css title. */
109
/* Information required for an image file. */
113
/* Information required for a directory path drive. */
114
size_t title_sizes[ TITLES_MAX ];
115
dvd_input_t title_devs[ TITLES_MAX ];
117
/* Calculated at open-time, size in blocks. */
121
int UDFReadBlocksRaw( dvd_reader_t *device, uint32_t lb_number,
122
size_t block_count, unsigned char *data,
126
* Set the level of caching on udf
127
* level = 0 (no caching)
128
* level = 1 (caching filesystem info)
130
int DVDUDFCacheLevel(dvd_reader_t *device, int level)
132
struct dvd_reader_s *dev = (struct dvd_reader_s *)device;
136
} else if(level < 0) {
137
return dev->udfcache_level;
140
dev->udfcache_level = level;
145
void *GetUDFCacheHandle(dvd_reader_t *device)
147
struct dvd_reader_s *dev = (struct dvd_reader_s *)device;
149
return dev->udfcache;
152
void SetUDFCacheHandle(dvd_reader_t *device, void *cache)
154
struct dvd_reader_s *dev = (struct dvd_reader_s *)device;
156
dev->udfcache = cache;
161
/* Loop over all titles and call dvdcss_title to crack the keys. */
162
static int initAllCSSKeys( dvd_reader_t *dvd )
164
struct timeval all_s, all_e;
165
struct timeval t_s, t_e;
166
char filename[ MAX_UDF_FILE_NAME_LEN ];
170
char *nokeys_str = getenv("DVDREAD_NOKEYS");
171
if(nokeys_str != NULL)
174
fprintf( stderr, "\n" );
175
fprintf( stderr, "libdvdread: Attempting to retrieve all CSS keys\n" );
176
fprintf( stderr, "libdvdread: This can take a _long_ time, "
177
"please be patient\n\n" );
178
gettimeofday(&all_s, NULL);
180
for( title = 0; title < 100; title++ ) {
181
gettimeofday( &t_s, NULL );
183
sprintf( filename, "/VIDEO_TS/VIDEO_TS.VOB" );
185
sprintf( filename, "/VIDEO_TS/VTS_%02d_%d.VOB", title, 0 );
187
start = UDFFindFile( dvd, filename, &len );
188
if( start != 0 && len != 0 ) {
189
/* Perform CSS key cracking for this title. */
190
fprintf( stderr, "libdvdread: Get key for %s at 0x%08x\n",
192
if( dvdinput_title( dvd->dev, (int)start ) < 0 ) {
193
fprintf( stderr, "libdvdread: Error cracking CSS key for %s (0x%08x)\n", filename, start);
195
gettimeofday( &t_e, NULL );
196
fprintf( stderr, "libdvdread: Elapsed time %ld\n",
197
(long int) t_e.tv_sec - t_s.tv_sec );
200
if( title == 0 ) continue;
202
gettimeofday( &t_s, NULL );
203
sprintf( filename, "/VIDEO_TS/VTS_%02d_%d.VOB", title, 1 );
204
start = UDFFindFile( dvd, filename, &len );
205
if( start == 0 || len == 0 ) break;
207
/* Perform CSS key cracking for this title. */
208
fprintf( stderr, "libdvdread: Get key for %s at 0x%08x\n",
210
if( dvdinput_title( dvd->dev, (int)start ) < 0 ) {
211
fprintf( stderr, "libdvdread: Error cracking CSS key for %s (0x%08x)!!\n", filename, start);
213
gettimeofday( &t_e, NULL );
214
fprintf( stderr, "libdvdread: Elapsed time %ld\n",
215
(long int) t_e.tv_sec - t_s.tv_sec );
219
fprintf( stderr, "libdvdread: Found %d VTS's\n", title );
220
gettimeofday(&all_e, NULL);
221
fprintf( stderr, "libdvdread: Elapsed time %ld\n",
222
(long int) all_e.tv_sec - all_s.tv_sec );
230
* Open a DVD image or block device file.
232
static dvd_reader_t *DVDOpenImageFile( const char *location, int have_css )
237
dev = dvdinput_open( location );
239
fprintf( stderr, "libdvdread: Can't open %s for reading\n", location );
243
dvd = (dvd_reader_t *) malloc( sizeof( dvd_reader_t ) );
248
dvd->isImageFile = 1;
250
dvd->path_root = NULL;
252
dvd->udfcache_level = DEFAULT_UDF_CACHE_LEVEL;
253
dvd->udfcache = NULL;
256
/* Only if DVDCSS_METHOD = title, a bit if it's disc or if
257
* DVDCSS_METHOD = key but region mismatch. Unfortunately we
258
* don't have that information. */
260
dvd->css_state = 1; /* Need key init. */
267
static dvd_reader_t *DVDOpenPath( const char *path_root )
271
dvd = (dvd_reader_t *) malloc( sizeof( dvd_reader_t ) );
272
if( !dvd ) return NULL;
273
dvd->isImageFile = 0;
275
dvd->path_root = strdup( path_root );
276
if(!dvd->path_root) {
280
dvd->udfcache_level = DEFAULT_UDF_CACHE_LEVEL;
281
dvd->udfcache = NULL;
283
dvd->css_state = 0; /* Only used in the UDF path */
284
dvd->css_title = 0; /* Only matters in the UDF path */
290
/* /dev/rdsk/c0t6d0s0 (link to /devices/...)
291
/vol/dev/rdsk/c0t6d0/??
293
static char *sun_block2char( const char *path )
297
/* Must contain "/dsk/" */
298
if( !strstr( path, "/dsk/" ) ) return (char *) strdup( path );
300
/* Replace "/dsk/" with "/rdsk/" */
301
new_path = malloc( strlen(path) + 2 );
302
strcpy( new_path, path );
303
strcpy( strstr( new_path, "/dsk/" ), "" );
304
strcat( new_path, "/rdsk/" );
305
strcat( new_path, strstr( path, "/dsk/" ) + strlen( "/dsk/" ) );
312
/* FreeBSD /dev/(r)(a)cd0c (a is for atapi), recommended to _not_ use r
313
update: FreeBSD and DragonFly no longer uses the prefix so don't add it.
314
OpenBSD /dev/rcd0c, it needs to be the raw device
315
NetBSD /dev/rcd0[d|c|..] d for x86, c (for non x86), perhaps others
316
Darwin /dev/rdisk0, it needs to be the raw device
317
BSD/OS /dev/sr0c (if not mounted) or /dev/rsr0c ('c' any letter will do)
318
returns a string allocated with strdup. It should be freed when no longer
320
static char *bsd_block2char( const char *path )
322
#if defined(__FreeBSD__) || defined(__DragonFly__)
323
return (char *) strdup( path );
327
/* If it doesn't start with "/dev/" or does start with "/dev/r" exit */
328
if( strncmp( path, "/dev/", 5 ) || !strncmp( path, "/dev/r", 6 ) )
329
return (char *) strdup( path );
331
/* Replace "/dev/" with "/dev/r" */
332
new_path = malloc( strlen(path) + 2 );
333
strcpy( new_path, "/dev/r" );
334
strcat( new_path, path + strlen( "/dev/" ) );
337
#endif /* __FreeBSD__ || __DragonFly__ */
342
dvd_reader_t *DVDOpen( const char *ppath )
344
struct stat fileinfo;
345
int ret, have_css, retval, cdir = -1;
346
dvd_reader_t *ret_val = NULL;
347
char *dev_name = NULL;
348
char *path = NULL, *new_path = NULL, *path_copy = NULL;
350
#if defined(_WIN32) || defined(__OS2__)
357
path = strdup(ppath);
361
/* Try to open libdvdcss or fall back to standard functions */
362
have_css = dvdinput_setup();
364
#if defined(_WIN32) || defined(__OS2__)
365
/* Strip off the trailing \ if it is not a drive */
368
(path[len - 1] == '\\') &&
369
(path[len - 2] != ':'))
375
ret = stat( path, &fileinfo );
379
/* maybe "host:port" url? try opening it with acCeSS library */
380
if( strchr(path,':') ) {
381
ret_val = DVDOpenImageFile( path, have_css );
386
/* If we can't stat the file, give up */
387
fprintf( stderr, "libdvdread: Can't stat %s\n", path );
392
/* First check if this is a block/char device or a file*/
393
if( S_ISBLK( fileinfo.st_mode ) ||
394
S_ISCHR( fileinfo.st_mode ) ||
395
S_ISREG( fileinfo.st_mode ) ) {
398
* Block devices and regular files are assumed to be DVD-Video images.
400
dvd_reader_t *dvd = NULL;
402
dev_name = sun_block2char( path );
403
#elif defined(SYS_BSD)
404
dev_name = bsd_block2char( path );
406
dev_name = strdup( path );
408
dvd = DVDOpenImageFile( dev_name, have_css );
412
} else if( S_ISDIR( fileinfo.st_mode ) ) {
413
dvd_reader_t *auth_drive = 0;
416
#elif defined(__sun) || defined(__linux__)
420
/* XXX: We should scream real loud here. */
421
if( !(path_copy = strdup( path ) ) )
424
#ifndef WIN32 /* don't have fchdir, and getcwd( NULL, ... ) is strange */
425
/* Also WIN32 does not have symlinks, so we don't need this bit of code. */
427
/* Resolve any symlinks and get the absolute dir name. */
429
if( ( cdir = open( ".", O_RDONLY ) ) >= 0 ) {
430
if( chdir( path_copy ) == -1 ) {
434
new_path = get_current_dir_name();
435
if(new_path == NULL) {
439
new_path = malloc(PATH_MAX+1);
443
if( getcwd( new_path, PATH_MAX ) == NULL ) {
447
retval = fchdir( cdir );
453
path_copy = new_path;
460
* If we're being asked to open a directory, check if that directory
461
* is the mount point for a DVD-ROM which we can use instead.
464
if( strlen( path_copy ) > 1 ) {
465
if( path_copy[ strlen( path_copy ) - 1 ] == '/' ) {
466
path_copy[ strlen( path_copy ) - 1 ] = '\0';
470
#if defined(_WIN32) || defined(__OS2__)
471
if(strlen(path_copy) > TITLES_MAX) {
472
if(!strcasecmp(&(path_copy[strlen( path_copy ) - TITLES_MAX]),
474
path_copy[strlen(path_copy) - (TITLES_MAX-1)] = '\0';
477
if( strlen( path_copy ) > TITLES_MAX ) {
478
if( !strcasecmp( &(path_copy[ strlen( path_copy ) - TITLES_MAX ]),
480
path_copy[ strlen( path_copy ) - TITLES_MAX ] = '\0';
484
if(path_copy[0] == '\0') {
489
#if defined(__APPLE__)
490
struct statfs s[128];
491
int r = getfsstat(NULL, 0, MNT_NOWAIT);
495
r = getfsstat(s, r * sizeof(s[0]), MNT_NOWAIT);
497
for (i=0; i<r; i++) {
498
if (!strcmp(path_copy, s[i].f_mntonname)) {
499
dev_name = bsd_block2char(s[i].f_mntfromname);
501
"libdvdread: Attempting to use device %s"
502
" mounted on %s for CSS authentication\n",
505
auth_drive = DVDOpenImageFile( dev_name, have_css );
510
#elif defined(SYS_BSD)
511
if( ( fe = getfsfile( path_copy ) ) ) {
512
dev_name = bsd_block2char( fe->fs_spec );
514
"libdvdread: Attempting to use device %s"
515
" mounted on %s for CSS authentication\n",
518
auth_drive = DVDOpenImageFile( dev_name, have_css );
521
mntfile = fopen( MNTTAB, "r" );
526
while( ( res = getmntent( mntfile, &mp ) ) != -1 ) {
527
if( res == 0 && !strcmp( mp.mnt_mountp, path_copy ) ) {
528
dev_name = sun_block2char( mp.mnt_special );
530
"libdvdread: Attempting to use device %s"
531
" mounted on %s for CSS authentication\n",
534
auth_drive = DVDOpenImageFile( dev_name, have_css );
540
#elif defined(__linux__)
541
mntfile = fopen( _PATH_MOUNTED, "r" );
545
while( ( me = getmntent( mntfile ) ) ) {
546
if( !strcmp( me->mnt_dir, path_copy ) ) {
548
"libdvdread: Attempting to use device %s"
549
" mounted on %s for CSS authentication\n",
552
auth_drive = DVDOpenImageFile( me->mnt_fsname, have_css );
553
dev_name = strdup(me->mnt_fsname);
559
#elif defined(_WIN32) || 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])))
566
auth_drive = DVDOpenImageFile( path, have_css );
569
#if !defined(_WIN32) && !defined(__OS2__)
571
fprintf( stderr, "libdvdread: Couldn't find device name.\n" );
572
} else if( !auth_drive ) {
573
fprintf( stderr, "libdvdread: Device %s inaccessible, "
574
"CSS authentication not available.\n", dev_name );
578
fprintf( stderr, "libdvdread: Device %s inaccessible, "
579
"CSS authentication not available.\n", path );
589
* If we've opened a drive, just use that.
596
* Otherwise, we now try to open the directory tree instead.
598
ret_val = DVDOpenPath( path );
604
/* If it's none of the above, screw it. */
605
fprintf( stderr, "libdvdread: Could not open %s\n", path );
608
if ( path_copy != NULL )
612
if ( new_path != NULL )
617
void DVDClose( dvd_reader_t *dvd )
620
if( dvd->dev ) dvdinput_close( dvd->dev );
621
if( dvd->path_root ) free( dvd->path_root );
622
if( dvd->udfcache ) FreeUDFCache( dvd->udfcache );
628
* Open an unencrypted file on a DVD image file.
630
static dvd_file_t *DVDOpenFileUDF( dvd_reader_t *dvd, char *filename )
633
dvd_file_t *dvd_file;
635
start = UDFFindFile( dvd, filename, &len );
637
fprintf( stderr, "libdvdnav:DVDOpenFileUDF:UDFFindFile %s failed\n", filename );
641
dvd_file = (dvd_file_t *) malloc( sizeof( dvd_file_t ) );
643
fprintf( stderr, "libdvdnav:DVDOpenFileUDF:malloc failed\n" );
647
dvd_file->lb_start = start;
648
dvd_file->seek_pos = 0;
649
memset( dvd_file->title_sizes, 0, sizeof( dvd_file->title_sizes ) );
650
memset( dvd_file->title_devs, 0, sizeof( dvd_file->title_devs ) );
651
dvd_file->filesize = len / DVD_VIDEO_LB_LEN;
657
* Searches for <file> in directory <path>, ignoring case.
658
* Returns 0 and full filename in <filename>.
659
* or -1 on file not found.
660
* or -2 on path not found.
662
static int findDirFile( const char *path, const char *file, char **filename )
668
dir = opendir( path );
669
if( !dir ) return -2;
671
while( ( ent = readdir( dir ) ) != NULL ) {
672
if( !strcasecmp( ent->d_name, file ) ) {
673
*filename = malloc( strlen( path ) + 1 + strlen( ent->d_name ) + 1 );
674
if( *filename == NULL ) {
678
sprintf( *filename, "%s%s%s", path,
679
( ( path[ strlen( path ) - 1 ] == '/' ) ? "" : "/" ),
689
static int findDVDFile( dvd_reader_t *dvd, const char *file, char **filename )
691
char *video_path = NULL;
692
const char *nodirfile;
695
/* Strip off the directory for our search */
696
if( !strncasecmp( "/VIDEO_TS/", file, 10 ) ) {
697
nodirfile = &(file[ 10 ]);
702
ret = findDirFile( dvd->path_root, nodirfile, filename );
704
/* Try also with adding the path, just in case. */
705
video_path = malloc( strlen( dvd->path_root ) + 10 + 1 );
706
if( video_path == NULL ) return 0;
707
sprintf( video_path, "%s/VIDEO_TS/", dvd->path_root );
708
ret = findDirFile( video_path, nodirfile, filename );
710
/* Try with the path, but in lower case. */
711
sprintf( video_path, "%s/video_ts/", dvd->path_root );
712
ret = findDirFile( video_path, nodirfile, filename );
725
* Open an unencrypted file from a DVD directory tree.
727
static dvd_file_t *DVDOpenFilePath( dvd_reader_t *dvd, char *filename )
729
char *full_path = NULL;
730
dvd_file_t *dvd_file;
731
struct stat fileinfo;
734
/* Get the full path of the file. */
735
if( !findDVDFile( dvd, filename, &full_path ) ) {
736
fprintf( stderr, "libdvdnav:DVDOpenFilePath:findDVDFile %s failed\n", filename );
741
dev = dvdinput_open( full_path );
743
fprintf( stderr, "libdvdnav:DVDOpenFilePath:dvdinput_open %s failed\n", full_path );
748
dvd_file = (dvd_file_t *) malloc( sizeof( dvd_file_t ) );
750
fprintf( stderr, "libdvdnav:DVDOpenFilePath:dvd_file malloc failed\n" );
756
dvd_file->lb_start = 0;
757
dvd_file->seek_pos = 0;
758
memset( dvd_file->title_sizes, 0, sizeof( dvd_file->title_sizes ) );
759
memset( dvd_file->title_devs, 0, sizeof( dvd_file->title_devs ) );
760
dvd_file->filesize = 0;
762
if( stat( full_path, &fileinfo ) < 0 ) {
763
fprintf( stderr, "libdvdread: Can't stat() %s.\n", filename );
766
dvdinput_close( dev );
769
dvd_file->title_sizes[ 0 ] = fileinfo.st_size / DVD_VIDEO_LB_LEN;
770
dvd_file->title_devs[ 0 ] = dev;
771
dvd_file->filesize = dvd_file->title_sizes[ 0 ];
777
static dvd_file_t *DVDOpenVOBUDF( dvd_reader_t *dvd, int title, int menu )
779
char filename[ MAX_UDF_FILE_NAME_LEN ];
781
dvd_file_t *dvd_file;
784
sprintf( filename, "/VIDEO_TS/VIDEO_TS.VOB" );
786
sprintf( filename, "/VIDEO_TS/VTS_%02d_%d.VOB", title, menu ? 0 : 1 );
788
start = UDFFindFile( dvd, filename, &len );
789
if( start == 0 ) return NULL;
791
dvd_file = (dvd_file_t *) malloc( sizeof( dvd_file_t ) );
792
if( !dvd_file ) return NULL;
794
/*Hack*/ dvd_file->css_title = title << 1 | menu;
795
dvd_file->lb_start = start;
796
dvd_file->seek_pos = 0;
797
memset( dvd_file->title_sizes, 0, sizeof( dvd_file->title_sizes ) );
798
memset( dvd_file->title_devs, 0, sizeof( dvd_file->title_devs ) );
799
dvd_file->filesize = len / DVD_VIDEO_LB_LEN;
801
/* Calculate the complete file size for every file in the VOBS */
805
for( cur = 2; cur < 10; cur++ ) {
806
sprintf( filename, "/VIDEO_TS/VTS_%02d_%d.VOB", title, cur );
807
if( !UDFFindFile( dvd, filename, &len ) ) break;
808
dvd_file->filesize += len / DVD_VIDEO_LB_LEN;
812
if( dvd->css_state == 1 /* Need key init */ ) {
813
initAllCSSKeys( dvd );
817
if( dvdinput_title( dvd_file->dvd->dev, (int)start ) < 0 ) {
818
fprintf( stderr, "libdvdread: Error cracking CSS key for %s\n",
826
static dvd_file_t *DVDOpenVOBPath( dvd_reader_t *dvd, int title, int menu )
828
char filename[ MAX_UDF_FILE_NAME_LEN ];
829
char *full_path = NULL;
830
struct stat fileinfo;
831
dvd_file_t *dvd_file;
834
dvd_file = (dvd_file_t *) malloc( sizeof( dvd_file_t ) );
835
if( !dvd_file ) return NULL;
837
/*Hack*/ dvd_file->css_title = title << 1 | menu;
838
dvd_file->lb_start = 0;
839
dvd_file->seek_pos = 0;
840
memset( dvd_file->title_sizes, 0, sizeof( dvd_file->title_sizes ) );
841
memset( dvd_file->title_devs, 0, sizeof( dvd_file->title_devs ) );
842
dvd_file->filesize = 0;
848
sprintf( filename, "VIDEO_TS.VOB" );
850
sprintf( filename, "VTS_%02i_0.VOB", title );
852
if( !findDVDFile( dvd, filename, &full_path ) ) {
858
dev = dvdinput_open( full_path );
865
if( stat( full_path, &fileinfo ) < 0 ) {
866
fprintf( stderr, "libdvdread: Can't stat() %s.\n", filename );
872
dvd_file->title_sizes[ 0 ] = fileinfo.st_size / DVD_VIDEO_LB_LEN;
873
dvd_file->title_devs[ 0 ] = dev;
874
dvdinput_title( dvd_file->title_devs[0], 0);
875
dvd_file->filesize = dvd_file->title_sizes[ 0 ];
878
for( i = 0; i < TITLES_MAX; ++i ) {
880
sprintf( filename, "VTS_%02i_%i.VOB", title, i + 1 );
881
if( !findDVDFile( dvd, filename, &full_path ) ) {
885
if( stat( full_path, &fileinfo ) < 0 ) {
886
fprintf( stderr, "libdvdread: Can't stat() %s.\n", filename );
890
dvd_file->title_sizes[ i ] = fileinfo.st_size / DVD_VIDEO_LB_LEN;
891
dvd_file->title_devs[ i ] = dvdinput_open( full_path );
892
dvdinput_title( dvd_file->title_devs[ i ], 0 );
893
dvd_file->filesize += dvd_file->title_sizes[ i ];
895
if( !dvd_file->title_devs[ 0 ] ) {
905
dvd_file_t *DVDOpenFile( dvd_reader_t *dvd, int titlenum,
906
dvd_read_domain_t domain )
908
char filename[ MAX_UDF_FILE_NAME_LEN ];
910
/* Check arguments. */
911
if( dvd == NULL || titlenum < 0 )
915
case DVD_READ_INFO_FILE:
916
if( titlenum == 0 ) {
917
sprintf( filename, "/VIDEO_TS/VIDEO_TS.IFO" );
919
sprintf( filename, "/VIDEO_TS/VTS_%02i_0.IFO", titlenum );
922
case DVD_READ_INFO_BACKUP_FILE:
923
if( titlenum == 0 ) {
924
sprintf( filename, "/VIDEO_TS/VIDEO_TS.BUP" );
926
sprintf( filename, "/VIDEO_TS/VTS_%02i_0.BUP", titlenum );
929
case DVD_READ_MENU_VOBS:
930
if( dvd->isImageFile ) {
931
return DVDOpenVOBUDF( dvd, titlenum, 1 );
933
return DVDOpenVOBPath( dvd, titlenum, 1 );
936
case DVD_READ_TITLE_VOBS:
937
if( titlenum == 0 ) return 0;
938
if( dvd->isImageFile ) {
939
return DVDOpenVOBUDF( dvd, titlenum, 0 );
941
return DVDOpenVOBPath( dvd, titlenum, 0 );
945
fprintf( stderr, "libdvdread: Invalid domain for file open.\n" );
949
if( dvd->isImageFile ) {
950
return DVDOpenFileUDF( dvd, filename );
952
return DVDOpenFilePath( dvd, filename );
956
void DVDCloseFile( dvd_file_t *dvd_file )
961
if( !dvd_file->dvd->isImageFile ) {
962
for( i = 0; i < TITLES_MAX; ++i ) {
963
if( dvd_file->title_devs[ i ] ) {
964
dvdinput_close( dvd_file->title_devs[i] );
974
static int DVDFileStatVOBUDF( dvd_reader_t *dvd, int title,
975
int menu, dvd_stat_t *statbuf )
977
char filename[ MAX_UDF_FILE_NAME_LEN ];
980
off_t parts_size[ 9 ];
985
sprintf( filename, "/VIDEO_TS/VIDEO_TS.VOB" );
987
sprintf( filename, "/VIDEO_TS/VTS_%02d_%d.VOB", title, menu ? 0 : 1 );
989
if( !UDFFindFile( dvd, filename, &size ) )
994
parts_size[ 0 ] = size;
999
for( cur = 2; cur < 10; cur++ ) {
1000
sprintf( filename, "/VIDEO_TS/VTS_%02d_%d.VOB", title, cur );
1001
if( !UDFFindFile( dvd, filename, &size ) )
1004
parts_size[ nr_parts ] = size;
1010
statbuf->size = tot_size;
1011
statbuf->nr_parts = nr_parts;
1012
for( n = 0; n < nr_parts; n++ )
1013
statbuf->parts_size[ n ] = parts_size[ n ];
1019
static int DVDFileStatVOBPath( dvd_reader_t *dvd, int title,
1020
int menu, dvd_stat_t *statbuf )
1022
char filename[ MAX_UDF_FILE_NAME_LEN ];
1023
char *full_path = NULL;
1024
struct stat fileinfo;
1026
off_t parts_size[ 9 ];
1031
sprintf( filename, "VIDEO_TS.VOB" );
1033
sprintf( filename, "VTS_%02d_%d.VOB", title, menu ? 0 : 1 );
1035
if( !findDVDFile( dvd, filename, &full_path ) ) {
1040
if( stat( full_path, &fileinfo ) < 0 ) {
1041
fprintf( stderr, "libdvdread: Can't stat() %s.\n", filename );
1046
tot_size = fileinfo.st_size;
1048
parts_size[ 0 ] = fileinfo.st_size;
1052
for( cur = 2; cur < 10; cur++ ) {
1053
sprintf( filename, "VTS_%02d_%d.VOB", title, cur );
1054
if( !findDVDFile( dvd, filename, &full_path ) )
1057
if( stat( full_path, &fileinfo ) < 0 ) {
1058
fprintf( stderr, "libdvdread: Can't stat() %s.\n", filename );
1062
parts_size[ nr_parts ] = fileinfo.st_size;
1063
tot_size += parts_size[ nr_parts ];
1068
statbuf->size = tot_size;
1069
statbuf->nr_parts = nr_parts;
1070
for( n = 0; n < nr_parts; n++ )
1071
statbuf->parts_size[ n ] = parts_size[ n ];
1078
int DVDFileStat( dvd_reader_t *dvd, int titlenum,
1079
dvd_read_domain_t domain, dvd_stat_t *statbuf )
1081
char filename[ MAX_UDF_FILE_NAME_LEN ];
1082
char *full_path = NULL;
1083
struct stat fileinfo;
1086
/* Check arguments. */
1087
if( dvd == NULL || titlenum < 0 ) {
1093
case DVD_READ_INFO_FILE:
1095
sprintf( filename, "/VIDEO_TS/VIDEO_TS.IFO" );
1097
sprintf( filename, "/VIDEO_TS/VTS_%02i_0.IFO", titlenum );
1100
case DVD_READ_INFO_BACKUP_FILE:
1102
sprintf( filename, "/VIDEO_TS/VIDEO_TS.BUP" );
1104
sprintf( filename, "/VIDEO_TS/VTS_%02i_0.BUP", titlenum );
1107
case DVD_READ_MENU_VOBS:
1108
if( dvd->isImageFile )
1109
return DVDFileStatVOBUDF( dvd, titlenum, 1, statbuf );
1111
return DVDFileStatVOBPath( dvd, titlenum, 1, statbuf );
1114
case DVD_READ_TITLE_VOBS:
1118
if( dvd->isImageFile )
1119
return DVDFileStatVOBUDF( dvd, titlenum, 0, statbuf );
1121
return DVDFileStatVOBPath( dvd, titlenum, 0, statbuf );
1125
fprintf( stderr, "libdvdread: Invalid domain for file stat.\n" );
1130
if( dvd->isImageFile ) {
1131
if( UDFFindFile( dvd, filename, &size ) ) {
1132
statbuf->size = size;
1133
statbuf->nr_parts = 1;
1134
statbuf->parts_size[ 0 ] = size;
1138
if( findDVDFile( dvd, filename, &full_path ) ) {
1139
if( stat( full_path, &fileinfo ) < 0 )
1140
fprintf( stderr, "libdvdread: Can't stat() %s.\n", filename );
1142
statbuf->size = fileinfo.st_size;
1143
statbuf->nr_parts = 1;
1144
statbuf->parts_size[ 0 ] = statbuf->size;
1154
/* Internal, but used from dvd_udf.c */
1155
int UDFReadBlocksRaw( dvd_reader_t *device, uint32_t lb_number,
1156
size_t block_count, unsigned char *data,
1161
if( !device->dev ) {
1162
fprintf( stderr, "libdvdread: Fatal error in block read.\n" );
1166
ret = dvdinput_seek( device->dev, (int) lb_number );
1167
if( ret != (int) lb_number ) {
1168
fprintf( stderr, "libdvdread: Can't seek to block %u\n", lb_number );
1172
ret = dvdinput_read( device->dev, (char *) data,
1173
(int) block_count, encrypted );
1177
/* This is using a single input and starting from 'dvd_file->lb_start' offset.
1179
* Reads 'block_count' blocks from 'dvd_file' at block offset 'offset'
1180
* into the buffer located at 'data' and if 'encrypted' is set
1181
* descramble the data if it's encrypted. Returning either an
1182
* negative error or the number of blocks read. */
1183
static int DVDReadBlocksUDF( dvd_file_t *dvd_file, uint32_t offset,
1184
size_t block_count, unsigned char *data,
1187
return UDFReadBlocksRaw( dvd_file->dvd, dvd_file->lb_start + offset,
1188
block_count, data, encrypted );
1191
/* This is using possibly several inputs and starting from an offset of '0'.
1193
* Reads 'block_count' blocks from 'dvd_file' at block offset 'offset'
1194
* into the buffer located at 'data' and if 'encrypted' is set
1195
* descramble the data if it's encrypted. Returning either an
1196
* negative error or the number of blocks read. */
1197
static int DVDReadBlocksPath( dvd_file_t *dvd_file, unsigned int offset,
1198
size_t block_count, unsigned char *data,
1206
for( i = 0; i < TITLES_MAX; ++i ) {
1207
if( !dvd_file->title_sizes[ i ] ) return 0; /* Past end of file */
1209
if( offset < dvd_file->title_sizes[ i ] ) {
1210
if( ( offset + block_count ) <= dvd_file->title_sizes[ i ] ) {
1211
off = dvdinput_seek( dvd_file->title_devs[ i ], (int)offset );
1212
if( off < 0 || off != (int)offset ) {
1213
fprintf( stderr, "libdvdread: Can't seek to block %d\n",
1215
return off < 0 ? off : 0;
1217
ret = dvdinput_read( dvd_file->title_devs[ i ], data,
1218
(int)block_count, encrypted );
1221
size_t part1_size = dvd_file->title_sizes[ i ] - offset;
1222
/* FIXME: Really needs to be a while loop.
1223
* (This is only true if you try and read >1GB at a time) */
1226
off = dvdinput_seek( dvd_file->title_devs[ i ], (int)offset );
1227
if( off < 0 || off != (int)offset ) {
1228
fprintf( stderr, "libdvdread: Can't seek to block %d\n",
1230
return off < 0 ? off : 0;
1232
ret = dvdinput_read( dvd_file->title_devs[ i ], data,
1233
(int)part1_size, encrypted );
1234
if( ret < 0 ) return ret;
1235
/* FIXME: This is wrong if i is the last file in the set.
1236
* also error from this read will not show in ret. */
1238
/* Does the next part exist? If not then return now. */
1239
if( i + 1 >= TITLES_MAX || !dvd_file->title_devs[ i + 1 ] )
1243
off = dvdinput_seek( dvd_file->title_devs[ i + 1 ], 0 );
1244
if( off < 0 || off != 0 ) {
1245
fprintf( stderr, "libdvdread: Can't seek to block %d\n",
1247
return off < 0 ? off : 0;
1249
ret2 = dvdinput_read( dvd_file->title_devs[ i + 1 ],
1251
* (int64_t)DVD_VIDEO_LB_LEN ),
1252
(int)(block_count - part1_size),
1254
if( ret2 < 0 ) return ret2;
1258
offset -= dvd_file->title_sizes[ i ];
1265
/* This is broken reading more than 2Gb at a time is ssize_t is 32-bit. */
1266
ssize_t DVDReadBlocks( dvd_file_t *dvd_file, int offset,
1267
size_t block_count, unsigned char *data )
1271
/* Check arguments. */
1272
if( dvd_file == NULL || offset < 0 || data == NULL )
1275
/* Hack, and it will still fail for multiple opens in a threaded app ! */
1276
if( dvd_file->dvd->css_title != dvd_file->css_title ) {
1277
dvd_file->dvd->css_title = dvd_file->css_title;
1278
if( dvd_file->dvd->isImageFile ) {
1279
dvdinput_title( dvd_file->dvd->dev, (int)dvd_file->lb_start );
1281
/* Here each vobu has it's own dvdcss handle, so no need to update
1283
dvdinput_title( dvd_file->title_devs[ 0 ], (int)dvd_file->lb_start );
1287
if( dvd_file->dvd->isImageFile ) {
1288
ret = DVDReadBlocksUDF( dvd_file, (uint32_t)offset,
1289
block_count, data, DVDINPUT_READ_DECRYPT );
1291
ret = DVDReadBlocksPath( dvd_file, (unsigned int)offset,
1292
block_count, data, DVDINPUT_READ_DECRYPT );
1295
return (ssize_t)ret;
1298
int32_t DVDFileSeek( dvd_file_t *dvd_file, int32_t offset )
1300
/* Check arguments. */
1301
if( dvd_file == NULL || offset < 0 )
1304
if( offset > dvd_file->filesize * DVD_VIDEO_LB_LEN ) {
1307
dvd_file->seek_pos = (uint32_t) offset;
1311
int DVDFileSeekForce(dvd_file_t *dvd_file, int offset, int force_size)
1313
/* Check arguments. */
1314
if( dvd_file == NULL || offset <= 0 )
1317
if( dvd_file->dvd->isImageFile ) {
1318
if( force_size < 0 )
1319
force_size = (offset - 1) / DVD_VIDEO_LB_LEN + 1;
1320
if( dvd_file->filesize < force_size ) {
1321
dvd_file->filesize = force_size;
1322
fprintf(stderr, "libdvdread: Ignored size of file indicated in UDF.\n");
1326
if( offset > dvd_file->filesize * DVD_VIDEO_LB_LEN )
1329
dvd_file->seek_pos = (uint32_t) offset;
1333
ssize_t DVDReadBytes( dvd_file_t *dvd_file, void *data, size_t byte_size )
1335
unsigned char *secbuf_base, *secbuf;
1336
unsigned int numsec, seek_sector, seek_byte;
1339
/* Check arguments. */
1340
if( dvd_file == NULL || data == NULL )
1343
seek_sector = dvd_file->seek_pos / DVD_VIDEO_LB_LEN;
1344
seek_byte = dvd_file->seek_pos % DVD_VIDEO_LB_LEN;
1346
numsec = ( ( seek_byte + byte_size ) / DVD_VIDEO_LB_LEN ) +
1347
( ( ( seek_byte + byte_size ) % DVD_VIDEO_LB_LEN ) ? 1 : 0 );
1349
secbuf_base = (unsigned char *) malloc( numsec * DVD_VIDEO_LB_LEN + 2048 );
1350
secbuf = (unsigned char *)(((uintptr_t)secbuf_base & ~((uintptr_t)2047)) + 2048);
1351
if( !secbuf_base ) {
1352
fprintf( stderr, "libdvdread: Can't allocate memory "
1353
"for file read!\n" );
1357
if( dvd_file->dvd->isImageFile ) {
1358
ret = DVDReadBlocksUDF( dvd_file, (uint32_t) seek_sector,
1359
(size_t) numsec, secbuf, DVDINPUT_NOFLAGS );
1361
ret = DVDReadBlocksPath( dvd_file, seek_sector,
1362
(size_t) numsec, secbuf, DVDINPUT_NOFLAGS );
1365
if( ret != (int) numsec ) {
1366
free( secbuf_base );
1367
return ret < 0 ? ret : 0;
1370
memcpy( data, &(secbuf[ seek_byte ]), byte_size );
1371
free( secbuf_base );
1373
DVDFileSeekForce(dvd_file, dvd_file->seek_pos + byte_size, -1);
1377
ssize_t DVDFileSize( dvd_file_t *dvd_file )
1379
/* Check arguments. */
1380
if( dvd_file == NULL )
1383
return dvd_file->filesize;
1386
int DVDDiscID( dvd_reader_t *dvd, unsigned char *discid )
1390
int nr_of_files = 0;
1392
/* Check arguments. */
1393
if( dvd == NULL || discid == NULL )
1396
/* Go through the first 10 IFO:s, in order,
1397
* and md5sum them, i.e VIDEO_TS.IFO and VTS_0?_0.IFO */
1398
md5_init_ctx( &ctx );
1399
for( title = 0; title < 10; title++ ) {
1400
dvd_file_t *dvd_file = DVDOpenFile( dvd, title, DVD_READ_INFO_FILE );
1401
if( dvd_file != NULL ) {
1403
size_t file_size = dvd_file->filesize * DVD_VIDEO_LB_LEN;
1404
char *buffer_base = malloc( file_size + 2048 );
1405
char *buffer = (char *)(((uintptr_t)buffer_base & ~((uintptr_t)2047)) + 2048);
1407
if( buffer_base == NULL ) {
1408
DVDCloseFile( dvd_file );
1409
fprintf( stderr, "libdvdread: DVDDiscId, failed to "
1410
"allocate memory for file read!\n" );
1414
bytes_read = DVDReadBytes( dvd_file, buffer, file_size );
1415
if( bytes_read != file_size ) {
1416
fprintf( stderr, "libdvdread: DVDDiscId read returned %zd bytes"
1417
", wanted %zd\n", bytes_read, file_size );
1418
DVDCloseFile( dvd_file );
1419
free( buffer_base );
1423
md5_process_bytes( buffer, file_size, &ctx );
1425
DVDCloseFile( dvd_file );
1426
free( buffer_base );
1430
md5_finish_ctx( &ctx, discid );
1438
int DVDISOVolumeInfo( dvd_reader_t *dvd,
1439
char *volid, unsigned int volid_size,
1440
unsigned char *volsetid, unsigned int volsetid_size )
1442
unsigned char *buffer, *buffer_base;
1445
/* Check arguments. */
1449
if( dvd->dev == NULL ) {
1450
/* No block access, so no ISO... */
1454
buffer_base = malloc( DVD_VIDEO_LB_LEN + 2048 );
1455
buffer = (unsigned char *)(((uintptr_t)buffer_base & ~((uintptr_t)2047)) + 2048);
1457
if( buffer_base == NULL ) {
1458
fprintf( stderr, "libdvdread: DVDISOVolumeInfo, failed to "
1459
"allocate memory for file read!\n" );
1463
ret = UDFReadBlocksRaw( dvd, 16, 1, buffer, 0 );
1465
fprintf( stderr, "libdvdread: DVDISOVolumeInfo, failed to "
1466
"read ISO9660 Primary Volume Descriptor!\n" );
1467
free( buffer_base );
1471
if( (volid != NULL) && (volid_size > 0) ) {
1473
for(n = 0; n < 32; n++) {
1474
if(buffer[40+n] == 0x20) {
1479
if(volid_size > n+1) {
1483
memcpy(volid, &buffer[40], volid_size-1);
1484
volid[volid_size-1] = '\0';
1487
if( (volsetid != NULL) && (volsetid_size > 0) ) {
1488
if(volsetid_size > 128) {
1489
volsetid_size = 128;
1491
memcpy(volsetid, &buffer[190], volsetid_size);
1493
free( buffer_base );
1498
int DVDUDFVolumeInfo( dvd_reader_t *dvd,
1499
char *volid, unsigned int volid_size,
1500
unsigned char *volsetid, unsigned int volsetid_size )
1503
/* Check arguments. */
1507
if( dvd->dev == NULL ) {
1508
/* No block access, so no UDF VolumeSet Identifier */
1512
if( (volid != NULL) && (volid_size > 0) ) {
1513
ret = UDFGetVolumeIdentifier(dvd, volid, volid_size);
1518
if( (volsetid != NULL) && (volsetid_size > 0) ) {
1519
ret = UDFGetVolumeSetIdentifier(dvd, volsetid, volsetid_size);