disc.c 18.4 KB
Newer Older
Petri Hintukainen's avatar
Petri Hintukainen committed
1 2
/*
 * This file is part of libbluray
3
 * Copyright (C) 2014-2017  Petri Hintukainen <phintuka@users.sourceforge.net>
Petri Hintukainen's avatar
Petri Hintukainen committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * 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
#include "dec.h"
27
#include "properties.h"
npzacs's avatar
npzacs committed
28

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

37
#include <ctype.h>
38
#include <stdio.h>
Petri Hintukainen's avatar
Petri Hintukainen committed
39 40
#include <string.h>

41 42
#include "udf_fs.h"

Petri Hintukainen's avatar
Petri Hintukainen committed
43
struct bd_disc {
44
    BD_MUTEX  ovl_mutex;     /* protect access to overlay root */
45
    BD_MUTEX  properties_mutex; /* protect access to properties file */
46 47 48

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

    BD_DEC   *dec;
51 52 53 54 55

    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 *);
56 57

    const char   *udf_volid;
58
    char         *properties_file;  /* NULL if not yet used */
59 60

    int8_t        avchd;  /* -1 - unknown. 0 - no. 1 - yes */
Petri Hintukainen's avatar
Petri Hintukainen committed
61 62
};

63 64 65 66 67
/*
 * BD-ROM filesystem
 */

static BD_FILE_H *_bdrom_open_path(void *p, const char *rel_path)
npzacs's avatar
npzacs committed
68
{
69
    BD_DISC *disc = (BD_DISC *)p;
npzacs's avatar
npzacs committed
70
    BD_FILE_H *fp;
71
    char *abs_path;
npzacs's avatar
npzacs committed
72

73
    abs_path = str_printf("%s%s", disc->disc_root, rel_path);
74 75 76 77
    if (!abs_path) {
        return NULL;
    }

78 79 80 81 82 83
    fp = file_open(abs_path, "rb");
    X_FREE(abs_path);

    return fp;
}

84
static BD_DIR_H *_bdrom_open_dir(void *p, const char *dir)
85
{
86
    BD_DISC *disc = (BD_DISC *)p;
87 88 89
    BD_DIR_H *dp;
    char *path;

90
    path = str_printf("%s%s", disc->disc_root, dir);
91 92 93 94
    if (!path) {
        return NULL;
    }

95 96 97 98 99 100
    dp = dir_open(path);
    X_FREE(path);

    return dp;
}

101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
/*
 * AVCHD 8.3 filenames
 */

static char *_avchd_file_name(const char *rel_path)
{
    static const char map[][2][6] = {
        { ".mpls", ".MPL" },
        { ".clpi", ".CPI" },
        { ".m2ts", ".MTS" },
        { ".bdmv", ".BDM" },
    };
    char *avchd_path = str_dup(rel_path);
    char *name = avchd_path ? strrchr(avchd_path, DIR_SEP_CHAR) : NULL;
    char *dot = name ? strrchr(name, '.') : NULL;
    size_t i;

    if (dot) {

        /* take up to 8 chars from file name */
        for (i = 0; *name && name < dot && i < 9; i++, name++) {
            *name = toupper(*name);
        }

        /* convert extension */
        for (i = 0; i < sizeof(map) / sizeof(map[0]); i++) {
            if (!strcmp(dot, map[i][0])) {
                strcpy(name, map[i][1]);
                return avchd_path;
            }
        }
    }

    /* failed */
    X_FREE(avchd_path);
    return NULL;
}

npzacs's avatar
npzacs committed
139
/*
140 141 142 143 144 145 146 147 148 149 150
 * 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);
151 152 153 154
        if (abs_path) {
            fp = file_open(abs_path, "rb");
            X_FREE(abs_path);
        }
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
    }

    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);
170 171 172 173
        if (abs_path) {
            dp = dir_open_default()(abs_path);
            X_FREE(abs_path);
        }
174 175 176 177 178 179 180 181 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 213 214 215 216 217 218 219 220 221 222 223
    }

    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 */
