disc.c 13.9 KB
Newer Older
Petri Hintukainen's avatar
Petri Hintukainen committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 * This file is part of libbluray
 * Copyright (C) 2014  Petri Hintukainen <phintuka@users.sourceforge.net>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see
 * <http://www.gnu.org/licenses/>.
 */

#if HAVE_CONFIG_H
#include "config.h"
#endif

#include "disc.h"

npzacs's avatar
npzacs committed
26 27
#include "dec.h"

Petri Hintukainen's avatar
Petri Hintukainen committed
28 29
#include "util/logging.h"
#include "util/macro.h"
30
#include "util/mutex.h"
Petri Hintukainen's avatar
Petri Hintukainen committed
31 32 33 34 35 36
#include "util/strutl.h"
#include "file/file.h"
#include "file/mount.h"

#include <string.h>

37 38 39 40
#ifdef ENABLE_UDF
#include "udf_fs.h"
#endif

Petri Hintukainen's avatar
Petri Hintukainen committed
41
struct bd_disc {
42 43 44 45
    BD_MUTEX  ovl_mutex;     /* protect access to overlay root */

    char     *disc_root;     /* disc filesystem root (if disc is mounted) */
    char     *overlay_root;  /* overlay filesystem root (if set) */
npzacs's avatar
npzacs committed
46 47

    BD_DEC   *dec;
48 49 50 51 52

    void         *fs_handle;
    BD_FILE_H * (*pf_file_open_bdrom)(void *, const char *);
    BD_DIR_H *  (*pf_dir_open_bdrom)(void *, const char *);
    void        (*pf_fs_close)(void *);
53 54

    const char   *udf_volid;
Petri Hintukainen's avatar
Petri Hintukainen committed
55 56
};

57 58 59 60 61
/*
 * BD-ROM filesystem
 */

static BD_FILE_H *_bdrom_open_path(void *p, const char *rel_path)
npzacs's avatar
npzacs committed
62
{
63
    BD_DISC *disc = (BD_DISC *)p;
npzacs's avatar
npzacs committed
64
    BD_FILE_H *fp;
65
    char *abs_path;
npzacs's avatar
npzacs committed
66

67
    abs_path = str_printf("%s%s", disc->disc_root, rel_path);
68 69 70 71
    if (!abs_path) {
        return NULL;
    }

72 73 74 75 76 77
    fp = file_open(abs_path, "rb");
    X_FREE(abs_path);

    return fp;
}

78
static BD_DIR_H *_bdrom_open_dir(void *p, const char *dir)
79
{
80
    BD_DISC *disc = (BD_DISC *)p;
81 82 83
    BD_DIR_H *dp;
    char *path;

84
    path = str_printf("%s%s", disc->disc_root, dir);
85 86 87 88
    if (!path) {
        return NULL;
    }

89 90 91 92 93 94
    dp = dir_open(path);
    X_FREE(path);

    return dp;
}

npzacs's avatar
npzacs committed
95
/*
96 97 98 99 100 101 102 103 104 105 106
 * overlay filesystem
 */

static BD_FILE_H *_overlay_open_path(BD_DISC *p, const char *rel_path)
{
    BD_FILE_H *fp = NULL;

    bd_mutex_lock(&p->ovl_mutex);

    if (p->overlay_root) {
        char *abs_path = str_printf("%s%s", p->overlay_root, rel_path);
107 108 109 110
        if (abs_path) {
            fp = file_open(abs_path, "rb");
            X_FREE(abs_path);
        }
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
    }

    bd_mutex_unlock(&p->ovl_mutex);

    return fp;
}

static BD_DIR_H *_overlay_open_dir(BD_DISC *p, const char *dir)
{
    BD_DIR_H *dp = NULL;

    bd_mutex_lock(&p->ovl_mutex);

    if (p->overlay_root) {
        char *abs_path = str_printf("%s%s", p->disc_root, dir);
126 127 128 129
        if (abs_path) {
            dp = dir_open_default()(abs_path);
            X_FREE(abs_path);
        }
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
    }

    bd_mutex_unlock(&p->ovl_mutex);

    return dp;
}

/*
 * directory combining
 */

typedef struct {
    unsigned int count;
    unsigned int pos;
    BD_DIRENT    entry[1]; /* VLA */
} COMB_DIR;

