Mercurial > libdvdnav.hg
annotate dvdread/dvd_reader.c @ 326:c1d5abe51fda src
more 0->NULL changes, part of ogle-1778
| author | nicodvb |
|---|---|
| date | Mon, 31 Dec 2007 17:48:08 +0000 |
| parents | d61d2dd248b2 |
| children | d0a19ce1aa3d |
| rev | line source |
|---|---|
| 225 | 1 /* |
| 250 | 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> | |
| 225 | 5 * |
| 6 * This program is free software; you can redistribute it and/or modify | |
| 7 * it under the terms of the GNU General Public License as published by | |
| 8 * the Free Software Foundation; either version 2 of the License, or (at | |
| 9 * your option) any later version. | |
| 10 * | |
| 11 * This program is distributed in the hope that it will be useful, but | |
| 12 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 14 * General Public License for more details. | |
| 15 * | |
| 16 * You should have received a copy of the GNU General Public License | |
| 17 * along with this program; if not, write to the Free Software | |
| 18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. | |
| 19 */ | |
| 20 | |
| 21 #include "config.h" | |
| 22 | |
| 23 #include <sys/types.h> | |
| 24 #include <sys/stat.h> | |
| 25 #include <sys/time.h> /* For the timing of dvdcss_title crack. */ | |
| 26 #include <fcntl.h> | |
| 27 #include <stdlib.h> | |
| 28 #include <stdio.h> | |
| 29 #include <errno.h> | |
| 30 #include <string.h> | |
| 31 #include <unistd.h> | |
| 32 #include <limits.h> | |
| 33 #include <dirent.h> | |
| 233 | 34 |
| 35 /* misc win32 helpers */ | |
| 36 #ifdef WIN32 | |
| 37 /* replacement gettimeofday implementation */ | |
| 38 #include <sys/timeb.h> | |
| 250 | 39 static inline int _private_gettimeofday( struct timeval *tv, void *tz ) |
| 233 | 40 { |
| 41 struct timeb t; | |
| 42 ftime( &t ); | |
| 43 tv->tv_sec = t.time; | |
| 44 tv->tv_usec = t.millitm * 1000; | |
| 45 return 0; | |
| 46 } | |
| 250 | 47 #define gettimeofday(TV, TZ) _private_gettimeofday((TV), (TZ)) |
| 233 | 48 #include <io.h> /* read() */ |
| 49 #define lseek64 _lseeki64 | |
| 50 #endif | |
| 225 | 51 |
| 52 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__bsdi__)|| defined(__DARWIN__) | |
| 53 #define SYS_BSD 1 | |
| 54 #endif | |
| 55 | |
| 56 #if defined(__sun) | |
| 57 #include <sys/mnttab.h> | |
| 58 #elif defined(SYS_BSD) | |
| 59 #include <fstab.h> | |
| 60 #elif defined(__linux__) | |
| 61 #include <mntent.h> | |
| 62 #endif | |
| 63 | |
| 64 #include "dvd_udf.h" | |
| 65 #include "dvd_input.h" | |
| 66 #include "dvd_reader.h" | |
| 67 #include "md5.h" | |
| 68 | |
| 69 #define DEFAULT_UDF_CACHE_LEVEL 1 | |
| 70 | |
| 71 struct dvd_reader_s { | |
| 72 /* Basic information. */ | |
| 73 int isImageFile; | |
| 74 | |
| 75 /* Hack for keeping track of the css status. | |
| 76 * 0: no css, 1: perhaps (need init of keys), 2: have done init */ | |
| 77 int css_state; | |
| 78 int css_title; /* Last title that we have called dvdinpute_title for. */ | |
| 79 | |
| 80 /* Information required for an image file. */ | |
| 81 dvd_input_t dev; | |
| 82 | |
| 83 /* Information required for a directory path drive. */ | |
| 84 char *path_root; | |
| 85 | |
| 86 /* Filesystem cache */ | |
| 87 int udfcache_level; /* 0 - turned off, 1 - on */ | |
| 88 void *udfcache; | |
| 89 }; | |
| 90 | |
| 91 struct dvd_file_s { | |
| 92 /* Basic information. */ | |
| 93 dvd_reader_t *dvd; | |
| 94 | |
| 95 /* Hack for selecting the right css title. */ | |
| 96 int css_title; | |
| 97 | |
| 98 /* Information required for an image file. */ | |
| 99 uint32_t lb_start; | |
| 100 uint32_t seek_pos; | |
| 101 | |
| 102 /* Information required for a directory path drive. */ | |
| 103 size_t title_sizes[ 9 ]; | |
| 104 dvd_input_t title_devs[ 9 ]; | |
| 105 | |
| 106 /* Calculated at open-time, size in blocks. */ | |
| 107 ssize_t filesize; | |
| 108 }; | |
| 109 | |
| 110 int UDFReadBlocksRaw( dvd_reader_t *device, uint32_t lb_number, | |
| 111 size_t block_count, unsigned char *data, | |
| 112 int encrypted ); | |
| 113 | |
| 114 /** | |
| 115 * Set the level of caching on udf | |
| 116 * level = 0 (no caching) | |
| 117 * level = 1 (caching filesystem info) | |
| 118 */ | |
| 119 int DVDUDFCacheLevel(dvd_reader_t *device, int level) | |
| 120 { | |
| 121 struct dvd_reader_s *dev = (struct dvd_reader_s *)device; | |
| 122 | |
| 123 if(level > 0) { | |
| 124 level = 1; | |
| 125 } else if(level < 0) { | |
| 126 return dev->udfcache_level; | |
| 127 } | |
| 128 | |
| 129 dev->udfcache_level = level; | |
| 130 | |
| 131 return level; | |
| 132 } | |
| 133 | |
| 134 void *GetUDFCacheHandle(dvd_reader_t *device) | |
| 135 { | |
| 136 struct dvd_reader_s *dev = (struct dvd_reader_s *)device; | |
| 137 | |
| 138 return dev->udfcache; | |
| 139 } | |
| 140 | |
| 141 void SetUDFCacheHandle(dvd_reader_t *device, void *cache) | |
| 142 { | |
| 143 struct dvd_reader_s *dev = (struct dvd_reader_s *)device; | |
| 144 | |
| 145 dev->udfcache = cache; | |
| 146 } | |
| 147 | |
| 148 | |
| 149 | |
| 150 /* Loop over all titles and call dvdcss_title to crack the keys. */ | |
| 151 static int initAllCSSKeys( dvd_reader_t *dvd ) | |
| 152 { | |
| 153 struct timeval all_s, all_e; | |
| 154 struct timeval t_s, t_e; | |
| 155 char filename[ MAX_UDF_FILE_NAME_LEN ]; | |
| 156 uint32_t start, len; | |
| 157 int title; | |
| 158 | |
| 159 char *nokeys_str = getenv("DVDREAD_NOKEYS"); | |
| 160 if(nokeys_str != NULL) | |
| 161 return 0; | |
| 162 | |
| 163 fprintf( stderr, "\n" ); | |
| 164 fprintf( stderr, "libdvdread: Attempting to retrieve all CSS keys\n" ); | |
| 165 fprintf( stderr, "libdvdread: This can take a _long_ time, " | |
| 166 "please be patient\n\n" ); | |
| 167 | |
| 168 gettimeofday(&all_s, NULL); | |
| 169 | |
| 170 for( title = 0; title < 100; title++ ) { | |
| 171 gettimeofday( &t_s, NULL ); | |
| 172 if( title == 0 ) { | |
| 173 sprintf( filename, "/VIDEO_TS/VIDEO_TS.VOB" ); | |
| 174 } else { | |
| 175 sprintf( filename, "/VIDEO_TS/VTS_%02d_%d.VOB", title, 0 ); | |
| 176 } | |
| 177 start = UDFFindFile( dvd, filename, &len ); | |
| 178 if( start != 0 && len != 0 ) { | |
| 179 /* Perform CSS key cracking for this title. */ | |
| 180 fprintf( stderr, "libdvdread: Get key for %s at 0x%08x\n", | |
| 181 filename, start ); | |
| 182 if( dvdinput_title( dvd->dev, (int)start ) < 0 ) { | |
| 183 fprintf( stderr, "libdvdread: Error cracking CSS key for %s (0x%08x)\n", filename, start); | |
| 184 } | |
| 185 gettimeofday( &t_e, NULL ); | |
| 186 fprintf( stderr, "libdvdread: Elapsed time %ld\n", | |
| 187 (long int) t_e.tv_sec - t_s.tv_sec ); | |
| 188 } | |
| 189 | |
| 190 if( title == 0 ) continue; | |
| 191 | |
| 192 gettimeofday( &t_s, NULL ); | |
| 193 sprintf( filename, "/VIDEO_TS/VTS_%02d_%d.VOB", title, 1 ); | |
| 194 start = UDFFindFile( dvd, filename, &len ); | |
| 195 if( start == 0 || len == 0 ) break; | |
| 196 | |
| 197 /* Perform CSS key cracking for this title. */ | |
| 198 fprintf( stderr, "libdvdread: Get key for %s at 0x%08x\n", | |
| 199 filename, start ); | |
| 200 if( dvdinput_title( dvd->dev, (int)start ) < 0 ) { | |
| 201 fprintf( stderr, "libdvdread: Error cracking CSS key for %s (0x%08x)!!\n", filename, start); | |
| 202 } | |
| 203 gettimeofday( &t_e, NULL ); | |
| 204 fprintf( stderr, "libdvdread: Elapsed time %ld\n", | |
| 205 (long int) t_e.tv_sec - t_s.tv_sec ); | |
| 206 } | |
| 207 title--; | |
| 208 | |
| 209 fprintf( stderr, "libdvdread: Found %d VTS's\n", title ); | |
| 210 gettimeofday(&all_e, NULL); | |
| 211 fprintf( stderr, "libdvdread: Elapsed time %ld\n", | |
| 212 (long int) all_e.tv_sec - all_s.tv_sec ); | |
| 213 | |
| 214 return 0; | |
| 215 } | |
| 216 | |
| 217 | |
| 218 | |
| 219 /** | |
| 220 * Open a DVD image or block device file. | |
| 221 */ | |
| 222 static dvd_reader_t *DVDOpenImageFile( const char *location, int have_css ) | |
| 223 { | |
| 224 dvd_reader_t *dvd; | |
| 225 dvd_input_t dev; | |
| 226 | |
| 227 dev = dvdinput_open( location ); | |
| 228 if( !dev ) { | |
| 229 fprintf( stderr, "libdvdread: Can't open %s for reading\n", location ); | |
| 324 | 230 return NULL; |
| 225 | 231 } |
| 232 | |
| 233 dvd = (dvd_reader_t *) malloc( sizeof( dvd_reader_t ) ); | |
|
325
d61d2dd248b2
call dvdinput_close() after having successfully opened it but before exiting in case of errors; part of ogle-1778
nicodvb
parents:
324
diff
changeset
|
234 if( !dvd ) { |
|
d61d2dd248b2
call dvdinput_close() after having successfully opened it but before exiting in case of errors; part of ogle-1778
nicodvb
parents:
324
diff
changeset
|
235 dvdinput_close(dev); |
|
d61d2dd248b2
call dvdinput_close() after having successfully opened it but before exiting in case of errors; part of ogle-1778
nicodvb
parents:
324
diff
changeset
|
236 return NULL; |
|
d61d2dd248b2
call dvdinput_close() after having successfully opened it but before exiting in case of errors; part of ogle-1778
nicodvb
parents:
324
diff
changeset
|
237 } |
| 225 | 238 dvd->isImageFile = 1; |
| 239 dvd->dev = dev; | |
| 326 | 240 dvd->path_root = NULL; |
| 225 | 241 |
| 242 dvd->udfcache_level = DEFAULT_UDF_CACHE_LEVEL; | |
| 243 dvd->udfcache = NULL; | |
| 244 | |
| 245 if( have_css ) { | |
| 246 /* Only if DVDCSS_METHOD = title, a bit if it's disc or if | |
| 247 * DVDCSS_METHOD = key but region missmatch. Unfortunaly we | |
| 248 * don't have that information. */ | |
| 249 | |
| 250 dvd->css_state = 1; /* Need key init. */ | |
| 251 } | |
| 252 dvd->css_title = 0; | |
| 253 | |
| 254 return dvd; | |
| 255 } | |
| 256 | |
| 257 static dvd_reader_t *DVDOpenPath( const char *path_root ) | |
| 258 { | |
| 259 dvd_reader_t *dvd; | |
| 260 | |
| 261 dvd = (dvd_reader_t *) malloc( sizeof( dvd_reader_t ) ); | |
| 324 | 262 if( !dvd ) return NULL; |
| 225 | 263 dvd->isImageFile = 0; |
| 264 dvd->dev = 0; | |
| 265 dvd->path_root = strdup( path_root ); | |
| 266 | |
| 267 dvd->udfcache_level = DEFAULT_UDF_CACHE_LEVEL; | |
| 268 dvd->udfcache = NULL; | |
| 269 | |
| 270 dvd->css_state = 0; /* Only used in the UDF path */ | |
| 271 dvd->css_title = 0; /* Only matters in the UDF path */ | |
| 272 | |
| 273 return dvd; | |
| 274 } | |
| 275 | |
| 276 #if defined(__sun) | |
| 277 /* /dev/rdsk/c0t6d0s0 (link to /devices/...) | |
| 278 /vol/dev/rdsk/c0t6d0/?? | |
| 279 /vol/rdsk/<name> */ | |
| 280 static char *sun_block2char( const char *path ) | |
| 281 { | |
| 282 char *new_path; | |
| 283 | |
| 284 /* Must contain "/dsk/" */ | |
| 285 if( !strstr( path, "/dsk/" ) ) return (char *) strdup( path ); | |
| 286 | |
| 287 /* Replace "/dsk/" with "/rdsk/" */ | |
| 288 new_path = malloc( strlen(path) + 2 ); | |
| 289 strcpy( new_path, path ); | |
| 290 strcpy( strstr( new_path, "/dsk/" ), "" ); | |
| 291 strcat( new_path, "/rdsk/" ); | |
| 292 strcat( new_path, strstr( path, "/dsk/" ) + strlen( "/dsk/" ) ); | |
| 293 | |
| 294 return new_path; | |
| 295 } | |
| 296 #endif | |
| 297 | |
| 298 #if defined(SYS_BSD) | |
| 299 /* FreeBSD /dev/(r)(a)cd0c (a is for atapi), recomended to _not_ use r | |
| 300 OpenBSD /dev/rcd0c, it needs to be the raw device | |
| 301 NetBSD /dev/rcd0[d|c|..] d for x86, c (for non x86), perhaps others | |
| 302 Darwin /dev/rdisk0, it needs to be the raw device | |
| 303 BSD/OS /dev/sr0c (if not mounted) or /dev/rsr0c ('c' any letter will do) */ | |
| 304 static char *bsd_block2char( const char *path ) | |
| 305 { | |
| 306 char *new_path; | |
| 307 | |
| 308 /* If it doesn't start with "/dev/" or does start with "/dev/r" exit */ | |
| 309 if( !strncmp( path, "/dev/", 5 ) || strncmp( path, "/dev/r", 6 ) ) | |
| 310 return (char *) strdup( path ); | |
| 311 | |
| 312 /* Replace "/dev/" with "/dev/r" */ | |
| 313 new_path = malloc( strlen(path) + 2 ); | |
| 314 strcpy( new_path, "/dev/r" ); | |
| 315 strcat( new_path, path + strlen( "/dev/" ) ); | |
| 316 | |
| 317 return new_path; | |
| 318 } | |
| 319 #endif | |
| 320 | |
| 321 dvd_reader_t *DVDOpen( const char *ppath ) | |
| 322 { | |
| 323 struct stat fileinfo; | |
| 324 int ret; | |
| 325 int have_css; | |
| 326 dvd_reader_t *ret_val = NULL; | |
| 326 | 327 char *dev_name = NULL; |
| 225 | 328 char *path; |
| 329 | |
| 330 #ifdef _MSC_VER | |
| 331 int len; | |
| 332 #endif | |
| 333 | |
| 334 if( ppath == NULL ) | |
| 335 return 0; | |
| 336 | |
| 337 path = strdup(ppath); | |
|
299
f57d023eb107
in DVDOpen() exit if strdup() fails; part of a patch by Videolan team
nicodvb
parents:
283
diff
changeset
|
338 if( path == NULL ) |
|
f57d023eb107
in DVDOpen() exit if strdup() fails; part of a patch by Videolan team
nicodvb
parents:
283
diff
changeset
|
339 return 0; |
| 225 | 340 |
| 341 /* Try to open libdvdcss or fall back to standard functions */ | |
| 342 have_css = dvdinput_setup(); | |
| 343 | |
| 344 #ifdef _MSC_VER | |
| 345 /* Strip off the trailing \ if it is not a drive */ | |
| 346 len = strlen(path); | |
| 347 if ((len > 1) && | |
| 348 (path[len - 1] == '\\') && | |
| 349 (path[len - 2] != ':')) | |
| 350 { | |
| 351 path[len-1] = '\0'; | |
| 352 } | |
| 353 #endif | |
| 354 | |
| 355 ret = stat( path, &fileinfo ); | |
| 356 | |
| 357 if( ret < 0 ) { | |
| 358 | |
| 359 /* maybe "host:port" url? try opening it with acCeSS library */ | |
| 360 if( strchr(path,':') ) { | |
| 361 ret_val = DVDOpenImageFile( path, have_css ); | |
| 362 free(path); | |
| 363 return ret_val; | |
| 364 } | |
| 365 | |
| 366 /* If we can't stat the file, give up */ | |
| 367 fprintf( stderr, "libdvdread: Can't stat %s\n", path ); | |
| 368 perror(""); | |
| 369 free(path); | |
| 324 | 370 return NULL; |
| 225 | 371 } |
| 372 | |
| 373 /* First check if this is a block/char device or a file*/ | |
| 374 if( S_ISBLK( fileinfo.st_mode ) || | |
| 375 S_ISCHR( fileinfo.st_mode ) || | |
| 376 S_ISREG( fileinfo.st_mode ) ) { | |
| 377 | |
| 378 /** | |
| 379 * Block devices and regular files are assumed to be DVD-Video images. | |
| 380 */ | |
| 381 #if defined(__sun) | |
| 382 ret_val = DVDOpenImageFile( sun_block2char( path ), have_css ); | |
| 383 #elif defined(SYS_BSD) | |
| 384 ret_val = DVDOpenImageFile( bsd_block2char( path ), have_css ); | |
| 385 #else | |
| 386 ret_val = DVDOpenImageFile( path, have_css ); | |
| 387 #endif | |
| 388 | |
| 389 free(path); | |
| 390 return ret_val; | |
| 391 | |
| 392 } else if( S_ISDIR( fileinfo.st_mode ) ) { | |
| 393 dvd_reader_t *auth_drive = 0; | |
| 394 char *path_copy; | |
| 395 #if defined(SYS_BSD) | |
| 396 struct fstab* fe; | |
| 397 #elif defined(__sun) || defined(__linux__) | |
| 398 FILE *mntfile; | |
| 399 #endif | |
| 400 | |
| 401 /* XXX: We should scream real loud here. */ | |
| 402 if( !(path_copy = strdup( path ) ) ) { | |
| 403 free(path); | |
| 324 | 404 return NULL; |
| 225 | 405 } |
| 406 | |
| 233 | 407 #ifndef WIN32 /* don't have fchdir, and getcwd( NULL, ... ) is strange */ |
| 408 /* Also WIN32 does not have symlinks, so we don't need this bit of code. */ | |
| 409 | |
| 225 | 410 /* Resolve any symlinks and get the absolut dir name. */ |
| 411 { | |
| 412 char *new_path; | |
| 413 int cdir = open( ".", O_RDONLY ); | |
| 414 | |
| 415 if( cdir >= 0 ) { | |
| 416 chdir( path_copy ); | |
| 417 new_path = getcwd( NULL, PATH_MAX ); | |
| 418 fchdir( cdir ); | |
| 419 close( cdir ); | |
| 420 if( new_path ) { | |
| 421 free( path_copy ); | |
| 422 path_copy = new_path; | |
| 423 } | |
| 424 } | |
| 425 } | |
| 233 | 426 #endif |
| 225 | 427 /** |
| 428 * If we're being asked to open a directory, check if that directory | |
| 429 * is the mountpoint for a DVD-ROM which we can use instead. | |
| 430 */ | |
| 431 | |
| 432 if( strlen( path_copy ) > 1 ) { | |
| 433 if( path_copy[ strlen( path_copy ) - 1 ] == '/' ) | |
| 434 path_copy[ strlen( path_copy ) - 1 ] = '\0'; | |
| 435 } | |
| 436 | |
| 437 if( strlen( path_copy ) > 9 ) { | |
| 438 if( !strcasecmp( &(path_copy[ strlen( path_copy ) - 9 ]), | |
| 439 "/video_ts" ) ) { | |
| 440 path_copy[ strlen( path_copy ) - 9 ] = '\0'; | |
| 441 } | |
| 442 } | |
| 443 | |
| 444 #if defined(SYS_BSD) | |
| 445 if( ( fe = getfsfile( path_copy ) ) ) { | |
| 446 dev_name = bsd_block2char( fe->fs_spec ); | |
| 447 fprintf( stderr, | |
| 448 "libdvdread: Attempting to use device %s" | |
| 449 " mounted on %s for CSS authentication\n", | |
| 450 dev_name, | |
| 451 fe->fs_file ); | |
| 452 auth_drive = DVDOpenImageFile( dev_name, have_css ); | |
| 453 } | |
| 454 #elif defined(__sun) | |
| 455 mntfile = fopen( MNTTAB, "r" ); | |
| 456 if( mntfile ) { | |
| 457 struct mnttab mp; | |
| 458 int res; | |
| 459 | |
| 460 while( ( res = getmntent( mntfile, &mp ) ) != -1 ) { | |
| 461 if( res == 0 && !strcmp( mp.mnt_mountp, path_copy ) ) { | |
| 462 dev_name = sun_block2char( mp.mnt_special ); | |
| 463 fprintf( stderr, | |
| 464 "libdvdread: Attempting to use device %s" | |
| 465 " mounted on %s for CSS authentication\n", | |
| 466 dev_name, | |
| 467 mp.mnt_mountp ); | |
| 468 auth_drive = DVDOpenImageFile( dev_name, have_css ); | |
| 469 break; | |
| 470 } | |
| 471 } | |
| 472 fclose( mntfile ); | |
| 473 } | |
| 474 #elif defined(__linux__) | |
| 475 mntfile = fopen( MOUNTED, "r" ); | |
| 476 if( mntfile ) { | |
| 477 struct mntent *me; | |
| 478 | |
| 479 while( ( me = getmntent( mntfile ) ) ) { | |
| 480 if( !strcmp( me->mnt_dir, path_copy ) ) { | |
| 481 fprintf( stderr, | |
| 482 "libdvdread: Attempting to use device %s" | |
| 483 " mounted on %s for CSS authentication\n", | |
| 484 me->mnt_fsname, | |
| 485 me->mnt_dir ); | |
| 486 auth_drive = DVDOpenImageFile( me->mnt_fsname, have_css ); | |
| 487 dev_name = strdup(me->mnt_fsname); | |
| 488 break; | |
| 489 } | |
| 490 } | |
| 491 fclose( mntfile ); | |
| 492 } | |
| 493 #elif defined(_MSC_VER) | |
| 494 auth_drive = DVDOpenImageFile( path, have_css ); | |
| 495 #endif | |
| 496 | |
| 497 #ifndef _MSC_VER | |
| 498 if( !dev_name ) { | |
| 499 fprintf( stderr, "libdvdread: Couldn't find device name.\n" ); | |
| 500 } else if( !auth_drive ) { | |
| 501 fprintf( stderr, "libdvdread: Device %s inaccessible, " | |
| 502 "CSS authentication not available.\n", dev_name ); | |
| 503 } | |
| 504 #else | |
| 505 if( !auth_drive ) { | |
| 506 fprintf( stderr, "libdvdread: Device %s inaccessible, " | |
| 507 "CSS authentication not available.\n", dev_name ); | |
| 508 } | |
| 509 #endif | |
| 510 | |
| 511 free( dev_name ); | |
| 512 free( path_copy ); | |
| 513 | |
| 514 /** | |
| 515 * If we've opened a drive, just use that. | |
| 516 */ | |
| 517 if( auth_drive ) { | |
| 518 free(path); | |
| 519 return auth_drive; | |
| 520 } | |
| 521 | |
| 522 /** | |
| 523 * Otherwise, we now try to open the directory tree instead. | |
| 524 */ | |
| 525 ret_val = DVDOpenPath( path ); | |
| 526 free( path ); | |
| 527 return ret_val; | |
| 528 } | |
| 529 | |
| 530 /* If it's none of the above, screw it. */ | |
| 531 fprintf( stderr, "libdvdread: Could not open %s\n", path ); | |
| 532 free( path ); | |
| 324 | 533 return NULL; |
| 225 | 534 } |
| 535 | |
| 536 void DVDClose( dvd_reader_t *dvd ) | |
| 537 { | |
| 538 if( dvd ) { | |
| 539 if( dvd->dev ) dvdinput_close( dvd->dev ); | |
| 540 if( dvd->path_root ) free( dvd->path_root ); | |
| 541 if( dvd->udfcache ) FreeUDFCache( dvd->udfcache ); | |
| 542 free( dvd ); | |
| 543 } | |
| 544 } | |
| 545 | |
| 546 /** | |
| 547 * Open an unencrypted file on a DVD image file. | |
| 548 */ | |
| 549 static dvd_file_t *DVDOpenFileUDF( dvd_reader_t *dvd, char *filename ) | |
| 550 { | |
| 551 uint32_t start, len; | |
| 552 dvd_file_t *dvd_file; | |
| 553 | |
| 554 start = UDFFindFile( dvd, filename, &len ); | |
| 555 if( !start ) { | |
| 556 fprintf( stderr, "libdvdnav:DVDOpenFileUDF:UDFFindFile %s failed\n", filename ); | |
| 324 | 557 return NULL; |
| 225 | 558 } |
| 559 | |
| 560 dvd_file = (dvd_file_t *) malloc( sizeof( dvd_file_t ) ); | |
| 561 if( !dvd_file ) { | |
| 562 fprintf( stderr, "libdvdnav:DVDOpenFileUDF:malloc failed\n" ); | |
| 324 | 563 return NULL; |
| 225 | 564 } |
| 565 dvd_file->dvd = dvd; | |
| 566 dvd_file->lb_start = start; | |
| 567 dvd_file->seek_pos = 0; | |
| 568 memset( dvd_file->title_sizes, 0, sizeof( dvd_file->title_sizes ) ); | |
| 569 memset( dvd_file->title_devs, 0, sizeof( dvd_file->title_devs ) ); | |
| 570 dvd_file->filesize = len / DVD_VIDEO_LB_LEN; | |
| 571 | |
| 572 return dvd_file; | |
| 573 } | |
| 574 | |
| 575 /** | |
| 576 * Searches for <file> in directory <path>, ignoring case. | |
| 577 * Returns 0 and full filename in <filename>. | |
| 578 * or -1 on file not found. | |
| 579 * or -2 on path not found. | |
| 580 */ | |
| 581 static int findDirFile( const char *path, const char *file, char *filename ) | |
| 582 { | |
| 583 DIR *dir; | |
| 584 struct dirent *ent; | |
| 585 | |
| 586 dir = opendir( path ); | |
| 587 if( !dir ) return -2; | |
| 588 | |
| 589 while( ( ent = readdir( dir ) ) != NULL ) { | |
| 590 if( !strcasecmp( ent->d_name, file ) ) { | |
| 591 sprintf( filename, "%s%s%s", path, | |
| 592 ( ( path[ strlen( path ) - 1 ] == '/' ) ? "" : "/" ), | |
| 593 ent->d_name ); | |
|
283
d0b684fc4b1f
avoid leaking file descriptors obtained by opendir(); synced with upstream as reported by Diego Petteno'
nicodvb
parents:
250
diff
changeset
|
594 closedir( dir ); |
| 225 | 595 return 0; |
| 596 } | |
| 597 } | |
| 598 | |
|
283
d0b684fc4b1f
avoid leaking file descriptors obtained by opendir(); synced with upstream as reported by Diego Petteno'
nicodvb
parents:
250
diff
changeset
|
599 closedir( dir ); |
| 225 | 600 return -1; |
| 601 } | |
| 602 | |
| 603 static int findDVDFile( dvd_reader_t *dvd, const char *file, char *filename ) | |
| 604 { | |
| 605 char video_path[ PATH_MAX + 1 ]; | |
| 606 const char *nodirfile; | |
| 607 int ret; | |
| 608 | |
| 609 /* Strip off the directory for our search */ | |
| 610 if( !strncasecmp( "/VIDEO_TS/", file, 10 ) ) { | |
| 611 nodirfile = &(file[ 10 ]); | |
| 612 } else { | |
| 613 nodirfile = file; | |
| 614 } | |
| 615 | |
| 616 ret = findDirFile( dvd->path_root, nodirfile, filename ); | |
| 617 if( ret < 0 ) { | |
| 618 /* Try also with adding the path, just in case. */ | |
| 619 sprintf( video_path, "%s/VIDEO_TS/", dvd->path_root ); | |
| 620 ret = findDirFile( video_path, nodirfile, filename ); | |
| 621 if( ret < 0 ) { | |
| 622 /* Try with the path, but in lower case. */ | |
| 623 sprintf( video_path, "%s/video_ts/", dvd->path_root ); | |
| 624 ret = findDirFile( video_path, nodirfile, filename ); | |
| 625 if( ret < 0 ) { | |
| 626 return 0; | |
| 627 } | |
| 628 } | |
| 629 } | |
| 630 | |
| 631 return 1; | |
| 632 } | |
| 633 | |
| 634 /** | |
| 635 * Open an unencrypted file from a DVD directory tree. | |
| 636 */ | |
| 637 static dvd_file_t *DVDOpenFilePath( dvd_reader_t *dvd, char *filename ) | |
| 638 { | |
| 639 char full_path[ PATH_MAX + 1 ]; | |
| 640 dvd_file_t *dvd_file; | |
| 641 struct stat fileinfo; | |
| 642 dvd_input_t dev; | |
| 643 | |
| 644 /* Get the full path of the file. */ | |
| 645 if( !findDVDFile( dvd, filename, full_path ) ) { | |
| 646 fprintf( stderr, "libdvdnav:DVDOpenFilePath:findDVDFile %s failed\n", filename ); | |
| 324 | 647 return NULL; |
| 225 | 648 } |
| 649 | |
| 650 dev = dvdinput_open( full_path ); | |
| 651 if( !dev ) { | |
| 652 fprintf( stderr, "libdvdnav:DVDOpenFilePath:dvdinput_open %s failed\n", full_path ); | |
| 324 | 653 return NULL; |
| 225 | 654 } |
| 655 | |
| 656 dvd_file = (dvd_file_t *) malloc( sizeof( dvd_file_t ) ); | |
| 657 if( !dvd_file ) { | |
| 658 fprintf( stderr, "libdvdnav:DVDOpenFilePath:dvd_file malloc failed\n" ); | |
|
325
d61d2dd248b2
call dvdinput_close() after having successfully opened it but before exiting in case of errors; part of ogle-1778
nicodvb
parents:
324
diff
changeset
|
659 dvdinput_close(dev); |
| 324 | 660 return NULL; |
| 225 | 661 } |
| 662 dvd_file->dvd = dvd; | |
| 663 dvd_file->lb_start = 0; | |
| 664 dvd_file->seek_pos = 0; | |
| 665 memset( dvd_file->title_sizes, 0, sizeof( dvd_file->title_sizes ) ); | |
| 666 memset( dvd_file->title_devs, 0, sizeof( dvd_file->title_devs ) ); | |
| 667 dvd_file->filesize = 0; | |
| 668 | |
| 669 if( stat( full_path, &fileinfo ) < 0 ) { | |
| 670 fprintf( stderr, "libdvdread: Can't stat() %s.\n", filename ); | |
| 671 free( dvd_file ); | |
| 324 | 672 return NULL; |
| 225 | 673 } |
| 674 dvd_file->title_sizes[ 0 ] = fileinfo.st_size / DVD_VIDEO_LB_LEN; | |
| 675 dvd_file->title_devs[ 0 ] = dev; | |
| 676 dvd_file->filesize = dvd_file->title_sizes[ 0 ]; | |
| 677 | |
| 678 return dvd_file; | |
| 679 } | |
| 680 | |
| 681 static dvd_file_t *DVDOpenVOBUDF( dvd_reader_t *dvd, int title, int menu ) | |
| 682 { | |
| 683 char filename[ MAX_UDF_FILE_NAME_LEN ]; | |
| 684 uint32_t start, len; | |
| 685 dvd_file_t *dvd_file; | |
| 686 | |
| 687 if( title == 0 ) { | |
| 688 sprintf( filename, "/VIDEO_TS/VIDEO_TS.VOB" ); | |
| 689 } else { | |
| 690 sprintf( filename, "/VIDEO_TS/VTS_%02d_%d.VOB", title, menu ? 0 : 1 ); | |
| 691 } | |
| 692 start = UDFFindFile( dvd, filename, &len ); | |
| 324 | 693 if( start == 0 ) return NULL; |
| 225 | 694 |
| 695 dvd_file = (dvd_file_t *) malloc( sizeof( dvd_file_t ) ); | |
| 324 | 696 if( !dvd_file ) return NULL; |
| 225 | 697 dvd_file->dvd = dvd; |
| 698 /*Hack*/ dvd_file->css_title = title << 1 | menu; | |
| 699 dvd_file->lb_start = start; | |
| 700 dvd_file->seek_pos = 0; | |
| 701 memset( dvd_file->title_sizes, 0, sizeof( dvd_file->title_sizes ) ); | |
| 702 memset( dvd_file->title_devs, 0, sizeof( dvd_file->title_devs ) ); | |
| 703 dvd_file->filesize = len / DVD_VIDEO_LB_LEN; | |
| 704 | |
| 705 /* Calculate the complete file size for every file in the VOBS */ | |
| 706 if( !menu ) { | |
| 707 int cur; | |
| 708 | |
| 709 for( cur = 2; cur < 10; cur++ ) { | |
| 710 sprintf( filename, "/VIDEO_TS/VTS_%02d_%d.VOB", title, cur ); | |
| 711 if( !UDFFindFile( dvd, filename, &len ) ) break; | |
| 712 dvd_file->filesize += len / DVD_VIDEO_LB_LEN; | |
| 713 } | |
| 714 } | |
| 715 | |
| 716 if( dvd->css_state == 1 /* Need key init */ ) { | |
| 717 initAllCSSKeys( dvd ); | |
| 718 dvd->css_state = 2; | |
| 719 } | |
| 720 /* | |
| 721 if( dvdinput_title( dvd_file->dvd->dev, (int)start ) < 0 ) { | |
| 722 fprintf( stderr, "libdvdread: Error cracking CSS key for %s\n", | |
| 723 filename ); | |
| 724 } | |
| 725 */ | |
| 726 | |
| 727 return dvd_file; | |
| 728 } | |
| 729 | |
| 730 static dvd_file_t *DVDOpenVOBPath( dvd_reader_t *dvd, int title, int menu ) | |
| 731 { | |
| 732 char filename[ MAX_UDF_FILE_NAME_LEN ]; | |
| 733 char full_path[ PATH_MAX + 1 ]; | |
| 734 struct stat fileinfo; | |
| 735 dvd_file_t *dvd_file; | |
| 736 int i; | |
| 737 | |
| 738 dvd_file = (dvd_file_t *) malloc( sizeof( dvd_file_t ) ); | |
| 324 | 739 if( !dvd_file ) return NULL; |
| 225 | 740 dvd_file->dvd = dvd; |
| 741 /*Hack*/ dvd_file->css_title = title << 1 | menu; | |
| 742 dvd_file->lb_start = 0; | |
| 743 dvd_file->seek_pos = 0; | |
| 744 memset( dvd_file->title_sizes, 0, sizeof( dvd_file->title_sizes ) ); | |
| 745 memset( dvd_file->title_devs, 0, sizeof( dvd_file->title_devs ) ); | |
| 746 dvd_file->filesize = 0; | |
| 747 | |
| 748 if( menu ) { | |
| 749 dvd_input_t dev; | |
| 750 | |
| 751 if( title == 0 ) { | |
| 752 sprintf( filename, "VIDEO_TS.VOB" ); | |
| 753 } else { | |
| 754 sprintf( filename, "VTS_%02i_0.VOB", title ); | |
| 755 } | |
| 756 if( !findDVDFile( dvd, filename, full_path ) ) { | |
| 757 free( dvd_file ); | |
| 324 | 758 return NULL; |
| 225 | 759 } |
| 760 | |
| 761 dev = dvdinput_open( full_path ); | |
| 762 if( dev == NULL ) { | |
| 763 free( dvd_file ); | |
| 324 | 764 return NULL; |
| 225 | 765 } |
| 766 | |
| 767 if( stat( full_path, &fileinfo ) < 0 ) { | |
| 768 fprintf( stderr, "libdvdread: Can't stat() %s.\n", filename ); | |
|
325
d61d2dd248b2
call dvdinput_close() after having successfully opened it but before exiting in case of errors; part of ogle-1778
nicodvb
parents:
324
diff
changeset
|
769 dvdinput_close(dev); |
| 225 | 770 free( dvd_file ); |
| 324 | 771 return NULL; |
| 225 | 772 } |
| 773 dvd_file->title_sizes[ 0 ] = fileinfo.st_size / DVD_VIDEO_LB_LEN; | |
| 774 dvd_file->title_devs[ 0 ] = dev; | |
| 775 dvdinput_title( dvd_file->title_devs[0], 0); | |
| 776 dvd_file->filesize = dvd_file->title_sizes[ 0 ]; | |
| 777 | |
| 778 } else { | |
| 779 for( i = 0; i < 9; ++i ) { | |
| 780 | |
| 781 sprintf( filename, "VTS_%02i_%i.VOB", title, i + 1 ); | |
| 782 if( !findDVDFile( dvd, filename, full_path ) ) { | |
| 783 break; | |
| 784 } | |
| 785 | |
| 786 if( stat( full_path, &fileinfo ) < 0 ) { | |
| 787 fprintf( stderr, "libdvdread: Can't stat() %s.\n", filename ); | |
| 788 break; | |
| 789 } | |
| 790 | |
| 791 dvd_file->title_sizes[ i ] = fileinfo.st_size / DVD_VIDEO_LB_LEN; | |
| 792 dvd_file->title_devs[ i ] = dvdinput_open( full_path ); | |
| 793 dvdinput_title( dvd_file->title_devs[ i ], 0 ); | |
| 794 dvd_file->filesize += dvd_file->title_sizes[ i ]; | |
| 795 } | |
| 796 if( !dvd_file->title_devs[ 0 ] ) { | |
| 797 free( dvd_file ); | |
| 324 | 798 return NULL; |
| 225 | 799 } |
| 800 } | |
| 801 | |
| 802 return dvd_file; | |
| 803 } | |
| 804 | |
| 805 dvd_file_t *DVDOpenFile( dvd_reader_t *dvd, int titlenum, | |
| 806 dvd_read_domain_t domain ) | |
| 807 { | |
| 808 char filename[ MAX_UDF_FILE_NAME_LEN ]; | |
| 809 | |
| 810 /* Check arguments. */ | |
| 811 if( dvd == NULL || titlenum < 0 ) | |
| 812 return NULL; | |
| 813 | |
| 814 switch( domain ) { | |
| 815 case DVD_READ_INFO_FILE: | |
| 816 if( titlenum == 0 ) { | |
| 817 sprintf( filename, "/VIDEO_TS/VIDEO_TS.IFO" ); | |
| 818 } else { | |
| 819 sprintf( filename, "/VIDEO_TS/VTS_%02i_0.IFO", titlenum ); | |
| 820 } | |
| 821 break; | |
| 822 case DVD_READ_INFO_BACKUP_FILE: | |
| 823 if( titlenum == 0 ) { | |
| 824 sprintf( filename, "/VIDEO_TS/VIDEO_TS.BUP" ); | |
| 825 } else { | |
| 826 sprintf( filename, "/VIDEO_TS/VTS_%02i_0.BUP", titlenum ); | |
| 827 } | |
| 828 break; | |
| 829 case DVD_READ_MENU_VOBS: | |
| 830 if( dvd->isImageFile ) { | |
| 831 return DVDOpenVOBUDF( dvd, titlenum, 1 ); | |
| 832 } else { | |
| 833 return DVDOpenVOBPath( dvd, titlenum, 1 ); | |
| 834 } | |
| 835 break; | |
| 836 case DVD_READ_TITLE_VOBS: | |
| 837 if( titlenum == 0 ) return 0; | |
| 838 if( dvd->isImageFile ) { | |
| 839 return DVDOpenVOBUDF( dvd, titlenum, 0 ); | |
| 840 } else { | |
| 841 return DVDOpenVOBPath( dvd, titlenum, 0 ); | |
| 842 } | |
| 843 break; | |
| 844 default: | |
| 845 fprintf( stderr, "libdvdread: Invalid domain for file open.\n" ); | |
| 846 return NULL; | |
| 847 } | |
| 848 | |
| 849 if( dvd->isImageFile ) { | |
| 850 return DVDOpenFileUDF( dvd, filename ); | |
| 851 } else { | |
| 852 return DVDOpenFilePath( dvd, filename ); | |
| 853 } | |
| 854 } | |
| 855 | |
| 856 void DVDCloseFile( dvd_file_t *dvd_file ) | |
| 857 { | |
| 858 int i; | |
| 859 | |
| 860 if( dvd_file ) { | |
| 861 if( dvd_file->dvd->isImageFile ) { | |
| 862 ; | |
| 863 } else { | |
| 864 for( i = 0; i < 9; ++i ) { | |
| 865 if( dvd_file->title_devs[ i ] ) { | |
| 866 dvdinput_close( dvd_file->title_devs[i] ); | |
| 867 } | |
| 868 } | |
| 869 } | |
| 870 | |
| 871 free( dvd_file ); | |
| 872 dvd_file = 0; | |
| 873 } | |
| 874 } | |
| 875 | |
| 876 /* Internal, but used from dvd_udf.c */ | |
| 877 int UDFReadBlocksRaw( dvd_reader_t *device, uint32_t lb_number, | |
| 878 size_t block_count, unsigned char *data, | |
| 879 int encrypted ) | |
| 880 { | |
| 881 int ret; | |
| 882 if( !device->dev ) { | |
| 883 fprintf( stderr, "libdvdread: Fatal error in block read.\n" ); | |
| 884 return 0; | |
| 885 } | |
| 886 | |
| 887 ret = dvdinput_seek( device->dev, (int) lb_number ); | |
| 888 if( ret != (int) lb_number ) { | |
| 889 fprintf( stderr, "libdvdread: Can't seek to block %u\n", lb_number ); | |
| 890 return 0; | |
| 891 } | |
| 892 | |
| 893 ret = dvdinput_read( device->dev, (char *) data, | |
| 894 (int) block_count, encrypted ); | |
| 895 return ret; | |
| 896 } | |
| 897 | |
| 898 /* This is using a single input and starting from 'dvd_file->lb_start' offset. | |
| 899 * | |
| 900 * Reads 'block_count' blocks from 'dvd_file' at block offset 'offset' | |
| 901 * into the buffer located at 'data' and if 'encrypted' is set | |
| 902 * descramble the data if it's encrypted. Returning either an | |
| 903 * negative error or the number of blocks read. */ | |
| 904 static int DVDReadBlocksUDF( dvd_file_t *dvd_file, uint32_t offset, | |
| 905 size_t block_count, unsigned char *data, | |
| 906 int encrypted ) | |
| 907 { | |
| 908 return UDFReadBlocksRaw( dvd_file->dvd, dvd_file->lb_start + offset, | |
| 909 block_count, data, encrypted ); | |
| 910 } | |
| 911 | |
| 912 /* This is using possibly several inputs and starting from an offset of '0'. | |
| 913 * | |
| 914 * Reads 'block_count' blocks from 'dvd_file' at block offset 'offset' | |
| 915 * into the buffer located at 'data' and if 'encrypted' is set | |
| 916 * descramble the data if it's encrypted. Returning either an | |
| 917 * negative error or the number of blocks read. */ | |
| 918 static int DVDReadBlocksPath( dvd_file_t *dvd_file, unsigned int offset, | |
| 919 size_t block_count, unsigned char *data, | |
| 920 int encrypted ) | |
| 921 { | |
| 922 int i; | |
| 923 int ret, ret2, off; | |
| 924 | |
| 925 ret = 0; | |
| 926 ret2 = 0; | |
| 927 for( i = 0; i < 9; ++i ) { | |
| 928 if( !dvd_file->title_sizes[ i ] ) return 0; /* Past end of file */ | |
| 929 | |
| 930 if( offset < dvd_file->title_sizes[ i ] ) { | |
| 931 if( ( offset + block_count ) <= dvd_file->title_sizes[ i ] ) { | |
| 932 off = dvdinput_seek( dvd_file->title_devs[ i ], (int)offset ); | |
| 933 if( off < 0 || off != (int)offset ) { | |
| 934 fprintf( stderr, "libdvdread: Can't seek to block %d\n", | |
| 935 offset ); | |
| 936 return off < 0 ? off : 0; | |
| 937 } | |
| 938 ret = dvdinput_read( dvd_file->title_devs[ i ], data, | |
| 939 (int)block_count, encrypted ); | |
| 940 break; | |
| 941 } else { | |
| 942 size_t part1_size = dvd_file->title_sizes[ i ] - offset; | |
| 943 /* FIXME: Really needs to be a while loop. | |
| 944 * (This is only true if you try and read >1GB at a time) */ | |
| 945 | |
| 946 /* Read part 1 */ | |
| 947 off = dvdinput_seek( dvd_file->title_devs[ i ], (int)offset ); | |
| 948 if( off < 0 || off != (int)offset ) { | |
| 949 fprintf( stderr, "libdvdread: Can't seek to block %d\n", | |
| 950 offset ); | |
| 951 return off < 0 ? off : 0; | |
| 952 } | |
| 953 ret = dvdinput_read( dvd_file->title_devs[ i ], data, | |
| 954 (int)part1_size, encrypted ); | |
| 955 if( ret < 0 ) return ret; | |
| 956 /* FIXME: This is wrong if i is the last file in the set. | |
| 957 * also error from this read will not show in ret. */ | |
| 958 | |
| 959 /* Does the next part exist? If not then return now. */ | |
| 960 if( !dvd_file->title_devs[ i + 1 ] ) return ret; | |
| 961 | |
| 962 /* Read part 2 */ | |
| 963 off = dvdinput_seek( dvd_file->title_devs[ i + 1 ], 0 ); | |
| 964 if( off < 0 || off != 0 ) { | |
| 965 fprintf( stderr, "libdvdread: Can't seek to block %d\n", | |
| 966 0 ); | |
| 967 return off < 0 ? off : 0; | |
| 968 } | |
| 969 ret2 = dvdinput_read( dvd_file->title_devs[ i + 1 ], | |
| 970 data + ( part1_size | |
| 971 * (int64_t)DVD_VIDEO_LB_LEN ), | |
| 972 (int)(block_count - part1_size), | |
| 973 encrypted ); | |
| 974 if( ret2 < 0 ) return ret2; | |
| 975 break; | |
| 976 } | |
| 977 } else { | |
| 978 offset -= dvd_file->title_sizes[ i ]; | |
| 979 } | |
| 980 } | |
| 981 | |
| 982 return ret + ret2; | |
| 983 } | |
| 984 | |
| 985 /* This is broken reading more than 2Gb at a time is ssize_t is 32-bit. */ | |
| 986 ssize_t DVDReadBlocks( dvd_file_t *dvd_file, int offset, | |
| 987 size_t block_count, unsigned char *data ) | |
| 988 { | |
| 989 int ret; | |
| 990 /* Check arguments. */ | |
| 991 if( dvd_file == NULL || offset < 0 || data == NULL ) | |
| 992 return -1; | |
| 993 | |
| 994 /* Hack, and it will still fail for multiple opens in a threaded app ! */ | |
| 995 if( dvd_file->dvd->css_title != dvd_file->css_title ) { | |
| 996 dvd_file->dvd->css_title = dvd_file->css_title; | |
| 997 if( dvd_file->dvd->isImageFile ) { | |
| 998 dvdinput_title( dvd_file->dvd->dev, (int)dvd_file->lb_start ); | |
| 999 } | |
| 1000 /* Here each vobu has it's own dvdcss handle, so no need to update | |
| 1001 else { | |
| 1002 dvdinput_title( dvd_file->title_devs[ 0 ], (int)dvd_file->lb_start ); | |
| 1003 }*/ | |
| 1004 } | |
| 1005 | |
| 1006 if( dvd_file->dvd->isImageFile ) { | |
| 1007 ret = DVDReadBlocksUDF( dvd_file, (uint32_t)offset, | |
| 1008 block_count, data, DVDINPUT_READ_DECRYPT ); | |
| 1009 } else { | |
| 1010 ret = DVDReadBlocksPath( dvd_file, (unsigned int)offset, | |
| 1011 block_count, data, DVDINPUT_READ_DECRYPT ); | |
| 1012 } | |
| 1013 | |
| 1014 return (ssize_t)ret; | |
| 1015 } | |
| 1016 | |
| 1017 int32_t DVDFileSeek( dvd_file_t *dvd_file, int32_t offset ) | |
| 1018 { | |
| 1019 /* Check arguments. */ | |
| 1020 if( dvd_file == NULL || offset < 0 ) | |
| 1021 return -1; | |
| 1022 | |
| 1023 if( offset > dvd_file->filesize * DVD_VIDEO_LB_LEN ) { | |
| 1024 return -1; | |
| 1025 } | |
| 1026 dvd_file->seek_pos = (uint32_t) offset; | |
| 1027 return offset; | |
| 1028 } | |
| 1029 | |
| 1030 ssize_t DVDReadBytes( dvd_file_t *dvd_file, void *data, size_t byte_size ) | |
| 1031 { | |
| 1032 unsigned char *secbuf_base, *secbuf; | |
| 1033 unsigned int numsec, seek_sector, seek_byte; | |
| 1034 int ret; | |
| 1035 | |
| 1036 /* Check arguments. */ | |
| 1037 if( dvd_file == NULL || data == NULL ) | |
| 1038 return -1; | |
| 1039 | |
| 1040 seek_sector = dvd_file->seek_pos / DVD_VIDEO_LB_LEN; | |
| 1041 seek_byte = dvd_file->seek_pos % DVD_VIDEO_LB_LEN; | |
| 1042 | |
| 1043 numsec = ( ( seek_byte + byte_size ) / DVD_VIDEO_LB_LEN ) + | |
| 1044 ( ( ( seek_byte + byte_size ) % DVD_VIDEO_LB_LEN ) ? 1 : 0 ); | |
| 1045 | |
| 1046 secbuf_base = (unsigned char *) malloc( numsec * DVD_VIDEO_LB_LEN + 2048 ); | |
|
242
f794e1c17947
porting AMD64 patches from xine (provided by Goetz Waschk and Gwenole Beauchesne
mroi
parents:
233
diff
changeset
|
1047 secbuf = (unsigned char *)(((uintptr_t)secbuf_base & ~((uintptr_t)2047)) + 2048); |
| 225 | 1048 if( !secbuf_base ) { |
| 1049 fprintf( stderr, "libdvdread: Can't allocate memory " | |
| 1050 "for file read!\n" ); | |
| 1051 return 0; | |
| 1052 } | |
| 1053 | |
| 1054 if( dvd_file->dvd->isImageFile ) { | |
| 1055 ret = DVDReadBlocksUDF( dvd_file, (uint32_t) seek_sector, | |
| 1056 (size_t) numsec, secbuf, DVDINPUT_NOFLAGS ); | |
| 1057 } else { | |
| 1058 ret = DVDReadBlocksPath( dvd_file, seek_sector, | |
| 1059 (size_t) numsec, secbuf, DVDINPUT_NOFLAGS ); | |
| 1060 } | |
| 1061 | |
| 1062 if( ret != (int) numsec ) { | |
| 1063 free( secbuf_base ); | |
| 1064 return ret < 0 ? ret : 0; | |
| 1065 } | |
| 1066 | |
| 1067 memcpy( data, &(secbuf[ seek_byte ]), byte_size ); | |
| 1068 free( secbuf_base ); | |
| 1069 | |
| 1070 dvd_file->seek_pos += byte_size; | |
| 1071 return byte_size; | |
| 1072 } | |
| 1073 | |
| 1074 ssize_t DVDFileSize( dvd_file_t *dvd_file ) | |
| 1075 { | |
| 1076 /* Check arguments. */ | |
| 1077 if( dvd_file == NULL ) | |
| 1078 return -1; | |
| 1079 | |
| 1080 return dvd_file->filesize; | |
| 1081 } | |
| 1082 | |
| 1083 int DVDDiscID( dvd_reader_t *dvd, unsigned char *discid ) | |
| 1084 { | |
| 1085 struct md5_ctx ctx; | |
| 1086 int title; | |
|
323
d132820611d3
fail DVDDiscID if no ifo files are found. ogle-1766
nicodvb
parents:
299
diff
changeset
|
1087 int nr_of_files = 0; |
| 225 | 1088 |
| 1089 /* Check arguments. */ | |
| 1090 if( dvd == NULL || discid == NULL ) | |
| 1091 return 0; | |
| 1092 | |
| 1093 /* Go through the first 10 IFO:s, in order, | |
| 1094 * and md5sum them, i.e VIDEO_TS.IFO and VTS_0?_0.IFO */ | |
| 1095 md5_init_ctx( &ctx ); | |
| 1096 for( title = 0; title < 10; title++ ) { | |
| 1097 dvd_file_t *dvd_file = DVDOpenFile( dvd, title, DVD_READ_INFO_FILE ); | |
| 1098 if( dvd_file != NULL ) { | |
| 1099 ssize_t bytes_read; | |
| 1100 size_t file_size = dvd_file->filesize * DVD_VIDEO_LB_LEN; | |
| 1101 char *buffer_base = malloc( file_size + 2048 ); | |
|
242
f794e1c17947
porting AMD64 patches from xine (provided by Goetz Waschk and Gwenole Beauchesne
mroi
parents:
233
diff
changeset
|
1102 char *buffer = (unsigned char *)(((uintptr_t)buffer_base & ~((uintptr_t)2047)) + 2048); |
| 225 | 1103 |
| 1104 if( buffer_base == NULL ) { | |
| 1105 fprintf( stderr, "libdvdread: DVDDiscId, failed to " | |
| 1106 "allocate memory for file read!\n" ); | |
| 1107 return -1; | |
| 1108 } | |
| 1109 bytes_read = DVDReadBytes( dvd_file, buffer, file_size ); | |
| 1110 if( bytes_read != file_size ) { | |
|
242
f794e1c17947
porting AMD64 patches from xine (provided by Goetz Waschk and Gwenole Beauchesne
mroi
parents:
233
diff
changeset
|
1111 fprintf( stderr, "libdvdread: DVDDiscId read returned %zd bytes" |
|
f794e1c17947
porting AMD64 patches from xine (provided by Goetz Waschk and Gwenole Beauchesne
mroi
parents:
233
diff
changeset
|
1112 ", wanted %zd\n", bytes_read, file_size ); |
| 225 | 1113 DVDCloseFile( dvd_file ); |
| 1114 free( buffer_base ); | |
| 1115 return -1; | |
| 1116 } | |
| 1117 | |
| 1118 md5_process_bytes( buffer, file_size, &ctx ); | |
| 1119 | |
| 1120 DVDCloseFile( dvd_file ); | |
| 1121 free( buffer_base ); | |
|
323
d132820611d3
fail DVDDiscID if no ifo files are found. ogle-1766
nicodvb
parents:
299
diff
changeset
|
1122 nr_of_files++; |
| 225 | 1123 } |
| 1124 } | |
| 1125 md5_finish_ctx( &ctx, discid ); | |
|
323
d132820611d3
fail DVDDiscID if no ifo files are found. ogle-1766
nicodvb
parents:
299
diff
changeset
|
1126 if(!nr_of_files) |
|
d132820611d3
fail DVDDiscID if no ifo files are found. ogle-1766
nicodvb
parents:
299
diff
changeset
|
1127 return -1; |
| 225 | 1128 |
| 1129 return 0; | |
| 1130 } | |
| 1131 | |
| 1132 | |
| 1133 int DVDISOVolumeInfo( dvd_reader_t *dvd, | |
| 1134 char *volid, unsigned int volid_size, | |
| 1135 unsigned char *volsetid, unsigned int volsetid_size ) | |
| 1136 { | |
| 1137 unsigned char *buffer, *buffer_base; | |
| 1138 int ret; | |
| 1139 | |
| 1140 /* Check arguments. */ | |
| 1141 if( dvd == NULL ) | |
| 1142 return 0; | |
| 1143 | |
| 1144 if( dvd->dev == NULL ) { | |
| 1145 /* No block access, so no ISO... */ | |
| 1146 return -1; | |
| 1147 } | |
| 1148 | |
| 1149 buffer_base = malloc( DVD_VIDEO_LB_LEN + 2048 ); | |
|
242
f794e1c17947
porting AMD64 patches from xine (provided by Goetz Waschk and Gwenole Beauchesne
mroi
parents:
233
diff
changeset
|
1150 buffer = (unsigned char *)(((uintptr_t)buffer_base & ~((uintptr_t)2047)) + 2048); |
| 225 | 1151 |
| 1152 if( buffer_base == NULL ) { | |
| 1153 fprintf( stderr, "libdvdread: DVDISOVolumeInfo, failed to " | |
| 1154 "allocate memory for file read!\n" ); | |
| 1155 return -1; | |
| 1156 } | |
| 1157 | |
| 1158 ret = UDFReadBlocksRaw( dvd, 16, 1, buffer, 0 ); | |
| 1159 if( ret != 1 ) { | |
| 1160 fprintf( stderr, "libdvdread: DVDISOVolumeInfo, failed to " | |
| 1161 "read ISO9660 Primary Volume Descriptor!\n" ); | |
| 1162 free( buffer_base ); | |
| 1163 return -1; | |
| 1164 } | |
| 1165 | |
| 1166 if( (volid != NULL) && (volid_size > 0) ) { | |
| 1167 unsigned int n; | |
| 1168 for(n = 0; n < 32; n++) { | |
| 1169 if(buffer[40+n] == 0x20) { | |
| 1170 break; | |
| 1171 } | |
| 1172 } | |
| 1173 | |
| 1174 if(volid_size > n+1) { | |
| 1175 volid_size = n+1; | |
| 1176 } | |
| 1177 | |
| 1178 memcpy(volid, &buffer[40], volid_size-1); | |
| 1179 volid[volid_size-1] = '\0'; | |
| 1180 } | |
| 1181 | |
| 1182 if( (volsetid != NULL) && (volsetid_size > 0) ) { | |
| 1183 if(volsetid_size > 128) { | |
| 1184 volsetid_size = 128; | |
| 1185 } | |
| 1186 memcpy(volsetid, &buffer[190], volsetid_size); | |
| 1187 } | |
| 1188 free( buffer_base ); | |
| 1189 return 0; | |
| 1190 } | |
| 1191 | |
| 1192 | |
| 1193 int DVDUDFVolumeInfo( dvd_reader_t *dvd, | |
| 1194 char *volid, unsigned int volid_size, | |
| 1195 unsigned char *volsetid, unsigned int volsetid_size ) | |
| 1196 { | |
| 1197 int ret; | |
| 1198 /* Check arguments. */ | |
| 1199 if( dvd == NULL ) | |
| 1200 return -1; | |
| 1201 | |
| 1202 if( dvd->dev == NULL ) { | |
| 1203 /* No block access, so no UDF VolumeSet Identifier */ | |
| 1204 return -1; | |
| 1205 } | |
| 1206 | |
| 1207 if( (volid != NULL) && (volid_size > 0) ) { | |
| 1208 ret = UDFGetVolumeIdentifier(dvd, volid, volid_size); | |
| 1209 if(!ret) { | |
| 1210 return -1; | |
| 1211 } | |
| 1212 } | |
| 1213 if( (volsetid != NULL) && (volsetid_size > 0) ) { | |
| 1214 ret = UDFGetVolumeSetIdentifier(dvd, volsetid, volsetid_size); | |
| 1215 if(!ret) { | |
| 1216 return -1; | |
| 1217 } | |
| 1218 } | |
| 1219 | |
| 1220 return 0; | |
| 1221 } |
