blob: 7c60c68f3c20ce8ab398c039df77d5a33af4a9e5 [file] [log] [blame]
Willy Tarreaue11f7272017-05-30 17:49:36 +02001/*
2 * include/common/ist.h
3 * Very simple indirect string manipulation functions.
4 *
5 * Copyright (C) 2014-2017 Willy Tarreau - w@1wt.eu
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
26 */
27
28#ifndef _COMMON_IST_H
29#define _COMMON_IST_H
30
Christopher Faulet20761452018-06-06 16:33:53 +020031#include <ctype.h>
Willy Tarreaue11f7272017-05-30 17:49:36 +020032#include <string.h>
33
34#include <common/config.h>
35
36/* This string definition will most often be used to represent a read-only
37 * string returned from a function, based on the starting point and its length
38 * in bytes. No storage is provided, only a pointer and a length. The types
39 * here are important as we only want to have 2 native machine words there so
40 * that on modern architectures the compiler is capable of efficiently
41 * returning a register pair without having to allocate stack room from the
42 * caller. This is done with -freg-struct which is often enabled by default.
43 */
44struct ist {
45 char *ptr;
46 size_t len;
47};
48
Willy Tarreau2ba67272017-09-21 15:24:10 +020049/* makes a constant ist from a constant string, for use in array declarations */
50#define IST(str) { .ptr = str "", .len = (sizeof str "") - 1 }
51
Willy Tarreaue11f7272017-05-30 17:49:36 +020052/* makes an ist from a regular zero terminated string. Null has length 0.
53 * Constants are detected and replaced with constant initializers. Other values
54 * are measured by hand without strlen() as it's much cheaper and inlinable on
55 * small strings. The construct is complex because we must never call
56 * __builtin_strlen() with an expression otherwise it involves a real
57 * measurement.
58 */
59#if __GNUC__ >= 4
60// gcc >= 4 detects constant propagation of str through __x and resolves the
61// length of constant strings easily.
62#define ist(str) ({ \
63 char *__x = (void *)(str); \
64 (struct ist){ \
65 .ptr = __x, \
66 .len = __builtin_constant_p(str) ? \
67 ((void *)str == (void *)0) ? 0 : \
68 __builtin_strlen(__x) : \
69 ({ \
70 size_t __l = 0; \
71 if (__x) for (__l--; __x[++__l]; ) ; \
72 __l; \
73 }) \
74 }; \
75})
76#else
77// gcc < 4 can't do this, and the side effect is a warning each time a NULL is
78// passed to ist() due to the check on __builtin_strlen(). It doesn't have the
79// ability to know that this code is never called.
80#define ist(str) ({ \
81 char *__x = (void *)(str); \
82 (struct ist){ \
83 .ptr = __x, \
84 .len = __builtin_constant_p(str) ? \
85 ((void *)str == (void *)0) ? 0 : \
86 __builtin_strlen(str) : \
87 ({ \
88 size_t __l = 0; \
89 if (__x) for (__l--; __x[++__l]; ) ; \
90 __l; \
91 }) \
92 }; \
93})
94#endif
95
96/* makes an ist struct from a string and a length */
97static inline struct ist ist2(const void *ptr, size_t len)
98{
99 return (struct ist){ .ptr = (char *)ptr, .len = len };
100}
101
Willy Tarreaue67c4e52017-10-19 06:28:23 +0200102/* This function MODIFIES the string to add a zero AFTER the end, and returns
103 * the start pointer. The purpose is to use it on strings extracted by parsers
104 * from larger strings cut with delimiters that are not important and can be
105 * destroyed. It allows any such string to be used with regular string
106 * functions. It's also convenient to use with printf() to show data extracted
107 * from writable areas. The caller is obviously responsible for ensuring that
108 * the string is valid and that the first byte past the end is writable. If
109 * these conditions cannot be satisfied, use istpad() below instead.
110 */
111static inline char *ist0(struct ist ist)
112{
113 ist.ptr[ist.len] = 0;
114 return ist.ptr;
115}
116
Willy Tarreaue11f7272017-05-30 17:49:36 +0200117/* returns the length of the string */
118static inline size_t istlen(const struct ist ist)
119{
120 return ist.len;
121}
122
123/* skips to next character in the string, always stops at the end */
124static inline struct ist istnext(const struct ist ist)
125{
126 struct ist ret = ist;
127
128 if (ret.len) {
129 ret.len--;
130 ret.ptr++;
131 }
132 return ret;
133}
134
135/* copies the contents from string <ist> to buffer <buf> and adds a trailing
136 * zero. The caller must ensure <buf> is large enough.
137 */
138static inline struct ist istpad(void *buf, const struct ist ist)
139{
140 struct ist ret = { .ptr = buf, .len = ist.len };
141
142 for (ret.len = 0; ret.len < ist.len; ret.len++)
143 ret.ptr[ret.len] = ist.ptr[ret.len];
144
145 ret.ptr[ret.len] = 0;
146 return ret;
147}
148
149/* trims string <ist> to no more than <size> characters. The string is
150 * returned.
151 */
152static inline struct ist isttrim(const struct ist ist, size_t size)
153{
154 struct ist ret = ist;
155
156 if (ret.len > size)
157 ret.len = size;
158 return ret;
159}
160
161/* trims string <ist> to no more than <size>-1 characters and ensures that a
162 * zero is placed after <ist.len> (possibly reduced by one) and before <size>,
163 * unless <size> is already zero. The string is returned. This is mostly aimed
164 * at building printable strings that need to be zero-terminated.
165 */
166static inline struct ist istzero(const struct ist ist, size_t size)
167{
168 struct ist ret = ist;
169
170 if (!size)
171 ret.len = 0;
172 else {
173 if (ret.len > size - 1)
174 ret.len = size - 1;
175 ret.ptr[ret.len] = 0;
176 }
177 return ret;
178}
179
180/* returns the ordinal difference between two strings :
181 * < 0 if ist1 < ist2
182 * = 0 if ist1 == ist2
183 * > 0 if ist1 > ist2
184 */
185static inline int istdiff(const struct ist ist1, const struct ist ist2)
186{
187 struct ist l = ist1;
188 struct ist r = ist2;
189
190 do {
191 if (!l.len--)
192 return -r.len;
193 if (!r.len--)
194 return 1;
195 } while (*l.ptr++ == *r.ptr++);
196
197 return *(unsigned char *)(l.ptr - 1) - *(unsigned char *)(r.ptr - 1);
198}
199
200/* returns non-zero if <ist1> starts like <ist2> (empty strings do match) */
201static inline int istmatch(const struct ist ist1, const struct ist ist2)
202{
203 struct ist l = ist1;
204 struct ist r = ist2;
205
206 if (l.len < r.len)
207 return 0;
208
209 while (r.len--) {
210 if (*l.ptr++ != *r.ptr++)
211 return 0;
212 }
213 return 1;
214}
215
216/* returns non-zero if <ist1> starts like <ist2> on the first <count>
217 * characters (empty strings do match).
218 */
219static inline int istnmatch(const struct ist ist1, const struct ist ist2, size_t count)
220{
221 struct ist l = ist1;
222 struct ist r = ist2;
223
224 if (l.len > count)
225 l.len = count;
226 if (r.len > count)
227 r.len = count;
228 return istmatch(l, r);
229}
230
231/* returns non-zero if <ist1> equals <ist2> (empty strings are equal) */
232static inline int isteq(const struct ist ist1, const struct ist ist2)
233{
234 struct ist l = ist1;
235 struct ist r = ist2;
236
237 if (l.len != r.len)
238 return 0;
239
240 while (l.len--) {
241 if (*l.ptr++ != *r.ptr++)
242 return 0;
243 }
244 return 1;
245}
246
Christopher Faulet20761452018-06-06 16:33:53 +0200247/* returns non-zero if <ist1> equals <ist2>, ignoring the case (empty strings are equal) */
248static inline int isteqi(const struct ist ist1, const struct ist ist2)
249{
250 struct ist l = ist1;
251 struct ist r = ist2;
252
253 if (l.len != r.len)
254 return 0;
255
256 while (l.len--) {
257 if (tolower(*l.ptr) != tolower(*r.ptr))
258 return 0;
259 l.ptr++;
260 r.ptr++;
261 }
262 return 1;
263}
264
Willy Tarreaue11f7272017-05-30 17:49:36 +0200265/* returns non-zero if <ist1> equals <ist2> on the first <count> characters
266 * (empty strings are equal).
267 */
268static inline int istneq(const struct ist ist1, const struct ist ist2, size_t count)
269{
270 struct ist l = ist1;
271 struct ist r = ist2;
272
273 if (l.len > count)
274 l.len = count;
275 if (r.len > count)
276 r.len = count;
277 return isteq(l, r);
278}
279
280/* copies <src> over <dst> for a maximum of <count> bytes. Returns the number
281 * of characters copied (src.len), or -1 if it does not fit. In all cases, the
282 * contents are copied prior to reporting an error, so that the destination
283 * at least contains a valid but truncated string.
284 */
285static inline ssize_t istcpy(struct ist *dst, const struct ist src, size_t count)
286{
287 dst->len = 0;
288
289 if (count > src.len)
290 count = src.len;
291
292 while (dst->len < count) {
293 dst->ptr[dst->len] = src.ptr[dst->len];
294 dst->len++;
295 }
296
297 if (dst->len == src.len)
298 return src.len;
299
300 return -1;
301}
302
303/* copies <src> over <dst> for a maximum of <count> bytes. Returns the number
304 * of characters copied, or -1 if it does not fit. A (possibly truncated) valid
305 * copy of <src> is always left into <dst>, and a trailing \0 is appended as
306 * long as <count> is not null, even if that results in reducing the string by
307 * one character.
308 */
309static inline ssize_t istscpy(struct ist *dst, const struct ist src, size_t count)
310{
311 dst->len = 0;
312
313 if (!count)
314 goto fail;
315
316 if (count > src.len)
317 count = src.len + 1;
318
319 while (dst->len < count - 1) {
320 dst->ptr[dst->len] = src.ptr[dst->len];
321 dst->len++;
322 }
323
324 dst->ptr[dst->len] = 0;
325 if (dst->len == src.len)
326 return src.len;
327 fail:
328 return -1;
329}
330
331/* appends <src> after <dst> for a maximum of <count> total bytes in <dst> after
332 * the copy. <dst> is assumed to be <count> or less before the call. The new
333 * string's length is returned, or -1 if a truncation happened. In all cases,
334 * the contents are copied prior to reporting an error, so that the destination
335 * at least contains a valid but truncated string.
336 */
337static inline ssize_t istcat(struct ist *dst, const struct ist src, size_t count)
338{
339 const char *s = src.ptr;
340
341 while (dst->len < count && s != src.ptr + src.len)
342 dst->ptr[dst->len++] = *s++;
343
344 if (s == src.ptr + src.len)
345 return dst->len;
346
347 return -1;
348}
349
350/* appends <src> after <dst> for a maximum of <count> total bytes in <dst> after
351 * the copy. <dst> is assumed to be <count> or less before the call. The new
352 * string's length is returned, or -1 if a truncation happened. In all cases,
353 * the contents are copied prior to reporting an error, so that the destination
354 * at least contains a valid but truncated string.
355 */
356static inline ssize_t istscat(struct ist *dst, const struct ist src, size_t count)
357{
358 const char *s = src.ptr;
359
360 if (!count)
361 goto fail;
362
363 while (dst->len < count - 1 && s != src.ptr + src.len) {
364 dst->ptr[dst->len++] = *s++;
365 }
366
367 dst->ptr[dst->len] = 0;
368 if (s == src.ptr + src.len)
369 return dst->len;
370 fail:
371 return -1;
372}
373
374/* looks for first occurrence of character <chr> in string <ist>. Returns the
375 * pointer if found, or NULL if not found.
376 */
377static inline char *istchr(const struct ist ist, char chr)
378{
379 char *s = ist.ptr;
380
381 do {
382 if (s >= ist.ptr + ist.len)
383 return NULL;
384 } while (*s++ != chr);
385 return s - 1;
386}
387
388/* looks for first occurrence of character <chr> in string <ist> and returns
389 * the tail of the string starting with this character, or (ist.end,0) if not
390 * found.
391 */
392static inline struct ist istfind(const struct ist ist, char chr)
393{
394 struct ist ret = ist;
395
396 while (ret.len--) {
397 if (*ret.ptr++ == chr)
398 return ist2(ret.ptr - 1, ret.len + 1);
399 }
400 return ist2(ret.ptr, 0);
401}
402
403/* looks for first occurrence of character different from <chr> in string <ist>
404 * and returns the tail of the string starting at this character, or (ist_end,0)
405 * if not found.
406 */
407static inline struct ist istskip(const struct ist ist, char chr)
408{
409 struct ist ret = ist;
410
411 while (ret.len--) {
412 if (*ret.ptr++ != chr)
413 return ist2(ret.ptr - 1, ret.len + 1);
414 }
415 return ist2(ret.ptr, 0);
416}
417
418/* looks for first occurrence of string <pat> in string <ist> and returns the
419 * tail of the string starting at this position, or (NULL,0) if not found. The
420 * empty pattern is found everywhere.
421 */
422static inline struct ist istist(const struct ist ist, const struct ist pat)
423{
424 struct ist ret = ist;
425 size_t pos;
426
427 if (!pat.len)
428 return ret;
429
430 while (1) {
431 loop:
432 ret = istfind(ret, *pat.ptr);
433 if (ret.len < pat.len)
434 break;
435
436 /* ret.len >= 1, pat.len >= 1 and *ret.ptr == *pat.ptr */
437
438 ret = istnext(ret);
439 for (pos = 0; pos < pat.len - 1; ) {
440 ++pos;
441 if (ret.ptr[pos - 1] != pat.ptr[pos])
442 goto loop;
443 }
444 return ist2(ret.ptr - 1, ret.len + 1);
445 }
446 return ist2(NULL, 0);
447}
448
449#endif