device.c 20.4 KB
Newer Older
1 2 3
/*****************************************************************************
 * device.h: DVD device access
 *****************************************************************************
4
 * Copyright (C) 1998-2006 VideoLAN
5
 *
6
 * Authors: Stéphane Borel <stef@via.ecp.fr>
7
 *          Sam Hocevar <sam@zoy.org>
8
 *          Håkan Hjort <d95hjort@dtek.chalmers.se>
9
 *
10
 * libdvdcss is free software; you can redistribute it and/or modify
11 12 13
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
14
 *
15
 * libdvdcss is distributed in the hope that it will be useful,
16 17 18 19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
Diego Biurrun's avatar
Diego Biurrun committed
20
 * You should have received a copy of the GNU General Public License along
21
 * with libdvdcss; if not, write to the Free Software Foundation, Inc.,
Diego Biurrun's avatar
Diego Biurrun committed
22
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 24 25 26 27 28 29
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include "config.h"

30
#include <limits.h>
31 32 33
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
34 35 36
#ifdef HAVE_ERRNO_H
#   include <errno.h>
#endif
37 38
#include <sys/types.h>
#include <sys/stat.h>
39 40 41
#ifdef HAVE_SYS_PARAM_H
#   include <sys/param.h>
#endif
42 43 44 45
#include <fcntl.h>

#ifdef HAVE_UNISTD_H
#   include <unistd.h>
46 47
#endif

48 49
#ifdef DARWIN_DVD_IOCTL
#   include <paths.h>
50
#   include <CoreFoundation/CoreFoundation.h>
51 52 53 54 55 56 57
#   include <IOKit/IOKitLib.h>
#   include <IOKit/IOBSD.h>
#   include <IOKit/storage/IOMedia.h>
#   include <IOKit/storage/IOCDMedia.h>
#   include <IOKit/storage/IODVDMedia.h>
#endif

58
#ifdef __OS2__
KO Myung-Hun's avatar
KO Myung-Hun committed
59 60 61
#   define INCL_DOS
#   define INCL_DOSDEVIOCTL
#   include <os2.h>
62 63
#   include <io.h>                                              /* setmode() */
#   include <fcntl.h>                                           /* O_BINARY  */
KO Myung-Hun's avatar
KO Myung-Hun committed
64 65
#endif

66 67 68 69 70 71 72 73
#include "dvdcss/dvdcss.h"

#include "common.h"
#include "css.h"
#include "libdvdcss.h"
#include "ioctl.h"
#include "device.h"

74
/*****************************************************************************
75
 * Device reading prototypes
76
 *****************************************************************************/
77
static int libc_open  ( dvdcss_t, const char * );
78 79
static int libc_seek  ( dvdcss_t, int );
static int libc_read  ( dvdcss_t, void *, int );
80
static int libc_readv ( dvdcss_t, const struct iovec *, int );
81

82
#ifdef WIN32
83 84 85
static int win2k_open  ( dvdcss_t, const char * );
static int win2k_seek  ( dvdcss_t, int );
static int win2k_read  ( dvdcss_t, void *, int );
86
static int win2k_readv ( dvdcss_t, const struct iovec *, int );
87

88
#elif defined( __OS2__ )
89
static int os2_open ( dvdcss_t, const char * );
90 91
#endif

92
int dvdcss_use_ioctls( dvdcss_t dvdcss )
93
{
94
#if defined( WIN32 )
95
    if( dvdcss->p_handle )
96
    {
97
        return 0;
98 99
    }

100
    /* FIXME: implement this for Windows */
101
    return 1;
102
#elif defined( __OS2__ )
KO Myung-Hun's avatar
KO Myung-Hun committed
103 104 105 106 107 108 109 110 111
    ULONG ulMode;

    if( DosQueryFHState( dvdcss->i_fd, &ulMode ) != 0 )
        return 1;  /* What to do?  Be conservative and try to use the ioctls */

    if( ulMode & OPEN_FLAGS_DASD )
        return 1;

    return 0;
112 113 114 115 116 117 118
#else
    struct stat fileinfo;
    int ret;

    ret = fstat( dvdcss->i_fd, &fileinfo );
    if( ret < 0 )
    {
119
        return 1;  /* What to do?  Be conservative and try to use the ioctls */
120
    }
121 122

    /* Complete this list and check that we test for the right things
123 124 125 126 127
     * (I've assumed for all OSs that 'r', (raw) device, are char devices
     *  and those that don't contain/use an 'r' in the name are block devices)
     *
     * Linux    needs a block device
     * Solaris  needs a char device
128
     * Darwin   needs a char device
129 130 131 132
     * OpenBSD  needs a char device
     * NetBSD   needs a char device
     * FreeBSD  can use either the block or the char device
     */
133

134
    /* Check if this is a block/char device */
135
    if( S_ISBLK( fileinfo.st_mode ) ||
136
        S_ISCHR( fileinfo.st_mode ) )
137
    {
138
        return 1;
139 140 141
    }
    else
    {
142
        return 0;
143 144 145 146
    }
#endif
}

