comparison libdvdread/dvd_reader.c @ 367:1274107d0eac src

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