mirror of
https://github.com/vgmstream/vgmstream.git
synced 2025-01-17 23:36:41 +01:00
Clean clHCA lib 3 (API)
- rename clHCA_DecodeSamples16 to clHCA_ReadSamples16 since it isn't really decoding - divide clHCA_Decode into clHCA_DecodeHeader and clHCA_DecodeBlock to clarify usage - unify clHCA_IsOurFile0/1 since one would need to call clHCA_DecodeHeader right after it anyway - add clHCA_SetKey for easier key handling - adapt vgmstream wrapper
This commit is contained in:
parent
a38e3a4878
commit
fd52fe0e95
@ -5,49 +5,38 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
enum { clHCA_samplesPerBlock = 0x80 * 8 };
|
||||
|
||||
/* Must pass at least 8 bytes of data to this function. Returns -1 on non-match, or
|
||||
* positive byte count on success. */
|
||||
int clHCA_isOurFile0(const void *data);
|
||||
|
||||
/* Must pass a full header block for success. Returns 0 on success, -1 on failure. */
|
||||
int clHCA_isOurFile1(const void *data, unsigned int size);
|
||||
/* Must pass at least 8 bytes of data to this function.
|
||||
* Returns <0 on non-match, or header size on success. */
|
||||
int clHCA_isOurFile(const void *data, unsigned int size);
|
||||
|
||||
/* The opaque state structure. */
|
||||
typedef struct clHCA clHCA;
|
||||
|
||||
/* In case you wish to allocate the structure on your own. */
|
||||
/* In case you wish to allocate and reset the structure on your own. */
|
||||
int clHCA_sizeof();
|
||||
void clHCA_clear(clHCA *, unsigned int ciphKey1, unsigned int ciphKey2);
|
||||
void clHCA_clear(clHCA *);
|
||||
void clHCA_done(clHCA *);
|
||||
|
||||
/* Or you could let the library allocate it. */
|
||||
clHCA * clHCA_new(unsigned int ciphKey1, unsigned int ciphKey2);
|
||||
clHCA * clHCA_new();
|
||||
void clHCA_delete(clHCA *);
|
||||
|
||||
/* Requires a pre-allocated data structure.
|
||||
* Before any decoding may be performed, the header block must be passed in.
|
||||
* The recommended way of doing this is to detect the header length with
|
||||
* clHCA_isOurFile0, validate the header with clHCA_isOurFile1, then pass
|
||||
* it to this function, with the address of 0.
|
||||
* Subsequent decodes with non-zero address are assumed to be sample blocks,
|
||||
* and should be of the blockSize returned by the clHCA_getInfo function.
|
||||
* Returns 0 on success, -1 on failure. */
|
||||
int clHCA_Decode(clHCA *, void *data, unsigned int size, unsigned int address);
|
||||
|
||||
/* This is the simplest decode function, for signed and clipped 16 bit samples.
|
||||
* May be called after clHCA_Decode, and will return the same data until the next
|
||||
* block of sample data is passed to clHCA_Decode. */
|
||||
void clHCA_DecodeSamples16(clHCA *, signed short * outSamples);
|
||||
/* Parses the HCA header. Must be called before any decoding may be performed,
|
||||
* and size must be at least headerSize long. The recommended way is to detect
|
||||
* the header length with clHCA_isOurFile, then read data and call this.
|
||||
* May be called multiple times to reset decoder state.
|
||||
* Returns 0 on success, <0 on failure. */
|
||||
int clHCA_DecodeHeader(clHCA *, void *data, unsigned int size);
|
||||
|
||||
typedef struct clHCA_stInfo {
|
||||
unsigned int version;
|
||||
unsigned int dataOffset;
|
||||
unsigned int headerSize;
|
||||
unsigned int samplingRate;
|
||||
unsigned int channelCount;
|
||||
unsigned int blockSize;
|
||||
unsigned int blockCount;
|
||||
|
||||
unsigned int loopEnabled;
|
||||
unsigned int loopStartBlock;
|
||||
unsigned int loopEndBlock;
|
||||
@ -55,16 +44,37 @@ typedef struct clHCA_stInfo {
|
||||
unsigned int loopEndPadding; /* samples in block after loop ends */
|
||||
unsigned int encoderDelay; /* samples appended to the beginning */
|
||||
unsigned int encoderPadding; /* samples appended to the end */
|
||||
unsigned int samplesPerBlock;
|
||||
unsigned int samplesPerBlock; /* should be 1024 */
|
||||
const char *comment;
|
||||
|
||||
/* Derived sample formulas:
|
||||
* - sample count: blockCount*samplesPerBlock - encoderDelay - encoderPadding;
|
||||
* - loop start sample = loopStartBlock*samplesPerBlock - encoderDelay + loopStartDelay
|
||||
* - loop end sample = loopEndBlock*samplesPerBlock - encoderDelay + (samplesPerBlock - info.loopEndPadding)
|
||||
*/
|
||||
} clHCA_stInfo;
|
||||
|
||||
/* Retrieve information relevant for decoding and playback with this function.
|
||||
* Must be called after successfully decoding a header block with clHCA_Decode,
|
||||
* or else it will fail.
|
||||
* Returns 0 on success, -1 on failure. */
|
||||
/* Retrieves header information for decoding and playback (it's the caller's responsability
|
||||
* to apply looping, encoder delay/skip samples, etc). May be called after clHCA_DecodeHeader.
|
||||
* Returns 0 on success, <0 on failure. */
|
||||
int clHCA_getInfo(clHCA *, clHCA_stInfo *out);
|
||||
|
||||
/* Sets a 64 bit encryption key, to properly decode blocks. This may be called
|
||||
* multiple times to change the key, before or after clHCA_DecodeHeader.
|
||||
* Key is ignored if the file is not encrypted. */
|
||||
void clHCA_SetKey(clHCA *, unsigned long long keycode);
|
||||
|
||||
/* Decodes a single frame, from data after headerSize. Should be called after
|
||||
* clHCA_DecodeHeader and size must be at least blockSize long.
|
||||
* Returns 0 on success, <0 on failure. */
|
||||
int clHCA_DecodeBlock(clHCA *, void *data, unsigned int size);
|
||||
|
||||
/* Extracts signed and clipped 16 bit samples into sample buffer.
|
||||
* May be called after clHCA_DecodeBlock, and will return the same data until
|
||||
* next decode. Buffer must be at least (samplesPerBlock*channels) long. */
|
||||
void clHCA_ReadSamples16(clHCA *, signed short * outSamples);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
110
ext_libs/clHCA.c
110
ext_libs/clHCA.c
@ -4,7 +4,7 @@
|
||||
* - Original decompilation and C++ decoder by nyaga
|
||||
* https://github.com/Nyagamon/HCADecoder
|
||||
* - Ported to C by kode54
|
||||
* https://gist.github.com/kode54/ce2bf799b445002e125f06ed833903c0
|
||||
* https://gist.github.com/kode54/ce2bf799b445002e125f06ed833903c0
|
||||
* - Cleaned up by bnnm using Thealexbarney's VGAudio decoder as reference
|
||||
* https://github.com/Thealexbarney/VGAudio
|
||||
*/
|
||||
@ -16,6 +16,7 @@
|
||||
* - check "read intensity" vs VGAudio (CriHcaPacking.ReadIntensity), skips intensities if first is 15
|
||||
* - simplify DCT4 code
|
||||
* - add extra validations: encoder_delay/padding < sample_count, bands/totals (max: 128?), track count==1, etc
|
||||
* - calling clHCA_clear multiple times will not deallocate "comment" correctly
|
||||
*/
|
||||
//--------------------------------------------------
|
||||
// Includes
|
||||
@ -230,37 +231,25 @@ static void bitreader_skip(clData *br, int bitsize) {
|
||||
}
|
||||
|
||||
//--------------------------------------------------
|
||||
// Decoder utilities
|
||||
// API/Utilities
|
||||
//--------------------------------------------------
|
||||
#if 0
|
||||
static int clHCA_CheckFile(void *data, unsigned int size) {
|
||||
return (data&&size>=4&&(get_be32(*(unsigned int *)data) & HCA_MASK) == 0x48434100);/*'HCA\0'*/
|
||||
}
|
||||
#endif
|
||||
|
||||
int clHCA_isOurFile0(const void *data) {
|
||||
int clHCA_isOurFile(const void *data, unsigned int size) {
|
||||
clData br;
|
||||
if (!data)
|
||||
unsigned int header_size = 0;
|
||||
|
||||
if (!data || size < 0x08)
|
||||
return -1;
|
||||
|
||||
bitreader_init(&br, data, 8);
|
||||
if ((bitreader_peek(&br, 32) & HCA_MASK) == 0x48434100) {/*'HCA\0'*/
|
||||
bitreader_skip(&br, 32 + 16);
|
||||
return bitreader_peek(&br, 16);
|
||||
header_size = bitreader_read(&br, 16);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int clHCA_isOurFile1(const void *data, unsigned int size) {
|
||||
int minsize;
|
||||
if (!data || size < 0x08)
|
||||
if (header_size == 0)
|
||||
return -1;
|
||||
minsize = clHCA_isOurFile0(data);
|
||||
if (minsize < 0 || (unsigned int) minsize > size)
|
||||
return -1;
|
||||
if (crc16_checksum(data, minsize))
|
||||
return -1;
|
||||
return 0;
|
||||
return header_size;
|
||||
}
|
||||
|
||||
int clHCA_getInfo(clHCA *hca, clHCA_stInfo *info) {
|
||||
@ -268,7 +257,7 @@ int clHCA_getInfo(clHCA *hca, clHCA_stInfo *info) {
|
||||
return -1;
|
||||
|
||||
info->version = hca->version;
|
||||
info->dataOffset = hca->header_size;
|
||||
info->headerSize = hca->header_size;
|
||||
info->samplingRate = hca->sample_rate;
|
||||
info->channelCount = hca->channels;
|
||||
info->blockSize = hca->frame_size;
|
||||
@ -285,7 +274,7 @@ int clHCA_getInfo(clHCA *hca, clHCA_stInfo *info) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
void clHCA_DecodeSamples16(clHCA *hca, signed short *samples) {
|
||||
void clHCA_ReadSamples16(clHCA *hca, signed short *samples) {
|
||||
const float scale = 32768.0f;
|
||||
float f;
|
||||
signed int s;
|
||||
@ -314,14 +303,17 @@ void clHCA_DecodeSamples16(clHCA *hca, signed short *samples) {
|
||||
//--------------------------------------------------
|
||||
// Allocation and creation
|
||||
//--------------------------------------------------
|
||||
static void clHCA_constructor(clHCA *hca, unsigned int keycode_upper, unsigned int keycode_lower) {
|
||||
static void clHCA_constructor(clHCA *hca) {
|
||||
if (!hca)
|
||||
return;
|
||||
memset(hca, 0, sizeof(*hca));
|
||||
hca->keycode = ((long long)(keycode_upper & 0xFFFFFFFF) << 32) | (keycode_lower & 0xFFFFFFFF);
|
||||
hca->is_valid = 0;
|
||||
hca->comment = 0;
|
||||
}
|
||||
|
||||
static void clHCA_destructor(clHCA *hca) {
|
||||
if (!hca)
|
||||
return;
|
||||
free(hca->comment);
|
||||
hca->comment = 0;
|
||||
}
|
||||
@ -330,30 +322,27 @@ int clHCA_sizeof() {
|
||||
return sizeof(clHCA);
|
||||
}
|
||||
|
||||
void clHCA_clear(clHCA *hca, unsigned int keycode_lower, unsigned int keycode_upper) {
|
||||
clHCA_constructor(hca, keycode_upper, keycode_lower);
|
||||
void clHCA_clear(clHCA *hca) {
|
||||
clHCA_constructor(hca);
|
||||
}
|
||||
|
||||
void clHCA_done(clHCA *hca) {
|
||||
clHCA_destructor(hca);
|
||||
}
|
||||
|
||||
clHCA * clHCA_new(unsigned int keycode_lower, unsigned int keycode_upper) {
|
||||
clHCA * clHCA_new() {
|
||||
clHCA *hca = (clHCA *) malloc(clHCA_sizeof());
|
||||
if (hca) {
|
||||
clHCA_constructor(hca, keycode_upper, keycode_lower);
|
||||
clHCA_constructor(hca);
|
||||
}
|
||||
return hca;
|
||||
}
|
||||
|
||||
void clHCA_delete(clHCA *hca) {
|
||||
if (hca) {
|
||||
clHCA_destructor(hca);
|
||||
free(hca);
|
||||
}
|
||||
clHCA_destructor(hca);
|
||||
free(hca);
|
||||
}
|
||||
|
||||
|
||||
//--------------------------------------------------
|
||||
// ATH
|
||||
//--------------------------------------------------
|
||||
@ -577,7 +566,7 @@ static unsigned int header_ceil2(unsigned int a, unsigned int b) {
|
||||
return (b > 0) ? (a / b + ((a % b) ? 1 : 0)) : 0;
|
||||
}
|
||||
|
||||
static int clHCA_DecodeHeader(clHCA *hca, void *data, unsigned int size) {
|
||||
int clHCA_DecodeHeader(clHCA *hca, void *data, unsigned int size) {
|
||||
clData br;
|
||||
|
||||
if (!hca || !data )
|
||||
@ -791,9 +780,9 @@ static int clHCA_DecodeHeader(clHCA *hca, void *data, unsigned int size) {
|
||||
size -= (size - 0x02); /* fills up to header_size, sans checksum */
|
||||
}
|
||||
|
||||
/* fully read */
|
||||
if (size != 0x02)
|
||||
return -1;
|
||||
/* should be fully read, but allow data buffer may be bigger than header_size */
|
||||
//if (size != 0x02)
|
||||
// return -1;
|
||||
|
||||
|
||||
/* extra validations */
|
||||
@ -803,18 +792,20 @@ static int clHCA_DecodeHeader(clHCA *hca, void *data, unsigned int size) {
|
||||
if (!(hca->min_resolution == 1 && hca->max_resolution == 15))
|
||||
return -1;
|
||||
|
||||
/* inits */
|
||||
|
||||
/* inits state */
|
||||
if (hca->track_count == 0)
|
||||
hca->track_count = 1; /* default to avoid division by zero */
|
||||
|
||||
hca->hfr_group_count = header_ceil2(
|
||||
hca->total_band_count - hca->base_band_count - hca->stereo_band_count,
|
||||
hca->bands_per_hfr_group);
|
||||
|
||||
if (ath_init(hca->ath_curve, hca->ath_type, hca->sample_rate) < 0)
|
||||
return -1;
|
||||
if (cipher_init(hca->cipher_table, hca->ciph_type, hca->keycode) < 0)
|
||||
return -1;
|
||||
|
||||
hca->hfr_group_count = header_ceil2(
|
||||
hca->total_band_count - hca->base_band_count - hca->stereo_band_count,
|
||||
hca->bands_per_hfr_group);
|
||||
|
||||
/* init channels */
|
||||
{
|
||||
@ -900,11 +891,27 @@ static int clHCA_DecodeHeader(clHCA *hca, void *data, unsigned int size) {
|
||||
}
|
||||
|
||||
|
||||
/* clHCA is correctly initialized and decoder state reset
|
||||
* (keycode is not changed between calls) */
|
||||
hca->is_valid = 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void clHCA_SetKey(clHCA *hca, unsigned long long keycode) {
|
||||
if (!hca)
|
||||
return;
|
||||
hca->keycode = keycode;
|
||||
|
||||
/* May be called even if clHCA is not valid (header not parsed), as the
|
||||
* key will be used during DecodeHeader ciph init. If header was already
|
||||
* parsed reinitializes the decryption table using the new key. */
|
||||
if (hca->is_valid) {
|
||||
/* ignore error since it can't really fail */
|
||||
cipher_init(hca->cipher_table, hca->ciph_type, hca->keycode);
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------
|
||||
// Decode
|
||||
//--------------------------------------------------
|
||||
@ -923,7 +930,7 @@ static void decode4_apply_intensity_stereo(stChannel *ch, int subframe,
|
||||
static void decoder5_run_imdct(stChannel *ch, int subframe);
|
||||
|
||||
|
||||
static int clHCA_DecodeFrame(clHCA *hca, void *data, unsigned int size) {
|
||||
int clHCA_DecodeBlock(clHCA *hca, void *data, unsigned int size) {
|
||||
clData br;
|
||||
unsigned short sync;
|
||||
unsigned int subframe, ch;
|
||||
@ -1546,20 +1553,3 @@ static void decoder5_run_imdct(stChannel *ch, int subframe) {
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------
|
||||
// Main decode
|
||||
//--------------------------------------------------
|
||||
int clHCA_Decode(clHCA *hca, void *data, unsigned int size, unsigned int address) {
|
||||
if (!hca)
|
||||
return -1;
|
||||
|
||||
if (address == 0) {
|
||||
return clHCA_DecodeHeader(hca, data, size);
|
||||
}
|
||||
else if (address >= hca->header_size) {
|
||||
return clHCA_DecodeFrame(hca, data, size);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -4,7 +4,19 @@
|
||||
/* init a HCA stream; STREAMFILE will be duplicated for internal use. */
|
||||
hca_codec_data * init_hca(STREAMFILE *streamFile) {
|
||||
char filename[PATH_LIMIT];
|
||||
uint8_t header_buffer[0x2000]; /* hca header buffer data (probable max ~0x400) */
|
||||
hca_codec_data * data = NULL; /* vgmstream HCA context */
|
||||
int header_size;
|
||||
int status;
|
||||
|
||||
/* test header */
|
||||
if (read_streamfile(header_buffer, 0x00, 0x08, streamFile) != 0x08)
|
||||
goto fail;
|
||||
header_size = clHCA_isOurFile(header_buffer, 0x08);
|
||||
if (header_size < 0 || header_size > 0x1000)
|
||||
goto fail;
|
||||
if (read_streamfile(header_buffer, 0x00, header_size, streamFile) != header_size)
|
||||
goto fail;
|
||||
|
||||
/* init vgmstream context */
|
||||
data = calloc(1, sizeof(hca_codec_data));
|
||||
@ -12,12 +24,28 @@ hca_codec_data * init_hca(STREAMFILE *streamFile) {
|
||||
|
||||
/* init library handle */
|
||||
data->handle = calloc(1, clHCA_sizeof());
|
||||
clHCA_clear(data->handle);
|
||||
|
||||
status = clHCA_DecodeHeader(data->handle, header_buffer, header_size); /* parse header */
|
||||
if (status < 0) goto fail;
|
||||
|
||||
status = clHCA_getInfo(data->handle, &data->info); /* extract header info */
|
||||
if (status < 0) goto fail;
|
||||
|
||||
data->data_buffer = malloc(data->info.blockSize);
|
||||
if (!data->data_buffer) goto fail;
|
||||
|
||||
data->sample_buffer = malloc(sizeof(signed short) * data->info.channelCount * data->info.samplesPerBlock);
|
||||
if (!data->sample_buffer) goto fail;
|
||||
|
||||
/* load streamfile for reads */
|
||||
get_streamfile_name(streamFile,filename, sizeof(filename));
|
||||
data->streamfile = open_streamfile(streamFile,filename);
|
||||
if (!data->streamfile) goto fail;
|
||||
|
||||
/* set initial values */
|
||||
reset_hca(data);
|
||||
|
||||
return data;
|
||||
|
||||
fail:
|
||||
@ -31,16 +59,6 @@ void decode_hca(hca_codec_data * data, sample * outbuf, int32_t samples_to_do) {
|
||||
const unsigned int blockSize = data->info.blockSize;
|
||||
|
||||
|
||||
//todo improve (can't be done on init since data->info is only read after setting key)
|
||||
if (!data->data_buffer) {
|
||||
data->data_buffer = malloc(data->info.blockSize);
|
||||
if (!data->data_buffer) return;
|
||||
|
||||
data->sample_buffer = malloc(sizeof(signed short) * data->info.channelCount * clHCA_samplesPerBlock);
|
||||
if (!data->sample_buffer) return;
|
||||
}
|
||||
|
||||
|
||||
while (samples_done < samples_to_do) {
|
||||
|
||||
if (data->samples_filled) {
|
||||
@ -68,7 +86,7 @@ void decode_hca(hca_codec_data * data, sample * outbuf, int32_t samples_to_do) {
|
||||
data->samples_filled -= samples_to_get;
|
||||
}
|
||||
else {
|
||||
const unsigned int address = data->info.dataOffset + data->current_block * blockSize;
|
||||
off_t offset = data->info.headerSize + data->current_block * blockSize;
|
||||
int status;
|
||||
size_t bytes;
|
||||
|
||||
@ -79,21 +97,21 @@ void decode_hca(hca_codec_data * data, sample * outbuf, int32_t samples_to_do) {
|
||||
}
|
||||
|
||||
/* read frame */
|
||||
bytes = read_streamfile(data->data_buffer, address, blockSize, data->streamfile);
|
||||
bytes = read_streamfile(data->data_buffer, offset, blockSize, data->streamfile);
|
||||
if (bytes != blockSize) {
|
||||
VGM_LOG("HCA: read %x vs expected %x bytes at %x\n", bytes, blockSize, address);
|
||||
VGM_LOG("HCA: read %x vs expected %x bytes at %lx\n", bytes, blockSize, offset);
|
||||
break;
|
||||
}
|
||||
|
||||
/* decode frame */
|
||||
status = clHCA_Decode(data->handle, (void*)(data->data_buffer), blockSize, address);
|
||||
status = clHCA_DecodeBlock(data->handle, (void*)(data->data_buffer), blockSize);
|
||||
if (status < 0) {
|
||||
VGM_LOG("HCA: decode fail at %x", address);
|
||||
VGM_LOG("HCA: decode fail at %lx", offset);
|
||||
break;
|
||||
}
|
||||
|
||||
/* extract samples */
|
||||
clHCA_DecodeSamples16(data->handle, data->sample_buffer);
|
||||
clHCA_ReadSamples16(data->handle, data->sample_buffer);
|
||||
|
||||
data->current_block++;
|
||||
data->samples_consumed = 0;
|
||||
|
111
src/meta/hca.c
111
src/meta/hca.c
@ -2,52 +2,34 @@
|
||||
#include "hca_keys.h"
|
||||
#include "../coding/coding.h"
|
||||
|
||||
static void find_hca_key(hca_codec_data * hca_data, uint8_t * header_buffer, int header_size, unsigned int * out_keycode_upper, unsigned int * out_keycode_lower);
|
||||
static void find_hca_key(hca_codec_data * hca_data, unsigned long long * out_keycode);
|
||||
|
||||
VGMSTREAM * init_vgmstream_hca(STREAMFILE *streamFile) {
|
||||
VGMSTREAM * vgmstream = NULL;
|
||||
uint8_t header_buffer[0x8000]; /* hca header buffer data (probably max ~0x400) */
|
||||
|
||||
hca_codec_data * hca_data = NULL; /* vgmstream HCA context */
|
||||
unsigned int keycode_upper, keycode_lower;
|
||||
int header_size;
|
||||
hca_codec_data * hca_data = NULL;
|
||||
unsigned long long keycode = 0;
|
||||
|
||||
/* checks */
|
||||
if ( !check_extensions(streamFile, "hca"))
|
||||
return NULL;
|
||||
|
||||
/* init header */
|
||||
if (read_streamfile(header_buffer, 0x00, 0x08, streamFile) != 0x08)
|
||||
goto fail;
|
||||
header_size = clHCA_isOurFile0(header_buffer);
|
||||
if (header_size < 0 || header_size > 0x8000) goto fail;
|
||||
|
||||
if (read_streamfile(header_buffer, 0x00, header_size, streamFile) != header_size) goto fail;
|
||||
if (clHCA_isOurFile1(header_buffer, header_size) < 0)
|
||||
if (((uint32_t)read_32bitBE(0x00,streamFile) & 0x7f7f7f7f) != 0x48434100) /* "HCA\0", possibly masked */
|
||||
goto fail;
|
||||
|
||||
/* init vgmstream context */
|
||||
/* init vgmstream and library's context, will validate the HCA */
|
||||
hca_data = init_hca(streamFile);
|
||||
|
||||
if (!hca_data) goto fail;
|
||||
|
||||
/* find decryption key in external file or preloaded list */
|
||||
{
|
||||
uint8_t keybuf[8];
|
||||
if (read_key_file(keybuf, 8, streamFile) == 8) {
|
||||
keycode_upper = get_32bitBE(keybuf+0);
|
||||
keycode_lower = get_32bitBE(keybuf+4);
|
||||
keycode = (uint64_t)get_64bitBE(keybuf+0x00);
|
||||
} else {
|
||||
find_hca_key(hca_data, header_buffer, header_size, &keycode_upper, &keycode_lower);
|
||||
find_hca_key(hca_data, &keycode);
|
||||
}
|
||||
}
|
||||
|
||||
/* re-init decoder with key (as it must be supplied on header read) */
|
||||
clHCA_clear(hca_data->handle, keycode_lower, keycode_upper);
|
||||
if (clHCA_Decode(hca_data->handle, header_buffer, header_size, 0) < 0) /* read header at 0x00 */
|
||||
goto fail;
|
||||
if (clHCA_getInfo(hca_data->handle, &hca_data->info) < 0) /* copy important header values to info struct */
|
||||
goto fail;
|
||||
reset_hca(hca_data);
|
||||
clHCA_SetKey(hca_data->handle, keycode); //maybe should be done through hca_decoder.c?
|
||||
}
|
||||
|
||||
|
||||
/* build the VGMSTREAM */
|
||||
@ -87,58 +69,40 @@ fail:
|
||||
|
||||
/* Tries to find the decryption key from a list. Simply decodes a few frames and checks if there aren't too many
|
||||
* clipped samples, as it's common for invalid keys (though possible with valid keys in poorly mastered files). */
|
||||
static void find_hca_key(hca_codec_data * hca_data, uint8_t * header_buffer, int header_size, unsigned int * out_keycode_upper, unsigned int * out_keycode_lower) {
|
||||
sample *testbuf = NULL;
|
||||
int i, j, bufsize = 0, tempsize;
|
||||
size_t keys_length = sizeof(hcakey_list) / sizeof(hcakey_info);
|
||||
|
||||
static void find_hca_key(hca_codec_data * hca_data, unsigned long long * out_keycode) {
|
||||
const size_t keys_length = sizeof(hcakey_list) / sizeof(hcakey_info);
|
||||
sample *test_samples = NULL;
|
||||
size_t buffer_samples = hca_data->info.samplesPerBlock * hca_data->info.channelCount;
|
||||
unsigned long long best_keycode;
|
||||
int i;
|
||||
int min_clip_count = -1;
|
||||
/* defaults to PSO2 key, most common */
|
||||
unsigned int best_keycode_upper = 0xCC554639;
|
||||
unsigned int best_keycode_lower = 0x30DBE1AB;
|
||||
|
||||
|
||||
test_samples = malloc(sizeof(sample) * buffer_samples);
|
||||
if (!test_samples)
|
||||
return; /* ??? */
|
||||
|
||||
best_keycode = 0xCC55463930DBE1AB; /* defaults to PSO2 key, most common */
|
||||
|
||||
|
||||
/* find a candidate key */
|
||||
for (i = 0; i < keys_length; i++) {
|
||||
int clip_count = 0, sample_count = 0;
|
||||
int frame = 0, s;
|
||||
unsigned long long keycode = (unsigned long long)hcakey_list[i].key;
|
||||
|
||||
unsigned int keycode_upper, keycode_lower;
|
||||
uint64_t key = hcakey_list[i].key;
|
||||
keycode_upper = (key >> 32) & 0xFFFFFFFF;
|
||||
keycode_lower = (key >> 0) & 0xFFFFFFFF;
|
||||
|
||||
|
||||
/* re-init HCA with the current key as buffer becomes invalid (probably can be simplified) */
|
||||
clHCA_SetKey(hca_data->handle, keycode);
|
||||
reset_hca(hca_data);
|
||||
if (read_streamfile(header_buffer, 0x00, header_size, hca_data->streamfile) != header_size)
|
||||
continue;
|
||||
|
||||
clHCA_clear(hca_data->handle, keycode_lower, keycode_upper);
|
||||
if (clHCA_Decode(hca_data->handle, header_buffer, header_size, 0) < 0)
|
||||
continue;
|
||||
if (clHCA_getInfo(hca_data->handle, &hca_data->info) < 0)
|
||||
continue;
|
||||
|
||||
tempsize = sizeof(sample) * clHCA_samplesPerBlock * hca_data->info.channelCount;
|
||||
if (tempsize > bufsize) { /* should happen once */
|
||||
sample *temp = (sample *)realloc(testbuf, tempsize);
|
||||
if (!temp) goto end;
|
||||
testbuf = temp;
|
||||
bufsize = tempsize;
|
||||
}
|
||||
|
||||
/* test enough frames, but not too many */
|
||||
while (frame < HCA_KEY_MAX_TEST_FRAMES && frame < hca_data->info.blockCount) {
|
||||
j = clHCA_samplesPerBlock;
|
||||
decode_hca(hca_data, testbuf, j);
|
||||
decode_hca(hca_data, test_samples, hca_data->info.samplesPerBlock);
|
||||
|
||||
j *= hca_data->info.channelCount;
|
||||
for (s = 0; s < j; s++) {
|
||||
if (testbuf[s] != 0x0000 && testbuf[s] != 0xFFFF)
|
||||
for (s = 0; s < buffer_samples; s++) {
|
||||
if (test_samples[s] != 0 && test_samples[s] != -1)
|
||||
sample_count++; /* ignore upper/lower blank samples */
|
||||
|
||||
if (testbuf[s] == 0x7FFF || testbuf[s] == 0x8000)
|
||||
if (test_samples[s] == 32767 || test_samples[s] == -32768)
|
||||
clip_count++; /* upper/lower clip */
|
||||
}
|
||||
|
||||
@ -152,8 +116,7 @@ static void find_hca_key(hca_codec_data * hca_data, uint8_t * header_buffer, int
|
||||
|
||||
if (min_clip_count < 0 || clip_count < min_clip_count) {
|
||||
min_clip_count = clip_count;
|
||||
best_keycode_upper = keycode_upper;
|
||||
best_keycode_lower = keycode_lower;
|
||||
best_keycode = keycode;
|
||||
}
|
||||
|
||||
if (min_clip_count == 0)
|
||||
@ -164,13 +127,11 @@ static void find_hca_key(hca_codec_data * hca_data, uint8_t * header_buffer, int
|
||||
// break;
|
||||
}
|
||||
|
||||
/* reset HCA header */
|
||||
hca_data->current_block = 0;
|
||||
read_streamfile(header_buffer, 0x00, header_size, hca_data->streamfile);
|
||||
VGM_ASSERT(min_clip_count > 0,
|
||||
"HCA: best key=%08x%08x (clips=%i)\n",
|
||||
(uint32_t)((best_keycode >> 32) & 0xFFFFFFFF), (uint32_t)(best_keycode & 0xFFFFFFFF), min_clip_count);
|
||||
|
||||
end:
|
||||
VGM_ASSERT(min_clip_count > 0, "HCA: best key=%08x%08x (clips=%i)\n", best_keycode_upper,best_keycode_lower, min_clip_count);
|
||||
*out_keycode_upper = best_keycode_upper;
|
||||
*out_keycode_lower = best_keycode_lower;
|
||||
free(testbuf);
|
||||
reset_hca(hca_data);
|
||||
*out_keycode = best_keycode;
|
||||
free(test_samples);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user