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

@ -4,48 +4,33 @@
typedef struct {
off_t start_offset;
} jstm_decryption_data;
off_t start;
} jstm_io_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;
static size_t jstm_io_read(STREAMFILE *sf, uint8_t *dest, off_t offset, size_t length, jstm_io_data* data) {
int i;
bytes_read = streamfile->read(streamfile, dest, offset, length);
size_t bytes = read_streamfile(dest, offset, length, sf);
/* decrypt data (xor) */
for (i = 0; i < bytes_read; i++) {
if (offset+i >= data->start_offset) {
dest[i] = dest[i] ^ 0x5A;
for (i = 0; i < bytes; i++) {
if (offset + i >= data->start) {
dest[i] ^= 0x5A;
}
}
return bytes_read;
return bytes;
}
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);
/* decrypts JSTM stream */
static STREAMFILE* setup_jstm_streamfile(STREAMFILE *sf, off_t start) {
STREAMFILE *new_sf = NULL;
jstm_io_data io_data = {0};
/* setup decryption */
io_data.start_offset = start_offset;
io_data.start = start;
/* 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;
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

@ -17,16 +17,14 @@ typedef struct {
} 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 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;
bytes_read = streamfile->read(streamfile, dest, offset, length);
int i;
size_t bytes = read_streamfile(dest, offset, length, sf);
if (data->cfg.is_encrypted) {
for (i = 0; i < bytes_read; i++) {
for (i = 0; i < bytes; i++) {
if (data->cfg.is_header_swap && (offset + i) < header_size) {
dest[i] = header_swap[(offset + i) % header_size];
}
@ -41,36 +39,21 @@ static size_t ogg_vorbis_io_read(STREAMFILE *streamfile, uint8_t *dest, off_t of
}
}
return bytes_read;
return bytes;
}
//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;
/* 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};
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;
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,22 +1,21 @@
#ifndef _RIFF_OGG_STREAMFILE_H_
#define _RIFF_OGG_STREAMFILE_H_
#include "../streamfile.h"
#include "deblock_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);
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_read) {
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_read;
return bytes;
}
static size_t ogg_get_page(uint8_t *buf, size_t bufsize, off_t offset, STREAMFILE *sf) {
@ -43,7 +42,7 @@ fail:
return 0;
}
/* patches Oggs with weirdness */
/* 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;
@ -125,6 +124,4 @@ static STREAMFILE* setup_riff_ogg_streamfile(STREAMFILE *sf, off_t start, size_t
}
}
#endif /* VGM_USE_VORBIS */
#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

@ -6,17 +6,15 @@ 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;
static size_t vsv_io_read(STREAMFILE *sf, uint8_t *dest, off_t offset, size_t length, vsv_io_data *data) {
int i;
bytes_read = streamfile->read(streamfile, dest, offset, length);
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_read)
max = bytes_read;
if (max > bytes)
max = bytes;
for (i = 0; i < max; i++) {
dest[i] = 0;
@ -24,30 +22,19 @@ static size_t vsv_io_read(STREAMFILE *streamfile, uint8_t *dest, off_t offset, s
}
/* VSV also has last 0x800 block with a PS-ADPCM flag of 0x10 (incorrect), but it's ignored by the decoder */
return bytes_read;
return bytes;
}
static STREAMFILE* setup_vsv_streamfile(STREAMFILE *streamFile, off_t start_offset, size_t data_size) {
STREAMFILE *temp_streamFile = NULL, *new_streamFile = NULL;
/* cleans VSV data */
static STREAMFILE* setup_vsv_streamfile(STREAMFILE *sf) {
STREAMFILE *new_sf = 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;
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_ */