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