Mercurial > mplayer.hg
annotate libdvdcss/libdvdcss.c @ 35952:691431d2289e
Update internal libdvdcss to Git revision 39e0e2b.
author | diego |
---|---|
date | Sat, 23 Mar 2013 14:32:26 +0000 |
parents | ee9664187153 |
children |
rev | line source |
---|---|
20613 | 1 /* libdvdcss.c: DVD reading library. |
2 * | |
27442 | 3 * Authors: Stéphane Borel <stef@via.ecp.fr> |
27462 | 4 * Sam Hocevar <sam@zoy.org> |
27442 | 5 * Håkan Hjort <d95hjort@dtek.chalmers.se> |
20613 | 6 * |
27462 | 7 * Copyright (C) 1998-2008 VideoLAN |
20613 | 8 * |
31098
9e9595c779cf
libdvdcss: cosmetics: Fix FSF address and program name in license headers.
diego
parents:
27462
diff
changeset
|
9 * This library is free software; you can redistribute it and/or modify |
20613 | 10 * it under the terms of the GNU General Public License as published by |
11 * the Free Software Foundation; either version 2 of the License, or | |
12 * (at your option) any later version. | |
13 * | |
31098
9e9595c779cf
libdvdcss: cosmetics: Fix FSF address and program name in license headers.
diego
parents:
27462
diff
changeset
|
14 * This library is distributed in the hope that it will be useful, |
20613 | 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 * GNU General Public License for more details. | |
18 * | |
31098
9e9595c779cf
libdvdcss: cosmetics: Fix FSF address and program name in license headers.
diego
parents:
27462
diff
changeset
|
19 * You should have received a copy of the GNU General Public License along |
9e9595c779cf
libdvdcss: cosmetics: Fix FSF address and program name in license headers.
diego
parents:
27462
diff
changeset
|
20 * with this library; if not, write to the Free Software Foundation, Inc., |
9e9595c779cf
libdvdcss: cosmetics: Fix FSF address and program name in license headers.
diego
parents:
27462
diff
changeset
|
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
20613 | 22 */ |
23 | |
24 /** | |
25 * \mainpage libdvdcss developer documentation | |
26 * | |
27 * \section intro Introduction | |
28 * | |
29 * \e libdvdcss is a simple library designed for accessing DVDs like a block | |
30 * device without having to bother about the decryption. The important features | |
31 * are: | |
32 * \li portability: currently supported platforms are GNU/Linux, FreeBSD, | |
35952 | 33 * NetBSD, OpenBSD, BeOS, Windows 98/ME, Windows NT/2000/XP, Mac OS X, |
20613 | 34 * Solaris, HP-UX and OS/2. |
35 * \li adaptability: unlike most similar projects, libdvdcss doesn't require | |
36 * the region of your drive to be set and will try its best to read from | |
37 * the disc even in the case of a region mismatch. | |
38 * \li simplicity: a DVD player can be built around the \e libdvdcss API using | |
39 * no more than 4 or 5 library calls. | |
40 * | |
41 * \e libdvdcss is free software, released under the General Public License. | |
42 * This ensures that \e libdvdcss remains free and used only with free | |
43 * software. | |
44 * | |
45 * \section api The libdvdcss API | |
46 * | |
47 * The complete \e libdvdcss programming interface is documented in the | |
48 * dvdcss.h file. | |
49 * | |
50 * \section env Environment variables | |
51 * | |
35952 | 52 * Some environment variables can be used to change the behavior of |
20613 | 53 * \e libdvdcss without having to modify the program which uses it. These |
54 * variables are: | |
55 * | |
56 * \li \b DVDCSS_VERBOSE: sets the verbosity level. | |
57 * - \c 0 outputs no messages at all. | |
58 * - \c 1 outputs error messages to stderr. | |
59 * - \c 2 outputs error messages and debug messages to stderr. | |
60 * | |
61 * \li \b DVDCSS_METHOD: sets the authentication and decryption method | |
62 * that \e libdvdcss will use to read scrambled discs. Can be one | |
63 * of \c title, \c key or \c disc. | |
64 * - \c key is the default method. \e libdvdcss will use a set of | |
65 * calculated player keys to try and get the disc key. This can fail | |
66 * if the drive does not recognize any of the player keys. | |
67 * - \c disc is a fallback method when \c key has failed. Instead of | |
68 * using player keys, \e libdvdcss will crack the disc key using | |
69 * a brute force algorithm. This process is CPU intensive and requires | |
70 * 64 MB of memory to store temporary data. | |
71 * - \c title is the fallback when all other methods have failed. It does | |
72 * not rely on a key exchange with the DVD drive, but rather uses a | |
73 * crypto attack to guess the title key. On rare cases this may fail | |
74 * because there is not enough encrypted data on the disc to perform | |
75 * a statistical attack, but in the other hand it is the only way to | |
76 * decrypt a DVD stored on a hard disc, or a DVD with the wrong region | |
77 * on an RPC2 drive. | |
78 * | |
79 * \li \b DVDCSS_RAW_DEVICE: specify the raw device to use. Exact usage will | |
80 * depend on your operating system, the Linux utility to set up raw devices | |
81 * is \c raw(8) for instance. Please note that on most operating systems, | |
82 * using a raw device requires highly aligned buffers: Linux requires a | |
83 * 2048 bytes alignment (which is the size of a DVD sector). | |
84 * | |
85 * \li \b DVDCSS_CACHE: specify a directory in which to store title key | |
86 * values. This will speed up descrambling of DVDs which are in the | |
87 * cache. The DVDCSS_CACHE directory is created if it does not exist, | |
88 * and a subdirectory is created named after the DVD's title or | |
89 * manufacturing date. If DVDCSS_CACHE is not set or is empty, \e libdvdcss | |
90 * will use the default value which is "${HOME}/.dvdcss/" under Unix and | |
91 * "C:\Documents and Settings\$USER\Application Data\dvdcss\" under Win32. | |
92 * The special value "off" disables caching. | |
93 */ | |
94 | |
95 /* | |
96 * Preamble | |
97 */ | |
98 #include "config.h" | |
99 | |
100 #include <stdio.h> | |
101 #include <stdlib.h> | |
102 #include <string.h> | |
103 #include <sys/types.h> | |
104 #include <sys/stat.h> | |
105 #ifdef HAVE_SYS_PARAM_H | |
106 # include <sys/param.h> | |
107 #endif | |
108 #ifdef HAVE_PWD_H | |
109 # include <pwd.h> | |
110 #endif | |
111 #include <fcntl.h> | |
112 #include <errno.h> | |
113 | |
114 #ifdef HAVE_UNISTD_H | |
115 # include <unistd.h> | |
116 #endif | |
117 | |
118 #ifdef HAVE_LIMITS_H | |
119 # include <limits.h> | |
120 #endif | |
121 | |
35952 | 122 #if defined(_WIN32_IE) && _WIN32_IE >= 0x500 |
123 # include <shlobj.h> | |
20613 | 124 #endif |
125 | |
126 #include "dvdcss/dvdcss.h" | |
127 | |
128 #include "common.h" | |
129 #include "css.h" | |
130 #include "libdvdcss.h" | |
131 #include "ioctl.h" | |
132 #include "device.h" | |
133 | |
35952 | 134 #ifdef HAVE_BROKEN_MKDIR |
135 #include <direct.h> | |
136 #define mkdir(a, b) _mkdir(a) | |
137 #endif | |
20613 | 138 |
139 /** | |
140 * \brief Open a DVD device or directory and return a dvdcss instance. | |
141 * | |
142 * \param psz_target a string containing the target name, for instance | |
143 * "/dev/hdc" or "E:". | |
144 * \return a handle to a dvdcss instance or NULL on error. | |
145 * | |
146 * Initialize the \e libdvdcss library and open the requested DVD device or | |
147 * directory. \e libdvdcss checks whether ioctls can be performed on the disc, | |
148 * and when possible, the disc key is retrieved. | |
149 * | |
150 * dvdcss_open() returns a handle to be used for all subsequent \e libdvdcss | |
151 * calls. If an error occurred, NULL is returned. | |
152 */ | |
153 LIBDVDCSS_EXPORT dvdcss_t dvdcss_open ( char *psz_target ) | |
154 { | |
155 char psz_buffer[PATH_MAX]; | |
156 int i_ret; | |
157 | |
158 char *psz_method = getenv( "DVDCSS_METHOD" ); | |
159 char *psz_verbose = getenv( "DVDCSS_VERBOSE" ); | |
160 char *psz_cache = getenv( "DVDCSS_CACHE" ); | |
35952 | 161 #ifdef DVDCSS_RAW_OPEN |
20613 | 162 char *psz_raw_device = getenv( "DVDCSS_RAW_DEVICE" ); |
163 #endif | |
164 | |
165 dvdcss_t dvdcss; | |
166 | |
167 /* | |
168 * Allocate the library structure | |
169 */ | |
170 dvdcss = malloc( sizeof( struct dvdcss_s ) ); | |
171 if( dvdcss == NULL ) | |
172 { | |
173 return NULL; | |
174 } | |
175 | |
176 /* | |
177 * Initialize structure with default values | |
178 */ | |
35952 | 179 #ifdef DVDCSS_RAW_OPEN |
20613 | 180 dvdcss->i_raw_fd = -1; |
181 #endif | |
182 dvdcss->p_titles = NULL; | |
183 dvdcss->psz_device = (char *)strdup( psz_target ); | |
184 dvdcss->psz_error = "no error"; | |
185 dvdcss->i_method = DVDCSS_METHOD_KEY; | |
186 dvdcss->psz_cachefile[0] = '\0'; | |
187 dvdcss->b_debug = 0; | |
188 dvdcss->b_errors = 0; | |
189 | |
190 /* | |
191 * Find verbosity from DVDCSS_VERBOSE environment variable | |
192 */ | |
193 if( psz_verbose != NULL ) | |
194 { | |
195 int i = atoi( psz_verbose ); | |
196 | |
197 if( i >= 2 ) dvdcss->b_debug = i; | |
198 if( i >= 1 ) dvdcss->b_errors = 1; | |
199 } | |
200 | |
201 /* | |
202 * Find method from DVDCSS_METHOD environment variable | |
203 */ | |
204 if( psz_method != NULL ) | |
205 { | |
206 if( !strncmp( psz_method, "key", 4 ) ) | |
207 { | |
208 dvdcss->i_method = DVDCSS_METHOD_KEY; | |
209 } | |
210 else if( !strncmp( psz_method, "disc", 5 ) ) | |
211 { | |
212 dvdcss->i_method = DVDCSS_METHOD_DISC; | |
213 } | |
214 else if( !strncmp( psz_method, "title", 5 ) ) | |
215 { | |
216 dvdcss->i_method = DVDCSS_METHOD_TITLE; | |
217 } | |
218 else | |
219 { | |
220 print_error( dvdcss, "unknown decrypt method, please choose " | |
221 "from 'title', 'key' or 'disc'" ); | |
222 free( dvdcss->psz_device ); | |
223 free( dvdcss ); | |
224 return NULL; | |
225 } | |
226 } | |
227 | |
228 /* | |
229 * If DVDCSS_CACHE was not set, try to guess a default value | |
230 */ | |
231 if( psz_cache == NULL || psz_cache[0] == '\0' ) | |
232 { | |
35952 | 233 #if defined(_WIN32_IE) && _WIN32_IE >= 0x500 |
20613 | 234 char psz_home[MAX_PATH]; |
235 | |
236 /* Cache our keys in | |
237 * C:\Documents and Settings\$USER\Application Data\dvdcss\ */ | |
35952 | 238 if (SHGetFolderPathA (NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, |
239 NULL, SHGFP_TYPE_CURRENT, psz_home ) == S_OK) | |
20613 | 240 { |
35952 | 241 snprintf( psz_buffer, PATH_MAX, "%s\\dvdcss", psz_home ); |
20613 | 242 psz_buffer[PATH_MAX-1] = '\0'; |
243 psz_cache = psz_buffer; | |
244 } | |
245 #else | |
246 char *psz_home = NULL; | |
247 # ifdef HAVE_PWD_H | |
248 struct passwd *p_pwd; | |
249 | |
250 /* Try looking in password file for home dir. */ | |
251 p_pwd = getpwuid(getuid()); | |
252 if( p_pwd ) | |
253 { | |
254 psz_home = p_pwd->pw_dir; | |
255 } | |
256 # endif | |
257 | |
258 if( psz_home == NULL ) | |
259 { | |
260 psz_home = getenv( "HOME" ); | |
261 } | |
262 if( psz_home == NULL ) | |
263 { | |
264 psz_home = getenv( "USERPROFILE" ); | |
265 } | |
266 | |
267 /* Cache our keys in ${HOME}/.dvdcss/ */ | |
268 if( psz_home ) | |
269 { | |
31099
f43e87f69590
libdvdcss: OS/2 support; this merges upstream revisions 220 and 229.
diego
parents:
31098
diff
changeset
|
270 int home_pos = 0; |
f43e87f69590
libdvdcss: OS/2 support; this merges upstream revisions 220 and 229.
diego
parents:
31098
diff
changeset
|
271 |
35952 | 272 #ifdef __OS2__ |
31099
f43e87f69590
libdvdcss: OS/2 support; this merges upstream revisions 220 and 229.
diego
parents:
31098
diff
changeset
|
273 if( *psz_home == '/' || *psz_home == '\\') |
f43e87f69590
libdvdcss: OS/2 support; this merges upstream revisions 220 and 229.
diego
parents:
31098
diff
changeset
|
274 { |
f43e87f69590
libdvdcss: OS/2 support; this merges upstream revisions 220 and 229.
diego
parents:
31098
diff
changeset
|
275 char *psz_unixroot = getenv("UNIXROOT"); |
f43e87f69590
libdvdcss: OS/2 support; this merges upstream revisions 220 and 229.
diego
parents:
31098
diff
changeset
|
276 |
f43e87f69590
libdvdcss: OS/2 support; this merges upstream revisions 220 and 229.
diego
parents:
31098
diff
changeset
|
277 if( psz_unixroot && |
f43e87f69590
libdvdcss: OS/2 support; this merges upstream revisions 220 and 229.
diego
parents:
31098
diff
changeset
|
278 psz_unixroot[0] && |
f43e87f69590
libdvdcss: OS/2 support; this merges upstream revisions 220 and 229.
diego
parents:
31098
diff
changeset
|
279 psz_unixroot[1] == ':' && |
f43e87f69590
libdvdcss: OS/2 support; this merges upstream revisions 220 and 229.
diego
parents:
31098
diff
changeset
|
280 psz_unixroot[2] == '\0') |
f43e87f69590
libdvdcss: OS/2 support; this merges upstream revisions 220 and 229.
diego
parents:
31098
diff
changeset
|
281 { |
f43e87f69590
libdvdcss: OS/2 support; this merges upstream revisions 220 and 229.
diego
parents:
31098
diff
changeset
|
282 strcpy( psz_buffer, psz_unixroot ); |
f43e87f69590
libdvdcss: OS/2 support; this merges upstream revisions 220 and 229.
diego
parents:
31098
diff
changeset
|
283 home_pos = 2; |
f43e87f69590
libdvdcss: OS/2 support; this merges upstream revisions 220 and 229.
diego
parents:
31098
diff
changeset
|
284 } |
f43e87f69590
libdvdcss: OS/2 support; this merges upstream revisions 220 and 229.
diego
parents:
31098
diff
changeset
|
285 } |
f43e87f69590
libdvdcss: OS/2 support; this merges upstream revisions 220 and 229.
diego
parents:
31098
diff
changeset
|
286 #endif |
f43e87f69590
libdvdcss: OS/2 support; this merges upstream revisions 220 and 229.
diego
parents:
31098
diff
changeset
|
287 snprintf( psz_buffer + home_pos, PATH_MAX - home_pos, |
f43e87f69590
libdvdcss: OS/2 support; this merges upstream revisions 220 and 229.
diego
parents:
31098
diff
changeset
|
288 "%s/.dvdcss", psz_home ); |
20613 | 289 psz_buffer[PATH_MAX-1] = '\0'; |
290 psz_cache = psz_buffer; | |
291 } | |
292 #endif | |
293 } | |
294 | |
295 /* | |
296 * Find cache dir from the DVDCSS_CACHE environment variable | |
297 */ | |
298 if( psz_cache != NULL ) | |
299 { | |
300 if( psz_cache[0] == '\0' || !strcmp( psz_cache, "off" ) ) | |
301 { | |
302 psz_cache = NULL; | |
303 } | |
304 /* Check that we can add the ID directory and the block filename */ | |
305 else if( strlen( psz_cache ) + 1 + 32 + 1 + (KEY_SIZE * 2) + 10 + 1 | |
306 > PATH_MAX ) | |
307 { | |
308 print_error( dvdcss, "cache directory name is too long" ); | |
309 psz_cache = NULL; | |
310 } | |
311 } | |
312 | |
313 /* | |
314 * Open device | |
315 */ | |
316 _dvdcss_check( dvdcss ); | |
317 i_ret = _dvdcss_open( dvdcss ); | |
318 if( i_ret < 0 ) | |
319 { | |
320 free( dvdcss->psz_device ); | |
321 free( dvdcss ); | |
322 return NULL; | |
323 } | |
324 | |
325 dvdcss->b_scrambled = 1; /* Assume the worst */ | |
326 dvdcss->b_ioctls = _dvdcss_use_ioctls( dvdcss ); | |
327 | |
328 if( dvdcss->b_ioctls ) | |
329 { | |
35952 | 330 i_ret = _dvdcss_test( dvdcss ); |
331 | |
332 if( i_ret == -3 ) | |
333 { | |
334 print_debug( dvdcss, "scrambled disc on a region-free RPC-II " | |
335 "drive: possible failure, but continuing " | |
336 "anyway" ); | |
337 } | |
338 else if( i_ret < 0 ) | |
339 { | |
340 /* Disable the CSS ioctls and hope that it works? */ | |
341 print_debug( dvdcss, | |
342 "could not check whether the disc was scrambled" ); | |
343 dvdcss->b_ioctls = 0; | |
344 } | |
345 else | |
346 { | |
347 print_debug( dvdcss, i_ret ? "disc is scrambled" | |
348 : "disc is unscrambled" ); | |
349 dvdcss->b_scrambled = i_ret; | |
350 } | |
20613 | 351 } |
352 | |
35952 | 353 memset( dvdcss->css.p_disc_key, 0, KEY_SIZE ); |
20613 | 354 /* If disc is CSS protected and the ioctls work, authenticate the drive */ |
355 if( dvdcss->b_scrambled && dvdcss->b_ioctls ) | |
356 { | |
357 i_ret = _dvdcss_disckey( dvdcss ); | |
358 | |
359 if( i_ret < 0 ) | |
360 { | |
27462 | 361 print_debug( dvdcss, "could not get disc key" ); |
20613 | 362 } |
363 } | |
364 | |
365 /* If the cache is enabled, write the cache directory tag */ | |
366 if( psz_cache ) | |
367 { | |
35952 | 368 static const char psz_tag[] = |
369 "Signature: 8a477f597d28d172789f06886806bc55\r\n" | |
20613 | 370 "# This file is a cache directory tag created by libdvdcss.\r\n" |
371 "# For information about cache directory tags, see:\r\n" | |
372 "# http://www.brynosaurus.com/cachedir/\r\n"; | |
373 char psz_tagfile[PATH_MAX + 1 + 12 + 1]; | |
374 int i_fd; | |
375 | |
376 sprintf( psz_tagfile, "%s/CACHEDIR.TAG", psz_cache ); | |
377 i_fd = open( psz_tagfile, O_RDWR|O_CREAT, 0644 ); | |
378 if( i_fd >= 0 ) | |
379 { | |
35952 | 380 size_t len = strlen(psz_tag); |
381 if( write( i_fd, psz_tag, len ) < (long)len ) | |
382 { | |
383 print_error( dvdcss, | |
384 "Error writing cache directory tag, continuing..\n" ); | |
385 } | |
20613 | 386 close( i_fd ); |
387 } | |
388 } | |
389 | |
390 /* If the cache is enabled, extract a unique disc ID */ | |
391 if( psz_cache ) | |
392 { | |
393 uint8_t p_sector[DVDCSS_BLOCK_SIZE]; | |
394 char psz_key[1 + KEY_SIZE * 2 + 1]; | |
23708 | 395 char *psz_title; |
396 uint8_t *psz_serial; | |
20613 | 397 int i; |
398 | |
399 /* We read sector 0. If it starts with 0x000001ba (BE), we are | |
400 * reading a VOB file, and we should not cache anything. */ | |
401 | |
402 i_ret = dvdcss->pf_seek( dvdcss, 0 ); | |
403 if( i_ret != 0 ) | |
404 { | |
405 goto nocache; | |
406 } | |
407 | |
408 i_ret = dvdcss->pf_read( dvdcss, p_sector, 1 ); | |
409 if( i_ret != 1 ) | |
410 { | |
411 goto nocache; | |
412 } | |
413 | |
414 if( p_sector[0] == 0x00 && p_sector[1] == 0x00 | |
415 && p_sector[2] == 0x01 && p_sector[3] == 0xba ) | |
416 { | |
417 goto nocache; | |
418 } | |
419 | |
420 /* The data we are looking for is at sector 16 (32768 bytes): | |
421 * - offset 40: disc title (32 uppercase chars) | |
422 * - offset 813: manufacturing date + serial no (16 digits) */ | |
423 | |
424 i_ret = dvdcss->pf_seek( dvdcss, 16 ); | |
425 if( i_ret != 16 ) | |
426 { | |
427 goto nocache; | |
428 } | |
429 | |
430 i_ret = dvdcss->pf_read( dvdcss, p_sector, 1 ); | |
431 if( i_ret != 1 ) | |
432 { | |
433 goto nocache; | |
434 } | |
435 | |
436 /* Get the disc title */ | |
437 psz_title = (char *)p_sector + 40; | |
438 psz_title[32] = '\0'; | |
439 | |
440 for( i = 0 ; i < 32 ; i++ ) | |
441 { | |
442 if( psz_title[i] <= ' ' ) | |
443 { | |
444 psz_title[i] = '\0'; | |
445 break; | |
446 } | |
447 else if( psz_title[i] == '/' || psz_title[i] == '\\' ) | |
448 { | |
449 psz_title[i] = '-'; | |
450 } | |
451 } | |
452 | |
453 /* Get the date + serial */ | |
23708 | 454 psz_serial = p_sector + 813; |
20613 | 455 psz_serial[16] = '\0'; |
456 | |
457 /* Check that all characters are digits, otherwise convert. */ | |
458 for( i = 0 ; i < 16 ; i++ ) | |
459 { | |
460 if( psz_serial[i] < '0' || psz_serial[i] > '9' ) | |
461 { | |
462 char psz_tmp[16 + 1]; | |
463 sprintf( psz_tmp, | |
27462 | 464 "%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x", |
20613 | 465 psz_serial[0], psz_serial[1], psz_serial[2], |
466 psz_serial[3], psz_serial[4], psz_serial[5], | |
467 psz_serial[6], psz_serial[7] ); | |
468 memcpy( psz_serial, psz_tmp, 16 ); | |
469 break; | |
470 } | |
471 } | |
472 | |
473 /* Get disk key, since some discs have got same title, manufacturing | |
474 * date and serial number, but different keys */ | |
475 if( dvdcss->b_scrambled ) | |
476 { | |
477 psz_key[0] = '-'; | |
478 for( i = 0; i < KEY_SIZE; i++ ) | |
479 { | |
27462 | 480 sprintf( &psz_key[1+i*2], "%.2x", dvdcss->css.p_disc_key[i] ); |
20613 | 481 } |
482 psz_key[1 + KEY_SIZE * 2] = '\0'; | |
483 } | |
484 else | |
485 { | |
486 psz_key[0] = 0; | |
487 } | |
488 | |
489 /* We have a disc name or ID, we can create the cache dir */ | |
490 i = sprintf( dvdcss->psz_cachefile, "%s", psz_cache ); | |
491 i_ret = mkdir( dvdcss->psz_cachefile, 0755 ); | |
492 if( i_ret < 0 && errno != EEXIST ) | |
493 { | |
494 print_error( dvdcss, "failed creating cache directory" ); | |
495 dvdcss->psz_cachefile[0] = '\0'; | |
496 goto nocache; | |
497 } | |
498 | |
499 i += sprintf( dvdcss->psz_cachefile + i, "/%s-%s%s", psz_title, | |
500 psz_serial, psz_key ); | |
501 i_ret = mkdir( dvdcss->psz_cachefile, 0755 ); | |
502 if( i_ret < 0 && errno != EEXIST ) | |
503 { | |
504 print_error( dvdcss, "failed creating cache subdirectory" ); | |
505 dvdcss->psz_cachefile[0] = '\0'; | |
506 goto nocache; | |
507 } | |
508 i += sprintf( dvdcss->psz_cachefile + i, "/"); | |
509 | |
510 /* Pointer to the filename we will use. */ | |
511 dvdcss->psz_block = dvdcss->psz_cachefile + i; | |
512 | |
31100
45159a4b8815
libdvdcss: Fix potential format string crash; check RPC status on disc access.
diego
parents:
31099
diff
changeset
|
513 print_debug( dvdcss, "using CSS key cache dir: %s", |
45159a4b8815
libdvdcss: Fix potential format string crash; check RPC status on disc access.
diego
parents:
31099
diff
changeset
|
514 dvdcss->psz_cachefile ); |
20613 | 515 } |
516 nocache: | |
517 | |
35952 | 518 #ifdef DVDCSS_RAW_OPEN |
20613 | 519 if( psz_raw_device != NULL ) |
520 { | |
521 _dvdcss_raw_open( dvdcss, psz_raw_device ); | |
522 } | |
523 #endif | |
524 | |
525 /* Seek at the beginning, just for safety. */ | |
526 dvdcss->pf_seek( dvdcss, 0 ); | |
527 | |
528 return dvdcss; | |
529 } | |
530 | |
531 /** | |
532 * \brief Return a string containing the latest error that occurred in the | |
533 * given \e libdvdcss instance. | |
534 * | |
535 * \param dvdcss a \e libdvdcss instance. | |
536 * \return a null-terminated string containing the latest error message. | |
537 * | |
538 * This function returns a constant string containing the latest error that | |
539 * occurred in \e libdvdcss. It can be used to format error messages at your | |
540 * convenience in your application. | |
541 */ | |
542 LIBDVDCSS_EXPORT char * dvdcss_error ( dvdcss_t dvdcss ) | |
543 { | |
544 return dvdcss->psz_error; | |
545 } | |
546 | |
547 /** | |
548 * \brief Seek in the disc and change the current key if requested. | |
549 * | |
550 * \param dvdcss a \e libdvdcss instance. | |
551 * \param i_blocks an absolute block offset to seek to. | |
35952 | 552 * \param i_flags #DVDCSS_NOFLAGS, optionally ORed with one of #DVDCSS_SEEK_KEY |
20613 | 553 * or #DVDCSS_SEEK_MPEG. |
554 * \return the new position in blocks, or a negative value in case an error | |
555 * happened. | |
556 * | |
557 * This function seeks to the requested position, in logical blocks. | |
558 * | |
559 * You typically set \p i_flags to #DVDCSS_NOFLAGS when seeking in a .IFO. | |
560 * | |
561 * If #DVDCSS_SEEK_MPEG is specified in \p i_flags and if \e libdvdcss finds it | |
35952 | 562 * reasonable to do so (i.e., if the dvdcss method is not "title"), the current |
20613 | 563 * title key will be checked and a new one will be calculated if necessary. |
564 * This flag is typically used when reading data from a VOB. | |
565 * | |
566 * If #DVDCSS_SEEK_KEY is specified, the title key will be always checked, | |
35952 | 567 * even with the "title" method. This flag is typically used when seeking |
20613 | 568 * in a new title. |
569 */ | |
570 LIBDVDCSS_EXPORT int dvdcss_seek ( dvdcss_t dvdcss, int i_blocks, int i_flags ) | |
571 { | |
572 /* title cracking method is too slow to be used at each seek */ | |
573 if( ( ( i_flags & DVDCSS_SEEK_MPEG ) | |
574 && ( dvdcss->i_method != DVDCSS_METHOD_TITLE ) ) | |
575 || ( i_flags & DVDCSS_SEEK_KEY ) ) | |
576 { | |
577 /* check the title key */ | |
578 if( _dvdcss_title( dvdcss, i_blocks ) ) | |
579 { | |
580 return -1; | |
581 } | |
582 } | |
583 | |
584 return dvdcss->pf_seek( dvdcss, i_blocks ); | |
585 } | |
586 | |
587 /** | |
588 * \brief Read from the disc and decrypt data if requested. | |
589 * | |
590 * \param dvdcss a \e libdvdcss instance. | |
591 * \param p_buffer a buffer that will contain the data read from the disc. | |
592 * \param i_blocks the amount of blocks to read. | |
35952 | 593 * \param i_flags #DVDCSS_NOFLAGS, optionally ORed with #DVDCSS_READ_DECRYPT. |
20613 | 594 * \return the amount of blocks read, or a negative value in case an |
595 * error happened. | |
596 * | |
597 * This function reads \p i_blocks logical blocks from the DVD. | |
598 * | |
599 * You typically set \p i_flags to #DVDCSS_NOFLAGS when reading data from a | |
600 * .IFO file on the DVD. | |
601 * | |
602 * If #DVDCSS_READ_DECRYPT is specified in \p i_flags, dvdcss_read() will | |
603 * automatically decrypt scrambled sectors. This flag is typically used when | |
604 * reading data from a .VOB file on the DVD. It has no effect on unscrambled | |
605 * discs or unscrambled sectors, and can be safely used on those. | |
606 * | |
607 * \warning dvdcss_read() expects to be able to write \p i_blocks * | |
608 * #DVDCSS_BLOCK_SIZE bytes in \p p_buffer. | |
609 */ | |
610 LIBDVDCSS_EXPORT int dvdcss_read ( dvdcss_t dvdcss, void *p_buffer, | |
611 int i_blocks, | |
612 int i_flags ) | |
613 { | |
614 int i_ret, i_index; | |
615 | |
616 i_ret = dvdcss->pf_read( dvdcss, p_buffer, i_blocks ); | |
617 | |
618 if( i_ret <= 0 | |
619 || !dvdcss->b_scrambled | |
620 || !(i_flags & DVDCSS_READ_DECRYPT) ) | |
621 { | |
622 return i_ret; | |
623 } | |
624 | |
625 if( ! memcmp( dvdcss->css.p_title_key, "\0\0\0\0\0", 5 ) ) | |
626 { | |
627 /* For what we believe is an unencrypted title, | |
628 * check that there are no encrypted blocks */ | |
629 for( i_index = i_ret; i_index; i_index-- ) | |
630 { | |
631 if( ((uint8_t*)p_buffer)[0x14] & 0x30 ) | |
632 { | |
633 print_error( dvdcss, "no key but found encrypted block" ); | |
634 /* Only return the initial range of unscrambled blocks? */ | |
635 /* or fail completely? return 0; */ | |
636 break; | |
637 } | |
638 p_buffer = (void *) ((uint8_t *)p_buffer + DVDCSS_BLOCK_SIZE); | |
639 } | |
640 } | |
641 else | |
642 { | |
643 /* Decrypt the blocks we managed to read */ | |
644 for( i_index = i_ret; i_index; i_index-- ) | |
645 { | |
646 _dvdcss_unscramble( dvdcss->css.p_title_key, p_buffer ); | |
647 ((uint8_t*)p_buffer)[0x14] &= 0x8f; | |
648 p_buffer = (void *) ((uint8_t *)p_buffer + DVDCSS_BLOCK_SIZE); | |
649 } | |
650 } | |
651 | |
652 return i_ret; | |
653 } | |
654 | |
655 /** | |
656 * \brief Read from the disc into multiple buffers and decrypt data if | |
657 * requested. | |
658 * | |
659 * \param dvdcss a \e libdvdcss instance. | |
660 * \param p_iovec a pointer to an array of iovec structures that will contain | |
661 * the data read from the disc. | |
662 * \param i_blocks the amount of blocks to read. | |
35952 | 663 * \param i_flags #DVDCSS_NOFLAGS, optionally ORed with #DVDCSS_READ_DECRYPT. |
20613 | 664 * \return the amount of blocks read, or a negative value in case an |
665 * error happened. | |
666 * | |
667 * This function reads \p i_blocks logical blocks from the DVD and writes them | |
668 * to an array of iovec structures. | |
669 * | |
670 * You typically set \p i_flags to #DVDCSS_NOFLAGS when reading data from a | |
671 * .IFO file on the DVD. | |
672 * | |
673 * If #DVDCSS_READ_DECRYPT is specified in \p i_flags, dvdcss_readv() will | |
674 * automatically decrypt scrambled sectors. This flag is typically used when | |
675 * reading data from a .VOB file on the DVD. It has no effect on unscrambled | |
676 * discs or unscrambled sectors, and can be safely used on those. | |
677 * | |
678 * \warning dvdcss_readv() expects to be able to write \p i_blocks * | |
679 * #DVDCSS_BLOCK_SIZE bytes in the buffers pointed by \p p_iovec. | |
680 * Moreover, all iov_len members of the iovec structures should be | |
681 * multiples of #DVDCSS_BLOCK_SIZE. | |
682 */ | |
683 LIBDVDCSS_EXPORT int dvdcss_readv ( dvdcss_t dvdcss, void *p_iovec, | |
684 int i_blocks, | |
685 int i_flags ) | |
686 { | |
687 struct iovec *_p_iovec = (struct iovec *)p_iovec; | |
688 int i_ret, i_index; | |
689 void *iov_base; | |
690 size_t iov_len; | |
691 | |
692 i_ret = dvdcss->pf_readv( dvdcss, _p_iovec, i_blocks ); | |
693 | |
694 if( i_ret <= 0 | |
695 || !dvdcss->b_scrambled | |
696 || !(i_flags & DVDCSS_READ_DECRYPT) ) | |
697 { | |
698 return i_ret; | |
699 } | |
700 | |
701 /* Initialize loop for decryption */ | |
702 iov_base = _p_iovec->iov_base; | |
703 iov_len = _p_iovec->iov_len; | |
704 | |
705 /* Decrypt the blocks we managed to read */ | |
706 for( i_index = i_ret; i_index; i_index-- ) | |
707 { | |
708 /* Check that iov_len is a multiple of 2048 */ | |
709 if( iov_len & 0x7ff ) | |
710 { | |
711 return -1; | |
712 } | |
713 | |
714 while( iov_len == 0 ) | |
715 { | |
716 _p_iovec++; | |
717 iov_base = _p_iovec->iov_base; | |
718 iov_len = _p_iovec->iov_len; | |
719 } | |
720 | |
721 _dvdcss_unscramble( dvdcss->css.p_title_key, iov_base ); | |
722 ((uint8_t*)iov_base)[0x14] &= 0x8f; | |
723 | |
724 iov_base = (void *) ((uint8_t*)iov_base + DVDCSS_BLOCK_SIZE); | |
725 iov_len -= DVDCSS_BLOCK_SIZE; | |
726 } | |
727 | |
728 return i_ret; | |
729 } | |
730 | |
731 /** | |
732 * \brief Close the DVD and clean up the library. | |
733 * | |
734 * \param dvdcss a \e libdvdcss instance. | |
735 * \return zero in case of success, a negative value otherwise. | |
736 * | |
737 * This function closes the DVD device and frees all the memory allocated | |
738 * by \e libdvdcss. On return, the #dvdcss_t is invalidated and may not be | |
739 * used again. | |
740 */ | |
741 LIBDVDCSS_EXPORT int dvdcss_close ( dvdcss_t dvdcss ) | |
742 { | |
743 dvd_title_t *p_title; | |
744 int i_ret; | |
745 | |
746 /* Free our list of keys */ | |
747 p_title = dvdcss->p_titles; | |
748 while( p_title ) | |
749 { | |
750 dvd_title_t *p_tmptitle = p_title->p_next; | |
751 free( p_title ); | |
752 p_title = p_tmptitle; | |
753 } | |
754 | |
755 i_ret = _dvdcss_close( dvdcss ); | |
756 | |
757 if( i_ret < 0 ) | |
758 { | |
759 return i_ret; | |
760 } | |
761 | |
762 free( dvdcss->psz_device ); | |
763 free( dvdcss ); | |
764 | |
765 return 0; | |
766 } | |
767 | |
27442 | 768 /** |
769 * \brief Return 1 if the DVD is scrambled, 0 otherwise. | |
770 * | |
771 * \param dvdcss a \e libdvdcss instance. | |
772 * \return 1 if the DVD is scrambled, 0 otherwise. | |
773 * | |
774 * This function returns whether the DVD is scrambled. | |
775 */ | |
776 LIBDVDCSS_EXPORT int dvdcss_is_scrambled ( dvdcss_t dvdcss ) | |
777 { | |
778 return dvdcss->b_scrambled; | |
779 } | |
27462 | 780 |