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-08-29 20:05:31 +02:00
|
|
|
static void find_hca_key(hca_codec_data * hca_data, uint8_t * header_buffer, int header_size, unsigned int * out_keycode_upper, unsigned int * out_keycode_lower);
|
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-08-29 20:05:31 +02:00
|
|
|
uint8_t header_buffer[0x8000]; /* hca header buffer data (probably max ~0x400) */
|
2017-09-17 03:41:36 +02:00
|
|
|
|
|
|
|
hca_codec_data * hca_data = NULL; /* vgmstream HCA context */
|
2018-08-29 20:05:31 +02:00
|
|
|
unsigned int keycode_upper, keycode_lower;
|
|
|
|
int header_size;
|
2016-06-28 09:20:37 +02:00
|
|
|
|
2018-08-29 20:05:31 +02:00
|
|
|
/* checks */
|
|
|
|
if ( !check_extensions(streamFile, "hca"))
|
|
|
|
return NULL;
|
2016-07-01 00:34:40 +02:00
|
|
|
|
2018-08-29 20:05:31 +02:00
|
|
|
/* init header */
|
|
|
|
if (read_streamfile(header_buffer, 0x00, 0x08, streamFile) != 0x08)
|
|
|
|
goto fail;
|
|
|
|
header_size = clHCA_isOurFile0(header_buffer);
|
|
|
|
if (header_size < 0 || header_size > 0x8000) goto fail;
|
2016-07-01 00:34:40 +02:00
|
|
|
|
2018-08-29 20:05:31 +02:00
|
|
|
if (read_streamfile(header_buffer, 0x00, header_size, streamFile) != header_size) goto fail;
|
|
|
|
if (clHCA_isOurFile1(header_buffer, header_size) < 0)
|
|
|
|
goto fail;
|
2016-07-01 00:34:40 +02:00
|
|
|
|
2017-09-17 03:41:36 +02:00
|
|
|
/* init vgmstream context */
|
2018-08-29 20:05:31 +02:00
|
|
|
hca_data = init_hca(streamFile);
|
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 */
|
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-08-29 20:05:31 +02:00
|
|
|
keycode_upper = get_32bitBE(keybuf+0);
|
|
|
|
keycode_lower = get_32bitBE(keybuf+4);
|
2016-12-04 14:12:23 +01:00
|
|
|
} else {
|
2018-08-29 20:05:31 +02:00
|
|
|
find_hca_key(hca_data, header_buffer, header_size, &keycode_upper, &keycode_lower);
|
2016-12-04 14:12:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-29 20:05:31 +02:00
|
|
|
/* re-init decoder with key (as it must be supplied on header read) */
|
|
|
|
clHCA_clear(hca_data->handle, keycode_lower, keycode_upper);
|
|
|
|
if (clHCA_Decode(hca_data->handle, header_buffer, header_size, 0) < 0) /* read header at 0x00 */
|
|
|
|
goto fail;
|
|
|
|
if (clHCA_getInfo(hca_data->handle, &hca_data->info) < 0) /* copy important header values to info struct */
|
|
|
|
goto fail;
|
2018-08-29 23:42:47 +02:00
|
|
|
reset_hca(hca_data);
|
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-08-29 20:05:31 +02:00
|
|
|
#define HCA_KEY_MAX_TEST_CLIPS 400 /* hopefully nobody masters files with more that a handful... */
|
|
|
|
#define HCA_KEY_MAX_TEST_FRAMES 100 /* ~102400 samples */
|
|
|
|
#define HCA_KEY_MAX_TEST_SAMPLES 10240 /* ~10 frames of non-blank samples */
|
|
|
|
|
2017-09-17 03:41:36 +02:00
|
|
|
/* Tries to find the decryption key from a list. Simply decodes a few frames and checks if there aren't too many
|
|
|
|
* clipped samples, as it's common for invalid keys (though possible with valid keys in poorly mastered files). */
|
2018-08-29 20:05:31 +02:00
|
|
|
static void find_hca_key(hca_codec_data * hca_data, uint8_t * header_buffer, int header_size, unsigned int * out_keycode_upper, unsigned int * out_keycode_lower) {
|
|
|
|
sample *testbuf = NULL;
|
2017-09-30 19:36:13 +02:00
|
|
|
int i, j, bufsize = 0, tempsize;
|
2017-09-17 03:41:36 +02:00
|
|
|
size_t keys_length = sizeof(hcakey_list) / sizeof(hcakey_info);
|
2016-06-28 09:20:37 +02:00
|
|
|
|
2017-09-17 03:41:36 +02:00
|
|
|
int min_clip_count = -1;
|
|
|
|
/* defaults to PSO2 key, most common */
|
2018-08-29 20:05:31 +02:00
|
|
|
unsigned int best_keycode_upper = 0xCC554639;
|
|
|
|
unsigned int best_keycode_lower = 0x30DBE1AB;
|
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++) {
|
|
|
|
int clip_count = 0, sample_count = 0;
|
2018-08-29 20:05:31 +02:00
|
|
|
int frame = 0, s;
|
2016-06-28 09:20:37 +02:00
|
|
|
|
2018-08-29 20:05:31 +02:00
|
|
|
unsigned int keycode_upper, keycode_lower;
|
2017-09-17 03:41:36 +02:00
|
|
|
uint64_t key = hcakey_list[i].key;
|
2018-08-29 20:05:31 +02:00
|
|
|
keycode_upper = (key >> 32) & 0xFFFFFFFF;
|
|
|
|
keycode_lower = (key >> 0) & 0xFFFFFFFF;
|
2017-09-17 03:41:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* re-init HCA with the current key as buffer becomes invalid (probably can be simplified) */
|
2018-08-29 23:42:47 +02:00
|
|
|
reset_hca(hca_data);
|
|
|
|
if (read_streamfile(header_buffer, 0x00, header_size, hca_data->streamfile) != header_size)
|
2018-08-29 20:05:31 +02:00
|
|
|
continue;
|
2017-09-17 03:41:36 +02:00
|
|
|
|
2018-08-29 20:05:31 +02:00
|
|
|
clHCA_clear(hca_data->handle, keycode_lower, keycode_upper);
|
|
|
|
if (clHCA_Decode(hca_data->handle, header_buffer, header_size, 0) < 0)
|
|
|
|
continue;
|
|
|
|
if (clHCA_getInfo(hca_data->handle, &hca_data->info) < 0)
|
|
|
|
continue;
|
2017-09-30 19:36:13 +02:00
|
|
|
|
|
|
|
tempsize = sizeof(sample) * clHCA_samplesPerBlock * hca_data->info.channelCount;
|
|
|
|
if (tempsize > bufsize) { /* should happen once */
|
2018-08-29 20:05:31 +02:00
|
|
|
sample *temp = (sample *)realloc(testbuf, tempsize);
|
2017-09-30 19:36:13 +02:00
|
|
|
if (!temp) goto end;
|
|
|
|
testbuf = temp;
|
|
|
|
bufsize = tempsize;
|
2017-09-30 01:56:43 +02:00
|
|
|
}
|
2017-09-17 03:41:36 +02:00
|
|
|
|
|
|
|
/* test enough frames, but not too many */
|
2018-08-29 20:05:31 +02:00
|
|
|
while (frame < HCA_KEY_MAX_TEST_FRAMES && frame < hca_data->info.blockCount) {
|
2017-09-30 01:56:43 +02:00
|
|
|
j = clHCA_samplesPerBlock;
|
2018-08-29 20:05:31 +02:00
|
|
|
decode_hca(hca_data, testbuf, j);
|
2017-09-17 03:41:36 +02:00
|
|
|
|
2017-09-30 01:56:43 +02:00
|
|
|
j *= hca_data->info.channelCount;
|
|
|
|
for (s = 0; s < j; s++) {
|
2017-09-17 03:41:36 +02:00
|
|
|
if (testbuf[s] != 0x0000 && testbuf[s] != 0xFFFF)
|
|
|
|
sample_count++; /* ignore upper/lower blank samples */
|
|
|
|
|
|
|
|
if (testbuf[s] == 0x7FFF || testbuf[s] == 0x8000)
|
|
|
|
clip_count++; /* upper/lower clip */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (clip_count > HCA_KEY_MAX_TEST_CLIPS)
|
|
|
|
break; /* too many, don't bother */
|
|
|
|
if (sample_count >= HCA_KEY_MAX_TEST_SAMPLES)
|
|
|
|
break; /* enough non-blank samples tested */
|
|
|
|
|
2018-08-29 20:05:31 +02:00
|
|
|
frame++;
|
2017-09-17 03:41:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (min_clip_count < 0 || clip_count < min_clip_count) {
|
|
|
|
min_clip_count = clip_count;
|
2018-08-29 20:05:31 +02:00
|
|
|
best_keycode_upper = keycode_upper;
|
|
|
|
best_keycode_lower = keycode_lower;
|
2017-09-17 03:41:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (min_clip_count == 0)
|
|
|
|
break; /* can't get better than this */
|
|
|
|
|
|
|
|
/* a few clips is normal, but some invalid keys may give low numbers too */
|
|
|
|
//if (clip_count < 10)
|
|
|
|
// break;
|
|
|
|
}
|
|
|
|
|
2018-08-29 23:42:47 +02:00
|
|
|
/* reset HCA header */
|
|
|
|
hca_data->current_block = 0;
|
|
|
|
read_streamfile(header_buffer, 0x00, header_size, hca_data->streamfile);
|
2017-09-17 03:41:36 +02:00
|
|
|
|
2017-09-29 23:24:25 +02:00
|
|
|
end:
|
2018-08-29 20:05:31 +02:00
|
|
|
VGM_ASSERT(min_clip_count > 0, "HCA: best key=%08x%08x (clips=%i)\n", best_keycode_upper,best_keycode_lower, min_clip_count);
|
|
|
|
*out_keycode_upper = best_keycode_upper;
|
|
|
|
*out_keycode_lower = best_keycode_lower;
|
|
|
|
free(testbuf);
|
2016-06-28 09:20:37 +02:00
|
|
|
}
|