Willy Tarreau | e11f727 | 2017-05-30 17:49:36 +0200 | [diff] [blame] | 1 | /* |
| 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 Faulet | 2076145 | 2018-06-06 16:33:53 +0200 | [diff] [blame] | 31 | #include <ctype.h> |
Willy Tarreau | e11f727 | 2017-05-30 17:49:36 +0200 | [diff] [blame] | 32 | #include <string.h> |
Willy Tarreau | a7280a1 | 2018-11-26 19:41:40 +0100 | [diff] [blame] | 33 | #include <unistd.h> |
Willy Tarreau | e11f727 | 2017-05-30 17:49:36 +0200 | [diff] [blame] | 34 | |
Tim Duesterhus | 35005d0 | 2020-03-05 17:56:32 +0100 | [diff] [blame] | 35 | #ifndef IST_FREESTANDING |
| 36 | #include <stdlib.h> |
| 37 | #endif |
| 38 | |
Willy Tarreau | e11f727 | 2017-05-30 17:49:36 +0200 | [diff] [blame] | 39 | #include <common/config.h> |
| 40 | |
Willy Tarreau | d6735d6 | 2018-12-07 09:40:01 +0100 | [diff] [blame] | 41 | /* ASCII to lower case conversion table */ |
Willy Tarreau | 0f35c59 | 2019-05-15 16:07:36 +0200 | [diff] [blame] | 42 | #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 Tarreau | d6735d6 | 2018-12-07 09:40:01 +0100 | [diff] [blame] | 76 | |
| 77 | /* ASCII to upper case conversion table */ |
Willy Tarreau | 0f35c59 | 2019-05-15 16:07:36 +0200 | [diff] [blame] | 78 | #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 |
| 120 | const unsigned char ist_lc[256] __attribute__((weak)) = _IST_LC; |
| 121 | const unsigned char ist_uc[256] __attribute__((weak)) = _IST_UC; |
| 122 | #endif |
Willy Tarreau | d6735d6 | 2018-12-07 09:40:01 +0100 | [diff] [blame] | 123 | |
Willy Tarreau | e11f727 | 2017-05-30 17:49:36 +0200 | [diff] [blame] | 124 | /* 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 | */ |
| 132 | struct ist { |
| 133 | char *ptr; |
| 134 | size_t len; |
| 135 | }; |
| 136 | |
Willy Tarreau | 2ba6727 | 2017-09-21 15:24:10 +0200 | [diff] [blame] | 137 | /* 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 Duesterhus | 241e29e | 2020-03-05 17:56:30 +0100 | [diff] [blame] | 140 | /* 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 Tarreau | e11f727 | 2017-05-30 17:49:36 +0200 | [diff] [blame] | 143 | /* 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 */ |
| 188 | static inline struct ist ist2(const void *ptr, size_t len) |
| 189 | { |
| 190 | return (struct ist){ .ptr = (char *)ptr, .len = len }; |
| 191 | } |
| 192 | |
Tim Duesterhus | e296d3e | 2020-03-05 17:56:31 +0100 | [diff] [blame] | 193 | /* returns the result of `ist.ptr != NULL` */ |
| 194 | static inline int isttest(const struct ist ist) |
| 195 | { |
| 196 | return ist.ptr != NULL; |
| 197 | } |
| 198 | |
Willy Tarreau | e67c4e5 | 2017-10-19 06:28:23 +0200 | [diff] [blame] | 199 | /* 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 | */ |
| 208 | static inline char *ist0(struct ist ist) |
| 209 | { |
| 210 | ist.ptr[ist.len] = 0; |
| 211 | return ist.ptr; |
| 212 | } |
| 213 | |
Christopher Faulet | 0417975 | 2020-04-21 10:46:43 +0200 | [diff] [blame] | 214 | /* returns the pointer of the string */ |
| 215 | static inline char *istptr(const struct ist ist) |
| 216 | { |
| 217 | return ist.ptr; |
| 218 | } |
| 219 | |
Willy Tarreau | e11f727 | 2017-05-30 17:49:36 +0200 | [diff] [blame] | 220 | /* returns the length of the string */ |
| 221 | static inline size_t istlen(const struct ist ist) |
| 222 | { |
| 223 | return ist.len; |
| 224 | } |
| 225 | |
| 226 | /* skips to next character in the string, always stops at the end */ |
| 227 | static inline struct ist istnext(const struct ist ist) |
| 228 | { |
| 229 | struct ist ret = ist; |
| 230 | |
| 231 | if (ret.len) { |
| 232 | ret.len--; |
| 233 | ret.ptr++; |
| 234 | } |
| 235 | return ret; |
| 236 | } |
| 237 | |
| 238 | /* copies the contents from string <ist> to buffer <buf> and adds a trailing |
| 239 | * zero. The caller must ensure <buf> is large enough. |
| 240 | */ |
| 241 | static inline struct ist istpad(void *buf, const struct ist ist) |
| 242 | { |
| 243 | struct ist ret = { .ptr = buf, .len = ist.len }; |
| 244 | |
| 245 | for (ret.len = 0; ret.len < ist.len; ret.len++) |
| 246 | ret.ptr[ret.len] = ist.ptr[ret.len]; |
| 247 | |
| 248 | ret.ptr[ret.len] = 0; |
| 249 | return ret; |
| 250 | } |
| 251 | |
| 252 | /* trims string <ist> to no more than <size> characters. The string is |
| 253 | * returned. |
| 254 | */ |
| 255 | static inline struct ist isttrim(const struct ist ist, size_t size) |
| 256 | { |
| 257 | struct ist ret = ist; |
| 258 | |
| 259 | if (ret.len > size) |
| 260 | ret.len = size; |
| 261 | return ret; |
| 262 | } |
| 263 | |
| 264 | /* trims string <ist> to no more than <size>-1 characters and ensures that a |
| 265 | * zero is placed after <ist.len> (possibly reduced by one) and before <size>, |
| 266 | * unless <size> is already zero. The string is returned. This is mostly aimed |
| 267 | * at building printable strings that need to be zero-terminated. |
| 268 | */ |
| 269 | static inline struct ist istzero(const struct ist ist, size_t size) |
| 270 | { |
| 271 | struct ist ret = ist; |
| 272 | |
| 273 | if (!size) |
| 274 | ret.len = 0; |
| 275 | else { |
| 276 | if (ret.len > size - 1) |
| 277 | ret.len = size - 1; |
| 278 | ret.ptr[ret.len] = 0; |
| 279 | } |
| 280 | return ret; |
| 281 | } |
| 282 | |
| 283 | /* returns the ordinal difference between two strings : |
| 284 | * < 0 if ist1 < ist2 |
| 285 | * = 0 if ist1 == ist2 |
| 286 | * > 0 if ist1 > ist2 |
| 287 | */ |
| 288 | static inline int istdiff(const struct ist ist1, const struct ist ist2) |
| 289 | { |
| 290 | struct ist l = ist1; |
| 291 | struct ist r = ist2; |
| 292 | |
| 293 | do { |
| 294 | if (!l.len--) |
| 295 | return -r.len; |
| 296 | if (!r.len--) |
| 297 | return 1; |
| 298 | } while (*l.ptr++ == *r.ptr++); |
| 299 | |
| 300 | return *(unsigned char *)(l.ptr - 1) - *(unsigned char *)(r.ptr - 1); |
| 301 | } |
| 302 | |
| 303 | /* returns non-zero if <ist1> starts like <ist2> (empty strings do match) */ |
| 304 | static inline int istmatch(const struct ist ist1, const struct ist ist2) |
| 305 | { |
| 306 | struct ist l = ist1; |
| 307 | struct ist r = ist2; |
| 308 | |
| 309 | if (l.len < r.len) |
| 310 | return 0; |
| 311 | |
| 312 | while (r.len--) { |
| 313 | if (*l.ptr++ != *r.ptr++) |
| 314 | return 0; |
| 315 | } |
| 316 | return 1; |
| 317 | } |
| 318 | |
| 319 | /* returns non-zero if <ist1> starts like <ist2> on the first <count> |
| 320 | * characters (empty strings do match). |
| 321 | */ |
| 322 | static inline int istnmatch(const struct ist ist1, const struct ist ist2, size_t count) |
| 323 | { |
| 324 | struct ist l = ist1; |
| 325 | struct ist r = ist2; |
| 326 | |
| 327 | if (l.len > count) |
| 328 | l.len = count; |
| 329 | if (r.len > count) |
| 330 | r.len = count; |
| 331 | return istmatch(l, r); |
| 332 | } |
| 333 | |
| 334 | /* returns non-zero if <ist1> equals <ist2> (empty strings are equal) */ |
| 335 | static inline int isteq(const struct ist ist1, const struct ist ist2) |
| 336 | { |
| 337 | struct ist l = ist1; |
| 338 | struct ist r = ist2; |
| 339 | |
| 340 | if (l.len != r.len) |
| 341 | return 0; |
| 342 | |
| 343 | while (l.len--) { |
| 344 | if (*l.ptr++ != *r.ptr++) |
| 345 | return 0; |
| 346 | } |
| 347 | return 1; |
| 348 | } |
| 349 | |
Christopher Faulet | 2076145 | 2018-06-06 16:33:53 +0200 | [diff] [blame] | 350 | /* returns non-zero if <ist1> equals <ist2>, ignoring the case (empty strings are equal) */ |
| 351 | static inline int isteqi(const struct ist ist1, const struct ist ist2) |
| 352 | { |
| 353 | struct ist l = ist1; |
| 354 | struct ist r = ist2; |
| 355 | |
| 356 | if (l.len != r.len) |
| 357 | return 0; |
| 358 | |
| 359 | while (l.len--) { |
Willy Tarreau | d6735d6 | 2018-12-07 09:40:01 +0100 | [diff] [blame] | 360 | if (*l.ptr != *r.ptr && |
| 361 | ist_lc[(unsigned char)*l.ptr] != ist_lc[(unsigned char)*r.ptr]) |
Christopher Faulet | 2076145 | 2018-06-06 16:33:53 +0200 | [diff] [blame] | 362 | return 0; |
Willy Tarreau | d6735d6 | 2018-12-07 09:40:01 +0100 | [diff] [blame] | 363 | |
Christopher Faulet | 2076145 | 2018-06-06 16:33:53 +0200 | [diff] [blame] | 364 | l.ptr++; |
| 365 | r.ptr++; |
| 366 | } |
| 367 | return 1; |
| 368 | } |
| 369 | |
Willy Tarreau | e11f727 | 2017-05-30 17:49:36 +0200 | [diff] [blame] | 370 | /* returns non-zero if <ist1> equals <ist2> on the first <count> characters |
| 371 | * (empty strings are equal). |
| 372 | */ |
| 373 | static inline int istneq(const struct ist ist1, const struct ist ist2, size_t count) |
| 374 | { |
| 375 | struct ist l = ist1; |
| 376 | struct ist r = ist2; |
| 377 | |
| 378 | if (l.len > count) |
| 379 | l.len = count; |
| 380 | if (r.len > count) |
| 381 | r.len = count; |
| 382 | return isteq(l, r); |
| 383 | } |
| 384 | |
| 385 | /* copies <src> over <dst> for a maximum of <count> bytes. Returns the number |
| 386 | * of characters copied (src.len), or -1 if it does not fit. In all cases, the |
| 387 | * contents are copied prior to reporting an error, so that the destination |
| 388 | * at least contains a valid but truncated string. |
| 389 | */ |
| 390 | static inline ssize_t istcpy(struct ist *dst, const struct ist src, size_t count) |
| 391 | { |
| 392 | dst->len = 0; |
| 393 | |
| 394 | if (count > src.len) |
| 395 | count = src.len; |
| 396 | |
| 397 | while (dst->len < count) { |
| 398 | dst->ptr[dst->len] = src.ptr[dst->len]; |
| 399 | dst->len++; |
| 400 | } |
| 401 | |
| 402 | if (dst->len == src.len) |
| 403 | return src.len; |
| 404 | |
| 405 | return -1; |
| 406 | } |
| 407 | |
| 408 | /* copies <src> over <dst> for a maximum of <count> bytes. Returns the number |
| 409 | * of characters copied, or -1 if it does not fit. A (possibly truncated) valid |
| 410 | * copy of <src> is always left into <dst>, and a trailing \0 is appended as |
| 411 | * long as <count> is not null, even if that results in reducing the string by |
| 412 | * one character. |
| 413 | */ |
| 414 | static inline ssize_t istscpy(struct ist *dst, const struct ist src, size_t count) |
| 415 | { |
| 416 | dst->len = 0; |
| 417 | |
| 418 | if (!count) |
| 419 | goto fail; |
| 420 | |
| 421 | if (count > src.len) |
| 422 | count = src.len + 1; |
| 423 | |
| 424 | while (dst->len < count - 1) { |
| 425 | dst->ptr[dst->len] = src.ptr[dst->len]; |
| 426 | dst->len++; |
| 427 | } |
| 428 | |
| 429 | dst->ptr[dst->len] = 0; |
| 430 | if (dst->len == src.len) |
| 431 | return src.len; |
| 432 | fail: |
| 433 | return -1; |
| 434 | } |
| 435 | |
| 436 | /* appends <src> after <dst> for a maximum of <count> total bytes in <dst> after |
| 437 | * the copy. <dst> is assumed to be <count> or less before the call. The new |
| 438 | * string's length is returned, or -1 if a truncation happened. In all cases, |
| 439 | * the contents are copied prior to reporting an error, so that the destination |
| 440 | * at least contains a valid but truncated string. |
| 441 | */ |
| 442 | static inline ssize_t istcat(struct ist *dst, const struct ist src, size_t count) |
| 443 | { |
| 444 | const char *s = src.ptr; |
| 445 | |
| 446 | while (dst->len < count && s != src.ptr + src.len) |
| 447 | dst->ptr[dst->len++] = *s++; |
| 448 | |
| 449 | if (s == src.ptr + src.len) |
| 450 | return dst->len; |
| 451 | |
| 452 | return -1; |
| 453 | } |
| 454 | |
| 455 | /* appends <src> after <dst> for a maximum of <count> total bytes in <dst> after |
| 456 | * the copy. <dst> is assumed to be <count> or less before the call. The new |
| 457 | * string's length is returned, or -1 if a truncation happened. In all cases, |
| 458 | * the contents are copied prior to reporting an error, so that the destination |
| 459 | * at least contains a valid but truncated string. |
| 460 | */ |
| 461 | static inline ssize_t istscat(struct ist *dst, const struct ist src, size_t count) |
| 462 | { |
| 463 | const char *s = src.ptr; |
| 464 | |
| 465 | if (!count) |
| 466 | goto fail; |
| 467 | |
| 468 | while (dst->len < count - 1 && s != src.ptr + src.len) { |
| 469 | dst->ptr[dst->len++] = *s++; |
| 470 | } |
| 471 | |
| 472 | dst->ptr[dst->len] = 0; |
| 473 | if (s == src.ptr + src.len) |
| 474 | return dst->len; |
| 475 | fail: |
| 476 | return -1; |
| 477 | } |
| 478 | |
Willy Tarreau | 3f2d696 | 2018-12-07 08:35:07 +0100 | [diff] [blame] | 479 | /* copies the entire <src> over <dst>, which must be allocated large enough to |
| 480 | * hold the whole contents. No trailing zero is appended, this is mainly used |
| 481 | * for protocol processing where the frame length has already been checked. An |
| 482 | * ist made of the output and its length are returned. The destination is not |
| 483 | * touched if src.len is null. |
| 484 | */ |
| 485 | static inline struct ist ist2bin(char *dst, const struct ist src) |
| 486 | { |
| 487 | size_t ofs = 0; |
| 488 | |
| 489 | /* discourage the compiler from trying to optimize for large strings, |
| 490 | * but tell it that most of our strings are not empty. |
| 491 | */ |
| 492 | if (__builtin_expect(ofs < src.len, 1)) { |
| 493 | do { |
| 494 | dst[ofs] = src.ptr[ofs]; |
| 495 | ofs++; |
| 496 | } while (__builtin_expect(ofs < src.len, 0)); |
| 497 | } |
| 498 | return ist2(dst, ofs); |
| 499 | } |
| 500 | |
| 501 | /* copies the entire <src> over <dst>, which must be allocated large enough to |
| 502 | * hold the whole contents as well as a trailing zero which is always appended. |
| 503 | * This is mainly used for protocol conversions where the frame length has |
| 504 | * already been checked. An ist made of the output and its length (not counting |
| 505 | * the trailing zero) are returned. |
| 506 | */ |
| 507 | static inline struct ist ist2str(char *dst, const struct ist src, size_t count) |
| 508 | { |
| 509 | size_t ofs = 0; |
| 510 | |
| 511 | /* discourage the compiler from trying to optimize for large strings, |
| 512 | * but tell it that most of our strings are not empty. |
| 513 | */ |
| 514 | if (__builtin_expect(ofs < src.len, 1)) { |
| 515 | do { |
| 516 | dst[ofs] = src.ptr[ofs]; |
| 517 | ofs++; |
| 518 | } while (__builtin_expect(ofs < src.len, 0)); |
| 519 | } |
| 520 | dst[ofs] = 0; |
| 521 | return ist2(dst, ofs); |
| 522 | } |
| 523 | |
| 524 | /* makes a lower case copy of the entire <src> into <dst>, which must have been |
| 525 | * allocated large enough to hold the whole contents. No trailing zero is |
| 526 | * appended, this is mainly used for protocol processing where the frame length |
| 527 | * has already been checked. An ist made of the output and its length are |
| 528 | * returned. The destination is not touched if src.len is null. |
| 529 | */ |
| 530 | static inline struct ist ist2bin_lc(char *dst, const struct ist src) |
| 531 | { |
| 532 | size_t ofs = 0; |
| 533 | |
| 534 | /* discourage the compiler from trying to optimize for large strings, |
| 535 | * but tell it that most of our strings are not empty. |
| 536 | */ |
| 537 | if (__builtin_expect(ofs < src.len, 1)) { |
| 538 | do { |
Willy Tarreau | d6735d6 | 2018-12-07 09:40:01 +0100 | [diff] [blame] | 539 | dst[ofs] = ist_lc[(unsigned char)src.ptr[ofs]]; |
Willy Tarreau | 3f2d696 | 2018-12-07 08:35:07 +0100 | [diff] [blame] | 540 | ofs++; |
| 541 | } while (__builtin_expect(ofs < src.len, 0)); |
| 542 | } |
| 543 | return ist2(dst, ofs); |
| 544 | } |
| 545 | |
| 546 | /* makes a lower case copy of the entire <src> into <dst>, which must have been |
| 547 | * allocated large enough to hold the whole contents as well as a trailing zero |
| 548 | * which is always appended. This is mainly used for protocol conversions where |
| 549 | * the frame length has already been checked. An ist made of the output and its |
| 550 | * length (not counting the trailing zero) are returned. |
| 551 | */ |
| 552 | static inline struct ist ist2str_lc(char *dst, const struct ist src, size_t count) |
| 553 | { |
| 554 | size_t ofs = 0; |
| 555 | |
| 556 | /* discourage the compiler from trying to optimize for large strings, |
| 557 | * but tell it that most of our strings are not empty. |
| 558 | */ |
| 559 | if (__builtin_expect(ofs < src.len, 1)) { |
| 560 | do { |
Willy Tarreau | d6735d6 | 2018-12-07 09:40:01 +0100 | [diff] [blame] | 561 | dst[ofs] = ist_lc[(unsigned char)src.ptr[ofs]]; |
Willy Tarreau | 3f2d696 | 2018-12-07 08:35:07 +0100 | [diff] [blame] | 562 | ofs++; |
| 563 | } while (__builtin_expect(ofs < src.len, 0)); |
| 564 | } |
| 565 | dst[ofs] = 0; |
| 566 | return ist2(dst, ofs); |
| 567 | } |
| 568 | |
| 569 | /* makes an upper case copy of the entire <src> into <dst>, which must have |
| 570 | * been allocated large enough to hold the whole contents. No trailing zero is |
| 571 | * appended, this is mainly used for protocol processing where the frame length |
| 572 | * has already been checked. An ist made of the output and its length are |
| 573 | * returned. The destination is not touched if src.len is null. |
| 574 | */ |
| 575 | static inline struct ist ist2bin_uc(char *dst, const struct ist src) |
| 576 | { |
| 577 | size_t ofs = 0; |
| 578 | |
| 579 | /* discourage the compiler from trying to optimize for large strings, |
| 580 | * but tell it that most of our strings are not empty. |
| 581 | */ |
| 582 | if (__builtin_expect(ofs < src.len, 1)) { |
| 583 | do { |
Willy Tarreau | d6735d6 | 2018-12-07 09:40:01 +0100 | [diff] [blame] | 584 | dst[ofs] = ist_uc[(unsigned char)src.ptr[ofs]]; |
Willy Tarreau | 3f2d696 | 2018-12-07 08:35:07 +0100 | [diff] [blame] | 585 | ofs++; |
| 586 | } while (__builtin_expect(ofs < src.len, 0)); |
| 587 | } |
| 588 | return ist2(dst, ofs); |
| 589 | } |
| 590 | |
| 591 | /* makes an upper case copy of the entire <src> into <dst>, which must have been |
| 592 | * allocated large enough to hold the whole contents as well as a trailing zero |
| 593 | * which is always appended. This is mainly used for protocol conversions where |
| 594 | * the frame length has already been checked. An ist made of the output and its |
| 595 | * length (not counting the trailing zero) are returned. |
| 596 | */ |
| 597 | static inline struct ist ist2str_uc(char *dst, const struct ist src, size_t count) |
| 598 | { |
| 599 | size_t ofs = 0; |
| 600 | |
| 601 | /* discourage the compiler from trying to optimize for large strings, |
| 602 | * but tell it that most of our strings are not empty. |
| 603 | */ |
| 604 | if (__builtin_expect(ofs < src.len, 1)) { |
| 605 | do { |
Willy Tarreau | d6735d6 | 2018-12-07 09:40:01 +0100 | [diff] [blame] | 606 | dst[ofs] = ist_uc[(unsigned char)src.ptr[ofs]]; |
Willy Tarreau | 3f2d696 | 2018-12-07 08:35:07 +0100 | [diff] [blame] | 607 | ofs++; |
| 608 | } while (__builtin_expect(ofs < src.len, 0)); |
| 609 | } |
| 610 | dst[ofs] = 0; |
| 611 | return ist2(dst, ofs); |
| 612 | } |
| 613 | |
Willy Tarreau | e11f727 | 2017-05-30 17:49:36 +0200 | [diff] [blame] | 614 | /* looks for first occurrence of character <chr> in string <ist>. Returns the |
| 615 | * pointer if found, or NULL if not found. |
| 616 | */ |
| 617 | static inline char *istchr(const struct ist ist, char chr) |
| 618 | { |
| 619 | char *s = ist.ptr; |
| 620 | |
| 621 | do { |
| 622 | if (s >= ist.ptr + ist.len) |
| 623 | return NULL; |
| 624 | } while (*s++ != chr); |
| 625 | return s - 1; |
| 626 | } |
| 627 | |
Willy Tarreau | 8f3ce06 | 2019-11-22 15:58:53 +0100 | [diff] [blame] | 628 | /* Returns a pointer to the first control character found in <ist>, or NULL if |
| 629 | * none is present. A control character is defined as a byte whose value is |
| 630 | * between 0x00 and 0x1F included. The function is optimized for strings having |
| 631 | * no CTL chars by processing up to sizeof(long) bytes at once on architectures |
| 632 | * supporting efficient unaligned accesses. Despite this it is not very fast |
| 633 | * (~0.43 byte/cycle) and should mostly be used on low match probability when |
| 634 | * it can save a call to a much slower function. |
| 635 | */ |
| 636 | static inline const char *ist_find_ctl(const struct ist ist) |
| 637 | { |
| 638 | const union { unsigned long v; } __attribute__((packed)) *u; |
| 639 | const char *curr = (void *)ist.ptr - sizeof(long); |
| 640 | const char *last = curr + ist.len; |
| 641 | unsigned long l1, l2; |
| 642 | |
| 643 | do { |
| 644 | curr += sizeof(long); |
| 645 | if (curr > last) |
| 646 | break; |
| 647 | u = (void *)curr; |
| 648 | /* subtract 0x202020...20 to the value to generate a carry in |
| 649 | * the lower byte if the byte contains a lower value. If we |
| 650 | * generate a bit 7 that was not there, it means the byte was |
| 651 | * within 0x00..0x1F. |
| 652 | */ |
| 653 | l2 = u->v; |
| 654 | l1 = ~l2 & ((~0UL / 255) * 0x80); /* 0x808080...80 */ |
| 655 | l2 -= (~0UL / 255) * 0x20; /* 0x202020...20 */ |
| 656 | } while ((l1 & l2) == 0); |
| 657 | |
| 658 | last += sizeof(long); |
| 659 | if (__builtin_expect(curr < last, 0)) { |
| 660 | do { |
| 661 | if ((uint8_t)*curr < 0x20) |
| 662 | return curr; |
| 663 | curr++; |
| 664 | } while (curr < last); |
| 665 | } |
| 666 | return NULL; |
| 667 | } |
| 668 | |
Willy Tarreau | e11f727 | 2017-05-30 17:49:36 +0200 | [diff] [blame] | 669 | /* looks for first occurrence of character <chr> in string <ist> and returns |
| 670 | * the tail of the string starting with this character, or (ist.end,0) if not |
| 671 | * found. |
| 672 | */ |
| 673 | static inline struct ist istfind(const struct ist ist, char chr) |
| 674 | { |
| 675 | struct ist ret = ist; |
| 676 | |
| 677 | while (ret.len--) { |
| 678 | if (*ret.ptr++ == chr) |
| 679 | return ist2(ret.ptr - 1, ret.len + 1); |
| 680 | } |
| 681 | return ist2(ret.ptr, 0); |
| 682 | } |
| 683 | |
| 684 | /* looks for first occurrence of character different from <chr> in string <ist> |
| 685 | * and returns the tail of the string starting at this character, or (ist_end,0) |
| 686 | * if not found. |
| 687 | */ |
| 688 | static inline struct ist istskip(const struct ist ist, char chr) |
| 689 | { |
| 690 | struct ist ret = ist; |
| 691 | |
| 692 | while (ret.len--) { |
| 693 | if (*ret.ptr++ != chr) |
| 694 | return ist2(ret.ptr - 1, ret.len + 1); |
| 695 | } |
| 696 | return ist2(ret.ptr, 0); |
| 697 | } |
| 698 | |
| 699 | /* looks for first occurrence of string <pat> in string <ist> and returns the |
| 700 | * tail of the string starting at this position, or (NULL,0) if not found. The |
| 701 | * empty pattern is found everywhere. |
| 702 | */ |
| 703 | static inline struct ist istist(const struct ist ist, const struct ist pat) |
| 704 | { |
| 705 | struct ist ret = ist; |
| 706 | size_t pos; |
| 707 | |
| 708 | if (!pat.len) |
| 709 | return ret; |
| 710 | |
| 711 | while (1) { |
| 712 | loop: |
| 713 | ret = istfind(ret, *pat.ptr); |
| 714 | if (ret.len < pat.len) |
| 715 | break; |
| 716 | |
| 717 | /* ret.len >= 1, pat.len >= 1 and *ret.ptr == *pat.ptr */ |
| 718 | |
| 719 | ret = istnext(ret); |
| 720 | for (pos = 0; pos < pat.len - 1; ) { |
| 721 | ++pos; |
| 722 | if (ret.ptr[pos - 1] != pat.ptr[pos]) |
| 723 | goto loop; |
| 724 | } |
| 725 | return ist2(ret.ptr - 1, ret.len + 1); |
| 726 | } |
Tim Duesterhus | 241e29e | 2020-03-05 17:56:30 +0100 | [diff] [blame] | 727 | return IST_NULL; |
Willy Tarreau | e11f727 | 2017-05-30 17:49:36 +0200 | [diff] [blame] | 728 | } |
| 729 | |
Jerome Magnin | 9dde0b2 | 2020-02-21 10:33:12 +0100 | [diff] [blame] | 730 | /* |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 731 | * looks for the first occurrence of <chr> in string <ist> and returns a shorter |
Jerome Magnin | 9dde0b2 | 2020-02-21 10:33:12 +0100 | [diff] [blame] | 732 | * ist if char is found. |
| 733 | */ |
| 734 | static inline struct ist iststop(const struct ist ist, char chr) |
| 735 | { |
| 736 | size_t len = 0; |
| 737 | |
| 738 | while (len++ < ist.len && ist.ptr[len - 1] != chr) |
| 739 | ; |
| 740 | return ist2(ist.ptr, len - 1); |
| 741 | } |
Tim Duesterhus | 35005d0 | 2020-03-05 17:56:32 +0100 | [diff] [blame] | 742 | |
| 743 | #ifndef IST_FREESTANDING |
| 744 | /* This function allocates <size> bytes and returns an `ist` pointing to |
| 745 | * the allocated area with size `0`. |
| 746 | * |
| 747 | * If this function fails to allocate memory the return value is equivalent |
| 748 | * to IST_NULL. |
| 749 | */ |
| 750 | static inline struct ist istalloc(const size_t size) |
| 751 | { |
| 752 | return ist2(malloc(size), 0); |
| 753 | } |
| 754 | |
| 755 | /* This function performs the equivalent of free() on the given <ist>. |
| 756 | * |
| 757 | * After this function returns the value of the given <ist> will be |
| 758 | * modified to be equivalent to IST_NULL. |
| 759 | */ |
| 760 | static inline void istfree(struct ist *ist) |
| 761 | { |
| 762 | free(ist->ptr); |
| 763 | *ist = IST_NULL; |
| 764 | } |
Tim Duesterhus | 9576ab7 | 2020-03-05 17:56:34 +0100 | [diff] [blame] | 765 | |
| 766 | /* This function performs the equivalent of strdup() on the given <src>. |
| 767 | * |
| 768 | * If this function fails to allocate memory the return value is equivalent |
| 769 | * to IST_NULL. |
| 770 | */ |
| 771 | static inline struct ist istdup(const struct ist src) |
| 772 | { |
| 773 | const size_t src_size = src.len; |
| 774 | |
| 775 | /* Allocate at least 1 byte to allow duplicating an empty string with |
| 776 | * malloc implementations that return NULL for a 0-size allocation. |
| 777 | */ |
| 778 | struct ist dst = istalloc(MAX(src_size, 1)); |
| 779 | |
| 780 | if (isttest(dst)) { |
| 781 | istcpy(&dst, src, src_size); |
| 782 | } |
| 783 | |
| 784 | return dst; |
| 785 | } |
Tim Duesterhus | 35005d0 | 2020-03-05 17:56:32 +0100 | [diff] [blame] | 786 | #endif |
| 787 | |
Willy Tarreau | e11f727 | 2017-05-30 17:49:36 +0200 | [diff] [blame] | 788 | #endif |