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