From cc3f33db7a8d3c4ad373e646b199808e01bc5d9b Mon Sep 17 00:00:00 2001 From: sss Date: Tue, 17 Jan 2023 00:38:19 +0300 Subject: added webrdp public code --- src/core/utilities.c | 115 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 src/core/utilities.c (limited to 'src/core/utilities.c') diff --git a/src/core/utilities.c b/src/core/utilities.c new file mode 100644 index 0000000..969e36b --- /dev/null +++ b/src/core/utilities.c @@ -0,0 +1,115 @@ +/* BSD-2-Clause license + * + * Copyright (c) 2018-2023 NST , sss . + * + */ + +#include +#include +#include + +#include +#include +#include + +#include + +bool +is_regular_file(const char *path) +{ + struct stat path_stat; + stat(path, &path_stat); + return S_ISREG(path_stat.st_mode); +} + +bool +is_directory(const char *path) +{ + struct stat path_stat; + stat(path, &path_stat); + return S_ISDIR(path_stat.st_mode); +} + +bool +sha1(uint8_t *dst, const uint8_t *src, size_t src_length) +{ + SHA_CTX ctx = {0}; + if (!SHA1_Init(&ctx)) + { + return false; + } + if (!SHA1_Update(&ctx, src, src_length)) + { + return false; + } + if (!SHA1_Final(dst, &ctx)) + { + return false; + } + return true; +} + +void +hex_print(uint8_t *buf, size_t buf_len) +{ + size_t p; + for (p = 0; p < buf_len; ++p) + { + if (!p) + { + printf("00: "); + } + if (p && !(p % 8)) + { + printf("| "); + } + if (p && !(p % 16)) + { + size_t pp = p - 16; + for (; pp < p; ++pp) + { + printf("%c", ((char *)(buf))[pp]); + } + printf("\n%lx: ", p); + } + printf("%x ", ((char *)(buf))[p]); + } +} + +char +random_ascii_character() +{ + /* use whole ascii table of printable characters */ + char c; +get_new_char: + c = ((rand() % 94) + 32); + while (!c) + goto get_new_char; + return c; +} + +char * +random_ascii_string(char *buf, const size_t len) +{ + size_t i = 0; + if (!buf) + { + return 0; + } + for (; i < len; ++i) + { + buf[i] = random_ascii_character(); + } + buf[len] = 0; + return buf; +} + +void +random_bytes(uint8_t *buf, size_t buf_len) +{ + size_t i = 0; + for (; i < buf_len; ++i) + { + buf[i] = rand(); + } +} -- cgit v1.2.3