Clean some IO streamfile code

This commit is contained in:
bnnm 2020-02-02 21:13:16 +01:00
parent 9b4769c3ad
commit a22defe1a9
5 changed files with 263 additions and 311 deletions

View File

@ -1,51 +1,36 @@
#ifndef _JSTM_STREAMFILE_H_
#define _JSTM_STREAMFILE_H_
#include "../streamfile.h"
typedef struct {
off_t start_offset;
} jstm_decryption_data;
static size_t jstm_decryption_read(STREAMFILE *streamfile, uint8_t *dest, off_t offset, size_t length, jstm_decryption_data* data) {
size_t bytes_read;
int i;
bytes_read = streamfile->read(streamfile, dest, offset, length);
/* decrypt data (xor) */
for (i = 0; i < bytes_read; i++) {
if (offset+i >= data->start_offset) {
dest[i] = dest[i] ^ 0x5A;
}
}
return bytes_read;
}
static STREAMFILE* setup_jstm_streamfile(STREAMFILE *streamFile, off_t start_offset) {
STREAMFILE *temp_streamFile = NULL, *new_streamFile = NULL;
jstm_decryption_data io_data = {0};
size_t io_data_size = sizeof(jstm_decryption_data);
/* setup decryption */
io_data.start_offset = start_offset;
/* setup custom streamfile */
new_streamFile = open_wrap_streamfile(streamFile);
if (!new_streamFile) goto fail;
temp_streamFile = new_streamFile;
new_streamFile = open_io_streamfile(temp_streamFile, &io_data,io_data_size, jstm_decryption_read,NULL);
if (!new_streamFile) goto fail;
temp_streamFile = new_streamFile;
return temp_streamFile;
fail:
close_streamfile(temp_streamFile);
return NULL;
}
#endif /* _JSTM_STREAMFILE_H_ */
#ifndef _JSTM_STREAMFILE_H_
#define _JSTM_STREAMFILE_H_
#include "../streamfile.h"
typedef struct {
off_t start;
} jstm_io_data;
static size_t jstm_io_read(STREAMFILE *sf, uint8_t *dest, off_t offset, size_t length, jstm_io_data* data) {
int i;
size_t bytes = read_streamfile(dest, offset, length, sf);
/* decrypt data (xor) */
for (i = 0; i < bytes; i++) {
if (offset + i >= data->start) {
dest[i] ^= 0x5A;
}
}
return bytes;
}
/* decrypts JSTM stream */
static STREAMFILE* setup_jstm_streamfile(STREAMFILE *sf, off_t start) {
STREAMFILE *new_sf = NULL;
jstm_io_data io_data = {0};
io_data.start = start;
new_sf = open_wrap_streamfile(sf);
new_sf = open_io_streamfile_f(new_sf, &io_data, sizeof(jstm_io_data), jstm_io_read, NULL);
return new_sf;
}
#endif /* _JSTM_STREAMFILE_H_ */

View File

