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