~ubuntu-branches/ubuntu/raring/libdvdread/raring-proposed

« back to all changes in this revision

Viewing changes to .pc/07-segfault.patch/src/dvd_reader.c

  • Committer: Package Import Robot
  • Author(s): Vibhav Pant
  • Date: 2012-12-15 17:06:42 UTC
  • mfrom: (1.3.2) (3.2.15 sid)
  • Revision ID: package-import@ubuntu.com-20121215170642-q0h4cc4r2mchkgcl
Tags: 4.2.0+20121016-1ubuntu1
* Merge from Debian unstable (LP: #1090692).  Remaining changes:
  - Re-add missing install-css.sh.
  - debian/control: add Suggests for install-css.sh on debhelper,
    fakeroot, and build-essential.
  - debian/rules: install install-css.sh, leave perms executable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2001-2004 Billy Biggs <vektor@dumbterm.net>,
 
3
 *                         Håkan Hjort <d95hjort@dtek.chalmers.se>,
 
4
 *                         Björn Englund <d4bjorn@dtek.chalmers.se>
 
5
 *
 
6
 * This file is part of libdvdread.
 
7
 *
 
8
 * libdvdread is free software; you can redistribute it and/or modify
 
9
 * it under the terms of the GNU General Public License as published by
 
10
 * the Free Software Foundation; either version 2 of the License, or
 
11
 * (at your option) any later version.
 
12
 *
 
13
 * libdvdread is distributed in the hope that it will be useful,
 
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
 * GNU General Public License for more details.
 
17
 *
 
18
 * You should have received a copy of the GNU General Public License along
 
19
 * with libdvdread; if not, write to the Free Software Foundation, Inc.,
 
20
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
21
 */
 
22
 
 
23
#include <sys/types.h>
 
24
#include <sys/stat.h>
 
25
#include <sys/time.h> /* For the timing of dvdcss_title crack. */
 
26
#include <fcntl.h>
 
27
#include <stdlib.h>
 
28
#include <stdio.h>
 
29
#include <errno.h>
 
30
#include <string.h>
 
31
#include <ctype.h>
 
32
#define _GNU_SOURCE
 
33
#include <unistd.h>
 
34
#include <limits.h>
 
35
#include <dirent.h>
 
36
 
 
37
/* misc win32 helpers */
 
38
#ifdef WIN32
 
39
#include <windows.h>
 
40
#ifndef HAVE_GETTIMEOFDAY
 
41
/* replacement gettimeofday implementation */
 
42
#include <sys/timeb.h>
 
43
static inline int _private_gettimeofday( struct timeval *tv, void *tz )
 
44
{
 
45
  struct timeb t;
 
46
  ftime( &t );
 
47
  tv->tv_sec = t.time;
 
48
  tv->tv_usec = t.millitm * 1000;
 
49
  return 0;
 
50
}
 
51
#define gettimeofday(TV, TZ) _private_gettimeofday((TV), (TZ))
 
52
#endif
 
53
#include <io.h> /* read() */
 
54
#define lseek64 _lseeki64
 
55
#endif
 
56
 
 
57
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__bsdi__) || defined(__APPLE__)
 
58
#define SYS_BSD 1
 
59
#endif
 
60
 
 
61
#if defined(__sun)
 
62
#include <sys/mnttab.h>
 
63
#elif defined(__APPLE__)
 
64
#include <sys/param.h>
 
65
#include <sys/ucred.h>
 
66
#include <sys/mount.h>
 
67
#elif defined(SYS_BSD)
 
68
#include <fstab.h>
 
69
#elif defined(__linux__)
 
70
#include <mntent.h>
 
71
#include <paths.h>
 
72
#endif
 
73
 
 
74
#include "dvdread/dvd_udf.h"
 
75
#include "dvd_input.h"
 
76
#include "dvdread/dvd_reader.h"
 
77
#include "md5.h"
 
78
 
 
79
#define DEFAULT_UDF_CACHE_LEVEL 1
 
80
 
 
81
struct dvd_reader_s {
 
82
  /* Basic information. */
 
83
  int isImageFile;
 
84
 
 
85
  /* Hack for keeping track of the css status.
 
86
   * 0: no css, 1: perhaps (need init of keys), 2: have done init */
 
87
  int css_state;
 
88
  int css_title; /* Last title that we have called dvdinpute_title for. */
 
89
 
 
90
  /* Information required for an image file. */
 
91
  dvd_input_t dev;
 
92
 
 
93
  /* Information required for a directory path drive. */
 
94
  char *path_root;
 
95
 
 
96
  /* Filesystem cache */
 
97
  int udfcache_level; /* 0 - turned off, 1 - on */
 
98
  void *udfcache;
 
99
};
 
100
 
 
101
#define TITLES_MAX 9
 
102
 
 
103
struct dvd_file_s {
 
104
  /* Basic information. */
 
105
  dvd_reader_t *dvd;
 
106
 
 
107
  /* Hack for selecting the right css title. */
 
108
  int css_title;
 
109
 
 
110
  /* Information required for an image file. */
 
111
  uint32_t lb_start;
 
112
  uint32_t seek_pos;
 
113
 
 
114
  /* Information required for a directory path drive. */
 
115
  size_t title_sizes[ TITLES_MAX ];
 
116
  dvd_input_t title_devs[ TITLES_MAX ];
 
117
 
 
118
  /* Calculated at open-time, size in blocks. */
 
119
  ssize_t filesize;
 
120
};
 
121
 
 
122
int UDFReadBlocksRaw( dvd_reader_t *device, uint32_t lb_number,
 
123
                      size_t block_count, unsigned char *data,
 
124
                      int encrypted );
 
125
 
 
126
/**
 
127
 * Set the level of caching on udf
 
128
 * level = 0 (no caching)
 
129
 * level = 1 (caching filesystem info)
 
130
 */
 
131
int DVDUDFCacheLevel(dvd_reader_t *device, int level)
 
132
{
 
133
  struct dvd_reader_s *dev = (struct dvd_reader_s *)device;
 
134
 
 
135
  if(level > 0) {
 
136
    level = 1;
 
137
  } else if(level < 0) {
 
138
    return dev->udfcache_level;
 
139
  }
 
140
 
 
141
  dev->udfcache_level = level;
 
142
 
 
143
  return level;
 
144
}
 
145
 
 
146
void *GetUDFCacheHandle(dvd_reader_t *device)
 
147
{
 
148
  struct dvd_reader_s *dev = (struct dvd_reader_s *)device;
 
149
 
 
150
  return dev->udfcache;
 
151
}
 
152
 
 
153
void SetUDFCacheHandle(dvd_reader_t *device, void *cache)
 
154
{
 
155
  struct dvd_reader_s *dev = (struct dvd_reader_s *)device;
 
156
 
 
157
  dev->udfcache = cache;
 
158
}
 
159
 
 
160
 
 
161
 
 
162
/* Loop over all titles and call dvdcss_title to crack the keys. */
 
163
static int initAllCSSKeys( dvd_reader_t *dvd )
 
164
{
 
165
  struct timeval all_s, all_e;
 
166
  struct timeval t_s, t_e;
 
167
  char filename[ MAX_UDF_FILE_NAME_LEN ];
 
168
  uint32_t start, len;
 
169
  int title;
 
170
 
 
171
  char *nokeys_str = getenv("DVDREAD_NOKEYS");
 
172
  if(nokeys_str != NULL)
 
173
    return 0;
 
174
 
 
175
  fprintf( stderr, "\n" );
 
176
  fprintf( stderr, "libdvdread: Attempting to retrieve all CSS keys\n" );
 
177
  fprintf( stderr, "libdvdread: This can take a _long_ time, "
 
178
           "please be patient\n\n" );
 
179
  gettimeofday(&all_s, NULL);
 
180
 
 
181
  for( title = 0; title < 100; title++ ) {
 
182
    gettimeofday( &t_s, NULL );
 
183
    if( title == 0 ) {
 
184
      sprintf( filename, "/VIDEO_TS/VIDEO_TS.VOB" );
 
185
    } else {
 
186
      sprintf( filename, "/VIDEO_TS/VTS_%02d_%d.VOB", title, 0 );
 
187
    }
 
188
    start = UDFFindFile( dvd, filename, &len );
 
189
    if( start != 0 && len != 0 ) {
 
190
      /* Perform CSS key cracking for this title. */
 
191
      fprintf( stderr, "libdvdread: Get key for %s at 0x%08x\n",
 
192
               filename, start );
 
193
      if( dvdinput_title( dvd->dev, (int)start ) < 0 ) {
 
194
        fprintf( stderr, "libdvdread: Error cracking CSS key for %s (0x%08x)\n", filename, start);
 
195
      }
 
196
      gettimeofday( &t_e, NULL );
 
197
      fprintf( stderr, "libdvdread: Elapsed time %ld\n",
 
198
               (long int) t_e.tv_sec - t_s.tv_sec );
 
199
    }
 
200
 
 
201
    if( title == 0 ) continue;
 
202
 
 
203
    gettimeofday( &t_s, NULL );
 
204
    sprintf( filename, "/VIDEO_TS/VTS_%02d_%d.VOB", title, 1 );
 
205
    start = UDFFindFile( dvd, filename, &len );
 
206
    if( start == 0 || len == 0 ) break;
 
207
 
 
208
    /* Perform CSS key cracking for this title. */
 
209
    fprintf( stderr, "libdvdread: Get key for %s at 0x%08x\n",
 
210
             filename, start );
 
211
    if( dvdinput_title( dvd->dev, (int)start ) < 0 ) {
 
212
      fprintf( stderr, "libdvdread: Error cracking CSS key for %s (0x%08x)!!\n", filename, start);
 
213
    }
 
214
    gettimeofday( &t_e, NULL );
 
215
    fprintf( stderr, "libdvdread: Elapsed time %ld\n",
 
216
             (long int) t_e.tv_sec - t_s.tv_sec );
 
217
  }
 
218
  title--;
 
219
 
 
220
  fprintf( stderr, "libdvdread: Found %d VTS's\n", title );
 
221
  gettimeofday(&all_e, NULL);
 
222
  fprintf( stderr, "libdvdread: Elapsed time %ld\n",
 
223
           (long int) all_e.tv_sec - all_s.tv_sec );
 
224
 
 
225
  return 0;
 
226
}
 