static void _comb_dir_close(BD_DIR_H *dp)
{
    X_FREE(dp->internal);
    X_FREE(dp);
}

static int _comb_dir_read(BD_DIR_H *dp, BD_DIRENT *entry)
{
    COMB_DIR *priv = (COMB_DIR *)dp->internal;
    if (priv->pos < priv->count) {
        strcpy(entry->d_name, priv->entry[priv->pos++].d_name);
        return 0;
    }
    return 1;
}

static void _comb_dir_append(BD_DIR_H *dp, BD_DIRENT *entry)
{
    COMB_DIR *priv = (COMB_DIR *)dp->internal;
    unsigned int i;

    if (!priv) {
        return;
    }

    /* no duplicates */
    for (i = 0; i < priv->count; i++) {
        if (!strcmp(priv->entry[i].d_name, entry->d_name)) {
            return;
        }
    }

    /* append */
    priv = realloc(priv, sizeof(*priv) + priv->count * sizeof(BD_DIRENT));
    if (!priv) {
        return;
    }
    strcpy(priv->entry[priv->count].d_name, entry->d_name);
    priv->count++;
    dp->internal = (void*)priv;
}

static BD_DIR_H *_combine_dirs(BD_DIR_H *ovl, BD_DIR_H *rom)
{
    BD_DIR_H *dp = calloc(1, sizeof(BD_DIR_H));
    BD_DIRENT entry;

    if (dp) {
        dp->read     = _comb_dir_read;
        dp->close    = _comb_dir_close;
        dp->internal = calloc(1, sizeof(COMB_DIR));
198 199 200 201
        if (!dp->internal) {
            X_FREE(dp);
            goto out;
        }
202 203 204 205 206 207 208 209

        while (!dir_read(ovl, &entry)) {
            _comb_dir_append(dp, &entry);
        }
        while (!dir_read(rom, &entry)) {
            _comb_dir_append(dp, &entry);
        }
    }
210 211

 out:
212 213 214 215 216 217 218 219
    dir_close(ovl);
    dir_close(rom);

    return dp;
}

/*
 * disc open / close
npzacs's avatar
npzacs committed
220 221
 */

222
static BD_DISC *_disc_init()
Petri Hintukainen's avatar
Petri Hintukainen committed
223 224 225
{
    BD_DISC *p = calloc(1, sizeof(BD_DISC));
    if (p) {
226 227 228 229 230 231 232 233 234
        bd_mutex_init(&p->ovl_mutex);

        /* default file access functions */
        p->fs_handle          = (void*)p;
        p->pf_file_open_bdrom = _bdrom_open_path;
        p->pf_dir_open_bdrom  = _bdrom_open_dir;
    }
    return p;
}
Petri Hintukainen's avatar
Petri Hintukainen committed
235

236 237 238
static void _set_paths(BD_DISC *p, const char *device_path)
{
    if (device_path) {
Petri Hintukainen's avatar
Petri Hintukainen committed
239 240 241
        char *disc_root = mount_get_mountpoint(device_path);

        /* make sure path ends to slash */
242
        if (!disc_root || (disc_root[0] && disc_root[strlen(disc_root) - 1] == DIR_SEP_CHAR)) {
Petri Hintukainen's avatar
Petri Hintukainen committed
243 244 245 246 247
            p->disc_root = disc_root;
        } else {
            p->disc_root = str_printf("%s%c", disc_root, DIR_SEP_CHAR);
            X_FREE(disc_root);
        }
248 249
    }
}
npzacs's avatar
npzacs committed
250

