From a9fe5fb76c03befa1b3df10a429f735f06e83c2c Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Tue, 24 Mar 2015 18:03:11 +0100 Subject: cleanup and add defs: PASSPHRASE_STAR_CHAR, PASSPHRASE_TEXT, PASSPHRASE_TEXT_EMPTY, PASSPHRASE_TEXT_NOT_EMPTY MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- Makefile | 28 ++-- src/passphrase.c | 423 +++++++++++++++++------------------------------- src/passphrase_helper.h | 313 +++++++++++++++++++++++++++++++++++ test-all-options.sh | 25 ++- 4 files changed, 489 insertions(+), 300 deletions(-) create mode 100644 src/passphrase_helper.h diff --git a/Makefile b/Makefile index 32fd7b2..8b233a6 100644 --- a/Makefile +++ b/Makefile @@ -30,24 +30,24 @@ PKGNAME ?= libpassphrase # Options with which to compile the library OPTIONS ?= -# PASSPHRASE_ECHO: Do not hide the passphrase -# PASSPHRASE_STAR: Use '*' for each character instead of no echo -# PASSPHRASE_TEXT: Use '(empty)' and '(not empty)' instead of no echo -# PASSPHRASE_REALLOC: Soften security by using `realloc` -# PASSPHRASE_MOVE: Enable move of point -# PASSPHRASE_INSERT: Enable insert mode -# PASSPHRASE_OVERRIDE: Enable override mode -# PASSPHRASE_DELETE: Enable reversed erase command -# PASSPHRASE_CONTROL: Enable use of control key combinations -# PASSPHRASE_DEDICATED: Enable use of dedicated keys -# DEFAULT_INSERT: Use insert mode as default -# PASSPHRASE_INVALID: Prevent duplication of non-initialised memory +# PASSPHRASE_ECHO: Do not hide the passphrase +# PASSPHRASE_STAR: Use '*' for each character instead of no echo +# PASSPHRASE_TEXT: Use '(empty)' and '(not empty)' instead of no echo +# PASSPHRASE_REALLOC: Soften security by using `realloc` +# PASSPHRASE_MOVE: Enable move of point +# PASSPHRASE_INSERT: Enable insert mode +# PASSPHRASE_OVERRIDE: Enable override mode +# PASSPHRASE_DELETE: Enable reversed erase command +# PASSPHRASE_CONTROL: Enable use of control key combinations +# PASSPHRASE_DEDICATED: Enable use of dedicated keys +# DEFAULT_INSERT: Use insert mode as default +# PASSPHRASE_INVALID: Prevent duplication of non-initialised memory # Optimisation settings for C code compilation OPTIMISE ?= -Os # Warnings settings for C code compilation -WARN = -Wall -Wextra -pedantic -Wdouble-promotion -Wformat=2 -Winit-self -Wmissing-include-dirs \ +WARN = -Wall -Wextra -Wdouble-promotion -Wformat=2 -Winit-self -Wmissing-include-dirs \ -Wtrampolines -Wfloat-equal -Wshadow -Wmissing-prototypes -Wmissing-declarations \ -Wredundant-decls -Wnested-externs -Winline -Wno-variadic-macros -Wsync-nand \ -Wunsafe-loop-optimizations -Wcast-align -Wstrict-overflow -Wdeclaration-after-statement \ @@ -109,7 +109,7 @@ bin/libpassphrase.a: obj/passphrase.o bin/test: bin/libpassphrase.so obj/test.o $(CC) $(LD_FLAGS) -Lbin -lpassphrase -o "$@" obj/test.o $(LDFLAGS) -obj/passphrase.o: src/passphrase.c src/passphrase.h +obj/passphrase.o: src/passphrase.c src/passphrase.h src/passphrase_helper.h @mkdir -p "$(shell dirname "$@")" $(CC) $(CC_FLAGS) -fPIC -o "$@" -c "$<" $(CFLAGS) $(CPPFLAGS) diff --git a/src/passphrase.c b/src/passphrase.c index 3fbdb68..19e3f32 100644 --- a/src/passphrase.c +++ b/src/passphrase.c @@ -23,17 +23,21 @@ #include #include "passphrase.h" +#include "passphrase_helper.h" #define START_PASSPHRASE_LIMIT 32 + + #if !defined(PASSPHRASE_ECHO) || defined(PASSPHRASE_MOVE) /** * The original TTY settings */ static struct termios saved_stty; -#endif +#endif /* !PASSPHRASE_ECHO || PASSPHRASE_MOVE */ + #ifndef PASSPHRASE_REALLOC @@ -48,31 +52,55 @@ static char* xrealloc(char* array, size_t cur_size, size_t new_size) free(array); return rc; } -#else -#define xrealloc(array, _cur_size, new_size) realloc(array, (new_size) * sizeof(char)) -#endif +#else /* !PASSPHRASE_REALLOC */ +# define xrealloc(array, _cur_size, new_size) realloc(array, (new_size) * sizeof(char)) +#endif /* !PASSPHRASE_REALLOC */ -#if defined(PASSPHRASE_MOVE) && !defined(PASSPHRASE_STAR) && !defined(PASSPHRASE_ECHO) -# define xprintf(...) /* do nothing */ -# define xflush() /* do nothing */ -#elif defined(PASSPHRASE_MOVE) || defined(PASSPHRASE_STAR) -# define xprintf(...) fprintf(stderr, __VA_ARGS__) -# define xflush() fflush(stderr) -#else -# define xflush() fflush(stderr) -#endif + +#if defined(PASSPHRASE_DEDICATED) && defined(PASSPHRASE_MOVE) +static int get_dedicated_control_key(void) +{ + int c = getchar(); + if (c == 'O') + { + c = getchar(); + if (c == 'H') return KEY_HOME; + if (c == 'F') return KEY_END; + } + else if (c == '[') + { + c = getchar(); + if (c == 'C') return KEY_RIGHT; + if (c == 'D') return KEY_LEFT; + if (('1' <= c) && (c <= '4') && (getchar() == '~')) + return -(c - '0'); + } + return 0; +} +#endif /* PASSPHRASE_DEDICATED && PASSPHRASE_MOVE */ + #ifdef PASSPHRASE_MOVE -# if defined(PASSPHRASE_STAR) -# define xputchar(C) ({ if ((C & 0xC0) != 0x80) fputc('*', stderr); }) -# elif defined(PASSPHRASE_ECHO) -# define xputchar(C) fputc(C, stderr) -# else -# define xputchar(C) ({ /* be silent */ }) -# endif -#endif +static int get_key(int c) +{ +#ifdef PASSPHRASE_DEDICATED + if (c == '\033') return get_dedicated_control_key(); +#endif /* PASSPHRASE_DEDICATED */ + if ((c == 8) || (c == 127)) return KEY_ERASE; + if ((c < 0) || (c >= ' ')) return ((int)c) & 255; +#ifdef PASSPHRASE_CONTROL + if (c == 'A' - '@') return KEY_HOME; + if (c == 'B' - '@') return KEY_LEFT; + if (c == 'D' - '@') return KEY_DELETE; + if (c == 'E' - '@') return KEY_END; + if (c == 'F' - '@') return KEY_RIGHT; +#endif /* PASSPHRASE_CONTROL */ + return 0; +} +#endif /* PASSPHRASE_MOVE */ + /** @@ -83,266 +111,117 @@ static char* xrealloc(char* array, size_t cur_size, size_t new_size) char* passphrase_read(void) { char* rc = malloc(START_PASSPHRASE_LIMIT * sizeof(char)); - long size = START_PASSPHRASE_LIMIT; - long len = 0; + size_t size = START_PASSPHRASE_LIMIT; + size_t len = 0; #ifdef PASSPHRASE_MOVE - long point = 0; - long i = 0; -# ifdef PASSPHRASE_OVERRIDE -# if defined(PASSPHRASE_INSERT) && defined(DEFAULT_INSERT) - char insert = 1; -# elif defined(PASSPHRASE_INSERT) - char insert = 0; -# endif -# endif -#endif + size_t point = 0; + size_t i = 0; +# if defined(PASSPHRASE_OVERRIDE) && defined(PASSPHRASE_INSERT) + char insert = DEFAULT_INSERT_VALUE; +# endif /* PASSPHRASE_OVERRIDE && PASSPHRASE_INSERT */ +#endif /* PASSPHRASE_MOVE */ +#ifdef PASSPHRASE_TEXT + size_t printed_len = 0; +#endif /* PASSPHRASE_TEXT */ int c; +#ifdef PASSPHRASE_MOVE + int cc; +#endif if (rc == NULL) return NULL; +#ifdef PASSPHRASE_TEXT + xprintf("%s%zn", PASSPHRASE_TEXT_EMPTY, &printed_len); + if (printed_len) + xprintf("\e[%zuD", printed_len); +#endif /* PASSPHRASE_TEXT */ + /* Read password until EOF or Enter, skip all \0 as that is probably not a part of the passphrase (good luck typing that in X.org) and can be echoed into stdin by the kernel. */ for (;;) { c = getchar(); - if ((c < 0) || (c == '\n')) - break; - if (c != 0) - { + if ((c < 0) || (c == '\n')) break; + if (c == 0) continue; + #if defined(PASSPHRASE_MOVE) - /* \e[1~ \eOH ^A -1 home */ - /* \e[2~ -2 insert */ - /* \e[3~ ^D -3 delete */ - /* \e[4~ \eOF ^E -4 end */ - /* \8 \127 -5 erase */ - /* \e[C ^F -6 right */ - /* \e[D ^B -7 left */ - int cc = 0; -#ifdef PASSPHRASE_DEDICATED - if (c == '\033') - { - c = getchar(); - if (c == 'O') - { - c = getchar(); - if (c == 'H') cc = -1; - else if (c == 'F') cc = -4; - } - else if (c == '[') - { - c = getchar(); - if (c == 'C') cc = -6; - else if (c == 'D') cc = -7; - else if (('1' <= c) && (c <= '4') && (getchar() == '~')) - cc = -(c - '0'); - } - } + cc = get_key(c); + if (cc > 0) + { + c = (char)cc; + if (point == len) + append_char(); +# ifdef PASSPHRASE_INSERT else -#endif - if ((c == 8) || (c == 127)) - cc = -5; - else if ((c < 0) || (c >= ' ')) - cc = ((int)c) & 255; -#ifdef PASSPHRASE_CONTROL - else if (c == 'A' - '@') cc = -1; - else if (c == 'B' - '@') cc = -7; - else if (c == 'D' - '@') cc = -3; - else if (c == 'E' - '@') cc = -4; - else if (c == 'F' - '@') cc = -6; -#endif - - if (cc > 0) - { - c = (char)cc; - if (point == len) - { -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wpedantic" - xputchar(c); -#pragma GCC diagnostic pop - *(rc + len++) = (char)c; - point++; - } -#ifdef PASSPHRASE_INSERT - else -#ifdef PASSPHRASE_OVERRIDE - if (insert) -#endif - { - if ((c & 0xC0) != 0x80) - { xprintf("\033[@"); } -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wpedantic" - xputchar(c); -#pragma GCC diagnostic pop - for (i = len; i > point; i--) - *(rc + i) = *(rc + i - 1); - len++; - *(rc + point++) = (char)c; - } -#endif -#ifdef PASSPHRASE_OVERRIDE - else - { - long n = 1; - char cn = (char)c; - while ((*(rc + point + n) & 0xC0) == 0x80) - n++; - for (i = point + n; i < len; i++) - *(rc + i - n) = *(rc + i); - passphrase_wipe(rc + len - n, (size_t)n); - len -= n; - n = 0; - while (cn & 0x80) - { - cn = (char)(cn << 1); - n++; - } - n = n ? n : 1; - if (len + n > size) - { - if ((rc = xrealloc(rc, (size_t)size, (size_t)size << 1L)) == NULL) - return NULL; - size <<= 1L; - } - len += n; - for (i = len - 1; i > point + n; i--) - *(rc + i) = *(rc + i - n); - if (len - 1 >= point + n) - *(rc + point + n) = *(rc + point); - for (i = 0; i < n; i++) - { - if (i) - c = getchar(); -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wpedantic" - xputchar(c); -#pragma GCC diagnostic pop - *(rc + point++) = (char)c; - } - } -#endif - } - else if ((cc == -1) && point) /* home */ - { - long n = 0; - for (i = 0; i < point; i++) - if ((*(rc + i) & 0xC0) != 0x80) - n++; - xprintf("\033[%liD", n); - point = 0; - } -#if defined(PASSPHRASE_INSERT) && defined(PASSPHRASE_OVERRIDE) - else if (cc == -2) /* insert */ - insert ^= 1; -#endif -#ifdef PASSPHRASE_DELETE - else if ((cc == -3) && (len != point)) /* delete */ - { - xprintf("\033[P"); -#ifdef PASSPHRASE_INVALID - *(rc + len) = 0; -#endif - do - { - for (i = point; i < len; i++) - *(rc + i) = *(rc + i + 1); - len--; - } - while ((len != point) && ((*(rc + point) & 0xC0) == 0x80)); - } -#endif - else if ((cc == -4) && (len != point)) /* end */ - { - long n = 0; - for (i = point; i < len; i++) - if ((*(rc + i) & 0xC0) != 0x80) - n++; - xprintf("\033[%liC", n); - point = len; - } - else if ((cc == -5) && point) /* erase */ - { - char redo = 1; - xprintf("\033[D\033[P"); -#ifdef PASSPHRASE_INVALID - *(rc + len) = 0; -#endif - while (redo) - { - redo = (*(rc + point - 1) & 0xC0) == 0x80; - for (i = point; i < len; i++) - *(rc + i - 1) = *(rc + i); - if (point <= len) - *(rc + len - 1) = *(rc + len); - point--; - len--; - } - } - else if ((cc == -6) && (len != point)) /* right */ - { - xprintf("\033[C"); - do - point++; - while ((len != point) && ((*(rc + point) & 0xC0) == 0x80)); - } - else if ((cc == -7) && point) /* left */ - { - xprintf("\033[D"); - point--; - while (point && ((*(rc + point) & 0xC0) == 0x80)) - point--; - } - -#elif defined(PASSPHRASE_STAR) - if ((c == 8) || (c == 127)) - { - if (len == 0) - continue; - xprintf("\033[D \033[D"); - xflush(); - *(rc + --len) = 0; -#ifdef DEBUG - goto debug; -#else - continue; -#endif - } - if ((c & 0xC0) != 0x80) - fputc('*', stderr); - *(rc + len++) = (char)c; -#else - *(rc + len++) = (char)c; -#endif - - xflush(); - if (len == size) - { - if ((rc = xrealloc(rc, (size_t)size, (size_t)size << 1)) == NULL) - return NULL; - size <<= 1L; - } - -#ifdef DEBUG -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wunused-label" - debug: - { - long n = 0; - for (i = point; i < len; i++) - if ((*(rc + i) & 0xC0) != 0x80) - n++; - *(rc + len) = 0; - if (n) - fprintf(stderr, "\033[s\033[H\033[K%s\033[%liD\033[01;34m%s\033[00m\033[u", rc, n, rc + point); +# ifdef PASSPHRASE_OVERRIDE + if (insert) +# endif /* PASSPHRASE_OVERRIDE */ + insert_char(); +# endif /* PASSPHRASE_INSERT */ +# ifdef PASSPHRASE_OVERRIDE else - fprintf(stderr, "\033[s\033[H\033[K%s\033[01;34m%s\033[00m\033[u", rc, rc + point); - fflush(stderr); - } -#endif + override_char(); +# endif /* PASSPHRASE_OVERRIDE */ + } +# if defined(PASSPHRASE_INSERT) && defined(PASSPHRASE_OVERRIDE) + else if (cc == KEY_INSERT) insert ^= 1; +# endif /* PASSPHRASE_INSERT && PASSPHRASE_OVERRIDE */ +# ifdef PASSPHRASE_DELETE + else if ((cc == KEY_DELETE) && (len != point)) delete_next(), print_delete(); +# endif /* PASSPHRASE_DELETE */ + else if ((cc == KEY_ERASE) && point) erase_prev(), print_erase(); + else if ((cc == KEY_HOME) && (point != 0)) move_home(); + else if ((cc == KEY_END) && (point != len)) move_end(); + else if ((cc == KEY_RIGHT) && (point != len)) move_right(); + else if ((cc == KEY_LEFT) && (point != 0)) move_left(); + +#elif defined(PASSPHRASE_STAR) || defined(PASSPHRASE_TEXT) /* PASSPHRASE_MOVE */ + if ((c == 8) || (c == 127)) + { + if (len == 0) + continue; + print_erase(); + xflush(); + erase_prev(); +# ifdef DEBUG + goto debug; +# else /* DEBUG */ + continue; +# endif /* DEBUG */ } + append_char(); + +#else /* PASSPHRASE_MOVE, PASSPHRASE_STAR || PASSPHRASE_TEXT */ + append_char(); +#endif /* PASSPHRASE_MOVE, PASSPHRASE_STAR || PASSPHRASE_TEXT */ + + xflush(); + if (len == size) + { + if ((rc = xrealloc(rc, (size_t)size, (size_t)size << 1)) == NULL) + return NULL; + size <<= 1L; + } + +#ifdef DEBUG +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wunused-label" + debug: + { + size_t n = 0; + for (i = point; i < len; i++) + if ((*(rc + i) & 0xC0) != 0x80) + n++; + *(rc + len) = 0; + if (n) + fprintf(stderr, "\033[s\033[H\033[K%s\033[%zuD\033[01;34m%s\033[00m\033[u", rc, n, rc + point); + else + fprintf(stderr, "\033[s\033[H\033[K%s\033[01;34m%s\033[00m\033[u", rc, rc + point); + fflush(stderr); + } +#endif /* DEBUG */ } /* NUL-terminate passphrase */ @@ -350,14 +229,14 @@ char* passphrase_read(void) #if !defined(PASSPHRASE_ECHO) || defined(PASSPHRASE_MOVE) fprintf(stderr, "\n"); -#endif +#endif /* !PASSPHRASE_ECHO || PASSPHRASE_MOVE */ return rc; } # pragma GCC diagnostic push -# pragma GCC diagnostic ignored "-Wsuggest-attribute=const" -# pragma GCC diagnostic ignored "-Wsuggest-attribute=pure" +# pragma GCC diagnostic ignored "-Wsuggest-attribute=const" +# pragma GCC diagnostic ignored "-Wsuggest-attribute=pure" /* Must positively absolutely not be flagged as possible to optimise away as it depends on configurations, and programs that uses this library must not be forced to be recompiled if the library is reconfigured. */ @@ -391,11 +270,11 @@ void passphrase_disable_echo(void) tcgetattr(STDIN_FILENO, &stty); saved_stty = stty; stty.c_lflag &= (tcflag_t)~ECHO; -#if defined(PASSPHRASE_STAR) || defined(PASSPHRASE_MOVE) +# if defined(PASSPHRASE_STAR) || defined(PASSPHRASE_TEXT) || defined(PASSPHRASE_MOVE) stty.c_lflag &= (tcflag_t)~ICANON; -#endif +# endif /* PASSPHRASE_STAR || PASSPHRASE_TEXT || PASSPHRASE_MOVE */ tcsetattr(STDIN_FILENO, TCSAFLUSH, &stty); -#endif +#endif /* !PASSPHRASE_ECHO || PASSPHRASE_MOVE */ } @@ -406,7 +285,7 @@ void passphrase_reenable_echo(void) { #if !defined(PASSPHRASE_ECHO) || defined(PASSPHRASE_MOVE) tcsetattr(STDIN_FILENO, TCSAFLUSH, &saved_stty); -#endif +#endif /* !PASSPHRASE_ECHO || !PASSPHRASE_MOVE */ } # pragma GCC diagnostic pop diff --git a/src/passphrase_helper.h b/src/passphrase_helper.h new file mode 100644 index 0000000..7ec57a8 --- /dev/null +++ b/src/passphrase_helper.h @@ -0,0 +1,313 @@ +/** + * libpassphrase – Personalisable library for TTY passphrase reading + * + * Copyright © 2013, 2014, 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program 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. + * + * This program 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 should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#ifndef PASSPHRASE_HELPER_H +#define PASSPHRASE_HELPER_H + + +/* Fix conflicting configurations */ +#if defined(PASSPHRASE_TEXT) && defined(PASSPHRASE_STAR) +# warning You cannot have both PASSPHRASE_TEXT and PASSPHRASE_STAR +# undef PASSPHRASE_TEXT +#endif + + +/* Default texts */ +#ifndef PASSPHRASE_STAR_CHAR +# define PASSPHRASE_STAR_CHAR "*" /* TODO document */ +#endif +#ifndef PASSPHRASE_TEXT_EMPTY +# define PASSPHRASE_TEXT_EMPTY "(empty)" /* TODO document */ +#endif +#ifndef PASSPHRASE_TEXT_NOT_EMPTY +# define PASSPHRASE_TEXT_NOT_EMPTY "(not empty)" /* TODO document */ +#endif + + + +/* Control keys. */ + +/** + * Home-key. + * Character sequences: \e[1~ \eOH + * Control-key combination: ^A + */ +#define KEY_HOME -1 + +/** + * Insert-key. + * Character sequences: \e[2~ + */ +#define KEY_INSERT -2 + +/** + * Delete-key. + * Character sequences: \e[3~ + * Control-key combination: ^D + */ +#define KEY_DELETE -3 + +/** + * End-key. + * Character sequences: \e[4~ \eOF + * Control-key combination: ^E + */ +#define KEY_END -4 + +/** + * Erase-key, also known as backspace. + * Character sequences: \d8 \d127 + */ +#define KEY_ERASE -5 + +/** + * Right-key. + * Character sequences: \e[C + * Control-key combination: ^F + */ +#define KEY_RIGHT -6 + +/** + * Left-key. + * Character sequences: \e[D + * Control-key combination: ^B + */ +#define KEY_LEFT -7 + + + +/* Custom fflush and fprintf */ +#if defined(PASSPHRASE_STAR) || defined(PASSPHRASE_TEXT) +# define xprintf(...) fprintf(stderr, __VA_ARGS__) +# define xflush() fflush(stderr) +#elif defined(PASSPHRASE_MOVE) && !defined(PASSPHRASE_ECHO) +# define xprintf(...) ({ /* do nothing */ }) +# define xflush() ({ /* do nothing */ }) +#elif defined(PASSPHRASE_MOVE) +# define xprintf(...) fprintf(stderr, __VA_ARGS__) +# define xflush() fflush(stderr) +#else +# define xflush() fflush(stderr) +#endif + + + +/* Custom putchar */ +#if defined(PASSPHRASE_STAR) +# define xputchar(C) (((C & 0xC0) != 0x80) ? fprintf(stderr, "%s", PASSPHRASE_STAR_CHAR) : 0) +#elif defined(PASSPHRASE_ECHO) && defined(PASSPHRASE_MOVE) +# define xputchar(C) fputc(C, stderr) +#else +# define xputchar(C) ({ /* be silent */ }) +#endif + + + +/* Is insert active by default? */ +#if defined(PASSPHRASE_OVERRIDE) && defined(PASSPHRASE_INSERT) +# if defined(DEFAULT_INSERT) +# define DEFAULT_INSERT_VALUE 1 +# else +# define DEFAULT_INSERT_VALUE 0 +# endif +#endif + + + +/* PASSPHRASE_INVALID's affect */ +#if defined(PASSPHRASE_INVALID) +# define null_terminate() (*(rc + len) = 0) +#else +# define null_terminate() ({ /* do nothing*/ }) +#endif + + + +/* Implementation of the right-key's action */ +#define move_right() \ + ({ \ + xprintf("\033[C"); \ + do \ + point++; \ + while ((len != point) && ((*(rc + point) & 0xC0) == 0x80)); \ + }) + + +/* Implementation of the left-key's action */ +#define move_left() \ + ({ \ + xprintf("\033[D"); \ + point--; \ + while (point && ((*(rc + point) & 0xC0) == 0x80)) \ + point--; \ + }) + + +/* Implementation of the home-key's action */ +#define move_home() \ + ({ \ + size_t n = 0; \ + for (i = 0; i < point; i++) \ + if ((*(rc + i) & 0xC0) != 0x80) \ + n++; \ + xprintf("\033[%zuD", n); \ + point = 0; \ + }) + + +/* Implementation of the end-key's action */ +#define move_end() \ + ({ \ + size_t n = 0; \ + for (i = point; i < len; i++) \ + if ((*(rc + i) & 0xC0) != 0x80) \ + n++; \ + xprintf("\033[%zuC", n); \ + point = len; \ + }) + + +/* Implementation of the delete-key's action upon the passphrase buffer */ +#define delete_next() \ + ({ \ + null_terminate(); \ + do \ + { \ + for (i = point; i < len; i++) \ + *(rc + i) = *(rc + i + 1); \ + len--; \ + } \ + while ((len != point) && ((*(rc + point) & 0xC0) == 0x80)); \ + }) + + +/* Implementation of the erase-key's action upon the passphrase buffer */ +#if defined(PASSPHRASE_MOVE) +#define erase_prev() \ + ({ \ + char redo = 1; \ + null_terminate(); \ + while (redo) \ + { \ + redo = (*(rc + point - 1) & 0xC0) == 0x80; \ + for (i = point; i < len; i++) \ + *(rc + i - 1) = *(rc + i); \ + if (point <= len) \ + *(rc + len - 1) = *(rc + len); \ + point--; \ + len--; \ + } \ + }) +#else +# define erase_prev() (*(rc + --len) = 0) +#endif + + +#if defined(PASSPHRASE_MOVE) +# define append_char() (xputchar(c), *(rc + len++) = (char)c, point++) +#elif defined(PASSPHRASE_TEXT) +# define append_char() \ + ({ \ + if (len == 0) \ + { \ + xprintf("\e[K%s%zn", PASSPHRASE_TEXT_NOT_EMPTY, &printed_len); \ + if (printed_len) \ + xprintf("\e[%zuD", printed_len); \ + } \ + *(rc + len++) = (char)c; \ + }) +#else +# define append_char() (xputchar(c), *(rc + len++) = (char)c) +#endif + + +#define insert_char() \ + ({ \ + if ((c & 0xC0) != 0x80) \ + xprintf("\033[@"); \ + xputchar(c); \ + for (i = len; i > point; i--) \ + *(rc + i) = *(rc + i - 1); \ + len++; \ + *(rc + point++) = (char)c; \ + }) + + +#define override_char() \ + ({ \ + size_t n = 1; \ + char cn = (char)c; \ + while ((*(rc + point + n) & 0xC0) == 0x80) \ + n++; \ + for (i = point + n; i < len; i++) \ + *(rc + i - n) = *(rc + i); \ + passphrase_wipe(rc + len - n, n); \ + len -= n; \ + n = 0; \ + while (cn & 0x80) \ + { \ + cn = (char)(cn << 1); \ + n++; \ + } \ + n = n ?: 1; \ + if (len + n > size) \ + { \ + if ((rc = xrealloc(rc, size, size << 1L)) == NULL) \ + return NULL; \ + size <<= 1L; \ + } \ + len += n; \ + for (i = len - 1; i > point + n; i--) \ + *(rc + i) = *(rc + i - n); \ + if (len - 1 >= point + n) \ + *(rc + point + n) = *(rc + point); \ + for (i = 0; i < n; i++) \ + { \ + if (i) \ + c = getchar(); \ + xputchar(c); \ + *(rc + point++) = (char)c; \ + } \ + }) + + +/* Implementation of the delete-key's action upon the display */ +#if defined(PASSPHRASE_TEXT) +# define print_erase() \ + (len == 0 ? 0 : (xprintf("\e[K%s%zn", PASSPHRASE_TEXT_EMPTY, &printed_len), \ + (printed_len ? xprintf("\e[%zuD", printed_len) : 0))) +#else +# define print_delete() xprintf("\033[P") +#endif + + +/* Implementation of the erase-key's action upon the display */ +#if defined(PASSPHRASE_TEXT) +# define print_erase() \ + (len == 0 ? 0 : (xprintf("\e[K%s%zn", PASSPHRASE_TEXT_EMPTY, &printed_len), \ + (printed_len ? xprintf("\e[%zuD", printed_len) : 0))) +#elif defined(PASSPHRASE_MOVE) +# define print_erase() xprintf("\033[D\033[P") +#elif defined(PASSPHRASE_STAR) +# define print_erase() xprintf("\033[D \033[D") +#endif + + + +#endif + diff --git a/test-all-options.sh b/test-all-options.sh index 039ba83..657858a 100755 --- a/test-all-options.sh +++ b/test-all-options.sh @@ -1,20 +1,16 @@ #!/bin/bash for a in PASSPHRASE_ECHO ""; do - for b in PASSPHRASE_STAR ""; do - for c in PASSPHRASE_TEXT ""; do - for d in PASSPHRASE_REALLOC ""; do - for e in PASSPHRASE_MOVE ""; do - for f in PASSPHRASE_INSERT ""; do - for g in PASSPHRASE_OVERRIDE ""; do - for h in PASSPHRASE_DELETE ""; do - for i in PASSPHRASE_CONTROL ""; do - for j in PASSPHRASE_DEDICATED ""; do - for k in DEFAULT_INSERT ""; do - for l in PASSPHRASE_INVALID ""; do - make libpassphrase -B OPTIONS="$a $b $c $d $e $f $g $h $i $j $k $l" || exit 1 - done - done + for b in PASSPHRASE_STAR PASSPHRASE_TEXT ""; do + for c in PASSPHRASE_REALLOC ""; do + for d in PASSPHRASE_MOVE ""; do + for e in PASSPHRASE_INSERT ""; do + for f in PASSPHRASE_OVERRIDE ""; do + for g in PASSPHRASE_DELETE ""; do + for h in PASSPHRASE_CONTROL ""; do + for i in PASSPHRASE_DEDICATED ""; do + for j in DEFAULT_INSERT ""; do + make libpassphrase -B OPTIONS="$a $b $c $d $e $f $g $h $i $j $k" || exit 1 done done done @@ -25,4 +21,5 @@ for a in PASSPHRASE_ECHO ""; do done done done +done -- cgit v1.2.3-70-g09d2