224
    priv = realloc(dp->internal, sizeof(*priv) + priv->count * sizeof(BD_DIRENT));
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
    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));
242 243 244 245
        if (!dp->internal) {
            X_FREE(dp);
            goto out;
        }
246 247 248 249 250 251 252 253

        while (!dir_read(ovl, &entry)) {
            _comb_dir_append(dp, &entry);
        }
        while (!dir_read(rom, &entry)) {
            _comb_dir_append(dp, &entry);
        }
    }
254 255

 out:
256 257 258 259 260 261 262 263
    dir_close(ovl);
    dir_close(rom);

    return dp;
}

/*
 * disc open / close
npzacs's avatar
npzacs committed
264 265
 */

266
static BD_DISC *_disc_init()
Petri Hintukainen's avatar
Petri Hintukainen committed
267 268 269
{
    BD_DISC *p = calloc(1, sizeof(BD_DISC));
    if (p) {
270
        bd_mutex_init(&p->ovl_mutex);
271
        bd_mutex_init(&p->properties_mutex);
272 273 274 275 276

        /* 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;
277 278

        p->avchd = -1;
279 280 281
    }
    return p;
}
Petri Hintukainen's avatar
Petri Hintukainen committed
282

283 284 285
static void _set_paths(BD_DISC *p, const char *device_path)
{
    if (device_path) {
Petri Hintukainen's avatar
Petri Hintukainen committed
286 287 288
        char *disc_root = mount_get_mountpoint(device_path);

        /* make sure path ends to slash */
289
        if (!disc_root || (disc_root[0] && disc_root[strlen(disc_root) - 1] == DIR_SEP_CHAR)) {
Petri Hintukainen's avatar
Petri Hintukainen committed
290 291 292 293 294
            p->disc_root = disc_root;
        } else {
            p->disc_root = str_printf("%s%c", disc_root, DIR_SEP_CHAR);
            X_FREE(disc_root);
        }
295 296
    }
}
npzacs's avatar
npzacs committed
297

298
BD_DISC *disc_open(const char *device_path,
299
                   fs_access *p_fs,
300 301 302 303 304
                   struct bd_enc_info *enc_info,
                   const char *keyfile_path,
                   void *regs, void *psr_read, void *psr_write)
{
    BD_DISC *p = _disc_init();
305

Petri Hintukainen's avatar
Petri Hintukainen committed
306 307 308
    if (!p) {
        return NULL;
    }
309

Petri Hintukainen's avatar
Petri Hintukainen committed
310 311 312 313 314
    if (p_fs && p_fs->open_dir) {
        p->fs_handle          = p_fs->fs_handle;
        p->pf_file_open_bdrom = p_fs->open_file;
        p->pf_dir_open_bdrom  = p_fs->open_dir;
    }
315

Petri Hintukainen's avatar
Petri Hintukainen committed
316
    _set_paths(p, device_path);
317

Petri Hintukainen's avatar
Petri Hintukainen committed
318 319 320 321 322 323
    /* check if disc root directory can be opened. If not, treat it as device/image file. */
    BD_DIR_H *dp_img = device_path ? dir_open(device_path) : NULL;
    if (!dp_img) {
        void *udf = udf_image_open(device_path, p_fs ? p_fs->fs_handle : NULL, p_fs ? p_fs->read_blocks : NULL);
        if (!udf) {
            BD_DEBUG(DBG_FILE | DBG_CRIT, "failed opening UDF image %s\n", device_path);
324
        } else {
Petri Hintukainen's avatar
Petri Hintukainen committed
325 326 327 328 329 330
            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;

            p->udf_volid = udf_volume_id(udf);
331

Petri Hintukainen's avatar
Petri Hintukainen committed
332 333 334 335 336 337
            /* root not accessible with stdio */
            X_FREE(p->disc_root);
        }
    } else {
        dir_close(dp_img);
        BD_DEBUG(DBG_FILE, "%s does not seem to be image file or device node\n", device_path);
Petri Hintukainen's avatar
Petri Hintukainen committed
338 339
    }

Petri Hintukainen's avatar
Petri Hintukainen committed
340 341 342
    struct dec_dev dev = { p->fs_handle, p->pf_file_open_bdrom, p, (file_openFp)disc_open_path, p->disc_root, device_path };
    p->dec = dec_init(&dev, enc_info, keyfile_path, regs, psr_read, psr_write);

Petri Hintukainen's avatar
Petri Hintukainen committed
343 344 345 346 347 348 349
    return p;
}

