Debian Codegraph @

src/dvd_reader.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /*
2 * Copyright (C) 2001-2004 Billy Biggs <[email protected]>,
3 * Håkan Hjort <[email protected]>,
4 * Björn Englund <[email protected]>
5 *
6 * This file is part of libdvdread.
7 *
8 * libdvdread is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * libdvdread is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with libdvdread; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23 #include "config.h"
24 #include <sys/time.h> /* For the timing of dvdcss_title crack. */
25 #include <stdlib.h> /* free */
26 #include <stdio.h> /* fprintf */
27 #include <errno.h> /* errno, EIN* */
28 #include <string.h> /* memcpy, strlen */
29 #include <unistd.h> /* pclose */
30 #include <limits.h> /* PATH_MAX */
31 #include <ctype.h> /* isalpha */
32
33 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__bsdi__) || defined(__APPLE__)
34 # define SYS_BSD 1
35 #endif
36
37 #if defined(__sun)
38 # include <sys/mnttab.h>
39 #elif defined(__APPLE__)
40 # include <sys/param.h>
41 # include <sys/ucred.h>
42 # include <sys/mount.h>
43 #elif defined(SYS_BSD)
44 # include <fstab.h>
45 #elif defined(__linux__)
46 # include <mntent.h>
47 # include <paths.h>
48 #endif
49
50 #include "dvdread/dvd_udf.h"
51 #include "dvdread/dvd_reader.h"
52 #include "dvd_input.h"
53 #include "dvdread_internal.h"
54 #include "md5.h"
55 #include "dvdread/ifo_read.h"
56 #include "file/filesystem.h"
57
58 #define BYTES_TO_DVD_BLOCKS_CEIL(bytes) \
59 (((uint64_t)(bytes) + DVD_VIDEO_LB_LEN - 1) / DVD_VIDEO_LB_LEN)
60
61 #if defined(_MSC_VER)
62 // sys/stat.h values
63 #define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
64 #define S_ISBLK(m) 0
65 #define S_ISCHR(m) 0
66
67 #define PATH_MAX _MAX_PATH
68 #endif
69
70 /* misc win32 helpers */
71
72 #ifdef _WIN32
73 # ifndef HAVE_GETTIMEOFDAY
74 /* replacement gettimeofday implementation */
75 # include <sys/timeb.h>
76 static inline int _private_gettimeofday( struct timeval *tv, void *tz )
77 {
78 struct timeb t;
79 ftime( &t );
80 tv->tv_sec = t.time;
81 tv->tv_usec = t.millitm * 1000;
82 return 0;
83 }
84 # define gettimeofday(TV, TZ) _private_gettimeofday((TV), (TZ))
85 # endif
86 #endif /* _WIN32 */
87
•88 #ifndef PATH_MAX
89 #define PATH_MAX 4096
90 #endif
91
92 #define DEFAULT_UDF_CACHE_LEVEL 1
93
94 struct dvd_reader_device_s {
95 /* Basic information. */
96 int isImageFile;
97
98 /* Hack for keeping track of the css status.
99 * 0: no css, 1: perhaps (need init of keys), 2: have done init */
100 int css_state;
101 int css_title; /* Last title that we have called dvdinpute_title for. */
102
103 /* Information required for an image file. */
104 dvd_input_t dev;
105
106 /* Information required for a directory path drive. */
107 char *path_root;
108
109 /* Filesystem cache */
110 int udfcache_level; /* 0 - turned off, 1 - on */
111 void *udfcache;
112 };
113
114 #define TITLES_MAX 9
115
116 struct dvd_file_s {
117 /* Basic information. */
118 dvd_reader_t *ctx;
119
120 /* Hack for selecting the right css title. */
121 int css_title;
122
123 /* Information required for an image file. */
124 uint32_t lb_start;
125 uint32_t seek_pos;
126
127 /* Information required for a directory path drive. */
128 size_t title_sizes[ TITLES_MAX ];
129 dvd_input_t title_devs[ TITLES_MAX ];
130
131 /* Calculated at open-time, size in blocks. */
132 ssize_t filesize;
133
134 /* Cache of the dvd_file. If not NULL, the cache corresponds to the whole
135 * dvd_file. Used only for IFO and BUP. */
136 unsigned char *cache;
137 };
138
139 /**
140 * Set the level of caching on udf
141 * level = 0 (no caching)
142 * level = 1 (caching filesystem info)
143 */
144 int DVDUDFCacheLevel(dvd_reader_t *reader, int level)
145 {
146 dvd_reader_device_t *dev = reader->rd;
147
148 if(level > 0) {
149 level = 1;
150 } else if(level < 0) {
151 return dev->udfcache_level;
152 }
153
154 dev->udfcache_level = level;
155
156 return level;
157 }
158
159 void *GetUDFCacheHandle(dvd_reader_t *reader)
160 {
161 dvd_reader_device_t *dev = reader->rd;
162
163 return dev->udfcache;
164 }
165
166 void SetUDFCacheHandle(dvd_reader_t *reader, void *cache)
167 {
168 dvd_reader_device_t *dev = reader->rd;
169
170 dev->udfcache = cache;
171 }
172
173
174
175 /* Loop over all titles and call dvdcss_title to crack the keys. */
176 static int initAllCSSKeys( dvd_reader_t *ctx )
177 {
178 dvd_reader_device_t *dvd = ctx->rd;
179 struct timeval all_s, all_e;
180 struct timeval t_s, t_e;
181 char filename[ MAX_UDF_FILE_NAME_LEN ];
182 uint32_t start, len;
183 int title;
184
185 const char *nokeys_str = getenv("DVDREAD_NOKEYS");
186 if(nokeys_str != NULL)
187 return 0;
188
189 Log2(ctx,"Attempting to retrieve all CSS keys" );
190 Log2(ctx,"This can take a _long_ time, please be patient" );
191 gettimeofday(&all_s, NULL);
192
193 for( title = 0; title < 100; title++ ) {
194 gettimeofday( &t_s, NULL );
195 if( title == 0 ) {
196 strcpy( filename, "/VIDEO_TS/VIDEO_TS.VOB" );
197 } else {
198 sprintf( filename, "/VIDEO_TS/VTS_%02d_%d.VOB", title, 0 );
199 }
200 start = UDFFindFile( ctx, filename, &len );
201 if( start != 0 && len != 0 ) {
202 /* Perform CSS key cracking for this title. */
203 Log3(ctx,"Get key for %s at 0x%08x",filename, start );
204 if( dvdinput_title( dvd->dev, (int)start ) < 0 ) {
205 Log1(ctx,"Error cracking CSS key for %s (0x%08x)", filename, start);
206 }
207 gettimeofday( &t_e, NULL );
208 Log3(ctx,"Elapsed time %ld", (long int) t_e.tv_sec - t_s.tv_sec );
209 }
210
211 if( title == 0 ) continue;
212
213 gettimeofday( &t_s, NULL );
214 sprintf( filename, "/VIDEO_TS/VTS_%02d_%d.VOB", title, 1 );
215 start = UDFFindFile( ctx, filename, &len );
216 if( start == 0 || len == 0 ) break;
217
218 /* Perform CSS key cracking for this title. */
219 Log3(ctx,"Get key for %s at 0x%08x",filename, start );
220 if( dvdinput_title( dvd->dev, (int)start ) < 0 ) {
221 Log1(ctx,"Error cracking CSS key for %s (0x%08x)", filename, start);
222 }
223 gettimeofday( &t_e, NULL );
224 Log3(ctx,"Elapsed time %ld", (long int) t_e.tv_sec - t_s.tv_sec );
225 }
226 title--;
227
228 Log3(ctx,"Found %d VTS's", title );
229 gettimeofday(&all_e, NULL);
230 Log3(ctx,"Elapsed time %ld", (long int) all_e.tv_sec - all_s.tv_sec );
231
232 return 0;
233 }
234
235
236
237 /**
238 * Open a DVD image or block device file or use stream_cb functions.
239 */
240 static dvd_reader_device_t *DVDOpenImageFile( dvd_reader_t *ctx,
241 const char *location,
242 dvd_reader_stream_cb *stream_cb,
243 int have_css )
244 {
245 dvd_reader_device_t *dvd;
246 dvd_input_t dev;
247
248 dev = dvdinput_open( ctx->priv, &ctx->logcb, location, stream_cb, ctx->fs );
249 if( !dev ) {
250 Log0(ctx,"Can't open %s for reading", location );
251 return NULL;
252 }
253
254 dvd = calloc( 1, sizeof( dvd_reader_device_t ) );
255 if( !dvd ) {
256 dvdinput_close(dev);
257 return NULL;
258 }
259 dvd->isImageFile = 1;
260 dvd->dev = dev;
261
262 dvd->udfcache_level = DEFAULT_UDF_CACHE_LEVEL;
263
264 if( have_css ) {
265 /* Only if DVDCSS_METHOD = title, a bit if it's disc or if
266 * DVDCSS_METHOD = key but region mismatch. Unfortunately we
267 * don't have that information. */
268
269 dvd->css_state = 1; /* Need key init. */
270 }
271
272 return dvd;
273 }
274
275 static dvd_reader_device_t *DVDOpenPath( const char *path_root )
276 {
277 dvd_reader_device_t *dvd;
278
279 dvd = calloc( 1, sizeof( dvd_reader_device_t ) );
280 if( !dvd ) return NULL;
281 dvd->path_root = strdup( path_root );
282 if(!dvd->path_root) {
283 free(dvd);
284 return NULL;
285 }
286 dvd->udfcache_level = DEFAULT_UDF_CACHE_LEVEL;
287
288 return dvd;
289 }
290
291 #if defined(__sun)
292 /* /dev/rdsk/c0t6d0s0 (link to /devices/...)
293 /vol/dev/rdsk/c0t6d0/??
294 /vol/rdsk/<name> */
295 static char *sun_block2char( const char *path )
296 {
297 char *new_path;
298
299 /* Must contain "/dsk/" */
300 if( !strstr( path, "/dsk/" ) ) return (char *) strdup( path );
301
302 /* Replace "/dsk/" with "/rdsk/" */
303 new_path = malloc( strlen(path) + 2 );
304 if(!new_path) return NULL;
305 strcpy( new_path, path );
306 strcpy( strstr( new_path, "/dsk/" ), "" );
307 strcat( new_path, "/rdsk/" );
308 strcat( new_path, strstr( path, "/dsk/" ) + strlen( "/dsk/" ) );
309
310 return new_path;
311 }
312 #endif
313
314 #if defined(SYS_BSD)
315 /* FreeBSD /dev/(r)(a)cd0c (a is for atapi), recommended to _not_ use r
316 update: FreeBSD and DragonFly no longer uses the prefix so don't add it.
317 OpenBSD /dev/rcd0c, it needs to be the raw device
318 NetBSD /dev/rcd0[d|c|..] d for x86, c (for non x86), perhaps others
319 Darwin /dev/rdisk0, it needs to be the raw device
320 BSD/OS /dev/sr0c (if not mounted) or /dev/rsr0c ('c' any letter will do)
321 returns a string allocated with strdup. It should be freed when no longer
322 used. */
323 static char *bsd_block2char( const char *path )
324 {
325 #if defined(__FreeBSD__) || defined(__DragonFly__)
326 return (char *) strdup( path );
327 #else
328 char *new_path;
329
330 /* If it doesn't start with "/dev/" or does start with "/dev/r" exit */
331 if( strncmp( path, "/dev/", 5 ) || !strncmp( path, "/dev/r", 6 ) )
332 return (char *) strdup( path );
333
334 /* Replace "/dev/" with "/dev/r" */
335 new_path = malloc( strlen(path) + 2 );
336 if(!new_path) return NULL;
337 strcpy( new_path, "/dev/r" );
338 strcat( new_path, path + strlen( "/dev/" ) );
339
340 return new_path;
341 #endif /* __FreeBSD__ || __DragonFly__ */
342 }
343 #endif
344
345 /* function is called on encrypted DVD-Audio discs */
346 /* with the cppm encryption scheme */
347 static uint8_t *cppm_get_mkb_or_backup( dvd_reader_t *ctx, int backup );
348
349 /* Since the dvd needs to be open in order to read the MKB ( media key block ) it was neccesary to split cpxm initilization into two steps, with one occuring after it's opened */
350 /* this should only be called if libdvdcss is available and if the disc type is DVD-Audio */
351 static int cpxm_init_condition( dvd_reader_t* ctx, dvd_type_t type, int have_css )
352 {
353 if ( !ctx->rd->dev )
354 return 0;
355
356 if ( type == DVD_A && have_css )
357 {
358 uint8_t *p_mkb = NULL;
359 p_mkb = cppm_get_mkb_or_backup( ctx, 0 );
360 if ( !p_mkb )
361 p_mkb = cppm_get_mkb_or_backup( ctx, 1 );
362 if ( !p_mkb )
363 Log2(ctx, "There is no MKB on this DVD-Audio disc, so there likely no encryption");
364 return dvdinput_init( ctx->rd->dev, p_mkb );
365 } else if ( type == DVD_VR && have_css ) {
366 /* open ifo to supply decryption context */
367 ifo_handle_t* ifo = ifoOpen( ctx, 0 );
368 if( !ifo )
369 return 0;
370 if( !ifo->rtav_vmgi ) {
371 ifoClose(ifo);
372 return 0;
373 }
374 rtav_vmgi_t* rtav_vmgi = ifo->rtav_vmgi;
375 /* pass the cprm title key to libdvdcss for decryption */
376 int ret = dvdinput_init( ctx->rd->dev, rtav_vmgi->cprm_info.title_key );
377 ifoClose(ifo);
378 return ret;
379 } else
380 return 0;
381 }
382
383 /* not for the DVDOpenFiles filesystem which the caller owns */
384 static dvd_reader_t *DVDFreeContext( dvd_reader_t *ctx )
385 {
386 if( ctx ) {
387 if( ctx->fs )
388 ctx->fs->close( ctx->fs );
389 free( ctx );
390 }
391 return NULL;
392 }
393
394 /* like findDirFile but quiet, used to probe the disc type */
395 static int dir_has_file( dvd_reader_t *ctx, const char *subdir, const char *name )
396 {
397 char path[ PATH_MAX + 1 ];
398 dvd_dirent_t entry;
399 int found = 0;
400 void *dir;
401
402 snprintf( path, sizeof(path), "%s/%s", ctx->rd->path_root, subdir );
403 dir = ctx->fs->dir_open( ctx->fs, path );
404 if( !dir )
405 return 0;
406 for( ;; ) {
407 int r = ctx->fs->dir_read( dir, &entry );
408 if( r != 0 )
409 break;
410 if( !strcasecmp( entry.d_name, name ) ) {
411 found = 1;
412 break;
413 }
414 }
415 ctx->fs->dir_close( dir );
416 return found;
417 }
418
419 /* like dir_has_file but also tries the lowercase spelling of subdir, as findDVDFile does */
420 static int disc_dir_has( dvd_reader_t *ctx, const char *subdir, const char *name )
421 {
422 char low[ 16 ];
423 size_t i;
424
425 if( dir_has_file( ctx, subdir, name ) )
426 return 1;
427 for( i = 0; subdir[i] && i + 1 < sizeof(low); i++ )
428 low[i] = tolower( (unsigned char)subdir[i] );
429 low[i] = 0;
430 return dir_has_file( ctx, low, name );
431 }
432
433 static dvd_reader_t *DVDOpenCommon( void *priv,
434 const dvd_logger_cb *logcb,
435 const char *ppath,
436 dvd_reader_stream_cb *stream_cb,
437 dvd_type_t type,
438 dvd_reader_filesystem_h *fs )
439 {
440 dvdstat_t fileinfo;
441 int ret, have_css, cdir = -1;
442 char *dev_name = NULL;
443 char *path = NULL, *new_path = NULL, *path_copy = NULL;
444 dvd_reader_t *ctx = calloc(1, sizeof(*ctx));
445 if(!ctx)
446 return NULL;
447 ctx->dvd_type = type;
448
449 ctx->priv = priv;
450 if(logcb)
451 ctx->logcb = *logcb;
452
453 // open files using the provided filesystem implementation
454 if (fs != NULL && ppath != NULL)
455 {
456 ctx->fs = fs;
457 dvdinput_setup_builtin(ctx->priv, &ctx->logcb);
458 /* DVDOpenFiles only opens a directory tree, so reject anything else */
459 if( ctx->fs->stat( ctx->fs, ppath, &fileinfo ) < 0 ||
460 ( fileinfo.st_mode & DVD_S_IFMT ) != DVD_S_IFDIR )
461 {
462 free(ctx);
463 return NULL;
464 }
465 ctx->rd = DVDOpenPath(ppath);
466 if (!ctx->rd)
467 {
468 free(ctx);
469 return NULL;
470 }
471 /* detect the disc type from the directory layout like DVDProbeType does */
472 if( disc_dir_has( ctx, "AUDIO_TS", "AUDIO_TS.IFO" ) &&
473 !disc_dir_has( ctx, "VIDEO_TS", "VIDEO_TS.IFO" ) )
474 ctx->dvd_type = DVD_A;
475 else if( disc_dir_has( ctx, "DVD_RTAV", "VR_MANGR.IFO" ) )
476 ctx->dvd_type = DVD_VR;
477 return ctx;
478 }
479
480 // create the internal filesystem
481 ctx->fs = InitInternalFilesystem();
482 if (!ctx->fs)
483 {
484 free(ctx);
485 return NULL;
486 }
487
488 /* Try to open DVD using stream_cb functions */
489 if( priv != NULL && stream_cb != NULL )
490 {
491 have_css = dvdinput_setup( ctx->priv, &ctx->logcb, type );
492 ctx->rd = DVDOpenImageFile( ctx, NULL, stream_cb, have_css );
493 if(!ctx->rd)
494 return DVDFreeContext(ctx);
495 cpxm_init_condition( ctx, type, have_css );
496 return ctx;
497 }
498
499 if( ppath == NULL )
500 goto DVDOpen_error;
501
502 path = strdup(ppath);
503 if( path == NULL )
504 goto DVDOpen_error;
505
506 /* Try to open libdvdcss or fall back to standard functions */
507 have_css = dvdinput_setup( ctx->priv, &ctx->logcb, type );
508
509 #if defined(_WIN32) || defined(__OS2__)
510 /* Strip off the trailing \ if it is not a drive */
511 size_t len = strlen(path);
512 if ((len > 1) &&
513 (path[len - 1] == '\\') &&
514 (path[len - 2] != ':'))
515 {
516 path[len-1] = '\0';
517 }
518 #endif
519
520 ret = ctx->fs->stat(ctx->fs, path, &fileinfo);
521
522 if( ret < 0 ) {
523
524 /* maybe "host:port" url? try opening it with acCeSS library */
525 if( strchr(path,':') ) {
526 ctx->rd = DVDOpenImageFile( ctx, path, NULL, have_css );
527 free(path);
528 if(!ctx->rd)
529 return DVDFreeContext(ctx);
530 cpxm_init_condition( ctx, type, have_css );
531 return ctx;
532 }
533
534 /* If we can't stat the file, give up */
535 Log0(ctx, "Can't stat %s", path );
536 perror("");
537 goto DVDOpen_error;
538 }
539
540 /* First check if this is a block/char device or a file*/
541 if( (fileinfo.st_mode & DVD_S_IFMT) == DVD_S_IFBLK ||
542 (fileinfo.st_mode & DVD_S_IFMT) == DVD_S_IFCHR ||
543 (fileinfo.st_mode & DVD_S_IFMT) == DVD_S_IFREG ) {
544
545 /**
546 * Block devices and regular files are assumed to be DVD-Video images.
547 */
548 #if defined(__sun)
549 dev_name = sun_block2char( path );
550 #elif defined(SYS_BSD)
551 dev_name = bsd_block2char( path );
552 #else
553 dev_name = strdup( path );
554 #endif
555 if(!dev_name)
556 goto DVDOpen_error;
557 ctx->rd = DVDOpenImageFile( ctx, dev_name, NULL, have_css );
558 free( dev_name );
559 free(path);
560 if(!ctx->rd)
561 return DVDFreeContext(ctx);
562 cpxm_init_condition( ctx, type, have_css );
563 return ctx;
564 } else if ((fileinfo.st_mode & DVD_S_IFMT) == DVD_S_IFDIR ) {
565 #if defined(SYS_BSD)
566 struct fstab* fe;
567 #elif defined(__sun) || defined(__linux__)
568 FILE *mntfile;
569 #endif
570
571 /* XXX: We should scream real loud here. */
572 if( !(path_copy = strdup( path ) ) )
573 goto DVDOpen_error;
574
575 #ifndef _WIN32 /* win32 doesn't have realpath */
576 /* Also WIN32 does not have symlinks, so we don't need this bit of code. */
577
578 /* Resolve any symlinks and get the absolute dir name. */
579 {
580 new_path = realpath( path_copy, NULL );
581 if( new_path == NULL ) {
582 goto DVDOpen_error;
583 }
584 free(path_copy);
585 path_copy = new_path;
586 new_path = NULL;
587 }
588 #endif
589
590 /**
591 * If we're being asked to open a directory, check if that directory
592 * is the mount point for a DVD-ROM which we can use instead.
593 */
594
595 if( strlen( path_copy ) > 1 ) {
596 if( path_copy[ strlen( path_copy ) - 1 ] == '/' ) {
597 path_copy[ strlen( path_copy ) - 1 ] = '\0';
598 }
599 }
600
601 #if defined(_WIN32) || defined(__OS2__)
602 if( strlen( path_copy ) > 9 ) {
603 if( !strcasecmp( &(path_copy[ strlen( path_copy ) - 9 ]),
604 "\\video_ts"))
605 path_copy[ strlen( path_copy ) - (9-1) ] = '\0';
606 }
607 #endif
608 if( strlen( path_copy ) > 9 ) {
609 if( !strcasecmp( &(path_copy[ strlen( path_copy ) - 9 ]),
610 "/video_ts" ) ) {
611 path_copy[ strlen( path_copy ) - 9 ] = '\0';
612 }
613 }
614
615 if(path_copy[0] == '\0') {
616 free( path_copy );
617 if( !(path_copy = strdup( "/" ) ) )
618 goto DVDOpen_error;
619 }
620
621 #if defined(__APPLE__)
622 struct statfs s[128];
623 int r = getfsstat(NULL, 0, MNT_NOWAIT);
624 if (r > 0) {
625 if (r > 128)
626 r = 128;
627 r = getfsstat(s, r * sizeof(s[0]), MNT_NOWAIT);
628 int i;
629 for (i=0; i<r; i++) {
630 if (!strcmp(path_copy, s[i].f_mntonname)) {
631 dev_name = bsd_block2char(s[i].f_mntfromname);
632 Log3(ctx, "Attempting to use device %s"
633 " mounted on %s for CSS authentication",
634 dev_name,
635 s[i].f_mntonname);
636 ctx->rd = DVDOpenImageFile( ctx, dev_name, NULL, have_css );
637 break;
638 }
639 }
640 }
641 #elif defined(SYS_BSD)
642 if( ( fe = getfsfile( path_copy ) ) ) {
643 dev_name = bsd_block2char( fe->fs_spec );
644 Log3(ctx, "Attempting to use device %s"
645 " mounted on %s for CSS authentication",
646 dev_name,
647 fe->fs_file );
648 ctx->rd = DVDOpenImageFile( ctx, dev_name, NULL, have_css );
649 }
650 #elif defined(__sun)
651 mntfile = fopen( MNTTAB, "r" );
652 if( mntfile ) {
653 struct mnttab mp;
654 int res;
655
656 while( ( res = getmntent( mntfile, &mp ) ) != -1 ) {
657 if( res == 0 && !strcmp( mp.mnt_mountp, path_copy ) ) {
658 dev_name = sun_block2char( mp.mnt_special );
659 Log3(ctx, "Attempting to use device %s"
660 " mounted on %s for CSS authentication",
661 dev_name,
662 mp.mnt_mountp );
663 ctx->rd = DVDOpenImageFile( ctx, dev_name, NULL, have_css );
664 break;
665 }
666 }
667 fclose( mntfile );
668 }
669 #elif defined(__linux__)
670 mntfile = fopen( _PATH_MOUNTED, "r" );
671 if( mntfile ) {
672
673 #ifdef HAVE_GETMNTENT_R
674 struct mntent *me, mbuf;
675 char buf [8192];
676 while( ( me = getmntent_r( mntfile, &mbuf, buf, sizeof(buf) ) ) ) {
677 #else
678 struct mntent *me;
679 while( ( me = getmntent( mntfile ) ) ) {
680 #endif
681 if( !strcmp( me->mnt_dir, path_copy ) ) {
682 Log3(ctx, "Attempting to use device %s"
683 " mounted on %s for CSS authentication",
684 me->mnt_fsname,
685 me->mnt_dir );
686 ctx->rd = DVDOpenImageFile( ctx, me->mnt_fsname, NULL, have_css );
687 dev_name = strdup(me->mnt_fsname);
688 break;
689 }
690 }
691 fclose( mntfile );
692 }
693 #elif defined(_WIN32) || defined(__OS2__)
694 #ifdef __OS2__
695 /* Use DVDOpenImageFile() only if it is a drive */
696 if(isalpha(path[0]) && path[1] == ':' &&
697 ( !path[2] ||
698 ((path[2] == '\\' || path[2] == '/') && !path[3])))
699 #endif
700 ctx->rd = DVDOpenImageFile( ctx, path, NULL, have_css );
701 #endif
702
703 #if !defined(_WIN32) && !defined(__OS2__)
704 if( !dev_name ) {
705 Log0(ctx, "Couldn't find device name." );
706 } else if( !ctx->rd ) {
707 Log0(ctx, "Device %s inaccessible, "
708 "CSS authentication not available.", dev_name );
709 }
710 #else
711 if( !ctx->rd ) {
712 Log0(ctx, "Device %s inaccessible, "
713 "CSS authentication not available.", path );
714 }
715 #endif
716
717 free( dev_name );
718 dev_name = NULL;
719 free( path_copy );
720 path_copy = NULL;
721
722 /**
723 * If we've opened a drive, just use that.
724 */
725 if(ctx->rd)
726 {
727 free(path);
728 cpxm_init_condition( ctx, type, have_css );
729 return ctx;
730 }
731 /**
732 * Otherwise, we now try to open the directory tree instead.
733 */
734 ctx->rd = DVDOpenPath( path );
735 free( path );
736 if(!ctx->rd)
737 return DVDFreeContext(ctx);
738 cpxm_init_condition( ctx, type, have_css );
739 return ctx;
740 }
741
742 DVDOpen_error:
743 /* If it's none of the above, screw it. */
744 Log0( ctx, "Could not open %s", path );
745 free( path );
746 free( path_copy );
747 if ( cdir >= 0 )
748 close( cdir );
749 free( new_path );
750 return DVDFreeContext( ctx );
751 }
752
753 /* opens the disc temporarily to peek at the file structure
754 * helps determine how to treat the disc (as DVD-VR, DVD-Audio or DVD-Video)
755 */
756 static dvd_type_t DVDProbeType( const char *ppath, void *stream,
757 dvd_reader_stream_cb *stream_cb )
758 {
759 /* set DVD_V as the fallback */
760 dvd_type_t ret = DVD_V;
761 int have_css;
762 uint32_t HAS_AUDIO_TS = 0, HAS_VIDEO_TS = 0, HAS_DVD_RTAV = 0;
763
764 /* open the disc temporarily for probing */
765 dvd_reader_t *ctx = calloc(1, sizeof(*ctx));
766 if(!ctx)
767 return ret;
768
769 /* the builtin input reads through ctx->fs so set it before probing */
770 ctx->fs = InitInternalFilesystem();
771 if(!ctx->fs) {
772 free(ctx);
773 return ret;
774 }
775
776 have_css = dvdinput_setup( ctx->priv, &ctx->logcb, DVD_V );
777
778 if (stream && stream_cb)
779 ctx->rd = DVDOpenImageFile( ctx, NULL, stream_cb, have_css );
780 else if (ppath)
781 ctx->rd = DVDOpenImageFile( ctx, ppath, NULL, have_css );
782
783 if (!ctx->rd) {
784 DVDFreeContext(ctx);
785 return ret;
786 }
787
788 /* check for ifos */
789 UDFFindFile( ctx, "/AUDIO_TS/AUDIO_TS.IFO", &HAS_AUDIO_TS );
790 UDFFindFile( ctx, "/DVD_RTAV/VR_MANGR.IFO", &HAS_DVD_RTAV );
791 UDFFindFile( ctx, "/VIDEO_TS/VIDEO_TS.IFO", &HAS_VIDEO_TS );
792
793 /* if the disc only has dvd-a, play that.
794 * if the disc has dvd-vr, play that
795 * otherwise default to dvd-v */
796 if ( HAS_AUDIO_TS && !HAS_VIDEO_TS )
797 ret = DVD_A;
798 else if ( HAS_DVD_RTAV )
799 ret = DVD_VR;
800
801 DVDClose(ctx);
802
803 return ret;
804 }
805
806 dvd_reader_t *DVDOpen( const char *ppath )
807 {
808 dvd_type_t type_flag = DVDProbeType( ppath, NULL, NULL );
809 return DVDOpenCommon( NULL, NULL, ppath, NULL, type_flag, NULL );
810 }
811
812 dvd_reader_t *DVDOpenStream( void *stream,
813 dvd_reader_stream_cb *stream_cb )
814 {
815 dvd_type_t type_flag = DVDProbeType( NULL, stream, stream_cb );
816 return DVDOpenCommon( stream, NULL, NULL, stream_cb, type_flag, NULL );
817 }
818
819 dvd_reader_t *DVDOpen2( void *priv, const dvd_logger_cb *logcb,
820 const char *ppath)
821 {
822 dvd_type_t type_flag = DVDProbeType( ppath, NULL, NULL );
823 return DVDOpenCommon( priv, logcb, ppath, NULL, type_flag, NULL );
824 }
825
826 dvd_reader_t *DVDOpenStream2( void *priv, const dvd_logger_cb *logcb,
827 dvd_reader_stream_cb *stream_cb )
828 {
829 /* Fix: Pass NULL for stream, do NOT pass priv as stream */
830 dvd_type_t type_flag = DVDProbeType( NULL, NULL, stream_cb );
831 return DVDOpenCommon( priv, logcb, NULL, stream_cb, type_flag, NULL );
832 }
833
834 dvd_reader_t *DVDOpenAudio( void *priv, const dvd_logger_cb *logcb,
835 const char *ppath )
836 {
837 return DVDOpenCommon( priv, logcb, ppath, NULL, DVD_A, NULL );
838 }
839
840 dvd_reader_t *DVDOpenStreamAudio( void *priv, const dvd_logger_cb *logcb,
841 dvd_reader_stream_cb *stream_cb )
842 {
843 return DVDOpenCommon( priv, logcb, NULL, stream_cb, DVD_A, NULL );
844 }
845
846 dvd_reader_t *DVDOpenVideoRecording( void *priv, const dvd_logger_cb *logcb,
847 const char *ppath )
848 {
849 return DVDOpenCommon( priv, logcb, ppath, NULL, DVD_VR, NULL );
850 }
851
852 dvd_reader_t *DVDOpenStreamVideoRecording( void *priv, const dvd_logger_cb *logcb,
853 dvd_reader_stream_cb *stream_cb )
854 {
855 return DVDOpenCommon( priv, logcb, NULL, stream_cb, DVD_VR, NULL );
856 }
857
858 dvd_reader_t *DVDOpenFiles( void *priv, const dvd_logger_cb *logcb,
859 const char *ppath, dvd_reader_filesystem_h *fs)
860 {
861 return DVDOpenCommon( priv, logcb, ppath, NULL, DVD_V, fs );
862 }
863
864 void DVDClose( dvd_reader_t *dvd )
865 {
866 if( dvd ) {
867 if( dvd->rd ) {
868 if( dvd->rd->dev ) dvdinput_close( dvd->rd->dev );
869 if( dvd->rd->path_root ) free( dvd->rd->path_root );
870 if( dvd->rd->udfcache ) FreeUDFCache( dvd->rd->udfcache );
871 free( dvd->rd );
872 }
873 if (dvd->fs) {
874 dvd->fs->close(dvd->fs);
875 }
876 free( dvd );
877 }
878 }
879
880 /**
881 * Open an unencrypted file on a DVD image file.
882 */
883 static dvd_file_t *DVDOpenFileUDF( dvd_reader_t *ctx, const char *filename,
884 int do_cache )
885 {
886 uint32_t start, len;
887 dvd_file_t *dvd_file;
888
889 start = UDFFindFile( ctx, filename, &len );
890 if( !start ) {
891 Log0( ctx, "DVDOpenFileUDF:UDFFindFile %s failed", filename );
892 return NULL;
893 }
894
895 dvd_file = calloc( 1, sizeof( dvd_file_t ) );
896 if( !dvd_file ) {
897 Log0( ctx, "DVDOpenFileUDF:malloc failed" );
898 return NULL;
899 }
900 dvd_file->ctx = ctx;
901 dvd_file->lb_start = start;
902 dvd_file->filesize = BYTES_TO_DVD_BLOCKS_CEIL(len);
903
904 /* Read the whole file in cache (unencrypted) if asked and if it doesn't
905 * exceed 128KB */
906 if( do_cache && len < 64 * DVD_VIDEO_LB_LEN ) {
907 int ret;
908 size_t cache_bytes = (size_t)dvd_file->filesize * DVD_VIDEO_LB_LEN;
909
910 dvd_file->cache = malloc( cache_bytes );
911 if( !dvd_file->cache )
912 return dvd_file;
913
914 ret = InternalUDFReadBlocksRaw( ctx, dvd_file->lb_start,
915 dvd_file->filesize, dvd_file->cache,
916 DVDINPUT_NOFLAGS );
917 if( ret != dvd_file->filesize ) {
918 free( dvd_file->cache );
919 dvd_file->cache = NULL;
920 }
921 }
922
923 return dvd_file;
924 }
925
926 /**
927 * Searches for <file> in directory <path>, ignoring case.
928 * Returns 0 and full filename in <filename>.
929 * or -1 on file not found.
930 * or -2 on path not found.
931 */
932 static int findDirFile(dvd_reader_t *ctx, const char *path, const char *file, char *filename )
933 {
934 dvd_dirent_t entry;
935 void *dir = ctx->fs->dir_open(ctx->fs, path);
936 if( !dir ) {
937 Log0(ctx, "findDirFile: Could not open dir %s ", path);
938 return -2;
939 }
940
941 int ret = -1;
942 for( ;; ) {
943 int result = ctx->fs->dir_read(dir, &entry);
944 if( result < 0 ) {
945 Log0(ctx, "findDirFile: Error reading dir %s (error: %d)", path, result);
946 break;
947 }
948 if( result > 0 )
949 break;
950 if( !strcasecmp( entry.d_name, file ) ) {
951 sprintf( filename, "%s%s%s", path,
952 ( ( path[ strlen( path ) - 1 ] == '/' ) ? "" : "/" ),
953 entry.d_name );
954 ret = 0;
955 break;
956 }
957 }
958
959 ctx->fs->dir_close(dir);
960 return ret;
961 }
962
963 static int findDVDFile( dvd_reader_t *dvd, const char *file, char *filename )
964 {
965 const char *nodirfile;
966 int ret;
967
968 /* Strip off the directory for our search */
969 if( !strncasecmp( (dvd->dvd_type == DVD_V) ? "/VIDEO_TS/" : "/AUDIO_TS/", file, 10 ) ) {
970 nodirfile = &(file[ 10 ]);
971 /* DVD-VR check */
972 } else if ( !strncasecmp( "/DVD_RTAV/", file, 10 ) ) {
973 nodirfile = &(file[ 10 ]);
974 } else {
975 nodirfile = file;
976 }
977
978 ret = findDirFile(dvd, dvd->rd->path_root, nodirfile, filename );
979 if( ret < 0 ) {
980 char video_path[ PATH_MAX + 1 ];
981
982 /* Try also with adding the path, just in case. */
983 if ( dvd->dvd_type == DVD_VR ) {
984 sprintf( video_path, "%s/DVD_RTAV/", dvd->rd->path_root );
985 ret = findDirFile( dvd, video_path, nodirfile, filename );
986 if( ret < 0 ) {
987 /* Try with the path, but in lower case. */
988 sprintf( video_path, "%s/dvd_rtav/", dvd->rd->path_root );
989 ret = findDirFile( dvd, video_path, nodirfile, filename );
990 if( ret < 0 ) {
991 return 0;
992 }
993 }
994 } else {
995 sprintf( video_path, "%s/%s_TS/", dvd->rd->path_root, DVD_TYPE_STRING( dvd->dvd_type ) );
996 ret = findDirFile( dvd, video_path, nodirfile, filename );
997 if( ret < 0 ) {
998 /* Try with the path, but in lower case. */
999 sprintf( video_path, "%s/%s_ts/", dvd->rd->path_root, (dvd->dvd_type == DVD_V) ? "video" : "audio" );
1000 ret = findDirFile( dvd, video_path, nodirfile, filename );
1001 if( ret < 0 ) {
1002 return 0;
1003 }
1004 }
1005 }
1006 }
1007
1008 return 1;
1009 }
1010
1011 /**
1012 * Open an unencrypted file from a DVD directory tree.
1013 */
1014 static dvd_file_t *DVDOpenFilePath( dvd_reader_t *ctx, const char *filename )
1015 {
1016 char full_path[ PATH_MAX + 1 ];
1017 dvd_file_t *dvd_file;
1018 dvdstat_t fileinfo;
1019 dvd_input_t dev;
1020
1021 /* Get the full path of the file. */
1022 if( !findDVDFile( ctx, filename, full_path ) ) {
1023 Log0(ctx, "DVDOpenFilePath:findDVDFile %s failed", filename );
1024 return NULL;
1025 }
1026
1027 dev = dvdinput_open( ctx->priv, &ctx->logcb, full_path, NULL, ctx->fs );
1028 if( !dev ) {
1029 Log0(ctx, "DVDOpenFilePath:dvdinput_open %s failed", full_path );
1030 return NULL;
1031 }
1032
1033 dvd_file = calloc( 1, sizeof( dvd_file_t ) );
1034 if( !dvd_file ) {
1035 Log0(ctx, "DVDOpenFilePath:dvd_file malloc failed" );
1036 dvdinput_close(dev);
1037 return NULL;
1038 }
1039 dvd_file->ctx = ctx;
1040
1041 if (ctx->fs->stat(ctx->fs, full_path, &fileinfo) < 0) {
1042 Log0(ctx, "Can't stat() %s.", filename );
1043 free( dvd_file );
1044 dvdinput_close( dev );
1045 return NULL;
1046 }
1047 dvd_file->title_sizes[ 0 ] = BYTES_TO_DVD_BLOCKS_CEIL(fileinfo.size);
1048 dvd_file->title_devs[ 0 ] = dev;
1049 dvd_file->filesize = dvd_file->title_sizes[ 0 ];
1050
1051 return dvd_file;
1052 }
1053
1054 static uint8_t *cppm_get_mkb_or_backup( dvd_reader_t *ctx, int backup )
1055 {
1056 uint8_t* p_mkb;
1057 dvd_file_t* mkb_file;
1058 char filename[ MAX_UDF_FILE_NAME_LEN ];
1059
1060 dvd_reader_device_t *dvd = ctx->rd;
1061 switch( backup )
1062 {
1063 case 0:
1064 strcpy( filename, "/AUDIO_TS/DVDAUDIO.MKB" );
1065 break;
1066 case 1:
1067 strcpy( filename, "/AUDIO_TS/DVDAUDIO.BUP" );
1068 break;
1069 }
1070
1071 uint32_t len;
1072
1073 /* exits early if there is no MKB file */
1074 if( dvd->isImageFile )
1075 {
1076 if( !UDFFindFile( ctx, filename, &len ) ) return NULL;
1077 mkb_file= DVDOpenFileUDF( ctx, filename, 0 );
1078 } else
1079 {
1080 if( !findDVDFile( ctx, filename, filename ) ) return NULL;
1081 mkb_file= DVDOpenFilePath( ctx, filename );
1082 }
1083
1084 if ( !mkb_file )
1085 return NULL;
1086
1087 p_mkb = malloc( mkb_file->filesize * DVD_VIDEO_LB_LEN );
1088 if ( !p_mkb ) {
1089 DVDCloseFile( mkb_file );
1090 return NULL;
1091 }
1092
1093 if ( !DVDReadBytes( mkb_file, p_mkb, mkb_file->filesize * DVD_VIDEO_LB_LEN ) )
1094 {
1095 free( p_mkb );
1096 DVDCloseFile( mkb_file );
1097 return NULL;
1098 }
1099
1100 /* checking header */
1101 if( ( !memcmp( p_mkb, "DVDAUDIO.MKB", 12 ) && !backup )
1102 || ( !memcmp( p_mkb, "DVDAUDIO.BUP", 12 ) && backup ) ) {
1103 free( p_mkb );
1104 DVDCloseFile( mkb_file );
1105 return NULL;
1106 }
1107
1108 DVDCloseFile( mkb_file );
1109 return p_mkb;
1110 }
1111
1112 static dvd_file_t *DVDOpenVOBUDF( dvd_reader_t *ctx, int title, int menu )
1113 {
1114 char filename[ MAX_UDF_FILE_NAME_LEN ];
1115 uint32_t start, len;
1116 dvd_file_t *dvd_file;
1117 /* stream type must be set to determine decryption method*/
1118 /* DVD-Audio discs contain both AOBs and VOBs */
1119 /* DVD_V = VOB with css, DVD_A = AOB with CPPM, DVD_VR = VRO with CPRM */
1120 dvd_type_t stream_type;
1121
1122 if ( ctx->dvd_type == DVD_VR && menu )
1123 return NULL;
1124
1125 if ( ctx->dvd_type == DVD_VR ) {
1126 sprintf( filename, "/DVD_RTAV/VR_MOVIE.VRO" );
1127 } else if( title == 0 ) {
1128 sprintf( filename, "/%s_TS/%s_TS.VOB", DVD_TYPE_STRING( ctx->dvd_type ), DVD_TYPE_STRING( ctx->dvd_type ) );
1129 } else if(!menu) {
1130 /* DVD Content - Tracks/Chapters */
1131 sprintf( filename, "/%s_TS/%cTS_%02d_1.%cOB", DVD_TYPE_STRING( ctx->dvd_type ),
1132 STREAM_TYPE_STRING( ctx->dvd_type ), title, STREAM_TYPE_STRING( ctx->dvd_type ) );
1133 } else {
1134 if ( ctx->dvd_type == DVD_V )
1135 /* DVD_Video title menu */
1136 sprintf( filename, "/VIDEO_TS/VTS_%02d_0.VOB", title );
1137 else if ( ctx->dvd_type == DVD_A )
1138 /* DVD_Audio title menu */
1139 sprintf( filename, "/AUDIO_TS/AUDIO_SV.VOB" );
1140 }
1141
1142 if ( ctx->dvd_type == DVD_VR )
1143 stream_type = DVD_VR;
1144 else if ( !strncmp( filename, "/VIDEO_TS/", 10 ) )
1145 stream_type = DVD_V;
1146 else
1147 stream_type = DVD_A;
1148
1149 start = UDFFindFile( ctx, filename, &len );
1150 if( start == 0 ) return NULL;
1151
1152 dvd_file = calloc( 1, sizeof( dvd_file_t ) );
1153 if( !dvd_file ) return NULL;
1154 dvd_file->ctx = ctx;
1155
1156 /* css vars not used in CPXM */
1157 if( stream_type == DVD_V )
1158 /*Hack*/ dvd_file->css_title = title << 1 | menu;
1159
1160 dvd_file->lb_start = start;
1161 dvd_file->filesize = BYTES_TO_DVD_BLOCKS_CEIL(len);
1162
1163 /* Calculate the complete file size for every file in the VOBS, AOBS */
1164 /* DVD-VR uses UDF 2.0 which allows for larger file sizes, 1GB limit does not exist */
1165 if( !menu && ctx->dvd_type != DVD_VR ) {
1166 int cur;
1167
1168 for( cur = 2; cur < 10; cur++ ) {
1169 sprintf( filename, "/%s_TS/%cTS_%02d_%d.%cOB", DVD_TYPE_STRING( ctx->dvd_type ),
1170 STREAM_TYPE_STRING( ctx->dvd_type ), title, cur, STREAM_TYPE_STRING( ctx->dvd_type ) );
1171 if( !UDFFindFile( ctx, filename, &len ) ) break;
1172 dvd_file->filesize += BYTES_TO_DVD_BLOCKS_CEIL(len);
1173 }
1174 }
1175
1176 if( stream_type == DVD_V && ctx->rd->css_state == 1 /* Need key init */ ) {
1177 initAllCSSKeys( ctx );
1178 ctx->rd->css_state = 2;
1179 }
1180
1181 dvdinput_set_stream( ctx->rd->dev, stream_type );
1182 return dvd_file;
1183 }
1184
1185 static dvd_file_t *DVDOpenVOBPath( dvd_reader_t *ctx, int title, int menu )
1186 {
1187 char filename[ MAX_UDF_FILE_NAME_LEN ];
1188 char full_path[ PATH_MAX + 1 ];
1189 dvdstat_t fileinfo;
1190 dvd_file_t *dvd_file;
1191 /* menus are always VOB streams, title type depends on disc disc */
1192 /* for now will set as the dvd_type,
1193 * when hybrid discs are implemented set as title type */
1194 /* DVD_V = VOB with css, DVD_A = AOB with CPPM, DVD_VR = VRO with CPRM */
1195 dvd_type_t stream_type = ctx->dvd_type;
1196
1197 dvd_file = calloc( 1, sizeof( dvd_file_t ) );
1198 if( !dvd_file ) return NULL;
1199 dvd_file->ctx = ctx;
1200
1201 /* DVD-VR has no menu vobs */
1202 if ( ctx->dvd_type == DVD_VR && menu ) {
1203 free( dvd_file );
1204 return NULL;
1205 }
1206
1207
1208 /* css vars aren't used in CPXM */
1209 if ( stream_type == DVD_V )
1210 /*Hack*/ dvd_file->css_title = title << 1 | menu;
1211
1212 if( menu ) {
1213 dvd_input_t dev;
1214
1215 if( title == 0 ) {
1216 /* the root menu is AUDIO_TS.VOB or VIDEO_TS.VOB */
1217 sprintf(filename, "%s_TS.VOB", DVD_TYPE_STRING( ctx->dvd_type ) );
1218 } else {
1219 /* there are no ATS_%02i_0.AOB's */
1220 if ( ctx->dvd_type == DVD_V )
1221 sprintf( filename, "VTS_%02i_0.VOB", title );
1222 else
1223 /* Remaining title menus would be in the still videos VOB */
1224 sprintf( filename, "AUDIO_SV.VOB" );
1225 }
1226 if( !findDVDFile( ctx, filename, full_path ) ) {
1227 free( dvd_file );
1228 return NULL;
1229 }
1230
1231 dev = dvdinput_open( ctx->priv, &ctx->logcb, full_path, NULL, ctx->fs );
1232 if( dev == NULL ) {
1233 free( dvd_file );
1234 return NULL;
1235 }
1236
1237 if (ctx->fs->stat(ctx->fs, full_path, &fileinfo) < 0) {
1238 Log0(ctx, "Can't stat() %s.", filename );
1239 dvdinput_close(dev);
1240 free( dvd_file );
1241 return NULL;
1242 }
1243 dvd_file->title_sizes[ 0 ] = BYTES_TO_DVD_BLOCKS_CEIL(fileinfo.size);
1244 dvd_file->title_devs[ 0 ] = dev;
1245 dvdinput_title( dvd_file->title_devs[0], 0);
1246 dvd_file->filesize = dvd_file->title_sizes[ 0 ];
1247 dvdinput_set_stream( dev, stream_type );
1248
1249 } else {
1250
1251 /* DVD-VR uses UDF 2.0 which allows for larger file sizes, 1GB limit does not exist */
1252 /* will only return one file, treating it as a single fragment with one title dev */
1253 if ( ctx->dvd_type == DVD_VR ) {
1254 sprintf( filename, "VR_MOVIE.VRO");
1255
1256 if( !findDVDFile( ctx, filename, full_path ) ) {
1257 free( dvd_file );
1258 return NULL;
1259 }
1260 if (ctx->fs->stat(ctx->fs, full_path, &fileinfo) < 0) {
1261 Log0(ctx, "Can't stat() %s.", filename );
1262 free( dvd_file );
1263 return NULL;
1264 }
1265
1266 dvd_file->title_sizes[ 0 ] = BYTES_TO_DVD_BLOCKS_CEIL(fileinfo.size);
1267 dvd_file->title_devs[ 0 ] = dvdinput_open( ctx->priv, &ctx->logcb, full_path, NULL, ctx->fs );
1268
1269 if( !dvd_file->title_devs[ 0 ] ) {
1270 free( dvd_file );
1271 return NULL;
1272 }
1273
1274 dvdinput_set_stream( dvd_file->title_devs[ 0 ], stream_type );
1275
1276 /* Initialize CPRM */
1277 if( dvdinput_init )
1278 dvdinput_init( dvd_file->title_devs[ 0 ], NULL );
1279
1280 dvdinput_title( dvd_file->title_devs[ 0 ], 0 );
1281 dvd_file->filesize = dvd_file->title_sizes[ 0 ];
1282
1283 } else {
1284
1285 int i;
1286
1287 for( i = 0; i < TITLES_MAX; ++i ) {
1288
1289 sprintf( filename, "%cTS_%02i_%i.%cOB", STREAM_TYPE_STRING( ctx->dvd_type ), title, i + 1 , STREAM_TYPE_STRING( ctx->dvd_type ));
1290 if( !findDVDFile( ctx, filename, full_path ) ) {
1291 break;
1292 }
1293
1294 if( ctx->fs->stat(ctx->fs, full_path, &fileinfo) < 0 ) {
1295 Log0(ctx, "Can't stat() %s.", filename );
1296 break;
1297 }
1298
1299 dvd_file->title_devs[ i ] = dvdinput_open( ctx->priv, &ctx->logcb, full_path, NULL, ctx->fs );
1300 if( !dvd_file->title_devs[ i ] )
1301 break;
1302 dvd_file->title_sizes[ i ] = BYTES_TO_DVD_BLOCKS_CEIL(fileinfo.size);
1303 /* setting type of stream will determine what decryption to use */
1304 dvdinput_set_stream( dvd_file->title_devs[ i ], stream_type );
1305
1306 /* if function is defined, cpxm was imported so call init */
1307 /* should have already been initialized in dvdinput_setup, will copy over
1308 * decryption context to each dev dvdcss instance */
1309 if( dvdinput_init && stream_type == DVD_A )
1310 dvdinput_init( dvd_file->title_devs[ i ], NULL );
1311
1312 dvdinput_title( dvd_file->title_devs[ i ], 0 );
1313 dvd_file->filesize += dvd_file->title_sizes[ i ];
1314 }
1315 if( !dvd_file->title_devs[ 0 ] ) {
1316 free( dvd_file );
1317 return NULL;
1318 }
1319 }
1320 }
1321
1322 return dvd_file;
1323 }
1324
1325 dvd_file_t *DVDOpenFile( dvd_reader_t *ctx, int titlenum,
1326 dvd_read_domain_t domain )
1327 {
1328 dvd_reader_device_t *dvd = ctx->rd;
1329 char filename[ MAX_UDF_FILE_NAME_LEN ];
1330 int do_cache = 0;
1331
1332 /* Check arguments. */
1333 if( dvd == NULL || titlenum < 0 )
1334 return NULL;
1335
1336 switch( domain ) {
1337 case DVD_READ_INFO_FILE:
1338 if ( ctx->dvd_type == DVD_VR )
1339 sprintf( filename, "/DVD_RTAV/VR_MANGR.IFO" );
1340 else if( titlenum == 0 ) {
1341 sprintf( filename, "/%s_TS/%s_TS.IFO", DVD_TYPE_STRING( ctx->dvd_type ),
1342 DVD_TYPE_STRING( ctx->dvd_type ) );
1343 } else {
1344 sprintf( filename, "/%s_TS/%cTS_%02i_0.IFO", DVD_TYPE_STRING( ctx->dvd_type ),
1345 STREAM_TYPE_STRING( ctx->dvd_type ), titlenum );
1346 }
1347 do_cache = 1;
1348 break;
1349 case DVD_READ_INFO_BACKUP_FILE:
1350 if ( ctx->dvd_type == DVD_VR )
1351 sprintf( filename, "/DVD_RTAV/VR_MANGR.BUP" );
1352 else if( titlenum == 0 ) {
1353 sprintf( filename, "/%s_TS/%s_TS.BUP", DVD_TYPE_STRING( ctx->dvd_type ),
1354 DVD_TYPE_STRING( ctx->dvd_type ) );
1355 } else {
1356 sprintf( filename, "/%s_TS/%cTS_%02i_0.BUP", DVD_TYPE_STRING( ctx->dvd_type ),
1357 STREAM_TYPE_STRING( ctx->dvd_type ), titlenum );
1358 }
1359 do_cache = 1;
1360 break;
1361 case DVD_READ_MENU_VOBS:
1362 if( dvd->isImageFile ) {
1363 /* there is only two DVD-Audio menu vobs, and the second is optional
1364 * in the case of DVD-Audio, this should return 0 for AUDIO_TS.VOB, which is the main menu
1365 * AUDIO_SV.VOB is the Audio Still Video Set (ASVS), and contains the title menus */
1366 if ( ctx->dvd_type == DVD_VR ) {
1367 Log1( ctx, "There is no DVD-VR menu VOB" );
1368 return NULL;
1369 } else if ( ctx->dvd_type == DVD_A && titlenum > 1 )
1370 Log2( ctx, "Defaulting to the only menu on DVD-Audio discs" );
1371 return DVDOpenVOBUDF( ctx, ( ctx->dvd_type == DVD_V ? titlenum : titlenum > 0 ), 1 );
1372 } else {
1373 return DVDOpenVOBPath( ctx, ( ctx->dvd_type == DVD_V ? titlenum : titlenum > 0 ), 1 );
1374 }
1375 break;
1376 case DVD_READ_TITLE_VOBS:
1377 if( titlenum == 0 && ctx->dvd_type != DVD_VR ) return NULL;
1378 if( dvd->isImageFile ) {
1379 return DVDOpenVOBUDF( ctx, titlenum, 0 );
1380 } else {
1381 return DVDOpenVOBPath( ctx, titlenum, 0 );
1382 }
1383 break;
1384 case DVD_READ_SAMG_INFO:
1385 /* no other way to reach SAMG menu*/
1386 if( ctx->dvd_type == DVD_V ) {
1387 Log1( ctx, "SAMG IFO is exclusive to DVD-Audio" );
1388 return NULL;
1389 }
1390 strcpy( filename, "/AUDIO_TS/AUDIO_PP.IFO" );
1391 break;
1392 case DVD_READ_ASVS_INFO:
1393 /* no other way to reach ASVS menu*/
1394 if( ctx->dvd_type == DVD_V ) {
1395 Log1( ctx, "ASVS IFO is exclusive to DVD-Audio" );
1396 return NULL;
1397 }
1398 strcpy( filename, "/AUDIO_TS/AUDIO_SV.IFO" );
1399
1400 break;
1401 case DVD_READ_ASVS_INFO_BACKUP:
1402 /* no other way to reach ASVS menu*/
1403 if( ctx->dvd_type == DVD_V ) {
1404 Log1( ctx, "ASVS IFO Backup is exclusive to DVD-Audio" );
1405 return NULL;
1406 }
1407 strcpy( filename, "/AUDIO_TS/AUDIO_SV.BUP" );
1408
1409 break;
1410 default:
1411 Log1( ctx, "Invalid domain for file open." );
1412 return NULL;
1413 }
1414
1415 if( dvd->isImageFile ) {
1416 return DVDOpenFileUDF( ctx, filename, do_cache );
1417 } else {
1418 return DVDOpenFilePath( ctx, filename );
1419 }
1420 }
1421
1422 void DVDCloseFile( dvd_file_t *dvd_file )
1423 {
1424 dvd_reader_device_t *dvd;
1425 if( !dvd_file )
1426 return;
1427 dvd = dvd_file->ctx->rd;
1428 if( dvd ) {
1429 if( !dvd->isImageFile ) {
1430 int i;
1431
1432 for( i = 0; i < TITLES_MAX; ++i ) {
1433 if( dvd_file->title_devs[ i ] ) {
1434 dvdinput_close( dvd_file->title_devs[i] );
1435 }
1436 }
1437 }
1438
1439 free( dvd_file->cache );
1440 free( dvd_file );
1441 dvd_file = NULL;
1442 }
1443 }
1444
1445 static int DVDFileStatVOBUDF( dvd_reader_t *dvd, int title,
1446 int menu, dvd_stat_t *statbuf )
1447 {
1448 char filename[ MAX_UDF_FILE_NAME_LEN ];
1449 uint32_t size;
1450 int64_t tot_size;
1451 int64_t parts_size[ 9 ];
1452 int nr_parts = 0;
1453 int n;
1454
1455 if( title == 0 )
1456 sprintf( filename, "/%s_TS/%s_TS.VOB", DVD_TYPE_STRING( dvd->dvd_type ),
1457 DVD_TYPE_STRING( dvd->dvd_type ) );
1458 else
1459 sprintf( filename, "/%s_TS/%cTS_%02d_%d.%cOB", DVD_TYPE_STRING( dvd->dvd_type ),
1460 STREAM_TYPE_STRING( dvd->dvd_type ), title, menu ? 0 : 1, STREAM_TYPE_STRING( dvd->dvd_type ) );
1461
1462 if( !UDFFindFile( dvd, filename, &size ) )
1463 return -1;
1464
1465 tot_size = size;
1466 nr_parts = 1;
1467 parts_size[ 0 ] = size;
1468
1469 if( !menu ) {
1470 int cur;
1471
1472 for( cur = 2; cur < 10; cur++ ) {
1473 sprintf( filename, "/%s_TS/%cTS_%02d_%d.%cOB", DVD_TYPE_STRING( dvd->dvd_type ),
1474 STREAM_TYPE_STRING( dvd->dvd_type ), title, cur , STREAM_TYPE_STRING( dvd->dvd_type ) );
1475 if( !UDFFindFile( dvd, filename, &size ) )
1476 break;
1477
1478 parts_size[ nr_parts ] = size;
1479 tot_size += size;
1480 nr_parts++;
1481 }
1482 }
1483
1484 statbuf->size = tot_size;
1485 statbuf->nr_parts = nr_parts;
1486 for( n = 0; n < nr_parts; n++ )
1487 statbuf->parts_size[ n ] = parts_size[ n ];
1488
1489 return 0;
1490 }
1491
1492
1493 static int DVDFileStatVOBPath( dvd_reader_t *dvd, int title,
1494 int menu, dvd_stat_t *statbuf )
1495 {
1496 char filename[ MAX_UDF_FILE_NAME_LEN ];
1497 char full_path[ PATH_MAX + 1 ];
1498 dvdstat_t fileinfo;
1499 int64_t tot_size;
1500 int64_t parts_size[ 9 ];
1501 int nr_parts = 0;
1502 int n;
1503
1504 if( title == 0 )
1505 sprintf( filename, "%s_TS.VOB", DVD_TYPE_STRING( dvd->dvd_type ) );
1506 else
1507 sprintf( filename, "%cTS_%02d_%d.%cOB", STREAM_TYPE_STRING( dvd->dvd_type ), title, menu ? 0 : 1,
1508 STREAM_TYPE_STRING( dvd->dvd_type ) );
1509
1510 if( !findDVDFile( dvd, filename, full_path ) )
1511 return -1;
1512
1513 if (dvd->fs->stat(dvd->fs, full_path, &fileinfo) < 0) {
1514 Log1(dvd, "Can't stat() %s.", filename );
1515 return -1;
1516 }
1517
1518 tot_size = fileinfo.size;
1519 nr_parts = 1;
1520 parts_size[ 0 ] = fileinfo.size;
1521
1522 if( !menu ) {
1523 int cur;
1524 for( cur = 2; cur < 10; cur++ ) {
1525 sprintf( filename, "%cTS_%02d_%d.%cOB", STREAM_TYPE_STRING( dvd->dvd_type ), title, cur,
1526 STREAM_TYPE_STRING( dvd->dvd_type ) );
1527 if( !findDVDFile( dvd, filename, full_path ) )
1528 break;
1529
1530 if (dvd->fs->stat(dvd->fs, full_path, &fileinfo) < 0) {
1531 Log1(dvd, "Can't stat() %s.", filename );
1532 break;
1533 }
1534
1535 parts_size[ nr_parts ] = fileinfo.size;
1536 tot_size += parts_size[ nr_parts ];
1537 nr_parts++;
1538 }
1539 }
1540
1541 statbuf->size = tot_size;
1542 statbuf->nr_parts = nr_parts;
1543 for( n = 0; n < nr_parts; n++ )
1544 statbuf->parts_size[ n ] = parts_size[ n ];
1545
1546 return 0;
1547 }
1548
1549
1550 int DVDFileStat( dvd_reader_t *reader, int titlenum,
1551 dvd_read_domain_t domain, dvd_stat_t *statbuf )
1552 {
1553 dvd_reader_device_t *dvd = reader->rd;
1554 char filename[ MAX_UDF_FILE_NAME_LEN ];
1555 dvdstat_t fileinfo;
1556 uint32_t size;
1557
1558 /* Check arguments. */
1559 if( dvd == NULL || titlenum < 0 ) {
1560 errno = EINVAL;
1561 return -1;
1562 }
1563
1564 switch( domain ) {
1565
1566 case DVD_READ_INFO_FILE:
1567 if( titlenum == 0 )
1568 sprintf( filename, "/%s_TS/%s_TS.IFO", DVD_TYPE_STRING( reader->dvd_type ), DVD_TYPE_STRING( reader->dvd_type ) );
1569 else
1570 sprintf( filename, "/%s_TS/%cTS_%02i_0.IFO", DVD_TYPE_STRING( reader->dvd_type ), STREAM_TYPE_STRING( reader->dvd_type ) ,titlenum );
1571
1572 break;
1573 case DVD_READ_INFO_BACKUP_FILE:
1574 if( titlenum == 0 )
1575 sprintf( filename, "/%s_TS/%s_TS.BUP", DVD_TYPE_STRING( reader->dvd_type ), DVD_TYPE_STRING( reader->dvd_type ) );
1576 else
1577 sprintf( filename, "/%s_TS/%cTS_%02i_0.BUP", DVD_TYPE_STRING( reader->dvd_type ), STREAM_TYPE_STRING( reader->dvd_type ), titlenum );
1578
1579 break;
1580 case DVD_READ_MENU_VOBS:
1581 if( dvd->isImageFile )
1582 return DVDFileStatVOBUDF( reader, titlenum, 1, statbuf );
1583 else
1584 return DVDFileStatVOBPath( reader, titlenum, 1, statbuf );
1585
1586 break;
1587 case DVD_READ_TITLE_VOBS:
1588 if( titlenum == 0 )
1589 return -1;
1590
1591 if( dvd->isImageFile )
1592 return DVDFileStatVOBUDF( reader, titlenum, 0, statbuf );
1593 else
1594 return DVDFileStatVOBPath( reader, titlenum, 0, statbuf );
1595
1596 break;
1597 case DVD_READ_SAMG_INFO:
1598 /* no other way to reach SAMG menu*/
1599 if( reader->dvd_type == DVD_V ) {
1600 Log1( reader, "SAMG IFO is exclusive to DVD-Audio" );
1601 return -1;
1602 }
1603 strcpy( filename, "/AUDIO_TS/AUDIO_PP.IFO" );
1604
1605 break;
1606 case DVD_READ_ASVS_INFO:
1607 /* no other way to reach ASVS menu*/
1608 if( reader->dvd_type == DVD_V ) {
1609 Log1( reader, "ASVS IFO is exclusive to DVD-Audio" );
1610 return -1;
1611 }
1612 strcpy( filename, "/AUDIO_TS/AUDIO_SV.IFO" );
1613
1614 break;
1615 case DVD_READ_ASVS_INFO_BACKUP:
1616 /* no other way to reach ASVS menu*/
1617 if( reader->dvd_type == DVD_V ) {
1618 Log1( reader, "ASVS IFO Backup is exclusive to DVD-Audio" );
1619 return -1;
1620 }
1621 strcpy( filename, "/AUDIO_TS/AUDIO_SV.BUP" );
1622
1623 break;
1624 default:
1625 Log1(reader, "Invalid domain for file stat." );
1626 errno = EINVAL;
1627 return -1;
1628 }
1629
1630 if( dvd->isImageFile ) {
1631 if( UDFFindFile( reader, filename, &size ) ) {
1632 statbuf->size = size;
1633 statbuf->nr_parts = 1;
1634 statbuf->parts_size[ 0 ] = size;
1635 return 0;
1636 }
1637 } else {
1638 char full_path[ PATH_MAX + 1 ];
1639
1640 if( findDVDFile( reader, filename, full_path ) ) {
1641 if (reader->fs->stat(reader->fs, full_path, &fileinfo) < 0)
1642 Log1(reader, "Can't stat() %s.", filename );
1643 else {
1644 statbuf->size = fileinfo.size;
1645 statbuf->nr_parts = 1;
1646 statbuf->parts_size[ 0 ] = statbuf->size;
1647 return 0;
1648 }
1649 }
1650 }
1651 return -1;
1652 }
1653
1654 /* Internal, but used from dvd_udf.c */
1655 int InternalUDFReadBlocksRaw( const dvd_reader_t *ctx, uint32_t lb_number,
1656 size_t block_count, unsigned char *data,
1657 int encrypted )
1658 {
1659 int ret;
1660
1661 if( !ctx->rd->dev ) {
1662 Log0( ctx, "Fatal error in block read." );
1663 return -1;
1664 }
1665
1666 ret = dvdinput_seek( ctx->rd->dev, (int) lb_number );
1667 if( ret != (int) lb_number ) {
1668 Log1( ctx, "Can't seek to block %u", lb_number );
1669 return ret;
1670 }
1671
1672 ret = dvdinput_read( ctx->rd->dev, (char *) data,
1673 (int) block_count, encrypted );
1674 return ret;
1675 }
1676
1677 /* This is using a single input and starting from 'dvd_file->lb_start' offset.
1678 *
1679 * Reads 'block_count' blocks from 'dvd_file' at block offset 'offset'
1680 * into the buffer located at 'data' and if 'encrypted' is set
1681 * descramble the data if it's encrypted. Returning either an
1682 * negative error or the number of blocks read. */
1683 static int DVDReadBlocksUDF( const dvd_file_t *dvd_file, uint32_t offset,
1684 size_t block_count, unsigned char *data,
1685 int encrypted )
1686 {
1687 /* If the cache is present and we don't need to decrypt, use the cache to
1688 * feed the data */
1689 if( dvd_file->cache && (encrypted & DVDINPUT_READ_DECRYPT) == 0 ) {
1690 /* Check if we don't exceed the cache (or file) size */
1691 if( block_count + offset > (size_t) dvd_file->filesize )
1692 return 0;
1693
1694 /* Copy the cache at a specified offset into data. offset and block_count
1695 * must be converted into bytes */
1696 memcpy( data, dvd_file->cache + (int64_t)offset * (int64_t)DVD_VIDEO_LB_LEN,
1697 (int64_t)block_count * (int64_t)DVD_VIDEO_LB_LEN );
1698
1699 /* return the amount of blocks copied */
1700 return block_count;
1701 } else {
1702 /* use dvdinput access */
1703 return InternalUDFReadBlocksRaw( dvd_file->ctx, dvd_file->lb_start + offset,
1704 block_count, data, encrypted );
1705 }
1706 }
1707
1708 /* This is using possibly several inputs and starting from an offset of '0'.
1709 *
1710 * Reads 'block_count' blocks from 'dvd_file' at block offset 'offset'
1711 * into the buffer located at 'data' and if 'encrypted' is set
1712 * descramble the data if it's encrypted. Returning either an
1713 * negative error or the number of blocks read. */
1714 static int DVDReadBlocksPath( const dvd_file_t *dvd_file, unsigned int offset,
1715 size_t block_count, unsigned char *data,
1716 int encrypted )
1717 {
1718 const dvd_reader_t *ctx = dvd_file->ctx;
1719 int i;
1720 int ret, ret2, off;
1721
1722 ret = 0;
1723 ret2 = 0;
1724 for( i = 0; i < TITLES_MAX; ++i ) {
1725 if( !dvd_file->title_sizes[ i ] ) return 0; /* Past end of file */
1726
1727 if( offset < dvd_file->title_sizes[ i ] ) {
1728 if( ( offset + block_count ) <= dvd_file->title_sizes[ i ] ) {
1729 off = dvdinput_seek( dvd_file->title_devs[ i ], (int)offset );
1730 if( off < 0 || off != (int)offset ) {
1731 Log1( ctx, "Can't seek to block %u", offset );
1732 return off < 0 ? off : 0;
1733 }
1734 ret = dvdinput_read( dvd_file->title_devs[ i ], data,
1735 (int)block_count, encrypted );
1736 break;
1737 } else {
1738 size_t part1_size = dvd_file->title_sizes[ i ] - offset;
1739 /* FIXME: Really needs to be a while loop.
1740 * (This is only true if you try and read >1GB at a time) */
1741
1742 /* Read part 1 */
1743 off = dvdinput_seek( dvd_file->title_devs[ i ], (int)offset );
1744 if( off < 0 || off != (int)offset ) {
1745 Log1( ctx, "Can't seek to block %u", offset );
1746 return off < 0 ? off : 0;
1747 }
1748 ret = dvdinput_read( dvd_file->title_devs[ i ], data,
1749 (int)part1_size, encrypted );
1750 if( ret < 0 ) return ret;
1751 /* FIXME: This is wrong if i is the last file in the set.
1752 * also error from this read will not show in ret. */
1753
1754 /* Does the next part exist? If not then return now. */
1755 if( i + 1 >= TITLES_MAX || !dvd_file->title_devs[ i + 1 ] )
1756 return ret;
1757
1758 /* Read part 2 */
1759 off = dvdinput_seek( dvd_file->title_devs[ i + 1 ], 0 );
1760 if( off < 0 || off != 0 ) {
1761 Log1( ctx, "Can't seek to block %d", 0 );
1762 return off < 0 ? off : 0;
1763 }
1764 ret2 = dvdinput_read( dvd_file->title_devs[ i + 1 ],
1765 data + ( part1_size
1766 * (int64_t)DVD_VIDEO_LB_LEN ),
1767 (int)(block_count - part1_size),
1768 encrypted );
1769 if( ret2 < 0 ) return ret2;
1770 break;
1771 }
1772 } else {
1773 offset -= dvd_file->title_sizes[ i ];
1774 }
1775 }
1776
1777 return ret + ret2;
1778 }
1779
1780 /* This is broken reading more than 2Gb at a time is ssize_t is 32-bit. */
1781 ssize_t DVDReadBlocks( dvd_file_t *dvd_file, int offset,
1782 size_t block_count, unsigned char *data )
1783 {
1784 dvd_reader_t *ctx = dvd_file->ctx;
1785 dvd_reader_device_t *dvd = ctx->rd;
1786 int ret;
1787
1788 /* Check arguments. */
1789 if( dvd_file == NULL || offset < 0 || data == NULL )
1790 return -1;
1791
1792 /* Hack, and it will still fail for multiple opens in a threaded app ! */
1793 if( dvd->css_title != dvd_file->css_title ) {
1794 dvd->css_title = dvd_file->css_title;
1795 if( dvd->isImageFile ) {
1796 dvdinput_title( dvd->dev, (int)dvd_file->lb_start );
1797 }
1798 /* Here each vobu has it's own dvdcss handle, so no need to update
1799 else {
1800 dvdinput_title( dvd_file->title_devs[ 0 ], (int)dvd_file->lb_start );
1801 }*/
1802 }
1803
1804 if( dvd->isImageFile ) {
1805 ret = DVDReadBlocksUDF( dvd_file, (uint32_t)offset,
1806 block_count, data, DVDINPUT_READ_DECRYPT );
1807 } else {
1808 ret = DVDReadBlocksPath( dvd_file, (unsigned int)offset,
1809 block_count, data, DVDINPUT_READ_DECRYPT );
1810 }
1811
1812 return (ssize_t)ret;
1813 }
1814
1815 int32_t DVDFileSeek( dvd_file_t *dvd_file, int32_t offset )
1816 {
1817 /* Check arguments. */
1818 if( dvd_file == NULL || offset < 0 )
1819 return -1;
1820
1821 if( offset > dvd_file->filesize * DVD_VIDEO_LB_LEN ) {
1822 return -1;
1823 }
1824 dvd_file->seek_pos = (uint32_t) offset;
1825 return offset;
1826 }
1827
1828 int DVDFileSeekForce(dvd_file_t *dvd_file, int offset, int force_size)
1829 {
1830 dvd_reader_t *ctx = dvd_file->ctx;
1831 dvd_reader_device_t *dvd = ctx->rd;
1832 /* Check arguments. */
1833 if( dvd_file == NULL || offset <= 0 )
1834 return -1;
1835
1836 if( dvd->isImageFile ) {
1837 if( force_size < 0 )
1838 force_size = (offset - 1) / DVD_VIDEO_LB_LEN + 1;
1839 if( dvd_file->filesize < force_size ) {
1840 dvd_file->filesize = force_size;
1841 free(dvd_file->cache);
1842 dvd_file->cache = NULL;
1843 Log2(ctx, "Ignored size of file indicated in UDF.");
1844 }
1845 }
1846
1847 if( offset > dvd_file->filesize * DVD_VIDEO_LB_LEN )
1848 return -1;
1849
1850 dvd_file->seek_pos = (uint32_t) offset;
1851 return offset;
1852 }
1853
1854 ssize_t DVDReadBytes( dvd_file_t *dvd_file, void *data, size_t byte_size )
1855 {
1856 dvd_reader_t *ctx = dvd_file->ctx;
1857 dvd_reader_device_t *dvd = ctx->rd;
1858 unsigned char *secbuf_base, *secbuf;
1859 unsigned int numsec, seek_sector, seek_byte;
1860 int ret;
1861
1862 /* Check arguments. */
1863 if( dvd_file == NULL || data == NULL || (ssize_t)byte_size < 0 )
1864 return -1;
1865
1866 seek_sector = dvd_file->seek_pos / DVD_VIDEO_LB_LEN;
1867 seek_byte = dvd_file->seek_pos % DVD_VIDEO_LB_LEN;
1868
1869 numsec = ( ( seek_byte + byte_size ) / DVD_VIDEO_LB_LEN ) +
1870 ( ( ( seek_byte + byte_size ) % DVD_VIDEO_LB_LEN ) ? 1 : 0 );
1871
1872 secbuf_base = malloc( numsec * DVD_VIDEO_LB_LEN + 2048 );
1873 if( !secbuf_base ) {
1874 Log0( ctx, "Can't allocate memory for file read" );
1875 return 0;
1876 }
1877 secbuf = (unsigned char *)(((uintptr_t)secbuf_base & ~((uintptr_t)2047)) + 2048);
1878
1879 if( dvd->isImageFile ) {
1880 ret = DVDReadBlocksUDF( dvd_file, (uint32_t) seek_sector,
1881 (size_t) numsec, secbuf, DVDINPUT_NOFLAGS );
1882 } else {
1883 ret = DVDReadBlocksPath( dvd_file, seek_sector,
1884 (size_t) numsec, secbuf, DVDINPUT_NOFLAGS );
1885 }
1886
1887 if( ret != (int) numsec ) {
1888 free( secbuf_base );
1889 return ret < 0 ? ret : 0;
1890 }
1891
1892 memcpy( data, &(secbuf[ seek_byte ]), byte_size );
1893 free( secbuf_base );
1894
1895 DVDFileSeekForce(dvd_file, dvd_file->seek_pos + byte_size, -1);
1896 return byte_size;
1897 }
1898
1899 ssize_t DVDFileSize( dvd_file_t *dvd_file )
1900 {
1901 /* Check arguments. */
1902 if( dvd_file == NULL )
1903 return -1;
1904
1905 return dvd_file->filesize;
1906 }
1907
1908 int DVDDiscID( dvd_reader_t *dvd, unsigned char *discid )
1909 {
1910 struct md5_s ctx;
1911 int title;
1912 int title_sets;
1913 int nr_of_files = 0;
1914 ifo_handle_t *vmg_ifo;
1915
1916 /* Check arguments. */
1917 if( dvd == NULL || discid == NULL )
1918 return 0;
1919
1920 vmg_ifo = ifoOpen( dvd, 0 );
1921 if( !vmg_ifo ) {
1922 Log0(dvd, "DVDDiscId, failed to open VMG IFO" );
1923 return -1;
1924 }
1925
1926 title_sets = vmg_ifo->vmgi_mat->vmg_nr_of_title_sets + 1;
1927 ifoClose( vmg_ifo );
1928
1929 if( title_sets > 10 )
1930 title_sets = 10;
1931
1932 /* Go through the first IFO:s, in order, up until the tenth,
1933 * and md5sum them, i.e VIDEO_TS.IFO and VTS_0?_0.IFO */
1934 InitMD5( &ctx );
1935 for( title = 0; title < title_sets; title++ ) {
1936 dvd_file_t *dvd_file = DVDOpenFile( dvd, title, DVD_READ_INFO_FILE );
1937 if( dvd_file != NULL ) {
1938 ssize_t bytes_read;
1939 ssize_t file_size = dvd_file->filesize * DVD_VIDEO_LB_LEN;
1940 char *buffer_base = malloc( file_size + 2048 );
1941
1942 if( buffer_base == NULL ) {
1943 DVDCloseFile( dvd_file );
1944 Log0(dvd, "DVDDiscId, failed to allocate memory for file read" );
1945 return -1;
1946 }
1947
1948 char *buffer = (char *)(((uintptr_t)buffer_base & ~((uintptr_t)2047)) + 2048);
1949
1950 bytes_read = DVDReadBytes( dvd_file, buffer, file_size );
1951 if( bytes_read != file_size ) {
1952 Log1(dvd, "DVDDiscId read returned %zd bytes"
1953 ", wanted %zd", bytes_read, file_size );
1954 DVDCloseFile( dvd_file );
1955 free( buffer_base );
1956 return -1;
1957 }
1958
1959 AddMD5( &ctx, buffer, file_size );
1960
1961 DVDCloseFile( dvd_file );
1962 free( buffer_base );
1963 nr_of_files++;
1964 }
1965 }
1966 EndMD5( &ctx );
1967 memcpy( discid, ctx.buf, 16 );
1968 if(!nr_of_files)
1969 return -1;
1970
1971 return 0;
1972 }
1973
1974
1975 int DVDISOVolumeInfo( dvd_reader_t *ctx,
1976 char *volid, unsigned int volid_size,
1977 unsigned char *volsetid, unsigned int volsetid_size )
1978 {
1979 dvd_reader_device_t *dvd = ctx->rd;
1980 unsigned char *buffer, *buffer_base;
1981 int ret;
1982
1983 /* Check arguments. */
1984 if( dvd == NULL )
1985 return 0;
1986
1987 if( dvd->dev == NULL ) {
1988 /* No block access, so no ISO... */
1989 return -1;
1990 }
1991
1992 buffer_base = malloc( DVD_VIDEO_LB_LEN + 2048 );
1993
1994 if( buffer_base == NULL ) {
1995 Log0(ctx, "DVDISOVolumeInfo, failed to "
1996 "allocate memory for file read" );
1997 return -1;
1998 }
1999
2000 buffer = (unsigned char *)(((uintptr_t)buffer_base & ~((uintptr_t)2047)) + 2048);
2001
2002 ret = InternalUDFReadBlocksRaw( ctx, 16, 1, buffer, 0 );
2003 if( ret != 1 ) {
2004 Log0(ctx, "DVDISOVolumeInfo, failed to "
2005 "read ISO9660 Primary Volume Descriptor" );
2006 free( buffer_base );
2007 return -1;
2008 }
2009
2010 if( (volid != NULL) && (volid_size > 0) ) {
2011 unsigned int n;
2012 for(n = 0; n < 32; n++) {
2013 if(buffer[40+n] == 0x20) {
2014 break;
2015 }
2016 }
2017
2018 if(volid_size > n+1) {
2019 volid_size = n+1;
2020 }
2021
2022 memcpy(volid, &buffer[40], volid_size-1);
2023 volid[volid_size-1] = '\0';
2024 }
2025
2026 if( (volsetid != NULL) && (volsetid_size > 0) ) {
2027 if(volsetid_size > 128) {
2028 volsetid_size = 128;
2029 }
2030 memcpy(volsetid, &buffer[190], volsetid_size);
2031 }
2032 free( buffer_base );
2033 return 0;
2034 }
2035
2036
2037 int DVDUDFVolumeInfo( dvd_reader_t *ctx,
2038 char *volid, unsigned int volid_size,
2039 unsigned char *volsetid, unsigned int volsetid_size )
2040 {
2041 int ret;
2042 /* Check arguments. */
2043 if( ctx == NULL || ctx->rd == NULL )
2044 return -1;
2045
2046 if( ctx->rd->dev == NULL ) {
2047 /* No block access, so no UDF VolumeSet Identifier */
2048 return -1;
2049 }
2050
2051 if( (volid != NULL) && (volid_size > 0) ) {
2052 ret = UDFGetVolumeIdentifier(ctx, volid, volid_size);
2053 if(!ret) {
2054 return -1;
2055 }
2056 }
2057 if( (volsetid != NULL) && (volsetid_size > 0) ) {
2058 ret = UDFGetVolumeSetIdentifier(ctx, volsetid, volsetid_size);
2059 if(!ret) {
2060 return -1;
2061 }
2062 }
2063
2064 return 0;
2065 }