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