mirror of
https://github.com/vgmstream/vgmstream.git
synced 2024-11-24 15:00:11 +01:00
cleanup
This commit is contained in:
parent
edaac2c289
commit
b947f9b9e1
@ -1,65 +1,65 @@
|
||||
#include "meta.h"
|
||||
#include "../coding/coding.h"
|
||||
|
||||
|
||||
/* .PCM - from Lunar: Eternal Blue (Sega CD) */
|
||||
VGMSTREAM * init_vgmstream_scd_pcm(STREAMFILE *streamFile) {
|
||||
VGMSTREAM * vgmstream = NULL;
|
||||
off_t start_offset;
|
||||
int loop_flag, channel_count;
|
||||
|
||||
|
||||
/* checks */
|
||||
if (!check_extensions(streamFile, "pcm"))
|
||||
goto fail;
|
||||
|
||||
if (read_16bitBE(0x00,streamFile) == 0x0002) {
|
||||
channel_count = 1;
|
||||
}
|
||||
else if (read_16bitBE(0x00,streamFile) == 0x0001) {
|
||||
channel_count = 2; /* RP025.PCM, RP039.PCM */
|
||||
}
|
||||
else {
|
||||
goto fail;
|
||||
}
|
||||
|
||||
start_offset = 0x800;
|
||||
|
||||
|
||||
/* extra validations since .pcm it's kinda generic */
|
||||
{
|
||||
off_t i;
|
||||
/* should be empty up to start (~0x0a/~0x10 sometimes has unknown values) */
|
||||
for (i = 0x20; i < start_offset; i++) {
|
||||
if (read_8bit(i, streamFile) != 0)
|
||||
goto fail;
|
||||
}
|
||||
}
|
||||
|
||||
/* loops start 0 is possible, plus all? files loops
|
||||
* (even sfx/voices loop, but those set loop start in silence near the end) */
|
||||
loop_flag = (read_32bitBE(0x06,streamFile) != 0);
|
||||
|
||||
|
||||
/* build the VGMSTREAM */
|
||||
vgmstream = allocate_vgmstream(channel_count,loop_flag);
|
||||
if (!vgmstream) goto fail;
|
||||
|
||||
vgmstream->meta_type = meta_SCD_PCM;
|
||||
vgmstream->sample_rate = 32500; /* looks correct compared to emu/recordings */
|
||||
vgmstream->num_samples = pcm_bytes_to_samples(get_streamfile_size(streamFile) - start_offset, channel_count, 8);
|
||||
vgmstream->loop_start_sample = read_32bitBE(0x02,streamFile)*0x400*2;
|
||||
vgmstream->loop_end_sample = read_32bitBE(0x06,streamFile)*2;
|
||||
|
||||
vgmstream->coding_type = coding_PCM8_SB;
|
||||
vgmstream->layout_type = layout_interleave;
|
||||
vgmstream->interleave_block_size = 0x800;
|
||||
|
||||
if (!vgmstream_open_stream(vgmstream,streamFile,start_offset))
|
||||
goto fail;
|
||||
return vgmstream;
|
||||
|
||||
fail:
|
||||
close_vgmstream(vgmstream);
|
||||
return NULL;
|
||||
}
|
||||
#include "meta.h"
|
||||
#include "../coding/coding.h"
|
||||
|
||||
|
||||
/* .PCM - from Lunar: Eternal Blue (Sega CD) */
|
||||
VGMSTREAM* init_vgmstream_scd_pcm(STREAMFILE* sf) {
|
||||
VGMSTREAM* vgmstream = NULL;
|
||||
off_t start_offset;
|
||||
int loop_flag, channels;
|
||||
|
||||
|
||||
/* checks */
|
||||
if (!check_extensions(sf, "pcm"))
|
||||
goto fail;
|
||||
|
||||
if (read_u16be(0x00,sf) == 0x0002) {
|
||||
channels = 1;
|
||||
}
|
||||
else if (read_u16be(0x00,sf) == 0x0001) {
|
||||
channels = 2; /* RP025.PCM, RP039.PCM */
|
||||
}
|
||||
else {
|
||||
goto fail;
|
||||
}
|
||||
|
||||
start_offset = 0x800;
|
||||
|
||||
|
||||
/* extra validations since .pcm it's kinda generic */
|
||||
{
|
||||
off_t i;
|
||||
/* should be empty up to start (~0x0a/~0x10 sometimes has unknown values) */
|
||||
for (i = 0x20; i < start_offset; i++) {
|
||||
if (read_u8(i, sf) != 0)
|
||||
goto fail;
|
||||
}
|
||||
}
|
||||
|
||||
/* loops start 0 is possible, plus all? files loops
|
||||
* (even sfx/voices loop, but those set loop start in silence near the end) */
|
||||
loop_flag = (read_u32be(0x06,sf) != 0);
|
||||
|
||||
|
||||
/* build the VGMSTREAM */
|
||||
vgmstream = allocate_vgmstream(channels,loop_flag);
|
||||
if (!vgmstream) goto fail;
|
||||
|
||||
vgmstream->meta_type = meta_SCD_PCM;
|
||||
vgmstream->sample_rate = 32500; /* looks correct compared to emu/recordings */
|
||||
vgmstream->num_samples = pcm_bytes_to_samples(get_streamfile_size(sf) - start_offset, channels, 8);
|
||||
vgmstream->loop_start_sample = read_s32be(0x02,sf)*0x400*2;
|
||||
vgmstream->loop_end_sample = read_s32be(0x06,sf)*2;
|
||||
|
||||
vgmstream->coding_type = coding_PCM8_SB;
|
||||
vgmstream->layout_type = layout_interleave;
|
||||
vgmstream->interleave_block_size = 0x800;
|
||||
|
||||
if (!vgmstream_open_stream(vgmstream,sf,start_offset))
|
||||
goto fail;
|
||||
return vgmstream;
|
||||
|
||||
fail:
|
||||
close_vgmstream(vgmstream);
|
||||
return NULL;
|
||||
}
|
||||
|
@ -1,174 +1,173 @@
|
||||
#include "meta.h"
|
||||
#include "../coding/coding.h"
|
||||
|
||||
/* SSCF - Square-Enix games, older version of .scd [Crisis Core -Final Fantasy VII- (PSP), Dissidia 012 (PSP)] */
|
||||
VGMSTREAM * init_vgmstream_scd_sscf(STREAMFILE *streamFile) {
|
||||
VGMSTREAM * vgmstream = NULL;
|
||||
off_t start_offset, meta_offset, stream_offset;
|
||||
size_t stream_size;
|
||||
int loop_flag, channel_count, sample_rate;
|
||||
int total_subsongs, target_subsong = streamFile->stream_index;
|
||||
|
||||
|
||||
/* checks */
|
||||
if (!check_extensions(streamFile, "scd"))
|
||||
goto fail;
|
||||
|
||||
if (read_32bitBE(0x00,streamFile) != 0x53534346) /* "SSCF" "*/
|
||||
goto fail;
|
||||
if (!(read_32bitBE(0x04,streamFile) == 0x002070210 || /* version? [Crisis Core (PSP)] */
|
||||
read_32bitBE(0x04,streamFile) == 0x10020702)) /* inverted version? [Dissidia (PSP)] */
|
||||
goto fail;
|
||||
/* 0x08: file size, except for a few files that with a weird value */
|
||||
/* 0x10: file id? */
|
||||
|
||||
|
||||
/* find total subsongs (entries can be dummy) and target subsong */
|
||||
{
|
||||
int i,j, is_dupe;
|
||||
int entries = read_32bitLE(0x0c,streamFile);
|
||||
off_t stream_offsets[0x800];
|
||||
|
||||
if (entries > 0x800) /* meh */
|
||||
goto fail;
|
||||
|
||||
|
||||
if (target_subsong == 0) target_subsong = 1;
|
||||
|
||||
meta_offset = 0;
|
||||
total_subsongs = 0;
|
||||
for (i = 0; i < entries; i++) {
|
||||
off_t entry_offset = 0x20 + (0x20*i);
|
||||
off_t entry_stream_offset;
|
||||
|
||||
/* skip dummies */
|
||||
if (read_32bitLE(entry_offset+0x08,streamFile) == 0) /* size 0 */
|
||||
continue;
|
||||
if (read_16bitLE(entry_offset+0x0c,streamFile) == 0) /* no sample rate */
|
||||
continue;
|
||||
|
||||
/* skip repeated sounds */
|
||||
is_dupe = 0;
|
||||
entry_stream_offset = read_32bitLE(entry_offset+0x04,streamFile);
|
||||
for (j = 0; j < total_subsongs; j++) {
|
||||
if (entry_stream_offset == stream_offsets[j]) {
|
||||
is_dupe = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (is_dupe)
|
||||
continue;
|
||||
stream_offsets[total_subsongs] = entry_stream_offset;
|
||||
|
||||
/* ok */
|
||||
total_subsongs++;
|
||||
if (total_subsongs == target_subsong) {
|
||||
meta_offset = entry_offset;
|
||||
}
|
||||
}
|
||||
|
||||
if (meta_offset == 0)
|
||||
goto fail;
|
||||
}
|
||||
|
||||
|
||||
/* 0x00(2): config? usually 0x00/0x01 */
|
||||
/* 0x02(2): loop config */ //todo when 1 uses PS-ADPCM uses SPU loop flags to set start/end
|
||||
stream_offset = read_32bitLE(meta_offset+0x04,streamFile); /* absolute */
|
||||
stream_size = read_32bitLE(meta_offset+0x08,streamFile);
|
||||
sample_rate = (uint16_t)read_16bitLE(meta_offset+0x0c,streamFile);
|
||||
/* 0x0e: config? */
|
||||
/* 0x10: config? */
|
||||
/* 0x14: 0xCA5F or 0x5FCA */
|
||||
/* 0x18: config? */
|
||||
/* 0x1c: null / some id? */
|
||||
|
||||
loop_flag = 0;
|
||||
channel_count = 1;
|
||||
start_offset = stream_offset;
|
||||
|
||||
|
||||
/* build the VGMSTREAM */
|
||||
vgmstream = allocate_vgmstream(channel_count, loop_flag);
|
||||
if (!vgmstream) goto fail;
|
||||
|
||||
vgmstream->sample_rate = sample_rate;
|
||||
vgmstream->num_streams = total_subsongs;
|
||||
vgmstream->stream_size = stream_size;
|
||||
vgmstream->num_samples = ps_bytes_to_samples(stream_size, channel_count);
|
||||
if (loop_flag) {
|
||||
vgmstream->loop_start_sample = 0;
|
||||
vgmstream->loop_end_sample = vgmstream->num_samples;
|
||||
}
|
||||
|
||||
vgmstream->meta_type = meta_SCD_SSCF;
|
||||
vgmstream->coding_type = coding_PSX;
|
||||
vgmstream->layout_type = layout_none;
|
||||
|
||||
if (!vgmstream_open_stream(vgmstream,streamFile,start_offset))
|
||||
goto fail;
|
||||
return vgmstream;
|
||||
|
||||
fail:
|
||||
close_vgmstream(vgmstream);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#if 0
|
||||
/* SSCF X360 - updated SCD with encrypted data [Final Fantasy XI (360), PlayOnline Viewer (X360)] */
|
||||
VGMSTREAM * init_vgmstream_scd_sscf_x360(STREAMFILE *streamFile) {
|
||||
VGMSTREAM * vgmstream = NULL;
|
||||
off_t start_offset, meta_offset, stream_offset;
|
||||
size_t stream_size;
|
||||
int loop_flag, channel_count, sample_rate;
|
||||
int total_subsongs, target_subsong = streamFile->stream_index;
|
||||
|
||||
|
||||
/* checks */
|
||||
if (!check_extensions(streamFile, "scd"))
|
||||
goto fail;
|
||||
|
||||
if (read_32bitBE(0x00,streamFile) != 0x53534346) /* "SSCF" "*/
|
||||
goto fail;
|
||||
if (read_32bitBE(0x04,streamFile) != 0x20050300)/* version? */
|
||||
goto fail;
|
||||
/* 0x08: file size, except for a few files that with a weird value */
|
||||
/* 0x0c: null */
|
||||
/* 0x10: file id? */
|
||||
/* 0x14: encryption key (different files with the same value encrypt the same) */
|
||||
|
||||
/* 0x1c: entry count */
|
||||
|
||||
/* ~0x20: entries start? */
|
||||
/* 0x40: num samples? */
|
||||
/* 0x44: loop start? */
|
||||
/* 0x50: channels */
|
||||
/* 0x54: sample rate */
|
||||
|
||||
/* 0x80: encrypted RIFF data */
|
||||
|
||||
|
||||
loop_flag = 0;
|
||||
channel_count = 1;
|
||||
|
||||
|
||||
/* build the VGMSTREAM */
|
||||
vgmstream = allocate_vgmstream(channel_count, loop_flag);
|
||||
if (!vgmstream) goto fail;
|
||||
|
||||
vgmstream->sample_rate = sample_rate;
|
||||
vgmstream->num_samples = ...;
|
||||
|
||||
vgmstream->meta_type = meta_SCD_SSCF;
|
||||
vgmstream->coding_type = coding_...;
|
||||
vgmstream->layout_type = layout_none;
|
||||
|
||||
if (!vgmstream_open_stream(vgmstream,streamFile,start_offset))
|
||||
goto fail;
|
||||
return vgmstream;
|
||||
|
||||
fail:
|
||||
close_vgmstream(vgmstream);
|
||||
return NULL;
|
||||
}
|
||||
#endif
|
||||
#include "meta.h"
|
||||
#include "../coding/coding.h"
|
||||
|
||||
/* SSCF - Square-Enix games, older version of .scd [Crisis Core -Final Fantasy VII- (PSP), Dissidia 012 (PSP)] */
|
||||
VGMSTREAM* init_vgmstream_scd_sscf(STREAMFILE* sf) {
|
||||
VGMSTREAM* vgmstream = NULL;
|
||||
uint32_t start_offset, meta_offset, stream_offset, stream_size;
|
||||
int loop_flag, channels, sample_rate;
|
||||
int total_subsongs, target_subsong = sf->stream_index;
|
||||
|
||||
|
||||
/* checks */
|
||||
if (!is_id32be(0x00,sf, "SSCF"))
|
||||
goto fail;
|
||||
if (!check_extensions(sf, "scd"))
|
||||
goto fail;
|
||||
|
||||
if (!(read_u32be(0x04,sf) == 0x02070210 || /* version? [Crisis Core (PSP)] */
|
||||
read_u32be(0x04,sf) == 0x10020702)) /* inverted version? [Dissidia (PSP)] */
|
||||
goto fail;
|
||||
/* 0x08: file size, except for a few files that with a weird value */
|
||||
/* 0x10: file id? */
|
||||
|
||||
|
||||
/* find total subsongs (entries can be dummy) and target subsong */
|
||||
{
|
||||
int i,j, is_dupe;
|
||||
int entries = read_s32le(0x0c,sf);
|
||||
uint32_t stream_offsets[0x800];
|
||||
|
||||
if (entries > 0x800) /* meh */
|
||||
goto fail;
|
||||
|
||||
|
||||
if (target_subsong == 0) target_subsong = 1;
|
||||
|
||||
meta_offset = 0;
|
||||
total_subsongs = 0;
|
||||
for (i = 0; i < entries; i++) {
|
||||
uint32_t entry_offset = 0x20 + (0x20*i);
|
||||
uint32_t entry_stream_offset;
|
||||
|
||||
/* skip dummies */
|
||||
if (read_u32le(entry_offset+0x08,sf) == 0) /* size 0 */
|
||||
continue;
|
||||
if (read_u16le(entry_offset+0x0c,sf) == 0) /* no sample rate */
|
||||
continue;
|
||||
|
||||
/* skip repeated sounds */
|
||||
is_dupe = 0;
|
||||
entry_stream_offset = read_u32le(entry_offset+0x04,sf);
|
||||
for (j = 0; j < total_subsongs; j++) {
|
||||
if (entry_stream_offset == stream_offsets[j]) {
|
||||
is_dupe = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (is_dupe)
|
||||
continue;
|
||||
stream_offsets[total_subsongs] = entry_stream_offset;
|
||||
|
||||
/* ok */
|
||||
total_subsongs++;
|
||||
if (total_subsongs == target_subsong) {
|
||||
meta_offset = entry_offset;
|
||||
}
|
||||
}
|
||||
|
||||
if (meta_offset == 0)
|
||||
goto fail;
|
||||
}
|
||||
|
||||
|
||||
/* 0x00(2): config? usually 0x00/0x01 */
|
||||
/* 0x02(2): loop config */ //todo when 1 uses PS-ADPCM uses SPU loop flags to set start/end
|
||||
stream_offset = read_u32le(meta_offset+0x04,sf); /* absolute */
|
||||
stream_size = read_u32le(meta_offset+0x08,sf);
|
||||
sample_rate = read_u16le(meta_offset+0x0c,sf);
|
||||
/* 0x0e: config? */
|
||||
/* 0x10: config? */
|
||||
/* 0x14: 0xCA5F or 0x5FCA */
|
||||
/* 0x18: config? */
|
||||
/* 0x1c: null / some id? */
|
||||
|
||||
loop_flag = 0;
|
||||
channels = 1;
|
||||
start_offset = stream_offset;
|
||||
|
||||
|
||||
/* build the VGMSTREAM */
|
||||
vgmstream = allocate_vgmstream(channels, loop_flag);
|
||||
if (!vgmstream) goto fail;
|
||||
|
||||
vgmstream->sample_rate = sample_rate;
|
||||
vgmstream->num_streams = total_subsongs;
|
||||
vgmstream->stream_size = stream_size;
|
||||
vgmstream->num_samples = ps_bytes_to_samples(stream_size, channels);
|
||||
if (loop_flag) {
|
||||
vgmstream->loop_start_sample = 0;
|
||||
vgmstream->loop_end_sample = vgmstream->num_samples;
|
||||
}
|
||||
|
||||
vgmstream->meta_type = meta_SCD_SSCF;
|
||||
vgmstream->coding_type = coding_PSX;
|
||||
vgmstream->layout_type = layout_none;
|
||||
|
||||
if (!vgmstream_open_stream(vgmstream,sf,start_offset))
|
||||
goto fail;
|
||||
return vgmstream;
|
||||
|
||||
fail:
|
||||
close_vgmstream(vgmstream);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#if 0
|
||||
/* SSCF X360 - updated SCD with encrypted data [Final Fantasy XI (360), PlayOnline Viewer (X360)] */
|
||||
VGMSTREAM * init_vgmstream_scd_sscf_x360(STREAMFILE *streamFile) {
|
||||
VGMSTREAM * vgmstream = NULL;
|
||||
off_t start_offset, meta_offset, stream_offset;
|
||||
size_t stream_size;
|
||||
int loop_flag, channel_count, sample_rate;
|
||||
int total_subsongs, target_subsong = streamFile->stream_index;
|
||||
|
||||
|
||||
/* checks */
|
||||
if (!check_extensions(streamFile, "scd"))
|
||||
goto fail;
|
||||
|
||||
if (read_32bitBE(0x00,streamFile) != 0x53534346) /* "SSCF" "*/
|
||||
goto fail;
|
||||
if (read_32bitBE(0x04,streamFile) != 0x20050300)/* version? */
|
||||
goto fail;
|
||||
/* 0x08: file size, except for a few files that with a weird value */
|
||||
/* 0x0c: null */
|
||||
/* 0x10: file id? */
|
||||
/* 0x14: encryption key (different files with the same value encrypt the same) */
|
||||
|
||||
/* 0x1c: entry count */
|
||||
|
||||
/* ~0x20: entries start? */
|
||||
/* 0x40: num samples? */
|
||||
/* 0x44: loop start? */
|
||||
/* 0x50: channels */
|
||||
/* 0x54: sample rate */
|
||||
|
||||
/* 0x80: encrypted RIFF data */
|
||||
|
||||
|
||||
loop_flag = 0;
|
||||
channel_count = 1;
|
||||
|
||||
|
||||
/* build the VGMSTREAM */
|
||||
vgmstream = allocate_vgmstream(channel_count, loop_flag);
|
||||
if (!vgmstream) goto fail;
|
||||
|
||||
vgmstream->sample_rate = sample_rate;
|
||||
vgmstream->num_samples = ...;
|
||||
|
||||
vgmstream->meta_type = meta_SCD_SSCF;
|
||||
vgmstream->coding_type = coding_...;
|
||||
vgmstream->layout_type = layout_none;
|
||||
|
||||
if (!vgmstream_open_stream(vgmstream,streamFile,start_offset))
|
||||
goto fail;
|
||||
return vgmstream;
|
||||
|
||||
fail:
|
||||
close_vgmstream(vgmstream);
|
||||
return NULL;
|
||||
}
|
||||
#endif
|
||||
|
@ -1,187 +1,187 @@
|
||||
#include <string.h>
|
||||
#include "text_reader.h"
|
||||
#include "log.h"
|
||||
|
||||
|
||||
/* convenience function to init the above struct */
|
||||
int text_reader_init(text_reader_t* tr, uint8_t* buf, int buf_size, STREAMFILE* sf, uint32_t offset, uint32_t max_offset) {
|
||||
memset(tr, 0, sizeof(text_reader_t));
|
||||
|
||||
if (buf_size <= 1 || !buf || !sf)
|
||||
return 0;
|
||||
|
||||
tr->buf = buf;
|
||||
tr->buf_size = buf_size;
|
||||
tr->sf = sf;
|
||||
tr->offset = offset;
|
||||
|
||||
if (!max_offset)
|
||||
max_offset = get_streamfile_size(sf);
|
||||
tr->max_offset = max_offset;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/* reads more data into buf and adjust values */
|
||||
static void prepare_buf(text_reader_t* tr) {
|
||||
|
||||
/* since we may read N lines in the same buffer, move starting pos each call */
|
||||
tr->pos = tr->next_pos;
|
||||
|
||||
/* not more data (but may still read lines so not an error) */
|
||||
if (tr->offset >= tr->max_offset) {
|
||||
return;
|
||||
}
|
||||
|
||||
/* request more data */
|
||||
if (tr->pos >= tr->filled) {
|
||||
tr->pos = 0;
|
||||
tr->filled = 0;
|
||||
}
|
||||
|
||||
/* partially filled, move buffer */
|
||||
if (tr->pos > 0) {
|
||||
int move_size = tr->filled - tr->pos;
|
||||
|
||||
memmove(tr->buf, &tr->buf[tr->pos], move_size); /* memmove = may overlap */
|
||||
tr->filled -= tr->pos; /* now less filled */
|
||||
tr->pos = 0;
|
||||
}
|
||||
|
||||
/* has enough data */
|
||||
if (tr->filled >= tr->buf_size) {
|
||||
return;
|
||||
}
|
||||
|
||||
/* read buf up to max */
|
||||
{
|
||||
int bytes;
|
||||
int read_size = tr->buf_size - tr->filled;
|
||||
if (read_size + tr->offset > tr->max_offset)
|
||||
read_size = tr->max_offset - tr->offset;
|
||||
|
||||
if (read_size <= 0) { /* ??? */
|
||||
bytes = 0;
|
||||
}
|
||||
else {
|
||||
if (tr->filled + read_size >= tr->buf_size)
|
||||
read_size -= 1; /* always leave an extra byte for c-string null */
|
||||
|
||||
bytes = read_streamfile(tr->buf + tr->filled, tr->offset, read_size, tr->sf);
|
||||
tr->offset += bytes;
|
||||
tr->filled += bytes;
|
||||
}
|
||||
|
||||
/* maybe some internal issue, force EOF */
|
||||
if (bytes == 0) {
|
||||
tr->offset = tr->max_offset;
|
||||
}
|
||||
|
||||
/* ensure no old data is used as valid (simplifies some checks during parse) */
|
||||
tr->buf[tr->filled] = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
static void parse_buf(text_reader_t* tr) {
|
||||
int i;
|
||||
|
||||
tr->line = (char*)&tr->buf[tr->pos];
|
||||
tr->line_len = 0;
|
||||
tr->line_ok = 0;
|
||||
|
||||
/* detect EOF (this should only happen if no more data was loaded) */
|
||||
if (tr->pos == tr->filled) {
|
||||
tr->line = NULL;
|
||||
tr->line_ok = 1;
|
||||
tr->line_len = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
/* assumes filled doesn't reach buf_size (to allow trailing \0 after filled) */
|
||||
for (i = tr->pos; i < tr->filled; i++) {
|
||||
char c = (char)tr->buf[i];
|
||||
|
||||
if (c == '\0') {
|
||||
i++;
|
||||
break; /* not a valid file? (line_ok=0) */
|
||||
}
|
||||
|
||||
if (c == '\r' && tr->buf[i+1] == '\n') { /* CRLF (0x0d0a) */
|
||||
/* i+1 may read past filled but it's pre-set to \0 */
|
||||
i += 2; //todo check that i < buf_size-1
|
||||
tr->line_ok = 1;
|
||||
break;
|
||||
}
|
||||
else if (c == '\n') { /* LF (0x0a) */
|
||||
i++;
|
||||
tr->line_ok = 1;
|
||||
break;
|
||||
}
|
||||
else if (c == '\r') { /* CR (0x0d) */
|
||||
i++;
|
||||
tr->line_ok = (i < tr->buf_size - 1);
|
||||
/* if buf ends with a CR, next buf may start be a LF (single CRLF), so line is not ok near buf end
|
||||
* (old Macs use single \r as lines, but using only that and reaching buf end should happen rarely) */
|
||||
break;
|
||||
}
|
||||
|
||||
tr->line_len++;
|
||||
}
|
||||
|
||||
/* when lines are small may read up to filled smaller than buf, with no more data */
|
||||
if (!tr->line_ok && i == tr->filled)
|
||||
tr->line_ok = (tr->filled < tr->buf_size - 1);
|
||||
|
||||
/* added after proper line (a \n) or after buf end, so we aren't changing valid data */
|
||||
tr->buf[tr->pos + tr->line_len] = '\0';
|
||||
tr->next_pos = i;
|
||||
}
|
||||
|
||||
int text_reader_get_line(text_reader_t* tr, char** p_line) {
|
||||
|
||||
if (!tr->buf) /* no init */
|
||||
return 0;
|
||||
|
||||
/* how it works:
|
||||
* - fills buffer up to max or buf_len, from pos 0
|
||||
* - counts from 0 to next '\n' or EOF
|
||||
* - nulls \n or after EOF to make a proper c-string
|
||||
* - returns from string from pos 0 to len
|
||||
* - on next call rather than re-reading continues from pos N (after \n)
|
||||
* - a buf will likely contain multiple lines
|
||||
* - if read chars reach buf_end (no proper line found):
|
||||
* - pos = 0: buf isn't big enough, error
|
||||
* - pos > 0: move data to pos=0, fill rest of buf, fill rest of buf
|
||||
*
|
||||
* ex.
|
||||
* - parse buf: read chunk full [aaaaa\nbbbb] (pos = 0)
|
||||
* - get line: returns "aaaaa\0" (next_pos points to first 'b')
|
||||
* - get line: from 'b', but reaches buf end before \n or EOF: must readjust
|
||||
* - parse buf: move chunk part [bbbb*******] ('b' to beginning, * is garbage)
|
||||
* - parse buf: read chunk part [bbbbbb\ncc_] (reaches EOF)
|
||||
* - get line: returns "bbbbbb\0" (pos points to first c)
|
||||
* - get line: returns "cc\0"
|
||||
* - get line: returns NULL (reached EOF, no more bytes)
|
||||
* - (there is an implicit \0 reserved in buf)
|
||||
*
|
||||
* ex.
|
||||
* - start: read chunk [aaaaaaaaaaa]
|
||||
* - get line: reaches buf end, but didn't reach EOF nor \n: error, can't store line
|
||||
*/
|
||||
|
||||
prepare_buf(tr); /* may not do anything */
|
||||
parse_buf(tr); /* next line */
|
||||
|
||||
/* if we are reading a partial line there may be more data */
|
||||
if (!tr->line_ok && tr->pos > 0) {
|
||||
prepare_buf(tr);
|
||||
parse_buf(tr); /* could continue from prev parse but makes logic more complex for little gain */
|
||||
}
|
||||
|
||||
/* always output line even if truncated */
|
||||
if (p_line) *p_line = tr->line;
|
||||
return !tr->line_ok ?
|
||||
-(tr->line_len + 1) : /* -0 also is possible, force -1 */
|
||||
tr->line_len;
|
||||
}
|
||||
#include <string.h>
|
||||
#include "text_reader.h"
|
||||
#include "log.h"
|
||||
|
||||
|
||||
/* convenience function to init the above struct */
|
||||
int text_reader_init(text_reader_t* tr, uint8_t* buf, int buf_size, STREAMFILE* sf, uint32_t offset, uint32_t max_offset) {
|
||||
memset(tr, 0, sizeof(text_reader_t));
|
||||
|
||||
if (buf_size <= 1 || !buf || !sf)
|
||||
return 0;
|
||||
|
||||
tr->buf = buf;
|
||||
tr->buf_size = buf_size;
|
||||
tr->sf = sf;
|
||||
tr->offset = offset;
|
||||
|
||||
if (!max_offset)
|
||||
max_offset = get_streamfile_size(sf);
|
||||
tr->max_offset = max_offset;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/* reads more data into buf and adjust values */
|
||||
static void prepare_buf(text_reader_t* tr) {
|
||||
|
||||
/* since we may read N lines in the same buffer, move starting pos each call */
|
||||
tr->pos = tr->next_pos;
|
||||
|
||||
/* not more data (but may still read lines so not an error) */
|
||||
if (tr->offset >= tr->max_offset) {
|
||||
return;
|
||||
}
|
||||
|
||||
/* request more data */
|
||||
if (tr->pos >= tr->filled) {
|
||||
tr->pos = 0;
|
||||
tr->filled = 0;
|
||||
}
|
||||
|
||||
/* partially filled, move buffer */
|
||||
if (tr->pos > 0) {
|
||||
int move_size = tr->filled - tr->pos;
|
||||
|
||||
memmove(tr->buf, &tr->buf[tr->pos], move_size); /* memmove = may overlap */
|
||||
tr->filled -= tr->pos; /* now less filled */
|
||||
tr->pos = 0;
|
||||
}
|
||||
|
||||
/* has enough data */
|
||||
if (tr->filled >= tr->buf_size) {
|
||||
return;
|
||||
}
|
||||
|
||||
/* read buf up to max */
|
||||
{
|
||||
int bytes;
|
||||
int read_size = tr->buf_size - tr->filled;
|
||||
if (read_size + tr->offset > tr->max_offset)
|
||||
read_size = tr->max_offset - tr->offset;
|
||||
|
||||
if (read_size <= 0) { /* ??? */
|
||||
bytes = 0;
|
||||
}
|
||||
else {
|
||||
if (tr->filled + read_size >= tr->buf_size)
|
||||
read_size -= 1; /* always leave an extra byte for c-string null */
|
||||
|
||||
bytes = read_streamfile(tr->buf + tr->filled, tr->offset, read_size, tr->sf);
|
||||
tr->offset += bytes;
|
||||
tr->filled += bytes;
|
||||
}
|
||||
|
||||
/* maybe some internal issue, force EOF */
|
||||
if (bytes == 0) {
|
||||
tr->offset = tr->max_offset;
|
||||
}
|
||||
|
||||
/* ensure no old data is used as valid (simplifies some checks during parse) */
|
||||
tr->buf[tr->filled] = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
static void parse_buf(text_reader_t* tr) {
|
||||
int i;
|
||||
|
||||
tr->line = (char*)&tr->buf[tr->pos];
|
||||
tr->line_len = 0;
|
||||
tr->line_ok = 0;
|
||||
|
||||
/* detect EOF (this should only happen if no more data was loaded) */
|
||||
if (tr->pos == tr->filled) {
|
||||
tr->line = NULL;
|
||||
tr->line_ok = 1;
|
||||
tr->line_len = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
/* assumes filled doesn't reach buf_size (to allow trailing \0 after filled) */
|
||||
for (i = tr->pos; i < tr->filled; i++) {
|
||||
char c = (char)tr->buf[i];
|
||||
|
||||
if (c == '\0') {
|
||||
i++;
|
||||
break; /* not a valid file? (line_ok=0) */
|
||||
}
|
||||
|
||||
if (c == '\r' && tr->buf[i+1] == '\n') { /* CRLF (0x0d0a) */
|
||||
/* i+1 may read past filled but it's pre-set to \0 */
|
||||
i += 2; //todo check that i < buf_size-1
|
||||
tr->line_ok = 1;
|
||||
break;
|
||||
}
|
||||
else if (c == '\n') { /* LF (0x0a) */
|
||||
i++;
|
||||
tr->line_ok = 1;
|
||||
break;
|
||||
}
|
||||
else if (c == '\r') { /* CR (0x0d) */
|
||||
i++;
|
||||
tr->line_ok = (i < tr->buf_size - 1);
|
||||
/* if buf ends with a CR, next buf may start be a LF (single CRLF), so line is not ok near buf end
|
||||
* (old Macs use single \r as lines, but using only that and reaching buf end should happen rarely) */
|
||||
break;
|
||||
}
|
||||
|
||||
tr->line_len++;
|
||||
}
|
||||
|
||||
/* when lines are small may read up to filled smaller than buf, with no more data */
|
||||
if (!tr->line_ok && i == tr->filled)
|
||||
tr->line_ok = (tr->filled < tr->buf_size - 1);
|
||||
|
||||
/* added after proper line (a \n) or after buf end, so we aren't changing valid data */
|
||||
tr->buf[tr->pos + tr->line_len] = '\0';
|
||||
tr->next_pos = i;
|
||||
}
|
||||
|
||||
int text_reader_get_line(text_reader_t* tr, char** p_line) {
|
||||
|
||||
if (!tr->buf) /* no init */
|
||||
return 0;
|
||||
|
||||
/* how it works:
|
||||
* - fills buffer up to max or buf_len, from pos 0
|
||||
* - counts from 0 to next '\n' or EOF
|
||||
* - nulls \n or after EOF to make a proper c-string
|
||||
* - returns from string from pos 0 to len
|
||||
* - on next call rather than re-reading continues from pos N (after \n)
|
||||
* - a buf will likely contain multiple lines
|
||||
* - if read chars reach buf_end (no proper line found):
|
||||
* - pos = 0: buf isn't big enough, error
|
||||
* - pos > 0: move data to pos=0, fill rest of buf, fill rest of buf
|
||||
*
|
||||
* ex.
|
||||
* - parse buf: read chunk full [aaaaa\nbbbb] (pos = 0)
|
||||
* - get line: returns "aaaaa\0" (next_pos points to first 'b')
|
||||
* - get line: from 'b', but reaches buf end before \n or EOF: must readjust
|
||||
* - parse buf: move chunk part [bbbb*******] ('b' to beginning, * is garbage)
|
||||
* - parse buf: read chunk part [bbbbbb\ncc_] (reaches EOF)
|
||||
* - get line: returns "bbbbbb\0" (pos points to first c)
|
||||
* - get line: returns "cc\0"
|
||||
* - get line: returns NULL (reached EOF, no more bytes)
|
||||
* - (there is an implicit \0 reserved in buf)
|
||||
*
|
||||
* ex.
|
||||
* - start: read chunk [aaaaaaaaaaa]
|
||||
* - get line: reaches buf end, but didn't reach EOF nor \n: error, can't store line
|
||||
*/
|
||||
|
||||
prepare_buf(tr); /* may not do anything */
|
||||
parse_buf(tr); /* next line */
|
||||
|
||||
/* if we are reading a partial line there may be more data */
|
||||
if (!tr->line_ok && tr->pos > 0) {
|
||||
prepare_buf(tr);
|
||||
parse_buf(tr); /* could continue from prev parse but makes logic more complex for little gain */
|
||||
}
|
||||
|
||||
/* always output line even if truncated */
|
||||
if (p_line) *p_line = tr->line;
|
||||
return !tr->line_ok ?
|
||||
-(tr->line_len + 1) : /* -0 also is possible, force -1 */
|
||||
tr->line_len;
|
||||
}
|
||||
|
@ -97,7 +97,6 @@ VGMSTREAM* (*init_vgmstream_functions[])(STREAMFILE* sf) = {
|
||||
init_vgmstream_ps2_kces,
|
||||
init_vgmstream_hxd,
|
||||
init_vgmstream_vsv,
|
||||
init_vgmstream_scd_pcm,
|
||||
init_vgmstream_ps2_pcm,
|
||||
init_vgmstream_ps2_rkv,
|
||||
init_vgmstream_ps2_vas,
|
||||
@ -528,6 +527,7 @@ VGMSTREAM* (*init_vgmstream_functions[])(STREAMFILE* sf) = {
|
||||
init_vgmstream_bigrp,
|
||||
|
||||
/* lower priority metas (no clean header identity, somewhat ambiguous, or need extension/companion file to identify) */
|
||||
init_vgmstream_scd_pcm,
|
||||
init_vgmstream_agsc,
|
||||
init_vgmstream_rsf,
|
||||
init_vgmstream_ps2_wmus,
|
||||
|
Loading…
Reference in New Issue
Block a user