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