2008-04-03 15:56:50 +02:00
|
|
|
#ifdef _MSC_VER
|
2008-04-03 15:40:36 +02:00
|
|
|
#define _USE_MATH_DEFINES
|
|
|
|
#endif
|
2008-01-31 07:04:26 +01:00
|
|
|
#include <math.h>
|
2008-12-24 09:28:19 +01:00
|
|
|
#include <limits.h>
|
2017-10-28 12:38:27 +02:00
|
|
|
#include "meta.h"
|
|
|
|
#include "adx_keys.h"
|
2008-12-24 08:19:15 +01:00
|
|
|
#include "../coding/coding.h"
|
2017-10-28 12:38:27 +02:00
|
|
|
|
2008-01-31 07:04:26 +01:00
|
|
|
|
2019-11-03 17:54:01 +01:00
|
|
|
#define ADX_KEY_MAX_TEST_FRAMES 32768
|
|
|
|
#define ADX_KEY_TEST_BUFFER_SIZE 0x8000
|
|
|
|
|
2021-09-19 23:54:06 +02:00
|
|
|
static int find_adx_key(STREAMFILE* sf, uint8_t type, uint16_t* xor_start, uint16_t* xor_mult, uint16_t* xor_add, uint16_t subkey);
|
2021-02-21 22:08:44 +01:00
|
|
|
|
|
|
|
VGMSTREAM* init_vgmstream_adx(STREAMFILE* sf) {
|
|
|
|
return init_vgmstream_adx_subkey(sf, 0);
|
|
|
|
}
|
2008-12-24 09:28:19 +01:00
|
|
|
|
2017-04-16 00:58:40 +02:00
|
|
|
/* ADX - CRI Middleware format */
|
2021-02-21 22:08:44 +01:00
|
|
|
VGMSTREAM* init_vgmstream_adx_subkey(STREAMFILE* sf, uint16_t subkey) {
|
|
|
|
VGMSTREAM* vgmstream = NULL;
|
2017-04-16 00:58:40 +02:00
|
|
|
off_t start_offset, hist_offset = 0;
|
|
|
|
int loop_flag = 0, channel_count;
|
|
|
|
int32_t loop_start_sample = 0, loop_end_sample = 0;
|
2019-11-03 17:54:01 +01:00
|
|
|
uint16_t version;
|
2017-05-13 23:04:30 +02:00
|
|
|
uint8_t encoding_type;
|
2017-05-13 22:17:27 +02:00
|
|
|
uint8_t frame_size;
|
2017-04-16 00:58:40 +02:00
|
|
|
|
2008-01-31 07:04:26 +01:00
|
|
|
meta_t header_type;
|
2017-05-14 00:37:24 +02:00
|
|
|
coding_t coding_type;
|
2008-01-31 07:04:26 +01:00
|
|
|
int16_t coef1, coef2;
|
2008-12-24 08:19:15 +01:00
|
|
|
uint16_t xor_start=0,xor_mult=0,xor_add=0;
|
2017-01-25 22:28:15 +01:00
|
|
|
|
2017-04-16 00:58:40 +02:00
|
|
|
|
2018-03-28 22:58:49 +02:00
|
|
|
/* checks*/
|
2021-09-19 23:54:06 +02:00
|
|
|
if (read_u16be(0x00,sf) != 0x8000)
|
|
|
|
goto fail;
|
|
|
|
|
2019-11-03 17:54:01 +01:00
|
|
|
/* .adx: standard
|
|
|
|
* .adp: Headhunter (DC) */
|
2021-02-21 22:08:44 +01:00
|
|
|
if (!check_extensions(sf,"adx,adp"))
|
2018-03-28 22:58:49 +02:00
|
|
|
goto fail;
|
2008-01-31 07:04:26 +01:00
|
|
|
|
2021-02-21 22:08:44 +01:00
|
|
|
start_offset = read_u16be(0x02,sf) + 0x04;
|
|
|
|
if (read_u16be(start_offset - 0x06,sf) != 0x2863 || /* "(c" */
|
|
|
|
read_u32be(start_offset - 0x04,sf) != 0x29435249) /* ")CRI" */
|
2019-11-03 17:54:01 +01:00
|
|
|
goto fail;
|
2017-01-25 22:28:15 +01:00
|
|
|
|
2021-02-21 22:08:44 +01:00
|
|
|
encoding_type = read_u8(0x04, sf);
|
2017-05-14 00:37:24 +02:00
|
|
|
switch (encoding_type) {
|
2019-11-03 17:54:01 +01:00
|
|
|
case 0x02:
|
2017-05-14 00:37:24 +02:00
|
|
|
coding_type = coding_CRI_ADX_fixed;
|
|
|
|
break;
|
2019-11-03 17:54:01 +01:00
|
|
|
case 0x03:
|
2017-05-14 00:37:24 +02:00
|
|
|
coding_type = coding_CRI_ADX;
|
|
|
|
break;
|
2019-11-03 17:54:01 +01:00
|
|
|
case 0x04:
|
2017-05-14 00:37:24 +02:00
|
|
|
coding_type = coding_CRI_ADX_exp;
|
|
|
|
break;
|
2019-11-03 17:54:01 +01:00
|
|
|
default: /* 0x10 is AHX for DC, 0x11 is AHX */
|
2017-05-14 00:37:24 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
2017-01-25 22:28:15 +01:00
|
|
|
|
2019-11-03 17:54:01 +01:00
|
|
|
/* ADX encoders can't set this value, but is honored by ADXPlay if changed and multiple of 0x12,
|
|
|
|
* though output is unusual and may not be fully supported (works in mono so not an interleave) */
|
2021-02-21 22:08:44 +01:00
|
|
|
frame_size = read_8bit(0x05, sf);
|
2017-01-25 22:28:15 +01:00
|
|
|
|
2021-02-21 22:08:44 +01:00
|
|
|
if (read_u8(0x06,sf) != 4) /* bits per sample */
|
2019-11-03 17:54:01 +01:00
|
|
|
goto fail;
|
2017-04-16 00:58:40 +02:00
|
|
|
|
|
|
|
/* older ADX (adxencd) up to 2ch, newer ADX (criatomencd) up to 8 */
|
2021-02-21 22:08:44 +01:00
|
|
|
channel_count = read_u8(0x07,sf);
|
2019-11-03 17:54:01 +01:00
|
|
|
/* 0x08: sample rate */
|
|
|
|
/* 0x0c: samples */
|
|
|
|
/* 0x10: high-pass frequency */
|
2017-01-25 22:28:15 +01:00
|
|
|
|
2021-02-21 22:08:44 +01:00
|
|
|
version = read_u16be(0x12,sf);
|
2017-04-16 00:58:40 +02:00
|
|
|
|
2017-01-25 22:28:15 +01:00
|
|
|
/* encryption */
|
2019-11-03 17:54:01 +01:00
|
|
|
if (version == 0x0408) {
|
2021-09-19 23:54:06 +02:00
|
|
|
|
2021-02-21 22:08:44 +01:00
|
|
|
if (find_adx_key(sf, 8, &xor_start, &xor_mult, &xor_add, 0)) {
|
2021-09-19 23:54:06 +02:00
|
|
|
vgm_logi("ADX: decryption keystring not found\n");
|
2017-01-25 22:28:15 +01:00
|
|
|
}
|
2021-09-19 23:54:06 +02:00
|
|
|
coding_type = coding_CRI_ADX_enc_8;
|
|
|
|
version = 0x0400;
|
2017-01-25 22:28:15 +01:00
|
|
|
}
|
2019-11-03 17:54:01 +01:00
|
|
|
else if (version == 0x0409) {
|
2021-02-21 22:08:44 +01:00
|
|
|
if (find_adx_key(sf, 9, &xor_start, &xor_mult, &xor_add, subkey)) {
|
2021-09-19 23:54:06 +02:00
|
|
|
vgm_logi("ADX: decryption keycode not found\n");
|
2017-01-25 22:28:15 +01:00
|
|
|
}
|
2021-09-19 23:54:06 +02:00
|
|
|
coding_type = coding_CRI_ADX_enc_9;
|
|
|
|
version = 0x0400;
|
2017-01-25 22:28:15 +01:00
|
|
|
}
|
|
|
|
|
2017-04-16 00:58:40 +02:00
|
|
|
/* version + extra data */
|
2019-11-03 17:54:01 +01:00
|
|
|
if (version == 0x0300) { /* early ADX (~1998) [Grandia (SAT), Baroque (SAT)] */
|
2017-04-16 00:58:40 +02:00
|
|
|
size_t base_size = 0x14, loops_size = 0x18;
|
|
|
|
|
2017-01-25 22:28:15 +01:00
|
|
|
header_type = meta_ADX_03;
|
|
|
|
|
2017-04-16 00:58:40 +02:00
|
|
|
/* no sample history */
|
2017-01-25 22:28:15 +01:00
|
|
|
|
2019-11-03 17:54:01 +01:00
|
|
|
if (start_offset - 0x06 >= base_size + loops_size) { /* enough space for loop info? */
|
2017-04-16 00:58:40 +02:00
|
|
|
off_t loops_offset = base_size;
|
2017-01-25 22:28:15 +01:00
|
|
|
|
2019-11-03 17:54:01 +01:00
|
|
|
/* 0x00 (2): initial loop padding (the encoder adds a few blank samples so loop start is block-aligned; max 31)
|
2017-04-16 00:58:40 +02:00
|
|
|
* ex. loop_start=12: enc_start=32, padding=20 (32-20=12); loop_start=35: enc_start=64, padding=29 (64-29=35)
|
2019-11-03 17:54:01 +01:00
|
|
|
* 0x02 (2): loop sample(?) flag (always 1) */
|
2021-02-21 22:08:44 +01:00
|
|
|
loop_flag = read_32bitBE(loops_offset+0x04,sf) != 0; /* loop offset(?) flag (always 1) */
|
|
|
|
loop_start_sample = read_32bitBE(loops_offset+0x08,sf);
|
|
|
|
//loop_start_offset = read_32bitBE(loops_offset+0x0c,sf);
|
|
|
|
loop_end_sample = read_32bitBE(loops_offset+0x10,sf);
|
|
|
|
//loop_end_offset = read_32bitBE(loops_offset+0x14,sf);
|
2017-01-25 22:28:15 +01:00
|
|
|
}
|
2017-04-16 00:58:40 +02:00
|
|
|
}
|
2019-11-03 17:54:01 +01:00
|
|
|
else if (version == 0x0400) { /* common */
|
2017-04-16 00:58:40 +02:00
|
|
|
size_t base_size = 0x18, hist_size, ainf_size = 0, loops_size = 0x18;
|
|
|
|
off_t ainf_offset;
|
2017-01-25 22:28:15 +01:00
|
|
|
|
2017-04-16 00:58:40 +02:00
|
|
|
header_type = meta_ADX_04;
|
2017-01-25 22:28:15 +01:00
|
|
|
|
2017-04-16 00:58:40 +02:00
|
|
|
hist_offset = base_size; /* always present but often blank */
|
2019-11-03 17:54:01 +01:00
|
|
|
hist_size = (channel_count > 1 ? 0x04 * channel_count : 0x04 + 0x04); /* min is 8, even in 1ch files */
|
2017-01-25 22:28:15 +01:00
|
|
|
|
2019-11-03 17:54:01 +01:00
|
|
|
ainf_offset = base_size + hist_size + 0x04; /* not seen with >2ch though */
|
2021-02-21 22:08:44 +01:00
|
|
|
if (read_u32be(ainf_offset+0x00,sf) == 0x41494E46) /* "AINF" */
|
|
|
|
ainf_size = read_32bitBE(ainf_offset+0x04,sf);
|
2017-01-25 22:28:15 +01:00
|
|
|
|
2019-11-03 17:54:01 +01:00
|
|
|
if (start_offset - ainf_size - 0x06 >= hist_offset + hist_size + loops_size) { /* enough space for loop info? */
|
2017-04-16 00:58:40 +02:00
|
|
|
off_t loops_offset = base_size + hist_size;
|
2017-01-25 22:28:15 +01:00
|
|
|
|
2019-11-03 17:54:01 +01:00
|
|
|
/* 0x00 (2): initial loop padding (the encoder adds a few blank samples so loop start is block-aligned; max 31)
|
2017-04-16 00:58:40 +02:00
|
|
|
* ex. loop_start=12: enc_start=32, padding=20 (32-20=12); loop_start=35: enc_start=64, padding=29 (64-29=35)
|
2019-11-03 17:54:01 +01:00
|
|
|
* 0x02 (2): loop sample(?) flag (always 1) */
|
2021-02-21 22:08:44 +01:00
|
|
|
loop_flag = read_32bitBE(loops_offset+0x04,sf) != 0; /* loop offset(?) flag (always 1) */
|
|
|
|
loop_start_sample = read_32bitBE(loops_offset+0x08,sf);
|
|
|
|
//loop_start_offset = read_32bitBE(loops_offset+0x0c,sf);
|
|
|
|
loop_end_sample = read_32bitBE(loops_offset+0x10,sf);
|
|
|
|
//loop_end_offset = read_32bitBE(loops_offset+0x14,sf);
|
2017-04-16 00:58:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* AINF header info (may be inserted by CRI's tools but is rarely used)
|
|
|
|
* Can also start right after the loop points (base_size + hist_size + loops_size)
|
2019-11-03 17:54:01 +01:00
|
|
|
* 0x00 (4): "AINF"
|
|
|
|
* 0x04 (4): size
|
|
|
|
* 0x08 (10): str_id
|
2017-04-16 00:58:40 +02:00
|
|
|
* 0x18 (2): volume (0=base/max?, negative=reduce)
|
2019-11-03 17:54:01 +01:00
|
|
|
* 0x1c (2): pan l
|
|
|
|
* 0x1e (2): pan r (0=base, max +-128) */
|
2018-09-02 21:06:38 +02:00
|
|
|
|
|
|
|
/* CINF header info (very rare, found after loops) [Sakura Taisen 3 (PS2)]
|
|
|
|
* 0x00 (4): "CINF"
|
|
|
|
* 0x04 (4): size
|
|
|
|
* 0x08 (4): "ASO ", unknown
|
|
|
|
* 0x28 (4): "SND ", unknown
|
|
|
|
* 0x48 (-): file name, null terminated
|
|
|
|
*/
|
2017-04-16 00:58:40 +02:00
|
|
|
}
|
2019-11-03 17:54:01 +01:00
|
|
|
else if (version == 0x0500) { /* found in some SFD: Buggy Heat, appears to have no loop */
|
2017-04-16 00:58:40 +02:00
|
|
|
header_type = meta_ADX_05;
|
|
|
|
}
|
|
|
|
else { /* not a known/supported version signature */
|
|
|
|
goto fail;
|
2017-01-25 22:28:15 +01:00
|
|
|
}
|
|
|
|
|
2017-04-16 00:58:40 +02:00
|
|
|
|
|
|
|
/* build the VGMSTREAM */
|
2008-01-31 07:04:26 +01:00
|
|
|
vgmstream = allocate_vgmstream(channel_count,loop_flag);
|
|
|
|
if (!vgmstream) goto fail;
|
|
|
|
|
2021-02-21 22:08:44 +01:00
|
|
|
vgmstream->sample_rate = read_32bitBE(0x08,sf);
|
|
|
|
vgmstream->num_samples = read_32bitBE(0x0c,sf);
|
2017-01-25 22:28:15 +01:00
|
|
|
vgmstream->loop_start_sample = loop_start_sample;
|
|
|
|
vgmstream->loop_end_sample = loop_end_sample;
|
|
|
|
|
|
|
|
vgmstream->coding_type = coding_type;
|
2019-11-03 17:54:01 +01:00
|
|
|
vgmstream->layout_type = layout_interleave;
|
2017-05-13 22:17:27 +02:00
|
|
|
vgmstream->interleave_block_size = frame_size;
|
2017-01-25 22:28:15 +01:00
|
|
|
vgmstream->meta_type = header_type;
|
|
|
|
|
|
|
|
|
|
|
|
/* calculate filter coefficients */
|
2017-05-13 23:04:30 +02:00
|
|
|
if (coding_type == coding_CRI_ADX_fixed) {
|
|
|
|
int i;
|
2019-11-03 17:54:01 +01:00
|
|
|
/* standard XA coefs * (2<<11) */
|
2017-05-13 23:04:30 +02:00
|
|
|
for (i = 0; i < channel_count; i++) {
|
|
|
|
vgmstream->ch[i].adpcm_coef[0] = 0x0000;
|
|
|
|
vgmstream->ch[i].adpcm_coef[1] = 0x0000;
|
|
|
|
vgmstream->ch[i].adpcm_coef[2] = 0x0F00;
|
|
|
|
vgmstream->ch[i].adpcm_coef[3] = 0x0000;
|
|
|
|
vgmstream->ch[i].adpcm_coef[4] = 0x1CC0;
|
|
|
|
vgmstream->ch[i].adpcm_coef[5] = 0xF300;
|
|
|
|
vgmstream->ch[i].adpcm_coef[6] = 0x1880;
|
|
|
|
vgmstream->ch[i].adpcm_coef[7] = 0xF240;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2017-01-25 22:28:15 +01:00
|
|
|
double x,y,z,a,b,c;
|
2017-05-14 01:48:35 +02:00
|
|
|
int i;
|
2017-04-16 00:58:40 +02:00
|
|
|
/* high-pass cutoff frequency, always 500 that I've seen */
|
2021-02-21 22:08:44 +01:00
|
|
|
uint16_t cutoff = read_u16be(0x10,sf);
|
2017-01-25 22:28:15 +01:00
|
|
|
|
|
|
|
x = cutoff;
|
|
|
|
y = vgmstream->sample_rate;
|
2019-11-03 17:54:01 +01:00
|
|
|
z = cos(2.0 * M_PI * x / y);
|
2017-01-25 22:28:15 +01:00
|
|
|
|
2019-11-03 17:54:01 +01:00
|
|
|
a = M_SQRT2 - z;
|
|
|
|
b = M_SQRT2 - 1.0;
|
|
|
|
c = (a - sqrt((a + b) * (a - b))) / b;
|
2017-01-25 22:28:15 +01:00
|
|
|
|
2019-11-03 17:54:01 +01:00
|
|
|
coef1 = (short)(c * 8192);
|
|
|
|
coef2 = (short)(c * c * -4096);
|
2017-05-13 23:04:30 +02:00
|
|
|
|
|
|
|
for (i = 0; i < channel_count; i++) {
|
2017-05-14 00:37:24 +02:00
|
|
|
vgmstream->ch[i].adpcm_coef[0] = coef1;
|
|
|
|
vgmstream->ch[i].adpcm_coef[1] = coef2;
|
2017-05-13 23:04:30 +02:00
|
|
|
}
|
2017-01-25 22:28:15 +01:00
|
|
|
}
|
|
|
|
|
2017-04-16 00:58:40 +02:00
|
|
|
/* init decoder */
|
2017-01-25 22:28:15 +01:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2019-11-03 17:54:01 +01:00
|
|
|
for (i = 0; i < channel_count; i++) {
|
2017-04-16 00:58:40 +02:00
|
|
|
/* 2 hist shorts per ch, corresponding to the very first original sample repeated (verified with CRI's encoders).
|
|
|
|
* Not vital as their effect is small, after a few samples they don't matter, and most songs start in silence. */
|
|
|
|
if (hist_offset) {
|
2021-02-21 22:08:44 +01:00
|
|
|
vgmstream->ch[i].adpcm_history1_32 = read_16bitBE(hist_offset + i*4 + 0x00,sf);
|
|
|
|
vgmstream->ch[i].adpcm_history2_32 = read_16bitBE(hist_offset + i*4 + 0x02,sf);
|
2017-04-16 00:58:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (coding_type == coding_CRI_ADX_enc_8 || coding_type == coding_CRI_ADX_enc_9) {
|
2017-01-25 22:28:15 +01:00
|
|
|
int j;
|
|
|
|
vgmstream->ch[i].adx_channels = channel_count;
|
|
|
|
vgmstream->ch[i].adx_xor = xor_start;
|
|
|
|
vgmstream->ch[i].adx_mult = xor_mult;
|
|
|
|
vgmstream->ch[i].adx_add = xor_add;
|
|
|
|
|
2019-11-03 17:54:01 +01:00
|
|
|
for (j = 0; j < i; j++)
|
2017-01-25 22:28:15 +01:00
|
|
|
adx_next_key(&vgmstream->ch[i]);
|
2008-12-24 08:19:15 +01:00
|
|
|
}
|
2008-01-31 07:04:26 +01:00
|
|
|
}
|
2017-01-25 22:28:15 +01:00
|
|
|
}
|
|
|
|
|
2017-04-16 00:58:40 +02:00
|
|
|
|
2021-02-21 22:08:44 +01:00
|
|
|
if ( !vgmstream_open_stream(vgmstream, sf, start_offset) )
|
2017-04-16 00:58:40 +02:00
|
|
|
goto fail;
|
2008-01-31 07:04:26 +01:00
|
|
|
return vgmstream;
|
|
|
|
|
|
|
|
fail:
|
2017-04-16 00:58:40 +02:00
|
|
|
close_vgmstream(vgmstream);
|
2008-01-31 07:04:26 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2008-12-24 09:28:19 +01:00
|
|
|
|
|
|
|
|
2019-11-03 17:54:01 +01:00
|
|
|
/* ADX key detection works by reading XORed ADPCM scales in frames, and un-XORing with keys in
|
|
|
|
* a list. If resulting values are within the expected range for N scales we accept that key. */
|
2021-02-21 22:08:44 +01:00
|
|
|
static int find_adx_key(STREAMFILE* sf, uint8_t type, uint16_t *xor_start, uint16_t *xor_mult, uint16_t *xor_add, uint16_t subkey) {
|
2019-11-03 17:54:01 +01:00
|
|
|
const int frame_size = 0x12;
|
|
|
|
uint16_t *scales = NULL;
|
|
|
|
uint16_t *prescales = NULL;
|
|
|
|
int bruteframe_start = 0, bruteframe_count = -1;
|
|
|
|
off_t start_offset;
|
2018-09-04 17:44:06 +02:00
|
|
|
int i, rc = 0;
|
2008-12-24 09:28:19 +01:00
|
|
|
|
2016-11-27 19:41:36 +01:00
|
|
|
|
|
|
|
/* try to find key in external file first */
|
|
|
|
{
|
2020-04-22 00:21:13 +02:00
|
|
|
uint8_t keybuf[0x40+1] = {0}; /* known max ~0x30, +1 extra null for keystrings */
|
2019-08-25 20:16:06 +02:00
|
|
|
size_t key_size;
|
|
|
|
|
|
|
|
/* handle type8 keystrings, key9 keycodes and derived keys too */
|
2020-04-22 00:21:13 +02:00
|
|
|
key_size = read_key_file(keybuf, sizeof(keybuf), sf);
|
2019-08-25 20:16:06 +02:00
|
|
|
|
|
|
|
if (key_size > 0) {
|
2021-07-29 22:29:49 +02:00
|
|
|
int is_ascii = 0;
|
2019-08-25 20:16:06 +02:00
|
|
|
|
|
|
|
/* keystrings should be ASCII, also needed to tell apart 0x06 strings from derived keys */
|
|
|
|
if (type == 8) {
|
|
|
|
is_ascii = 1;
|
|
|
|
for (i = 0; i < key_size; i++) {
|
|
|
|
if (keybuf[i] < 0x20 || keybuf[i] > 0x7f) {
|
|
|
|
is_ascii = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-27 19:41:36 +01:00
|
|
|
|
2019-08-25 20:16:06 +02:00
|
|
|
if (key_size == 0x06 && !is_ascii) {
|
|
|
|
*xor_start = get_16bitBE(keybuf + 0x00);
|
|
|
|
*xor_mult = get_16bitBE(keybuf + 0x02);
|
|
|
|
*xor_add = get_16bitBE(keybuf + 0x04);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else if (type == 8 && is_ascii) {
|
|
|
|
const char * keystring = (const char *)keybuf;
|
|
|
|
derive_adx_key8(keystring, xor_start, xor_mult, xor_add);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else if (type == 9 && key_size == 0x08) {
|
2021-02-21 22:08:44 +01:00
|
|
|
uint64_t keycode = get_u64be(keybuf);
|
|
|
|
if (subkey) {
|
|
|
|
keycode = keycode * ( ((uint64_t)subkey << 16u) | ((uint16_t)~subkey + 2u) );
|
|
|
|
}
|
|
|
|
derive_adx_key9(keycode, xor_start, xor_mult, xor_add);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else if (type == 9 && key_size == 0x08+0x02) {
|
|
|
|
uint64_t file_key = get_u64be(keybuf+0x00);
|
|
|
|
uint16_t file_sub = get_u16be(keybuf+0x08);
|
|
|
|
uint64_t keycode = file_key * ( ((uint64_t)file_sub << 16u) | ((uint16_t)~file_sub + 2u) );
|
2019-08-25 20:16:06 +02:00
|
|
|
derive_adx_key9(keycode, xor_start, xor_mult, xor_add);
|
|
|
|
return 1;
|
|
|
|
}
|
2016-11-27 19:41:36 +01:00
|
|
|
}
|
2019-08-25 20:16:06 +02:00
|
|
|
/* no key set or unknown format, try list */
|
2016-11-27 19:41:36 +01:00
|
|
|
}
|
|
|
|
|
2018-09-04 17:44:06 +02:00
|
|
|
/* setup totals */
|
|
|
|
{
|
|
|
|
int frame_count;
|
2019-11-03 17:54:01 +01:00
|
|
|
int channels = read_8bit(0x07, sf);
|
|
|
|
int num_samples = read_32bitBE(0x0c, sf);
|
|
|
|
off_t end_offset;
|
2016-11-27 19:41:36 +01:00
|
|
|
|
2019-11-03 17:54:01 +01:00
|
|
|
start_offset = read_16bitBE(0x02, sf) + 0x4;
|
|
|
|
end_offset = (num_samples + 31) / 32 * frame_size * channels + start_offset; /* samples-to-bytes */
|
2008-12-24 09:28:19 +01:00
|
|
|
|
2019-11-03 17:54:01 +01:00
|
|
|
frame_count = (end_offset - start_offset) / frame_size;
|
2018-09-04 17:44:06 +02:00
|
|
|
if (frame_count < bruteframe_count || bruteframe_count < 0)
|
|
|
|
bruteframe_count = frame_count;
|
2008-12-24 09:28:19 +01:00
|
|
|
}
|
|
|
|
|
2019-11-03 17:54:01 +01:00
|
|
|
/* find longest run of non-zero frames (zero frames aren't good for key testing) */
|
2008-12-24 09:28:19 +01:00
|
|
|
{
|
2019-11-03 17:54:01 +01:00
|
|
|
static const uint8_t zeroes[0x12] = {0};
|
|
|
|
uint8_t frame[0x12];
|
|
|
|
int longest_start = -1, longest_count = -1;
|
|
|
|
int count = 0;
|
|
|
|
|
2018-09-04 17:44:06 +02:00
|
|
|
for (i = 0; i < bruteframe_count; i++) {
|
2019-11-03 17:54:01 +01:00
|
|
|
read_streamfile(frame, start_offset + i*frame_size, frame_size, sf);
|
|
|
|
if (memcmp(zeroes, frame, frame_size) != 0)
|
|
|
|
count++;
|
2018-09-04 17:44:06 +02:00
|
|
|
else
|
2019-11-03 17:54:01 +01:00
|
|
|
count = 0;
|
|
|
|
|
|
|
|
/* update new record of non-zero frames */
|
|
|
|
if (count > longest_count) {
|
|
|
|
longest_count = count;
|
|
|
|
longest_start = i - count + 1;
|
|
|
|
if (longest_count >= ADX_KEY_MAX_TEST_FRAMES)
|
2018-09-04 17:44:06 +02:00
|
|
|
break;
|
2008-12-24 09:28:19 +01:00
|
|
|
}
|
|
|
|
}
|
2019-11-03 17:54:01 +01:00
|
|
|
|
|
|
|
/* no non-zero frames */
|
|
|
|
if (longest_start == -1) {
|
|
|
|
goto done;
|
2008-12-24 09:28:19 +01:00
|
|
|
}
|
2019-11-03 17:54:01 +01:00
|
|
|
|
|
|
|
bruteframe_start = longest_start;
|
|
|
|
bruteframe_count = longest_count;
|
|
|
|
if (bruteframe_count > ADX_KEY_MAX_TEST_FRAMES) //?
|
|
|
|
bruteframe_count = ADX_KEY_MAX_TEST_FRAMES;
|
2008-12-24 09:28:19 +01:00
|
|
|
}
|
|
|
|
|
2019-11-03 17:54:01 +01:00
|
|
|
/* pre-load scales in a table, to avoid re-reading them per key */
|
2008-12-24 09:28:19 +01:00
|
|
|
{
|
|
|
|
/* allocate storage for scales */
|
2019-11-03 17:54:01 +01:00
|
|
|
scales = malloc(bruteframe_count * sizeof(uint16_t));
|
|
|
|
if (!scales) goto done;
|
2018-09-04 17:44:06 +02:00
|
|
|
|
2019-11-03 17:54:01 +01:00
|
|
|
/* prescales are scales before the first test frame, with some blank frames no good
|
|
|
|
* for key testing, but we must read to compute XOR value at bruteframe_start */
|
|
|
|
if (bruteframe_start > 0) {
|
|
|
|
/* allocate storage for prescales */
|
|
|
|
prescales = malloc(bruteframe_start * sizeof(uint16_t));
|
|
|
|
if (!prescales) goto done;
|
2018-09-04 17:44:06 +02:00
|
|
|
|
2008-12-24 09:28:19 +01:00
|
|
|
/* read the prescales */
|
2019-11-03 17:54:01 +01:00
|
|
|
for (i = 0; i < bruteframe_start; i++) {
|
|
|
|
prescales[i] = read_16bitBE(start_offset + i*frame_size, sf);
|
2008-12-24 09:28:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* read in the scales */
|
2019-11-03 17:54:01 +01:00
|
|
|
for (i = 0; i < bruteframe_count; i++) {
|
|
|
|
scales[i] = read_16bitBE(start_offset + (bruteframe_start + i)*frame_size, sf);
|
2008-12-24 09:28:19 +01:00
|
|
|
}
|
2019-11-03 17:54:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* try to guess key */
|
|
|
|
{
|
|
|
|
const adxkey_info *keys = NULL;
|
|
|
|
int keycount = 0, keymask = 0;
|
|
|
|
int key_id;
|
2008-12-24 09:28:19 +01:00
|
|
|
|
2019-11-03 17:54:01 +01:00
|
|
|
/* setup test mask (used to check high bits that signal un-XORed scale would be too high to be valid) */
|
2018-09-04 17:44:06 +02:00
|
|
|
if (type == 8) {
|
2017-10-28 12:38:27 +02:00
|
|
|
keys = adxkey8_list;
|
|
|
|
keycount = adxkey8_list_count;
|
2017-06-17 23:23:30 +02:00
|
|
|
keymask = 0x6000;
|
2011-08-20 14:00:01 +02:00
|
|
|
}
|
2019-11-03 17:54:01 +01:00
|
|
|
else { //if (type == 9)
|
2017-06-17 23:23:30 +02:00
|
|
|
/* smarter XOR as seen in PSO2. The scale is technically 13 bits,
|
|
|
|
* but the maximum value assigned by the encoder is 0x1000.
|
|
|
|
* This is written to the ADX file as 0xFFF, leaving the high bit
|
|
|
|
* empty, which is used to validate a key */
|
2017-10-28 12:38:27 +02:00
|
|
|
keys = adxkey9_list;
|
|
|
|
keycount = adxkey9_list_count;
|
2017-06-17 23:23:30 +02:00
|
|
|
keymask = 0x1000;
|
|
|
|
}
|
2011-08-20 14:00:01 +02:00
|
|
|
|
2019-11-03 17:54:01 +01:00
|
|
|
/* try all keys until one decrypts correctly vs expected scales */
|
2018-09-04 17:44:06 +02:00
|
|
|
for (key_id = 0; key_id < keycount; key_id++) {
|
|
|
|
uint16_t key_xor, key_mul, key_add;
|
|
|
|
uint16_t xor, mul, add;
|
2017-06-17 23:23:30 +02:00
|
|
|
|
2018-09-04 17:44:06 +02:00
|
|
|
/* get pre-derived XOR values or derive if needed */
|
|
|
|
if (keys[key_id].start || keys[key_id].mult || keys[key_id].add) {
|
|
|
|
key_xor = keys[key_id].start;
|
|
|
|
key_mul = keys[key_id].mult;
|
|
|
|
key_add = keys[key_id].add;
|
|
|
|
}
|
|
|
|
else if (type == 8 && keys[key_id].key8) {
|
|
|
|
derive_adx_key8(keys[key_id].key8, &key_xor, &key_mul, &key_add);
|
|
|
|
}
|
|
|
|
else if (type == 9 && keys[key_id].key9) {
|
2021-02-21 22:08:44 +01:00
|
|
|
uint64_t keycode = keys[key_id].key9;
|
|
|
|
if (subkey) {
|
|
|
|
keycode = keycode * ( ((uint64_t)subkey << 16u) | ((uint16_t)~subkey + 2u) );
|
|
|
|
}
|
|
|
|
derive_adx_key9(keycode, &key_xor, &key_mul, &key_add);
|
2018-09-04 17:44:06 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
VGM_LOG("ADX: incorrectly defined key id=%i\n", key_id);
|
|
|
|
continue;
|
|
|
|
}
|
2019-11-03 17:54:01 +01:00
|
|
|
|
2018-09-04 17:44:06 +02:00
|
|
|
/* temp test values */
|
|
|
|
xor = key_xor;
|
|
|
|
mul = key_mul;
|
|
|
|
add = key_add;
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
/* derive and print all keys in the list, quick validity test */
|
2017-10-28 18:04:15 +02:00
|
|
|
{
|
2018-09-04 17:44:06 +02:00
|
|
|
uint16_t test_xor, test_mul, test_add;
|
|
|
|
xor = keys[key_id].start;
|
|
|
|
mul = keys[key_id].mult;
|
|
|
|
add = keys[key_id].add;
|
2017-10-28 18:04:15 +02:00
|
|
|
if (type == 8 && keys[key_id].key8) {
|
2018-09-04 17:44:06 +02:00
|
|
|
derive_adx_key8(keys[key_id].key8, &test_xor, &test_mul, &test_add);
|
2017-10-28 18:04:15 +02:00
|
|
|
VGM_LOG("key8: pre=%04x %04x %04x vs calc=%04x %04x %04x = %s (\"%s\")\n",
|
2018-09-04 17:44:06 +02:00
|
|
|
xor,mul,add, test_xor,test_mul,test_add,
|
|
|
|
xor==test_xor && mul==test_mul && add==test_add ? "ok" : "ko", keys[key_id].key8);
|
2017-10-28 18:04:15 +02:00
|
|
|
}
|
|
|
|
else if (type == 9 && keys[key_id].key9) {
|
2018-09-04 17:44:06 +02:00
|
|
|
derive_adx_key9(keys[key_id].key9, &test_xor, &test_mul, &test_add);
|
2017-10-28 18:04:15 +02:00
|
|
|
VGM_LOG("key9: pre=%04x %04x %04x vs calc=%04x %04x %04x = %s (%"PRIu64")\n",
|
2018-09-04 17:44:06 +02:00
|
|
|
xor,mul,add, test_xor,test_mul,test_add,
|
|
|
|
xor==test_xor && mul==test_mul && add==test_add ? "ok" : "ko", keys[key_id].key9);
|
2017-10-28 18:04:15 +02:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-11-03 17:54:01 +01:00
|
|
|
/* test vs prescales while XOR looks valid */
|
|
|
|
for (i = 0; i < bruteframe_start; i++) {
|
|
|
|
if ((prescales[i] & keymask) != (xor & keymask) && prescales[i] != 0)
|
|
|
|
break;
|
2018-09-04 17:44:06 +02:00
|
|
|
xor = xor * mul + add;
|
|
|
|
}
|
2019-11-03 17:54:01 +01:00
|
|
|
if (i != bruteframe_start)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* test vs scales while XOR looks valid */
|
|
|
|
for (i = 0; i < bruteframe_count; i++) {
|
|
|
|
if ((scales[i] & keymask) != (xor & keymask))
|
|
|
|
break;
|
|
|
|
xor = xor * mul + add;
|
2011-08-20 14:00:01 +02:00
|
|
|
}
|
2019-11-03 17:54:01 +01:00
|
|
|
if (i != bruteframe_count)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* all scales are valid, key is good */
|
|
|
|
*xor_start = key_xor;
|
|
|
|
*xor_mult = key_mul;
|
|
|
|
*xor_add = key_add;
|
|
|
|
rc = 1;
|
|
|
|
break;
|
2008-12-24 09:28:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-03 17:54:01 +01:00
|
|
|
done:
|
2018-09-04 17:44:06 +02:00
|
|
|
free(scales);
|
|
|
|
free(prescales);
|
2008-12-24 09:28:19 +01:00
|
|
|
return rc;
|
|
|
|
}
|