227
 
 
228
 
 
229
 
 
230
/**
 
231
 * Open a DVD image or block device file.
 
232
 */
 
233
static dvd_reader_t *DVDOpenImageFile( const char *location, int have_css )
 
234
{
 
235
  dvd_reader_t *dvd;
 
236
  dvd_input_t dev;
 
237
 
 
238
  dev = dvdinput_open( location );
 
239
  if( !dev ) {
 
240
    fprintf( stderr, "libdvdread: Can't open %s for reading\n", location );
 
241
    return NULL;
 
242
  }
 
243
 
 
244
  dvd = (dvd_reader_t *) malloc( sizeof( dvd_reader_t ) );
 
245
  if( !dvd ) {
 
246
    dvdinput_close(dev);
 
247
    return NULL;
 
248
  }
 
249
  dvd->isImageFile = 1;
 
250
  dvd->dev = dev;
 
251
  dvd->path_root = NULL;
 
252
 
 
253
  dvd->udfcache_level = DEFAULT_UDF_CACHE_LEVEL;
 
254
  dvd->udfcache = NULL;
 
255
 
 
256
  if( have_css ) {
 
257
    /* Only if DVDCSS_METHOD = title, a bit if it's disc or if
 
258
     * DVDCSS_METHOD = key but region mismatch. Unfortunately we
 
259
     * don't have that information. */
 
260
 
 
261
    dvd->css_state = 1; /* Need key init. */
 
262
  }
 
263
  dvd->css_title = 0;
 
264
 
 
265
  return dvd;
 
266
}
 
267
 
 
268
static dvd_reader_t *DVDOpenPath( const char *path_root )
 
269
{
 
270
  dvd_reader_t *dvd;
 
271
 
 
272
  dvd = (dvd_reader_t *) malloc( sizeof( dvd_reader_t ) );
 
273
  if( !dvd ) return NULL;
 
274
  dvd->isImageFile = 0;
 
275
  dvd->dev = 0;
 
276
  dvd->path_root = strdup( path_root );
 
277
  if(!dvd->path_root) {
 
278
    free(dvd);
 
279
    return 0;
 
280
  }
 
281
  dvd->udfcache_level = DEFAULT_UDF_CACHE_LEVEL;
 
282
  dvd->udfcache = NULL;
 
283
 
 
284
  dvd->css_state = 0; /* Only used in the UDF path */
 
285
  dvd->css_title = 0; /* Only matters in the UDF path */
 
286
 
 
287
  return dvd;
 
288
}
 
289
 
 
290
#if defined(__sun)
 
291
/* /dev/rdsk/c0t6d0s0 (link to /devices/...)
 
292
   /vol/dev/rdsk/c0t6d0/??
 
293
   /vol/rdsk/<name> */
 
294
static char *sun_block2char( const char *path )
 
295
{
 
296
  char *new_path;
 
297
 
 
298
  /* Must contain "/dsk/" */
 
299
  if( !strstr( path, "/dsk/" ) ) return (char *) strdup( path );
 
300
 
 
301
  /* Replace "/dsk/" with "/rdsk/" */
 
302
  new_path = malloc( strlen(path) + 2 );
 
303
  strcpy( new_path, path );
 
304
  strcpy( strstr( new_path, "/dsk/" ), "" );
 
305
  strcat( new_path, "/rdsk/" );
 
306
  strcat( new_path, strstr( path, "/dsk/" ) + strlen( "/dsk/" ) );
 
307
 
 
308
  return new_path;
 
309
}
 
310
#endif
 
311
 
 
312
#if defined(SYS_BSD)
 
313
/* FreeBSD /dev/(r)(a)cd0c (a is for atapi), recommended to _not_ use r
 
314
   update: FreeBSD and DragonFly no longer uses the prefix so don't add it.
 
315
   OpenBSD /dev/rcd0c, it needs to be the raw device
 
316
   NetBSD  /dev/rcd0[d|c|..] d for x86, c (for non x86), perhaps others
 
317
   Darwin  /dev/rdisk0,  it needs to be the raw device
 
318
   BSD/OS  /dev/sr0c (if not mounted) or /dev/rsr0c ('c' any letter will do)
 
319
   returns a string allocated with strdup. It should be freed when no longer
 
320
   used. */
 
321
static char *bsd_block2char( const char *path )
 
322
{
 
323
#if defined(__FreeBSD__) || defined(__DragonFly__)
 
324
  return (char *) strdup( path );
 
325
#else
 
326
  char *new_path;
 
327
 
 
328
  /* If it doesn't start with "/dev/" or does start with "/dev/r" exit */
 
329
  if( strncmp( path, "/dev/",  5 ) || !strncmp( path, "/dev/r", 6 ) )
 
330
    return (char *) strdup( path );
 
331
 
 
332
  /* Replace "/dev/" with "/dev/r" */
 
333
  new_path = malloc( strlen(path) + 2 );
 
334
  strcpy( new_path, "/dev/r" );
 
335
  strcat( new_path, path + strlen( "/dev/" ) );
 
336
 
 
337
  return new_path;
 
338
#endif /* __FreeBSD__ || __DragonFly__ */
 
339
}
 
340
#endif
 
341
 
 
342
 
 
343
dvd_reader_t *DVDOpen( const char *ppath )
 