@ -1,76 +1,59 @@
#ifndef _OGG_VORBIS_STREAMFILE_H_
#define _OGG_VORBIS_STREAMFILE_H_
#include "../streamfile.h"
typedef struct {
int is_encrypted;
uint8_t key[0x100];
size_t key_len;
int is_nibble_swap;
int is_header_swap;
} ogg_vorbis_io_config_data;
typedef struct {
/* config */
ogg_vorbis_io_config_data cfg;
} ogg_vorbis_io_data;
static size_t ogg_vorbis_io_read(STREAMFILE *streamfile, uint8_t *dest, off_t offset, size_t length, ogg_vorbis_io_data* data) {
size_t bytes_read;
int i;
static const uint8_t header_swap[4] = { 0x4F,0x67,0x67,0x53 }; /* "OggS" */
static const size_t header_size = 0x04;
bytes_read = streamfile->read(streamfile, dest, offset, length);
if (data->cfg.is_encrypted) {
for (i = 0; i < bytes_read; i++) {
if (data->cfg.is_header_swap && (offset + i) < header_size) {
dest[i] = header_swap[(offset + i) % header_size];
}
else {
if (!data->cfg.key_len && !data->cfg.is_nibble_swap)
break;
if (data->cfg.key_len)
dest[i] ^= data->cfg.key[(offset + i) % data->cfg.key_len];
if (data->cfg.is_nibble_swap)
dest[i] = ((dest[i] << 4) & 0xf0) | ((dest[i] >> 4) & 0x0f);
}
}
}
return bytes_read;
}
//todo maybe use generic decryption streamfile
static STREAMFILE* setup_ogg_vorbis_streamfile(STREAMFILE *streamFile, ogg_vorbis_io_config_data cfg) {
STREAMFILE *temp_streamFile = NULL, *new_streamFile = NULL;
ogg_vorbis_io_data io_data = {0};
size_t io_data_size = sizeof(ogg_vorbis_io_data);
/* setup decryption */
io_data.cfg = cfg; /* memcpy */
/* setup custom streamfile */
new_streamFile = open_wrap_streamfile(streamFile);
if (!new_streamFile) goto fail;
temp_streamFile = new_streamFile;
//todo extension .ogg?
new_streamFile = open_io_streamfile(temp_streamFile, &io_data,io_data_size, ogg_vorbis_io_read,NULL);
if (!new_streamFile) goto fail;
temp_streamFile = new_streamFile;
return temp_streamFile;
fail:
close_streamfile(temp_streamFile);
return NULL;
}
#endif /* _OGG_VORBIS_STREAMFILE_H_ */
#ifndef _OGG_VORBIS_STREAMFILE_H_
#define _OGG_VORBIS_STREAMFILE_H_
#include "../streamfile.h"
typedef struct {
int is_encrypted;
uint8_t key[0x100];
size_t key_len;
int is_nibble_swap;
int is_header_swap;
} ogg_vorbis_io_config_data;
typedef struct {
/* config */
ogg_vorbis_io_config_data cfg;
} ogg_vorbis_io_data;
static size_t ogg_vorbis_io_read(STREAMFILE *sf, uint8_t *dest, off_t offset, size_t length, ogg_vorbis_io_data* data) {
static const uint8_t header_swap[4] = { 0x4F,0x67,0x67,0x53 }; /* "OggS" */
static const size_t header_size = 0x04;
int i;
size_t bytes = read_streamfile(dest, offset, length, sf);
if (data->cfg.is_encrypted) {
for (i = 0; i < bytes; i++) {
if (data->cfg.is_header_swap && (offset + i) < header_size) {
dest[i] = header_swap[(offset + i) % header_size];
}
else {
if (!data->cfg.key_len && !data->cfg.is_nibble_swap)
break;
if (data->cfg.key_len)
dest[i] ^= data->cfg.key[(offset + i) % data->cfg.key_len];
if (data->cfg.is_nibble_swap)
dest[i] = ((dest[i] << 4) & 0xf0) | ((dest[i] >> 4) & 0x0f);
}
}
}
return bytes;
}
//todo maybe use generic decryption streamfile
/* Decrypts Ogg Vorbis streams */
static STREAMFILE* setup_ogg_vorbis_streamfile(STREAMFILE *sf, ogg_vorbis_io_config_data cfg) {
STREAMFILE *new_sf = NULL;
ogg_vorbis_io_data io_data = {0};
io_data.cfg = cfg; /* memcpy */
new_sf = open_wrap_streamfile(sf);
new_sf = open_io_streamfile_f(new_sf, &io_data, sizeof(ogg_vorbis_io_data), ogg_vorbis_io_read, NULL);
//new_sf = open_fakename_streamfile_f(new_sf, NULL, "ogg"); //todo?
return new_sf;
}
#endif /* _OGG_VORBIS_STREAMFILE_H_ */

View File