147
void dvdcss_check_device ( dvdcss_t dvdcss )
148 149 150 151
{
#if defined( WIN32 )
    DWORD drives;
    int i;
152 153 154 155 156 157
#elif defined( DARWIN_DVD_IOCTL )
    io_object_t next_media;
    mach_port_t master_port;
    kern_return_t kern_result;
    io_iterator_t media_iterator;
    CFMutableDictionaryRef classes_to_match;
158
#elif defined( __OS2__ )
KO Myung-Hun's avatar
KO Myung-Hun committed
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
#pragma pack( 1 )
    struct
    {
        BYTE bCmdInfo;
        BYTE bDrive;
    } param;

    struct
    {
        BYTE    abEBPB[31];
        USHORT  usCylinders;
        BYTE    bDevType;
        USHORT  usDevAttr;
    } data;
#pragma pack()

    ULONG ulParamLen;
    ULONG ulDataLen;
    ULONG rc;

    int i;
180
#else
181
    const char *ppsz_devices[] = { "/dev/dvd", "/dev/cdrom", "/dev/hdc", NULL };
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
    int i, i_fd;
#endif

    /* If the device name is non-null, return */
    if( dvdcss->psz_device[0] )
    {
        return;
    }

#if defined( WIN32 )
    drives = GetLogicalDrives();

    for( i = 0; drives; i++ )
    {
        char psz_device[5];
        DWORD cur = 1 << i;
        UINT i_ret;

        if( (drives & cur) == 0 )
        {
            continue;
        }
        drives &= ~cur;

        sprintf( psz_device, "%c:\\", 'A' + i );
        i_ret = GetDriveType( psz_device );
        if( i_ret != DRIVE_CDROM )
        {
            continue;
        }

213 214 215
        /* Remove trailing backslash */
        psz_device[2] = '\0';

216
        /* FIXME: we want to differentiate between CD and DVD drives
217 218 219 220 221 222
         * using DeviceIoControl() */
        print_debug( dvdcss, "defaulting to drive `%s'", psz_device );
        free( dvdcss->psz_device );
        dvdcss->psz_device = strdup( psz_device );
        return;
    }
223 224 225 226 227 228 229 230
#elif defined( DARWIN_DVD_IOCTL )

    kern_result = IOMasterPort( MACH_PORT_NULL, &master_port );
    if( kern_result != KERN_SUCCESS )
    {
        return;
    }

231
    classes_to_match = IOServiceMatching( kIODVDMediaClass );
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
    if( classes_to_match == NULL )
    {
        return;
    }

    CFDictionarySetValue( classes_to_match, CFSTR( kIOMediaEjectableKey ),
                          kCFBooleanTrue );

    kern_result = IOServiceGetMatchingServices( master_port, classes_to_match,
                                                &media_iterator );
    if( kern_result != KERN_SUCCESS )
    {
        return;
    }

    next_media = IOIteratorNext( media_iterator );
248
    for( ; ; )
249 250 251 252 253
    {
        char psz_buf[0x32];
        size_t i_pathlen;
        CFTypeRef psz_path;

254 255
        next_media = IOIteratorNext( media_iterator );
        if( next_media == 0 )
256
        {
257 258
            break;
        }
259

260 261 262 263 264 265 266 267 268
        psz_path = IORegistryEntryCreateCFProperty( next_media,
                                                    CFSTR( kIOBSDNameKey ),
                                                    kCFAllocatorDefault,
                                                    0 );
        if( psz_path == NULL )
        {
            IOObjectRelease( next_media );
            continue;
        }
269

270 271 272 273 274 275 276 277 278 279
        snprintf( psz_buf, sizeof(psz_buf), "%s%c", _PATH_DEV, 'r' );
        i_pathlen = strlen( psz_buf );

        if( CFStringGetCString( psz_path,
                                (char*)&psz_buf + i_pathlen,
                                sizeof(psz_buf) - i_pathlen,
                                kCFStringEncodingASCII ) )
        {
            print_debug( dvdcss, "defaulting to drive `%s'", psz_buf );
            CFRelease( psz_path );
280
            IOObjectRelease( next_media );
281 282 283 284 285 286 287
            IOObjectRelease( media_iterator );
            free( dvdcss->psz_device );
            dvdcss->psz_device = strdup( psz_buf );
            return;
        }

        CFRelease( psz_path );
288

289
        IOObjectRelease( next_media );
290 291 292
    }

    IOObjectRelease( media_iterator );
293
#elif defined( __OS2__ )
KO Myung-Hun's avatar
KO Myung-Hun committed
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
    for( i = 0; i < 26; i++ )
    {
        param.bCmdInfo = 0;
        param.bDrive = i;

        rc = DosDevIOCtl( ( HFILE )-1, IOCTL_DISK, DSK_GETDEVICEPARAMS,
                          &param, sizeof( param ), &ulParamLen,
                          &data, sizeof( data ), &ulDataLen );

        if( rc == 0 )
        {
            /* Check for removable and for cylinders */
            if( ( data.usDevAttr & 1 ) == 0 && data.usCylinders == 0xFFFF )
            {
                char psz_dvd[] = "A:";

                psz_dvd[0] += i;

                print_debug( dvdcss, "defaulting to drive `%s'", psz_dvd );
                free( dvdcss->psz_device );
                dvdcss->psz_device = strdup( psz_dvd );
                return;
            }
        }
    }
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
#else
    for( i = 0; ppsz_devices[i]; i++ )
    {
        i_fd = open( ppsz_devices[i], 0 );
        if( i_fd != -1 )
        {
            print_debug( dvdcss, "defaulting to drive `%s'", ppsz_devices[i] );
            close( i_fd );
            free( dvdcss->psz_device );
            dvdcss->psz_device = strdup( ppsz_devices[i] );
            return;
        }
    }
#endif

    print_error( dvdcss, "could not find a suitable default drive" );
}