344
{
 
345
  struct stat fileinfo;
 
346
  int ret, have_css, retval, cdir = -1;
 
347
  dvd_reader_t *ret_val = NULL;
 
348
  char *dev_name = NULL;
 
349
  char *path = NULL, *new_path = NULL, *path_copy = NULL;
 
350
 
 
351
#if defined(_WIN32) || defined(__OS2__)
 
352
      int len;
 
353
#endif
 
354
 
 
355
  if( ppath == NULL )
 
356
    goto DVDOpen_error;
 
357
 
 
358
      path = strdup(ppath);
 
359
  if( path == NULL )
 
360
    goto DVDOpen_error;
 
361
 
 
362
  /* Try to open libdvdcss or fall back to standard functions */
 
363
  have_css = dvdinput_setup();
 
364
 
 
365
#if defined(_WIN32) || defined(__OS2__)
 
366
  /* Strip off the trailing \ if it is not a drive */
 
367
  len = strlen(path);
 
368
  if ((len > 1) &&
 
369
      (path[len - 1] == '\\')  &&
 
370
      (path[len - 2] != ':'))
 
371
  {
 
372
    path[len-1] = '\0';
 
373
  }
 
374
#endif
 
375
 
 
376
  ret = stat( path, &fileinfo );
 
377
 
 
378
  if( ret < 0 ) {
 
379
 
 
380
    /* maybe "host:port" url? try opening it with acCeSS library */
 
381
    if( strchr(path,':') ) {
 
382
                    ret_val = DVDOpenImageFile( path, have_css );
 
383
                    free(path);
 
384
            return ret_val;
 
385
    }
 
386
 
 
387
    /* If we can't stat the file, give up */
 
388
    fprintf( stderr, "libdvdread: Can't stat %s\n", path );
 
389
    perror("");
 
390
    goto DVDOpen_error;
 
391
  }
 
392
 
 
393
  /* First check if this is a block/char device or a file*/
 
394
  if( S_ISBLK( fileinfo.st_mode ) ||
 
395
      S_ISCHR( fileinfo.st_mode ) ||
 
396
      S_ISREG( fileinfo.st_mode ) ) {
 
397
 
 
398
    /**
 
399
     * Block devices and regular files are assumed to be DVD-Video images.
 
400
     */
 
401
    dvd_reader_t *dvd = NULL;
 
402
#if defined(__sun)
 
403
    dev_name = sun_block2char( path );
 
404
#elif defined(SYS_BSD)
 
405
    dev_name = bsd_block2char( path );
 
406
#else
 
407
    dev_name = strdup( path );
 
408
#endif
 
409
    dvd = DVDOpenImageFile( dev_name, have_css );
 
410
    free( dev_name );
 
411
    free(path);
 
412
    return dvd;
 
413
  } else if( S_ISDIR( fileinfo.st_mode ) ) {
 
414
    dvd_reader_t *auth_drive = 0;
 
415
#if defined(SYS_BSD)
 
416
    struct fstab* fe;
 
417
#elif defined(__sun) || defined(__linux__)
 
418
    FILE *mntfile;
 
419
#endif
 
420
 
 
421
    /* XXX: We should scream real loud here. */
 
422
    if( !(path_copy = strdup( path ) ) )
 
423
      goto DVDOpen_error;
 
424
 
 
425
#ifndef WIN32 /* don't have fchdir, and getcwd( NULL, ... ) is strange */
 
426
              /* Also WIN32 does not have symlinks, so we don't need this bit of code. */
 
427
 
 
428
    /* Resolve any symlinks and get the absolute dir name. */
 
429
    {
 
430
      if( ( cdir  = open( ".", O_RDONLY ) ) >= 0 ) {
 
431
        if( chdir( path_copy ) == -1 ) {
 
432
          goto DVDOpen_error;
 
433
        }
 
434
#ifdef __GLIBC__
 
435
        new_path = get_current_dir_name();
 
436
        if(new_path == NULL) {
 
437
          goto DVDOpen_error;
 
438
        }
 
439
#else
 
440
        new_path = malloc(PATH_MAX+1);
 
441
        if(!new_path) {
 
442
          goto DVDOpen_error;
 
443
        }
 
444
        if( getcwd( new_path, PATH_MAX ) == NULL ) {
 
445
          goto DVDOpen_error;
 
446
        }
 
447
#endif
 
448
        retval = fchdir( cdir );
 
449
        close( cdir );
 
450
        cdir = -1;
 
451
        if( retval == -1 ) {
 
452
          goto DVDOpen_error;
 
453
        }
 
454
        path_copy = new_path;
 
455
        new_path = NULL;
 
456
      }
 
457
    }
 
458
#endif
 
459
 
 
460
    /**
 
461
     * If we're being asked to open a directory, check if that directory
 
462
     * is the mount point for a DVD-ROM which we can use instead.
 
463
     */
 
464
 
 
465
    if( strlen( path_copy ) > 1 ) {
 
466
      if( path_copy[ strlen( path_copy ) - 1 ] == '/' ) {
 
467
        path_copy[ strlen( path_copy ) - 1 ] = '\0';
 
468
      }
 
469
    }
 
470
 
 
471
#if defined(_WIN32) || defined(__OS2__)
 
472
    if(strlen(path_copy) > TITLES_MAX) {
 
473
      if(!strcasecmp(&(path_copy[strlen( path_copy ) - TITLES_MAX]),
 
474
                       "\\video_ts"))
 
475
        path_copy[strlen(path_copy) - (TITLES_MAX-1)] = '\0';
 
476
    }
 
477
#endif
 
478
    if( strlen( path_copy ) > TITLES_MAX ) {
 
479
      if( !strcasecmp( &(path_copy[ strlen( path_copy ) - TITLES_MAX ]),
 
480
                       "/video_ts" ) ) {
 
481
        path_copy[ strlen( path_copy ) - TITLES_MAX ] = '\0';
 
482
      }
 
483
    }
 
484
 
 
485
    if(path_copy[0] == '\0') {
 
486
      path_copy[0] = '/';
 
487
      path_copy[1] = '\0';
 
488
    }
 
489
 
 
490
#if defined(__APPLE__)
 
491
    struct statfs s[128];
 
492
    int r = getfsstat(NULL, 0, MNT_NOWAIT);
 
493
    if (r > 0) {
 
494
        if (r > 128)
 
495
            r = 128;
 
496
        r = getfsstat(s, r * sizeof(s[0]), MNT_NOWAIT);
 
497
        int i;
 
498
        for (i=0; i<r; i++) {
 
499
            if (!strcmp(path_copy, s[i].f_mntonname)) {
 
500
                dev_name = bsd_block2char(s[i].f_mntfromname);
 
501
                fprintf( stderr,
 
502
                        "libdvdread: Attempting to use device %s"
 
503
                        " mounted on %s for CSS authentication\n",
 
504
                        dev_name,
 
505
                        s[i].f_mntonname);
 
506
                auth_drive = DVDOpenImageFile( dev_name, have_css );
 
507
                break;
 
508
            }
 
509
        }
 
510
    }
 
511
#elif defined(SYS_BSD)
 
512
    if( ( fe = getfsfile( path_copy ) ) ) {
 
513
      dev_name = bsd_block2char( fe->fs_spec );
 
514
      fprintf( stderr,
 
515
               "libdvdread: Attempting to use device %s"
 
516
               " mounted on %s for CSS authentication\n",
 
517
               dev_name,
 
518
               fe->fs_file );
 
519
      auth_drive = DVDOpenImageFile( dev_name, have_css );
 
520
    }
 
521
#elif defined(__sun)
 
522
    mntfile = fopen( MNTTAB, "r" );
 
523
    if( mntfile ) {
 
524
      struct mnttab mp;
 
525
      int res;
 
526
 
 
527
      while( ( res = getmntent( mntfile, &mp ) ) != -1 ) {
 
528
        if( res == 0 && !strcmp( mp.mnt_mountp, path_copy ) ) {
 
529
          dev_name = sun_block2char( mp.mnt_special );
 
530
          fprintf( stderr,
 
531
                   "libdvdread: Attempting to use device %s"
 
532
                   " mounted on %s for CSS authentication\n",
 
533
                   dev_name,
 
534
                   mp.mnt_mountp );
 
535
          auth_drive = DVDOpenImageFile( dev_name, have_css );
 
536
          break;
 
537
        }
 
538
      }
 
539
      fclose( mntfile );
 
540
    }
 
541
#elif defined(__linux__)
 
542
    mntfile = fopen( _PATH_MOUNTED, "r" );
 
543
    if( mntfile ) {
 
544
      struct mntent *me;
 
545
 
 
546
      while( ( me = getmntent( mntfile ) ) ) {
 
547
        if( !strcmp( me->mnt_dir, path_copy ) ) {
 
548
          fprintf( stderr,
 
549
                   "libdvdread: Attempting to use device %s"
 
550
                   " mounted on %s for CSS authentication\n",
 
551
                   me->mnt_fsname,
 
552
                   me->mnt_dir );
 
553
          auth_drive = DVDOpenImageFile( me->mnt_fsname, have_css );
 
554
          dev_name = strdup(me->mnt_fsname);
 
555
          break;
 
556
        }
 
557
      }
 
558
      fclose( mntfile );
 
559
    }
 
560
#elif defined(__OS2__)
 
561
    /* Use DVDOpenImageFile() only if it is a drive */
 
562
    if(isalpha(path[0]) && path[1] == ':' &&
 
563
        ( !path[2] ||
 
564
          ((path[2] == '\\' || path[2] == '/') && !path[3])))
 
565
    auth_drive = DVDOpenImageFile( path, have_css );
 
566
#elif defined(_WIN32)
 
567
    if( GetDriveType( path_copy ) == DRIVE_CDROM ) {
 
568
      path_copy[2] = '\0';
 
569
      auth_drive = DVDOpenImageFile( path_copy, have_css );
 
570
    }
 
571
#endif
 
572
 
 
573
#if !defined(_WIN32) && !defined(__OS2__)
 
574
    if( !dev_name ) {
 
575
      fprintf( stderr, "libdvdread: Couldn't find device name.\n" );
 
576
    } else if( !auth_drive ) {
 
577
      fprintf( stderr, "libdvdread: Device %s inaccessible, "
 
578
               "CSS authentication not available.\n", dev_name );
 
579
    }
 
580
#else
 
581
    if( !auth_drive ) {
 
582
        fprintf( stderr, "libdvdread: Device %s inaccessible, "
 
583
                 "CSS authentication not available.\n", path );
 
584
    }
 
585
#endif
 
586
 
 
587
    free( dev_name );
 
588
    dev_name = NULL;
 
589
    free( path_copy );
 
590
    path_copy = NULL;
 
591
 
 
592
    /**
 
593
     * If we've opened a drive, just use that.
 
594
     */
 
595
    if( auth_drive ) {
 
596
      free(path);
 
597
      return auth_drive;
 
598
    }
 
599
    /**
 
600
     * Otherwise, we now try to open the directory tree instead.
 
601
     */
 
602
    ret_val = DVDOpenPath( path );
 
603
      free( path );
 
604
      return ret_val;
 
605
  }
 
606
 
 
607
DVDOpen_error:
 
608
  /* If it's none of the above, screw it. */
 
609
  fprintf( stderr, "libdvdread: Could not open %s\n", path );
 
610
  if( path != NULL )
 
611
    free( path );
 
612
  if ( path_copy != NULL )
 
613
    free( path_copy );
 
614
  if ( cdir >= 0 )
 
615
    close( cdir );
 
616
  if ( new_path != NULL )
 
617
    free( new_path );
 
618
  return NULL;
 
619
}
 
