From ae2ba1bc296cb33021661fc32371797c9fc23ccd Mon Sep 17 00:00:00 2001 From: bnnm Date: Sat, 20 Jan 2018 20:06:15 +0100 Subject: [PATCH] Update read_key_file for variable-sized keys --- src/meta/adx.c | 2 +- src/meta/ahx.c | 2 +- src/meta/hca.c | 2 +- src/streamfile.c | 24 +++++++++++------------- src/streamfile.h | 2 +- 5 files changed, 15 insertions(+), 17 deletions(-) diff --git a/src/meta/adx.c b/src/meta/adx.c index 683e5cc0..5e6a875c 100644 --- a/src/meta/adx.c +++ b/src/meta/adx.c @@ -249,7 +249,7 @@ static int find_key(STREAMFILE *file, uint8_t type, uint16_t *xor_start, uint16_ { uint8_t keybuf[6]; - if ( read_key_file(keybuf, 6, file) ) { + if (read_key_file(keybuf, 6, file) == 6) { *xor_start = get_16bitBE(keybuf+0); *xor_mult = get_16bitBE(keybuf+2); *xor_add = get_16bitBE(keybuf+4); diff --git a/src/meta/ahx.c b/src/meta/ahx.c index 516773e0..59570750 100644 --- a/src/meta/ahx.c +++ b/src/meta/ahx.c @@ -56,7 +56,7 @@ VGMSTREAM * init_vgmstream_ahx(STREAMFILE *streamFile) { if (cfg.encryption) { uint8_t keybuf[6]; - if (read_key_file(keybuf, 6, streamFile)) { + if (read_key_file(keybuf, 6, streamFile) == 6) { cfg.cri_key1 = get_16bitBE(keybuf+0); cfg.cri_key2 = get_16bitBE(keybuf+2); cfg.cri_key3 = get_16bitBE(keybuf+4); diff --git a/src/meta/hca.c b/src/meta/hca.c index 10261460..fcd599be 100644 --- a/src/meta/hca.c +++ b/src/meta/hca.c @@ -54,7 +54,7 @@ VGMSTREAM * init_vgmstream_hca(STREAMFILE *streamFile) { /* find decryption key in external file or preloaded list */ { uint8_t keybuf[8]; - if ( read_key_file(keybuf, 8, streamFile) ) { + if (read_key_file(keybuf, 8, streamFile) == 8) { ciphKey2 = get_32bitBE(keybuf+0); ciphKey1 = get_32bitBE(keybuf+4); } else { diff --git a/src/streamfile.c b/src/streamfile.c index 79e349fb..61e8b3bf 100644 --- a/src/streamfile.c +++ b/src/streamfile.c @@ -546,17 +546,15 @@ STREAMFILE * open_stream_name(STREAMFILE *streamFile, const char * name) { return streamFile->open(streamFile,filename,STREAMFILE_DEFAULT_BUFFER_SIZE); } -/** - * open file containing decryption keys and copy to buffer - * tries combinations of keynames based on the original filename - * - * returns true if found and copied - */ -int read_key_file(uint8_t * buf, size_t bufsize, STREAMFILE *streamFile) { +/* Opens a file containing decryption keys and copies to buffer. + * Tries combinations of keynames based on the original filename. + * returns size of key if found and copied */ +size_t read_key_file(uint8_t * buf, size_t bufsize, STREAMFILE *streamFile) { char keyname[PATH_LIMIT]; char filename[PATH_LIMIT]; const char *path, *ext; STREAMFILE * streamFileKey = NULL; + size_t keysize; streamFile->get_name(streamFile,filename,sizeof(filename)); @@ -609,17 +607,17 @@ int read_key_file(uint8_t * buf, size_t bufsize, STREAMFILE *streamFile) { } found: - if (get_streamfile_size(streamFileKey) != bufsize) goto fail; + keysize = get_streamfile_size(streamFileKey); + if (keysize > bufsize) goto fail; - if (read_streamfile(buf, 0, bufsize, streamFileKey)!=bufsize) goto fail; + if (read_streamfile(buf, 0, keysize, streamFileKey) != keysize) + goto fail; close_streamfile(streamFileKey); - - return 1; + return keysize; fail: - if (streamFileKey) close_streamfile(streamFileKey); - + close_streamfile(streamFileKey); return 0; } diff --git a/src/streamfile.h b/src/streamfile.h index 349ea584..fa9da54f 100644 --- a/src/streamfile.h +++ b/src/streamfile.h @@ -155,7 +155,7 @@ STREAMFILE * open_stream_name(STREAMFILE *streamFile, const char * ext); int read_string(char * buf, size_t bufsize, off_t offset, STREAMFILE *streamFile); -int read_key_file(uint8_t * buf, size_t bufsize, STREAMFILE *streamFile); +size_t read_key_file(uint8_t * buf, size_t bufsize, STREAMFILE *streamFile); int read_pos_file(uint8_t * buf, size_t bufsize, STREAMFILE *streamFile); int check_extensions(STREAMFILE *streamFile, const char * cmp_exts);