From cb758b9cc3870cf5655c49ec613fd24597f65f40 Mon Sep 17 00:00:00 2001 From: Shuanglei Tao Date: Sat, 27 Mar 2021 10:27:05 +0800 Subject: [PATCH] server: use base64 function from lws --- src/server.c | 8 ++++---- src/utils.c | 29 ----------------------------- src/utils.h | 3 --- 3 files changed, 4 insertions(+), 36 deletions(-) diff --git a/src/server.c b/src/server.c index 78571d1..a35bbb8 100644 --- a/src/server.c +++ b/src/server.c @@ -350,12 +350,12 @@ int main(int argc, char **argv) { break; case 'c': if (strchr(optarg, ':') == NULL) { - fprintf(stderr, - "ttyd: invalid credential, format: username:password\n"); + fprintf(stderr, "ttyd: invalid credential, format: username:password\n"); return -1; } - server->credential = - base64_encode((const unsigned char *)optarg, strlen(optarg)); + char b64_text[256]; + lws_b64_encode_string(optarg, strlen(optarg), b64_text, sizeof(b64_text)); + server->credential = strdup(b64_text); break; case 'u': info.uid = atoi(optarg); diff --git a/src/utils.c b/src/utils.c index 17d8cca..ed31643 100644 --- a/src/utils.c +++ b/src/utils.c @@ -7,7 +7,6 @@ #include #if defined(__linux__) && !defined(__ANDROID__) -// https://github.com/karelzak/util-linux/blob/master/misc-utils/kill.c const char *sys_signame[NSIG] = { "zero", "HUP", "INT", "QUIT", "ILL", "TRAP", "ABRT", "UNUSED", "FPE", "KILL", "USR1", "SEGV", "USR2", "PIPE", "ALRM", "TERM", "STKFLT", "CHLD", @@ -17,7 +16,6 @@ const char *sys_signame[NSIG] = { #if defined(_WIN32) || defined(__CYGWIN__) #include -// https://github.com/mirror/newlib-cygwin/blob/master/winsup/cygwin/strsig.cc #undef NSIG #define NSIG 33 const char *sys_signame[NSIG] = { @@ -88,32 +86,6 @@ int open_uri(char *uri) { #endif } -// https://github.com/darkk/redsocks/blob/master/base64.c -char *base64_encode(const unsigned char *buffer, size_t length) { - static const char b64[] = - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; - char *ret, *dst; - unsigned i_bits = 0; - int i_shift = 0; - int bytes_remaining = (int)length; - - ret = dst = xmalloc((size_t)(((length + 2) / 3 * 4) + 1)); - while (bytes_remaining) { - i_bits = (i_bits << 8) + *buffer++; - bytes_remaining--; - i_shift += 8; - - do { - *dst++ = b64[(i_bits << 6 >> i_shift) & 0x3f]; - i_shift -= 6; - } while (i_shift > 6 || (bytes_remaining == 0 && i_shift > 0)); - } - while ((dst - ret) & 3) *dst++ = '='; - *dst = '\0'; - - return ret; -} - #ifdef _WIN32 char *strsep(char **sp, char *sep) { char *p, *s; @@ -125,7 +97,6 @@ char *strsep(char **sp, char *sep) { return(s); } -// https://github.com/git/git/blob/306ee63a703ad67c54ba1209dc11dd9ea500dc1f/compat/mingw.c#L1111 const char *quote_arg(const char *arg) { int len = 0, n = 0; int force_quotes = 0; diff --git a/src/utils.h b/src/utils.h index 85aee46..c5e985c 100644 --- a/src/utils.h +++ b/src/utils.h @@ -28,9 +28,6 @@ int get_sig(const char *sig_name); // Open uri with the default application of system int open_uri(char *uri); -// Encode text to base64, the caller should free the returned string -char *base64_encode(const unsigned char *buffer, size_t length); - #ifdef _WIN32 char *strsep(char **sp, char *sep); const char *quote_arg(const char *arg); -- 2.43.4