Debian Codegraph @

src/dvd_input.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /*
2 * Copyright (C) 2002-2025 Samuel Hocevar <[email protected]>,
3 * Håkan Hjort <[email protected]>
4 * Jean-Baptiste Kempf <[email protected]>
5 * VideoLAN
6 *
7 * This file is part of libdvdread.
8 *
9 * libdvdread 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 * libdvdread 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 along
20 * with libdvdread; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
24 #include "config.h" /* Required for HAVE_DVDCSS_DVDCSS_H */
25 #include <stdio.h> /* fprintf */
26 #include <stdlib.h> /* free */
27 #include <string.h> /* strerror */
28 #include <errno.h>
29 #include <assert.h>
30
31 #if defined(_WIN32)
32 # include <winapifamily.h>
33 # if !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
34 # if !defined(_WIN32_WINNT) || _WIN32_WINNT < 0x602
35 # undef _WIN32_WINNT
36 # define _WIN32_WINNT 0x602 /* LoadPackagedLibrary is Win8 APP Family */
37 # endif
38 # endif
39 #endif
40
41 #ifdef _WIN32
42 #include <windows.h>
43 #endif
44
45 #include "dvdread/dvd_reader.h" /* DVD_VIDEO_LB_LEN */
46 #include "dvd_input.h"
47 #include "logger.h"
48
49
50 /* The function pointers that is the exported interface of this file. */
51 dvd_input_t (*dvdinput_open) (void *, dvd_logger_cb *,
52 const char *, dvd_reader_stream_cb *,
53 dvd_reader_filesystem_h *);
54 int (*dvdinput_close) (dvd_input_t);
55 int (*dvdinput_seek) (dvd_input_t, int);
56 int (*dvdinput_title) (dvd_input_t, int);
57 int (*dvdinput_read) (dvd_input_t, void *, int, int);
58 int (*dvdinput_init) (dvd_input_t, uint8_t* mkb) = NULL;
59
60 #ifdef HAVE_DVDCSS_DVDCSS_H
61 /* linking to libdvdcss */
62 # include <dvdcss/dvdcss.h>
63 # define DVDcss_open_stream(a, b) \
64 dvdcss_open_stream((void*)(a), (dvdcss_stream_cb*)(b))
65 # define DVDcss_open(a) dvdcss_open((char*)(a))
66 # define DVDcss_close dvdcss_close
67 # define DVDcss_seek dvdcss_seek
68 # define DVDcss_read dvdcss_read
69 /* linking cpxm functions*/
70 #ifdef HAVE_DVDCSS_DVDCPXM_H
71 # include <dvdcss/dvdcpxm.h>
72 # define DVDcpxm_read dvdcpxm_read
73 # define DVDcpxm_init dvdcpxm_init
74 #endif
75 #else
76
77 /* dlopening libdvdcss */
78 # if defined(HAVE_DLFCN_H)
79 # include <dlfcn.h>
80 # else
81 # if defined(_WIN32)
82 # define dlsym(h, name) (void*)GetProcAddress(h, name)
83 # define dlclose(h) FreeLibrary(h)
84 # endif
85 # endif
86
87 typedef struct dvdcss_s *dvdcss_t;
88 typedef struct dvdcss_stream_cb dvdcss_stream_cb;
89 static dvdcss_t (*DVDcss_open_stream) (void *, dvdcss_stream_cb *);
90 static dvdcss_t (*DVDcss_open) (const char *);
91 static int (*DVDcss_close) (dvdcss_t);
92 static int (*DVDcss_seek) (dvdcss_t, int, int);
93 static int (*DVDcss_read) (dvdcss_t, void *, int, int);
94 #define DVDCSS_SEEK_KEY (1 << 1)
95 /* function to setup the cpxm struct */
96 #ifdef HAVE_DVDCSS_DVDCPXM_H
97 static int (*DVDcpxm_read) (dvdcss_t, void *, int, int);
98 static int (*DVDcpxm_init) (dvdcss_t, uint8_t* p_mkb);
99 #endif
100
101 #endif
102
103 /* The DVDinput handle, add stuff here for new input methods.
104 * NOTE: All members of this structure must be initialized in dvd_input_New
105 */
106 struct dvd_input_s {
107 /* libdvdcss handle */
108 dvdcss_t dvdcss;
109 /* */
110 void *priv;
111 dvd_logger_cb *logcb;
112 off_t ipos;
113
114 /* This variable keeps track of the current files stream_type,
115 * and in turn determined the decryption method to use */
116 /* DVD_A -> AOB */
117 /* DVD_V -> VOB */
118 /* DVD_VR -> VRO with cprm */
119 dvd_type_t stream_type;
120
121 /* stream input */
122 dvd_reader_stream_cb *stream_cb;
123 /* file input */
124 dvd_reader_filesystem_h *fs;
125 void *file;
126 };
127
128 static dvd_input_t dvd_input_New(void *priv, dvd_logger_cb *logcb)
129 {
130 dvd_input_t dev = calloc(1, sizeof(*dev));
131 if(dev)
132 {
133 dev->priv = priv;
134 dev->logcb = logcb;
135 dev->ipos = 0;
136 dev->stream_type = DVD_V;
137
138 /* Initialize all inputs to safe defaults */
139 dev->dvdcss = NULL;
140 dev->stream_cb = NULL;
141 dev->fs = NULL;
142 dev->file = NULL;
143 }
144 return dev;
145 }
146
147 /**
148 * initialize and open a DVD (device or file or stream_cb)
149 */
150 static dvd_input_t css_open(void *priv, dvd_logger_cb *logcb,
151 const char *target,
152 dvd_reader_stream_cb *stream_cb,
153 dvd_reader_filesystem_h *fs UNUSED)
154 {
155 dvd_input_t dev;
156
157 /* Allocate the handle structure */
158 dev = dvd_input_New(priv, logcb);
159 if(dev == NULL) {
160 DVDReadLog(priv, logcb, DVD_LOGGER_LEVEL_ERROR,
161 "Could not allocate memory.");
162 return NULL;
163 }
164
165 /* Really open it with libdvdcss */
166 if(target)
167 dev->dvdcss = DVDcss_open(target);
168 else if(priv && stream_cb) {
169 #ifdef HAVE_DVDCSS_DVDCSS_H
170 dev->dvdcss = DVDcss_open_stream(priv, (dvdcss_stream_cb *)stream_cb);
171 #else
172 dev->dvdcss = DVDcss_open_stream ?
173 DVDcss_open_stream(priv, (dvdcss_stream_cb *)stream_cb) :
174 NULL;
175 #endif
176 }
177 if(dev->dvdcss == NULL) {
178 DVDReadLog(priv, logcb, DVD_LOGGER_LEVEL_ERROR,
179 "Could not open %s with libdvdcss.", target);
180 free(dev);
181 return NULL;
182 }
183
184 return dev;
185 }
186
187 /**
188 * seek into the device.
189 */
190 static int css_seek(dvd_input_t dev, int blocks)
191 {
192 /* DVDINPUT_NOFLAGS should match the DVDCSS_NOFLAGS value. */
193 return DVDcss_seek(dev->dvdcss, blocks, DVDINPUT_NOFLAGS);
194 }
195
196 /**
197 * set the block for the beginning of a new title (key).
198 */
199 static int css_title(dvd_input_t dev, int block)
200 {
201 #ifdef HAVE_DVDCSS_DVDCPXM_H
202 if (dev->stream_type == DVD_A || dev->stream_type == DVD_VR)
203 return DVDcss_seek(dev->dvdcss, block, DVDINPUT_NOFLAGS);
204 else
205 #endif
206 return DVDcss_seek(dev->dvdcss, block, DVDCSS_SEEK_KEY);
207 }
208
209 /**
210 * read data from the device.
211 */
212 static int css_read(dvd_input_t dev, void *buffer, int blocks, int flags)
213 {
214 #ifdef HAVE_DVDCSS_DVDCPXM_H
215 if (dev->stream_type == DVD_A || dev->stream_type == DVD_VR)
216 return DVDcpxm_read(dev->dvdcss, buffer, blocks, flags);
217 else
218 #endif
219 return DVDcss_read(dev->dvdcss, buffer, blocks, flags);
220 }
221
222 /**
223 * close the DVD device and clean up the library.
224 */
225 static int css_close(dvd_input_t dev)
226 {
227 int ret;
228
229 ret = DVDcss_close(dev->dvdcss);
230
231 free(dev);
232
233 return ret;
234 }
235
236 #ifdef HAVE_DVDCSS_DVDCPXM_H
237 /**
238 * Setup Datastructure.
239 */
240 static int cpxm_init(dvd_input_t dev, uint8_t* p_mkb )
241 {
242 return DVDcpxm_init(dev->dvdcss, p_mkb);
243 }
244 #endif /* CPXM */
245
246 /**
247 * initialize and open a DVD device or file.
248 */
249 static dvd_input_t file_open(void *priv, dvd_logger_cb *logcb,
250 const char *target,
251 dvd_reader_stream_cb *stream_cb,
252 dvd_reader_filesystem_h *fs)
253 {
254 dvd_input_t dev;
255
256 /* Allocate the library structure */
257 dev = dvd_input_New(priv, logcb);
258 if(dev == NULL) {
259 DVDReadLog(priv, logcb, DVD_LOGGER_LEVEL_ERROR, "Could not allocate memory.");
260 return NULL;
261 }
262
263 /* Initialize with stream callback if it is specified */
264 if (stream_cb) {
265 if (!stream_cb->pf_read || !stream_cb->pf_seek) {
266 DVDReadLog(priv, logcb, DVD_LOGGER_LEVEL_ERROR,
267 "Stream callback provided but lacks of pf_read or pf_seek methods.");
268 free(dev);
269 return NULL;
270 }
271 dev->stream_cb = stream_cb;
272 return dev;
273 }
274
275 /* Open the device */
276 if(target == NULL) {
277 free(dev);
278 return NULL;
279 }
280 dev->fs = fs;
281 dev->file = fs->file_open(fs, target);
282 if(!dev->file) {
283 char buf[256];
284 #if defined(HAVE_STRERROR_R) && defined(HAVE_DECL_STRERROR_R)
285 #ifdef STRERROR_R_CHAR_P
286 *buf=0;
287 if(strerror_r(errno, buf, 256) == NULL)
288 *buf=0;
289 #else
290 if(strerror_r(errno, buf, 256) != 0)
291 *buf=0;
292 #endif
293 #else
294 #if defined(HAVE_STRERROR_S)
295 if(strerror_s(buf, 256, errno) != 0)
296 *buf=0;
297 #else
298 *buf=0;
299 #endif
300 #endif
301 DVDReadLog(priv, logcb, DVD_LOGGER_LEVEL_ERROR,
302 "Could not open input: %s", buf);
303 free(dev);
304 return NULL;
305 }
306
307 return dev;
308 }
309
310 /**
311 * seek into the device.
312 */
313 static int file_seek(dvd_input_t dev, int blocks)
314 {
315 off64_t pos = -1;
316
317 if(dev->ipos == blocks)
318 {
319 /* We are already in position */
320 return blocks;
321 }
322
323 if (dev->stream_cb) {
324 /* Returns 0 on successful completion and -1 on error */
325 pos = dev->stream_cb->pf_seek(dev->priv, (off_t)blocks * (off_t)DVD_VIDEO_LB_LEN);
326
327 if (!pos) {
328 dev->ipos = blocks;
329 }
330 } else {
331 /* Returns the offset from the start of the file or -1 on error */
332 pos = dev->fs->file_seek(dev->file, (off64_t)blocks * (off64_t)DVD_VIDEO_LB_LEN, SEEK_SET);
333
334 if (pos >= 0) {
335 dev->ipos = pos / DVD_VIDEO_LB_LEN;
336 }
337 }
338
339 if(pos < 0) {
340 return pos;
341 }
342
343 return (int) dev->ipos;
344 }
345
346 /**
347 * set the block for the beginning of a new title (key).
348 */
349 static int file_title(dvd_input_t dev UNUSED, int block UNUSED)
350 {
351 return -1;
352 }
353
354 /**
355 * read data from the device.
356 */
357 static int file_read(dvd_input_t dev, void *buffer, int blocks,
358 int flags UNUSED)
359 {
360 size_t len, bytes, blocks_read;
361
362 len = (size_t)blocks * DVD_VIDEO_LB_LEN;
363 bytes = 0;
364 blocks_read = 0;
365
366 while(len > 0) {
367 ssize_t ret = -1;
368
369 /* Perform read based on the input type */
370 if (dev->stream_cb) {
371 /* Returns the number of bytes read or -1 on error */
372 ret = dev->stream_cb->pf_read(dev->priv, ((char*)buffer) + bytes, len);
373 } else {
374 /* Returns the number of bytes read or -1 on error */
375 ret = dev->fs->file_read(dev->file, ((char*)buffer) + bytes, len);
376 }
377
378 if(ret < 0) {
379 /* One of the reads failed, too bad. We won't even bother
380 * returning the reads that went OK, and as in the POSIX spec
381 * the file position is left unspecified after a failure. */
382 dev->ipos = -1;
383 return ret;
384 }
385
386 if(ret == 0) {
387 /* Nothing more to read. Return all of the whole blocks, if any.
388 * Adjust the file position back to the previous block boundary. */
389 ret = file_seek(dev, dev->ipos + blocks_read);
390 if(ret < 0)
391 return ret;
392
393 return (int) blocks_read;
394 }
395
396 len -= ret;
397 bytes += ret;
398 blocks_read = bytes / DVD_VIDEO_LB_LEN;
399 }
400
401 dev->ipos += blocks_read;
402 return blocks;
403 }
404
405 /**
406 * close the DVD device and clean up.
407 */
408 static int file_close(dvd_input_t dev)
409 {
410 int ret = 0;
411
412 /* close file if it was open */
413
414 if (dev->file) {
415 ret = dev->fs->file_close(dev->file);
416 }
417
418 free(dev);
419
420 return ret;
421 }
422
423 /**
424 * Setup read functions with either libdvdcss or minimal DVD access.
425 */
426 int dvdinput_setup(void *priv, dvd_logger_cb *logcb, dvd_type_t dvda_flag)
427 {
428 void *dvdcss_library = NULL;
429
430 #ifdef HAVE_DVDCSS_DVDCPXM_H
431 /* linking to libdvdcss */
432 dvdcss_library = &dvdcss_library; /* Give it some value != NULL */
433 #elif defined( HAVE_DVDCSS_DVDCSS_H )
434 if (dvda_flag == DVD_V )
435 dvdcss_library = &dvdcss_library; /* Give it some value != NULL */
436 #else
437 /* dlopening libdvdcss */
438
439 #ifdef __APPLE__
440 #define CSS_LIB "libdvdcss.2.dylib"
441 #elif defined(_WIN32)
442 #define CSS_LIB "libdvdcss-2.dll"
443 #elif defined(__OS2__)
444 #define CSS_LIB "dvdcss2.dll"
445 #elif defined(__OpenBSD__)
446 #define CSS_LIB "libdvdcss.so"
447 #else
448 #define CSS_LIB "libdvdcss.so.2"
449 #endif
450
451 #define WIDEN_(x) L ## x
452 #define WIDEN(x) WIDEN_(x)
453
454 #ifdef __OS2__
455 #define U_S "_"
456 #else
457 #define U_S
458 #endif
459
460 /* Actually dlopen */
461 #if defined(_WIN32)
462 # if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
463 UINT em;
464 /* First assume the dso/dll's required by -this- dso are sitting in the
465 * same path or can be found in the usual places. Failing that, let's
466 * let that dso look in the apache root.
467 */
468 em = SetErrorMode(SEM_FAILCRITICALERRORS);
469 dvdcss_library = LoadLibraryExA(CSS_LIB, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
470 if (!dvdcss_library)
471 {
472 SetLastError(0); // clear the last error
473 dvdcss_library = LoadLibraryExA(CSS_LIB, NULL, 0);
474 }
475 SetErrorMode(em);
476 SetLastError(0); // clear the last error
477 # else /* WINAPI_PARTITION_DESKTOP */
478 dvdcss_library = LoadPackagedLibrary(WIDEN(CSS_LIB), 0);
479 # endif /* WINAPI_PARTITION_DESKTOP */
480 #else
481 dvdcss_library = dlopen(CSS_LIB, RTLD_LAZY);
482 #endif
483
484 /* Locate the functions, DVD_V or DVD_A */
485 if(dvdcss_library != NULL) {
486 /* functions should have the same template*/
487 /* hybrid discs encrypt video tracks with css*/
488 DVDcss_open_stream = (dvdcss_t (*)(void *, dvdcss_stream_cb *))
489 dlsym(dvdcss_library, U_S "dvdcss_open_stream");
490 DVDcss_open = (dvdcss_t (*)(const char*))
491 dlsym(dvdcss_library, U_S "dvdcss_open");
492 DVDcss_close = (int (*)(dvdcss_t))
493 dlsym(dvdcss_library, U_S "dvdcss_close");
494 DVDcss_seek = (int (*)(dvdcss_t, int, int))
495 dlsym(dvdcss_library, U_S "dvdcss_seek");
496 DVDcss_read = (int (*)(dvdcss_t, void*, int, int))
497 dlsym(dvdcss_library, U_S "dvdcss_read");
498 if (dvda_flag == DVD_A || dvda_flag == DVD_VR ) {
499 #ifdef HAVE_DVDCSS_DVDCPXM_H
500 DVDcpxm_read = (int (*)(dvdcss_t, void*, int, int))
501 dlsym(dvdcss_library, U_S "dvdcpxm_read");
502 DVDcpxm_init = (int (*)(dvdcss_t, uint8_t *p_mkb))
503 dlsym(dvdcss_library, U_S "dvdcpxm_init");
504 #else
505 DVDReadLog(priv, logcb, DVD_LOGGER_LEVEL_ERROR,
506 "DVD-Audio headers not present, update the DVDCSS library");
507 dlclose(dvdcss_library);
508 dvdcss_library = NULL;
509 #endif
510 }
511 if(dlsym(dvdcss_library, U_S "dvdcss_crack")) {
512 DVDReadLog(priv, logcb, DVD_LOGGER_LEVEL_ERROR,
513 "Old (pre-0.0.2) version of libdvdcss found. "
514 "libdvdread: You should get the latest version from "
515 "https://www.videolan.org/" );
516 } else if(!DVDcss_open || !DVDcss_close || !DVDcss_seek || !DVDcss_read) {
517 DVDReadLog(priv, logcb, DVD_LOGGER_LEVEL_ERROR,
518 "Missing symbols in %s, "
519 "this shouldn't happen !", CSS_LIB);
520 dlclose(dvdcss_library);
521 dvdcss_library = NULL;
522 #ifdef HAVE_DVDCSS_DVDCPXM_H
523 } else if( (dvda_flag != DVD_V) && (!DVDcpxm_read || !DVDcpxm_init) ) {
524 DVDReadLog(priv, logcb, DVD_LOGGER_LEVEL_ERROR,
525 "Missing symbols for DVD-Audio in %s, "
526 "this shouldn't happen !", CSS_LIB);
527 dlclose(dvdcss_library);
528 dvdcss_library = NULL;
529 #endif
530 }
531 }
532 #endif /* HAVE_DVDCSS_DVDCSS_H */
533
534 if(dvdcss_library != NULL) {
535 /*
536 char *psz_method = getenv( "DVDCSS_METHOD" );
537 char *psz_verbose = getenv( "DVDCSS_VERBOSE" );
538 fprintf(stderr, "DVDCSS_METHOD %s\n", psz_method);
539 fprintf(stderr, "DVDCSS_VERBOSE %s\n", psz_verbose);
540 */
541
542 /* libdvdcss wrapper functions */
543 dvdinput_open = css_open;
544 dvdinput_close = css_close;
545 dvdinput_seek = css_seek;
546 dvdinput_title = css_title;
547 dvdinput_read = css_read;
548
549 /* additional setup function that must be run for DVD_A decryption */
550 if (dvda_flag == DVD_A || dvda_flag == DVD_VR) {
551 #ifdef HAVE_DVDCSS_DVDCPXM_H
552 dvdinput_init = cpxm_init;
553 #else
554 assert(!"libdvdcss compiled without DVD-Audio (CPXM) support");
555 #endif
556 }
557 return 1;
558
559 } else {
560 DVDReadLog(priv, logcb, DVD_LOGGER_LEVEL_WARN,
•561 "Encrypted DVD support unavailable."
562 "************************************************\n"
563 "** **\n"
564 "** No css library available. See **\n"
565 "** /usr/share/doc/libdvdread8t64/README.css **\n"
566 "** for more information. **\n"
567 "** **\n"
568 "************************************************\n");
569
570 dvdinput_setup_builtin(priv, logcb);
571 return 0;
572 }
573 }
574
575 /**
576 * Setup read functions with the builtin libdvdread implementation (minimal DVD access without css).
577 */
578 void dvdinput_setup_builtin(void *priv, dvd_logger_cb *logcb)
579 {
580 DVDReadLog(priv, logcb, DVD_LOGGER_LEVEL_INFO,
581 "Setting up builtin libdvdread implementation");
582
583 /* libdvdcss replacement functions */
584 dvdinput_open = file_open;
585 dvdinput_close = file_close;
586 dvdinput_seek = file_seek;
587 dvdinput_title = file_title;
588 dvdinput_read = file_read;
589 }
590
591 /* change the stream type, this will set the decryption method */
592 void dvdinput_set_stream(dvd_input_t dev, dvd_type_t decryption_type)
593 {
594 dev->stream_type = decryption_type;
595 }