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