251
BD_DISC *disc_open(const char *device_path,
Petri Hintukainen's avatar
Petri Hintukainen committed
252 253
                   void *read_blocks_handle,
                   int (*read_blocks)(void *handle, void *buf, int lba, int num_blocks),
254 255 256 257 258
                   struct bd_enc_info *enc_info,
                   const char *keyfile_path,
                   void *regs, void *psr_read, void *psr_write)
{
    BD_DISC *p = _disc_init();
259

260 261
    if (p) {
        _set_paths(p, device_path);
262 263 264

#ifdef ENABLE_UDF
        /* check if disc root directory can be opened. If not, treat it as device/image file. */
Petri Hintukainen's avatar
Petri Hintukainen committed
265
        BD_DIR_H *dp_img = device_path ? dir_open(device_path) : NULL;
266
        if (!dp_img) {
Petri Hintukainen's avatar
Petri Hintukainen committed
267
            void *udf = udf_image_open(device_path, read_blocks_handle, read_blocks);
268 269 270 271 272 273 274 275
            if (!udf) {
                BD_DEBUG(DBG_FILE | DBG_CRIT, "failed opening UDF image %s\n", device_path);
            } else {
                p->fs_handle          = udf;
                p->pf_fs_close        = udf_image_close;
                p->pf_file_open_bdrom = udf_file_open;
                p->pf_dir_open_bdrom  = udf_dir_open;

276 277
                p->udf_volid = udf_volume_id(udf);

278 279 280 281 282
                /* root not accessible with stdio */
                X_FREE(p->disc_root);
            }
        } else {
            dir_close(dp_img);
283
            BD_DEBUG(DBG_FILE, "%s does not seem to be image file or device node\n", device_path);
284
        }
285 286 287
#else
        (void)read_blocks_handle;
        (void)read_blocks;
288 289
#endif

290
        struct dec_dev dev = { p->fs_handle, p->pf_file_open_bdrom, p, (file_openFp)disc_open_path, p->disc_root, device_path };
npzacs's avatar
npzacs committed
291
        p->dec = dec_init(&dev, enc_info, keyfile_path, regs, psr_read, psr_write);
Petri Hintukainen's avatar
Petri Hintukainen committed
292 293 294 295 296 297 298 299 300
    }

    return p;
}

void disc_close(BD_DISC **pp)
{
    if (pp && *pp) {
        BD_DISC *p = *pp;
npzacs's avatar
npzacs committed
301 302 303

        dec_close(&p->dec);

304 305 306 307
        if (p->pf_fs_close) {
            p->pf_fs_close(p->fs_handle);
        }

308 309
        bd_mutex_destroy(&p->ovl_mutex);

Petri Hintukainen's avatar
Petri Hintukainen committed
310 311 312 313 314 315 316 317 318
        X_FREE(p->disc_root);
        X_FREE(*pp);
    }
}

/*
 *
 */

319
const char *disc_root(BD_DISC *p)
Petri Hintukainen's avatar
Petri Hintukainen committed
320 321 322 323
{
    return p->disc_root;
}

324 325 326 327 328
const char *disc_volume_id(BD_DISC *p)
{
    return p ? p->udf_volid : NULL;
}

329 330 331 332 333
BD_DIR_H *disc_open_bdrom_dir(BD_DISC *p, const char *rel_path)
{
    return p->pf_dir_open_bdrom(p->fs_handle, rel_path);
}

334 335 336
/*
 * VFS
 */
Petri Hintukainen's avatar
Petri Hintukainen committed
337 338 339 340 341

BD_FILE_H *disc_open_path(BD_DISC *p, const char *rel_path)
{
    BD_FILE_H *fp;

342 343
    /* search file from overlay */
    fp = _overlay_open_path(p, rel_path);
Petri Hintukainen's avatar
Petri Hintukainen committed
344

345
    /* if not found, try BD-ROM */
Petri Hintukainen's avatar
Petri Hintukainen committed
346
    if (!fp) {
347
        fp = p->pf_file_open_bdrom(p->fs_handle, rel_path);
Petri Hintukainen's avatar
Petri Hintukainen committed
348

349 350 351 352
        if (!fp) {
            BD_DEBUG(DBG_FILE | DBG_CRIT, "error opening file %s\n", rel_path);
        }
    }
Petri Hintukainen's avatar
Petri Hintukainen committed
353 354 355 356 357 358 359 360 361

    return fp;
}

BD_FILE_H *disc_open_file(BD_DISC *p, const char *dir, const char *file)
{
    BD_FILE_H *fp;
    char *path;

362
    path = str_printf("%s" DIR_SEP "%s", dir, file);
363 364 365 366
    if (!path) {
        return NULL;
    }

367
    fp = disc_open_path(p, path);
Petri Hintukainen's avatar
Petri Hintukainen committed
368 369 370 371 372 373 374
    X_FREE(path);

    return fp;
}

