bluray.c 3.36 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

Accident's avatar
 
Accident committed
9

cRTrn13's avatar
cRTrn13 committed
10 11
BLURAY *bd_open(const char* device_path, const char* keyfile_path)
{
cRTrn13's avatar
cRTrn13 committed
12
    BLURAY *bd = NULL;
cRTrn13's avatar
cRTrn13 committed
13

cRTrn13's avatar
cRTrn13 committed
14
    printf("A\n");
cRTrn13's avatar
cRTrn13 committed
15 16 17 18 19
    if (device_path) {
        strncpy(bd->device_path, device_path, 100);

        bd = malloc(sizeof(BLURAY));

cRTrn13's avatar
cRTrn13 committed
20 21 22 23
        bd->aacs = NULL;
        bd->h_libaacs = NULL;
        bd->fp = NULL;

cRTrn13's avatar
cRTrn13 committed
24 25 26
        if (keyfile_path) {
            if ((bd->h_libaacs = dlopen("libaacs.so", RTLD_LAZY))) {
                DEBUG(DBG_BLURAY, "Downloaded libaacs (0x%08x)\n", bd->h_libaacs);
cRTrn13's avatar
cRTrn13 committed
27

cRTrn13's avatar
cRTrn13 committed
28
                typedef AACS_KEYS* (*fptr)();
cRTrn13's avatar
cRTrn13 committed
29

cRTrn13's avatar
cRTrn13 committed
30 31 32 33 34 35 36 37 38 39
                fptr fptr_s = dlsym(bd->h_libaacs, "aacs_open");
                bd->aacs = fptr_s(device_path, keyfile_path);
            } else {
                DEBUG(DBG_BLURAY, "libaacs not present\n");
            }
        } else {
            DEBUG(DBG_BLURAY, "No keyfile provided. You will not be able to make use of crypto functionality (0x%08x)\n", bd);
        }

        DEBUG(DBG_BLURAY, "BLURAY initialized! (0x%08x)\n", bd);
cRTrn13's avatar
cRTrn13 committed
40
    } else {
cRTrn13's avatar
cRTrn13 committed
41
        DEBUG(DBG_BLURAY, "No device path provided!\n");
cRTrn13's avatar
cRTrn13 committed
42 43 44 45 46 47 48
    }

    return bd;
}

void bd_close(BLURAY *bd)
{
49
    if (bd->h_libaacs) {
cRTrn13's avatar
cRTrn13 committed
50 51
        typedef void* (*fptr)();

52
        fptr fptr_s = dlsym(bd->h_libaacs, "aacs_close");
cRTrn13's avatar
cRTrn13 committed
53
        fptr_s(bd->aacs);
cRTrn13's avatar
cRTrn13 committed
54 55
    }

56
    dlclose(bd->h_libaacs);
cRTrn13's avatar
cRTrn13 committed
57

cRTrn13's avatar
cRTrn13 committed
58 59 60
    if (bd->fp) {
        file_close(bd->fp);
    }
cRTrn13's avatar
cRTrn13 committed
61 62

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

    X_FREE(bd);
cRTrn13's avatar
cRTrn13 committed
65
}
cRTrn13's avatar
cRTrn13 committed
66

cRTrn13's avatar
cRTrn13 committed
67
off_t bd_seek(BLURAY *bd, uint64_t pos)
cRTrn13's avatar
cRTrn13 committed
68 69 70 71
{
    if (pos < bd->s_size) {
        bd->s_pos = pos - (pos % 6144);

cRTrn13's avatar
cRTrn13 committed
72
        file_seek(bd->fp, bd->s_pos, SEEK_SET);
73 74

        DEBUG(DBG_BLURAY, "Seek to %ld (0x%08x)\n", bd->s_pos, bd);
cRTrn13's avatar
cRTrn13 committed
75 76 77 78 79 80 81
    }

    return bd->s_pos;
}

int bd_read(BLURAY *bd, unsigned char *buf, int len)
{
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
    if (bd->fp) {
        DEBUG(DBG_BLURAY, "Reading unit [%d bytes] at %ld... (0x%08x)\n", len, bd->s_pos, bd);

        if (len + bd->s_pos < bd->s_size) {
            int read_len;

            if ((read_len = file_read(bd->fp, buf, len))) {
                if (bd->h_libaacs) {
                    if ((bd->libaacs_decrypt_unit = dlsym(bd->h_libaacs, "aacs_decrypt_unit"))) {
                        if (!bd->libaacs_decrypt_unit(bd->aacs, buf, len, bd->s_pos)) {
                            DEBUG(DBG_BLURAY, "Unable decrypt unit! (0x%08x)\n", bd);

                            return 0;
                        }
                    }
cRTrn13's avatar
cRTrn13 committed
97 98
                }

99
                bd->s_pos += len;
cRTrn13's avatar
cRTrn13 committed
100

101 102 103 104
                DEBUG(DBG_BLURAY, "%d bytes read OK! (0x%08x)\n", read_len, bd);

                return read_len;
            }
cRTrn13's avatar
cRTrn13 committed
105 106 107
        }
    }

108 109
    DEBUG(DBG_BLURAY, "No valid title selected! (0x%08x)\n", bd->s_pos);

cRTrn13's avatar
cRTrn13 committed
110 111
    return 0;
}
cRTrn13's avatar
cRTrn13 committed
112 113 114 115 116 117 118 119 120 121 122 123 124 125

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))) {
126 127 128
            bd_seek(bd, 0);

            DEBUG(DBG_BLURAY, "Title %s selected! (0x%08x)\n", f_name, bd);
cRTrn13's avatar
cRTrn13 committed
129 130 131

            return 1;
        }
132 133

        DEBUG(DBG_BLURAY, "Title %s empty! (0x%08x)\n", f_name, bd);
cRTrn13's avatar
cRTrn13 committed
134 135
    }

136 137
    DEBUG(DBG_BLURAY, "Unable to select title %s! (0x%08x)\n", f_name, bd);

cRTrn13's avatar
cRTrn13 committed
138 139
    return 0;
}