Debian Codegraph @

src/util.h

raw symbols SCIP
1 /*
2 * HTTPDirFS - HTTP Directory Filesystem
3 *
4 * Copyright (C) 2020-2026 Fufu Fang <[email protected]>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * In addition, as a special exception, the copyright holders give
20 * permission to link the code of portions of this program with the OpenSSL
21 * library.
22 */
23
24 #ifndef UTIL_H
25 #define UTIL_H
26 /**
27 * \file util.h
28 * \brief Utility functions and memory allocation wrappers header
29 */
30
31 #include <pthread.h>
32 #include <semaphore.h>
33 #include <stdint.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37
38 #ifdef __APPLE__
39 typedef struct {
40 pthread_mutex_t mutex;
41 pthread_cond_t cond;
42 int count;
43 } sys_sem_t;
44 #else
45 typedef sem_t sys_sem_t;
46 #endif
47
48 /**
49 * \brief append a path
50 * \details This function appends a path with the next level, while taking the
51 * trailing slash of the upper level into account.
52 * \note You need to free the char * after use.
53 */
54 char *path_append(const char *path, const char *filename);
55
56 /**
57 * \brief wrapper for pthread_mutex_init(), with error handling
58 */
59 void pthread_mutex_init_wrapper(pthread_mutex_t *x,
60 const pthread_mutexattr_t *attr,
61 const char *file, const char *func, int line);
62 #define PTHREAD_MUTEX_INIT(x, attr) \
63 pthread_mutex_init_wrapper(x, attr, __FILE__, __func__, __LINE__)
64
65 /**
66 * \brief wrapper for pthread_mutex_destroy(), with error handling
67 */
68 void pthread_mutex_destroy_wrapper(pthread_mutex_t *x, const char *file,
69 const char *func, int line);
70 #define PTHREAD_MUTEX_DESTROY(x) \
71 pthread_mutex_destroy_wrapper(x, __FILE__, __func__, __LINE__)
72
73 /**
74 * \brief wrapper for pthread_mutex_lock(), with error handling
75 */
76 void pthread_mutex_lock_wrapper(const char *file, const char *func, int line,
77 pthread_mutex_t *x);
78 #define PTHREAD_MUTEX_LOCK(x) \
79 pthread_mutex_lock_wrapper(__FILE__, __func__, __LINE__, x)
80
81 /**
82 * \brief wrapper for pthread_mutex_unlock(), with error handling
83 */
84 void pthread_mutex_unlock_wrapper(const char *file, const char *func, int line,
85 pthread_mutex_t *x);
86 #define PTHREAD_MUTEX_UNLOCK(x) \
87 pthread_mutex_unlock_wrapper(__FILE__, __func__, __LINE__, x)
88
89 /**
90 * \brief wrapper for sem_init(), with error handling
91 * */
92 void sem_init_wrapper(sys_sem_t *sem, int pshared, unsigned int value,
93 const char *file, const char *func, int line);
94 #define SEM_INIT(sem, pshared, value) \
95 sem_init_wrapper(sem, pshared, value, __FILE__, __func__, __LINE__)
96
97 /**
98 * \brief wrapper for sem_destroy(), with error handling
99 */
100 void sem_destroy_wrapper(sys_sem_t *sem, const char *file, const char *func,
101 int line);
102 #define SEM_DESTROY(sem) sem_destroy_wrapper(sem, __FILE__, __func__, __LINE__)
103
104 /**
105 * \brief wrapper for sem_wait(), with error handling
106 */
107 void sem_wait_wrapper(const char *file, const char *func, int line,
108 sys_sem_t *sem);
109 #define SEM_WAIT(sem) sem_wait_wrapper(__FILE__, __func__, __LINE__, sem)
110
111 /**
112 * \brief wrapper for sem_post(), with error handling
113 */
114 void sem_post_wrapper(const char *file, const char *func, int line,
115 sys_sem_t *sem);
116 #define SEM_POST(sem) sem_post_wrapper(__FILE__, __func__, __LINE__, sem)
117
118 /**
119 * \brief wrapper for sem_trywait(), with cross-platform support and error
120 * handling
121 */
122 int sys_sem_trywait_wrapper(const char *file, const char *func, int line,
123 sys_sem_t *sem);
124 #define SEM_TRYWAIT(sem) \
125 sys_sem_trywait_wrapper(__FILE__, __func__, __LINE__, sem)
126
127 /**
128 * \brief wrapper for pthread_cond_init(), with error handling
129 */
130 void pthread_cond_init_wrapper(pthread_cond_t *cond,
131 const pthread_condattr_t *attr, const char *file,
132 const char *func, int line);
133 #define PTHREAD_COND_INIT(cond, attr) \
134 pthread_cond_init_wrapper(cond, attr, __FILE__, __func__, __LINE__)
135
136 /**
137 * \brief wrapper for pthread_cond_destroy(), with error handling
138 */
139 void pthread_cond_destroy_wrapper(pthread_cond_t *cond, const char *file,
140 const char *func, int line);
141 #define PTHREAD_COND_DESTROY(cond) \
142 pthread_cond_destroy_wrapper(cond, __FILE__, __func__, __LINE__)
143
144 /**
145 * \brief wrapper for pthread_cond_broadcast(), with error handling
146 */
147 void pthread_cond_broadcast_wrapper(const char *file, const char *func,
148 int line, pthread_cond_t *cond);
149 #define PTHREAD_COND_BROADCAST(cond) \
150 pthread_cond_broadcast_wrapper(__FILE__, __func__, __LINE__, cond)
151
152 /**
153 * \brief wrapper for pthread_cond_wait(), with error handling
154 */
155 void pthread_cond_wait_wrapper(const char *file, const char *func, int line,
156 pthread_cond_t *cond, pthread_mutex_t *mutex);
157 #define PTHREAD_COND_WAIT(cond, mutex) \
158 pthread_cond_wait_wrapper(__FILE__, __func__, __LINE__, cond, mutex)
159
160 /**
161 * \brief wrapper for exit(EXIT_FAILURE), with error handling
162 */
163 _Noreturn void exit_failure(void);
164
165 /**
166 * \brief erase a string from the terminal
167 */
168 void erase_string(FILE *file, size_t max_len, char *s);
169
170 /**
171 * \brief generate the salt for authentication string
172 * \details this effectively generates a UUID string, which we use as the salt
173 * \return a pointer to a 37-char array with the salt.
174 */
175 char *generate_salt(void);
176
177 /**
178 * \brief generate the md5sum of a string
179 * \param[in] str a character array for the input string
180 * \return a pointer to a 33-char array with the salt
181 */
182 char *generate_md5sum(const char *str);
183
184 #ifdef DEBUG
185
186 /**
187 * \brief memory allocation tracker node
188 */
189 typedef struct MemNode {
190 void *ptr;
191 size_t size;
192 const char *file;
193 const char *func;
194 int line;
195 struct MemNode *next;
196 } MemNode;
197
198 #endif
199
200 /**
201 * \brief wrapper for calloc(), with error handling and memory tracking
202 */
203 void *CALLOC_wrapper(size_t nmemb, size_t size, const char *file,
204 const char *func, int line);
205 #define CALLOC(nmemb, size) \
206 CALLOC_wrapper(nmemb, size, __FILE__, __func__, __LINE__)
207
208 /**
209 * \brief Internal wrapper for free().
210 */
211 void FREE_wrapper(void *ptr, const char *file, const char *func, int line);
212
213 /**
214 * \brief Macro wrapper for free() that sets the pointer to NULL afterwards.
215 * \note This macro is safe for pointers to const data (e.g., const char *p)
216 * because it only modifies the pointer variable itself, not the data it points
217 * to. However, it will fail for const-qualified pointer variables (e.g., char *
218 * const p) as it attempts to set the pointer itself to NULL.
219 * \note When used on a function parameter (e.g., FREE(ptr)), it only nullifies
220 * the local copy of the pointer within the function scope. The caller's
221 * original pointer remains unchanged and may become a dangling pointer.
222 */
223 #define FREE(ptr) \
224 do { \
225 __extension__ __auto_type __ptr_to_free = &(ptr); \
226 FREE_wrapper((void *)*__ptr_to_free, __FILE__, __func__, __LINE__); \
227 *__ptr_to_free = NULL; \
228 } while (0)
229
230 /**
231 * \brief wrapper for STRDUP(), with memory tracking
232 */
233 char *STRDUP_wrapper(const char *s, const char *file, const char *func,
234 int line);
235 #define STRDUP(s) STRDUP_wrapper(s, __FILE__, __func__, __LINE__)
236
237 /**
238 * \brief wrapper for STRNDUP(), with memory tracking
239 */
240 char *STRNDUP_wrapper(const char *s, size_t n, const char *file,
241 const char *func, int line);
242 #define STRNDUP(s, n) STRNDUP_wrapper(s, n, __FILE__, __func__, __LINE__)
243
244 /**
245 * \brief wrapper for realloc(), with memory tracking
246 */
247 void *REALLOC_wrapper(void *ptr, size_t size, const char *file,
248 const char *func, int line);
249 #define REALLOC(ptr, size) \
250 REALLOC_wrapper(ptr, size, __FILE__, __func__, __LINE__)
251
252 /**
253 * \brief wrapper for realpath(), with memory tracking
254 */
255 char *REALPATH_wrapper(const char *path, char *resolved_path, const char *file,
256 const char *func, int line);
257 #define REALPATH(path, resolved_path) \
258 REALPATH_wrapper(path, resolved_path, __FILE__, __func__, __LINE__)
259
260 /**
261 * \brief cleanup all allocated memory
262 */
263 void mem_cleanup(void);
264
265 /**
266 * \brief Convert a string to hex
267 */
268 char *str_to_hex(char *s);
269
270 /**
271 * \brief initialise the configuration data structure
272 */
273 void Config_init(void);
274
275 #endif