blob: 933ec99fb4ccc7cd384f0f306fa4728f1d0fa32f [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
Willy Tarreau9e464962019-10-17 10:38:10 +020036/* makes a buffer pointer from an IST */
37#define b_fromist(in) ({ const struct ist __i = (in); &(const struct buffer){ .area = __i.ptr, .head = 0, .data = __i.len, .size = __i.len}; })
38
Willy Tarreauea1b06d2018-07-12 09:02:47 +020039/* b_isteq() : returns > 0 if the first <n> characters of buffer <b> starting
40 * at offset <o> relative to the buffer's head match <ist>. (empty strings do
41 * match). It is designed to be used with reasonably small strings (it matches
42 * a single byte per loop iteration). It is expected to be used with an offset
43 * to skip old data. For example :
44 * - "input" contents : b_isteq(b, old_cnt, new_cnt, ist);
45 * - "output" contents : b_isteq(b, 0, old_cnt, ist);
46 * Return value :
47 * >0 : the number of matching bytes
48 * =0 : not enough bytes (or matching of empty string)
49 * <0 : non-matching byte found
50 */
51static inline ssize_t b_isteq(const struct buffer *b, size_t o, size_t n, const struct ist ist)
52{
53 struct ist r = ist;
54 const char *p;
55 const char *end = b_wrap(b);
56
57 if (n < r.len)
58 return 0;
59
60 p = b_peek(b, o);
61 while (r.len--) {
62 if (*p++ != *r.ptr++)
63 return -1;
64 if (unlikely(p == end))
65 p = b_orig(b);
66 }
67 return ist.len;
68}
69
Christopher Faulet130cf212019-08-06 16:55:52 +020070/* Same as b_isteq but case-insensitive */
71static inline ssize_t b_isteqi(const struct buffer *b, size_t o, size_t n, const struct ist ist)
72{
73 struct ist r = ist;
74 const char *p;
75 const char *end = b_wrap(b);
76
77 if (n < r.len)
78 return 0;
79
80 p = b_peek(b, o);
81 while (r.len--) {
82 if (*p != *r.ptr &&
83 ist_lc[(unsigned char)*p] != ist_lc[(unsigned char)*r.ptr])
84 return -1;
85 p++;
86 r.ptr++;
87 if (unlikely(p == end))
88 p = b_orig(b);
89 }
90 return ist.len;
91}
92
Willy Tarreauea1b06d2018-07-12 09:02:47 +020093/* b_isteat() : "eats" string <ist> from the head of buffer <b>. Wrapping data
94 * is explicitly supported. It matches a single byte per iteration so strings
95 * should remain reasonably small. Returns :
96 * > 0 : number of bytes matched and eaten
97 * = 0 : not enough bytes (or matching an empty string)
98 * < 0 : non-matching byte found
99 */
100static inline ssize_t b_isteat(struct buffer *b, const struct ist ist)
101{
102 ssize_t ret = b_isteq(b, 0, b_data(b), ist);
103
104 if (ret > 0)
105 b_del(b, ret);
106 return ret;
107}
108
109/* b_istput() : injects string <ist> at the tail of output buffer <b> provided
110 * that it fits. Wrapping is supported. It's designed for small strings as it
111 * only writes a single byte per iteration. Returns the number of characters
112 * copied (ist.len), 0 if it temporarily does not fit, or -1 if it will never
113 * fit. It will only modify the buffer upon success. In all cases, the contents
114 * are copied prior to reporting an error, so that the destination at least
115 * contains a valid but truncated string.
116 */
117static inline ssize_t b_istput(struct buffer *b, const struct ist ist)
118{
119 const char *end = b_wrap(b);
120 struct ist r = ist;
121 char *p;
122
123 if (r.len > (size_t)b_room(b))
124 return r.len < b->size ? 0 : -1;
125
126 p = b_tail(b);
Willy Tarreaubd1dba82018-07-10 10:43:27 +0200127 b->data += r.len;
Willy Tarreauea1b06d2018-07-12 09:02:47 +0200128 while (r.len--) {
129 *p++ = *r.ptr++;
130 if (unlikely(p == end))
131 p = b_orig(b);
132 }
133 return ist.len;
134}
135
136/* b_putist() : tries to copy as much as possible of string <ist> into buffer
137 * <b> and returns the number of bytes copied (truncation is possible). It uses
138 * b_putblk() and is suitable for large blocks.
139 */
140static inline size_t b_putist(struct buffer *b, const struct ist ist)
141{
142 return b_putblk(b, ist.ptr, ist.len);
143}
144
145#endif /* _COMMON_ISTBUF_H */
146
147/*
148 * Local variables:
149 * c-indent-level: 8
150 * c-basic-offset: 8
151 * End:
152 */