Add encrypted .bgm [Nanami to Konomi no Oshiete ABC (PC)]

This commit is contained in:
bnnm 2021-04-10 19:46:10 +02:00
parent 92db12dc64
commit 84200c4cb9
5 changed files with 90 additions and 3 deletions

View File

@ -292,6 +292,10 @@
RelativePath=".\meta\ea_schl_streamfile.h"
>
</File>
<File
RelativePath=".\meta\encrypted_bgm_streamfile.h"
>
</File>
<File
RelativePath=".\meta\fsb_encrypted_streamfile.h"
>
@ -1271,7 +1275,7 @@
>
</File>
<File
RelativePath=".\meta\ps2_exst.c"
RelativePath=".\meta\exst.c"
>
</File>
<File

View File

@ -115,6 +115,7 @@
<ClInclude Include="meta\ea_eaac_streamfile.h" />
<ClInclude Include="meta\ea_eaac_opus_streamfile.h" />
<ClInclude Include="meta\ea_schl_streamfile.h" />
<ClInclude Include="meta\encrypted_bgm_streamfile.h" />
<ClInclude Include="meta\fsb_encrypted_streamfile.h" />
<ClInclude Include="meta\fsb_interleave_streamfile.h" />
<ClInclude Include="meta\fsb5_streamfile.h" />
@ -445,7 +446,7 @@
<ClCompile Include="meta\ps2_ccc.c" />
<ClCompile Include="meta\ps2_dxh.c" />
<ClCompile Include="meta\ps2_enth.c" />
<ClCompile Include="meta\ps2_exst.c" />
<ClCompile Include="meta\exst.c" />
<ClCompile Include="meta\ps2_filp.c" />
<ClCompile Include="meta\ps2_gbts.c" />
<ClCompile Include="meta\ps2_gcm.c" />

View File

@ -110,6 +110,9 @@
<ClInclude Include="meta\ea_schl_streamfile.h">
<Filter>meta\Header Files</Filter>
</ClInclude>
<ClInclude Include="meta\encrypted_bgm_streamfile">
<Filter>meta\Header Files</Filter>
</ClInclude>
<ClInclude Include="meta\fsb_encrypted_streamfile.h">
<Filter>meta\Header Files</Filter>
</ClInclude>
@ -832,7 +835,7 @@
<ClCompile Include="meta\ps2_enth.c">
<Filter>meta\Source Files</Filter>
</ClCompile>
<ClCompile Include="meta\ps2_exst.c">
<ClCompile Include="meta\exst.c">
<Filter>meta\Source Files</Filter>
</ClCompile>
<ClCompile Include="meta\ps2_filp.c">

View File

@ -1,6 +1,7 @@
#include "meta.h"
#include "../coding/coding.h"
#include "ogg_vorbis_streamfile.h"
#include "encrypted_bgm_streamfile.h"
//todo fuse ogg encryptions and use generic names
@ -80,6 +81,32 @@ VGMSTREAM* init_vgmstream_encrypted(STREAMFILE* sf) {
return vgmstream;
}
if (check_extensions(sf,"bgm")) {
uint8_t keybuf[0x100];
size_t key_size;
off_t start;
/* Studio Ring games [Nanami to Konomi no Oshiete ABC (PC), Oyatsu no Jikan (PC)] */
if (id != get_id32be("RIFF"))
goto fail;
/* Standard RIFF xor'd past "data", sometimes including extra chunks like JUNK or smpl.
* If .bgm is added to riff.c this needs to be reworked so detection goes first, or bgm+bgmkey is
* rejected in riff.c (most files are rejected due to the xor'd extra chunks though). */
key_size = read_key_file(keybuf, sizeof(keybuf), sf);
if (key_size <= 0) goto fail;
if (!find_chunk_le(sf, get_id32be("data"), 0x0c, 0, &start, NULL))
goto fail;
temp_sf = setup_bgm_streamfile(sf, start, keybuf, key_size);
if (!temp_sf) goto fail;
vgmstream = init_vgmstream_riff(temp_sf);
close_streamfile(temp_sf);
return vgmstream;
}
fail:
return NULL;

View File

@ -0,0 +1,52 @@
#ifndef _BGM_STREAMFILE_H_
#define _BGM_STREAMFILE_H_
#include "../streamfile.h"
typedef struct {
uint8_t key[0x100];
size_t key_len;
off_t start;
} bgm_io_data;
static size_t bgm_io_read(STREAMFILE *sf, uint8_t *dest, off_t offset, size_t length, bgm_io_data* data) {
int i, begin, pos;
size_t bytes = read_streamfile(dest, offset, length, sf);
/* decrypt data (xor) */
if (offset + length > data->start) {
if (offset < data->start) {
begin = data->start - offset;
pos = 0;
}
else {
begin = 0;
pos = offset - data->start;
}
for (i = begin; i < bytes; i++) {
dest[i] ^= data->key[(pos++) % data->key_len];
}
}
return bytes;
}
/* decrypts BGM stream */
static STREAMFILE* setup_bgm_streamfile(STREAMFILE *sf, off_t start, uint8_t* key, int key_len) {
STREAMFILE *new_sf = NULL;
bgm_io_data io_data = {0};
io_data.start = start;
io_data.key_len = key_len;
if (key_len > sizeof(io_data.key))
return NULL;
memcpy(io_data.key, key, key_len);
new_sf = open_wrap_streamfile(sf);
new_sf = open_io_streamfile_f(new_sf, &io_data, sizeof(bgm_io_data), bgm_io_read, NULL);
new_sf = open_fakename_streamfile_f(new_sf, NULL, "wav");
return new_sf;
}
#endif /* _BGM_STREAMFILE_H_ */