337
int dvdcss_open_device ( dvdcss_t dvdcss )
338
{
339 340 341 342 343
    const char *psz_device = getenv( "DVDCSS_RAW_DEVICE" );
    if( !psz_device )
    {
         psz_device = dvdcss->psz_device;
    }
344
    print_debug( dvdcss, "opening target `%s'", psz_device );
345

346
#if defined( WIN32 )
347
    dvdcss->p_handle         = NULL;
348 349
    dvdcss->p_readv_buffer   = NULL;
    dvdcss->i_readv_buf_size = 0;
350
#endif /* defined( WIN32 ) */
351

352 353 354 355
#if defined( WIN32 ) || defined( __OS2__ )
    /* If device is "X:" or "X:\", we are not actually opening a file. */
    if( psz_device[0] && psz_device[1] == ':' &&
       ( !psz_device[2] || ( psz_device[2] == '\\' && !psz_device[3] ) ) )
356
    {
357
#if defined( WIN32 )
358
        print_debug( dvdcss, "using Win2K API for access" );
359 360
        dvdcss->pf_seek  = win2k_seek;
        dvdcss->pf_read  = win2k_read;
361
        dvdcss->pf_readv = win2k_readv;
362
        return win2k_open( dvdcss, psz_device );
363
#elif defined( __OS2__ )
364
        print_debug( dvdcss, "using OS/2 API for access" );
365 366 367
        dvdcss->pf_seek  = libc_seek;
        dvdcss->pf_read  = libc_read;
        dvdcss->pf_readv = libc_readv;
KO Myung-Hun's avatar
KO Myung-Hun committed
368
        return os2_open( dvdcss, psz_device );
369
#endif /* ! ( defined( WIN32 ) || defined( __OS2__ ) ) */
KO Myung-Hun's avatar
KO Myung-Hun committed
370 371
    }
    else
372
#endif /* defined( WIN32 ) || defined( __OS2__ ) */
373
    {
374
        print_debug( dvdcss, "using libc for access" );
375 376 377 378 379
        dvdcss->pf_seek  = libc_seek;
        dvdcss->pf_read  = libc_read;
        dvdcss->pf_readv = libc_readv;
        return libc_open( dvdcss, psz_device );
    }
380 381
}

