Mercurial > mplayer.hg
comparison libmpdvdkit2/libdvdcss.c @ 7027:c9a4dfaa9868
importing libdvdcss 1.2.2 files
author | arpi |
---|---|
date | Fri, 16 Aug 2002 22:35:45 +0000 |
parents | |
children | fd2d4be9ed6f |
comparison
equal
deleted
inserted
replaced
7026:a3126e9099b4 | 7027:c9a4dfaa9868 |
---|---|
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 | |
100 #include "dvdcss/dvdcss.h" | |
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 | |
120 /** | |
121 * \brief Open a DVD device or directory and return a dvdcss instance. | |
122 * | |
123 * \param psz_target a string containing the target name, for instance | |
124 * "/dev/hdc" or "E:". | |
125 * \return a handle to a dvdcss instance or NULL on error. | |
126 * | |
127 * Initialize the \e libdvdcss library and open the requested DVD device or | |
128 * directory. \e libdvdcss checks whether ioctls can be performed on the disc, | |
129 * and when possible, the disc key is retrieved. | |
130 * | |
131 * dvdcss_open() returns a handle to be used for all subsequent \e libdvdcss | |
132 * calls. If an error occured, NULL is returned. | |
133 */ | |
134 extern dvdcss_t dvdcss_open ( char *psz_target ) | |
135 { | |
136 int i_ret; | |
137 | |
138 char *psz_method = getenv( "DVDCSS_METHOD" ); | |
139 char *psz_verbose = getenv( "DVDCSS_VERBOSE" ); | |
140 #ifndef WIN32 | |
141 char *psz_raw_device = getenv( "DVDCSS_RAW_DEVICE" ); | |
142 #endif | |
143 | |
144 dvdcss_t dvdcss; | |
145 | |
146 /* | |
147 * Allocate the library structure | |
148 */ | |
149 dvdcss = malloc( sizeof( struct dvdcss_s ) ); | |
150 if( dvdcss == NULL ) | |
151 { | |
152 return NULL; | |
153 } | |
154 | |
155 /* | |
156 * Initialize structure with default values | |
157 */ | |
158 #ifndef WIN32 | |
159 dvdcss->i_raw_fd = -1; | |
160 #endif | |
161 dvdcss->p_titles = NULL; | |
162 dvdcss->psz_device = (char *)strdup( psz_target ); | |
163 dvdcss->psz_error = "no error"; | |
164 dvdcss->i_method = DVDCSS_METHOD_KEY; | |
165 dvdcss->b_debug = 0; | |
166 dvdcss->b_errors = 0; | |
167 | |
168 /* | |
169 * Find verbosity from DVDCSS_VERBOSE environment variable | |
170 */ | |
171 if( psz_verbose != NULL ) | |
172 { | |
173 switch( atoi( psz_verbose ) ) | |
174 { | |
175 case 2: | |
176 dvdcss->b_debug = 1; | |
177 case 1: | |
178 dvdcss->b_errors = 1; | |
179 case 0: | |
180 break; | |
181 } | |
182 } | |
183 | |
184 /* | |
185 * Find method from DVDCSS_METHOD environment variable | |
186 */ | |
187 if( psz_method != NULL ) | |
188 { | |
189 if( !strncmp( psz_method, "key", 4 ) ) | |
190 { | |
191 dvdcss->i_method = DVDCSS_METHOD_KEY; | |
192 } | |
193 else if( !strncmp( psz_method, "disc", 5 ) ) | |
194 { | |
195 dvdcss->i_method = DVDCSS_METHOD_DISC; | |
196 } | |
197 else if( !strncmp( psz_method, "title", 5 ) ) | |
198 { | |
199 dvdcss->i_method = DVDCSS_METHOD_TITLE; | |
200 } | |
201 else | |
202 { | |
203 _dvdcss_error( dvdcss, "unknown decrypt method, please choose " | |
204 "from 'title', 'key' or 'disc'" ); | |
205 free( dvdcss->psz_device ); | |
206 free( dvdcss ); | |
207 return NULL; | |
208 } | |
209 } | |
210 | |
211 /* | |
212 * Open device | |
213 */ | |
214 i_ret = _dvdcss_open( dvdcss ); | |
215 if( i_ret < 0 ) | |
216 { | |
217 free( dvdcss->psz_device ); | |
218 free( dvdcss ); | |
219 return NULL; | |
220 } | |
221 | |
222 dvdcss->b_scrambled = 1; /* Assume the worst */ | |
223 dvdcss->b_ioctls = _dvdcss_use_ioctls( dvdcss ); | |
224 | |
225 if( dvdcss->b_ioctls ) | |
226 { | |
227 i_ret = _dvdcss_test( dvdcss ); | |
228 if( i_ret < 0 ) | |
229 { | |
230 /* Disable the CSS ioctls and hope that it works? */ | |
231 _dvdcss_debug( dvdcss, | |
232 "could not check whether the disc was scrambled" ); | |
233 dvdcss->b_ioctls = 0; | |
234 } | |
235 else | |
236 { | |
237 _dvdcss_debug( dvdcss, i_ret ? "disc is scrambled" | |
238 : "disc is unscrambled" ); | |
239 dvdcss->b_scrambled = i_ret; | |
240 } | |
241 } | |
242 | |
243 /* If disc is CSS protected and the ioctls work, authenticate the drive */ | |
244 if( dvdcss->b_scrambled && dvdcss->b_ioctls ) | |
245 { | |
246 i_ret = _dvdcss_disckey( dvdcss ); | |
247 | |
248 if( i_ret < 0 ) | |
249 { | |
250 _dvdcss_close( dvdcss ); | |
251 free( dvdcss->psz_device ); | |
252 free( dvdcss ); | |
253 return NULL; | |
254 } | |
255 } | |
256 | |
257 #ifndef WIN32 | |
258 if( psz_raw_device != NULL ) | |
259 { | |
260 _dvdcss_raw_open( dvdcss, psz_raw_device ); | |
261 } | |
262 #endif | |
263 | |
264 return dvdcss; | |
265 } | |
266 | |
267 /** | |
268 * \brief Return a string containing the latest error that occured in the | |
269 * given \e libdvdcss instance. | |
270 * | |
271 * \param dvdcss a \e libdvdcss instance. | |
272 * \return a null-terminated string containing the latest error message. | |
273 * | |
274 * This function returns a constant string containing the latest error that | |
275 * occured in \e libdvdcss. It can be used to format error messages at your | |
276 * convenience in your application. | |
277 */ | |
278 extern char * dvdcss_error ( dvdcss_t dvdcss ) | |
279 { | |
280 return dvdcss->psz_error; | |
281 } | |
282 | |
283 /** | |
284 * \brief Seek in the disc and change the current key if requested. | |
285 * | |
286 * \param dvdcss a \e libdvdcss instance. | |
287 * \param i_blocks an absolute block offset to seek to. | |
288 * \param i_flags #DVDCSS_NOFLAGS, optionally ored with one of #DVDCSS_SEEK_KEY | |
289 * or #DVDCSS_SEEK_MPEG. | |
290 * \return the new position in blocks, or a negative value in case an error | |
291 * happened. | |
292 * | |
293 * This function seeks to the requested position, in logical blocks. | |
294 * | |
295 * You typically set \p i_flags to #DVDCSS_NOFLAGS when seeking in a .IFO. | |
296 * | |
297 * If #DVDCSS_SEEK_MPEG is specified in \p i_flags and if \e libdvdcss finds it | |
298 * reasonable to do so (ie, if the dvdcss method is not "title"), the current | |
299 * title key will be checked and a new one will be calculated if necessary. | |
300 * This flag is typically used when reading data from a VOB. | |
301 * | |
302 * If #DVDCSS_SEEK_KEY is specified, the title key will be always checked, | |
303 * even with the "title" method. This is equivalent to using the now | |
304 * deprecated dvdcss_title() call. This flag is typically used when seeking | |
305 * in a new title. | |
306 */ | |
307 extern int dvdcss_seek ( dvdcss_t dvdcss, int i_blocks, int i_flags ) | |
308 { | |
309 /* title cracking method is too slow to be used at each seek */ | |
310 if( ( ( i_flags & DVDCSS_SEEK_MPEG ) | |
311 && ( dvdcss->i_method != DVDCSS_METHOD_TITLE ) ) | |
312 || ( i_flags & DVDCSS_SEEK_KEY ) ) | |
313 { | |
314 /* check the title key */ | |
315 if( _dvdcss_title( dvdcss, i_blocks ) ) | |
316 { | |
317 return -1; | |
318 } | |
319 } | |
320 | |
321 return _dvdcss_seek( dvdcss, i_blocks ); | |
322 } | |
323 | |
324 /** | |
325 * \brief Read from the disc and decrypt data if requested. | |
326 * | |
327 * \param dvdcss a \e libdvdcss instance. | |
328 * \param p_buffer a buffer that will contain the data read from the disc. | |
329 * \param i_blocks the amount of blocks to read. | |
330 * \param i_flags #DVDCSS_NOFLAGS, optionally ored with #DVDCSS_READ_DECRYPT. | |
331 * \return the amount of blocks read, or a negative value in case an | |
332 * error happened. | |
333 * | |
334 * This function reads \p i_blocks logical blocks from the DVD. | |
335 * | |
336 * You typically set \p i_flags to #DVDCSS_NOFLAGS when reading data from a | |
337 * .IFO file on the DVD. | |
338 * | |
339 * If #DVDCSS_READ_DECRYPT is specified in \p i_flags, dvdcss_read() will | |
340 * automatically decrypt scrambled sectors. This flag is typically used when | |
341 * reading data from a .VOB file on the DVD. It has no effect on unscrambled | |
342 * discs or unscrambled sectors, and can be safely used on those. | |
343 * | |
344 * \warning dvdcss_read() expects to be able to write \p i_blocks * | |
345 * #DVDCSS_BLOCK_SIZE bytes in \p p_buffer. | |
346 */ | |
347 extern int dvdcss_read ( dvdcss_t dvdcss, void *p_buffer, | |
348 int i_blocks, | |
349 int i_flags ) | |
350 { | |
351 int i_ret, i_index; | |
352 | |
353 i_ret = _dvdcss_read( dvdcss, p_buffer, i_blocks ); | |
354 | |
355 if( i_ret <= 0 | |
356 || !dvdcss->b_scrambled | |
357 || !(i_flags & DVDCSS_READ_DECRYPT) ) | |
358 { | |
359 return i_ret; | |
360 } | |
361 | |
362 if( ! memcmp( dvdcss->css.p_title_key, "\0\0\0\0\0", 5 ) ) | |
363 { | |
364 /* For what we believe is an unencrypted title, | |
365 * check that there are no encrypted blocks */ | |
366 for( i_index = i_ret; i_index; i_index-- ) | |
367 { | |
368 if( ((u8*)p_buffer)[0x14] & 0x30 ) | |
369 { | |
370 _dvdcss_error( dvdcss, "no key but found encrypted block" ); | |
371 /* Only return the initial range of unscrambled blocks? */ | |
372 /* or fail completely? return 0; */ | |
373 break; | |
374 } | |
375 p_buffer = (void *) ((u8 *)p_buffer + DVDCSS_BLOCK_SIZE); | |
376 } | |
377 } | |
378 else | |
379 { | |
380 /* Decrypt the blocks we managed to read */ | |
381 for( i_index = i_ret; i_index; i_index-- ) | |
382 { | |
383 _dvdcss_unscramble( dvdcss->css.p_title_key, p_buffer ); | |
384 ((u8*)p_buffer)[0x14] &= 0x8f; | |
385 p_buffer = (void *) ((u8 *)p_buffer + DVDCSS_BLOCK_SIZE); | |
386 } | |
387 } | |
388 | |
389 return i_ret; | |
390 } | |
391 | |
392 /** | |
393 * \brief Read from the disc into multiple buffers and decrypt data if | |
394 * requested. | |
395 * | |
396 * \param dvdcss a \e libdvdcss instance. | |
397 * \param p_iovec a pointer to an array of iovec structures that will contain | |
398 * the data read from the disc. | |
399 * \param i_blocks the amount of blocks to read. | |
400 * \param i_flags #DVDCSS_NOFLAGS, optionally ored with #DVDCSS_READ_DECRYPT. | |
401 * \return the amount of blocks read, or a negative value in case an | |
402 * error happened. | |
403 * | |
404 * This function reads \p i_blocks logical blocks from the DVD and writes them | |
405 * to an array of iovec structures. | |
406 * | |
407 * You typically set \p i_flags to #DVDCSS_NOFLAGS when reading data from a | |
408 * .IFO file on the DVD. | |
409 * | |
410 * If #DVDCSS_READ_DECRYPT is specified in \p i_flags, dvdcss_readv() will | |
411 * automatically decrypt scrambled sectors. This flag is typically used when | |
412 * reading data from a .VOB file on the DVD. It has no effect on unscrambled | |
413 * discs or unscrambled sectors, and can be safely used on those. | |
414 * | |
415 * \warning dvdcss_readv() expects to be able to write \p i_blocks * | |
416 * #DVDCSS_BLOCK_SIZE bytes in the buffers pointed by \p p_iovec. | |
417 * Moreover, all iov_len members of the iovec structures should be | |
418 * multiples of #DVDCSS_BLOCK_SIZE. | |
419 */ | |
420 extern int dvdcss_readv ( dvdcss_t dvdcss, void *p_iovec, | |
421 int i_blocks, | |
422 int i_flags ) | |
423 { | |
424 struct iovec *_p_iovec = (struct iovec *)p_iovec; | |
425 int i_ret, i_index; | |
426 void *iov_base; | |
427 size_t iov_len; | |
428 | |
429 i_ret = _dvdcss_readv( dvdcss, _p_iovec, i_blocks ); | |
430 | |
431 if( i_ret <= 0 | |
432 || !dvdcss->b_scrambled | |
433 || !(i_flags & DVDCSS_READ_DECRYPT) ) | |
434 { | |
435 return i_ret; | |
436 } | |
437 | |
438 /* Initialize loop for decryption */ | |
439 iov_base = _p_iovec->iov_base; | |
440 iov_len = _p_iovec->iov_len; | |
441 | |
442 /* Decrypt the blocks we managed to read */ | |
443 for( i_index = i_ret; i_index; i_index-- ) | |
444 { | |
445 /* Check that iov_len is a multiple of 2048 */ | |
446 if( iov_len & 0x7ff ) | |
447 { | |
448 return -1; | |
449 } | |
450 | |
451 while( iov_len == 0 ) | |
452 { | |
453 _p_iovec++; | |
454 iov_base = _p_iovec->iov_base; | |
455 iov_len = _p_iovec->iov_len; | |
456 } | |
457 | |
458 _dvdcss_unscramble( dvdcss->css.p_title_key, iov_base ); | |
459 ((u8*)iov_base)[0x14] &= 0x8f; | |
460 | |
461 iov_base = (void *) ((u8*)iov_base + DVDCSS_BLOCK_SIZE); | |
462 iov_len -= DVDCSS_BLOCK_SIZE; | |
463 } | |
464 | |
465 return i_ret; | |
466 } | |
467 | |
468 /** | |
469 * \brief Close the DVD and clean up the library. | |
470 * | |
471 * \param dvdcss a \e libdvdcss instance. | |
472 * \return zero in case of success, a negative value otherwise. | |
473 * | |
474 * This function closes the DVD device and frees all the memory allocated | |
475 * by \e libdvdcss. On return, the #dvdcss_t is invalidated and may not be | |
476 * used again. | |
477 */ | |
478 extern int dvdcss_close ( dvdcss_t dvdcss ) | |
479 { | |
480 dvd_title_t *p_title; | |
481 int i_ret; | |
482 | |
483 /* Free our list of keys */ | |
484 p_title = dvdcss->p_titles; | |
485 while( p_title ) | |
486 { | |
487 dvd_title_t *p_tmptitle = p_title->p_next; | |
488 free( p_title ); | |
489 p_title = p_tmptitle; | |
490 } | |
491 | |
492 i_ret = _dvdcss_close( dvdcss ); | |
493 | |
494 if( i_ret < 0 ) | |
495 { | |
496 return i_ret; | |
497 } | |
498 | |
499 free( dvdcss->psz_device ); | |
500 free( dvdcss ); | |
501 | |
502 return 0; | |
503 } | |
504 | |
505 /* | |
506 * Deprecated. See dvdcss_seek(). | |
507 */ | |
508 #undef dvdcss_title | |
509 extern int dvdcss_title ( dvdcss_t dvdcss, int i_block ) | |
510 { | |
511 return _dvdcss_title( dvdcss, i_block ); | |
512 } | |
513 |