Mercurial > mplayer.hg
annotate libmpdvdkit2/libdvdcss.c @ 8394:cf1ac6d3d4f2
sync
author | zdar |
---|---|
date | Sat, 07 Dec 2002 20:08:51 +0000 |
parents | 9fc45fe0d444 |
children | 0211de3039eb |
rev | line source |
---|---|
7027 | 1 /* libdvdcss.c: DVD reading library. |
2 * | |
3 * Authors: Stéphane Borel <stef@via.ecp.fr> | |
4 * Samuel Hocevar <sam@zoy.org> | |
5 * Håkan Hjort <d95hjort@dtek.chalmers.se> | |
6 * | |
7 * Copyright (C) 1998-2002 VideoLAN | |
8 * $Id$ | |
9 * | |
10 * This program is free software; you can redistribute it and/or modify | |
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 * | |
15 * This program is distributed in the hope that it will be useful, | |
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 * | |
20 * You should have received a copy of the GNU General Public License | |
21 * along with this program; if not, write to the Free Software | |
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. | |
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. | |
81 * | |
82 */ | |
83 | |
84 /* | |
85 * Preamble | |
86 */ | |
87 #include "config.h" | |
88 | |
89 #include <stdio.h> | |
90 #include <stdlib.h> | |
91 #include <string.h> | |
92 #include <sys/types.h> | |
93 #include <sys/stat.h> | |
94 #include <fcntl.h> | |
95 | |
96 #ifdef HAVE_UNISTD_H | |
97 # include <unistd.h> | |
98 #endif | |
99 | |
7033 | 100 #include "dvdcss.h" |
7027 | 101 |
102 #include "common.h" | |
103 #include "css.h" | |
104 #include "libdvdcss.h" | |
105 #include "ioctl.h" | |
106 #include "device.h" | |
107 | |
108 /** | |
109 * \brief Symbol for version checks. | |
110 * | |
111 * The name of this symbol contains the library major number, which makes it | |
112 * easy to check which \e libdvdcss development headers are installed on the | |
113 * system with tools such as autoconf. | |
114 * | |
115 * The variable itself contains the exact version number of the library, | |
116 * which can be useful for specific feature needs. | |
117 */ | |
118 char * dvdcss_interface_2 = VERSION; | |
119 | |
7032 | 120 char * dvdcss_cache_dir = NULL; |
121 | |
7027 | 122 /** |
123 * \brief Open a DVD device or directory and return a dvdcss instance. | |
124 * | |
125 * \param psz_target a string containing the target name, for instance | |
126 * "/dev/hdc" or "E:". | |
127 * \return a handle to a dvdcss instance or NULL on error. | |
128 * | |
129 * Initialize the \e libdvdcss library and open the requested DVD device or | |
130 * directory. \e libdvdcss checks whether ioctls can be performed on the disc, | |
131 * and when possible, the disc key is retrieved. | |
132 * | |
133 * dvdcss_open() returns a handle to be used for all subsequent \e libdvdcss | |
134 * calls. If an error occured, NULL is returned. | |
135 */ | |
136 extern dvdcss_t dvdcss_open ( char *psz_target ) | |
137 { | |
138 int i_ret; | |
139 | |
140 char *psz_method = getenv( "DVDCSS_METHOD" ); | |
141 char *psz_verbose = getenv( "DVDCSS_VERBOSE" ); | |
142 #ifndef WIN32 | |
143 char *psz_raw_device = getenv( "DVDCSS_RAW_DEVICE" ); | |
144 #endif | |
145 | |
146 dvdcss_t dvdcss; | |
147 | |
148 /* | |
149 * Allocate the library structure | |
150 */ | |
151 dvdcss = malloc( sizeof( struct dvdcss_s ) ); | |
152 if( dvdcss == NULL ) | |
153 { | |
154 return NULL; | |
155 } | |
156 | |
157 /* | |
158 * Initialize structure with default values | |
159 */ | |
160 #ifndef WIN32 | |
161 dvdcss->i_raw_fd = -1; | |
162 #endif | |
163 dvdcss->p_titles = NULL; | |
164 dvdcss->psz_device = (char *)strdup( psz_target ); | |
165 dvdcss->psz_error = "no error"; | |
166 dvdcss->i_method = DVDCSS_METHOD_KEY; | |
167 dvdcss->b_debug = 0; | |
168 dvdcss->b_errors = 0; | |
7032 | 169 dvdcss->psz_cache = NULL; |
7027 | 170 |
171 /* | |
172 * Find verbosity from DVDCSS_VERBOSE environment variable | |
173 */ | |
174 if( psz_verbose != NULL ) | |
175 { | |
176 switch( atoi( psz_verbose ) ) | |
177 { | |
178 case 2: | |
179 dvdcss->b_debug = 1; | |
180 case 1: | |
181 dvdcss->b_errors = 1; | |
182 case 0: | |
183 break; | |
184 } | |
185 } | |
186 | |
187 /* | |
188 * Find method from DVDCSS_METHOD environment variable | |
189 */ | |
190 if( psz_method != NULL ) | |
191 { | |
192 if( !strncmp( psz_method, "key", 4 ) ) | |
193 { | |
194 dvdcss->i_method = DVDCSS_METHOD_KEY; | |
195 } | |
196 else if( !strncmp( psz_method, "disc", 5 ) ) | |
197 { | |
198 dvdcss->i_method = DVDCSS_METHOD_DISC; | |
199 } | |
200 else if( !strncmp( psz_method, "title", 5 ) ) | |
201 { | |
202 dvdcss->i_method = DVDCSS_METHOD_TITLE; | |
203 } | |
204 else | |
205 { | |
206 _dvdcss_error( dvdcss, "unknown decrypt method, please choose " | |
207 "from 'title', 'key' or 'disc'" ); | |
208 free( dvdcss->psz_device ); | |
209 free( dvdcss ); | |
210 return NULL; | |
211 } | |
212 } | |
213 | |
7032 | 214 if(!dvdcss_cache_dir) dvdcss_cache_dir = getenv( "DVDCSS_CACHE" ); |
215 | |
7027 | 216 /* |
217 * Open device | |
218 */ | |
219 i_ret = _dvdcss_open( dvdcss ); | |
220 if( i_ret < 0 ) | |
221 { | |
222 free( dvdcss->psz_device ); | |
223 free( dvdcss ); | |
224 return NULL; | |
225 } | |
226 | |
227 dvdcss->b_scrambled = 1; /* Assume the worst */ | |
228 dvdcss->b_ioctls = _dvdcss_use_ioctls( dvdcss ); | |
229 | |
230 if( dvdcss->b_ioctls ) | |
231 { | |
232 i_ret = _dvdcss_test( dvdcss ); | |
233 if( i_ret < 0 ) | |
234 { | |
235 /* Disable the CSS ioctls and hope that it works? */ | |
236 _dvdcss_debug( dvdcss, | |
237 "could not check whether the disc was scrambled" ); | |
238 dvdcss->b_ioctls = 0; | |
239 } | |
240 else | |
241 { | |
242 _dvdcss_debug( dvdcss, i_ret ? "disc is scrambled" | |
243 : "disc is unscrambled" ); | |
244 dvdcss->b_scrambled = i_ret; | |
245 } | |
246 } | |
247 | |
248 /* If disc is CSS protected and the ioctls work, authenticate the drive */ | |
249 if( dvdcss->b_scrambled && dvdcss->b_ioctls ) | |
250 { | |
251 i_ret = _dvdcss_disckey( dvdcss ); | |
252 | |
253 if( i_ret < 0 ) | |
254 { | |
255 _dvdcss_close( dvdcss ); | |
256 free( dvdcss->psz_device ); | |
257 free( dvdcss ); | |
258 return NULL; | |
259 } | |
260 } | |
261 | |
262 #ifndef WIN32 | |
263 if( psz_raw_device != NULL ) | |
264 { | |
265 _dvdcss_raw_open( dvdcss, psz_raw_device ); | |
266 } | |
267 #endif | |
268 | |
7032 | 269 /* if the CACHE is enabled, extract some unique disc ID */ |
270 if(dvdcss_cache_dir){ | |
271 char* disc_id=NULL; | |
8123
9fc45fe0d444
*HUGE* set of compiler warning fixes, unused variables removal
arpi
parents:
7033
diff
changeset
|
272 /*char title_name[64];*/ |
7032 | 273 char sector[DVDCSS_BLOCK_SIZE]; |
274 // 32768+40 -> disc title (32 uppercase chars) | |
275 // 32768+813 -> disc manufacturing date + serial no (16 digit number) | |
276 _dvdcss_seek( dvdcss, 32768/DVDCSS_BLOCK_SIZE); | |
277 if(_dvdcss_read( dvdcss, sector, 1) == 1){ | |
278 // check disc title first: | |
279 char* title_name=§or[40]; | |
280 int i=31; | |
281 while(i>=0 && title_name[i]<=32) i--; | |
282 title_name[i+1]=0; | |
283 if(strlen(title_name)>5){ | |
284 disc_id=strdup(title_name); | |
285 } else { | |
286 // use disc date+serial: | |
287 title_name=§or[813]; | |
288 title_name[16]=0; | |
289 for ( i=0;i<16;i++ ) | |
290 if ( ( title_name[i] < '0' )||( title_name[i] > '9' ) ){ | |
291 disc_id=malloc(16+4); | |
8123
9fc45fe0d444
*HUGE* set of compiler warning fixes, unused variables removal
arpi
parents:
7033
diff
changeset
|
292 sprintf( disc_id,"%.2X%.2X%.2X%.2X%.2X%.2X%.2X%.2X",title_name[0],title_name[1],title_name[2],title_name[3],title_name[4],title_name[5],title_name[6],title_name[7] ); |
7032 | 293 break; |
294 } | |
295 if(!disc_id) disc_id=strdup(title_name); | |
296 } | |
297 if(disc_id){ | |
298 // yeah, we have a disc name/id, let's set up cache path: | |
8123
9fc45fe0d444
*HUGE* set of compiler warning fixes, unused variables removal
arpi
parents:
7033
diff
changeset
|
299 /*char* dir;*/ |
7032 | 300 dvdcss->psz_cache = malloc(strlen(dvdcss_cache_dir)+strlen(disc_id)+4); |
301 sprintf(dvdcss->psz_cache,"%s/%s",dvdcss_cache_dir,disc_id); | |
302 mkdir( dvdcss->psz_cache,493 ); | |
303 free(disc_id); | |
304 fprintf(stderr,"Using CSS Key-cache dir: %s\n",dvdcss->psz_cache); | |
305 } | |
306 } | |
307 } | |
308 | |
7027 | 309 return dvdcss; |
310 } | |
311 | |
312 /** | |
313 * \brief Return a string containing the latest error that occured in the | |
314 * given \e libdvdcss instance. | |
315 * | |
316 * \param dvdcss a \e libdvdcss instance. | |
317 * \return a null-terminated string containing the latest error message. | |
318 * | |
319 * This function returns a constant string containing the latest error that | |
320 * occured in \e libdvdcss. It can be used to format error messages at your | |
321 * convenience in your application. | |
322 */ | |
323 extern char * dvdcss_error ( dvdcss_t dvdcss ) | |
324 { | |
325 return dvdcss->psz_error; | |
326 } | |
327 | |
328 /** | |
329 * \brief Seek in the disc and change the current key if requested. | |
330 * | |
331 * \param dvdcss a \e libdvdcss instance. | |
332 * \param i_blocks an absolute block offset to seek to. | |
333 * \param i_flags #DVDCSS_NOFLAGS, optionally ored with one of #DVDCSS_SEEK_KEY | |
334 * or #DVDCSS_SEEK_MPEG. | |
335 * \return the new position in blocks, or a negative value in case an error | |
336 * happened. | |
337 * | |
338 * This function seeks to the requested position, in logical blocks. | |
339 * | |
340 * You typically set \p i_flags to #DVDCSS_NOFLAGS when seeking in a .IFO. | |
341 * | |
342 * If #DVDCSS_SEEK_MPEG is specified in \p i_flags and if \e libdvdcss finds it | |
343 * reasonable to do so (ie, if the dvdcss method is not "title"), the current | |
344 * title key will be checked and a new one will be calculated if necessary. | |
345 * This flag is typically used when reading data from a VOB. | |
346 * | |
347 * If #DVDCSS_SEEK_KEY is specified, the title key will be always checked, | |
348 * even with the "title" method. This is equivalent to using the now | |
349 * deprecated dvdcss_title() call. This flag is typically used when seeking | |
350 * in a new title. | |
351 */ | |
352 extern int dvdcss_seek ( dvdcss_t dvdcss, int i_blocks, int i_flags ) | |
353 { | |
354 /* title cracking method is too slow to be used at each seek */ | |
355 if( ( ( i_flags & DVDCSS_SEEK_MPEG ) | |
356 && ( dvdcss->i_method != DVDCSS_METHOD_TITLE ) ) | |
357 || ( i_flags & DVDCSS_SEEK_KEY ) ) | |
358 { | |
359 /* check the title key */ | |
360 if( _dvdcss_title( dvdcss, i_blocks ) ) | |
361 { | |
362 return -1; | |
363 } | |
364 } | |
365 | |
366 return _dvdcss_seek( dvdcss, i_blocks ); | |
367 } | |
368 | |
369 /** | |
370 * \brief Read from the disc and decrypt data if requested. | |
371 * | |
372 * \param dvdcss a \e libdvdcss instance. | |
373 * \param p_buffer a buffer that will contain the data read from the disc. | |
374 * \param i_blocks the amount of blocks to read. | |
375 * \param i_flags #DVDCSS_NOFLAGS, optionally ored with #DVDCSS_READ_DECRYPT. | |
376 * \return the amount of blocks read, or a negative value in case an | |
377 * error happened. | |
378 * | |
379 * This function reads \p i_blocks logical blocks from the DVD. | |
380 * | |
381 * You typically set \p i_flags to #DVDCSS_NOFLAGS when reading data from a | |
382 * .IFO file on the DVD. | |
383 * | |
384 * If #DVDCSS_READ_DECRYPT is specified in \p i_flags, dvdcss_read() will | |
385 * automatically decrypt scrambled sectors. This flag is typically used when | |
386 * reading data from a .VOB file on the DVD. It has no effect on unscrambled | |
387 * discs or unscrambled sectors, and can be safely used on those. | |
388 * | |
389 * \warning dvdcss_read() expects to be able to write \p i_blocks * | |
390 * #DVDCSS_BLOCK_SIZE bytes in \p p_buffer. | |
391 */ | |
392 extern int dvdcss_read ( dvdcss_t dvdcss, void *p_buffer, | |
393 int i_blocks, | |
394 int i_flags ) | |
395 { | |
396 int i_ret, i_index; | |
397 | |
398 i_ret = _dvdcss_read( dvdcss, p_buffer, i_blocks ); | |
399 | |
400 if( i_ret <= 0 | |
401 || !dvdcss->b_scrambled | |
402 || !(i_flags & DVDCSS_READ_DECRYPT) ) | |
403 { | |
404 return i_ret; | |
405 } | |
406 | |
407 if( ! memcmp( dvdcss->css.p_title_key, "\0\0\0\0\0", 5 ) ) | |
408 { | |
409 /* For what we believe is an unencrypted title, | |
410 * check that there are no encrypted blocks */ | |
411 for( i_index = i_ret; i_index; i_index-- ) | |
412 { | |
413 if( ((u8*)p_buffer)[0x14] & 0x30 ) | |
414 { | |
415 _dvdcss_error( dvdcss, "no key but found encrypted block" ); | |
416 /* Only return the initial range of unscrambled blocks? */ | |
417 /* or fail completely? return 0; */ | |
418 break; | |
419 } | |
420 p_buffer = (void *) ((u8 *)p_buffer + DVDCSS_BLOCK_SIZE); | |
421 } | |
422 } | |
423 else | |
424 { | |
425 /* Decrypt the blocks we managed to read */ | |
426 for( i_index = i_ret; i_index; i_index-- ) | |
427 { | |
428 _dvdcss_unscramble( dvdcss->css.p_title_key, p_buffer ); | |
429 ((u8*)p_buffer)[0x14] &= 0x8f; | |
430 p_buffer = (void *) ((u8 *)p_buffer + DVDCSS_BLOCK_SIZE); | |
431 } | |
432 } | |
433 | |
434 return i_ret; | |
435 } | |
436 | |
437 /** | |
438 * \brief Read from the disc into multiple buffers and decrypt data if | |
439 * requested. | |
440 * | |
441 * \param dvdcss a \e libdvdcss instance. | |
442 * \param p_iovec a pointer to an array of iovec structures that will contain | |
443 * the data read from the disc. | |
444 * \param i_blocks the amount of blocks to read. | |
445 * \param i_flags #DVDCSS_NOFLAGS, optionally ored with #DVDCSS_READ_DECRYPT. | |
446 * \return the amount of blocks read, or a negative value in case an | |
447 * error happened. | |
448 * | |
449 * This function reads \p i_blocks logical blocks from the DVD and writes them | |
450 * to an array of iovec structures. | |
451 * | |
452 * You typically set \p i_flags to #DVDCSS_NOFLAGS when reading data from a | |
453 * .IFO file on the DVD. | |
454 * | |
455 * If #DVDCSS_READ_DECRYPT is specified in \p i_flags, dvdcss_readv() will | |
456 * automatically decrypt scrambled sectors. This flag is typically used when | |
457 * reading data from a .VOB file on the DVD. It has no effect on unscrambled | |
458 * discs or unscrambled sectors, and can be safely used on those. | |
459 * | |
460 * \warning dvdcss_readv() expects to be able to write \p i_blocks * | |
461 * #DVDCSS_BLOCK_SIZE bytes in the buffers pointed by \p p_iovec. | |
462 * Moreover, all iov_len members of the iovec structures should be | |
463 * multiples of #DVDCSS_BLOCK_SIZE. | |
464 */ | |
465 extern int dvdcss_readv ( dvdcss_t dvdcss, void *p_iovec, | |
466 int i_blocks, | |
467 int i_flags ) | |
468 { | |
469 struct iovec *_p_iovec = (struct iovec *)p_iovec; | |
470 int i_ret, i_index; | |
471 void *iov_base; | |
472 size_t iov_len; | |
473 | |
474 i_ret = _dvdcss_readv( dvdcss, _p_iovec, i_blocks ); | |
475 | |
476 if( i_ret <= 0 | |
477 || !dvdcss->b_scrambled | |
478 || !(i_flags & DVDCSS_READ_DECRYPT) ) | |
479 { | |
480 return i_ret; | |
481 } | |
482 | |
483 /* Initialize loop for decryption */ | |
484 iov_base = _p_iovec->iov_base; | |
485 iov_len = _p_iovec->iov_len; | |
486 | |
487 /* Decrypt the blocks we managed to read */ | |
488 for( i_index = i_ret; i_index; i_index-- ) | |
489 { | |
490 /* Check that iov_len is a multiple of 2048 */ | |
491 if( iov_len & 0x7ff ) | |
492 { | |
493 return -1; | |
494 } | |
495 | |
496 while( iov_len == 0 ) | |
497 { | |
498 _p_iovec++; | |
499 iov_base = _p_iovec->iov_base; | |
500 iov_len = _p_iovec->iov_len; | |
501 } | |
502 | |
503 _dvdcss_unscramble( dvdcss->css.p_title_key, iov_base ); | |
504 ((u8*)iov_base)[0x14] &= 0x8f; | |
505 | |
506 iov_base = (void *) ((u8*)iov_base + DVDCSS_BLOCK_SIZE); | |
507 iov_len -= DVDCSS_BLOCK_SIZE; | |
508 } | |
509 | |
510 return i_ret; | |
511 } | |
512 | |
513 /** | |
514 * \brief Close the DVD and clean up the library. | |
515 * | |
516 * \param dvdcss a \e libdvdcss instance. | |
517 * \return zero in case of success, a negative value otherwise. | |
518 * | |
519 * This function closes the DVD device and frees all the memory allocated | |
520 * by \e libdvdcss. On return, the #dvdcss_t is invalidated and may not be | |
521 * used again. | |
522 */ | |
523 extern int dvdcss_close ( dvdcss_t dvdcss ) | |
524 { | |
525 dvd_title_t *p_title; | |
526 int i_ret; | |
527 | |
528 /* Free our list of keys */ | |
529 p_title = dvdcss->p_titles; | |
530 while( p_title ) | |
531 { | |
532 dvd_title_t *p_tmptitle = p_title->p_next; | |
533 free( p_title ); | |
534 p_title = p_tmptitle; | |
535 } | |
536 | |
537 i_ret = _dvdcss_close( dvdcss ); | |
538 | |
539 if( i_ret < 0 ) | |
540 { | |
541 return i_ret; | |
542 } | |
543 | |
544 free( dvdcss->psz_device ); | |
545 free( dvdcss ); | |
546 | |
547 return 0; | |
548 } | |
549 | |
550 /* | |
551 * Deprecated. See dvdcss_seek(). | |
552 */ | |
553 #undef dvdcss_title | |
554 extern int dvdcss_title ( dvdcss_t dvdcss, int i_block ) | |
555 { | |
556 return _dvdcss_title( dvdcss, i_block ); | |
557 } | |
558 |