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