mirror of
https://github.com/tsl0922/ttyd.git
synced 2025-12-22 11:54:19 +01:00
91 lines
2.0 KiB
C
91 lines
2.0 KiB
C
#define _GNU_SOURCE
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <stdbool.h>
|
|
#include <ctype.h>
|
|
#include <string.h>
|
|
#include <signal.h>
|
|
|
|
void *
|
|
xmalloc(size_t size) {
|
|
if (size == 0)
|
|
return NULL;
|
|
void *p = malloc(size);
|
|
if (!p)
|
|
abort();
|
|
return p;
|
|
}
|
|
|
|
void *
|
|
xrealloc(void *p, size_t size) {
|
|
if ((size == 0) && (p == NULL))
|
|
return NULL;
|
|
p = realloc(p, size);
|
|
if (!p)
|
|
abort();
|
|
return p;
|
|
}
|
|
|
|
char *
|
|
uppercase(char *str) {
|
|
int i = 0;
|
|
do {
|
|
str[i] = (char) toupper(str[i]);
|
|
} while (str[i++] != '\0');
|
|
return str;
|
|
}
|
|
|
|
bool
|
|
endswith(const char *str, const char *suffix) {
|
|
size_t str_len = strlen(str);
|
|
size_t suffix_len = strlen(suffix);
|
|
return str_len > suffix_len && !strcmp(str + (str_len - suffix_len), suffix);
|
|
}
|
|
|
|
int
|
|
get_sig_name(int sig, char *buf) {
|
|
int n = sprintf(buf, "SIG%s", sig < NSIG ? strsignal(sig) : "unknown");
|
|
uppercase(buf);
|
|
return n;
|
|
}
|
|
|
|
int
|
|
get_sig(const char *sig_name) {
|
|
if (strcasestr(sig_name, "sig") != sig_name || strlen(sig_name) <= 3) {
|
|
return -1;
|
|
}
|
|
for (int sig = 1; sig < NSIG; sig++) {
|
|
const char *name = strsignal(sig);
|
|
if (strcasecmp(name, sig_name + 3) == 0)
|
|
return sig;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
// 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;
|
|
} |