/work/testing/misc_tools.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* SPDX-License-Identifier: GPL-3.0-or-later |
2 | | * Copyright © 2016-2026 The TokTok team. |
3 | | * Copyright © 2013 Tox project. |
4 | | */ |
5 | | |
6 | | /* |
7 | | * Miscellaneous functions and data structures for doing random things. |
8 | | */ |
9 | | #ifndef _POSIX_C_SOURCE |
10 | | // For nanosleep(). |
11 | | #define _POSIX_C_SOURCE 200112L |
12 | | #endif |
13 | | |
14 | | #include "misc_tools.h" |
15 | | |
16 | | #include <assert.h> |
17 | | #include <ctype.h> |
18 | | #include <stdint.h> |
19 | | #include <stdio.h> |
20 | | #include <stdlib.h> |
21 | | #include <string.h> |
22 | | |
23 | | #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) |
24 | | #include <windows.h> |
25 | | #else |
26 | | #include <time.h> |
27 | | #endif |
28 | | |
29 | | #include "../toxcore/ccompat.h" |
30 | | #include "../toxcore/tox.h" |
31 | | |
32 | | void c_sleep(uint32_t x) |
33 | 114k | { |
34 | | #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) |
35 | | Sleep(x); |
36 | | #else |
37 | 114k | struct timespec req; |
38 | 114k | req.tv_sec = x / 1000; |
39 | 114k | req.tv_nsec = (long)x % 1000 * 1000 * 1000; |
40 | 114k | nanosleep(&req, nullptr); |
41 | 114k | #endif |
42 | 114k | } |
43 | | |
44 | | // You are responsible for freeing the return value! |
45 | | uint8_t *hex_string_to_bin(const char *hex_string) |
46 | 0 | { |
47 | | // byte is represented by exactly 2 hex digits, so length of binary string |
48 | | // is half of that of the hex one. only hex string with even length |
49 | | // valid. the more proper implementation would be to check if strlen(hex_string) |
50 | | // is odd and return error code if it is. we assume strlen is even. if it's not |
51 | | // then the last byte just won't be written in 'ret'. |
52 | 0 | size_t i, len = strlen(hex_string) / 2; |
53 | 0 | uint8_t *ret = (uint8_t *)malloc(len); |
54 | 0 | const char *pos = hex_string; |
55 | |
|
56 | 0 | if (ret == nullptr) { |
57 | 0 | return nullptr; |
58 | 0 | } |
59 | | |
60 | 0 | for (i = 0; i < len; ++i, pos += 2) { |
61 | 0 | unsigned int val; |
62 | 0 | sscanf(pos, "%02x", &val); |
63 | 0 | ret[i] = val; |
64 | 0 | } |
65 | |
|
66 | 0 | return ret; |
67 | 0 | } |
68 | | |
69 | | void to_hex(char *out, uint8_t *in, int size) |
70 | 1 | { |
71 | 39 | while (size--) { |
72 | 38 | if (*in >> 4 < 0xA) { |
73 | 24 | *out++ = '0' + (*in >> 4); |
74 | 24 | } else { |
75 | 14 | *out++ = 'A' + (*in >> 4) - 0xA; |
76 | 14 | } |
77 | | |
78 | 38 | if ((*in & 0xf) < 0xA) { |
79 | 21 | *out++ = '0' + (*in & 0xF); |
80 | 21 | } else { |
81 | 17 | *out++ = 'A' + (*in & 0xF) - 0xA; |
82 | 17 | } |
83 | | |
84 | 38 | in++; |
85 | 38 | } |
86 | 1 | } |
87 | | |
88 | | /* Reimplementation of strncasecmp() function from strings.h, as strings.h is |
89 | | * POSIX and not portable. Specifically it doesn't exist on MSVC. |
90 | | */ |
91 | | int tox_strncasecmp(const char *s1, const char *s2, size_t n) |
92 | 288 | { |
93 | 1.27k | while (n--) { |
94 | 1.11k | int c1 = tolower(*(s1++)); |
95 | 1.11k | int c2 = tolower(*(s2++)); |
96 | | |
97 | 1.11k | if (c1 == '\0' || c2 == '\0' || c1 != c2) { |
98 | 126 | return c1 - c2; |
99 | 126 | } |
100 | 1.11k | } |
101 | | |
102 | 162 | return 0; |
103 | 288 | } |
104 | | |
105 | | int cmdline_parsefor_ipv46(int argc, char **argv, bool *ipv6enabled) |
106 | 0 | { |
107 | 0 | int argvoffset = 0, argi; |
108 | |
|
109 | 0 | for (argi = 1; argi < argc; argi++) { |
110 | 0 | if (!tox_strncasecmp(argv[argi], "--ipv", 5)) { |
111 | 0 | if (argv[argi][5] && !argv[argi][6]) { |
112 | 0 | char c = argv[argi][5]; |
113 | |
|
114 | 0 | if (c == '4') { |
115 | 0 | *ipv6enabled = false; |
116 | 0 | } else if (c == '6') { |
117 | 0 | *ipv6enabled = true; |
118 | 0 | } else { |
119 | 0 | printf("Invalid argument: %s. Try --ipv4 or --ipv6!\n", argv[argi]); |
120 | 0 | return -1; |
121 | 0 | } |
122 | 0 | } else { |
123 | 0 | printf("Invalid argument: %s. Try --ipv4 or --ipv6!\n", argv[argi]); |
124 | 0 | return -1; |
125 | 0 | } |
126 | | |
127 | 0 | if (argvoffset != argi - 1) { |
128 | 0 | printf("Argument must come first: %s.\n", argv[argi]); |
129 | 0 | return -1; |
130 | 0 | } |
131 | | |
132 | 0 | argvoffset++; |
133 | 0 | } |
134 | 0 | } |
135 | | |
136 | 0 | return argvoffset; |
137 | 0 | } |