Util to read encryption keys from external file based on a songname

The format is "(songname.ext)key" (per song), "(.ext)key" (per folder)
This commit is contained in:
bnnm 2016-11-27 19:35:26 +01:00
parent 537dc454a5
commit 5a1a122698
2 changed files with 87 additions and 0 deletions

View File

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

View File

@ -34,6 +34,14 @@
#define STREAMFILE_DEFAULT_BUFFER_SIZE 0x400
#ifndef DIR_SEPARATOR
#if defined (_WIN32) || defined (WIN32)
#define DIR_SEPARATOR '\\'
#else
#define DIR_SEPARATOR '/'
#endif
#endif
typedef struct _STREAMFILE {
size_t (*read)(struct _STREAMFILE *,uint8_t * dest, off_t offset, size_t length);
size_t (*get_size)(struct _STREAMFILE *);
@ -141,4 +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,
STREAMFILE * infile, int *line_done_ptr);
int read_key_file(uint8_t * buf, size_t bufsize, const char * filename);
#endif