1#ifndef UTIL_H
2#define UTIL_H
3
4#include <stddef.h>
5
6/**
7 * @brief Case-insensitive substring search
8 *
9 * @param hay Haystack
10 * @param nee Needle
11 *
12 * @return Ptr to first match in hay, NULL=Not found
13 */
14const char* cistrstr(const char* hay, const char* nee);
15
16/**
17 * @brief Extract header value from raw HTTP request header block
18 *
19 * @param out Output buffer (NULL terminated on success)
20 * @param hdr Raw HTTP request header block
21 * @param key Header key to search for (case-insensitive)
22 *
23 * @return 0=Success, -1=Not found / Malformed
24 */
25int hdr_get_value(char out[512], const char* hdr, const char* key);
26
27/**
28 * @brief Join two path fragments
29 *
30 * @param out Output path buffer
31 * @param outsz Output buffer size
32 * @param a Left path
33 * @param b Right path
34 *
35 * @return 0=Success, -1=Overflow / Invalid input
36 */
37int join_path(char* out, size_t outsz, const char* a, const char* b);
38
39/**
40 * @brief Securely zero memory
41 *
42 * @note Prevents compiler from optimising the wipe away
43 *
44 * @param p Pointer to memory to wipe.
45 * @param n Number of bytes to wipe.
46 */
47void secure_bzero(void* p, size_t n);
48
49/**
50 * @brief Write exactly n bytes to a file descriptor
51 *
52 * @note Retries on EINTR.
53 *
54 * @param fd File descriptor
55 * @param buf Input buffer
56 * @param n Number of bytes to write
57 *
58 * @return 0=Success, -1=Failure
59 */
60int write_all(int fd, const void* buf, size_t n);
61
62#endif /* UTIL_H */
63