From 50357b8fac3e9f5c5edf8c93cb07faa8122fd155 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Sun, 22 Dec 2019 19:33:38 +1300 Subject: Make some dense code a little less opaque --- wtf8.c | 9 +++++++-- wtf8.h | 3 +++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/wtf8.c b/wtf8.c index 714a7cc..7078ec1 100644 --- a/wtf8.c +++ b/wtf8.c @@ -18,8 +18,13 @@ void print_octets(char *s) { /* * Iterate through the string, printing each octet, ending with a newline */ - while ((c = *s++)) - printf("%c%02x", (is_utf8_cont(c) ? '-' : ' '), c); + while ((c = *s++)) { + char sep; + sep = is_utf8_cont(c) + ? BYTE_SEP + : CHAR_SEP; + printf("%c%02x", sep, c); + } putchar('\n'); return; diff --git a/wtf8.h b/wtf8.h index a635e1e..f6f3de6 100644 --- a/wtf8.h +++ b/wtf8.h @@ -2,6 +2,9 @@ #include #include +#define BYTE_SEP '-' +#define CHAR_SEP ' ' + int is_utf8_cont(unsigned char); void print_octets(char *); void print_characters(char *); -- cgit v1.2.3 From 117aec772e1645e702db37d94e73cf0200d8ab12 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Sun, 22 Dec 2019 19:49:56 +1300 Subject: Refactor for legibility --- wtf8.c | 63 +++++++++++++++++++++++++++++++++++++++------------------------ wtf8.h | 4 ++-- 2 files changed, 41 insertions(+), 26 deletions(-) diff --git a/wtf8.c b/wtf8.c index 7078ec1..ea8950a 100644 --- a/wtf8.c +++ b/wtf8.c @@ -1,31 +1,31 @@ #include "wtf8.h" /* - * Check if first two bits of the character are "10", meaning it's a UTF-8 + * Check if first two bits of the character are "10", meaning it'str a UTF-8 * continuation character */ -int is_utf8_cont(unsigned char c) { - return (c & 0xC0) == 0x80; +int is_utf8_cont(unsigned char chr) { + return (chr & 0xC0) == 0x80; } /* * Print each octet of a string of characters as lowercase hex followed by a * trailing space, ending with a newline */ -void print_octets(char *s) { - unsigned char c; +void print_octets(FILE *stream, char *str) { + unsigned char chr; /* * Iterate through the string, printing each octet, ending with a newline */ - while ((c = *s++)) { + while ((chr = *str++)) { char sep; - sep = is_utf8_cont(c) + sep = is_utf8_cont(chr) ? BYTE_SEP : CHAR_SEP; - printf("%c%02x", sep, c); + fprintf(stream, "%c%02x", sep, chr); } - putchar('\n'); + fputc('\n', stream); return; } @@ -35,37 +35,52 @@ void print_octets(char *s) { * print_octets(), with each character in line with the end of the octet that * terminates it, ending with a newline */ -void print_characters(char *s) { - - /* - * We need a short counter to find how long each character is - */ - unsigned char c; +void print_characters(FILE *stream, char *str) { /* * Iterate through the string */ - while (*s) { + while (*str) { + + /* + * We need a short counter to find how long each character is + */ + unsigned char chr; /* * Print blanks and increment a counter until we find how long this * character is */ - for (c = 1; is_utf8_cont(s[c]) && c <= UCHAR_MAX; c++) - printf(" "); + for (chr = 1; is_utf8_cont(str[chr]); chr++) { + + /* + * Print blanks + */ + fprintf(stream, " "); + + /* + * If we've hit UCHAR_MAX, this is probably a perverse + * string of bytes for fuzzing or exploitation; bail + * out + */ + if (chr == UCHAR_MAX) { + fprintf(stderr, "Perverse byte count, bailing\n"); + exit(1); + } + } /* * Print two spaces, and then the full character */ - printf(" "); - while (c--) - putchar(*s++); + fprintf(stream, " "); + while (chr--) + fputc(*str++, stream); } /* * End with a newline */ - putchar('\n'); + fputc('\n', stream); return; } @@ -84,8 +99,8 @@ int main(int argc, char **argv) { * Print the sole argument first as hex octets, then as characters, spaced * accordingly */ - print_octets(argv[1]); - print_characters(argv[1]); + print_octets(stdout, argv[1]); + print_characters(stdout, argv[1]); /* * Done! diff --git a/wtf8.h b/wtf8.h index f6f3de6..e8a40f4 100644 --- a/wtf8.h +++ b/wtf8.h @@ -6,5 +6,5 @@ #define CHAR_SEP ' ' int is_utf8_cont(unsigned char); -void print_octets(char *); -void print_characters(char *); +void print_octets(FILE *, char *); +void print_characters(FILE *, char *); -- cgit v1.2.3 From 927560e25d6424ef134c85138b08a96e847d2841 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Mon, 23 Dec 2019 19:52:23 +1300 Subject: Add help message for improper usage --- wtf8.c | 4 +++- wtf8.h | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/wtf8.c b/wtf8.c index ea8950a..1b99d63 100644 --- a/wtf8.c +++ b/wtf8.c @@ -92,8 +92,10 @@ int main(int argc, char **argv) { /* * Check we have one and only one argument */ - if (argc != 2) + if (argc != 2) { + fprintf(stderr, "%s: Need one argument\n", PROGRAM_NAME); exit(EXIT_FAILURE); + } /* * Print the sole argument first as hex octets, then as characters, spaced diff --git a/wtf8.h b/wtf8.h index e8a40f4..f02e31b 100644 --- a/wtf8.h +++ b/wtf8.h @@ -2,6 +2,8 @@ #include #include +#define PROGRAM_NAME "wtf8" + #define BYTE_SEP '-' #define CHAR_SEP ' ' -- cgit v1.2.3 From 802a72aa0617bda058b893ca864f9150f42feaf5 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Mon, 23 Dec 2019 19:53:40 +1300 Subject: Bump VERSION --- VERSION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VERSION b/VERSION index 6085e94..f0bb29e 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.2.1 +1.3.0 -- cgit v1.2.3