Refactoring and fix up mpw_color.
This commit is contained in:
parent
2f99855cd4
commit
53eb5c8a73
@ -18,7 +18,6 @@
|
|||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <arpa/inet.h>
|
|
||||||
|
|
||||||
#include "mpw-types.h"
|
#include "mpw-types.h"
|
||||||
#include "mpw-util.h"
|
#include "mpw-util.h"
|
||||||
|
@ -18,7 +18,6 @@
|
|||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <arpa/inet.h>
|
|
||||||
|
|
||||||
#include "mpw-types.h"
|
#include "mpw-types.h"
|
||||||
#include "mpw-util.h"
|
#include "mpw-util.h"
|
||||||
|
@ -18,7 +18,6 @@
|
|||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <arpa/inet.h>
|
|
||||||
|
|
||||||
#include "mpw-types.h"
|
#include "mpw-types.h"
|
||||||
#include "mpw-util.h"
|
#include "mpw-util.h"
|
||||||
|
@ -92,6 +92,8 @@ bool mpw_push_string(uint8_t **buffer, size_t *bufferSize, const char *pushStrin
|
|||||||
|
|
||||||
bool mpw_string_push(char **string, const char *pushString) {
|
bool mpw_string_push(char **string, const char *pushString) {
|
||||||
|
|
||||||
|
if (!string || !pushString)
|
||||||
|
return false;
|
||||||
if (!*string)
|
if (!*string)
|
||||||
*string = calloc( 1, sizeof( char ) );
|
*string = calloc( 1, sizeof( char ) );
|
||||||
|
|
||||||
@ -103,10 +105,8 @@ bool mpw_string_pushf(char **string, const char *pushFormat, ...) {
|
|||||||
|
|
||||||
va_list args;
|
va_list args;
|
||||||
va_start( args, pushFormat );
|
va_start( args, pushFormat );
|
||||||
char *pushString = NULL;
|
bool success = mpw_string_push( string, mpw_vstr( pushFormat, args ) );
|
||||||
bool success = vasprintf( &pushString, pushFormat, args ) >= 0 && mpw_string_push( string, pushString );
|
|
||||||
va_end( args );
|
va_end( args );
|
||||||
mpw_free_string( &pushString );
|
|
||||||
|
|
||||||
return success;
|
return success;
|
||||||
}
|
}
|
||||||
@ -344,24 +344,31 @@ bool mpw_id_buf_equals(const char *id1, const char *id2) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static char *str_str;
|
|
||||||
|
|
||||||
const char *mpw_str(const char *format, ...) {
|
const char *mpw_str(const char *format, ...) {
|
||||||
|
|
||||||
va_list args;
|
va_list args;
|
||||||
va_start( args, format );
|
va_start( args, format );
|
||||||
vasprintf( &str_str, format, args );
|
const char *str_str = mpw_vstr( format, args );
|
||||||
va_end( args );
|
va_end( args );
|
||||||
|
|
||||||
return str_str;
|
return str_str;
|
||||||
}
|
}
|
||||||
|
|
||||||
static char **mpw_hex_buf;
|
const char *mpw_vstr(const char *format, va_list args) {
|
||||||
static unsigned int mpw_hex_buf_i;
|
|
||||||
|
// TODO: We should find a way to get rid of this shared storage medium.
|
||||||
|
// FIXME: Not thread-safe
|
||||||
|
static char *str_str;
|
||||||
|
return vasprintf( &str_str, format, args ) < 0? NULL: str_str;
|
||||||
|
}
|
||||||
|
|
||||||
const char *mpw_hex(const void *buf, size_t length) {
|
const char *mpw_hex(const void *buf, size_t length) {
|
||||||
|
|
||||||
|
// TODO: We should find a way to get rid of this shared storage medium.
|
||||||
// FIXME: Not thread-safe
|
// FIXME: Not thread-safe
|
||||||
|
static char **mpw_hex_buf;
|
||||||
|
static unsigned int mpw_hex_buf_i;
|
||||||
|
|
||||||
if (!mpw_hex_buf)
|
if (!mpw_hex_buf)
|
||||||
mpw_hex_buf = calloc( 10, sizeof( char * ) );
|
mpw_hex_buf = calloc( 10, sizeof( char * ) );
|
||||||
mpw_hex_buf_i = (mpw_hex_buf_i + 1) % 10;
|
mpw_hex_buf_i = (mpw_hex_buf_i + 1) % 10;
|
||||||
@ -383,30 +390,50 @@ const char *mpw_hex_l(uint32_t number) {
|
|||||||
return mpw_hex( &buf, sizeof( buf ) );
|
return mpw_hex( &buf, sizeof( buf ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef COLOR
|
#if MPW_COLOR
|
||||||
static int putvari;
|
static char *str_tputs;
|
||||||
static char *putvarc = NULL;
|
static int str_tputs_cursor;
|
||||||
static int termsetup;
|
static const int str_tputs_max = 256;
|
||||||
static int initputvar() {
|
|
||||||
if (!isatty(STDERR_FILENO))
|
static bool mpw_setupterm() {
|
||||||
return 0;
|
|
||||||
if (putvarc)
|
if (!isatty( STDERR_FILENO ))
|
||||||
free( putvarc );
|
return false;
|
||||||
|
|
||||||
|
static bool termsetup;
|
||||||
if (!termsetup) {
|
if (!termsetup) {
|
||||||
int status;
|
int errret;
|
||||||
if (! (termsetup = (setupterm( NULL, STDERR_FILENO, &status ) == 0 && status == 1))) {
|
if (!(termsetup = (setupterm( NULL, STDERR_FILENO, &errret ) == OK))) {
|
||||||
wrn( "Terminal doesn't support color (setupterm errno %d).\n", status );
|
wrn( "Terminal doesn't support color (setupterm errret %d).\n", errret );
|
||||||
return 0;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
putvarc=(char *)calloc(256, sizeof(char));
|
return true;
|
||||||
putvari=0;
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
static int putvar(int c) {
|
|
||||||
putvarc[putvari++]=c;
|
static int mpw_tputc(int c) {
|
||||||
return 0;
|
|
||||||
|
if (++str_tputs_cursor < str_tputs_max) {
|
||||||
|
str_tputs[str_tputs_cursor] = (char)c;
|
||||||
|
return OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ERR;
|
||||||
|
}
|
||||||
|
|
||||||
|
static char *mpw_tputs(const char *str, int affcnt) {
|
||||||
|
|
||||||
|
if (str_tputs)
|
||||||
|
mpw_free( &str_tputs, str_tputs_max );
|
||||||
|
str_tputs = calloc( str_tputs_max, sizeof( char ) );
|
||||||
|
str_tputs_cursor = -1;
|
||||||
|
|
||||||
|
char *result = tputs( str, affcnt, mpw_tputc ) == ERR? NULL: strndup( str_tputs, str_tputs_max );
|
||||||
|
if (str_tputs)
|
||||||
|
mpw_free( &str_tputs, str_tputs_max );
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@ -421,23 +448,20 @@ const char *mpw_identicon(const char *fullName, const char *masterPassword) {
|
|||||||
"♨", "♩", "♪", "♫", "⚐", "⚑", "⚔", "⚖", "⚙", "⚠", "⌘", "⏎", "✄", "✆", "✈", "✉", "✌"
|
"♨", "♩", "♪", "♫", "⚐", "⚑", "⚔", "⚖", "⚙", "⚠", "⌘", "⏎", "✄", "✆", "✈", "✉", "✌"
|
||||||
};
|
};
|
||||||
|
|
||||||
const uint8_t *identiconSeed = mpw_hash_hmac_sha256( (const uint8_t *)masterPassword, strlen( masterPassword ),
|
const uint8_t *identiconSeed = mpw_hash_hmac_sha256(
|
||||||
(const uint8_t *)fullName,
|
(const uint8_t *)masterPassword, strlen( masterPassword ),
|
||||||
strlen( fullName ) );
|
(const uint8_t *)fullName, strlen( fullName ) );
|
||||||
if (!identiconSeed)
|
if (!identiconSeed)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
char *colorString, *resetString;
|
char *colorString, *resetString;
|
||||||
#ifdef COLOR
|
#ifdef MPW_COLOR
|
||||||
if (initputvar()) {
|
if (mpw_setupterm()) {
|
||||||
uint8_t colorIdentifier = (uint8_t)(identiconSeed[4] % 7 + 1);
|
uint8_t colorIdentifier = (uint8_t)(identiconSeed[4] % 7 + 1);
|
||||||
tputs(tparm(tgetstr("AF", NULL), colorIdentifier), 1, putvar);
|
colorString = mpw_tputs( tparm( tgetstr( "AF", NULL ), colorIdentifier ), 1 );
|
||||||
colorString = calloc(strlen(putvarc) + 1, sizeof(char));
|
resetString = mpw_tputs( tgetstr( "me", NULL ), 1 );
|
||||||
strcpy(colorString, putvarc);
|
}
|
||||||
tputs(tgetstr("me", NULL), 1, putvar);
|
else
|
||||||
resetString = calloc(strlen(putvarc) + 1, sizeof(char));
|
|
||||||
strcpy(resetString, putvarc);
|
|
||||||
} else
|
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
colorString = calloc( 1, sizeof( char ) );
|
colorString = calloc( 1, sizeof( char ) );
|
||||||
@ -454,8 +478,7 @@ const char *mpw_identicon(const char *fullName, const char *masterPassword) {
|
|||||||
resetString );
|
resetString );
|
||||||
|
|
||||||
mpw_free( &identiconSeed, 32 );
|
mpw_free( &identiconSeed, 32 );
|
||||||
free( colorString );
|
mpw_free_strings( &colorString, &resetString, NULL );
|
||||||
free( resetString );
|
|
||||||
return identicon;
|
return identicon;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -181,6 +181,7 @@ uint8_t const *mpw_aes_decrypt(
|
|||||||
/** Compose a formatted string.
|
/** Compose a formatted string.
|
||||||
* @return A C-string in a reused buffer, do not free or store it. */
|
* @return A C-string in a reused buffer, do not free or store it. */
|
||||||
const char *mpw_str(const char *format, ...);
|
const char *mpw_str(const char *format, ...);
|
||||||
|
const char *mpw_vstr(const char *format, va_list args);
|
||||||
/** Encode a buffer as a string of hexadecimal characters.
|
/** Encode a buffer as a string of hexadecimal characters.
|
||||||
* @return A C-string in a reused buffer, do not free or store it. */
|
* @return A C-string in a reused buffer, do not free or store it. */
|
||||||
const char *mpw_hex(const void *buf, size_t length);
|
const char *mpw_hex(const void *buf, size_t length);
|
||||||
|
@ -3096,6 +3096,8 @@
|
|||||||
OTHER_CFLAGS = (
|
OTHER_CFLAGS = (
|
||||||
"-DHAS_SODIUM=1",
|
"-DHAS_SODIUM=1",
|
||||||
"-DHAS_CPERCIVA=0",
|
"-DHAS_CPERCIVA=0",
|
||||||
|
"-DMPW_JSON=1",
|
||||||
|
"-DMPW_COLOR=1",
|
||||||
);
|
);
|
||||||
PRODUCT_NAME = "$(TARGET_NAME)";
|
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||||
};
|
};
|
||||||
@ -3113,6 +3115,8 @@
|
|||||||
OTHER_CFLAGS = (
|
OTHER_CFLAGS = (
|
||||||
"-DHAS_SODIUM=1",
|
"-DHAS_SODIUM=1",
|
||||||
"-DHAS_CPERCIVA=0",
|
"-DHAS_CPERCIVA=0",
|
||||||
|
"-DMPW_JSON=1",
|
||||||
|
"-DMPW_COLOR=1",
|
||||||
);
|
);
|
||||||
PRODUCT_NAME = "$(TARGET_NAME)";
|
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||||
};
|
};
|
||||||
@ -3130,6 +3134,8 @@
|
|||||||
OTHER_CFLAGS = (
|
OTHER_CFLAGS = (
|
||||||
"-DHAS_SODIUM=1",
|
"-DHAS_SODIUM=1",
|
||||||
"-DHAS_CPERCIVA=0",
|
"-DHAS_CPERCIVA=0",
|
||||||
|
"-DMPW_JSON=1",
|
||||||
|
"-DMPW_COLOR=1",
|
||||||
);
|
);
|
||||||
PRODUCT_NAME = "$(TARGET_NAME)";
|
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user