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