The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
misc.h
Go to the documentation of this file.
1 #pragma once
2 /*
3  * This library is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU Lesser General Public
5  * License as published by the Free Software Foundation; either
6  * version 2.1 of the License, or (at your option) any later version.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with this library; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
16  */
17 
18 /** Various miscellaneous utility functions
19  *
20  * @file src/lib/util/misc.h
21  *
22  * @copyright 2000,2006 The FreeRADIUS server project
23  */
24 RCSIDH(misc_h, "$Id: de2598cee5289dbb7bcd58e116874b44501bdc30 $")
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 #include <freeradius-devel/build.h>
31 #include <freeradius-devel/missing.h>
32 #include <freeradius-devel/util/print.h>
33 #include <freeradius-devel/util/talloc.h>
34 #include <freeradius-devel/util/time.h>
35 
36 #include <signal.h>
37 
38 typedef int8_t (*fr_cmp_t)(void const *a, void const *b);
39 typedef void (*fr_free_t)(void *);
40 
41 /*
42  * Define TALLOC_DEBUG to check overflows with talloc.
43  * we can't use valgrind, because the memory used by
44  * talloc is valid memory... just not for us.
45  */
46 #ifdef TALLOC_DEBUG
47 void fr_talloc_verify_cb(const void *ptr, int depth,
48  int max_depth, int is_ref,
49  void *private_data);
50 # define VERIFY_ALL_TALLOC talloc_report_depth_cb(NULL, 0, -1, fr_talloc_verify_cb, NULL)
51 #else
52 # define VERIFY_ALL_TALLOC
53 #endif
54 
55 /** Skip whitespace ('\\t', '\\n', '\\v', '\\f', '\\r', ' ')
56  *
57  * @param[in,out] _p string to skip over.
58  */
59 #define fr_skip_whitespace(_p) while(isspace((uint8_t)*(_p))) _p++
60 
61 /** Skip whitespace, stopping at end ('\\t', '\\n', '\\v', '\\f', '\\r', ' ')
62  *
63  * @param[in,out] _p string to skip over.
64  * @param[in] _e pointer to end of string.
65  */
66 #define fr_bskip_whitespace(_p, _e) while((_p < _e) && isspace((uint8_t)*(_p))) _p++
67 
68 /** Skip everything that's not whitespace ('\\t', '\\n', '\\v', '\\f', '\\r', ' ')
69  *
70  * @param[in,out] _p string to skip over.
71  */
72 #define fr_skip_not_whitespace(_p) while(*_p && !isspace((uint8_t)*(_p))) _p++
73 
74 /** Zero out any whitespace with nul bytes
75  *
76  * @param[in,out] _p string to process
77  */
78 #define fr_zero_whitespace(_p) while (isspace((uint8_t) *_p)) *(_p++) = '\0'
79 
80 /** Check whether the string is all whitespace
81  *
82  * @return
83  * - true if the entirety of the string is whitespace.
84  * - false if the string contains non whitespace.
85  */
86 static inline bool is_whitespace(char const *value)
87 {
88 #ifdef STATIC_ANALYZER
89  if (*value == '\0') return false; /* clang analyzer doesn't seem to know what isspace does */
90 #endif
91  do {
92  if (!isspace((uint8_t) *value)) return false;
93  } while (*++value);
94 
95  return true;
96 }
97 
98 /** Check whether the string is made up of printable UTF8 chars
99  *
100  * @param value to check.
101  * @param len of value.
102  *
103  * @return
104  * - true if the string is printable.
105  * - false if the string contains non printable chars
106  */
107  static inline bool is_printable(void const *value, size_t len)
108  {
109  uint8_t const *p = value;
110  size_t clen;
111  size_t i;
112 
113  for (i = 0; i < len; i++) {
114  clen = fr_utf8_char(p, len - i);
115  if (clen == 0) return false;
116  i += (size_t)clen;
117  p += clen;
118  }
119  return true;
120  }
121 
122 /** Check whether the string is all numbers
123  *
124  * @return
125  * - true if the entirety of the string is number chars.
126  * - false if string contains non-numeric chars or is empty.
127  */
128 static inline bool is_integer(char const *value)
129 {
130 #ifdef STATIC_ANALYZER
131  if (*value == '\0') return false; /* clang analyzer doesn't seem to know what isdigit does */
132 #endif
133  do {
134  if (!isdigit((uint8_t) *value)) return false;
135  } while (*++value);
136 
137  return true;
138 }
139 
140 /** Check whether the string is all zeros
141  *
142  * @return
143  * - true if the entirety of the string is all zeros.
144  * - false if string contains non-zero chars or is empty.
145  */
146 static inline bool is_zero(char const *value)
147 {
148  do {
149  if (*value != '0') return false;
150  } while (*++value);
151 
152  return true;
153 }
154 
155 int fr_set_signal(int sig, sig_t func);
156 int fr_unset_signal(int sig);
157 int rad_lockfd(int fd, int lock_len);
158 int rad_lockfd_nonblock(int fd, int lock_len);
159 int rad_unlockfd(int fd, int lock_len);
160 int fr_strtoull(uint64_t *out, char **end, char const *value);
161 int fr_strtoll(int64_t *out, char **end, char const *value);
162 char *fr_trim(char const *str, size_t size);
163 char *fr_tolower(char *str);
164 
165 int fr_nonblock(int fd);
166 int fr_blocking(int fd);
167 
168 ssize_t fr_utf8_to_ucs2(uint8_t *out, size_t outlen, char const *in, size_t inlen);
169 size_t fr_snprint_uint128(char *out, size_t outlen, uint128_t const num);
170 
171 int8_t fr_pointer_cmp(void const *a, void const *b);
172 void fr_quick_sort(void const *to_sort[], int min_idx, int max_idx, fr_cmp_t cmp);
173 int fr_digest_cmp(uint8_t const *a, uint8_t const *b, size_t length) CC_HINT(nonnull);
174 
175 #ifdef __cplusplus
176 }
177 #endif
#define RCSIDH(h, id)
Definition: build.h:482
static fr_slen_t in
Definition: dict.h:821
Test enumeration values.
Definition: dict_test.h:92
long int ssize_t
Definition: merged_model.c:24
unsigned char uint8_t
Definition: merged_model.c:30
unsigned long int size_t
Definition: merged_model.c:25
static uint8_t depth(fr_minmax_heap_index_t i)
Definition: minmax_heap.c:83
int fr_unset_signal(int sig)
Uninstall a signal for a specific handler.
Definition: misc.c:76
char * fr_trim(char const *str, size_t size)
Trim whitespace from the end of a string.
Definition: misc.c:213
int fr_strtoull(uint64_t *out, char **end, char const *value)
Consume the integer (or hex) portion of a value string.
Definition: misc.c:160
int fr_set_signal(int sig, sig_t func)
Sets a signal handler using sigaction if available, else signal.
Definition: misc.c:47
int rad_unlockfd(int fd, int lock_len)
Definition: misc.c:141
static bool is_printable(void const *value, size_t len)
Check whether the string is made up of printable UTF8 chars.
Definition: misc.h:107
char * fr_tolower(char *str)
Definition: misc.c:225
static bool is_whitespace(char const *value)
Check whether the string is all whitespace.
Definition: misc.h:86
ssize_t fr_utf8_to_ucs2(uint8_t *out, size_t outlen, char const *in, size_t inlen)
Convert UTF8 string to UCS2 encoding.
Definition: misc.c:315
int fr_blocking(int fd)
int fr_strtoll(int64_t *out, char **end, char const *value)
Consume the integer (or hex) portion of a value string.
Definition: misc.c:191
int8_t fr_pointer_cmp(void const *a, void const *b)
Compares two pointers.
Definition: misc.c:417
static bool is_integer(char const *value)
Check whether the string is all numbers.
Definition: misc.h:128
void fr_quick_sort(void const *to_sort[], int min_idx, int max_idx, fr_cmp_t cmp)
Quick sort an array of pointers using a comparator.
Definition: misc.c:429
int fr_nonblock(int fd)
int8_t(* fr_cmp_t)(void const *a, void const *b)
Definition: misc.h:38
int rad_lockfd(int fd, int lock_len)
Definition: misc.c:119
int rad_lockfd_nonblock(int fd, int lock_len)
Definition: misc.c:129
size_t fr_snprint_uint128(char *out, size_t outlen, uint128_t const num)
Write 128bit unsigned integer to buffer.
Definition: misc.c:369
void(* fr_free_t)(void *)
Definition: misc.h:39
int fr_digest_cmp(uint8_t const *a, uint8_t const *b, size_t length)
Do a comparison of two authentication digests by comparing the FULL data.
Definition: misc.c:472
static bool is_zero(char const *value)
Check whether the string is all zeros.
Definition: misc.h:146
size_t fr_utf8_char(uint8_t const *str, ssize_t inlen)
Checks for utf-8, taken from http://www.w3.org/International/questions/qa-forms-utf-8.
Definition: print.c:39
Signals that can be sent to a request.
static size_t char fr_sbuff_t size_t inlen
Definition: value.h:997
int nonnull(2, 5))
static size_t char ** out
Definition: value.h:997