620
 
 
621
void DVDClose( dvd_reader_t *dvd )
 
622
{
 
623
  if( dvd ) {
 
624
    if( dvd->dev ) dvdinput_close( dvd->dev );
 
625
    if( dvd->path_root ) free( dvd->path_root );
 
626
    if( dvd->udfcache ) FreeUDFCache( dvd->udfcache );
 
627
    free( dvd );
 
628
  }
 
629
}
 
630
 
 
631
/**
 
632
 * Open an unencrypted file on a DVD image file.
 
633
 */
 
634
static dvd_file_t *DVDOpenFileUDF( dvd_reader_t *dvd, char *filename )
 
635
{
 
636
  uint32_t start, len;
 
637
  dvd_file_t *dvd_file;
 
638
 
 
639
  start = UDFFindFile( dvd, filename, &len );
 
640
  if( !start ) {
 
641
    fprintf( stderr, "libdvdnav:DVDOpenFileUDF:UDFFindFile %s failed\n", filename );
 
642
    return NULL;
 
643
  }
 
644
 
 
645
  dvd_file = (dvd_file_t *) malloc( sizeof( dvd_file_t ) );
 
646
  if( !dvd_file ) {
 
647
    fprintf( stderr, "libdvdnav:DVDOpenFileUDF:malloc failed\n" );
 
648
    return NULL;
 
649
  }
 
650
  dvd_file->dvd = dvd;
 
651
  dvd_file->lb_start = start;
 
652
  dvd_file->seek_pos = 0;
 
653
  memset( dvd_file->title_sizes, 0, sizeof( dvd_file->title_sizes ) );
 
654
  memset( dvd_file->title_devs, 0, sizeof( dvd_file->title_devs ) );
 
655
  dvd_file->filesize = len / DVD_VIDEO_LB_LEN;
 
656
 
 
657
  return dvd_file;
 
658
}
 
659
 
 
660
/**
 
661
 * Searches for <file> in directory <path>, ignoring case.
 
662
 * Returns 0 and full filename in <filename>.
 
663
 *     or -1 on file not found.
 
664
 *     or -2 on path not found.
 
665
 */
 
666
static int findDirFile( const char *path, const char *file, char **filename )
 
667
{
 
668
  DIR *dir;
 
669
  struct dirent *ent;
 
670
  *filename = NULL;
 
671
 
 
672
  dir = opendir( path );
 
673
  if( !dir ) return -2;
 
674
 
 
675
  while( ( ent = readdir( dir ) ) != NULL ) {
 
676
    if( !strcasecmp( ent->d_name, file ) ) {
 
677
      *filename = malloc( strlen( path ) + 1 + strlen( ent->d_name ) + 1 );
 
678
      if( *filename == NULL ) {
 
679
        closedir(dir);
 
680
        return -1;
 
681
      }
 
682
      sprintf( *filename, "%s%s%s", path,
 
683
               ( ( path[ strlen( path ) - 1 ] == '/' ) ? "" : "/" ),
 
684
               ent->d_name );
 
685
      closedir(dir);
 
686
      return 0;
 
687
    }
 
688
  }
 
689
  closedir(dir);
 
690
  return -1;
 
691
}
 
692
 
 
693
static int findDVDFile( dvd_reader_t *dvd, const char *file, char **filename )
 
694
{
 
695
  char *video_path = NULL;
 
696
  const char *nodirfile;
 
697
  int ret;
 
698
 
 
699
  /* Strip off the directory for our search */
 
700
  if( !strncasecmp( "/VIDEO_TS/", file, 10 ) ) {
 
701
    nodirfile = &(file[ 10 ]);
 
702
  } else {
 
703
    nodirfile = file;
 
704
  }
 
705
 
 
706
  ret = findDirFile( dvd->path_root, nodirfile, filename );
 
707
  if( ret < 0 ) {
 
708
    /* Try also with adding the path, just in case. */
 
709
    video_path = malloc( strlen( dvd->path_root ) + 10 + 1 );
 
710
    if( video_path == NULL ) return 0;
 
711
    sprintf( video_path, "%s/VIDEO_TS/", dvd->path_root );
 
712
    ret = findDirFile( video_path, nodirfile, filename );
 
713
    if( ret < 0 ) {
 
714
      /* Try with the path, but in lower case. */
 
715
      sprintf( video_path, "%s/video_ts/", dvd->path_root );
 
716
      ret = findDirFile( video_path, nodirfile, filename );
 
717
      if( ret < 0 ) {
 
718
        free( video_path );
 
719
        return 0;
 
720
      }
 
721
    }
 
722
    free( video_path );
 
723
  }
 
724
 
 
725
  return 1;
 
726
}
 
727
 
 
728
/**
 
729
 * Open an unencrypted file from a DVD directory tree.
 
730
 */
 
731
static dvd_file_t *DVDOpenFilePath( dvd_reader_t *dvd, char *filename )
 
732
{
 
733
  char *full_path = NULL;
 
734
  dvd_file_t *dvd_file;
 
735
  struct stat fileinfo;
 
736
  dvd_input_t dev;
 
737
 
 
738
  /* Get the full path of the file. */
 
739
  if( !findDVDFile( dvd, filename, &full_path ) ) {
 
740
    fprintf( stderr, "libdvdnav:DVDOpenFilePath:findDVDFile %s failed\n", filename );
 
741
    free( full_path );
 
742
    return NULL;
 
743
  }
 
744
 
 
745
  dev = dvdinput_open( full_path );
 
746
  if( !dev ) {
 
747
    fprintf( stderr, "libdvdnav:DVDOpenFilePath:dvdinput_open %s failed\n", full_path );
 
748
    free( full_path );
 
749
    return NULL;
 
750
  }
 
751
 
 
752
  dvd_file = (dvd_file_t *) malloc( sizeof( dvd_file_t ) );
 
753
  if( !dvd_file ) {
 
754
    fprintf( stderr, "libdvdnav:DVDOpenFilePath:dvd_file malloc failed\n" );
 
755
    dvdinput_close(dev);
 
756
    free( full_path );
 
757
    return NULL;
 
758
  }
 
759
  dvd_file->dvd = dvd;
 
760
  dvd_file->lb_start = 0;
 
761
  dvd_file->seek_pos = 0;
 
762
  memset( dvd_file->title_sizes, 0, sizeof( dvd_file->title_sizes ) );
 
763
  memset( dvd_file->title_devs, 0, sizeof( dvd_file->title_devs ) );
 
764
  dvd_file->filesize = 0;
 
765
 
 
766
  if( stat( full_path, &fileinfo ) < 0 ) {
 
767
    fprintf( stderr, "libdvdread: Can't stat() %s.\n", filename );
 
768
    free( full_path );
 
769
    free( dvd_file );
 
770
    dvdinput_close( dev );
 
771
    return NULL;
 
772
  }
 
773
  dvd_file->title_sizes[ 0 ] = fileinfo.st_size / DVD_VIDEO_LB_LEN;
 
774
  dvd_file->title_devs[ 0 ] = dev;
 
775
  dvd_file->filesize = dvd_file->title_sizes[ 0 ];
 
776
 
 
777
  free( full_path );
 
778
  return dvd_file;
 
779
}
 
780
 
 
781
static dvd_file_t *DVDOpenVOBUDF( dvd_reader_t *dvd, int title, int menu )
 