BD_DIR_H *disc_open_dir(BD_DISC *p, const char *dir)
{
375 376
    BD_DIR_H *dp_rom;
    BD_DIR_H *dp_ovl;
Petri Hintukainen's avatar
Petri Hintukainen committed
377

378
    dp_rom = p->pf_dir_open_bdrom(p->fs_handle, dir);
379
    dp_ovl = _overlay_open_dir(p, dir);
Petri Hintukainen's avatar
Petri Hintukainen committed
380

381 382 383 384 385 386 387 388
    if (!dp_ovl) {
        if (!dp_rom) {
            BD_DEBUG(DBG_FILE, "error opening dir %s\n", dir);
        }
        return dp_rom;
    }
    if (!dp_rom) {
        return dp_ovl;
Petri Hintukainen's avatar
Petri Hintukainen committed
389 390
    }

391
    return _combine_dirs(dp_ovl, dp_rom);
Petri Hintukainen's avatar
Petri Hintukainen committed
392 393
}

394
size_t disc_read_file(BD_DISC *disc, const char *dir, const char *file,
Petri Hintukainen's avatar
Petri Hintukainen committed
395 396 397 398 399 400 401 402 403
                       uint8_t **data)
{
    BD_FILE_H *fp;
    int64_t    size;

    *data = NULL;

    fp = disc_open_file(disc, dir, file);
    if (!fp) {
404
        return 0;
Petri Hintukainen's avatar
Petri Hintukainen committed
405 406 407
    }

    size = file_size(fp);
408 409
    if (size > 0 && size < BD_MAX_SSIZE) {
        *data = malloc((size_t)size);
Petri Hintukainen's avatar
Petri Hintukainen committed
410 411 412 413 414
        if (*data) {
            int64_t got = file_read(fp, *data, size);
            if (got != size) {
                BD_DEBUG(DBG_FILE | DBG_CRIT, "Error reading file %s from %s\n", file, dir);
                X_FREE(*data);
415
                size = 0;
Petri Hintukainen's avatar
Petri Hintukainen committed
416 417
            }
        } else {
418
          size = 0;
Petri Hintukainen's avatar
Petri Hintukainen committed
419 420
        }
    }
421 422 423
    else {
      size = 0;
    }
Petri Hintukainen's avatar
Petri Hintukainen committed
424 425

    file_close(fp);
426
    return (size_t)size;
Petri Hintukainen's avatar
Petri Hintukainen committed
427
}
npzacs's avatar
npzacs committed
428 429

/*
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
 * filesystem update
 */

void disc_update(BD_DISC *p, const char *overlay_root)
{
    bd_mutex_lock(&p->ovl_mutex);

    X_FREE(p->overlay_root);
    if (overlay_root) {
        p->overlay_root = str_dup(overlay_root);
    }

    bd_mutex_unlock(&p->ovl_mutex);
}

445 446 447 448 449 450 451 452 453 454 455 456 457
int disc_cache_bdrom_file(BD_DISC *p, const char *rel_path, const char *cache_path)
{
    BD_FILE_H *fp_in;
    BD_FILE_H *fp_out;
    int64_t    got;

    /* input file from BD-ROM */
    fp_in = p->pf_file_open_bdrom(p->fs_handle, rel_path);
    if (!fp_in) {
        BD_DEBUG(DBG_FILE | DBG_CRIT, "error caching file %s (does not exist ?)\n", rel_path);
        return -1;
    }

458 459 460
    /* make sure path exists */
    file_mkdirs(cache_path);

461
    /* output file in local filesystem */
462
    fp_out = file_open(cache_path, "wb");
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
    if (!fp_out) {
        BD_DEBUG(DBG_FILE | DBG_CRIT, "error creating cache file %s\n", cache_path);
        file_close(fp_in);
        return -1;
    }

    while (1) {
        uint8_t buf[16*2048];
        got = file_read(fp_in, buf, sizeof(buf));
        if (got <= 0) {
            break;
        }
        if (fp_out->write(fp_out, buf, got) != got) {
            BD_DEBUG(DBG_FILE | DBG_CRIT, "error caching file %s\n", rel_path);
            file_close(fp_out);
            file_close(fp_in);
            file_unlink(cache_path);
            return -1;
        }
    }
    BD_DEBUG(DBG_FILE, "cached %s to %s\n", rel_path, cache_path);

    file_close(fp_out);
    file_close(fp_in);
    return 0;
}

490 491
/*
 * streams
npzacs's avatar
npzacs committed
492 493 494 495
 */

