Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1 | /* |
| 2 | * include/common/istbuf.h |
| 3 | * Functions used to manipulate indirect strings with wrapping buffers. |
| 4 | * |
| 5 | * Copyright (C) 2000-2018 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_ISTBUF_H |
| 29 | #define _COMMON_ISTBUF_H |
| 30 | |
| 31 | #include <stdint.h> |
| 32 | #include <common/buf.h> |
| 33 | #include <common/ist.h> |
| 34 | |
| 35 | |
| 36 | /* b_isteq() : returns > 0 if the first <n> characters of buffer <b> starting |
| 37 | * at offset <o> relative to the buffer's head match <ist>. (empty strings do |
| 38 | * match). It is designed to be used with reasonably small strings (it matches |
| 39 | * a single byte per loop iteration). It is expected to be used with an offset |
| 40 | * to skip old data. For example : |
| 41 | * - "input" contents : b_isteq(b, old_cnt, new_cnt, ist); |
| 42 | * - "output" contents : b_isteq(b, 0, old_cnt, ist); |
| 43 | * Return value : |
| 44 | * >0 : the number of matching bytes |
| 45 | * =0 : not enough bytes (or matching of empty string) |
| 46 | * <0 : non-matching byte found |
| 47 | */ |
| 48 | static inline ssize_t b_isteq(const struct buffer *b, size_t o, size_t n, const struct ist ist) |
| 49 | { |
| 50 | struct ist r = ist; |
| 51 | const char *p; |
| 52 | const char *end = b_wrap(b); |
| 53 | |
| 54 | if (n < r.len) |
| 55 | return 0; |
| 56 | |
| 57 | p = b_peek(b, o); |
| 58 | while (r.len--) { |
| 59 | if (*p++ != *r.ptr++) |
| 60 | return -1; |
| 61 | if (unlikely(p == end)) |
| 62 | p = b_orig(b); |
| 63 | } |
| 64 | return ist.len; |
| 65 | } |
| 66 | |
| 67 | /* b_isteat() : "eats" string <ist> from the head of buffer <b>. Wrapping data |
| 68 | * is explicitly supported. It matches a single byte per iteration so strings |
| 69 | * should remain reasonably small. Returns : |
| 70 | * > 0 : number of bytes matched and eaten |
| 71 | * = 0 : not enough bytes (or matching an empty string) |
| 72 | * < 0 : non-matching byte found |
| 73 | */ |
| 74 | static inline ssize_t b_isteat(struct buffer *b, const struct ist ist) |
| 75 | { |
| 76 | ssize_t ret = b_isteq(b, 0, b_data(b), ist); |
| 77 | |
| 78 | if (ret > 0) |
| 79 | b_del(b, ret); |
| 80 | return ret; |
| 81 | } |
| 82 | |
| 83 | /* b_istput() : injects string <ist> at the tail of output buffer <b> provided |
| 84 | * that it fits. Wrapping is supported. It's designed for small strings as it |
| 85 | * only writes a single byte per iteration. Returns the number of characters |
| 86 | * copied (ist.len), 0 if it temporarily does not fit, or -1 if it will never |
| 87 | * fit. It will only modify the buffer upon success. In all cases, the contents |
| 88 | * are copied prior to reporting an error, so that the destination at least |
| 89 | * contains a valid but truncated string. |
| 90 | */ |
| 91 | static inline ssize_t b_istput(struct buffer *b, const struct ist ist) |
| 92 | { |
| 93 | const char *end = b_wrap(b); |
| 94 | struct ist r = ist; |
| 95 | char *p; |
| 96 | |
| 97 | if (r.len > (size_t)b_room(b)) |
| 98 | return r.len < b->size ? 0 : -1; |
| 99 | |
| 100 | p = b_tail(b); |
Willy Tarreau | bd1dba8 | 2018-07-10 10:43:27 +0200 | [diff] [blame] | 101 | b->data += r.len; |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 102 | while (r.len--) { |
| 103 | *p++ = *r.ptr++; |
| 104 | if (unlikely(p == end)) |
| 105 | p = b_orig(b); |
| 106 | } |
| 107 | return ist.len; |
| 108 | } |
| 109 | |
| 110 | /* b_putist() : tries to copy as much as possible of string <ist> into buffer |
| 111 | * <b> and returns the number of bytes copied (truncation is possible). It uses |
| 112 | * b_putblk() and is suitable for large blocks. |
| 113 | */ |
| 114 | static inline size_t b_putist(struct buffer *b, const struct ist ist) |
| 115 | { |
| 116 | return b_putblk(b, ist.ptr, ist.len); |
| 117 | } |
| 118 | |
| 119 | #endif /* _COMMON_ISTBUF_H */ |
| 120 | |
| 121 | /* |
| 122 | * Local variables: |
| 123 | * c-indent-level: 8 |
| 124 | * c-basic-offset: 8 |
| 125 | * End: |
| 126 | */ |