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