blob: 9b6ce7c6bdf4ea9918225fedf56473297c78d843 [file] [log] [blame]
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001/*
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
Willy Tarreaua1bd1fa2019-03-29 17:26:33 +010031#include <inttypes.h>
Willy Tarreauea1b06d2018-07-12 09:02:47 +020032#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 */
48static 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
Christopher Faulet130cf212019-08-06 16:55:52 +020067/* Same as b_isteq but case-insensitive */
68static inline ssize_t b_isteqi(const struct buffer *b, size_t o, size_t n, const struct ist ist)
69{
70 struct ist r = ist;
71 const char *p;
72 const char *end = b_wrap(b);
73
74 if (n < r.len)
75 return 0;
76
77 p = b_peek(b, o);
78 while (r.len--) {
79 if (*p != *r.ptr &&
80 ist_lc[(unsigned char)*p] != ist_lc[(unsigned char)*r.ptr])
81 return -1;
82 p++;
83 r.ptr++;
84 if (unlikely(p == end))
85 p = b_orig(b);
86 }
87 return ist.len;
88}
89
Willy Tarreauea1b06d2018-07-12 09:02:47 +020090/* b_isteat() : "eats" string <ist> from the head of buffer <b>. Wrapping data
91 * is explicitly supported. It matches a single byte per iteration so strings
92 * should remain reasonably small. Returns :
93 * > 0 : number of bytes matched and eaten
94 * = 0 : not enough bytes (or matching an empty string)
95 * < 0 : non-matching byte found
96 */
97static inline ssize_t b_isteat(struct buffer *b, const struct ist ist)
98{
99 ssize_t ret = b_isteq(b, 0, b_data(b), ist);
100
101 if (ret > 0)
102 b_del(b, ret);
103 return ret;
104}
105
106/* b_istput() : injects string <ist> at the tail of output buffer <b> provided
107 * that it fits. Wrapping is supported. It's designed for small strings as it
108 * only writes a single byte per iteration. Returns the number of characters
109 * copied (ist.len), 0 if it temporarily does not fit, or -1 if it will never
110 * fit. It will only modify the buffer upon success. In all cases, the contents
111 * are copied prior to reporting an error, so that the destination at least
112 * contains a valid but truncated string.
113 */
114static inline ssize_t b_istput(struct buffer *b, const struct ist ist)
115{
116 const char *end = b_wrap(b);
117 struct ist r = ist;
118 char *p;
119
120 if (r.len > (size_t)b_room(b))
121 return r.len < b->size ? 0 : -1;
122
123 p = b_tail(b);
Willy Tarreaubd1dba82018-07-10 10:43:27 +0200124 b->data += r.len;
Willy Tarreauea1b06d2018-07-12 09:02:47 +0200125 while (r.len--) {
126 *p++ = *r.ptr++;
127 if (unlikely(p == end))
128 p = b_orig(b);
129 }
130 return ist.len;
131}
132
133/* b_putist() : tries to copy as much as possible of string <ist> into buffer
134 * <b> and returns the number of bytes copied (truncation is possible). It uses
135 * b_putblk() and is suitable for large blocks.
136 */
137static inline size_t b_putist(struct buffer *b, const struct ist ist)
138{
139 return b_putblk(b, ist.ptr, ist.len);
140}
141
142#endif /* _COMMON_ISTBUF_H */
143
144/*
145 * Local variables:
146 * c-indent-level: 8
147 * c-basic-offset: 8
148 * End:
149 */