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