2008-07-05 13:49:29 +02:00
|
|
|
#include "meta.h"
|
2017-02-17 18:18:06 +01:00
|
|
|
#include "../coding/coding.h"
|
2022-12-31 17:31:46 +01:00
|
|
|
#include "ahx_keys.h"
|
|
|
|
#include "../util/cri_keys.h"
|
|
|
|
|
|
|
|
#ifdef VGM_USE_MPEG
|
|
|
|
static int find_ahx_key(STREAMFILE* sf, off_t offset, crikey_t* crikey);
|
2022-07-23 14:55:26 +02:00
|
|
|
#endif
|
2008-07-05 13:49:29 +02:00
|
|
|
|
2022-12-31 17:31:46 +01:00
|
|
|
|
2022-07-23 14:55:26 +02:00
|
|
|
/* AHX - CRI voice format */
|
|
|
|
VGMSTREAM* init_vgmstream_ahx(STREAMFILE* sf) {
|
|
|
|
VGMSTREAM* vgmstream = NULL;
|
|
|
|
uint32_t start_offset;
|
|
|
|
int channels = 1, loop_flag = 0, type;
|
2008-07-05 13:49:29 +02:00
|
|
|
|
2022-07-23 14:55:26 +02:00
|
|
|
/* checks */
|
|
|
|
if (read_u16be(0x00,sf) != 0x8000)
|
|
|
|
goto fail;
|
2008-07-05 13:49:29 +02:00
|
|
|
|
2022-07-23 14:55:26 +02:00
|
|
|
if (!check_extensions(sf, "ahx") )
|
|
|
|
goto fail;
|
2017-07-29 23:16:30 +02:00
|
|
|
|
2022-07-23 14:55:26 +02: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" */
|
2017-07-29 23:16:30 +02:00
|
|
|
goto fail;
|
|
|
|
|
2022-12-31 17:31:46 +01:00
|
|
|
/* types: 0x10 = AHX for DC with fixed MPEG frame bits (bigger frames), 0x11 = standard AHX, 0x0N = ADX */
|
2022-07-23 14:55:26 +02:00
|
|
|
type = read_u8(0x04,sf);
|
2017-08-04 21:48:55 +02:00
|
|
|
if (type != 0x10 && type != 0x11) goto fail;
|
2008-07-05 13:49:29 +02:00
|
|
|
|
2022-07-23 14:55:26 +02:00
|
|
|
/* frame size (0 for AHX) */
|
|
|
|
if (read_u8(0x05,sf) != 0) goto fail;
|
2008-07-05 13:49:29 +02:00
|
|
|
|
|
|
|
/* check for bits per sample? (0 for AHX) */
|
2022-07-23 14:55:26 +02:00
|
|
|
if (read_u8(0x06,sf) != 0) goto fail;
|
2008-07-05 13:49:29 +02:00
|
|
|
|
2017-07-29 23:16:30 +02:00
|
|
|
/* check channel count (only mono AHXs can be created by the encoder) */
|
2022-07-23 14:55:26 +02:00
|
|
|
if (read_u8(0x07,sf) != 1) goto fail;
|
2008-07-05 13:49:29 +02:00
|
|
|
|
2017-07-29 23:16:30 +02:00
|
|
|
/* check version signature */
|
2022-07-23 14:55:26 +02:00
|
|
|
if (read_u8(0x12,sf) != 0x06) goto fail;
|
|
|
|
|
2008-07-05 13:49:29 +02:00
|
|
|
|
2017-07-29 23:16:30 +02:00
|
|
|
/* build the VGMSTREAM */
|
2022-07-23 14:55:26 +02:00
|
|
|
vgmstream = allocate_vgmstream(channels, loop_flag);
|
2008-07-05 13:49:29 +02:00
|
|
|
if (!vgmstream) goto fail;
|
|
|
|
|
2022-07-23 14:55:26 +02:00
|
|
|
vgmstream->sample_rate = read_s32be(0x08,sf); /* real sample rate */
|
|
|
|
vgmstream->num_samples = read_s32be(0x0c,sf); /* doesn't include encoder_delay (handled in decoder) */
|
2008-07-05 13:49:29 +02:00
|
|
|
|
|
|
|
vgmstream->meta_type = meta_AHX;
|
2017-07-29 23:16:30 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
#ifdef VGM_USE_MPEG
|
2018-07-06 19:37:26 +02:00
|
|
|
mpeg_custom_config cfg = {0};
|
2022-12-31 17:31:46 +01:00
|
|
|
crikey_t* crikey = &cfg.crikey;
|
2017-07-29 23:16:30 +02:00
|
|
|
|
2022-12-31 17:31:46 +01:00
|
|
|
cfg.encryption = read_u8(0x13,sf); /* only type 0x08 is known */
|
|
|
|
crikey->type = cfg.encryption;
|
2017-08-04 21:48:55 +02:00
|
|
|
|
|
|
|
if (cfg.encryption) {
|
2022-12-31 17:31:46 +01:00
|
|
|
find_ahx_key(sf, start_offset, crikey);
|
2017-08-04 21:48:55 +02:00
|
|
|
}
|
2017-07-29 23:16:30 +02:00
|
|
|
|
|
|
|
vgmstream->layout_type = layout_none;
|
2022-07-23 14:55:26 +02:00
|
|
|
vgmstream->codec_data = init_mpeg_custom(sf, start_offset, &vgmstream->coding_type, channels, MPEG_AHX, &cfg);
|
2017-07-29 23:16:30 +02:00
|
|
|
if (!vgmstream->codec_data) goto fail;
|
|
|
|
#else
|
|
|
|
goto fail;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-07-23 14:55:26 +02:00
|
|
|
if (!vgmstream_open_stream(vgmstream, sf, start_offset))
|
2017-02-17 18:35:58 +01:00
|
|
|
goto fail;
|
2008-07-05 13:49:29 +02:00
|
|
|
return vgmstream;
|
|
|
|
|
|
|
|
fail:
|
2017-02-17 18:18:06 +01:00
|
|
|
close_vgmstream(vgmstream);
|
2008-07-05 13:49:29 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
2022-12-31 17:31:46 +01:00
|
|
|
|
|
|
|
#ifdef VGM_USE_MPEG
|
|
|
|
static int find_ahx_keyfile(STREAMFILE* sf, crikey_t* crikey) {
|
|
|
|
uint8_t keybuf[0x10+1] = {0}; /* approximate max for keystrings, +1 extra null for keystrings */
|
|
|
|
size_t key_size;
|
|
|
|
int is_keystring = 0;
|
|
|
|
|
|
|
|
key_size = read_key_file(keybuf, sizeof(keybuf) - 1, sf);
|
|
|
|
if (key_size <= 0)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
|
|
|
|
if (crikey->type == 8) {
|
|
|
|
is_keystring = cri_key8_valid_keystring(keybuf, key_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (key_size == 0x06 && !is_keystring) {
|
|
|
|
crikey->key1 = get_u16be(keybuf + 0x00);
|
|
|
|
crikey->key2 = get_u16be(keybuf + 0x02);
|
|
|
|
crikey->key3 = get_u16be(keybuf + 0x04);
|
|
|
|
}
|
|
|
|
else if (crikey->type == 8 && is_keystring) {
|
|
|
|
const char* keystring = (const char*)keybuf;
|
|
|
|
cri_key8_derive(keystring, &crikey->key1, &crikey->key2, &crikey->key3);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
fail:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int find_ahx_keylist(STREAMFILE* sf, off_t offset, crikey_t* crikey) {
|
|
|
|
int i;
|
|
|
|
int keycount = ahxkey8_list_count;
|
|
|
|
const ahxkey_info* keys = ahxkey8_list;
|
|
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < keycount; i++) {
|
|
|
|
if (crikey->type == 0x08) {
|
|
|
|
cri_key8_derive(keys[i].key8, &crikey->key1, &crikey->key2, &crikey->key3);
|
|
|
|
//;VGM_LOG("AHX: testing %s [%04x %04x %04x]\n", keys[i].key8, crikey->key1, crikey->key2, crikey->key3);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (test_ahx_key(sf, offset, crikey)) {
|
|
|
|
//;VGM_LOG("AHX key found\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int find_ahx_key(STREAMFILE* sf, off_t offset, crikey_t* crikey) {
|
|
|
|
int ok;
|
|
|
|
|
|
|
|
ok = find_ahx_keyfile(sf, crikey);
|
|
|
|
if (ok)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
ok = find_ahx_keylist(sf, offset, crikey);
|
|
|
|
if (ok)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
|
|
|
|
crikey->key1 = 0;
|
|
|
|
crikey->key2 = 0;
|
|
|
|
crikey->key3 = 0;
|
|
|
|
vgm_logi("AHX: decryption key not found\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|