bluray.c 1.37 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 13 14 15 16 17 18 19 20 21 22 23 24 25
#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;
    if ((bd->libaacs_h = dlopen("libaacs", RTLD_NOW))) {
        typedef AACS_KEYS* (*fptr)();

        fptr fptr_s = dlsym(bd->libaacs_h, "aacs_open");
        bd->aacs = fptr_s(device_path, keyfile_path);
    }

    return bd;
}

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

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

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

cRTrn13's avatar
cRTrn13 committed
34 35
    dlclose(bd->libaacs_h);
}
cRTrn13's avatar
cRTrn13 committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

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))) {
            if (bd->libaacs_h) {
                typedef int* (*fptr)();

                fptr fptr_s = dlsym(bd->libaacs_h, "aacs_decrypt_unit");
                if (!fptr_s(bd->aacs, buf, len)) {
                    return 0;
                }
            }

            bd->s_pos += len;

            return read;
        }
    }

    return 0;
}