782
{
 
783
  char filename[ MAX_UDF_FILE_NAME_LEN ];
 
784
  uint32_t start, len;
 
785
  dvd_file_t *dvd_file;
 
786
 
 
787
  if( title == 0 ) {
 
788
    sprintf( filename, "/VIDEO_TS/VIDEO_TS.VOB" );
 
789
  } else {
 
790
    sprintf( filename, "/VIDEO_TS/VTS_%02d_%d.VOB", title, menu ? 0 : 1 );
 
791
  }
 
792
  start = UDFFindFile( dvd, filename, &len );
 
793
  if( start == 0 ) return NULL;
 
794
 
 
795
  dvd_file = (dvd_file_t *) malloc( sizeof( dvd_file_t ) );
 
796
  if( !dvd_file ) return NULL;
 
797
  dvd_file->dvd = dvd;
 
798
  /*Hack*/ dvd_file->css_title = title << 1 | menu;
 
799
  dvd_file->lb_start = start;
 
800
  dvd_file->seek_pos = 0;
 
801
  memset( dvd_file->title_sizes, 0, sizeof( dvd_file->title_sizes ) );
 
802
  memset( dvd_file->title_devs, 0, sizeof( dvd_file->title_devs ) );
 
803
  dvd_file->filesize = len / DVD_VIDEO_LB_LEN;
 
804
 
 
805
  /* Calculate the complete file size for every file in the VOBS */
 
806
  if( !menu ) {
 
807
    int cur;
 
808
 
 
809
    for( cur = 2; cur < 10; cur++ ) {
 
810
      sprintf( filename, "/VIDEO_TS/VTS_%02d_%d.VOB", title, cur );
 
811
      if( !UDFFindFile( dvd, filename, &len ) ) break;
 
812
      dvd_file->filesize += len / DVD_VIDEO_LB_LEN;
 
813
    }
 
814
  }
 
815
 
 
816
  if( dvd->css_state == 1 /* Need key init */ ) {
 
817
    initAllCSSKeys( dvd );
 
818
    dvd->css_state = 2;
 
819
  }
 
820
  /*
 
821
  if( dvdinput_title( dvd_file->dvd->dev, (int)start ) < 0 ) {
 
822
      fprintf( stderr, "libdvdread: Error cracking CSS key for %s\n",
 
823
               filename );
 
824
  }
 
825
  */
 
826
 
 
827
  return dvd_file;
 
828
}
 
829
 
 
830
static dvd_file_t *DVDOpenVOBPath( dvd_reader_t *dvd, int title, int menu )
 
831
{
 
832
  char filename[ MAX_UDF_FILE_NAME_LEN ];
 
833
  char *full_path = NULL;
 
834
  struct stat fileinfo;
 
835
  dvd_file_t *dvd_file;
 
836
  int i;
 
837
 
 
838
  dvd_file = (dvd_file_t *) malloc( sizeof( dvd_file_t ) );
 
839
  if( !dvd_file ) return NULL;
 
840
  dvd_file->dvd = dvd;
 
841
  /*Hack*/ dvd_file->css_title = title << 1 | menu;
 
842
  dvd_file->lb_start = 0;
 
843
  dvd_file->seek_pos = 0;
 
844
  memset( dvd_file->title_sizes, 0, sizeof( dvd_file->title_sizes ) );
 
845
  memset( dvd_file->title_devs, 0, sizeof( dvd_file->title_devs ) );
 
846
  dvd_file->filesize = 0;
 
847
 
 
848
  if( menu ) {
 
849
    dvd_input_t dev;
 
850
 
 
851
    if( title == 0 ) {
 
852
      sprintf( filename, "VIDEO_TS.VOB" );
 
853
    } else {
 
854
      sprintf( filename, "VTS_%02i_0.VOB", title );
 
855
    }
 
856
    if( !findDVDFile( dvd, filename, &full_path ) ) {
 
857
      free( full_path );
 
858
      free( dvd_file );
 
859
      return NULL;
 
860
    }
 
861
 
 
862
    dev = dvdinput_open( full_path );
 
863
    if( dev == NULL ) {
 
864
      free( full_path );
 
865
      free( dvd_file );
 
866
      return NULL;
 
867
    }
 
868
 
 
869
    if( stat( full_path, &fileinfo ) < 0 ) {
 
870
      fprintf( stderr, "libdvdread: Can't stat() %s.\n", filename );
 
871
      dvdinput_close(dev);
 
872
      free( full_path );
 
873
      free( dvd_file );
 
874
      return NULL;
 
875
    }
 
876
    dvd_file->title_sizes[ 0 ] = fileinfo.st_size / DVD_VIDEO_LB_LEN;
 
877
    dvd_file->title_devs[ 0 ] = dev;
 
878
    dvdinput_title( dvd_file->title_devs[0], 0);
 
879
    dvd_file->filesize = dvd_file->title_sizes[ 0 ];
 
880
 
 
881
  } else {
 
882
    for( i = 0; i < TITLES_MAX; ++i ) {
 
883
 
 
884
      sprintf( filename, "VTS_%02i_%i.VOB", title, i + 1 );
 
885
      if( !findDVDFile( dvd, filename, &full_path ) ) {
 
886
        break;
 
887
      }
 
888
 
 
889
      if( stat( full_path, &fileinfo ) < 0 ) {
 
890
        fprintf( stderr, "libdvdread: Can't stat() %s.\n", filename );
 
891
        break;
 
892
      }
 
893
 
 
894
      dvd_file->title_sizes[ i ] = fileinfo.st_size / DVD_VIDEO_LB_LEN;
 
895
      dvd_file->title_devs[ i ] = dvdinput_open( full_path );
 
896
      dvdinput_title( dvd_file->title_devs[ i ], 0 );
 
897
      dvd_file->filesize += dvd_file->title_sizes[ i ];
 
898
    }
 
899
    if( !dvd_file->title_devs[ 0 ] ) {
 
900
      free( full_path );
 
901
      free( dvd_file );
 
902
      return NULL;
 
903
    }
 
904
  }
 
905
  free( full_path );
 
906
  return dvd_file;
 
907
}
 
908
 
 
909
dvd_file_t *DVDOpenFile( dvd_reader_t *dvd, int titlenum,
 
910
                         dvd_read_domain_t domain )
 
911
{
 
912
  char filename[ MAX_UDF_FILE_NAME_LEN ];
 
913
 
 
914
  /* Check arguments. */
 
915
  if( dvd == NULL || titlenum < 0 )
 
916
    return NULL;
 
917
 
 
918
  switch( domain ) {
 
919
  case DVD_READ_INFO_FILE:
 
920
    if( titlenum == 0 ) {
 
921
      sprintf( filename, "/VIDEO_TS/VIDEO_TS.IFO" );
 
922
    } else {
 
923
      sprintf( filename, "/VIDEO_TS/VTS_%02i_0.IFO", titlenum );
 
924
    }
 
925
    break;
 
926
  case DVD_READ_INFO_BACKUP_FILE:
 
927
    if( titlenum == 0 ) {
 
928
      sprintf( filename, "/VIDEO_TS/VIDEO_TS.BUP" );
 
929
    } else {
 
930
      sprintf( filename, "/VIDEO_TS/VTS_%02i_0.BUP", titlenum );
 
931
    }
 
932
    break;
 
933
  case DVD_READ_MENU_VOBS:
 
934
    if( dvd->isImageFile ) {
 
935
      return DVDOpenVOBUDF( dvd, titlenum, 1 );
 
936
    } else {
 
937
      return DVDOpenVOBPath( dvd, titlenum, 1 );
 
938
    }
 
939
    break;
 
940
  case DVD_READ_TITLE_VOBS:
 
941
    if( titlenum == 0 ) return 0;
 
942
    if( dvd->isImageFile ) {
 
943
      return DVDOpenVOBUDF( dvd, titlenum, 0 );
 
944
    } else {
 
945
      return DVDOpenVOBPath( dvd, titlenum, 0 );
 
946
    }
 
947
    break;
 
948
  default:
 
949
    fprintf( stderr, "libdvdread: Invalid domain for file open.\n" );
 
950
    return NULL;
 
951
  }
 
952
 
 
953
  if( dvd->isImageFile ) {
 
954
    return DVDOpenFileUDF( dvd, filename );
 
955
  } else {
 
956
    return DVDOpenFilePath( dvd, filename );
 
957
  }
 
958
}
 
959
 
 
960
void DVDCloseFile( dvd_file_t *dvd_file )
 
961
{
 
962
  int i;
 
963
 
 
964
  if( dvd_file ) {
 
965
    if( !dvd_file->dvd->isImageFile ) {
 
966
      for( i = 0; i < TITLES_MAX; ++i ) {
 
967
        if( dvd_file->title_devs[ i ] ) {
 
968
          dvdinput_close( dvd_file->title_devs[i] );
 
969
        }
 
970
      }
 
971
    }
 
972
 
 
973
    free( dvd_file );
 
974
    dvd_file = 0;
 
975
  }
 
976
}
 
