00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "config.h"
00022
00023 #include <sys/types.h>
00024 #include <sys/stat.h>
00025 #include <sys/time.h>
00026 #include <sys/param.h>
00027 #include <fcntl.h>
00028 #include <stdlib.h>
00029 #include <stdio.h>
00030 #include <errno.h>
00031 #include <string.h>
00032 #include <unistd.h>
00033 #include <limits.h>
00034 #include <dirent.h>
00035
00036
00037 #ifdef WIN32
00038
00039 #include <sys/timeb.h>
00040 static inline int _private_gettimeofday( struct timeval *tv, void *tz )
00041 {
00042 struct timeb t;
00043 ftime( &t );
00044 tv->tv_sec = t.time;
00045 tv->tv_usec = t.millitm * 1000;
00046 return 0;
00047 }
00048 #define gettimeofday(TV, TZ) _private_gettimeofday((TV), (TZ))
00049 #include <io.h>
00050 #define lseek64 _lseeki64
00051 #endif
00052
00053 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__bsdi__)|| defined(__DARWIN__)
00054 #define SYS_BSD 1
00055 #endif
00056
00057 #if defined(__sun)
00058 #include <sys/mnttab.h>
00059 #elif defined(SYS_BSD)
00060 #include <fstab.h>
00061 #elif defined(__linux__)
00062 #include <mntent.h>
00063 #endif
00064
00065 #include "dvd_udf.h"
00066 #include "dvd_input.h"
00067 #include "dvd_reader.h"
00068 #include "md5.h"
00069
00070 #define DEFAULT_UDF_CACHE_LEVEL 1
00071
00072 struct dvd_reader_s {
00073
00074 int isImageFile;
00075
00076
00077
00078 int css_state;
00079 int css_title;
00080
00081
00082 dvd_input_t dev;
00083
00084
00085 char *path_root;
00086
00087
00088 int udfcache_level;
00089 void *udfcache;
00090 };
00091
00092 struct dvd_file_s {
00093
00094 dvd_reader_t *dvd;
00095
00096
00097 int css_title;
00098
00099
00100 uint32_t lb_start;
00101 uint32_t seek_pos;
00102
00103
00104 size_t title_sizes[ 9 ];
00105 dvd_input_t title_devs[ 9 ];
00106
00107
00108 ssize_t filesize;
00109 };
00110
00111 int UDFReadBlocksRaw( dvd_reader_t *device, uint32_t lb_number,
00112 size_t block_count, unsigned char *data,
00113 int encrypted );
00114
00120 int DVDUDFCacheLevel(dvd_reader_t *device, int level)
00121 {
00122 struct dvd_reader_s *dev = (struct dvd_reader_s *)device;
00123
00124 if(level > 0) {
00125 level = 1;
00126 } else if(level < 0) {
00127 return dev->udfcache_level;
00128 }
00129
00130 dev->udfcache_level = level;
00131
00132 return level;
00133 }
00134
00135 void *GetUDFCacheHandle(dvd_reader_t *device)
00136 {
00137 struct dvd_reader_s *dev = (struct dvd_reader_s *)device;
00138
00139 return dev->udfcache;
00140 }
00141
00142 void SetUDFCacheHandle(dvd_reader_t *device, void *cache)
00143 {
00144 struct dvd_reader_s *dev = (struct dvd_reader_s *)device;
00145
00146 dev->udfcache = cache;
00147 }
00148
00149
00150
00151
00152 static int initAllCSSKeys( dvd_reader_t *dvd )
00153 {
00154 struct timeval all_s, all_e;
00155 struct timeval t_s, t_e;
00156 char filename[ MAX_UDF_FILE_NAME_LEN ];
00157 uint32_t start, len;
00158 int title;
00159
00160 char *nokeys_str = getenv("DVDREAD_NOKEYS");
00161 if(nokeys_str != NULL)
00162 return 0;
00163
00164 fprintf( stderr, "\n" );
00165 fprintf( stderr, "libdvdread: Attempting to retrieve all CSS keys\n" );
00166 fprintf( stderr, "libdvdread: This can take a _long_ time, "
00167 "please be patient\n\n" );
00168
00169 gettimeofday(&all_s, NULL);
00170
00171 for( title = 0; title < 100; title++ ) {
00172 gettimeofday( &t_s, NULL );
00173 if( title == 0 ) {
00174 sprintf( filename, "/VIDEO_TS/VIDEO_TS.VOB" );
00175 } else {
00176 sprintf( filename, "/VIDEO_TS/VTS_%02d_%d.VOB", title, 0 );
00177 }
00178 start = UDFFindFile( dvd, filename, &len );
00179 if( start != 0 && len != 0 ) {
00180
00181 fprintf( stderr, "libdvdread: Get key for %s at 0x%08x\n",
00182 filename, start );
00183 if( dvdinput_title( dvd->dev, (int)start ) < 0 ) {
00184 fprintf( stderr, "libdvdread: Error cracking CSS key for %s (0x%08x)\n", filename, start);
00185 }
00186 gettimeofday( &t_e, NULL );
00187 fprintf( stderr, "libdvdread: Elapsed time %ld\n",
00188 (long int) t_e.tv_sec - t_s.tv_sec );
00189 }
00190
00191 if( title == 0 ) continue;
00192
00193 gettimeofday( &t_s, NULL );
00194 sprintf( filename, "/VIDEO_TS/VTS_%02d_%d.VOB", title, 1 );
00195 start = UDFFindFile( dvd, filename, &len );
00196 if( start == 0 || len == 0 ) break;
00197
00198
00199 fprintf( stderr, "libdvdread: Get key for %s at 0x%08x\n",
00200 filename, start );
00201 if( dvdinput_title( dvd->dev, (int)start ) < 0 ) {
00202 fprintf( stderr, "libdvdread: Error cracking CSS key for %s (0x%08x)!!\n", filename, start);
00203 }
00204 gettimeofday( &t_e, NULL );
00205 fprintf( stderr, "libdvdread: Elapsed time %ld\n",
00206 (long int) t_e.tv_sec - t_s.tv_sec );
00207 }
00208 title--;
00209
00210 fprintf( stderr, "libdvdread: Found %d VTS's\n", title );
00211 gettimeofday(&all_e, NULL);
00212 fprintf( stderr, "libdvdread: Elapsed time %ld\n",
00213 (long int) all_e.tv_sec - all_s.tv_sec );
00214
00215 return 0;
00216 }
00217
00218
00219
00223 static dvd_reader_t *DVDOpenImageFile( const char *location, int have_css )
00224 {
00225 dvd_reader_t *dvd;
00226 dvd_input_t dev;
00227
00228 dev = dvdinput_open( location );
00229 if( !dev ) {
00230 fprintf( stderr, "libdvdread: Can't open %s for reading\n", location );
00231 return 0;
00232 }
00233
00234 dvd = (dvd_reader_t *) malloc( sizeof( dvd_reader_t ) );
00235 if( !dvd ) return 0;
00236 dvd->isImageFile = 1;
00237 dvd->dev = dev;
00238 dvd->path_root = 0;
00239
00240 dvd->udfcache_level = DEFAULT_UDF_CACHE_LEVEL;
00241 dvd->udfcache = NULL;
00242
00243 if( have_css ) {
00244
00245
00246
00247
00248 dvd->css_state = 1;
00249 }
00250 dvd->css_title = 0;
00251
00252 return dvd;
00253 }
00254
00255 static dvd_reader_t *DVDOpenPath( const char *path_root )
00256 {
00257 dvd_reader_t *dvd;
00258
00259 dvd = (dvd_reader_t *) malloc( sizeof( dvd_reader_t ) );
00260 if( !dvd ) return 0;
00261 dvd->isImageFile = 0;
00262 dvd->dev = 0;
00263 dvd->path_root = strdup( path_root );
00264
00265 dvd->udfcache_level = DEFAULT_UDF_CACHE_LEVEL;
00266 dvd->udfcache = NULL;
00267
00268 dvd->css_state = 0;
00269 dvd->css_title = 0;
00270
00271 return dvd;
00272 }
00273
00274 #if defined(__sun)
00275
00276
00277
00278 static char *sun_block2char( const char *path )
00279 {
00280 char *new_path;
00281
00282
00283 if( !strstr( path, "/dsk/" ) ) return (char *) strdup( path );
00284
00285
00286 new_path = malloc( strlen(path) + 2 );
00287 strcpy( new_path, path );
00288 strcpy( strstr( new_path, "/dsk/" ), "" );
00289 strcat( new_path, "/rdsk/" );
00290 strcat( new_path, strstr( path, "/dsk/" ) + strlen( "/dsk/" ) );
00291
00292 return new_path;
00293 }
00294 #endif
00295
00296 #if defined(SYS_BSD)
00297
00298
00299
00300
00301
00302 static char *bsd_block2char( const char *path )
00303 {
00304 char *new_path;
00305
00306
00307 if( !strncmp( path, "/dev/", 5 ) || strncmp( path, "/dev/r", 6 ) )
00308 return (char *) strdup( path );
00309
00310
00311 new_path = malloc( strlen(path) + 2 );
00312 strcpy( new_path, "/dev/r" );
00313 strcat( new_path, path + strlen( "/dev/" ) );
00314
00315 return new_path;
00316 }
00317 #endif
00318
00319 dvd_reader_t *DVDOpen( const char *ppath )
00320 {
00321 struct stat fileinfo;
00322 int ret;
00323 int have_css;
00324 dvd_reader_t *ret_val = NULL;
00325 char *dev_name = 0;
00326 char *path;
00327
00328 #ifdef _MSC_VER
00329 int len;
00330 #endif
00331
00332 if( ppath == NULL )
00333 return 0;
00334
00335 path = strdup(ppath);
00336
00337
00338 have_css = dvdinput_setup();
00339
00340 #ifdef _MSC_VER
00341
00342 len = strlen(path);
00343 if ((len > 1) &&
00344 (path[len - 1] == '\\') &&
00345 (path[len - 2] != ':'))
00346 {
00347 path[len-1] = '\0';
00348 }
00349 #endif
00350
00351 ret = stat( path, &fileinfo );
00352
00353 if( ret < 0 ) {
00354
00355
00356 if( strchr(path,':') ) {
00357 ret_val = DVDOpenImageFile( path, have_css );
00358 free(path);
00359 return ret_val;
00360 }
00361
00362
00363 fprintf( stderr, "libdvdread: Can't stat %s\n", path );
00364 perror("");
00365 free(path);
00366 return 0;
00367 }
00368
00369
00370 if( S_ISBLK( fileinfo.st_mode ) ||
00371 S_ISCHR( fileinfo.st_mode ) ||
00372 S_ISREG( fileinfo.st_mode ) ) {
00373
00377 #if defined(__sun)
00378 ret_val = DVDOpenImageFile( sun_block2char( path ), have_css );
00379 #elif defined(SYS_BSD)
00380 ret_val = DVDOpenImageFile( bsd_block2char( path ), have_css );
00381 #else
00382 ret_val = DVDOpenImageFile( path, have_css );
00383 #endif
00384
00385 free(path);
00386 return ret_val;
00387
00388 } else if( S_ISDIR( fileinfo.st_mode ) ) {
00389 dvd_reader_t *auth_drive = 0;
00390 char *path_copy;
00391 #if defined(SYS_BSD)
00392 struct fstab* fe;
00393 #elif defined(__sun) || defined(__linux__)
00394 FILE *mntfile;
00395 #endif
00396
00397
00398 if( !(path_copy = strdup( path ) ) ) {
00399 free(path);
00400 return 0;
00401 }
00402
00403 #ifndef WIN32
00404
00405
00406
00407 {
00408 char *new_path;
00409 int cdir = open( ".", O_RDONLY );
00410
00411 if( cdir >= 0 ) {
00412 chdir( path_copy );
00413 new_path = getcwd( NULL, PATH_MAX );
00414 fchdir( cdir );
00415 close( cdir );
00416 if( new_path ) {
00417 free( path_copy );
00418 path_copy = new_path;
00419 }
00420 }
00421 }
00422 #endif
00423
00428 if( strlen( path_copy ) > 1 ) {
00429 if( path_copy[ strlen( path_copy ) - 1 ] == '/' )
00430 path_copy[ strlen( path_copy ) - 1 ] = '\0';
00431 }
00432
00433 if( strlen( path_copy ) > 9 ) {
00434 if( !strcasecmp( &(path_copy[ strlen( path_copy ) - 9 ]),
00435 "/video_ts" ) ) {
00436 path_copy[ strlen( path_copy ) - 9 ] = '\0';
00437 }
00438 }
00439
00440 #if defined(SYS_BSD)
00441 if( ( fe = getfsfile( path_copy ) ) ) {
00442 dev_name = bsd_block2char( fe->fs_spec );
00443 fprintf( stderr,
00444 "libdvdread: Attempting to use device %s"
00445 " mounted on %s for CSS authentication\n",
00446 dev_name,
00447 fe->fs_file );
00448 auth_drive = DVDOpenImageFile( dev_name, have_css );
00449 }
00450 #elif defined(__sun)
00451 mntfile = fopen( MNTTAB, "r" );
00452 if( mntfile ) {
00453 struct mnttab mp;
00454 int res;
00455
00456 while( ( res = getmntent( mntfile, &mp ) ) != -1 ) {
00457 if( res == 0 && !strcmp( mp.mnt_mountp, path_copy ) ) {
00458 dev_name = sun_block2char( mp.mnt_special );
00459 fprintf( stderr,
00460 "libdvdread: Attempting to use device %s"
00461 " mounted on %s for CSS authentication\n",
00462 dev_name,
00463 mp.mnt_mountp );
00464 auth_drive = DVDOpenImageFile( dev_name, have_css );
00465 break;
00466 }
00467 }
00468 fclose( mntfile );
00469 }
00470 #elif defined(__linux__)
00471 mntfile = fopen( MOUNTED, "r" );
00472 if( mntfile ) {
00473 struct mntent *me;
00474
00475 while( ( me = getmntent( mntfile ) ) ) {
00476 if( !strcmp( me->mnt_dir, path_copy ) ) {
00477 fprintf( stderr,
00478 "libdvdread: Attempting to use device %s"
00479 " mounted on %s for CSS authentication\n",
00480 me->mnt_fsname,
00481 me->mnt_dir );
00482 auth_drive = DVDOpenImageFile( me->mnt_fsname, have_css );
00483 dev_name = strdup(me->mnt_fsname);
00484 break;
00485 }
00486 }
00487 fclose( mntfile );
00488 }
00489 #elif defined(_MSC_VER)
00490 auth_drive = DVDOpenImageFile( path, have_css );
00491 #endif
00492
00493 #ifndef _MSC_VER
00494 if( !dev_name ) {
00495 fprintf( stderr, "libdvdread: Couldn't find device name.\n" );
00496 } else if( !auth_drive ) {
00497 fprintf( stderr, "libdvdread: Device %s inaccessible, "
00498 "CSS authentication not available.\n", dev_name );
00499 }
00500 #else
00501 if( !auth_drive ) {
00502 fprintf( stderr, "libdvdread: Device %s inaccessible, "
00503 "CSS authentication not available.\n", dev_name );
00504 }
00505 #endif
00506
00507 free( dev_name );
00508 free( path_copy );
00509
00513 if( auth_drive ) {
00514 free(path);
00515 return auth_drive;
00516 }
00517
00521 ret_val = DVDOpenPath( path );
00522 free( path );
00523 return ret_val;
00524 }
00525
00526
00527 fprintf( stderr, "libdvdread: Could not open %s\n", path );
00528 free( path );
00529 return 0;
00530 }
00531
00532 void DVDClose( dvd_reader_t *dvd )
00533 {
00534 if( dvd ) {
00535 if( dvd->dev ) dvdinput_close( dvd->dev );
00536 if( dvd->path_root ) free( dvd->path_root );
00537 if( dvd->udfcache ) FreeUDFCache( dvd->udfcache );
00538 free( dvd );
00539 }
00540 }
00541
00545 static dvd_file_t *DVDOpenFileUDF( dvd_reader_t *dvd, char *filename )
00546 {
00547 uint32_t start, len;
00548 dvd_file_t *dvd_file;
00549
00550 start = UDFFindFile( dvd, filename, &len );
00551 if( !start ) {
00552 fprintf( stderr, "libdvdnav:DVDOpenFileUDF:UDFFindFile %s failed\n", filename );
00553 return 0;
00554 }
00555
00556 dvd_file = (dvd_file_t *) malloc( sizeof( dvd_file_t ) );
00557 if( !dvd_file ) {
00558 fprintf( stderr, "libdvdnav:DVDOpenFileUDF:malloc failed\n" );
00559 return 0;
00560 }
00561 dvd_file->dvd = dvd;
00562 dvd_file->lb_start = start;
00563 dvd_file->seek_pos = 0;
00564 memset( dvd_file->title_sizes, 0, sizeof( dvd_file->title_sizes ) );
00565 memset( dvd_file->title_devs, 0, sizeof( dvd_file->title_devs ) );
00566 dvd_file->filesize = len / DVD_VIDEO_LB_LEN;
00567
00568 return dvd_file;
00569 }
00570
00577 static int findDirFile( const char *path, const char *file, char *filename )
00578 {
00579 DIR *dir;
00580 struct dirent *ent;
00581
00582 dir = opendir( path );
00583 if( !dir ) return -2;
00584
00585 while( ( ent = readdir( dir ) ) != NULL ) {
00586 if( !strcasecmp( ent->d_name, file ) ) {
00587 sprintf( filename, "%s%s%s", path,
00588 ( ( path[ strlen( path ) - 1 ] == '/' ) ? "" : "/" ),
00589 ent->d_name );
00590 closedir( dir );
00591 return 0;
00592 }
00593 }
00594
00595 closedir( dir );
00596 return -1;
00597 }
00598
00599 static int findDVDFile( dvd_reader_t *dvd, const char *file, char *filename )
00600 {
00601 char video_path[ PATH_MAX + 1 ];
00602 const char *nodirfile;
00603 int ret;
00604
00605
00606 if( !strncasecmp( "/VIDEO_TS/", file, 10 ) ) {
00607 nodirfile = &(file[ 10 ]);
00608 } else {
00609 nodirfile = file;
00610 }
00611
00612 ret = findDirFile( dvd->path_root, nodirfile, filename );
00613 if( ret < 0 ) {
00614
00615 sprintf( video_path, "%s/VIDEO_TS/", dvd->path_root );
00616 ret = findDirFile( video_path, nodirfile, filename );
00617 if( ret < 0 ) {
00618
00619 sprintf( video_path, "%s/video_ts/", dvd->path_root );
00620 ret = findDirFile( video_path, nodirfile, filename );
00621 if( ret < 0 ) {
00622 return 0;
00623 }
00624 }
00625 }
00626
00627 return 1;
00628 }
00629
00633 static dvd_file_t *DVDOpenFilePath( dvd_reader_t *dvd, char *filename )
00634 {
00635 char full_path[ PATH_MAX + 1 ];
00636 dvd_file_t *dvd_file;
00637 struct stat fileinfo;
00638 dvd_input_t dev;
00639
00640
00641 if( !findDVDFile( dvd, filename, full_path ) ) {
00642 fprintf( stderr, "libdvdnav:DVDOpenFilePath:findDVDFile %s failed\n", filename );
00643 return 0;
00644 }
00645
00646 dev = dvdinput_open( full_path );
00647 if( !dev ) {
00648 fprintf( stderr, "libdvdnav:DVDOpenFilePath:dvdinput_open %s failed\n", full_path );
00649 return 0;
00650 }
00651
00652 dvd_file = (dvd_file_t *) malloc( sizeof( dvd_file_t ) );
00653 if( !dvd_file ) {
00654 fprintf( stderr, "libdvdnav:DVDOpenFilePath:dvd_file malloc failed\n" );
00655 return 0;
00656 }
00657 dvd_file->dvd = dvd;
00658 dvd_file->lb_start = 0;
00659 dvd_file->seek_pos = 0;
00660 memset( dvd_file->title_sizes, 0, sizeof( dvd_file->title_sizes ) );
00661 memset( dvd_file->title_devs, 0, sizeof( dvd_file->title_devs ) );
00662 dvd_file->filesize = 0;
00663
00664 if( stat( full_path, &fileinfo ) < 0 ) {
00665 fprintf( stderr, "libdvdread: Can't stat() %s.\n", filename );
00666 free( dvd_file );
00667 return 0;
00668 }
00669 dvd_file->title_sizes[ 0 ] = fileinfo.st_size / DVD_VIDEO_LB_LEN;
00670 dvd_file->title_devs[ 0 ] = dev;
00671 dvd_file->filesize = dvd_file->title_sizes[ 0 ];
00672
00673 return dvd_file;
00674 }
00675
00676 static dvd_file_t *DVDOpenVOBUDF( dvd_reader_t *dvd, int title, int menu )
00677 {
00678 char filename[ MAX_UDF_FILE_NAME_LEN ];
00679 uint32_t start, len;
00680 dvd_file_t *dvd_file;
00681
00682 if( title == 0 ) {
00683 sprintf( filename, "/VIDEO_TS/VIDEO_TS.VOB" );
00684 } else {
00685 sprintf( filename, "/VIDEO_TS/VTS_%02d_%d.VOB", title, menu ? 0 : 1 );
00686 }
00687 start = UDFFindFile( dvd, filename, &len );
00688 if( start == 0 ) return 0;
00689
00690 dvd_file = (dvd_file_t *) malloc( sizeof( dvd_file_t ) );
00691 if( !dvd_file ) return 0;
00692 dvd_file->dvd = dvd;
00693 dvd_file->css_title = title << 1 | menu;
00694 dvd_file->lb_start = start;
00695 dvd_file->seek_pos = 0;
00696 memset( dvd_file->title_sizes, 0, sizeof( dvd_file->title_sizes ) );
00697 memset( dvd_file->title_devs, 0, sizeof( dvd_file->title_devs ) );
00698 dvd_file->filesize = len / DVD_VIDEO_LB_LEN;
00699
00700
00701 if( !menu ) {
00702 int cur;
00703
00704 for( cur = 2; cur < 10; cur++ ) {
00705 sprintf( filename, "/VIDEO_TS/VTS_%02d_%d.VOB", title, cur );
00706 if( !UDFFindFile( dvd, filename, &len ) ) break;
00707 dvd_file->filesize += len / DVD_VIDEO_LB_LEN;
00708 }
00709 }
00710
00711 if( dvd->css_state == 1 ) {
00712 initAllCSSKeys( dvd );
00713 dvd->css_state = 2;
00714 }
00715
00716
00717
00718
00719
00720
00721
00722 return dvd_file;
00723 }
00724
00725 static dvd_file_t *DVDOpenVOBPath( dvd_reader_t *dvd, int title, int menu )
00726 {
00727 char filename[ MAX_UDF_FILE_NAME_LEN ];
00728 char full_path[ PATH_MAX + 1 ];
00729 struct stat fileinfo;
00730 dvd_file_t *dvd_file;
00731 int i;
00732
00733 dvd_file = (dvd_file_t *) malloc( sizeof( dvd_file_t ) );
00734 if( !dvd_file ) return 0;
00735 dvd_file->dvd = dvd;
00736 dvd_file->css_title = title << 1 | menu;
00737 dvd_file->lb_start = 0;
00738 dvd_file->seek_pos = 0;
00739 memset( dvd_file->title_sizes, 0, sizeof( dvd_file->title_sizes ) );
00740 memset( dvd_file->title_devs, 0, sizeof( dvd_file->title_devs ) );
00741 dvd_file->filesize = 0;
00742
00743 if( menu ) {
00744 dvd_input_t dev;
00745
00746 if( title == 0 ) {
00747 sprintf( filename, "VIDEO_TS.VOB" );
00748 } else {
00749 sprintf( filename, "VTS_%02i_0.VOB", title );
00750 }
00751 if( !findDVDFile( dvd, filename, full_path ) ) {
00752 free( dvd_file );
00753 return 0;
00754 }
00755
00756 dev = dvdinput_open( full_path );
00757 if( dev == NULL ) {
00758 free( dvd_file );
00759 return 0;
00760 }
00761
00762 if( stat( full_path, &fileinfo ) < 0 ) {
00763 fprintf( stderr, "libdvdread: Can't stat() %s.\n", filename );
00764 free( dvd_file );
00765 return 0;
00766 }
00767 dvd_file->title_sizes[ 0 ] = fileinfo.st_size / DVD_VIDEO_LB_LEN;
00768 dvd_file->title_devs[ 0 ] = dev;
00769 dvdinput_title( dvd_file->title_devs[0], 0);
00770 dvd_file->filesize = dvd_file->title_sizes[ 0 ];
00771
00772 } else {
00773 for( i = 0; i < 9; ++i ) {
00774
00775 sprintf( filename, "VTS_%02i_%i.VOB", title, i + 1 );
00776 if( !findDVDFile( dvd, filename, full_path ) ) {
00777 break;
00778 }
00779
00780 if( stat( full_path, &fileinfo ) < 0 ) {
00781 fprintf( stderr, "libdvdread: Can't stat() %s.\n", filename );
00782 break;
00783 }
00784
00785 dvd_file->title_sizes[ i ] = fileinfo.st_size / DVD_VIDEO_LB_LEN;
00786 dvd_file->title_devs[ i ] = dvdinput_open( full_path );
00787 dvdinput_title( dvd_file->title_devs[ i ], 0 );
00788 dvd_file->filesize += dvd_file->title_sizes[ i ];
00789 }
00790 if( !dvd_file->title_devs[ 0 ] ) {
00791 free( dvd_file );
00792 return 0;
00793 }
00794 }
00795
00796 return dvd_file;
00797 }
00798
00799 dvd_file_t *DVDOpenFile( dvd_reader_t *dvd, int titlenum,
00800 dvd_read_domain_t domain )
00801 {
00802 char filename[ MAX_UDF_FILE_NAME_LEN ];
00803
00804
00805 if( dvd == NULL || titlenum < 0 )
00806 return NULL;
00807
00808 switch( domain ) {
00809 case DVD_READ_INFO_FILE:
00810 if( titlenum == 0 ) {
00811 sprintf( filename, "/VIDEO_TS/VIDEO_TS.IFO" );
00812 } else {
00813 sprintf( filename, "/VIDEO_TS/VTS_%02i_0.IFO", titlenum );
00814 }
00815 break;
00816 case DVD_READ_INFO_BACKUP_FILE:
00817 if( titlenum == 0 ) {
00818 sprintf( filename, "/VIDEO_TS/VIDEO_TS.BUP" );
00819 } else {
00820 sprintf( filename, "/VIDEO_TS/VTS_%02i_0.BUP", titlenum );
00821 }
00822 break;
00823 case DVD_READ_MENU_VOBS:
00824 if( dvd->isImageFile ) {
00825 return DVDOpenVOBUDF( dvd, titlenum, 1 );
00826 } else {
00827 return DVDOpenVOBPath( dvd, titlenum, 1 );
00828 }
00829 break;
00830 case DVD_READ_TITLE_VOBS:
00831 if( titlenum == 0 ) return 0;
00832 if( dvd->isImageFile ) {
00833 return DVDOpenVOBUDF( dvd, titlenum, 0 );
00834 } else {
00835 return DVDOpenVOBPath( dvd, titlenum, 0 );
00836 }
00837 break;
00838 default:
00839 fprintf( stderr, "libdvdread: Invalid domain for file open.\n" );
00840 return NULL;
00841 }
00842
00843 if( dvd->isImageFile ) {
00844 return DVDOpenFileUDF( dvd, filename );
00845 } else {
00846 return DVDOpenFilePath( dvd, filename );
00847 }
00848 }
00849
00850 void DVDCloseFile( dvd_file_t *dvd_file )
00851 {
00852 int i;
00853
00854 if( dvd_file ) {
00855 if( dvd_file->dvd->isImageFile ) {
00856 ;
00857 } else {
00858 for( i = 0; i < 9; ++i ) {
00859 if( dvd_file->title_devs[ i ] ) {
00860 dvdinput_close( dvd_file->title_devs[i] );
00861 }
00862 }
00863 }
00864
00865 free( dvd_file );
00866 dvd_file = 0;
00867 }
00868 }
00869
00870
00871 int UDFReadBlocksRaw( dvd_reader_t *device, uint32_t lb_number,
00872 size_t block_count, unsigned char *data,
00873 int encrypted )
00874 {
00875 int ret;
00876 if( !device->dev ) {
00877 fprintf( stderr, "libdvdread: Fatal error in block read.\n" );
00878 return 0;
00879 }
00880
00881 ret = dvdinput_seek( device->dev, (int) lb_number );
00882 if( ret != (int) lb_number ) {
00883 fprintf( stderr, "libdvdread: Can't seek to block %u\n", lb_number );
00884 return 0;
00885 }
00886
00887 ret = dvdinput_read( device->dev, (char *) data,
00888 (int) block_count, encrypted );
00889 return ret;
00890 }
00891
00892
00893
00894
00895
00896
00897
00898 static int DVDReadBlocksUDF( dvd_file_t *dvd_file, uint32_t offset,
00899 size_t block_count, unsigned char *data,
00900 int encrypted )
00901 {
00902 return UDFReadBlocksRaw( dvd_file->dvd, dvd_file->lb_start + offset,
00903 block_count, data, encrypted );
00904 }
00905
00906
00907
00908
00909
00910
00911
00912 static int DVDReadBlocksPath( dvd_file_t *dvd_file, unsigned int offset,
00913 size_t block_count, unsigned char *data,
00914 int encrypted )
00915 {
00916 int i;
00917 int ret, ret2, off;
00918
00919 ret = 0;
00920 ret2 = 0;
00921 for( i = 0; i < 9; ++i ) {
00922 if( !dvd_file->title_sizes[ i ] ) return 0;
00923
00924 if( offset < dvd_file->title_sizes[ i ] ) {
00925 if( ( offset + block_count ) <= dvd_file->title_sizes[ i ] ) {
00926 off = dvdinput_seek( dvd_file->title_devs[ i ], (int)offset );
00927 if( off < 0 || off != (int)offset ) {
00928 fprintf( stderr, "libdvdread: Can't seek to block %d\n",
00929 offset );
00930 return off < 0 ? off : 0;
00931 }
00932 ret = dvdinput_read( dvd_file->title_devs[ i ], data,
00933 (int)block_count, encrypted );
00934 break;
00935 } else {
00936 size_t part1_size = dvd_file->title_sizes[ i ] - offset;
00937
00938
00939
00940
00941 off = dvdinput_seek( dvd_file->title_devs[ i ], (int)offset );
00942 if( off < 0 || off != (int)offset ) {
00943 fprintf( stderr, "libdvdread: Can't seek to block %d\n",
00944 offset );
00945 return off < 0 ? off : 0;
00946 }
00947 ret = dvdinput_read( dvd_file->title_devs[ i ], data,
00948 (int)part1_size, encrypted );
00949 if( ret < 0 ) return ret;
00950
00951
00952
00953
00954 if( !dvd_file->title_devs[ i + 1 ] ) return ret;
00955
00956
00957 off = dvdinput_seek( dvd_file->title_devs[ i + 1 ], 0 );
00958 if( off < 0 || off != 0 ) {
00959 fprintf( stderr, "libdvdread: Can't seek to block %d\n",
00960 0 );
00961 return off < 0 ? off : 0;
00962 }
00963 ret2 = dvdinput_read( dvd_file->title_devs[ i + 1 ],
00964 data + ( part1_size
00965 * (int64_t)DVD_VIDEO_LB_LEN ),
00966 (int)(block_count - part1_size),
00967 encrypted );
00968 if( ret2 < 0 ) return ret2;
00969 break;
00970 }
00971 } else {
00972 offset -= dvd_file->title_sizes[ i ];
00973 }
00974 }
00975
00976 return ret + ret2;
00977 }
00978
00979
00980 ssize_t DVDReadBlocks( dvd_file_t *dvd_file, int offset,
00981 size_t block_count, unsigned char *data )
00982 {
00983 int ret;
00984
00985 if( dvd_file == NULL || offset < 0 || data == NULL )
00986 return -1;
00987
00988
00989 if( dvd_file->dvd->css_title != dvd_file->css_title ) {
00990 dvd_file->dvd->css_title = dvd_file->css_title;
00991 if( dvd_file->dvd->isImageFile ) {
00992 dvdinput_title( dvd_file->dvd->dev, (int)dvd_file->lb_start );
00993 }
00994
00995
00996
00997
00998 }
00999
01000 if( dvd_file->dvd->isImageFile ) {
01001 ret = DVDReadBlocksUDF( dvd_file, (uint32_t)offset,
01002 block_count, data, DVDINPUT_READ_DECRYPT );
01003 } else {
01004 ret = DVDReadBlocksPath( dvd_file, (unsigned int)offset,
01005 block_count, data, DVDINPUT_READ_DECRYPT );
01006 }
01007
01008 return (ssize_t)ret;
01009 }
01010
01011 int32_t DVDFileSeek( dvd_file_t *dvd_file, int32_t offset )
01012 {
01013
01014 if( dvd_file == NULL || offset < 0 )
01015 return -1;
01016
01017 if( offset > dvd_file->filesize * DVD_VIDEO_LB_LEN ) {
01018 return -1;
01019 }
01020 dvd_file->seek_pos = (uint32_t) offset;
01021 return offset;
01022 }
01023
01024 ssize_t DVDReadBytes( dvd_file_t *dvd_file, void *data, size_t byte_size )
01025 {
01026 unsigned char *secbuf_base, *secbuf;
01027 unsigned int numsec, seek_sector, seek_byte;
01028 int ret;
01029
01030
01031 if( dvd_file == NULL || data == NULL )
01032 return -1;
01033
01034 seek_sector = dvd_file->seek_pos / DVD_VIDEO_LB_LEN;
01035 seek_byte = dvd_file->seek_pos % DVD_VIDEO_LB_LEN;
01036
01037 numsec = ( ( seek_byte + byte_size ) / DVD_VIDEO_LB_LEN ) +
01038 ( ( ( seek_byte + byte_size ) % DVD_VIDEO_LB_LEN ) ? 1 : 0 );
01039
01040 secbuf_base = (unsigned char *) malloc( numsec * DVD_VIDEO_LB_LEN + 2048 );
01041 secbuf = (unsigned char *)(((uintptr_t)secbuf_base & ~((uintptr_t)2047)) + 2048);
01042 if( !secbuf_base ) {
01043 fprintf( stderr, "libdvdread: Can't allocate memory "
01044 "for file read!\n" );
01045 return 0;
01046 }
01047
01048 if( dvd_file->dvd->isImageFile ) {
01049 ret = DVDReadBlocksUDF( dvd_file, (uint32_t) seek_sector,
01050 (size_t) numsec, secbuf, DVDINPUT_NOFLAGS );
01051 } else {
01052 ret = DVDReadBlocksPath( dvd_file, seek_sector,
01053 (size_t) numsec, secbuf, DVDINPUT_NOFLAGS );
01054 }
01055
01056 if( ret != (int) numsec ) {
01057 free( secbuf_base );
01058 return ret < 0 ? ret : 0;
01059 }
01060
01061 memcpy( data, &(secbuf[ seek_byte ]), byte_size );
01062 free( secbuf_base );
01063
01064 dvd_file->seek_pos += byte_size;
01065 return byte_size;
01066 }
01067
01068 ssize_t DVDFileSize( dvd_file_t *dvd_file )
01069 {
01070
01071 if( dvd_file == NULL )
01072 return -1;
01073
01074 return dvd_file->filesize;
01075 }
01076
01077 int DVDDiscID( dvd_reader_t *dvd, unsigned char *discid )
01078 {
01079 struct md5_ctx ctx;
01080 int title;
01081
01082
01083 if( dvd == NULL || discid == NULL )
01084 return 0;
01085
01086
01087
01088 md5_init_ctx( &ctx );
01089 for( title = 0; title < 10; title++ ) {
01090 dvd_file_t *dvd_file = DVDOpenFile( dvd, title, DVD_READ_INFO_FILE );
01091 if( dvd_file != NULL ) {
01092 ssize_t bytes_read;
01093 size_t file_size = dvd_file->filesize * DVD_VIDEO_LB_LEN;
01094 char *buffer_base = malloc( file_size + 2048 );
01095 char *buffer = (unsigned char *)(((uintptr_t)buffer_base & ~((uintptr_t)2047)) + 2048);
01096
01097 if( buffer_base == NULL ) {
01098 fprintf( stderr, "libdvdread: DVDDiscId, failed to "
01099 "allocate memory for file read!\n" );
01100 DVDCloseFile( dvd_file );
01101 return -1;
01102 }
01103 bytes_read = DVDReadBytes( dvd_file, buffer, file_size );
01104 if( bytes_read != file_size ) {
01105 fprintf( stderr, "libdvdread: DVDDiscId read returned %zd bytes"
01106 ", wanted %zd\n", bytes_read, file_size );
01107 DVDCloseFile( dvd_file );
01108 free( buffer_base );
01109 return -1;
01110 }
01111
01112 md5_process_bytes( buffer, file_size, &ctx );
01113
01114 DVDCloseFile( dvd_file );
01115 free( buffer_base );
01116 }
01117 }
01118 md5_finish_ctx( &ctx, discid );
01119
01120 return 0;
01121 }
01122
01123
01124 int DVDISOVolumeInfo( dvd_reader_t *dvd,
01125 char *volid, unsigned int volid_size,
01126 unsigned char *volsetid, unsigned int volsetid_size )
01127 {
01128 unsigned char *buffer, *buffer_base;
01129 int ret;
01130
01131
01132 if( dvd == NULL )
01133 return 0;
01134
01135 if( dvd->dev == NULL ) {
01136
01137 return -1;
01138 }
01139
01140 buffer_base = malloc( DVD_VIDEO_LB_LEN + 2048 );
01141 buffer = (unsigned char *)(((uintptr_t)buffer_base & ~((uintptr_t)2047)) + 2048);
01142
01143 if( buffer_base == NULL ) {
01144 fprintf( stderr, "libdvdread: DVDISOVolumeInfo, failed to "
01145 "allocate memory for file read!\n" );
01146 return -1;
01147 }
01148
01149 ret = UDFReadBlocksRaw( dvd, 16, 1, buffer, 0 );
01150 if( ret != 1 ) {
01151 fprintf( stderr, "libdvdread: DVDISOVolumeInfo, failed to "
01152 "read ISO9660 Primary Volume Descriptor!\n" );
01153 free( buffer_base );
01154 return -1;
01155 }
01156
01157 if( (volid != NULL) && (volid_size > 0) ) {
01158 unsigned int n;
01159 for(n = 0; n < 32; n++) {
01160 if(buffer[40+n] == 0x20) {
01161 break;
01162 }
01163 }
01164
01165 if(volid_size > n+1) {
01166 volid_size = n+1;
01167 }
01168
01169 memcpy(volid, &buffer[40], volid_size-1);
01170 volid[volid_size-1] = '\0';
01171 }
01172
01173 if( (volsetid != NULL) && (volsetid_size > 0) ) {
01174 if(volsetid_size > 128) {
01175 volsetid_size = 128;
01176 }
01177 memcpy(volsetid, &buffer[190], volsetid_size);
01178 }
01179 free( buffer_base );
01180 return 0;
01181 }
01182
01183
01184 int DVDUDFVolumeInfo( dvd_reader_t *dvd,
01185 char *volid, unsigned int volid_size,
01186 unsigned char *volsetid, unsigned int volsetid_size )
01187 {
01188 int ret;
01189
01190 if( dvd == NULL )
01191 return -1;
01192
01193 if( dvd->dev == NULL ) {
01194
01195 return -1;
01196 }
01197
01198 if( (volid != NULL) && (volid_size > 0) ) {
01199 ret = UDFGetVolumeIdentifier(dvd, volid, volid_size);
01200 if(!ret) {
01201 return -1;
01202 }
01203 }
01204 if( (volsetid != NULL) && (volsetid_size > 0) ) {
01205 ret = UDFGetVolumeSetIdentifier(dvd, volsetid, volsetid_size);
01206 if(!ret) {
01207 return -1;
01208 }
01209 }
01210
01211 return 0;
01212 }