2
0

Fix masterKeyProvider args & mem bugs + multiple format extensions.

This commit is contained in:
Maarten Billemont
2020-01-23 15:52:28 -05:00
parent 070f909a7f
commit dd123a431c
5 changed files with 133 additions and 47 deletions

View File

@@ -181,7 +181,7 @@ static bool mpw_marshal_write_flat(
if (!user->redacted) {
// Clear Text
mpw_free( &masterKey, MPMasterKeySize );
if (!(masterKey = user->masterKeyProvider( user->algorithm, user->fullName ))) {
if (!(masterKey = user->masterKeyProvider( site->algorithm, user->fullName ))) {
*error = (MPMarshalError){ MPMarshalErrorInternal, "Couldn't derive master key." };
return false;
}
@@ -212,6 +212,7 @@ static bool mpw_marshal_write_flat(
}
#if MPW_JSON
static bool mpw_marshal_write_json(
char **out, const MPMarshalledUser *user, MPMarshalError *error) {
@@ -263,7 +264,7 @@ static bool mpw_marshal_write_json(
if (!user->redacted) {
// Clear Text
mpw_free( &masterKey, MPMasterKeySize );
if (!(masterKey = user->masterKeyProvider( user->algorithm, user->fullName ))) {
if (!(masterKey = user->masterKeyProvider( site->algorithm, user->fullName ))) {
*error = (MPMarshalError){ MPMarshalErrorInternal, "Couldn't derive master key." };
return false;
}
@@ -339,6 +340,7 @@ static bool mpw_marshal_write_json(
*error = (MPMarshalError){ .type = MPMarshalSuccess };
return true;
}
#endif
bool mpw_marshal_write(
@@ -593,7 +595,7 @@ static MPMarshalledUser *mpw_marshal_read_flat(
if (!user->redacted) {
// Clear Text
mpw_free( &masterKey, MPMasterKeySize );
if (!(masterKey = masterKeyProvider( user->algorithm, user->fullName ))) {
if (!(masterKey = masterKeyProvider( site->algorithm, user->fullName ))) {
*error = (MPMarshalError){ MPMarshalErrorInternal, "Couldn't derive master key." };
return NULL;
}
@@ -632,6 +634,7 @@ static MPMarshalledUser *mpw_marshal_read_flat(
}
#if MPW_JSON
static void mpw_marshal_read_json_info(
const char *in, MPMarshalInfo *info) {
@@ -713,7 +716,7 @@ static MPMarshalledUser *mpw_marshal_read_json(
*error = (MPMarshalError){ MPMarshalErrorMissing, "Missing value for full name." };
return NULL;
}
if (!(masterKey = masterKeyProvider( user->algorithm, user->fullName ))) {
if (!(masterKey = masterKeyProvider( algorithm, fullName ))) {
*error = (MPMarshalError){ MPMarshalErrorInternal, "Couldn't derive master key." };
return NULL;
}
@@ -779,7 +782,7 @@ static MPMarshalledUser *mpw_marshal_read_json(
if (!user->redacted) {
// Clear Text
mpw_free( &masterKey, MPMasterKeySize );
if (!(masterKey = masterKeyProvider( user->algorithm, user->fullName ))) {
if (!(masterKey = masterKeyProvider( site->algorithm, user->fullName ))) {
*error = (MPMarshalError){ MPMarshalErrorInternal, "Couldn't derive master key." };
return NULL;
}
@@ -827,6 +830,7 @@ static MPMarshalledUser *mpw_marshal_read_json(
*error = (MPMarshalError){ .type = MPMarshalSuccess };
return user;
}
#endif
MPMarshalInfo *mpw_marshal_read_info(
@@ -913,10 +917,41 @@ const char *mpw_marshal_format_extension(
case MPMarshalFormatFlat:
return "mpsites";
case MPMarshalFormatJSON:
return "mpsites.json";
return "mpjson";
default: {
dbg( "Unknown format: %d", format );
return NULL;
}
}
}
int mpw_marshal_format_extensions(
const MPMarshalFormat format, const char ***extensions) {
int count = 0;
switch (format) {
case MPMarshalFormatNone: {
break;
}
case MPMarshalFormatFlat: {
const char *array[3] = { mpw_marshal_format_extension( format ), "mpsites.txt", "txt" };
count = sizeof( array ) / sizeof( *array );
*extensions = realloc( *extensions, count * sizeof( const char * ) );
memcpy( *extensions, array, count * sizeof( const char * ) );
break;
}
case MPMarshalFormatJSON: {
const char *array[3] = { mpw_marshal_format_extension( format ), "mpsites.json", "json" };
count = sizeof( array ) / sizeof( *array );
*extensions = realloc( *extensions, sizeof( array ) );
memcpy( *extensions, array, sizeof( array ) );
break;
}
default: {
dbg( "Unknown format: %d", format );
break;
}
}
return count;
}

View File

@@ -38,6 +38,8 @@ typedef mpw_enum( unsigned int, MPMarshalFormat ) {
#else
MPMarshalFormatDefault = MPMarshalFormatFlat,
#endif
MPMarshalFormatFirst = MPMarshalFormatFlat,
MPMarshalFormatLast = MPMarshalFormatJSON,
};
typedef mpw_enum( unsigned int, MPMarshalErrorType ) {
@@ -162,5 +164,12 @@ const char *mpw_nameForFormat(
*/
const char *mpw_marshal_format_extension(
const MPMarshalFormat format);
/**
* @param extensions An array of filename extensions that are used for files of this format,
* the first being the currently preferred/output extension. Free after use.
* @return The amount of filename extensions returned in the array.
*/
int mpw_marshal_format_extensions(
const MPMarshalFormat format, const char ***extensions);
#endif // _MPW_MARSHAL_H

View File

@@ -490,17 +490,25 @@ const size_t mpw_utf8_strlen(const char *utf8String) {
return charlen;
}
void *mpw_memdup(const void *src, size_t len) {
if (!src)
return NULL;
char *dst = malloc( len );
if (dst)
memcpy( dst, src, len );
return dst;
}
char *mpw_strdup(const char *src) {
if (!src)
return NULL;
size_t len = strlen( src );
char *dst = malloc( len + 1 );
memcpy( dst, src, len );
dst[len] = '\0';
return dst;
return mpw_memdup( src, len + 1 );
}
char *mpw_strndup(const char *src, size_t max) {

View File

@@ -223,6 +223,8 @@ bool mpw_id_buf_equals(const char *id1, const char *id2);
/** @return The amount of display characters in the given UTF-8 string. */
const size_t mpw_utf8_strlen(const char *utf8String);
/** Drop-in for memdup(3). */
void *mpw_memdup(const void *src, size_t len);
/** Drop-in for POSIX strdup(3). */
char *mpw_strdup(const char *src);
/** Drop-in for POSIX strndup(3). */