2017-07-16 01:13:49 +00:00
|
|
|
//==============================================================================
|
|
|
|
// This file is part of Master Password.
|
|
|
|
// Copyright (c) 2011-2017, Maarten Billemont.
|
|
|
|
//
|
|
|
|
// Master Password is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Master Password is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You can find a copy of the GNU General Public License in the
|
|
|
|
// LICENSE file. Alternatively, see <http://www.gnu.org/licenses/>.
|
|
|
|
//==============================================================================
|
|
|
|
|
2017-09-22 19:20:14 +00:00
|
|
|
#ifndef _MPW_MARSHAL_H
|
|
|
|
#define _MPW_MARSHAL_H
|
2017-07-16 01:13:49 +00:00
|
|
|
|
2017-07-28 13:50:26 +00:00
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#include "mpw-algorithm.h"
|
2017-07-16 01:13:49 +00:00
|
|
|
|
|
|
|
//// Types.
|
|
|
|
|
2017-09-20 14:48:04 +00:00
|
|
|
typedef mpw_enum( unsigned int, MPMarshalFormat ) {
|
2017-09-20 16:43:03 +00:00
|
|
|
/** Do not marshal. */
|
2017-09-20 14:48:04 +00:00
|
|
|
MPMarshalFormatNone,
|
2017-09-20 16:43:03 +00:00
|
|
|
/** Marshal using the line-based plain-text format. */
|
2017-09-20 14:48:04 +00:00
|
|
|
MPMarshalFormatFlat,
|
2017-09-20 16:43:03 +00:00
|
|
|
/** Marshal using the JSON structured format. */
|
2017-09-20 14:48:04 +00:00
|
|
|
MPMarshalFormatJSON,
|
2017-08-06 03:19:24 +00:00
|
|
|
|
2017-09-06 04:32:50 +00:00
|
|
|
#if MPW_JSON
|
2017-09-20 14:48:04 +00:00
|
|
|
MPMarshalFormatDefault = MPMarshalFormatJSON,
|
2017-09-06 04:32:50 +00:00
|
|
|
#else
|
2017-09-20 14:48:04 +00:00
|
|
|
MPMarshalFormatDefault = MPMarshalFormatFlat,
|
2017-09-06 04:32:50 +00:00
|
|
|
#endif
|
2017-07-16 01:13:49 +00:00
|
|
|
};
|
|
|
|
|
2017-09-20 14:48:04 +00:00
|
|
|
typedef mpw_enum( unsigned int, MPMarshalErrorType ) {
|
2017-07-23 20:49:55 +00:00
|
|
|
/** The marshalling operation completed successfully. */
|
2017-09-20 14:48:04 +00:00
|
|
|
MPMarshalSuccess,
|
2017-07-23 20:49:55 +00:00
|
|
|
/** An error in the structure of the marshall file interrupted marshalling. */
|
2017-09-20 14:48:04 +00:00
|
|
|
MPMarshalErrorStructure,
|
2017-07-23 20:49:55 +00:00
|
|
|
/** The marshall file uses an unsupported format version. */
|
2017-09-20 14:48:04 +00:00
|
|
|
MPMarshalErrorFormat,
|
2017-07-23 20:49:55 +00:00
|
|
|
/** A required value is missing or not specified. */
|
2017-09-20 14:48:04 +00:00
|
|
|
MPMarshalErrorMissing,
|
2017-07-23 20:49:55 +00:00
|
|
|
/** The given master password is not valid. */
|
2017-09-20 14:48:04 +00:00
|
|
|
MPMarshalErrorMasterPassword,
|
2017-07-23 20:49:55 +00:00
|
|
|
/** An illegal value was specified. */
|
2017-09-20 14:48:04 +00:00
|
|
|
MPMarshalErrorIllegal,
|
2017-07-23 20:49:55 +00:00
|
|
|
/** An internal system error interrupted marshalling. */
|
2017-09-20 14:48:04 +00:00
|
|
|
MPMarshalErrorInternal,
|
2017-07-23 20:49:55 +00:00
|
|
|
};
|
2017-09-20 14:48:04 +00:00
|
|
|
typedef struct MPMarshalError {
|
|
|
|
MPMarshalErrorType type;
|
2017-08-03 05:07:19 +00:00
|
|
|
const char *description;
|
2017-09-20 14:48:04 +00:00
|
|
|
} MPMarshalError;
|
2017-07-23 20:49:55 +00:00
|
|
|
|
2017-07-16 01:13:49 +00:00
|
|
|
typedef struct MPMarshalledQuestion {
|
|
|
|
const char *keyword;
|
2017-08-22 22:18:24 +00:00
|
|
|
const char *content;
|
|
|
|
MPResultType type;
|
2017-07-16 01:13:49 +00:00
|
|
|
} MPMarshalledQuestion;
|
|
|
|
|
|
|
|
typedef struct MPMarshalledSite {
|
|
|
|
const char *name;
|
|
|
|
const char *content;
|
2017-08-10 16:30:42 +00:00
|
|
|
MPResultType type;
|
2017-08-08 00:27:08 +00:00
|
|
|
MPCounterValue counter;
|
2017-07-23 01:38:53 +00:00
|
|
|
MPAlgorithmVersion algorithm;
|
2017-07-16 01:13:49 +00:00
|
|
|
|
2017-08-22 22:18:24 +00:00
|
|
|
const char *loginContent;
|
|
|
|
MPResultType loginType;
|
2017-07-16 01:13:49 +00:00
|
|
|
|
|
|
|
const char *url;
|
|
|
|
unsigned int uses;
|
|
|
|
time_t lastUsed;
|
|
|
|
|
|
|
|
size_t questions_count;
|
|
|
|
MPMarshalledQuestion *questions;
|
|
|
|
} MPMarshalledSite;
|
|
|
|
|
|
|
|
typedef struct MPMarshalledUser {
|
2017-08-06 03:19:24 +00:00
|
|
|
const char *fullName;
|
2017-07-23 03:45:54 +00:00
|
|
|
const char *masterPassword;
|
2017-07-23 01:38:53 +00:00
|
|
|
MPAlgorithmVersion algorithm;
|
2017-07-23 03:45:54 +00:00
|
|
|
bool redacted;
|
2017-07-16 01:13:49 +00:00
|
|
|
|
|
|
|
unsigned int avatar;
|
2017-08-10 16:30:42 +00:00
|
|
|
MPResultType defaultType;
|
2017-07-16 01:13:49 +00:00
|
|
|
time_t lastUsed;
|
|
|
|
|
|
|
|
size_t sites_count;
|
|
|
|
MPMarshalledSite *sites;
|
|
|
|
} MPMarshalledUser;
|
|
|
|
|
2017-09-20 14:48:04 +00:00
|
|
|
typedef struct MPMarshalInfo {
|
|
|
|
MPMarshalFormat format;
|
2017-08-13 01:57:47 +00:00
|
|
|
MPAlgorithmVersion algorithm;
|
|
|
|
const char *fullName;
|
|
|
|
const char *keyID;
|
|
|
|
bool redacted;
|
|
|
|
time_t date;
|
2017-09-20 14:48:04 +00:00
|
|
|
} MPMarshalInfo;
|
2017-08-13 01:57:47 +00:00
|
|
|
|
2017-07-16 01:13:49 +00:00
|
|
|
//// Marshalling.
|
|
|
|
|
2017-08-13 01:57:47 +00:00
|
|
|
/** Write the user and all associated data out to the given output buffer using the given marshalling format. */
|
2017-09-22 19:20:14 +00:00
|
|
|
bool mpw_marshal_write(
|
2017-09-20 14:48:04 +00:00
|
|
|
char **out, const MPMarshalFormat outFormat, const MPMarshalledUser *user, MPMarshalError *error);
|
2017-08-13 01:57:47 +00:00
|
|
|
/** Try to read metadata on the sites in the input buffer. */
|
2017-09-22 19:20:14 +00:00
|
|
|
MPMarshalInfo *mpw_marshal_read_info(
|
2017-08-13 01:57:47 +00:00
|
|
|
const char *in);
|
|
|
|
/** Unmarshall sites in the given input buffer by parsing it using the given marshalling format. */
|
2017-09-22 19:20:14 +00:00
|
|
|
MPMarshalledUser *mpw_marshal_read(
|
2017-09-20 14:48:04 +00:00
|
|
|
const char *in, const MPMarshalFormat inFormat, const char *masterPassword, MPMarshalError *error);
|
2017-07-16 01:13:49 +00:00
|
|
|
|
|
|
|
//// Utilities.
|
|
|
|
|
2017-08-13 01:57:47 +00:00
|
|
|
/** Create a new user object ready for marshalling. */
|
2017-09-22 19:20:14 +00:00
|
|
|
MPMarshalledUser *mpw_marshal_user(
|
2017-07-23 03:45:54 +00:00
|
|
|
const char *fullName, const char *masterPassword, const MPAlgorithmVersion algorithmVersion);
|
2017-08-13 01:57:47 +00:00
|
|
|
/** Create a new site attached to the given user object, ready for marshalling. */
|
2017-09-22 19:20:14 +00:00
|
|
|
MPMarshalledSite *mpw_marshal_site(
|
2017-08-07 21:42:38 +00:00
|
|
|
MPMarshalledUser *user,
|
2017-08-10 16:30:42 +00:00
|
|
|
const char *siteName, const MPResultType resultType, const MPCounterValue siteCounter, const MPAlgorithmVersion algorithmVersion);
|
2017-08-13 01:57:47 +00:00
|
|
|
/** Create a new question attached to the given site object, ready for marshalling. */
|
2017-07-23 01:38:53 +00:00
|
|
|
MPMarshalledQuestion *mpw_marshal_question(
|
2017-08-07 21:42:38 +00:00
|
|
|
MPMarshalledSite *site, const char *keyword);
|
2017-08-13 01:57:47 +00:00
|
|
|
/** Free the given user object and all associated data. */
|
|
|
|
bool mpw_marshal_info_free(
|
2017-09-20 14:48:04 +00:00
|
|
|
MPMarshalInfo **info);
|
2017-07-23 01:38:53 +00:00
|
|
|
bool mpw_marshal_free(
|
2017-08-23 04:01:23 +00:00
|
|
|
MPMarshalledUser **user);
|
2017-07-16 01:13:49 +00:00
|
|
|
|
2017-08-06 03:19:24 +00:00
|
|
|
//// Format.
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return The purpose represented by the given name.
|
|
|
|
*/
|
2017-09-20 14:48:04 +00:00
|
|
|
const MPMarshalFormat mpw_formatWithName(
|
2017-08-06 03:19:24 +00:00
|
|
|
const char *formatName);
|
|
|
|
/**
|
|
|
|
* @return The standard name for the given purpose.
|
|
|
|
*/
|
|
|
|
const char *mpw_nameForFormat(
|
2017-09-20 14:48:04 +00:00
|
|
|
const MPMarshalFormat format);
|
2017-08-13 01:57:47 +00:00
|
|
|
/**
|
|
|
|
* @return The file extension that's recommended for files that use the given marshalling format.
|
|
|
|
*/
|
2017-09-22 19:20:14 +00:00
|
|
|
const char *mpw_marshal_format_extension(
|
2017-09-20 14:48:04 +00:00
|
|
|
const MPMarshalFormat format);
|
2017-08-06 03:19:24 +00:00
|
|
|
|
2017-09-22 19:20:14 +00:00
|
|
|
#endif // _MPW_MARSHAL_H
|