BD_FILE_H *disc_open_stream(BD_DISC *disc, const char *file)
{
Petri Hintukainen's avatar
Petri Hintukainen committed
496 497 498 499 500 501 502 503 504 505 506 507 508
    BD_FILE_H *fp = disc_open_file(disc, "BDMV" DIR_SEP "STREAM", file);
    if (!fp) {
        return NULL;
    }

    if (disc->dec) {
        BD_FILE_H *st = dec_open_stream(disc->dec, fp, atoi(file));
        if (st) {
            return st;
        }
    }

    return fp;
npzacs's avatar
npzacs committed
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
}

const uint8_t *disc_get_data(BD_DISC *disc, int type)
{
    if (disc->dec) {
        return dec_data(disc->dec, type);
    }
    return NULL;
}

void disc_event(BD_DISC *disc, uint32_t event, uint32_t param)
{
    if (disc->dec) {
        switch (event) {
            case DISC_EVENT_START:
                dec_start(disc->dec, param);
                return;
            case DISC_EVENT_TITLE:
                dec_title(disc->dec, param);
                return;
            case DISC_EVENT_APPLICATION:
                dec_application(disc->dec, param);
                return;
        }
    }
}

536 537 538 539 540
/*
 * Pseudo disc ID
 * This is used when AACS disc ID is not available
 */

Petri Hintukainen's avatar
Petri Hintukainen committed
541
#define ROTL64(k, n)  (((k) << (n)) | ((k) >> (64 - (n))))
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569

static uint64_t _fmix64(uint64_t k)
{
    k ^= k >> 33;
    k *= UINT64_C(0xff51afd7ed558ccd);
    k ^= k >> 33;
    k *= UINT64_C(0xc4ceb9fe1a85ec53);
    k ^= k >> 33;
    return k;
}

static void _murmurhash3_128(const uint8_t *in, size_t len, void *out)
{
    // original MurmurHash3 was written by Austin Appleby, and is placed in the public domain.
    // https://code.google.com/p/smhasher/wiki/MurmurHash3
    const uint64_t c1 = UINT64_C(0x87c37b91114253d5);
    const uint64_t c2 = UINT64_C(0x4cf5ad432745937f);
    uint64_t h[2] = {0, 0};
    size_t i;

    /* use only N * 16 bytes, ignore tail */
    len &= ~15;

    for (i = 0; i < len; i += 16) {
        uint64_t k1, k2;
        memcpy(&k1, in + i,     sizeof(uint64_t));
        memcpy(&k2, in + i + 8, sizeof(uint64_t));

Petri Hintukainen's avatar
Petri Hintukainen committed
570
        k1 *= c1; k1 = ROTL64(k1, 31); k1 *= c2; h[0] ^= k1;
571

Petri Hintukainen's avatar
Petri Hintukainen committed
572
        h[0] = ROTL64(h[0], 27); h[0] += h[1]; h[0] = h[0] * 5 + 0x52dce729;
573

Petri Hintukainen's avatar
Petri Hintukainen committed
574
        k2 *= c2; k2 = ROTL64(k2, 33); k2 *= c1; h[1] ^= k2;
575

Petri Hintukainen's avatar
Petri Hintukainen committed
576
        h[1] = ROTL64(h[1], 31); h[1] += h[0]; h[1] = h[1] * 5 + 0x38495ab5;
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
    }

    h[0] ^= len;
    h[1] ^= len;

    h[0] += h[1];
    h[1] += h[0];

    h[0] = _fmix64(h[0]);
    h[1] = _fmix64(h[1]);

    h[0] += h[1];
    h[1] += h[0];

    memcpy(out, h, 2*sizeof(uint64_t));
}

static int _hash_file(BD_DISC *p, const char *dir, const char *file, void *hash)
{
    uint8_t *data = NULL;
    size_t sz;

    sz = disc_read_file(p, dir, file, &data);
    if (sz > 16) {
        _murmurhash3_128(data, sz, hash);
    }

    X_FREE(data);
    return sz > 16;
}

BD_PRIVATE void disc_pseudo_id(BD_DISC *p, uint8_t *id/*[20]*/)
{
    uint8_t h[2][20];
    int i;

    memset(h, 0, sizeof(h));
    _hash_file(p, "BDMV", "MovieObject.bdmv", h[0]);
    _hash_file(p, "BDMV", "index.bdmv", h[1]);

    for (i = 0; i < 20; i++) {
        id[i] = h[0][i] ^ h[1][i];
    }
}