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