void disc_close(BD_DISC **pp)
{
    if (pp && *pp) {
        BD_DISC *p = *pp;
npzacs's avatar
npzacs committed
350 351 352

        dec_close(&p->dec);

353 354 355 356
        if (p->pf_fs_close) {
            p->pf_fs_close(p->fs_handle);
        }

357
        bd_mutex_destroy(&p->ovl_mutex);
358
        bd_mutex_destroy(&p->properties_mutex);
359

Petri Hintukainen's avatar
Petri Hintukainen committed
360
        X_FREE(p->disc_root);
361
        X_FREE(p->properties_file);
Petri Hintukainen's avatar
Petri Hintukainen committed
362 363 364 365 366 367 368 369
        X_FREE(*pp);
    }
}

/*
 *
 */

370
const char *disc_root(BD_DISC *p)
Petri Hintukainen's avatar
Petri Hintukainen committed
371 372 373 374
{
    return p->disc_root;
}

375 376 377 378 379
const char *disc_volume_id(BD_DISC *p)
{
    return p ? p->udf_volid : NULL;
}

380 381 382 383 384
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);
}

385 386 387
/*
 * VFS
 */
Petri Hintukainen's avatar
Petri Hintukainen committed
388 389 390 391 392

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

393 394 395 396 397 398 399 400 401 402 403 404
    if (p->avchd > 0) {
        char *avchd_path = _avchd_file_name(rel_path);
        if (avchd_path) {
            BD_DEBUG(DBG_FILE, "AVCHD: %s -> %s\n", rel_path, avchd_path);
            fp = p->pf_file_open_bdrom(p->fs_handle, avchd_path);
            X_FREE(avchd_path);
            if (fp) {
                return fp;
            }
        }
    }

405 406
    /* search file from overlay */
    fp = _overlay_open_path(p, rel_path);
Petri Hintukainen's avatar
Petri Hintukainen committed
407

408
    /* if not found, try BD-ROM */
Petri Hintukainen's avatar
Petri Hintukainen committed
409
    if (!fp) {
410
        fp = p->pf_file_open_bdrom(p->fs_handle, rel_path);
Petri Hintukainen's avatar
Petri Hintukainen committed
411

412
        if (!fp) {
413 414 415 416 417 418 419 420 421 422 423 424 425

            /* AVCHD short filenames detection */
            if (p->avchd < 0 && !strcmp(rel_path, "BDMV" DIR_SEP "index.bdmv")) {
                fp = p->pf_file_open_bdrom(p->fs_handle, "BDMV" DIR_SEP "INDEX.BDM");
                if (fp) {
                    BD_DEBUG(DBG_FILE | DBG_CRIT, "detected AVCHD 8.3 filenames\n");
                }
                p->avchd = !!fp;
            }

            if (!fp) {
                BD_DEBUG(DBG_FILE | DBG_CRIT, "error opening file %s\n", rel_path);
            }
426 427
        }
    }
Petri Hintukainen's avatar
Petri Hintukainen committed
428 429 430 431 432 433 434 435 436

    return fp;
}

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

437
    path = str_printf("%s" DIR_SEP "%s", dir, file);
438 439 440 441
    if (!path) {
        return NULL;
    }

442
    fp = disc_open_path(p, path);
Petri Hintukainen's avatar
Petri Hintukainen committed
443 444 445 446 447 448 449
    X_FREE(path);

    return fp;
}

