]> git.sesse.net Git - vlc/blob - plugins/dvd/dummy_dvdcss.c
Some heavy changes today:
[vlc] / plugins / dvd / dummy_dvdcss.c
1 /*****************************************************************************
2  * dummy_dvdcss.c: Dummy libdvdcss with minimal DVD access.
3  *****************************************************************************
4  * Copyright (C) 2001 VideoLAN
5  * $Id: dummy_dvdcss.c,v 1.5 2001/12/30 07:09:55 sam Exp $
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
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  * 
14  * This program is distributed in the hope that it will be useful,
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  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <fcntl.h>
30 #include <unistd.h>
31
32 #include <videolan/vlc.h>
33
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <sys/uio.h>                                        /* struct iovec */
37 #include <sys/ioctl.h>
38
39 #ifdef DVD_STRUCT_IN_LINUX_CDROM_H
40 #   include <netinet/in.h>
41 #   include <linux/cdrom.h>
42 #else
43 #   error "building dummy libdvdcss on this system does not make sense !"
44 #endif
45
46 #include "dummy_dvdcss.h"
47
48 /*****************************************************************************
49  * Local structure
50  *****************************************************************************/
51 struct dvdcss_s
52 {
53     /* File descriptor */
54     int i_fd;
55 };
56
57 /*****************************************************************************
58  * dvdcss_open: initialize library, open a DVD device, crack CSS key
59  *****************************************************************************/
60 extern dvdcss_handle dummy_dvdcss_open ( char *psz_target )
61 {
62     dvdcss_handle dvdcss;
63     dvd_struct    dvd;
64
65     /* Allocate the library structure */
66     dvdcss = malloc( sizeof( struct dvdcss_s ) );
67     if( dvdcss == NULL )
68     {
69         fprintf( stderr, "dvd error: "
70                          "dummy libdvdcss could not allocate memory\n" );
71         return NULL;
72     }
73
74     /* Open the device */
75     dvdcss->i_fd = open( psz_target, 0 );
76     if( dvdcss->i_fd < 0 )
77     {
78         fprintf( stderr, "dvd error: "
79                          "dummy libdvdcss could not open device\n" );
80         free( dvdcss );
81         return NULL;
82     }
83
84     /* Check for encryption or ioctl failure */
85     dvd.type = DVD_STRUCT_COPYRIGHT;
86     dvd.copyright.layer_num = 0;
87     if( ioctl( dvdcss->i_fd, DVD_READ_STRUCT, &dvd ) != 0
88          || dvd.copyright.cpst )
89     {
90         fprintf( stderr, "dvd error: "
91                          "dummy libdvdcss could not decrypt disc\n" );
92         close( dvdcss->i_fd );
93         free( dvdcss );
94         return NULL;
95     }
96
97     return dvdcss;
98 }
99
100 /*****************************************************************************
101  * dvdcss_error: return the last libdvdcss error message
102  *****************************************************************************/
103 extern char * dummy_dvdcss_error ( dvdcss_handle dvdcss )
104 {
105     return "unknown error";
106 }
107
108 /*****************************************************************************
109  * dvdcss_seek: seek into the device
110  *****************************************************************************/
111 extern int dummy_dvdcss_seek ( dvdcss_handle dvdcss, int i_blocks,
112                                                      int i_flags )
113 {
114     off_t i_read;
115
116     i_read = lseek( dvdcss->i_fd,
117                     (off_t)i_blocks * (off_t)DVDCSS_BLOCK_SIZE, SEEK_SET );
118
119     return i_read / DVDCSS_BLOCK_SIZE;
120 }
121
122 /*****************************************************************************
123  * dvdcss_title: crack the current title key if needed
124  *****************************************************************************/
125 extern int dummy_dvdcss_title ( dvdcss_handle dvdcss, int i_block )
126 {
127     return 0;
128 }
129
130 /*****************************************************************************
131  * dvdcss_read: read data from the device, decrypt if requested
132  *****************************************************************************/
133 extern int dummy_dvdcss_read ( dvdcss_handle dvdcss, void *p_buffer,
134                                                      int i_blocks,
135                                                      int i_flags )
136 {
137     int i_bytes;
138
139     i_bytes = read( dvdcss->i_fd, p_buffer,
140                     (size_t)i_blocks * DVDCSS_BLOCK_SIZE );
141
142     return i_bytes / DVDCSS_BLOCK_SIZE;
143 }
144
145 /*****************************************************************************
146  * dvdcss_readv: read data to an iovec structure, decrypt if reaquested
147  *****************************************************************************/
148 extern int dummy_dvdcss_readv ( dvdcss_handle dvdcss, void *p_iovec,
149                                                       int i_blocks,
150                                                       int i_flags )
151 {
152     int i_read;
153
154     i_read = readv( dvdcss->i_fd, (struct iovec*)p_iovec, i_blocks );
155
156     return i_read / DVDCSS_BLOCK_SIZE;
157 }
158
159 /*****************************************************************************
160  * dvdcss_close: close the DVD device and clean up the library
161  *****************************************************************************/
162 extern int dummy_dvdcss_close ( dvdcss_handle dvdcss )
163 {
164     int i_ret;
165
166     i_ret = close( dvdcss->i_fd );
167
168     if( i_ret < 0 )
169     {
170         return i_ret;
171     }
172
173     free( dvdcss );
174
175     return 0;
176 }
177