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