BD_DIR_H *disc_open_dir(BD_DISC *p, const char *dir)
{
450 451
    BD_DIR_H *dp_rom;
    BD_DIR_H *dp_ovl;
Petri Hintukainen's avatar
Petri Hintukainen committed
452

453
    dp_rom = p->pf_dir_open_bdrom(p->fs_handle, dir);
454
    dp_ovl = _overlay_open_dir(p, dir);
Petri Hintukainen's avatar
Petri Hintukainen committed
455

456 457 458 459 460 461 462 463
    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
464 465
    }

466
    return _combine_dirs(dp_ovl, dp_rom);
Petri Hintukainen's avatar
Petri Hintukainen committed
467 468
}

469
size_t disc_read_file(BD_DISC *disc, const char *dir, const char *file,
Petri Hintukainen's avatar
Petri Hintukainen committed
470 471 472 473 474 475 476
                       uint8_t **data)
{
    BD_FILE_H *fp;
    int64_t    size;

    *data = NULL;

477 478 479 480 481
    if (dir) {
        fp = disc_open_file(disc, dir, file);
    } else {
        fp = disc_open_path(disc, file);
    }
Petri Hintukainen's avatar
Petri Hintukainen committed
482
    if (!fp) {
483
        return 0;
Petri Hintukainen's avatar
Petri Hintukainen committed
484 485 486
    }

    size = file_size(fp);
487 488
    if (size > 0 && size < BD_MAX_SSIZE) {
        *data = malloc((size_t)size);
Petri Hintukainen's avatar
Petri Hintukainen committed
489 490 491 492 493
        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);
494
                size = 0;
Petri Hintukainen's avatar
Petri Hintukainen committed
495 496
            }
        } else {
497
          size = 0;
Petri Hintukainen's avatar
Petri Hintukainen committed
498 499
        }
    }
500 501 502
    else {
      size = 0;
    }
Petri Hintukainen's avatar
Petri Hintukainen committed
503 504

    file_close(fp);
505
    return (size_t)size;
Petri Hintukainen's avatar
Petri Hintukainen committed
506
}
npzacs's avatar
npzacs committed
507 508

/*
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
 * 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);
}

524 525 526 527 528
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;
529
    size_t     size;
530

531 532 533 534 535 536 537 538 539 540 541 542
    if (!cache_path || !cache_path[0]) {
        return -1;
    }

    /* make sure cache directory exists */
    if (file_mkdirs(cache_path) < 0) {
        return -1;
    }

    /* plain directory ? */
    size = strlen(rel_path);
    if (rel_path[size - 1] == '/' || rel_path[size - 1] == '\\') {
Petri Hintukainen's avatar
Petri Hintukainen committed
543 544 545
        return 0;
    }

546 547 548 549 550 551 552 553
    /* 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;
    }

    /* output file in local filesystem */
554
    fp_out = file_open(cache_path, "wb");
555 556 557 558 559 560
    if (!fp_out) {
        BD_DEBUG(DBG_FILE | DBG_CRIT, "error creating cache file %s\n", cache_path);
        file_close(fp_in);
        return -1;
    }

561
    do {
562 563
        uint8_t buf[16*2048];
        got = file_read(fp_in, buf, sizeof(buf));
564 565 566

        /* we'll call write(fp, buf, 0) after EOF. It is used to check for errors. */
        if (got < 0 || fp_out->write(fp_out, buf, got) != got) {
567 568 569
            BD_DEBUG(DBG_FILE | DBG_CRIT, "error caching file %s\n", rel_path);
            file_close(fp_out);
            file_close(fp_in);
570
            (void)file_unlink(cache_path);
571 572
            return -1;
        }
573 574
    } while (got > 0);

575 576 577 578 579 580 581
    BD_DEBUG(DBG_FILE, "cached %s to %s\n", rel_path, cache_path);

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

582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
/*
 * persistent properties storage
 */