@ -1,130 +1,127 @@
#ifndef _RIFF_OGG_STREAMFILE_H_
#define _RIFF_OGG_STREAMFILE_H_
#include "../streamfile.h"
#ifdef VGM_USE_VORBIS
typedef struct {
off_t patch_offset;
} riff_ogg_io_data;
static size_t riff_ogg_io_read(STREAMFILE *streamfile, uint8_t *dest, off_t offset, size_t length, riff_ogg_io_data* data) {
size_t bytes_read = streamfile->read(streamfile, dest, offset, length);
/* has garbage init Oggs pages, patch bad flag */
if (data->patch_offset && data->patch_offset >= offset && data->patch_offset < offset + bytes_read) {
VGM_ASSERT(dest[data->patch_offset - offset] != 0x02, "RIFF Ogg: bad patch offset at %lx\n", data->patch_offset);
dest[data->patch_offset - offset] = 0x00;
}
return bytes_read;
}
static size_t ogg_get_page(uint8_t *buf, size_t bufsize, off_t offset, STREAMFILE *sf) {
size_t segments, bytes, page_size;
int i;
if (0x1b > bufsize) goto fail;
bytes = read_streamfile(buf, offset, 0x1b, sf);
if (bytes != 0x1b) goto fail;
segments = get_u8(buf + 0x1a);
if (0x1b + segments > bufsize) goto fail;
bytes = read_streamfile(buf + 0x1b, offset + 0x1b, segments, sf);
if (bytes != segments) goto fail;
page_size = 0x1b + segments;
for (i = 0; i < segments; i++) {
page_size += get_u8(buf + 0x1b + i);
}
return page_size;
fail:
return 0;
}
/* patches Oggs with weirdness */
static STREAMFILE* setup_riff_ogg_streamfile(STREAMFILE *sf, off_t start, size_t size) {
off_t patch_offset = 0;
size_t real_size = size;
uint8_t buf[0x1000];
/* initial page flag is repeated and causes glitches in decoders, find bad offset */
//todo callback could patch on-the-fly by analyzing all "OggS", but is problematic due to arbitrary offsets
{
off_t offset = start;
size_t page_size;
off_t offset_limit = start + size; /* usually in the first 0x3000 but can be +0x100000 */
//todo this doesn't seem to help much
STREAMFILE *temp_sf = reopen_streamfile(sf, 0x100); /* use small-ish sf to avoid reading the whole thing */
/* first page is ok */
page_size = ogg_get_page(buf, sizeof(buf), offset, temp_sf);
offset += page_size;
while (offset < offset_limit) {
page_size = ogg_get_page(buf, sizeof(buf), offset, temp_sf);
if (page_size == 0) break;
if (get_u32be(buf + 0x00) != 0x4f676753) /* "OggS" */
break;
if (get_u16be(buf + 0x04) == 0x0002) { /* start page flag */
//;VGM_ASSERT(patch_offset > 0, "RIFF Ogg: found multiple repeated start pages\n");
patch_offset = (offset - start) + 0x04 + 0x01; /* clamp'ed */
break;
}
offset += page_size;
}
close_streamfile(temp_sf);
if (patch_offset == 0)
return NULL;
}
/* has a bunch of padding(?) pages at the end with no data nor flag that confuse decoders, find actual end */
{
size_t chunk_size = sizeof(buf); /* not worth testing more */
size_t max_size = size;
size_t pos;
off_t read_offset = start + size - chunk_size;
pos = read_streamfile(buf, read_offset, chunk_size, sf);
if (read_offset < 0 || pos <= 0x1a) return NULL;
pos -= 0x1a; /* at least one OggS page */
while (pos > 0) {
if (get_u32be(buf + pos + 0x00) == 0x4f676753) { /* "OggS" */
if (get_u16be(buf + pos + 0x04) == 0x0004) { /* last page flag is ok */
real_size = max_size;
break;
}
else { /* last page flag is wrong */
max_size = size - (chunk_size - pos); /* update size up to this page */
}
}
pos--;
}
}
/* actual custom streamfile init */
{
STREAMFILE *new_sf = NULL;
riff_ogg_io_data io_data = {0};
io_data.patch_offset = patch_offset;
new_sf = open_wrap_streamfile(sf);
new_sf = open_clamp_streamfile_f(new_sf, start, real_size);
new_sf = open_io_streamfile_f(new_sf, &io_data, sizeof(riff_ogg_io_data), riff_ogg_io_read, NULL);
return new_sf;
}
}
#endif /* VGM_USE_VORBIS */
#endif /* _RIFF_OGG_STREAMFILE_H_ */
#ifndef _RIFF_OGG_STREAMFILE_H_
#define _RIFF_OGG_STREAMFILE_H_
#include "deblock_streamfile.h"
typedef struct {
off_t patch_offset;
} riff_ogg_io_data;
static size_t riff_ogg_io_read(STREAMFILE *sf, uint8_t *dest, off_t offset, size_t length, riff_ogg_io_data* data) {
size_t bytes = read_streamfile(dest, offset, length, sf);
/* has garbage init Oggs pages, patch bad flag */
if (data->patch_offset && data->patch_offset >= offset && data->patch_offset < offset + bytes) {
VGM_ASSERT(dest[data->patch_offset - offset] != 0x02, "RIFF Ogg: bad patch offset at %lx\n", data->patch_offset);
dest[data->patch_offset - offset] = 0x00;
}
return bytes;
}
static size_t ogg_get_page(uint8_t *buf, size_t bufsize, off_t offset, STREAMFILE *sf) {
size_t segments, bytes, page_size;
int i;
if (0x1b > bufsize) goto fail;
bytes = read_streamfile(buf, offset, 0x1b, sf);
if (bytes != 0x1b) goto fail;
segments = get_u8(buf + 0x1a);
if (0x1b + segments > bufsize) goto fail;
bytes = read_streamfile(buf + 0x1b, offset + 0x1b, segments, sf);
if (bytes != segments) goto fail;
page_size = 0x1b + segments;
for (i = 0; i < segments; i++) {
page_size += get_u8(buf + 0x1b + i);
}
return page_size;
fail:
return 0;
}
/* patches Ogg with weirdness */
static STREAMFILE* setup_riff_ogg_streamfile(STREAMFILE *sf, off_t start, size_t size) {
off_t patch_offset = 0;
size_t real_size = size;
uint8_t buf[0x1000];
/* initial page flag is repeated and causes glitches in decoders, find bad offset */
//todo callback could patch on-the-fly by analyzing all "OggS", but is problematic due to arbitrary offsets
{
off_t offset = start;
size_t page_size;
off_t offset_limit = start + size; /* usually in the first 0x3000 but can be +0x100000 */
//todo this doesn't seem to help much
STREAMFILE *temp_sf = reopen_streamfile(sf, 0x100); /* use small-ish sf to avoid reading the whole thing */
/* first page is ok */
page_size = ogg_get_page(buf, sizeof(buf), offset, temp_sf);
offset += page_size;
while (offset < offset_limit) {
page_size = ogg_get_page(buf, sizeof(buf), offset, temp_sf);
if (page_size == 0) break;
if (get_u32be(buf + 0x00) != 0x4f676753) /* "OggS" */
break;
if (get_u16be(buf + 0x04) == 0x0002) { /* start page flag */
//;VGM_ASSERT(patch_offset > 0, "RIFF Ogg: found multiple repeated start pages\n");
patch_offset = (offset - start) + 0x04 + 0x01; /* clamp'ed */
break;
}
offset += page_size;
}
close_streamfile(temp_sf);
if (patch_offset == 0)
return NULL;
}
/* has a bunch of padding(?) pages at the end with no data nor flag that confuse decoders, find actual end */
{
size_t chunk_size = sizeof(buf); /* not worth testing more */
size_t max_size = size;
size_t pos;
off_t read_offset = start + size - chunk_size;
pos = read_streamfile(buf, read_offset, chunk_size, sf);
if (read_offset < 0 || pos <= 0x1a) return NULL;
pos -= 0x1a; /* at least one OggS page */
while (pos > 0) {
if (get_u32be(buf + pos + 0x00) == 0x4f676753) { /* "OggS" */
if (get_u16be(buf + pos + 0x04) == 0x0004) { /* last page flag is ok */
real_size = max_size;
break;
}
else { /* last page flag is wrong */
max_size = size - (chunk_size - pos); /* update size up to this page */
}
}
pos--;
}
}
/* actual custom streamfile init */
{
STREAMFILE *new_sf = NULL;
riff_ogg_io_data io_data = {0};
io_data.patch_offset = patch_offset;
new_sf = open_wrap_streamfile(sf);
new_sf = open_clamp_streamfile_f(new_sf, start, real_size);
new_sf = open_io_streamfile_f(new_sf, &io_data, sizeof(riff_ogg_io_data), riff_ogg_io_read, NULL);
return new_sf;
}
}
#endif /* _RIFF_OGG_STREAMFILE_H_ */

