1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
/*
* include/haproxy/istbuf.h
* Functions used to manipulate indirect strings with wrapping buffers.
*
* Copyright (C) 2000-2020 Willy Tarreau - w@1wt.eu
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef _HAPROXY_ISTBUF_H
#define _HAPROXY_ISTBUF_H
#include <sys/types.h>
#include <import/ist.h>
#include <haproxy/buf.h>
/* b_isteq() : returns > 0 if the first <n> characters of buffer <b> starting
* at offset <o> relative to the buffer's head match <ist>. (empty strings do
* match). It is designed to be used with reasonably small strings (it matches
* a single byte per loop iteration). It is expected to be used with an offset
* to skip old data. For example :
* - "input" contents : b_isteq(b, old_cnt, new_cnt, ist);
* - "output" contents : b_isteq(b, 0, old_cnt, ist);
* Return value :
* >0 : the number of matching bytes
* =0 : not enough bytes (or matching of empty string)
* <0 : non-matching byte found
*/
static inline ssize_t b_isteq(const struct buffer *b, size_t o, size_t n, const struct ist ist)
{
struct ist r = ist;
const char *p;
const char *end = b_wrap(b);
if (n < r.len)
return 0;
p = b_peek(b, o);
while (r.len--) {
if (*p++ != *r.ptr++)
return -1;
if (unlikely(p == end))
p = b_orig(b);
}
return ist.len;
}
/* Same as b_isteq but case-insensitive */
static inline ssize_t b_isteqi(const struct buffer *b, size_t o, size_t n, const struct ist ist)
{
struct ist r = ist;
const char *p;
const char *end = b_wrap(b);
if (n < r.len)
return 0;
p = b_peek(b, o);
while (r.len--) {
if (*p != *r.ptr &&
ist_lc[(unsigned char)*p] != ist_lc[(unsigned char)*r.ptr])
return -1;
p++;
r.ptr++;
if (unlikely(p == end))
p = b_orig(b);
}
return ist.len;
}
/* b_isteat() : "eats" string <ist> from the head of buffer <b>. Wrapping data
* is explicitly supported. It matches a single byte per iteration so strings
* should remain reasonably small. Returns :
* > 0 : number of bytes matched and eaten
* = 0 : not enough bytes (or matching an empty string)
* < 0 : non-matching byte found
*/
static inline ssize_t b_isteat(struct buffer *b, const struct ist ist)
{
ssize_t ret = b_isteq(b, 0, b_data(b), ist);
if (ret > 0)
b_del(b, ret);
return ret;
}
/* b_istput() : injects string <ist> at the tail of output buffer <b> provided
* that it fits. Wrapping is supported. It's designed for small strings as it
* only writes a single byte per iteration. Returns the number of characters
* copied (ist.len), 0 if it temporarily does not fit, or -1 if it will never
* fit. It will only modify the buffer upon success. In all cases, the contents
* are copied prior to reporting an error, so that the destination at least
* contains a valid but truncated string.
*/
static inline ssize_t b_istput(struct buffer *b, const struct ist ist)
{
const char *end = b_wrap(b);
struct ist r = ist;
char *p;
if (r.len > (size_t)b_room(b))
return r.len < b->size ? 0 : -1;
p = b_tail(b);
b->data += r.len;
while (r.len--) {
*p++ = *r.ptr++;
if (unlikely(p == end))
p = b_orig(b);
}
return ist.len;
}
/* b_putist() : tries to copy as much as possible of string <ist> into buffer
* <b> and returns the number of bytes copied (truncation is possible). It uses
* b_putblk() and is suitable for large blocks.
*/
static inline size_t b_putist(struct buffer *b, const struct ist ist)
{
return b_putblk(b, ist.ptr, ist.len);
}
/* builds and return a <struct buffer> based on <ist>
*/
static inline struct buffer ist2buf(const struct ist ist)
{
struct buffer buf;
buf.area = ist.ptr;
buf.size = ist.len;
buf.data = ist.len;
buf.head = 0;
return buf;
}
#endif /* _HAPROXY_ISTBUF_H */
/*
* Local variables:
* c-indent-level: 8
* c-basic-offset: 8
* End:
*/
|