382
int dvdcss_close_device ( dvdcss_t dvdcss )
383 384 385
{
#if defined( WIN32 )
    /* Free readv temporary buffer */
386 387 388
    free( dvdcss->p_readv_buffer );
    dvdcss->p_readv_buffer   = NULL;
    dvdcss->i_readv_buf_size = 0;
389

390
    if( dvdcss->p_handle )
391
    {
392
        CloseHandle( dvdcss->p_handle );
393
    }
394
    else
395
#endif
396 397 398 399 400 401 402 403
    {
        int i_ret = close( dvdcss->i_fd );
        if( i_ret < 0 )
        {
            print_error( dvdcss, "Failed to close fd, data loss possible." );
            return i_ret;
        }
    }
404 405 406 407

    return 0;
}

408
/* Following functions are local */
409

410 411 412
/*****************************************************************************
 * Open commands.
 *****************************************************************************/
413
static int libc_open ( dvdcss_t dvdcss, const char *psz_device )
414
{
415
    dvdcss->i_fd = open( psz_device, O_BINARY );
416

417
    if( dvdcss->i_fd == -1 )
418
    {
419 420 421
        print_debug( dvdcss, "cannot open %s (%s)",
                             psz_device, strerror(errno) );
        print_error( dvdcss, "failed to open device" );
422
        return -1;
423 424
    }

425
    return 0;
426 427 428
}

#if defined( WIN32 )
429
static int win2k_open ( dvdcss_t dvdcss, const char *psz_device )
430
{
431 432
    char psz_dvd[7] = "\\\\.\\\0:";
    psz_dvd[4] = psz_device[0];
433 434 435 436

    /* To work around an M$ bug in IOCTL_DVD_READ_STRUCTURE, we need read
     * _and_ write access to the device (so we can make SCSI Pass Through
     * Requests). Unfortunately this is only allowed if you have
437
     * administrator privileges so we allow for a fallback method with
438 439 440 441
     * only read access to the device (in this case ioctl_ReadCopyright()
     * won't send back the right result).
     * (See Microsoft Q241374: Read and Write Access Required for SCSI
     * Pass Through Requests) */
442 443 444 445
    dvdcss->p_handle = CreateFile( psz_dvd, GENERIC_READ | GENERIC_WRITE,
                                   FILE_SHARE_READ | FILE_SHARE_WRITE,
                                   NULL, OPEN_EXISTING,
                                   FILE_FLAG_RANDOM_ACCESS, NULL );
446

447 448 449 450
    if( dvdcss->p_handle == INVALID_HANDLE_VALUE )
        dvdcss->p_handle = CreateFile( psz_dvd, GENERIC_READ, FILE_SHARE_READ,
                                       NULL, OPEN_EXISTING,
                                       FILE_FLAG_RANDOM_ACCESS, NULL );
451

452
    if( dvdcss->p_handle == INVALID_HANDLE_VALUE )
453
    {
454 455
        print_error( dvdcss, "failed opening device" );
        return -1;
456 457
    }

458 459
    dvdcss->i_pos = 0;

460
    return 0;
461
}
462
#endif /* defined( WIN32 ) */
463

464
#ifdef __OS2__
465
static int os2_open ( dvdcss_t dvdcss, const char *psz_device )
KO Myung-Hun's avatar
KO Myung-Hun committed
466 467 468 469 470 471 472 473
{
    char  psz_dvd[] = "X:";
    HFILE hfile;
    ULONG ulAction;
    ULONG rc;

    psz_dvd[0] = psz_device[0];

474 475 476 477
    rc = DosOpenL( ( PSZ )psz_dvd, &hfile, &ulAction, 0, FILE_NORMAL,
                   OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_FAIL_IF_NEW,
                   OPEN_ACCESS_READONLY | OPEN_SHARE_DENYNONE | OPEN_FLAGS_DASD,
                   NULL );
KO Myung-Hun's avatar
KO Myung-Hun committed
478 479 480 481 482 483 484

    if( rc )
    {
        print_error( dvdcss, "failed to open device" );
        return -1;
    }

485 486
    setmode( hfile, O_BINARY );

487
    dvdcss->i_fd = hfile;
KO Myung-Hun's avatar
KO Myung-Hun committed
488 489 490 491 492

    dvdcss->i_pos = 0;

    return 0;
}
493
#endif /* __OS2__ */
KO Myung-Hun's avatar
KO Myung-Hun committed
494

495
/*****************************************************************************
496
 * Seek commands.
497
 *****************************************************************************/
