Debian Codegraph @

src/dvd_reader.c

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