static char *_properties_file(BD_DISC *p)
{
    const uint8_t *disc_id = NULL;
    uint8_t  pseudo_id[20];
    char     id_type, id_str[41];
    char    *cache_home;
    char    *properties_file;

    /* get disc ID */
    if (p->dec) {
        id_type = 'A';
        disc_id = dec_disc_id(p->dec);
    }
    if (!disc_id) {
        id_type = 'P';
601 602 603 604 605 606 607 608 609 610 611
        if (disc_pseudo_id(p, pseudo_id) > 0) {
            disc_id = pseudo_id;
        }
    }
    if (!disc_id) {
        return NULL;
    }

    cache_home = file_get_cache_home();
    if (!cache_home) {
        return NULL;
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
    }

    properties_file = str_printf("%s" DIR_SEP "bluray" DIR_SEP "properties" DIR_SEP "%c%s",
                                 cache_home, id_type,
                                 str_print_hex(id_str, disc_id, 20));

    X_FREE(cache_home);

    return properties_file;
}

static int _ensure_properties_file(BD_DISC *p)
{
    bd_mutex_lock(&p->properties_mutex);
    if (!p->properties_file) {
        p->properties_file = _properties_file(p);
    }
    bd_mutex_unlock(&p->properties_mutex);

    return p->properties_file ? 0 : -1;
}

int disc_property_put(BD_DISC *p, const char *property, const char *val)
{
    int result;

    if (_ensure_properties_file(p) < 0) {
        return -1;
    }

    bd_mutex_lock(&p->properties_mutex);
    result = properties_put(p->properties_file, property, val);
    bd_mutex_unlock(&p->properties_mutex);

    return result;
}

char *disc_property_get(BD_DISC *p, const char *property)
{
    char *result;

    if (_ensure_properties_file(p) < 0) {
        return NULL;
    }

    bd_mutex_lock(&p->properties_mutex);
    result = properties_get(p->properties_file, property);
    bd_mutex_unlock(&p->properties_mutex);

    return result;
}

664 665
/*
 * streams
npzacs's avatar
npzacs committed
666 667 668 669
 */

BD_FILE_H *disc_open_stream(BD_DISC *disc, const char *file)
{
Petri Hintukainen's avatar
Petri Hintukainen committed
670 671 672 673 674 675 676 677 678 679 680 681 682
    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
683 684 685 686 687 688 689
}

const uint8_t *disc_get_data(BD_DISC *disc, int type)
{
    if (disc->dec) {
        return dec_data(disc->dec, type);
    }
690 691 692 693 694 695 696 697 698
    if (type == 0x1000) {
        /* this shouldn't cause any extra optical disc access */
        BD_DIR_H *d = disc->pf_dir_open_bdrom(disc->fs_handle, "MAKEMKV");
        if (d) {
            dir_close(d);
            BD_DEBUG(DBG_FILE, "Detected MakeMKV backup data\n");
            return (const uint8_t *)"mmbd;backup";
        }
    }
npzacs's avatar
npzacs committed
699 700 701 702 703
    return NULL;
}

void disc_event(BD_DISC *disc, uint32_t event, uint32_t param)
{
Petri Hintukainen's avatar
Petri Hintukainen committed
704
    if (disc && disc->dec) {
npzacs's avatar
npzacs committed
705 706 707 708 709 710 711 712 713 714 715 716 717 718
        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;
        }
    }
}

719 720 721 722 723
/*
 * Pseudo disc ID
 * This is used when AACS disc ID is not available
 */

Petri Hintukainen's avatar
Petri Hintukainen committed
724
#define ROTL64(k, n)  (((k) << (n)) | ((k) >> (64 - (n))))
725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752

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
753
        k1 *= c1; k1 = ROTL64(k1, 31); k1 *= c2; h[0] ^= k1;
754

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

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

Petri Hintukainen's avatar
Petri Hintukainen committed
759
        h[1] = ROTL64(h[1], 31); h[1] += h[0]; h[1] = h[1] * 5 + 0x38495ab5;
760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
    }

    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;
}

791
int disc_pseudo_id(BD_DISC *p, uint8_t *id/*[20]*/)
792 793
{
    uint8_t h[2][20];
794
    int i, r = 0;
795 796

    memset(h, 0, sizeof(h));
797 798
    r += _hash_file(p, "BDMV", "MovieObject.bdmv", h[0]);
    r += _hash_file(p, "BDMV", "index.bdmv", h[1]);
799 800 801 802

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

    return r > 0;
805
}