diff --git a/src/meta/sqex_sead.c b/src/meta/sqex_sead.c index 2c98eaa2..3e1d6d08 100644 --- a/src/meta/sqex_sead.c +++ b/src/meta/sqex_sead.c @@ -2,13 +2,14 @@ #include "../coding/coding.h" -static STREAMFILE* setup_sead_hca_streamfile(STREAMFILE *streamFile, off_t subfile_offset, size_t subfile_size); +static STREAMFILE* setup_sead_hca_streamfile(STREAMFILE *streamFile, off_t subfile_offset, size_t subfile_size, int encryption, size_t header_size, size_t key_start); -/* SABF/MABF - Square Enix's "Sead" audio games [Dragon Quest Builders (PS3), Dissidia Opera Omnia (mobile), FF XV (PS4)] */ +/* SABF/MABF - Square Enix's "sead" audio games [Dragon Quest Builders (PS3), Dissidia Opera Omnia (mobile), FF XV (PS4)] */ VGMSTREAM * init_vgmstream_sqex_sead(STREAMFILE * streamFile) { VGMSTREAM * vgmstream = NULL; off_t start_offset, mtrl_offset, meta_offset, post_meta_offset; //, info_offset, name_offset = 0; - size_t stream_size, subheader_size; //, name_size = 0; + size_t stream_size, subheader_size, special_size; //, name_size = 0; + int loop_flag = 0, channel_count, codec, sample_rate, loop_start, loop_end; int is_sab = 0, is_mab = 0; @@ -114,7 +115,7 @@ VGMSTREAM * init_vgmstream_sqex_sead(STREAMFILE * streamFile) { loop_end = read_32bit(meta_offset+0x10,streamFile); subheader_size = read_32bit(meta_offset+0x14,streamFile); /* including subfile header */ stream_size = read_32bit(meta_offset+0x18,streamFile); /* not including subfile header */ - /* 0x1c: null? */ + special_size = read_32bit(meta_offset+0x1c,streamFile); loop_flag = (loop_end > 0); post_meta_offset = meta_offset + 0x20; @@ -122,7 +123,7 @@ VGMSTREAM * init_vgmstream_sqex_sead(STREAMFILE * streamFile) { /** info section (get stream name) **/ //if (is_sab) { //todo load name based on entry id - /* "snd ": unknown flags/sizes and name */ + /* "snd " */ /* 0x08(2): file number within descriptor */ /* 0x1a(2): base_entry size (-0x10?) */ //name_size = read_32bit(snd_offset+0x20,streamFile); @@ -130,7 +131,11 @@ VGMSTREAM * init_vgmstream_sqex_sead(STREAMFILE * streamFile) { /* 0x24(4): unique id? (referenced in "seq" section) */ //} //else if (is_mab) { - /* "musc": unknown flags sizes and names, another format */ + /* "musc" */ + //looks like a "music cue" section, pointing to one subsection per "material". + // ex. one cue may point to 3 named subsongs/sections. + // some common header info from all materials is repeated (ex. sample rate), while other + // (loops, maybe proper num_samples) are listed per material but don't always match thei header //} @@ -170,12 +175,12 @@ VGMSTREAM * init_vgmstream_sqex_sead(STREAMFILE * streamFile) { cfg.channels = vgmstream->channels; cfg.config_data = read_32bit(post_meta_offset+0x0c,streamFile); cfg.encoder_delay = read_32bit(post_meta_offset+0x18,streamFile); -VGM_LOG("1\n"); + vgmstream->codec_data = init_atrac9(&cfg); if (!vgmstream->codec_data) goto fail; vgmstream->coding_type = coding_ATRAC9; vgmstream->layout_type = layout_none; -VGM_LOG("2\n"); + vgmstream->sample_rate = read_32bit(post_meta_offset+0x1c,streamFile); /* SAB's sample rate can be different but it's ignored */ vgmstream->num_samples = read_32bit(post_meta_offset+0x10,streamFile); /* loop values above are also weird and ignored */ vgmstream->loop_start_sample = read_32bit(post_meta_offset+0x20, streamFile) - (loop_flag ? cfg.encoder_delay : 0); //loop_start @@ -185,7 +190,7 @@ VGM_LOG("2\n"); #endif #ifdef VGM_USE_MPEG - case 0x06: { /* MSF subfile (MPEG mode) [Dragon Quest Builders (PS3)] */ + case 0x06: { /* MSF subfile (MPEG mode) [Dragon Quest Builders (PS3)] */ mpeg_codec_data *mpeg_data = NULL; mpeg_custom_config cfg = {0}; @@ -210,10 +215,14 @@ VGM_LOG("2\n"); STREAMFILE *temp_streamFile = NULL; off_t subfile_offset = post_meta_offset + 0x10; size_t subfile_size = stream_size + subheader_size - 0x10; - /* post header has 0x10 unknown + HCA header */ + /* post header: values from the HCA header, in file endianness + HCA header */ + size_t key_start = special_size & 0xff; + size_t header_size = read_16bit(post_meta_offset+0x02, streamFile); + int encryption = read_16bit(post_meta_offset+0x0c, streamFile); //maybe 8bit? + /* encryption type 0x01 found in Final Fantasy XII TZA (PS4/PC) */ - temp_streamFile = setup_sead_hca_streamfile(streamFile, subfile_offset, subfile_size); + temp_streamFile = setup_sead_hca_streamfile(streamFile, subfile_offset, subfile_size, encryption, header_size, key_start); if (!temp_streamFile) goto fail; temp_vgmstream = init_vgmstream_hca(temp_streamFile); @@ -249,7 +258,49 @@ fail: return NULL; } -static STREAMFILE* setup_sead_hca_streamfile(STREAMFILE *streamFile, off_t subfile_offset, size_t subfile_size) { + +typedef struct { + size_t header_size; + size_t key_start; +} sead_decryption_data; + +/* Encrypted HCA */ +static size_t sead_decryption_read(STREAMFILE *streamfile, uint8_t *dest, off_t offset, size_t length, sead_decryption_data* data) { + /* Found in FFXII_TZA.exe (same key in SCD Ogg V3) */ + static const uint8_t encryption_key[0x100] = { + 0x3A, 0x32, 0x32, 0x32, 0x03, 0x7E, 0x12, 0xF7, 0xB2, 0xE2, 0xA2, 0x67, 0x32, 0x32, 0x22, 0x32, // 00-0F + 0x32, 0x52, 0x16, 0x1B, 0x3C, 0xA1, 0x54, 0x7B, 0x1B, 0x97, 0xA6, 0x93, 0x1A, 0x4B, 0xAA, 0xA6, // 10-1F + 0x7A, 0x7B, 0x1B, 0x97, 0xA6, 0xF7, 0x02, 0xBB, 0xAA, 0xA6, 0xBB, 0xF7, 0x2A, 0x51, 0xBE, 0x03, // 20-2F + 0xF4, 0x2A, 0x51, 0xBE, 0x03, 0xF4, 0x2A, 0x51, 0xBE, 0x12, 0x06, 0x56, 0x27, 0x32, 0x32, 0x36, // 30-3F + 0x32, 0xB2, 0x1A, 0x3B, 0xBC, 0x91, 0xD4, 0x7B, 0x58, 0xFC, 0x0B, 0x55, 0x2A, 0x15, 0xBC, 0x40, // 40-4F + 0x92, 0x0B, 0x5B, 0x7C, 0x0A, 0x95, 0x12, 0x35, 0xB8, 0x63, 0xD2, 0x0B, 0x3B, 0xF0, 0xC7, 0x14, // 50-5F + 0x51, 0x5C, 0x94, 0x86, 0x94, 0x59, 0x5C, 0xFC, 0x1B, 0x17, 0x3A, 0x3F, 0x6B, 0x37, 0x32, 0x32, // 60-6F + 0x30, 0x32, 0x72, 0x7A, 0x13, 0xB7, 0x26, 0x60, 0x7A, 0x13, 0xB7, 0x26, 0x50, 0xBA, 0x13, 0xB4, // 70-7F + 0x2A, 0x50, 0xBA, 0x13, 0xB5, 0x2E, 0x40, 0xFA, 0x13, 0x95, 0xAE, 0x40, 0x38, 0x18, 0x9A, 0x92, // 80-8F + 0xB0, 0x38, 0x00, 0xFA, 0x12, 0xB1, 0x7E, 0x00, 0xDB, 0x96, 0xA1, 0x7C, 0x08, 0xDB, 0x9A, 0x91, // 90-9F + 0xBC, 0x08, 0xD8, 0x1A, 0x86, 0xE2, 0x70, 0x39, 0x1F, 0x86, 0xE0, 0x78, 0x7E, 0x03, 0xE7, 0x64, // A0-AF + 0x51, 0x9C, 0x8F, 0x34, 0x6F, 0x4E, 0x41, 0xFC, 0x0B, 0xD5, 0xAE, 0x41, 0xFC, 0x0B, 0xD5, 0xAE, // B0-BF + 0x41, 0xFC, 0x3B, 0x70, 0x71, 0x64, 0x33, 0x32, 0x12, 0x32, 0x32, 0x36, 0x70, 0x34, 0x2B, 0x56, // C0-CF + 0x22, 0x70, 0x3A, 0x13, 0xB7, 0x26, 0x60, 0xBA, 0x1B, 0x94, 0xAA, 0x40, 0x38, 0x00, 0xFA, 0xB2, // D0-DF + 0xE2, 0xA2, 0x67, 0x32, 0x32, 0x12, 0x32, 0xB2, 0x32, 0x32, 0x32, 0x32, 0x75, 0xA3, 0x26, 0x7B, // E0-EF + 0x83, 0x26, 0xF9, 0x83, 0x2E, 0xFF, 0xE3, 0x16, 0x7D, 0xC0, 0x1E, 0x63, 0x21, 0x07, 0xE3, 0x01, // F0-FF + }; + size_t bytes_read; + int i; + + bytes_read = streamfile->read(streamfile, dest, offset, length); + + /* decrypt data (xor) */ + if (offset >= data->header_size) { + for (i = 0; i < bytes_read; i++) { + dest[i] ^= encryption_key[(data->key_start + (offset - data->header_size) + i) % 0x100]; + } + } + + return bytes_read; +} + +static STREAMFILE* setup_sead_hca_streamfile(STREAMFILE *streamFile, off_t subfile_offset, size_t subfile_size, int encryption, size_t header_size, size_t key_start) { STREAMFILE *temp_streamFile = NULL, *new_streamFile = NULL; /* setup subfile */ @@ -261,6 +312,18 @@ static STREAMFILE* setup_sead_hca_streamfile(STREAMFILE *streamFile, off_t subfi if (!new_streamFile) goto fail; temp_streamFile = new_streamFile; + if (encryption) { + sead_decryption_data io_data = {0}; + size_t io_data_size = sizeof(sead_decryption_data); + + io_data.header_size = header_size; + io_data.key_start = key_start; + + new_streamFile = open_io_streamfile(temp_streamFile, &io_data,io_data_size, sead_decryption_read); + if (!new_streamFile) goto fail; + temp_streamFile = new_streamFile; + } + new_streamFile = open_fakename_streamfile(temp_streamFile, NULL,"hca"); if (!new_streamFile) goto fail; temp_streamFile = new_streamFile;