bluray.c 1.76 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 17

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

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

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

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

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

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

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

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

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

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

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

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

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

    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))) {
71 72 73
            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
74 75 76 77 78 79 80 81 82 83 84 85
                    return 0;
                }
            }

            bd->s_pos += len;

            return read;
        }
    }

    return 0;
}