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