977
 
 
978
static int DVDFileStatVOBUDF( dvd_reader_t *dvd, int title,
 
979
                              int menu, dvd_stat_t *statbuf )
 
980
{
 
981
  char filename[ MAX_UDF_FILE_NAME_LEN ];
 
982
  uint32_t size;
 
983
  off_t tot_size;
 
984
  off_t parts_size[ 9 ];
 
985
  int nr_parts = 0;
 
986
  int n;
 
987
 
 
988
  if( title == 0 )
 
989
    sprintf( filename, "/VIDEO_TS/VIDEO_TS.VOB" );
 
990
  else
 
991
    sprintf( filename, "/VIDEO_TS/VTS_%02d_%d.VOB", title, menu ? 0 : 1 );
 
992
 
 
993
  if( !UDFFindFile( dvd, filename, &size ) )
 
994
    return -1;
 
995
 
 
996
  tot_size = size;
 
997
  nr_parts = 1;
 
998
  parts_size[ 0 ] = size;
 
999
 
 
1000
  if( !menu ) {
 
1001
    int cur;
 
1002
 
 
1003
    for( cur = 2; cur < 10; cur++ ) {
 
1004
      sprintf( filename, "/VIDEO_TS/VTS_%02d_%d.VOB", title, cur );
 
1005
      if( !UDFFindFile( dvd, filename, &size ) )
 
1006
        break;
 
1007
 
 
1008
      parts_size[ nr_parts ] = size;
 
1009
      tot_size += size;
 
1010
      nr_parts++;
 
1011
    }
 
1012
  }
 
1013
 
 
1014
  statbuf->size = tot_size;
 
1015
  statbuf->nr_parts = nr_parts;
 
1016
  for( n = 0; n < nr_parts; n++ )
 
1017
    statbuf->parts_size[ n ] = parts_size[ n ];
 
1018
 
 
1019
  return 0;
 
1020
}
 
1021
 
 
1022
 
 
1023
static int DVDFileStatVOBPath( dvd_reader_t *dvd, int title,
 
1024
                               int menu, dvd_stat_t *statbuf )
 
1025
{
 
1026
  char filename[ MAX_UDF_FILE_NAME_LEN ];
 
1027
  char *full_path = NULL;
 
1028
  struct stat fileinfo;
 
1029
  off_t tot_size;
 
1030
  off_t parts_size[ 9 ];
 
1031
  int nr_parts = 0;
 
1032
  int n;
 
1033
 
 
1034
  if( title == 0 )
 
1035
    sprintf( filename, "VIDEO_TS.VOB" );
 
1036
  else
 
1037
    sprintf( filename, "VTS_%02d_%d.VOB", title, menu ? 0 : 1 );
 
1038
 
 
1039
  if( !findDVDFile( dvd, filename, &full_path ) ) {
 
1040
    free( full_path );
 
1041
    return -1;
 
1042
  }
 
1043
 
 
1044
  if( stat( full_path, &fileinfo ) < 0 ) {
 
1045
    fprintf( stderr, "libdvdread: Can't stat() %s.\n", filename );
 
1046
    free( full_path );
 
1047
    return -1;
 
1048
  }
 
1049
 
 
1050
  tot_size = fileinfo.st_size;
 
1051
  nr_parts = 1;
 
1052
  parts_size[ 0 ] = fileinfo.st_size;
 
1053
 
 
1054
  if( !menu ) {
 
1055
    int cur;
 
1056
    for( cur = 2; cur < 10; cur++ ) {
 
1057
      sprintf( filename, "VTS_%02d_%d.VOB", title, cur );
 
1058
      if( !findDVDFile( dvd, filename, &full_path ) )
 
1059
        break;
 
1060
 
 
1061
      if( stat( full_path, &fileinfo ) < 0 ) {
 
1062
        fprintf( stderr, "libdvdread: Can't stat() %s.\n", filename );
 
1063
        break;
 
1064
      }
 
1065
 
 
1066
      parts_size[ nr_parts ] = fileinfo.st_size;
 
1067
      tot_size += parts_size[ nr_parts ];
 
1068
      nr_parts++;
 
1069
    }
 
1070
  }
 
1071
 
 
1072
  statbuf->size = tot_size;
 
1073
  statbuf->nr_parts = nr_parts;
 
1074
  for( n = 0; n < nr_parts; n++ )
 
1075
    statbuf->parts_size[ n ] = parts_size[ n ];
 
1076
 
 
1077
  free( full_path );
 
1078
  return 0;
 
1079
}
 
1080
 
 
1081
 
 
1082
int DVDFileStat( dvd_reader_t *dvd, int titlenum,
 
1083
                 dvd_read_domain_t domain, dvd_stat_t *statbuf )
 
1084
{
 
1085
  char filename[ MAX_UDF_FILE_NAME_LEN ];
 
1086
  char *full_path = NULL;
 
1087
  struct stat fileinfo;
 
1088
  uint32_t size;
 
1089
 
 
1090
  /* Check arguments. */
 
1091
  if( dvd == NULL || titlenum < 0 ) {
 
1092
    errno = EINVAL;
 
1093
    return -1;
 
1094
  }
 
1095
 
 
1096
  switch( domain ) {
 
1097
  case DVD_READ_INFO_FILE:
 
1098
    if( titlenum == 0 )
 
1099
      sprintf( filename, "/VIDEO_TS/VIDEO_TS.IFO" );
 
1100
    else
 
1101
      sprintf( filename, "/VIDEO_TS/VTS_%02i_0.IFO", titlenum );
 
1102
 
 
1103
    break;
 
1104
  case DVD_READ_INFO_BACKUP_FILE:
 
1105
    if( titlenum == 0 )
 
1106
      sprintf( filename, "/VIDEO_TS/VIDEO_TS.BUP" );
 
1107
    else
 
1108
      sprintf( filename, "/VIDEO_TS/VTS_%02i_0.BUP", titlenum );
 
1109
 
 
1110
    break;
 
1111
  case DVD_READ_MENU_VOBS:
 
1112
    if( dvd->isImageFile )
 
1113
      return DVDFileStatVOBUDF( dvd, titlenum, 1, statbuf );
 
1114
    else
 
1115
      return DVDFileStatVOBPath( dvd, titlenum, 1, statbuf );
 
1116
 
 
1117
    break;
 
1118
  case DVD_READ_TITLE_VOBS:
 
1119
    if( titlenum == 0 )
 
1120
      return -1;
 
1121
 
 
1122
    if( dvd->isImageFile )
 
1123
      return DVDFileStatVOBUDF( dvd, titlenum, 0, statbuf );
 
1124
    else
 
1125
      return DVDFileStatVOBPath( dvd, titlenum, 0, statbuf );
 
1126
 
 
1127
    break;
 
1128
  default:
 
1129
    fprintf( stderr, "libdvdread: Invalid domain for file stat.\n" );
 
1130
    errno = EINVAL;
 
1131
    return -1;
 
1132
  }
 
1133
 
 
1134
  if( dvd->isImageFile ) {
 
1135
    if( UDFFindFile( dvd, filename, &size ) ) {
 
1136
      statbuf->size = size;
 
1137
      statbuf->nr_parts = 1;
 
1138
      statbuf->parts_size[ 0 ] = size;
 
1139
      return 0;
 
1140
    }
 
1141
  } else {
 
1142
    if( findDVDFile( dvd, filename, &full_path ) ) {
 
1143
      if( stat( full_path, &fileinfo ) < 0 )
 
1144
        fprintf( stderr, "libdvdread: Can't stat() %s.\n", filename );
 
1145
      else {
 
1146
        statbuf->size = fileinfo.st_size;
 
1147
        statbuf->nr_parts = 1;
 
1148
        statbuf->parts_size[ 0 ] = statbuf->size;
 
1149
        free( full_path );
 
1150
        return 0;
 
1151
      }
 
1152
    }
 
1153
  }
 
1154
  free( full_path );
 
1155
  return -1;
 
1156
}
 
1157
 
 
1158
/* Internal, but used from dvd_udf.c */
 
1159
int UDFReadBlocksRaw( dvd_reader_t *device, uint32_t lb_number,
 
1160
                      size_t block_count, unsigned char *data,
 
1161
                      int encrypted )
 
