annotate dvdread/dvd_input.c @ 282:918fe20358cb src

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