View File

@ -69,7 +69,7 @@ VGMSTREAM * init_vgmstream_vsv(STREAMFILE *streamFile) {
vgmstream->layout_type = layout_interleave;
vgmstream->interleave_block_size = interleave;
temp_streamFile = setup_vsv_streamfile(streamFile, start_offset, data_size);
temp_streamFile = setup_vsv_streamfile(streamFile);
if (!temp_streamFile) goto fail;
if (!vgmstream_open_stream(vgmstream, temp_streamFile, start_offset))

View File

@ -1,53 +1,40 @@
#ifndef _VSV_STREAMFILE_H_
#define _VSV_STREAMFILE_H_
#include "../streamfile.h"
typedef struct {
off_t null_offset;
} vsv_io_data;
static size_t vsv_io_read(STREAMFILE *streamfile, uint8_t *dest, off_t offset, size_t length, vsv_io_data* data) {
size_t bytes_read;
int i;
bytes_read = streamfile->read(streamfile, dest, offset, length);
/* VSVs do start at 0x00, but first line is also the header; must null it to avoid clicks */
if (offset < data->null_offset) {
int max = data->null_offset - offset;
if (max > bytes_read)
max = bytes_read;
for (i = 0; i < max; i++) {
dest[i] = 0;
}
}
/* VSV also has last 0x800 block with a PS-ADPCM flag of 0x10 (incorrect), but it's ignored by the decoder */
return bytes_read;
}
static STREAMFILE* setup_vsv_streamfile(STREAMFILE *streamFile, off_t start_offset, size_t data_size) {
STREAMFILE *temp_streamFile = NULL, *new_streamFile = NULL;
vsv_io_data io_data = {0};
size_t io_data_size = sizeof(vsv_io_data);
io_data.null_offset = 0x10;
/* setup custom streamfile */
new_streamFile = open_wrap_streamfile(streamFile);
if (!new_streamFile) goto fail;
temp_streamFile = new_streamFile;
new_streamFile = open_io_streamfile(temp_streamFile, &io_data,io_data_size, vsv_io_read,NULL);
if (!new_streamFile) goto fail;
temp_streamFile = new_streamFile;
return temp_streamFile;
fail:
close_streamfile(temp_streamFile);
return NULL;
}
#endif /* _VSV_STREAMFILE_H_ */
#ifndef _VSV_STREAMFILE_H_
#define _VSV_STREAMFILE_H_
#include "../streamfile.h"
typedef struct {
off_t null_offset;
} vsv_io_data;
static size_t vsv_io_read(STREAMFILE *sf, uint8_t *dest, off_t offset, size_t length, vsv_io_data *data) {
int i;
size_t bytes = read_streamfile(dest, offset, length, sf);
/* VSVs do start at 0x00, but first line is also the header; must null it to avoid clicks */
if (offset < data->null_offset) {
int max = data->null_offset - offset;
if (max > bytes)
max = bytes;
for (i = 0; i < max; i++) {
dest[i] = 0;
}
}
/* VSV also has last 0x800 block with a PS-ADPCM flag of 0x10 (incorrect), but it's ignored by the decoder */
return bytes;
}
/* cleans VSV data */
static STREAMFILE* setup_vsv_streamfile(STREAMFILE *sf) {
STREAMFILE *new_sf = NULL;
vsv_io_data io_data = {0};
io_data.null_offset = 0x10;
new_sf = open_wrap_streamfile(sf);
new_sf = open_io_streamfile_f(new_sf, &io_data, sizeof(vsv_io_data), vsv_io_read, NULL);
return new_sf;
}
#endif /* _VSV_STREAMFILE_H_ */