Mercurial > mplayer.hg
comparison libmpdvdkit2/dvd_reader.c @ 7029:9db58ffbd73c
importing libdvdread 0.9.3 files
author | arpi |
---|---|
date | Fri, 16 Aug 2002 22:37:48 +0000 |
parents | |
children | 596919e4f601 |
comparison
equal
deleted
inserted
replaced
7028:9d4273713562 | 7029:9db58ffbd73c |
---|---|
1 /* | |
2 * Copyright (C) 2001, 2002 Billy Biggs <vektor@dumbterm.net>, | |
3 * Håkan Hjort <d95hjort@dtek.chalmers.se> | |
4 * | |
5 * This program is free software; you can redistribute it and/or modify | |
6 * it under the terms of the GNU General Public License as published by | |
7 * the Free Software Foundation; either version 2 of the License, or (at | |
8 * your option) any later version. | |
9 * | |
10 * This program is distributed in the hope that it will be useful, but | |
11 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 * General Public License for more details. | |
14 * | |
15 * You should have received a copy of the GNU General Public License | |
16 * along with this program; if not, write to the Free Software | |
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. | |
18 */ | |
19 | |
20 #include <sys/types.h> | |
21 #include <sys/stat.h> | |
22 #include <sys/time.h> /* For the timing of dvdcss_title crack. */ | |
23 #include <fcntl.h> | |
24 #include <stdlib.h> | |
25 #include <stdio.h> | |
26 #include <errno.h> | |
27 #include <string.h> | |
28 #include <unistd.h> | |
29 #include <limits.h> | |
30 #include <dirent.h> | |
31 | |
32 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__bsdi__)|| defined(__DARWIN__) | |
33 #define SYS_BSD 1 | |
34 #endif | |
35 | |
36 #if defined(__sun) | |
37 #include <sys/mnttab.h> | |
38 #elif defined(SYS_BSD) | |
39 #include <fstab.h> | |
40 #elif defined(__linux__) | |
41 #include <mntent.h> | |
42 #endif | |
43 | |
44 #include "dvd_udf.h" | |
45 #include "dvd_input.h" | |
46 #include "dvd_reader.h" | |
47 | |
48 struct dvd_reader_s { | |
49 /* Basic information. */ | |
50 int isImageFile; | |
51 | |
52 /* Hack for keeping track of the css status. | |
53 * 0: no css, 1: perhaps (need init of keys), 2: have done init */ | |
54 int css_state; | |
55 int css_title; /* Last title that we have called DVDinpute_title for. */ | |
56 | |
57 /* Information required for an image file. */ | |
58 dvd_input_t dev; | |
59 | |
60 /* Information required for a directory path drive. */ | |
61 char *path_root; | |
62 }; | |
63 | |
64 struct dvd_file_s { | |
65 /* Basic information. */ | |
66 dvd_reader_t *dvd; | |
67 | |
68 /* Hack for selecting the right css title. */ | |
69 int css_title; | |
70 | |
71 /* Information required for an image file. */ | |
72 uint32_t lb_start; | |
73 uint32_t seek_pos; | |
74 | |
75 /* Information required for a directory path drive. */ | |
76 size_t title_sizes[ 9 ]; | |
77 dvd_input_t title_devs[ 9 ]; | |
78 | |
79 /* Calculated at open-time, size in blocks. */ | |
80 ssize_t filesize; | |
81 }; | |
82 | |
83 /* Loop over all titles and call dvdcss_title to crack the keys. */ | |
84 static int initAllCSSKeys( dvd_reader_t *dvd ) | |
85 { | |
86 struct timeval all_s, all_e; | |
87 struct timeval t_s, t_e; | |
88 char filename[ MAX_UDF_FILE_NAME_LEN ]; | |
89 uint32_t start, len; | |
90 int title; | |
91 | |
92 fprintf( stderr, "\n" ); | |
93 fprintf( stderr, "libdvdread: Attempting to retrieve all CSS keys\n" ); | |
94 fprintf( stderr, "libdvdread: This can take a _long_ time, " | |
95 "please be patient\n\n" ); | |
96 | |
97 gettimeofday(&all_s, NULL); | |
98 | |
99 for( title = 0; title < 100; title++ ) { | |
100 gettimeofday( &t_s, NULL ); | |
101 if( title == 0 ) { | |
102 sprintf( filename, "/VIDEO_TS/VIDEO_TS.VOB" ); | |
103 } else { | |
104 sprintf( filename, "/VIDEO_TS/VTS_%02d_%d.VOB", title, 0 ); | |
105 } | |
106 start = UDFFindFile( dvd, filename, &len ); | |
107 if( start != 0 && len != 0 ) { | |
108 /* Perform CSS key cracking for this title. */ | |
109 fprintf( stderr, "libdvdread: Get key for %s at 0x%08x\n", | |
110 filename, start ); | |
111 if( DVDinput_title( dvd->dev, (int)start ) < 0 ) { | |
112 fprintf( stderr, "libdvdread: Error cracking CSS key for %s (0x%08x)\n", filename, start); | |
113 } | |
114 gettimeofday( &t_e, NULL ); | |
115 fprintf( stderr, "libdvdread: Elapsed time %ld\n", | |
116 (long int) t_e.tv_sec - t_s.tv_sec ); | |
117 } | |
118 | |
119 if( title == 0 ) continue; | |
120 | |
121 gettimeofday( &t_s, NULL ); | |
122 sprintf( filename, "/VIDEO_TS/VTS_%02d_%d.VOB", title, 1 ); | |
123 start = UDFFindFile( dvd, filename, &len ); | |
124 if( start == 0 || len == 0 ) break; | |
125 | |
126 /* Perform CSS key cracking for this title. */ | |
127 fprintf( stderr, "libdvdread: Get key for %s at 0x%08x\n", | |
128 filename, start ); | |
129 if( DVDinput_title( dvd->dev, (int)start ) < 0 ) { | |
130 fprintf( stderr, "libdvdread: Error cracking CSS key for %s (0x%08x)!!\n", filename, start); | |
131 } | |
132 gettimeofday( &t_e, NULL ); | |
133 fprintf( stderr, "libdvdread: Elapsed time %ld\n", | |
134 (long int) t_e.tv_sec - t_s.tv_sec ); | |
135 } | |
136 title--; | |
137 | |
138 fprintf( stderr, "libdvdread: Found %d VTS's\n", title ); | |
139 gettimeofday(&all_e, NULL); | |
140 fprintf( stderr, "libdvdread: Elapsed time %ld\n", | |
141 (long int) all_e.tv_sec - all_s.tv_sec ); | |
142 | |
143 return 0; | |
144 } | |
145 | |
146 | |
147 | |
148 /** | |
149 * Open a DVD image or block device file. | |
150 */ | |
151 static dvd_reader_t *DVDOpenImageFile( const char *location, int have_css ) | |
152 { | |
153 dvd_reader_t *dvd; | |
154 dvd_input_t dev; | |
155 | |
156 dev = DVDinput_open( location ); | |
157 if( !dev ) { | |
158 fprintf( stderr, "libdvdread: Can't open %s for reading\n", location ); | |
159 return 0; | |
160 } | |
161 | |
162 dvd = (dvd_reader_t *) malloc( sizeof( dvd_reader_t ) ); | |
163 if( !dvd ) return 0; | |
164 dvd->isImageFile = 1; | |
165 dvd->dev = dev; | |
166 dvd->path_root = 0; | |
167 | |
168 if( have_css ) { | |
169 /* Only if DVDCSS_METHOD = title, a bit if it's disc or if | |
170 * DVDCSS_METHOD = key but region missmatch. Unfortunaly we | |
171 * don't have that information. */ | |
172 | |
173 dvd->css_state = 1; /* Need key init. */ | |
174 } | |
175 | |
176 return dvd; | |
177 } | |
178 | |
179 static dvd_reader_t *DVDOpenPath( const char *path_root ) | |
180 { | |
181 dvd_reader_t *dvd; | |
182 | |
183 dvd = (dvd_reader_t *) malloc( sizeof( dvd_reader_t ) ); | |
184 if( !dvd ) return 0; | |
185 dvd->isImageFile = 0; | |
186 dvd->dev = 0; | |
187 dvd->path_root = strdup( path_root ); | |
188 | |
189 return dvd; | |
190 } | |
191 | |
192 #if defined(__sun) | |
193 /* /dev/rdsk/c0t6d0s0 (link to /devices/...) | |
194 /vol/dev/rdsk/c0t6d0/?? | |
195 /vol/rdsk/<name> */ | |
196 static char *sun_block2char( const char *path ) | |
197 { | |
198 char *new_path; | |
199 | |
200 /* Must contain "/dsk/" */ | |
201 if( !strstr( path, "/dsk/" ) ) return (char *) strdup( path ); | |
202 | |
203 /* Replace "/dsk/" with "/rdsk/" */ | |
204 new_path = malloc( strlen(path) + 2 ); | |
205 strcpy( new_path, path ); | |
206 strcpy( strstr( new_path, "/dsk/" ), "" ); | |
207 strcat( new_path, "/rdsk/" ); | |
208 strcat( new_path, strstr( path, "/dsk/" ) + strlen( "/dsk/" ) ); | |
209 | |
210 return new_path; | |
211 } | |
212 #endif | |
213 | |
214 #if defined(SYS_BSD) | |
215 /* FreeBSD /dev/(r)(a)cd0c (a is for atapi), recomended to _not_ use r | |
216 OpenBSD /dev/rcd0c, it needs to be the raw device | |
217 NetBSD /dev/rcd0[d|c|..] d for x86, c (for non x86), perhaps others | |
218 Darwin /dev/rdisk0, it needs to be the raw device | |
219 BSD/OS /dev/sr0c (if not mounted) or /dev/rsr0c ('c' any letter will do) */ | |
220 static char *bsd_block2char( const char *path ) | |
221 { | |
222 char *new_path; | |
223 | |
224 /* If it doesn't start with "/dev/" or does start with "/dev/r" exit */ | |
225 if( !strncmp( path, "/dev/", 5 ) || strncmp( path, "/dev/r", 6 ) ) | |
226 return (char *) strdup( path ); | |
227 | |
228 /* Replace "/dev/" with "/dev/r" */ | |
229 new_path = malloc( strlen(path) + 2 ); | |
230 strcpy( new_path, "/dev/r" ); | |
231 strcat( new_path, path + strlen( "/dev/" ) ); | |
232 | |
233 return new_path; | |
234 } | |
235 #endif | |
236 | |
237 dvd_reader_t *DVDOpen( const char *path ) | |
238 { | |
239 struct stat fileinfo; | |
240 int ret, have_css; | |
241 char *dev_name = 0; | |
242 | |
243 if( !path ) return 0; | |
244 | |
245 ret = stat( path, &fileinfo ); | |
246 if( ret < 0 ) { | |
247 /* If we can't stat the file, give up */ | |
248 fprintf( stderr, "libdvdread: Can't stat %s\n", path ); | |
249 perror(""); | |
250 return 0; | |
251 } | |
252 | |
253 /* Try to open libdvdcss or fall back to standard functions */ | |
254 have_css = DVDInputSetup(); | |
255 | |
256 /* First check if this is a block/char device or a file*/ | |
257 if( S_ISBLK( fileinfo.st_mode ) || | |
258 S_ISCHR( fileinfo.st_mode ) || | |
259 S_ISREG( fileinfo.st_mode ) ) { | |
260 | |
261 /** | |
262 * Block devices and regular files are assumed to be DVD-Video images. | |
263 */ | |
264 #if defined(__sun) | |
265 return DVDOpenImageFile( sun_block2char( path ), have_css ); | |
266 #elif defined(SYS_BSD) | |
267 return DVDOpenImageFile( bsd_block2char( path ), have_css ); | |
268 #else | |
269 return DVDOpenImageFile( path, have_css ); | |
270 #endif | |
271 | |
272 } else if( S_ISDIR( fileinfo.st_mode ) ) { | |
273 dvd_reader_t *auth_drive = 0; | |
274 char *path_copy; | |
275 #if defined(SYS_BSD) | |
276 struct fstab* fe; | |
277 #elif defined(__sun) || defined(__linux__) | |
278 FILE *mntfile; | |
279 #endif | |
280 | |
281 /* XXX: We should scream real loud here. */ | |
282 if( !(path_copy = strdup( path ) ) ) return 0; | |
283 | |
284 /* Resolve any symlinks and get the absolut dir name. */ | |
285 { | |
286 char *new_path; | |
287 int cdir = open( ".", O_RDONLY ); | |
288 | |
289 if( cdir >= 0 ) { | |
290 chdir( path_copy ); | |
291 new_path = getcwd( NULL, PATH_MAX ); | |
292 fchdir( cdir ); | |
293 close( cdir ); | |
294 if( new_path ) { | |
295 free( path_copy ); | |
296 path_copy = new_path; | |
297 } | |
298 } | |
299 } | |
300 | |
301 /** | |
302 * If we're being asked to open a directory, check if that directory | |
303 * is the mountpoint for a DVD-ROM which we can use instead. | |
304 */ | |
305 | |
306 if( strlen( path_copy ) > 1 ) { | |
307 if( path_copy[ strlen( path_copy ) - 1 ] == '/' ) | |
308 path_copy[ strlen( path_copy ) - 1 ] = '\0'; | |
309 } | |
310 | |
311 if( strlen( path_copy ) > 9 ) { | |
312 if( !strcasecmp( &(path_copy[ strlen( path_copy ) - 9 ]), | |
313 "/video_ts" ) ) { | |
314 path_copy[ strlen( path_copy ) - 9 ] = '\0'; | |
315 } | |
316 } | |
317 | |
318 #if defined(SYS_BSD) | |
319 if( ( fe = getfsfile( path_copy ) ) ) { | |
320 dev_name = bsd_block2char( fe->fs_spec ); | |
321 fprintf( stderr, | |
322 "libdvdread: Attempting to use device %s" | |
323 " mounted on %s for CSS authentication\n", | |
324 dev_name, | |
325 fe->fs_file ); | |
326 auth_drive = DVDOpenImageFile( dev_name, have_css ); | |
327 } | |
328 #elif defined(__sun) | |
329 mntfile = fopen( MNTTAB, "r" ); | |
330 if( mntfile ) { | |
331 struct mnttab mp; | |
332 int res; | |
333 | |
334 while( ( res = getmntent( mntfile, &mp ) ) != -1 ) { | |
335 if( res == 0 && !strcmp( mp.mnt_mountp, path_copy ) ) { | |
336 dev_name = sun_block2char( mp.mnt_special ); | |
337 fprintf( stderr, | |
338 "libdvdread: Attempting to use device %s" | |
339 " mounted on %s for CSS authentication\n", | |
340 dev_name, | |
341 mp.mnt_mountp ); | |
342 auth_drive = DVDOpenImageFile( dev_name, have_css ); | |
343 break; | |
344 } | |
345 } | |
346 fclose( mntfile ); | |
347 } | |
348 #elif defined(__linux__) | |
349 mntfile = fopen( MOUNTED, "r" ); | |
350 if( mntfile ) { | |
351 struct mntent *me; | |
352 | |
353 while( ( me = getmntent( mntfile ) ) ) { | |
354 if( !strcmp( me->mnt_dir, path_copy ) ) { | |
355 fprintf( stderr, | |
356 "libdvdread: Attempting to use device %s" | |
357 " mounted on %s for CSS authentication\n", | |
358 me->mnt_fsname, | |
359 me->mnt_dir ); | |
360 auth_drive = DVDOpenImageFile( me->mnt_fsname, have_css ); | |
361 dev_name = strdup(me->mnt_fsname); | |
362 break; | |
363 } | |
364 } | |
365 fclose( mntfile ); | |
366 } | |
367 #endif | |
368 if( !dev_name ) { | |
369 fprintf( stderr, "libdvdread: Couldn't find device name.\n" ); | |
370 } else if( !auth_drive ) { | |
371 fprintf( stderr, "libdvdread: Device %s inaccessible, " | |
372 "CSS authentication not available.\n", dev_name ); | |
373 } | |
374 | |
375 free( dev_name ); | |
376 free( path_copy ); | |
377 | |
378 /** | |
379 * If we've opened a drive, just use that. | |
380 */ | |
381 if( auth_drive ) return auth_drive; | |
382 | |
383 /** | |
384 * Otherwise, we now try to open the directory tree instead. | |
385 */ | |
386 return DVDOpenPath( path ); | |
387 } | |
388 | |
389 /* If it's none of the above, screw it. */ | |
390 fprintf( stderr, "libdvdread: Could not open %s\n", path ); | |
391 return 0; | |
392 } | |
393 | |
394 void DVDClose( dvd_reader_t *dvd ) | |
395 { | |
396 if( dvd ) { | |
397 if( dvd->dev ) DVDinput_close( dvd->dev ); | |
398 if( dvd->path_root ) free( dvd->path_root ); | |
399 free( dvd ); | |
400 dvd = 0; | |
401 } | |
402 } | |
403 | |
404 /** | |
405 * Open an unencrypted file on a DVD image file. | |
406 */ | |
407 static dvd_file_t *DVDOpenFileUDF( dvd_reader_t *dvd, char *filename ) | |
408 { | |
409 uint32_t start, len; | |
410 dvd_file_t *dvd_file; | |
411 | |
412 start = UDFFindFile( dvd, filename, &len ); | |
413 if( !start ) return 0; | |
414 | |
415 dvd_file = (dvd_file_t *) malloc( sizeof( dvd_file_t ) ); | |
416 if( !dvd_file ) return 0; | |
417 dvd_file->dvd = dvd; | |
418 dvd_file->lb_start = start; | |
419 dvd_file->seek_pos = 0; | |
420 memset( dvd_file->title_sizes, 0, sizeof( dvd_file->title_sizes ) ); | |
421 memset( dvd_file->title_devs, 0, sizeof( dvd_file->title_devs ) ); | |
422 dvd_file->filesize = len / DVD_VIDEO_LB_LEN; | |
423 | |
424 return dvd_file; | |
425 } | |
426 | |
427 /** | |
428 * Searches for <file> in directory <path>, ignoring case. | |
429 * Returns 0 and full filename in <filename>. | |
430 * or -1 on file not found. | |
431 * or -2 on path not found. | |
432 */ | |
433 static int findDirFile( const char *path, const char *file, char *filename ) | |
434 { | |
435 DIR *dir; | |
436 struct dirent *ent; | |
437 | |
438 dir = opendir( path ); | |
439 if( !dir ) return -2; | |
440 | |
441 while( ( ent = readdir( dir ) ) != NULL ) { | |
442 if( !strcasecmp( ent->d_name, file ) ) { | |
443 sprintf( filename, "%s%s%s", path, | |
444 ( ( path[ strlen( path ) - 1 ] == '/' ) ? "" : "/" ), | |
445 ent->d_name ); | |
446 return 0; | |
447 } | |
448 } | |
449 | |
450 return -1; | |
451 } | |
452 | |
453 static int findDVDFile( dvd_reader_t *dvd, const char *file, char *filename ) | |
454 { | |
455 char video_path[ PATH_MAX + 1 ]; | |
456 const char *nodirfile; | |
457 int ret; | |
458 | |
459 /* Strip off the directory for our search */ | |
460 if( !strncasecmp( "/VIDEO_TS/", file, 10 ) ) { | |
461 nodirfile = &(file[ 10 ]); | |
462 } else { | |
463 nodirfile = file; | |
464 } | |
465 | |
466 ret = findDirFile( dvd->path_root, nodirfile, filename ); | |
467 if( ret < 0 ) { | |
468 /* Try also with adding the path, just in case. */ | |
469 sprintf( video_path, "%s/VIDEO_TS/", dvd->path_root ); | |
470 ret = findDirFile( video_path, nodirfile, filename ); | |
471 if( ret < 0 ) { | |
472 /* Try with the path, but in lower case. */ | |
473 sprintf( video_path, "%s/video_ts/", dvd->path_root ); | |
474 ret = findDirFile( video_path, nodirfile, filename ); | |
475 if( ret < 0 ) { | |
476 return 0; | |
477 } | |
478 } | |
479 } | |
480 | |
481 return 1; | |
482 } | |
483 | |
484 /** | |
485 * Open an unencrypted file from a DVD directory tree. | |
486 */ | |
487 static dvd_file_t *DVDOpenFilePath( dvd_reader_t *dvd, char *filename ) | |
488 { | |
489 char full_path[ PATH_MAX + 1 ]; | |
490 dvd_file_t *dvd_file; | |
491 struct stat fileinfo; | |
492 dvd_input_t dev; | |
493 | |
494 /* Get the full path of the file. */ | |
495 if( !findDVDFile( dvd, filename, full_path ) ) return 0; | |
496 | |
497 dev = DVDinput_open( full_path ); | |
498 if( !dev ) return 0; | |
499 | |
500 dvd_file = (dvd_file_t *) malloc( sizeof( dvd_file_t ) ); | |
501 if( !dvd_file ) return 0; | |
502 dvd_file->dvd = dvd; | |
503 dvd_file->lb_start = 0; | |
504 dvd_file->seek_pos = 0; | |
505 memset( dvd_file->title_sizes, 0, sizeof( dvd_file->title_sizes ) ); | |
506 memset( dvd_file->title_devs, 0, sizeof( dvd_file->title_devs ) ); | |
507 dvd_file->filesize = 0; | |
508 | |
509 if( stat( full_path, &fileinfo ) < 0 ) { | |
510 fprintf( stderr, "libdvdread: Can't stat() %s.\n", filename ); | |
511 free( dvd_file ); | |
512 return 0; | |
513 } | |
514 dvd_file->title_sizes[ 0 ] = fileinfo.st_size / DVD_VIDEO_LB_LEN; | |
515 dvd_file->title_devs[ 0 ] = dev; | |
516 dvd_file->filesize = dvd_file->title_sizes[ 0 ]; | |
517 | |
518 return dvd_file; | |
519 } | |
520 | |
521 static dvd_file_t *DVDOpenVOBUDF( dvd_reader_t *dvd, int title, int menu ) | |
522 { | |
523 char filename[ MAX_UDF_FILE_NAME_LEN ]; | |
524 uint32_t start, len; | |
525 dvd_file_t *dvd_file; | |
526 | |
527 if( title == 0 ) { | |
528 sprintf( filename, "/VIDEO_TS/VIDEO_TS.VOB" ); | |
529 } else { | |
530 sprintf( filename, "/VIDEO_TS/VTS_%02d_%d.VOB", title, menu ? 0 : 1 ); | |
531 } | |
532 start = UDFFindFile( dvd, filename, &len ); | |
533 if( start == 0 ) return 0; | |
534 | |
535 dvd_file = (dvd_file_t *) malloc( sizeof( dvd_file_t ) ); | |
536 if( !dvd_file ) return 0; | |
537 dvd_file->dvd = dvd; | |
538 /*Hack*/ dvd_file->css_title = title << 1 | menu; | |
539 dvd_file->lb_start = start; | |
540 dvd_file->seek_pos = 0; | |
541 memset( dvd_file->title_sizes, 0, sizeof( dvd_file->title_sizes ) ); | |
542 memset( dvd_file->title_devs, 0, sizeof( dvd_file->title_devs ) ); | |
543 dvd_file->filesize = len / DVD_VIDEO_LB_LEN; | |
544 | |
545 /* Calculate the complete file size for every file in the VOBS */ | |
546 if( !menu ) { | |
547 int cur; | |
548 | |
549 for( cur = 2; cur < 10; cur++ ) { | |
550 sprintf( filename, "/VIDEO_TS/VTS_%02d_%d.VOB", title, cur ); | |
551 if( !UDFFindFile( dvd, filename, &len ) ) break; | |
552 dvd_file->filesize += len / DVD_VIDEO_LB_LEN; | |
553 } | |
554 } | |
555 | |
556 if( dvd->css_state == 1 /* Need key init */ ) { | |
557 initAllCSSKeys( dvd ); | |
558 dvd->css_state = 2; | |
559 } | |
560 /* | |
561 if( DVDinput_seek( dvd_file->dvd->dev, | |
562 (int)start, DVDINPUT_SEEK_KEY ) < 0 ) { | |
563 fprintf( stderr, "libdvdread: Error cracking CSS key for %s\n", | |
564 filename ); | |
565 } | |
566 */ | |
567 | |
568 return dvd_file; | |
569 } | |
570 | |
571 static dvd_file_t *DVDOpenVOBPath( dvd_reader_t *dvd, int title, int menu ) | |
572 { | |
573 char filename[ MAX_UDF_FILE_NAME_LEN ]; | |
574 char full_path[ PATH_MAX + 1 ]; | |
575 struct stat fileinfo; | |
576 dvd_file_t *dvd_file; | |
577 int i; | |
578 | |
579 dvd_file = (dvd_file_t *) malloc( sizeof( dvd_file_t ) ); | |
580 if( !dvd_file ) return 0; | |
581 dvd_file->dvd = dvd; | |
582 /*Hack*/ dvd_file->css_title = title << 1 | menu; | |
583 dvd_file->lb_start = 0; | |
584 dvd_file->seek_pos = 0; | |
585 memset( dvd_file->title_sizes, 0, sizeof( dvd_file->title_sizes ) ); | |
586 memset( dvd_file->title_devs, 0, sizeof( dvd_file->title_devs ) ); | |
587 dvd_file->filesize = 0; | |
588 | |
589 if( menu ) { | |
590 dvd_input_t dev; | |
591 | |
592 if( title == 0 ) { | |
593 sprintf( filename, "VIDEO_TS.VOB" ); | |
594 } else { | |
595 sprintf( filename, "VTS_%02i_0.VOB", title ); | |
596 } | |
597 if( !findDVDFile( dvd, filename, full_path ) ) { | |
598 free( dvd_file ); | |
599 return 0; | |
600 } | |
601 | |
602 dev = DVDinput_open( full_path ); | |
603 if( dev == NULL ) { | |
604 free( dvd_file ); | |
605 return 0; | |
606 } | |
607 | |
608 if( stat( full_path, &fileinfo ) < 0 ) { | |
609 fprintf( stderr, "libdvdread: Can't stat() %s.\n", filename ); | |
610 free( dvd_file ); | |
611 return 0; | |
612 } | |
613 dvd_file->title_sizes[ 0 ] = fileinfo.st_size / DVD_VIDEO_LB_LEN; | |
614 dvd_file->title_devs[ 0 ] = dev; | |
615 DVDinput_seek( dvd_file->title_devs[0], 0, DVDINPUT_SEEK_KEY ); | |
616 dvd_file->filesize = dvd_file->title_sizes[ 0 ]; | |
617 | |
618 } else { | |
619 for( i = 0; i < 9; ++i ) { | |
620 | |
621 sprintf( filename, "VTS_%02i_%i.VOB", title, i + 1 ); | |
622 if( !findDVDFile( dvd, filename, full_path ) ) { | |
623 break; | |
624 } | |
625 | |
626 if( stat( full_path, &fileinfo ) < 0 ) { | |
627 fprintf( stderr, "libdvdread: Can't stat() %s.\n", filename ); | |
628 break; | |
629 } | |
630 | |
631 dvd_file->title_sizes[ i ] = fileinfo.st_size / DVD_VIDEO_LB_LEN; | |
632 dvd_file->title_devs[ i ] = DVDinput_open( full_path ); | |
633 dvd_file->filesize += dvd_file->title_sizes[ i ]; | |
634 } | |
635 if( dvd_file->title_devs[ 0 ] ) { | |
636 DVDinput_seek( dvd_file->title_devs[ 0 ], 0, DVDINPUT_SEEK_KEY ); | |
637 } else { | |
638 free( dvd_file ); | |
639 return 0; | |
640 } | |
641 } | |
642 | |
643 return dvd_file; | |
644 } | |
645 | |
646 dvd_file_t *DVDOpenFile( dvd_reader_t *dvd, int titlenum, | |
647 dvd_read_domain_t domain ) | |
648 { | |
649 char filename[ MAX_UDF_FILE_NAME_LEN ]; | |
650 | |
651 switch( domain ) { | |
652 case DVD_READ_INFO_FILE: | |
653 if( titlenum == 0 ) { | |
654 sprintf( filename, "/VIDEO_TS/VIDEO_TS.IFO" ); | |
655 } else { | |
656 sprintf( filename, "/VIDEO_TS/VTS_%02i_0.IFO", titlenum ); | |
657 } | |
658 break; | |
659 case DVD_READ_INFO_BACKUP_FILE: | |
660 if( titlenum == 0 ) { | |
661 sprintf( filename, "/VIDEO_TS/VIDEO_TS.BUP" ); | |
662 } else { | |
663 sprintf( filename, "/VIDEO_TS/VTS_%02i_0.BUP", titlenum ); | |
664 } | |
665 break; | |
666 case DVD_READ_MENU_VOBS: | |
667 if( dvd->isImageFile ) { | |
668 return DVDOpenVOBUDF( dvd, titlenum, 1 ); | |
669 } else { | |
670 return DVDOpenVOBPath( dvd, titlenum, 1 ); | |
671 } | |
672 break; | |
673 case DVD_READ_TITLE_VOBS: | |
674 if( titlenum == 0 ) return 0; | |
675 if( dvd->isImageFile ) { | |
676 return DVDOpenVOBUDF( dvd, titlenum, 0 ); | |
677 } else { | |
678 return DVDOpenVOBPath( dvd, titlenum, 0 ); | |
679 } | |
680 break; | |
681 default: | |
682 fprintf( stderr, "libdvdread: Invalid domain for file open.\n" ); | |
683 return 0; | |
684 } | |
685 | |
686 if( dvd->isImageFile ) { | |
687 return DVDOpenFileUDF( dvd, filename ); | |
688 } else { | |
689 return DVDOpenFilePath( dvd, filename ); | |
690 } | |
691 } | |
692 | |
693 void DVDCloseFile( dvd_file_t *dvd_file ) | |
694 { | |
695 int i; | |
696 | |
697 if( dvd_file ) { | |
698 if( dvd_file->dvd->isImageFile ) { | |
699 ; | |
700 } else { | |
701 for( i = 0; i < 9; ++i ) { | |
702 if( dvd_file->title_devs[ i ] ) { | |
703 DVDinput_close( dvd_file->title_devs[i] ); | |
704 } | |
705 } | |
706 } | |
707 | |
708 free( dvd_file ); | |
709 dvd_file = 0; | |
710 } | |
711 } | |
712 | |
713 /* Internal, but used from dvd_udf.c */ | |
714 int DVDReadBlocksUDFRaw( dvd_reader_t *device, uint32_t lb_number, | |
715 size_t block_count, unsigned char *data, | |
716 int encrypted ) | |
717 { | |
718 int ret; | |
719 | |
720 if( !device->dev ) { | |
721 fprintf( stderr, "libdvdread: Fatal error in block read.\n" ); | |
722 return 0; | |
723 } | |
724 | |
725 ret = DVDinput_seek( device->dev, (int) lb_number, DVDINPUT_NOFLAGS ); | |
726 if( ret != (int) lb_number ) { | |
727 fprintf( stderr, "libdvdread: Can't seek to block %u\n", lb_number ); | |
728 return 0; | |
729 } | |
730 | |
731 return DVDinput_read( device->dev, (char *) data, | |
732 (int) block_count, encrypted ); | |
733 } | |
734 | |
735 /* This is using a single input and starting from 'dvd_file->lb_start' offset. | |
736 * | |
737 * Reads 'block_count' blocks from 'dvd_file' at block offset 'offset' | |
738 * into the buffer located at 'data' and if 'encrypted' is set | |
739 * descramble the data if it's encrypted. Returning either an | |
740 * negative error or the number of blocks read. */ | |
741 static int DVDReadBlocksUDF( dvd_file_t *dvd_file, uint32_t offset, | |
742 size_t block_count, unsigned char *data, | |
743 int encrypted ) | |
744 { | |
745 return DVDReadBlocksUDFRaw( dvd_file->dvd, dvd_file->lb_start + offset, | |
746 block_count, data, encrypted ); | |
747 } | |
748 | |
749 /* This is using possibly several inputs and starting from an offset of '0'. | |
750 * | |
751 * Reads 'block_count' blocks from 'dvd_file' at block offset 'offset' | |
752 * into the buffer located at 'data' and if 'encrypted' is set | |
753 * descramble the data if it's encrypted. Returning either an | |
754 * negative error or the number of blocks read. */ | |
755 static int DVDReadBlocksPath( dvd_file_t *dvd_file, unsigned int offset, | |
756 size_t block_count, unsigned char *data, | |
757 int encrypted ) | |
758 { | |
759 int i; | |
760 int ret, ret2, off; | |
761 | |
762 ret = 0; | |
763 ret2 = 0; | |
764 for( i = 0; i < 9; ++i ) { | |
765 if( !dvd_file->title_sizes[ i ] ) return 0; /* Past end of file */ | |
766 | |
767 if( offset < dvd_file->title_sizes[ i ] ) { | |
768 if( ( offset + block_count ) <= dvd_file->title_sizes[ i ] ) { | |
769 off = DVDinput_seek( dvd_file->title_devs[ i ], | |
770 (int)offset, DVDINPUT_NOFLAGS ); | |
771 if( off < 0 || off != (int)offset ) { | |
772 fprintf( stderr, "libdvdread: Can't seek to block %d\n", | |
773 offset ); | |
774 return off < 0 ? off : 0; | |
775 } | |
776 ret = DVDinput_read( dvd_file->title_devs[ i ], data, | |
777 (int)block_count, encrypted ); | |
778 break; | |
779 } else { | |
780 size_t part1_size = dvd_file->title_sizes[ i ] - offset; | |
781 /* FIXME: Really needs to be a while loop. | |
782 * (This is only true if you try and read >1GB at a time) */ | |
783 | |
784 /* Read part 1 */ | |
785 off = DVDinput_seek( dvd_file->title_devs[ i ], | |
786 (int)offset, DVDINPUT_NOFLAGS ); | |
787 if( off < 0 || off != (int)offset ) { | |
788 fprintf( stderr, "libdvdread: Can't seek to block %d\n", | |
789 offset ); | |
790 return off < 0 ? off : 0; | |
791 } | |
792 ret = DVDinput_read( dvd_file->title_devs[ i ], data, | |
793 (int)part1_size, encrypted ); | |
794 if( ret < 0 ) return ret; | |
795 /* FIXME: This is wrong if i is the last file in the set. | |
796 * also error from this read will not show in ret. */ | |
797 | |
798 /* Read part 2 */ | |
799 off = DVDinput_seek( dvd_file->title_devs[ i + 1 ], | |
800 0, DVDINPUT_NOFLAGS ); | |
801 if( off < 0 || off != 0 ) { | |
802 fprintf( stderr, "libdvdread: Can't seek to block %d\n", | |
803 0 ); | |
804 return off < 0 ? off : 0; | |
805 } | |
806 ret2 = DVDinput_read( dvd_file->title_devs[ i + 1 ], | |
807 data + ( part1_size | |
808 * (int64_t)DVD_VIDEO_LB_LEN ), | |
809 (int)(block_count - part1_size), | |
810 encrypted ); | |
811 if( ret2 < 0 ) return ret2; | |
812 break; | |
813 } | |
814 } else { | |
815 offset -= dvd_file->title_sizes[ i ]; | |
816 } | |
817 } | |
818 | |
819 return ret + ret2; | |
820 } | |
821 | |
822 /* This is broken reading more than 2Gb at a time is ssize_t is 32-bit. */ | |
823 ssize_t DVDReadBlocks( dvd_file_t *dvd_file, int offset, | |
824 size_t block_count, unsigned char *data ) | |
825 { | |
826 int ret; | |
827 | |
828 /* Hack, and it will still fail for multiple opens in a threaded app ! */ | |
829 if( dvd_file->dvd->css_title != dvd_file->css_title ) { | |
830 dvd_file->dvd->css_title = dvd_file->css_title; | |
831 if( dvd_file->dvd->isImageFile ) { | |
832 DVDinput_title( dvd_file->dvd->dev, (int)dvd_file->lb_start ); | |
833 } else { | |
834 DVDinput_title( dvd_file->title_devs[ 0 ], (int)dvd_file->lb_start ); | |
835 } | |
836 } | |
837 | |
838 if( dvd_file->dvd->isImageFile ) { | |
839 ret = DVDReadBlocksUDF( dvd_file, (uint32_t)offset, | |
840 block_count, data, DVDINPUT_READ_DECRYPT ); | |
841 } else { | |
842 ret = DVDReadBlocksPath( dvd_file, (unsigned int)offset, | |
843 block_count, data, DVDINPUT_READ_DECRYPT ); | |
844 } | |
845 | |
846 return (ssize_t)ret; | |
847 } | |
848 | |
849 int32_t DVDFileSeek( dvd_file_t *dvd_file, int32_t offset ) | |
850 { | |
851 if( offset > dvd_file->filesize * DVD_VIDEO_LB_LEN ) { | |
852 return -1; | |
853 } | |
854 dvd_file->seek_pos = (uint32_t) offset; | |
855 return offset; | |
856 } | |
857 | |
858 ssize_t DVDReadBytes( dvd_file_t *dvd_file, void *data, size_t byte_size ) | |
859 { | |
860 unsigned char *secbuf; | |
861 unsigned int numsec, seek_sector, seek_byte; | |
862 int ret; | |
863 | |
864 seek_sector = dvd_file->seek_pos / DVD_VIDEO_LB_LEN; | |
865 seek_byte = dvd_file->seek_pos % DVD_VIDEO_LB_LEN; | |
866 | |
867 numsec = ( ( seek_byte + byte_size ) / DVD_VIDEO_LB_LEN ) + 1; | |
868 secbuf = (unsigned char *) malloc( numsec * DVD_VIDEO_LB_LEN ); | |
869 if( !secbuf ) { | |
870 fprintf( stderr, "libdvdread: Can't allocate memory " | |
871 "for file read!\n" ); | |
872 return 0; | |
873 } | |
874 | |
875 if( dvd_file->dvd->isImageFile ) { | |
876 ret = DVDReadBlocksUDF( dvd_file, (uint32_t) seek_sector, | |
877 (size_t) numsec, secbuf, DVDINPUT_NOFLAGS ); | |
878 } else { | |
879 ret = DVDReadBlocksPath( dvd_file, seek_sector, | |
880 (size_t) numsec, secbuf, DVDINPUT_NOFLAGS ); | |
881 } | |
882 | |
883 if( ret != (int) numsec ) { | |
884 free( secbuf ); | |
885 return ret < 0 ? ret : 0; | |
886 } | |
887 | |
888 memcpy( data, &(secbuf[ seek_byte ]), byte_size ); | |
889 free( secbuf ); | |
890 | |
891 dvd_file->seek_pos += byte_size; | |
892 return byte_size; | |
893 } | |
894 | |
895 ssize_t DVDFileSize( dvd_file_t *dvd_file ) | |
896 { | |
897 return dvd_file->filesize; | |
898 } |