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"
|
2016-06-28 09:20:37 +02:00
|
|
|
|
2018-09-01 20:28:00 +02:00
|
|
|
static void find_hca_key(hca_codec_data * hca_data, unsigned long long * out_keycode);
|
2016-06-28 09:20:37 +02:00
|
|
|
|
|
|
|
VGMSTREAM * init_vgmstream_hca(STREAMFILE *streamFile) {
|
2017-09-17 03:41:36 +02:00
|
|
|
VGMSTREAM * vgmstream = NULL;
|
2018-09-01 20:28:00 +02:00
|
|
|
hca_codec_data * hca_data = NULL;
|
|
|
|
unsigned long long keycode = 0;
|
2016-06-28 09:20:37 +02:00
|
|
|
|
2018-08-29 20:05:31 +02:00
|
|
|
/* checks */
|
|
|
|
if ( !check_extensions(streamFile, "hca"))
|
|
|
|
return NULL;
|
2018-09-01 20:28:00 +02:00
|
|
|
if (((uint32_t)read_32bitBE(0x00,streamFile) & 0x7f7f7f7f) != 0x48434100) /* "HCA\0", possibly masked */
|
2018-08-29 20:05:31 +02:00
|
|
|
goto fail;
|
2016-07-01 00:34:40 +02:00
|
|
|
|
2018-09-01 20:28:00 +02:00
|
|
|
/* init vgmstream and library's context, will validate the HCA */
|
2018-08-29 20:05:31 +02:00
|
|
|
hca_data = init_hca(streamFile);
|
2018-09-01 20:28:00 +02:00
|
|
|
if (!hca_data) goto fail;
|
2016-07-01 00:34:40 +02:00
|
|
|
|
2017-09-17 03:41:36 +02:00
|
|
|
/* find decryption key in external file or preloaded list */
|
2018-09-02 16:00:58 +02:00
|
|
|
if (hca_data->info.encryptionEnabled) {
|
2016-12-04 14:12:23 +01:00
|
|
|
uint8_t keybuf[8];
|
2018-01-20 20:06:15 +01:00
|
|
|
if (read_key_file(keybuf, 8, streamFile) == 8) {
|
2018-09-01 20:28:00 +02:00
|
|
|
keycode = (uint64_t)get_64bitBE(keybuf+0x00);
|
2016-12-04 14:12:23 +01:00
|
|
|
} else {
|
2018-09-01 20:28:00 +02:00
|
|
|
find_hca_key(hca_data, &keycode);
|
2016-12-04 14:12:23 +01:00
|
|
|
}
|
|
|
|
|
2018-09-01 20:28:00 +02:00
|
|
|
clHCA_SetKey(hca_data->handle, keycode); //maybe should be done through hca_decoder.c?
|
|
|
|
}
|
2017-09-17 03:41:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* build the VGMSTREAM */
|
|
|
|
vgmstream = allocate_vgmstream(hca_data->info.channelCount, hca_data->info.loopEnabled);
|
|
|
|
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;
|
2017-09-17 03:41:36 +02:00
|
|
|
vgmstream->sample_rate = hca_data->info.samplingRate;
|
2018-08-29 23:42:47 +02:00
|
|
|
|
|
|
|
vgmstream->num_samples = hca_data->info.blockCount * hca_data->info.samplesPerBlock -
|
|
|
|
hca_data->info.encoderDelay - hca_data->info.encoderPadding;
|
|
|
|
vgmstream->loop_start_sample = hca_data->info.loopStartBlock * hca_data->info.samplesPerBlock -
|
|
|
|
hca_data->info.encoderDelay + hca_data->info.loopStartDelay;
|
|
|
|
vgmstream->loop_end_sample = hca_data->info.loopEndBlock * hca_data->info.samplesPerBlock -
|
|
|
|
hca_data->info.encoderDelay + (hca_data->info.samplesPerBlock - hca_data->info.loopEndPadding);
|
|
|
|
/* 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
|
|
|
|
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
|
|
|
|
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
|
|
|
|
|
|
|
|
2018-09-02 16:00:58 +02:00
|
|
|
/* Try to find the decryption key from a list. */
|
2018-09-01 20:28:00 +02:00
|
|
|
static void find_hca_key(hca_codec_data * hca_data, unsigned long long * out_keycode) {
|
|
|
|
const size_t keys_length = sizeof(hcakey_list) / sizeof(hcakey_info);
|
|
|
|
unsigned long long best_keycode;
|
2018-09-02 16:00:58 +02:00
|
|
|
int best_score = -1;
|
2018-09-01 20:28:00 +02:00
|
|
|
int i;
|
|
|
|
|
|
|
|
best_keycode = 0xCC55463930DBE1AB; /* defaults to PSO2 key, most common */
|
2016-06-28 09:20:37 +02:00
|
|
|
|
|
|
|
|
2017-09-17 03:41:36 +02:00
|
|
|
/* find a candidate key */
|
|
|
|
for (i = 0; i < keys_length; i++) {
|
2018-09-02 16:00:58 +02:00
|
|
|
int score;
|
2018-09-01 20:28:00 +02:00
|
|
|
unsigned long long keycode = (unsigned long long)hcakey_list[i].key;
|
2016-06-28 09:20:37 +02:00
|
|
|
|
2018-09-02 16:00:58 +02:00
|
|
|
score = test_hca_key(hca_data, keycode);
|
2017-09-17 03:41:36 +02:00
|
|
|
|
2018-09-02 16:00:58 +02:00
|
|
|
//;VGM_LOG("HCA: test key=%08x%08x, score=%i\n",
|
|
|
|
// (uint32_t)((keycode >> 32) & 0xFFFFFFFF), (uint32_t)(keycode & 0xFFFFFFFF), score);
|
2017-09-17 03:41:36 +02:00
|
|
|
|
2018-09-02 16:00:58 +02:00
|
|
|
/* wrong key */
|
|
|
|
if (score < 0)
|
|
|
|
continue;
|
2017-09-17 03:41:36 +02:00
|
|
|
|
2018-09-02 16:00:58 +02:00
|
|
|
/* score 0 is not trustable, update too if something better is found */
|
|
|
|
if (best_score < 0 || score < best_score || (best_score == 0 && score == 1)) {
|
|
|
|
best_score = score;
|
2018-09-01 20:28:00 +02:00
|
|
|
best_keycode = keycode;
|
2017-09-17 03:41:36 +02:00
|
|
|
}
|
|
|
|
|
2018-09-02 16:00:58 +02:00
|
|
|
/* best possible score */
|
|
|
|
if (score == 1) {
|
|
|
|
break;
|
|
|
|
}
|
2017-09-17 03:41:36 +02:00
|
|
|
}
|
|
|
|
|
2018-09-02 16:00:58 +02:00
|
|
|
//;VGM_LOG("HCA: best key=%08x%08x (score=%i)\n",
|
|
|
|
// (uint32_t)((best_keycode >> 32) & 0xFFFFFFFF), (uint32_t)(best_keycode & 0xFFFFFFFF), best_score);
|
2017-09-17 03:41:36 +02:00
|
|
|
|
2018-09-02 16:00:58 +02:00
|
|
|
VGM_ASSERT(best_score > 1, "HCA: best key=%08x%08x (score=%i)\n",
|
|
|
|
(uint32_t)((best_keycode >> 32) & 0xFFFFFFFF), (uint32_t)(best_keycode & 0xFFFFFFFF), best_score);
|
2018-09-01 20:28:00 +02:00
|
|
|
*out_keycode = best_keycode;
|
2016-06-28 09:20:37 +02:00
|
|
|
}
|