498
static int libc_seek( dvdcss_t dvdcss, int i_blocks )
499
{
500
    off_t   i_seek;
501

502 503 504 505 506 507
    if( dvdcss->i_pos == i_blocks )
    {
        /* We are already in position */
        return i_blocks;
    }

508
    i_seek = i_blocks * DVDCSS_BLOCK_SIZE;
509
    i_seek = lseek( dvdcss->i_fd, i_seek, SEEK_SET );
510

511
    if( i_seek < 0 )
512
    {
513
        print_error( dvdcss, "seek error" );
514
        dvdcss->i_pos = -1;
515
        return (int) i_seek;
516 517
    }

518
    dvdcss->i_pos = (int) (i_seek / DVDCSS_BLOCK_SIZE);
519 520

    return dvdcss->i_pos;
521 522
}

523 524 525
#if defined( WIN32 )
static int win2k_seek( dvdcss_t dvdcss, int i_blocks )
{
526
    LARGE_INTEGER li_seek;
527

528 529 530 531 532 533
    if( dvdcss->i_pos == i_blocks )
    {
        /* We are already in position */
        return i_blocks;
    }

534
    li_seek.QuadPart = (LONGLONG)i_blocks * DVDCSS_BLOCK_SIZE;
535

536
    li_seek.LowPart = SetFilePointer( dvdcss->p_handle, li_seek.LowPart,
537 538
                                      &li_seek.HighPart, FILE_BEGIN );
    if( (li_seek.LowPart == INVALID_SET_FILE_POINTER)
539 540
        && GetLastError() != NO_ERROR)
    {
541 542
        dvdcss->i_pos = -1;
        return -1;
543 544
    }

545
    dvdcss->i_pos = (int) (li_seek.QuadPart / DVDCSS_BLOCK_SIZE);
546 547

    return dvdcss->i_pos;
548
}
549
#endif /* defined( WIN32 ) */
550 551

/*****************************************************************************
552
 * Read commands.
553
 *****************************************************************************/
554 555
static int libc_read ( dvdcss_t dvdcss, void *p_buffer, int i_blocks )
{
556
    ssize_t i_size, i_ret, i_ret_blocks;
557

558
    i_size = i_blocks * DVDCSS_BLOCK_SIZE;
559
    i_ret = read( dvdcss->i_fd, p_buffer, i_size );
560

561 562
    if( i_ret < 0 )
    {
563
        print_error( dvdcss, "read error" );
564
        dvdcss->i_pos = -1;
565 566 567
        return i_ret;
    }

568 569
    i_ret_blocks = i_ret / DVDCSS_BLOCK_SIZE;

570
    /* Handle partial reads */
Sam Hocevar's avatar
Sam Hocevar committed
571
    if( i_ret != i_size )
572
    {
573
        int i_seek;
574

575
        dvdcss->i_pos = -1;
576
        i_seek = libc_seek( dvdcss, i_ret_blocks );
577 578 579 580
        if( i_seek < 0 )
        {
            return i_seek;
        }
581

582
        /* We have to return now so that i_pos isn't clobbered */
583
        return i_ret_blocks;
584 585
    }

586 587
    dvdcss->i_pos += i_ret_blocks;
    return i_ret_blocks;
588 589 590 591 592
}

#if defined( WIN32 )
static int win2k_read ( dvdcss_t dvdcss, void *p_buffer, int i_blocks )
{
593
    DWORD i_bytes;
594

595 596
    if( !ReadFile( dvdcss->p_handle, p_buffer,i_blocks * DVDCSS_BLOCK_SIZE,
                   &i_bytes, NULL ) )
597
    {
598
        dvdcss->i_pos = -1;
599 600 601
        return -1;
    }

602 603 604
    i_bytes /= DVDCSS_BLOCK_SIZE;
    dvdcss->i_pos += i_bytes;
    return i_bytes;
605
}
606
#endif /* defined( WIN32 ) */
607 608 609 610

/*****************************************************************************
 * Readv commands.
 *****************************************************************************/
