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