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

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

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

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

    // open aacs decryptor if present
    bd->aacs = NULL;
cRTrn13's avatar
cRTrn13 committed
13
    if ((bd->h_libaacs = dlopen("libaacs.so", RTLD_NOW))) {
cRTrn13's avatar
cRTrn13 committed
14 15
        typedef AACS_KEYS* (*fptr)();

16
        fptr fptr_s = dlsym(bd->h_libaacs, "aacs_open");
cRTrn13's avatar
cRTrn13 committed
17 18 19 20 21 22 23 24
        bd->aacs = fptr_s(device_path, keyfile_path);
    }

    return bd;
}

void bd_close(BLURAY *bd)
{
25
    if (bd->h_libaacs) {
cRTrn13's avatar
cRTrn13 committed
26 27
        typedef void* (*fptr)();

28
        fptr fptr_s = dlsym(bd->h_libaacs, "aacs_close");
cRTrn13's avatar
cRTrn13 committed
29
        fptr_s(bd->aacs);
cRTrn13's avatar
cRTrn13 committed
30 31 32
    }

    X_FREE(bd);
cRTrn13's avatar
cRTrn13 committed
33

34
    dlclose(bd->h_libaacs);
cRTrn13's avatar
cRTrn13 committed
35
}
cRTrn13's avatar
cRTrn13 committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

off_t bd_seek(BLURAY *bd, off_t pos)
{
    if (pos < bd->s_size) {
        bd->s_pos = pos - (pos % 6144);

        file_seek(bd->fp, bd->s_pos, SEEK_SET);
    }

    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))) {
54 55 56
            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
57 58 59 60 61 62 63 64 65 66 67 68
                    return 0;
                }
            }

            bd->s_pos += len;

            return read;
        }
    }

    return 0;
}