611 612
static int libc_readv ( dvdcss_t dvdcss, const struct iovec *p_iovec,
                        int i_blocks )
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
{
#if defined( WIN32 )
    int i_index, i_len, i_total = 0;
    unsigned char *p_base;
    int i_bytes;

    for( i_index = i_blocks;
         i_index;
         i_index--, p_iovec++ )
    {
        i_len  = p_iovec->iov_len;
        p_base = p_iovec->iov_base;

        if( i_len <= 0 )
        {
            continue;
        }

        i_bytes = read( dvdcss->i_fd, p_base, i_len );

        if( i_bytes < 0 )
        {
            /* One of the reads failed, too bad.
636 637
             * We won't even bother returning the reads that went OK,
             * and as in the POSIX spec the file position is left
638
             * unspecified after a failure */
639
            dvdcss->i_pos = -1;
640 641 642 643
            return -1;
        }

        i_total += i_bytes;
644
        i_total /= DVDCSS_BLOCK_SIZE;
645 646 647 648 649

        if( i_bytes != i_len )
        {
            /* We reached the end of the file or a signal interrupted
             * the read. Return a partial read. */
650 651 652
            int i_seek;

            dvdcss->i_pos = -1;
653
            i_seek = libc_seek( dvdcss, i_total );
654 655 656 657 658 659
            if( i_seek < 0 )
            {
                return i_seek;
            }

            /* We have to return now so that i_pos isn't clobbered */
660
            return i_total;
661 662 663
        }
    }

664 665
    dvdcss->i_pos += i_total;
    return i_total;
666
#else
667
    int i_read = readv( dvdcss->i_fd, p_iovec, i_blocks );
668

669 670
    if( i_read < 0 )
    {
671
        dvdcss->i_pos = -1;
672 673 674
        return i_read;
    }

675 676 677
    i_read /= DVDCSS_BLOCK_SIZE;
    dvdcss->i_pos += i_read;
    return i_read;
678 679 680 681 682
#endif
}

#if defined( WIN32 )
/*****************************************************************************
683
 * win2k_readv: vectored read using ReadFile for Win2K
684
 *****************************************************************************/
685 686
static int win2k_readv ( dvdcss_t dvdcss, const struct iovec *p_iovec,
                         int i_blocks )
687 688 689
{
    int i_index;
    int i_blocks_read, i_blocks_total = 0;
690
    DWORD i_bytes;
691 692 693 694 695 696 697

    /* Check the size of the readv temp buffer, just in case we need to
     * realloc something bigger */
    if( dvdcss->i_readv_buf_size < i_blocks * DVDCSS_BLOCK_SIZE )
    {
        dvdcss->i_readv_buf_size = i_blocks * DVDCSS_BLOCK_SIZE;

698
        free( dvdcss->p_readv_buffer );
699 700 701 702 703 704

        /* Allocate a buffer which will be used as a temporary storage
         * for readv */
        dvdcss->p_readv_buffer = malloc( dvdcss->i_readv_buf_size );
        if( !dvdcss->p_readv_buffer )
        {
705
            print_error( dvdcss, " failed (readv)" );
706
            dvdcss->i_pos = -1;
707 708 709 710 711 712
            return -1;
        }
    }

    for( i_index = i_blocks; i_index; i_index-- )
    {
713
        i_blocks_total += p_iovec[i_index-1].iov_len;
714 715 716 717
    }

    if( i_blocks_total <= 0 ) return 0;

718
    if( !ReadFile( dvdcss->p_handle, dvdcss->p_readv_buffer,
719
                   i_blocks_total, &i_bytes, NULL ) )
720
    {
721 722 723 724 725
        /* The read failed... too bad.
         * As in the POSIX spec the file position is left
         * unspecified after a failure */
        dvdcss->i_pos = -1;
        return -1;
726
    }
727
    i_blocks_read = i_bytes / DVDCSS_BLOCK_SIZE;
728 729 730 731 732 733 734 735 736 737 738 739 740 741

    /* We just have to copy the content of the temp buffer into the iovecs */
    for( i_index = 0, i_blocks_total = i_blocks_read;
         i_blocks_total > 0;
         i_index++ )
    {
        memcpy( p_iovec[i_index].iov_base,
                dvdcss->p_readv_buffer + (i_blocks_read - i_blocks_total)
                                           * DVDCSS_BLOCK_SIZE,
                p_iovec[i_index].iov_len );
        /* if we read less blocks than asked, we'll just end up copying
         * garbage, this isn't an issue as we return the number of
         * blocks actually read */
        i_blocks_total -= ( p_iovec[i_index].iov_len / DVDCSS_BLOCK_SIZE );
742
    }
743

744
    dvdcss->i_pos += i_blocks_read;
745 746
    return i_blocks_read;
}
747
#endif /* defined( WIN32 ) */