bluray.c 2.26 KB
Newer Older
cRTrn13's avatar
cRTrn13 committed
1

cRTrn13's avatar
cRTrn13 committed
2
#include <dlfcn.h>
cRTrn13's avatar
cRTrn13 committed
3
#include <stdio.h>
cRTrn13's avatar
cRTrn13 committed
4

cRTrn13's avatar
cRTrn13 committed
5
#include "bluray.h"
cRTrn13's avatar
cRTrn13 committed
6
#include "util/macro.h"
cRTrn13's avatar
cRTrn13 committed
7
#include "util/logging.h"
cRTrn13's avatar
cRTrn13 committed
8 9 10 11 12 13

BLURAY *bd_open(const char* device_path, const char* keyfile_path)
{
    BLURAY *bd = malloc(sizeof(BLURAY));

    bd->aacs = NULL;
cRTrn13's avatar
cRTrn13 committed
14
    bd->h_libaacs = NULL;
cRTrn13's avatar
cRTrn13 committed
15
    bd->fp = NULL;
cRTrn13's avatar
cRTrn13 committed
16
    strncpy(bd->device_path, device_path, 100);
cRTrn13's avatar
cRTrn13 committed
17 18

    // open aacs decryptor if present
cRTrn13's avatar
cRTrn13 committed
19
    if ((bd->h_libaacs = dlopen("libaacs.so", RTLD_LAZY))) {
cRTrn13's avatar
cRTrn13 committed
20 21
        DEBUG(DBG_BLURAY, "Downloaded libaacs (0x%08x)\n", bd->h_libaacs);

cRTrn13's avatar
cRTrn13 committed
22 23
        typedef AACS_KEYS* (*fptr)();

24
        fptr fptr_s = dlsym(bd->h_libaacs, "aacs_open");
cRTrn13's avatar
cRTrn13 committed
25
        bd->aacs = fptr_s(device_path, keyfile_path);
cRTrn13's avatar
cRTrn13 committed
26 27
    } else {
        DEBUG(DBG_BLURAY, "libaacs not present\n");
cRTrn13's avatar
cRTrn13 committed
28 29
    }

cRTrn13's avatar
cRTrn13 committed
30 31
    DEBUG(DBG_BLURAY, "BLURAY initialized! (0x%08x)\n", bd);

cRTrn13's avatar
cRTrn13 committed
32 33 34 35 36
    return bd;
}

void bd_close(BLURAY *bd)
{
37
    if (bd->h_libaacs) {
cRTrn13's avatar
cRTrn13 committed
38 39
        typedef void* (*fptr)();

40
        fptr fptr_s = dlsym(bd->h_libaacs, "aacs_close");
cRTrn13's avatar
cRTrn13 committed
41
        fptr_s(bd->aacs);
cRTrn13's avatar
cRTrn13 committed
42 43
    }

44
    dlclose(bd->h_libaacs);
cRTrn13's avatar
cRTrn13 committed
45

cRTrn13's avatar
cRTrn13 committed
46 47 48
    if (bd->fp) {
        file_close(bd->fp);
    }
cRTrn13's avatar
cRTrn13 committed
49 50

    DEBUG(DBG_BLURAY, "BLURAY destroyed! (0x%08x)\n", bd);
cRTrn13's avatar
cRTrn13 committed
51 52

    X_FREE(bd);
cRTrn13's avatar
cRTrn13 committed
53
}
cRTrn13's avatar
cRTrn13 committed
54

cRTrn13's avatar
cRTrn13 committed
55
off_t bd_seek(BLURAY *bd, uint64_t pos)
cRTrn13's avatar
cRTrn13 committed
56 57 58 59
{
    if (pos < bd->s_size) {
        bd->s_pos = pos - (pos % 6144);

cRTrn13's avatar
cRTrn13 committed
60
        file_seek(bd->fp, bd->s_pos, SEEK_SET);
cRTrn13's avatar
cRTrn13 committed
61 62 63 64 65 66 67 68 69 70 71
    }

    return bd->s_pos;
}

int bd_read(BLURAY *bd, unsigned char *buf, int len)
{
    if (len + bd->s_pos < bd->s_size) {
        int read;

        if ((read = file_read(bd->fp, buf, len))) {
72 73 74
            if (bd->h_libaacs) {
                bd->libaacs_decrypt_unit = dlsym(bd->h_libaacs, "aacs_decrypt_unit");
                if (!bd->libaacs_decrypt_unit(bd->aacs, buf, len)) {
cRTrn13's avatar
cRTrn13 committed
75 76 77 78 79 80 81 82 83 84 85 86
                    return 0;
                }
            }

            bd->s_pos += len;

            return read;
        }
    }

    return 0;
}
cRTrn13's avatar
cRTrn13 committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108

int bd_select_title(BLURAY *bd, uint64_t title)
{
    char f_name[100];

    memset(f_name, 0, sizeof(f_name));
    snprintf(f_name, 100, "%s/BDMV/STREAM/%05ld.m2ts", bd->device_path, title);

    bd->s_size = 0;
    bd->s_pos = 0;

    if ((bd->fp = file_open(f_name, "rb"))) {
        file_seek(bd->fp, 0, SEEK_END);
        if ((bd->s_size = file_tell(bd->fp))) {
            file_seek(bd->fp, 0, SEEK_SET);

            return 1;
        }
    }

    return 0;
}