1162
{
 
1163
  int ret;
 
1164
 
 
1165
  if( !device->dev ) {
 
1166
    fprintf( stderr, "libdvdread: Fatal error in block read.\n" );
 
1167
    return 0;
 
1168
  }
 
1169
 
 
1170
  ret = dvdinput_seek( device->dev, (int) lb_number );
 
1171
  if( ret != (int) lb_number ) {
 
1172
    fprintf( stderr, "libdvdread: Can't seek to block %u\n", lb_number );
 
1173
    return 0;
 
1174
  }
 
1175
 
 
1176
  ret = dvdinput_read( device->dev, (char *) data,
 
1177
                       (int) block_count, encrypted );
 
1178
  return ret;
 
1179
}
 
1180
 
 
1181
/* This is using a single input and starting from 'dvd_file->lb_start' offset.
 
1182
 *
 
1183
 * Reads 'block_count' blocks from 'dvd_file' at block offset 'offset'
 
1184
 * into the buffer located at 'data' and if 'encrypted' is set
 
1185
 * descramble the data if it's encrypted.  Returning either an
 
1186
 * negative error or the number of blocks read. */
 
1187
static int DVDReadBlocksUDF( dvd_file_t *dvd_file, uint32_t offset,
 
1188
                             size_t block_count, unsigned char *data,
 
1189
                             int encrypted )
 
1190
{
 
1191
  return UDFReadBlocksRaw( dvd_file->dvd, dvd_file->lb_start + offset,
 
1192
                           block_count, data, encrypted );
 
1193
}
 
1194
 
 
1195
/* This is using possibly several inputs and starting from an offset of '0'.
 
1196
 *
 
1197
 * Reads 'block_count' blocks from 'dvd_file' at block offset 'offset'
 
1198
 * into the buffer located at 'data' and if 'encrypted' is set
 
1199
 * descramble the data if it's encrypted.  Returning either an
 
1200
 * negative error or the number of blocks read. */
 
1201
static int DVDReadBlocksPath( dvd_file_t *dvd_file, unsigned int offset,
 
1202
                              size_t block_count, unsigned char *data,
 
1203
                              int encrypted )
 
1204
{
 
1205
  int i;
 
1206
  int ret, ret2, off;
 
1207
 
 
1208
  ret = 0;
 
1209
  ret2 = 0;
 
1210
  for( i = 0; i < TITLES_MAX; ++i ) {
 
1211
    if( !dvd_file->title_sizes[ i ] ) return 0; /* Past end of file */
 
1212
 
 
1213
    if( offset < dvd_file->title_sizes[ i ] ) {
 
1214
      if( ( offset + block_count ) <= dvd_file->title_sizes[ i ] ) {
 
1215
        off = dvdinput_seek( dvd_file->title_devs[ i ], (int)offset );
 
1216
        if( off < 0 || off != (int)offset ) {
 
1217
          fprintf( stderr, "libdvdread: Can't seek to block %d\n",
 
1218
                   offset );
 
1219
          return off < 0 ? off : 0;
 
1220
        }
 
1221
        ret = dvdinput_read( dvd_file->title_devs[ i ], data,
 
1222
                             (int)block_count, encrypted );
 
1223
        break;
 
1224
      } else {
 
1225
        size_t part1_size = dvd_file->title_sizes[ i ] - offset;
 
1226
        /* FIXME: Really needs to be a while loop.
 
1227
         * (This is only true if you try and read >1GB at a time) */
 
1228
 
 
1229
        /* Read part 1 */
 
1230
        off = dvdinput_seek( dvd_file->title_devs[ i ], (int)offset );
 
1231
        if( off < 0 || off != (int)offset ) {
 
1232
          fprintf( stderr, "libdvdread: Can't seek to block %d\n",
 
1233
                   offset );
 
1234
          return off < 0 ? off : 0;
 
1235
        }
 
1236
        ret = dvdinput_read( dvd_file->title_devs[ i ], data,
 
1237
                             (int)part1_size, encrypted );
 
1238
        if( ret < 0 ) return ret;
 
1239
        /* FIXME: This is wrong if i is the last file in the set.
 
1240
         * also error from this read will not show in ret. */
 
1241
 
 
1242
        /* Does the next part exist? If not then return now. */
 
1243
        if( i + 1 >= TITLES_MAX || !dvd_file->title_devs[ i + 1 ] )
 
1244
          return ret;
 
1245
 
 
1246
        /* Read part 2 */
 
1247
        off = dvdinput_seek( dvd_file->title_devs[ i + 1 ], 0 );
 
1248
        if( off < 0 || off != 0 ) {
 
1249
          fprintf( stderr, "libdvdread: Can't seek to block %d\n",
 
1250
                   0 );
 
1251
          return off < 0 ? off : 0;
 
1252
        }
 
1253
        ret2 = dvdinput_read( dvd_file->title_devs[ i + 1 ],
 
1254
                              data + ( part1_size
 
1255
                                       * (int64_t)DVD_VIDEO_LB_LEN ),
 
1256
                              (int)(block_count - part1_size),
 
1257
                              encrypted );
 
1258
        if( ret2 < 0 ) return ret2;
 
1259
        break;
 
1260
      }
 
1261
    } else {
 
1262
      offset -= dvd_file->title_sizes[ i ];
 
1263
    }
 
1264
  }
 
1265
 
 
1266
  return ret + ret2;
 
1267
}
 
1268
 
 
1269
/* This is broken reading more than 2Gb at a time is ssize_t is 32-bit. */
 
1270
ssize_t DVDReadBlocks( dvd_file_t *dvd_file, int offset,
 
1271
                       size_t block_count, unsigned char *data )
 
1272
{
 
1273
  int ret;
 
1274
 
 
1275
  /* Check arguments. */
 
1276
  if( dvd_file == NULL || offset < 0 || data == NULL )
 
1277
    return -1;
 
1278
 
 
1279
  /* Hack, and it will still fail for multiple opens in a threaded app ! */
 
1280
  if( dvd_file->dvd->css_title != dvd_file->css_title ) {
 
1281
    dvd_file->dvd->css_title = dvd_file->css_title;
 
1282
    if( dvd_file->dvd->isImageFile ) {
 
1283
      dvdinput_title( dvd_file->dvd->dev, (int)dvd_file->lb_start );
 
1284
    }
 
1285
    /* Here each vobu has it's own dvdcss handle, so no need to update
 
1286
    else {
 
1287
      dvdinput_title( dvd_file->title_devs[ 0 ], (int)dvd_file->lb_start );
 
1288
    }*/
 
1289
  }
 
1290
 
 
1291
  if( dvd_file->dvd->isImageFile ) {
 
1292
    ret = DVDReadBlocksUDF( dvd_file, (uint32_t)offset,
 
1293
                            block_count, data, DVDINPUT_READ_DECRYPT );
 
1294
  } else {
 
1295
    ret = DVDReadBlocksPath( dvd_file, (unsigned int)offset,
 
1296
                             block_count, data, DVDINPUT_READ_DECRYPT );
 
1297
  }
 
1298
 
 
1299
  return (ssize_t)ret;
 
1300
}
 
1301
 
 
1302
int32_t DVDFileSeek( dvd_file_t *dvd_file, int32_t offset )
 
1303
{
 
1304
  /* Check arguments. */
 
1305
  if( dvd_file == NULL || offset < 0 )
 
1306
    return -1;
 
1307
 
 
1308
  if( offset > dvd_file->filesize * DVD_VIDEO_LB_LEN ) {
 
1309
    return -1;
 
1310
  }
 
1311
  dvd_file->seek_pos = (uint32_t) offset;
 
1312
  return offset;
 
1313
}
 
1314
 
 
1315
int DVDFileSeekForce(dvd_file_t *dvd_file, int offset, int force_size)
 
1316
{
 
1317
  /* Check arguments. */
 
1318
  if( dvd_file == NULL || offset <= 0 )
 
1319
      return -1;
 
1320
 
 
1321
  if( dvd_file->dvd->isImageFile ) {
 
1322
    if( force_size < 0 )
 
1323
      force_size = (offset - 1) / DVD_VIDEO_LB_LEN + 1;
 
1324
    if( dvd_file->filesize < force_size ) {
 
1325
      dvd_file->filesize = force_size;
 
1326
      fprintf(stderr, "libdvdread: Ignored size of file indicated in UDF.\n");
 
1327
    }
 
1328
  }
 
1329
 
 
1330
  if( offset > dvd_file->filesize * DVD_VIDEO_LB_LEN )
 
1331
    return -1;
 
1332
 
 
1333
  dvd_file->seek_pos = (uint32_t) offset;
 
1334
  return offset;
 
1335
}
 
1336
 
 
1337
ssize_t DVDReadBytes( dvd_file_t *dvd_file, void *data, size_t byte_size )
 
