libdvdcss.c
来自「君正早期ucos系统(只有早期的才不没有打包成库),MPLAYER,文件系统,图」· C语言 代码 · 共 799 行 · 第 1/2 页
C
799 行
/* If the cache is enabled, extract a unique disc ID */ if( psz_cache ) { uint8_t p_sector[DVDCSS_BLOCK_SIZE]; char psz_debug[PATH_MAX + 30]; char psz_key[1 + KEY_SIZE * 2 + 1]; char *psz_title; uint8_t *psz_serial; int i; /* We read sector 0. If it starts with 0x000001ba (BE), we are * reading a VOB file, and we should not cache anything. */ i_ret = dvdcss->pf_seek( dvdcss, 0 ); if( i_ret != 0 ) { goto nocache; } i_ret = dvdcss->pf_read( dvdcss, p_sector, 1 ); if( i_ret != 1 ) { goto nocache; } if( p_sector[0] == 0x00 && p_sector[1] == 0x00 && p_sector[2] == 0x01 && p_sector[3] == 0xba ) { goto nocache; } /* The data we are looking for is at sector 16 (32768 bytes): * - offset 40: disc title (32 uppercase chars) * - offset 813: manufacturing date + serial no (16 digits) */ i_ret = dvdcss->pf_seek( dvdcss, 16 ); if( i_ret != 16 ) { goto nocache; } i_ret = dvdcss->pf_read( dvdcss, p_sector, 1 ); if( i_ret != 1 ) { goto nocache; } /* Get the disc title */ psz_title = (char *)p_sector + 40; psz_title[32] = '\0'; for( i = 0 ; i < 32 ; i++ ) { if( psz_title[i] <= ' ' ) { psz_title[i] = '\0'; break; } else if( psz_title[i] == '/' || psz_title[i] == '\\' ) { psz_title[i] = '-'; } } /* Get the date + serial */ psz_serial = p_sector + 813; psz_serial[16] = '\0'; /* Check that all characters are digits, otherwise convert. */ for( i = 0 ; i < 16 ; i++ ) { if( psz_serial[i] < '0' || psz_serial[i] > '9' ) { char psz_tmp[16 + 1]; sprintf( psz_tmp, "%.2"PRIx8"%.2"PRIx8"%.2"PRIx8"%.2"PRIx8"%.2"PRIx8"%.2"PRIx8"%.2"PRIx8"%.2"PRIx8"", psz_serial[0], psz_serial[1], psz_serial[2], psz_serial[3], psz_serial[4], psz_serial[5], psz_serial[6], psz_serial[7] ); memcpy( psz_serial, psz_tmp, 16 ); break; } } /* Get disk key, since some discs have got same title, manufacturing * date and serial number, but different keys */ if( dvdcss->b_scrambled ) { psz_key[0] = '-'; for( i = 0; i < KEY_SIZE; i++ ) { sprintf( &psz_key[1+i*2], "%.2"PRIx8, dvdcss->css.p_disc_key[i] ); } psz_key[1 + KEY_SIZE * 2] = '\0'; } else { psz_key[0] = 0; } /* We have a disc name or ID, we can create the cache dir */ i = sprintf( dvdcss->psz_cachefile, "%s", psz_cache );#if !defined( WIN32 ) || defined( SYS_CYGWIN ) i_ret = mkdir( dvdcss->psz_cachefile, 0755 );#else i_ret = mkdir( dvdcss->psz_cachefile );#endif if( i_ret < 0 && errno != EEXIST ) { print_error( dvdcss, "failed creating cache directory" ); dvdcss->psz_cachefile[0] = '\0'; goto nocache; } i += sprintf( dvdcss->psz_cachefile + i, "/%s-%s%s", psz_title, psz_serial, psz_key );#if !defined( WIN32 ) || defined( SYS_CYGWIN ) i_ret = mkdir( dvdcss->psz_cachefile, 0755 );#else i_ret = mkdir( dvdcss->psz_cachefile );#endif if( i_ret < 0 && errno != EEXIST ) { print_error( dvdcss, "failed creating cache subdirectory" ); dvdcss->psz_cachefile[0] = '\0'; goto nocache; } i += sprintf( dvdcss->psz_cachefile + i, "/"); /* Pointer to the filename we will use. */ dvdcss->psz_block = dvdcss->psz_cachefile + i; sprintf( psz_debug, "using CSS key cache dir: %s", dvdcss->psz_cachefile ); print_debug( dvdcss, psz_debug ); } nocache:#ifndef WIN32 if( psz_raw_device != NULL ) { _dvdcss_raw_open( dvdcss, psz_raw_device ); }#endif /* Seek at the beginning, just for safety. */ dvdcss->pf_seek( dvdcss, 0 ); return dvdcss;}/** * \brief Return a string containing the latest error that occurred in the * given \e libdvdcss instance. * * \param dvdcss a \e libdvdcss instance. * \return a null-terminated string containing the latest error message. * * This function returns a constant string containing the latest error that * occurred in \e libdvdcss. It can be used to format error messages at your * convenience in your application. */LIBDVDCSS_EXPORT char * dvdcss_error ( dvdcss_t dvdcss ){ return dvdcss->psz_error;}/** * \brief Seek in the disc and change the current key if requested. * * \param dvdcss a \e libdvdcss instance. * \param i_blocks an absolute block offset to seek to. * \param i_flags #DVDCSS_NOFLAGS, optionally ored with one of #DVDCSS_SEEK_KEY * or #DVDCSS_SEEK_MPEG. * \return the new position in blocks, or a negative value in case an error * happened. * * This function seeks to the requested position, in logical blocks. * * You typically set \p i_flags to #DVDCSS_NOFLAGS when seeking in a .IFO. * * If #DVDCSS_SEEK_MPEG is specified in \p i_flags and if \e libdvdcss finds it * reasonable to do so (ie, if the dvdcss method is not "title"), the current * title key will be checked and a new one will be calculated if necessary. * This flag is typically used when reading data from a VOB. * * If #DVDCSS_SEEK_KEY is specified, the title key will be always checked, * even with the "title" method. This is equivalent to using the now * deprecated dvdcss_title() call. This flag is typically used when seeking * in a new title. */LIBDVDCSS_EXPORT int dvdcss_seek ( dvdcss_t dvdcss, int i_blocks, int i_flags ){ /* title cracking method is too slow to be used at each seek */ if( ( ( i_flags & DVDCSS_SEEK_MPEG ) && ( dvdcss->i_method != DVDCSS_METHOD_TITLE ) ) || ( i_flags & DVDCSS_SEEK_KEY ) ) { /* check the title key */ if( _dvdcss_title( dvdcss, i_blocks ) ) { return -1; } } return dvdcss->pf_seek( dvdcss, i_blocks );}/** * \brief Read from the disc and decrypt data if requested. * * \param dvdcss a \e libdvdcss instance. * \param p_buffer a buffer that will contain the data read from the disc. * \param i_blocks the amount of blocks to read. * \param i_flags #DVDCSS_NOFLAGS, optionally ored with #DVDCSS_READ_DECRYPT. * \return the amount of blocks read, or a negative value in case an * error happened. * * This function reads \p i_blocks logical blocks from the DVD. * * You typically set \p i_flags to #DVDCSS_NOFLAGS when reading data from a * .IFO file on the DVD. * * If #DVDCSS_READ_DECRYPT is specified in \p i_flags, dvdcss_read() will * automatically decrypt scrambled sectors. This flag is typically used when * reading data from a .VOB file on the DVD. It has no effect on unscrambled * discs or unscrambled sectors, and can be safely used on those. * * \warning dvdcss_read() expects to be able to write \p i_blocks * * #DVDCSS_BLOCK_SIZE bytes in \p p_buffer. */LIBDVDCSS_EXPORT int dvdcss_read ( dvdcss_t dvdcss, void *p_buffer, int i_blocks, int i_flags ){ int i_ret, i_index; i_ret = dvdcss->pf_read( dvdcss, p_buffer, i_blocks ); if( i_ret <= 0 || !dvdcss->b_scrambled || !(i_flags & DVDCSS_READ_DECRYPT) ) { return i_ret; } if( ! memcmp( dvdcss->css.p_title_key, "\0\0\0\0\0", 5 ) ) { /* For what we believe is an unencrypted title, * check that there are no encrypted blocks */ for( i_index = i_ret; i_index; i_index-- ) { if( ((uint8_t*)p_buffer)[0x14] & 0x30 ) { print_error( dvdcss, "no key but found encrypted block" ); /* Only return the initial range of unscrambled blocks? */ /* or fail completely? return 0; */ break; } p_buffer = (void *) ((uint8_t *)p_buffer + DVDCSS_BLOCK_SIZE); } } else { /* Decrypt the blocks we managed to read */ for( i_index = i_ret; i_index; i_index-- ) { _dvdcss_unscramble( dvdcss->css.p_title_key, p_buffer ); ((uint8_t*)p_buffer)[0x14] &= 0x8f; p_buffer = (void *) ((uint8_t *)p_buffer + DVDCSS_BLOCK_SIZE); } } return i_ret;}/** * \brief Read from the disc into multiple buffers and decrypt data if * requested. * * \param dvdcss a \e libdvdcss instance. * \param p_iovec a pointer to an array of iovec structures that will contain * the data read from the disc. * \param i_blocks the amount of blocks to read. * \param i_flags #DVDCSS_NOFLAGS, optionally ored with #DVDCSS_READ_DECRYPT. * \return the amount of blocks read, or a negative value in case an * error happened. * * This function reads \p i_blocks logical blocks from the DVD and writes them * to an array of iovec structures. * * You typically set \p i_flags to #DVDCSS_NOFLAGS when reading data from a * .IFO file on the DVD. * * If #DVDCSS_READ_DECRYPT is specified in \p i_flags, dvdcss_readv() will * automatically decrypt scrambled sectors. This flag is typically used when * reading data from a .VOB file on the DVD. It has no effect on unscrambled * discs or unscrambled sectors, and can be safely used on those. * * \warning dvdcss_readv() expects to be able to write \p i_blocks * * #DVDCSS_BLOCK_SIZE bytes in the buffers pointed by \p p_iovec. * Moreover, all iov_len members of the iovec structures should be * multiples of #DVDCSS_BLOCK_SIZE. */LIBDVDCSS_EXPORT int dvdcss_readv ( dvdcss_t dvdcss, void *p_iovec, int i_blocks, int i_flags ){ struct iovec *_p_iovec = (struct iovec *)p_iovec; int i_ret, i_index; void *iov_base; size_t iov_len; i_ret = dvdcss->pf_readv( dvdcss, _p_iovec, i_blocks ); if( i_ret <= 0 || !dvdcss->b_scrambled || !(i_flags & DVDCSS_READ_DECRYPT) ) { return i_ret; } /* Initialize loop for decryption */ iov_base = _p_iovec->iov_base; iov_len = _p_iovec->iov_len; /* Decrypt the blocks we managed to read */ for( i_index = i_ret; i_index; i_index-- ) { /* Check that iov_len is a multiple of 2048 */ if( iov_len & 0x7ff ) { return -1; } while( iov_len == 0 ) { _p_iovec++; iov_base = _p_iovec->iov_base; iov_len = _p_iovec->iov_len; } _dvdcss_unscramble( dvdcss->css.p_title_key, iov_base ); ((uint8_t*)iov_base)[0x14] &= 0x8f; iov_base = (void *) ((uint8_t*)iov_base + DVDCSS_BLOCK_SIZE); iov_len -= DVDCSS_BLOCK_SIZE; } return i_ret;}/** * \brief Close the DVD and clean up the library. * * \param dvdcss a \e libdvdcss instance. * \return zero in case of success, a negative value otherwise. * * This function closes the DVD device and frees all the memory allocated * by \e libdvdcss. On return, the #dvdcss_t is invalidated and may not be * used again. */LIBDVDCSS_EXPORT int dvdcss_close ( dvdcss_t dvdcss ){ dvd_title_t *p_title; int i_ret; /* Free our list of keys */ p_title = dvdcss->p_titles; while( p_title ) { dvd_title_t *p_tmptitle = p_title->p_next; free( p_title ); p_title = p_tmptitle; } i_ret = _dvdcss_close( dvdcss ); if( i_ret < 0 ) { return i_ret; } free( dvdcss->psz_device ); free( dvdcss ); return 0;}/* * Deprecated. See dvdcss_seek(). */#undef dvdcss_titleLIBDVDCSS_EXPORT int dvdcss_title ( dvdcss_t dvdcss, int i_block ){ return _dvdcss_title( dvdcss, i_block );}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?