blob: b15023473081018bd09d6a66d8dee939fd433c52 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
Willy Tarreau2dd0d472006-06-29 17:53:05 +02002 include/common/standard.h
Willy Tarreaubaaee002006-06-26 02:48:02 +02003 This files contains some general purpose functions and macros.
4
Willy Tarreau6911fa42007-03-04 18:06:08 +01005 Copyright (C) 2000-2007 Willy Tarreau - w@1wt.eu
Willy Tarreaubaaee002006-06-26 02:48:02 +02006
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation, version 2.1
10 exclusively.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20*/
21
Willy Tarreau2dd0d472006-06-29 17:53:05 +020022#ifndef _COMMON_STANDARD_H
23#define _COMMON_STANDARD_H
Willy Tarreaubaaee002006-06-26 02:48:02 +020024
Willy Tarreau167d8b52007-04-09 22:16:12 +020025#include <limits.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020026#include <netinet/in.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020027#include <common/config.h>
Willy Tarreauca28d1e2007-01-24 18:20:50 +010028#include <proto/fd.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020029
30/****** string-specific macros and functions ******/
31/* if a > max, then bound <a> to <max>. The macro returns the new <a> */
32#define UBOUND(a, max) ({ typeof(a) b = (max); if ((a) > b) (a) = b; (a); })
33
34/* if a < min, then bound <a> to <min>. The macro returns the new <a> */
35#define LBOUND(a, min) ({ typeof(a) b = (min); if ((a) < b) (a) = b; (a); })
36
37/* returns 1 only if only zero or one bit is set in X, which means that X is a
38 * power of 2, and 0 otherwise */
39#define POWEROF2(x) (((x) & ((x)-1)) == 0)
40
Willy Tarreau7d58a632007-01-13 23:06:06 +010041/*
42 * Gcc >= 3 provides the ability for the programme to give hints to the
43 * compiler about what branch of an if is most likely to be taken. This
44 * helps the compiler produce the most compact critical paths, which is
45 * generally better for the cache and to reduce the number of jumps.
46 */
47#if __GNUC__ < 3
48#define __builtin_expect(x,y) (x)
49#endif
50
51#define likely(x) (__builtin_expect((x) != 0, 1))
52#define unlikely(x) (__builtin_expect((x) != 0, 0))
53
Willy Tarreaubaaee002006-06-26 02:48:02 +020054
55/*
56 * copies at most <size-1> chars from <src> to <dst>. Last char is always
57 * set to 0, unless <size> is 0. The number of chars copied is returned
58 * (excluding the terminating zero).
59 * This code has been optimized for size and speed : on x86, it's 45 bytes
60 * long, uses only registers, and consumes only 4 cycles per char.
61 */
62extern int strlcpy2(char *dst, const char *src, int size);
63
64/*
65 * This function simply returns a statically allocated string containing
66 * the ascii representation for number 'n' in decimal.
67 */
68extern char *ultoa(unsigned long n);
69
70/*
71 * Returns non-zero if character <s> is a hex digit (0-9, a-f, A-F), else zero.
72 */
73extern int ishex(char s);
74
75/*
76 * converts <str> to a struct sockaddr_in* which is locally allocated.
77 * The format is "addr:port", where "addr" can be a dotted IPv4 address,
78 * a host name, or empty or "*" to indicate INADDR_ANY.
79 */
80struct sockaddr_in *str2sa(char *str);
81
82/*
83 * converts <str> to a two struct in_addr* which are locally allocated.
84 * The format is "addr[/mask]", where "addr" cannot be empty, and mask
85 * is optionnal and either in the dotted or CIDR notation.
86 * Note: "addr" can also be a hostname. Returns 1 if OK, 0 if error.
87 */
88int str2net(char *str, struct in_addr *addr, struct in_addr *mask);
89
90/* will try to encode the string <string> replacing all characters tagged in
91 * <map> with the hexadecimal representation of their ASCII-code (2 digits)
92 * prefixed by <escape>, and will store the result between <start> (included)
93 * and <stop> (excluded), and will always terminate the string with a '\0'
94 * before <stop>. The position of the '\0' is returned if the conversion
95 * completes. If bytes are missing between <start> and <stop>, then the
96 * conversion will be incomplete and truncated. If <stop> <= <start>, the '\0'
97 * cannot even be stored so we return <start> without writing the 0.
98 * The input string must also be zero-terminated.
99 */
100extern const char hextab[];
101char *encode_string(char *start, char *stop,
102 const char escape, const fd_set *map,
103 const char *string);
104
Willy Tarreau6911fa42007-03-04 18:06:08 +0100105/* This one is 6 times faster than strtoul() on athlon, but does
106 * no check at all.
107 */
108static inline unsigned int __str2ui(const char *s)
109{
110 unsigned int i = 0;
111 while (*s) {
112 i = i * 10 - '0';
113 i += (unsigned char)*s++;
114 }
115 return i;
116}
117
118/* This one is 5 times faster than strtoul() on athlon with checks.
119 * It returns the value of the number composed of all valid digits read.
120 */
121static inline unsigned int __str2uic(const char *s)
122{
123 unsigned int i = 0;
124 unsigned int j;
125 while (1) {
126 j = (*s++) - '0';
127 if (j > 9)
128 break;
129 i *= 10;
130 i += j;
131 }
132 return i;
133}
134
135/* This one is 28 times faster than strtoul() on athlon, but does
136 * no check at all!
137 */
138static inline unsigned int __strl2ui(const char *s, int len)
139{
140 unsigned int i = 0;
141 while (len-- > 0) {
142 i = i * 10 - '0';
143 i += (unsigned char)*s++;
144 }
145 return i;
146}
147
148/* This one is 7 times faster than strtoul() on athlon with checks.
149 * It returns the value of the number composed of all valid digits read.
150 */
151static inline unsigned int __strl2uic(const char *s, int len)
152{
153 unsigned int i = 0;
154 unsigned int j;
155
156 while (len-- > 0) {
157 j = (*s++) - '0';
158 i = i * 10;
159 if (j > 9)
160 break;
161 i += j;
162 }
163 return i;
164}
165
166extern unsigned int str2ui(const char *s);
167extern unsigned int str2uic(const char *s);
168extern unsigned int strl2ui(const char *s, int len);
169extern unsigned int strl2uic(const char *s, int len);
170extern int strl2ic(const char *s, int len);
171extern int strl2irc(const char *s, int len, int *ret);
172extern int strl2llrc(const char *s, int len, long long *ret);
173
Willy Tarreau2dd0d472006-06-29 17:53:05 +0200174#endif /* _COMMON_STANDARD_H */