blob: 3f63ed2dda114a92d3beef94371035675b090db0 [file] [log] [blame]
Willy Tarreaue11f7272017-05-30 17:49:36 +02001/*
Willy Tarreaueb6f7012020-05-27 16:21:26 +02002 * include/import/ist.h
Willy Tarreaue11f7272017-05-30 17:49:36 +02003 * Very simple indirect string manipulation functions.
4 *
Willy Tarreaueb6f7012020-05-27 16:21:26 +02005 * Copyright (C) 2014-2020 Willy Tarreau - w@1wt.eu
Willy Tarreaue11f7272017-05-30 17:49:36 +02006 *
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
Willy Tarreaueb6f7012020-05-27 16:21:26 +020028#ifndef _IMPORT_IST_H
29#define _IMPORT_IST_H
Willy Tarreaue11f7272017-05-30 17:49:36 +020030
Willy Tarreaueb6f7012020-05-27 16:21:26 +020031#include <sys/types.h>
Christopher Faulet20761452018-06-06 16:33:53 +020032#include <ctype.h>
Willy Tarreaueb6f7012020-05-27 16:21:26 +020033#include <stddef.h>
Willy Tarreaue11f7272017-05-30 17:49:36 +020034#include <string.h>
35
Tim Duesterhus35005d02020-03-05 17:56:32 +010036#ifndef IST_FREESTANDING
37#include <stdlib.h>
38#endif
39
Willy Tarreaud6735d62018-12-07 09:40:01 +010040/* ASCII to lower case conversion table */
Willy Tarreau0f35c592019-05-15 16:07:36 +020041#define _IST_LC ((const unsigned char[256]){ \
42 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, \
43 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, \
44 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, \
45 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, \
46 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, \
47 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, \
48 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, \
49 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, \
50 0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, \
51 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, \
52 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, \
53 0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, \
54 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, \
55 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, \
56 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, \
57 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, \
58 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, \
59 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, \
60 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, \
61 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, \
62 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, \
63 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, \
64 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, \
65 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, \
66 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, \
67 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, \
68 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, \
69 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, \
70 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, \
71 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, \
72 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, \
73 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, \
74})
Willy Tarreaud6735d62018-12-07 09:40:01 +010075
76/* ASCII to upper case conversion table */
Willy Tarreau0f35c592019-05-15 16:07:36 +020077#define _IST_UC ((const unsigned char[256]){ \
78 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, \
79 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, \
80 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, \
81 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, \
82 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, \
83 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, \
84 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, \
85 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, \
86 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, \
87 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, \
88 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, \
89 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, \
90 0x60, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, \
91 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, \
92 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, \
93 0x58, 0x59, 0x5a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, \
94 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, \
95 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, \
96 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, \
97 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, \
98 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, \
99 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, \
100 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, \
101 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, \
102 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, \
103 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, \
104 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, \
105 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, \
106 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, \
107 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, \
108 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, \
109 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, \
110})
111
112#ifdef USE_OBSOLETE_LINKER
113/* some old linkers and some non-ELF platforms have issues with the weak
114 * attribute so we turn these arrays to literals there.
115 */
116#define ist_lc _IST_LC
117#define ist_uc _IST_UC
118#else
119const unsigned char ist_lc[256] __attribute__((weak)) = _IST_LC;
120const unsigned char ist_uc[256] __attribute__((weak)) = _IST_UC;
121#endif
Willy Tarreaud6735d62018-12-07 09:40:01 +0100122
Willy Tarreaue11f7272017-05-30 17:49:36 +0200123/* This string definition will most often be used to represent a read-only
124 * string returned from a function, based on the starting point and its length
125 * in bytes. No storage is provided, only a pointer and a length. The types
126 * here are important as we only want to have 2 native machine words there so
127 * that on modern architectures the compiler is capable of efficiently
128 * returning a register pair without having to allocate stack room from the
129 * caller. This is done with -freg-struct which is often enabled by default.
130 */
131struct ist {
132 char *ptr;
133 size_t len;
134};
135
Willy Tarreau2ba67272017-09-21 15:24:10 +0200136/* makes a constant ist from a constant string, for use in array declarations */
137#define IST(str) { .ptr = str "", .len = (sizeof str "") - 1 }
138
Tim Duesterhus241e29e2020-03-05 17:56:30 +0100139/* IST_NULL is equivalent to an `ist` with `.ptr = NULL` and `.len = 0` */
140#define IST_NULL ((const struct ist){ .ptr = 0, .len = 0 })
141
Willy Tarreaue11f7272017-05-30 17:49:36 +0200142/* makes an ist from a regular zero terminated string. Null has length 0.
143 * Constants are detected and replaced with constant initializers. Other values
144 * are measured by hand without strlen() as it's much cheaper and inlinable on
145 * small strings. The construct is complex because we must never call
146 * __builtin_strlen() with an expression otherwise it involves a real
147 * measurement.
148 */
149#if __GNUC__ >= 4
150// gcc >= 4 detects constant propagation of str through __x and resolves the
151// length of constant strings easily.
152#define ist(str) ({ \
153 char *__x = (void *)(str); \
154 (struct ist){ \
155 .ptr = __x, \
156 .len = __builtin_constant_p(str) ? \
157 ((void *)str == (void *)0) ? 0 : \
158 __builtin_strlen(__x) : \
159 ({ \
160 size_t __l = 0; \
161 if (__x) for (__l--; __x[++__l]; ) ; \
162 __l; \
163 }) \
164 }; \
165})
166#else
167// gcc < 4 can't do this, and the side effect is a warning each time a NULL is
168// passed to ist() due to the check on __builtin_strlen(). It doesn't have the
169// ability to know that this code is never called.
170#define ist(str) ({ \
171 char *__x = (void *)(str); \
172 (struct ist){ \
173 .ptr = __x, \
174 .len = __builtin_constant_p(str) ? \
175 ((void *)str == (void *)0) ? 0 : \
176 __builtin_strlen(str) : \
177 ({ \
178 size_t __l = 0; \
179 if (__x) for (__l--; __x[++__l]; ) ; \
180 __l; \
181 }) \
182 }; \
183})
184#endif
185
186/* makes an ist struct from a string and a length */
187static inline struct ist ist2(const void *ptr, size_t len)
188{
189 return (struct ist){ .ptr = (char *)ptr, .len = len };
190}
191
Tim Duesterhuse296d3e2020-03-05 17:56:31 +0100192/* returns the result of `ist.ptr != NULL` */
193static inline int isttest(const struct ist ist)
194{
195 return ist.ptr != NULL;
196}
197
Willy Tarreaue67c4e52017-10-19 06:28:23 +0200198/* This function MODIFIES the string to add a zero AFTER the end, and returns
199 * the start pointer. The purpose is to use it on strings extracted by parsers
200 * from larger strings cut with delimiters that are not important and can be
201 * destroyed. It allows any such string to be used with regular string
202 * functions. It's also convenient to use with printf() to show data extracted
203 * from writable areas. The caller is obviously responsible for ensuring that
204 * the string is valid and that the first byte past the end is writable. If
205 * these conditions cannot be satisfied, use istpad() below instead.
206 */
207static inline char *ist0(struct ist ist)
208{
209 ist.ptr[ist.len] = 0;
210 return ist.ptr;
211}
212
Christopher Faulet04179752020-04-21 10:46:43 +0200213/* returns the pointer of the string */
214static inline char *istptr(const struct ist ist)
215{
216 return ist.ptr;
217}
218
Willy Tarreaue11f7272017-05-30 17:49:36 +0200219/* returns the length of the string */
220static inline size_t istlen(const struct ist ist)
221{
222 return ist.len;
223}
224
Christopher Fauletcf266232020-10-22 14:37:12 +0200225/* returns the pointer to the end the string */
226static inline char *istend(const struct ist ist)
227{
228 return (ist.ptr + ist.len);
229}
230
Willy Tarreaue11f7272017-05-30 17:49:36 +0200231/* skips to next character in the string, always stops at the end */
232static inline struct ist istnext(const struct ist ist)
233{
234 struct ist ret = ist;
235
236 if (ret.len) {
237 ret.len--;
238 ret.ptr++;
239 }
240 return ret;
241}
242
243/* copies the contents from string <ist> to buffer <buf> and adds a trailing
244 * zero. The caller must ensure <buf> is large enough.
245 */
246static inline struct ist istpad(void *buf, const struct ist ist)
247{
248 struct ist ret = { .ptr = buf, .len = ist.len };
249
250 for (ret.len = 0; ret.len < ist.len; ret.len++)
251 ret.ptr[ret.len] = ist.ptr[ret.len];
252
253 ret.ptr[ret.len] = 0;
254 return ret;
255}
256
257/* trims string <ist> to no more than <size> characters. The string is
258 * returned.
259 */
260static inline struct ist isttrim(const struct ist ist, size_t size)
261{
262 struct ist ret = ist;
263
264 if (ret.len > size)
265 ret.len = size;
266 return ret;
267}
268
269/* trims string <ist> to no more than <size>-1 characters and ensures that a
270 * zero is placed after <ist.len> (possibly reduced by one) and before <size>,
271 * unless <size> is already zero. The string is returned. This is mostly aimed
272 * at building printable strings that need to be zero-terminated.
273 */
274static inline struct ist istzero(const struct ist ist, size_t size)
275{
276 struct ist ret = ist;
277
278 if (!size)
279 ret.len = 0;
280 else {
281 if (ret.len > size - 1)
282 ret.len = size - 1;
283 ret.ptr[ret.len] = 0;
284 }
285 return ret;
286}
287
288/* returns the ordinal difference between two strings :
289 * < 0 if ist1 < ist2
290 * = 0 if ist1 == ist2
291 * > 0 if ist1 > ist2
292 */
293static inline int istdiff(const struct ist ist1, const struct ist ist2)
294{
295 struct ist l = ist1;
296 struct ist r = ist2;
297
298 do {
299 if (!l.len--)
300 return -r.len;
301 if (!r.len--)
302 return 1;
303 } while (*l.ptr++ == *r.ptr++);
304
305 return *(unsigned char *)(l.ptr - 1) - *(unsigned char *)(r.ptr - 1);
306}
307
308/* returns non-zero if <ist1> starts like <ist2> (empty strings do match) */
309static inline int istmatch(const struct ist ist1, const struct ist ist2)
310{
311 struct ist l = ist1;
312 struct ist r = ist2;
313
314 if (l.len < r.len)
315 return 0;
316
317 while (r.len--) {
318 if (*l.ptr++ != *r.ptr++)
319 return 0;
320 }
321 return 1;
322}
323
Remi Tricot-Le Bretonbb4582c2020-10-28 11:35:14 +0100324/* returns non-zero if <ist1> starts like <ist2>, ignoring the case (empty strings do match) */
325static inline int istmatchi(const struct ist ist1, const struct ist ist2)
326{
327 struct ist l = ist1;
328 struct ist r = ist2;
329
330 if (l.len < r.len)
331 return 0;
332
333 while (r.len--) {
334 if (*l.ptr != *r.ptr &&
335 ist_lc[(unsigned char)*l.ptr] != ist_lc[(unsigned char)*r.ptr])
336 return 0;
337
338 l.ptr++;
339 r.ptr++;
340 }
341 return 1;
342}
343
Willy Tarreaue11f7272017-05-30 17:49:36 +0200344/* returns non-zero if <ist1> starts like <ist2> on the first <count>
345 * characters (empty strings do match).
346 */
347static inline int istnmatch(const struct ist ist1, const struct ist ist2, size_t count)
348{
349 struct ist l = ist1;
350 struct ist r = ist2;
351
352 if (l.len > count)
353 l.len = count;
354 if (r.len > count)
355 r.len = count;
356 return istmatch(l, r);
357}
358
359/* returns non-zero if <ist1> equals <ist2> (empty strings are equal) */
360static inline int isteq(const struct ist ist1, const struct ist ist2)
361{
362 struct ist l = ist1;
363 struct ist r = ist2;
364
365 if (l.len != r.len)
366 return 0;
367
368 while (l.len--) {
369 if (*l.ptr++ != *r.ptr++)
370 return 0;
371 }
372 return 1;
373}
374
Christopher Faulet20761452018-06-06 16:33:53 +0200375/* returns non-zero if <ist1> equals <ist2>, ignoring the case (empty strings are equal) */
376static inline int isteqi(const struct ist ist1, const struct ist ist2)
377{
378 struct ist l = ist1;
379 struct ist r = ist2;
380
381 if (l.len != r.len)
382 return 0;
383
384 while (l.len--) {
Willy Tarreaud6735d62018-12-07 09:40:01 +0100385 if (*l.ptr != *r.ptr &&
386 ist_lc[(unsigned char)*l.ptr] != ist_lc[(unsigned char)*r.ptr])
Christopher Faulet20761452018-06-06 16:33:53 +0200387 return 0;
Willy Tarreaud6735d62018-12-07 09:40:01 +0100388
Christopher Faulet20761452018-06-06 16:33:53 +0200389 l.ptr++;
390 r.ptr++;
391 }
392 return 1;
393}
394
Willy Tarreaue11f7272017-05-30 17:49:36 +0200395/* returns non-zero if <ist1> equals <ist2> on the first <count> characters
396 * (empty strings are equal).
397 */
398static inline int istneq(const struct ist ist1, const struct ist ist2, size_t count)
399{
400 struct ist l = ist1;
401 struct ist r = ist2;
402
403 if (l.len > count)
404 l.len = count;
405 if (r.len > count)
406 r.len = count;
407 return isteq(l, r);
408}
409
Tim Duesterhus551eeae2021-04-05 17:53:54 +0200410/* appends <src> after <dst>. The caller must ensure that the underlying buffer
411 * is large enough to fit the character.
412 */
413static inline struct ist istappend(struct ist dst, const char src)
414{
415 dst.ptr[dst.len++] = src;
416
417 return dst;
418}
419
Willy Tarreaue11f7272017-05-30 17:49:36 +0200420/* copies <src> over <dst> for a maximum of <count> bytes. Returns the number
421 * of characters copied (src.len), or -1 if it does not fit. In all cases, the
422 * contents are copied prior to reporting an error, so that the destination
423 * at least contains a valid but truncated string.
424 */
425static inline ssize_t istcpy(struct ist *dst, const struct ist src, size_t count)
426{
427 dst->len = 0;
428
429 if (count > src.len)
430 count = src.len;
431
432 while (dst->len < count) {
433 dst->ptr[dst->len] = src.ptr[dst->len];
434 dst->len++;
435 }
436
437 if (dst->len == src.len)
438 return src.len;
439
440 return -1;
441}
442
443/* copies <src> over <dst> for a maximum of <count> bytes. Returns the number
444 * of characters copied, or -1 if it does not fit. A (possibly truncated) valid
445 * copy of <src> is always left into <dst>, and a trailing \0 is appended as
446 * long as <count> is not null, even if that results in reducing the string by
447 * one character.
448 */
449static inline ssize_t istscpy(struct ist *dst, const struct ist src, size_t count)
450{
451 dst->len = 0;
452
453 if (!count)
454 goto fail;
455
456 if (count > src.len)
457 count = src.len + 1;
458
459 while (dst->len < count - 1) {
460 dst->ptr[dst->len] = src.ptr[dst->len];
461 dst->len++;
462 }
463
464 dst->ptr[dst->len] = 0;
465 if (dst->len == src.len)
466 return src.len;
467 fail:
468 return -1;
469}
470
471/* appends <src> after <dst> for a maximum of <count> total bytes in <dst> after
472 * the copy. <dst> is assumed to be <count> or less before the call. The new
473 * string's length is returned, or -1 if a truncation happened. In all cases,
474 * the contents are copied prior to reporting an error, so that the destination
475 * at least contains a valid but truncated string.
476 */
477static inline ssize_t istcat(struct ist *dst, const struct ist src, size_t count)
478{
479 const char *s = src.ptr;
480
481 while (dst->len < count && s != src.ptr + src.len)
482 dst->ptr[dst->len++] = *s++;
483
484 if (s == src.ptr + src.len)
485 return dst->len;
486
487 return -1;
488}
489
490/* appends <src> after <dst> for a maximum of <count> total bytes in <dst> after
491 * the copy. <dst> is assumed to be <count> or less before the call. The new
492 * string's length is returned, or -1 if a truncation happened. In all cases,
493 * the contents are copied prior to reporting an error, so that the destination
494 * at least contains a valid but truncated string.
495 */
496static inline ssize_t istscat(struct ist *dst, const struct ist src, size_t count)
497{
498 const char *s = src.ptr;
499
500 if (!count)
501 goto fail;
502
503 while (dst->len < count - 1 && s != src.ptr + src.len) {
504 dst->ptr[dst->len++] = *s++;
505 }
506
507 dst->ptr[dst->len] = 0;
508 if (s == src.ptr + src.len)
509 return dst->len;
510 fail:
511 return -1;
512}
513
Willy Tarreau3f2d6962018-12-07 08:35:07 +0100514/* copies the entire <src> over <dst>, which must be allocated large enough to
515 * hold the whole contents. No trailing zero is appended, this is mainly used
516 * for protocol processing where the frame length has already been checked. An
517 * ist made of the output and its length are returned. The destination is not
518 * touched if src.len is null.
519 */
520static inline struct ist ist2bin(char *dst, const struct ist src)
521{
522 size_t ofs = 0;
523
524 /* discourage the compiler from trying to optimize for large strings,
525 * but tell it that most of our strings are not empty.
526 */
527 if (__builtin_expect(ofs < src.len, 1)) {
528 do {
529 dst[ofs] = src.ptr[ofs];
530 ofs++;
531 } while (__builtin_expect(ofs < src.len, 0));
532 }
533 return ist2(dst, ofs);
534}
535
536/* copies the entire <src> over <dst>, which must be allocated large enough to
537 * hold the whole contents as well as a trailing zero which is always appended.
538 * This is mainly used for protocol conversions where the frame length has
539 * already been checked. An ist made of the output and its length (not counting
540 * the trailing zero) are returned.
541 */
542static inline struct ist ist2str(char *dst, const struct ist src, size_t count)
543{
544 size_t ofs = 0;
545
546 /* discourage the compiler from trying to optimize for large strings,
547 * but tell it that most of our strings are not empty.
548 */
549 if (__builtin_expect(ofs < src.len, 1)) {
550 do {
551 dst[ofs] = src.ptr[ofs];
552 ofs++;
553 } while (__builtin_expect(ofs < src.len, 0));
554 }
555 dst[ofs] = 0;
556 return ist2(dst, ofs);
557}
558
559/* makes a lower case copy of the entire <src> into <dst>, which must have been
560 * allocated large enough to hold the whole contents. No trailing zero is
561 * appended, this is mainly used for protocol processing where the frame length
562 * has already been checked. An ist made of the output and its length are
563 * returned. The destination is not touched if src.len is null.
564 */
565static inline struct ist ist2bin_lc(char *dst, const struct ist src)
566{
567 size_t ofs = 0;
568
569 /* discourage the compiler from trying to optimize for large strings,
570 * but tell it that most of our strings are not empty.
571 */
572 if (__builtin_expect(ofs < src.len, 1)) {
573 do {
Willy Tarreaud6735d62018-12-07 09:40:01 +0100574 dst[ofs] = ist_lc[(unsigned char)src.ptr[ofs]];
Willy Tarreau3f2d6962018-12-07 08:35:07 +0100575 ofs++;
576 } while (__builtin_expect(ofs < src.len, 0));
577 }
578 return ist2(dst, ofs);
579}
580
581/* makes a lower case copy of the entire <src> into <dst>, which must have been
582 * allocated large enough to hold the whole contents as well as a trailing zero
583 * which is always appended. This is mainly used for protocol conversions where
584 * the frame length has already been checked. An ist made of the output and its
585 * length (not counting the trailing zero) are returned.
586 */
587static inline struct ist ist2str_lc(char *dst, const struct ist src, size_t count)
588{
589 size_t ofs = 0;
590
591 /* discourage the compiler from trying to optimize for large strings,
592 * but tell it that most of our strings are not empty.
593 */
594 if (__builtin_expect(ofs < src.len, 1)) {
595 do {
Willy Tarreaud6735d62018-12-07 09:40:01 +0100596 dst[ofs] = ist_lc[(unsigned char)src.ptr[ofs]];
Willy Tarreau3f2d6962018-12-07 08:35:07 +0100597 ofs++;
598 } while (__builtin_expect(ofs < src.len, 0));
599 }
600 dst[ofs] = 0;
601 return ist2(dst, ofs);
602}
603
604/* makes an upper case copy of the entire <src> into <dst>, which must have
605 * been allocated large enough to hold the whole contents. No trailing zero is
606 * appended, this is mainly used for protocol processing where the frame length
607 * has already been checked. An ist made of the output and its length are
608 * returned. The destination is not touched if src.len is null.
609 */
610static inline struct ist ist2bin_uc(char *dst, const struct ist src)
611{
612 size_t ofs = 0;
613
614 /* discourage the compiler from trying to optimize for large strings,
615 * but tell it that most of our strings are not empty.
616 */
617 if (__builtin_expect(ofs < src.len, 1)) {
618 do {
Willy Tarreaud6735d62018-12-07 09:40:01 +0100619 dst[ofs] = ist_uc[(unsigned char)src.ptr[ofs]];
Willy Tarreau3f2d6962018-12-07 08:35:07 +0100620 ofs++;
621 } while (__builtin_expect(ofs < src.len, 0));
622 }
623 return ist2(dst, ofs);
624}
625
626/* makes an upper case copy of the entire <src> into <dst>, which must have been
627 * allocated large enough to hold the whole contents as well as a trailing zero
628 * which is always appended. This is mainly used for protocol conversions where
629 * the frame length has already been checked. An ist made of the output and its
630 * length (not counting the trailing zero) are returned.
631 */
632static inline struct ist ist2str_uc(char *dst, const struct ist src, size_t count)
633{
634 size_t ofs = 0;
635
636 /* discourage the compiler from trying to optimize for large strings,
637 * but tell it that most of our strings are not empty.
638 */
639 if (__builtin_expect(ofs < src.len, 1)) {
640 do {
Willy Tarreaud6735d62018-12-07 09:40:01 +0100641 dst[ofs] = ist_uc[(unsigned char)src.ptr[ofs]];
Willy Tarreau3f2d6962018-12-07 08:35:07 +0100642 ofs++;
643 } while (__builtin_expect(ofs < src.len, 0));
644 }
645 dst[ofs] = 0;
646 return ist2(dst, ofs);
647}
648
Willy Tarreaue11f7272017-05-30 17:49:36 +0200649/* looks for first occurrence of character <chr> in string <ist>. Returns the
650 * pointer if found, or NULL if not found.
651 */
652static inline char *istchr(const struct ist ist, char chr)
653{
654 char *s = ist.ptr;
655
656 do {
657 if (s >= ist.ptr + ist.len)
658 return NULL;
659 } while (*s++ != chr);
660 return s - 1;
661}
662
Willy Tarreau8f3ce062019-11-22 15:58:53 +0100663/* Returns a pointer to the first control character found in <ist>, or NULL if
664 * none is present. A control character is defined as a byte whose value is
665 * between 0x00 and 0x1F included. The function is optimized for strings having
666 * no CTL chars by processing up to sizeof(long) bytes at once on architectures
667 * supporting efficient unaligned accesses. Despite this it is not very fast
668 * (~0.43 byte/cycle) and should mostly be used on low match probability when
669 * it can save a call to a much slower function.
670 */
671static inline const char *ist_find_ctl(const struct ist ist)
672{
673 const union { unsigned long v; } __attribute__((packed)) *u;
674 const char *curr = (void *)ist.ptr - sizeof(long);
675 const char *last = curr + ist.len;
676 unsigned long l1, l2;
677
678 do {
679 curr += sizeof(long);
680 if (curr > last)
681 break;
682 u = (void *)curr;
683 /* subtract 0x202020...20 to the value to generate a carry in
684 * the lower byte if the byte contains a lower value. If we
685 * generate a bit 7 that was not there, it means the byte was
686 * within 0x00..0x1F.
687 */
688 l2 = u->v;
689 l1 = ~l2 & ((~0UL / 255) * 0x80); /* 0x808080...80 */
690 l2 -= (~0UL / 255) * 0x20; /* 0x202020...20 */
691 } while ((l1 & l2) == 0);
692
693 last += sizeof(long);
694 if (__builtin_expect(curr < last, 0)) {
695 do {
Willy Tarreaueb6f7012020-05-27 16:21:26 +0200696 if ((unsigned char)*curr < 0x20)
Willy Tarreau8f3ce062019-11-22 15:58:53 +0100697 return curr;
698 curr++;
699 } while (curr < last);
700 }
701 return NULL;
702}
703
Willy Tarreaue11f7272017-05-30 17:49:36 +0200704/* looks for first occurrence of character <chr> in string <ist> and returns
705 * the tail of the string starting with this character, or (ist.end,0) if not
706 * found.
707 */
708static inline struct ist istfind(const struct ist ist, char chr)
709{
710 struct ist ret = ist;
711
712 while (ret.len--) {
713 if (*ret.ptr++ == chr)
714 return ist2(ret.ptr - 1, ret.len + 1);
715 }
716 return ist2(ret.ptr, 0);
717}
718
719/* looks for first occurrence of character different from <chr> in string <ist>
720 * and returns the tail of the string starting at this character, or (ist_end,0)
721 * if not found.
722 */
723static inline struct ist istskip(const struct ist ist, char chr)
724{
725 struct ist ret = ist;
726
727 while (ret.len--) {
728 if (*ret.ptr++ != chr)
729 return ist2(ret.ptr - 1, ret.len + 1);
730 }
731 return ist2(ret.ptr, 0);
732}
733
734/* looks for first occurrence of string <pat> in string <ist> and returns the
735 * tail of the string starting at this position, or (NULL,0) if not found. The
736 * empty pattern is found everywhere.
737 */
738static inline struct ist istist(const struct ist ist, const struct ist pat)
739{
740 struct ist ret = ist;
741 size_t pos;
742
743 if (!pat.len)
744 return ret;
745
746 while (1) {
747 loop:
748 ret = istfind(ret, *pat.ptr);
749 if (ret.len < pat.len)
750 break;
751
752 /* ret.len >= 1, pat.len >= 1 and *ret.ptr == *pat.ptr */
753
754 ret = istnext(ret);
755 for (pos = 0; pos < pat.len - 1; ) {
756 ++pos;
757 if (ret.ptr[pos - 1] != pat.ptr[pos])
758 goto loop;
759 }
760 return ist2(ret.ptr - 1, ret.len + 1);
761 }
Tim Duesterhus241e29e2020-03-05 17:56:30 +0100762 return IST_NULL;
Willy Tarreaue11f7272017-05-30 17:49:36 +0200763}
764
Jerome Magnin9dde0b22020-02-21 10:33:12 +0100765/*
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +0500766 * looks for the first occurrence of <chr> in string <ist> and returns a shorter
Jerome Magnin9dde0b22020-02-21 10:33:12 +0100767 * ist if char is found.
768 */
769static inline struct ist iststop(const struct ist ist, char chr)
770{
771 size_t len = 0;
772
773 while (len++ < ist.len && ist.ptr[len - 1] != chr)
774 ;
775 return ist2(ist.ptr, len - 1);
776}
Tim Duesterhus35005d02020-03-05 17:56:32 +0100777
Baptiste Assmann9ef19672020-03-29 08:10:16 +0200778/*
779 * advance <.ptr> by <nb> characters.
780 * If <ist> is too short, (ist.end,0) is returned.
781 */
782static inline struct ist istadv(const struct ist ist, const size_t nb)
783{
784 if (ist.len < nb)
785 return ist2(ist.ptr + ist.len, 0);
786 return ist2(ist.ptr + nb, ist.len - nb);
787}
788
Baptiste Assmannde802012020-03-29 09:10:27 +0200789/*
790 * compare 2 ists and return non-zero if they are the same
791 */
792static inline int istissame(const struct ist ist1, const struct ist ist2)
793{
794 return ((ist1.ptr == ist2.ptr) && (ist1.len == ist2.len));
795}
796
Tim Duesterhus35005d02020-03-05 17:56:32 +0100797#ifndef IST_FREESTANDING
798/* This function allocates <size> bytes and returns an `ist` pointing to
799 * the allocated area with size `0`.
800 *
801 * If this function fails to allocate memory the return value is equivalent
802 * to IST_NULL.
803 */
804static inline struct ist istalloc(const size_t size)
805{
806 return ist2(malloc(size), 0);
807}
808
809/* This function performs the equivalent of free() on the given <ist>.
810 *
811 * After this function returns the value of the given <ist> will be
812 * modified to be equivalent to IST_NULL.
813 */
814static inline void istfree(struct ist *ist)
815{
816 free(ist->ptr);
817 *ist = IST_NULL;
818}
Tim Duesterhus9576ab72020-03-05 17:56:34 +0100819
820/* This function performs the equivalent of strdup() on the given <src>.
821 *
822 * If this function fails to allocate memory the return value is equivalent
823 * to IST_NULL.
824 */
825static inline struct ist istdup(const struct ist src)
826{
827 const size_t src_size = src.len;
828
829 /* Allocate at least 1 byte to allow duplicating an empty string with
830 * malloc implementations that return NULL for a 0-size allocation.
831 */
Willy Tarreaueb6f7012020-05-27 16:21:26 +0200832 struct ist dst = istalloc(src_size ? src_size : 1);
Tim Duesterhus9576ab72020-03-05 17:56:34 +0100833
834 if (isttest(dst)) {
835 istcpy(&dst, src, src_size);
836 }
837
838 return dst;
839}
Tim Duesterhus35005d02020-03-05 17:56:32 +0100840#endif
841
Willy Tarreaue11f7272017-05-30 17:49:36 +0200842#endif