blob: dd0c38903f9ee9f5974c6b589da7a680b792ec5d [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
Willy Tarreau3f2d6962018-12-07 08:35:07 +0100375/* copies the entire <src> over <dst>, which must be allocated large enough to
376 * hold the whole contents. No trailing zero is appended, this is mainly used
377 * for protocol processing where the frame length has already been checked. An
378 * ist made of the output and its length are returned. The destination is not
379 * touched if src.len is null.
380 */
381static inline struct ist ist2bin(char *dst, const struct ist src)
382{
383 size_t ofs = 0;
384
385 /* discourage the compiler from trying to optimize for large strings,
386 * but tell it that most of our strings are not empty.
387 */
388 if (__builtin_expect(ofs < src.len, 1)) {
389 do {
390 dst[ofs] = src.ptr[ofs];
391 ofs++;
392 } while (__builtin_expect(ofs < src.len, 0));
393 }
394 return ist2(dst, ofs);
395}
396
397/* copies the entire <src> over <dst>, which must be allocated large enough to
398 * hold the whole contents as well as a trailing zero which is always appended.
399 * This is mainly used for protocol conversions where the frame length has
400 * already been checked. An ist made of the output and its length (not counting
401 * the trailing zero) are returned.
402 */
403static inline struct ist ist2str(char *dst, const struct ist src, size_t count)
404{
405 size_t ofs = 0;
406
407 /* discourage the compiler from trying to optimize for large strings,
408 * but tell it that most of our strings are not empty.
409 */
410 if (__builtin_expect(ofs < src.len, 1)) {
411 do {
412 dst[ofs] = src.ptr[ofs];
413 ofs++;
414 } while (__builtin_expect(ofs < src.len, 0));
415 }
416 dst[ofs] = 0;
417 return ist2(dst, ofs);
418}
419
420/* makes a lower case copy of the entire <src> into <dst>, which must have been
421 * allocated large enough to hold the whole contents. No trailing zero is
422 * appended, this is mainly used for protocol processing where the frame length
423 * has already been checked. An ist made of the output and its length are
424 * returned. The destination is not touched if src.len is null.
425 */
426static inline struct ist ist2bin_lc(char *dst, const struct ist src)
427{
428 size_t ofs = 0;
429
430 /* discourage the compiler from trying to optimize for large strings,
431 * but tell it that most of our strings are not empty.
432 */
433 if (__builtin_expect(ofs < src.len, 1)) {
434 do {
435 char c = src.ptr[ofs];
436 dst[ofs] = ((unsigned char)(c - 'A') <= 'Z' - 'A') ? c + ('a' - 'A') : c;
437 ofs++;
438 } while (__builtin_expect(ofs < src.len, 0));
439 }
440 return ist2(dst, ofs);
441}
442
443/* makes a lower case copy of the entire <src> into <dst>, which must have been
444 * allocated large enough to hold the whole contents as well as a trailing zero
445 * which is always appended. This is mainly used for protocol conversions where
446 * the frame length has already been checked. An ist made of the output and its
447 * length (not counting the trailing zero) are returned.
448 */
449static inline struct ist ist2str_lc(char *dst, const struct ist src, size_t count)
450{
451 size_t ofs = 0;
452
453 /* discourage the compiler from trying to optimize for large strings,
454 * but tell it that most of our strings are not empty.
455 */
456 if (__builtin_expect(ofs < src.len, 1)) {
457 do {
458 char c = src.ptr[ofs];
459 dst[ofs] = ((unsigned char)(c - 'A') <= 'Z' - 'A') ? c + ('a' - 'A') : c;
460 ofs++;
461 } while (__builtin_expect(ofs < src.len, 0));
462 }
463 dst[ofs] = 0;
464 return ist2(dst, ofs);
465}
466
467/* makes an upper case copy of the entire <src> into <dst>, which must have
468 * been allocated large enough to hold the whole contents. No trailing zero is
469 * appended, this is mainly used for protocol processing where the frame length
470 * has already been checked. An ist made of the output and its length are
471 * returned. The destination is not touched if src.len is null.
472 */
473static inline struct ist ist2bin_uc(char *dst, const struct ist src)
474{
475 size_t ofs = 0;
476
477 /* discourage the compiler from trying to optimize for large strings,
478 * but tell it that most of our strings are not empty.
479 */
480 if (__builtin_expect(ofs < src.len, 1)) {
481 do {
482 char c = src.ptr[ofs];
483 dst[ofs] = ((unsigned char)(c - 'a') <= 'z' - 'a') ? c + ('A' - 'a') : c;
484 ofs++;
485 } while (__builtin_expect(ofs < src.len, 0));
486 }
487 return ist2(dst, ofs);
488}
489
490/* makes an upper case copy of the entire <src> into <dst>, which must have been
491 * allocated large enough to hold the whole contents as well as a trailing zero
492 * which is always appended. This is mainly used for protocol conversions where
493 * the frame length has already been checked. An ist made of the output and its
494 * length (not counting the trailing zero) are returned.
495 */
496static inline struct ist ist2str_uc(char *dst, const struct ist src, size_t count)
497{
498 size_t ofs = 0;
499
500 /* discourage the compiler from trying to optimize for large strings,
501 * but tell it that most of our strings are not empty.
502 */
503 if (__builtin_expect(ofs < src.len, 1)) {
504 do {
505 char c = src.ptr[ofs];
506 dst[ofs] = ((unsigned char)(c - 'a') <= 'z' - 'a') ? c + ('A' - 'a') : c;
507 ofs++;
508 } while (__builtin_expect(ofs < src.len, 0));
509 }
510 dst[ofs] = 0;
511 return ist2(dst, ofs);
512}
513
Willy Tarreaue11f7272017-05-30 17:49:36 +0200514/* looks for first occurrence of character <chr> in string <ist>. Returns the
515 * pointer if found, or NULL if not found.
516 */
517static inline char *istchr(const struct ist ist, char chr)
518{
519 char *s = ist.ptr;
520
521 do {
522 if (s >= ist.ptr + ist.len)
523 return NULL;
524 } while (*s++ != chr);
525 return s - 1;
526}
527
528/* looks for first occurrence of character <chr> in string <ist> and returns
529 * the tail of the string starting with this character, or (ist.end,0) if not
530 * found.
531 */
532static inline struct ist istfind(const struct ist ist, char chr)
533{
534 struct ist ret = ist;
535
536 while (ret.len--) {
537 if (*ret.ptr++ == chr)
538 return ist2(ret.ptr - 1, ret.len + 1);
539 }
540 return ist2(ret.ptr, 0);
541}
542
543/* looks for first occurrence of character different from <chr> in string <ist>
544 * and returns the tail of the string starting at this character, or (ist_end,0)
545 * if not found.
546 */
547static inline struct ist istskip(const struct ist ist, char chr)
548{
549 struct ist ret = ist;
550
551 while (ret.len--) {
552 if (*ret.ptr++ != chr)
553 return ist2(ret.ptr - 1, ret.len + 1);
554 }
555 return ist2(ret.ptr, 0);
556}
557
558/* looks for first occurrence of string <pat> in string <ist> and returns the
559 * tail of the string starting at this position, or (NULL,0) if not found. The
560 * empty pattern is found everywhere.
561 */
562static inline struct ist istist(const struct ist ist, const struct ist pat)
563{
564 struct ist ret = ist;
565 size_t pos;
566
567 if (!pat.len)
568 return ret;
569
570 while (1) {
571 loop:
572 ret = istfind(ret, *pat.ptr);
573 if (ret.len < pat.len)
574 break;
575
576 /* ret.len >= 1, pat.len >= 1 and *ret.ptr == *pat.ptr */
577
578 ret = istnext(ret);
579 for (pos = 0; pos < pat.len - 1; ) {
580 ++pos;
581 if (ret.ptr[pos - 1] != pat.ptr[pos])
582 goto loop;
583 }
584 return ist2(ret.ptr - 1, ret.len + 1);
585 }
586 return ist2(NULL, 0);
587}
588
589#endif