2016-06-28 09:20:37 +02:00
|
|
|
#include "meta.h"
|
2017-09-17 03:41:36 +02:00
|
|
|
#include "hca_keys.h"
|
|
|
|
#include "../coding/coding.h"
|
2024-06-11 00:01:17 +02:00
|
|
|
#include "../coding/libs/clhca.h"
|
2023-05-07 22:00:30 +02:00
|
|
|
#include "../util/channel_mappings.h"
|
2023-05-14 23:20:20 +02:00
|
|
|
#include "../util/companion_files.h"
|
2024-02-11 22:36:26 +01:00
|
|
|
#include "../util/cri_keys.h"
|
2016-06-28 09:20:37 +02:00
|
|
|
|
2021-10-08 22:24:51 +02:00
|
|
|
#ifdef VGM_DEBUG_OUTPUT
|
2021-10-19 00:40:14 +02:00
|
|
|
//#define HCA_BRUTEFORCE
|
|
|
|
#ifdef HCA_BRUTEFORCE
|
|
|
|
#include "hca_bf.h"
|
|
|
|
#endif
|
2021-10-19 00:35:06 +02:00
|
|
|
#endif
|
|
|
|
|
2021-10-15 22:41:51 +02:00
|
|
|
static int find_hca_key(hca_codec_data* hca_data, uint64_t* p_keycode, uint16_t subkey);
|
2016-06-28 09:20:37 +02:00
|
|
|
|
2020-02-22 20:40:21 +01:00
|
|
|
|
|
|
|
/* CRI HCA - streamed audio from CRI ADX2/Atom middleware */
|
2021-03-14 16:28:35 +01:00
|
|
|
VGMSTREAM* init_vgmstream_hca(STREAMFILE* sf) {
|
2020-07-16 21:43:01 +02:00
|
|
|
return init_vgmstream_hca_subkey(sf, 0x0000);
|
2019-07-07 21:04:56 +02:00
|
|
|
}
|
|
|
|
|
2021-03-14 16:28:35 +01:00
|
|
|
VGMSTREAM* init_vgmstream_hca_subkey(STREAMFILE* sf, uint16_t subkey) {
|
2021-07-29 17:35:02 +02:00
|
|
|
VGMSTREAM* vgmstream = NULL;
|
2020-07-16 21:43:01 +02:00
|
|
|
hca_codec_data* hca_data = NULL;
|
|
|
|
clHCA_stInfo* hca_info;
|
2016-06-28 09:20:37 +02:00
|
|
|
|
2019-07-07 21:04:56 +02:00
|
|
|
|
2018-08-29 20:05:31 +02:00
|
|
|
/* checks */
|
2021-10-03 13:49:23 +02:00
|
|
|
if ((read_u32be(0x00,sf) & 0x7F7F7F7F) != get_id32be("HCA\0")) /* masked in encrypted files */
|
|
|
|
goto fail;
|
|
|
|
|
2020-07-16 21:43:01 +02:00
|
|
|
if (!check_extensions(sf, "hca"))
|
2018-08-29 20:05:31 +02:00
|
|
|
return NULL;
|
2020-07-16 21:43:01 +02:00
|
|
|
|
2018-09-01 20:28:00 +02:00
|
|
|
/* init vgmstream and library's context, will validate the HCA */
|
2020-07-16 21:43:01 +02:00
|
|
|
hca_data = init_hca(sf);
|
2021-08-26 19:39:58 +02:00
|
|
|
if (!hca_data) {
|
|
|
|
vgm_logi("HCA: unknown format (report)\n");
|
|
|
|
goto fail;
|
|
|
|
}
|
2016-07-01 00:34:40 +02:00
|
|
|
|
2020-07-16 21:43:01 +02:00
|
|
|
hca_info = hca_get_info(hca_data);
|
|
|
|
|
2017-09-17 03:41:36 +02:00
|
|
|
/* find decryption key in external file or preloaded list */
|
2020-07-16 21:43:01 +02:00
|
|
|
if (hca_info->encryptionEnabled) {
|
2020-04-12 20:36:46 +02:00
|
|
|
uint64_t keycode = 0;
|
2024-02-11 22:36:26 +01:00
|
|
|
uint8_t keybuf[20+1] = {0}; /* max keystring 20, +1 extra null */
|
|
|
|
size_t key_size;
|
2018-10-13 19:53:25 +02:00
|
|
|
|
2024-02-11 22:36:26 +01:00
|
|
|
key_size = read_key_file(keybuf, sizeof(keybuf) - 1, sf);
|
|
|
|
|
|
|
|
bool is_keystring = cri_key9_valid_keystring(keybuf, key_size);
|
|
|
|
|
|
|
|
if (is_keystring) { /* number */
|
|
|
|
const char* keystring = (const char*)keybuf;
|
|
|
|
keycode = strtoull(keystring, NULL, 10);
|
|
|
|
}
|
|
|
|
else if (key_size == 0x08) { /* hex */
|
2020-07-16 21:43:01 +02:00
|
|
|
keycode = get_u64be(keybuf+0x00);
|
2018-10-13 19:53:25 +02:00
|
|
|
}
|
2024-02-11 22:36:26 +01:00
|
|
|
else if (key_size == 0x08+0x02) { /* seed key + AWB subkey */
|
2021-12-11 12:43:06 +01:00
|
|
|
keycode = get_u64be(keybuf+0x00);
|
|
|
|
subkey = get_u16be(keybuf+0x08);
|
2018-10-13 19:53:25 +02:00
|
|
|
}
|
2020-02-22 20:40:21 +01:00
|
|
|
#ifdef HCA_BRUTEFORCE
|
|
|
|
else if (1) {
|
2021-10-15 22:41:51 +02:00
|
|
|
int ok = find_hca_key(hca_data, &keycode, subkey);
|
|
|
|
if (!ok)
|
|
|
|
bruteforce_hca_key(sf, hca_data, &keycode, subkey);
|
2020-02-22 20:40:21 +01:00
|
|
|
}
|
|
|
|
#endif
|
2018-10-13 19:53:25 +02:00
|
|
|
else {
|
2019-07-07 21:04:56 +02:00
|
|
|
find_hca_key(hca_data, &keycode, subkey);
|
2016-12-04 14:12:23 +01:00
|
|
|
}
|
|
|
|
|
2021-12-11 12:43:06 +01:00
|
|
|
hca_set_encryption_key(hca_data, keycode, subkey);
|
2018-09-01 20:28:00 +02:00
|
|
|
}
|
2017-09-17 03:41:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* build the VGMSTREAM */
|
2020-07-16 21:43:01 +02:00
|
|
|
vgmstream = allocate_vgmstream(hca_info->channelCount, hca_info->loopEnabled);
|
2017-09-17 03:41:36 +02:00
|
|
|
if (!vgmstream) goto fail;
|
2016-07-01 00:34:40 +02:00
|
|
|
|
2018-08-29 20:05:31 +02:00
|
|
|
vgmstream->meta_type = meta_HCA;
|
2020-07-16 21:43:01 +02:00
|
|
|
vgmstream->sample_rate = hca_info->samplingRate;
|
|
|
|
|
|
|
|
vgmstream->num_samples = hca_info->blockCount * hca_info->samplesPerBlock -
|
|
|
|
hca_info->encoderDelay - hca_info->encoderPadding;
|
|
|
|
vgmstream->loop_start_sample = hca_info->loopStartBlock * hca_info->samplesPerBlock -
|
|
|
|
hca_info->encoderDelay + hca_info->loopStartDelay;
|
|
|
|
vgmstream->loop_end_sample = hca_info->loopEndBlock * hca_info->samplesPerBlock -
|
|
|
|
hca_info->encoderDelay + (hca_info->samplesPerBlock - hca_info->loopEndPadding);
|
2018-08-29 23:42:47 +02:00
|
|
|
/* After loop end CRI's encoder removes the rest of the original samples and puts some
|
|
|
|
* garbage in the last frame that should be ignored. Optionally it can encode fully preserving
|
|
|
|
* the file too, but it isn't detectable, so we'll allow the whole thing just in case */
|
|
|
|
//if (vgmstream->loop_end_sample && vgmstream->num_samples > vgmstream->loop_end_sample)
|
|
|
|
// vgmstream->num_samples = vgmstream->loop_end_sample;
|
2016-07-01 00:34:40 +02:00
|
|
|
|
2019-07-14 21:24:28 +02:00
|
|
|
/* this can happen in preloading HCA from memory AWB */
|
2020-07-16 21:43:01 +02:00
|
|
|
if (hca_info->blockCount * hca_info->blockSize > get_streamfile_size(sf)) {
|
|
|
|
unsigned int max_block = get_streamfile_size(sf) / hca_info->blockSize;
|
|
|
|
vgmstream->num_samples = max_block * hca_info->samplesPerBlock -
|
|
|
|
hca_info->encoderDelay - hca_info->encoderPadding;
|
2019-07-14 21:24:28 +02:00
|
|
|
}
|
|
|
|
|
2017-09-17 03:41:36 +02:00
|
|
|
vgmstream->coding_type = coding_CRI_HCA;
|
|
|
|
vgmstream->layout_type = layout_none;
|
|
|
|
vgmstream->codec_data = hca_data;
|
2016-07-01 00:34:40 +02:00
|
|
|
|
2022-07-07 23:13:53 +02:00
|
|
|
/* Assumed mappings; seems correct vs Atom Viewer, that lists L/R/C/LFE/LS/RS and downmixes HCAs like that.
|
|
|
|
* USM HCA's seem to be L/R/SL/SR/C/LFE though (probably reordered at USM level, no detection done in Atom Viewer). */
|
2019-08-15 15:15:00 +02:00
|
|
|
{
|
|
|
|
static const uint32_t hca_mappings[] = {
|
|
|
|
0,
|
|
|
|
mapping_MONO,
|
|
|
|
mapping_STEREO,
|
|
|
|
mapping_2POINT1,
|
2022-07-07 23:13:53 +02:00
|
|
|
mapping_QUAD_side,
|
2019-08-15 15:15:00 +02:00
|
|
|
mapping_5POINT0,
|
2022-07-07 23:13:53 +02:00
|
|
|
mapping_5POINT1_surround,
|
2019-08-15 15:15:00 +02:00
|
|
|
mapping_7POINT0,
|
2022-07-07 23:13:53 +02:00
|
|
|
mapping_7POINT1_surround,
|
2019-08-15 15:15:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
vgmstream->channel_layout = hca_mappings[vgmstream->channels];
|
|
|
|
}
|
|
|
|
|
2017-09-17 03:41:36 +02:00
|
|
|
return vgmstream;
|
2016-06-28 09:20:37 +02:00
|
|
|
|
2017-09-17 03:41:36 +02:00
|
|
|
fail:
|
2018-08-29 20:05:31 +02:00
|
|
|
free_hca(hca_data);
|
2017-09-17 03:41:36 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
2016-06-28 09:20:37 +02:00
|
|
|
|
|
|
|
|
2021-10-19 00:35:06 +02:00
|
|
|
/* try to find the decryption key from a list */
|
2021-10-15 22:41:51 +02:00
|
|
|
static int find_hca_key(hca_codec_data* hca_data, uint64_t* p_keycode, uint16_t subkey) {
|
2021-10-19 00:35:06 +02:00
|
|
|
const size_t keys_length = sizeof(hcakey_list) / sizeof(hcakey_list[0]);
|
2021-07-29 18:38:07 +02:00
|
|
|
int i;
|
2021-10-19 00:35:06 +02:00
|
|
|
hca_keytest_t hk = {0};
|
2016-06-28 09:20:37 +02:00
|
|
|
|
2021-10-19 00:35:06 +02:00
|
|
|
hk.best_key = 0xCC55463930DBE1AB; /* defaults to PSO2 key, most common */
|
|
|
|
hk.subkey = subkey;
|
2016-06-28 09:20:37 +02:00
|
|
|
|
2017-09-17 03:41:36 +02:00
|
|
|
for (i = 0; i < keys_length; i++) {
|
2021-10-19 00:35:06 +02:00
|
|
|
hk.key = hcakey_list[i].key;
|
2018-10-13 19:53:25 +02:00
|
|
|
|
2021-10-19 00:35:06 +02:00
|
|
|
test_hca_key(hca_data, &hk);
|
|
|
|
if (hk.best_score == 1)
|
2019-07-07 21:04:56 +02:00
|
|
|
goto done;
|
|
|
|
|
2021-07-29 17:35:02 +02:00
|
|
|
#if 0
|
|
|
|
{
|
2021-07-29 18:38:07 +02:00
|
|
|
int j;
|
2021-07-29 17:35:02 +02:00
|
|
|
size_t subkeys_size = hcakey_list[i].subkeys_size;
|
2021-10-19 00:35:06 +02:00
|
|
|
const uint16_t* subkeys = hcakey_list[i].subkeys;
|
2021-07-29 17:35:02 +02:00
|
|
|
if (subkeys_size > 0 && subkey == 0) {
|
|
|
|
for (j = 0; j < subkeys_size; j++) {
|
2021-10-19 00:35:06 +02:00
|
|
|
hk.subkey = subkeys[j];
|
|
|
|
test_hca_key(hca_data, &hk);
|
|
|
|
if (hk.best_score == 1)
|
2021-07-29 17:35:02 +02:00
|
|
|
goto done;
|
|
|
|
}
|
2018-10-13 19:53:25 +02:00
|
|
|
}
|
2017-09-17 03:41:36 +02:00
|
|
|
}
|
2021-07-29 17:35:02 +02:00
|
|
|
#endif
|
2017-09-17 03:41:36 +02:00
|
|
|
}
|
|
|
|
|
2018-10-13 19:53:25 +02:00
|
|
|
done:
|
2021-10-19 00:35:06 +02:00
|
|
|
*p_keycode = hk.best_key;
|
|
|
|
VGM_ASSERT(hk.best_score > 1, "HCA: best key=%08x%08x (score=%i)\n",
|
|
|
|
(uint32_t)((*p_keycode >> 32) & 0xFFFFFFFF), (uint32_t)(*p_keycode & 0xFFFFFFFF), hk.best_score);
|
|
|
|
vgm_asserti(hk.best_score <= 0, "HCA: decryption key not found\n");
|
|
|
|
return hk.best_score > 0;
|
|
|
|
}
|