1338
{
 
1339
  unsigned char *secbuf_base, *secbuf;
 
1340
  unsigned int numsec, seek_sector, seek_byte;
 
1341
  int ret;
 
1342
 
 
1343
  /* Check arguments. */
 
1344
  if( dvd_file == NULL || data == NULL )
 
1345
    return -1;
 
1346
 
 
1347
  seek_sector = dvd_file->seek_pos / DVD_VIDEO_LB_LEN;
 
1348
  seek_byte   = dvd_file->seek_pos % DVD_VIDEO_LB_LEN;
 
1349
 
 
1350
  numsec = ( ( seek_byte + byte_size ) / DVD_VIDEO_LB_LEN ) +
 
1351
    ( ( ( seek_byte + byte_size ) % DVD_VIDEO_LB_LEN ) ? 1 : 0 );
 
1352
 
 
1353
  secbuf_base = (unsigned char *) malloc( numsec * DVD_VIDEO_LB_LEN + 2048 );
 
1354
  secbuf = (unsigned char *)(((uintptr_t)secbuf_base & ~((uintptr_t)2047)) + 2048);
 
1355
  if( !secbuf_base ) {
 
1356
    fprintf( stderr, "libdvdread: Can't allocate memory "
 
1357
             "for file read!\n" );
 
1358
    return 0;
 
1359
  }
 
1360
 
 
1361
  if( dvd_file->dvd->isImageFile ) {
 
1362
    ret = DVDReadBlocksUDF( dvd_file, (uint32_t) seek_sector,
 
1363
                            (size_t) numsec, secbuf, DVDINPUT_NOFLAGS );
 
1364
  } else {
 
1365
    ret = DVDReadBlocksPath( dvd_file, seek_sector,
 
1366
                             (size_t) numsec, secbuf, DVDINPUT_NOFLAGS );
 
1367
  }
 
1368
 
 
1369
  if( ret != (int) numsec ) {
 
1370
    free( secbuf_base );
 
1371
    return ret < 0 ? ret : 0;
 
1372
  }
 
1373
 
 
1374
  memcpy( data, &(secbuf[ seek_byte ]), byte_size );
 
1375
  free( secbuf_base );
 
1376
 
 
1377
  DVDFileSeekForce(dvd_file, dvd_file->seek_pos + byte_size, -1);
 
1378
  return byte_size;
 
1379
}
 
1380
 
 
1381
ssize_t DVDFileSize( dvd_file_t *dvd_file )
 
1382
{
 
1383
  /* Check arguments. */
 
1384
  if( dvd_file == NULL )
 
1385
    return -1;
 
1386
 
 
1387
  return dvd_file->filesize;
 
1388
}
 
1389
 
 
1390
int DVDDiscID( dvd_reader_t *dvd, unsigned char *discid )
 
1391
{
 
1392
  struct md5_ctx ctx;
 
1393
  int title;
 
1394
  int nr_of_files = 0;
 
1395
 
 
1396
  /* Check arguments. */
 
1397
  if( dvd == NULL || discid == NULL )
 
1398
    return 0;
 
1399
 
 
1400
  /* Go through the first 10 IFO:s, in order,
 
1401
   * and md5sum them, i.e  VIDEO_TS.IFO and VTS_0?_0.IFO */
 
1402
  md5_init_ctx( &ctx );
 
1403
  for( title = 0; title < 10; title++ ) {
 
1404
    dvd_file_t *dvd_file = DVDOpenFile( dvd, title, DVD_READ_INFO_FILE );
 
1405
    if( dvd_file != NULL ) {
 
1406
      ssize_t bytes_read;
 
1407
      size_t file_size = dvd_file->filesize * DVD_VIDEO_LB_LEN;
 
1408
      char *buffer_base = malloc( file_size + 2048 );
 
1409
      char *buffer = (char *)(((uintptr_t)buffer_base & ~((uintptr_t)2047)) + 2048);
 
1410
 
 
1411
      if( buffer_base == NULL ) {
 
1412
          DVDCloseFile( dvd_file );
 
1413
          fprintf( stderr, "libdvdread: DVDDiscId, failed to "
 
1414
                   "allocate memory for file read!\n" );
 
1415
          return -1;
 
1416
      }
 
1417
 
 
1418
      bytes_read = DVDReadBytes( dvd_file, buffer, file_size );
 
1419
      if( bytes_read != file_size ) {
 
1420
          fprintf( stderr, "libdvdread: DVDDiscId read returned %zd bytes"
 
1421
                   ", wanted %zd\n", bytes_read, file_size );
 
1422
          DVDCloseFile( dvd_file );
 
1423
          free( buffer_base );
 
1424
          return -1;
 
1425
      }
 
1426
 
 
1427
      md5_process_bytes( buffer, file_size,  &ctx );
 
1428
 
 
1429
      DVDCloseFile( dvd_file );
 
1430
      free( buffer_base );
 
1431
      nr_of_files++;
 
1432
    }
 
1433
  }
 
1434
  md5_finish_ctx( &ctx, discid );
 
1435
  if(!nr_of_files)
 
1436
    return -1;
 
1437
 
 
1438
  return 0;
 
1439
}
 
1440
 
 
1441
 
 
1442
int DVDISOVolumeInfo( dvd_reader_t *dvd,
 
1443
                      char *volid, unsigned int volid_size,
 
1444
                      unsigned char *volsetid, unsigned int volsetid_size )
 
1445
{
 
1446
  unsigned char *buffer, *buffer_base;
 
1447
  int ret;
 
1448
 
 
1449
  /* Check arguments. */
 
1450
  if( dvd == NULL )
 
1451
    return 0;
 
1452
 
 
1453
  if( dvd->dev == NULL ) {
 
1454
    /* No block access, so no ISO... */
 
1455
    return -1;
 
1456
  }
 
1457
 
 
1458
  buffer_base = malloc( DVD_VIDEO_LB_LEN + 2048 );
 
1459
  buffer = (unsigned char *)(((uintptr_t)buffer_base & ~((uintptr_t)2047)) + 2048);
 
1460
 
 
1461
  if( buffer_base == NULL ) {
 
1462
    fprintf( stderr, "libdvdread: DVDISOVolumeInfo, failed to "
 
1463
             "allocate memory for file read!\n" );
 
1464
    return -1;
 
1465
  }
 
1466
 
 
1467
  ret = UDFReadBlocksRaw( dvd, 16, 1, buffer, 0 );
 
1468
  if( ret != 1 ) {
 
1469
    fprintf( stderr, "libdvdread: DVDISOVolumeInfo, failed to "
 
1470
             "read ISO9660 Primary Volume Descriptor!\n" );
 
1471
    free( buffer_base );
 
1472
    return -1;
 
1473
  }
 
1474
 
 
1475
  if( (volid != NULL) && (volid_size > 0) ) {
 
1476
    unsigned int n;
 
1477
    for(n = 0; n < 32; n++) {
 
1478
      if(buffer[40+n] == 0x20) {
 
1479
        break;
 
1480
      }
 
1481
    }
 
1482
 
 
1483
    if(volid_size > n+1) {
 
1484
      volid_size = n+1;
 
1485
    }
 
1486
 
 
1487
    memcpy(volid, &buffer[40], volid_size-1);
 
1488
    volid[volid_size-1] = '\0';
 
1489
  }
 
1490
 
 
1491
  if( (volsetid != NULL) && (volsetid_size > 0) ) {
 
1492
    if(volsetid_size > 128) {
 
1493
      volsetid_size = 128;
 
1494
    }
 
1495
    memcpy(volsetid, &buffer[190], volsetid_size);
 
1496
  }
 
1497
  free( buffer_base );
 
1498
  return 0;
 
1499
}
 
1500
 
 
1501
 
 
1502
int DVDUDFVolumeInfo( dvd_reader_t *dvd,
 
1503
                      char *volid, unsigned int volid_size,
 
1504
                      unsigned char *volsetid, unsigned int volsetid_size )
 
1505
{
 
1506
  int ret;
 
1507
  /* Check arguments. */
 
1508
  if( dvd == NULL )
 
1509
    return -1;
 
1510
 
 
1511
  if( dvd->dev == NULL ) {
 
1512
    /* No block access, so no UDF VolumeSet Identifier */
 
1513
    return -1;
 
1514
  }
 
1515
 
 
1516
  if( (volid != NULL) && (volid_size > 0) ) {
 
1517
    ret = UDFGetVolumeIdentifier(dvd, volid, volid_size);
 
1518
    if(!ret) {
 
1519
      return -1;
 
1520
    }
 
1521
  }
 
1522
  if( (volsetid != NULL) && (volsetid_size > 0) ) {
 
1523
    ret =  UDFGetVolumeSetIdentifier(dvd, volsetid, volsetid_size);
 
1524
    if(!ret) {
 
1525
      return -1;
 
1526
    }
 
1527
  }
 
1528
 
 
1529
  return 0;
 
1530
}