The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
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 */
24RCSIDH(misc_h, "$Id: e7bacbc081e45f4e103299e3c620422378cd1108 $")
25
26#ifdef __cplusplus
27extern "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
38typedef int8_t (*fr_cmp_t)(void const *a, void const *b);
39typedef 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
47void 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/** Zero out any whitespace with nul bytes
56 *
57 * @param[in,out] _p string to process
58 */
59#define fr_zero_whitespace(_p) while (isspace((uint8_t) *_p)) *(_p++) = '\0'
60
61/** Check whether the string is all whitespace
62 *
63 * @return
64 * - true if the entirety of the string is whitespace.
65 * - false if the string contains non whitespace.
66 */
67static inline bool is_whitespace(char const *value)
68{
69#ifdef STATIC_ANALYZER
70 if (*value == '\0') return false; /* clang analyzer doesn't seem to know what isspace does */
71#endif
72 do {
73 if (!isspace((uint8_t) *value)) return false;
74 } while (*++value);
75
76 return true;
77}
78
79/** Check whether the string is made up of printable UTF8 chars
80 *
81 * @param value to check.
82 * @param len of value.
83 *
84 * @return
85 * - true if the string is printable.
86 * - false if the string contains non printable chars
87 */
88 static inline bool is_printable(void const *value, size_t len)
89 {
90 uint8_t const *p = value;
91 size_t clen;
92 size_t i;
93
94 for (i = 0; i < len; i++) {
95 clen = fr_utf8_char(p, len - i);
96 if (clen == 0) return false;
97 i += (size_t)clen;
98 p += clen;
99 }
100 return true;
101 }
102
103/** Check whether the string is all numbers
104 *
105 * @return
106 * - true if the entirety of the string is number chars.
107 * - false if string contains non-numeric chars or is empty.
108 */
109static inline bool is_integer(char const *value)
110{
111#ifdef STATIC_ANALYZER
112 if (*value == '\0') return false; /* clang analyzer doesn't seem to know what isdigit does */
113#endif
114 do {
115 if (!isdigit((uint8_t) *value)) return false;
116 } while (*++value);
117
118 return true;
119}
120
121/** Check whether the string is all zeros
122 *
123 * @return
124 * - true if the entirety of the string is all zeros.
125 * - false if string contains non-zero chars or is empty.
126 */
127static inline bool is_zero(char const *value)
128{
129 do {
130 if (*value != '0') return false;
131 } while (*++value);
132
133 return true;
134}
135
136int fr_set_signal(int sig, sig_t func);
137int fr_unset_signal(int sig);
138int rad_lockfd(int fd, int lock_len);
139int rad_lockfd_nonblock(int fd, int lock_len);
140int rad_unlockfd(int fd, int lock_len);
141int fr_strtoull(uint64_t *out, char **end, char const *value);
142int fr_strtoll(int64_t *out, char **end, char const *value);
143char *fr_trim(char const *str, size_t size);
144char *fr_tolower(char *str);
145
146int fr_nonblock(int fd);
147int fr_blocking(int fd);
148
149ssize_t fr_utf8_to_ucs2(uint8_t *out, size_t outlen, char const *in, size_t inlen);
150size_t fr_snprint_uint128(char *out, size_t outlen, uint128_t const num);
151
152int8_t fr_pointer_cmp(void const *a, void const *b);
153void fr_quick_sort(void const *to_sort[], int min_idx, int max_idx, fr_cmp_t cmp);
154int fr_digest_cmp(uint8_t const *a, uint8_t const *b, size_t length) CC_HINT(nonnull);
155
156#ifdef __cplusplus
157}
158#endif
#define RCSIDH(h, id)
Definition build.h:486
static fr_slen_t in
Definition dict.h:840
Test enumeration values.
Definition dict_test.h:92
long int ssize_t
unsigned char uint8_t
unsigned long int size_t
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
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:88
static bool is_whitespace(char const *value)
Check whether the string is all whitespace.
Definition misc.h:67
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
char * fr_trim(char const *str, size_t size)
Trim whitespace from the end of a string.
Definition misc.c:213
static bool is_integer(char const *value)
Check whether the string is all numbers.
Definition misc.h:109
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
char * fr_tolower(char *str)
Definition misc.c:225
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:127
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:1020
int nonnull(2, 5))
static size_t char ** out
Definition value.h:1020