xref: /lighttpd1.4/src/ck.h (revision b9706ac4)
1 /*
2  * ck - C11 Annex K wrappers  (selected functions; not complete)
3  *
4  * ck is also an abbreviation for "check".
5  * These are validating, checking functions.
6  *
7  * Copyright(c) 2016,2021 Glenn Strauss gstrauss()gluelogic.com  All rights reserved
8  * License: BSD 3-clause (same as lighttpd)
9  */
10 #ifndef INCLUDED_CK_H
11 #define INCLUDED_CK_H
12 #ifndef __STDC_WANT_LIB_EXT1__ /*(enable C11 Annex K ext1 *_s functions)*/
13 #define __STDC_WANT_LIB_EXT1__ 1
14 #endif
15 #if defined(__APPLE__) && defined(__MACH__)
16 #ifndef _DARWIN_C_SOURCE
17 #define _DARWIN_C_SOURCE
18 #endif
19 #endif
20 #include "first.h"
21 #ifdef __FreeBSD__
22 #ifndef _RSIZE_T_DEFINED /* expecting __EXT1_VISIBLE 1 and _RSIZE_T_DEFINED */
23 #define _RSIZE_T_DEFINED
24 typedef size_t rsize_t;
25 #endif
26 #include <errno.h>
27 #endif
28 #ifdef __sun
29 #include <errno.h>
30 #endif
31 
32 __BEGIN_DECLS
33 
34 
35 #ifndef RSIZE_MAX
36 #define RSIZE_MAX (SIZE_MAX >> 1)
37 typedef size_t rsize_t;
38 typedef int errno_t;
39 #endif
40 
41 
42 errno_t ck_getenv_s (size_t * restrict len, char * restrict value, rsize_t maxsize, const char * restrict name);
43 
44 /*(ck_memclear_s() is not from C11 Annex K
45  * ck_memclear_s() is similar to memset_s() using constant byte 0 for fill)*/
46 errno_t ck_memclear_s (void *s, rsize_t smax, rsize_t n);
47 
48 /*(ck_memzero() is not from C11 Annex K
49  * ck_memzero() is a convenience wrapper around ck_memclear_s())*/
50 static inline errno_t ck_memzero(void *s, rsize_t n);
51 static inline errno_t ck_memzero(void *s, rsize_t n) {
52     return ck_memclear_s(s, n, n);
53 }
54 
55 errno_t ck_strerror_s (char *s, rsize_t maxsize, errno_t errnum);
56 
57 /*(ck_memeq_const_time() is not from C11 Annex K)
58  * constant time memory compare for equality
59  * rounds to next multiple of 64 to avoid potentially leaking exact
60  * string lengths when subject to high precision timing attacks */
61 __attribute_nonnull__()
62 int ck_memeq_const_time (const void *a, size_t alen, const void *b, size_t blen);
63 
64 /*(ck_memeq_const_time_fixed_len() is not from C11 Annex K)
65  * constant time memory compare for equality for fixed len (e.g. digests)
66  * (padding not necessary for digests, which have fixed, defined lengths) */
67 __attribute_nonnull__()
68 int ck_memeq_const_time_fixed_len (const void *a, const void *b, size_t len);
69 
70 
71 /*(ck_bt() is not from C11 Annex K)
72  * ck_bt() prints backtrace to stderr */
73 __attribute_cold__
74 __attribute_nonnull__()
75 void ck_bt(const char *filename, unsigned int line, const char *msg);
76 
77 /*(ck_bt_abort() is not from C11 Annex K)
78  * ck_bt_abort() prints backtrace to stderr and calls abort() */
79 __attribute_cold__
80 __attribute_nonnull__()
81 __attribute_noreturn__
82 void ck_bt_abort(const char *filename, unsigned int line, const char *msg);
83 
84 /*(ck_assert() and ck_assert_failed() are not from C11 Annex K)
85  * ck_assert() executes a runtime assertion test or aborts
86  * ck_assert() *is not* optimized away if defined(NDEBUG)
87  * (unlike standard assert(), which *is* optimized away if defined(NDEBUG)) */
88 __attribute_cold__
89 __attribute_nonnull__()
90 __attribute_noreturn__
91 void ck_assert_failed(const char *filename, unsigned int line, const char *msg);
92 
93 #define ck_assert(x) \
94         do { if (!(x)) ck_assert_failed(__FILE__, __LINE__, #x); } while (0)
95 
96 
97 __END_DECLS
98 
99 
100 #endif
101