168
|
1 /*
|
|
2 * Copyright (C) 2002 Samuel Hocevar <sam@zoy.org>,
|
|
3 * Håkan Hjort <d95hjort@dtek.chalmers.se>
|
|
4 *
|
|
5 * This program is free software; you can redistribute it and/or modify
|
|
6 * it under the terms of the GNU General Public License as published by
|
|
7 * the Free Software Foundation; either version 2 of the License, or
|
|
8 * (at your option) any later version.
|
|
9 *
|
|
10 * This program is distributed in the hope that it will be useful,
|
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13 * GNU General Public License for more details.
|
|
14 *
|
|
15 * You should have received a copy of the GNU General Public License
|
|
16 * along with this program; if not, write to the Free Software
|
|
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
|
|
18 */
|
|
19
|
|
20 #include "config.h"
|
|
21
|
|
22 #include <stdio.h>
|
|
23 #include <stdlib.h>
|
|
24 #include <fcntl.h>
|
|
25 #include <unistd.h>
|
|
26
|
|
27 #include "dvd_reader.h"
|
|
28 #include "dvd_input.h"
|
|
29
|
|
30 /* The function pointers that is the exported interface of this file. */
|
|
31 dvd_input_t (*dvdinput_open) (const char *);
|
|
32 int (*dvdinput_close) (dvd_input_t);
|
|
33 int (*dvdinput_seek) (dvd_input_t, int);
|
|
34 int (*dvdinput_title) (dvd_input_t, int);
|
|
35 int (*dvdinput_read) (dvd_input_t, void *, int, int);
|
|
36 char * (*dvdinput_error) (dvd_input_t);
|
|
37
|
|
38 #ifdef HAVE_DVDCSS_DVDCSS_H
|
|
39 /* linking to libdvdcss */
|
|
40 #include <dvdcss/dvdcss.h>
|
|
41 #define DVDcss_open(a) dvdcss_open((char*)(a))
|
|
42 #define DVDcss_close dvdcss_close
|
|
43 #define DVDcss_seek dvdcss_seek
|
|
44 #define DVDcss_title dvdcss_title
|
|
45 #define DVDcss_read dvdcss_read
|
|
46 #define DVDcss_error dvdcss_error
|
|
47 #else
|
|
48 /* dlopening libdvdcss */
|
|
49 #include <dlfcn.h>
|
|
50 typedef struct dvdcss_s *dvdcss_handle;
|
|
51 static dvdcss_handle (*DVDcss_open) (const char *);
|
|
52 static int (*DVDcss_close) (dvdcss_handle);
|
|
53 static int (*DVDcss_seek) (dvdcss_handle, int, int);
|
|
54 static int (*DVDcss_title) (dvdcss_handle, int);
|
|
55 static int (*DVDcss_read) (dvdcss_handle, void *, int, int);
|
|
56 static char * (*DVDcss_error) (dvdcss_handle);
|
|
57 #endif
|
|
58
|
|
59 /* The DVDinput handle, add stuff here for new input methods. */
|
|
60 struct dvd_input_s {
|
|
61 /* libdvdcss handle */
|
|
62 dvdcss_handle dvdcss;
|
|
63
|
|
64 /* dummy file input */
|
|
65 int fd;
|
|
66 };
|
|
67
|
|
68
|
|
69 /**
|
|
70 * initialize and open a DVD device or file.
|
|
71 */
|
|
72 static dvd_input_t css_open(const char *target)
|
|
73 {
|
|
74 dvd_input_t dev;
|
|
75
|
|
76 /* Allocate the handle structure */
|
183
|
77 dev = (dvd_input_t) malloc(sizeof(*dev));
|
168
|
78 if(dev == NULL) {
|
|
79 fprintf(stderr, "libdvdread: Could not allocate memory.\n");
|
|
80 return NULL;
|
|
81 }
|
|
82
|
|
83 /* Really open it with libdvdcss */
|
|
84 dev->dvdcss = DVDcss_open(target);
|
|
85 if(dev->dvdcss == 0) {
|
|
86 fprintf(stderr, "libdvdread: Could not open %s with libdvdcss.\n", target);
|
|
87 free(dev);
|
|
88 return NULL;
|
|
89 }
|
|
90
|
|
91 return dev;
|
|
92 }
|
|
93
|
|
94 /**
|
|
95 * return the last error message
|
|
96 */
|
|
97 static char *css_error(dvd_input_t dev)
|
|
98 {
|
|
99 return DVDcss_error(dev->dvdcss);
|
|
100 }
|
|
101
|
|
102 /**
|
|
103 * seek into the device.
|
|
104 */
|
|
105 static int css_seek(dvd_input_t dev, int blocks)
|
|
106 {
|
|
107 /* DVDINPUT_NOFLAGS should match the DVDCSS_NOFLAGS value. */
|
|
108 return DVDcss_seek(dev->dvdcss, blocks, DVDINPUT_NOFLAGS);
|
|
109 }
|
|
110
|
|
111 /**
|
|
112 * set the block for the begining of a new title (key).
|
|
113 */
|
|
114 static int css_title(dvd_input_t dev, int block)
|
|
115 {
|
|
116 return DVDcss_title(dev->dvdcss, block);
|
|
117 }
|
|
118
|
|
119 /**
|
|
120 * read data from the device.
|
|
121 */
|
|
122 static int css_read(dvd_input_t dev, void *buffer, int blocks, int flags)
|
|
123 {
|
|
124 return DVDcss_read(dev->dvdcss, buffer, blocks, flags);
|
|
125 }
|
|
126
|
|
127 /**
|
|
128 * close the DVD device and clean up the library.
|
|
129 */
|
|
130 static int css_close(dvd_input_t dev)
|
|
131 {
|
|
132 int ret;
|
|
133
|
|
134 ret = DVDcss_close(dev->dvdcss);
|
|
135
|
|
136 if(ret < 0)
|
|
137 return ret;
|
|
138
|
|
139 free(dev);
|
|
140
|
|
141 return 0;
|
|
142 }
|
|
143
|
|
144
|
|
145
|
|
146
|
|
147
|
|
148
|
|
149 /**
|
|
150 * initialize and open a DVD device or file.
|
|
151 */
|
|
152 static dvd_input_t file_open(const char *target)
|
|
153 {
|
|
154 dvd_input_t dev;
|
|
155
|
|
156 /* Allocate the library structure */
|
183
|
157 dev = (dvd_input_t) malloc(sizeof(*dev));
|
168
|
158 if(dev == NULL) {
|
|
159 fprintf(stderr, "libdvdread: Could not allocate memory.\n");
|
|
160 return NULL;
|
|
161 }
|
|
162
|
|
163 /* Open the device */
|
|
164 dev->fd = open(target, O_RDONLY);
|
|
165 if(dev->fd < 0) {
|
|
166 perror("libdvdread: Could not open input");
|
|
167 free(dev);
|
|
168 return NULL;
|
|
169 }
|
|
170
|
|
171 return dev;
|
|
172 }
|
|
173
|
|
174 /**
|
|
175 * return the last error message
|
|
176 */
|
|
177 static char *file_error(dvd_input_t dev)
|
|
178 {
|
|
179 /* use strerror(errno)? */
|
|
180 return (char *)"unknown error";
|
|
181 }
|
|
182
|
|
183 /**
|
|
184 * seek into the device.
|
|
185 */
|
|
186 static int file_seek(dvd_input_t dev, int blocks)
|
|
187 {
|
|
188 off_t pos;
|
|
189
|
|
190 pos = lseek(dev->fd, (off_t)blocks * (off_t)DVD_VIDEO_LB_LEN, SEEK_SET);
|
|
191 if(pos < 0) {
|
|
192 return pos;
|
|
193 }
|
|
194 /* assert pos % DVD_VIDEO_LB_LEN == 0 */
|
|
195 return (int) (pos / DVD_VIDEO_LB_LEN);
|
|
196 }
|
|
197
|
|
198 /**
|
|
199 * set the block for the begining of a new title (key).
|
|
200 */
|
|
201 static int file_title(dvd_input_t dev, int block)
|
|
202 {
|
|
203 return -1;
|
|
204 }
|
|
205
|
|
206 /**
|
|
207 * read data from the device.
|
|
208 */
|
|
209 static int file_read(dvd_input_t dev, void *buffer, int blocks, int flags)
|
|
210 {
|
|
211 size_t len;
|
|
212 ssize_t ret;
|
|
213
|
|
214 len = (size_t)blocks * DVD_VIDEO_LB_LEN;
|
|
215
|
|
216 while(len > 0) {
|
|
217
|
|
218 ret = read(dev->fd, buffer, len);
|
|
219
|
|
220 if(ret < 0) {
|
|
221 /* One of the reads failed, too bad. We won't even bother
|
|
222 * returning the reads that went ok, and as in the posix spec
|
|
223 * the file postition is left unspecified after a failure. */
|
|
224 return ret;
|
|
225 }
|
|
226
|
|
227 if(ret == 0) {
|
|
228 /* Nothing more to read. Return the whole blocks, if any, that we got.
|
|
229 and adjust the file possition back to the previous block boundary. */
|
|
230 size_t bytes = (size_t)blocks * DVD_VIDEO_LB_LEN - len;
|
|
231 off_t over_read = -(bytes % DVD_VIDEO_LB_LEN);
|
|
232 /*off_t pos =*/ lseek(dev->fd, over_read, SEEK_CUR);
|
|
233 /* should have pos % 2048 == 0 */
|
|
234 return (int) (bytes / DVD_VIDEO_LB_LEN);
|
|
235 }
|
|
236
|
|
237 len -= ret;
|
|
238 }
|
|
239
|
|
240 return blocks;
|
|
241 }
|
|
242
|
|
243 /**
|
|
244 * close the DVD device and clean up.
|
|
245 */
|
|
246 static int file_close(dvd_input_t dev)
|
|
247 {
|
|
248 int ret;
|
|
249
|
|
250 ret = close(dev->fd);
|
|
251
|
|
252 if(ret < 0)
|
|
253 return ret;
|
|
254
|
|
255 free(dev);
|
|
256
|
|
257 return 0;
|
|
258 }
|
|
259
|
|
260
|
|
261 /**
|
|
262 * Setup read functions with either libdvdcss or minimal DVD access.
|
|
263 */
|
|
264 int dvdinput_setup(void)
|
|
265 {
|
|
266 void *dvdcss_library = NULL;
|
|
267 char **dvdcss_version = NULL;
|
|
268
|
|
269 #ifdef HAVE_DVDCSS_DVDCSS_H
|
|
270 /* linking to libdvdcss */
|
|
271 dvdcss_library = &dvdcss_library; /* Give it some value != NULL */
|
|
272 /* the DVDcss_* functions have been #defined at the top */
|
|
273 dvdcss_version = &dvdcss_interface_2;
|
|
274
|
|
275 #else
|
|
276 /* dlopening libdvdcss */
|
|
277 dvdcss_library = dlopen("libdvdcss.so.2", RTLD_LAZY);
|
|
278
|
|
279 if(dvdcss_library != NULL) {
|
|
280 #if defined(__OpenBSD__) && !defined(__ELF__)
|
|
281 #define U_S "_"
|
|
282 #else
|
|
283 #define U_S
|
|
284 #endif
|
|
285 DVDcss_open = (dvdcss_handle (*)(const char*))
|
|
286 dlsym(dvdcss_library, U_S "dvdcss_open");
|
|
287 DVDcss_close = (int (*)(dvdcss_handle))
|
|
288 dlsym(dvdcss_library, U_S "dvdcss_close");
|
|
289 DVDcss_title = (int (*)(dvdcss_handle, int))
|
|
290 dlsym(dvdcss_library, U_S "dvdcss_title");
|
|
291 DVDcss_seek = (int (*)(dvdcss_handle, int, int))
|
|
292 dlsym(dvdcss_library, U_S "dvdcss_seek");
|
|
293 DVDcss_read = (int (*)(dvdcss_handle, void*, int, int))
|
|
294 dlsym(dvdcss_library, U_S "dvdcss_read");
|
|
295 DVDcss_error = (char* (*)(dvdcss_handle))
|
|
296 dlsym(dvdcss_library, U_S "dvdcss_error");
|
|
297
|
|
298 dvdcss_version = (char **)dlsym(dvdcss_library, U_S "dvdcss_interface_2");
|
|
299
|
|
300 if(dlsym(dvdcss_library, U_S "dvdcss_crack")) {
|
|
301 fprintf(stderr,
|
|
302 "libdvdread: Old (pre-0.0.2) version of libdvdcss found.\n"
|
|
303 "libdvdread: You should get the latest version from "
|
|
304 "http://www.videolan.org/\n" );
|
|
305 dlclose(dvdcss_library);
|
|
306 dvdcss_library = NULL;
|
|
307 } else if(!DVDcss_open || !DVDcss_close || !DVDcss_title || !DVDcss_seek
|
|
308 || !DVDcss_read || !DVDcss_error || !dvdcss_version) {
|
|
309 fprintf(stderr, "libdvdread: Missing symbols in libdvdcss.so.2, "
|
|
310 "this shouldn't happen !\n");
|
|
311 dlclose(dvdcss_library);
|
|
312 }
|
|
313 }
|
|
314 #endif /* HAVE_DVDCSS_DVDCSS_H */
|
|
315
|
|
316 if(dvdcss_library != NULL) {
|
|
317 /*
|
|
318 char *psz_method = getenv( "DVDCSS_METHOD" );
|
|
319 char *psz_verbose = getenv( "DVDCSS_VERBOSE" );
|
|
320 fprintf(stderr, "DVDCSS_METHOD %s\n", psz_method);
|
|
321 fprintf(stderr, "DVDCSS_VERBOSE %s\n", psz_verbose);
|
|
322 */
|
|
323 fprintf(stderr, "libdvdread: Using libdvdcss version %s for DVD access\n",
|
|
324 *dvdcss_version);
|
|
325
|
|
326 /* libdvdcss wrapper functions */
|
|
327 dvdinput_open = css_open;
|
|
328 dvdinput_close = css_close;
|
|
329 dvdinput_seek = css_seek;
|
|
330 dvdinput_title = css_title;
|
|
331 dvdinput_read = css_read;
|
|
332 dvdinput_error = css_error;
|
|
333 return 1;
|
|
334
|
|
335 } else {
|
|
336 fprintf(stderr, "libdvdread: Encrypted DVD support unavailable.\n");
|
|
337
|
|
338 /* libdvdcss replacement functions */
|
|
339 dvdinput_open = file_open;
|
|
340 dvdinput_close = file_close;
|
|
341 dvdinput_seek = file_seek;
|
|
342 dvdinput_title = file_title;
|
|
343 dvdinput_read = file_read;
|
|
344 dvdinput_error = file_error;
|
|
345 return 0;
|
|
346 }
|
|
347 }
|