Open key using streamFile, needed to allow user-defined callbacks

This commit is contained in:
bnnm 2016-11-27 22:42:42 +01:00
parent 088fb4a480
commit f5e8e8ad8e
3 changed files with 17 additions and 16 deletions

View File

@ -496,11 +496,9 @@ static int find_key(STREAMFILE *file, uint8_t type, uint16_t *xor_start, uint16_
/* try to find key in external file first */ /* try to find key in external file first */
{ {
char filename[PATH_LIMIT];
uint8_t keybuf[6]; uint8_t keybuf[6];
file->get_name(file,filename,sizeof(filename)); if ( read_key_file(keybuf, 6, file) ) {
if ( read_key_file(keybuf, 6, filename) ) {
*xor_start = get_16bitBE(keybuf+0); *xor_start = get_16bitBE(keybuf+0);
*xor_mult = get_16bitBE(keybuf+2); *xor_mult = get_16bitBE(keybuf+2);
*xor_add = get_16bitBE(keybuf+4); *xor_add = get_16bitBE(keybuf+4);

View File

@ -275,57 +275,60 @@ size_t get_streamfile_dos_line(int dst_length, char * dst, off_t offset,
/** /**
* open file containing decryption keys and copy to buffer * open file containing decryption keys and copy to buffer
* tries combinations of keynames based on the original songname * tries combinations of keynames based on the original filename
* *
* returns true if found and copied * returns true if found and copied
*/ */
int read_key_file(uint8_t * buf, size_t bufsize, const char * songname) { int read_key_file(uint8_t * buf, size_t bufsize, STREAMFILE *streamFile) {
char keyname[PATH_LIMIT]; char keyname[PATH_LIMIT];
char filename[PATH_LIMIT];
const char *path, *ext; const char *path, *ext;
STREAMFILE * streamFileKey = NULL; STREAMFILE * streamFileKey = NULL;
if (strlen(songname)+4 > sizeof(keyname)) goto fail; streamFile->get_name(streamFile,filename,sizeof(filename));
if (strlen(filename)+4 > sizeof(keyname)) goto fail;
/* try to open a keyfile using variations: /* try to open a keyfile using variations:
* "(name.ext)key" (per song), "(.ext)key" (per folder) */ * "(name.ext)key" (per song), "(.ext)key" (per folder) */
{ {
ext = strrchr(songname,'.'); ext = strrchr(filename,'.');
if (ext!=NULL) ext = ext+1; if (ext!=NULL) ext = ext+1;
path = strrchr(songname,DIR_SEPARATOR); path = strrchr(filename,DIR_SEPARATOR);
if (path!=NULL) path = path+1; if (path!=NULL) path = path+1;
/* "(name.ext)key" */ /* "(name.ext)key" */
strcpy(keyname, songname); strcpy(keyname, filename);
strcat(keyname, "key"); strcat(keyname, "key");
streamFileKey = open_stdio_streamfile(keyname); streamFileKey = streamFile->open(streamFile,keyname,STREAMFILE_DEFAULT_BUFFER_SIZE);
if (streamFileKey) goto found; if (streamFileKey) goto found;
/* "(name.ext)KEY" */ /* "(name.ext)KEY" */
/* /*
strcpy(keyname+strlen(keyname)-3,"KEY"); strcpy(keyname+strlen(keyname)-3,"KEY");
streamFileKey = open_stdio_streamfile(keyname); streamFileKey = streamFile->open(streamFile,keyname,STREAMFILE_DEFAULT_BUFFER_SIZE);
if (streamFileKey) goto found; if (streamFileKey) goto found;
*/ */
/* "(.ext)key" */ /* "(.ext)key" */
if (path) { if (path) {
strcpy(keyname, songname); strcpy(keyname, filename);
keyname[path-songname] = '\0'; keyname[path-filename] = '\0';
strcat(keyname, "."); strcat(keyname, ".");
} else { } else {
strcpy(keyname, "."); strcpy(keyname, ".");
} }
if (ext) strcat(keyname, ext); if (ext) strcat(keyname, ext);
strcat(keyname, "key"); strcat(keyname, "key");
streamFileKey = open_stdio_streamfile(keyname); streamFileKey = streamFile->open(streamFile,keyname,STREAMFILE_DEFAULT_BUFFER_SIZE);
if (streamFileKey) goto found; if (streamFileKey) goto found;
/* "(.ext)KEY" */ /* "(.ext)KEY" */
/* /*
strcpy(keyname+strlen(keyname)-3,"KEY"); strcpy(keyname+strlen(keyname)-3,"KEY");
streamFileKey = open_stdio_streamfile(keyname); streamFileKey = streamFile->open(streamFile,keyname,STREAMFILE_DEFAULT_BUFFER_SIZE);
if (streamFileKey) goto found; if (streamFileKey) goto found;
*/ */

View File

@ -149,7 +149,7 @@ static inline STREAMFILE * open_stdio_streamfile(const char * const filename) {
size_t get_streamfile_dos_line(int dst_length, char * dst, off_t offset, size_t get_streamfile_dos_line(int dst_length, char * dst, off_t offset,
STREAMFILE * infile, int *line_done_ptr); STREAMFILE * infile, int *line_done_ptr);
int read_key_file(uint8_t * buf, size_t bufsize, const char * filename); int read_key_file(uint8_t * buf, size_t bufsize, STREAMFILE *streamFile);
#endif #endif